00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "libavutil/attributes.h"
00023 #include "libavutil/avassert.h"
00024 #include "libavutil/common.h"
00025 #include "dsputil.h"
00026 #include "dwt.h"
00027 #include "libavcodec/x86/dwt.h"
00028
00029 int ff_slice_buffer_init(slice_buffer *buf, int line_count,
00030 int max_allocated_lines, int line_width,
00031 IDWTELEM *base_buffer)
00032 {
00033 int i;
00034
00035 buf->base_buffer = base_buffer;
00036 buf->line_count = line_count;
00037 buf->line_width = line_width;
00038 buf->data_count = max_allocated_lines;
00039 buf->line = av_mallocz(sizeof(IDWTELEM *) * line_count);
00040 if (!buf->line)
00041 return AVERROR(ENOMEM);
00042 buf->data_stack = av_malloc(sizeof(IDWTELEM *) * max_allocated_lines);
00043 if (!buf->data_stack) {
00044 av_freep(&buf->line);
00045 return AVERROR(ENOMEM);
00046 }
00047
00048 for (i = 0; i < max_allocated_lines; i++) {
00049 buf->data_stack[i] = av_malloc(sizeof(IDWTELEM) * line_width);
00050 if (!buf->data_stack[i]) {
00051 for (i--; i >=0; i--)
00052 av_freep(&buf->data_stack[i]);
00053 av_freep(&buf->data_stack);
00054 av_freep(&buf->line);
00055 return AVERROR(ENOMEM);
00056 }
00057 }
00058
00059 buf->data_stack_top = max_allocated_lines - 1;
00060 return 0;
00061 }
00062
00063 IDWTELEM *ff_slice_buffer_load_line(slice_buffer *buf, int line)
00064 {
00065 IDWTELEM *buffer;
00066
00067 av_assert0(buf->data_stack_top >= 0);
00068
00069 if (buf->line[line])
00070 return buf->line[line];
00071
00072 buffer = buf->data_stack[buf->data_stack_top];
00073 buf->data_stack_top--;
00074 buf->line[line] = buffer;
00075
00076 return buffer;
00077 }
00078
00079 void ff_slice_buffer_release(slice_buffer *buf, int line)
00080 {
00081 IDWTELEM *buffer;
00082
00083 av_assert1(line >= 0 && line < buf->line_count);
00084 av_assert1(buf->line[line]);
00085
00086 buffer = buf->line[line];
00087 buf->data_stack_top++;
00088 buf->data_stack[buf->data_stack_top] = buffer;
00089 buf->line[line] = NULL;
00090 }
00091
00092 void ff_slice_buffer_flush(slice_buffer *buf)
00093 {
00094 int i;
00095 for (i = 0; i < buf->line_count; i++)
00096 if (buf->line[i])
00097 ff_slice_buffer_release(buf, i);
00098 }
00099
00100 void ff_slice_buffer_destroy(slice_buffer *buf)
00101 {
00102 int i;
00103 ff_slice_buffer_flush(buf);
00104
00105 for (i = buf->data_count - 1; i >= 0; i--)
00106 av_freep(&buf->data_stack[i]);
00107 av_freep(&buf->data_stack);
00108 av_freep(&buf->line);
00109 }
00110
00111 static inline int mirror(int v, int m)
00112 {
00113 while ((unsigned)v > (unsigned)m) {
00114 v = -v;
00115 if (v < 0)
00116 v += 2 * m;
00117 }
00118 return v;
00119 }
00120
00121 static av_always_inline void lift(DWTELEM *dst, DWTELEM *src, DWTELEM *ref,
00122 int dst_step, int src_step, int ref_step,
00123 int width, int mul, int add, int shift,
00124 int highpass, int inverse)
00125 {
00126 const int mirror_left = !highpass;
00127 const int mirror_right = (width & 1) ^ highpass;
00128 const int w = (width >> 1) - 1 + (highpass & width);
00129 int i;
00130
00131 #define LIFT(src, ref, inv) ((src) + ((inv) ? -(ref) : +(ref)))
00132 if (mirror_left) {
00133 dst[0] = LIFT(src[0], ((mul * 2 * ref[0] + add) >> shift), inverse);
00134 dst += dst_step;
00135 src += src_step;
00136 }
00137
00138 for (i = 0; i < w; i++)
00139 dst[i * dst_step] = LIFT(src[i * src_step],
00140 ((mul * (ref[i * ref_step] +
00141 ref[(i + 1) * ref_step]) +
00142 add) >> shift),
00143 inverse);
00144
00145 if (mirror_right)
00146 dst[w * dst_step] = LIFT(src[w * src_step],
00147 ((mul * 2 * ref[w * ref_step] + add) >> shift),
00148 inverse);
00149 }
00150
00151 static av_always_inline void liftS(DWTELEM *dst, DWTELEM *src, DWTELEM *ref,
00152 int dst_step, int src_step, int ref_step,
00153 int width, int mul, int add, int shift,
00154 int highpass, int inverse)
00155 {
00156 const int mirror_left = !highpass;
00157 const int mirror_right = (width & 1) ^ highpass;
00158 const int w = (width >> 1) - 1 + (highpass & width);
00159 int i;
00160
00161 av_assert1(shift == 4);
00162 #define LIFTS(src, ref, inv) \
00163 ((inv) ? (src) + (((ref) + 4 * (src)) >> shift) \
00164 : -((-16 * (src) + (ref) + add / \
00165 4 + 1 + (5 << 25)) / (5 * 4) - (1 << 23)))
00166 if (mirror_left) {
00167 dst[0] = LIFTS(src[0], mul * 2 * ref[0] + add, inverse);
00168 dst += dst_step;
00169 src += src_step;
00170 }
00171
00172 for (i = 0; i < w; i++)
00173 dst[i * dst_step] = LIFTS(src[i * src_step],
00174 mul * (ref[i * ref_step] +
00175 ref[(i + 1) * ref_step]) + add,
00176 inverse);
00177
00178 if (mirror_right)
00179 dst[w * dst_step] = LIFTS(src[w * src_step],
00180 mul * 2 * ref[w * ref_step] + add,
00181 inverse);
00182 }
00183
00184 static void horizontal_decompose53i(DWTELEM *b, DWTELEM *temp, int width)
00185 {
00186 const int width2 = width >> 1;
00187 int x;
00188 const int w2 = (width + 1) >> 1;
00189
00190 for (x = 0; x < width2; x++) {
00191 temp[x] = b[2 * x];
00192 temp[x + w2] = b[2 * x + 1];
00193 }
00194 if (width & 1)
00195 temp[x] = b[2 * x];
00196 lift(b + w2, temp + w2, temp, 1, 1, 1, width, -1, 0, 1, 1, 0);
00197 lift(b, temp, b + w2, 1, 1, 1, width, 1, 2, 2, 0, 0);
00198 }
00199
00200 static void vertical_decompose53iH0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2,
00201 int width)
00202 {
00203 int i;
00204
00205 for (i = 0; i < width; i++)
00206 b1[i] -= (b0[i] + b2[i]) >> 1;
00207 }
00208
00209 static void vertical_decompose53iL0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2,
00210 int width)
00211 {
00212 int i;
00213
00214 for (i = 0; i < width; i++)
00215 b1[i] += (b0[i] + b2[i] + 2) >> 2;
00216 }
00217
00218 static void spatial_decompose53i(DWTELEM *buffer, DWTELEM *temp,
00219 int width, int height, int stride)
00220 {
00221 int y;
00222 DWTELEM *b0 = buffer + mirror(-2 - 1, height - 1) * stride;
00223 DWTELEM *b1 = buffer + mirror(-2, height - 1) * stride;
00224
00225 for (y = -2; y < height; y += 2) {
00226 DWTELEM *b2 = buffer + mirror(y + 1, height - 1) * stride;
00227 DWTELEM *b3 = buffer + mirror(y + 2, height - 1) * stride;
00228
00229 if (y + 1 < (unsigned)height)
00230 horizontal_decompose53i(b2, temp, width);
00231 if (y + 2 < (unsigned)height)
00232 horizontal_decompose53i(b3, temp, width);
00233
00234 if (y + 1 < (unsigned)height)
00235 vertical_decompose53iH0(b1, b2, b3, width);
00236 if (y + 0 < (unsigned)height)
00237 vertical_decompose53iL0(b0, b1, b2, width);
00238
00239 b0 = b2;
00240 b1 = b3;
00241 }
00242 }
00243
00244 static void horizontal_decompose97i(DWTELEM *b, DWTELEM *temp, int width)
00245 {
00246 const int w2 = (width + 1) >> 1;
00247
00248 lift(temp + w2, b + 1, b, 1, 2, 2, width, W_AM, W_AO, W_AS, 1, 1);
00249 liftS(temp, b, temp + w2, 1, 2, 1, width, W_BM, W_BO, W_BS, 0, 0);
00250 lift(b + w2, temp + w2, temp, 1, 1, 1, width, W_CM, W_CO, W_CS, 1, 0);
00251 lift(b, temp, b + w2, 1, 1, 1, width, W_DM, W_DO, W_DS, 0, 0);
00252 }
00253
00254 static void vertical_decompose97iH0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2,
00255 int width)
00256 {
00257 int i;
00258
00259 for (i = 0; i < width; i++)
00260 b1[i] -= (W_AM * (b0[i] + b2[i]) + W_AO) >> W_AS;
00261 }
00262
00263 static void vertical_decompose97iH1(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2,
00264 int width)
00265 {
00266 int i;
00267
00268 for (i = 0; i < width; i++)
00269 b1[i] += (W_CM * (b0[i] + b2[i]) + W_CO) >> W_CS;
00270 }
00271
00272 static void vertical_decompose97iL0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2,
00273 int width)
00274 {
00275 int i;
00276
00277 for (i = 0; i < width; i++)
00278 b1[i] = (16 * 4 * b1[i] - 4 * (b0[i] + b2[i]) + W_BO * 5 + (5 << 27)) /
00279 (5 * 16) - (1 << 23);
00280 }
00281
00282 static void vertical_decompose97iL1(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2,
00283 int width)
00284 {
00285 int i;
00286
00287 for (i = 0; i < width; i++)
00288 b1[i] += (W_DM * (b0[i] + b2[i]) + W_DO) >> W_DS;
00289 }
00290
00291 static void spatial_decompose97i(DWTELEM *buffer, DWTELEM *temp,
00292 int width, int height, int stride)
00293 {
00294 int y;
00295 DWTELEM *b0 = buffer + mirror(-4 - 1, height - 1) * stride;
00296 DWTELEM *b1 = buffer + mirror(-4, height - 1) * stride;
00297 DWTELEM *b2 = buffer + mirror(-4 + 1, height - 1) * stride;
00298 DWTELEM *b3 = buffer + mirror(-4 + 2, height - 1) * stride;
00299
00300 for (y = -4; y < height; y += 2) {
00301 DWTELEM *b4 = buffer + mirror(y + 3, height - 1) * stride;
00302 DWTELEM *b5 = buffer + mirror(y + 4, height - 1) * stride;
00303
00304 if (y + 3 < (unsigned)height)
00305 horizontal_decompose97i(b4, temp, width);
00306 if (y + 4 < (unsigned)height)
00307 horizontal_decompose97i(b5, temp, width);
00308
00309 if (y + 3 < (unsigned)height)
00310 vertical_decompose97iH0(b3, b4, b5, width);
00311 if (y + 2 < (unsigned)height)
00312 vertical_decompose97iL0(b2, b3, b4, width);
00313 if (y + 1 < (unsigned)height)
00314 vertical_decompose97iH1(b1, b2, b3, width);
00315 if (y + 0 < (unsigned)height)
00316 vertical_decompose97iL1(b0, b1, b2, width);
00317
00318 b0 = b2;
00319 b1 = b3;
00320 b2 = b4;
00321 b3 = b5;
00322 }
00323 }
00324
00325 void ff_spatial_dwt(DWTELEM *buffer, DWTELEM *temp, int width, int height,
00326 int stride, int type, int decomposition_count)
00327 {
00328 int level;
00329
00330 for (level = 0; level < decomposition_count; level++) {
00331 switch (type) {
00332 case DWT_97:
00333 spatial_decompose97i(buffer, temp,
00334 width >> level, height >> level,
00335 stride << level);
00336 break;
00337 case DWT_53:
00338 spatial_decompose53i(buffer, temp,
00339 width >> level, height >> level,
00340 stride << level);
00341 break;
00342 }
00343 }
00344 }
00345
00346 static void horizontal_compose53i(IDWTELEM *b, IDWTELEM *temp, int width)
00347 {
00348 const int width2 = width >> 1;
00349 const int w2 = (width + 1) >> 1;
00350 int x;
00351
00352 for (x = 0; x < width2; x++) {
00353 temp[2 * x] = b[x];
00354 temp[2 * x + 1] = b[x + w2];
00355 }
00356 if (width & 1)
00357 temp[2 * x] = b[x];
00358
00359 b[0] = temp[0] - ((temp[1] + 1) >> 1);
00360 for (x = 2; x < width - 1; x += 2) {
00361 b[x] = temp[x] - ((temp[x - 1] + temp[x + 1] + 2) >> 2);
00362 b[x - 1] = temp[x - 1] + ((b[x - 2] + b[x] + 1) >> 1);
00363 }
00364 if (width & 1) {
00365 b[x] = temp[x] - ((temp[x - 1] + 1) >> 1);
00366 b[x - 1] = temp[x - 1] + ((b[x - 2] + b[x] + 1) >> 1);
00367 } else
00368 b[x - 1] = temp[x - 1] + b[x - 2];
00369 }
00370
00371 static void vertical_compose53iH0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2,
00372 int width)
00373 {
00374 int i;
00375
00376 for (i = 0; i < width; i++)
00377 b1[i] += (b0[i] + b2[i]) >> 1;
00378 }
00379
00380 static void vertical_compose53iL0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2,
00381 int width)
00382 {
00383 int i;
00384
00385 for (i = 0; i < width; i++)
00386 b1[i] -= (b0[i] + b2[i] + 2) >> 2;
00387 }
00388
00389 static void spatial_compose53i_buffered_init(DWTCompose *cs, slice_buffer *sb,
00390 int height, int stride_line)
00391 {
00392 cs->b0 = slice_buffer_get_line(sb,
00393 mirror(-1 - 1, height - 1) * stride_line);
00394 cs->b1 = slice_buffer_get_line(sb, mirror(-1, height - 1) * stride_line);
00395 cs->y = -1;
00396 }
00397
00398 static void spatial_compose53i_init(DWTCompose *cs, IDWTELEM *buffer,
00399 int height, int stride)
00400 {
00401 cs->b0 = buffer + mirror(-1 - 1, height - 1) * stride;
00402 cs->b1 = buffer + mirror(-1, height - 1) * stride;
00403 cs->y = -1;
00404 }
00405
00406 static void spatial_compose53i_dy_buffered(DWTCompose *cs, slice_buffer *sb,
00407 IDWTELEM *temp,
00408 int width, int height,
00409 int stride_line)
00410 {
00411 int y = cs->y;
00412
00413 IDWTELEM *b0 = cs->b0;
00414 IDWTELEM *b1 = cs->b1;
00415 IDWTELEM *b2 = slice_buffer_get_line(sb,
00416 mirror(y + 1, height - 1) *
00417 stride_line);
00418 IDWTELEM *b3 = slice_buffer_get_line(sb,
00419 mirror(y + 2, height - 1) *
00420 stride_line);
00421
00422 if (y + 1 < (unsigned)height && y < (unsigned)height) {
00423 int x;
00424
00425 for (x = 0; x < width; x++) {
00426 b2[x] -= (b1[x] + b3[x] + 2) >> 2;
00427 b1[x] += (b0[x] + b2[x]) >> 1;
00428 }
00429 } else {
00430 if (y + 1 < (unsigned)height)
00431 vertical_compose53iL0(b1, b2, b3, width);
00432 if (y + 0 < (unsigned)height)
00433 vertical_compose53iH0(b0, b1, b2, width);
00434 }
00435
00436 if (y - 1 < (unsigned)height)
00437 horizontal_compose53i(b0, temp, width);
00438 if (y + 0 < (unsigned)height)
00439 horizontal_compose53i(b1, temp, width);
00440
00441 cs->b0 = b2;
00442 cs->b1 = b3;
00443 cs->y += 2;
00444 }
00445
00446 static void spatial_compose53i_dy(DWTCompose *cs, IDWTELEM *buffer,
00447 IDWTELEM *temp, int width, int height,
00448 int stride)
00449 {
00450 int y = cs->y;
00451 IDWTELEM *b0 = cs->b0;
00452 IDWTELEM *b1 = cs->b1;
00453 IDWTELEM *b2 = buffer + mirror(y + 1, height - 1) * stride;
00454 IDWTELEM *b3 = buffer + mirror(y + 2, height - 1) * stride;
00455
00456 if (y + 1 < (unsigned)height)
00457 vertical_compose53iL0(b1, b2, b3, width);
00458 if (y + 0 < (unsigned)height)
00459 vertical_compose53iH0(b0, b1, b2, width);
00460
00461 if (y - 1 < (unsigned)height)
00462 horizontal_compose53i(b0, temp, width);
00463 if (y + 0 < (unsigned)height)
00464 horizontal_compose53i(b1, temp, width);
00465
00466 cs->b0 = b2;
00467 cs->b1 = b3;
00468 cs->y += 2;
00469 }
00470
00471 void ff_snow_horizontal_compose97i(IDWTELEM *b, IDWTELEM *temp, int width)
00472 {
00473 const int w2 = (width + 1) >> 1;
00474 int x;
00475
00476 temp[0] = b[0] - ((3 * b[w2] + 2) >> 2);
00477 for (x = 1; x < (width >> 1); x++) {
00478 temp[2 * x] = b[x] - ((3 * (b[x + w2 - 1] + b[x + w2]) + 4) >> 3);
00479 temp[2 * x - 1] = b[x + w2 - 1] - temp[2 * x - 2] - temp[2 * x];
00480 }
00481 if (width & 1) {
00482 temp[2 * x] = b[x] - ((3 * b[x + w2 - 1] + 2) >> 2);
00483 temp[2 * x - 1] = b[x + w2 - 1] - temp[2 * x - 2] - temp[2 * x];
00484 } else
00485 temp[2 * x - 1] = b[x + w2 - 1] - 2 * temp[2 * x - 2];
00486
00487 b[0] = temp[0] + ((2 * temp[0] + temp[1] + 4) >> 3);
00488 for (x = 2; x < width - 1; x += 2) {
00489 b[x] = temp[x] + ((4 * temp[x] + temp[x - 1] + temp[x + 1] + 8) >> 4);
00490 b[x - 1] = temp[x - 1] + ((3 * (b[x - 2] + b[x])) >> 1);
00491 }
00492 if (width & 1) {
00493 b[x] = temp[x] + ((2 * temp[x] + temp[x - 1] + 4) >> 3);
00494 b[x - 1] = temp[x - 1] + ((3 * (b[x - 2] + b[x])) >> 1);
00495 } else
00496 b[x - 1] = temp[x - 1] + 3 * b[x - 2];
00497 }
00498
00499 static void vertical_compose97iH0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2,
00500 int width)
00501 {
00502 int i;
00503
00504 for (i = 0; i < width; i++)
00505 b1[i] += (W_AM * (b0[i] + b2[i]) + W_AO) >> W_AS;
00506 }
00507
00508 static void vertical_compose97iH1(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2,
00509 int width)
00510 {
00511 int i;
00512
00513 for (i = 0; i < width; i++)
00514 b1[i] -= (W_CM * (b0[i] + b2[i]) + W_CO) >> W_CS;
00515 }
00516
00517 static void vertical_compose97iL0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2,
00518 int width)
00519 {
00520 int i;
00521
00522 for (i = 0; i < width; i++)
00523 b1[i] += (W_BM * (b0[i] + b2[i]) + 4 * b1[i] + W_BO) >> W_BS;
00524 }
00525
00526 static void vertical_compose97iL1(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2,
00527 int width)
00528 {
00529 int i;
00530
00531 for (i = 0; i < width; i++)
00532 b1[i] -= (W_DM * (b0[i] + b2[i]) + W_DO) >> W_DS;
00533 }
00534
00535 void ff_snow_vertical_compose97i(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2,
00536 IDWTELEM *b3, IDWTELEM *b4, IDWTELEM *b5,
00537 int width)
00538 {
00539 int i;
00540
00541 for (i = 0; i < width; i++) {
00542 b4[i] -= (W_DM * (b3[i] + b5[i]) + W_DO) >> W_DS;
00543 b3[i] -= (W_CM * (b2[i] + b4[i]) + W_CO) >> W_CS;
00544 b2[i] += (W_BM * (b1[i] + b3[i]) + 4 * b2[i] + W_BO) >> W_BS;
00545 b1[i] += (W_AM * (b0[i] + b2[i]) + W_AO) >> W_AS;
00546 }
00547 }
00548
00549 static void spatial_compose97i_buffered_init(DWTCompose *cs, slice_buffer *sb,
00550 int height, int stride_line)
00551 {
00552 cs->b0 = slice_buffer_get_line(sb, mirror(-3 - 1, height - 1) * stride_line);
00553 cs->b1 = slice_buffer_get_line(sb, mirror(-3, height - 1) * stride_line);
00554 cs->b2 = slice_buffer_get_line(sb, mirror(-3 + 1, height - 1) * stride_line);
00555 cs->b3 = slice_buffer_get_line(sb, mirror(-3 + 2, height - 1) * stride_line);
00556 cs->y = -3;
00557 }
00558
00559 static void spatial_compose97i_init(DWTCompose *cs, IDWTELEM *buffer, int height,
00560 int stride)
00561 {
00562 cs->b0 = buffer + mirror(-3 - 1, height - 1) * stride;
00563 cs->b1 = buffer + mirror(-3, height - 1) * stride;
00564 cs->b2 = buffer + mirror(-3 + 1, height - 1) * stride;
00565 cs->b3 = buffer + mirror(-3 + 2, height - 1) * stride;
00566 cs->y = -3;
00567 }
00568
00569 static void spatial_compose97i_dy_buffered(DWTContext *dsp, DWTCompose *cs,
00570 slice_buffer * sb, IDWTELEM *temp,
00571 int width, int height,
00572 int stride_line)
00573 {
00574 int y = cs->y;
00575
00576 IDWTELEM *b0 = cs->b0;
00577 IDWTELEM *b1 = cs->b1;
00578 IDWTELEM *b2 = cs->b2;
00579 IDWTELEM *b3 = cs->b3;
00580 IDWTELEM *b4 = slice_buffer_get_line(sb,
00581 mirror(y + 3, height - 1) *
00582 stride_line);
00583 IDWTELEM *b5 = slice_buffer_get_line(sb,
00584 mirror(y + 4, height - 1) *
00585 stride_line);
00586
00587 if (y > 0 && y + 4 < height) {
00588 dsp->vertical_compose97i(b0, b1, b2, b3, b4, b5, width);
00589 } else {
00590 if (y + 3 < (unsigned)height)
00591 vertical_compose97iL1(b3, b4, b5, width);
00592 if (y + 2 < (unsigned)height)
00593 vertical_compose97iH1(b2, b3, b4, width);
00594 if (y + 1 < (unsigned)height)
00595 vertical_compose97iL0(b1, b2, b3, width);
00596 if (y + 0 < (unsigned)height)
00597 vertical_compose97iH0(b0, b1, b2, width);
00598 }
00599
00600 if (y - 1 < (unsigned)height)
00601 dsp->horizontal_compose97i(b0, temp, width);
00602 if (y + 0 < (unsigned)height)
00603 dsp->horizontal_compose97i(b1, temp, width);
00604
00605 cs->b0 = b2;
00606 cs->b1 = b3;
00607 cs->b2 = b4;
00608 cs->b3 = b5;
00609 cs->y += 2;
00610 }
00611
00612 static void spatial_compose97i_dy(DWTCompose *cs, IDWTELEM *buffer,
00613 IDWTELEM *temp, int width, int height,
00614 int stride)
00615 {
00616 int y = cs->y;
00617 IDWTELEM *b0 = cs->b0;
00618 IDWTELEM *b1 = cs->b1;
00619 IDWTELEM *b2 = cs->b2;
00620 IDWTELEM *b3 = cs->b3;
00621 IDWTELEM *b4 = buffer + mirror(y + 3, height - 1) * stride;
00622 IDWTELEM *b5 = buffer + mirror(y + 4, height - 1) * stride;
00623
00624 if (y + 3 < (unsigned)height)
00625 vertical_compose97iL1(b3, b4, b5, width);
00626 if (y + 2 < (unsigned)height)
00627 vertical_compose97iH1(b2, b3, b4, width);
00628 if (y + 1 < (unsigned)height)
00629 vertical_compose97iL0(b1, b2, b3, width);
00630 if (y + 0 < (unsigned)height)
00631 vertical_compose97iH0(b0, b1, b2, width);
00632
00633 if (y - 1 < (unsigned)height)
00634 ff_snow_horizontal_compose97i(b0, temp, width);
00635 if (y + 0 < (unsigned)height)
00636 ff_snow_horizontal_compose97i(b1, temp, width);
00637
00638 cs->b0 = b2;
00639 cs->b1 = b3;
00640 cs->b2 = b4;
00641 cs->b3 = b5;
00642 cs->y += 2;
00643 }
00644
00645 void ff_spatial_idwt_buffered_init(DWTCompose *cs, slice_buffer *sb, int width,
00646 int height, int stride_line, int type,
00647 int decomposition_count)
00648 {
00649 int level;
00650 for (level = decomposition_count - 1; level >= 0; level--) {
00651 switch (type) {
00652 case DWT_97:
00653 spatial_compose97i_buffered_init(cs + level, sb, height >> level,
00654 stride_line << level);
00655 break;
00656 case DWT_53:
00657 spatial_compose53i_buffered_init(cs + level, sb, height >> level,
00658 stride_line << level);
00659 break;
00660 }
00661 }
00662 }
00663
00664 void ff_spatial_idwt_buffered_slice(DWTContext *dsp, DWTCompose *cs,
00665 slice_buffer *slice_buf, IDWTELEM *temp,
00666 int width, int height, int stride_line,
00667 int type, int decomposition_count, int y)
00668 {
00669 const int support = type == 1 ? 3 : 5;
00670 int level;
00671 if (type == 2)
00672 return;
00673
00674 for (level = decomposition_count - 1; level >= 0; level--)
00675 while (cs[level].y <= FFMIN((y >> level) + support, height >> level)) {
00676 switch (type) {
00677 case DWT_97:
00678 spatial_compose97i_dy_buffered(dsp, cs + level, slice_buf, temp,
00679 width >> level,
00680 height >> level,
00681 stride_line << level);
00682 break;
00683 case DWT_53:
00684 spatial_compose53i_dy_buffered(cs + level, slice_buf, temp,
00685 width >> level,
00686 height >> level,
00687 stride_line << level);
00688 break;
00689 }
00690 }
00691 }
00692
00693 static void ff_spatial_idwt_init(DWTCompose *cs, IDWTELEM *buffer, int width,
00694 int height, int stride, int type,
00695 int decomposition_count)
00696 {
00697 int level;
00698 for (level = decomposition_count - 1; level >= 0; level--) {
00699 switch (type) {
00700 case DWT_97:
00701 spatial_compose97i_init(cs + level, buffer, height >> level,
00702 stride << level);
00703 break;
00704 case DWT_53:
00705 spatial_compose53i_init(cs + level, buffer, height >> level,
00706 stride << level);
00707 break;
00708 }
00709 }
00710 }
00711
00712 static void ff_spatial_idwt_slice(DWTCompose *cs, IDWTELEM *buffer,
00713 IDWTELEM *temp, int width, int height,
00714 int stride, int type,
00715 int decomposition_count, int y)
00716 {
00717 const int support = type == 1 ? 3 : 5;
00718 int level;
00719 if (type == 2)
00720 return;
00721
00722 for (level = decomposition_count - 1; level >= 0; level--)
00723 while (cs[level].y <= FFMIN((y >> level) + support, height >> level)) {
00724 switch (type) {
00725 case DWT_97:
00726 spatial_compose97i_dy(cs + level, buffer, temp, width >> level,
00727 height >> level, stride << level);
00728 break;
00729 case DWT_53:
00730 spatial_compose53i_dy(cs + level, buffer, temp, width >> level,
00731 height >> level, stride << level);
00732 break;
00733 }
00734 }
00735 }
00736
00737 void ff_spatial_idwt(IDWTELEM *buffer, IDWTELEM *temp, int width, int height,
00738 int stride, int type, int decomposition_count)
00739 {
00740 DWTCompose cs[MAX_DECOMPOSITIONS];
00741 int y;
00742 ff_spatial_idwt_init(cs, buffer, width, height, stride, type,
00743 decomposition_count);
00744 for (y = 0; y < height; y += 4)
00745 ff_spatial_idwt_slice(cs, buffer, temp, width, height, stride, type,
00746 decomposition_count, y);
00747 }
00748
00749 static inline int w_c(void *v, uint8_t *pix1, uint8_t *pix2, int line_size,
00750 int w, int h, int type)
00751 {
00752 int s, i, j;
00753 const int dec_count = w == 8 ? 3 : 4;
00754 int tmp[32 * 32], tmp2[32];
00755 int level, ori;
00756 static const int scale[2][2][4][4] = {
00757 {
00758 {
00759 { 268, 239, 239, 213 },
00760 { 0, 224, 224, 152 },
00761 { 0, 135, 135, 110 },
00762 },
00763 {
00764 { 344, 310, 310, 280 },
00765 { 0, 320, 320, 228 },
00766 { 0, 175, 175, 136 },
00767 { 0, 129, 129, 102 },
00768 }
00769 },
00770 {
00771 {
00772 { 275, 245, 245, 218 },
00773 { 0, 230, 230, 156 },
00774 { 0, 138, 138, 113 },
00775 },
00776 {
00777 { 352, 317, 317, 286 },
00778 { 0, 328, 328, 233 },
00779 { 0, 180, 180, 140 },
00780 { 0, 132, 132, 105 },
00781 }
00782 }
00783 };
00784
00785 for (i = 0; i < h; i++) {
00786 for (j = 0; j < w; j += 4) {
00787 tmp[32 * i + j + 0] = (pix1[j + 0] - pix2[j + 0]) << 4;
00788 tmp[32 * i + j + 1] = (pix1[j + 1] - pix2[j + 1]) << 4;
00789 tmp[32 * i + j + 2] = (pix1[j + 2] - pix2[j + 2]) << 4;
00790 tmp[32 * i + j + 3] = (pix1[j + 3] - pix2[j + 3]) << 4;
00791 }
00792 pix1 += line_size;
00793 pix2 += line_size;
00794 }
00795
00796 ff_spatial_dwt(tmp, tmp2, w, h, 32, type, dec_count);
00797
00798 s = 0;
00799 av_assert1(w == h);
00800 for (level = 0; level < dec_count; level++)
00801 for (ori = level ? 1 : 0; ori < 4; ori++) {
00802 int size = w >> (dec_count - level);
00803 int sx = (ori & 1) ? size : 0;
00804 int stride = 32 << (dec_count - level);
00805 int sy = (ori & 2) ? stride >> 1 : 0;
00806
00807 for (i = 0; i < size; i++)
00808 for (j = 0; j < size; j++) {
00809 int v = tmp[sx + sy + i * stride + j] *
00810 scale[type][dec_count - 3][level][ori];
00811 s += FFABS(v);
00812 }
00813 }
00814 av_assert1(s >= 0);
00815 return s >> 9;
00816 }
00817
00818 static int w53_8_c(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h)
00819 {
00820 return w_c(v, pix1, pix2, line_size, 8, h, 1);
00821 }
00822
00823 static int w97_8_c(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h)
00824 {
00825 return w_c(v, pix1, pix2, line_size, 8, h, 0);
00826 }
00827
00828 static int w53_16_c(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h)
00829 {
00830 return w_c(v, pix1, pix2, line_size, 16, h, 1);
00831 }
00832
00833 static int w97_16_c(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h)
00834 {
00835 return w_c(v, pix1, pix2, line_size, 16, h, 0);
00836 }
00837
00838 int ff_w53_32_c(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h)
00839 {
00840 return w_c(v, pix1, pix2, line_size, 32, h, 1);
00841 }
00842
00843 int ff_w97_32_c(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h)
00844 {
00845 return w_c(v, pix1, pix2, line_size, 32, h, 0);
00846 }
00847
00848 void ff_dsputil_init_dwt(DSPContext *c)
00849 {
00850 c->w53[0] = w53_16_c;
00851 c->w53[1] = w53_8_c;
00852 c->w97[0] = w97_16_c;
00853 c->w97[1] = w97_8_c;
00854 }
00855
00856 void ff_dwt_init(DWTContext *c)
00857 {
00858 c->vertical_compose97i = ff_snow_vertical_compose97i;
00859 c->horizontal_compose97i = ff_snow_horizontal_compose97i;
00860 c->inner_add_yblock = ff_snow_inner_add_yblock;
00861
00862 if (HAVE_MMX)
00863 ff_dwt_init_x86(c);
00864 }
00865
00866
00867 static av_always_inline
00868 void interleave(IDWTELEM *dst, IDWTELEM *src0, IDWTELEM *src1, int w2, int add, int shift)
00869 {
00870 int i;
00871 for (i = 0; i < w2; i++) {
00872 dst[2*i ] = (src0[i] + add) >> shift;
00873 dst[2*i+1] = (src1[i] + add) >> shift;
00874 }
00875 }
00876
00877 static void horizontal_compose_dirac53i(IDWTELEM *b, IDWTELEM *temp, int w)
00878 {
00879 const int w2 = w >> 1;
00880 int x;
00881
00882 temp[0] = COMPOSE_53iL0(b[w2], b[0], b[w2]);
00883 for (x = 1; x < w2; x++) {
00884 temp[x ] = COMPOSE_53iL0 (b[x+w2-1], b[x ], b[x+w2]);
00885 temp[x+w2-1] = COMPOSE_DIRAC53iH0(temp[x-1], b[x+w2-1], temp[x]);
00886 }
00887 temp[w-1] = COMPOSE_DIRAC53iH0(temp[w2-1], b[w-1], temp[w2-1]);
00888
00889 interleave(b, temp, temp+w2, w2, 1, 1);
00890 }
00891
00892 static void horizontal_compose_dd97i(IDWTELEM *b, IDWTELEM *tmp, int w)
00893 {
00894 const int w2 = w >> 1;
00895 int x;
00896
00897 tmp[0] = COMPOSE_53iL0(b[w2], b[0], b[w2]);
00898 for (x = 1; x < w2; x++)
00899 tmp[x] = COMPOSE_53iL0(b[x+w2-1], b[x], b[x+w2]);
00900
00901
00902 tmp[-1] = tmp[0];
00903 tmp[w2+1] = tmp[w2] = tmp[w2-1];
00904
00905 for (x = 0; x < w2; x++) {
00906 b[2*x ] = (tmp[x] + 1)>>1;
00907 b[2*x+1] = (COMPOSE_DD97iH0(tmp[x-1], tmp[x], b[x+w2], tmp[x+1], tmp[x+2]) + 1)>>1;
00908 }
00909 }
00910
00911 static void horizontal_compose_dd137i(IDWTELEM *b, IDWTELEM *tmp, int w)
00912 {
00913 const int w2 = w >> 1;
00914 int x;
00915
00916 tmp[0] = COMPOSE_DD137iL0(b[w2], b[w2], b[0], b[w2 ], b[w2+1]);
00917 tmp[1] = COMPOSE_DD137iL0(b[w2], b[w2], b[1], b[w2+1], b[w2+2]);
00918 for (x = 2; x < w2-1; x++)
00919 tmp[x] = COMPOSE_DD137iL0(b[x+w2-2], b[x+w2-1], b[x], b[x+w2], b[x+w2+1]);
00920 tmp[w2-1] = COMPOSE_DD137iL0(b[w-3], b[w-2], b[w2-1], b[w-1], b[w-1]);
00921
00922
00923 tmp[-1] = tmp[0];
00924 tmp[w2+1] = tmp[w2] = tmp[w2-1];
00925
00926 for (x = 0; x < w2; x++) {
00927 b[2*x ] = (tmp[x] + 1)>>1;
00928 b[2*x+1] = (COMPOSE_DD97iH0(tmp[x-1], tmp[x], b[x+w2], tmp[x+1], tmp[x+2]) + 1)>>1;
00929 }
00930 }
00931
00932 static av_always_inline
00933 void horizontal_compose_haari(IDWTELEM *b, IDWTELEM *temp, int w, int shift)
00934 {
00935 const int w2 = w >> 1;
00936 int x;
00937
00938 for (x = 0; x < w2; x++) {
00939 temp[x ] = COMPOSE_HAARiL0(b[x ], b[x+w2]);
00940 temp[x+w2] = COMPOSE_HAARiH0(b[x+w2], temp[x]);
00941 }
00942
00943 interleave(b, temp, temp+w2, w2, shift, shift);
00944 }
00945
00946 static void horizontal_compose_haar0i(IDWTELEM *b, IDWTELEM *temp, int w)
00947 {
00948 horizontal_compose_haari(b, temp, w, 0);
00949 }
00950
00951 static void horizontal_compose_haar1i(IDWTELEM *b, IDWTELEM *temp, int w)
00952 {
00953 horizontal_compose_haari(b, temp, w, 1);
00954 }
00955
00956 static void horizontal_compose_fidelityi(IDWTELEM *b, IDWTELEM *tmp, int w)
00957 {
00958 const int w2 = w >> 1;
00959 int i, x;
00960 IDWTELEM v[8];
00961
00962 for (x = 0; x < w2; x++) {
00963 for (i = 0; i < 8; i++)
00964 v[i] = b[av_clip(x-3+i, 0, w2-1)];
00965 tmp[x] = COMPOSE_FIDELITYiH0(v[0], v[1], v[2], v[3], b[x+w2], v[4], v[5], v[6], v[7]);
00966 }
00967
00968 for (x = 0; x < w2; x++) {
00969 for (i = 0; i < 8; i++)
00970 v[i] = tmp[av_clip(x-4+i, 0, w2-1)];
00971 tmp[x+w2] = COMPOSE_FIDELITYiL0(v[0], v[1], v[2], v[3], b[x], v[4], v[5], v[6], v[7]);
00972 }
00973
00974 interleave(b, tmp+w2, tmp, w2, 0, 0);
00975 }
00976
00977 static void horizontal_compose_daub97i(IDWTELEM *b, IDWTELEM *temp, int w)
00978 {
00979 const int w2 = w >> 1;
00980 int x, b0, b1, b2;
00981
00982 temp[0] = COMPOSE_DAUB97iL1(b[w2], b[0], b[w2]);
00983 for (x = 1; x < w2; x++) {
00984 temp[x ] = COMPOSE_DAUB97iL1(b[x+w2-1], b[x ], b[x+w2]);
00985 temp[x+w2-1] = COMPOSE_DAUB97iH1(temp[x-1], b[x+w2-1], temp[x]);
00986 }
00987 temp[w-1] = COMPOSE_DAUB97iH1(temp[w2-1], b[w-1], temp[w2-1]);
00988
00989
00990 b0 = b2 = COMPOSE_DAUB97iL0(temp[w2], temp[0], temp[w2]);
00991 b[0] = (b0 + 1) >> 1;
00992 for (x = 1; x < w2; x++) {
00993 b2 = COMPOSE_DAUB97iL0(temp[x+w2-1], temp[x ], temp[x+w2]);
00994 b1 = COMPOSE_DAUB97iH0( b0, temp[x+w2-1], b2 );
00995 b[2*x-1] = (b1 + 1) >> 1;
00996 b[2*x ] = (b2 + 1) >> 1;
00997 b0 = b2;
00998 }
00999 b[w-1] = (COMPOSE_DAUB97iH0(b2, temp[w-1], b2) + 1) >> 1;
01000 }
01001
01002 static void vertical_compose_dirac53iH0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width)
01003 {
01004 int i;
01005
01006 for(i=0; i<width; i++){
01007 b1[i] = COMPOSE_DIRAC53iH0(b0[i], b1[i], b2[i]);
01008 }
01009 }
01010
01011 static void vertical_compose_dd97iH0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2,
01012 IDWTELEM *b3, IDWTELEM *b4, int width)
01013 {
01014 int i;
01015
01016 for(i=0; i<width; i++){
01017 b2[i] = COMPOSE_DD97iH0(b0[i], b1[i], b2[i], b3[i], b4[i]);
01018 }
01019 }
01020
01021 static void vertical_compose_dd137iL0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2,
01022 IDWTELEM *b3, IDWTELEM *b4, int width)
01023 {
01024 int i;
01025
01026 for(i=0; i<width; i++){
01027 b2[i] = COMPOSE_DD137iL0(b0[i], b1[i], b2[i], b3[i], b4[i]);
01028 }
01029 }
01030
01031 static void vertical_compose_haar(IDWTELEM *b0, IDWTELEM *b1, int width)
01032 {
01033 int i;
01034
01035 for (i = 0; i < width; i++) {
01036 b0[i] = COMPOSE_HAARiL0(b0[i], b1[i]);
01037 b1[i] = COMPOSE_HAARiH0(b1[i], b0[i]);
01038 }
01039 }
01040
01041 static void vertical_compose_fidelityiH0(IDWTELEM *dst, IDWTELEM *b[8], int width)
01042 {
01043 int i;
01044
01045 for(i=0; i<width; i++){
01046 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]);
01047 }
01048 }
01049
01050 static void vertical_compose_fidelityiL0(IDWTELEM *dst, IDWTELEM *b[8], int width)
01051 {
01052 int i;
01053
01054 for(i=0; i<width; i++){
01055 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]);
01056 }
01057 }
01058
01059 static void vertical_compose_daub97iH0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width)
01060 {
01061 int i;
01062
01063 for(i=0; i<width; i++){
01064 b1[i] = COMPOSE_DAUB97iH0(b0[i], b1[i], b2[i]);
01065 }
01066 }
01067
01068 static void vertical_compose_daub97iH1(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width)
01069 {
01070 int i;
01071
01072 for(i=0; i<width; i++){
01073 b1[i] = COMPOSE_DAUB97iH1(b0[i], b1[i], b2[i]);
01074 }
01075 }
01076
01077 static void vertical_compose_daub97iL0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width)
01078 {
01079 int i;
01080
01081 for(i=0; i<width; i++){
01082 b1[i] = COMPOSE_DAUB97iL0(b0[i], b1[i], b2[i]);
01083 }
01084 }
01085
01086 static void vertical_compose_daub97iL1(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width)
01087 {
01088 int i;
01089
01090 for(i=0; i<width; i++){
01091 b1[i] = COMPOSE_DAUB97iL1(b0[i], b1[i], b2[i]);
01092 }
01093 }
01094
01095
01096 static void spatial_compose_dd97i_dy(DWTContext *d, int level, int width, int height, int stride)
01097 {
01098 vertical_compose_3tap vertical_compose_l0 = (void*)d->vertical_compose_l0;
01099 vertical_compose_5tap vertical_compose_h0 = (void*)d->vertical_compose_h0;
01100 DWTCompose *cs = d->cs + level;
01101
01102 int i, y = cs->y;
01103 IDWTELEM *b[8];
01104 for (i = 0; i < 6; i++)
01105 b[i] = cs->b[i];
01106 b[6] = d->buffer + av_clip(y+5, 0, height-2)*stride;
01107 b[7] = d->buffer + av_clip(y+6, 1, height-1)*stride;
01108
01109 if(y+5<(unsigned)height) vertical_compose_l0( b[5], b[6], b[7], width);
01110 if(y+1<(unsigned)height) vertical_compose_h0(b[0], b[2], b[3], b[4], b[6], width);
01111
01112 if(y-1<(unsigned)height) d->horizontal_compose(b[0], d->temp, width);
01113 if(y+0<(unsigned)height) d->horizontal_compose(b[1], d->temp, width);
01114
01115 for (i = 0; i < 6; i++)
01116 cs->b[i] = b[i+2];
01117 cs->y += 2;
01118 }
01119
01120 static void spatial_compose_dirac53i_dy(DWTContext *d, int level, int width, int height, int stride)
01121 {
01122 vertical_compose_3tap vertical_compose_l0 = (void*)d->vertical_compose_l0;
01123 vertical_compose_3tap vertical_compose_h0 = (void*)d->vertical_compose_h0;
01124 DWTCompose *cs = d->cs + level;
01125
01126 int y= cs->y;
01127 IDWTELEM *b[4] = { cs->b[0], cs->b[1] };
01128 b[2] = d->buffer + mirror(y+1, height-1)*stride;
01129 b[3] = d->buffer + mirror(y+2, height-1)*stride;
01130
01131 if(y+1<(unsigned)height) vertical_compose_l0(b[1], b[2], b[3], width);
01132 if(y+0<(unsigned)height) vertical_compose_h0(b[0], b[1], b[2], width);
01133
01134 if(y-1<(unsigned)height) d->horizontal_compose(b[0], d->temp, width);
01135 if(y+0<(unsigned)height) d->horizontal_compose(b[1], d->temp, width);
01136
01137 cs->b[0] = b[2];
01138 cs->b[1] = b[3];
01139 cs->y += 2;
01140 }
01141
01142
01143 static void spatial_compose_dd137i_dy(DWTContext *d, int level, int width, int height, int stride)
01144 {
01145 vertical_compose_5tap vertical_compose_l0 = (void*)d->vertical_compose_l0;
01146 vertical_compose_5tap vertical_compose_h0 = (void*)d->vertical_compose_h0;
01147 DWTCompose *cs = d->cs + level;
01148
01149 int i, y = cs->y;
01150 IDWTELEM *b[10];
01151 for (i = 0; i < 8; i++)
01152 b[i] = cs->b[i];
01153 b[8] = d->buffer + av_clip(y+7, 0, height-2)*stride;
01154 b[9] = d->buffer + av_clip(y+8, 1, height-1)*stride;
01155
01156 if(y+5<(unsigned)height) vertical_compose_l0(b[3], b[5], b[6], b[7], b[9], width);
01157 if(y+1<(unsigned)height) vertical_compose_h0(b[0], b[2], b[3], b[4], b[6], width);
01158
01159 if(y-1<(unsigned)height) d->horizontal_compose(b[0], d->temp, width);
01160 if(y+0<(unsigned)height) d->horizontal_compose(b[1], d->temp, width);
01161
01162 for (i = 0; i < 8; i++)
01163 cs->b[i] = b[i+2];
01164 cs->y += 2;
01165 }
01166
01167
01168 static void spatial_compose_haari_dy(DWTContext *d, int level, int width, int height, int stride)
01169 {
01170 vertical_compose_2tap vertical_compose = (void*)d->vertical_compose;
01171 int y = d->cs[level].y;
01172 IDWTELEM *b0 = d->buffer + (y-1)*stride;
01173 IDWTELEM *b1 = d->buffer + (y )*stride;
01174
01175 vertical_compose(b0, b1, width);
01176 d->horizontal_compose(b0, d->temp, width);
01177 d->horizontal_compose(b1, d->temp, width);
01178
01179 d->cs[level].y += 2;
01180 }
01181
01182
01183
01184 static void spatial_compose_fidelity(DWTContext *d, int level, int width, int height, int stride)
01185 {
01186 vertical_compose_9tap vertical_compose_l0 = (void*)d->vertical_compose_l0;
01187 vertical_compose_9tap vertical_compose_h0 = (void*)d->vertical_compose_h0;
01188 int i, y;
01189 IDWTELEM *b[8];
01190
01191 for (y = 1; y < height; y += 2) {
01192 for (i = 0; i < 8; i++)
01193 b[i] = d->buffer + av_clip((y-7 + 2*i), 0, height-2)*stride;
01194 vertical_compose_h0(d->buffer + y*stride, b, width);
01195 }
01196
01197 for (y = 0; y < height; y += 2) {
01198 for (i = 0; i < 8; i++)
01199 b[i] = d->buffer + av_clip((y-7 + 2*i), 1, height-1)*stride;
01200 vertical_compose_l0(d->buffer + y*stride, b, width);
01201 }
01202
01203 for (y = 0; y < height; y++)
01204 d->horizontal_compose(d->buffer + y*stride, d->temp, width);
01205
01206 d->cs[level].y = height+1;
01207 }
01208
01209 static void spatial_compose_daub97i_dy(DWTContext *d, int level, int width, int height, int stride)
01210 {
01211 vertical_compose_3tap vertical_compose_l0 = (void*)d->vertical_compose_l0;
01212 vertical_compose_3tap vertical_compose_h0 = (void*)d->vertical_compose_h0;
01213 vertical_compose_3tap vertical_compose_l1 = (void*)d->vertical_compose_l1;
01214 vertical_compose_3tap vertical_compose_h1 = (void*)d->vertical_compose_h1;
01215 DWTCompose *cs = d->cs + level;
01216
01217 int i, y = cs->y;
01218 IDWTELEM *b[6];
01219 for (i = 0; i < 4; i++)
01220 b[i] = cs->b[i];
01221 b[4] = d->buffer + mirror(y+3, height-1)*stride;
01222 b[5] = d->buffer + mirror(y+4, height-1)*stride;
01223
01224 if(y+3<(unsigned)height) vertical_compose_l1(b[3], b[4], b[5], width);
01225 if(y+2<(unsigned)height) vertical_compose_h1(b[2], b[3], b[4], width);
01226 if(y+1<(unsigned)height) vertical_compose_l0(b[1], b[2], b[3], width);
01227 if(y+0<(unsigned)height) vertical_compose_h0(b[0], b[1], b[2], width);
01228
01229 if(y-1<(unsigned)height) d->horizontal_compose(b[0], d->temp, width);
01230 if(y+0<(unsigned)height) d->horizontal_compose(b[1], d->temp, width);
01231
01232 for (i = 0; i < 4; i++)
01233 cs->b[i] = b[i+2];
01234 cs->y += 2;
01235 }
01236
01237
01238 static void spatial_compose97i_init2(DWTCompose *cs, IDWTELEM *buffer, int height, int stride)
01239 {
01240 cs->b[0] = buffer + mirror(-3-1, height-1)*stride;
01241 cs->b[1] = buffer + mirror(-3 , height-1)*stride;
01242 cs->b[2] = buffer + mirror(-3+1, height-1)*stride;
01243 cs->b[3] = buffer + mirror(-3+2, height-1)*stride;
01244 cs->y = -3;
01245 }
01246
01247 static void spatial_compose53i_init2(DWTCompose *cs, IDWTELEM *buffer, int height, int stride)
01248 {
01249 cs->b[0] = buffer + mirror(-1-1, height-1)*stride;
01250 cs->b[1] = buffer + mirror(-1 , height-1)*stride;
01251 cs->y = -1;
01252 }
01253
01254 static void spatial_compose_dd97i_init(DWTCompose *cs, IDWTELEM *buffer, int height, int stride)
01255 {
01256 cs->b[0] = buffer + av_clip(-5-1, 0, height-2)*stride;
01257 cs->b[1] = buffer + av_clip(-5 , 1, height-1)*stride;
01258 cs->b[2] = buffer + av_clip(-5+1, 0, height-2)*stride;
01259 cs->b[3] = buffer + av_clip(-5+2, 1, height-1)*stride;
01260 cs->b[4] = buffer + av_clip(-5+3, 0, height-2)*stride;
01261 cs->b[5] = buffer + av_clip(-5+4, 1, height-1)*stride;
01262 cs->y = -5;
01263 }
01264
01265 static void spatial_compose_dd137i_init(DWTCompose *cs, IDWTELEM *buffer, int height, int stride)
01266 {
01267 cs->b[0] = buffer + av_clip(-5-1, 0, height-2)*stride;
01268 cs->b[1] = buffer + av_clip(-5 , 1, height-1)*stride;
01269 cs->b[2] = buffer + av_clip(-5+1, 0, height-2)*stride;
01270 cs->b[3] = buffer + av_clip(-5+2, 1, height-1)*stride;
01271 cs->b[4] = buffer + av_clip(-5+3, 0, height-2)*stride;
01272 cs->b[5] = buffer + av_clip(-5+4, 1, height-1)*stride;
01273 cs->b[6] = buffer + av_clip(-5+5, 0, height-2)*stride;
01274 cs->b[7] = buffer + av_clip(-5+6, 1, height-1)*stride;
01275 cs->y = -5;
01276 }
01277
01278 int ff_spatial_idwt_init2(DWTContext *d, IDWTELEM *buffer, int width, int height,
01279 int stride, enum dwt_type type, int decomposition_count,
01280 IDWTELEM *temp)
01281 {
01282 int level;
01283
01284 d->buffer = buffer;
01285 d->width = width;
01286 d->height = height;
01287 d->stride = stride;
01288 d->decomposition_count = decomposition_count;
01289 d->temp = temp + 8;
01290
01291 for(level=decomposition_count-1; level>=0; level--){
01292 int hl = height >> level;
01293 int stride_l = stride << level;
01294
01295 switch(type){
01296 case DWT_DIRAC_DD9_7:
01297 spatial_compose_dd97i_init(d->cs+level, buffer, hl, stride_l);
01298 break;
01299 case DWT_DIRAC_LEGALL5_3:
01300 spatial_compose53i_init2(d->cs+level, buffer, hl, stride_l);
01301 break;
01302 case DWT_DIRAC_DD13_7:
01303 spatial_compose_dd137i_init(d->cs+level, buffer, hl, stride_l);
01304 break;
01305 case DWT_DIRAC_HAAR0:
01306 case DWT_DIRAC_HAAR1:
01307 d->cs[level].y = 1;
01308 break;
01309 case DWT_DIRAC_DAUB9_7:
01310 spatial_compose97i_init2(d->cs+level, buffer, hl, stride_l);
01311 break;
01312 default:
01313 d->cs[level].y = 0;
01314 break;
01315 }
01316 }
01317
01318 switch (type) {
01319 case DWT_DIRAC_DD9_7:
01320 d->spatial_compose = spatial_compose_dd97i_dy;
01321 d->vertical_compose_l0 = (void*)vertical_compose53iL0;
01322 d->vertical_compose_h0 = (void*)vertical_compose_dd97iH0;
01323 d->horizontal_compose = horizontal_compose_dd97i;
01324 d->support = 7;
01325 break;
01326 case DWT_DIRAC_LEGALL5_3:
01327 d->spatial_compose = spatial_compose_dirac53i_dy;
01328 d->vertical_compose_l0 = (void*)vertical_compose53iL0;
01329 d->vertical_compose_h0 = (void*)vertical_compose_dirac53iH0;
01330 d->horizontal_compose = horizontal_compose_dirac53i;
01331 d->support = 3;
01332 break;
01333 case DWT_DIRAC_DD13_7:
01334 d->spatial_compose = spatial_compose_dd137i_dy;
01335 d->vertical_compose_l0 = (void*)vertical_compose_dd137iL0;
01336 d->vertical_compose_h0 = (void*)vertical_compose_dd97iH0;
01337 d->horizontal_compose = horizontal_compose_dd137i;
01338 d->support = 7;
01339 break;
01340 case DWT_DIRAC_HAAR0:
01341 case DWT_DIRAC_HAAR1:
01342 d->spatial_compose = spatial_compose_haari_dy;
01343 d->vertical_compose = (void*)vertical_compose_haar;
01344 if (type == DWT_DIRAC_HAAR0)
01345 d->horizontal_compose = horizontal_compose_haar0i;
01346 else
01347 d->horizontal_compose = horizontal_compose_haar1i;
01348 d->support = 1;
01349 break;
01350 case DWT_DIRAC_FIDELITY:
01351 d->spatial_compose = spatial_compose_fidelity;
01352 d->vertical_compose_l0 = (void*)vertical_compose_fidelityiL0;
01353 d->vertical_compose_h0 = (void*)vertical_compose_fidelityiH0;
01354 d->horizontal_compose = horizontal_compose_fidelityi;
01355 break;
01356 case DWT_DIRAC_DAUB9_7:
01357 d->spatial_compose = spatial_compose_daub97i_dy;
01358 d->vertical_compose_l0 = (void*)vertical_compose_daub97iL0;
01359 d->vertical_compose_h0 = (void*)vertical_compose_daub97iH0;
01360 d->vertical_compose_l1 = (void*)vertical_compose_daub97iL1;
01361 d->vertical_compose_h1 = (void*)vertical_compose_daub97iH1;
01362 d->horizontal_compose = horizontal_compose_daub97i;
01363 d->support = 5;
01364 break;
01365 default:
01366 av_log(NULL, AV_LOG_ERROR, "Unknown wavelet type %d\n", type);
01367 return -1;
01368 }
01369
01370 if (HAVE_MMX) ff_spatial_idwt_init_mmx(d, type);
01371
01372 return 0;
01373 }
01374
01375 void ff_spatial_idwt_slice2(DWTContext *d, int y)
01376 {
01377 int level, support = d->support;
01378
01379 for (level = d->decomposition_count-1; level >= 0; level--) {
01380 int wl = d->width >> level;
01381 int hl = d->height >> level;
01382 int stride_l = d->stride << level;
01383
01384 while (d->cs[level].y <= FFMIN((y>>level)+support, hl))
01385 d->spatial_compose(d, level, wl, hl, stride_l);
01386 }
01387 }
01388
01389 int ff_spatial_idwt2(IDWTELEM *buffer, int width, int height, int stride,
01390 enum dwt_type type, int decomposition_count, IDWTELEM *temp)
01391 {
01392 DWTContext d;
01393 int y;
01394
01395 if (ff_spatial_idwt_init2(&d, buffer, width, height, stride, type, decomposition_count, temp))
01396 return -1;
01397
01398 for (y = 0; y < d.height; y += 4)
01399 ff_spatial_idwt_slice2(&d, y);
01400
01401 return 0;
01402 }