Skip to content

Commit 5319e51

Browse files
committed
fix compile errors except for ffmpeg
1 parent 6db4a46 commit 5319e51

13 files changed

Lines changed: 279 additions & 410 deletions

File tree

‎nokhwa-core/src/decoder.rs‎

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use bytemuck::try_cast_slice_mut;
88
use crate::pixel_destination::PixelDestination;
99

1010
pub trait Decoder {
11-
type Config: Clone + Debug;
11+
type Config: Clone + Debug + ConfigHasResolution;
1212
type OutputMeta: Clone + Debug;
1313
const SUPPORTED_DESTINATIONS: &'static [PixelDestination];
1414

@@ -27,10 +27,7 @@ pub trait Decoder {
2727
where
2828
<P as Pixel>::Subpixel: NonFloatScalarWidth
2929
{
30-
let destination = match PixelDestination::get_by_pixel::<P>() {
31-
Some(dest) => dest,
32-
None => return Err(NokhwaError::DecoderUnknownDestinationPixelFormat(P::COLOR_MODEL, P::Subpixel::WIDTH_BYTES))
33-
};
30+
let Some(destination) = PixelDestination::get_by_pixel::<P>() else { return Err(NokhwaError::DecoderUnknownDestinationPixelFormat(P::COLOR_MODEL, P::Subpixel::WIDTH_BYTES)) };
3431

3532
if !Self::supports_destination(destination) {
3633
return Err(NokhwaError::DecoderUnsupportedDestinationPixelFormat(destination))
@@ -43,13 +40,29 @@ pub trait Decoder {
4340

4441
self.decode_to_buffer(to_decode, cast_slice, destination)
4542
}
46-
4743
fn decode<P: Pixel>(
4844
&mut self,
49-
to_decode: FrameBuffer,
45+
to_decode: FrameBuffer<'_>,
5046
) -> Result<DecodedImage<P, Self::OutputMeta>, NokhwaError>
5147
where
52-
<P as Pixel>::Subpixel: NonFloatScalarWidth;
48+
<P as Pixel>::Subpixel: NonFloatScalarWidth,
49+
{
50+
let resolution = self.config().resolution();
51+
let min_size_alloc = self.output_decoder_min_size_pixel::<P>(resolution)?;
52+
let mut out_buffer: Vec<P::Subpixel> = vec![P::Subpixel::DEFAULT_MIN_VALUE; min_size_alloc];
53+
let meta = self.decode_to_pixel_buffer::<P>(to_decode, &mut out_buffer)?;
54+
Ok(DecodedImage::new(
55+
ImageBuffer::from_vec(
56+
resolution.width(),
57+
resolution.height(),
58+
out_buffer,
59+
)
60+
.ok_or(NokhwaError::Decoder(
61+
"failed to convert into an image buffer".to_string(),
62+
))?,
63+
meta,
64+
))
65+
}
5366

5467
fn output_decoder_min_size_pixel<P>(&self, resolution: Resolution) -> Result<usize, NokhwaError> where
5568
P: Pixel,
@@ -58,9 +71,29 @@ pub trait Decoder {
5871

5972
}
6073

61-
fn output_decoder_min_size(&self, resolution: Resolution, destination_format: PixelDestination) -> Result<usize, NokhwaError>;
74+
fn output_decoder_min_size(&self, resolution: Resolution, destination_format: PixelDestination) -> Result<usize, NokhwaError> {
75+
if !Self::supports_destination(destination_format) {
76+
return Err(NokhwaError::DecoderUnsupportedDestinationPixelFormat(destination_format))
77+
}
6278

79+
let px_size = match destination_format {
80+
PixelDestination::Rgb8 | PixelDestination::Bgr8 => 3_u32,
81+
PixelDestination::Rgba8 | PixelDestination::Bgra8 | PixelDestination::LumaA16 => 4_u32,
82+
PixelDestination::Rgb16 | PixelDestination::Bgr16 => 3_u32 * 2_u32,
83+
PixelDestination::Rgba16 | PixelDestination::Bgra16 => 4_u32 * 2_u32,
84+
PixelDestination::Luma8 => 1_u32,
85+
PixelDestination::LumaA8 | PixelDestination::Luma16 => 2_u32,
86+
};
87+
let reso = resolution.width() * resolution.height();
88+
Ok((reso as usize) * (px_size as usize))
89+
}
90+
91+
#[must_use]
6392
fn supports_destination(pixel_destination: PixelDestination) -> bool {
6493
Self::SUPPORTED_DESTINATIONS.contains(&pixel_destination)
6594
}
6695
}
96+
97+
pub trait ConfigHasResolution {
98+
fn resolution(&self) -> Resolution;
99+
}

‎nokhwa-core/src/frame_format.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ define_frame_format_with_groups! {
206206
}
207207

208208
impl FrameFormat {
209+
#[must_use]
209210
pub fn is_custom(&self) -> bool {
210211
if let FrameFormat::Custom(_) = self {
211212
return true

‎nokhwa-core/src/pixel_destination.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub enum PixelDestination {
1919
}
2020

2121
impl PixelDestination {
22+
#[must_use]
2223
pub fn get_by_pixel<P>() -> Option<Self>
2324
where
2425
P: Pixel,

‎nokhwa-core/src/stream.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ impl<'a> StreamHandle<'a> {
165165
return Err(NokhwaError::ReadFrameError(why));
166166
}
167167
}
168-
Event::Error(e) => return Err(NokhwaError::ReadFrameError(e.to_string())),
168+
Event::Error(e) => return Err(NokhwaError::ReadFrameError(e.clone())),
169169
_ => {}
170170
}
171171
}

‎nokhwa-core/src/types.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ impl CameraIndex {
4343
pub fn as_string(&self) -> String {
4444
match self {
4545
CameraIndex::Index(i) => i.to_string(),
46-
CameraIndex::String(s) | CameraIndex::Stable(s) => s.to_string(),
46+
CameraIndex::String(s) | CameraIndex::Stable(s) => s.clone(),
4747
}
4848
}
4949

‎nokhwa-decoders/Cargo.toml‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ edition = "2024"
77
ffmpeg = ["ffmpeg-the-third"]
88
yuyv = ["dcv-color-primitives", "yuv"]
99
mjpeg = ["zune-jpeg", "zune-core"]
10-
luma = ["itertools", "nokhwa-iter-extensions", "itermore"]
11-
#static = ["ffmpeg-the-third/static"]
10+
luma = ["nokhwa-iter-extensions", "itermore"]
11+
static = ["ffmpeg-the-third/static"]
1212
#async = []
1313

1414
[dependencies]
@@ -19,7 +19,7 @@ version = "0.2"
1919
path = "../nokhwa-core"
2020

2121
[dependencies.ffmpeg-the-third]
22-
version = "4.0.0+ffmpeg-8.0"
22+
version = "4.0.0"
2323
optional = true
2424

2525
[dependencies.yuv]
@@ -38,10 +38,6 @@ optional = true
3838
version = "0.5.0-rc2"
3939
optional = true
4040

41-
[dependencies.itertools]
42-
version = "0.14"
43-
optional = true
44-
4541
[dependencies.itermore]
4642
version = "0.8"
4743
features = ["array_chunks"]

‎nokhwa-decoders/src/ffmpeg.rs‎

Lines changed: 12 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ use ffmpeg_the_third::ffi::{
1212
use ffmpeg_the_third::packet::{Borrow, Ref};
1313
use ffmpeg_the_third::{Frame, decoder, packet::Packet};
1414
use nokhwa_core::codec::Codec;
15-
use nokhwa_core::decoder::{Decoder, ImageBuffer, Pixel, Primitive};
15+
use nokhwa_core::decoder::{ConfigHasResolution, Decoder, Pixel};
1616
use nokhwa_core::error::NokhwaError;
1717
use nokhwa_core::frame_buffer::FrameBuffer;
1818
use nokhwa_core::frame_format::{CustomFrameFormat, FrameFormat};
19-
use nokhwa_core::image::{DecodedImage, NonFloatScalarWidth};
19+
use nokhwa_core::image::{NonFloatScalarWidth};
20+
use nokhwa_core::pixel_destination::PixelDestination;
2021
use nokhwa_core::types::{CameraFormat, FrameRate, Resolution};
2122

2223
pub struct FfmpegDecoder {
@@ -56,7 +57,8 @@ impl FfmpegDecoder {
5657
impl Decoder for FfmpegDecoder {
5758
type Config = <FfmpegCodec as Codec>::Config;
5859
type OutputMeta = <FfmpegCodec as Codec>::WrittenMeta;
59-
type DestinationFormatHint = AVPixelFormat;
60+
const SUPPORTED_DESTINATIONS: &'static [PixelDestination] = &[];
61+
6062

6163
fn config(&self) -> &Self::Config {
6264
self.codec.config()
@@ -71,7 +73,7 @@ impl Decoder for FfmpegDecoder {
7173
&mut self,
7274
to_decode: FrameBuffer,
7375
mut buffer: impl AsMut<[u8]>,
74-
_destination_format: Option<Self::DestinationFormatHint>,
76+
_destination_format: PixelDestination,
7577
) -> Result<Self::OutputMeta, NokhwaError> {
7678
// TODO: add an extra zippy happy path for rgb/bgr/luma
7779
let (frame, metadata) = self.receive_decoded_frame(to_decode)?;
@@ -188,54 +190,6 @@ impl Decoder for FfmpegDecoder {
188190
Ok(decoded_meta)
189191
}
190192
}
191-
192-
fn decode<P: Pixel>(
193-
&mut self,
194-
to_decode: FrameBuffer,
195-
) -> Result<DecodedImage<P, Self::OutputMeta>, NokhwaError>
196-
where
197-
<P as Pixel>::Subpixel: NonFloatScalarWidth,
198-
{
199-
let min_size = self.output_decoder_min_size_pixel::<P>(self.config().resolution);
200-
let mut buffer: Vec<P::Subpixel> = vec![<P::Subpixel>::DEFAULT_MIN_VALUE; min_size];
201-
let meta = self.decode_to_buffer(
202-
to_decode,
203-
try_cast_slice_mut(&mut buffer)
204-
.map_err(|why| NokhwaError::DecoderInvalidBuffer(why.to_string()))?,
205-
None,
206-
)?;
207-
Ok(DecodedImage::new(
208-
ImageBuffer::from_vec(
209-
self.codec.config.resolution.width(),
210-
self.codec.config.resolution.height(),
211-
buffer,
212-
)
213-
.ok_or(NokhwaError::Decoder(
214-
"Failed to create Image Buffer".to_string(),
215-
))?,
216-
meta,
217-
))
218-
}
219-
220-
fn output_decoder_min_size(
221-
&self,
222-
resolution: Resolution,
223-
destination_format: Self::DestinationFormatHint,
224-
) -> usize {
225-
let size = unsafe {
226-
av_image_get_buffer_size(
227-
destination_format,
228-
resolution.width() as i32,
229-
resolution.height() as i32,
230-
1,
231-
)
232-
};
233-
size as usize
234-
}
235-
236-
fn buffer_takes_destination_hint(&self) -> bool {
237-
false
238-
}
239193
}
240194

241195
fn create_sws_context(
@@ -261,53 +215,6 @@ fn create_sws_context(
261215
Ok(new_sws)
262216
}
263217

264-
fn pixel_to_destination_px_fmt<P: Pixel>() -> Option<AVPixelFormat>
265-
where
266-
<P as Pixel>::Subpixel: NonFloatScalarWidth,
267-
{
268-
match P::COLOR_MODEL {
269-
"RGB" => match <<P as Pixel>::Subpixel>::WIDTH_BYTES {
270-
1 => Some(AVPixelFormat::AV_PIX_FMT_RGB24),
271-
_ => None,
272-
},
273-
274-
"RGBA" => match <<P as Pixel>::Subpixel>::WIDTH_BYTES {
275-
1 => Some(AVPixelFormat::AV_PIX_FMT_RGBA),
276-
2 => Some(switch_endian(
277-
AVPixelFormat::AV_PIX_FMT_RGBA64LE,
278-
AVPixelFormat::AV_PIX_FMT_RGBA64BE,
279-
)),
280-
_ => None,
281-
},
282-
"BGR" => match <<P as Pixel>::Subpixel>::WIDTH_BYTES {
283-
1 => Some(AVPixelFormat::AV_PIX_FMT_BGR24),
284-
_ => None,
285-
},
286-
287-
"BGRA" => match <<P as Pixel>::Subpixel>::WIDTH_BYTES {
288-
1 => Some(AVPixelFormat::AV_PIX_FMT_BGRA),
289-
2 => Some(switch_endian(
290-
AVPixelFormat::AV_PIX_FMT_BGRA64LE,
291-
AVPixelFormat::AV_PIX_FMT_BGRA64BE,
292-
)),
293-
_ => None,
294-
},
295-
"Y" => match <<P as Pixel>::Subpixel>::WIDTH_BYTES {
296-
1 => Some(AVPixelFormat::AV_PIX_FMT_GRAY8),
297-
2 => Some(switch_endian(
298-
AVPixelFormat::AV_PIX_FMT_GRAY16LE,
299-
AVPixelFormat::AV_PIX_FMT_GRAY16BE,
300-
)),
301-
_ => None,
302-
},
303-
"YA" => match <<P as Pixel>::Subpixel>::WIDTH_BYTES {
304-
1 => Some(AVPixelFormat::AV_PIX_FMT_GRAY8A),
305-
_ => None,
306-
},
307-
_ => None,
308-
}
309-
}
310-
311218
pub struct Sws {
312219
pub sws: *mut SwsContext,
313220
pub source_pixel_format: AVPixelFormat,
@@ -686,6 +593,12 @@ pub struct FfmpegDecoderConfig {
686593
pub video_delay: i32,
687594
}
688595

596+
impl ConfigHasResolution for FfmpegDecoderConfig {
597+
fn resolution(&self) -> Resolution {
598+
self.resolution
599+
}
600+
}
601+
689602
impl FfmpegDecoderConfig {
690603
pub fn with_camera_format(camera_format: &CameraFormat) -> Self {
691604
FfmpegDecoderConfig::from(*camera_format)

0 commit comments

Comments
 (0)