Purpose and Capabilities
libswscale provides:- Video Scaling: Resize video frames to different resolutions
- Color Space Conversion: Convert between pixel formats (RGB, YUV, etc.)
- Bit Depth Conversion: Convert between different bit depths
- Interlacing Support: Handle interlaced video
- Optimized Algorithms: Multiple scaling algorithms for quality/speed tradeoffs
- SIMD Optimization: CPU-specific optimizations (SSE, AVX, NEON)
Key Concepts
SwsContext
The main structure for scaling and conversion operations.- Source and destination image parameters
- Scaling algorithm configuration
- Conversion tables and lookup data
- Internal buffers for multi-pass operations
Pixel Formats
FFmpeg supports hundreds of pixel formats:- YUV Formats: YUV420P, YUV422P, YUV444P, NV12, NV21
- RGB Formats: RGB24, BGR24, RGBA, BGRA, RGB48
- Packed vs Planar: Data organization
- Bit Depths: 8-bit, 10-bit, 12-bit, 16-bit
Scaling Algorithms
Different algorithms offer quality/speed tradeoffs:| Algorithm | Quality | Speed | Use Case |
|---|---|---|---|
| Fast Bilinear | Low | Fastest | Real-time previews |
| Bilinear | Medium | Fast | General purpose |
| Bicubic | High | Medium | High quality scaling |
| Lanczos | Highest | Slowest | Professional quality |
| Point | N/A | Very Fast | Nearest neighbor (no interpolation) |
Core Functions
Context Management
sws_getContext()
Allocates and initializes a SwsContext.srcW,srcH: Source width and heightsrcFormat: Source pixel formatdstW,dstH: Destination width and heightdstFormat: Destination pixel formatflags: Scaling algorithm flagssrcFilter,dstFilter: Custom filters (usually NULL)param: Algorithm-specific parameters (usually NULL)
sws_getCachedContext()
Reuses or reallocates a scaling context.sws_freeContext()
Frees a SwsContext.sws_free_context()
Modern API to free context (sets pointer to NULL).Modern Frame API
sws_scale_frame()
Scales an AVFrame (recommended modern API).c: SwsContext (can be NULL for automatic context creation)dst: Destination framesrc: Source frame
sws_frame_setup()
Prepares a scaling context from frame parameters.sws_is_noop()
Checks if scaling would be a no-op.Legacy Scaling API
sws_scale()
Scales image data (legacy API, still widely used).c: SwsContextsrcSlice: Source data planessrcStride: Source line sizessrcSliceY: Starting Y position in sourcesrcSliceH: Height of source slicedst: Destination data planesdstStride: Destination line sizes
Format Testing
sws_test_format()
Tests if a pixel format is supported.format: Pixel format to testoutput: 1 for output support, 0 for input support
sws_isSupportedInput()
Checks if format is supported as input.sws_isSupportedOutput()
Checks if format is supported as output.Color Space Functions
sws_getCoefficients()
Gets color space conversion coefficients.sws_setColorspaceDetails()
Sets color space conversion parameters.inv_table: Inverse color conversion matrix for inputsrcRange: Input color range (0=MPEG, 1=JPEG)table: Color conversion matrix for outputdstRange: Output color rangebrightness,contrast,saturation: Adjustments
Scaling Flags
Algorithm Flags
Additional Flags
Usage Patterns
Simple Scaling
Color Space Conversion
Batch Processing with Cached Context
Thumbnail Generation
Performance Optimization
Choose Appropriate Algorithm
Choose Appropriate Algorithm
Use SWS_FAST_BILINEAR for real-time processing, SWS_BICUBIC for general use, and SWS_LANCZOS for best quality offline processing.
Reuse Contexts
Reuse Contexts
Use
sws_getCachedContext() when processing multiple frames with similar parameters to avoid reinitialization overhead.Minimize Format Conversions
Minimize Format Conversions
Keep video in native format as long as possible. Only convert when necessary (e.g., for display or encoding).
SIMD Utilization
SIMD Utilization
libswscale automatically uses CPU-specific optimizations. Ensure your FFmpeg build includes SIMD support for your platform.
Common Pixel Formats
YUV Formats
| Format | Description | Subsampling | Planes |
|---|---|---|---|
| YUV420P | Planar 4:2:0 | 2x2 | 3 |
| YUV422P | Planar 4:2:2 | 2x1 | 3 |
| YUV444P | Planar 4:4:4 | None | 3 |
| NV12 | Semi-planar 4:2:0 | 2x2 | 2 |
| NV21 | Semi-planar 4:2:0 | 2x2 | 2 |
RGB Formats
| Format | Description | Bits/Pixel | Alpha |
|---|---|---|---|
| RGB24 | Packed RGB | 24 | No |
| BGR24 | Packed BGR | 24 | No |
| RGBA | Packed RGBA | 32 | Yes |
| BGRA | Packed BGRA | 32 | Yes |
| RGB48LE | 16-bit RGB | 48 | No |
Best Practices
Check Format Support
Check Format Support
Use
sws_test_format() to verify format support before creating contexts.Handle Color Spaces
Handle Color Spaces
Use appropriate color space conversion matrices (BT.601, BT.709, BT.2020) based on your source material.
Maintain Aspect Ratio
Maintain Aspect Ratio
Calculate destination dimensions to maintain aspect ratio:
dst_h = dst_w * src_h / src_wUse Modern API
Use Modern API
Prefer
sws_scale_frame() for new code as it’s simpler and handles frame metadata automatically.Color Space Constants
See Also
- libavfilter - Alternative for complex video processing
- libavutil - Pixel format definitions and frame handling
- libavcodec - Source of decoded frames