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