Skip to main content

libav_ng/avcodec/
encoder_decoder.rs

1/// Structures `Encoder` and `Decoder` provided by this module are used to differ
2/// decoder and encoder in CodecContext, because using wrong mode cause SIGSEGV.use std::ops::{Deref, DerefMut};
3
4use std::ops::{Deref, DerefMut};
5
6use libav_sys_ng::{avcodec_receive_frame, avcodec_send_frame};
7
8use crate::{avcodec::CodecContext, avframe::Frame};
9
10pub struct Encoder {
11    pub(crate) ctx: CodecContext,
12}
13
14impl Encoder {
15    /// Send frame to codec
16    pub fn send_frame(&mut self, frame: &Frame) -> i32 {
17        unsafe { avcodec_send_frame(self._codec_ctx, frame.raw()) }
18    }
19}
20
21impl Deref for Encoder {
22    type Target = CodecContext;
23
24    fn deref(&self) -> &Self::Target {
25        &self.ctx
26    }
27}
28
29impl DerefMut for Encoder {
30    fn deref_mut(&mut self) -> &mut Self::Target {
31        &mut self.ctx
32    }
33}
34
35pub struct Decoder {
36    pub(crate) ctx: CodecContext,
37}
38
39impl Decoder {
40    pub fn receive_frame(&mut self, out: &mut Frame) -> i32 {
41        unsafe { avcodec_receive_frame(self._codec_ctx, out.raw_mut()) }
42    }
43}
44
45impl Deref for Decoder {
46    type Target = CodecContext;
47
48    fn deref(&self) -> &Self::Target {
49        &self.ctx
50    }
51}
52
53impl DerefMut for Decoder {
54    fn deref_mut(&mut self) -> &mut Self::Target {
55        &mut self.ctx
56    }
57}