From 7ccf789b73a46d12e53b37b7416a4e66c4526374 Mon Sep 17 00:00:00 2001 From: Kostya Shishkov Date: Sat, 7 Mar 2026 15:12:32 +0100 Subject: [PATCH] h264: implement decoder wrappers --- .../src/codecs/h264/baseline/decoder_mt.rs | 8 +- .../src/codecs/h264/baseline/decoder_st.rs | 8 +- .../src/codecs/h264/baseline/dispatch.rs | 3 +- nihav-itu/src/codecs/h264/baseline/mod.rs | 6 +- nihav-itu/src/codecs/h264/mod.rs | 78 ++++++++++++++++++- 5 files changed, 85 insertions(+), 18 deletions(-) diff --git a/nihav-itu/src/codecs/h264/baseline/decoder_mt.rs b/nihav-itu/src/codecs/h264/baseline/decoder_mt.rs index 216b810..d5323f6 100644 --- a/nihav-itu/src/codecs/h264/baseline/decoder_mt.rs +++ b/nihav-itu/src/codecs/h264/baseline/decoder_mt.rs @@ -472,7 +472,7 @@ impl FrameDecoder { } } -struct H264MTDecoder { +pub struct H264MTDecoder { info: NACodecInfoRef, nal_len: u8, dispatch: Shareable, @@ -492,7 +492,7 @@ struct H264MTDecoder { } impl H264MTDecoder { - fn new() -> Self { + pub fn new() -> Self { Self { info: NACodecInfoRef::default(), nal_len: 0, @@ -981,7 +981,3 @@ impl NAOptionHandler for H264MTDecoder { } } } - -pub fn get_decoder_mt() -> Box { - Box::new(H264MTDecoder::new()) -} diff --git a/nihav-itu/src/codecs/h264/baseline/decoder_st.rs b/nihav-itu/src/codecs/h264/baseline/decoder_st.rs index 8577117..683c031 100644 --- a/nihav-itu/src/codecs/h264/baseline/decoder_st.rs +++ b/nihav-itu/src/codecs/h264/baseline/decoder_st.rs @@ -6,7 +6,7 @@ use nihav_core::io::bitreader::*; use super::super::*; use super::*; -struct H264Decoder { +pub struct H264Decoder { info: NACodecInfoRef, width: usize, height: usize, @@ -47,7 +47,7 @@ struct H264Decoder { } impl H264Decoder { - fn new() -> Self { + pub fn new() -> Self { H264Decoder{ info: NACodecInfoRef::default(), width: 0, @@ -942,7 +942,3 @@ impl NAOptionHandler for H264Decoder { } } } - -pub fn get_decoder() -> Box { - Box::new(H264Decoder::new()) -} diff --git a/nihav-itu/src/codecs/h264/baseline/dispatch.rs b/nihav-itu/src/codecs/h264/baseline/dispatch.rs index e3e27fc..e2adfb0 100644 --- a/nihav-itu/src/codecs/h264/baseline/dispatch.rs +++ b/nihav-itu/src/codecs/h264/baseline/dispatch.rs @@ -4,7 +4,8 @@ use std::thread; use nihav_core::codecs::{DecoderError, DecoderResult}; -use super::{FrameDecoder, PictureInfo}; +use super::PictureInfo; +use super::decoder_mt::FrameDecoder; use super::super::Shareable; #[derive(Clone,Copy,Debug,PartialEq)] diff --git a/nihav-itu/src/codecs/h264/baseline/mod.rs b/nihav-itu/src/codecs/h264/baseline/mod.rs index 89606fc..c7e28f4 100644 --- a/nihav-itu/src/codecs/h264/baseline/mod.rs +++ b/nihav-itu/src/codecs/h264/baseline/mod.rs @@ -20,11 +20,9 @@ use mb_recon::*; use super::sets::*; use super::slice::*; -mod decoder_st; -pub use decoder_st::*; +pub mod decoder_st; mod dispatch; -mod decoder_mt; -pub use decoder_mt::*; +pub mod decoder_mt; use super::common_types::*; diff --git a/nihav-itu/src/codecs/h264/mod.rs b/nihav-itu/src/codecs/h264/mod.rs index 52e8d0e..160970e 100644 --- a/nihav-itu/src/codecs/h264/mod.rs +++ b/nihav-itu/src/codecs/h264/mod.rs @@ -16,7 +16,6 @@ mod baseline; mod cabac_coder; mod common_types; use common_types::*; -pub use baseline::{get_decoder, get_decoder_mt}; mod sets; mod slice; use slice::*; @@ -167,6 +166,83 @@ fn unescape_nal(src: &[u8], dst: &mut Vec) -> usize { off } +struct STDecoderWrapper { + h264: Box, +} + +impl NADecoder for STDecoderWrapper { + fn init(&mut self, supp: &mut NADecoderSupport, info: NACodecInfoRef) -> DecoderResult<()> { + self.h264.init(supp, info) + } + fn decode(&mut self, supp: &mut NADecoderSupport, pkt: &NAPacket) -> DecoderResult { + self.h264.decode(supp, pkt) + } + fn flush(&mut self) { + self.h264.flush(); + } +} + +impl NAOptionHandler for STDecoderWrapper { + fn get_supported_options(&self) -> &[NAOptionDefinition] { + self.h264.get_supported_options() + } + fn set_options(&mut self, options: &[NAOption]) { + self.h264.set_options(options); + } + fn query_option_value(&self, name: &str) -> Option { + self.h264.query_option_value(name) + } +} + +pub fn get_decoder() -> Box { + Box::new(STDecoderWrapper { + h264: Box::new(baseline::decoder_st::H264Decoder::new()), + }) +} + +struct MTDecoderWrapper { + h264: Box, +} + +impl NADecoderMT for MTDecoderWrapper { + fn init(&mut self, supp: &mut NADecoderSupport, info: NACodecInfoRef, nthreads: usize) -> DecoderResult<()> { + self.h264.init(supp, info, nthreads) + } + fn can_take_input(&mut self) -> bool { + self.h264.can_take_input() + } + fn queue_pkt(&mut self, supp: &mut NADecoderSupport, pkt: &NAPacket, user_id: u32) -> DecoderResult { + self.h264.queue_pkt(supp, pkt, user_id) + } + fn has_output(&mut self) -> bool { + self.h264.has_output() + } + fn get_frame(&mut self) -> (DecoderResult, u32) { + self.h264.get_frame() + } + fn flush(&mut self) { + self.h264.flush(); + } +} + +impl NAOptionHandler for MTDecoderWrapper { + fn get_supported_options(&self) -> &[NAOptionDefinition] { + self.h264.get_supported_options() + } + fn set_options(&mut self, options: &[NAOption]) { + self.h264.set_options(options); + } + fn query_option_value(&self, name: &str) -> Option { + self.h264.query_option_value(name) + } +} + +pub fn get_decoder_mt() -> Box { + Box::new(MTDecoderWrapper { + h264: Box::new(baseline::decoder_mt::H264MTDecoder::new()), + }) +} + const DEBLOCK_SKIP_OPTION: &str = "skip_deblock"; const DECODER_OPTIONS: &[NAOptionDefinition] = &[ -- 2.39.5