forge
Explore
Log in

Don't you dare go Hollow

S10: aac encoder passes WindowSequence instead of a short: bool; Coded drops its duplicate short field (derived from seq); same values

Browse files at this commit

a06b46bd9e92df0af2538aa75947147a3691daa7Ahmet Kaan GümüşParent: 356e41e
crates/ec-aac/src/encode.rsmodified+37 -17
@@ -538,11 +538,11 @@ impl AacEncoder {
538 let mut ms = vec![false; max_sfb];538 let mut ms = vec![false; max_sfb];
539 if *element == Element::Cpe && self.config.mid_side {539 if *element == Element::Cpe && self.config.mid_side {
540 for b in 0..max_sfb {540 for b in 0..max_sfb {
541- if ms_wins(&pair[0], &pair[1], swb, b, short) {541+ if ms_wins(&pair[0], &pair[1], swb, b, seq) {
542 ms[b] = true;542 ms[b] = true;
543 }543 }
544 }544 }
545- apply_ms(&mut pair, &ms, swb, max_sfb, short);545+ apply_ms(&mut pair, &ms, swb, max_sfb, seq);
546 }546 }
547 elements.push(ElementPlan {547 elements.push(ElementPlan {
548 element: *element,548 element: *element,
@@ -563,7 +563,7 @@ impl AacEncoder {
563 .map(|plan| {563 .map(|plan| {
564 plan.frames564 plan.frames
565 .iter()565 .iter()
566- .map(|frame| band_profile(frame, swb, max_sfb, short))566+ .map(|frame| band_profile(frame, swb, max_sfb, seq))
567 .collect()567 .collect()
568 })568 })
569 .collect();569 .collect();
@@ -759,14 +759,14 @@ impl AacEncoder {
759 max_sfb,759 max_sfb,
760 b,760 b,
761 floor[b],761 floor[b],
762- || self.band_noise(frame, swb, b, floor[b], windows, short),762+ || self.band_noise(frame, swb, b, floor[b], windows, seq),
763 ) <= target763 ) <= target
764 {764 {
765 let (mut lo, mut hi) = (floor[b], 255);765 let (mut lo, mut hi) = (floor[b], 255);
766 while lo < hi {766 while lo < hi {
767 let mid = lo + (hi - lo + 1) / 2;767 let mid = lo + (hi - lo + 1) / 2;
768 if memo.noise(ch_slot, max_sfb, b, mid, || {768 if memo.noise(ch_slot, max_sfb, b, mid, || {
769- self.band_noise(frame, swb, b, mid, windows, short)769+ self.band_noise(frame, swb, b, mid, windows, seq)
770 }) <= target {770 }) <= target {
771 lo = mid;771 lo = mid;
772 } else {772 } else {
@@ -848,7 +848,7 @@ impl AacEncoder {
848 if books[b] == 0 {848 if books[b] == 0 {
849 continue;849 continue;
850 }850 }
851- bits += band_bits(&quant, swb, b, books[b], windows, short);851+ bits += band_bits(&quant, swb, b, books[b], windows, seq);
852 let value = prev.map_or(60, |p| (60 + sf[b] - p).clamp(0, 120));852 let value = prev.map_or(60, |p| (60 + sf[b] - p).clamp(0, 120));
853 bits += i32::from(SCALEFACTOR_CODES[value as usize].0);853 bits += i32::from(SCALEFACTOR_CODES[value as usize].0);
854 prev = Some(sf[b]);854 prev = Some(sf[b]);
@@ -871,7 +871,6 @@ impl AacEncoder {
871 books,871 books,
872 quant,872 quant,
873 bits,873 bits,
874- short,
875 }874 }
876 }875 }
877 876
@@ -883,8 +882,9 @@ impl AacEncoder {
883 band: usize,882 band: usize,
884 sf: i32,883 sf: i32,
885 windows: usize,884 windows: usize,
886- short: bool,885+ seq: WindowSequence,
887 ) -> f32 {886 ) -> f32 {
887+ let short = seq == WindowSequence::EightShort;
888 let (lo, hi) = (usize::from(swb[band]), usize::from(swb[band + 1]));888 let (lo, hi) = (usize::from(swb[band]), usize::from(swb[band + 1]));
889 let gain = pow_quarter(sf - SF_OFFSET);889 let gain = pow_quarter(sf - SF_OFFSET);
890 let gain_inv = pow_quarter(SF_OFFSET - sf);890 let gain_inv = pow_quarter(SF_OFFSET - sf);
@@ -922,12 +922,18 @@ struct Coded {
922 books: Vec<u8>,922 books: Vec<u8>,
923 quant: Vec<i32>,923 quant: Vec<i32>,
924 bits: i32,924 bits: i32,
925- short: bool,
926 }925 }
927 926
928 /// True when mid/side splits this band's energy more lopsidedly than left/right927 /// True when mid/side splits this band's energy more lopsidedly than left/right
929 /// does, which is when it costs fewer bits.928 /// does, which is when it costs fewer bits.
930-fn ms_wins(l: &ChannelFrame, r: &ChannelFrame, swb: &[u16], band: usize, short: bool) -> bool {929+fn ms_wins(
930+ l: &ChannelFrame,
931+ r: &ChannelFrame,
932+ swb: &[u16],
933+ band: usize,
934+ seq: WindowSequence,
935+) -> bool {
936+ let short = seq == WindowSequence::EightShort;
931 let (lo, hi) = (usize::from(swb[band]), usize::from(swb[band + 1]));937 let (lo, hi) = (usize::from(swb[band]), usize::from(swb[band + 1]));
932 let windows = if short { 8 } else { 1 };938 let windows = if short { 8 } else { 1 };
933 let (mut el, mut er, mut em, mut es) = (0.0f64, 0.0f64, 0.0f64, 0.0f64);939 let (mut el, mut er, mut em, mut es) = (0.0f64, 0.0f64, 0.0f64, 0.0f64);
@@ -958,7 +964,13 @@ struct BandProfile {
958 floor: Vec<i32>,964 floor: Vec<i32>,
959 }965 }
960 966
961-fn band_profile(frame: &ChannelFrame, swb: &[u16], max_sfb: usize, short: bool) -> BandProfile {967+fn band_profile(
968+ frame: &ChannelFrame,
969+ swb: &[u16],
970+ max_sfb: usize,
971+ seq: WindowSequence,
972+) -> BandProfile {
973+ let short = seq == WindowSequence::EightShort;
962 let windows = if short { 8 } else { 1 };974 let windows = if short { 8 } else { 1 };
963 let mut peak = vec![0.0f32; max_sfb];975 let mut peak = vec![0.0f32; max_sfb];
964 let mut energy = vec![0.0f32; max_sfb];976 let mut energy = vec![0.0f32; max_sfb];
@@ -1024,7 +1036,14 @@ impl NoiseMemo {
1024 }1036 }
1025 }1037 }
1026 1038
1027-fn apply_ms(pair: &mut [ChannelFrame], ms: &[bool], swb: &[u16], max_sfb: usize, short: bool) {1039+fn apply_ms(
1040+ pair: &mut [ChannelFrame],
1041+ ms: &[bool],
1042+ swb: &[u16],
1043+ max_sfb: usize,
1044+ seq: WindowSequence,
1045+) {
1046+ let short = seq == WindowSequence::EightShort;
1028 let windows = if short { 8 } else { 1 };1047 let windows = if short { 8 } else { 1 };
1029 for b in 0..max_sfb {1048 for b in 0..max_sfb {
1030 if !ms[b] {1049 if !ms[b] {
@@ -1125,8 +1144,9 @@ fn band_bits(
1125 band: usize,1144 band: usize,
1126 book: u8,1145 book: u8,
1127 windows: usize,1146 windows: usize,
1128- short: bool,1147+ seq: WindowSequence,
1129 ) -> i32 {1148 ) -> i32 {
1149+ let short = seq == WindowSequence::EightShort;
1130 let cb = &CODEBOOKS[usize::from(book) - 1];1150 let cb = &CODEBOOKS[usize::from(book) - 1];
1131 let (lo, hi) = (usize::from(swb[band]), usize::from(swb[band + 1]));1151 let (lo, hi) = (usize::from(swb[band]), usize::from(swb[band + 1]));
1132 let dim = usize::from(cb.dim);1152 let dim = usize::from(cb.dim);
@@ -1218,7 +1238,7 @@ fn write_ics_info(w: &mut BitWriter, c: &Coded) {
1218 };1238 };
1219 w.write_bits(seq, 2);1239 w.write_bits(seq, 2);
1220 w.write_bit(false); // sine window shape1240 w.write_bit(false); // sine window shape
1221- if c.short {1241+ if c.seq == WindowSequence::EightShort {
1222 w.write_bits(c.max_sfb as u32, 4);1242 w.write_bits(c.max_sfb as u32, 4);
1223 w.write_bits(0x7F, 7); // one group of eight windows1243 w.write_bits(0x7F, 7); // one group of eight windows
1224 } else {1244 } else {
@@ -1235,7 +1255,7 @@ fn write_ics(w: &mut BitWriter, c: &Coded, own_info: bool) {
1235 write_ics_info(w, c);1255 write_ics_info(w, c);
1236 }1256 }
1237 // section_data1257 // section_data
1238- let esc_bits = if c.short { 3 } else { 5 };1258+ let esc_bits = if c.seq == WindowSequence::EightShort { 3 } else { 5 };
1239 let esc = (1u32 << esc_bits) - 1;1259 let esc = (1u32 << esc_bits) - 1;
1240 let mut b = 0usize;1260 let mut b = 0usize;
1241 while b < c.max_sfb {1261 while b < c.max_sfb {
@@ -1267,7 +1287,7 @@ fn write_ics(w: &mut BitWriter, c: &Coded, own_info: bool) {
1267 w.write_bit(false); // tns_data_present1287 w.write_bit(false); // tns_data_present
1268 w.write_bit(false); // gain_control_data_present1288 w.write_bit(false); // gain_control_data_present
1269 // spectral_data1289 // spectral_data
1270- let windows = if c.short { 8 } else { 1 };1290+ let windows = if c.seq == WindowSequence::EightShort { 8 } else { 1 };
1271 for b in 0..c.max_sfb {1291 for b in 0..c.max_sfb {
1272 let book = c.books[b];1292 let book = c.books[b];
1273 if book == 0 {1293 if book == 0 {
@@ -1277,7 +1297,7 @@ fn write_ics(w: &mut BitWriter, c: &Coded, own_info: bool) {
1277 let dim = usize::from(cb.dim);1297 let dim = usize::from(cb.dim);
1278 let (lo, hi) = (usize::from(c.swb[b]), usize::from(c.swb[b + 1]));1298 let (lo, hi) = (usize::from(c.swb[b]), usize::from(c.swb[b + 1]));
1279 for win in 0..windows {1299 for win in 0..windows {
1280- let base = if c.short { win * SHORT_LEN } else { 0 };1300+ let base = if c.seq == WindowSequence::EightShort { win * SHORT_LEN } else { 0 };
1281 let mut k = lo;1301 let mut k = lo;
1282 while k < hi {1302 while k < hi {
1283 let end = (k + dim).min(hi);1303 let end = (k + dim).min(hi);