GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/cbs_av1_syntax_template.c Lines: 814 1104 73.7 %
Date: 2020-09-21 17:35:45 Branches: 698 1556 44.9 %

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

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

896
        if (!used_frame[i] && hint >= cur_frame_hint &&
385
224
            (ref < 0 || hint >= latest_order_hint)) {
386
232
            ref = i;
387
232
            latest_order_hint = hint;
388
        }
389
    }
390
112
    if (ref >= 0) {
391
112
        ref_frame_idx[AV1_REF_FRAME_ALTREF - AV1_REF_FRAME_LAST] = ref;
392
112
        used_frame[ref] = 1;
393
    }
394
395
112
    ref = -1;
396
1008
    for (i = 0; i < AV1_NUM_REF_FRAMES; i++) {
397
896
        int hint = shifted_order_hints[i];
398

896
        if (!used_frame[i] && hint >= cur_frame_hint &&
399
112
            (ref < 0 || hint < earliest_order_hint)) {
400
120
            ref = i;
401
120
            earliest_order_hint = hint;
402
        }
403
    }
404
112
    if (ref >= 0) {
405
112
        ref_frame_idx[AV1_REF_FRAME_BWDREF - AV1_REF_FRAME_LAST] = ref;
406
112
        used_frame[ref] = 1;
407
    }
408
409
112
    ref = -1;
410
1008
    for (i = 0; i < AV1_NUM_REF_FRAMES; i++) {
411
896
        int hint = shifted_order_hints[i];
412

896
        if (!used_frame[i] && hint >= cur_frame_hint &&
413
16
            (ref < 0 || hint < earliest_order_hint)) {
414
96
            ref = i;
415
96
            earliest_order_hint = hint;
416
        }
417
    }
418
112
    if (ref >= 0) {
419
96
        ref_frame_idx[AV1_REF_FRAME_ALTREF2 - AV1_REF_FRAME_LAST] = ref;
420
96
        used_frame[ref] = 1;
421
    }
422
423
672
    for (i = 0; i < AV1_REFS_PER_FRAME - 2; i++) {
424
560
        int ref_frame = ref_frame_list[i];
425
560
        if (ref_frame_idx[ref_frame - AV1_REF_FRAME_LAST] < 0 ) {
426
240
            ref = -1;
427
2160
            for (j = 0; j < AV1_NUM_REF_FRAMES; j++) {
428
1920
                int hint = shifted_order_hints[j];
429

1920
                if (!used_frame[j] && hint < cur_frame_hint &&
430
352
                    (ref < 0 || hint >= latest_order_hint)) {
431
448
                    ref = j;
432
448
                    latest_order_hint = hint;
433
                }
434
            }
435
240
            if (ref >= 0) {
436
240
                ref_frame_idx[ref_frame - AV1_REF_FRAME_LAST] = ref;
437
240
                used_frame[ref] = 1;
438
            }
439
        }
440
    }
441
442
112
    ref = -1;
443
1008
    for (i = 0; i < AV1_NUM_REF_FRAMES; i++) {
444
896
        int hint = shifted_order_hints[i];
445

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

416
        for (i = 0; start_sb < sb_cols && i < AV1_MAX_TILE_COLS; i++) {
655
208
            max_width = FFMIN(sb_cols - start_sb, max_tile_width_sb);
656
208
            ns(max_width, width_in_sbs_minus_1[i], 1, i);
657
208
            size_sb = current->width_in_sbs_minus_1[i] + 1;
658
208
            widest_tile_sb = FFMAX(size_sb, widest_tile_sb);
659
208
            start_sb += size_sb;
660
        }
661
208
        current->tile_cols_log2 = cbs_av1_tile_log2(1, i);
662
208
        current->tile_cols = i;
663
664
208
        if (min_log2_tiles > 0)
665
            max_tile_area_sb = (sb_rows * sb_cols) >> (min_log2_tiles + 1);
666
        else
667
208
            max_tile_area_sb = sb_rows * sb_cols;
668
208
        max_tile_height_sb = FFMAX(max_tile_area_sb / widest_tile_sb, 1);
669
670
208
        start_sb = 0;
671

1040
        for (i = 0; start_sb < sb_rows && i < AV1_MAX_TILE_ROWS; i++) {
672
832
            max_height = FFMIN(sb_rows - start_sb, max_tile_height_sb);
673
832
            ns(max_height, height_in_sbs_minus_1[i], 1, i);
674
832
            size_sb = current->height_in_sbs_minus_1[i] + 1;
675
832
            start_sb += size_sb;
676
        }
677
208
        current->tile_rows_log2 = cbs_av1_tile_log2(1, i);
678
208
        current->tile_rows = i;
679
    }
680
681
3010
    if (current->tile_cols_log2 > 0 ||
682
2604
        current->tile_rows_log2 > 0) {
683
614
        fb(current->tile_cols_log2 + current->tile_rows_log2,
684
           context_update_tile_id);
685
614
        fb(2, tile_size_bytes_minus1);
686
    } else {
687
2396
        infer(context_update_tile_id, 0);
688
    }
689
690
3010
    priv->tile_cols = current->tile_cols;
691
3010
    priv->tile_rows = current->tile_rows;
692
693
3010
    return 0;
694
}
695
696
3010
static int FUNC(quantization_params)(CodedBitstreamContext *ctx, RWContext *rw,
697
                                     AV1RawFrameHeader *current)
698
{
699
3010
    CodedBitstreamAV1Context  *priv = ctx->priv_data;
700
3010
    const AV1RawSequenceHeader *seq = priv->sequence_header;
701
    int err;
702
703
3010
    fb(8, base_q_idx);
704
705

3010
    delta_q(delta_q_y_dc);
706
707
3010
    if (priv->num_planes > 1) {
708
3010
        if (seq->color_config.separate_uv_delta_q)
709
            flag(diff_uv_delta);
710
        else
711
3010
            infer(diff_uv_delta, 0);
712
713

3010
        delta_q(delta_q_u_dc);
714

3010
        delta_q(delta_q_u_ac);
715
716
3010
        if (current->diff_uv_delta) {
717
            delta_q(delta_q_v_dc);
718
            delta_q(delta_q_v_ac);
719
        } else {
720
3010
            infer(delta_q_v_dc, current->delta_q_u_dc);
721
3010
            infer(delta_q_v_ac, current->delta_q_u_ac);
722
        }
723
    } else {
724
        infer(delta_q_u_dc, 0);
725
        infer(delta_q_u_ac, 0);
726
        infer(delta_q_v_dc, 0);
727
        infer(delta_q_v_ac, 0);
728
    }
729
730
3010
    flag(using_qmatrix);
731
3010
    if (current->using_qmatrix) {
732
        fb(4, qm_y);
733
        fb(4, qm_u);
734
        if (seq->color_config.separate_uv_delta_q)
735
            fb(4, qm_v);
736
        else
737
            infer(qm_v, current->qm_u);
738
    }
739
740
3010
    return 0;
741
}
742
743
3010
static int FUNC(segmentation_params)(CodedBitstreamContext *ctx, RWContext *rw,
744
                                     AV1RawFrameHeader *current)
