GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/cbs_av1_syntax_template.c Lines: 790 1071 73.8 %
Date: 2020-04-04 00:26:16 Branches: 674 1510 44.6 %

Line Branch Exec Source
1
/*
2
 * This file is part of FFmpeg.
3
 *
4
 * FFmpeg is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
6
 * License as published by the Free Software Foundation; either
7
 * version 2.1 of the License, or (at your option) any later version.
8
 *
9
 * FFmpeg is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
 * Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
15
 * License along with FFmpeg; if not, write to the Free Software
16
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
 */
18
19
10270
static int FUNC(obu_header)(CodedBitstreamContext *ctx, RWContext *rw,
20
                            AV1RawOBUHeader *current)
21
{
22
    int err;
23
24
10270
    HEADER("OBU header");
25
26
10270
    fc(1, obu_forbidden_bit, 0, 0);
27
28
10270
    fc(4, obu_type, 0, AV1_OBU_PADDING);
29
10270
    flag(obu_extension_flag);
30
10270
    flag(obu_has_size_field);
31
32
10270
    fc(1, obu_reserved_1bit, 0, 0);
33
34
10270
    if (current->obu_extension_flag) {
35
800
        fb(3, temporal_id);
36
800
        fb(2, spatial_id);
37
800
        fc(3, extension_header_reserved_3bits, 0, 0);
38
    }
39
40
10270
    return 0;
41
}
42
43
1450
static int FUNC(trailing_bits)(CodedBitstreamContext *ctx, RWContext *rw, int nb_bits)
44
{
45
    int err;
46
47
1450
    av_assert0(nb_bits > 0);
48
49
1450
    fixed(1, trailing_one_bit, 1);
50
1450
    --nb_bits;
51
52
7842
    while (nb_bits > 0) {
53
6392
        fixed(1, trailing_zero_bit, 0);
54
6392
        --nb_bits;
55
    }
56
57
1450
    return 0;
58
}
59
60
4680
static int FUNC(byte_alignment)(CodedBitstreamContext *ctx, RWContext *rw)
61
{
62
    int err;
63
64
16006
    while (byte_alignment(rw) != 0)
65
11326
        fixed(1, zero_bit, 0);
66
67
4680
    return 0;
68
}
69
70
874
static int FUNC(color_config)(CodedBitstreamContext *ctx, RWContext *rw,
71
                              AV1RawColorConfig *current, int seq_profile)
72
{
73
874
    CodedBitstreamAV1Context *priv = ctx->priv_data;
74
    int err;
75
76
874
    flag(high_bitdepth);
77
78
874
    if (seq_profile == FF_PROFILE_AV1_PROFESSIONAL &&
79
        current->high_bitdepth) {
80
        flag(twelve_bit);
81
        priv->bit_depth = current->twelve_bit ? 12 : 10;
82
    } else {
83
874
        priv->bit_depth = current->high_bitdepth ? 10 : 8;
84
    }
85
86
874
    if (seq_profile == FF_PROFILE_AV1_HIGH)
87
        infer(mono_chrome, 0);
88
    else
89
874
        flag(mono_chrome);
90
874
    priv->num_planes = current->mono_chrome ? 1 : 3;
91
92
874
    flag(color_description_present_flag);
93
874
    if (current->color_description_present_flag) {
94
        fb(8, color_primaries);
95
        fb(8, transfer_characteristics);
96
        fb(8, matrix_coefficients);
97
    } else {
98
874
        infer(color_primaries,          AVCOL_PRI_UNSPECIFIED);
99
874
        infer(transfer_characteristics, AVCOL_TRC_UNSPECIFIED);
100
874
        infer(matrix_coefficients,      AVCOL_SPC_UNSPECIFIED);
101
    }
102
103
874
    if (current->mono_chrome) {
104
        flag(color_range);
105
106
        infer(subsampling_x, 1);
107
        infer(subsampling_y, 1);
108
        infer(chroma_sample_position, AV1_CSP_UNKNOWN);
109
        infer(separate_uv_delta_q, 0);
110
111
874
    } else if (current->color_primaries          == AVCOL_PRI_BT709 &&
112
               current->transfer_characteristics == AVCOL_TRC_IEC61966_2_1 &&
113
               current->matrix_coefficients      == AVCOL_SPC_RGB) {
114
        infer(color_range,   1);
115
        infer(subsampling_x, 0);
116
        infer(subsampling_y, 0);
117
        flag(separate_uv_delta_q);
118
119
    } else {
120
874
        flag(color_range);
121
122
874
        if (seq_profile == FF_PROFILE_AV1_MAIN) {
123
874
            infer(subsampling_x, 1);
124
874
            infer(subsampling_y, 1);
125
        } else if (seq_profile == FF_PROFILE_AV1_HIGH) {
126
            infer(subsampling_x, 0);
127
            infer(subsampling_y, 0);
128
        } else {
129
            if (priv->bit_depth == 12) {
130
                fb(1, subsampling_x);
131
                if (current->subsampling_x)
132
                    fb(1, subsampling_y);
133
                else
134
                    infer(subsampling_y, 0);
135
            } else {
136
                infer(subsampling_x, 1);
137
                infer(subsampling_y, 0);
138
            }
139
        }
140

874
        if (current->subsampling_x && current->subsampling_y) {
141
874
            fc(2, chroma_sample_position, AV1_CSP_UNKNOWN,
142
                                          AV1_CSP_COLOCATED);
143
        }
144
145
874
        flag(separate_uv_delta_q);
146
    }
147
148
874
    return 0;
149
}
150
151
444
static int FUNC(timing_info)(CodedBitstreamContext *ctx, RWContext *rw,
152
                             AV1RawTimingInfo *current)
153
{
154
    int err;
155
156
444
    fc(32, num_units_in_display_tick, 1, MAX_UINT_BITS(32));
157
444
    fc(32, time_scale,                1, MAX_UINT_BITS(32));
158
159
444
    flag(equal_picture_interval);
160
444
    if (current->equal_picture_interval)
161
        uvlc(num_ticks_per_picture_minus_1, 0, MAX_UINT_BITS(32) - 1);
162
163
444
    return 0;
164
}
165
166
444
static int FUNC(decoder_model_info)(CodedBitstreamContext *ctx, RWContext *rw,
167
                                    AV1RawDecoderModelInfo *current)
168
{
169
    int err;
170
171
444
    fb(5, buffer_delay_length_minus_1);
172
444
    fb(32, num_units_in_decoding_tick);
173
444
    fb(5,  buffer_removal_time_length_minus_1);
174
444
    fb(5,  frame_presentation_time_length_minus_1);
175
176
444
    return 0;
177
}
178
179
874
static int FUNC(sequence_header_obu)(CodedBitstreamContext *ctx, RWContext *rw,
180
                                     AV1RawSequenceHeader *current)
181
{
182
    int i, err;
183
184
874
    HEADER("Sequence Header");
185
186
874
    fc(3, seq_profile, FF_PROFILE_AV1_MAIN,
187
                       FF_PROFILE_AV1_PROFESSIONAL);
188
874
    flag(still_picture);
189
874
    flag(reduced_still_picture_header);
190
191
874
    if (current->reduced_still_picture_header) {
192
        infer(timing_info_present_flag,           0);
193
        infer(decoder_model_info_present_flag,    0);
194
        infer(initial_display_delay_present_flag, 0);
195
        infer(operating_points_cnt_minus_1,       0);
196
        infer(operating_point_idc[0],             0);
197
198
        fb(5, seq_level_idx[0]);
199
200
        infer(seq_tier[0], 0);
201
        infer(decoder_model_present_for_this_op[0],         0);
202
        infer(initial_display_delay_present_for_this_op[0], 0);
203
204
    } else {
205
874
        flag(timing_info_present_flag);
206
874
        if (current->timing_info_present_flag) {
207
444
            CHECK(FUNC(timing_info)(ctx, rw, &current->timing_info));
208
209
444
            flag(decoder_model_info_present_flag);
210
444
            if (current->decoder_model_info_present_flag) {
211
444
                CHECK(FUNC(decoder_model_info)
212
                          (ctx, rw, &current->decoder_model_info));
213
            }
214
        } else {
215
430
            infer(decoder_model_info_present_flag, 0);
216
        }
217
218
874
        flag(initial_display_delay_present_flag);
219
220
874
        fb(5, operating_points_cnt_minus_1);
221
1808
        for (i = 0; i <= current->operating_points_cnt_minus_1; i++) {
222
934
            fbs(12, operating_point_idc[i], 1, i);
223
934
            fbs(5,  seq_level_idx[i], 1, i);
224
225
934
            if (current->seq_level_idx[i] > 7)
226
                flags(seq_tier[i], 1, i);
227
            else
228
934
                infer(seq_tier[i], 0);
229
230
934
            if (current->decoder_model_info_present_flag) {
231
444
                flags(decoder_model_present_for_this_op[i], 1, i);
232
444
                if (current->decoder_model_present_for_this_op[i]) {
233
444
                    int n = current->decoder_model_info.buffer_delay_length_minus_1 + 1;
234
444
                    fbs(n, decoder_buffer_delay[i], 1, i);
235
444
                    fbs(n, encoder_buffer_delay[i], 1, i);
236
444
                    flags(low_delay_mode_flag[i], 1, i);
237
                }
238
            } else {
239
490
                infer(decoder_model_present_for_this_op[i], 0);
240
            }
241
242
934
            if (current->initial_display_delay_present_flag) {
243
444
                flags(initial_display_delay_present_for_this_op[i], 1, i);
244
444
                if (current->initial_display_delay_present_for_this_op[i])
245
444
                    fbs(4, initial_display_delay_minus_1[i], 1, i);
246
            }
247
        }
248
    }
249
250
874
    fb(4, frame_width_bits_minus_1);
251
874
    fb(4, frame_height_bits_minus_1);
252
253
874
    fb(current->frame_width_bits_minus_1  + 1, max_frame_width_minus_1);
254
874
    fb(current->frame_height_bits_minus_1 + 1, max_frame_height_minus_1);
255
256
874
    if (current->reduced_still_picture_header)
257
        infer(frame_id_numbers_present_flag, 0);
258
    else
259
874
        flag(frame_id_numbers_present_flag);
260
874
    if (current->frame_id_numbers_present_flag) {
261
        fb(4, delta_frame_id_length_minus_2);
262
        fb(3, additional_frame_id_length_minus_1);
263
    }
264
265
874
    flag(use_128x128_superblock);
266
874
    flag(enable_filter_intra);
267
874
    flag(enable_intra_edge_filter);
268
269
874
    if (current->reduced_still_picture_header) {
270
        infer(enable_interintra_compound, 0);
271
        infer(enable_masked_compound,     0);
272
        infer(enable_warped_motion,       0);
273
        infer(enable_dual_filter,         0);
274
        infer(enable_order_hint,          0);
275
        infer(enable_jnt_comp,            0);
276
        infer(enable_ref_frame_mvs,       0);
277
278
        infer(seq_force_screen_content_tools,
279
              AV1_SELECT_SCREEN_CONTENT_TOOLS);
280
        infer(seq_force_integer_mv,
281
              AV1_SELECT_INTEGER_MV);
282
    } else {
283
874
        flag(enable_interintra_compound);
284
874
        flag(enable_masked_compound);
285
874
        flag(enable_warped_motion);
286
874
        flag(enable_dual_filter);
287
288
874
        flag(enable_order_hint);
289
874
        if (current->enable_order_hint) {
290
874
            flag(enable_jnt_comp);
291
874
            flag(enable_ref_frame_mvs);
292
        } else {
293
            infer(enable_jnt_comp,      0);
294
            infer(enable_ref_frame_mvs, 0);
295
        }
296
297
874
        flag(seq_choose_screen_content_tools);
298
874
        if (current->seq_choose_screen_content_tools)
299
874
            infer(seq_force_screen_content_tools,
300
                  AV1_SELECT_SCREEN_CONTENT_TOOLS);
301
        else
302
            fb(1, seq_force_screen_content_tools);
303
874
        if (current->seq_force_screen_content_tools > 0) {
304
874
            flag(seq_choose_integer_mv);
305
874
            if (current->seq_choose_integer_mv)
306
874
                infer(seq_force_integer_mv,
307
                      AV1_SELECT_INTEGER_MV);
308
            else
309
                fb(1, seq_force_integer_mv);
310
        } else {
311
            infer(seq_force_integer_mv, AV1_SELECT_INTEGER_MV);
312
        }
313
314
874
        if (current->enable_order_hint)
315
874
            fb(3, order_hint_bits_minus_1);
316
    }
317
318
874
    flag(enable_superres);
319
874
    flag(enable_cdef);
320
874
    flag(enable_restoration);
321
322
874
    CHECK(FUNC(color_config)(ctx, rw, &current->color_config,
323
                             current->seq_profile));
324
325
874
    flag(film_grain_params_present);
326
327
874
    return 0;
328
}
329
330
2038
static int FUNC(temporal_delimiter_obu)(CodedBitstreamContext *ctx, RWContext *rw)
331
{
332
2038
    CodedBitstreamAV1Context *priv = ctx->priv_data;
333
334
2038
    HEADER("Temporal Delimiter");
335
336
2038
    priv->seen_frame_header = 0;
337
338
2038
    return 0;
339
}
340
341
84
static int FUNC(set_frame_refs)(CodedBitstreamContext *ctx, RWContext *rw,
342
                                AV1RawFrameHeader *current)
