forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB86fb0f3Tahinli7 days ago3927 commits

decoder.rs130 lines, 4.5 KiBHistoryRawDownload
1//! [`ec_core::registry::Decoder`] over the DV frame decoder, and conversion of a native
2//! [`DvPicture`] into a [`VideoFrame`].
3
4use std::collections::VecDeque;
5
6use ec_core::error::{Error, Result};
7use ec_core::frame::{ColorInfo, Frame, Plane, PixelFormat, VideoFrame};
8use ec_core::packet::Packet;
9use ec_core::registry::{CodecId, CodecParameters, MediaParameters};
10use ec_core::timebase::{TimeBase, Timestamp};
11
12use crate::profile::Chroma;
13use crate::video::{DvPicture, DvVideoDecoder};
14
15/// Pixel format a DV system comes out as through [`DvDecoder`]: 4:2:0 PAL -> `I420`, 4:2:2 ->
16/// `I422`, and 4:1:1 -> `I422` by exact horizontal chroma duplication (each native chroma sample
17/// is repeated twice; use [`DvPicture`] / [`DvDecoder::decode_native`] for the native 4:1:1
18/// planes).
19pub fn output_format(c: Chroma) -> PixelFormat {
20 match c {
21 Chroma::Yuv420 => PixelFormat::I420,
22 Chroma::Yuv411 | Chroma::Yuv422 => PixelFormat::I422,
23 }
24}
25
26/// Convert a native DV picture to a [`VideoFrame`] (4:1:1 is widened to 4:2:2 by duplication).
27pub fn to_video_frame(p: &DvPicture) -> VideoFrame {
28 let w = p.width as usize;
29 let (cw, ch) = (p.chroma_width(), p.chroma_height());
30 let format = output_format(p.chroma);
31 let mut planes = vec![Plane::new(p.planes[0].clone(), w)];
32 for i in 1..3 {
33 if p.chroma == Chroma::Yuv411 {
34 let mut v = Vec::with_capacity(cw * 2 * ch);
35 for row in p.planes[i].chunks_exact(cw) {
36 for &s in row {
37 v.push(s);
38 v.push(s);
39 }
40 }
41 planes.push(Plane::new(v, cw * 2));
42 } else {
43 planes.push(Plane::new(p.planes[i].clone(), cw));
44 }
45 }
46 let mut f = VideoFrame::alloc(format, p.width, p.height);
47 f.planes = planes;
48 f.interlace = p.interlace;
49 f.color = if p.profile.is_hd() {
50 ColorInfo { primaries: 1, transfer: 1, matrix: 1, full_range: false }
51 } else if p.profile.dsf == 1 {
52 ColorInfo { primaries: 5, transfer: 6, matrix: 5, full_range: false }
53 } else {
54 ColorInfo { primaries: 6, transfer: 6, matrix: 6, full_range: false }
55 };
56 f
57}
58
59/// DV video decoder. Every packet is one whole DV frame (what an AVI/MOV sample or one frame of a
60/// raw `.dv` file holds); one packet yields one frame.
61pub struct DvDecoder {
62 params: CodecParameters,
63 inner: DvVideoDecoder,
64 out: VecDeque<VideoFrame>,
65 eof: bool,
66}
67
68impl DvDecoder {
69 /// A decoder for `params` (`CodecId::DvVideo`).
70 pub fn new(params: CodecParameters) -> Result<DvDecoder> {
71 if params.codec != CodecId::DvVideo {
72 return Err(Error::unsupported(format!("codec {:?}", params.codec), "this decoder implements DV video only"));
73 }
74 Ok(DvDecoder { params, inner: DvVideoDecoder::new(), out: VecDeque::new(), eof: false })
75 }
76
77 /// Decode one frame to its native planes (4:1:1 stays 4:1:1).
78 pub fn decode_native(&mut self, data: &[u8]) -> Result<DvPicture> {
79 let p = self.inner.decode(data)?;
80 if let MediaParameters::Video(v) = &mut self.params.media {
81 v.width = p.width;
82 v.height = p.height;
83 v.format = Some(output_format(p.chroma));
84 let (n, d) = p.profile.time_base;
85 v.frame_rate = Some(TimeBase::new(i64::from(d), i64::from(n)));
86 let (sn, sd) = p.profile.sar[usize::from(p.is_16_9)];
87 v.sample_aspect_ratio = Some(TimeBase::new(i64::from(sn), i64::from(sd)));
88 }
89 Ok(p)
90 }
91
92 /// Decode one frame to a [`VideoFrame`] (see [`output_format`]).
93 pub fn decode(&mut self, data: &[u8]) -> Result<VideoFrame> {
94 let p = self.decode_native(data)?;
95 Ok(to_video_frame(&p))
96 }
97}
98
99impl ec_core::registry::Decoder for DvDecoder {
100 fn codec_parameters(&self) -> &CodecParameters {
101 &self.params
102 }
103
104 fn send_packet(&mut self, packet: &Packet) -> Result<()> {
105 self.eof = false;
106 let mut v = self.decode(&packet.data)?;
107 v.pts = packet.pts.map(|t| Timestamp::new(t, packet.time_base));
108 self.out.push_back(v);
109 Ok(())
110 }
111
112 fn receive_frame(&mut self) -> Result<Frame> {
113 match self.out.pop_front() {
114 Some(f) => Ok(Frame::Video(f)),
115 None if self.eof => Err(Error::Eof),
116 None => Err(Error::NeedMore),
117 }
118 }
119
120 fn flush(&mut self) -> Result<()> {
121 self.eof = true;
122 Ok(())
123 }
124
125 fn reset(&mut self) {
126 self.out.clear();
127 self.eof = false;
128 self.inner.reset();
129 }
130}