ffmpeg_rs/codec/
context.rs

1use std::any::Any;
2use std::ptr;
3use std::rc::Rc;
4
5use super::decoder::Decoder;
6use super::encoder::Encoder;
7use super::{threading, Compliance, Debug, Flags, Id, Parameters};
8use ffi::*;
9use libc::c_int;
10use media;
11use {Codec, Error};
12
13pub struct Context {
14    ptr: *mut AVCodecContext,
15    owner: Option<Rc<dyn Any>>,
16}
17
18unsafe impl Send for Context {}
19
20impl Context {
21    pub unsafe fn wrap(ptr: *mut AVCodecContext, owner: Option<Rc<dyn Any>>) -> Self {
22        Context { ptr, owner }
23    }
24
25    pub unsafe fn as_ptr(&self) -> *const AVCodecContext {
26        self.ptr as *const _
27    }
28
29    pub unsafe fn as_mut_ptr(&mut self) -> *mut AVCodecContext {
30        self.ptr
31    }
32}
33
34impl Context {
35    pub fn new() -> Self {
36        unsafe {
37            Context {
38                ptr: avcodec_alloc_context3(ptr::null()),
39                owner: None,
40            }
41        }
42    }
43
44    pub fn from_parameters<P: Into<Parameters>>(parameters: P) -> Result<Self, Error> {
45        let parameters = parameters.into();
46        let mut context = Self::new();
47
48        unsafe {
49            match avcodec_parameters_to_context(context.as_mut_ptr(), parameters.as_ptr()) {
50                e if e < 0 => Err(Error::from(e)),
51                _ => Ok(context),
52            }
53        }
54    }
55
56    pub fn new_with_codec(codec: &Codec) -> Self {
57        unsafe {
58            Context {
59                ptr: avcodec_alloc_context3(codec.as_ptr()),
60                owner: None,
61            }
62        }
63    }
64
65    pub fn decoder(self) -> Decoder {
66        Decoder(self)
67    }
68
69    pub fn encoder(self) -> Encoder {
70        Encoder(self)
71    }
72
73    pub fn codec(&self) -> Option<Codec> {
74        unsafe {
75            if (*self.as_ptr()).codec.is_null() {
76                None
77            } else {
78                Some(Codec::wrap((*self.as_ptr()).codec as *mut _))
79            }
80        }
81    }
82
83    pub fn medium(&self) -> media::Type {
84        unsafe { media::Type::from((*self.as_ptr()).codec_type) }
85    }
86
87    pub fn set_flags(&mut self, value: Flags) {
88        unsafe {
89            (*self.as_mut_ptr()).flags = value.bits() as c_int;
90        }
91    }
92
93    pub fn id(&self) -> Id {
94        unsafe { Id::from((*self.as_ptr()).codec_id) }
95    }
96
97    pub fn compliance(&mut self, value: Compliance) {
98        unsafe {
99            (*self.as_mut_ptr()).strict_std_compliance = value.into();
100        }
101    }
102
103    pub fn debug(&mut self, value: Debug) {
104        unsafe {
105            (*self.as_mut_ptr()).debug = value.bits();
106        }
107    }
108
109    pub fn set_threading(&mut self, config: threading::Config) {
110        unsafe {
111            (*self.as_mut_ptr()).thread_type = config.kind.into();
112            (*self.as_mut_ptr()).thread_count = config.count as c_int;
113            (*self.as_mut_ptr()).thread_safe_callbacks = if config.safe { 1 } else { 0 };
114        }
115    }
116
117    pub fn threading(&self) -> threading::Config {
118        unsafe {
119            threading::Config {
120                kind: threading::Type::from((*self.as_ptr()).active_thread_type),
121                count: (*self.as_ptr()).thread_count as usize,
122                safe: (*self.as_ptr()).thread_safe_callbacks != 0,
123            }
124        }
125    }
126
127    pub fn set_parameters<P: Into<Parameters>>(&mut self, parameters: P) -> Result<(), Error> {
128        let parameters = parameters.into();
129
130        unsafe {
131            match avcodec_parameters_to_context(self.as_mut_ptr(), parameters.as_ptr()) {
132                e if e < 0 => Err(Error::from(e)),
133                _ => Ok(()),
134            }
135        }
136    }
137}
138
139impl Default for Context {
140    fn default() -> Self {
141        Self::new()
142    }
143}
144
145impl Drop for Context {
146    fn drop(&mut self) {
147        unsafe {
148            if self.owner.is_none() {
149                avcodec_free_context(&mut self.as_mut_ptr());
150            }
151        }
152    }
153}
154
155#[cfg(not(feature = "ffmpeg_5_0"))]
156impl Clone for Context {
157    fn clone(&self) -> Self {
158        let mut ctx = Context::new();
159        ctx.clone_from(self);
160
161        ctx
162    }
163
164    fn clone_from(&mut self, source: &Self) {
165        unsafe {
166            // Removed in ffmpeg >= 5.0.
167            avcodec_copy_context(self.as_mut_ptr(), source.as_ptr());
168        }
169    }
170}