forked from chikuzen/MPEG2DecPlus
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgetpic.cpp
1686 lines (1464 loc) · 52.7 KB
/
getpic.cpp
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
/* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */
/*
* Disclaimer of Warranty
*
* These software programs are available to the user without any license fee or
* royalty on an "as is" basis. The MPEG Software Simulation Group disclaims
* any and all warranties, whether express, implied, or statuary, including any
* implied warranties or merchantability or of fitness for a particular
* purpose. In no event shall the copyright-holder be liable for any
* incidental, punitive, or consequential damages of any kind whatsoever
* arising from the use of these programs.
*
* This disclaimer of warranty extends to the user of these programs and user's
* customers, employees, agents, transferees, successors, and assigns.
*
* The MPEG Software Simulation Group does not represent or warrant that the
* programs furnished hereunder are free of infringement of any third-party
* patents.
*
* Commercial implementations of MPEG-1 and MPEG-2 video, including shareware,
* are subject to royalty fees to patent holders. Many of these patents are
* general enough such that they are unavoidable regardless of implementation
* design.
*
*/
//#define MPEG2DEC_EXPORTS
#include <emmintrin.h>
#include "global.h"
#include "MPEG2Decoder.h"
#include "mc.h"
#include "idct.h"
const uint8_t cc_table[12] = {
0, 0, 0, 0, 1, 2, 1, 2, 1, 2, 1, 2
};
void CMPEG2Decoder::Decode_Picture(YV12PICT& dst)
{
if (picture_structure == FRAME_PICTURE && Second_Field)
Second_Field = 0;
if (picture_coding_type != B_TYPE)
{
pf_forward = pf_backward;
pf_backward = pf_current;
}
update_picture_buffers();
picture_data();
if (Fault_Flag == OUT_OF_BITS) return;
if (picture_structure == FRAME_PICTURE || Second_Field)
{
if (picture_coding_type == B_TYPE)
assembleFrame(auxframe, pf_current, dst);
else
assembleFrame(forward_reference_frame, pf_forward, dst);
}
if (picture_structure != FRAME_PICTURE)
Second_Field = !Second_Field;
}
/* reuse old picture buffers as soon as they are no longer needed */
void CMPEG2Decoder::update_picture_buffers()
{
int cc; /* color component index */
uint8_t* tmp; /* temporary swap pointer */
for (cc = 0; cc < 3; cc++)
{
/* B pictures do not need to be save for future reference */
if (picture_coding_type == B_TYPE)
{
current_frame[cc] = auxframe[cc];
}
else
{
if (!Second_Field)
{
/* only update at the beginning of the coded frame */
tmp = forward_reference_frame[cc];
/* the previously decoded reference frame is stored coincident with the
location where the backward reference frame is stored (backwards
prediction is not needed in P pictures) */
forward_reference_frame[cc] = backward_reference_frame[cc];
/* update pointer for potential future B pictures */
backward_reference_frame[cc] = tmp;
}
/* can erase over old backward reference frame since it is not used
in a P picture, and since any subsequent B pictures will use the
previously decoded I or P frame as the backward_reference_frame */
current_frame[cc] = backward_reference_frame[cc];
}
if (picture_structure == BOTTOM_FIELD)
current_frame[cc] += (cc == 0) ? Coded_Picture_Width : Chroma_Width;
}
}
/* decode all macroblocks of the current picture */
/* stages described in ISO/IEC 13818-2 section 7 */
inline void CMPEG2Decoder::picture_data()
{
/* number of macroblocks per picture */
int MBAmax = mb_width * mb_height;
if (picture_structure != FRAME_PICTURE)
MBAmax >>= 1;
for (;;) {
if (Fault_Flag == OUT_OF_BITS)
break;
Next_Start_Code();
uint32_t code = Show_Bits(32);
if (code < SLICE_START_CODE_MIN || code > SLICE_START_CODE_MAX)
break;
Flush_Buffer(32);
slice(MBAmax, code);
}
}
/* decode all macroblocks of the current picture */
/* ISO/IEC 13818-2 section 6.3.16 */
inline void CMPEG2Decoder::slice(int MBAmax, uint32_t code)
{
/* decode slice header (may change quantizer_scale) */
int slice_vert_pos_ext = slice_header();
/* decode macroblock address increment */
Fault_Flag = 0;
int MBAinc = Get_macroblock_address_increment();
if (MBAinc < 0)
{
// End of slice but we didn't process any macroblocks!
Fault_Flag = 4;
return;
}
/* set current location */
/* NOTE: the arithmetic used to derive macroblock_address below is
equivalent to ISO/IEC 13818-2 section 6.3.17: Macroblock */
int MBA = ((slice_vert_pos_ext << 7) + (code & 255) - 1) * mb_width + MBAinc - 1;
MBAinc = 1; // first macroblock in slice: not skipped
/* reset all DC coefficient and motion vector predictors */
/* ISO/IEC 13818-2 section 7.2.1: DC coefficients in intra blocks */
int dc_dct_pred[3] = { 0 };
/* ISO/IEC 13818-2 section 7.6.3.4: Resetting motion vector predictors */
int PMV[2][2][2] = { { { 0 } } };
// Set up pointer for storing quants for info and showQ.
int* qp = (picture_coding_type == B_TYPE) ? auxQP : backwardQP;
if (picture_structure == BOTTOM_FIELD)
qp += mb_width * mb_height / 2;
// This while loop condition just prevents us from processing more than
// the maximum number of macroblocks possible in a picture. The loop is
// unlikely to ever terminate on this condition. Usually, it will
// terminate at the end of the slice. The end of a slice is indicated
// by 23 zeroes after a macroblock. To detect that, we use a trick in
// Get_macroblock_address_increment(). See that function for an
// explanation.
int macroblock_type, motion_type, dct_type = 0;
int motion_vertical_field_select[2][2], dmvector[2];
while (MBA < MBAmax) {
if (MBAinc == 0) {
/* decode macroblock address increment */
MBAinc = Get_macroblock_address_increment();
if (MBAinc < 0 || Fault_Flag == OUT_OF_BITS) {
// End of slice or out of data.
break;
}
}
if (MBAinc == 1) {
decode_macroblock(macroblock_type, motion_type, dct_type, PMV,
dc_dct_pred, motion_vertical_field_select, dmvector);
}
else {
/* ISO/IEC 13818-2 section 7.6.6 */
skipped_macroblock(dc_dct_pred, PMV, motion_type, motion_vertical_field_select, macroblock_type);
}
if (Fault_Flag) {
break;
}
QP[MBA] = qp[MBA] = quantizer_scale;
/* ISO/IEC 13818-2 section 7.6 */
motion_compensation(MBA, macroblock_type, motion_type, PMV,
motion_vertical_field_select, dmvector, dct_type);
/* advance to next macroblock */
++MBA;
--MBAinc;
}
}
/* ISO/IEC 13818-2 section 6.3.17.1: Macroblock modes */
void CMPEG2Decoder::macroblock_modes(int& macroblock_type, int& motion_type,
int& motion_vector_count, int& mv_format,
int& dmv, int& mvscale, int& dct_type)
{
/* get macroblock_type */
macroblock_type = Get_macroblock_type();
if (Fault_Flag)
return;
/* get frame/field motion type */
if (macroblock_type & (MACROBLOCK_MOTION_FORWARD | MACROBLOCK_MOTION_BACKWARD)) {
if (picture_structure == FRAME_PICTURE && frame_pred_frame_dct)
motion_type = MC_FRAME;
else
motion_type = Get_Bits(2);
}
else if (picture_structure == FRAME_PICTURE) {
motion_type = MC_FRAME;
}
else {
motion_type = MC_FIELD;
}
/* derive motion_vector_count, mv_format and dmv, (table 6-17, 6-18) */
if (picture_structure == FRAME_PICTURE) {
motion_vector_count = (motion_type == MC_FIELD) ? 2 : 1;
mv_format = (motion_type == MC_FRAME) ? MV_FRAME : MV_FIELD;
}
else {
motion_vector_count = (motion_type == MC_16X8) ? 2 : 1;
mv_format = MV_FIELD;
}
dmv = (motion_type == MC_DMV); /* dual prime */
/*
field mv predictions in frame pictures have to be scaled
ISO/IEC 13818-2 section 7.6.3.1 Decoding the motion vectors
*/
mvscale = (mv_format == MV_FIELD && picture_structure == FRAME_PICTURE);
/* get dct_type (frame DCT / field DCT) */
dct_type = (picture_structure == FRAME_PICTURE) && (!frame_pred_frame_dct)
&& (macroblock_type & (MACROBLOCK_PATTERN | MACROBLOCK_INTRA)) ? Get_Bits(1) : 0;
}
/* move/add 8x8-Block from block[comp] to backward_reference_frame */
/* copy reconstructed 8x8 block from block[comp] to current_frame[]
ISO/IEC 13818-2 section 7.6.8: Adding prediction and coefficient data
This stage also embodies some of the operations implied by:
- ISO/IEC 13818-2 section 7.6.7: Combining predictions
- ISO/IEC 13818-2 section 6.1.3: Macroblock
*/
void CMPEG2Decoder::add_block(int count, int bx, int by, int dct_type, int addflag)
{
alignas(16) static const uint64_t mmmask_128C[2] = {
0x8080808080808080, 0x8080808080808080
};
const __m128i mask = addflag ? _mm_setzero_si128() :
_mm_load_si128(reinterpret_cast<const __m128i*>(mmmask_128C));
for (int comp = 0; comp < count; ++comp) {
int16_t* blockp = block[comp];
int cc = cc_table[comp];
int bxh = bx;
int byh = by;
int iincr;
uint8_t* rfp;
if (cc == 0) {
if (picture_structure == FRAME_PICTURE) {
if (dct_type) {
rfp = current_frame[0] + Coded_Picture_Width * (by + (comp & 2) / static_cast<int64_t>(2)) + bx + (comp & static_cast<int64_t>(1)) * 8;
iincr = Coded_Picture_Width * 2;
}
else {
rfp = current_frame[0] + Coded_Picture_Width * (by + (comp & 2) * static_cast<int64_t>(4)) + bx + (comp & static_cast<int64_t>(1)) * 8;
iincr = Coded_Picture_Width;
}
}
else {
rfp = current_frame[0] + (Coded_Picture_Width * static_cast<int64_t>(2)) * (by + (comp & 2) * static_cast<int64_t>(4)) + bx + (comp & 1) * static_cast<int64_t>(8);
iincr = Coded_Picture_Width * 2;
}
}
else {
if (chroma_format != CHROMA444) bxh /= 2;
if (chroma_format == CHROMA420) byh /= 2;
if (picture_structure == FRAME_PICTURE) {
if (dct_type && chroma_format != CHROMA420) {
/* field DCT coding */
rfp = current_frame[cc] + Chroma_Width * (byh + (comp & 2) / static_cast<int64_t>(2)) + bxh + (comp & 8);
iincr = Chroma_Width * 2;
}
else {
/* frame DCT coding */
rfp = current_frame[cc] + Chroma_Width * (byh + (comp & 2) * static_cast<int64_t>(4)) + bxh + (comp & 8);
iincr = Chroma_Width;
}
}
else {
/* field picture */
rfp = current_frame[cc] + (Chroma_Width * static_cast<int64_t>(2)) * (byh + (comp & 2) * static_cast<int64_t>(4)) + bxh + (comp & 8);
iincr = Chroma_Width * 2;
}
}
uint8_t* rfp1 = rfp + iincr;
iincr *= 2;
__m128i r0, r1;
if (addflag) {
r0 = _mm_loadl_epi64(reinterpret_cast<const __m128i*>(rfp));
r1 = _mm_loadl_epi64(reinterpret_cast<const __m128i*>(rfp1));
r0 = _mm_unpacklo_epi8(r0, mask);
r1 = _mm_unpacklo_epi8(r1, mask);
r0 = _mm_adds_epi16(r0, _mm_loadu_si128(reinterpret_cast<const __m128i*>(blockp)));
r1 = _mm_adds_epi16(r1, _mm_loadu_si128(reinterpret_cast<const __m128i*>(blockp + 8)));
_mm_storel_epi64(reinterpret_cast<__m128i*>(rfp), _mm_packus_epi16(r0, r0));
_mm_storel_epi64(reinterpret_cast<__m128i*>(rfp1), _mm_packus_epi16(r1, r1));
rfp += iincr;
rfp1 += iincr;
r0 = _mm_loadl_epi64(reinterpret_cast<const __m128i*>(rfp));
r1 = _mm_loadl_epi64(reinterpret_cast<const __m128i*>(rfp1));
r0 = _mm_unpacklo_epi8(r0, mask);
r1 = _mm_unpacklo_epi8(r1, mask);
r0 = _mm_adds_epi16(r0, _mm_loadu_si128(reinterpret_cast<const __m128i*>(blockp + 16)));
r1 = _mm_adds_epi16(r1, _mm_loadu_si128(reinterpret_cast<const __m128i*>(blockp + 24)));
_mm_storel_epi64(reinterpret_cast<__m128i*>(rfp), _mm_packus_epi16(r0, r0));
_mm_storel_epi64(reinterpret_cast<__m128i*>(rfp1), _mm_packus_epi16(r1, r1));
rfp += iincr;
rfp1 += iincr;
r0 = _mm_loadl_epi64(reinterpret_cast<const __m128i*>(rfp));
r1 = _mm_loadl_epi64(reinterpret_cast<const __m128i*>(rfp1));
r0 = _mm_unpacklo_epi8(r0, mask);
r1 = _mm_unpacklo_epi8(r1, mask);
r0 = _mm_adds_epi16(r0, _mm_loadu_si128(reinterpret_cast<const __m128i*>(blockp + 32)));
r1 = _mm_adds_epi16(r1, _mm_loadu_si128(reinterpret_cast<const __m128i*>(blockp + 40)));
_mm_storel_epi64(reinterpret_cast<__m128i*>(rfp), _mm_packus_epi16(r0, r0));
_mm_storel_epi64(reinterpret_cast<__m128i*>(rfp1), _mm_packus_epi16(r1, r1));
rfp += iincr;
rfp1 += iincr;
r0 = _mm_loadl_epi64(reinterpret_cast<const __m128i*>(rfp));
r1 = _mm_loadl_epi64(reinterpret_cast<const __m128i*>(rfp1));
r0 = _mm_unpacklo_epi8(r0, mask);
r1 = _mm_unpacklo_epi8(r1, mask);
r0 = _mm_adds_epi16(r0, _mm_loadu_si128(reinterpret_cast<const __m128i*>(blockp + 48)));
r1 = _mm_adds_epi16(r1, _mm_loadu_si128(reinterpret_cast<const __m128i*>(blockp + 56)));
_mm_storel_epi64(reinterpret_cast<__m128i*>(rfp), _mm_packus_epi16(r0, r0));
_mm_storel_epi64(reinterpret_cast<__m128i*>(rfp1), _mm_packus_epi16(r1, r1));
}
else {
r0 = _mm_load_si128(reinterpret_cast<const __m128i*>(blockp));
r1 = _mm_load_si128(reinterpret_cast<const __m128i*>(blockp + 8));
r0 = _mm_add_epi8(_mm_packs_epi16(r0, r1), mask);
_mm_storel_epi64(reinterpret_cast<__m128i*>(rfp), r0);
_mm_storel_epi64(reinterpret_cast<__m128i*>(rfp1), _mm_srli_si128(r0, 8));
rfp += iincr;
rfp1 += iincr;
r0 = _mm_load_si128(reinterpret_cast<const __m128i*>(blockp + 16));
r1 = _mm_load_si128(reinterpret_cast<const __m128i*>(blockp + 24));
r0 = _mm_add_epi8(_mm_packs_epi16(r0, r1), mask);
_mm_storel_epi64(reinterpret_cast<__m128i*>(rfp), r0);
_mm_storel_epi64(reinterpret_cast<__m128i*>(rfp1), _mm_srli_si128(r0, 8));
rfp += iincr;
rfp1 += iincr;
r0 = _mm_load_si128(reinterpret_cast<const __m128i*>(blockp + 32));
r1 = _mm_load_si128(reinterpret_cast<const __m128i*>(blockp + 40));
r0 = _mm_add_epi8(_mm_packs_epi16(r0, r1), mask);
_mm_storel_epi64(reinterpret_cast<__m128i*>(rfp), r0);
_mm_storel_epi64(reinterpret_cast<__m128i*>(rfp1), _mm_srli_si128(r0, 8));
rfp += iincr;
rfp1 += iincr;
r0 = _mm_load_si128(reinterpret_cast<const __m128i*>(blockp + 48));
r1 = _mm_load_si128(reinterpret_cast<const __m128i*>(blockp + 56));
r0 = _mm_add_epi8(_mm_packs_epi16(r0, r1), mask);
_mm_storel_epi64(reinterpret_cast<__m128i*>(rfp), r0);
_mm_storel_epi64(reinterpret_cast<__m128i*>(rfp1), _mm_srli_si128(r0, 8));
}
}
}
/* set scratch pad macroblock to zero */
void CMPEG2Decoder::clear_block(int count)
{
const __m128i zero = _mm_setzero_si128();
for (int comp = 0; comp < count; ++comp) {
//memset(block[comp], 0, 128);
__m128i* p = reinterpret_cast<__m128i*>(block[comp]);
_mm_store_si128(p, zero);
_mm_store_si128(++p, zero);
_mm_store_si128(++p, zero);
_mm_store_si128(++p, zero);
_mm_store_si128(++p, zero);
_mm_store_si128(++p, zero);
_mm_store_si128(++p, zero);
_mm_store_si128(++p, zero);
}
}
/* ISO/IEC 13818-2 section 7.6 */
void CMPEG2Decoder::motion_compensation(int MBA, int macroblock_type, int motion_type,
int PMV[2][2][2], int motion_vertical_field_select[2][2],
int dmvector[2], int dct_type)
{
prefetchTables();
/* derive current macroblock position within picture */
/* ISO/IEC 13818-2 section 6.3.1.6 and 6.3.1.7 */
int bx = 16 * (MBA % mb_width);
int by = 16 * (MBA / mb_width);
/* motion compensation */
if (!(macroblock_type & MACROBLOCK_INTRA))
form_predictions(bx, by, macroblock_type, motion_type, PMV,
motion_vertical_field_select, dmvector);
// idct is now a pointer
for (int comp = 0; comp < block_count - 1; ++comp) {
_mm_prefetch(reinterpret_cast<const char*>(block[comp + 1]), _MM_HINT_T0);
idctFunction(block[comp]);
}
idctFunction(block[block_count - 1]);
add_block(block_count, bx, by, dct_type, (macroblock_type & MACROBLOCK_INTRA) == 0);
}
/* ISO/IEC 13818-2 section 7.6.6 */
void CMPEG2Decoder::skipped_macroblock(int dc_dct_pred[3], int PMV[2][2][2], int& motion_type,
int motion_vertical_field_select[2][2], int& macroblock_type)
{
clear_block(block_count);
/* reset intra_dc predictors */
/* ISO/IEC 13818-2 section 7.2.1: DC coefficients in intra blocks */
dc_dct_pred[0] = dc_dct_pred[1] = dc_dct_pred[2] = 0;
/* reset motion vector predictors */
/* ISO/IEC 13818-2 section 7.6.3.4: Resetting motion vector predictors */
if (picture_coding_type == P_TYPE)
PMV[0][0][0] = PMV[0][0][1] = PMV[1][0][0] = PMV[1][0][1] = 0;
/* derive motion_type */
if (picture_structure == FRAME_PICTURE)
motion_type = MC_FRAME;
else
{
motion_type = MC_FIELD;
motion_vertical_field_select[0][0] = motion_vertical_field_select[0][1] =
(picture_structure == BOTTOM_FIELD);
}
if (picture_coding_type == I_TYPE)
{
Fault_Flag = true;
return;
}
/* clear MACROBLOCK_INTRA */
macroblock_type &= ~MACROBLOCK_INTRA;
}
/* ISO/IEC 13818-2 sections 7.2 through 7.5 */
void CMPEG2Decoder::decode_macroblock(int& macroblock_type, int& motion_type, int& dct_type,
int PMV[2][2][2], int dc_dct_pred[3],
int motion_vertical_field_select[2][2], int dmvector[2])
{
int quantizer_scale_code, comp, motion_vector_count, mv_format;
int dmv, mvscale, coded_block_pattern;
/* ISO/IEC 13818-2 section 6.3.17.1: Macroblock modes */
macroblock_modes(macroblock_type, motion_type, motion_vector_count, mv_format,
dmv, mvscale, dct_type);
if (Fault_Flag)
{
return; // go to next slice
}
if (macroblock_type & MACROBLOCK_QUANT)
{
quantizer_scale_code = Get_Bits(5);
/* ISO/IEC 13818-2 section 7.4.2.2: Quantizer scale factor */
if (mpeg_type == IS_MPEG2)
{
quantizer_scale = q_scale_type ?
Non_Linear_quantizer_scale[quantizer_scale_code] : (quantizer_scale_code << 1);
}
else
{
quantizer_scale = quantizer_scale_code;
}
}
/* ISO/IEC 13818-2 section 6.3.17.2: Motion vectors */
/* decode forward motion vectors */
if ((macroblock_type & MACROBLOCK_MOTION_FORWARD)
|| ((macroblock_type & MACROBLOCK_INTRA) && concealment_motion_vectors))
{
if (mpeg_type == IS_MPEG2)
{
motion_vectors(PMV, dmvector, motion_vertical_field_select, 0,
motion_vector_count, mv_format, f_code[0][0] - 1, f_code[0][1] - 1, dmv, mvscale);
}
else
{
motion_vector(PMV[0][0], dmvector, forward_f_code - 1, forward_f_code - 1, dmv, mvscale, full_pel_forward_vector);
}
}
if (Fault_Flag)
{
return; // go to next slice
}
/* decode backward motion vectors */
if (macroblock_type & MACROBLOCK_MOTION_BACKWARD)
{
if (mpeg_type == IS_MPEG2)
{
motion_vectors(PMV, dmvector, motion_vertical_field_select, 1,
motion_vector_count, mv_format, f_code[1][0] - 1, f_code[1][1] - 1, 0, mvscale);
}
else
{
motion_vector(PMV[0][1], dmvector, backward_f_code - 1, backward_f_code - 1, dmv, mvscale, full_pel_backward_vector);
}
}
if (Fault_Flag)
{
return; // go to next slice
}
if ((macroblock_type & MACROBLOCK_INTRA) && concealment_motion_vectors)
Flush_Buffer(1); // marker bit
/* macroblock_pattern */
/* ISO/IEC 13818-2 section 6.3.17.4: Coded block pattern */
if (macroblock_type & MACROBLOCK_PATTERN)
{
coded_block_pattern = Get_coded_block_pattern();
if (chroma_format == CHROMA422)
coded_block_pattern = (coded_block_pattern << 2) | Get_Bits(2);
else if (chroma_format == CHROMA444)
coded_block_pattern = (coded_block_pattern << 6) | Get_Bits(6);
}
else
coded_block_pattern = (macroblock_type & MACROBLOCK_INTRA) ? (1 << block_count) - 1 : 0;
if (Fault_Flag)
{
return; // go to next slice
}
clear_block(block_count);
/* decode blocks */
for (comp = 0; comp < block_count; comp++)
{
if (coded_block_pattern & (1 << (block_count - 1 - comp)))
{
if (macroblock_type & MACROBLOCK_INTRA)
{
if (mpeg_type == IS_MPEG2)
Decode_MPEG2_Intra_Block(comp, dc_dct_pred);
else
decode_mpeg1_intra_block(comp, dc_dct_pred);
}
else
{
if (mpeg_type == IS_MPEG2)
Decode_MPEG2_Non_Intra_Block(comp);
else
decode_mpeg1_non_intra_block(comp);
}
if (Fault_Flag)
{
return; // go to next slice
}
}
}
/* reset intra_dc predictors */
/* ISO/IEC 13818-2 section 7.2.1: DC coefficients in intra blocks */
if (!(macroblock_type & MACROBLOCK_INTRA))
dc_dct_pred[0] = dc_dct_pred[1] = dc_dct_pred[2] = 0;
/* reset motion vector predictors */
if ((macroblock_type & MACROBLOCK_INTRA) && !concealment_motion_vectors)
{
/* intra mb without concealment motion vectors */
/* ISO/IEC 13818-2 section 7.6.3.4: Resetting motion vector predictors */
PMV[0][0][0] = PMV[0][0][1] = PMV[1][0][0] = PMV[1][0][1] = 0;
PMV[0][1][0] = PMV[0][1][1] = PMV[1][1][0] = PMV[1][1][1] = 0;
}
/* special "No_MC" macroblock_type case */
/* ISO/IEC 13818-2 section 7.6.3.5: Prediction in P pictures */
if ((picture_coding_type == P_TYPE)
&& !(macroblock_type & (MACROBLOCK_MOTION_FORWARD | MACROBLOCK_INTRA)))
{
/* non-intra mb without forward mv in a P picture */
/* ISO/IEC 13818-2 section 7.6.3.4: Resetting motion vector predictors */
PMV[0][0][0] = PMV[0][0][1] = PMV[1][0][0] = PMV[1][0][1] = 0;
/* derive motion_type */
/* ISO/IEC 13818-2 section 6.3.17.1: Macroblock modes, frame_motion_type */
if (picture_structure == FRAME_PICTURE)
motion_type = MC_FRAME;
else
{
motion_type = MC_FIELD;
motion_vertical_field_select[0][0] = (picture_structure == BOTTOM_FIELD);
}
}
/* successfully decoded macroblock */
}
/* decode one intra coded MPEG-1 block */
void CMPEG2Decoder::decode_mpeg1_intra_block(int comp, int dc_dct_pred[])
{
long code, val = 0, i, j, sign;
const DCTtab* tab;
int16_t* bp;
bp = block[comp];
/* ISO/IEC 13818-2 section 7.2.1: decode DC coefficients */
switch (cc_table[comp])
{
case 0:
val = (dc_dct_pred[0] += Get_Luma_DC_dct_diff());
break;
case 1:
val = (dc_dct_pred[1] += Get_Chroma_DC_dct_diff());
break;
case 2:
val = (dc_dct_pred[2] += Get_Chroma_DC_dct_diff());
break;
}
bp[0] = (int16_t)(val << 3);
if (picture_coding_type == D_TYPE)
return;
/* decode AC coefficients */
for (i = 1; ; i++)
{
code = Show_Bits(16);
if (code >= 16384)
tab = &DCTtabnext[(code >> 12) - 4];
else if (code >= 1024)
tab = &DCTtab0[(code >> 8) - 4];
else if (code >= 512)
tab = &DCTtab1[(code >> 6) - 8];
else if (code >= 256)
tab = &DCTtab2[(code >> 4) - 16];
else if (code >= 128)
tab = &DCTtab3[(code >> 3) - 16];
else if (code >= 64)
tab = &DCTtab4[(code >> 2) - 16];
else if (code >= 32)
tab = &DCTtab5[(code >> 1) - 16];
else if (code >= 16)
tab = &DCTtab6[code - 16];
else
{
Fault_Flag = 1;
break;
}
Flush_Buffer(tab->len);
val = tab->run;
if (val == 65)
{
// escape
i += Get_Bits(6);
val = Get_Bits(8);
if (val == 0)
val = Get_Bits(8);
else if (val == 128)
val = Get_Bits(8) - 256;
else if (val > 128)
val -= 256;
sign = (val < 0);
if (sign)
val = -val;
}
else
{
if (val == 64)
break;
i += val;
val = tab->level;
sign = Get_Bits(1);
}
if (i >= 64)
{
Fault_Flag = 1;
break;
}
j = scan[0][i];
val = (val * quantizer_scale * intra_quantizer_matrix[j]) >> 3;
if (val)
val = (val - 1) | 1; // mismatch
if (val >= 2048) val = 2047 + sign; // saturation
if (sign)
val = -val;
bp[j] = (int16_t)val;
}
}
/* decode one non-intra coded MPEG-1 block */
void CMPEG2Decoder::decode_mpeg1_non_intra_block(int comp)
{
int32_t code, val = 0, i, j, sign;
const DCTtab* tab;
int16_t* bp;
bp = block[comp];
/* decode AC coefficients */
for (i = 0; ; i++)
{
code = Show_Bits(16);
if (code >= 16384)
{
if (i)
tab = &DCTtabnext[(code >> 12) - 4];
else
tab = &DCTtabfirst[(code >> 12) - 4];
}
else if (code >= 1024)
tab = &DCTtab0[(code >> 8) - 4];
else if (code >= 512)
tab = &DCTtab1[(code >> 6) - 8];
else if (code >= 256)
tab = &DCTtab2[(code >> 4) - 16];
else if (code >= 128)
tab = &DCTtab3[(code >> 3) - 16];
else if (code >= 64)
tab = &DCTtab4[(code >> 2) - 16];
else if (code >= 32)
tab = &DCTtab5[(code >> 1) - 16];
else if (code >= 16)
tab = &DCTtab6[code - 16];
else
{
Fault_Flag = 1;
break;
}
Flush_Buffer(tab->len);
val = tab->run;
if (val == 65)
{
// escape
i += Get_Bits(6);
val = Get_Bits(8);
if (val == 0)
val = Get_Bits(8);
else if (val == 128)
val = Get_Bits(8) - 256;
else if (val > 128)
val -= 256;
sign = (val < 0);
if (sign)
val = -val;
}
else
{
if (val == 64)
break;
i += val;
val = tab->level;
sign = Get_Bits(1);
}
if (i >= 64)
{
Fault_Flag = 1;
break;
}
j = scan[0][i];
val = (((val << 1) + 1) * quantizer_scale * non_intra_quantizer_matrix[j]) >> 4;
if (val)
val = (val - 1) | 1; // mismatch
if (val >= 2048)
val = 2047 + sign; //saturation
if (sign)
val = -val;
bp[j] = (int16_t)val;
}
}
/* decode one intra coded MPEG-2 block */
void CMPEG2Decoder::Decode_MPEG2_Intra_Block(int comp, int dc_dct_pred[])
{
int32_t code, val = 0, i, j, sign, sum;
const DCTtab* tab;
int16_t* bp;
int* qmat;
bp = block[comp];
qmat = (comp < 4 || chroma_format == CHROMA420)
? intra_quantizer_matrix : chroma_intra_quantizer_matrix;
/* ISO/IEC 13818-2 section 7.2.1: decode DC coefficients */
switch (cc_table[comp])
{
case 0:
val = (dc_dct_pred[0] += Get_Luma_DC_dct_diff());
break;
case 1:
val = (dc_dct_pred[1] += Get_Chroma_DC_dct_diff());
break;
case 2:
val = (dc_dct_pred[2] += Get_Chroma_DC_dct_diff());
break;
}
sum = val << (3 - intra_dc_precision);
bp[0] = (int16_t)sum;
/* decode AC coefficients */
for (i = 1; ; i++)
{
code = Show_Bits(16);
if (code >= 16384)
{
if (intra_vlc_format)
tab = &DCTtab0a[(code >> 8) - 4];
else
tab = &DCTtabnext[(code >> 12) - 4];
}
else if (code >= 1024)
{
if (intra_vlc_format)
tab = &DCTtab0a[(code >> 8) - 4];
else
tab = &DCTtab0[(code >> 8) - 4];
}
else if (code >= 512)
{
if (intra_vlc_format)
tab = &DCTtab1a[(code >> 6) - 8];
else
tab = &DCTtab1[(code >> 6) - 8];
}
else if (code >= 256)
tab = &DCTtab2[(code >> 4) - 16];
else if (code >= 128)
tab = &DCTtab3[(code >> 3) - 16];
else if (code >= 64)
tab = &DCTtab4[(code >> 2) - 16];
else if (code >= 32)
tab = &DCTtab5[(code >> 1) - 16];
else if (code >= 16)
tab = &DCTtab6[code - 16];
else
{
Fault_Flag = 1;
break;
}
Flush_Buffer(tab->len);
val = tab->run;
if (val == 65)
{
// escape
i += Get_Bits(6);
val = Get_Bits(12);
if (!(val & 2047))
{
Fault_Flag = 1;
break;
}
sign = (val >= 2048);
if (sign)
val = 4096 - val;
}
else
{
if (val == 64)
break;
i += val;
val = tab->level;
sign = Get_Bits(1);
}
if (i >= 64)
{
Fault_Flag = 1;
break;
}
j = scan[alternate_scan][i];
val = (val * quantizer_scale * qmat[j]) >> 4;
if (val >= 2048)
val = 2047 + sign; // saturation
if (sign)
val = -val;
bp[j] = (int16_t)val;
sum ^= val; // mismatch
}
if (!Fault_Flag && !(sum & 1))
bp[63] ^= 1; // mismatch control
}
/* decode one non-intra coded MPEG-2 block */
void CMPEG2Decoder::Decode_MPEG2_Non_Intra_Block(int comp)
{
int32_t code, val = 0, i, j, sign, sum;
const DCTtab* tab;
int16_t* bp;
int* qmat;
bp = block[comp];
qmat = (comp < 4 || chroma_format == CHROMA420)
? non_intra_quantizer_matrix : chroma_non_intra_quantizer_matrix;
/* decode AC coefficients */
sum = 0;
for (i = 0; ; i++)
{
code = Show_Bits(16);
if (code >= 16384)
{
if (i)
tab = &DCTtabnext[(code >> 12) - 4];
else
tab = &DCTtabfirst[(code >> 12) - 4];
}
else if (code >= 1024)
tab = &DCTtab0[(code >> 8) - 4];
else if (code >= 512)
tab = &DCTtab1[(code >> 6) - 8];
else if (code >= 256)
tab = &DCTtab2[(code >> 4) - 16];
else if (code >= 128)
tab = &DCTtab3[(code >> 3) - 16];
else if (code >= 64)
tab = &DCTtab4[(code >> 2) - 16];
else if (code >= 32)
tab = &DCTtab5[(code >> 1) - 16];
else if (code >= 16)
tab = &DCTtab6[code - 16];
else
{
Fault_Flag = 1;
break;
}
Flush_Buffer(tab->len);
val = tab->run;
if (val == 65)
{
// escape
i += Get_Bits(6);
val = Get_Bits(12);
if (!(val & 2047))