GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/cbs_av1_syntax_template.c Lines: 677 1006 67.3 %
Date: 2019-11-22 03:34:36 Branches: 556 1446 38.5 %

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
4164
static int FUNC(obu_header)(CodedBitstreamContext *ctx, RWContext *rw,
20
                            AV1RawOBUHeader *current)
21
{
22
    int err;
23
4164
    av_unused int zero = 0;
24
25
4164
    HEADER("OBU header");
26
27
4164
    fc(1, obu_forbidden_bit, 0, 0);
28
29
4164
    fc(4, obu_type, 0, AV1_OBU_PADDING);
30
4164
    flag(obu_extension_flag);
31
4164
    flag(obu_has_size_field);
32
33
4164
    fc(1, obu_reserved_1bit, 0, 0);
34
35
4164
    if (current->obu_extension_flag) {
36
        fb(3, temporal_id);
37
        fb(2, spatial_id);
38
        fc(3, extension_header_reserved_3bits, 0, 0);
39
    }
40
41
4164
    return 0;
42
}
43
44
540
static int FUNC(trailing_bits)(CodedBitstreamContext *ctx, RWContext *rw, int nb_bits)
45
{
46
    int err;
47
48
540
    av_assert0(nb_bits > 0);
49
50
540
    fixed(1, trailing_one_bit, 1);
51
540
    --nb_bits;
52
53
3225
    while (nb_bits > 0) {
54
2685
        fixed(1, trailing_zero_bit, 0);
55
2685
        --nb_bits;
56
    }
57
58
540
    return 0;
59
}
60
61
1590
static int FUNC(byte_alignment)(CodedBitstreamContext *ctx, RWContext *rw)
62
{
63
    int err;
64
65
5025
    while (byte_alignment(rw) != 0)
66
3435
        fixed(1, zero_bit, 0);
67
68
1590
    return 0;
69
}
70
71
396
static int FUNC(color_config)(CodedBitstreamContext *ctx, RWContext *rw,
72
                              AV1RawColorConfig *current, int seq_profile)
73
{
74
396
    CodedBitstreamAV1Context *priv = ctx->priv_data;
75
    int err;
76
77
396
    flag(high_bitdepth);
78
79
396
    if (seq_profile == FF_PROFILE_AV1_PROFESSIONAL &&
80
        current->high_bitdepth) {
81
        flag(twelve_bit);
82
        priv->bit_depth = current->twelve_bit ? 12 : 10;
83
    } else {
84
396
        priv->bit_depth = current->high_bitdepth ? 10 : 8;
85
    }
86
87
396
    if (seq_profile == FF_PROFILE_AV1_HIGH)
88
        infer(mono_chrome, 0);
89
    else
90
396
        flag(mono_chrome);
91
396
    priv->num_planes = current->mono_chrome ? 1 : 3;
92
93
396
    flag(color_description_present_flag);
94
396
    if (current->color_description_present_flag) {
95
        fb(8, color_primaries);
96
        fb(8, transfer_characteristics);
97
        fb(8, matrix_coefficients);
98
    } else {
99
396
        infer(color_primaries,          AVCOL_PRI_UNSPECIFIED);
100
396
        infer(transfer_characteristics, AVCOL_TRC_UNSPECIFIED);
101
396
        infer(matrix_coefficients,      AVCOL_SPC_UNSPECIFIED);
102
    }
103
104
396
    if (current->mono_chrome) {
105
        flag(color_range);
106
107
        infer(subsampling_x, 1);
108
        infer(subsampling_y, 1);
109
        infer(chroma_sample_position, AV1_CSP_UNKNOWN);
110
        infer(separate_uv_delta_q, 0);
111
112
396
    } else if (current->color_primaries          == AVCOL_PRI_BT709 &&
113
               current->transfer_characteristics == AVCOL_TRC_IEC61966_2_1 &&
114
               current->matrix_coefficients      == AVCOL_SPC_RGB) {
115
        infer(color_range,   1);
116
        infer(subsampling_x, 0);
117
        infer(subsampling_y, 0);
118
        flag(separate_uv_delta_q);
119
120
    } else {
121
396
        flag(color_range);
122
123
396
        if (seq_profile == FF_PROFILE_AV1_MAIN) {
124
396
            infer(subsampling_x, 1);
125
396
            infer(subsampling_y, 1);
126
        } else if (seq_profile == FF_PROFILE_AV1_HIGH) {
127
            infer(subsampling_x, 0);
128
            infer(subsampling_y, 0);
129
        } else {
130
            if (priv->bit_depth == 12) {
131
                fb(1, subsampling_x);
132
                if (current->subsampling_x)
133
                    fb(1, subsampling_y);
134
                else
135
                    infer(subsampling_y, 0);
136
            } else {
137
                infer(subsampling_x, 1);
138
                infer(subsampling_y, 0);
139
            }
140
        }
141

396
        if (current->subsampling_x && current->subsampling_y) {
142
396
            fc(2, chroma_sample_position, AV1_CSP_UNKNOWN,
143
                                          AV1_CSP_COLOCATED);
144
        }
145
146
396
        flag(separate_uv_delta_q);
147
    }
148
149
396
    return 0;
150
}
151
152
216
static int FUNC(timing_info)(CodedBitstreamContext *ctx, RWContext *rw,
153
                             AV1RawTimingInfo *current)
154
{
155
    int err;
156
157
216
    fc(32, num_units_in_display_tick, 1, MAX_UINT_BITS(32));
158
216
    fc(32, time_scale,                1, MAX_UINT_BITS(32));
159
160
216
    flag(equal_picture_interval);
161
216
    if (current->equal_picture_interval)
162
        uvlc(num_ticks_per_picture_minus_1, 0, MAX_UINT_BITS(32) - 1);
163
164
216
    return 0;
165
}
166
167
216
static int FUNC(decoder_model_info)(CodedBitstreamContext *ctx, RWContext *rw,
168
                                    AV1RawDecoderModelInfo *current)
169
{
170
    int err;
171
172
216
    fb(5, buffer_delay_length_minus_1);
173
216
    fb(32, num_units_in_decoding_tick);
174
216
    fb(5,  buffer_removal_time_length_minus_1);
175
216
    fb(5,  frame_presentation_time_length_minus_1);
176
177
216
    return 0;
178
}
179
180
396
static int FUNC(sequence_header_obu)(CodedBitstreamContext *ctx, RWContext *rw,
181
                                     AV1RawSequenceHeader *current)
182
{
183
    int i, err;
184
185
396
    HEADER("Sequence Header");
186
187
396
    fc(3, seq_profile, FF_PROFILE_AV1_MAIN,
188
                       FF_PROFILE_AV1_PROFESSIONAL);
189
396
    flag(still_picture);
190
396
    flag(reduced_still_picture_header);
191
192
396
    if (current->reduced_still_picture_header) {
193
        infer(timing_info_present_flag,           0);
194
        infer(decoder_model_info_present_flag,    0);
195
        infer(initial_display_delay_present_flag, 0);
196
        infer(operating_points_cnt_minus_1,       0);
197
        infer(operating_point_idc[0],             0);
198
199
        fb(5, seq_level_idx[0]);
200
201
        infer(seq_tier[0], 0);
202
        infer(decoder_model_present_for_this_op[0],         0);
203
        infer(initial_display_delay_present_for_this_op[0], 0);
204
205
    } else {
206
396
        flag(timing_info_present_flag);
207
396
        if (current->timing_info_present_flag) {
208
216
            CHECK(FUNC(timing_info)(ctx, rw, &current->timing_info));
209
210
216
            flag(decoder_model_info_present_flag);
211
216
            if (current->decoder_model_info_present_flag) {
212
216
                CHECK(FUNC(decoder_model_info)
213
                          (ctx, rw, &current->decoder_model_info));
214
            }
215
        } else {
216
180
            infer(decoder_model_info_present_flag, 0);
217
        }
218
219
396
        flag(initial_display_delay_present_flag);
220
221
396
        fb(5, operating_points_cnt_minus_1);
222
792
        for (i = 0; i <= current->operating_points_cnt_minus_1; i++) {
223
396
            fbs(12, operating_point_idc[i], 1, i);
224
396
            fbs(5,  seq_level_idx[i], 1, i);
225
226
396
            if (current->seq_level_idx[i] > 7)
227
                flags(seq_tier[i], 1, i);
228
            else
229
396
                infer(seq_tier[i], 0);
230
231
396
            if (current->decoder_model_info_present_flag) {
232
216
                flags(decoder_model_present_for_this_op[i], 1, i);
233
216
                if (current->decoder_model_present_for_this_op[i]) {
234
216
                    int n = current->decoder_model_info.buffer_delay_length_minus_1 + 1;
235
216
                    fbs(n, decoder_buffer_delay[i], 1, i);
236
216
                    fbs(n, encoder_buffer_delay[i], 1, i);
237
216
                    flags(low_delay_mode_flag[i], 1, i);
238
                }
239
            } else {
240
180
                infer(decoder_model_present_for_this_op[i], 0);
241
            }
242
243
396
            if (current->initial_display_delay_present_flag) {
244
216
                flags(initial_display_delay_present_for_this_op[i], 1, i);
245
216
                if (current->initial_display_delay_present_for_this_op[i])
246
216
                    fbs(4, initial_display_delay_minus_1[i], 1, i);
247
            }
248
        }
249
    }
250
251
396
    fb(4, frame_width_bits_minus_1);
252
396
    fb(4, frame_height_bits_minus_1);
253
254
396
    fb(current->frame_width_bits_minus_1  + 1, max_frame_width_minus_1);
255
396
    fb(current->frame_height_bits_minus_1 + 1, max_frame_height_minus_1);
256
257
396
    if (current->reduced_still_picture_header)
258
        infer(frame_id_numbers_present_flag, 0);
259
    else
260
396
        flag(frame_id_numbers_present_flag);
261
396
    if (current->frame_id_numbers_present_flag) {
262
        fb(4, delta_frame_id_length_minus_2);
263
        fb(3, additional_frame_id_length_minus_1);
264
    }
265
266
396
    flag(use_128x128_superblock);
267
396
    flag(enable_filter_intra);
268
396
    flag(enable_intra_edge_filter);
269
270
396
    if (current->reduced_still_picture_header) {
271
        infer(enable_intraintra_compound, 0);
272
        infer(enable_masked_compound,     0);
273
        infer(enable_warped_motion,       0);
274
        infer(enable_dual_filter,         0);
275
        infer(enable_order_hint,          0);
276
        infer(enable_jnt_comp,            0);
277
        infer(enable_ref_frame_mvs,       0);
278
279
        infer(seq_force_screen_content_tools,
280
              AV1_SELECT_SCREEN_CONTENT_TOOLS);
281
        infer(seq_force_integer_mv,
282
              AV1_SELECT_INTEGER_MV);
283
    } else {
284
396
        flag(enable_intraintra_compound);
285
396
        flag(enable_masked_compound);
286
396
        flag(enable_warped_motion);
287
396
        flag(enable_dual_filter);
288
289
396
        flag(enable_order_hint);
290
396
        if (current->enable_order_hint) {
291
396
            flag(enable_jnt_comp);
292
396
            flag(enable_ref_frame_mvs);
293
        } else {
294
            infer(enable_jnt_comp,      0);
295
            infer(enable_ref_frame_mvs, 0);
296
        }
297
298
396
        flag(seq_choose_screen_content_tools);
299
396
        if (current->seq_choose_screen_content_tools)
300
396
            infer(seq_force_screen_content_tools,
301
                  AV1_SELECT_SCREEN_CONTENT_TOOLS);
302
        else
303
            fb(1, seq_force_screen_content_tools);
304
396
        if (current->seq_force_screen_content_tools > 0) {
305
396
            flag(seq_choose_integer_mv);
306
396
            if (current->seq_choose_integer_mv)
307
396
                infer(seq_force_integer_mv,
308
                      AV1_SELECT_INTEGER_MV);
309
            else
310
                fb(1, seq_force_integer_mv);
311
        } else {
312
            infer(seq_force_integer_mv, AV1_SELECT_INTEGER_MV);
313
        }
314
315
396
        if (current->enable_order_hint)
316
396
            fb(3, order_hint_bits_minus_1);
317
    }
318
319
396
    flag(enable_superres);
320
396
    flag(enable_cdef);
321
396
    flag(enable_restoration);
322
323
396
    CHECK(FUNC(color_config)(ctx, rw, &current->color_config,
324
                             current->seq_profile));
325
326
396
    flag(film_grain_params_present);
327
328
396
    return 0;
329
}
330
331
741
static int FUNC(temporal_delimiter_obu)(CodedBitstreamContext *ctx, RWContext *rw)
332
{
333
741
    CodedBitstreamAV1Context *priv = ctx->priv_data;
334
335
741
    HEADER("Temporal Delimiter");
336
337
741
    priv->seen_frame_header = 0;
338
339
741
    return 0;
340
}
341
342
795
static int FUNC(superres_params)(CodedBitstreamContext *ctx, RWContext *rw,
343
                                 AV1RawFrameHeader *current)
