1 //! Routines for testing encoders and muxers.
3 use nihav_core::frame::*;
4 use nihav_core::codecs::*;
5 use nihav_core::demuxers::*;
6 use nihav_core::muxers::*;
7 use nihav_core::scale::*;
8 use nihav_core::soundcvt::*;
11 /// Parameters for the source used in the test.
12 pub struct DecoderTestParams {
13 /// Demuxer name e.g. `"mov"`.
14 pub demuxer: &'static str,
16 pub in_name: &'static str,
17 /// Timestamp for last decoded frame.
18 pub limit: Option<u64>,
19 /// Desired input stream type (that will be decoded and fed to the encoder).
20 pub stream_type: StreamType,
21 /// Registered demuxers.
22 pub dmx_reg: RegisteredDemuxers,
23 /// Registered decoders.
24 pub dec_reg: RegisteredDecoders,
27 /// Parameters for the encoding test output.
28 pub struct EncoderTestParams {
29 /// Muxer name e.g. `"avi"`.
30 pub muxer: &'static str,
32 pub enc_name: &'static str,
34 pub out_name: &'static str,
35 /// Registered muxers.
36 pub mux_reg: RegisteredMuxers,
37 /// Registered encoders.
38 pub enc_reg: RegisteredEncoders,
41 /// Tests muxer by making it mux raw streams from the input file.
43 /// Streams not fitting the output profile (e.g. a video stream or a second audio stream for WAV muxer) will be ignored.
44 pub fn test_remuxing(dec_config: &DecoderTestParams, enc_config: &EncoderTestParams) {
45 let dmx_f = dec_config.dmx_reg.find_demuxer(dec_config.demuxer).unwrap();
46 let mut file = File::open(dec_config.in_name).unwrap();
47 let mut fr = FileReader::new_read(&mut file);
48 let mut br = ByteReader::new(&mut fr);
49 let mut dmx = create_demuxer(dmx_f, &mut br).unwrap();
51 let mux_f = enc_config.mux_reg.find_muxer(enc_config.muxer).unwrap();
52 let out_name = "assets/test_out/".to_owned() + enc_config.out_name;
53 let file = File::create(&out_name).unwrap();
54 let mut fw = FileWriter::new_write(file);
55 let mut bw = ByteWriter::new(&mut fw);
56 let mut out_sm = StreamManager::new();
57 let mux_caps = mux_f.get_capabilities();
58 let mut stream_map: Vec<Option<usize>> = Vec::new();
59 let mut has_video = false;
60 let mut has_audio = false;
61 for stream in dmx.get_streams() {
62 let mut copy_stream = false;
64 MuxerCapabilities::SingleVideo(_) | MuxerCapabilities::OnlyVideo => {
65 copy_stream = stream.get_media_type() == StreamType::Video;
68 MuxerCapabilities::SingleAudio(_) | MuxerCapabilities::OnlyAudio => {
69 copy_stream = stream.get_media_type() == StreamType::Audio;
72 MuxerCapabilities::SingleVideoAndAudio(_, _) => {
73 if stream.get_media_type() == StreamType::Video {
74 copy_stream = !has_video;
77 if stream.get_media_type() == StreamType::Audio {
78 copy_stream = !has_audio;
82 MuxerCapabilities::Universal => {
83 if stream.get_media_type() == StreamType::Video {
87 if stream.get_media_type() == StreamType::Audio {
94 let streamno = out_sm.add_stream(NAStream::clone(&stream)).unwrap();
95 stream_map.push(Some(streamno));
97 MuxerCapabilities::SingleVideo(_) | MuxerCapabilities::SingleAudio(_) => break,
101 stream_map.push(None);
104 assert!(out_sm.get_num_streams() > 0);
105 let mut mux = create_muxer(mux_f, out_sm, &mut bw).unwrap();
108 let pktres = dmx.get_frame();
109 if let Err(e) = pktres {
110 if e == DemuxerError::EOF { break; }
113 let mut pkt = pktres.unwrap();
114 println!("Got {}", pkt);
115 if let Some(new_id) = stream_map[pkt.get_stream().id as usize] {
116 pkt.reassign(mux.get_stream(new_id).unwrap(), pkt.get_time_information());
117 mux.mux_frame(pkt).unwrap();
124 /// Tests muxer by making it mux raw streams from the input file and comparing MD5 hash of the result to the provided one.
126 /// Streams not fitting the output profile (e.g. a video stream or a second audio stream for WAV muxer) will be ignored.
127 pub fn test_remuxing_md5(dec_config: &DecoderTestParams, muxer: &str, mux_reg: &RegisteredMuxers, md5_hash: [u32; 4]) {
128 let dmx_f = dec_config.dmx_reg.find_demuxer(dec_config.demuxer).unwrap();
129 let mut file = File::open(dec_config.in_name).unwrap();
130 let mut fr = FileReader::new_read(&mut file);
131 let mut br = ByteReader::new(&mut fr);
132 let mut dmx = create_demuxer(dmx_f, &mut br).unwrap();
134 let mux_f = mux_reg.find_muxer(muxer).unwrap();
136 let mut dst = Vec::with_capacity(1 << 10);
137 let mut gw = GrowableMemoryWriter::new_write(&mut dst);
138 let mut bw = ByteWriter::new(&mut gw);
139 let mut out_sm = StreamManager::new();
140 let mux_caps = mux_f.get_capabilities();
141 let mut stream_map: Vec<Option<usize>> = Vec::new();
142 let mut has_video = false;
143 let mut has_audio = false;
144 for stream in dmx.get_streams() {
145 let mut copy_stream = false;
147 MuxerCapabilities::SingleVideo(_) | MuxerCapabilities::OnlyVideo => {
148 copy_stream = stream.get_media_type() == StreamType::Video;
151 MuxerCapabilities::SingleAudio(_) | MuxerCapabilities::OnlyAudio => {
152 copy_stream = stream.get_media_type() == StreamType::Audio;
155 MuxerCapabilities::SingleVideoAndAudio(_, _) => {
156 if stream.get_media_type() == StreamType::Video {
157 copy_stream = !has_video;
160 if stream.get_media_type() == StreamType::Audio {
161 copy_stream = !has_audio;
165 MuxerCapabilities::Universal => {
166 if stream.get_media_type() == StreamType::Video {
170 if stream.get_media_type() == StreamType::Audio {
177 let streamno = out_sm.add_stream(NAStream::clone(&stream)).unwrap();
178 stream_map.push(Some(streamno));
180 MuxerCapabilities::SingleVideo(_) | MuxerCapabilities::SingleAudio(_) => break,
184 stream_map.push(None);
187 assert!(out_sm.get_num_streams() > 0);
188 let mut mux = create_muxer(mux_f, out_sm, &mut bw).unwrap();
191 let pktres = dmx.get_frame();
192 if let Err(e) = pktres {
193 if e == DemuxerError::EOF { break; }
196 let mut pkt = pktres.unwrap();
197 println!("Got {}", pkt);
198 if let Some(new_id) = stream_map[pkt.get_stream().id as usize] {
199 pkt.reassign(mux.get_stream(new_id).unwrap(), pkt.get_time_information());
200 mux.mux_frame(pkt).unwrap();
206 let mut hash = [0; 4];
207 MD5::calculate_hash(dst.as_slice(), &mut hash);
208 println!("output hash {:08x}{:08x}{:08x}{:08x}", hash[0], hash[1], hash[2], hash[3]);
209 assert_eq!(hash, md5_hash);
212 /// Tests an encoder by decoding a stream from input file, feeding it to the encoder and muxing the result into output file.
213 pub fn test_encoding_to_file(dec_config: &DecoderTestParams, enc_config: &EncoderTestParams, mut enc_params: EncodeParameters) {
214 let dmx_f = dec_config.dmx_reg.find_demuxer(dec_config.demuxer).unwrap();
215 let mut file = File::open(dec_config.in_name).unwrap();
216 let mut fr = FileReader::new_read(&mut file);
217 let mut br = ByteReader::new(&mut fr);
218 let mut dmx = create_demuxer(dmx_f, &mut br).unwrap();
220 let in_stream = dmx.get_streams().find(|str| str.get_media_type() == dec_config.stream_type).unwrap();
221 let in_stream_id = in_stream.id;
222 let decfunc = dec_config.dec_reg.find_decoder(in_stream.get_info().get_name()).unwrap();
223 let mut dec = (decfunc)();
224 let mut dsupp = Box::new(NADecoderSupport::new());
225 dec.init(&mut dsupp, in_stream.get_info()).unwrap();
227 let mut out_sm = StreamManager::new();
228 enc_params.tb_num = in_stream.tb_num;
229 enc_params.tb_den = in_stream.tb_den;
231 if let (NACodecTypeInfo::Video(ref mut vinfo), Some(ref_vinfo)) = (&mut enc_params.format, in_stream.get_info().get_properties().get_video_info()) {
232 if vinfo.width == 0 {
233 vinfo.width = ref_vinfo.width;
234 vinfo.height = ref_vinfo.height;
237 let mut dst_chmap = NAChannelMap::new();
238 if let (NACodecTypeInfo::Audio(ref mut ainfo), Some(ref_ainfo)) = (&mut enc_params.format, in_stream.get_info().get_properties().get_audio_info()) {
239 if ainfo.sample_rate == 0 {
240 ainfo.sample_rate = ref_ainfo.sample_rate;
242 if ainfo.channels == 0 {
243 ainfo.channels = ref_ainfo.channels;
245 match ainfo.channels {
247 dst_chmap.add_channel(NAChannelType::C);
250 dst_chmap.add_channel(NAChannelType::L);
251 dst_chmap.add_channel(NAChannelType::R);
253 _ => panic!("cannot guess channel map"),
257 let encfunc = enc_config.enc_reg.find_encoder(enc_config.enc_name).unwrap();
258 let mut encoder = (encfunc)();
259 let out_str = encoder.init(0, enc_params).unwrap();
260 out_sm.add_stream(NAStream::clone(&out_str));
262 let mux_f = enc_config.mux_reg.find_muxer(enc_config.muxer).unwrap();
263 let out_name = "assets/test_out/".to_owned() + enc_config.out_name;
264 let file = File::create(&out_name).unwrap();
265 let mut fw = FileWriter::new_write(file);
266 let mut bw = ByteWriter::new(&mut fw);
267 let mut mux = create_muxer(mux_f, out_sm, &mut bw).unwrap();
269 let (mut ifmt, dst_vinfo) = if let NACodecTypeInfo::Video(vinfo) = enc_params.format {
270 (ScaleInfo { fmt: vinfo.format, width: vinfo.width, height: vinfo.height },
273 (ScaleInfo { fmt: YUV420_FORMAT, width: 2, height: 2 },
274 NAVideoInfo { width: 2, height: 2, format: YUV420_FORMAT, flipped: false, bits: 12 })
277 let mut scaler = NAScale::new(ifmt, ofmt).unwrap();
278 let mut cvt_buf = alloc_video_buffer(dst_vinfo, 2).unwrap();
280 let pktres = dmx.get_frame();
281 if let Err(e) = pktres {
282 if e == DemuxerError::EOF { break; }
283 panic!("decoding error");
285 let pkt = pktres.unwrap();
286 if pkt.get_stream().id != in_stream_id { continue; }
287 let frm = dec.decode(&mut dsupp, &pkt).unwrap();
288 let buf = frm.get_buffer();
289 let cfrm = if let NACodecTypeInfo::Video(_) = enc_params.format {
290 let cur_ifmt = get_scale_fmt_from_pic(&buf);
291 if cur_ifmt != ifmt {
293 scaler = NAScale::new(ifmt, ofmt).unwrap();
295 scaler.convert(&buf, &mut cvt_buf).unwrap();
296 NAFrame::new(frm.get_time_information(), frm.frame_type, frm.key, frm.get_info(), cvt_buf.clone())
297 } else if let NACodecTypeInfo::Audio(ref dst_ainfo) = enc_params.format {
298 let cvt_buf = convert_audio_frame(&buf, dst_ainfo, &dst_chmap).unwrap();
299 NAFrame::new(frm.get_time_information(), frm.frame_type, frm.key, frm.get_info(), cvt_buf)
301 panic!("unexpected format");
303 encoder.encode(&cfrm).unwrap();
304 while let Ok(Some(pkt)) = encoder.get_packet() {
305 mux.mux_frame(pkt).unwrap();
307 if let Some(maxts) = dec_config.limit {
308 if frm.get_pts().unwrap_or(0) >= maxts {
313 encoder.flush().unwrap();
314 while let Ok(Some(pkt)) = encoder.get_packet() {
315 mux.mux_frame(pkt).unwrap();
320 /// Tests an encoder by decoding a stream from input file, feeding it to the encoder and calculating the hash of codec information and packet data.
321 pub fn test_encoding_md5(dec_config: &DecoderTestParams, enc_config: &EncoderTestParams, mut enc_params: EncodeParameters, ref_hash: &[u32; 4]) {
322 let dmx_f = dec_config.dmx_reg.find_demuxer(dec_config.demuxer).unwrap();
323 let mut file = File::open(dec_config.in_name).unwrap();
324 let mut fr = FileReader::new_read(&mut file);
325 let mut br = ByteReader::new(&mut fr);
326 let mut dmx = create_demuxer(dmx_f, &mut br).unwrap();
328 let in_stream = dmx.get_streams().find(|str| str.get_media_type() == dec_config.stream_type).unwrap();
329 let in_stream_id = in_stream.id;
330 let decfunc = dec_config.dec_reg.find_decoder(in_stream.get_info().get_name()).unwrap();
331 let mut dec = (decfunc)();
332 let mut dsupp = Box::new(NADecoderSupport::new());
333 dec.init(&mut dsupp, in_stream.get_info()).unwrap();
335 enc_params.tb_num = in_stream.tb_num;
336 enc_params.tb_den = in_stream.tb_den;
338 if let (NACodecTypeInfo::Video(ref mut vinfo), Some(ref_vinfo)) = (&mut enc_params.format, in_stream.get_info().get_properties().get_video_info()) {
339 if vinfo.width == 0 {
340 vinfo.width = ref_vinfo.width;
341 vinfo.height = ref_vinfo.height;
344 let mut dst_chmap = NAChannelMap::new();
345 if let (NACodecTypeInfo::Audio(ref mut ainfo), Some(ref_ainfo)) = (&mut enc_params.format, in_stream.get_info().get_properties().get_audio_info()) {
346 if ainfo.sample_rate == 0 {
347 ainfo.sample_rate = ref_ainfo.sample_rate;
349 if ainfo.channels == 0 {
350 ainfo.channels = ref_ainfo.channels;
352 match ainfo.channels {
354 dst_chmap.add_channel(NAChannelType::C);
357 dst_chmap.add_channel(NAChannelType::L);
358 dst_chmap.add_channel(NAChannelType::R);
360 _ => panic!("cannot guess channel map"),
364 let encfunc = enc_config.enc_reg.find_encoder(enc_config.enc_name).unwrap();
365 let mut encoder = (encfunc)();
366 let out_str = encoder.init(0, enc_params).unwrap();
368 let mut md5 = MD5::new();
369 let info = out_str.get_info();
370 md5.update_hash(info.get_name().as_bytes());
371 match info.get_properties() {
372 NACodecTypeInfo::Video(ref vinfo) => {
373 let mut hdr = [0u8; 10];
374 hdr[0] = (vinfo.width >> 24) as u8;
375 hdr[1] = (vinfo.width >> 16) as u8;
376 hdr[2] = (vinfo.width >> 8) as u8;
377 hdr[3] = vinfo.width as u8;
378 hdr[4] = (vinfo.height >> 24) as u8;
379 hdr[5] = (vinfo.height >> 16) as u8;
380 hdr[6] = (vinfo.height >> 8) as u8;
381 hdr[7] = vinfo.height as u8;
382 hdr[8] = vinfo.flipped as u8;
384 md5.update_hash(&hdr);
386 NACodecTypeInfo::Audio(ref ainfo) => {
387 let mut hdr = [0u8; 10];
388 hdr[0] = (ainfo.sample_rate >> 24) as u8;
389 hdr[1] = (ainfo.sample_rate >> 16) as u8;
390 hdr[2] = (ainfo.sample_rate >> 8) as u8;
391 hdr[3] = ainfo.sample_rate as u8;
392 hdr[4] = ainfo.channels;
393 hdr[5] = ainfo.format.bits;
394 hdr[6] = (ainfo.block_len >> 24) as u8;
395 hdr[7] = (ainfo.block_len >> 16) as u8;
396 hdr[8] = (ainfo.block_len >> 8) as u8;
397 hdr[9] = ainfo.block_len as u8;
398 md5.update_hash(&hdr);
402 if let Some(ref buf) = info.get_extradata() {
403 md5.update_hash(buf.as_slice());
406 let (mut ifmt, dst_vinfo) = if let NACodecTypeInfo::Video(vinfo) = enc_params.format {
407 (ScaleInfo { fmt: vinfo.format, width: vinfo.width, height: vinfo.height },
410 (ScaleInfo { fmt: YUV420_FORMAT, width: 2, height: 2 },
411 NAVideoInfo { width: 2, height: 2, format: YUV420_FORMAT, flipped: false, bits: 12 })
414 let mut scaler = NAScale::new(ifmt, ofmt).unwrap();
415 let mut cvt_buf = alloc_video_buffer(dst_vinfo, 2).unwrap();
417 let pktres = dmx.get_frame();
418 if let Err(e) = pktres {
419 if e == DemuxerError::EOF { break; }
420 panic!("decoding error");
422 let pkt = pktres.unwrap();
423 if pkt.get_stream().id != in_stream_id { continue; }
424 let frm = dec.decode(&mut dsupp, &pkt).unwrap();
425 let buf = frm.get_buffer();
426 let cfrm = if let NACodecTypeInfo::Video(_) = enc_params.format {
427 let cur_ifmt = get_scale_fmt_from_pic(&buf);
428 if cur_ifmt != ifmt {
430 scaler = NAScale::new(ifmt, ofmt).unwrap();
432 scaler.convert(&buf, &mut cvt_buf).unwrap();
433 NAFrame::new(frm.get_time_information(), frm.frame_type, frm.key, frm.get_info(), cvt_buf.clone())
434 } else if let NACodecTypeInfo::Audio(ref dst_ainfo) = enc_params.format {
435 let cvt_buf = convert_audio_frame(&buf, dst_ainfo, &dst_chmap).unwrap();
436 NAFrame::new(frm.get_time_information(), frm.frame_type, frm.key, frm.get_info(), cvt_buf)
438 panic!("unexpected format");
440 encoder.encode(&cfrm).unwrap();
441 while let Ok(Some(pkt)) = encoder.get_packet() {
442 md5.update_hash(pkt.get_buffer().as_slice());
444 if let Some(maxts) = dec_config.limit {
445 if frm.get_pts().unwrap_or(0) >= maxts {
450 encoder.flush().unwrap();
451 while let Ok(Some(pkt)) = encoder.get_packet() {
452 md5.update_hash(pkt.get_buffer().as_slice());
455 let mut hash = [0; 4];
457 md5.get_hash(&mut hash);
458 println!("encode hash {}", md5);
459 assert_eq!(&hash, ref_hash);