343
{
344
84
    CodedBitstreamAV1Context *priv = ctx->priv_data;
345
84
    const AV1RawSequenceHeader *seq = priv->sequence_header;
346
    static const uint8_t ref_frame_list[AV1_NUM_REF_FRAMES - 2] = {
347
        AV1_REF_FRAME_LAST2, AV1_REF_FRAME_LAST3, AV1_REF_FRAME_BWDREF,
348
        AV1_REF_FRAME_ALTREF2, AV1_REF_FRAME_ALTREF
349
    };
350
    int8_t ref_frame_idx[AV1_REFS_PER_FRAME], used_frame[AV1_NUM_REF_FRAMES];
351
    int8_t shifted_order_hints[AV1_NUM_REF_FRAMES];
352
    int cur_frame_hint, latest_order_hint, earliest_order_hint, ref;
353
    int i, j;
354
355
672
    for (i = 0; i < AV1_REFS_PER_FRAME; i++)
356
588
        ref_frame_idx[i] = -1;
357
84
    ref_frame_idx[AV1_REF_FRAME_LAST - AV1_REF_FRAME_LAST] = current->last_frame_idx;
358
84
    ref_frame_idx[AV1_REF_FRAME_GOLDEN - AV1_REF_FRAME_LAST] = current->golden_frame_idx;
359
360
756
    for (i = 0; i < AV1_NUM_REF_FRAMES; i++)
361
672
        used_frame[i] = 0;
362
84
    used_frame[current->last_frame_idx] = 1;
363
84
    used_frame[current->golden_frame_idx] = 1;
364
365
84
    cur_frame_hint = 1 << (seq->order_hint_bits_minus_1);
366
756
    for (i = 0; i < AV1_NUM_REF_FRAMES; i++)
367
672
        shifted_order_hints[i] = cur_frame_hint +
368
672
                                 cbs_av1_get_relative_dist(seq, priv->ref[i].order_hint,
369
672
                                                           current->order_hint);
370
371
84
    latest_order_hint = shifted_order_hints[current->last_frame_idx];
372
84
    earliest_order_hint = shifted_order_hints[current->golden_frame_idx];
373
374
84
    ref = -1;
375
756
    for (i = 0; i < AV1_NUM_REF_FRAMES; i++) {
376
672
        int hint = shifted_order_hints[i];
377

672
        if (!used_frame[i] && hint >= cur_frame_hint &&
378
168
            (ref < 0 || hint >= latest_order_hint)) {
379
174
            ref = i;
380
174
            latest_order_hint = hint;
381
        }
382
    }
383
84
    if (ref >= 0) {
384
84
        ref_frame_idx[AV1_REF_FRAME_ALTREF - AV1_REF_FRAME_LAST] = ref;
385
84
        used_frame[ref] = 1;
386
    }
387
388
84
    ref = -1;
389
756
    for (i = 0; i < AV1_NUM_REF_FRAMES; i++) {
390
672
        int hint = shifted_order_hints[i];
391

672
        if (!used_frame[i] && hint >= cur_frame_hint &&
392
84
            (ref < 0 || hint < earliest_order_hint)) {
393
90
            ref = i;
394
90
            earliest_order_hint = hint;
395
        }
396
    }
397
84
    if (ref >= 0) {
398
84
        ref_frame_idx[AV1_REF_FRAME_BWDREF - AV1_REF_FRAME_LAST] = ref;
399
84
        used_frame[ref] = 1;
400
    }
401
402
84
    ref = -1;
403
756
    for (i = 0; i < AV1_NUM_REF_FRAMES; i++) {
404
672
        int hint = shifted_order_hints[i];
405

672
        if (!used_frame[i] && hint >= cur_frame_hint &&
406
12
            (ref < 0 || hint < earliest_order_hint)) {
407
72
            ref = i;
408
72
            earliest_order_hint = hint;
409
        }
410
    }
411
84
    if (ref >= 0) {
412
72
        ref_frame_idx[AV1_REF_FRAME_ALTREF2 - AV1_REF_FRAME_LAST] = ref;
413
72
        used_frame[ref] = 1;
414
    }
415
416
504
    for (i = 0; i < AV1_REFS_PER_FRAME - 2; i++) {
417
420
        int ref_frame = ref_frame_list[i];
418
420
        if (ref_frame_idx[ref_frame - AV1_REF_FRAME_LAST] < 0 ) {
419
180
            ref = -1;
420
1620
            for (j = 0; j < AV1_NUM_REF_FRAMES; j++) {
421
1440
                int hint = shifted_order_hints[j];
422

1440
                if (!used_frame[j] && hint < cur_frame_hint &&
423
264
                    (ref < 0 || hint >= latest_order_hint)) {
424
336
                    ref = j;
425
336
                    latest_order_hint = hint;
426
                }
427
            }
428
180
            if (ref >= 0) {
429
180
                ref_frame_idx[ref_frame - AV1_REF_FRAME_LAST] = ref;
430
180
                used_frame[ref] = 1;
431
            }
432
        }
433
    }
434
435
84
    ref = -1;
436
756
    for (i = 0; i < AV1_NUM_REF_FRAMES; i++) {
437
672
        int hint = shifted_order_hints[i];
438

672
        if (ref < 0 || hint < earliest_order_hint) {
439
150
            ref = i;
440
150
            earliest_order_hint = hint;
441
        }
442
    }
443
672
    for (i = 0; i < AV1_REFS_PER_FRAME; i++) {
444
588
        if (ref_frame_idx[i] < 0)
445
            ref_frame_idx[i] = ref;
446
588
        infer(ref_frame_idx[i], ref_frame_idx[i]);
447
    }
448
449
84
    return 0;
450
}
451
452
2268
static int FUNC(superres_params)(CodedBitstreamContext *ctx, RWContext *rw,
453
                                 AV1RawFrameHeader *current)
454
{
455
2268
    CodedBitstreamAV1Context  *priv = ctx->priv_data;
456
2268
    const AV1RawSequenceHeader *seq = priv->sequence_header;
457
    int denom, err;
458
459
2268
    if (seq->enable_superres)
460
        flag(use_superres);
461
    else
462
2268
        infer(use_superres, 0);
463
464
2268
    if (current->use_superres) {
465
        fb(3, coded_denom);
466
        denom = current->coded_denom + AV1_SUPERRES_DENOM_MIN;
467
    } else {
468
2268
        denom = AV1_SUPERRES_NUM;
469
    }
470
471
2268
    priv->upscaled_width = priv->frame_width;
472
2268
    priv->frame_width = (priv->upscaled_width * AV1_SUPERRES_NUM +
473
2268
                         denom / 2) / denom;
474
475
2268
    return 0;
476
}
477
478
2178
static int FUNC(frame_size)(CodedBitstreamContext *ctx, RWContext *rw,
479
                            AV1RawFrameHeader *current)
480
{
481
2178
    CodedBitstreamAV1Context  *priv = ctx->priv_data;
482
2178
    const AV1RawSequenceHeader *seq = priv->sequence_header;
483
    int err;
484
485
2178
    if (current->frame_size_override_flag) {
486
18
        fb(seq->frame_width_bits_minus_1 + 1,  frame_width_minus_1);
487
18
        fb(seq->frame_height_bits_minus_1 + 1, frame_height_minus_1);
488
489
18
        priv->frame_width  = current->frame_width_minus_1  + 1;
490
18
        priv->frame_height = current->frame_height_minus_1 + 1;
491
    } else {
492
2160
        priv->frame_width  = seq->max_frame_width_minus_1  + 1;
493
2160
        priv->frame_height = seq->max_frame_height_minus_1 + 1;
494
    }
495
496
2178
    CHECK(FUNC(superres_params)(ctx, rw, current));
497
498
2178
    return 0;
499
}
500
501
2178
static int FUNC(render_size)(CodedBitstreamContext *ctx, RWContext *rw,
502
                             AV1RawFrameHeader *current)
