GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/cbs_av1_syntax_template.c Lines: 791 1072 73.8 %
Date: 2020-01-28 04:56:05 Branches: 666 1504 44.3 %

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
4118
static int FUNC(obu_header)(CodedBitstreamContext *ctx, RWContext *rw,
20
                            AV1RawOBUHeader *current)
21
{
22
    int err;
23
24
4118
    HEADER("OBU header");
25
26
4118
    fc(1, obu_forbidden_bit, 0, 0);
27
28
4118
    fc(4, obu_type, 0, AV1_OBU_PADDING);
29
4118
    flag(obu_extension_flag);
30
4118
    flag(obu_has_size_field);
31
32
4118
    fc(1, obu_reserved_1bit, 0, 0);
33
34
4118
    if (current->obu_extension_flag) {
35
320
        fb(3, temporal_id);
36
320
        fb(2, spatial_id);
37
320
        fc(3, extension_header_reserved_3bits, 0, 0);
38
    }
39
40
4118
    return 0;
41
}
42
43
483
static int FUNC(trailing_bits)(CodedBitstreamContext *ctx, RWContext *rw, int nb_bits)
44
{
45
    int err;
46
47
483
    av_assert0(nb_bits > 0);
48
49
483
    fixed(1, trailing_one_bit, 1);
50
483
    --nb_bits;
51
52
2602
    while (nb_bits > 0) {
53
2119
        fixed(1, trailing_zero_bit, 0);
54
2119
        --nb_bits;
55
    }
56
57
483
    return 0;
58
}
59
60
1568
static int FUNC(byte_alignment)(CodedBitstreamContext *ctx, RWContext *rw)
61
{
62
    int err;
63
64
5357
    while (byte_alignment(rw) != 0)
65
3789
        fixed(1, zero_bit, 0);
66
67
1568
    return 0;
68
}
69
70
294
static int FUNC(color_config)(CodedBitstreamContext *ctx, RWContext *rw,
71
                              AV1RawColorConfig *current, int seq_profile)
72
{
73
294
    CodedBitstreamAV1Context *priv = ctx->priv_data;
74
    int err;
75
76
294
    flag(high_bitdepth);
77
78
294
    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
294
        priv->bit_depth = current->high_bitdepth ? 10 : 8;
84
    }
85
86
294
    if (seq_profile == FF_PROFILE_AV1_HIGH)
87
        infer(mono_chrome, 0);
88
    else
89
294
        flag(mono_chrome);
90
294
    priv->num_planes = current->mono_chrome ? 1 : 3;
91
92
294
    flag(color_description_present_flag);
93
294
    if (current->color_description_present_flag) {
94
        fb(8, color_primaries);
95
        fb(8, transfer_characteristics);
96
        fb(8, matrix_coefficients);
97
    } else {
98
294
        infer(color_primaries,          AVCOL_PRI_UNSPECIFIED);
99
294
        infer(transfer_characteristics, AVCOL_TRC_UNSPECIFIED);
100
294
        infer(matrix_coefficients,      AVCOL_SPC_UNSPECIFIED);
101
    }
102
103
294
    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
294
    } 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
294
        flag(color_range);
121
122
294
        if (seq_profile == FF_PROFILE_AV1_MAIN) {
123
294
            infer(subsampling_x, 1);
124
294
            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

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

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

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

224
        if (!used_frame[i] && hint >= cur_frame_hint &&
406
4
            (ref < 0 || hint < earliest_order_hint)) {
407
24
            ref = i;
408
24
            earliest_order_hint = hint;
409
        }
410
    }
411
28
    if (ref >= 0) {
412
24
        ref_frame_idx[AV1_REF_FRAME_ALTREF2 - AV1_REF_FRAME_LAST] = ref;
413
24
        used_frame[ref] = 1;
414
    }
415
416
168
    for (i = 0; i < AV1_REFS_PER_FRAME - 2; i++) {
417
140
        int ref_frame = ref_frame_list[i];
418
140
        if (ref_frame_idx[ref_frame - AV1_REF_FRAME_LAST] < 0 ) {
419
60
            ref = -1;
420
540
            for (j = 0; j < AV1_NUM_REF_FRAMES; j++) {
421
480
                int hint = shifted_order_hints[j];
422

480
                if (!used_frame[j] && hint < cur_frame_hint &&
423
88
                    (ref < 0 || hint >= latest_order_hint)) {
424
112
                    ref = j;
425
112
                    latest_order_hint = hint;
426
                }
427
            }
428
60
            if (ref >= 0) {
429
60
                ref_frame_idx[ref_frame - AV1_REF_FRAME_LAST] = ref;
430
60
                used_frame[ref] = 1;
431
            }
432
        }
433
    }
434
435
28
    ref = -1;
436
252
    for (i = 0; i < AV1_NUM_REF_FRAMES; i++) {
437
224
        int hint = shifted_order_hints[i];
438

224
        if (ref < 0 || hint < earliest_order_hint) {
439
50
            ref = i;
440
50
            earliest_order_hint = hint;
441
        }
442
    }
443
224
    for (i = 0; i < AV1_REFS_PER_FRAME; i++) {
444
196
        if (ref_frame_idx[i] < 0)
445
            ref_frame_idx[i] = ref;
446
196
        infer(ref_frame_idx[i], ref_frame_idx[i]);
447
    }
448
449
28
    return 0;
450
}
451
452
760
static int FUNC(superres_params)(CodedBitstreamContext *ctx, RWContext *rw,
453
                                 AV1RawFrameHeader *current)
454
{
455
760
    CodedBitstreamAV1Context  *priv = ctx->priv_data;
456
760
    const AV1RawSequenceHeader *seq = priv->sequence_header;
457
    int denom, err;
458
459
760
    if (seq->enable_superres)
460
        flag(use_superres);
461
    else
462
760
        infer(use_superres, 0);
463
464
760
    if (current->use_superres) {
465
        fb(3, coded_denom);
466
        denom = current->coded_denom + AV1_SUPERRES_DENOM_MIN;
467
    } else {
468
760
        denom = AV1_SUPERRES_NUM;
469
    }
470
471
760
    priv->upscaled_width = priv->frame_width;
472
760
    priv->frame_width = (priv->upscaled_width * AV1_SUPERRES_NUM +
473
760
                         denom / 2) / denom;
474
475
760
    return 0;
476
}
477
478
730
static int FUNC(frame_size)(CodedBitstreamContext *ctx, RWContext *rw,
479
                            AV1RawFrameHeader *current)
480
{
481
730
    CodedBitstreamAV1Context  *priv = ctx->priv_data;
482
730
    const AV1RawSequenceHeader *seq = priv->sequence_header;
483
    int err;
484
485
730
    if (current->frame_size_override_flag) {
486
6
        fb(seq->frame_width_bits_minus_1 + 1,  frame_width_minus_1);
487
6
        fb(seq->frame_height_bits_minus_1 + 1, frame_height_minus_1);
488
489
6
        priv->frame_width  = current->frame_width_minus_1  + 1;
490
6
        priv->frame_height = current->frame_height_minus_1 + 1;
491
    } else {
492
724
        priv->frame_width  = seq->max_frame_width_minus_1  + 1;
493
724
        priv->frame_height = seq->max_frame_height_minus_1 + 1;
494
    }
495
496
730
    CHECK(FUNC(superres_params)(ctx, rw, current));
497
498
730
    return 0;
499
}
500
501
730
static int FUNC(render_size)(CodedBitstreamContext *ctx, RWContext *rw,
502
                             AV1RawFrameHeader *current)
503
{
504
730
    CodedBitstreamAV1Context *priv = ctx->priv_data;
505
    int err;
506
507
730
    flag(render_and_frame_size_different);
508
509
730
    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
730
        priv->render_width  = priv->upscaled_width;
517
730
        priv->render_height = priv->frame_height;
518
    }
519
520
730
    return 0;
521
}
522
523
30
static int FUNC(frame_size_with_refs)(CodedBitstreamContext *ctx, RWContext *rw,
524
                                      AV1RawFrameHeader *current)
525
{
526
30
    CodedBitstreamAV1Context *priv = ctx->priv_data;
527
    int i, err;
528
529
30
    for (i = 0; i < AV1_REFS_PER_FRAME; i++) {
530
30
        flags(found_ref[i], 1, i);
531
30
        if (current->found_ref[i]) {
532
30
            AV1ReferenceFrameState *ref =
533
30
                &priv->ref[current->ref_frame_idx[i]];
534
535
30
            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
30
            priv->upscaled_width = ref->upscaled_width;
544
30
            priv->frame_width    = ref->frame_width;
545
30
            priv->frame_height   = ref->frame_height;
546
30
            priv->render_width   = ref->render_width;
547
30
            priv->render_height  = ref->render_height;
548
30
            break;
549
        }
550
    }
551
552
30
    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
30
        CHECK(FUNC(superres_params)(ctx, rw, current));
557
    }
558
559
30
    return 0;
560
}
561
562
641
static int FUNC(interpolation_filter)(CodedBitstreamContext *ctx, RWContext *rw,
563
                                      AV1RawFrameHeader *current)
564
{
565
    int err;
566
567
641
    flag(is_filter_switchable);
568
641
    if (current->is_filter_switchable)
569
511
        infer(interpolation_filter,
570
              AV1_INTERPOLATION_FILTER_SWITCHABLE);
571
    else
572
130
        fb(2, interpolation_filter);
573
574
641
    return 0;
575
}
576
577
760
static int FUNC(tile_info)(CodedBitstreamContext *ctx, RWContext *rw,
578
                           AV1RawFrameHeader *current)
579
{
580
760
    CodedBitstreamAV1Context  *priv = ctx->priv_data;
581
760
    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
760
    mi_cols = 2 * ((priv->frame_width  + 7) >> 3);
589
760
    mi_rows = 2 * ((priv->frame_height + 7) >> 3);
590
591
1924
    sb_cols = seq->use_128x128_superblock ? ((mi_cols + 31) >> 5)
592
760
                                          : ((mi_cols + 15) >> 4);
593
1924
    sb_rows = seq->use_128x128_superblock ? ((mi_rows + 31) >> 5)
594
760
                                          : ((mi_rows + 15) >> 4);
595
596
760
    sb_shift = seq->use_128x128_superblock ? 5 : 4;
597
760
    sb_size  = sb_shift + 2;
598
599
760
    max_tile_width_sb = AV1_MAX_TILE_WIDTH >> sb_size;
600
760
    max_tile_area_sb  = AV1_MAX_TILE_AREA  >> (2 * sb_size);
601
602
760
    min_log2_tile_cols = cbs_av1_tile_log2(max_tile_width_sb, sb_cols);
603
760
    max_log2_tile_cols = cbs_av1_tile_log2(1, FFMIN(sb_cols, AV1_MAX_TILE_COLS));
604
760
    max_log2_tile_rows = cbs_av1_tile_log2(1, FFMIN(sb_rows, AV1_MAX_TILE_ROWS));
605
760
    min_log2_tiles = FFMAX(min_log2_tile_cols,
606
                           cbs_av1_tile_log2(max_tile_area_sb, sb_rows * sb_cols));
607
608
760
    flag(uniform_tile_spacing_flag);
609
610
760
    if (current->uniform_tile_spacing_flag) {
611
        int tile_width_sb, tile_height_sb;
612
613
708
        increment(tile_cols_log2, min_log2_tile_cols, max_log2_tile_cols);
614
615
708
        tile_width_sb = (sb_cols + (1 << current->tile_cols_log2) - 1) >>
616
708
            current->tile_cols_log2;
617
708
        current->tile_cols = (sb_cols + tile_width_sb - 1) / tile_width_sb;
618
619
708
        min_log2_tile_rows = FFMAX(min_log2_tiles - current->tile_cols_log2, 0);
620
621
708
        increment(tile_rows_log2, min_log2_tile_rows, max_log2_tile_rows);
622
623
708
        tile_height_sb = (sb_rows + (1 << current->tile_rows_log2) - 1) >>
624
708
            current->tile_rows_log2;
625
708
        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
52
        widest_tile_sb = 0;
631
632
52
        start_sb = 0;
633

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

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

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

760
        delta_q(delta_q_u_dc);
693

760
        delta_q(delta_q_u_ac);
694
695
760
        if (current->diff_uv_delta) {
696
            delta_q(delta_q_v_dc);
697
            delta_q(delta_q_v_ac);
698
        } else {
699
760
            infer(delta_q_v_dc, current->delta_q_u_dc);
700
760
            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
760
    flag(using_qmatrix);
710
760
    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
760
    return 0;
720
}
721
722
760
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
760
    flag(segmentation_enabled);
730
731
760
    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
6840
        for (i = 0; i < AV1_MAX_SEGMENTS; i++) {
763
54720
            for (j = 0; j < AV1_SEG_LVL_MAX; j++) {
764
48640
                infer(feature_enabled[i][j], 0);
765
48640
                infer(feature_value[i][j],   0);
766
            }
767
        }
768
    }
769
770
760
    return 0;
771
}
772
773
760
static int FUNC(delta_q_params)(CodedBitstreamContext *ctx, RWContext *rw,
774
                                AV1RawFrameHeader *current)
775
{
776
    int err;
777
778
760
    if (current->base_q_idx > 0)
779
760
        flag(delta_q_present);
780
    else
781
        infer(delta_q_present, 0);
782
783
760
    if (current->delta_q_present)
784
8
        fb(2, delta_q_res);
785
786
760
    return 0;
787
}
788
789
760
static int FUNC(delta_lf_params)(CodedBitstreamContext *ctx, RWContext *rw,
790
                                 AV1RawFrameHeader *current)
791
{
792
    int err;
793
794
760
    if (current->delta_q_present) {
795
8
        if (!current->allow_intrabc)
796
8
            flag(delta_lf_present);
797
        else
798
            infer(delta_lf_present, 0);
799
8
        if (current->delta_lf_present) {
800
            fb(2, delta_lf_res);
801
            flag(delta_lf_multi);
802
        } else {
803
8
            infer(delta_lf_res,   0);
804
8
            infer(delta_lf_multi, 0);
805
        }
806
    } else {
807
752
        infer(delta_lf_present, 0);
808
752
        infer(delta_lf_res,     0);
809
752
        infer(delta_lf_multi,   0);
810
    }
811
812
760
    return 0;
813
}
814
815
760
static int FUNC(loop_filter_params)(CodedBitstreamContext *ctx, RWContext *rw,
816
                                    AV1RawFrameHeader *current)
817
{
818
760
    CodedBitstreamAV1Context *priv = ctx->priv_data;
819
    int i, err;
820
821

760
    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
760
    fb(6, loop_filter_level[0]);
838
760
    fb(6, loop_filter_level[1]);
839
840
760
    if (priv->num_planes > 1) {
841
760
        if (current->loop_filter_level[0] ||
842
381
            current->loop_filter_level[1]) {
843
387
            fb(6, loop_filter_level[2]);
844
387
            fb(6, loop_filter_level[3]);
845
        }
846
    }
847
848
760
    fb(3, loop_filter_sharpness);
849
850
760
    flag(loop_filter_delta_enabled);
851
760
    if (current->loop_filter_delta_enabled) {
852
760
        flag(loop_filter_delta_update);
853
760
        if (current->loop_filter_delta_update) {
854
1890
            for (i = 0; i < AV1_TOTAL_REFS_PER_FRAME; i++) {
855
1680
                flags(update_ref_delta[i], 1, i);
856
1680
                if (current->update_ref_delta[i])
857
                    sus(1 + 6, loop_filter_ref_deltas[i], 1, i);
858
            }
859
630
            for (i = 0; i < 2; i++) {
860
420
                flags(update_mode_delta[i], 1, i);
861
420
                if (current->update_mode_delta[i])
862
                    sus(1 + 6, loop_filter_mode_deltas[i], 1, i);
863
            }
864
        }
865
    }
866
867
760
    return 0;
868
}
869
870
760
static int FUNC(cdef_params)(CodedBitstreamContext *ctx, RWContext *rw,
871
                             AV1RawFrameHeader *current)
872
{
873
760
    CodedBitstreamAV1Context  *priv = ctx->priv_data;
874
760
    const AV1RawSequenceHeader *seq = priv->sequence_header;
875
    int i, err;
876
877

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

760
    if (priv->all_lossless || current->allow_intrabc ||
914
760
        !seq->enable_restoration) {
915
64
        return 0;
916
    }
917
918
696
    uses_lr = uses_chroma_lr = 0;
919
2784
    for (i = 0; i < priv->num_planes; i++) {
920
2088
        fbs(2, lr_type[i], 1, i);
921
922
2088
        if (current->lr_type[i] != 0) {
923
553
            uses_lr = 1;
924
553
            if (i > 0)
925
212
                uses_chroma_lr = 1;
926
        }
927
    }
928
929
696
    if (uses_lr) {
930
383
        if (seq->use_128x128_superblock)
931
247
            increment(lr_unit_shift, 1, 2);
932
        else
933
136
            increment(lr_unit_shift, 0, 2);
934
935
383
        if(seq->color_config.subsampling_x &&
936

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

641
        !current->reference_select || !seq->enable_order_hint) {
985
408
        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
352
        forward_idx  = -1;
992
352
        backward_idx = -1;
993
2816
        for (i = 0; i < AV1_REFS_PER_FRAME; i++) {
994
2464
            ref_hint = priv->ref[current->ref_frame_idx[i]].order_hint;
995
2464
            dist = cbs_av1_get_relative_dist(seq, ref_hint,
996
2464
                                             current->order_hint);
997
2464
            if (dist < 0) {
998

3804
                if (forward_idx < 0 ||
999
1726
                    cbs_av1_get_relative_dist(seq, ref_hint,
1000
                                              forward_hint) > 0) {
1001
399
                    forward_idx  = i;
1002
399
                    forward_hint = ref_hint;
1003
                }
1004
386
            } else if (dist > 0) {
1005

540
                if (backward_idx < 0 ||
1006
168
                    cbs_av1_get_relative_dist(seq, ref_hint,
1007
                                              backward_hint) < 0) {
1008
204
                    backward_idx  = i;
1009
204
                    backward_hint = ref_hint;
1010
                }
1011
            }
1012
        }
1013
1014
352
        if (forward_idx < 0) {
1015
            skip_mode_allowed = 0;
1016
352
        } else if (backward_idx >= 0) {
1017
204
            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
148
            second_forward_idx = -1;
1024
1184
            for (i = 0; i < AV1_REFS_PER_FRAME; i++) {
1025
1036
                ref_hint = priv->ref[current->ref_frame_idx[i]].order_hint;
1026
1036
                if (cbs_av1_get_relative_dist(seq, ref_hint,
1027
                                              forward_hint) < 0) {
1028

1511
                    if (second_forward_idx < 0 ||
1029
685
                        cbs_av1_get_relative_dist(seq, ref_hint,
1030
                                                  second_forward_hint) > 0) {
1031
149
                        second_forward_idx  = i;
1032
149
                        second_forward_hint = ref_hint;
1033
                    }
1034
                }
1035
            }
1036
1037
148
            if (second_forward_idx < 0) {
1038
7
                skip_mode_allowed = 0;
1039
            } else {
1040
141
                skip_mode_allowed = 1;
1041
                // Frames for skip mode are forward_idx and second_forward_idx.
1042
            }
1043
        }
1044
    }
1045
1046
760
    if (skip_mode_allowed)
1047
345
        flag(skip_mode_present);
1048
    else
1049
415
        infer(skip_mode_present, 0);
1050
1051
760
    return 0;
1052
}
1053
1054
168
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
168
    if (idx < 2) {
1062
84
        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
84
            abs_bits  = AV1_GM_ABS_TRANS_BITS;
1067
84
            prec_bits = AV1_GM_TRANS_PREC_BITS;
1068
        }
1069
    } else {
1070
84
        abs_bits  = AV1_GM_ABS_ALPHA_BITS;
1071
84
        prec_bits = AV1_GM_ALPHA_PREC_BITS;
1072
    }
1073
1074
168
    num_syms = 2 * (1 << abs_bits) + 1;
1075
168
    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
168
    return 0;
1081
}
1082
1083
760
static int FUNC(global_motion_params)(CodedBitstreamContext *ctx, RWContext *rw,
1084
                                      AV1RawFrameHeader *current)
1085
{
1086
    int ref, type;
1087
    int err;
1088
1089
760
    if (current->frame_type == AV1_FRAME_KEY ||
1090
641
        current->frame_type == AV1_FRAME_INTRA_ONLY)
1091
119
        return 0;
1092
1093
5128
    for (ref = AV1_REF_FRAME_LAST; ref <= AV1_REF_FRAME_ALTREF; ref++) {
1094
4487
        flags(is_global[ref], 1, ref);
1095
4487
        if (current->is_global[ref]) {
1096
42
            flags(is_rot_zoom[ref], 1, ref);
1097
42
            if (current->is_rot_zoom[ref]) {
1098
42
                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
4445
            type = AV1_WARP_MODEL_IDENTITY;
1106
        }
1107
1108
4487
        if (type >= AV1_WARP_MODEL_ROTZOOM) {
1109
42
            CHECK(FUNC(global_motion_param)(ctx, rw, current, type, ref, 2));
1110
42
            CHECK(FUNC(global_motion_param)(ctx, rw, current, type, ref, 3));
1111
42
            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
4487
        if (type >= AV1_WARP_MODEL_TRANSLATION) {
1120
42
            CHECK(FUNC(global_motion_param)(ctx, rw, current, type, ref, 0));
1121
42
            CHECK(FUNC(global_motion_param)(ctx, rw, current, type, ref, 1));
1122
        }
1123
    }
1124
1125
641
    return 0;
1126
}
1127
1128
760
static int FUNC(film_grain_params)(CodedBitstreamContext *ctx, RWContext *rw,
1129
                                   AV1RawFrameHeader *current)
1130
{
1131
760
    CodedBitstreamAV1Context  *priv = ctx->priv_data;
1132
760
    const AV1RawSequenceHeader *seq = priv->sequence_header;
1133
    int num_pos_luma, num_pos_chroma;
1134
    int i, err;
1135
1136
760
    if (!seq->film_grain_params_present ||
1137

44
        (!current->show_frame && !current->showable_frame))
1138
720
        return 0;
1139
1140
40
    flag(apply_grain);
1141
1142
40
    if (!current->apply_grain)
1143
        return 0;
1144
1145
40
    fb(16, grain_seed);
1146
1147
40
    if (current->frame_type == AV1_FRAME_INTER)
1148
36
        flag(update_grain);
1149
    else
1150
4
        infer(update_grain, 1);
1151
1152
40
    if (!current->update_grain) {
1153
        fb(3, film_grain_params_ref_idx);
1154
        return 0;
1155
    }
1156
1157
40
    fc(4, num_y_points, 0, 14);
1158
316
    for (i = 0; i < current->num_y_points; i++) {
1159
276
        fbs(8, point_y_value[i],   1, i);
1160
276
        fbs(8, point_y_scaling[i], 1, i);
1161
    }
1162
1163
40
    if (seq->color_config.mono_chrome)
1164
        infer(chroma_scaling_from_luma, 0);
1165
    else
1166
40
        flag(chroma_scaling_from_luma);
1167
1168
40
    if (seq->color_config.mono_chrome ||
1169
40
        current->chroma_scaling_from_luma ||
1170
40
        (seq->color_config.subsampling_x == 1 &&
1171
40
         seq->color_config.subsampling_y == 1 &&
1172
40
         current->num_y_points == 0)) {
1173
        infer(num_cb_points, 0);
1174
        infer(num_cr_points, 0);
1175
    } else {
1176
40
        fc(4, num_cb_points, 0, 10);
1177
196
        for (i = 0; i < current->num_cb_points; i++) {
1178
156
            fbs(8, point_cb_value[i],   1, i);
1179
156
            fbs(8, point_cb_scaling[i], 1, i);
1180
        }
1181
40
        fc(4, num_cr_points, 0, 10);
1182
200
        for (i = 0; i < current->num_cr_points; i++) {
1183
160
            fbs(8, point_cr_value[i],   1, i);
1184
160
            fbs(8, point_cr_scaling[i], 1, i);
1185
        }
1186
    }
1187
1188
40
    fb(2, grain_scaling_minus_8);
1189
40
    fb(2, ar_coeff_lag);
1190
40
    num_pos_luma = 2 * current->ar_coeff_lag * (current->ar_coeff_lag + 1);
1191
40
    if (current->num_y_points) {
1192
40
        num_pos_chroma = num_pos_luma + 1;
1193
1000
        for (i = 0; i < num_pos_luma; i++)
1194
960
            fbs(8, ar_coeffs_y_plus_128[i], 1, i);
1195
    } else {
1196
        num_pos_chroma = num_pos_luma;
1197
    }
1198

40
    if (current->chroma_scaling_from_luma || current->num_cb_points) {
1199
1040
        for (i = 0; i < num_pos_chroma; i++)
1200
1000
            fbs(8, ar_coeffs_cb_plus_128[i], 1, i);
1201
    }
1202

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

758
            (current->frame_type == AV1_FRAME_KEY && current->show_frame))
1295
121
            infer(error_resilient_mode, 1);
1296
        else
1297
639
            flag(error_resilient_mode);
1298
    }
1299
1300

760
    if (current->frame_type == AV1_FRAME_KEY && current->show_frame) {
1301
1071
        for (i = 0; i < AV1_NUM_REF_FRAMES; i++) {
1302
952
            priv->ref[i].valid = 0;
1303
952
            priv->ref[i].order_hint = 0;
1304
        }
1305
    }
1306
1307
760
    flag(disable_cdf_update);
1308
1309
760
    if (seq->seq_force_screen_content_tools ==
1310
        AV1_SELECT_SCREEN_CONTENT_TOOLS) {
1311
760
        flag(allow_screen_content_tools);
1312
    } else {
1313
        infer(allow_screen_content_tools,
1314
              seq->seq_force_screen_content_tools);
1315
    }
1316
760
    if (current->allow_screen_content_tools) {
1317
22
        if (seq->seq_force_integer_mv == AV1_SELECT_INTEGER_MV)
1318
22
            flag(force_integer_mv);
1319
        else
1320
            infer(force_integer_mv, seq->seq_force_integer_mv);
1321
    } else {
1322
738
        infer(force_integer_mv, 0);
1323
    }
1324
1325
760
    if (seq->frame_id_numbers_present_flag) {
1326
        fb(id_len, current_frame_id);
1327
1328
        diff_len = seq->delta_frame_id_length_minus_2 + 2;
1329
        for (i = 0; i < AV1_NUM_REF_FRAMES; i++) {
1330
            if (current->current_frame_id > (1 << diff_len)) {
1331
                if (priv->ref[i].frame_id > current->current_frame_id ||
1332
                    priv->ref[i].frame_id < (current->current_frame_id -
1333
                                             (1 << diff_len)))
1334
                    priv->ref[i].valid = 0;
1335
            } else {
1336
                if (priv->ref[i].frame_id > current->current_frame_id &&
1337
                    priv->ref[i].frame_id < ((1 << id_len) +
1338
                                             current->current_frame_id -
1339
                                             (1 << diff_len)))
1340
                    priv->ref[i].valid = 0;
1341
            }
1342
        }
1343
    } else {
1344
760
        infer(current_frame_id, 0);
1345
    }
1346
1347
760
    if (current->frame_type == AV1_FRAME_SWITCH)
1348
2
        infer(frame_size_override_flag, 1);
1349
758
    else if(seq->reduced_still_picture_header)
1350
        infer(frame_size_override_flag, 0);
1351
    else
1352
758
        flag(frame_size_override_flag);
1353
1354
760
    order_hint_bits =
1355
760
        seq->enable_order_hint ? seq->order_hint_bits_minus_1 + 1 : 0;
1356
760
    if (order_hint_bits > 0)
1357
760
        fb(order_hint_bits, order_hint);
1358
    else
1359
        infer(order_hint, 0);
1360
1361

760
    if (frame_is_intra || current->error_resilient_mode)
1362
121
        infer(primary_ref_frame, AV1_PRIMARY_REF_NONE);
1363
    else
1364
639
        fb(3, primary_ref_frame);
1365
1366
760
    if (seq->decoder_model_info_present_flag) {
1367
194
        flag(buffer_removal_time_present_flag);
1368
194
        if (current->buffer_removal_time_present_flag) {
1369
388
            for (i = 0; i <= seq->operating_points_cnt_minus_1; i++) {
1370
194
                if (seq->decoder_model_present_for_this_op[i]) {
1371
194
                    int op_pt_idc = seq->operating_point_idc[i];
1372
194
                    int in_temporal_layer = (op_pt_idc >>  priv->temporal_id    ) & 1;
1373
194
                    int in_spatial_layer  = (op_pt_idc >> (priv->spatial_id + 8)) & 1;
1374

194
                    if (seq->operating_point_idc[i] == 0 ||
1375
                        in_temporal_layer || in_spatial_layer) {
1376
194
                        fbs(seq->decoder_model_info.buffer_removal_time_length_minus_1 + 1,
1377
                            buffer_removal_time[i], 1, i);
1378
                    }
1379
                }
1380
            }
1381
        }
1382
    }
1383
1384
760
    if (current->frame_type == AV1_FRAME_SWITCH ||
1385

758
        (current->frame_type == AV1_FRAME_KEY && current->show_frame))
1386
121
        infer(refresh_frame_flags, all_frames);
1387
    else
1388
639
        fb(8, refresh_frame_flags);
1389
1390

760
    if (!frame_is_intra || current->refresh_frame_flags != all_frames) {
1391

641
        if (current->error_resilient_mode && seq->enable_order_hint) {
1392
18
            for (i = 0; i < AV1_NUM_REF_FRAMES; i++) {
1393
16
                fbs(order_hint_bits, ref_order_hint[i], 1, i);
1394
16
                if (current->ref_order_hint[i] != priv->ref[i].order_hint)
1395
                    priv->ref[i].valid = 0;
1396
            }
1397
        }
1398
    }
1399
1400
760
    if (current->frame_type == AV1_FRAME_KEY ||
1401
641
        current->frame_type == AV1_FRAME_INTRA_ONLY) {
1402
119
        CHECK(FUNC(frame_size)(ctx, rw, current));
1403
119
        CHECK(FUNC(render_size)(ctx, rw, current));
1404
1405
119
        if (current->allow_screen_content_tools &&
1406
2
            priv->upscaled_width == priv->frame_width)
1407
2
            flag(allow_intrabc);
1408
        else
1409
117
            infer(allow_intrabc, 0);
1410
1411
    } else {
1412
641
        if (!seq->enable_order_hint) {
1413
            infer(frame_refs_short_signaling, 0);
1414
        } else {
1415
641
            flag(frame_refs_short_signaling);
1416
641
            if (current->frame_refs_short_signaling) {
1417
28
                fb(3, last_frame_idx);
1418
28
                fb(3, golden_frame_idx);
1419
28
                CHECK(FUNC(set_frame_refs)(ctx, rw, current));
1420
            }
1421
        }
1422
1423
5128
        for (i = 0; i < AV1_REFS_PER_FRAME; i++) {
1424
4487
            if (!current->frame_refs_short_signaling)
1425
4291
                fbs(3, ref_frame_idx[i], 1, i);
1426
4487
            if (seq->frame_id_numbers_present_flag) {
1427
                fbs(seq->delta_frame_id_length_minus_2 + 2,
1428
                    delta_frame_id_minus1[i], 1, i);
1429
            }
1430
        }
1431
1432
641
        if (current->frame_size_override_flag &&
1433
32
            !current->error_resilient_mode) {
1434
30
            CHECK(FUNC(frame_size_with_refs)(ctx, rw, current));
1435
        } else {
1436
611
            CHECK(FUNC(frame_size)(ctx, rw, current));
1437
611
            CHECK(FUNC(render_size)(ctx, rw, current));
1438
        }
1439
1440
641
        if (current->force_integer_mv)
1441
            infer(allow_high_precision_mv, 0);
1442
        else
1443
641
            flag(allow_high_precision_mv);
1444
1445
641
        CHECK(FUNC(interpolation_filter)(ctx, rw, current));
1446
1447
641
        flag(is_motion_mode_switchable);
1448
1449
641
        if (current->error_resilient_mode ||
1450
639
            !seq->enable_ref_frame_mvs)
1451
62
            infer(use_ref_frame_mvs, 0);
1452
        else
1453
579
            flag(use_ref_frame_mvs);
1454
1455
641
        infer(allow_intrabc, 0);
1456
    }
1457
1458
    if (!frame_is_intra) {
1459
        // Derive reference frame sign biases.
1460
    }
1461
1462

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

6080
        if (qindex                || current->delta_q_y_dc ||
1503
            current->delta_q_u_ac || current->delta_q_u_dc ||
1504
            current->delta_q_v_ac || current->delta_q_v_dc) {
1505
6080
            priv->coded_lossless = 0;
1506
        }
1507
    }
1508
760
    priv->all_lossless = priv->coded_lossless &&
1509
        priv->frame_width == priv->upscaled_width;
1510
1511
760
    CHECK(FUNC(loop_filter_params)(ctx, rw, current));
1512
1513
760
    CHECK(FUNC(cdef_params)(ctx, rw, current));
1514
1515
760
    CHECK(FUNC(lr_params)(ctx, rw, current));
1516
1517
760
    CHECK(FUNC(read_tx_mode)(ctx, rw, current));
1518
1519
760
    CHECK(FUNC(frame_reference_mode)(ctx, rw, current));
1520
1521
760
    CHECK(FUNC(skip_mode_params)(ctx, rw, current));
1522
1523

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

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

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