00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "libavutil/attributes.h"
00022 #include "dsputil.h"
00023 #include "dwt.h"
00024
00025 void ff_slice_buffer_init(slice_buffer * buf, int line_count, int max_allocated_lines, int line_width, IDWTELEM * base_buffer)
00026 {
00027 int i;
00028
00029 buf->base_buffer = base_buffer;
00030 buf->line_count = line_count;
00031 buf->line_width = line_width;
00032 buf->data_count = max_allocated_lines;
00033 buf->line = av_mallocz (sizeof(IDWTELEM *) * line_count);
00034 buf->data_stack = av_malloc (sizeof(IDWTELEM *) * max_allocated_lines);
00035
00036 for(i = 0; i < max_allocated_lines; i++){
00037 buf->data_stack[i] = av_malloc (sizeof(IDWTELEM) * line_width);
00038 }
00039
00040 buf->data_stack_top = max_allocated_lines - 1;
00041 }
00042
00043 IDWTELEM * ff_slice_buffer_load_line(slice_buffer * buf, int line)
00044 {
00045 IDWTELEM * buffer;
00046
00047 assert(buf->data_stack_top >= 0);
00048
00049 if (buf->line[line])
00050 return buf->line[line];
00051
00052 buffer = buf->data_stack[buf->data_stack_top];
00053 buf->data_stack_top--;
00054 buf->line[line] = buffer;
00055
00056 return buffer;
00057 }
00058
00059 void ff_slice_buffer_release(slice_buffer * buf, int line)
00060 {
00061 IDWTELEM * buffer;
00062
00063 assert(line >= 0 && line < buf->line_count);
00064 assert(buf->line[line]);
00065
00066 buffer = buf->line[line];
00067 buf->data_stack_top++;
00068 buf->data_stack[buf->data_stack_top] = buffer;
00069 buf->line[line] = NULL;
00070 }
00071
00072 void ff_slice_buffer_flush(slice_buffer * buf)
00073 {
00074 int i;
00075 for(i = 0; i < buf->line_count; i++){
00076 if (buf->line[i])
00077 ff_slice_buffer_release(buf, i);
00078 }
00079 }
00080
00081 void ff_slice_buffer_destroy(slice_buffer * buf)
00082 {
00083 int i;
00084 ff_slice_buffer_flush(buf);
00085
00086 for(i = buf->data_count - 1; i >= 0; i--){
00087 av_freep(&buf->data_stack[i]);
00088 }
00089 av_freep(&buf->data_stack);
00090 av_freep(&buf->line);
00091 }
00092
00093 static inline int mirror(int v, int m){
00094 while((unsigned)v > (unsigned)m){
00095 v=-v;
00096 if(v<0) v+= 2*m;
00097 }
00098 return v;
00099 }
00100
00101 static av_always_inline void
00102 lift(DWTELEM *dst, DWTELEM *src, DWTELEM *ref,
00103 int dst_step, int src_step, int ref_step,
00104 int width, int mul, int add, int shift,
00105 int highpass, int inverse){
00106 const int mirror_left= !highpass;
00107 const int mirror_right= (width&1) ^ highpass;
00108 const int w= (width>>1) - 1 + (highpass & width);
00109 int i;
00110
00111 #define LIFT(src, ref, inv) ((src) + ((inv) ? - (ref) : + (ref)))
00112 if(mirror_left){
00113 dst[0] = LIFT(src[0], ((mul*2*ref[0]+add)>>shift), inverse);
00114 dst += dst_step;
00115 src += src_step;
00116 }
00117
00118 for(i=0; i<w; i++){
00119 dst[i*dst_step] =
00120 LIFT(src[i*src_step],
00121 ((mul*(ref[i*ref_step] + ref[(i+1)*ref_step])+add)>>shift),
00122 inverse);
00123 }
00124
00125 if(mirror_right){
00126 dst[w*dst_step] =
00127 LIFT(src[w*src_step],
00128 ((mul*2*ref[w*ref_step]+add)>>shift),
00129 inverse);
00130 }
00131 }
00132
00133 static av_always_inline void
00134 inv_lift(IDWTELEM *dst, IDWTELEM *src, IDWTELEM *ref,
00135 int dst_step, int src_step, int ref_step,
00136 int width, int mul, int add, int shift,
00137 int highpass, int inverse){
00138 const int mirror_left= !highpass;
00139 const int mirror_right= (width&1) ^ highpass;
00140 const int w= (width>>1) - 1 + (highpass & width);
00141 int i;
00142
00143 #define LIFT(src, ref, inv) ((src) + ((inv) ? - (ref) : + (ref)))
00144 if(mirror_left){
00145 dst[0] = LIFT(src[0], ((mul*2*ref[0]+add)>>shift), inverse);
00146 dst += dst_step;
00147 src += src_step;
00148 }
00149
00150 for(i=0; i<w; i++){
00151 dst[i*dst_step] =
00152 LIFT(src[i*src_step],
00153 ((mul*(ref[i*ref_step] + ref[(i+1)*ref_step])+add)>>shift),
00154 inverse);
00155 }
00156
00157 if(mirror_right){
00158 dst[w*dst_step] =
00159 LIFT(src[w*src_step],
00160 ((mul*2*ref[w*ref_step]+add)>>shift),
00161 inverse);
00162 }
00163 }
00164
00165 #ifndef liftS
00166 static av_always_inline void
00167 liftS(DWTELEM *dst, DWTELEM *src, DWTELEM *ref,
00168 int dst_step, int src_step, int ref_step,
00169 int width, int mul, int add, int shift,
00170 int highpass, int inverse){
00171 const int mirror_left= !highpass;
00172 const int mirror_right= (width&1) ^ highpass;
00173 const int w= (width>>1) - 1 + (highpass & width);
00174 int i;
00175
00176 assert(shift == 4);
00177 #define LIFTS(src, ref, inv) \
00178 ((inv) ? \
00179 (src) + (((ref) + 4*(src))>>shift): \
00180 -((-16*(src) + (ref) + add/4 + 1 + (5<<25))/(5*4) - (1<<23)))
00181 if(mirror_left){
00182 dst[0] = LIFTS(src[0], mul*2*ref[0]+add, inverse);
00183 dst += dst_step;
00184 src += src_step;
00185 }
00186
00187 for(i=0; i<w; i++){
00188 dst[i*dst_step] =
00189 LIFTS(src[i*src_step],
00190 mul*(ref[i*ref_step] + ref[(i+1)*ref_step])+add,
00191 inverse);
00192 }
00193
00194 if(mirror_right){
00195 dst[w*dst_step] =
00196 LIFTS(src[w*src_step], mul*2*ref[w*ref_step]+add, inverse);
00197 }
00198 }
00199 static av_always_inline void
00200 inv_liftS(IDWTELEM *dst, IDWTELEM *src, IDWTELEM *ref,
00201 int dst_step, int src_step, int ref_step,
00202 int width, int mul, int add, int shift,
00203 int highpass, int inverse){
00204 const int mirror_left= !highpass;
00205 const int mirror_right= (width&1) ^ highpass;
00206 const int w= (width>>1) - 1 + (highpass & width);
00207 int i;
00208
00209 assert(shift == 4);
00210 #define LIFTS(src, ref, inv) \
00211 ((inv) ? \
00212 (src) + (((ref) + 4*(src))>>shift): \
00213 -((-16*(src) + (ref) + add/4 + 1 + (5<<25))/(5*4) - (1<<23)))
00214 if(mirror_left){
00215 dst[0] = LIFTS(src[0], mul*2*ref[0]+add, inverse);
00216 dst += dst_step;
00217 src += src_step;
00218 }
00219
00220 for(i=0; i<w; i++){
00221 dst[i*dst_step] =
00222 LIFTS(src[i*src_step],
00223 mul*(ref[i*ref_step] + ref[(i+1)*ref_step])+add,
00224 inverse);
00225 }
00226
00227 if(mirror_right){
00228 dst[w*dst_step] =
00229 LIFTS(src[w*src_step], mul*2*ref[w*ref_step]+add, inverse);
00230 }
00231 }
00232 #endif
00233
00234 static void horizontal_decompose53i(DWTELEM *b, int width){
00235 DWTELEM temp[width];
00236 const int width2= width>>1;
00237 int x;
00238 const int w2= (width+1)>>1;
00239
00240 for(x=0; x<width2; x++){
00241 temp[x ]= b[2*x ];
00242 temp[x+w2]= b[2*x + 1];
00243 }
00244 if(width&1)
00245 temp[x ]= b[2*x ];
00246 #if 0
00247 {
00248 int A1,A2,A3,A4;
00249 A2= temp[1 ];
00250 A4= temp[0 ];
00251 A1= temp[0+width2];
00252 A1 -= (A2 + A4)>>1;
00253 A4 += (A1 + 1)>>1;
00254 b[0+width2] = A1;
00255 b[0 ] = A4;
00256 for(x=1; x+1<width2; x+=2){
00257 A3= temp[x+width2];
00258 A4= temp[x+1 ];
00259 A3 -= (A2 + A4)>>1;
00260 A2 += (A1 + A3 + 2)>>2;
00261 b[x+width2] = A3;
00262 b[x ] = A2;
00263
00264 A1= temp[x+1+width2];
00265 A2= temp[x+2 ];
00266 A1 -= (A2 + A4)>>1;
00267 A4 += (A1 + A3 + 2)>>2;
00268 b[x+1+width2] = A1;
00269 b[x+1 ] = A4;
00270 }
00271 A3= temp[width-1];
00272 A3 -= A2;
00273 A2 += (A1 + A3 + 2)>>2;
00274 b[width -1] = A3;
00275 b[width2-1] = A2;
00276 }
00277 #else
00278 lift(b+w2, temp+w2, temp, 1, 1, 1, width, -1, 0, 1, 1, 0);
00279 lift(b , temp , b+w2, 1, 1, 1, width, 1, 2, 2, 0, 0);
00280 #endif
00281 }
00282
00283 static void vertical_decompose53iH0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
00284 int i;
00285
00286 for(i=0; i<width; i++){
00287 b1[i] -= (b0[i] + b2[i])>>1;
00288 }
00289 }
00290
00291 static void vertical_decompose53iL0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
00292 int i;
00293
00294 for(i=0; i<width; i++){
00295 b1[i] += (b0[i] + b2[i] + 2)>>2;
00296 }
00297 }
00298
00299 static void spatial_decompose53i(DWTELEM *buffer, int width, int height, int stride){
00300 int y;
00301 DWTELEM *b0= buffer + mirror(-2-1, height-1)*stride;
00302 DWTELEM *b1= buffer + mirror(-2 , height-1)*stride;
00303
00304 for(y=-2; y<height; y+=2){
00305 DWTELEM *b2= buffer + mirror(y+1, height-1)*stride;
00306 DWTELEM *b3= buffer + mirror(y+2, height-1)*stride;
00307
00308 if(y+1<(unsigned)height) horizontal_decompose53i(b2, width);
00309 if(y+2<(unsigned)height) horizontal_decompose53i(b3, width);
00310
00311 if(y+1<(unsigned)height) vertical_decompose53iH0(b1, b2, b3, width);
00312 if(y+0<(unsigned)height) vertical_decompose53iL0(b0, b1, b2, width);
00313
00314 b0=b2;
00315 b1=b3;
00316 }
00317 }
00318
00319 static void horizontal_decompose97i(DWTELEM *b, int width){
00320 DWTELEM temp[width];
00321 const int w2= (width+1)>>1;
00322
00323 lift (temp+w2, b +1, b , 1, 2, 2, width, W_AM, W_AO, W_AS, 1, 1);
00324 liftS(temp , b , temp+w2, 1, 2, 1, width, W_BM, W_BO, W_BS, 0, 0);
00325 lift (b +w2, temp+w2, temp , 1, 1, 1, width, W_CM, W_CO, W_CS, 1, 0);
00326 lift (b , temp , b +w2, 1, 1, 1, width, W_DM, W_DO, W_DS, 0, 0);
00327 }
00328
00329
00330 static void vertical_decompose97iH0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
00331 int i;
00332
00333 for(i=0; i<width; i++){
00334 b1[i] -= (W_AM*(b0[i] + b2[i])+W_AO)>>W_AS;
00335 }
00336 }
00337
00338 static void vertical_decompose97iH1(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
00339 int i;
00340
00341 for(i=0; i<width; i++){
00342 b1[i] += (W_CM*(b0[i] + b2[i])+W_CO)>>W_CS;
00343 }
00344 }
00345
00346 static void vertical_decompose97iL0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
00347 int i;
00348
00349 for(i=0; i<width; i++){
00350 #ifdef liftS
00351 b1[i] -= (W_BM*(b0[i] + b2[i])+W_BO)>>W_BS;
00352 #else
00353 b1[i] = (16*4*b1[i] - 4*(b0[i] + b2[i]) + W_BO*5 + (5<<27)) / (5*16) - (1<<23);
00354 #endif
00355 }
00356 }
00357
00358 static void vertical_decompose97iL1(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
00359 int i;
00360
00361 for(i=0; i<width; i++){
00362 b1[i] += (W_DM*(b0[i] + b2[i])+W_DO)>>W_DS;
00363 }
00364 }
00365
00366 static void spatial_decompose97i(DWTELEM *buffer, int width, int height, int stride){
00367 int y;
00368 DWTELEM *b0= buffer + mirror(-4-1, height-1)*stride;
00369 DWTELEM *b1= buffer + mirror(-4 , height-1)*stride;
00370 DWTELEM *b2= buffer + mirror(-4+1, height-1)*stride;
00371 DWTELEM *b3= buffer + mirror(-4+2, height-1)*stride;
00372
00373 for(y=-4; y<height; y+=2){
00374 DWTELEM *b4= buffer + mirror(y+3, height-1)*stride;
00375 DWTELEM *b5= buffer + mirror(y+4, height-1)*stride;
00376
00377 if(y+3<(unsigned)height) horizontal_decompose97i(b4, width);
00378 if(y+4<(unsigned)height) horizontal_decompose97i(b5, width);
00379
00380 if(y+3<(unsigned)height) vertical_decompose97iH0(b3, b4, b5, width);
00381 if(y+2<(unsigned)height) vertical_decompose97iL0(b2, b3, b4, width);
00382 if(y+1<(unsigned)height) vertical_decompose97iH1(b1, b2, b3, width);
00383 if(y+0<(unsigned)height) vertical_decompose97iL1(b0, b1, b2, width);
00384
00385 b0=b2;
00386 b1=b3;
00387 b2=b4;
00388 b3=b5;
00389 }
00390 }
00391
00392 void ff_spatial_dwt(DWTELEM *buffer, int width, int height, int stride, int type, int decomposition_count){
00393 int level;
00394
00395 for(level=0; level<decomposition_count; level++){
00396 switch(type){
00397 case DWT_97: spatial_decompose97i(buffer, width>>level, height>>level, stride<<level); break;
00398 case DWT_53: spatial_decompose53i(buffer, width>>level, height>>level, stride<<level); break;
00399 }
00400 }
00401 }
00402
00403 static void horizontal_compose53i(IDWTELEM *b, int width){
00404 IDWTELEM temp[width];
00405 const int width2= width>>1;
00406 const int w2= (width+1)>>1;
00407 int x;
00408
00409 for(x=0; x<width2; x++){
00410 temp[2*x ]= b[x ];
00411 temp[2*x + 1]= b[x+w2];
00412 }
00413 if(width&1)
00414 temp[2*x ]= b[x ];
00415
00416 b[0] = temp[0] - ((temp[1]+1)>>1);
00417 for(x=2; x<width-1; x+=2){
00418 b[x ] = temp[x ] - ((temp[x-1] + temp[x+1]+2)>>2);
00419 b[x-1] = temp[x-1] + ((b [x-2] + b [x ]+1)>>1);
00420 }
00421 if(width&1){
00422 b[x ] = temp[x ] - ((temp[x-1]+1)>>1);
00423 b[x-1] = temp[x-1] + ((b [x-2] + b [x ]+1)>>1);
00424 }else
00425 b[x-1] = temp[x-1] + b[x-2];
00426 }
00427
00428 static void vertical_compose53iH0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width){
00429 int i;
00430
00431 for(i=0; i<width; i++){
00432 b1[i] += (b0[i] + b2[i])>>1;
00433 }
00434 }
00435
00436 static void vertical_compose53iL0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width){
00437 int i;
00438
00439 for(i=0; i<width; i++){
00440 b1[i] -= (b0[i] + b2[i] + 2)>>2;
00441 }
00442 }
00443
00444 static void spatial_compose53i_buffered_init(DWTCompose *cs, slice_buffer * sb, int height, int stride_line){
00445 cs->b0 = slice_buffer_get_line(sb, mirror(-1-1, height-1) * stride_line);
00446 cs->b1 = slice_buffer_get_line(sb, mirror(-1 , height-1) * stride_line);
00447 cs->y = -1;
00448 }
00449
00450 static void spatial_compose53i_init(DWTCompose *cs, IDWTELEM *buffer, int height, int stride){
00451 cs->b0 = buffer + mirror(-1-1, height-1)*stride;
00452 cs->b1 = buffer + mirror(-1 , height-1)*stride;
00453 cs->y = -1;
00454 }
00455
00456 static void spatial_compose53i_dy_buffered(DWTCompose *cs, slice_buffer * sb, int width, int height, int stride_line){
00457 int y= cs->y;
00458
00459 IDWTELEM *b0= cs->b0;
00460 IDWTELEM *b1= cs->b1;
00461 IDWTELEM *b2= slice_buffer_get_line(sb, mirror(y+1, height-1) * stride_line);
00462 IDWTELEM *b3= slice_buffer_get_line(sb, mirror(y+2, height-1) * stride_line);
00463
00464 if(y+1<(unsigned)height && y<(unsigned)height){
00465 int x;
00466
00467 for(x=0; x<width; x++){
00468 b2[x] -= (b1[x] + b3[x] + 2)>>2;
00469 b1[x] += (b0[x] + b2[x])>>1;
00470 }
00471 }else{
00472 if(y+1<(unsigned)height) vertical_compose53iL0(b1, b2, b3, width);
00473 if(y+0<(unsigned)height) vertical_compose53iH0(b0, b1, b2, width);
00474 }
00475
00476 if(y-1<(unsigned)height) horizontal_compose53i(b0, width);
00477 if(y+0<(unsigned)height) horizontal_compose53i(b1, width);
00478
00479 cs->b0 = b2;
00480 cs->b1 = b3;
00481 cs->y += 2;
00482 }
00483
00484 static void spatial_compose53i_dy(DWTCompose *cs, IDWTELEM *buffer, int width, int height, int stride){
00485 int y= cs->y;
00486 IDWTELEM *b0= cs->b0;
00487 IDWTELEM *b1= cs->b1;
00488 IDWTELEM *b2= buffer + mirror(y+1, height-1)*stride;
00489 IDWTELEM *b3= buffer + mirror(y+2, height-1)*stride;
00490
00491 if(y+1<(unsigned)height) vertical_compose53iL0(b1, b2, b3, width);
00492 if(y+0<(unsigned)height) vertical_compose53iH0(b0, b1, b2, width);
00493
00494 if(y-1<(unsigned)height) horizontal_compose53i(b0, width);
00495 if(y+0<(unsigned)height) horizontal_compose53i(b1, width);
00496
00497 cs->b0 = b2;
00498 cs->b1 = b3;
00499 cs->y += 2;
00500 }
00501
00502 static void av_unused spatial_compose53i(IDWTELEM *buffer, int width, int height, int stride){
00503 DWTCompose cs;
00504 spatial_compose53i_init(&cs, buffer, height, stride);
00505 while(cs.y <= height)
00506 spatial_compose53i_dy(&cs, buffer, width, height, stride);
00507 }
00508
00509
00510 void ff_snow_horizontal_compose97i(IDWTELEM *b, int width){
00511 IDWTELEM temp[width];
00512 const int w2= (width+1)>>1;
00513
00514 #if 0 //maybe more understadable but slower
00515 inv_lift (temp , b , b +w2, 2, 1, 1, width, W_DM, W_DO, W_DS, 0, 1);
00516 inv_lift (temp+1 , b +w2, temp , 2, 1, 2, width, W_CM, W_CO, W_CS, 1, 1);
00517
00518 inv_liftS(b , temp , temp+1 , 2, 2, 2, width, W_BM, W_BO, W_BS, 0, 1);
00519 inv_lift (b+1 , temp+1 , b , 2, 2, 2, width, W_AM, W_AO, W_AS, 1, 0);
00520 #else
00521 int x;
00522 temp[0] = b[0] - ((3*b[w2]+2)>>2);
00523 for(x=1; x<(width>>1); x++){
00524 temp[2*x ] = b[x ] - ((3*(b [x+w2-1] + b[x+w2])+4)>>3);
00525 temp[2*x-1] = b[x+w2-1] - temp[2*x-2] - temp[2*x];
00526 }
00527 if(width&1){
00528 temp[2*x ] = b[x ] - ((3*b [x+w2-1]+2)>>2);
00529 temp[2*x-1] = b[x+w2-1] - temp[2*x-2] - temp[2*x];
00530 }else
00531 temp[2*x-1] = b[x+w2-1] - 2*temp[2*x-2];
00532
00533 b[0] = temp[0] + ((2*temp[0] + temp[1]+4)>>3);
00534 for(x=2; x<width-1; x+=2){
00535 b[x ] = temp[x ] + ((4*temp[x ] + temp[x-1] + temp[x+1]+8)>>4);
00536 b[x-1] = temp[x-1] + ((3*(b [x-2] + b [x ] ))>>1);
00537 }
00538 if(width&1){
00539 b[x ] = temp[x ] + ((2*temp[x ] + temp[x-1]+4)>>3);
00540 b[x-1] = temp[x-1] + ((3*(b [x-2] + b [x ] ))>>1);
00541 }else
00542 b[x-1] = temp[x-1] + 3*b [x-2];
00543 #endif
00544 }
00545
00546 static void vertical_compose97iH0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width){
00547 int i;
00548
00549 for(i=0; i<width; i++){
00550 b1[i] += (W_AM*(b0[i] + b2[i])+W_AO)>>W_AS;
00551 }
00552 }
00553
00554 static void vertical_compose97iH1(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width){
00555 int i;
00556
00557 for(i=0; i<width; i++){
00558 b1[i] -= (W_CM*(b0[i] + b2[i])+W_CO)>>W_CS;
00559 }
00560 }
00561
00562 static void vertical_compose97iL0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width){
00563 int i;
00564
00565 for(i=0; i<width; i++){
00566 #ifdef liftS
00567 b1[i] += (W_BM*(b0[i] + b2[i])+W_BO)>>W_BS;
00568 #else
00569 b1[i] += (W_BM*(b0[i] + b2[i])+4*b1[i]+W_BO)>>W_BS;
00570 #endif
00571 }
00572 }
00573
00574 static void vertical_compose97iL1(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width){
00575 int i;
00576
00577 for(i=0; i<width; i++){
00578 b1[i] -= (W_DM*(b0[i] + b2[i])+W_DO)>>W_DS;
00579 }
00580 }
00581
00582 void ff_snow_vertical_compose97i(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, IDWTELEM *b3, IDWTELEM *b4, IDWTELEM *b5, int width){
00583 int i;
00584
00585 for(i=0; i<width; i++){
00586 b4[i] -= (W_DM*(b3[i] + b5[i])+W_DO)>>W_DS;
00587 b3[i] -= (W_CM*(b2[i] + b4[i])+W_CO)>>W_CS;
00588 #ifdef liftS
00589 b2[i] += (W_BM*(b1[i] + b3[i])+W_BO)>>W_BS;
00590 #else
00591 b2[i] += (W_BM*(b1[i] + b3[i])+4*b2[i]+W_BO)>>W_BS;
00592 #endif
00593 b1[i] += (W_AM*(b0[i] + b2[i])+W_AO)>>W_AS;
00594 }
00595 }
00596
00597 static void spatial_compose97i_buffered_init(DWTCompose *cs, slice_buffer * sb, int height, int stride_line){
00598 cs->b0 = slice_buffer_get_line(sb, mirror(-3-1, height-1) * stride_line);
00599 cs->b1 = slice_buffer_get_line(sb, mirror(-3 , height-1) * stride_line);
00600 cs->b2 = slice_buffer_get_line(sb, mirror(-3+1, height-1) * stride_line);
00601 cs->b3 = slice_buffer_get_line(sb, mirror(-3+2, height-1) * stride_line);
00602 cs->y = -3;
00603 }
00604
00605 static void spatial_compose97i_init(DWTCompose *cs, IDWTELEM *buffer, int height, int stride){
00606 cs->b0 = buffer + mirror(-3-1, height-1)*stride;
00607 cs->b1 = buffer + mirror(-3 , height-1)*stride;
00608 cs->b2 = buffer + mirror(-3+1, height-1)*stride;
00609 cs->b3 = buffer + mirror(-3+2, height-1)*stride;
00610 cs->y = -3;
00611 }
00612
00613 static void spatial_compose97i_dy_buffered(DWTContext *dsp, DWTCompose *cs, slice_buffer * sb, int width, int height, int stride_line){
00614 int y = cs->y;
00615
00616 IDWTELEM *b0= cs->b0;
00617 IDWTELEM *b1= cs->b1;
00618 IDWTELEM *b2= cs->b2;
00619 IDWTELEM *b3= cs->b3;
00620 IDWTELEM *b4= slice_buffer_get_line(sb, mirror(y + 3, height - 1) * stride_line);
00621 IDWTELEM *b5= slice_buffer_get_line(sb, mirror(y + 4, height - 1) * stride_line);
00622
00623 if(y>0 && y+4<height){
00624 dsp->vertical_compose97i(b0, b1, b2, b3, b4, b5, width);
00625 }else{
00626 if(y+3<(unsigned)height) vertical_compose97iL1(b3, b4, b5, width);
00627 if(y+2<(unsigned)height) vertical_compose97iH1(b2, b3, b4, width);
00628 if(y+1<(unsigned)height) vertical_compose97iL0(b1, b2, b3, width);
00629 if(y+0<(unsigned)height) vertical_compose97iH0(b0, b1, b2, width);
00630 }
00631
00632 if(y-1<(unsigned)height) dsp->horizontal_compose97i(b0, width);
00633 if(y+0<(unsigned)height) dsp->horizontal_compose97i(b1, width);
00634
00635 cs->b0=b2;
00636 cs->b1=b3;
00637 cs->b2=b4;
00638 cs->b3=b5;
00639 cs->y += 2;
00640 }
00641
00642 static void spatial_compose97i_dy(DWTCompose *cs, IDWTELEM *buffer, int width, int height, int stride){
00643 int y = cs->y;
00644 IDWTELEM *b0= cs->b0;
00645 IDWTELEM *b1= cs->b1;
00646 IDWTELEM *b2= cs->b2;
00647 IDWTELEM *b3= cs->b3;
00648 IDWTELEM *b4= buffer + mirror(y+3, height-1)*stride;
00649 IDWTELEM *b5= buffer + mirror(y+4, height-1)*stride;
00650
00651 if(y+3<(unsigned)height) vertical_compose97iL1(b3, b4, b5, width);
00652 if(y+2<(unsigned)height) vertical_compose97iH1(b2, b3, b4, width);
00653 if(y+1<(unsigned)height) vertical_compose97iL0(b1, b2, b3, width);
00654 if(y+0<(unsigned)height) vertical_compose97iH0(b0, b1, b2, width);
00655
00656 if(y-1<(unsigned)height) ff_snow_horizontal_compose97i(b0, width);
00657 if(y+0<(unsigned)height) ff_snow_horizontal_compose97i(b1, width);
00658
00659 cs->b0=b2;
00660 cs->b1=b3;
00661 cs->b2=b4;
00662 cs->b3=b5;
00663 cs->y += 2;
00664 }
00665
00666 static void av_unused spatial_compose97i(IDWTELEM *buffer, int width, int height, int stride){
00667 DWTCompose cs;
00668 spatial_compose97i_init(&cs, buffer, height, stride);
00669 while(cs.y <= height)
00670 spatial_compose97i_dy(&cs, buffer, width, height, stride);
00671 }
00672
00673 void ff_spatial_idwt_buffered_init(DWTCompose *cs, slice_buffer * sb, int width, int height, int stride_line, int type, int decomposition_count){
00674 int level;
00675 for(level=decomposition_count-1; level>=0; level--){
00676 switch(type){
00677 case DWT_97: spatial_compose97i_buffered_init(cs+level, sb, height>>level, stride_line<<level); break;
00678 case DWT_53: spatial_compose53i_buffered_init(cs+level, sb, height>>level, stride_line<<level); break;
00679 }
00680 }
00681 }
00682
00683 void ff_spatial_idwt_buffered_slice(DWTContext *dsp, DWTCompose *cs, slice_buffer * slice_buf, int width, int height, int stride_line, int type, int decomposition_count, int y){
00684 const int support = type==1 ? 3 : 5;
00685 int level;
00686 if(type==2) return;
00687
00688 for(level=decomposition_count-1; level>=0; level--){
00689 while(cs[level].y <= FFMIN((y>>level)+support, height>>level)){
00690 switch(type){
00691 case DWT_97: spatial_compose97i_dy_buffered(dsp, cs+level, slice_buf, width>>level, height>>level, stride_line<<level);
00692 break;
00693 case DWT_53: spatial_compose53i_dy_buffered(cs+level, slice_buf, width>>level, height>>level, stride_line<<level);
00694 break;
00695 }
00696 }
00697 }
00698 }
00699
00700 void ff_spatial_idwt_init(DWTCompose *cs, IDWTELEM *buffer, int width, int height, int stride, int type, int decomposition_count){
00701 int level;
00702 for(level=decomposition_count-1; level>=0; level--){
00703 switch(type){
00704 case DWT_97: spatial_compose97i_init(cs+level, buffer, height>>level, stride<<level); break;
00705 case DWT_53: spatial_compose53i_init(cs+level, buffer, height>>level, stride<<level); break;
00706 }
00707 }
00708 }
00709
00710 void ff_spatial_idwt_slice(DWTCompose *cs, IDWTELEM *buffer, int width, int height, int stride, int type, int decomposition_count, int y){
00711 const int support = type==1 ? 3 : 5;
00712 int level;
00713 if(type==2) return;
00714
00715 for(level=decomposition_count-1; level>=0; level--){
00716 while(cs[level].y <= FFMIN((y>>level)+support, height>>level)){
00717 switch(type){
00718 case DWT_97: spatial_compose97i_dy(cs+level, buffer, width>>level, height>>level, stride<<level);
00719 break;
00720 case DWT_53: spatial_compose53i_dy(cs+level, buffer, width>>level, height>>level, stride<<level);
00721 break;
00722 }
00723 }
00724 }
00725 }
00726
00727 void ff_spatial_idwt(IDWTELEM *buffer, int width, int height, int stride, int type, int decomposition_count){
00728 DWTCompose cs[MAX_DECOMPOSITIONS];
00729 int y;
00730 ff_spatial_idwt_init(cs, buffer, width, height, stride, type, decomposition_count);
00731 for(y=0; y<height; y+=4)
00732 ff_spatial_idwt_slice(cs, buffer, width, height, stride, type, decomposition_count, y);
00733 }
00734
00735 static inline int w_c(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int w, int h, int type){
00736 int s, i, j;
00737 const int dec_count= w==8 ? 3 : 4;
00738 int tmp[32*32];
00739 int level, ori;
00740 static const int scale[2][2][4][4]={
00741 {
00742 {
00743
00744 {268, 239, 239, 213},
00745 { 0, 224, 224, 152},
00746 { 0, 135, 135, 110},
00747 },{
00748
00749 {344, 310, 310, 280},
00750 { 0, 320, 320, 228},
00751 { 0, 175, 175, 136},
00752 { 0, 129, 129, 102},
00753 }
00754 },{
00755 {
00756
00757 {275, 245, 245, 218},
00758 { 0, 230, 230, 156},
00759 { 0, 138, 138, 113},
00760 },{
00761
00762 {352, 317, 317, 286},
00763 { 0, 328, 328, 233},
00764 { 0, 180, 180, 140},
00765 { 0, 132, 132, 105},
00766 }
00767 }
00768 };
00769
00770 for (i = 0; i < h; i++) {
00771 for (j = 0; j < w; j+=4) {
00772 tmp[32*i+j+0] = (pix1[j+0] - pix2[j+0])<<4;
00773 tmp[32*i+j+1] = (pix1[j+1] - pix2[j+1])<<4;
00774 tmp[32*i+j+2] = (pix1[j+2] - pix2[j+2])<<4;
00775 tmp[32*i+j+3] = (pix1[j+3] - pix2[j+3])<<4;
00776 }
00777 pix1 += line_size;
00778 pix2 += line_size;
00779 }
00780
00781 ff_spatial_dwt(tmp, w, h, 32, type, dec_count);
00782
00783 s=0;
00784 assert(w==h);
00785 for(level=0; level<dec_count; level++){
00786 for(ori= level ? 1 : 0; ori<4; ori++){
00787 int size= w>>(dec_count-level);
00788 int sx= (ori&1) ? size : 0;
00789 int stride= 32<<(dec_count-level);
00790 int sy= (ori&2) ? stride>>1 : 0;
00791
00792 for(i=0; i<size; i++){
00793 for(j=0; j<size; j++){
00794 int v= tmp[sx + sy + i*stride + j] * scale[type][dec_count-3][level][ori];
00795 s += FFABS(v);
00796 }
00797 }
00798 }
00799 }
00800 assert(s>=0);
00801 return s>>9;
00802 }
00803
00804 static int w53_8_c(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int h){
00805 return w_c(v, pix1, pix2, line_size, 8, h, 1);
00806 }
00807
00808 static int w97_8_c(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int h){
00809 return w_c(v, pix1, pix2, line_size, 8, h, 0);
00810 }
00811
00812 static int w53_16_c(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int h){
00813 return w_c(v, pix1, pix2, line_size, 16, h, 1);
00814 }
00815
00816 static int w97_16_c(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int h){
00817 return w_c(v, pix1, pix2, line_size, 16, h, 0);
00818 }
00819
00820 int ff_w53_32_c(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int h){
00821 return w_c(v, pix1, pix2, line_size, 32, h, 1);
00822 }
00823
00824 int ff_w97_32_c(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int h){
00825 return w_c(v, pix1, pix2, line_size, 32, h, 0);
00826 }
00827
00828 void ff_dsputil_init_dwt(DSPContext *c)
00829 {
00830 c->w53[0]= w53_16_c;
00831 c->w53[1]= w53_8_c;
00832 c->w97[0]= w97_16_c;
00833 c->w97[1]= w97_8_c;
00834 }
00835
00836 void ff_dwt_init(DWTContext *c)
00837 {
00838 c->vertical_compose97i = ff_snow_vertical_compose97i;
00839 c->horizontal_compose97i = ff_snow_horizontal_compose97i;
00840 c->inner_add_yblock = ff_snow_inner_add_yblock;
00841
00842 if (HAVE_MMX) ff_dwt_init_x86(c);
00843 }