1 /*
2 * Copyright (c) 2018, Alliance for Open Media. All rights reserved.
3 *
4 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10 */
11 #include <stdio.h>
12 #include <string.h>
13
14 #include "aom/aom_image.h"
15 #include "aom/aom_integer.h"
16 #include "aom_dsp/bitreader_buffer.h"
17 #include "av1/common/obu_util.h"
18 #include "common/av1_config.h"
19 #include "config/aom_config.h"
20
21 // Helper macros to reduce verbosity required to check for read errors.
22 //
23 // Note that when using these macros, even single line if statements should use
24 // curly braces to avoid unexpected behavior because all but the
25 // AV1C_POP_ERROR_HANDLER_DATA() macro consist of multiple statements.
26 #define AV1C_READ_BIT_OR_RETURN_ERROR(field) \
27 int field = 0; \
28 do { \
29 field = aom_rb_read_bit(reader); \
30 if (result == -1) { \
31 fprintf(stderr, \
32 "av1c: Error reading bit for " #field ", value=%d result=%d.\n", \
33 field, result); \
34 return -1; \
35 } \
36 } while (0)
37
38 #define AV1C_READ_BITS_OR_RETURN_ERROR(field, length) \
39 int field = 0; \
40 do { \
41 field = aom_rb_read_literal(reader, (length)); \
42 if (result == -1) { \
43 fprintf(stderr, \
44 "av1c: Could not read bits for " #field \
45 ", value=%d result=%d.\n", \
46 field, result); \
47 return -1; \
48 } \
49 } while (0)
50
51 // Helper macros for setting/restoring the error handler data in
52 // aom_read_bit_buffer.
53 #define AV1C_PUSH_ERROR_HANDLER_DATA(new_data) \
54 void *original_error_handler_data = NULL; \
55 do { \
56 original_error_handler_data = reader->error_handler_data; \
57 reader->error_handler_data = &new_data; \
58 } while (0)
59
60 #define AV1C_POP_ERROR_HANDLER_DATA() \
61 do { \
62 reader->error_handler_data = original_error_handler_data; \
63 } while (0)
64
65 static const size_t kAv1cSize = 4;
66
bitreader_error_handler(void * data)67 static void bitreader_error_handler(void *data) {
68 int *error_val = (int *)data;
69 *error_val = -1;
70 }
71
72 // Parse the AV1 timing_info() structure:
73 // timing_info( ) {
74 // num_units_in_display_tick f(32)
75 // time_scale f(32)
76 // equal_picture_interval f(1)
77 // if (equal_picture_interval)
78 // num_ticks_per_picture_minus_1 uvlc()
79 // }
parse_timing_info(struct aom_read_bit_buffer * reader)80 static int parse_timing_info(struct aom_read_bit_buffer *reader) {
81 int result = 0;
82 AV1C_PUSH_ERROR_HANDLER_DATA(result);
83
84 AV1C_READ_BITS_OR_RETURN_ERROR(num_units_in_display_tick, 32);
85 AV1C_READ_BITS_OR_RETURN_ERROR(time_scale, 32);
86
87 AV1C_READ_BIT_OR_RETURN_ERROR(equal_picture_interval);
88 if (equal_picture_interval) {
89 uint32_t num_ticks_per_picture_minus_1 = aom_rb_read_uvlc(reader);
90 if (result == -1) {
91 fprintf(stderr,
92 "av1c: Could not read bits for "
93 "num_ticks_per_picture_minus_1, value=%u.\n",
94 num_ticks_per_picture_minus_1);
95 return result;
96 }
97 }
98
99 AV1C_POP_ERROR_HANDLER_DATA();
100 return result;
101 }
102
103 // Parse the AV1 decoder_model_info() structure:
104 // decoder_model_info( ) {
105 // buffer_delay_length_minus_1 f(5)
106 // num_units_in_decoding_tick f(32)
107 // buffer_removal_time_length_minus_1 f(5)
108 // frame_presentation_time_length_minus_1 f(5)
109 // }
110 //
111 // Returns -1 upon failure, or the value of buffer_delay_length_minus_1 + 1.
parse_decoder_model_info(struct aom_read_bit_buffer * reader)112 static int parse_decoder_model_info(struct aom_read_bit_buffer *reader) {
113 int result = 0;
114 AV1C_PUSH_ERROR_HANDLER_DATA(result);
115
116 AV1C_READ_BITS_OR_RETURN_ERROR(buffer_delay_length_minus_1, 5);
117 AV1C_READ_BITS_OR_RETURN_ERROR(num_units_in_decoding_tick, 32);
118 AV1C_READ_BITS_OR_RETURN_ERROR(buffer_removal_time_length_minus_1, 5);
119 AV1C_READ_BITS_OR_RETURN_ERROR(frame_presentation_time_length_minus_1, 5);
120
121 AV1C_POP_ERROR_HANDLER_DATA();
122 return buffer_delay_length_minus_1 + 1;
123 }
124
125 // Parse the AV1 operating_parameters_info() structure:
126 // operating_parameters_info( op ) {
127 // n = buffer_delay_length_minus_1 + 1
128 // decoder_buffer_delay[ op ] f(n)
129 // encoder_buffer_delay[ op ] f(n)
130 // low_delay_mode_flag[ op ] f(1)
131 // }
parse_operating_parameters_info(struct aom_read_bit_buffer * reader,int buffer_delay_length_minus_1)132 static int parse_operating_parameters_info(struct aom_read_bit_buffer *reader,
133 int buffer_delay_length_minus_1) {
134 int result = 0;
135 AV1C_PUSH_ERROR_HANDLER_DATA(result);
136
137 const int buffer_delay_length = buffer_delay_length_minus_1 + 1;
138 AV1C_READ_BITS_OR_RETURN_ERROR(decoder_buffer_delay, buffer_delay_length);
139 AV1C_READ_BITS_OR_RETURN_ERROR(encoder_buffer_delay, buffer_delay_length);
140 AV1C_READ_BIT_OR_RETURN_ERROR(low_delay_mode_flag);
141
142 AV1C_POP_ERROR_HANDLER_DATA();
143 return result;
144 }
145
146 // Parse the AV1 color_config() structure..See:
147 // https://aomediacodec.github.io/av1-spec/av1-spec.pdf#page=44
parse_color_config(struct aom_read_bit_buffer * reader,Av1Config * config)148 static int parse_color_config(struct aom_read_bit_buffer *reader,
149 Av1Config *config) {
150 int result = 0;
151 AV1C_PUSH_ERROR_HANDLER_DATA(result);
152
153 AV1C_READ_BIT_OR_RETURN_ERROR(high_bitdepth);
154 config->high_bitdepth = high_bitdepth;
155
156 int bit_depth = 0;
157 if (config->seq_profile == 2 && config->high_bitdepth) {
158 AV1C_READ_BIT_OR_RETURN_ERROR(twelve_bit);
159 config->twelve_bit = twelve_bit;
160 bit_depth = config->twelve_bit ? 12 : 10;
161 } else {
162 bit_depth = config->high_bitdepth ? 10 : 8;
163 }
164
165 if (config->seq_profile != 1) {
166 AV1C_READ_BIT_OR_RETURN_ERROR(mono_chrome);
167 config->monochrome = mono_chrome;
168 }
169
170 int color_primaries = AOM_CICP_CP_UNSPECIFIED;
171 int transfer_characteristics = AOM_CICP_TC_UNSPECIFIED;
172 int matrix_coefficients = AOM_CICP_MC_UNSPECIFIED;
173
174 AV1C_READ_BIT_OR_RETURN_ERROR(color_description_present_flag);
175 if (color_description_present_flag) {
176 AV1C_READ_BITS_OR_RETURN_ERROR(color_primaries_val, 8);
177 color_primaries = color_primaries_val;
178 AV1C_READ_BITS_OR_RETURN_ERROR(transfer_characteristics_val, 8);
179 transfer_characteristics = transfer_characteristics_val;
180 AV1C_READ_BITS_OR_RETURN_ERROR(matrix_coefficients_val, 8);
181 matrix_coefficients = matrix_coefficients_val;
182 }
183
184 if (config->monochrome) {
185 AV1C_READ_BIT_OR_RETURN_ERROR(color_range);
186 config->chroma_subsampling_x = 1;
187 config->chroma_subsampling_y = 1;
188 } else if (color_primaries == AOM_CICP_CP_BT_709 &&
189 transfer_characteristics == AOM_CICP_TC_SRGB &&
190 matrix_coefficients == AOM_CICP_MC_IDENTITY) {
191 config->chroma_subsampling_x = 0;
192 config->chroma_subsampling_y = 0;
193 } else {
194 AV1C_READ_BIT_OR_RETURN_ERROR(color_range);
195 if (config->seq_profile == 0) {
196 config->chroma_subsampling_x = 1;
197 config->chroma_subsampling_y = 1;
198 } else if (config->seq_profile == 1) {
199 config->chroma_subsampling_x = 0;
200 config->chroma_subsampling_y = 0;
201 } else {
202 if (bit_depth == 12) {
203 AV1C_READ_BIT_OR_RETURN_ERROR(subsampling_x);
204 config->chroma_subsampling_x = subsampling_x;
205 if (subsampling_x) {
206 AV1C_READ_BIT_OR_RETURN_ERROR(subsampling_y);
207 config->chroma_subsampling_y = subsampling_y;
208 } else {
209 config->chroma_subsampling_y = 0;
210 }
211 } else {
212 config->chroma_subsampling_x = 1;
213 config->chroma_subsampling_y = 0;
214 }
215 }
216
217 if (config->chroma_subsampling_x && config->chroma_subsampling_y) {
218 AV1C_READ_BITS_OR_RETURN_ERROR(chroma_sample_position, 2);
219 config->chroma_sample_position = chroma_sample_position;
220 }
221 }
222
223 if (!config->monochrome) {
224 AV1C_READ_BIT_OR_RETURN_ERROR(separate_uv_delta_q);
225 }
226
227 AV1C_POP_ERROR_HANDLER_DATA();
228 return result;
229 }
230
231 // Parse AV1 Sequence Header OBU. See:
232 // https://aomediacodec.github.io/av1-spec/av1-spec.pdf#page=41
parse_sequence_header(const uint8_t * const buffer,size_t length,Av1Config * config)233 static int parse_sequence_header(const uint8_t *const buffer, size_t length,
234 Av1Config *config) {
235 int result = 0;
236 // The reader instance is local to this function, but a pointer to the
237 // reader instance is used within this function and throughout this file to
238 // allow use of the helper macros that reduce parse error checking verbosity.
239 struct aom_read_bit_buffer reader_instance = { buffer, buffer + length, 0,
240 &result,
241 bitreader_error_handler };
242 struct aom_read_bit_buffer *reader = &reader_instance;
243
244 AV1C_READ_BITS_OR_RETURN_ERROR(seq_profile, 3);
245 config->seq_profile = seq_profile;
246 AV1C_READ_BIT_OR_RETURN_ERROR(still_picture);
247 AV1C_READ_BIT_OR_RETURN_ERROR(reduced_still_picture_header);
248 if (reduced_still_picture_header) {
249 config->initial_presentation_delay_present = 0;
250 AV1C_READ_BITS_OR_RETURN_ERROR(seq_level_idx_0, 5);
251 config->seq_level_idx_0 = seq_level_idx_0;
252 config->seq_tier_0 = 0;
253 } else {
254 int has_decoder_model = 0;
255 int buffer_delay_length = 0;
256
257 AV1C_READ_BIT_OR_RETURN_ERROR(timing_info_present_flag);
258 if (timing_info_present_flag) {
259 if (parse_timing_info(reader) != 0) return -1;
260
261 AV1C_READ_BIT_OR_RETURN_ERROR(decoder_model_info_present_flag);
262 if (decoder_model_info_present_flag &&
263 (buffer_delay_length = parse_decoder_model_info(reader)) == -1) {
264 return -1;
265 }
266 has_decoder_model = 1;
267 }
268
269 AV1C_READ_BIT_OR_RETURN_ERROR(initial_presentation_delay_present);
270 config->initial_presentation_delay_present =
271 initial_presentation_delay_present;
272
273 AV1C_READ_BITS_OR_RETURN_ERROR(operating_points_cnt_minus_1, 5);
274 const int num_operating_points = operating_points_cnt_minus_1 + 1;
275
276 for (int op_index = 0; op_index < num_operating_points; ++op_index) {
277 AV1C_READ_BITS_OR_RETURN_ERROR(operating_point_idc, 12);
278 AV1C_READ_BITS_OR_RETURN_ERROR(seq_level_idx, 5);
279
280 int seq_tier = 0;
281 if (seq_level_idx > 7) {
282 AV1C_READ_BIT_OR_RETURN_ERROR(seq_tier_this_op);
283 seq_tier = seq_tier_this_op;
284 }
285
286 if (has_decoder_model) {
287 AV1C_READ_BIT_OR_RETURN_ERROR(decoder_model_present_for_op);
288 if (decoder_model_present_for_op) {
289 if (parse_operating_parameters_info(reader, buffer_delay_length) ==
290 -1) {
291 return -1;
292 }
293 }
294 }
295
296 if (config->initial_presentation_delay_present) {
297 // Skip the initial presentation delay bits if present since this
298 // function has no access to the data required to properly set the
299 // field.
300 AV1C_READ_BIT_OR_RETURN_ERROR(
301 initial_presentation_delay_present_for_this_op);
302 if (initial_presentation_delay_present_for_this_op) {
303 AV1C_READ_BITS_OR_RETURN_ERROR(initial_presentation_delay_minus_1, 4);
304 }
305 }
306
307 if (op_index == 0) {
308 // Av1Config needs only the values from the first operating point.
309 config->seq_level_idx_0 = seq_level_idx;
310 config->seq_tier_0 = seq_tier;
311 config->initial_presentation_delay_present = 0;
312 config->initial_presentation_delay_minus_one = 0;
313 }
314 }
315 }
316
317 AV1C_READ_BITS_OR_RETURN_ERROR(frame_width_bits_minus_1, 4);
318 AV1C_READ_BITS_OR_RETURN_ERROR(frame_height_bits_minus_1, 4);
319 AV1C_READ_BITS_OR_RETURN_ERROR(max_frame_width_minus_1,
320 frame_width_bits_minus_1 + 1);
321 AV1C_READ_BITS_OR_RETURN_ERROR(max_frame_height_minus_1,
322 frame_height_bits_minus_1 + 1);
323
324 uint8_t frame_id_numbers_present = 0;
325 if (!reduced_still_picture_header) {
326 AV1C_READ_BIT_OR_RETURN_ERROR(frame_id_numbers_present_flag);
327 frame_id_numbers_present = frame_id_numbers_present_flag;
328 }
329
330 if (frame_id_numbers_present) {
331 AV1C_READ_BITS_OR_RETURN_ERROR(delta_frame_id_length_minus_2, 4);
332 AV1C_READ_BITS_OR_RETURN_ERROR(additional_frame_id_length_minus_1, 3);
333 }
334
335 AV1C_READ_BIT_OR_RETURN_ERROR(use_128x128_superblock);
336 AV1C_READ_BIT_OR_RETURN_ERROR(enable_filter_intra);
337 AV1C_READ_BIT_OR_RETURN_ERROR(enable_intra_edge_filter);
338
339 if (!reduced_still_picture_header) {
340 AV1C_READ_BIT_OR_RETURN_ERROR(enable_interintra_compound);
341 AV1C_READ_BIT_OR_RETURN_ERROR(enable_masked_compound);
342 AV1C_READ_BIT_OR_RETURN_ERROR(enable_warped_motion);
343 AV1C_READ_BIT_OR_RETURN_ERROR(enable_dual_filter);
344
345 AV1C_READ_BIT_OR_RETURN_ERROR(enable_order_hint);
346 if (enable_order_hint) {
347 AV1C_READ_BIT_OR_RETURN_ERROR(enable_dist_wtd_comp);
348 AV1C_READ_BIT_OR_RETURN_ERROR(enable_ref_frame_mvs);
349 }
350
351 const int SELECT_SCREEN_CONTENT_TOOLS = 2;
352 int seq_force_screen_content_tools = SELECT_SCREEN_CONTENT_TOOLS;
353 AV1C_READ_BIT_OR_RETURN_ERROR(seq_choose_screen_content_tools);
354 if (!seq_choose_screen_content_tools) {
355 AV1C_READ_BIT_OR_RETURN_ERROR(seq_force_screen_content_tools_val);
356 seq_force_screen_content_tools = seq_force_screen_content_tools_val;
357 }
358
359 if (seq_force_screen_content_tools > 0) {
360 AV1C_READ_BIT_OR_RETURN_ERROR(seq_choose_integer_mv);
361
362 if (!seq_choose_integer_mv) {
363 AV1C_READ_BIT_OR_RETURN_ERROR(seq_force_integer_mv);
364 }
365 }
366
367 if (enable_order_hint) {
368 AV1C_READ_BITS_OR_RETURN_ERROR(order_hint_bits_minus_1, 3);
369 }
370 }
371
372 AV1C_READ_BIT_OR_RETURN_ERROR(enable_superres);
373 AV1C_READ_BIT_OR_RETURN_ERROR(enable_cdef);
374 AV1C_READ_BIT_OR_RETURN_ERROR(enable_restoration);
375
376 if (parse_color_config(reader, config) != 0) {
377 fprintf(stderr, "av1c: color_config() parse failed.\n");
378 return -1;
379 }
380
381 AV1C_READ_BIT_OR_RETURN_ERROR(film_grain_params_present);
382 return 0;
383 }
384
get_av1config_from_obu(const uint8_t * buffer,size_t length,int is_annexb,Av1Config * config)385 int get_av1config_from_obu(const uint8_t *buffer, size_t length, int is_annexb,
386 Av1Config *config) {
387 if (!buffer || length == 0 || !config) {
388 return -1;
389 }
390
391 ObuHeader obu_header;
392 memset(&obu_header, 0, sizeof(obu_header));
393
394 size_t sequence_header_length = 0;
395 size_t obu_header_length = 0;
396 if (aom_read_obu_header_and_size(buffer, length, is_annexb, &obu_header,
397 &sequence_header_length,
398 &obu_header_length) != AOM_CODEC_OK ||
399 obu_header.type != OBU_SEQUENCE_HEADER ||
400 sequence_header_length + obu_header_length > length) {
401 return -1;
402 }
403
404 memset(config, 0, sizeof(*config));
405 config->marker = 1;
406 config->version = 1;
407 return parse_sequence_header(buffer + obu_header_length,
408 sequence_header_length, config);
409 }
410
read_av1config(const uint8_t * buffer,size_t buffer_length,size_t * bytes_read,Av1Config * config)411 int read_av1config(const uint8_t *buffer, size_t buffer_length,
412 size_t *bytes_read, Av1Config *config) {
413 if (!buffer || buffer_length < kAv1cSize || !bytes_read || !config) return -1;
414
415 *bytes_read = 0;
416
417 int result = 0;
418 struct aom_read_bit_buffer reader_instance = { buffer, buffer + buffer_length,
419 0, &result,
420 bitreader_error_handler };
421 struct aom_read_bit_buffer *reader = &reader_instance;
422
423 memset(config, 0, sizeof(*config));
424
425 AV1C_READ_BIT_OR_RETURN_ERROR(marker);
426 config->marker = marker;
427
428 AV1C_READ_BITS_OR_RETURN_ERROR(version, 7);
429 config->version = version;
430
431 AV1C_READ_BITS_OR_RETURN_ERROR(seq_profile, 3);
432 config->seq_profile = seq_profile;
433
434 AV1C_READ_BITS_OR_RETURN_ERROR(seq_level_idx_0, 5);
435 config->seq_level_idx_0 = seq_level_idx_0;
436
437 AV1C_READ_BIT_OR_RETURN_ERROR(seq_tier_0);
438 config->seq_tier_0 = seq_tier_0;
439
440 AV1C_READ_BIT_OR_RETURN_ERROR(high_bitdepth);
441 config->high_bitdepth = high_bitdepth;
442
443 AV1C_READ_BIT_OR_RETURN_ERROR(twelve_bit);
444 config->twelve_bit = twelve_bit;
445
446 AV1C_READ_BIT_OR_RETURN_ERROR(monochrome);
447 config->monochrome = monochrome;
448
449 AV1C_READ_BIT_OR_RETURN_ERROR(chroma_subsampling_x);
450 config->chroma_subsampling_x = chroma_subsampling_x;
451
452 AV1C_READ_BIT_OR_RETURN_ERROR(chroma_subsampling_y);
453 config->chroma_subsampling_y = chroma_subsampling_y;
454
455 AV1C_READ_BITS_OR_RETURN_ERROR(chroma_sample_position, 2);
456 config->chroma_sample_position = chroma_sample_position;
457
458 AV1C_READ_BITS_OR_RETURN_ERROR(reserved, 3);
459
460 AV1C_READ_BIT_OR_RETURN_ERROR(initial_presentation_delay_present);
461 config->initial_presentation_delay_present =
462 initial_presentation_delay_present;
463
464 AV1C_READ_BITS_OR_RETURN_ERROR(initial_presentation_delay_minus_one, 4);
465 config->initial_presentation_delay_minus_one =
466 initial_presentation_delay_minus_one;
467
468 *bytes_read = aom_rb_bytes_read(reader);
469
470 return 0;
471 }
472
write_av1config(const Av1Config * config,size_t capacity,size_t * bytes_written,uint8_t * buffer)473 int write_av1config(const Av1Config *config, size_t capacity,
474 size_t *bytes_written, uint8_t *buffer) {
475 if (!config || !buffer || capacity < kAv1cSize || !bytes_written) return -1;
476
477 buffer[0] = (config->marker << 7) | config->version;
478 buffer[1] = (config->seq_profile << 5) | config->seq_level_idx_0;
479 buffer[2] = (config->seq_tier_0 << 7) | (config->high_bitdepth << 6) |
480 (config->twelve_bit << 5) | (config->monochrome << 4) |
481 (config->chroma_subsampling_x << 3) |
482 (config->chroma_subsampling_y << 2) |
483 config->chroma_sample_position;
484 buffer[3] = config->initial_presentation_delay_present << 4;
485 if (config->initial_presentation_delay_present) {
486 buffer[3] |= config->initial_presentation_delay_minus_one;
487 }
488
489 *bytes_written = kAv1cSize;
490 return 0;
491 }
492
493 #undef AV1C_READ_BIT_OR_RETURN_ERROR
494 #undef AV1C_READ_BITS_OR_RETURN_ERROR
495 #undef AV1C_PUSH_ERROR_HANDLER_DATA
496 #undef AV1C_POP_ERROR_HANDLER_DATA
497