1 /*****************************************************************************
2 * av1_obu.c: AV1 OBU parser
3 *****************************************************************************
4 * Copyright (C) 2018 VideoLabs, VLC authors and VideoLAN
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 *****************************************************************************/
24 #include <vlc_common.h>
30 #include "iso_color_tables.h"
34 typedef uint8_t obu_u1_t
;
35 typedef uint8_t obu_u2_t
;
36 typedef uint8_t obu_u3_t
;
37 typedef uint8_t obu_u4_t
;
38 typedef uint8_t obu_u5_t
;
39 typedef uint8_t obu_u6_t
;
40 typedef uint8_t obu_u7_t
;
41 typedef uint8_t obu_u8_t
;
42 typedef uint16_t obu_u12_t
;
43 typedef uint32_t obu_u32_t
;
44 typedef uint32_t obu_uvlc_t
;
46 #define SELECT_SCREEN_CONTENT_TOOLS 2
47 #define SELECT_INTEGER_MV 2
50 #define AV1_OPERATING_POINTS_COUNT 32
55 struct av1_header_info_s
62 static bool av1_read_header(bs_t
*p_bs
, struct av1_header_info_s
*p_hdr
)
66 p_hdr
->obu_type
= bs_read(p_bs
, 4);
67 const obu_u1_t obu_extension_flag
= bs_read1(p_bs
);
68 const obu_u1_t obu_has_size_field
= bs_read1(p_bs
);
71 if(obu_extension_flag
)
73 if(bs_remain(p_bs
) < 8)
75 p_hdr
->temporal_id
= bs_read(p_bs
, 3);
76 p_hdr
->spatial_id
= bs_read(p_bs
, 2);
79 if(obu_has_size_field
)
81 for (uint8_t i
= 0; i
< 8; i
++)
83 if(bs_remain(p_bs
) < 8)
85 uint8_t v
= bs_read(p_bs
, 8);
96 * Sequence sub sections readers
99 struct av1_timing_info_s
101 obu_u32_t num_units_in_display_tick
;
102 obu_u32_t time_scale
;
103 obu_u1_t equal_picture_interval
;
104 obu_uvlc_t num_ticks_per_picture_minus_1
;
107 static bool av1_parse_timing_info(bs_t
*p_bs
, struct av1_timing_info_s
*p_ti
)
109 p_ti
->num_units_in_display_tick
= bs_read(p_bs
, 32);
110 p_ti
->time_scale
= bs_read(p_bs
, 32);
111 p_ti
->equal_picture_interval
= bs_read1(p_bs
);
112 if(p_ti
->equal_picture_interval
)
113 p_ti
->num_ticks_per_picture_minus_1
= bs_read_ue(p_bs
);
117 struct av1_decoder_model_info_s
119 obu_u5_t buffer_delay_length_minus_1
;
120 obu_u32_t num_units_in_decoding_tick
;
121 obu_u5_t buffer_removal_time_length_minus_1
;
122 obu_u5_t frame_presentation_time_length_minus_1
;
125 static bool av1_parse_decoder_model_info(bs_t
*p_bs
, struct av1_decoder_model_info_s
*p_dm
)
127 p_dm
->buffer_delay_length_minus_1
= bs_read(p_bs
, 5);
128 p_dm
->num_units_in_decoding_tick
= bs_read(p_bs
, 32);
129 p_dm
->buffer_removal_time_length_minus_1
= bs_read(p_bs
, 5);
130 p_dm
->frame_presentation_time_length_minus_1
= bs_read(p_bs
, 5);
134 struct av1_operating_parameters_info_s
136 obu_u32_t decoder_buffer_delay
;
137 obu_u32_t encoder_buffer_delay
;
138 obu_u1_t low_delay_mode_flag
;
141 static bool av1_parse_operating_parameters_info(bs_t
*p_bs
,
142 struct av1_operating_parameters_info_s
*p_op
,
143 obu_u8_t buffer_delay_length_minus_1
)
145 p_op
->decoder_buffer_delay
= bs_read(p_bs
, 1 + buffer_delay_length_minus_1
);
146 p_op
->encoder_buffer_delay
= bs_read(p_bs
, 1 + buffer_delay_length_minus_1
);
147 p_op
->low_delay_mode_flag
= bs_read1(p_bs
);
151 struct av1_color_config_s
153 obu_u1_t high_bitdepth
;
155 obu_u1_t mono_chrome
;
156 obu_u1_t color_description_present_flag
;
157 obu_u8_t color_primaries
;
158 obu_u8_t transfer_characteristics
;
159 obu_u8_t matrix_coefficients
;
160 obu_u1_t color_range
;
161 obu_u1_t subsampling_x
;
162 obu_u1_t subsampling_y
;
163 obu_u2_t chroma_sample_position
;
164 obu_u1_t separate_uv_delta_q
;
167 static bool av1_parse_color_config(bs_t
*p_bs
,
168 struct av1_color_config_s
*p_cc
,
169 obu_u3_t seq_profile
)
171 p_cc
->high_bitdepth
= bs_read1(p_bs
);
174 if(p_cc
->high_bitdepth
)
175 p_cc
->twelve_bit
= bs_read1(p_bs
);
177 p_cc
->mono_chrome
= bs_read1(p_bs
);
179 const uint8_t BitDepth
= p_cc
->twelve_bit
? 12 : ((p_cc
->high_bitdepth
) ? 10 : 8);
181 p_cc
->color_description_present_flag
= bs_read1(p_bs
);
182 if(p_cc
->color_description_present_flag
)
184 p_cc
->color_primaries
= bs_read(p_bs
, 8);
185 p_cc
->transfer_characteristics
= bs_read(p_bs
, 8);
186 p_cc
->matrix_coefficients
= bs_read(p_bs
, 8);
190 p_cc
->color_primaries
= 2;
191 p_cc
->transfer_characteristics
= 2;
192 p_cc
->matrix_coefficients
= 2;
195 if(p_cc
->mono_chrome
)
197 p_cc
->color_range
= bs_read1(p_bs
) ? COLOR_RANGE_FULL
: COLOR_RANGE_LIMITED
;
199 else if( p_cc
->color_primaries
== 1 &&
200 p_cc
->transfer_characteristics
== 13 &&
201 p_cc
->matrix_coefficients
== 0 )
203 p_cc
->color_range
= COLOR_RANGE_FULL
;
207 p_cc
->color_range
= bs_read1(p_bs
) ? COLOR_RANGE_FULL
: COLOR_RANGE_LIMITED
;
212 p_cc
->subsampling_x
= bs_read1(p_bs
);
213 if(p_cc
->subsampling_x
)
214 p_cc
->subsampling_y
= bs_read1(p_bs
);
218 p_cc
->subsampling_x
= 1;
221 if(p_cc
->subsampling_x
&& p_cc
->subsampling_y
)
222 p_cc
->chroma_sample_position
= bs_read(p_bs
, 2);
225 p_cc
->separate_uv_delta_q
= bs_read1(p_bs
);
234 struct av1_OBU_sequence_header_t
236 struct av1_header_info_s obu_header
;
237 obu_u3_t seq_profile
;
238 obu_u1_t still_picture
;
239 obu_u1_t reduced_still_picture_header
;
240 obu_u1_t timing_info_present_flag
;
241 struct av1_timing_info_s timing_info
;
242 obu_u1_t decoder_model_info_present_flag
;
243 struct av1_decoder_model_info_s decoder_model_info
;
244 obu_u1_t initial_display_delay_present_flag
;
245 obu_u1_t operating_points_cnt_minus_1
;
248 obu_u12_t operating_point_idc
;
249 obu_u5_t seq_level_idx
;
251 obu_u1_t decoder_model_present_for_this_op
;
252 struct av1_operating_parameters_info_s operating_parameters_info
;
253 obu_u1_t initial_display_delay_present_for_this_op
;
254 obu_u4_t initial_display_delay_minus_1
;
255 } operating_points
[AV1_OPERATING_POINTS_COUNT
];
256 obu_u32_t max_frame_width_minus_1
;
257 obu_u32_t max_frame_height_minus_1
;
258 obu_u1_t frame_id_numbers_present_flag
;
259 obu_u4_t delta_frame_id_length_minus_2
;
260 obu_u3_t additional_frame_id_length_minus_1
;
261 obu_u1_t use_128x128_superblock
;
262 obu_u1_t enable_filter_intra
;
263 obu_u1_t enable_intra_edge_filter
;
265 obu_u1_t enable_interintra_compound
;
266 obu_u1_t enable_masked_compound
;
267 obu_u1_t enable_warped_motion
;
268 obu_u1_t enable_dual_filter
;
269 obu_u1_t enable_order_hint
;
270 obu_u1_t enable_jnt_comp
;
271 obu_u1_t enable_ref_frame_mvs
;
272 obu_u2_t seq_force_screen_content_tools
;
273 obu_u2_t seq_force_integer_mv
;
274 obu_u3_t order_hint_bits_minus_1
;
276 obu_u1_t enable_superres
;
277 obu_u1_t enable_cdef
;
278 obu_u1_t enable_restoration
;
279 struct av1_color_config_s color_config
;
280 obu_u1_t film_grain_params_present
;
283 void AV1_release_sequence_header(av1_OBU_sequence_header_t
*p_seq
)
288 av1_OBU_sequence_header_t
*
289 AV1_OBU_parse_sequence_header(const uint8_t *p_data
, size_t i_data
)
292 bs_init(&bs
, p_data
, i_data
);
294 av1_OBU_sequence_header_t
*p_seq
= calloc(1, sizeof(*p_seq
));
298 if(!av1_read_header(&bs
, &p_seq
->obu_header
))
300 AV1_release_sequence_header(p_seq
);
304 p_seq
->seq_force_screen_content_tools
= SELECT_SCREEN_CONTENT_TOOLS
;
305 p_seq
->seq_force_integer_mv
= SELECT_INTEGER_MV
;
308 p_seq
->seq_profile
= bs_read(&bs
, 3);
309 p_seq
->still_picture
= bs_read1(&bs
);
310 const obu_u1_t reduced_still_picture_header
= bs_read1(&bs
);
311 if(reduced_still_picture_header
)
313 p_seq
->operating_points
[0].seq_level_idx
= bs_read(&bs
, 5);
317 p_seq
->timing_info_present_flag
= bs_read1(&bs
);
318 if(p_seq
->timing_info_present_flag
)
320 av1_parse_timing_info(&bs
, &p_seq
->timing_info
);
321 p_seq
->decoder_model_info_present_flag
= bs_read1(&bs
);
322 if(p_seq
->decoder_model_info_present_flag
)
323 av1_parse_decoder_model_info(&bs
, &p_seq
->decoder_model_info
);
326 p_seq
->initial_display_delay_present_flag
= bs_read1(&bs
);
327 p_seq
->operating_points_cnt_minus_1
= bs_read(&bs
, 5);
328 for(obu_u5_t i
=0; i
<=p_seq
->operating_points_cnt_minus_1
; i
++)
330 p_seq
->operating_points
[i
].operating_point_idc
= bs_read(&bs
, 12);
331 p_seq
->operating_points
[i
].seq_level_idx
= bs_read(&bs
, 5);
332 if(p_seq
->operating_points
[i
].seq_level_idx
> 7)
333 p_seq
->operating_points
[i
].seq_tier
= bs_read1(&bs
);
334 if(p_seq
->decoder_model_info_present_flag
)
336 p_seq
->operating_points
[i
].decoder_model_present_for_this_op
= bs_read1(&bs
);
337 if(p_seq
->operating_points
[i
].decoder_model_present_for_this_op
)
338 av1_parse_operating_parameters_info(&bs
, &p_seq
->operating_points
[i
].operating_parameters_info
,
339 p_seq
->decoder_model_info
.buffer_delay_length_minus_1
);
341 if(p_seq
->initial_display_delay_present_flag
)
343 p_seq
->operating_points
[i
].initial_display_delay_present_for_this_op
= bs_read1(&bs
);
344 if(p_seq
->operating_points
[i
].initial_display_delay_present_for_this_op
)
346 p_seq
->operating_points
[i
].initial_display_delay_minus_1
= bs_read(&bs
, 4);
351 const obu_u4_t frame_width_bits_minus_1
= bs_read(&bs
, 4);
352 const obu_u4_t frame_height_bits_minus_1
= bs_read(&bs
, 4);
353 p_seq
->max_frame_width_minus_1
= bs_read(&bs
, 1 + frame_width_bits_minus_1
);
354 p_seq
->max_frame_height_minus_1
= bs_read(&bs
, 1 + frame_height_bits_minus_1
);
355 if(!reduced_still_picture_header
)
357 p_seq
->frame_id_numbers_present_flag
= bs_read1(&bs
);
358 if(p_seq
->frame_id_numbers_present_flag
)
360 p_seq
->delta_frame_id_length_minus_2
= bs_read(&bs
, 4);
361 p_seq
->additional_frame_id_length_minus_1
= bs_read(&bs
, 3);
364 p_seq
->use_128x128_superblock
= bs_read1(&bs
);
365 p_seq
->enable_filter_intra
= bs_read1(&bs
);
366 p_seq
->enable_intra_edge_filter
= bs_read1(&bs
);
367 if(!reduced_still_picture_header
)
369 p_seq
->enable_interintra_compound
= bs_read1(&bs
);
370 p_seq
->enable_masked_compound
= bs_read1(&bs
);
371 p_seq
->enable_warped_motion
= bs_read1(&bs
);
372 p_seq
->enable_dual_filter
= bs_read1(&bs
);
373 p_seq
->enable_order_hint
= bs_read1(&bs
);
374 if(p_seq
->enable_order_hint
)
376 p_seq
->enable_jnt_comp
= bs_read1(&bs
);
377 p_seq
->enable_ref_frame_mvs
= bs_read1(&bs
);
379 const obu_u1_t seq_choose_screen_content_tools
= bs_read1(&bs
);
380 if(!seq_choose_screen_content_tools
)
381 p_seq
->seq_force_screen_content_tools
= bs_read1(&bs
);
383 if(p_seq
->seq_force_screen_content_tools
)
385 const obu_u1_t seq_choose_integer_mv
= bs_read1(&bs
);
386 if(!seq_choose_integer_mv
)
387 p_seq
->seq_force_integer_mv
= bs_read1(&bs
);
390 if(p_seq
->enable_order_hint
)
391 p_seq
->order_hint_bits_minus_1
= bs_read(&bs
, 3);
393 p_seq
->enable_superres
= bs_read1(&bs
);
394 p_seq
->enable_cdef
= bs_read1(&bs
);
395 p_seq
->enable_restoration
= bs_read1(&bs
);
396 av1_parse_color_config(&bs
, &p_seq
->color_config
, p_seq
->seq_profile
);
397 if(bs_remain(&bs
) < 1)
399 AV1_release_sequence_header(p_seq
);
402 p_seq
->film_grain_params_present
= bs_read1(&bs
);
411 struct av1_uncompressed_header_s
413 obu_u1_t show_existing_frame
;
416 obu_u32_t frame_presentation_time
;
419 static bool av1_parse_uncompressed_header(bs_t
*p_bs
, struct av1_uncompressed_header_s
*p_uh
,
420 const av1_OBU_sequence_header_t
*p_seq
)
422 if(p_seq
->reduced_still_picture_header
)
424 p_uh
->frame_type
= AV1_FRAME_TYPE_KEY
;
425 p_uh
->show_frame
= 1;
429 p_uh
->show_existing_frame
= bs_read1(p_bs
);
430 if(p_uh
->show_existing_frame
)
432 const obu_u3_t frame_to_show_map_idx
= bs_read(p_bs
, 3);
433 VLC_UNUSED(frame_to_show_map_idx
);
434 if(p_seq
->decoder_model_info_present_flag
&& !p_seq
->timing_info
.equal_picture_interval
)
436 /* temporal_point_info() */
437 p_uh
->frame_presentation_time
=
438 bs_read(p_bs
, 1 + p_seq
->decoder_model_info
.frame_presentation_time_length_minus_1
);
440 if(p_seq
->frame_id_numbers_present_flag
)
442 const uint8_t idLen
= p_seq
->additional_frame_id_length_minus_1
+
443 p_seq
->delta_frame_id_length_minus_2
+ 3;
444 const obu_u32_t display_frame_id
= bs_read(p_bs
, idLen
);
445 VLC_UNUSED(display_frame_id
);
447 if(p_seq
->film_grain_params_present
)
452 p_uh
->frame_type
= bs_read(p_bs
, 2);
453 p_uh
->show_frame
= bs_read1(p_bs
);
463 struct av1_OBU_frame_header_t
465 struct av1_header_info_s obu_header
;
466 struct av1_uncompressed_header_s header
;
469 void AV1_release_frame_header(av1_OBU_frame_header_t
*p_fh
)
474 av1_OBU_frame_header_t
*
475 AV1_OBU_parse_frame_header(const uint8_t *p_data
, size_t i_data
,
476 const av1_OBU_sequence_header_t
*p_seq
)
479 bs_init(&bs
, p_data
, i_data
);
481 av1_OBU_frame_header_t
*p_fh
= calloc(1, sizeof(*p_fh
));
485 if(!av1_read_header(&bs
, &p_fh
->obu_header
) ||
486 !av1_parse_uncompressed_header(&bs
, &p_fh
->header
, p_seq
))
488 AV1_release_frame_header(p_fh
);
495 enum av1_frame_type_e
AV1_get_frame_type(const av1_OBU_frame_header_t
*p_fh
)
497 return p_fh
->header
.frame_type
;
500 bool AV1_get_frame_visibility(const av1_OBU_frame_header_t
*p_fh
)
502 return p_fh
->header
.show_frame
;
508 void AV1_get_frame_max_dimensions(const av1_OBU_sequence_header_t
*p_seq
, unsigned *w
, unsigned *h
)
510 *h
= 1 + p_seq
->max_frame_height_minus_1
;
511 *w
= 1 + p_seq
->max_frame_width_minus_1
;
514 void AV1_get_profile_level(const av1_OBU_sequence_header_t
*p_seq
,
515 int *pi_profile
, int *pi_level
, int *pi_tier
)
517 *pi_profile
= p_seq
->seq_profile
;
518 *pi_level
= p_seq
->operating_points
[0].seq_level_idx
;
519 *pi_tier
= p_seq
->operating_points
[0].seq_tier
;
522 bool AV1_get_frame_rate(const av1_OBU_sequence_header_t
*p_seq
,
523 unsigned *num
, unsigned *den
)
525 if(!p_seq
->timing_info_present_flag
||
526 !p_seq
->timing_info
.equal_picture_interval
) /* need support for VFR */
528 *num
= (1 + p_seq
->timing_info
.num_ticks_per_picture_minus_1
) *
529 p_seq
->timing_info
.num_units_in_display_tick
;
530 *den
= p_seq
->timing_info
.time_scale
;
534 bool AV1_get_colorimetry(const av1_OBU_sequence_header_t
*p_seq
,
535 video_color_primaries_t
*p_primaries
,
536 video_transfer_func_t
*p_transfer
,
537 video_color_space_t
*p_colorspace
,
538 video_color_range_t
*p_full_range
)
540 if(!p_seq
->color_config
.color_description_present_flag
)
542 *p_primaries
= iso_23001_8_cp_to_vlc_primaries(p_seq
->color_config
.color_primaries
);
543 *p_transfer
= iso_23001_8_tc_to_vlc_xfer(p_seq
->color_config
.transfer_characteristics
);
544 *p_colorspace
= iso_23001_8_mc_to_vlc_coeffs(p_seq
->color_config
.matrix_coefficients
);
545 *p_full_range
= p_seq
->color_config
.color_range
? COLOR_RANGE_FULL
: COLOR_RANGE_LIMITED
;
549 size_t AV1_create_DecoderConfigurationRecord(uint8_t **pp_buffer
,
550 const av1_OBU_sequence_header_t
*p_seq
,
551 size_t i_obu
, const uint8_t *p_obus
[],
552 const size_t pi_obus
[])
555 for(size_t i
=0; i
<i_obu
; i
++)
556 i_buffer
+= pi_obus
[i
];
558 uint8_t *p_buffer
= malloc(i_buffer
);
563 bs_write_init(&bs
, p_buffer
, i_buffer
);
564 bs_write(&bs
, 1, 1); /* unsigned int (1) marker = 1; */
565 bs_write(&bs
, 7, 1); /* unsigned int (7) version = 1; */
566 bs_write(&bs
, 3, p_seq
->seq_profile
); /* unsigned int (3) seq_profile; */
567 bs_write(&bs
, 5, p_seq
->operating_points
[0].seq_level_idx
); /* unsigned int (5) seq_level_idx_0; */
569 bs_write(&bs
, 1, p_seq
->operating_points
[0].seq_tier
); /* unsigned int (1) seq_tier_0; */
570 bs_write(&bs
, 1, p_seq
->color_config
.high_bitdepth
); /* unsigned int (1) high_bitdepth; */
571 bs_write(&bs
, 1, p_seq
->color_config
.twelve_bit
); /* unsigned int (1) twelve_bit; */
572 bs_write(&bs
, 1, p_seq
->color_config
.mono_chrome
); /* unsigned int (1) monochrome; */
573 bs_write(&bs
, 1, p_seq
->color_config
.subsampling_x
); /* unsigned int (1) chroma_subsampling_x; */
574 bs_write(&bs
, 1, p_seq
->color_config
.subsampling_y
); /* unsigned int (1) chroma_subsampling_y; */
575 bs_write(&bs
, 2, p_seq
->color_config
.chroma_sample_position
); /* unsigned int (2) chroma_sample_position; */
577 bs_write(&bs
, 3, 0); /* unsigned int (3) reserved = 0; */
578 bs_write(&bs
, 1, 0); /* unsigned int (1) initial_presentation_delay_present; (can't compute it) */
579 bs_write(&bs
, 4, 0); /* unsigned int (4) reserved = 0; */
581 /*unsigned int (8)[] configOBUs;*/
583 for(size_t i
=0; i
<i_obu
; i
++)
584 memcpy(&p_buffer
[i_offset
], p_obus
[i
], pi_obus
[i
]);
586 *pp_buffer
= p_buffer
;