2 * Copyright (C) 2004-2010 Michael Niedermayer <michaelni@gmx.at>
4 * This file is part of Libav.
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.
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.
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
21 #include "libavutil/attributes.h"
25 int ff_slice_buffer_init(slice_buffer *buf, int line_count,
26 int max_allocated_lines, int line_width,
27 IDWTELEM *base_buffer)
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);
37 return AVERROR(ENOMEM);
38 buf->data_stack = av_malloc(sizeof(IDWTELEM *) * max_allocated_lines);
39 if (!buf->data_stack) {
41 return AVERROR(ENOMEM);
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]) {
48 av_free(buf->data_stack[i]);
49 av_free(buf->data_stack);
51 return AVERROR(ENOMEM);
55 buf->data_stack_top = max_allocated_lines - 1;
59 IDWTELEM *ff_slice_buffer_load_line(slice_buffer *buf, int line)
63 assert(buf->data_stack_top >= 0);
64 // assert(!buf->line[line]);
66 return buf->line[line];
68 buffer = buf->data_stack[buf->data_stack_top];
69 buf->data_stack_top--;
70 buf->line[line] = buffer;
75 void ff_slice_buffer_release(slice_buffer *buf, int line)
79 assert(line >= 0 && line < buf->line_count);
80 assert(buf->line[line]);
82 buffer = buf->line[line];
83 buf->data_stack_top++;
84 buf->data_stack[buf->data_stack_top] = buffer;
85 buf->line[line] = NULL;
88 void ff_slice_buffer_flush(slice_buffer *buf)
91 for (i = 0; i < buf->line_count; i++)
93 ff_slice_buffer_release(buf, i);
96 void ff_slice_buffer_destroy(slice_buffer *buf)
99 ff_slice_buffer_flush(buf);
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);
107 static inline int mirror(int v, int m)
109 while ((unsigned)v > (unsigned)m) {
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)
122 const int mirror_left = !highpass;
123 const int mirror_right = (width & 1) ^ highpass;
124 const int w = (width >> 1) - 1 + (highpass & width);
127 #define LIFT(src, ref, inv) ((src) + ((inv) ? -(ref) : +(ref)))
129 dst[0] = LIFT(src[0], ((mul * 2 * ref[0] + add) >> shift), inverse);
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]) +
142 dst[w * dst_step] = LIFT(src[w * src_step],
143 ((mul * 2 * ref[w * ref_step] + add) >> shift),
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)
152 const int mirror_left = !highpass;
153 const int mirror_right = (width & 1) ^ highpass;
154 const int w = (width >> 1) - 1 + (highpass & width);
157 #define LIFT(src, ref, inv) ((src) + ((inv) ? -(ref) : +(ref)))
159 dst[0] = LIFT(src[0], ((mul * 2 * ref[0] + add) >> shift), inverse);
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]) +
172 dst[w * dst_step] = LIFT(src[w * src_step],
173 ((mul * 2 * ref[w * ref_step] + add) >> shift),
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)
184 const int mirror_left = !highpass;
185 const int mirror_right = (width & 1) ^ highpass;
186 const int w = (width >> 1) - 1 + (highpass & width);
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)))
195 dst[0] = LIFTS(src[0], mul * 2 * ref[0] + add, inverse);
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,
207 dst[w * dst_step] = LIFTS(src[w * src_step],
208 mul * 2 * ref[w * ref_step] + add,
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)
218 const int mirror_left = !highpass;
219 const int mirror_right = (width & 1) ^ highpass;
220 const int w = (width >> 1) - 1 + (highpass & width);
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)))
229 dst[0] = LIFTS(src[0], mul * 2 * ref[0] + add, inverse);
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,
241 dst[w * dst_step] = LIFTS(src[w * src_step],
242 mul * 2 * ref[w * ref_step] + add, inverse);
246 static void horizontal_decompose53i(DWTELEM *b, DWTELEM *temp, int width)
248 const int width2 = width >> 1;
250 const int w2 = (width + 1) >> 1;
252 for (x = 0; x < width2; x++) {
254 temp[x + w2] = b[2 * x + 1];
263 A1 = temp[0 + width2];
264 A1 -= (A2 + A4) >> 1;
268 for (x = 1; x + 1 < width2; x += 2) {
269 A3 = temp[x + width2];
271 A3 -= (A2 + A4) >> 1;
272 A2 += (A1 + A3 + 2) >> 2;
276 A1 = temp[x + 1 + width2];
278 A1 -= (A2 + A4) >> 1;
279 A4 += (A1 + A3 + 2) >> 2;
280 b[x + 1 + width2] = A1;
283 A3 = temp[width - 1];
285 A2 += (A1 + A3 + 2) >> 2;
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);
295 static void vertical_decompose53iH0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2,
300 for (i = 0; i < width; i++)
301 b1[i] -= (b0[i] + b2[i]) >> 1;
304 static void vertical_decompose53iL0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2,
309 for (i = 0; i < width; i++)
310 b1[i] += (b0[i] + b2[i] + 2) >> 2;
313 static void spatial_decompose53i(DWTELEM *buffer, DWTELEM *temp,
314 int width, int height, int stride)
317 DWTELEM *b0 = buffer + mirror(-2 - 1, height - 1) * stride;
318 DWTELEM *b1 = buffer + mirror(-2, height - 1) * stride;
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;
324 if (y + 1 < (unsigned)height)
325 horizontal_decompose53i(b2, temp, width);
326 if (y + 2 < (unsigned)height)
327 horizontal_decompose53i(b3, temp, width);
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);
339 static void horizontal_decompose97i(DWTELEM *b, DWTELEM *temp, int width)
341 const int w2 = (width + 1) >> 1;
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);
349 static void vertical_decompose97iH0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2,
354 for (i = 0; i < width; i++)
355 b1[i] -= (W_AM * (b0[i] + b2[i]) + W_AO) >> W_AS;
358 static void vertical_decompose97iH1(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2,
363 for (i = 0; i < width; i++)
364 b1[i] += (W_CM * (b0[i] + b2[i]) + W_CO) >> W_CS;
367 static void vertical_decompose97iL0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2,
372 for (i = 0; i < width; i++)
374 b1[i] -= (W_BM * (b0[i] + b2[i]) + W_BO) >> W_BS;
376 b1[i] = (16 * 4 * b1[i] - 4 * (b0[i] + b2[i]) + W_BO * 5 + (5 << 27)) /
377 (5 * 16) - (1 << 23);
381 static void vertical_decompose97iL1(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2,
386 for (i = 0; i < width; i++)
387 b1[i] += (W_DM * (b0[i] + b2[i]) + W_DO) >> W_DS;
390 static void spatial_decompose97i(DWTELEM *buffer, DWTELEM *temp,
391 int width, int height, int stride)
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;
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;
403 if (y + 3 < (unsigned)height)
404 horizontal_decompose97i(b4, temp, width);
405 if (y + 4 < (unsigned)height)
406 horizontal_decompose97i(b5, temp, width);
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);
424 void ff_spatial_dwt(DWTELEM *buffer, DWTELEM *temp, int width, int height,
425 int stride, int type, int decomposition_count)
429 for (level = 0; level < decomposition_count; level++) {
432 spatial_decompose97i(buffer, temp,
433 width >> level, height >> level,
437 spatial_decompose53i(buffer, temp,
438 width >> level, height >> level,
445 static void horizontal_compose53i(IDWTELEM *b, IDWTELEM *temp, int width)
447 const int width2 = width >> 1;
448 const int w2 = (width + 1) >> 1;
451 for (x = 0; x < width2; x++) {
453 temp[2 * x + 1] = b[x + w2];
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);
464 b[x] = temp[x] - ((temp[x - 1] + 1) >> 1);
465 b[x - 1] = temp[x - 1] + ((b[x - 2] + b[x] + 1) >> 1);
467 b[x - 1] = temp[x - 1] + b[x - 2];
470 static void vertical_compose53iH0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2,
475 for (i = 0; i < width; i++)
476 b1[i] += (b0[i] + b2[i]) >> 1;
479 static void vertical_compose53iL0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2,
484 for (i = 0; i < width; i++)
485 b1[i] -= (b0[i] + b2[i] + 2) >> 2;
488 static void spatial_compose53i_buffered_init(DWTCompose *cs, slice_buffer *sb,
489 int height, int stride_line)
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);
497 static void spatial_compose53i_init(DWTCompose *cs, IDWTELEM *buffer,
498 int height, int stride)
500 cs->b0 = buffer + mirror(-1 - 1, height - 1) * stride;
501 cs->b1 = buffer + mirror(-1, height - 1) * stride;
505 static void spatial_compose53i_dy_buffered(DWTCompose *cs, slice_buffer *sb,
507 int width, int height,
512 IDWTELEM *b0 = cs->b0;
513 IDWTELEM *b1 = cs->b1;
514 IDWTELEM *b2 = slice_buffer_get_line(sb,
515 mirror(y + 1, height - 1) *
517 IDWTELEM *b3 = slice_buffer_get_line(sb,
518 mirror(y + 2, height - 1) *
521 if (y + 1 < (unsigned)height && y < (unsigned)height) {
524 for (x = 0; x < width; x++) {
525 b2[x] -= (b1[x] + b3[x] + 2) >> 2;
526 b1[x] += (b0[x] + b2[x]) >> 1;
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);
535 if (y - 1 < (unsigned)height)
536 horizontal_compose53i(b0, temp, width);
537 if (y + 0 < (unsigned)height)
538 horizontal_compose53i(b1, temp, width);
545 static void spatial_compose53i_dy(DWTCompose *cs, IDWTELEM *buffer,
546 IDWTELEM *temp, int width, int height,
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;
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);
560 if (y - 1 < (unsigned)height)
561 horizontal_compose53i(b0, temp, width);
562 if (y + 0 < (unsigned)height)
563 horizontal_compose53i(b1, temp, width);
570 static void av_unused spatial_compose53i(IDWTELEM *buffer, IDWTELEM *temp,
571 int width, int height, int stride)
574 spatial_compose53i_init(&cs, buffer, height, stride);
575 while (cs.y <= height)
576 spatial_compose53i_dy(&cs, buffer, temp, width, height, stride);
579 void ff_snow_horizontal_compose97i(IDWTELEM *b, IDWTELEM *temp, int width)
581 const int w2 = (width + 1) >> 1;
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);
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);
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];
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];
600 temp[2 * x - 1] = b[x + w2 - 1] - 2 * temp[2 * x - 2];
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);
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);
611 b[x - 1] = temp[x - 1] + 3 * b[x - 2];
615 static void vertical_compose97iH0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2,
620 for (i = 0; i < width; i++)
621 b1[i] += (W_AM * (b0[i] + b2[i]) + W_AO) >> W_AS;
624 static void vertical_compose97iH1(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2,
629 for (i = 0; i < width; i++)
630 b1[i] -= (W_CM * (b0[i] + b2[i]) + W_CO) >> W_CS;
633 static void vertical_compose97iL0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2,
638 for (i = 0; i < width; i++)
640 b1[i] += (W_BM * (b0[i] + b2[i]) + W_BO) >> W_BS;
642 b1[i] += (W_BM * (b0[i] + b2[i]) + 4 * b1[i] + W_BO) >> W_BS;
646 static void vertical_compose97iL1(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2,
651 for (i = 0; i < width; i++)
652 b1[i] -= (W_DM * (b0[i] + b2[i]) + W_DO) >> W_DS;
655 void ff_snow_vertical_compose97i(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2,
656 IDWTELEM *b3, IDWTELEM *b4, IDWTELEM *b5,
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;
665 b2[i] += (W_BM * (b1[i] + b3[i]) + W_BO) >> W_BS;
667 b2[i] += (W_BM * (b1[i] + b3[i]) + 4 * b2[i] + W_BO) >> W_BS;
669 b1[i] += (W_AM * (b0[i] + b2[i]) + W_AO) >> W_AS;
673 static void spatial_compose97i_buffered_init(DWTCompose *cs, slice_buffer *sb,
674 int height, int stride_line)
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);
683 static void spatial_compose97i_init(DWTCompose *cs, IDWTELEM *buffer, int height,
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;
693 static void spatial_compose97i_dy_buffered(DWTContext *dsp, DWTCompose *cs,
694 slice_buffer * sb, IDWTELEM *temp,
695 int width, int height,
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) *
707 IDWTELEM *b5 = slice_buffer_get_line(sb,
708 mirror(y + 4, height - 1) *
711 if (y > 0 && y + 4 < height) {
712 dsp->vertical_compose97i(b0, b1, b2, b3, b4, b5, width);
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);
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);
736 static void spatial_compose97i_dy(DWTCompose *cs, IDWTELEM *buffer,
737 IDWTELEM *temp, int width, int height,
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;
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);
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);
769 static void av_unused spatial_compose97i(IDWTELEM *buffer, IDWTELEM *temp,
770 int width, int height, int stride)
773 spatial_compose97i_init(&cs, buffer, height, stride);
774 while (cs.y <= height)
775 spatial_compose97i_dy(&cs, buffer, temp, width, height, stride);
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)
783 for (level = decomposition_count - 1; level >= 0; level--) {
786 spatial_compose97i_buffered_init(cs + level, sb, height >> level,
787 stride_line << level);
790 spatial_compose53i_buffered_init(cs + level, sb, height >> level,
791 stride_line << level);
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)
802 const int support = type == 1 ? 3 : 5;
807 for (level = decomposition_count - 1; level >= 0; level--)
808 while (cs[level].y <= FFMIN((y >> level) + support, height >> level)) {
811 spatial_compose97i_dy_buffered(dsp, cs + level, slice_buf, temp,
814 stride_line << level);
817 spatial_compose53i_dy_buffered(cs + level, slice_buf, temp,
820 stride_line << level);
826 static void ff_spatial_idwt_init(DWTCompose *cs, IDWTELEM *buffer, int width,
827 int height, int stride, int type,
828 int decomposition_count)
831 for (level = decomposition_count - 1; level >= 0; level--) {
834 spatial_compose97i_init(cs + level, buffer, height >> level,
838 spatial_compose53i_init(cs + level, buffer, height >> level,
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)
850 const int support = type == 1 ? 3 : 5;
855 for (level = decomposition_count - 1; level >= 0; level--)
856 while (cs[level].y <= FFMIN((y >> level) + support, height >> level)) {
859 spatial_compose97i_dy(cs + level, buffer, temp, width >> level,
860 height >> level, stride << level);
863 spatial_compose53i_dy(cs + level, buffer, temp, width >> level,
864 height >> level, stride << level);
870 void ff_spatial_idwt(IDWTELEM *buffer, IDWTELEM *temp, int width, int height,
871 int stride, int type, int decomposition_count)
873 DWTCompose cs[MAX_DECOMPOSITIONS];
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);
882 static inline int w_c(void *v, uint8_t *pix1, uint8_t *pix2, int line_size,
883 int w, int h, int type)
886 const int dec_count = w == 8 ? 3 : 4;
887 int tmp[32 * 32], tmp2[32];
889 static const int scale[2][2][4][4] = {
892 { 268, 239, 239, 213 },
893 { 0, 224, 224, 152 },
894 { 0, 135, 135, 110 },
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 },
905 { 275, 245, 245, 218 },
906 { 0, 230, 230, 156 },
907 { 0, 138, 138, 113 },
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 },
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;
929 ff_spatial_dwt(tmp, tmp2, w, h, 32, type, dec_count);
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;
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];
951 static int w53_8_c(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h)
953 return w_c(v, pix1, pix2, line_size, 8, h, 1);
956 static int w97_8_c(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h)
958 return w_c(v, pix1, pix2, line_size, 8, h, 0);
961 static int w53_16_c(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h)
963 return w_c(v, pix1, pix2, line_size, 16, h, 1);
966 static int w97_16_c(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h)
968 return w_c(v, pix1, pix2, line_size, 16, h, 0);
971 int ff_w53_32_c(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h)
973 return w_c(v, pix1, pix2, line_size, 32, h, 1);
976 int ff_w97_32_c(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h)
978 return w_c(v, pix1, pix2, line_size, 32, h, 0);
981 void ff_dsputil_init_dwt(DSPContext *c)
983 c->w53[0] = w53_16_c;
985 c->w97[0] = w97_16_c;
989 void ff_dwt_init(DWTContext *c)
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;