X-Git-Url: https://git.nihav.org/?a=blobdiff_plain;f=nihav-core%2Fsrc%2Ftest%2Fdec_video.rs;h=9c219a0c4b6cc23a1494db2afcdf2ceb8886eeaf;hb=d24468d9dbd54f5cbe414649ff061699337fa7fe;hp=538a0ef82edf8ffd272495e6aad72270f3f45a65;hpb=171860fcc4a4ba3ec28bc4b720b9f582377be4cf;p=nihav.git diff --git a/nihav-core/src/test/dec_video.rs b/nihav-core/src/test/dec_video.rs index 538a0ef..9c219a0 100644 --- a/nihav-core/src/test/dec_video.rs +++ b/nihav-core/src/test/dec_video.rs @@ -4,11 +4,16 @@ use crate::frame::*; use crate::codecs::*; use crate::demuxers::*; //use crate::io::byteio::*; +use crate::scale::*; use super::wavwriter::WavWriter; +use super::md5::MD5; +pub use super::ExpectedTestResult; + +const OUTPUT_PREFIX: &str = "assets/test_out"; fn write_pgmyuv(pfx: &str, strno: usize, num: u64, frm: NAFrameRef) { if let NABufferType::None = frm.get_buffer() { return; } - let name = format!("assets/{}out{:02}_{:06}.pgm", pfx, strno, num); + let name = format!("{}/{}out{:02}_{:06}.pgm", OUTPUT_PREFIX, pfx, strno, num); let mut ofile = File::create(name).unwrap(); let buf = frm.get_buffer().get_vbuf().unwrap(); let (w, h) = buf.get_dimensions(0); @@ -27,20 +32,38 @@ fn write_pgmyuv(pfx: &str, strno: usize, num: u64, frm: NAFrameRef) { let ls = buf.get_stride(0); let mut idx = 0; let mut idx2 = w; + let is_flipped = buf.get_info().is_flipped(); + if is_flipped { + idx += h * ls; + idx2 += h * ls; + } for _ in 0..h { + if is_flipped { + idx -= ls; + idx2 -= ls; + } let line = &dta[idx..idx2]; ofile.write_all(line).unwrap(); - idx += ls; - idx2 += ls; + if !is_flipped { + idx += ls; + idx2 += ls; + } } if w2 <= w/2 { - let mut pad: Vec = Vec::with_capacity((w - w2 * 2) / 2); - pad.resize((w - w2 * 2) / 2, 0xFF); + let pad: Vec = vec![0xFF; (w - w2 * 2) / 2]; let mut base1 = buf.get_offset(1); let stride1 = buf.get_stride(1); let mut base2 = buf.get_offset(2); let stride2 = buf.get_stride(2); + if is_flipped { + base1 += h2 * stride1; + base2 += h2 * stride2; + } for _ in 0..h2 { + if is_flipped { + base1 -= stride1; + base2 -= stride2; + } let bend1 = base1 + w2; let line = &dta[base1..bend1]; ofile.write_all(line).unwrap(); @@ -51,46 +74,73 @@ fn write_pgmyuv(pfx: &str, strno: usize, num: u64, frm: NAFrameRef) { ofile.write_all(line).unwrap(); ofile.write_all(pad.as_slice()).unwrap(); - base1 += stride1; - base2 += stride2; + if !is_flipped { + base1 += stride1; + base2 += stride2; + } } } else { - let mut pad: Vec = Vec::with_capacity(w - w2); - pad.resize(w - w2, 0xFF); + let pad: Vec = vec![0xFF; w - w2]; let mut base1 = buf.get_offset(1); let stride1 = buf.get_stride(1); + if is_flipped { + base1 += h2 * stride1; + } for _ in 0..h2 { + if is_flipped { + base1 -= stride1; + } let bend1 = base1 + w2; let line = &dta[base1..bend1]; ofile.write_all(line).unwrap(); ofile.write_all(pad.as_slice()).unwrap(); - base1 += stride1; + if !is_flipped { + base1 += stride1; + } } let mut base2 = buf.get_offset(2); let stride2 = buf.get_stride(2); + if is_flipped { + base2 += h2 * stride2; + } for _ in 0..h2 { + if is_flipped { + base2 -= stride2; + } let bend2 = base2 + w2; let line = &dta[base2..bend2]; ofile.write_all(line).unwrap(); ofile.write_all(pad.as_slice()).unwrap(); - base2 += stride2; + if !is_flipped { + base2 += stride2; + } } } if has_alpha { let ls = buf.get_stride(3); let mut idx = buf.get_offset(3); let mut idx2 = idx + w; + if is_flipped { + idx += h * ls; + idx2 += h * ls; + } for _ in 0..h { + if is_flipped { + idx -= ls; + idx2 -= ls; + } let line = &dta[idx..idx2]; ofile.write_all(line).unwrap(); - idx += ls; - idx2 += ls; + if !is_flipped { + idx += ls; + idx2 += ls; + } } } } fn write_palppm(pfx: &str, strno: usize, num: u64, frm: NAFrameRef) { - let name = format!("assets/{}out{:02}_{:06}.ppm", pfx, strno, num); + let name = format!("{}/{}out{:02}_{:06}.ppm", OUTPUT_PREFIX, pfx, strno, num); let mut ofile = File::create(name).unwrap(); let buf = frm.get_buffer().get_vbuf().unwrap(); let (w, h) = buf.get_dimensions(0); @@ -105,8 +155,7 @@ fn write_palppm(pfx: &str, strno: usize, num: u64, frm: NAFrameRef) { buf.get_info().get_format().get_chromaton(2).unwrap().get_offset() as usize ]; let mut idx = 0; - let mut line: Vec = Vec::with_capacity(w * 3); - line.resize(w * 3, 0); + let mut line: Vec = vec![0; w * 3]; for _ in 0..h { let src = &dta[idx..(idx+w)]; for x in 0..w { @@ -121,68 +170,23 @@ fn write_palppm(pfx: &str, strno: usize, num: u64, frm: NAFrameRef) { } fn write_ppm(pfx: &str, strno: usize, num: u64, frm: NAFrameRef) { - let name = format!("assets/{}out{:02}_{:06}.ppm", pfx, strno, num); + let name = format!("{}/{}out{:02}_{:06}.ppm", OUTPUT_PREFIX, pfx, strno, num); let mut ofile = File::create(name).unwrap(); - if let NABufferType::VideoPacked(ref buf) = frm.get_buffer() { - let (w, h) = buf.get_dimensions(0); - let hdr = format!("P6\n{} {}\n255\n", w, h); - ofile.write_all(hdr.as_bytes()).unwrap(); - let dta = buf.get_data(); - let stride = buf.get_stride(0); - let offs: [usize; 3] = [ - buf.get_info().get_format().get_chromaton(0).unwrap().get_offset() as usize, - buf.get_info().get_format().get_chromaton(1).unwrap().get_offset() as usize, - buf.get_info().get_format().get_chromaton(2).unwrap().get_offset() as usize - ]; - let step = buf.get_info().get_format().get_elem_size() as usize; - let mut line: Vec = Vec::with_capacity(w * 3); - line.resize(w * 3, 0); - for src in dta.chunks(stride) { - for x in 0..w { - line[x * 3 + 0] = src[x * step + offs[0]]; - line[x * 3 + 1] = src[x * step + offs[1]]; - line[x * 3 + 2] = src[x * step + offs[2]]; - } - ofile.write_all(line.as_slice()).unwrap(); - } - } else if let NABufferType::Video16(ref buf) = frm.get_buffer() { + let info = frm.get_buffer().get_video_info().unwrap(); + let mut dpic = alloc_video_buffer(NAVideoInfo::new(info.get_width(), info.get_height(), false, RGB24_FORMAT), 0).unwrap(); + let ifmt = ScaleInfo { width: info.get_width(), height: info.get_height(), fmt: info.get_format() }; + let ofmt = ScaleInfo { width: info.get_width(), height: info.get_height(), fmt: RGB24_FORMAT }; + let mut scaler = NAScale::new(ifmt, ofmt).unwrap(); + scaler.convert(&frm.get_buffer(), &mut dpic).unwrap(); + let buf = dpic.get_vbuf().unwrap(); let (w, h) = buf.get_dimensions(0); let hdr = format!("P6\n{} {}\n255\n", w, h); ofile.write_all(hdr.as_bytes()).unwrap(); let dta = buf.get_data(); let stride = buf.get_stride(0); - let depths: [u8; 3] = [ - buf.get_info().get_format().get_chromaton(0).unwrap().get_depth(), - buf.get_info().get_format().get_chromaton(1).unwrap().get_depth(), - buf.get_info().get_format().get_chromaton(2).unwrap().get_depth() - ]; - let masks: [u16; 3] = [ - (1 << depths[0]) - 1, - (1 << depths[1]) - 1, - (1 << depths[2]) - 1 - ]; - let shifts: [u8; 3] = [ - buf.get_info().get_format().get_chromaton(0).unwrap().get_shift(), - buf.get_info().get_format().get_chromaton(1).unwrap().get_shift(), - buf.get_info().get_format().get_chromaton(2).unwrap().get_shift() - ]; - let mut line: Vec = Vec::with_capacity(w * 3); - line.resize(w * 3, 0); for src in dta.chunks(stride) { - for x in 0..w { - let elem = src[x]; - let r = ((elem >> shifts[0]) & masks[0]) << (8 - depths[0]); - let g = ((elem >> shifts[1]) & masks[1]) << (8 - depths[1]); - let b = ((elem >> shifts[2]) & masks[2]) << (8 - depths[2]); - line[x * 3 + 0] = r as u8; - line[x * 3 + 1] = g as u8; - line[x * 3 + 2] = b as u8; - } - ofile.write_all(line.as_slice()).unwrap(); + ofile.write_all(&src[0..w*3]).unwrap(); } - } else { -panic!(" unhandled buf format"); - } } /*fn open_wav_out(pfx: &str, strno: usize) -> WavWriter { @@ -203,7 +207,7 @@ pub fn test_file_decoding(demuxer: &str, name: &str, limit: Option, let mut br = ByteReader::new(&mut fr); let mut dmx = create_demuxer(dmx_f, &mut br).unwrap(); - let mut decs: Vec>> = Vec::new(); + let mut decs: Vec, Box)>> = Vec::new(); for i in 0..dmx.get_num_streams() { let s = dmx.get_stream(i).unwrap(); let info = s.get_info(); @@ -211,8 +215,9 @@ pub fn test_file_decoding(demuxer: &str, name: &str, limit: Option, if let Some(df) = decfunc { if (decode_video && info.is_video()) || (decode_audio && info.is_audio()) { let mut dec = (df)(); - dec.init(info).unwrap(); - decs.push(Some(dec)); + let mut dsupp = Box::new(NADecoderSupport::new()); + dec.init(&mut dsupp, info).unwrap(); + decs.push(Some((dsupp, dec))); } else { decs.push(None); } @@ -228,15 +233,15 @@ pub fn test_file_decoding(demuxer: &str, name: &str, limit: Option, panic!("error"); } let pkt = pktres.unwrap(); - if limit.is_some() && pkt.get_pts().is_some() { - if pkt.get_pts().unwrap() > limit.unwrap() { break; } - } let streamno = pkt.get_stream().get_id() as usize; - if let Some(ref mut dec) = decs[streamno] { - let frm = dec.decode(&pkt).unwrap(); + if let Some((ref mut dsupp, ref mut dec)) = decs[streamno] { + if let (Some(lim), Some(ppts)) = (limit, pkt.get_pts()) { + if ppts > lim { break; } + } + let frm = dec.decode(dsupp, &pkt).unwrap(); if pkt.get_stream().get_info().is_video() && video_pfx.is_some() && frm.get_frame_type() != FrameType::Skip { let pfx = video_pfx.unwrap(); - let pts = if let Some(fpts) = frm.get_pts() { fpts } else { pkt.get_pts().unwrap() }; + let pts = if let Some(fpts) = frm.get_pts() { fpts } else { pkt.get_pts().unwrap() }; let vinfo = frm.get_buffer().get_video_info().unwrap(); if vinfo.get_format().is_paletted() { write_palppm(pfx, streamno, pts, frm); @@ -252,7 +257,7 @@ panic!(" unknown format"); } } -pub fn test_decode_audio(demuxer: &str, name: &str, limit: Option, audio_pfx: &str, +pub fn test_decode_audio(demuxer: &str, name: &str, limit: Option, audio_pfx: Option<&str>, dmx_reg: &RegisteredDemuxers, dec_reg: &RegisteredDecoders) { let dmx_f = dmx_reg.find_demuxer(demuxer).unwrap(); let mut file = File::open(name).unwrap(); @@ -260,7 +265,7 @@ pub fn test_decode_audio(demuxer: &str, name: &str, limit: Option, audio_pf let mut br = ByteReader::new(&mut fr); let mut dmx = create_demuxer(dmx_f, &mut br).unwrap(); - let mut decs: Vec>> = Vec::new(); + let mut decs: Vec, Box)>> = Vec::new(); for i in 0..dmx.get_num_streams() { let s = dmx.get_stream(i).unwrap(); let info = s.get_info(); @@ -268,8 +273,9 @@ pub fn test_decode_audio(demuxer: &str, name: &str, limit: Option, audio_pf if let Some(df) = decfunc { if info.is_audio() { let mut dec = (df)(); - dec.init(info).unwrap(); - decs.push(Some(dec)); + let mut dsupp = Box::new(NADecoderSupport::new()); + dec.init(&mut dsupp, info).unwrap(); + decs.push(Some((dsupp, dec))); } else { decs.push(None); } @@ -278,13 +284,165 @@ pub fn test_decode_audio(demuxer: &str, name: &str, limit: Option, audio_pf } } - let name = format!("assets/{}out.wav", audio_pfx); - let file = File::create(name).unwrap(); - let mut fw = FileWriter::new_write(file); - let mut wr = ByteWriter::new(&mut fw); - let mut wwr = WavWriter::new(&mut wr); - let mut wrote_header = false; + if let Some(audio_pfx) = audio_pfx { + let name = format!("{}/{}out.wav", OUTPUT_PREFIX, audio_pfx); + let file = File::create(name).unwrap(); + let mut fw = FileWriter::new_write(file); + let mut wr = ByteWriter::new(&mut fw); + let mut wwr = WavWriter::new(&mut wr); + let mut wrote_header = false; + loop { + let pktres = dmx.get_frame(); + if let Err(e) = pktres { + if e == DemuxerError::EOF { break; } + panic!("error"); + } + let pkt = pktres.unwrap(); + if limit.is_some() && pkt.get_pts().is_some() && pkt.get_pts().unwrap() > limit.unwrap() { + break; + } + let streamno = pkt.get_stream().get_id() as usize; + if let Some((ref mut dsupp, ref mut dec)) = decs[streamno] { + let frm = dec.decode(dsupp, &pkt).unwrap(); + if frm.get_info().is_audio() { + if !wrote_header { + wwr.write_header(frm.get_info().as_ref().get_properties().get_audio_info().unwrap()).unwrap(); + wrote_header = true; + } + wwr.write_frame(frm.get_buffer()).unwrap(); + } + } + } + } else { + loop { + let pktres = dmx.get_frame(); + if let Err(e) = pktres { + if e == DemuxerError::EOF { break; } + panic!("error"); + } + let pkt = pktres.unwrap(); + if limit.is_some() && pkt.get_pts().is_some() && pkt.get_pts().unwrap() > limit.unwrap() { + break; + } + let streamno = pkt.get_stream().get_id() as usize; + if let Some((ref mut dsupp, ref mut dec)) = decs[streamno] { + let _ = dec.decode(dsupp, &pkt).unwrap(); + } + } + } +} + +fn frame_checksum(md5: &mut MD5, frm: NAFrameRef) { + match frm.get_buffer() { + NABufferType::Video(ref vb) => { + md5.update_hash(vb.get_data()); + }, + NABufferType::Video16(ref vb) => { + let mut samp = [0u8; 2]; + let data = vb.get_data(); + for el in data.iter() { + samp[0] = (*el >> 8) as u8; + samp[1] = (*el >> 0) as u8; + md5.update_hash(&samp); + } + }, + NABufferType::Video32(ref vb) => { + let mut samp = [0u8; 4]; + let data = vb.get_data(); + for el in data.iter() { + samp[0] = (*el >> 24) as u8; + samp[1] = (*el >> 16) as u8; + samp[2] = (*el >> 8) as u8; + samp[3] = (*el >> 0) as u8; + md5.update_hash(&samp); + } + }, + NABufferType::VideoPacked(ref vb) => { + md5.update_hash(vb.get_data()); + }, + NABufferType::AudioU8(ref ab) => { + md5.update_hash(ab.get_data()); + }, + NABufferType::AudioI16(ref ab) => { + let mut samp = [0u8; 2]; + let data = ab.get_data(); + for el in data.iter() { + samp[0] = (*el >> 8) as u8; + samp[1] = (*el >> 0) as u8; + md5.update_hash(&samp); + } + }, + NABufferType::AudioI32(ref ab) => { + let mut samp = [0u8; 4]; + let data = ab.get_data(); + for el in data.iter() { + samp[0] = (*el >> 24) as u8; + samp[1] = (*el >> 16) as u8; + samp[2] = (*el >> 8) as u8; + samp[3] = (*el >> 0) as u8; + md5.update_hash(&samp); + } + }, + NABufferType::AudioF32(ref ab) => { + let mut samp = [0u8; 4]; + let data = ab.get_data(); + for el in data.iter() { + let bits = el.to_bits(); + samp[0] = (bits >> 24) as u8; + samp[1] = (bits >> 16) as u8; + samp[2] = (bits >> 8) as u8; + samp[3] = (bits >> 0) as u8; + md5.update_hash(&samp); + } + }, + NABufferType::AudioPacked(ref ab) => { + md5.update_hash(ab.get_data()); + }, + NABufferType::Data(ref db) => { + md5.update_hash(db.as_ref()); + }, + NABufferType::None => {}, + }; +} + +pub fn test_decoding(demuxer: &str, dec_name: &str, filename: &str, limit: Option, + dmx_reg: &RegisteredDemuxers, dec_reg: &RegisteredDecoders, + test: ExpectedTestResult) { + let dmx_f = dmx_reg.find_demuxer(demuxer).unwrap(); + let mut file = File::open(filename).unwrap(); + let mut fr = FileReader::new_read(&mut file); + let mut br = ByteReader::new(&mut fr); + let mut dmx = create_demuxer(dmx_f, &mut br).unwrap(); + + let mut decs: Vec, Box)>> = Vec::new(); + let mut found = false; + for i in 0..dmx.get_num_streams() { + let s = dmx.get_stream(i).unwrap(); + let info = s.get_info(); +println!("stream {} codec {} / {}", i, info.get_name(), dec_name); + if !found && (info.get_name() == dec_name) { + let decfunc = dec_reg.find_decoder(info.get_name()); + if let Some(df) = decfunc { + let mut dec = (df)(); + let mut dsupp = Box::new(NADecoderSupport::new()); + dec.init(&mut dsupp, info).unwrap(); + decs.push(Some((dsupp, dec))); + found = true; + } else { + decs.push(None); + } + } else { + decs.push(None); + } + } + + let mut md5 = MD5::new(); + let mut frameiter = if let ExpectedTestResult::MD5Frames(ref vec) = test { + Some(vec.iter()) + } else { + None + }; loop { let pktres = dmx.get_frame(); if let Err(e) = pktres { @@ -292,19 +450,48 @@ pub fn test_decode_audio(demuxer: &str, name: &str, limit: Option, audio_pf panic!("error"); } let pkt = pktres.unwrap(); - if limit.is_some() && pkt.get_pts().is_some() { - if pkt.get_pts().unwrap() > limit.unwrap() { break; } - } let streamno = pkt.get_stream().get_id() as usize; - if let Some(ref mut dec) = decs[streamno] { - let frm = dec.decode(&pkt).unwrap(); - if frm.get_info().is_audio() { - if !wrote_header { - wwr.write_header(frm.get_info().as_ref().get_properties().get_audio_info().unwrap()).unwrap(); - wrote_header = true; - } - wwr.write_frame(frm.get_buffer()).unwrap(); + if let Some((ref mut dsupp, ref mut dec)) = decs[streamno] { + if limit.is_some() && pkt.get_pts().is_some() && pkt.get_pts().unwrap() > limit.unwrap() { + break; } + let frm = dec.decode(dsupp, &pkt).unwrap(); + match &test { + ExpectedTestResult::Decodes => {}, + ExpectedTestResult::MD5(_) => { frame_checksum(&mut md5, frm); }, + ExpectedTestResult::MD5Frames(_) => { + md5 = MD5::new(); + frame_checksum(&mut md5, frm); + md5.finish(); + if let Some(ref mut iter) = frameiter { + let ret = iter.next(); + if ret.is_none() { break; } + let ref_hash = ret.unwrap(); + let mut hash = [0u32; 4]; + md5.get_hash(&mut hash); +println!("frame pts {:?} hash {}", pkt.get_pts(), md5); + assert_eq!(&hash, ref_hash); + } + }, + ExpectedTestResult::GenerateMD5Frames => { + md5 = MD5::new(); + frame_checksum(&mut md5, frm); + md5.finish(); + let mut hash = [0u32; 4]; + md5.get_hash(&mut hash); +println!("frame pts {:?} hash [0x{:08x}, 0x{:08x}, 0x{:08x}, 0x{:08x}],", pkt.get_pts(), hash[0], hash[1], hash[2], hash[3]); + }, + }; } } + if let ExpectedTestResult::MD5(ref ref_hash) = test { + md5.finish(); + let mut hash = [0u32; 4]; + md5.get_hash(&mut hash); +println!("full hash {}", md5); + assert_eq!(&hash, ref_hash); + } + if let ExpectedTestResult::GenerateMD5Frames = test { + panic!("generated hashes"); + } }