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, | |
123 | } | |
124 | ||
125 | const SLICE_NO_END: usize = 99999999; | |
126 | ||
127 | impl SliceInfo { | |
128 | pub fn new(mb_x: usize, mb_y: usize, mb_end: usize, quant: u8) -> Self { | |
129 | SliceInfo{ mb_x: mb_x, mb_y: mb_y, mb_end: mb_end, quant: quant } | |
130 | } | |
131 | pub fn new_gob(mb_x: usize, mb_y: usize, quant: u8) -> Self { | |
132 | SliceInfo{ mb_x: mb_x, mb_y: mb_y, mb_end: SLICE_NO_END, quant: quant } | |
88c58b1b KS |
133 | } |
134 | pub fn get_default_slice(pinfo: &PicInfo) -> Self { | |
2a2aa421 | 135 | SliceInfo{ mb_x: 0, mb_y: 0, mb_end: SLICE_NO_END, quant: pinfo.get_quant() } |
88c58b1b KS |
136 | } |
137 | pub fn get_quant(&self) -> u8 { self.quant } | |
2a2aa421 KS |
138 | pub fn is_at_end(&self, mb_pos: usize) -> bool { self.mb_end == mb_pos } |
139 | pub fn needs_check(&self) -> bool { self.mb_end == SLICE_NO_END } | |
140 | } | |
141 | ||
142 | impl SliceState { | |
143 | pub fn new(is_iframe: bool) -> Self { | |
144 | SliceState { is_iframe: is_iframe, mb_x: 0, mb_y: 0 } | |
145 | } | |
146 | pub fn next_mb(&mut self) { self.mb_x += 1; } | |
147 | pub fn new_row(&mut self) { self.mb_x = 0; self.mb_y += 1; } | |
88c58b1b KS |
148 | } |
149 | ||
150 | #[derive(Debug,Clone,Copy)] | |
151 | pub struct BlockInfo { | |
ef3209c7 KS |
152 | pub intra: bool, |
153 | pub skip: bool, | |
154 | pub mode: Type, | |
155 | pub cbp: u8, | |
156 | pub q: u8, | |
157 | pub mv: [MV; 4], | |
158 | pub num_mv: usize, | |
159 | pub bpart: bool, | |
160 | pub b_cbp: u8, | |
161 | pub mv2: [MV; 2], | |
162 | pub num_mv2: usize, | |
163 | pub fwd: bool, | |
164 | pub acpred: ACPredMode, | |
88c58b1b KS |
165 | } |
166 | ||
167 | #[allow(dead_code)] | |
168 | #[derive(Debug,Clone,Copy)] | |
169 | pub struct BBlockInfo { | |
170 | present: bool, | |
171 | cbp: u8, | |
172 | num_mv: usize, | |
173 | fwd: bool, | |
174 | } | |
175 | ||
ef3209c7 KS |
176 | #[allow(dead_code)] |
177 | #[derive(Debug,Clone,Copy,PartialEq)] | |
178 | pub enum ACPredMode { | |
179 | None, | |
180 | DC, | |
181 | Ver, | |
182 | Hor, | |
183 | } | |
184 | ||
88c58b1b KS |
185 | #[allow(dead_code)] |
186 | impl BlockInfo { | |
187 | pub fn new(mode: Type, cbp: u8, q: u8) -> Self { | |
188 | BlockInfo { | |
189 | intra: mode == Type::I, | |
190 | skip: (cbp == 0) && (mode != Type::I), | |
191 | mode: mode, | |
192 | cbp: cbp, | |
193 | q: q, | |
194 | mv: [MV::new(0, 0), MV::new(0, 0), MV::new(0, 0), MV::new(0, 0)], | |
195 | num_mv: 0, | |
196 | bpart: false, | |
197 | b_cbp: 0, | |
198 | mv2: [ZERO_MV, ZERO_MV], | |
199 | num_mv2: 0, | |
200 | fwd: false, | |
ef3209c7 | 201 | acpred: ACPredMode::None, |
88c58b1b KS |
202 | } |
203 | } | |
204 | pub fn is_intra(&self) -> bool { self.intra } | |
205 | pub fn is_skipped(&self) -> bool { self.skip } | |
206 | pub fn get_mode(&self) -> Type { self.mode } | |
207 | pub fn get_cbp(&self) -> u8 { self.cbp } | |
208 | pub fn get_q(&self) -> u8 { self.q } | |
209 | pub fn get_num_mvs(&self) -> usize { self.num_mv } | |
210 | pub fn get_mv(&self, idx: usize) -> MV { self.mv[idx] } | |
211 | pub fn has_b_part(&self) -> bool { self.bpart } | |
212 | pub fn get_cbp_b(&self) -> u8 { self.b_cbp } | |
213 | pub fn get_num_mvs2(&self) -> usize { self.num_mv2 } | |
214 | pub fn get_mv2(&self, idx: usize) -> MV { self.mv2[idx] } | |
215 | pub fn set_mv(&mut self, mvs: &[MV]) { | |
216 | if mvs.len() > 0 { self.skip = false; } | |
217 | let mut mv_arr: [MV; 4] = [MV::new(0, 0), MV::new(0, 0), MV::new(0, 0), MV::new(0, 0)]; | |
218 | for i in 0..mvs.len() { mv_arr[i] = mvs[i]; } | |
219 | self.mv = mv_arr; | |
220 | self.num_mv = mvs.len(); | |
221 | } | |
222 | pub fn set_bpart(&mut self, bbinfo: BBlockInfo) { | |
223 | self.bpart = bbinfo.present; | |
224 | self.b_cbp = bbinfo.cbp; | |
225 | self.fwd = bbinfo.fwd; | |
226 | self.num_mv2 = bbinfo.get_num_mv(); | |
227 | } | |
228 | pub fn set_b_mv(&mut self, mvs: &[MV]) { | |
229 | if mvs.len() > 0 { self.skip = false; } | |
230 | let mut mv_arr: [MV; 2] = [ZERO_MV, ZERO_MV]; | |
231 | for i in 0..mvs.len() { mv_arr[i] = mvs[i]; } | |
232 | self.mv2 = mv_arr; | |
233 | } | |
234 | pub fn is_b_fwd(&self) -> bool { self.fwd } | |
ef3209c7 KS |
235 | pub fn set_acpred(&mut self, acpred: ACPredMode) { self.acpred = acpred } |
236 | pub fn get_acpred(&self) -> ACPredMode { self.acpred } | |
88c58b1b KS |
237 | } |
238 | ||
239 | impl BBlockInfo { | |
240 | pub fn new(present: bool, cbp: u8, num_mv: usize, fwd: bool) -> Self { | |
241 | BBlockInfo { | |
242 | present: present, | |
243 | cbp: cbp, | |
244 | num_mv: num_mv, | |
245 | fwd: fwd, | |
246 | } | |
247 | } | |
248 | pub fn get_num_mv(&self) -> usize { self.num_mv } | |
249 | } | |
250 | ||
2324f30b KS |
251 | #[derive(Debug,Clone,Copy)] |
252 | pub enum MVMode { | |
253 | Old, | |
254 | Long, | |
255 | UMV, | |
256 | } | |
257 | ||
88c58b1b KS |
258 | #[derive(Debug,Clone,Copy)] |
259 | pub struct MV { | |
260 | x: i16, | |
261 | y: i16, | |
262 | } | |
263 | ||
264 | impl MV { | |
265 | pub fn new(x: i16, y: i16) -> Self { MV{ x: x, y: y } } | |
266 | pub fn pred(a: MV, b: MV, c: MV) -> Self { | |
267 | let x; | |
268 | if a.x < b.x { | |
269 | if b.x < c.x { | |
270 | x = b.x; | |
271 | } else { | |
272 | if a.x < c.x { x = c.x; } else { x = a.x; } | |
273 | } | |
274 | } else { | |
275 | if b.x < c.x { | |
276 | if a.x < c.x { x = a.x; } else { x = c.x; } | |
277 | } else { | |
278 | x = b.x; | |
279 | } | |
280 | } | |
281 | let y; | |
282 | if a.y < b.y { | |
283 | if b.y < c.y { | |
284 | y = b.y; | |
285 | } else { | |
286 | if a.y < c.y { y = c.y; } else { y = a.y; } | |
287 | } | |
288 | } else { | |
289 | if b.y < c.y { | |
290 | if a.y < c.y { y = a.y; } else { y = c.y; } | |
291 | } else { | |
292 | y = b.y; | |
293 | } | |
294 | } | |
295 | MV { x: x, y: y } | |
296 | } | |
2324f30b | 297 | fn add_umv(pred_mv: MV, add: MV, mvmode: MVMode) -> Self { |
88c58b1b | 298 | let mut new_mv = pred_mv + add; |
2324f30b KS |
299 | match mvmode { |
300 | MVMode::Old => { | |
301 | if new_mv.x >= 64 { new_mv.x -= 64; } | |
302 | else if new_mv.x <= -64 { new_mv.x += 64; } | |
303 | if new_mv.y >= 64 { new_mv.y -= 64; } | |
304 | else if new_mv.y <= -64 { new_mv.y += 64; } | |
305 | }, | |
306 | MVMode::Long => { | |
307 | if new_mv.x > 31 { new_mv.x -= 64; } | |
308 | else if new_mv.x < -32 { new_mv.x += 64; } | |
309 | if new_mv.y > 31 { new_mv.y -= 64; } | |
310 | else if new_mv.y < -32 { new_mv.y += 64; } | |
311 | }, | |
312 | MVMode::UMV => { | |
313 | if pred_mv.x > 32 && new_mv.x > 63 { new_mv.x -= 64; } | |
314 | if pred_mv.x < -31 && new_mv.x < -63 { new_mv.x += 64; } | |
315 | if pred_mv.y > 32 && new_mv.y > 63 { new_mv.y -= 64; } | |
316 | if pred_mv.y < -31 && new_mv.y < -63 { new_mv.y += 64; } | |
317 | }, | |
318 | }; | |
88c58b1b KS |
319 | new_mv |
320 | } | |
321 | fn scale(&self, trb: u8, trd: u8) -> Self { | |
322 | if (trd == 0) || (trb == 0) { | |
323 | ZERO_MV | |
324 | } else { | |
325 | MV { x: (self.x * (trb as i16)) / (trd as i16), y: (self.y * (trb as i16)) / (trd as i16) } | |
326 | } | |
327 | } | |
328 | fn b_sub(pvec: MV, fwdvec: MV, bvec: MV, trb: u8, trd: u8) -> Self { | |
329 | let bscale = (trb as i16) - (trd as i16); | |
330 | let x = if bvec.x != 0 { fwdvec.x - pvec.x } else if trd != 0 { bscale * pvec.x / (trd as i16) } else { 0 }; | |
331 | let y = if bvec.y != 0 { fwdvec.y - pvec.y } else if trd != 0 { bscale * pvec.y / (trd as i16) } else { 0 }; | |
332 | MV { x: x, y: y } | |
333 | } | |
334 | } | |
335 | ||
336 | pub const ZERO_MV: MV = MV { x: 0, y: 0 }; | |
337 | ||
338 | impl Add for MV { | |
339 | type Output = MV; | |
340 | fn add(self, other: MV) -> MV { MV { x: self.x + other.x, y: self.y + other.y } } | |
341 | } | |
342 | ||
343 | impl Sub for MV { | |
344 | type Output = MV; | |
345 | fn sub(self, other: MV) -> MV { MV { x: self.x - other.x, y: self.y - other.y } } | |
346 | } | |
347 | ||
348 | impl fmt::Display for MV { | |
349 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
350 | write!(f, "{},{}", self.x, self.y) | |
351 | } | |
352 | } | |
353 | ||
354 | #[allow(dead_code)] | |
355 | pub struct CBPInfo { | |
356 | cbp: Vec<u8>, | |
357 | q: Vec<u8>, | |
358 | mb_w: usize, | |
359 | } | |
360 | ||
361 | impl CBPInfo { | |
362 | fn new() -> Self { CBPInfo{ cbp: Vec::new(), q: Vec::new(), mb_w: 0 } } | |
363 | fn reset(&mut self, mb_w: usize) { | |
364 | self.mb_w = mb_w; | |
365 | self.cbp.truncate(0); | |
366 | self.cbp.resize(self.mb_w * 2, 0); | |
367 | self.q.truncate(0); | |
368 | self.q.resize(self.mb_w * 2, 0); | |
369 | } | |
370 | fn update_row(&mut self) { | |
371 | for i in 0..self.mb_w { | |
372 | self.cbp[i] = self.cbp[self.mb_w + i]; | |
373 | self.q[i] = self.q[self.mb_w + i]; | |
374 | } | |
375 | } | |
376 | fn set_cbp(&mut self, mb_x: usize, cbp: u8) { | |
377 | self.cbp[self.mb_w + mb_x] = cbp; | |
378 | } | |
379 | fn set_q(&mut self, mb_x: usize, q: u8) { | |
380 | self.q[self.mb_w + mb_x] = q; | |
381 | } | |
382 | pub fn get_q(&self, mb_x: usize) -> u8 { self.q[mb_x] } | |
383 | pub fn is_coded(&self, mb_x: usize, blk_no: usize) -> bool { | |
384 | (self.cbp[self.mb_w + mb_x] & (1 << (5 - blk_no))) != 0 | |
385 | } | |
386 | pub fn is_coded_top(&self, mb_x: usize, blk_no: usize) -> bool { | |
387 | let cbp = self.cbp[self.mb_w + mb_x]; | |
388 | let cbp_top = self.cbp[mb_x]; | |
389 | match blk_no { | |
390 | 0 => { (cbp_top & 0b001000) != 0 }, | |
391 | 1 => { (cbp_top & 0b000100) != 0 }, | |
392 | 2 => { (cbp & 0b100000) != 0 }, | |
393 | 3 => { (cbp & 0b010000) != 0 }, | |
394 | 4 => { (cbp_top & 0b000010) != 0 }, | |
395 | _ => { (cbp_top & 0b000001) != 0 }, | |
396 | } | |
397 | } | |
398 | } | |
399 |