745
{
746
    static const uint8_t bits[AV1_SEG_LVL_MAX] = { 8, 6, 6, 6, 6, 3, 0, 0 };
747
    static const uint8_t sign[AV1_SEG_LVL_MAX] = { 1, 1, 1, 1, 1, 0, 0, 0 };
748
    int i, j, err;
749
750
3010
    flag(segmentation_enabled);
751
752
3010
    if (current->segmentation_enabled) {
753
        if (current->primary_ref_frame == AV1_PRIMARY_REF_NONE) {
754
            infer(segmentation_update_map,      1);
755
            infer(segmentation_temporal_update, 0);
756
            infer(segmentation_update_data,     1);
757
        } else {
758
            flag(segmentation_update_map);
759
            if (current->segmentation_update_map)
760
                flag(segmentation_temporal_update);
761
            else
762
                infer(segmentation_temporal_update, 0);
763
            flag(segmentation_update_data);
764
        }
765
766
        if (current->segmentation_update_data) {
767
            for (i = 0; i < AV1_MAX_SEGMENTS; i++) {
768
                for (j = 0; j < AV1_SEG_LVL_MAX; j++) {
769
                    flags(feature_enabled[i][j], 2, i, j);
770
771
                    if (current->feature_enabled[i][j] && bits[j] > 0) {
772
                        if (sign[j])
773
                            sus(1 + bits[j], feature_value[i][j], 2, i, j);
774
                        else
775
                            fbs(bits[j], feature_value[i][j], 2, i, j);
776
                    } else {
777
                        infer(feature_value[i][j], 0);
778
                    }
779
                }
780
            }
781
        }
782
    } else {
783
27090
        for (i = 0; i < AV1_MAX_SEGMENTS; i++) {
784
216720
            for (j = 0; j < AV1_SEG_LVL_MAX; j++) {
785
192640
                infer(feature_enabled[i][j], 0);
786
192640
                infer(feature_value[i][j],   0);
787
            }
788
        }
789
    }
790
791
3010
    return 0;
792
}
793
794
3010
static int FUNC(delta_q_params)(CodedBitstreamContext *ctx, RWContext *rw,
795
                                AV1RawFrameHeader *current)
796
{
797
    int err;
798
799
3010
    if (current->base_q_idx > 0)
800
3010
        flag(delta_q_present);
801
    else
802
        infer(delta_q_present, 0);
803
804
3010
    if (current->delta_q_present)
805
32
        fb(2, delta_q_res);
806
807
3010
    return 0;
808
}
809
810
3010
static int FUNC(delta_lf_params)(CodedBitstreamContext *ctx, RWContext *rw,
811
                                 AV1RawFrameHeader *current)
812
{
813
    int err;
814
815
3010
    if (current->delta_q_present) {
816
32
        if (!current->allow_intrabc)
817
32
            flag(delta_lf_present);
818
        else
819
            infer(delta_lf_present, 0);
820
32
        if (current->delta_lf_present) {
821
            fb(2, delta_lf_res);
822
            flag(delta_lf_multi);
823
        } else {
824
32
            infer(delta_lf_res,   0);
825
32
            infer(delta_lf_multi, 0);
826
        }
827
    } else {
828
2978
        infer(delta_lf_present, 0);
829
2978
        infer(delta_lf_res,     0);
830
2978
        infer(delta_lf_multi,   0);
831
    }
832
833
3010
    return 0;
834
}
835
836
3010
static int FUNC(loop_filter_params)(CodedBitstreamContext *ctx, RWContext *rw,
837
                                    AV1RawFrameHeader *current)
838
{
839
3010
    CodedBitstreamAV1Context *priv = ctx->priv_data;
840
    int i, err;
841
842

3010
    if (priv->coded_lossless || current->allow_intrabc) {
843
        infer(loop_filter_level[0], 0);
844
        infer(loop_filter_level[1], 0);
845
        infer(loop_filter_ref_deltas[AV1_REF_FRAME_INTRA],    1);
846
        infer(loop_filter_ref_deltas[AV1_REF_FRAME_LAST],     0);
847
        infer(loop_filter_ref_deltas[AV1_REF_FRAME_LAST2],    0);
848
        infer(loop_filter_ref_deltas[AV1_REF_FRAME_LAST3],    0);
849
        infer(loop_filter_ref_deltas[AV1_REF_FRAME_BWDREF],   0);
850
        infer(loop_filter_ref_deltas[AV1_REF_FRAME_GOLDEN],  -1);
851
        infer(loop_filter_ref_deltas[AV1_REF_FRAME_ALTREF],  -1);
852
        infer(loop_filter_ref_deltas[AV1_REF_FRAME_ALTREF2], -1);
853
        for (i = 0; i < 2; i++)
854
            infer(loop_filter_mode_deltas[i], 0);
855
        return 0;
856
    }
857
858
3010
    fb(6, loop_filter_level[0]);
859
3010
    fb(6, loop_filter_level[1]);
860
861
3010
    if (priv->num_planes > 1) {
862
3010
        if (current->loop_filter_level[0] ||
863
1484
            current->loop_filter_level[1]) {
864
1558
            fb(6, loop_filter_level[2]);
865
1558
            fb(6, loop_filter_level[3]);
866
        }
867
    }
868
869
3010
    fb(3, loop_filter_sharpness);
870
871
3010
    flag(loop_filter_delta_enabled);
872
3010
    if (current->loop_filter_delta_enabled) {
873
3010
        flag(loop_filter_delta_update);
874
3010
        if (current->loop_filter_delta_update) {
875
7236
            for (i = 0; i < AV1_TOTAL_REFS_PER_FRAME; i++) {
876
6432
                flags(update_ref_delta[i], 1, i);
877
6432
                if (current->update_ref_delta[i])
878
                    sus(1 + 6, loop_filter_ref_deltas[i], 1, i);
879
            }
880
2412
            for (i = 0; i < 2; i++) {
881
1608
                flags(update_mode_delta[i], 1, i);
882
1608
                if (current->update_mode_delta[i])
883
                    sus(1 + 6, loop_filter_mode_deltas[i], 1, i);
884
            }
885
        }
886
    }
887
888
3010
    return 0;
889
}
890
891
3010
static int FUNC(cdef_params)(CodedBitstreamContext *ctx, RWContext *rw,
892
                             AV1RawFrameHeader *current)
893
{
894
3010
    CodedBitstreamAV1Context  *priv = ctx->priv_data;
895
3010
    const AV1RawSequenceHeader *seq = priv->sequence_header;
896
    int i, err;
897
898

3010
    if (priv->coded_lossless || current->allow_intrabc ||
899
3010
        !seq->enable_cdef) {
900
342
        infer(cdef_damping_minus_3, 0);
901
342
        infer(cdef_bits, 0);
902
342
        infer(cdef_y_pri_strength[0],  0);
903
342
        infer(cdef_y_sec_strength[0],  0);
904
342
        infer(cdef_uv_pri_strength[0], 0);
905
342
        infer(cdef_uv_sec_strength[0], 0);
906
907
342
        return 0;
908
    }
909
910
2668
    fb(2, cdef_damping_minus_3);
911
2668
    fb(2, cdef_bits);
912
913
13280
    for (i = 0; i < (1 << current->cdef_bits); i++) {
914
10612
        fbs(4, cdef_y_pri_strength[i], 1, i);
915
10612
        fbs(2, cdef_y_sec_strength[i], 1, i);
916
917
10612
        if (priv->num_planes > 1) {
918
10612
            fbs(4, cdef_uv_pri_strength[i], 1, i);
919
10612
            fbs(2, cdef_uv_sec_strength[i], 1, i);
920
        }
921
    }
922
923
2668
    return 0;
924
}
925
926
3010
static int FUNC(lr_params)(CodedBitstreamContext *ctx, RWContext *rw,
927
                           AV1RawFrameHeader *current)
