aboutsummaryrefslogtreecommitdiffstats
path: root/nihav-codec-support/src/test/dec_video.rs
blob: 9833f23b3af58bea2ab4f3138d757ea8c935e316 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
//! Routines for testing decoders.
use std::fs::File;
use nihav_core::frame::*;
use nihav_core::codecs::*;
use nihav_core::demuxers::*;
//use nihav_core::io::byteio::*;
//use nihav_core::scale::*;
use nihav_core::reorder::MTFrameReorderer;
use super::wavwriter::WavWriter;
use super::md5::MD5;
use crate::imgwrite::write_pnm;
pub use super::ExpectedTestResult;

const OUTPUT_PREFIX: &str = "assets/test_out/";

/*fn open_wav_out(pfx: &str, strno: usize) -> WavWriter {
    let name = format!("assets/{}out{:02}.wav", pfx, strno);
    let mut file = File::create(name).unwrap();
    let mut fw = FileWriter::new_write(&mut file);
    let mut wr = ByteWriter::new(&mut fw);
    WavWriter::new(&mut wr)
}*/

/// Tests decoding of provided file and optionally outputs video frames as PNM (PPM for RGB video, PGM for YUV).
///
/// This function expects the following arguments:
/// * `demuxer` - container format name (used to find proper demuxer for it)
/// * `name` - input file name
/// * `limit` - optional PTS value after which decoding is stopped
/// * `decode_video`/`decode_audio` - flags for enabling video/audio decoding
/// * `video_pfx` - prefix for video frames written as pictures (if enabled then output picture names should look like `<crate_name>/assets/test_out/PFXout00_000000.ppm`
/// * `dmx_reg` and `dec_reg` - registered demuxers and decoders that should contain demuxer and decoder(s) needed to decode the provided file.
///
/// Since the function is intended for tests, it will panic instead of returning an error.
pub fn test_file_decoding(demuxer: &str, name: &str, limit: Option<u64>,
                          decode_video: bool, decode_audio: bool,
                          video_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();
    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<Option<(Box<NADecoderSupport>, Box<dyn NADecoder>)>> = Vec::new();
    for i in 0..dmx.get_num_streams() {
        let s = dmx.get_stream(i).unwrap();
        let info = s.get_info();
        let decfunc = dec_reg.find_decoder(info.get_name());
        if let Some(df) = decfunc {
            if (decode_video && info.is_video()) || (decode_audio && info.is_audio()) {
                let mut dec = (df)();
                let mut dsupp = Box::new(NADecoderSupport::new());
                dec.init(&mut dsupp, info).unwrap();
                decs.push(Some((dsupp, dec)));
            } else {
                decs.push(None);
            }
        } else {
            decs.push(None);
        }
    }

    loop {
        let pktres = dmx.get_frame();
        if let Err(e) = pktres {
            if e == DemuxerError::EOF { break; }
            panic!("error");
        }
        let pkt = pktres.unwrap();
        let streamno = pkt.get_stream().get_id() as usize;
        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 pts = if let Some(fpts) = frm.get_pts() { fpts } else { pkt.get_pts().unwrap() };
                let pfx = OUTPUT_PREFIX.to_owned() + video_pfx.unwrap_or("") + "out";
                write_pnm(pfx.as_str(), streamno, pts, frm).unwrap();
            }
        }
    }
}

/// Tests audio decoder with the content in the provided file and optionally outputs decoded audio.
///
/// The syntax is very similar to [`test_file_decoding`] except that it is intended for testing audio codecs.
///
/// Since the function is intended for tests, it will panic instead of returning an error.
///
/// [`test_file_decoding`]: ./fn.test_file_decoding.html
pub fn test_decode_audio(demuxer: &str, name: &str, limit: Option<u64>, 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();
    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<Option<(Box<NADecoderSupport>, Box<dyn NADecoder>)>> = Vec::new();
    for i in 0..dmx.get_num_streams() {
        let s = dmx.get_stream(i).unwrap();
        let info = s.get_info();
        let decfunc = dec_reg.find_decoder(info.get_name());
        if let Some(df) = decfunc {
            if info.is_audio() {
                let mut dec = (df)();
                let mut dsupp = Box::new(NADecoderSupport::new());
                dec.init(&mut dsupp, info).unwrap();
                decs.push(Some((dsupp, dec)));
            } else {
                decs.push(None);
            }
        } else {
            decs.push(None);
        }
    }

    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 => {},
    };
}

