Coverage Report

Created: 2026-01-20 07:37

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openh264/codec/encoder/plus/src/welsEncoderExt.cpp
Line
Count
Source
1
/*!
2
 * \copy
3
 *     Copyright (c)  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
33
#include <assert.h>
34
#include "welsEncoderExt.h"
35
#include "welsCodecTrace.h"
36
#include "typedefs.h"
37
#include "wels_const.h"
38
#include "utils.h"
39
#include "macros.h"
40
#include "version.h"
41
#include "crt_util_safe_x.h" // Safe CRT routines like util for cross platforms
42
#include "ref_list_mgr_svc.h"
43
#include "codec_ver.h"
44
45
#include <time.h>
46
#include <measure_time.h>
47
#if defined(_WIN32) /*&& defined(_DEBUG)*/
48
49
#include <windows.h>
50
#include <stdio.h>
51
#include <stdarg.h>
52
#include <sys/types.h>
53
#else
54
#include <sys/time.h>
55
#endif
56
57
namespace WelsEnc {
58
59
/*
60
 *  CWelsH264SVCEncoder class implementation
61
 */
62
CWelsH264SVCEncoder::CWelsH264SVCEncoder()
63
0
  : m_pEncContext (NULL),
64
0
    m_pWelsTrace (NULL),
65
0
    m_iMaxPicWidth (0),
66
0
    m_iMaxPicHeight (0),
67
0
    m_iCspInternal (0),
68
0
    m_bInitialFlag (false) {
69
#ifdef REC_FRAME_COUNT
70
  int32_t m_uiCountFrameNum = 0;
71
#endif//REC_FRAME_COUNT
72
73
#ifdef OUTPUT_BIT_STREAM
74
  char strStreamFileName[1024] = { 0 };  //for .264
75
  int32_t iBufferUsed = 0;
76
  int32_t iBufferLeft = 1023;
77
  int32_t iCurUsed;
78
79
  char strLenFileName[1024] = { 0 }; //for .len
80
  int32_t iBufferUsedSize = 0;
81
  int32_t iBufferLeftSize = 1023;
82
  int32_t iCurUsedSize;
83
#endif//OUTPUT_BIT_STREAM
84
85
#ifdef OUTPUT_BIT_STREAM
86
  SWelsTime tTime;
87
88
  WelsGetTimeOfDay (&tTime);
89
90
  iCurUsed      = WelsSnprintf (strStreamFileName, iBufferLeft, "enc_bs_0x%p_", (void*)this);
91
  iCurUsedSize  = WelsSnprintf (strLenFileName, iBufferLeftSize, "enc_size_0x%p_", (void*)this);
92
93
94
  iBufferUsed += iCurUsed;
95
  iBufferLeft -= iCurUsed;
96
  if (iBufferLeft > 0) {
97
    iCurUsed = WelsStrftime (&strStreamFileName[iBufferUsed], iBufferLeft, "%y%m%d%H%M%S", &tTime);
98
    iBufferUsed += iCurUsed;
99
    iBufferLeft -= iCurUsed;
100
  }
101
102
  iBufferUsedSize += iCurUsedSize;
103
  iBufferLeftSize -= iCurUsedSize;
104
  if (iBufferLeftSize > 0) {
105
    iCurUsedSize = WelsStrftime (&strLenFileName[iBufferUsedSize], iBufferLeftSize, "%y%m%d%H%M%S", &tTime);
106
    iBufferUsedSize += iCurUsedSize;
107
    iBufferLeftSize -= iCurUsedSize;
108
  }
109
110
  if (iBufferLeft > 0) {
111
    iCurUsed = WelsSnprintf (&strStreamFileName[iBufferUsed], iBufferLeft, ".%03.3u.264",
112
                             WelsGetMillisecond (&tTime));
113
    iBufferUsed += iCurUsed;
114
    iBufferLeft -= iCurUsed;
115
  }
116
117
  if (iBufferLeftSize > 0) {
118
    iCurUsedSize = WelsSnprintf (&strLenFileName[iBufferUsedSize], iBufferLeftSize, ".%03.3u.len",
119
                                 WelsGetMillisecond (&tTime));
120
    iBufferUsedSize += iCurUsedSize;
121
    iBufferLeftSize -= iCurUsedSize;
122
  }
123
124
  m_pFileBs     = WelsFopen (strStreamFileName, "wb");
125
  m_pFileBsSize = WelsFopen (strLenFileName, "wb");
126
127
  m_bSwitch      = false;
128
  m_iSwitchTimes = 0;
129
#endif//OUTPUT_BIT_STREAM
130
131
0
  InitEncoder();
132
0
}
133
134
0
CWelsH264SVCEncoder::~CWelsH264SVCEncoder() {
135
0
  if (m_pWelsTrace) {
136
0
    WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_INFO, "CWelsH264SVCEncoder::~CWelsH264SVCEncoder()");
137
0
  }
138
139
#ifdef REC_FRAME_COUNT
140
  m_uiCountFrameNum = 0;
141
#endif//REC_FRAME_COUNT
142
143
#ifdef OUTPUT_BIT_STREAM
144
  if (m_pFileBs) {
145
    WelsFclose (m_pFileBs);
146
    m_pFileBs = NULL;
147
  }
148
  if (m_pFileBsSize) {
149
    WelsFclose (m_pFileBsSize);
150
    m_pFileBsSize = NULL;
151
  }
152
  m_bSwitch = false;
153
  m_iSwitchTimes = 0;
154
#endif//OUTPUT_BIT_STREAM
155
156
0
  Uninitialize();
157
158
0
  if (m_pWelsTrace) {
159
0
    delete m_pWelsTrace;
160
0
    m_pWelsTrace = NULL;
161
0
  }
162
0
}
163
164
0
void CWelsH264SVCEncoder::InitEncoder (void) {
165
166
0
  m_pWelsTrace = new welsCodecTrace();
167
0
  if (m_pWelsTrace == NULL) {
168
0
    return;
169
0
  }
170
0
  m_pWelsTrace->SetCodecInstance (this);
171
0
}
172
173
/* Interfaces override from ISVCEncoder */
174
175
0
int CWelsH264SVCEncoder::GetDefaultParams (SEncParamExt* argv) {
176
0
  SWelsSvcCodingParam::FillDefault (*argv);
177
0
  return cmResultSuccess;
178
0
}
179
180
/*
181
 *  SVC Encoder Initialization
182
 */
183
0
int CWelsH264SVCEncoder::Initialize (const SEncParamBase* argv) {
184
0
  if (m_pWelsTrace == NULL) {
185
0
    return cmMallocMemeError;
186
0
  }
187
188
0
  WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_INFO, "CWelsH264SVCEncoder::InitEncoder(), openh264 codec version = %s",
189
0
           VERSION_NUMBER);
190
191
0
  if (NULL == argv) {
192
0
    WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_ERROR, "CWelsH264SVCEncoder::Initialize(), invalid argv= 0x%p",
193
0
             argv);
194
0
    return cmInitParaError;
195
0
  }
196
197
0
  SWelsSvcCodingParam sConfig;
198
  // Convert SEncParamBase into WelsSVCParamConfig here..
199
0
  if (sConfig.ParamBaseTranscode (*argv)) {
200
0
    WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_ERROR,
201
0
             "CWelsH264SVCEncoder::Initialize(), parameter_translation failed.");
202
0
    TraceParamInfo (&sConfig);
203
0
    Uninitialize();
204
0
    return cmInitParaError;
205
0
  }
206
207
0
  return InitializeInternal (&sConfig);
208
0
}
209
210
0
int CWelsH264SVCEncoder::InitializeExt (const SEncParamExt* argv) {
211
0
  if (m_pWelsTrace == NULL) {
212
0
    return cmMallocMemeError;
213
0
  }
214
215
0
  WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_INFO, "CWelsH264SVCEncoder::InitEncoder(), openh264 codec version = %s",
216
0
           VERSION_NUMBER);
217
218
0
  if (NULL == argv) {
219
0
    WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_ERROR, "CWelsH264SVCEncoder::InitializeExt(), invalid argv= 0x%p",
220
0
             argv);
221
0
    return cmInitParaError;
222
0
  }
223
224
0
  SWelsSvcCodingParam sConfig;
225
  // Convert SEncParamExt into WelsSVCParamConfig here..
226
0
  if (sConfig.ParamTranscode (*argv)) {
227
0
    WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_ERROR,
228
0
             "CWelsH264SVCEncoder::InitializeExt(), parameter_translation failed.");
229
0
    TraceParamInfo (&sConfig);
230
0
    Uninitialize();
231
0
    return cmInitParaError;
232
0
  }
233
234
0
  return InitializeInternal (&sConfig);
235
0
}
236
237
0
int CWelsH264SVCEncoder::InitializeInternal (SWelsSvcCodingParam* pCfg) {
238
0
  if (NULL == pCfg) {
239
0
    WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_ERROR, "CWelsH264SVCEncoder::Initialize(), invalid argv= 0x%p.",
240
0
             pCfg);
241
0
    return cmInitParaError;
242
0
  }
243
244
0
  if (m_bInitialFlag) {
245
0
    WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_WARNING,
246
0
             "CWelsH264SVCEncoder::Initialize(), reinitialize, m_bInitialFlag= %d.",
247
0
             m_bInitialFlag);
248
0
    Uninitialize();
249
0
  }
250
  // Check valid parameters
251
0
  const int32_t iNumOfLayers = pCfg->iSpatialLayerNum;
252
0
  if (iNumOfLayers < 1 || iNumOfLayers > MAX_DEPENDENCY_LAYER) {
253
0
    WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_ERROR,
254
0
             "CWelsH264SVCEncoder::Initialize(), invalid iSpatialLayerNum= %d, valid at range of [1, %d].", iNumOfLayers,
255
0
             MAX_DEPENDENCY_LAYER);
256
0
    Uninitialize();
257
0
    return cmInitParaError;
258
0
  }
259
0
  if (pCfg->iTemporalLayerNum < 1)
260
0
    pCfg->iTemporalLayerNum = 1;
261
0
  if (pCfg->iTemporalLayerNum > MAX_TEMPORAL_LEVEL) {
262
0
    WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_ERROR,
263
0
             "CWelsH264SVCEncoder::Initialize(), invalid iTemporalLayerNum= %d, valid at range of [1, %d].",
264
0
             pCfg->iTemporalLayerNum, MAX_TEMPORAL_LEVEL);
265
0
    Uninitialize();
