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