2 * This file is part of FFmpeg.
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.
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.
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
19 static int FUNC(obu_header)(CodedBitstreamContext *ctx, RWContext *rw,
20 AV1RawOBUHeader *current)
22 CodedBitstreamAV1Context *priv = ctx->priv_data;
27 fc(1, obu_forbidden_bit, 0, 0);
29 fc(4, obu_type, 0, AV1_OBU_PADDING);
30 flag(obu_extension_flag);
31 flag(obu_has_size_field);
33 fc(1, obu_reserved_1bit, 0, 0);
35 if (current->obu_extension_flag) {
38 fc(3, extension_header_reserved_3bits, 0, 0);
40 infer(temporal_id, 0);
44 priv->temporal_id = current->temporal_id;
45 priv->spatial_id = current->spatial_id;
50 static int FUNC(trailing_bits)(CodedBitstreamContext *ctx, RWContext *rw, int nb_bits)
54 av_assert0(nb_bits > 0);
56 fixed(1, trailing_one_bit, 1);
60 fixed(1, trailing_zero_bit, 0);
67 static int FUNC(byte_alignment)(CodedBitstreamContext *ctx, RWContext *rw)
71 while (byte_alignment(rw) != 0)
72 fixed(1, zero_bit, 0);
77 static int FUNC(color_config)(CodedBitstreamContext *ctx, RWContext *rw,
78 AV1RawColorConfig *current, int seq_profile)
80 CodedBitstreamAV1Context *priv = ctx->priv_data;
85 if (seq_profile == FF_PROFILE_AV1_PROFESSIONAL &&
86 current->high_bitdepth) {
88 priv->bit_depth = current->twelve_bit ? 12 : 10;
90 priv->bit_depth = current->high_bitdepth ? 10 : 8;
93 if (seq_profile == FF_PROFILE_AV1_HIGH)
94 infer(mono_chrome, 0);
97 priv->num_planes = current->mono_chrome ? 1 : 3;
99 flag(color_description_present_flag);
100 if (current->color_description_present_flag) {
101 fb(8, color_primaries);
102 fb(8, transfer_characteristics);
103 fb(8, matrix_coefficients);
105 infer(color_primaries, AVCOL_PRI_UNSPECIFIED);
106 infer(transfer_characteristics, AVCOL_TRC_UNSPECIFIED);
107 infer(matrix_coefficients, AVCOL_SPC_UNSPECIFIED);
110 if (current->mono_chrome) {
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);
118 } 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);
129 if (seq_profile == FF_PROFILE_AV1_MAIN) {
130 infer(subsampling_x, 1);
131 infer(subsampling_y, 1);
132 } else if (seq_profile == FF_PROFILE_AV1_HIGH) {
133 infer(subsampling_x, 0);
134 infer(subsampling_y, 0);
136 if (priv->bit_depth == 12) {
137 fb(1, subsampling_x);
138 if (current->subsampling_x)
139 fb(1, subsampling_y);
141 infer(subsampling_y, 0);
143 infer(subsampling_x, 1);
144 infer(subsampling_y, 0);
147 if (current->subsampling_x && current->subsampling_y) {
148 fc(2, chroma_sample_position, AV1_CSP_UNKNOWN,
152 flag(separate_uv_delta_q);
158 static int FUNC(timing_info)(CodedBitstreamContext *ctx, RWContext *rw,
159 AV1RawTimingInfo *current)
163 fc(32, num_units_in_display_tick, 1, MAX_UINT_BITS(32));
164 fc(32, time_scale, 1, MAX_UINT_BITS(32));
166 flag(equal_picture_interval);
167 if (current->equal_picture_interval)
168 uvlc(num_ticks_per_picture_minus_1, 0, MAX_UINT_BITS(32) - 1);
173 static int FUNC(decoder_model_info)(CodedBitstreamContext *ctx, RWContext *rw,
174 AV1RawDecoderModelInfo *current)
178 fb(5, buffer_delay_length_minus_1);
179 fb(32, num_units_in_decoding_tick);
180 fb(5, buffer_removal_time_length_minus_1);
181 fb(5, frame_presentation_time_length_minus_1);
186 static int FUNC(sequence_header_obu)(CodedBitstreamContext *ctx, RWContext *rw,
187 AV1RawSequenceHeader *current)
191 HEADER("Sequence Header");
193 fc(3, seq_profile, FF_PROFILE_AV1_MAIN,
194 FF_PROFILE_AV1_PROFESSIONAL);
196 flag(reduced_still_picture_header);
198 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);
205 fb(5, seq_level_idx[0]);
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);
212 flag(timing_info_present_flag);
213 if (current->timing_info_present_flag) {
214 CHECK(FUNC(timing_info)(ctx, rw, ¤t->timing_info));
216 flag(decoder_model_info_present_flag);
217 if (current->decoder_model_info_present_flag) {
218 CHECK(FUNC(decoder_model_info)
219 (ctx, rw, ¤t->decoder_model_info));
222 infer(decoder_model_info_present_flag, 0);
225 flag(initial_display_delay_present_flag);
227 fb(5, operating_points_cnt_minus_1);
228 for (i = 0; i <= current->operating_points_cnt_minus_1; i++) {
229 fbs(12, operating_point_idc[i], 1, i);
230 fbs(5, seq_level_idx[i], 1, i);
232 if (current->seq_level_idx[i] > 7)
233 flags(seq_tier[i], 1, i);
235 infer(seq_tier[i], 0);
237 if (current->decoder_model_info_present_flag) {
238 flags(decoder_model_present_for_this_op[i], 1, i);
239 if (current->decoder_model_present_for_this_op[i]) {
240 int n = current->decoder_model_info.buffer_delay_length_minus_1 + 1;
241 fbs(n, decoder_buffer_delay[i], 1, i);
242 fbs(n, encoder_buffer_delay[i], 1, i);
243 flags(low_delay_mode_flag[i], 1, i);
246 infer(decoder_model_present_for_this_op[i], 0);
249 if (current->initial_display_delay_present_flag) {
250 flags(initial_display_delay_present_for_this_op[i], 1, i);
251 if (current->initial_display_delay_present_for_this_op[i])
252 fbs(4, initial_display_delay_minus_1[i], 1, i);
257 fb(4, frame_width_bits_minus_1);
258 fb(4, frame_height_bits_minus_1);
260 fb(current->frame_width_bits_minus_1 + 1, max_frame_width_minus_1);
261 fb(current->frame_height_bits_minus_1 + 1, max_frame_height_minus_1);
263 if (current->reduced_still_picture_header)
264 infer(frame_id_numbers_present_flag, 0);
266 flag(frame_id_numbers_present_flag);
267 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);
272 flag(use_128x128_superblock);
273 flag(enable_filter_intra);
274 flag(enable_intra_edge_filter);
276 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);
285 infer(seq_force_screen_content_tools,
286 AV1_SELECT_SCREEN_CONTENT_TOOLS);
287 infer(seq_force_integer_mv,
288 AV1_SELECT_INTEGER_MV);
290 flag(enable_interintra_compound);
291 flag(enable_masked_compound);
292 flag(enable_warped_motion);
293 flag(enable_dual_filter);
295 flag(enable_order_hint);
296 if (current->enable_order_hint) {
297 flag(enable_jnt_comp);
298 flag(enable_ref_frame_mvs);
300 infer(enable_jnt_comp, 0);
301 infer(enable_ref_frame_mvs, 0);
304 flag(seq_choose_screen_content_tools);
305 if (current->seq_choose_screen_content_tools)
306 infer(seq_force_screen_content_tools,
307 AV1_SELECT_SCREEN_CONTENT_TOOLS);
309 fb(1, seq_force_screen_content_tools);
310 if (current->seq_force_screen_content_tools > 0) {
311 flag(seq_choose_integer_mv);
312 if (current->seq_choose_integer_mv)
313 infer(seq_force_integer_mv,
314 AV1_SELECT_INTEGER_MV);
316 fb(1, seq_force_integer_mv);
318 infer(seq_force_integer_mv, AV1_SELECT_INTEGER_MV);
321 if (current->enable_order_hint)
322 fb(3, order_hint_bits_minus_1);
325 flag(enable_superres);
327 flag(enable_restoration);
329 CHECK(FUNC(color_config)(ctx, rw, ¤t->color_config,
330 current->seq_profile));
332 flag(film_grain_params_present);
337 static int FUNC(temporal_delimiter_obu)(CodedBitstreamContext *ctx, RWContext *rw)
339 CodedBitstreamAV1Context *priv = ctx->priv_data;
341 HEADER("Temporal Delimiter");
343 priv->seen_frame_header = 0;
348 static int FUNC(set_frame_refs)(CodedBitstreamContext *ctx, RWContext *rw,
349 AV1RawFrameHeader *current)
351 CodedBitstreamAV1Context *priv = ctx->priv_data;
352 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
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;
362 for (i = 0; i < AV1_REFS_PER_FRAME; i++)
363 ref_frame_idx[i] = -1;
364 ref_frame_idx[AV1_REF_FRAME_LAST - AV1_REF_FRAME_LAST] = current->last_frame_idx;
365 ref_frame_idx[AV1_REF_FRAME_GOLDEN - AV1_REF_FRAME_LAST] = current->golden_frame_idx;
367 for (i = 0; i < AV1_NUM_REF_FRAMES; i++)
369 used_frame[current->last_frame_idx] = 1;
370 used_frame[current->golden_frame_idx] = 1;
372 cur_frame_hint = 1 << (seq->order_hint_bits_minus_1);
373 for (i = 0; i < AV1_NUM_REF_FRAMES; i++)
374 shifted_order_hints[i] = cur_frame_hint +
375 cbs_av1_get_relative_dist(seq, priv->ref[i].order_hint,
378 latest_order_hint = shifted_order_hints[current->last_frame_idx];
379 earliest_order_hint = shifted_order_hints[current->golden_frame_idx];
382 for (i = 0; i < AV1_NUM_REF_FRAMES; i++) {
383 int hint = shifted_order_hints[i];
384 if (!used_frame[i] && hint >= cur_frame_hint &&
385 (ref < 0 || hint >= latest_order_hint)) {
387 latest_order_hint = hint;
391 ref_frame_idx[AV1_REF_FRAME_ALTREF - AV1_REF_FRAME_LAST] = ref;
396 for (i = 0; i < AV1_NUM_REF_FRAMES; i++) {
397 int hint = shifted_order_hints[i];
398 if (!used_frame[i] && hint >= cur_frame_hint &&
399 (ref < 0 || hint < earliest_order_hint)) {
401 earliest_order_hint = hint;
405 ref_frame_idx[AV1_REF_FRAME_BWDREF - AV1_REF_FRAME_LAST] = ref;
410 for (i = 0; i < AV1_NUM_REF_FRAMES; i++) {
411 int hint = shifted_order_hints[i];
412 if (!used_frame[i] && hint >= cur_frame_hint &&
413 (ref < 0 || hint < earliest_order_hint)) {
415 earliest_order_hint = hint;
419 ref_frame_idx[AV1_REF_FRAME_ALTREF2 - AV1_REF_FRAME_LAST] = ref;
423 for (i = 0; i < AV1_REFS_PER_FRAME - 2; i++) {
424 int ref_frame = ref_frame_list[i];
425 if (ref_frame_idx[ref_frame - AV1_REF_FRAME_LAST] < 0 ) {
427 for (j = 0; j < AV1_NUM_REF_FRAMES; j++) {
428 int hint = shifted_order_hints[j];
429 if (!used_frame[j] && hint < cur_frame_hint &&
430 (ref < 0 || hint >= latest_order_hint)) {
432 latest_order_hint = hint;
436 ref_frame_idx[ref_frame - AV1_REF_FRAME_LAST] = ref;
443 for (i = 0; i < AV1_NUM_REF_FRAMES; i++) {
444 int hint = shifted_order_hints[i];
445 if (ref < 0 || hint < earliest_order_hint) {
447 earliest_order_hint = hint;
450 for (i = 0; i < AV1_REFS_PER_FRAME; i++) {
451 if (ref_frame_idx[i] < 0)
452 ref_frame_idx[i] = ref;
453 infer(ref_frame_idx[i], ref_frame_idx[i]);
459 static int FUNC(superres_params)(CodedBitstreamContext *ctx, RWContext *rw,
460 AV1RawFrameHeader *current)
462 CodedBitstreamAV1Context *priv = ctx->priv_data;
463 const AV1RawSequenceHeader *seq = priv->sequence_header;
466 if (seq->enable_superres)
469 infer(use_superres, 0);
471 if (current->use_superres) {
473 denom = current->coded_denom + AV1_SUPERRES_DENOM_MIN;
475 denom = AV1_SUPERRES_NUM;
478 priv->upscaled_width = priv->frame_width;
479 priv->frame_width = (priv->upscaled_width * AV1_SUPERRES_NUM +
485 static int FUNC(frame_size)(CodedBitstreamContext *ctx, RWContext *rw,
486 AV1RawFrameHeader *current)
488 CodedBitstreamAV1Context *priv = ctx->priv_data;
489 const AV1RawSequenceHeader *seq = priv->sequence_header;
492 if (current->frame_size_override_flag) {
493 fb(seq->frame_width_bits_minus_1 + 1, frame_width_minus_1);
494 fb(seq->frame_height_bits_minus_1 + 1, frame_height_minus_1);
496 infer(frame_width_minus_1, seq->max_frame_width_minus_1);
497 infer(frame_height_minus_1, seq->max_frame_height_minus_1);
500 priv->frame_width = current->frame_width_minus_1 + 1;
501 priv->frame_height = current->frame_height_minus_1 + 1;
503 CHECK(FUNC(superres_params)(ctx, rw, current));
508 static int FUNC(render_size)(CodedBitstreamContext *ctx, RWContext *rw,
509 AV1RawFrameHeader *current)
511 CodedBitstreamAV1Context *priv = ctx->priv_data;
514 flag(render_and_frame_size_different);
516 if (current->render_and_frame_size_different) {
517 fb(16, render_width_minus_1);
518 fb(16, render_height_minus_1);
520 infer(render_width_minus_1, current->frame_width_minus_1);
521 infer(render_height_minus_1, current->frame_height_minus_1);
524 priv->render_width = current->render_width_minus_1 + 1;
525 priv->render_height = current->render_height_minus_1 + 1;
530 static int FUNC(frame_size_with_refs)(CodedBitstreamContext *ctx, RWContext *rw,
531 AV1RawFrameHeader *current)
533 CodedBitstreamAV1Context *priv = ctx->priv_data;
536 for (i = 0; i < AV1_REFS_PER_FRAME; i++) {
537 flags(found_ref[i], 1, i);
538 if (current->found_ref[i]) {
539 AV1ReferenceFrameState *ref =
540 &priv->ref[current->ref_frame_idx[i]];
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;
550 infer(frame_width_minus_1, ref->upscaled_width - 1);
551 infer(frame_height_minus_1, ref->frame_height - 1);
552 infer(render_width_minus_1, ref->render_width - 1);
553 infer(render_height_minus_1, ref->render_height - 1);
555 priv->upscaled_width = ref->upscaled_width;
556 priv->frame_width = ref->frame_width;
557 priv->frame_height = ref->frame_height;
558 priv->render_width = ref->render_width;
559 priv->render_height = ref->render_height;
564 if (i >= AV1_REFS_PER_FRAME) {
565 CHECK(FUNC(frame_size)(ctx, rw, current));
566 CHECK(FUNC(render_size)(ctx, rw, current));
568 CHECK(FUNC(superres_params)(ctx, rw, current));
574 static int FUNC(interpolation_filter)(CodedBitstreamContext *ctx, RWContext *rw,
575 AV1RawFrameHeader *current)
579 flag(is_filter_switchable);
580 if (current->is_filter_switchable)
581 infer(interpolation_filter,
582 AV1_INTERPOLATION_FILTER_SWITCHABLE);
584 fb(2, interpolation_filter);
589 static int FUNC(tile_info)(CodedBitstreamContext *ctx, RWContext *rw,
590 AV1RawFrameHeader *current)
592 CodedBitstreamAV1Context *priv = ctx->priv_data;
593 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;
600 mi_cols = 2 * ((priv->frame_width + 7) >> 3);
601 mi_rows = 2 * ((priv->frame_height + 7) >> 3);
603 sb_cols = seq->use_128x128_superblock ? ((mi_cols + 31) >> 5)
604 : ((mi_cols + 15) >> 4);
605 sb_rows = seq->use_128x128_superblock ? ((mi_rows + 31) >> 5)
606 : ((mi_rows + 15) >> 4);
608 sb_shift = seq->use_128x128_superblock ? 5 : 4;
609 sb_size = sb_shift + 2;
611 max_tile_width_sb = AV1_MAX_TILE_WIDTH >> sb_size;
612 max_tile_area_sb = AV1_MAX_TILE_AREA >> (2 * sb_size);
614 min_log2_tile_cols = cbs_av1_tile_log2(max_tile_width_sb, sb_cols);
615 max_log2_tile_cols = cbs_av1_tile_log2(1, FFMIN(sb_cols, AV1_MAX_TILE_COLS));
616 max_log2_tile_rows = cbs_av1_tile_log2(1, FFMIN(sb_rows, AV1_MAX_TILE_ROWS));
617 min_log2_tiles = FFMAX(min_log2_tile_cols,
618 cbs_av1_tile_log2(max_tile_area_sb, sb_rows * sb_cols));
620 flag(uniform_tile_spacing_flag);
622 if (current->uniform_tile_spacing_flag) {
623 int tile_width_sb, tile_height_sb;
625 increment(tile_cols_log2, min_log2_tile_cols, max_log2_tile_cols);
627 tile_width_sb = (sb_cols + (1 << current->tile_cols_log2) - 1) >>
628 current->tile_cols_log2;
629 current->tile_cols = (sb_cols + tile_width_sb - 1) / tile_width_sb;
631 min_log2_tile_rows = FFMAX(min_log2_tiles - current->tile_cols_log2, 0);
633 increment(tile_rows_log2, min_log2_tile_rows, max_log2_tile_rows);
635 tile_height_sb = (sb_rows + (1 << current->tile_rows_log2) - 1) >>
636 current->tile_rows_log2;
637 current->tile_rows = (sb_rows + tile_height_sb - 1) / tile_height_sb;
640 int widest_tile_sb, start_sb, size_sb, max_width, max_height;
645 for (i = 0; start_sb < sb_cols && i < AV1_MAX_TILE_COLS; i++) {
646 max_width = FFMIN(sb_cols - start_sb, max_tile_width_sb);
647 ns(max_width, width_in_sbs_minus_1[i], 1, i);
648 size_sb = current->width_in_sbs_minus_1[i] + 1;
649 widest_tile_sb = FFMAX(size_sb, widest_tile_sb);
652 current->tile_cols_log2 = cbs_av1_tile_log2(1, i);
653 current->tile_cols = i;
655 if (min_log2_tiles > 0)
656 max_tile_area_sb = (sb_rows * sb_cols) >> (min_log2_tiles + 1);
658 max_tile_area_sb = sb_rows * sb_cols;
659 max_tile_height_sb = FFMAX(max_tile_area_sb / widest_tile_sb, 1);
662 for (i = 0; start_sb < sb_rows && i < AV1_MAX_TILE_ROWS; i++) {
663 max_height = FFMIN(sb_rows - start_sb, max_tile_height_sb);
664 ns(max_height, height_in_sbs_minus_1[i], 1, i);
665 size_sb = current->height_in_sbs_minus_1[i] + 1;
668 current->tile_rows_log2 = cbs_av1_tile_log2(1, i);
669 current->tile_rows = i;
672 if (current->tile_cols_log2 > 0 ||
673 current->tile_rows_log2 > 0) {
674 fb(current->tile_cols_log2 + current->tile_rows_log2,
675 context_update_tile_id);
676 fb(2, tile_size_bytes_minus1);
678 infer(context_update_tile_id, 0);
681 priv->tile_cols = current->tile_cols;
682 priv->tile_rows = current->tile_rows;
687 static int FUNC(quantization_params)(CodedBitstreamContext *ctx, RWContext *rw,
688 AV1RawFrameHeader *current)
690 CodedBitstreamAV1Context *priv = ctx->priv_data;
691 const AV1RawSequenceHeader *seq = priv->sequence_header;
696 delta_q(delta_q_y_dc);
698 if (priv->num_planes > 1) {
699 if (seq->color_config.separate_uv_delta_q)
702 infer(diff_uv_delta, 0);
704 delta_q(delta_q_u_dc);
705 delta_q(delta_q_u_ac);
707 if (current->diff_uv_delta) {
708 delta_q(delta_q_v_dc);
709 delta_q(delta_q_v_ac);
711 infer(delta_q_v_dc, current->delta_q_u_dc);
712 infer(delta_q_v_ac, current->delta_q_u_ac);
715 infer(delta_q_u_dc, 0);
716 infer(delta_q_u_ac, 0);
717 infer(delta_q_v_dc, 0);
718 infer(delta_q_v_ac, 0);
722 if (current->using_qmatrix) {
725 if (seq->color_config.separate_uv_delta_q)
728 infer(qm_v, current->qm_u);
734 static int FUNC(segmentation_params)(CodedBitstreamContext *ctx, RWContext *rw,
735 AV1RawFrameHeader *current)
737 static const uint8_t bits[AV1_SEG_LVL_MAX] = { 8, 6, 6, 6, 6, 3, 0, 0 };
738 static const uint8_t sign[AV1_SEG_LVL_MAX] = { 1, 1, 1, 1, 1, 0, 0, 0 };
741 flag(segmentation_enabled);
743 if (current->segmentation_enabled) {
744 if (current->primary_ref_frame == AV1_PRIMARY_REF_NONE) {
745 infer(segmentation_update_map, 1);
746 infer(segmentation_temporal_update, 0);
747 infer(segmentation_update_data, 1);
749 flag(segmentation_update_map);
750 if (current->segmentation_update_map)
751 flag(segmentation_temporal_update);
753 infer(segmentation_temporal_update, 0);
754 flag(segmentation_update_data);
757 if (current->segmentation_update_data) {
758 for (i = 0; i < AV1_MAX_SEGMENTS; i++) {
759 for (j = 0; j < AV1_SEG_LVL_MAX; j++) {
760 flags(feature_enabled[i][j], 2, i, j);
762 if (current->feature_enabled[i][j] && bits[j] > 0) {
764 sus(1 + bits[j], feature_value[i][j], 2, i, j);
766 fbs(bits[j], feature_value[i][j], 2, i, j);
768 infer(feature_value[i][j], 0);
774 for (i = 0; i < AV1_MAX_SEGMENTS; i++) {
775 for (j = 0; j < AV1_SEG_LVL_MAX; j++) {
776 infer(feature_enabled[i][j], 0);
777 infer(feature_value[i][j], 0);
785 static int FUNC(delta_q_params)(CodedBitstreamContext *ctx, RWContext *rw,
786 AV1RawFrameHeader *current)
790 if (current->base_q_idx > 0)
791 flag(delta_q_present);
793 infer(delta_q_present, 0);
795 if (current->delta_q_present)
801 static int FUNC(delta_lf_params)(CodedBitstreamContext *ctx, RWContext *rw,
802 AV1RawFrameHeader *current)
806 if (current->delta_q_present) {
807 if (!current->allow_intrabc)
808 flag(delta_lf_present);
810 infer(delta_lf_present, 0);
811 if (current->delta_lf_present) {
813 flag(delta_lf_multi);
815 infer(delta_lf_res, 0);
816 infer(delta_lf_multi, 0);
819 infer(delta_lf_present, 0);
820 infer(delta_lf_res, 0);
821 infer(delta_lf_multi, 0);
827 static int FUNC(loop_filter_params)(CodedBitstreamContext *ctx, RWContext *rw,
828 AV1RawFrameHeader *current)
830 CodedBitstreamAV1Context *priv = ctx->priv_data;
833 if (priv->coded_lossless || current->allow_intrabc) {
834 infer(loop_filter_level[0], 0);
835 infer(loop_filter_level[1], 0);
836 infer(loop_filter_ref_deltas[AV1_REF_FRAME_INTRA], 1);
837 infer(loop_filter_ref_deltas[AV1_REF_FRAME_LAST], 0);
838 infer(loop_filter_ref_deltas[AV1_REF_FRAME_LAST2], 0);
839 infer(loop_filter_ref_deltas[AV1_REF_FRAME_LAST3], 0);
840 infer(loop_filter_ref_deltas[AV1_REF_FRAME_BWDREF], 0);
841 infer(loop_filter_ref_deltas[AV1_REF_FRAME_GOLDEN], -1);
842 infer(loop_filter_ref_deltas[AV1_REF_FRAME_ALTREF], -1);
843 infer(loop_filter_ref_deltas[AV1_REF_FRAME_ALTREF2], -1);
844 for (i = 0; i < 2; i++)
845 infer(loop_filter_mode_deltas[i], 0);
849 fb(6, loop_filter_level[0]);
850 fb(6, loop_filter_level[1]);
852 if (priv->num_planes > 1) {
853 if (current->loop_filter_level[0] ||
854 current->loop_filter_level[1]) {
855 fb(6, loop_filter_level[2]);
856 fb(6, loop_filter_level[3]);
860 fb(3, loop_filter_sharpness);
862 flag(loop_filter_delta_enabled);
863 if (current->loop_filter_delta_enabled) {
864 flag(loop_filter_delta_update);
865 if (current->loop_filter_delta_update) {
866 for (i = 0; i < AV1_TOTAL_REFS_PER_FRAME; i++) {
867 flags(update_ref_delta[i], 1, i);
868 if (current->update_ref_delta[i])
869 sus(1 + 6, loop_filter_ref_deltas[i], 1, i);
871 for (i = 0; i < 2; i++) {
872 flags(update_mode_delta[i], 1, i);
873 if (current->update_mode_delta[i])
874 sus(1 + 6, loop_filter_mode_deltas[i], 1, i);
882 static int FUNC(cdef_params)(CodedBitstreamContext *ctx, RWContext *rw,
883 AV1RawFrameHeader *current)
885 CodedBitstreamAV1Context *priv = ctx->priv_data;
886 const AV1RawSequenceHeader *seq = priv->sequence_header;
889 if (priv->coded_lossless || current->allow_intrabc ||
891 infer(cdef_damping_minus_3, 0);
893 infer(cdef_y_pri_strength[0], 0);
894 infer(cdef_y_sec_strength[0], 0);
895 infer(cdef_uv_pri_strength[0], 0);
896 infer(cdef_uv_sec_strength[0], 0);
901 fb(2, cdef_damping_minus_3);
904 for (i = 0; i < (1 << current->cdef_bits); i++) {
905 fbs(4, cdef_y_pri_strength[i], 1, i);
906 fbs(2, cdef_y_sec_strength[i], 1, i);
908 if (priv->num_planes > 1) {
909 fbs(4, cdef_uv_pri_strength[i], 1, i);
910 fbs(2, cdef_uv_sec_strength[i], 1, i);
917 static int FUNC(lr_params)(CodedBitstreamContext *ctx, RWContext *rw,
918 AV1RawFrameHeader *current)
920 CodedBitstreamAV1Context *priv = ctx->priv_data;
921 const AV1RawSequenceHeader *seq = priv->sequence_header;
922 int uses_lr, uses_chroma_lr;
925 if (priv->all_lossless || current->allow_intrabc ||
926 !seq->enable_restoration) {
930 uses_lr = uses_chroma_lr = 0;
931 for (i = 0; i < priv->num_planes; i++) {
932 fbs(2, lr_type[i], 1, i);
934 if (current->lr_type[i] != 0) {
942 if (seq->use_128x128_superblock)
943 increment(lr_unit_shift, 1, 2);
945 increment(lr_unit_shift, 0, 2);
947 if(seq->color_config.subsampling_x &&
948 seq->color_config.subsampling_y && uses_chroma_lr) {
951 infer(lr_uv_shift, 0);
958 static int FUNC(read_tx_mode)(CodedBitstreamContext *ctx, RWContext *rw,
959 AV1RawFrameHeader *current)
961 CodedBitstreamAV1Context *priv = ctx->priv_data;
964 if (priv->coded_lossless)
967 increment(tx_mode, 1, 2);
972 static int FUNC(frame_reference_mode)(CodedBitstreamContext *ctx, RWContext *rw,
973 AV1RawFrameHeader *current)
977 if (current->frame_type == AV1_FRAME_INTRA_ONLY ||
978 current->frame_type == AV1_FRAME_KEY)
979 infer(reference_select, 0);
981 flag(reference_select);
986 static int FUNC(skip_mode_params)(CodedBitstreamContext *ctx, RWContext *rw,
987 AV1RawFrameHeader *current)
989 CodedBitstreamAV1Context *priv = ctx->priv_data;
990 const AV1RawSequenceHeader *seq = priv->sequence_header;
991 int skip_mode_allowed;
994 if (current->frame_type == AV1_FRAME_KEY ||
995 current->frame_type == AV1_FRAME_INTRA_ONLY ||
996 !current->reference_select || !seq->enable_order_hint) {
997 skip_mode_allowed = 0;
999 int forward_idx, backward_idx;
1000 int forward_hint, backward_hint;
1001 int ref_hint, dist, i;
1005 for (i = 0; i < AV1_REFS_PER_FRAME; i++) {
1006 ref_hint = priv->ref[current->ref_frame_idx[i]].order_hint;
1007 dist = cbs_av1_get_relative_dist(seq, ref_hint,
1010 if (forward_idx < 0 ||
1011 cbs_av1_get_relative_dist(seq, ref_hint,
1012 forward_hint) > 0) {
1014 forward_hint = ref_hint;
1016 } else if (dist > 0) {
1017 if (backward_idx < 0 ||
1018 cbs_av1_get_relative_dist(seq, ref_hint,
1019 backward_hint) < 0) {
1021 backward_hint = ref_hint;
1026 if (forward_idx < 0) {
1027 skip_mode_allowed = 0;
1028 } else if (backward_idx >= 0) {
1029 skip_mode_allowed = 1;
1030 // Frames for skip mode are forward_idx and backward_idx.
1032 int second_forward_idx;
1033 int second_forward_hint;
1035 second_forward_idx = -1;
1036 for (i = 0; i < AV1_REFS_PER_FRAME; i++) {
1037 ref_hint = priv->ref[current->ref_frame_idx[i]].order_hint;
1038 if (cbs_av1_get_relative_dist(seq, ref_hint,
1039 forward_hint) < 0) {
1040 if (second_forward_idx < 0 ||
1041 cbs_av1_get_relative_dist(seq, ref_hint,
1042 second_forward_hint) > 0) {
1043 second_forward_idx = i;
1044 second_forward_hint = ref_hint;
1049 if (second_forward_idx < 0) {
1050 skip_mode_allowed = 0;
1052 skip_mode_allowed = 1;
1053 // Frames for skip mode are forward_idx and second_forward_idx.
1058 if (skip_mode_allowed)
1059 flag(skip_mode_present);
1061 infer(skip_mode_present, 0);
1066 static int FUNC(global_motion_param)(CodedBitstreamContext *ctx, RWContext *rw,
1067 AV1RawFrameHeader *current,
1068 int type, int ref, int idx)
1070 uint32_t abs_bits, prec_bits, num_syms;
1074 if (type == AV1_WARP_MODEL_TRANSLATION) {
1075 abs_bits = AV1_GM_ABS_TRANS_ONLY_BITS - !current->allow_high_precision_mv;
1076 prec_bits = AV1_GM_TRANS_ONLY_PREC_BITS - !current->allow_high_precision_mv;
1078 abs_bits = AV1_GM_ABS_TRANS_BITS;
1079 prec_bits = AV1_GM_TRANS_PREC_BITS;
1082 abs_bits = AV1_GM_ABS_ALPHA_BITS;
1083 prec_bits = AV1_GM_ALPHA_PREC_BITS;
1086 num_syms = 2 * (1 << abs_bits) + 1;
1087 subexp(gm_params[ref][idx], num_syms, 2, ref, idx);
1089 // Actual gm_params value is not reconstructed here.
1095 static int FUNC(global_motion_params)(CodedBitstreamContext *ctx, RWContext *rw,
1096 AV1RawFrameHeader *current)
1101 if (current->frame_type == AV1_FRAME_KEY ||
1102 current->frame_type == AV1_FRAME_INTRA_ONLY)
1105 for (ref = AV1_REF_FRAME_LAST; ref <= AV1_REF_FRAME_ALTREF; ref++) {
1106 flags(is_global[ref], 1, ref);
1107 if (current->is_global[ref]) {
1108 flags(is_rot_zoom[ref], 1, ref);
1109 if (current->is_rot_zoom[ref]) {
1110 type = AV1_WARP_MODEL_ROTZOOM;
1112 flags(is_translation[ref], 1, ref);
1113 type = current->is_translation[ref] ? AV1_WARP_MODEL_TRANSLATION
1114 : AV1_WARP_MODEL_AFFINE;
1117 type = AV1_WARP_MODEL_IDENTITY;
1120 if (type >= AV1_WARP_MODEL_ROTZOOM) {
1121 CHECK(FUNC(global_motion_param)(ctx, rw, current, type, ref, 2));
1122 CHECK(FUNC(global_motion_param)(ctx, rw, current, type, ref, 3));
1123 if (type == AV1_WARP_MODEL_AFFINE) {
1124 CHECK(FUNC(global_motion_param)(ctx, rw, current, type, ref, 4));
1125 CHECK(FUNC(global_motion_param)(ctx, rw, current, type, ref, 5));
1127 // gm_params[ref][4] = -gm_params[ref][3]
1128 // gm_params[ref][5] = gm_params[ref][2]
1131 if (type >= AV1_WARP_MODEL_TRANSLATION) {
1132 CHECK(FUNC(global_motion_param)(ctx, rw, current, type, ref, 0));
1133 CHECK(FUNC(global_motion_param)(ctx, rw, current, type, ref, 1));
1140 static int FUNC(film_grain_params)(CodedBitstreamContext *ctx, RWContext *rw,
1141 AV1RawFrameHeader *current)
1143 CodedBitstreamAV1Context *priv = ctx->priv_data;
1144 const AV1RawSequenceHeader *seq = priv->sequence_header;
1145 int num_pos_luma, num_pos_chroma;
1148 if (!seq->film_grain_params_present ||
1149 (!current->show_frame && !current->showable_frame))
1154 if (!current->apply_grain)
1159 if (current->frame_type == AV1_FRAME_INTER)
1162 infer(update_grain, 1);
1164 if (!current->update_grain) {
1165 fb(3, film_grain_params_ref_idx);
1169 fc(4, num_y_points, 0, 14);
1170 for (i = 0; i < current->num_y_points; i++) {
1171 fcs(8, point_y_value[i],
1172 i ? current->point_y_value[i - 1] + 1 : 0,
1173 MAX_UINT_BITS(8) - (current->num_y_points - i - 1),
1175 fbs(8, point_y_scaling[i], 1, i);
1178 if (seq->color_config.mono_chrome)
1179 infer(chroma_scaling_from_luma, 0);
1181 flag(chroma_scaling_from_luma);
1183 if (seq->color_config.mono_chrome ||
1184 current->chroma_scaling_from_luma ||
1185 (seq->color_config.subsampling_x == 1 &&
1186 seq->color_config.subsampling_y == 1 &&
1187 current->num_y_points == 0)) {
1188 infer(num_cb_points, 0);
1189 infer(num_cr_points, 0);
1191 fc(4, num_cb_points, 0, 10);
1192 for (i = 0; i < current->num_cb_points; i++) {
1193 fcs(8, point_cb_value[i],
1194 i ? current->point_cb_value[i - 1] + 1 : 0,
1195 MAX_UINT_BITS(8) - (current->num_cb_points - i - 1),
1197 fbs(8, point_cb_scaling[i], 1, i);
1199 fc(4, num_cr_points, 0, 10);
1200 for (i = 0; i < current->num_cr_points; i++) {
1201 fcs(8, point_cr_value[i],
1202 i ? current->point_cr_value[i - 1] + 1 : 0,
1203 MAX_UINT_BITS(8) - (current->num_cr_points - i - 1),
1205 fbs(8, point_cr_scaling[i], 1, i);
1209 fb(2, grain_scaling_minus_8);
1210 fb(2, ar_coeff_lag);
1211 num_pos_luma = 2 * current->ar_coeff_lag * (current->ar_coeff_lag + 1);
1212 if (current->num_y_points) {
1213 num_pos_chroma = num_pos_luma + 1;
1214 for (i = 0; i < num_pos_luma; i++)
1215 fbs(8, ar_coeffs_y_plus_128[i], 1, i);
1217 num_pos_chroma = num_pos_luma;
1219 if (current->chroma_scaling_from_luma || current->num_cb_points) {
1220 for (i = 0; i < num_pos_chroma; i++)
1221 fbs(8, ar_coeffs_cb_plus_128[i], 1, i);
1223 if (current->chroma_scaling_from_luma || current->num_cr_points) {
1224 for (i = 0; i < num_pos_chroma; i++)
1225 fbs(8, ar_coeffs_cr_plus_128[i], 1, i);
1227 fb(2, ar_coeff_shift_minus_6);
1228 fb(2, grain_scale_shift);
1229 if (current->num_cb_points) {
1231 fb(8, cb_luma_mult);
1234 if (current->num_cr_points) {
1236 fb(8, cr_luma_mult);
1241 flag(clip_to_restricted_range);
1246 static int FUNC(uncompressed_header)(CodedBitstreamContext *ctx, RWContext *rw,
1247 AV1RawFrameHeader *current)
1249 CodedBitstreamAV1Context *priv = ctx->priv_data;
1250 const AV1RawSequenceHeader *seq;
1251 int id_len, diff_len, all_frames, frame_is_intra, order_hint_bits;
1254 if (!priv->sequence_header) {
1255 av_log(ctx->log_ctx, AV_LOG_ERROR, "No sequence header available: "
1256 "unable to decode frame header.\n");
1257 return AVERROR_INVALIDDATA;
1259 seq = priv->sequence_header;
1261 id_len = seq->additional_frame_id_length_minus_1 +
1262 seq->delta_frame_id_length_minus_2 + 3;
1263 all_frames = (1 << AV1_NUM_REF_FRAMES) - 1;
1265 if (seq->reduced_still_picture_header) {
1266 infer(show_existing_frame, 0);
1267 infer(frame_type, AV1_FRAME_KEY);
1268 infer(show_frame, 1);
1269 infer(showable_frame, 0);
1273 flag(show_existing_frame);
1275 if (current->show_existing_frame) {
1276 AV1ReferenceFrameState *frame;
1278 fb(3, frame_to_show_map_idx);
1279 frame = &priv->ref[current->frame_to_show_map_idx];
1281 if (!frame->valid) {
1282 av_log(ctx->log_ctx, AV_LOG_ERROR, "Missing reference frame needed for "
1283 "show_existing_frame (frame_to_show_map_idx = %d).\n",
1284 current->frame_to_show_map_idx);
1285 return AVERROR_INVALIDDATA;
1288 if (seq->decoder_model_info_present_flag &&
1289 !seq->timing_info.equal_picture_interval) {
1290 fb(seq->decoder_model_info.frame_presentation_time_length_minus_1 + 1,
1291 frame_presentation_time);
1294 if (seq->frame_id_numbers_present_flag)
1295 fb(id_len, display_frame_id);
1297 if (frame->frame_type == AV1_FRAME_KEY)
1298 infer(refresh_frame_flags, all_frames);
1300 infer(refresh_frame_flags, 0);
1302 infer(frame_type, frame->frame_type);
1303 infer(frame_width_minus_1, frame->upscaled_width - 1);
1304 infer(frame_height_minus_1, frame->frame_height - 1);
1305 infer(render_width_minus_1, frame->render_width - 1);
1306 infer(render_height_minus_1, frame->render_height - 1);
1308 priv->upscaled_width = frame->upscaled_width;
1309 priv->frame_width = frame->frame_width;
1310 priv->frame_height = frame->frame_height;
1311 priv->render_width = frame->render_width;
1312 priv->render_height = frame->render_height;
1318 frame_is_intra = (current->frame_type == AV1_FRAME_INTRA_ONLY ||
1319 current->frame_type == AV1_FRAME_KEY);
1322 if (current->show_frame &&
1323 seq->decoder_model_info_present_flag &&
1324 !seq->timing_info.equal_picture_interval) {
1325 fb(seq->decoder_model_info.frame_presentation_time_length_minus_1 + 1,
1326 frame_presentation_time);
1328 if (current->show_frame)
1329 infer(showable_frame, current->frame_type != AV1_FRAME_KEY);
1331 flag(showable_frame);
1333 if (current->frame_type == AV1_FRAME_SWITCH ||
1334 (current->frame_type == AV1_FRAME_KEY && current->show_frame))
1335 infer(error_resilient_mode, 1);
1337 flag(error_resilient_mode);
1340 if (current->frame_type == AV1_FRAME_KEY && current->show_frame) {
1341 for (i = 0; i < AV1_NUM_REF_FRAMES; i++) {
1342 priv->ref[i].valid = 0;
1343 priv->ref[i].order_hint = 0;
1347 flag(disable_cdf_update);
1349 if (seq->seq_force_screen_content_tools ==
1350 AV1_SELECT_SCREEN_CONTENT_TOOLS) {
1351 flag(allow_screen_content_tools);
1353 infer(allow_screen_content_tools,
1354 seq->seq_force_screen_content_tools);
1356 if (current->allow_screen_content_tools) {
1357 if (seq->seq_force_integer_mv == AV1_SELECT_INTEGER_MV)
1358 flag(force_integer_mv);
1360 infer(force_integer_mv, seq->seq_force_integer_mv);
1362 infer(force_integer_mv, 0);
1365 if (seq->frame_id_numbers_present_flag) {
1366 fb(id_len, current_frame_id);
1368 diff_len = seq->delta_frame_id_length_minus_2 + 2;
1369 for (i = 0; i < AV1_NUM_REF_FRAMES; i++) {
1370 if (current->current_frame_id > (1 << diff_len)) {
1371 if (priv->ref[i].frame_id > current->current_frame_id ||
1372 priv->ref[i].frame_id < (current->current_frame_id -
1374 priv->ref[i].valid = 0;
1376 if (priv->ref[i].frame_id > current->current_frame_id &&
1377 priv->ref[i].frame_id < ((1 << id_len) +
1378 current->current_frame_id -
1380 priv->ref[i].valid = 0;
1384 infer(current_frame_id, 0);
1387 if (current->frame_type == AV1_FRAME_SWITCH)
1388 infer(frame_size_override_flag, 1);
1389 else if(seq->reduced_still_picture_header)
1390 infer(frame_size_override_flag, 0);
1392 flag(frame_size_override_flag);
1395 seq->enable_order_hint ? seq->order_hint_bits_minus_1 + 1 : 0;
1396 if (order_hint_bits > 0)
1397 fb(order_hint_bits, order_hint);
1399 infer(order_hint, 0);
1400 priv->order_hint = current->order_hint;
1402 if (frame_is_intra || current->error_resilient_mode)
1403 infer(primary_ref_frame, AV1_PRIMARY_REF_NONE);
1405 fb(3, primary_ref_frame);
1407 if (seq->decoder_model_info_present_flag) {
1408 flag(buffer_removal_time_present_flag);
1409 if (current->buffer_removal_time_present_flag) {
1410 for (i = 0; i <= seq->operating_points_cnt_minus_1; i++) {
1411 if (seq->decoder_model_present_for_this_op[i]) {
1412 int op_pt_idc = seq->operating_point_idc[i];
1413 int in_temporal_layer = (op_pt_idc >> priv->temporal_id ) & 1;
1414 int in_spatial_layer = (op_pt_idc >> (priv->spatial_id + 8)) & 1;
1415 if (seq->operating_point_idc[i] == 0 ||
1416 (in_temporal_layer && in_spatial_layer)) {
1417 fbs(seq->decoder_model_info.buffer_removal_time_length_minus_1 + 1,
1418 buffer_removal_time[i], 1, i);
1425 if (current->frame_type == AV1_FRAME_SWITCH ||
1426 (current->frame_type == AV1_FRAME_KEY && current->show_frame))
1427 infer(refresh_frame_flags, all_frames);
1429 fb(8, refresh_frame_flags);
1431 if (!frame_is_intra || current->refresh_frame_flags != all_frames) {
1432 if (current->error_resilient_mode && seq->enable_order_hint) {
1433 for (i = 0; i < AV1_NUM_REF_FRAMES; i++) {
1434 fbs(order_hint_bits, ref_order_hint[i], 1, i);
1435 if (current->ref_order_hint[i] != priv->ref[i].order_hint)
1436 priv->ref[i].valid = 0;
1441 if (current->frame_type == AV1_FRAME_KEY ||
1442 current->frame_type == AV1_FRAME_INTRA_ONLY) {
1443 CHECK(FUNC(frame_size)(ctx, rw, current));
1444 CHECK(FUNC(render_size)(ctx, rw, current));
1446 if (current->allow_screen_content_tools &&
1447 priv->upscaled_width == priv->frame_width)
1448 flag(allow_intrabc);
1450 infer(allow_intrabc, 0);
1453 if (!seq->enable_order_hint) {
1454 infer(frame_refs_short_signaling, 0);
1456 flag(frame_refs_short_signaling);
1457 if (current->frame_refs_short_signaling) {
1458 fb(3, last_frame_idx);
1459 fb(3, golden_frame_idx);
1460 CHECK(FUNC(set_frame_refs)(ctx, rw, current));
1464 for (i = 0; i < AV1_REFS_PER_FRAME; i++) {
1465 if (!current->frame_refs_short_signaling)
1466 fbs(3, ref_frame_idx[i], 1, i);
1467 if (seq->frame_id_numbers_present_flag) {
1468 fbs(seq->delta_frame_id_length_minus_2 + 2,
1469 delta_frame_id_minus1[i], 1, i);
1473 if (current->frame_size_override_flag &&
1474 !current->error_resilient_mode) {
1475 CHECK(FUNC(frame_size_with_refs)(ctx, rw, current));
1477 CHECK(FUNC(frame_size)(ctx, rw, current));
1478 CHECK(FUNC(render_size)(ctx, rw, current));
1481 if (current->force_integer_mv)
1482 infer(allow_high_precision_mv, 0);
1484 flag(allow_high_precision_mv);
1486 CHECK(FUNC(interpolation_filter)(ctx, rw, current));
1488 flag(is_motion_mode_switchable);
1490 if (current->error_resilient_mode ||
1491 !seq->enable_ref_frame_mvs)
1492 infer(use_ref_frame_mvs, 0);
1494 flag(use_ref_frame_mvs);
1496 infer(allow_intrabc, 0);
1499 if (!frame_is_intra) {
1500 // Derive reference frame sign biases.
1503 if (seq->reduced_still_picture_header || current->disable_cdf_update)
1504 infer(disable_frame_end_update_cdf, 1);
1506 flag(disable_frame_end_update_cdf);
1508 if (current->primary_ref_frame == AV1_PRIMARY_REF_NONE) {
1509 // Init non-coeff CDFs.
1510 // Setup past independence.
1512 // Load CDF tables from previous frame.
1513 // Load params from previous frame.
1516 if (current->use_ref_frame_mvs) {
1517 // Perform motion field estimation process.
1520 CHECK(FUNC(tile_info)(ctx, rw, current));
1522 CHECK(FUNC(quantization_params)(ctx, rw, current));
1524 CHECK(FUNC(segmentation_params)(ctx, rw, current));
1526 CHECK(FUNC(delta_q_params)(ctx, rw, current));
1528 CHECK(FUNC(delta_lf_params)(ctx, rw, current));
1530 // Init coeff CDFs / load previous segments.
1532 priv->coded_lossless = 1;
1533 for (i = 0; i < AV1_MAX_SEGMENTS; i++) {
1535 if (current->feature_enabled[i][AV1_SEG_LVL_ALT_Q]) {
1536 qindex = (current->base_q_idx +
1537 current->feature_value[i][AV1_SEG_LVL_ALT_Q]);
1539 qindex = current->base_q_idx;
1541 qindex = av_clip_uintp2(qindex, 8);
1543 if (qindex || current->delta_q_y_dc ||
1544 current->delta_q_u_ac || current->delta_q_u_dc ||
1545 current->delta_q_v_ac || current->delta_q_v_dc) {
1546 priv->coded_lossless = 0;
1549 priv->all_lossless = priv->coded_lossless &&
1550 priv->frame_width == priv->upscaled_width;
1552 CHECK(FUNC(loop_filter_params)(ctx, rw, current));
1554 CHECK(FUNC(cdef_params)(ctx, rw, current));
1556 CHECK(FUNC(lr_params)(ctx, rw, current));
1558 CHECK(FUNC(read_tx_mode)(ctx, rw, current));
1560 CHECK(FUNC(frame_reference_mode)(ctx, rw, current));
1562 CHECK(FUNC(skip_mode_params)(ctx, rw, current));
1564 if (frame_is_intra || current->error_resilient_mode ||
1565 !seq->enable_warped_motion)
1566 infer(allow_warped_motion, 0);
1568 flag(allow_warped_motion);
1570 flag(reduced_tx_set);
1572 CHECK(FUNC(global_motion_params)(ctx, rw, current));
1574 CHECK(FUNC(film_grain_params)(ctx, rw, current));
1576 for (i = 0; i < AV1_NUM_REF_FRAMES; i++) {
1577 if (current->refresh_frame_flags & (1 << i)) {
1578 priv->ref[i] = (AV1ReferenceFrameState) {
1580 .frame_id = current->current_frame_id,
1581 .upscaled_width = priv->upscaled_width,
1582 .frame_width = priv->frame_width,
1583 .frame_height = priv->frame_height,
1584 .render_width = priv->render_width,
1585 .render_height = priv->render_height,
1586 .frame_type = current->frame_type,
1587 .subsampling_x = seq->color_config.subsampling_x,
1588 .subsampling_y = seq->color_config.subsampling_y,
1589 .bit_depth = priv->bit_depth,
1590 .order_hint = priv->order_hint,
1595 av_log(ctx->log_ctx, AV_LOG_DEBUG, "Frame %d: size %dx%d "
1596 "upscaled %d render %dx%d subsample %dx%d "
1597 "bitdepth %d tiles %dx%d.\n", priv->order_hint,
1598 priv->frame_width, priv->frame_height, priv->upscaled_width,
1599 priv->render_width, priv->render_height,
1600 seq->color_config.subsampling_x + 1,
1601 seq->color_config.subsampling_y + 1, priv->bit_depth,
1602 priv->tile_rows, priv->tile_cols);
1607 static int FUNC(frame_header_obu)(CodedBitstreamContext *ctx, RWContext *rw,
1608 AV1RawFrameHeader *current, int redundant,
1609 AVBufferRef *rw_buffer_ref)
1611 CodedBitstreamAV1Context *priv = ctx->priv_data;
1612 int start_pos, fh_bits, fh_bytes, err;
1615 if (priv->seen_frame_header) {
1617 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid repeated "
1618 "frame header OBU.\n");
1619 return AVERROR_INVALIDDATA;
1625 HEADER("Redundant Frame Header");
1627 av_assert0(priv->frame_header_ref && priv->frame_header);
1629 init_get_bits(&fh, priv->frame_header,
1630 priv->frame_header_size);
1631 for (i = 0; i < priv->frame_header_size; i += 8) {
1632 b = FFMIN(priv->frame_header_size - i, 8);
1633 val = get_bits(&fh, b);
1634 xf(b, frame_header_copy[i],
1635 val, val, val, 1, i / 8);
1640 HEADER("Redundant Frame Header (used as Frame Header)");
1642 HEADER("Frame Header");
1645 start_pos = get_bits_count(rw);
1647 start_pos = put_bits_count(rw);
1650 CHECK(FUNC(uncompressed_header)(ctx, rw, current));
1652 if (current->show_existing_frame) {
1653 priv->seen_frame_header = 0;
1655 priv->seen_frame_header = 1;
1657 av_buffer_unref(&priv->frame_header_ref);
1660 fh_bits = get_bits_count(rw) - start_pos;
1661 fh_start = (uint8_t*)rw->buffer + start_pos / 8;
1663 // Need to flush the bitwriter so that we can copy its output,
1664 // but use a copy so we don't affect the caller's structure.
1666 PutBitContext tmp = *rw;
1667 flush_put_bits(&tmp);
1670 fh_bits = put_bits_count(rw) - start_pos;
1671 fh_start = rw->buf + start_pos / 8;
1673 fh_bytes = (fh_bits + 7) / 8;
1675 priv->frame_header_size = fh_bits;
1677 if (rw_buffer_ref) {
1678 priv->frame_header_ref = av_buffer_ref(rw_buffer_ref);
1679 if (!priv->frame_header_ref)
1680 return AVERROR(ENOMEM);
1681 priv->frame_header = fh_start;
1683 priv->frame_header_ref =
1684 av_buffer_alloc(fh_bytes + AV_INPUT_BUFFER_PADDING_SIZE);
1685 if (!priv->frame_header_ref)
1686 return AVERROR(ENOMEM);
1687 priv->frame_header = priv->frame_header_ref->data;
1688 memcpy(priv->frame_header, fh_start, fh_bytes);
1696 static int FUNC(tile_group_obu)(CodedBitstreamContext *ctx, RWContext *rw,
1697 AV1RawTileGroup *current)
1699 CodedBitstreamAV1Context *priv = ctx->priv_data;
1700 int num_tiles, tile_bits;
1703 HEADER("Tile Group");
1705 num_tiles = priv->tile_cols * priv->tile_rows;
1707 flag(tile_start_and_end_present_flag);
1709 infer(tile_start_and_end_present_flag, 0);
1711 if (num_tiles == 1 || !current->tile_start_and_end_present_flag) {
1713 infer(tg_end, num_tiles - 1);
1715 tile_bits = cbs_av1_tile_log2(1, priv->tile_cols) +
1716 cbs_av1_tile_log2(1, priv->tile_rows);
1717 fb(tile_bits, tg_start);
1718 fb(tile_bits, tg_end);
1721 CHECK(FUNC(byte_alignment)(ctx, rw));
1723 // Reset header for next frame.
1724 if (current->tg_end == num_tiles - 1)
1725 priv->seen_frame_header = 0;
1727 // Tile data follows.
1732 static int FUNC(frame_obu)(CodedBitstreamContext *ctx, RWContext *rw,
1733 AV1RawFrame *current,
1734 AVBufferRef *rw_buffer_ref)
1738 CHECK(FUNC(frame_header_obu)(ctx, rw, ¤t->header,
1741 CHECK(FUNC(byte_alignment)(ctx, rw));
1743 CHECK(FUNC(tile_group_obu)(ctx, rw, ¤t->tile_group));
1748 static int FUNC(tile_list_obu)(CodedBitstreamContext *ctx, RWContext *rw,
1749 AV1RawTileList *current)
1753 fb(8, output_frame_width_in_tiles_minus_1);
1754 fb(8, output_frame_height_in_tiles_minus_1);
1756 fb(16, tile_count_minus_1);
1758 // Tile data follows.
1763 static int FUNC(metadata_hdr_cll)(CodedBitstreamContext *ctx, RWContext *rw,
1764 AV1RawMetadataHDRCLL *current)
1774 static int FUNC(metadata_hdr_mdcv)(CodedBitstreamContext *ctx, RWContext *rw,
1775 AV1RawMetadataHDRMDCV *current)
1779 for (i = 0; i < 3; i++) {
1780 fbs(16, primary_chromaticity_x[i], 1, i);
1781 fbs(16, primary_chromaticity_y[i], 1, i);
1784 fb(16, white_point_chromaticity_x);
1785 fb(16, white_point_chromaticity_y);
1787 fc(32, luminance_max, 1, MAX_UINT_BITS(32));
1788 // luminance_min must be lower than luminance_max. Convert luminance_max from
1789 // 24.8 fixed point to 18.14 fixed point in order to compare them.
1790 fc(32, luminance_min, 0, FFMIN(((uint64_t)current->luminance_max << 6) - 1,
1791 MAX_UINT_BITS(32)));
1796 static int FUNC(scalability_structure)(CodedBitstreamContext *ctx, RWContext *rw,
1797 AV1RawMetadataScalability *current)
1799 CodedBitstreamAV1Context *priv = ctx->priv_data;
1800 const AV1RawSequenceHeader *seq;
1803 if (!priv->sequence_header) {
1804 av_log(ctx->log_ctx, AV_LOG_ERROR, "No sequence header available: "
1805 "unable to parse scalability metadata.\n");
1806 return AVERROR_INVALIDDATA;
1808 seq = priv->sequence_header;
1810 fb(2, spatial_layers_cnt_minus_1);
1811 flag(spatial_layer_dimensions_present_flag);
1812 flag(spatial_layer_description_present_flag);
1813 flag(temporal_group_description_present_flag);
1814 fc(3, scalability_structure_reserved_3bits, 0, 0);
1815 if (current->spatial_layer_dimensions_present_flag) {
1816 for (i = 0; i <= current->spatial_layers_cnt_minus_1; i++) {
1817 fcs(16, spatial_layer_max_width[i],
1818 0, seq->max_frame_width_minus_1 + 1, 1, i);
1819 fcs(16, spatial_layer_max_height[i],
1820 0, seq->max_frame_height_minus_1 + 1, 1, i);
1823 if (current->spatial_layer_description_present_flag) {
1824 for (i = 0; i <= current->spatial_layers_cnt_minus_1; i++)
1825 fbs(8, spatial_layer_ref_id[i], 1, i);
1827 if (current->temporal_group_description_present_flag) {
1828 fb(8, temporal_group_size);
1829 for (i = 0; i < current->temporal_group_size; i++) {
1830 fbs(3, temporal_group_temporal_id[i], 1, i);
1831 flags(temporal_group_temporal_switching_up_point_flag[i], 1, i);
1832 flags(temporal_group_spatial_switching_up_point_flag[i], 1, i);
1833 fbs(3, temporal_group_ref_cnt[i], 1, i);
1834 for (j = 0; j < current->temporal_group_ref_cnt[i]; j++) {
1835 fbs(8, temporal_group_ref_pic_diff[i][j], 2, i, j);
1843 static int FUNC(metadata_scalability)(CodedBitstreamContext *ctx, RWContext *rw,
1844 AV1RawMetadataScalability *current)
1848 fb(8, scalability_mode_idc);
1850 if (current->scalability_mode_idc == AV1_SCALABILITY_SS)
1851 CHECK(FUNC(scalability_structure)(ctx, rw, current));
1856 static int FUNC(metadata_itut_t35)(CodedBitstreamContext *ctx, RWContext *rw,
1857 AV1RawMetadataITUTT35 *current)
1862 fb(8, itu_t_t35_country_code);
1863 if (current->itu_t_t35_country_code == 0xff)
1864 fb(8, itu_t_t35_country_code_extension_byte);
1867 // The payload runs up to the start of the trailing bits, but there might
1868 // be arbitrarily many trailing zeroes so we need to read through twice.
1869 current->payload_size = cbs_av1_get_payload_bytes_left(rw);
1871 current->payload_ref = av_buffer_alloc(current->payload_size);
1872 if (!current->payload_ref)
1873 return AVERROR(ENOMEM);
1874 current->payload = current->payload_ref->data;
1877 for (i = 0; i < current->payload_size; i++)
1878 xf(8, itu_t_t35_payload_bytes[i], current->payload[i],
1884 static int FUNC(metadata_timecode)(CodedBitstreamContext *ctx, RWContext *rw,
1885 AV1RawMetadataTimecode *current)
1889 fb(5, counting_type);
1890 flag(full_timestamp_flag);
1891 flag(discontinuity_flag);
1892 flag(cnt_dropped_flag);
1895 if (current->full_timestamp_flag) {
1896 fc(6, seconds_value, 0, 59);
1897 fc(6, minutes_value, 0, 59);
1898 fc(5, hours_value, 0, 23);
1901 if (current->seconds_flag) {
1902 fc(6, seconds_value, 0, 59);
1904 if (current->minutes_flag) {
1905 fc(6, minutes_value, 0, 59);
1907 if (current->hours_flag)
1908 fc(5, hours_value, 0, 23);
1913 fb(5, time_offset_length);
1914 if (current->time_offset_length > 0)
1915 fb(current->time_offset_length, time_offset_value);
1917 infer(time_offset_length, 0);
1922 static int FUNC(metadata_obu)(CodedBitstreamContext *ctx, RWContext *rw,
1923 AV1RawMetadata *current)
1927 leb128(metadata_type);
1929 switch (current->metadata_type) {
1930 case AV1_METADATA_TYPE_HDR_CLL:
1931 CHECK(FUNC(metadata_hdr_cll)(ctx, rw, ¤t->metadata.hdr_cll));
1933 case AV1_METADATA_TYPE_HDR_MDCV:
1934 CHECK(FUNC(metadata_hdr_mdcv)(ctx, rw, ¤t->metadata.hdr_mdcv));
1936 case AV1_METADATA_TYPE_SCALABILITY:
1937 CHECK(FUNC(metadata_scalability)(ctx, rw, ¤t->metadata.scalability));
1939 case AV1_METADATA_TYPE_ITUT_T35:
1940 CHECK(FUNC(metadata_itut_t35)(ctx, rw, ¤t->metadata.itut_t35));
1942 case AV1_METADATA_TYPE_TIMECODE:
1943 CHECK(FUNC(metadata_timecode)(ctx, rw, ¤t->metadata.timecode));
1946 // Unknown metadata type.
1947 return AVERROR_PATCHWELCOME;
1953 static int FUNC(padding_obu)(CodedBitstreamContext *ctx, RWContext *rw,
1954 AV1RawPadding *current)
1961 // The payload runs up to the start of the trailing bits, but there might
1962 // be arbitrarily many trailing zeroes so we need to read through twice.
1963 current->payload_size = cbs_av1_get_payload_bytes_left(rw);
1965 current->payload_ref = av_buffer_alloc(current->payload_size);
1966 if (!current->payload_ref)
1967 return AVERROR(ENOMEM);
1968 current->payload = current->payload_ref->data;
1971 for (i = 0; i < current->payload_size; i++)
1972 xf(8, obu_padding_byte[i], current->payload[i], 0x00, 0xff, 1, i);