Showing the changes to codec/encoder/core/src/encoder.cpp (1 of 225 files). Show all
Unified · Split
| @@ -1,554 +1,498 @@ | ||
| 1 | −/*! | |
| 2 | − * \copy | |
| 3 | − * Copyright (c) 2009-2013, Cisco Systems | |
| 4 | − * All rights reserved. | |
| 5 | − * | |
| 6 | − * Redistribution and use in source and binary forms, with or without | |
| 7 | − * modification, are permitted provided that the following conditions | |
| 8 | − * are met: | |
| 9 | − * | |
| 10 | − * * Redistributions of source code must retain the above copyright | |
| 11 | − * notice, this list of conditions and the following disclaimer. | |
| 12 | − * | |
| 13 | − * * Redistributions in binary form must reproduce the above copyright | |
| 14 | − * notice, this list of conditions and the following disclaimer in | |
| 15 | − * the documentation and/or other materials provided with the | |
| 16 | − * distribution. | |
| 17 | − * | |
| 18 | − * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 19 | − * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 20 | − * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | |
| 21 | − * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | |
| 22 | − * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | |
| 23 | − * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | |
| 24 | − * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
| 25 | − * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | |
| 26 | − * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
| 27 | − * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN | |
| 28 | − * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
| 29 | − * POSSIBILITY OF SUCH DAMAGE. | |
| 30 | − * | |
| 31 | − * | |
| 32 | − * \file encoder.c | |
| 33 | − * | |
| 34 | − * \brief core encoder | |
| 35 | − * | |
| 36 | − * \date 5/14/2009 Created | |
| 37 | − * | |
| 38 | − ************************************************************************************* | |
| 39 | − */ | |
| 40 | −#include <stdio.h> | |
| 41 | −#include <stdlib.h> | |
| 42 | −#include <string.h> | |
| 43 | −#include <assert.h> | |
| 44 | −#include "encoder.h" | |
| 45 | −#include "extern.h" | |
| 46 | −#include "cpu.h" | |
| 47 | −#include "cpu_core.h" | |
| 48 | −#include "utils.h" | |
| 49 | − | |
| 50 | −#include "encode_mb_aux.h" | |
| 51 | −#include "decode_mb_aux.h" | |
| 52 | −#include "get_intra_predictor.h" | |
| 53 | −#include "svc_encode_mb.h" | |
| 54 | − | |
| 55 | −#include "deblocking.h" | |
| 56 | −#include "expand_pic.h" | |
| 57 | − | |
| 58 | −#include "mc.h" | |
| 59 | −#include "sample.h" | |
| 60 | − | |
| 61 | −#include "svc_encode_slice.h" | |
| 62 | −#include "svc_base_layer_md.h" | |
| 63 | −#include "svc_mode_decision.h" | |
| 64 | −#include "set_mb_syn_cavlc.h" | |
| 65 | −#include "crt_util_safe_x.h" // Safe CRT routines like utils for cross_platforms | |
| 66 | −#include "codec_def.h" | |
| 67 | −#ifdef MT_ENABLED | |
| 68 | −#include "slice_multi_threading.h" | |
| 69 | −#endif//MT_ENABLED | |
| 70 | − | |
| 71 | −// global function pointers definition | |
| 72 | −namespace WelsSVCEnc { | |
| 73 | −/* Motion compensation */ | |
| 74 | − | |
| 75 | − | |
| 76 | −/*! | |
| 77 | − * \brief initialize source picture body | |
| 78 | − * \param pSrc SSourcePicture* | |
| 79 | − * \param csp internal csp format | |
| 80 | − * \param iWidth widht of picture in pixels | |
| 81 | − * \param iHeight iHeight of picture in pixels | |
| 82 | − * \return successful - 0; otherwise none 0 for failed | |
| 83 | − */ | |
| 84 | −int32_t InitPic( const void *kpSrc, const int32_t kiColorspace, const int32_t kiWidth, const int32_t kiHeight ) | |
| 85 | −{ | |
| 86 | − SSourcePicture *pSrcPic = (SSourcePicture *)kpSrc; | |
| 87 | − | |
| 88 | − if ( NULL == pSrcPic || kiWidth == 0 || kiHeight == 0 ) | |
| 89 | − return 1; | |
| 90 | − | |
| 91 | − pSrcPic->iColorFormat = kiColorspace; | |
| 92 | − pSrcPic->iPicWidth = kiWidth; | |
| 93 | − pSrcPic->iPicHeight = kiHeight; | |
| 94 | − | |
| 95 | − switch( kiColorspace & (~videoFormatVFlip) ) { | |
| 96 | − case videoFormatI420: | |
| 97 | − case videoFormatYV12: | |
| 98 | − pSrcPic->pData[0] = NULL; | |
| 99 | − pSrcPic->pData[1] = NULL; | |
| 100 | − pSrcPic->pData[2] = NULL; | |
| 101 | − pSrcPic->pData[3] = NULL; | |
| 102 | − pSrcPic->iStride[0] = kiWidth; | |
| 103 | − pSrcPic->iStride[2] = pSrcPic->iStride[1] = kiWidth >> 1; | |
| 104 | − pSrcPic->iStride[3] = 0; | |
| 105 | − break; | |
| 106 | − case videoFormatYUY2: | |
| 107 | − case videoFormatYVYU: | |
| 108 | − case videoFormatUYVY: | |
| 109 | − pSrcPic->pData[0] = NULL; | |
| 110 | − pSrcPic->pData[1] = NULL; | |
| 111 | − pSrcPic->pData[2] = NULL; | |
| 112 | − pSrcPic->pData[3] = NULL; | |
| 113 | − pSrcPic->iStride[0] = CALC_BI_STRIDE(kiWidth, 16); | |
| 114 | − pSrcPic->iStride[3] = pSrcPic->iStride[2] = pSrcPic->iStride[1] = 0; | |
| 115 | − break; | |
| 116 | − case videoFormatRGB: | |
| 117 | − case videoFormatBGR: | |
| 118 | − pSrcPic->pData[0] = NULL; | |
| 119 | − pSrcPic->pData[1] = NULL; | |
| 120 | − pSrcPic->pData[2] = NULL; | |
| 121 | − pSrcPic->pData[3] = NULL; | |
| 122 | − pSrcPic->iStride[0] = CALC_BI_STRIDE(kiWidth, 24); | |
| 123 | − pSrcPic->iStride[3] = pSrcPic->iStride[2] = pSrcPic->iStride[1] = 0; | |
| 124 | − if( kiColorspace & videoFormatVFlip ) | |
| 125 | − pSrcPic->iColorFormat = kiColorspace & (~videoFormatVFlip); | |
| 126 | − else | |
| 127 | − pSrcPic->iColorFormat = kiColorspace | videoFormatVFlip; | |
| 128 | − break; | |
| 129 | − case videoFormatBGRA: | |
| 130 | − case videoFormatRGBA: | |
| 131 | − case videoFormatARGB: | |
| 132 | − case videoFormatABGR: | |
| 133 | − pSrcPic->pData[0] = NULL; | |
| 134 | − pSrcPic->pData[1] = NULL; | |
| 135 | − pSrcPic->pData[2] = NULL; | |
| 136 | − pSrcPic->pData[3] = NULL; | |
| 137 | − pSrcPic->iStride[0] = kiWidth << 2; | |
| 138 | − pSrcPic->iStride[3] = pSrcPic->iStride[2] = pSrcPic->iStride[1] = 0; | |
| 139 | − if( kiColorspace & videoFormatVFlip ) | |
| 140 | − pSrcPic->iColorFormat = kiColorspace & (~videoFormatVFlip); | |
| 141 | − else | |
| 142 | − pSrcPic->iColorFormat = kiColorspace | videoFormatVFlip; | |
| 143 | − break; | |
| 144 | − default: | |
| 145 | − return 2; // any else? | |
| 146 | − } | |
| 147 | − | |
| 148 | − return 0; | |
| 149 | −} | |
| 150 | − | |
| 151 | − | |
| 152 | −void WelsInitBGDFunc( SWelsFuncPtrList *pFuncList, const bool_t kbEnableBackgroundDetection ) | |
| 153 | −{ | |
| 154 | − if ( kbEnableBackgroundDetection ) | |
| 155 | − { | |
| 156 | − pFuncList->pfInterMdBackgroundDecision = (PInterMdBackgroundDecisionFunc)WelsMdInterJudgeBGDPskip; | |
| 157 | − pFuncList->pfInterMdBackgroundInfoUpdate = (PInterMdBackgroundInfoUpdateFunc)WelsMdInterUpdateBGDInfo; | |
| 158 | − } | |
| 159 | − else | |
| 160 | − { | |
| 161 | − pFuncList->pfInterMdBackgroundDecision = (PInterMdBackgroundDecisionFunc)WelsMdInterJudgeBGDPskipFalse; | |
| 162 | − pFuncList->pfInterMdBackgroundInfoUpdate = (PInterMdBackgroundInfoUpdateFunc)WelsMdInterUpdateBGDInfoNULL; | |
| 163 | − } | |
| 164 | −} | |
| 165 | − | |
| 166 | −/*! | |
| 167 | − * \brief initialize function pointers that potentially used in Wels encoding | |
| 168 | − * \param pEncCtx sWelsEncCtx* | |
| 169 | − * \return successful - 0; otherwise none 0 for failed | |
| 170 | − */ | |
| 171 | −int32_t InitFunctionPointers( SWelsFuncPtrList *pFuncList, SWelsSvcCodingParam *pParam, uint32_t uiCpuFlag ) | |
| 172 | −{ | |
| 173 | − int32_t iReturn = 0; | |
| 174 | − | |
| 175 | − /* Functionality utilization of CPU instructions dependency */ | |
| 176 | − pFuncList->pfSetMemZeroSize8 = WelsSetMemZero_c; // confirmed_safe_unsafe_usage | |
| 177 | − pFuncList->pfSetMemZeroSize64Aligned16 = WelsSetMemZero_c; // confirmed_safe_unsafe_usage | |
| 178 | − pFuncList->pfSetMemZeroSize64 = WelsSetMemZero_c; // confirmed_safe_unsafe_usage | |
| 179 | −#if defined(X86_ASM) | |
| 180 | − if ( uiCpuFlag & WELS_CPU_MMXEXT ) | |
| 181 | − { | |
| 182 | − pFuncList->pfSetMemZeroSize8 = WelsSetMemZeroSize8_mmx; // confirmed_safe_unsafe_usage | |
| 183 | − pFuncList->pfSetMemZeroSize64Aligned16 = WelsSetMemZeroSize64_mmx; // confirmed_safe_unsafe_usage | |
| 184 | − pFuncList->pfSetMemZeroSize64 = WelsSetMemZeroSize64_mmx; // confirmed_safe_unsafe_usage | |
| 185 | − } | |
| 186 | − if ( uiCpuFlag & WELS_CPU_SSE2 ) | |
| 187 | − { | |
| 188 | − pFuncList->pfSetMemZeroSize64Aligned16 = WelsSetMemZeroAligned64_sse2; // confirmed_safe_unsafe_usage | |
| 189 | − } | |
| 190 | −#endif//X86_ASM | |
| 191 | − | |
| 192 | − InitExpandPictureFunc( pFuncList, uiCpuFlag ); | |
| 193 | − | |
| 194 | − /* Intra_Prediction_fn*/ | |
| 195 | − WelsInitFillingPredFuncs( uiCpuFlag ); | |
| 196 | − WelsInitIntraPredFuncs( pFuncList, uiCpuFlag ); | |
| 197 | − | |
| 198 | − /* sad, satd, average */ | |
| 199 | − WelsInitSampleSadFunc(pFuncList, uiCpuFlag); | |
| 200 | − | |
| 201 | − // | |
| 202 | − WelsInitBGDFunc(pFuncList, pParam->bEnableBackgroundDetection ); | |
| 203 | − // for pfGetVarianceFromIntraVaa function ptr adaptive by CPU features, 6/7/2010 | |
| 204 | − InitIntraAnalysisVaaInfo( pFuncList, uiCpuFlag ); | |
| 205 | − | |
| 206 | − /* Motion compensation */ | |
| 207 | − /*init pixel average function*/ | |
| 208 | − /*get one column or row pixel when refinement*/ | |
| 209 | − WelsInitMcFuncs(pFuncList, uiCpuFlag); | |
| 210 | − InitCoeffFunc( uiCpuFlag ); | |
| 211 | − | |
| 212 | − WelsInitEncodingFuncs( pFuncList, uiCpuFlag ); | |
| 213 | − WelsInitReconstructionFuncs( pFuncList, uiCpuFlag ); | |
| 214 | − | |
| 215 | − DeblockingInit( &pFuncList->pfDeblocking, uiCpuFlag ); | |
| 216 | − WelsBlockFuncInit( &pFuncList->pfSetNZCZero, uiCpuFlag ); | |
| 217 | − | |
| 218 | − InitFillNeighborCacheInterFunc ( pFuncList, pParam->bEnableBackgroundDetection ); | |
| 219 | − | |
| 220 | − return iReturn; | |
| 221 | −} | |
| 222 | − | |
| 223 | −/*! | |
| 224 | − * \brief initialize frame coding | |
| 225 | − */ | |
| 226 | −void InitFrameCoding( sWelsEncCtx *pEncCtx, const EFrameType keFrameType ) | |
| 227 | −{ | |
| 228 | − // for bitstream writing | |
| 229 | − pEncCtx->iPosBsBuffer = 0; // reset bs pBuffer position | |
| 230 | − pEncCtx->pOut->iNalIndex = 0; // reset NAL index | |
| 231 | − | |
| 232 | − InitBits( &pEncCtx->pOut->sBsWrite, pEncCtx->pOut->pBsBuffer, pEncCtx->pOut->uiSize ); | |
| 233 | − | |
| 234 | − if ( keFrameType == WELS_FRAME_TYPE_P ) | |
| 235 | − { | |
| 236 | − if ( pEncCtx->pSvcParam->uiIntraPeriod ) | |
| 237 | − { | |
| 238 | − ++pEncCtx->iFrameIndex; | |
| 239 | − } | |
| 240 | − | |
| 241 | − ++pEncCtx->uiFrameIdxRc; | |
| 242 | − | |
| 243 | − if ( pEncCtx->iPOC < ( 1 << pEncCtx->pSps->iLog2MaxPocLsb ) - 2 ) // if iPOC type is no 0, this need be modification | |
| 244 | − pEncCtx->iPOC += 2; // for POC type 0 | |
| 245 | − else | |
| 246 | − pEncCtx->iPOC = 0; | |
| 247 | − | |
| 248 | − if ( pEncCtx->eLastNalPriority != 0 ) | |
| 249 | − { | |
| 250 | − if ( pEncCtx->iFrameNum < (1 << pEncCtx->pSps->uiLog2MaxFrameNum) - 1 ) | |
| 251 | − ++ pEncCtx->iFrameNum; | |
| 252 | − else | |
| 253 | − pEncCtx->iFrameNum = 0; // if iFrameNum overflow | |
| 254 | − } | |
| 255 | − pEncCtx->eNalType = NAL_UNIT_CODED_SLICE; | |
| 256 | − pEncCtx->eSliceType = P_SLICE; | |
| 257 | − pEncCtx->eNalPriority = NRI_PRI_HIGH; | |
| 258 | − } | |
| 259 | − else if ( keFrameType == WELS_FRAME_TYPE_IDR ) | |
| 260 | − { | |
| 261 | − pEncCtx->iFrameNum = 0; | |
| 262 | − pEncCtx->iPOC = 0; | |
| 263 | − pEncCtx->bEncCurFrmAsIdrFlag = false; | |
| 264 | − if ( pEncCtx->pSvcParam->uiIntraPeriod ) | |
| 265 | − { | |
| 266 | − pEncCtx->iFrameIndex = 0; | |
| 267 | − } | |
| 268 | − pEncCtx->uiFrameIdxRc = 0; | |
| 269 | − | |
| 270 | − pEncCtx->eNalType = NAL_UNIT_CODED_SLICE_IDR; | |
| 271 | − pEncCtx->eSliceType = I_SLICE; | |
| 272 | − pEncCtx->eNalPriority = NRI_PRI_HIGHEST; | |
| 273 | − | |
| 274 | − pEncCtx->iCodingIndex = 0; | |
| 275 | − | |
| 276 | − // reset_ref_list | |
| 277 | − | |
| 278 | − // rc_init_gop | |
| 279 | − } | |
| 280 | − else if ( keFrameType == WELS_FRAME_TYPE_I ) | |
| 281 | − { | |
| 282 | − if ( pEncCtx->iPOC < ( 1 << pEncCtx->pSps->iLog2MaxPocLsb ) - 2 ) // if iPOC type is no 0, this need be modification | |
| 283 | − pEncCtx->iPOC += 2; // for POC type 0 | |
| 284 | − else | |
| 285 | − pEncCtx->iPOC = 0; | |
| 286 | − | |
| 287 | − if ( pEncCtx->eLastNalPriority != 0 ) | |
| 288 | − { | |
| 289 | − if ( pEncCtx->iFrameNum < (1 << pEncCtx->pSps->uiLog2MaxFrameNum) - 1 ) | |
| 290 | − ++ pEncCtx->iFrameNum; | |
| 291 | − else | |
| 292 | − pEncCtx->iFrameNum = 0; // if iFrameNum overflow | |
| 293 | − } | |
| 294 | − | |
| 295 | − pEncCtx->eNalType = NAL_UNIT_CODED_SLICE; | |
| 296 | − pEncCtx->eSliceType = I_SLICE; | |
| 297 | − pEncCtx->eNalPriority = NRI_PRI_HIGHEST; | |
| 298 | − | |
| 299 | − // rc_init_gop | |
| 300 | − } | |
| 301 | − else // B pictures are not supported now, any else? | |
| 302 | − { | |
| 303 | − assert( 0 ); | |
| 304 | − } | |
| 305 | − | |
| 306 | −#if defined(STAT_OUTPUT) | |
| 307 | − memset( &pEncCtx->sPerInfo, 0, sizeof(SStatSliceInfo) ); | |
| 308 | −#endif//FRAME_INFO_OUTPUT | |
| 309 | − | |
| 310 | −#if defined(MT_ENABLED) && defined(PACKING_ONE_SLICE_PER_LAYER) | |
| 311 | − if ( pEncCtx->pSvcParam->iMultipleThreadIdc > 1 ) | |
| 312 | − reset_env_mt( pEncCtx ); | |
| 313 | −#endif | |
| 314 | −} | |
| 315 | − | |
| 316 | −EFrameType DecideFrameType( sWelsEncCtx *pEncCtx, const int8_t kiSpatialNum ) | |
| 317 | −{ | |
| 318 | − SWelsSvcCodingParam *pSvcParam = pEncCtx->pSvcParam; | |
| 319 | − EFrameType iFrameType = WELS_FRAME_TYPE_AUTO; | |
| 320 | − bool_t bSceneChangeFlag = false; | |
| 321 | − | |
| 322 | − // perform scene change detection | |
| 323 | − if ( (!pSvcParam->bEnableSceneChangeDetect) || pEncCtx->pVaa->bIdrPeriodFlag || | |
| 324 | − (kiSpatialNum < pSvcParam->iNumDependencyLayer) || (pEncCtx->uiFrameIdxRc < (VGOP_SIZE << 1)) ) // avoid too frequent I frame coding, rc control | |
| 325 | − { | |
| 326 | − bSceneChangeFlag = false; | |
| 327 | − } | |
| 328 | − else | |
| 329 | − { | |
| 330 | − bSceneChangeFlag = pEncCtx->pVaa->bSceneChangeFlag; | |
| 331 | − } | |
| 332 | − | |
| 333 | − //scene_changed_flag: RC enable && iSpatialNum == pSvcParam->iNumDependencyLayer | |
| 334 | − //bIdrPeriodFlag: RC disable || iSpatialNum != pSvcParam->iNumDependencyLayer | |
| 335 | − //pEncCtx->bEncCurFrmAsIdrFlag: 1. first frame should be IDR; 2. idr pause; 3. idr request | |
| 336 | − iFrameType = ( pEncCtx->pVaa->bIdrPeriodFlag || bSceneChangeFlag || pEncCtx->bEncCurFrmAsIdrFlag ) ? WELS_FRAME_TYPE_IDR : WELS_FRAME_TYPE_P; | |
| 337 | − | |
| 338 | − if ( WELS_FRAME_TYPE_P == iFrameType && pEncCtx->iSkipFrameFlag > 0 ) // for frame skip, 1/5/2010 | |
| 339 | − { | |
| 340 | − -- pEncCtx->iSkipFrameFlag; | |
| 341 | − iFrameType = WELS_FRAME_TYPE_SKIP; | |
| 342 | − } | |
| 343 | − else if ( WELS_FRAME_TYPE_IDR == iFrameType ) | |
| 344 | − { | |
| 345 | − pEncCtx->iCodingIndex = 0; | |
| 346 | − } | |
| 347 | − | |
| 348 | − return iFrameType; | |
| 349 | −} | |
| 350 | − | |
| 351 | −/*! | |
| 352 | − * \brief Dump reconstruction for dependency layer | |
| 353 | − */ | |
| 354 | − | |
| 355 | −extern "C" void DumpDependencyRec( SPicture *pCurPicture, const str_t *kpFileName, const int8_t kiDid ) | |
| 356 | −{ | |
| 357 | − FILE *pDumpRecFile = NULL; | |
| 358 | − static bool_t bDependencyRecFlag[MAX_DEPENDENCY_LAYER] = {0}; | |
| 359 | − int32_t iWrittenSize = 0; | |
| 360 | − | |
| 361 | − if ( NULL == pCurPicture || NULL == kpFileName || kiDid >= MAX_DEPENDENCY_LAYER ) | |
| 362 | − return; | |
| 363 | − | |
| 364 | − if ( bDependencyRecFlag[kiDid] ) | |
| 365 | − { | |
| 366 | − if ( STRNLEN(kpFileName, MAX_FNAME_LEN) > 0 ) // confirmed_safe_unsafe_usage | |
| 367 | −#if defined(__GNUC__) || (defined(WIN32) && defined(_MSC_VER) && (_MSC_VER<1500)) | |
| 368 | − pDumpRecFile = FOPEN( kpFileName, "ab" ); | |
| 369 | −#elif defined(WIN32) && defined(_MSC_VER) && (_MSC_VER>=1500) // vs2008 | |
| 370 | − FOPEN(&pDumpRecFile, kpFileName, "ab"); | |
| 371 | −#endif//__GNUC__.. | |
| 372 | − else | |
| 373 | − { | |
| 374 | − str_t sDependencyRecFileName[16] = {0}; | |
| 375 | −#if defined(WIN32) && defined(_MSC_VER) && (_MSC_VER>=1500) // vs2008 | |
| 376 | − SNPRINTF( sDependencyRecFileName, 16, 16, "rec%d.yuv", kiDid ); // confirmed_safe_unsafe_usage | |
| 377 | − FOPEN( &pDumpRecFile, sDependencyRecFileName, "ab" ); | |
| 378 | −#else | |
| 379 | − SNPRINTF( sDependencyRecFileName, 16, "rec%d.yuv", kiDid ); // confirmed_safe_unsafe_usage | |
| 380 | − pDumpRecFile = FOPEN( sDependencyRecFileName, "ab" ); | |
| 381 | −#endif//WIN32.. | |
| 382 | − } | |
| 383 | − if ( NULL != pDumpRecFile) | |
| 384 | − fseek( pDumpRecFile, 0, SEEK_END ); | |
| 385 | − } | |
| 386 | − else | |
| 387 | − { | |
| 388 | − if ( STRNLEN(kpFileName, MAX_FNAME_LEN) > 0 ) // confirmed_safe_unsafe_usage | |
| 389 | − { | |
| 390 | −#if defined(WIN32) && defined(_MSC_VER) && (_MSC_VER>=1500) // vs2008 | |
| 391 | − FOPEN(&pDumpRecFile, kpFileName, "wb"); | |
| 392 | −#else | |
| 393 | − pDumpRecFile = FOPEN( kpFileName, "wb" ); | |
| 394 | −#endif//WIN32.. | |
| 395 | − } | |
| 396 | − else | |
| 397 | − { | |
| 398 | − str_t sDependencyRecFileName[16] = {0}; | |
| 399 | −#if defined(WIN32) && defined(_MSC_VER) && (_MSC_VER>=1500) // vs2008 | |
| 400 | − SNPRINTF( sDependencyRecFileName, 16, 16, "rec%d.yuv", kiDid ); // confirmed_safe_unsafe_usage | |
| 401 | − FOPEN(&pDumpRecFile, sDependencyRecFileName, "wb"); | |
| 402 | −#else | |
| 403 | − SNPRINTF( sDependencyRecFileName, 16, "rec%d.yuv", kiDid ); // confirmed_safe_unsafe_usage | |
| 404 | − pDumpRecFile = FOPEN( sDependencyRecFileName, "wb"); | |
| 405 | −#endif//WIN32.. | |
| 406 | − } | |
| 407 | − bDependencyRecFlag[kiDid] = true; | |
| 408 | − } | |
| 409 | − | |
| 410 | − if ( NULL != pDumpRecFile ) | |
| 411 | − { | |
| 412 | − int32_t i = 0; | |
| 413 | − int32_t j = 0; | |
| 414 | − const int32_t kiStrideY = pCurPicture->iLineSize[0]; | |
| 415 | − const int32_t kiLumaWidth = pCurPicture->iWidthInPixel; | |
| 416 | − const int32_t kiLumaHeight = pCurPicture->iHeightInPixel; | |
| 417 | − const int32_t kiChromaWidth = kiLumaWidth >> 1; | |
| 418 | − const int32_t kiChromaHeight = kiLumaHeight >> 1; | |
| 419 | − | |
| 420 | − for( j = 0; j < kiLumaHeight; ++ j) | |
| 421 | − { | |
| 422 | − iWrittenSize = fwrite( &pCurPicture->pData[0][j*kiStrideY], 1, kiLumaWidth, pDumpRecFile ); | |
| 423 | − assert( iWrittenSize == kiLumaWidth ); | |
| 424 | − if ( iWrittenSize < kiLumaWidth ) | |
| 425 | − { | |
| 426 | − assert( 0 ); // make no sense for us if writing failed | |
| 427 | − fclose(pDumpRecFile); | |
| 428 | − return; | |
| 429 | − } | |
| 430 | − } | |
| 431 | − for( i = 1; i < I420_PLANES; ++ i) | |
| 432 | − { | |
| 433 | − const int32_t kiStrideUV = pCurPicture->iLineSize[i]; | |
| 434 | − for ( j = 0; j < kiChromaHeight; ++ j) | |
| 435 | − { | |
| 436 | − iWrittenSize = fwrite( &pCurPicture->pData[i][j*kiStrideUV], 1, kiChromaWidth, pDumpRecFile ); | |
| 437 | − assert(iWrittenSize == kiChromaWidth ); | |
| 438 | − if ( iWrittenSize < kiChromaWidth ) | |
| 439 | − { | |
| 440 | − assert( 0 ); // make no sense for us if writing failed | |
| 441 | − fclose(pDumpRecFile); | |
| 442 | − return; | |
| 443 | − } | |
| 444 | − } | |
| 445 | − } | |
| 446 | − fclose(pDumpRecFile); | |
| 447 | − pDumpRecFile = NULL; | |
| 448 | − } | |
| 449 | −} | |
| 450 | − | |
| 451 | −/*! | |
| 452 | − * \brief Dump the reconstruction pictures | |
| 453 | − */ | |
| 454 | − | |
| 455 | −void DumpRecFrame( SPicture *pCurPicture, const str_t *kpFileName ) | |
| 456 | −{ | |
| 457 | − FILE *pDumpRecFile = NULL; | |
| 458 | − static bool_t bRecFlag = false; | |
| 459 | − int32_t iWrittenSize = 0; | |
| 460 | − | |
| 461 | − if ( NULL == pCurPicture || NULL == kpFileName ) | |
| 462 | − return; | |
| 463 | − | |
| 464 | − if ( bRecFlag ) | |
| 465 | − { | |
| 466 | − if ( STRNLEN(kpFileName, MAX_FNAME_LEN) > 0 ) // confirmed_safe_unsafe_usage | |
| 467 | − { | |
| 468 | −#if defined(WIN32) && defined(_MSC_VER) && (_MSC_VER>=1500) // vs2008 | |
| 469 | − FOPEN(&pDumpRecFile, kpFileName, "ab"); | |
| 470 | −#else | |
| 471 | − pDumpRecFile = FOPEN( kpFileName, "ab" ); | |
| 472 | −#endif//WIN32 | |
| 473 | − } | |
| 474 | − else | |
| 475 | − { | |
| 476 | −#if defined(WIN32) && defined(_MSC_VER) && (_MSC_VER>=1500) // vs2008 | |
| 477 | − FOPEN(&pDumpRecFile, "rec.yuv", "ab"); | |
| 478 | −#else | |
| 479 | − pDumpRecFile = FOPEN( "rec.yuv", "ab" ); | |
| 480 | −#endif//WIN32 | |
| 481 | − } | |
| 482 | − if ( NULL != pDumpRecFile) | |
| 483 | − fseek( pDumpRecFile, 0, SEEK_END ); | |
| 484 | − } | |
| 485 | − else | |
| 486 | − { | |
| 487 | − if ( STRNLEN(kpFileName, MAX_FNAME_LEN) > 0 ) // confirmed_safe_unsafe_usage | |
| 488 | − { | |
| 489 | −#if defined(WIN32) && defined(_MSC_VER) && (_MSC_VER>=1500) // vs2008 | |
| 490 | − FOPEN(&pDumpRecFile, kpFileName, "wb"); | |
| 491 | −#else | |
| 492 | − pDumpRecFile = FOPEN( kpFileName, "wb" ); | |
| 493 | −#endif//WIN32 | |
| 494 | − } | |
| 495 | − else | |
| 496 | − { | |
| 497 | −#if defined(WIN32) && defined(_MSC_VER) && (_MSC_VER>=1500) // vs2008 | |
| 498 | − FOPEN(&pDumpRecFile, "rec.yuv", "wb"); | |
| 499 | −#else | |
| 500 | − pDumpRecFile = FOPEN( "rec.yuv", "wb"); | |
| 501 | −#endif//WIN32.. | |
| 502 | − } | |
| 503 | − bRecFlag = true; | |
| 504 | − } | |
| 505 | − | |
| 506 | − if ( NULL != pDumpRecFile ) | |
| 507 | − { | |
| 508 | − int32_t i = 0; | |
| 509 | − int32_t j = 0; | |
| 510 | − const int32_t kiStrideY = pCurPicture->iLineSize[0]; | |
| 511 | − const int32_t kiLumaWidth = pCurPicture->iWidthInPixel; | |
| 512 | − const int32_t kiLumaHeight = pCurPicture->iHeightInPixel; | |
| 513 | − const int32_t kiChromaWidth = kiLumaWidth >> 1; | |
| 514 | − const int32_t kiChromaHeight = kiLumaHeight >> 1; | |
| 515 | − | |
| 516 | − for( j = 0; j < kiLumaHeight; ++ j) | |
| 517 | − { | |
| 518 | − iWrittenSize = fwrite( &pCurPicture->pData[0][j*kiStrideY], 1, kiLumaWidth, pDumpRecFile ); | |
| 519 | − assert( iWrittenSize == kiLumaWidth ); | |
| 520 | − if ( iWrittenSize < kiLumaWidth ) | |
| 521 | − { | |
| 522 | − assert( 0 ); // make no sense for us if writing failed | |
| 523 | − fclose(pDumpRecFile); | |
| 524 | − return; | |
| 525 | − } | |
| 526 | − } | |
| 527 | − for( i = 1; i < I420_PLANES; ++ i) | |
| 528 | − { | |
| 529 | − const int32_t kiStrideUV = pCurPicture->iLineSize[i]; | |
| 530 | − for ( j = 0; j < kiChromaHeight; ++ j) | |
| 531 | − { | |
| 532 | − iWrittenSize = fwrite( &pCurPicture->pData[i][j*kiStrideUV], 1, kiChromaWidth, pDumpRecFile ); | |
| 533 | − assert(iWrittenSize == kiChromaWidth ); | |
| 534 | − if ( iWrittenSize < kiChromaWidth ) | |
| 535 | − { | |
| 536 | − assert( 0 ); // make no sense for us if writing failed | |
| 537 | − fclose(pDumpRecFile); | |
| 538 | − return; | |
| 539 | − } | |
| 540 | − } | |
| 541 | − } | |
| 542 | − fclose(pDumpRecFile); | |
| 543 | − pDumpRecFile = NULL; | |
| 544 | − } | |
| 545 | −} | |
| 546 | − | |
| 547 | − | |
| 548 | − | |
| 549 | −/***********************************************************************************/ | |
| 550 | −void WelsSetMemZero_c(void *pDst, int32_t iSize) // confirmed_safe_unsafe_usage | |
| 551 | −{ | |
| 552 | − memset(pDst, 0, iSize); | |
| 553 | −} | |
| 554 | −} | |
| 1 | +/*! | |
| 2 | + * \copy | |
| 3 | + * Copyright (c) 2009-2013, Cisco Systems | |
| 4 | + * All rights reserved. | |
| 5 | + * | |
| 6 | + * Redistribution and use in source and binary forms, with or without | |
| 7 | + * modification, are permitted provided that the following conditions | |
| 8 | + * are met: | |
| 9 | + * | |
| 10 | + * * Redistributions of source code must retain the above copyright | |
| 11 | + * notice, this list of conditions and the following disclaimer. | |
| 12 | + * | |
| 13 | + * * Redistributions in binary form must reproduce the above copyright | |
| 14 | + * notice, this list of conditions and the following disclaimer in | |
| 15 | + * the documentation and/or other materials provided with the | |
| 16 | + * distribution. | |
| 17 | + * | |
| 18 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 19 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 20 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | |
| 21 | + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | |
| 22 | + * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | |
| 23 | + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | |
| 24 | + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
| 25 | + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | |
| 26 | + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
| 27 | + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN | |
| 28 | + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
| 29 | + * POSSIBILITY OF SUCH DAMAGE. | |
| 30 | + * | |
| 31 | + * | |
| 32 | + * \file encoder.c | |
| 33 | + * | |
| 34 | + * \brief core encoder | |
| 35 | + * | |
| 36 | + * \date 5/14/2009 Created | |
| 37 | + * | |
| 38 | + ************************************************************************************* | |
| 39 | + */ | |
| 40 | +#include <stdio.h> | |
| 41 | +#include <stdlib.h> | |
| 42 | +#include <string.h> | |
| 43 | +#include <assert.h> | |
| 44 | +#include "encoder.h" | |
| 45 | +#include "extern.h" | |
| 46 | +#include "cpu.h" | |
| 47 | +#include "cpu_core.h" | |
| 48 | +#include "utils.h" | |
| 49 | + | |
| 50 | +#include "encode_mb_aux.h" | |
| 51 | +#include "decode_mb_aux.h" | |
| 52 | +#include "get_intra_predictor.h" | |
| 53 | +#include "svc_encode_mb.h" | |
| 54 | + | |
| 55 | +#include "deblocking.h" | |
| 56 | +#include "expand_pic.h" | |
| 57 | + | |
| 58 | +#include "mc.h" | |
| 59 | +#include "sample.h" | |
| 60 | + | |
| 61 | +#include "svc_encode_slice.h" | |
| 62 | +#include "svc_base_layer_md.h" | |
| 63 | +#include "svc_mode_decision.h" | |
| 64 | +#include "set_mb_syn_cavlc.h" | |
| 65 | +#include "crt_util_safe_x.h" // Safe CRT routines like utils for cross_platforms | |
| 66 | +#include "codec_def.h" | |
| 67 | +#ifdef MT_ENABLED | |
| 68 | +#include "slice_multi_threading.h" | |
| 69 | +#endif//MT_ENABLED | |
| 70 | + | |
| 71 | +// global function pointers definition | |
| 72 | +namespace WelsSVCEnc { | |
| 73 | +/* Motion compensation */ | |
| 74 | + | |
| 75 | + | |
| 76 | +/*! | |
| 77 | + * \brief initialize source picture body | |
| 78 | + * \param pSrc SSourcePicture* | |
| 79 | + * \param csp internal csp format | |
| 80 | + * \param iWidth widht of picture in pixels | |
| 81 | + * \param iHeight iHeight of picture in pixels | |
| 82 | + * \return successful - 0; otherwise none 0 for failed | |
| 83 | + */ | |
| 84 | +int32_t InitPic (const void* kpSrc, const int32_t kiColorspace, const int32_t kiWidth, const int32_t kiHeight) { | |
| 85 | + SSourcePicture* pSrcPic = (SSourcePicture*)kpSrc; | |
| 86 | + | |
| 87 | + if (NULL == pSrcPic || kiWidth == 0 || kiHeight == 0) | |
| 88 | + return 1; | |
| 89 | + | |
| 90 | + pSrcPic->iColorFormat = kiColorspace; | |
| 91 | + pSrcPic->iPicWidth = kiWidth; | |
| 92 | + pSrcPic->iPicHeight = kiHeight; | |
| 93 | + | |
| 94 | + switch (kiColorspace & (~videoFormatVFlip)) { | |
| 95 | + case videoFormatI420: | |
| 96 | + case videoFormatYV12: | |
| 97 | + pSrcPic->pData[0] = NULL; | |
| 98 | + pSrcPic->pData[1] = NULL; | |
| 99 | + pSrcPic->pData[2] = NULL; | |
| 100 | + pSrcPic->pData[3] = NULL; | |
| 101 | + pSrcPic->iStride[0] = kiWidth; | |
| 102 | + pSrcPic->iStride[2] = pSrcPic->iStride[1] = kiWidth >> 1; | |
| 103 | + pSrcPic->iStride[3] = 0; | |
| 104 | + break; | |
| 105 | + case videoFormatYUY2: | |
| 106 | + case videoFormatYVYU: | |
| 107 | + case videoFormatUYVY: | |
| 108 | + pSrcPic->pData[0] = NULL; | |
| 109 | + pSrcPic->pData[1] = NULL; | |
| 110 | + pSrcPic->pData[2] = NULL; | |
| 111 | + pSrcPic->pData[3] = NULL; | |
| 112 | + pSrcPic->iStride[0] = CALC_BI_STRIDE (kiWidth, 16); | |
| 113 | + pSrcPic->iStride[3] = pSrcPic->iStride[2] = pSrcPic->iStride[1] = 0; | |
| 114 | + break; | |
| 115 | + case videoFormatRGB: | |
| 116 | + case videoFormatBGR: | |
| 117 | + pSrcPic->pData[0] = NULL; | |
| 118 | + pSrcPic->pData[1] = NULL; | |
| 119 | + pSrcPic->pData[2] = NULL; | |
| 120 | + pSrcPic->pData[3] = NULL; | |
| 121 | + pSrcPic->iStride[0] = CALC_BI_STRIDE (kiWidth, 24); | |
| 122 | + pSrcPic->iStride[3] = pSrcPic->iStride[2] = pSrcPic->iStride[1] = 0; | |
| 123 | + if (kiColorspace & videoFormatVFlip) | |
| 124 | + pSrcPic->iColorFormat = kiColorspace & (~videoFormatVFlip); | |
| 125 | + else | |
| 126 | + pSrcPic->iColorFormat = kiColorspace | videoFormatVFlip; | |
| 127 | + break; | |
| 128 | + case videoFormatBGRA: | |
| 129 | + case videoFormatRGBA: | |
| 130 | + case videoFormatARGB: | |
| 131 | + case videoFormatABGR: | |
| 132 | + pSrcPic->pData[0] = NULL; | |
| 133 | + pSrcPic->pData[1] = NULL; | |
| 134 | + pSrcPic->pData[2] = NULL; | |
| 135 | + pSrcPic->pData[3] = NULL; | |
| 136 | + pSrcPic->iStride[0] = kiWidth << 2; | |
| 137 | + pSrcPic->iStride[3] = pSrcPic->iStride[2] = pSrcPic->iStride[1] = 0; | |
| 138 | + if (kiColorspace & videoFormatVFlip) | |
| 139 | + pSrcPic->iColorFormat = kiColorspace & (~videoFormatVFlip); | |
| 140 | + else | |
| 141 | + pSrcPic->iColorFormat = kiColorspace | videoFormatVFlip; | |
| 142 | + break; | |
| 143 | + default: | |
| 144 | + return 2; // any else? | |
| 145 | + } | |
| 146 | + | |
| 147 | + return 0; | |
| 148 | +} | |
| 149 | + | |
| 150 | + | |
| 151 | +void WelsInitBGDFunc (SWelsFuncPtrList* pFuncList, const bool_t kbEnableBackgroundDetection) { | |
| 152 | + if (kbEnableBackgroundDetection) { | |
| 153 | + pFuncList->pfInterMdBackgroundDecision = (PInterMdBackgroundDecisionFunc)WelsMdInterJudgeBGDPskip; | |
| 154 | + pFuncList->pfInterMdBackgroundInfoUpdate = (PInterMdBackgroundInfoUpdateFunc)WelsMdInterUpdateBGDInfo; | |
| 155 | + } else { | |
| 156 | + pFuncList->pfInterMdBackgroundDecision = (PInterMdBackgroundDecisionFunc)WelsMdInterJudgeBGDPskipFalse; | |
| 157 | + pFuncList->pfInterMdBackgroundInfoUpdate = (PInterMdBackgroundInfoUpdateFunc)WelsMdInterUpdateBGDInfoNULL; | |
| 158 | + } | |
| 159 | +} | |
| 160 | + | |
| 161 | +/*! | |
| 162 | + * \brief initialize function pointers that potentially used in Wels encoding | |
| 163 | + * \param pEncCtx sWelsEncCtx* | |
| 164 | + * \return successful - 0; otherwise none 0 for failed | |
| 165 | + */ | |
| 166 | +int32_t InitFunctionPointers (SWelsFuncPtrList* pFuncList, SWelsSvcCodingParam* pParam, uint32_t uiCpuFlag) { | |
| 167 | + int32_t iReturn = 0; | |
| 168 | + | |
| 169 | + /* Functionality utilization of CPU instructions dependency */ | |
| 170 | + pFuncList->pfSetMemZeroSize8 = WelsSetMemZero_c; // confirmed_safe_unsafe_usage | |
| 171 | + pFuncList->pfSetMemZeroSize64Aligned16 = WelsSetMemZero_c; // confirmed_safe_unsafe_usage | |
| 172 | + pFuncList->pfSetMemZeroSize64 = WelsSetMemZero_c; // confirmed_safe_unsafe_usage | |
| 173 | +#if defined(X86_ASM) | |
| 174 | + if (uiCpuFlag & WELS_CPU_MMXEXT) { | |
| 175 | + pFuncList->pfSetMemZeroSize8 = WelsSetMemZeroSize8_mmx; // confirmed_safe_unsafe_usage | |
| 176 | + pFuncList->pfSetMemZeroSize64Aligned16 = WelsSetMemZeroSize64_mmx; // confirmed_safe_unsafe_usage | |
| 177 | + pFuncList->pfSetMemZeroSize64 = WelsSetMemZeroSize64_mmx; // confirmed_safe_unsafe_usage | |
| 178 | + } | |
| 179 | + if (uiCpuFlag & WELS_CPU_SSE2) { | |
| 180 | + pFuncList->pfSetMemZeroSize64Aligned16 = WelsSetMemZeroAligned64_sse2; // confirmed_safe_unsafe_usage | |
| 181 | + } | |
| 182 | +#endif//X86_ASM | |
| 183 | + | |
| 184 | + InitExpandPictureFunc (pFuncList, uiCpuFlag); | |
| 185 | + | |
| 186 | + /* Intra_Prediction_fn*/ | |
| 187 | + WelsInitFillingPredFuncs (uiCpuFlag); | |
| 188 | + WelsInitIntraPredFuncs (pFuncList, uiCpuFlag); | |
| 189 | + | |
| 190 | + /* sad, satd, average */ | |
| 191 | + WelsInitSampleSadFunc (pFuncList, uiCpuFlag); | |
| 192 | + | |
| 193 | + // | |
| 194 | + WelsInitBGDFunc (pFuncList, pParam->bEnableBackgroundDetection); | |
| 195 | + // for pfGetVarianceFromIntraVaa function ptr adaptive by CPU features, 6/7/2010 | |
| 196 | + InitIntraAnalysisVaaInfo (pFuncList, uiCpuFlag); | |
| 197 | + | |
| 198 | + /* Motion compensation */ | |
| 199 | + /*init pixel average function*/ | |
| 200 | + /*get one column or row pixel when refinement*/ | |
| 201 | + WelsInitMcFuncs (pFuncList, uiCpuFlag); | |
| 202 | + InitCoeffFunc (uiCpuFlag); | |
| 203 | + | |
| 204 | + WelsInitEncodingFuncs (pFuncList, uiCpuFlag); | |
| 205 | + WelsInitReconstructionFuncs (pFuncList, uiCpuFlag); | |
| 206 | + | |
| 207 | + DeblockingInit (&pFuncList->pfDeblocking, uiCpuFlag); | |
| 208 | + WelsBlockFuncInit (&pFuncList->pfSetNZCZero, uiCpuFlag); | |
| 209 | + | |
| 210 | + InitFillNeighborCacheInterFunc (pFuncList, pParam->bEnableBackgroundDetection); | |
| 211 | + | |
| 212 | + return iReturn; | |
| 213 | +} | |
| 214 | + | |
| 215 | +/*! | |
| 216 | + * \brief initialize frame coding | |
| 217 | + */ | |
| 218 | +void InitFrameCoding (sWelsEncCtx* pEncCtx, const EFrameType keFrameType) { | |
| 219 | + // for bitstream writing | |
| 220 | + pEncCtx->iPosBsBuffer = 0; // reset bs pBuffer position | |
| 221 | + pEncCtx->pOut->iNalIndex = 0; // reset NAL index | |
| 222 | + | |
| 223 | + InitBits (&pEncCtx->pOut->sBsWrite, pEncCtx->pOut->pBsBuffer, pEncCtx->pOut->uiSize); | |
| 224 | + | |
| 225 | + if (keFrameType == WELS_FRAME_TYPE_P) { | |
| 226 | + if (pEncCtx->pSvcParam->uiIntraPeriod) { | |
| 227 | + ++pEncCtx->iFrameIndex; | |
| 228 | + } | |
| 229 | + | |
| 230 | + ++pEncCtx->uiFrameIdxRc; | |
| 231 | + | |
| 232 | + if (pEncCtx->iPOC < (1 << pEncCtx->pSps->iLog2MaxPocLsb) - 2) // if iPOC type is no 0, this need be modification | |
| 233 | + pEncCtx->iPOC += 2; // for POC type 0 | |
| 234 | + else | |
| 235 | + pEncCtx->iPOC = 0; | |
| 236 | + | |
| 237 | + if (pEncCtx->eLastNalPriority != 0) { | |
| 238 | + if (pEncCtx->iFrameNum < (1 << pEncCtx->pSps->uiLog2MaxFrameNum) - 1) | |
| 239 | + ++ pEncCtx->iFrameNum; | |
| 240 | + else | |
| 241 | + pEncCtx->iFrameNum = 0; // if iFrameNum overflow | |
| 242 | + } | |
| 243 | + pEncCtx->eNalType = NAL_UNIT_CODED_SLICE; | |
| 244 | + pEncCtx->eSliceType = P_SLICE; | |
| 245 | + pEncCtx->eNalPriority = NRI_PRI_HIGH; | |
| 246 | + } else if (keFrameType == WELS_FRAME_TYPE_IDR) { | |
| 247 | + pEncCtx->iFrameNum = 0; | |
| 248 | + pEncCtx->iPOC = 0; | |
| 249 | + pEncCtx->bEncCurFrmAsIdrFlag = false; | |
| 250 | + if (pEncCtx->pSvcParam->uiIntraPeriod) { | |
| 251 | + pEncCtx->iFrameIndex = 0; | |
| 252 | + } | |
| 253 | + pEncCtx->uiFrameIdxRc = 0; | |
| 254 | + | |
| 255 | + pEncCtx->eNalType = NAL_UNIT_CODED_SLICE_IDR; | |
| 256 | + pEncCtx->eSliceType = I_SLICE; | |
| 257 | + pEncCtx->eNalPriority = NRI_PRI_HIGHEST; | |
| 258 | + | |
| 259 | + pEncCtx->iCodingIndex = 0; | |
| 260 | + | |
| 261 | + // reset_ref_list | |
| 262 | + | |
| 263 | + // rc_init_gop | |
| 264 | + } else if (keFrameType == WELS_FRAME_TYPE_I) { | |
| 265 | + if (pEncCtx->iPOC < (1 << pEncCtx->pSps->iLog2MaxPocLsb) - 2) // if iPOC type is no 0, this need be modification | |
| 266 | + pEncCtx->iPOC += 2; // for POC type 0 | |
| 267 | + else | |
| 268 | + pEncCtx->iPOC = 0; | |
| 269 | + | |
| 270 | + if (pEncCtx->eLastNalPriority != 0) { | |
| 271 | + if (pEncCtx->iFrameNum < (1 << pEncCtx->pSps->uiLog2MaxFrameNum) - 1) | |
| 272 | + ++ pEncCtx->iFrameNum; | |
| 273 | + else | |
| 274 | + pEncCtx->iFrameNum = 0; // if iFrameNum overflow | |
| 275 | + } | |
| 276 | + | |
| 277 | + pEncCtx->eNalType = NAL_UNIT_CODED_SLICE; | |
| 278 | + pEncCtx->eSliceType = I_SLICE; | |
| 279 | + pEncCtx->eNalPriority = NRI_PRI_HIGHEST; | |
| 280 | + | |
| 281 | + // rc_init_gop | |
| 282 | + } else { // B pictures are not supported now, any else? | |
| 283 | + assert (0); | |
| 284 | + } | |
| 285 | + | |
| 286 | +#if defined(STAT_OUTPUT) | |
| 287 | + memset (&pEncCtx->sPerInfo, 0, sizeof (SStatSliceInfo)); | |
| 288 | +#endif//FRAME_INFO_OUTPUT | |
| 289 | + | |
| 290 | +#if defined(MT_ENABLED) && defined(PACKING_ONE_SLICE_PER_LAYER) | |
| 291 | + if (pEncCtx->pSvcParam->iMultipleThreadIdc > 1) | |
| 292 | + reset_env_mt (pEncCtx); | |
| 293 | +#endif | |
| 294 | +} | |
| 295 | + | |
| 296 | +EFrameType DecideFrameType (sWelsEncCtx* pEncCtx, const int8_t kiSpatialNum) { | |
| 297 | + SWelsSvcCodingParam* pSvcParam = pEncCtx->pSvcParam; | |
| 298 | + EFrameType iFrameType = WELS_FRAME_TYPE_AUTO; | |
| 299 | + bool_t bSceneChangeFlag = false; | |
| 300 | + | |
| 301 | + // perform scene change detection | |
| 302 | + if ((!pSvcParam->bEnableSceneChangeDetect) || pEncCtx->pVaa->bIdrPeriodFlag || | |
| 303 | + (kiSpatialNum < pSvcParam->iNumDependencyLayer) | |
| 304 | + || (pEncCtx->uiFrameIdxRc < (VGOP_SIZE << 1))) { // avoid too frequent I frame coding, rc control | |
| 305 | + bSceneChangeFlag = false; | |
| 306 | + } else { | |
| 307 | + bSceneChangeFlag = pEncCtx->pVaa->bSceneChangeFlag; | |
| 308 | + } | |
| 309 | + | |
| 310 | + //scene_changed_flag: RC enable && iSpatialNum == pSvcParam->iNumDependencyLayer | |
| 311 | + //bIdrPeriodFlag: RC disable || iSpatialNum != pSvcParam->iNumDependencyLayer | |
| 312 | + //pEncCtx->bEncCurFrmAsIdrFlag: 1. first frame should be IDR; 2. idr pause; 3. idr request | |
| 313 | + iFrameType = (pEncCtx->pVaa->bIdrPeriodFlag || bSceneChangeFlag | |
| 314 | + || pEncCtx->bEncCurFrmAsIdrFlag) ? WELS_FRAME_TYPE_IDR : WELS_FRAME_TYPE_P; | |
| 315 | + | |
| 316 | + if (WELS_FRAME_TYPE_P == iFrameType && pEncCtx->iSkipFrameFlag > 0) { // for frame skip, 1/5/2010 | |
| 317 | + -- pEncCtx->iSkipFrameFlag; | |
| 318 | + iFrameType = WELS_FRAME_TYPE_SKIP; | |
| 319 | + } else if (WELS_FRAME_TYPE_IDR == iFrameType) { | |
| 320 | + pEncCtx->iCodingIndex = 0; | |
| 321 | + } | |
| 322 | + | |
| 323 | + return iFrameType; | |
| 324 | +} | |
| 325 | + | |
| 326 | +/*! | |
| 327 | + * \brief Dump reconstruction for dependency layer | |
| 328 | + */ | |
| 329 | + | |
| 330 | +extern "C" void DumpDependencyRec (SPicture* pCurPicture, const str_t* kpFileName, const int8_t kiDid) { | |
| 331 | + FILE* pDumpRecFile = NULL; | |
| 332 | + static bool_t bDependencyRecFlag[MAX_DEPENDENCY_LAYER] = {0}; | |
| 333 | + int32_t iWrittenSize = 0; | |
| 334 | + | |
| 335 | + if (NULL == pCurPicture || NULL == kpFileName || kiDid >= MAX_DEPENDENCY_LAYER) | |
| 336 | + return; | |
| 337 | + | |
| 338 | + if (bDependencyRecFlag[kiDid]) { | |
| 339 | + if (STRNLEN (kpFileName, MAX_FNAME_LEN) > 0) // confirmed_safe_unsafe_usage | |
| 340 | +#if defined(__GNUC__) || (defined(WIN32) && defined(_MSC_VER) && (_MSC_VER<1500)) | |
| 341 | + pDumpRecFile = FOPEN (kpFileName, "ab"); | |
| 342 | +#elif defined(WIN32) && defined(_MSC_VER) && (_MSC_VER>=1500) // vs2008 | |
| 343 | + FOPEN (&pDumpRecFile, kpFileName, "ab"); | |
| 344 | +#endif//__GNUC__.. | |
| 345 | + else { | |
| 346 | + str_t sDependencyRecFileName[16] = {0}; | |
| 347 | +#if defined(WIN32) && defined(_MSC_VER) && (_MSC_VER>=1500) // vs2008 | |
| 348 | + SNPRINTF (sDependencyRecFileName, 16, 16, "rec%d.yuv", kiDid); // confirmed_safe_unsafe_usage | |
| 349 | + FOPEN (&pDumpRecFile, sDependencyRecFileName, "ab"); | |
| 350 | +#else | |
| 351 | + SNPRINTF (sDependencyRecFileName, 16, "rec%d.yuv", kiDid); // confirmed_safe_unsafe_usage | |
| 352 | + pDumpRecFile = FOPEN (sDependencyRecFileName, "ab"); | |
| 353 | +#endif//WIN32.. | |
| 354 | + } | |
| 355 | + if (NULL != pDumpRecFile) | |
| 356 | + fseek (pDumpRecFile, 0, SEEK_END); | |
| 357 | + } else { | |
| 358 | + if (STRNLEN (kpFileName, MAX_FNAME_LEN) > 0) { // confirmed_safe_unsafe_usage | |
| 359 | +#if defined(WIN32) && defined(_MSC_VER) && (_MSC_VER>=1500) // vs2008 | |
| 360 | + FOPEN (&pDumpRecFile, kpFileName, "wb"); | |
| 361 | +#else | |
| 362 | + pDumpRecFile = FOPEN (kpFileName, "wb"); | |
| 363 | +#endif//WIN32.. | |
| 364 | + } else { | |
| 365 | + str_t sDependencyRecFileName[16] = {0}; | |
| 366 | +#if defined(WIN32) && defined(_MSC_VER) && (_MSC_VER>=1500) // vs2008 | |
| 367 | + SNPRINTF (sDependencyRecFileName, 16, 16, "rec%d.yuv", kiDid); // confirmed_safe_unsafe_usage | |
| 368 | + FOPEN (&pDumpRecFile, sDependencyRecFileName, "wb"); | |
| 369 | +#else | |
| 370 | + SNPRINTF (sDependencyRecFileName, 16, "rec%d.yuv", kiDid); // confirmed_safe_unsafe_usage | |
| 371 | + pDumpRecFile = FOPEN (sDependencyRecFileName, "wb"); | |
| 372 | +#endif//WIN32.. | |
| 373 | + } | |
| 374 | + bDependencyRecFlag[kiDid] = true; | |
| 375 | + } | |
| 376 | + | |
| 377 | + if (NULL != pDumpRecFile) { | |
| 378 | + int32_t i = 0; | |
| 379 | + int32_t j = 0; | |
| 380 | + const int32_t kiStrideY = pCurPicture->iLineSize[0]; | |
| 381 | + const int32_t kiLumaWidth = pCurPicture->iWidthInPixel; | |
| 382 | + const int32_t kiLumaHeight = pCurPicture->iHeightInPixel; | |
| 383 | + const int32_t kiChromaWidth = kiLumaWidth >> 1; | |
| 384 | + const int32_t kiChromaHeight = kiLumaHeight >> 1; | |
| 385 | + | |
| 386 | + for (j = 0; j < kiLumaHeight; ++ j) { | |
| 387 | + iWrittenSize = fwrite (&pCurPicture->pData[0][j * kiStrideY], 1, kiLumaWidth, pDumpRecFile); | |
| 388 | + assert (iWrittenSize == kiLumaWidth); | |
| 389 | + if (iWrittenSize < kiLumaWidth) { | |
| 390 | + assert (0); // make no sense for us if writing failed | |
| 391 | + fclose (pDumpRecFile); | |
| 392 | + return; | |
| 393 | + } | |
| 394 | + } | |
| 395 | + for (i = 1; i < I420_PLANES; ++ i) { | |
| 396 | + const int32_t kiStrideUV = pCurPicture->iLineSize[i]; | |
| 397 | + for (j = 0; j < kiChromaHeight; ++ j) { | |
| 398 | + iWrittenSize = fwrite (&pCurPicture->pData[i][j * kiStrideUV], 1, kiChromaWidth, pDumpRecFile); | |
| 399 | + assert (iWrittenSize == kiChromaWidth); | |
| 400 | + if (iWrittenSize < kiChromaWidth) { | |
| 401 | + assert (0); // make no sense for us if writing failed | |
| 402 | + fclose (pDumpRecFile); | |
| 403 | + return; | |
| 404 | + } | |
| 405 | + } | |
| 406 | + } | |
| 407 | + fclose (pDumpRecFile); | |
| 408 | + pDumpRecFile = NULL; | |
| 409 | + } | |
| 410 | +} | |
| 411 | + | |
| 412 | +/*! | |
| 413 | + * \brief Dump the reconstruction pictures | |
| 414 | + */ | |
| 415 | + | |
| 416 | +void DumpRecFrame (SPicture* pCurPicture, const str_t* kpFileName) { | |
| 417 | + FILE* pDumpRecFile = NULL; | |
| 418 | + static bool_t bRecFlag = false; | |
| 419 | + int32_t iWrittenSize = 0; | |
| 420 | + | |
| 421 | + if (NULL == pCurPicture || NULL == kpFileName) | |
| 422 | + return; | |
| 423 | + | |
| 424 | + if (bRecFlag) { | |
| 425 | + if (STRNLEN (kpFileName, MAX_FNAME_LEN) > 0) { // confirmed_safe_unsafe_usage | |
| 426 | +#if defined(WIN32) && defined(_MSC_VER) && (_MSC_VER>=1500) // vs2008 | |
| 427 | + FOPEN (&pDumpRecFile, kpFileName, "ab"); | |
| 428 | +#else | |
| 429 | + pDumpRecFile = FOPEN (kpFileName, "ab"); | |
| 430 | +#endif//WIN32 | |
| 431 | + } else { | |
| 432 | +#if defined(WIN32) && defined(_MSC_VER) && (_MSC_VER>=1500) // vs2008 | |
| 433 | + FOPEN (&pDumpRecFile, "rec.yuv", "ab"); | |
| 434 | +#else | |
| 435 | + pDumpRecFile = FOPEN ("rec.yuv", "ab"); | |
| 436 | +#endif//WIN32 | |
| 437 | + } | |
| 438 | + if (NULL != pDumpRecFile) | |
| 439 | + fseek (pDumpRecFile, 0, SEEK_END); | |
| 440 | + } else { | |
| 441 | + if (STRNLEN (kpFileName, MAX_FNAME_LEN) > 0) { // confirmed_safe_unsafe_usage | |
| 442 | +#if defined(WIN32) && defined(_MSC_VER) && (_MSC_VER>=1500) // vs2008 | |
| 443 | + FOPEN (&pDumpRecFile, kpFileName, "wb"); | |
| 444 | +#else | |
| 445 | + pDumpRecFile = FOPEN (kpFileName, "wb"); | |
| 446 | +#endif//WIN32 | |
| 447 | + } else { | |
| 448 | +#if defined(WIN32) && defined(_MSC_VER) && (_MSC_VER>=1500) // vs2008 | |
| 449 | + FOPEN (&pDumpRecFile, "rec.yuv", "wb"); | |
| 450 | +#else | |
| 451 | + pDumpRecFile = FOPEN ("rec.yuv", "wb"); | |
| 452 | +#endif//WIN32.. | |
| 453 | + } | |
| 454 | + bRecFlag = true; | |
| 455 | + } | |
| 456 | + | |
| 457 | + if (NULL != pDumpRecFile) { | |
| 458 | + int32_t i = 0; | |
| 459 | + int32_t j = 0; | |
| 460 | + const int32_t kiStrideY = pCurPicture->iLineSize[0]; | |
| 461 | + const int32_t kiLumaWidth = pCurPicture->iWidthInPixel; | |
| 462 | + const int32_t kiLumaHeight = pCurPicture->iHeightInPixel; | |
| 463 | + const int32_t kiChromaWidth = kiLumaWidth >> 1; | |
| 464 | + const int32_t kiChromaHeight = kiLumaHeight >> 1; | |
| 465 | + | |
| 466 | + for (j = 0; j < kiLumaHeight; ++ j) { | |
| 467 | + iWrittenSize = fwrite (&pCurPicture->pData[0][j * kiStrideY], 1, kiLumaWidth, pDumpRecFile); | |
| 468 | + assert (iWrittenSize == kiLumaWidth); | |
| 469 | + if (iWrittenSize < kiLumaWidth) { | |
| 470 | + assert (0); // make no sense for us if writing failed | |
| 471 | + fclose (pDumpRecFile); | |
| 472 | + return; | |
| 473 | + } | |
| 474 | + } | |
| 475 | + for (i = 1; i < I420_PLANES; ++ i) { | |
| 476 | + const int32_t kiStrideUV = pCurPicture->iLineSize[i]; | |
| 477 | + for (j = 0; j < kiChromaHeight; ++ j) { | |
| 478 | + iWrittenSize = fwrite (&pCurPicture->pData[i][j * kiStrideUV], 1, kiChromaWidth, pDumpRecFile); | |
| 479 | + assert (iWrittenSize == kiChromaWidth); | |
| 480 | + if (iWrittenSize < kiChromaWidth) { | |
| 481 | + assert (0); // make no sense for us if writing failed | |
| 482 | + fclose (pDumpRecFile); | |
| 483 | + return; | |
| 484 | + } | |
| 485 | + } | |
| 486 | + } | |
| 487 | + fclose (pDumpRecFile); | |
| 488 | + pDumpRecFile = NULL; | |
| 489 | + } | |
| 490 | +} | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | +/***********************************************************************************/ | |
| 495 | +void WelsSetMemZero_c (void* pDst, int32_t iSize) { // confirmed_safe_unsafe_usage | |
| 496 | + memset (pDst, 0, iSize); | |
| 497 | +} | |
| 498 | +} | |