]> git.nihav.org Git - nihav.git/blob - src/codecs/gremlinvideo.rs
h263: update num_mv2 in set_mv2()
[nihav.git] / src / codecs / gremlinvideo.rs
1 use formats;
2 use super::*;
3 use io::byteio::*;
4
5 struct GremlinVideoDecoder {
6 info: Rc<NACodecInfo>,
7 pal: [u8; 768],
8 frame: Vec<u8>,
9 scale_v: bool,
10 scale_h: bool,
11 }
12
13 struct Bits8 {
14 queue: u8,
15 fill: u8,
16 }
17
18 struct Bits32 {
19 queue: u32,
20 fill: u8,
21 }
22
23 const PREAMBLE_SIZE: usize = 4096;
24
25 impl Bits8 {
26 fn new() -> Self { Bits8 { queue: 0, fill: 0 } }
27 fn read_2bits(&mut self, br: &mut ByteReader) -> ByteIOResult<u8> {
28 if self.fill == 0 {
29 self.queue = br.read_byte()?;
30 self.fill += 8;
31 }
32 let res = self.queue >> 6;
33 self.queue <<= 2;
34 self.fill -= 2;
35 Ok(res)
36 }
37 }
38
39 impl Bits32 {
40 fn new() -> Self { Bits32 { queue: 0, fill: 0 } }
41 fn fill(&mut self, br: &mut ByteReader) -> ByteIOResult<()> {
42 self.queue = br.read_u32le()?;
43 self.fill = 32;
44 Ok(())
45 }
46 fn read_bits(&mut self, br: &mut ByteReader, nbits: u8) -> ByteIOResult<u32> {
47 let res = self.queue & ((1 << nbits) - 1);
48 self.queue >>= nbits;
49 self.fill -= nbits;
50 if self.fill <= 16 {
51 self.queue |= (br.read_u16le()? as u32) << self.fill;
52 self.fill += 16;
53 }
54 Ok(res)
55 }
56 }
57
58 impl GremlinVideoDecoder {
59 fn new() -> Self {
60 let dummy_info = Rc::new(DUMMY_CODEC_INFO);
61 GremlinVideoDecoder {
62 info: dummy_info, pal: [0; 768], frame: Vec::new(),
63 scale_v: false, scale_h: false
64 }
65 }
66
67 fn lz_copy(&mut self, idx: usize, offset: isize, len: usize) -> DecoderResult<()> {
68 if idx + len > self.frame.len() { return Err(DecoderError::InvalidData); }
69 if offset == -1 {
70 let c = self.frame[idx - 1];
71 for i in 0..len { self.frame[idx + i] = c; }
72 } else if offset < 0 {
73 let start = idx - (-offset as usize);
74 for i in 0..len { self.frame[idx + i] = self.frame[start + i]; }
75 } else {
76 if idx + (offset as usize) + len > self.frame.len() { return Err(DecoderError::InvalidData); }
77 let start = idx + (offset as usize);
78 for i in 0..len { self.frame[idx + i] = self.frame[start + i]; }
79 }
80 Ok(())
81 }
82
83 fn rescale(&mut self, w: usize, h: usize, scale_v: bool, scale_h: bool) {
84 if (self.scale_v == scale_v) && (self.scale_h == scale_h) { return; }
85
86 if self.scale_h && self.scale_v {
87 for j in 0..h {
88 let y = h - j - 1;
89 for i in 0..w {
90 let x = w - i - 1;
91 self.frame[PREAMBLE_SIZE + x + y * w] = self.frame[PREAMBLE_SIZE + x/2 + (y/2) * (w/2)];
92 }
93 }
94 } else if self.scale_h {
95 for j in 0..h {
96 let y = h - j - 1;
97 for x in 0..w {
98 self.frame[PREAMBLE_SIZE + x + y * w] = self.frame[PREAMBLE_SIZE + x + (y/2) * w];
99 }
100 }
101 } else if self.scale_v {
102 for j in 0..h {
103 let y = h - j - 1;
104 for i in 0..w {
105 let x = w - i - 1;
106 self.frame[PREAMBLE_SIZE + x + y * w] = self.frame[PREAMBLE_SIZE + x/2 + y * (w/2)];
107 }
108 }
109 }
110
111 if scale_h && scale_v {
112 for y in 0..h/2 {
113 for x in 0..w/2 {
114 self.frame[PREAMBLE_SIZE + x + y * (w/2)] = self.frame[PREAMBLE_SIZE + x*2 + y*2 * w];
115 }
116 }
117 } else if scale_h {
118 for y in 0..h/2 {
119 for x in 0..w {
120 self.frame[PREAMBLE_SIZE + x + y * w] = self.frame[PREAMBLE_SIZE + x + y*2 * w];
121 }
122 }
123 } else if scale_v {
124 for y in 0..h {
125 for x in 0..w/2 {
126 self.frame[PREAMBLE_SIZE + x + y * w] = self.frame[PREAMBLE_SIZE + x*2 + y * w];
127 }
128 }
129 }
130
131 self.scale_v = scale_v;
132 self.scale_h = scale_h;
133 }
134
135 fn output_frame(&mut self, bufinfo: &mut NABufferType, w: usize, h: usize) {
136 let bufo = bufinfo.get_vbuf();
137 let mut buf = bufo.unwrap();
138 let paloff = buf.get_offset(1);
139 let stride = buf.get_stride(0);
140 let mut data = buf.get_data_mut();
141 let dst = data.as_mut_slice();
142 let mut sidx = PREAMBLE_SIZE;
143 let mut didx = 0;
144
145 for i in 0..768 { dst[paloff + i] = self.pal[i]; }
146 if !self.scale_v && !self.scale_h {
147 for _ in 0..h {
148 for x in 0..w { dst[didx + x] = self.frame[sidx + x]; }
149 sidx += w;
150 didx += stride;
151 }
152 } else {
153 for y in 0..h {
154 if !self.scale_v {
155 for x in 0..w { dst[didx + x] = self.frame[sidx + x]; }
156 } else {
157 for x in 0..w { dst[didx + x] = self.frame[sidx + x/2]; }
158 }
159 if !self.scale_h || ((y & 1) == 1) {
160 sidx += if !self.scale_v { w } else { w/2 };
161 }
162 didx += stride;
163 }
164 }
165 }
166
167 fn decode_method2(&mut self, br: &mut ByteReader) -> DecoderResult<()> {
168 let mut bits = Bits8::new();
169
170 let mut size = self.info.get_properties().get_video_info().unwrap().get_width() *
171 self.info.get_properties().get_video_info().unwrap().get_height();
172 let mut idx = PREAMBLE_SIZE;
173 if self.frame[8] != 0 {
174 for c in 0..256 {
175 for i in 0..16 { self.frame[c * 16 + i] = c as u8; }
176 }
177 }
178 while size > 0 {
179 let tag = bits.read_2bits(br)?;
180 if tag == 0 {
181 self.frame[idx] = br.read_byte()?;
182 size -= 1;
183 idx += 1;
184 } else if tag == 1 {
185 let b = br.read_byte()?;
186 let len = ((b & 0xF) as usize) + 3;
187 let bot = (b >> 4) as isize;
188 let off = ((br.read_byte()? as isize) << 4) + bot - 4096;
189 validate!(len <= size);
190 size -= len;
191 self.lz_copy(idx, off, len)?;
192 idx += len;
193 } else if tag == 2 {
194 let len = (br.read_byte()? as usize) + 2;
195 validate!(len <= size);
196 size -= len;
197 idx += len;
198 } else {
199 break;
200 }
201 }
202 Ok(())
203 }
204
205 fn decode_method5(&mut self, br: &mut ByteReader, skip: usize) -> DecoderResult<()> {
206 let mut bits = Bits8::new();
207
208 let mut size = self.info.get_properties().get_video_info().unwrap().get_width() *
209 self.info.get_properties().get_video_info().unwrap().get_height();
210 let mut idx = PREAMBLE_SIZE;
211 validate!(size >= skip);
212 size -= skip;
213 idx += skip;
214 while size > 0 {
215 let tag = bits.read_2bits(br)?;
216 if tag == 0 {
217 self.frame[idx] = br.read_byte()?;
218 size -= 1;
219 idx += 1;
220 } else if tag == 1 {
221 let b = br.read_byte()?;
222 let len = ((b & 0xF) as usize) + 3;
223 let bot = (b >> 4) as isize;
224 let off = ((br.read_byte()? as isize) << 4) + bot - 4096;
225 validate!(len <= size);
226 size -= len;
227 self.lz_copy(idx, off, len)?;
228 idx += len;
229 } else if tag == 2 {
230 let b = br.read_byte()?;
231 if b == 0 { break; }
232 let len: usize = (if b != 0xFF { b as usize } else { br.read_u16le()? as usize }) + 1;
233 validate!(len <= size);
234 size -= len;
235 idx += len;
236 } else {
237 let b = br.read_byte()?;
238 let len = ((b & 0x3) as usize) + 2;
239 let off = -((b >> 2) as isize) - 1;
240 validate!(len <= size);
241 size -= len;
242 self.lz_copy(idx, off, len)?;
243 idx += len;
244 }
245 }
246 Ok(())
247 }
248
249 fn decode_method68(&mut self, br: &mut ByteReader,
250 skip: usize, use8: bool) -> DecoderResult<()> {
251 let mut bits = Bits32::new();
252
253 let mut size = self.info.get_properties().get_video_info().unwrap().get_width() *
254 self.info.get_properties().get_video_info().unwrap().get_height();
255 let mut idx = PREAMBLE_SIZE;
256 validate!(size >= skip);
257 size -= skip;
258 idx += skip;
259 bits.fill(br)?;
260 while size > 0 {
261 let tag = bits.read_bits(br, 2)?;
262 if tag == 0 { //draw
263 let b = bits.read_bits(br, 1)?;
264 if b == 0 {
265 self.frame[idx] = br.read_byte()?;
266 size -= 1;
267 idx += 1;
268 } else {
269 let mut len: usize = 2;
270 let mut lbits = 0;
271 loop {
272 lbits += 1;
273 let val = bits.read_bits(br, lbits)?;
274 len += val as usize;
275 if val != ((1 << lbits) - 1) { break; }
276 validate!(lbits < 16);
277 }
278 validate!(len <= size);
279 for i in 0..len { self.frame[idx + i] = br.read_byte()?; }
280 size -= len;
281 idx += len;
282 }
283 } else if tag == 1 { //skip
284 let b = bits.read_bits(br, 1)?;
285 let len: usize;
286 if b == 0 {
287 len = (bits.read_bits(br, 4)? as usize) + 2;
288 } else {
289 let bb = br.read_byte()?;
290 if (bb & 0x80) == 0 {
291 len = (bb as usize) + 18;
292 } else {
293 let top = ((bb & 0x7F) as usize) << 8;
294 len = top + (br.read_byte()? as usize) + 146;
295 }
296 }
297 validate!(len <= size);
298 size -= len;
299 idx += len;
300 } else if tag == 2 {
301 let subtag = bits.read_bits(br, 2)? as usize;
302 if subtag != 3 {
303 let top = (bits.read_bits(br, 4)? as usize) << 8;
304 let offs = top + (br.read_byte()? as usize);
305 if (subtag != 0) || (offs <= 0xF80) {
306 let len = (subtag as usize) + 3;
307 self.lz_copy(idx, (offs as isize) - 4096, len)?;
308 idx += len;
309 } else {
310 if offs == 0xFFF { return Ok(()); }
311 let real_off = ((offs >> 4) & 0x7) + 1;
312 let len = ((offs & 0xF) + 2) * 2;
313 validate!(len <= size);
314 size -= len;
315 let c1 = self.frame[idx - real_off];
316 let c2 = self.frame[idx - real_off + 1];
317 for i in 0..len/2 {
318 self.frame[idx + i*2 + 0] = c1;
319 self.frame[idx + i*2 + 1] = c2;
320 }
321 idx += len;
322 }
323 } else {
324 let b = br.read_byte()?;
325 let off = ((b & 0x7F) as usize) + 1;
326 let len = if (b & 0x80) == 0 { 2 } else { 3 };
327 validate!(len <= size);
328 size -= len;
329 self.lz_copy(idx, -(off as isize), len)?;
330 idx += len;
331 }
332 } else {
333 let len: usize;
334 let off: isize;
335 if use8 {
336 let b = br.read_byte()?;
337 if (b & 0xC0) == 0xC0 {
338 len = ((b & 0x3F) as usize) + 8;
339 let q = bits.read_bits(br, 4)? as isize;
340 off = (q << 8) + (br.read_byte()? as isize) + 1;
341 } else {
342 let ofs1: isize;
343 if (b & 0x80) == 0 {
344 len = ((b >> 4) as usize) + 6;
345 ofs1 = (b & 0xF) as isize;
346 } else {
347 len = ((b & 0x3F) as usize) + 14;
348 ofs1 = bits.read_bits(br, 4)? as isize;
349 }
350 off = (ofs1 << 8) + (br.read_byte()? as isize) - 4096;
351 }
352 } else {
353 let b = br.read_byte()?;
354 if (b >> 4) == 0xF {
355 len = (br.read_byte()? as usize) + 21;
356 } else {
357 len = ((b >> 4) as usize) + 6;
358 }
359 let ofs1 = (b & 0xF) as isize;
360 off = (ofs1 << 8) + (br.read_byte()? as isize) - 4096;
361 }
362 validate!(len <= size);
363 size -= len;
364 self.lz_copy(idx, off, len)?;
365 idx += len;
366 }
367 }
368 Ok(())
369 }
370 }
371
372 impl NADecoder for GremlinVideoDecoder {
373 fn init(&mut self, info: Rc<NACodecInfo>) -> DecoderResult<()> {
374 if let NACodecTypeInfo::Video(vinfo) = info.get_properties() {
375 let w = vinfo.get_width();
376 let h = vinfo.get_height();
377 if !vinfo.get_format().is_paletted() { return Err(DecoderError::NotImplemented); }
378 let fmt = formats::PAL8_FORMAT;
379 let myinfo = NACodecTypeInfo::Video(NAVideoInfo::new(w, h, false, fmt));
380 self.info = Rc::new(NACodecInfo::new_ref(info.get_name(), myinfo, info.get_extradata()));
381
382 self.frame.resize(PREAMBLE_SIZE + w * h, 0);
383 for i in 0..2 {
384 for j in 0..256 {
385 for k in 0..8 {
386 self.frame[i * 2048 + j * 8 + k] = j as u8;
387 }
388 }
389 }
390 let edata = info.get_extradata().unwrap();
391 validate!(edata.len() == 768);
392 for c in 0..256 {
393 for i in 0..3 {
394 let cc = edata[c * 3 + i];
395 self.pal[c * 3 + (2 - i)] = (cc << 2) | (cc >> 4);
396 }
397 }
398 Ok(())
399 } else {
400 Err(DecoderError::InvalidData)
401 }
402 }
403 fn decode(&mut self, pkt: &NAPacket) -> DecoderResult<NAFrameRef> {
404 let src = pkt.get_buffer();
405 let mut mr = MemoryReader::new_read(&src);
406 let mut br = ByteReader::new(&mut mr);
407 let flags = br.read_u32le()?;
408 let w = self.info.get_properties().get_video_info().unwrap().get_width();
409 let h = self.info.get_properties().get_video_info().unwrap().get_height();
410
411 let cmethod = flags & 0xF;
412 let is_intra = (flags & 0x40) != 0;
413 let scale_v = (flags & 0x10) != 0;
414 let scale_h = (flags & 0x20) != 0;
415
416 self.rescale(w, h, scale_v, scale_h);
417
418 if (cmethod == 0) || (cmethod == 1) {
419 for c in 0..256 {
420 for i in 0..3 {
421 let b = br.read_byte()?;
422 self.pal[c * 3 + (2 - i)] = (b << 2) | (b >> 4);
423 }
424 }
425 if cmethod == 1 {
426 for i in PREAMBLE_SIZE..self.frame.len() { self.frame[i] = 0x00; }
427 }
428 let mut frm = NAFrame::new_from_pkt(pkt, self.info.clone(), NABufferType::None);
429 frm.set_keyframe(false);
430 frm.set_frame_type(FrameType::Skip);
431 return Ok(Rc::new(RefCell::new(frm)))
432 } else if cmethod == 3 {
433 let mut frm = NAFrame::new_from_pkt(pkt, self.info.clone(), NABufferType::None);
434 frm.set_keyframe(false);
435 frm.set_frame_type(FrameType::Skip);
436 return Ok(Rc::new(RefCell::new(frm)))
437 } else if cmethod == 2 {
438 self.decode_method2(&mut br)?;
439 } else if cmethod == 5 {
440 self.decode_method5(&mut br, (flags >> 8) as usize)?;
441 } else if cmethod == 6 {
442 self.decode_method68(&mut br, (flags >> 8) as usize, false)?;
443 } else if cmethod == 8 {
444 self.decode_method68(&mut br, (flags >> 8) as usize, true)?;
445 } else {
446 return Err(DecoderError::NotImplemented);
447 }
448
449 let bufret = alloc_video_buffer(self.info.get_properties().get_video_info().unwrap(), 0);
450 if let Err(_) = bufret { return Err(DecoderError::InvalidData); }
451 let mut bufinfo = bufret.unwrap();
452
453 self.output_frame(&mut bufinfo, w, h);
454
455 let mut frm = NAFrame::new_from_pkt(pkt, self.info.clone(), bufinfo);
456 frm.set_keyframe(is_intra);
457 frm.set_frame_type(if is_intra { FrameType::I } else { FrameType::P });
458 Ok(Rc::new(RefCell::new(frm)))
459 }
460 }
461
462 pub fn get_decoder() -> Box<NADecoder> {
463 Box::new(GremlinVideoDecoder::new())
464 }
465
466 #[cfg(test)]
467 mod test {
468 use test::dec_video::test_file_decoding;
469 #[test]
470 fn test_gdv() {
471 test_file_decoding("gdv", "assets/intro1.gdv", Some(10), true, false, None);
472 }
473 }