| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | use std::sync::mpsc::SyncSender; |
| 23 | |
| 24 | use ec_core::frame::{Interlace, PixelFormat, Plane, VideoFrame}; |
| 25 | use ec_core::registry::{CodecId, CodecParameters, MediaParameters}; |
| 26 | |
| 27 | use super::{Abort, Deep, DecodeFailure, Frame, I420, LATE_RUN, Render, Wide, skip_for_speed}; |
| 28 | use crate::colorspace::{Chroma, Tags}; |
| 29 | use crate::deinterlace::Deinterlacer; |
| 30 | use crate::demux::{Codec, Demuxer, VideoMeta}; |
| 31 | use crate::project::Speed; |
| 32 | |
| 33 | |
| 34 | #[derive(Debug, Clone, Copy, Default, PartialEq, Eq)] |
| 35 | pub(crate) struct IntraFacts { |
| 36 | |
| 37 | pub interlaced: bool, |
| 38 | |
| 39 | |
| 40 | pub order: Interlace, |
| 41 | |
| 42 | pub coded_height: u32, |
| 43 | } |
| 44 | |
| 45 | impl IntraFacts { |
| 46 | |
| 47 | pub(crate) fn field_order(self) -> Interlace { |
| 48 | if self.order.is_interlaced() { self.order } else { Interlace::TopFieldFirst } |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | |
| 53 | pub(crate) struct Probed { |
| 54 | pub facts: IntraFacts, |
| 55 | pub bit_depth: u8, |
| 56 | pub chroma: Chroma, |
| 57 | |
| 58 | |
| 59 | pub profile: Option<&'static str>, |
| 60 | |
| 61 | pub tags: Tags, |
| 62 | } |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | pub(crate) fn probe( |
| 69 | codec: Codec, |
| 70 | sample: &[u8], |
| 71 | meta: &VideoMeta, |
| 72 | container_order: Option<Interlace>, |
| 73 | header: &[u8], |
| 74 | ) -> Result<Option<Probed>, String> { |
| 75 | let named = |e: ec_core::Error| match e { |
| 76 | ec_core::Error::Unsupported { .. } => Err(format!("{} is not supported: {e}", codec.name())), |
| 77 | _ => Ok(None), |
| 78 | }; |
| 79 | match codec { |
| 80 | Codec::ProRes => { |
| 81 | let mut d = ec_prores::ProResDecoder::new(CodecParameters::new(CodecId::ProRes)).map_err(|e| e.to_string())?; |
| 82 | d.set_threads(1); |
| 83 | let full = match d.decode_full(sample) { |
| 84 | Ok(f) => f, |
| 85 | Err(e) => return named(e), |
| 86 | }; |
| 87 | let i = full.info; |
| 88 | Ok(Some(Probed { |
| 89 | facts: IntraFacts { interlaced: i.interlace.is_interlaced(), order: i.interlace, coded_height: meta.coded_height }, |
| 90 | bit_depth: if i.is_444 { 12 } else { 10 }, |
| 91 | chroma: if i.is_444 { Chroma::C444 } else { Chroma::C422 }, |
| 92 | profile: None, |
| 93 | tags: Tags::from_codes(u64::from(i.matrix), u64::from(i.transfer), 0).with_primaries(u64::from(i.primaries)), |
| 94 | })) |
| 95 | } |
| 96 | Codec::Dnxhd => { |
| 97 | let mut d = ec_dnxhd::DnxhdDecoder::new(CodecParameters::new(CodecId::Dnxhd)).map_err(|e| e.to_string())?; |
| 98 | d.set_threads(1); |
| 99 | let pic = match d.decode_native(sample) { |
| 100 | Ok(p) => p, |
| 101 | Err(e) => return named(e), |
| 102 | }; |
| 103 | let i = pic.info; |
| 104 | let order = match (i.interlaced, i.top_field_first) { |
| 105 | (false, _) => Interlace::Progressive, |
| 106 | (true, true) => Interlace::TopFieldFirst, |
| 107 | (true, false) => Interlace::BottomFieldFirst, |
| 108 | }; |
| 109 | Ok(Some(Probed { |
| 110 | facts: IntraFacts { interlaced: i.interlaced, order, coded_height: meta.coded_height }, |
| 111 | bit_depth: i.bit_depth as u8, |
| 112 | chroma: match i.chroma { |
| 113 | ec_dnxhd::Chroma::C444 => Chroma::C444, |
| 114 | _ => Chroma::C422, |
| 115 | }, |
| 116 | profile: crate::video_profile::dnxhd(i.cid, i.bit_depth, sample.len(), meta.frame_rate), |
| 117 | tags: Tags::from_codes(u64::from(i.matrix), 0, 0), |
| 118 | })) |
| 119 | } |
| 120 | Codec::Mjpeg => { |
| 121 | let mut d = mjpeg_decoder(meta.coded_height, container_order.unwrap_or(Interlace::TopFieldFirst))?; |
| 122 | let pic = match d.decode(sample, None) { |
| 123 | Ok(p) => p, |
| 124 | Err(e) => return named(e), |
| 125 | }; |
| 126 | let interlaced = pic.interlace.is_interlaced(); |
| 127 | Ok(Some(Probed { |
| 128 | facts: IntraFacts { |
| 129 | interlaced, |
| 130 | order: if interlaced { pic.interlace } else { Interlace::Progressive }, |
| 131 | coded_height: meta.coded_height, |
| 132 | }, |
| 133 | bit_depth: 8, |
| 134 | chroma: match pic.format { |
| 135 | PixelFormat::Gray8 => Chroma::Gray, |
| 136 | PixelFormat::I422 => Chroma::C422, |
| 137 | PixelFormat::I444 => Chroma::C444, |
| 138 | _ => Chroma::C420, |
| 139 | }, |
| 140 | profile: None, |
| 141 | |
| 142 | |
| 143 | |
| 144 | tags: Tags { |
| 145 | full_range: Some(true), |
| 146 | ..Tags::from_codes(5, 0, 0) |
| 147 | }, |
| 148 | })) |
| 149 | } |
| 150 | Codec::Dv => { |
| 151 | let mut d = ec_dv::DvDecoder::new(CodecParameters::new(CodecId::DvVideo)).map_err(|e| e.to_string())?; |
| 152 | let pic = match d.decode_native(sample) { |
| 153 | Ok(p) => p, |
| 154 | Err(e) => return named(e), |
| 155 | }; |
| 156 | let interlaced = pic.interlace.is_interlaced(); |
| 157 | Ok(Some(Probed { |
| 158 | facts: IntraFacts { |
| 159 | interlaced, |
| 160 | |
| 161 | |
| 162 | order: if interlaced { pic.interlace } else { Interlace::Progressive }, |
| 163 | coded_height: meta.coded_height, |
| 164 | }, |
| 165 | bit_depth: 8, |
| 166 | |
| 167 | chroma: match pic.chroma { |
| 168 | ec_dv::Chroma::Yuv420 => Chroma::C420, |
| 169 | ec_dv::Chroma::Yuv411 | ec_dv::Chroma::Yuv422 => Chroma::C422, |
| 170 | }, |
| 171 | profile: Some(crate::video_profile::dv(pic.profile)), |
| 172 | |
| 173 | tags: Tags::default(), |
| 174 | })) |
| 175 | } |
| 176 | Codec::Raw => { |
| 177 | let seat = super::raw::RawSeat::open(IntraFacts::default(), header)?; |
| 178 | let pic = match seat.0.decode(sample) { |
| 179 | Ok(p) => p, |
| 180 | Err(e) => return named(e), |
| 181 | }; |
| 182 | let full = Tags { full_range: Some(true), ..Tags::default() }; |
| 183 | let (chroma, tags, bit_depth) = match pic.format { |
| 184 | |
| 185 | PixelFormat::Bgr8 | PixelFormat::Rgb8 | PixelFormat::Bgra8 => { |
| 186 | (Chroma::C444, Tags { full_range: Some(true), ..Tags::from_codes(1, 0, 0) }, 8) |
| 187 | } |
| 188 | PixelFormat::I422 => (Chroma::C422, Tags::default(), 8), |
| 189 | PixelFormat::I210 => (Chroma::C422, Tags::default(), 10), |
| 190 | PixelFormat::I410 => (Chroma::C444, Tags::default(), 10), |
| 191 | |
| 192 | PixelFormat::Gray8 => (Chroma::Gray, full, 8), |
| 193 | PixelFormat::Gray16 => (Chroma::Gray, full, 10), |
| 194 | _ => (Chroma::C420, Tags::default(), 8), |
| 195 | }; |
| 196 | Ok(Some(Probed { |
| 197 | facts: IntraFacts { coded_height: meta.coded_height, ..IntraFacts::default() }, |
| 198 | bit_depth, |
| 199 | chroma, |
| 200 | profile: Some(crate::video_profile::raw(header)), |
| 201 | tags, |
| 202 | })) |
| 203 | } |
| 204 | _ => Ok(None), |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | pub(super) fn mjpeg_decoder(container_height: u32, order: Interlace) -> Result<ec_mjpeg::MjpegDecoder, String> { |
| 209 | let mut params = CodecParameters::new(CodecId::Mjpeg); |
| 210 | if let MediaParameters::Video(v) = &mut params.media { |
| 211 | v.height = container_height; |
| 212 | } |
| 213 | let mut d = ec_mjpeg::MjpegDecoder::new(params).map_err(|e| e.to_string())?; |
| 214 | d.set_field_order(order); |
| 215 | Ok(d) |
| 216 | } |
| 217 | |
| 218 | |
| 219 | pub(crate) trait Seat: Sized { |
| 220 | const NAME: &'static str; |
| 221 | |
| 222 | |
| 223 | fn open(facts: IntraFacts, header: &[u8]) -> Result<Self, String>; |
| 224 | fn decode(&mut self, sample: &[u8]) -> Result<VideoFrame, String>; |
| 225 | } |
| 226 | |
| 227 | |
| 228 | pub(crate) struct Stream<S: Seat> { |
| 229 | seat: Result<S, String>, |
| 230 | |
| 231 | index: i64, |
| 232 | flushed: bool, |
| 233 | deint: Deinterlacer, |
| 234 | deep: Deep, |
| 235 | scratch: I420, |
| 236 | } |
| 237 | |
| 238 | impl<S: Seat> Stream<S> { |
| 239 | |
| 240 | |
| 241 | pub(crate) fn open(demuxer: &mut Demuxer, start_frame: u32) -> Self { |
| 242 | let facts = demuxer.intra(); |
| 243 | let index = demuxer.seek_to_sync_at_or_before(if facts.interlaced { |
| 244 | start_frame.saturating_sub(1) |
| 245 | } else { |
| 246 | start_frame |
| 247 | }); |
| 248 | Self { |
| 249 | seat: S::open(facts, demuxer.video_params().map_or(&[][..], |p| p.3)), |
| 250 | index, |
| 251 | flushed: false, |
| 252 | deint: Deinterlacer::new(facts.interlaced, i64::from(start_frame), Interlace::Progressive).flagged_only(), |
| 253 | deep: Deep::default(), |
| 254 | scratch: I420::default(), |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | |
| 259 | pub(crate) fn next(&mut self, demuxer: &mut Demuxer, out: &mut I420) -> Result<Option<i64>, String> { |
| 260 | self.pull(demuxer, Some(out), None) |
| 261 | } |
| 262 | |
| 263 | |
| 264 | pub(crate) fn next_wide(&mut self, demuxer: &mut Demuxer, out: &mut Wide) -> Result<Option<i64>, String> { |
| 265 | self.pull(demuxer, None, Some(out)) |
| 266 | } |
| 267 | |
| 268 | fn pull( |
| 269 | &mut self, |
| 270 | demuxer: &mut Demuxer, |
| 271 | mut narrow: Option<&mut I420>, |
| 272 | mut wide: Option<&mut Wide>, |
| 273 | ) -> Result<Option<i64>, String> { |
| 274 | loop { |
| 275 | if self.flushed { |
| 276 | return match self.deint.finish() { |
| 277 | Some((due, pic)) => { |
| 278 | self.fill(&pic, narrow.as_deref_mut(), wide.as_deref_mut()); |
| 279 | Ok(Some(due)) |
| 280 | } |
| 281 | None => Ok(None), |
| 282 | }; |
| 283 | } |
| 284 | match demuxer.next_access_unit() { |
| 285 | Ok(Some(sample)) => { |
| 286 | let seat = self.seat.as_mut().map_err(|e| e.clone())?; |
| 287 | let pic = seat |
| 288 | .decode(&sample) |
| 289 | .map_err(|e| format!("software {} decode failed at frame {}: {e}", S::NAME, self.index))?; |
| 290 | let tag = self.index; |
| 291 | self.index += 1; |
| 292 | let Some(pic) = canon(pic) else { |
| 293 | return Err(format!("software {} frame {tag} has a pixel layout edith does not carry", S::NAME)); |
| 294 | }; |
| 295 | if let Some((due, pic)) = self.deint.push(tag, pic) { |
| 296 | self.fill(&pic, narrow.as_deref_mut(), wide.as_deref_mut()); |
| 297 | return Ok(Some(due)); |
| 298 | } |
| 299 | } |
| 300 | Ok(None) => self.flushed = true, |
| 301 | Err(e) => return Err(format!("demux error: {e}")), |
| 302 | } |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | |
| 307 | fn fill(&mut self, pic: &VideoFrame, narrow: Option<&mut I420>, wide: Option<&mut Wide>) { |
| 308 | let sixteen = matches!(pic.format, PixelFormat::I010 | PixelFormat::I210 | PixelFormat::I410); |
| 309 | match (narrow, wide) { |
| 310 | (Some(out), _) => self.fill_narrow(pic, out), |
| 311 | (None, Some(out)) if sixteen => { |
| 312 | self.deep.load(pic, true); |
| 313 | std::mem::swap(out, &mut self.deep.wide); |
| 314 | } |
| 315 | (None, Some(out)) => { |
| 316 | let mut tmp = std::mem::take(&mut self.scratch); |
| 317 | self.fill_narrow(pic, &mut tmp); |
| 318 | out.width = tmp.width; |
| 319 | out.height = tmp.height; |
| 320 | for (src, dst) in [(&tmp.y, &mut out.y), (&tmp.u, &mut out.u), (&tmp.v, &mut out.v)] { |
| 321 | dst.clear(); |
| 322 | dst.extend(src.iter().map(|&s| u16::from(s) << 2)); |
| 323 | } |
| 324 | self.scratch = tmp; |
| 325 | } |
| 326 | (None, None) => {} |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | fn fill_narrow(&mut self, pic: &VideoFrame, out: &mut I420) { |
| 331 | if pic.format == PixelFormat::I420 { |
| 332 | out.width = pic.width; |
| 333 | out.height = pic.height; |
| 334 | let (cw, ch) = (pic.width.div_ceil(2) as usize, pic.height.div_ceil(2) as usize); |
| 335 | let dims = [(pic.width as usize, pic.height as usize), (cw, ch), (cw, ch)]; |
| 336 | for ((plane, (w, h)), dst) in pic.planes.iter().zip(dims).zip([&mut out.y, &mut out.u, &mut out.v]) { |
| 337 | dst.clear(); |
| 338 | for row in plane.data.chunks(plane.stride).take(h) { |
| 339 | dst.extend_from_slice(&row[..w]); |
| 340 | } |
| 341 | } |
| 342 | } else { |
| 343 | self.deep.load(pic, false); |
| 344 | std::mem::swap(out, &mut self.deep.narrow); |
| 345 | } |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | |
| 350 | |
| 351 | fn canon(pic: VideoFrame) -> Option<VideoFrame> { |
| 352 | match pic.format { |
| 353 | PixelFormat::I420 |
| 354 | | PixelFormat::I422 |
| 355 | | PixelFormat::I444 |
| 356 | | PixelFormat::I010 |
| 357 | | PixelFormat::I210 |
| 358 | | PixelFormat::I410 => Some(pic), |
| 359 | PixelFormat::I412 => { |
| 360 | let mut out = VideoFrame::alloc(PixelFormat::I410, pic.width, pic.height); |
| 361 | out.color = pic.color; |
| 362 | out.pts = pic.pts; |
| 363 | out.interlace = pic.interlace; |
| 364 | for (dst, src) in out.planes.iter_mut().zip(&pic.planes) { |
| 365 | let stride = dst.stride; |
| 366 | let mut data = vec![0u8; dst.data.len()]; |
| 367 | for (row, line) in data.chunks_mut(stride).enumerate() { |
| 368 | let from = &src.data[row * src.stride..row * src.stride + stride]; |
| 369 | for (d, s) in line.as_chunks_mut::<2>().0.iter_mut().zip(from.as_chunks::<2>().0) { |
| 370 | *d = (u16::from_le_bytes(*s) >> 2).to_le_bytes(); |
| 371 | } |
| 372 | } |
| 373 | *dst = Plane::new(data, stride); |
| 374 | } |
| 375 | Some(out) |
| 376 | } |
| 377 | PixelFormat::Gray8 => { |
| 378 | let mut out = VideoFrame::alloc(PixelFormat::I420, pic.width, pic.height); |
| 379 | out.color = pic.color; |
| 380 | out.pts = pic.pts; |
| 381 | out.interlace = pic.interlace; |
| 382 | let w = pic.width as usize; |
| 383 | let mut luma = vec![0u8; out.planes[0].data.len()]; |
| 384 | for (row, line) in luma.chunks_mut(out.planes[0].stride).enumerate() { |
| 385 | line[..w].copy_from_slice(&pic.planes[0].data[row * pic.planes[0].stride..][..w]); |
| 386 | } |
| 387 | out.planes[0] = Plane::new(luma, out.planes[0].stride); |
| 388 | for p in 1..3 { |
| 389 | let stride = out.planes[p].stride; |
| 390 | out.planes[p] = Plane::new(vec![128u8; out.planes[p].data.len()], stride); |
| 391 | } |
| 392 | Some(out) |
| 393 | } |
| 394 | _ => None, |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | |
| 399 | |
| 400 | |
| 401 | #[allow(clippy::too_many_arguments)] |
| 402 | pub(super) fn run<S: Seat>( |
| 403 | demuxer: &mut Demuxer, |
| 404 | tx: &SyncSender<Frame>, |
| 405 | start_frame: u32, |
| 406 | end_frame: u32, |
| 407 | render: &mut Render, |
| 408 | abort: &Abort, |
| 409 | speed: Speed, |
| 410 | failure: &DecodeFailure, |
| 411 | ) { |
| 412 | let mut stream = Stream::<S>::open(demuxer, start_frame); |
| 413 | let mut out = I420::default(); |
| 414 | let mut skipped = 0; |
| 415 | while !abort.hit() { |
| 416 | let due = match stream.next(demuxer, &mut out) { |
| 417 | Ok(Some(due)) => due, |
| 418 | Ok(None) => break, |
| 419 | Err(e) => { |
| 420 | failure.set(e); |
| 421 | break; |
| 422 | } |
| 423 | }; |
| 424 | if due < i64::from(start_frame) { |
| 425 | continue; |
| 426 | } |
| 427 | let due = due as u32; |
| 428 | let sent = if abort.late(due) { |
| 429 | if skipped < LATE_RUN { |
| 430 | skipped += 1; |
| 431 | false |
| 432 | } else { |
| 433 | skipped = 0; |
| 434 | send(&out, due, render, tx) |
| 435 | } |
| 436 | } else if skip_for_speed(speed, start_frame, due, end_frame) { |
| 437 | false |
| 438 | } else { |
| 439 | skipped = 0; |
| 440 | send(&out, due, render, tx) |
| 441 | }; |
| 442 | if sent || i64::from(due) + 1 >= i64::from(end_frame) { |
| 443 | break; |
| 444 | } |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | |
| 449 | fn send(out: &I420, due: u32, render: &mut Render, tx: &SyncSender<Frame>) -> bool { |
| 450 | let frame = render.frame(due, &out.y, &out.u, &out.v, out.width, out.height); |
| 451 | tx.send(frame).is_err() |
| 452 | } |