1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//! Frame format conversion utilities
//!
//! Provides efficient FFmpeg swscale-based conversion between pixel formats.
//! Reuses swscale contexts to avoid expensive recreations.
use playa_ffmpeg as ffmpeg;
/// Reusable swscale context for efficient format conversions
pub struct SwsContext {
ctx: Option<ffmpeg::software::scaling::Context>,
src_format: ffmpeg::format::Pixel,
dst_format: ffmpeg::format::Pixel,
width: u32,
height: u32,
}
impl SwsContext {
/// Create new swscale context with custom formats
pub fn new(
src_format: ffmpeg::format::Pixel,
dst_format: ffmpeg::format::Pixel,
width: u32,
height: u32,
) -> Result<Self, String> {
let ctx = ffmpeg::software::scaling::Context::get(
src_format,
width,
height,
dst_format,
width,
height,
ffmpeg::software::scaling::Flags::BILINEAR,
)
.map_err(|e| format!("Failed to create swscale context: {}", e))?;
Ok(Self {
ctx: Some(ctx),
src_format,
dst_format,
width,
height,
})
}
/// Convert RGB24 data to destination format (YUV420P, YUV422P10, etc.)
///
/// Uses the destination format specified during SwsContext creation.
/// Reuses internal swscale context. Recreates if dimensions change.
///
/// # Arguments
/// * `rgb24_data` - RGB24 pixel data (width * height * 3 bytes)
/// * `width` - Frame width
/// * `height` - Frame height
///
/// # Returns
/// FFmpeg video frame in destination format ready for encoding
pub fn convert(
&mut self,
rgb24_data: &[u8],
width: u32,
height: u32,
) -> Result<ffmpeg::util::frame::video::Video, String> {
// Validate input size
let expected_size = (width * height * 3) as usize;
if rgb24_data.len() != expected_size {
return Err(format!(
"Invalid RGB24 data size: expected {} bytes, got {}",
expected_size,
rgb24_data.len()
));
}
// Recreate context if dimensions changed
if self.width != width || self.height != height {
self.recreate(width, height)?;
}
// Create source RGB24 frame
let mut src_frame = ffmpeg::util::frame::video::Video::new(
self.src_format,
width,
height,
);
// Copy RGB24 data to source frame
let src_stride = src_frame.stride(0);
let row_bytes = (width * 3) as usize;
{
let dst_data = src_frame.data_mut(0);
for y in 0..height as usize {
let src_offset = y * row_bytes;
let dst_offset = y * src_stride;
dst_data[dst_offset..dst_offset + row_bytes]
.copy_from_slice(&rgb24_data[src_offset..src_offset + row_bytes]);
}
}
// Create destination frame with configured format
let mut dst_frame = ffmpeg::util::frame::video::Video::new(
self.dst_format,
width,
height,
);
// Convert using swscale context
self.ctx
.as_mut()
.unwrap()
.run(&src_frame, &mut dst_frame)
.map_err(|e| format!("swscale conversion failed: {}", e))?;
Ok(dst_frame)
}
/// Recreate swscale context with new dimensions
fn recreate(&mut self, width: u32, height: u32) -> Result<(), String> {
self.ctx = Some(
ffmpeg::software::scaling::Context::get(
self.src_format,
width,
height,
self.dst_format,
width,
height,
ffmpeg::software::scaling::Flags::BILINEAR,
)
.map_err(|e| format!("Failed to recreate swscale context: {}", e))?,
);
self.width = width;
self.height = height;
Ok(())
}
}