Revision 05aec7bb

View differences:

configure
894 894
    bzlib
895 895
    dct
896 896
    doc
897
    dwt
897 898
    dxva2
898 899
    fastdiv
899 900
    ffmpeg
......
1276 1277
rv40_decoder_select="golomb"
1277 1278
shorten_decoder_select="golomb"
1278 1279
sipr_decoder_select="lsp"
1279
snow_encoder_select="aandct"
1280
snow_decoder_select="dwt"
1281
snow_encoder_select="aandct dwt"
1280 1282
sonic_decoder_select="golomb"
1281 1283
sonic_encoder_select="golomb"
1282 1284
sonic_ls_encoder_select="golomb"
libavcodec/Makefile
28 28
OBJS-$(CONFIG_AANDCT)                  += aandcttab.o
29 29
OBJS-$(CONFIG_ENCODERS)                += faandct.o jfdctfst.o jfdctint.o
30 30
OBJS-$(CONFIG_DCT)                     += dct.o
31
OBJS-$(CONFIG_DWT)                     += dwt.o
31 32
OBJS-$(CONFIG_DXVA2)                   += dxva2.o
32 33
FFT-OBJS-$(CONFIG_HARDCODED_TABLES)    += cos_tables.o
33 34
OBJS-$(CONFIG_FFT)                     += avfft.o fft.o $(FFT-OBJS-yes)
......
598 599
MMX-OBJS-$(CONFIG_ENCODERS)            += x86/dsputilenc_mmx.o
599 600
MMX-OBJS-$(CONFIG_GPL)                 += x86/idct_mmx.o
600 601
MMX-OBJS-$(CONFIG_LPC)                 += x86/lpc_mmx.o
601
MMX-OBJS-$(CONFIG_SNOW_DECODER)        += x86/snowdsp_mmx.o
602
MMX-OBJS-$(CONFIG_DWT)                 += x86/snowdsp_mmx.o
602 603
MMX-OBJS-$(CONFIG_VC1_DECODER)         += x86/vc1dsp_mmx.o
603 604
MMX-OBJS-$(CONFIG_VP3_DECODER)         += x86/vp3dsp_mmx.o              \
604 605
                                          x86/vp3dsp_sse2.o
libavcodec/dsputil.c
33 33
#include "faandct.h"
34 34
#include "faanidct.h"
35 35
#include "mathops.h"
36
#include "snow.h"
37 36
#include "mpegvideo.h"
38 37
#include "config.h"
39 38
#include "lpc.h"
......
329 328
    return s;
330 329
}
331 330

  
332

  
333
#if CONFIG_SNOW_ENCODER //dwt is in snow.c
334
static inline int w_c(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int w, int h, int type){
335
    int s, i, j;
336
    const int dec_count= w==8 ? 3 : 4;
337
    int tmp[32*32];
338
    int level, ori;
339
    static const int scale[2][2][4][4]={
340
      {
341
        {
342
            // 9/7 8x8 dec=3
343
            {268, 239, 239, 213},
344
            {  0, 224, 224, 152},
345
            {  0, 135, 135, 110},
346
        },{
347
            // 9/7 16x16 or 32x32 dec=4
348
            {344, 310, 310, 280},
349
            {  0, 320, 320, 228},
350
            {  0, 175, 175, 136},
351
            {  0, 129, 129, 102},
352
        }
353
      },{
354
        {
355
            // 5/3 8x8 dec=3
356
            {275, 245, 245, 218},
357
            {  0, 230, 230, 156},
358
            {  0, 138, 138, 113},
359
        },{
360
            // 5/3 16x16 or 32x32 dec=4
361
            {352, 317, 317, 286},
362
            {  0, 328, 328, 233},
363
            {  0, 180, 180, 140},
364
            {  0, 132, 132, 105},
365
        }
366
      }
367
    };
368

  
369
    for (i = 0; i < h; i++) {
370
        for (j = 0; j < w; j+=4) {
371
            tmp[32*i+j+0] = (pix1[j+0] - pix2[j+0])<<4;
372
            tmp[32*i+j+1] = (pix1[j+1] - pix2[j+1])<<4;
373
            tmp[32*i+j+2] = (pix1[j+2] - pix2[j+2])<<4;
374
            tmp[32*i+j+3] = (pix1[j+3] - pix2[j+3])<<4;
375
        }
376
        pix1 += line_size;
377
        pix2 += line_size;
378
    }
379

  
380
    ff_spatial_dwt(tmp, w, h, 32, type, dec_count);
381

  
382
    s=0;
383
    assert(w==h);
384
    for(level=0; level<dec_count; level++){
385
        for(ori= level ? 1 : 0; ori<4; ori++){
386
            int size= w>>(dec_count-level);
387
            int sx= (ori&1) ? size : 0;
388
            int stride= 32<<(dec_count-level);
389
            int sy= (ori&2) ? stride>>1 : 0;
390

  
391
            for(i=0; i<size; i++){
392
                for(j=0; j<size; j++){
393
                    int v= tmp[sx + sy + i*stride + j] * scale[type][dec_count-3][level][ori];
394
                    s += FFABS(v);
395
                }
396
            }
397
        }
398
    }
399
    assert(s>=0);
400
    return s>>9;
401
}
402

  
403
static int w53_8_c(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int h){
404
    return w_c(v, pix1, pix2, line_size,  8, h, 1);
405
}
406

  
407
static int w97_8_c(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int h){
408
    return w_c(v, pix1, pix2, line_size,  8, h, 0);
409
}
410

  
411
static int w53_16_c(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int h){
412
    return w_c(v, pix1, pix2, line_size, 16, h, 1);
413
}
414

  
415
static int w97_16_c(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int h){
416
    return w_c(v, pix1, pix2, line_size, 16, h, 0);
417
}
418

  
419
int w53_32_c(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int h){
420
    return w_c(v, pix1, pix2, line_size, 32, h, 1);
421
}
422

  
423
int w97_32_c(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int h){
424
    return w_c(v, pix1, pix2, line_size, 32, h, 0);
425
}
426
#endif
427

  
428 331
/* draw the edges of width 'w' of an image of size width, height */
429 332
//FIXME check that this is ok for mpeg4 interlaced
430 333
static void draw_edges_c(uint8_t *buf, int wrap, int width, int height, int w)
......
3531 3434
        case FF_CMP_NSSE:
3532 3435
            cmp[i]= c->nsse[i];
3533 3436
            break;
3534
#if CONFIG_SNOW_ENCODER
3437
#if CONFIG_DWT
3535 3438
        case FF_CMP_W53:
3536 3439
            cmp[i]= c->w53[i];
3537 3440
            break;
......
4816 4719
    c->vsse[5]= vsse_intra8_c;
4817 4720
    c->nsse[0]= nsse16_c;
4818 4721
    c->nsse[1]= nsse8_c;
4819
#if CONFIG_SNOW_ENCODER
4820
    c->w53[0]= w53_16_c;
4821
    c->w53[1]= w53_8_c;
4822
    c->w97[0]= w97_16_c;
4823
    c->w97[1]= w97_8_c;
4722
#if CONFIG_DWT
4723
    ff_dsputil_init_dwt(c);
4824 4724
#endif
4825 4725

  
4826 4726
    c->ssd_int8_vs_int16 = ssd_int8_vs_int16_c;
......
4865 4765
    c->try_8x8basis= try_8x8basis_c;
4866 4766
    c->add_8x8basis= add_8x8basis_c;
4867 4767

  
4868
#if CONFIG_SNOW_DECODER
4869
    c->vertical_compose97i = ff_snow_vertical_compose97i;
4870
    c->horizontal_compose97i = ff_snow_horizontal_compose97i;
4871
    c->inner_add_yblock = ff_snow_inner_add_yblock;
