7 #include <packager/media/codecs/av1_parser.h>
11 #include <absl/log/check.h>
12 #include <absl/log/log.h>
14 #include <packager/macros/logging.h>
15 #include <packager/media/base/bit_reader.h>
16 #include <packager/media/base/rcheck.h>
30 const int kSelectScreenContentTools = 2;
31 const int kSelectIntegerMv = 2;
32 const int kPrimaryRefNone = 7;
33 const int kNumRefFrames = 8;
34 const int kAllFrames = (1 << kNumRefFrames) - 1;
38 OBU_SEQUENCE_HEADER = 1,
39 OBU_TEMPORAL_DELIMITER,
44 OBU_REDUNDENT_FRAME_HEADER,
56 enum TransferCharacteristics {
61 enum MatrixCoefficients {
66 enum ChromaSamplePosition {
94 int Clip3(
int min_value,
int max_value,
int value) {
95 if (value < min_value)
97 if (value > max_value)
104 int FloorLog2(
int x) {
114 bool ReadUvlc(BitReader* reader, uint32_t* val) {
116 int leading_zeros = 0;
119 RCHECK(reader->ReadBits(1, &done));
125 if (leading_zeros >= 32) {
126 *val = (1ull << 32) - 1;
131 if (leading_zeros > 0)
132 RCHECK(reader->ReadBits(leading_zeros, &value));
134 *val = value + (1 << leading_zeros) - 1;
140 bool ReadLe(
int n, BitReader* reader,
size_t* val) {
142 for (
int i = 0; i < n; i++) {
144 RCHECK(reader->ReadBits(8, &
byte));
145 t += (
byte << (i * 8));
153 bool ReadLeb128(BitReader* reader,
size_t* size) {
155 for (
int i = 0; i < 8; i++) {
156 size_t leb128_byte = 0;
157 RCHECK(reader->ReadBits(8, &leb128_byte));
158 value |= (leb128_byte & 0x7f) << (i * 7);
159 if (!(leb128_byte & 0x80))
164 RCHECK(value <= ((1ull << 32) - 1));
171 bool ReadSu(
int n, BitReader* reader,
int* value) {
172 RCHECK(reader->ReadBits(n, value));
173 int sign_mask = 1 << (n - 1);
174 if (*value & sign_mask)
175 *value = *value - 2 * sign_mask;
181 bool ReadNs(
int n, BitReader* reader,
int* value) {
182 const int w = FloorLog2(n) + 1;
183 const int m = (1 << w) - n;
184 RCHECK(reader->ReadBits(w - 1, value));
188 RCHECK(reader->ReadBits(1, &extra_bit));
189 *value = (*value << 1) - m + extra_bit;
195 int TileLog2(
int blk_size,
int target) {
197 for (k = 0; (blk_size << k) < target; k++)
203 int FindLatestBackward(
int shifted_order_hints[],
205 int cur_frame_hint) {
207 int latest_order_hint = 0;
208 for (
int i = 0; i < kNumRefFrames; i++) {
209 const int hint = shifted_order_hints[i];
210 if (!used_frame[i] && hint >= cur_frame_hint &&
211 (ref < 0 || hint >= latest_order_hint)) {
213 latest_order_hint = hint;
220 int FindEarliestBackward(
int shifted_order_hints[],
222 int cur_frame_hint) {
224 int earliest_order_hint = 0;
225 for (
int i = 0; i < kNumRefFrames; i++) {
226 const int hint = shifted_order_hints[i];
227 if (!used_frame[i] && hint >= cur_frame_hint &&
228 (ref < 0 || hint < earliest_order_hint)) {
230 earliest_order_hint = hint;
237 int FindLatestForward(
int shifted_order_hints[],
239 int cur_frame_hint) {
241 int latest_order_hint = 0;
242 for (
int i = 0; i < kNumRefFrames; i++) {
243 const int hint = shifted_order_hints[i];
244 if (!used_frame[i] && hint < cur_frame_hint &&
245 (ref < 0 || hint >= latest_order_hint)) {
247 latest_order_hint = hint;
255 AV1Parser::AV1Parser() =
default;
256 AV1Parser::~AV1Parser() =
default;
260 std::vector<Tile>* tiles) {
265 if (!ParseOpenBitstreamUnit(&reader, tiles))
272 bool AV1Parser::ParseOpenBitstreamUnit(
BitReader* reader,
273 std::vector<Tile>* tiles) {
274 ObuHeader obu_header;
275 RCHECK(ParseObuHeader(reader, &obu_header));
278 if (obu_header.obu_has_size_field)
279 RCHECK(ReadLeb128(reader, &obu_size));
283 VLOG(4) <<
"OBU " << obu_header.obu_type <<
" size " << obu_size;
286 switch (obu_header.obu_type) {
287 case OBU_SEQUENCE_HEADER:
288 RCHECK(ParseSequenceHeaderObu(reader));
290 case OBU_FRAME_HEADER:
291 case OBU_REDUNDENT_FRAME_HEADER:
292 RCHECK(ParseFrameHeaderObu(obu_header, reader));
295 RCHECK(ParseTileGroupObu(obu_size, reader, tiles));
298 RCHECK(ParseFrameObu(obu_header, obu_size, reader, tiles));
302 RCHECK(reader->
SkipBits(obu_size * 8));
307 const size_t payload_bits = current_position - start_position;
308 if (obu_header.obu_type == OBU_TILE_GROUP ||
309 obu_header.obu_type == OBU_FRAME) {
310 RCHECK(payload_bits == obu_size * 8);
311 }
else if (obu_size > 0) {
312 RCHECK(payload_bits <= obu_size * 8);
313 RCHECK(ParseTrailingBits(obu_size * 8 - payload_bits, reader));
319 bool AV1Parser::ParseObuHeader(BitReader* reader, ObuHeader* obu_header) {
320 int obu_forbidden_bit = 0;
321 RCHECK(reader->ReadBits(1, &obu_forbidden_bit));
322 RCHECK(obu_forbidden_bit == 0);
323 RCHECK(reader->ReadBits(4, &obu_header->obu_type));
324 bool obu_extension_flag =
false;
325 RCHECK(reader->ReadBits(1, &obu_extension_flag));
326 RCHECK(reader->ReadBits(1, &obu_header->obu_has_size_field));
327 RCHECK(reader->SkipBits(1));
329 if (obu_extension_flag)
330 RCHECK(ParseObuExtensionHeader(reader, &obu_header->extension_header));
336 bool AV1Parser::ParseObuExtensionHeader(
338 ObuExtensionHeader* obu_extension_header) {
339 RCHECK(reader->ReadBits(3, &obu_extension_header->temporal_id));
340 RCHECK(reader->ReadBits(2, &obu_extension_header->spatial_id));
341 RCHECK(reader->SkipBits(3));
346 bool AV1Parser::ParseTrailingBits(
size_t nb_bits, BitReader* reader) {
347 int trailing_one_bit = 0;
348 RCHECK(reader->ReadBits(1, &trailing_one_bit));
349 RCHECK(trailing_one_bit == 1);
351 while (nb_bits > 0) {
352 int trailing_zero_bit = 0;
353 RCHECK(reader->ReadBits(1, &trailing_zero_bit));
354 RCHECK(trailing_zero_bit == 0);
360 bool AV1Parser::ByteAlignment(BitReader* reader) {
361 while (reader->bit_position() & 7) {
363 RCHECK(reader->ReadBits(1, &zero_bit));
364 RCHECK(zero_bit == 0);
370 bool AV1Parser::ParseSequenceHeaderObu(BitReader* reader) {
371 RCHECK(reader->ReadBits(3, &sequence_header_.seq_profile));
373 RCHECK(reader->SkipBits(1));
375 RCHECK(reader->ReadBits(1, &sequence_header_.reduced_still_picture_header));
376 if (sequence_header_.reduced_still_picture_header) {
377 sequence_header_.decoder_model_info_present_flag =
false;
378 sequence_header_.operating_points_cnt_minus_1 = 0;
379 sequence_header_.operating_point_idc[0] = 0;
381 RCHECK(reader->SkipBits(5));
382 sequence_header_.decoder_model_present_for_this_op[0] =
false;
384 bool timing_info_present_flag =
false;
385 RCHECK(reader->ReadBits(1, &timing_info_present_flag));
387 bool decoder_model_info_present_flag =
false;
388 if (timing_info_present_flag) {
389 RCHECK(ParseTimingInfo(reader));
390 RCHECK(reader->ReadBits(1, &decoder_model_info_present_flag));
391 if (decoder_model_info_present_flag)
392 RCHECK(ParseDecoderModelInfo(reader));
394 sequence_header_.decoder_model_info_present_flag =
395 decoder_model_info_present_flag;
397 bool initial_display_delay_present_flag =
false;
398 RCHECK(reader->ReadBits(1, &initial_display_delay_present_flag));
400 RCHECK(reader->ReadBits(5, &sequence_header_.operating_points_cnt_minus_1));
401 for (
int i = 0; i <= sequence_header_.operating_points_cnt_minus_1; i++) {
402 RCHECK(reader->ReadBits(12, &sequence_header_.operating_point_idc[i]));
403 int seq_level_idx_i = 0;
404 RCHECK(reader->ReadBits(5, &seq_level_idx_i));
405 if (seq_level_idx_i > 7) {
407 RCHECK(reader->SkipBits(1));
410 if (sequence_header_.decoder_model_info_present_flag) {
411 RCHECK(reader->ReadBits(
412 1, &sequence_header_.decoder_model_present_for_this_op[i]));
413 if (sequence_header_.decoder_model_present_for_this_op[i]) {
414 RCHECK(SkipOperatingParametersInfo(reader));
417 sequence_header_.decoder_model_present_for_this_op[i] =
false;
420 if (initial_display_delay_present_flag) {
423 RCHECK(reader->SkipBitsConditional(
true, 4));
428 RCHECK(reader->ReadBits(4, &sequence_header_.frame_width_bits_minus_1));
429 RCHECK(reader->ReadBits(4, &sequence_header_.frame_height_bits_minus_1));
430 RCHECK(reader->ReadBits(sequence_header_.frame_width_bits_minus_1 + 1,
431 &sequence_header_.max_frame_width_minus_1));
432 RCHECK(reader->ReadBits(sequence_header_.frame_height_bits_minus_1 + 1,
433 &sequence_header_.max_frame_height_minus_1));
435 if (sequence_header_.reduced_still_picture_header) {
436 sequence_header_.frame_id_numbers_present_flag =
false;
439 reader->ReadBits(1, &sequence_header_.frame_id_numbers_present_flag));
441 if (sequence_header_.frame_id_numbers_present_flag) {
443 reader->ReadBits(4, &sequence_header_.delta_frame_id_length_minus_2));
444 RCHECK(reader->ReadBits(
445 3, &sequence_header_.additional_frame_id_length_minus_1));
448 RCHECK(reader->ReadBits(1, &sequence_header_.use_128x128_superblock));
450 RCHECK(reader->SkipBits(1 + 1));
452 if (sequence_header_.reduced_still_picture_header) {
453 sequence_header_.enable_warped_motion =
false;
454 sequence_header_.enable_order_hint =
false;
455 sequence_header_.enable_ref_frame_mvs =
false;
456 sequence_header_.order_hint_bits = 0;
457 sequence_header_.seq_force_screen_content_tools = kSelectScreenContentTools;
458 sequence_header_.seq_force_integer_mv = kSelectIntegerMv;
461 RCHECK(reader->SkipBits(1 + 1));
463 RCHECK(reader->ReadBits(1, &sequence_header_.enable_warped_motion));
464 RCHECK(reader->SkipBits(1));
465 RCHECK(reader->ReadBits(1, &sequence_header_.enable_order_hint));
466 if (sequence_header_.enable_order_hint) {
468 RCHECK(reader->SkipBits(1));
469 RCHECK(reader->ReadBits(1, &sequence_header_.enable_ref_frame_mvs));
471 sequence_header_.enable_ref_frame_mvs =
false;
474 bool seq_choose_screen_content_tools =
false;
475 RCHECK(reader->ReadBits(1, &seq_choose_screen_content_tools));
477 if (seq_choose_screen_content_tools) {
478 sequence_header_.seq_force_screen_content_tools =
479 kSelectScreenContentTools;
481 RCHECK(reader->ReadBits(
482 1, &sequence_header_.seq_force_screen_content_tools));
485 if (sequence_header_.seq_force_screen_content_tools > 0) {
486 bool seq_choose_integer_mv =
false;
487 RCHECK(reader->ReadBits(1, &seq_choose_integer_mv));
488 if (seq_choose_integer_mv)
489 sequence_header_.seq_force_integer_mv = kSelectIntegerMv;
491 RCHECK(reader->ReadBits(1, &sequence_header_.seq_force_integer_mv));
493 sequence_header_.seq_force_integer_mv = kSelectIntegerMv;
496 if (sequence_header_.enable_order_hint) {
497 int order_hint_bits_minus_1 = 0;
498 RCHECK(reader->ReadBits(3, &order_hint_bits_minus_1));
499 sequence_header_.order_hint_bits = order_hint_bits_minus_1 + 1;
501 sequence_header_.order_hint_bits = 0;
505 RCHECK(reader->ReadBits(1, &sequence_header_.enable_superres));
506 RCHECK(reader->ReadBits(1, &sequence_header_.enable_cdef));
507 RCHECK(reader->ReadBits(1, &sequence_header_.enable_restoration));
508 RCHECK(ParseColorConfig(reader));
509 RCHECK(reader->ReadBits(1, &sequence_header_.film_grain_params_present));
514 bool AV1Parser::ParseColorConfig(BitReader* reader) {
515 ColorConfig& color_config = sequence_header_.color_config;
517 bool high_bitdepth =
false;
518 RCHECK(reader->ReadBits(1, &high_bitdepth));
519 if (sequence_header_.seq_profile == 2 && high_bitdepth) {
520 bool twelve_bit =
false;
521 RCHECK(reader->ReadBits(1, &twelve_bit));
522 color_config.bit_depth = twelve_bit ? 12 : 10;
523 }
else if (sequence_header_.seq_profile <= 2) {
524 color_config.bit_depth = high_bitdepth ? 10 : 8;
527 if (sequence_header_.seq_profile == 1)
528 color_config.mono_chrome = 0;
530 RCHECK(reader->ReadBits(1, &color_config.mono_chrome));
531 color_config.num_planes = color_config.mono_chrome ? 1 : 3;
533 bool color_description_present_flag =
false;
534 RCHECK(reader->ReadBits(1, &color_description_present_flag));
536 if (color_description_present_flag) {
537 RCHECK(reader->ReadBits(8, &color_config.color_primaries));
538 RCHECK(reader->ReadBits(8, &color_config.transfer_chracteristics));
539 RCHECK(reader->ReadBits(8, &color_config.matrix_coefficients));
541 color_config.color_primaries = CP_UNSPECIFIED;
542 color_config.transfer_chracteristics = TC_UNSPECIFIED;
543 color_config.matrix_coefficients = MC_UNSPECIFIED;
546 if (color_config.mono_chrome) {
547 RCHECK(reader->ReadBits(1, &color_config.color_range));
548 color_config.subsampling_x =
true;
549 color_config.subsampling_y =
true;
550 color_config.chroma_sampling_position = CSP_UNKNOWN;
551 color_config.separate_uv_delta_q =
false;
553 }
else if (color_config.color_primaries == CP_BT_709 &&
554 color_config.transfer_chracteristics == TC_SRGB &&
555 color_config.matrix_coefficients == MC_IDENTITY) {
556 color_config.color_range =
true;
557 color_config.subsampling_x =
false;
558 color_config.subsampling_y =
false;
560 RCHECK(reader->ReadBits(1, &color_config.color_range));
561 if (sequence_header_.seq_profile == 0) {
562 color_config.subsampling_x =
true;
563 color_config.subsampling_y =
true;
564 }
else if (sequence_header_.seq_profile == 1) {
565 color_config.subsampling_x =
false;
566 color_config.subsampling_y =
false;
568 if (color_config.bit_depth == 12) {
569 RCHECK(reader->ReadBits(1, &color_config.subsampling_x));
570 if (color_config.subsampling_x)
571 RCHECK(reader->ReadBits(1, &color_config.subsampling_y));
573 color_config.subsampling_y =
false;
575 color_config.subsampling_x =
true;
576 color_config.subsampling_y =
false;
580 if (color_config.subsampling_x && color_config.subsampling_y)
581 RCHECK(reader->ReadBits(2, &color_config.chroma_sampling_position));
584 RCHECK(reader->ReadBits(1, &color_config.separate_uv_delta_q));
589 bool AV1Parser::ParseTimingInfo(BitReader* reader) {
591 RCHECK(reader->SkipBits(32 + 32));
592 bool equal_picture_interval =
false;
593 RCHECK(reader->ReadBits(1, &equal_picture_interval));
594 sequence_header_.timing_info.equal_picture_interval = equal_picture_interval;
595 if (equal_picture_interval) {
596 uint32_t num_ticks_per_picture_minus_1 = 0;
597 RCHECK(ReadUvlc(reader, &num_ticks_per_picture_minus_1));
603 bool AV1Parser::ParseDecoderModelInfo(BitReader* reader) {
604 DecoderModelInfo& decoder_model_info = sequence_header_.decoder_model_info;
606 RCHECK(reader->ReadBits(5, &decoder_model_info.buffer_delay_length_minus_1));
608 RCHECK(reader->SkipBits(32));
609 RCHECK(reader->ReadBits(
610 5, &decoder_model_info.buffer_removal_time_length_minus_1));
611 RCHECK(reader->ReadBits(
612 5, &decoder_model_info.frame_presentation_time_length_minus_1));
617 bool AV1Parser::SkipOperatingParametersInfo(BitReader* reader) {
619 sequence_header_.decoder_model_info.buffer_delay_length_minus_1 + 1;
622 RCHECK(reader->SkipBits(n + n + 1));
627 bool AV1Parser::ParseFrameHeaderObu(
const ObuHeader& obu_header,
629 if (frame_header_.seen_frame_header)
632 frame_header_.seen_frame_header =
true;
633 RCHECK(ParseUncompressedHeader(obu_header, reader));
634 if (frame_header_.show_existing_frame) {
636 frame_header_.seen_frame_header =
false;
638 frame_header_.seen_frame_header =
true;
644 bool AV1Parser::ParseUncompressedHeader(
const ObuHeader& obu_header,
647 if (sequence_header_.frame_id_numbers_present_flag) {
648 id_len = sequence_header_.additional_frame_id_length_minus_1 + 1 +
649 sequence_header_.delta_frame_id_length_minus_2 + 2;
652 bool frame_is_intra =
false;
653 bool show_frame =
false;
654 bool showable_frame =
false;
655 bool error_resilient_mode =
false;
657 if (sequence_header_.reduced_still_picture_header) {
658 frame_header_.show_existing_frame =
false;
659 frame_header_.frame_type = KEY_FRAME;
660 frame_is_intra =
true;
662 showable_frame =
false;
664 RCHECK(reader->ReadBits(1, &frame_header_.show_existing_frame));
665 if (frame_header_.show_existing_frame) {
666 RCHECK(reader->ReadBits(3, &frame_header_.frame_to_show_map_idx));
667 if (sequence_header_.decoder_model_info_present_flag &&
668 !sequence_header_.timing_info.equal_picture_interval) {
669 RCHECK(SkipTemporalPointInfo(reader));
671 frame_header_.refresh_frame_flags = 0;
672 if (sequence_header_.frame_id_numbers_present_flag) {
674 RCHECK(reader->SkipBits(id_len));
676 frame_header_.frame_type =
677 reference_frames_[frame_header_.frame_to_show_map_idx].frame_type;
678 if (frame_header_.frame_type == KEY_FRAME) {
679 frame_header_.refresh_frame_flags = kAllFrames;
684 RCHECK(reader->ReadBits(2, &frame_header_.frame_type));
685 frame_is_intra = frame_header_.frame_type == INTRA_ONLY_FRAME ||
686 frame_header_.frame_type == KEY_FRAME;
687 RCHECK(reader->ReadBits(1, &show_frame));
688 if (show_frame && sequence_header_.decoder_model_info_present_flag &&
689 !sequence_header_.timing_info.equal_picture_interval) {
690 RCHECK(SkipTemporalPointInfo(reader));
693 showable_frame = frame_header_.frame_type != KEY_FRAME;
695 RCHECK(reader->ReadBits(1, &showable_frame));
697 if (frame_header_.frame_type == SWITCH_FRAME ||
698 (frame_header_.frame_type == KEY_FRAME && show_frame)) {
699 error_resilient_mode =
true;
701 RCHECK(reader->ReadBits(1, &error_resilient_mode));
705 if (frame_header_.frame_type == KEY_FRAME && show_frame) {
706 for (
int i = 0; i < kNumRefFrames; i++) {
707 reference_frames_[i].order_hint = 0;
711 bool disable_cdf_update =
false;
712 RCHECK(reader->ReadBits(1, &disable_cdf_update));
714 bool allow_screen_content_tools =
false;
715 if (sequence_header_.seq_force_screen_content_tools ==
716 kSelectScreenContentTools) {
717 RCHECK(reader->ReadBits(1, &allow_screen_content_tools));
719 allow_screen_content_tools =
720 sequence_header_.seq_force_screen_content_tools != 0;
723 int force_integer_mv = 0;
724 if (allow_screen_content_tools) {
725 if (sequence_header_.seq_force_integer_mv == kSelectIntegerMv)
726 RCHECK(reader->ReadBits(1, &force_integer_mv));
728 force_integer_mv = sequence_header_.seq_force_integer_mv;
731 force_integer_mv = 1;
733 if (sequence_header_.frame_id_numbers_present_flag) {
735 RCHECK(reader->SkipBits(id_len));
738 bool frame_size_override_flag =
false;
739 if (frame_header_.frame_type == SWITCH_FRAME)
740 frame_size_override_flag =
true;
741 else if (sequence_header_.reduced_still_picture_header)
742 frame_size_override_flag =
false;
744 RCHECK(reader->ReadBits(1, &frame_size_override_flag));
746 RCHECK(reader->ReadBits(sequence_header_.order_hint_bits,
747 &frame_header_.order_hint));
748 int primary_ref_frame = 0;
749 if (frame_is_intra || error_resilient_mode) {
750 primary_ref_frame = kPrimaryRefNone;
752 RCHECK(reader->ReadBits(3, &primary_ref_frame));
754 if (sequence_header_.decoder_model_info_present_flag) {
755 bool buffer_removal_time_present_flag =
false;
756 RCHECK(reader->ReadBits(1, &buffer_removal_time_present_flag));
757 if (buffer_removal_time_present_flag) {
759 op_num <= sequence_header_.operating_points_cnt_minus_1; op_num++) {
760 if (sequence_header_.decoder_model_present_for_this_op[op_num]) {
761 const int op_pt_idc = sequence_header_.operating_point_idc[op_num];
762 const int in_temporal_layer =
763 (op_pt_idc >> obu_header.extension_header.temporal_id) & 1;
764 const int in_spatial_layer =
765 (op_pt_idc >> (obu_header.extension_header.spatial_id + 8)) & 1;
766 if (op_pt_idc == 0 || (in_temporal_layer && in_spatial_layer)) {
768 RCHECK(reader->SkipBits(sequence_header_.decoder_model_info
769 .buffer_removal_time_length_minus_1 +
777 bool allow_high_precision_mv =
false;
778 bool allow_intrabc =
false;
780 if (frame_header_.frame_type == SWITCH_FRAME ||
781 (frame_header_.frame_type == KEY_FRAME && show_frame)) {
782 frame_header_.refresh_frame_flags = kAllFrames;
784 RCHECK(reader->ReadBits(8, &frame_header_.refresh_frame_flags));
786 if (!frame_is_intra || frame_header_.refresh_frame_flags != kAllFrames) {
787 if (error_resilient_mode && sequence_header_.enable_order_hint) {
788 for (
int i = 0; i < kNumRefFrames; i++) {
790 RCHECK(reader->SkipBits(sequence_header_.order_hint_bits));
795 if (frame_is_intra) {
796 RCHECK(ParseFrameSize(frame_size_override_flag, reader));
797 RCHECK(ParseRenderSize(reader));
798 if (allow_screen_content_tools &&
799 frame_header_.upscaled_width == frame_header_.frame_width)
800 RCHECK(reader->ReadBits(1, &allow_intrabc));
802 bool frame_refs_short_signaling =
false;
803 if (sequence_header_.enable_order_hint) {
804 RCHECK(reader->ReadBits(1, &frame_refs_short_signaling));
805 if (frame_refs_short_signaling) {
806 int last_frame_idx = 0;
807 RCHECK(reader->ReadBits(3, &last_frame_idx));
808 int gold_frame_idx = 0;
809 RCHECK(reader->ReadBits(3, &gold_frame_idx));
810 RCHECK(SetFrameRefs(last_frame_idx, gold_frame_idx));
813 for (
int i = 0; i < kRefsPerFrame; i++) {
814 if (!frame_refs_short_signaling)
815 RCHECK(reader->ReadBits(3, &frame_header_.ref_frame_idx[i]));
816 if (sequence_header_.frame_id_numbers_present_flag) {
818 RCHECK(reader->SkipBits(sequence_header_.delta_frame_id_length_minus_2 +
822 if (frame_size_override_flag && !error_resilient_mode) {
823 RCHECK(ParseFrameSizeWithRefs(frame_size_override_flag, reader));
825 RCHECK(ParseFrameSize(frame_size_override_flag, reader));
826 RCHECK(ParseRenderSize(reader));
829 if (force_integer_mv)
830 allow_high_precision_mv =
false;
832 RCHECK(reader->ReadBits(1, &allow_high_precision_mv));
834 RCHECK(SkipInterpolationFilter(reader));
836 RCHECK(reader->SkipBits(1));
837 if (!error_resilient_mode && sequence_header_.enable_ref_frame_mvs) {
839 RCHECK(reader->SkipBits(1));
843 if (!sequence_header_.reduced_still_picture_header && !disable_cdf_update) {
845 RCHECK(reader->SkipBits(1));
848 RCHECK(ParseTileInfo(reader));
849 RCHECK(ParseQuantizationParams(reader));
850 RCHECK(ParseSegmentationParams(primary_ref_frame, reader));
852 bool delta_q_present =
false;
853 RCHECK(SkipDeltaQParams(reader, &delta_q_present));
854 RCHECK(SkipDeltaLfParams(delta_q_present, allow_intrabc, reader));
856 const auto& quantization_params = frame_header_.quantization_params;
857 bool coded_lossless =
true;
858 for (
int segment_id = 0; segment_id < kMaxSegments; segment_id++) {
859 const int qindex = GetQIndex(
true, segment_id);
860 const bool lossless = qindex == 0 && quantization_params.delta_qydc == 0 &&
861 quantization_params.delta_quac == 0 &&
862 quantization_params.delta_qudc == 0 &&
863 quantization_params.delta_qvac == 0 &&
864 quantization_params.delta_qvdc == 0;
866 coded_lossless =
false;
868 const bool all_lossless = coded_lossless && (frame_header_.frame_width ==
869 frame_header_.upscaled_width);
871 RCHECK(ParseLoopFilterParams(coded_lossless, allow_intrabc, reader));
872 RCHECK(ParseCdefParams(coded_lossless, allow_intrabc, reader));
873 RCHECK(ParseLrParams(all_lossless, allow_intrabc, reader));
874 RCHECK(SkipTxMode(coded_lossless, reader));
875 bool reference_select =
false;
876 RCHECK(ParseFrameReferenceMode(frame_is_intra, reader, &reference_select));
877 RCHECK(SkipSkipModeParams(frame_is_intra, reference_select, reader));
879 bool allow_warped_motion =
false;
880 if (frame_is_intra || error_resilient_mode ||
881 !sequence_header_.enable_warped_motion) {
882 allow_warped_motion =
false;
884 RCHECK(reader->ReadBits(1, &allow_warped_motion));
887 RCHECK(reader->SkipBits(1));
890 SkipGlobalMotionParams(frame_is_intra, allow_high_precision_mv, reader));
891 RCHECK(SkipFilmGrainParams(show_frame, showable_frame, reader));
896 int AV1Parser::GetRelativeDist(
int a,
int b) {
897 if (!sequence_header_.enable_order_hint)
900 const int m = 1 << (sequence_header_.order_hint_bits - 1);
901 diff = (diff & (m - 1)) - (diff & m);
906 bool AV1Parser::ParseFrameSize(
bool frame_size_override_flag,
908 if (frame_size_override_flag) {
909 int frame_width_minus_1 = 0;
910 RCHECK(reader->ReadBits(sequence_header_.frame_width_bits_minus_1 + 1,
911 &frame_width_minus_1));
912 int frame_height_minus_1 = 0;
913 RCHECK(reader->ReadBits(sequence_header_.frame_height_bits_minus_1 + 1,
914 &frame_height_minus_1));
915 frame_header_.frame_width = frame_width_minus_1 + 1;
916 frame_header_.frame_height = frame_height_minus_1 + 1;
918 frame_header_.frame_width = sequence_header_.max_frame_width_minus_1 + 1;
919 frame_header_.frame_height = sequence_header_.max_frame_height_minus_1 + 1;
921 RCHECK(ParseSuperresParams(reader));
927 bool AV1Parser::ParseRenderSize(BitReader* reader) {
928 bool render_and_frame_size_different =
false;
929 RCHECK(reader->ReadBits(1, &render_and_frame_size_different));
930 if (render_and_frame_size_different) {
931 int render_width_minus_1 = 0;
932 RCHECK(reader->ReadBits(16, &render_width_minus_1));
933 int render_height_minus_1 = 0;
934 RCHECK(reader->ReadBits(16, &render_height_minus_1));
935 frame_header_.render_width = render_width_minus_1 + 1;
936 frame_header_.render_height = render_height_minus_1 + 1;
938 frame_header_.render_width = frame_header_.upscaled_width;
939 frame_header_.render_height = frame_header_.frame_height;
945 bool AV1Parser::ParseFrameSizeWithRefs(
bool frame_size_override_flag,
947 bool found_ref =
false;
948 for (
int i = 0; i < kRefsPerFrame; i++) {
949 RCHECK(reader->ReadBits(1, &found_ref));
951 const ReferenceFrame& reference_frame =
952 reference_frames_[frame_header_.ref_frame_idx[i]];
953 frame_header_.upscaled_width = reference_frame.upscaled_width;
954 frame_header_.frame_width = frame_header_.upscaled_width;
955 frame_header_.frame_height = reference_frame.frame_height;
956 frame_header_.render_width = reference_frame.render_width;
957 frame_header_.render_height = reference_frame.render_height;
962 RCHECK(ParseFrameSize(frame_size_override_flag, reader));
963 RCHECK(ParseRenderSize(reader));
965 RCHECK(ParseSuperresParams(reader));
972 bool AV1Parser::ParseSuperresParams(BitReader* reader) {
973 const int kSuperresNum = 8;
974 const int kSuperresDenomMin = 9;
975 const int kSuperresDenomBits = 3;
977 bool use_superres =
false;
978 if (sequence_header_.enable_superres)
979 RCHECK(reader->ReadBits(1, &use_superres));
981 int superres_denom = 0;
984 RCHECK(reader->ReadBits(kSuperresDenomBits, &coded_denom));
985 superres_denom = coded_denom + kSuperresDenomMin;
987 superres_denom = kSuperresNum;
990 const int upscaled_width = frame_header_.frame_width;
991 frame_header_.upscaled_width =
992 (upscaled_width * kSuperresNum + superres_denom / 2) / superres_denom;
997 void AV1Parser::ComputeImageSize() {
998 frame_header_.mi_cols = 2 * ((frame_header_.frame_width + 7) >> 3);
999 frame_header_.mi_rows = 2 * ((frame_header_.frame_height + 7) >> 3);
1003 bool AV1Parser::SkipInterpolationFilter(BitReader* reader) {
1005 RCHECK(reader->SkipBitsConditional(
false, 2));
1010 bool AV1Parser::ParseLoopFilterParams(
bool coded_lossless,
1012 BitReader* reader) {
1013 if (coded_lossless || allow_intrabc)
1016 int loop_filter_level[] = {0, 0};
1017 RCHECK(reader->ReadBits(6, &loop_filter_level[0]));
1018 RCHECK(reader->ReadBits(6, &loop_filter_level[1]));
1019 if (sequence_header_.color_config.num_planes > 1) {
1020 if (loop_filter_level[0] || loop_filter_level[1]) {
1022 RCHECK(reader->SkipBits(6 + 6));
1026 RCHECK(reader->SkipBits(3));
1027 bool loop_filter_delta_enabled =
false;
1028 RCHECK(reader->ReadBits(1, &loop_filter_delta_enabled));
1029 if (loop_filter_delta_enabled) {
1030 bool loop_filter_delta_update =
false;
1031 RCHECK(reader->ReadBits(1, &loop_filter_delta_update));
1032 if (loop_filter_delta_update) {
1033 const int kTotalRefsPerFrame = 8;
1034 for (
int i = 0; i < kTotalRefsPerFrame; i++) {
1036 RCHECK(reader->SkipBitsConditional(
true, 1 + 6));
1038 for (
int i = 0; i < 2; i++) {
1040 RCHECK(reader->SkipBitsConditional(
true, 1 + 6));
1048 bool AV1Parser::ParseQuantizationParams(BitReader* reader) {
1049 QuantizationParams& quantization_params = frame_header_.quantization_params;
1051 RCHECK(reader->ReadBits(8, &quantization_params.base_q_idx));
1052 RCHECK(ReadDeltaQ(reader, &quantization_params.delta_qydc));
1054 const ColorConfig& color_config = sequence_header_.color_config;
1055 if (color_config.num_planes > 1) {
1056 bool diff_uv_delta =
false;
1057 if (color_config.separate_uv_delta_q)
1058 RCHECK(reader->ReadBits(1, &diff_uv_delta));
1059 RCHECK(ReadDeltaQ(reader, &quantization_params.delta_qudc));
1060 RCHECK(ReadDeltaQ(reader, &quantization_params.delta_quac));
1061 if (diff_uv_delta) {
1062 RCHECK(ReadDeltaQ(reader, &quantization_params.delta_qvdc));
1063 RCHECK(ReadDeltaQ(reader, &quantization_params.delta_qvac));
1065 quantization_params.delta_qvdc = quantization_params.delta_qudc;
1066 quantization_params.delta_qvac = quantization_params.delta_quac;
1069 quantization_params.delta_qudc = 0;
1070 quantization_params.delta_quac = 0;
1071 quantization_params.delta_qvdc = 0;
1072 quantization_params.delta_qvac = 0;
1074 bool using_qmatrix =
false;
1075 RCHECK(reader->ReadBits(1, &using_qmatrix));
1076 if (using_qmatrix) {
1078 RCHECK(reader->SkipBits(4 + 4));
1079 if (color_config.separate_uv_delta_q) {
1081 RCHECK(reader->SkipBits(4));
1088 bool AV1Parser::ReadDeltaQ(BitReader* reader,
int* delta_q) {
1089 bool delta_coded =
false;
1090 RCHECK(reader->ReadBits(1, &delta_coded));
1092 RCHECK(ReadSu(1 + 6, reader, delta_q));
1099 bool AV1Parser::ParseSegmentationParams(
int primary_ref_frame,
1100 BitReader* reader) {
1101 SegmentationParams& segmentation_params = frame_header_.segmentation_params;
1103 RCHECK(reader->ReadBits(1, &segmentation_params.segmentation_enabled));
1104 if (segmentation_params.segmentation_enabled) {
1105 bool segmentation_update_data =
false;
1106 if (primary_ref_frame == kPrimaryRefNone) {
1107 segmentation_update_data =
true;
1110 RCHECK(reader->SkipBitsConditional(
true, 1));
1111 RCHECK(reader->ReadBits(1, &segmentation_update_data));
1113 if (segmentation_update_data) {
1114 static const int kSegmentationFeatureBits[kSegLvlMax] = {8, 6, 6, 6,
1116 static const int kSegmentationFeatureSigned[kSegLvlMax] = {1, 1, 1, 1,
1118 const int kMaxLoopFilter = 63;
1119 static const int kSegmentationFeatureMax[kSegLvlMax] = {255,
1128 for (
int i = 0; i < kMaxSegments; i++) {
1129 for (
int j = 0; j < kSegLvlMax; j++) {
1130 bool feature_enabled =
false;
1131 RCHECK(reader->ReadBits(1, &feature_enabled));
1132 segmentation_params.feature_enabled[i][j] = feature_enabled;
1133 int clipped_value = 0;
1134 if (feature_enabled) {
1135 const int bits_to_read = kSegmentationFeatureBits[j];
1136 const int limit = kSegmentationFeatureMax[j];
1137 if (kSegmentationFeatureSigned[j]) {
1138 int feature_value = 0;
1139 RCHECK(ReadSu(1 + bits_to_read, reader, &feature_value));
1140 clipped_value = Clip3(-limit, limit, feature_value);
1142 int feature_value = 0;
1143 RCHECK(reader->ReadBits(bits_to_read, &feature_value));
1144 clipped_value = Clip3(0, limit, feature_value);
1147 segmentation_params.feature_data[i][j] = clipped_value;
1152 for (
int i = 0; i < kMaxSegments; i++) {
1153 for (
int j = 0; j < kSegLvlMax; j++) {
1154 segmentation_params.feature_enabled[i][j] =
false;
1155 segmentation_params.feature_data[i][j] = 0;
1163 bool AV1Parser::ParseTileInfo(BitReader* reader) {
1164 const int kMaxTileWidth = 4096;
1165 const int kMaxTileArea = 4096 * 2304;
1166 const int kMaxTileRows = 64;
1167 const int kMaxTileCols = 64;
1169 TileInfo& tile_info = frame_header_.tile_info;
1171 const int sb_cols = sequence_header_.use_128x128_superblock
1172 ? ((frame_header_.mi_cols + 31) >> 5)
1173 : ((frame_header_.mi_cols + 15) >> 4);
1174 const int sb_rows = sequence_header_.use_128x128_superblock
1175 ? ((frame_header_.mi_rows + 31) >> 5)
1176 : ((frame_header_.mi_rows + 15) >> 4);
1177 const int sb_shift = sequence_header_.use_128x128_superblock ? 5 : 4;
1178 const int sb_size = sb_shift + 2;
1179 const int max_tile_width_sb = kMaxTileWidth >> sb_size;
1180 int max_tile_area_sb = kMaxTileArea >> (2 * sb_size);
1181 const int min_log2_tile_cols = TileLog2(max_tile_width_sb, sb_cols);
1182 const int max_log2_tile_cols = TileLog2(1, std::min(sb_cols, kMaxTileCols));
1183 const int max_log2_tile_rows = TileLog2(1, std::min(sb_rows, kMaxTileRows));
1184 const int min_log2_tiles = std::max(
1185 min_log2_tile_cols, TileLog2(max_tile_area_sb, sb_rows * sb_cols));
1187 bool uniform_tile_spacing_flag =
false;
1188 RCHECK(reader->ReadBits(1, &uniform_tile_spacing_flag));
1189 if (uniform_tile_spacing_flag) {
1190 tile_info.tile_cols_log2 = min_log2_tile_cols;
1191 while (tile_info.tile_cols_log2 < max_log2_tile_cols) {
1192 bool increment_tile_cols_log2 =
false;
1193 RCHECK(reader->ReadBits(1, &increment_tile_cols_log2));
1194 if (increment_tile_cols_log2)
1195 tile_info.tile_cols_log2++;
1199 const int tile_width_sb = (sb_cols + (1 << tile_info.tile_cols_log2) - 1) >>
1200 tile_info.tile_cols_log2;
1202 for (
int start_sb = 0; start_sb < sb_cols; start_sb += tile_width_sb) {
1205 tile_info.tile_cols = i;
1207 const int min_log2_tile_rows =
1208 std::max(min_log2_tiles - tile_info.tile_cols_log2, 0);
1209 tile_info.tile_rows_log2 = min_log2_tile_rows;
1210 while (tile_info.tile_rows_log2 < max_log2_tile_rows) {
1211 bool increment_tile_rows_log2 =
false;
1212 RCHECK(reader->ReadBits(1, &increment_tile_rows_log2));
1213 if (increment_tile_rows_log2)
1214 tile_info.tile_rows_log2++;
1218 const int tile_height_sb =
1219 (sb_rows + (1 << tile_info.tile_rows_log2) - 1) >>
1220 tile_info.tile_rows_log2;
1222 for (
int start_sb = 0; start_sb < sb_rows; start_sb += tile_height_sb) {
1225 tile_info.tile_rows = i;
1227 int widest_tile_sb = 0;
1230 for (; start_sb < sb_cols; i++) {
1231 const int max_width = std::min(sb_cols - start_sb, max_tile_width_sb);
1232 int width_in_sbs_minus_1 = 0;
1233 RCHECK(ReadNs(max_width, reader, &width_in_sbs_minus_1));
1234 const int size_sb = width_in_sbs_minus_1 + 1;
1235 widest_tile_sb = std::max(size_sb, widest_tile_sb);
1236 start_sb += size_sb;
1238 tile_info.tile_cols = i;
1239 tile_info.tile_cols_log2 = TileLog2(1, tile_info.tile_cols);
1241 if (min_log2_tiles > 0)
1242 max_tile_area_sb = (sb_rows * sb_cols) >> (min_log2_tiles + 1);
1244 max_tile_area_sb = sb_rows * sb_cols;
1245 const int max_tile_height_sb =
1246 std::max(max_tile_area_sb / widest_tile_sb, 1);
1250 for (; start_sb < sb_rows; i++) {
1251 const int max_height = std::min(sb_rows - start_sb, max_tile_height_sb);
1252 int height_in_sbs_minus_1 = 0;
1253 RCHECK(ReadNs(max_height, reader, &height_in_sbs_minus_1));
1254 const int size_sb = height_in_sbs_minus_1 + 1;
1255 start_sb += size_sb;
1257 tile_info.tile_rows = i;
1258 tile_info.tile_rows_log2 = TileLog2(1, tile_info.tile_rows);
1260 if (tile_info.tile_cols_log2 > 0 || tile_info.tile_rows_log2 > 0) {
1263 reader->SkipBits(tile_info.tile_rows_log2 + tile_info.tile_cols_log2));
1264 int tile_size_bytes_minus_1 = 0;
1265 RCHECK(reader->ReadBits(2, &tile_size_bytes_minus_1));
1266 tile_info.tile_size_bytes = tile_size_bytes_minus_1 + 1;
1272 bool AV1Parser::SkipDeltaQParams(BitReader* reader,
bool* delta_q_present) {
1273 *delta_q_present =
false;
1274 if (frame_header_.quantization_params.base_q_idx > 0)
1275 RCHECK(reader->ReadBits(1, delta_q_present));
1276 if (*delta_q_present) {
1278 RCHECK(reader->SkipBits(2));
1284 bool AV1Parser::SkipDeltaLfParams(
bool delta_q_present,
1286 BitReader* reader) {
1287 bool delta_lf_present =
false;
1288 if (delta_q_present) {
1290 RCHECK(reader->ReadBits(1, &delta_lf_present));
1291 if (delta_lf_present) {
1293 RCHECK(reader->SkipBits(2 + 1));
1300 bool AV1Parser::ParseCdefParams(
bool coded_lossless,
1302 BitReader* reader) {
1303 if (coded_lossless || allow_intrabc || !sequence_header_.enable_cdef)
1307 RCHECK(reader->SkipBits(2));
1309 RCHECK(reader->ReadBits(2, &cdef_bits));
1310 for (
int i = 0; i < (1 << cdef_bits); i++) {
1312 RCHECK(reader->SkipBits(4 + 2));
1313 if (sequence_header_.color_config.num_planes > 1) {
1315 RCHECK(reader->SkipBits(4 + 2));
1322 bool AV1Parser::ParseLrParams(
bool all_lossless,
1324 BitReader* reader) {
1325 if (all_lossless || allow_intrabc || !sequence_header_.enable_restoration)
1328 enum FrameRestorationType {
1330 RESTORE_SWITCHABLE = 3,
1332 RESTORE_SGRPROJ = 2,
1334 static const int kRemapLrType[4] = {RESTORE_NONE, RESTORE_SWITCHABLE,
1335 RESTORE_WIENER, RESTORE_SGRPROJ};
1336 bool uses_lr =
false;
1337 bool uses_chroma_lr =
false;
1338 for (
int i = 0; i < sequence_header_.color_config.num_planes; i++) {
1340 RCHECK(reader->ReadBits(2, &lr_type));
1341 const int frame_restoration_type = kRemapLrType[lr_type];
1342 if (frame_restoration_type != RESTORE_NONE) {
1345 uses_chroma_lr =
true;
1350 if (sequence_header_.use_128x128_superblock) {
1352 RCHECK(reader->SkipBits(1));
1355 RCHECK(reader->SkipBitsConditional(
true, 1));
1357 if (sequence_header_.color_config.subsampling_x &&
1358 sequence_header_.color_config.subsampling_y && uses_chroma_lr) {
1360 RCHECK(reader->SkipBits(1));
1367 bool AV1Parser::SkipTxMode(
bool coded_lossless, BitReader* reader) {
1368 if (!coded_lossless) {
1370 RCHECK(reader->SkipBits(1));
1376 bool AV1Parser::SkipSkipModeParams(
bool frame_is_intra,
1377 bool reference_select,
1378 BitReader* reader) {
1379 bool skip_mode_allowed =
false;
1380 if (frame_is_intra || !reference_select ||
1381 !sequence_header_.enable_order_hint) {
1382 skip_mode_allowed =
false;
1384 int forward_idx = -1;
1385 int forward_hint = 0;
1386 int backward_idx = -1;
1387 int backward_hint = 0;
1388 for (
int i = 0; i < kRefsPerFrame; i++) {
1389 const int ref_hint =
1390 reference_frames_[frame_header_.ref_frame_idx[i]].order_hint;
1391 if (GetRelativeDist(ref_hint, frame_header_.order_hint) < 0) {
1392 if (forward_idx < 0 || GetRelativeDist(ref_hint, forward_hint) > 0) {
1394 forward_hint = ref_hint;
1396 }
else if (GetRelativeDist(ref_hint, frame_header_.order_hint) > 0) {
1397 if (backward_idx < 0 || GetRelativeDist(ref_hint, backward_hint) < 0) {
1399 backward_hint = ref_hint;
1403 if (forward_idx < 0) {
1404 skip_mode_allowed =
false;
1405 }
else if (backward_idx >= 0) {
1406 skip_mode_allowed =
true;
1408 int second_forward_idx = -1;
1409 int second_forward_hint = 0;
1410 for (
int i = 0; i < kRefsPerFrame; i++) {
1411 const int ref_hint =
1412 reference_frames_[frame_header_.ref_frame_idx[i]].order_hint;
1413 if (GetRelativeDist(ref_hint, forward_hint) < 0) {
1414 if (second_forward_idx < 0 ||
1415 GetRelativeDist(ref_hint, second_forward_hint) > 0) {
1416 second_forward_idx = i;
1417 second_forward_hint = ref_hint;
1421 skip_mode_allowed = second_forward_idx >= 0;
1425 if (skip_mode_allowed) {
1427 RCHECK(reader->SkipBits(1));
1433 bool AV1Parser::ParseFrameReferenceMode(
bool frame_is_intra,
1435 bool* reference_select) {
1437 *reference_select =
false;
1439 RCHECK(reader->ReadBits(1, reference_select));
1444 bool AV1Parser::SkipGlobalMotionParams(
bool frame_is_intra,
1445 bool allow_high_precision_mv,
1446 BitReader* reader) {
1450 for (
int ref = LAST_FRAME; ref <= ALTREF_FRAME; ref++) {
1453 bool is_global =
false;
1454 RCHECK(reader->ReadBits(1, &is_global));
1456 bool is_rot_zoom =
false;
1457 RCHECK(reader->ReadBits(1, &is_rot_zoom));
1461 bool is_translation =
false;
1462 RCHECK(reader->ReadBits(1, &is_translation));
1463 type = is_translation ? TRANSLATION : AFFINE;
1469 if (type >= ROTZOOM) {
1470 RCHECK(SkipGlobalParam(type, ref, 2, allow_high_precision_mv, reader));
1471 RCHECK(SkipGlobalParam(type, ref, 3, allow_high_precision_mv, reader));
1472 if (type == AFFINE) {
1473 RCHECK(SkipGlobalParam(type, ref, 4, allow_high_precision_mv, reader));
1474 RCHECK(SkipGlobalParam(type, ref, 5, allow_high_precision_mv, reader));
1477 if (type >= TRANSLATION) {
1478 RCHECK(SkipGlobalParam(type, ref, 0, allow_high_precision_mv, reader));
1479 RCHECK(SkipGlobalParam(type, ref, 1, allow_high_precision_mv, reader));
1486 bool AV1Parser::SkipGlobalParam(
int type,
1489 bool allow_high_precision_mv,
1490 BitReader* reader) {
1491 const int kGmAbsTransBits = 12;
1492 const int kGmAbsTransOnlyBits = 9;
1493 const int kGmAbsAlphaBits = 12;
1495 int abs_bits = kGmAbsAlphaBits;
1497 if (type == TRANSLATION) {
1498 abs_bits = kGmAbsTransOnlyBits - (allow_high_precision_mv ? 0 : 1);
1500 abs_bits = kGmAbsTransBits;
1503 const int mx = 1 << abs_bits;
1504 RCHECK(SkipDecodeSignedSubexpWithRef(-mx, mx + 1, reader));
1509 bool AV1Parser::SkipDecodeSignedSubexpWithRef(
int low,
1511 BitReader* reader) {
1512 RCHECK(SkipDecodeUnsignedSubexpWithRef(high - low, reader));
1517 bool AV1Parser::SkipDecodeUnsignedSubexpWithRef(
int mx, BitReader* reader) {
1518 RCHECK(SkipDecodeSubexp(mx, reader));
1523 bool AV1Parser::SkipDecodeSubexp(
int num_syms, BitReader* reader) {
1528 const int b2 = i ? (k + i - 1) : k;
1529 const int a = 1 << b2;
1530 if (num_syms <= mk + 3 * a) {
1531 int subexp_final_bits = 0;
1532 RCHECK(ReadNs(num_syms - mk, reader, &subexp_final_bits));
1535 bool subexp_more_bits =
false;
1536 RCHECK(reader->ReadBits(1, &subexp_more_bits));
1537 if (subexp_more_bits) {
1542 RCHECK(reader->SkipBits(b2));
1551 bool AV1Parser::SkipFilmGrainParams(
bool show_frame,
1552 bool showable_frame,
1553 BitReader* reader) {
1554 if (!sequence_header_.film_grain_params_present ||
1555 (!show_frame && !showable_frame)) {
1559 bool apply_grain =
false;
1560 RCHECK(reader->ReadBits(1, &apply_grain));
1565 RCHECK(reader->SkipBits(16));
1566 bool update_grain =
true;
1567 if (frame_header_.frame_type == INTER_FRAME)
1568 RCHECK(reader->ReadBits(1, &update_grain));
1569 if (!update_grain) {
1571 RCHECK(reader->SkipBits(3));
1575 int num_y_points = 0;
1576 RCHECK(reader->ReadBits(4, &num_y_points));
1578 RCHECK(reader->SkipBits((8 + 8) * num_y_points));
1580 const ColorConfig& color_config = sequence_header_.color_config;
1581 bool chroma_scaling_from_luma =
false;
1582 if (!color_config.mono_chrome)
1583 RCHECK(reader->ReadBits(1, &chroma_scaling_from_luma));
1584 int num_cb_points = 0;
1585 int num_cr_points = 0;
1586 if (color_config.mono_chrome || chroma_scaling_from_luma ||
1587 (color_config.subsampling_x && color_config.subsampling_y &&
1588 num_y_points == 0)) {
1592 RCHECK(reader->ReadBits(4, &num_cb_points));
1594 RCHECK(reader->SkipBits((8 + 8) * num_cb_points));
1595 RCHECK(reader->ReadBits(4, &num_cr_points));
1597 RCHECK(reader->SkipBits((8 + 8) * num_cr_points));
1601 RCHECK(reader->SkipBits(2));
1602 int ar_coeff_lag = 0;
1603 RCHECK(reader->ReadBits(2, &ar_coeff_lag));
1604 const int num_pos_luma = 2 * ar_coeff_lag * (ar_coeff_lag + 1);
1605 int num_pos_chroma = num_pos_luma;
1607 num_pos_chroma = num_pos_luma + 1;
1609 RCHECK(reader->SkipBits(8 * num_pos_luma));
1611 if (chroma_scaling_from_luma || num_cb_points) {
1613 RCHECK(reader->SkipBits(8 * num_pos_chroma));
1615 if (chroma_scaling_from_luma || num_cr_points) {
1617 RCHECK(reader->SkipBits(8 * num_pos_chroma));
1621 RCHECK(reader->SkipBits(2 + 2));
1622 if (num_cb_points) {
1624 RCHECK(reader->SkipBits(8 + 8 + 9));
1626 if (num_cr_points) {
1628 RCHECK(reader->SkipBits(8 + 8 + 9));
1631 RCHECK(reader->SkipBits(1 + 1));
1636 bool AV1Parser::SkipTemporalPointInfo(BitReader* reader) {
1637 const int frame_presentation_time_length =
1638 sequence_header_.decoder_model_info
1639 .frame_presentation_time_length_minus_1 +
1642 RCHECK(reader->SkipBits(frame_presentation_time_length));
1647 bool AV1Parser::ParseFrameObu(
const ObuHeader& obu_header,
1650 std::vector<Tile>* tiles) {
1651 const size_t start_bit_pos = reader->bit_position();
1652 RCHECK(ParseFrameHeaderObu(obu_header, reader));
1653 RCHECK(ByteAlignment(reader));
1654 const size_t end_bit_pos = reader->bit_position();
1655 const size_t header_bytes = (end_bit_pos - start_bit_pos) / 8;
1656 RCHECK(ParseTileGroupObu(size - header_bytes, reader, tiles));
1661 bool AV1Parser::ParseTileGroupObu(
size_t size,
1663 std::vector<Tile>* tiles) {
1664 const TileInfo& tile_info = frame_header_.tile_info;
1665 const size_t start_bit_pos = reader->bit_position();
1667 const int num_tiles = tile_info.tile_cols * tile_info.tile_rows;
1668 bool tile_start_and_end_present_flag =
false;
1670 RCHECK(reader->ReadBits(1, &tile_start_and_end_present_flag));
1673 int tg_end = num_tiles - 1;
1674 if (num_tiles > 1 && tile_start_and_end_present_flag) {
1675 const int tile_bits = tile_info.tile_cols_log2 + tile_info.tile_rows_log2;
1676 RCHECK(reader->ReadBits(tile_bits, &tg_start));
1677 RCHECK(reader->ReadBits(tile_bits, &tg_end));
1679 RCHECK(ByteAlignment(reader));
1681 const size_t end_bit_pos = reader->bit_position();
1682 const size_t header_bytes = (end_bit_pos - start_bit_pos) / 8;
1683 size -= header_bytes;
1685 for (
int tile_num = tg_start; tile_num <= tg_end; tile_num++) {
1686 const bool last_tile = tile_num == tg_end;
1687 size_t tile_size = size;
1689 size_t tile_size_minus_1 = 0;
1690 RCHECK(ReadLe(tile_info.tile_size_bytes, reader, &tile_size_minus_1));
1691 tile_size = tile_size_minus_1 + 1;
1692 size -= tile_size + tile_info.tile_size_bytes;
1694 tiles->push_back({reader->bit_position() / 8, tile_size});
1695 RCHECK(reader->SkipBits(tile_size * 8));
1698 if (tg_end == num_tiles - 1) {
1699 DecodeFrameWrapup();
1700 frame_header_.seen_frame_header =
false;
1706 bool AV1Parser::SegFeatureActiveIdx(
int idx,
int feature) {
1707 const SegmentationParams& segmentation_params =
1708 frame_header_.segmentation_params;
1709 return segmentation_params.segmentation_enabled &&
1710 segmentation_params.feature_enabled[idx][feature];
1714 void AV1Parser::DecodeFrameWrapup() {
1715 const int refresh_frame_flags = frame_header_.refresh_frame_flags;
1716 if (frame_header_.show_existing_frame &&
1717 frame_header_.frame_type == KEY_FRAME) {
1719 const ReferenceFrame& reference_frame =
1720 reference_frames_[frame_header_.frame_to_show_map_idx];
1722 frame_header_.upscaled_width = reference_frame.upscaled_width;
1723 frame_header_.frame_width = reference_frame.frame_width;
1724 frame_header_.frame_height = reference_frame.frame_height;
1725 frame_header_.render_width = reference_frame.render_width;
1726 frame_header_.render_height = reference_frame.render_height;
1727 frame_header_.mi_cols = reference_frame.mi_cols;
1728 frame_header_.mi_rows = reference_frame.mi_rows;
1730 ColorConfig& color_config = sequence_header_.color_config;
1731 color_config.subsampling_x = reference_frame.subsampling_x;
1732 color_config.subsampling_y = reference_frame.subsampling_y;
1733 color_config.bit_depth = reference_frame.bit_depth;
1735 frame_header_.order_hint = reference_frame.order_hint;
1738 for (
int i = 0; i <= kNumRefFrames - 1; i++) {
1739 if ((refresh_frame_flags >> i) & 1) {
1740 ReferenceFrame& reference_frame = reference_frames_[i];
1742 reference_frame.upscaled_width = frame_header_.upscaled_width;
1743 reference_frame.frame_width = frame_header_.frame_width;
1744 reference_frame.frame_height = frame_header_.frame_height;
1745 reference_frame.render_width = frame_header_.render_width;
1746 reference_frame.render_height = frame_header_.render_height;
1747 reference_frame.mi_cols = frame_header_.mi_cols;
1748 reference_frame.mi_rows = frame_header_.mi_rows;
1749 reference_frame.frame_type = frame_header_.frame_type;
1751 const ColorConfig& color_config = sequence_header_.color_config;
1752 reference_frame.subsampling_x = color_config.subsampling_x;
1753 reference_frame.subsampling_y = color_config.subsampling_y;
1754 reference_frame.bit_depth = color_config.bit_depth;
1756 reference_frame.order_hint = frame_header_.order_hint;
1762 bool AV1Parser::SetFrameRefs(
int last_frame_idx,
int gold_frame_idx) {
1763 for (
int i = 0; i < kRefsPerFrame; i++)
1764 frame_header_.ref_frame_idx[i] = -1;
1765 frame_header_.ref_frame_idx[LAST_FRAME - LAST_FRAME] = last_frame_idx;
1766 frame_header_.ref_frame_idx[GOLDEN_FRAME - LAST_FRAME] = gold_frame_idx;
1768 bool used_frame[kNumRefFrames] = {};
1769 used_frame[last_frame_idx] =
true;
1770 used_frame[gold_frame_idx] =
true;
1772 const int cur_frame_hint = 1 << (sequence_header_.order_hint_bits - 1);
1776 int shifted_order_hints[kNumRefFrames];
1777 for (
int i = 0; i < kNumRefFrames; i++) {
1778 shifted_order_hints[i] =
1779 cur_frame_hint + GetRelativeDist(reference_frames_[i].order_hint,
1780 frame_header_.order_hint);
1783 const int last_order_hint = shifted_order_hints[last_frame_idx];
1784 RCHECK(last_order_hint < cur_frame_hint);
1785 const int gold_order_hint = shifted_order_hints[gold_frame_idx];
1786 RCHECK(gold_order_hint < cur_frame_hint);
1790 int ref = FindLatestBackward(shifted_order_hints, used_frame, cur_frame_hint);
1792 frame_header_.ref_frame_idx[ALTREF_FRAME - LAST_FRAME] = ref;
1793 used_frame[ref] =
true;
1798 ref = FindEarliestBackward(shifted_order_hints, used_frame, cur_frame_hint);
1800 frame_header_.ref_frame_idx[BWDREF_FRAME - LAST_FRAME] = ref;
1801 used_frame[ref] =
true;
1805 ref = FindEarliestBackward(shifted_order_hints, used_frame, cur_frame_hint);
1807 frame_header_.ref_frame_idx[ALTREF2_FRAME - LAST_FRAME] = ref;
1808 used_frame[ref] =
true;
1813 static const int kRefFrameList[] = {
1814 LAST2_FRAME, LAST3_FRAME, BWDREF_FRAME, ALTREF2_FRAME, ALTREF_FRAME,
1816 static_assert(std::size(kRefFrameList) == kRefsPerFrame - 2,
1817 "Unexpected kRefFrameList size.");
1818 for (
const int ref_frame : kRefFrameList) {
1819 if (frame_header_.ref_frame_idx[ref_frame - LAST_FRAME] < 0) {
1820 ref = FindLatestForward(shifted_order_hints, used_frame, cur_frame_hint);
1822 frame_header_.ref_frame_idx[ref_frame - LAST_FRAME] = ref;
1823 used_frame[ref] =
true;
1831 int earliest_order_hint = 0;
1832 for (
int i = 0; i < kNumRefFrames; i++) {
1833 const int hint = shifted_order_hints[i];
1834 if (ref < 0 || hint < earliest_order_hint) {
1836 earliest_order_hint = hint;
1839 for (
int i = 0; i < kRefsPerFrame; i++) {
1840 if (frame_header_.ref_frame_idx[i] < 0) {
1841 frame_header_.ref_frame_idx[i] = ref;
1850 int AV1Parser::GetQIndex(
bool ignore_delta_q,
int segment_id) {
1852 CHECK(ignore_delta_q) <<
"ignoreDeltaQ equal to 0 is not supported.";
1854 const int base_q_idx = frame_header_.quantization_params.base_q_idx;
1856 const int kSegLvlAltQ = 0;
1857 if (SegFeatureActiveIdx(segment_id, kSegLvlAltQ)) {
1859 frame_header_.segmentation_params.feature_data[segment_id][kSegLvlAltQ];
1860 const int qindex = base_q_idx + data;
1861 return Clip3(0, 255, qindex);
All the methods that are virtual are virtual for mocking.