503
{
504
2178
    CodedBitstreamAV1Context *priv = ctx->priv_data;
505
    int err;
506
507
2178
    flag(render_and_frame_size_different);
508
509
2178
    if (current->render_and_frame_size_different) {
510
        fb(16, render_width_minus_1);
511
        fb(16, render_height_minus_1);
512
513
        priv->render_width  = current->render_width_minus_1  + 1;
514
        priv->render_height = current->render_height_minus_1 + 1;
515
    } else {
516
2178
        priv->render_width  = priv->upscaled_width;
517
2178
        priv->render_height = priv->frame_height;
518
    }
519
520
2178
    return 0;
521
}
522
523
90
static int FUNC(frame_size_with_refs)(CodedBitstreamContext *ctx, RWContext *rw,
524
                                      AV1RawFrameHeader *current)
525
{
526
90
    CodedBitstreamAV1Context *priv = ctx->priv_data;
527
    int i, err;
528
529
90
    for (i = 0; i < AV1_REFS_PER_FRAME; i++) {
530
90
        flags(found_ref[i], 1, i);
531
90
        if (current->found_ref[i]) {
532
90
            AV1ReferenceFrameState *ref =
533
90
                &priv->ref[current->ref_frame_idx[i]];
534
535
90
            if (!ref->valid) {
536
                av_log(ctx->log_ctx, AV_LOG_ERROR,
537
                       "Missing reference frame needed for frame size "
538
                       "(ref = %d, ref_frame_idx = %d).\n",
539
                       i, current->ref_frame_idx[i]);
540
                return AVERROR_INVALIDDATA;
541
            }
542
543
90
            priv->upscaled_width = ref->upscaled_width;
544
90
            priv->frame_width    = ref->frame_width;
545
90
            priv->frame_height   = ref->frame_height;
546
90
            priv->render_width   = ref->render_width;
547
90
            priv->render_height  = ref->render_height;
548
90
            break;
549
        }
550
    }
551
552
90
    if (i >= AV1_REFS_PER_FRAME) {
553
        CHECK(FUNC(frame_size)(ctx, rw, current));
554
        CHECK(FUNC(render_size)(ctx, rw, current));
555
    } else {
556
90
        CHECK(FUNC(superres_params)(ctx, rw, current));
557
    }
558
559
90
    return 0;
560
}
561
562
1914
static int FUNC(interpolation_filter)(CodedBitstreamContext *ctx, RWContext *rw,
563
                                      AV1RawFrameHeader *current)
564
{
565
    int err;
566
567
1914
    flag(is_filter_switchable);
568
1914
    if (current->is_filter_switchable)
569
1524
        infer(interpolation_filter,
570
              AV1_INTERPOLATION_FILTER_SWITCHABLE);
571
    else
572
390
        fb(2, interpolation_filter);
573
574
1914
    return 0;
575
}
576
577
2268
static int FUNC(tile_info)(CodedBitstreamContext *ctx, RWContext *rw,
578
                           AV1RawFrameHeader *current)
579
{
580
2268
    CodedBitstreamAV1Context  *priv = ctx->priv_data;
581
2268
    const AV1RawSequenceHeader *seq = priv->sequence_header;
582
    int mi_cols, mi_rows, sb_cols, sb_rows, sb_shift, sb_size;
583
    int max_tile_width_sb, max_tile_height_sb, max_tile_area_sb;
584
    int min_log2_tile_cols, max_log2_tile_cols, max_log2_tile_rows;
585
    int min_log2_tiles, min_log2_tile_rows;
586
    int i, err;
587
588
2268
    mi_cols = 2 * ((priv->frame_width  + 7) >> 3);
589
2268
    mi_rows = 2 * ((priv->frame_height + 7) >> 3);
590
591
5736
    sb_cols = seq->use_128x128_superblock ? ((mi_cols + 31) >> 5)
592
2268
                                          : ((mi_cols + 15) >> 4);
593
5736
    sb_rows = seq->use_128x128_superblock ? ((mi_rows + 31) >> 5)
594
2268
                                          : ((mi_rows + 15) >> 4);
595
596
2268
    sb_shift = seq->use_128x128_superblock ? 5 : 4;
597
2268
    sb_size  = sb_shift + 2;
598
599
2268
    max_tile_width_sb = AV1_MAX_TILE_WIDTH >> sb_size;
600
2268
    max_tile_area_sb  = AV1_MAX_TILE_AREA  >> (2 * sb_size);
601
602
2268
    min_log2_tile_cols = cbs_av1_tile_log2(max_tile_width_sb, sb_cols);
603
2268
    max_log2_tile_cols = cbs_av1_tile_log2(1, FFMIN(sb_cols, AV1_MAX_TILE_COLS));
604
2268
    max_log2_tile_rows = cbs_av1_tile_log2(1, FFMIN(sb_rows, AV1_MAX_TILE_ROWS));
605
2268
    min_log2_tiles = FFMAX(min_log2_tile_cols,
606
                           cbs_av1_tile_log2(max_tile_area_sb, sb_rows * sb_cols));
607
608
2268
    flag(uniform_tile_spacing_flag);
609
610
2268
    if (current->uniform_tile_spacing_flag) {
611
        int tile_width_sb, tile_height_sb;
612
613
2112
        increment(tile_cols_log2, min_log2_tile_cols, max_log2_tile_cols);
614
615
2112
        tile_width_sb = (sb_cols + (1 << current->tile_cols_log2) - 1) >>
616
2112
            current->tile_cols_log2;
617
2112
        current->tile_cols = (sb_cols + tile_width_sb - 1) / tile_width_sb;
618
619
2112
        min_log2_tile_rows = FFMAX(min_log2_tiles - current->tile_cols_log2, 0);
620
621
2112
        increment(tile_rows_log2, min_log2_tile_rows, max_log2_tile_rows);
622
623
2112
        tile_height_sb = (sb_rows + (1 << current->tile_rows_log2) - 1) >>
624
2112
            current->tile_rows_log2;
625
2112
        current->tile_rows = (sb_rows + tile_height_sb - 1) / tile_height_sb;
626
627
    } else {
628
        int widest_tile_sb, start_sb, size_sb, max_width, max_height;
629
630
156
        widest_tile_sb = 0;
631
632
156
        start_sb = 0;
633

312
        for (i = 0; start_sb < sb_cols && i < AV1_MAX_TILE_COLS; i++) {
634
156
            max_width = FFMIN(sb_cols - start_sb, max_tile_width_sb);
635
156
            ns(max_width, width_in_sbs_minus_1[i], 1, i);
636
156
            size_sb = current->width_in_sbs_minus_1[i] + 1;
637
156
            widest_tile_sb = FFMAX(size_sb, widest_tile_sb);
638
156
            start_sb += size_sb;
639
        }
640
156
        current->tile_cols_log2 = cbs_av1_tile_log2(1, i);
641
156
        current->tile_cols = i;
642
643
156
        if (min_log2_tiles > 0)
644
            max_tile_area_sb = (sb_rows * sb_cols) >> (min_log2_tiles + 1);
645
        else
646
156
            max_tile_area_sb = sb_rows * sb_cols;
647
156
        max_tile_height_sb = FFMAX(max_tile_area_sb / widest_tile_sb, 1);
648
649
156
        start_sb = 0;
650

780
        for (i = 0; start_sb < sb_rows && i < AV1_MAX_TILE_ROWS; i++) {
651
624
            max_height = FFMIN(sb_rows - start_sb, max_tile_height_sb);
652
624
            ns(max_height, height_in_sbs_minus_1[i], 1, i);
653
624
            size_sb = current->height_in_sbs_minus_1[i] + 1;
654
624
            start_sb += size_sb;
655
        }
656
156
        current->tile_rows_log2 = cbs_av1_tile_log2(1, i);
657
156
        current->tile_rows = i;
658
    }
659
660
2268
    if (current->tile_cols_log2 > 0 ||
661
1962
        current->tile_rows_log2 > 0) {
662
462
        fb(current->tile_cols_log2 + current->tile_rows_log2,
663
           context_update_tile_id);
664
462
        fb(2, tile_size_bytes_minus1);
665
    } else {
666
1806
        infer(context_update_tile_id, 0);
667
    }
668
669
2268
    priv->tile_cols = current->tile_cols;
670
2268
    priv->tile_rows = current->tile_rows;
671
672
2268
    return 0;
673
}
674
675
2268
static int FUNC(quantization_params)(CodedBitstreamContext *ctx, RWContext *rw,
676
                                     AV1RawFrameHeader *current)
677
{
678
2268
    CodedBitstreamAV1Context  *priv = ctx->priv_data;
679
2268
    const AV1RawSequenceHeader *seq = priv->sequence_header;
680
    int err;
681
682
2268
    fb(8, base_q_idx);
683
684

2268
    delta_q(delta_q_y_dc);
685
686
2268
    if (priv->num_planes > 1) {
687
2268
        if (seq->color_config.separate_uv_delta_q)
688
            flag(diff_uv_delta);
689
        else
690
2268
            infer(diff_uv_delta, 0);
691
692

2268
        delta_q(delta_q_u_dc);
693

2268
        delta_q(delta_q_u_ac);
694
695
2268
        if (current->diff_uv_delta) {
696
            delta_q(delta_q_v_dc);
697
            delta_q(delta_q_v_ac);
698
        } else {
699
2268
            infer(delta_q_v_dc, current->delta_q_u_dc);
700
2268
            infer(delta_q_v_ac, current->delta_q_u_ac);
701
        }
702
    } else {
703
        infer(delta_q_u_dc, 0);
704
        infer(delta_q_u_ac, 0);
705
        infer(delta_q_v_dc, 0);
706
        infer(delta_q_v_ac, 0);
707
    }
708
709
2268
    flag(using_qmatrix);
710
2268
    if (current->using_qmatrix) {
711
        fb(4, qm_y);
712
        fb(4, qm_u);
713
        if (seq->color_config.separate_uv_delta_q)
714
            fb(4, qm_v);
715
        else
716
            infer(qm_v, current->qm_u);
717
    }
718
719
2268
    return 0;
720
}
721
722
2268
static int FUNC(segmentation_params)(CodedBitstreamContext *ctx, RWContext *rw,
723
                                     AV1RawFrameHeader *current)
