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>; | |
19 | fn decode_slice_header(&mut self, pinfo: &PicInfo) -> DecoderResult<Slice>; | |
20 | fn decode_block_header(&mut self, pinfo: &PicInfo, sinfo: &Slice) -> DecoderResult<BlockInfo>; | |
21 | fn decode_block_intra(&mut self, info: &BlockInfo, quant: u8, no: usize, coded: bool, blk: &mut [i16; 64]) -> DecoderResult<()>; | |
22 | fn decode_block_inter(&mut self, info: &BlockInfo, quant: u8, no: usize, coded: bool, blk: &mut [i16; 64]) -> DecoderResult<()>; | |
23 | fn is_slice_end(&mut self) -> bool; | |
24 | ||
25 | fn filter_row(&mut self, buf: &mut NAVideoBuffer<u8>, mb_y: usize, mb_w: usize, cbpi: &CBPInfo); | |
26 | } | |
27 | ||
28 | #[derive(Debug,Clone,Copy,PartialEq)] | |
29 | pub enum Type { | |
30 | I, P, Skip, Special | |
31 | } | |
32 | ||
33 | #[allow(dead_code)] | |
34 | #[derive(Debug,Clone,Copy)] | |
35 | pub struct PBInfo { | |
36 | trb: u8, | |
37 | dbquant: u8, | |
38 | } | |
39 | ||
40 | impl PBInfo { | |
41 | pub fn new(trb: u8, dbquant: u8) -> Self { | |
42 | PBInfo{ trb: trb, dbquant: dbquant } | |
43 | } | |
44 | pub fn get_trb(&self) -> u8 { self.trb } | |
45 | pub fn get_dbquant(&self) -> u8 { self.dbquant } | |
46 | } | |
47 | ||
48 | #[allow(dead_code)] | |
49 | #[derive(Debug,Clone,Copy)] | |
50 | pub struct PicInfo { | |
51 | w: usize, | |
52 | h: usize, | |
53 | mode: Type, | |
54 | quant: u8, | |
55 | apm: bool, | |
2324f30b | 56 | mvmode: MVMode, |
88c58b1b KS |
57 | pb: Option<PBInfo>, |
58 | ts: u8, | |
59 | deblock: bool, | |
60 | } | |
61 | ||
62 | #[allow(dead_code)] | |
63 | impl PicInfo { | |
2324f30b KS |
64 | pub fn new(w: usize, h: usize, mode: Type, quant: u8, apm: bool, mvmode: MVMode, ts: u8, pb: Option<PBInfo>, deblock: bool) -> Self { |
65 | PicInfo{ w: w, h: h, mode: mode, quant: quant, apm: apm, mvmode: mvmode, ts: ts, pb: pb, deblock: deblock } | |
88c58b1b KS |
66 | } |
67 | pub fn get_width(&self) -> usize { self.w } | |
68 | pub fn get_height(&self) -> usize { self.h } | |
69 | pub fn get_mode(&self) -> Type { self.mode } | |
70 | pub fn get_quant(&self) -> u8 { self.quant } | |
71 | pub fn get_apm(&self) -> bool { self.apm } | |
2324f30b | 72 | pub fn get_mvmode(&self) -> MVMode { self.mvmode } |
88c58b1b KS |
73 | pub fn is_pb(&self) -> bool { self.pb.is_some() } |
74 | pub fn get_ts(&self) -> u8 { self.ts } | |
75 | pub fn get_pbinfo(&self) -> PBInfo { self.pb.unwrap() } | |
76 | } | |
77 | ||
78 | #[derive(Debug,Clone,Copy)] | |
79 | pub struct Slice { | |
80 | mb_x: usize, | |
81 | mb_y: usize, | |
82 | quant: u8, | |
83 | } | |
84 | ||
85 | impl Slice { | |
86 | pub fn new(mb_x: usize, mb_y: usize, quant: u8) -> Self { | |
87 | Slice{ mb_x: mb_x, mb_y: mb_y, quant: quant } | |
88 | } | |
89 | pub fn get_default_slice(pinfo: &PicInfo) -> Self { | |
90 | Slice{ mb_x: 0, mb_y: 0, quant: pinfo.get_quant() } | |
91 | } | |
92 | pub fn get_quant(&self) -> u8 { self.quant } | |
93 | } | |
94 | ||
95 | #[derive(Debug,Clone,Copy)] | |
96 | pub struct BlockInfo { | |
97 | intra: bool, | |
98 | skip: bool, | |
99 | mode: Type, | |
100 | cbp: u8, | |
101 | q: u8, | |
102 | mv: [MV; 4], | |
103 | num_mv: usize, | |
104 | bpart: bool, | |
105 | b_cbp: u8, | |
106 | mv2: [MV; 2], | |
107 | num_mv2: usize, | |
108 | fwd: bool, | |
109 | } | |
110 | ||
111 | #[allow(dead_code)] | |
112 | #[derive(Debug,Clone,Copy)] | |
113 | pub struct BBlockInfo { | |
114 | present: bool, | |
115 | cbp: u8, | |
116 | num_mv: usize, | |
117 | fwd: bool, | |
118 | } | |
119 | ||
120 | #[allow(dead_code)] | |
121 | impl BlockInfo { | |
122 | pub fn new(mode: Type, cbp: u8, q: u8) -> Self { | |
123 | BlockInfo { | |
124 | intra: mode == Type::I, | |
125 | skip: (cbp == 0) && (mode != Type::I), | |
126 | mode: mode, | |
127 | cbp: cbp, | |
128 | q: q, | |
129 | mv: [MV::new(0, 0), MV::new(0, 0), MV::new(0, 0), MV::new(0, 0)], | |
130 | num_mv: 0, | |
131 | bpart: false, | |
132 | b_cbp: 0, | |
133 | mv2: [ZERO_MV, ZERO_MV], | |
134 | num_mv2: 0, | |
135 | fwd: false, | |
136 | } | |
137 | } | |
138 | pub fn is_intra(&self) -> bool { self.intra } | |
139 | pub fn is_skipped(&self) -> bool { self.skip } | |
140 | pub fn get_mode(&self) -> Type { self.mode } | |
141 | pub fn get_cbp(&self) -> u8 { self.cbp } | |
142 | pub fn get_q(&self) -> u8 { self.q } | |
143 | pub fn get_num_mvs(&self) -> usize { self.num_mv } | |
144 | pub fn get_mv(&self, idx: usize) -> MV { self.mv[idx] } | |
145 | pub fn has_b_part(&self) -> bool { self.bpart } | |
146 | pub fn get_cbp_b(&self) -> u8 { self.b_cbp } | |
147 | pub fn get_num_mvs2(&self) -> usize { self.num_mv2 } | |
148 | pub fn get_mv2(&self, idx: usize) -> MV { self.mv2[idx] } | |
149 | pub fn set_mv(&mut self, mvs: &[MV]) { | |
150 | if mvs.len() > 0 { self.skip = false; } | |
151 | let mut mv_arr: [MV; 4] = [MV::new(0, 0), MV::new(0, 0), MV::new(0, 0), MV::new(0, 0)]; | |
152 | for i in 0..mvs.len() { mv_arr[i] = mvs[i]; } | |
153 | self.mv = mv_arr; | |
154 | self.num_mv = mvs.len(); | |
155 | } | |
156 | pub fn set_bpart(&mut self, bbinfo: BBlockInfo) { | |
157 | self.bpart = bbinfo.present; | |
158 | self.b_cbp = bbinfo.cbp; | |
159 | self.fwd = bbinfo.fwd; | |
160 | self.num_mv2 = bbinfo.get_num_mv(); | |
161 | } | |
162 | pub fn set_b_mv(&mut self, mvs: &[MV]) { | |
163 | if mvs.len() > 0 { self.skip = false; } | |
164 | let mut mv_arr: [MV; 2] = [ZERO_MV, ZERO_MV]; | |
165 | for i in 0..mvs.len() { mv_arr[i] = mvs[i]; } | |
166 | self.mv2 = mv_arr; | |
167 | } | |
168 | pub fn is_b_fwd(&self) -> bool { self.fwd } | |
169 | } | |
170 | ||
171 | impl BBlockInfo { | |
172 | pub fn new(present: bool, cbp: u8, num_mv: usize, fwd: bool) -> Self { | |
173 | BBlockInfo { | |
174 | present: present, | |
175 | cbp: cbp, | |
176 | num_mv: num_mv, | |
177 | fwd: fwd, | |
178 | } | |
179 | } | |
180 | pub fn get_num_mv(&self) -> usize { self.num_mv } | |
181 | } | |
182 | ||
2324f30b KS |
183 | #[derive(Debug,Clone,Copy)] |
184 | pub enum MVMode { | |
185 | Old, | |
186 | Long, | |
187 | UMV, | |
188 | } | |
189 | ||
88c58b1b KS |
190 | #[derive(Debug,Clone,Copy)] |
191 | pub struct MV { | |
192 | x: i16, | |
193 | y: i16, | |
194 | } | |
195 | ||
196 | impl MV { | |
197 | pub fn new(x: i16, y: i16) -> Self { MV{ x: x, y: y } } | |
198 | pub fn pred(a: MV, b: MV, c: MV) -> Self { | |
199 | let x; | |
200 | if a.x < b.x { | |
201 | if b.x < c.x { | |
202 | x = b.x; | |
203 | } else { | |
204 | if a.x < c.x { x = c.x; } else { x = a.x; } | |
205 | } | |
206 | } else { | |
207 | if b.x < c.x { | |
208 | if a.x < c.x { x = a.x; } else { x = c.x; } | |
209 | } else { | |
210 | x = b.x; | |
211 | } | |
212 | } | |
213 | let y; | |
214 | if a.y < b.y { | |
215 | if b.y < c.y { | |
216 | y = b.y; | |
217 | } else { | |
218 | if a.y < c.y { y = c.y; } else { y = a.y; } | |
219 | } | |
220 | } else { | |
221 | if b.y < c.y { | |
222 | if a.y < c.y { y = a.y; } else { y = c.y; } | |
223 | } else { | |
224 | y = b.y; | |
225 | } | |
226 | } | |
227 | MV { x: x, y: y } | |
228 | } | |
2324f30b | 229 | fn add_umv(pred_mv: MV, add: MV, mvmode: MVMode) -> Self { |
88c58b1b | 230 | let mut new_mv = pred_mv + add; |
2324f30b KS |
231 | match mvmode { |
232 | MVMode::Old => { | |
233 | if new_mv.x >= 64 { new_mv.x -= 64; } | |
234 | else if new_mv.x <= -64 { new_mv.x += 64; } | |
235 | if new_mv.y >= 64 { new_mv.y -= 64; } | |
236 | else if new_mv.y <= -64 { new_mv.y += 64; } | |
237 | }, | |
238 | MVMode::Long => { | |
239 | if new_mv.x > 31 { new_mv.x -= 64; } | |
240 | else if new_mv.x < -32 { new_mv.x += 64; } | |
241 | if new_mv.y > 31 { new_mv.y -= 64; } | |
242 | else if new_mv.y < -32 { new_mv.y += 64; } | |
243 | }, | |
244 | MVMode::UMV => { | |
245 | if pred_mv.x > 32 && new_mv.x > 63 { new_mv.x -= 64; } | |
246 | if pred_mv.x < -31 && new_mv.x < -63 { new_mv.x += 64; } | |
247 | if pred_mv.y > 32 && new_mv.y > 63 { new_mv.y -= 64; } | |
248 | if pred_mv.y < -31 && new_mv.y < -63 { new_mv.y += 64; } | |
249 | }, | |
250 | }; | |
88c58b1b KS |
251 | new_mv |
252 | } | |
253 | fn scale(&self, trb: u8, trd: u8) -> Self { | |
254 | if (trd == 0) || (trb == 0) { | |
255 | ZERO_MV | |
256 | } else { | |
257 | MV { x: (self.x * (trb as i16)) / (trd as i16), y: (self.y * (trb as i16)) / (trd as i16) } | |
258 | } | |
259 | } | |
260 | fn b_sub(pvec: MV, fwdvec: MV, bvec: MV, trb: u8, trd: u8) -> Self { | |
261 | let bscale = (trb as i16) - (trd as i16); | |
262 | let x = if bvec.x != 0 { fwdvec.x - pvec.x } else if trd != 0 { bscale * pvec.x / (trd as i16) } else { 0 }; | |
263 | let y = if bvec.y != 0 { fwdvec.y - pvec.y } else if trd != 0 { bscale * pvec.y / (trd as i16) } else { 0 }; | |
264 | MV { x: x, y: y } | |
265 | } | |
266 | } | |
267 | ||
268 | pub const ZERO_MV: MV = MV { x: 0, y: 0 }; | |
269 | ||
270 | impl Add for MV { | |
271 | type Output = MV; | |
272 | fn add(self, other: MV) -> MV { MV { x: self.x + other.x, y: self.y + other.y } } | |
273 | } | |
274 | ||
275 | impl Sub for MV { | |
276 | type Output = MV; | |
277 | fn sub(self, other: MV) -> MV { MV { x: self.x - other.x, y: self.y - other.y } } | |
278 | } | |
279 | ||
280 | impl fmt::Display for MV { | |
281 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
282 | write!(f, "{},{}", self.x, self.y) | |
283 | } | |
284 | } | |
285 | ||
286 | #[allow(dead_code)] | |
287 | pub struct CBPInfo { | |
288 | cbp: Vec<u8>, | |
289 | q: Vec<u8>, | |
290 | mb_w: usize, | |
291 | } | |
292 | ||
293 | impl CBPInfo { | |
294 | fn new() -> Self { CBPInfo{ cbp: Vec::new(), q: Vec::new(), mb_w: 0 } } | |
295 | fn reset(&mut self, mb_w: usize) { | |
296 | self.mb_w = mb_w; | |
297 | self.cbp.truncate(0); | |
298 | self.cbp.resize(self.mb_w * 2, 0); | |
299 | self.q.truncate(0); | |
300 | self.q.resize(self.mb_w * 2, 0); | |
301 | } | |
302 | fn update_row(&mut self) { | |
303 | for i in 0..self.mb_w { | |
304 | self.cbp[i] = self.cbp[self.mb_w + i]; | |
305 | self.q[i] = self.q[self.mb_w + i]; | |
306 | } | |
307 | } | |
308 | fn set_cbp(&mut self, mb_x: usize, cbp: u8) { | |
309 | self.cbp[self.mb_w + mb_x] = cbp; | |
310 | } | |
311 | fn set_q(&mut self, mb_x: usize, q: u8) { | |
312 | self.q[self.mb_w + mb_x] = q; | |
313 | } | |
314 | pub fn get_q(&self, mb_x: usize) -> u8 { self.q[mb_x] } | |
315 | pub fn is_coded(&self, mb_x: usize, blk_no: usize) -> bool { | |
316 | (self.cbp[self.mb_w + mb_x] & (1 << (5 - blk_no))) != 0 | |
317 | } | |
318 | pub fn is_coded_top(&self, mb_x: usize, blk_no: usize) -> bool { | |
319 | let cbp = self.cbp[self.mb_w + mb_x]; | |
320 | let cbp_top = self.cbp[mb_x]; | |
321 | match blk_no { | |
322 | 0 => { (cbp_top & 0b001000) != 0 }, | |
323 | 1 => { (cbp_top & 0b000100) != 0 }, | |
324 | 2 => { (cbp & 0b100000) != 0 }, | |
325 | 3 => { (cbp & 0b010000) != 0 }, | |
326 | 4 => { (cbp_top & 0b000010) != 0 }, | |
327 | _ => { (cbp_top & 0b000001) != 0 }, | |
328 | } | |
329 | } | |
330 | } | |
331 |