4872
#endif
4873

  
4874 4768
#if CONFIG_VORBIS_DECODER
4875 4769
    c->vorbis_inverse_coupling = vorbis_inverse_coupling;
4876 4770
#endif
libavcodec/dsputil.h
37 37
//#define DEBUG
38 38
/* dct code */
39 39
typedef short DCTELEM;
40
typedef int DWTELEM;
41
typedef short IDWTELEM;
42 40

  
43 41
void fdct_ifast (DCTELEM *data);
44 42
void fdct_ifast248 (DCTELEM *data);
......
185 183
// although currently h<4 is not used as functions with width <8 are neither used nor implemented
186 184
typedef int (*me_cmp_func)(void /*MpegEncContext*/ *s, uint8_t *blk1/*align width (8 or 16)*/, uint8_t *blk2/*align 1*/, int line_size, int h)/* __attribute__ ((const))*/;
187 185

  
188

  
189
// for snow slices
190
typedef struct slice_buffer_s slice_buffer;
191

  
192 186
/**
193 187
 * Scantable.
194 188
 */
......
538 532
    void (*h264_idct_add8)(uint8_t **dst/*align 16*/, const int *blockoffset, DCTELEM *block/*align 16*/, int stride, const uint8_t nnzc[6*8]);
539 533
    void (*h264_idct_add16intra)(uint8_t *dst/*align 16*/, const int *blockoffset, DCTELEM *block/*align 16*/, int stride, const uint8_t nnzc[6*8]);
540 534

  
541
    /* snow wavelet */
542
    void (*vertical_compose97i)(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, IDWTELEM *b3, IDWTELEM *b4, IDWTELEM *b5, int width);
543
    void (*horizontal_compose97i)(IDWTELEM *b, int width);
544
    void (*inner_add_yblock)(const uint8_t *obmc, const int obmc_stride, uint8_t * * block, int b_w, int b_h, int src_x, int src_y, int src_stride, slice_buffer * sb, int add, uint8_t * dst8);
545

  
546 535
    void (*prefetch)(void *mem, int stride, int h);
547 536

  
548 537
    void (*shrink[4])(uint8_t *dst, int dst_wrap, const uint8_t *src, int src_wrap, int width, int height);
......
681 670
void dsputil_init_sh4(DSPContext* c, AVCodecContext *avctx);
682 671
void dsputil_init_vis(DSPContext* c, AVCodecContext *avctx);
683 672

  
673
void ff_dsputil_init_dwt(DSPContext *c);
684 674
void ff_cavsdsp_init(DSPContext* c, AVCodecContext *avctx);
685 675
void ff_rv30dsp_init(DSPContext* c, AVCodecContext *avctx);
686 676
void ff_rv40dsp_init(DSPContext* c, AVCodecContext *avctx);
libavcodec/dwt.c
1
/*
2
 * Copyright (C) 2004-2010 Michael Niedermayer <michaelni@gmx.at>
3
 *
4
 * This file is part of FFmpeg.
5
 *
6
 * FFmpeg is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2.1 of the License, or (at your option) any later version.
10
 *
11
 * FFmpeg is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with FFmpeg; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
 */