724
{
725
    static const uint8_t bits[AV1_SEG_LVL_MAX] = { 8, 6, 6, 6, 6, 3, 0, 0 };
726
    static const uint8_t sign[AV1_SEG_LVL_MAX] = { 1, 1, 1, 1, 1, 0, 0, 0 };
727
    int i, j, err;
728
729
2268
    flag(segmentation_enabled);
730
731
2268
    if (current->segmentation_enabled) {
732
        if (current->primary_ref_frame == AV1_PRIMARY_REF_NONE) {
733
            infer(segmentation_update_map,      1);
734
            infer(segmentation_temporal_update, 0);
735
            infer(segmentation_update_data,     1);
736
        } else {
737
            flag(segmentation_update_map);
738
            if (current->segmentation_update_map)
739
                flag(segmentation_temporal_update);
740
            else
741
                infer(segmentation_temporal_update, 0);
742
            flag(segmentation_update_data);
743
        }
744
745
        if (current->segmentation_update_data) {
746
            for (i = 0; i < AV1_MAX_SEGMENTS; i++) {
747
                for (j = 0; j < AV1_SEG_LVL_MAX; j++) {
748
                    flags(feature_enabled[i][j], 2, i, j);
749
750
                    if (current->feature_enabled[i][j] && bits[j] > 0) {
751
                        if (sign[j])
752
                            sus(1 + bits[j], feature_value[i][j], 2, i, j);
753
                        else
754
                            fbs(bits[j], feature_value[i][j], 2, i, j);
755
                    } else {
756
                        infer(feature_value[i][j], 0);
757
                    }
758
                }
759
            }
760
        }
761
    } else {
762
20412
        for (i = 0; i < AV1_MAX_SEGMENTS; i++) {
763
163296
            for (j = 0; j < AV1_SEG_LVL_MAX; j++) {
764
145152
                infer(feature_enabled[i][j], 0);
765
145152
                infer(feature_value[i][j],   0);
766
            }
767
        }
768
    }
769
770
2268
    return 0;
771
}
772
773
2268
static int FUNC(delta_q_params)(CodedBitstreamContext *ctx, RWContext *rw,
774
                                AV1RawFrameHeader *current)
775
{
776
    int err;
777
778
2268
    if (current->base_q_idx > 0)
779
2268
        flag(delta_q_present);
780
    else
781
        infer(delta_q_present, 0);
782
783
2268
    if (current->delta_q_present)
784
24
        fb(2, delta_q_res);
785
786
2268
    return 0;
787
}
788
789
2268
static int FUNC(delta_lf_params)(CodedBitstreamContext *ctx, RWContext *rw,
790
                                 AV1RawFrameHeader *current)
791
{
792
    int err;
793
794
2268
    if (current->delta_q_present) {
795
24
        if (!current->allow_intrabc)
796
24
            flag(delta_lf_present);
797
        else
798
            infer(delta_lf_present, 0);
799
24
        if (current->delta_lf_present) {
800
            fb(2, delta_lf_res);
801
            flag(delta_lf_multi);
802
        } else {
803
24
            infer(delta_lf_res,   0);
804
24
            infer(delta_lf_multi, 0);
805
        }
806
    } else {
807
2244
        infer(delta_lf_present, 0);
808
2244
        infer(delta_lf_res,     0);
809
2244
        infer(delta_lf_multi,   0);
810
    }
811
812
2268
    return 0;
813
}
814
815
2268
static int FUNC(loop_filter_params)(CodedBitstreamContext *ctx, RWContext *rw,
816
                                    AV1RawFrameHeader *current)
817
{
818
2268
    CodedBitstreamAV1Context *priv = ctx->priv_data;
819
    int i, err;
820
821

2268
    if (priv->coded_lossless || current->allow_intrabc) {
822
        infer(loop_filter_level[0], 0);
823
        infer(loop_filter_level[1], 0);
824
        infer(loop_filter_ref_deltas[AV1_REF_FRAME_INTRA],    1);
825
        infer(loop_filter_ref_deltas[AV1_REF_FRAME_LAST],     0);
826
        infer(loop_filter_ref_deltas[AV1_REF_FRAME_LAST2],    0);
827
        infer(loop_filter_ref_deltas[AV1_REF_FRAME_LAST3],    0);
828
        infer(loop_filter_ref_deltas[AV1_REF_FRAME_BWDREF],   0);
829
        infer(loop_filter_ref_deltas[AV1_REF_FRAME_GOLDEN],  -1);
830
        infer(loop_filter_ref_deltas[AV1_REF_FRAME_ALTREF],  -1);
831
        infer(loop_filter_ref_deltas[AV1_REF_FRAME_ALTREF2], -1);
832
        for (i = 0; i < 2; i++)
833
            infer(loop_filter_mode_deltas[i], 0);
834
        return 0;
835
    }
836
837
2268
    fb(6, loop_filter_level[0]);
838
2268
    fb(6, loop_filter_level[1]);
839
840
2268
    if (priv->num_planes > 1) {
841
2268
        if (current->loop_filter_level[0] ||
842
1140
            current->loop_filter_level[1]) {
843
1152
            fb(6, loop_filter_level[2]);
844
1152
            fb(6, loop_filter_level[3]);
845
        }
846
    }
847
848
2268
    fb(3, loop_filter_sharpness);
849
850
2268
    flag(loop_filter_delta_enabled);
851
2268
    if (current->loop_filter_delta_enabled) {
852
2268
        flag(loop_filter_delta_update);
853
2268
        if (current->loop_filter_delta_update) {
854
5616
            for (i = 0; i < AV1_TOTAL_REFS_PER_FRAME; i++) {
855
4992
                flags(update_ref_delta[i], 1, i);
856
4992
                if (current->update_ref_delta[i])
857
                    sus(1 + 6, loop_filter_ref_deltas[i], 1, i);
858
            }
859
1872
            for (i = 0; i < 2; i++) {
860
1248
                flags(update_mode_delta[i], 1, i);
861
1248
                if (current->update_mode_delta[i])
862
                    sus(1 + 6, loop_filter_mode_deltas[i], 1, i);
863
            }
864
        }
865
    }
866
867
2268
    return 0;
868
}
869
870
2268
static int FUNC(cdef_params)(CodedBitstreamContext *ctx, RWContext *rw,
871
                             AV1RawFrameHeader *current)
872
{
873
2268
    CodedBitstreamAV1Context  *priv = ctx->priv_data;
874
2268
    const AV1RawSequenceHeader *seq = priv->sequence_header;
875
    int i, err;
876
877

2268
    if (priv->coded_lossless || current->allow_intrabc ||
878
2268
        !seq->enable_cdef) {
879
258
        infer(cdef_damping_minus_3, 0);
880
258
        infer(cdef_bits, 0);
881
258
        infer(cdef_y_pri_strength[0],  0);
882
258
        infer(cdef_y_sec_strength[0],  0);
883
258
        infer(cdef_uv_pri_strength[0], 0);
884
258
        infer(cdef_uv_sec_strength[0], 0);
885
886
258
        return 0;
887
    }
888
889
2010
    fb(2, cdef_damping_minus_3);
890
2010
    fb(2, cdef_bits);
891
892
9900
    for (i = 0; i < (1 << current->cdef_bits); i++) {
893
7890
        fbs(4, cdef_y_pri_strength[i], 1, i);
894
7890
        fbs(2, cdef_y_sec_strength[i], 1, i);
895
896
7890
        if (priv->num_planes > 1) {
897
7890
            fbs(4, cdef_uv_pri_strength[i], 1, i);
898
7890
            fbs(2, cdef_uv_sec_strength[i], 1, i);
899
        }
900
    }
901
902
2010
    return 0;
903
}
904
905
2268
static int FUNC(lr_params)(CodedBitstreamContext *ctx, RWContext *rw,
906
                           AV1RawFrameHeader *current)
907
{
908
2268
    CodedBitstreamAV1Context  *priv = ctx->priv_data;
909
2268
    const AV1RawSequenceHeader *seq = priv->sequence_header;
910
    int uses_lr,  uses_chroma_lr;
911
    int i, err;
912
913

2268
    if (priv->all_lossless || current->allow_intrabc ||
914
2268
        !seq->enable_restoration) {
915
192
        return 0;
916
    }
917
918
2076
    uses_lr = uses_chroma_lr = 0;
919
8304
    for (i = 0; i < priv->num_planes; i++) {
920
6228
        fbs(2, lr_type[i], 1, i);
921
922
6228
        if (current->lr_type[i] != 0) {
923
1638
            uses_lr = 1;
924
1638
            if (i > 0)
925
624
                uses_chroma_lr = 1;
926
        }
927
    }
928
929
2076
    if (uses_lr) {
930
1140
        if (seq->use_128x128_superblock)
931
732
            increment(lr_unit_shift, 1, 2);
932
        else
933
408
            increment(lr_unit_shift, 0, 2);
934
935
1140
        if(seq->color_config.subsampling_x &&
936

1140
           seq->color_config.subsampling_y && uses_chroma_lr) {
937
408
            fb(1, lr_uv_shift);
938
        } else {
939
732
            infer(lr_uv_shift, 0);
940
        }
941
    }
942
943
2076
    return 0;
944
}
945
946
2268
static int FUNC(read_tx_mode)(CodedBitstreamContext *ctx, RWContext *rw,
947
                              AV1RawFrameHeader *current)
948
{
949
2268
    CodedBitstreamAV1Context *priv = ctx->priv_data;
950
    int err;
951
952
2268
    if (priv->coded_lossless)
953
        infer(tx_mode, 0);
954
    else
955
2268
        increment(tx_mode, 1, 2);
956
957
2268
    return 0;
958
}
959
960
2268
static int FUNC(frame_reference_mode)(CodedBitstreamContext *ctx, RWContext *rw,
961
                                      AV1RawFrameHeader *current)
962
{
963
    int err;
964
965
2268
    if (current->frame_type == AV1_FRAME_INTRA_ONLY ||
966
2268
        current->frame_type == AV1_FRAME_KEY)
967
354
        infer(reference_select, 0);
968
    else
969
1914
        flag(reference_select);
970
971
2268
    return 0;
972
}
973
974
2268
static int FUNC(skip_mode_params)(CodedBitstreamContext *ctx, RWContext *rw,
975
                                  AV1RawFrameHeader *current)
