1use crate::bool_decoder::{BoolDecoder, BoolDecoderError};
88use core::fmt;
89
90#[derive(Debug, Clone, Copy, PartialEq, Eq)]
92#[non_exhaustive]
93pub enum CodedHeaderError {
94 BoolDecoder(BoolDecoderError),
99}
100
101impl fmt::Display for CodedHeaderError {
102 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
103 match self {
104 CodedHeaderError::BoolDecoder(inner) => {
105 write!(f, "vp8 coded header: {inner}")
106 }
107 }
108 }
109}
110
111impl std::error::Error for CodedHeaderError {}
112
113impl From<BoolDecoderError> for CodedHeaderError {
114 fn from(value: BoolDecoderError) -> Self {
115 CodedHeaderError::BoolDecoder(value)
116 }
117}
118
119#[derive(Debug, Clone, Copy, PartialEq, Eq)]
124pub struct UpdateSegmentation {
125 pub update_mb_segmentation_map: bool,
128 pub update_segment_feature_data: bool,
130 pub segment_feature_mode_absolute: bool,
134 pub quantizer_update: [Option<i16>; 4],
138 pub loop_filter_update: [Option<i16>; 4],
142 pub segment_prob: [Option<u8>; 3],
148}
149
150#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
152pub struct MbLfAdjustments {
153 pub loop_filter_adj_enable: bool,
156 pub mode_ref_lf_delta_update: bool,
159 pub ref_frame_delta_update: [Option<i16>; 4],
164 pub mb_mode_delta_update: [Option<i16>; 4],
167}
168
169#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
171pub struct QuantIndices {
172 pub y_ac_qi: u8,
174 pub y_dc_delta: Option<i8>,
177 pub y2_dc_delta: Option<i8>,
179 pub y2_ac_delta: Option<i8>,
181 pub uv_dc_delta: Option<i8>,
183 pub uv_ac_delta: Option<i8>,
185}
186
187pub type TokenProbUpdates = [[[[Option<u8>; 11]; 3]; 8]; 4];
193
194pub const MV_PROB_COUNT: usize = 19;
199
200pub type MvProbUpdates = [[Option<u8>; MV_PROB_COUNT]; 2];
206
207#[derive(Debug, Clone, PartialEq, Eq, Default)]
222#[non_exhaustive]
223pub struct Vp8CodedHeader {
224 pub color_space: Option<bool>,
228 pub clamping_type: Option<bool>,
231 pub segmentation_enabled: bool,
233 pub update_segmentation: Option<UpdateSegmentation>,
236 pub filter_type: bool,
238 pub loop_filter_level: u8,
240 pub sharpness_level: u8,
242 pub mb_lf_adjustments: MbLfAdjustments,
244 pub log2_nbr_of_dct_partitions: u8,
246 pub nbr_of_dct_partitions: u8,
248 pub quant_indices: QuantIndices,
250 pub refresh_entropy_probs: bool,
253 pub refresh_golden_frame: Option<bool>,
257 pub refresh_alternate_frame: Option<bool>,
260 pub copy_buffer_to_golden: Option<u8>,
264 pub copy_buffer_to_alternate: Option<u8>,
269 pub sign_bias_golden: Option<bool>,
272 pub sign_bias_alternate: Option<bool>,
274 pub refresh_last: Option<bool>,
277 pub token_prob_updates: TokenProbUpdates,
283 pub mb_no_skip_coeff: bool,
286 pub prob_skip_false: Option<u8>,
291 pub prob_intra: Option<u8>,
295 pub prob_last: Option<u8>,
300 pub prob_gf: Option<u8>,
304 pub intra_y_mode_prob_update: Option<[u8; 4]>,
313 pub intra_uv_mode_prob_update: Option<[u8; 3]>,
318 pub mv_prob_update: Option<MvProbUpdates>,
326}
327
328impl Vp8CodedHeader {
329 #[must_use]
331 pub fn with_color_space(mut self, v: Option<bool>) -> Self {
332 self.color_space = v;
333 self
334 }
335
336 #[must_use]
338 pub fn with_clamping_type(mut self, v: Option<bool>) -> Self {
339 self.clamping_type = v;
340 self
341 }
342
343 #[must_use]
345 pub fn with_segmentation_enabled(mut self, v: bool) -> Self {
346 self.segmentation_enabled = v;
347 self
348 }
349
350 #[must_use]
352 pub fn with_update_segmentation(mut self, v: Option<UpdateSegmentation>) -> Self {
353 self.update_segmentation = v;
354 self
355 }
356
357 #[must_use]
359 pub fn with_filter_type(mut self, v: bool) -> Self {
360 self.filter_type = v;
361 self
362 }
363
364 #[must_use]
366 pub fn with_loop_filter_level(mut self, v: u8) -> Self {
367 self.loop_filter_level = v;
368 self
369 }
370
371 #[must_use]
373 pub fn with_sharpness_level(mut self, v: u8) -> Self {
374 self.sharpness_level = v;
375 self
376 }
377
378 #[must_use]
380 pub fn with_mb_lf_adjustments(mut self, v: MbLfAdjustments) -> Self {
381 self.mb_lf_adjustments = v;
382 self
383 }
384
385 #[must_use]
387 pub fn with_log2_nbr_of_dct_partitions(mut self, v: u8) -> Self {
388 self.log2_nbr_of_dct_partitions = v;
389 self
390 }
391
392 #[must_use]
394 pub fn with_nbr_of_dct_partitions(mut self, v: u8) -> Self {
395 self.nbr_of_dct_partitions = v;
396 self
397 }
398
399 #[must_use]
401 pub fn with_quant_indices(mut self, v: QuantIndices) -> Self {
402 self.quant_indices = v;
403 self
404 }
405
406 #[must_use]
408 pub fn with_refresh_entropy_probs(mut self, v: bool) -> Self {
409 self.refresh_entropy_probs = v;
410 self
411 }
412
413 #[must_use]
415 pub fn with_refresh_golden_frame(mut self, v: Option<bool>) -> Self {
416 self.refresh_golden_frame = v;
417 self
418 }
419
420 #[must_use]
422 pub fn with_refresh_alternate_frame(mut self, v: Option<bool>) -> Self {
423 self.refresh_alternate_frame = v;
424 self
425 }
426
427 #[must_use]
429 pub fn with_copy_buffer_to_golden(mut self, v: Option<u8>) -> Self {
430 self.copy_buffer_to_golden = v;
431 self
432 }
433
434 #[must_use]
436 pub fn with_copy_buffer_to_alternate(mut self, v: Option<u8>) -> Self {
437 self.copy_buffer_to_alternate = v;
438 self
439 }
440
441 #[must_use]
443 pub fn with_sign_bias_golden(mut self, v: Option<bool>) -> Self {
444 self.sign_bias_golden = v;
445 self
446 }
447
448 #[must_use]
450 pub fn with_sign_bias_alternate(mut self, v: Option<bool>) -> Self {
451 self.sign_bias_alternate = v;
452 self
453 }
454
455 #[must_use]
457 pub fn with_refresh_last(mut self, v: Option<bool>) -> Self {
458 self.refresh_last = v;
459 self
460 }
461
462 #[must_use]
464 pub fn with_token_prob_updates(mut self, v: TokenProbUpdates) -> Self {
465 self.token_prob_updates = v;
466 self
467 }
468
469 #[must_use]
471 pub fn with_mb_no_skip_coeff(mut self, v: bool) -> Self {
472 self.mb_no_skip_coeff = v;
473 self
474 }
475
476 #[must_use]
478 pub fn with_prob_skip_false(mut self, v: Option<u8>) -> Self {
479 self.prob_skip_false = v;
480 self
481 }
482
483 #[must_use]
485 pub fn with_prob_intra(mut self, v: Option<u8>) -> Self {
486 self.prob_intra = v;
487 self
488 }
489
490 #[must_use]
492 pub fn with_prob_last(mut self, v: Option<u8>) -> Self {
493 self.prob_last = v;
494 self
495 }
496
497 #[must_use]
499 pub fn with_prob_gf(mut self, v: Option<u8>) -> Self {
500 self.prob_gf = v;
501 self
502 }
503
504 #[must_use]
506 pub fn with_intra_y_mode_prob_update(mut self, v: Option<[u8; 4]>) -> Self {
507 self.intra_y_mode_prob_update = v;
508 self
509 }
510
511 #[must_use]
513 pub fn with_intra_uv_mode_prob_update(mut self, v: Option<[u8; 3]>) -> Self {
514 self.intra_uv_mode_prob_update = v;
515 self
516 }
517
518 #[must_use]
520 pub fn with_mv_prob_update(mut self, v: Option<MvProbUpdates>) -> Self {
521 self.mv_prob_update = v;
522 self
523 }
524}
525
526impl Vp8CodedHeader {
527 pub fn parse(partition: &[u8], key_frame: bool) -> Result<Self, CodedHeaderError> {
536 Self::parse_with_decoder(partition, key_frame).map(|(hdr, _dec)| hdr)
537 }
538
539 pub fn parse_with_decoder<'a>(
545 partition: &'a [u8],
546 key_frame: bool,
547 ) -> Result<(Self, BoolDecoder<'a>), CodedHeaderError> {
548 let mut dec = BoolDecoder::init(partition)?;
549 let hdr = Self::parse_from_decoder(&mut dec, key_frame)?;
550 Ok((hdr, dec))
551 }
552
553 fn parse_from_decoder(
558 dec: &mut BoolDecoder<'_>,
559 key_frame: bool,
560 ) -> Result<Self, CodedHeaderError> {
561 let (color_space, clamping_type) = if key_frame {
563 let cs = dec.read_bool(128)?;
564 let ct = dec.read_bool(128)?;
565 (Some(cs), Some(ct))
566 } else {
567 (None, None)
568 };
569
570 let segmentation_enabled = dec.read_bool(128)?;
572 let update_segmentation = if segmentation_enabled {
573 Some(parse_update_segmentation(dec)?)
574 } else {
575 None
576 };
577
578 let filter_type = dec.read_bool(128)?;
580 let loop_filter_level = dec.read_literal(6)? as u8;
581 let sharpness_level = dec.read_literal(3)? as u8;
582
583 let mb_lf_adjustments = parse_mb_lf_adjustments(dec)?;
585
586 let log2_nbr_of_dct_partitions = dec.read_literal(2)? as u8;
588 let nbr_of_dct_partitions = 1u8 << log2_nbr_of_dct_partitions;
589
590 let quant_indices = parse_quant_indices(dec)?;
592
593 let (
599 refresh_golden_frame,
600 refresh_alternate_frame,
601 copy_buffer_to_golden,
602 copy_buffer_to_alternate,
603 sign_bias_golden,
604 sign_bias_alternate,
605 refresh_entropy_probs,
606 refresh_last,
607 ) = if key_frame {
608 let refresh_entropy_probs = dec.read_bool(128)?;
609 (
610 None,
611 None,
612 None,
613 None,
614 None,
615 None,
616 refresh_entropy_probs,
617 None,
618 )
619 } else {
620 let refresh_golden_frame = dec.read_bool(128)?;
621 let refresh_alternate_frame = dec.read_bool(128)?;
622 let copy_buffer_to_golden = if !refresh_golden_frame {
623 Some(dec.read_literal(2)? as u8)
624 } else {
625 None
626 };
627 let copy_buffer_to_alternate = if !refresh_alternate_frame {
628 Some(dec.read_literal(2)? as u8)
629 } else {
630 None
631 };
632 let sign_bias_golden = dec.read_bool(128)?;
633 let sign_bias_alternate = dec.read_bool(128)?;
634 let refresh_entropy_probs = dec.read_bool(128)?;
635 let refresh_last = dec.read_bool(128)?;
636 (
637 Some(refresh_golden_frame),
638 Some(refresh_alternate_frame),
639 copy_buffer_to_golden,
640 copy_buffer_to_alternate,
641 Some(sign_bias_golden),
642 Some(sign_bias_alternate),
643 refresh_entropy_probs,
644 Some(refresh_last),
645 )
646 };
647
648 let token_prob_updates = parse_token_prob_update(dec)?;
652
653 let mb_no_skip_coeff = dec.read_bool(128)?;
655 let prob_skip_false = if mb_no_skip_coeff {
656 Some(dec.read_literal(8)? as u8)
657 } else {
658 None
659 };
660
661 let (
666 prob_intra,
667 prob_last,
668 prob_gf,
669 intra_y_mode_prob_update,
670 intra_uv_mode_prob_update,
671 mv_prob_update,
672 ) = if key_frame {
673 (None, None, None, None, None, None)
674 } else {
675 let prob_intra = dec.read_literal(8)? as u8;
676 let prob_last = dec.read_literal(8)? as u8;
677 let prob_gf = dec.read_literal(8)? as u8;
678
679 let intra_y_mode_prob_update = if dec.read_bool(128)? {
682 let mut buf = [0u8; 4];
683 for slot in &mut buf {
684 *slot = dec.read_literal(8)? as u8;
685 }
686 Some(buf)
687 } else {
688 None
689 };
690
691 let intra_uv_mode_prob_update = if dec.read_bool(128)? {
693 let mut buf = [0u8; 3];
694 for slot in &mut buf {
695 *slot = dec.read_literal(8)? as u8;
696 }
697 Some(buf)
698 } else {
699 None
700 };
701
702 let mv_prob_update = Some(parse_mv_prob_update(dec)?);
704
705 (
706 Some(prob_intra),
707 Some(prob_last),
708 Some(prob_gf),
709 intra_y_mode_prob_update,
710 intra_uv_mode_prob_update,
711 mv_prob_update,
712 )
713 };
714
715 Ok(Vp8CodedHeader {
716 color_space,
717 clamping_type,
718 segmentation_enabled,
719 update_segmentation,
720 filter_type,
721 loop_filter_level,
722 sharpness_level,
723 mb_lf_adjustments,
724 log2_nbr_of_dct_partitions,
725 nbr_of_dct_partitions,
726 quant_indices,
727 refresh_entropy_probs,
728 refresh_golden_frame,
729 refresh_alternate_frame,
730 copy_buffer_to_golden,
731 copy_buffer_to_alternate,
732 sign_bias_golden,
733 sign_bias_alternate,
734 refresh_last,
735 token_prob_updates,
736 mb_no_skip_coeff,
737 prob_skip_false,
738 prob_intra,
739 prob_last,
740 prob_gf,
741 intra_y_mode_prob_update,
742 intra_uv_mode_prob_update,
743 mv_prob_update,
744 })
745 }
746}
747
748const COEFF_UPDATE_PROBS: [[[[u8; 11]; 3]; 8]; 4] = [
756 [
757 [
758 [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
759 [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
760 [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
761 ],
762 [
763 [176, 246, 255, 255, 255, 255, 255, 255, 255, 255, 255],
764 [223, 241, 252, 255, 255, 255, 255, 255, 255, 255, 255],
765 [249, 253, 253, 255, 255, 255, 255, 255, 255, 255, 255],
766 ],
767 [
768 [255, 244, 252, 255, 255, 255, 255, 255, 255, 255, 255],
769 [234, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255],
770 [253, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
771 ],
772 [
773 [255, 246, 254, 255, 255, 255, 255, 255, 255, 255, 255],
774 [239, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255],
775 [254, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255],
776 ],
777 [
778 [255, 248, 254, 255, 255, 255, 255, 255, 255, 255, 255],
779 [251, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255],
780 [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
781 ],
782 [
783 [255, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255],
784 [251, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255],
785 [254, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255],
786 ],
787 [
788 [255, 254, 253, 255, 254, 255, 255, 255, 255, 255, 255],
789 [250, 255, 254, 255, 254, 255, 255, 255, 255, 255, 255],
790 [254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
791 ],
792 [
793 [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
794 [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
795 [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
796 ],
797 ],
798 [
799 [
800 [217, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
801 [225, 252, 241, 253, 255, 255, 254, 255, 255, 255, 255],
802 [234, 250, 241, 250, 253, 255, 253, 254, 255, 255, 255],
803 ],
804 [
805 [255, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255],
806 [223, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255],
807 [238, 253, 254, 254, 255, 255, 255, 255, 255, 255, 255],
808 ],
809 [
810 [255, 248, 254, 255, 255, 255, 255, 255, 255, 255, 255],
811 [249, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255],
812 [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
813 ],
814 [
815 [255, 253, 255, 255, 255, 255, 255, 255, 255, 255, 255],
816 [247, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255],
817 [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
818 ],
819 [
820 [255, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255],
821 [252, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
822 [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
823 ],
824 [
825 [255, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255],
826 [253, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
827 [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
828 ],
829 [
830 [255, 254, 253, 255, 255, 255, 255, 255, 255, 255, 255],
831 [250, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
832 [254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
833 ],
834 [
835 [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
836 [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
837 [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
838 ],
839 ],
840 [
841 [
842 [186, 251, 250, 255, 255, 255, 255, 255, 255, 255, 255],
843 [234, 251, 244, 254, 255, 255, 255, 255, 255, 255, 255],
844 [251, 251, 243, 253, 254, 255, 254, 255, 255, 255, 255],
845 ],
846 [
847 [255, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255],
848 [236, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255],
849 [251, 253, 253, 254, 254, 255, 255, 255, 255, 255, 255],
850 ],
851 [
852 [255, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255],
853 [254, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255],
854 [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
855 ],
856 [
857 [255, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255],
858 [254, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255],
859 [254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
860 ],
861 [
862 [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
863 [254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
864 [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
865 ],
866 [
867 [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
868 [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
869 [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
870 ],
871 [
872 [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
873 [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
874 [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
875 ],
876 [
877 [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
878 [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
879 [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
880 ],
881 ],
882 [
883 [
884 [248, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
885 [250, 254, 252, 254, 255, 255, 255, 255, 255, 255, 255],
886 [248, 254, 249, 253, 255, 255, 255, 255, 255, 255, 255],
887 ],
888 [
889 [255, 253, 253, 255, 255, 255, 255, 255, 255, 255, 255],
890 [246, 253, 253, 255, 255, 255, 255, 255, 255, 255, 255],
891 [252, 254, 251, 254, 254, 255, 255, 255, 255, 255, 255],
892 ],
893 [
894 [255, 254, 252, 255, 255, 255, 255, 255, 255, 255, 255],
895 [248, 254, 253, 255, 255, 255, 255, 255, 255, 255, 255],
896 [253, 255, 254, 254, 255, 255, 255, 255, 255, 255, 255],
897 ],
898 [
899 [255, 251, 254, 255, 255, 255, 255, 255, 255, 255, 255],
900 [245, 251, 254, 255, 255, 255, 255, 255, 255, 255, 255],
901 [253, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255],
902 ],
903 [
904 [255, 251, 253, 255, 255, 255, 255, 255, 255, 255, 255],
905 [252, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255],
906 [255, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255],
907 ],
908 [
909 [255, 252, 255, 255, 255, 255, 255, 255, 255, 255, 255],
910 [249, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255],
911 [255, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255],
912 ],
913 [
914 [255, 255, 253, 255, 255, 255, 255, 255, 255, 255, 255],
915 [250, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
916 [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
917 ],
918 [
919 [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
920 [254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
921 [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
922 ],
923 ],
924];
925
926pub(crate) const MV_UPDATE_PROBS: [[u8; MV_PROB_COUNT]; 2] = [
942 [
943 237, 246, 253, 253, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 250, 250, 252, 254,
944 254,
945 ],
946 [
947 231, 243, 245, 253, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 251, 251, 254, 254,
948 254,
949 ],
950];
951
952pub const DEFAULT_MV_CONTEXT: [[u8; MV_PROB_COUNT]; 2] = [
958 [
960 162, 128, 225, 146, 172, 147, 214, 39, 156, 128, 129, 132, 75, 145, 178, 206, 239, 254, 254,
961 ],
962 [
964 164, 128, 204, 170, 119, 235, 140, 230, 228, 128, 130, 130, 74, 148, 180, 203, 236, 254,
965 254,
966 ],
967];
968
969fn parse_mv_prob_update(dec: &mut BoolDecoder<'_>) -> Result<MvProbUpdates, BoolDecoderError> {
970 let mut out: MvProbUpdates = [[None; MV_PROB_COUNT]; 2];
971 for (i, ctx) in out.iter_mut().enumerate() {
972 for (j, slot) in ctx.iter_mut().enumerate() {
973 if dec.read_bool(MV_UPDATE_PROBS[i][j])? {
976 let x = dec.read_literal(7)? as u8;
980 *slot = Some(if x == 0 { 1 } else { x << 1 });
981 }
982 }
983 }
984 Ok(out)
985}
986
987fn parse_token_prob_update(
988 dec: &mut BoolDecoder<'_>,
989) -> Result<TokenProbUpdates, BoolDecoderError> {
990 let mut out: TokenProbUpdates = [[[[None; 11]; 3]; 8]; 4];
991 for (out_plane, prob_plane) in out.iter_mut().zip(COEFF_UPDATE_PROBS.iter()) {
1003 for (out_band, prob_band) in out_plane.iter_mut().zip(prob_plane.iter()) {
1004 for (out_ctx, prob_ctx) in out_band.iter_mut().zip(prob_band.iter()) {
1005 for (slot, &prob) in out_ctx.iter_mut().zip(prob_ctx.iter()) {
1006 let present = dec.read_bool(prob)?;
1007 *slot = if present {
1008 Some(dec.read_literal(8)? as u8)
1009 } else {
1010 None
1011 };
1012 }
1013 }
1014 }
1015 }
1016 Ok(out)
1017}
1018
1019#[doc(hidden)]
1025pub fn bench_parse_token_prob_update(
1026 dec: &mut BoolDecoder<'_>,
1027) -> Result<TokenProbUpdates, BoolDecoderError> {
1028 parse_token_prob_update(dec)
1029}
1030
1031fn read_signed_delta(dec: &mut BoolDecoder<'_>, n: u32) -> Result<i16, BoolDecoderError> {
1038 let magnitude = dec.read_literal(n)? as i16;
1039 let sign = dec.read_bool(128)?;
1040 Ok(if sign { -magnitude } else { magnitude })
1041}
1042
1043fn parse_update_segmentation(
1044 dec: &mut BoolDecoder<'_>,
1045) -> Result<UpdateSegmentation, BoolDecoderError> {
1046 let update_mb_segmentation_map = dec.read_bool(128)?;
1047 let update_segment_feature_data = dec.read_bool(128)?;
1048
1049 let mut segment_feature_mode_absolute = false;
1050 let mut quantizer_update: [Option<i16>; 4] = [None; 4];
1051 let mut loop_filter_update: [Option<i16>; 4] = [None; 4];
1052
1053 if update_segment_feature_data {
1054 segment_feature_mode_absolute = dec.read_bool(128)?;
1055 for slot in &mut quantizer_update {
1056 let present = dec.read_bool(128)?;
1057 *slot = if present {
1058 Some(read_signed_delta(dec, 7)?)
1059 } else {
1060 None
1061 };
1062 }
1063 for slot in &mut loop_filter_update {
1064 let present = dec.read_bool(128)?;
1065 *slot = if present {
1066 Some(read_signed_delta(dec, 6)?)
1067 } else {
1068 None
1069 };
1070 }
1071 }
1072
1073 let mut segment_prob: [Option<u8>; 3] = [None; 3];
1074 if update_mb_segmentation_map {
1075 for slot in &mut segment_prob {
1076 let present = dec.read_bool(128)?;
1077 *slot = if present {
1078 Some(dec.read_literal(8)? as u8)
1079 } else {
1080 None
1081 };
1082 }
1083 }
1084
1085 Ok(UpdateSegmentation {
1086 update_mb_segmentation_map,
1087 update_segment_feature_data,
1088 segment_feature_mode_absolute,
1089 quantizer_update,
1090 loop_filter_update,
1091 segment_prob,
1092 })
1093}
1094
1095fn parse_mb_lf_adjustments(dec: &mut BoolDecoder<'_>) -> Result<MbLfAdjustments, BoolDecoderError> {
1096 let loop_filter_adj_enable = dec.read_bool(128)?;
1097 let mut mode_ref_lf_delta_update = false;
1098 let mut ref_frame_delta_update: [Option<i16>; 4] = [None; 4];
1099 let mut mb_mode_delta_update: [Option<i16>; 4] = [None; 4];
1100
1101 if loop_filter_adj_enable {
1102 mode_ref_lf_delta_update = dec.read_bool(128)?;
1103 if mode_ref_lf_delta_update {
1104 for slot in &mut ref_frame_delta_update {
1105 let present = dec.read_bool(128)?;
1106 *slot = if present {
1107 Some(read_signed_delta(dec, 6)?)
1108 } else {
1109 None
1110 };
1111 }
1112 for slot in &mut mb_mode_delta_update {
1113 let present = dec.read_bool(128)?;
1114 *slot = if present {
1115 Some(read_signed_delta(dec, 6)?)
1116 } else {
1117 None
1118 };
1119 }
1120 }
1121 }
1122
1123 Ok(MbLfAdjustments {
1124 loop_filter_adj_enable,
1125 mode_ref_lf_delta_update,
1126 ref_frame_delta_update,
1127 mb_mode_delta_update,
1128 })
1129}
1130
1131fn parse_quant_indices(dec: &mut BoolDecoder<'_>) -> Result<QuantIndices, BoolDecoderError> {
1132 let y_ac_qi = dec.read_literal(7)? as u8;
1133 let mut deltas: [Option<i8>; 5] = [None; 5];
1134 for slot in &mut deltas {
1135 let present = dec.read_bool(128)?;
1136 *slot = if present {
1137 let mag = dec.read_literal(4)? as i8;
1140 let sign = dec.read_bool(128)?;
1141 Some(if sign { -mag } else { mag })
1142 } else {
1143 None
1144 };
1145 }
1146
1147 Ok(QuantIndices {
1148 y_ac_qi,
1149 y_dc_delta: deltas[0],
1150 y2_dc_delta: deltas[1],
1151 y2_ac_delta: deltas[2],
1152 uv_dc_delta: deltas[3],
1153 uv_ac_delta: deltas[4],
1154 })
1155}
1156
1157#[cfg(test)]
1158mod tests {
1159 use super::*;
1160
1161 struct TestEncoder {
1167 out: Vec<u8>,
1168 range: u32,
1169 bottom: u32,
1170 bit_count: i32,
1171 }
1172
1173 impl TestEncoder {
1174 fn new() -> Self {
1175 TestEncoder {
1176 out: Vec::new(),
1177 range: 255,
1178 bottom: 0,
1179 bit_count: 24,
1180 }
1181 }
1182
1183 fn write_bool(&mut self, prob: u8, val: bool) {
1184 let split = 1 + (((self.range - 1) * prob as u32) >> 8);
1185 if val {
1186 self.bottom = self.bottom.wrapping_add(split);
1187 self.range -= split;
1188 } else {
1189 self.range = split;
1190 }
1191 while self.range < 128 {
1192 self.range <<= 1;
1193 if (self.bottom >> 31) & 1 == 1 {
1194 let mut i = self.out.len();
1195 while i > 0 {
1196 i -= 1;
1197 if self.out[i] == 255 {
1198 self.out[i] = 0;
1199 } else {
1200 self.out[i] = self.out[i].wrapping_add(1);
1201 break;
1202 }
1203 }
1204 }
1205 self.bottom <<= 1;
1206 self.bit_count -= 1;
1207 if self.bit_count == 0 {
1208 let byte = (self.bottom >> 24) as u8;
1209 self.out.push(byte);
1210 self.bottom &= (1 << 24) - 1;
1211 self.bit_count = 8;
1212 }
1213 }
1214 }
1215
1216 fn write_literal(&mut self, value: u32, num_bits: u32) {
1217 for i in (0..num_bits).rev() {
1218 let bit = ((value >> i) & 1) == 1;
1219 self.write_bool(128, bit);
1220 }
1221 }
1222
1223 fn write_empty_token_prob_update(&mut self) {
1229 for plane in &COEFF_UPDATE_PROBS {
1230 for band in plane {
1231 for ctx in band {
1232 for &prob in ctx {
1233 self.write_bool(prob, false);
1234 }
1235 }
1236 }
1237 }
1238 }
1239
1240 fn write_empty_mv_prob_update(&mut self) {
1245 for ctx in &MV_UPDATE_PROBS {
1246 for &prob in ctx {
1247 self.write_bool(prob, false);
1248 }
1249 }
1250 }
1251
1252 fn finish(mut self) -> Vec<u8> {
1253 let c = self.bit_count;
1254 let mut v = self.bottom;
1255 if v & (1u32 << (32 - c)) != 0 {
1256 let mut i = self.out.len();
1257 while i > 0 {
1258 i -= 1;
1259 if self.out[i] == 255 {
1260 self.out[i] = 0;
1261 } else {
1262 self.out[i] = self.out[i].wrapping_add(1);
1263 break;
1264 }
1265 }
1266 }
1267 v <<= c & 7;
1268 let mut c_shift = c >> 3;
1269 while c_shift > 0 {
1270 v <<= 8;
1271 c_shift -= 1;
1272 }
1273 for _ in 0..4 {
1274 self.out.push((v >> 24) as u8);
1275 v <<= 8;
1276 }
1277 self.out
1278 }
1279 }
1280
1281 fn build_minimal_key_frame() -> Vec<u8> {
1284 let mut enc = TestEncoder::new();
1285 enc.write_bool(128, false); enc.write_bool(128, false); enc.write_bool(128, false); enc.write_bool(128, false); enc.write_literal(0, 6); enc.write_literal(0, 3); enc.write_bool(128, false); enc.write_literal(0, 2); enc.write_literal(4, 7); for _ in 0..5 {
1295 enc.write_bool(128, false); }
1297 enc.write_bool(128, true); enc.write_empty_token_prob_update();
1299 enc.write_bool(128, false); enc.finish()
1301 }
1302
1303 #[test]
1304 fn minimal_key_frame_round_trips() {
1305 let buf = build_minimal_key_frame();
1306 let hdr = Vp8CodedHeader::parse(&buf, true).unwrap();
1307 assert_eq!(hdr.color_space, Some(false));
1308 assert_eq!(hdr.clamping_type, Some(false));
1309 assert!(!hdr.segmentation_enabled);
1310 assert!(hdr.update_segmentation.is_none());
1311 assert!(!hdr.filter_type);
1312 assert_eq!(hdr.loop_filter_level, 0);
1313 assert_eq!(hdr.sharpness_level, 0);
1314 assert!(!hdr.mb_lf_adjustments.loop_filter_adj_enable);
1315 assert!(!hdr.mb_lf_adjustments.mode_ref_lf_delta_update);
1316 assert_eq!(hdr.log2_nbr_of_dct_partitions, 0);
1317 assert_eq!(hdr.nbr_of_dct_partitions, 1);
1318 assert_eq!(hdr.quant_indices.y_ac_qi, 4);
1319 assert_eq!(hdr.quant_indices.y_dc_delta, None);
1320 assert_eq!(hdr.quant_indices.uv_ac_delta, None);
1321 assert!(hdr.refresh_entropy_probs);
1322 assert_eq!(hdr.refresh_golden_frame, None);
1323 assert_eq!(hdr.refresh_alternate_frame, None);
1324 assert_eq!(hdr.refresh_last, None);
1325 assert!(!hdr.mb_no_skip_coeff);
1326 assert_eq!(hdr.prob_skip_false, None);
1327 }
1328
1329 #[test]
1330 fn key_frame_filter_and_partition_fields_round_trip() {
1331 let mut enc = TestEncoder::new();
1332 enc.write_bool(128, false); enc.write_bool(128, true); enc.write_bool(128, false); enc.write_bool(128, true); enc.write_literal(63, 6); enc.write_literal(7, 3); enc.write_bool(128, false); enc.write_literal(3, 2); enc.write_literal(127, 7); for _ in 0..5 {
1342 enc.write_bool(128, false);
1343 }
1344 enc.write_bool(128, false); enc.write_empty_token_prob_update();
1346 enc.write_bool(128, true); enc.write_literal(0xab, 8); let buf = enc.finish();
1349
1350 let hdr = Vp8CodedHeader::parse(&buf, true).unwrap();
1351 assert_eq!(hdr.clamping_type, Some(true));
1352 assert!(hdr.filter_type);
1353 assert_eq!(hdr.loop_filter_level, 63);
1354 assert_eq!(hdr.sharpness_level, 7);
1355 assert_eq!(hdr.log2_nbr_of_dct_partitions, 3);
1356 assert_eq!(hdr.nbr_of_dct_partitions, 8);
1357 assert_eq!(hdr.quant_indices.y_ac_qi, 127);
1358 assert!(!hdr.refresh_entropy_probs);
1359 assert!(hdr.mb_no_skip_coeff);
1360 assert_eq!(hdr.prob_skip_false, Some(0xab));
1361 }
1362
1363 #[test]
1364 fn mb_lf_adjustments_full_block_round_trips() {
1365 let mut enc = TestEncoder::new();
1370 enc.write_bool(128, false); enc.write_bool(128, false); enc.write_bool(128, false); enc.write_bool(128, false); enc.write_literal(0, 6); enc.write_literal(0, 3); enc.write_bool(128, true); enc.write_bool(128, true); enc.write_bool(128, true); enc.write_literal(1, 6);
1381 enc.write_bool(128, false); enc.write_bool(128, false); enc.write_bool(128, true);
1384 enc.write_literal(2, 6);
1385 enc.write_bool(128, true); enc.write_bool(128, true);
1387 enc.write_literal(5, 6);
1388 enc.write_bool(128, false);
1389 enc.write_bool(128, false);
1391 enc.write_bool(128, true);
1392 enc.write_literal(3, 6);
1393 enc.write_bool(128, false);
1394 enc.write_bool(128, true);
1395 enc.write_literal(1, 6);
1396 enc.write_bool(128, true);
1397 enc.write_bool(128, false);
1398 enc.write_literal(0, 2); enc.write_literal(0, 7); for _ in 0..5 {
1401 enc.write_bool(128, false);
1402 }
1403 enc.write_bool(128, false); enc.write_empty_token_prob_update();
1405 enc.write_bool(128, false); let buf = enc.finish();
1407
1408 let hdr = Vp8CodedHeader::parse(&buf, true).unwrap();
1409 let lf = hdr.mb_lf_adjustments;
1410 assert!(lf.loop_filter_adj_enable);
1411 assert!(lf.mode_ref_lf_delta_update);
1412 assert_eq!(
1413 lf.ref_frame_delta_update,
1414 [Some(1), None, Some(-2), Some(5)]
1415 );
1416 assert_eq!(lf.mb_mode_delta_update, [None, Some(3), Some(-1), None]);
1417 }
1418
1419 #[test]
1420 fn update_segmentation_block_round_trips() {
1421 let mut enc = TestEncoder::new();
1427 enc.write_bool(128, false); enc.write_bool(128, false); enc.write_bool(128, true); enc.write_bool(128, true); enc.write_bool(128, true); enc.write_bool(128, true); enc.write_bool(128, true);
1436 enc.write_literal(10, 7);
1437 enc.write_bool(128, false); enc.write_bool(128, false); enc.write_bool(128, false); enc.write_bool(128, true);
1441 enc.write_literal(20, 7);
1442 enc.write_bool(128, true); enc.write_bool(128, false);
1445 enc.write_bool(128, true);
1446 enc.write_literal(5, 6);
1447 enc.write_bool(128, false); enc.write_bool(128, true);
1449 enc.write_literal(3, 6);
1450 enc.write_bool(128, true); enc.write_bool(128, false);
1452 enc.write_bool(128, true);
1454 enc.write_literal(255, 8);
1455 enc.write_bool(128, false);
1456 enc.write_bool(128, true);
1457 enc.write_literal(0, 8);
1458
1459 enc.write_bool(128, false); enc.write_literal(0, 6); enc.write_literal(0, 3); enc.write_bool(128, false); enc.write_literal(0, 2); enc.write_literal(0, 7); for _ in 0..5 {
1467 enc.write_bool(128, false);
1468 }
1469 enc.write_bool(128, false); enc.write_empty_token_prob_update();
1471 enc.write_bool(128, false); let buf = enc.finish();
1473
1474 let hdr = Vp8CodedHeader::parse(&buf, true).unwrap();
1475 assert!(hdr.segmentation_enabled);
1476 let seg = hdr.update_segmentation.unwrap();
1477 assert!(seg.update_mb_segmentation_map);
1478 assert!(seg.update_segment_feature_data);
1479 assert!(seg.segment_feature_mode_absolute);
1480 assert_eq!(seg.quantizer_update, [Some(10), None, None, Some(-20)]);
1481 assert_eq!(seg.loop_filter_update, [None, Some(5), Some(-3), None]);
1482 assert_eq!(seg.segment_prob, [Some(255), None, Some(0)]);
1483 }
1484
1485 #[test]
1486 fn quant_indices_all_deltas_present() {
1487 let mut enc = TestEncoder::new();
1489 enc.write_bool(128, false); enc.write_bool(128, false); enc.write_bool(128, false); enc.write_bool(128, false); enc.write_literal(0, 6); enc.write_literal(0, 3); enc.write_bool(128, false); enc.write_literal(0, 2); enc.write_literal(64, 7); enc.write_bool(128, true);
1500 enc.write_literal(1, 4);
1501 enc.write_bool(128, false);
1502 enc.write_bool(128, true);
1504 enc.write_literal(2, 4);
1505 enc.write_bool(128, true);
1506 enc.write_bool(128, true);
1508 enc.write_literal(15, 4);
1509 enc.write_bool(128, false);
1510 enc.write_bool(128, true);
1512 enc.write_literal(15, 4);
1513 enc.write_bool(128, true);
1514 enc.write_bool(128, true);
1516 enc.write_literal(0, 4);
1517 enc.write_bool(128, false);
1518
1519 enc.write_bool(128, false); enc.write_empty_token_prob_update();
1521 enc.write_bool(128, false); let buf = enc.finish();
1523
1524 let hdr = Vp8CodedHeader::parse(&buf, true).unwrap();
1525 assert_eq!(hdr.quant_indices.y_ac_qi, 64);
1526 assert_eq!(hdr.quant_indices.y_dc_delta, Some(1));
1527 assert_eq!(hdr.quant_indices.y2_dc_delta, Some(-2));
1528 assert_eq!(hdr.quant_indices.y2_ac_delta, Some(15));
1529 assert_eq!(hdr.quant_indices.uv_dc_delta, Some(-15));
1530 assert_eq!(hdr.quant_indices.uv_ac_delta, Some(0));
1531 }
1532
1533 #[test]
1534 fn interframe_refresh_block_full_path() {
1535 let mut enc = TestEncoder::new();
1543 enc.write_bool(128, false); enc.write_bool(128, false); enc.write_literal(10, 6); enc.write_literal(2, 3); enc.write_bool(128, false); enc.write_literal(1, 2); enc.write_literal(8, 7); for _ in 0..5 {
1552 enc.write_bool(128, false);
1553 }
1554 enc.write_bool(128, false); enc.write_bool(128, true); enc.write_literal(2, 2); enc.write_bool(128, true); enc.write_bool(128, false); enc.write_bool(128, false); enc.write_bool(128, true); enc.write_empty_token_prob_update();
1564 enc.write_bool(128, true); enc.write_literal(200, 8); enc.write_literal(17, 8); enc.write_literal(130, 8); enc.write_literal(240, 8); enc.write_bool(128, false); enc.write_bool(128, false); enc.write_empty_mv_prob_update();
1572 let buf = enc.finish();
1573
1574 let hdr = Vp8CodedHeader::parse(&buf, false).unwrap();
1575 assert_eq!(hdr.color_space, None);
1576 assert_eq!(hdr.clamping_type, None);
1577 assert_eq!(hdr.loop_filter_level, 10);
1578 assert_eq!(hdr.sharpness_level, 2);
1579 assert_eq!(hdr.nbr_of_dct_partitions, 2);
1580 assert_eq!(hdr.refresh_golden_frame, Some(false));
1581 assert_eq!(hdr.refresh_alternate_frame, Some(true));
1582 assert_eq!(hdr.copy_buffer_to_golden, Some(2));
1583 assert_eq!(hdr.copy_buffer_to_alternate, None);
1584 assert_eq!(hdr.sign_bias_golden, Some(true));
1585 assert_eq!(hdr.sign_bias_alternate, Some(false));
1586 assert!(!hdr.refresh_entropy_probs);
1587 assert_eq!(hdr.refresh_last, Some(true));
1588 assert!(hdr.mb_no_skip_coeff);
1589 assert_eq!(hdr.prob_skip_false, Some(200));
1590 assert_eq!(hdr.prob_intra, Some(17));
1592 assert_eq!(hdr.prob_last, Some(130));
1593 assert_eq!(hdr.prob_gf, Some(240));
1594 assert_eq!(hdr.intra_y_mode_prob_update, None);
1595 assert_eq!(hdr.intra_uv_mode_prob_update, None);
1596 let mv = hdr.mv_prob_update.unwrap();
1597 assert!(mv[0].iter().all(|s| s.is_none()));
1598 assert!(mv[1].iter().all(|s| s.is_none()));
1599 }
1600
1601 #[test]
1602 fn mb_no_skip_coeff_false_omits_prob_skip_false() {
1603 let buf = build_minimal_key_frame();
1604 let hdr = Vp8CodedHeader::parse(&buf, true).unwrap();
1605 assert!(!hdr.mb_no_skip_coeff);
1606 assert_eq!(hdr.prob_skip_false, None);
1607 }
1608
1609 #[test]
1610 fn input_too_short_surfaces_bool_decoder_error() {
1611 let err = Vp8CodedHeader::parse(&[], true).unwrap_err();
1613 assert!(matches!(
1614 err,
1615 CodedHeaderError::BoolDecoder(BoolDecoderError::InputTooShort)
1616 ));
1617 let err = Vp8CodedHeader::parse(&[0x00], true).unwrap_err();
1619 assert!(matches!(
1620 err,
1621 CodedHeaderError::BoolDecoder(BoolDecoderError::InputTooShort)
1622 ));
1623 }
1624
1625 #[test]
1626 fn parses_real_tiny_fixture_partition() {
1627 let partition: [u8; 23] = [
1641 0x00, 0x47, 0x08, 0x85, 0x85, 0x88, 0x85, 0x84, 0x88, 0x02, 0x02, 0x02, 0x75, 0xaa,
1642 0x03, 0xf8, 0x03, 0xfa, 0x02, 0x0d, 0x4d, 0x18, 0x00,
1643 ];
1644 let hdr = Vp8CodedHeader::parse(&partition, true).unwrap();
1645 assert!(hdr.color_space.is_some());
1650 assert!(hdr.clamping_type.is_some());
1651 assert!(!hdr.segmentation_enabled);
1652 assert!(!hdr.filter_type, "filter_simple=0 per trace");
1653 assert_eq!(hdr.loop_filter_level, 1, "filter_level=1 per trace");
1654 assert_eq!(hdr.sharpness_level, 0, "filter_sharpness=0 per trace");
1655 assert!(
1656 hdr.mb_lf_adjustments.loop_filter_adj_enable,
1657 "lf_delta_enabled=1 per trace"
1658 );
1659 assert_eq!(
1660 hdr.log2_nbr_of_dct_partitions, 0,
1661 "num_partitions=1 per trace → log2=0"
1662 );
1663 assert_eq!(hdr.nbr_of_dct_partitions, 1);
1664 assert_eq!(hdr.quant_indices.y_ac_qi, 4, "qi_y_ac=4 per trace");
1665 assert_eq!(hdr.quant_indices.y_dc_delta, None, "qi_y_dc_d=0 per trace");
1666 assert_eq!(hdr.quant_indices.y2_dc_delta, None);
1667 assert_eq!(hdr.quant_indices.y2_ac_delta, None);
1668 assert_eq!(hdr.quant_indices.uv_dc_delta, None);
1669 assert_eq!(hdr.quant_indices.uv_ac_delta, None);
1670 assert!(hdr.refresh_entropy_probs, "update_probs=1 per trace");
1671 assert!(hdr.mb_no_skip_coeff, "mbskip_enabled=1 per trace");
1672 assert_eq!(hdr.prob_skip_false, Some(255), "prob_skip=255 per trace");
1673 assert_eq!(hdr.prob_intra, None);
1676 assert_eq!(hdr.prob_last, None);
1677 assert_eq!(hdr.prob_gf, None);
1678 assert_eq!(hdr.intra_y_mode_prob_update, None);
1679 assert_eq!(hdr.intra_uv_mode_prob_update, None);
1680 assert!(hdr.mv_prob_update.is_none());
1681 }
1682
1683 #[test]
1684 fn key_frame_omits_section_9_10_tail() {
1685 let buf = build_minimal_key_frame();
1688 let hdr = Vp8CodedHeader::parse(&buf, true).unwrap();
1689 assert_eq!(hdr.prob_intra, None);
1690 assert_eq!(hdr.prob_last, None);
1691 assert_eq!(hdr.prob_gf, None);
1692 assert_eq!(hdr.intra_y_mode_prob_update, None);
1693 assert_eq!(hdr.intra_uv_mode_prob_update, None);
1694 assert!(hdr.mv_prob_update.is_none());
1695 }
1696
1697 fn write_inter_prefix_through_skip_block(enc: &mut TestEncoder) {
1703 enc.write_bool(128, false); enc.write_bool(128, false); enc.write_literal(0, 6); enc.write_literal(0, 3); enc.write_bool(128, false); enc.write_literal(0, 2); enc.write_literal(0, 7); for _ in 0..5 {
1711 enc.write_bool(128, false); }
1713 enc.write_bool(128, false); enc.write_bool(128, false); enc.write_literal(0, 2); enc.write_literal(0, 2); enc.write_bool(128, false); enc.write_bool(128, false); enc.write_bool(128, false); enc.write_bool(128, false); enc.write_empty_token_prob_update();
1724 enc.write_bool(128, false); }
1726
1727 #[test]
1728 fn interframe_prob_intra_last_gf_round_trip() {
1729 let mut enc = TestEncoder::new();
1733 write_inter_prefix_through_skip_block(&mut enc);
1734 enc.write_literal(0x00, 8); enc.write_literal(0x80, 8); enc.write_literal(0xff, 8); enc.write_bool(128, false); enc.write_bool(128, false); enc.write_empty_mv_prob_update();
1740 let buf = enc.finish();
1741
1742 let hdr = Vp8CodedHeader::parse(&buf, false).unwrap();
1743 assert_eq!(hdr.prob_intra, Some(0));
1744 assert_eq!(hdr.prob_last, Some(128));
1745 assert_eq!(hdr.prob_gf, Some(255));
1746 }
1747
1748 #[test]
1749 fn interframe_intra_y_mode_prob_update_full_block() {
1750 let mut enc = TestEncoder::new();
1753 write_inter_prefix_through_skip_block(&mut enc);
1754 enc.write_literal(0, 8); enc.write_literal(0, 8); enc.write_literal(0, 8); enc.write_bool(128, true); for &p in &[200u8, 100, 33, 250] {
1759 enc.write_literal(p as u32, 8);
1760 }
1761 enc.write_bool(128, false); enc.write_empty_mv_prob_update();
1763 let buf = enc.finish();
1764
1765 let hdr = Vp8CodedHeader::parse(&buf, false).unwrap();
1766 assert_eq!(hdr.intra_y_mode_prob_update, Some([200, 100, 33, 250]));
1767 assert_eq!(hdr.intra_uv_mode_prob_update, None);
1768 }
1769
1770 #[test]
1771 fn interframe_intra_uv_mode_prob_update_full_block() {
1772 let mut enc = TestEncoder::new();
1775 write_inter_prefix_through_skip_block(&mut enc);
1776 enc.write_literal(0, 8); enc.write_literal(0, 8); enc.write_literal(0, 8); enc.write_bool(128, false); enc.write_bool(128, true); for &p in &[10u8, 20, 30] {
1782 enc.write_literal(p as u32, 8);
1783 }
1784 enc.write_empty_mv_prob_update();
1785 let buf = enc.finish();
1786
1787 let hdr = Vp8CodedHeader::parse(&buf, false).unwrap();
1788 assert_eq!(hdr.intra_y_mode_prob_update, None);
1789 assert_eq!(hdr.intra_uv_mode_prob_update, Some([10, 20, 30]));
1790 }
1791
1792 #[test]
1793 fn interframe_mv_prob_update_round_trip() {
1794 let mut enc = TestEncoder::new();
1799 write_inter_prefix_through_skip_block(&mut enc);
1800 enc.write_literal(0, 8); enc.write_literal(0, 8); enc.write_literal(0, 8); enc.write_bool(128, false); enc.write_bool(128, false); for (j, &update_prob) in MV_UPDATE_PROBS[0].iter().enumerate() {
1808 if j == 0 {
1809 enc.write_bool(update_prob, true);
1811 enc.write_literal(50, 7);
1812 } else if j == 5 {
1813 enc.write_bool(update_prob, true);
1816 enc.write_literal(0, 7);
1817 } else {
1818 enc.write_bool(update_prob, false);
1819 }
1820 }
1821 for (j, &update_prob) in MV_UPDATE_PROBS[1].iter().enumerate() {
1823 if j == MV_PROB_COUNT - 1 {
1824 enc.write_bool(update_prob, true);
1827 enc.write_literal(127, 7);
1828 } else {
1829 enc.write_bool(update_prob, false);
1830 }
1831 }
1832 let buf = enc.finish();
1833
1834 let hdr = Vp8CodedHeader::parse(&buf, false).unwrap();
1835 let mv = hdr.mv_prob_update.unwrap();
1836 assert_eq!(mv[0][0], Some(100));
1838 assert_eq!(mv[0][1], None);
1839 assert_eq!(mv[0][5], Some(1));
1840 for (j, &slot) in mv[0].iter().enumerate() {
1841 if j != 0 && j != 5 {
1842 assert!(slot.is_none(), "row[{j}] should be None");
1843 }
1844 }
1845 assert_eq!(mv[1][MV_PROB_COUNT - 1], Some(254));
1847 for (j, &slot) in mv[1].iter().enumerate().take(MV_PROB_COUNT - 1) {
1848 assert!(slot.is_none(), "col[{j}] should be None");
1849 }
1850 }
1851
1852 #[test]
1853 fn mv_default_context_matches_spec_listing() {
1854 assert_eq!(DEFAULT_MV_CONTEXT[0][0], 162); assert_eq!(DEFAULT_MV_CONTEXT[0][1], 128); assert_eq!(DEFAULT_MV_CONTEXT[0][2], 225); assert_eq!(DEFAULT_MV_CONTEXT[0][8], 156); assert_eq!(DEFAULT_MV_CONTEXT[0][9], 128); assert_eq!(DEFAULT_MV_CONTEXT[0][18], 254); assert_eq!(DEFAULT_MV_CONTEXT[1][0], 164); assert_eq!(DEFAULT_MV_CONTEXT[1][1], 128); assert_eq!(DEFAULT_MV_CONTEXT[1][2], 204); assert_eq!(DEFAULT_MV_CONTEXT[1][8], 228); assert_eq!(DEFAULT_MV_CONTEXT[1][9], 128); assert_eq!(DEFAULT_MV_CONTEXT[1][18], 254); assert_eq!(MV_UPDATE_PROBS[0][0], 237);
1874 assert_eq!(MV_UPDATE_PROBS[0][18], 254);
1875 assert_eq!(MV_UPDATE_PROBS[1][0], 231);
1876 assert_eq!(MV_UPDATE_PROBS[1][18], 254);
1877 }
1878
1879 fn reference_parse_token_prob_update_indexed(
1889 dec: &mut BoolDecoder<'_>,
1890 ) -> Result<TokenProbUpdates, BoolDecoderError> {
1891 let mut out: TokenProbUpdates = [[[[None; 11]; 3]; 8]; 4];
1892 for (i, plane) in out.iter_mut().enumerate() {
1893 for (j, band) in plane.iter_mut().enumerate() {
1894 for (k, ctx) in band.iter_mut().enumerate() {
1895 for (t, slot) in ctx.iter_mut().enumerate() {
1896 let present = dec.read_bool(COEFF_UPDATE_PROBS[i][j][k][t])?;
1897 *slot = if present {
1898 Some(dec.read_literal(8)? as u8)
1899 } else {
1900 None
1901 };
1902 }
1903 }
1904 }
1905 }
1906 Ok(out)
1907 }
1908
1909 #[test]
1910 fn parse_token_prob_update_matches_indexed_reference() {
1911 let mut intended: TokenProbUpdates = [[[[None; 11]; 3]; 8]; 4];
1913 let mut salt: u32 = 1;
1914 for (i, plane) in intended.iter_mut().enumerate() {
1915 for (j, band) in plane.iter_mut().enumerate() {
1916 for (k, ctx) in band.iter_mut().enumerate() {
1917 for (t, slot) in ctx.iter_mut().enumerate() {
1918 salt = salt.wrapping_mul(1103515245).wrapping_add(12345);
1919 if (i + j + k + t + (salt >> 16) as usize).is_multiple_of(5) {
1921 *slot = Some(((salt >> 8) & 0xFF) as u8);
1922 }
1923 }
1924 }
1925 }
1926 }
1927
1928 let mut enc = TestEncoder::new();
1930 for (i, plane) in intended.iter().enumerate() {
1931 for (j, band) in plane.iter().enumerate() {
1932 for (k, ctx) in band.iter().enumerate() {
1933 for (t, slot) in ctx.iter().enumerate() {
1934 let p = COEFF_UPDATE_PROBS[i][j][k][t];
1935 match slot {
1936 Some(prob) => {
1937 enc.write_bool(p, true);
1938 enc.write_literal(*prob as u32, 8);
1939 }
1940 None => enc.write_bool(p, false),
1941 }
1942 }
1943 }
1944 }
1945 }
1946 enc.write_bool(128, false);
1948 let bytes = enc.finish();
1949
1950 let mut dec_prod = BoolDecoder::init(&bytes).unwrap();
1951 let produced = parse_token_prob_update(&mut dec_prod).unwrap();
1952
1953 let mut dec_ref = BoolDecoder::init(&bytes).unwrap();
1954 let reference = reference_parse_token_prob_update_indexed(&mut dec_ref).unwrap();
1955
1956 assert_eq!(
1957 produced, reference,
1958 "zipped loop must match the 4-level-indexed reference loop"
1959 );
1960 assert_eq!(
1961 produced, intended,
1962 "decode must recover the intended payload"
1963 );
1964 assert_eq!(
1966 dec_prod.remaining_input(),
1967 dec_ref.remaining_input(),
1968 "both parsers must leave the decoder at the same stream position"
1969 );
1970 }
1971}