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