use std::fmt;
use crate::audio::{CodecState, decompress_into, westwood};
use crate::chunk::{Chunk, Chunks, is_chunk_id};
use crate::error::{Error, ErrorKind, Limit};
use crate::header::{FrameInfo, VQAHeader, VQAVersion};
use crate::video::{Frame, FrameDecoder, FrameRef};
const MAX_SOUNDTRACK_SAMPLES: usize = 1 << 26;
const VQHD_OFFSET: usize = 12;
const VQHD_START: &[u8; 8] = b"VQHD\0\0\0\x2a";
fn is_cut_off_form(start: &[u8]) -> bool {
let matches = |at: usize, want: &[u8]| {
let have = start.get(at..).unwrap_or_default();
want.iter().zip(have).all(|(w, h)| w == h)
};
!start.is_empty() && matches(0, b"FORM") && matches(8, b"WVQA")
}
#[derive(Clone, PartialEq, Eq)]
pub struct VQA<'a> {
pub form_size: u32,
pub header: VQAHeader,
pub frame_index: Option<Vec<FrameInfo>>,
body: &'a [u8],
body_offset: usize,
codebook_schedule: &'a [CindEntry],
}
impl fmt::Debug for VQA<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("VQA")
.field("form_size", &self.form_size)
.field("header", &self.header)
.field("frame_index", &self.frame_index)
.field("body_offset", &self.body_offset)
.field("body_len", &self.body.len())
.field(
"codebook_starts",
&self.codebook_starts().collect::<Vec<_>>(),
)
.finish()
}
}
impl<'a> VQA<'a> {
pub fn parse(buffer: &'a [u8]) -> Result<VQA<'a>, Error> {
let Some((form, rest)) = buffer.split_first_chunk::<12>() else {
return Err(Error::from(if is_cut_off_form(buffer) {
ErrorKind::Truncated
} else {
ErrorKind::NotVqa
}));
};
if &form[..4] != b"FORM" || &form[8..] != b"WVQA" {
return Err(ErrorKind::NotVqa.into());
}
let form_size = u32::from_be_bytes(form[4..8].try_into().expect("four bytes"));
let found = rest.first_chunk::<4>().copied();
let found = found.filter(|id| is_chunk_id(id));
let header_error = |kind| Error::at(kind, found, VQHD_OFFSET);
let Some((vqhd, rest)) = rest.split_first_chunk::<8>() else {
return Err(header_error(if VQHD_START.starts_with(rest) {
ErrorKind::Truncated
} else {
ErrorKind::InvalidHeader
}));
};
if vqhd != VQHD_START {
return Err(header_error(ErrorKind::InvalidHeader));
}
let (vqhd, body) = rest
.split_first_chunk::<42>()
.ok_or_else(|| header_error(ErrorKind::Truncated))?;
let header = VQAHeader::parse(vqhd).map_err(|e| header_error(e.kind()))?;
let body_offset = buffer.len() - body.len();
let mut frame_index = None;
let mut codebook_schedule = None;
let mut in_frames = false;
for chunk in Chunks::at(body, body_offset).map_while(Result::ok) {
match &chunk.id {
b"CINF" if codebook_schedule.is_none() => {
codebook_schedule = Some(cind_entries(&chunk));
}
b"FINF" if frame_index.is_none() => frame_index = Some(finf_entries(&chunk)),
id => in_frames |= is_frame_data(id),
}
if frame_index.is_some() && (codebook_schedule.is_some() || in_frames) {
break;
}
}
Ok(VQA {
form_size,
header,
frame_index,
body,
body_offset,
codebook_schedule: codebook_schedule.unwrap_or_default(),
})
}
pub fn chunks(&self) -> Chunks<'a> {
Chunks::at(self.body, self.body_offset)
}
pub fn frames(&self) -> Result<Frames<'a>, Error> {
Ok(Frames {
chunks: self.chunks(),
decoder: FrameDecoder::new(&self.header)
.map_err(|e| Error::at(e.kind(), Some(*b"VQHD"), VQHD_OFFSET))?,
codebook_schedule: if self.header.cbparts == 0 {
self.codebook_schedule
} else {
&[]
},
frame: 0,
done: false,
})
}
pub fn decode_audio(&self) -> Result<Vec<i16>, Error> {
self.decode_audio_up_to(MAX_SOUNDTRACK_SAMPLES)
}
fn decode_audio_up_to(&self, max_samples: usize) -> Result<Vec<i16>, Error> {
let mut chunks = self.audio_chunks();
let mut samples = Vec::new();
while let Some(result) = chunks.next_into(&mut samples) {
result?;
if samples.len() > max_samples {
return Err(ErrorKind::TooLarge(Limit::Soundtrack).into());
}
}
Ok(samples)
}
pub fn codebook_starts(&self) -> impl Iterator<Item = u16> + 'a {
self.codebook_schedule.iter().map(cind_start)
}
pub fn audio_chunks(&self) -> AudioChunks<'a> {
AudioChunks {
chunks: self.chunks(),
version: self.header.version,
stereo: self.header.num_channels() >= 2,
pcm16: self.header.bit_depth() == 16,
left: CodecState::new(),
right: CodecState::new(),
bytes: Vec::new(),
done: false,
}
}
}
#[derive(Clone)]
pub struct AudioChunks<'a> {
chunks: Chunks<'a>,
version: VQAVersion,
stereo: bool,
pcm16: bool,
left: CodecState,
right: CodecState,
bytes: Vec<u8>,
done: bool,
}
impl fmt::Debug for AudioChunks<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("AudioChunks")
.field("chunks", &self.chunks)
.field("version", &self.version)
.field("stereo", &self.stereo)
.field("pcm16", &self.pcm16)
.field("left", &self.left)
.field("right", &self.right)
.field("done", &self.done)
.finish_non_exhaustive()
}
}
impl AudioChunks<'_> {
pub fn next_into(&mut self, samples: &mut Vec<i16>) -> Option<Result<(), Error>> {
if self.done {
return None;
}
loop {
let chunk = match self.chunks.next()? {
Ok(chunk) => chunk,
Err(e) => {
self.done = true;
return Some(Err(e));
}
};
match &chunk.id {
b"SND2" => self.decode_snd2(chunk.data, samples),
b"SND1" => {
self.bytes.clear();
westwood::decompress_into(chunk.data, &mut self.bytes);
let mono = self.bytes.iter().map(|&b| widen(b));
if self.stereo {
samples.extend(mono.flat_map(|sample| [sample; 2]));
} else {
samples.extend(mono);
}
}
b"SND0" if self.pcm16 => samples.extend(
chunk
.data
.as_chunks::<2>()
.0
.iter()
.map(|&b| i16::from_le_bytes(b)),
),
b"SND0" => samples.extend(chunk.data.iter().map(|&b| widen(b))),
_ => continue,
}
return Some(Ok(()));
}
}
fn decode_snd2(&mut self, data: &[u8], samples: &mut Vec<i16>) {
let (left, right) = (&mut self.left, &mut self.right);
if !self.stereo {
decompress_into(left, data, samples);
} else if self.version == VQAVersion::Three {
let (l, r) = data.split_at(data.len() / 2);
decode_stereo(left, right, l.iter().zip(r), samples);
if let Some(&unpaired) = r.get(l.len()) {
right.decode_byte(unpaired);
}
} else {
let (pairs, rest) = data.as_chunks::<2>();
let pairs = pairs.iter().map(|[l, r]| (l, r));
decode_stereo(left, right, pairs, samples);
if let [unpaired] = rest {
left.decode_byte(*unpaired);
}
}
}
}
impl Iterator for AudioChunks<'_> {
type Item = Result<Vec<i16>, Error>;
fn next(&mut self) -> Option<Self::Item> {
let mut samples = Vec::new();
Some(self.next_into(&mut samples)?.map(|()| samples))
}
}
fn widen(sample: u8) -> i16 {
(i16::from(sample) - 128) << 8
}
fn decode_stereo<'a>(
left: &mut CodecState,
right: &mut CodecState,
pairs: impl ExactSizeIterator<Item = (&'a u8, &'a u8)>,
samples: &mut Vec<i16>,
) {
samples.reserve(pairs.len() * 4);
for (&l, &r) in pairs {
let [l0, l1] = left.decode_byte(l);
let [r0, r1] = right.decode_byte(r);
samples.extend_from_slice(&[l0, r0, l1, r1]);
}
}
#[derive(Clone)]
pub struct Frames<'a> {
chunks: Chunks<'a>,
decoder: FrameDecoder,
codebook_schedule: &'a [CindEntry],
frame: usize,
done: bool,
}
enum FrameData<'a> {
Container(Chunk<'a>),
Table(Chunk<'a>),
}
impl<'a> FrameData<'a> {
fn of(chunk: Chunk<'a>) -> Option<FrameData<'a>> {
match &chunk.id {
b"VQFR" | b"VQFK" => Some(FrameData::Container(chunk)),
b"VPT0" | b"VPTZ" | b"VPTK" | b"VPTD" => Some(FrameData::Table(chunk)),
_ => None,
}
}
}
impl<'a> Frames<'a> {
pub fn next_ref(&mut self) -> Option<Result<FrameRef<'_>, Error>> {
if self.done {
return None;
}
let frame = self.frame;
let data = match self.start_frame().map(|()| self.next_frame_data()) {
Ok(None) => return None,
Ok(Some(Ok(data))) => data,
Ok(Some(Err(e))) | Err(e) => {
self.done = true;
return Some(Err(e.in_frame(frame)));
}
};
self.frame += 1;
let result = match data {
FrameData::Container(chunk) => self
.decoder
.decode_chunks(chunk.sub_chunks())
.map_err(|e| e.or_in(&chunk)),
FrameData::Table(chunk) => match self.decoder.frame_chunk(&chunk) {
Ok(()) => self.decoder.end_frame(),
Err(kind) => Err(kind),
}
.map_err(|kind| Error::in_chunk(kind, &chunk)),
};
self.done = result.is_err();
Some(result.map_err(|e| e.in_frame(frame)))
}
fn next_frame_data(&mut self) -> Option<Result<FrameData<'a>, Error>> {
loop {
let chunk = match self.chunks.next()? {
Ok(chunk) => chunk,
Err(e) => return Some(Err(e)),
};
if let Some(data) = FrameData::of(chunk) {
return Some(Ok(data));
}
let applied = match &chunk.id {
b"VQFL" => self.decoder.apply_side_chunks(chunk.sub_chunks()),
b"CBF0" | b"CBFZ" | b"CBP0" | b"CBPZ" | b"CPL0" | b"CPLZ" => self
.decoder
.frame_chunk(&chunk)
.map_err(|kind| Error::in_chunk(kind, &chunk)),
_ => Ok(()),
};
if let Err(e) = applied {
return Some(Err(e));
}
}
}
fn start_frame(&mut self) -> Result<(), Error> {
let mut due = false;
while let Some((entry, rest)) = self.codebook_schedule.split_first() {
let start = usize::from(cind_start(entry));
if start > self.frame {
break;
}
self.codebook_schedule = rest;
due |= start == self.frame;
}
if due && self.frame_ahead() {
self.decoder.swap_in_codebook_parts()?;
}
Ok(())
}
fn frame_ahead(&self) -> bool {
let mut chunks = self.chunks.clone().map_while(Result::ok);
chunks.any(|chunk| FrameData::of(chunk).is_some())
}
}
fn is_frame_data(id: &[u8; 4]) -> bool {
matches!(
id,
b"VQFR"
| b"VQFK"
| b"VQFL"
| b"SND0"
| b"SND1"
| b"SND2"
| b"VPT0"
| b"VPTZ"
| b"VPTK"
| b"VPTD"
| b"CBF0"
| b"CBFZ"
| b"CBP0"
| b"CBPZ"
| b"CPL0"
| b"CPLZ"
)
}
fn finf_entries(finf: &Chunk<'_>) -> Vec<FrameInfo> {
let (entries, _) = finf.data.as_chunks::<4>();
entries
.iter()
.map(|&entry| FrameInfo::from_raw(u32::from_le_bytes(entry)))
.collect()
}
type CindEntry = [u8; 6];
fn cind_entries<'a>(cinf: &Chunk<'a>) -> &'a [CindEntry] {
let cind = cinf
.sub_chunks()
.map_while(Result::ok)
.find(|chunk| &chunk.id == b"CIND");
cind.map_or(&[], |chunk| chunk.data.as_chunks().0)
}
fn cind_start(entry: &CindEntry) -> u16 {
u16::from_le_bytes([entry[0], entry[1]])
}
impl Iterator for Frames<'_> {
type Item = Result<Frame, Error>;
fn next(&mut self) -> Option<Self::Item> {
Some(self.next_ref()?.map(|frame| frame.to_frame()))
}
fn nth(&mut self, n: usize) -> Option<Self::Item> {
for _ in 0..n {
if self.next_ref()?.is_err() {
return None;
}
}
self.next()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::audio::decompress;
fn movie(version: u16, channels: u8, sound: &[Vec<u8>]) -> Vec<u8> {
let mut header = Vec::new();
header.extend(version.to_le_bytes());
header.extend(1u16.to_le_bytes()); header.extend(0u16.to_le_bytes()); header.extend(8u16.to_le_bytes()); header.extend(4u16.to_le_bytes()); header.extend([4, 2, 15, 0]); header.extend(256u16.to_le_bytes()); header.extend(0u16.to_le_bytes()); header.extend([0; 6]); header.extend(22050u16.to_le_bytes());
header.extend([channels, 16]);
header.extend([0; 14]); assert_eq!(header.len(), 42);
let mut body = b"WVQA".to_vec();
for (id, data) in
std::iter::once((b"VQHD", &header)).chain(sound.iter().map(|d| (b"SND2", d)))
{
body.extend(id);
body.extend((data.len() as u32).to_be_bytes());
body.extend(data);
if data.len() % 2 == 1 {
body.push(0);
}
}
let mut file = b"FORM".to_vec();
file.extend((body.len() as u32).to_be_bytes());
file.extend(body);
file
}
fn split_decode_zip(version: u16, channels: u8, sound: &[Vec<u8>]) -> Vec<i16> {
let (mut left, mut right) = (CodecState::new(), CodecState::new());
let mut samples = Vec::new();
for data in sound {
if channels < 2 {
samples.extend(decompress(&mut left, data));
continue;
}
let (l, r): (Vec<u8>, Vec<u8>) = if version == 3 {
let (l, r) = data.split_at(data.len() / 2);
(l.to_vec(), r.to_vec())
} else {
(
data.iter().step_by(2).copied().collect(),
data.iter().skip(1).step_by(2).copied().collect(),
)
};
let l = decompress(&mut left, &l);
let r = decompress(&mut right, &r);
for (&l, &r) in l.iter().zip(&r) {
samples.extend([l, r]);
}
}
samples
}
#[test]
fn debug_shows_lengths_rather_than_the_movie() {
let file = movie(2, 1, &[vec![0x77; 1000]]);
let vqa = VQA::parse(&file).unwrap();
let debug = format!("{vqa:?}");
assert!(debug.contains("body_offset: 62, body_len: 1008"), "{debug}");
assert!(debug.len() < 1000, "{debug}");
let debug = format!("{:?}", vqa.audio_chunks());
let chunks = "chunks: Chunks { offset: 62, len: 1008, nested: false }";
assert!(debug.contains(chunks), "{debug}");
assert!(debug.len() < 1000, "{debug}");
}
#[test]
fn decode_audio_fails_past_its_sample_cap() {
let file = movie(2, 1, &[vec![0x77; 3], vec![0x77; 3], vec![0x77; 3]]);
let vqa = VQA::parse(&file).unwrap();
assert_eq!(vqa.decode_audio_up_to(18).map(|s| s.len()), Ok(18));
assert_eq!(
vqa.decode_audio_up_to(17),
Err(ErrorKind::TooLarge(Limit::Soundtrack).into())
);
}
#[test]
fn decode_audio_matches_per_channel_decoding_in_every_layout() {
let mut state = 0x9e37_79b9_7f4a_7c15u64;
let sound: Vec<Vec<u8>> = [7, 64, 1, 33, 0, 100, 5]
.iter()
.map(|&len| {
(0..len)
.map(|_| {
state ^= state << 13;
state ^= state >> 7;
state ^= state << 17;
state as u8
})
.collect()
})
.collect();
for version in [1, 2, 3] {
for channels in [1, 2] {
let file = movie(version, channels, &sound);
let vqa = VQA::parse(&file).unwrap();
assert_eq!(
vqa.decode_audio().unwrap(),
split_decode_zip(version, channels, &sound),
"version {version}, {channels} channel(s)"
);
}
}
}
}