Commit | Line | Data |
---|---|---|
be91b1c3 KS |
1 | use io::bitreader::*; |
2 | use io::codebook::*; | |
3 | use formats; | |
4 | use super::super::*; | |
5 | use super::*; | |
6 | use super::decoder::*; | |
7 | use super::data::*; | |
8 | ||
9 | #[allow(dead_code)] | |
10 | struct Tables { | |
11 | intra_mcbpc_cb: Codebook<u8>, | |
12 | inter_mcbpc_cb: Codebook<u8>, | |
13 | cbpy_cb: Codebook<u8>, | |
14 | rl_cb: Codebook<H263RLSym>, | |
15 | aic_rl_cb: Codebook<H263RLSym>, | |
16 | mv_cb: Codebook<u8>, | |
17 | luma_dc_cb: Codebook<u8>, | |
18 | chroma_dc_cb: Codebook<u8>, | |
19 | } | |
20 | ||
21 | struct RealVideo10Decoder { | |
22 | info: Rc<NACodecInfo>, | |
23 | dec: H263BaseDecoder, | |
24 | tables: Tables, | |
25 | w: usize, | |
26 | h: usize, | |
27 | new_ver: bool, | |
28 | } | |
29 | ||
30 | struct RealVideo10BR<'a> { | |
31 | br: BitReader<'a>, | |
32 | tables: &'a Tables, | |
33 | num_slices: usize, | |
34 | slice_no: usize, | |
35 | slice_off: Vec<u32>, | |
36 | w: usize, | |
37 | h: usize, | |
38 | mb_w: usize, | |
39 | mb_h: usize, | |
40 | mb_x: usize, | |
41 | mb_y: usize, | |
42 | mb_count: usize, | |
43 | mb_end: usize, | |
44 | new_ver: bool, | |
45 | dc_coded: [bool; 3], | |
46 | last_dc: [i16; 3], | |
47 | is_intra: bool, | |
48 | } | |
49 | ||
50 | struct RV10SliceInfo { | |
51 | is_p: bool, | |
52 | qscale: u8, | |
53 | mb_x: usize, | |
54 | mb_y: usize, | |
55 | mb_c: usize, | |
56 | } | |
57 | ||
58 | impl RV10SliceInfo { | |
59 | fn new(is_p: bool, qscale: u8, mb_x: usize, mb_y: usize, mb_c: usize) -> Self { | |
60 | RV10SliceInfo { is_p: is_p, qscale: qscale, mb_x: mb_x, mb_y: mb_y, mb_c: mb_c } | |
61 | } | |
62 | } | |
63 | ||
64 | impl<'a> RealVideo10BR<'a> { | |
65 | fn new(src: &'a [u8], tables: &'a Tables, width: usize, height: usize, new_ver: bool) -> Self { | |
66 | let nslices = (src[0] as usize) + 1; | |
67 | let mut slice_offs = Vec::with_capacity(nslices); | |
68 | { | |
69 | let offs = &src[1..][..nslices * 8]; | |
70 | let mut br = BitReader::new(offs, offs.len(), BitReaderMode::BE); | |
71 | for _ in 0..nslices { | |
72 | br.skip(32).unwrap(); | |
73 | let off = br.read(32).unwrap(); | |
74 | slice_offs.push(off); | |
75 | } | |
76 | } | |
77 | let soff = nslices * 8 + 1; | |
78 | RealVideo10BR { | |
79 | br: BitReader::new(&src[soff..], src.len() - soff, BitReaderMode::BE), | |
80 | tables: tables, | |
81 | num_slices: nslices, | |
82 | slice_no: 0, | |
83 | slice_off: slice_offs, | |
84 | w: width, | |
85 | h: height, | |
86 | mb_w: (width + 15) >> 4, | |
87 | mb_h: (height + 15) >> 4, | |
88 | mb_x: 0, | |
89 | mb_y: 0, | |
90 | mb_count: 0, | |
91 | mb_end: 0, | |
92 | new_ver: new_ver, | |
93 | dc_coded: [false; 3], | |
94 | last_dc: [0; 3], | |
95 | is_intra: false, | |
96 | } | |
97 | } | |
98 | ||
99 | fn decode_block(&mut self, quant: u8, intra: bool, coded: bool, blk: &mut [i16; 64], plane_no: usize) -> DecoderResult<()> { | |
100 | let mut br = &mut self.br; | |
101 | let mut idx = 0; | |
102 | if intra { | |
103 | let mut dc; | |
104 | //println!("dc prev {} @ {} of {},{} / {}x{}", self.last_dc[plane_no], br.tell() - (self.slice_off[self.slice_no - 1] as usize) * 8, self.mb_x, self.mb_y, self.mb_w, self.mb_h); | |
105 | if !self.new_ver || !self.is_intra { | |
106 | dc = br.read(8)? as i16; | |
107 | if dc == 255 { dc = 128; } | |
108 | } else { | |
109 | if self.dc_coded[plane_no] { | |
110 | let diff; | |
111 | let bits = br.peek(14); | |
112 | let ret = if plane_no == 0 { | |
113 | br.read_cb(&self.tables.luma_dc_cb) | |
114 | } else { | |
115 | br.read_cb(&self.tables.chroma_dc_cb) | |
116 | }; | |
117 | if ret.is_err() { | |
118 | println!("Illegal {} code {:X}", if plane_no==0{"luma"}else{"chroma"},bits); | |
119 | } | |
120 | let val = ret.unwrap() as i16; | |
121 | ||
122 | if val != 0 { | |
123 | diff = val - 128; | |
124 | } else { | |
125 | println!("escape!!!"); | |
126 | let code = br.read(2)?; | |
127 | match code { | |
128 | 0x0 => { diff = ((br.read(7)? + 1) as i8) as i16; }, | |
129 | 0x1 => { diff = (br.read(7)? as i16) - 128; }, | |
130 | 0x2 => { | |
131 | if plane_no == 0 { | |
132 | if br.read_bool()? { | |
133 | diff = ((br.read(8)? + 1) as i8) as i16; | |
134 | } else { | |
135 | diff = (br.read(8)? as i8) as i16; | |
136 | } | |
137 | } else { | |
138 | br.skip(9)?; | |
139 | diff = 1; | |
140 | } | |
141 | }, | |
142 | _ => { | |
143 | if plane_no == 0 { | |
144 | br.skip(4)?; | |
145 | diff = 1; | |
146 | } else { | |
147 | println!("!!! wrong chroma esc"); | |
148 | return Err(DecoderError::InvalidData); | |
149 | } | |
150 | }, | |
151 | }; | |
152 | println!("!!! diff = {} @ {}", diff, br.tell()); | |
153 | } | |
154 | dc = (self.last_dc[plane_no] - diff) & 0xFF; | |
155 | self.last_dc[plane_no] = dc; | |
156 | } else { | |
157 | self.dc_coded[plane_no] = true; | |
158 | dc = self.last_dc[plane_no]; | |
159 | } | |
160 | } | |
161 | //println!("dc new {} @ {}", self.last_dc[plane_no], br.tell()); | |
162 | blk[0] = dc << 3; | |
163 | idx = 1; | |
164 | } | |
165 | if !coded { return Ok(()); } | |
166 | ||
167 | let rl_cb = &self.tables.rl_cb; // could be aic too | |
168 | let q_add = if quant == 0 { 0i16 } else { ((quant - 1) | 1) as i16 }; | |
169 | let q = (quant * 2) as i16; | |
170 | while idx < 64 { | |
171 | let code = br.read_cb(rl_cb)?; | |
172 | let run; | |
173 | let mut level; | |
174 | let last; | |
175 | if !code.is_escape() { | |
176 | run = code.get_run(); | |
177 | level = code.get_level(); | |
178 | last = code.is_last(); | |
179 | if br.read_bool()? { level = -level; } | |
180 | level = (level * q) + q_add; | |
181 | } else { | |
182 | last = br.read_bool()?; | |
183 | run = br.read(6)? as u8; | |
184 | level = br.read_s(8)? as i16; | |
185 | if level == -128 { | |
186 | let low = br.read(5)? as i16; | |
187 | let top = br.read_s(6)? as i16; | |
188 | level = (top << 5) | low; | |
189 | } | |
190 | level = (level * q) + q_add; | |
191 | if level < -2048 { level = -2048; } | |
192 | if level > 2047 { level = 2047; } | |
193 | } | |
194 | idx += run; | |
195 | validate!(idx < 64); | |
196 | let oidx = H263_ZIGZAG[idx as usize]; | |
197 | blk[oidx] = level; | |
198 | idx += 1; | |
199 | if last { break; } | |
200 | } | |
201 | Ok(()) | |
202 | } | |
203 | } | |
204 | ||
205 | fn decode_mv_component(br: &mut BitReader, mv_cb: &Codebook<u8>) -> DecoderResult<i16> { | |
206 | let code = br.read_cb(mv_cb)? as i16; | |
207 | if code == 0 { return Ok(0) } | |
208 | if !br.read_bool()? { | |
209 | Ok(code) | |
210 | } else { | |
211 | Ok(-code) | |
212 | } | |
213 | } | |
214 | ||
215 | fn decode_mv(br: &mut BitReader, mv_cb: &Codebook<u8>) -> DecoderResult<MV> { | |
216 | let xval = decode_mv_component(br, mv_cb)?; | |
217 | let yval = decode_mv_component(br, mv_cb)?; | |
218 | //println!(" MV {},{} @ {}", xval, yval, br.tell()); | |
219 | Ok(MV::new(xval, yval)) | |
220 | } | |
221 | ||
222 | impl<'a> BlockDecoder for RealVideo10BR<'a> { | |
223 | ||
224 | #[allow(unused_variables)] | |
225 | fn decode_pichdr(&mut self) -> DecoderResult<PicInfo> { | |
226 | self.slice_no = 0; | |
227 | println!("decoding picture header size {}", if self.num_slices > 1 { self.slice_off[1] } else { ((self.br.tell() as u32) + (self.br.left() as u32))/8 }); | |
228 | self.mb_x = 0; | |
229 | self.mb_y = 0; | |
230 | self.mb_count = self.mb_w * self.mb_h; | |
231 | let shdr = self.read_slice_header()?; | |
232 | self.slice_no += 1; | |
233 | validate!((shdr.mb_x == 0) && (shdr.mb_y == 0)); | |
234 | ||
235 | self.mb_x = shdr.mb_x; | |
236 | self.mb_y = shdr.mb_y; | |
237 | self.mb_count = shdr.mb_c; | |
238 | self.mb_end = self.mb_x + self.mb_y * self.mb_w + self.mb_count; | |
239 | self.is_intra = !shdr.is_p; | |
240 | ||
241 | let ftype = if !shdr.is_p { Type::I } else { Type::P }; | |
242 | let picinfo = PicInfo::new(self.w, self.h, ftype, shdr.qscale, false, MVMode::Old, 0, None, true); | |
243 | Ok(picinfo) | |
244 | } | |
245 | ||
246 | #[allow(unused_variables)] | |
247 | fn decode_slice_header(&mut self, info: &PicInfo) -> DecoderResult<Slice> { | |
248 | //println!("read slice {} header", self.slice_no); | |
249 | let shdr = self.read_slice_header()?; | |
250 | let ret = Slice::new(shdr.mb_x, shdr.mb_y, shdr.qscale); | |
251 | self.slice_no += 1; | |
252 | self.mb_x = shdr.mb_x; | |
253 | self.mb_y = shdr.mb_y; | |
254 | self.mb_count = shdr.mb_c; | |
255 | self.mb_end = self.mb_x + self.mb_y * self.mb_w + self.mb_count; | |
256 | ||
257 | Ok(ret) | |
258 | } | |
259 | ||
260 | fn decode_block_header(&mut self, info: &PicInfo, slice: &Slice) -> DecoderResult<BlockInfo> { | |
261 | let mut br = &mut self.br; | |
262 | let mut q = slice.get_quant(); | |
263 | match info.get_mode() { | |
264 | Type::I => { | |
265 | let mut cbpc = br.read_cb(&self.tables.intra_mcbpc_cb)?; | |
266 | while cbpc == 8 { cbpc = br.read_cb(&self.tables.intra_mcbpc_cb)?; } | |
267 | let cbpy = br.read_cb(&self.tables.cbpy_cb)?; | |
268 | let cbp = (cbpy << 2) | (cbpc & 3); | |
269 | let dquant = (cbpc & 4) != 0; | |
270 | if dquant { | |
271 | let idx = br.read(2)? as usize; | |
272 | q = ((q as i16) + (H263_DQUANT_TAB[idx] as i16)) as u8; | |
273 | } | |
274 | self.mb_x += 1; | |
275 | if self.mb_x == self.mb_w { | |
276 | self.mb_x = 0; | |
277 | self.mb_y += 1; | |
278 | } | |
279 | Ok(BlockInfo::new(Type::I, cbp, q)) | |
280 | }, | |
281 | Type::P => { | |
282 | if br.read_bool()? { | |
283 | self.mb_x += 1; | |
284 | if self.mb_x == self.mb_w { | |
285 | self.mb_x = 0; | |
286 | self.mb_y += 1; | |
287 | } | |
288 | return Ok(BlockInfo::new(Type::Skip, 0, info.get_quant())); | |
289 | } | |
290 | let mut cbpc = br.read_cb(&self.tables.inter_mcbpc_cb)?; | |
291 | while cbpc == 20 { cbpc = br.read_cb(&self.tables.inter_mcbpc_cb)?; } | |
292 | let is_intra = (cbpc & 0x04) != 0; | |
293 | let dquant = (cbpc & 0x08) != 0; | |
294 | let is_4x4 = (cbpc & 0x10) != 0; | |
295 | if is_intra { | |
296 | let cbpy = br.read_cb(&self.tables.cbpy_cb)?; | |
297 | let cbp = (cbpy << 2) | (cbpc & 3); | |
298 | if dquant { | |
299 | let idx = br.read(2)? as usize; | |
300 | q = ((q as i16) + (H263_DQUANT_TAB[idx] as i16)) as u8; | |
301 | } | |
302 | let binfo = BlockInfo::new(Type::I, cbp, q); | |
303 | self.mb_x += 1; | |
304 | if self.mb_x == self.mb_w { | |
305 | self.mb_x = 0; | |
306 | self.mb_y += 1; | |
307 | } | |
308 | return Ok(binfo); | |
309 | } | |
310 | ||
311 | let mut cbpy = br.read_cb(&self.tables.cbpy_cb)?; | |
312 | // if /* !aiv && */(cbpc & 3) != 3 { | |
313 | cbpy ^= 0xF; | |
314 | // } | |
315 | let cbp = (cbpy << 2) | (cbpc & 3); | |
316 | if dquant { | |
317 | let idx = br.read(2)? as usize; | |
318 | q = ((q as i16) + (H263_DQUANT_TAB[idx] as i16)) as u8; | |
319 | } | |
320 | println!(" MB {}.{} cbp = {:X}", self.mb_x, self.mb_y, cbp); | |
321 | let mut binfo = BlockInfo::new(Type::P, cbp, q); | |
322 | if !is_4x4 { | |
323 | let mvec: [MV; 1] = [decode_mv(br, &self.tables.mv_cb)?]; | |
324 | binfo.set_mv(&mvec); | |
325 | } else { | |
326 | let mvec: [MV; 4] = [ | |
327 | decode_mv(br, &self.tables.mv_cb)?, | |
328 | decode_mv(br, &self.tables.mv_cb)?, | |
329 | decode_mv(br, &self.tables.mv_cb)?, | |
330 | decode_mv(br, &self.tables.mv_cb)? | |
331 | ]; | |
332 | binfo.set_mv(&mvec); | |
333 | } | |
334 | self.mb_x += 1; | |
335 | if self.mb_x == self.mb_w { | |
336 | self.mb_x = 0; | |
337 | self.mb_y += 1; | |
338 | } | |
339 | Ok(binfo) | |
340 | }, | |
341 | _ => { println!("wrong info mode"); Err(DecoderError::InvalidData) }, | |
342 | } | |
343 | } | |
344 | ||
345 | #[allow(unused_variables)] | |
346 | fn decode_block_intra(&mut self, info: &BlockInfo, quant: u8, no: usize, coded: bool, blk: &mut [i16; 64]) -> DecoderResult<()> { | |
347 | self.decode_block(quant, true, coded, blk, if no < 4 { 0 } else { no - 3 }) | |
348 | } | |
349 | ||
350 | #[allow(unused_variables)] | |
351 | fn decode_block_inter(&mut self, info: &BlockInfo, quant: u8, no: usize, coded: bool, blk: &mut [i16; 64]) -> DecoderResult<()> { | |
352 | self.decode_block(quant, false, coded, blk, if no < 4 { 0 } else { no - 3 }) | |
353 | } | |
354 | ||
355 | fn is_slice_end(&mut self) -> bool { self.mb_x + self.mb_y * self.mb_w >= self.mb_end } | |
356 | ||
357 | fn filter_row(&mut self, buf: &mut NAVideoBuffer<u8>, mb_y: usize, mb_w: usize, cbpi: &CBPInfo) { | |
358 | let stride = buf.get_stride(0); | |
359 | let mut off = buf.get_offset(0) + mb_y * 16 * stride; | |
360 | for mb_x in 0..mb_w { | |
361 | let coff = off; | |
362 | let coded0 = cbpi.is_coded(mb_x, 0); | |
363 | let coded1 = cbpi.is_coded(mb_x, 1); | |
364 | let q = cbpi.get_q(mb_w + mb_x); | |
365 | if mb_y != 0 { | |
366 | if coded0 && cbpi.is_coded_top(mb_x, 0) { deblock_hor(buf, 0, q, coff); } | |
367 | if coded1 && cbpi.is_coded_top(mb_x, 1) { deblock_hor(buf, 0, q, coff + 8); } | |
368 | } | |
369 | let coff = off + 8 * stride; | |
370 | if cbpi.is_coded(mb_x, 2) && coded0 { deblock_hor(buf, 0, q, coff); } | |
371 | if cbpi.is_coded(mb_x, 3) && coded1 { deblock_hor(buf, 0, q, coff + 8); } | |
372 | off += 16; | |
373 | } | |
374 | let mut leftt = false; | |
375 | let mut leftc = false; | |
376 | let mut off = buf.get_offset(0) + mb_y * 16 * stride; | |
377 | for mb_x in 0..mb_w { | |
378 | let ctop0 = cbpi.is_coded_top(mb_x, 0); | |
379 | let ctop1 = cbpi.is_coded_top(mb_x, 0); | |
380 | let ccur0 = cbpi.is_coded(mb_x, 0); | |
381 | let ccur1 = cbpi.is_coded(mb_x, 1); | |
382 | let q = cbpi.get_q(mb_w + mb_x); | |
383 | if mb_y != 0 { | |
384 | let coff = off - 8 * stride; | |
385 | let qtop = cbpi.get_q(mb_x); | |
386 | if leftt && ctop0 { deblock_ver(buf, 0, qtop, coff); } | |
387 | if ctop0 && ctop1 { deblock_ver(buf, 0, qtop, coff + 8); } | |
388 | } | |
389 | if leftc && ccur0 { deblock_ver(buf, 0, q, off); } | |
390 | if ccur0 && ccur1 { deblock_ver(buf, 0, q, off + 8); } | |
391 | leftt = ctop1; | |
392 | leftc = ccur1; | |
393 | off += 16; | |
394 | } | |
395 | let strideu = buf.get_stride(1); | |
396 | let stridev = buf.get_stride(2); | |
397 | let offu = buf.get_offset(1) + mb_y * 8 * strideu; | |
398 | let offv = buf.get_offset(2) + mb_y * 8 * stridev; | |
399 | if mb_y != 0 { | |
400 | for mb_x in 0..mb_w { | |
401 | let ctu = cbpi.is_coded_top(mb_x, 4); | |
402 | let ccu = cbpi.is_coded(mb_x, 4); | |
403 | let ctv = cbpi.is_coded_top(mb_x, 5); | |
404 | let ccv = cbpi.is_coded(mb_x, 5); | |
405 | let q = cbpi.get_q(mb_w + mb_x); | |
406 | if ctu && ccu { deblock_hor(buf, 1, q, offu + mb_x * 8); } | |
407 | if ctv && ccv { deblock_hor(buf, 2, q, offv + mb_x * 8); } | |
408 | } | |
409 | let mut leftu = false; | |
410 | let mut leftv = false; | |
411 | let offu = buf.get_offset(1) + (mb_y - 1) * 8 * strideu; | |
412 | let offv = buf.get_offset(2) + (mb_y - 1) * 8 * stridev; | |
413 | for mb_x in 0..mb_w { | |
414 | let ctu = cbpi.is_coded_top(mb_x, 4); | |
415 | let ctv = cbpi.is_coded_top(mb_x, 5); | |
416 | let qt = cbpi.get_q(mb_x); | |
417 | if leftu && ctu { deblock_ver(buf, 1, qt, offu + mb_x * 8); } | |
418 | if leftv && ctv { deblock_ver(buf, 2, qt, offv + mb_x * 8); } | |
419 | leftu = ctu; | |
420 | leftv = ctv; | |
421 | } | |
422 | } | |
423 | } | |
424 | } | |
425 | ||
426 | fn deblock_hor(buf: &mut NAVideoBuffer<u8>, comp: usize, q: u8, off: usize) { | |
427 | let stride = buf.get_stride(comp); | |
428 | let mut dptr = buf.get_data_mut(); | |
429 | let mut buf = dptr.as_mut_slice(); | |
430 | for x in 0..8 { | |
431 | let a = buf[off - 2 * stride + x] as i16; | |
432 | let b = buf[off - 1 * stride + x] as i16; | |
433 | let c = buf[off + 0 * stride + x] as i16; | |
434 | let d = buf[off + 1 * stride + x] as i16; | |
435 | let diff = ((a - d) * 3 + (c - b) * 8) >> 4; | |
436 | if (diff != 0) && (diff >= -32) && (diff < 32) { | |
437 | let d0 = diff.abs() * 2 - (q as i16); | |
438 | let d1 = if d0 < 0 { 0 } else { d0 }; | |
439 | let d2 = diff.abs() - d1; | |
440 | let d3 = if d2 < 0 { 0 } else { d2 }; | |
441 | ||
442 | let delta = if diff < 0 { -d3 } else { d3 }; | |
443 | ||
444 | let b1 = b + delta; | |
445 | if b1 < 0 { buf[off - 1 * stride + x] = 0; } | |
446 | else if b1 > 255 { buf[off - 1 * stride + x] = 0xFF; } | |
447 | else { buf[off - 1 * stride + x] = b1 as u8; } | |
448 | let c1 = c - delta; | |
449 | if c1 < 0 { buf[off + x] = 0; } | |
450 | else if c1 > 255 { buf[off + x] = 0xFF; } | |
451 | else { buf[off + x] = c1 as u8; } | |
452 | } | |
453 | } | |
454 | } | |
455 | ||
456 | impl<'a> RealVideo10BR<'a> { | |
457 | fn read_slice_header(&mut self) -> DecoderResult<RV10SliceInfo> { | |
458 | validate!(self.slice_no < self.num_slices); | |
459 | ||
460 | let mut br = &mut self.br; | |
461 | br.seek(self.slice_off[self.slice_no] * 8)?; | |
462 | //println!(" slice at off {}", br.tell()); | |
463 | ||
464 | let marker = br.read(1)?; | |
465 | validate!(marker == 1); | |
466 | let is_p = br.read_bool()?; | |
467 | let pb_frame = br.read_bool()?; | |
468 | validate!(!pb_frame); | |
469 | let qscale = br.read(5)? as u8; | |
470 | validate!(qscale > 0); | |
471 | ||
472 | if !is_p && self.new_ver { | |
473 | self.last_dc[0] = br.read(8)? as i16; | |
474 | self.last_dc[1] = br.read(8)? as i16; | |
475 | self.last_dc[2] = br.read(8)? as i16; | |
476 | } else { | |
477 | self.last_dc[0] = 0; | |
478 | self.last_dc[1] = 0; | |
479 | self.last_dc[2] = 0; | |
480 | } | |
481 | self.dc_coded[0] = false; | |
482 | self.dc_coded[1] = false; | |
483 | self.dc_coded[2] = false; | |
484 | ||
485 | let mb_x; | |
486 | let mb_y; | |
487 | let mb_count; | |
488 | let mb_pos = self.mb_x + self.mb_y * self.mb_w; | |
489 | if (br.peek(12) == 0) || (mb_pos > 0) { | |
490 | mb_x = br.read(6)? as usize; | |
491 | mb_y = br.read(6)? as usize; | |
492 | mb_count = br.read(12)? as usize; | |
493 | } else { | |
494 | mb_x = self.mb_x; | |
495 | mb_y = self.mb_y; | |
496 | mb_count = self.mb_count; | |
497 | } | |
498 | br.skip(3)?; | |
499 | println!("slice q {} mb {},{} {}", qscale, mb_x, mb_y, mb_count); | |
500 | validate!(mb_x + mb_y * self.mb_w + mb_count <= self.mb_w * self.mb_h); | |
501 | ||
502 | Ok(RV10SliceInfo::new(is_p, qscale, mb_x, mb_y, mb_count)) | |
503 | } | |
504 | } | |
505 | ||
506 | fn deblock_ver(buf: &mut NAVideoBuffer<u8>, comp: usize, q: u8, off: usize) { | |
507 | let stride = buf.get_stride(comp); | |
508 | let mut dptr = buf.get_data_mut(); | |
509 | let mut buf = dptr.as_mut_slice(); | |
510 | for y in 0..8 { | |
511 | let a = buf[off - 2 + y * stride] as i16; | |
512 | let b = buf[off - 1 + y * stride] as i16; | |
513 | let c = buf[off + 0 + y * stride] as i16; | |
514 | let d = buf[off + 1 + y * stride] as i16; | |
515 | let diff = ((a - d) * 3 + (c - b) * 8) >> 4; | |
516 | if (diff != 0) && (diff >= -32) && (diff < 32) { | |
517 | let d0 = diff.abs() * 2 - (q as i16); | |
518 | let d1 = if d0 < 0 { 0 } else { d0 }; | |
519 | let d2 = diff.abs() - d1; | |
520 | let d3 = if d2 < 0 { 0 } else { d2 }; | |
521 | ||
522 | let delta = if diff < 0 { -d3 } else { d3 }; | |
523 | ||
524 | let b1 = b + delta; | |
525 | if b1 < 0 { buf[off - 1 + y * stride] = 0; } | |
526 | else if b1 > 255 { buf[off - 1 + y * stride] = 0xFF; } | |
527 | else { buf[off - 1 + y * stride] = b1 as u8; } | |
528 | let c1 = c - delta; | |
529 | if c1 < 0 { buf[off + y * stride] = 0; } | |
530 | else if c1 > 255 { buf[off + y * stride] = 0xFF; } | |
531 | else { buf[off + y * stride] = c1 as u8; } | |
532 | } | |
533 | } | |
534 | } | |
535 | ||
536 | impl RealVideo10Decoder { | |
537 | fn new() -> Self { | |
538 | let mut coderead = H263ShortCodeReader::new(H263_INTRA_MCBPC); | |
539 | let intra_mcbpc_cb = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap(); | |
540 | let mut coderead = H263ShortCodeReader::new(H263_INTER_MCBPC); | |
541 | let inter_mcbpc_cb = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap(); | |
542 | let mut coderead = H263ShortCodeReader::new(H263_CBPY); | |
543 | let cbpy_cb = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap(); | |
544 | let mut coderead = H263RLCodeReader::new(H263_RL_CODES); | |
545 | let rl_cb = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap(); | |
546 | let mut coderead = H263RLCodeReader::new(H263_RL_CODES_AIC); | |
547 | let aic_rl_cb = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap(); | |
548 | let mut coderead = H263ShortCodeReader::new(H263_MV); | |
549 | let mv_cb = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap(); | |
550 | let mut coderead = CodeReader::new(RV10_LUMA_DC_CODES, RV10_LUMA_DC_BITS); | |
551 | let luma_dc_cb = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap(); | |
552 | let mut coderead = CodeReader::new(RV10_CHROMA_DC_CODES, RV10_CHROMA_DC_BITS); | |
553 | let chroma_dc_cb = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap(); | |
554 | ||
555 | let tables = Tables { | |
556 | intra_mcbpc_cb: intra_mcbpc_cb, | |
557 | inter_mcbpc_cb: inter_mcbpc_cb, | |
558 | cbpy_cb: cbpy_cb, | |
559 | rl_cb: rl_cb, | |
560 | aic_rl_cb: aic_rl_cb, | |
561 | mv_cb: mv_cb, | |
562 | luma_dc_cb: luma_dc_cb, | |
563 | chroma_dc_cb: chroma_dc_cb, | |
564 | }; | |
565 | ||
566 | RealVideo10Decoder{ | |
567 | info: Rc::new(DUMMY_CODEC_INFO), | |
568 | dec: H263BaseDecoder::new(), | |
569 | tables: tables, | |
570 | w: 0, | |
571 | h: 0, | |
572 | new_ver: false, | |
573 | } | |
574 | } | |
575 | } | |
576 | ||
577 | impl NADecoder for RealVideo10Decoder { | |
578 | fn init(&mut self, info: Rc<NACodecInfo>) -> DecoderResult<()> { | |
579 | if let NACodecTypeInfo::Video(vinfo) = info.get_properties() { | |
580 | let w = vinfo.get_width(); | |
581 | let h = vinfo.get_height(); | |
582 | let fmt = formats::YUV420_FORMAT; | |
583 | let myinfo = NACodecTypeInfo::Video(NAVideoInfo::new(w, h, false, fmt)); | |
584 | self.info = Rc::new(NACodecInfo::new_ref(info.get_name(), myinfo, info.get_extradata())); | |
585 | self.w = w; | |
586 | self.h = h; | |
587 | ||
588 | let edata = info.get_extradata().unwrap(); | |
589 | let src: &[u8] = &edata; | |
590 | let ver = ((src[4] as u32) << 12) | ((src[5] as u32) << 4) | ((src[6] as u32) >> 4); | |
591 | let maj_ver = ver >> 16; | |
592 | let mic_ver = ver & 0xFF; | |
593 | println!("ver {:06X} -> {}", ver, mic_ver); | |
594 | validate!(maj_ver == 1); | |
595 | self.new_ver = mic_ver > 1; | |
596 | { | |
597 | let mut br = BitReader::new(src, src.len(), BitReaderMode::BE); | |
598 | println!("edata:"); | |
599 | println!("{:08X}", br.read(32).unwrap()); | |
600 | println!("{:08X}", br.read(32).unwrap()); | |
601 | } | |
602 | Ok(()) | |
603 | } else { | |
604 | println!("???"); | |
605 | Err(DecoderError::InvalidData) | |
606 | } | |
607 | } | |
608 | fn decode(&mut self, pkt: &NAPacket) -> DecoderResult<NAFrameRef> { | |
609 | let src = pkt.get_buffer(); | |
610 | ||
611 | println!(" decode frame size {}, {} slices", src.len(), src[0]+1); | |
612 | let mut ibr = RealVideo10BR::new(&src, &self.tables, self.w, self.h, self.new_ver); | |
613 | ||
614 | let bufinfo = self.dec.parse_frame(&mut ibr)?; | |
615 | ||
616 | let mut frm = NAFrame::new_from_pkt(pkt, self.info.clone(), bufinfo); | |
617 | frm.set_keyframe(self.dec.is_intra()); | |
618 | frm.set_frame_type(if self.dec.is_intra() { FrameType::I } else { FrameType::P }); | |
619 | Ok(Rc::new(RefCell::new(frm))) | |
620 | } | |
621 | } | |
622 | ||
623 | ||
624 | pub fn get_decoder() -> Box<NADecoder> { | |
625 | Box::new(RealVideo10Decoder::new()) | |
626 | } | |
627 | ||
628 | #[cfg(test)] | |
629 | mod test { | |
630 | use test::dec_video::test_file_decoding; | |
631 | #[test] | |
632 | fn test_rv10() { | |
633 | test_file_decoding("realmedia", "assets/RV/rv10_dnet_640x352_realvideo_encoder_4.0.rm", Some(1000), true, false, Some("rv10")); | |
634 | } | |
635 | } | |
636 | ||
637 | pub struct CodeReader { codes: &'static [u16], bits: &'static [u8] } | |
638 | ||
639 | impl CodeReader { | |
640 | pub fn new(codes: &'static [u16], bits: &'static [u8]) -> Self { | |
641 | CodeReader { codes: codes, bits: bits } | |
642 | } | |
643 | } | |
644 | ||
645 | impl CodebookDescReader<u8> for CodeReader { | |
646 | fn bits(&mut self, idx: usize) -> u8 { self.bits[idx] } | |
647 | fn code(&mut self, idx: usize) -> u32 { self.codes[idx] as u32 } | |
648 | fn sym (&mut self, idx: usize) -> u8 { idx as u8 } | |
649 | fn len(&mut self) -> usize { self.bits.len() } | |
650 | } | |
651 | ||
652 | const RV10_LUMA_DC_CODES: &[u16] = &[ | |
653 | 0x001f, 0x0f00, 0x0f01, 0x0f02, 0x0f03, 0x0f04, 0x0f05, 0x0f06, | |
654 | 0x0f07, 0x0f08, 0x0f09, 0x0f0a, 0x0f0b, 0x0f0c, 0x0f0d, 0x0f0e, | |
655 | 0x0f0f, 0x0f10, 0x0f11, 0x0f12, 0x0f13, 0x0f14, 0x0f15, 0x0f16, | |
656 | 0x0f17, 0x0f18, 0x0f19, 0x0f1a, 0x0f1b, 0x0f1c, 0x0f1d, 0x0f1e, | |
657 | 0x0f1f, 0x0f20, 0x0f21, 0x0f22, 0x0f23, 0x0f24, 0x0f25, 0x0f26, | |
658 | 0x0f27, 0x0f28, 0x0f29, 0x0f2a, 0x0f2b, 0x0f2c, 0x0f2d, 0x0f2e, | |
659 | 0x0f2f, 0x0f30, 0x0f31, 0x0f32, 0x0f33, 0x0f34, 0x0f35, 0x0f36, | |
660 | 0x0f37, 0x0f38, 0x0f39, 0x0f3a, 0x0f3b, 0x0f3c, 0x0f3d, 0x0f3e, | |
661 | 0x0f3f, 0x0380, 0x0381, 0x0382, 0x0383, 0x0384, 0x0385, 0x0386, | |
662 | 0x0387, 0x0388, 0x0389, 0x038a, 0x038b, 0x038c, 0x038d, 0x038e, | |
663 | 0x038f, 0x0390, 0x0391, 0x0392, 0x0393, 0x0394, 0x0395, 0x0396, | |
664 | 0x0397, 0x0398, 0x0399, 0x039a, 0x039b, 0x039c, 0x039d, 0x039e, | |
665 | 0x039f, 0x00c0, 0x00c1, 0x00c2, 0x00c3, 0x00c4, 0x00c5, 0x00c6, | |
666 | 0x00c7, 0x00c8, 0x00c9, 0x00ca, 0x00cb, 0x00cc, 0x00cd, 0x00ce, | |
667 | 0x00cf, 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, | |
668 | 0x0057, 0x0020, 0x0021, 0x0022, 0x0023, 0x000c, 0x000d, 0x0004, | |
669 | 0x0000, 0x0005, 0x000e, 0x000f, 0x0024, 0x0025, 0x0026, 0x0027, | |
670 | 0x0058, 0x0059, 0x005a, 0x005b, 0x005c, 0x005d, 0x005e, 0x005f, | |
671 | 0x00d0, 0x00d1, 0x00d2, 0x00d3, 0x00d4, 0x00d5, 0x00d6, 0x00d7, | |
672 | 0x00d8, 0x00d9, 0x00da, 0x00db, 0x00dc, 0x00dd, 0x00de, 0x00df, | |
673 | 0x03a0, 0x03a1, 0x03a2, 0x03a3, 0x03a4, 0x03a5, 0x03a6, 0x03a7, | |
674 | 0x03a8, 0x03a9, 0x03aa, 0x03ab, 0x03ac, 0x03ad, 0x03ae, 0x03af, | |
675 | 0x03b0, 0x03b1, 0x03b2, 0x03b3, 0x03b4, 0x03b5, 0x03b6, 0x03b7, | |
676 | 0x03b8, 0x03b9, 0x03ba, 0x03bb, 0x03bc, 0x03bd, 0x03be, 0x03bf, | |
677 | 0x0f40, 0x0f41, 0x0f42, 0x0f43, 0x0f44, 0x0f45, 0x0f46, 0x0f47, | |
678 | 0x0f48, 0x0f49, 0x0f4a, 0x0f4b, 0x0f4c, 0x0f4d, 0x0f4e, 0x0f4f, | |
679 | 0x0f50, 0x0f51, 0x0f52, 0x0f53, 0x0f54, 0x0f55, 0x0f56, 0x0f57, | |
680 | 0x0f58, 0x0f59, 0x0f5a, 0x0f5b, 0x0f5c, 0x0f5d, 0x0f5e, 0x0f5f, | |
681 | 0x0f60, 0x0f61, 0x0f62, 0x0f63, 0x0f64, 0x0f65, 0x0f66, 0x0f67, | |
682 | 0x0f68, 0x0f69, 0x0f6a, 0x0f6b, 0x0f6c, 0x0f6d, 0x0f6e, 0x0f6f, | |
683 | 0x0f70, 0x0f71, 0x0f72, 0x0f73, 0x0f74, 0x0f75, 0x0f76, 0x0f77, | |
684 | 0x0f78, 0x0f79, 0x0f7a, 0x0f7b, 0x0f7c, 0x0f7d, 0x0f7e, 0x0f7f, | |
685 | ]; | |
686 | ||
687 | const RV10_LUMA_DC_BITS: &[u8] = &[ | |
688 | 5, 12, 12, 12, 12, 12, 12, 12, | |
689 | 12, 12, 12, 12, 12, 12, 12, 12, | |
690 | 12, 12, 12, 12, 12, 12, 12, 12, | |
691 | 12, 12, 12, 12, 12, 12, 12, 12, | |
692 | 12, 12, 12, 12, 12, 12, 12, 12, | |
693 | 12, 12, 12, 12, 12, 12, 12, 12, | |
694 | 12, 12, 12, 12, 12, 12, 12, 12, | |
695 | 12, 12, 12, 12, 12, 12, 12, 12, | |
696 | 12, 10, 10, 10, 10, 10, 10, 10, | |
697 | 10, 10, 10, 10, 10, 10, 10, 10, | |
698 | 10, 10, 10, 10, 10, 10, 10, 10, | |
699 | 10, 10, 10, 10, 10, 10, 10, 10, | |
700 | 10, 8, 8, 8, 8, 8, 8, 8, | |
701 | 8, 8, 8, 8, 8, 8, 8, 8, | |
702 | 8, 7, 7, 7, 7, 7, 7, 7, | |
703 | 7, 6, 6, 6, 6, 5, 5, 4, | |
704 | 2, 4, 5, 5, 6, 6, 6, 6, | |
705 | 7, 7, 7, 7, 7, 7, 7, 7, | |
706 | 8, 8, 8, 8, 8, 8, 8, 8, | |
707 | 8, 8, 8, 8, 8, 8, 8, 8, | |
708 | 10, 10, 10, 10, 10, 10, 10, 10, | |
709 | 10, 10, 10, 10, 10, 10, 10, 10, | |
710 | 10, 10, 10, 10, 10, 10, 10, 10, | |
711 | 10, 10, 10, 10, 10, 10, 10, 10, | |
712 | 12, 12, 12, 12, 12, 12, 12, 12, | |
713 | 12, 12, 12, 12, 12, 12, 12, 12, | |
714 | 12, 12, 12, 12, 12, 12, 12, 12, | |
715 | 12, 12, 12, 12, 12, 12, 12, 12, | |
716 | 12, 12, 12, 12, 12, 12, 12, 12, | |
717 | 12, 12, 12, 12, 12, 12, 12, 12, | |
718 | 12, 12, 12, 12, 12, 12, 12, 12, | |
719 | 12, 12, 12, 12, 12, 12, 12, 12, | |
720 | ]; | |
721 | ||
722 | const RV10_CHROMA_DC_CODES: &[u16] = &[ | |
723 | 0x003f, 0x3f00, 0x3f01, 0x3f02, 0x3f03, 0x3f04, 0x3f05, 0x3f06, | |
724 | 0x3f07, 0x3f08, 0x3f09, 0x3f0a, 0x3f0b, 0x3f0c, 0x3f0d, 0x3f0e, | |
725 | 0x3f0f, 0x3f10, 0x3f11, 0x3f12, 0x3f13, 0x3f14, 0x3f15, 0x3f16, | |
726 | 0x3f17, 0x3f18, 0x3f19, 0x3f1a, 0x3f1b, 0x3f1c, 0x3f1d, 0x3f1e, | |
727 | 0x3f1f, 0x3f20, 0x3f21, 0x3f22, 0x3f23, 0x3f24, 0x3f25, 0x3f26, | |
728 | 0x3f27, 0x3f28, 0x3f29, 0x3f2a, 0x3f2b, 0x3f2c, 0x3f2d, 0x3f2e, | |
729 | 0x3f2f, 0x3f30, 0x3f31, 0x3f32, 0x3f33, 0x3f34, 0x3f35, 0x3f36, | |
730 | 0x3f37, 0x3f38, 0x3f39, 0x3f3a, 0x3f3b, 0x3f3c, 0x3f3d, 0x3f3e, | |
731 | 0x3f3f, 0x0f80, 0x0f81, 0x0f82, 0x0f83, 0x0f84, 0x0f85, 0x0f86, | |
732 | 0x0f87, 0x0f88, 0x0f89, 0x0f8a, 0x0f8b, 0x0f8c, 0x0f8d, 0x0f8e, | |
733 | 0x0f8f, 0x0f90, 0x0f91, 0x0f92, 0x0f93, 0x0f94, 0x0f95, 0x0f96, | |
734 | 0x0f97, 0x0f98, 0x0f99, 0x0f9a, 0x0f9b, 0x0f9c, 0x0f9d, 0x0f9e, | |
735 | 0x0f9f, 0x03c0, 0x03c1, 0x03c2, 0x03c3, 0x03c4, 0x03c5, 0x03c6, | |
736 | 0x03c7, 0x03c8, 0x03c9, 0x03ca, 0x03cb, 0x03cc, 0x03cd, 0x03ce, | |
737 | 0x03cf, 0x00e0, 0x00e1, 0x00e2, 0x00e3, 0x00e4, 0x00e5, 0x00e6, | |
738 | 0x00e7, 0x0030, 0x0031, 0x0032, 0x0033, 0x0008, 0x0009, 0x0002, | |
739 | 0x0000, 0x0003, 0x000a, 0x000b, 0x0034, 0x0035, 0x0036, 0x0037, | |
740 | 0x00e8, 0x00e9, 0x00ea, 0x00eb, 0x00ec, 0x00ed, 0x00ee, 0x00ef, | |
741 | 0x03d0, 0x03d1, 0x03d2, 0x03d3, 0x03d4, 0x03d5, 0x03d6, 0x03d7, | |
742 | 0x03d8, 0x03d9, 0x03da, 0x03db, 0x03dc, 0x03dd, 0x03de, 0x03df, | |
743 | 0x0fa0, 0x0fa1, 0x0fa2, 0x0fa3, 0x0fa4, 0x0fa5, 0x0fa6, 0x0fa7, | |
744 | 0x0fa8, 0x0fa9, 0x0faa, 0x0fab, 0x0fac, 0x0fad, 0x0fae, 0x0faf, | |
745 | 0x0fb0, 0x0fb1, 0x0fb2, 0x0fb3, 0x0fb4, 0x0fb5, 0x0fb6, 0x0fb7, | |
746 | 0x0fb8, 0x0fb9, 0x0fba, 0x0fbb, 0x0fbc, 0x0fbd, 0x0fbe, 0x0fbf, | |
747 | 0x3f40, 0x3f41, 0x3f42, 0x3f43, 0x3f44, 0x3f45, 0x3f46, 0x3f47, | |
748 | 0x3f48, 0x3f49, 0x3f4a, 0x3f4b, 0x3f4c, 0x3f4d, 0x3f4e, 0x3f4f, | |
749 | 0x3f50, 0x3f51, 0x3f52, 0x3f53, 0x3f54, 0x3f55, 0x3f56, 0x3f57, | |
750 | 0x3f58, 0x3f59, 0x3f5a, 0x3f5b, 0x3f5c, 0x3f5d, 0x3f5e, 0x3f5f, | |
751 | 0x3f60, 0x3f61, 0x3f62, 0x3f63, 0x3f64, 0x3f65, 0x3f66, 0x3f67, | |
752 | 0x3f68, 0x3f69, 0x3f6a, 0x3f6b, 0x3f6c, 0x3f6d, 0x3f6e, 0x3f6f, | |
753 | 0x3f70, 0x3f71, 0x3f72, 0x3f73, 0x3f74, 0x3f75, 0x3f76, 0x3f77, | |
754 | 0x3f78, 0x3f79, 0x3f7a, 0x3f7b, 0x3f7c, 0x3f7d, 0x3f7e, 0x3f7f, | |
755 | ]; | |
756 | ||
757 | const RV10_CHROMA_DC_BITS: &[u8] = &[ | |
758 | 7, 14, 14, 14, 14, 14, 14, 14, | |
759 | 14, 14, 14, 14, 14, 14, 14, 14, | |
760 | 14, 14, 14, 14, 14, 14, 14, 14, | |
761 | 14, 14, 14, 14, 14, 14, 14, 14, | |
762 | 14, 14, 14, 14, 14, 14, 14, 14, | |
763 | 14, 14, 14, 14, 14, 14, 14, 14, | |
764 | 14, 14, 14, 14, 14, 14, 14, 14, | |
765 | 14, 14, 14, 14, 14, 14, 14, 14, | |
766 | 14, 12, 12, 12, 12, 12, 12, 12, | |
767 | 12, 12, 12, 12, 12, 12, 12, 12, | |
768 | 12, 12, 12, 12, 12, 12, 12, 12, | |
769 | 12, 12, 12, 12, 12, 12, 12, 12, | |
770 | 12, 10, 10, 10, 10, 10, 10, 10, | |
771 | 10, 10, 10, 10, 10, 10, 10, 10, | |
772 | 10, 8, 8, 8, 8, 8, 8, 8, | |
773 | 8, 6, 6, 6, 6, 4, 4, 3, | |
774 | 2, 3, 4, 4, 6, 6, 6, 6, | |
775 | 8, 8, 8, 8, 8, 8, 8, 8, | |
776 | 10, 10, 10, 10, 10, 10, 10, 10, | |
777 | 10, 10, 10, 10, 10, 10, 10, 10, | |
778 | 12, 12, 12, 12, 12, 12, 12, 12, | |
779 | 12, 12, 12, 12, 12, 12, 12, 12, | |
780 | 12, 12, 12, 12, 12, 12, 12, 12, | |
781 | 12, 12, 12, 12, 12, 12, 12, 12, | |
782 | 14, 14, 14, 14, 14, 14, 14, 14, | |
783 | 14, 14, 14, 14, 14, 14, 14, 14, | |
784 | 14, 14, 14, 14, 14, 14, 14, 14, | |
785 | 14, 14, 14, 14, 14, 14, 14, 14, | |
786 | 14, 14, 14, 14, 14, 14, 14, 14, | |
787 | 14, 14, 14, 14, 14, 14, 14, 14, | |
788 | 14, 14, 14, 14, 14, 14, 14, 14, | |
789 | 14, 14, 14, 14, 14, 14, 14, 14, | |
790 | ]; |