928
{
929
3010
    CodedBitstreamAV1Context  *priv = ctx->priv_data;
930
3010
    const AV1RawSequenceHeader *seq = priv->sequence_header;
931
    int uses_lr,  uses_chroma_lr;
932
    int i, err;
933
934

3010
    if (priv->all_lossless || current->allow_intrabc ||
935
3010
        !seq->enable_restoration) {
936
256
        return 0;
937
    }
938
939
2754
    uses_lr = uses_chroma_lr = 0;
940
11016
    for (i = 0; i < priv->num_planes; i++) {
941
8262
        fbs(2, lr_type[i], 1, i);
942
943
8262
        if (current->lr_type[i] != AV1_RESTORE_NONE) {
944
2212
            uses_lr = 1;
945
2212
            if (i > 0)
946
848
                uses_chroma_lr = 1;
947
        }
948
    }
949
950
2754
    if (uses_lr) {
951
1518
        if (seq->use_128x128_superblock)
952
974
            increment(lr_unit_shift, 1, 2);
953
        else
954
544
            increment(lr_unit_shift, 0, 2);
955
956
1518
        if(seq->color_config.subsampling_x &&
957

1518
           seq->color_config.subsampling_y && uses_chroma_lr) {
958
554
            fb(1, lr_uv_shift);
959
        } else {
960
964
            infer(lr_uv_shift, 0);
961
        }
962
    }
963
964
2754
    return 0;
965
}
966
967
3010
static int FUNC(read_tx_mode)(CodedBitstreamContext *ctx, RWContext *rw,
968
                              AV1RawFrameHeader *current)
969
{
970
3010
    CodedBitstreamAV1Context *priv = ctx->priv_data;
971
    int err;
972
973
3010
    if (priv->coded_lossless)
974
        infer(tx_mode, 0);
975
    else
976
3010
        increment(tx_mode, 1, 2);
977
978
3010
    return 0;
979
}
980
981
3010
static int FUNC(frame_reference_mode)(CodedBitstreamContext *ctx, RWContext *rw,
982
                                      AV1RawFrameHeader *current)
983
{
984
    int err;
985
986
3010
    if (current->frame_type == AV1_FRAME_INTRA_ONLY ||
987
3010
        current->frame_type == AV1_FRAME_KEY)
988
436
        infer(reference_select, 0);
989
    else
990
2574
        flag(reference_select);
991
992
3010
    return 0;
993
}
994
995
3010
static int FUNC(skip_mode_params)(CodedBitstreamContext *ctx, RWContext *rw,
996
                                  AV1RawFrameHeader *current)
997
{
998
3010
    CodedBitstreamAV1Context  *priv = ctx->priv_data;
999
3010
    const AV1RawSequenceHeader *seq = priv->sequence_header;
1000
    int skip_mode_allowed;
1001
    int err;
1002
1003
3010
    if (current->frame_type == AV1_FRAME_KEY ||
1004
2574
        current->frame_type == AV1_FRAME_INTRA_ONLY ||
1005

2574
        !current->reference_select || !seq->enable_order_hint) {
1006
1596
        skip_mode_allowed = 0;
1007
    } else {
1008
        int forward_idx,  backward_idx;
1009
        int forward_hint, backward_hint;
1010
        int ref_hint, dist, i;
1011
1012
1414
        forward_idx  = -1;
1013
1414
        backward_idx = -1;
1014
11312
        for (i = 0; i < AV1_REFS_PER_FRAME; i++) {
1015
9898
            ref_hint = priv->ref[current->ref_frame_idx[i]].order_hint;
1016
9898
            dist = cbs_av1_get_relative_dist(seq, ref_hint,
1017
9898
                                             priv->order_hint);
1018
9898
            if (dist < 0) {
1019

15278
                if (forward_idx < 0 ||
1020
6932
                    cbs_av1_get_relative_dist(seq, ref_hint,
1021
                                              forward_hint) > 0) {
1022
1602
                    forward_idx  = i;
1023
1602
                    forward_hint = ref_hint;
1024
                }
1025
1552
            } else if (dist > 0) {
1026

2168
                if (backward_idx < 0 ||
1027
672
                    cbs_av1_get_relative_dist(seq, ref_hint,
1028
                                              backward_hint) < 0) {
1029
824
                    backward_idx  = i;
1030
824
                    backward_hint = ref_hint;
1031
                }
1032
            }
1033
        }
1034
1035
1414
        if (forward_idx < 0) {
1036
            skip_mode_allowed = 0;
1037
1414
        } else if (backward_idx >= 0) {
1038
824
            skip_mode_allowed = 1;
1039
            // Frames for skip mode are forward_idx and backward_idx.
1040
        } else {
1041
            int second_forward_idx;
1042
            int second_forward_hint;
1043
1044
590
            second_forward_idx = -1;
1045
4720
            for (i = 0; i < AV1_REFS_PER_FRAME; i++) {
1046
4130
                ref_hint = priv->ref[current->ref_frame_idx[i]].order_hint;
1047
4130
                if (cbs_av1_get_relative_dist(seq, ref_hint,
1048
                                              forward_hint) < 0) {
1049

6044
                    if (second_forward_idx < 0 ||
1050
2740
                        cbs_av1_get_relative_dist(seq, ref_hint,
1051
                                                  second_forward_hint) > 0) {
1052
596
                        second_forward_idx  = i;
1053
596
                        second_forward_hint = ref_hint;
1054
                    }
1055
                }
1056
            }
1057
1058
590
            if (second_forward_idx < 0) {
1059
26
                skip_mode_allowed = 0;
1060
            } else {
1061
564
                skip_mode_allowed = 1;
1062
                // Frames for skip mode are forward_idx and second_forward_idx.
1063
            }
1064
        }
1065
    }
1066
1067
3010
    if (skip_mode_allowed)
1068
1388
        flag(skip_mode_present);
1069
    else
1070
1622
        infer(skip_mode_present, 0);
1071
1072
3010
    return 0;
1073
}
1074
1075
736
static int FUNC(global_motion_param)(CodedBitstreamContext *ctx, RWContext *rw,
1076
                                     AV1RawFrameHeader *current,
1077
                                     int type, int ref, int idx)
1078
{
1079
    uint32_t abs_bits, prec_bits, num_syms;
1080
    int err;
1081
1082
736
    if (idx < 2) {
1083
368
        if (type == AV1_WARP_MODEL_TRANSLATION) {
1084
            abs_bits  = AV1_GM_ABS_TRANS_ONLY_BITS  - !current->allow_high_precision_mv;
1085
            prec_bits = AV1_GM_TRANS_ONLY_PREC_BITS - !current->allow_high_precision_mv;
1086
        } else {
1087
368
            abs_bits  = AV1_GM_ABS_TRANS_BITS;
1088
368
            prec_bits = AV1_GM_TRANS_PREC_BITS;
1089
        }
1090
    } else {
1091
368
        abs_bits  = AV1_GM_ABS_ALPHA_BITS;
1092
368
        prec_bits = AV1_GM_ALPHA_PREC_BITS;
1093
    }
1094
1095
736
    num_syms = 2 * (1 << abs_bits) + 1;
1096
736
    subexp(gm_params[ref][idx], num_syms, 2, ref, idx);
1097
1098
    // Actual gm_params value is not reconstructed here.
1099
    (void)prec_bits;
1100
1101
736
    return 0;
1102
}
1103
1104
3010
static int FUNC(global_motion_params)(CodedBitstreamContext *ctx, RWContext *rw,
1105
                                      AV1RawFrameHeader *current)
1106
{
1107
    int ref, type;
1108
    int err;
1109
1110
3010
    if (current->frame_type == AV1_FRAME_KEY ||
1111
2574
        current->frame_type == AV1_FRAME_INTRA_ONLY)
1112
436
        return 0;
1113
1114
20592
    for (ref = AV1_REF_FRAME_LAST; ref <= AV1_REF_FRAME_ALTREF; ref++) {
1115
18018
        flags(is_global[ref], 1, ref);
1116
18018
        if (current->is_global[ref]) {
1117
184
            flags(is_rot_zoom[ref], 1, ref);
1118
184
            if (current->is_rot_zoom[ref]) {
1119
184
                type = AV1_WARP_MODEL_ROTZOOM;
1120
            } else {
1121
                flags(is_translation[ref], 1, ref);
1122
                type = current->is_translation[ref] ? AV1_WARP_MODEL_TRANSLATION
1123
                                                    : AV1_WARP_MODEL_AFFINE;
1124
            }
1125
        } else {
1126
17834
            type = AV1_WARP_MODEL_IDENTITY;
1127
        }
1128
1129
18018
        if (type >= AV1_WARP_MODEL_ROTZOOM) {
1130
184
            CHECK(FUNC(global_motion_param)(ctx, rw, current, type, ref, 2));
1131
184
            CHECK(FUNC(global_motion_param)(ctx, rw, current, type, ref, 3));
1132
184
            if (type == AV1_WARP_MODEL_AFFINE) {
1133
                CHECK(FUNC(global_motion_param)(ctx, rw, current, type, ref, 4));
1134
                CHECK(FUNC(global_motion_param)(ctx, rw, current, type, ref, 5));
1135
            } else {
1136
                // gm_params[ref][4] = -gm_params[ref][3]
1137
                // gm_params[ref][5] =  gm_params[ref][2]
1138
            }
1139
        }
1140
18018
        if (type >= AV1_WARP_MODEL_TRANSLATION) {
1141
184
            CHECK(FUNC(global_motion_param)(ctx, rw, current, type, ref, 0));
1142
184
            CHECK(FUNC(global_motion_param)(ctx, rw, current, type, ref, 1));
1143
        }
1144
    }
1145
1146
2574
    return 0;
1147
}
1148
1149
3010
static int FUNC(film_grain_params)(CodedBitstreamContext *ctx, RWContext *rw,
1150
                                   AV1RawFrameHeader *current)
1151
{
1152
3010
    CodedBitstreamAV1Context  *priv = ctx->priv_data;
1153
3010
    const AV1RawSequenceHeader *seq = priv->sequence_header;
1154
    int num_pos_luma, num_pos_chroma;
1155
    int i, err;
1156
1157
3010
    if (!seq->film_grain_params_present ||
1158

176
        (!current->show_frame && !current->showable_frame))
1159
2850
        return 0;
1160
1161
160
    flag(apply_grain);
1162
1163
160
    if (!current->apply_grain)
1164
        return 0;
1165
1166
160
    fb(16, grain_seed);
1167
1168
160
    if (current->frame_type == AV1_FRAME_INTER)
1169
144
        flag(update_grain);
1170
    else
1171
16
        infer(update_grain, 1);
1172
1173
160
    if (!current->update_grain) {
1174
        fb(3, film_grain_params_ref_idx);
1175
        return 0;
1176
    }
1177
1178
160
    fc(4, num_y_points, 0, 14);
1179
1264
    for (i = 0; i < current->num_y_points; i++) {
1180

1104
        fcs(8, point_y_value[i],
1181
            i ? current->point_y_value[i - 1] + 1 : 0,
1182
            MAX_UINT_BITS(8) - (current->num_y_points - i - 1),
1183
            1, i);
1184
1104
        fbs(8, point_y_scaling[i], 1, i);
1185
    }
1186
1187
160
    if (seq->color_config.mono_chrome)
1188
        infer(chroma_scaling_from_luma, 0);
1189
    else
1190
160
        flag(chroma_scaling_from_luma);
1191
1192
160
    if (seq->color_config.mono_chrome ||
1193
160
        current->chroma_scaling_from_luma ||
1194
160
        (seq->color_config.subsampling_x == 1 &&
1195
160
         seq->color_config.subsampling_y == 1 &&
1196
160
         current->num_y_points == 0)) {
1197
        infer(num_cb_points, 0);
1198
        infer(num_cr_points, 0);
1199
    } else {
1200
160
        fc(4, num_cb_points, 0, 10);
1201
784
        for (i = 0; i < current->num_cb_points; i++) {
1202

624
            fcs(8, point_cb_value[i],
1203
                i ? current->point_cb_value[i - 1] + 1 : 0,
1204
                MAX_UINT_BITS(8) - (current->num_cb_points - i - 1),
1205
                1, i);
1206
624
            fbs(8, point_cb_scaling[i], 1, i);
1207
        }
1208
160
        fc(4, num_cr_points, 0, 10);
1209
800
        for (i = 0; i < current->num_cr_points; i++) {
1210

640
            fcs(8, point_cr_value[i],
1211
                i ? current->point_cr_value[i - 1] + 1 : 0,
1212
                MAX_UINT_BITS(8) - (current->num_cr_points - i - 1),
1213
                1, i);
1214
640
            fbs(8, point_cr_scaling[i], 1, i);
1215
        }
1216
    }
1217
1218
160
    fb(2, grain_scaling_minus_8);
1219
160
    fb(2, ar_coeff_lag);
1220
160
    num_pos_luma = 2 * current->ar_coeff_lag * (current->ar_coeff_lag + 1);
1221
160
    if (current->num_y_points) {
1222
160
        num_pos_chroma = num_pos_luma + 1;
1223
4000
        for (i = 0; i < num_pos_luma; i++)
1224
3840
            fbs(8, ar_coeffs_y_plus_128[i], 1, i);
1225
    } else {
1226
        num_pos_chroma = num_pos_luma;
1227
    }
1228

160
    if (current->chroma_scaling_from_luma || current->num_cb_points) {
1229
4160
        for (i = 0; i < num_pos_chroma; i++)
1230
4000
            fbs(8, ar_coeffs_cb_plus_128[i], 1, i);
1231
    }
1232

160
    if (current->chroma_scaling_from_luma || current->num_cr_points) {
1233
4160
        for (i = 0; i < num_pos_chroma; i++)
1234
4000
            fbs(8, ar_coeffs_cr_plus_128[i], 1, i);
1235
    }
1236
160
    fb(2, ar_coeff_shift_minus_6);
1237
160
    fb(2, grain_scale_shift);
1238
160
    if (current->num_cb_points) {
1239
160
        fb(8, cb_mult);
1240
160
        fb(8, cb_luma_mult);
1241
160
        fb(9, cb_offset);
1242
    }
1243
160
    if (current->num_cr_points) {
1244
160
        fb(8, cr_mult);
1245
160
        fb(8, cr_luma_mult);
1246
160
        fb(9, cr_offset);
1247
    }
1248
1249
160
    flag(overlap_flag);
1250
160
    flag(clip_to_restricted_range);
1251
1252
160
    return 0;
1253
}
1254
1255
3402
static int FUNC(uncompressed_header)(CodedBitstreamContext *ctx, RWContext *rw,
1256
                                     AV1RawFrameHeader *current)
1257
{
1258
3402
    CodedBitstreamAV1Context *priv = ctx->priv_data;
1259
    const AV1RawSequenceHeader *seq;
1260
    int id_len, diff_len, all_frames, frame_is_intra, order_hint_bits;
1261
    int i, err;
1262
1263
3402
    if (!priv->sequence_header) {
1264
        av_log(ctx->log_ctx, AV_LOG_ERROR, "No sequence header available: "
1265
               "unable to decode frame header.\n");
1266
        return AVERROR_INVALIDDATA;
1267
    }
1268
3402
    seq = priv->sequence_header;
1269
1270
3402
    id_len = seq->additional_frame_id_length_minus_1 +
1271
3402
             seq->delta_frame_id_length_minus_2 + 3;
1272
3402
    all_frames = (1 << AV1_NUM_REF_FRAMES) - 1;
1273
1274
3402
    if (seq->reduced_still_picture_header) {
1275
        infer(show_existing_frame, 0);
1276
        infer(frame_type,     AV1_FRAME_KEY);
1277
        infer(show_frame,     1);
1278
        infer(showable_frame, 0);
1279
        frame_is_intra = 1;
1280
1281
    } else {
1282
3402
        flag(show_existing_frame);
1283
1284
3402
        if (current->show_existing_frame) {
1285
            AV1ReferenceFrameState *ref;
1286
1287
392
            fb(3, frame_to_show_map_idx);
1288
392
            ref = &priv->ref[current->frame_to_show_map_idx];
1289
1290
392
            if (!ref->valid) {
1291
                av_log(ctx->log_ctx, AV_LOG_ERROR, "Missing reference frame needed for "
1292
                       "show_existing_frame (frame_to_show_map_idx = %d).\n",
1293
                       current->frame_to_show_map_idx);
1294
                return AVERROR_INVALIDDATA;
1295
            }
1296
1297
392
            if (seq->decoder_model_info_present_flag &&
1298
56
                !seq->timing_info.equal_picture_interval) {
1299
56
                fb(seq->decoder_model_info.frame_presentation_time_length_minus_1 + 1,
1300
                   frame_presentation_time);
1301
            }
1302
1303
392
            if (seq->frame_id_numbers_present_flag)
1304
                fb(id_len, display_frame_id);
1305
1306
392
            infer(frame_type, ref->frame_type);
1307
392
            if (current->frame_type == AV1_FRAME_KEY) {
1308
                infer(refresh_frame_flags, all_frames);
1309
1310
                // Section 7.21
1311
                infer(current_frame_id, ref->frame_id);
1312
                priv->upscaled_width  = ref->upscaled_width;
1313
                priv->frame_width     = ref->frame_width;
1314
                priv->frame_height    = ref->frame_height;
1315
                priv->render_width    = ref->render_width;
1316
                priv->render_height   = ref->render_height;
1317
                priv->bit_depth       = ref->bit_depth;
1318
                priv->order_hint      = ref->order_hint;
1319
            } else
1320
392
                infer(refresh_frame_flags, 0);
1321
1322
392
            infer(frame_width_minus_1,   ref->upscaled_width - 1);
1323
392
            infer(frame_height_minus_1,  ref->frame_height - 1);
1324
392
            infer(render_width_minus_1,  ref->render_width - 1);
1325
392
            infer(render_height_minus_1, ref->render_height - 1);
1326
1327
            // Section 7.20
1328
392
            goto update_refs;
1329
        }
1330
1331
3010
        fb(2, frame_type);
1332
6020
        frame_is_intra = (current->frame_type == AV1_FRAME_INTRA_ONLY ||
1333
3010
                          current->frame_type == AV1_FRAME_KEY);
1334
1335
3010
        flag(show_frame);
1336
3010
        if (current->show_frame &&
1337
2450
            seq->decoder_model_info_present_flag &&
1338
648
            !seq->timing_info.equal_picture_interval) {
1339
648
            fb(seq->decoder_model_info.frame_presentation_time_length_minus_1 + 1,
1340
               frame_presentation_time);
1341
        }
1342
3010
        if (current->show_frame)
1343
2450
            infer(showable_frame, current->frame_type != AV1_FRAME_KEY);
1344
        else
1345
560
            flag(showable_frame);
1346
1347
3010
        if (current->frame_type == AV1_FRAME_SWITCH ||
1348

3002
            (current->frame_type == AV1_FRAME_KEY && current->show_frame))
1349
444
            infer(error_resilient_mode, 1);
1350
        else
1351
2566
            flag(error_resilient_mode);
1352
    }
1353
1354

3010
    if (current->frame_type == AV1_FRAME_KEY && current->show_frame) {
1355
3924
        for (i = 0; i < AV1_NUM_REF_FRAMES; i++) {
1356
3488
            priv->ref[i].valid = 0;
1357
3488
            priv->ref[i].order_hint = 0;
1358
        }
1359
    }
1360
1361
3010
    flag(disable_cdf_update);
1362
1363
3010
    if (seq->seq_force_screen_content_tools ==
1364
        AV1_SELECT_SCREEN_CONTENT_TOOLS) {
1365
3010
        flag(allow_screen_content_tools);
1366
    } else {
1367
        infer(allow_screen_content_tools,
1368
              seq->seq_force_screen_content_tools);
1369
    }
1370
3010
    if (current->allow_screen_content_tools) {
1371
86
        if (seq->seq_force_integer_mv == AV1_SELECT_INTEGER_MV)
1372
86
            flag(force_integer_mv);
1373
        else
1374
            infer(force_integer_mv, seq->seq_force_integer_mv);
1375
    } else {
1376
2924
        infer(force_integer_mv, 0);
1377
    }
1378
1379
3010
    if (seq->frame_id_numbers_present_flag) {
1380
        fb(id_len, current_frame_id);
1381
1382
        diff_len = seq->delta_frame_id_length_minus_2 + 2;
1383
        for (i = 0; i < AV1_NUM_REF_FRAMES; i++) {
1384
            if (current->current_frame_id > (1 << diff_len)) {
1385
                if (priv->ref[i].frame_id > current->current_frame_id ||
1386
                    priv->ref[i].frame_id < (current->current_frame_id -
1387
                                             (1 << diff_len)))
1388
                    priv->ref[i].valid = 0;
1389
            } else {
1390
                if (priv->ref[i].frame_id > current->current_frame_id &&
1391
                    priv->ref[i].frame_id < ((1 << id_len) +
1392
                                             current->current_frame_id -
1393
                                             (1 << diff_len)))
1394
                    priv->ref[i].valid = 0;
1395
            }
1396
        }
1397
    } else {
1398
3010
        infer(current_frame_id, 0);
1399
    }
1400
1401
3010
    if (current->frame_type == AV1_FRAME_SWITCH)
1402
8
        infer(frame_size_override_flag, 1);
1403
3002
    else if(seq->reduced_still_picture_header)
1404
        infer(frame_size_override_flag, 0);
1405
    else
1406
3002
        flag(frame_size_override_flag);
1407
1408
3010
    order_hint_bits =
1409
3010
        seq->enable_order_hint ? seq->order_hint_bits_minus_1 + 1 : 0;
1410
3010
    if (order_hint_bits > 0)
1411
3010
        fb(order_hint_bits, order_hint);
1412
    else
1413
        infer(order_hint, 0);
1414
3010
    priv->order_hint = current->order_hint;
1415
1416

3010
    if (frame_is_intra || current->error_resilient_mode)
1417
444
        infer(primary_ref_frame, AV1_PRIMARY_REF_NONE);
1418
    else
1419
2566
        fb(3, primary_ref_frame);
1420
1421
3010
    if (seq->decoder_model_info_present_flag) {
1422
776
        flag(buffer_removal_time_present_flag);
1423
776
        if (current->buffer_removal_time_present_flag) {
1424
1552
            for (i = 0; i <= seq->operating_points_cnt_minus_1; i++) {
1425
776
                if (seq->decoder_model_present_for_this_op[i]) {
1426
776
                    int op_pt_idc = seq->operating_point_idc[i];
1427
776
                    int in_temporal_layer = (op_pt_idc >>  priv->temporal_id    ) & 1;
1428
776
                    int in_spatial_layer  = (op_pt_idc >> (priv->spatial_id + 8)) & 1;
1429

776
                    if (seq->operating_point_idc[i] == 0 ||
1430
                        (in_temporal_layer && in_spatial_layer)) {
1431
776
                        fbs(seq->decoder_model_info.buffer_removal_time_length_minus_1 + 1,
1432
                            buffer_removal_time[i], 1, i);
1433
                    }
1434
                }
1435
            }
1436
        }
1437
    }
1438
1439
3010
    if (current->frame_type == AV1_FRAME_SWITCH ||
1440

3002
        (current->frame_type == AV1_FRAME_KEY && current->show_frame))
1441
444
        infer(refresh_frame_flags, all_frames);
1442
    else
1443
2566
        fb(8, refresh_frame_flags);
1444
1445

3010
    if (!frame_is_intra || current->refresh_frame_flags != all_frames) {
1446

2574
        if (current->error_resilient_mode && seq->enable_order_hint) {
1447
72
            for (i = 0; i < AV1_NUM_REF_FRAMES; i++) {
1448
64
                fbs(order_hint_bits, ref_order_hint[i], 1, i);
1449
64
                if (current->ref_order_hint[i] != priv->ref[i].order_hint)
1450
                    priv->ref[i].valid = 0;
1451
            }
1452
        }
1453
    }
1454
1455
3010
    if (current->frame_type == AV1_FRAME_KEY ||
1456
2574
        current->frame_type == AV1_FRAME_INTRA_ONLY) {
1457
436
        CHECK(FUNC(frame_size)(ctx, rw, current));
1458
436
        CHECK(FUNC(render_size)(ctx, rw, current));
1459
1460
436
        if (current->allow_screen_content_tools &&
1461
8
            priv->upscaled_width == priv->frame_width)
1462
8
            flag(allow_intrabc);
1463
        else
1464
428
            infer(allow_intrabc, 0);
1465
1466
    } else {
1467
2574
        if (!seq->enable_order_hint) {
1468
            infer(frame_refs_short_signaling, 0);
1469
        } else {
1470
2574
            flag(frame_refs_short_signaling);
1471
2574
            if (current->frame_refs_short_signaling) {
1472
112
                fb(3, last_frame_idx);
1473
112
                fb(3, golden_frame_idx);
1474
112
                CHECK(FUNC(set_frame_refs)(ctx, rw, current));
1475
            }
1476
        }
1477
1478
20592
        for (i = 0; i < AV1_REFS_PER_FRAME; i++) {
1479
18018
            if (!current->frame_refs_short_signaling)
1480
17234
                fbs(3, ref_frame_idx[i], 1, i);
1481
18018
            if (seq->frame_id_numbers_present_flag) {
1482
                fbs(seq->delta_frame_id_length_minus_2 + 2,
1483
                    delta_frame_id_minus1[i], 1, i);
1484
            }
1485
        }
1486
1487
2574
        if (current->frame_size_override_flag &&
1488
128
            !current->error_resilient_mode) {
1489
120
            CHECK(FUNC(frame_size_with_refs)(ctx, rw, current));
1490
        } else {
1491
2454
            CHECK(FUNC(frame_size)(ctx, rw, current));
1492
2454
            CHECK(FUNC(render_size)(ctx, rw, current));
1493
        }
1494
1495
2574
        if (current->force_integer_mv)
1496
            infer(allow_high_precision_mv, 0);
1497
        else
1498
2574
            flag(allow_high_precision_mv);
1499
1500
2574
        CHECK(FUNC(interpolation_filter)(ctx, rw, current));
1501
1502
2574
        flag(is_motion_mode_switchable);
1503
1504
2574
        if (current->error_resilient_mode ||
1505
2566
            !seq->enable_ref_frame_mvs)
1506
248
            infer(use_ref_frame_mvs, 0);
1507
        else
1508
2326
            flag(use_ref_frame_mvs);
1509
1510
2574
        infer(allow_intrabc, 0);
1511
    }
1512
1513
    if (!frame_is_intra) {
1514
        // Derive reference frame sign biases.
1515
    }
1516
1517

3010
    if (seq->reduced_still_picture_header || current->disable_cdf_update)
1518
        infer(disable_frame_end_update_cdf, 1);
1519
    else
1520
3010
        flag(disable_frame_end_update_cdf);
1521
1522
3010
    if (current->primary_ref_frame == AV1_PRIMARY_REF_NONE) {
1523
        // Init non-coeff CDFs.
1524
        // Setup past independence.
1525
    } else {
1526
        // Load CDF tables from previous frame.
1527
        // Load params from previous frame.
1528
    }
1529
1530
3010
    if (current->use_ref_frame_mvs) {
1531
        // Perform motion field estimation process.
1532
    }
1533
1534
3010
    CHECK(FUNC(tile_info)(ctx, rw, current));
1535
1536
3010
    CHECK(FUNC(quantization_params)(ctx, rw, current));
1537
1538
3010
    CHECK(FUNC(segmentation_params)(ctx, rw, current));
1539
1540
3010
    CHECK(FUNC(delta_q_params)(ctx, rw, current));
1541
1542
3010
    CHECK(FUNC(delta_lf_params)(ctx, rw, current));
1543
1544
    // Init coeff CDFs / load previous segments.
1545
1546
3010
    priv->coded_lossless = 1;
1547
27090
    for (i = 0; i < AV1_MAX_SEGMENTS; i++) {
1548
        int qindex;
1549
24080
        if (current->feature_enabled[i][AV1_SEG_LVL_ALT_Q]) {
1550
            qindex = (current->base_q_idx +
1551
                      current->feature_value[i][AV1_SEG_LVL_ALT_Q]);
1552
        } else {
1553
24080
            qindex = current->base_q_idx;
1554
        }
1555
24080
        qindex = av_clip_uintp2(qindex, 8);
1556
1557

24080
        if (qindex                || current->delta_q_y_dc ||
1558
            current->delta_q_u_ac || current->delta_q_u_dc ||
1559
            current->delta_q_v_ac || current->delta_q_v_dc) {
1560
24080
            priv->coded_lossless = 0;
1561
        }
1562
    }
1563
3010
    priv->all_lossless = priv->coded_lossless &&
1564
        priv->frame_width == priv->upscaled_width;
1565
1566
3010
    CHECK(FUNC(loop_filter_params)(ctx, rw, current));
1567
1568
3010
    CHECK(FUNC(cdef_params)(ctx, rw, current));
1569
1570
3010
    CHECK(FUNC(lr_params)(ctx, rw, current));
1571
1572
3010
    CHECK(FUNC(read_tx_mode)(ctx, rw, current));
1573
1574
3010
    CHECK(FUNC(frame_reference_mode)(ctx, rw, current));
1575
1576
3010
    CHECK(FUNC(skip_mode_params)(ctx, rw, current));
1577
1578

3010
    if (frame_is_intra || current->error_resilient_mode ||
1579
2566
        !seq->enable_warped_motion)
1580
444
        infer(allow_warped_motion, 0);
1581
    else
1582
2566
        flag(allow_warped_motion);
1583
1584
3010
    flag(reduced_tx_set);
1585
1586
3010
    CHECK(FUNC(global_motion_params)(ctx, rw, current));
1587
1588
3010
    CHECK(FUNC(film_grain_params)(ctx, rw, current));
1589
1590
3010
    av_log(ctx->log_ctx, AV_LOG_DEBUG, "Frame %d:  size %dx%d  "
1591
           "upscaled %d  render %dx%d  subsample %dx%d  "
1592
           "bitdepth %d  tiles %dx%d.\n", priv->order_hint,
1593
           priv->frame_width, priv->frame_height, priv->upscaled_width,
1594
           priv->render_width, priv->render_height,
1595
3010
           seq->color_config.subsampling_x + 1,
1596
3010
           seq->color_config.subsampling_y + 1, priv->bit_depth,
1597
           priv->tile_rows, priv->tile_cols);
1598
1599
3402
update_refs:
1600
30618
    for (i = 0; i < AV1_NUM_REF_FRAMES; i++) {
1601
27216
        if (current->refresh_frame_flags & (1 << i)) {
1602
6014
            priv->ref[i] = (AV1ReferenceFrameState) {
1603
                .valid          = 1,
1604
6014
                .frame_id       = current->current_frame_id,
1605
6014
                .upscaled_width = priv->upscaled_width,
1606
6014
                .frame_width    = priv->frame_width,
1607
6014
                .frame_height   = priv->frame_height,
1608
6014
                .render_width   = priv->render_width,
1609
6014
                .render_height  = priv->render_height,
1610
6014
                .frame_type     = current->frame_type,
1611
6014
                .subsampling_x  = seq->color_config.subsampling_x,
1612
6014
                .subsampling_y  = seq->color_config.subsampling_y,
1613
6014
                .bit_depth      = priv->bit_depth,
1614
6014
                .order_hint     = priv->order_hint,
1615
            };
1616
        }
1617
    }
1618
1619
3402
    return 0;
1620
}
1621
1622
3402
static int FUNC(frame_header_obu)(CodedBitstreamContext *ctx, RWContext *rw,
1623
                                  AV1RawFrameHeader *current, int redundant,
1624
                                  AVBufferRef *rw_buffer_ref)
1625
{
1626
3402
    CodedBitstreamAV1Context *priv = ctx->priv_data;
1627
    int start_pos, fh_bits, fh_bytes, err;
1628
    uint8_t *fh_start;
1629
1630
3402
    if (priv->seen_frame_header) {
1631
        if (!redundant) {
1632
            av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid repeated "
1633
                   "frame header OBU.\n");
1634
            return AVERROR_INVALIDDATA;
1635
        } else {
1636
            GetBitContext fh;
1637
            size_t i, b;
1638
            uint32_t val;
1639
1640
            HEADER("Redundant Frame Header");
1641
1642
            av_assert0(priv->frame_header_ref && priv->frame_header);
1643
1644
            init_get_bits(&fh, priv->frame_header,
1645
                          priv->frame_header_size);
1646
            for (i = 0; i < priv->frame_header_size; i += 8) {
1647
                b = FFMIN(priv->frame_header_size - i, 8);
1648
                val = get_bits(&fh, b);
1649
                xf(b, frame_header_copy[i],
1650
                   val, val, val, 1, i / 8);
1651
            }
1652
        }
1653
    } else {
1654
3402
        if (redundant)
1655
            HEADER("Redundant Frame Header (used as Frame Header)");
1656
        else
1657
3402
            HEADER("Frame Header");
1658
1659
#ifdef READ
1660
2560
        start_pos = get_bits_count(rw);
1661
#else
1662
842
        start_pos = put_bits_count(rw);
1663
#endif
1664
1665
3402
        CHECK(FUNC(uncompressed_header)(ctx, rw, current));
1666
1667
3402
        if (current->show_existing_frame) {
1668
392
            priv->seen_frame_header = 0;
1669
        } else {
1670
3010
            priv->seen_frame_header = 1;
1671
1672
3010
            av_buffer_unref(&priv->frame_header_ref);
1673
1674
#ifdef READ
1675
2262
            fh_bits  = get_bits_count(rw) - start_pos;
1676
2262
            fh_start = (uint8_t*)rw->buffer + start_pos / 8;
1677
#else
1678
            // Need to flush the bitwriter so that we can copy its output,
1679
            // but use a copy so we don't affect the caller's structure.
1680
            {
1681
748
                PutBitContext tmp = *rw;
1682
748
                flush_put_bits(&tmp);
1683
            }
1684
1685
748
            fh_bits  = put_bits_count(rw) - start_pos;
1686
748
            fh_start = rw->buf + start_pos / 8;
1687
#endif
1688
3010
            fh_bytes = (fh_bits + 7) / 8;
1689
1690
3010
            priv->frame_header_size = fh_bits;
1691
1692
3010
            if (rw_buffer_ref) {
1693
2262
                priv->frame_header_ref = av_buffer_ref(rw_buffer_ref);
1694
2262
                if (!priv->frame_header_ref)
1695
                    return AVERROR(ENOMEM);
1696
2262
                priv->frame_header = fh_start;
1697
            } else {
1698
748
                priv->frame_header_ref =
1699
748
                    av_buffer_alloc(fh_bytes + AV_INPUT_BUFFER_PADDING_SIZE);
1700
748
                if (!priv->frame_header_ref)
1701
                    return AVERROR(ENOMEM);
1702
748
                priv->frame_header = priv->frame_header_ref->data;
1703
748
                memcpy(priv->frame_header, fh_start, fh_bytes);
1704
            }
1705
        }
1706
    }
1707
1708
3402
    return 0;
1709
}
1710
1711
3522
static int FUNC(tile_group_obu)(CodedBitstreamContext *ctx, RWContext *rw,
1712
                                AV1RawTileGroup *current)
1713
{
1714
3522
    CodedBitstreamAV1Context *priv = ctx->priv_data;
1715
    int num_tiles, tile_bits;
1716
    int err;
1717
1718
3522
    HEADER("Tile Group");
1719
1720
3522
    num_tiles = priv->tile_cols * priv->tile_rows;
1721
3522
    if (num_tiles > 1)
1722
1126
        flag(tile_start_and_end_present_flag);
1723
    else
1724
2396
        infer(tile_start_and_end_present_flag, 0);
1725
1726

3522
    if (num_tiles == 1 || !current->tile_start_and_end_present_flag) {
1727
2690
        infer(tg_start, 0);
1728
2690
        infer(tg_end, num_tiles - 1);
1729
    } else {
1730
832
        tile_bits = cbs_av1_tile_log2(1, priv->tile_cols) +
1731
832
                    cbs_av1_tile_log2(1, priv->tile_rows);
1732
832
        fb(tile_bits, tg_start);
1733
832
        fb(tile_bits, tg_end);
1734
    }
1735
1736
3522
    CHECK(FUNC(byte_alignment)(ctx, rw));
1737
1738
    // Reset header for next frame.
1739
3522
    if (current->tg_end == num_tiles - 1)
1740
3010
        priv->seen_frame_header = 0;
1741
1742
    // Tile data follows.
1743
1744
3522
    return 0;
1745
}
1746
1747
2690
static int FUNC(frame_obu)(CodedBitstreamContext *ctx, RWContext *rw,
1748
                           AV1RawFrame *current,
1749
                           AVBufferRef *rw_buffer_ref)
1750
{
1751
    int err;
1752
1753
2690
    CHECK(FUNC(frame_header_obu)(ctx, rw, &current->header,
1754
                                 0, rw_buffer_ref));
1755
1756
2690
    CHECK(FUNC(byte_alignment)(ctx, rw));
1757
1758
2690
    CHECK(FUNC(tile_group_obu)(ctx, rw, &current->tile_group));
1759
1760
2690
    return 0;
1761
}
1762
1763
static int FUNC(tile_list_obu)(CodedBitstreamContext *ctx, RWContext *rw,
1764
                               AV1RawTileList *current)
1765
{
1766
    int err;
1767
1768
    fb(8, output_frame_width_in_tiles_minus_1);
1769
    fb(8, output_frame_height_in_tiles_minus_1);
1770
1771
    fb(16, tile_count_minus_1);
1772
1773
    // Tile data follows.
1774
1775
    return 0;
1776
}
1777
1778
32
static int FUNC(metadata_hdr_cll)(CodedBitstreamContext *ctx, RWContext *rw,
1779
                                  AV1RawMetadataHDRCLL *current)
1780
{
1781
    int err;
1782
1783
32
    fb(16, max_cll);
1784
32
    fb(16, max_fall);
1785
1786
32
    return 0;
1787
}
1788
1789
32
static int FUNC(metadata_hdr_mdcv)(CodedBitstreamContext *ctx, RWContext *rw,
1790
                                   AV1RawMetadataHDRMDCV *current)
1791
{
1792
    int err, i;
1793
1794
128
    for (i = 0; i < 3; i++) {
1795
96
        fbs(16, primary_chromaticity_x[i], 1, i);
1796
96
        fbs(16, primary_chromaticity_y[i], 1, i);
1797
    }
1798
1799
32
    fb(16, white_point_chromaticity_x);
1800
32
    fb(16, white_point_chromaticity_y);
1801
1802
32
    fc(32, luminance_max, 1, MAX_UINT_BITS(32));
1803
    // luminance_min must be lower than luminance_max. Convert luminance_max from
1804
    // 24.8 fixed point to 18.14 fixed point in order to compare them.
1805
32
    fc(32, luminance_min, 0, FFMIN(((uint64_t)current->luminance_max << 6) - 1,
1806
                                   MAX_UINT_BITS(32)));
1807
1808
32
    return 0;
1809
}
1810
1811
static int FUNC(scalability_structure)(CodedBitstreamContext *ctx, RWContext *rw,
1812
                                       AV1RawMetadataScalability *current)
1813
{
1814
    CodedBitstreamAV1Context *priv = ctx->priv_data;
1815
    const AV1RawSequenceHeader *seq;
1816
    int err, i, j;
1817
1818
    if (!priv->sequence_header) {
1819
        av_log(ctx->log_ctx, AV_LOG_ERROR, "No sequence header available: "
1820
               "unable to parse scalability metadata.\n");
1821
        return AVERROR_INVALIDDATA;
1822
    }
1823
    seq = priv->sequence_header;
1824
1825
    fb(2, spatial_layers_cnt_minus_1);
1826
    flag(spatial_layer_dimensions_present_flag);
1827
    flag(spatial_layer_description_present_flag);
1828
    flag(temporal_group_description_present_flag);
1829
    fc(3, scalability_structure_reserved_3bits, 0, 0);
1830
    if (current->spatial_layer_dimensions_present_flag) {
1831
        for (i = 0; i <= current->spatial_layers_cnt_minus_1; i++) {
1832
            fcs(16, spatial_layer_max_width[i],
1833
                0, seq->max_frame_width_minus_1 + 1, 1, i);
1834
            fcs(16, spatial_layer_max_height[i],
1835
                0, seq->max_frame_height_minus_1 + 1, 1, i);
1836
        }
1837
    }
1838
    if (current->spatial_layer_description_present_flag) {
1839
        for (i = 0; i <= current->spatial_layers_cnt_minus_1; i++)
1840
            fbs(8, spatial_layer_ref_id[i], 1, i);
1841
    }
1842
    if (current->temporal_group_description_present_flag) {
1843
        fb(8, temporal_group_size);
1844
        for (i = 0; i < current->temporal_group_size; i++) {
1845
            fbs(3, temporal_group_temporal_id[i], 1, i);
1846
            flags(temporal_group_temporal_switching_up_point_flag[i], 1, i);
1847
            flags(temporal_group_spatial_switching_up_point_flag[i], 1, i);
1848
            fbs(3, temporal_group_ref_cnt[i], 1, i);
1849
            for (j = 0; j < current->temporal_group_ref_cnt[i]; j++) {
1850
                fbs(8, temporal_group_ref_pic_diff[i][j], 2, i, j);
1851
            }
1852
        }
1853
    }
1854
1855
    return 0;
1856
}
1857
1858
static int FUNC(metadata_scalability)(CodedBitstreamContext *ctx, RWContext *rw,
1859
                                      AV1RawMetadataScalability *current)
1860
{
1861
    int err;
1862
1863
    fb(8, scalability_mode_idc);
1864
1865
    if (current->scalability_mode_idc == AV1_SCALABILITY_SS)
1866
        CHECK(FUNC(scalability_structure)(ctx, rw, current));
1867
1868
    return 0;
1869
}
1870
1871
static int FUNC(metadata_itut_t35)(CodedBitstreamContext *ctx, RWContext *rw,
1872
                                   AV1RawMetadataITUTT35 *current)
1873
{
1874
    int err;
1875
    size_t i;
1876
1877
    fb(8, itu_t_t35_country_code);
1878
    if (current->itu_t_t35_country_code == 0xff)
1879
        fb(8, itu_t_t35_country_code_extension_byte);
1880
1881
#ifdef READ
1882
    // The payload runs up to the start of the trailing bits, but there might
1883
    // be arbitrarily many trailing zeroes so we need to read through twice.
1884
    current->payload_size = cbs_av1_get_payload_bytes_left(rw);
1885
1886
    current->payload_ref = av_buffer_alloc(current->payload_size);
1887
    if (!current->payload_ref)
1888
        return AVERROR(ENOMEM);
1889
    current->payload = current->payload_ref->data;
1890
#endif
1891
1892
    for (i = 0; i < current->payload_size; i++)
1893
        xf(8, itu_t_t35_payload_bytes[i], current->payload[i],
1894
           0x00, 0xff, 1, i);
1895
1896
    return 0;
1897
}
1898
1899
static int FUNC(metadata_timecode)(CodedBitstreamContext *ctx, RWContext *rw,
1900
                                   AV1RawMetadataTimecode *current)
1901
{
1902
    int err;
1903
1904
    fb(5, counting_type);
1905
    flag(full_timestamp_flag);
1906
    flag(discontinuity_flag);
1907
    flag(cnt_dropped_flag);
1908
    fb(9, n_frames);
1909
1910
    if (current->full_timestamp_flag) {
1911
        fc(6, seconds_value, 0, 59);
1912
        fc(6, minutes_value, 0, 59);
1913
        fc(5, hours_value,   0, 23);
1914
    } else {
1915
        flag(seconds_flag);
1916
        if (current->seconds_flag) {
1917
            fc(6, seconds_value, 0, 59);
1918
            flag(minutes_flag);
1919
            if (current->minutes_flag) {
1920
                fc(6, minutes_value, 0, 59);
1921
                flag(hours_flag);
1922
                if (current->hours_flag)
1923
                    fc(5, hours_value, 0, 23);
1924
            }
1925
        }
1926
    }
1927
1928
    fb(5, time_offset_length);
1929
    if (current->time_offset_length > 0)
1930
        fb(current->time_offset_length, time_offset_value);
1931
    else
1932
        infer(time_offset_length, 0);
1933
1934
    return 0;
1935
}
1936
1937
64
static int FUNC(metadata_obu)(CodedBitstreamContext *ctx, RWContext *rw,
1938
                              AV1RawMetadata *current)
1939
{
1940
    int err;
1941
1942
64
    leb128(metadata_type);
1943
1944

64
    switch (current->metadata_type) {
1945
32
    case AV1_METADATA_TYPE_HDR_CLL:
1946
32
        CHECK(FUNC(metadata_hdr_cll)(ctx, rw, &current->metadata.hdr_cll));
1947
32
        break;
1948
32
    case AV1_METADATA_TYPE_HDR_MDCV:
1949
32
        CHECK(FUNC(metadata_hdr_mdcv)(ctx, rw, &current->metadata.hdr_mdcv));
1950
32
        break;
1951
    case AV1_METADATA_TYPE_SCALABILITY:
1952
        CHECK(FUNC(metadata_scalability)(ctx, rw, &current->metadata.scalability));
1953
        break;
1954
    case AV1_METADATA_TYPE_ITUT_T35:
1955
        CHECK(FUNC(metadata_itut_t35)(ctx, rw, &current->metadata.itut_t35));
1956
        break;
1957
    case AV1_METADATA_TYPE_TIMECODE:
1958
        CHECK(FUNC(metadata_timecode)(ctx, rw, &current->metadata.timecode));
1959
        break;
1960
    default:
1961
        // Unknown metadata type.
1962
        return AVERROR_PATCHWELCOME;
1963
    }
1964
1965
64
    return 0;
1966
}
1967
1968
static int FUNC(padding_obu)(CodedBitstreamContext *ctx, RWContext *rw,
1969
                             AV1RawPadding *current)
1970
{
1971
    int i, err;
1972
1973
    HEADER("Padding");
1974
1975
#ifdef READ
1976
    // The payload runs up to the start of the trailing bits, but there might
1977
    // be arbitrarily many trailing zeroes so we need to read through twice.
1978
    current->payload_size = cbs_av1_get_payload_bytes_left(rw);
1979
1980
    current->payload_ref = av_buffer_alloc(current->payload_size);
1981
    if (!current->payload_ref)
1982
        return AVERROR(ENOMEM);
1983
    current->payload = current->payload_ref->data;
1984
#endif
1985
1986
    for (i = 0; i < current->payload_size; i++)
1987
        xf(8, obu_padding_byte[i], current->payload[i], 0x00, 0xff, 1, i);
1988
1989
    return 0;
1990
}