FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
snow_dwt.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2004-2010 Michael Niedermayer <michaelni@gmx.at>
3  * Copyright (C) 2008 David Conrad
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21 
22 #include "libavutil/attributes.h"
23 #include "libavutil/avassert.h"
24 #include "libavutil/common.h"
25 #include "me_cmp.h"
26 #include "snow_dwt.h"
27 
28 int ff_slice_buffer_init(slice_buffer *buf, int line_count,
29  int max_allocated_lines, int line_width,
30  IDWTELEM *base_buffer)
31 {
32  int i;
33 
34  buf->base_buffer = base_buffer;
35  buf->line_count = line_count;
36  buf->line_width = line_width;
37  buf->data_count = max_allocated_lines;
38  buf->line = av_mallocz_array(line_count, sizeof(IDWTELEM *));
39  if (!buf->line)
40  return AVERROR(ENOMEM);
41  buf->data_stack = av_malloc_array(max_allocated_lines, sizeof(IDWTELEM *));
42  if (!buf->data_stack) {
43  av_freep(&buf->line);
44  return AVERROR(ENOMEM);
45  }
46 
47  for (i = 0; i < max_allocated_lines; i++) {
48  buf->data_stack[i] = av_malloc_array(line_width, sizeof(IDWTELEM));
49  if (!buf->data_stack[i]) {
50  for (i--; i >=0; i--)
51  av_freep(&buf->data_stack[i]);
52  av_freep(&buf->data_stack);
53  av_freep(&buf->line);
54  return AVERROR(ENOMEM);
55  }
56  }
57 
58  buf->data_stack_top = max_allocated_lines - 1;
59  return 0;
60 }
61 
63 {
65 
66  av_assert0(buf->data_stack_top >= 0);
67 // av_assert1(!buf->line[line]);
68  if (buf->line[line])
69  return buf->line[line];
70 
71  buffer = buf->data_stack[buf->data_stack_top];
72  buf->data_stack_top--;
73  buf->line[line] = buffer;
74 
75  return buffer;
76 }
77 
79 {
81 
82  av_assert1(line >= 0 && line < buf->line_count);
83  av_assert1(buf->line[line]);
84 
85  buffer = buf->line[line];
86  buf->data_stack_top++;
87  buf->data_stack[buf->data_stack_top] = buffer;
88  buf->line[line] = NULL;
89 }
90 
92 {
93  int i;
94 
95  if (!buf->line)
96  return;
97 
98  for (i = 0; i < buf->line_count; i++)
99  if (buf->line[i])
100  ff_slice_buffer_release(buf, i);
101 }
102 
104 {
105  int i;
107 
108  if (buf->data_stack)
109  for (i = buf->data_count - 1; i >= 0; i--)
110  av_freep(&buf->data_stack[i]);
111  av_freep(&buf->data_stack);
112  av_freep(&buf->line);
113 }
114 
115 static inline int mirror(int v, int m)
116 {
117  while ((unsigned)v > (unsigned)m) {
118  v = -v;
119  if (v < 0)
120  v += 2 * m;
121  }
122  return v;
123 }
124 
125 static av_always_inline void lift(DWTELEM *dst, DWTELEM *src, DWTELEM *ref,
126  int dst_step, int src_step, int ref_step,
127  int width, int mul, int add, int shift,
128  int highpass, int inverse)
129 {
130  const int mirror_left = !highpass;
131  const int mirror_right = (width & 1) ^ highpass;
132  const int w = (width >> 1) - 1 + (highpass & width);
133  int i;
134 
135 #define LIFT(src, ref, inv) ((src) + ((inv) ? -(ref) : +(ref)))
136  if (mirror_left) {
137  dst[0] = LIFT(src[0], ((mul * 2 * ref[0] + add) >> shift), inverse);
138  dst += dst_step;
139  src += src_step;
140  }
141 
142  for (i = 0; i < w; i++)
143  dst[i * dst_step] = LIFT(src[i * src_step],
144  ((mul * (ref[i * ref_step] +
145  ref[(i + 1) * ref_step]) +
146  add) >> shift),
147  inverse);
148 
149  if (mirror_right)
150  dst[w * dst_step] = LIFT(src[w * src_step],
151  ((mul * 2 * ref[w * ref_step] + add) >> shift),
152  inverse);
153 }
154 
155 static av_always_inline void liftS(DWTELEM *dst, DWTELEM *src, DWTELEM *ref,
156  int dst_step, int src_step, int ref_step,
157  int width, int mul, int add, int shift,
158  int highpass, int inverse)
159 {
160  const int mirror_left = !highpass;
161  const int mirror_right = (width & 1) ^ highpass;
162  const int w = (width >> 1) - 1 + (highpass & width);
163  int i;
164 
165  av_assert1(shift == 4);
166 #define LIFTS(src, ref, inv) \
167  ((inv) ? (src) + (((ref) + 4 * (src)) >> shift) \
168  : -((-16 * (src) + (ref) + add / \
169  4 + 1 + (5 << 25)) / (5 * 4) - (1 << 23)))
170  if (mirror_left) {
171  dst[0] = LIFTS(src[0], mul * 2 * ref[0] + add, inverse);
172  dst += dst_step;
173  src += src_step;
174  }
175 
176  for (i = 0; i < w; i++)
177  dst[i * dst_step] = LIFTS(src[i * src_step],
178  mul * (ref[i * ref_step] +
179  ref[(i + 1) * ref_step]) + add,
180  inverse);
181 
182  if (mirror_right)
183  dst[w * dst_step] = LIFTS(src[w * src_step],
184  mul * 2 * ref[w * ref_step] + add,
185  inverse);
186 }
187 
189 {
190  const int width2 = width >> 1;
191  int x;
192  const int w2 = (width + 1) >> 1;
193 
194  for (x = 0; x < width2; x++) {
195  temp[x] = b[2 * x];
196  temp[x + w2] = b[2 * x + 1];
197  }
198  if (width & 1)
199  temp[x] = b[2 * x];
200  lift(b + w2, temp + w2, temp, 1, 1, 1, width, -1, 0, 1, 1, 0);
201  lift(b, temp, b + w2, 1, 1, 1, width, 1, 2, 2, 0, 0);
202 }
203 
204 static void vertical_decompose53iH0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2,
205  int width)
206 {
207  int i;
208 
209  for (i = 0; i < width; i++)
210  b1[i] -= (b0[i] + b2[i]) >> 1;
211 }
212 
213 static void vertical_decompose53iL0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2,
214  int width)
215 {
216  int i;
217 
218  for (i = 0; i < width; i++)
219  b1[i] += (b0[i] + b2[i] + 2) >> 2;
220 }
221 
223  int width, int height, int stride)
224 {
225  int y;
226  DWTELEM *b0 = buffer + mirror(-2 - 1, height - 1) * stride;
227  DWTELEM *b1 = buffer + mirror(-2, height - 1) * stride;
228 
229  for (y = -2; y < height; y += 2) {
230  DWTELEM *b2 = buffer + mirror(y + 1, height - 1) * stride;
231  DWTELEM *b3 = buffer + mirror(y + 2, height - 1) * stride;
232 
233  if (y + 1 < (unsigned)height)
234  horizontal_decompose53i(b2, temp, width);
235  if (y + 2 < (unsigned)height)
236  horizontal_decompose53i(b3, temp, width);
237 
238  if (y + 1 < (unsigned)height)
239  vertical_decompose53iH0(b1, b2, b3, width);
240  if (y + 0 < (unsigned)height)
241  vertical_decompose53iL0(b0, b1, b2, width);
242 
243  b0 = b2;
244  b1 = b3;
245  }
246 }
247 
249 {
250  const int w2 = (width + 1) >> 1;
251 
252  lift(temp + w2, b + 1, b, 1, 2, 2, width, W_AM, W_AO, W_AS, 1, 1);
253  liftS(temp, b, temp + w2, 1, 2, 1, width, W_BM, W_BO, W_BS, 0, 0);
254  lift(b + w2, temp + w2, temp, 1, 1, 1, width, W_CM, W_CO, W_CS, 1, 0);
255  lift(b, temp, b + w2, 1, 1, 1, width, W_DM, W_DO, W_DS, 0, 0);
256 }
257 
258 static void vertical_decompose97iH0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2,
259  int width)
260 {
261  int i;
262 
263  for (i = 0; i < width; i++)
264  b1[i] -= (W_AM * (b0[i] + b2[i]) + W_AO) >> W_AS;
265 }
266 
267 static void vertical_decompose97iH1(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2,
268  int width)
269 {
270  int i;
271 
272  for (i = 0; i < width; i++)
273  b1[i] += (W_CM * (b0[i] + b2[i]) + W_CO) >> W_CS;
274 }
275 
276 static void vertical_decompose97iL0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2,
277  int width)
278 {
279  int i;
280 
281  for (i = 0; i < width; i++)
282  b1[i] = (16 * 4 * b1[i] - 4 * (b0[i] + b2[i]) + W_BO * 5 + (5 << 27)) /
283  (5 * 16) - (1 << 23);
284 }
285 
286 static void vertical_decompose97iL1(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2,
287  int width)
288 {
289  int i;
290 
291  for (i = 0; i < width; i++)
292  b1[i] += (W_DM * (b0[i] + b2[i]) + W_DO) >> W_DS;
293 }
294 
296  int width, int height, int stride)
297 {
298  int y;
299  DWTELEM *b0 = buffer + mirror(-4 - 1, height - 1) * stride;
300  DWTELEM *b1 = buffer + mirror(-4, height - 1) * stride;
301  DWTELEM *b2 = buffer + mirror(-4 + 1, height - 1) * stride;
302  DWTELEM *b3 = buffer + mirror(-4 + 2, height - 1) * stride;
303 
304  for (y = -4; y < height; y += 2) {
305  DWTELEM *b4 = buffer + mirror(y + 3, height - 1) * stride;
306  DWTELEM *b5 = buffer + mirror(y + 4, height - 1) * stride;
307 
308  if (y + 3 < (unsigned)height)
309  horizontal_decompose97i(b4, temp, width);
310  if (y + 4 < (unsigned)height)
311  horizontal_decompose97i(b5, temp, width);
312 
313  if (y + 3 < (unsigned)height)
314  vertical_decompose97iH0(b3, b4, b5, width);
315  if (y + 2 < (unsigned)height)
316  vertical_decompose97iL0(b2, b3, b4, width);
317  if (y + 1 < (unsigned)height)
318  vertical_decompose97iH1(b1, b2, b3, width);
319  if (y + 0 < (unsigned)height)
320  vertical_decompose97iL1(b0, b1, b2, width);
321 
322  b0 = b2;
323  b1 = b3;
324  b2 = b4;
325  b3 = b5;
326  }
327 }
328 
330  int stride, int type, int decomposition_count)
331 {
332  int level;
333 
334  for (level = 0; level < decomposition_count; level++) {
335  switch (type) {
336  case DWT_97:
337  spatial_decompose97i(buffer, temp,
338  width >> level, height >> level,
339  stride << level);
340  break;
341  case DWT_53:
342  spatial_decompose53i(buffer, temp,
343  width >> level, height >> level,
344  stride << level);
345  break;
346  }
347  }
348 }
349 
351 {
352  const int width2 = width >> 1;
353  const int w2 = (width + 1) >> 1;
354  int x;
355 
356  for (x = 0; x < width2; x++) {
357  temp[2 * x] = b[x];
358  temp[2 * x + 1] = b[x + w2];
359  }
360  if (width & 1)
361  temp[2 * x] = b[x];
362 
363  b[0] = temp[0] - ((temp[1] + 1) >> 1);
364  for (x = 2; x < width - 1; x += 2) {
365  b[x] = temp[x] - ((temp[x - 1] + temp[x + 1] + 2) >> 2);
366  b[x - 1] = temp[x - 1] + ((b[x - 2] + b[x] + 1) >> 1);
367  }
368  if (width & 1) {
369  b[x] = temp[x] - ((temp[x - 1] + 1) >> 1);
370  b[x - 1] = temp[x - 1] + ((b[x - 2] + b[x] + 1) >> 1);
371  } else
372  b[x - 1] = temp[x - 1] + b[x - 2];
373 }
374 
376  int width)
377 {
378  int i;
379 
380  for (i = 0; i < width; i++)
381  b1[i] += (b0[i] + b2[i]) >> 1;
382 }
383 
385  int width)
386 {
387  int i;
388 
389  for (i = 0; i < width; i++)
390  b1[i] -= (b0[i] + b2[i] + 2) >> 2;
391 }
392 
394  int height, int stride_line)
395 {
396  cs->b0 = slice_buffer_get_line(sb,
397  mirror(-1 - 1, height - 1) * stride_line);
398  cs->b1 = slice_buffer_get_line(sb, mirror(-1, height - 1) * stride_line);
399  cs->y = -1;
400 }
401 
403  int height, int stride)
404 {
405  cs->b0 = buffer + mirror(-1 - 1, height - 1) * stride;
406  cs->b1 = buffer + mirror(-1, height - 1) * stride;
407  cs->y = -1;
408 }
409 
411  IDWTELEM *temp,
412  int width, int height,
413  int stride_line)
414 {
415  int y = cs->y;
416 
417  IDWTELEM *b0 = cs->b0;
418  IDWTELEM *b1 = cs->b1;
420  mirror(y + 1, height - 1) *
421  stride_line);
423  mirror(y + 2, height - 1) *
424  stride_line);
425 
426  if (y + 1 < (unsigned)height && y < (unsigned)height) {
427  int x;
428 
429  for (x = 0; x < width; x++) {
430  b2[x] -= (b1[x] + b3[x] + 2) >> 2;
431  b1[x] += (b0[x] + b2[x]) >> 1;
432  }
433  } else {
434  if (y + 1 < (unsigned)height)
435  vertical_compose53iL0(b1, b2, b3, width);
436  if (y + 0 < (unsigned)height)
437  vertical_compose53iH0(b0, b1, b2, width);
438  }
439 
440  if (y - 1 < (unsigned)height)
441  horizontal_compose53i(b0, temp, width);
442  if (y + 0 < (unsigned)height)
443  horizontal_compose53i(b1, temp, width);
444 
445  cs->b0 = b2;
446  cs->b1 = b3;
447  cs->y += 2;
448 }
449 
451  IDWTELEM *temp, int width, int height,
452  int stride)
453 {
454  int y = cs->y;
455  IDWTELEM *b0 = cs->b0;
456  IDWTELEM *b1 = cs->b1;
457  IDWTELEM *b2 = buffer + mirror(y + 1, height - 1) * stride;
458  IDWTELEM *b3 = buffer + mirror(y + 2, height - 1) * stride;
459 
460  if (y + 1 < (unsigned)height)
461  vertical_compose53iL0(b1, b2, b3, width);
462  if (y + 0 < (unsigned)height)
463  vertical_compose53iH0(b0, b1, b2, width);
464 
465  if (y - 1 < (unsigned)height)
466  horizontal_compose53i(b0, temp, width);
467  if (y + 0 < (unsigned)height)
468  horizontal_compose53i(b1, temp, width);
469 
470  cs->b0 = b2;
471  cs->b1 = b3;
472  cs->y += 2;
473 }
474 
476 {
477  const int w2 = (width + 1) >> 1;
478  int x;
479 
480  temp[0] = b[0] - ((3 * b[w2] + 2) >> 2);
481  for (x = 1; x < (width >> 1); x++) {
482  temp[2 * x] = b[x] - ((3 * (b[x + w2 - 1] + b[x + w2]) + 4) >> 3);
483  temp[2 * x - 1] = b[x + w2 - 1] - temp[2 * x - 2] - temp[2 * x];
484  }
485  if (width & 1) {
486  temp[2 * x] = b[x] - ((3 * b[x + w2 - 1] + 2) >> 2);
487  temp[2 * x - 1] = b[x + w2 - 1] - temp[2 * x - 2] - temp[2 * x];
488  } else
489  temp[2 * x - 1] = b[x + w2 - 1] - 2 * temp[2 * x - 2];
490 
491  b[0] = temp[0] + ((2 * temp[0] + temp[1] + 4) >> 3);
492  for (x = 2; x < width - 1; x += 2) {
493  b[x] = temp[x] + ((4 * temp[x] + temp[x - 1] + temp[x + 1] + 8) >> 4);
494  b[x - 1] = temp[x - 1] + ((3 * (b[x - 2] + b[x])) >> 1);
495  }
496  if (width & 1) {
497  b[x] = temp[x] + ((2 * temp[x] + temp[x - 1] + 4) >> 3);
498  b[x - 1] = temp[x - 1] + ((3 * (b[x - 2] + b[x])) >> 1);
499  } else
500  b[x - 1] = temp[x - 1] + 3 * b[x - 2];
501 }
502 
504  int width)
505 {
506  int i;
507 
508  for (i = 0; i < width; i++)
509  b1[i] += (W_AM * (b0[i] + b2[i]) + W_AO) >> W_AS;
510 }
511 
513  int width)
514 {
515  int i;
516 
517  for (i = 0; i < width; i++)
518  b1[i] -= (W_CM * (b0[i] + b2[i]) + W_CO) >> W_CS;
519 }
520 
522  int width)
523 {
524  int i;
525 
526  for (i = 0; i < width; i++)
527  b1[i] += (W_BM * (b0[i] + b2[i]) + 4 * b1[i] + W_BO) >> W_BS;
528 }
529 
531  int width)
532 {
533  int i;
534 
535  for (i = 0; i < width; i++)
536  b1[i] -= (W_DM * (b0[i] + b2[i]) + W_DO) >> W_DS;
537 }
538 
540  IDWTELEM *b3, IDWTELEM *b4, IDWTELEM *b5,
541  int width)
542 {
543  int i;
544 
545  for (i = 0; i < width; i++) {
546  b4[i] -= (W_DM * (b3[i] + b5[i]) + W_DO) >> W_DS;
547  b3[i] -= (W_CM * (b2[i] + b4[i]) + W_CO) >> W_CS;
548  b2[i] += (W_BM * (b1[i] + b3[i]) + 4 * b2[i] + W_BO) >> W_BS;
549  b1[i] += (W_AM * (b0[i] + b2[i]) + W_AO) >> W_AS;
550  }
551 }
552 
554  int height, int stride_line)
555 {
556  cs->b0 = slice_buffer_get_line(sb, mirror(-3 - 1, height - 1) * stride_line);
557  cs->b1 = slice_buffer_get_line(sb, mirror(-3, height - 1) * stride_line);
558  cs->b2 = slice_buffer_get_line(sb, mirror(-3 + 1, height - 1) * stride_line);
559  cs->b3 = slice_buffer_get_line(sb, mirror(-3 + 2, height - 1) * stride_line);
560  cs->y = -3;
561 }
562 
564  int stride)
565 {
566  cs->b0 = buffer + mirror(-3 - 1, height - 1) * stride;
567  cs->b1 = buffer + mirror(-3, height - 1) * stride;
568  cs->b2 = buffer + mirror(-3 + 1, height - 1) * stride;
569  cs->b3 = buffer + mirror(-3 + 2, height - 1) * stride;
570  cs->y = -3;
571 }
572 
574  slice_buffer * sb, IDWTELEM *temp,
575  int width, int height,
576  int stride_line)
577 {
578  int y = cs->y;
579 
580  IDWTELEM *b0 = cs->b0;
581  IDWTELEM *b1 = cs->b1;
582  IDWTELEM *b2 = cs->b2;
583  IDWTELEM *b3 = cs->b3;
585  mirror(y + 3, height - 1) *
586  stride_line);
588  mirror(y + 4, height - 1) *
589  stride_line);
590 
591  if (y > 0 && y + 4 < height) {
592  dsp->vertical_compose97i(b0, b1, b2, b3, b4, b5, width);
593  } else {
594  if (y + 3 < (unsigned)height)
595  vertical_compose97iL1(b3, b4, b5, width);
596  if (y + 2 < (unsigned)height)
597  vertical_compose97iH1(b2, b3, b4, width);
598  if (y + 1 < (unsigned)height)
599  vertical_compose97iL0(b1, b2, b3, width);
600  if (y + 0 < (unsigned)height)
601  vertical_compose97iH0(b0, b1, b2, width);
602  }
603 
604  if (y - 1 < (unsigned)height)
605  dsp->horizontal_compose97i(b0, temp, width);
606  if (y + 0 < (unsigned)height)
607  dsp->horizontal_compose97i(b1, temp, width);
608 
609  cs->b0 = b2;
610  cs->b1 = b3;
611  cs->b2 = b4;
612  cs->b3 = b5;
613  cs->y += 2;
614 }
615 
617  IDWTELEM *temp, int width, int height,
618  int stride)
619 {
620  int y = cs->y;
621  IDWTELEM *b0 = cs->b0;
622  IDWTELEM *b1 = cs->b1;
623  IDWTELEM *b2 = cs->b2;
624  IDWTELEM *b3 = cs->b3;
625  IDWTELEM *b4 = buffer + mirror(y + 3, height - 1) * stride;
626  IDWTELEM *b5 = buffer + mirror(y + 4, height - 1) * stride;
627 
628  if (y + 3 < (unsigned)height)
629  vertical_compose97iL1(b3, b4, b5, width);
630  if (y + 2 < (unsigned)height)
631  vertical_compose97iH1(b2, b3, b4, width);
632  if (y + 1 < (unsigned)height)
633  vertical_compose97iL0(b1, b2, b3, width);
634  if (y + 0 < (unsigned)height)
635  vertical_compose97iH0(b0, b1, b2, width);
636 
637  if (y - 1 < (unsigned)height)
638  ff_snow_horizontal_compose97i(b0, temp, width);
639  if (y + 0 < (unsigned)height)
640  ff_snow_horizontal_compose97i(b1, temp, width);
641 
642  cs->b0 = b2;
643  cs->b1 = b3;
644  cs->b2 = b4;
645  cs->b3 = b5;
646  cs->y += 2;
647 }
648 
650  int height, int stride_line, int type,
651  int decomposition_count)
652 {
653  int level;
654  for (level = decomposition_count - 1; level >= 0; level--) {
655  switch (type) {
656  case DWT_97:
657  spatial_compose97i_buffered_init(cs + level, sb, height >> level,
658  stride_line << level);
659  break;
660  case DWT_53:
661  spatial_compose53i_buffered_init(cs + level, sb, height >> level,
662  stride_line << level);
663  break;
664  }
665  }
666 }
667 
669  slice_buffer *slice_buf, IDWTELEM *temp,
670  int width, int height, int stride_line,
671  int type, int decomposition_count, int y)
672 {
673  const int support = type == 1 ? 3 : 5;
674  int level;
675  if (type == 2)
676  return;
677 
678  for (level = decomposition_count - 1; level >= 0; level--)
679  while (cs[level].y <= FFMIN((y >> level) + support, height >> level)) {
680  switch (type) {
681  case DWT_97:
682  spatial_compose97i_dy_buffered(dsp, cs + level, slice_buf, temp,
683  width >> level,
684  height >> level,
685  stride_line << level);
686  break;
687  case DWT_53:
688  spatial_compose53i_dy_buffered(cs + level, slice_buf, temp,
689  width >> level,
690  height >> level,
691  stride_line << level);
692  break;
693  }
694  }
695 }
696 
698  int height, int stride, int type,
699  int decomposition_count)
700 {
701  int level;
702  for (level = decomposition_count - 1; level >= 0; level--) {
703  switch (type) {
704  case DWT_97:
705  spatial_compose97i_init(cs + level, buffer, height >> level,
706  stride << level);
707  break;
708  case DWT_53:
709  spatial_compose53i_init(cs + level, buffer, height >> level,
710  stride << level);
711  break;
712  }
713  }
714 }
715 
717  IDWTELEM *temp, int width, int height,
718  int stride, int type,
719  int decomposition_count, int y)
720 {
721  const int support = type == 1 ? 3 : 5;
722  int level;
723  if (type == 2)
724  return;
725 
726  for (level = decomposition_count - 1; level >= 0; level--)
727  while (cs[level].y <= FFMIN((y >> level) + support, height >> level)) {
728  switch (type) {
729  case DWT_97:
730  spatial_compose97i_dy(cs + level, buffer, temp, width >> level,
731  height >> level, stride << level);
732  break;
733  case DWT_53:
734  spatial_compose53i_dy(cs + level, buffer, temp, width >> level,
735  height >> level, stride << level);
736  break;
737  }
738  }
739 }
740 
742  int stride, int type, int decomposition_count)
743 {
745  int y;
746  ff_spatial_idwt_init(cs, buffer, width, height, stride, type,
747  decomposition_count);
748  for (y = 0; y < height; y += 4)
749  ff_spatial_idwt_slice(cs, buffer, temp, width, height, stride, type,
750  decomposition_count, y);
751 }
752 
753 static inline int w_c(struct MpegEncContext *v, uint8_t *pix1, uint8_t *pix2, ptrdiff_t line_size,
754  int w, int h, int type)
755 {
756  int s, i, j;
757  const int dec_count = w == 8 ? 3 : 4;
758  int tmp[32 * 32], tmp2[32];
759  int level, ori;
760  static const int scale[2][2][4][4] = {
761  {
762  { // 9/7 8x8 dec=3
763  { 268, 239, 239, 213 },
764  { 0, 224, 224, 152 },
765  { 0, 135, 135, 110 },
766  },
767  { // 9/7 16x16 or 32x32 dec=4
768  { 344, 310, 310, 280 },
769  { 0, 320, 320, 228 },
770  { 0, 175, 175, 136 },
771  { 0, 129, 129, 102 },
772  }
773  },
774  {
775  { // 5/3 8x8 dec=3
776  { 275, 245, 245, 218 },
777  { 0, 230, 230, 156 },
778  { 0, 138, 138, 113 },
779  },
780  { // 5/3 16x16 or 32x32 dec=4
781  { 352, 317, 317, 286 },
782  { 0, 328, 328, 233 },
783  { 0, 180, 180, 140 },
784  { 0, 132, 132, 105 },
785  }
786  }
787  };
788 
789  for (i = 0; i < h; i++) {
790  for (j = 0; j < w; j += 4) {
791  tmp[32 * i + j + 0] = (pix1[j + 0] - pix2[j + 0]) << 4;
792  tmp[32 * i + j + 1] = (pix1[j + 1] - pix2[j + 1]) << 4;
793  tmp[32 * i + j + 2] = (pix1[j + 2] - pix2[j + 2]) << 4;
794  tmp[32 * i + j + 3] = (pix1[j + 3] - pix2[j + 3]) << 4;
795  }
796  pix1 += line_size;
797  pix2 += line_size;
798  }
799 
800  ff_spatial_dwt(tmp, tmp2, w, h, 32, type, dec_count);
801 
802  s = 0;
803  av_assert1(w == h);
804  for (level = 0; level < dec_count; level++)
805  for (ori = level ? 1 : 0; ori < 4; ori++) {
806  int size = w >> (dec_count - level);
807  int sx = (ori & 1) ? size : 0;
808  int stride = 32 << (dec_count - level);
809  int sy = (ori & 2) ? stride >> 1 : 0;
810 
811  for (i = 0; i < size; i++)
812  for (j = 0; j < size; j++) {
813  int v = tmp[sx + sy + i * stride + j] *
814  scale[type][dec_count - 3][level][ori];
815  s += FFABS(v);
816  }
817  }
818  av_assert1(s >= 0);
819  return s >> 9;
820 }
821 
822 static int w53_8_c(struct MpegEncContext *v, uint8_t *pix1, uint8_t *pix2, ptrdiff_t line_size, int h)
823 {
824  return w_c(v, pix1, pix2, line_size, 8, h, 1);
825 }
826 
827 static int w97_8_c(struct MpegEncContext *v, uint8_t *pix1, uint8_t *pix2, ptrdiff_t line_size, int h)
828 {
829  return w_c(v, pix1, pix2, line_size, 8, h, 0);
830 }
831 
832 static int w53_16_c(struct MpegEncContext *v, uint8_t *pix1, uint8_t *pix2, ptrdiff_t line_size, int h)
833 {
834  return w_c(v, pix1, pix2, line_size, 16, h, 1);
835 }
836 
837 static int w97_16_c(struct MpegEncContext *v, uint8_t *pix1, uint8_t *pix2, ptrdiff_t line_size, int h)
838 {
839  return w_c(v, pix1, pix2, line_size, 16, h, 0);
840 }
841 
842 int ff_w53_32_c(struct MpegEncContext *v, uint8_t *pix1, uint8_t *pix2, ptrdiff_t line_size, int h)
843 {
844  return w_c(v, pix1, pix2, line_size, 32, h, 1);
845 }
846 
847 int ff_w97_32_c(struct MpegEncContext *v, uint8_t *pix1, uint8_t *pix2, ptrdiff_t line_size, int h)
848 {
849  return w_c(v, pix1, pix2, line_size, 32, h, 0);
850 }
851 
853 {
854  c->w53[0] = w53_16_c;
855  c->w53[1] = w53_8_c;
856  c->w97[0] = w97_16_c;
857  c->w97[1] = w97_8_c;
858 }
859 
861 {
865 
866  if (HAVE_MMX)
867  ff_dwt_init_x86(c);
868 }
869 
870