]> git.sesse.net Git - ffmpeg/blob - libavcodec/ivi_dsp.c
lavc: mark the old audio/video encoding API as deprecated
[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 recompositions)
26  * for Indeo Video Interactive codecs.
27  */
28
29 #include "avcodec.h"
30 #include "ivi.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         /* load storage variables with values */
58         if (num_bands > 0) {
59             b0_1 = b0_ptr[0];
60             b0_2 = b0_ptr[pitch];
61         }
62
63         if (num_bands > 1) {
64             b1_1 = b1_ptr[back_pitch];
65             b1_2 = b1_ptr[0];
66             b1_3 = b1_1 - b1_2*6 + b1_ptr[pitch];
67         }
68
69         if (num_bands > 2) {
70             b2_2 = b2_ptr[0];     // b2[x,  y  ]
71             b2_3 = b2_2;          // b2[x+1,y  ] = b2[x,y]
72             b2_5 = b2_ptr[pitch]; // b2[x  ,y+1]
73             b2_6 = b2_5;          // b2[x+1,y+1] = b2[x,y+1]
74         }
75
76         if (num_bands > 3) {
77             b3_2 = b3_ptr[back_pitch]; // b3[x  ,y-1]
78             b3_3 = b3_2;               // b3[x+1,y-1] = b3[x  ,y-1]
79             b3_5 = b3_ptr[0];          // b3[x  ,y  ]
80             b3_6 = b3_5;               // b3[x+1,y  ] = b3[x  ,y  ]
81             b3_8 = b3_2 - b3_5*6 + b3_ptr[pitch];
82             b3_9 = b3_8;
83         }
84
85         for (x = 0, indx = 0; x < plane->width; x+=2, indx++) {
86             /* some values calculated in the previous iterations can */
87             /* be reused in the next ones, so do appropriate copying */
88             b2_1 = b2_2; // b2[x-1,y  ] = b2[x,  y  ]
89             b2_2 = b2_3; // b2[x  ,y  ] = b2[x+1,y  ]
90             b2_4 = b2_5; // b2[x-1,y+1] = b2[x  ,y+1]
91             b2_5 = b2_6; // b2[x  ,y+1] = b2[x+1,y+1]
92             b3_1 = b3_2; // b3[x-1,y-1] = b3[x  ,y-1]
93             b3_2 = b3_3; // b3[x  ,y-1] = b3[x+1,y-1]
94             b3_4 = b3_5; // b3[x-1,y  ] = b3[x  ,y  ]
95             b3_5 = b3_6; // b3[x  ,y  ] = b3[x+1,y  ]
96             b3_7 = b3_8; // vert_HPF(x-1)
97             b3_8 = b3_9; // vert_HPF(x  )
98
99             p0 = p1 = p2 = p3 = 0;
100
101             /* process the LL-band by applying LPF both vertically and horizontally */
102             if (num_bands > 0) {
103                 tmp0 = b0_1;
104                 tmp2 = b0_2;
105                 b0_1 = b0_ptr[indx+1];
106                 b0_2 = b0_ptr[pitch+indx+1];
107                 tmp1 = tmp0 + b0_1;
108
109                 p0 =  tmp0 << 4;
110                 p1 =  tmp1 << 3;
111                 p2 = (tmp0 + tmp2) << 3;
112                 p3 = (tmp1 + tmp2 + b0_2) << 2;
113             }
114
115             /* process the HL-band by applying HPF vertically and LPF horizontally */
116             if (num_bands > 1) {
117                 tmp0 = b1_2;
118                 tmp1 = b1_1;
119                 b1_2 = b1_ptr[indx+1];
120                 b1_1 = b1_ptr[back_pitch+indx+1];
121
122                 tmp2 = tmp1 - tmp0*6 + b1_3;
123                 b1_3 = b1_1 - b1_2*6 + b1_ptr[pitch+indx+1];
124
125                 p0 += (tmp0 + tmp1) << 3;
126                 p1 += (tmp0 + tmp1 + b1_1 + b1_2) << 2;
127                 p2 +=  tmp2 << 2;
128                 p3 += (tmp2 + b1_3) << 1;
129             }
130
131             /* process the LH-band by applying LPF vertically and HPF horizontally */
132             if (num_bands > 2) {
133                 b2_3 = b2_ptr[indx+1];
134                 b2_6 = b2_ptr[pitch+indx+1];
135
136                 tmp0 = b2_1 + b2_2;
137                 tmp1 = b2_1 - b2_2*6 + b2_3;
138
139                 p0 += tmp0 << 3;
140                 p1 += tmp1 << 2;
141                 p2 += (tmp0 + b2_4 + b2_5) << 2;
142                 p3 += (tmp1 + b2_4 - b2_5*6 + b2_6) << 1;
143             }
144
145             /* process the HH-band by applying HPF both vertically and horizontally */
146             if (num_bands > 3) {
147                 b3_6 = b3_ptr[indx+1];            // b3[x+1,y  ]
148                 b3_3 = b3_ptr[back_pitch+indx+1]; // b3[x+1,y-1]
149
150                 tmp0 = b3_1 + b3_4;
151                 tmp1 = b3_2 + b3_5;
152                 tmp2 = b3_3 + b3_6;
153
154                 b3_9 = b3_3 - b3_6*6 + b3_ptr[pitch+indx+1];
155
156                 p0 += (tmp0 + tmp1) << 2;
157                 p1 += (tmp0 - tmp1*6 + tmp2) << 1;
158                 p2 += (b3_7 + b3_8) << 1;
159                 p3 +=  b3_7 - b3_8*6 + b3_9;
160             }
161
162             /* output four pixels */
163             dst[x]             = av_clip_uint8((p0 >> 6) + 128);
164             dst[x+1]           = av_clip_uint8((p1 >> 6) + 128);
165             dst[dst_pitch+x]   = av_clip_uint8((p2 >> 6) + 128);
166             dst[dst_pitch+x+1] = av_clip_uint8((p3 >> 6) + 128);
167         }// for x
168
169         dst += dst_pitch << 1;
170
171         back_pitch = -pitch;
172
173         b0_ptr += pitch;
174         b1_ptr += pitch;
175         b2_ptr += pitch;
176         b3_ptr += pitch;
177     }
178 }
179
180 void ff_ivi_recompose_haar(const IVIPlaneDesc *plane, uint8_t *dst,
181                            const int dst_pitch)
182 {
183     int             x, y, indx, b0, b1, b2, b3, p0, p1, p2, p3;
184     const short    *b0_ptr, *b1_ptr, *b2_ptr, *b3_ptr;
185     int32_t         pitch;
186
187     /* all bands should have the same pitch */
188     pitch = plane->bands[0].pitch;
189
190     /* get pointers to the wavelet bands */
191     b0_ptr = plane->bands[0].buf;
192     b1_ptr = plane->bands[1].buf;
193     b2_ptr = plane->bands[2].buf;
194     b3_ptr = plane->bands[3].buf;
195
196     for (y = 0; y < plane->height; y += 2) {
197         for (x = 0, indx = 0; x < plane->width; x += 2, indx++) {
198             /* load coefficients */
199             b0 = b0_ptr[indx]; //should be: b0 = (num_bands > 0) ? b0_ptr[indx] : 0;
200             b1 = b1_ptr[indx]; //should be: b1 = (num_bands > 1) ? b1_ptr[indx] : 0;
201             b2 = b2_ptr[indx]; //should be: b2 = (num_bands > 2) ? b2_ptr[indx] : 0;
202             b3 = b3_ptr[indx]; //should be: b3 = (num_bands > 3) ? b3_ptr[indx] : 0;
203
204             /* haar wavelet recomposition */
205             p0 = (b0 + b1 + b2 + b3 + 2) >> 2;
206             p1 = (b0 + b1 - b2 - b3 + 2) >> 2;
207             p2 = (b0 - b1 + b2 - b3 + 2) >> 2;
208             p3 = (b0 - b1 - b2 + b3 + 2) >> 2;
209
210             /* bias, convert and output four pixels */
211             dst[x]                 = av_clip_uint8(p0 + 128);
212             dst[x + 1]             = av_clip_uint8(p1 + 128);
213             dst[dst_pitch + x]     = av_clip_uint8(p2 + 128);
214             dst[dst_pitch + x + 1] = av_clip_uint8(p3 + 128);
215         }// for x
216
217         dst += dst_pitch << 1;
218
219         b0_ptr += pitch;
220         b1_ptr += pitch;
221         b2_ptr += pitch;
222         b3_ptr += pitch;
223     }// for y
224 }
225
226 /** butterfly operation for the inverse Haar transform */
227 #define IVI_HAAR_BFLY(s1, s2, o1, o2, t) \
228     t  = (s1 - s2) >> 1;\
229     o1 = (s1 + s2) >> 1;\
230     o2 = t;\
231
232 /** inverse 8-point Haar transform */
233 #define INV_HAAR8(s1, s5, s3, s7, s2, s4, s6, s8,\
234                   d1, d2, d3, d4, d5, d6, d7, d8,\
235                   t0, t1, t2, t3, t4, t5, t6, t7, t8) {\
236     t1 = s1 << 1; t5 = s5 << 1;\
237     IVI_HAAR_BFLY(t1, t5, t1, t5, t0); IVI_HAAR_BFLY(t1, s3, t1, t3, t0);\
238     IVI_HAAR_BFLY(t5, s7, t5, t7, t0); IVI_HAAR_BFLY(t1, s2, t1, t2, t0);\
239     IVI_HAAR_BFLY(t3, s4, t3, t4, t0); IVI_HAAR_BFLY(t5, s6, t5, t6, t0);\
240     IVI_HAAR_BFLY(t7, s8, t7, t8, t0);\
241     d1 = COMPENSATE(t1);\
242     d2 = COMPENSATE(t2);\
243     d3 = COMPENSATE(t3);\
244     d4 = COMPENSATE(t4);\
245     d5 = COMPENSATE(t5);\
246     d6 = COMPENSATE(t6);\
247     d7 = COMPENSATE(t7);\
248     d8 = COMPENSATE(t8); }
249
250 /** inverse 4-point Haar transform */
251 #define INV_HAAR4(s1, s3, s5, s7, d1, d2, d3, d4, t0, t1, t2, t3, t4) {\
252     IVI_HAAR_BFLY(s1, s3, t0, t1, t4);\
253     IVI_HAAR_BFLY(t0, s5, t2, t3, t4);\
254     d1 = COMPENSATE(t2);\
255     d2 = COMPENSATE(t3);\
256     IVI_HAAR_BFLY(t1, s7, t2, t3, t4);\
257     d3 = COMPENSATE(t2);\
258     d4 = COMPENSATE(t3); }
259
260 void ff_ivi_inverse_haar_8x8(const int32_t *in, int16_t *out, uint32_t pitch,
261                              const uint8_t *flags)
262 {
263     int     i, shift, sp1, sp2, sp3, sp4;
264     const int32_t *src;
265     int32_t *dst;
266     int     tmp[64];
267     int     t0, t1, t2, t3, t4, t5, t6, t7, t8;
268
269     /* apply the InvHaar8 to all columns */
270 #define COMPENSATE(x) (x)
271     src = in;
272     dst = tmp;
273     for (i = 0; i < 8; i++) {
274         if (flags[i]) {
275             /* pre-scaling */
276             shift = !(i & 4);
277             sp1 = src[ 0] << shift;
278             sp2 = src[ 8] << shift;
279             sp3 = src[16] << shift;
280             sp4 = src[24] << shift;
281             INV_HAAR8(    sp1,     sp2,     sp3,     sp4,
282                       src[32], src[40], src[48], src[56],
283                       dst[ 0], dst[ 8], dst[16], dst[24],
284                       dst[32], dst[40], dst[48], dst[56],
285                       t0, t1, t2, t3, t4, t5, t6, t7, t8);
286         } else
287             dst[ 0] = dst[ 8] = dst[16] = dst[24] =
288             dst[32] = dst[40] = dst[48] = dst[56] = 0;
289
290         src++;
291         dst++;
292     }
293 #undef  COMPENSATE
294
295     /* apply the InvHaar8 to all rows */
296 #define COMPENSATE(x) (x)
297     src = tmp;
298     for (i = 0; i < 8; i++) {
299         if (   !src[0] && !src[1] && !src[2] && !src[3]
300             && !src[4] && !src[5] && !src[6] && !src[7]) {
301             memset(out, 0, 8 * sizeof(out[0]));
302         } else {
303             INV_HAAR8(src[0], src[1], src[2], src[3],
304                       src[4], src[5], src[6], src[7],
305                       out[0], out[1], out[2], out[3],
306                       out[4], out[5], out[6], out[7],
307                       t0, t1, t2, t3, t4, t5, t6, t7, t8);
308         }
309         src += 8;
310         out += pitch;
311     }
312 #undef  COMPENSATE
313 }
314
315 void ff_ivi_row_haar8(const int32_t *in, int16_t *out, uint32_t pitch,
316                       const uint8_t *flags)
317 {
318     int     i;
319     int     t0, t1, t2, t3, t4, t5, t6, t7, t8;
320
321     /* apply the InvHaar8 to all rows */
322 #define COMPENSATE(x) (x)
323     for (i = 0; i < 8; i++) {
324         if (   !in[0] && !in[1] && !in[2] && !in[3]
325             && !in[4] && !in[5] && !in[6] && !in[7]) {
326             memset(out, 0, 8 * sizeof(out[0]));
327         } else {
328             INV_HAAR8(in[0],  in[1],  in[2],  in[3],
329                       in[4],  in[5],  in[6],  in[7],
330                       out[0], out[1], out[2], out[3],
331                       out[4], out[5], out[6], out[7],
332                       t0, t1, t2, t3, t4, t5, t6, t7, t8);
333         }
334         in  += 8;
335         out += pitch;
336     }
337 #undef  COMPENSATE
338 }
339
340 void ff_ivi_col_haar8(const int32_t *in, int16_t *out, uint32_t pitch,
341                       const uint8_t *flags)
342 {
343     int     i;
344     int     t0, t1, t2, t3, t4, t5, t6, t7, t8;
345
346     /* apply the InvHaar8 to all columns */
347 #define COMPENSATE(x) (x)
348     for (i = 0; i < 8; i++) {
349         if (flags[i]) {
350             INV_HAAR8(in[ 0], in[ 8], in[16], in[24],
351                       in[32], in[40], in[48], in[56],
352                       out[0 * pitch], out[1 * pitch],
353                       out[2 * pitch], out[3 * pitch],
354                       out[4 * pitch], out[5 * pitch],
355                       out[6 * pitch], out[7 * pitch],
356                       t0, t1, t2, t3, t4, t5, t6, t7, t8);
357         } else
358             out[0 * pitch] = out[1 * pitch] =
359             out[2 * pitch] = out[3 * pitch] =
360             out[4 * pitch] = out[5 * pitch] =
361             out[6 * pitch] = out[7 * pitch] = 0;
362
363         in++;
364         out++;
365     }
366 #undef  COMPENSATE
367 }
368
369 void ff_ivi_inverse_haar_4x4(const int32_t *in, int16_t *out, uint32_t pitch,
370                              const uint8_t *flags)
371 {
372     int     i, shift, sp1, sp2;
373     const int32_t *src;
374     int32_t *dst;
375     int     tmp[16];
376     int     t0, t1, t2, t3, t4;
377
378     /* apply the InvHaar4 to all columns */
379 #define COMPENSATE(x) (x)
380     src = in;
381     dst = tmp;
382     for (i = 0; i < 4; i++) {
383         if (flags[i]) {
384             /* pre-scaling */
385             shift = !(i & 2);
386             sp1 = src[0] << shift;
387             sp2 = src[4] << shift;
388             INV_HAAR4(   sp1,    sp2, src[8], src[12],
389                       dst[0], dst[4], dst[8], dst[12],
390                       t0, t1, t2, t3, t4);
391         } else
392             dst[0] = dst[4] = dst[8] = dst[12] = 0;
393
394         src++;
395         dst++;
396     }
397 #undef  COMPENSATE
398
399     /* apply the InvHaar8 to all rows */
400 #define COMPENSATE(x) (x)
401     src = tmp;
402     for (i = 0; i < 4; i++) {
403         if (!src[0] && !src[1] && !src[2] && !src[3]) {
404             memset(out, 0, 4 * sizeof(out[0]));
405         } else {
406             INV_HAAR4(src[0], src[1], src[2], src[3],
407                       out[0], out[1], out[2], out[3],
408                       t0, t1, t2, t3, t4);
409         }
410         src += 4;
411         out += pitch;
412     }
413 #undef  COMPENSATE
414 }
415
416 void ff_ivi_row_haar4(const int32_t *in, int16_t *out, uint32_t pitch,
417                       const uint8_t *flags)
418 {
419     int     i;
420     int     t0, t1, t2, t3, t4;
421
422     /* apply the InvHaar4 to all rows */
423 #define COMPENSATE(x) (x)
424     for (i = 0; i < 4; i++) {
425         if (!in[0] && !in[1] && !in[2] && !in[3]) {
426             memset(out, 0, 4 * sizeof(out[0]));
427         } else {
428             INV_HAAR4(in[0], in[1], in[2], in[3],
429                       out[0], out[1], out[2], out[3],
430                       t0, t1, t2, t3, t4);
431         }
432         in  += 4;
433         out += pitch;
434     }
435 #undef  COMPENSATE
436 }
437
438 void ff_ivi_col_haar4(const int32_t *in, int16_t *out, uint32_t pitch,
439                       const uint8_t *flags)
440 {
441     int     i;
442     int     t0, t1, t2, t3, t4;
443
444     /* apply the InvHaar8 to all columns */
445 #define COMPENSATE(x) (x)
446     for (i = 0; i < 4; i++) {
447         if (flags[i]) {
448             INV_HAAR4(in[0], in[4], in[8], in[12],
449                       out[0 * pitch], out[1 * pitch],
450                       out[2 * pitch], out[3 * pitch],
451                       t0, t1, t2, t3, t4);
452         } else
453             out[0 * pitch] = out[1 * pitch] =
454             out[2 * pitch] = out[3 * pitch] = 0;
455
456         in++;
457         out++;
458     }
459 #undef  COMPENSATE
460 }
461
462 void ff_ivi_dc_haar_2d(const int32_t *in, int16_t *out, uint32_t pitch,
463                        int blk_size)
464 {
465     int     x, y;
466     int16_t dc_coeff;
467
468     dc_coeff = (*in + 0) >> 3;
469
470     for (y = 0; y < blk_size; out += pitch, y++) {
471         for (x = 0; x < blk_size; x++)
472             out[x] = dc_coeff;
473     }
474 }
475
476 /** butterfly operation for the inverse slant transform */
477 #define IVI_SLANT_BFLY(s1, s2, o1, o2, t) \
478     t  = s1 - s2;\
479     o1 = s1 + s2;\
480     o2 = t;\
481
482 /** This is a reflection a,b = 1/2, 5/4 for the inverse slant transform */
483 #define IVI_IREFLECT(s1, s2, o1, o2, t) \
484     t  = ((s1 + s2*2 + 2) >> 2) + s1;\
485     o2 = ((s1*2 - s2 + 2) >> 2) - s2;\
486     o1 = t;\
487
488 /** This is a reflection a,b = 1/2, 7/8 for the inverse slant transform */
489 #define IVI_SLANT_PART4(s1, s2, o1, o2, t) \
490     t  = s2 + ((s1*4  - s2 + 4) >> 3);\
491     o2 = s1 + ((-s1 - s2*4 + 4) >> 3);\
492     o1 = t;\
493
494 /** inverse slant8 transform */
495 #define IVI_INV_SLANT8(s1, s4, s8, s5, s2, s6, s3, s7,\
496                        d1, d2, d3, d4, d5, d6, d7, d8,\
497                        t0, t1, t2, t3, t4, t5, t6, t7, t8) {\
498     IVI_SLANT_PART4(s4, s5, t4, t5, t0);\
499 \
500     IVI_SLANT_BFLY(s1, t5, t1, t5, t0); IVI_SLANT_BFLY(s2, s6, t2, t6, t0);\
501     IVI_SLANT_BFLY(s7, s3, t7, t3, t0); IVI_SLANT_BFLY(t4, s8, t4, t8, t0);\
502 \
503     IVI_SLANT_BFLY(t1, t2, t1, t2, t0); IVI_IREFLECT  (t4, t3, t4, t3, t0);\
504     IVI_SLANT_BFLY(t5, t6, t5, t6, t0); IVI_IREFLECT  (t8, t7, t8, t7, t0);\
505     IVI_SLANT_BFLY(t1, t4, t1, t4, t0); IVI_SLANT_BFLY(t2, t3, t2, t3, t0);\
506     IVI_SLANT_BFLY(t5, t8, t5, t8, t0); IVI_SLANT_BFLY(t6, t7, t6, t7, t0);\
507     d1 = COMPENSATE(t1);\
508     d2 = COMPENSATE(t2);\
509     d3 = COMPENSATE(t3);\
510     d4 = COMPENSATE(t4);\
511     d5 = COMPENSATE(t5);\
512     d6 = COMPENSATE(t6);\
513     d7 = COMPENSATE(t7);\
514     d8 = COMPENSATE(t8);}
515
516 /** inverse slant4 transform */
517 #define IVI_INV_SLANT4(s1, s4, s2, s3, d1, d2, d3, d4, t0, t1, t2, t3, t4) {\
518     IVI_SLANT_BFLY(s1, s2, t1, t2, t0); IVI_IREFLECT  (s4, s3, t4, t3, t0);\
519 \
520     IVI_SLANT_BFLY(t1, t4, t1, t4, t0); IVI_SLANT_BFLY(t2, t3, t2, t3, t0);\
521     d1 = COMPENSATE(t1);\
522     d2 = COMPENSATE(t2);\
523     d3 = COMPENSATE(t3);\
524     d4 = COMPENSATE(t4);}
525
526 void ff_ivi_inverse_slant_8x8(const int32_t *in, int16_t *out, uint32_t pitch, const uint8_t *flags)
527 {
528     int     i;
529     const int32_t *src;
530     int32_t *dst;
531     int     tmp[64];
532     int     t0, t1, t2, t3, t4, t5, t6, t7, t8;
533
534 #define COMPENSATE(x) (x)
535     src = in;
536     dst = tmp;
537     for (i = 0; i < 8; i++) {
538         if (flags[i]) {
539             IVI_INV_SLANT8(src[0], src[8], src[16], src[24], src[32], src[40], src[48], src[56],
540                            dst[0], dst[8], dst[16], dst[24], dst[32], dst[40], dst[48], dst[56],
541                            t0, t1, t2, t3, t4, t5, t6, t7, t8);
542         } else
543             dst[0] = dst[8] = dst[16] = dst[24] = dst[32] = dst[40] = dst[48] = dst[56] = 0;
544
545             src++;
546             dst++;
547     }
548 #undef COMPENSATE
549
550 #define COMPENSATE(x) ((x + 1)>>1)
551     src = tmp;
552     for (i = 0; i < 8; i++) {
553         if (!src[0] && !src[1] && !src[2] && !src[3] && !src[4] && !src[5] && !src[6] && !src[7]) {
554             memset(out, 0, 8*sizeof(out[0]));
555         } else {
556             IVI_INV_SLANT8(src[0], src[1], src[2], src[3], src[4], src[5], src[6], src[7],
557                            out[0], out[1], out[2], out[3], out[4], out[5], out[6], out[7],
558                            t0, t1, t2, t3, t4, t5, t6, t7, t8);
559         }
560         src += 8;
561         out += pitch;
562     }
563 #undef COMPENSATE
564 }
565
566 void ff_ivi_inverse_slant_4x4(const int32_t *in, int16_t *out, uint32_t pitch, const uint8_t *flags)
567 {
568     int     i;
569     const int32_t *src;
570     int32_t *dst;
571     int     tmp[16];
572     int     t0, t1, t2, t3, t4;
573
574 #define COMPENSATE(x) (x)
575     src = in;
576     dst = tmp;
577     for (i = 0; i < 4; i++) {
578         if (flags[i]) {
579             IVI_INV_SLANT4(src[0], src[4], src[8], src[12],
580                            dst[0], dst[4], dst[8], dst[12],
581                            t0, t1, t2, t3, t4);
582         } else
583             dst[0] = dst[4] = dst[8] = dst[12] = 0;
584
585             src++;
586             dst++;
587     }
588 #undef COMPENSATE
589
590 #define COMPENSATE(x) ((x + 1)>>1)
591     src = tmp;
592     for (i = 0; i < 4; i++) {
593         if (!src[0] && !src[1] && !src[2] && !src[3]) {
594             out[0] = out[1] = out[2] = out[3] = 0;
595         } else {
596             IVI_INV_SLANT4(src[0], src[1], src[2], src[3],
597                            out[0], out[1], out[2], out[3],
598                            t0, t1, t2, t3, t4);
599         }
600         src += 4;
601         out += pitch;
602     }
603 #undef COMPENSATE
604 }
605
606 void ff_ivi_dc_slant_2d(const int32_t *in, int16_t *out, uint32_t pitch, int blk_size)
607 {
608     int     x, y;
609     int16_t dc_coeff;
610
611     dc_coeff = (*in + 1) >> 1;
612
613     for (y = 0; y < blk_size; out += pitch, y++) {
614         for (x = 0; x < blk_size; x++)
615             out[x] = dc_coeff;
616     }
617 }
618
619 void ff_ivi_row_slant8(const int32_t *in, int16_t *out, uint32_t pitch, const uint8_t *flags)
620 {
621     int     i;
622     int     t0, t1, t2, t3, t4, t5, t6, t7, t8;
623
624 #define COMPENSATE(x) ((x + 1)>>1)
625     for (i = 0; i < 8; i++) {
626         if (!in[0] && !in[1] && !in[2] && !in[3] && !in[4] && !in[5] && !in[6] && !in[7]) {
627             memset(out, 0, 8*sizeof(out[0]));
628         } else {
629             IVI_INV_SLANT8( in[0],  in[1],  in[2],  in[3],  in[4],  in[5],  in[6],  in[7],
630                            out[0], out[1], out[2], out[3], out[4], out[5], out[6], out[7],
631                            t0, t1, t2, t3, t4, t5, t6, t7, t8);
632         }
633         in += 8;
634         out += pitch;
635     }
636 #undef COMPENSATE
637 }
638
639 void ff_ivi_dc_row_slant(const int32_t *in, int16_t *out, uint32_t pitch, int blk_size)
640 {
641     int     x, y;
642     int16_t dc_coeff;
643
644     dc_coeff = (*in + 1) >> 1;
645
646     for (x = 0; x < blk_size; x++)
647         out[x] = dc_coeff;
648
649     out += pitch;
650
651     for (y = 1; y < blk_size; out += pitch, y++) {
652         for (x = 0; x < blk_size; x++)
653             out[x] = 0;
654     }
655 }
656
657 void ff_ivi_col_slant8(const int32_t *in, int16_t *out, uint32_t pitch, const uint8_t *flags)
658 {
659     int     i, row2, row4, row8;
660     int     t0, t1, t2, t3, t4, t5, t6, t7, t8;
661
662     row2 = pitch << 1;
663     row4 = pitch << 2;
664     row8 = pitch << 3;
665
666 #define COMPENSATE(x) ((x + 1)>>1)
667     for (i = 0; i < 8; i++) {
668         if (flags[i]) {
669             IVI_INV_SLANT8(in[0], in[8], in[16], in[24], in[32], in[40], in[48], in[56],
670                            out[0], out[pitch], out[row2], out[row2 + pitch], out[row4],
671                            out[row4 + pitch],  out[row4 + row2], out[row8 - pitch],
672                            t0, t1, t2, t3, t4, t5, t6, t7, t8);
673         } else {
674             out[0] = out[pitch] = out[row2] = out[row2 + pitch] = out[row4] =
675             out[row4 + pitch] =  out[row4 + row2] = out[row8 - pitch] = 0;
676         }
677
678         in++;
679         out++;
680     }
681 #undef COMPENSATE
682 }
683
684 void ff_ivi_dc_col_slant(const int32_t *in, int16_t *out, uint32_t pitch, int blk_size)
685 {
686     int     x, y;
687     int16_t dc_coeff;
688
689     dc_coeff = (*in + 1) >> 1;
690
691     for (y = 0; y < blk_size; out += pitch, y++) {
692         out[0] = dc_coeff;
693         for (x = 1; x < blk_size; x++)
694             out[x] = 0;
695     }
696 }
697
698 void ff_ivi_row_slant4(const int32_t *in, int16_t *out, uint32_t pitch, const uint8_t *flags)
699 {
700     int     i;
701     int     t0, t1, t2, t3, t4;
702
703 #define COMPENSATE(x) ((x + 1)>>1)
704     for (i = 0; i < 4; i++) {
705         if (!in[0] && !in[1] && !in[2] && !in[3]) {
706             memset(out, 0, 4*sizeof(out[0]));
707         } else {
708             IVI_INV_SLANT4( in[0],  in[1],  in[2],  in[3],
709                            out[0], out[1], out[2], out[3],
710                            t0, t1, t2, t3, t4);
711         }
712         in  += 4;
713         out += pitch;
714     }
715 #undef COMPENSATE
716 }
717
718 void ff_ivi_col_slant4(const int32_t *in, int16_t *out, uint32_t pitch, const uint8_t *flags)
719 {
720     int     i, row2;
721     int     t0, t1, t2, t3, t4;
722
723     row2 = pitch << 1;
724
725 #define COMPENSATE(x) ((x + 1)>>1)
726     for (i = 0; i < 4; i++) {
727         if (flags[i]) {
728             IVI_INV_SLANT4(in[0], in[4], in[8], in[12],
729                            out[0], out[pitch], out[row2], out[row2 + pitch],
730                            t0, t1, t2, t3, t4);
731         } else {
732             out[0] = out[pitch] = out[row2] = out[row2 + pitch] = 0;
733         }
734
735         in++;
736         out++;
737     }
738 #undef COMPENSATE
739 }
740
741 void ff_ivi_put_pixels_8x8(const int32_t *in, int16_t *out, uint32_t pitch,
742                            const uint8_t *flags)
743 {
744     int     x, y;
745
746     for (y = 0; y < 8; out += pitch, in += 8, y++)
747         for (x = 0; x < 8; x++)
748             out[x] = in[x];
749 }
750
751 void ff_ivi_put_dc_pixel_8x8(const int32_t *in, int16_t *out, uint32_t pitch,
752                              int blk_size)
753 {
754     int     y;
755
756     out[0] = in[0];
757     memset(out + 1, 0, 7*sizeof(out[0]));
758     out += pitch;
759
760     for (y = 1; y < 8; out += pitch, y++)
761         memset(out, 0, 8*sizeof(out[0]));
762 }
763
764 #define IVI_MC_TEMPLATE(size, suffix, OP) \
765 static void ivi_mc_ ## size ##x## size ## suffix(int16_t *buf, \
766                                                  uint32_t dpitch, \
767                                                  const int16_t *ref_buf, \
768                                                  uint32_t pitch, int mc_type) \
769 { \
770     int     i, j; \
771     const int16_t *wptr; \
772 \
773     switch (mc_type) { \
774     case 0: /* fullpel (no interpolation) */ \
775         for (i = 0; i < size; i++, buf += dpitch, ref_buf += pitch) { \
776             for (j = 0; j < size; j++) {\
777                 OP(buf[j], ref_buf[j]); \
778             } \
779         } \
780         break; \
781     case 1: /* horizontal halfpel interpolation */ \
782         for (i = 0; i < size; i++, buf += dpitch, ref_buf += pitch) \
783             for (j = 0; j < size; j++) \
784                 OP(buf[j], (ref_buf[j] + ref_buf[j+1]) >> 1); \
785         break; \
786     case 2: /* vertical halfpel interpolation */ \
787         wptr = ref_buf + pitch; \
788         for (i = 0; i < size; i++, buf += dpitch, wptr += pitch, ref_buf += pitch) \
789             for (j = 0; j < size; j++) \
790                 OP(buf[j], (ref_buf[j] + wptr[j]) >> 1); \
791         break; \
792     case 3: /* vertical and horizontal halfpel interpolation */ \
793         wptr = ref_buf + pitch; \
794         for (i = 0; i < size; i++, buf += dpitch, wptr += pitch, ref_buf += pitch) \
795             for (j = 0; j < size; j++) \
796                 OP(buf[j], (ref_buf[j] + ref_buf[j+1] + wptr[j] + wptr[j+1]) >> 2); \
797         break; \
798     } \
799 } \
800 \
801 void ff_ivi_mc_ ## size ##x## size ## suffix(int16_t *buf, const int16_t *ref_buf, \
802                                              uint32_t pitch, int mc_type) \
803 { \
804     ivi_mc_ ## size ##x## size ## suffix(buf, pitch, ref_buf, pitch, mc_type); \
805 } \
806
807 #define IVI_MC_AVG_TEMPLATE(size, suffix, OP) \
808 void ff_ivi_mc_avg_ ## size ##x## size ## suffix(int16_t *buf, \
809                                                  const int16_t *ref_buf, \
810                                                  const int16_t *ref_buf2, \
811                                                  uint32_t pitch, \
812                                                  int mc_type, int mc_type2) \
813 { \
814     int16_t tmp[size * size]; \
815     int i, j; \
816 \
817     ivi_mc_ ## size ##x## size ## _no_delta(tmp, size, ref_buf, pitch, mc_type); \
818     ivi_mc_ ## size ##x## size ## _delta(tmp, size, ref_buf2, pitch, mc_type2); \
819     for (i = 0; i < size; i++, buf += pitch) { \
820         for (j = 0; j < size; j++) {\
821             OP(buf[j], tmp[i * size + j] >> 1); \
822         } \
823     } \
824 } \
825
826 #define OP_PUT(a, b)  (a) = (b)
827 #define OP_ADD(a, b)  (a) += (b)
828
829 IVI_MC_TEMPLATE(8, _no_delta, OP_PUT)
830 IVI_MC_TEMPLATE(8, _delta,    OP_ADD)
831 IVI_MC_TEMPLATE(4, _no_delta, OP_PUT)
832 IVI_MC_TEMPLATE(4, _delta,    OP_ADD)
833 IVI_MC_AVG_TEMPLATE(8, _no_delta, OP_PUT)
834 IVI_MC_AVG_TEMPLATE(8, _delta,    OP_ADD)
835 IVI_MC_AVG_TEMPLATE(4, _no_delta, OP_PUT)
836 IVI_MC_AVG_TEMPLATE(4, _delta,    OP_ADD)