ez-ffmpeg 0.10.0

A safe and ergonomic Rust interface for FFmpeg integration, designed for ease of use.
Documentation
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
use crate::core::codec::Codec;
use crate::core::context::decoder_stream::DecoderStream;
use crate::core::context::PacketBox;
use crate::core::hwaccel::HWAccelID;
use crate::core::scheduler::input_controller::SchNode;
use crate::error::OpenInputError;
use crate::filter::frame_pipeline::FramePipeline;
use crossbeam_channel::Sender;
use ffmpeg_sys_next::AVHWDeviceType::AV_HWDEVICE_TYPE_NONE;
use ffmpeg_sys_next::AVMediaType::{AVMEDIA_TYPE_AUDIO, AVMEDIA_TYPE_SUBTITLE, AVMEDIA_TYPE_VIDEO};
use ffmpeg_sys_next::AVPixelFormat::{
    AV_PIX_FMT_CUDA, AV_PIX_FMT_MEDIACODEC, AV_PIX_FMT_NONE, AV_PIX_FMT_QSV,
};
use ffmpeg_sys_next::{
    av_channel_layout_default, av_codec_is_decoder, av_codec_iterate, av_get_pix_fmt, av_hwdevice_find_type_by_name, av_hwdevice_get_type_name, avcodec_descriptor_get, avcodec_descriptor_get_by_name, avcodec_find_decoder, avcodec_find_decoder_by_name, avcodec_get_hw_config, AVChannelOrder, AVCodecID, AVCodecParameters, AVFormatContext, AVHWDeviceType, AVMediaType, AVPixelFormat, AVRational, AVERROR, AVERROR_DECODER_NOT_FOUND, EINVAL
};
use log::{debug, error, warn};
use std::ffi::{CStr, CString};
use std::ptr::{null, null_mut};
use std::sync::Arc;

pub(crate) struct Demuxer {
    pub(crate) url: String,
    pub(crate) is_set_read_callback: bool,
    pub(crate) in_fmt_ctx: *mut AVFormatContext,
    pub(crate) ts_offset: i64,
    pub(crate) frame_pipelines: Option<Vec<FramePipeline>>,

    pub(crate) readrate: Option<f32>,
    pub(crate) start_time_us: Option<i64>,
    pub(crate) recording_time_us: Option<i64>,
    pub(crate) exit_on_error: Option<bool>,
    pub(crate) stream_loop: Option<i32>,
    pub(crate) copy_ts: bool,

    /// Automatically rotate video based on display matrix metadata.
    /// Default is true (enabled).
    ///
    /// FFmpeg CLI: `-autorotate 0/1`
    /// FFmpeg source: `ffmpeg_demux.c:1319`, `ffmpeg_filter.c` (FFmpeg 7.x)
    pub(crate) autorotate: bool,

    /// Timestamp scale factor for pts/dts values.
    /// Default is 1.0 (no scaling).
    ///
    /// FFmpeg CLI: `-itsscale <scale>`
    /// FFmpeg source: `ffmpeg_demux.c:420-422` (FFmpeg 7.x)
    pub(crate) ts_scale: f64,

    /// Forced framerate for the input video stream.
    /// When set (num != 0), overrides the DTS estimation logic to use framerate-based
    /// grid calculation. When `{0, 0}` (default), packet duration is used for DTS
    /// estimation, matching FFmpeg CLI behavior when `-r` is not specified.
    ///
    /// FFmpeg CLI: `-r <rate>`
    /// FFmpeg source: `ffmpeg.h:452`, `ffmpeg_demux.c:329-333` (FFmpeg 7.x)
    pub(crate) framerate: AVRational,

    #[cfg(windows)]
    pub(crate) hwaccel: Option<String>,

    pub(crate) start_time_effective: i64,

    pub(crate) node: Arc<SchNode>,
    streams: Vec<DecoderStream>,
    dsts: Vec<(Sender<PacketBox>, usize, Option<usize>)>,
}

// SAFETY: Demuxer can be sent to another thread. The raw FFmpeg pointers are only
// accessed from the owning thread. Note: Demuxer is NOT Sync because it contains
// frame_pipelines with Box<dyn FrameFilter> which only implements Send.
unsafe impl Send for Demuxer {}

