openh264-sys2 0.9.7

Low-level bindings for OpenH264.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
/*!
 * \copy
 *     Copyright (c)  2013, Cisco Systems
 *     All rights reserved.
 *
 *     Redistribution and use in source and binary forms, with or without
 *     modification, are permitted provided that the following conditions
 *     are met:
 *
 *        * Redistributions of source code must retain the above copyright
 *          notice, this list of conditions and the following disclaimer.
 *
 *        * Redistributions in binary form must reproduce the above copyright
 *          notice, this list of conditions and the following disclaimer in
 *          the documentation and/or other materials provided with the
 *          distribution.
 *
 *     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 *     "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 *     LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 *     FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 *     COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 *     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 *     BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 *     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 *     CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 *     LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 *     ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 *     POSSIBILITY OF SUCH DAMAGE.
 *
 */

#include "au_set.h"
#include "encoder_context.h"
#include "paraset_strategy.h"

namespace WelsEnc {


IWelsParametersetStrategy*   IWelsParametersetStrategy::CreateParametersetStrategy (
  EParameterSetStrategy eSpsPpsIdStrategy, bool bSimulcastAVC,
  const int32_t kiSpatialLayerNum) {

  IWelsParametersetStrategy* pParametersetStrategy = NULL;
  switch (eSpsPpsIdStrategy) {
  case INCREASING_ID:
    pParametersetStrategy = WELS_NEW_OP (CWelsParametersetIdIncreasing (bSimulcastAVC, kiSpatialLayerNum),
                                         CWelsParametersetIdIncreasing);
    WELS_VERIFY_RETURN_IF (NULL, NULL == pParametersetStrategy)
    break;
  case SPS_LISTING:
    pParametersetStrategy = WELS_NEW_OP (CWelsParametersetSpsListing (bSimulcastAVC, kiSpatialLayerNum),
                                         CWelsParametersetSpsListing);
    WELS_VERIFY_RETURN_IF (NULL, NULL == pParametersetStrategy)
    break;
  case SPS_LISTING_AND_PPS_INCREASING:
    pParametersetStrategy = WELS_NEW_OP (CWelsParametersetSpsListingPpsIncreasing (bSimulcastAVC, kiSpatialLayerNum),
                                         CWelsParametersetSpsListingPpsIncreasing);
    WELS_VERIFY_RETURN_IF (NULL, NULL == pParametersetStrategy)
    break;
  case SPS_PPS_LISTING:
    pParametersetStrategy = WELS_NEW_OP (CWelsParametersetSpsPpsListing (bSimulcastAVC, kiSpatialLayerNum),
                                         CWelsParametersetSpsPpsListing);
    WELS_VERIFY_RETURN_IF (NULL, NULL == pParametersetStrategy)
    break;
  case CONSTANT_ID:
  default:
    pParametersetStrategy = WELS_NEW_OP (CWelsParametersetIdConstant (bSimulcastAVC, kiSpatialLayerNum),
                                         CWelsParametersetIdConstant);
    WELS_VERIFY_RETURN_IF (NULL, NULL == pParametersetStrategy)
    break;
  }

  return pParametersetStrategy;
}


static int32_t WelsGenerateNewSps (sWelsEncCtx* pCtx, const bool kbUseSubsetSps, const int32_t iDlayerIndex,
                                   const int32_t iDlayerCount, const int32_t kiSpsId,
                                   SWelsSPS*& pSps, SSubsetSps*& pSubsetSps, bool bSVCBaselayer) {
  int32_t iRet = 0;

  if (!kbUseSubsetSps) {
    pSps        = & (pCtx->pSpsArray[kiSpsId]);
  } else {
    pSubsetSps  = & (pCtx->pSubsetArray[kiSpsId]);
    pSps        = &pSubsetSps->pSps;
  }

  SWelsSvcCodingParam* pParam = pCtx->pSvcParam;
  SSpatialLayerConfig* pDlayerParam = &pParam->sSpatialLayers[iDlayerIndex];
  // Need port pSps/pPps initialization due to spatial scalability changed
  if (!kbUseSubsetSps) {
    iRet = WelsInitSps (pSps, pDlayerParam, &pParam->sDependencyLayers[iDlayerIndex], pParam->uiIntraPeriod,
                        pParam->iMaxNumRefFrame,
                        kiSpsId, pParam->bEnableFrameCroppingFlag, pParam->iRCMode != RC_OFF_MODE, iDlayerCount,
                        bSVCBaselayer);
  } else {
    iRet = WelsInitSubsetSps (pSubsetSps, pDlayerParam, &pParam->sDependencyLayers[iDlayerIndex], pParam->uiIntraPeriod,
                              pParam->iMaxNumRefFrame,
                              kiSpsId, pParam->bEnableFrameCroppingFlag, pParam->iRCMode != RC_OFF_MODE, iDlayerCount);
  }
  return iRet;
}

static bool CheckMatchedSps (SWelsSPS* const pSps1, SWelsSPS* const pSps2) {

  if ((pSps1->iMbWidth != pSps2->iMbWidth)
      || (pSps1->iMbHeight != pSps2->iMbHeight)) {
    return false;
  }

  if ((pSps1->uiLog2MaxFrameNum != pSps2->uiLog2MaxFrameNum)
      || (pSps1->iLog2MaxPocLsb != pSps2->iLog2MaxPocLsb)) {
    return false;
  }

  if (pSps1->iNumRefFrames != pSps2->iNumRefFrames) {
    return false;
  }

  if ((pSps1->bFrameCroppingFlag != pSps2->bFrameCroppingFlag)
      || (pSps1->sFrameCrop.iCropLeft != pSps2->sFrameCrop.iCropLeft)
      || (pSps1->sFrameCrop.iCropRight != pSps2->sFrameCrop.iCropRight)
      || (pSps1->sFrameCrop.iCropTop != pSps2->sFrameCrop.iCropTop)
      || (pSps1->sFrameCrop.iCropBottom != pSps2->sFrameCrop.iCropBottom)
     ) {
    return false;
  }

  if ((pSps1->uiProfileIdc != pSps2->uiProfileIdc)
      || (pSps1->bConstraintSet0Flag != pSps2->bConstraintSet0Flag)
      || (pSps1->bConstraintSet1Flag != pSps2->bConstraintSet1Flag)
      || (pSps1->bConstraintSet2Flag != pSps2->bConstraintSet2Flag)
      || (pSps1->bConstraintSet3Flag != pSps2->bConstraintSet3Flag)
      || (pSps1->iLevelIdc != pSps2->iLevelIdc)) {
    return false;
  }

  return true;
}

static bool CheckMatchedSubsetSps (SSubsetSps* const pSubsetSps1, SSubsetSps* const pSubsetSps2) {
  if (!CheckMatchedSps (&pSubsetSps1->pSps, &pSubsetSps2->pSps)) {
    return false;
  }

  if ((pSubsetSps1->sSpsSvcExt.iExtendedSpatialScalability      != pSubsetSps2->sSpsSvcExt.iExtendedSpatialScalability)
      || (pSubsetSps1->sSpsSvcExt.bAdaptiveTcoeffLevelPredFlag  != pSubsetSps2->sSpsSvcExt.bAdaptiveTcoeffLevelPredFlag)
      || (pSubsetSps1->sSpsSvcExt.bSeqTcoeffLevelPredFlag != pSubsetSps2->sSpsSvcExt.bSeqTcoeffLevelPredFlag)
      || (pSubsetSps1->sSpsSvcExt.bSliceHeaderRestrictionFlag != pSubsetSps2->sSpsSvcExt.bSliceHeaderRestrictionFlag)) {
    return false;
  }

  return true;
}

/*!
 * \brief   check if the current parameter can found a presenting sps
 * \param   pParam          the current encoding paramter in SWelsSvcCodingParam
 * \param   kbUseSubsetSps  bool
 * \param   iDlayerIndex    int, the index of current D layer
 * \param   iDlayerCount    int, the number of total D layer
 * \param pSpsArray         array of all the stored SPSs
 * \param   pSubsetArray    array of all the stored Subset-SPSs
 * \return  0 - successful
 *         -1 - cannot find existing SPS for current encoder parameter
 */
int32_t FindExistingSps (SWelsSvcCodingParam* pParam, const bool kbUseSubsetSps, const int32_t iDlayerIndex,
                         const int32_t iDlayerCount, const int32_t iSpsNumInUse,
                         SWelsSPS* pSpsArray,
                         SSubsetSps* pSubsetArray, bool bSVCBaseLayer) {
  SSpatialLayerConfig* pDlayerParam = &pParam->sSpatialLayers[iDlayerIndex];

  assert (iSpsNumInUse <= MAX_SPS_COUNT);
  if (!kbUseSubsetSps) {
    SWelsSPS sTmpSps;
    WelsInitSps (&sTmpSps, pDlayerParam, &pParam->sDependencyLayers[iDlayerIndex], pParam->uiIntraPeriod,
                 pParam->iMaxNumRefFrame,
                 0, pParam->bEnableFrameCroppingFlag, pParam->iRCMode != RC_OFF_MODE, iDlayerCount,
                 bSVCBaseLayer);
    for (int32_t iId = 0; iId < iSpsNumInUse; iId++) {
      if (CheckMatchedSps (&sTmpSps, &pSpsArray[iId])) {
        return iId;
      }
    }
  } else {
    SSubsetSps sTmpSubsetSps;
    WelsInitSubsetSps (&sTmpSubsetSps, pDlayerParam, &pParam->sDependencyLayers[iDlayerIndex], pParam->uiIntraPeriod,
                       pParam->iMaxNumRefFrame,
                       0, pParam->bEnableFrameCroppingFlag, pParam->iRCMode != RC_OFF_MODE, iDlayerCount);

    for (int32_t iId = 0; iId < iSpsNumInUse; iId++) {
      if (CheckMatchedSubsetSps (&sTmpSubsetSps, &pSubsetArray[iId])) {
        return iId;
      }
    }
  }

  return INVALID_ID;
}

CWelsParametersetIdConstant::CWelsParametersetIdConstant (const bool bSimulcastAVC, const int32_t kiSpatialLayerNum) {
  memset (&m_sParaSetOffset, 0, sizeof (m_sParaSetOffset));

  m_bSimulcastAVC = bSimulcastAVC;
  m_iSpatialLayerNum = kiSpatialLayerNum;

  m_iBasicNeededSpsNum = 1;
  m_iBasicNeededPpsNum = (1 + m_iSpatialLayerNum);
}

CWelsParametersetIdConstant::~CWelsParametersetIdConstant() {
}

int32_t CWelsParametersetIdConstant::GetPpsIdOffset (const int32_t iPpsId) {
  return 0;
};
int32_t CWelsParametersetIdConstant::GetSpsIdOffset (const int32_t iPpsId, const int32_t iSpsId) {
  return 0;
};

int32_t* CWelsParametersetIdConstant::GetSpsIdOffsetList (const int iParasetType) {
  return & (m_sParaSetOffset.sParaSetOffsetVariable[iParasetType].iParaSetIdDelta[0]);
}

uint32_t CWelsParametersetIdConstant::GetAllNeededParasetNum() {
  return (GetNeededSpsNum()
          + GetNeededSubsetSpsNum()
          + GetNeededPpsNum());
}

uint32_t CWelsParametersetIdConstant::GetNeededSpsNum() {
  if (0 >= m_sParaSetOffset.uiNeededSpsNum) {
    m_sParaSetOffset.uiNeededSpsNum = m_iBasicNeededSpsNum * ((m_bSimulcastAVC) ? (m_iSpatialLayerNum) : (1));
  }
  return m_sParaSetOffset.uiNeededSpsNum;
}


uint32_t CWelsParametersetIdConstant::GetNeededSubsetSpsNum() {
  if (0 >= m_sParaSetOffset.uiNeededSubsetSpsNum) {
    m_sParaSetOffset.uiNeededSubsetSpsNum = (m_bSimulcastAVC ? 0 : (m_iSpatialLayerNum - 1));
  }
  return m_sParaSetOffset.uiNeededSubsetSpsNum;
}

uint32_t CWelsParametersetIdConstant::GetNeededPpsNum() {
  if (0 == m_sParaSetOffset.uiNeededPpsNum) {
    m_sParaSetOffset.uiNeededPpsNum = m_iBasicNeededPpsNum * ((m_bSimulcastAVC) ? (m_iSpatialLayerNum) :
                                      (1));
  }
  return m_sParaSetOffset.uiNeededPpsNum;
}

void CWelsParametersetIdConstant::LoadPrevious (SExistingParasetList* pExistingParasetList,  SWelsSPS* pSpsArray,
    SSubsetSps*       pSubsetArray, SWelsPPS* pPpsArray) {
  return;
}

void CWelsParametersetIdConstant::Update (const uint32_t kuiId, const int iParasetType) {
  memset (&m_sParaSetOffset, 0, sizeof (SParaSetOffset));
}

uint32_t CWelsParametersetIdConstant::GenerateNewSps (sWelsEncCtx* pCtx, const bool kbUseSubsetSps,
    const int32_t iDlayerIndex,
    const int32_t iDlayerCount, uint32_t kuiSpsId,
    SWelsSPS*& pSps, SSubsetSps*& pSubsetSps, bool bSVCBaselayer) {
  WelsGenerateNewSps (pCtx, kbUseSubsetSps, iDlayerIndex,
                      iDlayerCount, kuiSpsId,
                      pSps, pSubsetSps, bSVCBaselayer);
  return kuiSpsId;
}


uint32_t CWelsParametersetIdConstant::InitPps (sWelsEncCtx* pCtx, uint32_t kiSpsId,
    SWelsSPS* pSps,
    SSubsetSps* pSubsetSps,
    uint32_t kuiPpsId,
    const bool kbDeblockingFilterPresentFlag,
    const bool kbUsingSubsetSps,
    const bool kbEntropyCodingModeFlag) {
  WelsInitPps (& pCtx->pPPSArray[kuiPpsId], pSps, pSubsetSps, kuiPpsId, true, kbUsingSubsetSps, kbEntropyCodingModeFlag);
  SetUseSubsetFlag (kuiPpsId, kbUsingSubsetSps);
  return kuiPpsId;
}

void CWelsParametersetIdConstant::SetUseSubsetFlag (const uint32_t iPpsId, const bool bUseSubsetSps) {
  m_sParaSetOffset.bPpsIdMappingIntoSubsetsps[iPpsId] = bUseSubsetSps;
}

void CWelsParametersetIdNonConstant::OutputCurrentStructure (SParaSetOffsetVariable* pParaSetOffsetVariable,
    int32_t* pPpsIdList, sWelsEncCtx* pCtx, SExistingParasetList* pExistingParasetList) {
  for (int32_t k = 0; k < PARA_SET_TYPE; k++) {
    memset ((m_sParaSetOffset.sParaSetOffsetVariable[k].bUsedParaSetIdInBs), 0, MAX_PPS_COUNT * sizeof (bool));
  }
  memcpy (pParaSetOffsetVariable, m_sParaSetOffset.sParaSetOffsetVariable,
          (PARA_SET_TYPE)*sizeof (SParaSetOffsetVariable)); // confirmed_safe_unsafe_usage
}
void CWelsParametersetIdNonConstant::LoadPreviousStructure (SParaSetOffsetVariable* pParaSetOffsetVariable,
    int32_t* pPpsIdList) {
  memcpy (m_sParaSetOffset.sParaSetOffsetVariable, pParaSetOffsetVariable,
          (PARA_SET_TYPE)*sizeof (SParaSetOffsetVariable)); // confirmed_safe_unsafe_usage
}

//
//CWelsParametersetIdIncreasing
//

void CWelsParametersetIdIncreasing::DebugSpsPps (const int32_t kiPpsId, const int32_t kiSpsId) {
#if _DEBUG
  //SParaSetOffset use, 110421
  //if ( (INCREASING_ID & eSpsPpsIdStrategy)) {
  const int32_t kiParameterSetType = (m_sParaSetOffset.bPpsIdMappingIntoSubsetsps[kiPpsId] ?
                                      PARA_SET_TYPE_SUBSETSPS : PARA_SET_TYPE_AVCSPS) ;

  const int32_t kiTmpSpsIdInBs = kiSpsId +
                                 m_sParaSetOffset.sParaSetOffsetVariable[kiParameterSetType].iParaSetIdDelta[kiSpsId];
  const int32_t tmp_pps_id_in_bs = kiPpsId +
                                   m_sParaSetOffset.sParaSetOffsetVariable[PARA_SET_TYPE_PPS].iParaSetIdDelta[kiPpsId];
  assert (MAX_SPS_COUNT > kiTmpSpsIdInBs);
  assert (MAX_PPS_COUNT > tmp_pps_id_in_bs);
  assert (m_sParaSetOffset.sParaSetOffsetVariable[kiParameterSetType].bUsedParaSetIdInBs[kiTmpSpsIdInBs]);
  //}
#endif
}
void CWelsParametersetIdIncreasing::DebugPps (const int32_t kiPpsId) {
#if _DEBUG
  const int32_t kiTmpPpsIdInBs = kiPpsId +
                                 m_sParaSetOffset.sParaSetOffsetVariable[PARA_SET_TYPE_PPS].iParaSetIdDelta[ kiPpsId ];
  assert (MAX_PPS_COUNT > kiTmpPpsIdInBs);

  //when activated need to sure there is avialable PPS
  assert (m_sParaSetOffset.sParaSetOffsetVariable[PARA_SET_TYPE_PPS].bUsedParaSetIdInBs[kiTmpPpsIdInBs]);
#endif
}

void ParasetIdAdditionIdAdjust (SParaSetOffsetVariable* sParaSetOffsetVariable,
                                const int32_t kiCurEncoderParaSetId,
                                const uint32_t kuiMaxIdInBs) { //paraset_type = 0: SPS; =1: PPS
  //SPS_ID in avc_sps and pSubsetSps will be different using this
  //SPS_ID case example:
  //1st enter:  next_spsid_in_bs == 0; spsid == 0; delta==0;            //actual spsid_in_bs == 0
  //1st finish: next_spsid_in_bs == 1;
  //2nd enter:  next_spsid_in_bs == 1; spsid == 0; delta==1;            //actual spsid_in_bs == 1
  //2nd finish: next_spsid_in_bs == 2;
  //31st enter: next_spsid_in_bs == 31; spsid == 0~2; delta==31~29;     //actual spsid_in_bs == 31
  //31st finish:next_spsid_in_bs == 0;
  //31st enter: next_spsid_in_bs == 0; spsid == 0~2; delta==-2~0;       //actual spsid_in_bs == 0
  //31st finish:next_spsid_in_bs == 1;

  const int32_t kiEncId = kiCurEncoderParaSetId;
  uint32_t uiNextIdInBs = sParaSetOffsetVariable->uiNextParaSetIdToUseInBs;

  //update current layer's pCodingParam
  sParaSetOffsetVariable->iParaSetIdDelta[kiEncId] = uiNextIdInBs -
      kiEncId;  //for current parameter set, change its id_delta
  //write pso pData for next update:
  sParaSetOffsetVariable->bUsedParaSetIdInBs[uiNextIdInBs] = true; //   update current used_id

  //prepare for next update:
  //   find the next avaibable iId
  ++uiNextIdInBs;
  if (uiNextIdInBs >= kuiMaxIdInBs) {
    uiNextIdInBs = 0;//ensure the SPS_ID wound not exceed MAX_SPS_COUNT
  }
  //   update next_id
  sParaSetOffsetVariable->uiNextParaSetIdToUseInBs = uiNextIdInBs;
}

void CWelsParametersetIdIncreasing::Update (const uint32_t kuiId, const int iParasetType) {
#if _DEBUG
  m_sParaSetOffset.eSpsPpsIdStrategy = INCREASING_ID;
  assert (kuiId < MAX_DQ_LAYER_NUM);
#endif

  ParasetIdAdditionIdAdjust (& (m_sParaSetOffset.sParaSetOffsetVariable[iParasetType]),
                             kuiId,
                             (iParasetType != PARA_SET_TYPE_PPS) ? MAX_SPS_COUNT : MAX_PPS_COUNT);
}
//((SPS_PPS_LISTING != pEncCtx->pSvcParam->eSpsPpsIdStrategy) ? (&
//  (pEncCtx->sPSOVector.sParaSetOffsetVariable[PARA_SET_TYPE_PPS].iParaSetIdDelta[0])) : NULL)

int32_t CWelsParametersetIdIncreasing::GetPpsIdOffset (const int32_t kiPpsId) {
#if _DEBUG
  DebugPps (kiPpsId);
#endif
  return (m_sParaSetOffset.sParaSetOffsetVariable[PARA_SET_TYPE_PPS].iParaSetIdDelta[kiPpsId]);
}

int32_t CWelsParametersetIdIncreasing::GetSpsIdOffset (const int32_t kiPpsId, const int32_t kiSpsId) {
  const int32_t kiParameterSetType = (m_sParaSetOffset.bPpsIdMappingIntoSubsetsps[kiPpsId] ?
                                      PARA_SET_TYPE_SUBSETSPS : PARA_SET_TYPE_AVCSPS);
#if _DEBUG
  DebugSpsPps (kiPpsId, kiSpsId);
#endif
  return (m_sParaSetOffset.sParaSetOffsetVariable[kiParameterSetType].iParaSetIdDelta[kiSpsId]);
}

//
//CWelsParametersetSpsListing
//

CWelsParametersetSpsListing::CWelsParametersetSpsListing (const bool bSimulcastAVC,
    const int32_t kiSpatialLayerNum) : CWelsParametersetIdNonConstant (bSimulcastAVC, kiSpatialLayerNum) {
  memset (&m_sParaSetOffset, 0, sizeof (m_sParaSetOffset));

  m_bSimulcastAVC = bSimulcastAVC;
  m_iSpatialLayerNum = kiSpatialLayerNum;

  m_iBasicNeededSpsNum = MAX_SPS_COUNT;
  m_iBasicNeededPpsNum = 1;
}

uint32_t CWelsParametersetSpsListing::GetNeededSubsetSpsNum() {
  if (0 >= m_sParaSetOffset.uiNeededSubsetSpsNum) {
    //      sPSOVector.uiNeededSubsetSpsNum = ((pSvcParam->bSimulcastAVC) ? (0) :((SPS_LISTING & pSvcParam->eSpsPpsIdStrategy) ? (MAX_SPS_COUNT) : (pSvcParam->iSpatialLayerNum - 1)));
    m_sParaSetOffset.uiNeededSubsetSpsNum = ((m_bSimulcastAVC) ? (0) :
                                            (MAX_SPS_COUNT));
  }
  return m_sParaSetOffset.uiNeededSubsetSpsNum;
}

void CWelsParametersetSpsListing::LoadPreviousSps (SExistingParasetList* pExistingParasetList, SWelsSPS* pSpsArray,
    SSubsetSps*       pSubsetArray) {
  //if ((SPS_LISTING & pParam->eSpsPpsIdStrategy) && (NULL != pExistingParasetList)) {
  m_sParaSetOffset.uiInUseSpsNum = pExistingParasetList->uiInUseSpsNum;
  memcpy (pSpsArray, pExistingParasetList->sSps, MAX_SPS_COUNT * sizeof (SWelsSPS));

  if (GetNeededSubsetSpsNum() > 0) {
    m_sParaSetOffset.uiInUseSubsetSpsNum = pExistingParasetList->uiInUseSubsetSpsNum;
    memcpy (pSubsetArray, pExistingParasetList->sSubsetSps, MAX_SPS_COUNT * sizeof (SSubsetSps));
  } else {
    m_sParaSetOffset.uiInUseSubsetSpsNum = 0;
  }
  //}

}
void CWelsParametersetSpsListing::LoadPrevious (SExistingParasetList* pExistingParasetList,  SWelsSPS* pSpsArray,
    SSubsetSps*       pSubsetArray, SWelsPPS* pPpsArray) {
  if (NULL == pExistingParasetList) {
    return;
  }
  LoadPreviousSps (pExistingParasetList, pSpsArray, pSubsetArray);
  LoadPreviousPps (pExistingParasetList, pPpsArray);
}

bool CWelsParametersetSpsListing::CheckParamCompatibility (SWelsSvcCodingParam* pCodingParam, SLogContext* pLogCtx) {
  if (pCodingParam->iSpatialLayerNum > 1 && (!pCodingParam->bSimulcastAVC)) {
    WelsLog (pLogCtx, WELS_LOG_WARNING,
             "ParamValidationExt(), eSpsPpsIdStrategy setting (%d) with multiple svc SpatialLayers (%d) not supported! eSpsPpsIdStrategy adjusted to CONSTANT_ID",
             pCodingParam->eSpsPpsIdStrategy, pCodingParam->iSpatialLayerNum);
    pCodingParam->eSpsPpsIdStrategy = CONSTANT_ID;
    return false;
  }
  return true;
}

bool CWelsParametersetSpsListing::CheckPpsGenerating() {
  return true;
}
int32_t CWelsParametersetSpsListing::SpsReset (sWelsEncCtx* pCtx, bool kbUseSubsetSps) {

  // reset current list
  if (!kbUseSubsetSps) {
    m_sParaSetOffset.uiInUseSpsNum = 1;
    memset (pCtx->pSpsArray, 0, MAX_SPS_COUNT * sizeof (SWelsSPS));
  } else {
    m_sParaSetOffset.uiInUseSubsetSpsNum = 1;
    memset (pCtx->pSubsetArray, 0, MAX_SPS_COUNT * sizeof (SSubsetSps));
  }

  //iSpsId = 0;
  return 0;
}
uint32_t CWelsParametersetSpsListing::GenerateNewSps (sWelsEncCtx* pCtx, const bool kbUseSubsetSps,
    const int32_t iDlayerIndex,
    const int32_t iDlayerCount, uint32_t kuiSpsId,
    SWelsSPS*& pSps, SSubsetSps*& pSubsetSps, bool bSvcBaselayer) {
  //check if the current param can fit in an existing SPS
  const int32_t kiFoundSpsId = FindExistingSps (pCtx->pSvcParam, kbUseSubsetSps, iDlayerIndex, iDlayerCount,
                               kbUseSubsetSps ? (m_sParaSetOffset.uiInUseSubsetSpsNum) : (m_sParaSetOffset.uiInUseSpsNum),
                               pCtx->pSpsArray,
                               pCtx->pSubsetArray, bSvcBaselayer);


  if (INVALID_ID != kiFoundSpsId) {
    //if yes, set pSps or pSubsetSps to it
    kuiSpsId = kiFoundSpsId;
    if (!kbUseSubsetSps) {
      pSps = & (pCtx->pSpsArray[kiFoundSpsId]);
    } else {
      pSubsetSps = & (pCtx->pSubsetArray[kiFoundSpsId]);
    }
  } else {
    //if no, generate a new SPS as usual
    if (!CheckPpsGenerating()) {
      return -1;
    }

    kuiSpsId = (!kbUseSubsetSps) ? (m_sParaSetOffset.uiInUseSpsNum++) : (m_sParaSetOffset.uiInUseSubsetSpsNum++);
    if (kuiSpsId >= MAX_SPS_COUNT) {
      if (SpsReset (pCtx, kbUseSubsetSps) < 0) {
        return -1;
      }
      kuiSpsId = 0;
    }

    WelsGenerateNewSps (pCtx, kbUseSubsetSps, iDlayerIndex,
                        iDlayerCount, kuiSpsId, pSps, pSubsetSps, bSvcBaselayer);
  }
  return kuiSpsId;
}

void CWelsParametersetSpsListing::UpdateParaSetNum (sWelsEncCtx* pCtx) {
  pCtx->iSpsNum = m_sParaSetOffset.uiInUseSpsNum;
  pCtx->iSubsetSpsNum = m_sParaSetOffset.uiInUseSubsetSpsNum;
};

void CWelsParametersetSpsListing::OutputCurrentStructure (SParaSetOffsetVariable* pParaSetOffsetVariable,
    int32_t* pPpsIdList, sWelsEncCtx* pCtx, SExistingParasetList* pExistingParasetList) {
  CWelsParametersetIdNonConstant::OutputCurrentStructure (pParaSetOffsetVariable, pPpsIdList, pCtx, pExistingParasetList);
  pExistingParasetList->uiInUseSpsNum = m_sParaSetOffset.uiInUseSpsNum;

  memcpy (pExistingParasetList->sSps, pCtx->pSpsArray, MAX_SPS_COUNT * sizeof (SWelsSPS));
  if (NULL != pCtx->pSubsetArray) {
    pExistingParasetList->uiInUseSubsetSpsNum = m_sParaSetOffset.uiInUseSubsetSpsNum;
    memcpy (pExistingParasetList->sSubsetSps, pCtx->pSubsetArray, MAX_SPS_COUNT * sizeof (SSubsetSps));
  } else {
    pExistingParasetList->uiInUseSubsetSpsNum = 0;
  }
}

//
//CWelsParametersetSpsPpsListing
//

CWelsParametersetSpsPpsListing::CWelsParametersetSpsPpsListing (const bool bSimulcastAVC,
    const int32_t kiSpatialLayerNum): CWelsParametersetSpsListing (bSimulcastAVC, kiSpatialLayerNum) {
  memset (&m_sParaSetOffset, 0, sizeof (m_sParaSetOffset));

  m_bSimulcastAVC = bSimulcastAVC;
  m_iSpatialLayerNum = kiSpatialLayerNum;

  m_iBasicNeededSpsNum = MAX_SPS_COUNT;
  m_iBasicNeededPpsNum = MAX_PPS_COUNT;
}

void CWelsParametersetSpsPpsListing::LoadPreviousPps (SExistingParasetList* pExistingParasetList, SWelsPPS* pPpsArray) {
  // copy from existing if the pointer exists
  //if ((SPS_PPS_LISTING == pParam->eSpsPpsIdStrategy) && (NULL != pExistingParasetList)) {
  m_sParaSetOffset.uiInUsePpsNum = pExistingParasetList->uiInUsePpsNum;
  memcpy (pPpsArray, pExistingParasetList->sPps, MAX_PPS_COUNT * sizeof (SWelsPPS));
  //}
}

/*  if ((SPS_PPS_LISTING == pCtx->pSvcParam->eSpsPpsIdStrategy) && (pCtx->iPpsNum < MAX_PPS_COUNT)) {
 UpdatePpsList (pCtx);
 }*/
void CWelsParametersetSpsPpsListing::UpdatePpsList (sWelsEncCtx* pCtx) {
  if (pCtx->iPpsNum >= MAX_PPS_COUNT) {
    return;
  }
  assert (pCtx->iPpsNum <= MAX_DQ_LAYER_NUM);

  //Generate PPS LIST
  int32_t iPpsId = 0, iUsePpsNum = pCtx->iPpsNum;

  for (int32_t iIdrRound = 0; iIdrRound < MAX_PPS_COUNT; iIdrRound++) {
    for (iPpsId = 0; iPpsId < pCtx->iPpsNum; iPpsId++) {
      m_sParaSetOffset.iPpsIdList[iPpsId][iIdrRound] = ((iIdrRound * iUsePpsNum + iPpsId) % MAX_PPS_COUNT);
    }
  }

  for (iPpsId = iUsePpsNum; iPpsId < MAX_PPS_COUNT; iPpsId++) {
    memcpy (& (pCtx->pPPSArray[iPpsId]), & (pCtx->pPPSArray[iPpsId % iUsePpsNum]), sizeof (SWelsPPS));
    pCtx->pPPSArray[iPpsId].iPpsId = iPpsId;
    pCtx->iPpsNum++;
  }

  assert (pCtx->iPpsNum == MAX_PPS_COUNT);
  m_sParaSetOffset.uiInUsePpsNum = pCtx->iPpsNum;
}


bool CWelsParametersetSpsPpsListing::CheckPpsGenerating() {
  /*if ((SPS_PPS_LISTING == pCtx->pSvcParam->eSpsPpsIdStrategy) && (MAX_PPS_COUNT <= pCtx->sPSOVector.uiInUsePpsNum)) {
    //check if we can generate new SPS or not
    WelsLog (& pCtx->sLogCtx, WELS_LOG_ERROR,
             "InitDqLayers(), cannot generate new SPS under the SPS_PPS_LISTING mode!");
    return ENC_RETURN_UNSUPPORTED_PARA;
  }*/
  if (MAX_PPS_COUNT <= m_sParaSetOffset.uiInUsePpsNum) {
    return false;
  }

  return true;
}
int32_t CWelsParametersetSpsPpsListing::SpsReset (sWelsEncCtx* pCtx, bool kbUseSubsetSps) {
  /*        if (SPS_PPS_LISTING == pParam->eSpsPpsIdStrategy) {
   WelsLog (& (*ppCtx)->sLogCtx, WELS_LOG_ERROR,
   "InitDqLayers(), cannot generate new SPS under the SPS_PPS_LISTING mode!");
   return ENC_RETURN_UNSUPPORTED_PARA;
   }*/
  return -1;
}

int32_t FindExistingPps (SWelsSPS* pSps, SSubsetSps* pSubsetSps, const bool kbUseSubsetSps, const int32_t iSpsId,
                         const bool kbEntropyCodingFlag, const int32_t iPpsNumInUse,
                         SWelsPPS* pPpsArray) {
#if !defined(DISABLE_FMO_FEATURE)
  // feature not supported yet
  return INVALID_ID;
#endif//!DISABLE_FMO_FEATURE

  SWelsPPS sTmpPps;
  WelsInitPps (&sTmpPps,
               pSps,
               pSubsetSps,
               0,
               true,
               kbUseSubsetSps,
               kbEntropyCodingFlag);

  assert (iPpsNumInUse <= MAX_PPS_COUNT);
  for (int32_t iId = 0; iId < iPpsNumInUse; iId++) {
    if ((sTmpPps.iSpsId == pPpsArray[iId].iSpsId)
        && (sTmpPps.bEntropyCodingModeFlag == pPpsArray[iId].bEntropyCodingModeFlag)
        && (sTmpPps.iPicInitQp == pPpsArray[iId].iPicInitQp)
        && (sTmpPps.iPicInitQs == pPpsArray[iId].iPicInitQs)
        && (sTmpPps.uiChromaQpIndexOffset == pPpsArray[iId].uiChromaQpIndexOffset)
        && (sTmpPps.bDeblockingFilterControlPresentFlag == pPpsArray[iId].bDeblockingFilterControlPresentFlag)
       ) {
      return iId;
    }
  }

  return INVALID_ID;
}

uint32_t CWelsParametersetSpsPpsListing::InitPps (sWelsEncCtx* pCtx, uint32_t kiSpsId,
    SWelsSPS* pSps,
    SSubsetSps* pSubsetSps,
    uint32_t kuiPpsId,
    const bool kbDeblockingFilterPresentFlag,
    const bool kbUsingSubsetSps,
    const bool kbEntropyCodingModeFlag) {
  const int32_t kiFoundPpsId = FindExistingPps (pSps, pSubsetSps, kbUsingSubsetSps, kiSpsId,
                               kbEntropyCodingModeFlag,
                               m_sParaSetOffset.uiInUsePpsNum,
                               pCtx->pPPSArray);


  if (INVALID_ID != kiFoundPpsId) {
    //if yes, set pPps to it
    kuiPpsId = kiFoundPpsId;
  } else {
    kuiPpsId = (m_sParaSetOffset.uiInUsePpsNum++);
    WelsInitPps (& pCtx->pPPSArray[kuiPpsId], pSps, pSubsetSps, kuiPpsId, true, kbUsingSubsetSps, kbEntropyCodingModeFlag);
  }
  SetUseSubsetFlag (kuiPpsId, kbUsingSubsetSps);
  return kuiPpsId;
}

void CWelsParametersetSpsPpsListing::UpdateParaSetNum (sWelsEncCtx* pCtx) {
  CWelsParametersetSpsListing::UpdateParaSetNum (pCtx);

  //UpdatePpsList (pCtx);
  pCtx->iPpsNum = m_sParaSetOffset.uiInUsePpsNum;
}

int32_t CWelsParametersetSpsPpsListing::GetCurrentPpsId (const int32_t iPpsId, const int32_t iIdrLoop) {
  return m_sParaSetOffset.iPpsIdList[iPpsId][iIdrLoop];
}

void CWelsParametersetSpsPpsListing::LoadPreviousStructure (SParaSetOffsetVariable* pParaSetOffsetVariable,
    int32_t* pPpsIdList) {
  memcpy (m_sParaSetOffset.sParaSetOffsetVariable, pParaSetOffsetVariable,
          (PARA_SET_TYPE)*sizeof (SParaSetOffsetVariable)); // confirmed_safe_unsafe_usage

  memcpy ((m_sParaSetOffset.iPpsIdList), pPpsIdList, MAX_DQ_LAYER_NUM * MAX_PPS_COUNT * sizeof (int32_t));

}

void CWelsParametersetSpsPpsListing::OutputCurrentStructure (SParaSetOffsetVariable* pParaSetOffsetVariable,
    int32_t* pPpsIdList, sWelsEncCtx* pCtx, SExistingParasetList* pExistingParasetList) {
  CWelsParametersetSpsListing::OutputCurrentStructure (pParaSetOffsetVariable, pPpsIdList, pCtx, pExistingParasetList);

  pExistingParasetList->uiInUsePpsNum = m_sParaSetOffset.uiInUsePpsNum;
  memcpy (pExistingParasetList->sPps, pCtx->pPps, MAX_PPS_COUNT * sizeof (SWelsPPS));
  memcpy (pPpsIdList, (m_sParaSetOffset.iPpsIdList), MAX_DQ_LAYER_NUM * MAX_PPS_COUNT * sizeof (int32_t));
}

//
//CWelsParametersetSpsListingPpsIncreasing
//

int32_t CWelsParametersetSpsListingPpsIncreasing::GetPpsIdOffset (const int32_t kiPpsId) {
  //same as CWelsParametersetIdIncreasing::GetPpsIdOffset
  return (m_sParaSetOffset.sParaSetOffsetVariable[PARA_SET_TYPE_PPS].iParaSetIdDelta[kiPpsId]);
}

void CWelsParametersetSpsListingPpsIncreasing::Update (const uint32_t kuiId, const int iParasetType) {
  //same as CWelsParametersetIdIncreasing::Update
#if _DEBUG
  assert (kuiId < MAX_DQ_LAYER_NUM);
#endif

  ParasetIdAdditionIdAdjust (& (m_sParaSetOffset.sParaSetOffsetVariable[iParasetType]),
                             kuiId,
                             (iParasetType != PARA_SET_TYPE_PPS) ? MAX_SPS_COUNT : MAX_PPS_COUNT);
}
}