Welcome to mirror list, hosted at ThFree Co, Russian Federation.

imp.rs « ffv1dec « src « ffv1 « video - gitlab.freedesktop.org/gstreamer/gst-plugins-rs.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: da359954f3dc91fa10bee38896b2fce026ee1c49 (plain)
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
477
478
// Copyright (C) 2020 Arun Raghavan <arun@asymptotic.io>
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//
// SPDX-License-Identifier: MIT/Apache-2.0

use ffv1::constants::{RGB, YCBCR};
use ffv1::decoder::{Decoder, Frame};
use ffv1::record::ConfigRecord;

use gst::glib;
use gst::prelude::*;
use gst::subclass::prelude::*;
use gst_video::prelude::*;
use gst_video::subclass::prelude::*;
use gst_video::VideoFormat;

use once_cell::sync::Lazy;
use std::sync::Mutex;

enum DecoderState {
    Stopped,
    Started {
        output_info: Option<gst_video::VideoInfo>,
        decoder: Box<Decoder>,
        video_meta_supported: bool,
    },
}

impl Default for DecoderState {
    fn default() -> Self {
        DecoderState::Stopped
    }
}

#[derive(Default)]
pub struct Ffv1Dec {
    state: Mutex<DecoderState>,
}

fn get_all_video_formats() -> Vec<glib::SendValue> {
    let values = [
        VideoFormat::Gray8,
        VideoFormat::Gray16Le,
        VideoFormat::Gray16Be,
        VideoFormat::Y444,
        VideoFormat::Y44410le,
        VideoFormat::Y44410be,
        VideoFormat::A44410le,
        VideoFormat::A44410be,
        VideoFormat::Y44412le,
        VideoFormat::Y44412be,
        VideoFormat::Y44416le,
        VideoFormat::Y44416be,
        VideoFormat::A420,
        VideoFormat::Y42b,
        VideoFormat::I42210le,
        VideoFormat::I42210be,
        VideoFormat::A42210le,
        VideoFormat::A42210be,
        VideoFormat::I42212le,
        VideoFormat::I42212be,
        VideoFormat::I420,
        VideoFormat::I42010le,
        VideoFormat::I42010be,
        VideoFormat::I42012le,
        VideoFormat::I42012be,
        VideoFormat::Gbra,
        VideoFormat::Gbr,
        VideoFormat::Gbr10le,
        VideoFormat::Gbr10be,
        VideoFormat::Gbra10le,
        VideoFormat::Gbra10be,
        VideoFormat::Gbr12le,
        VideoFormat::Gbr12be,
        VideoFormat::Gbra12le,
        VideoFormat::Gbra12be,
        VideoFormat::Y41b,
        VideoFormat::Yuv9,
    ];

    values.iter().map(|i| i.to_str().to_send_value()).collect()
}

