aboutsummaryrefslogtreecommitdiffstats
path: root/nihav-commonfmt/src/codecs/zmbvenc.rs
blob: 332cc0e47b9230c44dd6cdd8f754c5f4b290dfc2 (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
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
use nihav_core::codecs::*;
use nihav_core::compr::deflate::{Deflate, DeflateMode, DeflateWriter};
use nihav_core::io::byteio::*;

const RGB555_FORMAT: NAPixelFormaton = NAPixelFormaton { model: ColorModel::RGB(RGBSubmodel::RGB), components: 3,
                                        comp_info: [
                                            Some(NAPixelChromaton{ h_ss: 0, v_ss: 0, packed: true, depth: 5, shift: 10, comp_offs: 0, next_elem: 2 }),
                                            Some(NAPixelChromaton{ h_ss: 0, v_ss: 0, packed: true, depth: 5, shift:  5, comp_offs: 0, next_elem: 2 }),
                                            Some(NAPixelChromaton{ h_ss: 0, v_ss: 0, packed: true, depth: 5, shift:  0, comp_offs: 0, next_elem: 2 }),
                                            None, None],
                                        elem_size: 2, be: false, alpha: false, palette: false };
const RGB24_0_FORMAT: NAPixelFormaton = NAPixelFormaton { model: ColorModel::RGB(RGBSubmodel::RGB), components: 3,
                                        comp_info: [
                                            Some(NAPixelChromaton{ h_ss: 0, v_ss: 0, packed: true, depth: 8, shift: 0, comp_offs: 2, next_elem: 4 }),
                                            Some(NAPixelChromaton{ h_ss: 0, v_ss: 0, packed: true, depth: 8, shift: 0, comp_offs: 1, next_elem: 4 }),
                                            Some(NAPixelChromaton{ h_ss: 0, v_ss: 0, packed: true, depth: 8, shift: 0, comp_offs: 0, next_elem: 4 }),
                                            None, None],
                                        elem_size: 4, be: false, alpha: false, palette: false };

struct ZMBVEncoder {
    stream:     Option<NAStreamRef>,
    frm1:       Vec<u8>,
    frm2:       Vec<u8>,
    tmp_buf:    Vec<u8>,
    zbuf:       Vec<u8>,
    pal:        [u8; 768],
    pkt:        Option<NAPacket>,
    frmcount:   u8,
    key_int:    u8,
    tile_w:     usize,
    tile_h:     usize,
    cmode:      DeflateMode,
    compr:      Deflate,
    bpp:        u8,
    width:      usize,
    height:     usize,
    range:      usize,
    sent_pal:   bool,
}

fn buf_type_to_bpp(buf: &NABufferType) -> u8 {
    match buf {
        NABufferType::Video(ref vbuf) => {
            let vinfo = vbuf.get_info();
            if vinfo.get_format().is_paletted() {
                8
            } else {
                vinfo.get_format().get_total_depth()
            }
        },
        NABufferType::VideoPacked(ref vbuf) => {
            let vinfo = vbuf.get_info();
            vinfo.get_format().elem_size * 8
        },
        NABufferType::Video16(ref vbuf) => {
            let vinfo = vbuf.get_info();
            vinfo.get_format().get_total_depth()
        },
        _ => 0,
    }
}

fn copy_frame(buf: NABufferType, dst: &mut [u8], bpp: u8) -> EncoderResult<()> {
    match buf {
        NABufferType::Video(ref vbuf) => {
            if bpp != 8 {
                return Err(EncoderError::FormatError);
            }
            let off     = vbuf.get_offset(0);
            let stride  = vbuf.get_stride(0);
            let data    = vbuf.get_data();
            let w       = vbuf.get_info().get_width();
            let h       = vbuf.get_info().get_height();

            for (dline, sline) in dst.chunks_mut(w).zip(data[off..].chunks(stride)).take(h) {
                dline[..w].copy_from_slice(&sline[..w]);
            }
        },
        NABufferType::Video16(ref vbuf) => {
            let off     = vbuf.get_offset(0);
            let stride  = vbuf.get_stride(0);
            let data    = vbuf.get_data();
            let w       = vbuf.get_info().get_width();
            let h       = vbuf.get_info().get_height();

            for (dline, sline) in dst.chunks_mut(w * 2).zip(data[off..].chunks(stride)).take(h) {
                for (dst, &src) in dline[..w * 2].chunks_exact_mut(2).zip(sline.iter()) {
                    dst[0] = src as u8;
                    dst[1] = (src >> 8) as u8;
                }
            }
        },
        NABufferType::VideoPacked(ref vbuf) => {
            let off     = vbuf.get_offset(0);
            let stride  = vbuf.get_stride(0);
            let data    = vbuf.get_data();
            let w       = vbuf.get_info().get_width();
            let h       = vbuf.get_info().get_height();
            let w = w * (((bpp as usize) + 7) / 8);

            for (dline, sline) in dst.chunks_mut(w).zip(data[off..].chunks(stride)).take(h) {
                dline[..w].copy_from_slice(&sline[..w]);
            }
        },
        _ => return Err(EncoderError::FormatError),
    };
    Ok(())
}

fn to_signed(val: usize) -> isize {
    if (val & 1) == 0 {
        (val >> 1) as isize
    } else {
        -((val >> 1) as isize) - 1
    }
}

impl ZMBVEncoder {
    fn new() -> Self {
        Self {
            stream:     None,
            pkt:        None,
            frm1:       Vec::new(),
            frm2:       Vec::new(),
            pal:        [0; 768],
            tmp_buf:    Vec::new(),
            zbuf:       Vec::new(),
            frmcount:   0,
            key_int:    25,
            tile_w:     16,
            tile_h:     16,
            cmode:      DeflateMode::default(),
            compr:      Deflate::new(DeflateMode::default()),
            bpp:        0,
            width:      0,
            height:     0,
            range:      128,
            sent_pal:   false,
        }
    }
    fn encode_intra(&mut self, bw: &mut ByteWriter, buf: NABufferType) -> EncoderResult<()> {
        let bpp = buf_type_to_bpp(&buf);

        if let NABufferType::None = buf {
            if self.bpp == 0 {
                return Err(EncoderError::FormatError);
            }
            self.frm1.copy_from_slice(&self.frm2);
        } else {
            if bpp == 0 {
                return Err(EncoderError::FormatError);
            }
            self.bpp = bpp;

            if let (NABufferType::Video(ref vbuf), true) = (&buf, bpp == 8) {
                let off = vbuf.get_offset(1);
                let data = vbuf.get_data();
                self.pal.copy_from_slice(&data[off..][..768]);
            }

            copy_frame(buf, &mut self.frm1, self.bpp)?;
        }

        bw.write_byte(1)?; // intra flag
        bw.write_byte(0)?; // high version
        bw.write_byte(1)?; // low version
        bw.write_byte(if self.cmode == DeflateMode::NoCompr { 0 } else { 1 })?;
        let fmt = match self.bpp {
                 8 => 4,
                15 => 5,
                16 => 6,
                24 => 7,
                32 => 8,
                 _ => unreachable!(),
            };
        bw.write_byte(fmt)?;
        bw.write_byte(self.tile_w as u8)?;
        bw.write_byte(self.tile_h as u8)?;

        let bm = ((bpp as usize) + 7) / 8;
        if self.cmode == DeflateMode::NoCompr {
            if bpp == 8 {
                bw.write_buf(&self.pal)?;
            }
            bw.write_buf(&self.frm1[..self.width * self.height * bm])?;
        } else {
            self.tmp_buf.clear();
            if bpp == 8 {
                self.tmp_buf.extend_from_slice(&self.pal);
            }
            self.tmp_buf.extend_from_slice(&self.frm1[..self.width * self.height * bm]);
            self.compr = Deflate::new(self.cmode);

            let mut db = Vec::new();
            std::mem::swap(&mut db, &mut self.zbuf);
            db.clear();
            let mut wr = DeflateWriter::new(db);
            self.compr.write_zlib_header(&mut wr);
            self.compr.compress(&self.tmp_buf, &mut wr);
            self.compr.compress_flush(&mut wr);
            let mut db = wr.end();
            std::mem::swap(&mut db, &mut self.zbuf);

            bw.write_buf(&self.zbuf)?;
        }

        Ok(())
    }
    fn encode_inter(&mut self, bw: &mut ByteWriter, buf: NABufferType) -> EncoderResult<()> {
        if let NABufferType::None = buf {
            self.frm1.copy_from_slice(&self.frm2);

            bw.write_byte(0)?;
            self.tmp_buf.clear();
            let tile_w = (self.width  + self.tile_w - 1) / self.tile_w;
            let tile_h = (self.height + self.tile_h - 1) / self.tile_h;
            let mv_size = (tile_w * tile_h * 2 + 3) & !3;
            for _ in 0..mv_size {
                self.tmp_buf.push(0);
            }
            if self.cmode == DeflateMode::NoCompr {
                bw.write_buf(&self.tmp_buf)?;
            } else {
                let mut db = Vec::new();

                std::mem::swap(&mut db, &mut self.zbuf);
                db.clear();
                let mut wr = DeflateWriter::new(db);
                self.compr.compress(&self.tmp_buf, &mut wr);
                self.compr.compress_flush(&mut wr);
                let mut db = wr.end();
                std::mem::swap(&mut db, &mut self.zbuf);

                bw.write_buf(&self.zbuf)?;
            }
            return Ok(());
        }
        let bpp = buf_type_to_bpp(&buf);
        if bpp == 0 || bpp != self.bpp {
            return Err(EncoderError::FormatError);
        }

        self.tmp_buf.clear();
        if let (NABufferType::Video(ref vbuf), true) = (&buf, bpp == 8) {
            let mut npal = [0; 768];
            let off = vbuf.get_offset(1);
            let data = vbuf.get_data();
            npal.copy_from_slice(&data[off..][..768]);
            let mut cmp = true;
            for (&a, &b) in self.pal.iter().zip(npal.iter()) {
                if a != b {
                    cmp = false;
                    break;
                }
            }
            if !cmp {
                for (&a, &b) in self.pal.iter().zip(npal.iter()) {
                    self.tmp_buf.push(a ^ b);
                }
                self.pal = npal;

                bw.write_byte(2)?;

                self.sent_pal = false;
            } else {
                bw.write_byte(0)?;
            }
        } else {
            bw.write_byte(0)?;
        }
        copy_frame(buf, &mut self.frm1, self.bpp)?;

        let tile_w = (self.width  + self.tile_w - 1) / self.tile_w;
        let tile_h = (self.height + self.tile_h - 1) / self.tile_h;
        let mut mv_start = self.tmp_buf.len();
        let mv_size = (tile_w * tile_h * 2 + 3) & !3;
        for _ in 0..mv_size {
            self.tmp_buf.push(0);
        }

        let bpp = ((self.bpp as usize) + 7) / 8;
        let stride = self.width * bpp;
        let mut off = 0;
        for y in (0..self.height).step_by(self.tile_h) {
            let cur_h = (self.height - y).min(self.tile_h);
            for x in (0..self.width).step_by(self.tile_w) {
                let cur_w = (self.width - x).min(self.tile_w);

                let mut best_dist = std::u32::MAX;
                let mut best_x = x;
                let mut best_y = y;

                'search: for yoff in 0..self.range {
                    let ypos = (y as isize) + to_signed(yoff);
                    if ypos < 0 {
                        continue;
                    }
                    let ypos = ypos as usize;
                    if ypos + cur_h > self.height {
                        break;
                    }
                    for xoff in 0..self.range {
                        let xpos = (x as isize) + to_signed(xoff);
                        if xpos < 0 {
                            continue;
                        }
                        let xpos = xpos as usize;
                        if xpos + cur_w > self.width {
                            break;
                        }

                        let mut diff = 0;
                        let roff = xpos * bpp + ypos * stride;
                        for (line0, line1) in self.frm1[off..].chunks(stride).take(cur_h).zip(self.frm2[roff..].chunks(stride)) {
                            for (&a, &b) in line0[..cur_w * bpp].iter().zip(line1[..cur_w * bpp].iter()) {
                                diff += u32::from(a ^ b);
                            }
                        }

                        if best_dist > diff {
                            best_dist = diff;
                            best_x = xpos;
                            best_y = ypos;
                            if diff == 0 {
                                break 'search;
                            }
                        }
                    }
                }

                let has_delta = best_dist != 0;
                self.tmp_buf[mv_start] = (best_x.wrapping_sub(x) << 1) as u8;
                if has_delta {
                    self.tmp_buf[mv_start] |= 1;
                }
                self.tmp_buf[mv_start + 1] = (best_y.wrapping_sub(y) << 1) as u8;
                mv_start += 2;
                if has_delta {
                    let rpos = best_x * bpp + best_y * stride;
                    for (line0, line1) in self.frm1[off..].chunks(stride).take(cur_h).zip(self.frm2[rpos..].chunks(stride)) {
                        for (&a, &b) in line0[..cur_w * bpp].iter().zip(line1[..cur_w * bpp].iter()) {
                            self.tmp_buf.push(a ^ b);
                        }
                    }
                }

                off += self.tile_w * bpp;
            }
            off -= tile_w * self.tile_w * bpp;
            off += stride * self.tile_h;
        }

        if self.cmode == DeflateMode::NoCompr {
            bw.write_buf(&self.tmp_buf)?;
        } else {
            let mut db = Vec::new();

            std::mem::swap(&mut db, &mut self.zbuf);
            db.clear();
            let mut wr = DeflateWriter::new(db);
            self.compr.compress(&self.tmp_buf, &mut wr);
            self.compr.compress_flush(&mut wr);
            let mut db = wr.end();
            std::mem::swap(&mut db, &mut self.zbuf);

            bw.write_buf(&self.zbuf)?;
        }

        Ok(())
    }
}

