-
Notifications
You must be signed in to change notification settings - Fork 54
/
decodemv.c
1560 lines (1382 loc) · 57.8 KB
/
decodemv.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
/*
* Copyright (c) 2016, Alliance for Open Media. All rights reserved
*
* This source code is subject to the terms of the BSD 2 Clause License and
* the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
* was not distributed with this source code in the LICENSE file, you can
* obtain it at www.aomedia.org/license/software. If the Alliance for Open
* Media Patent License 1.0 was not distributed with this source code in the
* PATENTS file, you can obtain it at www.aomedia.org/license/patent.
*/
#include <assert.h>
#include "av1/common/cfl.h"
#include "av1/common/common.h"
#include "av1/common/entropy.h"
#include "av1/common/entropymode.h"
#include "av1/common/entropymv.h"
#include "av1/common/mvref_common.h"
#include "av1/common/pred_common.h"
#include "av1/common/reconinter.h"
#include "av1/common/reconintra.h"
#include "av1/common/seg_common.h"
#include "av1/common/warped_motion.h"
#include "av1/decoder/decodeframe.h"
#include "av1/decoder/decodemv.h"
#include "aom_dsp/aom_dsp_common.h"
#define ACCT_STR __func__
#define DEC_MISMATCH_DEBUG 0
static PREDICTION_MODE read_intra_mode(aom_reader *r, aom_cdf_prob *cdf) {
return (PREDICTION_MODE)aom_read_symbol(r, cdf, INTRA_MODES, ACCT_STR);
}
static void read_cdef(AV1_COMMON *cm, aom_reader *r, MACROBLOCKD *const xd,
int mi_col, int mi_row) {
MB_MODE_INFO *const mbmi = xd->mi[0];
if (cm->coded_lossless) return;
if (cm->allow_intrabc) {
assert(cm->cdef_bits == 0);
return;
}
if (!(mi_col & (cm->seq_params.mib_size - 1)) &&
!(mi_row & (cm->seq_params.mib_size - 1))) { // Top left?
xd->cdef_preset[0] = xd->cdef_preset[1] = xd->cdef_preset[2] =
xd->cdef_preset[3] = -1;
}
// Read CDEF param at the first non-skip coding block
const int mask = (1 << (6 - MI_SIZE_LOG2));
const int m = ~(mask - 1);
const int index = cm->seq_params.sb_size == BLOCK_128X128
? !!(mi_col & mask) + 2 * !!(mi_row & mask)
: 0;
cm->mi_grid_visible[(mi_row & m) * cm->mi_stride + (mi_col & m)]
->cdef_strength = xd->cdef_preset[index] =
xd->cdef_preset[index] == -1 && !mbmi->skip
? aom_read_literal(r, cm->cdef_bits, ACCT_STR)
: xd->cdef_preset[index];
}
static int read_delta_qindex(AV1_COMMON *cm, const MACROBLOCKD *xd,
aom_reader *r, MB_MODE_INFO *const mbmi,
int mi_col, int mi_row) {
int sign, abs, reduced_delta_qindex = 0;
BLOCK_SIZE bsize = mbmi->sb_type;
const int b_col = mi_col & (cm->seq_params.mib_size - 1);
const int b_row = mi_row & (cm->seq_params.mib_size - 1);
const int read_delta_q_flag = (b_col == 0 && b_row == 0);
FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
if ((bsize != cm->seq_params.sb_size || mbmi->skip == 0) &&
read_delta_q_flag) {
abs = aom_read_symbol(r, ec_ctx->delta_q_cdf, DELTA_Q_PROBS + 1, ACCT_STR);
const int smallval = (abs < DELTA_Q_SMALL);
if (!smallval) {
const int rem_bits = aom_read_literal(r, 3, ACCT_STR) + 1;
const int thr = (1 << rem_bits) + 1;
abs = aom_read_literal(r, rem_bits, ACCT_STR) + thr;
}
if (abs) {
sign = aom_read_bit(r, ACCT_STR);
} else {
sign = 1;
}
reduced_delta_qindex = sign ? -abs : abs;
}
return reduced_delta_qindex;
}
static int read_delta_lflevel(const AV1_COMMON *const cm, aom_reader *r,
aom_cdf_prob *const cdf,
const MB_MODE_INFO *const mbmi, int mi_col,
int mi_row) {
int reduced_delta_lflevel = 0;
const BLOCK_SIZE bsize = mbmi->sb_type;
const int b_col = mi_col & (cm->seq_params.mib_size - 1);
const int b_row = mi_row & (cm->seq_params.mib_size - 1);
const int read_delta_lf_flag = (b_col == 0 && b_row == 0);
if ((bsize != cm->seq_params.sb_size || mbmi->skip == 0) &&
read_delta_lf_flag) {
int abs = aom_read_symbol(r, cdf, DELTA_LF_PROBS + 1, ACCT_STR);
const int smallval = (abs < DELTA_LF_SMALL);
if (!smallval) {
const int rem_bits = aom_read_literal(r, 3, ACCT_STR) + 1;
const int thr = (1 << rem_bits) + 1;
abs = aom_read_literal(r, rem_bits, ACCT_STR) + thr;
}
const int sign = abs ? aom_read_bit(r, ACCT_STR) : 1;
reduced_delta_lflevel = sign ? -abs : abs;
}
return reduced_delta_lflevel;
}
static UV_PREDICTION_MODE read_intra_mode_uv(FRAME_CONTEXT *ec_ctx,
aom_reader *r,
CFL_ALLOWED_TYPE cfl_allowed,
PREDICTION_MODE y_mode) {
const UV_PREDICTION_MODE uv_mode =
aom_read_symbol(r, ec_ctx->uv_mode_cdf[cfl_allowed][y_mode],
UV_INTRA_MODES - !cfl_allowed, ACCT_STR);
return uv_mode;
}
static int read_cfl_alphas(FRAME_CONTEXT *const ec_ctx, aom_reader *r,
int *signs_out) {
const int joint_sign =
aom_read_symbol(r, ec_ctx->cfl_sign_cdf, CFL_JOINT_SIGNS, "cfl:signs");
int idx = 0;
// Magnitudes are only coded for nonzero values
if (CFL_SIGN_U(joint_sign) != CFL_SIGN_ZERO) {
aom_cdf_prob *cdf_u = ec_ctx->cfl_alpha_cdf[CFL_CONTEXT_U(joint_sign)];
idx = aom_read_symbol(r, cdf_u, CFL_ALPHABET_SIZE, "cfl:alpha_u")
<< CFL_ALPHABET_SIZE_LOG2;
}
if (CFL_SIGN_V(joint_sign) != CFL_SIGN_ZERO) {
aom_cdf_prob *cdf_v = ec_ctx->cfl_alpha_cdf[CFL_CONTEXT_V(joint_sign)];
idx += aom_read_symbol(r, cdf_v, CFL_ALPHABET_SIZE, "cfl:alpha_v");
}
*signs_out = joint_sign;
return idx;
}
static INTERINTRA_MODE read_interintra_mode(MACROBLOCKD *xd, aom_reader *r,
int size_group) {
const INTERINTRA_MODE ii_mode = (INTERINTRA_MODE)aom_read_symbol(
r, xd->tile_ctx->interintra_mode_cdf[size_group], INTERINTRA_MODES,
ACCT_STR);
return ii_mode;
}
static PREDICTION_MODE read_inter_mode(FRAME_CONTEXT *ec_ctx, aom_reader *r,
int16_t ctx) {
int16_t mode_ctx = ctx & NEWMV_CTX_MASK;
int is_newmv, is_zeromv, is_refmv;
is_newmv = aom_read_symbol(r, ec_ctx->newmv_cdf[mode_ctx], 2, ACCT_STR) == 0;
if (is_newmv) return NEWMV;
mode_ctx = (ctx >> GLOBALMV_OFFSET) & GLOBALMV_CTX_MASK;
is_zeromv =
aom_read_symbol(r, ec_ctx->zeromv_cdf[mode_ctx], 2, ACCT_STR) == 0;
if (is_zeromv) return GLOBALMV;
mode_ctx = (ctx >> REFMV_OFFSET) & REFMV_CTX_MASK;
is_refmv = aom_read_symbol(r, ec_ctx->refmv_cdf[mode_ctx], 2, ACCT_STR) == 0;
if (is_refmv)
return NEARESTMV;
else
return NEARMV;
}
static void read_drl_idx(FRAME_CONTEXT *ec_ctx, MACROBLOCKD *xd,
MB_MODE_INFO *mbmi, aom_reader *r) {
uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame);
mbmi->ref_mv_idx = 0;
if (mbmi->mode == NEWMV || mbmi->mode == NEW_NEWMV) {
for (int idx = 0; idx < 2; ++idx) {
if (xd->ref_mv_count[ref_frame_type] > idx + 1) {
uint8_t drl_ctx = av1_drl_ctx(xd->ref_mv_stack[ref_frame_type], idx);
int drl_idx = aom_read_symbol(r, ec_ctx->drl_cdf[drl_ctx], 2, ACCT_STR);
mbmi->ref_mv_idx = idx + drl_idx;
if (!drl_idx) return;
}
}
}
if (have_nearmv_in_inter_mode(mbmi->mode)) {
// Offset the NEARESTMV mode.
// TODO(jingning): Unify the two syntax decoding loops after the NEARESTMV
// mode is factored in.
for (int idx = 1; idx < 3; ++idx) {
if (xd->ref_mv_count[ref_frame_type] > idx + 1) {
uint8_t drl_ctx = av1_drl_ctx(xd->ref_mv_stack[ref_frame_type], idx);
int drl_idx = aom_read_symbol(r, ec_ctx->drl_cdf[drl_ctx], 2, ACCT_STR);
mbmi->ref_mv_idx = idx + drl_idx - 1;
if (!drl_idx) return;
}
}
}
}
static MOTION_MODE read_motion_mode(AV1_COMMON *cm, MACROBLOCKD *xd,
MB_MODE_INFO *mbmi, aom_reader *r) {
if (cm->switchable_motion_mode == 0) return SIMPLE_TRANSLATION;
if (mbmi->skip_mode) return SIMPLE_TRANSLATION;
const MOTION_MODE last_motion_mode_allowed =
motion_mode_allowed(xd->global_motion, xd, mbmi, cm->allow_warped_motion);
int motion_mode;
if (last_motion_mode_allowed == SIMPLE_TRANSLATION) return SIMPLE_TRANSLATION;
if (last_motion_mode_allowed == OBMC_CAUSAL) {
motion_mode =
aom_read_symbol(r, xd->tile_ctx->obmc_cdf[mbmi->sb_type], 2, ACCT_STR);
return (MOTION_MODE)(SIMPLE_TRANSLATION + motion_mode);
} else {
motion_mode =
aom_read_symbol(r, xd->tile_ctx->motion_mode_cdf[mbmi->sb_type],
MOTION_MODES, ACCT_STR);
return (MOTION_MODE)(SIMPLE_TRANSLATION + motion_mode);
}
}
static PREDICTION_MODE read_inter_compound_mode(MACROBLOCKD *xd, aom_reader *r,
int16_t ctx) {
const int mode =
aom_read_symbol(r, xd->tile_ctx->inter_compound_mode_cdf[ctx],
INTER_COMPOUND_MODES, ACCT_STR);
assert(is_inter_compound_mode(NEAREST_NEARESTMV + mode));
return NEAREST_NEARESTMV + mode;
}
int av1_neg_deinterleave(int diff, int ref, int max) {
if (!ref) return diff;
if (ref >= (max - 1)) return max - diff - 1;
if (2 * ref < max) {
if (diff <= 2 * ref) {
if (diff & 1)
return ref + ((diff + 1) >> 1);
else
return ref - (diff >> 1);
}
return diff;
} else {
if (diff <= 2 * (max - ref - 1)) {
if (diff & 1)
return ref + ((diff + 1) >> 1);
else
return ref - (diff >> 1);
}
return max - (diff + 1);
}
}
static int read_segment_id(AV1_COMMON *const cm, const MACROBLOCKD *const xd,
int mi_row, int mi_col, aom_reader *r, int skip) {
int cdf_num;
const int pred = av1_get_spatial_seg_pred(cm, xd, mi_row, mi_col, &cdf_num);
if (skip) return pred;
FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
struct segmentation *const seg = &cm->seg;
struct segmentation_probs *const segp = &ec_ctx->seg;
aom_cdf_prob *pred_cdf = segp->spatial_pred_seg_cdf[cdf_num];
const int coded_id = aom_read_symbol(r, pred_cdf, MAX_SEGMENTS, ACCT_STR);
const int segment_id =
av1_neg_deinterleave(coded_id, pred, seg->last_active_segid + 1);
if (segment_id < 0 || segment_id > seg->last_active_segid) {
aom_internal_error(xd->error_info, AOM_CODEC_CORRUPT_FRAME,
"Corrupted segment_ids");
}
return segment_id;
}
static int dec_get_segment_id(const AV1_COMMON *cm, const uint8_t *segment_ids,
int mi_offset, int x_mis, int y_mis) {
int segment_id = INT_MAX;
for (int y = 0; y < y_mis; y++)
for (int x = 0; x < x_mis; x++)
segment_id =
AOMMIN(segment_id, segment_ids[mi_offset + y * cm->mi_cols + x]);
assert(segment_id >= 0 && segment_id < MAX_SEGMENTS);
return segment_id;
}
static void set_segment_id(AV1_COMMON *cm, int mi_offset, int x_mis, int y_mis,
int segment_id) {
assert(segment_id >= 0 && segment_id < MAX_SEGMENTS);
for (int y = 0; y < y_mis; y++)
for (int x = 0; x < x_mis; x++)
cm->current_frame_seg_map[mi_offset + y * cm->mi_cols + x] = segment_id;
}
static int read_intra_segment_id(AV1_COMMON *const cm,
const MACROBLOCKD *const xd, int mi_row,
int mi_col, int bsize, aom_reader *r,
int skip) {
struct segmentation *const seg = &cm->seg;
if (!seg->enabled) return 0; // Default for disabled segmentation
assert(seg->update_map && !seg->temporal_update);
const int mi_offset = mi_row * cm->mi_cols + mi_col;
const int bw = mi_size_wide[bsize];
const int bh = mi_size_high[bsize];
const int x_mis = AOMMIN(cm->mi_cols - mi_col, bw);
const int y_mis = AOMMIN(cm->mi_rows - mi_row, bh);
const int segment_id = read_segment_id(cm, xd, mi_row, mi_col, r, skip);
set_segment_id(cm, mi_offset, x_mis, y_mis, segment_id);
return segment_id;
}
static void copy_segment_id(const AV1_COMMON *cm,
const uint8_t *last_segment_ids,
uint8_t *current_segment_ids, int mi_offset,
int x_mis, int y_mis) {
for (int y = 0; y < y_mis; y++)
for (int x = 0; x < x_mis; x++)
current_segment_ids[mi_offset + y * cm->mi_cols + x] =
last_segment_ids ? last_segment_ids[mi_offset + y * cm->mi_cols + x]
: 0;
}
static int get_predicted_segment_id(AV1_COMMON *const cm, int mi_offset,
int x_mis, int y_mis) {
return cm->last_frame_seg_map ? dec_get_segment_id(cm, cm->last_frame_seg_map,
mi_offset, x_mis, y_mis)
: 0;
}
static int read_inter_segment_id(AV1_COMMON *const cm, MACROBLOCKD *const xd,
int mi_row, int mi_col, int preskip,
aom_reader *r) {
struct segmentation *const seg = &cm->seg;
MB_MODE_INFO *const mbmi = xd->mi[0];
const int mi_offset = mi_row * cm->mi_cols + mi_col;
const int bw = mi_size_wide[mbmi->sb_type];
const int bh = mi_size_high[mbmi->sb_type];
// TODO(slavarnway): move x_mis, y_mis into xd ?????
const int x_mis = AOMMIN(cm->mi_cols - mi_col, bw);
const int y_mis = AOMMIN(cm->mi_rows - mi_row, bh);
if (!seg->enabled) return 0; // Default for disabled segmentation
if (!seg->update_map) {
copy_segment_id(cm, cm->last_frame_seg_map, cm->current_frame_seg_map,
mi_offset, x_mis, y_mis);
return get_predicted_segment_id(cm, mi_offset, x_mis, y_mis);
}
int segment_id;
if (preskip) {
if (!seg->segid_preskip) return 0;
} else {
if (seg->segid_preskip) return mbmi->segment_id;
if (mbmi->skip) {
if (seg->temporal_update) {
mbmi->seg_id_predicted = 0;
}
segment_id = read_segment_id(cm, xd, mi_row, mi_col, r, 1);
set_segment_id(cm, mi_offset, x_mis, y_mis, segment_id);
return segment_id;
}
}
if (seg->temporal_update) {
const int ctx = av1_get_pred_context_seg_id(xd);
FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
struct segmentation_probs *const segp = &ec_ctx->seg;
aom_cdf_prob *pred_cdf = segp->pred_cdf[ctx];
mbmi->seg_id_predicted = aom_read_symbol(r, pred_cdf, 2, ACCT_STR);
if (mbmi->seg_id_predicted) {
segment_id = get_predicted_segment_id(cm, mi_offset, x_mis, y_mis);
} else {
segment_id = read_segment_id(cm, xd, mi_row, mi_col, r, 0);
}
} else {
segment_id = read_segment_id(cm, xd, mi_row, mi_col, r, 0);
}
set_segment_id(cm, mi_offset, x_mis, y_mis, segment_id);
return segment_id;
}
static int read_skip_mode(AV1_COMMON *cm, const MACROBLOCKD *xd, int segment_id,
aom_reader *r) {
if (!cm->skip_mode_flag) return 0;
if (segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP)) {
return 0;
}
if (!is_comp_ref_allowed(xd->mi[0]->sb_type)) return 0;
if (segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME) ||
segfeature_active(&cm->seg, segment_id, SEG_LVL_GLOBALMV)) {
// These features imply single-reference mode, while skip mode implies
// compound reference. Hence, the two are mutually exclusive.
// In other words, skip_mode is implicitly 0 here.
return 0;
}
const int ctx = av1_get_skip_mode_context(xd);
FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
const int skip_mode =
aom_read_symbol(r, ec_ctx->skip_mode_cdfs[ctx], 2, ACCT_STR);
return skip_mode;
}
static int read_skip(AV1_COMMON *cm, const MACROBLOCKD *xd, int segment_id,
aom_reader *r) {
if (segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP)) {
return 1;
} else {
const int ctx = av1_get_skip_context(xd);
FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
const int skip = aom_read_symbol(r, ec_ctx->skip_cdfs[ctx], 2, ACCT_STR);
return skip;
}
}
// Merge the sorted list of cached colors(cached_colors[0...n_cached_colors-1])
// and the sorted list of transmitted colors(colors[n_cached_colors...n-1]) into
// one single sorted list(colors[...]).
static void merge_colors(uint16_t *colors, uint16_t *cached_colors,
int n_colors, int n_cached_colors) {
if (n_cached_colors == 0) return;
int cache_idx = 0, trans_idx = n_cached_colors;
for (int i = 0; i < n_colors; ++i) {
if (cache_idx < n_cached_colors &&
(trans_idx >= n_colors ||
cached_colors[cache_idx] <= colors[trans_idx])) {
colors[i] = cached_colors[cache_idx++];
} else {
assert(trans_idx < n_colors);
colors[i] = colors[trans_idx++];
}
}
}
static void read_palette_colors_y(MACROBLOCKD *const xd, int bit_depth,
PALETTE_MODE_INFO *const pmi, aom_reader *r) {
uint16_t color_cache[2 * PALETTE_MAX_SIZE];
uint16_t cached_colors[PALETTE_MAX_SIZE];
const int n_cache = av1_get_palette_cache(xd, 0, color_cache);
const int n = pmi->palette_size[0];
int idx = 0;
for (int i = 0; i < n_cache && idx < n; ++i)
if (aom_read_bit(r, ACCT_STR)) cached_colors[idx++] = color_cache[i];
if (idx < n) {
const int n_cached_colors = idx;
pmi->palette_colors[idx++] = aom_read_literal(r, bit_depth, ACCT_STR);
if (idx < n) {
const int min_bits = bit_depth - 3;
int bits = min_bits + aom_read_literal(r, 2, ACCT_STR);
int range = (1 << bit_depth) - pmi->palette_colors[idx - 1] - 1;
for (; idx < n; ++idx) {
assert(range >= 0);
const int delta = aom_read_literal(r, bits, ACCT_STR) + 1;
pmi->palette_colors[idx] = clamp(pmi->palette_colors[idx - 1] + delta,
0, (1 << bit_depth) - 1);
range -= (pmi->palette_colors[idx] - pmi->palette_colors[idx - 1]);
bits = AOMMIN(bits, av1_ceil_log2(range));
}
}
merge_colors(pmi->palette_colors, cached_colors, n, n_cached_colors);
} else {
memcpy(pmi->palette_colors, cached_colors, n * sizeof(cached_colors[0]));
}
}
static void read_palette_colors_uv(MACROBLOCKD *const xd, int bit_depth,
PALETTE_MODE_INFO *const pmi,
aom_reader *r) {
const int n = pmi->palette_size[1];
// U channel colors.
uint16_t color_cache[2 * PALETTE_MAX_SIZE];
uint16_t cached_colors[PALETTE_MAX_SIZE];
const int n_cache = av1_get_palette_cache(xd, 1, color_cache);
int idx = 0;
for (int i = 0; i < n_cache && idx < n; ++i)
if (aom_read_bit(r, ACCT_STR)) cached_colors[idx++] = color_cache[i];
if (idx < n) {
const int n_cached_colors = idx;
idx += PALETTE_MAX_SIZE;
pmi->palette_colors[idx++] = aom_read_literal(r, bit_depth, ACCT_STR);
if (idx < PALETTE_MAX_SIZE + n) {
const int min_bits = bit_depth - 3;
int bits = min_bits + aom_read_literal(r, 2, ACCT_STR);
int range = (1 << bit_depth) - pmi->palette_colors[idx - 1];
for (; idx < PALETTE_MAX_SIZE + n; ++idx) {
assert(range >= 0);
const int delta = aom_read_literal(r, bits, ACCT_STR);
pmi->palette_colors[idx] = clamp(pmi->palette_colors[idx - 1] + delta,
0, (1 << bit_depth) - 1);
range -= (pmi->palette_colors[idx] - pmi->palette_colors[idx - 1]);
bits = AOMMIN(bits, av1_ceil_log2(range));
}
}
merge_colors(pmi->palette_colors + PALETTE_MAX_SIZE, cached_colors, n,
n_cached_colors);
} else {
memcpy(pmi->palette_colors + PALETTE_MAX_SIZE, cached_colors,
n * sizeof(cached_colors[0]));
}
// V channel colors.
if (aom_read_bit(r, ACCT_STR)) { // Delta encoding.
const int min_bits_v = bit_depth - 4;
const int max_val = 1 << bit_depth;
int bits = min_bits_v + aom_read_literal(r, 2, ACCT_STR);
pmi->palette_colors[2 * PALETTE_MAX_SIZE] =
aom_read_literal(r, bit_depth, ACCT_STR);
for (int i = 1; i < n; ++i) {
int delta = aom_read_literal(r, bits, ACCT_STR);
if (delta && aom_read_bit(r, ACCT_STR)) delta = -delta;
int val = (int)pmi->palette_colors[2 * PALETTE_MAX_SIZE + i - 1] + delta;
if (val < 0) val += max_val;
if (val >= max_val) val -= max_val;
pmi->palette_colors[2 * PALETTE_MAX_SIZE + i] = val;
}
} else {
for (int i = 0; i < n; ++i) {
pmi->palette_colors[2 * PALETTE_MAX_SIZE + i] =
aom_read_literal(r, bit_depth, ACCT_STR);
}
}
}
static void read_palette_mode_info(AV1_COMMON *const cm, MACROBLOCKD *const xd,
int mi_row, int mi_col, aom_reader *r) {
const int num_planes = av1_num_planes(cm);
MB_MODE_INFO *const mbmi = xd->mi[0];
const BLOCK_SIZE bsize = mbmi->sb_type;
assert(av1_allow_palette(cm->allow_screen_content_tools, bsize));
PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info;
const int bsize_ctx = av1_get_palette_bsize_ctx(bsize);
if (mbmi->mode == DC_PRED) {
const int palette_mode_ctx = av1_get_palette_mode_ctx(xd);
const int modev = aom_read_symbol(
r, xd->tile_ctx->palette_y_mode_cdf[bsize_ctx][palette_mode_ctx], 2,
ACCT_STR);
if (modev) {
pmi->palette_size[0] =
aom_read_symbol(r, xd->tile_ctx->palette_y_size_cdf[bsize_ctx],
PALETTE_SIZES, ACCT_STR) +
2;
read_palette_colors_y(xd, cm->seq_params.bit_depth, pmi, r);
}
}
if (num_planes > 1 && mbmi->uv_mode == UV_DC_PRED &&
is_chroma_reference(mi_row, mi_col, bsize, xd->plane[1].subsampling_x,
xd->plane[1].subsampling_y)) {
const int palette_uv_mode_ctx = (pmi->palette_size[0] > 0);
const int modev = aom_read_symbol(
r, xd->tile_ctx->palette_uv_mode_cdf[palette_uv_mode_ctx], 2, ACCT_STR);
if (modev) {
pmi->palette_size[1] =
aom_read_symbol(r, xd->tile_ctx->palette_uv_size_cdf[bsize_ctx],
PALETTE_SIZES, ACCT_STR) +
2;
read_palette_colors_uv(xd, cm->seq_params.bit_depth, pmi, r);
}
}
}
static int read_angle_delta(aom_reader *r, aom_cdf_prob *cdf) {
const int sym = aom_read_symbol(r, cdf, 2 * MAX_ANGLE_DELTA + 1, ACCT_STR);
return sym - MAX_ANGLE_DELTA;
}
static void read_filter_intra_mode_info(const AV1_COMMON *const cm,
MACROBLOCKD *const xd, aom_reader *r) {
MB_MODE_INFO *const mbmi = xd->mi[0];
FILTER_INTRA_MODE_INFO *filter_intra_mode_info =
&mbmi->filter_intra_mode_info;
if (av1_filter_intra_allowed(cm, mbmi)) {
filter_intra_mode_info->use_filter_intra = aom_read_symbol(
r, xd->tile_ctx->filter_intra_cdfs[mbmi->sb_type], 2, ACCT_STR);
if (filter_intra_mode_info->use_filter_intra) {
filter_intra_mode_info->filter_intra_mode = aom_read_symbol(
r, xd->tile_ctx->filter_intra_mode_cdf, FILTER_INTRA_MODES, ACCT_STR);
}
} else {
filter_intra_mode_info->use_filter_intra = 0;
}
}
void av1_read_tx_type(const AV1_COMMON *const cm, MACROBLOCKD *xd, int blk_row,
int blk_col, TX_SIZE tx_size, aom_reader *r) {
MB_MODE_INFO *mbmi = xd->mi[0];
const int txk_type_idx =
av1_get_txk_type_index(mbmi->sb_type, blk_row, blk_col);
TX_TYPE *tx_type = &mbmi->txk_type[txk_type_idx];
*tx_type = DCT_DCT;
// No need to read transform type if block is skipped.
if (mbmi->skip || segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP))
return;
// No need to read transform type for lossless mode(qindex==0).
const int qindex =
cm->seg.enabled ? xd->qindex[mbmi->segment_id] : cm->base_qindex;
if (qindex <= 0) return;
const int inter_block = is_inter_block(mbmi);
if (get_ext_tx_types(tx_size, inter_block, cm->reduced_tx_set_used) > 1) {
const TxSetType tx_set_type =
av1_get_ext_tx_set_type(tx_size, inter_block, cm->reduced_tx_set_used);
const int eset =
get_ext_tx_set(tx_size, inter_block, cm->reduced_tx_set_used);
// eset == 0 should correspond to a set with only DCT_DCT and
// there is no need to read the tx_type
assert(eset != 0);
const TX_SIZE square_tx_size = txsize_sqr_map[tx_size];
FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
if (inter_block) {
*tx_type = av1_ext_tx_inv[tx_set_type][aom_read_symbol(
r, ec_ctx->inter_ext_tx_cdf[eset][square_tx_size],
av1_num_ext_tx_set[tx_set_type], ACCT_STR)];
} else {
const PREDICTION_MODE intra_mode =
mbmi->filter_intra_mode_info.use_filter_intra
? fimode_to_intradir[mbmi->filter_intra_mode_info
.filter_intra_mode]
: mbmi->mode;
*tx_type = av1_ext_tx_inv[tx_set_type][aom_read_symbol(
r, ec_ctx->intra_ext_tx_cdf[eset][square_tx_size][intra_mode],
av1_num_ext_tx_set[tx_set_type], ACCT_STR)];
}
}
}
static INLINE void read_mv(aom_reader *r, MV *mv, const MV *ref,
nmv_context *ctx, MvSubpelPrecision precision);
static INLINE int is_mv_valid(const MV *mv);
static INLINE int assign_dv(AV1_COMMON *cm, MACROBLOCKD *xd, int_mv *mv,
const int_mv *ref_mv, int mi_row, int mi_col,
BLOCK_SIZE bsize, aom_reader *r) {
FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
read_mv(r, &mv->as_mv, &ref_mv->as_mv, &ec_ctx->ndvc, MV_SUBPEL_NONE);
// DV should not have sub-pel.
assert((mv->as_mv.col & 7) == 0);
assert((mv->as_mv.row & 7) == 0);
mv->as_mv.col = (mv->as_mv.col >> 3) * 8;
mv->as_mv.row = (mv->as_mv.row >> 3) * 8;
int valid = is_mv_valid(&mv->as_mv) &&
av1_is_dv_valid(mv->as_mv, cm, xd, mi_row, mi_col, bsize,
cm->seq_params.mib_size_log2);
return valid;
}
static void read_intrabc_info(AV1_COMMON *const cm, MACROBLOCKD *const xd,
int mi_row, int mi_col, aom_reader *r) {
MB_MODE_INFO *const mbmi = xd->mi[0];
FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
mbmi->use_intrabc = aom_read_symbol(r, ec_ctx->intrabc_cdf, 2, ACCT_STR);
if (mbmi->use_intrabc) {
BLOCK_SIZE bsize = mbmi->sb_type;
mbmi->mode = DC_PRED;
mbmi->uv_mode = UV_DC_PRED;
mbmi->interp_filters = av1_broadcast_interp_filter(BILINEAR);
mbmi->motion_mode = SIMPLE_TRANSLATION;
int16_t inter_mode_ctx[MODE_CTX_REF_FRAMES];
int_mv ref_mvs[INTRA_FRAME + 1][MAX_MV_REF_CANDIDATES];
int_mv global_mvs[REF_FRAMES];
av1_find_mv_refs(cm, xd, mbmi, INTRA_FRAME, xd->ref_mv_count,
xd->ref_mv_stack, ref_mvs, global_mvs, mi_row, mi_col,
inter_mode_ctx);
int_mv nearestmv, nearmv;
av1_find_best_ref_mvs(0, ref_mvs[INTRA_FRAME], &nearestmv, &nearmv, 0);
int_mv dv_ref = nearestmv.as_int == 0 ? nearmv : nearestmv;
if (dv_ref.as_int == 0)
av1_find_ref_dv(&dv_ref, &xd->tile, cm->seq_params.mib_size, mi_row,
mi_col);
// Ref DV should not have sub-pel.
int valid_dv = (dv_ref.as_mv.col & 7) == 0 && (dv_ref.as_mv.row & 7) == 0;
dv_ref.as_mv.col = (dv_ref.as_mv.col >> 3) * 8;
dv_ref.as_mv.row = (dv_ref.as_mv.row >> 3) * 8;
valid_dv = valid_dv && assign_dv(cm, xd, &mbmi->mv[0], &dv_ref, mi_row,
mi_col, bsize, r);
if (!valid_dv) {
// Intra bc motion vectors are not valid - signal corrupt frame
aom_merge_corrupted_flag(&xd->corrupted, 1);
}
}
}
// If delta q is present, reads delta_q index.
// Also reads delta_q loop filter levels, if present.
static void read_delta_q_params(AV1_COMMON *const cm, MACROBLOCKD *const xd,
const int mi_row, const int mi_col,
aom_reader *r) {
if (cm->delta_q_present_flag) {
MB_MODE_INFO *const mbmi = xd->mi[0];
xd->current_qindex +=
read_delta_qindex(cm, xd, r, mbmi, mi_col, mi_row) * cm->delta_q_res;
/* Normative: Clamp to [1,MAXQ] to not interfere with lossless mode */
xd->current_qindex = clamp(xd->current_qindex, 1, MAXQ);
FRAME_CONTEXT *const ec_ctx = xd->tile_ctx;
if (cm->delta_lf_present_flag) {
if (cm->delta_lf_multi) {
const int frame_lf_count =
av1_num_planes(cm) > 1 ? FRAME_LF_COUNT : FRAME_LF_COUNT - 2;
for (int lf_id = 0; lf_id < frame_lf_count; ++lf_id) {
const int tmp_lvl =
xd->delta_lf[lf_id] +
read_delta_lflevel(cm, r, ec_ctx->delta_lf_multi_cdf[lf_id], mbmi,
mi_col, mi_row) *
cm->delta_lf_res;
mbmi->delta_lf[lf_id] = xd->delta_lf[lf_id] =
clamp(tmp_lvl, -MAX_LOOP_FILTER, MAX_LOOP_FILTER);
}
} else {
const int tmp_lvl = xd->delta_lf_from_base +
read_delta_lflevel(cm, r, ec_ctx->delta_lf_cdf,
mbmi, mi_col, mi_row) *
cm->delta_lf_res;
mbmi->delta_lf_from_base = xd->delta_lf_from_base =
clamp(tmp_lvl, -MAX_LOOP_FILTER, MAX_LOOP_FILTER);
}
}
}
}
static void read_intra_frame_mode_info(AV1_COMMON *const cm,
MACROBLOCKD *const xd, int mi_row,
int mi_col, aom_reader *r) {
MB_MODE_INFO *const mbmi = xd->mi[0];
const MB_MODE_INFO *above_mi = xd->above_mbmi;
const MB_MODE_INFO *left_mi = xd->left_mbmi;
const BLOCK_SIZE bsize = mbmi->sb_type;
struct segmentation *const seg = &cm->seg;
FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
if (seg->segid_preskip)
mbmi->segment_id =
read_intra_segment_id(cm, xd, mi_row, mi_col, bsize, r, 0);
mbmi->skip = read_skip(cm, xd, mbmi->segment_id, r);
if (!seg->segid_preskip)
mbmi->segment_id =
read_intra_segment_id(cm, xd, mi_row, mi_col, bsize, r, mbmi->skip);
read_cdef(cm, r, xd, mi_col, mi_row);
read_delta_q_params(cm, xd, mi_row, mi_col, r);
mbmi->current_qindex = xd->current_qindex;
mbmi->ref_frame[0] = INTRA_FRAME;
mbmi->ref_frame[1] = NONE_FRAME;
mbmi->palette_mode_info.palette_size[0] = 0;
mbmi->palette_mode_info.palette_size[1] = 0;
mbmi->filter_intra_mode_info.use_filter_intra = 0;
xd->above_txfm_context = cm->above_txfm_context[xd->tile.tile_row] + mi_col;
xd->left_txfm_context =
xd->left_txfm_context_buffer + (mi_row & MAX_MIB_MASK);
if (av1_allow_intrabc(cm)) {
read_intrabc_info(cm, xd, mi_row, mi_col, r);
if (is_intrabc_block(mbmi)) return;
}
mbmi->mode = read_intra_mode(r, get_y_mode_cdf(ec_ctx, above_mi, left_mi));
const int use_angle_delta = av1_use_angle_delta(bsize);
mbmi->angle_delta[PLANE_TYPE_Y] =
(use_angle_delta && av1_is_directional_mode(mbmi->mode))
? read_angle_delta(r, ec_ctx->angle_delta_cdf[mbmi->mode - V_PRED])
: 0;
if (!cm->seq_params.monochrome &&
is_chroma_reference(mi_row, mi_col, bsize, xd->plane[1].subsampling_x,
xd->plane[1].subsampling_y)) {
xd->cfl.is_chroma_reference = 1;
mbmi->uv_mode =
read_intra_mode_uv(ec_ctx, r, is_cfl_allowed(xd), mbmi->mode);
if (mbmi->uv_mode == UV_CFL_PRED) {
mbmi->cfl_alpha_idx = read_cfl_alphas(ec_ctx, r, &mbmi->cfl_alpha_signs);
}
mbmi->angle_delta[PLANE_TYPE_UV] =
(use_angle_delta && av1_is_directional_mode(get_uv_mode(mbmi->uv_mode)))
? read_angle_delta(r,
ec_ctx->angle_delta_cdf[mbmi->uv_mode - V_PRED])
: 0;
} else {
// Avoid decoding angle_info if there is is no chroma prediction
mbmi->uv_mode = UV_DC_PRED;
xd->cfl.is_chroma_reference = 0;
}
xd->cfl.store_y = store_cfl_required(cm, xd);
if (av1_allow_palette(cm->allow_screen_content_tools, bsize))
read_palette_mode_info(cm, xd, mi_row, mi_col, r);
read_filter_intra_mode_info(cm, xd, r);
}
static int read_mv_component(aom_reader *r, nmv_component *mvcomp,
int use_subpel, int usehp) {
int mag, d, fr, hp;
const int sign = aom_read_symbol(r, mvcomp->sign_cdf, 2, ACCT_STR);
const int mv_class =
aom_read_symbol(r, mvcomp->classes_cdf, MV_CLASSES, ACCT_STR);
const int class0 = mv_class == MV_CLASS_0;
// Integer part
if (class0) {
d = aom_read_symbol(r, mvcomp->class0_cdf, CLASS0_SIZE, ACCT_STR);
mag = 0;
} else {
const int n = mv_class + CLASS0_BITS - 1; // number of bits
d = 0;
for (int i = 0; i < n; ++i)
d |= aom_read_symbol(r, mvcomp->bits_cdf[i], 2, ACCT_STR) << i;
mag = CLASS0_SIZE << (mv_class + 2);
}
if (use_subpel) {
// Fractional part
fr = aom_read_symbol(r, class0 ? mvcomp->class0_fp_cdf[d] : mvcomp->fp_cdf,
MV_FP_SIZE, ACCT_STR);
// High precision part (if hp is not used, the default value of the hp is 1)
hp = usehp ? aom_read_symbol(
r, class0 ? mvcomp->class0_hp_cdf : mvcomp->hp_cdf, 2,
ACCT_STR)
: 1;
} else {
fr = 3;
hp = 1;
}
// Result
mag += ((d << 3) | (fr << 1) | hp) + 1;
return sign ? -mag : mag;
}
static INLINE void read_mv(aom_reader *r, MV *mv, const MV *ref,
nmv_context *ctx, MvSubpelPrecision precision) {
MV diff = kZeroMv;
const MV_JOINT_TYPE joint_type =
(MV_JOINT_TYPE)aom_read_symbol(r, ctx->joints_cdf, MV_JOINTS, ACCT_STR);
if (mv_joint_vertical(joint_type))
diff.row = read_mv_component(r, &ctx->comps[0], precision > MV_SUBPEL_NONE,
precision > MV_SUBPEL_LOW_PRECISION);
if (mv_joint_horizontal(joint_type))
diff.col = read_mv_component(r, &ctx->comps[1], precision > MV_SUBPEL_NONE,
precision > MV_SUBPEL_LOW_PRECISION);
mv->row = ref->row + diff.row;
mv->col = ref->col + diff.col;
}
static REFERENCE_MODE read_block_reference_mode(AV1_COMMON *cm,
const MACROBLOCKD *xd,
aom_reader *r) {
if (!is_comp_ref_allowed(xd->mi[0]->sb_type)) return SINGLE_REFERENCE;
if (cm->reference_mode == REFERENCE_MODE_SELECT) {
const int ctx = av1_get_reference_mode_context(xd);
const REFERENCE_MODE mode = (REFERENCE_MODE)aom_read_symbol(
r, xd->tile_ctx->comp_inter_cdf[ctx], 2, ACCT_STR);
return mode; // SINGLE_REFERENCE or COMPOUND_REFERENCE
} else {
assert(cm->reference_mode == SINGLE_REFERENCE);
return cm->reference_mode;
}
}
#define READ_REF_BIT(pname) \
aom_read_symbol(r, av1_get_pred_cdf_##pname(xd), 2, ACCT_STR)
static COMP_REFERENCE_TYPE read_comp_reference_type(const MACROBLOCKD *xd,
aom_reader *r) {
const int ctx = av1_get_comp_reference_type_context(xd);
const COMP_REFERENCE_TYPE comp_ref_type =
(COMP_REFERENCE_TYPE)aom_read_symbol(
r, xd->tile_ctx->comp_ref_type_cdf[ctx], 2, ACCT_STR);
return comp_ref_type; // UNIDIR_COMP_REFERENCE or BIDIR_COMP_REFERENCE
}
static void set_ref_frames_for_skip_mode(AV1_COMMON *const cm,
MV_REFERENCE_FRAME ref_frame[2]) {
ref_frame[0] = LAST_FRAME + cm->ref_frame_idx_0;
ref_frame[1] = LAST_FRAME + cm->ref_frame_idx_1;
}
// Read the referncence frame
static void read_ref_frames(AV1_COMMON *const cm, MACROBLOCKD *const xd,
aom_reader *r, int segment_id,
MV_REFERENCE_FRAME ref_frame[2]) {
if (xd->mi[0]->skip_mode) {
set_ref_frames_for_skip_mode(cm, ref_frame);
return;
}
if (segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME)) {
ref_frame[0] = (MV_REFERENCE_FRAME)get_segdata(&cm->seg, segment_id,
SEG_LVL_REF_FRAME);
ref_frame[1] = NONE_FRAME;
} else if (segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP) ||
segfeature_active(&cm->seg, segment_id, SEG_LVL_GLOBALMV)) {
ref_frame[0] = LAST_FRAME;
ref_frame[1] = NONE_FRAME;
} else {
const REFERENCE_MODE mode = read_block_reference_mode(cm, xd, r);
if (mode == COMPOUND_REFERENCE) {
const COMP_REFERENCE_TYPE comp_ref_type = read_comp_reference_type(xd, r);
if (comp_ref_type == UNIDIR_COMP_REFERENCE) {
const int bit = READ_REF_BIT(uni_comp_ref_p);
if (bit) {
ref_frame[0] = BWDREF_FRAME;
ref_frame[1] = ALTREF_FRAME;
} else {
const int bit1 = READ_REF_BIT(uni_comp_ref_p1);
if (bit1) {
const int bit2 = READ_REF_BIT(uni_comp_ref_p2);
if (bit2) {
ref_frame[0] = LAST_FRAME;
ref_frame[1] = GOLDEN_FRAME;
} else {
ref_frame[0] = LAST_FRAME;
ref_frame[1] = LAST3_FRAME;
}
} else {
ref_frame[0] = LAST_FRAME;
ref_frame[1] = LAST2_FRAME;
}
}
return;
}
assert(comp_ref_type == BIDIR_COMP_REFERENCE);
const int idx = 1;
const int bit = READ_REF_BIT(comp_ref_p);
// Decode forward references.
if (!bit) {
const int bit1 = READ_REF_BIT(comp_ref_p1);
ref_frame[!idx] = cm->comp_fwd_ref[bit1 ? 1 : 0];
} else {
const int bit2 = READ_REF_BIT(comp_ref_p2);
ref_frame[!idx] = cm->comp_fwd_ref[bit2 ? 3 : 2];
}
// Decode backward references.
const int bit_bwd = READ_REF_BIT(comp_bwdref_p);
if (!bit_bwd) {
const int bit1_bwd = READ_REF_BIT(comp_bwdref_p1);
ref_frame[idx] = cm->comp_bwd_ref[bit1_bwd];
} else {
ref_frame[idx] = cm->comp_bwd_ref[2];
}
} else if (mode == SINGLE_REFERENCE) {
const int bit0 = READ_REF_BIT(single_ref_p1);
if (bit0) {
const int bit1 = READ_REF_BIT(single_ref_p2);
if (!bit1) {
const int bit5 = READ_REF_BIT(single_ref_p6);
ref_frame[0] = bit5 ? ALTREF2_FRAME : BWDREF_FRAME;
} else {
ref_frame[0] = ALTREF_FRAME;
}
} else {
const int bit2 = READ_REF_BIT(single_ref_p3);
if (bit2) {
const int bit4 = READ_REF_BIT(single_ref_p5);
ref_frame[0] = bit4 ? GOLDEN_FRAME : LAST3_FRAME;
} else {
const int bit3 = READ_REF_BIT(single_ref_p4);
ref_frame[0] = bit3 ? LAST2_FRAME : LAST_FRAME;