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
// Copyright 2022 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use crate::avcodec::AvError;
use crate::avcodec::AvPixelFormat;
use crate::ffi;

pub const AV_NOPTS_VALUE: u64 = 0x8000000000000000;
const MAX_FFMPEG_PLANES: usize = 4;

/// Get the maximum data alignment that may be required by FFmpeg.
/// This could change depending on FFmpeg's build configuration (AVX etc.).
pub fn max_buffer_alignment() -> usize {
    // SAFETY:
    // Safe because this function has no side effects and just returns an integer.
    unsafe { ffi::av_cpu_max_align() }
}

// See AvPixelFormat::line_size.
pub(crate) fn av_image_line_size(
    format: AvPixelFormat,
    width: u32,
    plane: usize,
) -> Result<usize, AvError> {
    // SAFETY:
    // Safe because format is a valid format and this function is pure computation.
    match unsafe { ffi::av_image_get_linesize(format.pix_fmt(), width as _, plane as _) } {
        i if i >= 0 => Ok(i as _),
        err => Err(AvError(err)),
    }
}

// See AvPixelFormat::plane_sizes.
pub(crate) fn av_image_plane_sizes<I: IntoIterator<Item = u32>>(
    format: AvPixelFormat,
    linesizes: I,
    height: u32,
) -> Result<Vec<usize>, AvError> {
    let mut linesizes_buf = [0; MAX_FFMPEG_PLANES];
    let mut planes = 0;
    for (i, linesize) in linesizes.into_iter().take(MAX_FFMPEG_PLANES).enumerate() {
        linesizes_buf[i] = linesize as _;
        planes += 1;
    }
    let mut plane_sizes_buf = [0; MAX_FFMPEG_PLANES];
    // SAFETY:
    // Safe because plane_sizes_buf and linesizes_buf have the size specified by the API, format is
    // valid, and this function doesn't have any side effects other than writing to plane_sizes_buf.
    AvError::result(unsafe {
        ffi::av_image_fill_plane_sizes(
            plane_sizes_buf.as_mut_ptr(),
            format.pix_fmt(),
            height as _,
            linesizes_buf.as_ptr(),
        )
    })?;
    Ok(plane_sizes_buf
        .into_iter()
        .map(|x| x as _)
        .take(planes)
        .collect())
}