?? sequence.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 "globals.h"#include "debug.h"#include "nccglob.h"#include "vlcutility.h"#include "frame.h"#include "sequence.h"#include "avcencoder.h"#include "parameterset.h"#include "dpb.h"/* * Definitions of SEI types*/#define SEI_TYPE_SCENE_INFO 9#define MIN_CHROMA_QP_INDEX -12#define MAX_CHROMA_QP_INDEX 12#define MIN_ALPHA_BETA_OFFSET -6#define MAX_ALPHA_BETA_OFFSET 6#if PRINT_DETAILED_SEQ_STATISTICSstatic void printIntraStat(statSlice_s *intraStat, FILE *fp);static void printInterStat(statSlice_s *interStat, FILE *fp);#endifstatic void getStat(avceStatistics_s *retStat, statSlice_s *stat);void calculateBufOffs(int bufWidth);/** seqLoad** Parameters:* seq Sequence parameters** Function:* Initializes sequence constants* * Returns:* -*/void avceLoad(){ frmLoad();}/** seqOpen** Parameters:* param Parameters for sequence** Function:* Initializes sequence by opening files and allocating memory for* framebuffers and other buffers* * Returns:* Pointer to encoder instance or NULL if error.*/avceEncoder_t *avceOpen(avceOpenParams_s *param,void **nalBuffer, int *nalBufSize){ sequence_s *seq; encParams_s *pEncPar; int bitBufInitialSize; int numMbsPerFrame; int maxCPBSize=10000000/8; int counter; int i; param->entropyEncoder = ENTROPY_CAVLC; if ((seq = nccMalloc(sizeof(sequence_s))) == NULL) return NULL; /* * Initialize sequence variables */ seq->lastIdrTr = 0; seq->nextIdrPicId = 0; seq->encPar = *param; // initialize the encoding parameter structure pEncPar = & seq->encPar; numMbsPerFrame = (pEncPar->picWidth/16) * (pEncPar->picHeight/16); /* If level is 3.1 or higher, we can't use 4x4 motion blocks because */ /* we might exceed level limit (max. number of motion vector for two */ /* consecutive macroblocks is 16 for level 3.1 and higher). */ if (pEncPar->level >= 31) pEncPar->modeFlags = pEncPar->modeFlags & 0x3f; pEncPar->filterMode = clip(0, 2, pEncPar->filterMode); pEncPar->chromaQpIdx = clip(MIN_CHROMA_QP_INDEX, MAX_CHROMA_QP_INDEX, pEncPar->chromaQpIdx); pEncPar->alphaOffset = 2 * clip(MIN_ALPHA_BETA_OFFSET, MAX_ALPHA_BETA_OFFSET, pEncPar->alphaOffset); pEncPar->betaOffset = 2 * clip(MIN_ALPHA_BETA_OFFSET, MAX_ALPHA_BETA_OFFSET, pEncPar->betaOffset); pEncPar->maxNumRefFrms = clip(0, FRM_MAX_REF_FRAMES, pEncPar->maxNumRefFrms); pEncPar->numReorderFrames = min(pEncPar->numReorderFrames, pEncPar->maxNumRefFrms); pEncPar->range = clip(4, 64, pEncPar->range); pEncPar->log2_max_frame_num_minus4 = clip(0, 12, pEncPar->log2_max_frame_num_minus4); pEncPar->nRandomIntraRefresh = clip(0, numMbsPerFrame, pEncPar->nRandomIntraRefresh); pEncPar->numNonref = param->numNonref; // Forced Intra Refresh seq->forcedIRNo = FORCE_INTRA_FREQUENCY; seq->forcedIR = (int*) nccMalloc(sizeof(int)*numMbsPerFrame); counter = seq->forcedIRNo; for (i = 0; i < numMbsPerFrame; i++) { seq->forcedIR[i] = counter--; if (counter < 0) counter = seq->forcedIRNo; } ///// LOW_COMPLEX_PROF3 if (pEncPar->profile == PROF_CODING_LC3) { pEncPar->profile = PROF_CODING_SPEED; pEncPar->low_complex_prof3 = 1; pEncPar->spec_search_pos = ( pEncPar->range == LC3_RANGE ) ; } ///// /* * Initialize frame buffer and reference frame buffers */ frmOpen(& seq->recoFrm, pEncPar->picWidth, pEncPar->picHeight, pEncPar->plr); frmEncBufOpen(& seq->mbData, pEncPar->picWidth, pEncPar->picHeight); seq->sliceGroupCycle = 0; seq->sps.level_idc = pEncPar->level; switch(pEncPar->level){ case 10: if(numMbsPerFrame>99) { if(numMbsPerFrame>792) { seq->sps.level_idc = 30; maxCPBSize = 10000000/8; } else if(numMbsPerFrame>396) { seq->sps.level_idc = 21; maxCPBSize = 4000000/8; } else { seq->sps.level_idc = 20; maxCPBSize = 1000000/8; } } else maxCPBSize = 175000/8; break; case 11: if(numMbsPerFrame>396) { if(numMbsPerFrame>792) { seq->sps.level_idc = 30; maxCPBSize = 10000000/8; } else { seq->sps.level_idc = 20; maxCPBSize = 1000000/8; } } else maxCPBSize = 500000/8; break; case 12: if(numMbsPerFrame>396) { if(numMbsPerFrame>792) { seq->sps.level_idc = 30; maxCPBSize = 10000000/8; } else { seq->sps.level_idc = 20; maxCPBSize = 1000000/8; } } else maxCPBSize = 1000000/8; break; case 13: if(numMbsPerFrame>396) { if(numMbsPerFrame>792) { seq->sps.level_idc = 30; maxCPBSize = 10000000/8; } else { seq->sps.level_idc = 20; maxCPBSize = 1000000/8; } } else maxCPBSize = 2000000/8; break; case 20: if(numMbsPerFrame>396) { if(numMbsPerFrame>792) { seq->sps.level_idc = 30; maxCPBSize = 10000000/8; } else { seq->sps.level_idc = 20; maxCPBSize = 1000000/8; } } else maxCPBSize = 2000000/8; break; case 21: if(numMbsPerFrame>792) { seq->sps.level_idc = 30; maxCPBSize = 10000000/8; } else maxCPBSize = 4000000/8; break; case 22: maxCPBSize = 4000000/8; break; case 30: maxCPBSize = 10000000/8; break; } if(seq->encPar.brcBufSize == 0) seq->encPar.brcBufSize = maxCPBSize; else { if(seq->encPar.brcBufSize > maxCPBSize) { fprintf(stdout,"Buffer Size is larger than MaxCPB defined in the specified level..!!\n"); seq->encPar.brcBufSize = maxCPBSize; } if(seq->encPar.brcBufSize*8.0/seq->encPar.brcBitRate < 0.2 && seq->encPar.brcBitRate > 0 ) { fprintf(stdout,"Buffer Size is too low. Increasing to 0.2 seconds..!\n"); seq->encPar.brcBufSize = (int)(seq->encPar.brcBitRate*0.2/8); } } rc_init_seq(&seq->bRC, & seq->encPar); if(seq->encPar.brcBitRate!= 0) { if(seq->encPar.useSEIMessages) { fillHRDParameters(&seq->bRC, &seq->sps.vui_parameters.vcl_hrd_parameters); fillBufferingPeriodSEI(&seq->bRC); } } // initialize dpb buffer dpbInitialize(& seq->dpbBuf, pEncPar); /* * Open bitbuffer. Decide bitbuffer initial size */ /* Initial bit buffer size is frame_width*frame_heigth/4 rounded up to nearest kilobyte */ bitBufInitialSize = 3*(pEncPar->picWidth * pEncPar->picHeight/8); bibOpen(& seq->bitbuf, bitBufInitialSize); /* * Clear statistics */ staClear(& seq->intraStat); staClear(& seq->interStat); staNonSliceClear(&seq->nonSliceStat); /* * Initialize the Parameter Set. */ // do additional check on parameters for slice definition psValidateSliceParams(pEncPar); psFillParameterSetStructures(& seq->sps, & seq->pps, pEncPar); /* If this is baseline/extended profile and level is <= 3.0, */ /* we must enable sub-macroblock rect size checking */ if ((seq->sps.profile_idc == PS_BASELINE_PROFILE_IDC || seq->sps.profile_idc != PS_EXTENDED_PROFILE_IDC) && seq->sps.level_idc <= 30) seq->slice.meProfile.subMbRectSizeFlag = 1; else seq->slice.meProfile.subMbRectSizeFlag = 0; /* Set maximum vertical motion vector range according to level limitation */ if (seq->sps.level_idc <= 10) seq->slice.meProfile.maxVerticalMvRange = 64; else if (seq->sps.level_idc <= 20) seq->slice.meProfile.maxVerticalMvRange = 128; else if (seq->sps.level_idc <= 30) seq->slice.meProfile.maxVerticalMvRange = 256; else seq->slice.meProfile.maxVerticalMvRange = 512; // send the picture parameter set to the bitbuffer seq->nonSliceStat.bitsSPS += psSendSeqParameterSet (& seq->bitbuf, & seq->sps, & seq->nonSliceStat.bitsNAL); seq->nonSliceStat.bitsPPS += psSendPicParameterSet (& seq->bitbuf, & seq->pps, & seq->nonSliceStat.bitsNAL); if(param->useSEIMessages) seq->nonSliceStat.bitsSEI += sendBufferingPeriodSEIMessage(&seq->bitbuf,&seq->bRC.bufperiodSEI,&seq->sps.vui_parameters.vcl_hrd_parameters, &seq->nonSliceStat.bitsNAL); seq->slice.mbksPerLine = (int16) (seq->encPar.picWidth/MBK_SIZE); seq->slice.mbksPerCol = (int16) (seq->encPar.picHeight/MBK_SIZE); seq->slice.picSizeInMbs = (int16) (seq->slice.mbksPerLine * seq->slice.mbksPerCol); seq->slice.mbStateArr = seq->mbData.mbStateArr; seq->slice.bitbuf = & seq->bitbuf; seq->slice.recoBuf = & seq->recoFrm; mbkInit(& seq->slice.mb, seq->encPar.plr); if (pEncPar->maxNumRefFrms > 0) { int yBufWidth; // width of the reference frame, bufferfor luma component yBufWidth = seq->dpbBuf.refFrmArr[0].yBufWidth; calculateBufOffs(yBufWidth); } *nalBufSize = bibGetBuffer(&seq->bitbuf, nalBuffer); /* Reset bit buffer to empty state. This does not delete contents of bit buffer */ bibInit(&seq->bitbuf); return seq;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -