h4m 0.3.0

A dependency-free, pure Rust HVQM4 1.3/1.5 (H4M) video decoder
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
use crate::{
    error::{be16, be32, Result},
    BlockState, DecoderBuffers, Error, Frame, FrameType, Limits, Version, VideoDecoder, VideoInfo,
};
#[cfg(feature = "std")]
use std::io::Read;

/// Parsed 68-byte H4M file header.
#[derive(Debug, Clone, Copy)]
pub struct Header {
    /// Video dimensions, sampling, and codec version.
    pub video: VideoInfo,
    /// Declared bytes following the file header.
    pub body_size: u32,
    /// Number of independently framed container blocks (GOPs).
    pub blocks: u32,
    /// Total number of video frames.
    pub video_frames: u32,
    /// Total number of audio packets, skipped by this decoder.
    pub audio_frames: u32,
    /// Frame duration in microseconds.
    pub microseconds_per_frame: u32,
    /// Declared maximum compressed frame size.
    pub max_frame_size: u32,
    /// Audio channel count.
    pub audio_channels: u8,
    /// Audio sample rate, if present.
    pub audio_sample_rate: u32,
}

impl Header {
    /// Parse a file header without allocating frame buffers.
    pub fn parse(data: &[u8]) -> Result<Self> {
        if data.len() < 68 {
            return Err(Error::Truncated);
        }
        let version = match &data[..16] {
            b"HVQM4 1.3\0\0\0\0\0\0\0" => Version::V13,
            b"HVQM4 1.5\0\0\0\0\0\0\0" => Version::V15,
            _ => return Err(Error::Invalid("file signature")),
        };
        if be32(data, 16)? != 68 {
            return Err(Error::Invalid("header size"));
        }
        if be32(data, 44)? != 0 || data[59] != 0 {
            return Err(Error::Invalid("reserved header fields"));
        }
        if !matches!(data[58], 0 | 0x12) {
            return Err(Error::Invalid("video mode"));
        }
        let result = Self {
            video: VideoInfo::new(
                version,
                be16(data, 52)?,
                be16(data, 54)?,
                (data[56], data[57]).try_into()?,
            )?,
            body_size: be32(data, 20)?,
            blocks: be32(data, 24)?,
            video_frames: be32(data, 28)?,
            audio_frames: be32(data, 32)?,
            microseconds_per_frame: be32(data, 36)?,
            max_frame_size: be32(data, 40)?,
            audio_channels: data[60],
            audio_sample_rate: be32(data, 64)?,
        };
        if result.blocks == 0 {
            return Err(Error::Invalid("zero container blocks"));
        }
        Ok(result)
    }
}

/// Decode a complete in-memory H4M file without an I/O or allocation dependency.
///
/// Input packets are borrowed directly. Frame buffers and block workspace are
/// supplied to [`Self::with_buffers`]; no allocation occurs while decoding.
pub struct SliceDecoder<'input, F, B> {
    inner: ContainerDecoder<SliceSource<'input>, F, B>,
}
impl<'input, F: AsRef<[u8]> + AsMut<[u8]>, B: AsMut<[BlockState]>> SliceDecoder<'input, F, B> {
    /// Read the header and initialize caller-owned decoder buffers.
    pub fn with_buffers(
        input: &'input [u8],
        buffers: DecoderBuffers<F, B>,
        limits: Limits,
    ) -> Result<Self> {
        let header = Header::parse(input)?;
        validate_packet_limit(header, limits)?;
        let video = VideoDecoder::with_buffers(header.video, buffers, limits)?;
        Ok(Self {
            inner: ContainerDecoder::new(
                header,
                SliceSource {
                    remaining: &input[68..],
                },
                video,
                limits,
            )?,
        })
    }
    /// Parsed file metadata.
    pub fn header(&self) -> &Header {
        &self.inner.demux.header
    }
    /// Decode the next frame in decoding order, or `None` after the last block.
    /// An error permanently invalidates this instance.
    pub fn next_frame(&mut self) -> Result<Option<Frame<'_>>> {
        self.inner.next_frame()
    }
    /// Recover the unconsumed input, including any trailing data, and storage.
    pub fn into_inner(self) -> (&'input [u8], DecoderBuffers<F, B>) {
        (
            self.inner.demux.source.remaining,
            self.inner.video.into_buffers(),
        )
    }
}

/// Streaming H4M decoder with reusable, allocated buffers.
///
/// Available with `std`. Wrap files in `BufReader` for efficient small reads.
/// Construction allocates the packet, frame, and descriptor buffers once;
/// decoding does not grow them. Use [`SliceDecoder`] for caller-owned buffers
/// and in-memory input without `std` or `alloc`.
#[cfg(feature = "std")]
pub struct Decoder<R> {
    inner: ContainerDecoder<ReaderSource<R>, alloc::vec::Vec<u8>, alloc::vec::Vec<BlockState>>,
}
#[cfg(feature = "std")]
impl<R: Read> Decoder<R> {
    /// Initialize a streaming decoder with default resource limits.
    pub fn new(reader: R) -> Result<Self> {
        Self::with_limits(reader, Limits::default())
    }
    /// Read the header and allocate storage after validating resource limits.
    pub fn with_limits(mut reader: R, limits: Limits) -> Result<Self> {
        let mut bytes = [0; 68];
        reader.read_exact(&mut bytes)?;
        let header = Header::parse(&bytes)?;
        validate_packet_limit(header, limits)?;
        let video = VideoDecoder::with_limits(header.video, limits)?;
        let source = ReaderSource {
            reader,
            scratch: alloc::vec![0; (header.max_frame_size as usize).max(20)],
        };
        Ok(Self {
            inner: ContainerDecoder::new(header, source, video, limits)?,
        })
    }
    /// Parsed file metadata.
    pub fn header(&self) -> &Header {
        &self.inner.demux.header
    }
    /// Recover the reader, leaving trailing container data unread.
    pub fn into_inner(self) -> R {
        self.inner.demux.source.reader
    }
    /// Decode the next frame, or `None` after all declared blocks.
    /// After an error, subsequent calls return [`Error::Failed`].
    pub fn next_frame(&mut self) -> Result<Option<Frame<'_>>> {
        self.inner.next_frame()
    }
}

#[derive(Clone, Copy)]
enum DecodeState {
    Ready,
    Finished,
    Failed,
}
struct ContainerDecoder<S, F, B> {
    demux: Demuxer<S>,
    video: VideoDecoder<F, B>,
    state: DecodeState,
}
impl<S: Source, F: AsRef<[u8]> + AsMut<[u8]>, B: AsMut<[BlockState]>> ContainerDecoder<S, F, B> {
    fn new(header: Header, source: S, video: VideoDecoder<F, B>, limits: Limits) -> Result<Self> {
        validate_packet_limit(header, limits)?;
        Ok(Self {
            demux: Demuxer {
                source,
                header,
                block: None,
                blocks_read: 0,
                read: PacketCounts::default(),
            },
            video,
            state: DecodeState::Ready,
        })
    }
    fn next_frame(&mut self) -> Result<Option<Frame<'_>>> {
        match self.state {
            DecodeState::Failed => return Err(Error::Failed),
            DecodeState::Finished => return Ok(None),
            DecodeState::Ready => {}
        }
        self.state = DecodeState::Failed;
        match self.demux.next_video()? {
            None => {
                self.state = DecodeState::Finished;
                Ok(None)
            }
            Some(packet) => {
                let frame = self
                    .video
                    .decode(packet.kind, packet.display_index, packet.data)?;
                self.state = DecodeState::Ready;
                Ok(Some(frame))
            }
        }
    }
}
fn validate_packet_limit(header: Header, limits: Limits) -> Result<()> {
    if header.max_frame_size as usize > limits.max_frame_bytes {
        return Err(Error::Limit("compressed frame size"));
    }
    Ok(())
}

/// The demuxer asks for short-lived input slices; slice input borrows directly,
/// while the std adapter fills its one preallocated scratch buffer.
trait Source {
    fn read_bytes(&mut self, size: usize) -> Result<&[u8]>;
    fn skip(&mut self, size: usize) -> Result<()>;
}
struct SliceSource<'a> {
    remaining: &'a [u8],
}
impl Source for SliceSource<'_> {
    fn read_bytes(&mut self, size: usize) -> Result<&[u8]> {
        let (bytes, rest) = self
            .remaining
            .split_at_checked(size)
            .ok_or(Error::Truncated)?;
        self.remaining = rest;
        Ok(bytes)
    }
    fn skip(&mut self, size: usize) -> Result<()> {
        self.read_bytes(size).map(|_| ())
    }
}
#[cfg(feature = "std")]
struct ReaderSource<R> {
    reader: R,
    scratch: alloc::vec::Vec<u8>,
}
#[cfg(feature = "std")]
impl<R: Read> Source for ReaderSource<R> {
    fn read_bytes(&mut self, size: usize) -> Result<&[u8]> {
        let target = self
            .scratch
            .get_mut(..size)
            .ok_or(Error::Invalid("packet exceeds declared maximum"))?;
        self.reader.read_exact(target)?;
        Ok(target)
    }
    fn skip(&mut self, mut size: usize) -> Result<()> {
        while size != 0 {
            let count = size.min(self.scratch.len());
            self.reader.read_exact(&mut self.scratch[..count])?;
            size -= count;
        }
        Ok(())
    }
}

#[derive(Default)]
struct PacketCounts {
    video: u32,
    audio: u32,
}
impl PacketCounts {
    fn is_empty(&self) -> bool {
        self.video == 0 && self.audio == 0
    }
}
struct BlockProgress {
    remaining_bytes: u32,
    remaining: PacketCounts,
    video_total: u32,
    first_display_index: u32,
}
impl BlockProgress {
    fn parse(bytes: &[u8], first_display_index: u32) -> Result<Self> {
        if be32(bytes, 16)? != 0x01000000 {
            return Err(Error::Invalid("container block marker"));
        }
        let video = be32(bytes, 8)?;
        Ok(Self {
            remaining_bytes: be32(bytes, 4)?,
            remaining: PacketCounts {
                video,
                audio: be32(bytes, 12)?,
            },
            video_total: video,
            first_display_index,
        })
    }
    fn consume_packet(&mut self, size: u32) -> Result<()> {
        self.remaining_bytes = self
            .remaining_bytes
            .checked_sub(8)
            .ok_or(Error::Invalid("packet header exceeds block"))?;
        self.remaining_bytes = self
            .remaining_bytes
            .checked_sub(size)
            .ok_or(Error::Invalid("packet exceeds block"))?;
        Ok(())
    }
}
enum PacketKind {
    Audio,
    Video(FrameType),
}
struct PacketHeader {
    kind: PacketKind,
    size: u32,
}
impl PacketHeader {
    fn parse(bytes: &[u8]) -> Result<Self> {
        let kind = match be16(bytes, 0)? {
            0 => PacketKind::Audio,
            1 => PacketKind::Video(be16(bytes, 2)?.try_into()?),
            _ => return Err(Error::Invalid("unknown packet kind")),
        };
        Ok(Self {
            kind,
            size: be32(bytes, 4)?,
        })
    }
}
struct VideoPacket<'a> {
    kind: FrameType,
    display_index: u32,
    data: &'a [u8],
}
struct Demuxer<S> {
    source: S,
    header: Header,
    block: Option<BlockProgress>,
    blocks_read: u32,
    read: PacketCounts,
}
impl<S: Source> Demuxer<S> {
    fn next_video(&mut self) -> Result<Option<VideoPacket<'_>>> {
        loop {
            if self
                .block
                .as_ref()
                .is_none_or(|block| block.remaining.is_empty())
            {
                if self
                    .block
                    .as_ref()
                    .is_some_and(|block| block.remaining_bytes != 0)
                {
                    return Err(Error::Invalid("container block size mismatch"));
                }
                if self.blocks_read == self.header.blocks {
                    if self.read.video != self.header.video_frames
                        || self.read.audio != self.header.audio_frames
                    {
                        return Err(Error::Invalid("total frame count mismatch"));
                    }
                    return Ok(None);
                }
                let block = BlockProgress::parse(self.source.read_bytes(20)?, self.read.video)?;
                if block.remaining.video > self.header.video_frames - self.read.video
                    || block.remaining.audio > self.header.audio_frames - self.read.audio
                {
                    return Err(Error::Invalid("block frame count exceeds file total"));
                }
                self.block = Some(block);
                self.blocks_read += 1;
                continue;
            }
            let block = self
                .block
                .as_mut()
                .ok_or(Error::Invalid("missing container block"))?;
            if block.remaining_bytes < 8 {
                return Err(Error::Invalid("packet header exceeds block"));
            }
            let packet = PacketHeader::parse(self.source.read_bytes(8)?)?;
            block.consume_packet(packet.size)?;
            match packet.kind {
                PacketKind::Audio => {
                    block.remaining.audio = block
                        .remaining
                        .audio
                        .checked_sub(1)
                        .ok_or(Error::Invalid("too many audio packets"))?;
                    self.source.skip(packet.size as usize)?;
                    self.read.audio += 1;
                }
                PacketKind::Video(kind) => {
                    if block.remaining.video == 0 {
                        return Err(Error::Invalid("too many video packets"));
                    }
                    if packet.size > self.header.max_frame_size {
                        return Err(Error::Invalid("packet exceeds declared maximum"));
                    }
                    if block.remaining.video == block.video_total && kind != FrameType::I {
                        return Err(Error::Invalid("GOP must start with an I frame"));
                    }
                    let data = self.source.read_bytes(packet.size as usize)?;
                    let display = be32(data, 0)?;
                    if display >= block.video_total {
                        return Err(Error::Invalid("display index outside GOP"));
                    }
                    block.remaining.video -= 1;
                    self.read.video += 1;
                    return Ok(Some(VideoPacket {
                        kind,
                        display_index: block.first_display_index + display,
                        data: &data[4..],
                    }));
                }
            }
        }
    }
}