20

  
21
#include "libavutil/attributes.h"
22
#include "dsputil.h"
23
#include "dwt.h"
24

  
25
void slice_buffer_init(slice_buffer * buf, int line_count, int max_allocated_lines, int line_width, IDWTELEM * base_buffer)
26
{
27
    int i;
28

  
29
    buf->base_buffer = base_buffer;
30
    buf->line_count = line_count;
31
    buf->line_width = line_width;
32
    buf->data_count = max_allocated_lines;
33
    buf->line = av_mallocz (sizeof(IDWTELEM *) * line_count);
34
    buf->data_stack = av_malloc (sizeof(IDWTELEM *) * max_allocated_lines);
35

  
36
    for(i = 0; i < max_allocated_lines; i++){
37
        buf->data_stack[i] = av_malloc (sizeof(IDWTELEM) * line_width);
38
    }
39

  
40
    buf->data_stack_top = max_allocated_lines - 1;
41
}
42

  
43
IDWTELEM * slice_buffer_load_line(slice_buffer * buf, int line)
44
{
45
    IDWTELEM * buffer;
46

  
47
    assert(buf->data_stack_top >= 0);
48
//  assert(!buf->line[line]);
49
    if (buf->line[line])
50
        return buf->line[line];
51

  
52
    buffer = buf->data_stack[buf->data_stack_top];
53
    buf->data_stack_top--;
54
    buf->line[line] = buffer;
55

  
56
    return buffer;
57
}
58

  
59
void slice_buffer_release(slice_buffer * buf, int line)
60
{
61
    IDWTELEM * buffer;
62

  
63
    assert(line >= 0 && line < buf->line_count);
64
    assert(buf->line[line]);
65

  
66
    buffer = buf->line[line];
67
    buf->data_stack_top++;
68
    buf->data_stack[buf->data_stack_top] = buffer;
69
    buf->line[line] = NULL;
70
}
71

  
72
void slice_buffer_flush(slice_buffer * buf)
73
{
74
    int i;
75
    for(i = 0; i < buf->line_count; i++){
76
        if (buf->line[i])
77
            slice_buffer_release(buf, i);
78
    }
79
}
80

  
81
void slice_buffer_destroy(slice_buffer * buf)
82
{
83
    int i;
84
    slice_buffer_flush(buf);
85

  
86
    for(i = buf->data_count - 1; i >= 0; i--){
87
        av_freep(&buf->data_stack[i]);
88
    }
89
    av_freep(&buf->data_stack);
90
    av_freep(&buf->line);
91
}
92

  
93
static inline int mirror(int v, int m){
94
    while((unsigned)v > (unsigned)m){
95
        v=-v;
96
        if(v<0) v+= 2*m;
97
    }
98
    return v;
99
}
100

  
101
static av_always_inline void
102
lift(DWTELEM *dst, DWTELEM *src, DWTELEM *ref,
103
     int dst_step, int src_step, int ref_step,
104
     int width, int mul, int add, int shift,
105
     int highpass, int inverse){
106
    const int mirror_left= !highpass;
107
    const int mirror_right= (width&1) ^ highpass;
108
    const int w= (width>>1) - 1 + (highpass & width);
109
    int i;
110

  
111
#define LIFT(src, ref, inv) ((src) + ((inv) ? - (ref) : + (ref)))
112
    if(mirror_left){
113
        dst[0] = LIFT(src[0], ((mul*2*ref[0]+add)>>shift), inverse);
114
        dst += dst_step;
115
        src += src_step;
116
    }
117

  
118
    for(i=0; i<w; i++){
119
        dst[i*dst_step] =
120
            LIFT(src[i*src_step],
121
                 ((mul*(ref[i*ref_step] + ref[(i+1)*ref_step])+add)>>shift),
122
                 inverse);
123
    }
124

  
125
    if(mirror_right){
126
        dst[w*dst_step] =
127
            LIFT(src[w*src_step],
128
                 ((mul*2*ref[w*ref_step]+add)>>shift),
129
                 inverse);
130
    }
131
}
132

  
133
static av_always_inline void
134
inv_lift(IDWTELEM *dst, IDWTELEM *src, IDWTELEM *ref,
135
         int dst_step, int src_step, int ref_step,
136
         int width, int mul, int add, int shift,
137
         int highpass, int inverse){
138
    const int mirror_left= !highpass;
139
    const int mirror_right= (width&1) ^ highpass;
140
    const int w= (width>>1) - 1 + (highpass & width);
141
    int i;
142

  
143
#define LIFT(src, ref, inv) ((src) + ((inv) ? - (ref) : + (ref)))
144
    if(mirror_left){
145
        dst[0] = LIFT(src[0], ((mul*2*ref[0]+add)>>shift), inverse);
146
        dst += dst_step;
147
        src += src_step;
148
    }
149

  
150
    for(i=0; i<w; i++){
151
        dst[i*dst_step] =
152
            LIFT(src[i*src_step],
153
                 ((mul*(ref[i*ref_step] + ref[(i+1)*ref_step])+add)>>shift),
154
                 inverse);
155
    }
156

  
157
    if(mirror_right){
158
        dst[w*dst_step] =
159
            LIFT(src[w*src_step],
160
                 ((mul*2*ref[w*ref_step]+add)>>shift),
161
                 inverse);
162
    }
163
}
164

  
165
#ifndef liftS
166
static av_always_inline void
167
liftS(DWTELEM *dst, DWTELEM *src, DWTELEM *ref,
168
      int dst_step, int src_step, int ref_step,
169
      int width, int mul, int add, int shift,
170
      int highpass, int inverse){
171
    const int mirror_left= !highpass;
172
    const int mirror_right= (width&1) ^ highpass;
173
    const int w= (width>>1) - 1 + (highpass & width);
174
    int i;
175

  
176
    assert(shift == 4);
177
#define LIFTS(src, ref, inv) \
178
        ((inv) ? \
179
            (src) + (((ref) + 4*(src))>>shift): \
180
            -((-16*(src) + (ref) + add/4 + 1 + (5<<25))/(5*4) - (1<<23)))
181
    if(mirror_left){
182
        dst[0] = LIFTS(src[0], mul*2*ref[0]+add, inverse);
183
        dst += dst_step;
184
        src += src_step;
185
    }
186

  
187
    for(i=0; i<w; i++){
188
        dst[i*dst_step] =
189
            LIFTS(src[i*src_step],
190
                  mul*(ref[i*ref_step] + ref[(i+1)*ref_step])+add,
191
                  inverse);
192
    }
193

  
194
    if(mirror_right){
195
        dst[w*dst_step] =
196
            LIFTS(src[w*src_step], mul*2*ref[w*ref_step]+add, inverse);
197
    }
198
}
199
static av_always_inline void
200
inv_liftS(IDWTELEM *dst, IDWTELEM *src, IDWTELEM *ref,
201
          int dst_step, int src_step, int ref_step,
202
          int width, int mul, int add, int shift,
203
          int highpass, int inverse){
204
    const int mirror_left= !highpass;
205
    const int mirror_right= (width&1) ^ highpass;
206
    const int w= (width>>1) - 1 + (highpass & width);
207
    int i;
208

  
209
    assert(shift == 4);
210
#define LIFTS(src, ref, inv) \
211
    ((inv) ? \
212
        (src) + (((ref) + 4*(src))>>shift): \
213
        -((-16*(src) + (ref) + add/4 + 1 + (5<<25))/(5*4) - (1<<23)))
214
    if(mirror_left){
215
        dst[0] = LIFTS(src[0], mul*2*ref[0]+add, inverse);
216
        dst += dst_step;
217
        src += src_step;
218
    }
219

  
220
    for(i=0; i<w; i++){
221
        dst[i*dst_step] =
222
            LIFTS(src[i*src_step],
223
                  mul*(ref[i*ref_step] + ref[(i+1)*ref_step])+add,
224
                  inverse);
225
    }
226

  
227
    if(mirror_right){
228
        dst[w*dst_step] =
229
            LIFTS(src[w*src_step], mul*2*ref[w*ref_step]+add, inverse);
230
    }
231
}
232
#endif /* ! liftS */
233

  
234
static void horizontal_decompose53i(DWTELEM *b, int width){
235
    DWTELEM temp[width];
236
    const int width2= width>>1;
237
    int x;
238
    const int w2= (width+1)>>1;
239

  
240
    for(x=0; x<width2; x++){
241
        temp[x   ]= b[2*x    ];
242
        temp[x+w2]= b[2*x + 1];
243
    }
244
    if(width&1)
245
        temp[x   ]= b[2*x    ];
246
#if 0
247
    {
248
    int A1,A2,A3,A4;
249
    A2= temp[1       ];
250
    A4= temp[0       ];
251
    A1= temp[0+width2];
252
    A1 -= (A2 + A4)>>1;
253
    A4 += (A1 + 1)>>1;
254
    b[0+width2] = A1;
255
    b[0       ] = A4;
256
    for(x=1; x+1<width2; x+=2){
257
        A3= temp[x+width2];
258
        A4= temp[x+1     ];
259
        A3 -= (A2 + A4)>>1;
260
        A2 += (A1 + A3 + 2)>>2;
261
        b[x+width2] = A3;
262
        b[x       ] = A2;
263

  
264
        A1= temp[x+1+width2];
265
        A2= temp[x+2       ];
266
        A1 -= (A2 + A4)>>1;
267
        A4 += (A1 + A3 + 2)>>2;
268
        b[x+1+width2] = A1;
269
        b[x+1       ] = A4;
270
    }
271
    A3= temp[width-1];
272
    A3 -= A2;
273
    A2 += (A1 + A3 + 2)>>2;
274
    b[width -1] = A3;
275
    b[width2-1] = A2;
276
    }
277
#else
278
    lift(b+w2, temp+w2, temp, 1, 1, 1, width, -1, 0, 1, 1, 0);
279
    lift(b   , temp   , b+w2, 1, 1, 1, width,  1, 2, 2, 0, 0);
280
#endif /* 0 */
281
}
282

  
283
static void vertical_decompose53iH0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
284
    int i;
285

  
286
    for(i=0; i<width; i++){
287
        b1[i] -= (b0[i] + b2[i])>>1;
288
    }
289
}
290

  
291
static void vertical_decompose53iL0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
292
    int i;
293

  
294
    for(i=0; i<width; i++){
295
        b1[i] += (b0[i] + b2[i] + 2)>>2;
296
    }
297
}
298

  
299
static void spatial_decompose53i(DWTELEM *buffer, int width, int height, int stride){
300
    int y;
301
    DWTELEM *b0= buffer + mirror(-2-1, height-1)*stride;
302
    DWTELEM *b1= buffer + mirror(-2  , height-1)*stride;
303

  
304
    for(y=-2; y<height; y+=2){
305
        DWTELEM *b2= buffer + mirror(y+1, height-1)*stride;
306
        DWTELEM *b3= buffer + mirror(y+2, height-1)*stride;
307

  
308
        if(y+1<(unsigned)height) horizontal_decompose53i(b2, width);
309
        if(y+2<(unsigned)height) horizontal_decompose53i(b3, width);
310

  
311
        if(y+1<(unsigned)height) vertical_decompose53iH0(b1, b2, b3, width);
312
        if(y+0<(unsigned)height) vertical_decompose53iL0(b0, b1, b2, width);
313

  
314
        b0=b2;
315
        b1=b3;
316
    }
317
}
318

  
319
static void horizontal_decompose97i(DWTELEM *b, int width){
320
    DWTELEM temp[width];
321
    const int w2= (width+1)>>1;
322

  
323
    lift (temp+w2, b    +1, b      , 1, 2, 2, width,  W_AM, W_AO, W_AS, 1, 1);
324
    liftS(temp   , b      , temp+w2, 1, 2, 1, width,  W_BM, W_BO, W_BS, 0, 0);
325
    lift (b   +w2, temp+w2, temp   , 1, 1, 1, width,  W_CM, W_CO, W_CS, 1, 0);
326
    lift (b      , temp   , b   +w2, 1, 1, 1, width,  W_DM, W_DO, W_DS, 0, 0);
327
}
328

  
329

  
330
static void vertical_decompose97iH0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
331
    int i;
