zenavif 0.1.4

Pure Rust AVIF image codec powered by rav1d and zenravif
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
//! Raw AV1 OBU bitstream decoding
//!
//! Decodes raw AV1 OBU byte sequences to pixels without requiring an AVIF
//! container. This is used for AVIF gain map images, which are stored as
//! raw AV1 bitstreams inside the AVIF container but outside the normal
//! primary/alpha item structure.

#![deny(unsafe_code)]

use crate::error::{Error, Result};
use rav1d_safe::src::managed::{
    Decoder as Rav1dDecoder, Frame, MatrixCoefficients as Rav1dMatrixCoefficients, PixelLayout,
    Planes, Settings,
};
use rgb::Rgb;
use whereat::at;
use yuv::{YuvGrayImage, YuvPlanarImage, YuvRange, YuvStandardMatrix};

/// Convert rav1d matrix coefficients to yuv crate's YuvStandardMatrix
fn to_yuv_matrix(mc: Rav1dMatrixCoefficients) -> YuvStandardMatrix {
    match mc {
        Rav1dMatrixCoefficients::BT709 => YuvStandardMatrix::Bt709,
        Rav1dMatrixCoefficients::BT601 => YuvStandardMatrix::Bt601,
        Rav1dMatrixCoefficients::BT2020NCL => YuvStandardMatrix::Bt2020,
        _ => YuvStandardMatrix::Bt601,
    }
}

/// Decode a raw AV1 OBU bitstream to pixels.
///
/// This decodes AV1 data that is not wrapped in an AVIF container,
/// such as AVIF gain map images stored as raw AV1 bitstreams.
///
/// Returns `(pixel_data, width, height, channels)` where channels is
/// 1 for grayscale (monochrome AV1) or 3 for RGB.
///
/// For 10-bit or 12-bit AV1 streams, values are scaled down to 8-bit.
///
/// # Errors
///
/// Returns an error if the data is not valid AV1, if decoding fails,
/// or if the decoded frame cannot be converted to pixels.
///
/// # Example
///
/// ```no_run
/// let av1_obu_data: &[u8] = &[/* raw AV1 OBU bytes */];
/// let (pixels, width, height, channels) = zenavif::decode_av1_obu(av1_obu_data).unwrap();
/// if channels == 1 {
///     println!("Grayscale {}x{}", width, height);
/// } else {
///     println!("RGB {}x{}", width, height);
/// }
/// ```
pub fn decode_av1_obu(data: &[u8]) -> Result<(Vec<u8>, u32, u32, u8)> {
    if data.is_empty() {
        return Err(at!(Error::Decode {
            code: -1,
            msg: "empty AV1 OBU data",
        }));
    }

    let mut settings = Settings::default();
    settings.threads = 1;

    let mut decoder = Rav1dDecoder::with_settings(settings).map_err(|_e| {
        at!(Error::Decode {
            code: -1,
            msg: "failed to create AV1 decoder",
        })
    })?;

    let frame = decode_single_frame(&mut decoder, data)?;

    let bit_depth = frame.bit_depth();
    let layout = frame.pixel_layout();

    let color_info = frame.color_info();
    let yuv_range = if matches!(
        color_info.color_range,
        rav1d_safe::src::managed::ColorRange::Full
    ) {
        YuvRange::Full
    } else {
        YuvRange::Limited
    };
    let matrix = to_yuv_matrix(color_info.matrix_coefficients);

    match layout {
        PixelLayout::I400 => convert_monochrome(&frame, bit_depth, yuv_range, matrix),
        _ => convert_to_rgb(&frame, bit_depth, yuv_range, matrix),
    }
}

/// Decode a single frame from AV1 OBU data, handling progressive/multi-layer
/// streams by flushing the decoder if needed.
fn decode_single_frame(decoder: &mut Rav1dDecoder, data: &[u8]) -> Result<Frame> {
    match decoder.decode(data) {
        Ok(Some(frame)) => {
            let _ = decoder.flush();
            Ok(frame)
        }
        Ok(None) => {
            // Progressive/multi-layer: flush to get the composed frame
            let frames = decoder.flush().map_err(|_e| {
                at!(Error::Decode {
                    code: -1,
                    msg: "failed to flush AV1 decoder",
                })
            })?;
            frames.into_iter().last().ok_or_else(|| {
                at!(Error::Decode {
                    code: -1,
                    msg: "AV1 decoder produced no frames",
                })
            })
        }
        Err(_e) => Err(at!(Error::Decode {
            code: -1,
            msg: "failed to decode AV1 OBU data",
        })),
    }
}

/// Convert a monochrome (I400) frame to grayscale u8 pixels.
fn convert_monochrome(
    frame: &Frame,
    bit_depth: u8,
    yuv_range: YuvRange,
    matrix: YuvStandardMatrix,
) -> Result<(Vec<u8>, u32, u32, u8)> {
    let width = frame.width();
    let height = frame.height();
    let pixel_count = (width as usize)
        .checked_mul(height as usize)
        .ok_or_else(|| at!(Error::OutOfMemory))?;

    if bit_depth == 8 {
        let Planes::Depth8(planes) = frame.planes() else {
            return Err(at!(Error::Decode {
                code: -1,
                msg: "expected 8-bit planes for 8-bit frame",
            }));
        };

        let y_view = planes.y();

        // For monochrome, we need Y-to-gray conversion respecting range
        // Use yuv crate's yuv400_to_rgb which produces R=G=B=luma, then
        // extract just one channel.
        let mut rgb_out = vec![Rgb { r: 0u8, g: 0, b: 0 }; pixel_count];
        let rgb_stride = width * 3;
        let gray = YuvGrayImage {
            y_plane: y_view.as_slice(),
            y_stride: y_view.stride() as u32,
            width,
            height,
        };
        yuv::yuv400_to_rgb(
            &gray,
            rgb::bytemuck::cast_slice_mut(rgb_out.as_mut_slice()),
            rgb_stride,
            yuv_range,
            matrix,
        )
        .map_err(|e| at!(Error::ColorConversion(e)))?;

        // Extract just the R channel (R=G=B for monochrome)
        let gray_pixels: Vec<u8> = rgb_out.iter().map(|px| px.r).collect();
        Ok((gray_pixels, width, height, 1))
    } else {
        let Planes::Depth16(planes) = frame.planes() else {
            return Err(at!(Error::Decode {
                code: -1,
                msg: "expected 16-bit planes for high-bit-depth frame",
            }));
        };

        let y_view = planes.y();
        let pixel_count_u = width as usize * height as usize;

        // Convert 10/12-bit Y plane through yuv crate, then scale to 8-bit
        let mut rgb16_out = vec![Rgb::<u16> { r: 0, g: 0, b: 0 }; pixel_count_u];
        let rgb_stride = width * 3;
        let gray = YuvGrayImage {
            y_plane: y_view.as_slice(),
            y_stride: y_view.stride() as u32,
            width,
            height,
        };

        match bit_depth {
            10 => yuv::y010_to_rgb10(
                &gray,
                rgb::bytemuck::cast_slice_mut(rgb16_out.as_mut_slice()),
                rgb_stride,
                yuv_range,
                matrix,
            ),
            12 => yuv::y012_to_rgb12(
                &gray,
                rgb::bytemuck::cast_slice_mut(rgb16_out.as_mut_slice()),
                rgb_stride,
                yuv_range,
                matrix,
            ),
            _ => yuv::y016_to_rgb16(
                &gray,
                rgb::bytemuck::cast_slice_mut(rgb16_out.as_mut_slice()),
                rgb_stride,
                yuv_range,
                matrix,
            ),
        }
        .map_err(|e| at!(Error::ColorConversion(e)))?;

        // Scale 10/12/16-bit down to 8-bit, extracting just the R channel
        let shift = bit_depth.saturating_sub(8);
        let gray_pixels: Vec<u8> = rgb16_out
            .iter()
            .map(|px| (px.r >> shift).min(255) as u8)
            .collect();
        Ok((gray_pixels, width, height, 1))
    }
}

/// Convert a YUV frame (I420/I422/I444) to RGB u8 pixels.
fn convert_to_rgb(
    frame: &Frame,
    bit_depth: u8,
    yuv_range: YuvRange,
    matrix: YuvStandardMatrix,
) -> Result<(Vec<u8>, u32, u32, u8)> {
    let width = frame.width();
    let height = frame.height();
    let layout = frame.pixel_layout();
    let pixel_count = (width as usize)
        .checked_mul(height as usize)
        .ok_or_else(|| at!(Error::OutOfMemory))?;

    if bit_depth == 8 {
        let Planes::Depth8(planes) = frame.planes() else {
            return Err(at!(Error::Decode {
                code: -1,
                msg: "expected 8-bit planes for 8-bit frame",
            }));
        };

        let y_view = planes.y();
        let u_view = planes.u().ok_or_else(|| {
            at!(Error::Decode {
                code: -1,
                msg: "missing U chroma plane",
            })
        })?;
        let v_view = planes.v().ok_or_else(|| {
            at!(Error::Decode {
                code: -1,
                msg: "missing V chroma plane",
            })
        })?;

        let planar = YuvPlanarImage {
            y_plane: y_view.as_slice(),
            y_stride: y_view.stride() as u32,
            u_plane: u_view.as_slice(),
            u_stride: u_view.stride() as u32,
            v_plane: v_view.as_slice(),
            v_stride: v_view.stride() as u32,
            width,
            height,
        };

        let mut out = vec![Rgb { r: 0u8, g: 0, b: 0 }; pixel_count];
        let rgb_stride = width * 3;

        match layout {
            PixelLayout::I420 => yuv::yuv420_to_rgb_bilinear(
                &planar,
                rgb::bytemuck::cast_slice_mut(out.as_mut_slice()),
                rgb_stride,
                yuv_range,
                matrix,
            ),
            PixelLayout::I422 => yuv::yuv422_to_rgb_bilinear(
                &planar,
                rgb::bytemuck::cast_slice_mut(out.as_mut_slice()),
                rgb_stride,
                yuv_range,
                matrix,
            ),
            PixelLayout::I444 => yuv::yuv444_to_rgb(
                &planar,
                rgb::bytemuck::cast_slice_mut(out.as_mut_slice()),
                rgb_stride,
                yuv_range,
                matrix,
            ),
            PixelLayout::I400 => unreachable!("monochrome handled separately"),
        }
        .map_err(|e| at!(Error::ColorConversion(e)))?;

        let bytes: Vec<u8> = rgb::bytemuck::cast_vec(out);
        Ok((bytes, width, height, 3))
    } else {
        let Planes::Depth16(planes) = frame.planes() else {
            return Err(at!(Error::Decode {
                code: -1,
                msg: "expected 16-bit planes for high-bit-depth frame",
            }));
        };

        let y_view = planes.y();
        let u_view = planes.u().ok_or_else(|| {
            at!(Error::Decode {
                code: -1,
                msg: "missing U chroma plane",
            })
        })?;
        let v_view = planes.v().ok_or_else(|| {
            at!(Error::Decode {
                code: -1,
                msg: "missing V chroma plane",
            })
        })?;

        let planar = YuvPlanarImage {
            y_plane: y_view.as_slice(),
            y_stride: y_view.stride() as u32,
            u_plane: u_view.as_slice(),
            u_stride: u_view.stride() as u32,
            v_plane: v_view.as_slice(),
            v_stride: v_view.stride() as u32,
            width,
            height,
        };

        let mut out = vec![Rgb::<u16> { r: 0, g: 0, b: 0 }; pixel_count];
        let rgb_stride = width * 3;

        match (layout, bit_depth) {
            (PixelLayout::I420, 10) => yuv::i010_to_rgb10_bilinear(
                &planar,
                rgb::bytemuck::cast_slice_mut(out.as_mut_slice()),
                rgb_stride,
                yuv_range,
                matrix,
            ),
            (PixelLayout::I420, 12) => yuv::i012_to_rgb12_bilinear(
                &planar,
                rgb::bytemuck::cast_slice_mut(out.as_mut_slice()),
                rgb_stride,
                yuv_range,
                matrix,
            ),
            (PixelLayout::I420, _) => yuv::i016_to_rgb16_bilinear(
                &planar,
                rgb::bytemuck::cast_slice_mut(out.as_mut_slice()),
                rgb_stride,
                yuv_range,
                matrix,
            ),
            (PixelLayout::I422, 10) => yuv::i210_to_rgb10(
                &planar,
                rgb::bytemuck::cast_slice_mut(out.as_mut_slice()),
                rgb_stride,
                yuv_range,
                matrix,
            ),
            (PixelLayout::I422, 12) => yuv::i212_to_rgb12(
                &planar,
                rgb::bytemuck::cast_slice_mut(out.as_mut_slice()),
                rgb_stride,
                yuv_range,
                matrix,
            ),
            (PixelLayout::I422, _) => yuv::i216_to_rgb16(
                &planar,
                rgb::bytemuck::cast_slice_mut(out.as_mut_slice()),
                rgb_stride,
                yuv_range,
                matrix,
            ),
            (PixelLayout::I444, 10) => yuv::i410_to_rgb10(
                &planar,
                rgb::bytemuck::cast_slice_mut(out.as_mut_slice()),
                rgb_stride,
                yuv_range,
                matrix,
            ),
            (PixelLayout::I444, 12) => yuv::i412_to_rgb12(
                &planar,
                rgb::bytemuck::cast_slice_mut(out.as_mut_slice()),
                rgb_stride,
                yuv_range,
                matrix,
            ),
            (PixelLayout::I444, _) => yuv::i416_to_rgb16(
                &planar,
                rgb::bytemuck::cast_slice_mut(out.as_mut_slice()),
                rgb_stride,
                yuv_range,
                matrix,
            ),
            (PixelLayout::I400, _) => unreachable!("monochrome handled separately"),
        }
        .map_err(|e| at!(Error::ColorConversion(e)))?;

        // Scale 10/12/16-bit RGB down to 8-bit
        let shift = bit_depth.saturating_sub(8);
        let mut bytes = Vec::with_capacity(pixel_count * 3);
        for px in &out {
            bytes.push((px.r >> shift).min(255) as u8);
            bytes.push((px.g >> shift).min(255) as u8);
            bytes.push((px.b >> shift).min(255) as u8);
        }
        Ok((bytes, width, height, 3))
    }
}

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

    #[test]
    fn empty_data_returns_error() {
        let result = decode_av1_obu(&[]);
        assert!(result.is_err());
    }

    #[test]
    fn invalid_data_returns_error() {
        let result = decode_av1_obu(&[0x00, 0x01, 0x02, 0x03]);
        assert!(result.is_err());
    }

    #[test]
    fn truncated_obu_returns_error() {
        // A valid OBU header byte (temporal delimiter) but truncated
        let result = decode_av1_obu(&[0x12, 0x00]);
        assert!(result.is_err());
    }

    #[test]
    fn random_bytes_return_error() {
        let garbage: Vec<u8> = (0..256).map(|i| (i * 37 + 13) as u8).collect();
        let result = decode_av1_obu(&garbage);
        assert!(result.is_err());
    }

    /// Extract the gain map AV1 data from a test AVIF file and decode it
    /// using `decode_av1_obu`. This exercises the real use case: AVIF gain
    /// maps are stored as raw AV1 bitstreams.
    #[test]
    fn decode_gain_map_from_avif_test_file() {
        let avif_path = "tests/vectors/libavif/seine_sdr_gainmap_srgb.avif";
        let avif_data = match std::fs::read(avif_path) {
            Ok(data) => data,
            Err(_) => {
                eprintln!("skipping: test vector not found at {avif_path}");
                return;
            }
        };

        // Parse the AVIF to extract the gain map AV1 data
        let config = crate::config::DecoderConfig::default();
        let decoder = crate::decoder_managed::ManagedAvifDecoder::new(&avif_data, &config)
            .expect("should parse AVIF");
        let info = decoder.probe_info().expect("should probe");
        let gm = info.gain_map.expect("seine test file should have gain map");
        let av1_data = &gm.gain_map_data;
        assert!(
            !av1_data.is_empty(),
            "gain map AV1 data should be non-empty"
        );

        // Decode the raw AV1 OBU data
        let (pixels, width, height, channels) =
            decode_av1_obu(av1_data).expect("should decode gain map AV1 data");

        assert!(width > 0, "decoded width should be positive");
        assert!(height > 0, "decoded height should be positive");
        assert!(
            channels == 1 || channels == 3,
            "channels should be 1 (gray) or 3 (RGB), got {channels}"
        );

        let expected_len = width as usize * height as usize * channels as usize;
        assert_eq!(
            pixels.len(),
            expected_len,
            "pixel data length should match width*height*channels: {width}x{height}x{channels} = {expected_len}, got {}",
            pixels.len()
        );

        // Verify pixel values are not all zero (actual image content)
        let nonzero_count = pixels.iter().filter(|&&p| p != 0).count();
        assert!(
            nonzero_count > 0,
            "decoded gain map should have non-zero pixel values"
        );
    }
}