| 1 | |
| 2 | |
| 3 | use std::collections::VecDeque; |
| 4 | |
| 5 | use ec_core::error::{Error, Result}; |
| 6 | use ec_core::frame::{Frame, PixelFormat}; |
| 7 | use ec_core::packet::Packet; |
| 8 | use ec_core::registry::{CodecId, CodecParameters, MediaParameters}; |
| 9 | use ec_core::timebase::Timestamp; |
| 10 | |
| 11 | use crate::frame::{AlphaPlane, DecodedFrame, decode_frame}; |
| 12 | use crate::header::FrameInfo; |
| 13 | |
| 14 | |
| 15 | |
| 16 | pub 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 | |
| 25 | impl ProResDecoder { |
| 26 | |
| 27 | |
| 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 | |
| 46 | |
| 47 | pub fn set_threads(&mut self, n: usize) { |
| 48 | self.threads = n.clamp(1, 6); |
| 49 | } |
| 50 | |
| 51 | |
| 52 | |
| 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 | |
| 65 | |
| 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 | |
| 74 | pub fn last_alpha(&self) -> Option<&AlphaPlane> { |
| 75 | self.last_alpha.as_ref() |
| 76 | } |
| 77 | |
| 78 | |
| 79 | pub fn last_info(&self) -> Option<&FrameInfo> { |
| 80 | self.last_info.as_ref() |
| 81 | } |
| 82 | |
| 83 | |
| 84 | pub fn output_format(is_444: bool) -> PixelFormat { |
| 85 | if is_444 { PixelFormat::I412 } else { PixelFormat::I210 } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | impl 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 | } |