332

  
333
    for(i=0; i<width; i++){
334
        b1[i] -= (W_AM*(b0[i] + b2[i])+W_AO)>>W_AS;
335
    }
336
}
337

  
338
static void vertical_decompose97iH1(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
339
    int i;
340

  
341
    for(i=0; i<width; i++){
342
        b1[i] += (W_CM*(b0[i] + b2[i])+W_CO)>>W_CS;
343
    }
344
}
345

  
346
static void vertical_decompose97iL0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
347
    int i;
348

  
349
    for(i=0; i<width; i++){
350
#ifdef liftS
351
        b1[i] -= (W_BM*(b0[i] + b2[i])+W_BO)>>W_BS;
352
#else
353
        b1[i] = (16*4*b1[i] - 4*(b0[i] + b2[i]) + W_BO*5 + (5<<27)) / (5*16) - (1<<23);
354
#endif
355
    }
356
}
357

  
358
static void vertical_decompose97iL1(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
359
    int i;
360

  
361
    for(i=0; i<width; i++){
362
        b1[i] += (W_DM*(b0[i] + b2[i])+W_DO)>>W_DS;
363
    }
364
}
365

  
366
static void spatial_decompose97i(DWTELEM *buffer, int width, int height, int stride){
367
    int y;
368
    DWTELEM *b0= buffer + mirror(-4-1, height-1)*stride;
369
    DWTELEM *b1= buffer + mirror(-4  , height-1)*stride;
370
    DWTELEM *b2= buffer + mirror(-4+1, height-1)*stride;
371
    DWTELEM *b3= buffer + mirror(-4+2, height-1)*stride;
372

  
373
    for(y=-4; y<height; y+=2){
374
        DWTELEM *b4= buffer + mirror(y+3, height-1)*stride;
375
        DWTELEM *b5= buffer + mirror(y+4, height-1)*stride;
376

  
377
        if(y+3<(unsigned)height) horizontal_decompose97i(b4, width);
378
        if(y+4<(unsigned)height) horizontal_decompose97i(b5, width);
379

  
380
        if(y+3<(unsigned)height) vertical_decompose97iH0(b3, b4, b5, width);
381
        if(y+2<(unsigned)height) vertical_decompose97iL0(b2, b3, b4, width);
382
        if(y+1<(unsigned)height) vertical_decompose97iH1(b1, b2, b3, width);
383
        if(y+0<(unsigned)height) vertical_decompose97iL1(b0, b1, b2, width);
384

  
385
        b0=b2;
386
        b1=b3;
387
        b2=b4;
388
        b3=b5;
389
    }
390
}
391

  
392
void ff_spatial_dwt(DWTELEM *buffer, int width, int height, int stride, int type, int decomposition_count){
393
    int level;
394

  
395
    for(level=0; level<decomposition_count; level++){
396
        switch(type){
397
        case DWT_97: spatial_decompose97i(buffer, width>>level, height>>level, stride<<level); break;
398
        case DWT_53: spatial_decompose53i(buffer, width>>level, height>>level, stride<<level); break;
399
        }
400
    }
401
}
402

  
403
static void horizontal_compose53i(IDWTELEM *b, int width){
404
    IDWTELEM temp[width];
405
    const int width2= width>>1;
406
    const int w2= (width+1)>>1;
407
    int x;
408

  
409
    for(x=0; x<width2; x++){
410
        temp[2*x    ]= b[x   ];
411
        temp[2*x + 1]= b[x+w2];
412
    }
413
    if(width&1)
414
        temp[2*x    ]= b[x   ];
415

  
416
    b[0] = temp[0] - ((temp[1]+1)>>1);
417
    for(x=2; x<width-1; x+=2){
418
        b[x  ] = temp[x  ] - ((temp[x-1] + temp[x+1]+2)>>2);
419
        b[x-1] = temp[x-1] + ((b   [x-2] + b   [x  ]+1)>>1);
420
    }
421
    if(width&1){
422
        b[x  ] = temp[x  ] - ((temp[x-1]+1)>>1);
423
        b[x-1] = temp[x-1] + ((b   [x-2] + b  [x  ]+1)>>1);
424
    }else
425
        b[x-1] = temp[x-1] + b[x-2];
426
}
427

  
428
static void vertical_compose53iH0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width){
429
    int i;
430

  
431
    for(i=0; i<width; i++){
432
        b1[i] += (b0[i] + b2[i])>>1;
433
    }
434
}
435

  
436
static void vertical_compose53iL0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width){
437
    int i;
438

  
439
    for(i=0; i<width; i++){
440
        b1[i] -= (b0[i] + b2[i] + 2)>>2;
441
    }
442
}
443

  
444
static void spatial_compose53i_buffered_init(DWTCompose *cs, slice_buffer * sb, int height, int stride_line){
445
    cs->b0 = slice_buffer_get_line(sb, mirror(-1-1, height-1) * stride_line);
446
    cs->b1 = slice_buffer_get_line(sb, mirror(-1  , height-1) * stride_line);
447
    cs->y = -1;
448
}
449

  
450
static void spatial_compose53i_init(DWTCompose *cs, IDWTELEM *buffer, int height, int stride){
451
    cs->b0 = buffer + mirror(-1-1, height-1)*stride;
452
    cs->b1 = buffer + mirror(-1  , height-1)*stride;
453
    cs->y = -1;
454
}
455

  
456
static void spatial_compose53i_dy_buffered(DWTCompose *cs, slice_buffer * sb, int width, int height, int stride_line){
457
    int y= cs->y;
458

  
459
    IDWTELEM *b0= cs->b0;
460
    IDWTELEM *b1= cs->b1;
461
    IDWTELEM *b2= slice_buffer_get_line(sb, mirror(y+1, height-1) * stride_line);
462
    IDWTELEM *b3= slice_buffer_get_line(sb, mirror(y+2, height-1) * stride_line);
463

  
464
    if(y+1<(unsigned)height && y<(unsigned)height){
465
        int x;
466

  
467
        for(x=0; x<width; x++){
468
            b2[x] -= (b1[x] + b3[x] + 2)>>2;
469
            b1[x] += (b0[x] + b2[x])>>1;
470
        }
471
    }else{
472
        if(y+1<(unsigned)height) vertical_compose53iL0(b1, b2, b3, width);
473
        if(y+0<(unsigned)height) vertical_compose53iH0(b0, b1, b2, width);
474
    }
475

  
476
        if(y-1<(unsigned)height) horizontal_compose53i(b0, width);
477
        if(y+0<(unsigned)height) horizontal_compose53i(b1, width);
478

  
479
    cs->b0 = b2;
480
    cs->b1 = b3;
481
    cs->y += 2;
482
}
483

  
484
static void spatial_compose53i_dy(DWTCompose *cs, IDWTELEM *buffer, int width, int height, int stride){
485
    int y= cs->y;
486
    IDWTELEM *b0= cs->b0;
487
    IDWTELEM *b1= cs->b1;
488
    IDWTELEM *b2= buffer + mirror(y+1, height-1)*stride;
489
    IDWTELEM *b3= buffer + mirror(y+2, height-1)*stride;
490

  
491
        if(y+1<(unsigned)height) vertical_compose53iL0(b1, b2, b3, width);
492
        if(y+0<(unsigned)height) vertical_compose53iH0(b0, b1, b2, width);
493

  
494
        if(y-1<(unsigned)height) horizontal_compose53i(b0, width);
495
        if(y+0<(unsigned)height) horizontal_compose53i(b1, width);
496

  
497
    cs->b0 = b2;
498
    cs->b1 = b3;
499
    cs->y += 2;
500
}
501

  
502
static void av_unused spatial_compose53i(IDWTELEM *buffer, int width, int height, int stride){
503
    DWTCompose cs;
504
    spatial_compose53i_init(&cs, buffer, height, stride);
505
    while(cs.y <= height)
506
        spatial_compose53i_dy(&cs, buffer, width, height, stride);
507
}
508

  
509

  
510
void ff_snow_horizontal_compose97i(IDWTELEM *b, int width){
511
    IDWTELEM temp[width];
512
    const int w2= (width+1)>>1;
513

  
514
#if 0 //maybe more understadable but slower
515
    inv_lift (temp   , b      , b   +w2, 2, 1, 1, width,  W_DM, W_DO, W_DS, 0, 1);
516
    inv_lift (temp+1 , b   +w2, temp   , 2, 1, 2, width,  W_CM, W_CO, W_CS, 1, 1);
517

  
518
    inv_liftS(b      , temp   , temp+1 , 2, 2, 2, width,  W_BM, W_BO, W_BS, 0, 1);
519
    inv_lift (b+1    , temp+1 , b      , 2, 2, 2, width,  W_AM, W_AO, W_AS, 1, 0);
520
#else
521
    int x;
522
    temp[0] = b[0] - ((3*b[w2]+2)>>2);
523
    for(x=1; x<(width>>1); x++){
524
        temp[2*x  ] = b[x     ] - ((3*(b   [x+w2-1] + b[x+w2])+4)>>3);
525
        temp[2*x-1] = b[x+w2-1] - temp[2*x-2] - temp[2*x];
526
    }
527
    if(width&1){
528
        temp[2*x  ] = b[x     ] - ((3*b   [x+w2-1]+2)>>2);
529
        temp[2*x-1] = b[x+w2-1] - temp[2*x-2] - temp[2*x];
530
    }else
531
        temp[2*x-1] = b[x+w2-1] - 2*temp[2*x-2];
532

  
533
    b[0] = temp[0] + ((2*temp[0] + temp[1]+4)>>3);
534
    for(x=2; x<width-1; x+=2){
535
        b[x  ] = temp[x  ] + ((4*temp[x  ] + temp[x-1] + temp[x+1]+8)>>4);
536
        b[x-1] = temp[x-1] + ((3*(b  [x-2] + b   [x  ] ))>>1);
537
    }
538
    if(width&1){
539
        b[x  ] = temp[x  ] + ((2*temp[x  ] + temp[x-1]+4)>>3);
540
        b[x-1] = temp[x-1] + ((3*(b  [x-2] + b   [x  ] ))>>1);
541
    }else
542
        b[x-1] = temp[x-1] + 3*b [x-2];
543
#endif
544
}
545

  
546
static void vertical_compose97iH0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width){
547
    int i;
548

  
549
    for(i=0; i<width; i++){
550
        b1[i] += (W_AM*(b0[i] + b2[i])+W_AO)>>W_AS;
551
    }
552
}
553

  
554
static void vertical_compose97iH1(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width){
555
    int i;
556

  
557
    for(i=0; i<width; i++){
558
        b1[i] -= (W_CM*(b0[i] + b2[i])+W_CO)>>W_CS;
559
    }
560
}
561

  
562
static void vertical_compose97iL0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width){
563
    int i;
564

  
565
    for(i=0; i<width; i++){
566
#ifdef liftS
567
        b1[i] += (W_BM*(b0[i] + b2[i])+W_BO)>>W_BS;
568
#else
569
        b1[i] += (W_BM*(b0[i] + b2[i])+4*b1[i]+W_BO)>>W_BS;
570
#endif
571
    }
572
}
573

  
574
static void vertical_compose97iL1(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width){
575
    int i;
576

  
577
    for(i=0; i<width; i++){
578
        b1[i] -= (W_DM*(b0[i] + b2[i])+W_DO)>>W_DS;
579
    }
580
}
581

  
582
void ff_snow_vertical_compose97i(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, IDWTELEM *b3, IDWTELEM *b4, IDWTELEM *b5, int width){
583
    int i;
584

  
585
    for(i=0; i<width; i++){
586
        b4[i] -= (W_DM*(b3[i] + b5[i])+W_DO)>>W_DS;
587
        b3[i] -= (W_CM*(b2[i] + b4[i])+W_CO)>>W_CS;
588
#ifdef liftS
589
        b2[i] += (W_BM*(b1[i] + b3[i])+W_BO)>>W_BS;
590
#else
591
        b2[i] += (W_BM*(b1[i] + b3[i])+4*b2[i]+W_BO)>>W_BS;
592
#endif
593
        b1[i] += (W_AM*(b0[i] + b2[i])+W_AO)>>W_AS;
594
    }
595
}
596

  
597
static void spatial_compose97i_buffered_init(DWTCompose *cs, slice_buffer * sb, int height, int stride_line){
598
    cs->b0 = slice_buffer_get_line(sb, mirror(-3-1, height-1) * stride_line);
599
    cs->b1 = slice_buffer_get_line(sb, mirror(-3  , height-1) * stride_line);
600
    cs->b2 = slice_buffer_get_line(sb, mirror(-3+1, height-1) * stride_line);
601
    cs->b3 = slice_buffer_get_line(sb, mirror(-3+2, height-1) * stride_line);
602
    cs->y = -3;
603
}
604

  
605
static void spatial_compose97i_init(DWTCompose *cs, IDWTELEM *buffer, int height, int stride){
606
    cs->b0 = buffer + mirror(-3-1, height-1)*stride;
607
    cs->b1 = buffer + mirror(-3  , height-1)*stride;
608
    cs->b2 = buffer + mirror(-3+1, height-1)*stride;
609
    cs->b3 = buffer + mirror(-3+2, height-1)*stride;
610
    cs->y = -3;
611
}
612

  
613
static void spatial_compose97i_dy_buffered(DWTContext *dsp, DWTCompose *cs, slice_buffer * sb, int width, int height, int stride_line){
614
    int y = cs->y;
615

  
616
    IDWTELEM *b0= cs->b0;
617
    IDWTELEM *b1= cs->b1;
618
    IDWTELEM *b2= cs->b2;
619
    IDWTELEM *b3= cs->b3;
620
    IDWTELEM *b4= slice_buffer_get_line(sb, mirror(y + 3, height - 1) * stride_line);
621
    IDWTELEM *b5= slice_buffer_get_line(sb, mirror(y + 4, height - 1) * stride_line);
622

  
623
    if(y>0 && y+4<height){
624
        dsp->vertical_compose97i(b0, b1, b2, b3, b4, b5, width);
625
    }else{
626
        if(y+3<(unsigned)height) vertical_compose97iL1(b3, b4, b5, width);
627
        if(y+2<(unsigned)height) vertical_compose97iH1(b2, b3, b4, width);
628
        if(y+1<(unsigned)height) vertical_compose97iL0(b1, b2, b3, width);
629
        if(y+0<(unsigned)height) vertical_compose97iH0(b0, b1, b2, width);
630
    }
631

  
632
    if(y-1<(unsigned)height) dsp->horizontal_compose97i(b0, width);
633
    if(y+0<(unsigned)height) dsp->horizontal_compose97i(b1, width);
634

  
635
    cs->b0=b2;
636
    cs->b1=b3;
637
    cs->b2=b4;
638
    cs->b3=b5;
639
    cs->y += 2;
640
}
641

  
642
static void spatial_compose97i_dy(DWTCompose *cs, IDWTELEM *buffer, int width, int height, int stride){
643
    int y = cs->y;
644
    IDWTELEM *b0= cs->b0;
645
    IDWTELEM *b1= cs->b1;
646
    IDWTELEM *b2= cs->b2;
647
    IDWTELEM *b3= cs->b3;
648
    IDWTELEM *b4= buffer + mirror(y+3, height-1)*stride;
649
    IDWTELEM *b5= buffer + mirror(y+4, height-1)*stride;
650

  
651
    if(y+3<(unsigned)height) vertical_compose97iL1(b3, b4, b5, width);
652
    if(y+2<(unsigned)height) vertical_compose97iH1(b2, b3, b4, width);
653
    if(y+1<(unsigned)height) vertical_compose97iL0(b1, b2, b3, width);
654
    if(y+0<(unsigned)height) vertical_compose97iH0(b0, b1, b2, width);
655

  
656
    if(y-1<(unsigned)height) ff_snow_horizontal_compose97i(b0, width);
657
    if(y+0<(unsigned)height) ff_snow_horizontal_compose97i(b1, width);
658

  
659
    cs->b0=b2;
660
    cs->b1=b3;
661
    cs->b2=b4;
662
    cs->b3=b5;
663
    cs->y += 2;
664
}
665

  
666
static void av_unused spatial_compose97i(IDWTELEM *buffer, int width, int height, int stride){
667
    DWTCompose cs;
668
    spatial_compose97i_init(&cs, buffer, height, stride);
669
    while(cs.y <= height)
670
        spatial_compose97i_dy(&cs, buffer, width, height, stride);
671
}
672

  
673
void ff_spatial_idwt_buffered_init(DWTCompose *cs, slice_buffer * sb, int width, int height, int stride_line, int type, int decomposition_count){
674
    int level;
675
    for(level=decomposition_count-1; level>=0; level--){
676
        switch(type){
677
        case DWT_97: spatial_compose97i_buffered_init(cs+level, sb, height>>level, stride_line<<level); break;
678
        case DWT_53: spatial_compose53i_buffered_init(cs+level, sb, height>>level, stride_line<<level); break;
679
        }
680
    }
681
}
682

  
683
void ff_spatial_idwt_buffered_slice(DWTContext *dsp, DWTCompose *cs, slice_buffer * slice_buf, int width, int height, int stride_line, int type, int decomposition_count, int y){
684
    const int support = type==1 ? 3 : 5;
685
    int level;
686
    if(type==2) return;
687

  
688
    for(level=decomposition_count-1; level>=0; level--){
689
        while(cs[level].y <= FFMIN((y>>level)+support, height>>level)){
690
            switch(type){
691
            case DWT_97: spatial_compose97i_dy_buffered(dsp, cs+level, slice_buf, width>>level, height>>level, stride_line<<level);
692
                break;
693
            case DWT_53: spatial_compose53i_dy_buffered(cs+level, slice_buf, width>>level, height>>level, stride_line<<level);
694
                break;
695
            }
696
        }
697
    }
698
}
699

  
700
void ff_spatial_idwt_init(DWTCompose *cs, IDWTELEM *buffer, int width, int height, int stride, int type, int decomposition_count){
701
    int level;
702
    for(level=decomposition_count-1; level>=0; level--){
703
        switch(type){
704
        case DWT_97: spatial_compose97i_init(cs+level, buffer, height>>level, stride<<level); break;
705
        case DWT_53: spatial_compose53i_init(cs+level, buffer, height>>level, stride<<level); break;
706
        }
707
    }
708
}
709

  
710
void ff_spatial_idwt_slice(DWTCompose *cs, IDWTELEM *buffer, int width, int height, int stride, int type, int decomposition_count, int y){
711
    const int support = type==1 ? 3 : 5;
712
    int level;
713
    if(type==2) return;
714

  
715
    for(level=decomposition_count-1; level>=0; level--){
716
        while(cs[level].y <= FFMIN((y>>level)+support, height>>level)){
717
            switch(type){
718
            case DWT_97: spatial_compose97i_dy(cs+level, buffer, width>>level, height>>level, stride<<level);
719
                break;
720
            case DWT_53: spatial_compose53i_dy(cs+level, buffer, width>>level, height>>level, stride<<level);
721
                break;
722
            }
723
        }
724
    }
725
}
726

  
727
void ff_spatial_idwt(IDWTELEM *buffer, int width, int height, int stride, int type, int decomposition_count){
728
        DWTCompose cs[MAX_DECOMPOSITIONS];
729
        int y;
730
        ff_spatial_idwt_init(cs, buffer, width, height, stride, type, decomposition_count);
731
        for(y=0; y<height; y+=4)
732
            ff_spatial_idwt_slice(cs, buffer, width, height, stride, type, decomposition_count, y);
733
}
734

  
735
static inline int w_c(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int w, int h, int type){
736
    int s, i, j;
737
    const int dec_count= w==8 ? 3 : 4;
738
    int tmp[32*32];
739
    int level, ori;
740
    static const int scale[2][2][4][4]={
741
      {
742
        {
743
            // 9/7 8x8 dec=3
744
            {268, 239, 239, 213},
745
            {  0, 224, 224, 152},
746
            {  0, 135, 135, 110},
747
        },{
748
            // 9/7 16x16 or 32x32 dec=4
749
            {344, 310, 310, 280},
750
            {  0, 320, 320, 228},
751
            {  0, 175, 175, 136},
752
            {  0, 129, 129, 102},
753
        }
754
      },{
755
        {
756
            // 5/3 8x8 dec=3
757
            {275, 245, 245, 218},
758
            {  0, 230, 230, 156},
759
            {  0, 138, 138, 113},
760
        },{
761
            // 5/3 16x16 or 32x32 dec=4
762
            {352, 317, 317, 286},
763
            {  0, 328, 328, 233},
764
            {  0, 180, 180, 140},
765
            {  0, 132, 132, 105},
766
        }
767
      }
768
    };
769

  
770
    for (i = 0; i < h; i++) {
771
        for (j = 0; j < w; j+=4) {
772
            tmp[32*i+j+0] = (pix1[j+0] - pix2[j+0])<<4;
773
            tmp[32*i+j+1] = (pix1[j+1] - pix2[j+1])<<4;
774
            tmp[32*i+j+2] = (pix1[j+2] - pix2[j+2])<<4;
775
            tmp[32*i+j+3] = (pix1[j+3] - pix2[j+3])<<4;
776
        }
777
        pix1 += line_size;
778
        pix2 += line_size;
779
    }
780

  
781
    ff_spatial_dwt(tmp, w, h, 32, type, dec_count);
782

  
783
    s=0;
784
    assert(w==h);
785
    for(level=0; level<dec_count; level++){
786
        for(ori= level ? 1 : 0; ori<4; ori++){
787
            int size= w>>(dec_count-level);
788
            int sx= (ori&1) ? size : 0;
789
            int stride= 32<<(dec_count-level);
790
            int sy= (ori&2) ? stride>>1 : 0;
791

  
792
            for(i=0; i<size; i++){
793
                for(j=0; j<size; j++){
794
                    int v= tmp[sx + sy + i*stride + j] * scale[type][dec_count-3][level][ori];
795
                    s += FFABS(v);
796
                }
797
            }
798
        }
799
    }
800
    assert(s>=0);
801
    return s>>9;
802
}
803

  
804
static int w53_8_c(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int h){
805
    return w_c(v, pix1, pix2, line_size,  8, h, 1);
806
}
807

  
808
static int w97_8_c(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int h){
809
    return w_c(v, pix1, pix2, line_size,  8, h, 0);
810
}
811

  
812
static int w53_16_c(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int h){
813
    return w_c(v, pix1, pix2, line_size, 16, h, 1);
814
}
815

  
816
static int w97_16_c(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int h){
817
    return w_c(v, pix1, pix2, line_size, 16, h, 0);
818
}
819

  
820
int w53_32_c(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int h){
821
    return w_c(v, pix1, pix2, line_size, 32, h, 1);
822
}
823

  
824
int w97_32_c(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int h){
825
    return w_c(v, pix1, pix2, line_size, 32, h, 0);
826
}
827

  
828
void ff_dsputil_init_dwt(DSPContext *c)
829
{
830
    c->w53[0]= w53_16_c;
831
    c->w53[1]= w53_8_c;
832
    c->w97[0]= w97_16_c;
833
    c->w97[1]= w97_8_c;
834
}
835

  
836
void ff_dwt_init(DWTContext *c)
837
{
838
    c->vertical_compose97i = ff_snow_vertical_compose97i;
839
    c->horizontal_compose97i = ff_snow_horizontal_compose97i;
840
    c->inner_add_yblock = ff_snow_inner_add_yblock;
841

  
842
    if (ARCH_X86) ff_dwt_init_x86(c);
843
}
libavcodec/dwt.h
1
/*
2
 * Copyright (C) 2004-2010 Michael Niedermayer <michaelni@gmx.at>
3
 *
4
 * This file is part of FFmpeg.
5
 *
6
 * FFmpeg is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2.1 of the License, or (at your option) any later version.
10
 *
11
 * FFmpeg is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with FFmpeg; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
 */
