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