7 #include "packager/media/codecs/av1_parser.h"
11 #include "packager/base/logging.h"
12 #include "packager/media/base/bit_reader.h"
13 #include "packager/media/base/rcheck.h"
27 const int kSelectScreenContentTools = 2;
28 const int kSelectIntegerMv = 2;
29 const int kPrimaryRefNone = 7;
30 const int kNumRefFrames = 8;
31 const int kAllFrames = (1 << kNumRefFrames) - 1;
35 OBU_SEQUENCE_HEADER = 1,
36 OBU_TEMPORAL_DELIMITER,
41 OBU_REDUNDENT_FRAME_HEADER,
53 enum TransferCharacteristics {
58 enum MatrixCoefficients {
63 enum ChromaSamplePosition {
91 int Clip3(
int min_value,
int max_value,
int value) {
92 if (value < min_value)
94 if (value > max_value)
101 int FloorLog2(
int x) {
111 bool ReadUvlc(BitReader* reader, uint32_t* val) {
113 int leading_zeros = 0;
116 RCHECK(reader->ReadBits(1, &done));
122 if (leading_zeros >= 32) {
123 *val = (1ull << 32) - 1;
128 if (leading_zeros > 0)
129 RCHECK(reader->ReadBits(leading_zeros, &value));
131 *val = value + (1 << leading_zeros) - 1;
137 bool ReadLe(
int n, BitReader* reader,
size_t* val) {
139 for (
int i = 0; i < n; i++) {
141 RCHECK(reader->ReadBits(8, &
byte));
142 t += (
byte << (i * 8));
150 bool ReadLeb128(BitReader* reader,
size_t* size) {
152 for (
int i = 0; i < 8; i++) {
153 size_t leb128_byte = 0;
154 RCHECK(reader->ReadBits(8, &leb128_byte));
155 value |= (leb128_byte & 0x7f) << (i * 7);
156 if (!(leb128_byte & 0x80))
161 RCHECK(value <= ((1ull << 32) - 1));
168 bool ReadSu(
int n, BitReader* reader,
int* value) {
169 RCHECK(reader->ReadBits(n, value));
170 int sign_mask = 1 << (n - 1);
171 if (*value & sign_mask)
172 *value = *value - 2 * sign_mask;
178 bool ReadNs(
int n, BitReader* reader,
int* value) {
179 const int w = FloorLog2(n) + 1;
180 const int m = (1 << w) - n;
181 RCHECK(reader->ReadBits(w - 1, value));
185 RCHECK(reader->ReadBits(1, &extra_bit));
186 *value = (*value << 1) - m + extra_bit;
192 int TileLog2(
int blk_size,
int target) {
194 for (k = 0; (blk_size << k) < target; k++)
200 int FindLatestBackward(
int shifted_order_hints[],
202 int cur_frame_hint) {
204 int latest_order_hint = 0;
205 for (
int i = 0; i < kNumRefFrames; i++) {
206 const int hint = shifted_order_hints[i];
207 if (!used_frame[i] && hint >= cur_frame_hint &&
208 (ref < 0 || hint >= latest_order_hint)) {
210 latest_order_hint = hint;
217 int FindEarliestBackward(
int shifted_order_hints[],
219 int cur_frame_hint) {
221 int earliest_order_hint = 0;
222 for (
int i = 0; i < kNumRefFrames; i++) {
223 const int hint = shifted_order_hints[i];
224 if (!used_frame[i] && hint >= cur_frame_hint &&
225 (ref < 0 || hint < earliest_order_hint)) {
227 earliest_order_hint = hint;
234 int FindLatestForward(
int shifted_order_hints[],
236 int cur_frame_hint) {
238 int latest_order_hint = 0;
239 for (
int i = 0; i < kNumRefFrames; i++) {
240 const int hint = shifted_order_hints[i];
241 if (!used_frame[i] && hint < cur_frame_hint &&
242 (ref < 0 || hint >= latest_order_hint)) {
244 latest_order_hint = hint;
252 AV1Parser::AV1Parser() =
default;
253 AV1Parser::~AV1Parser() =
default;
257 std::vector<Tile>* tiles) {
262 if (!ParseOpenBitstreamUnit(&reader, tiles))
269 bool AV1Parser::ParseOpenBitstreamUnit(
BitReader* reader,
270 std::vector<Tile>* tiles) {
271 ObuHeader obu_header;
272 RCHECK(ParseObuHeader(reader, &obu_header));
275 if (obu_header.obu_has_size_field)
276 RCHECK(ReadLeb128(reader, &obu_size));
280 VLOG(4) <<
"OBU " << obu_header.obu_type <<
" size " << obu_size;
283 switch (obu_header.obu_type) {
284 case OBU_SEQUENCE_HEADER:
285 RCHECK(ParseSequenceHeaderObu(reader));
287 case OBU_FRAME_HEADER:
288 case OBU_REDUNDENT_FRAME_HEADER:
289 RCHECK(ParseFrameHeaderObu(obu_header, reader));
292 RCHECK(ParseTileGroupObu(obu_size, reader, tiles));
295 RCHECK(ParseFrameObu(obu_header, obu_size, reader, tiles));
299 RCHECK(reader->
SkipBits(obu_size * 8));
304 const size_t payload_bits = current_position - start_position;
305 if (obu_header.obu_type == OBU_TILE_GROUP ||
306 obu_header.obu_type == OBU_FRAME) {
307 RCHECK(payload_bits == obu_size * 8);
308 }
else if (obu_size > 0) {
309 RCHECK(payload_bits <= obu_size * 8);
310 RCHECK(ParseTrailingBits(obu_size * 8 - payload_bits, reader));
316 bool AV1Parser::ParseObuHeader(BitReader* reader, ObuHeader* obu_header) {
317 int obu_forbidden_bit = 0;
318 RCHECK(reader->ReadBits(1, &obu_forbidden_bit));
319 RCHECK(obu_forbidden_bit == 0);
320 RCHECK(reader->ReadBits(4, &obu_header->obu_type));
321 bool obu_extension_flag =
false;
322 RCHECK(reader->ReadBits(1, &obu_extension_flag));
323 RCHECK(reader->ReadBits(1, &obu_header->obu_has_size_field));
324 RCHECK(reader->SkipBits(1));
326 if (obu_extension_flag)
327 RCHECK(ParseObuExtensionHeader(reader, &obu_header->extension_header));
333 bool AV1Parser::ParseObuExtensionHeader(
335 ObuExtensionHeader* obu_extension_header) {
336 RCHECK(reader->ReadBits(3, &obu_extension_header->temporal_id));
337 RCHECK(reader->ReadBits(2, &obu_extension_header->spatial_id));
338 RCHECK(reader->SkipBits(3));
343 bool AV1Parser::ParseTrailingBits(
size_t nb_bits, BitReader* reader) {
344 int trailing_one_bit = 0;
345 RCHECK(reader->ReadBits(1, &trailing_one_bit));
346 RCHECK(trailing_one_bit == 1);
348 while (nb_bits > 0) {
349 int trailing_zero_bit = 0;
350 RCHECK(reader->ReadBits(1, &trailing_zero_bit));
351 RCHECK(trailing_zero_bit == 0);
357 bool AV1Parser::ByteAlignment(BitReader* reader) {
358 while (reader->bit_position() & 7) {
360 RCHECK(reader->ReadBits(1, &zero_bit));
361 RCHECK(zero_bit == 0);
367 bool AV1Parser::ParseSequenceHeaderObu(BitReader* reader) {
368 RCHECK(reader->ReadBits(3, &sequence_header_.seq_profile));
370 RCHECK(reader->SkipBits(1));
372 RCHECK(reader->ReadBits(1, &sequence_header_.reduced_still_picture_header));
373 if (sequence_header_.reduced_still_picture_header) {
374 sequence_header_.decoder_model_info_present_flag =
false;
375 sequence_header_.operating_points_cnt_minus_1 = 0;
376 sequence_header_.operating_point_idc[0] = 0;
378 RCHECK(reader->SkipBits(5));
379 sequence_header_.decoder_model_present_for_this_op[0] =
false;
381 bool timing_info_present_flag =
false;
382 RCHECK(reader->ReadBits(1, &timing_info_present_flag));
384 bool decoder_model_info_present_flag =
false;
385 if (timing_info_present_flag) {
386 RCHECK(ParseTimingInfo(reader));
387 RCHECK(reader->ReadBits(1, &decoder_model_info_present_flag));
388 if (decoder_model_info_present_flag)
389 RCHECK(ParseDecoderModelInfo(reader));
391 sequence_header_.decoder_model_info_present_flag =
392 decoder_model_info_present_flag;
394 bool initial_display_delay_present_flag =
false;
395 RCHECK(reader->ReadBits(1, &initial_display_delay_present_flag));
397 RCHECK(reader->ReadBits(5, &sequence_header_.operating_points_cnt_minus_1));
398 for (
int i = 0; i <= sequence_header_.operating_points_cnt_minus_1; i++) {
399 RCHECK(reader->ReadBits(12, &sequence_header_.operating_point_idc[i]));
400 int seq_level_idx_i = 0;
401 RCHECK(reader->ReadBits(5, &seq_level_idx_i));
402 if (seq_level_idx_i > 7) {
404 RCHECK(reader->SkipBits(1));
407 if (sequence_header_.decoder_model_info_present_flag) {
408 RCHECK(reader->ReadBits(
409 1, &sequence_header_.decoder_model_present_for_this_op[i]));
410 if (sequence_header_.decoder_model_present_for_this_op[i]) {
411 RCHECK(SkipOperatingParametersInfo(reader));
414 sequence_header_.decoder_model_present_for_this_op[i] =
false;
417 if (initial_display_delay_present_flag) {
420 RCHECK(reader->SkipBitsConditional(
true, 4));
425 RCHECK(reader->ReadBits(4, &sequence_header_.frame_width_bits_minus_1));
426 RCHECK(reader->ReadBits(4, &sequence_header_.frame_height_bits_minus_1));
427 RCHECK(reader->ReadBits(sequence_header_.frame_width_bits_minus_1 + 1,
428 &sequence_header_.max_frame_width_minus_1));
429 RCHECK(reader->ReadBits(sequence_header_.frame_height_bits_minus_1 + 1,
430 &sequence_header_.max_frame_height_minus_1));
432 if (sequence_header_.reduced_still_picture_header) {
433 sequence_header_.frame_id_numbers_present_flag =
false;
436 reader->ReadBits(1, &sequence_header_.frame_id_numbers_present_flag));
438 if (sequence_header_.frame_id_numbers_present_flag) {
440 reader->ReadBits(4, &sequence_header_.delta_frame_id_length_minus_2));
441 RCHECK(reader->ReadBits(
442 3, &sequence_header_.additional_frame_id_length_minus_1));
445 RCHECK(reader->ReadBits(1, &sequence_header_.use_128x128_superblock));
447 RCHECK(reader->SkipBits(1 + 1));
449 if (sequence_header_.reduced_still_picture_header) {
450 sequence_header_.enable_warped_motion =
false;
451 sequence_header_.enable_order_hint =
false;
452 sequence_header_.enable_ref_frame_mvs =
false;
453 sequence_header_.order_hint_bits = 0;
454 sequence_header_.seq_force_screen_content_tools = kSelectScreenContentTools;
455 sequence_header_.seq_force_integer_mv = kSelectIntegerMv;
458 RCHECK(reader->SkipBits(1 + 1));
460 RCHECK(reader->ReadBits(1, &sequence_header_.enable_warped_motion));
461 RCHECK(reader->SkipBits(1));
462 RCHECK(reader->ReadBits(1, &sequence_header_.enable_order_hint));
463 if (sequence_header_.enable_order_hint) {
465 RCHECK(reader->SkipBits(1));
466 RCHECK(reader->ReadBits(1, &sequence_header_.enable_ref_frame_mvs));
468 sequence_header_.enable_ref_frame_mvs =
false;
471 bool seq_choose_screen_content_tools =
false;
472 RCHECK(reader->ReadBits(1, &seq_choose_screen_content_tools));
474 if (seq_choose_screen_content_tools) {
475 sequence_header_.seq_force_screen_content_tools =
476 kSelectScreenContentTools;
478 RCHECK(reader->ReadBits(
479 1, &sequence_header_.seq_force_screen_content_tools));
482 if (sequence_header_.seq_force_screen_content_tools > 0) {
483 bool seq_choose_integer_mv =
false;
484 RCHECK(reader->ReadBits(1, &seq_choose_integer_mv));
485 if (seq_choose_integer_mv)
486 sequence_header_.seq_force_integer_mv = kSelectIntegerMv;
488 RCHECK(reader->ReadBits(1, &sequence_header_.seq_force_integer_mv));
490 sequence_header_.seq_force_integer_mv = kSelectIntegerMv;
493 if (sequence_header_.enable_order_hint) {
494 int order_hint_bits_minus_1 = 0;
495 RCHECK(reader->ReadBits(3, &order_hint_bits_minus_1));
496 sequence_header_.order_hint_bits = order_hint_bits_minus_1 + 1;
498 sequence_header_.order_hint_bits = 0;
502 RCHECK(reader->ReadBits(1, &sequence_header_.enable_superres));
503 RCHECK(reader->ReadBits(1, &sequence_header_.enable_cdef));
504 RCHECK(reader->ReadBits(1, &sequence_header_.enable_restoration));
505 RCHECK(ParseColorConfig(reader));
506 RCHECK(reader->ReadBits(1, &sequence_header_.film_grain_params_present));
511 bool AV1Parser::ParseColorConfig(BitReader* reader) {
512 ColorConfig& color_config = sequence_header_.color_config;
514 bool high_bitdepth =
false;
515 RCHECK(reader->ReadBits(1, &high_bitdepth));
516 if (sequence_header_.seq_profile == 2 && high_bitdepth) {
517 bool twelve_bit =
false;
518 RCHECK(reader->ReadBits(1, &twelve_bit));
519 color_config.bit_depth = twelve_bit ? 12 : 10;
520 }
else if (sequence_header_.seq_profile <= 2) {
521 color_config.bit_depth = high_bitdepth ? 10 : 8;
524 if (sequence_header_.seq_profile == 1)
525 color_config.mono_chrome = 0;
527 RCHECK(reader->ReadBits(1, &color_config.mono_chrome));
528 color_config.num_planes = color_config.mono_chrome ? 1 : 3;
530 bool color_description_present_flag =
false;
531 RCHECK(reader->ReadBits(1, &color_description_present_flag));
533 if (color_description_present_flag) {
534 RCHECK(reader->ReadBits(8, &color_config.color_primaries));
535 RCHECK(reader->ReadBits(8, &color_config.transfer_chracteristics));
536 RCHECK(reader->ReadBits(8, &color_config.matrix_coefficients));
538 color_config.color_primaries = CP_UNSPECIFIED;
539 color_config.transfer_chracteristics = TC_UNSPECIFIED;
540 color_config.matrix_coefficients = MC_UNSPECIFIED;
543 if (color_config.mono_chrome) {
544 RCHECK(reader->ReadBits(1, &color_config.color_range));
545 color_config.subsampling_x =
true;
546 color_config.subsampling_y =
true;
547 color_config.chroma_sampling_position = CSP_UNKNOWN;
548 color_config.separate_uv_delta_q =
false;
550 }
else if (color_config.color_primaries == CP_BT_709 &&
551 color_config.transfer_chracteristics == TC_SRGB &&
552 color_config.matrix_coefficients == MC_IDENTITY) {
553 color_config.color_range =
true;
554 color_config.subsampling_x =
false;
555 color_config.subsampling_y =
false;
557 RCHECK(reader->ReadBits(1, &color_config.color_range));
558 if (sequence_header_.seq_profile == 0) {
559 color_config.subsampling_x =
true;
560 color_config.subsampling_y =
true;
561 }
else if (sequence_header_.seq_profile == 1) {
562 color_config.subsampling_x =
false;
563 color_config.subsampling_y =
false;
565 if (color_config.bit_depth == 12) {
566 RCHECK(reader->ReadBits(1, &color_config.subsampling_x));
567 if (color_config.subsampling_x)
568 RCHECK(reader->ReadBits(1, &color_config.subsampling_y));
570 color_config.subsampling_y =
false;
572 color_config.subsampling_x =
true;
573 color_config.subsampling_y =
false;
577 if (color_config.subsampling_x && color_config.subsampling_y)
578 RCHECK(reader->ReadBits(2, &color_config.chroma_sampling_position));
581 RCHECK(reader->ReadBits(1, &color_config.separate_uv_delta_q));
586 bool AV1Parser::ParseTimingInfo(BitReader* reader) {
588 RCHECK(reader->SkipBits(32 + 32));
589 bool equal_picture_interval =
false;
590 RCHECK(reader->ReadBits(1, &equal_picture_interval));
591 sequence_header_.timing_info.equal_picture_interval = equal_picture_interval;
592 if (equal_picture_interval) {
593 uint32_t num_ticks_per_picture_minus_1 = 0;
594 RCHECK(ReadUvlc(reader, &num_ticks_per_picture_minus_1));
600 bool AV1Parser::ParseDecoderModelInfo(BitReader* reader) {
601 DecoderModelInfo& decoder_model_info = sequence_header_.decoder_model_info;
603 RCHECK(reader->ReadBits(5, &decoder_model_info.buffer_delay_length_minus_1));
605 RCHECK(reader->SkipBits(32));
606 RCHECK(reader->ReadBits(
607 5, &decoder_model_info.buffer_removal_time_length_minus_1));
608 RCHECK(reader->ReadBits(
609 5, &decoder_model_info.frame_presentation_time_length_minus_1));
614 bool AV1Parser::SkipOperatingParametersInfo(BitReader* reader) {
616 sequence_header_.decoder_model_info.buffer_delay_length_minus_1 + 1;
619 RCHECK(reader->SkipBits(n + n + 1));
624 bool AV1Parser::ParseFrameHeaderObu(
const ObuHeader& obu_header,
626 if (frame_header_.seen_frame_header)
629 frame_header_.seen_frame_header =
true;
630 RCHECK(ParseUncompressedHeader(obu_header, reader));
631 if (frame_header_.show_existing_frame) {
633 frame_header_.seen_frame_header =
false;
635 frame_header_.seen_frame_header =
true;
641 bool AV1Parser::ParseUncompressedHeader(
const ObuHeader& obu_header,
644 if (sequence_header_.frame_id_numbers_present_flag) {
645 id_len = sequence_header_.additional_frame_id_length_minus_1 + 1 +
646 sequence_header_.delta_frame_id_length_minus_2 + 2;
649 bool frame_is_intra =
false;
650 bool show_frame =
false;
651 bool showable_frame =
false;
652 bool error_resilient_mode =
false;
654 if (sequence_header_.reduced_still_picture_header) {
655 frame_header_.show_existing_frame =
false;
656 frame_header_.frame_type = KEY_FRAME;
657 frame_is_intra =
true;
659 showable_frame =
false;
661 RCHECK(reader->ReadBits(1, &frame_header_.show_existing_frame));
662 if (frame_header_.show_existing_frame) {
663 RCHECK(reader->ReadBits(3, &frame_header_.frame_to_show_map_idx));
664 if (sequence_header_.decoder_model_info_present_flag &&
665 !sequence_header_.timing_info.equal_picture_interval) {
666 RCHECK(SkipTemporalPointInfo(reader));
668 frame_header_.refresh_frame_flags = 0;
669 if (sequence_header_.frame_id_numbers_present_flag) {
671 RCHECK(reader->SkipBits(id_len));
673 frame_header_.frame_type =
674 reference_frames_[frame_header_.frame_to_show_map_idx].frame_type;
675 if (frame_header_.frame_type == KEY_FRAME) {
676 frame_header_.refresh_frame_flags = kAllFrames;
681 RCHECK(reader->ReadBits(2, &frame_header_.frame_type));
682 frame_is_intra = frame_header_.frame_type == INTRA_ONLY_FRAME ||
683 frame_header_.frame_type == KEY_FRAME;
684 RCHECK(reader->ReadBits(1, &show_frame));
685 if (show_frame && sequence_header_.decoder_model_info_present_flag &&
686 !sequence_header_.timing_info.equal_picture_interval) {
687 RCHECK(SkipTemporalPointInfo(reader));
690 showable_frame = frame_header_.frame_type != KEY_FRAME;
692 RCHECK(reader->ReadBits(1, &showable_frame));
694 if (frame_header_.frame_type == SWITCH_FRAME ||
695 (frame_header_.frame_type == KEY_FRAME && show_frame)) {
696 error_resilient_mode =
true;
698 RCHECK(reader->ReadBits(1, &error_resilient_mode));
702 if (frame_header_.frame_type == KEY_FRAME && show_frame) {
703 for (
int i = 0; i < kNumRefFrames; i++) {
704 reference_frames_[i].order_hint = 0;
708 bool disable_cdf_update =
false;
709 RCHECK(reader->ReadBits(1, &disable_cdf_update));
711 bool allow_screen_content_tools =
false;
712 if (sequence_header_.seq_force_screen_content_tools ==
713 kSelectScreenContentTools) {
714 RCHECK(reader->ReadBits(1, &allow_screen_content_tools));
716 allow_screen_content_tools =
717 sequence_header_.seq_force_screen_content_tools != 0;
720 int force_integer_mv = 0;
721 if (allow_screen_content_tools) {
722 if (sequence_header_.seq_force_integer_mv == kSelectIntegerMv)
723 RCHECK(reader->ReadBits(1, &force_integer_mv));
725 force_integer_mv = sequence_header_.seq_force_integer_mv;
728 force_integer_mv = 1;
730 if (sequence_header_.frame_id_numbers_present_flag) {
732 RCHECK(reader->SkipBits(id_len));
735 bool frame_size_override_flag =
false;
736 if (frame_header_.frame_type == SWITCH_FRAME)
737 frame_size_override_flag =
true;
738 else if (sequence_header_.reduced_still_picture_header)
739 frame_size_override_flag =
false;
741 RCHECK(reader->ReadBits(1, &frame_size_override_flag));
743 RCHECK(reader->ReadBits(sequence_header_.order_hint_bits,
744 &frame_header_.order_hint));
745 int primary_ref_frame = 0;
746 if (frame_is_intra || error_resilient_mode) {
747 primary_ref_frame = kPrimaryRefNone;
749 RCHECK(reader->ReadBits(3, &primary_ref_frame));
751 if (sequence_header_.decoder_model_info_present_flag) {
752 bool buffer_removal_time_present_flag =
false;
753 RCHECK(reader->ReadBits(1, &buffer_removal_time_present_flag));
754 if (buffer_removal_time_present_flag) {
756 op_num <= sequence_header_.operating_points_cnt_minus_1; op_num++) {
757 if (sequence_header_.decoder_model_present_for_this_op[op_num]) {
758 const int op_pt_idc = sequence_header_.operating_point_idc[op_num];
759 const int in_temporal_layer =
760 (op_pt_idc >> obu_header.extension_header.temporal_id) & 1;
761 const int in_spatial_layer =
762 (op_pt_idc >> (obu_header.extension_header.spatial_id + 8)) & 1;
763 if (op_pt_idc == 0 || (in_temporal_layer && in_spatial_layer)) {
765 RCHECK(reader->SkipBits(sequence_header_.decoder_model_info
766 .buffer_removal_time_length_minus_1 +
774 bool allow_high_precision_mv =
false;
775 bool allow_intrabc =
false;
777 if (frame_header_.frame_type == SWITCH_FRAME ||
778 (frame_header_.frame_type == KEY_FRAME && show_frame)) {
779 frame_header_.refresh_frame_flags = kAllFrames;
781 RCHECK(reader->ReadBits(8, &frame_header_.refresh_frame_flags));
783 if (!frame_is_intra || frame_header_.refresh_frame_flags != kAllFrames) {
784 if (error_resilient_mode && sequence_header_.enable_order_hint) {
785 for (
int i = 0; i < kNumRefFrames; i++) {
787 RCHECK(reader->SkipBits(sequence_header_.order_hint_bits));
792 if (frame_is_intra) {
793 RCHECK(ParseFrameSize(frame_size_override_flag, reader));
794 RCHECK(ParseRenderSize(reader));
795 if (allow_screen_content_tools &&
796 frame_header_.upscaled_width == frame_header_.frame_width)
797 RCHECK(reader->ReadBits(1, &allow_intrabc));
799 bool frame_refs_short_signaling =
false;
800 if (sequence_header_.enable_order_hint) {
801 RCHECK(reader->ReadBits(1, &frame_refs_short_signaling));
802 if (frame_refs_short_signaling) {
803 int last_frame_idx = 0;
804 RCHECK(reader->ReadBits(3, &last_frame_idx));
805 int gold_frame_idx = 0;
806 RCHECK(reader->ReadBits(3, &gold_frame_idx));
807 RCHECK(SetFrameRefs(last_frame_idx, gold_frame_idx));
810 for (
int i = 0; i < kRefsPerFrame; i++) {
811 if (!frame_refs_short_signaling)
812 RCHECK(reader->ReadBits(3, &frame_header_.ref_frame_idx[i]));
813 if (sequence_header_.frame_id_numbers_present_flag) {
815 RCHECK(reader->SkipBits(sequence_header_.delta_frame_id_length_minus_2 +
819 if (frame_size_override_flag && !error_resilient_mode) {
820 RCHECK(ParseFrameSizeWithRefs(frame_size_override_flag, reader));
822 RCHECK(ParseFrameSize(frame_size_override_flag, reader));
823 RCHECK(ParseRenderSize(reader));
826 if (force_integer_mv)
827 allow_high_precision_mv =
false;
829 RCHECK(reader->ReadBits(1, &allow_high_precision_mv));
831 RCHECK(SkipInterpolationFilter(reader));
833 RCHECK(reader->SkipBits(1));
834 if (!error_resilient_mode && sequence_header_.enable_ref_frame_mvs) {
836 RCHECK(reader->SkipBits(1));
840 if (!sequence_header_.reduced_still_picture_header && !disable_cdf_update) {
842 RCHECK(reader->SkipBits(1));
845 RCHECK(ParseTileInfo(reader));
846 RCHECK(ParseQuantizationParams(reader));
847 RCHECK(ParseSegmentationParams(primary_ref_frame, reader));
849 bool delta_q_present =
false;
850 RCHECK(SkipDeltaQParams(reader, &delta_q_present));
851 RCHECK(SkipDeltaLfParams(delta_q_present, allow_intrabc, reader));
853 const auto& quantization_params = frame_header_.quantization_params;
854 bool coded_lossless =
true;
855 for (
int segment_id = 0; segment_id < kMaxSegments; segment_id++) {
856 const int qindex = GetQIndex(
true, segment_id);
857 const bool lossless = qindex == 0 && quantization_params.delta_qydc == 0 &&
858 quantization_params.delta_quac == 0 &&
859 quantization_params.delta_qudc == 0 &&
860 quantization_params.delta_qvac == 0 &&
861 quantization_params.delta_qvdc == 0;
863 coded_lossless =
false;
865 const bool all_lossless = coded_lossless && (frame_header_.frame_width ==
866 frame_header_.upscaled_width);
868 RCHECK(ParseLoopFilterParams(coded_lossless, allow_intrabc, reader));
869 RCHECK(ParseCdefParams(coded_lossless, allow_intrabc, reader));
870 RCHECK(ParseLrParams(all_lossless, allow_intrabc, reader));
871 RCHECK(SkipTxMode(coded_lossless, reader));
872 bool reference_select =
false;
873 RCHECK(ParseFrameReferenceMode(frame_is_intra, reader, &reference_select));
874 RCHECK(SkipSkipModeParams(frame_is_intra, reference_select, reader));
876 bool allow_warped_motion =
false;
877 if (frame_is_intra || error_resilient_mode ||
878 !sequence_header_.enable_warped_motion) {
879 allow_warped_motion =
false;
881 RCHECK(reader->ReadBits(1, &allow_warped_motion));
884 RCHECK(reader->SkipBits(1));
887 SkipGlobalMotionParams(frame_is_intra, allow_high_precision_mv, reader));
888 RCHECK(SkipFilmGrainParams(show_frame, showable_frame, reader));
893 int AV1Parser::GetRelativeDist(
int a,
int b) {
894 if (!sequence_header_.enable_order_hint)
897 const int m = 1 << (sequence_header_.order_hint_bits - 1);
898 diff = (diff & (m - 1)) - (diff & m);
903 bool AV1Parser::ParseFrameSize(
bool frame_size_override_flag,
905 if (frame_size_override_flag) {
906 int frame_width_minus_1 = 0;
907 RCHECK(reader->ReadBits(sequence_header_.frame_width_bits_minus_1 + 1,
908 &frame_width_minus_1));
909 int frame_height_minus_1 = 0;
910 RCHECK(reader->ReadBits(sequence_header_.frame_height_bits_minus_1 + 1,
911 &frame_height_minus_1));
912 frame_header_.frame_width = frame_width_minus_1 + 1;
913 frame_header_.frame_height = frame_height_minus_1 + 1;
915 frame_header_.frame_width = sequence_header_.max_frame_width_minus_1 + 1;
916 frame_header_.frame_height = sequence_header_.max_frame_height_minus_1 + 1;
918 RCHECK(ParseSuperresParams(reader));
924 bool AV1Parser::ParseRenderSize(BitReader* reader) {
925 bool render_and_frame_size_different =
false;
926 RCHECK(reader->ReadBits(1, &render_and_frame_size_different));
927 if (render_and_frame_size_different) {
928 int render_width_minus_1 = 0;
929 RCHECK(reader->ReadBits(16, &render_width_minus_1));
930 int render_height_minus_1 = 0;
931 RCHECK(reader->ReadBits(16, &render_height_minus_1));
932 frame_header_.render_width = render_width_minus_1 + 1;
933 frame_header_.render_height = render_height_minus_1 + 1;
935 frame_header_.render_width = frame_header_.upscaled_width;
936 frame_header_.render_height = frame_header_.frame_height;
942 bool AV1Parser::ParseFrameSizeWithRefs(
bool frame_size_override_flag,
944 bool found_ref =
false;
945 for (
int i = 0; i < kRefsPerFrame; i++) {
946 RCHECK(reader->ReadBits(1, &found_ref));
948 const ReferenceFrame& reference_frame =
949 reference_frames_[frame_header_.ref_frame_idx[i]];
950 frame_header_.upscaled_width = reference_frame.upscaled_width;
951 frame_header_.frame_width = frame_header_.upscaled_width;
952 frame_header_.frame_height = reference_frame.frame_height;
953 frame_header_.render_width = reference_frame.render_width;
954 frame_header_.render_height = reference_frame.render_height;
959 RCHECK(ParseFrameSize(frame_size_override_flag, reader));
960 RCHECK(ParseRenderSize(reader));
962 RCHECK(ParseSuperresParams(reader));
969 bool AV1Parser::ParseSuperresParams(BitReader* reader) {
970 const int kSuperresNum = 8;
971 const int kSuperresDenomMin = 9;
972 const int kSuperresDenomBits = 3;
974 bool use_superres =
false;
975 if (sequence_header_.enable_superres)
976 RCHECK(reader->ReadBits(1, &use_superres));
978 int superres_denom = 0;
981 RCHECK(reader->ReadBits(kSuperresDenomBits, &coded_denom));
982 superres_denom = coded_denom + kSuperresDenomMin;
984 superres_denom = kSuperresNum;
987 const int upscaled_width = frame_header_.frame_width;
988 frame_header_.upscaled_width =
989 (upscaled_width * kSuperresNum + superres_denom / 2) / superres_denom;
994 void AV1Parser::ComputeImageSize() {
995 frame_header_.mi_cols = 2 * ((frame_header_.frame_width + 7) >> 3);
996 frame_header_.mi_rows = 2 * ((frame_header_.frame_height + 7) >> 3);
1000 bool AV1Parser::SkipInterpolationFilter(BitReader* reader) {
1002 RCHECK(reader->SkipBitsConditional(
false, 2));
1007 bool AV1Parser::ParseLoopFilterParams(
bool coded_lossless,
1009 BitReader* reader) {
1010 if (coded_lossless || allow_intrabc)
1013 int loop_filter_level[] = {0, 0};
1014 RCHECK(reader->ReadBits(6, &loop_filter_level[0]));
1015 RCHECK(reader->ReadBits(6, &loop_filter_level[1]));
1016 if (sequence_header_.color_config.num_planes > 1) {
1017 if (loop_filter_level[0] || loop_filter_level[1]) {
1019 RCHECK(reader->SkipBits(6 + 6));
1023 RCHECK(reader->SkipBits(3));
1024 bool loop_filter_delta_enabled =
false;
1025 RCHECK(reader->ReadBits(1, &loop_filter_delta_enabled));
1026 if (loop_filter_delta_enabled) {
1027 bool loop_filter_delta_update =
false;
1028 RCHECK(reader->ReadBits(1, &loop_filter_delta_update));
1029 if (loop_filter_delta_update) {
1030 const int kTotalRefsPerFrame = 8;
1031 for (
int i = 0; i < kTotalRefsPerFrame; i++) {
1033 RCHECK(reader->SkipBitsConditional(
true, 1 + 6));
1035 for (
int i = 0; i < 2; i++) {
1037 RCHECK(reader->SkipBitsConditional(
true, 1 + 6));
1045 bool AV1Parser::ParseQuantizationParams(BitReader* reader) {
1046 QuantizationParams& quantization_params = frame_header_.quantization_params;
1048 RCHECK(reader->ReadBits(8, &quantization_params.base_q_idx));
1049 RCHECK(ReadDeltaQ(reader, &quantization_params.delta_qydc));
1051 const ColorConfig& color_config = sequence_header_.color_config;
1052 if (color_config.num_planes > 1) {
1053 bool diff_uv_delta =
false;
1054 if (color_config.separate_uv_delta_q)
1055 RCHECK(reader->ReadBits(1, &diff_uv_delta));
1056 RCHECK(ReadDeltaQ(reader, &quantization_params.delta_qudc));
1057 RCHECK(ReadDeltaQ(reader, &quantization_params.delta_quac));
1058 if (diff_uv_delta) {
1059 RCHECK(ReadDeltaQ(reader, &quantization_params.delta_qvdc));
1060 RCHECK(ReadDeltaQ(reader, &quantization_params.delta_qvac));
1062 quantization_params.delta_qvdc = quantization_params.delta_qudc;
1063 quantization_params.delta_qvac = quantization_params.delta_quac;
1066 quantization_params.delta_qudc = 0;
1067 quantization_params.delta_quac = 0;
1068 quantization_params.delta_qvdc = 0;
1069 quantization_params.delta_qvac = 0;
1071 bool using_qmatrix =
false;
1072 RCHECK(reader->ReadBits(1, &using_qmatrix));
1073 if (using_qmatrix) {
1075 RCHECK(reader->SkipBits(4 + 4));
1076 if (color_config.separate_uv_delta_q) {
1078 RCHECK(reader->SkipBits(4));
1085 bool AV1Parser::ReadDeltaQ(BitReader* reader,
int* delta_q) {
1086 bool delta_coded =
false;
1087 RCHECK(reader->ReadBits(1, &delta_coded));
1089 RCHECK(ReadSu(1 + 6, reader, delta_q));
1096 bool AV1Parser::ParseSegmentationParams(
int primary_ref_frame,
1097 BitReader* reader) {
1098 SegmentationParams& segmentation_params = frame_header_.segmentation_params;
1100 RCHECK(reader->ReadBits(1, &segmentation_params.segmentation_enabled));
1101 if (segmentation_params.segmentation_enabled) {
1102 bool segmentation_update_data =
false;
1103 if (primary_ref_frame == kPrimaryRefNone) {
1104 segmentation_update_data =
true;
1107 RCHECK(reader->SkipBitsConditional(
true, 1));
1108 RCHECK(reader->ReadBits(1, &segmentation_update_data));
1110 if (segmentation_update_data) {
1111 static const int kSegmentationFeatureBits[kSegLvlMax] = {8, 6, 6, 6,
1113 static const int kSegmentationFeatureSigned[kSegLvlMax] = {1, 1, 1, 1,
1115 const int kMaxLoopFilter = 63;
1116 static const int kSegmentationFeatureMax[kSegLvlMax] = {255,
1125 for (
int i = 0; i < kMaxSegments; i++) {
1126 for (
int j = 0; j < kSegLvlMax; j++) {
1127 bool feature_enabled =
false;
1128 RCHECK(reader->ReadBits(1, &feature_enabled));
1129 segmentation_params.feature_enabled[i][j] = feature_enabled;
1130 int clipped_value = 0;
1131 if (feature_enabled) {
1132 const int bits_to_read = kSegmentationFeatureBits[j];
1133 const int limit = kSegmentationFeatureMax[j];
1134 if (kSegmentationFeatureSigned[j]) {
1135 int feature_value = 0;
1136 RCHECK(ReadSu(1 + bits_to_read, reader, &feature_value));
1137 clipped_value = Clip3(-limit, limit, feature_value);
1139 int feature_value = 0;
1140 RCHECK(reader->ReadBits(bits_to_read, &feature_value));
1141 clipped_value = Clip3(0, limit, feature_value);
1144 segmentation_params.feature_data[i][j] = clipped_value;
1149 for (
int i = 0; i < kMaxSegments; i++) {
1150 for (
int j = 0; j < kSegLvlMax; j++) {
1151 segmentation_params.feature_enabled[i][j] =
false;
1152 segmentation_params.feature_data[i][j] = 0;
1160 bool AV1Parser::ParseTileInfo(BitReader* reader) {
1161 const int kMaxTileWidth = 4096;
1162 const int kMaxTileArea = 4096 * 2304;
1163 const int kMaxTileRows = 64;
1164 const int kMaxTileCols = 64;
1166 TileInfo& tile_info = frame_header_.tile_info;
1168 const int sb_cols = sequence_header_.use_128x128_superblock
1169 ? ((frame_header_.mi_cols + 31) >> 5)
1170 : ((frame_header_.mi_cols + 15) >> 4);
1171 const int sb_rows = sequence_header_.use_128x128_superblock
1172 ? ((frame_header_.mi_rows + 31) >> 5)
1173 : ((frame_header_.mi_rows + 15) >> 4);
1174 const int sb_shift = sequence_header_.use_128x128_superblock ? 5 : 4;
1175 const int sb_size = sb_shift + 2;
1176 const int max_tile_width_sb = kMaxTileWidth >> sb_size;
1177 int max_tile_area_sb = kMaxTileArea >> (2 * sb_size);
1178 const int min_log2_tile_cols = TileLog2(max_tile_width_sb, sb_cols);
1179 const int max_log2_tile_cols = TileLog2(1, std::min(sb_cols, kMaxTileCols));
1180 const int max_log2_tile_rows = TileLog2(1, std::min(sb_rows, kMaxTileRows));
1181 const int min_log2_tiles = std::max(
1182 min_log2_tile_cols, TileLog2(max_tile_area_sb, sb_rows * sb_cols));
1184 bool uniform_tile_spacing_flag =
false;
1185 RCHECK(reader->ReadBits(1, &uniform_tile_spacing_flag));
1186 if (uniform_tile_spacing_flag) {
1187 tile_info.tile_cols_log2 = min_log2_tile_cols;
1188 while (tile_info.tile_cols_log2 < max_log2_tile_cols) {
1189 bool increment_tile_cols_log2 =
false;
1190 RCHECK(reader->ReadBits(1, &increment_tile_cols_log2));
1191 if (increment_tile_cols_log2)
1192 tile_info.tile_cols_log2++;
1196 const int tile_width_sb = (sb_cols + (1 << tile_info.tile_cols_log2) - 1) >>
1197 tile_info.tile_cols_log2;
1199 for (
int start_sb = 0; start_sb < sb_cols; start_sb += tile_width_sb) {
1202 tile_info.tile_cols = i;
1204 const int min_log2_tile_rows =
1205 std::max(min_log2_tiles - tile_info.tile_cols_log2, 0);
1206 tile_info.tile_rows_log2 = min_log2_tile_rows;
1207 while (tile_info.tile_rows_log2 < max_log2_tile_rows) {
1208 bool increment_tile_rows_log2 =
false;
1209 RCHECK(reader->ReadBits(1, &increment_tile_rows_log2));
1210 if (increment_tile_rows_log2)
1211 tile_info.tile_rows_log2++;
1215 const int tile_height_sb =
1216 (sb_rows + (1 << tile_info.tile_rows_log2) - 1) >>
1217 tile_info.tile_rows_log2;
1219 for (
int start_sb = 0; start_sb < sb_rows; start_sb += tile_height_sb) {
1222 tile_info.tile_rows = i;
1224 int widest_tile_sb = 0;
1227 for (; start_sb < sb_cols; i++) {
1228 const int max_width = std::min(sb_cols - start_sb, max_tile_width_sb);
1229 int width_in_sbs_minus_1 = 0;
1230 RCHECK(ReadNs(max_width, reader, &width_in_sbs_minus_1));
1231 const int size_sb = width_in_sbs_minus_1 + 1;
1232 widest_tile_sb = std::max(size_sb, widest_tile_sb);
1233 start_sb += size_sb;
1235 tile_info.tile_cols = i;
1236 tile_info.tile_cols_log2 = TileLog2(1, tile_info.tile_cols);
1238 if (min_log2_tiles > 0)
1239 max_tile_area_sb = (sb_rows * sb_cols) >> (min_log2_tiles + 1);
1241 max_tile_area_sb = sb_rows * sb_cols;
1242 const int max_tile_height_sb =
1243 std::max(max_tile_area_sb / widest_tile_sb, 1);
1247 for (; start_sb < sb_rows; i++) {
1248 const int max_height = std::min(sb_rows - start_sb, max_tile_height_sb);
1249 int height_in_sbs_minus_1 = 0;
1250 RCHECK(ReadNs(max_height, reader, &height_in_sbs_minus_1));
1251 const int size_sb = height_in_sbs_minus_1 + 1;
1252 start_sb += size_sb;
1254 tile_info.tile_rows = i;
1255 tile_info.tile_rows_log2 = TileLog2(1, tile_info.tile_rows);
1257 if (tile_info.tile_cols_log2 > 0 || tile_info.tile_rows_log2 > 0) {
1260 reader->SkipBits(tile_info.tile_rows_log2 + tile_info.tile_cols_log2));
1261 int tile_size_bytes_minus_1 = 0;
1262 RCHECK(reader->ReadBits(2, &tile_size_bytes_minus_1));
1263 tile_info.tile_size_bytes = tile_size_bytes_minus_1 + 1;
1269 bool AV1Parser::SkipDeltaQParams(BitReader* reader,
bool* delta_q_present) {
1270 *delta_q_present =
false;
1271 if (frame_header_.quantization_params.base_q_idx > 0)
1272 RCHECK(reader->ReadBits(1, delta_q_present));
1273 if (*delta_q_present) {
1275 RCHECK(reader->SkipBits(2));
1281 bool AV1Parser::SkipDeltaLfParams(
bool delta_q_present,
1283 BitReader* reader) {
1284 bool delta_lf_present =
false;
1285 if (delta_q_present) {
1287 RCHECK(reader->ReadBits(1, &delta_lf_present));
1288 if (delta_lf_present) {
1290 RCHECK(reader->SkipBits(2 + 1));
1297 bool AV1Parser::ParseCdefParams(
bool coded_lossless,
1299 BitReader* reader) {
1300 if (coded_lossless || allow_intrabc || !sequence_header_.enable_cdef)
1304 RCHECK(reader->SkipBits(2));
1306 RCHECK(reader->ReadBits(2, &cdef_bits));
1307 for (
int i = 0; i < (1 << cdef_bits); i++) {
1309 RCHECK(reader->SkipBits(4 + 2));
1310 if (sequence_header_.color_config.num_planes > 1) {
1312 RCHECK(reader->SkipBits(4 + 2));
1319 bool AV1Parser::ParseLrParams(
bool all_lossless,
1321 BitReader* reader) {
1322 if (all_lossless || allow_intrabc || !sequence_header_.enable_restoration)
1325 enum FrameRestorationType {
1327 RESTORE_SWITCHABLE = 3,
1329 RESTORE_SGRPROJ = 2,
1331 static const int kRemapLrType[4] = {RESTORE_NONE, RESTORE_SWITCHABLE,
1332 RESTORE_WIENER, RESTORE_SGRPROJ};
1333 bool uses_lr =
false;
1334 bool uses_chroma_lr =
false;
1335 for (
int i = 0; i < sequence_header_.color_config.num_planes; i++) {
1337 RCHECK(reader->ReadBits(2, &lr_type));
1338 const int frame_restoration_type = kRemapLrType[lr_type];
1339 if (frame_restoration_type != RESTORE_NONE) {
1342 uses_chroma_lr =
true;
1347 if (sequence_header_.use_128x128_superblock) {
1349 RCHECK(reader->SkipBits(1));
1352 RCHECK(reader->SkipBitsConditional(
true, 1));
1354 if (sequence_header_.color_config.subsampling_x &&
1355 sequence_header_.color_config.subsampling_y && uses_chroma_lr) {
1357 RCHECK(reader->SkipBits(1));
1364 bool AV1Parser::SkipTxMode(
bool coded_lossless, BitReader* reader) {
1365 if (!coded_lossless) {
1367 RCHECK(reader->SkipBits(1));
1373 bool AV1Parser::SkipSkipModeParams(
bool frame_is_intra,
1374 bool reference_select,
1375 BitReader* reader) {
1376 bool skip_mode_allowed =
false;
1377 if (frame_is_intra || !reference_select ||
1378 !sequence_header_.enable_order_hint) {
1379 skip_mode_allowed =
false;
1381 int forward_idx = -1;
1382 int forward_hint = 0;
1383 int backward_idx = -1;
1384 int backward_hint = 0;
1385 for (
int i = 0; i < kRefsPerFrame; i++) {
1386 const int ref_hint =
1387 reference_frames_[frame_header_.ref_frame_idx[i]].order_hint;
1388 if (GetRelativeDist(ref_hint, frame_header_.order_hint) < 0) {
1389 if (forward_idx < 0 || GetRelativeDist(ref_hint, forward_hint) > 0) {
1391 forward_hint = ref_hint;
1393 }
else if (GetRelativeDist(ref_hint, frame_header_.order_hint) > 0) {
1394 if (backward_idx < 0 || GetRelativeDist(ref_hint, backward_hint) < 0) {
1396 backward_hint = ref_hint;
1400 if (forward_idx < 0) {
1401 skip_mode_allowed =
false;
1402 }
else if (backward_idx >= 0) {
1403 skip_mode_allowed =
true;
1405 int second_forward_idx = -1;
1406 int second_forward_hint = 0;
1407 for (
int i = 0; i < kRefsPerFrame; i++) {
1408 const int ref_hint =
1409 reference_frames_[frame_header_.ref_frame_idx[i]].order_hint;
1410 if (GetRelativeDist(ref_hint, forward_hint) < 0) {
1411 if (second_forward_idx < 0 ||
1412 GetRelativeDist(ref_hint, second_forward_hint) > 0) {
1413 second_forward_idx = i;
1414 second_forward_hint = ref_hint;
1418 skip_mode_allowed = second_forward_idx >= 0;
1422 if (skip_mode_allowed) {
1424 RCHECK(reader->SkipBits(1));
1430 bool AV1Parser::ParseFrameReferenceMode(
bool frame_is_intra,
1432 bool* reference_select) {
1434 *reference_select =
false;
1436 RCHECK(reader->ReadBits(1, reference_select));
1441 bool AV1Parser::SkipGlobalMotionParams(
bool frame_is_intra,
1442 bool allow_high_precision_mv,
1443 BitReader* reader) {
1447 for (
int ref = LAST_FRAME; ref <= ALTREF_FRAME; ref++) {
1450 bool is_global =
false;
1451 RCHECK(reader->ReadBits(1, &is_global));
1453 bool is_rot_zoom =
false;
1454 RCHECK(reader->ReadBits(1, &is_rot_zoom));
1458 bool is_translation =
false;
1459 RCHECK(reader->ReadBits(1, &is_translation));
1460 type = is_translation ? TRANSLATION : AFFINE;
1466 if (type >= ROTZOOM) {
1467 RCHECK(SkipGlobalParam(type, ref, 2, allow_high_precision_mv, reader));
1468 RCHECK(SkipGlobalParam(type, ref, 3, allow_high_precision_mv, reader));
1469 if (type == AFFINE) {
1470 RCHECK(SkipGlobalParam(type, ref, 4, allow_high_precision_mv, reader));
1471 RCHECK(SkipGlobalParam(type, ref, 5, allow_high_precision_mv, reader));
1474 if (type >= TRANSLATION) {
1475 RCHECK(SkipGlobalParam(type, ref, 0, allow_high_precision_mv, reader));
1476 RCHECK(SkipGlobalParam(type, ref, 1, allow_high_precision_mv, reader));
1483 bool AV1Parser::SkipGlobalParam(
int type,
1486 bool allow_high_precision_mv,
1487 BitReader* reader) {
1488 const int kGmAbsTransBits = 12;
1489 const int kGmAbsTransOnlyBits = 9;
1490 const int kGmAbsAlphaBits = 12;
1492 int abs_bits = kGmAbsAlphaBits;
1494 if (type == TRANSLATION) {
1495 abs_bits = kGmAbsTransOnlyBits - (allow_high_precision_mv ? 0 : 1);
1497 abs_bits = kGmAbsTransBits;
1500 const int mx = 1 << abs_bits;
1501 RCHECK(SkipDecodeSignedSubexpWithRef(-mx, mx + 1, reader));
1506 bool AV1Parser::SkipDecodeSignedSubexpWithRef(
int low,
1508 BitReader* reader) {
1509 RCHECK(SkipDecodeUnsignedSubexpWithRef(high - low, reader));
1514 bool AV1Parser::SkipDecodeUnsignedSubexpWithRef(
int mx, BitReader* reader) {
1515 RCHECK(SkipDecodeSubexp(mx, reader));
1520 bool AV1Parser::SkipDecodeSubexp(
int num_syms, BitReader* reader) {
1525 const int b2 = i ? (k + i - 1) : k;
1526 const int a = 1 << b2;
1527 if (num_syms <= mk + 3 * a) {
1528 int subexp_final_bits = 0;
1529 RCHECK(ReadNs(num_syms - mk, reader, &subexp_final_bits));
1532 bool subexp_more_bits =
false;
1533 RCHECK(reader->ReadBits(1, &subexp_more_bits));
1534 if (subexp_more_bits) {
1539 RCHECK(reader->SkipBits(b2));
1548 bool AV1Parser::SkipFilmGrainParams(
bool show_frame,
1549 bool showable_frame,
1550 BitReader* reader) {
1551 if (!sequence_header_.film_grain_params_present ||
1552 (!show_frame && !showable_frame)) {
1556 bool apply_grain =
false;
1557 RCHECK(reader->ReadBits(1, &apply_grain));
1562 RCHECK(reader->SkipBits(16));
1563 bool update_grain =
true;
1564 if (frame_header_.frame_type == INTER_FRAME)
1565 RCHECK(reader->ReadBits(1, &update_grain));
1566 if (!update_grain) {
1568 RCHECK(reader->SkipBits(3));
1572 int num_y_points = 0;
1573 RCHECK(reader->ReadBits(4, &num_y_points));
1575 RCHECK(reader->SkipBits((8 + 8) * num_y_points));
1577 const ColorConfig& color_config = sequence_header_.color_config;
1578 bool chroma_scaling_from_luma =
false;
1579 if (!color_config.mono_chrome)
1580 RCHECK(reader->ReadBits(1, &chroma_scaling_from_luma));
1581 int num_cb_points = 0;
1582 int num_cr_points = 0;
1583 if (color_config.mono_chrome || chroma_scaling_from_luma ||
1584 (color_config.subsampling_x && color_config.subsampling_y &&
1585 num_y_points == 0)) {
1589 RCHECK(reader->ReadBits(4, &num_cb_points));
1591 RCHECK(reader->SkipBits((8 + 8) * num_cb_points));
1592 RCHECK(reader->ReadBits(4, &num_cr_points));
1594 RCHECK(reader->SkipBits((8 + 8) * num_cr_points));
1598 RCHECK(reader->SkipBits(2));
1599 int ar_coeff_lag = 0;
1600 RCHECK(reader->ReadBits(2, &ar_coeff_lag));
1601 const int num_pos_luma = 2 * ar_coeff_lag * (ar_coeff_lag + 1);
1602 int num_pos_chroma = num_pos_luma;
1604 num_pos_chroma = num_pos_luma + 1;
1606 RCHECK(reader->SkipBits(8 * num_pos_luma));
1608 if (chroma_scaling_from_luma || num_cb_points) {
1610 RCHECK(reader->SkipBits(8 * num_pos_chroma));
1612 if (chroma_scaling_from_luma || num_cr_points) {
1614 RCHECK(reader->SkipBits(8 * num_pos_chroma));
1618 RCHECK(reader->SkipBits(2 + 2));
1619 if (num_cb_points) {
1621 RCHECK(reader->SkipBits(8 + 8 + 9));
1623 if (num_cr_points) {
1625 RCHECK(reader->SkipBits(8 + 8 + 9));
1628 RCHECK(reader->SkipBits(1 + 1));
1633 bool AV1Parser::SkipTemporalPointInfo(BitReader* reader) {
1634 const int frame_presentation_time_length =
1635 sequence_header_.decoder_model_info
1636 .frame_presentation_time_length_minus_1 +
1639 RCHECK(reader->SkipBits(frame_presentation_time_length));
1644 bool AV1Parser::ParseFrameObu(
const ObuHeader& obu_header,
1647 std::vector<Tile>* tiles) {
1648 const size_t start_bit_pos = reader->bit_position();
1649 RCHECK(ParseFrameHeaderObu(obu_header, reader));
1650 RCHECK(ByteAlignment(reader));
1651 const size_t end_bit_pos = reader->bit_position();
1652 const size_t header_bytes = (end_bit_pos - start_bit_pos) / 8;
1653 RCHECK(ParseTileGroupObu(size - header_bytes, reader, tiles));
1658 bool AV1Parser::ParseTileGroupObu(
size_t size,
1660 std::vector<Tile>* tiles) {
1661 const TileInfo& tile_info = frame_header_.tile_info;
1662 const size_t start_bit_pos = reader->bit_position();
1664 const int num_tiles = tile_info.tile_cols * tile_info.tile_rows;
1665 bool tile_start_and_end_present_flag =
false;
1667 RCHECK(reader->ReadBits(1, &tile_start_and_end_present_flag));
1670 int tg_end = num_tiles - 1;
1671 if (num_tiles > 1 && tile_start_and_end_present_flag) {
1672 const int tile_bits = tile_info.tile_cols_log2 + tile_info.tile_rows_log2;
1673 RCHECK(reader->ReadBits(tile_bits, &tg_start));
1674 RCHECK(reader->ReadBits(tile_bits, &tg_end));
1676 RCHECK(ByteAlignment(reader));
1678 const size_t end_bit_pos = reader->bit_position();
1679 const size_t header_bytes = (end_bit_pos - start_bit_pos) / 8;
1680 size -= header_bytes;
1682 for (
int tile_num = tg_start; tile_num <= tg_end; tile_num++) {
1683 const bool last_tile = tile_num == tg_end;
1684 size_t tile_size = size;
1686 size_t tile_size_minus_1 = 0;
1687 RCHECK(ReadLe(tile_info.tile_size_bytes, reader, &tile_size_minus_1));
1688 tile_size = tile_size_minus_1 + 1;
1689 size -= tile_size + tile_info.tile_size_bytes;
1691 tiles->push_back({reader->bit_position() / 8, tile_size});
1692 RCHECK(reader->SkipBits(tile_size * 8));
1695 if (tg_end == num_tiles - 1) {
1696 DecodeFrameWrapup();
1697 frame_header_.seen_frame_header =
false;
1703 bool AV1Parser::SegFeatureActiveIdx(
int idx,
int feature) {
1704 const SegmentationParams& segmentation_params =
1705 frame_header_.segmentation_params;
1706 return segmentation_params.segmentation_enabled &&
1707 segmentation_params.feature_enabled[idx][feature];
1711 void AV1Parser::DecodeFrameWrapup() {
1712 const int refresh_frame_flags = frame_header_.refresh_frame_flags;
1713 if (frame_header_.show_existing_frame &&
1714 frame_header_.frame_type == KEY_FRAME) {
1716 const ReferenceFrame& reference_frame =
1717 reference_frames_[frame_header_.frame_to_show_map_idx];
1719 frame_header_.upscaled_width = reference_frame.upscaled_width;
1720 frame_header_.frame_width = reference_frame.frame_width;
1721 frame_header_.frame_height = reference_frame.frame_height;
1722 frame_header_.render_width = reference_frame.render_width;
1723 frame_header_.render_height = reference_frame.render_height;
1724 frame_header_.mi_cols = reference_frame.mi_cols;
1725 frame_header_.mi_rows = reference_frame.mi_rows;
1727 ColorConfig& color_config = sequence_header_.color_config;
1728 color_config.subsampling_x = reference_frame.subsampling_x;
1729 color_config.subsampling_y = reference_frame.subsampling_y;
1730 color_config.bit_depth = reference_frame.bit_depth;
1732 frame_header_.order_hint = reference_frame.order_hint;
1735 for (
int i = 0; i <= kNumRefFrames - 1; i++) {
1736 if ((refresh_frame_flags >> i) & 1) {
1737 ReferenceFrame& reference_frame = reference_frames_[i];
1739 reference_frame.upscaled_width = frame_header_.upscaled_width;
1740 reference_frame.frame_width = frame_header_.frame_width;
1741 reference_frame.frame_height = frame_header_.frame_height;
1742 reference_frame.render_width = frame_header_.render_width;
1743 reference_frame.render_height = frame_header_.render_height;
1744 reference_frame.mi_cols = frame_header_.mi_cols;
1745 reference_frame.mi_rows = frame_header_.mi_rows;
1746 reference_frame.frame_type = frame_header_.frame_type;
1748 const ColorConfig& color_config = sequence_header_.color_config;
1749 reference_frame.subsampling_x = color_config.subsampling_x;
1750 reference_frame.subsampling_y = color_config.subsampling_y;
1751 reference_frame.bit_depth = color_config.bit_depth;
1753 reference_frame.order_hint = frame_header_.order_hint;
1759 bool AV1Parser::SetFrameRefs(
int last_frame_idx,
int gold_frame_idx) {
1760 for (
int i = 0; i < kRefsPerFrame; i++)
1761 frame_header_.ref_frame_idx[i] = -1;
1762 frame_header_.ref_frame_idx[LAST_FRAME - LAST_FRAME] = last_frame_idx;
1763 frame_header_.ref_frame_idx[GOLDEN_FRAME - LAST_FRAME] = gold_frame_idx;
1765 bool used_frame[kNumRefFrames] = {};
1766 used_frame[last_frame_idx] =
true;
1767 used_frame[gold_frame_idx] =
true;
1769 const int cur_frame_hint = 1 << (sequence_header_.order_hint_bits - 1);
1773 int shifted_order_hints[kNumRefFrames];
1774 for (
int i = 0; i < kNumRefFrames; i++) {
1775 shifted_order_hints[i] =
1776 cur_frame_hint + GetRelativeDist(reference_frames_[i].order_hint,
1777 frame_header_.order_hint);
1780 const int last_order_hint = shifted_order_hints[last_frame_idx];
1781 RCHECK(last_order_hint < cur_frame_hint);
1782 const int gold_order_hint = shifted_order_hints[gold_frame_idx];
1783 RCHECK(gold_order_hint < cur_frame_hint);
1787 int ref = FindLatestBackward(shifted_order_hints, used_frame, cur_frame_hint);
1789 frame_header_.ref_frame_idx[ALTREF_FRAME - LAST_FRAME] = ref;
1790 used_frame[ref] =
true;
1795 ref = FindEarliestBackward(shifted_order_hints, used_frame, cur_frame_hint);
1797 frame_header_.ref_frame_idx[BWDREF_FRAME - LAST_FRAME] = ref;
1798 used_frame[ref] =
true;
1802 ref = FindEarliestBackward(shifted_order_hints, used_frame, cur_frame_hint);
1804 frame_header_.ref_frame_idx[ALTREF2_FRAME - LAST_FRAME] = ref;
1805 used_frame[ref] =
true;
1810 static const int kRefFrameList[] = {
1811 LAST2_FRAME, LAST3_FRAME, BWDREF_FRAME, ALTREF2_FRAME, ALTREF_FRAME,
1813 static_assert(arraysize(kRefFrameList) == kRefsPerFrame - 2,
1814 "Unexpected kRefFrameList size.");
1815 for (
const int ref_frame : kRefFrameList) {
1816 if (frame_header_.ref_frame_idx[ref_frame - LAST_FRAME] < 0) {
1817 ref = FindLatestForward(shifted_order_hints, used_frame, cur_frame_hint);
1819 frame_header_.ref_frame_idx[ref_frame - LAST_FRAME] = ref;
1820 used_frame[ref] =
true;
1828 int earliest_order_hint = 0;
1829 for (
int i = 0; i < kNumRefFrames; i++) {
1830 const int hint = shifted_order_hints[i];
1831 if (ref < 0 || hint < earliest_order_hint) {
1833 earliest_order_hint = hint;
1836 for (
int i = 0; i < kRefsPerFrame; i++) {
1837 if (frame_header_.ref_frame_idx[i] < 0) {
1838 frame_header_.ref_frame_idx[i] = ref;
1847 int AV1Parser::GetQIndex(
bool ignore_delta_q,
int segment_id) {
1849 CHECK(ignore_delta_q) <<
"ignoreDeltaQ equal to 0 is not supported.";
1851 const int base_q_idx = frame_header_.quantization_params.base_q_idx;
1853 const int kSegLvlAltQ = 0;
1854 if (SegFeatureActiveIdx(segment_id, kSegLvlAltQ)) {
1856 frame_header_.segmentation_params.feature_data[segment_id][kSegLvlAltQ];
1857 const int qindex = base_q_idx + data;
1858 return Clip3(0, 255, qindex);