forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB86fb0f3Tahinli7 days ago3927 commits

codec.rs121 lines, 3.9 KiBHistoryRawDownload
1//! The [`ec_core::registry::Decoder`] entry path and a direct frame API.
2
3use std::collections::VecDeque;
4
5use ec_core::error::{Error, Result};
6use ec_core::frame::{Frame, PixelFormat};
7use ec_core::packet::Packet;
8use ec_core::registry::{CodecId, CodecParameters, MediaParameters};
9use ec_core::timebase::Timestamp;
10
11use crate::frame::{AlphaPlane, DecodedFrame, decode_frame};
12use crate::header::FrameInfo;
13
14/// ProRes decoder. Every packet is one whole frame (the `icpf` atom a QuickTime / MP4 sample
15/// holds); there is no reordering, so each packet yields exactly one frame.
16pub struct ProResDecoder {
17 params: CodecParameters,
18 out: VecDeque<ec_core::frame::VideoFrame>,
19 eof: bool,
20 threads: usize,
21 last_alpha: Option<AlphaPlane>,
22 last_info: Option<FrameInfo>,
23}
24
25impl ProResDecoder {
26 /// A decoder for `params` (`CodecId::ProRes`). Slice-parallel over the available cores
27 /// (up to 6) by default; see [`ProResDecoder::set_threads`].
28 pub fn new(params: CodecParameters) -> Result<ProResDecoder> {
29 if params.codec != CodecId::ProRes {
30 return Err(Error::unsupported(
31 format!("codec {:?}", params.codec),
32 "this decoder implements Apple ProRes only",
33 ));
34 }
35 Ok(ProResDecoder {
36 params,
37 out: VecDeque::new(),
38 eof: false,
39 threads: std::thread::available_parallelism().map_or(1, |n| n.get()).clamp(1, 6),
40 last_alpha: None,
41 last_info: None,
42 })
43 }
44
45 /// Slice-parallel decode with up to `n` threads (clamped to 1..=6). Output is identical for
46 /// any value.
47 pub fn set_threads(&mut self, n: usize) {
48 self.threads = n.clamp(1, 6);
49 }
50
51 /// Decode one frame from its bytes (`size`, `icpf`, frame header, pictures). Returns the
52 /// picture, the alpha plane (when the stream has one) and the header facts.
53 pub fn decode_full(&mut self, data: &[u8]) -> Result<DecodedFrame> {
54 let f = decode_frame(data, self.threads)?;
55 if let MediaParameters::Video(v) = &mut self.params.media {
56 v.width = f.info.width;
57 v.height = f.info.height;
58 v.format = Some(f.video.format);
59 v.color = f.video.color;
60 }
61 Ok(f)
62 }
63
64 /// Decode one frame and return its picture; alpha, if any, is kept for
65 /// [`ProResDecoder::last_alpha`].
66 pub fn decode(&mut self, data: &[u8]) -> Result<ec_core::frame::VideoFrame> {
67 let f = self.decode_full(data)?;
68 self.last_alpha = f.alpha;
69 self.last_info = Some(f.info);
70 Ok(f.video)
71 }
72
73 /// Alpha plane of the frame last decoded by `decode` or `send_packet`.
74 pub fn last_alpha(&self) -> Option<&AlphaPlane> {
75 self.last_alpha.as_ref()
76 }
77
78 /// Header facts of the frame last decoded.
79 pub fn last_info(&self) -> Option<&FrameInfo> {
80 self.last_info.as_ref()
81 }
82
83 /// Pixel format a stream of this kind decodes to.
84 pub fn output_format(is_444: bool) -> PixelFormat {
85 if is_444 { PixelFormat::I412 } else { PixelFormat::I210 }
86 }
87}
88
89impl ec_core::registry::Decoder for ProResDecoder {
90 fn codec_parameters(&self) -> &CodecParameters {
91 &self.params
92 }
93
94 fn send_packet(&mut self, packet: &Packet) -> Result<()> {
95 self.eof = false;
96 let mut v = self.decode(&packet.data)?;
97 v.pts = packet.pts.map(|t| Timestamp::new(t, packet.time_base));
98 self.out.push_back(v);
99 Ok(())
100 }
101
102 fn receive_frame(&mut self) -> Result<Frame> {
103 match self.out.pop_front() {
104 Some(f) => Ok(Frame::Video(f)),
105 None if self.eof => Err(Error::Eof),
106 None => Err(Error::NeedMore),
107 }
108 }
109
110 fn flush(&mut self) -> Result<()> {
111 self.eof = true;
112 Ok(())
113 }
114
115 fn reset(&mut self) {
116 self.out.clear();
117 self.eof = false;
118 self.last_alpha = None;
119 self.last_info = None;
120 }
121}