| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | use crate::common::*; |
| 6 | use ec_core::registry::Demuxer; |
| 7 | use ec_core::{CodecId, PixelFormat, TimeBase}; |
| 8 | |
| 9 | fn pictures(d: &[u8]) -> Vec<u8> { |
| 10 | |
| 11 | d.windows(6) |
| 12 | .filter(|w| w[..4] == [0, 0, 1, 0]) |
| 13 | .map(|w| (w[5] >> 3) & 7) |
| 14 | .collect() |
| 15 | } |
| 16 | |
| 17 | fn check(codec: &str, name: &str, mpeg2: bool) { |
| 18 | let Some(ts) = fixture( |
| 19 | name, |
| 20 | &[ |
| 21 | "-f", |
| 22 | "lavfi", |
| 23 | "-i", |
| 24 | "testsrc2=size=352x288:rate=25", |
| 25 | "-frames:v", |
| 26 | "12", |
| 27 | "-c:v", |
| 28 | codec, |
| 29 | "-g", |
| 30 | "6", |
| 31 | "-bf", |
| 32 | "2", |
| 33 | "-f", |
| 34 | "mpegts", |
| 35 | ], |
| 36 | ) else { |
| 37 | return; |
| 38 | }; |
| 39 | let es = fixture_dir().join(format!("{name}.m2v")); |
| 40 | if let Err(e) = ffmpeg( |
| 41 | &["-i", ts.to_str().unwrap(), "-c", "copy", "-f", codec], |
| 42 | &es, |
| 43 | ) { |
| 44 | assert!(!required(), "es extraction failed: {e}"); |
| 45 | return; |
| 46 | } |
| 47 | let want = std::fs::read(&es).unwrap(); |
| 48 | let mut d = open(&ts); |
| 49 | assert_eq!(d.streams().len(), 1); |
| 50 | let s = &d.streams()[0]; |
| 51 | assert_eq!(s.params.codec, CodecId::Mpeg2Video); |
| 52 | let v = s.params.video().unwrap(); |
| 53 | assert_eq!((v.width, v.height), (352, 288)); |
| 54 | assert_eq!(v.frame_rate, Some(TimeBase::new(25, 1))); |
| 55 | assert_eq!(v.format, Some(PixelFormat::I420)); |
| 56 | let ex = s.params.extradata.clone().unwrap(); |
| 57 | assert_eq!(ex[..4], [0, 0, 1, 0xB3]); |
| 58 | assert_eq!(ex.windows(4).any(|w| w == [0, 0, 1, 0xB5]), mpeg2); |
| 59 | assert!(d.tracks()[0].refusal.is_none()); |
| 60 | |
| 61 | let pkts = read_all(&mut d); |
| 62 | assert_eq!(pkts.len(), 12, "one packet per picture"); |
| 63 | let got: Vec<u8> = pkts.iter().flat_map(|p| p.data.clone()).collect(); |
| 64 | assert_eq!(pictures(&got), pictures(&want)); |
| 65 | assert_eq!(got, want, "packet data equals ffmpeg's elementary stream"); |
| 66 | for p in &pkts { |
| 67 | assert_eq!(pictures(&p.data).len(), 1); |
| 68 | assert_eq!(p.key, pictures(&p.data)[0] == 1 || p.data[3] == 0xB3); |
| 69 | assert!(p.pts.is_some()); |
| 70 | } |
| 71 | assert!(pkts[0].key); |
| 72 | assert!(pkts.iter().filter(|p| p.key).count() >= 2); |
| 73 | |
| 74 | let mut pts: Vec<i64> = pkts.iter().map(|p| p.pts.unwrap()).collect(); |
| 75 | pts.sort_unstable(); |
| 76 | assert!(pts.windows(2).all(|w| w[1] - w[0] == 3600), "{pts:?}"); |
| 77 | |
| 78 | let dts: Vec<i64> = pkts.iter().filter_map(|p| p.dts).collect(); |
| 79 | assert!(dts.windows(2).all(|w| w[1] >= w[0])); |
| 80 | } |
| 81 | |
| 82 | #[test] |
| 83 | fn mpeg2video_in_ts() { |
| 84 | check("mpeg2video", "m2v.ts", true); |
| 85 | } |
| 86 | |
| 87 | #[test] |
| 88 | fn mpeg1video_in_ts() { |
| 89 | check("mpeg1video", "m1v.ts", false); |
| 90 | } |