-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathedge264_headers.c
1951 lines (1818 loc) · 83.7 KB
/
edge264_headers.c
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
#include "edge264_internal.h"
#include "edge264_bitstream.c"
#include "edge264_deblock.c"
#include "edge264_inter.c"
#include "edge264_intra.c"
#include "edge264_mvpred.c"
#include "edge264_residual.c"
#define CABAC 0
#include "edge264_slice.c"
#define CABAC 1
#include "edge264_slice.c"
/**
* Default scaling matrices (tables 7-3 and 7-4).
*/
static const i8x16 Default_4x4_Intra =
{6, 13, 20, 28, 13, 20, 28, 32, 20, 28, 32, 37, 28, 32, 37, 42};
static const i8x16 Default_4x4_Inter =
{10, 14, 20, 24, 14, 20, 24, 27, 20, 24, 27, 30, 24, 27, 30, 34};
static const i8x16 Default_8x8_Intra[4] = {
{ 6, 10, 13, 16, 18, 23, 25, 27, 10, 11, 16, 18, 23, 25, 27, 29},
{13, 16, 18, 23, 25, 27, 29, 31, 16, 18, 23, 25, 27, 29, 31, 33},
{18, 23, 25, 27, 29, 31, 33, 36, 23, 25, 27, 29, 31, 33, 36, 38},
{25, 27, 29, 31, 33, 36, 38, 40, 27, 29, 31, 33, 36, 38, 40, 42},
};
static const i8x16 Default_8x8_Inter[4] = {
{ 9, 13, 15, 17, 19, 21, 22, 24, 13, 13, 17, 19, 21, 22, 24, 25},
{15, 17, 19, 21, 22, 24, 25, 27, 17, 19, 21, 22, 24, 25, 27, 28},
{19, 21, 22, 24, 25, 27, 28, 30, 21, 22, 24, 25, 27, 28, 30, 32},
{22, 24, 25, 27, 28, 30, 32, 33, 24, 25, 27, 28, 30, 32, 33, 35},
};
/**
* This function sets the context pointers to the frame about to be decoded,
* and fills the context caches with useful values.
*/
static void initialize_context(Edge264Context *ctx, int currPic)
{
static const int8_t QP_Y2C[88] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 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, 29, 30, 31, 32, 32, 33, 34, 34, 35, 35, 36, 36, 37, 37, 37, 38, 38, 38, 39, 39, 39, 39,
39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39};
union { int8_t q[32]; i8x16 v[2]; } tb, td;
ctx->PicOrderCnt = min(ctx->d->FieldOrderCnt[0][currPic], ctx->d->FieldOrderCnt[1][currPic]);
ctx->CurrMbAddr = ctx->t.first_mb_in_slice;
ctx->mby = (unsigned)ctx->t.first_mb_in_slice / (unsigned)ctx->t.pic_width_in_mbs;
ctx->mbx = (unsigned)ctx->t.first_mb_in_slice % (unsigned)ctx->t.pic_width_in_mbs;
ctx->samples_mb[0] = ctx->t.samples_base + (ctx->mbx + ctx->mby * ctx->t.stride[0]) * 16;
ctx->samples_mb[1] = ctx->t.samples_base + ctx->t.plane_size_Y + (ctx->mbx + ctx->mby * ctx->t.stride[1]) * 8;
ctx->samples_mb[2] = ctx->samples_mb[1] + (ctx->t.stride[1] >> 1);
int mb_offset = ctx->t.plane_size_Y + ctx->t.plane_size_C + sizeof(Edge264Macroblock) * (ctx->mbx + ctx->mby * (ctx->t.pic_width_in_mbs + 1));
ctx->mbCol = ctx->_mb = (Edge264Macroblock *)(ctx->t.samples_base + mb_offset);
ctx->A4x4_int8_v = (i16x16){0, 0, 2, 2, 1, 4, 3, 6, 8, 8, 10, 10, 9, 12, 11, 14};
ctx->B4x4_int8_v = (i32x16){0, 1, 0, 1, 4, 5, 4, 5, 2, 3, 8, 9, 6, 7, 12, 13};
if (ctx->t.ChromaArrayType == 1) {
ctx->ACbCr_int8_v[0] = (i16x8){0, 0, 2, 2, 4, 4, 6, 6};
ctx->BCbCr_int8_v[0] = (i32x8){0, 1, 0, 1, 4, 5, 4, 5};
}
ctx->QP_C_v[0] = load128(QP_Y2C + 12 + ctx->t.pps.chroma_qp_index_offset);
ctx->QP_C_v[1] = load128(QP_Y2C + 28 + ctx->t.pps.chroma_qp_index_offset);
ctx->QP_C_v[2] = load128(QP_Y2C + 44 + ctx->t.pps.chroma_qp_index_offset);
ctx->QP_C_v[3] = load128(QP_Y2C + 60 + ctx->t.pps.chroma_qp_index_offset);
ctx->QP_C_v[4] = load128(QP_Y2C + 12 + ctx->t.pps.second_chroma_qp_index_offset);
ctx->QP_C_v[5] = load128(QP_Y2C + 28 + ctx->t.pps.second_chroma_qp_index_offset);
ctx->QP_C_v[6] = load128(QP_Y2C + 44 + ctx->t.pps.second_chroma_qp_index_offset);
ctx->QP_C_v[7] = load128(QP_Y2C + 60 + ctx->t.pps.second_chroma_qp_index_offset);
ctx->t.QP[1] = ctx->QP_C[0][ctx->t.QP[0]];
ctx->t.QP[2] = ctx->QP_C[1][ctx->t.QP[0]];
for (int i = 1; i < 4; i++) {
ctx->sig_inc_v[i] = sig_inc_8x8[0][i];
ctx->last_inc_v[i] = last_inc_8x8[i];
ctx->scan_v[i] = scan_8x8_cabac[0][i];
}
// P/B slices
if (ctx->t.slice_type < 2) {
ctx->refIdx4x4_C_v = (i8x16){2, 3, 12, -1, 3, 6, 13, -1, 12, 13, 14, -1, 13, -1, 15, -1};
ctx->absMvd_A_v = (i16x16){0, 0, 4, 4, 2, 8, 6, 12, 16, 16, 20, 20, 18, 24, 22, 28};
ctx->absMvd_B_v = (i32x16){0, 2, 0, 2, 8, 10, 8, 10, 4, 6, 16, 18, 12, 14, 24, 26};
ctx->mvs_A_v = (i16x16){0, 0, 2, 2, 1, 4, 3, 6, 8, 8, 10, 10, 9, 12, 11, 14};
ctx->mvs_B_v = (i32x16){0, 1, 0, 1, 4, 5, 4, 5, 2, 3, 8, 9, 6, 7, 12, 13};
ctx->mvs_C_v = (i32x16){0, 1, 1, -1, 4, 5, 5, -1, 3, 6, 9, -1, 7, -1, 13, -1};
ctx->mvs_D_v = (i32x16){0, 1, 2, 0, 4, 5, 1, 4, 8, 2, 10, 8, 3, 6, 9, 12};
ctx->num_ref_idx_mask = (ctx->t.pps.num_ref_idx_active[0] > 1) * 0x0f + (ctx->t.pps.num_ref_idx_active[1] > 1) * 0xf0;
ctx->transform_8x8_mode_flag = ctx->t.pps.transform_8x8_mode_flag; // for P slices this value is constant
int max0 = ctx->t.pps.num_ref_idx_active[0] - 1;
int max1 = ctx->t.slice_type == 0 ? -1 : ctx->t.pps.num_ref_idx_active[1] - 1;
ctx->clip_ref_idx_v = (i8x8){max0, max0, max0, max0, max1, max1, max1, max1};
// B slides
if (ctx->t.slice_type == 1) {
ctx->mbCol = (Edge264Macroblock *)(ctx->t.frame_buffers[ctx->t.RefPicList[1][0]] + mb_offset);
ctx->col_short_term = 1 & ~(ctx->t.long_term_flags >> ctx->t.RefPicList[1][0]);
// initializations for temporal prediction and implicit weights
int rangeL1 = ctx->t.pps.num_ref_idx_active[1];
if (ctx->t.pps.weighted_bipred_idc == 2 || (rangeL1 = 1, !ctx->t.direct_spatial_mv_pred_flag)) {
tb.v[0] = packs16(ctx->t.diff_poc_v[0], ctx->t.diff_poc_v[1]);
tb.v[1] = packs16(ctx->t.diff_poc_v[2], ctx->t.diff_poc_v[3]);
ctx->MapPicToList0_v[0] = ctx->MapPicToList0_v[1] = (i8x16){}; // FIXME pictures not found in RefPicList0 should point to self
for (int refIdxL0 = ctx->t.pps.num_ref_idx_active[0], DistScaleFactor; refIdxL0-- > 0; ) {
int pic0 = ctx->t.RefPicList[0][refIdxL0];
ctx->MapPicToList0[pic0] = refIdxL0;
i16x8 diff0 = set16(ctx->t.diff_poc[pic0]);
td.v[0] = packs16(diff0 - ctx->t.diff_poc_v[0], diff0 - ctx->t.diff_poc_v[1]);
td.v[1] = packs16(diff0 - ctx->t.diff_poc_v[2], diff0 - ctx->t.diff_poc_v[3]);
for (int refIdxL1 = rangeL1, implicit_weight; refIdxL1-- > 0; ) {
int pic1 = ctx->t.RefPicList[1][refIdxL1];
if (td.q[pic1] != 0 && !(ctx->t.long_term_flags & 1 << pic0)) {
int tx = (16384 + abs(td.q[pic1] / 2)) / td.q[pic1];
DistScaleFactor = min(max((tb.q[pic0] * tx + 32) >> 6, -1024), 1023);
implicit_weight = (!(ctx->t.long_term_flags & 1 << pic1) && DistScaleFactor >= -256 && DistScaleFactor <= 515) ? DistScaleFactor >> 2 : 32;
} else {
DistScaleFactor = 256;
implicit_weight = 32;
}
ctx->implicit_weights[refIdxL0][refIdxL1] = implicit_weight + 64;
}
ctx->DistScaleFactor[refIdxL0] = DistScaleFactor;
}
}
}
}
}
/**
* Helper function to raise a probability sampled to 0..65535 to a power k.
*/
static unsigned ppow(unsigned p65536, unsigned k) {
unsigned r = 65536;
while (k) {
if (k & 1)
r = (r * p65536) >> 16;
p65536 = (p65536 * p65536) >> 16;
k >>= 1;
}
return r;
}
/**
* If the slice ends on error, invalidate all its mbs and recover them.
*
* For CAVLC the error is equiprobable in all of the slice mbs.
* For CABAC every erroneous mb had a random probability p=2/383 to exit
* early at end_of_slice_flag, so for each mb we only count a proportion
* that reached CurrMbAddr without early exit: (1-p)^d, d being the
* distance to the last decoded mb. We sum these proportions to normalize
* the probabilities: (1-(1-p)^n)/p. Then we compute each probability as
* the normalized sum of its proportion and all proportions before it:
* 1-(1-(1-p)^d)/(1-(1-p)^n). Note that p is sampled to 16-bits int to
* avoid dependency on float and to fit all multiplications on 32 bits
* with max precision.
*/
static void recover_slice(Edge264Context *ctx, int currPic) {
// mark all previous mbs as erroneous and assign them an error probability
ctx->mby = (unsigned)ctx->t.first_mb_in_slice / (unsigned)ctx->t.pic_width_in_mbs;
ctx->mbx = (unsigned)ctx->t.first_mb_in_slice % (unsigned)ctx->t.pic_width_in_mbs;
ctx->samples_mb[0] = ctx->t.samples_base + (ctx->mbx + ctx->mby * ctx->t.stride[0]) * 16;
ctx->samples_mb[1] = ctx->t.samples_base + ctx->t.plane_size_Y + (ctx->mbx + ctx->mby * ctx->t.stride[1]) * 8;
ctx->samples_mb[2] = ctx->samples_mb[1] + (ctx->t.stride[1] >> 1);
int mb_offset = ctx->t.plane_size_Y + ctx->t.plane_size_C + sizeof(Edge264Macroblock) * (ctx->mbx + ctx->mby * (ctx->t.pic_width_in_mbs + 1));
ctx->_mb = (Edge264Macroblock *)(ctx->t.samples_base + mb_offset);
ctx->mbCol = (Edge264Macroblock *)(ctx->t.frame_buffers[ctx->t.RefPicList[1][0]] + mb_offset);
unsigned num = ctx->CurrMbAddr - ctx->t.first_mb_in_slice;
unsigned div = 65536 - ppow(65194, num);
for (unsigned i = 0; i < num; i++) {
unsigned p12800 = (!ctx->t.pps.entropy_coding_mode_flag) ?
((i + 1) * 12800 + num - 1) / num : // division with upward rounding
((div - (65536 - ppow(65194, num - 1 - i))) * 12800 + div - 1) / div;
ctx->_mb->error_probability = p12800 >> 7;
unsigned p128 = p12800 / 100;
// recover the macroblock depending on slice_type
if (ctx->t.slice_type == 2) { // I slice -> blend with intra DC
uint8_t * restrict p = ctx->samples_mb[0];
size_t stride = ctx->t.stride[0];
INIT_P();
i8x16 l = set8(-128), t = l;
if (i == 0 || ctx->mbx == 0) { // A not available
if (i >= ctx->t.pic_width_in_mbs) // B available
l = t = load128(P(0, -1));
} else { // A available
l = t = ldleft16(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
if (i >= ctx->t.pic_width_in_mbs) // B available
t = load128(P(0, -1));
}
i8x16 dcY = broadcast8(shrru16(sumd8(t, l), 5), __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__);
i8x16 w8 = ziplo8(set8(128 - p128), set8(p128));
i16x8 w16 = {128 - p128, p128};
i16x8 o = {};
i64x2 wd64 = {7};
i16x8 wd16 = set16(7);
if (p128 == 128) {
w8 = (i8x16){0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1};
w16 = (i16x8){0, 1};
wd64 = wd16 = o;
}
*(i8x16 *)P(0, 0) = maddshr8(*(i8x16 *)P(0, 0), dcY, w8, w16, o, wd64, wd16);
*(i8x16 *)P(0, 1) = maddshr8(*(i8x16 *)P(0, 1), dcY, w8, w16, o, wd64, wd16);
*(i8x16 *)P(0, 2) = maddshr8(*(i8x16 *)P(0, 2), dcY, w8, w16, o, wd64, wd16);
*(i8x16 *)P(0, 3) = maddshr8(*(i8x16 *)P(0, 3), dcY, w8, w16, o, wd64, wd16);
*(i8x16 *)P(0, 4) = maddshr8(*(i8x16 *)P(0, 4), dcY, w8, w16, o, wd64, wd16);
*(i8x16 *)P(0, 5) = maddshr8(*(i8x16 *)P(0, 5), dcY, w8, w16, o, wd64, wd16);
*(i8x16 *)P(0, 6) = maddshr8(*(i8x16 *)P(0, 6), dcY, w8, w16, o, wd64, wd16);
*(i8x16 *)P(0, 7) = maddshr8(*(i8x16 *)P(0, 7), dcY, w8, w16, o, wd64, wd16);
*(i8x16 *)P(0, 8) = maddshr8(*(i8x16 *)P(0, 8), dcY, w8, w16, o, wd64, wd16);
*(i8x16 *)P(0, 9) = maddshr8(*(i8x16 *)P(0, 9), dcY, w8, w16, o, wd64, wd16);
*(i8x16 *)P(0, 10) = maddshr8(*(i8x16 *)P(0, 10), dcY, w8, w16, o, wd64, wd16);
*(i8x16 *)P(0, 11) = maddshr8(*(i8x16 *)P(0, 11), dcY, w8, w16, o, wd64, wd16);
*(i8x16 *)P(0, 12) = maddshr8(*(i8x16 *)P(0, 12), dcY, w8, w16, o, wd64, wd16);
*(i8x16 *)P(0, 13) = maddshr8(*(i8x16 *)P(0, 13), dcY, w8, w16, o, wd64, wd16);
*(i8x16 *)P(0, 14) = maddshr8(*(i8x16 *)P(0, 14), dcY, w8, w16, o, wd64, wd16);
*(i8x16 *)P(0, 15) = maddshr8(*(i8x16 *)P(0, 15), dcY, w8, w16, o, wd64, wd16);
{
uint8_t * restrict p = ctx->samples_mb[1];
size_t stride = ctx->t.stride[1] >> 1;
INIT_P();
i8x16 b = ziplo64(load64(P(0, -2)), ldleft8(0, 2, 4, 6, 8, 10, 12, 14));
i8x16 r = ziplo64(load64(P(0, -1)), ldleft8(1, 3, 5, 7, 9, 11, 13, 15));
i8x16 dcb = broadcast8(shrru16(sum8(b), 4), __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__);
i8x16 dcr = broadcast8(shrru16(sum8(r), 4), __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__);
i8x16 dcC = ziplo64(dcb, dcr);
i64x2 v0 = maddshr8(load8x2(P(0, 0), P(0, 1)), dcC, w8, w16, o, wd64, wd16);
i64x2 v1 = maddshr8(load8x2(P(0, 2), P(0, 3)), dcC, w8, w16, o, wd64, wd16);
i64x2 v2 = maddshr8(load8x2(P(0, 4), P(0, 5)), dcC, w8, w16, o, wd64, wd16);
i64x2 v3 = maddshr8(load8x2(P(0, 6), P(0, 7)), dcC, w8, w16, o, wd64, wd16);
i64x2 v4 = maddshr8(load8x2(P(0, 8), P(0, 9)), dcC, w8, w16, o, wd64, wd16);
i64x2 v5 = maddshr8(load8x2(P(0, 10), P(0, 11)), dcC, w8, w16, o, wd64, wd16);
i64x2 v6 = maddshr8(load8x2(P(0, 12), P(0, 13)), dcC, w8, w16, o, wd64, wd16);
i64x2 v7 = maddshr8(load8x2(P(0, 14), P(0, 15)), dcC, w8, w16, o, wd64, wd16);
*(int64_t *)P(0, 0) = v0[0];
*(int64_t *)P(0, 1) = v0[1];
*(int64_t *)P(0, 2) = v1[0];
*(int64_t *)P(0, 3) = v1[1];
*(int64_t *)P(0, 4) = v2[0];
*(int64_t *)P(0, 5) = v2[1];
*(int64_t *)P(0, 6) = v3[0];
*(int64_t *)P(0, 7) = v3[1];
*(int64_t *)P(0, 8) = v4[0];
*(int64_t *)P(0, 9) = v4[1];
*(int64_t *)P(0, 10) = v5[0];
*(int64_t *)P(0, 11) = v5[1];
*(int64_t *)P(0, 12) = v6[0];
*(int64_t *)P(0, 13) = v6[1];
*(int64_t *)P(0, 14) = v7[0];
*(int64_t *)P(0, 15) = v7[1];
}
} else if (i > 0 && p128 >= 32) { // recover above 25% error (arbitrary)
if (ctx->t.slice_type == 0) { // P slice -> P_Skip
mb->nC_v[0] = (i8x16){};
decode_P_skip(ctx);
} else { // B slice -> B_Skip
mb->nC_v[0] = (i8x16){};
decode_direct_mv_pred(ctx, 0xffffffff);
}
}
__atomic_store_n(&ctx->_mb->recovery_bits, ctx->t.frame_flip_bit + 2, __ATOMIC_RELEASE);
// point to the next macroblock
ctx->_mb++;
ctx->mbx++;
ctx->mbCol++;
ctx->samples_mb[0] += 16;
ctx->samples_mb[1] += 8;
ctx->samples_mb[2] += 8;
if (ctx->mbx >= ctx->t.pic_width_in_mbs) {
ctx->_mb++;
ctx->mbx = 0;
ctx->mby++;
ctx->mbCol++;
ctx->samples_mb[0] += ctx->t.stride[0] * 16 - ctx->t.pic_width_in_mbs * 16;
ctx->samples_mb[1] += ctx->t.stride[1] * 8 - ctx->t.pic_width_in_mbs * 8;
ctx->samples_mb[2] += ctx->t.stride[1] * 8 - ctx->t.pic_width_in_mbs * 8;
}
}
}
/**
* This function is called when a frame ends with a positive remaining_mbs.
*
* It sets up recover_slice to go through all mbs and recover them while
* setting their error probability to 100%.
*/
static void recover_frame(Edge264Decoder *dec) {
}
/**
* This function is the entry point for each worker thread, where it consumes
* tasks continuously until killed by the parent process.
*/
void *ADD_VARIANT(worker_loop)(Edge264Decoder *dec) {
Edge264Context c;
c.d = dec;
c.n_threads = dec->n_threads;
c.trace_slices = dec->trace_slices;
if (c.n_threads)
pthread_mutex_lock(&dec->lock);
for (;;) {
while (c.n_threads && !dec->ready_tasks)
pthread_cond_wait(&dec->task_ready, &dec->lock);
int task_id = __builtin_ctz(dec->ready_tasks); // FIXME arbitrary selection for now
int currPic = dec->taskPics[task_id];
dec->pending_tasks &= ~(1 << task_id);
dec->ready_tasks &= ~(1 << task_id);
if (c.n_threads) {
pthread_mutex_unlock(&dec->lock);
print_header(dec, "<h>Thread started decoding frame %d at macroblock %d</h>\n", dec->FieldOrderCnt[0][dec->taskPics[task_id]], dec->tasks[task_id].first_mb_in_slice);
}
c.t = dec->tasks[task_id];
initialize_context(&c, currPic);
size_t ret = 0;
if (!c.t.pps.entropy_coding_mode_flag) {
c.mb_skip_run = -1;
parse_slice_data_cavlc(&c);
// FIXME detect and signal error
} else {
// cabac_alignment_one_bit gives a good probability to catch random errors.
if (cabac_start(&c)) {
ret = EBADMSG; // FIXME error_flag
} else {
cabac_init(&c);
c.mb_qp_delta_nz = 0;
parse_slice_data_cabac(&c);
// the possibility of cabac_zero_word implies we should not expect a start code yet
if (c.t._gb.msb_cache != 0 || (c.t._gb.lsb_cache & (c.t._gb.lsb_cache - 1))) {
ret = EBADMSG; // FIXME error_flag
}
}
}
// deblock the rest of mbs in this slice
if (c.t.next_deblock_addr >= 0) {
c.t.next_deblock_addr = max(c.t.next_deblock_addr, c.t.first_mb_in_slice);
c.mby = (unsigned)c.t.next_deblock_addr / (unsigned)c.t.pic_width_in_mbs;
c.mbx = (unsigned)c.t.next_deblock_addr % (unsigned)c.t.pic_width_in_mbs;
c.samples_mb[0] = c.t.samples_base + (c.mbx + c.mby * c.t.stride[0]) * 16;
c.samples_mb[1] = c.t.samples_base + c.t.plane_size_Y + (c.mbx + c.mby * c.t.stride[1]) * 8;
c.samples_mb[2] = c.samples_mb[1] + (c.t.stride[1] >> 1);
c._mb = (Edge264Macroblock *)(c.t.samples_base + c.t.plane_size_Y + c.t.plane_size_C) + c.mbx + c.mby * (c.t.pic_width_in_mbs + 1);
while (c.t.next_deblock_addr < c.CurrMbAddr) {
deblock_mb(&c);
c.t.next_deblock_addr++;
c._mb++;
c.mbx++;
c.samples_mb[0] += 16;
c.samples_mb[1] += 8;
c.samples_mb[2] += 8;
if (c.mbx >= c.t.pic_width_in_mbs) {
c._mb++;
c.mbx = 0;
c.samples_mb[0] += c.t.stride[0] * 16 - c.t.pic_width_in_mbs * 16;
c.samples_mb[1] += c.t.stride[1] * 8 - c.t.pic_width_in_mbs * 8;
c.samples_mb[2] += c.t.stride[1] * 8 - c.t.pic_width_in_mbs * 8;
}
}
}
// on error, recover mbs and signal them as erroneous (allows overwrite by redundant slices)
if (__builtin_expect(ret != 0, 0))
recover_slice(&c, currPic);
// update dec->next_deblock_addr, considering it might have reached first_mb_in_slice since start
if (dec->next_deblock_addr[currPic] >= c.t.first_mb_in_slice &&
!(c.t.disable_deblocking_filter_idc == 0 && c.t.next_deblock_addr < 0)) {
dec->next_deblock_addr[currPic] = c.CurrMbAddr;
pthread_cond_broadcast(&dec->task_progress);
}
// deblock the rest of the frame if all mbs have been decoded correctly
int remaining_mbs = ret ?: __atomic_sub_fetch(&dec->remaining_mbs[currPic], c.CurrMbAddr - c.t.first_mb_in_slice, __ATOMIC_ACQ_REL);
if (remaining_mbs == 0) {
c.t.next_deblock_addr = dec->next_deblock_addr[currPic];
c.CurrMbAddr = c.t.pic_width_in_mbs * c.t.pic_height_in_mbs;
if ((unsigned)c.t.next_deblock_addr < c.CurrMbAddr) {
c.mby = (unsigned)c.t.next_deblock_addr / (unsigned)c.t.pic_width_in_mbs;
c.mbx = (unsigned)c.t.next_deblock_addr % (unsigned)c.t.pic_width_in_mbs;
c.samples_mb[0] = c.t.samples_base + (c.mbx + c.mby * c.t.stride[0]) * 16;
c.samples_mb[1] = c.t.samples_base + c.t.plane_size_Y + (c.mbx + c.mby * c.t.stride[1]) * 8;
c.samples_mb[2] = c.samples_mb[1] + (c.t.stride[1] >> 1);
c._mb = (Edge264Macroblock *)(c.t.samples_base + c.t.plane_size_Y + c.t.plane_size_C) + c.mbx + c.mby * (c.t.pic_width_in_mbs + 1);
while (c.t.next_deblock_addr < c.CurrMbAddr) {
deblock_mb(&c);
c.t.next_deblock_addr++;
c._mb++;
c.mbx++;
c.samples_mb[0] += 16;
c.samples_mb[1] += 8;
c.samples_mb[2] += 8;
if (c.mbx >= c.t.pic_width_in_mbs) {
c._mb++;
c.mbx = 0;
c.samples_mb[0] += c.t.stride[0] * 16 - c.t.pic_width_in_mbs * 16;
c.samples_mb[1] += c.t.stride[1] * 8 - c.t.pic_width_in_mbs * 8;
c.samples_mb[2] += c.t.stride[1] * 8 - c.t.pic_width_in_mbs * 8;
}
}
}
dec->next_deblock_addr[currPic] = INT_MAX; // signals the frame is complete
}
// if multi-threaded, check if we are the last task to touch this frame and ensure it is complete
if (c.n_threads) {
pthread_mutex_lock(&dec->lock);
pthread_cond_signal(&dec->task_complete);
print_header(dec, "<h>Thread finished decoding frame %d at macroblock %d</h>\n", dec->FieldOrderCnt[0][currPic], c.t.first_mb_in_slice);
if (remaining_mbs == 0) {
pthread_cond_broadcast(&dec->task_progress);
dec->ready_tasks = ready_tasks(dec);
if (dec->ready_tasks)
pthread_cond_broadcast(&dec->task_ready);
}
}
if (c.t.free_cb)
c.t.free_cb(c.t.free_arg, (int)ret);
dec->busy_tasks &= ~(1 << task_id);
dec->task_dependencies[task_id] = 0;
dec->taskPics[task_id] = -1;
// in single-thread mode update the buffer pointer and return
if (!c.n_threads) {
dec->_gb = c.t._gb;
return (void *)ret;
}
}
return NULL;
}
/**
* Updates the reference flags by adaptive memory control or sliding window
* marking process (8.2.5).
*/
static void parse_dec_ref_pic_marking(Edge264Decoder *dec)
{
static const char * const memory_management_control_operation_names[6] = {
"%s1 (dereference frame %u)",
"%s2 (dereference long-term frame %3$u)",
"%s3 (convert frame %u into long-term index %u)",
"%s4 (dereference long-term frames on and above %3$d)",
"%s5 (convert current picture to IDR and dereference all frames)",
"%s6 (assign long-term index %3$u to current picture)"};
// while the exact release time of non-ref frames in C.4.5.2 is ambiguous, we ignore no_output_of_prior_pics_flag
if (dec->IdrPicFlag) {
int no_output_of_prior_pics_flag = get_u1(&dec->_gb);
dec->pic_reference_flags = 1 << dec->currPic;
dec->pic_long_term_flags = get_u1(&dec->_gb) << dec->currPic;
dec->pic_LongTermFrameIdx_v[0] = dec->pic_LongTermFrameIdx_v[1] = (i8x16){};
print_header(dec, "<k>no_output_of_prior_pics_flag</k><v>%x</v>\n"
"<k>long_term_reference_flag</k><v>%x</v>\n",
no_output_of_prior_pics_flag,
dec->pic_long_term_flags >> dec->currPic);
return;
}
// 8.2.5.4 - Adaptive memory control marking process.
int memory_management_control_operation;
int i = 32;
if (get_u1(&dec->_gb)) {
while ((memory_management_control_operation = get_ue16(&dec->_gb, 6)) != 0 && i-- > 0) {
int target = dec->currPic, long_term_frame_idx = 0;
if (10 & 1 << memory_management_control_operation) { // 1 or 3
int FrameNum = dec->FrameNum - 1 - get_ue32(&dec->_gb, 4294967294);
for (unsigned r = dec->pic_reference_flags & ~dec->pic_long_term_flags; r; r &= r - 1) {
int j = __builtin_ctz(r);
if (dec->FrameNums[j] == FrameNum) {
target = j;
if (memory_management_control_operation == 1) {
dec->pic_reference_flags ^= 1 << j;
dec->pic_long_term_flags &= ~(1 << j);
}
}
}
}
if (92 & 1 << memory_management_control_operation) { // 2 or 3 or 4 or 6
long_term_frame_idx = get_ue16(&dec->_gb, (dec->sps.max_num_ref_frames >> dec->sps.mvc) - (memory_management_control_operation != 4));
for (unsigned r = dec->pic_long_term_flags; r; r &= r - 1) {
int j = __builtin_ctz(r);
if (dec->pic_LongTermFrameIdx[j] == long_term_frame_idx ||
(dec->pic_LongTermFrameIdx[j] > long_term_frame_idx &&
memory_management_control_operation == 4)) {
dec->pic_reference_flags ^= 1 << j;
dec->pic_long_term_flags ^= 1 << j;
}
}
if (72 & 1 << memory_management_control_operation) { // 3 or 6
dec->pic_LongTermFrameIdx[target] = long_term_frame_idx;
dec->pic_long_term_flags |= 1 << target;
}
}
if (memory_management_control_operation == 5) {
dec->IdrPicFlag = 1;
dec->pic_reference_flags = 0;
dec->FrameNums[dec->currPic] = 0;
dec->pic_long_term_flags = 0;
dec->pic_LongTermFrameIdx_v[0] = dec->pic_LongTermFrameIdx_v[1] = (i8x16){};
int tempPicOrderCnt = min(dec->TopFieldOrderCnt, dec->BottomFieldOrderCnt);
dec->FieldOrderCnt[0][dec->currPic] = dec->TopFieldOrderCnt - tempPicOrderCnt;
dec->FieldOrderCnt[1][dec->currPic] = dec->BottomFieldOrderCnt - tempPicOrderCnt;
}
print_header(dec, memory_management_control_operation_names[memory_management_control_operation - 1],
(i == 31) ? "<k>memory_management_control_operations</k><v>" : "<br>", dec->FrameNums[target], long_term_frame_idx);
}
print_header(dec, "</v>\n");
}
// 8.2.5.3 - Sliding window marking process
if (__builtin_popcount(dec->pic_reference_flags) >= (dec->sps.max_num_ref_frames >> dec->sps.mvc)) {
int best = INT_MAX;
int next = 0;
for (unsigned r = dec->pic_reference_flags ^ dec->pic_long_term_flags; r != 0; r &= r - 1) {
int i = __builtin_ctz(r);
if (best > dec->FrameNums[i])
best = dec->FrameNums[next = i];
}
dec->pic_reference_flags ^= 1 << next;
}
dec->pic_reference_flags |= 1 << dec->currPic;
}
/**
* Parses coefficients for weighted sample prediction (7.4.3.2 and 8.4.2.3).
*/
static void parse_pred_weight_table(Edge264Decoder *dec, Edge264Task *t)
{
// further tests will depend only on weighted_bipred_idc
if (t->slice_type == 0)
t->pps.weighted_bipred_idc = t->pps.weighted_pred_flag;
// parse explicit weights/offsets
if (t->pps.weighted_bipred_idc == 1) {
t->luma_log2_weight_denom = get_ue16(&dec->_gb, 7);
if (dec->sps.ChromaArrayType != 0)
t->chroma_log2_weight_denom = get_ue16(&dec->_gb, 7);
for (int l = 0; l <= t->slice_type; l++) {
print_header(dec, "<k>Prediction weights L%x (weight/offset)</k><v>", l);
for (int i = l * 32; i < l * 32 + t->pps.num_ref_idx_active[l]; i++) {
if (get_u1(&dec->_gb)) {
t->explicit_weights[0][i] = get_se16(&dec->_gb, -128, 127);
t->explicit_offsets[0][i] = get_se16(&dec->_gb, -128, 127);
} else {
t->explicit_weights[0][i] = 1 << t->luma_log2_weight_denom;
t->explicit_offsets[0][i] = 0;
}
if (dec->sps.ChromaArrayType != 0 && get_u1(&dec->_gb)) {
t->explicit_weights[1][i] = get_se16(&dec->_gb, -128, 127);
t->explicit_offsets[1][i] = get_se16(&dec->_gb, -128, 127);
t->explicit_weights[2][i] = get_se16(&dec->_gb, -128, 127);
t->explicit_offsets[2][i] = get_se16(&dec->_gb, -128, 127);
} else {
t->explicit_weights[1][i] = 1 << t->chroma_log2_weight_denom;
t->explicit_offsets[1][i] = 0;
t->explicit_weights[2][i] = 1 << t->chroma_log2_weight_denom;
t->explicit_offsets[2][i] = 0;
}
print_header(dec, (dec->sps.ChromaArrayType == 0) ? "*%d/%u+%d" : "*%d/%u+%d : *%d/%u+%d : *%d/%u+%d",
t->explicit_weights[0][i], 1 << t->luma_log2_weight_denom, t->explicit_offsets[0][i] << (dec->sps.BitDepth_Y - 8),
t->explicit_weights[1][i], 1 << t->chroma_log2_weight_denom, t->explicit_offsets[1][i] << (dec->sps.BitDepth_C - 8),
t->explicit_weights[2][i], 1 << t->chroma_log2_weight_denom, t->explicit_offsets[2][i] << (dec->sps.BitDepth_C - 8));
print_header(dec, (i < t->pps.num_ref_idx_active[l] - 1) ? "<br>" : "</v>\n");
}
}
}
}
/**
* Initialises and updates the reference picture lists (8.2.4).
*
* Both initialisation and parsing of ref_pic_list_modification are fit into a
* single function to foster compactness and maintenance. Performance is not
* crucial here.
*/
static void parse_ref_pic_list_modification(Edge264Decoder *dec, Edge264Task *t)
{
// For P we sort on FrameNum, for B we sort on PicOrderCnt.
const int32_t *values = (t->slice_type == 0) ? dec->FrameNums : dec->FieldOrderCnt[0];
int pic_value = (t->slice_type == 0) ? dec->FrameNum : dec->TopFieldOrderCnt;
int count[3] = {0, 0, 0}; // number of refs before/after/long
int size = 0;
// sort all short and long term references for RefPicListL0
for (unsigned refs = dec->pic_reference_flags, next = 0; refs; refs ^= 1 << next) {
int best = INT_MAX;
for (unsigned r = refs; r; r &= r - 1) {
int i = __builtin_ctz(r);
int diff = values[i] - pic_value;
int ShortTermNum = (diff <= 0) ? -diff : 0x10000 + diff;
int LongTermNum = dec->LongTermFrameIdx[i] + 0x20000;
int v = (dec->pic_long_term_flags & 1 << i) ? LongTermNum : ShortTermNum;
if (v < best)
best = v, next = i;
}
t->RefPicList[0][size++] = next;
count[best >> 16]++;
}
if (dec->basePic >= 0)
t->RefPicList[0][size++] = dec->basePic; // add inter-view ref for MVC
// fill RefPicListL1 by swapping before/after references
for (int src = 0; src < size; src++) {
int dst = (src < count[0]) ? src + count[1] :
(src < count[0] + count[1]) ? src - count[0] : src;
t->RefPicList[1][dst] = t->RefPicList[0][src];
}
// When decoding a field, extract a list of fields from each list of frames.
/*union { int8_t q[32]; i8x16 v[2]; } RefFrameList;
for (int l = 0; t->field_pic_flag && l <= t->slice_type; l++) {
i8x16 v = t->RefPicList_v[l * 2];
RefFrameList.v[0] = v;
RefFrameList.v[1] = v + (i8x16){16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16};
size = 0;
int i = t->bottom_field_flag << 4; // first parity to check
int j = i ^ 16; // other parity to alternate
int lim_i = i + count[0] + count[1]; // set a first limit to short term frames
int lim_j = j + count[0] + count[1]; // don't init with XOR as there can be 16 refs!
int tot = count[0] + count[1] + count[2]; // ... then long term
// probably not the most readable portion, yet otherwise needs a lot of code
for (int k;;) {
if (i >= lim_i) {
if (j < lim_j) { // i reached limit but not j, swap them
k = i, i = j, j = k;
k = lim_i, lim_i = lim_j, lim_j = k;
} else if (min(lim_i, lim_j) < tot) { // end of short term refs, go for long
int parity = t->bottom_field_flag << 4;
i = (t->bottom_field_flag << 4) + count[0] + count[1];
j = i ^ 16;
lim_i = i + count[2];
lim_j = j + count[2];
} else break; // end of long term refs, break
}
int pic = RefFrameList.q[i++];
if (dec->reference_flags & 1 << pic) {
t->RefPicList[l][size++] = pic;
if (j < lim_j) { // swap parity if we have not emptied other parity yet
k = i, i = j, j = k;
k = lim_i, lim_i = lim_j, lim_j = k;
}
}
}
}*/
// Swap the two first slots of RefPicListL1 if it the same as RefPicListL0.
if (t->RefPicList[0][1] >= 0 && t->RefPicList[0][0] == t->RefPicList[1][0]) {
t->RefPicList[1][0] = t->RefPicList[0][1];
t->RefPicList[1][1] = t->RefPicList[0][0];
}
// parse the ref_pic_list_modification() header
for (int l = 0; l <= t->slice_type; l++) {
unsigned picNumLX = (t->field_pic_flag) ? dec->FrameNum * 2 + 1 : dec->FrameNum;
if (get_u1(&dec->_gb)) { // ref_pic_list_modification_flag
print_header(dec, "<k>ref_pic_list_modifications_l%x</k><v>", l);
for (int refIdx = 0, modification_of_pic_nums_idc; (modification_of_pic_nums_idc = get_ue16(&dec->_gb, 5)) != 3 && refIdx < 32; refIdx++) {
int num = get_ue32(&dec->_gb, 4294967294);
print_header(dec, "%s%d%s", refIdx ? ", " : "",
modification_of_pic_nums_idc % 4 == 0 ? -num - 1 : num + (modification_of_pic_nums_idc != 2),
modification_of_pic_nums_idc == 2 ? "l" : modification_of_pic_nums_idc > 3 ? "v" : "");
int pic = dec->basePic;
if (modification_of_pic_nums_idc < 2) {
picNumLX = (modification_of_pic_nums_idc == 0) ? picNumLX - (num + 1) : picNumLX + (num + 1);
unsigned MaskFrameNum = (1 << dec->sps.log2_max_frame_num) - 1;
for (unsigned r = dec->pic_reference_flags & ~dec->pic_long_term_flags; r; r &= r - 1) {
pic = __builtin_ctz(r);
if (!((dec->FrameNums[pic] ^ picNumLX) & MaskFrameNum))
break;
}
} else if (modification_of_pic_nums_idc == 2) {
for (unsigned r = dec->pic_reference_flags & dec->pic_long_term_flags; r; r &= r - 1) {
pic = __builtin_ctz(r);
if (dec->LongTermFrameIdx[pic] == num)
break;
}
}
int buf = pic;
int cIdx = refIdx;
do {
int swap = t->RefPicList[l][cIdx];
t->RefPicList[l][cIdx] = buf;
buf = swap;
} while (++cIdx < t->pps.num_ref_idx_active[l] && buf != pic);
}
print_header(dec, "</v>\n");
}
}
#ifdef TRACE
print_header(dec, "<k>RefPicLists</k><v>");
for (int lx = 0; lx <= t->slice_type; lx++) {
for (int i = 0; i < t->pps.num_ref_idx_active[lx]; i++)
print_header(dec, "%d%s", t->RefPicList[lx][i], (i < t->pps.num_ref_idx_active[lx] - 1) ? ", " : (t->slice_type - lx == 1) ? "<br>" : "");
}
print_header(dec, "</v>\n");
#endif
}
static int alloc_frame(Edge264Decoder *dec, int id) {
dec->frame_buffers[id] = malloc(dec->frame_size);
if (dec->frame_buffers[id] == NULL)
return ENOMEM;
Edge264Macroblock *m = (Edge264Macroblock *)(dec->frame_buffers[id] + dec->plane_size_Y + dec->plane_size_C);
int mbs = (dec->sps.pic_width_in_mbs + 1) * dec->sps.pic_height_in_mbs - 1;
for (int i = 0; i < mbs; i += dec->sps.pic_width_in_mbs + 1) {
for (int j = i; j < i + dec->sps.pic_width_in_mbs; j++)
m[j].recovery_bits = 0;
if (i + dec->sps.pic_width_in_mbs < mbs)
m[i + dec->sps.pic_width_in_mbs] = unavail_mb;
}
return 0;
}
/**
* This function applies the updates required for the next picture. It is
* called when a slice is received with a different frame_num/POC/view_id.
* pair_view is set if the picture differs only by view_id.
*
* The test on POC alone is not sufficient without frame_num, because the
* correct POC value depends on FrameNum which needs an up-to-date PrevFrameNum.
*/
static void finish_frame(Edge264Decoder *dec, int pair_view) {
if (dec->pic_reference_flags & 1 << dec->currPic)
dec->prevPicOrderCnt = dec->FieldOrderCnt[0][dec->currPic];
int non_base_view = dec->sps.mvc & dec->currPic & 1;
unsigned other_views = -dec->sps.mvc & 0xaaaaaaaa >> non_base_view;
dec->reference_flags = (dec->reference_flags & other_views) | dec->pic_reference_flags;
dec->long_term_flags = (dec->long_term_flags & other_views) | dec->pic_long_term_flags;
dec->LongTermFrameIdx_v[0] = dec->pic_LongTermFrameIdx_v[0];
dec->LongTermFrameIdx_v[1] = dec->pic_LongTermFrameIdx_v[1];
dec->prevRefFrameNum[non_base_view] = dec->FrameNums[dec->currPic]; // for mmco5
dec->basePic = (dec->sps.mvc & ~non_base_view & pair_view) ? dec->currPic : -1;
dec->currPic = -1;
}
/**
* This fonction copies the last set of fields to finish initializing the task.
*/
static void initialize_task(Edge264Decoder *dec, Edge264Task *t)
{
// set task pointer to current pointer and current pointer to next start code
t->_gb = dec->_gb;
if (dec->n_threads) {
t->_gb.end = edge264_find_start_code(dec->_gb.CPB - 2, dec->_gb.end); // works if CPB already crossed end
dec->_gb.CPB = t->_gb.end + 2;
}
// copy most essential fields from st
t->ChromaArrayType = dec->sps.ChromaArrayType;
t->direct_8x8_inference_flag = dec->sps.direct_8x8_inference_flag;
t->frame_flip_bit = dec->frame_flip_bits >> dec->currPic & 1;
t->pic_width_in_mbs = dec->sps.pic_width_in_mbs;
t->pic_height_in_mbs = dec->sps.pic_height_in_mbs;
t->stride[0] = dec->out.stride_Y;
t->stride[1] = t->stride[2] = dec->out.stride_C;
t->plane_size_Y = dec->plane_size_Y;
t->plane_size_C = dec->plane_size_C;
t->next_deblock_idc = (dec->next_deblock_addr[dec->currPic] == t->first_mb_in_slice &&
dec->nal_ref_idc) ? dec->currPic : -1;
t->next_deblock_addr = (dec->next_deblock_addr[dec->currPic] == t->first_mb_in_slice ||
t->disable_deblocking_filter_idc == 2) ? t->first_mb_in_slice : INT_MIN;
t->long_term_flags = dec->long_term_flags;
t->samples_base = dec->frame_buffers[dec->currPic];
t->samples_clip_v[0] = set16((1 << dec->sps.BitDepth_Y) - 1);
t->samples_clip_v[1] = t->samples_clip_v[2] = set16((1 << dec->sps.BitDepth_C) - 1);
// P/B slices
if (t->slice_type < 2) {
memcpy(t->frame_buffers, dec->frame_buffers, sizeof(t->frame_buffers));
if (t->slice_type == 1 && (t->pps.weighted_bipred_idc == 2 || !t->direct_spatial_mv_pred_flag)) {
i32x4 poc = set32(min(dec->TopFieldOrderCnt, dec->BottomFieldOrderCnt));
t->diff_poc_v[0] = packs32(poc - min32(dec->FieldOrderCnt_v[0][0], dec->FieldOrderCnt_v[1][0]),
poc - min32(dec->FieldOrderCnt_v[0][1], dec->FieldOrderCnt_v[1][1]));
t->diff_poc_v[1] = packs32(poc - min32(dec->FieldOrderCnt_v[0][2], dec->FieldOrderCnt_v[1][2]),
poc - min32(dec->FieldOrderCnt_v[0][3], dec->FieldOrderCnt_v[1][3]));
t->diff_poc_v[2] = packs32(poc - min32(dec->FieldOrderCnt_v[0][4], dec->FieldOrderCnt_v[1][4]),
poc - min32(dec->FieldOrderCnt_v[0][5], dec->FieldOrderCnt_v[1][5]));
t->diff_poc_v[3] = packs32(poc - min32(dec->FieldOrderCnt_v[0][6], dec->FieldOrderCnt_v[1][6]),
poc - min32(dec->FieldOrderCnt_v[0][7], dec->FieldOrderCnt_v[1][7]));
}
}
}
/**
* This function matches slice_header() in 7.3.3, which it parses while updating
* the DPB and initialising slice data for further decoding.
*/
int ADD_VARIANT(parse_slice_layer_without_partitioning)(Edge264Decoder *dec, int non_blocking, void(*free_cb)(void*,int), void *free_arg)
{
static const char * const slice_type_names[5] = {"P", "B", "I", "SP", "SI"};
static const char * const disable_deblocking_filter_idc_names[3] = {"enabled", "disabled", "disabled across slices"};
// reserving a slot without locking is fine since workers can only unset busy_tasks
unsigned avail_tasks;
while (!(avail_tasks = 0xffff & ~dec->busy_tasks)) {
if (non_blocking)
return EWOULDBLOCK;
pthread_cond_wait(&dec->task_complete, &dec->lock);
}
Edge264Task *t = dec->tasks + __builtin_ctz(avail_tasks);
t->free_cb = free_cb;
t->free_arg = free_arg;
// first important fields and checks before decoding the slice header
if (!dec->_gb.lsb_cache)
refill(&dec->_gb, 0);
t->first_mb_in_slice = get_ue32(&dec->_gb, 139263);
int slice_type = get_ue16(&dec->_gb, 9);
t->slice_type = (slice_type < 5) ? slice_type : slice_type - 5;
int pic_parameter_set_id = get_ue16(&dec->_gb, 255);
print_header(dec, "<k>first_mb_in_slice</k><v>%u</v>\n"
"<k>slice_type</k><v%s>%u (%s)</v>\n"
"<k>pic_parameter_set_id</k><v%s>%u</v>\n",
t->first_mb_in_slice,
red_if(t->slice_type > 2), slice_type, slice_type_names[t->slice_type],
red_if(pic_parameter_set_id >= 4 || dec->PPS[pic_parameter_set_id].num_ref_idx_active[0] == 0), pic_parameter_set_id);
if (t->slice_type > 2 || pic_parameter_set_id >= 4)
return ENOTSUP;
t->pps = dec->PPS[pic_parameter_set_id];
if (t->pps.num_ref_idx_active[0] == 0) // if PPS wasn't initialized
return EBADMSG;
// parse frame_num
int frame_num = get_uv(&dec->_gb, dec->sps.log2_max_frame_num);
int FrameNumMask = (1 << dec->sps.log2_max_frame_num) - 1;
if (dec->currPic >= 0 && frame_num != (dec->FrameNum & FrameNumMask))
finish_frame(dec, 0);
int non_base_view = 1;
if (dec->nal_unit_type != 20) {
dec->IdrPicFlag = dec->nal_unit_type == 5;
non_base_view = 0;
}
unsigned view_mask = ((dec->sps.mvc - 1) | 0x55555555 << non_base_view) & ((1 << dec->sps.num_frame_buffers) - 1);
int prevRefFrameNum = dec->IdrPicFlag ? 0 : dec->prevRefFrameNum[non_base_view];
dec->FrameNum = prevRefFrameNum + ((frame_num - prevRefFrameNum) & FrameNumMask);
print_header(dec, "<k>frame_num => FrameNum</k><v>%u => %u</v>\n", frame_num, dec->FrameNum);
// Check for gaps in frame_num (8.2.5.2)
int gap = dec->FrameNum - prevRefFrameNum;
if (__builtin_expect(gap > 1, 0)) {
// make enough non-referenced slots by dereferencing frames
int max_num_ref_frames = dec->sps.max_num_ref_frames >> dec->sps.mvc;
int non_existing = min(gap - 1, max_num_ref_frames - __builtin_popcount(dec->long_term_flags & view_mask));
for (int excess = __builtin_popcount(view_mask & dec->reference_flags) + non_existing - max_num_ref_frames; excess > 0; excess--) {
int unref, poc = INT_MAX;
for (unsigned r = view_mask & dec->reference_flags & ~dec->long_term_flags; r; r &= r - 1) {
int i = __builtin_ctz(r);
if (dec->FrameNums[i] < poc)
poc = dec->FrameNums[unref = i];
}
dec->reference_flags &= ~(1 << unref);
}
// make enough non-outputable slots by raising dispPicOrderCnt
unsigned output_flags = dec->output_flags;
for (int excess = non_existing - __builtin_popcount(view_mask & ~dec->reference_flags & ~output_flags); excess > 0; excess--) {
int disp, poc = INT_MAX;
for (unsigned o = view_mask & ~dec->reference_flags & output_flags; o; o &= o - 1) {
int i = __builtin_ctz(o);
if (dec->FieldOrderCnt[0][i] < poc)
poc = dec->FieldOrderCnt[0][disp = i];
}
output_flags &= ~(1 << disp);
dec->dispPicOrderCnt = max(dec->dispPicOrderCnt, poc);
}
// wait until enough of the slots we freed are undepended
unsigned avail;
while (__builtin_popcount(avail = view_mask & ~dec->reference_flags & ~output_flags & ~depended_frames(dec)) < non_existing) {
if (non_blocking)
return EWOULDBLOCK;
pthread_cond_wait(&dec->task_complete, &dec->lock);
}
// stop here if we must wait for get_frame to consume and return enough frames
avail &= ~dec->borrow_flags;
if (output_flags != dec->output_flags || __builtin_popcount(avail) < non_existing)
return ENOBUFS;
// finally insert the last non-existing frames one by one
for (unsigned FrameNum = dec->FrameNum - non_existing; FrameNum < dec->FrameNum; FrameNum++) {
int i = __builtin_ctz(avail);
avail ^= 1 << i;
dec->reference_flags |= 1 << i;
dec->FrameNums[i] = FrameNum;
int PicOrderCnt = 0;
if (dec->sps.pic_order_cnt_type == 2) {
PicOrderCnt = FrameNum * 2;
} else if (dec->sps.num_ref_frames_in_pic_order_cnt_cycle > 0) {
PicOrderCnt = (FrameNum / dec->sps.num_ref_frames_in_pic_order_cnt_cycle) *
dec->sps.PicOrderCntDeltas[dec->sps.num_ref_frames_in_pic_order_cnt_cycle] +
dec->sps.PicOrderCntDeltas[FrameNum % dec->sps.num_ref_frames_in_pic_order_cnt_cycle];
}
dec->FieldOrderCnt[0][i] = dec->FieldOrderCnt[1][i] = PicOrderCnt;
if (dec->frame_buffers[i] == NULL && alloc_frame(dec, i))
return ENOMEM;
}
dec->prevRefFrameNum[non_base_view] = dec->FrameNum - 1;
}
if (dec->nal_ref_idc)
dec->prevRefFrameNum[non_base_view] = dec->FrameNum;
// As long as PAFF/MBAFF are unsupported, this code won't execute (but is still kept).
t->field_pic_flag = 0;
t->bottom_field_flag = 0;
if (!dec->sps.frame_mbs_only_flag) {
t->field_pic_flag = get_u1(&dec->_gb);
print_header(dec, "<k>field_pic_flag</k><v>%x</v>\n", t->field_pic_flag);
if (t->field_pic_flag) {
t->bottom_field_flag = get_u1(&dec->_gb);
print_header(dec, "<k>bottom_field_flag</k><v>%x</v>\n",
t->bottom_field_flag);
}
}
t->MbaffFrameFlag = dec->sps.mb_adaptive_frame_field_flag & ~t->field_pic_flag;
// I did not get the point of idr_pic_id yet.
if (dec->IdrPicFlag) {
int idr_pic_id = get_ue32(&dec->_gb, 65535);
print_header(dec, "<k>idr_pic_id</k><v>%u</v>\n", idr_pic_id);
}
// Compute Top/BottomFieldOrderCnt (8.2.1).
if (dec->sps.pic_order_cnt_type == 0) {
int pic_order_cnt_lsb = get_uv(&dec->_gb, dec->sps.log2_max_pic_order_cnt_lsb);
int shift = WORD_BIT - dec->sps.log2_max_pic_order_cnt_lsb;
if (dec->currPic >= 0 && pic_order_cnt_lsb != ((unsigned)dec->FieldOrderCnt[0][dec->currPic] << shift >> shift))
finish_frame(dec, 0);
int prevPicOrderCnt = dec->IdrPicFlag ? 0 : dec->prevPicOrderCnt;
int inc = (pic_order_cnt_lsb - prevPicOrderCnt) << shift >> shift;
dec->TopFieldOrderCnt = prevPicOrderCnt + inc;
int delta_pic_order_cnt_bottom = 0;
if (t->pps.bottom_field_pic_order_in_frame_present_flag && !t->field_pic_flag)
delta_pic_order_cnt_bottom = get_se32(&dec->_gb, (-1u << 31) + 1, (1u << 31) - 1);
dec->BottomFieldOrderCnt = dec->TopFieldOrderCnt + delta_pic_order_cnt_bottom;
print_header(dec, "<k>pic_order_cnt_lsb/delta_pic_order_cnt_bottom => Top/Bottom POC</k><v>%u/%d => %d/%d</v>\n",
pic_order_cnt_lsb, delta_pic_order_cnt_bottom, dec->TopFieldOrderCnt, dec->BottomFieldOrderCnt);
} else if (dec->sps.pic_order_cnt_type == 1) {
unsigned absFrameNum = dec->FrameNum + (dec->nal_ref_idc != 0) - 1;
dec->TopFieldOrderCnt = (dec->nal_ref_idc) ? 0 : dec->sps.offset_for_non_ref_pic;
if (dec->sps.num_ref_frames_in_pic_order_cnt_cycle > 0) {
dec->TopFieldOrderCnt += (absFrameNum / dec->sps.num_ref_frames_in_pic_order_cnt_cycle) *
dec->sps.PicOrderCntDeltas[dec->sps.num_ref_frames_in_pic_order_cnt_cycle] +
dec->sps.PicOrderCntDeltas[absFrameNum % dec->sps.num_ref_frames_in_pic_order_cnt_cycle];
}
int delta_pic_order_cnt0 = 0, delta_pic_order_cnt1 = 0;
if (!dec->sps.delta_pic_order_always_zero_flag) {
delta_pic_order_cnt0 = get_se32(&dec->_gb, (-1u << 31) + 1, (1u << 31) - 1);
if (t->pps.bottom_field_pic_order_in_frame_present_flag && !t->field_pic_flag)
delta_pic_order_cnt1 = get_se32(&dec->_gb, (-1u << 31) + 1, (1u << 31) - 1);
}
dec->TopFieldOrderCnt += delta_pic_order_cnt0;
if (dec->currPic >= 0 && dec->TopFieldOrderCnt != dec->FieldOrderCnt[0][dec->currPic])
finish_frame(dec, 0);
dec->BottomFieldOrderCnt = dec->TopFieldOrderCnt + delta_pic_order_cnt1;
print_header(dec, "<k>delta_pic_order_cnt[0/1] => Top/Bottom POC</k><v>%d/%d => %d/%d</v>\n", delta_pic_order_cnt0, delta_pic_order_cnt1, dec->TopFieldOrderCnt, dec->BottomFieldOrderCnt);
} else {
dec->TopFieldOrderCnt = dec->BottomFieldOrderCnt = dec->FrameNum * 2 + (dec->nal_ref_idc != 0) - 1;
print_header(dec, "<k>PicOrderCnt</k><v>%d</v>\n", dec->TopFieldOrderCnt);
}
if (abs(dec->TopFieldOrderCnt) >= 1 << 25 || abs(dec->BottomFieldOrderCnt) >= 1 << 25)
return ENOTSUP;
// find and possibly allocate a DPB slot for the upcoming frame
if (dec->currPic >= 0 && non_base_view != (dec->currPic & dec->sps.mvc))
finish_frame(dec, 1);
int is_first_slice = 0;
if (dec->currPic < 0) {
// get a mask of free slots or find the next to be released by get_frame
unsigned avail = view_mask & ~dec->reference_flags & ~dec->output_flags, ready;
if (!avail) {
int poc = INT_MAX;
for (unsigned o = view_mask & ~dec->reference_flags & ~avail; o; o &= o - 1) {
int i = __builtin_ctz(o);
if (dec->FieldOrderCnt[0][i] < poc) {
poc = dec->FieldOrderCnt[0][i];