1 // Copyright 2022 The ChromiumOS Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 use crate::avcodec::AvError;
6 use crate::avcodec::AvPixelFormat;
7 use crate::ffi;
8
9 pub const AV_NOPTS_VALUE: u64 = 0x8000000000000000;
10 const MAX_FFMPEG_PLANES: usize = 4;
11
12 /// Get the maximum data alignment that may be required by FFmpeg.
13 /// This could change depending on FFmpeg's build configuration (AVX etc.).
max_buffer_alignment() -> usize14 pub fn max_buffer_alignment() -> usize {
15 // Safe because this function has no side effects and just returns an integer.
16 unsafe { ffi::av_cpu_max_align() as usize }
17 }
18
19 // See AvPixelFormat::line_size.
av_image_line_size( format: AvPixelFormat, width: u32, plane: usize, ) -> Result<usize, AvError>20 pub(crate) fn av_image_line_size(
21 format: AvPixelFormat,
22 width: u32,
23 plane: usize,
24 ) -> Result<usize, AvError> {
25 // Safe because format is a valid format and this function is pure computation.
26 match unsafe { ffi::av_image_get_linesize(format.pix_fmt(), width as _, plane as _) } {
27 i if i >= 0 => Ok(i as _),
28 err => Err(AvError(err)),
29 }
30 }
31
32 // See AvPixelFormat::plane_sizes.
av_image_plane_sizes<I: IntoIterator<Item = u32>>( format: AvPixelFormat, linesizes: I, height: u32, ) -> Result<Vec<usize>, AvError>33 pub(crate) fn av_image_plane_sizes<I: IntoIterator<Item = u32>>(
34 format: AvPixelFormat,
35 linesizes: I,
36 height: u32,
37 ) -> Result<Vec<usize>, AvError> {
38 let mut linesizes_buf = [0; MAX_FFMPEG_PLANES];
39 let mut planes = 0;
40 for (i, linesize) in linesizes.into_iter().take(MAX_FFMPEG_PLANES).enumerate() {
41 linesizes_buf[i] = linesize as _;
42 planes += 1;
43 }
44 let mut plane_sizes_buf = [0; MAX_FFMPEG_PLANES];
45 // Safe because plane_sizes_buf and linesizes_buf have the size specified by the API, format is
46 // valid, and this function doesn't have any side effects other than writing to plane_sizes_buf.
47 AvError::result(unsafe {
48 ffi::av_image_fill_plane_sizes(
49 plane_sizes_buf.as_mut_ptr(),
50 format.pix_fmt(),
51 height as _,
52 linesizes_buf.as_ptr(),
53 )
54 })?;
55 Ok(plane_sizes_buf
56 .into_iter()
57 .map(|x| x as _)
58 .take(planes)
59 .collect())
60 }
61