av1_config.c 19 KB

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