266
0
    return cmInitParaError;
267
0
  }
268
269
  // assert( cfg.uiGopSize >= 1 && ( cfg.uiIntraPeriod && (cfg.uiIntraPeriod % cfg.uiGopSize) == 0) );
270
271
0
  if (pCfg->uiGopSize < 1 || pCfg->uiGopSize > MAX_GOP_SIZE) {
272
0
    WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_ERROR,
273
0
             "CWelsH264SVCEncoder::Initialize(), invalid uiGopSize= %d, valid at range of [1, %d].", pCfg->uiGopSize,
274
0
             MAX_GOP_SIZE);
275
0
    Uninitialize();
276
0
    return cmInitParaError;
277
0
  }
278
279
0
  if (!WELS_POWER2_IF (pCfg->uiGopSize)) {
280
0
    WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_ERROR,
281
0
             "CWelsH264SVCEncoder::Initialize(), invalid uiGopSize= %d, valid at range of [1, %d] and yield to power of 2.",
282
0
             pCfg->uiGopSize, MAX_GOP_SIZE);
283
0
    Uninitialize();
284
0
    return cmInitParaError;
285
0
  }
286
287
0
  if (pCfg->uiIntraPeriod && pCfg->uiIntraPeriod < pCfg->uiGopSize) {
288
0
    WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_ERROR,
289
0
             "CWelsH264SVCEncoder::Initialize(), invalid uiIntraPeriod= %d, valid in case it equals to 0 for unlimited intra period or exceeds specified uiGopSize= %d.",
290
0
             pCfg->uiIntraPeriod, pCfg->uiGopSize);
291
0
    Uninitialize();
292
0
    return cmInitParaError;
293
0
  }
294
295
0
  if ((pCfg->uiIntraPeriod && (pCfg->uiIntraPeriod & (pCfg->uiGopSize - 1)) != 0)) {
296
0
    WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_ERROR,
297
0
             "CWelsH264SVCEncoder::Initialize(), invalid uiIntraPeriod= %d, valid in case it equals to 0 for unlimited intra period or exceeds specified uiGopSize= %d also multiple of it.",
298
0
             pCfg->uiIntraPeriod, pCfg->uiGopSize);
299
0
    Uninitialize();
300
0
    return cmInitParaError;
301
0
  }
302
0
  if (pCfg->iUsageType == SCREEN_CONTENT_REAL_TIME) {
303
0
    if (pCfg->bEnableLongTermReference) {
304
0
      pCfg->iLTRRefNum = LONG_TERM_REF_NUM_SCREEN;
305
0
      if (pCfg->iNumRefFrame == AUTO_REF_PIC_COUNT)
306
0
        pCfg->iNumRefFrame = WELS_MAX (1, WELS_LOG2 (pCfg->uiGopSize)) + pCfg->iLTRRefNum;
307
0
    } else {
308
0
      pCfg->iLTRRefNum = 0;
309
0
      if (pCfg->iNumRefFrame == AUTO_REF_PIC_COUNT)
310
0
        pCfg->iNumRefFrame = WELS_MAX (1, pCfg->uiGopSize >> 1);
311
0
    }
312
0
  } else {
313
0
    pCfg->iLTRRefNum = pCfg->bEnableLongTermReference ? LONG_TERM_REF_NUM : 0;
314
0
    if (pCfg->iNumRefFrame == AUTO_REF_PIC_COUNT) {
315
0
      pCfg->iNumRefFrame = ((pCfg->uiGopSize >> 1) > 1) ? ((pCfg->uiGopSize >> 1) + pCfg->iLTRRefNum) :
316
0
                           (MIN_REF_PIC_COUNT + pCfg->iLTRRefNum);
317
0
      pCfg->iNumRefFrame = WELS_CLIP3 (pCfg->iNumRefFrame, MIN_REF_PIC_COUNT, MAX_REFERENCE_PICTURE_COUNT_NUM_CAMERA);
318
0
    }
319
0
  }
320
321
0
  if (pCfg->iLtrMarkPeriod == 0) {
322
0
    pCfg->iLtrMarkPeriod = 30;
323
0
  }
324
325
0
  const int32_t kiDecStages = WELS_LOG2 (pCfg->uiGopSize);
326
0
  pCfg->iTemporalLayerNum        = (int8_t) (1 + kiDecStages);
327
0
  pCfg->iLoopFilterAlphaC0Offset = WELS_CLIP3 (pCfg->iLoopFilterAlphaC0Offset, -6, 6);
328
0
  pCfg->iLoopFilterBetaOffset    = WELS_CLIP3 (pCfg->iLoopFilterBetaOffset, -6, 6);
329
330
  // decide property list size between INIT_TYPE_PARAMETER_BASED/INIT_TYPE_CONFIG_BASED
331
0
  m_iMaxPicWidth  = pCfg->iPicWidth;
332
0
  m_iMaxPicHeight = pCfg->iPicHeight;
333
334
0
  TraceParamInfo (pCfg);
335
0
  if (WelsInitEncoderExt (&m_pEncContext, pCfg, &m_pWelsTrace->m_sLogCtx, NULL)) {
336
0
    WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_ERROR, "CWelsH264SVCEncoder::Initialize(), WelsInitEncoderExt failed.");
337
0
    WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_DEBUG,
338
0
             "Problematic Input Base Param: iUsageType=%d, Resolution=%dx%d, FR=%f, TLayerNum=%d, DLayerNum=%d",
339
0
             pCfg->iUsageType, pCfg->iPicWidth, pCfg->iPicHeight, pCfg->fMaxFrameRate, pCfg->iTemporalLayerNum,
340
0
             pCfg->iSpatialLayerNum);
341
0
    Uninitialize();
342
0
    return cmInitParaError;
343
0
  }
344
345
0
  m_bInitialFlag  = true;
346
347
0
  return cmResultSuccess;
348
0
}
349
350
/*
351
 *  SVC Encoder Uninitialization
352
 */
353
0
int32_t CWelsH264SVCEncoder::Uninitialize() {
354
0
  if (!m_bInitialFlag) {
355
0
    return 0;
356
0
  }
357
358
0
  WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_INFO, "CWelsH264SVCEncoder::Uninitialize(), openh264 codec version = %s.",
359
0
           VERSION_NUMBER);
360
361
0
  if (NULL != m_pEncContext) {
362
0
    WelsUninitEncoderExt (&m_pEncContext);
363
0
    m_pEncContext = NULL;
364
0
  }
365
366
0
  m_bInitialFlag = false;
367
368
0
  return 0;
369
0
}
370
371
372
/*
373
 *  SVC core encoding
374
 */
375
0
int CWelsH264SVCEncoder::EncodeFrame (const SSourcePicture* kpSrcPic, SFrameBSInfo* pBsInfo) {
376
0
  if (! (kpSrcPic && m_bInitialFlag && pBsInfo)) {
377
0
    WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_ERROR, "CWelsH264SVCEncoder::EncodeFrame(), cmInitParaError.");
378
0
    return cmInitParaError;
379
0
  }
380
0
  if (kpSrcPic->iColorFormat != videoFormatI420) {
381
0
    WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_ERROR, "CWelsH264SVCEncoder::EncodeFrame(), wrong iColorFormat %d",
382
0
             kpSrcPic->iColorFormat);
383
0
    return cmInitParaError;
384
0
  }
385
386
0
  const int32_t kiEncoderReturn = EncodeFrameInternal (kpSrcPic, pBsInfo);
387
388
0
  if (kiEncoderReturn != cmResultSuccess) {
389
0
    WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_ERROR, "CWelsH264SVCEncoder::EncodeFrame(), kiEncoderReturn %d",
390
0
             kiEncoderReturn);
391
0
    return kiEncoderReturn;
392
0
  }
393
394
#ifdef REC_FRAME_COUNT
395
  ++ m_uiCountFrameNum;
396
  WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_INFO,
397
           "CWelsH264SVCEncoder::EncodeFrame(), m_uiCountFrameNum= %d,", m_uiCountFrameNum);
398
#endif//REC_FRAME_COUNT
399
400
0
  return kiEncoderReturn;
401
0
}
402
403
404
0
int CWelsH264SVCEncoder ::EncodeFrameInternal (const SSourcePicture*  pSrcPic, SFrameBSInfo* pBsInfo) {
405
406
0
  if ((pSrcPic->iPicWidth < 16) || ((pSrcPic->iPicHeight < 16))) {
407
0
    WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_ERROR, "Don't support width(%d) or height(%d) which is less than 16!",
408
0
             pSrcPic->iPicWidth, pSrcPic->iPicHeight);
409
0
    return cmUnsupportedData;
410
0
  }
411
412
0
  const int64_t kiBeforeFrameUs = WelsTime();
413
0
  const int32_t kiEncoderReturn = WelsEncoderEncodeExt (m_pEncContext, pBsInfo, pSrcPic);
414
0
  const int64_t kiCurrentFrameMs = (WelsTime() - kiBeforeFrameUs) / 1000;
415
0
  if ((kiEncoderReturn == ENC_RETURN_MEMALLOCERR) || (kiEncoderReturn == ENC_RETURN_MEMOVERFLOWFOUND)
416
0
      || (kiEncoderReturn == ENC_RETURN_VLCOVERFLOWFOUND)) {
417
0
    WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_DEBUG, "CWelsH264SVCEncoder::EncodeFrame() not succeed, err=%d",
418
0
             kiEncoderReturn);
419
0
    WelsUninitEncoderExt (&m_pEncContext);
420
0
    return cmMallocMemeError;
421
0
  } else if ((kiEncoderReturn != ENC_RETURN_SUCCESS) && (kiEncoderReturn == ENC_RETURN_CORRECTED)) {
422
0
    WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_ERROR, "unexpected return(%d) from EncodeFrameInternal()!",
423
0
             kiEncoderReturn);
424
0
    return cmUnknownReason;
425
0
  }
426
427
0
  UpdateStatistics (pBsInfo, kiCurrentFrameMs);
428
429
  ///////////////////for test