/// Tests decoder for requested codec in provided file.
///
/// This functions tries to decode a stream corresponding to `dec_name` codec in input file and validate the results against expected ones.
///
/// Since the function is intended for tests, it will panic instead of returning an error.
///
/// # Examples
///
/// Test RealVideo 4 decoder in test stream:
/// ```no_run
/// use nihav_codec_support::test::ExpectedTestResult;
/// use nihav_codec_support::test::dec_video::test_decoding;
/// use nihav_core::codecs::RegisteredDecoders;
/// use nihav_core::demuxers::RegisteredDemuxers;
///
/// let mut dmx_reg = RegisteredDemuxers::new();
/// let mut dec_reg = RegisteredDecoders::new();
/// // ... register RealMedia demuxers and RealVideo decoders ...
/// test_decoding("realmedia", "rv40", "assets/test_file.rmvb", None, &dmx_reg, &dec_reg, ExpectedTestResult::MD5([0x00010203, 0x04050607, 0x08090a0b, 0x0c0d0e0f]));
/// ```
pub fn test_decoding(demuxer: &str, dec_name: &str, filename: &str, limit: Option<u64>,
                     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<Option<(Box<NADecoderSupport>, Box<dyn NADecoder>)>> = 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 {
            if e == DemuxerError::EOF { break; }
            panic!("error");
        }
        let pkt = pktres.unwrap();
        let streamno = pkt.get_stream().get_id() as usize;
        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");
    }
}

