Skip to content

Commit 3b0ae53

Browse files
committed
mjpeg, yuv test
1 parent 68716fc commit 3b0ae53

6 files changed

Lines changed: 1115 additions & 530 deletions

File tree

‎nokhwa-core/src/decoder.rs‎

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use crate::error::NokhwaError;
22
use crate::frame_buffer::FrameBuffer;
33
use crate::image::{DecodedImage, NonFloatScalarWidth};
4-
use crate::types::{Resolution};
4+
use crate::pixel_destination::PixelDestination;
5+
use crate::types::Resolution;
6+
use bytemuck::try_cast_slice_mut;
57
pub use image::{ImageBuffer, Pixel, Primitive};
68
use std::fmt::Debug;
7-
use bytemuck::try_cast_slice_mut;
8-
use crate::pixel_destination::PixelDestination;
99

1010
pub trait Decoder {
1111
type Config: Clone + Debug + ConfigHasResolution;
@@ -23,14 +23,25 @@ pub trait Decoder {
2323
destination_format: PixelDestination,
2424
) -> Result<Self::OutputMeta, NokhwaError>;
2525

26-
fn decode_to_pixel_buffer<P: Pixel>(&mut self, to_decode: FrameBuffer, mut buffer: impl AsMut<[P::Subpixel]>) -> Result<Self::OutputMeta, NokhwaError>
26+
fn decode_to_pixel_buffer<P: Pixel>(
27+
&mut self,
28+
to_decode: FrameBuffer,
29+
mut buffer: impl AsMut<[P::Subpixel]>,
30+
) -> Result<Self::OutputMeta, NokhwaError>
2731
where
28-
<P as Pixel>::Subpixel: NonFloatScalarWidth
32+
<P as Pixel>::Subpixel: NonFloatScalarWidth,
2933
{
30-
let Some(destination) = PixelDestination::get_by_pixel::<P>() else { return Err(NokhwaError::DecoderUnknownDestinationPixelFormat(P::COLOR_MODEL, P::Subpixel::WIDTH_BYTES)) };
34+
let Some(destination) = PixelDestination::get_by_pixel::<P>() else {
35+
return Err(NokhwaError::DecoderUnknownDestinationPixelFormat(
36+
P::COLOR_MODEL,
37+
P::Subpixel::WIDTH_BYTES,
38+
));
39+
};
3140

3241
if !Self::supports_destination(destination) {
33-
return Err(NokhwaError::DecoderUnsupportedDestinationPixelFormat(destination))
42+
return Err(NokhwaError::DecoderUnsupportedDestinationPixelFormat(
43+
destination,
44+
));
3445
}
3546

3647
let buffer = buffer.as_mut();
@@ -52,28 +63,35 @@ pub trait Decoder {
5263
let mut out_buffer: Vec<P::Subpixel> = vec![P::Subpixel::DEFAULT_MIN_VALUE; min_size_alloc];
5364
let meta = self.decode_to_pixel_buffer::<P>(to_decode, &mut out_buffer)?;
5465
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-
))?,
66+
ImageBuffer::from_vec(resolution.width(), resolution.height(), out_buffer).ok_or(
67+
NokhwaError::Decoder("failed to convert into an image buffer".to_string()),
68+
)?,
6369
meta,
6470
))
6571
}
66-
67-
fn output_decoder_min_size_pixel<P>(&self, resolution: Resolution) -> Result<usize, NokhwaError> where
68-
P: Pixel,
69-
<P as Pixel>::Subpixel: NonFloatScalarWidth {
70-
PixelDestination::get_by_pixel::<P>().map(|dest| self.output_decoder_min_size(resolution, dest)).ok_or(NokhwaError::DecoderUnknownDestinationPixelFormat(P::COLOR_MODEL, P::Subpixel::WIDTH_BYTES))?
7172

73+
fn output_decoder_min_size_pixel<P>(&self, resolution: Resolution) -> Result<usize, NokhwaError>
74+
where
75+
P: Pixel,
76+
<P as Pixel>::Subpixel: NonFloatScalarWidth,
77+
{
78+
PixelDestination::get_by_pixel::<P>()
79+
.map(|dest| self.output_decoder_min_size(resolution, dest))
80+
.ok_or(NokhwaError::DecoderUnknownDestinationPixelFormat(
81+
P::COLOR_MODEL,
82+
P::Subpixel::WIDTH_BYTES,
83+
))?
7284
}
73-
74-
fn output_decoder_min_size(&self, resolution: Resolution, destination_format: PixelDestination) -> Result<usize, NokhwaError> {
85+
86+
fn output_decoder_min_size(
87+
&self,
88+
resolution: Resolution,
89+
destination_format: PixelDestination,
90+
) -> Result<usize, NokhwaError> {
7591
if !Self::supports_destination(destination_format) {
76-
return Err(NokhwaError::DecoderUnsupportedDestinationPixelFormat(destination_format))
92+
return Err(NokhwaError::DecoderUnsupportedDestinationPixelFormat(
93+
destination_format,
94+
));
7795
}
7896

7997
let px_size = match destination_format {
@@ -83,7 +101,7 @@ pub trait Decoder {
83101
PixelDestination::Rgba16 | PixelDestination::Bgra16 => 4_u32 * 2_u32,
84102
PixelDestination::Luma8 => 1_u32,
85103
PixelDestination::LumaA8 | PixelDestination::Luma16 => 2_u32,
86-
};
104+
};
87105
let reso = resolution.width() * resolution.height();
88106
Ok((reso as usize) * (px_size as usize))
89107
}

‎nokhwa-core/src/frame_buffer.rs‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,24 @@ impl Deref for FrameBuffer<'_> {
146146
self.buffer.as_ref()
147147
}
148148
}
149+
150+
impl From<Vec<u8>> for FrameBuffer<'_> {
151+
fn from(value: Vec<u8>) -> Self {
152+
let buffer = Cow::Owned(value);
153+
154+
FrameBuffer {
155+
buffer,
156+
metadata: None,
157+
}
158+
}
159+
}
160+
161+
impl<'a> From<&'a [u8]> for FrameBuffer<'a> {
162+
fn from(value: &'a [u8]) -> Self {
163+
let buffer = Cow::Borrowed(value);
164+
FrameBuffer {
165+
buffer,
166+
metadata: None,
167+
}
168+
}
169+
}