430
#ifdef OUTPUT_BIT_STREAM
431
  if (pBsInfo->eFrameType != videoFrameTypeInvalid && pBsInfo->eFrameType != videoFrameTypeSkip) {
432
    SLayerBSInfo* pLayer = NULL;
433
    int32_t i = 0, j = 0, iCurLayerBits = 0, total_bits = 0;
434
435
    if (m_bSwitch) {
436
      if (m_pFileBs) {
437
        WelsFclose (m_pFileBs);
438
        m_pFileBs = NULL;
439
      }
440
      if (m_pFileBsSize) {
441
        WelsFclose (m_pFileBsSize);
442
        m_pFileBsSize = NULL;
443
      }
444
      char strStreamFileName[128] = {0};
445
      WelsSnprintf (strStreamFileName, 128, "adj%d_w%d.264", m_iSwitchTimes,
446
                    m_pEncContext->pSvcParam->iPicWidth);
447
      m_pFileBs = WelsFopen (strStreamFileName, "wb");
448
      WelsSnprintf (strStreamFileName, 128, "adj%d_w%d_size.iLen", m_iSwitchTimes,
449
                    m_pEncContext->pSvcParam->iPicWidth);
450
      m_pFileBsSize = WelsFopen (strStreamFileName, "wb");
451
452
453
      m_bSwitch = false;
454
    }
455
456
    for (i = 0; i < pBsInfo->iLayerNum; i++) {
457
      pLayer = &pBsInfo->sLayerInfo[i];
458
459
      iCurLayerBits = 0;
460
      for (j = 0; j < pLayer->iNalCount; j++) {
461
        iCurLayerBits += pLayer->pNalLengthInByte[j];
462
      }
463
      total_bits += iCurLayerBits;
464
      if (m_pFileBs != NULL)
465
        WelsFwrite (pLayer->pBsBuf, 1, iCurLayerBits, m_pFileBs);
466
    }
467
468
    if (m_pFileBsSize != NULL)
469
      WelsFwrite (&total_bits, sizeof (int32_t), 1, m_pFileBsSize);
470
  }
471
#endif //OUTPUT_BIT_STREAM
472
#ifdef DUMP_SRC_PICTURE
473
  DumpSrcPicture (pSrcPic, m_pEncContext->pSvcParam->iUsageType);
474
#endif // DUMP_SRC_PICTURE
475
476
0
  return cmResultSuccess;
477
478
0
}
479
480
0
int CWelsH264SVCEncoder::EncodeParameterSets (SFrameBSInfo* pBsInfo) {
481
0
  return WelsEncoderEncodeParameterSets (m_pEncContext, pBsInfo);
482
0
}
483
484
/*
485
 *  Force key frame
486
 */
487
0
int CWelsH264SVCEncoder::ForceIntraFrame (bool bIDR, int iLayerId) {
488
0
  if (bIDR) {
489
0
    if (! (m_pEncContext && m_bInitialFlag)) {
490
0
      return 1;
491
0
    }
492
493
0
    ForceCodingIDR (m_pEncContext, iLayerId);
494
0
  } else {
495
0
    WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_INFO,
496
0
             "CWelsH264SVCEncoder::ForceIntraFrame(),nothing to do as bIDR set to false");
497
0
  }
498
499
0
  return 0;
500
0
}
501
0
void CWelsH264SVCEncoder::TraceParamInfo (SEncParamExt* pParam) {
502
0
  WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_INFO,
503
0
           "iUsageType = %d,iPicWidth= %d;iPicHeight= %d;iTargetBitrate= %d;iMaxBitrate= %d;iRCMode= %d;iPaddingFlag= %d;iTemporalLayerNum= %d;iSpatialLayerNum= %d;fFrameRate= %.6ff;uiIntraPeriod= %d;"
504
0
           "eSpsPpsIdStrategy = %d;bPrefixNalAddingCtrl = %d;bSimulcastAVC=%d;bEnableDenoise= %d;bEnableBackgroundDetection= %d;bEnableSceneChangeDetect = %d;bEnableAdaptiveQuant= %d;bEnableFrameSkip= %d;bEnableLongTermReference= %d;iLtrMarkPeriod= %d, bIsLosslessLink=%d;"
505
0
           "iComplexityMode = %d;iNumRefFrame = %d;iEntropyCodingModeFlag = %d;uiMaxNalSize = %d;iLTRRefNum = %d;iMultipleThreadIdc = %d;iLoopFilterDisableIdc = %d (offset(alpha/beta): %d,%d;iComplexityMode = %d,iMaxQp = %d;iMinQp = %d)",
506
0
           pParam->iUsageType,
507
0
           pParam->iPicWidth,
508
0
           pParam->iPicHeight,
509
0
           pParam->iTargetBitrate,
510
0
           pParam->iMaxBitrate,
511
0
           pParam->iRCMode,
512
0
           pParam->iPaddingFlag,
513
0
           pParam->iTemporalLayerNum,
514
0
           pParam->iSpatialLayerNum,
515
0
           pParam->fMaxFrameRate,
516
0
           pParam->uiIntraPeriod,
517
0
           pParam->eSpsPpsIdStrategy,
518
0
           pParam->bPrefixNalAddingCtrl,
519
0
           pParam->bSimulcastAVC,
520
0
           pParam->bEnableDenoise,
521
0
           pParam->bEnableBackgroundDetection,
522
0
           pParam->bEnableSceneChangeDetect,
523
0
           pParam->bEnableAdaptiveQuant,
524
0
           pParam->bEnableFrameSkip,
525
0
           pParam->bEnableLongTermReference,
526
0
           pParam->iLtrMarkPeriod,
527
0
           pParam->bIsLosslessLink,
528
0
           pParam->iComplexityMode,
529
0
           pParam->iNumRefFrame,
530
0
           pParam->iEntropyCodingModeFlag,
531
0
           pParam->uiMaxNalSize,
532
0
           pParam->iLTRRefNum,
533
0
           pParam->iMultipleThreadIdc,
534
0
           pParam->iLoopFilterDisableIdc,
535
0
           pParam->iLoopFilterAlphaC0Offset,
536
0
           pParam->iLoopFilterBetaOffset,
537
0
           pParam->iComplexityMode,
538
0
           pParam->iMaxQp,
539
0
           pParam->iMinQp
540
0
          );
541
0
  int32_t i = 0;
542
0
  int32_t iSpatialLayers = (pParam->iSpatialLayerNum < MAX_SPATIAL_LAYER_NUM) ? (pParam->iSpatialLayerNum) :
543
0
                           MAX_SPATIAL_LAYER_NUM;
544
0
  while (i < iSpatialLayers) {
545
0
    SSpatialLayerConfig* pSpatialCfg = &pParam->sSpatialLayers[i];
546
0
    WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_INFO,
547
0
             "sSpatialLayers[%d]: .iVideoWidth= %d; .iVideoHeight= %d; .fFrameRate= %.6ff; .iSpatialBitrate= %d; .iMaxSpatialBitrate= %d; .sSliceArgument.uiSliceMode= %d; .sSliceArgument.iSliceNum= %d; .sSliceArgument.uiSliceSizeConstraint= %d;"
548
0
             "uiProfileIdc = %d;uiLevelIdc = %d;iDLayerQp = %d",
549
0
             i, pSpatialCfg->iVideoWidth,
550
0
             pSpatialCfg->iVideoHeight,
551
0
             pSpatialCfg->fFrameRate,
552
0
             pSpatialCfg->iSpatialBitrate,
553
0
             pSpatialCfg->iMaxSpatialBitrate,
554
0
             pSpatialCfg->sSliceArgument.uiSliceMode,
555
0
             pSpatialCfg->sSliceArgument.uiSliceNum,
556
0
             pSpatialCfg->sSliceArgument.uiSliceSizeConstraint,
557
0
             pSpatialCfg->uiProfileIdc,
558
0
             pSpatialCfg->uiLevelIdc,
559
0
             pSpatialCfg->iDLayerQp
560
0
            );
561
0
    ++ i;
562
0
  }
563
0
}
564
565
0
void CWelsH264SVCEncoder::LogStatistics (const int64_t kiCurrentFrameTs, int32_t iMaxDid) {
566
0
  for (int32_t iDid = 0; iDid <= iMaxDid; iDid++) {
567
0
    SEncoderStatistics* pStatistics = & (m_pEncContext->sEncoderStatistics[iDid]);
568
0
    WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_INFO,
569
0
             "EncoderStatistics: SpatialId = %d,%dx%d, SpeedInMs: %f, fAverageFrameRate=%f, "
570
0
             "LastFrameRate=%f, LatestBitRate=%d, LastFrameQP=%d, uiInputFrameCount=%d, uiSkippedFrameCount=%d, "
571
0
             "uiResolutionChangeTimes=%d, uIDRReqNum=%d, uIDRSentNum=%d, uLTRSentNum=NA, iTotalEncodedBytes=%lu at Ts = %" PRId64,
572
0
             iDid, pStatistics->uiWidth, pStatistics->uiHeight,
573
0
             pStatistics->fAverageFrameSpeedInMs, pStatistics->fAverageFrameRate,
574
0
             pStatistics->fLatestFrameRate, pStatistics->uiBitRate, pStatistics->uiAverageFrameQP,
575
0
             pStatistics->uiInputFrameCount, pStatistics->uiSkippedFrameCount,
576
0
             pStatistics->uiResolutionChangeTimes, pStatistics->uiIDRReqNum, pStatistics->uiIDRSentNum,
577
0
             pStatistics->iTotalEncodedBytes, kiCurrentFrameTs);
578
0
  }
