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)
26 fc(1, obu_forbidden_bit, 0, 0);
28 fc(4, obu_type, 0, AV1_OBU_PADDING);
29 flag(obu_extension_flag);
30 flag(obu_has_size_field);
32 fc(1, obu_reserved_1bit, 0, 0);
34 if (current->obu_extension_flag) {
37 fc(3, extension_header_reserved_3bits, 0, 0);
43 static int FUNC(trailing_bits)(CodedBitstreamContext *ctx, RWContext *rw, int nb_bits)
47 av_assert0(nb_bits > 0);
49 fixed(1, trailing_one_bit, 1);
53 fixed(1, trailing_zero_bit, 0);
60 static int FUNC(byte_alignment)(CodedBitstreamContext *ctx, RWContext *rw)
64 while (byte_alignment(rw) != 0)
65 fixed(1, zero_bit, 0);
70 static int FUNC(color_config)(CodedBitstreamContext *ctx, RWContext *rw,
71 AV1RawColorConfig *current, int seq_profile)
73 CodedBitstreamAV1Context *priv = ctx->priv_data;
78 if (seq_profile == FF_PROFILE_AV1_PROFESSIONAL &&
79 current->high_bitdepth) {
81 priv->bit_depth = current->twelve_bit ? 12 : 10;
83 priv->bit_depth = current->high_bitdepth ? 10 : 8;
86 if (seq_profile == FF_PROFILE_AV1_HIGH)
87 infer(mono_chrome, 0);
90 priv->num_planes = current->mono_chrome ? 1 : 3;
92 flag(color_description_present_flag);
93 if (current->color_description_present_flag) {
94 fb(8, color_primaries);
95 fb(8, transfer_characteristics);
96 fb(8, matrix_coefficients);
98 infer(color_primaries, AVCOL_PRI_UNSPECIFIED);
99 infer(transfer_characteristics, AVCOL_TRC_UNSPECIFIED);
100 infer(matrix_coefficients, AVCOL_SPC_UNSPECIFIED);
103 if (current->mono_chrome) {
106 infer(subsampling_x, 1);
107 infer(subsampling_y, 1);
108 infer(chroma_sample_position, AV1_CSP_UNKNOWN);
109 infer(separate_uv_delta_q, 0);
111 } else if (current->color_primaries == AVCOL_PRI_BT709 &&
112 current->transfer_characteristics == AVCOL_TRC_IEC61966_2_1 &&
113 current->matrix_coefficients == AVCOL_SPC_RGB) {
114 infer(color_range, 1);
115 infer(subsampling_x, 0);
116 infer(subsampling_y, 0);
117 flag(separate_uv_delta_q);
122 if (seq_profile == FF_PROFILE_AV1_MAIN) {
123 infer(subsampling_x, 1);
124 infer(subsampling_y, 1);
125 } else if (seq_profile == FF_PROFILE_AV1_HIGH) {
126 infer(subsampling_x, 0);
127 infer(subsampling_y, 0);
129 if (priv->bit_depth == 12) {
130 fb(1, subsampling_x);
131 if (current->subsampling_x)
132 fb(1, subsampling_y);
134 infer(subsampling_y, 0);
136 infer(subsampling_x, 1);
137 infer(subsampling_y, 0);
140 if (current->subsampling_x && current->subsampling_y) {
141 fc(2, chroma_sample_position, AV1_CSP_UNKNOWN,
145 flag(separate_uv_delta_q);
151 static int FUNC(timing_info)(CodedBitstreamContext *ctx, RWContext *rw,
152 AV1RawTimingInfo *current)
156 fc(32, num_units_in_display_tick, 1, MAX_UINT_BITS(32));
157 fc(32, time_scale, 1, MAX_UINT_BITS(32));
159 flag(equal_picture_interval);
160 if (current->equal_picture_interval)
161 uvlc(num_ticks_per_picture_minus_1, 0, MAX_UINT_BITS(32) - 1);
166 static int FUNC(decoder_model_info)(CodedBitstreamContext *ctx, RWContext *rw,
167 AV1RawDecoderModelInfo *current)
171 fb(5, buffer_delay_length_minus_1);
172 fb(32, num_units_in_decoding_tick);
173 fb(5, buffer_removal_time_length_minus_1);
174 fb(5, frame_presentation_time_length_minus_1);
179 static int FUNC(sequence_header_obu)(CodedBitstreamContext *ctx, RWContext *rw,
180 AV1RawSequenceHeader *current)
184 HEADER("Sequence Header");
186 fc(3, seq_profile, FF_PROFILE_AV1_MAIN,
187 FF_PROFILE_AV1_PROFESSIONAL);
189 flag(reduced_still_picture_header);
191 if (current->reduced_still_picture_header) {
192 infer(timing_info_present_flag, 0);
193 infer(decoder_model_info_present_flag, 0);
194 infer(initial_display_delay_present_flag, 0);
195 infer(operating_points_cnt_minus_1, 0);
196 infer(operating_point_idc[0], 0);
198 fb(5, seq_level_idx[0]);
200 infer(seq_tier[0], 0);
201 infer(decoder_model_present_for_this_op[0], 0);
202 infer(initial_display_delay_present_for_this_op[0], 0);
205 flag(timing_info_present_flag);
206 if (current->timing_info_present_flag) {
207 CHECK(FUNC(timing_info)(ctx, rw, ¤t->timing_info));
209 flag(decoder_model_info_present_flag);
210 if (current->decoder_model_info_present_flag) {
211 CHECK(FUNC(decoder_model_info)
212 (ctx, rw, ¤t->decoder_model_info));
215 infer(decoder_model_info_present_flag, 0);
218 flag(initial_display_delay_present_flag);
220 fb(5, operating_points_cnt_minus_1);
221 for (i = 0; i <= current->operating_points_cnt_minus_1; i++) {
222 fbs(12, operating_point_idc[i], 1, i);
223 fbs(5, seq_level_idx[i], 1, i);
225 if (current->seq_level_idx[i] > 7)
226 flags(seq_tier[i], 1, i);
228 infer(seq_tier[i], 0);
230 if (current->decoder_model_info_present_flag) {
231 flags(decoder_model_present_for_this_op[i], 1, i);
232 if (current->decoder_model_present_for_this_op[i]) {
233 int n = current->decoder_model_info.buffer_delay_length_minus_1 + 1;
234 fbs(n, decoder_buffer_delay[i], 1, i);
235 fbs(n, encoder_buffer_delay[i], 1, i);
236 flags(low_delay_mode_flag[i], 1, i);
239 infer(decoder_model_present_for_this_op[i], 0);
242 if (current->initial_display_delay_present_flag) {
243 flags(initial_display_delay_present_for_this_op[i], 1, i);
244 if (current->initial_display_delay_present_for_this_op[i])
245 fbs(4, initial_display_delay_minus_1[i], 1, i);
250 fb(4, frame_width_bits_minus_1);
251 fb(4, frame_height_bits_minus_1);
253 fb(current->frame_width_bits_minus_1 + 1, max_frame_width_minus_1);
254 fb(current->frame_height_bits_minus_1 + 1, max_frame_height_minus_1);
256 if (current->reduced_still_picture_header)
257 infer(frame_id_numbers_present_flag, 0);
259 flag(frame_id_numbers_present_flag);
260 if (current->frame_id_numbers_present_flag) {
261 fb(4, delta_frame_id_length_minus_2);
262 fb(3, additional_frame_id_length_minus_1);
265 flag(use_128x128_superblock);
266 flag(enable_filter_intra);
267 flag(enable_intra_edge_filter);
269 if (current->reduced_still_picture_header) {
270 infer(enable_interintra_compound, 0);
271 infer(enable_masked_compound, 0);
272 infer(enable_warped_motion, 0);
273 infer(enable_dual_filter, 0);
274 infer(enable_order_hint, 0);
275 infer(enable_jnt_comp, 0);
276 infer(enable_ref_frame_mvs, 0);
278 infer(seq_force_screen_content_tools,
279 AV1_SELECT_SCREEN_CONTENT_TOOLS);
280 infer(seq_force_integer_mv,
281 AV1_SELECT_INTEGER_MV);
283 flag(enable_interintra_compound);
284 flag(enable_masked_compound);
285 flag(enable_warped_motion);
286 flag(enable_dual_filter);
288 flag(enable_order_hint);
289 if (current->enable_order_hint) {
290 flag(enable_jnt_comp);
291 flag(enable_ref_frame_mvs);
293 infer(enable_jnt_comp, 0);
294 infer(enable_ref_frame_mvs, 0);
297 flag(seq_choose_screen_content_tools);
298 if (current->seq_choose_screen_content_tools)
299 infer(seq_force_screen_content_tools,
300 AV1_SELECT_SCREEN_CONTENT_TOOLS);
302 fb(1, seq_force_screen_content_tools);
303 if (current->seq_force_screen_content_tools > 0) {
304 flag(seq_choose_integer_mv);
305 if (current->seq_choose_integer_mv)
306 infer(seq_force_integer_mv,
307 AV1_SELECT_INTEGER_MV);
309 fb(1, seq_force_integer_mv);
311 infer(seq_force_integer_mv, AV1_SELECT_INTEGER_MV);
314 if (current->enable_order_hint)
315 fb(3, order_hint_bits_minus_1);
318 flag(enable_superres);
320 flag(enable_restoration);
322 CHECK(FUNC(color_config)(ctx, rw, ¤t->color_config,
323 current->seq_profile));
325 flag(film_grain_params_present);
330 static int FUNC(temporal_delimiter_obu)(CodedBitstreamContext *ctx, RWContext *rw)
332 CodedBitstreamAV1Context *priv = ctx->priv_data;
334 HEADER("Temporal Delimiter");
336 priv->seen_frame_header = 0;
341 static int FUNC(set_frame_refs)(CodedBitstreamContext *ctx, RWContext *rw,
342 AV1RawFrameHeader *current)
344 CodedBitstreamAV1Context *priv = ctx->priv_data;
345 const AV1RawSequenceHeader *seq = priv->sequence_header;
346 static const uint8_t ref_frame_list[AV1_NUM_REF_FRAMES - 2] = {
347 AV1_REF_FRAME_LAST2, AV1_REF_FRAME_LAST3, AV1_REF_FRAME_BWDREF,
348 AV1_REF_FRAME_ALTREF2, AV1_REF_FRAME_ALTREF
350 int8_t ref_frame_idx[AV1_REFS_PER_FRAME], used_frame[AV1_NUM_REF_FRAMES];
351 int8_t shifted_order_hints[AV1_NUM_REF_FRAMES];
352 int cur_frame_hint, latest_order_hint, earliest_order_hint, ref;
355 for (i = 0; i < AV1_REFS_PER_FRAME; i++)
356 ref_frame_idx[i] = -1;
357 ref_frame_idx[AV1_REF_FRAME_LAST - AV1_REF_FRAME_LAST] = current->last_frame_idx;
358 ref_frame_idx[AV1_REF_FRAME_GOLDEN - AV1_REF_FRAME_LAST] = current->golden_frame_idx;
360 for (i = 0; i < AV1_NUM_REF_FRAMES; i++)
362 used_frame[current->last_frame_idx] = 1;
363 used_frame[current->golden_frame_idx] = 1;
365 cur_frame_hint = 1 << (seq->order_hint_bits_minus_1);
366 for (i = 0; i < AV1_NUM_REF_FRAMES; i++)
367 shifted_order_hints[i] = cur_frame_hint +
368 cbs_av1_get_relative_dist(seq, priv->ref[i].order_hint,
369 current->order_hint);
371 latest_order_hint = shifted_order_hints[current->last_frame_idx];
372 earliest_order_hint = shifted_order_hints[current->golden_frame_idx];
375 for (i = 0; i < AV1_NUM_REF_FRAMES; i++) {
376 int hint = shifted_order_hints[i];
377 if (!used_frame[i] && hint >= cur_frame_hint &&
378 (ref < 0 || hint >= latest_order_hint)) {
380 latest_order_hint = hint;
384 ref_frame_idx[AV1_REF_FRAME_ALTREF - AV1_REF_FRAME_LAST] = ref;
389 for (i = 0; i < AV1_NUM_REF_FRAMES; i++) {
390 int hint = shifted_order_hints[i];
391 if (!used_frame[i] && hint >= cur_frame_hint &&
392 (ref < 0 || hint < earliest_order_hint)) {
394 earliest_order_hint = hint;
398 ref_frame_idx[AV1_REF_FRAME_BWDREF - AV1_REF_FRAME_LAST] = ref;
403 for (i = 0; i < AV1_NUM_REF_FRAMES; i++) {
404 int hint = shifted_order_hints[i];
405 if (!used_frame[i] && hint >= cur_frame_hint &&
406 (ref < 0 || hint < earliest_order_hint)) {
408 earliest_order_hint = hint;
412 ref_frame_idx[AV1_REF_FRAME_ALTREF2 - AV1_REF_FRAME_LAST] = ref;
416 for (i = 0; i < AV1_REFS_PER_FRAME - 2; i++) {
417 int ref_frame = ref_frame_list[i];
418 if (ref_frame_idx[ref_frame - AV1_REF_FRAME_LAST] < 0 ) {
420 for (j = 0; j < AV1_NUM_REF_FRAMES; j++) {
421 int hint = shifted_order_hints[j];
422 if (!used_frame[j] && hint < cur_frame_hint &&
423 (ref < 0 || hint >= latest_order_hint)) {
425 latest_order_hint = hint;
429 ref_frame_idx[ref_frame - AV1_REF_FRAME_LAST] = ref;
436 for (i = 0; i < AV1_NUM_REF_FRAMES; i++) {
437 int hint = shifted_order_hints[i];
438 if (ref < 0 || hint < earliest_order_hint) {
440 earliest_order_hint = hint;
443 for (i = 0; i < AV1_REFS_PER_FRAME; i++) {
444 if (ref_frame_idx[i] < 0)
445 ref_frame_idx[i] = ref;
446 infer(ref_frame_idx[i], ref_frame_idx[i]);
452 static int FUNC(superres_params)(CodedBitstreamContext *ctx, RWContext *rw,
453 AV1RawFrameHeader *current)
455 CodedBitstreamAV1Context *priv = ctx->priv_data;
456 const AV1RawSequenceHeader *seq = priv->sequence_header;
459 if (seq->enable_superres)
462 infer(use_superres, 0);
464 if (current->use_superres) {
466 denom = current->coded_denom + AV1_SUPERRES_DENOM_MIN;
468 denom = AV1_SUPERRES_NUM;
471 priv->upscaled_width = priv->frame_width;
472 priv->frame_width = (priv->upscaled_width * AV1_SUPERRES_NUM +
478 static int FUNC(frame_size)(CodedBitstreamContext *ctx, RWContext *rw,
479 AV1RawFrameHeader *current)
481 CodedBitstreamAV1Context *priv = ctx->priv_data;
482 const AV1RawSequenceHeader *seq = priv->sequence_header;
485 if (current->frame_size_override_flag) {
486 fb(seq->frame_width_bits_minus_1 + 1, frame_width_minus_1);
487 fb(seq->frame_height_bits_minus_1 + 1, frame_height_minus_1);
489 priv->frame_width = current->frame_width_minus_1 + 1;
490 priv->frame_height = current->frame_height_minus_1 + 1;
492 priv->frame_width = seq->max_frame_width_minus_1 + 1;
493 priv->frame_height = seq->max_frame_height_minus_1 + 1;
496 CHECK(FUNC(superres_params)(ctx, rw, current));
501 static int FUNC(render_size)(CodedBitstreamContext *ctx, RWContext *rw,
502 AV1RawFrameHeader *current)
504 CodedBitstreamAV1Context *priv = ctx->priv_data;
507 flag(render_and_frame_size_different);
509 if (current->render_and_frame_size_different) {
510 fb(16, render_width_minus_1);
511 fb(16, render_height_minus_1);
513 priv->render_width = current->render_width_minus_1 + 1;
514 priv->render_height = current->render_height_minus_1 + 1;
516 priv->render_width = priv->upscaled_width;
517 priv->render_height = priv->frame_height;
523 static int FUNC(frame_size_with_refs)(CodedBitstreamContext *ctx, RWContext *rw,
524 AV1RawFrameHeader *current)
526 CodedBitstreamAV1Context *priv = ctx->priv_data;
529 for (i = 0; i < AV1_REFS_PER_FRAME; i++) {
530 flags(found_ref[i], 1, i);
531 if (current->found_ref[i]) {
532 AV1ReferenceFrameState *ref =
533 &priv->ref[current->ref_frame_idx[i]];
536 av_log(ctx->log_ctx, AV_LOG_ERROR,
537 "Missing reference frame needed for frame size "
538 "(ref = %d, ref_frame_idx = %d).\n",
539 i, current->ref_frame_idx[i]);
540 return AVERROR_INVALIDDATA;
543 priv->upscaled_width = ref->upscaled_width;
544 priv->frame_width = ref->frame_width;
545 priv->frame_height = ref->frame_height;
546 priv->render_width = ref->render_width;
547 priv->render_height = ref->render_height;
552 if (i >= AV1_REFS_PER_FRAME) {
553 CHECK(FUNC(frame_size)(ctx, rw, current));
554 CHECK(FUNC(render_size)(ctx, rw, current));
556 CHECK(FUNC(superres_params)(ctx, rw, current));
562 static int FUNC(interpolation_filter)(CodedBitstreamContext *ctx, RWContext *rw,
563 AV1RawFrameHeader *current)
567 flag(is_filter_switchable);
568 if (current->is_filter_switchable)
569 infer(interpolation_filter,
570 AV1_INTERPOLATION_FILTER_SWITCHABLE);
572 fb(2, interpolation_filter);
577 static int FUNC(tile_info)(CodedBitstreamContext *ctx, RWContext *rw,
578 AV1RawFrameHeader *current)
580 CodedBitstreamAV1Context *priv = ctx->priv_data;
581 const AV1RawSequenceHeader *seq = priv->sequence_header;
582 int mi_cols, mi_rows, sb_cols, sb_rows, sb_shift, sb_size;
583 int max_tile_width_sb, max_tile_height_sb, max_tile_area_sb;
584 int min_log2_tile_cols, max_log2_tile_cols, max_log2_tile_rows;
585 int min_log2_tiles, min_log2_tile_rows;
588 mi_cols = 2 * ((priv->frame_width + 7) >> 3);
589 mi_rows = 2 * ((priv->frame_height + 7) >> 3);
591 sb_cols = seq->use_128x128_superblock ? ((mi_cols + 31) >> 5)
592 : ((mi_cols + 15) >> 4);
593 sb_rows = seq->use_128x128_superblock ? ((mi_rows + 31) >> 5)
594 : ((mi_rows + 15) >> 4);
596 sb_shift = seq->use_128x128_superblock ? 5 : 4;
597 sb_size = sb_shift + 2;
599 max_tile_width_sb = AV1_MAX_TILE_WIDTH >> sb_size;
600 max_tile_area_sb = AV1_MAX_TILE_AREA >> (2 * sb_size);
602 min_log2_tile_cols = cbs_av1_tile_log2(max_tile_width_sb, sb_cols);
603 max_log2_tile_cols = cbs_av1_tile_log2(1, FFMIN(sb_cols, AV1_MAX_TILE_COLS));
604 max_log2_tile_rows = cbs_av1_tile_log2(1, FFMIN(sb_rows, AV1_MAX_TILE_ROWS));
605 min_log2_tiles = FFMAX(min_log2_tile_cols,
606 cbs_av1_tile_log2(max_tile_area_sb, sb_rows * sb_cols));
608 flag(uniform_tile_spacing_flag);
610 if (current->uniform_tile_spacing_flag) {
611 int tile_width_sb, tile_height_sb;
613 increment(tile_cols_log2, min_log2_tile_cols, max_log2_tile_cols);
615 tile_width_sb = (sb_cols + (1 << current->tile_cols_log2) - 1) >>
616 current->tile_cols_log2;
617 current->tile_cols = (sb_cols + tile_width_sb - 1) / tile_width_sb;
619 min_log2_tile_rows = FFMAX(min_log2_tiles - current->tile_cols_log2, 0);
621 increment(tile_rows_log2, min_log2_tile_rows, max_log2_tile_rows);
623 tile_height_sb = (sb_rows + (1 << current->tile_rows_log2) - 1) >>
624 current->tile_rows_log2;
625 current->tile_rows = (sb_rows + tile_height_sb - 1) / tile_height_sb;
628 int widest_tile_sb, start_sb, size_sb, max_width, max_height;
633 for (i = 0; start_sb < sb_cols && i < AV1_MAX_TILE_COLS; i++) {
634 max_width = FFMIN(sb_cols - start_sb, max_tile_width_sb);
635 ns(max_width, width_in_sbs_minus_1[i], 1, i);
636 size_sb = current->width_in_sbs_minus_1[i] + 1;
637 widest_tile_sb = FFMAX(size_sb, widest_tile_sb);
640 current->tile_cols_log2 = cbs_av1_tile_log2(1, i);
641 current->tile_cols = i;
643 if (min_log2_tiles > 0)
644 max_tile_area_sb = (sb_rows * sb_cols) >> (min_log2_tiles + 1);
646 max_tile_area_sb = sb_rows * sb_cols;
647 max_tile_height_sb = FFMAX(max_tile_area_sb / widest_tile_sb, 1);
650 for (i = 0; start_sb < sb_rows && i < AV1_MAX_TILE_ROWS; i++) {
651 max_height = FFMIN(sb_rows - start_sb, max_tile_height_sb);
652 ns(max_height, height_in_sbs_minus_1[i], 1, i);
653 size_sb = current->height_in_sbs_minus_1[i] + 1;
656 current->tile_rows_log2 = cbs_av1_tile_log2(1, i);
657 current->tile_rows = i;
660 if (current->tile_cols_log2 > 0 ||
661 current->tile_rows_log2 > 0) {
662 fb(current->tile_cols_log2 + current->tile_rows_log2,
663 context_update_tile_id);
664 fb(2, tile_size_bytes_minus1);
666 infer(context_update_tile_id, 0);
669 priv->tile_cols = current->tile_cols;
670 priv->tile_rows = current->tile_rows;
675 static int FUNC(quantization_params)(CodedBitstreamContext *ctx, RWContext *rw,
676 AV1RawFrameHeader *current)
678 CodedBitstreamAV1Context *priv = ctx->priv_data;
679 const AV1RawSequenceHeader *seq = priv->sequence_header;
684 delta_q(delta_q_y_dc);
686 if (priv->num_planes > 1) {
687 if (seq->color_config.separate_uv_delta_q)
690 infer(diff_uv_delta, 0);
692 delta_q(delta_q_u_dc);
693 delta_q(delta_q_u_ac);
695 if (current->diff_uv_delta) {
696 delta_q(delta_q_v_dc);
697 delta_q(delta_q_v_ac);
699 infer(delta_q_v_dc, current->delta_q_u_dc);
700 infer(delta_q_v_ac, current->delta_q_u_ac);
703 infer(delta_q_u_dc, 0);
704 infer(delta_q_u_ac, 0);
705 infer(delta_q_v_dc, 0);
706 infer(delta_q_v_ac, 0);
710 if (current->using_qmatrix) {
713 if (seq->color_config.separate_uv_delta_q)
716 infer(qm_v, current->qm_u);
722 static int FUNC(segmentation_params)(CodedBitstreamContext *ctx, RWContext *rw,
723 AV1RawFrameHeader *current)
725 static const uint8_t bits[AV1_SEG_LVL_MAX] = { 8, 6, 6, 6, 6, 3, 0, 0 };
726 static const uint8_t sign[AV1_SEG_LVL_MAX] = { 1, 1, 1, 1, 1, 0, 0, 0 };
729 flag(segmentation_enabled);
731 if (current->segmentation_enabled) {
732 if (current->primary_ref_frame == AV1_PRIMARY_REF_NONE) {
733 infer(segmentation_update_map, 1);
734 infer(segmentation_temporal_update, 0);
735 infer(segmentation_update_data, 1);
737 flag(segmentation_update_map);
738 if (current->segmentation_update_map)
739 flag(segmentation_temporal_update);
741 infer(segmentation_temporal_update, 0);
742 flag(segmentation_update_data);
745 if (current->segmentation_update_data) {
746 for (i = 0; i < AV1_MAX_SEGMENTS; i++) {
747 for (j = 0; j < AV1_SEG_LVL_MAX; j++) {
748 flags(feature_enabled[i][j], 2, i, j);
750 if (current->feature_enabled[i][j] && bits[j] > 0) {
752 sus(1 + bits[j], feature_value[i][j], 2, i, j);
754 fbs(bits[j], feature_value[i][j], 2, i, j);
756 infer(feature_value[i][j], 0);
762 for (i = 0; i < AV1_MAX_SEGMENTS; i++) {
763 for (j = 0; j < AV1_SEG_LVL_MAX; j++) {
764 infer(feature_enabled[i][j], 0);
765 infer(feature_value[i][j], 0);
773 static int FUNC(delta_q_params)(CodedBitstreamContext *ctx, RWContext *rw,
774 AV1RawFrameHeader *current)
778 if (current->base_q_idx > 0)
779 flag(delta_q_present);
781 infer(delta_q_present, 0);
783 if (current->delta_q_present)
789 static int FUNC(delta_lf_params)(CodedBitstreamContext *ctx, RWContext *rw,
790 AV1RawFrameHeader *current)
794 if (current->delta_q_present) {
795 if (!current->allow_intrabc)
796 flag(delta_lf_present);
798 infer(delta_lf_present, 0);
799 if (current->delta_lf_present) {
801 flag(delta_lf_multi);
803 infer(delta_lf_res, 0);
804 infer(delta_lf_multi, 0);
807 infer(delta_lf_present, 0);
808 infer(delta_lf_res, 0);
809 infer(delta_lf_multi, 0);
815 static int FUNC(loop_filter_params)(CodedBitstreamContext *ctx, RWContext *rw,
816 AV1RawFrameHeader *current)
818 CodedBitstreamAV1Context *priv = ctx->priv_data;
821 if (priv->coded_lossless || current->allow_intrabc) {
822 infer(loop_filter_level[0], 0);
823 infer(loop_filter_level[1], 0);
824 infer(loop_filter_ref_deltas[AV1_REF_FRAME_INTRA], 1);
825 infer(loop_filter_ref_deltas[AV1_REF_FRAME_LAST], 0);
826 infer(loop_filter_ref_deltas[AV1_REF_FRAME_LAST2], 0);
827 infer(loop_filter_ref_deltas[AV1_REF_FRAME_LAST3], 0);
828 infer(loop_filter_ref_deltas[AV1_REF_FRAME_BWDREF], 0);
829 infer(loop_filter_ref_deltas[AV1_REF_FRAME_GOLDEN], -1);
830 infer(loop_filter_ref_deltas[AV1_REF_FRAME_ALTREF], -1);
831 infer(loop_filter_ref_deltas[AV1_REF_FRAME_ALTREF2], -1);
832 for (i = 0; i < 2; i++)
833 infer(loop_filter_mode_deltas[i], 0);
837 fb(6, loop_filter_level[0]);
838 fb(6, loop_filter_level[1]);
840 if (priv->num_planes > 1) {
841 if (current->loop_filter_level[0] ||
842 current->loop_filter_level[1]) {
843 fb(6, loop_filter_level[2]);
844 fb(6, loop_filter_level[3]);
848 fb(3, loop_filter_sharpness);
850 flag(loop_filter_delta_enabled);
851 if (current->loop_filter_delta_enabled) {
852 flag(loop_filter_delta_update);
853 if (current->loop_filter_delta_update) {
854 for (i = 0; i < AV1_TOTAL_REFS_PER_FRAME; i++) {
855 flags(update_ref_delta[i], 1, i);
856 if (current->update_ref_delta[i])
857 sus(1 + 6, loop_filter_ref_deltas[i], 1, i);
859 for (i = 0; i < 2; i++) {
860 flags(update_mode_delta[i], 1, i);
861 if (current->update_mode_delta[i])
862 sus(1 + 6, loop_filter_mode_deltas[i], 1, i);
870 static int FUNC(cdef_params)(CodedBitstreamContext *ctx, RWContext *rw,
871 AV1RawFrameHeader *current)
873 CodedBitstreamAV1Context *priv = ctx->priv_data;
874 const AV1RawSequenceHeader *seq = priv->sequence_header;
877 if (priv->coded_lossless || current->allow_intrabc ||
879 infer(cdef_damping_minus_3, 0);
881 infer(cdef_y_pri_strength[0], 0);
882 infer(cdef_y_sec_strength[0], 0);
883 infer(cdef_uv_pri_strength[0], 0);
884 infer(cdef_uv_sec_strength[0], 0);
889 fb(2, cdef_damping_minus_3);
892 for (i = 0; i < (1 << current->cdef_bits); i++) {
893 fbs(4, cdef_y_pri_strength[i], 1, i);
894 fbs(2, cdef_y_sec_strength[i], 1, i);
896 if (priv->num_planes > 1) {
897 fbs(4, cdef_uv_pri_strength[i], 1, i);
898 fbs(2, cdef_uv_sec_strength[i], 1, i);
905 static int FUNC(lr_params)(CodedBitstreamContext *ctx, RWContext *rw,
906 AV1RawFrameHeader *current)
908 CodedBitstreamAV1Context *priv = ctx->priv_data;
909 const AV1RawSequenceHeader *seq = priv->sequence_header;
910 int uses_lr, uses_chroma_lr;
913 if (priv->all_lossless || current->allow_intrabc ||
914 !seq->enable_restoration) {
918 uses_lr = uses_chroma_lr = 0;
919 for (i = 0; i < priv->num_planes; i++) {
920 fbs(2, lr_type[i], 1, i);
922 if (current->lr_type[i] != 0) {
930 if (seq->use_128x128_superblock)
931 increment(lr_unit_shift, 1, 2);
933 increment(lr_unit_shift, 0, 2);
935 if(seq->color_config.subsampling_x &&
936 seq->color_config.subsampling_y && uses_chroma_lr) {
939 infer(lr_uv_shift, 0);
946 static int FUNC(read_tx_mode)(CodedBitstreamContext *ctx, RWContext *rw,
947 AV1RawFrameHeader *current)
949 CodedBitstreamAV1Context *priv = ctx->priv_data;
952 if (priv->coded_lossless)
955 increment(tx_mode, 1, 2);
960 static int FUNC(frame_reference_mode)(CodedBitstreamContext *ctx, RWContext *rw,
961 AV1RawFrameHeader *current)
965 if (current->frame_type == AV1_FRAME_INTRA_ONLY ||
966 current->frame_type == AV1_FRAME_KEY)
967 infer(reference_select, 0);
969 flag(reference_select);
974 static int FUNC(skip_mode_params)(CodedBitstreamContext *ctx, RWContext *rw,
975 AV1RawFrameHeader *current)
977 CodedBitstreamAV1Context *priv = ctx->priv_data;
978 const AV1RawSequenceHeader *seq = priv->sequence_header;
979 int skip_mode_allowed;
982 if (current->frame_type == AV1_FRAME_KEY ||
983 current->frame_type == AV1_FRAME_INTRA_ONLY ||
984 !current->reference_select || !seq->enable_order_hint) {
985 skip_mode_allowed = 0;
987 int forward_idx, backward_idx;
988 int forward_hint, backward_hint;
989 int ref_hint, dist, i;
993 for (i = 0; i < AV1_REFS_PER_FRAME; i++) {
994 ref_hint = priv->ref[current->ref_frame_idx[i]].order_hint;
995 dist = cbs_av1_get_relative_dist(seq, ref_hint,
996 current->order_hint);
998 if (forward_idx < 0 ||
999 cbs_av1_get_relative_dist(seq, ref_hint,
1000 forward_hint) > 0) {
1002 forward_hint = ref_hint;
1004 } else if (dist > 0) {
1005 if (backward_idx < 0 ||
1006 cbs_av1_get_relative_dist(seq, ref_hint,
1007 backward_hint) < 0) {
1009 backward_hint = ref_hint;
1014 if (forward_idx < 0) {
1015 skip_mode_allowed = 0;
1016 } else if (backward_idx >= 0) {
1017 skip_mode_allowed = 1;
1018 // Frames for skip mode are forward_idx and backward_idx.
1020 int second_forward_idx;
1021 int second_forward_hint;
1023 second_forward_idx = -1;
1024 for (i = 0; i < AV1_REFS_PER_FRAME; i++) {
1025 ref_hint = priv->ref[current->ref_frame_idx[i]].order_hint;
1026 if (cbs_av1_get_relative_dist(seq, ref_hint,
1027 forward_hint) < 0) {
1028 if (second_forward_idx < 0 ||
1029 cbs_av1_get_relative_dist(seq, ref_hint,
1030 second_forward_hint) > 0) {
1031 second_forward_idx = i;
1032 second_forward_hint = ref_hint;
1037 if (second_forward_idx < 0) {
1038 skip_mode_allowed = 0;
1040 skip_mode_allowed = 1;
1041 // Frames for skip mode are forward_idx and second_forward_idx.
1046 if (skip_mode_allowed)
1047 flag(skip_mode_present);
1049 infer(skip_mode_present, 0);
1054 static int FUNC(global_motion_param)(CodedBitstreamContext *ctx, RWContext *rw,
1055 AV1RawFrameHeader *current,
1056 int type, int ref, int idx)
1058 uint32_t abs_bits, prec_bits, num_syms;
1062 if (type == AV1_WARP_MODEL_TRANSLATION) {
1063 abs_bits = AV1_GM_ABS_TRANS_ONLY_BITS - !current->allow_high_precision_mv;
1064 prec_bits = AV1_GM_TRANS_ONLY_PREC_BITS - !current->allow_high_precision_mv;
1066 abs_bits = AV1_GM_ABS_TRANS_BITS;
1067 prec_bits = AV1_GM_TRANS_PREC_BITS;
1070 abs_bits = AV1_GM_ABS_ALPHA_BITS;
1071 prec_bits = AV1_GM_ALPHA_PREC_BITS;
1074 num_syms = 2 * (1 << abs_bits) + 1;
1075 subexp(gm_params[ref][idx], num_syms, 2, ref, idx);
1077 // Actual gm_params value is not reconstructed here.
1083 static int FUNC(global_motion_params)(CodedBitstreamContext *ctx, RWContext *rw,
1084 AV1RawFrameHeader *current)
1089 if (current->frame_type == AV1_FRAME_KEY ||
1090 current->frame_type == AV1_FRAME_INTRA_ONLY)
1093 for (ref = AV1_REF_FRAME_LAST; ref <= AV1_REF_FRAME_ALTREF; ref++) {
1094 flags(is_global[ref], 1, ref);
1095 if (current->is_global[ref]) {
1096 flags(is_rot_zoom[ref], 1, ref);
1097 if (current->is_rot_zoom[ref]) {
1098 type = AV1_WARP_MODEL_ROTZOOM;
1100 flags(is_translation[ref], 1, ref);
1101 type = current->is_translation[ref] ? AV1_WARP_MODEL_TRANSLATION
1102 : AV1_WARP_MODEL_AFFINE;
1105 type = AV1_WARP_MODEL_IDENTITY;
1108 if (type >= AV1_WARP_MODEL_ROTZOOM) {
1109 CHECK(FUNC(global_motion_param)(ctx, rw, current, type, ref, 2));
1110 CHECK(FUNC(global_motion_param)(ctx, rw, current, type, ref, 3));
1111 if (type == AV1_WARP_MODEL_AFFINE) {
1112 CHECK(FUNC(global_motion_param)(ctx, rw, current, type, ref, 4));
1113 CHECK(FUNC(global_motion_param)(ctx, rw, current, type, ref, 5));
1115 // gm_params[ref][4] = -gm_params[ref][3]
1116 // gm_params[ref][5] = gm_params[ref][2]
1119 if (type >= AV1_WARP_MODEL_TRANSLATION) {
1120 CHECK(FUNC(global_motion_param)(ctx, rw, current, type, ref, 0));
1121 CHECK(FUNC(global_motion_param)(ctx, rw, current, type, ref, 1));
1128 static int FUNC(film_grain_params)(CodedBitstreamContext *ctx, RWContext *rw,
1129 AV1RawFrameHeader *current)
1131 CodedBitstreamAV1Context *priv = ctx->priv_data;
1132 const AV1RawSequenceHeader *seq = priv->sequence_header;
1133 int num_pos_luma, num_pos_chroma;
1136 if (!seq->film_grain_params_present ||
1137 (!current->show_frame && !current->showable_frame))
1142 if (!current->apply_grain)
1147 if (current->frame_type == AV1_FRAME_INTER)
1150 infer(update_grain, 1);
1152 if (!current->update_grain) {
1153 fb(3, film_grain_params_ref_idx);
1157 fc(4, num_y_points, 0, 14);
1158 for (i = 0; i < current->num_y_points; i++) {
1159 fcs(8, point_y_value[i],
1160 i ? current->point_y_value[i - 1] + 1 : 0,
1161 MAX_UINT_BITS(8) - (current->num_y_points - i - 1),
1163 fbs(8, point_y_scaling[i], 1, i);
1166 if (seq->color_config.mono_chrome)
1167 infer(chroma_scaling_from_luma, 0);
1169 flag(chroma_scaling_from_luma);
1171 if (seq->color_config.mono_chrome ||
1172 current->chroma_scaling_from_luma ||
1173 (seq->color_config.subsampling_x == 1 &&
1174 seq->color_config.subsampling_y == 1 &&
1175 current->num_y_points == 0)) {
1176 infer(num_cb_points, 0);
1177 infer(num_cr_points, 0);
1179 fc(4, num_cb_points, 0, 10);
1180 for (i = 0; i < current->num_cb_points; i++) {
1181 fcs(8, point_cb_value[i],
1182 i ? current->point_cb_value[i - 1] + 1 : 0,
1183 MAX_UINT_BITS(8) - (current->num_cb_points - i - 1),
1185 fbs(8, point_cb_scaling[i], 1, i);
1187 fc(4, num_cr_points, 0, 10);
1188 for (i = 0; i < current->num_cr_points; i++) {
1189 fcs(8, point_cr_value[i],
1190 i ? current->point_cr_value[i - 1] + 1 : 0,
1191 MAX_UINT_BITS(8) - (current->num_cr_points - i - 1),
1193 fbs(8, point_cr_scaling[i], 1, i);
1197 fb(2, grain_scaling_minus_8);
1198 fb(2, ar_coeff_lag);
1199 num_pos_luma = 2 * current->ar_coeff_lag * (current->ar_coeff_lag + 1);
1200 if (current->num_y_points) {
1201 num_pos_chroma = num_pos_luma + 1;
1202 for (i = 0; i < num_pos_luma; i++)
1203 fbs(8, ar_coeffs_y_plus_128[i], 1, i);
1205 num_pos_chroma = num_pos_luma;
1207 if (current->chroma_scaling_from_luma || current->num_cb_points) {
1208 for (i = 0; i < num_pos_chroma; i++)
1209 fbs(8, ar_coeffs_cb_plus_128[i], 1, i);
1211 if (current->chroma_scaling_from_luma || current->num_cr_points) {
1212 for (i = 0; i < num_pos_chroma; i++)
1213 fbs(8, ar_coeffs_cr_plus_128[i], 1, i);
1215 fb(2, ar_coeff_shift_minus_6);
1216 fb(2, grain_scale_shift);
1217 if (current->num_cb_points) {
1219 fb(8, cb_luma_mult);
1222 if (current->num_cr_points) {
1224 fb(8, cr_luma_mult);
1229 flag(clip_to_restricted_range);
1234 static int FUNC(uncompressed_header)(CodedBitstreamContext *ctx, RWContext *rw,
1235 AV1RawFrameHeader *current)
1237 CodedBitstreamAV1Context *priv = ctx->priv_data;
1238 const AV1RawSequenceHeader *seq;
1239 int id_len, diff_len, all_frames, frame_is_intra, order_hint_bits;
1242 if (!priv->sequence_header) {
1243 av_log(ctx->log_ctx, AV_LOG_ERROR, "No sequence header available: "
1244 "unable to decode frame header.\n");
1245 return AVERROR_INVALIDDATA;
1247 seq = priv->sequence_header;
1249 id_len = seq->additional_frame_id_length_minus_1 +
1250 seq->delta_frame_id_length_minus_2 + 3;
1251 all_frames = (1 << AV1_NUM_REF_FRAMES) - 1;
1253 if (seq->reduced_still_picture_header) {
1254 infer(show_existing_frame, 0);
1255 infer(frame_type, AV1_FRAME_KEY);
1256 infer(show_frame, 1);
1257 infer(showable_frame, 0);
1261 flag(show_existing_frame);
1263 if (current->show_existing_frame) {
1264 AV1ReferenceFrameState *frame;
1266 fb(3, frame_to_show_map_idx);
1267 frame = &priv->ref[current->frame_to_show_map_idx];
1269 if (seq->decoder_model_info_present_flag &&
1270 !seq->timing_info.equal_picture_interval) {
1271 fb(seq->decoder_model_info.frame_presentation_time_length_minus_1 + 1,
1272 frame_presentation_time);
1275 if (seq->frame_id_numbers_present_flag)
1276 fb(id_len, display_frame_id);
1278 if (frame->frame_type == AV1_FRAME_KEY)
1279 infer(refresh_frame_flags, all_frames);
1281 infer(refresh_frame_flags, 0);
1287 frame_is_intra = (current->frame_type == AV1_FRAME_INTRA_ONLY ||
1288 current->frame_type == AV1_FRAME_KEY);
1291 if (current->show_frame &&
1292 seq->decoder_model_info_present_flag &&
1293 !seq->timing_info.equal_picture_interval) {
1294 fb(seq->decoder_model_info.frame_presentation_time_length_minus_1 + 1,
1295 frame_presentation_time);
1297 if (current->show_frame)
1298 infer(showable_frame, current->frame_type != AV1_FRAME_KEY);
1300 flag(showable_frame);
1302 if (current->frame_type == AV1_FRAME_SWITCH ||
1303 (current->frame_type == AV1_FRAME_KEY && current->show_frame))
1304 infer(error_resilient_mode, 1);
1306 flag(error_resilient_mode);
1309 if (current->frame_type == AV1_FRAME_KEY && current->show_frame) {
1310 for (i = 0; i < AV1_NUM_REF_FRAMES; i++) {
1311 priv->ref[i].valid = 0;
1312 priv->ref[i].order_hint = 0;
1316 flag(disable_cdf_update);
1318 if (seq->seq_force_screen_content_tools ==
1319 AV1_SELECT_SCREEN_CONTENT_TOOLS) {
1320 flag(allow_screen_content_tools);
1322 infer(allow_screen_content_tools,
1323 seq->seq_force_screen_content_tools);
1325 if (current->allow_screen_content_tools) {
1326 if (seq->seq_force_integer_mv == AV1_SELECT_INTEGER_MV)
1327 flag(force_integer_mv);
1329 infer(force_integer_mv, seq->seq_force_integer_mv);
1331 infer(force_integer_mv, 0);
1334 if (seq->frame_id_numbers_present_flag) {
1335 fb(id_len, current_frame_id);
1337 diff_len = seq->delta_frame_id_length_minus_2 + 2;
1338 for (i = 0; i < AV1_NUM_REF_FRAMES; i++) {
1339 if (current->current_frame_id > (1 << diff_len)) {
1340 if (priv->ref[i].frame_id > current->current_frame_id ||
1341 priv->ref[i].frame_id < (current->current_frame_id -
1343 priv->ref[i].valid = 0;
1345 if (priv->ref[i].frame_id > current->current_frame_id &&
1346 priv->ref[i].frame_id < ((1 << id_len) +
1347 current->current_frame_id -
1349 priv->ref[i].valid = 0;
1353 infer(current_frame_id, 0);
1356 if (current->frame_type == AV1_FRAME_SWITCH)
1357 infer(frame_size_override_flag, 1);
1358 else if(seq->reduced_still_picture_header)
1359 infer(frame_size_override_flag, 0);
1361 flag(frame_size_override_flag);
1364 seq->enable_order_hint ? seq->order_hint_bits_minus_1 + 1 : 0;
1365 if (order_hint_bits > 0)
1366 fb(order_hint_bits, order_hint);
1368 infer(order_hint, 0);
1370 if (frame_is_intra || current->error_resilient_mode)
1371 infer(primary_ref_frame, AV1_PRIMARY_REF_NONE);
1373 fb(3, primary_ref_frame);
1375 if (seq->decoder_model_info_present_flag) {
1376 flag(buffer_removal_time_present_flag);
1377 if (current->buffer_removal_time_present_flag) {
1378 for (i = 0; i <= seq->operating_points_cnt_minus_1; i++) {
1379 if (seq->decoder_model_present_for_this_op[i]) {
1380 int op_pt_idc = seq->operating_point_idc[i];
1381 int in_temporal_layer = (op_pt_idc >> priv->temporal_id ) & 1;
1382 int in_spatial_layer = (op_pt_idc >> (priv->spatial_id + 8)) & 1;
1383 if (seq->operating_point_idc[i] == 0 ||
1384 in_temporal_layer || in_spatial_layer) {
1385 fbs(seq->decoder_model_info.buffer_removal_time_length_minus_1 + 1,
1386 buffer_removal_time[i], 1, i);
1393 if (current->frame_type == AV1_FRAME_SWITCH ||
1394 (current->frame_type == AV1_FRAME_KEY && current->show_frame))
1395 infer(refresh_frame_flags, all_frames);
1397 fb(8, refresh_frame_flags);
1399 if (!frame_is_intra || current->refresh_frame_flags != all_frames) {
1400 if (current->error_resilient_mode && seq->enable_order_hint) {
1401 for (i = 0; i < AV1_NUM_REF_FRAMES; i++) {
1402 fbs(order_hint_bits, ref_order_hint[i], 1, i);
1403 if (current->ref_order_hint[i] != priv->ref[i].order_hint)
1404 priv->ref[i].valid = 0;
1409 if (current->frame_type == AV1_FRAME_KEY ||
1410 current->frame_type == AV1_FRAME_INTRA_ONLY) {
1411 CHECK(FUNC(frame_size)(ctx, rw, current));
1412 CHECK(FUNC(render_size)(ctx, rw, current));
1414 if (current->allow_screen_content_tools &&
1415 priv->upscaled_width == priv->frame_width)
1416 flag(allow_intrabc);
1418 infer(allow_intrabc, 0);
1421 if (!seq->enable_order_hint) {
1422 infer(frame_refs_short_signaling, 0);
1424 flag(frame_refs_short_signaling);
1425 if (current->frame_refs_short_signaling) {
1426 fb(3, last_frame_idx);
1427 fb(3, golden_frame_idx);
1428 CHECK(FUNC(set_frame_refs)(ctx, rw, current));
1432 for (i = 0; i < AV1_REFS_PER_FRAME; i++) {
1433 if (!current->frame_refs_short_signaling)
1434 fbs(3, ref_frame_idx[i], 1, i);
1435 if (seq->frame_id_numbers_present_flag) {
1436 fbs(seq->delta_frame_id_length_minus_2 + 2,
1437 delta_frame_id_minus1[i], 1, i);
1441 if (current->frame_size_override_flag &&
1442 !current->error_resilient_mode) {
1443 CHECK(FUNC(frame_size_with_refs)(ctx, rw, current));
1445 CHECK(FUNC(frame_size)(ctx, rw, current));
1446 CHECK(FUNC(render_size)(ctx, rw, current));
1449 if (current->force_integer_mv)
1450 infer(allow_high_precision_mv, 0);
1452 flag(allow_high_precision_mv);
1454 CHECK(FUNC(interpolation_filter)(ctx, rw, current));
1456 flag(is_motion_mode_switchable);
1458 if (current->error_resilient_mode ||
1459 !seq->enable_ref_frame_mvs)
1460 infer(use_ref_frame_mvs, 0);
1462 flag(use_ref_frame_mvs);
1464 infer(allow_intrabc, 0);
1467 if (!frame_is_intra) {
1468 // Derive reference frame sign biases.
1471 if (seq->reduced_still_picture_header || current->disable_cdf_update)
1472 infer(disable_frame_end_update_cdf, 1);
1474 flag(disable_frame_end_update_cdf);
1476 if (current->primary_ref_frame == AV1_PRIMARY_REF_NONE) {
1477 // Init non-coeff CDFs.
1478 // Setup past independence.
1480 // Load CDF tables from previous frame.
1481 // Load params from previous frame.
1484 if (current->use_ref_frame_mvs) {
1485 // Perform motion field estimation process.
1488 CHECK(FUNC(tile_info)(ctx, rw, current));
1490 CHECK(FUNC(quantization_params)(ctx, rw, current));
1492 CHECK(FUNC(segmentation_params)(ctx, rw, current));
1494 CHECK(FUNC(delta_q_params)(ctx, rw, current));
1496 CHECK(FUNC(delta_lf_params)(ctx, rw, current));
1498 // Init coeff CDFs / load previous segments.
1500 priv->coded_lossless = 1;
1501 for (i = 0; i < AV1_MAX_SEGMENTS; i++) {
1503 if (current->feature_enabled[i][AV1_SEG_LVL_ALT_Q]) {
1504 qindex = (current->base_q_idx +
1505 current->feature_value[i][AV1_SEG_LVL_ALT_Q]);
1507 qindex = current->base_q_idx;
1509 qindex = av_clip_uintp2(qindex, 8);
1511 if (qindex || current->delta_q_y_dc ||
1512 current->delta_q_u_ac || current->delta_q_u_dc ||
1513 current->delta_q_v_ac || current->delta_q_v_dc) {
1514 priv->coded_lossless = 0;
1517 priv->all_lossless = priv->coded_lossless &&
1518 priv->frame_width == priv->upscaled_width;
1520 CHECK(FUNC(loop_filter_params)(ctx, rw, current));
1522 CHECK(FUNC(cdef_params)(ctx, rw, current));
1524 CHECK(FUNC(lr_params)(ctx, rw, current));
1526 CHECK(FUNC(read_tx_mode)(ctx, rw, current));
1528 CHECK(FUNC(frame_reference_mode)(ctx, rw, current));
1530 CHECK(FUNC(skip_mode_params)(ctx, rw, current));
1532 if (frame_is_intra || current->error_resilient_mode ||
1533 !seq->enable_warped_motion)
1534 infer(allow_warped_motion, 0);
1536 flag(allow_warped_motion);
1538 flag(reduced_tx_set);
1540 CHECK(FUNC(global_motion_params)(ctx, rw, current));
1542 CHECK(FUNC(film_grain_params)(ctx, rw, current));
1544 for (i = 0; i < AV1_NUM_REF_FRAMES; i++) {
1545 if (current->refresh_frame_flags & (1 << i)) {
1546 priv->ref[i] = (AV1ReferenceFrameState) {
1548 .frame_id = current->current_frame_id,
1549 .upscaled_width = priv->upscaled_width,
1550 .frame_width = priv->frame_width,
1551 .frame_height = priv->frame_height,
1552 .render_width = priv->render_width,
1553 .render_height = priv->render_height,
1554 .frame_type = current->frame_type,
1555 .subsampling_x = seq->color_config.subsampling_x,
1556 .subsampling_y = seq->color_config.subsampling_y,
1557 .bit_depth = priv->bit_depth,
1558 .order_hint = current->order_hint,
1563 av_log(ctx->log_ctx, AV_LOG_DEBUG, "Frame %d: size %dx%d "
1564 "upscaled %d render %dx%d subsample %dx%d "
1565 "bitdepth %d tiles %dx%d.\n", current->order_hint,
1566 priv->frame_width, priv->frame_height, priv->upscaled_width,
1567 priv->render_width, priv->render_height,
1568 seq->color_config.subsampling_x + 1,
1569 seq->color_config.subsampling_y + 1, priv->bit_depth,
1570 priv->tile_rows, priv->tile_cols);
1575 static int FUNC(frame_header_obu)(CodedBitstreamContext *ctx, RWContext *rw,
1576 AV1RawFrameHeader *current, int redundant,
1577 AVBufferRef *rw_buffer_ref)
1579 CodedBitstreamAV1Context *priv = ctx->priv_data;
1580 int start_pos, fh_bits, fh_bytes, err;
1583 if (priv->seen_frame_header) {
1585 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid repeated "
1586 "frame header OBU.\n");
1587 return AVERROR_INVALIDDATA;
1593 HEADER("Redundant Frame Header");
1595 av_assert0(priv->frame_header_ref && priv->frame_header);
1597 init_get_bits(&fh, priv->frame_header,
1598 priv->frame_header_size);
1599 for (i = 0; i < priv->frame_header_size; i += 8) {
1600 b = FFMIN(priv->frame_header_size - i, 8);
1601 val = get_bits(&fh, b);
1602 xf(b, frame_header_copy[i],
1603 val, val, val, 1, i / 8);
1608 HEADER("Redundant Frame Header (used as Frame Header)");
1610 HEADER("Frame Header");
1613 start_pos = get_bits_count(rw);
1615 start_pos = put_bits_count(rw);
1618 CHECK(FUNC(uncompressed_header)(ctx, rw, current));
1620 if (current->show_existing_frame) {
1621 priv->seen_frame_header = 0;
1623 priv->seen_frame_header = 1;
1625 av_buffer_unref(&priv->frame_header_ref);
1628 fh_bits = get_bits_count(rw) - start_pos;
1629 fh_start = (uint8_t*)rw->buffer + start_pos / 8;
1631 // Need to flush the bitwriter so that we can copy its output,
1632 // but use a copy so we don't affect the caller's structure.
1634 PutBitContext tmp = *rw;
1635 flush_put_bits(&tmp);
1638 fh_bits = put_bits_count(rw) - start_pos;
1639 fh_start = rw->buf + start_pos / 8;
1641 fh_bytes = (fh_bits + 7) / 8;
1643 priv->frame_header_size = fh_bits;
1645 if (rw_buffer_ref) {
1646 priv->frame_header_ref = av_buffer_ref(rw_buffer_ref);
1647 if (!priv->frame_header_ref)
1648 return AVERROR(ENOMEM);
1649 priv->frame_header = fh_start;
1651 priv->frame_header_ref =
1652 av_buffer_alloc(fh_bytes + AV_INPUT_BUFFER_PADDING_SIZE);
1653 if (!priv->frame_header_ref)
1654 return AVERROR(ENOMEM);
1655 priv->frame_header = priv->frame_header_ref->data;
1656 memcpy(priv->frame_header, fh_start, fh_bytes);
1664 static int FUNC(tile_group_obu)(CodedBitstreamContext *ctx, RWContext *rw,
1665 AV1RawTileGroup *current)
1667 CodedBitstreamAV1Context *priv = ctx->priv_data;
1668 int num_tiles, tile_bits;
1671 HEADER("Tile Group");
1673 num_tiles = priv->tile_cols * priv->tile_rows;
1675 flag(tile_start_and_end_present_flag);
1677 infer(tile_start_and_end_present_flag, 0);
1679 if (num_tiles == 1 || !current->tile_start_and_end_present_flag) {
1681 infer(tg_end, num_tiles - 1);
1683 tile_bits = cbs_av1_tile_log2(1, priv->tile_cols) +
1684 cbs_av1_tile_log2(1, priv->tile_rows);
1685 fb(tile_bits, tg_start);
1686 fb(tile_bits, tg_end);
1689 CHECK(FUNC(byte_alignment)(ctx, rw));
1691 // Reset header for next frame.
1692 if (current->tg_end == num_tiles - 1)
1693 priv->seen_frame_header = 0;
1695 // Tile data follows.
1700 static int FUNC(frame_obu)(CodedBitstreamContext *ctx, RWContext *rw,
1701 AV1RawFrame *current,
1702 AVBufferRef *rw_buffer_ref)
1706 CHECK(FUNC(frame_header_obu)(ctx, rw, ¤t->header,
1709 CHECK(FUNC(byte_alignment)(ctx, rw));
1711 CHECK(FUNC(tile_group_obu)(ctx, rw, ¤t->tile_group));
1716 static int FUNC(tile_list_obu)(CodedBitstreamContext *ctx, RWContext *rw,
1717 AV1RawTileList *current)
1721 fb(8, output_frame_width_in_tiles_minus_1);
1722 fb(8, output_frame_height_in_tiles_minus_1);
1724 fb(16, tile_count_minus_1);
1726 // Tile data follows.
1731 static int FUNC(metadata_hdr_cll)(CodedBitstreamContext *ctx, RWContext *rw,
1732 AV1RawMetadataHDRCLL *current)
1742 static int FUNC(metadata_hdr_mdcv)(CodedBitstreamContext *ctx, RWContext *rw,
1743 AV1RawMetadataHDRMDCV *current)
1747 for (i = 0; i < 3; i++) {
1748 fbs(16, primary_chromaticity_x[i], 1, i);
1749 fbs(16, primary_chromaticity_y[i], 1, i);
1752 fb(16, white_point_chromaticity_x);
1753 fb(16, white_point_chromaticity_y);
1755 fc(32, luminance_max, 1, MAX_UINT_BITS(32));
1756 // luminance_min must be lower than luminance_max. Convert luminance_max from
1757 // 24.8 fixed point to 18.14 fixed point in order to compare them.
1758 fc(32, luminance_min, 0, FFMIN(((uint64_t)current->luminance_max << 6) - 1,
1759 MAX_UINT_BITS(32)));
1764 static int FUNC(scalability_structure)(CodedBitstreamContext *ctx, RWContext *rw,
1765 AV1RawMetadataScalability *current)
1767 CodedBitstreamAV1Context *priv = ctx->priv_data;
1768 const AV1RawSequenceHeader *seq;
1771 if (!priv->sequence_header) {
1772 av_log(ctx->log_ctx, AV_LOG_ERROR, "No sequence header available: "
1773 "unable to parse scalability metadata.\n");
1774 return AVERROR_INVALIDDATA;
1776 seq = priv->sequence_header;
1778 fb(2, spatial_layers_cnt_minus_1);
1779 flag(spatial_layer_dimensions_present_flag);
1780 flag(spatial_layer_description_present_flag);
1781 flag(temporal_group_description_present_flag);
1782 fc(3, scalability_structure_reserved_3bits, 0, 0);
1783 if (current->spatial_layer_dimensions_present_flag) {
1784 for (i = 0; i <= current->spatial_layers_cnt_minus_1; i++) {
1785 fcs(16, spatial_layer_max_width[i],
1786 0, seq->max_frame_width_minus_1 + 1, 1, i);
1787 fcs(16, spatial_layer_max_height[i],
1788 0, seq->max_frame_height_minus_1 + 1, 1, i);
1791 if (current->spatial_layer_description_present_flag) {
1792 for (i = 0; i <= current->spatial_layers_cnt_minus_1; i++)
1793 fbs(8, spatial_layer_ref_id[i], 1, i);
1795 if (current->temporal_group_description_present_flag) {
1796 fb(8, temporal_group_size);
1797 for (i = 0; i < current->temporal_group_size; i++) {
1798 fbs(3, temporal_group_temporal_id[i], 1, i);
1799 flags(temporal_group_temporal_switching_up_point_flag[i], 1, i);
1800 flags(temporal_group_spatial_switching_up_point_flag[i], 1, i);
1801 fbs(3, temporal_group_ref_cnt[i], 1, i);
1802 for (j = 0; j < current->temporal_group_ref_cnt[i]; j++) {
1803 fbs(8, temporal_group_ref_pic_diff[i][j], 2, i, j);
1811 static int FUNC(metadata_scalability)(CodedBitstreamContext *ctx, RWContext *rw,
1812 AV1RawMetadataScalability *current)
1816 fb(8, scalability_mode_idc);
1818 if (current->scalability_mode_idc == AV1_SCALABILITY_SS)
1819 CHECK(FUNC(scalability_structure)(ctx, rw, current));
1824 static int FUNC(metadata_itut_t35)(CodedBitstreamContext *ctx, RWContext *rw,
1825 AV1RawMetadataITUTT35 *current)
1830 fb(8, itu_t_t35_country_code);
1831 if (current->itu_t_t35_country_code == 0xff)
1832 fb(8, itu_t_t35_country_code_extension_byte);
1835 // The payload runs up to the start of the trailing bits, but there might
1836 // be arbitrarily many trailing zeroes so we need to read through twice.
1837 current->payload_size = cbs_av1_get_payload_bytes_left(rw);
1839 current->payload_ref = av_buffer_alloc(current->payload_size);
1840 if (!current->payload_ref)
1841 return AVERROR(ENOMEM);
1842 current->payload = current->payload_ref->data;
1845 for (i = 0; i < current->payload_size; i++)
1846 xf(8, itu_t_t35_payload_bytes[i], current->payload[i],
1852 static int FUNC(metadata_timecode)(CodedBitstreamContext *ctx, RWContext *rw,
1853 AV1RawMetadataTimecode *current)
1857 fb(5, counting_type);
1858 flag(full_timestamp_flag);
1859 flag(discontinuity_flag);
1860 flag(cnt_dropped_flag);
1863 if (current->full_timestamp_flag) {
1864 fc(6, seconds_value, 0, 59);
1865 fc(6, minutes_value, 0, 59);
1866 fc(5, hours_value, 0, 23);
1869 if (current->seconds_flag) {
1870 fc(6, seconds_value, 0, 59);
1872 if (current->minutes_flag) {
1873 fc(6, minutes_value, 0, 59);
1875 if (current->hours_flag)
1876 fc(5, hours_value, 0, 23);
1881 fb(5, time_offset_length);
1882 if (current->time_offset_length > 0)
1883 fb(current->time_offset_length, time_offset_value);
1885 infer(time_offset_length, 0);
1890 static int FUNC(metadata_obu)(CodedBitstreamContext *ctx, RWContext *rw,
1891 AV1RawMetadata *current)
1895 leb128(metadata_type);
1897 switch (current->metadata_type) {
1898 case AV1_METADATA_TYPE_HDR_CLL:
1899 CHECK(FUNC(metadata_hdr_cll)(ctx, rw, ¤t->metadata.hdr_cll));
1901 case AV1_METADATA_TYPE_HDR_MDCV:
1902 CHECK(FUNC(metadata_hdr_mdcv)(ctx, rw, ¤t->metadata.hdr_mdcv));
1904 case AV1_METADATA_TYPE_SCALABILITY:
1905 CHECK(FUNC(metadata_scalability)(ctx, rw, ¤t->metadata.scalability));
1907 case AV1_METADATA_TYPE_ITUT_T35:
1908 CHECK(FUNC(metadata_itut_t35)(ctx, rw, ¤t->metadata.itut_t35));
1910 case AV1_METADATA_TYPE_TIMECODE:
1911 CHECK(FUNC(metadata_timecode)(ctx, rw, ¤t->metadata.timecode));
1914 // Unknown metadata type.
1915 return AVERROR_PATCHWELCOME;
1921 static int FUNC(padding_obu)(CodedBitstreamContext *ctx, RWContext *rw,
1922 AV1RawPadding *current)
1929 // The payload runs up to the start of the trailing bits, but there might
1930 // be arbitrarily many trailing zeroes so we need to read through twice.
1931 current->payload_size = cbs_av1_get_payload_bytes_left(rw);
1933 current->payload_ref = av_buffer_alloc(current->payload_size);
1934 if (!current->payload_ref)
1935 return AVERROR(ENOMEM);
1936 current->payload = current->payload_ref->data;
1939 for (i = 0; i < current->payload_size; i++)
1940 xf(8, obu_padding_byte[i], current->payload[i], 0x00, 0xff, 1, i);