344
{
345
795
    CodedBitstreamAV1Context  *priv = ctx->priv_data;
346
795
    const AV1RawSequenceHeader *seq = priv->sequence_header;
347
    int denom, err;
348
349
795
    if (seq->enable_superres)
350
        flag(use_superres);
351
    else
352
795
        infer(use_superres, 0);
353
354
795
    if (current->use_superres) {
355
        fb(3, coded_denom);
356
        denom = current->coded_denom + AV1_SUPERRES_DENOM_MIN;
357
    } else {
358
795
        denom = AV1_SUPERRES_NUM;
359
    }
360
361
795
    priv->upscaled_width = priv->frame_width;
362
795
    priv->frame_width = (priv->upscaled_width * AV1_SUPERRES_NUM +
363
795
                         denom / 2) / denom;
364
365
795
    return 0;
366
}
367
368
795
static int FUNC(frame_size)(CodedBitstreamContext *ctx, RWContext *rw,
369
                            AV1RawFrameHeader *current)
370
{
371
795
    CodedBitstreamAV1Context  *priv = ctx->priv_data;
372
795
    const AV1RawSequenceHeader *seq = priv->sequence_header;
373
    int err;
374
375
795
    if (current->frame_size_override_flag) {
376
        fb(seq->frame_width_bits_minus_1 + 1,  frame_width_minus_1);
377
        fb(seq->frame_height_bits_minus_1 + 1, frame_height_minus_1);
378
379
        priv->frame_width  = current->frame_width_minus_1  + 1;
380
        priv->frame_height = current->frame_height_minus_1 + 1;
381
    } else {
382
795
        priv->frame_width  = seq->max_frame_width_minus_1  + 1;
383
795
        priv->frame_height = seq->max_frame_height_minus_1 + 1;
384
    }
385
386
795
    CHECK(FUNC(superres_params)(ctx, rw, current));
387
388
795
    return 0;
389
}
390
391
795
static int FUNC(render_size)(CodedBitstreamContext *ctx, RWContext *rw,
392
                             AV1RawFrameHeader *current)
393
{
394
795
    CodedBitstreamAV1Context *priv = ctx->priv_data;
395
    int err;
396
397
795
    flag(render_and_frame_size_different);
398
399
795
    if (current->render_and_frame_size_different) {
400
        fb(16, render_width_minus_1);
401
        fb(16, render_height_minus_1);
402
403
        priv->render_width  = current->render_width_minus_1  + 1;
404
        priv->render_height = current->render_height_minus_1 + 1;
405
    } else {
406
795
        priv->render_width  = priv->upscaled_width;
407
795
        priv->render_height = priv->frame_height;
408
    }
409
410
795
    return 0;
411
}
412
413
static int FUNC(frame_size_with_refs)(CodedBitstreamContext *ctx, RWContext *rw,
414
                                      AV1RawFrameHeader *current)
415
{
416
    CodedBitstreamAV1Context *priv = ctx->priv_data;
417
    int i, err;
418
419
    for (i = 0; i < AV1_REFS_PER_FRAME; i++) {
420
        flags(found_ref[i], 1, i);
421
        if (current->found_ref[i]) {
422
            AV1ReferenceFrameState *ref;
423
424
            if (current->ref_frame_idx[i] < 0 ||
425
                !priv->ref[current->ref_frame_idx[i]].valid) {
426
                av_log(ctx->log_ctx, AV_LOG_ERROR,
427
                       "Missing reference frame needed for frame size "
428
                       "(ref = %d, ref_frame_idx = %d).\n",
429
                       i, current->ref_frame_idx[i]);
430
                return AVERROR_INVALIDDATA;
431
            }
432
            ref = &priv->ref[current->ref_frame_idx[i]];
433
434
            priv->upscaled_width = ref->upscaled_width;
435
            priv->frame_width    = ref->frame_width;
436
            priv->frame_height   = ref->frame_height;
437
            priv->render_width   = ref->render_width;
438
            priv->render_height  = ref->render_height;
439
            break;
440
        }
441
    }
442
443
    if (i >= AV1_REFS_PER_FRAME) {
444
        CHECK(FUNC(frame_size)(ctx, rw, current));
445
        CHECK(FUNC(render_size)(ctx, rw, current));
446
    } else {
447
        CHECK(FUNC(superres_params)(ctx, rw, current));
448
    }
449
450
    return 0;
451
}
452
453
639
static int FUNC(interpolation_filter)(CodedBitstreamContext *ctx, RWContext *rw,
454
                                      AV1RawFrameHeader *current)
455
{
456
    int err;
457
458
639
    flag(is_filter_switchable);
459
639
    if (current->is_filter_switchable)
460
471
        infer(interpolation_filter,
461
              AV1_INTERPOLATION_FILTER_SWITCHABLE);
462
    else
463
168
        fb(2, interpolation_filter);
464
465
639
    return 0;
466
}
467
468
795
static int FUNC(tile_info)(CodedBitstreamContext *ctx, RWContext *rw,
469
                           AV1RawFrameHeader *current)
470
{
471
795
    CodedBitstreamAV1Context  *priv = ctx->priv_data;
472
795
    const AV1RawSequenceHeader *seq = priv->sequence_header;
473
    int mi_cols, mi_rows, sb_cols, sb_rows, sb_shift, sb_size;
474
    int max_tile_width_sb, max_tile_height_sb, max_tile_area_sb;
475
    int min_log2_tile_cols, max_log2_tile_cols, max_log2_tile_rows;
476
    int min_log2_tiles, min_log2_tile_rows;
477
    int i, err;
478
479
795
    mi_cols = 2 * ((priv->frame_width  + 7) >> 3);
480
795
    mi_rows = 2 * ((priv->frame_height + 7) >> 3);
481
482
1962
    sb_cols = seq->use_128x128_superblock ? ((mi_cols + 31) >> 5)
483
795
                                          : ((mi_cols + 15) >> 4);
484
1962
    sb_rows = seq->use_128x128_superblock ? ((mi_rows + 31) >> 5)
485
795
                                          : ((mi_rows + 15) >> 4);
486
487
795
    sb_shift = seq->use_128x128_superblock ? 5 : 4;
488
795
    sb_size  = sb_shift + 2;
489
490
795
    max_tile_width_sb = AV1_MAX_TILE_WIDTH >> sb_size;
491
795
    max_tile_area_sb  = AV1_MAX_TILE_AREA  >> (2 * sb_size);
492
493
795
    min_log2_tile_cols = cbs_av1_tile_log2(max_tile_width_sb, sb_cols);
494
795
    max_log2_tile_cols = cbs_av1_tile_log2(1, FFMIN(sb_cols, AV1_MAX_TILE_COLS));
495
795
    max_log2_tile_rows = cbs_av1_tile_log2(1, FFMIN(sb_rows, AV1_MAX_TILE_ROWS));
496
795
    min_log2_tiles = FFMAX(min_log2_tile_cols,
497
                           cbs_av1_tile_log2(max_tile_area_sb, sb_rows * sb_cols));
498
499
795
    flag(uniform_tile_spacing_flag);
500
501
795
    if (current->uniform_tile_spacing_flag) {
502
        int tile_width_sb, tile_height_sb;
503
504
717
        increment(tile_cols_log2, min_log2_tile_cols, max_log2_tile_cols);
505
506
717
        tile_width_sb = (sb_cols + (1 << current->tile_cols_log2) - 1) >>
507
717
            current->tile_cols_log2;
508
717
        current->tile_cols = (sb_cols + tile_width_sb - 1) / tile_width_sb;
509
510
717
        min_log2_tile_rows = FFMAX(min_log2_tiles - current->tile_cols_log2, 0);
511
512
717
        increment(tile_rows_log2, min_log2_tile_rows, max_log2_tile_rows);
513
514
717
        tile_height_sb = (sb_rows + (1 << current->tile_rows_log2) - 1) >>
515
717
            current->tile_rows_log2;
516
717
        current->tile_rows = (sb_rows + tile_height_sb - 1) / tile_height_sb;
517
518
    } else {
519
        int widest_tile_sb, start_sb, size_sb, max_width, max_height;
520
521
78
        widest_tile_sb = 0;
522
523
78
        start_sb = 0;
524

156
        for (i = 0; start_sb < sb_cols && i < AV1_MAX_TILE_COLS; i++) {
525
78
            max_width = FFMIN(sb_cols - start_sb, max_tile_width_sb);
526
78
            ns(max_width, width_in_sbs_minus_1[i], 1, i);
527
78
            size_sb = current->width_in_sbs_minus_1[i] + 1;
528
78
            widest_tile_sb = FFMAX(size_sb, widest_tile_sb);
529
78
            start_sb += size_sb;
530
        }
531
78
        current->tile_cols_log2 = cbs_av1_tile_log2(1, i);
532
78
        current->tile_cols = i;
533
534
78
        if (min_log2_tiles > 0)
535
            max_tile_area_sb = (sb_rows * sb_cols) >> (min_log2_tiles + 1);
536
        else
537
78
            max_tile_area_sb = sb_rows * sb_cols;
538
78
        max_tile_height_sb = FFMAX(max_tile_area_sb / widest_tile_sb, 1);
539
540
78
        start_sb = 0;
541

390
        for (i = 0; start_sb < sb_rows && i < AV1_MAX_TILE_ROWS; i++) {
542
312
            max_height = FFMIN(sb_rows - start_sb, max_tile_height_sb);
543
312
            ns(max_height, height_in_sbs_minus_1[i], 1, i);
544
312
            size_sb = current->height_in_sbs_minus_1[i] + 1;
545
312
            start_sb += size_sb;
546
        }
547
78
        current->tile_rows_log2 = cbs_av1_tile_log2(1, i);
548
78
        current->tile_rows = i;
549
    }
550
551
795
    if (current->tile_cols_log2 > 0 ||
552
795
        current->tile_rows_log2 > 0) {
553
78
        fb(current->tile_cols_log2 + current->tile_rows_log2,
554
           context_update_tile_id);
555
78
        fb(2, tile_size_bytes_minus1);
556
    } else {
557
717
        infer(context_update_tile_id, 0);
558
    }
559
560
795
    priv->tile_cols = current->tile_cols;
561
795
    priv->tile_rows = current->tile_rows;
562
563
795
    return 0;
564
}
565
566
795
static int FUNC(quantization_params)(CodedBitstreamContext *ctx, RWContext *rw,
567
                                     AV1RawFrameHeader *current)
568
{
569
795
    CodedBitstreamAV1Context  *priv = ctx->priv_data;
570
795
    const AV1RawSequenceHeader *seq = priv->sequence_header;
571
    int err;
572
573
795
    fb(8, base_q_idx);
574
575

795
    delta_q(delta_q_y_dc);
576
577
795
    if (priv->num_planes > 1) {
578
795
        if (seq->color_config.separate_uv_delta_q)
579
            flag(diff_uv_delta);
580
        else
581
795
            infer(diff_uv_delta, 0);
582
583

795
        delta_q(delta_q_u_dc);
584

795
        delta_q(delta_q_u_ac);
585
586
795
        if (current->diff_uv_delta) {
587
            delta_q(delta_q_v_dc);
588
            delta_q(delta_q_v_ac);
589
        } else {
590
795
            infer(delta_q_v_dc, current->delta_q_u_dc);
591
795
            infer(delta_q_v_ac, current->delta_q_u_ac);
592
        }
593
    } else {
594
        infer(delta_q_u_dc, 0);
595
        infer(delta_q_u_ac, 0);
596
        infer(delta_q_v_dc, 0);
597
        infer(delta_q_v_ac, 0);
598
    }
599
600
795
    flag(using_qmatrix);
601
795
    if (current->using_qmatrix) {
602
        fb(4, qm_y);
603
        fb(4, qm_u);
604
        if (seq->color_config.separate_uv_delta_q)
605
            fb(4, qm_v);
606
        else
607
            infer(qm_v, current->qm_u);
608
    }
609
610
795
    return 0;
611
}
612
613
795
static int FUNC(segmentation_params)(CodedBitstreamContext *ctx, RWContext *rw,
614
                                     AV1RawFrameHeader *current)
615
{
616
    static const uint8_t bits[AV1_SEG_LVL_MAX] = { 8, 6, 6, 6, 6, 3, 0, 0 };
617
    static const uint8_t sign[AV1_SEG_LVL_MAX] = { 1, 1, 1, 1, 1, 0, 0, 0 };
618
    int i, j, err;
619
620
795
    flag(segmentation_enabled);
621
622
795
    if (current->segmentation_enabled) {
623
        if (current->primary_ref_frame == AV1_PRIMARY_REF_NONE) {
624
            infer(segmentation_update_map,      1);
625
            infer(segmentation_temporal_update, 0);
626
            infer(segmentation_update_data,     1);
627
        } else {
628
            flag(segmentation_update_map);
629
            if (current->segmentation_update_map)
630
                flag(segmentation_temporal_update);
631
            else
632
                infer(segmentation_temporal_update, 0);
633
            flag(segmentation_update_data);
634
        }
635
636
        if (current->segmentation_update_data) {
637
            for (i = 0; i < AV1_MAX_SEGMENTS; i++) {
638
                for (j = 0; j < AV1_SEG_LVL_MAX; j++) {
639
                    flags(feature_enabled[i][j], 2, i, j);
640
641
                    if (current->feature_enabled[i][j] && bits[j] > 0) {
642
                        if (sign[j])
643
                            sus(1 + bits[j], feature_value[i][j], 2, i, j);
644
                        else
645
                            fbs(bits[j], feature_value[i][j], 2, i, j);
646
                    } else {
647
                        infer(feature_value[i][j], 0);
648
                    }
649
                }
650
            }
651
        }
652
    } else {
653
7155
        for (i = 0; i < AV1_MAX_SEGMENTS; i++) {
654
57240
            for (j = 0; j < AV1_SEG_LVL_MAX; j++) {
655
50880
                infer(feature_enabled[i][j], 0);
656
50880
                infer(feature_value[i][j],   0);
657
            }
658
        }
659
    }
660
661
795
    return 0;
662
}
663
664
795
static int FUNC(delta_q_params)(CodedBitstreamContext *ctx, RWContext *rw,
665
                                AV1RawFrameHeader *current)
666
{
667
    int err;
668
669
795
    if (current->base_q_idx > 0)
670
795
        flag(delta_q_present);
671
    else
672
        infer(delta_q_present, 0);
673
674
795
    if (current->delta_q_present)
675
12
        fb(2, delta_q_res);
676
677
795
    return 0;
678
}
679
680
795
static int FUNC(delta_lf_params)(CodedBitstreamContext *ctx, RWContext *rw,
681
                                 AV1RawFrameHeader *current)
682
{
683
    int err;
684
685
795
    if (current->delta_q_present) {
686
12
        if (!current->allow_intrabc)
687
12
            flag(delta_lf_present);
688
        else
689
            infer(delta_lf_present, 0);
690
12
        if (current->delta_lf_present) {
691
            fb(2, delta_lf_res);
692
            flag(delta_lf_multi);
693
        } else {
694
12
            infer(delta_lf_res,   0);
695
12
            infer(delta_lf_multi, 0);
696
        }
697
    } else {
698
783
        infer(delta_lf_present, 0);
699
783
        infer(delta_lf_res,     0);
700
783
        infer(delta_lf_multi,   0);
701
    }
702
703
795
    return 0;
704
}
705
706
795
static int FUNC(loop_filter_params)(CodedBitstreamContext *ctx, RWContext *rw,
707
                                    AV1RawFrameHeader *current)
708
{
709
795
    CodedBitstreamAV1Context *priv = ctx->priv_data;
710
    int i, err;
711
712

795
    if (priv->coded_lossless || current->allow_intrabc) {
713
        infer(loop_filter_level[0], 0);
714
        infer(loop_filter_level[1], 0);
715
        infer(loop_filter_ref_deltas[AV1_REF_FRAME_INTRA],    1);
716
        infer(loop_filter_ref_deltas[AV1_REF_FRAME_LAST],     0);
717
        infer(loop_filter_ref_deltas[AV1_REF_FRAME_LAST2],    0);
718
        infer(loop_filter_ref_deltas[AV1_REF_FRAME_LAST3],    0);
719
        infer(loop_filter_ref_deltas[AV1_REF_FRAME_BWDREF],   0);
720
        infer(loop_filter_ref_deltas[AV1_REF_FRAME_GOLDEN],  -1);
721
        infer(loop_filter_ref_deltas[AV1_REF_FRAME_ALTREF],  -1);
722
        infer(loop_filter_ref_deltas[AV1_REF_FRAME_ALTREF2], -1);
723
        for (i = 0; i < 2; i++)
724
            infer(loop_filter_mode_deltas[i], 0);
725
        return 0;
726
    }
727
728
795
    fb(6, loop_filter_level[0]);
729
795
    fb(6, loop_filter_level[1]);
730
731
795
    if (priv->num_planes > 1) {
732
795
        if (current->loop_filter_level[0] ||
733
288
            current->loop_filter_level[1]) {
734
513
            fb(6, loop_filter_level[2]);
735
513
            fb(6, loop_filter_level[3]);
736
        }
737
    }
738
739
795
    fb(3, loop_filter_sharpness);
740
741
795
    flag(loop_filter_delta_enabled);
742
795
    if (current->loop_filter_delta_enabled) {
743
795
        flag(loop_filter_delta_update);
744
795
        if (current->loop_filter_delta_update) {
745
2322
            for (i = 0; i < AV1_TOTAL_REFS_PER_FRAME; i++) {
746
2064
                flags(update_ref_delta[i], 1, i);
747
2064
                if (current->update_ref_delta[i])
748
                    sus(1 + 6, loop_filter_ref_deltas[i], 1, i);
749
            }
750
774
            for (i = 0; i < 2; i++) {
751
516
                flags(update_mode_delta[i], 1, i);
752
516
                if (current->update_mode_delta[i])
753
                    sus(1 + 6, loop_filter_mode_deltas[i], 1, i);
754
            }
755
        }
756
    }
757
758
795
    return 0;
759
}
760
761
795
static int FUNC(cdef_params)(CodedBitstreamContext *ctx, RWContext *rw,
762
                             AV1RawFrameHeader *current)
763
{
764
795
    CodedBitstreamAV1Context  *priv = ctx->priv_data;
765
795
    const AV1RawSequenceHeader *seq = priv->sequence_header;
766
    int i, err;
767
768

795
    if (priv->coded_lossless || current->allow_intrabc ||
769
795
        !seq->enable_cdef) {
770
        infer(cdef_damping_minus_3, 0);
771
        infer(cdef_bits, 0);
772
        infer(cdef_y_pri_strength[0],  0);
773
        infer(cdef_y_sec_strength[0],  0);
774
        infer(cdef_uv_pri_strength[0], 0);
775
        infer(cdef_uv_sec_strength[0], 0);
776
777
        return 0;
778
    }
779
780
795
    fb(2, cdef_damping_minus_3);
781
795
    fb(2, cdef_bits);
782
783
3558
    for (i = 0; i < (1 << current->cdef_bits); i++) {
784
2763
        fbs(4, cdef_y_pri_strength[i], 1, i);
785
2763
        fbs(2, cdef_y_sec_strength[i], 1, i);
786
787
2763
        if (priv->num_planes > 1) {
788
2763
            fbs(4, cdef_uv_pri_strength[i], 1, i);
789
2763
            fbs(2, cdef_uv_sec_strength[i], 1, i);
790
        }
791
    }
792
793
795
    return 0;
794
}
795
796
795
static int FUNC(lr_params)(CodedBitstreamContext *ctx, RWContext *rw,
797
                           AV1RawFrameHeader *current)
798
{
799
795
    CodedBitstreamAV1Context  *priv = ctx->priv_data;
800
795
    const AV1RawSequenceHeader *seq = priv->sequence_header;
801
    int uses_lr,  uses_chroma_lr;
802
    int i, err;
803
804

795
    if (priv->all_lossless || current->allow_intrabc ||
805
795
        !seq->enable_restoration) {
806
        return 0;
807
    }
808
809
795
    uses_lr = uses_chroma_lr = 0;
810
3180
    for (i = 0; i < priv->num_planes; i++) {
811
2385
        fbs(2, lr_type[i], 1, i);
812
813
2385
        if (current->lr_type[i] != 0) {
814
591
            uses_lr = 1;
815
591
            if (i > 0)
816
195
                uses_chroma_lr = 1;
817
        }
818
    }
819
820
795
    if (uses_lr) {
821
453
        if (seq->use_128x128_superblock)
822
264
            increment(lr_unit_shift, 1, 2);
823
        else
824
189
            increment(lr_unit_shift, 0, 2);
825
826
453
        if(seq->color_config.subsampling_x &&
827

453
           seq->color_config.subsampling_y && uses_chroma_lr) {
828
135
            fb(1, lr_uv_shift);
829
        } else {
830
318
            infer(lr_uv_shift, 0);
831
        }
832
    }
833
834
795
    return 0;
835
}
836
837
795
static int FUNC(read_tx_mode)(CodedBitstreamContext *ctx, RWContext *rw,
838
                              AV1RawFrameHeader *current)
839
{
840
795
    CodedBitstreamAV1Context *priv = ctx->priv_data;
841
    int err;
842
843
795
    if (priv->coded_lossless)
844
        infer(tx_mode, 0);
845
    else
846
795
        increment(tx_mode, 1, 2);
847
848
795
    return 0;
849
}
850
851
795
static int FUNC(frame_reference_mode)(CodedBitstreamContext *ctx, RWContext *rw,
852
                                      AV1RawFrameHeader *current)
853
{
854
    int err;
855
856
795
    if (current->frame_type == AV1_FRAME_INTRA_ONLY ||
857
795
        current->frame_type == AV1_FRAME_KEY)
858
156
        infer(reference_select, 0);
859
    else
860
639
        flag(reference_select);
861
862
795
    return 0;
863
}
864
865
795
static int FUNC(skip_mode_params)(CodedBitstreamContext *ctx, RWContext *rw,
866
                                  AV1RawFrameHeader *current)
867
{
868
795
    CodedBitstreamAV1Context  *priv = ctx->priv_data;
869
795
    const AV1RawSequenceHeader *seq = priv->sequence_header;
870
    int skip_mode_allowed;
871
    int err;
872
873
795
    if (current->frame_type == AV1_FRAME_KEY ||
874
639
        current->frame_type == AV1_FRAME_INTRA_ONLY ||
875

639
        !current->reference_select || !seq->enable_order_hint) {
876
369
        skip_mode_allowed = 0;
877
    } else {
878
        int forward_idx,  backward_idx;
879
        int forward_hint, backward_hint;
880
        int ref_hint, dist, i;
881
882
426
        forward_idx  = -1;
883
426
        backward_idx = -1;
884
3408
        for (i = 0; i < AV1_REFS_PER_FRAME; i++) {
885
2982
            ref_hint = priv->ref[current->ref_frame_idx[i]].order_hint;
886
2982
            dist = cbs_av1_get_relative_dist(seq, ref_hint,
887
2982
                                             current->order_hint);
888
2982
            if (dist < 0) {
889

4860
                if (forward_idx < 0 ||
890
2217
                    cbs_av1_get_relative_dist(seq, ref_hint,
891
                                              forward_hint) > 0) {
892
486
                    forward_idx  = i;
893
486
                    forward_hint = ref_hint;
894
                }
895
339
            } else if (dist > 0) {
896

447
                if (backward_idx < 0 ||
897
126
                    cbs_av1_get_relative_dist(seq, ref_hint,
898
                                              backward_hint) < 0) {
899
195
                    backward_idx  = i;
900
195
                    backward_hint = ref_hint;
901
                }
902
            }
903
        }
904
905
426
        if (forward_idx < 0) {
906
            skip_mode_allowed = 0;
907
426
        } else if (backward_idx >= 0) {
908
195
            skip_mode_allowed = 1;
909
            // Frames for skip mode are forward_idx and backward_idx.
910
        } else {
911
            int second_forward_idx;
912
            int second_forward_hint;
913
914
231
            second_forward_idx = -1;
915
1848
            for (i = 0; i < AV1_REFS_PER_FRAME; i++) {
916
1617
                ref_hint = priv->ref[current->ref_frame_idx[i]].order_hint;
917
1617
                if (cbs_av1_get_relative_dist(seq, ref_hint,
918
                                              forward_hint) < 0) {
919

2421
                    if (second_forward_idx < 0 ||
920
1098
                        cbs_av1_get_relative_dist(seq, ref_hint,
921
                                                  second_forward_hint) > 0) {
922
267
                        second_forward_idx  = i;
923
267
                        second_forward_hint = ref_hint;
924
                    }
925
                }
926
            }
927
928
231
            if (second_forward_idx < 0) {
929
6
                skip_mode_allowed = 0;
930
            } else {
931
225
                skip_mode_allowed = 1;
932
                // Frames for skip mode are forward_idx and second_forward_idx.
933
            }
934
        }
935
    }
936
937
795
    if (skip_mode_allowed)
938
420
        flag(skip_mode_present);
939
    else
940
375
        infer(skip_mode_present, 0);
941
942
795
    return 0;
943
}
944
945
180
static int FUNC(global_motion_param)(CodedBitstreamContext *ctx, RWContext *rw,
946
                                     AV1RawFrameHeader *current,
947
                                     int type, int ref, int idx)
