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