1 use super::ivi::{IVITransformType,TDir,TrFunc,TrFuncDC};
4 fn hbutterfly(a: i32, b: i32) -> (i32, i32) {
5 ((a + b) >> 1, (a - b) >> 1)
8 fn butterfly(a: i32, b: i32) -> (i32, i32) {
12 fn ireflect(a: i32, b: i32) -> (i32, i32) {
13 (((b * 2 - a + 2) >> 2) - a, ((b + 2 * a + 2) >> 2) + b)
16 macro_rules! haar_transform {
17 ($c0:expr, $c1:expr, $c2:expr, $c3:expr) => {{
18 let (t0, t1) = hbutterfly($c0, $c1);
19 let (t2, t3) = hbutterfly(t0, $c2);
22 let (t4, t5) = hbutterfly(t1, $c3);
26 ($c0:expr, $c1:expr, $c2:expr, $c3:expr, $c4:expr, $c5:expr, $c6:expr, $c7:expr) => {{
27 let (a0, a1) = hbutterfly($c0 << 1, $c1 << 1);
29 let (t0, t1) = hbutterfly(a0, $c2);
30 let (t2, t3) = hbutterfly(a1, $c3);
31 let (u0, u1) = hbutterfly(t0, $c4);
32 let (u2, u3) = hbutterfly(t1, $c5);
33 let (u4, u5) = hbutterfly(t2, $c6);
34 let (u6, u7) = hbutterfly(t3, $c7);
46 macro_rules! slant_transform {
47 ($c0:expr, $c1:expr, $c2:expr, $c3:expr, $output:ident) => {{
48 let (t0, t1) = butterfly($c0, $c2);
49 let (t2, t3) = ireflect ($c3, $c1);
50 let (t4, t5) = butterfly(t0, t3);
51 let (t6, t7) = butterfly(t1, t2);
57 ($c0:expr, $c1:expr, $c2:expr, $c3:expr, $c4:expr, $c5:expr, $c6:expr, $c7:expr, $output:ident) => {{
58 let t0 = $c3 + (($c1 * 4 - $c3 + 4) >> 3);
59 let t1 = $c1 + ((-$c1 - $c3 * 4 + 4) >> 3);
61 let (t2, t3) = butterfly($c0, t1);
62 let (t4, t5) = butterfly($c4, $c5);
63 let (t6, t7) = butterfly($c7, $c6);
64 let (t8, t9) = butterfly(t0, $c2);
66 let (u0, u1) = butterfly(t2, t4);
67 let (u2, u3) = ireflect (t7, t8);
68 let (u4, u5) = butterfly(t3, t5);
69 let (u6, u7) = ireflect (t6, t9);
71 let (t0, t1) = butterfly(u0, u3);
72 let (t2, t3) = butterfly(u1, u2);
73 let (t4, t5) = butterfly(u4, u7);
74 let (t6, t7) = butterfly(u5, u6);
87 fn haar8x8_2d(blk: &mut[i32; 64]) {
89 let mut c0 = blk[i + 0*8] << 1;
90 let mut c1 = blk[i + 1*8] << 1;
91 let mut c2 = blk[i + 2*8] << 1;
92 let mut c3 = blk[i + 3*8] << 1;
93 haar_transform!(c0, c1, c2, c3,
94 blk[i + 4*8], blk[i + 5*8], blk[i + 6*8], blk[i + 7*8]);
101 haar_transform!(blk[i + 0*8], blk[i + 1*8], blk[i + 2*8], blk[i + 3*8],
102 blk[i + 4*8], blk[i + 5*8], blk[i + 6*8], blk[i + 7*8]);
105 let row = &mut blk[i*8..(i+1)*8];
106 haar_transform!(row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7]);
109 fn haar8x8_row(blk: &mut[i32; 64]) {
111 let row = &mut blk[i*8..(i+1)*8];
112 haar_transform!(row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7]);
115 fn haar8x8_col(blk: &mut[i32; 64]) {
117 haar_transform!(blk[i + 0*8], blk[i + 1*8], blk[i + 2*8], blk[i + 3*8],
118 blk[i + 4*8], blk[i + 5*8], blk[i + 6*8], blk[i + 7*8]);
121 fn haar8x8_dc(blk: &mut[i32; 64], in0: i32) {
123 for i in 0..64 { blk[i] = dc; }
126 fn haar4x4_2d(blk: &mut[i32; 64]) {
128 let mut c0 = blk[i + 0*4] << 1;
129 let mut c1 = blk[i + 1*4] << 1;
130 haar_transform!(c0, c1, blk[i + 2*4], blk[i + 3*4]);
135 haar_transform!(blk[i + 0*4], blk[i + 1*4], blk[i + 2*4], blk[i + 3*4]);
138 let row = &mut blk[i*4..(i+1)*4];
139 haar_transform!(row[0], row[1], row[2], row[3]);
142 fn haar4x4_row(blk: &mut[i32; 64]) {
144 let row = &mut blk[i*4..(i+1)*4];
145 haar_transform!(row[0], row[1], row[2], row[3]);
148 fn haar4x4_col(blk: &mut[i32; 64]) {
150 haar_transform!(blk[i + 0*4], blk[i + 1*4], blk[i + 2*4], blk[i + 3*4]);
153 fn haar4x4_dc(blk: &mut[i32; 64], in0: i32) {
155 for i in 0..16 { blk[i] = dc; }
158 fn slant8x8_2d(blk: &mut[i32; 64]) {
159 let pass1 = |x: i32| x;
160 let pass2 = |x: i32| (x + 1) >> 1;
164 for j in 0..8 { s0 |= blk[i + j*8]; }
165 if s0 == 0 { continue; }
167 slant_transform!(blk[i + 0*8], blk[i + 1*8], blk[i + 2*8], blk[i + 3*8],
168 blk[i + 4*8], blk[i + 5*8], blk[i + 6*8], blk[i + 7*8], pass1);
171 let row = &mut blk[i*8..(i+1)*8];
172 slant_transform!(row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], pass2);
175 fn slant8x8_2d_dc(blk: &mut[i32; 64], in0: i32) {
176 let dc = (in0 + 1) >> 1;
177 for i in 0..64 { blk[i] = dc; }
179 fn slant8x8_row(blk: &mut[i32; 64]) {
180 let pass = |x: i32| (x + 1) >> 1;
183 let row = &mut blk[i*8..(i+1)*8];
184 slant_transform!(row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], pass);
187 fn slant8x8_row_dc(blk: &mut[i32; 64], in0: i32) {
188 let dc = (in0 + 1) >> 1;
190 for i in 0..8 { blk[i] = dc; }
191 for i in 8..64 { blk[i] = 0; }
193 fn slant8x8_col(blk: &mut[i32; 64]) {
194 let pass = |x: i32| (x + 1) >> 1;
197 slant_transform!(blk[i + 0*8], blk[i + 1*8], blk[i + 2*8], blk[i + 3*8],
198 blk[i + 4*8], blk[i + 5*8], blk[i + 6*8], blk[i + 7*8], pass);
201 fn slant8x8_col_dc(blk: &mut[i32; 64], in0: i32) {
202 let dc = (in0 + 1) >> 1;
206 for j in 1..8 { blk[i * 8 + j] = 0; }
210 fn slant4x4_2d(blk: &mut[i32; 64]) {
211 let pass1 = |x: i32| x;
212 let pass2 = |x: i32| (x + 1) >> 1;
215 slant_transform!(blk[i + 0*4], blk[i + 1*4], blk[i + 2*4], blk[i + 3*4], pass1);
218 let row = &mut blk[i*4..(i+1)*4];
219 slant_transform!(row[0], row[1], row[2], row[3], pass2);
222 fn slant4x4_2d_dc(blk: &mut[i32; 64], in0: i32) {
223 let dc = (in0 + 1) >> 1;
224 for i in 0..16 { blk[i] = dc; }
226 fn slant4x4_row(blk: &mut[i32; 64]) {
227 let pass = |x: i32| (x + 1) >> 1;
230 let row = &mut blk[i*4..(i+1)*4];
231 slant_transform!(row[0], row[1], row[2], row[3], pass);
234 fn slant4x4_row_dc(blk: &mut[i32; 64], in0: i32) {
235 let dc = (in0 + 1) >> 1;
237 for i in 0..4 { blk[i] = dc; }
238 for i in 4..16 { blk[i] = 0; }
240 fn slant4x4_col(blk: &mut[i32; 64]) {
241 let pass = |x: i32| (x + 1) >> 1;
244 slant_transform!(blk[i + 0*4], blk[i + 1*4], blk[i + 2*4], blk[i + 3*4], pass);
247 fn slant4x4_col_dc(blk: &mut[i32; 64], in0: i32) {
248 let dc = (in0 + 1) >> 1;
252 for j in 1..4 { blk[i * 4 + j] = 0; }
256 #[allow(unused_variables)]
257 fn none8x8(blk: &mut[i32; 64]) {
259 fn none8x8_dc(blk: &mut[i32; 64], dc: i32) {
260 for i in 1..8 { blk[i] = dc; }
261 for i in 8..64 { blk[i] = 0; }
263 #[allow(unused_variables)]
264 fn none4x4(blk: &mut[i32; 64]) {
266 fn none4x4_dc(blk: &mut[i32; 64], dc: i32) {
267 for i in 1..4 { blk[i] = dc; }
268 for i in 4..16 { blk[i] = 0; }
271 pub fn ivi_get_transform8x8_funcs(ttype: IVITransformType) -> (TrFunc, TrFuncDC) {
273 IVITransformType::Haar(_, ref dir) => {
275 TDir::TwoD => { (haar8x8_2d, haar8x8_dc) },
276 TDir::Row => { (haar8x8_row, haar8x8_dc) },
277 TDir::Col => { (haar8x8_col, haar8x8_dc) },
279 IVITransformType::Slant(_, ref dir) => {
281 TDir::TwoD => { (slant8x8_2d, slant8x8_2d_dc) },
282 TDir::Row => { (slant8x8_row, slant8x8_row_dc) },
283 TDir::Col => { (slant8x8_col, slant8x8_col_dc) },
285 IVITransformType::DCT(_, _) => { unimplemented!() },
286 IVITransformType::None(_) => { (none8x8, none8x8_dc) }
289 pub fn ivi_get_transform4x4_funcs(ttype: IVITransformType) -> (TrFunc, TrFuncDC) {
291 IVITransformType::Haar(_, ref dir) => {
293 TDir::TwoD => { (haar4x4_2d, haar4x4_dc) },
294 TDir::Row => { (haar4x4_row, haar4x4_dc) },
295 TDir::Col => { (haar4x4_col, haar4x4_dc) },
297 IVITransformType::Slant(_, ref dir) => {
299 TDir::TwoD => { (slant4x4_2d, slant4x4_2d_dc) },
300 TDir::Row => { (slant4x4_row, slant4x4_row_dc) },
301 TDir::Col => { (slant4x4_col, slant4x4_col_dc) },
303 IVITransformType::DCT(_, _) => { unimplemented!() },
304 IVITransformType::None(_) => { (none4x4, none4x4_dc) }
308 pub fn ivi_mc_put(dst: &mut [i16], dstride: usize, src: &[i16], sstride: usize, mode: u8, w: usize, h: usize) {
311 if src.len() < w + h * sstride { return; }
315 let dest = &mut dst[didx..didx+w];
316 dest.copy_from_slice(&src[sidx..sidx+w]);
324 let val = (src[sidx + x] + src[sidx + x + 1]) >> 1;
331 let mut sptr = src.as_ptr();
332 let mut dptr = dst.as_mut_ptr();
334 let mut last = *sptr;
336 let nv = *sptr.add(x + 1);
337 *dptr.add(x) = nv.wrapping_add(last) >> 1;
340 sptr = sptr.add(sstride);
341 dptr = dptr.add(dstride);
348 let val = (src[sidx + x] + src[sidx + x + sstride]) >> 1;
355 let mut sptr0 = src.as_ptr();
356 let mut sptr1 = sptr0.add(sstride);
357 let mut dptr = dst.as_mut_ptr();
360 let a = *sptr0.add(x);
361 let b = *sptr1.add(x);
362 *dptr.add(x) = a.wrapping_add(b) >> 1;
364 sptr0 = sptr0.add(sstride);
365 sptr1 = sptr1.add(sstride);
366 dptr = dptr.add(sstride);
373 let val = (src[sidx + x + 0] + src[sidx + x + sstride + 0] +
374 src[sidx + x + 1] + src[sidx + x + sstride + 1]) >> 2;
381 let mut sptr0 = src.as_ptr();
382 let mut sptr1 = sptr0.add(sstride);
383 let mut dptr = dst.as_mut_ptr();
388 let a = *sptr0.add(x + 1);
389 let b = *sptr1.add(x + 1);
390 let aas = a.wrapping_add(la);
391 let bbs = b.wrapping_add(lb);
392 *dptr.add(x) = aas.wrapping_add(bbs) >> 2;
396 sptr0 = sptr0.add(sstride);
397 sptr1 = sptr1.add(sstride);
398 dptr = dptr.add(dstride);
405 fn ivi_mc_add(dst: &mut [i16], dstride: usize, src: &[i16], sstride: usize, mode: u8, w: usize, h: usize) {
412 dst[didx + x] += src[sidx + x];
421 let val = (src[sidx + x] + src[sidx + x + 1]) >> 1;
422 dst[didx + x] += val;
431 let val = (src[sidx + x] + src[sidx + x + sstride]) >> 1;
432 dst[didx + x] += val;
441 let val = (src[sidx + x + 0] + src[sidx + x + sstride + 0] +
442 src[sidx + x + 1] + src[sidx + x + sstride + 1]) >> 2;
443 dst[didx + x] += val;
452 pub fn ivi_mc_avg(dst: &mut [i16], dstride: usize,
453 src1: &[i16], sstride1: usize, mode1: u8,
454 src2: &[i16], sstride2: usize, mode2: u8,
455 w: usize, h: usize) {
459 let mut tmp: [i16; 64] = [0; 64];
460 ivi_mc_add(&mut tmp, tstride, src1, sstride1, mode1, w, h);
461 ivi_mc_add(&mut tmp, tstride, src2, sstride2, mode2, w, h);
463 for x in 0..w { dst[didx + x] = tmp[tidx + x] >> 1; }