]> git.sesse.net Git - ffmpeg/blob - libavcodec/ivi_dsp.c
Merge commit 'ffba2053edfc177d217bf4a95edf51cd0fc40753'
[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 "ivi_common.h"
31 #include "ivi_dsp.h"
32
33 void ff_ivi_recompose53(const IVIPlaneDesc *plane, uint8_t *dst,
34                         const int dst_pitch)
35 {
36     int             x, y, indx;
37     int32_t         p0, p1, p2, p3, tmp0, tmp1, tmp2;
38     int32_t         b0_1, b0_2, b1_1, b1_2, b1_3, b2_1, b2_2, b2_3, b2_4, b2_5, b2_6;
39     int32_t         b3_1, b3_2, b3_3, b3_4, b3_5, b3_6, b3_7, b3_8, b3_9;
40     int32_t         pitch, back_pitch;
41     const short     *b0_ptr, *b1_ptr, *b2_ptr, *b3_ptr;
42     const int       num_bands = 4;
43
44     /* all bands should have the same pitch */
45     pitch = plane->bands[0].pitch;
46
47     /* pixels at the position "y-1" will be set to pixels at the "y" for the 1st iteration */
48     back_pitch = 0;
49
50     /* get pointers to the wavelet bands */
51     b0_ptr = plane->bands[0].buf;
52     b1_ptr = plane->bands[1].buf;
53     b2_ptr = plane->bands[2].buf;
54     b3_ptr = plane->bands[3].buf;
55
56     for (y = 0; y < plane->height; y += 2) {
57
58         if (y+2 >= plane->height)
59             pitch= 0;
60         /* load storage variables with values */
61         if (num_bands > 0) {
62             b0_1 = b0_ptr[0];
63             b0_2 = b0_ptr[pitch];
64         }
65
66         if (num_bands > 1) {
67             b1_1 = b1_ptr[back_pitch];
68             b1_2 = b1_ptr[0];
69             b1_3 = b1_1 - b1_2*6 + b1_ptr[pitch];
70         }
71
72         if (num_bands > 2) {
73             b2_2 = b2_ptr[0];     // b2[x,  y  ]
74             b2_3 = b2_2;          // b2[x+1,y  ] = b2[x,y]
75             b2_5 = b2_ptr[pitch]; // b2[x  ,y+1]
76             b2_6 = b2_5;          // b2[x+1,y+1] = b2[x,y+1]
77         }
78
79         if (num_bands > 3) {
80             b3_2 = b3_ptr[back_pitch]; // b3[x  ,y-1]
81             b3_3 = b3_2;               // b3[x+1,y-1] = b3[x  ,y-1]
82             b3_5 = b3_ptr[0];          // b3[x  ,y  ]
83             b3_6 = b3_5;               // b3[x+1,y  ] = b3[x  ,y  ]
84             b3_8 = b3_2 - b3_5*6 + b3_ptr[pitch];
85             b3_9 = b3_8;
86         }
87
88         for (x = 0, indx = 0; x < plane->width; x+=2, indx++) {
89             if (x+2 >= plane->width) {
90                 b0_ptr --;
91                 b1_ptr --;
92                 b2_ptr --;
93                 b3_ptr --;
94             }
95
96             /* some values calculated in the previous iterations can */
97             /* be reused in the next ones, so do appropriate copying */
98             b2_1 = b2_2; // b2[x-1,y  ] = b2[x,  y  ]
99             b2_2 = b2_3; // b2[x  ,y  ] = b2[x+1,y  ]
100             b2_4 = b2_5; // b2[x-1,y+1] = b2[x  ,y+1]
101             b2_5 = b2_6; // b2[x  ,y+1] = b2[x+1,y+1]
102             b3_1 = b3_2; // b3[x-1,y-1] = b3[x  ,y-1]
103             b3_2 = b3_3; // b3[x  ,y-1] = b3[x+1,y-1]
104             b3_4 = b3_5; // b3[x-1,y  ] = b3[x  ,y  ]
105             b3_5 = b3_6; // b3[x  ,y  ] = b3[x+1,y  ]
106             b3_7 = b3_8; // vert_HPF(x-1)
107             b3_8 = b3_9; // vert_HPF(x  )
108
109             p0 = p1 = p2 = p3 = 0;
110
111             /* process the LL-band by applying LPF both vertically and horizontally */
112             if (num_bands > 0) {
113                 tmp0 = b0_1;
114                 tmp2 = b0_2;
115                 b0_1 = b0_ptr[indx+1];
116                 b0_2 = b0_ptr[pitch+indx+1];
117                 tmp1 = tmp0 + b0_1;
118
119                 p0 =  tmp0 << 4;
120                 p1 =  tmp1 << 3;
121                 p2 = (tmp0 + tmp2) << 3;
122                 p3 = (tmp1 + tmp2 + b0_2) << 2;
123             }
124
125             /* process the HL-band by applying HPF vertically and LPF horizontally */
126             if (num_bands > 1) {
127                 tmp0 = b1_2;
128                 tmp1 = b1_1;
129                 b1_2 = b1_ptr[indx+1];
130                 b1_1 = b1_ptr[back_pitch+indx+1];
131
132                 tmp2 = tmp1 - tmp0*6 + b1_3;
133                 b1_3 = b1_1 - b1_2*6 + b1_ptr[pitch+indx+1];
134
135                 p0 += (tmp0 + tmp1) << 3;
136                 p1 += (tmp0 + tmp1 + b1_1 + b1_2) << 2;
137                 p2 +=  tmp2 << 2;
138                 p3 += (tmp2 + b1_3) << 1;
139             }
140
141             /* process the LH-band by applying LPF vertically and HPF horizontally */
142             if (num_bands > 2) {
143                 b2_3 = b2_ptr[indx+1];
144                 b2_6 = b2_ptr[pitch+indx+1];
145
146                 tmp0 = b2_1 + b2_2;
147                 tmp1 = b2_1 - b2_2*6 + b2_3;
148
149                 p0 += tmp0 << 3;
150                 p1 += tmp1 << 2;
151                 p2 += (tmp0 + b2_4 + b2_5) << 2;
152                 p3 += (tmp1 + b2_4 - b2_5*6 + b2_6) << 1;
153             }
154
155             /* process the HH-band by applying HPF both vertically and horizontally */
156             if (num_bands > 3) {
157                 b3_6 = b3_ptr[indx+1];            // b3[x+1,y  ]
158                 b3_3 = b3_ptr[back_pitch+indx+1]; // b3[x+1,y-1]
159
160                 tmp0 = b3_1 + b3_4;
161                 tmp1 = b3_2 + b3_5;
162                 tmp2 = b3_3 + b3_6;
163
164                 b3_9 = b3_3 - b3_6*6 + b3_ptr[pitch+indx+1];
165
166                 p0 += (tmp0 + tmp1) << 2;
167                 p1 += (tmp0 - tmp1*6 + tmp2) << 1;
168                 p2 += (b3_7 + b3_8) << 1;
169                 p3 +=  b3_7 - b3_8*6 + b3_9;
170             }
171
172             /* output four pixels */
173             dst[x]             = av_clip_uint8((p0 >> 6) + 128);
174             dst[x+1]           = av_clip_uint8((p1 >> 6) + 128);
175             dst[dst_pitch+x]   = av_clip_uint8((p2 >> 6) + 128);
176             dst[dst_pitch+x+1] = av_clip_uint8((p3 >> 6) + 128);
177         }// for x
178
179         dst += dst_pitch << 1;
180
181         back_pitch = -pitch;
182
183         b0_ptr += pitch + 1;
184         b1_ptr += pitch + 1;
185         b2_ptr += pitch + 1;
186         b3_ptr += pitch + 1;
187     }
188 }
189
190 void ff_ivi_recompose_haar(const IVIPlaneDesc *plane, uint8_t *dst,
191                            const int dst_pitch)
192 {
193     int             x, y, indx, b0, b1, b2, b3, p0, p1, p2, p3;
194     const short     *b0_ptr, *b1_ptr, *b2_ptr, *b3_ptr;
195     int32_t         pitch;
196
197     /* all bands should have the same pitch */
198     pitch = plane->bands[0].pitch;
199
200     /* get pointers to the wavelet bands */
201     b0_ptr = plane->bands[0].buf;
202     b1_ptr = plane->bands[1].buf;
203     b2_ptr = plane->bands[2].buf;
204     b3_ptr = plane->bands[3].buf;
205
206     for (y = 0; y < plane->height; y += 2) {
207         for (x = 0, indx = 0; x < plane->width; x += 2, indx++) {
208             /* load coefficients */
209             b0 = b0_ptr[indx]; //should be: b0 = (num_bands > 0) ? b0_ptr[indx] : 0;
210             b1 = b1_ptr[indx]; //should be: b1 = (num_bands > 1) ? b1_ptr[indx] : 0;
211             b2 = b2_ptr[indx]; //should be: b2 = (num_bands > 2) ? b2_ptr[indx] : 0;
212             b3 = b3_ptr[indx]; //should be: b3 = (num_bands > 3) ? b3_ptr[indx] : 0;
213
214             /* haar wavelet recomposition */
215             p0 = (b0 + b1 + b2 + b3 + 2) >> 2;
216             p1 = (b0 + b1 - b2 - b3 + 2) >> 2;
217             p2 = (b0 - b1 + b2 - b3 + 2) >> 2;
218             p3 = (b0 - b1 - b2 + b3 + 2) >> 2;
219
220             /* bias, convert and output four pixels */
221             dst[x]                 = av_clip_uint8(p0 + 128);
222             dst[x + 1]             = av_clip_uint8(p1 + 128);
223             dst[dst_pitch + x]     = av_clip_uint8(p2 + 128);
224             dst[dst_pitch + x + 1] = av_clip_uint8(p3 + 128);
225         }// for x
226
227         dst += dst_pitch << 1;
228
229         b0_ptr += pitch;
230         b1_ptr += pitch;
231         b2_ptr += pitch;
232         b3_ptr += pitch;
233     }// for y
234 }
235
236 /** butterfly operation for the inverse Haar transform */
237 #define IVI_HAAR_BFLY(s1, s2, o1, o2, t) \
238     t  = (s1 - s2) >> 1;\
239     o1 = (s1 + s2) >> 1;\
240     o2 = t;\
241
242 /** inverse 8-point Haar transform */
243 #define INV_HAAR8(s1, s5, s3, s7, s2, s4, s6, s8,\
244                   d1, d2, d3, d4, d5, d6, d7, d8,\
245                   t0, t1, t2, t3, t4, t5, t6, t7, t8) {\
246     t1 = s1 << 1; t5 = s5 << 1;\
247     IVI_HAAR_BFLY(t1, t5, t1, t5, t0); IVI_HAAR_BFLY(t1, s3, t1, t3, t0);\
248     IVI_HAAR_BFLY(t5, s7, t5, t7, t0); IVI_HAAR_BFLY(t1, s2, t1, t2, t0);\
249     IVI_HAAR_BFLY(t3, s4, t3, t4, t0); IVI_HAAR_BFLY(t5, s6, t5, t6, t0);\
250     IVI_HAAR_BFLY(t7, s8, t7, t8, t0);\
251     d1 = COMPENSATE(t1);\
252     d2 = COMPENSATE(t2);\
253     d3 = COMPENSATE(t3);\
254     d4 = COMPENSATE(t4);\
255     d5 = COMPENSATE(t5);\
256     d6 = COMPENSATE(t6);\
257     d7 = COMPENSATE(t7);\
258     d8 = COMPENSATE(t8); }
259
260 /** inverse 4-point Haar transform */
261 #define INV_HAAR4(s1, s3, s5, s7) {\
262     HAAR_BFLY(s1, s5);  HAAR_BFLY(s1, s3);  HAAR_BFLY(s5, s7);\
263     s1 = COMPENSATE(s1);\
264     s3 = COMPENSATE(s3);\
265     s5 = COMPENSATE(s5);\
266     s7 = COMPENSATE(s7); }
267
268 void ff_ivi_inverse_haar_8x8(const int32_t *in, int16_t *out, uint32_t pitch,
269                              const uint8_t *flags)
270 {
271     int     i, shift, sp1, sp2, sp3, sp4;
272     const int32_t *src;
273     int32_t *dst;
274     int     tmp[64];
275     int     t0, t1, t2, t3, t4, t5, t6, t7, t8;
276
277     /* apply the InvHaar8 to all columns */
278 #define COMPENSATE(x) (x)
279     src = in;
280     dst = tmp;
281     for (i = 0; i < 8; i++) {
282         if (flags[i]) {
283             /* pre-scaling */
284             shift = !(i & 4);
285             sp1 = src[ 0] << shift;
286             sp2 = src[ 8] << shift;
287             sp3 = src[16] << shift;
288             sp4 = src[24] << shift;
289             INV_HAAR8(    sp1,     sp2,     sp3,     sp4,
290                       src[32], src[40], src[48], src[56],
291                       dst[ 0], dst[ 8], dst[16], dst[24],
292                       dst[32], dst[40], dst[48], dst[56],
293                       t0, t1, t2, t3, t4, t5, t6, t7, t8);
294         } else
295             dst[ 0] = dst[ 8] = dst[16] = dst[24] =
296             dst[32] = dst[40] = dst[48] = dst[56] = 0;
297
298         src++;
299         dst++;
300     }
301 #undef  COMPENSATE
302
303     /* apply the InvHaar8 to all rows */
304 #define COMPENSATE(x) (x)
305     src = tmp;
306     for (i = 0; i < 8; i++) {
307         if (   !src[0] && !src[1] && !src[2] && !src[3]
308             && !src[4] && !src[5] && !src[6] && !src[7]) {
309             memset(out, 0, 8 * sizeof(out[0]));
310         } else {
311             INV_HAAR8(src[0], src[1], src[2], src[3],
312                       src[4], src[5], src[6], src[7],
313                       out[0], out[1], out[2], out[3],
314                       out[4], out[5], out[6], out[7],
315                       t0, t1, t2, t3, t4, t5, t6, t7, t8);
316         }
317         src += 8;
318         out += pitch;
319     }
320 #undef  COMPENSATE
321 }
322
323 void ff_ivi_inverse_haar_1x8(const int32_t *in, int16_t *out, uint32_t pitch,
324                              const uint8_t *flags)
325 {
326     int     i;
327     const int32_t *src;
328     int     t0, t1, t2, t3, t4, t5, t6, t7, t8;
329
330     /* apply the InvHaar8 to all columns */
331 #define COMPENSATE(x) (x)
332     src = in;
333     for (i = 0; i < 8; i++) {
334         if (flags[i]) {
335             INV_HAAR8(src[ 0], src[ 8], src[16], src[24],
336                       src[32], src[40], src[48], src[56],
337                       out[ 0], out[pitch], out[2*pitch], out[3*pitch],
338                       out[4*pitch], out[5*pitch], out[6*pitch], out[7*pitch],
339                       t0, t1, t2, t3, t4, t5, t6, t7, t8);
340         } else
341             out[      0]= out[  pitch]= out[2*pitch]= out[3*pitch]=
342             out[4*pitch]= out[5*pitch]= out[6*pitch]= out[7*pitch]= 0;
343
344         src++;
345         out++;
346     }
347 #undef  COMPENSATE
348 }
349
350 void ff_ivi_inverse_haar_8x1(const int32_t *in, int16_t *out, uint32_t pitch,
351                              const uint8_t *flags)
352 {
353     int     i;
354     const int32_t *src;
355     int     t0, t1, t2, t3, t4, t5, t6, t7, t8;
356
357     /* apply the InvHaar8 to all rows */
358 #define COMPENSATE(x) (x)
359     src = in;
360     for (i = 0; i < 8; i++) {
361         if (   !src[0] && !src[1] && !src[2] && !src[3]
362             && !src[4] && !src[5] && !src[6] && !src[7]) {
363             memset(out, 0, 8 * sizeof(out[0]));
364         } else {
365             INV_HAAR8(src[0], src[1], src[2], src[3],
366                       src[4], src[5], src[6], src[7],
367                       out[0], out[1], out[2], out[3],
368                       out[4], out[5], out[6], out[7],
369                       t0, t1, t2, t3, t4, t5, t6, t7, t8);
370         }
371         src += 8;
372         out += pitch;
373     }
374 #undef  COMPENSATE
375 }
376
377 void ff_ivi_dc_haar_2d(const int32_t *in, int16_t *out, uint32_t pitch,
378                        int blk_size)
379 {
380     int     x, y;
381     int16_t dc_coeff;
382
383     dc_coeff = (*in + 0) >> 3;
384
385     for (y = 0; y < blk_size; out += pitch, y++) {
386         for (x = 0; x < blk_size; x++)
387             out[x] = dc_coeff;
388     }
389 }
390
391 /** butterfly operation for the inverse slant transform */
392 #define IVI_SLANT_BFLY(s1, s2, o1, o2, t) \
393     t  = s1 - s2;\
394     o1 = s1 + s2;\
395     o2 = t;\
396
397 /** This is a reflection a,b = 1/2, 5/4 for the inverse slant transform */
398 #define IVI_IREFLECT(s1, s2, o1, o2, t) \
399     t  = ((s1 + s2*2 + 2) >> 2) + s1;\
400     o2 = ((s1*2 - s2 + 2) >> 2) - s2;\
401     o1 = t;\
402
403 /** This is a reflection a,b = 1/2, 7/8 for the inverse slant transform */
404 #define IVI_SLANT_PART4(s1, s2, o1, o2, t) \
405     t  = s2 + ((s1*4  - s2 + 4) >> 3);\
406     o2 = s1 + ((-s1 - s2*4 + 4) >> 3);\
407     o1 = t;\
408
409 /** inverse slant8 transform */
410 #define IVI_INV_SLANT8(s1, s4, s8, s5, s2, s6, s3, s7,\
411                        d1, d2, d3, d4, d5, d6, d7, d8,\
412                        t0, t1, t2, t3, t4, t5, t6, t7, t8) {\
413     IVI_SLANT_PART4(s4, s5, t4, t5, t0);\
414 \
415     IVI_SLANT_BFLY(s1, t5, t1, t5, t0); IVI_SLANT_BFLY(s2, s6, t2, t6, t0);\
416     IVI_SLANT_BFLY(s7, s3, t7, t3, t0); IVI_SLANT_BFLY(t4, s8, t4, t8, t0);\
417 \
418     IVI_SLANT_BFLY(t1, t2, t1, t2, t0); IVI_IREFLECT  (t4, t3, t4, t3, t0);\
419     IVI_SLANT_BFLY(t5, t6, t5, t6, t0); IVI_IREFLECT  (t8, t7, t8, t7, t0);\
420     IVI_SLANT_BFLY(t1, t4, t1, t4, t0); IVI_SLANT_BFLY(t2, t3, t2, t3, t0);\
421     IVI_SLANT_BFLY(t5, t8, t5, t8, t0); IVI_SLANT_BFLY(t6, t7, t6, t7, t0);\
422     d1 = COMPENSATE(t1);\
423     d2 = COMPENSATE(t2);\
424     d3 = COMPENSATE(t3);\
425     d4 = COMPENSATE(t4);\
426     d5 = COMPENSATE(t5);\
427     d6 = COMPENSATE(t6);\
428     d7 = COMPENSATE(t7);\
429     d8 = COMPENSATE(t8);}
430
431 /** inverse slant4 transform */
432 #define IVI_INV_SLANT4(s1, s4, s2, s3, d1, d2, d3, d4, t0, t1, t2, t3, t4) {\
433     IVI_SLANT_BFLY(s1, s2, t1, t2, t0); IVI_IREFLECT  (s4, s3, t4, t3, t0);\
434 \
435     IVI_SLANT_BFLY(t1, t4, t1, t4, t0); IVI_SLANT_BFLY(t2, t3, t2, t3, t0);\
436     d1 = COMPENSATE(t1);\
437     d2 = COMPENSATE(t2);\
438     d3 = COMPENSATE(t3);\
439     d4 = COMPENSATE(t4);}
440
441 void ff_ivi_inverse_slant_8x8(const int32_t *in, int16_t *out, uint32_t pitch, const uint8_t *flags)
442 {
443     int     i;
444     const int32_t *src;
445     int32_t *dst;
446     int     tmp[64];
447     int     t0, t1, t2, t3, t4, t5, t6, t7, t8;
448
449 #define COMPENSATE(x) (x)
450     src = in;
451     dst = tmp;
452     for (i = 0; i < 8; i++) {
453         if (flags[i]) {
454             IVI_INV_SLANT8(src[0], src[8], src[16], src[24], src[32], src[40], src[48], src[56],
455                            dst[0], dst[8], dst[16], dst[24], dst[32], dst[40], dst[48], dst[56],
456                            t0, t1, t2, t3, t4, t5, t6, t7, t8);
457         } else
458             dst[0] = dst[8] = dst[16] = dst[24] = dst[32] = dst[40] = dst[48] = dst[56] = 0;
459
460             src++;
461             dst++;
462     }
463 #undef COMPENSATE
464
465 #define COMPENSATE(x) ((x + 1)>>1)
466     src = tmp;
467     for (i = 0; i < 8; i++) {
468         if (!src[0] && !src[1] && !src[2] && !src[3] && !src[4] && !src[5] && !src[6] && !src[7]) {
469             memset(out, 0, 8*sizeof(out[0]));
470         } else {
471             IVI_INV_SLANT8(src[0], src[1], src[2], src[3], src[4], src[5], src[6], src[7],
472                            out[0], out[1], out[2], out[3], out[4], out[5], out[6], out[7],
473                            t0, t1, t2, t3, t4, t5, t6, t7, t8);
474         }
475         src += 8;
476         out += pitch;
477     }
478 #undef COMPENSATE
479 }
480
481 void ff_ivi_inverse_slant_4x4(const int32_t *in, int16_t *out, uint32_t pitch, const uint8_t *flags)
482 {
483     int     i;
484     const int32_t *src;
485     int32_t *dst;
486     int     tmp[16];
487     int     t0, t1, t2, t3, t4;
488
489 #define COMPENSATE(x) (x)
490     src = in;
491     dst = tmp;
492     for (i = 0; i < 4; i++) {
493         if (flags[i]) {
494             IVI_INV_SLANT4(src[0], src[4], src[8], src[12],
495                            dst[0], dst[4], dst[8], dst[12],
496                            t0, t1, t2, t3, t4);
497         } else
498             dst[0] = dst[4] = dst[8] = dst[12] = 0;
499
500             src++;
501             dst++;
502     }
503 #undef COMPENSATE
504
505 #define COMPENSATE(x) ((x + 1)>>1)
506     src = tmp;
507     for (i = 0; i < 4; i++) {
508         if (!src[0] && !src[1] && !src[2] && !src[3]) {
509             out[0] = out[1] = out[2] = out[3] = 0;
510         } else {
511             IVI_INV_SLANT4(src[0], src[1], src[2], src[3],
512                            out[0], out[1], out[2], out[3],
513                            t0, t1, t2, t3, t4);
514         }
515         src += 4;
516         out += pitch;
517     }
518 #undef COMPENSATE
519 }
520
521 void ff_ivi_dc_slant_2d(const int32_t *in, int16_t *out, uint32_t pitch, int blk_size)
522 {
523     int     x, y;
524     int16_t dc_coeff;
525
526     dc_coeff = (*in + 1) >> 1;
527
528     for (y = 0; y < blk_size; out += pitch, y++) {
529         for (x = 0; x < blk_size; x++)
530             out[x] = dc_coeff;
531     }
532 }
533
534 void ff_ivi_row_slant8(const int32_t *in, int16_t *out, uint32_t pitch, const uint8_t *flags)
535 {
536     int     i;
537     int     t0, t1, t2, t3, t4, t5, t6, t7, t8;
538
539 #define COMPENSATE(x) ((x + 1)>>1)
540     for (i = 0; i < 8; i++) {
541         if (!in[0] && !in[1] && !in[2] && !in[3] && !in[4] && !in[5] && !in[6] && !in[7]) {
542             memset(out, 0, 8*sizeof(out[0]));
543         } else {
544             IVI_INV_SLANT8( in[0],  in[1],  in[2],  in[3],  in[4],  in[5],  in[6],  in[7],
545                            out[0], out[1], out[2], out[3], out[4], out[5], out[6], out[7],
546                            t0, t1, t2, t3, t4, t5, t6, t7, t8);
547         }
548         in += 8;
549         out += pitch;
550     }
551 #undef COMPENSATE
552 }
553
554 void ff_ivi_dc_row_slant(const int32_t *in, int16_t *out, uint32_t pitch, int blk_size)
555 {
556     int     x, y;
557     int16_t dc_coeff;
558
559     dc_coeff = (*in + 1) >> 1;
560
561     for (x = 0; x < blk_size; x++)
562         out[x] = dc_coeff;
563
564     out += pitch;
565
566     for (y = 1; y < blk_size; out += pitch, y++) {
567         for (x = 0; x < blk_size; x++)
568             out[x] = 0;
569     }
570 }
571
572 void ff_ivi_col_slant8(const int32_t *in, int16_t *out, uint32_t pitch, const uint8_t *flags)
573 {
574     int     i, row2, row4, row8;
575     int     t0, t1, t2, t3, t4, t5, t6, t7, t8;
576
577     row2 = pitch << 1;
578     row4 = pitch << 2;
579     row8 = pitch << 3;
580
581 #define COMPENSATE(x) ((x + 1)>>1)
582     for (i = 0; i < 8; i++) {
583         if (flags[i]) {
584             IVI_INV_SLANT8(in[0], in[8], in[16], in[24], in[32], in[40], in[48], in[56],
585                            out[0], out[pitch], out[row2], out[row2 + pitch], out[row4],
586                            out[row4 + pitch],  out[row4 + row2], out[row8 - pitch],
587                            t0, t1, t2, t3, t4, t5, t6, t7, t8);
588         } else {
589             out[0] = out[pitch] = out[row2] = out[row2 + pitch] = out[row4] =
590             out[row4 + pitch] =  out[row4 + row2] = out[row8 - pitch] = 0;
591         }
592
593         in++;
594         out++;
595     }
596 #undef COMPENSATE
597 }
598
599 void ff_ivi_dc_col_slant(const int32_t *in, int16_t *out, uint32_t pitch, int blk_size)
600 {
601     int     x, y;
602     int16_t dc_coeff;
603
604     dc_coeff = (*in + 1) >> 1;
605
606     for (y = 0; y < blk_size; out += pitch, y++) {
607         out[0] = dc_coeff;
608         for (x = 1; x < blk_size; x++)
609             out[x] = 0;
610     }
611 }
612
613 void ff_ivi_put_pixels_8x8(const int32_t *in, int16_t *out, uint32_t pitch,
614                            const uint8_t *flags)
615 {
616     int     x, y;
617
618     for (y = 0; y < 8; out += pitch, in += 8, y++)
619         for (x = 0; x < 8; x++)
620             out[x] = in[x];
621 }
622
623 void ff_ivi_put_dc_pixel_8x8(const int32_t *in, int16_t *out, uint32_t pitch,
624                              int blk_size)
625 {
626     int     y;
627
628     out[0] = in[0];
629     memset(out + 1, 0, 7*sizeof(out[0]));
630     out += pitch;
631
632     for (y = 1; y < 8; out += pitch, y++)
633         memset(out, 0, 8*sizeof(out[0]));
634 }
635
636 #define IVI_MC_TEMPLATE(size, suffix, OP) \
637 void ff_ivi_mc_ ## size ##x## size ## suffix (int16_t *buf, const int16_t *ref_buf, \
638                                               uint32_t pitch, int mc_type) \
639 { \
640     int     i, j; \
641     const int16_t *wptr; \
642 \
643     switch (mc_type) { \
644     case 0: /* fullpel (no interpolation) */ \
645         for (i = 0; i < size; i++, buf += pitch, ref_buf += pitch) { \
646             for (j = 0; j < size; j++) {\
647                 OP(buf[j], ref_buf[j]); \
648             } \
649         } \
650         break; \
651     case 1: /* horizontal halfpel interpolation */ \
652         for (i = 0; i < size; i++, buf += pitch, ref_buf += pitch) \
653             for (j = 0; j < size; j++) \
654                 OP(buf[j], (ref_buf[j] + ref_buf[j+1]) >> 1); \
655         break; \
656     case 2: /* vertical halfpel interpolation */ \
657         wptr = ref_buf + pitch; \
658         for (i = 0; i < size; i++, buf += pitch, wptr += pitch, ref_buf += pitch) \
659             for (j = 0; j < size; j++) \
660                 OP(buf[j], (ref_buf[j] + wptr[j]) >> 1); \
661         break; \
662     case 3: /* vertical and horizontal halfpel interpolation */ \
663         wptr = ref_buf + pitch; \
664         for (i = 0; i < size; i++, buf += pitch, wptr += pitch, ref_buf += pitch) \
665             for (j = 0; j < size; j++) \
666                 OP(buf[j], (ref_buf[j] + ref_buf[j+1] + wptr[j] + wptr[j+1]) >> 2); \
667         break; \
668     } \
669 } \
670
671 #define OP_PUT(a, b)  (a) = (b)
672 #define OP_ADD(a, b)  (a) += (b)
673
674 IVI_MC_TEMPLATE(8, _no_delta, OP_PUT)
675 IVI_MC_TEMPLATE(8, _delta,    OP_ADD)
676 IVI_MC_TEMPLATE(4, _no_delta, OP_PUT)
677 IVI_MC_TEMPLATE(4, _delta,    OP_ADD)