976
{
977
2268
    CodedBitstreamAV1Context  *priv = ctx->priv_data;
978
2268
    const AV1RawSequenceHeader *seq = priv->sequence_header;
979
    int skip_mode_allowed;
980
    int err;
981
982
2268
    if (current->frame_type == AV1_FRAME_KEY ||
983
1914
        current->frame_type == AV1_FRAME_INTRA_ONLY ||
984

1914
        !current->reference_select || !seq->enable_order_hint) {
985
1218
        skip_mode_allowed = 0;
986
    } else {
987
        int forward_idx,  backward_idx;
988
        int forward_hint, backward_hint;
989
        int ref_hint, dist, i;
990
991
1050
        forward_idx  = -1;
992
1050
        backward_idx = -1;
993
8400
        for (i = 0; i < AV1_REFS_PER_FRAME; i++) {
994
7350
            ref_hint = priv->ref[current->ref_frame_idx[i]].order_hint;
995
7350
            dist = cbs_av1_get_relative_dist(seq, ref_hint,
996
7350
                                             current->order_hint);
997
7350
            if (dist < 0) {
998

11346
                if (forward_idx < 0 ||
999
5148
                    cbs_av1_get_relative_dist(seq, ref_hint,
1000
                                              forward_hint) > 0) {
1001
1192
                    forward_idx  = i;
1002
1192
                    forward_hint = ref_hint;
1003
                }
1004
1152
            } else if (dist > 0) {
1005

1614
                if (backward_idx < 0 ||
1006
504
                    cbs_av1_get_relative_dist(seq, ref_hint,
1007
                                              backward_hint) < 0) {
1008
606
                    backward_idx  = i;
1009
606
                    backward_hint = ref_hint;
1010
                }
1011
            }
1012
        }
1013
1014
1050
        if (forward_idx < 0) {
1015
            skip_mode_allowed = 0;
1016
1050
        } else if (backward_idx >= 0) {
1017
606
            skip_mode_allowed = 1;
1018
            // Frames for skip mode are forward_idx and backward_idx.
1019
        } else {
1020
            int second_forward_idx;
1021
            int second_forward_hint;
1022
1023
444
            second_forward_idx = -1;
1024
3552
            for (i = 0; i < AV1_REFS_PER_FRAME; i++) {
1025
3108
                ref_hint = priv->ref[current->ref_frame_idx[i]].order_hint;
1026
3108
                if (cbs_av1_get_relative_dist(seq, ref_hint,
1027
                                              forward_hint) < 0) {
1028

4544
                    if (second_forward_idx < 0 ||
1029
2060
                        cbs_av1_get_relative_dist(seq, ref_hint,
1030
                                                  second_forward_hint) > 0) {
1031
448
                        second_forward_idx  = i;
1032
448
                        second_forward_hint = ref_hint;
1033
                    }
1034
                }
1035
            }
1036
1037
444
            if (second_forward_idx < 0) {
1038
20
                skip_mode_allowed = 0;
1039
            } else {
1040
424
                skip_mode_allowed = 1;
1041
                // Frames for skip mode are forward_idx and second_forward_idx.
1042
            }
1043
        }
1044
    }
1045
1046
2268
    if (skip_mode_allowed)
1047
1030
        flag(skip_mode_present);
1048
    else
1049
1238
        infer(skip_mode_present, 0);
1050
1051
2268
    return 0;
1052
}
1053
1054
456
static int FUNC(global_motion_param)(CodedBitstreamContext *ctx, RWContext *rw,
1055
                                     AV1RawFrameHeader *current,
1056
                                     int type, int ref, int idx)
1057
{
1058
    uint32_t abs_bits, prec_bits, num_syms;
1059
    int err;
1060
1061
456
    if (idx < 2) {
1062
228
        if (type == AV1_WARP_MODEL_TRANSLATION) {
1063
            abs_bits  = AV1_GM_ABS_TRANS_ONLY_BITS  - !current->allow_high_precision_mv;
1064
            prec_bits = AV1_GM_TRANS_ONLY_PREC_BITS - !current->allow_high_precision_mv;
1065
        } else {
1066
228
            abs_bits  = AV1_GM_ABS_TRANS_BITS;
1067
228
            prec_bits = AV1_GM_TRANS_PREC_BITS;
1068
        }
1069
    } else {
1070
228
        abs_bits  = AV1_GM_ABS_ALPHA_BITS;
1071
228
        prec_bits = AV1_GM_ALPHA_PREC_BITS;
1072
    }
1073
1074
456
    num_syms = 2 * (1 << abs_bits) + 1;
1075
456
    subexp(gm_params[ref][idx], num_syms, 2, ref, idx);
1076
1077
    // Actual gm_params value is not reconstructed here.
1078
    (void)prec_bits;
1079
1080
456
    return 0;
1081
}
1082
1083
2268
static int FUNC(global_motion_params)(CodedBitstreamContext *ctx, RWContext *rw,
1084
                                      AV1RawFrameHeader *current)
1085
{
1086
    int ref, type;
1087
    int err;
1088
1089
2268
    if (current->frame_type == AV1_FRAME_KEY ||
1090
1914
        current->frame_type == AV1_FRAME_INTRA_ONLY)
1091
354
        return 0;
1092
1093
15312
    for (ref = AV1_REF_FRAME_LAST; ref <= AV1_REF_FRAME_ALTREF; ref++) {
1094
13398
        flags(is_global[ref], 1, ref);
1095
13398
        if (current->is_global[ref]) {
1096
114
            flags(is_rot_zoom[ref], 1, ref);
1097
114
            if (current->is_rot_zoom[ref]) {
1098
114
                type = AV1_WARP_MODEL_ROTZOOM;
1099
            } else {
1100
                flags(is_translation[ref], 1, ref);
1101
                type = current->is_translation[ref] ? AV1_WARP_MODEL_TRANSLATION
1102
                                                    : AV1_WARP_MODEL_AFFINE;
1103
            }
1104
        } else {
1105
13284
            type = AV1_WARP_MODEL_IDENTITY;
1106
        }
1107
1108
13398
        if (type >= AV1_WARP_MODEL_ROTZOOM) {
1109
114
            CHECK(FUNC(global_motion_param)(ctx, rw, current, type, ref, 2));
1110
114
            CHECK(FUNC(global_motion_param)(ctx, rw, current, type, ref, 3));
1111
114
            if (type == AV1_WARP_MODEL_AFFINE) {
1112
                CHECK(FUNC(global_motion_param)(ctx, rw, current, type, ref, 4));
1113
                CHECK(FUNC(global_motion_param)(ctx, rw, current, type, ref, 5));
1114
            } else {
1115
                // gm_params[ref][4] = -gm_params[ref][3]
1116
                // gm_params[ref][5] =  gm_params[ref][2]
1117
            }
1118
        }
1119
13398
        if (type >= AV1_WARP_MODEL_TRANSLATION) {
1120
114
            CHECK(FUNC(global_motion_param)(ctx, rw, current, type, ref, 0));
1121
114
            CHECK(FUNC(global_motion_param)(ctx, rw, current, type, ref, 1));
1122
        }
1123
    }
1124
1125
1914
    return 0;
1126
}
1127
1128
2268
static int FUNC(film_grain_params)(CodedBitstreamContext *ctx, RWContext *rw,
1129
                                   AV1RawFrameHeader *current)
1130
{
1131
2268
    CodedBitstreamAV1Context  *priv = ctx->priv_data;
1132
2268
    const AV1RawSequenceHeader *seq = priv->sequence_header;
1133
    int num_pos_luma, num_pos_chroma;
1134
    int i, err;
1135
1136
2268
    if (!seq->film_grain_params_present ||
1137

132
        (!current->show_frame && !current->showable_frame))
1138
2148
        return 0;
1139
1140
120
    flag(apply_grain);
1141
1142
120
    if (!current->apply_grain)
1143
        return 0;
1144
1145
120
    fb(16, grain_seed);
1146
1147
120
    if (current->frame_type == AV1_FRAME_INTER)
1148
108
        flag(update_grain);
1149
    else
1150
12
        infer(update_grain, 1);
1151
1152
120
    if (!current->update_grain) {
1153
        fb(3, film_grain_params_ref_idx);
1154
        return 0;
1155
    }
1156
1157
120
    fc(4, num_y_points, 0, 14);
1158
948
    for (i = 0; i < current->num_y_points; i++) {
1159

828
        fcs(8, point_y_value[i],
1160
            i ? current->point_y_value[i - 1] + 1 : 0,
1161
            MAX_UINT_BITS(8) - (current->num_y_points - i - 1),
1162
            1, i);
1163
828
        fbs(8, point_y_scaling[i], 1, i);
1164
    }
1165
1166
120
    if (seq->color_config.mono_chrome)
1167
        infer(chroma_scaling_from_luma, 0);
1168
    else
1169
120
        flag(chroma_scaling_from_luma);
1170
1171
120
    if (seq->color_config.mono_chrome ||
1172
120
        current->chroma_scaling_from_luma ||
1173
120
        (seq->color_config.subsampling_x == 1 &&
1174
120
         seq->color_config.subsampling_y == 1 &&
1175
120
         current->num_y_points == 0)) {
1176
        infer(num_cb_points, 0);
1177
        infer(num_cr_points, 0);
1178
    } else {
1179
120
        fc(4, num_cb_points, 0, 10);
1180
588
        for (i = 0; i < current->num_cb_points; i++) {
1181

468
            fcs(8, point_cb_value[i],
1182
                i ? current->point_cb_value[i - 1] + 1 : 0,
1183
                MAX_UINT_BITS(8) - (current->num_cb_points - i - 1),
1184
                1, i);
1185
468
            fbs(8, point_cb_scaling[i], 1, i);
1186
        }
1187
120
        fc(4, num_cr_points, 0, 10);
1188
600
        for (i = 0; i < current->num_cr_points; i++) {
1189

480
            fcs(8, point_cr_value[i],
1190
                i ? current->point_cr_value[i - 1] + 1 : 0,
1191
                MAX_UINT_BITS(8) - (current->num_cr_points - i - 1),
1192
                1, i);
1193
480
            fbs(8, point_cr_scaling[i], 1, i);
1194
        }
1195
    }
1196
1197
120
    fb(2, grain_scaling_minus_8);
1198
120
    fb(2, ar_coeff_lag);
1199
120
    num_pos_luma = 2 * current->ar_coeff_lag * (current->ar_coeff_lag + 1);
1200
120
    if (current->num_y_points) {
1201
120
        num_pos_chroma = num_pos_luma + 1;
1202
3000
        for (i = 0; i < num_pos_luma; i++)
1203
2880
            fbs(8, ar_coeffs_y_plus_128[i], 1, i);
1204
    } else {
1205
        num_pos_chroma = num_pos_luma;
1206
    }
1207

120
    if (current->chroma_scaling_from_luma || current->num_cb_points) {
1208
3120
        for (i = 0; i < num_pos_chroma; i++)
1209
3000
            fbs(8, ar_coeffs_cb_plus_128[i], 1, i);
1210
    }
1211

120
    if (current->chroma_scaling_from_luma || current->num_cr_points) {
1212
3120
        for (i = 0; i < num_pos_chroma; i++)
1213
3000
            fbs(8, ar_coeffs_cr_plus_128[i], 1, i);
1214
    }
1215
120
    fb(2, ar_coeff_shift_minus_6);
1216
120
    fb(2, grain_scale_shift);
1217
120
    if (current->num_cb_points) {
1218
120
        fb(8, cb_mult);
1219
120
        fb(8, cb_luma_mult);
1220
120
        fb(9, cb_offset);
1221
    }
1222
120
    if (current->num_cr_points) {
1223
120
        fb(8, cr_mult);
1224
120
        fb(8, cr_luma_mult);
1225
120
        fb(9, cr_offset);
1226
    }
1227
1228
120
    flag(overlap_flag);
1229
120
    flag(clip_to_restricted_range);
1230
1231
120
    return 0;
1232
}
1233
1234
2556
static int FUNC(uncompressed_header)(CodedBitstreamContext *ctx, RWContext *rw,
1235
                                     AV1RawFrameHeader *current)