948
{
949
    uint32_t abs_bits, prec_bits, num_syms;
950
    int err;
951
952
180
    if (idx < 2) {
953
90
        if (type == AV1_WARP_MODEL_TRANSLATION) {
954
            abs_bits  = AV1_GM_ABS_TRANS_ONLY_BITS  - !current->allow_high_precision_mv;
955
            prec_bits = AV1_GM_TRANS_ONLY_PREC_BITS - !current->allow_high_precision_mv;
956
        } else {
957
90
            abs_bits  = AV1_GM_ABS_TRANS_BITS;
958
90
            prec_bits = AV1_GM_TRANS_PREC_BITS;
959
        }
960
    } else {
961
90
        abs_bits  = AV1_GM_ABS_ALPHA_BITS;
962
90
        prec_bits = AV1_GM_ALPHA_PREC_BITS;
963
    }
964
965
180
    num_syms = 2 * (1 << abs_bits) + 1;
966
180
    subexp(gm_params[ref][idx], num_syms, 2, ref, idx);
967
968
    // Actual gm_params value is not reconstructed here.
969
    (void)prec_bits;
970
971
180
    return 0;
972
}
973
974
795
static int FUNC(global_motion_params)(CodedBitstreamContext *ctx, RWContext *rw,
975
                                      AV1RawFrameHeader *current)
976
{
977
    int ref, type;
978
    int err;
979
980
795
    if (current->frame_type == AV1_FRAME_KEY ||
981
639
        current->frame_type == AV1_FRAME_INTRA_ONLY)
982
156
        return 0;
983
984
5112
    for (ref = AV1_REF_FRAME_LAST; ref <= AV1_REF_FRAME_ALTREF; ref++) {
985
4473
        flags(is_global[ref], 1, ref);
986
4473
        if (current->is_global[ref]) {
987
45
            flags(is_rot_zoom[ref], 1, ref);
988
45
            if (current->is_rot_zoom[ref]) {
989
45
                type = AV1_WARP_MODEL_ROTZOOM;
990
            } else {
991
                flags(is_translation[ref], 1, ref);
992
                type = current->is_translation[ref] ? AV1_WARP_MODEL_TRANSLATION
993
                                                    : AV1_WARP_MODEL_AFFINE;
994
            }
995
        } else {
996
4428
            type = AV1_WARP_MODEL_IDENTITY;
997
        }
998
999
4473
        if (type >= AV1_WARP_MODEL_ROTZOOM) {
1000
45
            CHECK(FUNC(global_motion_param)(ctx, rw, current, type, ref, 2));
1001
45
            CHECK(FUNC(global_motion_param)(ctx, rw, current, type, ref, 3));
1002
45
            if (type == AV1_WARP_MODEL_AFFINE) {
1003
                CHECK(FUNC(global_motion_param)(ctx, rw, current, type, ref, 4));
1004
                CHECK(FUNC(global_motion_param)(ctx, rw, current, type, ref, 5));
1005
            } else {
1006
                // gm_params[ref][4] = -gm_params[ref][3]
1007
                // gm_params[ref][5] =  gm_params[ref][2]
1008
            }
1009
        }
1010
4473
        if (type >= AV1_WARP_MODEL_TRANSLATION) {
1011
45
            CHECK(FUNC(global_motion_param)(ctx, rw, current, type, ref, 0));
1012
45
            CHECK(FUNC(global_motion_param)(ctx, rw, current, type, ref, 1));
1013
        }
1014
    }
1015
1016
639
    return 0;
1017
}
1018
1019
795
static int FUNC(film_grain_params)(CodedBitstreamContext *ctx, RWContext *rw,
1020
                                   AV1RawFrameHeader *current)
1021
{
1022
795
    CodedBitstreamAV1Context  *priv = ctx->priv_data;
1023
795
    const AV1RawSequenceHeader *seq = priv->sequence_header;
1024
    int num_pos_luma, num_pos_chroma;
1025
    int i, err;
1026
1027
795
    if (!seq->film_grain_params_present ||
1028

66
        (!current->show_frame && !current->showable_frame))
1029
735
        return 0;
1030
1031
60
    flag(apply_grain);
1032
1033
60
    if (!current->apply_grain)
1034
        return 0;
1035
1036
60
    fb(16, grain_seed);
1037
1038
60
    if (current->frame_type == AV1_FRAME_INTER)
1039
54
        flag(update_grain);
1040
    else
1041
6
        infer(update_grain, 1);
1042
1043
60
    if (!current->update_grain) {
1044
        fb(3, film_grain_params_ref_idx);
1045
        return 0;
1046
    }
1047
1048
60
    fb(4, num_y_points);
1049
474
    for (i = 0; i < current->num_y_points; i++) {
1050
414
        fbs(8, point_y_value[i],   1, i);
1051
414
        fbs(8, point_y_scaling[i], 1, i);
1052
    }
1053
1054
60
    if (seq->color_config.mono_chrome)
1055
        infer(chroma_scaling_from_luma, 0);
1056
    else
1057
60
        flag(chroma_scaling_from_luma);
1058
1059
60
    if (seq->color_config.mono_chrome ||
1060
60
        current->chroma_scaling_from_luma ||
1061
60
        (seq->color_config.subsampling_x == 1 &&
1062
60
         seq->color_config.subsampling_y == 1 &&
1063
60
         current->num_y_points == 0)) {
1064
        infer(num_cb_points, 0);
1065
        infer(num_cr_points, 0);
1066
    } else {
1067
60
        fb(4, num_cb_points);
1068
294
        for (i = 0; i < current->num_cb_points; i++) {
1069
234
            fbs(8, point_cb_value[i],   1, i);
1070
234
            fbs(8, point_cb_scaling[i], 1, i);
1071
        }
1072
60
        fb(4, num_cr_points);
1073
300
        for (i = 0; i < current->num_cr_points; i++) {
1074
240
            fbs(8, point_cr_value[i],   1, i);
1075
240
            fbs(8, point_cr_scaling[i], 1, i);
1076
        }
1077
    }
1078
1079
60
    fb(2, grain_scaling_minus_8);
1080
60
    fb(2, ar_coeff_lag);
1081
60
    num_pos_luma = 2 * current->ar_coeff_lag * (current->ar_coeff_lag + 1);
1082
60
    if (current->num_y_points) {
1083
60
        num_pos_chroma = num_pos_luma + 1;
1084
1500
        for (i = 0; i < num_pos_luma; i++)
1085
1440
            fbs(8, ar_coeffs_y_plus_128[i], 1, i);
1086
    } else {
1087
        num_pos_chroma = num_pos_luma;
1088
    }
1089

60
    if (current->chroma_scaling_from_luma || current->num_cb_points) {
1090
1560
        for (i = 0; i < num_pos_chroma; i++)
1091
1500
            fbs(8, ar_coeffs_cb_plus_128[i], 1, i);
1092
    }
1093

60
    if (current->chroma_scaling_from_luma || current->num_cr_points) {
1094
1560
        for (i = 0; i < num_pos_chroma; i++)
1095
1500
            fbs(8, ar_coeffs_cr_plus_128[i], 1, i);
1096
    }
1097
60
    fb(2, ar_coeff_shift_minus_6);
1098
60
    fb(2, grain_scale_shift);
1099
60
    if (current->num_cb_points) {
1100
60
        fb(8, cb_mult);
1101
60
        fb(8, cb_luma_mult);
1102
60
        fb(9, cb_offset);
1103
    }
1104
60
    if (current->num_cr_points) {
1105
60
        fb(8, cr_mult);
1106
60
        fb(8, cr_luma_mult);
1107
60
        fb(9, cr_offset);
1108
    }
1109
1110
60
    flag(overlap_flag);
1111
60
    flag(clip_to_restricted_range);
1112
1113
60
    return 0;
1114
}
1115
1116
915
static int FUNC(uncompressed_header)(CodedBitstreamContext *ctx, RWContext *rw,
1117
                                     AV1RawFrameHeader *current)
