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