edith_codecs
Clone
https://git.tahinli.com/Tahinli/edith_codecs.git
0 watching·0 forks·Rust·370.3 MiB·9b67700·Tahinli·3 days ago·4021 commits
| 1 | //! DV audio: the PCM carried in the audio DIF blocks (port of `dv_extract_audio` in FFmpeg's |
| 2 | //! `libavformat/dv.c`). 16-bit linear and 12-bit non-linear data both come out as 16-bit linear. |
| 3 | |
| 4 | use crate::profile::{Profile, frame_profile}; |
| 5 | |
| 6 | const DV_AUDIO_SOURCE: u8 = 0x50; |
| 7 | const FREQUENCIES: [u32; 3] = [48000, 44100, 32000]; |
| 8 | |
| 9 | /// The audio of one DV frame. |
| 10 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 11 | pub struct DvAudio { |
| 12 | /// Sample rate in Hz (48000, 44100 or 32000). |
| 13 | pub sample_rate: u32, |
| 14 | /// Samples per channel in this frame. |
| 15 | pub samples: usize, |
| 16 | /// True when the source was 12-bit non-linear (already expanded to 16-bit linear). |
| 17 | pub nonlinear_12bit: bool, |
| 18 | /// One entry per stereo pair announced by the audio source pack (1, 2 or 4). Each is |
| 19 | /// interleaved L/R `i16`, `2 * samples` long; empty when the pair is not carried by this |
| 20 | /// frame (the second half of a 720p frame pair). |
| 21 | pub pairs: Vec<Vec<i16>>, |
| 22 | } |
| 23 | |
| 24 | fn audio_12to16(sample: u16) -> u16 { |
| 25 | let sample = if sample < 0x800 { sample } else { sample | 0xf000 }; |
| 26 | let shift = (sample & 0xf00) >> 8; |
| 27 | if !(0x2..=0xd).contains(&shift) { |
| 28 | sample |
| 29 | } else if shift < 0x8 { |
| 30 | let shift = shift - 1; |
| 31 | sample.wrapping_sub(256 * shift).wrapping_shl(u32::from(shift)) |
| 32 | } else { |
| 33 | let shift = 0xe - shift; |
| 34 | (sample.wrapping_add(256 * shift + 1).wrapping_shl(u32::from(shift))).wrapping_sub(1) |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | fn extract_pack(frame: &[u8], t: u8) -> Option<usize> { |
| 39 | for c in 0..10usize { |
| 40 | let offs = if c & 1 == 1 { 80 * 6 + 3 + c * 12000 } else { 80 * 6 + 80 * 16 * 3 + 3 + c * 12000 }; |
| 41 | if frame.get(offs) == Some(&t) { |
| 42 | return Some(offs); |
| 43 | } |
| 44 | } |
| 45 | None |
| 46 | } |
| 47 | |
| 48 | /// Extract the audio of a DV frame. `None` when the frame carries no (decodable) audio. |
| 49 | pub fn extract_audio(frame: &[u8]) -> Option<DvAudio> { |
| 50 | let sys = frame_profile(None, frame)?; |
| 51 | if frame.len() < sys.frame_size { |
| 52 | return None; |
| 53 | } |
| 54 | extract_audio_with(frame, sys) |
| 55 | } |
| 56 | |
| 57 | pub(crate) fn extract_audio_with(frame: &[u8], sys: &Profile) -> Option<DvAudio> { |
| 58 | let off = extract_pack(frame, DV_AUDIO_SOURCE)?; |
| 59 | let smpls = usize::from(frame[off + 1] & 0x3f); |
| 60 | let freq = usize::from(frame[off + 4] >> 3 & 7); |
| 61 | let stype = frame[off + 3] & 0x1f; |
| 62 | let quant = frame[off + 4] & 7; |
| 63 | if freq >= FREQUENCIES.len() || stype > 3 || quant > 1 { |
| 64 | return None; |
| 65 | } |
| 66 | let sr = FREQUENCIES[freq]; |
| 67 | let mut ach = [1usize, 0, 2, 4][usize::from(stype)]; |
| 68 | if ach == 1 && quant != 0 && freq == 2 { |
| 69 | ach = 2; |
| 70 | } |
| 71 | if ach == 0 { |
| 72 | return None; |
| 73 | } |
| 74 | let size = (sys.audio_min_samples[freq] + smpls) * 4; |
| 75 | let half_ch = sys.difseg_size / 2; |
| 76 | let mut ipcm = if sys.height == 720 && frame[1] & 0x0c == 0 { 2 } else { 0 }; |
| 77 | let mut pcm: Vec<Vec<u8>> = vec![vec![0u8; 8192]; ach.max(4)]; |
| 78 | let available = ach; |
| 79 | |
| 80 | if ipcm + sys.n_difchan <= if quant == 1 { 2 } else { 4 } { |
| 81 | let mut pos = 0usize; |
| 82 | 'chans: for _chan in 0..sys.n_difchan { |
| 83 | let mut cur = ipcm; |
| 84 | ipcm += 1; |
| 85 | if cur >= available { |
| 86 | break; |
| 87 | } |
| 88 | for i in 0..sys.difseg_size { |
| 89 | pos += 6 * 80; |
| 90 | if quant == 1 && i == half_ch { |
| 91 | cur = ipcm; |
| 92 | ipcm += 1; |
| 93 | if cur >= available { |
| 94 | break 'chans; |
| 95 | } |
| 96 | } |
| 97 | for j in 0..9 { |
| 98 | let mut d = 8; |
| 99 | while d < 80 { |
| 100 | let f = |k: usize| frame[pos + k]; |
| 101 | if quant == 0 { |
| 102 | let of = usize::from(sys.audio_shuffle[i][j]) + (d - 8) / 2 * sys.audio_stride; |
| 103 | if of * 2 < size { |
| 104 | pcm[cur][of * 2] = f(d + 1); |
| 105 | pcm[cur][of * 2 + 1] = f(d); |
| 106 | if pcm[cur][of * 2 + 1] == 0x80 && pcm[cur][of * 2] == 0x00 { |
| 107 | pcm[cur][of * 2 + 1] = 0; |
| 108 | } |
| 109 | } |
| 110 | } else { |
| 111 | let mut lc = (u16::from(f(d)) << 4) | (u16::from(f(d + 2)) >> 4); |
| 112 | let mut rc = (u16::from(f(d + 1)) << 4) | (u16::from(f(d + 2)) & 0x0f); |
| 113 | lc = if lc == 0x800 { 0 } else { audio_12to16(lc) }; |
| 114 | rc = if rc == 0x800 { 0 } else { audio_12to16(rc) }; |
| 115 | let of = usize::from(sys.audio_shuffle[i % half_ch][j]) + (d - 8) / 3 * sys.audio_stride; |
| 116 | if of * 2 < size { |
| 117 | pcm[cur][of * 2] = (lc & 0xff) as u8; |
| 118 | pcm[cur][of * 2 + 1] = (lc >> 8) as u8; |
| 119 | let of = usize::from(sys.audio_shuffle[i % half_ch + half_ch][j]) + (d - 8) / 3 * sys.audio_stride; |
| 120 | if of * 2 + 1 < pcm[cur].len() { |
| 121 | pcm[cur][of * 2] = (rc & 0xff) as u8; |
| 122 | pcm[cur][of * 2 + 1] = (rc >> 8) as u8; |
| 123 | } |
| 124 | d += 1; |
| 125 | } |
| 126 | } |
| 127 | d += 2; |
| 128 | } |
| 129 | pos += 16 * 80; |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | let mut pairs: Vec<Vec<i16>> = pcm |
| 136 | .into_iter() |
| 137 | .take(ach) |
| 138 | .map(|b| b[..size].as_chunks::<2>().0.iter().map(|c| i16::from_le_bytes(*c)).collect()) |
| 139 | .collect(); |
| 140 | if sys.height == 720 { |
| 141 | let clear: &[usize] = if frame[1] & 0x0c != 0 { &[2, 3] } else { &[0, 1] }; |
| 142 | for &c in clear { |
| 143 | if let Some(p) = pairs.get_mut(c) { |
| 144 | p.clear(); |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | Some(DvAudio { sample_rate: sr, samples: size / 4, nonlinear_12bit: quant == 1, pairs }) |
| 149 | } |