1118
{
1119
915
    CodedBitstreamAV1Context *priv = ctx->priv_data;
1120
    const AV1RawSequenceHeader *seq;
1121
    int id_len, diff_len, all_frames, frame_is_intra, order_hint_bits;
1122
    int i, err;
1123
1124
915
    if (!priv->sequence_header) {
1125
        av_log(ctx->log_ctx, AV_LOG_ERROR, "No sequence header available: "
1126
               "unable to decode frame header.\n");
1127
        return AVERROR_INVALIDDATA;
1128
    }
1129
915
    seq = priv->sequence_header;
1130
1131
915
    id_len = seq->additional_frame_id_length_minus_1 +
1132
915
             seq->delta_frame_id_length_minus_2 + 3;
1133
915
    all_frames = (1 << AV1_NUM_REF_FRAMES) - 1;
1134
1135
915
    if (seq->reduced_still_picture_header) {
1136
        infer(show_existing_frame, 0);
1137
        infer(frame_type,     AV1_FRAME_KEY);
1138
        infer(show_frame,     1);
1139
        infer(showable_frame, 0);
1140
        frame_is_intra = 1;
1141
1142
    } else {
1143
915
        flag(show_existing_frame);
1144
1145
915
        if (current->show_existing_frame) {
1146
            AV1ReferenceFrameState *frame;
1147
1148
120
            fb(3, frame_to_show_map_idx);
1149
120
            frame = &priv->ref[current->frame_to_show_map_idx];
1150
1151
120
            if (seq->decoder_model_info_present_flag &&
1152
                !seq->timing_info.equal_picture_interval) {
1153
                fb(seq->decoder_model_info.frame_presentation_time_length_minus_1 + 1,
1154
                   frame_presentation_time);
1155
            }
1156
1157
120
            if (seq->frame_id_numbers_present_flag)
1158
                fb(id_len, display_frame_id);
1159
1160
120
            if (frame->frame_type == AV1_FRAME_KEY)
1161
                infer(refresh_frame_flags, all_frames);
1162
            else
1163
120
                infer(refresh_frame_flags, 0);
1164
1165
120
            return 0;
1166
        }
1167
1168
795
        fb(2, frame_type);
1169
1590
        frame_is_intra = (current->frame_type == AV1_FRAME_INTRA_ONLY ||
1170
795
                          current->frame_type == AV1_FRAME_KEY);
1171
1172
795
        flag(show_frame);
1173
795
        if (current->show_frame &&
1174
621
            seq->decoder_model_info_present_flag &&
1175
192
            !seq->timing_info.equal_picture_interval) {
1176
192
            fb(seq->decoder_model_info.frame_presentation_time_length_minus_1 + 1,
1177
               frame_presentation_time);
1178
        }
1179
795
        if (current->show_frame)
1180
621
            infer(showable_frame, current->frame_type != AV1_FRAME_KEY);
1181
        else
1182
174
            flag(showable_frame);
1183
1184
795
        if (current->frame_type == AV1_FRAME_SWITCH ||
1185

795
            (current->frame_type == AV1_FRAME_KEY && current->show_frame))
1186
156
            infer(error_resilient_mode, 1);
1187
        else
1188
639
            flag(error_resilient_mode);
1189
    }
1190
1191

795
    if (current->frame_type == AV1_FRAME_KEY && current->show_frame) {
1192
1404
        for (i = 0; i < AV1_NUM_REF_FRAMES; i++) {
1193
1248
            priv->ref[i].valid = 0;
1194
1248
            priv->ref[i].order_hint = 0;
1195
        }
1196
    }
1197
1198
795
    flag(disable_cdf_update);
1199
1200
795
    if (seq->seq_force_screen_content_tools ==
1201
        AV1_SELECT_SCREEN_CONTENT_TOOLS) {
1202
795
        flag(allow_screen_content_tools);
1203
    } else {
1204
        infer(allow_screen_content_tools,
1205
              seq->seq_force_screen_content_tools);
1206
    }
1207
795
    if (current->allow_screen_content_tools) {
1208
        if (seq->seq_force_integer_mv == AV1_SELECT_INTEGER_MV)
1209
            flag(force_integer_mv);
1210
        else
1211
            infer(force_integer_mv, seq->seq_force_integer_mv);
1212
    } else {
1213
795
        infer(force_integer_mv, 0);
1214
    }
1215
1216
795
    if (seq->frame_id_numbers_present_flag) {
1217
        fb(id_len, current_frame_id);
1218
1219
        diff_len = seq->delta_frame_id_length_minus_2 + 2;
1220
        for (i = 0; i < AV1_NUM_REF_FRAMES; i++) {
1221
            if (current->current_frame_id > (1 << diff_len)) {
1222
                if (priv->ref[i].frame_id > current->current_frame_id ||
1223
                    priv->ref[i].frame_id < (current->current_frame_id -
1224
                                             (1 << diff_len)))
1225
                    priv->ref[i].valid = 0;
1226
            } else {
1227
                if (priv->ref[i].frame_id > current->current_frame_id &&
1228
                    priv->ref[i].frame_id < ((1 << id_len) +
1229
                                             current->current_frame_id -
1230
                                             (1 << diff_len)))
1231
                    priv->ref[i].valid = 0;
1232
            }
1233
        }
1234
    } else {
1235
795
        infer(current_frame_id, 0);
1236
    }
1237
1238
795
    if (current->frame_type == AV1_FRAME_SWITCH)
1239
        infer(frame_size_override_flag, 1);
1240
795
    else if(seq->reduced_still_picture_header)
1241
        infer(frame_size_override_flag, 0);
1242
    else
1243
795
        flag(frame_size_override_flag);
1244
1245
795
    order_hint_bits =
1246
795
        seq->enable_order_hint ? seq->order_hint_bits_minus_1 + 1 : 0;
1247
795
    if (order_hint_bits > 0)
1248
795
        fb(order_hint_bits, order_hint);
1249
    else
1250
        infer(order_hint, 0);
1251
1252

795
    if (frame_is_intra || current->error_resilient_mode)
1253
156
        infer(primary_ref_frame, AV1_PRIMARY_REF_NONE);
1254
    else
1255
639
        fb(3, primary_ref_frame);
1256
1257
795
    if (seq->decoder_model_info_present_flag) {
1258
213
        flag(buffer_removal_time_present_flag);
1259
213
        if (current->buffer_removal_time_present_flag) {
1260
426
            for (i = 0; i <= seq->operating_points_cnt_minus_1; i++) {
1261
213
                if (seq->decoder_model_present_for_this_op[i]) {
1262
213
                    int op_pt_idc = seq->operating_point_idc[i];
1263
213
                    int in_temporal_layer = (op_pt_idc >>  priv->temporal_id    ) & 1;
1264
213
                    int in_spatial_layer  = (op_pt_idc >> (priv->spatial_id + 8)) & 1;
1265

213
                    if (seq->operating_point_idc[i] == 0 ||
1266
                        in_temporal_layer || in_spatial_layer) {
1267
213
                        fbs(seq->decoder_model_info.buffer_removal_time_length_minus_1 + 1,
1268
                            buffer_removal_time[i], 1, i);
1269
                    }
1270
                }
1271
            }
1272
        }
1273
    }
1274
1275
795
    if (current->frame_type == AV1_FRAME_SWITCH ||
1276

795
        (current->frame_type == AV1_FRAME_KEY && current->show_frame))
1277
156
        infer(refresh_frame_flags, all_frames);
1278
    else
1279
639
        fb(8, refresh_frame_flags);
1280
1281

795
    if (!frame_is_intra || current->refresh_frame_flags != all_frames) {
1282

639
        if (current->error_resilient_mode && seq->enable_order_hint) {
1283
            for (i = 0; i < AV1_NUM_REF_FRAMES; i++) {
1284
                fbs(order_hint_bits, ref_order_hint[i], 1, i);
1285
                if (current->ref_order_hint[i] != priv->ref[i].order_hint)
1286
                    priv->ref[i].valid = 0;
1287
            }
1288
        }
1289
    }
1290
1291
795
    if (current->frame_type == AV1_FRAME_KEY ||
1292
639
        current->frame_type == AV1_FRAME_INTRA_ONLY) {
1293
156
        CHECK(FUNC(frame_size)(ctx, rw, current));
1294
156
        CHECK(FUNC(render_size)(ctx, rw, current));
1295
1296
156
        if (current->allow_screen_content_tools &&
1297
            priv->upscaled_width == priv->frame_width)
1298
            flag(allow_intrabc);
1299
        else
1300
156
            infer(allow_intrabc, 0);
1301
1302
    } else {
1303
639
        if (!seq->enable_order_hint) {
1304
            infer(frame_refs_short_signaling, 0);
1305
        } else {
1306
639
            flag(frame_refs_short_signaling);
1307
639
            if (current->frame_refs_short_signaling) {
1308
42
                fb(3, last_frame_idx);
1309
42
                fb(3, golden_frame_idx);
1310
1311
336
                for (i = 0; i < AV1_REFS_PER_FRAME; i++) {
1312
294
                    if (i == 0)
1313
42
                        infer(ref_frame_idx[i], current->last_frame_idx);
1314
252
                    else if (i == AV1_REF_FRAME_GOLDEN -
1315
                                  AV1_REF_FRAME_LAST)
1316
42
                        infer(ref_frame_idx[i], current->golden_frame_idx);
1317
                    else
1318
210
                        infer(ref_frame_idx[i], -1);
1319
                }
1320
            }
1321
        }
1322
1323
5112
        for (i = 0; i < AV1_REFS_PER_FRAME; i++) {
1324
4473
            if (!current->frame_refs_short_signaling)
1325
4179
                fbs(3, ref_frame_idx[i], 1, i);
1326
4473
            if (seq->frame_id_numbers_present_flag) {
1327
                fbs(seq->delta_frame_id_length_minus_2 + 2,
1328
                    delta_frame_id_minus1[i], 1, i);
1329
            }
1330
        }
1331
1332
639
        if (current->frame_size_override_flag &&
1333
            !current->error_resilient_mode) {
1334
            CHECK(FUNC(frame_size_with_refs)(ctx, rw, current));
1335
        } else {
1336
639
            CHECK(FUNC(frame_size)(ctx, rw, current));
1337
639
            CHECK(FUNC(render_size)(ctx, rw, current));
1338
        }
1339
1340
639
        if (current->force_integer_mv)
1341
            infer(allow_high_precision_mv, 0);
1342
        else
1343
639
            flag(allow_high_precision_mv);
1344
1345
639
        CHECK(FUNC(interpolation_filter)(ctx, rw, current));
1346
1347
639
        flag(is_motion_mode_switchable);
1348
1349
639
        if (current->error_resilient_mode ||
1350
639
            !seq->enable_ref_frame_mvs)
1351
            infer(use_ref_frame_mvs, 0);
1352
        else
1353
639
            flag(use_ref_frame_mvs);
1354
1355
639
        infer(allow_intrabc, 0);
1356
    }
1357
1358
    if (!frame_is_intra) {
1359
        // Derive reference frame sign biases.
1360
    }
1361
1362

795
    if (seq->reduced_still_picture_header || current->disable_cdf_update)
1363
        infer(disable_frame_end_update_cdf, 1);
1364
    else
1365
795
        flag(disable_frame_end_update_cdf);
1366
1367
795
    if (current->primary_ref_frame == AV1_PRIMARY_REF_NONE) {
1368
        // Init non-coeff CDFs.
1369
        // Setup past independence.
1370
    } else {
1371
        // Load CDF tables from previous frame.
1372
        // Load params from previous frame.
1373
    }
1374
1375
795
    if (current->use_ref_frame_mvs) {
1376
        // Perform motion field estimation process.
1377
    }
1378
1379
795
    CHECK(FUNC(tile_info)(ctx, rw, current));
1380
1381
795
    CHECK(FUNC(quantization_params)(ctx, rw, current));
1382
1383
795
    CHECK(FUNC(segmentation_params)(ctx, rw, current));
1384
1385
795
    CHECK(FUNC(delta_q_params)(ctx, rw, current));
1386
1387
795
    CHECK(FUNC(delta_lf_params)(ctx, rw, current));
1388
1389
    // Init coeff CDFs / load previous segments.
1390
1391
795
    priv->coded_lossless = 1;
1392
7155
    for (i = 0; i < AV1_MAX_SEGMENTS; i++) {
1393
        int qindex;
1394
6360
        if (current->feature_enabled[i][AV1_SEG_LVL_ALT_Q]) {
1395
            qindex = (current->base_q_idx +
1396
                      current->feature_value[i][AV1_SEG_LVL_ALT_Q]);
1397
        } else {
1398
6360
            qindex = current->base_q_idx;
1399
        }
1400
6360
        qindex = av_clip_uintp2(qindex, 8);
1401
1402

6360
        if (qindex                || current->delta_q_y_dc ||
1403
            current->delta_q_u_ac || current->delta_q_u_dc ||
1404
            current->delta_q_v_ac || current->delta_q_v_dc) {
1405
6360
            priv->coded_lossless = 0;
1406
        }
1407
    }
1408
795
    priv->all_lossless = priv->coded_lossless &&
1409
        priv->frame_width == priv->upscaled_width;
1410
1411
795
    CHECK(FUNC(loop_filter_params)(ctx, rw, current));
1412
1413
795
    CHECK(FUNC(cdef_params)(ctx, rw, current));
1414
1415
795
    CHECK(FUNC(lr_params)(ctx, rw, current));
1416
1417
795
    CHECK(FUNC(read_tx_mode)(ctx, rw, current));
1418
1419
795
    CHECK(FUNC(frame_reference_mode)(ctx, rw, current));
1420
1421
795
    CHECK(FUNC(skip_mode_params)(ctx, rw, current));
1422
1423

795
    if (frame_is_intra || current->error_resilient_mode ||
1424
639
        !seq->enable_warped_motion)
1425
156
        infer(allow_warped_motion, 0);
1426
    else
1427
639
        flag(allow_warped_motion);
1428
1429
795
    flag(reduced_tx_set);
1430
1431
795
    CHECK(FUNC(global_motion_params)(ctx, rw, current));
1432
1433
795
    CHECK(FUNC(film_grain_params)(ctx, rw, current));
1434
1435
7155
    for (i = 0; i < AV1_NUM_REF_FRAMES; i++) {
1436
6360
        if (current->refresh_frame_flags & (1 << i)) {
1437
1881
            priv->ref[i] = (AV1ReferenceFrameState) {
1438
                .valid          = 1,
1439
1881
                .frame_id       = current->current_frame_id,
1440
1881
                .upscaled_width = priv->upscaled_width,
1441
1881
                .frame_width    = priv->frame_width,
1442
1881
                .frame_height   = priv->frame_height,
1443
1881
                .render_width   = priv->render_width,
1444
1881
                .render_height  = priv->render_height,
1445
1881
                .frame_type     = current->frame_type,
1446
1881
                .subsampling_x  = seq->color_config.subsampling_x,
1447
1881
                .subsampling_y  = seq->color_config.subsampling_y,
1448
1881
                .bit_depth      = priv->bit_depth,
1449
1881
                .order_hint     = current->order_hint,
1450
            };
1451
        }
1452
    }
1453
1454
795
    av_log(ctx->log_ctx, AV_LOG_DEBUG, "Frame %d:  size %dx%d  "
1455
           "upscaled %d  render %dx%d  subsample %dx%d  "
1456
795
           "bitdepth %d  tiles %dx%d.\n", current->order_hint,
1457
           priv->frame_width, priv->frame_height, priv->upscaled_width,
1458
           priv->render_width, priv->render_height,
1459
795
           seq->color_config.subsampling_x + 1,
1460
795
           seq->color_config.subsampling_y + 1, priv->bit_depth,
1461
           priv->tile_rows, priv->tile_cols);
1462
1463
795
    return 0;
1464
}
1465
1466
915
static int FUNC(frame_header_obu)(CodedBitstreamContext *ctx, RWContext *rw,
1467
                                  AV1RawFrameHeader *current, int redundant,
1468
                                  AVBufferRef *rw_buffer_ref)
