1use crate::{Error, InternalInterface, Kind};
2use std::ops::{Deref, DerefMut};
3use vpx_sys as ffi;
4
5#[derive(Debug)]
6pub struct Cfg(ffi::vpx_codec_enc_cfg_t);
7impl Default for Cfg {
8 fn default() -> Self {
9 let interface = Interface;
10 let mut cfg = ffi::vpx_codec_enc_cfg_t::default();
11 let err = unsafe { ffi::vpx_codec_enc_config_default(interface.iface(), std::ptr::from_mut(&mut cfg), 0) };
12 assert_eq!(err, ffi::VPX_CODEC_OK);
13 Self(cfg)
14 }
15}
16impl AsRef<ffi::vpx_codec_enc_cfg_t> for Cfg {
17 fn as_ref(&self) -> &ffi::vpx_codec_enc_cfg_t {
18 &self.0
19 }
20}
21impl AsMut<ffi::vpx_codec_enc_cfg_t> for Cfg {
22 fn as_mut(&mut self) -> &mut ffi::vpx_codec_enc_cfg_t {
23 &mut self.0
24 }
25}
26impl Deref for Cfg {
27 type Target = ffi::vpx_codec_enc_cfg_t;
28
29 fn deref(&self) -> &ffi::vpx_codec_enc_cfg_t {
30 &self.0
31 }
32}
33impl DerefMut for Cfg {
34 fn deref_mut(&mut self) -> &mut ffi::vpx_codec_enc_cfg_t {
35 &mut self.0
36 }
37}
38
39#[derive(Copy, Clone)]
40pub struct Interface;
41impl Default for Interface {
42 fn default() -> Self {
43 Self
44 }
45}
46impl crate::Interface for Interface {
47 type Context = Context;
48 type Cfg = Cfg;
49 fn kind(&self) -> Kind { Kind::Encoder }
50
51 fn create(&self, cfg: <Self as crate::Interface>::Cfg,
52 flags: ffi::vpx_codec_flags_t) ->
53 Result<<Self as crate::Interface>::Context, Error>
54 {
55 let mut ctx = ffi::vpx_codec_ctx_t::default();
56 let err = unsafe {
57 ffi::vpx_codec_enc_init_ver(std::ptr::from_mut(&mut ctx),
58 self.iface(),
59 std::ptr::from_ref(&cfg.0),
60 flags,
61 ffi::VPX_ENCODER_ABI_VERSION as i32)
62 };
63 if err == ffi::VPX_CODEC_OK {
64 Ok(Context(ctx))
65 } else {
66 Err(From::from(err))
67 }
68 }
69}
70impl InternalInterface for Interface {
71 fn iface(&self) -> *mut ffi::vpx_codec_iface_t {
72 &raw mut ffi::vpx_codec_vp9_cx_algo
73 }
74}
75
76pub struct Context(ffi::vpx_codec_ctx_t);
77unsafe impl Send for Context {}
78impl Drop for Context {
79 fn drop(&mut self) {
80 unsafe {
81 ffi::vpx_codec_destroy(std::ptr::from_mut(&mut self.0));
82 }
83 }
84}
85impl super::InternalEncoder for Context {
86 fn get_ref_ctx(&self) -> *const ffi::vpx_codec_ctx_t {
87 std::ptr::from_ref(&self.0)
88 }
89
90 fn get_mut_ctx(&mut self) -> *mut ffi::vpx_codec_ctx_t {
91 std::ptr::from_mut(&mut self.0)
92 }
93}
94impl super::Encoder for Context {
95 type Cfg = Cfg;
96}