1236
{
1237
2556
    CodedBitstreamAV1Context *priv = ctx->priv_data;
1238
    const AV1RawSequenceHeader *seq;
1239
    int id_len, diff_len, all_frames, frame_is_intra, order_hint_bits;
1240
    int i, err;
1241
1242
2556
    if (!priv->sequence_header) {
1243
        av_log(ctx->log_ctx, AV_LOG_ERROR, "No sequence header available: "
1244
               "unable to decode frame header.\n");
1245
        return AVERROR_INVALIDDATA;
1246
    }
1247
2556
    seq = priv->sequence_header;
1248
1249
2556
    id_len = seq->additional_frame_id_length_minus_1 +
1250
2556
             seq->delta_frame_id_length_minus_2 + 3;
1251
2556
    all_frames = (1 << AV1_NUM_REF_FRAMES) - 1;
1252
1253
2556
    if (seq->reduced_still_picture_header) {
1254
        infer(show_existing_frame, 0);
1255
        infer(frame_type,     AV1_FRAME_KEY);
1256
        infer(show_frame,     1);
1257
        infer(showable_frame, 0);
1258
        frame_is_intra = 1;
1259
1260
    } else {
1261
2556
        flag(show_existing_frame);
1262
1263
2556
        if (current->show_existing_frame) {
1264
            AV1ReferenceFrameState *frame;
1265
1266
288
            fb(3, frame_to_show_map_idx);
1267
288
            frame = &priv->ref[current->frame_to_show_map_idx];
1268
1269
288
            if (seq->decoder_model_info_present_flag &&
1270
42
                !seq->timing_info.equal_picture_interval) {
1271
42
                fb(seq->decoder_model_info.frame_presentation_time_length_minus_1 + 1,
1272
                   frame_presentation_time);
1273
            }
1274
1275
288
            if (seq->frame_id_numbers_present_flag)
1276
                fb(id_len, display_frame_id);
1277
1278
288
            if (frame->frame_type == AV1_FRAME_KEY)
1279
                infer(refresh_frame_flags, all_frames);
1280
            else
1281
288
                infer(refresh_frame_flags, 0);
1282
1283
288
            return 0;
1284
        }
1285
1286
2268
        fb(2, frame_type);
1287
4536
        frame_is_intra = (current->frame_type == AV1_FRAME_INTRA_ONLY ||
1288
2268
                          current->frame_type == AV1_FRAME_KEY);
1289
1290
2268
        flag(show_frame);
1291
2268
        if (current->show_frame &&
1292
1854
            seq->decoder_model_info_present_flag &&
1293
486
            !seq->timing_info.equal_picture_interval) {
1294
486
            fb(seq->decoder_model_info.frame_presentation_time_length_minus_1 + 1,
1295
               frame_presentation_time);
1296
        }
1297
2268
        if (current->show_frame)
1298
1854
            infer(showable_frame, current->frame_type != AV1_FRAME_KEY);
1299
        else
1300
414
            flag(showable_frame);
1301
1302
2268
        if (current->frame_type == AV1_FRAME_SWITCH ||
1303

2262
            (current->frame_type == AV1_FRAME_KEY && current->show_frame))
1304
360
            infer(error_resilient_mode, 1);
1305
        else
1306
1908
            flag(error_resilient_mode);
1307
    }
1308
1309

2268
    if (current->frame_type == AV1_FRAME_KEY && current->show_frame) {
1310
3186
        for (i = 0; i < AV1_NUM_REF_FRAMES; i++) {
1311
2832
            priv->ref[i].valid = 0;
1312
2832
            priv->ref[i].order_hint = 0;
1313
        }
1314
    }
1315
1316
2268
    flag(disable_cdf_update);
1317
1318
2268
    if (seq->seq_force_screen_content_tools ==
1319
        AV1_SELECT_SCREEN_CONTENT_TOOLS) {
1320
2268
        flag(allow_screen_content_tools);
1321
    } else {
1322
        infer(allow_screen_content_tools,
1323
              seq->seq_force_screen_content_tools);
1324
    }
1325
2268
    if (current->allow_screen_content_tools) {
1326
66
        if (seq->seq_force_integer_mv == AV1_SELECT_INTEGER_MV)
1327
66
            flag(force_integer_mv);
1328
        else
1329
            infer(force_integer_mv, seq->seq_force_integer_mv);
1330
    } else {
1331
2202
        infer(force_integer_mv, 0);
1332
    }
1333
1334
2268
    if (seq->frame_id_numbers_present_flag) {
1335
        fb(id_len, current_frame_id);
1336
1337
        diff_len = seq->delta_frame_id_length_minus_2 + 2;
1338
        for (i = 0; i < AV1_NUM_REF_FRAMES; i++) {
1339
            if (current->current_frame_id > (1 << diff_len)) {
1340
                if (priv->ref[i].frame_id > current->current_frame_id ||
1341
                    priv->ref[i].frame_id < (current->current_frame_id -
1342
                                             (1 << diff_len)))
1343
                    priv->ref[i].valid = 0;
1344
            } else {
1345
                if (priv->ref[i].frame_id > current->current_frame_id &&
1346
                    priv->ref[i].frame_id < ((1 << id_len) +
1347
                                             current->current_frame_id -
1348
                                             (1 << diff_len)))
1349
                    priv->ref[i].valid = 0;
1350
            }
1351
        }
1352
    } else {
1353
2268
        infer(current_frame_id, 0);
1354
    }
1355
1356
2268
    if (current->frame_type == AV1_FRAME_SWITCH)
1357
6
        infer(frame_size_override_flag, 1);
1358
2262
    else if(seq->reduced_still_picture_header)
1359
        infer(frame_size_override_flag, 0);
1360
    else
1361
2262
        flag(frame_size_override_flag);
1362
1363
2268
    order_hint_bits =
1364
2268
        seq->enable_order_hint ? seq->order_hint_bits_minus_1 + 1 : 0;
1365
2268
    if (order_hint_bits > 0)
1366
2268
        fb(order_hint_bits, order_hint);
1367
    else
1368
        infer(order_hint, 0);
1369
1370

2268
    if (frame_is_intra || current->error_resilient_mode)
1371
360
        infer(primary_ref_frame, AV1_PRIMARY_REF_NONE);
1372
    else
1373
1908
        fb(3, primary_ref_frame);
1374
1375
2268
    if (seq->decoder_model_info_present_flag) {
1376
582
        flag(buffer_removal_time_present_flag);
1377
582
        if (current->buffer_removal_time_present_flag) {
1378
1164
            for (i = 0; i <= seq->operating_points_cnt_minus_1; i++) {
1379
582
                if (seq->decoder_model_present_for_this_op[i]) {
1380
582
                    int op_pt_idc = seq->operating_point_idc[i];
1381
582
                    int in_temporal_layer = (op_pt_idc >>  priv->temporal_id    ) & 1;
1382
582
                    int in_spatial_layer  = (op_pt_idc >> (priv->spatial_id + 8)) & 1;
1383

582
                    if (seq->operating_point_idc[i] == 0 ||
1384
                        in_temporal_layer || in_spatial_layer) {
1385
582
                        fbs(seq->decoder_model_info.buffer_removal_time_length_minus_1 + 1,
1386
                            buffer_removal_time[i], 1, i);
1387
                    }
1388
                }
1389
            }
1390
        }
1391
    }
1392
1393
2268
    if (current->frame_type == AV1_FRAME_SWITCH ||
1394

2262
        (current->frame_type == AV1_FRAME_KEY && current->show_frame))
1395
360
        infer(refresh_frame_flags, all_frames);
1396
    else
1397
1908
        fb(8, refresh_frame_flags);
1398
1399

2268
    if (!frame_is_intra || current->refresh_frame_flags != all_frames) {
1400

1914
        if (current->error_resilient_mode && seq->enable_order_hint) {
1401
54
            for (i = 0; i < AV1_NUM_REF_FRAMES; i++) {
1402
48
                fbs(order_hint_bits, ref_order_hint[i], 1, i);
1403
48
                if (current->ref_order_hint[i] != priv->ref[i].order_hint)
1404
                    priv->ref[i].valid = 0;
1405
            }
1406
        }
1407
    }
1408
1409
2268
    if (current->frame_type == AV1_FRAME_KEY ||
1410
1914
        current->frame_type == AV1_FRAME_INTRA_ONLY) {
1411
354
        CHECK(FUNC(frame_size)(ctx, rw, current));
1412
354
        CHECK(FUNC(render_size)(ctx, rw, current));
1413
1414
354
        if (current->allow_screen_content_tools &&
1415
6
            priv->upscaled_width == priv->frame_width)
1416
6
            flag(allow_intrabc);
1417
        else
1418
348
            infer(allow_intrabc, 0);
1419
1420
    } else {
1421
1914
        if (!seq->enable_order_hint) {
1422
            infer(frame_refs_short_signaling, 0);
1423
        } else {
1424
1914
            flag(frame_refs_short_signaling);
1425
1914
            if (current->frame_refs_short_signaling) {
1426
84
                fb(3, last_frame_idx);
1427
84
                fb(3, golden_frame_idx);
1428
84
                CHECK(FUNC(set_frame_refs)(ctx, rw, current));
1429
            }
1430
        }
1431
1432
15312
        for (i = 0; i < AV1_REFS_PER_FRAME; i++) {
1433
13398
            if (!current->frame_refs_short_signaling)
1434
12810
                fbs(3, ref_frame_idx[i], 1, i);
1435
13398
            if (seq->frame_id_numbers_present_flag) {
1436
                fbs(seq->delta_frame_id_length_minus_2 + 2,
1437
                    delta_frame_id_minus1[i], 1, i);
1438
            }
1439
        }
1440
1441
1914
        if (current->frame_size_override_flag &&
1442
96
            !current->error_resilient_mode) {
1443
90
            CHECK(FUNC(frame_size_with_refs)(ctx, rw, current));
1444
        } else {
1445
1824
            CHECK(FUNC(frame_size)(ctx, rw, current));
1446
1824
            CHECK(FUNC(render_size)(ctx, rw, current));
1447
        }
1448
1449
1914
        if (current->force_integer_mv)
1450
            infer(allow_high_precision_mv, 0);
1451
        else
1452
1914
            flag(allow_high_precision_mv);
1453
1454
1914
        CHECK(FUNC(interpolation_filter)(ctx, rw, current));
1455
1456
1914
        flag(is_motion_mode_switchable);
1457
1458
1914
        if (current->error_resilient_mode ||
1459
1908
            !seq->enable_ref_frame_mvs)
1460
186
            infer(use_ref_frame_mvs, 0);
1461
        else
1462
1728
            flag(use_ref_frame_mvs);
1463
1464
1914
        infer(allow_intrabc, 0);
1465
    }
1466
1467
    if (!frame_is_intra) {
1468
        // Derive reference frame sign biases.
1469
    }
1470
1471

2268
    if (seq->reduced_still_picture_header || current->disable_cdf_update)
1472
        infer(disable_frame_end_update_cdf, 1);
1473
    else
1474
2268
        flag(disable_frame_end_update_cdf);
1475
1476
2268
    if (current->primary_ref_frame == AV1_PRIMARY_REF_NONE) {
1477
        // Init non-coeff CDFs.
1478
        // Setup past independence.
1479
    } else {
1480
        // Load CDF tables from previous frame.
1481
        // Load params from previous frame.
1482
    }
1483
1484
2268
    if (current->use_ref_frame_mvs) {
1485
        // Perform motion field estimation process.
1486
    }
1487
1488
2268
    CHECK(FUNC(tile_info)(ctx, rw, current));
1489
1490
2268
    CHECK(FUNC(quantization_params)(ctx, rw, current));
1491
1492
2268
    CHECK(FUNC(segmentation_params)(ctx, rw, current));
1493
1494
2268
    CHECK(FUNC(delta_q_params)(ctx, rw, current));
1495
1496
2268
    CHECK(FUNC(delta_lf_params)(ctx, rw, current));
1497
1498
    // Init coeff CDFs / load previous segments.
1499
1500
2268
    priv->coded_lossless = 1;
1501
20412
    for (i = 0; i < AV1_MAX_SEGMENTS; i++) {
1502
        int qindex;
1503
18144
        if (current->feature_enabled[i][AV1_SEG_LVL_ALT_Q]) {
1504
            qindex = (current->base_q_idx +
1505
                      current->feature_value[i][AV1_SEG_LVL_ALT_Q]);
1506
        } else {
1507
18144
            qindex = current->base_q_idx;
1508
        }
1509
18144
        qindex = av_clip_uintp2(qindex, 8);
1510
1511

18144
        if (qindex                || current->delta_q_y_dc ||
1512
            current->delta_q_u_ac || current->delta_q_u_dc ||
1513
            current->delta_q_v_ac || current->delta_q_v_dc) {
1514
18144
            priv->coded_lossless = 0;
1515
        }
1516
    }
1517
2268
    priv->all_lossless = priv->coded_lossless &&
1518
        priv->frame_width == priv->upscaled_width;
1519
1520
2268
    CHECK(FUNC(loop_filter_params)(ctx, rw, current));
1521
1522
2268
    CHECK(FUNC(cdef_params)(ctx, rw, current));
1523
1524
2268
    CHECK(FUNC(lr_params)(ctx, rw, current));
1525
1526
2268
    CHECK(FUNC(read_tx_mode)(ctx, rw, current));
1527
1528
2268
    CHECK(FUNC(frame_reference_mode)(ctx, rw, current));
1529
1530
2268
    CHECK(FUNC(skip_mode_params)(ctx, rw, current));
1531
1532

2268
    if (frame_is_intra || current->error_resilient_mode ||
1533
1908
        !seq->enable_warped_motion)
1534
360
        infer(allow_warped_motion, 0);
1535
    else
1536
1908
        flag(allow_warped_motion);
1537
1538
2268
    flag(reduced_tx_set);
1539
1540
2268
    CHECK(FUNC(global_motion_params)(ctx, rw, current));
1541
1542
2268
    CHECK(FUNC(film_grain_params)(ctx, rw, current));
1543
1544
20412
    for (i = 0; i < AV1_NUM_REF_FRAMES; i++) {
1545
18144
        if (current->refresh_frame_flags & (1 << i)) {
1546
4710
            priv->ref[i] = (AV1ReferenceFrameState) {
1547
                .valid          = 1,
1548
4710
                .frame_id       = current->current_frame_id,
1549
4710
                .upscaled_width = priv->upscaled_width,
1550
4710
                .frame_width    = priv->frame_width,
1551
4710
                .frame_height   = priv->frame_height,
1552
4710
                .render_width   = priv->render_width,
1553
4710
                .render_height  = priv->render_height,
1554
4710
                .frame_type     = current->frame_type,
1555
4710
                .subsampling_x  = seq->color_config.subsampling_x,
1556
4710
                .subsampling_y  = seq->color_config.subsampling_y,
1557
4710
                .bit_depth      = priv->bit_depth,
1558
4710
                .order_hint     = current->order_hint,
1559
            };
1560
        }
1561
    }
1562
1563
2268
    av_log(ctx->log_ctx, AV_LOG_DEBUG, "Frame %d:  size %dx%d  "
1564
           "upscaled %d  render %dx%d  subsample %dx%d  "
1565
2268
           "bitdepth %d  tiles %dx%d.\n", current->order_hint,
1566
           priv->frame_width, priv->frame_height, priv->upscaled_width,
1567
           priv->render_width, priv->render_height,
1568
2268
           seq->color_config.subsampling_x + 1,
1569
2268
           seq->color_config.subsampling_y + 1, priv->bit_depth,
1570
           priv->tile_rows, priv->tile_cols);
1571
1572
2268
    return 0;
1573
}
1574
1575
2556
static int FUNC(frame_header_obu)(CodedBitstreamContext *ctx, RWContext *rw,
1576
                                  AV1RawFrameHeader *current, int redundant,
1577
                                  AVBufferRef *rw_buffer_ref)