fn get_output_format(record: &ConfigRecord) -> Option<VideoFormat> {
    const IS_LITTLE_ENDIAN: bool = cfg!(target_endian = "little");

    match record.colorspace_type as usize {
        YCBCR => match (
            record.chroma_planes,
            record.log2_v_chroma_subsample,
            record.log2_h_chroma_subsample,
            record.bits_per_raw_sample,
            record.extra_plane,
            IS_LITTLE_ENDIAN,
        ) {
            // Interpret luma-only as grayscale
            (false, _, _, 8, false, _) => Some(VideoFormat::Gray8),
            (false, _, _, 16, false, true) => Some(VideoFormat::Gray16Le),
            (false, _, _, 16, false, false) => Some(VideoFormat::Gray16Be),
            // 4:4:4
            (true, 0, 0, 8, false, _) => Some(VideoFormat::Y444),
            (true, 0, 0, 10, false, true) => Some(VideoFormat::Y44410le),
            (true, 0, 0, 10, false, false) => Some(VideoFormat::Y44410be),
            (true, 0, 0, 10, true, true) => Some(VideoFormat::A44410le),
            (true, 0, 0, 10, true, false) => Some(VideoFormat::A44410be),
            (true, 0, 0, 12, false, true) => Some(VideoFormat::Y44412le),
            (true, 0, 0, 12, false, false) => Some(VideoFormat::Y44412be),
            (true, 0, 0, 16, false, true) => Some(VideoFormat::Y44416le),
            (true, 0, 0, 16, false, false) => Some(VideoFormat::Y44416be),
            // 4:2:2
            (true, 0, 1, 8, false, _) => Some(VideoFormat::Y42b),
            (true, 0, 1, 10, false, true) => Some(VideoFormat::I42210le),
            (true, 0, 1, 10, false, false) => Some(VideoFormat::I42210be),
            (true, 0, 1, 10, true, true) => Some(VideoFormat::A42210le),
            (true, 0, 1, 10, true, false) => Some(VideoFormat::A42210be),
            (true, 0, 1, 12, false, true) => Some(VideoFormat::I42212le),
            (true, 0, 1, 12, false, false) => Some(VideoFormat::I42212be),
            // 4:1:1
            (true, 0, 2, 8, false, _) => Some(VideoFormat::Y41b),
            // 4:2:0
            (true, 1, 1, 8, false, _) => Some(VideoFormat::I420),
            (true, 1, 1, 8, true, _) => Some(VideoFormat::A420),
            (true, 1, 1, 10, false, true) => Some(VideoFormat::I42010le),
            (true, 1, 1, 10, false, false) => Some(VideoFormat::I42010be),
            (true, 1, 1, 12, false, true) => Some(VideoFormat::I42012le),
            (true, 1, 1, 12, false, false) => Some(VideoFormat::I42012be),
            // 4:1:0
            (true, 2, 2, 8, false, _) => Some(VideoFormat::Yuv9),
            // Nothing matched
            (_, _, _, _, _, _) => None,
        },
        RGB => match (
            record.bits_per_raw_sample,
            record.extra_plane,
            IS_LITTLE_ENDIAN,
        ) {
            (8, true, _) => Some(VideoFormat::Gbra),
            (8, false, _) => Some(VideoFormat::Gbr),
            (10, false, true) => Some(VideoFormat::Gbr10le),
            (10, false, false) => Some(VideoFormat::Gbr10be),
            (10, true, true) => Some(VideoFormat::Gbra10le),
            (10, true, false) => Some(VideoFormat::Gbra10be),
            (12, false, true) => Some(VideoFormat::Gbr12le),
            (12, false, false) => Some(VideoFormat::Gbr12be),
            (12, true, true) => Some(VideoFormat::Gbra12le),
            (12, true, false) => Some(VideoFormat::Gbra12be),
            // Nothing matched
            (_, _, _) => None,
        },
        _ => panic!("Unknown color_space type"),
    }
}