579
0
}
580
581
void CWelsH264SVCEncoder::UpdateStatistics (SFrameBSInfo* pBsInfo,
582
0
    const int64_t kiCurrentFrameMs) {
583
584
0
  const int64_t kiCurrentFrameTs = m_pEncContext->uiLastTimestamp = pBsInfo->uiTimeStamp;
585
0
  const int64_t kiTimeDiff = kiCurrentFrameTs - m_pEncContext->iLastStatisticsLogTs;
586
587
0
  int32_t iMaxDid = m_pEncContext->pSvcParam->iSpatialLayerNum - 1;
588
0
  SLayerBSInfo*  pLayerInfo = &pBsInfo->sLayerInfo[0];
589
0
  uint32_t iMaxInputFrame = 0;
590
0
  float iMaxFrameRate = 0;
591
0
  for (int32_t iDid = 0; iDid <= iMaxDid; iDid++) {
592
0
    EVideoFrameType eFrameType = videoFrameTypeSkip;
593
0
    int32_t kiCurrentFrameSize = 0;
594
0
    for (int32_t iLayerNum = 0; iLayerNum < pBsInfo->iLayerNum; iLayerNum++) {
595
0
      pLayerInfo = &pBsInfo->sLayerInfo[iLayerNum];
596
0
      if ((pLayerInfo->uiLayerType == VIDEO_CODING_LAYER) && (pLayerInfo->uiSpatialId == iDid)) {
597
0
        eFrameType = pLayerInfo->eFrameType;
598
0
        for (int32_t iNalIdx = 0; iNalIdx < pLayerInfo->iNalCount; iNalIdx++) {
599
0
          kiCurrentFrameSize += pLayerInfo->pNalLengthInByte[iNalIdx];
600
0
        }
601
0
      }
602
0
    }
603
0
    SEncoderStatistics* pStatistics = & (m_pEncContext->sEncoderStatistics[iDid]);
604
0
    SSpatialLayerInternal* pSpatialLayerInternalParam = & (m_pEncContext->pSvcParam->sDependencyLayers[iDid]);
605
606
0
    if ((0 != pStatistics->uiWidth && 0 != pStatistics->uiHeight)
607
0
        && (pStatistics->uiWidth != (unsigned int) pSpatialLayerInternalParam->iActualWidth
608
0
            || pStatistics->uiHeight != (unsigned int) pSpatialLayerInternalParam->iActualHeight)) {
609
0
      pStatistics->uiResolutionChangeTimes ++;
610
0
    }
611
0
    pStatistics->uiWidth = pSpatialLayerInternalParam->iActualWidth;
612
0
    pStatistics->uiHeight = pSpatialLayerInternalParam->iActualHeight;
613
614
0
    const bool kbCurrentFrameSkipped = (videoFrameTypeSkip == eFrameType);
615
0
    pStatistics->uiInputFrameCount ++;
616
0
    pStatistics->uiSkippedFrameCount += (kbCurrentFrameSkipped ? 1 : 0);
617
0
    iMaxInputFrame = WELS_MAX (pStatistics->uiInputFrameCount, iMaxInputFrame);
618
0
    int32_t iProcessedFrameCount = pStatistics->uiInputFrameCount - pStatistics->uiSkippedFrameCount;
619
0
    if (!kbCurrentFrameSkipped && iProcessedFrameCount != 0) {
620
0
      pStatistics->fAverageFrameSpeedInMs += (kiCurrentFrameMs - pStatistics->fAverageFrameSpeedInMs) / iProcessedFrameCount;
621
0
    }
622
    // rate control related
623
0
    if (0 != m_pEncContext->uiStartTimestamp) {
624
0
      if (kiCurrentFrameTs > m_pEncContext->uiStartTimestamp + 800) {
625
0
        pStatistics->fAverageFrameRate = (static_cast<float> (pStatistics->uiInputFrameCount) * 1000 /
626
0
                                          (kiCurrentFrameTs - m_pEncContext->uiStartTimestamp));
627
0
      }
628
0
    } else {
629
0
      m_pEncContext->uiStartTimestamp = kiCurrentFrameTs;
630
0
    }
631
0
    iMaxFrameRate = WELS_MAX (iMaxFrameRate, pStatistics->fAverageFrameRate);
632
    //pStatistics->fLatestFrameRate = m_pEncContext->pWelsSvcRc->fLatestFrameRate; //TODO: finish the calculation in RC
633
    //pStatistics->uiBitRate = m_pEncContext->pWelsSvcRc->iActualBitRate; //TODO: finish the calculation in RC
634
0
    pStatistics->uiAverageFrameQP = m_pEncContext->pWelsSvcRc[iDid].iAverageFrameQp;
635
636
0
    if (videoFrameTypeIDR == eFrameType || videoFrameTypeI == eFrameType) {
637
0
      pStatistics->uiIDRSentNum ++;
638
0
    }
639
0
    if (m_pEncContext->pLtr->bLTRMarkingFlag) {
640
0
      pStatistics->uiLTRSentNum ++;
641
0
    }
642
643
0
    pStatistics->iTotalEncodedBytes += kiCurrentFrameSize;
644
645
0
    const int32_t kiDeltaFrames = static_cast<int32_t> (pStatistics->uiInputFrameCount -
646
0
                                  pStatistics->iLastStatisticsFrameCount);
647
0
    if (kiDeltaFrames > (m_pEncContext->pSvcParam->fMaxFrameRate * 2)) {
648
0
      if (kiTimeDiff >= m_pEncContext->iStatisticsLogInterval) {
649
0
        float fTimeDiffSec = kiTimeDiff / 1000.0f;
650
0
        pStatistics->fLatestFrameRate = static_cast<float> ((pStatistics->uiInputFrameCount -
651
0
                                        pStatistics->iLastStatisticsFrameCount) / fTimeDiffSec);
652
0
        pStatistics->uiBitRate = static_cast<unsigned int> ((pStatistics->iTotalEncodedBytes) * 8  / fTimeDiffSec);
653
654
0
        if (WELS_ABS (pStatistics->fLatestFrameRate - m_pEncContext->pSvcParam->fMaxFrameRate) > 30) {
655
0
          WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_WARNING,
656
0
                   "Actual input fLatestFrameRate = %f is quite different from framerate in setting %f, please check setting or timestamp unit (ms), cur_Ts = %"
657
0
                   PRId64 " start_Ts = %" PRId64,
658
0
                   pStatistics->fLatestFrameRate, m_pEncContext->pSvcParam->fMaxFrameRate, kiCurrentFrameTs,
659
0
                   static_cast<int64_t> (m_pEncContext->iLastStatisticsLogTs));
660
0
        }
661
662
0
        if (m_pEncContext->pSvcParam->iRCMode == RC_QUALITY_MODE || m_pEncContext->pSvcParam->iRCMode == RC_BITRATE_MODE) {
663
0
          if ((pStatistics->fLatestFrameRate > 0)
664
0
              && WELS_ABS (m_pEncContext->pSvcParam->fMaxFrameRate - pStatistics->fLatestFrameRate) > 5) {
665
0
            WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_WARNING,
666
0
                     "Actual input framerate %f is different from framerate in setting %f, suggest to use other rate control modes",
667
0
                     pStatistics->fLatestFrameRate, m_pEncContext->pSvcParam->fMaxFrameRate);
668
0
          }
669
0
        }
670
        // update variables
671
0
        pStatistics->iLastStatisticsBytes = pStatistics->iTotalEncodedBytes;
672
0
        pStatistics->iLastStatisticsFrameCount = pStatistics->uiInputFrameCount;
673
0
        m_pEncContext->iLastStatisticsLogTs = kiCurrentFrameTs;
674
0
        LogStatistics (kiCurrentFrameTs, iMaxDid);
675
0
        pStatistics->iTotalEncodedBytes = 0;
676
        //TODO: the following statistics will be calculated and added later
677
        //pStatistics->uiLTRSentNum
678
679
0
      }
680
0
    }
681
0
  }
682
683
0
}
684
685
/************************************************************************
686
* InDataFormat, IDRInterval, SVC Encode Param, Frame Rate, Bitrate,..
687
************************************************************************/
688
0
int CWelsH264SVCEncoder::SetOption (ENCODER_OPTION eOptionId, void* pOption) {
689
0
  if (NULL == pOption) {
690
0
    return cmInitParaError;
691
0
  }
692
693
0
  if ((NULL == m_pEncContext || false == m_bInitialFlag) && eOptionId != ENCODER_OPTION_TRACE_LEVEL
694
0
      && eOptionId != ENCODER_OPTION_TRACE_CALLBACK && eOptionId != ENCODER_OPTION_TRACE_CALLBACK_CONTEXT) {
695
0
    return cmInitExpected;
696
0
  }
697
698
0
  switch (eOptionId) {
699
0
  case ENCODER_OPTION_INTER_SPATIAL_PRED: { // Inter spatial layer prediction flag
700
0
    WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_INFO,
701
0
             "ENCODER_OPTION_INTER_SPATIAL_PRED, this feature not supported at present.");
702
0
  }
703
0
  break;
704
0
  case ENCODER_OPTION_DATAFORMAT: { // Input color space
705
0
    int32_t iValue = * ((int32_t*)pOption);
706
0
    int32_t iColorspace = iValue;
707
0
    if (iColorspace == 0) {
708
0
      return cmInitParaError;
709
0
    }
710
711
0
    m_iCspInternal = iColorspace;
712
0
    WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_INFO,
713
0
             "CWelsH264SVCEncoder::SetOption():ENCODER_OPTION_DATAFORMAT, m_iCspInternal = 0x%x", m_iCspInternal);
714
0
  }
715
0
  break;
716
0
  case ENCODER_OPTION_IDR_INTERVAL: { // IDR Interval
717
0
    int32_t iValue = * ((int32_t*)pOption);
718
0
    WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_INFO,
719
0
             "CWelsH264SVCEncoder::SetOption():ENCODER_OPTION_IDR_INTERVAL iValue = %d", iValue);
720
0
    if (iValue <= -1) {
721
0
      iValue = 0;
722
0
    }
723
0
    if (iValue == (int32_t)m_pEncContext->pSvcParam->uiIntraPeriod) {
724
0
      return cmResultSuccess;
725
0
    }
726
0
    m_pEncContext->pSvcParam->uiIntraPeriod = (uint32_t)iValue;
727
0
    WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_INFO,
728
0
             "CWelsH264SVCEncoder::SetOption():ENCODER_OPTION_IDR_INTERVAL uiIntraPeriod updated to %d",
729
0
             m_pEncContext->pSvcParam->uiIntraPeriod);
730
0
  }
731
0
  break;
732
0
  case ENCODER_OPTION_SVC_ENCODE_PARAM_BASE: { // SVC Encoding Parameter
733
0
    SEncParamBase sEncodingParam;
734
0
    SWelsSvcCodingParam sConfig;
735
0
    int32_t iTargetWidth = 0;
736
0
    int32_t iTargetHeight = 0;
737
738
0
    memcpy (&sEncodingParam, pOption, sizeof (SEncParamBase)); // confirmed_safe_unsafe_usage
739
#ifdef OUTPUT_BIT_STREAM
740
    if ((sEncodingParam.iPicWidth  != m_pEncContext->pSvcParam->sDependencyLayers[m_pEncContext->pSvcParam->iSpatialLayerNum
741
         - 1].iActualWidth) ||
742
        (sEncodingParam.iPicHeight != m_pEncContext->pSvcParam->sDependencyLayers[m_pEncContext->pSvcParam->iSpatialLayerNum -
743
                                                       1].iActualHeight)) {
744
      ++m_iSwitchTimes;
745
      m_bSwitch = true;
746
    }
747
#endif//OUTPUT_BIT_STREAM
748
0
    if (sConfig.ParamBaseTranscode (sEncodingParam)) {
749
0
      WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_ERROR,
750
0
               "CWelsH264SVCEncoder::SetOption():ENCODER_OPTION_SVC_ENCODE_PARAM_BASE, ParamTranscode failed!");
751
0
      return cmInitParaError;
752
0
    }