1578
{
1579
2556
    CodedBitstreamAV1Context *priv = ctx->priv_data;
1580
    int start_pos, fh_bits, fh_bytes, err;
1581
    uint8_t *fh_start;
1582
1583
2556
    if (priv->seen_frame_header) {
1584
        if (!redundant) {
1585
            av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid repeated "
1586
                   "frame header OBU.\n");
1587
            return AVERROR_INVALIDDATA;
1588
        } else {
1589
            GetBitContext fh;
1590
            size_t i, b;
1591
            uint32_t val;
1592
1593
            HEADER("Redundant Frame Header");
1594
1595
            av_assert0(priv->frame_header_ref && priv->frame_header);
1596
1597
            init_get_bits(&fh, priv->frame_header,
1598
                          priv->frame_header_size);
1599
            for (i = 0; i < priv->frame_header_size; i += 8) {
1600
                b = FFMIN(priv->frame_header_size - i, 8);
1601
                val = get_bits(&fh, b);
1602
                xf(b, frame_header_copy[i],
1603
                   val, val, val, 1, i / 8);
1604
            }
1605
        }
1606
    } else {
1607
2556
        if (redundant)
1608
            HEADER("Redundant Frame Header (used as Frame Header)");
1609
        else
1610
2556
            HEADER("Frame Header");
1611
1612
#ifdef READ
1613
1714
        start_pos = get_bits_count(rw);
1614
#else
1615
842
        start_pos = put_bits_count(rw);
1616
#endif
1617
1618
2556
        CHECK(FUNC(uncompressed_header)(ctx, rw, current));
1619
1620
2556
        if (current->show_existing_frame) {
1621
288
            priv->seen_frame_header = 0;
1622
        } else {
1623
2268
            priv->seen_frame_header = 1;
1624
1625
2268
            av_buffer_unref(&priv->frame_header_ref);
1626
1627
#ifdef READ
1628
1520
            fh_bits  = get_bits_count(rw) - start_pos;
1629
1520
            fh_start = (uint8_t*)rw->buffer + start_pos / 8;
1630
#else
1631
            // Need to flush the bitwriter so that we can copy its output,
1632
            // but use a copy so we don't affect the caller's structure.
1633
            {
1634
748
                PutBitContext tmp = *rw;
1635
748
                flush_put_bits(&tmp);
1636
            }
1637
1638
748
            fh_bits  = put_bits_count(rw) - start_pos;
1639
748
            fh_start = rw->buf + start_pos / 8;
1640
#endif
1641
2268
            fh_bytes = (fh_bits + 7) / 8;
1642
1643
2268
            priv->frame_header_size = fh_bits;
1644
1645
2268
            if (rw_buffer_ref) {
1646
1520
                priv->frame_header_ref = av_buffer_ref(rw_buffer_ref);
1647
1520
                if (!priv->frame_header_ref)
1648
                    return AVERROR(ENOMEM);
1649
1520
                priv->frame_header = fh_start;
1650
            } else {
1651
748
                priv->frame_header_ref =
1652
748
                    av_buffer_alloc(fh_bytes + AV_INPUT_BUFFER_PADDING_SIZE);
1653
748
                if (!priv->frame_header_ref)
1654
                    return AVERROR(ENOMEM);
1655
748
                priv->frame_header = priv->frame_header_ref->data;
1656
748
                memcpy(priv->frame_header, fh_start, fh_bytes);
1657
            }
1658
        }
1659
    }
1660
1661
2556
    return 0;
1662
}
1663
1664
2652
static int FUNC(tile_group_obu)(CodedBitstreamContext *ctx, RWContext *rw,
1665
                                AV1RawTileGroup *current)
1666
{
1667
2652
    CodedBitstreamAV1Context *priv = ctx->priv_data;
1668
    int num_tiles, tile_bits;
1669
    int err;
1670
1671
2652
    HEADER("Tile Group");
1672
1673
2652
    num_tiles = priv->tile_cols * priv->tile_rows;
1674
2652
    if (num_tiles > 1)
1675
846
        flag(tile_start_and_end_present_flag);
1676
    else
1677
1806
        infer(tile_start_and_end_present_flag, 0);
1678
1679

2652
    if (num_tiles == 1 || !current->tile_start_and_end_present_flag) {
1680
2028
        infer(tg_start, 0);
1681
2028
        infer(tg_end, num_tiles - 1);
1682
    } else {
1683
624
        tile_bits = cbs_av1_tile_log2(1, priv->tile_cols) +
1684
624
                    cbs_av1_tile_log2(1, priv->tile_rows);
1685
624
        fb(tile_bits, tg_start);
1686
624
        fb(tile_bits, tg_end);
1687
    }
1688
1689
2652
    CHECK(FUNC(byte_alignment)(ctx, rw));
1690
1691
    // Reset header for next frame.
1692
2652
    if (current->tg_end == num_tiles - 1)
1693
2268
        priv->seen_frame_header = 0;
1694
1695
    // Tile data follows.
1696
1697
2652
    return 0;
1698
}
1699
1700
2028
static int FUNC(frame_obu)(CodedBitstreamContext *ctx, RWContext *rw,
1701
                           AV1RawFrame *current,
1702
                           AVBufferRef *rw_buffer_ref)