impl Demuxer {
    pub(crate) fn new(
        url: String,
        is_set_read_callback: bool,
        in_fmt_ctx: *mut AVFormatContext,
        ts_offset: i64,
        frame_pipelines: Option<Vec<FramePipeline>>,
        video_codec: Option<String>,
        audio_codec: Option<String>,
        subtitle_codec: Option<String>,
        readrate: Option<f32>,
        start_time_us: Option<i64>,
        recording_time_us: Option<i64>,
        exit_on_error: Option<bool>,
        stream_loop: Option<i32>,
        hwaccel: Option<String>,
        hwaccel_device: Option<String>,
        hwaccel_output_format: Option<String>,
        copy_ts: bool,
        autorotate: bool,
        ts_scale: f64,
        framerate: AVRational,
    ) -> crate::error::Result<Self> {
        let streams = Self::init_streams(
            in_fmt_ctx,
            video_codec,
            audio_codec,
            subtitle_codec,
            hwaccel.clone(),
            hwaccel_device,
            hwaccel_output_format,
        )?;

        Ok(Self {
            url,
            is_set_read_callback,
            in_fmt_ctx,
            ts_offset,
            frame_pipelines,
            readrate,
            start_time_us,
            recording_time_us,
            exit_on_error,
            stream_loop,
            copy_ts,
            autorotate,
            ts_scale,
            framerate,
            #[cfg(windows)]
            hwaccel,
            node: Arc::new(SchNode::Demux { waiter: Arc::new(Default::default()), task_exited: Arc::new(Default::default()) }),
            streams,
            dsts: vec![],
            start_time_effective: 0,
        })
    }

    fn init_streams(
        fmt_ctx: *mut AVFormatContext,
        video_codec: Option<String>,
        audio_codec: Option<String>,
        subtitle_codec: Option<String>,
        hwaccel: Option<String>,
        hwaccel_device: Option<String>,
        hwaccel_output_format: Option<String>,
    ) -> crate::error::Result<Vec<DecoderStream>> {
        unsafe {
            let stream_count = (*fmt_ctx).nb_streams;
            let mut streams = Vec::with_capacity(stream_count as usize);

            for i in 0..stream_count {
                let st = *(*fmt_ctx).streams.add(i as usize);

                let duration = (*st).duration;
                let time_base = (*st).time_base;
                let avg_framerate = (*st).avg_frame_rate;
                let codec_parameters = (*st).codecpar;
                let codec_type = (*codec_parameters).codec_type;

                let (hwaccel_id, hwaccel_device_type, hwaccel_device, hwaccel_output_format) =
                    find_hwaccel(
                        codec_type,
                        hwaccel.clone(),
                        hwaccel_device.clone(),
                        hwaccel_output_format.clone(),
                    )?;

                let codec_id = (*codec_parameters).codec_id;

                if codec_type == AVMEDIA_TYPE_AUDIO
                    && (*codec_parameters).ch_layout.order == AVChannelOrder::AV_CHANNEL_ORDER_UNSPEC
                        && (*codec_parameters).ch_layout.nb_channels > 0 {
                        av_channel_layout_default(&mut (*codec_parameters).ch_layout, (*codec_parameters).ch_layout.nb_channels);
                }

                let codec_name =
                    get_codec_name(codec_type, &video_codec, &audio_codec, &subtitle_codec);
                let decoder = choose_decoder(
                    codec_name,
                    codec_type,
                    codec_parameters,
                    codec_id,
                    hwaccel_id,
                    hwaccel_device_type,
                )?;
                let codec_desc = avcodec_descriptor_get(codec_id);

                let stream = DecoderStream::new(
                    i as usize,
                    st,
                    codec_parameters,
                    codec_type,
                    match decoder {
                        Some(decoder) => { decoder.as_ptr() },
                        None => { null() },
                    },
                    codec_desc,
                    duration,
                    time_base,
                    avg_framerate,
                    hwaccel_id,
                    hwaccel_device_type,
                    hwaccel_device,
                    hwaccel_output_format,
                );
                streams.push(stream);
            }

            Ok(streams)
        }
    }

    pub(crate) fn add_packet_dst(
        &mut self,
        packet_dst: Sender<PacketBox>,
        input_stream_index: usize,
        output_stream_index: usize,
    ) {
        self.dsts
            .push((packet_dst, input_stream_index, Some(output_stream_index)));
    }

    pub(crate) fn get_streams(&self) -> &Vec<DecoderStream> {
        &self.streams
    }

    pub(crate) fn get_streams_mut(&mut self) -> &mut Vec<DecoderStream> {
        &mut self.streams
    }

    pub(crate) fn get_stream_mut(&mut self, index: usize) -> &mut DecoderStream {
        &mut self.streams[index]
    }

    pub(crate) fn get_stream(&self, index: usize) -> &DecoderStream {
        &self.streams[index]
    }

    pub(crate) fn connect_stream(&mut self, index: usize) {
        if self.streams[index].is_used() {
            return;
        }
        let (sender, receiver) = crossbeam_channel::bounded(8);
        self.dsts.push((sender, index, None));
        self.streams[index].set_src(receiver);
    }

    pub(crate) fn take_dsts(&mut self) -> Vec<(Sender<PacketBox>, usize, Option<usize>)> {
        std::mem::take(&mut self.dsts)
    }

    pub(crate) fn destination_is_empty(&mut self) -> bool {
        self.dsts.is_empty()
    }
}

fn get_codec_name(
    codec_type: AVMediaType,
    video_codec: &Option<String>,
    audio_codec: &Option<String>,
    subtitle_codec: &Option<String>,
) -> Option<String> {
    if codec_type == AVMEDIA_TYPE_VIDEO {
        video_codec.clone()
    } else if codec_type == AVMEDIA_TYPE_AUDIO {
        audio_codec.clone()
    } else if codec_type == AVMEDIA_TYPE_SUBTITLE {
        subtitle_codec.clone()
    } else {
        None
    }
}

fn choose_decoder(
    codec_name: Option<String>,
    codec_type: AVMediaType,
    codec_parameters: *mut AVCodecParameters,
    codec_id: AVCodecID,
    hwaccel_id: HWAccelID,
    hwaccel_device_type: AVHWDeviceType,
) -> crate::error::Result<Option<Codec>> {
    match codec_name {
        Some(codec_name) => unsafe {
            let codec_cstr = CString::new(codec_name.clone())?;

            let mut codec = avcodec_find_decoder_by_name(codec_cstr.as_ptr());
            let desc = avcodec_descriptor_get_by_name(codec_cstr.as_ptr());

            if codec.is_null() && !desc.is_null() {
                codec = avcodec_find_decoder((*desc).id);
                if !codec.is_null() {
                    let codec_name = (*codec).name;
                    let codec_name = CStr::from_ptr(codec_name).to_str();
                    let desc_name = (*desc).name;
                    let desc_name = CStr::from_ptr(desc_name).to_str();
                    if let (Ok(codec_name), Ok(desc_name)) = (codec_name, desc_name) {
                        debug!("Matched decoder '{codec_name}' for codec '{desc_name}'.");
                    }
                }
            }

            if codec.is_null() {
                error!("Unknown decoder '{codec_name}'");
                return Err(OpenInputError::from(AVERROR_DECODER_NOT_FOUND).into());
            }

            if (*codec).type_ != codec_type {
                error!("Invalid decoder type '{codec_name}'");
                return Err(OpenInputError::InvalidArgument.into());
            }
            let codec_id = (*codec).id;

            (*codec_parameters).codec_id = codec_id;
            if (*codec_parameters).codec_type != codec_type {
                (*codec_parameters).codec_type = codec_type;
            }

            Ok(Some(Codec::new(codec)))
        },
        None => {
            if codec_type == AVMEDIA_TYPE_VIDEO
                && hwaccel_id == HWAccelID::HwaccelGeneric
                && hwaccel_device_type != AV_HWDEVICE_TYPE_NONE
            {
                let mut i = null_mut();
                loop {
                    let c = unsafe { av_codec_iterate(&mut i) };
                    if c.is_null() {
                        break;
                    }
                    unsafe {
                        if (*c).id != codec_id || av_codec_is_decoder(c) == 0 {
                            continue;
                        }
                    }

                    let mut j = 0;
                    loop {
                        unsafe {
                            let config = avcodec_get_hw_config(c, j);
                            if config.is_null() {
                                break;
                            }
                            if (*config).device_type == hwaccel_device_type {
                                let name = (*c).name;
                                let name = CStr::from_ptr(name).to_str();
                                let type_name = av_hwdevice_get_type_name(hwaccel_device_type);
                                let type_name = CStr::from_ptr(type_name).to_str();
                                if let (Ok(name), Ok(type_name)) = (name, type_name) {
                                    debug!("Selecting decoder '{name}' because of requested hwaccel method {type_name}");
                                }

                                return Ok(Some(Codec::new(c)));
                            }
                        }
                        j += 1;
                    }
                }
            }

            let c = unsafe { avcodec_find_decoder(codec_id) };
            if c.is_null() {
                Ok(None)
            } else {
                Ok(Some(Codec::new(c)))
            }
        }
    }
}

fn find_hwaccel(
    codec_type: AVMediaType,
    hwaccel: Option<String>,
    hwaccel_device: Option<String>,
    hwaccel_output_format: Option<String>,
) -> crate::error::Result<(HWAccelID, AVHWDeviceType, Option<String>, AVPixelFormat)> {
    if codec_type != AVMediaType::AVMEDIA_TYPE_VIDEO {
        return Ok((
            HWAccelID::HwaccelNone,
            AVHWDeviceType::AV_HWDEVICE_TYPE_NONE,
            None,
            AV_PIX_FMT_NONE,
        ));
    }
    let mut out_hwaccel_output_format = AV_PIX_FMT_NONE;

    match (&hwaccel, hwaccel_output_format) {
        (Some(hwaccel), None) if hwaccel == "cuvid" => {
            warn!("WARNING: Defaulting hwaccel_output_format to cuda for compatibility with older.This behavior is DEPRECATED and will be removed in the future.Please explicitly set \"hwaccel_output_format\" to \"cuda\" using the appropriate API method.");
            out_hwaccel_output_format = AV_PIX_FMT_CUDA;
        }
        (Some(hwaccel), None) if hwaccel == "qsv" => {
            warn!("WARNING: Defaulting hwaccel_output_format to qsv for compatibility with older.This behavior is DEPRECATED and will be removed in the future.Please explicitly set \"hwaccel_output_format\" to \"qsv\" using the appropriate API method.");
            out_hwaccel_output_format = AV_PIX_FMT_QSV;
        }
        (Some(hwaccel), None) if hwaccel == "mediacodec" => {
            // There is no real AVHWFrameContext implementation. Set
            // hwaccel_output_format to avoid av_hwframe_transfer_data error.
            out_hwaccel_output_format = AV_PIX_FMT_MEDIACODEC;
        }
        (_, Some(hwaccel_output_format)) => {
            let hwaccel_output_format_cstr = CString::new(hwaccel_output_format)?;

            let hwaccel_format = unsafe { av_get_pix_fmt(hwaccel_output_format_cstr.as_ptr()) };
            if hwaccel_format == AV_PIX_FMT_NONE {
                error!("Unrecognised hwaccel output format: {:?}", hwaccel_format);
            } else {
                out_hwaccel_output_format = hwaccel_format;
            }
        }
        _ => {}
    }

    let mut out_hwaccel_id = HWAccelID::HwaccelNone;
    let mut out_hwaccel_device_type = AV_HWDEVICE_TYPE_NONE;
    if let Some(mut hwaccel) = hwaccel {
        // The NVDEC hwaccels use a CUDA device, so remap the name here.
        if hwaccel == "nvdec" || hwaccel == "cuvid" {
            hwaccel = "cuda".to_string();
        }
        if hwaccel == "none" {
        } else if hwaccel == "auto" {
            out_hwaccel_id = HWAccelID::HwaccelAuto;
        } else {
            let hwaccel_cstr = CString::new(hwaccel.clone())?;
            let device_type = unsafe { av_hwdevice_find_type_by_name(hwaccel_cstr.as_ptr()) };
            if device_type != AV_HWDEVICE_TYPE_NONE {
                out_hwaccel_id = HWAccelID::HwaccelGeneric;
                out_hwaccel_device_type = device_type;
            }

            if out_hwaccel_id == HWAccelID::HwaccelNone {
                error!("Unrecognized hwaccel: {hwaccel}.");

                let mut hwaccels = Vec::new();
                loop {
                    let device_type =
                        unsafe { av_hwdevice_find_type_by_name(hwaccel_cstr.as_ptr()) };
                    if device_type == AV_HWDEVICE_TYPE_NONE {
                        break;
                    }
                    hwaccels.push(device_type);
                }
                if !hwaccels.is_empty() {
                    error!("Supported hwaccels: {:?}.", hwaccels);
                } else {
                    error!("No hardware acceleration.");
                }
                return Err(OpenInputError::from(AVERROR(EINVAL)).into());
            }
        }
    }

    Ok((
        out_hwaccel_id,
        out_hwaccel_device_type,
        hwaccel_device,
        out_hwaccel_output_format,
    ))
}