-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathedge264_headers.c
More file actions
2059 lines (1922 loc) · 88.4 KB
/
Copy pathedge264_headers.c
File metadata and controls
2059 lines (1922 loc) · 88.4 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
#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"
#ifdef LOGS
#include "edge264_sei.c"
#endif
#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},
};
/**
* These functions ease management of the DPB and the decoder state.
*
* unset_currPic is called when a new picture is detected as per 7.4.1.2.4:
* _ frame_num differs (dec->FrameNum contains the previous unwrapped decoded
* value unaffected by mmco=5)
* _ nal_ref_idc differs
* _ pic_order_cnt_type=0 and pic_order_cnt_lsb differs
* (dec->TopFieldOrderCnt contains the previous value unaffected by mmco=5)
* _ pic_order_cnt_type=1 and TopFieldOrderCnt differs (as long as frame_num
* and nal_ref_idc tests are done before, this is equivalent with a
* difference on delta_pic_order_cnt[0])
* _ idr_pic_id differs (useful on streams of IDR frames to distinguish them),
* with -1 when IdrPicFlag=0
* _ view_id differs (between base and non-base)
*
* pic_parameter_set_id is ignored since not stored, and spec mandates that
* POCs should differ anyway. BottomFieldOrderCnt is ignored too because the
* test on TopFieldOrderCnt is sufficient.
*/
static void unset_currPic(Edge264Decoder *dec) {
assert(dec->currPic >= 0);
int non_base_view = dec->non_base_frames >> dec->currPic & 1;
if ((dec->view_short_term_frames | dec->view_long_term_frames) & 1 << dec->currPic) {
unsigned same_views = non_base_view ? dec->non_base_frames : ~dec->non_base_frames;
dec->PrevRefFrameNum[non_base_view] = dec->FrameNums[dec->currPic];
dec->prevPicOrderCnt[non_base_view] = dec->FieldOrderCnt[0][dec->currPic];
dec->prev_short_term_frames = (dec->prev_short_term_frames & ~same_views) | dec->view_short_term_frames;
dec->prev_long_term_frames = (dec->prev_long_term_frames & ~same_views) | dec->view_long_term_frames;
dec->prev_LongTermFrameIdx_v[0] = dec->view_LongTermFrameIdx_v[0];
dec->prev_LongTermFrameIdx_v[1] = dec->view_LongTermFrameIdx_v[1];
}
if (!non_base_view)
dec->basePic = dec->currPic;
dec->currPic = -1;
}
static int bump_frame(Edge264Decoder *dec, int non_base_view, unsigned ignored) {
int pic = -1;
int lowest_poc = INT_MAX;
unsigned same_views = non_base_view ? dec->non_base_frames : ~dec->non_base_frames;
for (unsigned o = dec->to_get_frames & ~dec->output_frames & same_views & ~ignored; o; o &= o - 1) {
int i = __builtin_ctz(o);
if (dec->FieldOrderCnt[0][i] < lowest_poc)
lowest_poc = dec->FieldOrderCnt[0][pic = i];
}
if (pic < 0)
return 0;
dec->output_frames |= 1 << pic;
assert(dec->output_queue[non_base_view][31] < 0); // output_queue should never be full
i8x16 *queue = dec->output_queue_v[non_base_view];
queue[1] = shrd128(queue[0], queue[1], 15);
queue[0] = shrd128(set8(pic), queue[0], 15);
return 1;
}
static int bump_all_frames(Edge264Decoder *dec) {
if (dec->currPic >= 0)
unset_currPic(dec);
while (bump_frame(dec, 0, 0) | bump_frame(dec, 1, 0));
while (dec->busy_tasks)
pthread_cond_wait(&dec->task_complete, &dec->lock);
return dec->to_get_frames | dec->output_frames ? ENOBUFS : 0;
}
static void flush_frames(Edge264Decoder *dec) {
// FIXME interrupt all threads then wait until they are back to wait
assert(!(dec->n_threads == 0 && dec->busy_tasks));
while (dec->busy_tasks)
pthread_cond_wait(&dec->task_complete, &dec->lock);
}
static int alloc_frame(Edge264Decoder *dec, int id, int errno_on_fail) {
int mbs = (dec->sps.pic_width_in_mbs + 1) * dec->sps.pic_height_in_mbs - 1;
unsigned samples_size = dec->plane_size_Y + dec->plane_size_C + 16; // plus margin for overreads
unsigned mbs_size = sizeof(Edge264Macroblock) * mbs;
dec->alloc_cb((void **)&dec->samples_buffers[id], samples_size, (void **)&dec->mb_buffers[id], mbs_size, errno_on_fail, dec->alloc_arg);
Edge264Macroblock *m = dec->mb_buffers[id];
if (dec->samples_buffers[id] && m) {
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;
} else {
dec->free_cb(dec->samples_buffers[id], m, dec->alloc_arg);
dec->samples_buffers[id] = NULL;
dec->mb_buffers[id] = NULL;
return errno_on_fail;
}
}
static void clear_decoder(Edge264Decoder *dec) {
memset((void *)dec + offsetof(Edge264Decoder, nal_ref_idc), 0, offsetof(Edge264Decoder, log_base_us) - offsetof(Edge264Decoder, nal_ref_idc));
dec->currPic = dec->basePic = -1;
dec->PrevRefFrameNum[0] = dec->PrevRefFrameNum[1] = -1;
dec->taskPics_v = dec->output_queue_v[0][0] = dec->output_queue_v[0][1] =
dec->output_queue_v[1][0] = dec->output_queue_v[1][1] = set8(-1);
}
int ADD_VARIANT(parse_end_of_sequence)(Edge264Decoder *dec, Edge264UnrefCb unref_cb, void *unref_arg) {
int ret = EBADMSG;
if (rbsp_end(&dec->gb, 0)) {
if (bump_all_frames(dec))
return ENOBUFS;
clear_decoder(dec);
ret = 0;
}
return print_dec(dec, " decode_NAL_result: %s\n", ret);
}
#ifdef LOGS
int ignore_NAL_log(Edge264Decoder *dec, Edge264UnrefCb unref_cb, void *unref_arg) {
return print_dec(dec, " decode_NAL_result: %s\n", 0);
}
int unsup_NAL_log(Edge264Decoder *dec, Edge264UnrefCb unref_cb, void *unref_arg) {
return print_dec(dec, " decode_NAL_result: %s\n", ENOTSUP);
}
#endif
/**
* 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->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_buffers[currPic] + (ctx->mbx + ctx->mby * ctx->t.stride[0]) * 16;
ctx->samples_mb[1] = ctx->t.samples_buffers[currPic] + (ctx->mbx + ctx->mby * ctx->t.stride[1]) * 8 + ctx->t.plane_size_Y;
ctx->samples_mb[2] = ctx->samples_mb[1] + (ctx->t.stride[1] >> 1);
int mb_offset = ctx->mbx + ctx->mby * (ctx->t.pic_width_in_mbs + 1);
ctx->mbCol = ctx->_mb = ctx->t.mb_buffer + 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] = loadu128(QP_Y2C + 12 + ctx->t.pps.chroma_qp_index_offset);
ctx->QP_C_v[1] = loadu128(QP_Y2C + 28 + ctx->t.pps.chroma_qp_index_offset);
ctx->QP_C_v[2] = loadu128(QP_Y2C + 44 + ctx->t.pps.chroma_qp_index_offset);
ctx->QP_C_v[3] = loadu128(QP_Y2C + 60 + ctx->t.pps.chroma_qp_index_offset);
ctx->QP_C_v[4] = loadu128(QP_Y2C + 12 + ctx->t.pps.second_chroma_qp_index_offset);
ctx->QP_C_v[5] = loadu128(QP_Y2C + 28 + ctx->t.pps.second_chroma_qp_index_offset);
ctx->QP_C_v[6] = loadu128(QP_Y2C + 44 + ctx->t.pps.second_chroma_qp_index_offset);
ctx->QP_C_v[7] = loadu128(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];
}
for (int i = 0; i < 16; i++)
ctx->c_v[i] = (i8x16){};
// 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 = ctx->t.mbCol_buffer + mb_offset;
ctx->col_short_term = 1 & ~(ctx->t.prev_long_term_frames >> 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 = 0; 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.prev_long_term_frames & 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.prev_long_term_frames & 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.
*
* FIXME remove ldleft macros eventually
*/
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_buffers[currPic] + (ctx->mbx + ctx->mby * ctx->t.stride[0]) * 16;
ctx->samples_mb[1] = ctx->t.samples_buffers[currPic] + (ctx->mbx + ctx->mby * ctx->t.stride[1]) * 8 + ctx->t.plane_size_Y;
ctx->samples_mb[2] = ctx->samples_mb[1] + (ctx->t.stride[1] >> 1);
int mb_offset = ctx->mbx + ctx->mby * (ctx->t.pic_width_in_mbs + 1);
ctx->_mb = ctx->t.mb_buffer + mb_offset;
ctx->mbCol = ctx->t.mbCol_buffer + 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
// FIXME use Intra function instead
if (ctx->t.slice_type == 2) { // I slice -> blend with intra DC
size_t stride_Y = ctx->t.stride[0];
DECL_SSTRIDE(stride_Y);
uint8_t * restrict y0 = ctx->samples_mb[0];
uint8_t * restrict y7 = y0 + stride_Y * 7;
uint8_t * restrict yE = y7 + stride_Y * 7;
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 = loada128(SADDR(y0, -1));
} else { // A available
l = t = ldleftC(y0, stride_Y, 0); // from intra.c
if (i >= ctx->t.pic_width_in_mbs) // B available
t = loada128(SADDR(y0, -1));
}
i8x16 dcY = broadcast8(shrru16(sum8(t) + sum8(l), 5), 0);
#if SIMD == SSE
i8x16 w0 = (p128 < 128) ? ziplo8(set8(128 - p128), set8(p128)) : (i8x16){0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1};
i8x16 w1 = w0;
i64x2 wd = {p128 < 128 ? 7 : 0};
#elif SIMD == NEON
i16x8 w0 = (p128 < 128) ? (i16x8){128 - p128, p128} : (i16x8){0, 1};
i16x8 w1 = w0;
i16x8 wd = set16(p128 < 128 ? 7 : 0);
#elif SIMD == WASM
i16x8 w0 = set16(p128 < 128 ? 128 - p128 : 0);
i16x8 w1 = set16(p128 < 128 ? p128 : 1);
int wd = p128 < 128 ? 7 : 0;
#elif SIMD == CLANG
i16x8 w0 = set16(p128 < 128 ? 128 - p128 : 0);
i16x8 w1 = set16(p128 < 128 ? p128 : 1);
i16x8 wd = set16(p128 < 128 ? 7 : 0);
#endif
i16x8 o = {};
*(i8x16 *)SADDR(y0, 0) = maddshrL(*(i8x16 *)SADDR(y0, 0), dcY, w0, w1, o, wd);
*(i8x16 *)SADDR(y0, 1) = maddshrL(*(i8x16 *)SADDR(y0, 1), dcY, w0, w1, o, wd);
*(i8x16 *)SADDR(y0, 2) = maddshrL(*(i8x16 *)SADDR(y0, 2), dcY, w0, w1, o, wd);
*(i8x16 *)SADDR(y7, -4) = maddshrL(*(i8x16 *)SADDR(y7, -4), dcY, w0, w1, o, wd);
*(i8x16 *)SADDR(y0, 4) = maddshrL(*(i8x16 *)SADDR(y0, 4), dcY, w0, w1, o, wd);
*(i8x16 *)SADDR(y7, -2) = maddshrL(*(i8x16 *)SADDR(y7, -2), dcY, w0, w1, o, wd);
*(i8x16 *)SADDR(y7, -1) = maddshrL(*(i8x16 *)SADDR(y7, -1), dcY, w0, w1, o, wd);
*(i8x16 *)SADDR(y7, 0) = maddshrL(*(i8x16 *)SADDR(y7, 0), dcY, w0, w1, o, wd);
*(i8x16 *)SADDR(y7, 1) = maddshrL(*(i8x16 *)SADDR(y7, 1), dcY, w0, w1, o, wd);
*(i8x16 *)SADDR(y7, 2) = maddshrL(*(i8x16 *)SADDR(y7, 2), dcY, w0, w1, o, wd);
*(i8x16 *)SADDR(yE, -4) = maddshrL(*(i8x16 *)SADDR(yE, -4), dcY, w0, w1, o, wd);
*(i8x16 *)SADDR(y7, 4) = maddshrL(*(i8x16 *)SADDR(y7, 4), dcY, w0, w1, o, wd);
*(i8x16 *)SADDR(yE, -2) = maddshrL(*(i8x16 *)SADDR(yE, -2), dcY, w0, w1, o, wd);
*(i8x16 *)SADDR(yE, -1) = maddshrL(*(i8x16 *)SADDR(yE, -1), dcY, w0, w1, o, wd);
*(i8x16 *)SADDR(yE, 0) = maddshrL(*(i8x16 *)SADDR(yE, 0), dcY, w0, w1, o, wd);
*(i8x16 *)SADDR(yE, 1) = maddshrL(*(i8x16 *)SADDR(yE, 1), dcY, w0, w1, o, wd);
size_t stride_C = ctx->t.stride[1] >> 1;
DECL_DSTRIDE(stride_C);
uint8_t * restrict c0 = ctx->samples_mb[1];
uint8_t * restrict c7 = c0 + stride_C * 7;
uint8_t * restrict cE = c7 + stride_C * 7;
i8x16 lbr = ldleftC(c0, stride_C, 0); // from intra.c
i8x16 b = ziplo64(loada64(DADDR(c0, -2)), l);
i8x16 r = combine64(loada64(DADDR(c0, -1)), l);
i8x16 dcb = broadcast8(shrru16(sum8(b), 4), 0);
i8x16 dcr = broadcast8(shrru16(sum8(r), 4), 0);
i8x16 dcC = ziplo64(dcb, dcr);
i64x2 v0 = maddshrL(loada64x2(DADDR(c0, 0), DADDR(c0, 1)), dcC, w0, w1, o, wd);
i64x2 v1 = maddshrL(loada64x2(DADDR(c0, 2), DADDR(c7, -4)), dcC, w0, w1, o, wd);
i64x2 v2 = maddshrL(loada64x2(DADDR(c0, 4), DADDR(c7, -2)), dcC, w0, w1, o, wd);
i64x2 v3 = maddshrL(loada64x2(DADDR(c7, -1), DADDR(c7, 0)), dcC, w0, w1, o, wd);
i64x2 v4 = maddshrL(loada64x2(DADDR(c7, 1), DADDR(c7, 2)), dcC, w0, w1, o, wd);
i64x2 v5 = maddshrL(loada64x2(DADDR(cE, -4), DADDR(c7, 4)), dcC, w0, w1, o, wd);
i64x2 v6 = maddshrL(loada64x2(DADDR(cE, -2), DADDR(cE, -1)), dcC, w0, w1, o, wd);
i64x2 v7 = maddshrL(loada64x2(DADDR(cE, 0), DADDR(cE, 1)), dcC, w0, w1, o, wd);
*(int64_t *)DADDR(c0, 0) = v0[0];
*(int64_t *)DADDR(c0, 1) = v0[1];
*(int64_t *)DADDR(c0, 2) = v1[0];
*(int64_t *)DADDR(c7, -4) = v1[1];
*(int64_t *)DADDR(c0, 4) = v2[0];
*(int64_t *)DADDR(c7, -2) = v2[1];
*(int64_t *)DADDR(c7, -1) = v3[0];
*(int64_t *)DADDR(c7, 0) = v3[1];
*(int64_t *)DADDR(c7, 1) = v4[0];
*(int64_t *)DADDR(c7, 2) = v4[1];
*(int64_t *)DADDR(cE, -4) = v5[0];
*(int64_t *)DADDR(c7, 4) = v5[1];
*(int64_t *)DADDR(cE, -2) = v6[0];
*(int64_t *)DADDR(cE, -1) = v6[1];
*(int64_t *)DADDR(cE, 0) = v7[0];
*(int64_t *)DADDR(cE, 1) = 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 worker threads, where they consume
* tasks continuously until stopped by the parent process.
*/
void *ADD_VARIANT(worker_loop)(void *arg) {
Edge264Context c;
c.d = (void *)((uintptr_t)arg & -16);
c.thread_id = c.d->n_threads ? (uintptr_t)arg & 15 : -1;
c.log_base_us = c.d->log_base_us;
c.log_cb = c.d->log_cb;
c.log_arg = c.d->log_arg;
c.log_indent = c.d->n_threads ? " " : " ";
c.log_pos = 0;
if (c.thread_id >= 0)
pthread_mutex_lock(&c.d->lock);
while (1) {
// wait until a task becomes available and reserve it
while (c.thread_id >= 0 && !c.d->ready_tasks)
pthread_cond_wait(&c.d->task_ready, &c.d->lock);
assert((unsigned)c.d->ready_tasks - 1 < 65535); // 0 < ready_tasks < 65536
int task_id = __builtin_ctz(c.d->ready_tasks); // FIXME arbitrary selection for now
int currPic = c.d->taskPics[task_id];
c.d->pending_tasks &= ~(1 << task_id);
c.d->ready_tasks &= ~(1 << task_id);
if (c.thread_id >= 0)
pthread_mutex_unlock(&c.d->lock);
unsigned long long clock_start = get_relative_time_us() - c.log_base_us;
c.t = c.d->tasks[task_id];
unsigned approx_byte_size = c.t.gb.end - c.t.gb.CPB;
initialize_context(&c, currPic);
// call the function containing the macroblock decoding loop
size_t ret = 0;
if (!c.t.pps.entropy_coding_mode_flag) {
c.mb_skip_run = -1;
parse_slice_data_cavlc(&c);
if (!rbsp_end(&c.t.gb, 1))
ret = EBADMSG;
} 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);
// rbsp_stop_one_bit was consumed in cabac_terminate, and the possibility of cabac_zero_word implies we cannot require reaching end
if (c.t.gb.msb_cache || (c.t.gb.lsb_cache & (c.t.gb.lsb_cache - 1)))
ret = EBADMSG; // FIXME error_flag
}
}
if (c.t.unref_cb)
c.t.unref_cb((int)ret, c.t.unref_arg);
// 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_buffers[currPic] + (c.mbx + c.mby * c.t.stride[0]) * 16;
c.samples_mb[1] = c.t.samples_buffers[currPic] + (c.mbx + c.mby * c.t.stride[1]) * 8 + c.t.plane_size_Y;
c.samples_mb[2] = c.samples_mb[1] + (c.t.stride[1] >> 1);
c._mb = (Edge264Macroblock *)c.t.mb_buffer + 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 c.d->next_deblock_addr, considering it might have reached first_mb_in_slice since start
if (c.d->next_deblock_addr[currPic] >= c.t.first_mb_in_slice &&
!(c.t.disable_deblocking_filter_idc == 0 && c.t.next_deblock_addr < 0)) {
c.d->next_deblock_addr[currPic] = c.CurrMbAddr;
pthread_cond_broadcast(&c.d->task_progress);
}
// deblock the rest of the frame if all mbs have been decoded correctly
int remaining_mbs = ret ?: __atomic_sub_fetch(&c.d->remaining_mbs[currPic], c.CurrMbAddr - c.t.first_mb_in_slice, __ATOMIC_ACQ_REL);
if (remaining_mbs == 0) {
c.t.next_deblock_addr = c.d->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_buffers[currPic] + (c.mbx + c.mby * c.t.stride[0]) * 16;
c.samples_mb[1] = c.t.samples_buffers[currPic] + (c.mbx + c.mby * c.t.stride[1]) * 8 + c.t.plane_size_Y;
c.samples_mb[2] = c.samples_mb[1] + (c.t.stride[1] >> 1);
c._mb = (Edge264Macroblock *)c.t.mb_buffer + 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;
}
}
}
c.d->next_deblock_addr[currPic] = INT_MAX; // signals the frame is complete
}
// print benchmarking information
if (c.log_cb) {
unsigned long long clock_end = get_relative_time_us() - c.log_base_us;
snprintf(c.log_buf, sizeof(c.log_buf),
"\n- thread_id: %d\n"
" FrameId: %u\n"
" first_mb_in_slice: %u\n"
" approx_byte_size: %u\n"
" decoding_start_us: %llu\n"
" decoding_end_us: %llu\n"
" slice_result: %s\n",
c.thread_id, c.t.FrameId, c.t.first_mb_in_slice, approx_byte_size, clock_start, clock_end, ret_to_str(ret));
c.log_cb(c.log_buf, c.log_arg);
}
// if multi-threaded, check if we are the last task to touch this frame and ensure it is complete
if (c.thread_id >= 0) {
pthread_mutex_lock(&c.d->lock);
pthread_cond_signal(&c.d->task_complete);
if (remaining_mbs == 0) {
pthread_cond_broadcast(&c.d->task_progress);
c.d->ready_tasks = ready_tasks(c.d);
if (c.d->ready_tasks)
pthread_cond_broadcast(&c.d->task_ready);
}
}
c.d->busy_tasks &= ~(1 << task_id);
c.d->task_dependencies[task_id] = 0;
c.d->taskPics[task_id] = -1;
if (c.thread_id < 0)
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, Edge264SeqParameterSet *sps)
{
static const char * const mmco_names[6] = {
" - {mmco: 1, sref: %u} # dereference\n",
" - {mmco: 2, lref: %2$u} # dereference\n",
" - {mmco: 3, sref: %u, lref: %u} # convert\n",
" - {mmco: 4, lref: %2$d} # dereference on and above\n",
" - {mmco: 5} # dereference all\n",
" - {mmco: 6, lref: %2$u} # convert current\n"};
// no_output_of_prior_pics_flag is easier to support than to signal unsupported
if (dec->IdrPicFlag) {
int no_output_of_prior_pics_flag = get_u1(&dec->gb);
int long_term_flag = get_u1(&dec->gb);
dec->view_short_term_frames = (long_term_flag ^ 1) << dec->currPic;
dec->view_long_term_frames = long_term_flag << dec->currPic;
dec->view_LongTermFrameIdx_v[0] = dec->view_LongTermFrameIdx_v[1] = (i8x16){};
log_dec(dec, " no_output_of_prior_pics_flag: %d\n"
" long_term_reference_flag: %d\n",
no_output_of_prior_pics_flag,
dec->view_long_term_frames >> dec->currPic);
while (bump_frame(dec, dec->nal_unit_type == 20, 1 << dec->currPic));
return;
}
// 8.2.5.4 - Adaptive memory control marking process.
int long_term_frame = 0;
if (get_u1(&dec->gb)) {
log_dec(dec, " memory_management_control_operations:\n");
int memory_management_control_operation;
int i = 32;
while ((memory_management_control_operation = get_ue16(&dec->gb, 6)) != 0 && i-- > 0) {
int target = dec->currPic, FrameNum = 0, long_term_frame_idx = 0;
if (10 & 1 << memory_management_control_operation) { // 1 or 3
// target and dereference a given short-term or non-existing frame
FrameNum = dec->FrameNum - 1 - get_ue32(&dec->gb, 4294967294);
for (unsigned r = dec->view_short_term_frames; r; r &= r - 1) {
int j = __builtin_ctz(r);
if (dec->FrameNums[j] == FrameNum) {
target = j;
dec->view_short_term_frames ^= 1 << j;
dec->view_long_term_frames &= ~(1 << j);
}
}
}
if (92 & 1 << memory_management_control_operation) { // 2 or 3 or 4 or 6
long_term_frame_idx = get_ue16(&dec->gb, sps->max_num_ref_frames - (memory_management_control_operation != 4));
int up = (memory_management_control_operation == 4) ? INT_MAX : long_term_frame_idx;
// dereference one or many long-term frames
for (unsigned r = dec->view_long_term_frames & ~dec->view_short_term_frames; r; r &= r - 1) {
int j = __builtin_ctz(r);
if (dec->view_LongTermFrameIdx[j] >= long_term_frame_idx && dec->view_LongTermFrameIdx[j] <= up)
dec->view_long_term_frames ^= 1 << j;
}
if (72 & 1 << memory_management_control_operation) { // 3 or 6
dec->view_LongTermFrameIdx[target] = long_term_frame_idx;
if (memory_management_control_operation == 6)
long_term_frame = 1;
else if (target != dec->currPic)
dec->view_long_term_frames |= 1 << target;
}
}
if (memory_management_control_operation == 5) { // dereference all frames
dec->view_short_term_frames = dec->view_long_term_frames = 0;
dec->FrameNums[dec->currPic] = 0;
dec->view_LongTermFrameIdx_v[0] = dec->view_LongTermFrameIdx_v[1] = (i8x16){};
int tempPicOrderCnt = minw(dec->TopFieldOrderCnt, dec->BottomFieldOrderCnt);
dec->FieldOrderCnt[0][dec->currPic] = dec->TopFieldOrderCnt - tempPicOrderCnt;
dec->FieldOrderCnt[1][dec->currPic] = dec->BottomFieldOrderCnt - tempPicOrderCnt;
while (bump_frame(dec, dec->nal_unit_type == 20, 1 << dec->currPic));
}
log_dec(dec, mmco_names[memory_management_control_operation - 1],
FrameNum, long_term_frame_idx);
}
}
// 8.2.5.3 - Sliding window marking process
if (__builtin_popcount(dec->view_short_term_frames | dec->view_long_term_frames) >= sps->max_num_ref_frames) {
int best = INT_MAX;
int next = 0;
// iterate on short-term and non-existing frames
for (unsigned r = dec->view_short_term_frames; r != 0; r &= r - 1) {
int i = __builtin_ctz(r);
if (best > dec->FrameNums[i])
best = dec->FrameNums[next = i];
}
dec->view_short_term_frames ^= 1 << next;
dec->view_long_term_frames &= ~(1 << next);
}
*(long_term_frame ? &dec->view_long_term_frames : &dec->view_short_term_frames) |= 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, Edge264SeqParameterSet *sps, 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 (sps->ChromaArrayType != 0)
t->chroma_log2_weight_denom = get_ue16(&dec->gb, 7);
for (int l = 0; l <= t->slice_type; l++) {
log_dec(dec, " explicit_weights_l%u:\n", 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 (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;
}
log_dec(dec, sps->ChromaArrayType ? " - {Y: \"*%d>>%u+%d\", Cb: \"*%d>>%u+%d\", Cr: \"*%d>>%u+%d\"}\n" : " - {Y: \"*%d>>%u+%d\"}\n",
t->explicit_weights[0][i], t->luma_log2_weight_denom, t->explicit_offsets[0][i],
t->explicit_weights[1][i], t->chroma_log2_weight_denom, t->explicit_offsets[1][i],
t->explicit_weights[2][i], t->chroma_log2_weight_denom, t->explicit_offsets[2][i]);
}
}
}
}
/**
* 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, Edge264SeqParameterSet *sps, Edge264Task *t)
{
// initial sort on FrameNum for P, on PicOrderCnt for B
int count[3] = {0, 0, 0}; // number of refs before/after/long
int size = 0;
if (!dec->IdrPicFlag) {
const int32_t *values = (t->slice_type == 0) ? dec->FrameNums : dec->FieldOrderCnt[0];
int pic_value = (t->slice_type == 0) ? dec->FrameNum : dec->TopFieldOrderCnt;
unsigned refs = (t->slice_type != 0 && sps->pic_order_cnt_type == 0) ?
dec->view_short_term_frames ^ dec->view_long_term_frames :
dec->view_short_term_frames | dec->view_long_term_frames;
for (unsigned 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->prev_LongTermFrameIdx[i] + 0x20000;
int v = (dec->view_short_term_frames & 1 << i) ? ShortTermNum : LongTermNum;
if (v < best)
best = v, next = i;
}
t->RefPicList[0][size++] = next;
count[best >> 16]++;
}
}
if (dec->nal_unit_type == 20) // if we're second view
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->prev_short_term_frames & 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
log_dec(dec, " ref_pic_list_modifications_l%u: [", 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);
log_dec(dec, "[\"%s\",%+d],",
modification_of_pic_nums_idc < 2 ? "sref" : modification_of_pic_nums_idc == 2 ? "lref" : "view",
modification_of_pic_nums_idc % 4 == 0 ? -num - 1 : num + (modification_of_pic_nums_idc != 2));
int pic = dec->basePic; // for modification_of_pic_nums_idc == 4 and 5
if (modification_of_pic_nums_idc < 2) {
picNumLX = (modification_of_pic_nums_idc == 0) ? picNumLX - (num + 1) : picNumLX + (num + 1);
unsigned MaskFrameNum = (1 << sps->log2_max_frame_num) - 1;
// iterate on short-term and non-existing frames
for (unsigned r = dec->view_short_term_frames; r; r &= r - 1) {
pic = __builtin_ctz(r);
if (!((dec->FrameNums[pic] ^ picNumLX) & MaskFrameNum))
break;
}
} else if (modification_of_pic_nums_idc == 2) {
// iterate on long-term frames only
for (unsigned r = dec->view_long_term_frames & ~dec->view_short_term_frames; r; r &= r - 1) {
pic = __builtin_ctz(r);
if (dec->prev_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);
}
log_dec(dec, "]\n");
}
}
#ifdef LOGS
for (int lx = 0; lx <= t->slice_type; lx++) {
log_dec(dec, lx == 0 ? " RefPicLists: [[" : "], [");
for (int i = 0; i < t->pps.num_ref_idx_active[lx]; i++) {
int pic = t->RefPicList[lx][i];
log_dec(dec, i == 0 ? "%d" : ",%d", dec->FrameIds[pic]);
}
}
log_dec(dec, "]]\n");
#endif
}
/**
* This fonction copies the last set of fields to finish initializing the task.
*/
static void initialize_task(Edge264Decoder *dec, Edge264SeqParameterSet *sps, Edge264Task *t)
{
// copy most essential fields from dec
memcpy(&t->gb, &dec->gb, sizeof(dec->gb)); // GCC-14 crashes on dec->out = format
t->ChromaArrayType = sps->ChromaArrayType;
t->direct_8x8_inference_flag = sps->direct_8x8_inference_flag;
t->pic_width_in_mbs = sps->pic_width_in_mbs;
t->pic_height_in_mbs = sps->pic_height_in_mbs;
if (t->pps.pic_scaling_matrix_present_flag) {
t->pps.weightScale4x4_v[0] = ifelse_mask(t->pps.weightScale4x4_v[0] == 0, sps->weightScale4x4_v[0], t->pps.weightScale4x4_v[0]);
t->pps.weightScale4x4_v[1] = ifelse_mask(t->pps.weightScale4x4_v[1] == 0, sps->weightScale4x4_v[0], t->pps.weightScale4x4_v[1]);
t->pps.weightScale4x4_v[2] = ifelse_mask(t->pps.weightScale4x4_v[2] == 0, sps->weightScale4x4_v[0], t->pps.weightScale4x4_v[2]);
t->pps.weightScale4x4_v[3] = ifelse_mask(t->pps.weightScale4x4_v[3] == 0, sps->weightScale4x4_v[3], t->pps.weightScale4x4_v[3]);
t->pps.weightScale4x4_v[4] = ifelse_mask(t->pps.weightScale4x4_v[4] == 0, sps->weightScale4x4_v[3], t->pps.weightScale4x4_v[4]);
t->pps.weightScale4x4_v[5] = ifelse_mask(t->pps.weightScale4x4_v[5] == 0, sps->weightScale4x4_v[3], t->pps.weightScale4x4_v[5]);
for (unsigned i = 0; i < 24; i++)
t->pps.weightScale8x8_v[i] = ifelse_mask(t->pps.weightScale8x8_v[i] == 0, sps->weightScale8x8_v[i & 7], t->pps.weightScale8x8_v[i]);
} else {
memcpy(t->pps.weightScale4x4_v, sps->weightScale4x4_v, 96);
memcpy(t->pps.weightScale8x8_v, sps->weightScale8x8_v, 384);
}
t->frame_flip_bit = dec->frame_flip_bits >> dec->currPic & 1;
t->stride[0] = dec->out.stride_Y;
t->stride[1] = t->stride[2] = dec->out.stride_C;
t->FrameId = dec->FrameIds[dec->currPic];
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->prev_long_term_frames = dec->prev_long_term_frames & ~dec->prev_short_term_frames; // mask of only long-term frames
t->mb_buffer = (Edge264Macroblock *)dec->mb_buffers[dec->currPic];
memcpy(t->samples_buffers, dec->samples_buffers, sizeof(t->samples_buffers));
t->samples_clip_v[0] = set16((1 << sps->BitDepth_Y) - 1);
t->samples_clip_v[1] = t->samples_clip_v[2] = set16((1 << sps->BitDepth_C) - 1);
if (t->slice_type == 1) { // B slices
t->mbCol_buffer = (Edge264Macroblock *)dec->mb_buffers[t->RefPicList[1][0]];
if (t->pps.weighted_bipred_idc == 2 || !t->direct_spatial_mv_pred_flag) {
u32x4 poc = set32(minw(dec->TopFieldOrderCnt, dec->BottomFieldOrderCnt));
t->diff_poc_v[0] = packs32(poc - minw32(dec->FieldOrderCnt_v[0][0], dec->FieldOrderCnt_v[1][0]),
poc - minw32(dec->FieldOrderCnt_v[0][1], dec->FieldOrderCnt_v[1][1]));
t->diff_poc_v[1] = packs32(poc - minw32(dec->FieldOrderCnt_v[0][2], dec->FieldOrderCnt_v[1][2]),
poc - minw32(dec->FieldOrderCnt_v[0][3], dec->FieldOrderCnt_v[1][3]));
t->diff_poc_v[2] = packs32(poc - minw32(dec->FieldOrderCnt_v[0][4], dec->FieldOrderCnt_v[1][4]),
poc - minw32(dec->FieldOrderCnt_v[0][5], dec->FieldOrderCnt_v[1][5]));
t->diff_poc_v[3] = packs32(poc - minw32(dec->FieldOrderCnt_v[0][6], dec->FieldOrderCnt_v[1][6]),
poc - minw32(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, Edge264UnrefCb unref_cb, void *unref_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", "sliced"};
int ret;
// find and reserve an empty task to fill
unsigned avail_tasks;
while (!(avail_tasks = 0xffff & ~dec->busy_tasks))
pthread_cond_wait(&dec->task_complete, &dec->lock);
Edge264Task *t = dec->tasks + __builtin_ctz(avail_tasks);
t->unref_cb = unref_cb;
t->unref_arg = unref_arg;
t->RefPicList_v[0] = t->RefPicList_v[1] = t->RefPicList_v[2] = t->RefPicList_v[3] =
(i8x16){-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
// check on view_id
int non_base_view = 1;
unsigned same_views = dec->non_base_frames;
Edge264SeqParameterSet *sps = &dec->ssps;
if (dec->nal_unit_type != 20) {
non_base_view = 0;
same_views = ~same_views;
sps = &dec->sps;
dec->IdrPicFlag = dec->nal_unit_type == 5;
}
// first important fields and checks before decoding the slice header
t->first_mb_in_slice = get_ue32(&dec->gb, 139263);
int slice_type = get_ue16(&dec->gb, 9);
slice_type = (dec->nal_unit_type == 5 || sps->max_num_ref_frames == 0) ? 2 : slice_type; // enforce condition in 7.4.3
t->slice_type = (slice_type < 5) ? slice_type : slice_type - 5;
int pic_parameter_set_id = get_ue16(&dec->gb, 255);
log_dec(dec, " first_mb_in_slice: %u\n"
" slice_type: %u # %s%s\n"
" pic_parameter_set_id: %u%s\n",
t->first_mb_in_slice,
slice_type, slice_type_names[t->slice_type], unsup_if(t->slice_type > 2),
pic_parameter_set_id, unsup_if(pic_parameter_set_id >= 4));
if (t->slice_type > 2 || pic_parameter_set_id >= 4)
return print_dec(dec, " decode_NAL_result: %s\n", ENOTSUP); // exit now if the rest of the slice can't be correctly parsed
t->pps = dec->PPS[pic_parameter_set_id];
if (!sps->BitDepth_Y || !t->pps.num_ref_idx_active[0])
return print_dec(dec, " decode_NAL_result: %s\n", EBADMSG); // exit now if SPS or PPS wasn't initialized
// keep frame_num on stack until we can compute FrameNum after all unset_currPic
int frame_num = get_uv(&dec->gb, sps->log2_max_frame_num);
frame_num = dec->IdrPicFlag ? 0 : frame_num; // enforce condition in 7.4.3