1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/// This module represents (almost) safe binding to AVCodecContext
use libav_sys_ng::{
    avcodec_alloc_context3, avcodec_find_encoder, avcodec_free_context, avcodec_is_open, avcodec_open2, avcodec_parameters_alloc, avcodec_parameters_copy, avcodec_parameters_free, avcodec_parameters_from_context, avcodec_parameters_to_context, avcodec_receive_packet, avcodec_send_frame, AVCodec, AVCodecContext, AVCodecID, AVCodecParameters, AVDictionary, AVPixelFormat, AVRational
};

use crate::{avdictionary::Dictionary, avframe, avstream::Stream};

/// AVCodecContext wrapper
pub struct CodecContext {
    _codec: *const AVCodec,
    _codec_ctx: *mut AVCodecContext,
}

impl CodecContext {
    /// Creates CodecContext from encoder ID
    ///
    /// Returns Some(CodecContext) on success, None on error.
    pub fn from_encoder_id(id: AVCodecID) -> Option<CodecContext> {
        unsafe {
            let codec = avcodec_find_encoder(id);

            if codec == core::ptr::null() {
                return None;
            }

            let codec_ctx = avcodec_alloc_context3(codec);

            if codec_ctx == core::ptr::null_mut() {
                return None;
            }

            Some(CodecContext {
                _codec: codec,
                _codec_ctx: codec_ctx,
            })
        }
    }

    /// Opens a codec
    pub fn open(&mut self, options: Option<&mut Dictionary>) -> Result<(), i32> {
        let raw_options = match options {
            Some(opt) => (unsafe { &mut opt.raw() }) as *mut *mut AVDictionary,
            None => core::ptr::null_mut(),
        };
        let code = unsafe { avcodec_open2(self._codec_ctx, self._codec, raw_options) };

        if code < 0 {
            return Err(code);
        }

        Ok(())
    }

    /// Set size of codec picture size
    pub fn set_size(&mut self, width: i32, height: i32) -> &mut Self {
        unsafe {
            (*self._codec_ctx).width = width;
            (*self._codec_ctx).height = height;
        }

        self
    }

    /// Get size of codec picture size
    pub fn get_size(&self) -> (i32, i32) {
        unsafe { ((*self._codec_ctx).width, (*self._codec_ctx).height) }
    }

    /// Set bitrate
    pub fn set_bitrate(&mut self, bitrate: i64) -> &mut CodecContext {
        unsafe {
            (*self._codec_ctx).bit_rate = bitrate;
        }

        self
    }

    /// Get bitrate
    pub fn get_bitrate(&self) -> i64 {
        unsafe {
            return (*self._codec_ctx).bit_rate;
        }
    }

    /// Set framerate (this also sets `time_base` but in inverse order)
    pub fn set_framerate(&mut self, fps: i32) -> &mut CodecContext {
        unsafe {
            (*self._codec_ctx).time_base.num = 1;
            (*self._codec_ctx).time_base.den = fps;

            (*self._codec_ctx).framerate.num = fps;
            (*self._codec_ctx).framerate.den = 1;
        }

        self
    }

    /// Get framerate
    pub fn get_framerate(&self) -> AVRational {
        unsafe {
            return (*self._codec_ctx).framerate;
        }
    }

    /// Get time base
    pub fn get_time_base(&self) -> AVRational {
        unsafe {
            return (*self._codec_ctx).time_base;
        }
    }

    /// Set pixel format
    pub fn set_pixel_format(&mut self, fmt: AVPixelFormat) -> &mut CodecContext {
        unsafe {
            (*self._codec_ctx).pix_fmt = fmt;
        }

        self
    }

    /// Get pixel format
    pub fn get_pixel_format(&self) -> AVPixelFormat {
        unsafe {
            return (*self._codec_ctx).pix_fmt;
        }
    }

    pub fn set_gop_size(&mut self, gop_size: i32) -> &mut CodecContext {
        unsafe {
            (*self._codec_ctx).gop_size = gop_size;
        }

        self
    }

    pub fn get_gop_size(&self) -> i32 {
        unsafe {
            return (*self._codec_ctx).gop_size;
        }
    }

    pub fn set_max_b_frames(&mut self, max_b_frames: i32) -> &mut CodecContext {
        unsafe {
            (*self._codec_ctx).max_b_frames = max_b_frames;
        }

        self
    }

    /// Fills parameters from codec into `params`
    pub fn fill_parameters(&self, params: &mut CodecParameters) {
        unsafe {
            avcodec_parameters_from_context(params._par, self._codec_ctx);
        }
    }

    /// Fills codec parameters from `params`
    pub fn fill_from_parameters(&self, params: &CodecParameters) {
        unsafe {
            avcodec_parameters_to_context(self._codec_ctx, params._par);
        }
    }

    pub fn fill_stream_parameters(&self, stream: &mut Stream) {
        unsafe {
            avcodec_parameters_from_context((*stream.raw()).codecpar, self._codec_ctx);
        }
    }

    /// Set codec flags
    pub fn set_flags(&mut self, flags: i32) -> &mut CodecContext {
        unsafe {
            (*self._codec_ctx).flags = flags;
        }

        self
    }

    /// Get codec flags
    pub fn get_flags(&self) -> i32 {
        unsafe {
            return (*self._codec_ctx).flags;
        }
    }

    /// Send frame to codec
    pub fn send_frame(&mut self, frame: &mut avframe::Frame) -> i32 {
        unsafe {
            return avcodec_send_frame(self._codec_ctx, frame.raw());
        }
    }

    // TODO: Make this function return Packet when it gets implemented.
    /// Receive packet from codec to `out`
    pub unsafe fn receive_packet(&mut self, out: *mut libav_sys_ng::AVPacket) -> i32 {
        return avcodec_receive_packet(self._codec_ctx, out);
    }

    /// Returns true if codec is opened.
    pub fn is_open(&self) -> bool {
        unsafe {
            return avcodec_is_open(self._codec_ctx) != 0;
        }
    }
}

impl Drop for CodecContext {
    /// Frees context on drop
    fn drop(&mut self) {
        unsafe {
            avcodec_free_context(&mut self._codec_ctx);
        }
    }
}



pub struct CodecParameters {
    pub(crate) _par: *mut AVCodecParameters
}

impl CodecParameters {
    pub fn new() -> Option<CodecParameters> {
        unsafe {
            let raw = avcodec_parameters_alloc();
        
            if raw.is_null() {
                return None;
            }

            Some(CodecParameters { _par: raw })
        }
    }
}

impl Clone for CodecParameters {
    fn clone(&self) -> Self {
        let parameters = CodecParameters::new().expect("Failed to allocate AVCodecParameters");

        unsafe { avcodec_parameters_copy(parameters._par, self._par) };

        parameters
    }
}

impl Drop for CodecParameters {
    fn drop(&mut self) {
        unsafe {
            avcodec_parameters_free(&mut self._par)
        }
    }
}