753
    /* New configuration available here */
754
0
    iTargetWidth        = sConfig.iPicWidth;
755
0
    iTargetHeight       = sConfig.iPicHeight;
756
0
    if (m_iMaxPicWidth != iTargetWidth
757
0
        || m_iMaxPicHeight != iTargetHeight) {
758
0
      m_iMaxPicWidth    = iTargetWidth;
759
0
      m_iMaxPicHeight   = iTargetHeight;
760
0
    }
761
0
    if (sConfig.DetermineTemporalSettings()) {
762
0
      WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_ERROR,
763
0
               "CWelsH264SVCEncoder::SetOption():ENCODER_OPTION_SVC_ENCODE_PARAM_BASE, DetermineTemporalSettings failed!");
764
0
      return cmInitParaError;
765
0
    }
766
0
    WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_INFO,
767
0
             "CWelsH264SVCEncoder::SetOption():ENCODER_OPTION_SVC_ENCODE_PARAM_BASE iUsageType = %d,iPicWidth= %d;iPicHeight= %d;iTargetBitrate= %d;fMaxFrameRate=  %.6ff;iRCMode= %d",
768
0
             sEncodingParam.iUsageType,
769
0
             sEncodingParam.iPicWidth,
770
0
             sEncodingParam.iPicHeight,
771
0
             sEncodingParam.iTargetBitrate,
772
0
             sEncodingParam.fMaxFrameRate,
773
0
             sEncodingParam.iRCMode);
774
0
    if (WelsEncoderParamAdjust (&m_pEncContext, &sConfig)) {
775
0
      return cmInitParaError;
776
0
    }
777
778
    //LogStatistics
779
0
    WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_INFO,
780
0
             "CWelsH264SVCEncoder::SetOption():ENCODER_OPTION_SVC_ENCODE_PARAM_BASE, LogStatisticsBeforeNewEncoding");
781
0
    LogStatistics (m_pEncContext->iLastStatisticsLogTs, 0);
782
0
  }
783
0
  break;
784
785
0
  case ENCODER_OPTION_SVC_ENCODE_PARAM_EXT: { // SVC Encoding Parameter
786
0
    SEncParamExt sEncodingParam;
787
0
    SWelsSvcCodingParam sConfig;
788
0
    int32_t iTargetWidth = 0;
789
0
    int32_t iTargetHeight = 0;
790
791
0
    memcpy (&sEncodingParam, pOption, sizeof (SEncParamExt)); // confirmed_safe_unsafe_usage
792
0
    TraceParamInfo (&sEncodingParam);
793
#ifdef OUTPUT_BIT_STREAM
794
    if ((sEncodingParam.sSpatialLayers[sEncodingParam.iSpatialLayerNum - 1].iVideoWidth !=
795
         m_pEncContext->pSvcParam->sDependencyLayers[m_pEncContext->pSvcParam->iSpatialLayerNum - 1].iActualWidth) ||
796
        (sEncodingParam.sSpatialLayers[sEncodingParam.iSpatialLayerNum - 1].iVideoHeight !=
797
         m_pEncContext->pSvcParam->sDependencyLayers[m_pEncContext->pSvcParam->iSpatialLayerNum - 1].iActualHeight)) {
798
      ++ m_iSwitchTimes;
799
      m_bSwitch = true;
800
    }
801
#endif//OUTPUT_BIT_STREAM
802
0
    if (sEncodingParam.iSpatialLayerNum < 1
803
0
        || sEncodingParam.iSpatialLayerNum > MAX_SPATIAL_LAYER_NUM) { // verify number of spatial layer
804
0
      WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_ERROR,
805
0
               "CWelsH264SVCEncoder::SetOption():ENCODER_OPTION_SVC_ENCODE_PARAM_EXT, iSpatialLayerNum(%d) failed!",
806
0
               sEncodingParam.iSpatialLayerNum);
807
0
      return cmInitParaError;
808
0
    }
809
810
0
    if (sConfig.ParamTranscode (sEncodingParam)) {
811
0
      WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_ERROR,
812
0
               "CWelsH264SVCEncoder::SetOption():ENCODER_OPTION_SVC_ENCODE_PARAM_EXT, ParamTranscode failed!");
813
0
      return cmInitParaError;
814
0
    }
815
0
    if (sConfig.iSpatialLayerNum < 1) {
816
0
      WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_ERROR,
817
0
               "CWelsH264SVCEncoder::SetOption():ENCODER_OPTION_SVC_ENCODE_PARAM_EXT, iSpatialLayerNum(%d) failed!",
818
0
               sConfig.iSpatialLayerNum);
819
0
      return cmInitParaError;
820
0
    }
821
0
    if (sConfig.DetermineTemporalSettings()) {
822
0
      WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_ERROR,
823
0
               "CWelsH264SVCEncoder::SetOption():ENCODER_OPTION_SVC_ENCODE_PARAM_EXT, DetermineTemporalSettings failed!");
824
0
      return cmInitParaError;
825
0
    }
826
827
    /* New configuration available here */
828
0
    iTargetWidth        = sConfig.iPicWidth;
829
0
    iTargetHeight       = sConfig.iPicHeight;
830
0
    if (m_iMaxPicWidth != iTargetWidth
831
0
        || m_iMaxPicHeight != iTargetHeight) {
832
0
      m_iMaxPicWidth    = iTargetWidth;
833
0
      m_iMaxPicHeight   = iTargetHeight;
834
0
    }
835
    /* Check every field whether there is new request for memory block changed or else, Oct. 24, 2008 */
836
0
    if (WelsEncoderParamAdjust (&m_pEncContext, &sConfig)) {
837
0
      return cmInitParaError;
838
0
    }
839
840
    //LogStatistics
841
0
    WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_INFO,
842
0
             "CWelsH264SVCEncoder::SetOption():ENCODER_OPTION_SVC_ENCODE_PARAM_EXT, LogStatisticsBeforeNewEncoding");
843
0
    LogStatistics (m_pEncContext->iLastStatisticsLogTs, sEncodingParam.iSpatialLayerNum - 1);
844
0
  }
845
0
  break;
846
0
  case ENCODER_OPTION_FRAME_RATE: { // Maximal input frame rate
847
0
    float iValue = * ((float*)pOption);
848
0
    if (iValue <= 0) {
849
0
      return cmInitParaError;
850
0
    }
851
    //adjust to valid range
852
0
    m_pEncContext->pSvcParam->fMaxFrameRate = WELS_CLIP3 (iValue, MIN_FRAME_RATE, MAX_FRAME_RATE);
853
0
    WelsEncoderApplyFrameRate (m_pEncContext->pSvcParam);
854
0
    WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_INFO,
855
0
             "CWelsH264SVCEncoder::SetOption():ENCODER_OPTION_FRAME_RATE,m_pEncContext->pSvcParam->fMaxFrameRate= %f",
856
0
             m_pEncContext->pSvcParam->fMaxFrameRate);
857
0
  }
858
0
  break;
859
0
  case ENCODER_OPTION_BITRATE: { // Target bit-rate
860
0
    SBitrateInfo* pInfo = (static_cast<SBitrateInfo*> (pOption));
861
0
    int32_t iBitrate = pInfo->iBitrate;
862
0
    if (iBitrate <= 0) {
863
0
      WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_ERROR,
864
0
               "CWelsH264SVCEncoder::SetOption():ENCODER_OPTION_BITRATE,iBitrate = %d",
865
0
               iBitrate);
866
0
      return cmInitParaError;
867
0
    }
868
0
    iBitrate = WELS_CLIP3 (iBitrate, MIN_BIT_RATE, MAX_BIT_RATE);
869
0
    switch (pInfo->iLayer) {
870
0
    case SPATIAL_LAYER_ALL:
871
0
      m_pEncContext->pSvcParam->iTargetBitrate = iBitrate;
872
0
      break;
873
0
    case SPATIAL_LAYER_0:
874
0
      m_pEncContext->pSvcParam->sSpatialLayers[0].iSpatialBitrate = iBitrate;
875
0
      break;
876
0
    case SPATIAL_LAYER_1:
877
0
      m_pEncContext->pSvcParam->sSpatialLayers[1].iSpatialBitrate = iBitrate;
878
0
      break;
879
0
    case SPATIAL_LAYER_2:
880
0
      m_pEncContext->pSvcParam->sSpatialLayers[2].iSpatialBitrate = iBitrate;
881
0
      break;
882
0
    case SPATIAL_LAYER_3:
883
0
      m_pEncContext->pSvcParam->sSpatialLayers[3].iSpatialBitrate = iBitrate;
884
0
      break;
885
0
    default:
886
0
      WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_ERROR,
887
0
               "CWelsH264SVCEncoder::SetOption():ENCODER_OPTION_BITRATE,iLayer = %d",
888
0
               pInfo->iLayer);
889
0
      return cmInitParaError;
890
0
      break;
891
0
    }
892
    //adjust to valid range
893
0
    if (WelsEncoderApplyBitRate (&m_pWelsTrace->m_sLogCtx, m_pEncContext->pSvcParam, pInfo->iLayer)) {
894
0
      WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_ERROR,
895
0
               "CWelsH264SVCEncoder::SetOption():ENCODER_OPTION_BITRATE layerId= %d,iSpatialBitrate = %d", pInfo->iLayer, iBitrate);
896
0
      return cmInitParaError;
897
0
    } else {
898
0
      WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_INFO,
899
0
               "CWelsH264SVCEncoder::SetOption():ENCODER_OPTION_BITRATE layerId= %d,iSpatialBitrate = %d", pInfo->iLayer, iBitrate);
900
901
0
    }
902
903
0
  }
904
0
  break;
905
0
  case ENCODER_OPTION_MAX_BITRATE: { // Target bit-rate
906
0
    SBitrateInfo* pInfo = (static_cast<SBitrateInfo*> (pOption));
907
0
    int32_t iBitrate = pInfo->iBitrate;
908
0
    if (iBitrate <= 0) {
909
0
      WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_ERROR,
910
0
               "CWelsH264SVCEncoder::SetOption():ENCODER_OPTION_MAX_BITRATE,iBitrate = %d",
911
0
               iBitrate);
912
0
      return cmInitParaError;
913
0
    }
914
0
    iBitrate = WELS_CLIP3 (iBitrate, MIN_BIT_RATE, MAX_BIT_RATE);
915
0
    switch (pInfo->iLayer) {
916
0
    case SPATIAL_LAYER_ALL:
917
0
      m_pEncContext->pSvcParam->iMaxBitrate = iBitrate;
918
0
      break;
919
0
    case SPATIAL_LAYER_0:
920
0
      m_pEncContext->pSvcParam->sSpatialLayers[0].iMaxSpatialBitrate = iBitrate;
921
0
      break;
922
0
    case SPATIAL_LAYER_1:
923
0
      m_pEncContext->pSvcParam->sSpatialLayers[1].iMaxSpatialBitrate = iBitrate;
924
0
      break;
925
0
    case SPATIAL_LAYER_2:
926
0
      m_pEncContext->pSvcParam->sSpatialLayers[2].iMaxSpatialBitrate = iBitrate;
927
0
      break;
928
0
    case SPATIAL_LAYER_3:
929
0
      m_pEncContext->pSvcParam->sSpatialLayers[3].iMaxSpatialBitrate = iBitrate;
930
0
      break;
931
0
    default:
932
0
      WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_ERROR,
933
0
               "CWelsH264SVCEncoder::SetOption():ENCODER_OPTION_MAX_BITRATE,iLayer = %d",
934
0
               pInfo->iLayer);
935
0
      return cmInitParaError;
936
0
      break;
937
0
    }
938
    //adjust to valid range
939
0
    if (WelsEncoderApplyBitRate (&m_pWelsTrace->m_sLogCtx, m_pEncContext->pSvcParam, pInfo->iLayer)) {
940
0
      WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_ERROR,
941
0
               "CWelsH264SVCEncoder::SetOption():ENCODER_OPTION_BITRATE layerId= %d,iMaxSpatialBitrate = %d", pInfo->iLayer, iBitrate);
942
0
      return cmInitParaError;
943
0
    } else {
944
0
      WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_INFO,
945
0
               "CWelsH264SVCEncoder::SetOption():ENCODER_OPTION_BITRATE layerId= %d,iMaxSpatialBitrate = %d", pInfo->iLayer, iBitrate);
946
947
0
    }
948
0
  }
949
0
  break;
950
0
  case ENCODER_OPTION_RC_MODE: { // 0:quality mode;1:bit-rate mode;2:bitrate limited mode
951
0
    int32_t iValue = * ((int32_t*)pOption);
952
0
    m_pEncContext->pSvcParam->iRCMode = (RC_MODES) iValue;
953
0
    WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_INFO,
954
0
             "CWelsH264SVCEncoder::SetOption():ENCODER_OPTION_RC_MODE iRCMode= %d (Note: not suggest changing RC-mode in middle of encoding)",
955
0
             iValue);
956
0
    WelsRcInitFuncPointers (m_pEncContext, m_pEncContext->pSvcParam->iRCMode);
957
0
  }
958
0
  break;
959
0
  case ENCODER_OPTION_RC_FRAME_SKIP: { // 0:FRAME-SKIP disabled;1:FRAME-SKIP enabled
960
0
    bool bValue = * ((bool*)pOption);
961
0
    if (m_pEncContext->pSvcParam->iRCMode != RC_OFF_MODE) {
962
0
      m_pEncContext->pSvcParam->bEnableFrameSkip = bValue;
963
0
      WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_INFO,
964
0
               "CWelsH264SVCEncoder::SetOption():ENCODER_OPTION_RC_FRAME_SKIP, frame-skip setting(%d)",
965
0
               bValue);
966
0
    } else {
967
0
      WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_INFO,
968
0
               "CWelsH264SVCEncoder::SetOption():ENCODER_OPTION_RC_FRAME_SKIP, rc off, frame-skip setting(%d) un-useful",
969
0
               bValue);
970
0
    }
971
0
  }
972
0
  break;
973
0
  case ENCODER_PADDING_PADDING: { // 0:disable padding;1:padding
974
0
    int32_t iValue = * ((int32_t*)pOption);
975
0
    m_pEncContext->pSvcParam->iPaddingFlag = iValue;
976
0
    WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_INFO,
977
0
             "CWelsH264SVCEncoder::SetOption():ENCODER_PADDING_PADDING iPaddingFlag= %d ",
978
0
             iValue);
979
0
  }
980
0
  break;
981
0
  case ENCODER_LTR_RECOVERY_REQUEST: {
982
0
    SLTRRecoverRequest* pLTR_Recover_Request = (SLTRRecoverRequest*) (pOption);
983
0
    FilterLTRRecoveryRequest (m_pEncContext, pLTR_Recover_Request);
984
0
  }
985
0
  break;
986
0
  case ENCODER_LTR_MARKING_FEEDBACK: {
987
0
    SLTRMarkingFeedback* fb = (SLTRMarkingFeedback*) (pOption);
988
0
    FilterLTRMarkingFeedback (m_pEncContext, fb);
989
0
  }
990
0
  break;
991
0
  case ENCODER_LTR_MARKING_PERIOD: {
992
0
    uint32_t iValue = * ((uint32_t*) (pOption));
993
0
    m_pEncContext->pSvcParam->iLtrMarkPeriod = iValue;
994
0
    WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_INFO,
995
0
             "CWelsH264SVCEncoder::SetOption():ENCODER_LTR_MARKING_PERIOD iLtrMarkPeriod= %d ",
996
0
             iValue);
997
0
  }
998
0
  break;
999
0
  case ENCODER_OPTION_LTR: {
1000
0
    SLTRConfig* pLTRValue = ((SLTRConfig*) (pOption));
1001
0
    if (WelsEncoderApplyLTR (&m_pWelsTrace->m_sLogCtx, &m_pEncContext, pLTRValue)) {
1002
0
      return cmInitParaError;
1003
0
    }
1004
0
    WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_INFO,
1005
0
             "CWelsH264SVCEncoder::SetOption():ENCODER_OPTION_LTR,expected bEnableLongTermReference = %d,expeced iLTRRefNum = %d,actual bEnableLongTermReference = %d,actual iLTRRefNum = %d",
1006
0
             pLTRValue->bEnableLongTermReference, pLTRValue->iLTRRefNum, m_pEncContext->pSvcParam->bEnableLongTermReference,
1007
0
             m_pEncContext->pSvcParam->iLTRRefNum);
1008
0
  }
1009
0
  break;
1010
0
  case ENCODER_OPTION_ENABLE_SSEI: {
1011
0
    bool iValue = * ((bool*)pOption);
1012
0
    m_pEncContext->pSvcParam->bEnableSSEI = iValue;
1013
0
    WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_INFO,
1014
0
             " CWelsH264SVCEncoder::SetOption enable SSEI = %d -- this is not supported yet",
1015
0
             m_pEncContext->pSvcParam->bEnableSSEI);
1016
0
  }
1017
0
  break;
1018
0
  case ENCODER_OPTION_ENABLE_PREFIX_NAL_ADDING: {
1019
0
    bool iValue = * ((bool*)pOption);
1020
0
    m_pEncContext->pSvcParam->bPrefixNalAddingCtrl = iValue;
1021
0
    WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_INFO, " CWelsH264SVCEncoder::SetOption bPrefixNalAddingCtrl = %d ",
1022
0
             m_pEncContext->pSvcParam->bPrefixNalAddingCtrl);
1023
0
  }
1024
0
  break;
1025
0
  case ENCODER_OPTION_SPS_PPS_ID_STRATEGY: {
1026
0
    int32_t iValue = * (static_cast<int32_t*> (pOption));
1027
0
    EParameterSetStrategy eNewStrategy = CONSTANT_ID;
1028
0
    switch (iValue) {
1029
0
    case 0:
1030
0
      eNewStrategy = CONSTANT_ID;
1031
0
      break;
1032
0
    case 0x01:
1033
0
      eNewStrategy = INCREASING_ID;
1034
0
      break;
1035
0
    case 0x02:
1036
0
      eNewStrategy = SPS_LISTING;
1037
0
      break;
1038
0
    case 0x03:
1039
0
      eNewStrategy = SPS_LISTING_AND_PPS_INCREASING;
1040
0
      break;
1041
0
    case 0x06:
1042
0
      eNewStrategy = SPS_PPS_LISTING;
1043
0
      break;
1044
0
    default:
1045
0
      WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_ERROR,
1046
0
               " CWelsH264SVCEncoder::SetOption eSpsPpsIdStrategy(%d) not in valid range, unchanged! existing=%d",
1047
0
               iValue, m_pEncContext->pSvcParam->eSpsPpsIdStrategy);
1048
0
      break;
1049
0
    }
1050
1051
0
    if (((eNewStrategy & SPS_LISTING) || (m_pEncContext->pSvcParam->eSpsPpsIdStrategy & SPS_LISTING))
1052
0
        && m_pEncContext->pSvcParam->eSpsPpsIdStrategy != eNewStrategy) {
1053
0
      WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_ERROR,
1054
0
               " CWelsH264SVCEncoder::SetOption eSpsPpsIdStrategy changing in the middle of call is NOT allowed for eSpsPpsIdStrategy>INCREASING_ID: existing setting is %d and the new one is %d",
1055
0
               m_pEncContext->pSvcParam->eSpsPpsIdStrategy, iValue);
1056
0
      return cmInitParaError;
1057
0
    }
1058
0
    SWelsSvcCodingParam sConfig;
1059
0
    memcpy ((void*) &sConfig, m_pEncContext->pSvcParam, sizeof (SWelsSvcCodingParam));
1060
0
    sConfig.eSpsPpsIdStrategy = eNewStrategy;
1061
0
    WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_INFO, " CWelsH264SVCEncoder::SetOption eSpsPpsIdStrategy = %d ",
1062
0
             sConfig.eSpsPpsIdStrategy);
1063
1064
0
    if (WelsEncoderParamAdjust (&m_pEncContext, &sConfig)) {
1065
0
      return cmInitParaError;
1066
0
    }
1067
0
  }
1068
0
  break;
1069
0
  case ENCODER_OPTION_CURRENT_PATH: {
1070
0
    if (m_pEncContext->pSvcParam != NULL) {
1071
0
      char* path = static_cast<char*> (pOption);
1072
0
      m_pEncContext->pSvcParam->pCurPath = path;
1073
0
    }
1074
0
  }
1075
0
  break;
