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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
//! 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)
}
/// Convert RGB48LE data (u16 per channel) to destination format (YUV420P10LE, YUV422P10LE)
///
/// Used for 10-bit encoding pipeline. Handles 16-bit RGB data and converts to 10-bit YUV.
/// Reuses internal swscale context. Recreates if dimensions change.
///
/// # Arguments
/// * `rgb48_data` - RGB48LE pixel data (width * height * 3 u16 values, little-endian)
/// * `width` - Frame width
/// * `height` - Frame height
///
/// # Returns
/// FFmpeg video frame in destination format (10-bit YUV) ready for encoding
pub fn convert_rgb48(
&mut self,
rgb48_data: &[u16],
width: u32,
height: u32,
) -> Result<ffmpeg::util::frame::video::Video, String> {
// Validate input size (3 u16 values per pixel = RGB)
let expected_size = (width * height * 3) as usize;
if rgb48_data.len() != expected_size {
return Err(format!(
"Invalid RGB48 data size: expected {} u16 values, got {}",
expected_size,
rgb48_data.len()
));
}
// Recreate context if dimensions changed
if self.width != width || self.height != height {
self.recreate(width, height)?;
}
// Create source RGB48LE frame (48-bit RGB, little-endian)
let mut src_frame = ffmpeg::util::frame::video::Video::new(
ffmpeg::format::Pixel::RGB48LE,
width,
height,
);
// Copy RGB48 data to source frame (u16 → bytes, little-endian)
let src_stride = src_frame.stride(0);
let row_pixels = width as usize;
{
let dst_data = src_frame.data_mut(0);
for y in 0..height as usize {
for x in 0..row_pixels {
let pixel_idx = (y * row_pixels + x) * 3; // 3 u16 per pixel
let dst_offset = y * src_stride + x * 6; // 6 bytes per pixel (3 * u16)
// Write R, G, B as little-endian u16
let r = rgb48_data[pixel_idx];
let g = rgb48_data[pixel_idx + 1];
let b = rgb48_data[pixel_idx + 2];
dst_data[dst_offset..dst_offset + 2].copy_from_slice(&r.to_le_bytes());
dst_data[dst_offset + 2..dst_offset + 4].copy_from_slice(&g.to_le_bytes());
dst_data[dst_offset + 4..dst_offset + 6].copy_from_slice(&b.to_le_bytes());
}
}
}
// Create destination frame with configured format (YUV420P10LE / YUV422P10LE)
let mut dst_frame = ffmpeg::util::frame::video::Video::new(
self.dst_format,
width,
height,
);
// Convert RGB48LE → YUV10 using swscale context
self.ctx
.as_mut()
.unwrap()
.run(&src_frame, &mut dst_frame)
.map_err(|e| format!("RGB48→YUV10 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(())
}
}