ffmpeg_the_third/codec/parameters/
owned.rs

1use std::ptr::NonNull;
2
3use crate::codec::Context;
4use crate::ffi::*;
5use crate::{AsMutPtr, AsPtr};
6
7pub struct Parameters {
8    ptr: NonNull<AVCodecParameters>,
9}
10
11unsafe impl Send for Parameters {}
12
13impl Parameters {
14    /// # Safety
15    ///
16    /// Ensure that
17    /// - it is valid for the returned struct to take ownership of the [`AVCodecParameters`]
18    ///   and that
19    /// - `ptr` is not used to break Rust's ownership rules after calling this function.
20    pub unsafe fn from_raw(ptr: *mut AVCodecParameters) -> Option<Self> {
21        NonNull::new(ptr).map(|ptr| Self { ptr })
22    }
23
24    /// Exposes a pointer to the contained [`AVCodecParameters`] for FFI purposes.
25    ///
26    /// This is guaranteed to be a non-null pointer.
27    pub fn as_ptr(&self) -> *const AVCodecParameters {
28        self.ptr.as_ptr()
29    }
30
31    /// Exposes a mutable pointer to the contained [`AVCodecParameters`] for FFI purposes.
32    ///
33    /// This is guaranteed to be a non-null pointer.
34    pub fn as_mut_ptr(&mut self) -> *mut AVCodecParameters {
35        self.ptr.as_ptr()
36    }
37}
38
39impl Parameters {
40    /// Allocates a new set of codec parameters set to default values.
41    pub fn new() -> Self {
42        let ptr = unsafe { avcodec_parameters_alloc() };
43
44        Self {
45            ptr: NonNull::new(ptr).expect("can allocate AVCodecParameters"),
46        }
47    }
48}
49
50impl Default for Parameters {
51    fn default() -> Self {
52        Self::new()
53    }
54}
55
56impl Drop for Parameters {
57    fn drop(&mut self) {
58        unsafe {
59            avcodec_parameters_free(&mut self.as_mut_ptr());
60        }
61    }
62}
63
64impl Clone for Parameters {
65    fn clone(&self) -> Self {
66        let mut ctx = Parameters::new();
67        ctx.clone_from(self);
68
69        ctx
70    }
71
72    fn clone_from(&mut self, source: &Self) {
73        unsafe {
74            avcodec_parameters_copy(self.as_mut_ptr(), source.as_ptr());
75        }
76    }
77}
78
79impl AsPtr<AVCodecParameters> for Parameters {
80    fn as_ptr(&self) -> *const AVCodecParameters {
81        self.as_ptr()
82    }
83}
84
85impl AsMutPtr<AVCodecParameters> for Parameters {
86    fn as_mut_ptr(&mut self) -> *mut AVCodecParameters {
87        self.as_mut_ptr()
88    }
89}
90
91impl<C: AsRef<Context>> From<C> for Parameters {
92    fn from(context: C) -> Parameters {
93        let mut parameters = Parameters::new();
94        let context = context.as_ref();
95        unsafe {
96            avcodec_parameters_from_context(parameters.as_mut_ptr(), context.as_ptr());
97        }
98        parameters
99    }
100}