1076
0
  case ENCODER_OPTION_DUMP_FILE: {
1077
#ifdef ENABLE_FRAME_DUMP
1078
    if (m_pEncContext->pSvcParam != NULL) {
1079
      SDumpLayer* pDump = (static_cast<SDumpLayer*> (pOption));
1080
      WelsStrncpy (m_pEncContext->pSvcParam->sDependencyLayers[pDump->iLayer].sRecFileName,
1081
                   sizeof (m_pEncContext->pSvcParam->sDependencyLayers[pDump->iLayer].sRecFileName), pDump->pFileName);
1082
    }
1083
#endif
1084
0
  }
1085
0
  break;
1086
0
  case ENCODER_OPTION_TRACE_LEVEL: {
1087
0
    if (m_pWelsTrace) {
1088
0
      uint32_t level = * ((uint32_t*)pOption);
1089
0
      m_pWelsTrace->SetTraceLevel (level);
1090
0
    }
1091
0
  }
1092
0
  break;
1093
0
  case ENCODER_OPTION_TRACE_CALLBACK: {
1094
0
    if (m_pWelsTrace) {
1095
0
      WelsTraceCallback callback = * ((WelsTraceCallback*)pOption);
1096
0
      m_pWelsTrace->SetTraceCallback (callback);
1097
0
      WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_INFO,
1098
0
               "CWelsH264SVCEncoder::SetOption():ENCODER_OPTION_TRACE_CALLBACK callback = %p.",
1099
0
               callback);
1100
0
    }
1101
0
  }
1102
0
  break;
1103
0
  case ENCODER_OPTION_TRACE_CALLBACK_CONTEXT: {
1104
0
    if (m_pWelsTrace) {
1105
0
      void* ctx = * ((void**)pOption);
1106
0
      m_pWelsTrace->SetTraceCallbackContext (ctx);
1107
0
    }
1108
0
  }
1109
0
  break;
1110
0
  case ENCODER_OPTION_PROFILE: {
1111
0
    SProfileInfo* pProfileInfo = (static_cast<SProfileInfo*> (pOption));
1112
0
    if ((pProfileInfo->iLayer < SPATIAL_LAYER_0) || (pProfileInfo->iLayer > SPATIAL_LAYER_3)) {
1113
0
      WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_ERROR,
1114
0
               "CWelsH264SVCEncoder::SetOption():ENCODER_OPTION_PROFILE,iLayer = %d(rang0-3)", pProfileInfo->iLayer);
1115
0
      return cmInitParaError;
1116
0
    }
1117
0
    CheckProfileSetting (&m_pWelsTrace->m_sLogCtx, m_pEncContext->pSvcParam, pProfileInfo->iLayer,
1118
0
                         pProfileInfo->uiProfileIdc);
1119
0
    WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_INFO,
1120
0
             "CWelsH264SVCEncoder::SetOption():ENCODER_OPTION_PROFILE,layerId = %d,expected profile = %d,actual profile = %d",
1121
0
             pProfileInfo->iLayer, pProfileInfo->uiProfileIdc,
1122
0
             m_pEncContext->pSvcParam->sSpatialLayers[pProfileInfo->iLayer].uiProfileIdc);
1123
0
  }
1124
0
  break;
1125
0
  case ENCODER_OPTION_LEVEL: {
1126
0
    SLevelInfo* pLevelInfo = (static_cast<SLevelInfo*> (pOption));
1127
0
    if ((pLevelInfo->iLayer < SPATIAL_LAYER_0) || (pLevelInfo->iLayer > SPATIAL_LAYER_3)) {
1128
0
      WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_ERROR,
1129
0
               "CWelsH264SVCEncoder::SetOption():ENCODER_OPTION_PROFILE,iLayer = %d(rang0-3)", pLevelInfo->iLayer);
1130
0
      return cmInitParaError;
1131
0
    }
1132
0
    CheckLevelSetting (&m_pWelsTrace->m_sLogCtx, m_pEncContext->pSvcParam, pLevelInfo->iLayer, pLevelInfo->uiLevelIdc);
1133
0
    WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_INFO,
1134
0
             "CWelsH264SVCEncoder::SetOption():ENCODER_OPTION_LEVEL,layerId = %d,expected level = %d,actual level = %d",
1135
0
             pLevelInfo->iLayer, pLevelInfo->uiLevelIdc, m_pEncContext->pSvcParam->sSpatialLayers[pLevelInfo->iLayer].uiLevelIdc);
1136
0
  }
1137
0
  break;
1138
0
  case ENCODER_OPTION_NUMBER_REF: {
1139
0
    int32_t iValue = * ((int32_t*)pOption);
1140
0
    CheckReferenceNumSetting (&m_pWelsTrace->m_sLogCtx, m_pEncContext->pSvcParam, iValue);
1141
0
    WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_INFO,
1142
0
             "CWelsH264SVCEncoder::SetOption():ENCODER_OPTION_NUMBER_REF,expected refNum = %d,actual refnum = %d", iValue,
1143
0
             m_pEncContext->pSvcParam->iNumRefFrame);
1144
0
  }
1145
0
  break;
1146
0
  case ENCODER_OPTION_DELIVERY_STATUS: {
1147
0
    SDeliveryStatus* pValue = (static_cast<SDeliveryStatus*> (pOption));
1148
0
    m_pEncContext->bDeliveryFlag = pValue->bDeliveryFlag;
1149
0
    WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_DEBUG,
1150
0
             "CWelsH264SVCEncoder::SetOption():ENCODER_OPTION_DELIVERY_STATUS,bDeliveryFlag = %d", pValue->bDeliveryFlag);
1151
0
  }
1152
0
  break;
1153
0
  case ENCODER_OPTION_COMPLEXITY: {
1154
0
    int32_t iValue = * (static_cast<int32_t*> (pOption));
1155
0
    m_pEncContext->pSvcParam->iComplexityMode = (ECOMPLEXITY_MODE)iValue;
1156
0
    WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_INFO,
1157
0
             "CWelsH264SVCEncoder::SetOption():ENCODER_OPTION_COMPLEXITY,iComplexityMode = %d", iValue);
1158
0
  }
1159
0
  break;
1160
0
  case ENCODER_OPTION_GET_STATISTICS: {
1161
0
    WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_WARNING,
1162
0
             "CWelsH264SVCEncoder::SetOption():ENCODER_OPTION_GET_STATISTICS: this option is get-only!");
1163
0
  }
1164
0
  break;
1165
0
  case ENCODER_OPTION_STATISTICS_LOG_INTERVAL: {
1166
0
    int32_t iValue = * (static_cast<int32_t*> (pOption));
1167
0
    m_pEncContext->iStatisticsLogInterval = iValue;
1168
0
    WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_INFO,
1169
0
             "CWelsH264SVCEncoder::SetOption():ENCODER_OPTION_STATISTICS_LOG_INTERVAL,iStatisticsLogInterval = %d", iValue);
1170
0
  }
1171
0
  break;
1172
0
  case ENCODER_OPTION_IS_LOSSLESS_LINK: {
1173
0
    bool bValue = * (static_cast<bool*> (pOption));
1174
0
    m_pEncContext->pSvcParam->bIsLosslessLink = bValue;
1175
0
    WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_INFO,
1176
0
             "CWelsH264SVCEncoder::SetOption():ENCODER_OPTION_IS_LOSSLESS_LINK,bIsLosslessLink = %d", bValue);
1177
0
  }
1178
0
  break;
1179
0
  case ENCODER_OPTION_BITS_VARY_PERCENTAGE: {
1180
0
    int32_t iValue = * (static_cast<int32_t*> (pOption));
1181
0
    m_pEncContext->pSvcParam->iBitsVaryPercentage = WELS_CLIP3 (iValue, 0, 100);
1182
0
    WelsEncoderApplyBitVaryRang (&m_pWelsTrace->m_sLogCtx, m_pEncContext->pSvcParam,
1183
0
                                 m_pEncContext->pSvcParam->iBitsVaryPercentage);
1184
0
    WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_INFO,
1185
0
             "CWelsH264SVCEncoder::SetOption():ENCODER_OPTION_BITS_VARY_PERCENTAGE,iBitsVaryPercentage = %d", iValue);
1186
0
  }
1187
0
  break;
1188
1189
0
  default:
1190
0
    return cmInitParaError;
1191
0
  }
1192
1193
0
  return 0;
1194
0
}
1195
1196
0
int CWelsH264SVCEncoder::GetOption (ENCODER_OPTION eOptionId, void* pOption) {
1197
0
  if (NULL == pOption) {
1198
0
    return cmInitParaError;
1199
0
  }
1200
0
  if (NULL == m_pEncContext || false == m_bInitialFlag) {
1201
0
    return cmInitExpected;
1202
0
  }
1203
1204
0
  switch (eOptionId) {
1205
0
  case ENCODER_OPTION_INTER_SPATIAL_PRED: { // Inter spatial layer prediction flag
1206
0
    WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_INFO,
1207
0
             "ENCODER_OPTION_INTER_SPATIAL_PRED, this feature not supported at present.");
1208
0
  }
1209
0
  break;
1210
0
  case ENCODER_OPTION_DATAFORMAT: { // Input color space
1211
0
    WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_INFO,
1212
0
             "CWelsH264SVCEncoder::GetOption():ENCODER_OPTION_DATAFORMAT, m_iCspInternal= 0x%x", m_iCspInternal);
1213
0
    * ((int32_t*)pOption) = m_iCspInternal;
1214
0
  }
1215
0
  break;
1216
0
  case ENCODER_OPTION_IDR_INTERVAL: { // IDR Interval
1217
0
    WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_INFO,
1218
0
             "CWelsH264SVCEncoder::GetOption():ENCODER_OPTION_IDR_INTERVAL, uiIntraPeriod= %d",
1219
0
             m_pEncContext->pSvcParam->uiIntraPeriod);
1220
0
    * ((int32_t*)pOption) = m_pEncContext->pSvcParam->uiIntraPeriod;
1221
0
  }
1222
0
  break;
1223
0
  case ENCODER_OPTION_SVC_ENCODE_PARAM_EXT: { // SVC Encoding Parameter
1224
0
    WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_INFO,
1225
0
             "CWelsH264SVCEncoder::GetOption():ENCODER_OPTION_SVC_ENCODE_PARAM_EXT");
1226
0
    memcpy (pOption, m_pEncContext->pSvcParam, sizeof (SEncParamExt)); // confirmed_safe_unsafe_usage