impl NAEncoder for ZMBVEncoder {
    fn negotiate_format(&self, encinfo: &EncodeParameters) -> EncoderResult<EncodeParameters> {
        match encinfo.format {
            NACodecTypeInfo::None => {
                Ok(EncodeParameters {
                    format: NACodecTypeInfo::Video(NAVideoInfo::new(0, 0, true, YUV420_FORMAT)),
                    ..Default::default()
                })
            },
            NACodecTypeInfo::Audio(_) => Err(EncoderError::FormatError),
            NACodecTypeInfo::Video(vinfo) => {
                let depth = vinfo.format.get_total_depth();
                let pix_fmt = if vinfo.format.is_paletted() {
                        PAL8_FORMAT
                    } else if !vinfo.format.model.is_rgb() || depth > 16 {
                        RGB24_0_FORMAT
                    } else if depth < 16 {
                        RGB555_FORMAT
                    } else {
                        RGB565_FORMAT
                    };
                let outinfo = NAVideoInfo::new(vinfo.width, vinfo.height, false, pix_fmt);
                let mut ofmt = *encinfo;
                ofmt.format = NACodecTypeInfo::Video(outinfo);
                Ok(ofmt)
            }
        }
    }
    fn get_capabilities(&self) -> u64 { ENC_CAPS_SKIPFRAME }
    fn init(&mut self, stream_id: u32, encinfo: EncodeParameters) -> EncoderResult<NAStreamRef> {
        match encinfo.format {
            NACodecTypeInfo::None => Err(EncoderError::FormatError),
            NACodecTypeInfo::Audio(_) => Err(EncoderError::FormatError),
            NACodecTypeInfo::Video(vinfo) => {
                self.width  = vinfo.width;
                self.height = vinfo.height;

                let out_info = NAVideoInfo::new(vinfo.width, vinfo.height, false, vinfo.format);
                let info = NACodecInfo::new("zmbv", NACodecTypeInfo::Video(out_info), None);
                let mut stream = NAStream::new(StreamType::Video, stream_id, info, encinfo.tb_num, encinfo.tb_den, 0);
                stream.set_num(stream_id as usize);
                let stream = stream.into_ref();

                self.stream = Some(stream.clone());

                self.frm1 = vec![0; vinfo.width * vinfo.height * 4];
                self.frm2 = vec![0; vinfo.width * vinfo.height * 4];

                Ok(stream)
            },
        }
    }
    fn encode(&mut self, frm: &NAFrame) -> EncoderResult<()> {
        let buf = frm.get_buffer();
        let mut dbuf = Vec::with_capacity(4);
        let mut gw   = GrowableMemoryWriter::new_write(&mut dbuf);
        let mut bw   = ByteWriter::new(&mut gw);
        let is_intra = if self.frmcount == 0 {
                self.encode_intra(&mut bw, buf)?;
                true
            } else {
                self.encode_inter(&mut bw, buf)?;
                false
            };
        self.pkt = Some(NAPacket::new(self.stream.clone().unwrap(), frm.ts, is_intra, dbuf));
        if self.bpp == 8 && !self.sent_pal {
            if let NABufferType::Video(ref buf) = frm.get_buffer() {
                let paloff = buf.get_offset(1);
                let data = buf.get_data();
                let mut pal = [0; 1024];
                let srcpal = &data[paloff..][..768];
                for (dclr, sclr) in pal.chunks_exact_mut(4).zip(srcpal.chunks_exact(3)) {
                    dclr[..3].copy_from_slice(sclr);
                }
                if let Some(ref mut pkt) = &mut self.pkt {
                    pkt.side_data.push(NASideData::Palette(true, Arc::new(pal)));
                }
            }
            self.sent_pal = true;
        }
        self.frmcount += 1;
        if self.frmcount == self.key_int {
            self.frmcount = 0;
        }
        std::mem::swap(&mut self.frm1, &mut self.frm2);
        Ok(())
    }
    fn get_packet(&mut self) -> EncoderResult<Option<NAPacket>> {
        let mut npkt = None;
        std::mem::swap(&mut self.pkt, &mut npkt);
        Ok(npkt)
    }
    fn flush(&mut self) -> EncoderResult<()> {
        self.frmcount = 0;
        Ok(())
    }
}

const ENCODER_OPTS: &[NAOptionDefinition] = &[
    NAOptionDefinition {
        name: KEYFRAME_OPTION, description: KEYFRAME_OPTION_DESC,
        opt_type: NAOptionDefinitionType::Int(Some(0), Some(128)) },
    NAOptionDefinition {
        name: "range", description: "Block search range (0-128)",
        opt_type: NAOptionDefinitionType::Int(Some(0), Some(128)) },
    NAOptionDefinition {
        name: "tile_width", description: "Block width (1-255)",
        opt_type: NAOptionDefinitionType::Int(Some(1), Some(255)) },
    NAOptionDefinition {
        name: "tile_height", description: "Block width (1-255)",
        opt_type: NAOptionDefinitionType::Int(Some(1), Some(255)) },
    NAOptionDefinition {
        name: "compr_level", description: "Compression level",
        opt_type: DEFLATE_OPTION_VALUES },
];

impl NAOptionHandler for ZMBVEncoder {
    fn get_supported_options(&self) -> &[NAOptionDefinition] { ENCODER_OPTS }
    fn set_options(&mut self, options: &[NAOption]) {
        for option in options.iter() {
            for opt_def in ENCODER_OPTS.iter() {
                if opt_def.check(option).is_ok() {
                    match option.name {
                        "compr_level" => {
                            if let NAValue::String(ref s) = option.value {
                                if let Ok(val) = s.parse::<DeflateMode>() {
                                    self.cmode = val;
                                }
                            }
                        },
                        KEYFRAME_OPTION => {
                            if let NAValue::Int(intval) = option.value {
                                self.key_int = intval as u8;
                            }
                        },
                        "range" => {
                            if let NAValue::Int(intval) = option.value {
                                self.range = intval as usize;
                            }
                        },
                        "tile_width" => {
                            if let NAValue::Int(intval) = option.value {
                                self.tile_w = intval as usize;
                            }
                        },
                        "tile_height" => {
                            if let NAValue::Int(intval) = option.value {
                                self.tile_h = intval as usize;
                            }
                        },
                        _ => {},
                    };
                }
            }
        }
    }
    fn query_option_value(&self, name: &str) -> Option<NAValue> {
        match name {
            "compr_level" => Some(NAValue::String(self.cmode.to_string())),
            KEYFRAME_OPTION => Some(NAValue::Int(i64::from(self.key_int))),
            "range"       => Some(NAValue::Int(self.range as i64)),
            "tile_width"  => Some(NAValue::Int(self.tile_w as i64)),
            "tile_height" => Some(NAValue::Int(self.tile_h as i64)),
            _ => None,
        }
    }
}