1469
{
1470
915
    CodedBitstreamAV1Context *priv = ctx->priv_data;
1471
    int start_pos, fh_bits, fh_bytes, err;
1472
    uint8_t *fh_start;
1473
1474
915
    if (priv->seen_frame_header) {
1475
        if (!redundant) {
1476
            av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid repeated "
1477
                   "frame header OBU.\n");
1478
            return AVERROR_INVALIDDATA;
1479
        } else {
1480
            GetBitContext fh;
1481
            size_t i, b;
1482
            uint32_t val;
1483
1484
            HEADER("Redundant Frame Header");
1485
1486
            av_assert0(priv->frame_header_ref && priv->frame_header);
1487
1488
            init_get_bits(&fh, priv->frame_header,
1489
                          priv->frame_header_size);
1490
            for (i = 0; i < priv->frame_header_size; i += 8) {
1491
                b = FFMIN(priv->frame_header_size - i, 8);
1492
                val = get_bits(&fh, b);
1493
                xf(b, frame_header_copy[i],
1494
                   val, val, val, 1, i / 8);
1495
            }
1496
        }
1497
    } else {
1498
915
        if (redundant)
1499
            HEADER("Redundant Frame Header (used as Frame Header)");
1500
        else
1501
915
            HEADER("Frame Header");
1502
1503
915
        priv->seen_frame_header = 1;
1504
1505
#ifdef READ
1506
915
        start_pos = get_bits_count(rw);
1507
#else
1508
610
        start_pos = put_bits_count(rw);
1509
#endif
1510
1511
915
        CHECK(FUNC(uncompressed_header)(ctx, rw, current));
1512
1513
915
        if (current->show_existing_frame) {
1514
120
            priv->seen_frame_header = 0;
1515
        } else {
1516
795
            priv->seen_frame_header = 1;
1517
1518
795
            av_buffer_unref(&priv->frame_header_ref);
1519
1520
#ifdef READ
1521
795
            fh_bits  = get_bits_count(rw) - start_pos;
1522
795
            fh_start = (uint8_t*)rw->buffer + start_pos / 8;
1523
#else
1524
            // Need to flush the bitwriter so that we can copy its output,
1525
            // but use a copy so we don't affect the caller's structure.
1526
            {
1527
530
                PutBitContext tmp = *rw;
1528
530
                flush_put_bits(&tmp);
1529
            }
1530
1531
530
            fh_bits  = put_bits_count(rw) - start_pos;
1532
530
            fh_start = rw->buf + start_pos / 8;
1533
#endif
1534
795
            fh_bytes = (fh_bits + 7) / 8;
1535
1536
795
            priv->frame_header_size = fh_bits;
1537
1538
795
            if (rw_buffer_ref) {
1539
795
                priv->frame_header_ref = av_buffer_ref(rw_buffer_ref);
1540
795
                if (!priv->frame_header_ref)
1541
                    return AVERROR(ENOMEM);
1542
795
                priv->frame_header = fh_start;
1543
            } else {
1544
530
                priv->frame_header_ref =
1545
530
                    av_buffer_alloc(fh_bytes + AV_INPUT_BUFFER_PADDING_SIZE);
1546
530
                if (!priv->frame_header_ref)
1547
                    return AVERROR(ENOMEM);
1548
530
                priv->frame_header = priv->frame_header_ref->data;
1549
530
                memcpy(priv->frame_header, fh_start, fh_bytes);
1550
            }
1551
        }
1552
    }
1553
1554
915
    return 0;
1555
}
1556
1557
795
static int FUNC(tile_group_obu)(CodedBitstreamContext *ctx, RWContext *rw,
1558
                                AV1RawTileGroup *current)
1559
{
1560
795
    CodedBitstreamAV1Context *priv = ctx->priv_data;
1561
    int num_tiles, tile_bits;
1562
    int err;
1563
1564
795
    HEADER("Tile Group");
1565
1566
795
    num_tiles = priv->tile_cols * priv->tile_rows;
1567
795
    if (num_tiles > 1)
1568
78
        flag(tile_start_and_end_present_flag);
1569
    else
1570
717
        infer(tile_start_and_end_present_flag, 0);
1571
1572

795
    if (num_tiles == 1 || !current->tile_start_and_end_present_flag) {
1573
795
        infer(tg_start, 0);
1574
795
        infer(tg_end, num_tiles - 1);
1575
    } else {
1576
        tile_bits = cbs_av1_tile_log2(1, priv->tile_cols) +
1577
                    cbs_av1_tile_log2(1, priv->tile_rows);
1578
        fb(tile_bits, tg_start);
1579
        fb(tile_bits, tg_end);
1580
    }
1581
1582
795
    CHECK(FUNC(byte_alignment)(ctx, rw));
1583
1584
    // Reset header for next frame.
1585
795
    if (current->tg_end == num_tiles - 1)
1586
795
        priv->seen_frame_header = 0;
1587
1588
    // Tile data follows.
1589
1590
795
    return 0;
1591
}
1592
1593
795
static int FUNC(frame_obu)(CodedBitstreamContext *ctx, RWContext *rw,
1594
                           AV1RawFrame *current,
1595
                           AVBufferRef *rw_buffer_ref)
1596
{
1597
    int err;
1598
1599
795
    CHECK(FUNC(frame_header_obu)(ctx, rw, &current->header,
1600
                                 0, rw_buffer_ref));
1601
1602
795
    CHECK(FUNC(byte_alignment)(ctx, rw));
1603
1604
795
    CHECK(FUNC(tile_group_obu)(ctx, rw, &current->tile_group));
1605
1606
795
    return 0;
1607
}
1608
1609
static int FUNC(tile_list_obu)(CodedBitstreamContext *ctx, RWContext *rw,
1610
                               AV1RawTileList *current)
