📄 frame.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 <assert.h>#include <string.h>#include "nccglob.h"#include "globals.h"#include "debug.h"#include "frame.h"#include "loopfilter.h"#include "slice.h"/* * Prototypes for static functions */static void *allocMem(int blkSize, size_t unitSize);/* * frmLoad: * * Parameters: * * Function: * Initialize frame constants * * Returns: * - */void frmLoad(){ mbkLoad();}/* * allocMem: * * Parameters: * blkSize Block size * unitSize unit size * * Function: * Allocate blkSize*unitSize bytes of memory * * Returns: * Pointer to allocated memory */static void *allocMem(int blkSize, size_t unitSize){ void *mem; if ((mem = nccMalloc(blkSize*unitSize)) == NULL) { deb0f(stderr, "Cannot allocate memory for frame\n"); nccExit(1); } return mem;}/* * frmOpen: * * Parameters: * frame Frame * width Width of the frame * height Height of the frame * plr plr/100 is the packet loss rate * * Function: * Allocate memory for frame buffers * * Returns: * - */void frmOpen(frmBuf_s *frame, int width, int height, int plr){ int numPels = width*height; frame->width = (int16) width; frame->height = (int16) height; frame->y = (u_int8 *)allocMem(numPels, sizeof(u_int8)); frame->u = (u_int8 *)allocMem(numPels/4, sizeof(u_int8)); frame->v = (u_int8 *)allocMem(numPels/4, sizeof(u_int8)); if (plr != 0) frame->channelDistortion = (int*) nccMalloc(sizeof(int) * numPels/MBK_SIZE/MBK_SIZE); else frame->channelDistortion = NULL;}/* * frmEncBufOpen: * * Parameters: * mbData Buffers for for macroblock attributes * width Width of the frame * height Height of the frame * * Function: * Allocate buffers for storing the macroblock attributes. * * Returns: * - */void frmEncBufOpen(mbAttributes_s *mbData, int width, int height){ int numBlocks = width/BLK_SIZE*height/BLK_SIZE; int numMacroblocks = width/MBK_SIZE*height/MBK_SIZE; // buffers for MB/block level encoding information mbData->blksPerLine = width/BLK_SIZE; mbData->mbStateArr = (mbState_s *)allocMem(numMacroblocks, sizeof(mbState_s)); mbData->refIdxTable = (int8 *)allocMem(numBlocks, sizeof(int8)); mbData->motVecTable = (motVec_s *)allocMem(numBlocks, sizeof(motVec_s));}/* * frmClose: * * Parameters: * frame Frame * * Function: * Deallocate frame buffers memory. * * Returns: * - * */void frmClose(frmBuf_s *frame){ nccFree(frame->y); nccFree(frame->u); nccFree(frame->v); nccFree(frame->channelDistortion);}/* * frmEncBufClose: * * Parameters: * mbData Buffers for for macroblock attributes * * Function: * Deallocate buffers used for storing the macroblock attributes. * * Returns: * - */void frmEncBufClose(mbAttributes_s *mbData){ nccFree(mbData->mbStateArr); nccFree(mbData->refIdxTable); nccFree(mbData->motVecTable);}/* * * frmEncode * * Parameters: * slice Slice coding object * dpbBuf Buffer for decoded frames, used as reference frames * encPar Encoding parameters (motion range, etc.) * mbData Buffers for macroblock attributes * sps Sequence parameter set * pps Picture parameter set * pBrc pointer to bit rate control engine, * * Function: * Encode a frame, as 1 or multiple slices. * * Returns: * - * */int frmEncode(slice_s *slice, dpbBuf_s *dpbBuf, encParams_s *encPar, mbAttributes_s *mbData, seq_parameter_set_s *sps, pic_parameter_set_s *pps, int forcedIRNo, int *forcedIR, rateCtrl_s *pbRC){ int i; int width, height; int firstMbAddr; int frmMarker, numFrmBits; int *mapRIR; int numIntraMbs; int tuneFactor; int sliceIdxInGroup[MAX_SLICE_GROUP_NUM]; // this is to tune the rdoLambda if (slice->nalRefIdc == 0) tuneFactor = encPar->tuneNonref; else tuneFactor = (slice->sliceType == SLICE_I) ? encPar->tuneIntra : encPar->tuneInter; // Random Intra Refresh mapRIR = NULL; if(encPar->nRandomIntraRefresh != 0) { mapRIR = (int *) malloc(encPar->nRandomIntraRefresh * sizeof(int)); for(i = 0; i < encPar->nRandomIntraRefresh; i ++) mapRIR[i] = rand() % slice->picSizeInMbs; } // Generate slice group map, and mark all macroblocks as un-encoded sliceGenerateMbMap(slice, pps);#if PRINT_IMG_TYPE deb0p("Pic sync: type %i, format %i, qp %i, num %i\n", (IS_INTRA(slice->sliceType)) ? 2 : (dbpBuf->numRefFrms == 1 ? 0 : 1), picFormat, slice->qp, picNum%256);#endif /* * Scan through all slices in picture */ // configuration of bit rate control before encoding frame // is idrFlag same as intra frame flag in this implementation ??? frmMarker = bibGetNumBits(slice->bitbuf); numIntraMbs = 0; firstMbAddr = 0; for (i = 0; i < MAX_SLICE_GROUP_NUM; i++) sliceIdxInGroup[i] = -1;///// LOW_COMPLEX_PROF3 // :per slice/frame init { int ii; LC3_s *plc3 = &(slice->meProfile.lc3); plc3->old_cnt=plc3->old_cnt2=plc3->cnt=-1; for (ii=MOT_COPY;ii<=MOT_8x8; ii++) plc3->thre_change[ii]=-1; // disable plc3->thre_change[MOT_16x16]=TH_HPEL_GAIN; }///// do { if (slice->mbStateArr[firstMbAddr].mbType == -1) { // find one MB not encoded, encode slice that contains this MB slice->groupId = (int16) (slice->mbStateArr[firstMbAddr].sliceMap & 0x0F); slice->firstMbAddr = (int16) firstMbAddr; slice->firstMbX = (int16) (firstMbAddr % slice->mbksPerLine); slice->firstMbY = (int16) (firstMbAddr / slice->mbksPerLine); // this is for identifying different slices within a group sliceIdxInGroup[slice->groupId] ++; numIntraMbs += sliceEncode(slice, sliceIdxInGroup[slice->groupId], dpbBuf, encPar, tuneFactor, mapRIR, sps, pps, forcedIRNo, forcedIR, pbRC); } firstMbAddr ++; } while (firstMbAddr < slice->picSizeInMbs); // : per slice/frame finish // how many bits generated for this frame numFrmBits = bibGetNumBits(slice->bitbuf); /* * Invoke loopfilter for current frame */ width = encPar->picWidth; height = encPar->picHeight; // copy MV & ref frame indices from the other buffer, // so we do not have to modify loopfilter.c { int i, j, k, l, blksPerLine; int8 *refPtr; motVec_s *vecPtr; mbState_s *pMbState; blksPerLine = mbData->blksPerLine; pMbState = mbData->mbStateArr; for (i = 0; i < height/BLK_SIZE; i += 4) { for (j = 0; j < width/BLK_SIZE; j += 4) { refPtr = & mbData->refIdxTable[i * blksPerLine + j]; vecPtr = & mbData->motVecTable[i * blksPerLine + j]; if (pMbState->mbType >= MB_TYPE_INTER_SKIPPED) for (k = 0; k < 4; k ++) for (l = 0; l < 4; l ++) { vecPtr[k * blksPerLine + l] = pMbState->blkInfo[k][l].mv; refPtr[k * blksPerLine + l] = pMbState->blkInfo[k][l].ref; } pMbState ++; } } } filFilterFrame(slice->recoBuf, mbData, encPar->chromaQpIdx); if (mapRIR) { free(mapRIR); mapRIR = 0; } return numIntraMbs;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -