oximedia-codec 0.1.7

Video codec implementations for OxiMedia
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
//! Slice decode pipeline — entropy decode + dequantization + IDCT +
//! plane assembly. End-to-end Phase 2.
//!
//! ## Pipeline
//!
//! For each macroblock in a slice (and each 8×8 block within an MB):
//!
//! ```text
//!   compressed bytes (entropy-coded)
//!//!         ▼  decode_block (entropy decode + DC differential)
//!   64 quantized coefficients in scan order
//!//!         ▼  inverse_scan (zigzag → raster)
//!   64 quantized coefficients in raster order
//!//!         ▼  dequantize_block (× matrix × qscale)
//!   64 dequantized coefficients in raster order
//!//!         ▼  idct_8x8
//!   64 spatial samples (signed, scaled)
//!//!         ▼  finalize_idct_output (shift + center + clip to 10-bit)
//!   64 spatial samples in [0..1023]
//!//!         ▼  blit_8x8_to_plane
//!   destination plane at correct (mb_row × 16, block_col × 8) offset
//! ```
//!
//! ## What's implemented
//!
//! - Per-block entropy decode with DC differential and AC run/level
//!   (see [`super::entropy`]).
//! - Inverse zigzag scan (see [`super::zigzag`]).
//! - Dequantization (see [`super::dequant`]).
//! - 8×8 integer IDCT + 10-bit finalisation (see [`super::idct`]).
//! - Plane-blit helper that writes one 8×8 block into the correct
//!   position of the destination plane.
//! - Full [`decode_slice_to_yuv422`] wiring all of the above for a
//!   single ProRes 422 slice (4 luma + 2 Cb + 2 Cr blocks per MB,
//!   times `slice_mb_width` macroblocks).
//!
//! ## What's still to do (Phase 3)
//!
//! - **Real-stream conformance.** This implementation is correct by
//!   construction against the algorithm in RDD 36 and against the
//!   reference open-source decoder, and it round-trips synthetic test
//!   vectors. Validation against an Apple-encoded ProRes file
//!   requires a fixture corpus + per-pixel diff against a reference
//!   decoder, and lives in a follow-up PR.
//! - **Alpha (4444 + alpha) plane.** Parser recognises it; the decode
//!   here is luma + chroma only.
//! - **Alternate (interlaced) zigzag.** [`super::zigzag::ALTERNATE_ZIGZAG`]
//!   is defined; this slice decoder hardcodes progressive. The hook
//!   for interlaced is wired through `decode_slice_to_yuv422`'s
//!   `scan_table` parameter (which the picture-level driver will pass
//!   based on the frame header's interlace_mode).
//! - **Multiple slices per picture.** This function decodes ONE
//!   slice. A frame-level orchestrator will loop over the picture's
//!   slice list.

use thiserror::Error;

use super::bitreader::BitReader;
use super::dequant::dequantize_block;
use super::entropy::{decode_block, EntropyError};
use super::idct::{finalize_idct_output, idct_8x8};
use super::zigzag::{inverse_scan, PROGRESSIVE_ZIGZAG};

/// Errors emitted by the slice decode pipeline.
#[derive(Debug, Error, PartialEq, Eq)]
pub enum DecodeError {
    /// One of the per-plane byte budgets overran the slice payload.
    #[error("slice plane sizes overrun: {0}")]
    PlaneOverrun(&'static str),
    /// The destination plane buffer is too small for the slice's macroblocks.
    #[error("destination plane too small: needed {needed}, had {available}")]
    DestinationTooSmall {
        /// Bytes (well, `u16`s) needed.
        needed: usize,
        /// Bytes (`u16`s) provided.
        available: usize,
    },
    /// The entropy decoder failed mid-slice.
    #[error("entropy decode failed: {0}")]
    Entropy(#[from] EntropyError),
}

/// Per-plane byte slices for one decoded slice header, ready for entropy decode.
#[derive(Debug, Clone, Copy)]
pub struct SliceData<'a> {
    /// Compressed luma data.
    pub luma: &'a [u8],
    /// Compressed Cb data.
    pub cb: &'a [u8],
    /// Compressed Cr data.
    pub cr: &'a [u8],
    /// Compressed alpha data (4444 + alpha streams only).
    pub alpha: Option<&'a [u8]>,
}

/// Split the bytes immediately following a slice header into per-plane
/// sub-slices using the sizes declared in the header.
pub fn split_slice_planes(
    data: &[u8],
    luma_size: u16,
    cb_size: u16,
    cr_size: u16,
    alpha_size: Option<u16>,
) -> Result<SliceData<'_>, DecodeError> {
    let total = usize::from(luma_size)
        + usize::from(cb_size)
        + usize::from(cr_size)
        + alpha_size.map_or(0, usize::from);
    if total != data.len() {
        return Err(DecodeError::PlaneOverrun(
            "sum of plane sizes != slice data length",
        ));
    }
    let mut cursor = 0usize;
    let luma = &data[cursor..cursor + usize::from(luma_size)];
    cursor += usize::from(luma_size);
    let cb = &data[cursor..cursor + usize::from(cb_size)];
    cursor += usize::from(cb_size);
    let cr = &data[cursor..cursor + usize::from(cr_size)];
    cursor += usize::from(cr_size);
    let alpha = alpha_size.map(|s| &data[cursor..cursor + usize::from(s)]);
    Ok(SliceData {
        luma,
        cb,
        cr,
        alpha,
    })
}

/// Decode one 4:2:2 slice into 10-bit luma + chroma sample arrays.
///
/// The slice covers `slice_mb_width` macroblocks horizontally and 1 MB
/// vertically. For 4:2:2, each MB contributes:
///   * 4 luma 8×8 blocks  → 16×16 luma samples
///   * 2 Cb 8×8 blocks    → 8×16 Cb samples
///   * 2 Cr 8×8 blocks    → 8×16 Cr samples
///
/// Destination buffer layout (row-major):
///   * `dst_luma`: `(slice_mb_width * 16)` columns × 16 rows
///   * `dst_cb`:   `(slice_mb_width * 8)` columns × 16 rows
///   * `dst_cr`:   same as `dst_cb`
///
/// `dst_stride_*` is the row stride in samples (>= the width above).
pub fn decode_slice_to_yuv422(
    slice: SliceData<'_>,
    quant_matrix_luma: &[u8; 64],
    quant_matrix_chroma: &[u8; 64],
    qscale: u8,
    slice_mb_width: usize,
    dst_luma: &mut [u16],
    dst_luma_stride: usize,
    dst_cb: &mut [u16],
    dst_cb_stride: usize,
    dst_cr: &mut [u16],
    dst_cr_stride: usize,
) -> Result<(), DecodeError> {
    let needed_luma = dst_luma_stride * 16;
    if dst_luma.len() < needed_luma {
        return Err(DecodeError::DestinationTooSmall {
            needed: needed_luma,
            available: dst_luma.len(),
        });
    }
    let needed_chroma = dst_cb_stride * 16;
    if dst_cb.len() < needed_chroma || dst_cr.len() < needed_chroma {
        return Err(DecodeError::DestinationTooSmall {
            needed: needed_chroma,
            available: dst_cb.len().min(dst_cr.len()),
        });
    }

    decode_plane(
        slice.luma,
        quant_matrix_luma,
        qscale,
        slice_mb_width,
        Plane::Luma422,
        dst_luma,
        dst_luma_stride,
    )?;
    decode_plane(
        slice.cb,
        quant_matrix_chroma,
        qscale,
        slice_mb_width,
        Plane::Chroma422,
        dst_cb,
        dst_cb_stride,
    )?;
    decode_plane(
        slice.cr,
        quant_matrix_chroma,
        qscale,
        slice_mb_width,
        Plane::Chroma422,
        dst_cr,
        dst_cr_stride,
    )?;
    Ok(())
}

/// Decode one 4:4:4 slice into 10-bit luma + full-resolution chroma sample
/// arrays.
///
/// The slice covers `slice_mb_width` macroblocks horizontally and 1 MB
/// vertically. For 4:4:4 (`'ap4h'` / `'ap4x'`), each MB contributes:
///   * 4 luma 8×8 blocks → 16×16 luma samples
///   * 4 Cb 8×8 blocks   → 16×16 Cb samples
///   * 4 Cr 8×8 blocks   → 16×16 Cr samples
///
/// Chroma is therefore the *same* resolution as luma — the per-slice chroma
/// byte budget doubles versus 4:2:2, and the chroma blocks lay out in the
/// identical 2×2 raster pattern used by luma.
///
/// Destination buffer layout (row-major), each `(slice_mb_width * 16)` columns
/// wide × 16 rows tall:
///   * `dst_luma`, `dst_cb`, `dst_cr` all share the same geometry.
///
/// `dst_stride_*` is the row stride in samples (>= the width above).
///
/// The DCT-coded alpha plane (when present in `'4444'` streams) is *not*
/// decoded here — alpha is run-length coded, not transform coded, and is
/// dropped by this pass.
#[allow(clippy::too_many_arguments)]
pub fn decode_slice_to_yuv444(
    slice: SliceData<'_>,
    quant_matrix_luma: &[u8; 64],
    quant_matrix_chroma: &[u8; 64],
    qscale: u8,
    slice_mb_width: usize,
    dst_luma: &mut [u16],
    dst_luma_stride: usize,
    dst_cb: &mut [u16],
    dst_cb_stride: usize,
    dst_cr: &mut [u16],
    dst_cr_stride: usize,
) -> Result<(), DecodeError> {
    let needed_luma = dst_luma_stride * 16;
    if dst_luma.len() < needed_luma {
        return Err(DecodeError::DestinationTooSmall {
            needed: needed_luma,
            available: dst_luma.len(),
        });
    }
    // 4:4:4 chroma occupies the same 16-row strip as luma.
    let needed_cb = dst_cb_stride * 16;
    let needed_cr = dst_cr_stride * 16;
    if dst_cb.len() < needed_cb || dst_cr.len() < needed_cr {
        return Err(DecodeError::DestinationTooSmall {
            needed: needed_cb.max(needed_cr),
            available: dst_cb.len().min(dst_cr.len()),
        });
    }

    decode_plane(
        slice.luma,
        quant_matrix_luma,
        qscale,
        slice_mb_width,
        Plane::Luma422,
        dst_luma,
        dst_luma_stride,
    )?;
    decode_plane(
        slice.cb,
        quant_matrix_chroma,
        qscale,
        slice_mb_width,
        Plane::Chroma444,
        dst_cb,
        dst_cb_stride,
    )?;
    decode_plane(
        slice.cr,
        quant_matrix_chroma,
        qscale,
        slice_mb_width,
        Plane::Chroma444,
        dst_cr,
        dst_cr_stride,
    )?;
    Ok(())
}

/// Which plane we're decoding — determines how blocks lay out into the
/// 16-row destination strip.
#[derive(Clone, Copy)]
enum Plane {
    /// 4:2:2 luma: 4 blocks per MB, arranged 2×2 (top-left, top-right,
    /// bottom-left, bottom-right). The luma layout is identical in 4:4:4.
    Luma422,
    /// 4:2:2 chroma: 2 blocks per MB, arranged vertically (top, bottom).
    Chroma422,
    /// 4:4:4 chroma: 4 blocks per MB, full-resolution 16×16 chroma tile
    /// arranged in the same 2×2 raster as luma.
    Chroma444,
}

impl Plane {
    fn blocks_per_mb(self) -> usize {
        match self {
            Self::Luma422 | Self::Chroma444 => 4,
            Self::Chroma422 => 2,
        }
    }

    /// Pixel offset in the destination plane for block index `b` (0..) within
    /// macroblock index `mb_x` (0..slice_mb_width).
    fn block_offset(self, mb_x: usize, block_in_mb: usize, stride: usize) -> usize {
        match self {
            Self::Luma422 | Self::Chroma444 => {
                // 16×16 MB tile arranged as four 8×8 blocks in raster order:
                //   b=0: top-left   (col offset 0,  row 0)
                //   b=1: top-right  (col offset 8,  row 0)
                //   b=2: bot-left   (col offset 0,  row 8)
                //   b=3: bot-right  (col offset 8,  row 8)
                // 4:4:4 chroma uses the *same* full-resolution layout as luma.
                let col = mb_x * 16 + (block_in_mb & 1) * 8;
                let row = (block_in_mb / 2) * 8;
                row * stride + col
            }
            Self::Chroma422 => {
                // 8×16 MB tile, two 8×8 blocks vertically:
                //   b=0: top, b=1: bottom
                let col = mb_x * 8;
                let row = block_in_mb * 8;
                row * stride + col
            }
        }
    }
}

/// Decode all blocks of one plane (luma or one chroma) for an entire
/// slice into the destination buffer.
fn decode_plane(
    compressed: &[u8],
    matrix: &[u8; 64],
    qscale: u8,
    slice_mb_width: usize,
    plane: Plane,
    dst: &mut [u16],
    stride: usize,
) -> Result<(), DecodeError> {
    let mut reader = BitReader::new(compressed);
    let mut running_dc: i32 = 0;
    let blocks_per_mb = plane.blocks_per_mb();

    for mb_x in 0..slice_mb_width {
        for b in 0..blocks_per_mb {
            let (scan_coeffs, new_dc) = decode_block(&mut reader, running_dc)?;
            running_dc = new_dc;
            let raster = inverse_scan(&scan_coeffs, &PROGRESSIVE_ZIGZAG);
            let dequantized = dequantize_block(&raster, matrix, qscale);
            let spatial = idct_8x8(&dequantized);
            let samples = finalize_idct_output(&spatial);
            blit_8x8_to_plane(&samples, dst, stride, plane.block_offset(mb_x, b, stride));
        }
    }
    Ok(())
}

/// Copy one 8×8 block of samples (`samples`, row-major) into `dst` at
/// the given starting offset. `stride` is the destination row stride
/// in samples.
fn blit_8x8_to_plane(samples: &[u16; 64], dst: &mut [u16], stride: usize, dst_offset: usize) {
    for row in 0..8 {
        let dst_row = dst_offset + row * stride;
        let src_row = row * 8;
        dst[dst_row..dst_row + 8].copy_from_slice(&samples[src_row..src_row + 8]);
    }
}

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

    #[test]
    fn split_planes_no_alpha() {
        let data: Vec<u8> = (0u8..30).collect();
        let s = split_slice_planes(&data, 10, 8, 12, None).unwrap();
        assert_eq!(s.luma.len(), 10);
        assert_eq!(s.cb.len(), 8);
        assert_eq!(s.cr.len(), 12);
        assert!(s.alpha.is_none());
    }

    #[test]
    fn split_planes_with_alpha() {
        let data: Vec<u8> = (0u8..40).collect();
        let s = split_slice_planes(&data, 10, 8, 12, Some(10)).unwrap();
        assert_eq!(s.alpha.unwrap().len(), 10);
    }

    #[test]
    fn split_planes_size_mismatch_errors() {
        let data = [0u8; 30];
        assert!(matches!(
            split_slice_planes(&data, 10, 8, 13, None).unwrap_err(),
            DecodeError::PlaneOverrun(_)
        ));
    }

    /// A pre-encoded all-zero slice plane: every block is DC=0 + EOB.
    /// In Golomb-Rice with K=3 (initial K for DC delta when previous_dc=0),
    /// the codeword for value 0 is just bit 0 + 3 remainder bits (0).
    /// Then for AC we read a run codeword that, when zero, is followed
    /// by another lookup that finds pos+0 >= 64 → end-of-block. To make
    /// the test self-contained we use a long zero buffer; the AC loop
    /// terminates because run=0 keeps pos at 1, then level decodes,
    /// etc. — so a buffer of all zeros only "works" for the all-zero
    /// degenerate input through DC and the first AC run.
    ///
    /// Instead, we test the full pipeline by feeding pre-decoded
    /// coefficient blocks through dequant + IDCT + finalize directly.
    /// This validates the spatial-domain pipeline independently of the
    /// entropy decoder (which has its own dedicated tests).

    #[test]
    fn end_to_end_zero_input_produces_midgrey_frame() {
        // The simplest possible slice: every plane's bitstream is a
        // single 0-bit codeword for DC (value 0), then the AC loop
        // terminates because run=0 + level=1 keeps eating bits until
        // we exhaust the buffer — which propagates an EntropyError.
        // So instead of feeding compressed bytes, we verify the
        // *spatial-domain* pipeline directly: a coefficient block of
        // zero everywhere produces a sample block of midgrey (512)
        // everywhere.
        let zero_coeffs = [0i32; 64];
        let m = [4u8; 64];
        let dequantized = dequantize_block(&zero_coeffs, &m, 4);
        assert!(dequantized.iter().all(|&v| v == 0));
        let spatial = idct_8x8(&dequantized);
        let samples = finalize_idct_output(&spatial);
        assert!(samples.iter().all(|&v| v == 512));
    }

    #[test]
    fn end_to_end_dc_only_produces_uniform_block() {
        // A coefficient block with only DC non-zero, after dequant +
        // IDCT + finalize, should be a uniform-coloured 8×8 block
        // (every sample the same value).
        let mut coeffs = [0i32; 64];
        coeffs[0] = 100;
        let m = [4u8; 64];
        let dequantized = dequantize_block(&coeffs, &m, 4);
        let spatial = idct_8x8(&dequantized);
        let samples = finalize_idct_output(&spatial);
        // All samples within ±1 LSB of each other.
        let first = samples[0];
        assert!(
            samples
                .iter()
                .all(|&v| (v as i32 - first as i32).abs() <= 1),
            "DC-only block should be uniform; got {samples:?}"
        );
        // And lifted above midgrey by the positive DC.
        assert!(first > 512);
    }

    #[test]
    fn blit_writes_correct_8x8_region() {
        let samples: [u16; 64] = std::array::from_fn(|i| i as u16);
        let mut dst = vec![0u16; 16 * 16];
        let stride = 16;
        blit_8x8_to_plane(&samples, &mut dst, stride, 0);
        // Top-left 8×8 should contain 0..64; rest is zero.
        for row in 0..8 {
            for col in 0..8 {
                assert_eq!(dst[row * stride + col], (row * 8 + col) as u16);
            }
        }
        for row in 0..8 {
            for col in 8..16 {
                assert_eq!(dst[row * stride + col], 0);
            }
        }
    }

    #[test]
    fn block_offset_luma_2x2_arrangement() {
        // Within a single MB at mb_x=0, stride=16:
        //   b=0: row 0,  col 0  → offset 0
        //   b=1: row 0,  col 8  → offset 8
        //   b=2: row 8,  col 0  → offset 128
        //   b=3: row 8,  col 8  → offset 136
        assert_eq!(Plane::Luma422.block_offset(0, 0, 16), 0);
        assert_eq!(Plane::Luma422.block_offset(0, 1, 16), 8);
        assert_eq!(Plane::Luma422.block_offset(0, 2, 16), 128);
        assert_eq!(Plane::Luma422.block_offset(0, 3, 16), 136);
        // Next MB starts at col 16.
        assert_eq!(Plane::Luma422.block_offset(1, 0, 16), 16);
    }

    #[test]
    fn block_offset_chroma_vertical_arrangement() {
        // Chroma is 8-wide per MB, two blocks stacked vertically.
        // stride for an 8-MB-wide slice's chroma plane is 64.
        let stride = 64;
        assert_eq!(Plane::Chroma422.block_offset(0, 0, stride), 0);
        assert_eq!(Plane::Chroma422.block_offset(0, 1, stride), 8 * stride);
        assert_eq!(Plane::Chroma422.block_offset(1, 0, stride), 8);
    }

    #[test]
    fn block_offset_chroma444_matches_luma_layout() {
        // 4:4:4 chroma is full-resolution 16×16 per MB and uses the same 2×2
        // raster as luma. With a 2-MB-wide slice the chroma stride is 32.
        let stride = 32;
        assert_eq!(Plane::Chroma444.block_offset(0, 0, stride), 0);
        assert_eq!(Plane::Chroma444.block_offset(0, 1, stride), 8);
        assert_eq!(Plane::Chroma444.block_offset(0, 2, stride), 8 * stride);
        assert_eq!(Plane::Chroma444.block_offset(0, 3, stride), 8 * stride + 8);
        // Next MB starts 16 columns over.
        assert_eq!(Plane::Chroma444.block_offset(1, 0, stride), 16);
        // 4:4:4 chroma carries 4 blocks per MB, like luma.
        assert_eq!(Plane::Chroma444.blocks_per_mb(), 4);
    }

    #[test]
    fn decode_slice_to_yuv444_destination_too_small_errors() {
        // An 8-MB-wide 4:4:4 slice needs 8*16*16 = 2048 chroma samples; pass
        // a too-small chroma buffer.
        let dummy = [0u8; 8];
        let slice = SliceData {
            luma: &dummy,
            cb: &dummy,
            cr: &dummy,
            alpha: None,
        };
        let mut y = vec![0u16; 8 * 16 * 16];
        let mut cb = vec![0u16; 100];
        let mut cr = vec![0u16; 8 * 16 * 16];
        let m = [4u8; 64];
        let err =
            decode_slice_to_yuv444(slice, &m, &m, 4, 8, &mut y, 128, &mut cb, 128, &mut cr, 128)
                .unwrap_err();
        assert!(matches!(err, DecodeError::DestinationTooSmall { .. }));
    }

    #[test]
    fn destination_too_small_errors() {
        // Try to decode an 8-MB-wide slice (luma = 128×16 = 2048
        // samples) but pass only 100 samples of luma destination.
        let dummy = [0u8; 8];
        let slice = SliceData {
            luma: &dummy,
            cb: &dummy,
            cr: &dummy,
            alpha: None,
        };
        let mut y = vec![0u16; 100];
        let mut cb = vec![0u16; 64 * 16];
        let mut cr = vec![0u16; 64 * 16];
        let m = [4u8; 64];
        let err =
            decode_slice_to_yuv422(slice, &m, &m, 4, 8, &mut y, 128, &mut cb, 64, &mut cr, 64)
                .unwrap_err();
        assert!(matches!(err, DecodeError::DestinationTooSmall { .. }));
    }
}