Browse Source

app: add ravid video codec decoder wrapper

darkfi 7 tháng trước cách đây
mục cha
commit
37a5402606

+ 1 - 4
bin/app/src/plugin/mod.rs

@@ -26,10 +26,7 @@ pub mod fud;
 pub use fud::FudPluginPtr as FudPtr;
 
 #[cfg(feature = "enable-plugins")]
-pub use {
-    darkirc::DarkIrc,
-    fud::FudPlugin
-};
+pub use {darkirc::DarkIrc, fud::FudPlugin};
 
 use darkfi::net::Settings as NetSettings;
 

+ 99 - 0
bin/app/src/video/decoder.rs

@@ -0,0 +1,99 @@
+/* This file is part of DarkFi (https://dark.fi)
+ *
+ * Copyright (C) 2020-2025 Dyne.org foundation
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ */
+
+//! rav1d AV1 video decoder wrapper
+//!
+//! This module provides a Rust wrapper around the rav1d AV1 decoder.
+
+use rav1d::{Decoder as Rav1dDecoderInner, Picture, PlanarImageComponent, Rav1dError};
+
+use super::yuv_conv::yuv420p_to_rgba;
+
+/// A decoded frame containing RGBA data
+#[derive(Debug, Clone)]
+pub struct DecodedFrame {
+    /// Frame width in pixels
+    pub width: u32,
+    /// Frame height in pixels
+    pub height: u32,
+    /// RGBA pixel data (width * height * 4 bytes)
+    pub data: Vec<u8>,
+}
+
+/// rav1d AV1 video decoder wrapper
+///
+/// This wraps the rav1d decoder and provides automatic YUV to RGBA conversion.
+pub struct Rav1dDecoder {
+    /// Inner decoder from rav1d
+    decoder: Rav1dDecoderInner,
+}
+
+impl Rav1dDecoder {
+    pub fn new() -> Self {
+        Self { decoder: Rav1dDecoderInner::new().unwrap() }
+    }
+
+    /// Decode AV1 bitstream data
+    pub fn decode(&mut self, data: &[u8]) -> Result<DecodedFrame, Rav1dError> {
+        // Send data to decoder
+        // Need to copy data because send_data requires 'static ownership
+        let data = data.to_vec();
+        match self.decoder.send_data(data, None, None, None) {
+            Ok(_) => {}
+            Err(Rav1dError::TryAgain) => {
+                // Pending data - try to send it again
+                while let Err(Rav1dError::TryAgain) = self.decoder.send_pending_data() {
+                    // Continue sending pending data
+                }
+            }
+            Err(err) => return Err(err),
+        }
+
+        self.get_pic()
+    }
+
+    fn get_pic(&mut self) -> Result<DecodedFrame, Rav1dError> {
+        self.decoder.get_picture().map(|pic| Self::conv(pic))
+    }
+
+    /// Convert a rav1d Picture to RGBA
+    fn conv(pic: Picture) -> DecodedFrame {
+        let y_plane = pic.plane(PlanarImageComponent::Y);
+        let u_plane = pic.plane(PlanarImageComponent::U);
+        let v_plane = pic.plane(PlanarImageComponent::V);
+
+        let y_stride = pic.stride(PlanarImageComponent::Y) as usize;
+        let u_stride = pic.stride(PlanarImageComponent::U) as usize;
+        let v_stride = pic.stride(PlanarImageComponent::V) as usize;
+
+        let width = pic.width() as usize;
+        let height = pic.height() as usize;
+
+        let data = yuv420p_to_rgba(
+            &y_plane, &u_plane, &v_plane, width, height, y_stride, u_stride, v_stride,
+        );
+
+        DecodedFrame { width: width as u32, height: height as u32, data }
+    }
+
+    /// Flush the decoder to get any remaining frames
+    pub fn flush(&mut self) -> Result<DecodedFrame, Rav1dError> {
+        self.decoder.flush();
+        self.get_pic()
+    }
+}

+ 1 - 8
bin/app/src/video/ivf.rs

@@ -39,14 +39,11 @@ pub enum IvfError {
 
     #[error("Unexpected end of file")]
     UnexpectedEof,
-
-    #[error("Invalid frame size: {0}")]
-    InvalidFrameSize(u32),
 }
 
 impl From<std::io::Error> for IvfError {
     fn from(_: std::io::Error) -> Self {
-        IvfError::UnexpectedEof
+        Self::UnexpectedEof
     }
 }
 
@@ -90,10 +87,6 @@ pub struct IvfDemuxer {
 impl IvfDemuxer {
     /// Create a new IVF demuxer from raw bytes
     pub fn from_bytes(data: Vec<u8>) -> IvfResult<Self> {
-        if data.len() < 32 {
-            return Err(IvfError::UnexpectedEof);
-        }
-
         let mut self_ = Self {
             cur: Cursor::new(data),
             header: unsafe { std::mem::zeroed() },

+ 3 - 3
bin/app/src/video/mod.rs

@@ -16,9 +16,9 @@
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 
-pub mod ivf;
-pub mod yuv_conv;
+mod decoder;
+mod ivf;
+mod yuv_conv;
 
 pub use ivf::{IvfDemuxer, IvfError, IvfResult};
 pub use yuv_conv::yuv420p_to_rgba;
-

+ 1 - 8
bin/app/src/video/yuv_conv.rs

@@ -114,14 +114,7 @@ mod tests {
         let v_stride = width / 2;
 
         let rgba = yuv420p_to_rgba(
-            &y_plane,
-            &u_plane,
-            &v_plane,
-            width,
-            height,
-            y_stride,
-            u_stride,
-            v_stride,
+            &y_plane, &u_plane, &v_plane, width, height, y_stride, u_stride, v_stride,
         );
 
         // Check output size