dirac: enable ff_spatial_idwt_init_mmx()
[ffmpeg.git] / libavcodec / dwt.c
1 /*
2  * Copyright (C) 2004-2010 Michael Niedermayer <michaelni@gmx.at>
3  * Copyright (C) 2008 David Conrad
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 #include "libavutil/attributes.h"
23 #include "dsputil.h"
24 #include "dwt.h"
25 #include "libavcodec/x86/dwt.h"
26
27 void ff_slice_buffer_init(slice_buffer * buf, int line_count, int max_allocated_lines, int line_width, IDWTELEM * base_buffer)
28 {
29     int i;
30
31     buf->base_buffer = base_buffer;
32     buf->line_count = line_count;
33     buf->line_width = line_width;
34     buf->data_count = max_allocated_lines;
35     buf->line = av_mallocz (sizeof(IDWTELEM *) * line_count);
36     buf->data_stack = av_malloc (sizeof(IDWTELEM *) * max_allocated_lines);
37
38     for(i = 0; i < max_allocated_lines; i++){
39         buf->data_stack[i] = av_malloc (sizeof(IDWTELEM) * line_width);
40     }
41
42     buf->data_stack_top = max_allocated_lines - 1;
43 }
44
45 IDWTELEM * ff_slice_buffer_load_line(slice_buffer * buf, int line)
46 {
47     IDWTELEM * buffer;
48
49     assert(buf->data_stack_top >= 0);
50 //  assert(!buf->line[line]);
51     if (buf->line[line])
52         return buf->line[line];
53
54     buffer = buf->data_stack[buf->data_stack_top];
55     buf->data_stack_top--;
56     buf->line[line] = buffer;
57
58     return buffer;
59 }
60
61 void ff_slice_buffer_release(slice_buffer * buf, int line)
62 {
63     IDWTELEM * buffer;
64
65     assert(line >= 0 && line < buf->line_count);
66     assert(buf->line[line]);
67
68     buffer = buf->line[line];
69     buf->data_stack_top++;
70     buf->data_stack[buf->data_stack_top] = buffer;
71     buf->line[line] = NULL;
72 }
73
74 void ff_slice_buffer_flush(slice_buffer * buf)
75 {
76     int i;
77     for(i = 0; i < buf->line_count; i++){
78         if (buf->line[i])
79             ff_slice_buffer_release(buf, i);
80     }
81 }
82
83 void ff_slice_buffer_destroy(slice_buffer * buf)
84 {
85     int i;
86     ff_slice_buffer_flush(buf);
87
88     for(i = buf->data_count - 1; i >= 0; i--){
89         av_freep(&buf->data_stack[i]);
90     }
91     av_freep(&buf->data_stack);
92     av_freep(&buf->line);
93 }
94
95 static inline int mirror(int v, int m){
96     while((unsigned)v > (unsigned)m){
97         v=-v;
98         if(v<0) v+= 2*m;
99     }
100     return v;
101 }
102
103 static av_always_inline void
104 lift(DWTELEM *dst, DWTELEM *src, DWTELEM *ref,
105      int dst_step, int src_step, int ref_step,
106      int width, int mul, int add, int shift,
107      int highpass, int inverse){
108     const int mirror_left= !highpass;
109     const int mirror_right= (width&1) ^ highpass;
110     const int w= (width>>1) - 1 + (highpass & width);
111     int i;
112
113 #define LIFT(src, ref, inv) ((src) + ((inv) ? - (ref) : + (ref)))
114     if(mirror_left){
115         dst[0] = LIFT(src[0], ((mul*2*ref[0]+add)>>shift), inverse);
116         dst += dst_step;
117         src += src_step;
118     }
119
120     for(i=0; i<w; i++){
121         dst[i*dst_step] =
122             LIFT(src[i*src_step],
123                  ((mul*(ref[i*ref_step] + ref[(i+1)*ref_step])+add)>>shift),
124                  inverse);
125     }
126
127     if(mirror_right){
128         dst[w*dst_step] =
129             LIFT(src[w*src_step],
130                  ((mul*2*ref[w*ref_step]+add)>>shift),
131                  inverse);
132     }
133 }
134
135 static av_always_inline void
136 inv_lift(IDWTELEM *dst, IDWTELEM *src, IDWTELEM *ref,
137          int dst_step, int src_step, int ref_step,
138          int width, int mul, int add, int shift,
139          int highpass, int inverse){
140     const int mirror_left= !highpass;
141     const int mirror_right= (width&1) ^ highpass;
142     const int w= (width>>1) - 1 + (highpass & width);
143     int i;
144
145 #define LIFT(src, ref, inv) ((src) + ((inv) ? - (ref) : + (ref)))
146     if(mirror_left){
147         dst[0] = LIFT(src[0], ((mul*2*ref[0]+add)>>shift), inverse);
148         dst += dst_step;
149         src += src_step;
150     }
151
152     for(i=0; i<w; i++){
153         dst[i*dst_step] =
154             LIFT(src[i*src_step],
155                  ((mul*(ref[i*ref_step] + ref[(i+1)*ref_step])+add)>>shift),
156                  inverse);
157     }
158
159     if(mirror_right){
160         dst[w*dst_step] =
161             LIFT(src[w*src_step],
162                  ((mul*2*ref[w*ref_step]+add)>>shift),
163                  inverse);
164     }
165 }
166
167 #ifndef liftS
168 static av_always_inline void
169 liftS(DWTELEM *dst, DWTELEM *src, DWTELEM *ref,
170       int dst_step, int src_step, int ref_step,
171       int width, int mul, int add, int shift,
172       int highpass, int inverse){
173     const int mirror_left= !highpass;
174     const int mirror_right= (width&1) ^ highpass;
175     const int w= (width>>1) - 1 + (highpass & width);
176     int i;
177
178     assert(shift == 4);
179 #define LIFTS(src, ref, inv) \
180         ((inv) ? \
181             (src) + (((ref) + 4*(src))>>shift): \
182             -((-16*(src) + (ref) + add/4 + 1 + (5<<25))/(5*4) - (1<<23)))
183     if(mirror_left){
184         dst[0] = LIFTS(src[0], mul*2*ref[0]+add, inverse);
185         dst += dst_step;
186         src += src_step;
187     }
188
189     for(i=0; i<w; i++){
190         dst[i*dst_step] =
191             LIFTS(src[i*src_step],
192                   mul*(ref[i*ref_step] + ref[(i+1)*ref_step])+add,
193                   inverse);
194     }
195
196     if(mirror_right){
197         dst[w*dst_step] =
198             LIFTS(src[w*src_step], mul*2*ref[w*ref_step]+add, inverse);
199     }
200 }
201 static av_always_inline void
202 inv_liftS(IDWTELEM *dst, IDWTELEM *src, IDWTELEM *ref,
203           int dst_step, int src_step, int ref_step,
204           int width, int mul, int add, int shift,
205           int highpass, int inverse){
206     const int mirror_left= !highpass;
207     const int mirror_right= (width&1) ^ highpass;
208     const int w= (width>>1) - 1 + (highpass & width);
209     int i;
210
211     assert(shift == 4);
212 #define LIFTS(src, ref, inv) \
213     ((inv) ? \
214         (src) + (((ref) + 4*(src))>>shift): \
215         -((-16*(src) + (ref) + add/4 + 1 + (5<<25))/(5*4) - (1<<23)))
216     if(mirror_left){
217         dst[0] = LIFTS(src[0], mul*2*ref[0]+add, inverse);
218         dst += dst_step;
219         src += src_step;
220     }
221
222     for(i=0; i<w; i++){
223         dst[i*dst_step] =
224             LIFTS(src[i*src_step],
225                   mul*(ref[i*ref_step] + ref[(i+1)*ref_step])+add,
226                   inverse);
227     }
228
229     if(mirror_right){
230         dst[w*dst_step] =
231             LIFTS(src[w*src_step], mul*2*ref[w*ref_step]+add, inverse);
232     }
233 }
234 #endif /* ! liftS */
235
236 static void horizontal_decompose53i(DWTELEM *b, int width){
237     DWTELEM temp[width];
238     const int width2= width>>1;
239     int x;
240     const int w2= (width+1)>>1;
241
242     for(x=0; x<width2; x++){
243         temp[x   ]= b[2*x    ];
244         temp[x+w2]= b[2*x + 1];
245     }
246     if(width&1)
247         temp[x   ]= b[2*x    ];
248 #if 0
249     {
250     int A1,A2,A3,A4;
251     A2= temp[1       ];
252     A4= temp[0       ];
253     A1= temp[0+width2];
254     A1 -= (A2 + A4)>>1;
255     A4 += (A1 + 1)>>1;
256     b[0+width2] = A1;
257     b[0       ] = A4;
258     for(x=1; x+1<width2; x+=2){
259         A3= temp[x+width2];
260         A4= temp[x+1     ];
261         A3 -= (A2 + A4)>>1;
262         A2 += (A1 + A3 + 2)>>2;
263         b[x+width2] = A3;
264         b[x       ] = A2;
265
266         A1= temp[x+1+width2];
267         A2= temp[x+2       ];
268         A1 -= (A2 + A4)>>1;
269         A4 += (A1 + A3 + 2)>>2;
270         b[x+1+width2] = A1;
271         b[x+1       ] = A4;
272     }
273     A3= temp[width-1];
274     A3 -= A2;
275     A2 += (A1 + A3 + 2)>>2;
276     b[width -1] = A3;
277     b[width2-1] = A2;
278     }
279 #else
280     lift(b+w2, temp+w2, temp, 1, 1, 1, width, -1, 0, 1, 1, 0);
281     lift(b   , temp   , b+w2, 1, 1, 1, width,  1, 2, 2, 0, 0);
282 #endif /* 0 */
283 }
284
285 static void vertical_decompose53iH0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
286     int i;
287
288     for(i=0; i<width; i++){
289         b1[i] -= (b0[i] + b2[i])>>1;
290     }
291 }
292
293 static void vertical_decompose53iL0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
294     int i;
295
296     for(i=0; i<width; i++){
297         b1[i] += (b0[i] + b2[i] + 2)>>2;
298     }
299 }
300
301 static void spatial_decompose53i(DWTELEM *buffer, int width, int height, int stride){
302     int y;
303     DWTELEM *b0= buffer + mirror(-2-1, height-1)*stride;
304     DWTELEM *b1= buffer + mirror(-2  , height-1)*stride;
305
306     for(y=-2; y<height; y+=2){
307         DWTELEM *b2= buffer + mirror(y+1, height-1)*stride;
308         DWTELEM *b3= buffer + mirror(y+2, height-1)*stride;
309
310         if(y+1<(unsigned)height) horizontal_decompose53i(b2, width);
311         if(y+2<(unsigned)height) horizontal_decompose53i(b3, width);
312
313         if(y+1<(unsigned)height) vertical_decompose53iH0(b1, b2, b3, width);
314         if(y+0<(unsigned)height) vertical_decompose53iL0(b0, b1, b2, width);
315
316         b0=b2;
317         b1=b3;
318     }
319 }
320
321 static void horizontal_decompose97i(DWTELEM *b, int width){
322     DWTELEM temp[width];
323     const int w2= (width+1)>>1;
324
325     lift (temp+w2, b    +1, b      , 1, 2, 2, width,  W_AM, W_AO, W_AS, 1, 1);
326     liftS(temp   , b      , temp+w2, 1, 2, 1, width,  W_BM, W_BO, W_BS, 0, 0);
327     lift (b   +w2, temp+w2, temp   , 1, 1, 1, width,  W_CM, W_CO, W_CS, 1, 0);
328     lift (b      , temp   , b   +w2, 1, 1, 1, width,  W_DM, W_DO, W_DS, 0, 0);
329 }
330
331
332 static void vertical_decompose97iH0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
333     int i;
334
335     for(i=0; i<width; i++){
336         b1[i] -= (W_AM*(b0[i] + b2[i])+W_AO)>>W_AS;
337     }
338 }
339
340 static void vertical_decompose97iH1(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
341     int i;
342
343     for(i=0; i<width; i++){
344         b1[i] += (W_CM*(b0[i] + b2[i])+W_CO)>>W_CS;
345     }
346 }
347
348 static void vertical_decompose97iL0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
349     int i;
350
351     for(i=0; i<width; i++){
352 #ifdef liftS
353         b1[i] -= (W_BM*(b0[i] + b2[i])+W_BO)>>W_BS;
354 #else
355         b1[i] = (16*4*b1[i] - 4*(b0[i] + b2[i]) + W_BO*5 + (5<<27)) / (5*16) - (1<<23);
356 #endif
357     }
358 }
359
360 static void vertical_decompose97iL1(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
361     int i;
362
363     for(i=0; i<width; i++){
364         b1[i] += (W_DM*(b0[i] + b2[i])+W_DO)>>W_DS;
365     }
366 }
367
368 static void spatial_decompose97i(DWTELEM *buffer, int width, int height, int stride){
369     int y;
370     DWTELEM *b0= buffer + mirror(-4-1, height-1)*stride;
371     DWTELEM *b1= buffer + mirror(-4  , height-1)*stride;
372     DWTELEM *b2= buffer + mirror(-4+1, height-1)*stride;
373     DWTELEM *b3= buffer + mirror(-4+2, height-1)*stride;
374
375     for(y=-4; y<height; y+=2){
376         DWTELEM *b4= buffer + mirror(y+3, height-1)*stride;
377         DWTELEM *b5= buffer + mirror(y+4, height-1)*stride;
378
379         if(y+3<(unsigned)height) horizontal_decompose97i(b4, width);
380         if(y+4<(unsigned)height) horizontal_decompose97i(b5, width);
381
382         if(y+3<(unsigned)height) vertical_decompose97iH0(b3, b4, b5, width);
383         if(y+2<(unsigned)height) vertical_decompose97iL0(b2, b3, b4, width);
384         if(y+1<(unsigned)height) vertical_decompose97iH1(b1, b2, b3, width);
385         if(y+0<(unsigned)height) vertical_decompose97iL1(b0, b1, b2, width);
386
387         b0=b2;
388         b1=b3;
389         b2=b4;
390         b3=b5;
391     }
392 }
393
394 void ff_spatial_dwt(DWTELEM *buffer, int width, int height, int stride, int type, int decomposition_count){
395     int level;
396
397     for(level=0; level<decomposition_count; level++){
398         switch(type){
399         case DWT_97: spatial_decompose97i(buffer, width>>level, height>>level, stride<<level); break;
400         case DWT_53: spatial_decompose53i(buffer, width>>level, height>>level, stride<<level); break;
401         }
402     }
403 }
404
405 static void horizontal_compose53i(IDWTELEM *b, int width){
406     IDWTELEM temp[width];
407     const int width2= width>>1;
408     const int w2= (width+1)>>1;
409     int x;
410
411     for(x=0; x<width2; x++){
412         temp[2*x    ]= b[x   ];
413         temp[2*x + 1]= b[x+w2];
414     }
415     if(width&1)
416         temp[2*x    ]= b[x   ];
417
418     b[0] = temp[0] - ((temp[1]+1)>>1);
419     for(x=2; x<width-1; x+=2){
420         b[x  ] = temp[x  ] - ((temp[x-1] + temp[x+1]+2)>>2);
421         b[x-1] = temp[x-1] + ((b   [x-2] + b   [x  ]+1)>>1);
422     }
423     if(width&1){
424         b[x  ] = temp[x  ] - ((temp[x-1]+1)>>1);
425         b[x-1] = temp[x-1] + ((b   [x-2] + b  [x  ]+1)>>1);
426     }else
427         b[x-1] = temp[x-1] + b[x-2];
428 }
429
430 static void vertical_compose53iH0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width){
431     int i;
432
433     for(i=0; i<width; i++){
434         b1[i] += (b0[i] + b2[i])>>1;
435     }
436 }
437
438 static void vertical_compose53iL0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width){
439     int i;
440
441     for(i=0; i<width; i++){
442         b1[i] -= (b0[i] + b2[i] + 2)>>2;
443     }
444 }
445
446 static void spatial_compose53i_buffered_init(DWTCompose *cs, slice_buffer * sb, int height, int stride_line){
447     cs->b0 = slice_buffer_get_line(sb, mirror(-1-1, height-1) * stride_line);
448     cs->b1 = slice_buffer_get_line(sb, mirror(-1  , height-1) * stride_line);
449     cs->y = -1;
450 }
451
452 static void spatial_compose53i_init(DWTCompose *cs, IDWTELEM *buffer, int height, int stride){
453     cs->b0 = buffer + mirror(-1-1, height-1)*stride;
454     cs->b1 = buffer + mirror(-1  , height-1)*stride;
455     cs->y = -1;
456 }
457
458 static void spatial_compose53i_dy_buffered(DWTCompose *cs, slice_buffer * sb, int width, int height, int stride_line){
459     int y= cs->y;
460
461     IDWTELEM *b0= cs->b0;
462     IDWTELEM *b1= cs->b1;
463     IDWTELEM *b2= slice_buffer_get_line(sb, mirror(y+1, height-1) * stride_line);
464     IDWTELEM *b3= slice_buffer_get_line(sb, mirror(y+2, height-1) * stride_line);
465
466     if(y+1<(unsigned)height && y<(unsigned)height){
467         int x;
468
469         for(x=0; x<width; x++){
470             b2[x] -= (b1[x] + b3[x] + 2)>>2;
471             b1[x] += (b0[x] + b2[x])>>1;
472         }
473     }else{
474         if(y+1<(unsigned)height) vertical_compose53iL0(b1, b2, b3, width);
475         if(y+0<(unsigned)height) vertical_compose53iH0(b0, b1, b2, width);
476     }
477
478         if(y-1<(unsigned)height) horizontal_compose53i(b0, width);
479         if(y+0<(unsigned)height) horizontal_compose53i(b1, width);
480
481     cs->b0 = b2;
482     cs->b1 = b3;
483     cs->y += 2;
484 }
485
486 static void spatial_compose53i_dy(DWTCompose *cs, IDWTELEM *buffer, int width, int height, int stride){
487     int y= cs->y;
488     IDWTELEM *b0= cs->b0;
489     IDWTELEM *b1= cs->b1;
490     IDWTELEM *b2= buffer + mirror(y+1, height-1)*stride;
491     IDWTELEM *b3= buffer + mirror(y+2, height-1)*stride;
492
493         if(y+1<(unsigned)height) vertical_compose53iL0(b1, b2, b3, width);
494         if(y+0<(unsigned)height) vertical_compose53iH0(b0, b1, b2, width);
495
496         if(y-1<(unsigned)height) horizontal_compose53i(b0, width);
497         if(y+0<(unsigned)height) horizontal_compose53i(b1, width);
498
499     cs->b0 = b2;
500     cs->b1 = b3;
501     cs->y += 2;
502 }
503
504 static void av_unused spatial_compose53i(IDWTELEM *buffer, int width, int height, int stride){
505     DWTCompose cs;
506     spatial_compose53i_init(&cs, buffer, height, stride);
507     while(cs.y <= height)
508         spatial_compose53i_dy(&cs, buffer, width, height, stride);
509 }
510
511
512 void ff_snow_horizontal_compose97i(IDWTELEM *b, int width){
513     IDWTELEM temp[width];
514     const int w2= (width+1)>>1;
515
516 #if 0 //maybe more understadable but slower
517     inv_lift (temp   , b      , b   +w2, 2, 1, 1, width,  W_DM, W_DO, W_DS, 0, 1);
518     inv_lift (temp+1 , b   +w2, temp   , 2, 1, 2, width,  W_CM, W_CO, W_CS, 1, 1);
519
520     inv_liftS(b      , temp   , temp+1 , 2, 2, 2, width,  W_BM, W_BO, W_BS, 0, 1);
521     inv_lift (b+1    , temp+1 , b      , 2, 2, 2, width,  W_AM, W_AO, W_AS, 1, 0);
522 #else
523     int x;
524     temp[0] = b[0] - ((3*b[w2]+2)>>2);
525     for(x=1; x<(width>>1); x++){
526         temp[2*x  ] = b[x     ] - ((3*(b   [x+w2-1] + b[x+w2])+4)>>3);
527         temp[2*x-1] = b[x+w2-1] - temp[2*x-2] - temp[2*x];
528     }
529     if(width&1){
530         temp[2*x  ] = b[x     ] - ((3*b   [x+w2-1]+2)>>2);
531         temp[2*x-1] = b[x+w2-1] - temp[2*x-2] - temp[2*x];
532     }else
533         temp[2*x-1] = b[x+w2-1] - 2*temp[2*x-2];
534
535     b[0] = temp[0] + ((2*temp[0] + temp[1]+4)>>3);
536     for(x=2; x<width-1; x+=2){
537         b[x  ] = temp[x  ] + ((4*temp[x  ] + temp[x-1] + temp[x+1]+8)>>4);
538         b[x-1] = temp[x-1] + ((3*(b  [x-2] + b   [x  ] ))>>1);
539     }
540     if(width&1){
541         b[x  ] = temp[x  ] + ((2*temp[x  ] + temp[x-1]+4)>>3);
542         b[x-1] = temp[x-1] + ((3*(b  [x-2] + b   [x  ] ))>>1);
543     }else
544         b[x-1] = temp[x-1] + 3*b [x-2];
545 #endif
546 }
547
548 static void vertical_compose97iH0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width){
549     int i;
550
551     for(i=0; i<width; i++){
552         b1[i] += (W_AM*(b0[i] + b2[i])+W_AO)>>W_AS;
553     }
554 }
555
556 static void vertical_compose97iH1(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width){
557     int i;
558
559     for(i=0; i<width; i++){
560         b1[i] -= (W_CM*(b0[i] + b2[i])+W_CO)>>W_CS;
561     }
562 }
563
564 static void vertical_compose97iL0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width){
565     int i;
566
567     for(i=0; i<width; i++){
568 #ifdef liftS
569         b1[i] += (W_BM*(b0[i] + b2[i])+W_BO)>>W_BS;
570 #else
571         b1[i] += (W_BM*(b0[i] + b2[i])+4*b1[i]+W_BO)>>W_BS;
572 #endif
573     }
574 }
575
576 static void vertical_compose97iL1(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width){
577     int i;
578
579     for(i=0; i<width; i++){
580         b1[i] -= (W_DM*(b0[i] + b2[i])+W_DO)>>W_DS;
581     }
582 }
583
584 void ff_snow_vertical_compose97i(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, IDWTELEM *b3, IDWTELEM *b4, IDWTELEM *b5, int width){
585     int i;
586
587     for(i=0; i<width; i++){
588         b4[i] -= (W_DM*(b3[i] + b5[i])+W_DO)>>W_DS;
589         b3[i] -= (W_CM*(b2[i] + b4[i])+W_CO)>>W_CS;
590 #ifdef liftS
591         b2[i] += (W_BM*(b1[i] + b3[i])+W_BO)>>W_BS;
592 #else
593         b2[i] += (W_BM*(b1[i] + b3[i])+4*b2[i]+W_BO)>>W_BS;
594 #endif
595         b1[i] += (W_AM*(b0[i] + b2[i])+W_AO)>>W_AS;
596     }
597 }
598
599 static void spatial_compose97i_buffered_init(DWTCompose *cs, slice_buffer * sb, int height, int stride_line){
600     cs->b0 = slice_buffer_get_line(sb, mirror(-3-1, height-1) * stride_line);
601     cs->b1 = slice_buffer_get_line(sb, mirror(-3  , height-1) * stride_line);
602     cs->b2 = slice_buffer_get_line(sb, mirror(-3+1, height-1) * stride_line);
603     cs->b3 = slice_buffer_get_line(sb, mirror(-3+2, height-1) * stride_line);
604     cs->y = -3;
605 }
606
607 static void spatial_compose97i_init(DWTCompose *cs, IDWTELEM *buffer, int height, int stride){
608     cs->b0 = buffer + mirror(-3-1, height-1)*stride;
609     cs->b1 = buffer + mirror(-3  , height-1)*stride;
610     cs->b2 = buffer + mirror(-3+1, height-1)*stride;
611     cs->b3 = buffer + mirror(-3+2, height-1)*stride;
612     cs->y = -3;
613 }
614
615 static void spatial_compose97i_dy_buffered(DWTContext *dsp, DWTCompose *cs, slice_buffer * sb, int width, int height, int stride_line){
616     int y = cs->y;
617
618     IDWTELEM *b0= cs->b0;
619     IDWTELEM *b1= cs->b1;
620     IDWTELEM *b2= cs->b2;
621     IDWTELEM *b3= cs->b3;
622     IDWTELEM *b4= slice_buffer_get_line(sb, mirror(y + 3, height - 1) * stride_line);
623     IDWTELEM *b5= slice_buffer_get_line(sb, mirror(y + 4, height - 1) * stride_line);
624
625     if(y>0 && y+4<height){
626         dsp->vertical_compose97i(b0, b1, b2, b3, b4, b5, width);
627     }else{
628         if(y+3<(unsigned)height) vertical_compose97iL1(b3, b4, b5, width);
629         if(y+2<(unsigned)height) vertical_compose97iH1(b2, b3, b4, width);
630         if(y+1<(unsigned)height) vertical_compose97iL0(b1, b2, b3, width);
631         if(y+0<(unsigned)height) vertical_compose97iH0(b0, b1, b2, width);
632     }
633
634     if(y-1<(unsigned)height) dsp->horizontal_compose97i(b0, width);
635     if(y+0<(unsigned)height) dsp->horizontal_compose97i(b1, width);
636
637     cs->b0=b2;
638     cs->b1=b3;
639     cs->b2=b4;
640     cs->b3=b5;
641     cs->y += 2;
642 }
643
644 static void spatial_compose97i_dy(DWTCompose *cs, IDWTELEM *buffer, int width, int height, int stride){
645     int y = cs->y;
646     IDWTELEM *b0= cs->b0;
647     IDWTELEM *b1= cs->b1;
648     IDWTELEM *b2= cs->b2;
649     IDWTELEM *b3= cs->b3;
650     IDWTELEM *b4= buffer + mirror(y+3, height-1)*stride;
651     IDWTELEM *b5= buffer + mirror(y+4, height-1)*stride;
652
653     if(y+3<(unsigned)height) vertical_compose97iL1(b3, b4, b5, width);
654     if(y+2<(unsigned)height) vertical_compose97iH1(b2, b3, b4, width);
655     if(y+1<(unsigned)height) vertical_compose97iL0(b1, b2, b3, width);
656     if(y+0<(unsigned)height) vertical_compose97iH0(b0, b1, b2, width);
657
658     if(y-1<(unsigned)height) ff_snow_horizontal_compose97i(b0, width);
659     if(y+0<(unsigned)height) ff_snow_horizontal_compose97i(b1, width);
660
661     cs->b0=b2;
662     cs->b1=b3;
663     cs->b2=b4;
664     cs->b3=b5;
665     cs->y += 2;
666 }
667
668 static void av_unused spatial_compose97i(IDWTELEM *buffer, int width, int height, int stride){
669     DWTCompose cs;
670     spatial_compose97i_init(&cs, buffer, height, stride);
671     while(cs.y <= height)
672         spatial_compose97i_dy(&cs, buffer, width, height, stride);
673 }
674
675 void ff_spatial_idwt_buffered_init(DWTCompose *cs, slice_buffer * sb, int width, int height, int stride_line, int type, int decomposition_count){
676     int level;
677     for(level=decomposition_count-1; level>=0; level--){
678         switch(type){
679         case DWT_97: spatial_compose97i_buffered_init(cs+level, sb, height>>level, stride_line<<level); break;
680         case DWT_53: spatial_compose53i_buffered_init(cs+level, sb, height>>level, stride_line<<level); break;
681         }
682     }
683 }
684
685 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){
686     const int support = type==1 ? 3 : 5;
687     int level;
688     if(type==2) return;
689
690     for(level=decomposition_count-1; level>=0; level--){
691         while(cs[level].y <= FFMIN((y>>level)+support, height>>level)){
692             switch(type){
693             case DWT_97: spatial_compose97i_dy_buffered(dsp, cs+level, slice_buf, width>>level, height>>level, stride_line<<level);
694                 break;
695             case DWT_53: spatial_compose53i_dy_buffered(cs+level, slice_buf, width>>level, height>>level, stride_line<<level);
696                 break;
697             }
698         }
699     }
700 }
701
702 static void ff_spatial_idwt_init(DWTCompose *cs, IDWTELEM *buffer, int width, int height, int stride, int type, int decomposition_count){
703     int level;
704     for(level=decomposition_count-1; level>=0; level--){
705         switch(type){
706         case DWT_97: spatial_compose97i_init(cs+level, buffer, height>>level, stride<<level); break;
707         case DWT_53: spatial_compose53i_init(cs+level, buffer, height>>level, stride<<level); break;
708         }
709     }
710 }
711
712 static void ff_spatial_idwt_slice(DWTCompose *cs, IDWTELEM *buffer, int width, int height, int stride, int type, int decomposition_count, int y){
713     const int support = type==1 ? 3 : 5;
714     int level;
715     if(type==2) return;
716
717     for(level=decomposition_count-1; level>=0; level--){
718         while(cs[level].y <= FFMIN((y>>level)+support, height>>level)){
719             switch(type){
720             case DWT_97: spatial_compose97i_dy(cs+level, buffer, width>>level, height>>level, stride<<level);
721                 break;
722             case DWT_53: spatial_compose53i_dy(cs+level, buffer, width>>level, height>>level, stride<<level);
723                 break;
724             }
725         }
726     }
727 }
728
729 void ff_spatial_idwt(IDWTELEM *buffer, int width, int height, int stride, int type, int decomposition_count){
730         DWTCompose cs[MAX_DECOMPOSITIONS];
731         int y;
732         ff_spatial_idwt_init(cs, buffer, width, height, stride, type, decomposition_count);
733         for(y=0; y<height; y+=4)
734             ff_spatial_idwt_slice(cs, buffer, width, height, stride, type, decomposition_count, y);
735 }
736
737 static inline int w_c(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int w, int h, int type){
738     int s, i, j;
739     const int dec_count= w==8 ? 3 : 4;
740     int tmp[32*32];
741     int level, ori;
742     static const int scale[2][2][4][4]={
743       {
744         {
745             // 9/7 8x8 dec=3
746             {268, 239, 239, 213},
747             {  0, 224, 224, 152},
748             {  0, 135, 135, 110},
749         },{
750             // 9/7 16x16 or 32x32 dec=4
751             {344, 310, 310, 280},
752             {  0, 320, 320, 228},
753             {  0, 175, 175, 136},
754             {  0, 129, 129, 102},
755         }
756       },{
757         {
758             // 5/3 8x8 dec=3
759             {275, 245, 245, 218},
760             {  0, 230, 230, 156},
761             {  0, 138, 138, 113},
762         },{
763             // 5/3 16x16 or 32x32 dec=4
764             {352, 317, 317, 286},
765             {  0, 328, 328, 233},
766             {  0, 180, 180, 140},
767             {  0, 132, 132, 105},
768         }
769       }
770     };
771
772     for (i = 0; i < h; i++) {
773         for (j = 0; j < w; j+=4) {
774             tmp[32*i+j+0] = (pix1[j+0] - pix2[j+0])<<4;
775             tmp[32*i+j+1] = (pix1[j+1] - pix2[j+1])<<4;
776             tmp[32*i+j+2] = (pix1[j+2] - pix2[j+2])<<4;
777             tmp[32*i+j+3] = (pix1[j+3] - pix2[j+3])<<4;
778         }
779         pix1 += line_size;
780         pix2 += line_size;
781     }
782
783     ff_spatial_dwt(tmp, w, h, 32, type, dec_count);
784
785     s=0;
786     assert(w==h);
787     for(level=0; level<dec_count; level++){
788         for(ori= level ? 1 : 0; ori<4; ori++){
789             int size= w>>(dec_count-level);
790             int sx= (ori&1) ? size : 0;
791             int stride= 32<<(dec_count-level);
792             int sy= (ori&2) ? stride>>1 : 0;
793
794             for(i=0; i<size; i++){
795                 for(j=0; j<size; j++){
796                     int v= tmp[sx + sy + i*stride + j] * scale[type][dec_count-3][level][ori];
797                     s += FFABS(v);
798                 }
799             }
800         }
801     }
802     assert(s>=0);
803     return s>>9;
804 }
805
806 static int w53_8_c(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int h){
807     return w_c(v, pix1, pix2, line_size,  8, h, 1);
808 }
809
810 static int w97_8_c(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int h){
811     return w_c(v, pix1, pix2, line_size,  8, h, 0);
812 }
813
814 static int w53_16_c(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int h){
815     return w_c(v, pix1, pix2, line_size, 16, h, 1);
816 }
817
818 static int w97_16_c(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int h){
819     return w_c(v, pix1, pix2, line_size, 16, h, 0);
820 }
821
822 int ff_w53_32_c(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int h){
823     return w_c(v, pix1, pix2, line_size, 32, h, 1);
824 }
825
826 int ff_w97_32_c(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int h){
827     return w_c(v, pix1, pix2, line_size, 32, h, 0);
828 }
829
830 void ff_dsputil_init_dwt(DSPContext *c)
831 {
832     c->w53[0]= w53_16_c;
833     c->w53[1]= w53_8_c;
834     c->w97[0]= w97_16_c;
835     c->w97[1]= w97_8_c;
836 }
837
838 void ff_dwt_init(DWTContext *c)
839 {
840     c->vertical_compose97i = ff_snow_vertical_compose97i;
841     c->horizontal_compose97i = ff_snow_horizontal_compose97i;
842     c->inner_add_yblock = ff_snow_inner_add_yblock;
843
844     if (HAVE_MMX) ff_dwt_init_x86(c);
845 }
846
847
848 static av_always_inline
849 void interleave(IDWTELEM *dst, IDWTELEM *src0, IDWTELEM *src1, int w2, int add, int shift)
850 {
851     int i;
852     for (i = 0; i < w2; i++) {
853         dst[2*i  ] = (src0[i] + add) >> shift;
854         dst[2*i+1] = (src1[i] + add) >> shift;
855     }
856 }
857
858 static void horizontal_compose_dirac53i(IDWTELEM *b, IDWTELEM *temp, int w)
859 {
860     const int w2 = w >> 1;
861     int x;
862
863     temp[0] = COMPOSE_53iL0(b[w2], b[0], b[w2]);
864     for (x = 1; x < w2; x++) {
865         temp[x     ] = COMPOSE_53iL0     (b[x+w2-1], b[x     ], b[x+w2]);
866         temp[x+w2-1] = COMPOSE_DIRAC53iH0(temp[x-1], b[x+w2-1], temp[x]);
867     }
868     temp[w-1] = COMPOSE_DIRAC53iH0(temp[w2-1], b[w-1], temp[w2-1]);
869
870     interleave(b, temp, temp+w2, w2, 1, 1);
871 }
872
873 static void horizontal_compose_dd97i(IDWTELEM *b, IDWTELEM *tmp, int w)
874 {
875     const int w2 = w >> 1;
876     int x;
877
878     tmp[0] = COMPOSE_53iL0(b[w2], b[0], b[w2]);
879     for (x = 1; x < w2; x++)
880         tmp[x] = COMPOSE_53iL0(b[x+w2-1], b[x], b[x+w2]);
881
882     // extend the edges
883     tmp[-1]   = tmp[0];
884     tmp[w2+1] = tmp[w2] = tmp[w2-1];
885
886     for (x = 0; x < w2; x++) {
887         b[2*x  ] = (tmp[x] + 1)>>1;
888         b[2*x+1] = (COMPOSE_DD97iH0(tmp[x-1], tmp[x], b[x+w2], tmp[x+1], tmp[x+2]) + 1)>>1;
889     }
890 }
891
892 static void horizontal_compose_dd137i(IDWTELEM *b, IDWTELEM *tmp, int w)
893 {
894     const int w2 = w >> 1;
895     int x;
896
897     tmp[0] = COMPOSE_DD137iL0(b[w2], b[w2], b[0], b[w2  ], b[w2+1]);
898     tmp[1] = COMPOSE_DD137iL0(b[w2], b[w2], b[1], b[w2+1], b[w2+2]);
899     for (x = 2; x < w2-1; x++)
900         tmp[x] = COMPOSE_DD137iL0(b[x+w2-2], b[x+w2-1], b[x], b[x+w2], b[x+w2+1]);
901     tmp[w2-1] = COMPOSE_DD137iL0(b[w-3], b[w-2], b[w2-1], b[w-1], b[w-1]);
902
903     // extend the edges
904     tmp[-1]   = tmp[0];
905     tmp[w2+1] = tmp[w2] = tmp[w2-1];
906
907     for (x = 0; x < w2; x++) {
908         b[2*x  ] = (tmp[x] + 1)>>1;
909         b[2*x+1] = (COMPOSE_DD97iH0(tmp[x-1], tmp[x], b[x+w2], tmp[x+1], tmp[x+2]) + 1)>>1;
910     }
911 }
912
913 static av_always_inline
914 void horizontal_compose_haari(IDWTELEM *b, IDWTELEM *temp, int w, int shift)
915 {
916     const int w2 = w >> 1;
917     int x;
918
919     for (x = 0; x < w2; x++) {
920         temp[x   ] = COMPOSE_HAARiL0(b[x   ], b[x+w2]);
921         temp[x+w2] = COMPOSE_HAARiH0(b[x+w2], temp[x]);
922     }
923
924     interleave(b, temp, temp+w2, w2, shift, shift);
925 }
926
927 static void horizontal_compose_haar0i(IDWTELEM *b, IDWTELEM *temp, int w)
928 {
929     horizontal_compose_haari(b, temp, w, 0);
930 }
931
932 static void horizontal_compose_haar1i(IDWTELEM *b, IDWTELEM *temp, int w)
933 {
934     horizontal_compose_haari(b, temp, w, 1);
935 }
936
937 static void horizontal_compose_fidelityi(IDWTELEM *b, IDWTELEM *tmp, int w)
938 {
939     const int w2 = w >> 1;
940     int i, x;
941     IDWTELEM v[8];
942
943     for (x = 0; x < w2; x++) {
944         for (i = 0; i < 8; i++)
945             v[i] = b[av_clip(x-3+i, 0, w2-1)];
946         tmp[x] = COMPOSE_FIDELITYiH0(v[0], v[1], v[2], v[3], b[x+w2], v[4], v[5], v[6], v[7]);
947     }
948
949     for (x = 0; x < w2; x++) {
950         for (i = 0; i < 8; i++)
951             v[i] = tmp[av_clip(x-4+i, 0, w2-1)];
952         tmp[x+w2] = COMPOSE_FIDELITYiL0(v[0], v[1], v[2], v[3], b[x], v[4], v[5], v[6], v[7]);
953     }
954
955     interleave(b, tmp+w2, tmp, w2, 0, 0);
956 }
957
958 static void horizontal_compose_daub97i(IDWTELEM *b, IDWTELEM *temp, int w)
959 {
960     const int w2 = w >> 1;
961     int x, b0, b1, b2;
962
963     temp[0] = COMPOSE_DAUB97iL1(b[w2], b[0], b[w2]);
964     for (x = 1; x < w2; x++) {
965         temp[x     ] = COMPOSE_DAUB97iL1(b[x+w2-1], b[x     ], b[x+w2]);
966         temp[x+w2-1] = COMPOSE_DAUB97iH1(temp[x-1], b[x+w2-1], temp[x]);
967     }
968     temp[w-1] = COMPOSE_DAUB97iH1(temp[w2-1], b[w-1], temp[w2-1]);
969
970     // second stage combined with interleave and shift
971     b0 = b2 = COMPOSE_DAUB97iL0(temp[w2], temp[0], temp[w2]);
972     b[0] = (b0 + 1) >> 1;
973     for (x = 1; x < w2; x++) {
974         b2 = COMPOSE_DAUB97iL0(temp[x+w2-1], temp[x     ], temp[x+w2]);
975         b1 = COMPOSE_DAUB97iH0(          b0, temp[x+w2-1], b2        );
976         b[2*x-1] = (b1 + 1) >> 1;
977         b[2*x  ] = (b2 + 1) >> 1;
978         b0 = b2;
979     }
980     b[w-1] = (COMPOSE_DAUB97iH0(b2, temp[w-1], b2) + 1) >> 1;
981 }
982
983 static void vertical_compose_dirac53iH0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width)
984 {
985     int i;
986
987     for(i=0; i<width; i++){
988         b1[i] = COMPOSE_DIRAC53iH0(b0[i], b1[i], b2[i]);
989     }
990 }
991
992 static void vertical_compose_dd97iH0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2,
993                                   IDWTELEM *b3, IDWTELEM *b4, int width)
994 {
995     int i;
996
997     for(i=0; i<width; i++){
998         b2[i] = COMPOSE_DD97iH0(b0[i], b1[i], b2[i], b3[i], b4[i]);
999     }
1000 }
1001
1002 static void vertical_compose_dd137iL0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2,
1003                                       IDWTELEM *b3, IDWTELEM *b4, int width)
1004 {
1005     int i;
1006
1007     for(i=0; i<width; i++){
1008         b2[i] = COMPOSE_DD137iL0(b0[i], b1[i], b2[i], b3[i], b4[i]);
1009     }
1010 }
1011
1012 static void vertical_compose_haar(IDWTELEM *b0, IDWTELEM *b1, int width)
1013 {
1014     int i;
1015
1016     for (i = 0; i < width; i++) {
1017         b0[i] = COMPOSE_HAARiL0(b0[i], b1[i]);
1018         b1[i] = COMPOSE_HAARiH0(b1[i], b0[i]);
1019     }
1020 }
1021
1022 static void vertical_compose_fidelityiH0(IDWTELEM *dst, IDWTELEM *b[8], int width)
1023 {
1024     int i;
1025
1026     for(i=0; i<width; i++){
1027         dst[i] = COMPOSE_FIDELITYiH0(b[0][i], b[1][i], b[2][i], b[3][i], dst[i], b[4][i], b[5][i], b[6][i], b[7][i]);
1028     }
1029 }
1030
1031 static void vertical_compose_fidelityiL0(IDWTELEM *dst, IDWTELEM *b[8], int width)
1032 {
1033     int i;
1034
1035     for(i=0; i<width; i++){
1036         dst[i] = COMPOSE_FIDELITYiL0(b[0][i], b[1][i], b[2][i], b[3][i], dst[i], b[4][i], b[5][i], b[6][i], b[7][i]);
1037     }
1038 }
1039
1040 static void vertical_compose_daub97iH0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width)
1041 {
1042     int i;
1043
1044     for(i=0; i<width; i++){
1045         b1[i] = COMPOSE_DAUB97iH0(b0[i], b1[i], b2[i]);
1046     }
1047 }
1048
1049 static void vertical_compose_daub97iH1(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width)
1050 {
1051     int i;
1052
1053     for(i=0; i<width; i++){
1054         b1[i] = COMPOSE_DAUB97iH1(b0[i], b1[i], b2[i]);
1055     }
1056 }
1057
1058 static void vertical_compose_daub97iL0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width)
1059 {
1060     int i;
1061
1062     for(i=0; i<width; i++){
1063         b1[i] = COMPOSE_DAUB97iL0(b0[i], b1[i], b2[i]);
1064     }
1065 }
1066
1067 static void vertical_compose_daub97iL1(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width)
1068 {
1069     int i;
1070
1071     for(i=0; i<width; i++){
1072         b1[i] = COMPOSE_DAUB97iL1(b0[i], b1[i], b2[i]);
1073     }
1074 }
1075
1076
1077 static void spatial_compose_dd97i_dy(DWTContext *d, int level, int width, int height, int stride)
1078 {
1079     vertical_compose_3tap vertical_compose_l0 = d->vertical_compose_l0;
1080     vertical_compose_5tap vertical_compose_h0 = d->vertical_compose_h0;
1081     DWTCompose *cs = d->cs + level;
1082
1083     int i, y = cs->y;
1084     IDWTELEM *b[8];
1085     for (i = 0; i < 6; i++)
1086         b[i] = cs->b[i];
1087     b[6] = d->buffer + av_clip(y+5, 0, height-2)*stride;
1088     b[7] = d->buffer + av_clip(y+6, 1, height-1)*stride;
1089
1090         if(y+5<(unsigned)height) vertical_compose_l0(      b[5], b[6], b[7],       width);
1091         if(y+1<(unsigned)height) vertical_compose_h0(b[0], b[2], b[3], b[4], b[6], width);
1092
1093         if(y-1<(unsigned)height) d->horizontal_compose(b[0], d->temp, width);
1094         if(y+0<(unsigned)height) d->horizontal_compose(b[1], d->temp, width);
1095
1096     for (i = 0; i < 6; i++)
1097         cs->b[i] = b[i+2];
1098     cs->y += 2;
1099 }
1100
1101 static void spatial_compose_dirac53i_dy(DWTContext *d, int level, int width, int height, int stride)
1102 {
1103     vertical_compose_3tap vertical_compose_l0 = d->vertical_compose_l0;
1104     vertical_compose_3tap vertical_compose_h0 = d->vertical_compose_h0;
1105     DWTCompose *cs = d->cs + level;
1106
1107     int y= cs->y;
1108     IDWTELEM *b[4] = { cs->b[0], cs->b[1] };
1109     b[2] = d->buffer + mirror(y+1, height-1)*stride;
1110     b[3] = d->buffer + mirror(y+2, height-1)*stride;
1111
1112         if(y+1<(unsigned)height) vertical_compose_l0(b[1], b[2], b[3], width);
1113         if(y+0<(unsigned)height) vertical_compose_h0(b[0], b[1], b[2], width);
1114
1115         if(y-1<(unsigned)height) d->horizontal_compose(b[0], d->temp, width);
1116         if(y+0<(unsigned)height) d->horizontal_compose(b[1], d->temp, width);
1117
1118     cs->b[0] = b[2];
1119     cs->b[1] = b[3];
1120     cs->y += 2;
1121 }
1122
1123
1124 static void spatial_compose_dd137i_dy(DWTContext *d, int level, int width, int height, int stride)
1125 {
1126     vertical_compose_5tap vertical_compose_l0 = d->vertical_compose_l0;
1127     vertical_compose_5tap vertical_compose_h0 = d->vertical_compose_h0;
1128     DWTCompose *cs = d->cs + level;
1129
1130     int i, y = cs->y;
1131     IDWTELEM *b[10];
1132     for (i = 0; i < 8; i++)
1133         b[i] = cs->b[i];
1134     b[8] = d->buffer + av_clip(y+7, 0, height-2)*stride;
1135     b[9] = d->buffer + av_clip(y+8, 1, height-1)*stride;
1136
1137         if(y+5<(unsigned)height) vertical_compose_l0(b[3], b[5], b[6], b[7], b[9], width);
1138         if(y+1<(unsigned)height) vertical_compose_h0(b[0], b[2], b[3], b[4], b[6], width);
1139
1140         if(y-1<(unsigned)height) d->horizontal_compose(b[0], d->temp, width);
1141         if(y+0<(unsigned)height) d->horizontal_compose(b[1], d->temp, width);
1142
1143     for (i = 0; i < 8; i++)
1144         cs->b[i] = b[i+2];
1145     cs->y += 2;
1146 }
1147
1148 // haar makes the assumption that height is even (always true for dirac)
1149 static void spatial_compose_haari_dy(DWTContext *d, int level, int width, int height, int stride)
1150 {
1151     vertical_compose_2tap vertical_compose = d->vertical_compose;
1152     int y = d->cs[level].y;
1153     IDWTELEM *b0 = d->buffer + (y-1)*stride;
1154     IDWTELEM *b1 = d->buffer + (y  )*stride;
1155
1156     vertical_compose(b0, b1, width);
1157     d->horizontal_compose(b0, d->temp, width);
1158     d->horizontal_compose(b1, d->temp, width);
1159
1160     d->cs[level].y += 2;
1161 }
1162
1163 // Don't do sliced idwt for fidelity; the 9 tap filter makes it a bit annoying
1164 // Fortunately, this filter isn't used in practice.
1165 static void spatial_compose_fidelity(DWTContext *d, int level, int width, int height, int stride)
1166 {
1167     vertical_compose_9tap vertical_compose_l0 = d->vertical_compose_l0;
1168     vertical_compose_9tap vertical_compose_h0 = d->vertical_compose_h0;
1169     int i, y;
1170     IDWTELEM *b[8];
1171
1172     for (y = 1; y < height; y += 2) {
1173         for (i = 0; i < 8; i++)
1174             b[i] = d->buffer + av_clip((y-7 + 2*i), 0, height-2)*stride;
1175         vertical_compose_h0(d->buffer + y*stride, b, width);
1176     }
1177
1178     for (y = 0; y < height; y += 2) {
1179         for (i = 0; i < 8; i++)
1180             b[i] = d->buffer + av_clip((y-7 + 2*i), 1, height-1)*stride;
1181         vertical_compose_l0(d->buffer + y*stride, b, width);
1182     }
1183
1184     for (y = 0; y < height; y++)
1185         d->horizontal_compose(d->buffer + y*stride, d->temp, width);
1186
1187     d->cs[level].y = height+1;
1188 }
1189
1190 static void spatial_compose_daub97i_dy(DWTContext *d, int level, int width, int height, int stride)
1191 {
1192     vertical_compose_3tap vertical_compose_l0 = d->vertical_compose_l0;
1193     vertical_compose_3tap vertical_compose_h0 = d->vertical_compose_h0;
1194     vertical_compose_3tap vertical_compose_l1 = d->vertical_compose_l1;
1195     vertical_compose_3tap vertical_compose_h1 = d->vertical_compose_h1;
1196     DWTCompose *cs = d->cs + level;
1197
1198     int i, y = cs->y;
1199     IDWTELEM *b[6];
1200     for (i = 0; i < 4; i++)
1201         b[i] = cs->b[i];
1202     b[4] = d->buffer + mirror(y+3, height-1)*stride;
1203     b[5] = d->buffer + mirror(y+4, height-1)*stride;
1204
1205         if(y+3<(unsigned)height) vertical_compose_l1(b[3], b[4], b[5], width);
1206         if(y+2<(unsigned)height) vertical_compose_h1(b[2], b[3], b[4], width);
1207         if(y+1<(unsigned)height) vertical_compose_l0(b[1], b[2], b[3], width);
1208         if(y+0<(unsigned)height) vertical_compose_h0(b[0], b[1], b[2], width);
1209
1210         if(y-1<(unsigned)height) d->horizontal_compose(b[0], d->temp, width);
1211         if(y+0<(unsigned)height) d->horizontal_compose(b[1], d->temp, width);
1212
1213     for (i = 0; i < 4; i++)
1214         cs->b[i] = b[i+2];
1215     cs->y += 2;
1216 }
1217
1218
1219 static void spatial_compose97i_init2(DWTCompose *cs, IDWTELEM *buffer, int height, int stride)
1220 {
1221     cs->b[0] = buffer + mirror(-3-1, height-1)*stride;
1222     cs->b[1] = buffer + mirror(-3  , height-1)*stride;
1223     cs->b[2] = buffer + mirror(-3+1, height-1)*stride;
1224     cs->b[3] = buffer + mirror(-3+2, height-1)*stride;
1225     cs->y = -3;
1226 }
1227
1228 static void spatial_compose53i_init2(DWTCompose *cs, IDWTELEM *buffer, int height, int stride)
1229 {
1230     cs->b[0] = buffer + mirror(-1-1, height-1)*stride;
1231     cs->b[1] = buffer + mirror(-1  , height-1)*stride;
1232     cs->y = -1;
1233 }
1234
1235 static void spatial_compose_dd97i_init(DWTCompose *cs, IDWTELEM *buffer, int height, int stride)
1236 {
1237     cs->b[0] = buffer + av_clip(-5-1, 0, height-2)*stride;
1238     cs->b[1] = buffer + av_clip(-5  , 1, height-1)*stride;
1239     cs->b[2] = buffer + av_clip(-5+1, 0, height-2)*stride;
1240     cs->b[3] = buffer + av_clip(-5+2, 1, height-1)*stride;
1241     cs->b[4] = buffer + av_clip(-5+3, 0, height-2)*stride;
1242     cs->b[5] = buffer + av_clip(-5+4, 1, height-1)*stride;
1243     cs->y = -5;
1244 }
1245
1246 static void spatial_compose_dd137i_init(DWTCompose *cs, IDWTELEM *buffer, int height, int stride)
1247 {
1248     cs->b[0] = buffer + av_clip(-5-1, 0, height-2)*stride;
1249     cs->b[1] = buffer + av_clip(-5  , 1, height-1)*stride;
1250     cs->b[2] = buffer + av_clip(-5+1, 0, height-2)*stride;
1251     cs->b[3] = buffer + av_clip(-5+2, 1, height-1)*stride;
1252     cs->b[4] = buffer + av_clip(-5+3, 0, height-2)*stride;
1253     cs->b[5] = buffer + av_clip(-5+4, 1, height-1)*stride;
1254     cs->b[6] = buffer + av_clip(-5+5, 0, height-2)*stride;
1255     cs->b[7] = buffer + av_clip(-5+6, 1, height-1)*stride;
1256     cs->y = -5;
1257 }
1258
1259 int ff_spatial_idwt_init2(DWTContext *d, IDWTELEM *buffer, int width, int height,
1260                           int stride, enum dwt_type type, int decomposition_count,
1261                           IDWTELEM *temp)
1262 {
1263     int level;
1264
1265     d->buffer = buffer;
1266     d->width = width;
1267     d->height = height;
1268     d->stride = stride;
1269     d->decomposition_count = decomposition_count;
1270     d->temp = temp + 8;
1271
1272     for(level=decomposition_count-1; level>=0; level--){
1273         int hl = height >> level;
1274         int stride_l = stride << level;
1275
1276         switch(type){
1277         case DWT_DIRAC_DD9_7:
1278             spatial_compose_dd97i_init(d->cs+level, buffer, hl, stride_l);
1279             break;
1280         case DWT_DIRAC_LEGALL5_3:
1281             spatial_compose53i_init2(d->cs+level, buffer, hl, stride_l);
1282             break;
1283         case DWT_DIRAC_DD13_7:
1284             spatial_compose_dd137i_init(d->cs+level, buffer, hl, stride_l);
1285             break;
1286         case DWT_DIRAC_HAAR0:
1287         case DWT_DIRAC_HAAR1:
1288             d->cs[level].y = 1;
1289             break;
1290         case DWT_DIRAC_DAUB9_7:
1291             spatial_compose97i_init2(d->cs+level, buffer, hl, stride_l);
1292             break;
1293         default:
1294             d->cs[level].y = 0;
1295             break;
1296         }
1297     }
1298
1299     switch (type) {
1300     case DWT_DIRAC_DD9_7:
1301         d->spatial_compose = spatial_compose_dd97i_dy;
1302         d->vertical_compose_l0 = vertical_compose53iL0;
1303         d->vertical_compose_h0 = vertical_compose_dd97iH0;
1304         d->horizontal_compose = horizontal_compose_dd97i;
1305         d->support = 7;
1306         break;
1307     case DWT_DIRAC_LEGALL5_3:
1308         d->spatial_compose = spatial_compose_dirac53i_dy;
1309         d->vertical_compose_l0 = vertical_compose53iL0;
1310         d->vertical_compose_h0 = vertical_compose_dirac53iH0;
1311         d->horizontal_compose = horizontal_compose_dirac53i;
1312         d->support = 3;
1313         break;
1314     case DWT_DIRAC_DD13_7:
1315         d->spatial_compose = spatial_compose_dd137i_dy;
1316         d->vertical_compose_l0 = vertical_compose_dd137iL0;
1317         d->vertical_compose_h0 = vertical_compose_dd97iH0;
1318         d->horizontal_compose = horizontal_compose_dd137i;
1319         d->support = 7;
1320         break;
1321     case DWT_DIRAC_HAAR0:
1322     case DWT_DIRAC_HAAR1:
1323         d->spatial_compose = spatial_compose_haari_dy;
1324         d->vertical_compose = vertical_compose_haar;
1325         if (type == DWT_DIRAC_HAAR0)
1326             d->horizontal_compose = horizontal_compose_haar0i;
1327         else
1328             d->horizontal_compose = horizontal_compose_haar1i;
1329         d->support = 1;
1330         break;
1331     case DWT_DIRAC_FIDELITY:
1332         d->spatial_compose = spatial_compose_fidelity;
1333         d->vertical_compose_l0 = vertical_compose_fidelityiL0;
1334         d->vertical_compose_h0 = vertical_compose_fidelityiH0;
1335         d->horizontal_compose = horizontal_compose_fidelityi;
1336         break;
1337     case DWT_DIRAC_DAUB9_7:
1338         d->spatial_compose = spatial_compose_daub97i_dy;
1339         d->vertical_compose_l0 = vertical_compose_daub97iL0;
1340         d->vertical_compose_h0 = vertical_compose_daub97iH0;
1341         d->vertical_compose_l1 = vertical_compose_daub97iL1;
1342         d->vertical_compose_h1 = vertical_compose_daub97iH1;
1343         d->horizontal_compose = horizontal_compose_daub97i;
1344         d->support = 5;
1345         break;
1346     default:
1347         av_log(NULL, AV_LOG_ERROR, "Unknown wavelet type %d\n", type);
1348         return -1;
1349     }
1350
1351     if (HAVE_MMX) ff_spatial_idwt_init_mmx(d, type);
1352
1353     return 0;
1354 }
1355
1356 void ff_spatial_idwt_slice2(DWTContext *d, int y)
1357 {
1358     int level, support = d->support;
1359
1360     for (level = d->decomposition_count-1; level >= 0; level--) {
1361         int wl = d->width  >> level;
1362         int hl = d->height >> level;
1363         int stride_l = d->stride << level;
1364
1365         while (d->cs[level].y <= FFMIN((y>>level)+support, hl))
1366             d->spatial_compose(d, level, wl, hl, stride_l);
1367     }
1368 }
1369
1370 int ff_spatial_idwt2(IDWTELEM *buffer, int width, int height, int stride,
1371                      enum dwt_type type, int decomposition_count, IDWTELEM *temp)
1372 {
1373     DWTContext d;
1374     int y;
1375
1376     if (ff_spatial_idwt_init2(&d, buffer, width, height, stride, type, decomposition_count, temp))
1377         return -1;
1378
1379     for (y = 0; y < d.height; y += 4)
1380         ff_spatial_idwt_slice2(&d, y);
1381
1382     return 0;
1383 }