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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
// Take a look at the license at the top of the repository in the LICENSE file.

use std::{mem, ptr};

use glib::{prelude::*, translate::*};

#[cfg(feature = "v1_16")]
#[cfg_attr(docsrs, doc(cfg(feature = "v1_16")))]
use crate::VideoInterlaceMode;
use crate::{
    utils::HasStreamLock,
    video_codec_state::{InNegotiation, Readable, VideoCodecState, VideoCodecStateContext},
    VideoCodecFrame, VideoDecoder, VideoFormat,
};

extern "C" {
    fn _gst_video_decoder_error(
        dec: *mut ffi::GstVideoDecoder,
        weight: i32,
        domain: glib::ffi::GQuark,
        code: i32,
        txt: *mut libc::c_char,
        debug: *mut libc::c_char,
        file: *const libc::c_char,
        function: *const libc::c_char,
        line: i32,
    ) -> gst::ffi::GstFlowReturn;
}

mod sealed {
    pub trait Sealed {}
    impl<T: super::IsA<super::VideoDecoder>> Sealed for T {}
}

pub trait VideoDecoderExtManual: sealed::Sealed + IsA<VideoDecoder> + 'static {
    /// Helper function that allocates a buffer to hold a video frame for `self`'s
    /// current [`VideoCodecState`][crate::VideoCodecState]. Subclass should already have configured video
    /// state and set src pad caps.
    ///
    /// The buffer allocated here is owned by the frame and you should only
    /// keep references to the frame, not the buffer.
    /// ## `frame`
    /// a [`VideoCodecFrame`][crate::VideoCodecFrame]
    ///
    /// # Returns
    ///
    /// [`gst::FlowReturn::Ok`][crate::gst::FlowReturn::Ok] if an output buffer could be allocated
    #[doc(alias = "gst_video_decoder_allocate_output_frame")]
    fn allocate_output_frame(
        &self,
        frame: &mut VideoCodecFrame,
        params: Option<&gst::BufferPoolAcquireParams>,
    ) -> Result<gst::FlowSuccess, gst::FlowError> {
        unsafe {
            let params_ptr = params.to_glib_none().0 as *mut _;
            try_from_glib(ffi::gst_video_decoder_allocate_output_frame_with_params(
                self.as_ref().to_glib_none().0,
                frame.to_glib_none().0,
                params_ptr,
            ))
        }
    }

    /// Get a pending unfinished [`VideoCodecFrame`][crate::VideoCodecFrame]
    /// ## `frame_number`
    /// system_frame_number of a frame
    ///
    /// # Returns
    ///
    /// pending unfinished [`VideoCodecFrame`][crate::VideoCodecFrame] identified by `frame_number`.
    #[doc(alias = "get_frame")]
    #[doc(alias = "gst_video_decoder_get_frame")]
    fn frame(&self, frame_number: i32) -> Option<VideoCodecFrame> {
        let frame = unsafe {
            ffi::gst_video_decoder_get_frame(self.as_ref().to_glib_none().0, frame_number)
        };

        if frame.is_null() {
            None
        } else {
            unsafe { Some(VideoCodecFrame::new(frame, self.as_ref())) }
        }
    }

    /// Get all pending unfinished [`VideoCodecFrame`][crate::VideoCodecFrame]
    ///
    /// # Returns
    ///
    /// pending unfinished [`VideoCodecFrame`][crate::VideoCodecFrame].
    #[doc(alias = "get_frames")]
    #[doc(alias = "gst_video_decoder_get_frames")]
    fn frames(&self) -> Vec<VideoCodecFrame> {
        unsafe {
            let frames = ffi::gst_video_decoder_get_frames(self.as_ref().to_glib_none().0);
            let mut iter: *const glib::ffi::GList = frames;
            let mut vec = Vec::new();

            while !iter.is_null() {
                let frame_ptr = Ptr::from((*iter).data);
                /* transfer ownership of the frame */
                let frame = VideoCodecFrame::new(frame_ptr, self.as_ref());
                vec.push(frame);
                iter = (*iter).next;
            }

            glib::ffi::g_list_free(frames);
            vec
        }
    }

    /// Get the oldest pending unfinished [`VideoCodecFrame`][crate::VideoCodecFrame]
    ///
    /// # Returns
    ///
    /// oldest pending unfinished [`VideoCodecFrame`][crate::VideoCodecFrame].
    #[doc(alias = "get_oldest_frame")]
    #[doc(alias = "gst_video_decoder_get_oldest_frame")]
    fn oldest_frame(&self) -> Option<VideoCodecFrame> {
        let frame =
            unsafe { ffi::gst_video_decoder_get_oldest_frame(self.as_ref().to_glib_none().0) };

        if frame.is_null() {
            None
        } else {
            unsafe { Some(VideoCodecFrame::new(frame, self.as_ref())) }
        }
    }

    /// Lets [`VideoDecoder`][crate::VideoDecoder] sub-classes to know the memory `allocator`
    /// used by the base class and its `params`.
    ///
    /// Unref the `allocator` after use it.
    ///
    /// # Returns
    ///
    ///
    /// ## `allocator`
    /// the [`gst::Allocator`][crate::gst::Allocator]
    /// used
    ///
    /// ## `params`
    /// the
    /// [`gst::AllocationParams`][crate::gst::AllocationParams] of `allocator`
    #[doc(alias = "get_allocator")]
    #[doc(alias = "gst_video_decoder_get_allocator")]
    fn allocator(&self) -> (Option<gst::Allocator>, gst::AllocationParams) {
        unsafe {
            let mut allocator = ptr::null_mut();
            let mut params = mem::MaybeUninit::uninit();
            ffi::gst_video_decoder_get_allocator(
                self.as_ref().to_glib_none().0,
                &mut allocator,
                params.as_mut_ptr(),
            );
            (from_glib_full(allocator), params.assume_init().into())
        }
    }
    /// Query the configured decoder latency. Results will be returned via
    /// `min_latency` and `max_latency`.
    ///
    /// # Returns
    ///
    ///
    /// ## `min_latency`
    /// address of variable in which to store the
    ///  configured minimum latency, or [`None`]
    ///
    /// ## `max_latency`
    /// address of variable in which to store the
    ///  configured mximum latency, or [`None`]
    #[doc(alias = "get_latency")]
    #[doc(alias = "gst_video_decoder_get_latency")]
    fn latency(&self) -> (gst::ClockTime, Option<gst::ClockTime>) {
        let mut min_latency = gst::ffi::GST_CLOCK_TIME_NONE;
        let mut max_latency = gst::ffi::GST_CLOCK_TIME_NONE;

        unsafe {
            ffi::gst_video_decoder_get_latency(
                self.as_ref().to_glib_none().0,
                &mut min_latency,
                &mut max_latency,
            );

            (
                try_from_glib(min_latency).expect("undefined min_latency"),
                from_glib(max_latency),
            )
        }
    }

    /// Lets [`VideoDecoder`][crate::VideoDecoder] sub-classes tell the baseclass what the decoder latency
    /// is. If the provided values changed from previously provided ones, this will
    /// also post a LATENCY message on the bus so the pipeline can reconfigure its
    /// global latency.
    /// ## `min_latency`
    /// minimum latency
    /// ## `max_latency`
    /// maximum latency
    #[doc(alias = "gst_video_decoder_set_latency")]
    fn set_latency(
        &self,
        min_latency: gst::ClockTime,
        max_latency: impl Into<Option<gst::ClockTime>>,
    ) {
        unsafe {
            ffi::gst_video_decoder_set_latency(
                self.as_ref().to_glib_none().0,
                min_latency.into_glib(),
                max_latency.into().into_glib(),
            );
        }
    }

    /// Get the [`VideoCodecState`][crate::VideoCodecState] currently describing the output stream.
    ///
    /// # Returns
    ///
    /// [`VideoCodecState`][crate::VideoCodecState] describing format of video data.
    #[doc(alias = "get_output_state")]
    #[doc(alias = "gst_video_decoder_get_output_state")]
    fn output_state(&self) -> Option<VideoCodecState<'static, Readable>> {
        let state =
            unsafe { ffi::gst_video_decoder_get_output_state(self.as_ref().to_glib_none().0) };

        if state.is_null() {
            None
        } else {
            unsafe { Some(VideoCodecState::<Readable>::new(state)) }
        }
    }

    /// Creates a new [`VideoCodecState`][crate::VideoCodecState] with the specified `fmt`, `width` and `height`
    /// as the output state for the decoder.
    /// Any previously set output state on `self` will be replaced by the newly
    /// created one.
    ///
    /// If the subclass wishes to copy over existing fields (like pixel aspec ratio,
    /// or framerate) from an existing [`VideoCodecState`][crate::VideoCodecState], it can be provided as a
    /// `reference`.
    ///
    /// If the subclass wishes to override some fields from the output state (like
    /// pixel-aspect-ratio or framerate) it can do so on the returned [`VideoCodecState`][crate::VideoCodecState].
    ///
    /// The new output state will only take effect (set on pads and buffers) starting
    /// from the next call to [`VideoDecoderExt::finish_frame()`][crate::prelude::VideoDecoderExt::finish_frame()].
    /// ## `fmt`
    /// a [`VideoFormat`][crate::VideoFormat]
    /// ## `width`
    /// The width in pixels
    /// ## `height`
    /// The height in pixels
    /// ## `reference`
    /// An optional reference [`VideoCodecState`][crate::VideoCodecState]
    ///
    /// # Returns
    ///
    /// the newly configured output state.
    #[doc(alias = "gst_video_decoder_set_output_state")]
    fn set_output_state(
        &self,
        fmt: VideoFormat,
        width: u32,
        height: u32,
        reference: Option<&VideoCodecState<Readable>>,
    ) -> Result<VideoCodecState<InNegotiation>, gst::FlowError> {
        let state = unsafe {
            let reference = match reference {
                Some(reference) => reference.as_mut_ptr(),
                None => ptr::null_mut(),
            };
            ffi::gst_video_decoder_set_output_state(
                self.as_ref().to_glib_none().0,
                fmt.into_glib(),
                width,
                height,
                reference,
            )
        };

        if state.is_null() {
            Err(gst::FlowError::NotNegotiated)
        } else {
            unsafe { Ok(VideoCodecState::<InNegotiation>::new(state, self.as_ref())) }
        }
    }

    /// Same as [`set_output_state()`][Self::set_output_state()] but also allows you to also set
    /// the interlacing mode.
    /// ## `fmt`
    /// a [`VideoFormat`][crate::VideoFormat]
    /// ## `interlace_mode`
    /// A [`VideoInterlaceMode`][crate::VideoInterlaceMode]
    /// ## `width`
    /// The width in pixels
    /// ## `height`
    /// The height in pixels
    /// ## `reference`
    /// An optional reference [`VideoCodecState`][crate::VideoCodecState]
    ///
    /// # Returns
    ///
    /// the newly configured output state.
    #[cfg(feature = "v1_16")]
    #[cfg_attr(docsrs, doc(cfg(feature = "v1_16")))]
    #[doc(alias = "gst_video_decoder_set_interlaced_output_state")]
    fn set_interlaced_output_state(
        &self,
        fmt: VideoFormat,
        mode: VideoInterlaceMode,
        width: u32,
        height: u32,
        reference: Option<&VideoCodecState<Readable>>,
    ) -> Result<VideoCodecState<InNegotiation>, gst::FlowError> {
        let state = unsafe {
            let reference = match reference {
                Some(reference) => reference.as_mut_ptr(),
                None => ptr::null_mut(),
            };
            ffi::gst_video_decoder_set_interlaced_output_state(
                self.as_ref().to_glib_none().0,
                fmt.into_glib(),
                mode.into_glib(),
                width,
                height,
                reference,
            )
        };

        if state.is_null() {
            Err(gst::FlowError::NotNegotiated)
        } else {
            unsafe { Ok(VideoCodecState::<InNegotiation>::new(state, self.as_ref())) }
        }
    }

    /// Negotiate with downstream elements to currently configured [`VideoCodecState`][crate::VideoCodecState].
    /// Unmark GST_PAD_FLAG_NEED_RECONFIGURE in any case. But mark it again if
    /// negotiate fails.
    ///
    /// # Returns
    ///
    /// [`true`] if the negotiation succeeded, else [`false`].
    #[doc(alias = "gst_video_decoder_negotiate")]
    fn negotiate<'a>(
        &'a self,
        output_state: VideoCodecState<'a, InNegotiation<'a>>,
    ) -> Result<(), gst::FlowError> {
        // Consume output_state so user won't be able to modify it anymore
        let self_ptr = self.to_glib_none().0 as *const gst::ffi::GstElement;
        assert_eq!(output_state.context.element_as_ptr(), self_ptr);

        let ret = unsafe {
            from_glib(ffi::gst_video_decoder_negotiate(
                self.as_ref().to_glib_none().0,
            ))
        };
        if ret {
            Ok(())
        } else {
            Err(gst::FlowError::NotNegotiated)
        }
    }

    #[allow(clippy::too_many_arguments)]
    fn error<T: gst::MessageErrorDomain>(
        &self,
        weight: i32,
        code: T,
        message: Option<&str>,
        debug: Option<&str>,
        file: &str,
        function: &str,
        line: u32,
    ) -> Result<gst::FlowSuccess, gst::FlowError> {
        unsafe {
            try_from_glib(_gst_video_decoder_error(
                self.as_ref().to_glib_none().0,
                weight,
                T::domain().into_glib(),
                code.code(),
                message.to_glib_full(),
                debug.to_glib_full(),
                file.to_glib_none().0,
                function.to_glib_none().0,
                line as i32,
            ))
        }
    }

    fn sink_pad(&self) -> &gst::Pad {
        unsafe {
            let elt = &*(self.as_ptr() as *const ffi::GstVideoDecoder);
            &*(&elt.sinkpad as *const *mut gst::ffi::GstPad as *const gst::Pad)
        }
    }

    fn src_pad(&self) -> &gst::Pad {
        unsafe {
            let elt = &*(self.as_ptr() as *const ffi::GstVideoDecoder);
            &*(&elt.srcpad as *const *mut gst::ffi::GstPad as *const gst::Pad)
        }
    }

    fn input_segment(&self) -> gst::Segment {
        unsafe {
            let ptr: &ffi::GstVideoDecoder = &*(self.as_ptr() as *const _);
            glib::ffi::g_rec_mutex_lock(mut_override(&ptr.stream_lock));
            let segment = ptr.input_segment;
            glib::ffi::g_rec_mutex_unlock(mut_override(&ptr.stream_lock));
            from_glib_none(&segment as *const gst::ffi::GstSegment)
        }
    }

    fn output_segment(&self) -> gst::Segment {
        unsafe {
            let ptr: &ffi::GstVideoDecoder = &*(self.as_ptr() as *const _);
            glib::ffi::g_rec_mutex_lock(mut_override(&ptr.stream_lock));
            let segment = ptr.output_segment;
            glib::ffi::g_rec_mutex_unlock(mut_override(&ptr.stream_lock));
            from_glib_none(&segment as *const gst::ffi::GstSegment)
        }
    }
}

impl<O: IsA<VideoDecoder>> VideoDecoderExtManual for O {}

impl HasStreamLock for VideoDecoder {
    fn stream_lock(&self) -> *mut glib::ffi::GRecMutex {
        let decoder_sys: *const ffi::GstVideoDecoder = self.to_glib_none().0;
        unsafe { mut_override(&(*decoder_sys).stream_lock) }
    }

    fn element_as_ptr(&self) -> *const gst::ffi::GstElement {
        self.as_ptr() as *mut gst::ffi::GstElement
    }
}

#[macro_export]
macro_rules! video_decoder_error(
    ($obj:expr, $weight:expr, $err:expr, ($($msg:tt)*), [$($debug:tt)*]) => { {
        use $crate::prelude::VideoDecoderExtManual;
        $obj.error(
            $weight,
            $err,
            Some(&format!($($msg)*)),
            Some(&format!($($debug)*)),
            file!(),
            $crate::glib::function_name!(),
            line!(),
        )
    }};
    ($obj:expr, $weight:expr, $err:expr, ($($msg:tt)*)) => { {
        use $crate::prelude::VideoDecoderExtManual;
        $obj.error(
            $weight,
            $err,
            Some(&format!($($msg)*)),
            None,
            file!(),
            $crate::glib::function_name!(),
            line!(),
        )
    }};
    ($obj:expr, $weight:expr, $err:expr, [$($debug:tt)*]) => { {
        use $crate::prelude::VideoDecoderExtManual;
        $obj.error(
            $weight,
            $err,
            None,
            Some(&format!($($debug)*)),
            file!(),
            $crate::glib::function_name!(),
            line!(),
        )
    }};
);