1611
{
1612
    int err;
1613
1614
    fb(8, output_frame_width_in_tiles_minus_1);
1615
    fb(8, output_frame_height_in_tiles_minus_1);
1616
1617
    fb(16, tile_count_minus_1);
1618
1619
    // Tile data follows.
1620
1621
    return 0;
1622
}
1623
1624
12
static int FUNC(metadata_hdr_cll)(CodedBitstreamContext *ctx, RWContext *rw,
1625
                                  AV1RawMetadataHDRCLL *current)
1626
{
1627
    int err;
1628
1629
12
    fb(16, max_cll);
1630
12
    fb(16, max_fall);
1631
1632
12
    return 0;
1633
}
1634
1635
12
static int FUNC(metadata_hdr_mdcv)(CodedBitstreamContext *ctx, RWContext *rw,
1636
                                   AV1RawMetadataHDRMDCV *current)
1637
{
1638
    int err, i;
1639
1640
48
    for (i = 0; i < 3; i++) {
1641
36
        fbs(16, primary_chromaticity_x[i], 1, i);
1642
36
        fbs(16, primary_chromaticity_y[i], 1, i);
1643
    }
1644
1645
12
    fb(16, white_point_chromaticity_x);
1646
12
    fb(16, white_point_chromaticity_y);
1647
1648
12
    fc(32, luminance_max, 1, MAX_UINT_BITS(32));
1649
    // luminance_min must be lower than luminance_max. Convert luminance_max from
1650
    // 24.8 fixed point to 18.14 fixed point in order to compare them.
1651
12
    fc(32, luminance_min, 0, FFMIN(((uint64_t)current->luminance_max << 6) - 1,
1652
                                   MAX_UINT_BITS(32)));
1653
1654
12
    return 0;
1655
}
1656
1657
static int FUNC(scalability_structure)(CodedBitstreamContext *ctx, RWContext *rw,
1658
                                       AV1RawMetadataScalability *current)
1659
{
1660
    CodedBitstreamAV1Context *priv = ctx->priv_data;
1661
    const AV1RawSequenceHeader *seq;
1662
    int err, i, j;
1663
1664
    if (!priv->sequence_header) {
1665
        av_log(ctx->log_ctx, AV_LOG_ERROR, "No sequence header available: "
1666
               "unable to parse scalability metadata.\n");
1667
        return AVERROR_INVALIDDATA;
1668
    }
1669
    seq = priv->sequence_header;
1670
1671
    fb(2, spatial_layers_cnt_minus_1);
1672
    flag(spatial_layer_dimensions_present_flag);
1673
    flag(spatial_layer_description_present_flag);
1674
    flag(temporal_group_description_present_flag);
1675
    fc(3, scalability_structure_reserved_3bits, 0, 0);
1676
    if (current->spatial_layer_dimensions_present_flag) {
1677
        for (i = 0; i <= current->spatial_layers_cnt_minus_1; i++) {
1678
            fcs(16, spatial_layer_max_width[i],
1679
                0, seq->max_frame_width_minus_1 + 1, 1, i);
1680
            fcs(16, spatial_layer_max_height[i],
1681
                0, seq->max_frame_height_minus_1 + 1, 1, i);
1682
        }
1683
    }
1684
    if (current->spatial_layer_description_present_flag) {
1685
        for (i = 0; i <= current->spatial_layers_cnt_minus_1; i++)
1686
            fbs(8, spatial_layer_ref_id[i], 1, i);
1687
    }
1688
    if (current->temporal_group_description_present_flag) {
1689
        fb(8, temporal_group_size);
1690
        for (i = 0; i < current->temporal_group_size; i++) {
1691
            fbs(3, temporal_group_temporal_id[i], 1, i);
1692
            flags(temporal_group_temporal_switching_up_point_flag[i], 1, i);
1693
            flags(temporal_group_spatial_switching_up_point_flag[i], 1, i);
1694
            fbs(3, temporal_group_ref_cnt[i], 1, i);
1695
            for (j = 0; j < current->temporal_group_ref_cnt[i]; j++) {
1696
                fbs(8, temporal_group_ref_pic_diff[i][j], 2, i, j);
1697
            }
1698
        }
1699
    }
1700
1701
    return 0;
1702
}
1703
1704
static int FUNC(metadata_scalability)(CodedBitstreamContext *ctx, RWContext *rw,
1705
                                      AV1RawMetadataScalability *current)
1706
{
1707
    int err;
1708
1709
    fb(8, scalability_mode_idc);
1710
1711
    if (current->scalability_mode_idc == AV1_SCALABILITY_SS)
1712
        CHECK(FUNC(scalability_structure)(ctx, rw, current));
1713
1714
    return 0;
1715
}
1716
1717
static int FUNC(metadata_itut_t35)(CodedBitstreamContext *ctx, RWContext *rw,
1718
                                   AV1RawMetadataITUTT35 *current)
1719
{
1720
    int err;
1721
    size_t i;
1722
1723
    fb(8, itu_t_t35_country_code);
1724
    if (current->itu_t_t35_country_code == 0xff)
1725
        fb(8, itu_t_t35_country_code_extension_byte);
1726
1727
#ifdef READ
1728
    // The payload runs up to the start of the trailing bits, but there might
1729
    // be arbitrarily many trailing zeroes so we need to read through twice.
1730
    current->payload_size = cbs_av1_get_payload_bytes_left(rw);
1731
1732
    current->payload_ref = av_buffer_alloc(current->payload_size);
1733
    if (!current->payload_ref)
1734
        return AVERROR(ENOMEM);
1735
    current->payload = current->payload_ref->data;
1736
#endif
1737
1738
    for (i = 0; i < current->payload_size; i++)
1739
        xf(8, itu_t_t35_payload_bytes[i], current->payload[i],
1740
           0x00, 0xff, 1, i);
1741
1742
    return 0;
1743
}
1744
1745
static int FUNC(metadata_timecode)(CodedBitstreamContext *ctx, RWContext *rw,
1746
                                   AV1RawMetadataTimecode *current)
1747
{
1748
    int err;
1749
1750
    fb(5, counting_type);
1751
    flag(full_timestamp_flag);
1752
    flag(discontinuity_flag);
1753
    flag(cnt_dropped_flag);
1754
    fb(9, n_frames);
1755
1756
    if (current->full_timestamp_flag) {
1757
        fc(6, seconds_value, 0, 59);
1758
        fc(6, minutes_value, 0, 59);
1759
        fc(5, hours_value,   0, 23);
1760
    } else {
1761
        flag(seconds_flag);
1762
        if (current->seconds_flag) {
1763
            fc(6, seconds_value, 0, 59);
1764
            flag(minutes_flag);
1765
            if (current->minutes_flag) {
1766
                fc(6, minutes_value, 0, 59);
1767
                flag(hours_flag);
1768
                if (current->hours_flag)
1769
                    fc(5, hours_value, 0, 23);
1770
            }
1771
        }
1772
    }
1773
1774
    fb(5, time_offset_length);
1775
    if (current->time_offset_length > 0)
1776
        fb(current->time_offset_length, time_offset_value);
1777
    else
1778
        infer(time_offset_length, 0);
1779
1780
    return 0;
1781
}
1782
1783
24
static int FUNC(metadata_obu)(CodedBitstreamContext *ctx, RWContext *rw,
1784
                              AV1RawMetadata *current)
1785
{
1786
    int err;
1787
1788
24
    leb128(metadata_type);
1789
1790

24
    switch (current->metadata_type) {
1791
12
    case AV1_METADATA_TYPE_HDR_CLL:
1792
12
        CHECK(FUNC(metadata_hdr_cll)(ctx, rw, &current->metadata.hdr_cll));
1793
12
        break;
1794
12
    case AV1_METADATA_TYPE_HDR_MDCV:
1795
12
        CHECK(FUNC(metadata_hdr_mdcv)(ctx, rw, &current->metadata.hdr_mdcv));
1796
12
        break;
1797
    case AV1_METADATA_TYPE_SCALABILITY:
1798
        CHECK(FUNC(metadata_scalability)(ctx, rw, &current->metadata.scalability));
1799
        break;
1800
    case AV1_METADATA_TYPE_ITUT_T35:
1801
        CHECK(FUNC(metadata_itut_t35)(ctx, rw, &current->metadata.itut_t35));
1802
        break;
1803
    case AV1_METADATA_TYPE_TIMECODE:
1804
        CHECK(FUNC(metadata_timecode)(ctx, rw, &current->metadata.timecode));
1805
        break;
1806
    default:
1807
        // Unknown metadata type.
1808
        return AVERROR_PATCHWELCOME;
1809
    }
1810
1811
24
    return 0;
1812
}
1813
1814
static int FUNC(padding_obu)(CodedBitstreamContext *ctx, RWContext *rw,
1815
                             AV1RawPadding *current)
1816
{
1817
    int i, err;
1818
1819
    HEADER("Padding");
1820
1821
#ifdef READ
1822
    // The payload runs up to the start of the trailing bits, but there might
1823
    // be arbitrarily many trailing zeroes so we need to read through twice.
1824
    current->payload_size = cbs_av1_get_payload_bytes_left(rw);
1825
1826
    current->payload_ref = av_buffer_alloc(current->payload_size);
1827
    if (!current->payload_ref)
1828
        return AVERROR(ENOMEM);
1829
    current->payload = current->payload_ref->data;
1830
#endif
1831
1832
    for (i = 0; i < current->payload_size; i++)
1833
        xf(8, obu_padding_byte[i], current->payload[i], 0x00, 0xff, 1, i);
1834
1835
    return 0;
1836
}