playa_ffmpeg/filter/mod.rs
1//! Audio and video filtering.
2//!
3//! This module provides filtering capabilities for audio and video streams.
4//! Filters can modify, analyze, or transform media frames (scaling, cropping,
5//! color correction, audio mixing, etc.).
6//!
7//! It wraps FFmpeg's `libavfilter` library.
8//!
9//! # Main Components
10//!
11//! - [`Graph`] - Filter graph connecting multiple filters
12//! - [`Filter`] - Individual filter definition (scale, crop, overlay, etc.)
13//! - [`Context`] - Instance of a filter within a graph
14//! - [`Pad`] - Input/output connection point on a filter
15//!
16//! # Usage
17//!
18//! Filters are organized into graphs. Create a graph, add filters, link them together,
19//! then push frames through the graph to process them.
20//!
21//! Common use cases include video scaling, format conversion, overlay composition,
22//! and audio mixing.
23
24pub mod flag;
25pub use self::flag::Flags;
26
27pub mod pad;
28pub use self::pad::Pad;
29
30pub mod filter;
31pub use self::filter::Filter;
32
33pub mod context;
34pub use self::context::{Context, Sink, Source};
35
36pub mod graph;
37pub use self::graph::Graph;
38
39use std::{
40 ffi::{CStr, CString},
41 str::from_utf8_unchecked,
42};
43
44#[cfg(not(feature = "ffmpeg_5_0"))]
45use crate::Error;
46use crate::ffi::*;
47
48/// Registers all available filters (FFmpeg < 5.0 only).
49///
50/// In FFmpeg 5.0+, filters are automatically registered.
51/// Called automatically by [`crate::init()`].
52#[cfg(not(feature = "ffmpeg_5_0"))]
53pub fn register_all() {
54 unsafe {
55 avfilter_register_all();
56 }
57}
58
59/// Registers a specific filter (FFmpeg < 5.0 only).
60///
61/// Most users should rely on [`register_all()`] or automatic registration.
62#[cfg(not(feature = "ffmpeg_5_0"))]
63pub fn register(filter: &Filter) -> Result<(), Error> {
64 unsafe {
65 match avfilter_register(filter.as_ptr() as *mut _) {
66 0 => Ok(()),
67 _ => Err(Error::InvalidData),
68 }
69 }
70}
71
72/// Returns the libavfilter version number.
73pub fn version() -> u32 {
74 unsafe { avfilter_version() }
75}
76
77/// Returns the libavfilter build configuration string.
78pub fn configuration() -> &'static str {
79 unsafe { from_utf8_unchecked(CStr::from_ptr(avfilter_configuration()).to_bytes()) }
80}
81
82/// Returns the libavfilter license string.
83pub fn license() -> &'static str {
84 unsafe { from_utf8_unchecked(CStr::from_ptr(avfilter_license()).to_bytes()) }
85}
86
87/// Finds a filter by name.
88///
89/// Returns `None` if the filter doesn't exist.
90///
91/// # Examples
92///
93/// Common filter names: `"scale"`, `"crop"`, `"overlay"`, `"fps"`, `"format"`,
94/// `"aresample"`, `"volume"`, `"concat"`.
95pub fn find(name: &str) -> Option<Filter> {
96 unsafe {
97 let name = CString::new(name).unwrap();
98 let ptr = avfilter_get_by_name(name.as_ptr());
99
100 if ptr.is_null() { None } else { Some(Filter::wrap(ptr as *mut _)) }
101 }
102}
103
104#[cfg(test)]
105mod tests {
106 use super::*;
107
108 #[test]
109 fn test_paditer() {
110 #[cfg(not(feature = "ffmpeg_5_0"))]
111 register_all();
112 assert_eq!(find("overlay").unwrap().inputs().unwrap().map(|input| input.name().unwrap().to_string()).collect::<Vec<_>>(), vec!("main", "overlay"));
113 }
114}