S10: aac raw_data_block FIL arm moves to fill_element (same reads, same SBR calls, same order); the match keeps every arm
Browse files at this commitcc3e7cc7ecbd59384fa628b6216e1249d8969de3Parent: 1ac57b31 changed file +81 -71
- crates/ec-aac/src/decode.rs +81 -71
crates/ec-aac/src/decode.rsmodified+81 -71
| @@ -788,6 +788,86 @@ impl BlockDecoder { | ||
| 788 | 788 | } |
| 789 | 789 | } |
| 790 | 790 | |
| 791 | + fn fill_element( | |
| 792 | + &mut self, | |
| 793 | + r: &mut BitReader<'_>, | |
| 794 | + out: &mut Vec<Vec<f32>>, | |
| 795 | + pending: &mut Option<(u8, usize, usize, bool)>, | |
| 796 | + ) -> Result<()> { | |
| 797 | + let mut count = r.read_bits(4)? as u64; | |
| 798 | + if count == 15 { | |
| 799 | + // 15 + esc - 1, written so an escape of 0 cannot underflow. | |
| 800 | + count = 14 + r.read_bits(8)? as u64; | |
| 801 | + } | |
| 802 | + let end_bit = r.bit_position() + count * 8; | |
| 803 | + // Implicit SBR: no SBR in the config, but this FIL is the first | |
| 804 | + // to carry an SBR extension right after an element. Arm the | |
| 805 | + // chain for it; if its payload does not parse, disarm again. | |
| 806 | + let arming = match self.implicit_core { | |
| 807 | + Some(core) | |
| 808 | + if self.sbr.is_none() | |
| 809 | + && pending.is_some() | |
| 810 | + && r.bits_remaining() >= 4 | |
| 811 | + && matches!(r.peek_bits(4)?, 13 | 14) => | |
| 812 | + { | |
| 813 | + self.sbr = Some(crate::sbr_chain::SbrChain::new(core * 2)); | |
| 814 | + true | |
| 815 | + } | |
| 816 | + _ => false, | |
| 817 | + }; | |
| 818 | + let mut applied = false; | |
| 819 | + if let (Some(sbr), Some((tag, start, n, is_cpe))) = | |
| 820 | + (self.sbr.as_mut(), pending.take()) | |
| 821 | + { | |
| 822 | + // extension_payload(): bs_extension_type(4), then | |
| 823 | + // (for SBR) an optional 10-bit CRC, then | |
| 824 | + // sbr_extension_data() itself. | |
| 825 | + if r.bits_remaining() >= 4 { | |
| 826 | + let ext_type = r.peek_bits(4)?; | |
| 827 | + if ext_type == 13 || ext_type == 14 { | |
| 828 | + r.skip_bits(4)?; | |
| 829 | + if ext_type == 14 && r.bits_remaining() >= 10 { | |
| 830 | + r.skip_bits(10)?; // bs_sbr_crc_bits | |
| 831 | + } | |
| 832 | + if let Some(data) = sbr.parse(r, tag, is_cpe) { | |
| 833 | + if data.ps.is_some() && (arming || self.implicit_on) { | |
| 834 | + self.implicit_ps = true; | |
| 835 | + } | |
| 836 | + sbr.apply_planes(tag, is_cpe, &data, out, start, n)?; | |
| 837 | + applied = true; | |
| 838 | + } | |
| 839 | + } | |
| 840 | + } | |
| 841 | + if !applied && !arming { | |
| 842 | + // No fresh SBR payload this frame (no SBR fill | |
| 843 | + // element, or one whose extension_type wasn't | |
| 844 | + // SBR, or a parse failure): §4.6.18 still | |
| 845 | + // requires doubled-rate reconstruction, using | |
| 846 | + // the last successfully parsed frame's data. | |
| 847 | + sbr.apply_last_planes(tag, is_cpe, out, start, n)?; | |
| 848 | + } | |
| 849 | + } | |
| 850 | + if arming { | |
| 851 | + if applied { | |
| 852 | + self.implicit_on = true; | |
| 853 | + self.implicit_core = None; | |
| 854 | + } else { | |
| 855 | + // The payload did not parse: this was not SBR after all. | |
| 856 | + self.sbr = None; | |
| 857 | + } | |
| 858 | + } | |
| 859 | + // Resync to the FIL's declared byte boundary regardless | |
| 860 | + // of how many bits SBR parsing above actually consumed: | |
| 861 | + // a parse error or trailing padding must never desync | |
| 862 | + // the elements that follow. | |
| 863 | + let now = r.bit_position(); | |
| 864 | + if now < end_bit { | |
| 865 | + r.skip_bits(end_bit - now)?; | |
| 866 | + } | |
| 867 | + *pending = None; | |
| 868 | + Ok(()) | |
| 869 | + } | |
| 870 | + | |
| 791 | 871 | /// Decodes one `raw_data_block`, returning one PCM plane per channel in |
| 792 | 872 | /// bitstream element order. |
| 793 | 873 | pub fn raw_data_block(&mut self, r: &mut BitReader<'_>) -> Result<Vec<Vec<f32>>> { |
| @@ -887,77 +967,7 @@ impl BlockDecoder { | ||
| 887 | 967 | pending = None; |
| 888 | 968 | } |
| 889 | 969 | 6 => { |
| 890 | - let mut count = r.read_bits(4)? as u64; | |
| 891 | - if count == 15 { | |
| 892 | - // 15 + esc - 1, written so an escape of 0 cannot underflow. | |
| 893 | - count = 14 + r.read_bits(8)? as u64; | |
| 894 | - } | |
| 895 | - let end_bit = r.bit_position() + count * 8; | |
| 896 | - // Implicit SBR: no SBR in the config, but this FIL is the first | |
| 897 | - // to carry an SBR extension right after an element. Arm the | |
| 898 | - // chain for it; if its payload does not parse, disarm again. | |
| 899 | - let arming = match self.implicit_core { | |
| 900 | - Some(core) | |
| 901 | - if self.sbr.is_none() | |
| 902 | - && pending.is_some() | |
| 903 | - && r.bits_remaining() >= 4 | |
| 904 | - && matches!(r.peek_bits(4)?, 13 | 14) => | |
| 905 | - { | |
| 906 | - self.sbr = Some(crate::sbr_chain::SbrChain::new(core * 2)); | |
| 907 | - true | |
| 908 | - } | |
| 909 | - _ => false, | |
| 910 | - }; | |
| 911 | - let mut applied = false; | |
| 912 | - if let (Some(sbr), Some((tag, start, n, is_cpe))) = | |
| 913 | - (self.sbr.as_mut(), pending.take()) | |
| 914 | - { | |
| 915 | - // extension_payload(): bs_extension_type(4), then | |
| 916 | - // (for SBR) an optional 10-bit CRC, then | |
| 917 | - // sbr_extension_data() itself. | |
| 918 | - if r.bits_remaining() >= 4 { | |
| 919 | - let ext_type = r.peek_bits(4)?; | |
| 920 | - if ext_type == 13 || ext_type == 14 { | |
| 921 | - r.skip_bits(4)?; | |
| 922 | - if ext_type == 14 && r.bits_remaining() >= 10 { | |
| 923 | - r.skip_bits(10)?; // bs_sbr_crc_bits | |
| 924 | - } | |
| 925 | - if let Some(data) = sbr.parse(r, tag, is_cpe) { | |
| 926 | - if data.ps.is_some() && (arming || self.implicit_on) { | |
| 927 | - self.implicit_ps = true; | |
| 928 | - } | |
| 929 | - sbr.apply_planes(tag, is_cpe, &data, &mut out, start, n)?; | |
| 930 | - applied = true; | |
| 931 | - } | |
| 932 | - } | |
| 933 | - } | |
| 934 | - if !applied && !arming { | |
| 935 | - // No fresh SBR payload this frame (no SBR fill | |
| 936 | - // element, or one whose extension_type wasn't | |
| 937 | - // SBR, or a parse failure): §4.6.18 still | |
| 938 | - // requires doubled-rate reconstruction, using | |
| 939 | - // the last successfully parsed frame's data. | |
| 940 | - sbr.apply_last_planes(tag, is_cpe, &mut out, start, n)?; | |
| 941 | - } | |
| 942 | - } | |
| 943 | - if arming { | |
| 944 | - if applied { | |
| 945 | - self.implicit_on = true; | |
| 946 | - self.implicit_core = None; | |
| 947 | - } else { | |
| 948 | - // The payload did not parse: this was not SBR after all. | |
| 949 | - self.sbr = None; | |
| 950 | - } | |
| 951 | - } | |
| 952 | - // Resync to the FIL's declared byte boundary regardless | |
| 953 | - // of how many bits SBR parsing above actually consumed: | |
| 954 | - // a parse error or trailing padding must never desync | |
| 955 | - // the elements that follow. | |
| 956 | - let now = r.bit_position(); | |
| 957 | - if now < end_bit { | |
| 958 | - r.skip_bits(end_bit - now)?; | |
| 959 | - } | |
| 960 | - pending = None; | |
| 970 | + self.fill_element(r, &mut out, &mut pending)?; | |
| 961 | 971 | } |
| 962 | 972 | _ => { |
| 963 | 973 | // The block ended with no FIL element ever following the |