Skip to main content

vpx/encoder/
mod.rs

1use super::{Error, Frame, Image};
2use std::io;
3use vpx_sys as ffi;
4
5pub mod vp9;
6
7pub const DL_REALTIME: u64 = 1;
8pub const DL_GOOD_QUALITY: u64 = 1_000_000;
9pub const DL_BEST_QUALITY: u64 = 0;
10
11#[derive(Copy, Clone, Eq, PartialEq)]
12#[derive(Default)]
13pub struct FrameFlags {
14    keyframe: bool,
15}
16
17#[doc(hidden)]
18impl From<FrameFlags> for ffi::vpx_enc_frame_flags_t {
19    fn from(val: FrameFlags) -> Self {
20        if val.keyframe { 1 } else { 0 }
21    }
22}
23
24impl FrameFlags {
25    #[must_use]
26    pub fn new() -> Self { Self::default() }
27
28    #[must_use]
29    pub fn keyframe(mut self, keyframe: bool) -> Self {
30        self.keyframe = keyframe;
31        self
32    }
33}
34
35pub trait Encoder: InternalEncoder
36where <Self as Encoder>::Cfg: AsRef<ffi::vpx_codec_enc_cfg_t>
37{
38    type Cfg;
39    fn set_cfg(&mut self, cfg: Self::Cfg) -> Result<(), Error> {
40        let res = unsafe {
41            ffi::vpx_codec_enc_config_set(self.get_mut_ctx(),
42                                          std::ptr::from_ref(cfg.as_ref()))
43        };
44        if res == ffi::VPX_CODEC_OK {
45            Ok(())
46        } else {
47            Err(Error::from(res))
48        }
49    }
50
51    /// `duration` must be non-zero.
52    fn encode(&mut self, image: &Image<'_>,
53              pts: ffi::vpx_codec_pts_t,
54              duration: u64,
55              flags: FrameFlags,
56              deadline: u64) -> Result<(), Error> {
57        let res = unsafe {
58            ffi::vpx_codec_encode(self.get_mut_ctx(),
59                                  std::ptr::from_ref(&image.0),
60                                  pts,
61                                  duration as _,
62                                  flags.into(),
63                                  deadline as _)
64        };
65        if res == ffi::VPX_CODEC_OK {
66            Ok(())
67        } else {
68            Err(Error::from(res))
69        }
70    }
71
72    /// Call once there are no more frames to encode.
73    fn flush(&mut self,
74             pts: ffi::vpx_codec_pts_t,
75             duration: u64,
76             flags: ffi::vpx_enc_frame_flags_t,
77             deadline: u64) -> Result<(), Error>
78    {
79        let res = unsafe {
80            ffi::vpx_codec_encode(self.get_mut_ctx(),
81                                  std::ptr::null(),
82                                  pts, duration as _,
83                                  flags, deadline as _)
84        };
85        if res == ffi::VPX_CODEC_OK {
86            Ok(())
87        } else {
88            Err(Error::from(res))
89        }
90    }
91
92    fn packets<T: PacketWriter>(&mut self, dest: &mut T) -> Result<(), io::Error> {
93        use std::slice::from_raw_parts;
94        let mut iter: ffi::vpx_codec_iter_t = std::ptr::null_mut();
95        unsafe {
96            loop {
97                let pkt = ffi::vpx_codec_get_cx_data(self.get_mut_ctx(), std::ptr::from_mut(&mut iter));
98                if pkt.is_null() {
99                    return Ok(());
100                }
101
102                let pkt: &ffi::vpx_codec_cx_pkt_t = &*pkt;
103                match pkt.kind {
104                    ffi::vpx_codec_cx_pkt_kind::VPX_CODEC_CX_FRAME_PKT => {
105                        let frame = Frame::from(&pkt.data.frame);
106                        dest.write_frame(&frame)?;
107                    },
108                    ffi::vpx_codec_cx_pkt_kind::VPX_CODEC_STATS_PKT | ffi::vpx_codec_cx_pkt_kind::VPX_CODEC_FPMB_STATS_PKT => {
109                        let buf = &pkt.data.twopass_stats;
110                        let buf = from_raw_parts(buf.buf as *const u8, buf.sz as usize);
111                        dest.write_two_pass_stats(buf)?;
112                    },
113                    ffi::vpx_codec_cx_pkt_kind::VPX_CODEC_PSNR_PKT => {
114                        let psnr = &pkt.data.psnr;
115                        dest.write_psnr(&psnr.samples, &psnr.sse, &psnr.psnr)?;
116                    },
117                    kind @ ffi::vpx_codec_cx_pkt_kind::VPX_CODEC_CUSTOM_PKT => {
118                        dest.write_custom(kind, &pkt.data)?;
119                    },
120                }
121            }
122        }
123    }
124}
125
126#[doc(hidden)]
127pub trait InternalEncoder {
128    fn get_ref_ctx(&self) -> *const ffi::vpx_codec_ctx_t;
129    fn get_mut_ctx(&mut self) -> *mut ffi::vpx_codec_ctx_t;
130}
131
132pub trait PacketWriter {
133    fn write_frame(&mut self, _frame: &Frame<'_>) -> Result<(), io::Error> { Ok(()) }
134    fn write_two_pass_stats(&mut self, _stats: &[u8]) -> Result<(), io::Error> { Ok(()) }
135    fn write_first_pass_mb_stats(&mut self, _stats: &[u8]) -> Result<(), io::Error> { Ok(()) }
136    fn write_psnr(&mut self, _samples: &[u32; 4], _sse: &[u64; 4],
137                  _psnr: &[f64; 4]) -> Result<(), io::Error> { Ok(()) }
138    fn write_custom(&mut self,
139                    _kind: ffi::vpx_codec_cx_pkt_kind,
140                    _data: &ffi::vpx_codec_cx_pkt__bindgen_ty_1) -> Result<(), io::Error> { Ok(()) }
141}