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