|
1 | 1 | #[cfg(feature = "x264-video-encoder")] |
2 | 2 | mod ve_x264; |
3 | 3 |
|
4 | | -use derive_setters::Setters; |
5 | | -#[cfg(feature = "x264-video-encoder")] |
6 | | -pub use ve_x264::*; |
7 | | - |
8 | 4 | #[cfg(feature = "openh264-video-encoder")] |
9 | 5 | mod ve_openh264; |
10 | 6 |
|
11 | | -#[cfg(feature = "openh264-video-encoder")] |
12 | | -pub use ve_openh264::*; |
13 | | - |
14 | 7 | use crate::{FPS, RecorderError, recorder::ResizedImageBuffer}; |
| 8 | +use derive_setters::Setters; |
15 | 9 |
|
16 | 10 | #[derive(Debug, Clone)] |
17 | 11 | pub enum EncodedFrame { |
@@ -54,48 +48,10 @@ impl VideoEncoderConfig { |
54 | 48 |
|
55 | 49 | pub fn new(config: VideoEncoderConfig) -> Result<Box<dyn VideoEncoder>, RecorderError> { |
56 | 50 | #[cfg(feature = "x264-video-encoder")] |
57 | | - let ve = X264VideoEncoder::new(config)?; |
58 | | - |
59 | | - Ok(Box::new(ve)) |
60 | | -} |
| 51 | + let ve = ve_x264::X264VideoEncoder::new(config)?; |
61 | 52 |
|
62 | | -pub fn rgb_to_i420_yuv(rgb_data: &[u8], width: u32, height: u32) -> Result<Vec<u8>, RecorderError> { |
63 | | - use yuv::{ |
64 | | - YuvChromaSubsampling, YuvConversionMode, YuvPlanarImageMut, YuvRange, YuvStandardMatrix, |
65 | | - rgb_to_yuv420, |
66 | | - }; |
| 53 | + #[cfg(feature = "openh264-video-encoder")] |
| 54 | + let ve = ve_openh264::OpenH264VideoEncoder::new(config)?; |
67 | 55 |
|
68 | | - let frame_size = (width * height) as usize; |
69 | | - |
70 | | - // Allocate YUV planar image |
71 | | - let mut planar_image = |
72 | | - YuvPlanarImageMut::<u8>::alloc(width, height, YuvChromaSubsampling::Yuv420); |
73 | | - |
74 | | - // Convert RGB to YUV420 |
75 | | - rgb_to_yuv420( |
76 | | - &mut planar_image, |
77 | | - rgb_data, |
78 | | - width * 3, // RGB stride (3 bytes per pixel) |
79 | | - YuvRange::Limited, |
80 | | - YuvStandardMatrix::Bt601, |
81 | | - YuvConversionMode::Balanced, |
82 | | - ) |
83 | | - .map_err(|e| { |
84 | | - RecorderError::ImageProcessingFailed(format!("RGB to YUV conversion failed: {:?}", e)) |
85 | | - })?; |
86 | | - |
87 | | - // Extract the YUV data from the planar image |
88 | | - let mut yuv_data = vec![0u8; frame_size * 3 / 2]; |
89 | | - |
90 | | - // Copy Y plane |
91 | | - yuv_data[0..frame_size].copy_from_slice(planar_image.y_plane.borrow()); |
92 | | - |
93 | | - // Copy U plane |
94 | | - let u_plane_end = frame_size + frame_size / 4; |
95 | | - yuv_data[frame_size..u_plane_end].copy_from_slice(planar_image.u_plane.borrow()); |
96 | | - |
97 | | - // Copy V plane |
98 | | - yuv_data[u_plane_end..].copy_from_slice(planar_image.v_plane.borrow()); |
99 | | - |
100 | | - Ok(yuv_data) |
| 56 | + Ok(Box::new(ve)) |
101 | 57 | } |
0 commit comments