pub fn get_encoder() -> Box<dyn NAEncoder + Send> {
    Box::new(ZMBVEncoder::new())
}

#[cfg(test)]
mod test {
    use nihav_core::codecs::*;
    use nihav_core::demuxers::*;
    use nihav_core::muxers::*;
    use crate::*;
    use nihav_codec_support::test::enc_video::*;
    use super::{RGB555_FORMAT, RGB24_0_FORMAT};

    // samples are from https://samples.mplayerhq.hu/V-codecs/ZMBV/
    #[test]
    fn test_zmbv_encoder_8bit() {
        let mut dmx_reg = RegisteredDemuxers::new();
        generic_register_all_demuxers(&mut dmx_reg);
        let mut dec_reg = RegisteredDecoders::new();
        generic_register_all_decoders(&mut dec_reg);
        let mut mux_reg = RegisteredMuxers::new();
        generic_register_all_muxers(&mut mux_reg);
        let mut enc_reg = RegisteredEncoders::new();
        generic_register_all_encoders(&mut enc_reg);

        let dec_config = DecoderTestParams {
                demuxer:        "avi",
                in_name:        "assets/Misc/td3_000.avi",
                stream_type:    StreamType::Video,
                limit:          Some(20),
                dmx_reg, dec_reg,
            };
        let enc_config = EncoderTestParams {
                muxer:          "avi",
                enc_name:       "zmbv",
                out_name:       "zmbv8.avi",
                mux_reg, enc_reg,
            };
        let dst_vinfo = NAVideoInfo {
                width:   0,
                height:  0,
                format:  PAL8_FORMAT,
                flipped: false,
                bits:    8,
            };
        let enc_params = EncodeParameters {
                format:  NACodecTypeInfo::Video(dst_vinfo),
                quality: 0,
                bitrate: 0,
                tb_num:  0,
                tb_den:  0,
                flags:   0,
            };
        let enc_options = &[
                NAOption { name: "range", value: NAValue::Int(16) },
            ];
        //test_encoding_to_file(&dec_config, &enc_config, enc_params, enc_options);
        test_encoding_md5(&dec_config, &enc_config, enc_params, enc_options,
                          &[0x08615111, 0x6f644a35, 0xa4e28f32, 0x35d2e66c]);
    }

