Commit | Line | Data |
---|---|---|
88c58b1b KS |
1 | use std::fmt; |
2 | use std::ops::{Add, Sub}; | |
3 | use super::DecoderResult; | |
4 | use frame::NAVideoBuffer; | |
5 | ||
6 | pub mod code; | |
7 | pub mod data; | |
8 | pub mod decoder; | |
9 | ||
10 | #[cfg(feature="decoder_intel263")] | |
11 | pub mod intel263; | |
2324f30b KS |
12 | #[cfg(feature="decoder_realvideo1")] |
13 | pub mod rv10; | |
be91b1c3 KS |
14 | #[cfg(feature="decoder_realvideo2")] |
15 | pub mod rv20; | |
88c58b1b KS |
16 | |
17 | pub trait BlockDecoder { | |
18 | fn decode_pichdr(&mut self) -> DecoderResult<PicInfo>; | |
2a2aa421 KS |
19 | fn decode_slice_header(&mut self, pinfo: &PicInfo) -> DecoderResult<SliceInfo>; |
20 | fn decode_block_header(&mut self, pinfo: &PicInfo, sinfo: &SliceInfo, sstate: &SliceState) -> DecoderResult<BlockInfo>; | |
21 | fn decode_block_intra(&mut self, info: &BlockInfo, sstate: &SliceState, quant: u8, no: usize, coded: bool, blk: &mut [i16; 64]) -> DecoderResult<()>; | |
22 | fn decode_block_inter(&mut self, info: &BlockInfo, sstate: &SliceState, quant: u8, no: usize, coded: bool, blk: &mut [i16; 64]) -> DecoderResult<()>; | |
88c58b1b | 23 | fn is_slice_end(&mut self) -> bool; |
2a2aa421 | 24 | } |
88c58b1b | 25 | |
2a2aa421 KS |
26 | pub trait BlockDSP { |
27 | fn idct(&self, blk: &mut [i16; 64]); | |
28 | fn copy_blocks(&self, dst: &mut NAVideoBuffer<u8>, src: &NAVideoBuffer<u8>, xpos: usize, ypos: usize, w: usize, h: usize, mv: MV); | |
29 | fn avg_blocks(&self, dst: &mut NAVideoBuffer<u8>, src: &NAVideoBuffer<u8>, xpos: usize, ypos: usize, w: usize, h: usize, mv: MV); | |
30 | fn filter_row(&self, buf: &mut NAVideoBuffer<u8>, mb_y: usize, mb_w: usize, cbpi: &CBPInfo); | |
88c58b1b KS |
31 | } |
32 | ||
2a2aa421 | 33 | #[allow(dead_code)] |
88c58b1b KS |
34 | #[derive(Debug,Clone,Copy,PartialEq)] |
35 | pub enum Type { | |
2a2aa421 | 36 | I, P, PB, Skip, B, Special |
88c58b1b KS |
37 | } |
38 | ||
39 | #[allow(dead_code)] | |
40 | #[derive(Debug,Clone,Copy)] | |
41 | pub struct PBInfo { | |
42 | trb: u8, | |
43 | dbquant: u8, | |
44 | } | |
45 | ||
46 | impl PBInfo { | |
47 | pub fn new(trb: u8, dbquant: u8) -> Self { | |
48 | PBInfo{ trb: trb, dbquant: dbquant } | |
49 | } | |
50 | pub fn get_trb(&self) -> u8 { self.trb } | |
51 | pub fn get_dbquant(&self) -> u8 { self.dbquant } | |
52 | } | |
53 | ||
54 | #[allow(dead_code)] | |
55 | #[derive(Debug,Clone,Copy)] | |
56 | pub struct PicInfo { | |
2a2aa421 KS |
57 | pub w: usize, |
58 | pub h: usize, | |
59 | pub mode: Type, | |
60 | pub umv: bool, | |
61 | pub apm: bool, | |
62 | pub quant: u8, | |
63 | pub pb: Option<PBInfo>, | |
64 | pub ts: u8, | |
65 | pub plusinfo: Option<PlusInfo>, | |
88c58b1b KS |
66 | } |
67 | ||
68 | #[allow(dead_code)] | |
69 | impl PicInfo { | |
2a2aa421 KS |
70 | pub fn new(w: usize, h: usize, mode: Type, umv: bool, apm: bool, quant: u8, ts: u8, pb: Option<PBInfo>, plusinfo: Option<PlusInfo>) -> Self { |
71 | PicInfo { | |
72 | w: w, h: h, mode: mode, | |
73 | umv: umv, apm: apm, quant: quant, | |
74 | pb: pb, ts: ts, plusinfo: plusinfo | |
75 | } | |
88c58b1b KS |
76 | } |
77 | pub fn get_width(&self) -> usize { self.w } | |
78 | pub fn get_height(&self) -> usize { self.h } | |
79 | pub fn get_mode(&self) -> Type { self.mode } | |
80 | pub fn get_quant(&self) -> u8 { self.quant } | |
81 | pub fn get_apm(&self) -> bool { self.apm } | |
88c58b1b KS |
82 | pub fn is_pb(&self) -> bool { self.pb.is_some() } |
83 | pub fn get_ts(&self) -> u8 { self.ts } | |
84 | pub fn get_pbinfo(&self) -> PBInfo { self.pb.unwrap() } | |
2a2aa421 KS |
85 | pub fn get_plusifo(&self) -> Option<PlusInfo> { self.plusinfo } |
86 | pub fn get_mvmode(&self) -> MVMode { | |
87 | if self.umv { MVMode::UMV } | |
88 | else if self.apm { MVMode::Long } | |
89 | else { MVMode::Old } | |
90 | } | |
88c58b1b KS |
91 | } |
92 | ||
2a2aa421 | 93 | #[allow(dead_code)] |
88c58b1b | 94 | #[derive(Debug,Clone,Copy)] |
2a2aa421 KS |
95 | pub struct PlusInfo { |
96 | pub aic: bool, | |
97 | pub deblock: bool, | |
98 | pub aiv_mode: bool, | |
99 | pub mq_mode: bool, | |
88c58b1b KS |
100 | } |
101 | ||
2a2aa421 KS |
102 | impl PlusInfo { |
103 | pub fn new(aic: bool, deblock: bool, aiv_mode: bool, mq_mode: bool) -> Self { | |
104 | PlusInfo { aic: aic, deblock: deblock, aiv_mode: aiv_mode, mq_mode: mq_mode } | |
105 | } | |
106 | } | |
107 | ||
108 | #[allow(dead_code)] | |
109 | #[derive(Debug,Clone,Copy)] | |
110 | pub struct SliceInfo { | |
111 | pub mb_x: usize, | |
112 | pub mb_y: usize, | |
113 | pub mb_end: usize, | |
114 | pub quant: u8, | |
115 | } | |
116 | ||
117 | #[allow(dead_code)] | |
118 | #[derive(Debug,Clone,Copy)] | |
119 | pub struct SliceState { | |
120 | pub is_iframe: bool, | |
121 | pub mb_x: usize, | |
122 | pub mb_y: usize, | |
6f21f5f4 | 123 | pub first_line: bool, |
2eb21477 | 124 | pub first_mb: bool, |
2a2aa421 KS |
125 | } |
126 | ||
127 | const SLICE_NO_END: usize = 99999999; | |
128 | ||
129 | impl SliceInfo { | |
130 | pub fn new(mb_x: usize, mb_y: usize, mb_end: usize, quant: u8) -> Self { | |
131 | SliceInfo{ mb_x: mb_x, mb_y: mb_y, mb_end: mb_end, quant: quant } | |
132 | } | |
133 | pub fn new_gob(mb_x: usize, mb_y: usize, quant: u8) -> Self { | |
134 | SliceInfo{ mb_x: mb_x, mb_y: mb_y, mb_end: SLICE_NO_END, quant: quant } | |
88c58b1b KS |
135 | } |
136 | pub fn get_default_slice(pinfo: &PicInfo) -> Self { | |
2a2aa421 | 137 | SliceInfo{ mb_x: 0, mb_y: 0, mb_end: SLICE_NO_END, quant: pinfo.get_quant() } |
88c58b1b KS |
138 | } |
139 | pub fn get_quant(&self) -> u8 { self.quant } | |
2a2aa421 KS |
140 | pub fn is_at_end(&self, mb_pos: usize) -> bool { self.mb_end == mb_pos } |
141 | pub fn needs_check(&self) -> bool { self.mb_end == SLICE_NO_END } | |
142 | } | |
143 | ||
144 | impl SliceState { | |
145 | pub fn new(is_iframe: bool) -> Self { | |
2eb21477 KS |
146 | SliceState { is_iframe: is_iframe, mb_x: 0, mb_y: 0, first_line: true, first_mb: true } |
147 | } | |
148 | pub fn next_mb(&mut self) { self.mb_x += 1; self.first_mb = false; } | |
149 | pub fn new_row(&mut self) { | |
150 | self.mb_x = 0; self.mb_y += 1; | |
151 | self.first_line = false; self.first_mb = true; | |
2a2aa421 | 152 | } |
88c58b1b KS |
153 | } |
154 | ||
155 | #[derive(Debug,Clone,Copy)] | |
156 | pub struct BlockInfo { | |
ef3209c7 KS |
157 | pub intra: bool, |
158 | pub skip: bool, | |
159 | pub mode: Type, | |
160 | pub cbp: u8, | |
161 | pub q: u8, | |
162 | pub mv: [MV; 4], | |
163 | pub num_mv: usize, | |
164 | pub bpart: bool, | |
165 | pub b_cbp: u8, | |
166 | pub mv2: [MV; 2], | |
167 | pub num_mv2: usize, | |
168 | pub fwd: bool, | |
169 | pub acpred: ACPredMode, | |
88c58b1b KS |
170 | } |
171 | ||
172 | #[allow(dead_code)] | |
173 | #[derive(Debug,Clone,Copy)] | |
174 | pub struct BBlockInfo { | |
175 | present: bool, | |
176 | cbp: u8, | |
177 | num_mv: usize, | |
178 | fwd: bool, | |
179 | } | |
180 | ||
ef3209c7 KS |
181 | #[allow(dead_code)] |
182 | #[derive(Debug,Clone,Copy,PartialEq)] | |
183 | pub enum ACPredMode { | |
184 | None, | |
185 | DC, | |
186 | Ver, | |
187 | Hor, | |
188 | } | |
189 | ||
88c58b1b KS |
190 | #[allow(dead_code)] |
191 | impl BlockInfo { | |
192 | pub fn new(mode: Type, cbp: u8, q: u8) -> Self { | |
193 | BlockInfo { | |
194 | intra: mode == Type::I, | |
195 | skip: (cbp == 0) && (mode != Type::I), | |
196 | mode: mode, | |
197 | cbp: cbp, | |
198 | q: q, | |
199 | mv: [MV::new(0, 0), MV::new(0, 0), MV::new(0, 0), MV::new(0, 0)], | |
200 | num_mv: 0, | |
201 | bpart: false, | |
202 | b_cbp: 0, | |
203 | mv2: [ZERO_MV, ZERO_MV], | |
204 | num_mv2: 0, | |
205 | fwd: false, | |
ef3209c7 | 206 | acpred: ACPredMode::None, |
88c58b1b KS |
207 | } |
208 | } | |
209 | pub fn is_intra(&self) -> bool { self.intra } | |
210 | pub fn is_skipped(&self) -> bool { self.skip } | |
211 | pub fn get_mode(&self) -> Type { self.mode } | |
212 | pub fn get_cbp(&self) -> u8 { self.cbp } | |
213 | pub fn get_q(&self) -> u8 { self.q } | |
214 | pub fn get_num_mvs(&self) -> usize { self.num_mv } | |
215 | pub fn get_mv(&self, idx: usize) -> MV { self.mv[idx] } | |
216 | pub fn has_b_part(&self) -> bool { self.bpart } | |
217 | pub fn get_cbp_b(&self) -> u8 { self.b_cbp } | |
218 | pub fn get_num_mvs2(&self) -> usize { self.num_mv2 } | |
219 | pub fn get_mv2(&self, idx: usize) -> MV { self.mv2[idx] } | |
220 | pub fn set_mv(&mut self, mvs: &[MV]) { | |
221 | if mvs.len() > 0 { self.skip = false; } | |
222 | let mut mv_arr: [MV; 4] = [MV::new(0, 0), MV::new(0, 0), MV::new(0, 0), MV::new(0, 0)]; | |
223 | for i in 0..mvs.len() { mv_arr[i] = mvs[i]; } | |
224 | self.mv = mv_arr; | |
225 | self.num_mv = mvs.len(); | |
226 | } | |
227 | pub fn set_bpart(&mut self, bbinfo: BBlockInfo) { | |
228 | self.bpart = bbinfo.present; | |
229 | self.b_cbp = bbinfo.cbp; | |
230 | self.fwd = bbinfo.fwd; | |
231 | self.num_mv2 = bbinfo.get_num_mv(); | |
232 | } | |
233 | pub fn set_b_mv(&mut self, mvs: &[MV]) { | |
234 | if mvs.len() > 0 { self.skip = false; } | |
235 | let mut mv_arr: [MV; 2] = [ZERO_MV, ZERO_MV]; | |
236 | for i in 0..mvs.len() { mv_arr[i] = mvs[i]; } | |
237 | self.mv2 = mv_arr; | |
238 | } | |
239 | pub fn is_b_fwd(&self) -> bool { self.fwd } | |
ef3209c7 KS |
240 | pub fn set_acpred(&mut self, acpred: ACPredMode) { self.acpred = acpred } |
241 | pub fn get_acpred(&self) -> ACPredMode { self.acpred } | |
88c58b1b KS |
242 | } |
243 | ||
244 | impl BBlockInfo { | |
245 | pub fn new(present: bool, cbp: u8, num_mv: usize, fwd: bool) -> Self { | |
246 | BBlockInfo { | |
247 | present: present, | |
248 | cbp: cbp, | |
249 | num_mv: num_mv, | |
250 | fwd: fwd, | |
251 | } | |
252 | } | |
253 | pub fn get_num_mv(&self) -> usize { self.num_mv } | |
254 | } | |
255 | ||
2324f30b KS |
256 | #[derive(Debug,Clone,Copy)] |
257 | pub enum MVMode { | |
258 | Old, | |
259 | Long, | |
260 | UMV, | |
261 | } | |
262 | ||
88c58b1b KS |
263 | #[derive(Debug,Clone,Copy)] |
264 | pub struct MV { | |
265 | x: i16, | |
266 | y: i16, | |
267 | } | |
268 | ||
269 | impl MV { | |
270 | pub fn new(x: i16, y: i16) -> Self { MV{ x: x, y: y } } | |
271 | pub fn pred(a: MV, b: MV, c: MV) -> Self { | |
272 | let x; | |
273 | if a.x < b.x { | |
274 | if b.x < c.x { | |
275 | x = b.x; | |
276 | } else { | |
277 | if a.x < c.x { x = c.x; } else { x = a.x; } | |
278 | } | |
279 | } else { | |
280 | if b.x < c.x { | |
281 | if a.x < c.x { x = a.x; } else { x = c.x; } | |
282 | } else { | |
283 | x = b.x; | |
284 | } | |
285 | } | |
286 | let y; | |
287 | if a.y < b.y { | |
288 | if b.y < c.y { | |
289 | y = b.y; | |
290 | } else { | |
291 | if a.y < c.y { y = c.y; } else { y = a.y; } | |
292 | } | |
293 | } else { | |
294 | if b.y < c.y { | |
295 | if a.y < c.y { y = a.y; } else { y = c.y; } | |
296 | } else { | |
297 | y = b.y; | |
298 | } | |
299 | } | |
300 | MV { x: x, y: y } | |
301 | } | |
2324f30b | 302 | fn add_umv(pred_mv: MV, add: MV, mvmode: MVMode) -> Self { |
88c58b1b | 303 | let mut new_mv = pred_mv + add; |
2324f30b KS |
304 | match mvmode { |
305 | MVMode::Old => { | |
306 | if new_mv.x >= 64 { new_mv.x -= 64; } | |
307 | else if new_mv.x <= -64 { new_mv.x += 64; } | |
308 | if new_mv.y >= 64 { new_mv.y -= 64; } | |
309 | else if new_mv.y <= -64 { new_mv.y += 64; } | |
310 | }, | |
311 | MVMode::Long => { | |
312 | if new_mv.x > 31 { new_mv.x -= 64; } | |
313 | else if new_mv.x < -32 { new_mv.x += 64; } | |
314 | if new_mv.y > 31 { new_mv.y -= 64; } | |
315 | else if new_mv.y < -32 { new_mv.y += 64; } | |
316 | }, | |
317 | MVMode::UMV => { | |
318 | if pred_mv.x > 32 && new_mv.x > 63 { new_mv.x -= 64; } | |
319 | if pred_mv.x < -31 && new_mv.x < -63 { new_mv.x += 64; } | |
320 | if pred_mv.y > 32 && new_mv.y > 63 { new_mv.y -= 64; } | |
321 | if pred_mv.y < -31 && new_mv.y < -63 { new_mv.y += 64; } | |
322 | }, | |
323 | }; | |
88c58b1b KS |
324 | new_mv |
325 | } | |
326 | fn scale(&self, trb: u8, trd: u8) -> Self { | |
327 | if (trd == 0) || (trb == 0) { | |
328 | ZERO_MV | |
329 | } else { | |
330 | MV { x: (self.x * (trb as i16)) / (trd as i16), y: (self.y * (trb as i16)) / (trd as i16) } | |
331 | } | |
332 | } | |
333 | fn b_sub(pvec: MV, fwdvec: MV, bvec: MV, trb: u8, trd: u8) -> Self { | |
334 | let bscale = (trb as i16) - (trd as i16); | |
335 | let x = if bvec.x != 0 { fwdvec.x - pvec.x } else if trd != 0 { bscale * pvec.x / (trd as i16) } else { 0 }; | |
336 | let y = if bvec.y != 0 { fwdvec.y - pvec.y } else if trd != 0 { bscale * pvec.y / (trd as i16) } else { 0 }; | |
337 | MV { x: x, y: y } | |
338 | } | |
339 | } | |
340 | ||
341 | pub const ZERO_MV: MV = MV { x: 0, y: 0 }; | |
342 | ||
343 | impl Add for MV { | |
344 | type Output = MV; | |
345 | fn add(self, other: MV) -> MV { MV { x: self.x + other.x, y: self.y + other.y } } | |
346 | } | |
347 | ||
348 | impl Sub for MV { | |
349 | type Output = MV; | |
350 | fn sub(self, other: MV) -> MV { MV { x: self.x - other.x, y: self.y - other.y } } | |
351 | } | |
352 | ||
353 | impl fmt::Display for MV { | |
354 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
355 | write!(f, "{},{}", self.x, self.y) | |
356 | } | |
357 | } | |
358 | ||
359 | #[allow(dead_code)] | |
360 | pub struct CBPInfo { | |
361 | cbp: Vec<u8>, | |
362 | q: Vec<u8>, | |
363 | mb_w: usize, | |
364 | } | |
365 | ||
366 | impl CBPInfo { | |
367 | fn new() -> Self { CBPInfo{ cbp: Vec::new(), q: Vec::new(), mb_w: 0 } } | |
368 | fn reset(&mut self, mb_w: usize) { | |
369 | self.mb_w = mb_w; | |
370 | self.cbp.truncate(0); | |
371 | self.cbp.resize(self.mb_w * 2, 0); | |
372 | self.q.truncate(0); | |
373 | self.q.resize(self.mb_w * 2, 0); | |
374 | } | |
375 | fn update_row(&mut self) { | |
376 | for i in 0..self.mb_w { | |
377 | self.cbp[i] = self.cbp[self.mb_w + i]; | |
378 | self.q[i] = self.q[self.mb_w + i]; | |
379 | } | |
380 | } | |
381 | fn set_cbp(&mut self, mb_x: usize, cbp: u8) { | |
382 | self.cbp[self.mb_w + mb_x] = cbp; | |
383 | } | |
384 | fn set_q(&mut self, mb_x: usize, q: u8) { | |
385 | self.q[self.mb_w + mb_x] = q; | |
386 | } | |
387 | pub fn get_q(&self, mb_x: usize) -> u8 { self.q[mb_x] } | |
388 | pub fn is_coded(&self, mb_x: usize, blk_no: usize) -> bool { | |
389 | (self.cbp[self.mb_w + mb_x] & (1 << (5 - blk_no))) != 0 | |
390 | } | |
391 | pub fn is_coded_top(&self, mb_x: usize, blk_no: usize) -> bool { | |
392 | let cbp = self.cbp[self.mb_w + mb_x]; | |
393 | let cbp_top = self.cbp[mb_x]; | |
394 | match blk_no { | |
395 | 0 => { (cbp_top & 0b001000) != 0 }, | |
396 | 1 => { (cbp_top & 0b000100) != 0 }, | |
397 | 2 => { (cbp & 0b100000) != 0 }, | |
398 | 3 => { (cbp & 0b010000) != 0 }, | |
399 | 4 => { (cbp_top & 0b000010) != 0 }, | |
400 | _ => { (cbp_top & 0b000001) != 0 }, | |
401 | } | |
402 | } | |
403 | } | |
404 |