ez_ffmpeg/core/context/mod.rs
1use crate::error::AllocFrameError;
2use ffmpeg_sys_next::AVMediaType::{
3 AVMEDIA_TYPE_ATTACHMENT, AVMEDIA_TYPE_AUDIO, AVMEDIA_TYPE_DATA, AVMEDIA_TYPE_SUBTITLE,
4 AVMEDIA_TYPE_VIDEO,
5};
6// Only SideDataList::push_clone (gated to FFmpeg 8+) clones entries; the
7// free side runs on every real lane via Drop. Both symbols are FFmpeg 7.0+,
8// and docs.rs generates its bindings against an older apt FFmpeg — so like
9// every other >=7.0 symbol in this crate they stay out of docsrs builds.
10#[cfg(ffmpeg_8_0)]
11use ffmpeg_sys_next::av_frame_side_data_clone;
12#[cfg(not(docsrs))]
13use ffmpeg_sys_next::av_frame_side_data_free;
14use ffmpeg_sys_next::{
15 av_freep, av_gettime_relative, avcodec_free_context, avformat_close_input,
16 avformat_free_context, avio_closep, avio_context_free, AVCodecContext, AVFormatContext,
17 AVFrameSideData, AVIOContext, AVMediaType, AVRational, AVStream, AVFMT_NOFILE,
18};
19use std::ffi::c_void;
20use std::ptr::null_mut;
21use std::sync::atomic::{AtomicI64, AtomicUsize, Ordering};
22use std::sync::Arc;
23
24/// How long output I/O may continue after STATUS_END before the interrupt
25/// callback cuts it: long enough for a healthy muxer to finish its trailer,
26/// short enough that stop() on a dead network peer returns within a second.
27/// While a muxer holds an [`OutputFinalizeGuard`] the window does not apply
28/// at all — a `movflags=+faststart` trailer rewrites the whole file and can
29/// legitimately take far longer than any fixed grace.
30const OUTPUT_END_GRACE_US: i64 = 500_000;
31
32/// Default size of the AVIO buffer backing a custom read/write callback. FFmpeg
33/// hands the callback one buffer-sized chunk at a time, so a larger buffer means
34/// fewer Rust↔FFmpeg round-trips for sequential/network IO; the 64 KiB default
35/// keeps first-packet latency low for live/low-latency use. Configurable per
36/// Input/Output via set_io_buffer_size (sys-06).
37pub(crate) const DEFAULT_CUSTOM_IO_BUFFER_SIZE: usize = 64 * 1024;
38
39/// Shared state behind the AVIO interrupt callbacks (fftools installs
40/// decode_interrupt_cb on inputs and outputs, ffmpeg_mux_init.c:3326,3371).
41///
42/// Inputs are interrupted as soon as the scheduler is stopping: nothing
43/// meaningful is read after that. Outputs distinguish the terminal states:
44/// STATUS_ABORT cuts I/O immediately (the caller gave up on the files), while
45/// STATUS_END grants a grace window so trailers still get written — only an
46/// output that stays blocked past the window (dead network peer) is cut.
47pub(crate) struct InterruptState {
48 scheduler_status: Arc<AtomicUsize>,
49 // Microsecond timestamp of the first output callback that observed
50 // STATUS_END; 0 = not observed yet.
51 end_grace_start_us: AtomicI64,
52 /// Number of mux workers currently inside their finalize window
53 /// (av_write_trailer through the output-context free). While > 0,
54 /// STATUS_END does not interrupt output I/O — only STATUS_ABORT does: a
55 /// graceful stop() must not corrupt a trailer that is being written
56 /// (H3), and abort() stays the hard-cancel escape hatch.
57 finalizing_outputs: AtomicUsize,
58}
59
60impl InterruptState {
61 pub(crate) fn new(scheduler_status: Arc<AtomicUsize>) -> Self {
62 Self {
63 scheduler_status,
64 end_grace_start_us: AtomicI64::new(0),
65 finalizing_outputs: AtomicUsize::new(0),
66 }
67 }
68
69 /// Marks one output as finalizing for the guard's lifetime.
70 pub(crate) fn begin_output_finalize(self: &Arc<Self>) -> OutputFinalizeGuard {
71 self.finalizing_outputs.fetch_add(1, Ordering::AcqRel);
72 OutputFinalizeGuard {
73 state: Arc::clone(self),
74 }
75 }
76
77 fn should_interrupt_input(&self) -> bool {
78 crate::core::scheduler::ffmpeg_scheduler::is_stopping(
79 self.scheduler_status.load(Ordering::Acquire),
80 )
81 }
82
83 fn should_interrupt_output(&self) -> bool {
84 let status = self.scheduler_status.load(Ordering::Acquire);
85 if status == crate::core::scheduler::ffmpeg_scheduler::STATUS_ABORT {
86 return true;
87 }
88 if status != crate::core::scheduler::ffmpeg_scheduler::STATUS_END {
89 return false;
90 }
91 if self.finalizing_outputs.load(Ordering::Acquire) > 0 {
92 // A trailer (e.g. a faststart moov rewrite) is in flight somewhere
93 // on this scheduler; only abort() may cut output I/O now. Checked
94 // BEFORE the grace CAS so the clock does not even start while a
95 // finalize is running; once the last guard drops, a still-blocked
96 // OTHER output starts its grace normally.
97 return false;
98 }
99 let now = unsafe { av_gettime_relative() };
100 let start = match self.end_grace_start_us.compare_exchange(
101 0,
102 now,
103 Ordering::AcqRel,
104 Ordering::Acquire,
105 ) {
106 Ok(_) => now,
107 Err(previous) => previous,
108 };
109 now - start > OUTPUT_END_GRACE_US
110 }
111}
112
113/// RAII finalize marker. The decrement is mandatory even on unwind — a stuck
114/// counter would exempt every later STATUS_END grace cut for this scheduler,
115/// and stop() on a blocked sibling output would hang forever.
116pub(crate) struct OutputFinalizeGuard {
117 state: Arc<InterruptState>,
118}
119
120impl Drop for OutputFinalizeGuard {
121 fn drop(&mut self) {
122 self.state.finalizing_outputs.fetch_sub(1, Ordering::AcqRel);
123 }
124}
125
126/// # Safety
127/// `opaque` must point to an `InterruptState` that outlives every
128/// AVFormatContext carrying this callback (the owning FfmpegContext holds the
129/// Arc and outlives all worker threads).
130pub(crate) unsafe extern "C" fn input_interrupt_cb(opaque: *mut c_void) -> libc::c_int {
131 let state = &*(opaque as *const InterruptState);
132 state.should_interrupt_input() as libc::c_int
133}
134
135/// # Safety
136/// Same contract as [`input_interrupt_cb`].
137pub(crate) unsafe extern "C" fn output_interrupt_cb(opaque: *mut c_void) -> libc::c_int {
138 let state = &*(opaque as *const InterruptState);
139 let interrupt = state.should_interrupt_output();
140 // This callback runs on the muxer's own thread, inside the retry loop
141 // of whatever blocking I/O it is cutting, so an election here is the
142 // one place that can attribute the cut to the exact write in flight.
143 #[cfg(test)]
144 if interrupt {
145 crate::core::scheduler::tcp_write_probe::note_cut_election();
146 }
147 interrupt as libc::c_int
148}
149
150/// Gate for the muxer's deferred start. fftools: `SchMux.mux_started` +
151/// `PreMuxQueue` under `Scheduler.mux_ready_lock` (ffmpeg_sched.c).
152///
153/// Until the muxer thread is running, encoders park packets in a bounded
154/// pre-queue; at start the muxer drains that queue and flips `started`.
155/// Without a lock the flip races the send: an encoder that read
156/// `started == false` can enqueue into the pre-queue AFTER the drain
157/// finished, and that packet is never delivered. Pre-queue sends therefore
158/// happen under the same lock as the drain-and-flip.
159pub(crate) struct MuxStartGate {
160 started: std::sync::atomic::AtomicBool,
161 lock: std::sync::Mutex<()>,
162}
163
164/// What a gated pre-queue send resolved to.
165pub(crate) enum PreSendOutcome {
166 /// Parked in the pre-queue; the drain will deliver it.
167 Sent,
168 /// The gate opened first: send to the live queue instead.
169 Started(PacketBox),
170 /// Pre-queue full: park on the queue condvar and retry (the gate lock
171 /// must not be held across the wait, or the drain could never run).
172 Full(PacketBox),
173 /// Pre-queue receiver is gone (muxer never started).
174 Disconnected(PacketBox),
175}
176
177impl MuxStartGate {
178 pub(crate) fn new() -> Self {
179 Self {
180 started: std::sync::atomic::AtomicBool::new(false),
181 lock: std::sync::Mutex::new(()),
182 }
183 }
184
185 pub(crate) fn is_started(&self) -> bool {
186 self.started.load(Ordering::Acquire)
187 }
188
189 /// Runs the pre-queue drain and opens the gate as one atomic step.
190 pub(crate) fn start_with(&self, drain: impl FnOnce()) {
191 let _guard = self.lock.lock().unwrap();
192 drain();
193 self.started.store(true, Ordering::Release);
194 }
195
196 /// Attempts a pre-queue send while the gate is verifiably closed.
197 pub(crate) fn send_pre(
198 &self,
199 pre_sender: &pre_mux_queue::PreMuxQueueSender,
200 packet_box: PacketBox,
201 ) -> PreSendOutcome {
202 let _guard = self.lock.lock().unwrap();
203 if self.started.load(Ordering::Acquire) {
204 return PreSendOutcome::Started(packet_box);
205 }
206 match pre_sender.try_push(packet_box) {
207 pre_mux_queue::PreQueueTryPush::Sent => PreSendOutcome::Sent,
208 pre_mux_queue::PreQueueTryPush::Full(pb) => PreSendOutcome::Full(pb),
209 pre_mux_queue::PreQueueTryPush::Disconnected(pb) => PreSendOutcome::Disconnected(pb),
210 }
211 }
212}
213
214use ffmpeg_context::{InputOpaque, OutputOpaque};
215
216/// The **ffmpeg_context** module is responsible for assembling FFmpeg’s configuration:
217/// inputs, outputs, codecs, filters, and other parameters needed to construct a
218/// complete media processing pipeline.
219///
220/// # Example
221/// ```rust,ignore
222///
223/// // Build an FFmpeg context with one input, some filter settings, and one output
224/// let context = FfmpegContext::builder()
225/// .input("test.mp4")
226/// .filter_desc("hue=s=0")
227/// .output("output.mp4")
228/// .build()
229/// .unwrap();
230/// // The context now holds all info needed for an FFmpeg job.
231/// ```
232pub mod ffmpeg_context;
233
234/// The **ffmpeg_context_builder** module defines the builder pattern for creating
235/// [`FfmpegContext`](ffmpeg_context::FfmpegContext) objects.
236///
237/// It exposes the [`FfmpegContextBuilder`](ffmpeg_context_builder::FfmpegContextBuilder) struct, which allows you to:
238/// - Configure multiple [`Input`](input::Input) and
239/// [`Output`](output::Output) streams.
240/// - Attach filter descriptions via [`FilterComplex`](crate::core::context::filter_complex::FilterComplex)
241/// or inline strings (e.g., `"scale=1280:720"`, `"hue=s=0"`).
242/// - Produce a finished `FfmpegContext` that can then be executed by
243/// [`FfmpegScheduler`](crate::core::scheduler::ffmpeg_scheduler::FfmpegScheduler).
244///
245/// # Examples
246///
247/// ```rust,ignore
248/// // 1. Create a builder (usually via FfmpegContext::builder())
249/// let builder = FfmpegContext::builder();
250///
251/// // 2. Add inputs, outputs, and filters
252/// let ffmpeg_context = builder
253/// .input("input.mp4")
254/// .filter_desc("hue=s=0")
255/// .output("output.mp4")
256/// .build()
257/// .expect("Failed to build FfmpegContext");
258///
259/// // 3. Use `ffmpeg_context` with FfmpegScheduler (e.g., `.start()` and `.wait()`).
260/// ```
261pub mod ffmpeg_context_builder;
262
263/// The **input** module defines the [`Input`](crate::core::context::input::Input) struct,
264/// representing an FFmpeg input source. An input can be:
265/// - A file path or URL (e.g., `"video.mp4"`, `rtmp://example.com/live/stream`).
266/// - A **custom data source** via a `read_callback` (and optionally `seek_callback`) for
267/// advanced scenarios like in-memory buffers or network protocols.
268///
269/// You can also specify **frame pipelines** to apply custom [`FrameFilter`](crate::core::filter::frame_filter::FrameFilter)
270/// transformations **after decoding** but **before** the frames move on to the rest of the pipeline.
271///
272/// # Example
273///
274/// ```rust,ignore
275/// use ez_ffmpeg::core::context::input::Input;
276///
277/// // Basic file or network URL:
278/// let file_input: Input = "example.mp4".into();
279///
280/// // Or a custom read callback:
281/// let custom_input = Input::new_by_read_callback(|buf| {
282/// // Fill `buf` with data from your source
283/// // Return the number of bytes read, or negative for errors
284/// 0
285/// });
286/// ```
287pub mod input;
288
289/// The **output** module defines the [`Output`](crate::core::context::output::Output) struct,
290/// representing an FFmpeg output destination. An output may be:
291/// - A file path or URL (e.g., `"output.mp4"`, `rtmp://...`).
292/// - A **custom write callback** that processes encoded data (e.g., storing it
293/// in-memory or sending it over a custom network protocol).
294///
295/// You can specify additional details such as:
296/// - **Container format** (e.g., `"mp4"`, `"flv"`, `"mkv"`).
297/// - **Video/Audio/Subtitle codecs** (e.g., `"h264"`, `"aac"`, `"mov_text"`).
298/// - **Frame pipelines** to apply [`FrameFilter`](crate::core::filter::frame_filter::FrameFilter)
299/// transformations **before encoding**.
300///
301/// # Example
302///
303/// ```rust,ignore
304/// use ez_ffmpeg::core::context::output::Output;
305///
306/// // Basic file/URL output:
307/// let file_output: Output = "output.mp4".into();
308///
309/// // Or a custom write callback:
310/// let custom_output = Output::new_by_write_callback(|encoded_data| {
311/// // Write `encoded_data` somewhere
312/// encoded_data.len() as i32
313/// }).set_format("mp4");
314/// ```
315pub mod output;
316
317/// The **filter_complex** module defines the [`FilterComplex`](crate::core::context::filter_complex::FilterComplex)
318/// struct, which encapsulates one or more FFmpeg filter descriptions (e.g., `"scale=1280:720"`,
319/// `"hue=s=0"`, etc.). You can use `FilterComplex` to construct more advanced or multi-step
320/// filter graphs than simple inline strings allow.
321///
322/// `FilterComplex` can also associate a particular hardware device (e.g., for GPU-based
323/// filtering) via `hw_device`.
324///
325/// # Example
326///
327/// ```rust,ignore
328/// use ez_ffmpeg::core::context::filter_complex::FilterComplex;
329///
330/// // Build a FilterComplex from a string:
331/// let my_filters = FilterComplex::from("scale=1280:720");
332///
333/// // Optionally specify a hardware device (e.g., "cuda"):
334/// // my_filters.set_hw_device("cuda");
335/// ```
336pub mod filter_complex;
337
338pub(super) mod attachment;
339pub(super) mod decoder_stream;
340pub(super) mod demuxer;
341pub(super) mod encoder_stream;
342pub(super) mod filter_graph;
343pub(super) mod frame_source;
344pub(super) mod input_filter;
345pub(super) mod muxer;
346pub(super) mod obj_pool;
347pub(super) mod output_filter;
348pub(super) mod pre_mux_queue;
349
350/// The **null_output** module provides a custom null output implementation for FFmpeg
351/// that discards all data while supporting seeking.
352///
353/// It exposes the [`create_null_output`](null_output::create_null_output) function, which returns an
354/// [`Output`](crate::Output) object configured to:
355/// - Discard all written data, behaving like `/dev/null`.
356/// - Maintain a seekable position state using atomic operations for thread-safe, high-performance access.
357/// - Support scenarios such as testing or processing streaming inputs (e.g., RTMP) where no output file is needed.
358///
359/// # Usage Scenario
360/// This module is useful when processing FFmpeg input streams without generating an output file, such as
361/// when handling RTMP streams that require a seekable output format like MP4, even if the output is discarded.
362///
363/// # Examples
364///
365/// ```rust,ignore
366/// use ez_ffmpeg::Output;
367/// let output: Output = create_null_output();
368/// // Pass `output` to an FFmpeg context for processing
369/// ```
370///
371/// # Performance
372/// - Utilizes `AtomicU64` with `Relaxed` ordering for lock-free position tracking, ensuring efficient concurrent access.
373/// - Write and seek operations are optimized to minimize overhead by avoiding locks.
374///
375/// # Notes
376/// - The default output format is "mp4", but this can be modified using `set_format` as needed.
377/// - Write operations assume individual buffers do not exceed `i32::MAX` bytes, which aligns with typical FFmpeg usage.
378pub mod null_output;
379
380pub(crate) struct CodecContext {
381 inner: *mut AVCodecContext,
382}
383
384// SAFETY: CodecContext can be sent to another thread. It is a single-owner
385// handle: application-side lifecycle API calls (open/decode/flush/free) go
386// through its one owner (a worker's resource struct or an open-function
387// local on failure paths), never through aliases on other Rust threads.
388// Rust code DOES also run on FFmpeg-managed threads — decode callbacks like
389// `get_format` receive a context pointer (often a per-thread copy) there —
390// but libavcodec synchronizes those accesses, and `avcodec_free_context`
391// (Drop) quiesces all callbacks and joins FFmpeg's workers before returning.
392unsafe impl Send for CodecContext {}
393
394impl CodecContext {
395 pub(crate) fn new(avcodec_context: *mut AVCodecContext) -> Self {
396 Self {
397 inner: avcodec_context,
398 }
399 }
400
401 pub(crate) fn as_mut_ptr(&self) -> *mut AVCodecContext {
402 self.inner
403 }
404
405 pub(crate) fn as_ptr(&self) -> *const AVCodecContext {
406 self.inner as *const AVCodecContext
407 }
408}
409
410impl Drop for CodecContext {
411 fn drop(&mut self) {
412 unsafe {
413 avcodec_free_context(&mut self.inner);
414 }
415 }
416}
417
418#[derive(Copy, Clone)]
419pub(crate) struct Stream {
420 pub(crate) inner: *mut AVStream,
421}
422
423// SAFETY: Stream can be sent to another thread. The raw AVStream pointer is owned
424// by the parent AVFormatContext, and the crate ensures the format context outlives
425// all Stream references.
426unsafe impl Send for Stream {}
427
428pub(crate) struct FrameBox {
429 pub(crate) frame: ffmpeg_next::Frame,
430 // stream copy or filtergraph
431 pub(crate) frame_data: FrameData,
432}
433
434// SAFETY: FrameBox can be sent to another thread. It contains an ffmpeg_next::Frame
435// (which wraps AVFrame) and FrameData, both of which are only accessed from the owning thread.
436unsafe impl Send for FrameBox {}
437
438pub fn frame_alloc() -> crate::error::Result<ffmpeg_next::Frame> {
439 unsafe {
440 let frame = ffmpeg_next::Frame::empty();
441 if frame.as_ptr().is_null() {
442 return Err(AllocFrameError::OutOfMemory.into());
443 }
444 Ok(frame)
445 }
446}
447
448pub fn null_frame() -> ffmpeg_next::Frame {
449 unsafe { ffmpeg_next::Frame::wrap(null_mut()) }
450}
451
452/// Owned array of global (stream-level) side data entries — the Rust shape of
453/// the `AVFrameSideData **side_data / int nb_side_data` pairs that fftools
454/// threads through InputFilterPriv, OutputFilterPriv and FrameData
455/// (ffmpeg_filter.c, commits e61b9d4094 / 7b18beb477). Entries are
456/// deep-cloned in and freed exactly once on drop.
457pub(crate) struct SideDataList {
458 entries: *mut *mut AVFrameSideData,
459 count: i32,
460}
461
462impl SideDataList {
463 pub(crate) fn new() -> Self {
464 Self {
465 entries: null_mut(),
466 count: 0,
467 }
468 }
469
470 /// Deep-clones one entry onto the list (flags as in
471 /// `av_frame_side_data_clone`). Returns 0 or a negative AVERROR.
472 #[cfg(ffmpeg_8_0)]
473 pub(crate) fn push_clone(&mut self, sd: *const AVFrameSideData, flags: u32) -> i32 {
474 unsafe { av_frame_side_data_clone(&mut self.entries, &mut self.count, sd, flags) }
475 }
476
477 pub(crate) fn clear(&mut self) {
478 // docs.rs bindings predate av_frame_side_data_free (FFmpeg 7.0), but
479 // there the list is provably empty — push_clone is ffmpeg_8_0-gated
480 // and that cfg never exists on docs.rs — so skipping the call is the
481 // exact semantics, not a stub.
482 #[cfg(not(docsrs))]
483 unsafe {
484 av_frame_side_data_free(&mut self.entries, &mut self.count)
485 }
486 }
487
488 #[cfg(ffmpeg_8_0)]
489 pub(crate) fn len(&self) -> i32 {
490 self.count
491 }
492
493 /// Raw entry array for FFmpeg parameter structs
494 /// (e.g. `AVBufferSrcParameters.side_data`); callees deep-copy what they
495 /// need, the list keeps ownership. Takes `&mut self` so handing out a
496 /// `*mut` view is justified by the signature, not by convention — which
497 /// also keeps the Sync claim honest (shared refs never leak mutability).
498 #[cfg(ffmpeg_8_0)]
499 pub(crate) fn as_mut_ptr(&mut self) -> *mut *mut AVFrameSideData {
500 self.entries
501 }
502
503 #[cfg(ffmpeg_8_0)]
504 pub(crate) fn iter(&self) -> impl Iterator<Item = *const AVFrameSideData> + '_ {
505 (0..self.count)
506 .map(|i| unsafe { *self.entries.offset(i as isize) as *const AVFrameSideData })
507 }
508}
509
510impl Drop for SideDataList {
511 fn drop(&mut self) {
512 self.clear();
513 }
514}
515
516// SAFETY: the list exclusively owns its deep-cloned entries. All mutation
517// happens on the thread that is building it; once frozen behind an Arc it is
518// only read, and readers clone entries out instead of mutating — so both
519// moving it across threads and sharing references are sound.
520unsafe impl Send for SideDataList {}
521unsafe impl Sync for SideDataList {}
522
523#[derive(Clone)]
524pub(crate) struct FrameData {
525 pub(crate) framerate: Option<AVRational>,
526 pub(crate) bits_per_raw_sample: i32,
527 pub(crate) input_stream_width: i32,
528 pub(crate) input_stream_height: i32,
529 /// Owned copy of the decoder's subtitle header (e.g. ASS script info),
530 /// shared across fan-out sends without reallocation. Owning the bytes
531 /// keeps the header valid after the decoder context is freed.
532 pub(crate) subtitle_header: Option<Arc<[u8]>>,
533
534 pub(crate) fg_input_index: usize,
535
536 /// Graph-level global side data snapshot, attached by the filtergraph
537 /// output for the frame that opens the encoder and consumed by
538 /// `enc_open` on FFmpeg 8+ (fftools FrameData.side_data, 7b18beb477).
539 /// Always `None` on FFmpeg 7.x, where the encoder scans frame side data
540 /// instead (n7.1 behavior) — hence dead there by design.
541 #[cfg_attr(not(ffmpeg_8_0), allow(dead_code))]
542 pub(crate) side_data: Option<Arc<SideDataList>>,
543}
544// Send + Sync are auto-derived. For side_data that rests on SideDataList's
545// hand-written unsafe impls (frozen-behind-Arc discipline); every other
546// field is plain owned data.
547
548pub(crate) struct PacketBox {
549 pub(crate) packet: ffmpeg_next::Packet,
550 pub(crate) packet_data: PacketData,
551}
552
553// SAFETY: PacketBox can be sent to another thread. It contains an ffmpeg_next::Packet
554// and PacketData, both only accessed from the owning thread.
555unsafe impl Send for PacketBox {}
556
557// optionally attached as opaque_ref to decoded AVFrames
558#[derive(Clone, Copy)]
559pub(crate) struct PacketData {
560 // demuxer-estimated dts in AV_TIME_BASE_Q,
561 // to be used when real dts is missing
562 pub(crate) dts_est: i64,
563 pub(crate) codec_type: AVMediaType,
564 pub(crate) output_stream_index: i32,
565 pub(crate) is_copy: bool,
566}
567
568pub(crate) fn out_fmt_ctx_free(out_fmt_ctx: *mut AVFormatContext, is_set_write_callback: bool) {
569 if out_fmt_ctx.is_null() {
570 return;
571 }
572 unsafe {
573 if is_set_write_callback {
574 free_output_opaque((*out_fmt_ctx).pb);
575 } else if (*(*out_fmt_ctx).oformat).flags & AVFMT_NOFILE == 0 {
576 // AVFMT_NOFILE lives on the *output format* flags, not the context flags
577 // (where the same bit 0x1 is AVFMT_FLAG_GENPTS). Reading it off the
578 // context could skip avio_closep and leak the pb when GENPTS is set.
579 let mut pb = (*out_fmt_ctx).pb;
580 if !pb.is_null() {
581 avio_closep(&mut pb);
582 }
583 }
584 avformat_free_context(out_fmt_ctx);
585 }
586}
587
588pub(crate) unsafe fn free_output_opaque(mut avio_ctx: *mut AVIOContext) {
589 if avio_ctx.is_null() {
590 return;
591 }
592 if !(*avio_ctx).buffer.is_null() {
593 av_freep(&mut (*avio_ctx).buffer as *mut _ as *mut c_void);
594 }
595 let opaque_ptr = (*avio_ctx).opaque as *mut OutputOpaque;
596 if !opaque_ptr.is_null() {
597 // The opaque box carries the USER's write/seek callback state, and
598 // its Drop is arbitrary user code with two distinct panic cases:
599 //
600 // - ALREADY UNWINDING (this free running as drop glue — the mux
601 // teardown guard dropping on a worker panic): a second panic here
602 // aborts the process before the frames above can release their
603 // thread slots. Destroy the box under its own catch (per-box
604 // containment, like the packet-sink disposal) and surface it
605 // through a catch-protected best-effort log; the job outcome is
606 // already the in-flight panic.
607 // - NORMAL TEARDOWN: let the panic PROPAGATE. The scheduler guards
608 // above this frame contain the fallout (bundle disposal is
609 // contained, the slot releases via its armed guard) and record
610 // the job as WorkerPanicked — the pinned contract
611 // (tests/packet_sink.rs, sibling_custom_io_destruction_panic_
612 // prevents_on_end): a teardown that destroyed user state by
613 // panicking must fail the job, not vanish into a log line.
614 if std::thread::panicking() {
615 let callback_state_panicked = std::panic::catch_unwind(std::panic::AssertUnwindSafe(
616 || drop(Box::from_raw(opaque_ptr)),
617 ))
618 .map_err(crate::core::packet_sink::dispose_panic_payload)
619 .is_err();
620 avio_context_free(&mut avio_ctx);
621 if callback_state_panicked {
622 // Contained itself: a panicking logger inside unwind drop
623 // glue is exactly the abort this branch exists to prevent.
624 let _ = std::panic::catch_unwind(|| {
625 log::error!(
626 "custom-IO output callback state panicked during teardown; the reclaim completed"
627 );
628 })
629 .map_err(crate::core::packet_sink::dispose_panic_payload);
630 }
631 return;
632 }
633 let _ = Box::from_raw(opaque_ptr);
634 }
635 avio_context_free(&mut avio_ctx);
636}
637
638pub(crate) fn in_fmt_ctx_free(mut in_fmt_ctx: *mut AVFormatContext, is_set_read_callback: bool) {
639 if in_fmt_ctx.is_null() {
640 return;
641 }
642 unsafe {
643 // Close the input FIRST: the demuxer's read_close may still touch
644 // s->pb (the official custom-IO example frees the AVIOContext only
645 // after avformat_close_input). With AVFMT_FLAG_CUSTOM_IO the close
646 // leaves pb alone, so capture it beforehand and free it after.
647 let avio_ctx = if is_set_read_callback {
648 (*in_fmt_ctx).pb
649 } else {
650 null_mut()
651 };
652 avformat_close_input(&mut in_fmt_ctx);
653 free_input_opaque(avio_ctx);
654 }
655}
656
657pub(crate) unsafe fn free_input_opaque(mut avio_ctx: *mut AVIOContext) {
658 if !avio_ctx.is_null() {
659 let opaque_ptr = (*avio_ctx).opaque as *mut InputOpaque;
660 if !opaque_ptr.is_null() {
661 let _ = Box::from_raw(opaque_ptr);
662 }
663 av_freep(&mut (*avio_ctx).buffer as *mut _ as *mut c_void);
664 avio_context_free(&mut avio_ctx);
665 }
666}
667
668/// RAII guard for a partially-initialized `AVFormatContext` (input or output).
669///
670/// During `open_input_file` / `open_output_file` the raw context (and, for
671/// custom-IO, its `AVIOContext` + callback `Box`) is owned by nobody until a
672/// [`Demuxer`]/[`Muxer`] takes it. Any `?`/early return in that window used to
673/// leak it. [`arm`](FmtCtxGuard::arm) it once the context is valid; on drop it
674/// frees via the same [`in_fmt_ctx_free`]/[`out_fmt_ctx_free`] paths a
675/// success-path [`crate::raw::FormatContext`] drop uses — unless
676/// [`release`](FmtCtxGuard::release) is called when ownership transfers.
677///
678/// The teardown path is selected by [`crate::raw::Mode`] (the same discriminant
679/// `FormatContext` carries), replacing the two former bool-keyed guards
680/// (`OutFmtCtxGuard`/`InFmtCtxGuard`).
681///
682/// Unlike `FormatContext`, this guard is re-armable and covers contexts that are
683/// only *partially* initialized (allocated but not yet opened, or mid custom-IO
684/// setup) — which is why it stays a separate type rather than reusing
685/// `FormatContext`'s already-opened constructors.
686pub(crate) struct FmtCtxGuard {
687 ctx: *mut AVFormatContext,
688 mode: crate::raw::Mode,
689}
690
691impl FmtCtxGuard {
692 pub(crate) fn disarmed() -> Self {
693 // `mode` is irrelevant while `ctx` is null (Drop no-ops on null).
694 Self {
695 ctx: null_mut(),
696 mode: crate::raw::Mode::Input,
697 }
698 }
699
700 /// Take ownership of a now-valid context so any early return frees it, with
701 /// the teardown path selected by `mode`.
702 pub(crate) fn arm(&mut self, ctx: *mut AVFormatContext, mode: crate::raw::Mode) {
703 self.ctx = ctx;
704 self.mode = mode;
705 }
706
707 /// Relinquish ownership (a Demuxer/Muxer/FormatContext now owns the context).
708 pub(crate) fn release(&mut self) -> *mut AVFormatContext {
709 let ctx = self.ctx;
710 self.ctx = null_mut();
711 ctx
712 }
713
714 /// Relinquish ownership into a [`crate::raw::FormatContext`] carrying the
715 /// same teardown [`crate::raw::Mode`] this guard was armed with: the mode
716 /// is decided once, at the arm site, and preserved end-to-end — callers
717 /// never re-infer it from output shape.
718 ///
719 /// Panics if the guard is disarmed (nothing to release); callers invoke
720 /// this only after a successful `arm`.
721 pub(crate) fn release_into(&mut self) -> crate::raw::FormatContext {
722 let mode = self.mode;
723 let ctx = self.release();
724 assert!(!ctx.is_null(), "release_into on a disarmed FmtCtxGuard");
725 // SAFETY: `ctx` was armed as a valid, fully-initialized context of
726 // exactly this mode (for OutputCustomIo the AVIO/pb wiring precedes
727 // the arm); ownership transfers to the returned FormatContext and the
728 // guard is now disarmed, so no double free is possible.
729 unsafe { crate::raw::FormatContext::from_mode(ctx, mode) }
730 }
731}
732
733impl Drop for FmtCtxGuard {
734 fn drop(&mut self) {
735 if self.ctx.is_null() {
736 return;
737 }
738 // Same dispatch as `FormatContext::Drop`.
739 match self.mode {
740 crate::raw::Mode::Input => in_fmt_ctx_free(self.ctx, false),
741 crate::raw::Mode::InputCustomIo => in_fmt_ctx_free(self.ctx, true),
742 crate::raw::Mode::Output => out_fmt_ctx_free(self.ctx, false),
743 crate::raw::Mode::OutputCustomIo => out_fmt_ctx_free(self.ctx, true),
744 }
745 }
746}
747
748#[allow(dead_code)]
749pub(crate) fn type_to_linklabel(media_type: AVMediaType, index: usize) -> Option<String> {
750 match media_type {
751 AVMediaType::AVMEDIA_TYPE_UNKNOWN => None,
752 AVMEDIA_TYPE_VIDEO => Some(format!("{index}:v")),
753 AVMEDIA_TYPE_AUDIO => Some(format!("{index}:a")),
754 AVMEDIA_TYPE_DATA => Some(format!("{index}:d")),
755 AVMEDIA_TYPE_SUBTITLE => Some(format!("{index}:s")),
756 AVMEDIA_TYPE_ATTACHMENT => Some(format!("{index}:t")),
757 AVMediaType::AVMEDIA_TYPE_NB => None,
758 }
759}
760
761#[cfg(test)]
762mod tests {
763 use super::*;
764 use crate::core::scheduler::ffmpeg_scheduler::{STATUS_ABORT, STATUS_END, STATUS_RUN};
765
766 fn state_with(status: usize) -> Arc<InterruptState> {
767 Arc::new(InterruptState::new(Arc::new(AtomicUsize::new(status))))
768 }
769
770 /// Rewinds the grace clock so the window is already expired — no sleeps.
771 fn expire_grace(state: &InterruptState) {
772 let past = unsafe { av_gettime_relative() } - OUTPUT_END_GRACE_US - 1;
773 state.end_grace_start_us.store(past, Ordering::Release);
774 }
775
776 #[test]
777 fn output_grace_cut_fires_after_the_window() {
778 let state = state_with(STATUS_END);
779 expire_grace(&state);
780 assert!(state.should_interrupt_output());
781 }
782
783 #[test]
784 fn finalize_guard_holds_the_grace_cut_open() {
785 let state = state_with(STATUS_END);
786 expire_grace(&state);
787 let guard = state.begin_output_finalize();
788 assert!(
789 !state.should_interrupt_output(),
790 "a finalizing output must not be cut by the STATUS_END grace"
791 );
792 drop(guard);
793 assert!(
794 state.should_interrupt_output(),
795 "after the last finalize guard drops, the expired grace cuts again"
796 );
797 }
798
799 #[test]
800 fn abort_cuts_through_a_finalize_window() {
801 let state = state_with(STATUS_ABORT);
802 let _guard = state.begin_output_finalize();
803 assert!(
804 state.should_interrupt_output(),
805 "abort() is the hard cancel: it must cut even a finalizing output"
806 );
807 }
808
809 #[test]
810 fn running_scheduler_never_cuts_output() {
811 let state = state_with(STATUS_RUN);
812 assert!(!state.should_interrupt_output());
813 }
814
815 #[test]
816 fn overlapping_finalize_windows_hold_until_the_last_drops() {
817 let state = state_with(STATUS_END);
818 expire_grace(&state);
819 let g1 = state.begin_output_finalize();
820 let g2 = state.begin_output_finalize();
821 drop(g1);
822 assert!(
823 !state.should_interrupt_output(),
824 "one of two overlapping finalize windows dropping must keep the hold"
825 );
826 drop(g2);
827 assert!(state.should_interrupt_output());
828 }
829}