    #[test]
    fn test_zmbv_encoder_15bit() {
        let mut dmx_reg = RegisteredDemuxers::new();
        generic_register_all_demuxers(&mut dmx_reg);
        let mut dec_reg = RegisteredDecoders::new();
        generic_register_all_decoders(&mut dec_reg);
        let mut mux_reg = RegisteredMuxers::new();
        generic_register_all_muxers(&mut mux_reg);
        let mut enc_reg = RegisteredEncoders::new();
        generic_register_all_encoders(&mut enc_reg);

        let dec_config = DecoderTestParams {
                demuxer:        "avi",
                in_name:        "assets/Misc/zmbv_15bit.avi",
                stream_type:    StreamType::Video,
                limit:          Some(4),
                dmx_reg, dec_reg,
            };
        let enc_config = EncoderTestParams {
                muxer:          "avi",
                enc_name:       "zmbv",
                out_name:       "zmbv15.avi",
                mux_reg, enc_reg,
            };
        let dst_vinfo = NAVideoInfo {
                width:   0,
                height:  0,
                format:  RGB555_FORMAT,
                flipped: false,
                bits:    8,
            };
        let enc_params = EncodeParameters {
                format:  NACodecTypeInfo::Video(dst_vinfo),
                quality: 0,
                bitrate: 0,
                tb_num:  0,
                tb_den:  0,
                flags:   0,
            };
        let enc_options = &[
                NAOption { name: "range", value: NAValue::Int(16) },
            ];
        //test_encoding_to_file(&dec_config, &enc_config, enc_params, enc_options);
        test_encoding_md5(&dec_config, &enc_config, enc_params, enc_options,
                          &[0x1cff4116, 0x5926d91b, 0x60aac53f, 0x8a22bc7b]);
    }

