Statistics
| Branch: | Revision:

ffmpeg / libavcodec / ivi_dsp.c @ 1cf31685

History | View | Annotate | Download (15 KB)

1
/*
2
 * DSP functions for Indeo Video Interactive codecs (Indeo4 and Indeo5)
3
 *
4
 * Copyright (c) 2009 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 libavcodec/ivi_dsp.c
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 "ivi_common.h"
32
#include "ivi_dsp.h"
33

    
34
void ff_ivi_recompose53(const IVIPlaneDesc *plane, uint8_t *dst,
35
                        const int dst_pitch, const int num_bands)
36
{
37
    int             x, y, indx;
38
    int32_t         p0, p1, p2, p3, tmp0, tmp1, tmp2;
39
    int32_t         b0_1, b0_2, b1_1, b1_2, b1_3, b2_1, b2_2, b2_3, b2_4, b2_5, b2_6;
40
    int32_t         b3_1, b3_2, b3_3, b3_4, b3_5, b3_6, b3_7, b3_8, b3_9;
41
    uint32_t        pitch, back_pitch;
42
    const IDWTELEM *b0_ptr, *b1_ptr, *b2_ptr, *b3_ptr;
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
/** butterfly operation for the inverse slant transform */
181
#define IVI_SLANT_BFLY(s1, s2, o1, o2, t) \
182
    t  = s1 - s2;\
183
    o1 = s1 + s2;\
184
    o2 = t;\
185

    
186
/** This is a reflection a,b = 1/2, 5/4 for the inverse slant transform */
187
#define IVI_IREFLECT(s1, s2, o1, o2, t) \
188
    t  = ((s1 + s2*2 + 2) >> 2) + s1;\
189
    o2 = ((s1*2 - s2 + 2) >> 2) - s2;\
190
    o1 = t;\
191

    
192
/** This is a reflection a,b = 1/2, 7/8 for the inverse slant transform */
193
#define IVI_SLANT_PART4(s1, s2, o1, o2, t) \
194
    t  = s2 + ((s1*4  - s2 + 4) >> 3);\
195
    o2 = s1 + ((-s1 - s2*4 + 4) >> 3);\
196
    o1 = t;\
197

    
198
/** inverse slant8 transform */
199
#define IVI_INV_SLANT8(s1, s4, s8, s5, s2, s6, s3, s7,\
200
                       d1, d2, d3, d4, d5, d6, d7, d8,\
201
                       t0, t1, t2, t3, t4, t5, t6, t7, t8) {\
202
    IVI_SLANT_PART4(s4, s5, t4, t5, t0);\
203
\
204
    IVI_SLANT_BFLY(s1, t5, t1, t5, t0); IVI_SLANT_BFLY(s2, s6, t2, t6, t0);\
205
    IVI_SLANT_BFLY(s7, s3, t7, t3, t0); IVI_SLANT_BFLY(t4, s8, t4, t8, t0);\
206
\
207
    IVI_SLANT_BFLY(t1, t2, t1, t2, t0); IVI_IREFLECT  (t4, t3, t4, t3, t0);\
208
    IVI_SLANT_BFLY(t5, t6, t5, t6, t0); IVI_IREFLECT  (t8, t7, t8, t7, t0);\
209
    IVI_SLANT_BFLY(t1, t4, t1, t4, t0); IVI_SLANT_BFLY(t2, t3, t2, t3, t0);\
210
    IVI_SLANT_BFLY(t5, t8, t5, t8, t0); IVI_SLANT_BFLY(t6, t7, t6, t7, t0);\
211
    d1 = COMPENSATE(t1);\
212
    d2 = COMPENSATE(t2);\
213
    d3 = COMPENSATE(t3);\
214
    d4 = COMPENSATE(t4);\
215
    d5 = COMPENSATE(t5);\
216
    d6 = COMPENSATE(t6);\
217
    d7 = COMPENSATE(t7);\
218
    d8 = COMPENSATE(t8);}
219

    
220
/** inverse slant4 transform */
221
#define IVI_INV_SLANT4(s1, s4, s2, s3, d1, d2, d3, d4, t0, t1, t2, t3, t4) {\
222
    IVI_SLANT_BFLY(s1, s2, t1, t2, t0); IVI_IREFLECT  (s4, s3, t4, t3, t0);\
223
\
224
    IVI_SLANT_BFLY(t1, t4, t1, t4, t0); IVI_SLANT_BFLY(t2, t3, t2, t3, t0);\
225
    d1 = COMPENSATE(t1);\
226
    d2 = COMPENSATE(t2);\
227
    d3 = COMPENSATE(t3);\
228
    d4 = COMPENSATE(t4);}
229

    
230
void ff_ivi_inverse_slant_8x8(const int32_t *in, int16_t *out, uint32_t pitch, const uint8_t *flags)
231
{
232
    int     i;
233
    const int32_t *src;
234
    int32_t *dst;
235
    int     tmp[64];
236
    int     t0, t1, t2, t3, t4, t5, t6, t7, t8;
237

    
238
#define COMPENSATE(x) (x)
239
    src = in;
240
    dst = tmp;
241
    for (i = 0; i < 8; i++) {
242
        if (flags[i]) {
243
            IVI_INV_SLANT8(src[0], src[8], src[16], src[24], src[32], src[40], src[48], src[56],
244
                           dst[0], dst[8], dst[16], dst[24], dst[32], dst[40], dst[48], dst[56],
245
                           t0, t1, t2, t3, t4, t5, t6, t7, t8);
246
        } else
247
            dst[0] = dst[8] = dst[16] = dst[24] = dst[32] = dst[40] = dst[48] = dst[56] = 0;
248

    
249
            src++;
250
            dst++;
251
    }
252
#undef COMPENSATE
253

    
254
#define COMPENSATE(x) ((x + 1)>>1)
255
    src = tmp;
256
    for (i = 0; i < 8; i++) {
257
        if (!src[0] && !src[1] && !src[2] && !src[3] && !src[4] && !src[5] && !src[6] && !src[7]) {
258
            memset(out, 0, 8*sizeof(out[0]));
259
        } else {
260
            IVI_INV_SLANT8(src[0], src[1], src[2], src[3], src[4], src[5], src[6], src[7],
261
                           out[0], out[1], out[2], out[3], out[4], out[5], out[6], out[7],
262
                           t0, t1, t2, t3, t4, t5, t6, t7, t8);
263
        }
264
        src += 8;
265
        out += pitch;
266
    }
267
#undef COMPENSATE
268
}
269

    
270
void ff_ivi_inverse_slant_4x4(const int32_t *in, int16_t *out, uint32_t pitch, const uint8_t *flags)
271
{
272
    int     i;
273
    const int32_t *src;
274
    int32_t *dst;
275
    int     tmp[16];
276
    int     t0, t1, t2, t3, t4;
277

    
278
#define COMPENSATE(x) (x)
279
    src = in;
280
    dst = tmp;
281
    for (i = 0; i < 4; i++) {
282
        if (flags[i]) {
283
            IVI_INV_SLANT4(src[0], src[4], src[8], src[12],
284
                           dst[0], dst[4], dst[8], dst[12],
285
                           t0, t1, t2, t3, t4);
286
        } else
287
            dst[0] = dst[4] = dst[8] = dst[12] = 0;
288

    
289
            src++;
290
            dst++;
291
    }
292
#undef COMPENSATE
293

    
294
#define COMPENSATE(x) ((x + 1)>>1)
295
    src = tmp;
296
    for (i = 0; i < 4; i++) {
297
        if (!src[0] && !src[1] && !src[2] && !src[3]) {
298
            out[0] = out[1] = out[2] = out[3] = 0;
299
        } else {
300
            IVI_INV_SLANT4(src[0], src[1], src[2], src[3],
301
                           out[0], out[1], out[2], out[3],
302
                           t0, t1, t2, t3, t4);
303
        }
304
        src += 4;
305
        out += pitch;
306
    }
307
#undef COMPENSATE
308
}
309

    
310
void ff_ivi_dc_slant_2d(const int32_t *in, int16_t *out, uint32_t pitch, int blk_size)
311
{
312
    int     x, y;
313
    int16_t dc_coeff;
314

    
315
    dc_coeff = (*in + 1) >> 1;
316

    
317
    for (y = 0; y < blk_size; out += pitch, y++) {
318
        for (x = 0; x < blk_size; x++)
319
            out[x] = dc_coeff;
320
    }
321
}
322

    
323
void ff_ivi_row_slant8(const int32_t *in, int16_t *out, uint32_t pitch, const uint8_t *flags)
324
{
325
    int     i;
326
    int     t0, t1, t2, t3, t4, t5, t6, t7, t8;
327

    
328
#define COMPENSATE(x) ((x + 1)>>1)
329
    for (i = 0; i < 8; i++) {
330
        if (!in[0] && !in[1] && !in[2] && !in[3] && !in[4] && !in[5] && !in[6] && !in[7]) {
331
            memset(out, 0, 8*sizeof(out[0]));
332
        } else {
333
            IVI_INV_SLANT8( in[0],  in[1],  in[2],  in[3],  in[4],  in[5],  in[6],  in[7],
334
                           out[0], out[1], out[2], out[3], out[4], out[5], out[6], out[7],
335
                           t0, t1, t2, t3, t4, t5, t6, t7, t8);
336
        }
337
        in += 8;
338
        out += pitch;
339
    }
340
#undef COMPENSATE
341
}
342

    
343
void ff_ivi_dc_row_slant(const int32_t *in, int16_t *out, uint32_t pitch, int blk_size)
344
{
345
    int     x, y;
346
    int16_t dc_coeff;
347

    
348
    dc_coeff = (*in + 1) >> 1;
349

    
350
    for (x = 0; x < blk_size; x++)
351
        out[x] = dc_coeff;
352

    
353
    out += pitch;
354

    
355
    for (y = 1; y < blk_size; out += pitch, y++) {
356
        for (x = 0; x < blk_size; x++)
357
            out[x] = 0;
358
    }
359
}
360

    
361
void ff_ivi_col_slant8(const int32_t *in, int16_t *out, uint32_t pitch, const uint8_t *flags)
362
{
363
    int     i, row2, row4, row8;
364
    int     t0, t1, t2, t3, t4, t5, t6, t7, t8;
365

    
366
    row2 = pitch << 1;
367
    row4 = pitch << 2;
368
    row8 = pitch << 3;
369

    
370
#define COMPENSATE(x) ((x + 1)>>1)
371
    for (i = 0; i < 8; i++) {
372
        if (flags[i]) {
373
            IVI_INV_SLANT8(in[0], in[8], in[16], in[24], in[32], in[40], in[48], in[56],
374
                           out[0], out[pitch], out[row2], out[row2 + pitch], out[row4],
375
                           out[row4 + pitch],  out[row4 + row2], out[row8 - pitch],
376
                           t0, t1, t2, t3, t4, t5, t6, t7, t8);
377
        } else {
378
            out[0] = out[pitch] = out[row2] = out[row2 + pitch] = out[row4] =
379
            out[row4 + pitch] =  out[row4 + row2] = out[row8 - pitch] = 0;
380
        }
381

    
382
        in++;
383
        out++;
384
    }
385
#undef COMPENSATE
386
}
387

    
388
void ff_ivi_dc_col_slant(const int32_t *in, int16_t *out, uint32_t pitch, int blk_size)
389
{
390
    int     x, y;
391
    int16_t dc_coeff;
392

    
393
    dc_coeff = (*in + 1) >> 1;
394

    
395
    for (y = 0; y < blk_size; out += pitch, y++) {
396
        out[0] = dc_coeff;
397
        for (x = 1; x < blk_size; x++)
398
            out[x] = 0;
399
    }
400
}
401

    
402
void ff_ivi_put_pixels_8x8(const int32_t *in, int16_t *out, uint32_t pitch,
403
                           const uint8_t *flags)
404
{
405
    int     x, y;
406

    
407
    for (y = 0; y < 8; out += pitch, in += 8, y++)
408
        for (x = 0; x < 8; x++)
409
            out[x] = in[x];
410
}
411

    
412
void ff_ivi_put_dc_pixel_8x8(const int32_t *in, int16_t *out, uint32_t pitch,
413
                             int blk_size)
414
{
415
    int     y;
416

    
417
    out[0] = in[0];
418
    memset(out + 1, 0, 7*sizeof(out[0]));
419
    out += pitch;
420

    
421
    for (y = 1; y < 8; out += pitch, y++)
422
        memset(out, 0, 8*sizeof(out[0]));
423
}
424

    
425
#define IVI_MC_TEMPLATE(size, suffix, OP) \
426
void ff_ivi_mc_ ## size ##x## size ## suffix (int16_t *buf, const int16_t *ref_buf, \
427
                                              uint32_t pitch, int mc_type) \
428
{ \
429
    int     i, j; \
430
    const int16_t *wptr; \
431
\
432
    switch (mc_type) { \
433
    case 0: /* fullpel (no interpolation) */ \
434
        for (i = 0; i < size; i++, buf += pitch, ref_buf += pitch) { \
435
            for (j = 0; j < size; j++) {\
436
                OP(buf[j], ref_buf[j]); \
437
            } \
438
        } \
439
        break; \
440
    case 1: /* horizontal halfpel interpolation */ \
441
        for (i = 0; i < size; i++, buf += pitch, ref_buf += pitch) \
442
            for (j = 0; j < size; j++) \
443
                OP(buf[j], (ref_buf[j] + ref_buf[j+1]) >> 1); \
444
        break; \
445
    case 2: /* vertical halfpel interpolation */ \
446
        wptr = ref_buf + pitch; \
447
        for (i = 0; i < size; i++, buf += pitch, wptr += pitch, ref_buf += pitch) \
448
            for (j = 0; j < size; j++) \
449
                OP(buf[j], (ref_buf[j] + wptr[j]) >> 1); \
450
        break; \
451
    case 3: /* vertical and horizontal halfpel interpolation */ \
452
        wptr = ref_buf + pitch; \
453
        for (i = 0; i < size; i++, buf += pitch, wptr += pitch, ref_buf += pitch) \
454
            for (j = 0; j < size; j++) \
455
                OP(buf[j], (ref_buf[j] + ref_buf[j+1] + wptr[j] + wptr[j+1]) >> 2); \
456
        break; \
457
    } \
458
} \
459

    
460
#define OP_PUT(a, b)  (a) = (b)
461
#define OP_ADD(a, b)  (a) += (b)
462

    
463
IVI_MC_TEMPLATE(8, _no_delta, OP_PUT);
464
IVI_MC_TEMPLATE(8, _delta,    OP_ADD);
465
IVI_MC_TEMPLATE(4, _no_delta, OP_PUT);
466
IVI_MC_TEMPLATE(4, _delta,    OP_ADD);