Skip to content

Commit

Permalink
finish decoder and frame format searcher implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
l1npengtul committed Apr 16, 2024
1 parent 32c8a9e commit fd5559a
Show file tree
Hide file tree
Showing 9 changed files with 959 additions and 1,258 deletions.
1,713 changes: 725 additions & 988 deletions Cargo.lock

Large diffs are not rendered by default.

19 changes: 10 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ input-native = ["input-avfoundation", "input-v4l", "input-msmf"]
# Re-enable it once soundness has been proven + mozjpeg is updated to 0.9.x
# input-uvc = ["uvc", "uvc/vendor", "usb_enumeration", "lazy_static"]
input-opencv = ["opencv", "opencv/rgb", "rgb", "nokhwa-core/opencv-mat"]
# FIXME: Change me back to web-sys being optional! People will be mad otherwise peg!
input-jscam = [ "wasm-bindgen-futures", "wasm-rs-async-executor", "output-async"]
input-jscam = [ "wasm-bindgen-futures", "wasm-rs-async-executor", "output-async", "js-sys", "web-sys"]
output-wgpu = ["wgpu", "nokhwa-core/wgpu-types"]
#output-wasm = ["input-jscam"]
output-threaded = []
Expand All @@ -46,11 +45,11 @@ thiserror = "1.0"
paste = "1.0"

[dependencies.mozjpeg]
version = "0.9"
version = "0.10"
optional = true

[dependencies.dcv-color-primitives]
version = "0.5"
version = "0.6"
optional = true

[dependencies.nokhwa-core]
Expand All @@ -62,23 +61,23 @@ version = "1.0"
optional = true

[dependencies.flume]
version = "0.10"
version = "0.11"
optional = true

[dependencies.image]
version = "0.24"
version = "0.25"
default-features = false

[dependencies.usb_enumeration]
version = "0.2"
optional = true

[dependencies.wgpu]
version = "0.17"
version = "0.19"
optional = true

[dependencies.opencv]
version = "0.84"
version = "0.89"
default-features = false
features = ["videoio"]
optional = true
Expand Down Expand Up @@ -106,7 +105,6 @@ optional = true
version = "1.7"
optional = true

