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