📄 slice.c
字号:
/*COPYRIGHT, LICENSE AND WARRANTY INFORMATIONThis software module has been originally developed by Nokia Corporation. Provided that a person, entity or a company willing to use the Software (hereinafter Licensee) comply with all the terms and conditions of this Statement and subject to the limitations set forth in this Statement Nokia grants to such Licensee a non-exclusive, sub-licensable, worldwide, limited license under copyrights owned by Nokia to use the Software for the sole purpose of creating, manufacturing, selling, marketing, or distributing (including the right to make modifications to the Software) a fully compliant decoder implementation (hereinafter "Decoder") of ITU-T Recommendation H.264 / ISO/IEC International Standard 14496-10 and an encoder implementation producing output that is decodable with the Decoder.Nokia retains the ownership of copyrights to the Software. There is no patent nor other intellectual property right of Nokia licensed under this Statement (except the copyright license above). Licensee hereby assumes sole responsibility to secure any other intellectual property rights needed, if any. For example, if patent licenses are required, it is their responsibility to acquire the license before utilizing the Software.The license by Nokia is subject to that the Licensee grants to Nokia the non-exclusive, worldwide, royalty-free, perpetual and irrevocable covenant that the Licensee(s) shall not bring a suit before any court or administrative agency or otherwise assert a claim for infringement under the Licensee intellectual property rights that, but for a license, would be infringed by the Software against (a) Nokia or Nokia's Affiliate; or (b) other recipient of a license and covenant not to sue with respect to the Software from Nokia; or (c) contractor, customer or distributor of a party listed above in a or b, which suit or claim is related to the Software or use thereof.The Licensee(s) further agrees to grant a reciprocal license to Nokia (as granted by Nokia to the Licensee(s) on the modifications made by Licensee(s) to the Software. THE SOFTWARE IS PROVIDED "AS IS" AND THE ORIGINAL DEVELOPER DISCLAIMS ANY AND ALL WARRANTIES WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. THOSE INTENDING TO USE THE SOFTWARE ARE EXPRESSLY ADVISED THAT ITS USE MAY INFRINGE EXISTING PATENTS AND BE SUBJECT TO ROYALTY PAYMENTS TO PATENT OWNERS. ANYONE USING THE SOFTWARE ON THE BASIS OF THIS LICENSE AGREES TO OBTAIN THE NECESSARY PERMISSIONS FROM ANY AND ALL APPLICABLE PATENT OWNERS FOR SUCH USE.IN NO EVENT SHALL THE ORIGINAL DEVELOPER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.This copyright, license and warranty information notice must be retained in all copies and derivative works of the Software or substantial portions thereof.*/#include <math.h>#include <string.h>#include "nccglob.h"#include "globals.h"#include "debug.h"#include "bitbuffer.h"#include "vlcutility.h"#include "macroblock.h"#include "slice.h"#include "refframe.h"#include <limits.h>#include "stream.h"#include "ratecontrol.h"static int sliceSendRefListReordering(dpbBuf_s *dpbBuf, bitbuffer_s *bitbuf){ int i, bits, numCmds; int refPicListReorderingFlagL0; // let's check whether it is necessary to send re-order command numCmds = dpbGetReorderCommands(dpbBuf); /* encode the reordering commands */ refPicListReorderingFlagL0 = (numCmds != 0); bits = u_n(bitbuf, refPicListReorderingFlagL0, 1); if (refPicListReorderingFlagL0) { /* TODO */ for (i = 0; i < numCmds; i++) { bits += ue_v(bitbuf, dpbBuf->reorderCmds[i].idc); if (dpbBuf->reorderCmds[i].idc == 0 || dpbBuf->reorderCmds[i].idc == 1) bits += ue_v(bitbuf, dpbBuf->reorderCmds[i].absDiffPicNumMinus1); else if (dpbBuf->reorderCmds[i].idc == 2) bits += ue_v(bitbuf, dpbBuf->reorderCmds[i].longTermPicNum); } // end of the re-order command list bits += ue_v(bitbuf, 3); } return bits;}static int encRefPicMarking(slice_s *slice, MMCO_s *mmcoList, int idrLongTermRefFlag, bitbuffer_s *bitbuf){ int bits = 0; int noOutputOfPriorPicsFlag, adaptiveRefMarkingModeFlag; noOutputOfPriorPicsFlag = 0; /* Decoded reference picture marking process will be delayed after all the slices are decoded */ if (slice->isIDR) { bits += u_n(bitbuf, noOutputOfPriorPicsFlag, 1); bits += u_n(bitbuf, idrLongTermRefFlag, 1); } else { /* non-IDR */ adaptiveRefMarkingModeFlag = 0; if (mmcoList->mmco != 0) adaptiveRefMarkingModeFlag = 1; bits += u_n(bitbuf, adaptiveRefMarkingModeFlag, 1); if (adaptiveRefMarkingModeFlag) { do { bits += ue_v(bitbuf, mmcoList->mmco); if (mmcoList->mmco == 1 || mmcoList->mmco == 3) bits += ue_v(bitbuf, mmcoList->diffPicNumMinus1); if (mmcoList->mmco == 2) bits += ue_v(bitbuf, mmcoList->longTermPicNum); if (mmcoList->mmco == 3 || mmcoList->mmco == 6) bits += ue_v(bitbuf, mmcoList->longTermFrmIdx); if (mmcoList->mmco == 4) bits += ue_v(bitbuf, mmcoList->maxLongTermFrmIdxPlus1); if (mmcoList->mmco != 0) { mmcoList = mmcoList->next; } else mmcoList = 0; } while (mmcoList != 0); } } return bits;}/* * startSlice * * Parameters: * slice Slice coding structure * sps Sequence parameter set * pps Picture parameter set * bitsNal Pointer to counter accumulating NAL bits * * Function: * Send slice header information. * * Returns: * Length of the slice header in bits. * */static int startSlice(slice_s *slice, encParams_s *encPar, int firstMbAddr, dpbBuf_s *dpbBuf, seq_parameter_set_s *sps, pic_parameter_set_s *pps, int *bitsNal){ int i, bits; int deltaQp, value; int expectedPicOrderCnt; int picOrderCntCycleCnt; int expectedDeltaPerPicOrderCntCycle; int frameNumInPicOrderCntCycle; int absFrameNum; int temp; int len1; bitbuffer_s *bitbuf; bitbuf = slice->bitbuf; bits = 0; picOrderCntCycleCnt = 0; frameNumInPicOrderCntCycle = 0; /* Do not take the bits prior to the starting code into account */ /* Nor the naltype byte */ /* 1 zero byte is always present */ *bitsNal += vlcuSendNalUnitStartCodePrefix(slice->bitbuf, 1); if (slice->isIDR == 1) *bitsNal += vlcuSendNalHead(slice->bitbuf, slice->nalRefIdc, CODED_SLICE_IDR_NAL_TYPE); else *bitsNal += vlcuSendNalHead(slice->bitbuf, slice->nalRefIdc, CODED_SLICE_NAL_TYPE); /* first_mb_in_slice */ bits += ue_v(bitbuf, firstMbAddr); /* slice_type */ bits += ue_v(bitbuf, slice->sliceType); /* pps id */ bits += ue_v(bitbuf, 0); /* frame_num */ bits += u_n(bitbuf, dpbBuf->frameNum, encPar->log2_max_frame_num_minus4+4); /* idr pic id */ if (slice->isIDR) { bits += ue_v(bitbuf, slice->idrPicId); } /* POC: Picture order count */ if (sps->pic_order_cnt_type == 0) { unsigned int mask; /* pic_order_cnt_lsb */ mask = (1 << (sps->log2_max_pic_order_cnt_lsb_minus4 + 4)) - 1; value = (slice->poc & mask); bits += u_n(bitbuf, value, sps->log2_max_pic_order_cnt_lsb_minus4+4); } else if (sps->pic_order_cnt_type == 1 && !sps->delta_pic_order_always_zero_flag) { /* Reset prevFrameNumOffset if needed */ if (slice->isIDR || dpbBuf->prevPicHasMMCO5) { /* Todo: prevPicHasMMCO5 has not been tested. */ dpbBuf->prevFrameNumOffset = 0; } /* frameNumOffset is derived as follows: */ if (slice->isIDR) dpbBuf->frameNumOffset = 0; else if (dpbBuf->prevRefFrameNum > dpbBuf->frameNum) dpbBuf->frameNumOffset = dpbBuf->prevFrameNumOffset + dpbBuf->maxFrameNum; else dpbBuf->frameNumOffset = dpbBuf->prevFrameNumOffset; /* absFrameNum is derived as follows: */ if (sps->num_ref_frames_in_pic_order_cnt_cycle != 0) absFrameNum = dpbBuf->frameNumOffset + dpbBuf->frameNum; else absFrameNum = 0; if (slice->nalRefIdc == 0 && absFrameNum > 0) absFrameNum = absFrameNum - 1; /* When absFrameNum > 0, picOrderCntCycleCnt and frameNumInPicOrderCntCycle are derived as follows */ if (absFrameNum > 0) { picOrderCntCycleCnt = (absFrameNum - 1) / sps->num_ref_frames_in_pic_order_cnt_cycle; frameNumInPicOrderCntCycle = (absFrameNum - 1) % sps->num_ref_frames_in_pic_order_cnt_cycle; } /* expectedDeltaPerPicOrderCntCycle */ expectedDeltaPerPicOrderCntCycle = 0; for (i = 0; i < (int)sps->num_ref_frames_in_pic_order_cnt_cycle; i++) expectedDeltaPerPicOrderCntCycle += sps->offset_for_ref_frame[i]; /* expectedPicOrderCnt */ if (absFrameNum > 0) { expectedPicOrderCnt = picOrderCntCycleCnt * expectedDeltaPerPicOrderCntCycle; for (i = 0; i <= frameNumInPicOrderCntCycle; i++) expectedPicOrderCnt = expectedPicOrderCnt + sps->offset_for_ref_frame[i]; } else expectedPicOrderCnt = 0; if (slice->nalRefIdc == 0) expectedPicOrderCnt = expectedPicOrderCnt + sps->offset_for_non_ref_pic; /* delta_pic_order_cnt[ 0 ] */ value = slice->poc - expectedPicOrderCnt; bits += se_v(bitbuf, value); } /* reference picture management */ if (IS_SLICE_P(slice->sliceType)) { int numRefIdxActiveOverrideFlag; // these should be dynamically determined numRefIdxActiveOverrideFlag = ((int) (pps->num_ref_idx_l0_active_minus1 + 1) > dpbBuf->numRefFrms) ? 1 : 0; bits += u_n(bitbuf, numRefIdxActiveOverrideFlag, 1); if (numRefIdxActiveOverrideFlag) bits += ue_v(bitbuf, dpbBuf->numRefFrms - 1); } /* send reference frame reordering syntax elements */ if (! IS_SLICE_I(slice->sliceType)) bits += sliceSendRefListReordering(dpbBuf, bitbuf); /* ref pic marking, including writing MMCO commands if any. */ if (slice->nalRefIdc) { bits += encRefPicMarking(slice, dpbBuf->mmcoList, dpbBuf->ltrIdr, bitbuf); } /* slice_qp_delta */ deltaQp = slice->qp - pps->pic_init_qp_minus26 - 26; bits += se_v(bitbuf, deltaQp); /* deblocking filter */ if (pps->deblocking_filter_parameters_present_flag == 1) { bits += ue_v(bitbuf, encPar->filterMode); // ue_v(bitbuf, &slice->disableDeblockingFilterIdc); if (encPar->filterMode != 1) { bits += se_v(bitbuf, encPar->alphaOffset/2); bits += se_v(bitbuf, encPar->betaOffset/2); } } /* macroblock to slice group map */ if ((pps->num_slice_groups_minus1 > 0) && (pps->slice_group_map_type >= 3) && (pps->slice_group_map_type <= 5)) { int picSizeInMapUnits; picSizeInMapUnits = slice->mbksPerCol * slice->mbksPerLine; // find minimal # of bits to store maximal possible sliceGroupChangeCycle // slightly different from the original implementation temp = picSizeInMapUnits/(pps->slice_group_change_rate_minus1 + 1); len1 = 0; do { len1 ++; temp >>= 1; } while (temp); /* read slice_group_change_cycle */ bits += u_n(bitbuf, slice->sliceGroupChangeCycle, len1); } return bits;}/* * sliceGenerateMbMap * * Parameters: * slice Slice coding structure * pps Picture parameter set * * Function: * Generate the slice group maps from the parameters stored in picture * parameter set. * * Returns: * - * */void sliceGenerateMbMap(slice_s *slice, pic_parameter_set_s *pps){ int i, j, k; int x, y; int iGroup; int picSizeInMapUnits, picWidthInMbs, picHeightInMapUnits; int mapUnitsInSliceGroup0; int sizeOfUpperLeftGroup; mbState_s *pMbStateArr; pMbStateArr = slice->mbStateArr; for (i = 0; i < slice->picSizeInMbs; i ++) { // mbType is used to indicate whether an MB has been encoded pMbStateArr[i].mbType = -1; pMbStateArr[i].sliceMap = 0; } /* macroblock to slice group map */ if (pps->num_slice_groups_minus1 > 0) { picSizeInMapUnits = slice->mbksPerCol * slice->mbksPerLine; picWidthInMbs = slice->mbksPerLine; picHeightInMapUnits = slice->mbksPerCol; // mapUnitsInSliceGroup0 only used in slice_group_map_type 3, 4, 5
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -