]> git.sesse.net Git - ffmpeg/blob - libavcodec/ivi_dsp.c
Merge commit '1961e46c15c23a041f8d8614a25388a3ee9eff63'
[ffmpeg] / libavcodec / ivi_dsp.c
1 /*
2  * DSP functions for Indeo Video Interactive codecs (Indeo4 and Indeo5)
3  *
4  * Copyright (c) 2009-2011 Maxim Poliakovski
5  *
6  * This file is part of FFmpeg.
7  *
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.
12  *
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.
17  *
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
21  */
22
23 /**
24  * @file
25  * DSP functions (inverse transforms, motion compensation, wavelet recompostions)
26  * for Indeo Video Interactive codecs.
27  */
28
29 #include "avcodec.h"
30 #include "dsputil.h"
31 #include "dwt.h"
32 #include "ivi_common.h"
33 #include "ivi_dsp.h"
34
35 void ff_ivi_recompose53(const IVIPlaneDesc *plane, uint8_t *dst,
36                         const int dst_pitch, const int num_bands)
37 {
38     int             x, y, indx;
39     int32_t         p0, p1, p2, p3, tmp0, tmp1, tmp2;
40     int32_t         b0_1, b0_2, b1_1, b1_2, b1_3, b2_1, b2_2, b2_3, b2_4, b2_5, b2_6;
41     int32_t         b3_1, b3_2, b3_3, b3_4, b3_5, b3_6, b3_7, b3_8, b3_9;
42     int32_t         pitch, back_pitch;
43     const IDWTELEM *b0_ptr, *b1_ptr, *b2_ptr, *b3_ptr;
44
45     /* all bands should have the same pitch */
46     pitch = plane->bands[0].pitch;
47
48     /* pixels at the position "y-1" will be set to pixels at the "y" for the 1st iteration */
49     back_pitch = 0;
50
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;
56
57     for (y = 0; y < plane->height; y += 2) {
58
59         if (y+2 >= plane->height)
60             pitch= 0;
61         /* load storage variables with values */
62         if (num_bands > 0) {
63             b0_1 = b0_ptr[0];
64             b0_2 = b0_ptr[pitch];
65         }
66
67         if (num_bands > 1) {
68             b1_1 = b1_ptr[back_pitch];
69             b1_2 = b1_ptr[0];
70             b1_3 = b1_1 - b1_2*6 + b1_ptr[pitch];
71         }
72
73         if (num_bands > 2) {
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]
78         }
79
80         if (num_bands > 3) {
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];
86             b3_9 = b3_8;
87         }
88
89         for (x = 0, indx = 0; x < plane->width; x+=2, indx++) {
90             if (x+2 >= plane->width) {
91                 b0_ptr --;
92                 b1_ptr --;
93                 b2_ptr --;
94                 b3_ptr --;
95             }
96
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  )
109
110             p0 = p1 = p2 = p3 = 0;
111
112             /* process the LL-band by applying LPF both vertically and horizontally */
113             if (num_bands > 0) {
114                 tmp0 = b0_1;
115                 tmp2 = b0_2;
116                 b0_1 = b0_ptr[indx+1];
117                 b0_2 = b0_ptr[pitch+indx+1];
118                 tmp1 = tmp0 + b0_1;
119
120                 p0 =  tmp0 << 4;
121                 p1 =  tmp1 << 3;
122                 p2 = (tmp0 + tmp2) << 3;
123                 p3 = (tmp1 + tmp2 + b0_2) << 2;
124             }
125
126             /* process the HL-band by applying HPF vertically and LPF horizontally */
127             if (num_bands > 1) {
128                 tmp0 = b1_2;
129                 tmp1 = b1_1;
130                 b1_2 = b1_ptr[indx+1];
131                 b1_1 = b1_ptr[back_pitch+indx+1];
132
133                 tmp2 = tmp1 - tmp0*6 + b1_3;
134                 b1_3 = b1_1 - b1_2*6 + b1_ptr[pitch+indx+1];
135
136                 p0 += (tmp0 + tmp1) << 3;
137                 p1 += (tmp0 + tmp1 + b1_1 + b1_2) << 2;
138                 p2 +=  tmp2 << 2;
139                 p3 += (tmp2 + b1_3) << 1;
140             }
141
142             /* process the LH-band by applying LPF vertically and HPF horizontally */
143             if (num_bands > 2) {
144                 b2_3 = b2_ptr[indx+1];
145                 b2_6 = b2_ptr[pitch+indx+1];
146
147                 tmp0 = b2_1 + b2_2;
148                 tmp1 = b2_1 - b2_2*6 + b2_3;
149
150                 p0 += tmp0 << 3;
151                 p1 += tmp1 << 2;
152                 p2 += (tmp0 + b2_4 + b2_5) << 2;
153                 p3 += (tmp1 + b2_4 - b2_5*6 + b2_6) << 1;
154             }
155
156             /* process the HH-band by applying HPF both vertically and horizontally */
157             if (num_bands > 3) {
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]
160
161                 tmp0 = b3_1 + b3_4;
162                 tmp1 = b3_2 + b3_5;
163                 tmp2 = b3_3 + b3_6;
164
165                 b3_9 = b3_3 - b3_6*6 + b3_ptr[pitch+indx+1];
166
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;
171             }
172
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);
178         }// for x
179
180         dst += dst_pitch << 1;
181
182         back_pitch = -pitch;
183
184         b0_ptr += pitch + 1;
185         b1_ptr += pitch + 1;
186         b2_ptr += pitch + 1;
187         b3_ptr += pitch + 1;
188     }
189 }
190
191 void ff_ivi_recompose_haar(const IVIPlaneDesc *plane, uint8_t *dst,
192                            const int dst_pitch, const int num_bands)
193 {
194     int             x, y, indx, b0, b1, b2, b3, p0, p1, p2, p3;
195     const IDWTELEM *b0_ptr, *b1_ptr, *b2_ptr, *b3_ptr;
196     int32_t         pitch;
197
198     /* all bands should have the same pitch */
199     pitch = plane->bands[0].pitch;
200
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;
206
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;
214
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;
220
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);
226         }// for x
227
228         dst += dst_pitch << 1;
229
230         b0_ptr += pitch;
231         b1_ptr += pitch;
232         b2_ptr += pitch;
233         b3_ptr += pitch;
234     }// for y
235 }
236
237 /** butterfly operation for the inverse Haar transform */
238 #define IVI_HAAR_BFLY(s1, s2, o1, o2, t) \
239     t  = (s1 - s2) >> 1;\
240     o1 = (s1 + s2) >> 1;\
241     o2 = t;\
242
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); }
260
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); }
268
269 void ff_ivi_inverse_haar_8x8(const int32_t *in, int16_t *out, uint32_t pitch,
270                              const uint8_t *flags)
271 {
272     int     i, shift, sp1, sp2, sp3, sp4;
273     const int32_t *src;
274     int32_t *dst;
275     int     tmp[64];
276     int     t0, t1, t2, t3, t4, t5, t6, t7, t8;
277
278     /* apply the InvHaar8 to all columns */
279 #define COMPENSATE(x) (x)
280     src = in;
281     dst = tmp;
282     for (i = 0; i < 8; i++) {
283         if (flags[i]) {
284             /* pre-scaling */
285             shift = !(i & 4);
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);
295         } else
296             dst[ 0] = dst[ 8] = dst[16] = dst[24] =
297             dst[32] = dst[40] = dst[48] = dst[56] = 0;
298
299         src++;
300         dst++;
301     }
302 #undef  COMPENSATE
303
304     /* apply the InvHaar8 to all rows */
305 #define COMPENSATE(x) (x)
306     src = tmp;
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]));
311         } else {
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);
317         }
318         src += 8;
319         out += pitch;
320     }
321 #undef  COMPENSATE
322 }
323
324 void ff_ivi_dc_haar_2d(const int32_t *in, int16_t *out, uint32_t pitch,
325                        int blk_size)
326 {
327     int     x, y;
328     int16_t dc_coeff;
329
330     dc_coeff = (*in + 0) >> 3;
331
332     for (y = 0; y < blk_size; out += pitch, y++) {
333         for (x = 0; x < blk_size; x++)
334             out[x] = dc_coeff;
335     }
336 }
337
338 /** butterfly operation for the inverse slant transform */
339 #define IVI_SLANT_BFLY(s1, s2, o1, o2, t) \
340     t  = s1 - s2;\
341     o1 = s1 + s2;\
342     o2 = t;\
343
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;\
348     o1 = t;\
349
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);\
354     o1 = t;\
355
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);\
361 \
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);\
364 \
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);}
377
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);\
381 \
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);}
387
388 void ff_ivi_inverse_slant_8x8(const int32_t *in, int16_t *out, uint32_t pitch, const uint8_t *flags)
389 {
390     int     i;
391     const int32_t *src;
392     int32_t *dst;
393     int     tmp[64];
394     int     t0, t1, t2, t3, t4, t5, t6, t7, t8;
395
396 #define COMPENSATE(x) (x)
397     src = in;
398     dst = tmp;
399     for (i = 0; i < 8; i++) {
400         if (flags[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);
404         } else
405             dst[0] = dst[8] = dst[16] = dst[24] = dst[32] = dst[40] = dst[48] = dst[56] = 0;
406
407             src++;
408             dst++;
409     }
410 #undef COMPENSATE
411
412 #define COMPENSATE(x) ((x + 1)>>1)
413     src = tmp;
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]));
417         } else {
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);
421         }
422         src += 8;
423         out += pitch;
424     }
425 #undef COMPENSATE
426 }
427
428 void ff_ivi_inverse_slant_4x4(const int32_t *in, int16_t *out, uint32_t pitch, const uint8_t *flags)
429 {
430     int     i;
431     const int32_t *src;
432     int32_t *dst;
433     int     tmp[16];
434     int     t0, t1, t2, t3, t4;
435
436 #define COMPENSATE(x) (x)
437     src = in;
438     dst = tmp;
439     for (i = 0; i < 4; i++) {
440         if (flags[i]) {
441             IVI_INV_SLANT4(src[0], src[4], src[8], src[12],
442                            dst[0], dst[4], dst[8], dst[12],
443                            t0, t1, t2, t3, t4);
444         } else
445             dst[0] = dst[4] = dst[8] = dst[12] = 0;
446
447             src++;
448             dst++;
449     }
450 #undef COMPENSATE
451
452 #define COMPENSATE(x) ((x + 1)>>1)
453     src = tmp;
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;
457         } else {
458             IVI_INV_SLANT4(src[0], src[1], src[2], src[3],
459                            out[0], out[1], out[2], out[3],
460                            t0, t1, t2, t3, t4);
461         }
462         src += 4;
463         out += pitch;
464     }
465 #undef COMPENSATE
466 }
467
468 void ff_ivi_dc_slant_2d(const int32_t *in, int16_t *out, uint32_t pitch, int blk_size)
469 {
470     int     x, y;
471     int16_t dc_coeff;
472
473     dc_coeff = (*in + 1) >> 1;
474
475     for (y = 0; y < blk_size; out += pitch, y++) {
476         for (x = 0; x < blk_size; x++)
477             out[x] = dc_coeff;
478     }
479 }
480
481 void ff_ivi_row_slant8(const int32_t *in, int16_t *out, uint32_t pitch, const uint8_t *flags)
482 {
483     int     i;
484     int     t0, t1, t2, t3, t4, t5, t6, t7, t8;
485
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]));
490         } else {
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);
494         }
495         in += 8;
496         out += pitch;
497     }
498 #undef COMPENSATE
499 }
500
501 void ff_ivi_dc_row_slant(const int32_t *in, int16_t *out, uint32_t pitch, int blk_size)
502 {
503     int     x, y;
504     int16_t dc_coeff;
505
506     dc_coeff = (*in + 1) >> 1;
507
508     for (x = 0; x < blk_size; x++)
509         out[x] = dc_coeff;
510
511     out += pitch;
512
513     for (y = 1; y < blk_size; out += pitch, y++) {
514         for (x = 0; x < blk_size; x++)
515             out[x] = 0;
516     }
517 }
518
519 void ff_ivi_col_slant8(const int32_t *in, int16_t *out, uint32_t pitch, const uint8_t *flags)
520 {
521     int     i, row2, row4, row8;
522     int     t0, t1, t2, t3, t4, t5, t6, t7, t8;
523
524     row2 = pitch << 1;
525     row4 = pitch << 2;
526     row8 = pitch << 3;
527
528 #define COMPENSATE(x) ((x + 1)>>1)
529     for (i = 0; i < 8; i++) {
530         if (flags[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);
535         } else {
536             out[0] = out[pitch] = out[row2] = out[row2 + pitch] = out[row4] =
537             out[row4 + pitch] =  out[row4 + row2] = out[row8 - pitch] = 0;
538         }
539
540         in++;
541         out++;
542     }
543 #undef COMPENSATE
544 }
545
546 void ff_ivi_dc_col_slant(const int32_t *in, int16_t *out, uint32_t pitch, int blk_size)
547 {
548     int     x, y;
549     int16_t dc_coeff;
550
551     dc_coeff = (*in + 1) >> 1;
552
553     for (y = 0; y < blk_size; out += pitch, y++) {
554         out[0] = dc_coeff;
555         for (x = 1; x < blk_size; x++)
556             out[x] = 0;
557     }
558 }
559
560 void ff_ivi_put_pixels_8x8(const int32_t *in, int16_t *out, uint32_t pitch,
561                            const uint8_t *flags)
562 {
563     int     x, y;
564
565     for (y = 0; y < 8; out += pitch, in += 8, y++)
566         for (x = 0; x < 8; x++)
567             out[x] = in[x];
568 }
569
570 void ff_ivi_put_dc_pixel_8x8(const int32_t *in, int16_t *out, uint32_t pitch,
571                              int blk_size)
572 {
573     int     y;
574
575     out[0] = in[0];
576     memset(out + 1, 0, 7*sizeof(out[0]));
577     out += pitch;
578
579     for (y = 1; y < 8; out += pitch, y++)
580         memset(out, 0, 8*sizeof(out[0]));
581 }
582
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) \
586 { \
587     int     i, j; \
588     const int16_t *wptr; \
589 \
590     switch (mc_type) { \
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]); \
595             } \
596         } \
597         break; \
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); \
602         break; \
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); \
608         break; \
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); \
614         break; \
615     } \
616 } \
617
618 #define OP_PUT(a, b)  (a) = (b)
619 #define OP_ADD(a, b)  (a) += (b)
620
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)