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