h4m 0.4.0

Dependency-free pure rust HVQM4 video and IMA audio 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
424
425
426
427
428
429
#![doc = include_str!("../README.md")]
#![no_std]
#![forbid(unsafe_code)]

#[cfg(feature = "alloc")]
extern crate alloc;
#[cfg(any(feature = "std", test))]
extern crate std;

mod audio;
#[cfg(feature = "std")]
pub use audio::AudioDecoder;
pub use audio::{
    AudioBufferRequirements, AudioInfo, AudioLimits, AudioPacketDecoder, AudioPacketMode,
    SliceAudioDecoder,
};
mod container;
mod entropy;
mod error;
mod storage;
mod syntax;
mod video;

#[cfg(feature = "std")]
pub use container::VideoDecoder;
pub use container::{Header, SliceVideoDecoder};
pub use error::{BufferKind, Error};
pub use syntax::BlockState;
pub use video::{VideoBuffers, VideoPacketDecoder};

/// Video decoder backed by caller-owned mutable slices.
pub type BorrowedVideoPacketDecoder<'a> = VideoPacketDecoder<&'a mut [u8], &'a mut [BlockState]>;

/// Video decoder backed by vectors allocated once at construction.
#[cfg(feature = "alloc")]
pub type OwnedVideoPacketDecoder =
    VideoPacketDecoder<alloc::vec::Vec<u8>, alloc::vec::Vec<BlockState>>;

/// HVQM4 bitstream version (affects chroma motion interpolation).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Version {
    /// HVQM4 1.3.
    V13,
    /// HVQM4 1.5.
    V15,
}

/// Picture coding type.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u16)]
pub enum FrameType {
    /// Independently coded picture.
    I = 0x10,
    /// Picture predicted from the past reference or current destination.
    P = 0x20,
    /// Picture predicted from past and future references.
    B = 0x30,
}

impl TryFrom<u16> for FrameType {
    type Error = Error;

    fn try_from(value: u16) -> Result<Self, Error> {
        match value {
            0x10 => Ok(Self::I),
            0x20 => Ok(Self::P),
            0x30 => Ok(Self::B),
            _ => Err(Error::Invalid("unknown frame type")),
        }
    }
}

/// Allocation limits applied before allocating pixel or packet buffers.
#[derive(Debug, Clone, Copy)]
pub struct VideoLimits {
    /// Maximum luma pixels per frame. Default: 4096 × 4096.
    pub max_pixels: usize,
    /// Maximum compressed picture payload bytes, excluding the four-byte display
    /// index and eight-byte container packet header. Default: 64 MiB.
    /// Uses the same units for raw-packet and container decoders.
    pub max_frame_bytes: usize,
}

impl Default for VideoLimits {
    fn default() -> Self {
        Self {
            max_pixels: 4096 * 4096,
            max_frame_bytes: 64 * 1024 * 1024,
        }
    }
}

/// Supported chroma subsampling layouts.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ChromaSampling {
    /// Full-resolution chroma in both dimensions.
    Yuv444,
    /// Half-resolution chroma horizontally.
    Yuv422,
    /// Half-resolution chroma in both dimensions.
    Yuv420,
}

impl ChromaSampling {
    /// Number of luma samples per chroma sample horizontally.
    pub const fn horizontal_factor(self) -> u8 {
        match self {
            Self::Yuv444 => 1,
            Self::Yuv422 | Self::Yuv420 => 2,
        }
    }

    /// Number of luma samples per chroma sample vertically.
    pub const fn vertical_factor(self) -> u8 {
        match self {
            Self::Yuv444 | Self::Yuv422 => 1,
            Self::Yuv420 => 2,
        }
    }
}

impl TryFrom<(u8, u8)> for ChromaSampling {
    type Error = Error;

    fn try_from(factors: (u8, u8)) -> Result<Self, Error> {
        match factors {
            (1, 1) => Ok(Self::Yuv444),
            (2, 1) => Ok(Self::Yuv422),
            (2, 2) => Ok(Self::Yuv420),
            _ => Err(Error::Invalid(
                "unsupported chroma sampling (expected 4:4:4, 4:2:2 or 4:2:0)",
            )),
        }
    }
}

/// Required reusable storage for one video decoder.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct VideoBufferRequirements {
    /// Bytes in **each** of the three frame buffers, including all Y/U/V planes.
    pub frame_bytes: usize,
    /// Elements in the block-descriptor buffer (not bytes).
    pub block_states: usize,
}

/// Validated, immutable video layout and codec version.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct VideoInfo {
    version: Version,
    width: u16,
    height: u16,
    sampling: ChromaSampling,
}

impl VideoInfo {
    /// Validate dimensions and construct a video format without allocating.
    /// Width and height must be nonzero multiples of eight.
    pub fn new(
        version: Version,
        width: u16,
        height: u16,
        sampling: ChromaSampling,
    ) -> Result<Self, Error> {
        if width == 0 || height == 0 || !width.is_multiple_of(8) || !height.is_multiple_of(8) {
            return Err(Error::Invalid(
                "dimensions must be nonzero multiples of eight",
            ));
        }
        let pixels = usize::from(width)
            .checked_mul(usize::from(height))
            .ok_or(Error::Limit("pixel count"))?;
        if pixels > isize::MAX as usize / 9 {
            return Err(Error::Limit("pixel count"));
        }
        Ok(Self {
            version,
            width,
            height,
            sampling,
        })
    }

    /// Codec version.
    pub const fn version(self) -> Version {
        self.version
    }

    /// Width in luma samples.
    pub const fn width(self) -> u16 {
        self.width
    }

    /// Height in luma samples.
    pub const fn height(self) -> u16 {
        self.height
    }

    /// Chroma subsampling layout.
    pub const fn sampling(self) -> ChromaSampling {
        self.sampling
    }

    /// Storage sizes for caller-owned buffers. This does not allocate.
    pub fn buffer_requirements(self) -> VideoBufferRequirements {
        let width = usize::from(self.width);
        let height = usize::from(self.height);
        let chroma_width = width / usize::from(self.sampling.horizontal_factor());
        let chroma_height = height / usize::from(self.sampling.vertical_factor());
        VideoBufferRequirements {
            frame_bytes: width * height + 2 * chroma_width * chroma_height,
            block_states: (width / 4 + 2) * (height / 4 + 2)
                + 2 * (chroma_width / 4 + 2) * (chroma_height / 4 + 2),
        }
    }

    /// Bytes required for packed RGB24 output. Independent of resource limits.
    pub fn rgb_buffer_size(self) -> usize {
        usize::from(self.width) * usize::from(self.height) * 3
    }

    /// Check the pixel requirement against a resource policy before allocating.
    /// Packet byte limits are checked by the decoder when packets are supplied.
    pub fn validate_limits(self, limits: VideoLimits) -> Result<(), Error> {
        if usize::from(self.width) * usize::from(self.height) > limits.max_pixels {
            return Err(Error::Limit("pixel count"));
        }
        Ok(())
    }
}

/// A validated, tightly packed, borrowed eight-bit image plane.
/// Obtain planes from [`Frame::y`], [`Frame::u`] and [`Frame::v`].
#[derive(Debug, Clone, Copy)]
pub struct Plane<'a> {
    data: &'a [u8],
    width: usize,
    height: usize,
}

impl<'a> Plane<'a> {
    /// Samples in row-major order; length is exactly width times height.
    pub const fn data(self) -> &'a [u8] {
        self.data
    }

    /// Samples per row (also the stride in bytes).
    pub const fn width(self) -> usize {
        self.width
    }

    /// Number of rows.
    pub const fn height(self) -> usize {
        self.height
    }
}

/// A validated, immutable decoded frame, borrowing reusable storage.
///
/// Decoders produce frames automatically. Use [`Self::from_planes`] to wrap
/// external tightly packed planes; sizes are checked before a frame is created.
#[derive(Debug, Clone, Copy)]
pub struct Frame<'a> {
    display_index: u32,
    kind: FrameType,
    info: VideoInfo,
    y: Plane<'a>,
    u: Plane<'a>,
    v: Plane<'a>,
}

impl<'a> Frame<'a> {
    /// Validate three tightly packed Y/U/V planes against a video layout.
    /// Plane lengths must exactly match the layout; padded strides are unsupported.
    pub fn from_planes(
        info: VideoInfo,
        kind: FrameType,
        display_index: u32,
        planes: [&'a [u8]; 3],
    ) -> Result<Self, Error> {
        let width = usize::from(info.width);
        let height = usize::from(info.height);
        let cw = width / usize::from(info.sampling.horizontal_factor());
        let ch = height / usize::from(info.sampling.vertical_factor());
        if planes[0].len() != width * height
            || planes[1].len() != cw * ch
            || planes[2].len() != cw * ch
        {
            return Err(Error::Invalid("plane lengths disagree with video layout"));
        }
        Ok(Self {
            info,
            kind,
            display_index,
            y: Plane {
                data: planes[0],
                width,
                height,
            },
            u: Plane {
                data: planes[1],
                width: cw,
                height: ch,
            },
            v: Plane {
                data: planes[2],
                width: cw,
                height: ch,
            },
        })
    }

    pub(crate) fn new(
        info: VideoInfo,
        kind: FrameType,
        display_index: u32,
        data: &'a [u8],
    ) -> Result<Self, Error> {
        let width = usize::from(info.width);
        let height = usize::from(info.height);
        let cw = width / usize::from(info.sampling.horizontal_factor());
        let ch = height / usize::from(info.sampling.vertical_factor());
        let (y, rest) = data
            .split_at_checked(width * height)
            .ok_or(Error::Truncated)?;
        let (u, v) = rest.split_at_checked(cw * ch).ok_or(Error::Truncated)?;
        Self::from_planes(info, kind, display_index, [y, u, v])
    }

    /// Zero-based presentation index, including the container's GOP offset.
    pub const fn display_index(self) -> u32 {
        self.display_index
    }

    /// Picture coding type.
    pub const fn kind(self) -> FrameType {
        self.kind
    }

    /// Validated video layout.
    pub const fn info(self) -> VideoInfo {
        self.info
    }

    /// Luma plane.
    pub const fn y(self) -> Plane<'a> {
        self.y
    }

    /// Blue-difference chroma plane.
    pub const fn u(self) -> Plane<'a> {
        self.u
    }

    /// Red-difference chroma plane.
    pub const fn v(self) -> Plane<'a> {
        self.v
    }

    /// Y, U, V planes in order.
    pub const fn planes(self) -> [Plane<'a>; 3] {
        [self.y, self.u, self.v]
    }

    /// Bytes needed for packed RGB24 output.
    pub fn rgb_buffer_size(self) -> usize {
        self.info.rgb_buffer_size()
    }

    /// Convert to packed RGB24 in caller-provided storage without allocating.
    /// Extra output bytes are unchanged. Insufficient storage leaves output untouched.
    /// Uses full-range YUV, nearest-neighbor chroma sampling, and truncation.
    pub fn to_rgb_into(&self, output: &mut [u8]) -> Result<(), Error> {
        let required = self.rgb_buffer_size();
        if output.len() < required {
            return Err(Error::BufferTooSmall {
                buffer: BufferKind::Rgb,
                required,
                provided: output.len(),
            });
        }
        let output = &mut output[..required];
        let hs = usize::from(self.info.sampling.horizontal_factor());
        let vs = usize::from(self.info.sampling.vertical_factor());
        for row in 0..self.y.height {
            for col in 0..self.y.width {
                let pos = row * self.y.width + col;
                let uv = (row / vs) * self.u.width + col / hs;
                let y = f32::from(self.y.data[pos]);
                let u = f32::from(self.u.data[uv]) - 128.0;
                let v = f32::from(self.v.data[uv]) - 128.0;
                output[pos * 3] = (y + 1.402 * v) as u8;
                output[pos * 3 + 1] = (y - 0.34414 * u - 0.71414 * v) as u8;
                output[pos * 3 + 2] = (y + 1.772 * u) as u8;
            }
        }
        Ok(())
    }

    /// Convert to RGB24, growing/reusing a vector. Allocation failures return an error.
    #[cfg(feature = "alloc")]
    pub fn to_rgb(&self, output: &mut alloc::vec::Vec<u8>) -> Result<(), Error> {
        let required = self.rgb_buffer_size();
        output.try_reserve(required.saturating_sub(output.len()))?;
        output.resize(required, 0);
        self.to_rgb_into(output)
    }

    /// Write a binary PPM image, reusing `scratch` for RGB conversion.
    #[cfg(feature = "std")]
    pub fn write_ppm<W: std::io::Write>(
        &self,
        mut writer: W,
        scratch: &mut alloc::vec::Vec<u8>,
    ) -> Result<(), Error> {
        self.to_rgb(scratch)?;
        write!(writer, "P6\n{} {}\n255\n", self.y.width, self.y.height)?;
        writer.write_all(scratch)?;
        Ok(())
    }

    /// Write tightly packed Y, U, V planes in that order.
    #[cfg(feature = "std")]
    pub fn write_yuv<W: std::io::Write>(&self, mut writer: W) -> Result<(), Error> {
        writer.write_all(self.y.data)?;
        writer.write_all(self.u.data)?;
        writer.write_all(self.v.data)?;
        Ok(())
    }
}