/// Tests decoder for requested codec in provided file with raw streams.
///
/// This functions tries to decode a stream corresponding to `dec_name` codec in input file and validate the results against expected ones.
///
/// Since the function is intended for tests, it will panic instead of returning an error.
///
/// # Examples
///
/// Test FLAC decoder in test stream:
/// ```no_run
/// use nihav_codec_support::test::ExpectedTestResult;
/// use nihav_codec_support::test::dec_video::test_decoding_raw;
/// use nihav_core::codecs::RegisteredDecoders;
/// use nihav_core::demuxers::RegisteredDemuxers;
///
/// let mut dmx_reg = RegisteredRawDemuxers::new();
/// let mut pkt_reg = RegisteredPacketisers::new();
/// let mut dec_reg = RegisteredDecoders::new();
/// // ... register FLAC demuxer, packetiser and decoders ...
/// test_decoding_raw("flac", "flac", "assets/test_file.flac", None, &dmx_reg, &pkt_reg, &dec_reg, ExpectedTestResult::MD5([0x00010203, 0x04050607, 0x08090a0b, 0x0c0d0e0f]));
/// ```
pub fn test_decoding_raw(demuxer: &str, dec_name: &str, filename: &str, limit: Option<u64>,
                         dmx_reg: &RegisteredRawDemuxers, pkt_reg: &RegisteredPacketisers,
                         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_raw_demuxer(dmx_f, &mut br).unwrap();

    let mut decs: Vec<Option<(Box<dyn NAPacketiser>, Box<NADecoderSupport>, Box<dyn NADecoder>)>> = 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());
            let pktfunc = pkt_reg.find_packetiser(info.get_name());
            if let (Some(df), Some(pf)) = (decfunc, pktfunc) {
                let mut pkt = (pf)();
                pkt.attach_stream(s);
                let mut dec = (df)();
                let mut dsupp = Box::new(NADecoderSupport::new());
                dec.init(&mut dsupp, info).unwrap();
                decs.push(Some((pkt, 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 datares = dmx.get_data();
        if let Err(e) = datares {
            if e == DemuxerError::EOF { break; }
            panic!("error");
        }
        let data = datares.unwrap();
        let streamno = data.get_stream().get_id() as usize;
        if let Some((ref mut pktr, ref mut dsupp, ref mut dec)) = decs[streamno] {
            pktr.add_data(&data.get_buffer());
            while let Ok(Some(pkt)) = pktr.get_packet(data.get_stream()) {
                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");
    }
}

const THREADS: usize = 3;

fn check_frame(frm: NAFrameRef, test: &ExpectedTestResult, glbl_md5: &mut MD5, frameiter: &mut Option<std::slice::Iter<[u32; 4]>>, last_ts: &mut Option<u64>) -> bool {
    let frm_pts = frm.get_pts();
    let frm_dts = frm.get_dts();
    if let (Some(lts), Some(cts)) = (*last_ts, frm_dts) {
        assert!(lts < cts);
    }
    *last_ts = frm_dts;
    match test {
        ExpectedTestResult::Decodes => {},
        ExpectedTestResult::MD5(_) => { frame_checksum(glbl_md5, frm); },
        ExpectedTestResult::MD5Frames(_) => {
            let mut loc_md5 = MD5::new();
            frame_checksum(&mut loc_md5, frm);
            loc_md5.finish();
            if let Some(ref mut iter) = frameiter {
                let ret = iter.next();
                if ret.is_none() {
                    return true;
                }
                let ref_hash = ret.unwrap();
                let mut hash = [0u32; 4];
                loc_md5.get_hash(&mut hash);
println!("frame pts {:?} dts {:?} hash {}", frm_pts, frm_dts, loc_md5);
                assert_eq!(&hash, ref_hash);
            }
        },
        ExpectedTestResult::GenerateMD5Frames => {
            let mut loc_md5 = MD5::new();
            frame_checksum(&mut loc_md5, frm);
            loc_md5.finish();
            let mut hash = [0u32; 4];
            loc_md5.get_hash(&mut hash);
println!("frame pts {:?} dts {:?} hash [0x{:08x}, 0x{:08x}, 0x{:08x}, 0x{:08x}],", frm_pts, frm_dts, hash[0], hash[1], hash[2], hash[3]);
        },
    };
    false
}

/// Tests multi-threaded decoder for requested codec in provided file.
///
/// The syntax is very similar to [`test_file_decoding`] except that it tests multi-threaded decoders instead.
///
/// [`test_file_decoding`]: ./fn.test_file_decoding.html
pub fn test_mt_decoding(demuxer: &str, dec_name: &str, filename: &str, limit: Option<u64>,
                     dmx_reg: &RegisteredDemuxers, dec_reg: &RegisteredMTDecoders,
                     test: ExpectedTestResult) {
    let mut dec_threads = THREADS;
    for (key, value) in std::env::vars_os() {
        if key == "MT_THREADS" {
            if let Some(val) = value.to_str() {
                dec_threads = val.parse::<usize>().unwrap_or(THREADS);
                break;
            }
        }
    }

    let dmx_f = dmx_reg.find_demuxer(demuxer).expect("demuxer is not found");
    let mut file = File::open(filename).expect("input file should be present");
    let mut fr = FileReader::new_read(&mut file);
    let mut br = ByteReader::new(&mut fr);
    let mut dmx = create_demuxer(dmx_f, &mut br).expect("demuxer can't be created");

    let mut decs: Vec<Option<(Box<NADecoderSupport>, Box<dyn NADecoderMT>)>> = 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, dec_threads).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
        };
    let mut reord = MTFrameReorderer::new();
    let mut last_ts = None;
    'dec_loop: loop {
        let pktres = dmx.get_frame();
        if let Err(e) = pktres {
            if e == DemuxerError::EOF { break; }
            panic!("error");
        }
        let pkt = pktres.expect("packet");
        let streamno = pkt.get_stream().get_id() as usize;
        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 queue_id = reord.register_frame();
            match dec.queue_pkt(dsupp, &pkt, queue_id) {
                Ok(true) => {},
                Ok(false) => {
                    while !dec.can_take_input() || dec.has_output() {
                        match dec.get_frame() {
                            (Ok(frm), id) => {
                                reord.add_frame(frm, id);
                                while let Some(nfrm) = reord.get_frame() {
                                    if check_frame(nfrm, &test, &mut md5, &mut frameiter, &mut last_ts) {
                                        break 'dec_loop;
                                    }
                                }
                            },
                            (Err(err), id) => {
                                reord.drop_frame(id);
                                panic!("frame {} decoding error {:?}", id, err);
                            },
                        };
                    }
                    match dec.queue_pkt(dsupp, &pkt, queue_id) {
                        Ok(true) => {},
                        Ok(false) => panic!("still can't queue frame!"),
                        Err(err) => panic!("queueing error {:?}", err),
                    };
                },
                Err(err) => panic!("queueing error {:?}", err),
            };

        }
    }
    'tail_loop: for (_, ref mut dec) in decs.iter_mut().flatten() {
            loop {
                match dec.get_frame() {
                    (Ok(frm), id) => {
                        reord.add_frame(frm, id);
                        while let Some(nfrm) = reord.get_frame() {
                            if check_frame(nfrm, &test, &mut md5, &mut frameiter, &mut last_ts) {
                                break 'tail_loop;
                            }
                        }
                    },
                    (Err(DecoderError::NoFrame), _) => break,
                    (Err(err), id) => panic!("frame {} decoding error {:?}", id, err),
                };
            }
            while let Some(nfrm) = reord.get_last_frames() {
                if check_frame(nfrm, &test, &mut md5, &mut frameiter, &mut last_ts) {
                    break;
                }
            }
    }
    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");
    }
}

