| 1 | |
| 2 | |
| 3 | use ec_core::error::{Error, Result}; |
| 4 | use ec_core::frame::{Frame, VideoFrame}; |
| 5 | use ec_core::packet::Packet; |
| 6 | use ec_core::registry::{CodecId, CodecParameters, MediaParameters}; |
| 7 | use ec_core::timebase::{TimeBase, Timestamp}; |
| 8 | |
| 9 | use crate::decoder::{Core, PictureInfo}; |
| 10 | use crate::header::SequenceInfo; |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | pub struct Mpeg2Decoder { |
| 19 | params: CodecParameters, |
| 20 | core: Core, |
| 21 | eof: bool, |
| 22 | last_info: Option<PictureInfo>, |
| 23 | } |
| 24 | |
| 25 | impl Mpeg2Decoder { |
| 26 | |
| 27 | |
| 28 | |
| 29 | pub fn new(params: CodecParameters) -> Result<Mpeg2Decoder> { |
| 30 | if params.codec != CodecId::Mpeg2Video { |
| 31 | return Err(Error::unsupported( |
| 32 | format!("codec {:?}", params.codec), |
| 33 | "this decoder implements MPEG-1/2 video only", |
| 34 | )); |
| 35 | } |
| 36 | let mut d = Mpeg2Decoder { |
| 37 | params, |
| 38 | core: Core::new(), |
| 39 | eof: false, |
| 40 | last_info: None, |
| 41 | }; |
| 42 | if let Some(extra) = d.params.extradata.clone() { |
| 43 | |
| 44 | let _ = d.core.push_complete(&extra, None); |
| 45 | d.refresh_params(); |
| 46 | } |
| 47 | Ok(d) |
| 48 | } |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | pub fn send_bytes(&mut self, data: &[u8], pts: Option<Timestamp>) -> Result<()> { |
| 54 | self.eof = false; |
| 55 | let r = self.core.push(data, pts); |
| 56 | self.refresh_params(); |
| 57 | r |
| 58 | } |
| 59 | |
| 60 | |
| 61 | pub fn flush_stream(&mut self) -> Result<()> { |
| 62 | let r = self.core.flush(); |
| 63 | self.eof = true; |
| 64 | self.refresh_params(); |
| 65 | r |
| 66 | } |
| 67 | |
| 68 | |
| 69 | pub fn pop_picture(&mut self) -> Option<(VideoFrame, PictureInfo)> { |
| 70 | self.core.out.pop_front() |
| 71 | } |
| 72 | |
| 73 | |
| 74 | pub fn sequence(&self) -> Option<&SequenceInfo> { |
| 75 | self.core.seq.as_ref() |
| 76 | } |
| 77 | |
| 78 | |
| 79 | pub fn last_picture_info(&self) -> Option<&PictureInfo> { |
| 80 | self.last_info.as_ref() |
| 81 | } |
| 82 | |
| 83 | |
| 84 | |
| 85 | pub fn dropped_pictures(&self) -> u64 { |
| 86 | self.core.dropped |
| 87 | } |
| 88 | |
| 89 | fn refresh_params(&mut self) { |
| 90 | let Some(seq) = self.core.seq.as_ref() else { |
| 91 | return; |
| 92 | }; |
| 93 | if let MediaParameters::Video(v) = &mut self.params.media { |
| 94 | v.width = seq.width; |
| 95 | v.height = seq.height; |
| 96 | v.format = Some(if seq.chroma_format == 2 { |
| 97 | ec_core::frame::PixelFormat::I422 |
| 98 | } else { |
| 99 | ec_core::frame::PixelFormat::I420 |
| 100 | }); |
| 101 | v.frame_rate = seq.frame_rate().map(|(n, d)| TimeBase::new(n as i64, d as i64)); |
| 102 | |
| 103 | let dar = match seq.aspect_ratio_code { |
| 104 | 2 => Some((4i64, 3i64)), |
| 105 | 3 => Some((16, 9)), |
| 106 | 4 => Some((221, 100)), |
| 107 | _ => None, |
| 108 | }; |
| 109 | v.sample_aspect_ratio = if seq.mpeg2 { |
| 110 | dar.map(|(n, d)| { |
| 111 | let (num, den) = (n * seq.height as i64, d * seq.width as i64); |
| 112 | let g = gcd(num, den).max(1); |
| 113 | TimeBase::new(num / g, den / g) |
| 114 | }) |
| 115 | } else { |
| 116 | None |
| 117 | }; |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | fn gcd(a: i64, b: i64) -> i64 { |
| 123 | if b == 0 { a } else { gcd(b, a % b) } |
| 124 | } |
| 125 | |
| 126 | impl ec_core::registry::Decoder for Mpeg2Decoder { |
| 127 | fn codec_parameters(&self) -> &CodecParameters { |
| 128 | &self.params |
| 129 | } |
| 130 | |
| 131 | fn send_packet(&mut self, packet: &Packet) -> Result<()> { |
| 132 | self.eof = false; |
| 133 | let pts = packet |
| 134 | .pts |
| 135 | .map(|ticks| Timestamp::new(ticks, packet.time_base)); |
| 136 | let r = self.core.push_complete(&packet.data, pts); |
| 137 | self.refresh_params(); |
| 138 | r |
| 139 | } |
| 140 | |
| 141 | fn receive_frame(&mut self) -> Result<Frame> { |
| 142 | match self.core.out.pop_front() { |
| 143 | Some((f, info)) => { |
| 144 | self.last_info = Some(info); |
| 145 | Ok(Frame::Video(f)) |
| 146 | } |
| 147 | None if self.eof => Err(Error::Eof), |
| 148 | None => Err(Error::NeedMore), |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | fn flush(&mut self) -> Result<()> { |
| 153 | self.flush_stream() |
| 154 | } |
| 155 | |
| 156 | fn reset(&mut self) { |
| 157 | self.core.reset(); |
| 158 | self.eof = false; |
| 159 | self.last_info = None; |
| 160 | } |
| 161 | } |