| 1 | |
| 2 | |
| 3 | |
| 4 | use std::collections::VecDeque; |
| 5 | |
| 6 | use ec_core::error::{Error, Result}; |
| 7 | use ec_core::frame::{ColorInfo, Frame, Plane, PixelFormat, VideoFrame}; |
| 8 | use ec_core::packet::Packet; |
| 9 | use ec_core::registry::{CodecId, CodecParameters, MediaParameters}; |
| 10 | use ec_core::timebase::{TimeBase, Timestamp}; |
| 11 | |
| 12 | use crate::profile::Chroma; |
| 13 | use crate::video::{DvPicture, DvVideoDecoder}; |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | pub 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 | |
| 27 | pub 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 | |
| 60 | |
| 61 | pub struct DvDecoder { |
| 62 | params: CodecParameters, |
| 63 | inner: DvVideoDecoder, |
| 64 | out: VecDeque<VideoFrame>, |
| 65 | eof: bool, |
| 66 | } |
| 67 | |
| 68 | impl DvDecoder { |
| 69 | |
| 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 | |
| 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 | |
| 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 | |
| 99 | impl 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 | } |