]>
Commit | Line | Data |
---|---|---|
5641dccf KS |
1 | use nihav_core::frame::*; |
2 | use nihav_core::formats; | |
3 | use nihav_core::formats::{NAChannelType, NAChannelMap}; | |
4 | use nihav_core::codecs::*; | |
5 | use nihav_core::io::byteio::*; | |
e120f8fd KS |
6 | |
7 | struct GremlinVideoDecoder { | |
2422d969 | 8 | info: NACodecInfoRef, |
e120f8fd KS |
9 | pal: [u8; 768], |
10 | frame: Vec<u8>, | |
11 | scale_v: bool, | |
12 | scale_h: bool, | |
13 | } | |
14 | ||
8e652498 KS |
15 | struct Bits8 { |
16 | queue: u8, | |
e120f8fd KS |
17 | fill: u8, |
18 | } | |
19 | ||
20 | struct Bits32 { | |
21 | queue: u32, | |
22 | fill: u8, | |
23 | } | |
24 | ||
25 | const PREAMBLE_SIZE: usize = 4096; | |
26 | ||
8e652498 KS |
27 | impl Bits8 { |
28 | fn new() -> Self { Bits8 { queue: 0, fill: 0 } } | |
29 | fn read_2bits(&mut self, br: &mut ByteReader) -> ByteIOResult<u8> { | |
e120f8fd | 30 | if self.fill == 0 { |
8e652498 | 31 | self.queue = br.read_byte()?; |
e120f8fd KS |
32 | self.fill += 8; |
33 | } | |
8e652498 KS |
34 | let res = self.queue >> 6; |
35 | self.queue <<= 2; | |
e120f8fd KS |
36 | self.fill -= 2; |
37 | Ok(res) | |
38 | } | |
39 | } | |
40 | ||
41 | impl Bits32 { | |
42 | fn new() -> Self { Bits32 { queue: 0, fill: 0 } } | |
43 | fn fill(&mut self, br: &mut ByteReader) -> ByteIOResult<()> { | |
44 | self.queue = br.read_u32le()?; | |
45 | self.fill = 32; | |
46 | Ok(()) | |
47 | } | |
48 | fn read_bits(&mut self, br: &mut ByteReader, nbits: u8) -> ByteIOResult<u32> { | |
49 | let res = self.queue & ((1 << nbits) - 1); | |
50 | self.queue >>= nbits; | |
51 | self.fill -= nbits; | |
52 | if self.fill <= 16 { | |
e69b1148 | 53 | self.queue |= u32::from(br.read_u16le()?) << self.fill; |
e120f8fd KS |
54 | self.fill += 16; |
55 | } | |
56 | Ok(res) | |
57 | } | |
58 | } | |
59 | ||
60 | impl GremlinVideoDecoder { | |
61 | fn new() -> Self { | |
e120f8fd | 62 | GremlinVideoDecoder { |
2422d969 | 63 | info: NACodecInfoRef::default(), pal: [0; 768], frame: Vec::new(), |
e120f8fd KS |
64 | scale_v: false, scale_h: false |
65 | } | |
66 | } | |
67 | ||
68 | fn lz_copy(&mut self, idx: usize, offset: isize, len: usize) -> DecoderResult<()> { | |
69 | if idx + len > self.frame.len() { return Err(DecoderError::InvalidData); } | |
70 | if offset == -1 { | |
71 | let c = self.frame[idx - 1]; | |
72 | for i in 0..len { self.frame[idx + i] = c; } | |
73 | } else if offset < 0 { | |
74 | let start = idx - (-offset as usize); | |
75 | for i in 0..len { self.frame[idx + i] = self.frame[start + i]; } | |
76 | } else { | |
77 | if idx + (offset as usize) + len > self.frame.len() { return Err(DecoderError::InvalidData); } | |
78 | let start = idx + (offset as usize); | |
79 | for i in 0..len { self.frame[idx + i] = self.frame[start + i]; } | |
80 | } | |
81 | Ok(()) | |
82 | } | |
83 | ||
84 | fn rescale(&mut self, w: usize, h: usize, scale_v: bool, scale_h: bool) { | |
85 | if (self.scale_v == scale_v) && (self.scale_h == scale_h) { return; } | |
86 | ||
87 | if self.scale_h && self.scale_v { | |
88 | for j in 0..h { | |
89 | let y = h - j - 1; | |
90 | for i in 0..w { | |
91 | let x = w - i - 1; | |
92 | self.frame[PREAMBLE_SIZE + x + y * w] = self.frame[PREAMBLE_SIZE + x/2 + (y/2) * (w/2)]; | |
93 | } | |
94 | } | |
95 | } else if self.scale_h { | |
96 | for j in 0..h { | |
97 | let y = h - j - 1; | |
98 | for x in 0..w { | |
99 | self.frame[PREAMBLE_SIZE + x + y * w] = self.frame[PREAMBLE_SIZE + x + (y/2) * w]; | |
100 | } | |
101 | } | |
102 | } else if self.scale_v { | |
103 | for j in 0..h { | |
104 | let y = h - j - 1; | |
105 | for i in 0..w { | |
106 | let x = w - i - 1; | |
107 | self.frame[PREAMBLE_SIZE + x + y * w] = self.frame[PREAMBLE_SIZE + x/2 + y * (w/2)]; | |
108 | } | |
109 | } | |
110 | } | |
111 | ||
112 | if scale_h && scale_v { | |
113 | for y in 0..h/2 { | |
114 | for x in 0..w/2 { | |
115 | self.frame[PREAMBLE_SIZE + x + y * (w/2)] = self.frame[PREAMBLE_SIZE + x*2 + y*2 * w]; | |
116 | } | |
117 | } | |
118 | } else if scale_h { | |
119 | for y in 0..h/2 { | |
120 | for x in 0..w { | |
121 | self.frame[PREAMBLE_SIZE + x + y * w] = self.frame[PREAMBLE_SIZE + x + y*2 * w]; | |
122 | } | |
123 | } | |
124 | } else if scale_v { | |
125 | for y in 0..h { | |
126 | for x in 0..w/2 { | |
127 | self.frame[PREAMBLE_SIZE + x + y * w] = self.frame[PREAMBLE_SIZE + x*2 + y * w]; | |
128 | } | |
129 | } | |
130 | } | |
131 | ||
132 | self.scale_v = scale_v; | |
133 | self.scale_h = scale_h; | |
134 | } | |
135 | ||
136 | fn output_frame(&mut self, bufinfo: &mut NABufferType, w: usize, h: usize) { | |
137 | let bufo = bufinfo.get_vbuf(); | |
138 | let mut buf = bufo.unwrap(); | |
139 | let paloff = buf.get_offset(1); | |
140 | let stride = buf.get_stride(0); | |
1a967e6b | 141 | let data = buf.get_data_mut().unwrap(); |
9037cf6b | 142 | let dst = data.as_mut_slice(); |
e120f8fd KS |
143 | let mut sidx = PREAMBLE_SIZE; |
144 | let mut didx = 0; | |
145 | ||
146 | for i in 0..768 { dst[paloff + i] = self.pal[i]; } | |
147 | if !self.scale_v && !self.scale_h { | |
148 | for _ in 0..h { | |
149 | for x in 0..w { dst[didx + x] = self.frame[sidx + x]; } | |
150 | sidx += w; | |
151 | didx += stride; | |
152 | } | |
153 | } else { | |
154 | for y in 0..h { | |
155 | if !self.scale_v { | |
156 | for x in 0..w { dst[didx + x] = self.frame[sidx + x]; } | |
157 | } else { | |
158 | for x in 0..w { dst[didx + x] = self.frame[sidx + x/2]; } | |
159 | } | |
160 | if !self.scale_h || ((y & 1) == 1) { | |
161 | sidx += if !self.scale_v { w } else { w/2 }; | |
162 | } | |
163 | didx += stride; | |
164 | } | |
165 | } | |
166 | } | |
167 | ||
168 | fn decode_method2(&mut self, br: &mut ByteReader) -> DecoderResult<()> { | |
8e652498 | 169 | let mut bits = Bits8::new(); |
e120f8fd KS |
170 | |
171 | let mut size = self.info.get_properties().get_video_info().unwrap().get_width() * | |
172 | self.info.get_properties().get_video_info().unwrap().get_height(); | |
173 | let mut idx = PREAMBLE_SIZE; | |
8e652498 KS |
174 | if self.frame[8] != 0 { |
175 | for c in 0..256 { | |
176 | for i in 0..16 { self.frame[c * 16 + i] = c as u8; } | |
177 | } | |
e120f8fd KS |
178 | } |
179 | while size > 0 { | |
180 | let tag = bits.read_2bits(br)?; | |
181 | if tag == 0 { | |
182 | self.frame[idx] = br.read_byte()?; | |
183 | size -= 1; | |
184 | idx += 1; | |
185 | } else if tag == 1 { | |
186 | let b = br.read_byte()?; | |
187 | let len = ((b & 0xF) as usize) + 3; | |
8e652498 KS |
188 | let bot = (b >> 4) as isize; |
189 | let off = ((br.read_byte()? as isize) << 4) + bot - 4096; | |
e120f8fd KS |
190 | validate!(len <= size); |
191 | size -= len; | |
8e652498 | 192 | self.lz_copy(idx, off, len)?; |
e120f8fd KS |
193 | idx += len; |
194 | } else if tag == 2 { | |
195 | let len = (br.read_byte()? as usize) + 2; | |
196 | validate!(len <= size); | |
197 | size -= len; | |
198 | idx += len; | |
199 | } else { | |
200 | break; | |
201 | } | |
202 | } | |
203 | Ok(()) | |
204 | } | |
205 | ||
206 | fn decode_method5(&mut self, br: &mut ByteReader, skip: usize) -> DecoderResult<()> { | |
8e652498 | 207 | let mut bits = Bits8::new(); |
e120f8fd KS |
208 | |
209 | let mut size = self.info.get_properties().get_video_info().unwrap().get_width() * | |
210 | self.info.get_properties().get_video_info().unwrap().get_height(); | |
211 | let mut idx = PREAMBLE_SIZE; | |
212 | validate!(size >= skip); | |
213 | size -= skip; | |
214 | idx += skip; | |
215 | while size > 0 { | |
216 | let tag = bits.read_2bits(br)?; | |
217 | if tag == 0 { | |
218 | self.frame[idx] = br.read_byte()?; | |
219 | size -= 1; | |
220 | idx += 1; | |
221 | } else if tag == 1 { | |
222 | let b = br.read_byte()?; | |
223 | let len = ((b & 0xF) as usize) + 3; | |
8e652498 KS |
224 | let bot = (b >> 4) as isize; |
225 | let off = ((br.read_byte()? as isize) << 4) + bot - 4096; | |
e120f8fd KS |
226 | validate!(len <= size); |
227 | size -= len; | |
8e652498 | 228 | self.lz_copy(idx, off, len)?; |
e120f8fd KS |
229 | idx += len; |
230 | } else if tag == 2 { | |
231 | let b = br.read_byte()?; | |
232 | if b == 0 { break; } | |
8e652498 | 233 | let len: usize = (if b != 0xFF { b as usize } else { br.read_u16le()? as usize }) + 1; |
e120f8fd KS |
234 | validate!(len <= size); |
235 | size -= len; | |
236 | idx += len; | |
237 | } else { | |
238 | let b = br.read_byte()?; | |
239 | let len = ((b & 0x3) as usize) + 2; | |
240 | let off = -((b >> 2) as isize) - 1; | |
241 | validate!(len <= size); | |
242 | size -= len; | |
1a151e53 | 243 | self.lz_copy(idx, off, len)?; |
e120f8fd KS |
244 | idx += len; |
245 | } | |
246 | } | |
247 | Ok(()) | |
248 | } | |
249 | ||
250 | fn decode_method68(&mut self, br: &mut ByteReader, | |
251 | skip: usize, use8: bool) -> DecoderResult<()> { | |
252 | let mut bits = Bits32::new(); | |
253 | ||
254 | let mut size = self.info.get_properties().get_video_info().unwrap().get_width() * | |
255 | self.info.get_properties().get_video_info().unwrap().get_height(); | |
256 | let mut idx = PREAMBLE_SIZE; | |
257 | validate!(size >= skip); | |
258 | size -= skip; | |
259 | idx += skip; | |
260 | bits.fill(br)?; | |
261 | while size > 0 { | |
262 | let tag = bits.read_bits(br, 2)?; | |
263 | if tag == 0 { //draw | |
264 | let b = bits.read_bits(br, 1)?; | |
265 | if b == 0 { | |
266 | self.frame[idx] = br.read_byte()?; | |
267 | size -= 1; | |
268 | idx += 1; | |
269 | } else { | |
270 | let mut len: usize = 2; | |
271 | let mut lbits = 0; | |
272 | loop { | |
273 | lbits += 1; | |
274 | let val = bits.read_bits(br, lbits)?; | |
275 | len += val as usize; | |
276 | if val != ((1 << lbits) - 1) { break; } | |
277 | validate!(lbits < 16); | |
278 | } | |
279 | validate!(len <= size); | |
280 | for i in 0..len { self.frame[idx + i] = br.read_byte()?; } | |
281 | size -= len; | |
282 | idx += len; | |
283 | } | |
284 | } else if tag == 1 { //skip | |
285 | let b = bits.read_bits(br, 1)?; | |
286 | let len: usize; | |
287 | if b == 0 { | |
288 | len = (bits.read_bits(br, 4)? as usize) + 2; | |
289 | } else { | |
290 | let bb = br.read_byte()?; | |
291 | if (bb & 0x80) == 0 { | |
292 | len = (bb as usize) + 18; | |
293 | } else { | |
294 | let top = ((bb & 0x7F) as usize) << 8; | |
295 | len = top + (br.read_byte()? as usize) + 146; | |
296 | } | |
297 | } | |
298 | validate!(len <= size); | |
299 | size -= len; | |
300 | idx += len; | |
301 | } else if tag == 2 { | |
302 | let subtag = bits.read_bits(br, 2)? as usize; | |
303 | if subtag != 3 { | |
304 | let top = (bits.read_bits(br, 4)? as usize) << 8; | |
305 | let offs = top + (br.read_byte()? as usize); | |
306 | if (subtag != 0) || (offs <= 0xF80) { | |
307 | let len = (subtag as usize) + 3; | |
308 | self.lz_copy(idx, (offs as isize) - 4096, len)?; | |
309 | idx += len; | |
310 | } else { | |
311 | if offs == 0xFFF { return Ok(()); } | |
312 | let real_off = ((offs >> 4) & 0x7) + 1; | |
313 | let len = ((offs & 0xF) + 2) * 2; | |
314 | validate!(len <= size); | |
315 | size -= len; | |
316 | let c1 = self.frame[idx - real_off]; | |
317 | let c2 = self.frame[idx - real_off + 1]; | |
318 | for i in 0..len/2 { | |
319 | self.frame[idx + i*2 + 0] = c1; | |
320 | self.frame[idx + i*2 + 1] = c2; | |
321 | } | |
322 | idx += len; | |
323 | } | |
324 | } else { | |
325 | let b = br.read_byte()?; | |
326 | let off = ((b & 0x7F) as usize) + 1; | |
327 | let len = if (b & 0x80) == 0 { 2 } else { 3 }; | |
328 | validate!(len <= size); | |
329 | size -= len; | |
1a151e53 | 330 | self.lz_copy(idx, -(off as isize), len)?; |
e120f8fd KS |
331 | idx += len; |
332 | } | |
333 | } else { | |
334 | let len: usize; | |
335 | let off: isize; | |
336 | if use8 { | |
337 | let b = br.read_byte()?; | |
338 | if (b & 0xC0) == 0xC0 { | |
339 | len = ((b & 0x3F) as usize) + 8; | |
340 | let q = bits.read_bits(br, 4)? as isize; | |
341 | off = (q << 8) + (br.read_byte()? as isize) + 1; | |
342 | } else { | |
343 | let ofs1: isize; | |
344 | if (b & 0x80) == 0 { | |
345 | len = ((b >> 4) as usize) + 6; | |
346 | ofs1 = (b & 0xF) as isize; | |
347 | } else { | |
348 | len = ((b & 0x3F) as usize) + 14; | |
349 | ofs1 = bits.read_bits(br, 4)? as isize; | |
350 | } | |
351 | off = (ofs1 << 8) + (br.read_byte()? as isize) - 4096; | |
352 | } | |
353 | } else { | |
354 | let b = br.read_byte()?; | |
355 | if (b >> 4) == 0xF { | |
356 | len = (br.read_byte()? as usize) + 21; | |
357 | } else { | |
358 | len = ((b >> 4) as usize) + 6; | |
359 | } | |
360 | let ofs1 = (b & 0xF) as isize; | |
361 | off = (ofs1 << 8) + (br.read_byte()? as isize) - 4096; | |
362 | } | |
363 | validate!(len <= size); | |
364 | size -= len; | |
1a151e53 | 365 | self.lz_copy(idx, off, len)?; |
e120f8fd KS |
366 | idx += len; |
367 | } | |
368 | } | |
369 | Ok(()) | |
370 | } | |
371 | } | |
372 | ||
373 | impl NADecoder for GremlinVideoDecoder { | |
01613464 | 374 | fn init(&mut self, _supp: &mut NADecoderSupport, info: NACodecInfoRef) -> DecoderResult<()> { |
e120f8fd KS |
375 | if let NACodecTypeInfo::Video(vinfo) = info.get_properties() { |
376 | let w = vinfo.get_width(); | |
377 | let h = vinfo.get_height(); | |
378 | if !vinfo.get_format().is_paletted() { return Err(DecoderError::NotImplemented); } | |
cbc3c7a2 | 379 | let fmt = PAL8_FORMAT; |
e120f8fd | 380 | let myinfo = NACodecTypeInfo::Video(NAVideoInfo::new(w, h, false, fmt)); |
2422d969 | 381 | self.info = NACodecInfo::new_ref(info.get_name(), myinfo, info.get_extradata()).into_ref(); |
e120f8fd KS |
382 | |
383 | self.frame.resize(PREAMBLE_SIZE + w * h, 0); | |
384 | for i in 0..2 { | |
385 | for j in 0..256 { | |
386 | for k in 0..8 { | |
387 | self.frame[i * 2048 + j * 8 + k] = j as u8; | |
388 | } | |
389 | } | |
390 | } | |
391 | let edata = info.get_extradata().unwrap(); | |
392 | validate!(edata.len() == 768); | |
393 | for c in 0..256 { | |
394 | for i in 0..3 { | |
395 | let cc = edata[c * 3 + i]; | |
cbc3c7a2 | 396 | self.pal[c * 3 + i] = (cc << 2) | (cc >> 4); |
e120f8fd KS |
397 | } |
398 | } | |
399 | Ok(()) | |
400 | } else { | |
401 | Err(DecoderError::InvalidData) | |
402 | } | |
403 | } | |
01613464 | 404 | fn decode(&mut self, _supp: &mut NADecoderSupport, pkt: &NAPacket) -> DecoderResult<NAFrameRef> { |
e120f8fd KS |
405 | let src = pkt.get_buffer(); |
406 | let mut mr = MemoryReader::new_read(&src); | |
407 | let mut br = ByteReader::new(&mut mr); | |
408 | let flags = br.read_u32le()?; | |
409 | let w = self.info.get_properties().get_video_info().unwrap().get_width(); | |
410 | let h = self.info.get_properties().get_video_info().unwrap().get_height(); | |
411 | ||
412 | let cmethod = flags & 0xF; | |
413 | let is_intra = (flags & 0x40) != 0; | |
414 | let scale_v = (flags & 0x10) != 0; | |
415 | let scale_h = (flags & 0x20) != 0; | |
416 | ||
417 | self.rescale(w, h, scale_v, scale_h); | |
418 | ||
419 | if (cmethod == 0) || (cmethod == 1) { | |
420 | for c in 0..256 { | |
421 | for i in 0..3 { | |
422 | let b = br.read_byte()?; | |
cbc3c7a2 | 423 | self.pal[c * 3 + i] = (b << 2) | (b >> 4); |
e120f8fd KS |
424 | } |
425 | } | |
426 | if cmethod == 1 { | |
427 | for i in PREAMBLE_SIZE..self.frame.len() { self.frame[i] = 0x00; } | |
428 | } | |
429 | let mut frm = NAFrame::new_from_pkt(pkt, self.info.clone(), NABufferType::None); | |
430 | frm.set_keyframe(false); | |
431 | frm.set_frame_type(FrameType::Skip); | |
171860fc | 432 | return Ok(frm.into_ref()) |
e120f8fd KS |
433 | } else if cmethod == 3 { |
434 | let mut frm = NAFrame::new_from_pkt(pkt, self.info.clone(), NABufferType::None); | |
435 | frm.set_keyframe(false); | |
436 | frm.set_frame_type(FrameType::Skip); | |
171860fc | 437 | return Ok(frm.into_ref()) |
e120f8fd KS |
438 | } else if cmethod == 2 { |
439 | self.decode_method2(&mut br)?; | |
440 | } else if cmethod == 5 { | |
441 | self.decode_method5(&mut br, (flags >> 8) as usize)?; | |
442 | } else if cmethod == 6 { | |
443 | self.decode_method68(&mut br, (flags >> 8) as usize, false)?; | |
444 | } else if cmethod == 8 { | |
445 | self.decode_method68(&mut br, (flags >> 8) as usize, true)?; | |
446 | } else { | |
447 | return Err(DecoderError::NotImplemented); | |
448 | } | |
449 | ||
e69b1148 | 450 | let mut bufinfo = alloc_video_buffer(self.info.get_properties().get_video_info().unwrap(), 0)?; |
e120f8fd KS |
451 | |
452 | self.output_frame(&mut bufinfo, w, h); | |
453 | ||
454 | let mut frm = NAFrame::new_from_pkt(pkt, self.info.clone(), bufinfo); | |
455 | frm.set_keyframe(is_intra); | |
456 | frm.set_frame_type(if is_intra { FrameType::I } else { FrameType::P }); | |
171860fc | 457 | Ok(frm.into_ref()) |
e120f8fd | 458 | } |
f9be4e75 KS |
459 | fn flush(&mut self) { |
460 | } | |
e120f8fd KS |
461 | } |
462 | ||
08a1fab7 | 463 | pub fn get_decoder_video() -> Box<dyn NADecoder + Send> { |
e120f8fd KS |
464 | Box::new(GremlinVideoDecoder::new()) |
465 | } | |
466 | ||
b74ff9fa KS |
467 | struct GremlinAudioDecoder { |
468 | ainfo: NAAudioInfo, | |
469 | chmap: NAChannelMap, | |
470 | delta_tab: [i16; 256], | |
471 | state0: i16, | |
472 | state1: i16, | |
473 | } | |
474 | ||
475 | impl GremlinAudioDecoder { | |
476 | fn new() -> Self { | |
477 | let mut delta_tab: [i16; 256] = [0; 256]; | |
478 | let mut delta = 0; | |
479 | let mut code = 64; | |
480 | let mut step = 45; | |
481 | for i in 0..127 { | |
482 | delta += code >> 5; | |
483 | code += step; | |
484 | step += 2; | |
485 | delta_tab[i * 2 + 1] = delta; | |
486 | delta_tab[i * 2 + 2] = -delta; | |
487 | } | |
488 | delta_tab[255] = 32767;//delta + (code >> 5); | |
489 | GremlinAudioDecoder { | |
490 | ainfo: NAAudioInfo::new(0, 1, formats::SND_S16_FORMAT, 0), | |
491 | chmap: NAChannelMap::new(), | |
492 | delta_tab, | |
493 | state0: 0, | |
494 | state1: 0, | |
495 | } | |
496 | } | |
497 | } | |
498 | ||
499 | const CHMAP_MONO: [NAChannelType; 1] = [NAChannelType::C]; | |
500 | const CHMAP_STEREO: [NAChannelType; 2] = [NAChannelType::L, NAChannelType::R]; | |
501 | ||
502 | fn get_default_chmap(nch: u8) -> NAChannelMap { | |
503 | let mut chmap = NAChannelMap::new(); | |
504 | match nch { | |
505 | 1 => chmap.add_channels(&CHMAP_MONO), | |
506 | 2 => chmap.add_channels(&CHMAP_STEREO), | |
507 | _ => (), | |
508 | } | |
509 | chmap | |
510 | } | |
511 | ||
512 | impl NADecoder for GremlinAudioDecoder { | |
01613464 | 513 | fn init(&mut self, _supp: &mut NADecoderSupport, info: NACodecInfoRef) -> DecoderResult<()> { |
b74ff9fa KS |
514 | if let NACodecTypeInfo::Audio(ainfo) = info.get_properties() { |
515 | self.ainfo = NAAudioInfo::new(ainfo.get_sample_rate(), ainfo.get_channels(), formats::SND_S16P_FORMAT, ainfo.get_block_len()); | |
516 | self.chmap = get_default_chmap(ainfo.get_channels()); | |
517 | if self.chmap.num_channels() == 0 { return Err(DecoderError::InvalidData); } | |
518 | Ok(()) | |
519 | } else { | |
520 | Err(DecoderError::InvalidData) | |
521 | } | |
522 | } | |
01613464 | 523 | fn decode(&mut self, _supp: &mut NADecoderSupport, pkt: &NAPacket) -> DecoderResult<NAFrameRef> { |
b74ff9fa KS |
524 | let info = pkt.get_stream().get_info(); |
525 | if let NACodecTypeInfo::Audio(_) = info.get_properties() { | |
526 | let pktbuf = pkt.get_buffer(); | |
527 | let samples = pktbuf.len() / self.chmap.num_channels(); | |
b70cc006 | 528 | let abuf = alloc_audio_buffer(self.ainfo, samples, self.chmap.clone())?; |
b74ff9fa KS |
529 | let mut adata = abuf.get_abuf_i16().unwrap(); |
530 | let off1 = adata.get_offset(1); | |
1a967e6b | 531 | let buf = adata.get_data_mut().unwrap(); |
b74ff9fa KS |
532 | if self.chmap.num_channels() == 2 { |
533 | for (i, e) in pktbuf.chunks(2).enumerate() { | |
534 | self.state0 = self.state0.wrapping_add(self.delta_tab[e[0] as usize]); | |
535 | buf[i] = self.state0; | |
536 | self.state1 = self.state1.wrapping_add(self.delta_tab[e[1] as usize]); | |
537 | buf[off1 + i] = self.state1; | |
538 | } | |
539 | } else { | |
540 | for (i, e) in pktbuf.iter().enumerate() { | |
541 | self.state0 += self.delta_tab[*e as usize]; | |
542 | buf[i] = self.state0; | |
543 | } | |
544 | } | |
545 | let mut frm = NAFrame::new_from_pkt(pkt, info, abuf); | |
546 | frm.set_duration(Some(samples as u64)); | |
547 | frm.set_keyframe(false); | |
171860fc | 548 | Ok(frm.into_ref()) |
b74ff9fa KS |
549 | } else { |
550 | Err(DecoderError::InvalidData) | |
551 | } | |
552 | } | |
f9be4e75 KS |
553 | fn flush(&mut self) { |
554 | } | |
b74ff9fa KS |
555 | } |
556 | ||
08a1fab7 | 557 | pub fn get_decoder_audio() -> Box<dyn NADecoder + Send> { |
b74ff9fa KS |
558 | Box::new(GremlinAudioDecoder::new()) |
559 | } | |
560 | ||
e120f8fd KS |
561 | #[cfg(test)] |
562 | mod test { | |
3167c45c KS |
563 | use nihav_core::codecs::RegisteredDecoders; |
564 | use nihav_core::demuxers::RegisteredDemuxers; | |
8a790ba7 | 565 | use nihav_codec_support::test::dec_video::*; |
e64739f8 KS |
566 | use crate::game_register_all_codecs; |
567 | use crate::game_register_all_demuxers; | |
8a790ba7 KS |
568 | |
569 | #[test] | |
570 | fn test_gdv_video() { | |
571 | let mut dmx_reg = RegisteredDemuxers::new(); | |
572 | game_register_all_demuxers(&mut dmx_reg); | |
573 | let mut dec_reg = RegisteredDecoders::new(); | |
574 | game_register_all_codecs(&mut dec_reg); | |
575 | ||
576 | test_decoding("gdv", "gdv-video", "assets/Game/intro1.gdv", Some(10), &dmx_reg, &dec_reg, | |
577 | ExpectedTestResult::MD5([0x7ea302bf, 0xc3e210cf, 0x6e341376, 0x9e976056])); | |
578 | } | |
e120f8fd | 579 | #[test] |
8a790ba7 | 580 | fn test_gdv_audio() { |
3167c45c KS |
581 | let mut dmx_reg = RegisteredDemuxers::new(); |
582 | game_register_all_demuxers(&mut dmx_reg); | |
583 | let mut dec_reg = RegisteredDecoders::new(); | |
584 | game_register_all_codecs(&mut dec_reg); | |
585 | ||
8a790ba7 KS |
586 | test_decoding("gdv", "gdv-audio", "assets/Game/intro1.gdv", None, &dmx_reg, &dec_reg, |
587 | ExpectedTestResult::MD5([0xd41d8cd9, 0x8f00b204, 0xe9800998, 0xecf8427e])); | |
e120f8fd KS |
588 | } |
589 | } |