2 * Copyright (C) 2004-2010 Michael Niedermayer <michaelni@gmx.at>
3 * Copyright (C) 2008 David Conrad
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "libavutil/attributes.h"
23 #include "libavutil/avassert.h"
24 #include "libavutil/common.h"
25 #include "dirac_dwt.h"
26 #include "libavcodec/x86/dirac_dwt.h"
29 static inline int mirror(int v, int m)
31 while ((unsigned)v > (unsigned)m) {
39 static void vertical_compose53iL0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2,
44 for (i = 0; i < width; i++)
45 b1[i] -= (b0[i] + b2[i] + 2) >> 2;
49 static av_always_inline
50 void interleave(IDWTELEM *dst, IDWTELEM *src0, IDWTELEM *src1, int w2, int add, int shift)
53 for (i = 0; i < w2; i++) {
54 dst[2*i ] = (src0[i] + add) >> shift;
55 dst[2*i+1] = (src1[i] + add) >> shift;
59 static void horizontal_compose_dirac53i(IDWTELEM *b, IDWTELEM *temp, int w)
61 const int w2 = w >> 1;
64 temp[0] = COMPOSE_53iL0(b[w2], b[0], b[w2]);
65 for (x = 1; x < w2; x++) {
66 temp[x ] = COMPOSE_53iL0 (b[x+w2-1], b[x ], b[x+w2]);
67 temp[x+w2-1] = COMPOSE_DIRAC53iH0(temp[x-1], b[x+w2-1], temp[x]);
69 temp[w-1] = COMPOSE_DIRAC53iH0(temp[w2-1], b[w-1], temp[w2-1]);
71 interleave(b, temp, temp+w2, w2, 1, 1);
74 static void horizontal_compose_dd97i(IDWTELEM *b, IDWTELEM *tmp, int w)
76 const int w2 = w >> 1;
79 tmp[0] = COMPOSE_53iL0(b[w2], b[0], b[w2]);
80 for (x = 1; x < w2; x++)
81 tmp[x] = COMPOSE_53iL0(b[x+w2-1], b[x], b[x+w2]);
85 tmp[w2+1] = tmp[w2] = tmp[w2-1];
87 for (x = 0; x < w2; x++) {
88 b[2*x ] = (tmp[x] + 1)>>1;
89 b[2*x+1] = (COMPOSE_DD97iH0(tmp[x-1], tmp[x], b[x+w2], tmp[x+1], tmp[x+2]) + 1)>>1;
93 static void horizontal_compose_dd137i(IDWTELEM *b, IDWTELEM *tmp, int w)
95 const int w2 = w >> 1;
98 tmp[0] = COMPOSE_DD137iL0(b[w2], b[w2], b[0], b[w2 ], b[w2+1]);
99 tmp[1] = COMPOSE_DD137iL0(b[w2], b[w2], b[1], b[w2+1], b[w2+2]);
100 for (x = 2; x < w2-1; x++)
101 tmp[x] = COMPOSE_DD137iL0(b[x+w2-2], b[x+w2-1], b[x], b[x+w2], b[x+w2+1]);
102 tmp[w2-1] = COMPOSE_DD137iL0(b[w-3], b[w-2], b[w2-1], b[w-1], b[w-1]);
106 tmp[w2+1] = tmp[w2] = tmp[w2-1];
108 for (x = 0; x < w2; x++) {
109 b[2*x ] = (tmp[x] + 1)>>1;
110 b[2*x+1] = (COMPOSE_DD97iH0(tmp[x-1], tmp[x], b[x+w2], tmp[x+1], tmp[x+2]) + 1)>>1;
114 static av_always_inline
115 void horizontal_compose_haari(IDWTELEM *b, IDWTELEM *temp, int w, int shift)
117 const int w2 = w >> 1;
120 for (x = 0; x < w2; x++) {
121 temp[x ] = COMPOSE_HAARiL0(b[x ], b[x+w2]);
122 temp[x+w2] = COMPOSE_HAARiH0(b[x+w2], temp[x]);
125 interleave(b, temp, temp+w2, w2, shift, shift);
128 static void horizontal_compose_haar0i(IDWTELEM *b, IDWTELEM *temp, int w)
130 horizontal_compose_haari(b, temp, w, 0);
133 static void horizontal_compose_haar1i(IDWTELEM *b, IDWTELEM *temp, int w)
135 horizontal_compose_haari(b, temp, w, 1);
138 static void horizontal_compose_fidelityi(IDWTELEM *b, IDWTELEM *tmp, int w)
140 const int w2 = w >> 1;
144 for (x = 0; x < w2; x++) {
145 for (i = 0; i < 8; i++)
146 v[i] = b[av_clip(x-3+i, 0, w2-1)];
147 tmp[x] = COMPOSE_FIDELITYiH0(v[0], v[1], v[2], v[3], b[x+w2], v[4], v[5], v[6], v[7]);
150 for (x = 0; x < w2; x++) {
151 for (i = 0; i < 8; i++)
152 v[i] = tmp[av_clip(x-4+i, 0, w2-1)];
153 tmp[x+w2] = COMPOSE_FIDELITYiL0(v[0], v[1], v[2], v[3], b[x], v[4], v[5], v[6], v[7]);
156 interleave(b, tmp+w2, tmp, w2, 0, 0);
159 static void horizontal_compose_daub97i(IDWTELEM *b, IDWTELEM *temp, int w)
161 const int w2 = w >> 1;
164 temp[0] = COMPOSE_DAUB97iL1(b[w2], b[0], b[w2]);
165 for (x = 1; x < w2; x++) {
166 temp[x ] = COMPOSE_DAUB97iL1(b[x+w2-1], b[x ], b[x+w2]);
167 temp[x+w2-1] = COMPOSE_DAUB97iH1(temp[x-1], b[x+w2-1], temp[x]);
169 temp[w-1] = COMPOSE_DAUB97iH1(temp[w2-1], b[w-1], temp[w2-1]);
171 // second stage combined with interleave and shift
172 b0 = b2 = COMPOSE_DAUB97iL0(temp[w2], temp[0], temp[w2]);
173 b[0] = (b0 + 1) >> 1;
174 for (x = 1; x < w2; x++) {
175 b2 = COMPOSE_DAUB97iL0(temp[x+w2-1], temp[x ], temp[x+w2]);
176 b1 = COMPOSE_DAUB97iH0( b0, temp[x+w2-1], b2 );
177 b[2*x-1] = (b1 + 1) >> 1;
178 b[2*x ] = (b2 + 1) >> 1;
181 b[w-1] = (COMPOSE_DAUB97iH0(b2, temp[w-1], b2) + 1) >> 1;
184 static void vertical_compose_dirac53iH0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width)
188 for(i=0; i<width; i++){
189 b1[i] = COMPOSE_DIRAC53iH0(b0[i], b1[i], b2[i]);
193 static void vertical_compose_dd97iH0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2,
194 IDWTELEM *b3, IDWTELEM *b4, int width)
198 for(i=0; i<width; i++){
199 b2[i] = COMPOSE_DD97iH0(b0[i], b1[i], b2[i], b3[i], b4[i]);
203 static void vertical_compose_dd137iL0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2,
204 IDWTELEM *b3, IDWTELEM *b4, int width)
208 for(i=0; i<width; i++){
209 b2[i] = COMPOSE_DD137iL0(b0[i], b1[i], b2[i], b3[i], b4[i]);
213 static void vertical_compose_haar(IDWTELEM *b0, IDWTELEM *b1, int width)
217 for (i = 0; i < width; i++) {
218 b0[i] = COMPOSE_HAARiL0(b0[i], b1[i]);
219 b1[i] = COMPOSE_HAARiH0(b1[i], b0[i]);
223 static void vertical_compose_fidelityiH0(IDWTELEM *dst, IDWTELEM *b[8], int width)
227 for(i=0; i<width; i++){
228 dst[i] = COMPOSE_FIDELITYiH0(b[0][i], b[1][i], b[2][i], b[3][i], dst[i], b[4][i], b[5][i], b[6][i], b[7][i]);
232 static void vertical_compose_fidelityiL0(IDWTELEM *dst, IDWTELEM *b[8], int width)
236 for(i=0; i<width; i++){
237 dst[i] = COMPOSE_FIDELITYiL0(b[0][i], b[1][i], b[2][i], b[3][i], dst[i], b[4][i], b[5][i], b[6][i], b[7][i]);
241 static void vertical_compose_daub97iH0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width)
245 for(i=0; i<width; i++){
246 b1[i] = COMPOSE_DAUB97iH0(b0[i], b1[i], b2[i]);
250 static void vertical_compose_daub97iH1(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width)
254 for(i=0; i<width; i++){
255 b1[i] = COMPOSE_DAUB97iH1(b0[i], b1[i], b2[i]);
259 static void vertical_compose_daub97iL0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width)
263 for(i=0; i<width; i++){
264 b1[i] = COMPOSE_DAUB97iL0(b0[i], b1[i], b2[i]);
268 static void vertical_compose_daub97iL1(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width)
272 for(i=0; i<width; i++){
273 b1[i] = COMPOSE_DAUB97iL1(b0[i], b1[i], b2[i]);
278 static void spatial_compose_dd97i_dy(DWTContext *d, int level, int width, int height, int stride)
280 vertical_compose_3tap vertical_compose_l0 = (void*)d->vertical_compose_l0;
281 vertical_compose_5tap vertical_compose_h0 = (void*)d->vertical_compose_h0;
282 DWTCompose *cs = d->cs + level;
286 for (i = 0; i < 6; i++)
288 b[6] = d->buffer + av_clip(y+5, 0, height-2)*stride;
289 b[7] = d->buffer + av_clip(y+6, 1, height-1)*stride;
291 if(y+5<(unsigned)height) vertical_compose_l0( b[5], b[6], b[7], width);
292 if(y+1<(unsigned)height) vertical_compose_h0(b[0], b[2], b[3], b[4], b[6], width);
294 if(y-1<(unsigned)height) d->horizontal_compose(b[0], d->temp, width);
295 if(y+0<(unsigned)height) d->horizontal_compose(b[1], d->temp, width);
297 for (i = 0; i < 6; i++)
302 static void spatial_compose_dirac53i_dy(DWTContext *d, int level, int width, int height, int stride)
304 vertical_compose_3tap vertical_compose_l0 = (void*)d->vertical_compose_l0;
305 vertical_compose_3tap vertical_compose_h0 = (void*)d->vertical_compose_h0;
306 DWTCompose *cs = d->cs + level;
309 IDWTELEM *b[4] = { cs->b[0], cs->b[1] };
310 b[2] = d->buffer + mirror(y+1, height-1)*stride;
311 b[3] = d->buffer + mirror(y+2, height-1)*stride;
313 if(y+1<(unsigned)height) vertical_compose_l0(b[1], b[2], b[3], width);
314 if(y+0<(unsigned)height) vertical_compose_h0(b[0], b[1], b[2], width);
316 if(y-1<(unsigned)height) d->horizontal_compose(b[0], d->temp, width);
317 if(y+0<(unsigned)height) d->horizontal_compose(b[1], d->temp, width);
325 static void spatial_compose_dd137i_dy(DWTContext *d, int level, int width, int height, int stride)
327 vertical_compose_5tap vertical_compose_l0 = (void*)d->vertical_compose_l0;
328 vertical_compose_5tap vertical_compose_h0 = (void*)d->vertical_compose_h0;
329 DWTCompose *cs = d->cs + level;
333 for (i = 0; i < 8; i++)
335 b[8] = d->buffer + av_clip(y+7, 0, height-2)*stride;
336 b[9] = d->buffer + av_clip(y+8, 1, height-1)*stride;
338 if(y+5<(unsigned)height) vertical_compose_l0(b[3], b[5], b[6], b[7], b[9], width);
339 if(y+1<(unsigned)height) vertical_compose_h0(b[0], b[2], b[3], b[4], b[6], width);
341 if(y-1<(unsigned)height) d->horizontal_compose(b[0], d->temp, width);
342 if(y+0<(unsigned)height) d->horizontal_compose(b[1], d->temp, width);
344 for (i = 0; i < 8; i++)
349 // haar makes the assumption that height is even (always true for dirac)
350 static void spatial_compose_haari_dy(DWTContext *d, int level, int width, int height, int stride)
352 vertical_compose_2tap vertical_compose = (void*)d->vertical_compose;
353 int y = d->cs[level].y;
354 IDWTELEM *b0 = d->buffer + (y-1)*stride;
355 IDWTELEM *b1 = d->buffer + (y )*stride;
357 vertical_compose(b0, b1, width);
358 d->horizontal_compose(b0, d->temp, width);
359 d->horizontal_compose(b1, d->temp, width);
364 // Don't do sliced idwt for fidelity; the 9 tap filter makes it a bit annoying
365 // Fortunately, this filter isn't used in practice.
366 static void spatial_compose_fidelity(DWTContext *d, int level, int width, int height, int stride)
368 vertical_compose_9tap vertical_compose_l0 = (void*)d->vertical_compose_l0;
369 vertical_compose_9tap vertical_compose_h0 = (void*)d->vertical_compose_h0;
373 for (y = 1; y < height; y += 2) {
374 for (i = 0; i < 8; i++)
375 b[i] = d->buffer + av_clip((y-7 + 2*i), 0, height-2)*stride;
376 vertical_compose_h0(d->buffer + y*stride, b, width);
379 for (y = 0; y < height; y += 2) {
380 for (i = 0; i < 8; i++)
381 b[i] = d->buffer + av_clip((y-7 + 2*i), 1, height-1)*stride;
382 vertical_compose_l0(d->buffer + y*stride, b, width);
385 for (y = 0; y < height; y++)
386 d->horizontal_compose(d->buffer + y*stride, d->temp, width);
388 d->cs[level].y = height+1;
391 static void spatial_compose_daub97i_dy(DWTContext *d, int level, int width, int height, int stride)
393 vertical_compose_3tap vertical_compose_l0 = (void*)d->vertical_compose_l0;
394 vertical_compose_3tap vertical_compose_h0 = (void*)d->vertical_compose_h0;
395 vertical_compose_3tap vertical_compose_l1 = (void*)d->vertical_compose_l1;
396 vertical_compose_3tap vertical_compose_h1 = (void*)d->vertical_compose_h1;
397 DWTCompose *cs = d->cs + level;
401 for (i = 0; i < 4; i++)
403 b[4] = d->buffer + mirror(y+3, height-1)*stride;
404 b[5] = d->buffer + mirror(y+4, height-1)*stride;
406 if(y+3<(unsigned)height) vertical_compose_l1(b[3], b[4], b[5], width);
407 if(y+2<(unsigned)height) vertical_compose_h1(b[2], b[3], b[4], width);
408 if(y+1<(unsigned)height) vertical_compose_l0(b[1], b[2], b[3], width);
409 if(y+0<(unsigned)height) vertical_compose_h0(b[0], b[1], b[2], width);
411 if(y-1<(unsigned)height) d->horizontal_compose(b[0], d->temp, width);
412 if(y+0<(unsigned)height) d->horizontal_compose(b[1], d->temp, width);
414 for (i = 0; i < 4; i++)
420 static void spatial_compose97i_init2(DWTCompose *cs, IDWTELEM *buffer, int height, int stride)
422 cs->b[0] = buffer + mirror(-3-1, height-1)*stride;
423 cs->b[1] = buffer + mirror(-3 , height-1)*stride;
424 cs->b[2] = buffer + mirror(-3+1, height-1)*stride;
425 cs->b[3] = buffer + mirror(-3+2, height-1)*stride;
429 static void spatial_compose53i_init2(DWTCompose *cs, IDWTELEM *buffer, int height, int stride)
431 cs->b[0] = buffer + mirror(-1-1, height-1)*stride;
432 cs->b[1] = buffer + mirror(-1 , height-1)*stride;
436 static void spatial_compose_dd97i_init(DWTCompose *cs, IDWTELEM *buffer, int height, int stride)
438 cs->b[0] = buffer + av_clip(-5-1, 0, height-2)*stride;
439 cs->b[1] = buffer + av_clip(-5 , 1, height-1)*stride;
440 cs->b[2] = buffer + av_clip(-5+1, 0, height-2)*stride;
441 cs->b[3] = buffer + av_clip(-5+2, 1, height-1)*stride;
442 cs->b[4] = buffer + av_clip(-5+3, 0, height-2)*stride;
443 cs->b[5] = buffer + av_clip(-5+4, 1, height-1)*stride;
447 static void spatial_compose_dd137i_init(DWTCompose *cs, IDWTELEM *buffer, int height, int stride)
449 cs->b[0] = buffer + av_clip(-5-1, 0, height-2)*stride;
450 cs->b[1] = buffer + av_clip(-5 , 1, height-1)*stride;
451 cs->b[2] = buffer + av_clip(-5+1, 0, height-2)*stride;
452 cs->b[3] = buffer + av_clip(-5+2, 1, height-1)*stride;
453 cs->b[4] = buffer + av_clip(-5+3, 0, height-2)*stride;
454 cs->b[5] = buffer + av_clip(-5+4, 1, height-1)*stride;
455 cs->b[6] = buffer + av_clip(-5+5, 0, height-2)*stride;
456 cs->b[7] = buffer + av_clip(-5+6, 1, height-1)*stride;
460 int ff_spatial_idwt_init2(DWTContext *d, IDWTELEM *buffer, int width, int height,
461 int stride, enum dwt_type type, int decomposition_count,
470 d->decomposition_count = decomposition_count;
473 for(level=decomposition_count-1; level>=0; level--){
474 int hl = height >> level;
475 int stride_l = stride << level;
478 case DWT_DIRAC_DD9_7:
479 spatial_compose_dd97i_init(d->cs+level, buffer, hl, stride_l);
481 case DWT_DIRAC_LEGALL5_3:
482 spatial_compose53i_init2(d->cs+level, buffer, hl, stride_l);
484 case DWT_DIRAC_DD13_7:
485 spatial_compose_dd137i_init(d->cs+level, buffer, hl, stride_l);
487 case DWT_DIRAC_HAAR0:
488 case DWT_DIRAC_HAAR1:
491 case DWT_DIRAC_DAUB9_7:
492 spatial_compose97i_init2(d->cs+level, buffer, hl, stride_l);
501 case DWT_DIRAC_DD9_7:
502 d->spatial_compose = spatial_compose_dd97i_dy;
503 d->vertical_compose_l0 = (void*)vertical_compose53iL0;
504 d->vertical_compose_h0 = (void*)vertical_compose_dd97iH0;
505 d->horizontal_compose = horizontal_compose_dd97i;
508 case DWT_DIRAC_LEGALL5_3:
509 d->spatial_compose = spatial_compose_dirac53i_dy;
510 d->vertical_compose_l0 = (void*)vertical_compose53iL0;
511 d->vertical_compose_h0 = (void*)vertical_compose_dirac53iH0;
512 d->horizontal_compose = horizontal_compose_dirac53i;
515 case DWT_DIRAC_DD13_7:
516 d->spatial_compose = spatial_compose_dd137i_dy;
517 d->vertical_compose_l0 = (void*)vertical_compose_dd137iL0;
518 d->vertical_compose_h0 = (void*)vertical_compose_dd97iH0;
519 d->horizontal_compose = horizontal_compose_dd137i;
522 case DWT_DIRAC_HAAR0:
523 case DWT_DIRAC_HAAR1:
524 d->spatial_compose = spatial_compose_haari_dy;
525 d->vertical_compose = (void*)vertical_compose_haar;
526 if (type == DWT_DIRAC_HAAR0)
527 d->horizontal_compose = horizontal_compose_haar0i;
529 d->horizontal_compose = horizontal_compose_haar1i;
532 case DWT_DIRAC_FIDELITY:
533 d->spatial_compose = spatial_compose_fidelity;
534 d->vertical_compose_l0 = (void*)vertical_compose_fidelityiL0;
535 d->vertical_compose_h0 = (void*)vertical_compose_fidelityiH0;
536 d->horizontal_compose = horizontal_compose_fidelityi;
537 d->support = 0; // not really used
539 case DWT_DIRAC_DAUB9_7:
540 d->spatial_compose = spatial_compose_daub97i_dy;
541 d->vertical_compose_l0 = (void*)vertical_compose_daub97iL0;
542 d->vertical_compose_h0 = (void*)vertical_compose_daub97iH0;
543 d->vertical_compose_l1 = (void*)vertical_compose_daub97iL1;
544 d->vertical_compose_h1 = (void*)vertical_compose_daub97iH1;
545 d->horizontal_compose = horizontal_compose_daub97i;
549 av_log(NULL, AV_LOG_ERROR, "Unknown wavelet type %d\n", type);
553 if (HAVE_MMX) ff_spatial_idwt_init_mmx(d, type);
558 void ff_spatial_idwt_slice2(DWTContext *d, int y)
560 int level, support = d->support;
562 for (level = d->decomposition_count-1; level >= 0; level--) {
563 int wl = d->width >> level;
564 int hl = d->height >> level;
565 int stride_l = d->stride << level;
567 while (d->cs[level].y <= FFMIN((y>>level)+support, hl))
568 d->spatial_compose(d, level, wl, hl, stride_l);