/src/vlc/modules/packetizer/av1_obu.c
Line | Count | Source |
1 | | /***************************************************************************** |
2 | | * av1_obu.c: AV1 OBU parser |
3 | | ***************************************************************************** |
4 | | * Copyright (C) 2018 VideoLabs, VLC authors and VideoLAN |
5 | | * |
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. |
10 | | * |
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. |
15 | | * |
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 | | *****************************************************************************/ |
20 | | #ifdef HAVE_CONFIG_H |
21 | | # include "config.h" |
22 | | #endif |
23 | | |
24 | | #include <vlc_common.h> |
25 | | #include <vlc_bits.h> |
26 | | #include <vlc_es.h> |
27 | | |
28 | | #include "av1.h" |
29 | | #include "av1_obu.h" |
30 | | #include "iso_color_tables.h" |
31 | | |
32 | | #include <assert.h> |
33 | | |
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; |
45 | | |
46 | 155k | #define SELECT_SCREEN_CONTENT_TOOLS 2 |
47 | 155k | #define SELECT_INTEGER_MV 2 |
48 | | |
49 | | |
50 | | #define AV1_OPERATING_POINTS_COUNT 32 |
51 | | |
52 | | /* |
53 | | * Header |
54 | | */ |
55 | | struct av1_header_info_s |
56 | | { |
57 | | obu_u4_t obu_type; |
58 | | obu_u3_t temporal_id; |
59 | | obu_u2_t spatial_id; |
60 | | }; |
61 | | |
62 | | static bool av1_read_header(bs_t *p_bs, struct av1_header_info_s *p_hdr) |
63 | 216k | { |
64 | 216k | if(bs_read1(p_bs)) |
65 | 0 | return false; |
66 | 216k | p_hdr->obu_type = bs_read(p_bs, 4); |
67 | 216k | const obu_u1_t obu_extension_flag = bs_read1(p_bs); |
68 | 216k | const obu_u1_t obu_has_size_field = bs_read1(p_bs); |
69 | 216k | if(bs_read1(p_bs)) |
70 | 0 | return false; |
71 | 216k | if(obu_extension_flag) |
72 | 5.65k | { |
73 | 5.65k | p_hdr->temporal_id = bs_read(p_bs, 3); |
74 | 5.65k | p_hdr->spatial_id = bs_read(p_bs, 2); |
75 | 5.65k | bs_skip(p_bs, 3); |
76 | 5.65k | } |
77 | 216k | if(obu_has_size_field) |
78 | 166k | { |
79 | 170k | for (uint8_t i = 0; i < 8; i++) |
80 | 170k | { |
81 | 170k | uint8_t v = bs_read(p_bs, 8); |
82 | 170k | if (!(v & 0x80)) |
83 | 166k | break; |
84 | 3.85k | if(i == 7) |
85 | 0 | return false; |
86 | 3.85k | } |
87 | 166k | } |
88 | 216k | return !bs_error(p_bs); |
89 | 216k | } |
90 | | |
91 | | /* |
92 | | * Sequence sub sections readers |
93 | | */ |
94 | | |
95 | | struct av1_timing_info_s |
96 | | { |
97 | | obu_u32_t num_units_in_display_tick; |
98 | | obu_u32_t time_scale; |
99 | | obu_u1_t equal_picture_interval; |
100 | | obu_uvlc_t num_ticks_per_picture_minus_1; |
101 | | }; |
102 | | |
103 | | static bool av1_parse_timing_info(bs_t *p_bs, struct av1_timing_info_s *p_ti) |
104 | 30.2k | { |
105 | 30.2k | p_ti->num_units_in_display_tick = bs_read(p_bs, 32); |
106 | 30.2k | p_ti->time_scale = bs_read(p_bs, 32); |
107 | 30.2k | p_ti->equal_picture_interval = bs_read1(p_bs); |
108 | 30.2k | if(p_ti->equal_picture_interval) |
109 | 8.99k | p_ti->num_ticks_per_picture_minus_1 = bs_read_ue(p_bs); |
110 | 30.2k | return true; |
111 | 30.2k | } |
112 | | |
113 | | struct av1_decoder_model_info_s |
114 | | { |
115 | | obu_u5_t buffer_delay_length_minus_1; |
116 | | obu_u32_t num_units_in_decoding_tick; |
117 | | obu_u5_t buffer_removal_time_length_minus_1; |
118 | | obu_u5_t frame_presentation_time_length_minus_1; |
119 | | }; |
120 | | |
121 | | static bool av1_parse_decoder_model_info(bs_t *p_bs, struct av1_decoder_model_info_s *p_dm) |
122 | 21.2k | { |
123 | 21.2k | p_dm->buffer_delay_length_minus_1 = bs_read(p_bs, 5); |
124 | 21.2k | p_dm->num_units_in_decoding_tick = bs_read(p_bs, 32); |
125 | 21.2k | p_dm->buffer_removal_time_length_minus_1 = bs_read(p_bs, 5); |
126 | 21.2k | p_dm->frame_presentation_time_length_minus_1 = bs_read(p_bs, 5); |
127 | 21.2k | return true; |
128 | 21.2k | } |
129 | | |
130 | | struct av1_operating_parameters_info_s |
131 | | { |
132 | | obu_u32_t decoder_buffer_delay; |
133 | | obu_u32_t encoder_buffer_delay; |
134 | | obu_u1_t low_delay_mode_flag; |
135 | | }; |
136 | | |
137 | | static bool av1_parse_operating_parameters_info(bs_t *p_bs, |
138 | | struct av1_operating_parameters_info_s *p_op, |
139 | | obu_u8_t buffer_delay_length_minus_1) |
140 | 265k | { |
141 | 265k | p_op->decoder_buffer_delay = bs_read(p_bs, 1 + buffer_delay_length_minus_1); |
142 | 265k | p_op->encoder_buffer_delay = bs_read(p_bs, 1 + buffer_delay_length_minus_1); |
143 | 265k | p_op->low_delay_mode_flag = bs_read1(p_bs); |
144 | 265k | return true; |
145 | 265k | } |
146 | | |
147 | | struct av1_color_config_s |
148 | | { |
149 | | obu_u1_t high_bitdepth; |
150 | | obu_u1_t twelve_bit; |
151 | | obu_u1_t mono_chrome; |
152 | | obu_u1_t color_description_present_flag; |
153 | | obu_u8_t color_primaries; |
154 | | obu_u8_t transfer_characteristics; |
155 | | obu_u8_t matrix_coefficients; |
156 | | obu_u1_t color_range; |
157 | | obu_u1_t subsampling_x; |
158 | | obu_u1_t subsampling_y; |
159 | | obu_u2_t chroma_sample_position; |
160 | | obu_u1_t separate_uv_delta_q; |
161 | | |
162 | | vlc_fourcc_t i_chroma; |
163 | | }; |
164 | | |
165 | | static bool av1_parse_color_config(bs_t *p_bs, |
166 | | struct av1_color_config_s *p_cc, |
167 | | obu_u3_t seq_profile) |
168 | 155k | { |
169 | 155k | p_cc->high_bitdepth = bs_read1(p_bs); |
170 | 155k | if (seq_profile == 2 && p_cc->high_bitdepth) |
171 | 33.3k | p_cc->twelve_bit = bs_read1(p_bs); |
172 | 155k | if (seq_profile != 1) |
173 | 128k | p_cc->mono_chrome = bs_read1(p_bs); |
174 | 155k | const uint8_t BitDepth = p_cc->twelve_bit ? 12 : ((p_cc->high_bitdepth) ? 10 : 8); |
175 | | |
176 | 155k | p_cc->color_description_present_flag = bs_read1(p_bs); |
177 | 155k | if(p_cc->color_description_present_flag) |
178 | 50.7k | { |
179 | 50.7k | p_cc->color_primaries = bs_read(p_bs, 8); |
180 | 50.7k | p_cc->transfer_characteristics = bs_read(p_bs, 8); |
181 | 50.7k | p_cc->matrix_coefficients = bs_read(p_bs, 8); |
182 | 50.7k | } |
183 | 105k | else |
184 | 105k | { |
185 | 105k | p_cc->color_primaries = 2; |
186 | 105k | p_cc->transfer_characteristics = 2; |
187 | 105k | p_cc->matrix_coefficients = 2; |
188 | 105k | } |
189 | | |
190 | 155k | if(p_cc->mono_chrome) |
191 | 42.2k | { |
192 | 42.2k | p_cc->color_range = bs_read1(p_bs) ? COLOR_RANGE_FULL : COLOR_RANGE_LIMITED; |
193 | 42.2k | p_cc->i_chroma = VLC_CODEC_GREY; |
194 | 42.2k | p_cc->subsampling_x = 1; |
195 | 42.2k | p_cc->subsampling_y = 1; |
196 | 42.2k | } |
197 | 113k | else if( p_cc->color_primaries == 1 && |
198 | 1.66k | p_cc->transfer_characteristics == 13 && |
199 | 769 | p_cc->matrix_coefficients == 0 ) |
200 | 769 | { |
201 | 769 | p_cc->color_range = COLOR_RANGE_FULL; |
202 | 769 | p_cc->i_chroma = VLC_CODEC_I444; |
203 | 769 | p_cc->subsampling_x = 0; |
204 | 769 | p_cc->subsampling_y = 0; |
205 | 769 | } |
206 | 112k | else |
207 | 112k | { |
208 | 112k | p_cc->color_range = bs_read1(p_bs) ? COLOR_RANGE_FULL : COLOR_RANGE_LIMITED; |
209 | 112k | if(seq_profile > 1) |
210 | 42.2k | { |
211 | 42.2k | if(BitDepth == 12) |
212 | 9.82k | { |
213 | 9.82k | p_cc->subsampling_x = bs_read1(p_bs); |
214 | 9.82k | p_cc->subsampling_y = p_cc->subsampling_x ? bs_read1(p_bs) : 0; |
215 | 9.82k | } |
216 | 32.4k | else |
217 | 32.4k | { |
218 | 32.4k | p_cc->subsampling_x = 1; |
219 | 32.4k | p_cc->subsampling_y = 0; |
220 | 32.4k | } |
221 | 42.2k | p_cc->i_chroma = p_cc->subsampling_x ? |
222 | 35.6k | p_cc->subsampling_y ? VLC_CODEC_I420 : |
223 | 35.6k | VLC_CODEC_I422 : |
224 | 42.2k | VLC_CODEC_I444; |
225 | 42.2k | } |
226 | 70.5k | else if(seq_profile == 1) |
227 | 26.9k | { |
228 | 26.9k | p_cc->i_chroma = VLC_CODEC_I444; |
229 | 26.9k | p_cc->subsampling_x = 0; |
230 | 26.9k | p_cc->subsampling_y = 0; |
231 | 26.9k | } |
232 | 43.6k | else |
233 | 43.6k | { |
234 | 43.6k | p_cc->i_chroma = VLC_CODEC_I420; |
235 | 43.6k | p_cc->subsampling_x = 1; |
236 | 43.6k | p_cc->subsampling_y = 1; |
237 | 43.6k | } |
238 | | |
239 | 112k | if(p_cc->subsampling_x && p_cc->subsampling_y) |
240 | 46.6k | p_cc->chroma_sample_position = bs_read(p_bs, 2); |
241 | 112k | } |
242 | | |
243 | 155k | p_cc->separate_uv_delta_q = bs_read1(p_bs); |
244 | | |
245 | 155k | return true; |
246 | 155k | } |
247 | | |
248 | | /* |
249 | | * OBU readers |
250 | | */ |
251 | | |
252 | | struct av1_OBU_sequence_header_t |
253 | | { |
254 | | struct av1_header_info_s obu_header; |
255 | | obu_u3_t seq_profile; |
256 | | obu_u1_t still_picture; |
257 | | obu_u1_t reduced_still_picture_header; |
258 | | obu_u1_t timing_info_present_flag; |
259 | | struct av1_timing_info_s timing_info; |
260 | | obu_u1_t decoder_model_info_present_flag; |
261 | | struct av1_decoder_model_info_s decoder_model_info; |
262 | | obu_u1_t initial_display_delay_present_flag; |
263 | | obu_u1_t operating_points_cnt_minus_1; |
264 | | struct |
265 | | { |
266 | | obu_u12_t operating_point_idc; |
267 | | obu_u5_t seq_level_idx; |
268 | | obu_u1_t seq_tier; |
269 | | obu_u1_t decoder_model_present_for_this_op; |
270 | | struct av1_operating_parameters_info_s operating_parameters_info; |
271 | | obu_u1_t initial_display_delay_present_for_this_op; |
272 | | obu_u4_t initial_display_delay_minus_1; |
273 | | } operating_points[AV1_OPERATING_POINTS_COUNT]; |
274 | | obu_u32_t max_frame_width_minus_1; |
275 | | obu_u32_t max_frame_height_minus_1; |
276 | | obu_u1_t frame_id_numbers_present_flag; |
277 | | obu_u4_t delta_frame_id_length_minus_2; |
278 | | obu_u3_t additional_frame_id_length_minus_1; |
279 | | obu_u1_t use_128x128_superblock; |
280 | | obu_u1_t enable_filter_intra; |
281 | | obu_u1_t enable_intra_edge_filter; |
282 | | |
283 | | obu_u1_t enable_interintra_compound; |
284 | | obu_u1_t enable_masked_compound; |
285 | | obu_u1_t enable_warped_motion; |
286 | | obu_u1_t enable_dual_filter; |
287 | | obu_u1_t enable_order_hint; |
288 | | obu_u1_t enable_jnt_comp; |
289 | | obu_u1_t enable_ref_frame_mvs; |
290 | | obu_u2_t seq_force_screen_content_tools; |
291 | | obu_u2_t seq_force_integer_mv; |
292 | | obu_u3_t order_hint_bits_minus_1; |
293 | | |
294 | | obu_u1_t enable_superres; |
295 | | obu_u1_t enable_cdef; |
296 | | obu_u1_t enable_restoration; |
297 | | struct av1_color_config_s color_config; |
298 | | obu_u1_t film_grain_params_present; |
299 | | }; |
300 | | |
301 | | void AV1_release_sequence_header(av1_OBU_sequence_header_t *p_seq) |
302 | 157k | { |
303 | 157k | free(p_seq); |
304 | 157k | } |
305 | | |
306 | | av1_OBU_sequence_header_t * |
307 | | AV1_OBU_parse_sequence_header(const uint8_t *p_data, size_t i_data) |
308 | 157k | { |
309 | 157k | bs_t bs; |
310 | 157k | bs_init(&bs, p_data, i_data); |
311 | | |
312 | 157k | av1_OBU_sequence_header_t *p_seq = calloc(1, sizeof(*p_seq)); |
313 | 157k | if(!p_seq) |
314 | 0 | return NULL; |
315 | | |
316 | 157k | if(!av1_read_header(&bs, &p_seq->obu_header)) |
317 | 1.18k | { |
318 | 1.18k | AV1_release_sequence_header(p_seq); |
319 | 1.18k | return NULL; |
320 | 1.18k | } |
321 | | |
322 | 155k | p_seq->seq_force_screen_content_tools = SELECT_SCREEN_CONTENT_TOOLS; |
323 | 155k | p_seq->seq_force_integer_mv = SELECT_INTEGER_MV; |
324 | | |
325 | | |
326 | 155k | p_seq->seq_profile = bs_read(&bs, 3); |
327 | 155k | p_seq->still_picture = bs_read1(&bs); |
328 | 155k | p_seq->reduced_still_picture_header = bs_read1(&bs); |
329 | 155k | if(p_seq->reduced_still_picture_header) |
330 | 94.6k | { |
331 | 94.6k | p_seq->operating_points[0].seq_level_idx = bs_read(&bs, 5); |
332 | 94.6k | } |
333 | 61.2k | else |
334 | 61.2k | { |
335 | 61.2k | p_seq->timing_info_present_flag = bs_read1(&bs); |
336 | 61.2k | if(p_seq->timing_info_present_flag) |
337 | 30.2k | { |
338 | 30.2k | av1_parse_timing_info(&bs, &p_seq->timing_info); |
339 | 30.2k | p_seq->decoder_model_info_present_flag = bs_read1(&bs); |
340 | 30.2k | if(p_seq->decoder_model_info_present_flag) |
341 | 21.2k | av1_parse_decoder_model_info(&bs, &p_seq->decoder_model_info); |
342 | 30.2k | } |
343 | | |
344 | 61.2k | p_seq->initial_display_delay_present_flag = bs_read1(&bs); |
345 | 61.2k | p_seq->operating_points_cnt_minus_1 = bs_read(&bs, 5); |
346 | 766k | for(obu_u5_t i=0; i<=p_seq->operating_points_cnt_minus_1; i++) |
347 | 705k | { |
348 | 705k | p_seq->operating_points[i].operating_point_idc = bs_read(&bs, 12); |
349 | 705k | p_seq->operating_points[i].seq_level_idx = bs_read(&bs, 5); |
350 | 705k | if(p_seq->operating_points[i].seq_level_idx > 7) |
351 | 393k | p_seq->operating_points[i].seq_tier = bs_read1(&bs); |
352 | 705k | if(p_seq->decoder_model_info_present_flag) |
353 | 465k | { |
354 | 465k | p_seq->operating_points[i].decoder_model_present_for_this_op = bs_read1(&bs); |
355 | 465k | if(p_seq->operating_points[i].decoder_model_present_for_this_op) |
356 | 265k | av1_parse_operating_parameters_info(&bs, &p_seq->operating_points[i].operating_parameters_info, |
357 | 265k | p_seq->decoder_model_info.buffer_delay_length_minus_1); |
358 | 465k | } |
359 | 705k | if(p_seq->initial_display_delay_present_flag) |
360 | 567k | { |
361 | 567k | p_seq->operating_points[i].initial_display_delay_present_for_this_op = bs_read1(&bs); |
362 | 567k | if(p_seq->operating_points[i].initial_display_delay_present_for_this_op) |
363 | 223k | { |
364 | 223k | p_seq->operating_points[i].initial_display_delay_minus_1 = bs_read(&bs, 4); |
365 | 223k | } |
366 | 567k | } |
367 | 705k | } |
368 | 61.2k | } |
369 | 155k | const obu_u4_t frame_width_bits_minus_1 = bs_read(&bs, 4); |
370 | 155k | const obu_u4_t frame_height_bits_minus_1 = bs_read(&bs, 4); |
371 | 155k | p_seq->max_frame_width_minus_1 = bs_read(&bs, 1 + frame_width_bits_minus_1); |
372 | 155k | p_seq->max_frame_height_minus_1 = bs_read(&bs, 1 + frame_height_bits_minus_1); |
373 | 155k | if(!p_seq->reduced_still_picture_header) |
374 | 61.2k | { |
375 | 61.2k | p_seq->frame_id_numbers_present_flag = bs_read1(&bs); |
376 | 61.2k | if(p_seq->frame_id_numbers_present_flag) |
377 | 12.9k | { |
378 | 12.9k | p_seq->delta_frame_id_length_minus_2 = bs_read(&bs, 4); |
379 | 12.9k | p_seq->additional_frame_id_length_minus_1 = bs_read(&bs, 3); |
380 | 12.9k | } |
381 | 61.2k | } |
382 | 155k | p_seq->use_128x128_superblock = bs_read1(&bs); |
383 | 155k | p_seq->enable_filter_intra = bs_read1(&bs); |
384 | 155k | p_seq->enable_intra_edge_filter = bs_read1(&bs); |
385 | 155k | if(!p_seq->reduced_still_picture_header) |
386 | 61.2k | { |
387 | 61.2k | p_seq->enable_interintra_compound = bs_read1(&bs); |
388 | 61.2k | p_seq->enable_masked_compound = bs_read1(&bs); |
389 | 61.2k | p_seq->enable_warped_motion = bs_read1(&bs); |
390 | 61.2k | p_seq->enable_dual_filter = bs_read1(&bs); |
391 | 61.2k | p_seq->enable_order_hint = bs_read1(&bs); |
392 | 61.2k | if(p_seq->enable_order_hint) |
393 | 15.5k | { |
394 | 15.5k | p_seq->enable_jnt_comp = bs_read1(&bs); |
395 | 15.5k | p_seq->enable_ref_frame_mvs = bs_read1(&bs); |
396 | 15.5k | } |
397 | 61.2k | const obu_u1_t seq_choose_screen_content_tools = bs_read1(&bs); |
398 | 61.2k | if(!seq_choose_screen_content_tools) |
399 | 47.6k | p_seq->seq_force_screen_content_tools = bs_read1(&bs); |
400 | | |
401 | 61.2k | if(p_seq->seq_force_screen_content_tools) |
402 | 21.8k | { |
403 | 21.8k | const obu_u1_t seq_choose_integer_mv = bs_read1(&bs); |
404 | 21.8k | if(!seq_choose_integer_mv) |
405 | 7.17k | p_seq->seq_force_integer_mv = bs_read1(&bs); |
406 | 21.8k | } |
407 | | |
408 | 61.2k | if(p_seq->enable_order_hint) |
409 | 15.5k | p_seq->order_hint_bits_minus_1 = bs_read(&bs, 3); |
410 | 61.2k | } |
411 | 155k | p_seq->enable_superres = bs_read1(&bs); |
412 | 155k | p_seq->enable_cdef = bs_read1(&bs); |
413 | 155k | p_seq->enable_restoration = bs_read1(&bs); |
414 | 155k | av1_parse_color_config(&bs, &p_seq->color_config, p_seq->seq_profile); |
415 | | |
416 | 155k | if(bs_error(&bs)) |
417 | 21.4k | { |
418 | 21.4k | AV1_release_sequence_header(p_seq); |
419 | 21.4k | return NULL; |
420 | 21.4k | } |
421 | | |
422 | 134k | p_seq->film_grain_params_present = bs_read1(&bs); |
423 | | |
424 | 134k | return p_seq; |
425 | 155k | } |
426 | | |
427 | | /* |
428 | | * Frame sub readers |
429 | | */ |
430 | | |
431 | | struct av1_uncompressed_header_s |
432 | | { |
433 | | obu_u1_t show_existing_frame; |
434 | | obu_u2_t frame_type; |
435 | | obu_u1_t show_frame; |
436 | | obu_u32_t frame_presentation_time; |
437 | | }; |
438 | | |
439 | | static bool av1_parse_uncompressed_header(bs_t *p_bs, struct av1_uncompressed_header_s *p_uh, |
440 | | const av1_OBU_sequence_header_t *p_seq) |
441 | 59.5k | { |
442 | 59.5k | if(p_seq->reduced_still_picture_header) |
443 | 44.9k | { |
444 | 44.9k | p_uh->frame_type = AV1_FRAME_TYPE_KEY; |
445 | 44.9k | p_uh->show_frame = 1; |
446 | 44.9k | } |
447 | 14.6k | else |
448 | 14.6k | { |
449 | 14.6k | p_uh->show_existing_frame = bs_read1(p_bs); |
450 | 14.6k | if(p_uh->show_existing_frame) |
451 | 691 | { |
452 | 691 | const obu_u3_t frame_to_show_map_idx = bs_read(p_bs, 3); |
453 | 691 | VLC_UNUSED(frame_to_show_map_idx); |
454 | 691 | if(p_seq->decoder_model_info_present_flag && !p_seq->timing_info.equal_picture_interval) |
455 | 19 | { |
456 | | /* temporal_point_info() */ |
457 | 19 | p_uh->frame_presentation_time = |
458 | 19 | bs_read(p_bs, 1 + p_seq->decoder_model_info.frame_presentation_time_length_minus_1); |
459 | 19 | } |
460 | 691 | if(p_seq->frame_id_numbers_present_flag) |
461 | 163 | { |
462 | 163 | const uint8_t idLen = p_seq->additional_frame_id_length_minus_1 + |
463 | 163 | p_seq->delta_frame_id_length_minus_2 + 3; |
464 | 163 | const obu_u32_t display_frame_id = bs_read(p_bs, idLen); |
465 | 163 | VLC_UNUSED(display_frame_id); |
466 | 163 | } |
467 | 691 | if(p_seq->film_grain_params_present) |
468 | 53 | { |
469 | | /* load_grain */ |
470 | 53 | } |
471 | 691 | p_uh->frame_type = AV1_FRAME_TYPE_INTER; |
472 | 691 | p_uh->show_frame = 1; |
473 | 691 | return true; |
474 | 691 | } |
475 | 13.9k | p_uh->frame_type = bs_read(p_bs, 2); |
476 | 13.9k | p_uh->show_frame = bs_read1(p_bs); |
477 | 13.9k | } |
478 | | |
479 | 58.8k | return true; |
480 | 59.5k | } |
481 | | |
482 | | /* |
483 | | * Frame OBU |
484 | | */ |
485 | | |
486 | | struct av1_OBU_frame_header_t |
487 | | { |
488 | | struct av1_header_info_s obu_header; |
489 | | struct av1_uncompressed_header_s header; |
490 | | }; |
491 | | |
492 | | void AV1_release_frame_header(av1_OBU_frame_header_t *p_fh) |
493 | 59.7k | { |
494 | 59.7k | free(p_fh); |
495 | 59.7k | } |
496 | | |
497 | | av1_OBU_frame_header_t * |
498 | | AV1_OBU_parse_frame_header(const uint8_t *p_data, size_t i_data, |
499 | | const av1_OBU_sequence_header_t *p_seq) |
500 | 59.7k | { |
501 | 59.7k | bs_t bs; |
502 | 59.7k | bs_init(&bs, p_data, i_data); |
503 | | |
504 | 59.7k | av1_OBU_frame_header_t *p_fh = calloc(1, sizeof(*p_fh)); |
505 | 59.7k | if(!p_fh) |
506 | 0 | return NULL; |
507 | | |
508 | 59.7k | if(!av1_read_header(&bs, &p_fh->obu_header) || |
509 | 59.5k | !av1_parse_uncompressed_header(&bs, &p_fh->header, p_seq)) |
510 | 122 | { |
511 | 122 | AV1_release_frame_header(p_fh); |
512 | 122 | return NULL; |
513 | 122 | } |
514 | | |
515 | 59.5k | return p_fh; |
516 | 59.7k | } |
517 | | |
518 | | enum av1_frame_type_e AV1_get_frame_type(const av1_OBU_frame_header_t *p_fh) |
519 | 59.5k | { |
520 | 59.5k | return p_fh->header.frame_type; |
521 | 59.5k | } |
522 | | |
523 | | bool AV1_get_frame_visibility(const av1_OBU_frame_header_t *p_fh) |
524 | 59.5k | { |
525 | 59.5k | return p_fh->header.show_frame; |
526 | 59.5k | } |
527 | | |
528 | | /* |
529 | | * Getters |
530 | | */ |
531 | | void AV1_get_frame_max_dimensions(const av1_OBU_sequence_header_t *p_seq, unsigned *w, unsigned *h) |
532 | 751k | { |
533 | 751k | *h = 1 + p_seq->max_frame_height_minus_1; |
534 | 751k | *w = 1 + p_seq->max_frame_width_minus_1; |
535 | 751k | } |
536 | | |
537 | | void AV1_get_profile_level(const av1_OBU_sequence_header_t *p_seq, |
538 | | int *pi_profile, int *pi_level, int *pi_tier) |
539 | 723k | { |
540 | 723k | *pi_profile = p_seq->seq_profile; |
541 | 723k | *pi_level = p_seq->operating_points[0].seq_level_idx; |
542 | 723k | *pi_tier = p_seq->operating_points[0].seq_tier; |
543 | 723k | } |
544 | | |
545 | | bool AV1_get_frame_rate(const av1_OBU_sequence_header_t *p_seq, |
546 | | unsigned *num, unsigned *den) |
547 | 346k | { |
548 | 346k | if(!p_seq->timing_info_present_flag || |
549 | 15.1k | !p_seq->timing_info.equal_picture_interval) /* need support for VFR */ |
550 | 343k | return false; |
551 | 3.25k | *num = (1 + p_seq->timing_info.num_ticks_per_picture_minus_1) * |
552 | 3.25k | p_seq->timing_info.num_units_in_display_tick; |
553 | 3.25k | *den = p_seq->timing_info.time_scale; |
554 | 3.25k | return true; |
555 | 346k | } |
556 | | |
557 | | bool AV1_get_super_res(const av1_OBU_sequence_header_t *p_seq) |
558 | 28.5k | { |
559 | 28.5k | return p_seq->enable_superres; |
560 | 28.5k | } |
561 | | |
562 | | bool AV1_get_colorimetry(const av1_OBU_sequence_header_t *p_seq, |
563 | | video_color_primaries_t *p_primaries, |
564 | | video_transfer_func_t *p_transfer, |
565 | | video_color_space_t *p_colorspace, |
566 | | video_color_range_t *p_full_range) |
567 | 723k | { |
568 | 723k | if(!p_seq->color_config.color_description_present_flag) |
569 | 600k | return false; |
570 | 122k | *p_primaries = iso_23001_8_cp_to_vlc_primaries(p_seq->color_config.color_primaries); |
571 | 122k | *p_transfer = iso_23001_8_tc_to_vlc_xfer(p_seq->color_config.transfer_characteristics); |
572 | 122k | *p_colorspace = iso_23001_8_mc_to_vlc_coeffs(p_seq->color_config.matrix_coefficients); |
573 | 122k | *p_full_range = p_seq->color_config.color_range ? COLOR_RANGE_FULL : COLOR_RANGE_LIMITED; |
574 | 122k | return true; |
575 | 723k | } |
576 | | |
577 | | vlc_fourcc_t AV1_get_chroma(const av1_OBU_sequence_header_t *p_seq) |
578 | 57.0k | { |
579 | 57.0k | switch (p_seq->color_config.i_chroma) |
580 | 57.0k | { |
581 | 16.5k | case VLC_CODEC_GREY: |
582 | 16.5k | switch (p_seq->color_config.high_bitdepth + p_seq->color_config.twelve_bit) |
583 | 16.5k | { |
584 | 2.39k | case 0: return VLC_CODEC_GREY; |
585 | 7.41k | case 1: return VLC_CODEC_GREY_10L; |
586 | 6.78k | case 2: return VLC_CODEC_GREY_12L; |
587 | 0 | default: |
588 | 16.5k | vlc_assert_unreachable(); |
589 | 16.5k | } |
590 | 0 | break; |
591 | 13.6k | case VLC_CODEC_I420: |
592 | 13.6k | switch (p_seq->color_config.high_bitdepth + p_seq->color_config.twelve_bit) |
593 | 13.6k | { |
594 | 6.15k | case 0: return VLC_CODEC_I420; |
595 | 6.41k | case 1: return VLC_CODEC_I420_10L; |
596 | 1.12k | case 2: return VLC_CODEC_I420_12L; |
597 | 0 | default: |
598 | 13.6k | vlc_assert_unreachable(); |
599 | 13.6k | } |
600 | 0 | break; |
601 | 13.7k | case VLC_CODEC_I422: |
602 | 13.7k | switch (p_seq->color_config.high_bitdepth + p_seq->color_config.twelve_bit) |
603 | 13.7k | { |
604 | 12.1k | case 0: return VLC_CODEC_I422; |
605 | 1.59k | case 1: return VLC_CODEC_I422_10L; |
606 | 48 | case 2: return VLC_CODEC_I422_12L; |
607 | 0 | default: |
608 | 13.7k | vlc_assert_unreachable(); |
609 | 13.7k | } |
610 | 0 | break; |
611 | 12.9k | case VLC_CODEC_I444: |
612 | 12.9k | switch (p_seq->color_config.high_bitdepth + p_seq->color_config.twelve_bit) |
613 | 12.9k | { |
614 | 2.21k | case 0: return VLC_CODEC_I444; |
615 | 8.44k | case 1: return VLC_CODEC_I444_10L; |
616 | 2.33k | case 2: return VLC_CODEC_I444_12L; |
617 | 0 | default: |
618 | 12.9k | vlc_assert_unreachable(); |
619 | 12.9k | } |
620 | 0 | default: |
621 | 57.0k | vlc_assert_unreachable(); |
622 | 57.0k | } |
623 | 57.0k | } |
624 | | |
625 | | bool AV1_OBU_parse_hdr_metadata(const uint8_t *p_data, size_t i_data, |
626 | | av1_OBU_hdr_metadata_t *p_meta) |
627 | 0 | { |
628 | 0 | if(!AV1_OBUSkipHeader(&p_data, &i_data)) |
629 | 0 | return false; |
630 | | |
631 | 0 | uint8_t i_len; |
632 | 0 | const uint64_t i_type = leb128(p_data, i_data, &i_len); |
633 | 0 | if(i_len == 0 || (i_type != AV1_METADATA_TYPE_HDR_MDCV && |
634 | 0 | i_type != AV1_METADATA_TYPE_HDR_CLL)) |
635 | 0 | return false; |
636 | 0 | p_meta->i_type = i_type; |
637 | |
|
638 | 0 | bs_t bs; |
639 | 0 | bs_init(&bs, p_data + i_len, i_data - i_len); |
640 | 0 | if(p_meta->i_type == AV1_METADATA_TYPE_HDR_CLL) |
641 | 0 | { |
642 | 0 | p_meta->lighting.MaxCLL = bs_read(&bs, 16); |
643 | 0 | p_meta->lighting.MaxFALL = bs_read(&bs, 16); |
644 | 0 | } |
645 | 0 | else |
646 | 0 | { |
647 | 0 | const uint8_t RGB2GBR[3] = {2, 0, 1}; |
648 | 0 | for(int i = 0; i < 3; i++) |
649 | 0 | { |
650 | 0 | p_meta->mastering.primaries[RGB2GBR[i] * 2] = |
651 | 0 | bs_read(&bs, 16) * 50000 >> 16; |
652 | 0 | p_meta->mastering.primaries[RGB2GBR[i] * 2 + 1] = |
653 | 0 | bs_read(&bs, 16) * 50000 >> 16; |
654 | 0 | } |
655 | 0 | for(int i = 0; i < 2; i++) |
656 | 0 | p_meta->mastering.white_point[i] = bs_read(&bs, 16) * 50000 >> 16; |
657 | 0 | p_meta->mastering.max_luminance = |
658 | 0 | (uint64_t) bs_read(&bs, 32) * 10000 >> 8; |
659 | 0 | p_meta->mastering.min_luminance = |
660 | 0 | (uint64_t) bs_read(&bs, 32) * 10000 >> 14; |
661 | 0 | } |
662 | 0 | return !bs_error(&bs); |
663 | 0 | } |
664 | | |
665 | | bool AV1_OBU_get_itut_t35(const uint8_t *p_data, size_t i_data, |
666 | | uint8_t *pi_country_code, |
667 | | const uint8_t **pp_payload, size_t *pi_payload) |
668 | 0 | { |
669 | 0 | if(!AV1_OBUSkipHeader(&p_data, &i_data)) |
670 | 0 | return false; |
671 | | |
672 | 0 | uint8_t i_len; |
673 | 0 | if(leb128(p_data, i_data, &i_len) != AV1_METADATA_TYPE_ITUT_T35) |
674 | 0 | return false; |
675 | 0 | p_data += i_len; |
676 | 0 | i_data -= i_len; |
677 | |
|
678 | 0 | while(i_data > 0 && p_data[i_data - 1] == 0) |
679 | 0 | i_data--; |
680 | 0 | if(i_data < 2) |
681 | 0 | return false; |
682 | 0 | i_data--; |
683 | |
|
684 | 0 | const size_t i_header = p_data[0] == 0xFF ? 2 : 1; |
685 | 0 | if(i_data < i_header) |
686 | 0 | return false; |
687 | 0 | *pi_country_code = p_data[0]; |
688 | 0 | *pp_payload = &p_data[i_header]; |
689 | 0 | *pi_payload = i_data - i_header; |
690 | 0 | return true; |
691 | 0 | } |
692 | | |
693 | | size_t AV1_create_DecoderConfigurationRecord(uint8_t **pp_buffer, |
694 | | const av1_OBU_sequence_header_t *p_seq, |
695 | | size_t i_obu, const uint8_t *p_obus[], |
696 | | const size_t pi_obus[]) |
697 | 44.9k | { |
698 | 44.9k | size_t i_buffer = 4; |
699 | 89.9k | for(size_t i=0; i<i_obu; i++) |
700 | 44.9k | i_buffer += pi_obus[i]; |
701 | | |
702 | 44.9k | uint8_t *p_buffer = malloc(i_buffer); |
703 | 44.9k | if(!p_buffer) |
704 | 0 | return 0; |
705 | | |
706 | 44.9k | bs_t bs; |
707 | 44.9k | bs_write_init(&bs, p_buffer, i_buffer); |
708 | 44.9k | bs_write(&bs, 1, 1); /* unsigned int (1) marker = 1; */ |
709 | 44.9k | bs_write(&bs, 7, 1); /* unsigned int (7) version = 1; */ |
710 | 44.9k | bs_write(&bs, 3, p_seq->seq_profile); /* unsigned int (3) seq_profile; */ |
711 | 44.9k | bs_write(&bs, 5, p_seq->operating_points[0].seq_level_idx); /* unsigned int (5) seq_level_idx_0; */ |
712 | | |
713 | 44.9k | bs_write(&bs, 1, p_seq->operating_points[0].seq_tier); /* unsigned int (1) seq_tier_0; */ |
714 | 44.9k | bs_write(&bs, 1, p_seq->color_config.high_bitdepth); /* unsigned int (1) high_bitdepth; */ |
715 | 44.9k | bs_write(&bs, 1, p_seq->color_config.twelve_bit); /* unsigned int (1) twelve_bit; */ |
716 | 44.9k | bs_write(&bs, 1, p_seq->color_config.mono_chrome); /* unsigned int (1) monochrome; */ |
717 | 44.9k | bs_write(&bs, 1, p_seq->color_config.subsampling_x); /* unsigned int (1) chroma_subsampling_x; */ |
718 | 44.9k | bs_write(&bs, 1, p_seq->color_config.subsampling_y); /* unsigned int (1) chroma_subsampling_y; */ |
719 | 44.9k | bs_write(&bs, 2, p_seq->color_config.chroma_sample_position); /* unsigned int (2) chroma_sample_position; */ |
720 | | |
721 | 44.9k | bs_write(&bs, 3, 0); /* unsigned int (3) reserved = 0; */ |
722 | 44.9k | bs_write(&bs, 1, 0); /* unsigned int (1) initial_presentation_delay_present; (can't compute it) */ |
723 | 44.9k | bs_write(&bs, 4, 0); /* unsigned int (4) reserved = 0; */ |
724 | | |
725 | | /*unsigned int (8)[] configOBUs;*/ |
726 | 44.9k | size_t i_offset = 4; |
727 | 89.9k | for(size_t i=0; i<i_obu; i++) |
728 | 44.9k | memcpy(&p_buffer[i_offset], p_obus[i], pi_obus[i]); |
729 | | |
730 | 44.9k | *pp_buffer = p_buffer; |
731 | 44.9k | return i_buffer; |
732 | 44.9k | } |
733 | | |
734 | | bool AV1_sequence_header_equal(const av1_OBU_sequence_header_t *seq1,const av1_OBU_sequence_header_t *seq2) |
735 | 66.5k | { |
736 | 66.5k | #define DIFF(field) \ |
737 | 7.86M | seq1->field != seq2->field || |
738 | | |
739 | 66.5k | if ( |
740 | 66.5k | DIFF(obu_header.obu_type) |
741 | 66.5k | DIFF(obu_header.temporal_id) |
742 | 66.5k | DIFF(obu_header.spatial_id) |
743 | 66.5k | DIFF(seq_profile) |
744 | 54.0k | DIFF(still_picture) |
745 | 52.3k | DIFF(reduced_still_picture_header) |
746 | 50.8k | DIFF(timing_info_present_flag) |
747 | 50.6k | DIFF(timing_info.num_units_in_display_tick) |
748 | 49.8k | DIFF(timing_info.time_scale) |
749 | 49.1k | DIFF(timing_info.equal_picture_interval) |
750 | 49.1k | DIFF(timing_info.num_ticks_per_picture_minus_1) |
751 | 49.0k | DIFF(decoder_model_info_present_flag) |
752 | 49.0k | DIFF(decoder_model_info.buffer_delay_length_minus_1) |
753 | 48.9k | DIFF(decoder_model_info.num_units_in_decoding_tick) |
754 | 48.1k | DIFF(decoder_model_info.buffer_removal_time_length_minus_1) |
755 | 48.1k | DIFF(decoder_model_info.frame_presentation_time_length_minus_1) |
756 | 48.1k | DIFF(initial_display_delay_present_flag) |
757 | 47.9k | DIFF(operating_points_cnt_minus_1) |
758 | 47.6k | DIFF(max_frame_width_minus_1) |
759 | 40.6k | DIFF(max_frame_height_minus_1) |
760 | 37.3k | DIFF(frame_id_numbers_present_flag) |
761 | 36.9k | DIFF(delta_frame_id_length_minus_2) |
762 | 36.9k | DIFF(additional_frame_id_length_minus_1) |
763 | 36.8k | DIFF(use_128x128_superblock) |
764 | 36.2k | DIFF(enable_filter_intra) |
765 | 36.1k | DIFF(enable_intra_edge_filter) |
766 | | |
767 | 32.6k | DIFF(enable_interintra_compound) |
768 | 32.2k | DIFF(enable_masked_compound) |
769 | 30.9k | DIFF(enable_warped_motion) |
770 | 30.6k | DIFF(enable_dual_filter) |
771 | 30.5k | DIFF(enable_order_hint) |
772 | 30.3k | DIFF(enable_jnt_comp) |
773 | 30.3k | DIFF(enable_ref_frame_mvs) |
774 | 30.2k | DIFF(seq_force_screen_content_tools) |
775 | 30.0k | DIFF(seq_force_integer_mv) |
776 | 30.0k | DIFF(order_hint_bits_minus_1) |
777 | | |
778 | 29.9k | DIFF(enable_superres) |
779 | 27.4k | DIFF(enable_cdef) |
780 | 25.1k | DIFF(enable_restoration) |
781 | 25.1k | DIFF(color_config.high_bitdepth) |
782 | 24.2k | DIFF(color_config.twelve_bit) |
783 | 24.2k | DIFF(color_config.mono_chrome) |
784 | 24.0k | DIFF(color_config.color_description_present_flag) |
785 | 23.9k | DIFF(color_config.color_primaries) |
786 | 22.2k | DIFF(color_config.transfer_characteristics) |
787 | 22.0k | DIFF(color_config.matrix_coefficients) |
788 | 21.9k | DIFF(color_config.color_range) |
789 | 21.9k | DIFF(color_config.subsampling_x) |
790 | 21.9k | DIFF(color_config.subsampling_y) |
791 | 21.9k | DIFF(color_config.chroma_sample_position) |
792 | 20.8k | DIFF(color_config.separate_uv_delta_q) |
793 | 20.6k | DIFF(color_config.i_chroma) |
794 | 20.6k | DIFF(film_grain_params_present) |
795 | 66.5k | false |
796 | 66.5k | ) |
797 | 45.8k | return false; |
798 | | |
799 | 674k | for (size_t i=0; i<ARRAY_SIZE(seq1->operating_points); i++) |
800 | 654k | { |
801 | 654k | if ( |
802 | 654k | DIFF(operating_points[i].operating_point_idc) |
803 | 654k | DIFF(operating_points[i].seq_level_idx) |
804 | 654k | DIFF(operating_points[i].seq_tier) |
805 | 654k | DIFF(operating_points[i].decoder_model_present_for_this_op) |
806 | 654k | DIFF(operating_points[i].operating_parameters_info.decoder_buffer_delay) |
807 | 654k | DIFF(operating_points[i].operating_parameters_info.encoder_buffer_delay) |
808 | 654k | DIFF(operating_points[i].operating_parameters_info.low_delay_mode_flag) |
809 | 654k | DIFF(operating_points[i].initial_display_delay_present_for_this_op) |
810 | 654k | DIFF(operating_points[i].initial_display_delay_minus_1) |
811 | 654k | false |
812 | 654k | ) |
813 | 319 | return false; |
814 | 654k | } |
815 | | |
816 | 20.3k | return true; |
817 | 20.6k | } |