playa_ffmpeg/software/mod.rs
1//! Software scaling and resampling.
2//!
3//! This module provides CPU-based media processing for format conversion and resizing.
4//! It wraps FFmpeg's `libswscale` (video scaling) and `libswresample` (audio resampling).
5//!
6//! # Scaling (Video)
7//!
8//! The [`scaling`] module provides video frame scaling (resizing) and pixel format conversion.
9//! Common uses include:
10//! - Resizing video to different resolutions
11//! - Converting between pixel formats (YUV ↔ RGB, different bit depths)
12//! - Aspect ratio changes
13//!
14//! # Resampling (Audio)
15//!
16//! The [`resampling`] module provides audio sample rate conversion and format changes.
17//! Common uses include:
18//! - Changing sample rate (e.g., 48kHz → 44.1kHz)
19//! - Converting sample formats (s16 → f32, planar ↔ packed)
20//! - Channel layout conversion (stereo → 5.1)
21
22#[cfg(feature = "software-scaling")]
23pub mod scaling;
24
25/// Creates a video scaler for resizing frames.
26///
27/// Convenience function for creating a scaling context that changes resolution
28/// but preserves pixel format.
29///
30/// # Parameters
31///
32/// * `format` - Pixel format (same for input and output)
33/// * `flags` - Scaling algorithm flags (quality vs. speed)
34/// * `(in_width, in_height)` - Input dimensions
35/// * `(out_width, out_height)` - Output dimensions
36#[cfg(feature = "software-scaling")]
37#[inline]
38pub fn scaler(format: crate::format::Pixel, flags: scaling::Flags, (in_width, in_height): (u32, u32), (out_width, out_height): (u32, u32)) -> Result<scaling::Context, crate::Error> {
39 scaling::Context::get(format, in_width, in_height, format, out_width, out_height, flags)
40}
41
42/// Creates a pixel format converter.
43///
44/// Convenience function for converting between pixel formats without changing resolution.
45/// Uses fast bilinear scaling (though no actual scaling occurs).
46///
47/// # Parameters
48///
49/// * `(width, height)` - Frame dimensions (unchanged)
50/// * `input` - Input pixel format
51/// * `output` - Output pixel format
52#[cfg(feature = "software-scaling")]
53#[inline]
54pub fn converter((width, height): (u32, u32), input: crate::format::Pixel, output: crate::format::Pixel) -> Result<scaling::Context, crate::Error> {
55 scaling::Context::get(input, width, height, output, width, height, scaling::flag::Flags::FAST_BILINEAR)
56}
57
58#[cfg(feature = "software-resampling")]
59pub mod resampling;
60
61/// Creates an audio resampler.
62///
63/// Convenience function for creating a resampling context that handles sample rate,
64/// format, and channel layout conversion.
65///
66/// # Parameters
67///
68/// * `(in_format, in_layout, in_rate)` - Input sample format, channel layout, and sample rate
69/// * `(out_format, out_layout, out_rate)` - Output sample format, channel layout, and sample rate
70#[cfg(feature = "software-resampling")]
71#[inline]
72pub fn resampler((in_format, in_layout, in_rate): (crate::util::format::Sample, crate::ChannelLayout, u32), (out_format, out_layout, out_rate): (crate::util::format::Sample, crate::ChannelLayout, u32)) -> Result<resampling::Context, crate::Error> {
73 resampling::Context::get(in_format, in_layout, in_rate, out_format, out_layout, out_rate)
74}