indeo3enc: rework cell intra flag logic
[nihav.git] / nihav-indeo / src / codecs / indeo3enc / tree.rs
CommitLineData
77c25c7b
KS
1use super::Indeo3Writer;
2use super::mv::*;
3use super::cell::{CellEncoder, MAX_CELL_SIZE};
bafe9cd4 4use std::ops::DerefMut;
77c25c7b
KS
5
6pub enum Indeo3PrimaryTree {
7 VSplit(Box<Indeo3PrimaryTree>, Box<Indeo3PrimaryTree>),
8 HSplit(Box<Indeo3PrimaryTree>, Box<Indeo3PrimaryTree>),
9 RelFill(MV, Box<Indeo3SecondaryTree>),
10 AbsFill(Box<Indeo3SecondaryTree>),
11}
12
13impl Indeo3PrimaryTree {
14 pub fn print(&self) {
15 println!("Plane tree:");
16 self.print1(1);
17 }
18 fn print1(&self, depth: u8) {
19 for _ in 0..depth {
20 print!(" ");
21 }
22 match self {
23 Indeo3PrimaryTree::VSplit(t1, t2) => {
24 println!("vertical split");
25 t1.print1(depth + 1);
26 t2.print1(depth + 1);
27 },
28 Indeo3PrimaryTree::HSplit(t1, t2) => {
29 println!("horizontal split");
30 t1.print1(depth + 1);
31 t2.print1(depth + 1);
32 },
33 Indeo3PrimaryTree::RelFill(mv, sec) => {
34 println!("relative fill {},{}", mv.x, mv.y);
35 sec.print1(depth + 1);
36 },
37 Indeo3PrimaryTree::AbsFill(sec) => {
38 println!("absolute fill");
39 sec.print1(depth + 1);
40 }
41 }
42 }
43}
44
45pub enum Indeo3SecondaryTree {
46 VSplit(Box<Indeo3SecondaryTree>, Box<Indeo3SecondaryTree>),
47 HSplit(Box<Indeo3SecondaryTree>, Box<Indeo3SecondaryTree>),
48 VQData(u8),
49 VQNull(u8),
50}
51
52impl Indeo3SecondaryTree {
53 fn print1(&self, depth: u8) {
54 for _ in 0..depth {
55 print!(" ");
56 }
57 match self {
58 Indeo3SecondaryTree::VSplit(t1, t2) => {
59 println!("vertical split");
60 t1.print1(depth + 1);
61 t2.print1(depth + 1);
62 },
63 Indeo3SecondaryTree::HSplit(t1, t2) => {
64 println!("horizontal split");
65 t1.print1(depth + 1);
66 t2.print1(depth + 1);
67 },
68 Indeo3SecondaryTree::VQData(mode) => {
69 println!("VQ data ({})", mode);
70 },
71 Indeo3SecondaryTree::VQNull(mode) => {
72 println!("VQ Null ({})", mode);
73 }
74 }
75 }
76}
77
78const THRESHOLD: u32 = 64;
79
80#[derive(Clone, Copy)]
81pub struct Indeo3Cell {
82 x: u8,
83 y: u8,
84 w: u8,
85 h: u8,
86 intra: bool,
87}
88
89impl Indeo3Cell {
3ff976b2 90 pub fn new(width: usize, height: usize) -> Self {
77c25c7b
KS
91 Self {
92 x: 0,
93 y: 0,
94 w: (width / 4) as u8,
95 h: (height / 4) as u8,
3ff976b2 96 intra: false,
77c25c7b
KS
97 }
98 }
99
100 pub fn get_x(&self) -> usize { usize::from(self.x) * 4 }
101 pub fn get_y(&self) -> usize { usize::from(self.y) * 4 }
102 pub fn get_width(&self) -> usize { usize::from(self.w) * 4 }
103 pub fn get_height(&self) -> usize { usize::from(self.h) * 4 }
104 pub fn is_intra(&self) -> bool { self.intra }
105
106 fn split_h(&self) -> (Self, Self) {
107 let h1 = if self.h > 2 { ((self.h + 2) >> 2) << 1 } else { 1 };
108 let h2 = self.h - h1;
109 let mut cell1 = *self;
110 cell1.h = h1;
111 let mut cell2 = *self;
112 cell2.y += h1;
113 cell2.h = h2;
114 (cell1, cell2)
115 }
116 fn split_v(&self, stripw: u8) -> (Self, Self) {
117 let w1 = if self.w > stripw {
118 if self.w > stripw * 2 { stripw * 2 } else { stripw }
119 } else {
120 if self.w > 2 { ((self.w + 2) >> 2) << 1 } else { 1 }
121 };
122 let w2 = self.w - w1;
123 let mut cell1 = *self;
124 cell1.w = w1;
125 let mut cell2 = *self;
126 cell2.x += w1;
127 cell2.w = w2;
128 (cell1, cell2)
129 }
130}
131
132impl std::fmt::Display for Indeo3Cell {
133 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
134 write!(f, "[{}x{} @ {},{}{}]", self.get_width(), self.get_height(), self.get_x(), self.get_y(), if self.intra { " intra" } else { "" })
135 }
136}
137
138#[derive(Default)]
139pub struct Plane {
140 pub data: Vec<u8>,
141 pub width: usize,
142 pub height: usize,
143 pub stripw: u8,
144 pub mvs: Vec<(MV, u16)>,
145}
146
147fn ssd(a: u8, b: u8) -> u32 {
148 let diff = i32::from(a) - i32::from(b);
149 (diff * diff) as u32
150}
151
152impl Plane {
153 pub fn alloc(&mut self, width: usize, height: usize, stripw: u8) {
154 self.data.resize(width * height, 0);
155 self.width = width;
156 self.height = height;
157 self.stripw = stripw;
158 if self.mvs.capacity() < 256 {
159 self.mvs.reserve(256);
160 }
161 }
162 pub fn fill(&mut self, src: &[u8], stride: usize) {
163 for (dline, sline) in self.data.chunks_mut(self.width).zip(src.chunks(stride)) {
164 for (dst, &src) in dline.iter_mut().zip(sline.iter()) {
165 *dst = src >> 1;
166 }
167 }
168 }
169 pub fn clear_mvs(&mut self){
170 self.mvs.clear();
171 }
172 pub fn checksum(&self) -> u16 {
7b430a1e 173 let xors = self.data.chunks(2).fold([0u8; 2], |acc, pair| [acc[0] ^ pair[0], acc[1] ^ pair[1]]);
77c25c7b
KS
174 u16::from(xors[0]) | (u16::from(xors[1]) * 256)
175 }
176 pub fn find_cells(&mut self, is_intra: bool, pplane: &Plane, mv_est: &MotionEstimator) -> Box<Indeo3PrimaryTree> {
3ff976b2
KS
177 let cell = Indeo3Cell::new(self.width, self.height);
178 self.split_pri(cell, pplane, mv_est, is_intra)
77c25c7b 179 }
3ff976b2 180 fn split_pri(&mut self, mut cell: Indeo3Cell, pplane: &Plane, mv_est: &MotionEstimator, is_intra: bool) -> Box<Indeo3PrimaryTree> {
77c25c7b
KS
181 let width = cell.get_width();
182 let height = cell.get_height();
183 if width * height > MAX_CELL_SIZE {
184 let (hsplit, vsplit) = if width != height {
185 (width > (self.stripw as usize) * 4 || width > height, height > width)
186 } else {
187 let (hdiff, vdiff) = self.calculate_diffs(cell);
188 (vdiff > THRESHOLD && vdiff > hdiff,
189 hdiff > THRESHOLD && hdiff > vdiff)
190 };
191 match (hsplit, vsplit) {
192 (true, _) => {
193 let (cell1, cell2) = cell.split_v(self.stripw);
3ff976b2
KS
194 let tree1 = self.split_pri(cell1, pplane, mv_est, is_intra);
195 let tree2 = self.split_pri(cell2, pplane, mv_est, is_intra);
77c25c7b
KS
196 Box::new(Indeo3PrimaryTree::VSplit(tree1, tree2))
197 },
198 (_, true) => {
199 let (cell1, cell2) = cell.split_h();
3ff976b2
KS
200 let tree1 = self.split_pri(cell1, pplane, mv_est, is_intra);
201 let tree2 = self.split_pri(cell2, pplane, mv_est, is_intra);
77c25c7b
KS
202 Box::new(Indeo3PrimaryTree::HSplit(tree1, tree2))
203 },
204 (false, false) => {
205 let sec = self.split_sec(cell);
206 Box::new(Indeo3PrimaryTree::AbsFill(sec))
207 },
208 }
209 } else {
3ff976b2 210 if !is_intra {
77c25c7b 211 if let Some((mv, flat)) = mv_est.mv_search(self, pplane, cell) {
bafe9cd4 212 return self.add_mv_tree(mv, flat, cell);
77c25c7b
KS
213 }
214
215 // try splitting once to see if it improves situation
216 if width >= 16 && height >= 16 {
217 let vsplit = width > height;
3ff976b2 218 let (mut cell1, mut cell2) = if vsplit {
77c25c7b
KS
219 cell.split_v(self.stripw)
220 } else {
221 cell.split_h()
222 };
223 let search1 = mv_est.mv_search(self, pplane, cell1);
224 let search2 = mv_est.mv_search(self, pplane, cell2);
225 if search1.is_some() || search2.is_some() {
226 let tree1 = if let Some((mv, flat)) = search1 {
bafe9cd4 227 self.add_mv_tree(mv, flat, cell1)
77c25c7b 228 } else {
3ff976b2 229 cell1.intra = true;
77c25c7b
KS
230 let sec = self.split_sec(cell1);
231 Box::new(Indeo3PrimaryTree::AbsFill(sec))
232 };
233 let tree2 = if let Some((mv, flat)) = search2 {
bafe9cd4 234 self.add_mv_tree(mv, flat, cell2)
77c25c7b 235 } else {
3ff976b2 236 cell2.intra = true;
77c25c7b
KS
237 let sec = self.split_sec(cell2);
238 Box::new(Indeo3PrimaryTree::AbsFill(sec))
239 };
240 return if vsplit {
241 Box::new(Indeo3PrimaryTree::VSplit(tree1, tree2))
242 } else {
243 Box::new(Indeo3PrimaryTree::HSplit(tree1, tree2))
244 }
245 }
246 }
247 }
3ff976b2 248 cell.intra = true;
77c25c7b
KS
249 let sec = self.split_sec(cell);
250 Box::new(Indeo3PrimaryTree::AbsFill(sec))
251 }
252 }
bafe9cd4 253 fn add_mv_tree(&mut self, mv: MV, flat: bool, cell: Indeo3Cell) -> Box<Indeo3PrimaryTree> {
77c25c7b
KS
254 let sec = if flat {
255 Box::new(Indeo3SecondaryTree::VQNull(0))
256 } else {
257 Box::new(Indeo3SecondaryTree::VQData(0))
258 };
259
260 let mut found = false;
261 for (ref cmv, ref mut count) in self.mvs.iter_mut() {
262 if cmv == &mv {
bafe9cd4 263 *count += u16::from(cell.w) * u16::from(cell.h);
77c25c7b
KS
264 found = true;
265 break;
266 }
267 }
268 if !found {
269 self.mvs.push((mv, 1));
270 }
271
272 Box::new(Indeo3PrimaryTree::RelFill(mv, sec))
273 }
274 fn split_sec(&mut self, cell: Indeo3Cell) -> Box<Indeo3SecondaryTree> {
275 let (hdiff, vdiff) = self.calculate_diffs(cell);
276 if hdiff == 0 && vdiff == 0 {
277 if !cell.intra {
278 return Box::new(Indeo3SecondaryTree::VQNull(0));
279 } else {
280 return Box::new(Indeo3SecondaryTree::VQData(0));
281 }
282 }
283 if cell.get_width() > 16 && cell.get_height() > 16 {
284 let hsplit = vdiff > THRESHOLD && vdiff > hdiff * 2;
285 let vsplit = hdiff > THRESHOLD && hdiff > vdiff * 2;
286 match (vsplit, hsplit) {
287 (true, _) => {
288 let (cell1, cell2) = cell.split_v(self.stripw);
289 let tree1 = self.split_sec(cell1);
290 let tree2 = self.split_sec(cell2);
291 Box::new(Indeo3SecondaryTree::VSplit(tree1, tree2))
292 },
293 (_, true) => {
294 let (cell1, cell2) = cell.split_h();
295 let tree1 = self.split_sec(cell1);
296 let tree2 = self.split_sec(cell2);
297 Box::new(Indeo3SecondaryTree::HSplit(tree1, tree2))
298 },
299 _ => {
300 Box::new(Indeo3SecondaryTree::VQData(0))
301 },
302 }
303 } else {
304 let is_w8 = (cell.get_width() & 7) == 0;
305 let is_h8 = (cell.get_height() & 7) == 0;
306 let mode = match (hdiff > THRESHOLD, vdiff > THRESHOLD) {
307 (false, false) if is_w8 && is_h8 => 10,
308 (_, true) if is_h8 => 3,
309 _ => 0,
310 };
311 Box::new(Indeo3SecondaryTree::VQData(mode))
312 }
313 }
314 fn calculate_diffs(&self, cell: Indeo3Cell) -> (u32, u32) {
315 let offset = cell.get_x() + cell.get_y() * self.width;
316 let mut w = cell.get_width();
317 if cell.get_x() + w == self.width { w -= 1; }
318 let mut h = cell.get_height();
319 if cell.get_y() + h == self.height { h -= 1; }
320
321 let mut vdiff = 0;
322 let mut hdiff = 0;
323 let src0 = &self.data[offset..];
324 let src1 = &self.data[offset + self.width..];
325 for (line0, line1) in src0.chunks(self.width).zip(src1.chunks(self.width)).take(h) {
326 for ((&cur, &right), &bottom) in line0.iter().zip(line0[1..].iter()).zip(line1.iter()).take(w) {
327 hdiff += ssd(cur, right);
328 vdiff += ssd(cur, bottom);
329 }
330 }
331 let area = (w * h) as u32;
332 (hdiff * 16 / area, vdiff * 16 / area)
333 }
bafe9cd4 334 pub fn prune_extra_mvs(&mut self, tree: &mut Box<Indeo3PrimaryTree>) {
3ff976b2 335 let cell = Indeo3Cell::new(self.width, self.height);
bafe9cd4
KS
336 self.prune_pri(cell, tree)
337 }
338 fn prune_pri(&mut self, cell: Indeo3Cell, tree: &mut Box<Indeo3PrimaryTree>) {
339 match tree.deref_mut() {
340 Indeo3PrimaryTree::HSplit(ref mut tree1, ref mut tree2) => {
341 let (cell1, cell2) = cell.split_h();
342 self.prune_pri(cell1, tree1);
343 self.prune_pri(cell2, tree2);
344 },
345 Indeo3PrimaryTree::VSplit(ref mut tree1, ref mut tree2) => {
346 let (cell1, cell2) = cell.split_v(self.stripw);
347 self.prune_pri(cell1, tree1);
348 self.prune_pri(cell2, tree2);
349 },
350 Indeo3PrimaryTree::AbsFill(_) => {},
351 Indeo3PrimaryTree::RelFill(ref mv, ref _sec) => {
352 if find_mv(*mv, &self.mvs).is_none() {
353 let sec = self.split_sec(cell);
354 *tree = Box::new(Indeo3PrimaryTree::AbsFill(sec));
355 }
356 },
357 }
358 }
3ff976b2
KS
359 pub fn encode_tree(&mut self, iw: &mut Indeo3Writer, tree: &Indeo3PrimaryTree, cenc: &mut CellEncoder, refp: &Plane) {
360 let cell = Indeo3Cell::new(self.width, self.height);
77c25c7b
KS
361 self.encode_pri(iw, cell, tree, cenc, refp);
362 }
363 fn encode_pri(&mut self, iw: &mut Indeo3Writer, mut cell: Indeo3Cell, tree: &Indeo3PrimaryTree, cenc: &mut CellEncoder, refp: &Plane) {
364 match tree {
365 Indeo3PrimaryTree::HSplit(t1, t2) => {
366 iw.put_2bits(0);
367 let (cell1, cell2) = cell.split_h();
368 self.encode_pri(iw, cell1, t1, cenc, refp);
369 self.encode_pri(iw, cell2, t2, cenc, refp);
370 },
371 Indeo3PrimaryTree::VSplit(t1, t2) => {
372 iw.put_2bits(1);
373 let (cell1, cell2) = cell.split_v(self.stripw);
374 self.encode_pri(iw, cell1, t1, cenc, refp);
375 self.encode_pri(iw, cell2, t2, cenc, refp);
376 },
377 Indeo3PrimaryTree::AbsFill(sec) => {
378 iw.put_2bits(2);
379 cell.intra = true;
380 self.encode_sec(iw, cell, sec, cenc);
381 }
382 Indeo3PrimaryTree::RelFill(mv, sec) => {
383 if let Some(mv_idx) = find_mv(*mv, &self.mvs) {
384 iw.put_2bits(3);
385 iw.put_byte(mv_idx);
386 cell.intra = false;
387 let real_mv = self.mvs[usize::from(mv_idx)].0;
388 self.encode_sec_inter(iw, cell, sec, cenc, real_mv, refp);
389 } else {
390 iw.put_2bits(2);
391 cell.intra = true;
392 self.encode_sec(iw, cell, sec, cenc);
393 }
394 },
395 }
396 }
397 fn encode_sec(&mut self, iw: &mut Indeo3Writer, cell: Indeo3Cell, tree: &Indeo3SecondaryTree, cenc: &mut CellEncoder) {
398 match tree {
399 Indeo3SecondaryTree::HSplit(t1, t2) => {
400 iw.put_2bits(0);
401 let (cell1, cell2) = cell.split_h();
402 self.encode_sec(iw, cell1, t1, cenc);
403 self.encode_sec(iw, cell2, t2, cenc);
404 },
405 Indeo3SecondaryTree::VSplit(t1, t2) => {
406 iw.put_2bits(1);
407 let (cell1, cell2) = cell.split_v(self.stripw);
408 self.encode_sec(iw, cell1, t1, cenc);
409 self.encode_sec(iw, cell2, t2, cenc);
410 },
411 Indeo3SecondaryTree::VQNull(mode) => {
412 iw.put_2bits(2);
413 iw.put_2bits(*mode);
414 },
415 Indeo3SecondaryTree::VQData(mode) => {
416 iw.put_2bits(3);
417 self.encode_cell_data_intra(iw, cell, cenc, *mode);
418 },
419 }
420 }
421 fn encode_sec_inter(&mut self, iw: &mut Indeo3Writer, cell: Indeo3Cell, tree: &Indeo3SecondaryTree, cenc: &mut CellEncoder, mv: MV, refp: &Plane) {
422 match tree {
423 Indeo3SecondaryTree::HSplit(_t1, _t2) => {
424 unimplemented!();
425 },
426 Indeo3SecondaryTree::VSplit(_t1, _t2) => {
427 unimplemented!();
428 },
429 Indeo3SecondaryTree::VQNull(mode) => {
430 iw.put_2bits(2);
431 iw.put_2bits(*mode);
432 cenc.read_mv_buffer(refp, cell, mv);
433 cenc.null_mv();
434 cenc.put_buffer(self);
435 },
436 Indeo3SecondaryTree::VQData(_mode) => {
437 iw.put_2bits(3);
438 self.encode_cell_data_inter(iw, cell, cenc, mv, refp);
439 },
440 }
441 }
442 fn encode_cell_data_intra(&mut self, iw: &mut Indeo3Writer, cell: Indeo3Cell, cenc: &mut CellEncoder, mode: u8) {
443 cenc.read_buffer(self, cell);
444 cenc.gen_diffs_intra();
445 cenc.compress_intra(mode);
446 cenc.put_buffer(self);
447 for &b in cenc.out[..cenc.osize].iter() {
448 iw.put_byte(b);
449 }
450 }
451 fn encode_cell_data_inter(&mut self, iw: &mut Indeo3Writer, cell: Indeo3Cell, cenc: &mut CellEncoder, mv: MV, refp: &Plane) {
452 cenc.read_buffer(self, cell);
453 cenc.read_mv_buffer(refp, cell, mv);
454 cenc.gen_diffs_inter();
455 cenc.compress_inter();
456 cenc.put_buffer(self);
457 for &b in cenc.out[..cenc.osize].iter() {
458 iw.put_byte(b);
459 }
460 }
461}