‎nokhwa-decoders/Cargo.toml‎

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,53 @@
11
[package]
2-
name = "nokhwa-decoders"
2+
name = "nokhwa-decoders"
33
version = "0.1.0"
44
edition = "2024"
55

66
[features]
7-
ffmpeg = ["ffmpeg-the-third"]
8-
yuyv = ["dcv-color-primitives", "yuv"]
9-
mjpeg = ["zune-jpeg", "zune-core"]
10-
luma = ["nokhwa-iter-extensions", "itermore"]
11-
static = ["ffmpeg-the-third/static"]
7+
ffmpeg = [ "ffmpeg-the-third" ]
8+
yuyv = [ "dcv-color-primitives", "yuv" ]
9+
mjpeg = [ "zune-jpeg", "zune-core" ]
10+
luma = [ "nokhwa-iter-extensions", "itermore" ]
11+
# static = ["ffmpeg-the-third/static"]
1212
#async = []
1313

1414
[dependencies]
1515
bytemuck = "1.23"
1616

1717
[dependencies.nokhwa-core]
1818
version = "0.2"
19-
path = "../nokhwa-core"
19+
path = "../nokhwa-core"
2020

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

2525
[dependencies.yuv]
26-
version = "0.8"
26+
version = "0.8"
2727
optional = true
2828

2929
[dependencies.dcv-color-primitives]
30-
version = "0.7"
30+
version = "0.7"
3131
optional = true
3232

3333
[dependencies.zune-jpeg]
34-
version = "0.5.0-rc9"
34+
version = "0.5.0-rc9"
3535
optional = true
3636

3737
[dependencies.zune-core]
38-
version = "0.5.0-rc2"
38+
version = "0.5.0-rc2"
3939
optional = true
4040

4141
[dependencies.itermore]
42-
version = "0.8"
43-
features = ["array_chunks"]
42+
version = "0.8"
43+
features = [ "array_chunks" ]
4444
optional = true
4545

4646
[dependencies.nokhwa-iter-extensions]
47-
version = "0.1"
48-
path = "../nokhwa-iter-extensions"
47+
version = "0.1"
48+
path = "../nokhwa-iter-extensions"
4949
optional = true
5050

5151
[dev-dependencies.image]
5252
workspace = true
53-
features = ["png", "jpeg", "avif"]
53+
features = [ "png", "jpeg", "avif" ]

0 commit comments

Comments
 (0)