impl Ffv1Dec {
    pub fn get_decoded_frame(
        &self,
        mut decoded_frame: Frame,
        output_info: &gst_video::VideoInfo,
        video_meta_supported: bool,
    ) -> gst::Buffer {
        let mut buf = gst::Buffer::new();
        let mut_buf = buf.make_mut();
        let format_info = output_info.format_info();

        let mut offsets = vec![];
        let mut strides = vec![];
        let mut acc_offset = 0;

        if decoded_frame.bit_depth == 8 {
            for (plane, decoded_plane) in decoded_frame.buf.drain(..).enumerate() {
                let component = format_info
                    .plane()
                    .iter()
                    .position(|&p| p == plane as u32)
                    .unwrap() as u8;

                let comp_height =
                    format_info.scale_height(component, output_info.height()) as usize;
                let src_stride = decoded_plane.len() / comp_height;
                let dest_stride = output_info.stride()[plane] as usize;

                let mem = if video_meta_supported || src_stride == dest_stride {
                    // Just wrap the decoded frame vecs and push them out
                    gst::Memory::from_mut_slice(decoded_plane)
                } else {
                    // Mismatched stride, let's copy
                    let out_plane = gst::Memory::with_size(dest_stride * comp_height);
                    let mut out_plane_mut = out_plane.into_mapped_memory_writable().unwrap();

                    for (in_line, out_line) in decoded_plane
                        .as_slice()
                        .chunks_exact(src_stride)
                        .zip(out_plane_mut.as_mut_slice().chunks_exact_mut(dest_stride))
                    {
                        out_line[..src_stride].copy_from_slice(in_line);
                    }

                    out_plane_mut.into_memory()
                };

                let mem_size = mem.size();
                mut_buf.append_memory(mem);

                strides.push(src_stride as i32);
                offsets.push(acc_offset);
                acc_offset += mem_size;
            }
        } else if decoded_frame.bit_depth <= 16 {
            use byte_slice_cast::{AsByteSlice, AsMutByteSlice};

            for (plane, decoded_plane) in decoded_frame.buf16.drain(..).enumerate() {
                let component = format_info
                    .plane()
                    .iter()
                    .position(|&p| p == plane as u32)
                    .unwrap() as u8;

                let comp_height =
                    format_info.scale_height(component, output_info.height()) as usize;
                let src_stride = (decoded_plane.len() * 2) / comp_height;
                let dest_stride = output_info.stride()[plane] as usize;

                let mem = if video_meta_supported || src_stride == dest_stride {
                    struct WrappedVec16(Vec<u16>);
                    impl AsRef<[u8]> for WrappedVec16 {
                        fn as_ref(&self) -> &[u8] {
                            self.0.as_byte_slice()
                        }
                    }
                    impl AsMut<[u8]> for WrappedVec16 {
                        fn as_mut(&mut self) -> &mut [u8] {
                            self.0.as_mut_byte_slice()
                        }
                    }
                    // Just wrap the decoded frame vecs and push them out
                    gst::Memory::from_mut_slice(WrappedVec16(decoded_plane))
                } else {
                    // Mismatched stride, let's copy
                    let out_plane = gst::Memory::with_size(dest_stride * comp_height);
                    let mut out_plane_mut = out_plane.into_mapped_memory_writable().unwrap();

                    for (in_line, out_line) in decoded_plane
                        .as_slice()
                        .as_byte_slice()
                        .chunks_exact(src_stride)
                        .zip(out_plane_mut.as_mut_slice().chunks_exact_mut(dest_stride))
                    {
                        out_line[..src_stride].copy_from_slice(in_line);
                    }

                    out_plane_mut.into_memory()
                };

                let mem_size = mem.size();
                mut_buf.append_memory(mem);

                strides.push(src_stride as i32);
                offsets.push(acc_offset);
                acc_offset += mem_size;
            }
        } else {
            unimplemented!("Bit depth {} not supported yet", decoded_frame.bit_depth);
        }

        if video_meta_supported {
            gst_video::VideoMeta::add_full(
                buf.get_mut().unwrap(),
                gst_video::VideoFrameFlags::empty(),
                output_info.format(),
                output_info.width(),
                output_info.height(),
                &offsets,
                &strides[..],
            )
            .unwrap();
        }

        buf
    }
}

static CAT: Lazy<gst::DebugCategory> = Lazy::new(|| {
    gst::DebugCategory::new(
        "ffv1dec",
        gst::DebugColorFlags::empty(),
        Some("FFV1 decoder"),
    )
});

#[glib::object_subclass]
impl ObjectSubclass for Ffv1Dec {
    const NAME: &'static str = "Ffv1Dec";
    type Type = super::Ffv1Dec;
    type ParentType = gst_video::VideoDecoder;
}

impl ObjectImpl for Ffv1Dec {}

impl GstObjectImpl for Ffv1Dec {}

impl ElementImpl for Ffv1Dec {
    fn metadata() -> Option<&'static gst::subclass::ElementMetadata> {
        static ELEMENT_METADATA: Lazy<gst::subclass::ElementMetadata> = Lazy::new(|| {
            gst::subclass::ElementMetadata::new(
                "FFV1 Decoder",
                "Codec/Decoder/Video",
                "Decode FFV1 video streams",
                "Arun Raghavan <arun@asymptotic.io>",
            )
        });

        Some(&*ELEMENT_METADATA)
    }

    fn pad_templates() -> &'static [gst::PadTemplate] {
        static PAD_TEMPLATES: Lazy<Vec<gst::PadTemplate>> = Lazy::new(|| {
            let sink_caps = gst::Caps::builder("video/x-ffv")
                .field("ffvversion", 1)
                .field("width", gst::IntRange::new(1, i32::MAX))
                .field("height", gst::IntRange::new(1, i32::MAX))
                .field(
                    "framerate",
                    gst::FractionRange::new(
                        gst::Fraction::new(0, 1),
                        gst::Fraction::new(i32::MAX, 1),
                    ),
                )
                .build();
            let sink_pad_template = gst::PadTemplate::new(
                "sink",
                gst::PadDirection::Sink,
                gst::PadPresence::Always,
                &sink_caps,
            )
            .unwrap();

            let src_caps = gst::Caps::builder("video/x-raw")
                .field("format", gst::List::from(get_all_video_formats()))
                .field("width", gst::IntRange::new(1, i32::MAX))
                .field("height", gst::IntRange::new(1, i32::MAX))
                .field(
                    "framerate",
                    gst::FractionRange::new(
                        gst::Fraction::new(0, 1),
                        gst::Fraction::new(i32::MAX, 1),
                    ),
                )
                .build();
            let src_pad_template = gst::PadTemplate::new(
                "src",
                gst::PadDirection::Src,
                gst::PadPresence::Always,
                &src_caps,
            )
            .unwrap();

            vec![sink_pad_template, src_pad_template]
        });

        &*PAD_TEMPLATES
    }
}

impl VideoDecoderImpl for Ffv1Dec {
    /* We allocate the decoder here rather than start() because we need the sink caps */
    fn set_format(
        &self,
        element: &super::Ffv1Dec,
        state: &gst_video::VideoCodecState<'static, gst_video::video_codec_state::Readable>,
    ) -> Result<(), gst::LoggableError> {
        let info = state.info();
        let codec_data = state
            .codec_data()
            .ok_or_else(|| gst::loggable_error!(CAT, "Missing codec_data"))?
            .map_readable()?;
        let decoder = Decoder::new(codec_data.as_slice(), info.width(), info.height())
            .map_err(|err| gst::loggable_error!(CAT, "Could not instantiate decoder: {}", err))?;

        let format = get_output_format(decoder.config_record())
            .ok_or_else(|| gst::loggable_error!(CAT, "Unsupported format"))?;

        let output_state = element
            .set_output_state(format, info.width(), info.height(), Some(state))
            .map_err(|err| gst::loggable_error!(CAT, "Failed to set output params: {}", err))?;

        let output_info = Some(output_state.info());

        let mut decoder_state = self.state.lock().unwrap();
        *decoder_state = DecoderState::Started {
            output_info,
            decoder: Box::new(decoder),
            video_meta_supported: false,
        };
        drop(decoder_state);

        element
            .negotiate(output_state)
            .map_err(|err| gst::loggable_error!(CAT, "Negotiation failed: {}", err))?;

        self.parent_set_format(element, state)
    }

    fn stop(&self, element: &super::Ffv1Dec) -> Result<(), gst::ErrorMessage> {
        let mut decoder_state = self.state.lock().unwrap();
        *decoder_state = DecoderState::Stopped;

        self.parent_stop(element)
    }

    fn handle_frame(
        &self,
        element: &super::Ffv1Dec,
        mut frame: gst_video::VideoCodecFrame,
    ) -> Result<gst::FlowSuccess, gst::FlowError> {
        let mut state = self.state.lock().unwrap();

        let (output_info, decoder, video_meta_supported) = match *state {
            DecoderState::Started {
                output_info: Some(ref output_info),
                ref mut decoder,
                video_meta_supported,
                ..
            } => Ok((output_info, decoder, video_meta_supported)),
            _ => Err(gst::FlowError::Error),
        }?;

        let input_buffer = frame
            .input_buffer()
            .expect("Frame must have input buffer")
            .map_readable()
            .expect("Could not map input buffer for read");

        let decoded_frame = decoder.decode_frame(input_buffer.as_slice()).map_err(|e| {
            gst::gst_error!(CAT, "Decoding failed: {}", e);
            gst::FlowError::Error
        })?;

        // Drop so we can mutably borrow frame later
        drop(input_buffer);

        //  * Make sure the decoder and output plane orders match for all cases
        let buf = self.get_decoded_frame(decoded_frame, output_info, video_meta_supported);

        // We no longer need the state lock
        drop(state);

        frame.set_output_buffer(buf);
        element.finish_frame(frame)?;

        Ok(gst::FlowSuccess::Ok)
    }

    fn decide_allocation(
        &self,
        element: &Self::Type,
        query: &mut gst::query::Allocation<gst::QueryRef>,
    ) -> Result<(), gst::LoggableError> {
        let supported = query
            .find_allocation_meta::<gst_video::VideoMeta>()
            .is_some();

        let mut state = self.state.lock().unwrap();
        if let DecoderState::Started {
            ref mut video_meta_supported,
            ..
        } = *state
        {
            *video_meta_supported = supported;
        }

        self.parent_decide_allocation(element, query)
    }
}