    #[test]
    fn test_zmbv_encoder_16bit() {
        let mut dmx_reg = RegisteredDemuxers::new();
        generic_register_all_demuxers(&mut dmx_reg);
        let mut dec_reg = RegisteredDecoders::new();
        generic_register_all_decoders(&mut dec_reg);
        let mut mux_reg = RegisteredMuxers::new();
        generic_register_all_muxers(&mut mux_reg);
        let mut enc_reg = RegisteredEncoders::new();
        generic_register_all_encoders(&mut enc_reg);

        let dec_config = DecoderTestParams {
                demuxer:        "avi",
                in_name:        "assets/Misc/zmbv_16bit.avi",
                stream_type:    StreamType::Video,
                limit:          Some(4),
                dmx_reg, dec_reg,
            };
        let enc_config = EncoderTestParams {
                muxer:          "avi",
                enc_name:       "zmbv",
                out_name:       "zmbv16.avi",
                mux_reg, enc_reg,
            };
        let dst_vinfo = NAVideoInfo {
                width:   0,
                height:  0,
                format:  RGB565_FORMAT,
                flipped: false,
                bits:    8,
            };
        let enc_params = EncodeParameters {
                format:  NACodecTypeInfo::Video(dst_vinfo),
                quality: 0,
                bitrate: 0,
                tb_num:  0,
                tb_den:  0,
                flags:   0,
            };
        let enc_options = &[
                NAOption { name: "range", value: NAValue::Int(16) },
            ];
        //test_encoding_to_file(&dec_config, &enc_config, enc_params, enc_options);
        test_encoding_md5(&dec_config, &enc_config, enc_params, enc_options,
                          &[0xb48aa633, 0x673539fe, 0xa312d45a, 0x76eee134]);
    }