1227
0
  }
1228
0
  break;
1229
0
  case ENCODER_OPTION_SVC_ENCODE_PARAM_BASE: { // SVC Encoding Parameter
1230
0
    WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_INFO,
1231
0
             "CWelsH264SVCEncoder::GetOption():ENCODER_OPTION_SVC_ENCODE_PARAM_BASE");
1232
0
    m_pEncContext->pSvcParam->GetBaseParams ((SEncParamBase*) pOption);
1233
0
  }
1234
0
  break;
1235
1236
0
  case ENCODER_OPTION_FRAME_RATE: { // Maximal input frame rate
1237
0
    WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_INFO,
1238
0
             "CWelsH264SVCEncoder::GetOption():ENCODER_OPTION_FRAME_RATE, fMaxFrameRate = %.6ff",
1239
0
             m_pEncContext->pSvcParam->fMaxFrameRate);
1240
0
    * ((float*)pOption) = m_pEncContext->pSvcParam->fMaxFrameRate;
1241
0
  }
1242
0
  break;
1243
0
  case ENCODER_OPTION_BITRATE: { // Target bit-rate
1244
1245
0
    SBitrateInfo* pInfo = (static_cast<SBitrateInfo*> (pOption));
1246
0
    if ((pInfo->iLayer != SPATIAL_LAYER_ALL) && (pInfo->iLayer != SPATIAL_LAYER_0) && (pInfo->iLayer != SPATIAL_LAYER_1)
1247
0
        && (pInfo->iLayer != SPATIAL_LAYER_2) && (pInfo->iLayer != SPATIAL_LAYER_3))
1248
0
      return cmInitParaError;
1249
0
    if (pInfo->iLayer == SPATIAL_LAYER_ALL) {
1250
0
      pInfo->iBitrate = m_pEncContext->pSvcParam->iTargetBitrate;
1251
0
    } else {
1252
0
      pInfo->iBitrate = m_pEncContext->pSvcParam->sSpatialLayers[pInfo->iLayer].iSpatialBitrate;
1253
0
    }
1254
0
    WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_INFO,
1255
0
             "CWelsH264SVCEncoder::GetOption():ENCODER_OPTION_BITRATE, layerId =%d,iBitrate = %d",
1256
0
             pInfo->iLayer, pInfo->iBitrate);
1257
0
  }
1258
0
  break;
1259
0
  case ENCODER_OPTION_MAX_BITRATE: { // Target bit-rate
1260
0
    SBitrateInfo* pInfo = (static_cast<SBitrateInfo*> (pOption));
1261
0
    if ((pInfo->iLayer != SPATIAL_LAYER_ALL) && (pInfo->iLayer != SPATIAL_LAYER_0) && (pInfo->iLayer != SPATIAL_LAYER_1)
1262
0
        && (pInfo->iLayer != SPATIAL_LAYER_2) && (pInfo->iLayer != SPATIAL_LAYER_3))
1263
0
      return cmInitParaError;
1264
0
    if (pInfo->iLayer == SPATIAL_LAYER_ALL) {
1265
0
      pInfo->iBitrate = m_pEncContext->pSvcParam->iMaxBitrate;
1266
0
    } else {
1267
0
      pInfo->iBitrate = m_pEncContext->pSvcParam->sSpatialLayers[pInfo->iLayer].iMaxSpatialBitrate;
1268
0
    }
1269
0
    WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_INFO,
1270
0
             "CWelsH264SVCEncoder::GetOption():ENCODER_OPTION_MAX_BITRATE,, layerId =%d,iBitrate = %d",
1271
0
             pInfo->iLayer, pInfo->iBitrate);
1272
0
  }
1273
0
  break;
1274
0
  case ENCODER_OPTION_GET_STATISTICS: {
1275
0
    SEncoderStatistics* pStatistics = (static_cast<SEncoderStatistics*> (pOption));
1276
0
    SEncoderStatistics* pEncStatistics = &m_pEncContext->sEncoderStatistics[m_pEncContext->pSvcParam->iSpatialLayerNum - 1];
1277
0
    pStatistics->uiWidth = pEncStatistics->uiWidth;
1278
0
    pStatistics->uiHeight = pEncStatistics->uiHeight;
1279
0
    pStatistics->fAverageFrameSpeedInMs = pEncStatistics->fAverageFrameSpeedInMs;
1280
1281
    // rate control related
1282
0
    pStatistics->fAverageFrameRate = pEncStatistics->fAverageFrameRate;
1283
0
    pStatistics->fLatestFrameRate = pEncStatistics->fLatestFrameRate;
1284
0
    pStatistics->uiBitRate = pEncStatistics->uiBitRate;
1285
0
    pStatistics->uiAverageFrameQP = pEncStatistics->uiAverageFrameQP;
1286
1287
0
    pStatistics->uiInputFrameCount = pEncStatistics->uiInputFrameCount;
1288
0
    pStatistics->uiSkippedFrameCount = pEncStatistics->uiSkippedFrameCount;
1289
1290
0
    pStatistics->uiResolutionChangeTimes = pEncStatistics->uiResolutionChangeTimes;
1291
0
    pStatistics->uiIDRReqNum = pEncStatistics->uiIDRReqNum;
1292
0
    pStatistics->uiIDRSentNum = pEncStatistics->uiIDRSentNum;
1293
0
    pStatistics->uiLTRSentNum = pEncStatistics->uiLTRSentNum;
1294
0
  }
1295
0
  break;
1296
0
  case ENCODER_OPTION_STATISTICS_LOG_INTERVAL: {
1297
0
    * ((int32_t*)pOption) = m_pEncContext->iStatisticsLogInterval;
1298
0
  }
1299
0
  break;
1300
0
  case ENCODER_OPTION_COMPLEXITY: {
1301
0
    * ((int32_t*)pOption) =  m_pEncContext->pSvcParam->iComplexityMode;
1302
0
  }
1303
0
  break;
1304
0
  default:
1305
0
    return cmInitParaError;
1306
0
  }
1307
1308
0
  return 0;
1309
0
}
1310
1311
0
void CWelsH264SVCEncoder::DumpSrcPicture (const SSourcePicture*  pSrcPic, const int iUsageType) {
1312
#ifdef DUMP_SRC_PICTURE
1313
  FILE* pFile = NULL;
1314
  char strFileName[256] = {0};
1315
  const int32_t iDataLength = m_iMaxPicWidth * m_iMaxPicHeight;
1316
1317
  WelsSnprintf (strFileName, sizeof (strFileName), "pic_in_%dx%d.yuv", m_iMaxPicWidth,
1318
                m_iMaxPicHeight);// confirmed_safe_unsafe_usage
1319
1320
  switch (pSrcPic->iColorFormat) {
1321
  case videoFormatI420:
1322
  case videoFormatYV12:
1323
    pFile = WelsFopen (strFileName, "ab+");
1324
1325
    if (NULL != pFile) {
1326
      fwrite (pSrcPic->pData[0], sizeof (uint8_t), pSrcPic->iStride[0]*m_iMaxPicHeight, pFile);
1327
      fwrite (pSrcPic->pData[1], sizeof (uint8_t), pSrcPic->iStride[1] * (m_iMaxPicHeight >> 1), pFile);
1328
      fwrite (pSrcPic->pData[2], sizeof (uint8_t), pSrcPic->iStride[2] * (m_iMaxPicHeight >> 1), pFile);
1329
      fflush (pFile);
1330
      fclose (pFile);
1331
    } else {
1332
      WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_INFO, "DumpSrcPicture, strFileName %s open failed!", strFileName);
1333
    }
1334
    break;
1335
  case videoFormatRGB:
1336
    WelsStrcat (strFileName, 256, ".rgb"); // confirmed_safe_unsafe_usage
1337
    pFile = WelsFopen (strFileName, "ab+");
1338
    if (NULL != pFile) {
1339
      fwrite (pSrcPic->pData[0], sizeof (uint8_t), iDataLength * 3, pFile);
1340
      fflush (pFile);
1341
      fclose (pFile);
1342
    }
1343
  case videoFormatBGR:
1344
    WelsStrcat (strFileName, 256, ".bgr"); // confirmed_safe_unsafe_usage
1345
    pFile = WelsFopen (strFileName, "ab+");
1346
    if (NULL != pFile) {
1347
      fwrite (pSrcPic->pData[0], sizeof (uint8_t), iDataLength * 3, pFile);
1348
      fflush (pFile);
1349
      fclose (pFile);
1350
    }
1351
    break;
1352
  case videoFormatYUY2:
1353
    WelsStrcat (strFileName, 256, ".yuy2"); // confirmed_safe_unsafe_usage
1354
    pFile = WelsFopen (strFileName, "ab+");
1355
    if (NULL != pFile) {
1356
      fwrite (pSrcPic->pData[0], sizeof (uint8_t), (CALC_BI_STRIDE (m_iMaxPicWidth,  16)) * m_iMaxPicHeight, pFile);
1357
      fflush (pFile);
1358
      fclose (pFile);
1359
    }
1360
    break;
1361
  default:
1362
    WelsLog (&m_pWelsTrace->m_sLogCtx, WELS_LOG_INFO, "Exclusive case, m_iCspInternal= 0x%x", m_iCspInternal);
1363
    break;
1364
  }
1365
#endif//DUMP_SRC_PICTURE
1366
0
  return;
1367
0
}
1368
}
1369
1370
using namespace WelsEnc;
1371
1372
0
int32_t WelsCreateSVCEncoder (ISVCEncoder** ppEncoder) {
1373
0
  if ((*ppEncoder = new CWelsH264SVCEncoder()) != NULL) {
1374
0
    return 0;
1375
0
  }
1376
1377
0
  return 1;
1378
0
}
1379
1380
0
void WelsDestroySVCEncoder (ISVCEncoder* pEncoder) {
1381
0
  CWelsH264SVCEncoder* pSVCEncoder = (CWelsH264SVCEncoder*)pEncoder;
1382
1383
0
  if (pSVCEncoder) {
1384
0
    delete pSVCEncoder;
1385
0
    pSVCEncoder = NULL;
1386
0
  }
1387
0
}
1388
1389
0
OpenH264Version WelsGetCodecVersion() {
1390
0
  return g_stCodecVersion;
1391
0
}
1392
1393
0
void WelsGetCodecVersionEx (OpenH264Version* pVersion) {
1394
0
  *pVersion = g_stCodecVersion;
1395
0
}
1396
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////