pngdec: decode and expose iCCP chunks as side data
[ffmpeg.git] / libavcodec / dirac_dwt_template.c
1 /*
2  * Copyright (C) 2004-2010 Michael Niedermayer <michaelni@gmx.at>
3  * Copyright (C) 2008 David Conrad
4  * Copyright (C) 2015 Open Broadcast Systems Ltd.
5  * Author    (C) 2015 Rostislav Pehlivanov <atomnuker@gmail.com>
6  *
7  * This file is part of FFmpeg.
8  *
9  * FFmpeg is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * FFmpeg is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with FFmpeg; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  */
23
24 #if defined(TEMPLATE_8bit)
25
26 #    define RENAME(N)   N ## _8bit
27 #    define TYPE        int16_t
28 #    undef  TEMPLATE_8bit
29
30 #elif defined(TEMPLATE_10bit)
31
32 #    define RENAME(N)   N ## _10bit
33 #    define TYPE        int32_t
34 #    undef  TEMPLATE_10bit
35
36 #elif defined(TEMPLATE_12bit)
37
38 #    define RENAME(N)   N ## _12bit
39 #    define TYPE        int32_t
40 #    undef  TEMPLATE_12bit
41
42 #endif
43
44 static void RENAME(vertical_compose53iL0)(uint8_t *_b0, uint8_t *_b1, uint8_t *_b2,
45                                           int width)
46 {
47     int i;
48     TYPE *b0 = (TYPE *)_b0;
49     TYPE *b1 = (TYPE *)_b1;
50     TYPE *b2 = (TYPE *)_b2;
51     for (i = 0; i < width; i++)
52         b1[i] -= (b0[i] + b2[i] + 2) >> 2;
53 }
54
55 static av_always_inline void RENAME(interleave)(TYPE *dst, TYPE *src0, TYPE *src1, int w2,
56                                                 int add, int shift)
57 {
58     int i;
59     for (i = 0; i < w2; i++) {
60         dst[2*i  ] = (src0[i] + add) >> shift;
61         dst[2*i+1] = (src1[i] + add) >> shift;
62     }
63 }
64
65 static void RENAME(horizontal_compose_dirac53i)(uint8_t *_b, uint8_t *_temp, int w)
66 {
67     int x;
68     const int w2 = w >> 1;
69     TYPE *b     = (TYPE *)_b;
70     TYPE *temp  = (TYPE *)_temp;
71
72     temp[0] = COMPOSE_53iL0(b[w2], b[0], b[w2]);
73     for (x = 1; x < w2; x++) {
74         temp[x     ] = COMPOSE_53iL0     (b[x+w2-1], b[x     ], b[x+w2]);
75         temp[x+w2-1] = COMPOSE_DIRAC53iH0(temp[x-1], b[x+w2-1], temp[x]);
76     }
77     temp[w-1] = COMPOSE_DIRAC53iH0(temp[w2-1], b[w-1], temp[w2-1]);
78
79     RENAME(interleave)(b, temp, temp+w2, w2, 1, 1);
80 }
81
82 static void RENAME(horizontal_compose_dd97i)(uint8_t *_b, uint8_t *_tmp, int w)
83 {
84     int x;
85     const int w2 = w >> 1;
86     TYPE *b   = (TYPE *)_b;
87     TYPE *tmp = (TYPE *)_tmp;
88
89     tmp[0] = COMPOSE_53iL0(b[w2], b[0], b[w2]);
90     for (x = 1; x < w2; x++)
91         tmp[x] = COMPOSE_53iL0(b[x+w2-1], b[x], b[x+w2]);
92
93     // extend the edges
94     tmp[-1]   = tmp[0];
95     tmp[w2+1] = tmp[w2] = tmp[w2-1];
96
97     for (x = 0; x < w2; x++) {
98         b[2*x  ] = (tmp[x] + 1)>>1;
99         b[2*x+1] = (COMPOSE_DD97iH0(tmp[x-1], tmp[x], b[x+w2], tmp[x+1], tmp[x+2]) + 1)>>1;
100     }
101 }
102
103 static void RENAME(horizontal_compose_dd137i)(uint8_t *_b, uint8_t *_tmp, int w)
104 {
105     const int w2 = w >> 1;
106     int x;
107     TYPE *b   = (TYPE *)_b;
108     TYPE *tmp = (TYPE *)_tmp;
109
110     tmp[0] = COMPOSE_DD137iL0(b[w2], b[w2], b[0], b[w2  ], b[w2+1]);
111     tmp[1] = COMPOSE_DD137iL0(b[w2], b[w2], b[1], b[w2+1], b[w2+2]);
112     for (x = 2; x < w2-1; x++)
113         tmp[x] = COMPOSE_DD137iL0(b[x+w2-2], b[x+w2-1], b[x], b[x+w2], b[x+w2+1]);
114     tmp[w2-1] = COMPOSE_DD137iL0(b[w-3], b[w-2], b[w2-1], b[w-1], b[w-1]);
115
116     // extend the edges
117     tmp[-1]   = tmp[0];
118     tmp[w2+1] = tmp[w2] = tmp[w2-1];
119
120     for (x = 0; x < w2; x++) {
121         b[2*x  ] = (tmp[x] + 1)>>1;
122         b[2*x+1] = (COMPOSE_DD97iH0(tmp[x-1], tmp[x], b[x+w2], tmp[x+1], tmp[x+2]) + 1)>>1;
123     }
124 }
125
126 static av_always_inline void RENAME(horizontal_compose_haari)(TYPE *b, TYPE *temp,
127                                                               int w, int shift)
128 {
129     const int w2 = w >> 1;
130     int x;
131
132     for (x = 0; x < w2; x++) {
133         temp[x   ] = COMPOSE_HAARiL0(b[x   ], b[x+w2]);
134         temp[x+w2] = COMPOSE_HAARiH0(b[x+w2], temp[x]);
135     }
136
137     RENAME(interleave)(b, temp, temp+w2, w2, shift, shift);
138 }
139
140 static void RENAME(horizontal_compose_haar0i)(uint8_t *_b, uint8_t *_temp, int w)
141 {
142     TYPE *b    = (TYPE *)_b;
143     TYPE *temp = (TYPE *)_temp;
144     RENAME(horizontal_compose_haari)(b, temp, w, 0);
145 }
146
147 static void RENAME(horizontal_compose_haar1i)(uint8_t *_b, uint8_t *_temp, int w)
148 {
149     TYPE *b    = (TYPE *)_b;
150     TYPE *temp = (TYPE *)_temp;
151     RENAME(horizontal_compose_haari)(b, temp, w, 1);
152 }
153
154 static void RENAME(horizontal_compose_fidelityi)(uint8_t *_b, uint8_t *_tmp, int w)
155 {
156     const int w2 = w >> 1;
157     int i, x;
158     TYPE v[8];
159     TYPE *b   = (TYPE *)_b;
160     TYPE *tmp = (TYPE *)_tmp;
161
162     for (x = 0; x < w2; x++) {
163         for (i = 0; i < 8; i++)
164             v[i] = b[av_clip(x-3+i, 0, w2-1)];
165         tmp[x] = COMPOSE_FIDELITYiH0(v[0], v[1], v[2], v[3], b[x+w2], v[4], v[5], v[6], v[7]);
166     }
167
168     for (x = 0; x < w2; x++) {
169         for (i = 0; i < 8; i++)
170             v[i] = tmp[av_clip(x-4+i, 0, w2-1)];
171         tmp[x+w2] = COMPOSE_FIDELITYiL0(v[0], v[1], v[2], v[3], b[x], v[4], v[5], v[6], v[7]);
172     }
173
174     RENAME(interleave)(b, tmp+w2, tmp, w2, 0, 0);
175 }
176
177 static void RENAME(horizontal_compose_daub97i)(uint8_t *_b, uint8_t *_temp, int w)
178 {
179     const int w2 = w >> 1;
180     int x, b0, b1, b2;
181     TYPE *b    = (TYPE *)_b;
182     TYPE *temp = (TYPE *)_temp;
183
184     temp[0] = COMPOSE_DAUB97iL1(b[w2], b[0], b[w2]);
185     for (x = 1; x < w2; x++) {
186         temp[x     ] = COMPOSE_DAUB97iL1(b[x+w2-1], b[x     ], b[x+w2]);
187         temp[x+w2-1] = COMPOSE_DAUB97iH1(temp[x-1], b[x+w2-1], temp[x]);
188     }
189     temp[w-1] = COMPOSE_DAUB97iH1(temp[w2-1], b[w-1], temp[w2-1]);
190
191     // second stage combined with interleave and shift
192     b0 = b2 = COMPOSE_DAUB97iL0(temp[w2], temp[0], temp[w2]);
193     b[0] = (b0 + 1) >> 1;
194     for (x = 1; x < w2; x++) {
195         b2 = COMPOSE_DAUB97iL0(temp[x+w2-1], temp[x     ], temp[x+w2]);
196         b1 = COMPOSE_DAUB97iH0(          b0, temp[x+w2-1], b2        );
197         b[2*x-1] = (b1 + 1) >> 1;
198         b[2*x  ] = (b2 + 1) >> 1;
199         b0 = b2;
200     }
201     b[w-1] = (COMPOSE_DAUB97iH0(b2, temp[w-1], b2) + 1) >> 1;
202 }
203
204 static void RENAME(vertical_compose_dirac53iH0)(uint8_t *_b0, uint8_t *_b1, uint8_t *_b2,
205                                                 int width)
206 {
207     int i;
208     TYPE *b0  = (TYPE *)_b0;
209     TYPE *b1 = (TYPE *)_b1;
210     TYPE *b2 = (TYPE *)_b2;
211     for(i=0; i<width; i++){
212         b1[i] = COMPOSE_DIRAC53iH0(b0[i], b1[i], b2[i]);
213     }
214 }
215
216 static void RENAME(vertical_compose_dd97iH0)(uint8_t *_b0, uint8_t *_b1, uint8_t *_b2,
217                                              uint8_t *_b3, uint8_t *_b4, int width)
218 {
219     int i;
220     TYPE *b0 = (TYPE *)_b0;
221     TYPE *b1 = (TYPE *)_b1;
222     TYPE *b2 = (TYPE *)_b2;
223     TYPE *b3 = (TYPE *)_b3;
224     TYPE *b4 = (TYPE *)_b4;
225     for(i=0; i<width; i++){
226         b2[i] = COMPOSE_DD97iH0(b0[i], b1[i], b2[i], b3[i], b4[i]);
227     }
228 }
229
230 static void RENAME(vertical_compose_dd137iL0)(uint8_t *_b0, uint8_t *_b1, uint8_t *_b2,
231                                               uint8_t *_b3, uint8_t *_b4, int width)
232 {
233     int i;
234     TYPE *b0 = (TYPE *)_b0;
235     TYPE *b1 = (TYPE *)_b1;
236     TYPE *b2 = (TYPE *)_b2;
237     TYPE *b3 = (TYPE *)_b3;
238     TYPE *b4 = (TYPE *)_b4;
239     for(i=0; i<width; i++){
240         b2[i] = COMPOSE_DD137iL0(b0[i], b1[i], b2[i], b3[i], b4[i]);
241     }
242 }
243
244 static void RENAME(vertical_compose_haar)(uint8_t *_b0, uint8_t *_b1, int width)
245 {
246     int i;
247     TYPE *b0 = (TYPE *)_b0;
248     TYPE *b1 = (TYPE *)_b1;
249
250     for (i = 0; i < width; i++) {
251         b0[i] = COMPOSE_HAARiL0(b0[i], b1[i]);
252         b1[i] = COMPOSE_HAARiH0(b1[i], b0[i]);
253     }
254 }
255
256 static void RENAME(vertical_compose_fidelityiH0)(uint8_t *_dst, uint8_t *_b[8], int width)
257 {
258     int i;
259     TYPE *dst = (TYPE *)_dst;
260     TYPE *b0  = (TYPE *)_b[0];
261     TYPE *b1  = (TYPE *)_b[1];
262     TYPE *b2  = (TYPE *)_b[2];
263     TYPE *b3  = (TYPE *)_b[3];
264     TYPE *b4  = (TYPE *)_b[4];
265     TYPE *b5  = (TYPE *)_b[5];
266     TYPE *b6  = (TYPE *)_b[6];
267     TYPE *b7  = (TYPE *)_b[7];
268     for(i=0; i<width; i++){
269         dst[i] = COMPOSE_FIDELITYiH0(b0[i], b1[i], b2[i], b3[i], dst[i], b4[i], b5[i], b6[i], b7[i]);
270     }
271 }
272
273 static void RENAME(vertical_compose_fidelityiL0)(uint8_t *_dst, uint8_t *_b[8], int width)
274 {
275     int i;
276     TYPE *dst = (TYPE *)_dst;
277     TYPE *b0  = (TYPE *)_b[0];
278     TYPE *b1  = (TYPE *)_b[1];
279     TYPE *b2  = (TYPE *)_b[2];
280     TYPE *b3  = (TYPE *)_b[3];
281     TYPE *b4  = (TYPE *)_b[4];
282     TYPE *b5  = (TYPE *)_b[5];
283     TYPE *b6  = (TYPE *)_b[6];
284     TYPE *b7  = (TYPE *)_b[7];
285
286     for(i=0; i<width; i++){
287         dst[i] = COMPOSE_FIDELITYiL0(b0[i], b1[i], b2[i], b3[i], dst[i], b4[i], b5[i], b6[i], b7[i]);
288     }
289 }
290
291 static void RENAME(vertical_compose_daub97iH0)(uint8_t *_b0, uint8_t *_b1, uint8_t *_b2, int width)
292 {
293     int i;
294     TYPE *b0 = (TYPE *)_b0;
295     TYPE *b1 = (TYPE *)_b1;
296     TYPE *b2 = (TYPE *)_b2;
297
298     for(i=0; i<width; i++){
299         b1[i] = COMPOSE_DAUB97iH0(b0[i], b1[i], b2[i]);
300     }
301 }
302
303 static void RENAME(vertical_compose_daub97iH1)(uint8_t *_b0, uint8_t *_b1, uint8_t *_b2, int width)
304 {
305     int i;
306     TYPE *b0 = (TYPE *)_b0;
307     TYPE *b1 = (TYPE *)_b1;
308     TYPE *b2 = (TYPE *)_b2;
309
310     for(i=0; i<width; i++){
311         b1[i] = COMPOSE_DAUB97iH1(b0[i], b1[i], b2[i]);
312     }
313 }
314
315 static void RENAME(vertical_compose_daub97iL0)(uint8_t *_b0, uint8_t *_b1, uint8_t *_b2, int width)
316 {
317     int i;
318     TYPE *b0 = (TYPE *)_b0;
319     TYPE *b1 = (TYPE *)_b1;
320     TYPE *b2 = (TYPE *)_b2;
321
322     for(i=0; i<width; i++){
323         b1[i] = COMPOSE_DAUB97iL0(b0[i], b1[i], b2[i]);
324     }
325 }
326
327 static void RENAME(vertical_compose_daub97iL1)(uint8_t *_b0, uint8_t *_b1, uint8_t *_b2, int width)
328 {
329     int i;
330     TYPE *b0 = (TYPE *)_b0;
331     TYPE *b1 = (TYPE *)_b1;
332     TYPE *b2 = (TYPE *)_b2;
333
334     for(i=0; i<width; i++){
335         b1[i] = COMPOSE_DAUB97iL1(b0[i], b1[i], b2[i]);
336     }
337 }
338
339 static void RENAME(spatial_compose_dd97i_dy)(DWTContext *d, int level, int width, int height, int stride)
340 {
341     vertical_compose_3tap vertical_compose_l0 = (void*)d->vertical_compose_l0;
342     vertical_compose_5tap vertical_compose_h0 = (void*)d->vertical_compose_h0;
343     DWTCompose *cs = d->cs + level;
344
345     int i, y = cs->y;
346     uint8_t *b[8];
347     for (i = 0; i < 6; i++)
348         b[i] = cs->b[i];
349     b[6] = d->buffer + av_clip(y+5, 0, height-2)*stride;
350     b[7] = d->buffer + av_clip(y+6, 1, height-1)*stride;
351
352     if(y+5<(unsigned)height) vertical_compose_l0(      b[5], b[6], b[7],       width);
353     if(y+1<(unsigned)height) vertical_compose_h0(b[0], b[2], b[3], b[4], b[6], width);
354
355     if(y-1<(unsigned)height) d->horizontal_compose(b[0], d->temp, width);
356     if(y+0<(unsigned)height) d->horizontal_compose(b[1], d->temp, width);
357
358     for (i = 0; i < 6; i++)
359         cs->b[i] = b[i+2];
360     cs->y += 2;
361 }
362
363 static void RENAME(spatial_compose_dirac53i_dy)(DWTContext *d, int level, int width, int height, int stride)
364 {
365     vertical_compose_3tap vertical_compose_l0 = (void*)d->vertical_compose_l0;
366     vertical_compose_3tap vertical_compose_h0 = (void*)d->vertical_compose_h0;
367     DWTCompose *cs = d->cs + level;
368
369     int y= cs->y;
370     uint8_t *b[4] = { cs->b[0], cs->b[1] };
371     b[2] = d->buffer + avpriv_mirror(y+1, height-1)*stride;
372     b[3] = d->buffer + avpriv_mirror(y+2, height-1)*stride;
373
374     if(y+1<(unsigned)height) vertical_compose_l0(b[1], b[2], b[3], width);
375     if(y+0<(unsigned)height) vertical_compose_h0(b[0], b[1], b[2], width);
376
377     if(y-1<(unsigned)height) d->horizontal_compose(b[0], d->temp, width);
378     if(y+0<(unsigned)height) d->horizontal_compose(b[1], d->temp, width);
379
380     cs->b[0] = b[2];
381     cs->b[1] = b[3];
382     cs->y += 2;
383 }
384
385 static void RENAME(spatial_compose_dd137i_dy)(DWTContext *d, int level, int width, int height, int stride)
386 {
387     vertical_compose_5tap vertical_compose_l0 = (void*)d->vertical_compose_l0;
388     vertical_compose_5tap vertical_compose_h0 = (void*)d->vertical_compose_h0;
389     DWTCompose *cs = d->cs + level;
390
391     int i, y = cs->y;
392     uint8_t *b[10];
393     for (i = 0; i < 8; i++)
394         b[i] = cs->b[i];
395     b[8] = d->buffer + av_clip(y+7, 0, height-2)*stride;
396     b[9] = d->buffer + av_clip(y+8, 1, height-1)*stride;
397
398     if(y+5<(unsigned)height) vertical_compose_l0(b[3], b[5], b[6], b[7], b[9], width);
399     if(y+1<(unsigned)height) vertical_compose_h0(b[0], b[2], b[3], b[4], b[6], width);
400
401     if(y-1<(unsigned)height) d->horizontal_compose(b[0], d->temp, width);
402     if(y+0<(unsigned)height) d->horizontal_compose(b[1], d->temp, width);
403
404     for (i = 0; i < 8; i++)
405         cs->b[i] = b[i+2];
406     cs->y += 2;
407 }
408
409 // haar makes the assumption that height is even (always true for dirac)
410 static void RENAME(spatial_compose_haari_dy)(DWTContext *d, int level, int width, int height, int stride)
411 {
412     vertical_compose_2tap vertical_compose = (void*)d->vertical_compose;
413     int y = d->cs[level].y;
414     uint8_t *b0 = d->buffer + (y-1)*stride;
415     uint8_t *b1 = d->buffer + (y  )*stride;
416
417     vertical_compose(b0, b1, width);
418     d->horizontal_compose(b0, d->temp, width);
419     d->horizontal_compose(b1, d->temp, width);
420
421     d->cs[level].y += 2;
422 }
423
424 // Don't do sliced idwt for fidelity; the 9 tap filter makes it a bit annoying
425 // Fortunately, this filter isn't used in practice.
426 static void RENAME(spatial_compose_fidelity)(DWTContext *d, int level, int width, int height, int stride)
427 {
428     vertical_compose_9tap vertical_compose_l0 = (void*)d->vertical_compose_l0;
429     vertical_compose_9tap vertical_compose_h0 = (void*)d->vertical_compose_h0;
430     int i, y;
431     uint8_t *b[8];
432
433     for (y = 1; y < height; y += 2) {
434         for (i = 0; i < 8; i++)
435             b[i] = d->buffer + av_clip((y-7 + 2*i), 0, height-2)*stride;
436         vertical_compose_h0(d->buffer + y*stride, b, width);
437     }
438
439     for (y = 0; y < height; y += 2) {
440         for (i = 0; i < 8; i++)
441             b[i] = d->buffer + av_clip((y-7 + 2*i), 1, height-1)*stride;
442         vertical_compose_l0(d->buffer + y*stride, b, width);
443     }
444
445     for (y = 0; y < height; y++)
446         d->horizontal_compose(d->buffer + y*stride, d->temp, width);
447
448     d->cs[level].y = height+1;
449 }
450
451 static void RENAME(spatial_compose_daub97i_dy)(DWTContext *d, int level, int width, int height, int stride)
452 {
453     vertical_compose_3tap vertical_compose_l0 = (void*)d->vertical_compose_l0;
454     vertical_compose_3tap vertical_compose_h0 = (void*)d->vertical_compose_h0;
455     vertical_compose_3tap vertical_compose_l1 = (void*)d->vertical_compose_l1;
456     vertical_compose_3tap vertical_compose_h1 = (void*)d->vertical_compose_h1;
457     DWTCompose *cs = d->cs + level;
458
459     int i, y = cs->y;
460     uint8_t *b[6];
461     for (i = 0; i < 4; i++)
462         b[i] = cs->b[i];
463     b[4] = d->buffer + avpriv_mirror(y+3, height-1)*stride;
464     b[5] = d->buffer + avpriv_mirror(y+4, height-1)*stride;
465
466     if(y+3<(unsigned)height) vertical_compose_l1(b[3], b[4], b[5], width);
467     if(y+2<(unsigned)height) vertical_compose_h1(b[2], b[3], b[4], width);
468     if(y+1<(unsigned)height) vertical_compose_l0(b[1], b[2], b[3], width);
469     if(y+0<(unsigned)height) vertical_compose_h0(b[0], b[1], b[2], width);
470
471     if(y-1<(unsigned)height) d->horizontal_compose(b[0], d->temp, width);
472     if(y+0<(unsigned)height) d->horizontal_compose(b[1], d->temp, width);
473
474     for (i = 0; i < 4; i++)
475         cs->b[i] = b[i+2];
476     cs->y += 2;
477 }
478
479 static void RENAME(spatial_compose97i_init)(DWTCompose *cs, uint8_t *buffer, int height, int stride)
480 {
481     cs->b[0] = buffer + avpriv_mirror(-3-1, height-1)*stride;
482     cs->b[1] = buffer + avpriv_mirror(-3  , height-1)*stride;
483     cs->b[2] = buffer + avpriv_mirror(-3+1, height-1)*stride;
484     cs->b[3] = buffer + avpriv_mirror(-3+2, height-1)*stride;
485     cs->y = -3;
486 }
487
488 static void RENAME(spatial_compose53i_init)(DWTCompose *cs, uint8_t *buffer, int height, int stride)
489 {
490     cs->b[0] = buffer + avpriv_mirror(-1-1, height-1)*stride;
491     cs->b[1] = buffer + avpriv_mirror(-1  , height-1)*stride;
492     cs->y = -1;
493 }
494
495 static void RENAME(spatial_compose_dd97i_init)(DWTCompose *cs, uint8_t *buffer, int height, int stride)
496 {
497     cs->b[0] = buffer + av_clip(-5-1, 0, height-2)*stride;
498     cs->b[1] = buffer + av_clip(-5  , 1, height-1)*stride;
499     cs->b[2] = buffer + av_clip(-5+1, 0, height-2)*stride;
500     cs->b[3] = buffer + av_clip(-5+2, 1, height-1)*stride;
501     cs->b[4] = buffer + av_clip(-5+3, 0, height-2)*stride;
502     cs->b[5] = buffer + av_clip(-5+4, 1, height-1)*stride;
503     cs->y = -5;
504 }
505
506 static void RENAME(spatial_compose_dd137i_init)(DWTCompose *cs, uint8_t *buffer, int height, int stride)
507 {
508     cs->b[0] = buffer + av_clip(-5-1, 0, height-2)*stride;
509     cs->b[1] = buffer + av_clip(-5  , 1, height-1)*stride;
510     cs->b[2] = buffer + av_clip(-5+1, 0, height-2)*stride;
511     cs->b[3] = buffer + av_clip(-5+2, 1, height-1)*stride;
512     cs->b[4] = buffer + av_clip(-5+3, 0, height-2)*stride;
513     cs->b[5] = buffer + av_clip(-5+4, 1, height-1)*stride;
514     cs->b[6] = buffer + av_clip(-5+5, 0, height-2)*stride;
515     cs->b[7] = buffer + av_clip(-5+6, 1, height-1)*stride;
516     cs->y = -5;
517 }
518
519 static int RENAME(ff_spatial_idwt_init)(DWTContext *d, enum dwt_type type)
520 {
521     int level;
522
523     d->temp = (uint8_t *)(((TYPE *)d->temp) + 8);
524
525     for (level = d->decomposition_count - 1; level >= 0; level--){
526         int hl = d->height >> level;
527         int stride_l = d->stride << level;
528
529         switch(type){
530             case DWT_DIRAC_DD9_7:
531                 RENAME(spatial_compose_dd97i_init)(d->cs+level, d->buffer, hl, stride_l);
532                 break;
533             case DWT_DIRAC_LEGALL5_3:
534                 RENAME(spatial_compose53i_init)(d->cs+level, d->buffer, hl, stride_l);
535                 break;
536             case DWT_DIRAC_DD13_7:
537                 RENAME(spatial_compose_dd137i_init)(d->cs+level, d->buffer, hl, stride_l);
538                 break;
539             case DWT_DIRAC_HAAR0:
540             case DWT_DIRAC_HAAR1:
541                 d->cs[level].y = 1;
542                 break;
543             case DWT_DIRAC_DAUB9_7:
544                 RENAME(spatial_compose97i_init)(d->cs+level, d->buffer, hl, stride_l);
545                 break;
546             default:
547                 d->cs[level].y = 0;
548                 break;
549         }
550     }
551
552     switch (type) {
553         case DWT_DIRAC_DD9_7:
554             d->spatial_compose = RENAME(spatial_compose_dd97i_dy);
555             d->vertical_compose_l0 = (void*)RENAME(vertical_compose53iL0);
556             d->vertical_compose_h0 = (void*)RENAME(vertical_compose_dd97iH0);
557             d->horizontal_compose = RENAME(horizontal_compose_dd97i);
558             d->support = 7;
559             break;
560         case DWT_DIRAC_LEGALL5_3:
561             d->spatial_compose = RENAME(spatial_compose_dirac53i_dy);
562             d->vertical_compose_l0 = (void*)RENAME(vertical_compose53iL0);
563             d->vertical_compose_h0 = (void*)RENAME(vertical_compose_dirac53iH0);
564             d->horizontal_compose = RENAME(horizontal_compose_dirac53i);
565             d->support = 3;
566             break;
567         case DWT_DIRAC_DD13_7:
568             d->spatial_compose = RENAME(spatial_compose_dd137i_dy);
569             d->vertical_compose_l0 = (void*)RENAME(vertical_compose_dd137iL0);
570             d->vertical_compose_h0 = (void*)RENAME(vertical_compose_dd97iH0);
571             d->horizontal_compose = RENAME(horizontal_compose_dd137i);
572             d->support = 7;
573             break;
574         case DWT_DIRAC_HAAR0:
575         case DWT_DIRAC_HAAR1:
576             d->spatial_compose = RENAME(spatial_compose_haari_dy);
577             d->vertical_compose = (void*)RENAME(vertical_compose_haar);
578             if (type == DWT_DIRAC_HAAR0)
579                 d->horizontal_compose = RENAME(horizontal_compose_haar0i);
580             else
581                 d->horizontal_compose = RENAME(horizontal_compose_haar1i);
582             d->support = 1;
583             break;
584         case DWT_DIRAC_FIDELITY:
585             d->spatial_compose = RENAME(spatial_compose_fidelity);
586             d->vertical_compose_l0 = (void*)RENAME(vertical_compose_fidelityiL0);
587             d->vertical_compose_h0 = (void*)RENAME(vertical_compose_fidelityiH0);
588             d->horizontal_compose = RENAME(horizontal_compose_fidelityi);
589             d->support = 0; // not really used
590             break;
591         case DWT_DIRAC_DAUB9_7:
592             d->spatial_compose = RENAME(spatial_compose_daub97i_dy);
593             d->vertical_compose_l0 = (void*)RENAME(vertical_compose_daub97iL0);
594             d->vertical_compose_h0 = (void*)RENAME(vertical_compose_daub97iH0);
595             d->vertical_compose_l1 = (void*)RENAME(vertical_compose_daub97iL1);
596             d->vertical_compose_h1 = (void*)RENAME(vertical_compose_daub97iH1);
597             d->horizontal_compose = RENAME(horizontal_compose_daub97i);
598             d->support = 5;
599             break;
600         default:
601             return AVERROR_INVALIDDATA;
602     }
603
604     return 0;
605 }
606
607 #undef RENAME
608 #undef TYPE