forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

codec.rs111 lines, 3.8 KiBHistoryRawDownload
1//! The [`ec_core::registry::Decoder`] entry path and the one-shot API.
2
3use std::collections::VecDeque;
4
5use ec_core::error::{Error, Result};
6use ec_core::frame::{Frame, PixelFormat, VideoFrame};
7use ec_core::packet::Packet;
8use ec_core::registry::{CodecId, CodecParameters, MediaParameters};
9use ec_core::timebase::{TimeBase, Timestamp};
10
11use crate::decoder::{Chroma, Core, Picture, to_picture, to_video_frame};
12
13/// Avid DNxHD / DNxHR decoder. One packet is one frame (both fields of an
14/// interlaced frame included), as MOV, Matroska and MXF carry it.
15pub struct DnxhdDecoder {
16 params: CodecParameters,
17 core: Core,
18 ready: VecDeque<VideoFrame>,
19 eof: bool,
20}
21
22impl DnxhdDecoder {
23 /// A decoder for `params` (codec must be [`CodecId::Dnxhd`]). It uses up to
24 /// six threads, one band of macroblock rows each.
25 pub fn new(params: CodecParameters) -> Result<DnxhdDecoder> {
26 if params.codec != CodecId::Dnxhd {
27 return Err(Error::unsupported(
28 format!("codec {:?}", params.codec),
29 "this decoder implements DNxHD/DNxHR only",
30 ));
31 }
32 let threads = std::thread::available_parallelism().map_or(1, |n| n.get());
33 Ok(DnxhdDecoder { params, core: Core::new(threads), ready: VecDeque::new(), eof: false })
34 }
35
36 /// Limit the worker threads used per frame (1 to 6).
37 pub fn set_threads(&mut self, n: usize) {
38 self.core.threads = n.clamp(1, 6);
39 }
40
41 /// Decode one frame packet into a [`VideoFrame`] (I422, I210, I444 or I410;
42 /// 12-bit pictures are narrowed to 10 bits). The interlace field is set for
43 /// two-field frames; `pts` follows the packet.
44 pub fn decode(&mut self, packet: &Packet) -> Result<VideoFrame> {
45 let mut f = self.decode_bytes(&packet.data)?;
46 f.pts = packet.pts.map(|t| Timestamp::new(t, packet.time_base));
47 Ok(f)
48 }
49
50 /// [`DnxhdDecoder::decode`] on raw frame bytes.
51 pub fn decode_bytes(&mut self, data: &[u8]) -> Result<VideoFrame> {
52 let canvas = self.core.decode(data)?;
53 self.refresh_params(&canvas.info);
54 to_video_frame(canvas)
55 }
56
57 /// Decode a frame to `u16` planes at the coded bit depth (8, 10 or 12),
58 /// with the header facts in [`Picture::info`].
59 pub fn decode_native(&mut self, data: &[u8]) -> Result<Picture> {
60 let canvas = self.core.decode(data)?;
61 self.refresh_params(&canvas.info);
62 Ok(to_picture(canvas))
63 }
64
65 fn refresh_params(&mut self, info: &crate::decoder::FrameInfo) {
66 if let MediaParameters::Video(v) = &mut self.params.media {
67 v.width = info.width;
68 v.height = info.height;
69 v.format = Some(match (info.chroma, info.bit_depth) {
70 (Chroma::C422, 8) => PixelFormat::I422,
71 (Chroma::C422, _) => PixelFormat::I210,
72 (Chroma::C444, 8) => PixelFormat::I444,
73 (Chroma::C444, _) => PixelFormat::I410,
74 });
75 v.sample_aspect_ratio = info
76 .sample_aspect_ratio
77 .map(|(n, d)| TimeBase::new(i64::from(n), i64::from(d)));
78 }
79 }
80}
81
82impl ec_core::registry::Decoder for DnxhdDecoder {
83 fn codec_parameters(&self) -> &CodecParameters {
84 &self.params
85 }
86
87 fn send_packet(&mut self, packet: &Packet) -> Result<()> {
88 self.eof = false;
89 let f = self.decode(packet)?;
90 self.ready.push_back(f);
91 Ok(())
92 }
93
94 fn receive_frame(&mut self) -> Result<Frame> {
95 match self.ready.pop_front() {
96 Some(f) => Ok(Frame::Video(f)),
97 None if self.eof => Err(Error::Eof),
98 None => Err(Error::NeedMore),
99 }
100 }
101
102 fn flush(&mut self) -> Result<()> {
103 self.eof = true;
104 Ok(())
105 }
106
107 fn reset(&mut self) {
108 self.ready.clear();
109 self.eof = false;
110 }
111}