1703
{
1704
    int err;
1705
1706
2028
    CHECK(FUNC(frame_header_obu)(ctx, rw, &current->header,
1707
                                 0, rw_buffer_ref));
1708
1709
2028
    CHECK(FUNC(byte_alignment)(ctx, rw));
1710
1711
2028
    CHECK(FUNC(tile_group_obu)(ctx, rw, &current->tile_group));
1712
1713
2028
    return 0;
1714
}
1715
1716
static int FUNC(tile_list_obu)(CodedBitstreamContext *ctx, RWContext *rw,
1717
                               AV1RawTileList *current)
1718
{
1719
    int err;
1720
1721
    fb(8, output_frame_width_in_tiles_minus_1);
1722
    fb(8, output_frame_height_in_tiles_minus_1);
1723
1724
    fb(16, tile_count_minus_1);
1725
1726
    // Tile data follows.
1727
1728
    return 0;
1729
}
1730
1731
24
static int FUNC(metadata_hdr_cll)(CodedBitstreamContext *ctx, RWContext *rw,
1732
                                  AV1RawMetadataHDRCLL *current)
1733
{
1734
    int err;
1735
1736
24
    fb(16, max_cll);
1737
24
    fb(16, max_fall);
1738
1739
24
    return 0;
1740
}
1741
1742
24
static int FUNC(metadata_hdr_mdcv)(CodedBitstreamContext *ctx, RWContext *rw,
1743
                                   AV1RawMetadataHDRMDCV *current)
1744
{
1745
    int err, i;
1746
1747
96
    for (i = 0; i < 3; i++) {
1748
72
        fbs(16, primary_chromaticity_x[i], 1, i);
1749
72
        fbs(16, primary_chromaticity_y[i], 1, i);
1750
    }
1751
1752
24
    fb(16, white_point_chromaticity_x);
1753
24
    fb(16, white_point_chromaticity_y);
1754
1755
24
    fc(32, luminance_max, 1, MAX_UINT_BITS(32));
1756
    // luminance_min must be lower than luminance_max. Convert luminance_max from
1757
    // 24.8 fixed point to 18.14 fixed point in order to compare them.
1758
24
    fc(32, luminance_min, 0, FFMIN(((uint64_t)current->luminance_max << 6) - 1,
1759
                                   MAX_UINT_BITS(32)));
1760
1761
24
    return 0;
1762
}
1763
1764
static int FUNC(scalability_structure)(CodedBitstreamContext *ctx, RWContext *rw,
1765
                                       AV1RawMetadataScalability *current)
1766
{
1767
    CodedBitstreamAV1Context *priv = ctx->priv_data;
1768
    const AV1RawSequenceHeader *seq;
1769
    int err, i, j;
1770
1771
    if (!priv->sequence_header) {
1772
        av_log(ctx->log_ctx, AV_LOG_ERROR, "No sequence header available: "
1773
               "unable to parse scalability metadata.\n");
1774
        return AVERROR_INVALIDDATA;
1775
    }
1776
    seq = priv->sequence_header;
1777
1778
    fb(2, spatial_layers_cnt_minus_1);
1779
    flag(spatial_layer_dimensions_present_flag);
1780
    flag(spatial_layer_description_present_flag);
1781
    flag(temporal_group_description_present_flag);
1782
    fc(3, scalability_structure_reserved_3bits, 0, 0);
1783
    if (current->spatial_layer_dimensions_present_flag) {
1784
        for (i = 0; i <= current->spatial_layers_cnt_minus_1; i++) {
1785
            fcs(16, spatial_layer_max_width[i],
1786
                0, seq->max_frame_width_minus_1 + 1, 1, i);
1787
            fcs(16, spatial_layer_max_height[i],
1788
                0, seq->max_frame_height_minus_1 + 1, 1, i);
1789
        }
1790
    }
1791
    if (current->spatial_layer_description_present_flag) {
1792
        for (i = 0; i <= current->spatial_layers_cnt_minus_1; i++)
1793
            fbs(8, spatial_layer_ref_id[i], 1, i);
1794
    }
1795
    if (current->temporal_group_description_present_flag) {
1796
        fb(8, temporal_group_size);
1797
        for (i = 0; i < current->temporal_group_size; i++) {
1798
            fbs(3, temporal_group_temporal_id[i], 1, i);
1799
            flags(temporal_group_temporal_switching_up_point_flag[i], 1, i);
1800
            flags(temporal_group_spatial_switching_up_point_flag[i], 1, i);
1801
            fbs(3, temporal_group_ref_cnt[i], 1, i);
1802
            for (j = 0; j < current->temporal_group_ref_cnt[i]; j++) {
1803
                fbs(8, temporal_group_ref_pic_diff[i][j], 2, i, j);
1804
            }
1805
        }
1806
    }
1807
1808
    return 0;
1809
}
1810
1811
static int FUNC(metadata_scalability)(CodedBitstreamContext *ctx, RWContext *rw,
1812
                                      AV1RawMetadataScalability *current)
1813
{
1814
    int err;
1815
1816
    fb(8, scalability_mode_idc);
1817
1818
    if (current->scalability_mode_idc == AV1_SCALABILITY_SS)
1819
        CHECK(FUNC(scalability_structure)(ctx, rw, current));
1820
1821
    return 0;
1822
}
1823
1824
static int FUNC(metadata_itut_t35)(CodedBitstreamContext *ctx, RWContext *rw,
1825
                                   AV1RawMetadataITUTT35 *current)
1826
{
1827
    int err;
1828
    size_t i;
1829
1830
    fb(8, itu_t_t35_country_code);
1831
    if (current->itu_t_t35_country_code == 0xff)
1832
        fb(8, itu_t_t35_country_code_extension_byte);
1833
1834
#ifdef READ
1835
    // The payload runs up to the start of the trailing bits, but there might
1836
    // be arbitrarily many trailing zeroes so we need to read through twice.
1837
    current->payload_size = cbs_av1_get_payload_bytes_left(rw);
1838
1839
    current->payload_ref = av_buffer_alloc(current->payload_size);
1840
    if (!current->payload_ref)
1841
        return AVERROR(ENOMEM);
1842
    current->payload = current->payload_ref->data;
1843
#endif
1844
1845
    for (i = 0; i < current->payload_size; i++)
1846
        xf(8, itu_t_t35_payload_bytes[i], current->payload[i],
1847
           0x00, 0xff, 1, i);
1848
1849
    return 0;
1850
}
1851
1852
static int FUNC(metadata_timecode)(CodedBitstreamContext *ctx, RWContext *rw,
1853
                                   AV1RawMetadataTimecode *current)
1854
{
1855
    int err;
1856
1857
    fb(5, counting_type);
1858
    flag(full_timestamp_flag);
1859
    flag(discontinuity_flag);
1860
    flag(cnt_dropped_flag);
1861
    fb(9, n_frames);
1862
1863
    if (current->full_timestamp_flag) {
1864
        fc(6, seconds_value, 0, 59);
1865
        fc(6, minutes_value, 0, 59);
1866
        fc(5, hours_value,   0, 23);
1867
    } else {
1868
        flag(seconds_flag);
1869
        if (current->seconds_flag) {
1870
            fc(6, seconds_value, 0, 59);
1871
            flag(minutes_flag);
1872
            if (current->minutes_flag) {
1873
                fc(6, minutes_value, 0, 59);
1874
                flag(hours_flag);
1875
                if (current->hours_flag)
1876
                    fc(5, hours_value, 0, 23);
1877
            }
1878
        }
1879
    }
1880
1881
    fb(5, time_offset_length);
1882
    if (current->time_offset_length > 0)
1883
        fb(current->time_offset_length, time_offset_value);
1884
    else
1885
        infer(time_offset_length, 0);
1886
1887
    return 0;
1888
}
1889
1890
48
static int FUNC(metadata_obu)(CodedBitstreamContext *ctx, RWContext *rw,
1891
                              AV1RawMetadata *current)
1892
{
1893
    int err;
1894
1895
48
    leb128(metadata_type);
1896
1897

48
    switch (current->metadata_type) {
1898
24
    case AV1_METADATA_TYPE_HDR_CLL:
1899
24
        CHECK(FUNC(metadata_hdr_cll)(ctx, rw, &current->metadata.hdr_cll));
1900
24
        break;
1901
24
    case AV1_METADATA_TYPE_HDR_MDCV:
1902
24
        CHECK(FUNC(metadata_hdr_mdcv)(ctx, rw, &current->metadata.hdr_mdcv));
1903
24
        break;
1904
    case AV1_METADATA_TYPE_SCALABILITY:
1905
        CHECK(FUNC(metadata_scalability)(ctx, rw, &current->metadata.scalability));
1906
        break;
1907
    case AV1_METADATA_TYPE_ITUT_T35:
1908
        CHECK(FUNC(metadata_itut_t35)(ctx, rw, &current->metadata.itut_t35));
1909
        break;
1910
    case AV1_METADATA_TYPE_TIMECODE:
1911
        CHECK(FUNC(metadata_timecode)(ctx, rw, &current->metadata.timecode));
1912
        break;
1913
    default:
1914
        // Unknown metadata type.
1915
        return AVERROR_PATCHWELCOME;
1916
    }
1917
1918
48
    return 0;
1919
}
1920
1921
static int FUNC(padding_obu)(CodedBitstreamContext *ctx, RWContext *rw,
1922
                             AV1RawPadding *current)
1923
{
1924
    int i, err;
1925
1926
    HEADER("Padding");
1927
1928
#ifdef READ
1929
    // The payload runs up to the start of the trailing bits, but there might
1930
    // be arbitrarily many trailing zeroes so we need to read through twice.
1931
    current->payload_size = cbs_av1_get_payload_bytes_left(rw);
1932
1933
    current->payload_ref = av_buffer_alloc(current->payload_size);
1934
    if (!current->payload_ref)
1935
        return AVERROR(ENOMEM);
1936
    current->payload = current->payload_ref->data;
1937
#endif
1938
1939
    for (i = 0; i < current->payload_size; i++)
1940
        xf(8, obu_padding_byte[i], current->payload[i], 0x00, 0xff, 1, i);
1941
1942
    return 0;
1943
}