/// Tests decoding of provided file by outputting video frames as PNM (PPM for RGB video, PGM for YUV).
///
/// This function expects the following arguments:
/// * `demuxer` - container format name (used to find proper demuxer for it)
/// * `name` - input file name
/// * `video_pfx` - prefix for video frames written as pictures (output picture names should look like `<crate_name>/assets/test_out/PFXout00_000000.ppm`
/// * `limit` - optional PTS value after which decoding is stopped
/// * `dmx_reg` and `dec_reg` - registered demuxers and decoders that should contain demuxer and decoder(s) needed to decode the provided file.
///
/// Since the function is intended for tests, it will panic instead of returning an error.
pub fn test_decode_images(demuxer: &str, name: &str, video_pfx: &str, limit: Option<u64>,
                          dmx_reg: &RegisteredDemuxers, dec_reg: &RegisteredDecoders) {
    let dmx_f = dmx_reg.find_demuxer(demuxer).unwrap();
    let mut file = File::open(name).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<Option<(Box<NADecoderSupport>, Box<dyn NADecoder>)>> = Vec::new();
    for i in 0..dmx.get_num_streams() {
        let s = dmx.get_stream(i).unwrap();
        let info = s.get_info();
        let decfunc = dec_reg.find_decoder(info.get_name());
        if let Some(df) = decfunc {
            if info.is_video() {
                let mut dec = (df)();
                let mut dsupp = Box::new(NADecoderSupport::new());
                dec.init(&mut dsupp, info).unwrap();
                decs.push(Some((dsupp, dec)));
                break;
            } else {
                decs.push(None);
            }
        } else {
            decs.push(None);
        }
    }

    loop {
        let pktres = dmx.get_frame();
        if let Err(e) = pktres {
            if e == DemuxerError::EOF { break; }
            panic!("error");
        }
        let pkt = pktres.unwrap();
        let streamno = pkt.get_stream().get_id() as usize;
        if streamno >= decs.len() { continue; }
        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 frm.get_frame_type() != FrameType::Skip {
                let pts = if let Some(fpts) = frm.get_pts() { fpts } else { pkt.get_pts().unwrap() };
                let pfx = OUTPUT_PREFIX.to_owned() + video_pfx + "out";
                write_pnm(pfx.as_str(), streamno, pts, frm).unwrap();
            }
        }
    }
}