2 * DSP functions for Indeo Video Interactive codecs (Indeo4 and Indeo5)
4 * Copyright (c) 2009-2011 Maxim Poliakovski
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 * DSP functions (inverse transforms, motion compensation, wavelet recompostions)
26 * for Indeo Video Interactive codecs.
31 #include "ivi_common.h"
34 void ff_ivi_recompose53(const IVIPlaneDesc *plane, uint8_t *dst,
38 int32_t p0, p1, p2, p3, tmp0, tmp1, tmp2;
39 int32_t b0_1, b0_2, b1_1, b1_2, b1_3, b2_1, b2_2, b2_3, b2_4, b2_5, b2_6;
40 int32_t b3_1, b3_2, b3_3, b3_4, b3_5, b3_6, b3_7, b3_8, b3_9;
41 int32_t pitch, back_pitch;
42 const short *b0_ptr, *b1_ptr, *b2_ptr, *b3_ptr;
43 const int num_bands = 4;
45 /* all bands should have the same pitch */
46 pitch = plane->bands[0].pitch;
48 /* pixels at the position "y-1" will be set to pixels at the "y" for the 1st iteration */
51 /* get pointers to the wavelet bands */
52 b0_ptr = plane->bands[0].buf;
53 b1_ptr = plane->bands[1].buf;
54 b2_ptr = plane->bands[2].buf;
55 b3_ptr = plane->bands[3].buf;
57 for (y = 0; y < plane->height; y += 2) {
59 if (y+2 >= plane->height)
61 /* load storage variables with values */
68 b1_1 = b1_ptr[back_pitch];
70 b1_3 = b1_1 - b1_2*6 + b1_ptr[pitch];
74 b2_2 = b2_ptr[0]; // b2[x, y ]
75 b2_3 = b2_2; // b2[x+1,y ] = b2[x,y]
76 b2_5 = b2_ptr[pitch]; // b2[x ,y+1]
77 b2_6 = b2_5; // b2[x+1,y+1] = b2[x,y+1]
81 b3_2 = b3_ptr[back_pitch]; // b3[x ,y-1]
82 b3_3 = b3_2; // b3[x+1,y-1] = b3[x ,y-1]
83 b3_5 = b3_ptr[0]; // b3[x ,y ]
84 b3_6 = b3_5; // b3[x+1,y ] = b3[x ,y ]
85 b3_8 = b3_2 - b3_5*6 + b3_ptr[pitch];
89 for (x = 0, indx = 0; x < plane->width; x+=2, indx++) {
90 if (x+2 >= plane->width) {
97 /* some values calculated in the previous iterations can */
98 /* be reused in the next ones, so do appropriate copying */
99 b2_1 = b2_2; // b2[x-1,y ] = b2[x, y ]
100 b2_2 = b2_3; // b2[x ,y ] = b2[x+1,y ]
101 b2_4 = b2_5; // b2[x-1,y+1] = b2[x ,y+1]
102 b2_5 = b2_6; // b2[x ,y+1] = b2[x+1,y+1]
103 b3_1 = b3_2; // b3[x-1,y-1] = b3[x ,y-1]
104 b3_2 = b3_3; // b3[x ,y-1] = b3[x+1,y-1]
105 b3_4 = b3_5; // b3[x-1,y ] = b3[x ,y ]
106 b3_5 = b3_6; // b3[x ,y ] = b3[x+1,y ]
107 b3_7 = b3_8; // vert_HPF(x-1)
108 b3_8 = b3_9; // vert_HPF(x )
110 p0 = p1 = p2 = p3 = 0;
112 /* process the LL-band by applying LPF both vertically and horizontally */
116 b0_1 = b0_ptr[indx+1];
117 b0_2 = b0_ptr[pitch+indx+1];
122 p2 = (tmp0 + tmp2) << 3;
123 p3 = (tmp1 + tmp2 + b0_2) << 2;
126 /* process the HL-band by applying HPF vertically and LPF horizontally */
130 b1_2 = b1_ptr[indx+1];
131 b1_1 = b1_ptr[back_pitch+indx+1];
133 tmp2 = tmp1 - tmp0*6 + b1_3;
134 b1_3 = b1_1 - b1_2*6 + b1_ptr[pitch+indx+1];
136 p0 += (tmp0 + tmp1) << 3;
137 p1 += (tmp0 + tmp1 + b1_1 + b1_2) << 2;
139 p3 += (tmp2 + b1_3) << 1;
142 /* process the LH-band by applying LPF vertically and HPF horizontally */
144 b2_3 = b2_ptr[indx+1];
145 b2_6 = b2_ptr[pitch+indx+1];
148 tmp1 = b2_1 - b2_2*6 + b2_3;
152 p2 += (tmp0 + b2_4 + b2_5) << 2;
153 p3 += (tmp1 + b2_4 - b2_5*6 + b2_6) << 1;
156 /* process the HH-band by applying HPF both vertically and horizontally */
158 b3_6 = b3_ptr[indx+1]; // b3[x+1,y ]
159 b3_3 = b3_ptr[back_pitch+indx+1]; // b3[x+1,y-1]
165 b3_9 = b3_3 - b3_6*6 + b3_ptr[pitch+indx+1];
167 p0 += (tmp0 + tmp1) << 2;
168 p1 += (tmp0 - tmp1*6 + tmp2) << 1;
169 p2 += (b3_7 + b3_8) << 1;
170 p3 += b3_7 - b3_8*6 + b3_9;
173 /* output four pixels */
174 dst[x] = av_clip_uint8((p0 >> 6) + 128);
175 dst[x+1] = av_clip_uint8((p1 >> 6) + 128);
176 dst[dst_pitch+x] = av_clip_uint8((p2 >> 6) + 128);
177 dst[dst_pitch+x+1] = av_clip_uint8((p3 >> 6) + 128);
180 dst += dst_pitch << 1;
191 void ff_ivi_recompose_haar(const IVIPlaneDesc *plane, uint8_t *dst,
194 int x, y, indx, b0, b1, b2, b3, p0, p1, p2, p3;
195 const short *b0_ptr, *b1_ptr, *b2_ptr, *b3_ptr;
198 /* all bands should have the same pitch */
199 pitch = plane->bands[0].pitch;
201 /* get pointers to the wavelet bands */
202 b0_ptr = plane->bands[0].buf;
203 b1_ptr = plane->bands[1].buf;
204 b2_ptr = plane->bands[2].buf;
205 b3_ptr = plane->bands[3].buf;
207 for (y = 0; y < plane->height; y += 2) {
208 for (x = 0, indx = 0; x < plane->width; x += 2, indx++) {
209 /* load coefficients */
210 b0 = b0_ptr[indx]; //should be: b0 = (num_bands > 0) ? b0_ptr[indx] : 0;
211 b1 = b1_ptr[indx]; //should be: b1 = (num_bands > 1) ? b1_ptr[indx] : 0;
212 b2 = b2_ptr[indx]; //should be: b2 = (num_bands > 2) ? b2_ptr[indx] : 0;
213 b3 = b3_ptr[indx]; //should be: b3 = (num_bands > 3) ? b3_ptr[indx] : 0;
215 /* haar wavelet recomposition */
216 p0 = (b0 + b1 + b2 + b3 + 2) >> 2;
217 p1 = (b0 + b1 - b2 - b3 + 2) >> 2;
218 p2 = (b0 - b1 + b2 - b3 + 2) >> 2;
219 p3 = (b0 - b1 - b2 + b3 + 2) >> 2;
221 /* bias, convert and output four pixels */
222 dst[x] = av_clip_uint8(p0 + 128);
223 dst[x + 1] = av_clip_uint8(p1 + 128);
224 dst[dst_pitch + x] = av_clip_uint8(p2 + 128);
225 dst[dst_pitch + x + 1] = av_clip_uint8(p3 + 128);
228 dst += dst_pitch << 1;
237 /** butterfly operation for the inverse Haar transform */
238 #define IVI_HAAR_BFLY(s1, s2, o1, o2, t) \
240 o1 = (s1 + s2) >> 1;\
243 /** inverse 8-point Haar transform */
244 #define INV_HAAR8(s1, s5, s3, s7, s2, s4, s6, s8,\
245 d1, d2, d3, d4, d5, d6, d7, d8,\
246 t0, t1, t2, t3, t4, t5, t6, t7, t8) {\
247 t1 = s1 << 1; t5 = s5 << 1;\
248 IVI_HAAR_BFLY(t1, t5, t1, t5, t0); IVI_HAAR_BFLY(t1, s3, t1, t3, t0);\
249 IVI_HAAR_BFLY(t5, s7, t5, t7, t0); IVI_HAAR_BFLY(t1, s2, t1, t2, t0);\
250 IVI_HAAR_BFLY(t3, s4, t3, t4, t0); IVI_HAAR_BFLY(t5, s6, t5, t6, t0);\
251 IVI_HAAR_BFLY(t7, s8, t7, t8, t0);\
252 d1 = COMPENSATE(t1);\
253 d2 = COMPENSATE(t2);\
254 d3 = COMPENSATE(t3);\
255 d4 = COMPENSATE(t4);\
256 d5 = COMPENSATE(t5);\
257 d6 = COMPENSATE(t6);\
258 d7 = COMPENSATE(t7);\
259 d8 = COMPENSATE(t8); }
261 /** inverse 4-point Haar transform */
262 #define INV_HAAR4(s1, s3, s5, s7) {\
263 HAAR_BFLY(s1, s5); HAAR_BFLY(s1, s3); HAAR_BFLY(s5, s7);\
264 s1 = COMPENSATE(s1);\
265 s3 = COMPENSATE(s3);\
266 s5 = COMPENSATE(s5);\
267 s7 = COMPENSATE(s7); }
269 void ff_ivi_inverse_haar_8x8(const int32_t *in, int16_t *out, uint32_t pitch,
270 const uint8_t *flags)
272 int i, shift, sp1, sp2, sp3, sp4;
276 int t0, t1, t2, t3, t4, t5, t6, t7, t8;
278 /* apply the InvHaar8 to all columns */
279 #define COMPENSATE(x) (x)
282 for (i = 0; i < 8; i++) {
286 sp1 = src[ 0] << shift;
287 sp2 = src[ 8] << shift;
288 sp3 = src[16] << shift;
289 sp4 = src[24] << shift;
290 INV_HAAR8( sp1, sp2, sp3, sp4,
291 src[32], src[40], src[48], src[56],
292 dst[ 0], dst[ 8], dst[16], dst[24],
293 dst[32], dst[40], dst[48], dst[56],
294 t0, t1, t2, t3, t4, t5, t6, t7, t8);
296 dst[ 0] = dst[ 8] = dst[16] = dst[24] =
297 dst[32] = dst[40] = dst[48] = dst[56] = 0;
304 /* apply the InvHaar8 to all rows */
305 #define COMPENSATE(x) (x)
307 for (i = 0; i < 8; i++) {
308 if ( !src[0] && !src[1] && !src[2] && !src[3]
309 && !src[4] && !src[5] && !src[6] && !src[7]) {
310 memset(out, 0, 8 * sizeof(out[0]));
312 INV_HAAR8(src[0], src[1], src[2], src[3],
313 src[4], src[5], src[6], src[7],
314 out[0], out[1], out[2], out[3],
315 out[4], out[5], out[6], out[7],
316 t0, t1, t2, t3, t4, t5, t6, t7, t8);
324 void ff_ivi_dc_haar_2d(const int32_t *in, int16_t *out, uint32_t pitch,
330 dc_coeff = (*in + 0) >> 3;
332 for (y = 0; y < blk_size; out += pitch, y++) {
333 for (x = 0; x < blk_size; x++)
338 /** butterfly operation for the inverse slant transform */
339 #define IVI_SLANT_BFLY(s1, s2, o1, o2, t) \
344 /** This is a reflection a,b = 1/2, 5/4 for the inverse slant transform */
345 #define IVI_IREFLECT(s1, s2, o1, o2, t) \
346 t = ((s1 + s2*2 + 2) >> 2) + s1;\
347 o2 = ((s1*2 - s2 + 2) >> 2) - s2;\
350 /** This is a reflection a,b = 1/2, 7/8 for the inverse slant transform */
351 #define IVI_SLANT_PART4(s1, s2, o1, o2, t) \
352 t = s2 + ((s1*4 - s2 + 4) >> 3);\
353 o2 = s1 + ((-s1 - s2*4 + 4) >> 3);\
356 /** inverse slant8 transform */
357 #define IVI_INV_SLANT8(s1, s4, s8, s5, s2, s6, s3, s7,\
358 d1, d2, d3, d4, d5, d6, d7, d8,\
359 t0, t1, t2, t3, t4, t5, t6, t7, t8) {\
360 IVI_SLANT_PART4(s4, s5, t4, t5, t0);\
362 IVI_SLANT_BFLY(s1, t5, t1, t5, t0); IVI_SLANT_BFLY(s2, s6, t2, t6, t0);\
363 IVI_SLANT_BFLY(s7, s3, t7, t3, t0); IVI_SLANT_BFLY(t4, s8, t4, t8, t0);\
365 IVI_SLANT_BFLY(t1, t2, t1, t2, t0); IVI_IREFLECT (t4, t3, t4, t3, t0);\
366 IVI_SLANT_BFLY(t5, t6, t5, t6, t0); IVI_IREFLECT (t8, t7, t8, t7, t0);\
367 IVI_SLANT_BFLY(t1, t4, t1, t4, t0); IVI_SLANT_BFLY(t2, t3, t2, t3, t0);\
368 IVI_SLANT_BFLY(t5, t8, t5, t8, t0); IVI_SLANT_BFLY(t6, t7, t6, t7, t0);\
369 d1 = COMPENSATE(t1);\
370 d2 = COMPENSATE(t2);\
371 d3 = COMPENSATE(t3);\
372 d4 = COMPENSATE(t4);\
373 d5 = COMPENSATE(t5);\
374 d6 = COMPENSATE(t6);\
375 d7 = COMPENSATE(t7);\
376 d8 = COMPENSATE(t8);}
378 /** inverse slant4 transform */
379 #define IVI_INV_SLANT4(s1, s4, s2, s3, d1, d2, d3, d4, t0, t1, t2, t3, t4) {\
380 IVI_SLANT_BFLY(s1, s2, t1, t2, t0); IVI_IREFLECT (s4, s3, t4, t3, t0);\
382 IVI_SLANT_BFLY(t1, t4, t1, t4, t0); IVI_SLANT_BFLY(t2, t3, t2, t3, t0);\
383 d1 = COMPENSATE(t1);\
384 d2 = COMPENSATE(t2);\
385 d3 = COMPENSATE(t3);\
386 d4 = COMPENSATE(t4);}
388 void ff_ivi_inverse_slant_8x8(const int32_t *in, int16_t *out, uint32_t pitch, const uint8_t *flags)
394 int t0, t1, t2, t3, t4, t5, t6, t7, t8;
396 #define COMPENSATE(x) (x)
399 for (i = 0; i < 8; i++) {
401 IVI_INV_SLANT8(src[0], src[8], src[16], src[24], src[32], src[40], src[48], src[56],
402 dst[0], dst[8], dst[16], dst[24], dst[32], dst[40], dst[48], dst[56],
403 t0, t1, t2, t3, t4, t5, t6, t7, t8);
405 dst[0] = dst[8] = dst[16] = dst[24] = dst[32] = dst[40] = dst[48] = dst[56] = 0;
412 #define COMPENSATE(x) ((x + 1)>>1)
414 for (i = 0; i < 8; i++) {
415 if (!src[0] && !src[1] && !src[2] && !src[3] && !src[4] && !src[5] && !src[6] && !src[7]) {
416 memset(out, 0, 8*sizeof(out[0]));
418 IVI_INV_SLANT8(src[0], src[1], src[2], src[3], src[4], src[5], src[6], src[7],
419 out[0], out[1], out[2], out[3], out[4], out[5], out[6], out[7],
420 t0, t1, t2, t3, t4, t5, t6, t7, t8);
428 void ff_ivi_inverse_slant_4x4(const int32_t *in, int16_t *out, uint32_t pitch, const uint8_t *flags)
434 int t0, t1, t2, t3, t4;
436 #define COMPENSATE(x) (x)
439 for (i = 0; i < 4; i++) {
441 IVI_INV_SLANT4(src[0], src[4], src[8], src[12],
442 dst[0], dst[4], dst[8], dst[12],
445 dst[0] = dst[4] = dst[8] = dst[12] = 0;
452 #define COMPENSATE(x) ((x + 1)>>1)
454 for (i = 0; i < 4; i++) {
455 if (!src[0] && !src[1] && !src[2] && !src[3]) {
456 out[0] = out[1] = out[2] = out[3] = 0;
458 IVI_INV_SLANT4(src[0], src[1], src[2], src[3],
459 out[0], out[1], out[2], out[3],
468 void ff_ivi_dc_slant_2d(const int32_t *in, int16_t *out, uint32_t pitch, int blk_size)
473 dc_coeff = (*in + 1) >> 1;
475 for (y = 0; y < blk_size; out += pitch, y++) {
476 for (x = 0; x < blk_size; x++)
481 void ff_ivi_row_slant8(const int32_t *in, int16_t *out, uint32_t pitch, const uint8_t *flags)
484 int t0, t1, t2, t3, t4, t5, t6, t7, t8;
486 #define COMPENSATE(x) ((x + 1)>>1)
487 for (i = 0; i < 8; i++) {
488 if (!in[0] && !in[1] && !in[2] && !in[3] && !in[4] && !in[5] && !in[6] && !in[7]) {
489 memset(out, 0, 8*sizeof(out[0]));
491 IVI_INV_SLANT8( in[0], in[1], in[2], in[3], in[4], in[5], in[6], in[7],
492 out[0], out[1], out[2], out[3], out[4], out[5], out[6], out[7],
493 t0, t1, t2, t3, t4, t5, t6, t7, t8);
501 void ff_ivi_dc_row_slant(const int32_t *in, int16_t *out, uint32_t pitch, int blk_size)
506 dc_coeff = (*in + 1) >> 1;
508 for (x = 0; x < blk_size; x++)
513 for (y = 1; y < blk_size; out += pitch, y++) {
514 for (x = 0; x < blk_size; x++)
519 void ff_ivi_col_slant8(const int32_t *in, int16_t *out, uint32_t pitch, const uint8_t *flags)
521 int i, row2, row4, row8;
522 int t0, t1, t2, t3, t4, t5, t6, t7, t8;
528 #define COMPENSATE(x) ((x + 1)>>1)
529 for (i = 0; i < 8; i++) {
531 IVI_INV_SLANT8(in[0], in[8], in[16], in[24], in[32], in[40], in[48], in[56],
532 out[0], out[pitch], out[row2], out[row2 + pitch], out[row4],
533 out[row4 + pitch], out[row4 + row2], out[row8 - pitch],
534 t0, t1, t2, t3, t4, t5, t6, t7, t8);
536 out[0] = out[pitch] = out[row2] = out[row2 + pitch] = out[row4] =
537 out[row4 + pitch] = out[row4 + row2] = out[row8 - pitch] = 0;
546 void ff_ivi_dc_col_slant(const int32_t *in, int16_t *out, uint32_t pitch, int blk_size)
551 dc_coeff = (*in + 1) >> 1;
553 for (y = 0; y < blk_size; out += pitch, y++) {
555 for (x = 1; x < blk_size; x++)
560 void ff_ivi_put_pixels_8x8(const int32_t *in, int16_t *out, uint32_t pitch,
561 const uint8_t *flags)
565 for (y = 0; y < 8; out += pitch, in += 8, y++)
566 for (x = 0; x < 8; x++)
570 void ff_ivi_put_dc_pixel_8x8(const int32_t *in, int16_t *out, uint32_t pitch,
576 memset(out + 1, 0, 7*sizeof(out[0]));
579 for (y = 1; y < 8; out += pitch, y++)
580 memset(out, 0, 8*sizeof(out[0]));
583 #define IVI_MC_TEMPLATE(size, suffix, OP) \
584 void ff_ivi_mc_ ## size ##x## size ## suffix (int16_t *buf, const int16_t *ref_buf, \
585 uint32_t pitch, int mc_type) \
588 const int16_t *wptr; \
591 case 0: /* fullpel (no interpolation) */ \
592 for (i = 0; i < size; i++, buf += pitch, ref_buf += pitch) { \
593 for (j = 0; j < size; j++) {\
594 OP(buf[j], ref_buf[j]); \
598 case 1: /* horizontal halfpel interpolation */ \
599 for (i = 0; i < size; i++, buf += pitch, ref_buf += pitch) \
600 for (j = 0; j < size; j++) \
601 OP(buf[j], (ref_buf[j] + ref_buf[j+1]) >> 1); \
603 case 2: /* vertical halfpel interpolation */ \
604 wptr = ref_buf + pitch; \
605 for (i = 0; i < size; i++, buf += pitch, wptr += pitch, ref_buf += pitch) \
606 for (j = 0; j < size; j++) \
607 OP(buf[j], (ref_buf[j] + wptr[j]) >> 1); \
609 case 3: /* vertical and horizontal halfpel interpolation */ \
610 wptr = ref_buf + pitch; \
611 for (i = 0; i < size; i++, buf += pitch, wptr += pitch, ref_buf += pitch) \
612 for (j = 0; j < size; j++) \
613 OP(buf[j], (ref_buf[j] + ref_buf[j+1] + wptr[j] + wptr[j+1]) >> 2); \
618 #define OP_PUT(a, b) (a) = (b)
619 #define OP_ADD(a, b) (a) += (b)
621 IVI_MC_TEMPLATE(8, _no_delta, OP_PUT)
622 IVI_MC_TEMPLATE(8, _delta, OP_ADD)
623 IVI_MC_TEMPLATE(4, _no_delta, OP_PUT)
624 IVI_MC_TEMPLATE(4, _delta, OP_ADD)