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