aboutsummaryrefslogtreecommitdiffstats
path: root/nihav-game/src/codecs/gremlinvideo.rs
blob: 1488b00257df008d90fe905becf9aab172e88f05 (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
use nihav_core::frame::*;
use nihav_core::formats;
use nihav_core::formats::{NAChannelType, NAChannelMap};
use nihav_core::codecs::*;
use nihav_core::io::byteio::*;

struct GremlinVideoDecoder {
    info:       NACodecInfoRef,
    pal:        [u8; 768],
    frame:      Vec<u8>,
    scale_v:    bool,
    scale_h:    bool,
}

struct Bits8 {
    queue: u8,
    fill:  u8,
}

struct Bits32 {
    queue: u32,
    fill:  u8,
}

const PREAMBLE_SIZE: usize = 4096;

impl Bits8 {
    fn new() -> Self { Bits8 { queue: 0, fill: 0 } }
    fn read_2bits(&mut self, br: &mut ByteReader) -> ByteIOResult<u8> {
        if self.fill == 0 {
            self.queue  = br.read_byte()?;
            self.fill  += 8;
        }
        let res = self.queue >> 6;
        self.queue <<= 2;
        self.fill   -= 2;
        Ok(res)
    }
}

impl Bits32 {
    fn new() -> Self { Bits32 { queue: 0, fill: 0 } }
    fn fill(&mut self, br: &mut ByteReader) -> ByteIOResult<()> {
        self.queue = br.read_u32le()?;
        self.fill  = 32;
        Ok(())
    }
    fn read_bits(&mut self, br: &mut ByteReader, nbits: u8) -> ByteIOResult<u32> {
        let res = self.queue & ((1 << nbits) - 1);
        self.queue >>= nbits;
        self.fill   -= nbits;
        if self.fill <= 16 {
            self.queue |= u32::from(br.read_u16le()?) << self.fill;
            self.fill  += 16;
        }
        Ok(res)
    }
}

impl GremlinVideoDecoder {
    fn new() -> Self {
        GremlinVideoDecoder {
            info: NACodecInfoRef::default(), pal: [0; 768], frame: Vec::new(),
            scale_v: false, scale_h: false
        }
    }

    fn lz_copy(&mut self, idx: usize, offset: isize, len: usize) -> DecoderResult<()> {
        if idx + len > self.frame.len() { return Err(DecoderError::InvalidData); }
        if offset == -1 {
            let c = self.frame[idx - 1];
            for i in 0..len { self.frame[idx + i] = c; }
        } else if offset < 0 {
            let start = idx - (-offset as usize);
            for i in 0..len { self.frame[idx + i] = self.frame[start + i]; }
        } else {
            if idx + (offset as usize) + len > self.frame.len() { return Err(DecoderError::InvalidData); }
            let start = idx + (offset as usize);
            for i in 0..len { self.frame[idx + i] = self.frame[start + i]; }
        }
        Ok(())
    }

    fn rescale(&mut self, w: usize, h: usize, scale_v: bool, scale_h: bool) {
        if (self.scale_v == scale_v) && (self.scale_h == scale_h) { return; }

        if self.scale_h && self.scale_v {
            for j in 0..h {
                let y = h - j - 1;
                for i in 0..w {
                    let x = w - i - 1;
                    self.frame[PREAMBLE_SIZE + x + y * w] = self.frame[PREAMBLE_SIZE + x/2 + (y/2) * (w/2)];
                }
            }
        } else if self.scale_h {
            for j in 0..h {
                let y = h - j - 1;
                for x in 0..w {
                    self.frame[PREAMBLE_SIZE + x + y * w] = self.frame[PREAMBLE_SIZE + x + (y/2) * w];
                }
            }
        } else if self.scale_v {
            for j in 0..h {
                let y = h - j - 1;
                for i in 0..w {
                    let x = w - i - 1;
                    self.frame[PREAMBLE_SIZE + x + y * w] = self.frame[PREAMBLE_SIZE + x/2 + y * (w/2)];
                }
            }
        }

        if scale_h && scale_v {
            for y in 0..h/2 {
                for x in 0..w/2 {
                    self.frame[PREAMBLE_SIZE + x + y * (w/2)] = self.frame[PREAMBLE_SIZE + x*2 + y*2 * w];
                }
            }
        } else if scale_h {
            for y in 0..h/2 {
                for x in 0..w {
                    self.frame[PREAMBLE_SIZE + x + y * w] = self.frame[PREAMBLE_SIZE + x + y*2 * w];
                }
            }
        } else if scale_v {
            for y in 0..h {
                for x in 0..w/2 {
                    self.frame[PREAMBLE_SIZE + x + y * w] = self.frame[PREAMBLE_SIZE + x*2 + y * w];
                }
            }
        }

        self.scale_v = scale_v;
        self.scale_h = scale_h;
    }

    fn output_frame(&mut self, bufinfo: &mut NABufferType, w: usize, h: usize) {
        let bufo = bufinfo.get_vbuf();
        let mut buf = bufo.unwrap();
        let paloff = buf.get_offset(1);
        let stride = buf.get_stride(0);
        let data = buf.get_data_mut().unwrap();
        let dst = data.as_mut_slice();
        let mut sidx = PREAMBLE_SIZE;
        let mut didx = 0;

        for i in 0..768 { dst[paloff + i] = self.pal[i]; }
        if !self.scale_v && !self.scale_h {
            for _ in 0..h {
                for x in 0..w { dst[didx + x] = self.frame[sidx + x]; }
                sidx += w;
                didx += stride;
            }
        } else {
            for y in 0..h {
                if !self.scale_v {
                    for x in 0..w { dst[didx + x] = self.frame[sidx + x]; }
                } else {
                    for x in 0..w { dst[didx + x] = self.frame[sidx + x/2]; }
                }
                if !self.scale_h || ((y & 1) == 1) {
                    sidx += if !self.scale_v { w } else { w/2 };
                }
                didx += stride;
            }
        }
    }

    fn decode_method2(&mut self, br: &mut ByteReader) -> DecoderResult<()> {
        let mut bits = Bits8::new();

        let mut size = self.info.get_properties().get_video_info().unwrap().get_width() *
                        self.info.get_properties().get_video_info().unwrap().get_height();
        let mut idx = PREAMBLE_SIZE;
        if self.frame[8] != 0 {
            for c in 0..256 {
                for i in 0..16 { self.frame[c * 16 + i] = c as u8; }
            }
        }
        while size > 0 {
            let tag = bits.read_2bits(br)?;
            if tag == 0 {
                self.frame[idx] = br.read_byte()?;
                size -= 1;
                idx  += 1;
            } else if tag == 1 {
                let b = br.read_byte()?;
                let len = ((b & 0xF) as usize) + 3;
                let bot = (b >> 4) as isize;
                let off = ((br.read_byte()? as isize) << 4) + bot - 4096;
                validate!(len <= size);
                size -= len;
                self.lz_copy(idx, off, len)?;
                idx += len;
            } else if tag == 2 {
                let len = (br.read_byte()? as usize) + 2;
                validate!(len <= size);
                size -= len;
                idx += len;
            } else {
                break;
            }
        }
        Ok(())
    }

    fn decode_method5(&mut self, br: &mut ByteReader, skip: usize) -> DecoderResult<()> {
        let mut bits = Bits8::new();

        let mut size = self.info.get_properties().get_video_info().unwrap().get_width() *
                        self.info.get_properties().get_video_info().unwrap().get_height();
        let mut idx = PREAMBLE_SIZE;
        validate!(size >= skip);
        size -= skip;
        idx  += skip;
        while size > 0 {
            let tag = bits.read_2bits(br)?;
            if tag == 0 {
                self.frame[idx] = br.read_byte()?;
                size -= 1;
                idx  += 1;
            } else if tag == 1 {
                let b = br.read_byte()?;
                let len = ((b & 0xF) as usize) + 3;
                let bot = (b >> 4) as isize;
                let off = ((br.read_byte()? as isize) << 4) + bot - 4096;
                validate!(len <= size);
                size -= len;
                self.lz_copy(idx, off, len)?;
                idx += len;
            } else if tag == 2 {
                let b = br.read_byte()?;
                if b == 0 { break; }
                let len: usize = (if b != 0xFF { b as usize } else { br.read_u16le()? as usize }) + 1;
                validate!(len <= size);
                size -= len;
                idx += len;
            } else {
                let b = br.read_byte()?;
                let len = ((b & 0x3) as usize) + 2;
                let off = -((b >> 2) as isize) - 1;
                validate!(len <= size);
                size -= len;
                self.lz_copy(idx, off, len)?;
                idx += len;
            }
        }
        Ok(())
    }

    fn decode_method68(&mut self, br: &mut ByteReader,
                       skip: usize, use8: bool) -> DecoderResult<()> {
        let mut bits = Bits32::new();

        let mut size = self.info.get_properties().get_video_info().unwrap().get_width() *
                        self.info.get_properties().get_video_info().unwrap().get_height();
        let mut idx = PREAMBLE_SIZE;
        validate!(size >= skip);
        size -= skip;
        idx  += skip;
        bits.fill(br)?;
        while size > 0 {
            let tag = bits.read_bits(br, 2)?;
            if tag == 0 { //draw
                let b = bits.read_bits(br, 1)?;
                if b == 0 {
                    self.frame[idx] = br.read_byte()?;
                    size -= 1;
                    idx  += 1;
                } else {
                    let mut len: usize = 2;
                    let mut lbits = 0;
                    loop {
                        lbits += 1;
                        let val = bits.read_bits(br, lbits)?;
                        len += val as usize;
                        if val != ((1 << lbits) - 1) { break; }
                        validate!(lbits < 16);
                    }
                    validate!(len <= size);
                    for i in 0..len { self.frame[idx + i] = br.read_byte()?; }
                    size -= len;
                    idx  += len;
                }
            } else if tag == 1 { //skip
                let b = bits.read_bits(br, 1)?;
                let len: usize;
                if b == 0 {
                    len = (bits.read_bits(br, 4)? as usize) + 2;
                } else {
                    let bb = br.read_byte()?;
                    if (bb & 0x80) == 0 {
                        len = (bb as usize) + 18;
                    } else {
                        let top = ((bb & 0x7F) as usize) << 8;
                        len = top + (br.read_byte()? as usize) + 146;
                    }
                }
                validate!(len <= size);
                size -= len;
                idx  += len;
            } else if tag == 2 {
                let subtag = bits.read_bits(br, 2)? as usize;
                if subtag != 3 {
                    let top = (bits.read_bits(br, 4)? as usize) << 8;
                    let offs = top + (br.read_byte()? as usize);
                    if (subtag != 0) || (offs <= 0xF80) {
                        let len = (subtag as usize) + 3;
                        self.lz_copy(idx, (offs as isize) - 4096, len)?;
                        idx += len;
                    } else {
                        if offs == 0xFFF { return Ok(()); }
                        let real_off = ((offs >> 4) & 0x7) + 1;
                        let len = ((offs & 0xF) + 2) * 2;
                        validate!(len <= size);
                        size -= len;
                        let c1 = self.frame[idx - real_off];
                        let c2 = self.frame[idx - real_off + 1];
                        for i in 0..len/2 {
                            self.frame[idx + i*2 + 0] = c1;
                            self.frame[idx + i*2 + 1] = c2;
                        }
                        idx += len;
                    }
                } else {
                    let b = br.read_byte()?;
                    let off = ((b & 0x7F) as usize) + 1;
                    let len = if (b & 0x80) == 0 { 2 } else { 3 };
                    validate!(len <= size);
                    size -= len;
                    self.lz_copy(idx, -(off as isize), len)?;
                    idx += len;
                }
            } else {
                let len: usize;
                let off: isize;
                if use8 {
                    let b = br.read_byte()?;
                    if (b & 0xC0) == 0xC0 {
                        len = ((b & 0x3F) as usize) + 8;
                        let q = bits.read_bits(br, 4)? as isize;
                        off = (q << 8) + (br.read_byte()? as isize) + 1;
                    } else {
                        let ofs1: isize;
                        if (b & 0x80) == 0 {
                            len = ((b >> 4) as usize) + 6;
                            ofs1 = (b & 0xF) as isize;
                        } else {
                            len = ((b & 0x3F) as usize) + 14;
                            ofs1 = bits.read_bits(br, 4)? as isize;
                        }
                        off = (ofs1 << 8) + (br.read_byte()? as isize) - 4096;
                    }
                } else {
                    let b = br.read_byte()?;
                    if (b >> 4) == 0xF {
                        len = (br.read_byte()? as usize) + 21;
                    } else {
                        len = ((b >> 4) as usize) + 6;
                    }
                    let ofs1 = (b & 0xF) as isize;
                    off = (ofs1 << 8) + (br.read_byte()? as isize) - 4096;
                }
                validate!(len <= size);
                size -= len;
                self.lz_copy(idx, off, len)?;
                idx += len;
            }
        }
        Ok(())
    }
}

impl NADecoder for GremlinVideoDecoder {
    fn init(&mut self, _supp: &mut NADecoderSupport, info: NACodecInfoRef) -> DecoderResult<()> {
        if let NACodecTypeInfo::Video(vinfo) = info.get_properties() {
            let w = vinfo.get_width();
            let h = vinfo.get_height();
            if !vinfo.get_format().is_paletted() { return Err(DecoderError::NotImplemented); }
            let fmt = PAL8_FORMAT;
            let myinfo = NACodecTypeInfo::Video(NAVideoInfo::new(w, h, false, fmt));
            self.info = NACodecInfo::new_ref(info.get_name(), myinfo, info.get_extradata()).into_ref();

            self.frame.resize(PREAMBLE_SIZE + w * h, 0);
            for i in 0..2 {
                for j in 0..256 {
                    for k in 0..8 {
                        self.frame[i * 2048 + j * 8 + k] = j as u8;
                    }
                }
            }
            let edata = info.get_extradata().unwrap();
            validate!(edata.len() == 768);
            for c in 0..256 {
                for i in 0..3 {
                    let cc = edata[c * 3 + i];
                    self.pal[c * 3 + i] = (cc << 2) | (cc >> 4);
                }
            }
            Ok(())
        } else {
            Err(DecoderError::InvalidData)
        }
    }
    fn decode(&mut self, _supp: &mut NADecoderSupport, pkt: &NAPacket) -> DecoderResult<NAFrameRef> {
        let src = pkt.get_buffer();
        let mut mr = MemoryReader::new_read(&src);
        let mut br = ByteReader::new(&mut mr);
        let flags = br.read_u32le()?;
        let w = self.info.get_properties().get_video_info().unwrap().get_width();
        let h = self.info.get_properties().get_video_info().unwrap().get_height();

        let cmethod = flags & 0xF;
        let is_intra = (flags & 0x40) != 0;
        let scale_v = (flags & 0x10) != 0;
        let scale_h = (flags & 0x20) != 0;

        self.rescale(w, h, scale_v, scale_h);

        if (cmethod == 0) || (cmethod == 1) {
            for c in 0..256 {
                for i in 0..3 {
                    let b = br.read_byte()?;
                    self.pal[c * 3 + i] = (b << 2) | (b >> 4);
                }
            }
            if cmethod == 1 {
                for i in PREAMBLE_SIZE..self.frame.len() { self.frame[i] = 0x00; }
            }
            let mut frm = NAFrame::new_from_pkt(pkt, self.info.clone(), NABufferType::None);
            frm.set_keyframe(false);
            frm.set_frame_type(FrameType::Skip);
            return Ok(frm.into_ref())
        } else if cmethod == 3 {
            let mut frm = NAFrame::new_from_pkt(pkt, self.info.clone(), NABufferType::None);
            frm.set_keyframe(false);
            frm.set_frame_type(FrameType::Skip);
            return Ok(frm.into_ref())
        } else if cmethod == 2 {
            self.decode_method2(&mut br)?;
        } else if cmethod == 5 {
            self.decode_method5(&mut br, (flags >> 8) as usize)?;
        } else if cmethod == 6 {
            self.decode_method68(&mut br, (flags >> 8) as usize, false)?;
        } else if cmethod == 8 {
            self.decode_method68(&mut br, (flags >> 8) as usize, true)?;
        } else {
            return Err(DecoderError::NotImplemented);
        }

        let mut bufinfo = alloc_video_buffer(self.info.get_properties().get_video_info().unwrap(), 0)?;

        self.output_frame(&mut bufinfo, w, h);

        let mut frm = NAFrame::new_from_pkt(pkt, self.info.clone(), bufinfo);
        frm.set_keyframe(is_intra);
        frm.set_frame_type(if is_intra { FrameType::I } else { FrameType::P });
        Ok(frm.into_ref())
    }
    fn flush(&mut self) {
    }
}

pub fn get_decoder_video() -> Box<dyn NADecoder + Send> {
    Box::new(GremlinVideoDecoder::new())
}

struct GremlinAudioDecoder {
    ainfo:      NAAudioInfo,
    chmap:      NAChannelMap,
    delta_tab: [i16; 256],
    state0:     i16,
    state1:     i16,
}

impl GremlinAudioDecoder {
    fn new() -> Self {
        let mut delta_tab: [i16; 256] = [0; 256];
        let mut delta = 0;
        let mut code = 64;
        let mut step = 45;
        for i in 0..127 {
            delta += code >> 5;
            code  += step;
            step  += 2;
            delta_tab[i * 2 + 1] =  delta;
            delta_tab[i * 2 + 2] = -delta;
        }
        delta_tab[255] = 32767;//delta + (code >> 5);
        GremlinAudioDecoder {
            ainfo:  NAAudioInfo::new(0, 1, formats::SND_S16_FORMAT, 0),
            chmap:  NAChannelMap::new(),
            delta_tab,
            state0: 0,
            state1: 0,
        }
    }
}

const CHMAP_MONO: [NAChannelType; 1] = [NAChannelType::C];
const CHMAP_STEREO: [NAChannelType; 2] = [NAChannelType::L, NAChannelType::R];

fn get_default_chmap(nch: u8) -> NAChannelMap {
    let mut chmap = NAChannelMap::new();
    match nch {
        1 => chmap.add_channels(&CHMAP_MONO),
        2 => chmap.add_channels(&CHMAP_STEREO),
        _ => (),
    }
    chmap
}

impl NADecoder for GremlinAudioDecoder {
    fn init(&mut self, _supp: &mut NADecoderSupport, info: NACodecInfoRef) -> DecoderResult<()> {
        if let NACodecTypeInfo::Audio(ainfo) = info.get_properties() {
            self.ainfo = NAAudioInfo::new(ainfo.get_sample_rate(), ainfo.get_channels(), formats::SND_S16P_FORMAT, ainfo.get_block_len());
            self.chmap = get_default_chmap(ainfo.get_channels());
            if self.chmap.num_channels() == 0 { return Err(DecoderError::InvalidData); }
            Ok(())
        } else {
            Err(DecoderError::InvalidData)
        }
    }
    fn decode(&mut self, _supp: &mut NADecoderSupport, pkt: &NAPacket) -> DecoderResult<NAFrameRef> {
        let info = pkt.get_stream().get_info();
        if let NACodecTypeInfo::Audio(_) = info.get_properties() {
            let pktbuf = pkt.get_buffer();
            let samples = pktbuf.len() / self.chmap.num_channels();
            let abuf = alloc_audio_buffer(self.ainfo, samples, self.chmap.clone())?;
            let mut adata = abuf.get_abuf_i16().unwrap();
            let off1 = adata.get_offset(1);
            let buf = adata.get_data_mut().unwrap();
            if self.chmap.num_channels() == 2 {
                for (i, e) in pktbuf.chunks(2).enumerate() {
                    self.state0 = self.state0.wrapping_add(self.delta_tab[e[0] as usize]);
                    buf[i] = self.state0;
                    self.state1 = self.state1.wrapping_add(self.delta_tab[e[1] as usize]);
                    buf[off1 + i] = self.state1;
                }
            } else {
                for (i, e) in pktbuf.iter().enumerate() {
                    self.state0 += self.delta_tab[*e as usize];
                    buf[i] = self.state0;
                }
            }
            let mut frm = NAFrame::new_from_pkt(pkt, info, abuf);
            frm.set_duration(Some(samples as u64));
            frm.set_keyframe(false);
            Ok(frm.into_ref())
        } else {
            Err(DecoderError::InvalidData)
        }
    }
    fn flush(&mut self) {
    }
}

pub fn get_decoder_audio() -> Box<dyn NADecoder + Send> {
    Box::new(GremlinAudioDecoder::new())
}

#[cfg(test)]
mod test {
    use nihav_core::codecs::RegisteredDecoders;
    use nihav_core::demuxers::RegisteredDemuxers;
    use nihav_codec_support::test::dec_video::test_file_decoding;
    use crate::game_register_all_codecs;
    use crate::game_register_all_demuxers;
    #[test]
    fn test_gdv() {
        let mut dmx_reg = RegisteredDemuxers::new();
        game_register_all_demuxers(&mut dmx_reg);
        let mut dec_reg = RegisteredDecoders::new();
        game_register_all_codecs(&mut dec_reg);

        test_file_decoding("gdv", "assets/Game/intro1.gdv", Some(10), true, false, None, &dmx_reg, &dec_reg);
    }
}