| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | use std::time::Instant; |
| 12 | |
| 13 | use ec_core::frame::{Frame, Plane}; |
| 14 | use ec_core::packet::Packet; |
| 15 | use ec_core::registry::{CodecId, Decoder as _, Demuxer as _}; |
| 16 | use ec_h264::{Decoder as H264NalDecoder, H264Decoder, NalOutcome}; |
| 17 | |
| 18 | |
| 19 | |
| 20 | fn hash_frame(h: &mut u64, w: u32, height: u32, planes: &[Plane]) { |
| 21 | let mut fold = |bytes: &[u8]| { |
| 22 | for &b in bytes { |
| 23 | *h ^= u64::from(b); |
| 24 | *h = h.wrapping_mul(0x100000001b3); |
| 25 | } |
| 26 | }; |
| 27 | fold(&w.to_le_bytes()); |
| 28 | fold(&height.to_le_bytes()); |
| 29 | let (w2, h2) = (w.div_ceil(2) as usize, height.div_ceil(2) as usize); |
| 30 | let dims = [(w as usize, height as usize), (w2, h2), (w2, h2)]; |
| 31 | for (plane, (pw, ph)) in planes.iter().zip(dims) { |
| 32 | for y in 0..ph { |
| 33 | let row = plane.row(y, pw).expect("plane row"); |
| 34 | fold(row); |
| 35 | } |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | fn decode_mp4( |
| 40 | packets: &[Packet], |
| 41 | params: &ec_core::registry::CodecParameters, |
| 42 | hash: bool, |
| 43 | ) -> (usize, u64, u32, u32) { |
| 44 | let mut dec = H264Decoder::new(params.clone()).expect("h264 decoder"); |
| 45 | let mut frames = 0usize; |
| 46 | let mut h: u64 = 0xcbf29ce484222325; |
| 47 | let mut dims = (0u32, 0u32); |
| 48 | for p in packets { |
| 49 | dec.send_packet(p).expect("send_packet"); |
| 50 | while let Ok(Frame::Video(f)) = dec.receive_frame() { |
| 51 | if hash { |
| 52 | hash_frame(&mut h, f.width, f.height, &f.planes); |
| 53 | } |
| 54 | dims = (f.width, f.height); |
| 55 | frames += 1; |
| 56 | } |
| 57 | } |
| 58 | dec.flush().expect("flush"); |
| 59 | while let Ok(Frame::Video(f)) = dec.receive_frame() { |
| 60 | if hash { |
| 61 | hash_frame(&mut h, f.width, f.height, &f.planes); |
| 62 | } |
| 63 | dims = (f.width, f.height); |
| 64 | frames += 1; |
| 65 | } |
| 66 | (frames, h, dims.0, dims.1) |
| 67 | } |
| 68 | |
| 69 | fn decode_annexb(bytes: &[u8], hash: bool) -> (usize, u64, u32, u32) { |
| 70 | let mut dec = H264NalDecoder::new(); |
| 71 | let mut frames = 0usize; |
| 72 | let mut h: u64 = 0xcbf29ce484222325; |
| 73 | let mut dims = (0u32, 0u32); |
| 74 | for nal in ec_h264_syntax::AnnexBIter::new(bytes) { |
| 75 | if dec.push_nal(nal).expect("push_nal") == NalOutcome::PictureBoundary { |
| 76 | dec.end_picture().expect("end_picture"); |
| 77 | dec.push_nal(nal).expect("re-push"); |
| 78 | } |
| 79 | while let Some(f) = dec.next_frame() { |
| 80 | if hash { |
| 81 | hash_frame(&mut h, f.width, f.height, &f.planes); |
| 82 | } |
| 83 | dims = (f.width, f.height); |
| 84 | frames += 1; |
| 85 | } |
| 86 | } |
| 87 | dec.flush().expect("flush"); |
| 88 | while let Some(f) = dec.next_frame() { |
| 89 | if hash { |
| 90 | hash_frame(&mut h, f.width, f.height, &f.planes); |
| 91 | } |
| 92 | dims = (f.width, f.height); |
| 93 | frames += 1; |
| 94 | } |
| 95 | (frames, h, dims.0, dims.1) |
| 96 | } |
| 97 | |
| 98 | enum Source { |
| 99 | Mp4(Vec<Packet>, ec_core::registry::CodecParameters), |
| 100 | AnnexB(Vec<u8>), |
| 101 | } |
| 102 | |
| 103 | fn decode(src: &Source, hash: bool) -> (usize, u64, u32, u32) { |
| 104 | match src { |
| 105 | Source::Mp4(p, par) => decode_mp4(p, par, hash), |
| 106 | Source::AnnexB(b) => decode_annexb(b, hash), |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | fn main() { |
| 111 | let args: Vec<String> = std::env::args().skip(1).collect(); |
| 112 | let path = args |
| 113 | .first() |
| 114 | .expect("usage: bench_decode <file.mp4|.264> [reps] [--hash]"); |
| 115 | let reps: u32 = args.get(1).and_then(|s| s.parse().ok()).unwrap_or(3); |
| 116 | let hash = args.iter().any(|a| a == "--hash"); |
| 117 | |
| 118 | let src = if path.ends_with(".mp4") { |
| 119 | let file = std::fs::File::open(path).expect("open mp4"); |
| 120 | let mut demux = |
| 121 | ec_mp4::Mp4Demuxer::new(std::io::BufReader::new(file)).expect("ec-mp4 open"); |
| 122 | let (idx, params) = demux |
| 123 | .streams() |
| 124 | .iter() |
| 125 | .find(|s| s.params.codec == CodecId::H264) |
| 126 | .map(|s| (s.index, s.params.clone())) |
| 127 | .expect("no H.264 track"); |
| 128 | let mut packets = Vec::new(); |
| 129 | while let Ok(p) = demux.next_packet() { |
| 130 | if p.stream == idx { |
| 131 | packets.push(p); |
| 132 | } |
| 133 | } |
| 134 | Source::Mp4(packets, params) |
| 135 | } else { |
| 136 | Source::AnnexB(std::fs::read(path).expect("read annexb")) |
| 137 | }; |
| 138 | |
| 139 | |
| 140 | let (frames, h, w, height) = decode(&src, hash); |
| 141 | assert!(frames > 0, "no frames decoded"); |
| 142 | if hash { |
| 143 | println!("hash={h:016x} frames={frames} {w}x{height}"); |
| 144 | } |
| 145 | |
| 146 | let px = u64::from(w) * u64::from(height) * frames as u64; |
| 147 | let mut best = f64::MAX; |
| 148 | for _ in 0..reps { |
| 149 | let t = Instant::now(); |
| 150 | let r = decode(&src, false); |
| 151 | let e = t.elapsed().as_secs_f64(); |
| 152 | assert_eq!(r.0, frames, "frame count diverged between passes"); |
| 153 | best = best.min(e); |
| 154 | } |
| 155 | println!( |
| 156 | "{path}: frames={frames} px={px} best={:.1}ms over {reps} -> {:.1} Mpx/s", |
| 157 | best * 1e3, |
| 158 | px as f64 / best / 1e6 |
| 159 | ); |
| 160 | } |