Repository navigation
Expand file tree
/
Copy pathdecoder_core.cpp
More file actions
3069 lines (2710 loc) · 133 KB
/
Copy pathdecoder_core.cpp
File metadata and controls
3069 lines (2710 loc) · 133 KB
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
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*!
* \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.
*
* decoder_core.c: Wels decoder framework core implementation
*/
#include "decoder_core.h"
#include "error_code.h"
#include "memmgr_nal_unit.h"
#include "au_parser.h"
#include "decode_slice.h"
#include "manage_dec_ref.h"
#include "expand_pic.h"
#include "decoder.h"
#include "decode_mb_aux.h"
#include "memory_align.h"
#include "error_concealment.h"
namespace WelsDec {
static inline int32_t DecodeFrameConstruction (PWelsDecoderContext pCtx, uint8_t** ppDst, SBufferInfo* pDstInfo) {
PDqLayer pCurDq = pCtx->pCurDqLayer;
PPicture pPic = pCtx->pDec;
const int32_t kiWidth = pCurDq->iMbWidth << 4;
const int32_t kiHeight = pCurDq->iMbHeight << 4;
const int32_t kiTotalNumMbInCurLayer = pCurDq->iMbWidth * pCurDq->iMbHeight;
bool bFrameCompleteFlag = true;
if (pPic->bNewSeqBegin) {
memcpy (& (pCtx->sFrameCrop), & (pCurDq->sLayerInfo.sSliceInLayer.sSliceHeaderExt.sSliceHeader.pSps->sFrameCrop),
sizeof (SPosOffset)); //confirmed_safe_unsafe_usage
#ifdef LONG_TERM_REF
pCtx->bParamSetsLostFlag = false;
#else
pCtx->bReferenceLostAtT0Flag = false; // need initialize it due new seq, 6/4/2010
#endif //LONG_TERM_REF
if (pCtx->iTotalNumMbRec == kiTotalNumMbInCurLayer) {
pCtx->bPrintFrameErrorTraceFlag = true;
WelsLog (& (pCtx->sLogCtx), WELS_LOG_INFO,
"DecodeFrameConstruction(): will output first frame of new sequence, %d x %d, crop_left:%d, crop_right:%d, crop_top:%d, crop_bottom:%d, ignored error packet:%d.",
kiWidth, kiHeight, pCtx->sFrameCrop.iLeftOffset, pCtx->sFrameCrop.iRightOffset, pCtx->sFrameCrop.iTopOffset,
pCtx->sFrameCrop.iBottomOffset, pCtx->iIgnoredErrorInfoPacketCount);
pCtx->iIgnoredErrorInfoPacketCount = 0;
}
}
const int32_t kiActualWidth = kiWidth - (pCtx->sFrameCrop.iLeftOffset + pCtx->sFrameCrop.iRightOffset) * 2;
const int32_t kiActualHeight = kiHeight - (pCtx->sFrameCrop.iTopOffset + pCtx->sFrameCrop.iBottomOffset) * 2;
if (pCtx->pParam->eEcActiveIdc == ERROR_CON_DISABLE) {
if ((pCtx->pDecoderStatistics->uiWidth != (unsigned int) kiActualWidth)
|| (pCtx->pDecoderStatistics->uiHeight != (unsigned int) kiActualHeight)) {
pCtx->pDecoderStatistics->uiResolutionChangeTimes++;
pCtx->pDecoderStatistics->uiWidth = kiActualWidth;
pCtx->pDecoderStatistics->uiHeight = kiActualHeight;
}
UpdateDecStatNoFreezingInfo (pCtx);
}
if (pCtx->pParam->bParseOnly) { //should exit for parse only to prevent access NULL pDstInfo
PAccessUnit pCurAu = pCtx->pAccessUnitList;
if (dsErrorFree == pCtx->iErrorCode) { //correct decoding, add to data buffer
SParserBsInfo* pParser = pCtx->pParserBsInfo;
SNalUnit* pCurNal = NULL;
int32_t iTotalNalLen = 0;
int32_t iNalLen = 0;
int32_t iNum = 0;
while (iNum < pParser->iNalNum) {
iTotalNalLen += pParser->pNalLenInByte[iNum++];
}
uint8_t* pDstBuf = pParser->pDstBuff + iTotalNalLen;
int32_t iIdx = pCurAu->uiStartPos;
int32_t iEndIdx = pCurAu->uiEndPos;
uint8_t* pNalBs = NULL;
pParser->uiOutBsTimeStamp = (pCurAu->pNalUnitsList [iIdx]) ? pCurAu->pNalUnitsList [iIdx]->uiTimeStamp : 0;
//pParser->iNalNum = 0;
pParser->iSpsWidthInPixel = (pCtx->pSps->iMbWidth << 4) - ((pCtx->pSps->sFrameCrop.iLeftOffset +
pCtx->pSps->sFrameCrop.iRightOffset) << 1);
pParser->iSpsHeightInPixel = (pCtx->pSps->iMbHeight << 4) - ((pCtx->pSps->sFrameCrop.iTopOffset +
pCtx->pSps->sFrameCrop.iBottomOffset) << 1);
if (pCurAu->pNalUnitsList [iIdx]->sNalHeaderExt.bIdrFlag) { //IDR
if (pCtx->bFrameFinish) { //add required sps/pps
if (pParser->iNalNum > pCtx->iMaxNalNum - 2) { //2 reserved for sps+pps
WelsLog (& (pCtx->sLogCtx), WELS_LOG_INFO,
"DecodeFrameConstruction(): current NAL num (%d) plus sps & pps exceeds permitted num (%d). Will expand",
pParser->iNalNum, pCtx->iMaxNalNum);
WELS_VERIFY_RETURN_IF (ERR_INFO_OUT_OF_MEMORY, ExpandBsLenBuffer (pCtx, pParser->iNalNum + 2))
}
bool bSubSps = (NAL_UNIT_CODED_SLICE_EXT == pCurAu->pNalUnitsList [iIdx]->sNalHeaderExt.sNalUnitHeader.eNalUnitType);
SSpsBsInfo* pSpsBs = NULL;
SPpsBsInfo* pPpsBs = NULL;
int32_t iSpsId = pCtx->pSps->iSpsId;
int32_t iPpsId = pCtx->pPps->iPpsId;
pCtx->bParamSetsLostFlag = false;
//find required sps, pps and write into dst buff
pSpsBs = bSubSps ? &pCtx->sSubsetSpsBsInfo [iSpsId] : &pCtx->sSpsBsInfo [iSpsId];
pPpsBs = &pCtx->sPpsBsInfo [iPpsId];
if (pDstBuf - pParser->pDstBuff + pSpsBs->uiSpsBsLen + pPpsBs->uiPpsBsLen >= MAX_ACCESS_UNIT_CAPACITY) {
WelsLog (& (pCtx->sLogCtx), WELS_LOG_ERROR,
"DecodeFrameConstruction(): sps pps size: (%d %d) too large. Failed to parse. \n", pSpsBs->uiSpsBsLen,
pPpsBs->uiPpsBsLen);
pCtx->iErrorCode |= dsOutOfMemory;
pCtx->pParserBsInfo->iNalNum = 0;
return ERR_INFO_OUT_OF_MEMORY;
}
memcpy (pDstBuf, pSpsBs->pSpsBsBuf, pSpsBs->uiSpsBsLen);
pParser->pNalLenInByte [pParser->iNalNum ++] = pSpsBs->uiSpsBsLen;
pDstBuf += pSpsBs->uiSpsBsLen;
memcpy (pDstBuf, pPpsBs->pPpsBsBuf, pPpsBs->uiPpsBsLen);
pParser->pNalLenInByte [pParser->iNalNum ++] = pPpsBs->uiPpsBsLen;
pDstBuf += pPpsBs->uiPpsBsLen;
pCtx->bFrameFinish = false;
}
}
//then VCL data re-write
if (pParser->iNalNum + iEndIdx - iIdx + 1 > pCtx->iMaxNalNum) { //calculate total NAL num
WelsLog (& (pCtx->sLogCtx), WELS_LOG_INFO,
"DecodeFrameConstruction(): current NAL num (%d) exceeds permitted num (%d). Will expand",
pParser->iNalNum + iEndIdx - iIdx + 1, pCtx->iMaxNalNum);
WELS_VERIFY_RETURN_IF (ERR_INFO_OUT_OF_MEMORY, ExpandBsLenBuffer (pCtx, pParser->iNalNum + iEndIdx - iIdx + 1))
}
while (iIdx <= iEndIdx) {
pCurNal = pCurAu->pNalUnitsList [iIdx ++];
iNalLen = pCurNal->sNalData.sVclNal.iNalLength;
pNalBs = pCurNal->sNalData.sVclNal.pNalPos;
pParser->pNalLenInByte [pParser->iNalNum ++] = iNalLen;
if (pDstBuf - pParser->pDstBuff + iNalLen >= MAX_ACCESS_UNIT_CAPACITY) {
WelsLog (& (pCtx->sLogCtx), WELS_LOG_ERROR,
"DecodeFrameConstruction(): composed output size (%ld) exceeds (%d). Failed to parse. current data pos %d out of %d:, previously accumulated num: %d, total num: %d, previously accumulated len: %d, current len: %d, current buf pos: %p, header buf pos: %p \n",
(long) (pDstBuf - pParser->pDstBuff + iNalLen), MAX_ACCESS_UNIT_CAPACITY, iIdx, iEndIdx, iNum, pParser->iNalNum,
iTotalNalLen, iNalLen, pDstBuf, pParser->pDstBuff);
pCtx->iErrorCode |= dsOutOfMemory;
pCtx->pParserBsInfo->iNalNum = 0;
return ERR_INFO_OUT_OF_MEMORY;
}
memcpy (pDstBuf, pNalBs, iNalLen);
pDstBuf += iNalLen;
}
if (pCtx->iTotalNumMbRec == kiTotalNumMbInCurLayer) { //frame complete
pCtx->iTotalNumMbRec = 0;
pCtx->bFramePending = false;
pCtx->bFrameFinish = true; //finish current frame and mark it
} else if (pCtx->iTotalNumMbRec != 0) { //frame incomplete
pCtx->bFramePending = true;
pCtx->pDec->bIsComplete = false;
pCtx->bFrameFinish = false; //current frame not finished
pCtx->iErrorCode |= dsFramePending;
return ERR_INFO_PARSEONLY_PENDING;
//pCtx->pParserBsInfo->iNalNum = 0;
}
} else { //error
pCtx->pParserBsInfo->uiOutBsTimeStamp = 0;
pCtx->pParserBsInfo->iNalNum = 0;
pCtx->pParserBsInfo->iSpsWidthInPixel = 0;
pCtx->pParserBsInfo->iSpsHeightInPixel = 0;
return ERR_INFO_PARSEONLY_ERROR;
}
return ERR_NONE;
}
if (pCtx->iTotalNumMbRec != kiTotalNumMbInCurLayer) {
WelsLog (& (pCtx->sLogCtx), WELS_LOG_DEBUG,
"DecodeFrameConstruction(): iTotalNumMbRec:%d, total_num_mb_sps:%d, cur_layer_mb_width:%d, cur_layer_mb_height:%d ",
pCtx->iTotalNumMbRec, kiTotalNumMbInCurLayer, pCurDq->iMbWidth, pCurDq->iMbHeight);
bFrameCompleteFlag = false; //return later after output buffer is done
if (pCtx->bInstantDecFlag) { //no-delay decoding, wait for new slice
return ERR_INFO_MB_NUM_INADEQUATE;
}
} else if (pCurDq->sLayerInfo.sNalHeaderExt.bIdrFlag
&& (pCtx->iErrorCode == dsErrorFree)) { //complete non-ECed IDR frame done
pCtx->pDec->bIsComplete = true;
pCtx->bFreezeOutput = false;
}
pCtx->iTotalNumMbRec = 0;
//////output:::normal path
pDstInfo->uiOutYuvTimeStamp = pPic->uiTimeStamp;
ppDst[0] = pPic->pData[0];
ppDst[1] = pPic->pData[1];
ppDst[2] = pPic->pData[2];
pDstInfo->UsrData.sSystemBuffer.iFormat = videoFormatI420;
pDstInfo->UsrData.sSystemBuffer.iWidth = kiActualWidth;
pDstInfo->UsrData.sSystemBuffer.iHeight = kiActualHeight;
pDstInfo->UsrData.sSystemBuffer.iStride[0] = pPic->iLinesize[0];
pDstInfo->UsrData.sSystemBuffer.iStride[1] = pPic->iLinesize[1];
ppDst[0] = ppDst[0] + pCtx->sFrameCrop.iTopOffset * 2 * pPic->iLinesize[0] + pCtx->sFrameCrop.iLeftOffset * 2;
ppDst[1] = ppDst[1] + pCtx->sFrameCrop.iTopOffset * pPic->iLinesize[1] + pCtx->sFrameCrop.iLeftOffset;
ppDst[2] = ppDst[2] + pCtx->sFrameCrop.iTopOffset * pPic->iLinesize[1] + pCtx->sFrameCrop.iLeftOffset;
for (int i = 0; i < 3; ++i) {
pDstInfo->pDst[i] = ppDst[i];
}
pDstInfo->iBufferStatus = 1;
if (GetThreadCount (pCtx) > 1 && pPic->bIsComplete == false) {
pPic->bIsComplete = true;
}
if (GetThreadCount (pCtx) > 1) {
uint32_t uiMbHeight = (pCtx->pDec->iHeightInPixel + 15) >> 4;
for (uint32_t i = 0; i < uiMbHeight; ++i) {
SET_EVENT (&pCtx->pDec->pReadyEvent[i]);
}
}
bool bOutResChange = false;
if (GetThreadCount (pCtx) <= 1 || pCtx->pLastThreadCtx == NULL) {
bOutResChange = (pCtx->iLastImgWidthInPixel != pDstInfo->UsrData.sSystemBuffer.iWidth)
|| (pCtx->iLastImgHeightInPixel != pDstInfo->UsrData.sSystemBuffer.iHeight);
} else {
if (pCtx->pLastThreadCtx != NULL) {
PWelsDecoderThreadCTX pLastThreadCtx = (PWelsDecoderThreadCTX) (pCtx->pLastThreadCtx);
bOutResChange = (pLastThreadCtx->pCtx->iLastImgWidthInPixel != pDstInfo->UsrData.sSystemBuffer.iWidth)
|| (pLastThreadCtx->pCtx->iLastImgHeightInPixel != pDstInfo->UsrData.sSystemBuffer.iHeight);
}
}
pCtx->iLastImgWidthInPixel = pDstInfo->UsrData.sSystemBuffer.iWidth;
pCtx->iLastImgHeightInPixel = pDstInfo->UsrData.sSystemBuffer.iHeight;
if (pCtx->pParam->eEcActiveIdc == ERROR_CON_DISABLE) //no buffer output if EC is disabled and frame incomplete
pDstInfo->iBufferStatus = (int32_t) (bFrameCompleteFlag
&& pPic->bIsComplete); // When EC disable, ECed picture not output
else if ((pCtx->pParam->eEcActiveIdc == ERROR_CON_SLICE_COPY_CROSS_IDR_FREEZE_RES_CHANGE
|| pCtx->pParam->eEcActiveIdc == ERROR_CON_SLICE_MV_COPY_CROSS_IDR_FREEZE_RES_CHANGE)
&& pCtx->iErrorCode && bOutResChange)
pCtx->bFreezeOutput = true;
if (pDstInfo->iBufferStatus == 0) {
if (!bFrameCompleteFlag)
pCtx->iErrorCode |= dsBitstreamError;
return ERR_INFO_MB_NUM_INADEQUATE;
}
if (pCtx->bFreezeOutput) {
pDstInfo->iBufferStatus = 0;
if (pPic->bNewSeqBegin) {
WelsLog (& (pCtx->sLogCtx), WELS_LOG_INFO,
"DecodeFrameConstruction():New sequence detected, but freezed, correct MBs (%d) out of whole MBs (%d).",
kiTotalNumMbInCurLayer - pCtx->iMbEcedNum, kiTotalNumMbInCurLayer);
}
}
pCtx->iMbEcedNum = pPic->iMbEcedNum;
pCtx->iMbNum = pPic->iMbNum;
pCtx->iMbEcedPropNum = pPic->iMbEcedPropNum;
if (pCtx->pParam->eEcActiveIdc != ERROR_CON_DISABLE) {
if (pDstInfo->iBufferStatus && ((pCtx->pDecoderStatistics->uiWidth != (unsigned int) kiActualWidth)
|| (pCtx->pDecoderStatistics->uiHeight != (unsigned int) kiActualHeight))) {
pCtx->pDecoderStatistics->uiResolutionChangeTimes++;
pCtx->pDecoderStatistics->uiWidth = kiActualWidth;
pCtx->pDecoderStatistics->uiHeight = kiActualHeight;
}
UpdateDecStat (pCtx, pDstInfo->iBufferStatus != 0);
}
return ERR_NONE;
}
inline bool CheckSliceNeedReconstruct (uint8_t uiLayerDqId, uint8_t uiTargetDqId) {
return (uiLayerDqId == uiTargetDqId); // target layer
}
inline uint8_t GetTargetDqId (uint8_t uiTargetDqId, SDecodingParam* psParam) {
uint8_t uiRequiredDqId = psParam ? psParam->uiTargetDqLayer : (uint8_t)255;
return WELS_MIN (uiTargetDqId, uiRequiredDqId);
}
inline void HandleReferenceLostL0 (PWelsDecoderContext pCtx, PNalUnit pCurNal) {
if (0 == pCurNal->sNalHeaderExt.uiTemporalId) {
pCtx->bReferenceLostAtT0Flag = true;
}
pCtx->iErrorCode |= dsBitstreamError;
}
inline void HandleReferenceLost (PWelsDecoderContext pCtx, PNalUnit pCurNal) {
if ((0 == pCurNal->sNalHeaderExt.uiTemporalId) || (1 == pCurNal->sNalHeaderExt.uiTemporalId)) {
pCtx->bReferenceLostAtT0Flag = true;
}
pCtx->iErrorCode |= dsRefLost;
}
inline int32_t WelsDecodeConstructSlice (PWelsDecoderContext pCtx, PNalUnit pCurNal) {
int32_t iRet = WelsTargetSliceConstruction (pCtx);
if (iRet) {
HandleReferenceLostL0 (pCtx, pCurNal);
}
return iRet;
}
int32_t ParsePredWeightedTable (PBitStringAux pBs, PSliceHeader pSh) {
uint32_t uiCode;
int32_t iList = 0;
int32_t iCode;
WELS_READ_VERIFY (BsGetUe (pBs, &uiCode));
WELS_CHECK_SE_BOTH_ERROR_NOLOG (uiCode, 0, 7, "luma_log2_weight_denom",
GENERATE_ERROR_NO (ERR_LEVEL_SLICE_HEADER, ERR_INFO_INVALID_LUMA_LOG2_WEIGHT_DENOM));
pSh->sPredWeightTable.uiLumaLog2WeightDenom = uiCode;
if (pSh->pSps->uiChromaArrayType != 0) {
WELS_READ_VERIFY (BsGetUe (pBs, &uiCode));
WELS_CHECK_SE_BOTH_ERROR_NOLOG (uiCode, 0, 7, "chroma_log2_weight_denom",
GENERATE_ERROR_NO (ERR_LEVEL_SLICE_HEADER, ERR_INFO_INVALID_CHROMA_LOG2_WEIGHT_DENOM));
pSh->sPredWeightTable.uiChromaLog2WeightDenom = uiCode;
}
if ((pSh->sPredWeightTable.uiLumaLog2WeightDenom | pSh->sPredWeightTable.uiChromaLog2WeightDenom) > 7)
return ERR_NONE;
do {
for (int i = 0; i < pSh->uiRefCount[iList]; i++) {
//luma
WELS_READ_VERIFY (BsGetOneBit (pBs, &uiCode));
if (!!uiCode) {
WELS_READ_VERIFY (BsGetSe (pBs, &iCode));
WELS_CHECK_SE_BOTH_ERROR_NOLOG (iCode, -128, 127, "luma_weight",
GENERATE_ERROR_NO (ERR_LEVEL_SLICE_HEADER, ERR_INFO_INVALID_LUMA_WEIGHT));
pSh->sPredWeightTable.sPredList[iList].iLumaWeight[i] = iCode;
WELS_READ_VERIFY (BsGetSe (pBs, &iCode));
WELS_CHECK_SE_BOTH_ERROR_NOLOG (iCode, -128, 127, "luma_offset",
GENERATE_ERROR_NO (ERR_LEVEL_SLICE_HEADER, ERR_INFO_INVALID_LUMA_OFFSET));
pSh->sPredWeightTable.sPredList[iList].iLumaOffset[i] = iCode;
} else {
pSh->sPredWeightTable.sPredList[iList].iLumaWeight[i] = 1 << (pSh->sPredWeightTable.uiLumaLog2WeightDenom);
pSh->sPredWeightTable.sPredList[iList].iLumaOffset[i] = 0;
}
//chroma
if (pSh->pSps->uiChromaArrayType == 0)
continue;
WELS_READ_VERIFY (BsGetOneBit (pBs, &uiCode));
if (!!uiCode) {
for (int j = 0; j < 2; j++) {
WELS_READ_VERIFY (BsGetSe (pBs, &iCode));
WELS_CHECK_SE_BOTH_ERROR_NOLOG (iCode, -128, 127, "chroma_weight",
GENERATE_ERROR_NO (ERR_LEVEL_SLICE_HEADER, ERR_INFO_INVALID_CHROMA_WEIGHT));
pSh->sPredWeightTable.sPredList[iList].iChromaWeight[i][j] = iCode;
WELS_READ_VERIFY (BsGetSe (pBs, &iCode));
WELS_CHECK_SE_BOTH_ERROR_NOLOG (iCode, -128, 127, "chroma_offset",
GENERATE_ERROR_NO (ERR_LEVEL_SLICE_HEADER, ERR_INFO_INVALID_CHROMA_OFFSET));
pSh->sPredWeightTable.sPredList[iList].iChromaOffset[i][j] = iCode;
}
} else {
for (int j = 0; j < 2; j++) {
pSh->sPredWeightTable.sPredList[iList].iChromaWeight[i][j] = 1 << (pSh->sPredWeightTable.uiChromaLog2WeightDenom);
pSh->sPredWeightTable.sPredList[iList].iChromaOffset[i][j] = 0;
}
}
}
++iList;
if (pSh->eSliceType != B_SLICE) {
break;
}
} while (iList < LIST_A);//TODO: SUPPORT LIST_A
return ERR_NONE;
}
void CreateImplicitWeightTable (PWelsDecoderContext pCtx) {
PSlice pSlice = &pCtx->pCurDqLayer->sLayerInfo.sSliceInLayer;
PSliceHeader pSliceHeader = &pSlice->sSliceHeaderExt.sSliceHeader;
PDqLayer pCurDqLayer = pCtx->pCurDqLayer;
if (pCurDqLayer->bUseWeightedBiPredIdc && pSliceHeader->pPps->uiWeightedBipredIdc == 2) {
int32_t iPoc = pSliceHeader->iPicOrderCntLsb;
//fix Bugzilla 1485229 check if pointers are NULL
if (pCtx->sRefPic.pRefList[LIST_0][0] && pCtx->sRefPic.pRefList[LIST_1][0]) {
if (pSliceHeader->uiRefCount[0] == 1 && pSliceHeader->uiRefCount[1] == 1
&& int64_t(pCtx->sRefPic.pRefList[LIST_0][0]->iFramePoc) + int64_t(pCtx->sRefPic.pRefList[LIST_1][0]->iFramePoc) == 2 * int64_t(iPoc)) {
pCurDqLayer->bUseWeightedBiPredIdc = false;
return;
}
}
pCurDqLayer->pPredWeightTable->uiLumaLog2WeightDenom = 5;
pCurDqLayer->pPredWeightTable->uiChromaLog2WeightDenom = 5;
for (int32_t iRef0 = 0; iRef0 < pSliceHeader->uiRefCount[0]; iRef0++) {
if (pCtx->sRefPic.pRefList[LIST_0][iRef0]) {
const int32_t iPoc0 = pCtx->sRefPic.pRefList[LIST_0][iRef0]->iFramePoc;
bool bIsLongRef0 = pCtx->sRefPic.pRefList[LIST_0][iRef0]->bIsLongRef;
for (int32_t iRef1 = 0; iRef1 < pSliceHeader->uiRefCount[1]; iRef1++) {
if (pCtx->sRefPic.pRefList[LIST_1][iRef1]) {
const int32_t iPoc1 = pCtx->sRefPic.pRefList[LIST_1][iRef1]->iFramePoc;
bool bIsLongRef1 = pCtx->sRefPic.pRefList[LIST_1][iRef1]->bIsLongRef;
pCurDqLayer->pPredWeightTable->iImplicitWeight[iRef0][iRef1] = 32;
if (!bIsLongRef0 && !bIsLongRef1) {
const int32_t iTd = WELS_CLIP3 (iPoc1 - iPoc0, -128, 127);
if (iTd) {
int32_t iTb = WELS_CLIP3 (iPoc - iPoc0, -128, 127);
int32_t iTx = (16384 + (WELS_ABS (iTd) >> 1)) / iTd;
int32_t iDistScaleFactor = (iTb * iTx + 32) >> 8;
if (iDistScaleFactor >= -64 && iDistScaleFactor <= 128) {
pCurDqLayer->pPredWeightTable->iImplicitWeight[iRef0][iRef1] = 64 - iDistScaleFactor;
}
}
}
}
}
}
}
}
return;
}
/*
* Predeclared function routines ..
*/
int32_t ParseRefPicListReordering (PBitStringAux pBs, PSliceHeader pSh) {
int32_t iList = 0;
const EWelsSliceType keSt = pSh->eSliceType;
PRefPicListReorderSyn pRefPicListReordering = &pSh->pRefPicListReordering;
PSps pSps = pSh->pSps;
uint32_t uiCode;
if (keSt == I_SLICE || keSt == SI_SLICE)
return ERR_NONE;
// Common syntaxs for P or B slices: list0, list1 followed if B slices used.
do {
WELS_READ_VERIFY (BsGetOneBit (pBs, &uiCode)); //ref_pic_list_modification_flag_l0
pRefPicListReordering->bRefPicListReorderingFlag[iList] = !!uiCode;
if (pRefPicListReordering->bRefPicListReorderingFlag[iList]) {
int32_t iIdx = 0;
do {
WELS_READ_VERIFY (BsGetUe (pBs, &uiCode)); //modification_of_pic_nums_idc
const uint32_t kuiIdc = uiCode;
//Fixed the referrence list reordering crash issue.(fault kIdc value > 3 case)---
if (((iIdx >= MAX_REF_PIC_COUNT) && (kuiIdc != 3)) || (kuiIdc > 3)) {
return GENERATE_ERROR_NO (ERR_LEVEL_SLICE_HEADER, ERR_INFO_INVALID_REF_REORDERING);
}
pRefPicListReordering->sReorderingSyn[iList][iIdx].uiReorderingOfPicNumsIdc = kuiIdc;
if (kuiIdc == 3)
break;
if (iIdx >= pSh->uiRefCount[iList] || iIdx >= MAX_REF_PIC_COUNT)
return GENERATE_ERROR_NO (ERR_LEVEL_SLICE_HEADER, ERR_INFO_INVALID_REF_REORDERING);
if (kuiIdc == 0 || kuiIdc == 1) {
// abs_diff_pic_num_minus1 should be in range 0 to MaxPicNum-1, MaxPicNum is derived as
// 2^(4+log2_max_frame_num_minus4)
WELS_READ_VERIFY (BsGetUe (pBs, &uiCode)); //abs_diff_pic_num_minus1
WELS_CHECK_SE_UPPER_ERROR_NOLOG (uiCode, (uint32_t) (1 << pSps->uiLog2MaxFrameNum), "abs_diff_pic_num_minus1",
GENERATE_ERROR_NO (ERR_LEVEL_SLICE_HEADER, ERR_INFO_INVALID_REF_REORDERING));
pRefPicListReordering->sReorderingSyn[iList][iIdx].uiAbsDiffPicNumMinus1 = uiCode; // uiAbsDiffPicNumMinus1
} else if (kuiIdc == 2) {
WELS_READ_VERIFY (BsGetUe (pBs, &uiCode)); //long_term_pic_num
pRefPicListReordering->sReorderingSyn[iList][iIdx].uiLongTermPicNum = uiCode;
}
++ iIdx;
} while (true);
}
if (keSt != B_SLICE)
break;
++ iList;
} while (iList < LIST_A);
return ERR_NONE;
}
int32_t ParseDecRefPicMarking (PWelsDecoderContext pCtx, PBitStringAux pBs, PSliceHeader pSh, PSps pSps,
const bool kbIdrFlag) {
PRefPicMarking const kpRefMarking = &pSh->sRefMarking;
uint32_t uiCode;
if (kbIdrFlag) {
WELS_READ_VERIFY (BsGetOneBit (pBs, &uiCode)); //no_output_of_prior_pics_flag
kpRefMarking->bNoOutputOfPriorPicsFlag = !!uiCode;
WELS_READ_VERIFY (BsGetOneBit (pBs, &uiCode)); //long_term_reference_flag
kpRefMarking->bLongTermRefFlag = !!uiCode;
} else {
WELS_READ_VERIFY (BsGetOneBit (pBs, &uiCode)); //adaptive_ref_pic_marking_mode_flag
kpRefMarking->bAdaptiveRefPicMarkingModeFlag = !!uiCode;
if (kpRefMarking->bAdaptiveRefPicMarkingModeFlag) {
int32_t iIdx = 0;
bool bAllowMmco5 = true, bMmco4Exist = false, bMmco5Exist = false, bMmco6Exist = false;
do {
WELS_READ_VERIFY (BsGetUe (pBs, &uiCode)); //memory_management_control_operation
const uint32_t kuiMmco = uiCode;
kpRefMarking->sMmcoRef[iIdx].uiMmcoType = kuiMmco;
if (kuiMmco == MMCO_END)
break;
if (kuiMmco == MMCO_SHORT2UNUSED || kuiMmco == MMCO_SHORT2LONG) {
bAllowMmco5 = false;
WELS_READ_VERIFY (BsGetUe (pBs, &uiCode)); //difference_of_pic_nums_minus1
kpRefMarking->sMmcoRef[iIdx].iDiffOfPicNum = 1 + uiCode;
kpRefMarking->sMmcoRef[iIdx].iShortFrameNum = (pSh->iFrameNum - kpRefMarking->sMmcoRef[iIdx].iDiffOfPicNum) & ((
1 << pSps->uiLog2MaxFrameNum) - 1);
} else if (kuiMmco == MMCO_LONG2UNUSED) {
bAllowMmco5 = false;
WELS_READ_VERIFY (BsGetUe (pBs, &uiCode)); //long_term_pic_num
kpRefMarking->sMmcoRef[iIdx].uiLongTermPicNum = uiCode;
}
if (kuiMmco == MMCO_SHORT2LONG || kuiMmco == MMCO_LONG) {
if (kuiMmco == MMCO_LONG) {
WELS_VERIFY_RETURN_IF (-1, bMmco6Exist);
bMmco6Exist = true;
}
WELS_READ_VERIFY (BsGetUe (pBs, &uiCode)); //long_term_frame_idx
kpRefMarking->sMmcoRef[iIdx].iLongTermFrameIdx = uiCode;
} else if (kuiMmco == MMCO_SET_MAX_LONG) {
WELS_VERIFY_RETURN_IF (-1, bMmco4Exist);
bMmco4Exist = true;
WELS_READ_VERIFY (BsGetUe (pBs, &uiCode)); //max_long_term_frame_idx_plus1
int32_t iMaxLongTermFrameIdx = -1 + uiCode;
if (iMaxLongTermFrameIdx > pSps->iNumRefFrames) {
//ISO/IEC 14496-10:2009(E) 7.4.3.3 Decoded reference picture marking semantics page 96
return GENERATE_ERROR_NO (ERR_LEVEL_SLICE_HEADER, ERR_INFO_INVALID_REF_MARKING);
}
kpRefMarking->sMmcoRef[iIdx].iMaxLongTermFrameIdx = iMaxLongTermFrameIdx;
} else if (kuiMmco == MMCO_RESET) {
WELS_VERIFY_RETURN_IF (-1, (!bAllowMmco5 || bMmco5Exist));
bMmco5Exist = true;
pCtx->pLastDecPicInfo->iPrevPicOrderCntLsb = 0;
pCtx->pLastDecPicInfo->iPrevPicOrderCntMsb = 0;
pSh->iPicOrderCntLsb = 0;
if (pCtx->pSliceHeader)
pCtx->pSliceHeader->iPicOrderCntLsb = 0;
}
++ iIdx;
} while (iIdx < MAX_MMCO_COUNT);
}
}
return ERR_NONE;
}
bool FillDefaultSliceHeaderExt (PSliceHeaderExt pShExt, PNalUnitHeaderExt pNalExt) {
if (pShExt == NULL || pNalExt == NULL)
return false;
if (pNalExt->iNoInterLayerPredFlag || pNalExt->uiQualityId > 0)
pShExt->bBasePredWeightTableFlag = false;
else
pShExt->bBasePredWeightTableFlag = true;
pShExt->uiRefLayerDqId = (uint8_t) - 1;
pShExt->uiDisableInterLayerDeblockingFilterIdc = 0;
pShExt->iInterLayerSliceAlphaC0Offset = 0;
pShExt->iInterLayerSliceBetaOffset = 0;
pShExt->bConstrainedIntraResamplingFlag = false;
pShExt->uiRefLayerChromaPhaseXPlus1Flag = 0;
pShExt->uiRefLayerChromaPhaseYPlus1 = 1;
//memset(&pShExt->sScaledRefLayer, 0, sizeof(SPosOffset));
pShExt->iScaledRefLayerPicWidthInSampleLuma = pShExt->sSliceHeader.iMbWidth << 4;
pShExt->iScaledRefLayerPicHeightInSampleLuma = pShExt->sSliceHeader.iMbHeight << 4;
pShExt->bSliceSkipFlag = false;
pShExt->bAdaptiveBaseModeFlag = false;
pShExt->bDefaultBaseModeFlag = false;
pShExt->bAdaptiveMotionPredFlag = false;
pShExt->bDefaultMotionPredFlag = false;
pShExt->bAdaptiveResidualPredFlag = false;
pShExt->bDefaultResidualPredFlag = false;
pShExt->bTCoeffLevelPredFlag = false;
pShExt->uiScanIdxStart = 0;
pShExt->uiScanIdxEnd = 15;
return true;
}
int32_t InitBsBuffer (PWelsDecoderContext pCtx) {
if (pCtx == NULL)
return ERR_INFO_INVALID_PTR;
CMemoryAlign* pMa = pCtx->pMemAlign;
pCtx->iMaxBsBufferSizeInByte = MIN_ACCESS_UNIT_CAPACITY * MAX_BUFFERED_NUM;
if ((pCtx->sRawData.pHead = static_cast<uint8_t*> (pMa->WelsMallocz (pCtx->iMaxBsBufferSizeInByte,
"pCtx->sRawData.pHead"))) == NULL) {
return ERR_INFO_OUT_OF_MEMORY;
}
pCtx->sRawData.pStartPos = pCtx->sRawData.pCurPos = pCtx->sRawData.pHead;
pCtx->sRawData.pEnd = pCtx->sRawData.pHead + pCtx->iMaxBsBufferSizeInByte;
if (pCtx->pParam->bParseOnly) {
pCtx->pParserBsInfo = static_cast<SParserBsInfo*> (pMa->WelsMallocz (sizeof (SParserBsInfo), "pCtx->pParserBsInfo"));
if (pCtx->pParserBsInfo == NULL) {
return ERR_INFO_OUT_OF_MEMORY;
}
memset (pCtx->pParserBsInfo, 0, sizeof (SParserBsInfo));
pCtx->pParserBsInfo->pDstBuff = static_cast<uint8_t*> (pMa->WelsMallocz (MAX_ACCESS_UNIT_CAPACITY * sizeof (uint8_t),
"pCtx->pParserBsInfo->pDstBuff"));
if (pCtx->pParserBsInfo->pDstBuff == NULL) {
return ERR_INFO_OUT_OF_MEMORY;
}
memset (pCtx->pParserBsInfo->pDstBuff, 0, MAX_ACCESS_UNIT_CAPACITY * sizeof (uint8_t));
if ((pCtx->sSavedData.pHead = static_cast<uint8_t*> (pMa->WelsMallocz (pCtx->iMaxBsBufferSizeInByte,
"pCtx->sSavedData.pHead"))) == NULL) {
return ERR_INFO_OUT_OF_MEMORY;
}
pCtx->sSavedData.pStartPos = pCtx->sSavedData.pCurPos = pCtx->sSavedData.pHead;
pCtx->sSavedData.pEnd = pCtx->sSavedData.pHead + pCtx->iMaxBsBufferSizeInByte;
pCtx->iMaxNalNum = MAX_NAL_UNITS_IN_LAYER + 2; //2 reserved for SPS+PPS
pCtx->pParserBsInfo->pNalLenInByte = static_cast<int*> (pMa->WelsMallocz (pCtx->iMaxNalNum * sizeof (int),
"pCtx->pParserBsInfo->pNalLenInByte"));
if (pCtx->pParserBsInfo->pNalLenInByte == NULL) {
return ERR_INFO_OUT_OF_MEMORY;
}
}
return ERR_NONE;
}
int32_t ExpandBsBuffer (PWelsDecoderContext pCtx, const int kiSrcLen) {
if (pCtx == NULL)
return ERR_INFO_INVALID_PTR;
int32_t iExpandStepShift = 1;
int32_t iNewBuffLen = WELS_MAX ((kiSrcLen * MAX_BUFFERED_NUM), (pCtx->iMaxBsBufferSizeInByte << iExpandStepShift));
//allocate new bs buffer
CMemoryAlign* pMa = pCtx->pMemAlign;
//Realloc sRawData
uint8_t* pNewBsBuff = static_cast<uint8_t*> (pMa->WelsMallocz (iNewBuffLen, "pCtx->sRawData.pHead"));
if (pNewBsBuff == NULL) {
WelsLog (& (pCtx->sLogCtx), WELS_LOG_ERROR, "ExpandBsBuffer() Failed for malloc pNewBsBuff (%d)", iNewBuffLen);
pCtx->iErrorCode |= dsOutOfMemory;
return ERR_INFO_OUT_OF_MEMORY;
}
// Retarget all queued NAL units in current AU list. uiAvailUnitsNum is the
// queued-NAL count that can be consumed later; using uiActualUnitsNum here
// can leave queued entries pointing to freed old buffer.
const uint32_t kuiRetargetNum = pCtx->pAccessUnitList->uiAvailUnitsNum;
for (uint32_t i = 0; i < kuiRetargetNum; ++i) {
PNalUnit pNal = pCtx->pAccessUnitList->pNalUnitsList[i];
if (pNal == NULL) {
continue;
}
PBitStringAux pSliceBitsRead = &pNal->sNalData.sVclNal.sSliceBitsRead;
pSliceBitsRead->pStartBuf = pSliceBitsRead->pStartBuf - pCtx->sRawData.pHead + pNewBsBuff;
pSliceBitsRead->pEndBuf = pSliceBitsRead->pEndBuf - pCtx->sRawData.pHead + pNewBsBuff;
pSliceBitsRead->pCurBuf = pSliceBitsRead->pCurBuf - pCtx->sRawData.pHead + pNewBsBuff;
}
//Copy current buffer status to new buffer
memcpy (pNewBsBuff, pCtx->sRawData.pHead, pCtx->iMaxBsBufferSizeInByte);
pCtx->sRawData.pStartPos = pNewBsBuff + (pCtx->sRawData.pStartPos - pCtx->sRawData.pHead);
pCtx->sRawData.pCurPos = pNewBsBuff + (pCtx->sRawData.pCurPos - pCtx->sRawData.pHead);
pCtx->sRawData.pEnd = pNewBsBuff + iNewBuffLen;
pMa->WelsFree (pCtx->sRawData.pHead, "pCtx->sRawData.pHead");
pCtx->sRawData.pHead = pNewBsBuff;
if (pCtx->pParam->bParseOnly) {
//Realloc sSavedData
uint8_t* pOldSavedBsBuff = pCtx->sSavedData.pHead;
uint8_t* pNewSavedBsBuff = static_cast<uint8_t*> (pMa->WelsMallocz (iNewBuffLen, "pCtx->sSavedData.pHead"));
if (pNewSavedBsBuff == NULL) {
WelsLog (& (pCtx->sLogCtx), WELS_LOG_ERROR, "ExpandBsBuffer() Failed for malloc pNewSavedBsBuff (%d)", iNewBuffLen);
pCtx->iErrorCode |= dsOutOfMemory;
return ERR_INFO_OUT_OF_MEMORY;
}
//Copy current buffer status to new buffer
memcpy (pNewSavedBsBuff, pCtx->sSavedData.pHead, pCtx->iMaxBsBufferSizeInByte);
pCtx->sSavedData.pStartPos = pNewSavedBsBuff + (pCtx->sSavedData.pStartPos - pCtx->sSavedData.pHead);
pCtx->sSavedData.pCurPos = pNewSavedBsBuff + (pCtx->sSavedData.pCurPos - pCtx->sSavedData.pHead);
pCtx->sSavedData.pEnd = pNewSavedBsBuff + iNewBuffLen;
// Retarget pNalPos for all queued NALs (current AU + next AU) before freeing.
const uintptr_t kuiOldStart = reinterpret_cast<uintptr_t> (pOldSavedBsBuff);
const uintptr_t kuiOldEnd = kuiOldStart + static_cast<uintptr_t> (pCtx->iMaxBsBufferSizeInByte);
for (uint32_t i = 0; i < kuiRetargetNum; ++i) {
PNalUnit pNal = pCtx->pAccessUnitList->pNalUnitsList[i];
if (pNal == NULL || pNal->sNalData.sVclNal.pNalPos == NULL) {
continue;
}
const uintptr_t kuiNalPos = reinterpret_cast<uintptr_t> (pNal->sNalData.sVclNal.pNalPos);
if (kuiNalPos >= kuiOldStart && kuiNalPos < kuiOldEnd) {
pNal->sNalData.sVclNal.pNalPos = pNewSavedBsBuff + (kuiNalPos - kuiOldStart);
}
}
pMa->WelsFree (pCtx->sSavedData.pHead, "pCtx->sSavedData.pHead");
pCtx->sSavedData.pHead = pNewSavedBsBuff;
}
pCtx->iMaxBsBufferSizeInByte = iNewBuffLen;
return ERR_NONE;
}
int32_t ExpandBsLenBuffer (PWelsDecoderContext pCtx, const int kiCurrLen) {
SParserBsInfo* pParser = pCtx->pParserBsInfo;
if (!pParser->pNalLenInByte)
return ERR_INFO_INVALID_ACCESS;
int iNewLen = kiCurrLen;
if (kiCurrLen >= MAX_MB_SIZE + 2) { //exceeds the max MB number of level 5.2
WelsLog (& (pCtx->sLogCtx), WELS_LOG_WARNING, "Current nal num (%d) exceededs %d.", kiCurrLen, MAX_MB_SIZE);
pCtx->iErrorCode |= dsOutOfMemory;
return ERR_INFO_OUT_OF_MEMORY;
} else {
iNewLen = kiCurrLen << 1;
iNewLen = WELS_MIN (iNewLen, MAX_MB_SIZE + 2);
}
CMemoryAlign* pMa = pCtx->pMemAlign;
int* pNewLenBuffer = static_cast<int*> (pMa->WelsMallocz (iNewLen * sizeof (int),
"pCtx->pParserBsInfo->pNalLenInByte"));
if (pNewLenBuffer == NULL) {
pCtx->iErrorCode |= dsOutOfMemory;
return ERR_INFO_OUT_OF_MEMORY;
}
//copy existing data from old length buffer to new
memcpy (pNewLenBuffer, pParser->pNalLenInByte, pCtx->iMaxNalNum * sizeof (int));
pMa->WelsFree (pParser->pNalLenInByte, "pCtx->pParserBsInfo->pNalLenInByte");
pParser->pNalLenInByte = pNewLenBuffer;
pCtx->iMaxNalNum = iNewLen;
return ERR_NONE;
}
int32_t CheckBsBuffer (PWelsDecoderContext pCtx, const int32_t kiSrcLen) {
if (kiSrcLen > MAX_ACCESS_UNIT_CAPACITY) { //exceeds max allowed data
WelsLog (& (pCtx->sLogCtx), WELS_LOG_WARNING, "Max AU size exceeded. Allowed size = %d, current size = %d",
MAX_ACCESS_UNIT_CAPACITY,
kiSrcLen);
pCtx->iErrorCode |= dsBitstreamError;
return ERR_INFO_INVALID_ACCESS;
} else if (kiSrcLen > pCtx->iMaxBsBufferSizeInByte /
MAX_BUFFERED_NUM) { //may lead to buffer overwrite, prevent it by expanding buffer
if (ExpandBsBuffer (pCtx, kiSrcLen)) {
return ERR_INFO_OUT_OF_MEMORY;
}
}
return ERR_NONE;
}
/*
* WelsInitStaticMemory
* Memory request for new introduced data
* Especially for:
* rbsp_au_buffer, cur_dq_layer_ptr and ref_dq_layer_ptr in MB info cache.
* return:
* 0 - success; otherwise returned error_no defined in error_no.h.
*/
int32_t WelsInitStaticMemory (PWelsDecoderContext pCtx) {
if (pCtx == NULL) {
return ERR_INFO_INVALID_PTR;
}
if (MemInitNalList (&pCtx->pAccessUnitList, MAX_NAL_UNIT_NUM_IN_AU, pCtx->pMemAlign) != 0)
return ERR_INFO_OUT_OF_MEMORY;
if (InitBsBuffer (pCtx) != 0)
return ERR_INFO_OUT_OF_MEMORY;
pCtx->uiTargetDqId = (uint8_t) - 1;
pCtx->bEndOfStreamFlag = false;
return ERR_NONE;
}
/*
* WelsFreeStaticMemory
* Free memory introduced in WelsInitStaticMemory at destruction of decoder.
*
*/
void WelsFreeStaticMemory (PWelsDecoderContext pCtx) {
if (pCtx == NULL)
return;
CMemoryAlign* pMa = pCtx->pMemAlign;
MemFreeNalList (&pCtx->pAccessUnitList, pMa);
if (pCtx->sRawData.pHead) {
pMa->WelsFree (pCtx->sRawData.pHead, "pCtx->sRawData->pHead");
}
pCtx->sRawData.pHead = NULL;
pCtx->sRawData.pEnd = NULL;
pCtx->sRawData.pStartPos = NULL;
pCtx->sRawData.pCurPos = NULL;
if (pCtx->pParam->bParseOnly) {
if (pCtx->sSavedData.pHead) {
pMa->WelsFree (pCtx->sSavedData.pHead, "pCtx->sSavedData->pHead");
}
pCtx->sSavedData.pHead = NULL;
pCtx->sSavedData.pEnd = NULL;
pCtx->sSavedData.pStartPos = NULL;
pCtx->sSavedData.pCurPos = NULL;
if (pCtx->pParserBsInfo) {
if (pCtx->pParserBsInfo->pNalLenInByte) {
pMa->WelsFree (pCtx->pParserBsInfo->pNalLenInByte, "pCtx->pParserBsInfo->pNalLenInByte");
pCtx->pParserBsInfo->pNalLenInByte = NULL;
pCtx->iMaxNalNum = 0;
}
if (pCtx->pParserBsInfo->pDstBuff) {
pMa->WelsFree (pCtx->pParserBsInfo->pDstBuff, "pCtx->pParserBsInfo->pDstBuff");
pCtx->pParserBsInfo->pDstBuff = NULL;
}
pMa->WelsFree (pCtx->pParserBsInfo, "pCtx->pParserBsInfo");
pCtx->pParserBsInfo = NULL;
}
}
if (NULL != pCtx->pParam) {
pMa->WelsFree (pCtx->pParam, "pCtx->pParam");
pCtx->pParam = NULL;
}
}
/*
* DecodeNalHeaderExt
* Trigger condition: NAL_UNIT_TYPE = NAL_UNIT_PREFIX or NAL_UNIT_CODED_SLICE_EXT
* Parameter:
* pNal: target NALUnit ptr
* pSrc: NAL Unit bitstream
*/
void DecodeNalHeaderExt (PNalUnit pNal, uint8_t* pSrc) {
PNalUnitHeaderExt pHeaderExt = &pNal->sNalHeaderExt;
uint8_t uiCurByte = *pSrc;
pHeaderExt->bIdrFlag = !! (uiCurByte & 0x40);
pHeaderExt->uiPriorityId = uiCurByte & 0x3F;
uiCurByte = * (++pSrc);
pHeaderExt->iNoInterLayerPredFlag = uiCurByte >> 7;
pHeaderExt->uiDependencyId = (uiCurByte & 0x70) >> 4;
pHeaderExt->uiQualityId = uiCurByte & 0x0F;
uiCurByte = * (++pSrc);
pHeaderExt->uiTemporalId = uiCurByte >> 5;
pHeaderExt->bUseRefBasePicFlag = !! (uiCurByte & 0x10);
pHeaderExt->bDiscardableFlag = !! (uiCurByte & 0x08);
pHeaderExt->bOutputFlag = !! (uiCurByte & 0x04);
pHeaderExt->uiReservedThree2Bits = uiCurByte & 0x03;
pHeaderExt->uiLayerDqId = (pHeaderExt->uiDependencyId << 4) | pHeaderExt->uiQualityId;
}
void UpdateDecoderStatisticsForActiveParaset (SDecoderStatistics* pDecoderStatistics,
PSps pSps, PPps pPps) {
pDecoderStatistics->iCurrentActiveSpsId = pSps->iSpsId;
pDecoderStatistics->iCurrentActivePpsId = pPps->iPpsId;
pDecoderStatistics->uiProfile = static_cast<unsigned int> (pSps->uiProfileIdc);
pDecoderStatistics->uiLevel = pSps->uiLevelIdc;
}
#define SLICE_HEADER_IDR_PIC_ID_MAX 65535
#define SLICE_HEADER_REDUNDANT_PIC_CNT_MAX 127
#define SLICE_HEADER_ALPHAC0_BETA_OFFSET_MIN -12
#define SLICE_HEADER_ALPHAC0_BETA_OFFSET_MAX 12
#define SLICE_HEADER_INTER_LAYER_ALPHAC0_BETA_OFFSET_MIN -12
#define SLICE_HEADER_INTER_LAYER_ALPHAC0_BETA_OFFSET_MAX 12
#define MAX_NUM_REF_IDX_L0_ACTIVE_MINUS1 15
#define MAX_NUM_REF_IDX_L1_ACTIVE_MINUS1 15
#define SLICE_HEADER_CABAC_INIT_IDC_MAX 2
/*
* decode_slice_header_avc
* Parse slice header of bitstream in avc for storing data structure
*/
int32_t ParseSliceHeaderSyntaxs (PWelsDecoderContext pCtx, PBitStringAux pBs, const bool kbExtensionFlag) {
PNalUnit const kpCurNal =
pCtx->pAccessUnitList->pNalUnitsList[pCtx->pAccessUnitList->uiAvailUnitsNum -
1];
PNalUnitHeaderExt pNalHeaderExt = NULL;
PSliceHeader pSliceHead = NULL;
PSliceHeaderExt pSliceHeadExt = NULL;
PSubsetSps pSubsetSps = NULL;
PSps pSps = NULL;
PPps pPps = NULL;
EWelsNalUnitType eNalType = static_cast<EWelsNalUnitType> (0);
int32_t iPpsId = 0;
int32_t iRet = ERR_NONE;
uint8_t uiSliceType = 0;
uint8_t uiQualityId = BASE_QUALITY_ID;
bool bIdrFlag = false;
bool bSgChangeCycleInvolved = false; // involved slice group change cycle ?
uint32_t uiCode;
int32_t iCode;
SLogContext* pLogCtx = & (pCtx->sLogCtx);
if (kpCurNal == NULL) {
return ERR_INFO_OUT_OF_MEMORY;
}
pNalHeaderExt = &kpCurNal->sNalHeaderExt;
pSliceHead = &kpCurNal->sNalData.sVclNal.sSliceHeaderExt.sSliceHeader;
eNalType = pNalHeaderExt->sNalUnitHeader.eNalUnitType;
pSliceHeadExt = &kpCurNal->sNalData.sVclNal.sSliceHeaderExt;
if (pSliceHeadExt) {
SRefBasePicMarking sBaseMarking;
const bool kbStoreRefBaseFlag = pSliceHeadExt->bStoreRefBasePicFlag;
memcpy (&sBaseMarking, &pSliceHeadExt->sRefBasePicMarking, sizeof (SRefBasePicMarking)); //confirmed_safe_unsafe_usage
memset (pSliceHeadExt, 0, sizeof (SSliceHeaderExt));
pSliceHeadExt->bStoreRefBasePicFlag = kbStoreRefBaseFlag;
memcpy (&pSliceHeadExt->sRefBasePicMarking, &sBaseMarking, sizeof (SRefBasePicMarking)); //confirmed_safe_unsafe_usage
}
kpCurNal->sNalData.sVclNal.bSliceHeaderExtFlag = kbExtensionFlag;
// first_mb_in_slice
WELS_READ_VERIFY (BsGetUe (pBs, &uiCode)); //first_mb_in_slice
WELS_CHECK_SE_UPPER_ERROR (uiCode, 36863u, "first_mb_in_slice", GENERATE_ERROR_NO (ERR_LEVEL_SLICE_HEADER,
ERR_INFO_INVALID_FIRST_MB_IN_SLICE));
pSliceHead->iFirstMbInSlice = uiCode;
WELS_READ_VERIFY (BsGetUe (pBs, &uiCode)); //slice_type
uiSliceType = uiCode;
if (uiSliceType > 9) {
WelsLog (pLogCtx, WELS_LOG_WARNING, "slice type too large (%d) at first_mb(%d)", uiSliceType,
pSliceHead->iFirstMbInSlice);
return GENERATE_ERROR_NO (ERR_LEVEL_SLICE_HEADER, ERR_INFO_INVALID_SLICE_TYPE);
}
if (uiSliceType > 4)
uiSliceType -= 5;
if ((NAL_UNIT_CODED_SLICE_IDR == eNalType) && (I_SLICE != uiSliceType)) {
WelsLog (pLogCtx, WELS_LOG_WARNING, "Invalid slice type(%d) in IDR picture. ", uiSliceType);
return GENERATE_ERROR_NO (ERR_LEVEL_SLICE_HEADER, ERR_INFO_INVALID_SLICE_TYPE);
}
if (kbExtensionFlag) {
if (uiSliceType > 2) {
WelsLog (pLogCtx, WELS_LOG_WARNING, "Invalid slice type(%d).", uiSliceType);
return GENERATE_ERROR_NO (ERR_LEVEL_SLICE_HEADER, ERR_INFO_INVALID_SLICE_TYPE);
}
}
pSliceHead->eSliceType = static_cast <EWelsSliceType> (uiSliceType);
WELS_READ_VERIFY (BsGetUe (pBs, &uiCode)); //pic_parameter_set_id
WELS_CHECK_SE_UPPER_ERROR (uiCode, (MAX_PPS_COUNT - 1), "iPpsId out of range",
GENERATE_ERROR_NO (ERR_LEVEL_SLICE_HEADER,
ERR_INFO_PPS_ID_OVERFLOW));
iPpsId = uiCode;
//add check PPS available here
if (pCtx->sSpsPpsCtx.bPpsAvailFlags[iPpsId] == false) {
pCtx->pDecoderStatistics->iPpsReportErrorNum++;
if (pCtx->sSpsPpsCtx.iPPSLastInvalidId != iPpsId) {
WelsLog (pLogCtx, WELS_LOG_ERROR, "PPS id (%d) is invalid, previous id (%d) error ignored (%d)!", iPpsId,
pCtx->sSpsPpsCtx.iPPSLastInvalidId, pCtx->sSpsPpsCtx.iPPSInvalidNum);
pCtx->sSpsPpsCtx.iPPSLastInvalidId = iPpsId;
pCtx->sSpsPpsCtx.iPPSInvalidNum = 0;
} else {
pCtx->sSpsPpsCtx.iPPSInvalidNum++;
}
pCtx->iErrorCode |= dsNoParamSets;
return GENERATE_ERROR_NO (ERR_LEVEL_SLICE_HEADER, ERR_INFO_INVALID_PPS_ID);
}
pCtx->sSpsPpsCtx.iPPSLastInvalidId = -1;
pPps = &pCtx->sSpsPpsCtx.sPpsBuffer[iPpsId];
if (pPps->uiNumSliceGroups == 0) {
WelsLog (pLogCtx, WELS_LOG_WARNING, "Invalid PPS referenced");
pCtx->iErrorCode |= dsNoParamSets;
return GENERATE_ERROR_NO (ERR_LEVEL_SLICE_HEADER, ERR_INFO_NO_PARAM_SETS);
}
if (kbExtensionFlag) {