2 * AV1 helper functions for muxers
3 * Copyright (c) 2018 James Almer <jamrial@gmail.com>
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "libavutil/mem.h"
23 #include "libavcodec/av1.h"
24 #include "libavcodec/av1_parse.h"
25 #include "libavcodec/profiles.h"
26 #include "libavcodec/put_bits.h"
30 int ff_av1_filter_obus(AVIOContext *pb, const uint8_t *buf, int size)
32 const uint8_t *end = buf + size;
34 int start_pos, type, temporal_id, spatial_id;
38 int len = parse_obu_header(buf, end - buf, &obu_size, &start_pos,
39 &type, &temporal_id, &spatial_id);
44 case AV1_OBU_TEMPORAL_DELIMITER:
45 case AV1_OBU_REDUNDANT_FRAME_HEADER:
46 case AV1_OBU_TILE_LIST:
50 avio_write(pb, buf, len);
60 int ff_av1_filter_obus_buf(const uint8_t *buf, uint8_t **out, int *size)
65 ret = avio_open_dyn_buf(&pb);
69 ret = ff_av1_filter_obus(pb, buf, *size);
74 *size = avio_close_dyn_buf(pb, out);
79 static inline void uvlc(GetBitContext *gb)
81 int leading_zeros = 0;
83 while (get_bits_left(gb)) {
89 if (leading_zeros >= 32)
92 skip_bits_long(gb, leading_zeros);
95 static int parse_color_config(AV1SequenceParameters *seq_params, GetBitContext *gb)
98 int high_bitdepth = get_bits1(gb);
99 if (seq_params->profile == FF_PROFILE_AV1_PROFESSIONAL && high_bitdepth)
100 twelve_bit = get_bits1(gb);
102 seq_params->bitdepth = 8 + (high_bitdepth * 2) + (twelve_bit * 2);
104 if (seq_params->profile == FF_PROFILE_AV1_HIGH)
105 seq_params->monochrome = 0;
107 seq_params->monochrome = get_bits1(gb);
109 seq_params->color_description_present_flag = get_bits1(gb);
110 if (seq_params->color_description_present_flag) {
111 seq_params->color_primaries = get_bits(gb, 8);
112 seq_params->transfer_characteristics = get_bits(gb, 8);
113 seq_params->matrix_coefficients = get_bits(gb, 8);
115 seq_params->color_primaries = AVCOL_PRI_UNSPECIFIED;
116 seq_params->transfer_characteristics = AVCOL_TRC_UNSPECIFIED;
117 seq_params->matrix_coefficients = AVCOL_SPC_UNSPECIFIED;
120 if (seq_params->monochrome) {
121 seq_params->color_range = get_bits1(gb);
122 seq_params->chroma_subsampling_x = 1;
123 seq_params->chroma_subsampling_y = 1;
124 seq_params->chroma_sample_position = 0;
126 } else if (seq_params->color_primaries == AVCOL_PRI_BT709 &&
127 seq_params->transfer_characteristics == AVCOL_TRC_IEC61966_2_1 &&
128 seq_params->matrix_coefficients == AVCOL_SPC_RGB) {
129 seq_params->chroma_subsampling_x = 0;
130 seq_params->chroma_subsampling_y = 0;
132 seq_params->color_range = get_bits1(gb);
134 if (seq_params->profile == FF_PROFILE_AV1_MAIN) {
135 seq_params->chroma_subsampling_x = 1;
136 seq_params->chroma_subsampling_y = 1;
137 } else if (seq_params->profile == FF_PROFILE_AV1_HIGH) {
138 seq_params->chroma_subsampling_x = 0;
139 seq_params->chroma_subsampling_y = 0;
142 seq_params->chroma_subsampling_x = get_bits1(gb);
143 if (seq_params->chroma_subsampling_x)
144 seq_params->chroma_subsampling_y = get_bits1(gb);
146 seq_params->chroma_subsampling_y = 0;
148 seq_params->chroma_subsampling_x = 1;
149 seq_params->chroma_subsampling_y = 0;
152 if (seq_params->chroma_subsampling_x && seq_params->chroma_subsampling_y)
153 seq_params->chroma_sample_position = get_bits(gb, 2);
156 skip_bits1(gb); // separate_uv_delta_q
161 static int parse_sequence_header(AV1SequenceParameters *seq_params, const uint8_t *buf, int size)
164 int reduced_still_picture_header;
165 int frame_width_bits_minus_1, frame_height_bits_minus_1;
168 size_bits = get_obu_bit_length(buf, size, AV1_OBU_SEQUENCE_HEADER);
172 ret = init_get_bits(&gb, buf, size_bits);
176 memset(seq_params, 0, sizeof(*seq_params));
178 seq_params->profile = get_bits(&gb, 3);
180 skip_bits1(&gb); // still_picture
181 reduced_still_picture_header = get_bits1(&gb);
183 if (reduced_still_picture_header) {
184 seq_params->level = get_bits(&gb, 5);
185 seq_params->tier = 0;
187 int initial_display_delay_present_flag, operating_points_cnt_minus_1;
188 int decoder_model_info_present_flag, buffer_delay_length_minus_1;
190 if (get_bits1(&gb)) { // timing_info_present_flag
191 skip_bits_long(&gb, 32); // num_units_in_display_tick
192 skip_bits_long(&gb, 32); // time_scale
194 if (get_bits1(&gb)) // equal_picture_interval
195 uvlc(&gb); // num_ticks_per_picture_minus_1
197 decoder_model_info_present_flag = get_bits1(&gb);
198 if (decoder_model_info_present_flag) {
199 buffer_delay_length_minus_1 = get_bits(&gb, 5);
200 skip_bits_long(&gb, 32); // num_units_in_decoding_tick
201 skip_bits(&gb, 10); // buffer_removal_time_length_minus_1 (5)
202 // frame_presentation_time_length_minus_1 (5)
205 decoder_model_info_present_flag = 0;
207 initial_display_delay_present_flag = get_bits1(&gb);
209 operating_points_cnt_minus_1 = get_bits(&gb, 5);
210 for (int i = 0; i <= operating_points_cnt_minus_1; i++) {
211 int seq_level_idx, seq_tier;
213 skip_bits(&gb, 12); // operating_point_idc
214 seq_level_idx = get_bits(&gb, 5);
216 if (seq_level_idx > 7)
217 seq_tier = get_bits1(&gb);
221 if (decoder_model_info_present_flag) {
222 if (get_bits1(&gb)) { // decoder_model_present_for_this_op
223 skip_bits_long(&gb, buffer_delay_length_minus_1 + 1); // decoder_buffer_delay
224 skip_bits_long(&gb, buffer_delay_length_minus_1 + 1); // encoder_buffer_delay
225 skip_bits1(&gb); // low_delay_mode_flag
229 if (initial_display_delay_present_flag) {
230 if (get_bits1(&gb)) // initial_display_delay_present_for_this_op
231 skip_bits(&gb, 4); // initial_display_delay_minus_1
235 seq_params->level = seq_level_idx;
236 seq_params->tier = seq_tier;
241 frame_width_bits_minus_1 = get_bits(&gb, 4);
242 frame_height_bits_minus_1 = get_bits(&gb, 4);
244 skip_bits(&gb, frame_width_bits_minus_1 + 1); // max_frame_width_minus_1
245 skip_bits(&gb, frame_height_bits_minus_1 + 1); // max_frame_height_minus_1
247 if (!reduced_still_picture_header) {
248 if (get_bits1(&gb)) // frame_id_numbers_present_flag
249 skip_bits(&gb, 7); // delta_frame_id_length_minus_2 (4), additional_frame_id_length_minus_1 (3)
252 skip_bits(&gb, 3); // use_128x128_superblock (1), enable_filter_intra (1), enable_intra_edge_filter (1)
254 if (!reduced_still_picture_header) {
255 int enable_order_hint, seq_force_screen_content_tools;
257 skip_bits(&gb, 4); // enable_intraintra_compound (1), enable_masked_compound (1)
258 // enable_warped_motion (1), enable_dual_filter (1)
260 enable_order_hint = get_bits1(&gb);
261 if (enable_order_hint)
262 skip_bits(&gb, 2); // enable_jnt_comp (1), enable_ref_frame_mvs (1)
264 if (get_bits1(&gb)) // seq_choose_screen_content_tools
265 seq_force_screen_content_tools = 2;
267 seq_force_screen_content_tools = get_bits1(&gb);
269 if (seq_force_screen_content_tools) {
270 if (!get_bits1(&gb)) // seq_choose_integer_mv
271 skip_bits1(&gb); // seq_force_integer_mv
274 if (enable_order_hint)
275 skip_bits(&gb, 3); // order_hint_bits_minus_1
278 skip_bits(&gb, 3); // enable_superres (1), enable_cdef (1), enable_restoration (1)
280 parse_color_config(seq_params, &gb);
282 skip_bits1(&gb); // film_grain_params_present
284 if (get_bits_left(&gb))
285 return AVERROR_INVALIDDATA;
290 int ff_av1_parse_seq_header(AV1SequenceParameters *seq, const uint8_t *buf, int size)
293 int start_pos, type, temporal_id, spatial_id;
296 return AVERROR_INVALIDDATA;
299 int len = parse_obu_header(buf, size, &obu_size, &start_pos,
300 &type, &temporal_id, &spatial_id);
305 case AV1_OBU_SEQUENCE_HEADER:
307 return AVERROR_INVALIDDATA;
309 return parse_sequence_header(seq, buf + start_pos, obu_size);
317 return AVERROR_INVALIDDATA;
320 int ff_isom_write_av1c(AVIOContext *pb, const uint8_t *buf, int size)
322 AVIOContext *seq_pb = NULL, *meta_pb = NULL;
323 AV1SequenceParameters seq_params;
326 uint8_t *seq = NULL, *meta = NULL;
328 int start_pos, type, temporal_id, spatial_id;
329 int ret, nb_seq = 0, seq_size, meta_size;
332 return AVERROR_INVALIDDATA;
334 ret = avio_open_dyn_buf(&seq_pb);
337 ret = avio_open_dyn_buf(&meta_pb);
342 int len = parse_obu_header(buf, size, &obu_size, &start_pos,
343 &type, &temporal_id, &spatial_id);
350 case AV1_OBU_SEQUENCE_HEADER:
352 if (!obu_size || nb_seq > 1) {
353 ret = AVERROR_INVALIDDATA;
356 ret = parse_sequence_header(&seq_params, buf + start_pos, obu_size);
360 avio_write(seq_pb, buf, len);
362 case AV1_OBU_METADATA:
364 ret = AVERROR_INVALIDDATA;
367 avio_write(meta_pb, buf, len);
376 seq_size = avio_close_dyn_buf(seq_pb, &seq);
378 ret = AVERROR_INVALIDDATA;
382 init_put_bits(&pbc, header, sizeof(header));
384 put_bits(&pbc, 1, 1); // marker
385 put_bits(&pbc, 7, 1); // version
386 put_bits(&pbc, 3, seq_params.profile);
387 put_bits(&pbc, 5, seq_params.level);
388 put_bits(&pbc, 1, seq_params.tier);
389 put_bits(&pbc, 1, seq_params.bitdepth > 8);
390 put_bits(&pbc, 1, seq_params.bitdepth == 12);
391 put_bits(&pbc, 1, seq_params.monochrome);
392 put_bits(&pbc, 1, seq_params.chroma_subsampling_x);
393 put_bits(&pbc, 1, seq_params.chroma_subsampling_y);
394 put_bits(&pbc, 2, seq_params.chroma_sample_position);
395 put_bits(&pbc, 8, 0); // padding
396 flush_put_bits(&pbc);
398 avio_write(pb, header, sizeof(header));
399 avio_write(pb, seq, seq_size);
401 meta_size = avio_close_dyn_buf(meta_pb, &meta);
403 avio_write(pb, meta, meta_size);
407 avio_close_dyn_buf(seq_pb, &seq);
409 avio_close_dyn_buf(meta_pb, &meta);