# TODO: Change me back!
[dependencies.web-sys]
version = "0.3"
features = [
Expand All @@ -126,13 +124,16 @@ features = [
"Plugin", "PluginArray",
"Window"
]
optional = true

# FIXME: Change me back! Pls! REMEMBER PEG!
[dependencies.js-sys]
version = "0.3"
optional = true

[dependencies.wasm-bindgen]
version = "0.2"
optional = true

[dependencies.wasm-bindgen-futures]
version = "0.4"
Expand Down
4 changes: 2 additions & 2 deletions nokhwa-bindings-linux/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ version = "0.2"
path = "../nokhwa-core"

[target.'cfg(target_os="linux")'.dependencies]
v4l = "0.13"
v4l2-sys-mit = "0.2"
v4l = "0.14"
v4l2-sys-mit = "0.3"
16 changes: 10 additions & 6 deletions nokhwa-core/src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@
* limitations under the License.
*/

use crate::{frame_format::SourceFrameFormat, types::Resolution};
use crate::{ types::Resolution};
use bytes::Bytes;
use image::ImageBuffer;
use crate::error::NokhwaError;

/// A buffer returned by a camera to accommodate custom decoding.
/// Contains information of Resolution, the buffer's [`FrameFormat`], and the buffer.
Expand All @@ -27,14 +25,14 @@ use crate::error::NokhwaError;
pub struct Buffer {
resolution: Resolution,
buffer: Bytes,
source_frame_format: SourceFrameFormat,
source_frame_format: FrameFormat,
}

impl Buffer {
/// Creates a new buffer with a [`&[u8]`].
#[must_use]
#[inline]
pub fn new(res: Resolution, buf: &[u8], source_frame_format: SourceFrameFormat) -> Self {
pub fn new(res: Resolution, buf: &[u8], source_frame_format: FrameFormat) -> Self {
Self {
resolution: res,
buffer: Bytes::copy_from_slice(buf),
Expand Down Expand Up @@ -62,13 +60,19 @@ impl Buffer {

/// Get the [`SourceFrameFormat`] of this buffer.
#[must_use]
pub fn source_frame_format(&self) -> SourceFrameFormat {
pub fn source_frame_format(&self) -> FrameFormat {
self.source_frame_format
}
}

#[cfg(feature = "opencv-mat")]
use crate::error::NokhwaError;
#[cfg(feature = "opencv-mat")]
use image::ImageBuffer;

#[cfg(feature = "opencv-mat")]
impl Buffer {

/// Decodes a image with allocation using the provided [`FormatDecoder`].
/// # Errors
/// Will error when the decoding fails.
Expand Down
34 changes: 16 additions & 18 deletions nokhwa-core/src/decoder.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use std::ops::Deref;
use image::{ImageBuffer, Pixel};
use serde::de::Error;
use crate::buffer::Buffer;
use crate::frame_format::FrameFormat;

Expand All @@ -9,19 +7,19 @@ pub trait Decoder {
/// Formats that the decoder can decode.
const ALLOWED_FORMATS: &'static FrameFormat;
/// Output pixel type (e.g. [`Rgb<u8>`](image::Rgb))
type Pixel: Pixel;
/// Container for [`Self::Pixel`] - must have the same [`Pixel::Subpixel`]
type Container: Deref<Target = [Pixel::Subpixel]>;
type OutputPixels: Pixel;

type PixelContainer;
/// Error that the decoder will output (use [`NokhwaError`] if you're not sure)
type Error: Error;
type Error;

/// Decode function.
fn decode(&mut self, buffer: Buffer) -> Result<ImageBuffer<Self::Pixel, Self::Container>, Self::Error>;
fn decode(&mut self, buffer: Buffer) -> Result<ImageBuffer<Self::OutputPixels, Self::PixelContainer>, Self::Error>;

/// Decode to user-provided Buffer
///
/// Incase that the buffer is not large enough this should error.
fn decode_buffer(&mut self, buffer: &mut [Pixel::Subpixel]) -> Result<(), Self::Error>;
fn decode_buffer(&mut self, buffer: &mut [<<Self as Decoder>::OutputPixels as Pixel>::Subpixel]) -> Result<(), Self::Error>;

/// Decoder Predicted Size
fn predicted_size_of_frame(&mut self, ) -> Option<usize>;
Expand All @@ -31,46 +29,46 @@ pub trait Decoder {
///
/// This is useful for times that a simple function is all that is required.
pub trait StaticDecoder: Decoder {
fn decode_static(buffer: Buffer) -> Result<ImageBuffer<Self::Pixel, Self::Container>, Self::Error>;
fn decode_static(buffer: Buffer) -> Result<ImageBuffer<Self::OutputPixels, Self::PixelContainer>, Self::Error>;

fn decode_static_to_buffer(buffer: &mut [Pixel::Subpixel]) -> Result<(), Self::Error>;
fn decode_static_to_buffer(buffer: &mut [<<Self as Decoder>::OutputPixels as Pixel>::Subpixel]) -> Result<(), Self::Error>;
}

/// Decoder that does not change its internal state.
pub trait IdemptDecoder: Decoder {
/// Decoder that does not change its internal state.
fn decode_nm(&self, buffer: Buffer) -> Result<ImageBuffer<Self::Pixel, Self::Container>, Self::Error>;
fn decode_nm(&self, buffer: Buffer) -> Result<ImageBuffer<Self::OutputPixels, Self::PixelContainer>, Self::Error>;

/// Decoder that does not change its internal state, decoding to a user provided buffer.
fn decode_nm_to_buffer(&self, buffer: &mut [Pixel::Subpixel]) -> Result<(), Self::Error>;
fn decode_nm_to_buffer(&self, buffer: &mut [<<Self as Decoder>::OutputPixels as Pixel>::Subpixel]) -> Result<(), Self::Error>;
}

#[cfg(feature = "async")]
#[cfg_attr(feature = "async", async_trait::async_trait)]
pub trait AsyncDecoder: Decoder {
/// Asynchronous decoder
async fn decode_async(&mut self, buffer: Buffer) -> Result<ImageBuffer<Self::Pixel, Self::Container>, Self::Error>;
async fn decode_async(&mut self, buffer: Buffer) -> Result<ImageBuffer<Self::OutputPixels, Self::PixelContainer>, Self::Error>;

/// Asynchronous decoder to user buffer.
async fn decode_buffer(&mut self, buffer: &mut [Pixel::Subpixel]) -> Result<(), Self::Error>;
async fn decode_buffer(&mut self, buffer: &mut [Self::OutputPixels::Subpixel]) -> Result<(), Self::Error>;
}

#[cfg(feature = "async")]
#[cfg_attr(feature = "async", async_trait::async_trait)]
pub trait AsyncStaticDecoder: Decoder {
/// Asynchronous decoder
async fn decode_static_async(buffer: Buffer) -> Result<ImageBuffer<Self::Pixel, Self::Container>, Self::Error>;
async fn decode_static_async(buffer: Buffer) -> Result<ImageBuffer<Self::OutputPixels, Self::PixelContainer>, Self::Error>;

/// Asynchronous decoder to user buffer.
async fn decode_static_buffer(buffer: &mut [Pixel::Subpixel]) -> Result<(), Self::Error>;
async fn decode_static_buffer(buffer: &mut [Self::OutputPixels::Subpixel]) -> Result<(), Self::Error>;
}

#[cfg(feature = "async")]
#[cfg_attr(feature = "async", async_trait::async_trait)]
pub trait AsyncIdemptDecoder: Decoder {
/// Asynchronous decoder
async fn decode_nm_async(&self, buffer: Buffer) -> Result<ImageBuffer<Self::Pixel, Self::Container>, Self::Error>;
async fn decode_nm_async(&self, buffer: Buffer) -> Result<ImageBuffer<Self::OutputPixels, Self::PixelContainer>, Self::Error>;

/// Asynchronous decoder to user buffer.
async fn decode_nm_buffer(&self, buffer: &mut [Pixel::Subpixel]) -> Result<(), Self::Error>;
async fn decode_nm_buffer(&self, buffer: &mut [Self::OutputPixels::Subpixel]) -> Result<(), Self::Error>;
}
Loading

0 comments on commit fd5559a

Please sign in to comment.