]> git.sesse.net Git - ffmpeg/blob - libavcodec/dwt.c
avcodec: Drop long-deprecated imgconvert.h header
[ffmpeg] / libavcodec / dwt.c
1 /*
2  * Copyright (C) 2004-2010 Michael Niedermayer <michaelni@gmx.at>
3  *
4  * This file is part of Libav.
5  *
6  * Libav is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * Libav is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with Libav; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 #include "libavutil/attributes.h"
22 #include "libavutil/common.h"
23 #include "dsputil.h"
24 #include "dwt.h"
25
26 int ff_slice_buffer_init(slice_buffer *buf, int line_count,
27                          int max_allocated_lines, int line_width,
28                          IDWTELEM *base_buffer)
29 {
30     int i;
31
32     buf->base_buffer = base_buffer;
33     buf->line_count  = line_count;
34     buf->line_width  = line_width;
35     buf->data_count  = max_allocated_lines;
36     buf->line        = av_mallocz(sizeof(IDWTELEM *) * line_count);
37     if (!buf->line)
38         return AVERROR(ENOMEM);
39     buf->data_stack  = av_malloc(sizeof(IDWTELEM *) * max_allocated_lines);
40     if (!buf->data_stack) {
41         av_free(buf->line);
42         return AVERROR(ENOMEM);
43     }
44
45     for (i = 0; i < max_allocated_lines; i++) {
46         buf->data_stack[i] = av_malloc(sizeof(IDWTELEM) * line_width);
47         if (!buf->data_stack[i]) {
48             for (i--; i >=0; i--)
49                 av_free(buf->data_stack[i]);
50             av_free(buf->data_stack);
51             av_free(buf->line);
52             return AVERROR(ENOMEM);
53         }
54     }
55
56     buf->data_stack_top = max_allocated_lines - 1;
57     return 0;
58 }
59
60 IDWTELEM *ff_slice_buffer_load_line(slice_buffer *buf, int line)
61 {
62     IDWTELEM *buffer;
63
64     assert(buf->data_stack_top >= 0);
65 //  assert(!buf->line[line]);
66     if (buf->line[line])
67         return buf->line[line];
68
69     buffer = buf->data_stack[buf->data_stack_top];
70     buf->data_stack_top--;
71     buf->line[line] = buffer;
72
73     return buffer;
74 }
75
76 void ff_slice_buffer_release(slice_buffer *buf, int line)
77 {
78     IDWTELEM *buffer;
79
80     assert(line >= 0 && line < buf->line_count);
81     assert(buf->line[line]);
82
83     buffer = buf->line[line];
84     buf->data_stack_top++;
85     buf->data_stack[buf->data_stack_top] = buffer;
86     buf->line[line]                      = NULL;
87 }
88
89 void ff_slice_buffer_flush(slice_buffer *buf)
90 {
91     int i;
92     for (i = 0; i < buf->line_count; i++)
93         if (buf->line[i])
94             ff_slice_buffer_release(buf, i);
95 }
96
97 void ff_slice_buffer_destroy(slice_buffer *buf)
98 {
99     int i;
100     ff_slice_buffer_flush(buf);
101
102     for (i = buf->data_count - 1; i >= 0; i--)
103         av_freep(&buf->data_stack[i]);
104     av_freep(&buf->data_stack);
105     av_freep(&buf->line);
106 }
107
108 static inline int mirror(int v, int m)
109 {
110     while ((unsigned)v > (unsigned)m) {
111         v = -v;
112         if (v < 0)
113             v += 2 * m;
114     }
115     return v;
116 }
117
118 static av_always_inline void lift(DWTELEM *dst, DWTELEM *src, DWTELEM *ref,
119                                   int dst_step, int src_step, int ref_step,
120                                   int width, int mul, int add, int shift,
121                                   int highpass, int inverse)
122 {
123     const int mirror_left  = !highpass;
124     const int mirror_right = (width & 1) ^ highpass;
125     const int w            = (width >> 1) - 1 + (highpass & width);
126     int i;
127
128 #define LIFT(src, ref, inv) ((src) + ((inv) ? -(ref) : +(ref)))
129     if (mirror_left) {
130         dst[0] = LIFT(src[0], ((mul * 2 * ref[0] + add) >> shift), inverse);
131         dst   += dst_step;
132         src   += src_step;
133     }
134
135     for (i = 0; i < w; i++)
136         dst[i * dst_step] = LIFT(src[i * src_step],
137                                  ((mul * (ref[i * ref_step] +
138                                           ref[(i + 1) * ref_step]) +
139                                    add) >> shift),
140                                  inverse);
141
142     if (mirror_right)
143         dst[w * dst_step] = LIFT(src[w * src_step],
144                                  ((mul * 2 * ref[w * ref_step] + add) >> shift),
145                                  inverse);
146 }
147
148 static av_always_inline void liftS(DWTELEM *dst, DWTELEM *src, DWTELEM *ref,
149                                    int dst_step, int src_step, int ref_step,
150                                    int width, int mul, int add, int shift,
151                                    int highpass, int inverse)
152 {
153     const int mirror_left  = !highpass;
154     const int mirror_right = (width & 1) ^ highpass;
155     const int w            = (width >> 1) - 1 + (highpass & width);
156     int i;
157
158     assert(shift == 4);
159 #define LIFTS(src, ref, inv)                                            \
160     ((inv) ? (src) + (((ref) + 4 * (src)) >> shift)                     \
161            : -((-16 * (src) + (ref) + add /                             \
162                 4 + 1 + (5 << 25)) / (5 * 4) - (1 << 23)))
163     if (mirror_left) {
164         dst[0] = LIFTS(src[0], mul * 2 * ref[0] + add, inverse);
165         dst   += dst_step;
166         src   += src_step;
167     }
168
169     for (i = 0; i < w; i++)
170         dst[i * dst_step] = LIFTS(src[i * src_step],
171                                   mul * (ref[i * ref_step] +
172                                          ref[(i + 1) * ref_step]) + add,
173                                   inverse);
174
175     if (mirror_right)
176         dst[w * dst_step] = LIFTS(src[w * src_step],
177                                   mul * 2 * ref[w * ref_step] + add,
178                                   inverse);
179 }
180
181 static void horizontal_decompose53i(DWTELEM *b, DWTELEM *temp, int width)
182 {
183     const int width2 = width >> 1;
184     int x;
185     const int w2 = (width + 1) >> 1;
186
187     for (x = 0; x < width2; x++) {
188         temp[x]      = b[2 * x];
189         temp[x + w2] = b[2 * x + 1];
190     }
191     if (width & 1)
192         temp[x] = b[2 * x];
193     lift(b + w2, temp + w2, temp,   1, 1, 1, width, -1, 0, 1, 1, 0);
194     lift(b,      temp,      b + w2, 1, 1, 1, width,  1, 2, 2, 0, 0);
195 }
196
197 static void vertical_decompose53iH0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2,
198                                     int width)
199 {
200     int i;
201
202     for (i = 0; i < width; i++)
203         b1[i] -= (b0[i] + b2[i]) >> 1;
204 }
205
206 static void vertical_decompose53iL0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2,
207                                     int width)
208 {
209     int i;
210
211     for (i = 0; i < width; i++)
212         b1[i] += (b0[i] + b2[i] + 2) >> 2;
213 }
214
215 static void spatial_decompose53i(DWTELEM *buffer, DWTELEM *temp,
216                                  int width, int height, int stride)
217 {
218     int y;
219     DWTELEM *b0 = buffer + mirror(-2 - 1, height - 1) * stride;
220     DWTELEM *b1 = buffer + mirror(-2,     height - 1) * stride;
221
222     for (y = -2; y < height; y += 2) {
223         DWTELEM *b2 = buffer + mirror(y + 1, height - 1) * stride;
224         DWTELEM *b3 = buffer + mirror(y + 2, height - 1) * stride;
225
226         if (y + 1 < (unsigned)height)
227             horizontal_decompose53i(b2, temp, width);
228         if (y + 2 < (unsigned)height)
229             horizontal_decompose53i(b3, temp, width);
230
231         if (y + 1 < (unsigned)height)
232             vertical_decompose53iH0(b1, b2, b3, width);
233         if (y + 0 < (unsigned)height)
234             vertical_decompose53iL0(b0, b1, b2, width);
235
236         b0 = b2;
237         b1 = b3;
238     }
239 }
240
241 static void horizontal_decompose97i(DWTELEM *b, DWTELEM *temp, int width)
242 {
243     const int w2 = (width + 1) >> 1;
244
245     lift(temp + w2, b + 1, b,         1, 2, 2, width, W_AM, W_AO, W_AS, 1, 1);
246     liftS(temp,     b,     temp + w2, 1, 2, 1, width, W_BM, W_BO, W_BS, 0, 0);
247     lift(b + w2, temp + w2, temp,     1, 1, 1, width, W_CM, W_CO, W_CS, 1, 0);
248     lift(b,      temp,      b + w2,   1, 1, 1, width, W_DM, W_DO, W_DS, 0, 0);
249 }
250
251 static void vertical_decompose97iH0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2,
252                                     int width)
253 {
254     int i;
255
256     for (i = 0; i < width; i++)
257         b1[i] -= (W_AM * (b0[i] + b2[i]) + W_AO) >> W_AS;
258 }
259
260 static void vertical_decompose97iH1(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2,
261                                     int width)
262 {
263     int i;
264
265     for (i = 0; i < width; i++)
266         b1[i] += (W_CM * (b0[i] + b2[i]) + W_CO) >> W_CS;
267 }
268
269 static void vertical_decompose97iL0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2,
270                                     int width)
271 {
272     int i;
273
274     for (i = 0; i < width; i++)
275         b1[i] = (16 * 4 * b1[i] - 4 * (b0[i] + b2[i]) + W_BO * 5 + (5 << 27)) /
276                 (5 * 16) - (1 << 23);
277 }
278
279 static void vertical_decompose97iL1(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2,
280                                     int width)
281 {
282     int i;
283
284     for (i = 0; i < width; i++)
285         b1[i] += (W_DM * (b0[i] + b2[i]) + W_DO) >> W_DS;
286 }
287
288 static void spatial_decompose97i(DWTELEM *buffer, DWTELEM *temp,
289                                  int width, int height, int stride)
290 {
291     int y;
292     DWTELEM *b0 = buffer + mirror(-4 - 1, height - 1) * stride;
293     DWTELEM *b1 = buffer + mirror(-4,     height - 1) * stride;
294     DWTELEM *b2 = buffer + mirror(-4 + 1, height - 1) * stride;
295     DWTELEM *b3 = buffer + mirror(-4 + 2, height - 1) * stride;
296
297     for (y = -4; y < height; y += 2) {
298         DWTELEM *b4 = buffer + mirror(y + 3, height - 1) * stride;
299         DWTELEM *b5 = buffer + mirror(y + 4, height - 1) * stride;
300
301         if (y + 3 < (unsigned)height)
302             horizontal_decompose97i(b4, temp, width);
303         if (y + 4 < (unsigned)height)
304             horizontal_decompose97i(b5, temp, width);
305
306         if (y + 3 < (unsigned)height)
307             vertical_decompose97iH0(b3, b4, b5, width);
308         if (y + 2 < (unsigned)height)
309             vertical_decompose97iL0(b2, b3, b4, width);
310         if (y + 1 < (unsigned)height)
311             vertical_decompose97iH1(b1, b2, b3, width);
312         if (y + 0 < (unsigned)height)
313             vertical_decompose97iL1(b0, b1, b2, width);
314
315         b0 = b2;
316         b1 = b3;
317         b2 = b4;
318         b3 = b5;
319     }
320 }
321
322 void ff_spatial_dwt(DWTELEM *buffer, DWTELEM *temp, int width, int height,
323                     int stride, int type, int decomposition_count)
324 {
325     int level;
326
327     for (level = 0; level < decomposition_count; level++) {
328         switch (type) {
329         case DWT_97:
330             spatial_decompose97i(buffer, temp,
331                                  width >> level, height >> level,
332                                  stride << level);
333             break;
334         case DWT_53:
335             spatial_decompose53i(buffer, temp,
336                                  width >> level, height >> level,
337                                  stride << level);
338             break;
339         }
340     }
341 }
342
343 static void horizontal_compose53i(IDWTELEM *b, IDWTELEM *temp, int width)
344 {
345     const int width2 = width >> 1;
346     const int w2     = (width + 1) >> 1;
347     int x;
348
349     for (x = 0; x < width2; x++) {
350         temp[2 * x]     = b[x];
351         temp[2 * x + 1] = b[x + w2];
352     }
353     if (width & 1)
354         temp[2 * x] = b[x];
355
356     b[0] = temp[0] - ((temp[1] + 1) >> 1);
357     for (x = 2; x < width - 1; x += 2) {
358         b[x]     = temp[x]     - ((temp[x - 1] + temp[x + 1] + 2) >> 2);
359         b[x - 1] = temp[x - 1] + ((b[x - 2]    + b[x]        + 1) >> 1);
360     }
361     if (width & 1) {
362         b[x]     = temp[x]     - ((temp[x - 1]     + 1) >> 1);
363         b[x - 1] = temp[x - 1] + ((b[x - 2] + b[x] + 1) >> 1);
364     } else
365         b[x - 1] = temp[x - 1] + b[x - 2];
366 }
367
368 static void vertical_compose53iH0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2,
369                                   int width)
370 {
371     int i;
372
373     for (i = 0; i < width; i++)
374         b1[i] += (b0[i] + b2[i]) >> 1;
375 }
376
377 static void vertical_compose53iL0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2,
378                                   int width)
379 {
380     int i;
381
382     for (i = 0; i < width; i++)
383         b1[i] -= (b0[i] + b2[i] + 2) >> 2;
384 }
385
386 static void spatial_compose53i_buffered_init(DWTCompose *cs, slice_buffer *sb,
387                                              int height, int stride_line)
388 {
389     cs->b0 = slice_buffer_get_line(sb,
390                                    mirror(-1 - 1, height - 1) * stride_line);
391     cs->b1 = slice_buffer_get_line(sb, mirror(-1, height - 1) * stride_line);
392     cs->y  = -1;
393 }
394
395 static void spatial_compose53i_init(DWTCompose *cs, IDWTELEM *buffer,
396                                     int height, int stride)
397 {
398     cs->b0 = buffer + mirror(-1 - 1, height - 1) * stride;
399     cs->b1 = buffer + mirror(-1,     height - 1) * stride;
400     cs->y  = -1;
401 }
402
403 static void spatial_compose53i_dy_buffered(DWTCompose *cs, slice_buffer *sb,
404                                            IDWTELEM *temp,
405                                            int width, int height,
406                                            int stride_line)
407 {
408     int y = cs->y;
409
410     IDWTELEM *b0 = cs->b0;
411     IDWTELEM *b1 = cs->b1;
412     IDWTELEM *b2 = slice_buffer_get_line(sb,
413                                          mirror(y + 1, height - 1) *
414                                          stride_line);
415     IDWTELEM *b3 = slice_buffer_get_line(sb,
416                                          mirror(y + 2, height - 1) *
417                                          stride_line);
418
419     if (y + 1 < (unsigned)height && y < (unsigned)height) {
420         int x;
421
422         for (x = 0; x < width; x++) {
423             b2[x] -= (b1[x] + b3[x] + 2) >> 2;
424             b1[x] += (b0[x] + b2[x])     >> 1;
425         }
426     } else {
427         if (y + 1 < (unsigned)height)
428             vertical_compose53iL0(b1, b2, b3, width);
429         if (y + 0 < (unsigned)height)
430             vertical_compose53iH0(b0, b1, b2, width);
431     }
432
433     if (y - 1 < (unsigned)height)
434         horizontal_compose53i(b0, temp, width);
435     if (y + 0 < (unsigned)height)
436         horizontal_compose53i(b1, temp, width);
437
438     cs->b0  = b2;
439     cs->b1  = b3;
440     cs->y  += 2;
441 }
442
443 static void spatial_compose53i_dy(DWTCompose *cs, IDWTELEM *buffer,
444                                   IDWTELEM *temp, int width, int height,
445                                   int stride)
446 {
447     int y        = cs->y;
448     IDWTELEM *b0 = cs->b0;
449     IDWTELEM *b1 = cs->b1;
450     IDWTELEM *b2 = buffer + mirror(y + 1, height - 1) * stride;
451     IDWTELEM *b3 = buffer + mirror(y + 2, height - 1) * stride;
452
453     if (y + 1 < (unsigned)height)
454         vertical_compose53iL0(b1, b2, b3, width);
455     if (y + 0 < (unsigned)height)
456         vertical_compose53iH0(b0, b1, b2, width);
457
458     if (y - 1 < (unsigned)height)
459         horizontal_compose53i(b0, temp, width);
460     if (y + 0 < (unsigned)height)
461         horizontal_compose53i(b1, temp, width);
462
463     cs->b0  = b2;
464     cs->b1  = b3;
465     cs->y  += 2;
466 }
467
468 static void av_unused spatial_compose53i(IDWTELEM *buffer, IDWTELEM *temp,
469                                          int width, int height, int stride)
470 {
471     DWTCompose cs;
472     spatial_compose53i_init(&cs, buffer, height, stride);
473     while (cs.y <= height)
474         spatial_compose53i_dy(&cs, buffer, temp, width, height, stride);
475 }
476
477 void ff_snow_horizontal_compose97i(IDWTELEM *b, IDWTELEM *temp, int width)
478 {
479     const int w2 = (width + 1) >> 1;
480     int x;
481
482     temp[0] = b[0] - ((3 * b[w2] + 2) >> 2);
483     for (x = 1; x < (width >> 1); x++) {
484         temp[2 * x]     = b[x] - ((3 * (b[x + w2 - 1] + b[x + w2]) + 4) >> 3);
485         temp[2 * x - 1] = b[x + w2 - 1] - temp[2 * x - 2] - temp[2 * x];
486     }
487     if (width & 1) {
488         temp[2 * x]     = b[x] - ((3 * b[x + w2 - 1] + 2) >> 2);
489         temp[2 * x - 1] = b[x + w2 - 1] - temp[2 * x - 2] - temp[2 * x];
490     } else
491         temp[2 * x - 1] = b[x + w2 - 1] - 2 * temp[2 * x - 2];
492
493     b[0] = temp[0] + ((2 * temp[0] + temp[1] + 4) >> 3);
494     for (x = 2; x < width - 1; x += 2) {
495         b[x]     = temp[x] + ((4 * temp[x] + temp[x - 1] + temp[x + 1] + 8) >> 4);
496         b[x - 1] = temp[x - 1] + ((3 * (b[x - 2] + b[x])) >> 1);
497     }
498     if (width & 1) {
499         b[x]     = temp[x] + ((2 * temp[x] + temp[x - 1] + 4) >> 3);
500         b[x - 1] = temp[x - 1] + ((3 * (b[x - 2] + b[x])) >> 1);
501     } else
502         b[x - 1] = temp[x - 1] + 3 * b[x - 2];
503 }
504
505 static void vertical_compose97iH0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2,
506                                   int width)
507 {
508     int i;
509
510     for (i = 0; i < width; i++)
511         b1[i] += (W_AM * (b0[i] + b2[i]) + W_AO) >> W_AS;
512 }
513
514 static void vertical_compose97iH1(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2,
515                                   int width)
516 {
517     int i;
518
519     for (i = 0; i < width; i++)
520         b1[i] -= (W_CM * (b0[i] + b2[i]) + W_CO) >> W_CS;
521 }
522
523 static void vertical_compose97iL0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2,
524                                   int width)
525 {
526     int i;
527
528     for (i = 0; i < width; i++)
529         b1[i] += (W_BM * (b0[i] + b2[i]) + 4 * b1[i] + W_BO) >> W_BS;
530 }
531
532 static void vertical_compose97iL1(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2,
533                                   int width)
534 {
535     int i;
536
537     for (i = 0; i < width; i++)
538         b1[i] -= (W_DM * (b0[i] + b2[i]) + W_DO) >> W_DS;
539 }
540
541 void ff_snow_vertical_compose97i(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2,
542                                  IDWTELEM *b3, IDWTELEM *b4, IDWTELEM *b5,
543                                  int width)
544 {
545     int i;
546
547     for (i = 0; i < width; i++) {
548         b4[i] -= (W_DM * (b3[i] + b5[i]) + W_DO) >> W_DS;
549         b3[i] -= (W_CM * (b2[i] + b4[i]) + W_CO) >> W_CS;
550         b2[i] += (W_BM * (b1[i] + b3[i]) + 4 * b2[i] + W_BO) >> W_BS;
551         b1[i] += (W_AM * (b0[i] + b2[i]) + W_AO) >> W_AS;
552     }
553 }
554
555 static void spatial_compose97i_buffered_init(DWTCompose *cs, slice_buffer *sb,
556                                              int height, int stride_line)
557 {
558     cs->b0 = slice_buffer_get_line(sb, mirror(-3 - 1, height - 1) * stride_line);
559     cs->b1 = slice_buffer_get_line(sb, mirror(-3,     height - 1) * stride_line);
560     cs->b2 = slice_buffer_get_line(sb, mirror(-3 + 1, height - 1) * stride_line);
561     cs->b3 = slice_buffer_get_line(sb, mirror(-3 + 2, height - 1) * stride_line);
562     cs->y  = -3;
563 }
564
565 static void spatial_compose97i_init(DWTCompose *cs, IDWTELEM *buffer, int height,
566                                     int stride)
567 {
568     cs->b0 = buffer + mirror(-3 - 1, height - 1) * stride;
569     cs->b1 = buffer + mirror(-3,     height - 1) * stride;
570     cs->b2 = buffer + mirror(-3 + 1, height - 1) * stride;
571     cs->b3 = buffer + mirror(-3 + 2, height - 1) * stride;
572     cs->y  = -3;
573 }
574
575 static void spatial_compose97i_dy_buffered(DWTContext *dsp, DWTCompose *cs,
576                                            slice_buffer * sb, IDWTELEM *temp,
577                                            int width, int height,
578                                            int stride_line)
579 {
580     int y = cs->y;
581
582     IDWTELEM *b0 = cs->b0;
583     IDWTELEM *b1 = cs->b1;
584     IDWTELEM *b2 = cs->b2;
585     IDWTELEM *b3 = cs->b3;
586     IDWTELEM *b4 = slice_buffer_get_line(sb,
587                                          mirror(y + 3, height - 1) *
588                                          stride_line);
589     IDWTELEM *b5 = slice_buffer_get_line(sb,
590                                          mirror(y + 4, height - 1) *
591                                          stride_line);
592
593     if (y > 0 && y + 4 < height) {
594         dsp->vertical_compose97i(b0, b1, b2, b3, b4, b5, width);
595     } else {
596         if (y + 3 < (unsigned)height)
597             vertical_compose97iL1(b3, b4, b5, width);
598         if (y + 2 < (unsigned)height)
599             vertical_compose97iH1(b2, b3, b4, width);
600         if (y + 1 < (unsigned)height)
601             vertical_compose97iL0(b1, b2, b3, width);
602         if (y + 0 < (unsigned)height)
603             vertical_compose97iH0(b0, b1, b2, width);
604     }
605
606     if (y - 1 < (unsigned)height)
607         dsp->horizontal_compose97i(b0, temp, width);
608     if (y + 0 < (unsigned)height)
609         dsp->horizontal_compose97i(b1, temp, width);
610
611     cs->b0  = b2;
612     cs->b1  = b3;
613     cs->b2  = b4;
614     cs->b3  = b5;
615     cs->y  += 2;
616 }
617
618 static void spatial_compose97i_dy(DWTCompose *cs, IDWTELEM *buffer,
619                                   IDWTELEM *temp, int width, int height,
620                                   int stride)
621 {
622     int y        = cs->y;
623     IDWTELEM *b0 = cs->b0;
624     IDWTELEM *b1 = cs->b1;
625     IDWTELEM *b2 = cs->b2;
626     IDWTELEM *b3 = cs->b3;
627     IDWTELEM *b4 = buffer + mirror(y + 3, height - 1) * stride;
628     IDWTELEM *b5 = buffer + mirror(y + 4, height - 1) * stride;
629
630     if (y + 3 < (unsigned)height)
631         vertical_compose97iL1(b3, b4, b5, width);
632     if (y + 2 < (unsigned)height)
633         vertical_compose97iH1(b2, b3, b4, width);
634     if (y + 1 < (unsigned)height)
635         vertical_compose97iL0(b1, b2, b3, width);
636     if (y + 0 < (unsigned)height)
637         vertical_compose97iH0(b0, b1, b2, width);
638
639     if (y - 1 < (unsigned)height)
640         ff_snow_horizontal_compose97i(b0, temp, width);
641     if (y + 0 < (unsigned)height)
642         ff_snow_horizontal_compose97i(b1, temp, width);
643
644     cs->b0  = b2;
645     cs->b1  = b3;
646     cs->b2  = b4;
647     cs->b3  = b5;
648     cs->y  += 2;
649 }
650
651 static void av_unused spatial_compose97i(IDWTELEM *buffer, IDWTELEM *temp,
652                                          int width, int height, int stride)
653 {
654     DWTCompose cs;
655     spatial_compose97i_init(&cs, buffer, height, stride);
656     while (cs.y <= height)
657         spatial_compose97i_dy(&cs, buffer, temp, width, height, stride);
658 }
659
660 void ff_spatial_idwt_buffered_init(DWTCompose *cs, slice_buffer *sb, int width,
661                                    int height, int stride_line, int type,
662                                    int decomposition_count)
663 {
664     int level;
665     for (level = decomposition_count - 1; level >= 0; level--) {
666         switch (type) {
667         case DWT_97:
668             spatial_compose97i_buffered_init(cs + level, sb, height >> level,
669                                              stride_line << level);
670             break;
671         case DWT_53:
672             spatial_compose53i_buffered_init(cs + level, sb, height >> level,
673                                              stride_line << level);
674             break;
675         }
676     }
677 }
678
679 void ff_spatial_idwt_buffered_slice(DWTContext *dsp, DWTCompose *cs,
680                                     slice_buffer *slice_buf, IDWTELEM *temp,
681                                     int width, int height, int stride_line,
682                                     int type, int decomposition_count, int y)
683 {
684     const int support = type == 1 ? 3 : 5;
685     int level;
686     if (type == 2)
687         return;
688
689     for (level = decomposition_count - 1; level >= 0; level--)
690         while (cs[level].y <= FFMIN((y >> level) + support, height >> level)) {
691             switch (type) {
692             case DWT_97:
693                 spatial_compose97i_dy_buffered(dsp, cs + level, slice_buf, temp,
694                                                width >> level,
695                                                height >> level,
696                                                stride_line << level);
697                 break;
698             case DWT_53:
699                 spatial_compose53i_dy_buffered(cs + level, slice_buf, temp,
700                                                width >> level,
701                                                height >> level,
702                                                stride_line << level);
703                 break;
704             }
705         }
706 }
707
708 static void ff_spatial_idwt_init(DWTCompose *cs, IDWTELEM *buffer, int width,
709                                  int height, int stride, int type,
710                                  int decomposition_count)
711 {
712     int level;
713     for (level = decomposition_count - 1; level >= 0; level--) {
714         switch (type) {
715         case DWT_97:
716             spatial_compose97i_init(cs + level, buffer, height >> level,
717                                     stride << level);
718             break;
719         case DWT_53:
720             spatial_compose53i_init(cs + level, buffer, height >> level,
721                                     stride << level);
722             break;
723         }
724     }
725 }
726
727 static void ff_spatial_idwt_slice(DWTCompose *cs, IDWTELEM *buffer,
728                                   IDWTELEM *temp, int width, int height,
729                                   int stride, int type,
730                                   int decomposition_count, int y)
731 {
732     const int support = type == 1 ? 3 : 5;
733     int level;
734     if (type == 2)
735         return;
736
737     for (level = decomposition_count - 1; level >= 0; level--)
738         while (cs[level].y <= FFMIN((y >> level) + support, height >> level)) {
739             switch (type) {
740             case DWT_97:
741                 spatial_compose97i_dy(cs + level, buffer, temp, width >> level,
742                                       height >> level, stride << level);
743                 break;
744             case DWT_53:
745                 spatial_compose53i_dy(cs + level, buffer, temp, width >> level,
746                                       height >> level, stride << level);
747                 break;
748             }
749         }
750 }
751
752 void ff_spatial_idwt(IDWTELEM *buffer, IDWTELEM *temp, int width, int height,
753                      int stride, int type, int decomposition_count)
754 {
755     DWTCompose cs[MAX_DECOMPOSITIONS];
756     int y;
757     ff_spatial_idwt_init(cs, buffer, width, height, stride, type,
758                          decomposition_count);
759     for (y = 0; y < height; y += 4)
760         ff_spatial_idwt_slice(cs, buffer, temp, width, height, stride, type,
761                               decomposition_count, y);
762 }
763
764 static inline int w_c(void *v, uint8_t *pix1, uint8_t *pix2, int line_size,
765                       int w, int h, int type)
766 {
767     int s, i, j;
768     const int dec_count = w == 8 ? 3 : 4;
769     int tmp[32 * 32], tmp2[32];
770     int level, ori;
771     static const int scale[2][2][4][4] = {
772         {
773             { // 9/7 8x8 dec=3
774                 { 268, 239, 239, 213 },
775                 { 0,   224, 224, 152 },
776                 { 0,   135, 135, 110 },
777             },
778             { // 9/7 16x16 or 32x32 dec=4
779                 { 344, 310, 310, 280 },
780                 { 0,   320, 320, 228 },
781                 { 0,   175, 175, 136 },
782                 { 0,   129, 129, 102 },
783             }
784         },
785         {
786             { // 5/3 8x8 dec=3
787                 { 275, 245, 245, 218 },
788                 { 0,   230, 230, 156 },
789                 { 0,   138, 138, 113 },
790             },
791             { // 5/3 16x16 or 32x32 dec=4
792                 { 352, 317, 317, 286 },
793                 { 0,   328, 328, 233 },
794                 { 0,   180, 180, 140 },
795                 { 0,   132, 132, 105 },
796             }
797         }
798     };
799
800     for (i = 0; i < h; i++) {
801         for (j = 0; j < w; j += 4) {
802             tmp[32 * i + j + 0] = (pix1[j + 0] - pix2[j + 0]) << 4;
803             tmp[32 * i + j + 1] = (pix1[j + 1] - pix2[j + 1]) << 4;
804             tmp[32 * i + j + 2] = (pix1[j + 2] - pix2[j + 2]) << 4;
805             tmp[32 * i + j + 3] = (pix1[j + 3] - pix2[j + 3]) << 4;
806         }
807         pix1 += line_size;
808         pix2 += line_size;
809     }
810
811     ff_spatial_dwt(tmp, tmp2, w, h, 32, type, dec_count);
812
813     s = 0;
814     assert(w == h);
815     for (level = 0; level < dec_count; level++)
816         for (ori = level ? 1 : 0; ori < 4; ori++) {
817             int size   = w >> (dec_count - level);
818             int sx     = (ori & 1) ? size : 0;
819             int stride = 32 << (dec_count - level);
820             int sy     = (ori & 2) ? stride >> 1 : 0;
821
822             for (i = 0; i < size; i++)
823                 for (j = 0; j < size; j++) {
824                     int v = tmp[sx + sy + i * stride + j] *
825                             scale[type][dec_count - 3][level][ori];
826                     s += FFABS(v);
827                 }
828         }
829     assert(s >= 0);
830     return s >> 9;
831 }
832
833 static int w53_8_c(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h)
834 {
835     return w_c(v, pix1, pix2, line_size, 8, h, 1);
836 }
837
838 static int w97_8_c(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h)
839 {
840     return w_c(v, pix1, pix2, line_size, 8, h, 0);
841 }
842
843 static int w53_16_c(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h)
844 {
845     return w_c(v, pix1, pix2, line_size, 16, h, 1);
846 }
847
848 static int w97_16_c(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h)
849 {
850     return w_c(v, pix1, pix2, line_size, 16, h, 0);
851 }
852
853 int ff_w53_32_c(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h)
854 {
855     return w_c(v, pix1, pix2, line_size, 32, h, 1);
856 }
857
858 int ff_w97_32_c(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h)
859 {
860     return w_c(v, pix1, pix2, line_size, 32, h, 0);
861 }
862
863 void ff_dsputil_init_dwt(DSPContext *c)
864 {
865     c->w53[0] = w53_16_c;
866     c->w53[1] = w53_8_c;
867     c->w97[0] = w97_16_c;
868     c->w97[1] = w97_8_c;
869 }
870
871 void ff_dwt_init(DWTContext *c)
872 {
873     c->vertical_compose97i   = ff_snow_vertical_compose97i;
874     c->horizontal_compose97i = ff_snow_horizontal_compose97i;
875     c->inner_add_yblock      = ff_snow_inner_add_yblock;
876
877     if (HAVE_MMX)
878         ff_dwt_init_x86(c);
879 }