vqa 0.5.1

Parser and decoder for Westwood Studios' VQA (Vector Quantized Animation) full-motion-video format
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
//! Low-level nom parsers for the individual chunks of a VQA file.
//!
//! Every parser here consumes one chunk - tag, big-endian size, payload, and
//! the pad byte that follows an odd-sized payload - and borrows the payload
//! from the input (zero-copy). For whole-file access prefer [`crate::VQA`],
//! which composes these parsers and handles chunk types this module has no
//! dedicated parser for.

use std::convert::TryInto;

use bitflags::bitflags;
use nom::{
    IResult, Parser,
    branch::alt,
    bytes::complete::{tag, take},
    combinator::{cond, map, map_opt, opt, value, verify},
    multi::count,
    number::complete::{be_u32, le_u16, le_u32, u8},
};

/// Take `size` bytes of chunk payload, also consuming the pad byte that
/// follows an odd-sized chunk - chunks always start at even offsets. The pad
/// byte may be absent when the chunk ends the input.
fn chunk_data<'a>(size: u32) -> impl FnMut(&'a [u8]) -> IResult<&'a [u8], &'a [u8]> {
    move |input| {
        let (input, data) = take(size).parse(input)?;
        let (input, _) = cond(size % 2 == 1, opt(take(1usize))).parse(input)?;

        Ok((input, data))
    }
}

/// The prelude shared by every chunk parser: the chunk's 4-character ID, its
/// big-endian size, and the (padded) payload.
fn plain_chunk<'a>(id: &'static str) -> impl FnMut(&'a [u8]) -> IResult<&'a [u8], (u32, &'a [u8])> {
    move |input| {
        let (input, _) = tag(id).parse(input)?;
        let (input, size) = be_u32(input)?;
        let (input, data) = chunk_data(size)(input)?;

        Ok((input, (size, data)))
    }
}

/// `(compressed, size, data)` of a chunk parsed by [`compressible_chunk`]
type CompressiblePayload<'a> = (bool, u32, &'a [u8]);

/// A chunk whose last ID character selects LCW compression: `Z` means the
/// payload is compressed, `0` that it is raw.
fn compressible_chunk<'a>(
    prefix: &'static str,
) -> impl FnMut(&'a [u8]) -> IResult<&'a [u8], CompressiblePayload<'a>> {
    move |input| {
        let (input, _) = tag(prefix).parse(input)?;
        let (input, compressed) =
            alt((value(true, tag("Z")), value(false, tag("0")))).parse(input)?;
        let (input, size) = be_u32(input)?;
        let (input, data) = chunk_data(size)(input)?;

        Ok((input, (compressed, size, data)))
    }
}

/// Any chunk, without knowing its type: the generic escape hatch for walking
/// over chunks this crate has no dedicated parser for (LINF, CINF, PINF, ...).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RawChunk<'a> {
    /// The chunk's 4-character ID, e.g. `b"SND2"`
    pub id: [u8; 4],
    /// Payload size in bytes.
    pub size: u32,
    /// The raw payload, borrowed from the input.
    pub data: &'a [u8],
}

/// Parse any single chunk. IDs are validated to be uppercase ASCII letters
/// and digits, so a desynced or corrupt stream fails instead of producing
/// nonsense chunks.
pub fn raw_chunk(input: &[u8]) -> IResult<&[u8], RawChunk<'_>> {
    let (input, id) = verify(take(4usize), |id: &[u8]| {
        id.iter()
            .all(|b| b.is_ascii_uppercase() || b.is_ascii_digit())
    })
    .parse(input)?;
    let (input, size) = be_u32(input)?;
    let (input, data) = chunk_data(size)(input)?;

    Ok((
        input,
        RawChunk {
            id: id.try_into().expect("take(4) yields 4 bytes"),
            size,
            data,
        },
    ))
}

/// The outer `FORM` container that wraps the whole file (an EA IFF 85 form).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FormChunk {
    /// Size of the form's contents in bytes. In v2/v3 movies this is the
    /// file size minus the 8-byte chunk header.
    pub size: u32,
}

/// Parse the `FORM` chunk that opens every VQA file. Consumes only the tag
/// and size; the form's contents (the `WVQA` signature followed by all other
/// chunks) stay in the input.
pub fn form_chunk(input: &[u8]) -> IResult<&[u8], FormChunk> {
    let (input, _) = tag("FORM").parse(input)?;
    let (input, size) = be_u32(input)?;

    Ok((input, FormChunk { size }))
}

/// The format version stored in the header.
///
/// The version alone does not imply the pixel format: most HiColor movies
/// are v3, but some v2 movies (Dune 2000) are HiColor too - check
/// [`VQAHeader::is_hicolor`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum VQAVersion {
    /// Version 1, used only in The Legend of Kyrandia III.
    One,
    /// Version 2, used in C&C, Red Alert, Lands of Lore II, and Dune 2000.
    Two,
    /// Version 3, used in the HiColor-era games (Tiberian Sun, Lands of
    /// Lore III, Blade Runner, Nox).
    Three,
}

/// Parse the header's little-endian version field; values other than 1-3
/// fail.
pub fn vqa_version(input: &[u8]) -> IResult<&[u8], VQAVersion> {
    map_opt(le_u16, |n| match n {
        1 => Some(VQAVersion::One),
        2 => Some(VQAVersion::Two),
        3 => Some(VQAVersion::Three),
        _ => None,
    })
    .parse(input)
}

bitflags! {
    /// Flag bits from the header's `flags` field.
    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    pub struct VQAFlags: u16 {
        /// The movie carries a soundtrack.
        const HAS_SOUND = 0b00000001;
    }
}

/// The fixed 42-byte `VQHD` header describing the whole movie.
///
/// v1 movies leave several sound fields zeroed; the [`sample_rate`],
/// [`num_channels`], and [`bit_depth`] helpers apply the documented
/// fallbacks, so prefer them over reading `freq`, `channels`, and `bits`
/// directly.
///
/// [`sample_rate`]: VQAHeader::sample_rate
/// [`num_channels`]: VQAHeader::num_channels
/// [`bit_depth`]: VQAHeader::bit_depth
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct VQAHeader {
    /// VQA version number
    pub version: VQAVersion,
    /// VQA flags
    pub flags: VQAFlags,
    /// Number of frames
    pub num_frames: u16,
    /// Movie width (pixels)
    pub width: u16,
    /// Movie height (pixels)
    pub height: u16,
    /// Width of each image block (pixels)
    pub block_width: u8,
    /// Height of each image block (pixels)
    pub block_height: u8,
    /// Frame rate of the VQA
    pub frame_rate: u8,
    /// How many images use the same lookup table
    pub cbparts: u8,
    /// Max number of colors used in VQA
    pub colors: u16,
    /// Max number of image blocks
    pub maxblocks: u16,
    /// Always 0?
    pub unk1: u32,
    /// Some kind of size?
    pub unk2: u16,
    /// Sound sampling frequency
    pub freq: u16,
    /// Number of sound channels
    pub channels: u8,
    /// Sound resolution
    pub bits: u8,
    /// Always 0?
    pub unk3: u32,
    /// 0 in old VQAs, 4 in HiColor VQAs?
    pub unk4: u16,
    /// 0 in old VQAs, CBFZ size in HiColor
    pub max_cbfz_size: u32,
    /// Always 0?
    pub unk5: u32,
}

impl VQAHeader {
    /// The sound sampling rate in Hz, applying the documented v1 fallback
    /// (a stored 0 means 22050 Hz).
    pub fn sample_rate(&self) -> u32 {
        match self.freq {
            0 => 22050,
            freq => u32::from(freq),
        }
    }

    /// The number of sound channels (a stored 0 means mono).
    pub fn num_channels(&self) -> u8 {
        match self.channels {
            0 => 1,
            channels => channels,
        }
    }

    /// The sound resolution in bits (a stored 0 means 8-bit).
    pub fn bit_depth(&self) -> u8 {
        match self.bits {
            0 => 8,
            bits => bits,
        }
    }

    /// Whether the movie carries a soundtrack.
    pub fn has_sound(&self) -> bool {
        self.flags.contains(VQAFlags::HAS_SOUND)
    }

    /// Whether the movie is HiColor (15-bit pixels) rather than 8-bit
    /// palettized. HiColor movies store 0 in the `colors` field.
    pub fn is_hicolor(&self) -> bool {
        self.colors == 0
    }
}

/// Parse the `VQHD` header chunk: the tag, the fixed 42-byte size, and every
/// header field.
pub fn vqa_header(input: &[u8]) -> IResult<&[u8], VQAHeader> {
    let (input, _) = tag("VQHD").parse(input)?;
    let (input, _) = verify(be_u32, |&size| size == 42).parse(input)?;
    let (input, version) = vqa_version(input)?;
    let (input, flags) = le_u16(input)?;
    let (input, num_frames) = le_u16(input)?;
    let (input, width) = le_u16(input)?;
    let (input, height) = le_u16(input)?;
    let (input, block_width) = u8(input)?;
    let (input, block_height) = u8(input)?;
    let (input, frame_rate) = u8(input)?;
    let (input, cbparts) = u8(input)?;
    let (input, colors) = le_u16(input)?;
    let (input, maxblocks) = le_u16(input)?;
    let (input, unk1) = le_u32(input)?;
    let (input, unk2) = le_u16(input)?;
    let (input, freq) = le_u16(input)?;
    let (input, channels) = u8(input)?;
    let (input, bits) = u8(input)?;
    let (input, unk3) = le_u32(input)?;
    let (input, unk4) = le_u16(input)?;
    let (input, max_cbfz_size) = le_u32(input)?;
    let (input, unk5) = le_u32(input)?;

    Ok((
        input,
        VQAHeader {
            version,
            flags: VQAFlags::from_bits_truncate(flags),
            num_frames,
            width,
            height,
            block_width,
            block_height,
            frame_rate,
            cbparts,
            colors,
            maxblocks,
            unk1,
            unk2,
            freq,
            channels,
            bits,
            unk3,
            unk4,
            max_cbfz_size,
            unk5,
        },
    ))
}

/// Position of one frame's data, decoded from a FINF entry.
///
/// Stored FINF values are in 16-bit words with bit 30 flagging a new palette;
/// `offset` is the decoded absolute byte position of the frame's data
/// (its SND? chunk when the movie has sound, its VQFR chunk otherwise).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FrameInfo {
    /// Absolute byte offset of the frame's data from the start of the file.
    pub offset: u32,
    /// Whether the frame carries a new palette (stored bit 30).
    pub has_palette: bool,
}

const FINF_PALETTE_FLAG: u32 = 0x4000_0000;

/// Parse one raw FINF entry, applying the transforms described on
/// [`FrameInfo`].
pub fn frame_info(input: &[u8]) -> IResult<&[u8], FrameInfo> {
    map(le_u32, |raw| FrameInfo {
        offset: (raw & (FINF_PALETTE_FLAG - 1)) * 2,
        has_palette: raw & FINF_PALETTE_FLAG != 0,
    })
    .parse(input)
}

/// The frame index, sitting between the header and the first frame's data:
/// the position of every frame in the file, in frame order.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FINFChunk {
    /// Payload size in bytes (4 bytes per frame).
    pub size: u32,
    /// One decoded entry per frame.
    pub frames: Vec<FrameInfo>,
}

/// Parse the `FINF` chunk into decoded per-frame positions.
pub fn finf_chunk(input: &[u8]) -> IResult<&[u8], FINFChunk> {
    let (input, _) = tag("FINF").parse(input)?;
    let (input, size) = be_u32(input)?;
    let (input, frames) = count(frame_info, size as usize / 4).parse(input)?;

    Ok((input, FINFChunk { size, frames }))
}

/// A sound chunk holding IMA ADPCM compressed 16-bit samples; decode with
/// [`crate::audio::decompress`]. Stereo data is laid out per VQA version:
/// v3 stores the left channel in the first half of the chunk and the right
/// in the second, v1/v2 alternate bytes between the channels.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SND2Chunk<'a> {
    /// Payload size in bytes.
    pub size: u32,
    /// The compressed sample data, borrowed from the input.
    pub data: &'a [u8],
}

/// Parse an `SND2` (IMA ADPCM audio) chunk.
pub fn snd2_chunk(input: &[u8]) -> IResult<&[u8], SND2Chunk<'_>> {
    let (input, (size, data)) = plain_chunk("SND2")(input)?;

    Ok((input, SND2Chunk { size, data }))
}

/// One frame's video data; `data` holds the nested sub-chunks (CBF?, CBP?,
/// CPL?, VPT?, VPTR/VPRZ).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct VQFRChunk<'a> {
    /// Payload size in bytes.
    pub size: u32,
    /// The nested sub-chunks, unparsed, borrowed from the input.
    pub data: &'a [u8],
}

/// Parse a `VQFR` (video frame) chunk; its sub-chunks stay unparsed in
/// `data`.
pub fn vqfr_chunk(input: &[u8]) -> IResult<&[u8], VQFRChunk<'_>> {
    let (input, (size, data)) = plain_chunk("VQFR")(input)?;

    Ok((input, VQFRChunk { size, data }))
}

/// A full codebook (block lookup table), replacing the current one. In
/// HiColor movies a compressed payload starting with a NUL byte uses the
/// relative LCW variant ([`crate::lcw::decompress`] handles both).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CBFChunk<'a> {
    /// Payload size in bytes.
    pub size: u32,
    /// Whether `data` is LCW-compressed (a `CBFZ` chunk).
    pub compressed: bool,
    /// The codebook data, borrowed from the input.
    pub data: &'a [u8],
}

/// Parse a `CBF0`/`CBFZ` (full codebook) chunk.
pub fn cbf_chunk(input: &[u8]) -> IResult<&[u8], CBFChunk<'_>> {
    let (input, (compressed, size, data)) = compressible_chunk("CBF")(input)?;

    Ok((
        input,
        CBFChunk {
            size,
            compressed,
            data,
        },
    ))
}

/// One part of the next codebook. `cbparts` (from the header) parts are
/// concatenated in frame order to form the new codebook; compressed parts
/// are decompressed only after concatenation.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CBPChunk<'a> {
    /// Payload size in bytes.
    pub size: u32,
    /// Whether the *concatenated* parts are LCW-compressed (`CBPZ` chunks).
    pub compressed: bool,
    /// This part's bytes, borrowed from the input.
    pub data: &'a [u8],
}

/// Parse a `CBP0`/`CBPZ` (codebook part) chunk.
pub fn cbp_chunk(input: &[u8]) -> IResult<&[u8], CBPChunk<'_>> {
    let (input, (compressed, size, data)) = compressible_chunk("CBP")(input)?;

    Ok((
        input,
        CBPChunk {
            size,
            compressed,
            data,
        },
    ))
}

/// A color palette: consecutive R, G, B bytes per color. Only the low 6 bits
/// of each value are significant (VGA hardware).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CPLChunk<'a> {
    /// Payload size in bytes.
    pub size: u32,
    /// Whether `data` is LCW-compressed (a `CPLZ` chunk).
    pub compressed: bool,
    /// The palette data, borrowed from the input.
    pub data: &'a [u8],
}

/// Parse a `CPL0`/`CPLZ` (palette) chunk.
pub fn cpl_chunk(input: &[u8]) -> IResult<&[u8], CPLChunk<'_>> {
    let (input, (compressed, size, data)) = compressible_chunk("CPL")(input)?;

    Ok((
        input,
        CPLChunk {
            size,
            compressed,
            data,
        },
    ))
}

/// A vector pointer table for one 8-bit frame: per-block indexes into the
/// codebook (layout differs between v1 and v2, see `doc/vqa.txt`).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct VPTChunk<'a> {
    /// Payload size in bytes.
    pub size: u32,
    /// Whether `data` is LCW-compressed (a `VPTZ` chunk).
    pub compressed: bool,
    /// The pointer table, borrowed from the input.
    pub data: &'a [u8],
}

/// Parse a `VPT0`/`VPTZ` (8-bit vector pointer table) chunk.
pub fn vpt_chunk(input: &[u8]) -> IResult<&[u8], VPTChunk<'_>> {
    let (input, (compressed, size, data)) = compressible_chunk("VPT")(input)?;

    Ok((
        input,
        VPTChunk {
            size,
            compressed,
            data,
        },
    ))
}

/// A HiColor vector pointer stream (`VPTR` raw, `VPRZ` LCW-compressed): a
/// differential command stream updating the previous frame's blocks.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct VPTRChunk<'a> {
    /// Payload size in bytes.
    pub size: u32,
    /// Whether `data` is LCW-compressed (a `VPRZ` chunk).
    pub compressed: bool,
    /// The command stream, borrowed from the input.
    pub data: &'a [u8],
}

/// Parse a `VPTR`/`VPRZ` (HiColor vector pointer stream) chunk.
pub fn vptr_chunk(input: &[u8]) -> IResult<&[u8], VPTRChunk<'_>> {
    let (input, compressed) =
        alt((value(false, tag("VPTR")), value(true, tag("VPRZ")))).parse(input)?;
    let (input, size) = be_u32(input)?;
    let (input, data) = chunk_data(size)(input)?;

    Ok((
        input,
        VPTRChunk {
            size,
            compressed,
            data,
        },
    ))
}

/// A HiColor full-codebook refresh between frames; `data` holds the nested
/// sub-chunks (in practice a single CBFZ).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct VQFLChunk<'a> {
    /// Payload size in bytes.
    pub size: u32,
    /// The nested sub-chunks, unparsed, borrowed from the input.
    pub data: &'a [u8],
}

/// Parse a `VQFL` (HiColor codebook refresh) chunk.
pub fn vqfl_chunk(input: &[u8]) -> IResult<&[u8], VQFLChunk<'_>> {
    let (input, (size, data)) = plain_chunk("VQFL")(input)?;

    Ok((input, VQFLChunk { size, data }))
}

/// An IMA ADPCM seek-state chunk accompanying SND2 data (decoder state for
/// jumping into the stream); not needed for sequential playback.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SN2JChunk<'a> {
    /// Payload size in bytes.
    pub size: u32,
    /// The seek-state data, borrowed from the input.
    pub data: &'a [u8],
}

/// Parse an `SN2J` (ADPCM seek state) chunk.
pub fn sn2j_chunk(input: &[u8]) -> IResult<&[u8], SN2JChunk<'_>> {
    let (input, (size, data)) = plain_chunk("SN2J")(input)?;

    Ok((input, SN2JChunk { size, data }))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn successfully_parses_form_chunk() {
        let input = b"FORM\x00\x00\x00\x10trailing";
        let res = form_chunk(input);

        assert!(matches!(res, Ok((b"trailing", FormChunk { size: 16 }))));
    }

    #[test]
    fn successfully_parses_vqa_version() {
        assert!(matches!(
            vqa_version(b"\x01\x00trailing"),
            Ok((b"trailing", VQAVersion::One))
        ));

        assert!(matches!(
            vqa_version(b"\x02\x00trailing"),
            Ok((b"trailing", VQAVersion::Two))
        ));

        assert!(matches!(
            vqa_version(b"\x03\x00trailing"),
            Ok((b"trailing", VQAVersion::Three))
        ));

        assert!(vqa_version(b"\x04\x00trailing").is_err());
    }

    #[test]
    fn snd2_chunk_leaves_even_sized_payload_unpadded() {
        let input = b"SND2\x00\x00\x00\x04abcdnext";
        let (rest, chunk) = snd2_chunk(input).unwrap();

        assert_eq!(chunk.size, 4);
        assert_eq!(chunk.data, b"abcd");
        assert_eq!(rest, b"next");
    }

    #[test]
    fn snd2_chunk_consumes_pad_byte_after_odd_sized_payload() {
        let input = b"SND2\x00\x00\x00\x03abc\x00next";
        let (rest, chunk) = snd2_chunk(input).unwrap();

        assert_eq!(chunk.size, 3);
        assert_eq!(chunk.data, b"abc");
        assert_eq!(rest, b"next");
    }

    #[test]
    fn odd_sized_chunk_at_end_of_input_needs_no_pad_byte() {
        let input = b"SND2\x00\x00\x00\x03abc";
        let (rest, chunk) = snd2_chunk(input).unwrap();

        assert_eq!(chunk.data, b"abc");
        assert!(rest.is_empty());
    }

    #[test]
    fn cbf_chunk_parses_both_variants_and_pads() {
        let (rest, chunk) = cbf_chunk(b"CBFZ\x00\x00\x00\x03abc\x00next").unwrap();
        assert!(chunk.compressed);
        assert_eq!(chunk.data, b"abc");
        assert_eq!(rest, b"next");

        let (rest, chunk) = cbf_chunk(b"CBF0\x00\x00\x00\x04abcdnext").unwrap();
        assert!(!chunk.compressed);
        assert_eq!(chunk.data, b"abcd");
        assert_eq!(rest, b"next");
    }

    #[test]
    fn vqfr_chunk_consumes_pad_byte_after_odd_sized_payload() {
        let (rest, chunk) = vqfr_chunk(b"VQFR\x00\x00\x00\x05abcde\x00next").unwrap();
        assert_eq!(chunk.data, b"abcde");
        assert_eq!(rest, b"next");
    }

    #[test]
    fn raw_chunk_parses_any_id_and_pads() {
        let (rest, chunk) = raw_chunk(b"LINF\x00\x00\x00\x03abc\x00next").unwrap();
        assert_eq!(&chunk.id, b"LINF");
        assert_eq!(chunk.data, b"abc");
        assert_eq!(rest, b"next");
    }

    #[test]
    fn raw_chunk_rejects_non_chunk_ids() {
        assert!(raw_chunk(b"lin \x00\x00\x00\x00").is_err());
        assert!(raw_chunk(b"\x00\x01\x02\x03\x00\x00\x00\x00").is_err());
    }

    #[test]
    fn cbp_cpl_vpt_chunks_parse_both_variants() {
        let (_, chunk) = cbp_chunk(b"CBPZ\x00\x00\x00\x02ab").unwrap();
        assert!(chunk.compressed);
        assert_eq!(chunk.data, b"ab");

        let (_, chunk) = cpl_chunk(b"CPL0\x00\x00\x00\x03rgb\x00").unwrap();
        assert!(!chunk.compressed);
        assert_eq!(chunk.data, b"rgb");

        let (_, chunk) = vpt_chunk(b"VPTZ\x00\x00\x00\x02ab").unwrap();
        assert!(chunk.compressed);
        assert_eq!(chunk.data, b"ab");
    }

    #[test]
    fn vptr_chunk_parses_raw_and_compressed_ids() {
        let (_, chunk) = vptr_chunk(b"VPTR\x00\x00\x00\x02ab").unwrap();
        assert!(!chunk.compressed);
        assert_eq!(chunk.data, b"ab");

        let (_, chunk) = vptr_chunk(b"VPRZ\x00\x00\x00\x02ab").unwrap();
        assert!(chunk.compressed);

        // VPT0/VPTZ are a different chunk type
        assert!(vptr_chunk(b"VPTZ\x00\x00\x00\x02ab").is_err());
        assert!(vpt_chunk(b"VPTR\x00\x00\x00\x02ab").is_err());
    }

    #[test]
    fn vqfl_and_sn2j_chunks_parse() {
        let (_, chunk) = vqfl_chunk(b"VQFL\x00\x00\x00\x04abcd").unwrap();
        assert_eq!(chunk.data, b"abcd");

        let (_, chunk) = sn2j_chunk(b"SN2J\x00\x00\x00\x04abcd").unwrap();
        assert_eq!(chunk.data, b"abcd");
    }

    #[test]
    fn finf_chunk_decodes_frame_positions() {
        let mut input = b"FINF\x00\x00\x00\x08".to_vec();
        input.extend(100u32.to_le_bytes());
        input.extend((0x4000_0000u32 | 150).to_le_bytes());

        let (_, chunk) = finf_chunk(&input).unwrap();
        assert_eq!(
            chunk.frames,
            vec![
                FrameInfo {
                    offset: 200,
                    has_palette: false
                },
                FrameInfo {
                    offset: 300,
                    has_palette: true
                },
            ]
        );
    }
}