20

  
21
#ifndef AVCODEC_DWT_H
22
#define AVCODEC_DWT_H
23

  
24
#include <stdint.h>
25

  
26
typedef int DWTELEM;
27
typedef short IDWTELEM;
28

  
29
typedef struct {
30
    IDWTELEM *b0;
31
    IDWTELEM *b1;
32
    IDWTELEM *b2;
33
    IDWTELEM *b3;
34
    int y;
35
} DWTCompose;
36

  
37
/** Used to minimize the amount of memory used in order to optimize cache performance. **/
38
typedef struct slice_buffer_s {
39
    IDWTELEM * * line; ///< For use by idwt and predict_slices.
40
    IDWTELEM * * data_stack; ///< Used for internal purposes.
41
    int data_stack_top;
42
    int line_count;
43
    int line_width;
44
    int data_count;
45
    IDWTELEM * base_buffer; ///< Buffer that this structure is caching.
46
} slice_buffer;
47

  
48
typedef struct DWTContext {
49
    void (*vertical_compose97i)(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, IDWTELEM *b3, IDWTELEM *b4, IDWTELEM *b5, int width);
50
    void (*horizontal_compose97i)(IDWTELEM *b, int width);
51
    void (*inner_add_yblock)(const uint8_t *obmc, const int obmc_stride, uint8_t * * block, int b_w, int b_h, int src_x, int src_y, int src_stride, slice_buffer * sb, int add, uint8_t * dst8);
52
} DWTContext;
53

  
54
#define MAX_DECOMPOSITIONS 8
55

  
56
#define DWT_97 0
57
#define DWT_53 1
58

  
59
#define liftS lift
60
#if 1
61
#define W_AM 3
62
#define W_AO 0
63
#define W_AS 1
64

  
65
#undef liftS
66
#define W_BM 1
67
#define W_BO 8
68
#define W_BS 4
69

  
70
#define W_CM 1
71
#define W_CO 0
72
#define W_CS 0
73

  
74
#define W_DM 3
75
#define W_DO 4
76
#define W_DS 3
77
#elif 0
78
#define W_AM 55
79
#define W_AO 16
80
#define W_AS 5
81

  
82
#define W_BM 3
83
#define W_BO 32
84
#define W_BS 6
85

  
86
#define W_CM 127
87
#define W_CO 64
88
#define W_CS 7
89

  
90
#define W_DM 7
91
#define W_DO 8
92
#define W_DS 4
93
#elif 0
94
#define W_AM 97
95
#define W_AO 32
96
#define W_AS 6
97

  
98
#define W_BM 63
99
#define W_BO 512
100
#define W_BS 10
101

  
102
#define W_CM 13
103
#define W_CO 8
104
#define W_CS 4
105

  
106
#define W_DM 15
107
#define W_DO 16
108
#define W_DS 5
109

  
110
#else
111

  
112
#define W_AM 203
113
#define W_AO 64
114
#define W_AS 7
115

  
116
#define W_BM 217
117
#define W_BO 2048
118
#define W_BS 12
119

  
120
#define W_CM 113
121
#define W_CO 64
122
#define W_CS 7
123

  
124
#define W_DM 227
125
#define W_DO 128
126
#define W_DS 9
127
#endif
128

  
129
#define slice_buffer_get_line(slice_buf, line_num) ((slice_buf)->line[line_num] ? (slice_buf)->line[line_num] : slice_buffer_load_line((slice_buf), (line_num)))
130
//#define slice_buffer_get_line(slice_buf, line_num) (slice_buffer_load_line((slice_buf), (line_num)))
131

  
132
void slice_buffer_init(slice_buffer * buf, int line_count, int max_allocated_lines, int line_width, IDWTELEM * base_buffer);
133
void slice_buffer_release(slice_buffer * buf, int line);
134
void slice_buffer_flush(slice_buffer * buf);
135
void slice_buffer_destroy(slice_buffer * buf);
136
IDWTELEM * slice_buffer_load_line(slice_buffer * buf, int line);
137

  
138
void ff_snow_vertical_compose97i(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, IDWTELEM *b3, IDWTELEM *b4, IDWTELEM *b5, int width);
139
void ff_snow_horizontal_compose97i(IDWTELEM *b, int width);
140
void ff_snow_inner_add_yblock(const uint8_t *obmc, const int obmc_stride, uint8_t * * block, int b_w, int b_h, int src_x, int src_y, int src_stride, slice_buffer * sb, int add, uint8_t * dst8);
141

  
142
int w53_32_c(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int h);
143
int w97_32_c(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int h);
144

  
145
void ff_spatial_dwt(int *buffer, int width, int height, int stride, int type, int decomposition_count);
146

  
147
void ff_spatial_idwt_buffered_init(DWTCompose *cs, slice_buffer * sb, int width, int height, int stride_line, int type, int decomposition_count);
148
void ff_spatial_idwt_buffered_slice(DWTContext *dsp, DWTCompose *cs, slice_buffer * slice_buf, int width, int height, int stride_line, int type, int decomposition_count, int y);
149
void ff_spatial_idwt_init(DWTCompose *cs, IDWTELEM *buffer, int width, int height, int stride, int type, int decomposition_count);
150
void ff_spatial_idwt_slice(DWTCompose *cs, IDWTELEM *buffer, int width, int height, int stride, int type, int decomposition_count, int y);
151
void ff_spatial_idwt(IDWTELEM *buffer, int width, int height, int stride, int type, int decomposition_count);
152

  
153
void ff_dwt_init(DWTContext *c);
154
void ff_dwt_init_x86(DWTContext *c);
155

  
156
#endif /* AVCODEC_DWT_H */
libavcodec/ivi_dsp.c
28 28

  
29 29
#include "avcodec.h"
30 30
#include "dsputil.h"
31
#include "dwt.h"
31 32
#include "ivi_common.h"
32 33
#include "ivi_dsp.h"
33 34

  
libavcodec/snow.c
21 21
#include "libavutil/intmath.h"
22 22
#include "avcodec.h"
23 23
#include "dsputil.h"
24
#include "dwt.h"
24 25
#include "snow.h"
25 26

  
26 27
#include "rangecoder.h"
......
440 441
    AVCodecContext *avctx;
441 442
    RangeCoder c;
442 443
    DSPContext dsp;
444
    DWTContext dwt;
443 445
    AVFrame new_picture;
444 446
    AVFrame input_picture;              ///< new_picture with the internal linesizes
445 447
    AVFrame current_picture;
......
494 496
    uint8_t *scratchbuf;
495 497
}SnowContext;
496 498

  
497
typedef struct {
498
    IDWTELEM *b0;
499
    IDWTELEM *b1;
500
    IDWTELEM *b2;
501
    IDWTELEM *b3;
502
    int y;
503
} DWTCompose;
504

  
505
#define slice_buffer_get_line(slice_buf, line_num) ((slice_buf)->line[line_num] ? (slice_buf)->line[line_num] : slice_buffer_load_line((slice_buf), (line_num)))
506
//#define slice_buffer_get_line(slice_buf, line_num) (slice_buffer_load_line((slice_buf), (line_num)))
507

  
508
static void slice_buffer_init(slice_buffer * buf, int line_count, int max_allocated_lines, int line_width, IDWTELEM * base_buffer)
509
{
510
    int i;
511

  
512
    buf->base_buffer = base_buffer;
513
    buf->line_count = line_count;
514
    buf->line_width = line_width;
515
    buf->data_count = max_allocated_lines;
516
    buf->line = av_mallocz (sizeof(IDWTELEM *) * line_count);
517
    buf->data_stack = av_malloc (sizeof(IDWTELEM *) * max_allocated_lines);
518

  
519
    for(i = 0; i < max_allocated_lines; i++){
520
        buf->data_stack[i] = av_malloc (sizeof(IDWTELEM) * line_width);
521
    }
522

  
523
    buf->data_stack_top = max_allocated_lines - 1;
524
}
525

  
526
static IDWTELEM * slice_buffer_load_line(slice_buffer * buf, int line)
527
{
528
    IDWTELEM * buffer;
529

  
530
    assert(buf->data_stack_top >= 0);
531
//  assert(!buf->line[line]);
532
    if (buf->line[line])
533
        return buf->line[line];
534

  
535
    buffer = buf->data_stack[buf->data_stack_top];
536
    buf->data_stack_top--;
537
    buf->line[line] = buffer;
538

  
539
    return buffer;
540
}
541

  
542
static void slice_buffer_release(slice_buffer * buf, int line)
543
{
544
    IDWTELEM * buffer;
545

  
546
    assert(line >= 0 && line < buf->line_count);
547
    assert(buf->line[line]);
548

  
549
    buffer = buf->line[line];
550
    buf->data_stack_top++;
551
    buf->data_stack[buf->data_stack_top] = buffer;
552
    buf->line[line] = NULL;
553
}
554

  
555
static void slice_buffer_flush(slice_buffer * buf)
556
{
557
    int i;
558
    for(i = 0; i < buf->line_count; i++){
559
        if (buf->line[i])
560
            slice_buffer_release(buf, i);
561
    }
562
}
563

  
564
static void slice_buffer_destroy(slice_buffer * buf)
565
{
566
    int i;
567
    slice_buffer_flush(buf);
568

  
569
    for(i = buf->data_count - 1; i >= 0; i--){
570
        av_freep(&buf->data_stack[i]);
571
    }
572
    av_freep(&buf->data_stack);
573
    av_freep(&buf->line);
574
}
575

  
576 499
#ifdef __sgi
577 500
// Avoid a name clash on SGI IRIX
578 501
#undef qexp
......
580 503
#define QEXPSHIFT (7-FRAC_BITS+8) //FIXME try to change this to 0
581 504
static uint8_t qexp[QROOT];
582 505

  
583
static inline int mirror(int v, int m){
584
    while((unsigned)v > (unsigned)m){
585
        v=-v;
586
        if(v<0) v+= 2*m;
587
    }
588
    return v;
589
}
590

  
591 506
static inline void put_symbol(RangeCoder *c, uint8_t *state, int v, int is_signed){
592 507
    int i;
593 508

  
......
709 624
    return v;
710 625
}
711 626

  
712
static av_always_inline void
713
lift(DWTELEM *dst, DWTELEM *src, DWTELEM *ref,
714
     int dst_step, int src_step, int ref_step,
715
     int width, int mul, int add, int shift,
716
     int highpass, int inverse){
717
    const int mirror_left= !highpass;
718
    const int mirror_right= (width&1) ^ highpass;
719
    const int w= (width>>1) - 1 + (highpass & width);
720
    int i;
721

  
722
#define LIFT(src, ref, inv) ((src) + ((inv) ? - (ref) : + (ref)))
723
    if(mirror_left){
724
        dst[0] = LIFT(src[0], ((mul*2*ref[0]+add)>>shift), inverse);
725
        dst += dst_step;
726
        src += src_step;
727
    }
728

  
729
    for(i=0; i<w; i++){
730
        dst[i*dst_step] =
731
            LIFT(src[i*src_step],
732
                 ((mul*(ref[i*ref_step] + ref[(i+1)*ref_step])+add)>>shift),
733
                 inverse);
734
    }
735

  
736
    if(mirror_right){
737
        dst[w*dst_step] =
738
            LIFT(src[w*src_step],
739
                 ((mul*2*ref[w*ref_step]+add)>>shift),
740
                 inverse);
741
    }
742
}
743

  
744
static av_always_inline void
745
inv_lift(IDWTELEM *dst, IDWTELEM *src, IDWTELEM *ref,
746
         int dst_step, int src_step, int ref_step,
747
         int width, int mul, int add, int shift,
748
         int highpass, int inverse){
749
    const int mirror_left= !highpass;
750
    const int mirror_right= (width&1) ^ highpass;
751
    const int w= (width>>1) - 1 + (highpass & width);
752
    int i;
753

  
754
#define LIFT(src, ref, inv) ((src) + ((inv) ? - (ref) : + (ref)))
755
    if(mirror_left){
756
        dst[0] = LIFT(src[0], ((mul*2*ref[0]+add)>>shift), inverse);
757
        dst += dst_step;
758
        src += src_step;
759
    }
760

  
761
    for(i=0; i<w; i++){
762
        dst[i*dst_step] =
763
            LIFT(src[i*src_step],
764
                 ((mul*(ref[i*ref_step] + ref[(i+1)*ref_step])+add)>>shift),
765
                 inverse);
766
    }
767

  
768
    if(mirror_right){
769
        dst[w*dst_step] =
770
            LIFT(src[w*src_step],
771
                 ((mul*2*ref[w*ref_step]+add)>>shift),
772
                 inverse);
773
    }
774
}
775

  
776
#ifndef liftS
777
static av_always_inline void
778
liftS(DWTELEM *dst, DWTELEM *src, DWTELEM *ref,
779
      int dst_step, int src_step, int ref_step,
780
      int width, int mul, int add, int shift,
781
      int highpass, int inverse){
782
    const int mirror_left= !highpass;
783
    const int mirror_right= (width&1) ^ highpass;
784
    const int w= (width>>1) - 1 + (highpass & width);
785
    int i;
786

  
787
    assert(shift == 4);
788
#define LIFTS(src, ref, inv) \
789
        ((inv) ? \
790
            (src) + (((ref) + 4*(src))>>shift): \
791
            -((-16*(src) + (ref) + add/4 + 1 + (5<<25))/(5*4) - (1<<23)))
792
    if(mirror_left){
793
        dst[0] = LIFTS(src[0], mul*2*ref[0]+add, inverse);
794
        dst += dst_step;
795
        src += src_step;
796
    }
797

  
798
    for(i=0; i<w; i++){
799
        dst[i*dst_step] =
800
            LIFTS(src[i*src_step],
801
                  mul*(ref[i*ref_step] + ref[(i+1)*ref_step])+add,
802
                  inverse);
803
    }
804

  
805
    if(mirror_right){
806
        dst[w*dst_step] =
807
            LIFTS(src[w*src_step], mul*2*ref[w*ref_step]+add, inverse);
808
    }
809
}
810
static av_always_inline void
811
inv_liftS(IDWTELEM *dst, IDWTELEM *src, IDWTELEM *ref,
812
          int dst_step, int src_step, int ref_step,
813
          int width, int mul, int add, int shift,
814
          int highpass, int inverse){
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff