Skip to main content

ez_ffmpeg/core/
mod.rs

1//! The **core** module provides the foundational building blocks for configuring and running FFmpeg
2//! pipelines. It encompasses:
3//!
4//! - **Input & Output Handling** (in [`context`]): Structures and logic (`Input`, `Output`) for
5//!   specifying where media data originates and where it should be written.
6//! - **Filter Descriptions**: Define filter graphs with `FilterComplex` or attach custom [`FrameFilter`](filter::frame_filter::FrameFilter)
7//!   implementations at the input/output stage.
8//! - **Stream and Device Queries** (in [`stream_info`] and [`device`]): Utilities for retrieving
9//!   information about media streams and available input devices.
10//! - **Hardware Acceleration** (in [`hwaccel`]): Enumerate/configure GPU-accelerated codecs (CUDA, VAAPI, etc.).
11//! - **Codec Discovery** (in [`codec`]): List encoders/decoders supported by FFmpeg.
12//! - **Custom Filters** (in [`filter`]): Implement user-defined [`FrameFilter`](filter::frame_filter::FrameFilter) logic for frames.
13//! - **Lifecycle Orchestration** (in [`scheduler`]): [`FfmpegScheduler`](scheduler::ffmpeg_scheduler::FfmpegScheduler) that runs the configured pipeline
14//!   (synchronously or asynchronously if the `async` feature is enabled).
15//!
16//! # Submodules
17//!
18//! - [`context`]: Houses [`FfmpegContext`](context::ffmpeg_context::FfmpegContext)—the central struct for assembling inputs, outputs, and filters.
19//! - [`scheduler`]: Defines [`FfmpegScheduler`](scheduler::ffmpeg_scheduler::FfmpegScheduler), managing the execution of an `FfmpegContext` pipeline.
20//! - [`container_info`]: Utilities to extract information about the container, such as duration and format details.
21//! - [`stream_info`]: Inspect media streams (e.g., find video/audio streams in a file).
22//! - [`device`]: Query audio/video input devices (cameras, microphones, etc.) on various platforms.
23//! - [`hwaccel`]: Helpers for hardware-accelerated encoding/decoding setup.
24//! - [`codec`]: Tools to discover which encoders/decoders your FFmpeg build supports.
25//! - [`filter`]: Query FFmpeg's built-in filters and infrastructure for building custom frame-processing filters.
26//!
27//! # Example Workflow
28//!
29//! 1. **Build a context** using [`FfmpegContext::builder()`](crate::core::context::ffmpeg_context::FfmpegContext::builder)
30//!    specifying your input, any filters, and your output.
31//! 2. **Create a scheduler** with [`FfmpegScheduler::new`](crate::core::scheduler::ffmpeg_scheduler::FfmpegScheduler::new),
32//!    then call `.start()` to begin processing.
33//! 3. **Wait** (or `.await` if `async` feature is enabled) for the job to complete. Use the returned
34//!    `Result` to detect success or failure.
35//!
36//! # Example
37//! ```rust,ignore
38//!
39//! fn main() -> Result<(), Box<dyn std::error::Error>> {
40//!     // 1. Build an FfmpegContext with an input, a simple filter, and an output
41//!     let context = FfmpegContext::builder()
42//!         .input("test.mp4")
43//!         .filter_desc("hue=s=0") // Example: desaturate video
44//!         .output("output.mp4")
45//!         .build()?;
46//!
47//!     // 2. Create a scheduler and start the job
48//!     let scheduler = FfmpegScheduler::new(context).start()?;
49//!
50//!     // 3. Block until it's finished
51//!     scheduler.wait()?;
52//!     Ok(())
53//! }
54//! ```
55
56/// The **context** module provides tools for assembling an entire FFmpeg pipeline,
57/// culminating in the [`FfmpegContext`](context::ffmpeg_context::FfmpegContext). This includes:
58///
59/// - **Inputs**: [`Input`](context::input::Input) objects representing files, URLs, or custom I/O callbacks.
60/// - **Outputs**: [`Output`](context::output::Output) objects representing target files, streams, or custom sinks.
61/// - **Filter Descriptions**: Simple inline filters via `filter_desc` or more complex
62///   [`FilterComplex`](context::filter_complex::FilterComplex) graphs.
63/// - **Builders**: e.g., [`FfmpegContextBuilder`](context::ffmpeg_context_builder::FfmpegContextBuilder) for constructing a complete context
64///   with multiple inputs, outputs, and filter settings.
65///
66/// Once you’ve built an [`FfmpegContext`](context::ffmpeg_context::FfmpegContext), you can execute it via the [`FfmpegScheduler`](scheduler::ffmpeg_scheduler::FfmpegScheduler).
67///
68/// # Example
69///
70/// ```rust,ignore
71/// // Build an FFmpeg context with one input, some filter settings, and one output.
72/// let context = FfmpegContext::builder()
73///     .input("test.mp4")
74///     .filter_desc("hue=s=0")
75///     .output("output.mp4")
76///     .build()
77///     .unwrap();
78/// // The context now holds all info needed for an FFmpeg job.
79/// ```
80pub mod context;
81
82/// Push raw video frames from Rust code into a full FFmpeg pipeline
83/// ([`VideoWriter`](writer::VideoWriter)).
84pub mod writer;
85
86/// Display matrix helpers shared by stream probing and filter graph setup.
87pub(crate) mod display;
88
89/// The **scheduler** module orchestrates the execution of a configured [`FfmpegContext`](context::ffmpeg_context::FfmpegContext).
90/// It provides the [`FfmpegScheduler`](scheduler::ffmpeg_scheduler::FfmpegScheduler) struct, which:
91///
92/// - **Starts** the FFmpeg pipeline via [`FfmpegScheduler::start()`](scheduler::ffmpeg_scheduler::FfmpegScheduler<crate::core::scheduler::ffmpeg_scheduler::Initialization>::start()).
93/// - **Manages** thread or subprocess creation, ensuring all streams and filters run.
94/// - **Waits** for completion (blocking or asynchronous, depending on whether the `async` feature is enabled).
95/// - **Returns** the final result, indicating success or failure.
96///
97/// # Synchronous Example
98///
99/// ```rust,ignore
100/// let context = FfmpegContext::builder()
101///     .input("test.mp4")
102///     .filter_desc("hue=s=0")
103///     .output("output.mp4")
104///     .build()
105///     .unwrap();
106///
107/// let result = FfmpegScheduler::new(context)
108///     .start()
109///     .unwrap()
110///     .wait();
111///
112/// assert!(result.is_ok(), "FFmpeg job failed unexpectedly");
113/// ```
114///
115/// # Asynchronous Example (requires `async` feature)
116///
117/// ```rust,ignore
118/// #[tokio::main]
119/// async fn main() {
120///     let context = FfmpegContext::builder()
121///         .input("test.mp4")
122///         .output("output.mp4")
123///         .build()
124///         .unwrap();
125///
126///     let mut scheduler = FfmpegScheduler::new(context)
127///         .start()
128///         .expect("Failed to start FFmpeg job");
129///
130///     // Asynchronous wait
131///     scheduler.await.expect("FFmpeg job failed unexpectedly");
132/// }
133/// ```
134pub mod scheduler;
135
136/// The **container_info** module provides utilities for retrieving metadata related to the media container,
137/// such as duration, format, and other general properties of the media file.
138///
139/// This module helps to query the overall properties of a media container file (e.g., `.mp4`, `.avi`, `.mkv`)
140/// without diving into individual streams (audio, video, etc.). It is useful when you need information
141/// about the file as a whole, such as total duration, format type, and container-specific properties.
142///
143/// # Examples
144///
145/// ```rust,ignore
146/// // Retrieve the duration in microseconds for the media file "test.mp4"
147/// let duration = get_duration_us("test.mp4").unwrap();
148/// println!("Duration: {} us", duration);
149///
150/// // Retrieve the format name for "test.mp4"
151/// let format = get_format("test.mp4").unwrap();
152/// println!("Format: {}", format);
153///
154/// // Retrieve the metadata for "test.mp4"
155/// let metadata = get_metadata("test.mp4").unwrap();
156/// for (key, value) in metadata {
157///     println!("{}: {}", key, value);
158/// }
159/// ```
160///
161/// These helper functions return the container-level metadata, and they handle any errors that may arise
162/// (e.g., if the file can't be opened or if there is an issue reading the data).
163pub mod container_info;
164
165/// The **stream_info** module provides utilities to retrieve detailed information
166/// about media streams (video, audio, and more) from an input source (e.g., a local file
167/// path, an RTMP URL, etc.). It queries FFmpeg for metadata regarding stream types, codec
168/// parameters, duration, and other relevant details.
169///
170/// # Examples
171///
172/// ```rust,ignore
173/// // Retrieve information about the first video stream in "test.mp4"
174/// let maybe_video_info = find_video_stream_info("test.mp4").unwrap();
175/// if let Some(video_info) = maybe_video_info {
176///     println!("Found video stream: {:?}", video_info);
177/// } else {
178///     println!("No video stream found.");
179/// }
180///
181/// // Retrieve information about the first audio stream in "test.mp4"
182/// let maybe_audio_info = find_audio_stream_info("test.mp4").unwrap();
183/// if let Some(audio_info) = maybe_audio_info {
184///     println!("Found audio stream: {:?}", audio_info);
185/// } else {
186///     println!("No audio stream found.");
187/// }
188///
189/// // Retrieve information about all streams (video, audio, etc.) in "test.mp4"
190/// let all_infos = find_all_stream_infos("test.mp4").unwrap();
191/// println!("Total streams found: {}", all_infos.len());
192/// for info in all_infos {
193///     println!("{:?}", info);
194/// }
195/// ```
196///
197/// These helper functions return `Result<Option<StreamInfo>, Error>` or `Result<Vec<StreamInfo>, Error>`
198/// depending on the call, allowing you to differentiate between "no stream found" (returns `Ok(None)`)
199/// and encountering an actual error (returns `Err(...)`).
200pub mod stream_info;
201
202/// The **packet_scanner** module provides a lightweight packet-level scanner for media files.
203///
204/// Unlike the full demuxing pipeline, `PacketScanner` iterates over raw demuxed packets
205/// without any decoding. This is useful for inspecting packet metadata such as timestamps,
206/// keyframe flags, sizes, and stream indices.
207///
208/// # Examples
209///
210/// ```rust,ignore
211/// use ez_ffmpeg::packet_scanner::PacketScanner;
212///
213/// let mut scanner = PacketScanner::open("test.mp4")?;
214/// for packet in scanner.packets() {
215///     let packet = packet?;
216///     println!(
217///         "stream={} pts={:?} size={} keyframe={}",
218///         packet.stream_index(),
219///         packet.pts(),
220///         packet.size(),
221///         packet.is_keyframe(),
222///     );
223/// }
224/// ```
225pub mod packet_scanner;
226
227/// The **packet_sink** module delivers **encoded packets** to user callbacks
228/// instead of muxing them into container bytes — the packet-domain
229/// counterpart of the byte-domain `write_callback` output.
230///
231/// Build a [`PacketSink`](packet_sink::PacketSink) (or a
232/// [`PacketSink::channel`](packet_sink::PacketSink::channel) adapter) and hand
233/// it to [`Output::new_by_packet_sink`](crate::Output::new_by_packet_sink).
234/// The v1 strict tier is WebCodecs-aligned: avcC H.264 access units
235/// (libx264) and raw AAC frames with their AudioSpecificConfig. See the
236/// module documentation for the callback order and the blocking-backpressure
237/// contract.
238pub mod packet_sink;
239
240/// The **device** module provides cross-platform methods to query available audio and video
241/// input devices on the system. Depending on the target operating system, it internally
242/// delegates to different platform APIs or FFmpeg’s device capabilities:
243///
244/// - **macOS**: Leverages AVFoundation for enumerating devices such as cameras ("vide")
245///   and microphones ("soun").
246/// - **Other OSes**: Uses FFmpeg’s `avdevice` to list input devices for video and audio.
247///
248/// These functions can be used to programmatically discover devices before choosing one
249/// for capture or recording in an FFmpeg-based pipeline.
250///
251/// # Examples
252///
253/// ```rust,ignore
254/// // Query video input devices (e.g., cameras)
255/// let video_devices = get_input_video_devices().unwrap();
256/// for device in &video_devices {
257///     println!("Available video device: {}", device);
258/// }
259///
260/// // Query audio input devices (e.g., microphones)
261/// let audio_devices = get_input_audio_devices().unwrap();
262/// for device in &audio_devices {
263///     println!("Available audio device: {}", device);
264/// }
265/// ```
266///
267/// # Notes
268///
269/// - If the query process fails (e.g., missing permissions or no devices available),
270///   the functions return an appropriate error from `crate::error`.
271/// - On macOS, the `AVFoundation` framework is used directly. On other platforms, FFmpeg’s
272///   `avdevice` functionality is used. Implementation details differ, but the returned
273///   results have a uniform format: a list of human-readable device names.
274/// - For more advanced device details (e.g., supported formats or resolutions), you may need
275///   to perform additional FFmpeg queries or platform-specific calls.
276pub mod device;
277/// The **hwaccel** module provides functionality for working with hardware-accelerated
278/// codecs in FFmpeg. It allows you to detect and configure various hardware devices
279/// (like NVENC, VAAPI, DXVA2, or VideoToolbox) so that FFmpeg can offload encoding or
280/// decoding tasks to GPU or specialized hardware.
281///
282/// # Public API
283///
284/// - [`get_hwaccels()`](hwaccel::get_hwaccels): Enumerates the hardware acceleration backends available on the
285///   current system, returning a list of [`HWAccelInfo`](hwaccel::HWAccelInfo) items. Each item contains a
286///   readable name (e.g., `"cuda"`, `"vaapi"`) and the corresponding `AVHWDeviceType`.
287///
288/// # Example
289///
290/// ```rust,ignore
291/// // Query hardware acceleration backends
292/// let hwaccels = get_hwaccels();
293/// for accel in hwaccels {
294///     println!("Found HW Accel: {} (type: {:?})", accel.name, accel.hw_device_type);
295/// }
296/// ```
297///
298/// # Notes
299///
300/// - While only [`get_hwaccels()`](hwaccel::get_hwaccels) is directly exposed, internally the module contains
301///   various helpers to initialize and manage hardware devices (e.g., `hw_device_init_from_string`).
302///   These are used behind the scenes or in more advanced scenarios where explicit control
303///   over device creation is required.
304/// - Hardware acceleration support depends on both FFmpeg’s compilation configuration
305///   and the underlying system drivers/frameworks. Not all listed accelerations may be
306///   fully functional on every platform.
307///
308/// # Device lifetime and caching
309///
310/// Hardware device contexts are expensive to create — initialization loads
311/// vendor drivers and libraries, with CUDA context creation the classic
312/// costly case — so ez-ffmpeg manages them with a process-global cache:
313///
314/// - A device context is created on first use of a given configuration
315///   (accel type plus device spec) and cached; later jobs requesting the
316///   same configuration reuse the cached context instead of creating a
317///   new one.
318/// - The cache is bounded at 32 entries; adding a further distinct
319///   configuration evicts the least-recently-requested one. Eviction
320///   releases only the cache's own handle: the underlying FFmpeg device
321///   context is reference-counted and stays alive until no codec, filter
322///   graph, or frame still uses it, so eviction never affects a running
323///   job.
324/// - All cache-owned handles are released at process exit.
325///
326/// There is deliberately no idle-timeout release: recreation is the
327/// expensive direction, so a cached context is kept until the 32-entry
328/// bound evicts it or the process exits (the ffmpeg CLI likewise keeps
329/// its devices until final cleanup).
330pub mod hwaccel;
331
332/// The **codec** module provides helpers for enumerating and querying FFmpeg’s
333/// available audio/video **encoders** and **decoders**. This can be useful for
334/// discovering which codecs are supported in your current FFmpeg build, along
335/// with their core attributes.
336///
337/// # Public API
338///
339/// - [`get_encoders()`](codec::get_encoders): Returns a list of [`CodecInfo`](codec::CodecInfo) representing all
340///   encoders (e.g., H.264, AAC) recognized by FFmpeg.
341/// - [`get_decoders()`](codec::get_decoders): Returns a list of [`CodecInfo`](codec::CodecInfo) representing all
342///   decoders (e.g., H.264, AAC) recognized by FFmpeg.
343///
344/// # Example
345///
346/// ```rust,ignore
347/// // List all available encoders
348/// let encoders = get_encoders();
349/// for enc in &encoders {
350///     println!("Encoder: {} - {}", enc.codec_name, enc.codec_long_name);
351/// }
352///
353/// // List all available decoders
354/// let decoders = get_decoders();
355/// for dec in &decoders {
356///     println!("Decoder: {} - {}", dec.codec_name, dec.codec_long_name);
357/// }
358/// ```
359///
360/// # Data Structures
361///
362/// - [`CodecInfo`](codec::CodecInfo): Contains user-friendly fields such as:
363///   - `codec_name` / `codec_long_name`
364///   - `desc_name`: The descriptor name from FFmpeg.
365///   - `media_type` (audio/video/subtitle, etc.)
366///   - `codec_id` (internal FFmpeg ID)
367///   - `codec_capabilities` (bitmask indicating codec features)
368///
369/// # Notes
370///
371/// - The underlying `Codec` struct is for internal usage only (`pub(crate)`,
372///   not part of the documented API), bridging to the raw FFmpeg APIs. In most
373///   cases, you only need the higher-level [`CodecInfo`](codec::CodecInfo)
374///   data from the public functions above.
375/// - The available encoders/decoders can vary depending on your FFmpeg build
376///   and any external libraries installed on the system.
377pub mod codec;
378
379/// The **capabilities** module provides lightweight probes for what the
380/// linked FFmpeg build contains — whether a muxer (output format) or an
381/// output-capable I/O protocol was compiled in. Use these before configuring
382/// outputs that depend on optional components (e.g. `whip`, `srt`) to fail
383/// fast with an actionable error instead of a mid-pipeline failure.
384///
385/// # Public API
386///
387/// - [`is_muxer_available()`](capabilities::is_muxer_available): Checks for a
388///   muxer by short name (e.g. `"matroska"`, `"mpegts"`, `"whip"`).
389/// - [`is_output_protocol_available()`](capabilities::is_output_protocol_available):
390///   Checks for an I/O protocol that supports writing (e.g. `"file"`, `"srt"`).
391///
392/// # Example
393///
394/// ```rust,ignore
395/// if !ez_ffmpeg::capabilities::is_muxer_available("whip") {
396///     eprintln!("this FFmpeg build lacks the whip muxer");
397/// }
398/// ```
399///
400/// # Notes
401///
402/// - A `true` result only means the component is registered in the linked
403///   FFmpeg build; encoders, TLS backends, endpoint compatibility, and
404///   network reachability are separate concerns.
405/// - Muxer names and protocol names are separate namespaces (the `srt`
406///   muxer is the SubRip subtitle format, not the SRT streaming protocol).
407pub mod capabilities;
408
409/// The **filter** module provides a flexible framework for custom frame processing
410/// within the FFmpeg pipeline, along with the ability to query FFmpeg's built-in filters.
411/// It introduces the [`FrameFilter`](filter::frame_filter::FrameFilter) trait, which defines how to apply transformations
412/// (e.g., scaling, color adjustments, GPU-accelerated effects) to decoded frames.
413/// You can attach these filters to either the input or the output side
414/// (depending on your desired pipeline design) so that frames are automatically
415/// processed in your FFmpeg workflow.
416///
417/// # FFmpeg Built-in Filters
418///
419/// ```rust,ignore
420/// use ez_ffmpeg::core::filter::get_filters;
421///
422/// // Query available FFmpeg filters
423/// let filters = get_filters();
424/// for filter in filters {
425///     println!("Filter: {} - {}", filter.name, filter.description);
426/// }
427/// ```
428///
429/// # Defining and Using a Custom Filter
430///
431/// Below is a minimal example showing how to implement a custom filter and attach it to
432/// an `Output` so that every frame is processed before encoding. You could likewise
433/// attach it to an `Input` if you want the frames processed immediately after decoding.
434///
435/// ```rust,ignore
436///
437/// // 1. Define your custom filter by implementing the FrameFilter trait.
438/// struct FlipFilter;
439///
440/// impl FrameFilter for FlipFilter {
441///     fn media_type(&self) -> AVMediaType {
442///         // This filter operates on video frames.
443///         AVMediaType::AVMEDIA_TYPE_VIDEO
444///     }
445///
446///     fn filter_frame(
447///         &mut self,
448///         mut frame: Frame,
449///         _ctx: &mut FrameFilterContext,
450///     ) -> Result<Option<Frame>, Box<dyn std::error::Error + Send + Sync>> {
451///         // Forward an end-of-stream flush marker straight through.
452///         if ez_ffmpeg::util::ffmpeg_utils::frame_is_eof_marker(&frame) {
453///             return Ok(Some(frame));
454///         }
455///
456///         // Here you would implement the logic to transform the frame.
457///         // As a trivial example, we just return the original frame.
458///         // (Replace this with your actual transformation code.)
459///
460///         Ok(Some(frame))
461///     }
462/// }
463///
464/// fn main() -> Result<(), Box<dyn std::error::Error>> {
465///     // 2. Create a pipeline builder for video frames.
466///     let mut pipeline_builder: FramePipelineBuilder = AVMediaType::AVMEDIA_TYPE_VIDEO.into();
467///
468///     // 3. Add your custom filter to the pipeline, giving it a unique name.
469///     pipeline_builder = pipeline_builder.filter("flip-filter", Box::new(FlipFilter));
470///
471///     // 4. Attach the pipeline to an Output (could also attach to an Input).
472///     let mut output: Output = "output.mp4".into();
473///     output.add_frame_pipeline(pipeline_builder);
474///
475///     // 5. Build the FFmpeg context with both input and output.
476///     let context = FfmpegContext::builder()
477///         .input("input.mp4")
478///         .output(output)
479///         .build()?;
480///
481///     // 6. Run the FFmpeg job via the scheduler.
482///     FfmpegScheduler::new(context)
483///         .start()?
484///         .wait()?;
485///
486///     Ok(())
487/// }
488/// ```
489///
490/// In this example:
491/// 1. We define a **`FlipFilter`** that implements the [`FrameFilter`](filter::frame_filter::FrameFilter) trait and specifies
492///    `AVMediaType::AVMEDIA_TYPE_VIDEO`.
493/// 2. We create a **`FramePipelineBuilder`** for `VIDEO` frames and add our filter to it.
494/// 3. We attach that pipeline to the **`Output`** configuration, so frames will be processed
495///    (in this case, “flipped”) before encoding.
496/// 4. Finally, we build the FFmpeg context and run it with the **`FfmpegScheduler`**.
497///
498/// # More Advanced Filters
499///
500/// For a more complex, GPU-accelerated example, see the wgpu-based filters in the
501/// `wgpu_filter` module (feature `"wgpu"`). There, you can use custom WGSL fragment
502/// shaders to apply sophisticated transformations or visual effects on video frames.
503/// (The former `opengl` module remains available but is deprecated.)
504///
505/// # Trait Overview
506///
507/// The [`FrameFilter`](filter::frame_filter::FrameFilter) trait exposes several methods you can override:
508/// - [`FrameFilter::media_type()`](filter::frame_filter::FrameFilter::media_type): Indicates which media type (video, audio, etc.) this filter handles.
509/// - [`FrameFilter::init()`](filter::frame_filter::FrameFilter::init): Called once when the filter is first created (e.g., allocate resources).
510/// - [`FrameFilter::filter_frame()`](filter::frame_filter::FrameFilter::filter_frame): The primary method for transforming an incoming frame.
511/// - [`FrameFilter::request_frame()`](filter::frame_filter::FrameFilter::request_frame): If your filter generates frames on its own, you can override this.
512/// - [`FrameFilter::uninit()`](filter::frame_filter::FrameFilter::uninit): Called during cleanup when the filter is removed or the pipeline ends.
513///
514/// By chaining multiple filters in a pipeline, you can create sophisticated processing
515/// chains for your media data.
516pub mod filter;
517
518/// The **metadata** module provides internal metadata handling for FFmpeg operations.
519///
520/// **Internal Use Only**: This module contains unsafe FFmpeg C API wrappers.
521/// Users should use the safe public API on `Output` instead:
522/// - `Output::add_metadata()` for global metadata
523/// - `Output::add_stream_metadata()` for stream metadata
524/// - `Output::map_metadata_from_input()` for metadata mapping
525/// - `Output::disable_auto_copy_metadata()` for controlling auto-copy
526///
527/// # Example
528/// ```rust,ignore
529/// let output = Output::from("output.mp4")
530///     .add_metadata("title", "My Video")
531///     .add_metadata("author", "John Doe")
532///     .add_stream_metadata("v:0", "language", "eng")?;
533/// ```
534pub(crate) mod metadata;
535
536/// The **analysis** module surfaces the results of FFmpeg detector/measurement
537/// filters (`blackdetect`, `silencedetect`, `scdet`, `cropdetect`, `ebur128`)
538/// as typed Rust events and a folded report, instead of only FFmpeg logs.
539pub mod analysis;
540
541/// The **recipes** module provides one-shot helpers for common workflows
542/// (thumbnails/sprite sheets, animated GIF export, HLS ABR ladders) built on
543/// top of the ez-ffmpeg builder. The raw `filter_desc` escape hatch remains
544/// available for anything these do not cover.
545pub mod recipes;
546
547pub mod frame_export;
548
549#[cfg(feature = "cli")]
550pub mod cli;
551
552static INIT_FFMPEG: std::sync::Once = std::sync::Once::new();
553
554extern "C" fn cleanup() {
555    let _ = std::panic::catch_unwind(|| {
556        hwaccel::hw_device_free_all();
557        unsafe {
558            ffmpeg_sys_next::avformat_network_deinit();
559        }
560
561        log::debug!("FFmpeg cleaned up");
562    });
563}
564
565// C adjusts an array-typed `va_list` parameter to a pointer. Bindgen preserves
566// that adjustment in FFmpeg's function signatures, while its public `va_list`
567// alias remains the original one-element array. Extract the generated element
568// type so the callback follows the same ABI without naming bindgen internals.
569#[cfg(any(
570    all(target_arch = "powerpc", not(target_os = "uefi"), not(windows)),
571    target_arch = "s390x",
572    all(target_arch = "x86_64", not(target_os = "uefi"), not(windows)),
573))]
574trait VaListArray {
575    type Element;
576}
577
578#[cfg(any(
579    all(target_arch = "powerpc", not(target_os = "uefi"), not(windows)),
580    target_arch = "s390x",
581    all(target_arch = "x86_64", not(target_os = "uefi"), not(windows)),
582))]
583impl<T, const N: usize> VaListArray for [T; N] {
584    type Element = T;
585}
586
587#[cfg(any(
588    all(target_arch = "powerpc", not(target_os = "uefi"), not(windows)),
589    target_arch = "s390x",
590    all(target_arch = "x86_64", not(target_os = "uefi"), not(windows)),
591))]
592type VaListType = *mut <ffmpeg_sys_next::va_list as VaListArray>::Element;
593
594#[cfg(not(any(
595    all(target_arch = "powerpc", not(target_os = "uefi"), not(windows)),
596    target_arch = "s390x",
597    all(target_arch = "x86_64", not(target_os = "uefi"), not(windows)),
598)))]
599type VaListType = ffmpeg_sys_next::va_list;
600
601/// Log target used for every message forwarded from FFmpeg, so applications
602/// can tune FFmpeg's verbosity independently of ez-ffmpeg's own logs,
603/// e.g. `RUST_LOG=ez_ffmpeg=info,ez_ffmpeg::ffmpeg=warn`.
604pub const FFMPEG_LOG_TARGET: &str = "ez_ffmpeg::ffmpeg";
605
606/// Highest FFmpeg log level forwarded to the Rust `log` facade.
607/// Defaults to [`FfmpegLogLevel::Info`], matching the historical behavior.
608static FFMPEG_LOG_MAX_LEVEL: std::sync::atomic::AtomicI32 =
609    std::sync::atomic::AtomicI32::new(ffmpeg_sys_next::AV_LOG_INFO);
610
611/// Verbosity levels of the FFmpeg logging system (mirrors `AV_LOG_*`).
612#[derive(Debug, Clone, Copy, PartialEq, Eq)]
613#[non_exhaustive]
614pub enum FfmpegLogLevel {
615    Quiet,
616    Panic,
617    Fatal,
618    Error,
619    Warning,
620    Info,
621    Verbose,
622    Debug,
623    Trace,
624}
625
626impl FfmpegLogLevel {
627    fn to_av_level(self) -> libc::c_int {
628        match self {
629            FfmpegLogLevel::Quiet => ffmpeg_sys_next::AV_LOG_QUIET,
630            FfmpegLogLevel::Panic => ffmpeg_sys_next::AV_LOG_PANIC,
631            FfmpegLogLevel::Fatal => ffmpeg_sys_next::AV_LOG_FATAL,
632            FfmpegLogLevel::Error => ffmpeg_sys_next::AV_LOG_ERROR,
633            FfmpegLogLevel::Warning => ffmpeg_sys_next::AV_LOG_WARNING,
634            FfmpegLogLevel::Info => ffmpeg_sys_next::AV_LOG_INFO,
635            FfmpegLogLevel::Verbose => ffmpeg_sys_next::AV_LOG_VERBOSE,
636            FfmpegLogLevel::Debug => ffmpeg_sys_next::AV_LOG_DEBUG,
637            FfmpegLogLevel::Trace => ffmpeg_sys_next::AV_LOG_TRACE,
638        }
639    }
640}
641
642/// Sets the highest FFmpeg log level forwarded to the `log` facade
643/// (under the [`FFMPEG_LOG_TARGET`] target).
644///
645/// Messages above this level are dropped before any formatting work.
646/// Defaults to [`FfmpegLogLevel::Info`]; raise to [`FfmpegLogLevel::Trace`]
647/// to receive FFmpeg's debug/trace diagnostics (they map to `log::trace!`),
648/// or lower to [`FfmpegLogLevel::Error`] to keep only errors.
649pub fn set_ffmpeg_log_level(level: FfmpegLogLevel) {
650    FFMPEG_LOG_MAX_LEVEL.store(level.to_av_level(), std::sync::atomic::Ordering::Relaxed);
651}
652
653fn av_level_to_rust(level: libc::c_int) -> log::Level {
654    if level <= ffmpeg_sys_next::AV_LOG_ERROR {
655        log::Level::Error
656    } else if level <= ffmpeg_sys_next::AV_LOG_WARNING {
657        log::Level::Warn
658    } else if level <= ffmpeg_sys_next::AV_LOG_INFO {
659        log::Level::Info
660    } else if level <= ffmpeg_sys_next::AV_LOG_VERBOSE {
661        log::Level::Debug
662    } else {
663        log::Level::Trace
664    }
665}
666
667/// Shared formatting state for [`ffmpeg_log_callback`].
668///
669/// `print_prefix` must survive across invocations: FFmpeg emits partial log
670/// lines (not ending in '\n') and uses this flag to decide whether the next
671/// chunk starts a new prefixed line; a per-call flag broke multi-part
672/// messages. The same lock serializes `av_log_format_line` across threads
673/// and carries the duplicate-folding state (`AV_LOG_SKIP_REPEATED`
674/// semantics), mirroring FFmpeg's own default callback (libavutil/log.c).
675struct FfmpegLogState {
676    print_prefix: libc::c_int,
677    last_level: libc::c_int,
678    repeated: u64,
679    last_msg: String,
680}
681
682static FFMPEG_LOG_STATE: std::sync::Mutex<FfmpegLogState> = std::sync::Mutex::new(FfmpegLogState {
683    print_prefix: 1,
684    last_level: ffmpeg_sys_next::AV_LOG_INFO,
685    repeated: 0,
686    last_msg: String::new(),
687});
688
689struct DeferredFfmpegLog {
690    level: log::Level,
691    message: String,
692}
693
694thread_local! {
695    static DEFERRED_FFMPEG_LOGS: std::cell::RefCell<Vec<Vec<DeferredFfmpegLog>>> =
696        const { std::cell::RefCell::new(Vec::new()) };
697}
698
699/// Defers FFmpeg callback records on this thread until [`flush`](Self::flush).
700/// Hardware-device creation uses this while holding its process-wide init
701/// lock because FFmpeg may log synchronously from inside the create call.
702pub(crate) struct FfmpegLogScope {
703    active: bool,
704    _thread_bound: std::marker::PhantomData<std::rc::Rc<()>>,
705}
706
707pub(crate) fn defer_ffmpeg_logs() -> FfmpegLogScope {
708    DEFERRED_FFMPEG_LOGS.with(|stacks| stacks.borrow_mut().push(Vec::new()));
709    FfmpegLogScope {
710        active: true,
711        _thread_bound: std::marker::PhantomData,
712    }
713}
714
715impl FfmpegLogScope {
716    /// Releases the deferral scope and emits its records in callback order.
717    /// Nested scopes append to their parent; only the outermost scope reaches
718    /// the user logger.
719    pub(crate) fn flush(mut self) {
720        let records = DEFERRED_FFMPEG_LOGS.with(|stacks| {
721            let mut stacks = stacks.borrow_mut();
722            let records = stacks.pop().expect("FFmpeg log deferral scope is active");
723            if let Some(parent) = stacks.last_mut() {
724                parent.extend(records);
725                Vec::new()
726            } else {
727                records
728            }
729        });
730        self.active = false;
731
732        for record in records {
733            log::log!(target: FFMPEG_LOG_TARGET, record.level, "{}", record.message);
734        }
735    }
736}
737
738impl Drop for FfmpegLogScope {
739    fn drop(&mut self) {
740        if self.active {
741            // Unwinding a protected operation must first release its lock;
742            // dispatching arbitrary logger code from this Drop could mask the
743            // original panic or abort on a second panic.
744            DEFERRED_FFMPEG_LOGS.with(|stacks| {
745                stacks.borrow_mut().pop();
746            });
747        }
748    }
749}
750
751fn defer_ffmpeg_log(level: log::Level, args: std::fmt::Arguments<'_>) -> bool {
752    DEFERRED_FFMPEG_LOGS.with(|stacks| {
753        let mut stacks = stacks.borrow_mut();
754        let Some(records) = stacks.last_mut() else {
755            return false;
756        };
757        records.push(DeferredFfmpegLog {
758            level,
759            message: args.to_string(),
760        });
761        true
762    })
763}
764
765unsafe extern "C" fn ffmpeg_log_callback(
766    ptr: *mut libc::c_void,
767    level: libc::c_int,
768    fmt: *const libc::c_char,
769    args: VaListType,
770) {
771    // Cheap early exits before any formatting: av_vlog does not filter by
772    // level for custom callbacks, so verbose/debug/trace chatter would
773    // otherwise be vsnprintf-formatted only to be thrown away.
774    if level > FFMPEG_LOG_MAX_LEVEL.load(std::sync::atomic::Ordering::Relaxed) {
775        return;
776    }
777    let rust_level = av_level_to_rust(level);
778    if rust_level > log::max_level() {
779        return;
780    }
781
782    // A panicked holder cannot exist (no panicking code below), but never
783    // propagate poisoning out of an extern "C" callback.
784    let mut state = FFMPEG_LOG_STATE
785        .lock()
786        .unwrap_or_else(|poisoned| poisoned.into_inner());
787
788    let mut buffer = [0u8; 1024];
789    ffmpeg_sys_next::av_log_format_line(
790        ptr,
791        level,
792        fmt,
793        args,
794        buffer.as_mut_ptr() as *mut libc::c_char,
795        buffer.len() as libc::c_int,
796        &mut state.print_prefix,
797    );
798
799    let Ok(msg) = std::ffi::CStr::from_ptr(buffer.as_ptr() as *const libc::c_char).to_str() else {
800        return;
801    };
802    let trimmed_msg = msg.trim_end_matches(['\n', '\r']);
803
804    // Fold consecutive duplicates, like ffmpeg CLI's AV_LOG_SKIP_REPEATED
805    // (e.g. per-frame h264 decode errors after a mid-GOP seek).
806    if level == state.last_level && trimmed_msg == state.last_msg {
807        state.repeated += 1;
808        return;
809    }
810    let flush_repeated = if state.repeated > 0 {
811        Some((av_level_to_rust(state.last_level), state.repeated))
812    } else {
813        None
814    };
815    state.repeated = 0;
816    state.last_level = level;
817    state.last_msg.clear();
818    state.last_msg.push_str(trimmed_msg);
819
820    // Emit outside the lock: a logger backend may itself call into FFmpeg
821    // (re-entering this callback and self-deadlocking the Mutex) or panic
822    // (which must not unwind while the state lock is held).
823    drop(state);
824
825    let emitted = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
826        if let Some((repeated_level, repeated)) = flush_repeated {
827            if !defer_ffmpeg_log(
828                repeated_level,
829                format_args!("FFmpeg: last message repeated {} times", repeated),
830            ) {
831                log::log!(
832                    target: FFMPEG_LOG_TARGET,
833                    repeated_level,
834                    "FFmpeg: last message repeated {} times",
835                    repeated
836                );
837            }
838        }
839        if !defer_ffmpeg_log(rust_level, format_args!("FFmpeg: {}", trimmed_msg)) {
840            log::log!(target: FFMPEG_LOG_TARGET, rust_level, "FFmpeg: {}", trimmed_msg);
841        }
842    }));
843    if let Err(payload) = emitted {
844        // A logger backend is user code. Keep its unwind inside this C
845        // callback, and publish the failure when FFmpeg called us from a
846        // scheduler-owned worker.
847        let reported = std::panic::catch_unwind(|| {
848            crate::util::thread_synchronizer::report_worker_callback_panic();
849        });
850        if let Err(report_payload) = reported {
851            crate::core::packet_sink::dispose_panic_payload(report_payload);
852        }
853        crate::core::packet_sink::dispose_panic_payload(payload);
854    }
855}
856
857fn initialize_ffmpeg() {
858    let mut first_init = false;
859    INIT_FFMPEG.call_once(|| {
860        unsafe {
861            libc::atexit(cleanup as extern "C" fn());
862            ffmpeg_sys_next::avdevice_register_all();
863            ffmpeg_sys_next::avformat_network_init();
864            ffmpeg_sys_next::av_log_set_callback(Some(ffmpeg_log_callback));
865        }
866        first_init = true;
867    });
868    // Log AFTER call_once: a panicking logger backend must not poison the
869    // Once (which would permanently fail every later FFmpeg entry point),
870    // and a logger that re-enters this crate must not deadlock the
871    // initializer. Same emit-outside-the-lock policy as ffmpeg_log_callback.
872    if first_init {
873        log::info!("FFmpeg initialized.");
874    }
875}