    #[test]
    fn test_zmbv_encoder_32bit() {
        let mut dmx_reg = RegisteredDemuxers::new();
        generic_register_all_demuxers(&mut dmx_reg);
        let mut dec_reg = RegisteredDecoders::new();
        generic_register_all_decoders(&mut dec_reg);
        let mut mux_reg = RegisteredMuxers::new();
        generic_register_all_muxers(&mut mux_reg);
        let mut enc_reg = RegisteredEncoders::new();
        generic_register_all_encoders(&mut enc_reg);

        let dec_config = DecoderTestParams {
                demuxer:        "avi",
                in_name:        "assets/Misc/zmbv_32bit.avi",
                stream_type:    StreamType::Video,
                limit:          Some(4),
                dmx_reg, dec_reg,
            };
        let enc_config = EncoderTestParams {
                muxer:          "avi",
                enc_name:       "zmbv",
                out_name:       "zmbv32.avi",
                mux_reg, enc_reg,
            };
        let dst_vinfo = NAVideoInfo {
                width:   0,
                height:  0,
                format:  RGB24_0_FORMAT,
                flipped: false,
                bits:    8,
            };
        let enc_params = EncodeParameters {
                format:  NACodecTypeInfo::Video(dst_vinfo),
                quality: 0,
                bitrate: 0,
                tb_num:  0,
                tb_den:  0,
                flags:   0,
            };
        let enc_options = &[
                NAOption { name: "range", value: NAValue::Int(16) },
            ];
        //test_encoding_to_file(&dec_config, &enc_config, enc_params, enc_options);
        test_encoding_md5(&dec_config, &enc_config, enc_params, enc_options,
                          &[0x0836152c, 0xfcd7e1fc, 0xf1e2f619, 0x874d3dbc]);
    }
}