You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

844 lines
26KB

  1. /*
  2. * Copyright (C) 2004-2010 Michael Niedermayer <michaelni@gmx.at>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "libavutil/attributes.h"
  21. #include "dsputil.h"
  22. #include "dwt.h"
  23. void ff_slice_buffer_init(slice_buffer * buf, int line_count, int max_allocated_lines, int line_width, IDWTELEM * base_buffer)
  24. {
  25. int i;
  26. buf->base_buffer = base_buffer;
  27. buf->line_count = line_count;
  28. buf->line_width = line_width;
  29. buf->data_count = max_allocated_lines;
  30. buf->line = av_mallocz (sizeof(IDWTELEM *) * line_count);
  31. buf->data_stack = av_malloc (sizeof(IDWTELEM *) * max_allocated_lines);
  32. for(i = 0; i < max_allocated_lines; i++){
  33. buf->data_stack[i] = av_malloc (sizeof(IDWTELEM) * line_width);
  34. }
  35. buf->data_stack_top = max_allocated_lines - 1;
  36. }
  37. IDWTELEM * ff_slice_buffer_load_line(slice_buffer * buf, int line)
  38. {
  39. IDWTELEM * buffer;
  40. assert(buf->data_stack_top >= 0);
  41. // assert(!buf->line[line]);
  42. if (buf->line[line])
  43. return buf->line[line];
  44. buffer = buf->data_stack[buf->data_stack_top];
  45. buf->data_stack_top--;
  46. buf->line[line] = buffer;
  47. return buffer;
  48. }
  49. void ff_slice_buffer_release(slice_buffer * buf, int line)
  50. {
  51. IDWTELEM * buffer;
  52. assert(line >= 0 && line < buf->line_count);
  53. assert(buf->line[line]);
  54. buffer = buf->line[line];
  55. buf->data_stack_top++;
  56. buf->data_stack[buf->data_stack_top] = buffer;
  57. buf->line[line] = NULL;
  58. }
  59. void ff_slice_buffer_flush(slice_buffer * buf)
  60. {
  61. int i;
  62. for(i = 0; i < buf->line_count; i++){
  63. if (buf->line[i])
  64. ff_slice_buffer_release(buf, i);
  65. }
  66. }
  67. void ff_slice_buffer_destroy(slice_buffer * buf)
  68. {
  69. int i;
  70. ff_slice_buffer_flush(buf);
  71. for(i = buf->data_count - 1; i >= 0; i--){
  72. av_freep(&buf->data_stack[i]);
  73. }
  74. av_freep(&buf->data_stack);
  75. av_freep(&buf->line);
  76. }
  77. static inline int mirror(int v, int m){
  78. while((unsigned)v > (unsigned)m){
  79. v=-v;
  80. if(v<0) v+= 2*m;
  81. }
  82. return v;
  83. }
  84. static av_always_inline void
  85. lift(DWTELEM *dst, DWTELEM *src, DWTELEM *ref,
  86. int dst_step, int src_step, int ref_step,
  87. int width, int mul, int add, int shift,
  88. int highpass, int inverse){
  89. const int mirror_left= !highpass;
  90. const int mirror_right= (width&1) ^ highpass;
  91. const int w= (width>>1) - 1 + (highpass & width);
  92. int i;
  93. #define LIFT(src, ref, inv) ((src) + ((inv) ? - (ref) : + (ref)))
  94. if(mirror_left){
  95. dst[0] = LIFT(src[0], ((mul*2*ref[0]+add)>>shift), inverse);
  96. dst += dst_step;
  97. src += src_step;
  98. }
  99. for(i=0; i<w; i++){
  100. dst[i*dst_step] =
  101. LIFT(src[i*src_step],
  102. ((mul*(ref[i*ref_step] + ref[(i+1)*ref_step])+add)>>shift),
  103. inverse);
  104. }
  105. if(mirror_right){
  106. dst[w*dst_step] =
  107. LIFT(src[w*src_step],
  108. ((mul*2*ref[w*ref_step]+add)>>shift),
  109. inverse);
  110. }
  111. }
  112. static av_always_inline void
  113. inv_lift(IDWTELEM *dst, IDWTELEM *src, IDWTELEM *ref,
  114. int dst_step, int src_step, int ref_step,
  115. int width, int mul, int add, int shift,
  116. int highpass, int inverse){
  117. const int mirror_left= !highpass;
  118. const int mirror_right= (width&1) ^ highpass;
  119. const int w= (width>>1) - 1 + (highpass & width);
  120. int i;
  121. #define LIFT(src, ref, inv) ((src) + ((inv) ? - (ref) : + (ref)))
  122. if(mirror_left){
  123. dst[0] = LIFT(src[0], ((mul*2*ref[0]+add)>>shift), inverse);
  124. dst += dst_step;
  125. src += src_step;
  126. }
  127. for(i=0; i<w; i++){
  128. dst[i*dst_step] =
  129. LIFT(src[i*src_step],
  130. ((mul*(ref[i*ref_step] + ref[(i+1)*ref_step])+add)>>shift),
  131. inverse);
  132. }
  133. if(mirror_right){
  134. dst[w*dst_step] =
  135. LIFT(src[w*src_step],
  136. ((mul*2*ref[w*ref_step]+add)>>shift),
  137. inverse);
  138. }
  139. }
  140. #ifndef liftS
  141. static av_always_inline void
  142. liftS(DWTELEM *dst, DWTELEM *src, DWTELEM *ref,
  143. int dst_step, int src_step, int ref_step,
  144. int width, int mul, int add, int shift,
  145. int highpass, int inverse){
  146. const int mirror_left= !highpass;
  147. const int mirror_right= (width&1) ^ highpass;
  148. const int w= (width>>1) - 1 + (highpass & width);
  149. int i;
  150. assert(shift == 4);
  151. #define LIFTS(src, ref, inv) \
  152. ((inv) ? \
  153. (src) + (((ref) + 4*(src))>>shift): \
  154. -((-16*(src) + (ref) + add/4 + 1 + (5<<25))/(5*4) - (1<<23)))
  155. if(mirror_left){
  156. dst[0] = LIFTS(src[0], mul*2*ref[0]+add, inverse);
  157. dst += dst_step;
  158. src += src_step;
  159. }
  160. for(i=0; i<w; i++){
  161. dst[i*dst_step] =
  162. LIFTS(src[i*src_step],
  163. mul*(ref[i*ref_step] + ref[(i+1)*ref_step])+add,
  164. inverse);
  165. }
  166. if(mirror_right){
  167. dst[w*dst_step] =
  168. LIFTS(src[w*src_step], mul*2*ref[w*ref_step]+add, inverse);
  169. }
  170. }
  171. static av_always_inline void
  172. inv_liftS(IDWTELEM *dst, IDWTELEM *src, IDWTELEM *ref,
  173. int dst_step, int src_step, int ref_step,
  174. int width, int mul, int add, int shift,
  175. int highpass, int inverse){
  176. const int mirror_left= !highpass;
  177. const int mirror_right= (width&1) ^ highpass;
  178. const int w= (width>>1) - 1 + (highpass & width);
  179. int i;
  180. assert(shift == 4);
  181. #define LIFTS(src, ref, inv) \
  182. ((inv) ? \
  183. (src) + (((ref) + 4*(src))>>shift): \
  184. -((-16*(src) + (ref) + add/4 + 1 + (5<<25))/(5*4) - (1<<23)))
  185. if(mirror_left){
  186. dst[0] = LIFTS(src[0], mul*2*ref[0]+add, inverse);
  187. dst += dst_step;
  188. src += src_step;
  189. }
  190. for(i=0; i<w; i++){
  191. dst[i*dst_step] =
  192. LIFTS(src[i*src_step],
  193. mul*(ref[i*ref_step] + ref[(i+1)*ref_step])+add,
  194. inverse);
  195. }
  196. if(mirror_right){
  197. dst[w*dst_step] =
  198. LIFTS(src[w*src_step], mul*2*ref[w*ref_step]+add, inverse);
  199. }
  200. }
  201. #endif /* ! liftS */
  202. static void horizontal_decompose53i(DWTELEM *b, int width){
  203. DWTELEM temp[width];
  204. const int width2= width>>1;
  205. int x;
  206. const int w2= (width+1)>>1;
  207. for(x=0; x<width2; x++){
  208. temp[x ]= b[2*x ];
  209. temp[x+w2]= b[2*x + 1];
  210. }
  211. if(width&1)
  212. temp[x ]= b[2*x ];
  213. #if 0
  214. {
  215. int A1,A2,A3,A4;
  216. A2= temp[1 ];
  217. A4= temp[0 ];
  218. A1= temp[0+width2];
  219. A1 -= (A2 + A4)>>1;
  220. A4 += (A1 + 1)>>1;
  221. b[0+width2] = A1;
  222. b[0 ] = A4;
  223. for(x=1; x+1<width2; x+=2){
  224. A3= temp[x+width2];
  225. A4= temp[x+1 ];
  226. A3 -= (A2 + A4)>>1;
  227. A2 += (A1 + A3 + 2)>>2;
  228. b[x+width2] = A3;
  229. b[x ] = A2;
  230. A1= temp[x+1+width2];
  231. A2= temp[x+2 ];
  232. A1 -= (A2 + A4)>>1;
  233. A4 += (A1 + A3 + 2)>>2;
  234. b[x+1+width2] = A1;
  235. b[x+1 ] = A4;
  236. }
  237. A3= temp[width-1];
  238. A3 -= A2;
  239. A2 += (A1 + A3 + 2)>>2;
  240. b[width -1] = A3;
  241. b[width2-1] = A2;
  242. }
  243. #else
  244. lift(b+w2, temp+w2, temp, 1, 1, 1, width, -1, 0, 1, 1, 0);
  245. lift(b , temp , b+w2, 1, 1, 1, width, 1, 2, 2, 0, 0);
  246. #endif /* 0 */
  247. }
  248. static void vertical_decompose53iH0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
  249. int i;
  250. for(i=0; i<width; i++){
  251. b1[i] -= (b0[i] + b2[i])>>1;
  252. }
  253. }
  254. static void vertical_decompose53iL0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
  255. int i;
  256. for(i=0; i<width; i++){
  257. b1[i] += (b0[i] + b2[i] + 2)>>2;
  258. }
  259. }
  260. static void spatial_decompose53i(DWTELEM *buffer, int width, int height, int stride){
  261. int y;
  262. DWTELEM *b0= buffer + mirror(-2-1, height-1)*stride;
  263. DWTELEM *b1= buffer + mirror(-2 , height-1)*stride;
  264. for(y=-2; y<height; y+=2){
  265. DWTELEM *b2= buffer + mirror(y+1, height-1)*stride;
  266. DWTELEM *b3= buffer + mirror(y+2, height-1)*stride;
  267. if(y+1<(unsigned)height) horizontal_decompose53i(b2, width);
  268. if(y+2<(unsigned)height) horizontal_decompose53i(b3, width);
  269. if(y+1<(unsigned)height) vertical_decompose53iH0(b1, b2, b3, width);
  270. if(y+0<(unsigned)height) vertical_decompose53iL0(b0, b1, b2, width);
  271. b0=b2;
  272. b1=b3;
  273. }
  274. }
  275. static void horizontal_decompose97i(DWTELEM *b, int width){
  276. DWTELEM temp[width];
  277. const int w2= (width+1)>>1;
  278. lift (temp+w2, b +1, b , 1, 2, 2, width, W_AM, W_AO, W_AS, 1, 1);
  279. liftS(temp , b , temp+w2, 1, 2, 1, width, W_BM, W_BO, W_BS, 0, 0);
  280. lift (b +w2, temp+w2, temp , 1, 1, 1, width, W_CM, W_CO, W_CS, 1, 0);
  281. lift (b , temp , b +w2, 1, 1, 1, width, W_DM, W_DO, W_DS, 0, 0);
  282. }
  283. static void vertical_decompose97iH0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
  284. int i;
  285. for(i=0; i<width; i++){
  286. b1[i] -= (W_AM*(b0[i] + b2[i])+W_AO)>>W_AS;
  287. }
  288. }
  289. static void vertical_decompose97iH1(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
  290. int i;
  291. for(i=0; i<width; i++){
  292. b1[i] += (W_CM*(b0[i] + b2[i])+W_CO)>>W_CS;
  293. }
  294. }
  295. static void vertical_decompose97iL0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
  296. int i;
  297. for(i=0; i<width; i++){
  298. #ifdef liftS
  299. b1[i] -= (W_BM*(b0[i] + b2[i])+W_BO)>>W_BS;
  300. #else
  301. b1[i] = (16*4*b1[i] - 4*(b0[i] + b2[i]) + W_BO*5 + (5<<27)) / (5*16) - (1<<23);
  302. #endif
  303. }
  304. }
  305. static void vertical_decompose97iL1(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
  306. int i;
  307. for(i=0; i<width; i++){
  308. b1[i] += (W_DM*(b0[i] + b2[i])+W_DO)>>W_DS;
  309. }
  310. }
  311. static void spatial_decompose97i(DWTELEM *buffer, int width, int height, int stride){
  312. int y;
  313. DWTELEM *b0= buffer + mirror(-4-1, height-1)*stride;
  314. DWTELEM *b1= buffer + mirror(-4 , height-1)*stride;
  315. DWTELEM *b2= buffer + mirror(-4+1, height-1)*stride;
  316. DWTELEM *b3= buffer + mirror(-4+2, height-1)*stride;
  317. for(y=-4; y<height; y+=2){
  318. DWTELEM *b4= buffer + mirror(y+3, height-1)*stride;
  319. DWTELEM *b5= buffer + mirror(y+4, height-1)*stride;
  320. if(y+3<(unsigned)height) horizontal_decompose97i(b4, width);
  321. if(y+4<(unsigned)height) horizontal_decompose97i(b5, width);
  322. if(y+3<(unsigned)height) vertical_decompose97iH0(b3, b4, b5, width);
  323. if(y+2<(unsigned)height) vertical_decompose97iL0(b2, b3, b4, width);
  324. if(y+1<(unsigned)height) vertical_decompose97iH1(b1, b2, b3, width);
  325. if(y+0<(unsigned)height) vertical_decompose97iL1(b0, b1, b2, width);
  326. b0=b2;
  327. b1=b3;
  328. b2=b4;
  329. b3=b5;
  330. }
  331. }
  332. void ff_spatial_dwt(DWTELEM *buffer, int width, int height, int stride, int type, int decomposition_count){
  333. int level;
  334. for(level=0; level<decomposition_count; level++){
  335. switch(type){
  336. case DWT_97: spatial_decompose97i(buffer, width>>level, height>>level, stride<<level); break;
  337. case DWT_53: spatial_decompose53i(buffer, width>>level, height>>level, stride<<level); break;
  338. }
  339. }
  340. }
  341. static void horizontal_compose53i(IDWTELEM *b, int width){
  342. IDWTELEM temp[width];
  343. const int width2= width>>1;
  344. const int w2= (width+1)>>1;
  345. int x;
  346. for(x=0; x<width2; x++){
  347. temp[2*x ]= b[x ];
  348. temp[2*x + 1]= b[x+w2];
  349. }
  350. if(width&1)
  351. temp[2*x ]= b[x ];
  352. b[0] = temp[0] - ((temp[1]+1)>>1);
  353. for(x=2; x<width-1; x+=2){
  354. b[x ] = temp[x ] - ((temp[x-1] + temp[x+1]+2)>>2);
  355. b[x-1] = temp[x-1] + ((b [x-2] + b [x ]+1)>>1);
  356. }
  357. if(width&1){
  358. b[x ] = temp[x ] - ((temp[x-1]+1)>>1);
  359. b[x-1] = temp[x-1] + ((b [x-2] + b [x ]+1)>>1);
  360. }else
  361. b[x-1] = temp[x-1] + b[x-2];
  362. }
  363. static void vertical_compose53iH0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width){
  364. int i;
  365. for(i=0; i<width; i++){
  366. b1[i] += (b0[i] + b2[i])>>1;
  367. }
  368. }
  369. static void vertical_compose53iL0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width){
  370. int i;
  371. for(i=0; i<width; i++){
  372. b1[i] -= (b0[i] + b2[i] + 2)>>2;
  373. }
  374. }
  375. static void spatial_compose53i_buffered_init(DWTCompose *cs, slice_buffer * sb, int height, int stride_line){
  376. cs->b0 = slice_buffer_get_line(sb, mirror(-1-1, height-1) * stride_line);
  377. cs->b1 = slice_buffer_get_line(sb, mirror(-1 , height-1) * stride_line);
  378. cs->y = -1;
  379. }
  380. static void spatial_compose53i_init(DWTCompose *cs, IDWTELEM *buffer, int height, int stride){
  381. cs->b0 = buffer + mirror(-1-1, height-1)*stride;
  382. cs->b1 = buffer + mirror(-1 , height-1)*stride;
  383. cs->y = -1;
  384. }
  385. static void spatial_compose53i_dy_buffered(DWTCompose *cs, slice_buffer * sb, int width, int height, int stride_line){
  386. int y= cs->y;
  387. IDWTELEM *b0= cs->b0;
  388. IDWTELEM *b1= cs->b1;
  389. IDWTELEM *b2= slice_buffer_get_line(sb, mirror(y+1, height-1) * stride_line);
  390. IDWTELEM *b3= slice_buffer_get_line(sb, mirror(y+2, height-1) * stride_line);
  391. if(y+1<(unsigned)height && y<(unsigned)height){
  392. int x;
  393. for(x=0; x<width; x++){
  394. b2[x] -= (b1[x] + b3[x] + 2)>>2;
  395. b1[x] += (b0[x] + b2[x])>>1;
  396. }
  397. }else{
  398. if(y+1<(unsigned)height) vertical_compose53iL0(b1, b2, b3, width);
  399. if(y+0<(unsigned)height) vertical_compose53iH0(b0, b1, b2, width);
  400. }
  401. if(y-1<(unsigned)height) horizontal_compose53i(b0, width);
  402. if(y+0<(unsigned)height) horizontal_compose53i(b1, width);
  403. cs->b0 = b2;
  404. cs->b1 = b3;
  405. cs->y += 2;
  406. }
  407. static void spatial_compose53i_dy(DWTCompose *cs, IDWTELEM *buffer, int width, int height, int stride){
  408. int y= cs->y;
  409. IDWTELEM *b0= cs->b0;
  410. IDWTELEM *b1= cs->b1;
  411. IDWTELEM *b2= buffer + mirror(y+1, height-1)*stride;
  412. IDWTELEM *b3= buffer + mirror(y+2, height-1)*stride;
  413. if(y+1<(unsigned)height) vertical_compose53iL0(b1, b2, b3, width);
  414. if(y+0<(unsigned)height) vertical_compose53iH0(b0, b1, b2, width);
  415. if(y-1<(unsigned)height) horizontal_compose53i(b0, width);
  416. if(y+0<(unsigned)height) horizontal_compose53i(b1, width);
  417. cs->b0 = b2;
  418. cs->b1 = b3;
  419. cs->y += 2;
  420. }
  421. static void av_unused spatial_compose53i(IDWTELEM *buffer, int width, int height, int stride){
  422. DWTCompose cs;
  423. spatial_compose53i_init(&cs, buffer, height, stride);
  424. while(cs.y <= height)
  425. spatial_compose53i_dy(&cs, buffer, width, height, stride);
  426. }
  427. void ff_snow_horizontal_compose97i(IDWTELEM *b, int width){
  428. IDWTELEM temp[width];
  429. const int w2= (width+1)>>1;
  430. #if 0 //maybe more understadable but slower
  431. inv_lift (temp , b , b +w2, 2, 1, 1, width, W_DM, W_DO, W_DS, 0, 1);
  432. inv_lift (temp+1 , b +w2, temp , 2, 1, 2, width, W_CM, W_CO, W_CS, 1, 1);
  433. inv_liftS(b , temp , temp+1 , 2, 2, 2, width, W_BM, W_BO, W_BS, 0, 1);
  434. inv_lift (b+1 , temp+1 , b , 2, 2, 2, width, W_AM, W_AO, W_AS, 1, 0);
  435. #else
  436. int x;
  437. temp[0] = b[0] - ((3*b[w2]+2)>>2);
  438. for(x=1; x<(width>>1); x++){
  439. temp[2*x ] = b[x ] - ((3*(b [x+w2-1] + b[x+w2])+4)>>3);
  440. temp[2*x-1] = b[x+w2-1] - temp[2*x-2] - temp[2*x];
  441. }
  442. if(width&1){
  443. temp[2*x ] = b[x ] - ((3*b [x+w2-1]+2)>>2);
  444. temp[2*x-1] = b[x+w2-1] - temp[2*x-2] - temp[2*x];
  445. }else
  446. temp[2*x-1] = b[x+w2-1] - 2*temp[2*x-2];
  447. b[0] = temp[0] + ((2*temp[0] + temp[1]+4)>>3);
  448. for(x=2; x<width-1; x+=2){
  449. b[x ] = temp[x ] + ((4*temp[x ] + temp[x-1] + temp[x+1]+8)>>4);
  450. b[x-1] = temp[x-1] + ((3*(b [x-2] + b [x ] ))>>1);
  451. }
  452. if(width&1){
  453. b[x ] = temp[x ] + ((2*temp[x ] + temp[x-1]+4)>>3);
  454. b[x-1] = temp[x-1] + ((3*(b [x-2] + b [x ] ))>>1);
  455. }else
  456. b[x-1] = temp[x-1] + 3*b [x-2];
  457. #endif
  458. }
  459. static void vertical_compose97iH0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width){
  460. int i;
  461. for(i=0; i<width; i++){
  462. b1[i] += (W_AM*(b0[i] + b2[i])+W_AO)>>W_AS;
  463. }
  464. }
  465. static void vertical_compose97iH1(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width){
  466. int i;
  467. for(i=0; i<width; i++){
  468. b1[i] -= (W_CM*(b0[i] + b2[i])+W_CO)>>W_CS;
  469. }
  470. }
  471. static void vertical_compose97iL0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width){
  472. int i;
  473. for(i=0; i<width; i++){
  474. #ifdef liftS
  475. b1[i] += (W_BM*(b0[i] + b2[i])+W_BO)>>W_BS;
  476. #else
  477. b1[i] += (W_BM*(b0[i] + b2[i])+4*b1[i]+W_BO)>>W_BS;
  478. #endif
  479. }
  480. }
  481. static void vertical_compose97iL1(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width){
  482. int i;
  483. for(i=0; i<width; i++){
  484. b1[i] -= (W_DM*(b0[i] + b2[i])+W_DO)>>W_DS;
  485. }
  486. }
  487. void ff_snow_vertical_compose97i(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, IDWTELEM *b3, IDWTELEM *b4, IDWTELEM *b5, int width){
  488. int i;
  489. for(i=0; i<width; i++){
  490. b4[i] -= (W_DM*(b3[i] + b5[i])+W_DO)>>W_DS;
  491. b3[i] -= (W_CM*(b2[i] + b4[i])+W_CO)>>W_CS;
  492. #ifdef liftS
  493. b2[i] += (W_BM*(b1[i] + b3[i])+W_BO)>>W_BS;
  494. #else
  495. b2[i] += (W_BM*(b1[i] + b3[i])+4*b2[i]+W_BO)>>W_BS;
  496. #endif
  497. b1[i] += (W_AM*(b0[i] + b2[i])+W_AO)>>W_AS;
  498. }
  499. }
  500. static void spatial_compose97i_buffered_init(DWTCompose *cs, slice_buffer * sb, int height, int stride_line){
  501. cs->b0 = slice_buffer_get_line(sb, mirror(-3-1, height-1) * stride_line);
  502. cs->b1 = slice_buffer_get_line(sb, mirror(-3 , height-1) * stride_line);
  503. cs->b2 = slice_buffer_get_line(sb, mirror(-3+1, height-1) * stride_line);
  504. cs->b3 = slice_buffer_get_line(sb, mirror(-3+2, height-1) * stride_line);
  505. cs->y = -3;
  506. }
  507. static void spatial_compose97i_init(DWTCompose *cs, IDWTELEM *buffer, int height, int stride){
  508. cs->b0 = buffer + mirror(-3-1, height-1)*stride;
  509. cs->b1 = buffer + mirror(-3 , height-1)*stride;
  510. cs->b2 = buffer + mirror(-3+1, height-1)*stride;
  511. cs->b3 = buffer + mirror(-3+2, height-1)*stride;
  512. cs->y = -3;
  513. }
  514. static void spatial_compose97i_dy_buffered(DWTContext *dsp, DWTCompose *cs, slice_buffer * sb, int width, int height, int stride_line){
  515. int y = cs->y;
  516. IDWTELEM *b0= cs->b0;
  517. IDWTELEM *b1= cs->b1;
  518. IDWTELEM *b2= cs->b2;
  519. IDWTELEM *b3= cs->b3;
  520. IDWTELEM *b4= slice_buffer_get_line(sb, mirror(y + 3, height - 1) * stride_line);
  521. IDWTELEM *b5= slice_buffer_get_line(sb, mirror(y + 4, height - 1) * stride_line);
  522. if(y>0 && y+4<height){
  523. dsp->vertical_compose97i(b0, b1, b2, b3, b4, b5, width);
  524. }else{
  525. if(y+3<(unsigned)height) vertical_compose97iL1(b3, b4, b5, width);
  526. if(y+2<(unsigned)height) vertical_compose97iH1(b2, b3, b4, width);
  527. if(y+1<(unsigned)height) vertical_compose97iL0(b1, b2, b3, width);
  528. if(y+0<(unsigned)height) vertical_compose97iH0(b0, b1, b2, width);
  529. }
  530. if(y-1<(unsigned)height) dsp->horizontal_compose97i(b0, width);
  531. if(y+0<(unsigned)height) dsp->horizontal_compose97i(b1, width);
  532. cs->b0=b2;
  533. cs->b1=b3;
  534. cs->b2=b4;
  535. cs->b3=b5;
  536. cs->y += 2;
  537. }
  538. static void spatial_compose97i_dy(DWTCompose *cs, IDWTELEM *buffer, int width, int height, int stride){
  539. int y = cs->y;
  540. IDWTELEM *b0= cs->b0;
  541. IDWTELEM *b1= cs->b1;
  542. IDWTELEM *b2= cs->b2;
  543. IDWTELEM *b3= cs->b3;
  544. IDWTELEM *b4= buffer + mirror(y+3, height-1)*stride;
  545. IDWTELEM *b5= buffer + mirror(y+4, height-1)*stride;
  546. if(y+3<(unsigned)height) vertical_compose97iL1(b3, b4, b5, width);
  547. if(y+2<(unsigned)height) vertical_compose97iH1(b2, b3, b4, width);
  548. if(y+1<(unsigned)height) vertical_compose97iL0(b1, b2, b3, width);
  549. if(y+0<(unsigned)height) vertical_compose97iH0(b0, b1, b2, width);
  550. if(y-1<(unsigned)height) ff_snow_horizontal_compose97i(b0, width);
  551. if(y+0<(unsigned)height) ff_snow_horizontal_compose97i(b1, width);
  552. cs->b0=b2;
  553. cs->b1=b3;
  554. cs->b2=b4;
  555. cs->b3=b5;
  556. cs->y += 2;
  557. }
  558. static void av_unused spatial_compose97i(IDWTELEM *buffer, int width, int height, int stride){
  559. DWTCompose cs;
  560. spatial_compose97i_init(&cs, buffer, height, stride);
  561. while(cs.y <= height)
  562. spatial_compose97i_dy(&cs, buffer, width, height, stride);
  563. }
  564. void ff_spatial_idwt_buffered_init(DWTCompose *cs, slice_buffer * sb, int width, int height, int stride_line, int type, int decomposition_count){
  565. int level;
  566. for(level=decomposition_count-1; level>=0; level--){
  567. switch(type){
  568. case DWT_97: spatial_compose97i_buffered_init(cs+level, sb, height>>level, stride_line<<level); break;
  569. case DWT_53: spatial_compose53i_buffered_init(cs+level, sb, height>>level, stride_line<<level); break;
  570. }
  571. }
  572. }
  573. 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){
  574. const int support = type==1 ? 3 : 5;
  575. int level;
  576. if(type==2) return;
  577. for(level=decomposition_count-1; level>=0; level--){
  578. while(cs[level].y <= FFMIN((y>>level)+support, height>>level)){
  579. switch(type){
  580. case DWT_97: spatial_compose97i_dy_buffered(dsp, cs+level, slice_buf, width>>level, height>>level, stride_line<<level);
  581. break;
  582. case DWT_53: spatial_compose53i_dy_buffered(cs+level, slice_buf, width>>level, height>>level, stride_line<<level);
  583. break;
  584. }
  585. }
  586. }
  587. }
  588. void ff_spatial_idwt_init(DWTCompose *cs, IDWTELEM *buffer, int width, int height, int stride, int type, int decomposition_count){
  589. int level;
  590. for(level=decomposition_count-1; level>=0; level--){
  591. switch(type){
  592. case DWT_97: spatial_compose97i_init(cs+level, buffer, height>>level, stride<<level); break;
  593. case DWT_53: spatial_compose53i_init(cs+level, buffer, height>>level, stride<<level); break;
  594. }
  595. }
  596. }
  597. void ff_spatial_idwt_slice(DWTCompose *cs, IDWTELEM *buffer, int width, int height, int stride, int type, int decomposition_count, int y){
  598. const int support = type==1 ? 3 : 5;
  599. int level;
  600. if(type==2) return;
  601. for(level=decomposition_count-1; level>=0; level--){
  602. while(cs[level].y <= FFMIN((y>>level)+support, height>>level)){
  603. switch(type){
  604. case DWT_97: spatial_compose97i_dy(cs+level, buffer, width>>level, height>>level, stride<<level);
  605. break;
  606. case DWT_53: spatial_compose53i_dy(cs+level, buffer, width>>level, height>>level, stride<<level);
  607. break;
  608. }
  609. }
  610. }
  611. }
  612. void ff_spatial_idwt(IDWTELEM *buffer, int width, int height, int stride, int type, int decomposition_count){
  613. DWTCompose cs[MAX_DECOMPOSITIONS];
  614. int y;
  615. ff_spatial_idwt_init(cs, buffer, width, height, stride, type, decomposition_count);
  616. for(y=0; y<height; y+=4)
  617. ff_spatial_idwt_slice(cs, buffer, width, height, stride, type, decomposition_count, y);
  618. }
  619. static inline int w_c(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int w, int h, int type){
  620. int s, i, j;
  621. const int dec_count= w==8 ? 3 : 4;
  622. int tmp[32*32];
  623. int level, ori;
  624. static const int scale[2][2][4][4]={
  625. {
  626. {
  627. // 9/7 8x8 dec=3
  628. {268, 239, 239, 213},
  629. { 0, 224, 224, 152},
  630. { 0, 135, 135, 110},
  631. },{
  632. // 9/7 16x16 or 32x32 dec=4
  633. {344, 310, 310, 280},
  634. { 0, 320, 320, 228},
  635. { 0, 175, 175, 136},
  636. { 0, 129, 129, 102},
  637. }
  638. },{
  639. {
  640. // 5/3 8x8 dec=3
  641. {275, 245, 245, 218},
  642. { 0, 230, 230, 156},
  643. { 0, 138, 138, 113},
  644. },{
  645. // 5/3 16x16 or 32x32 dec=4
  646. {352, 317, 317, 286},
  647. { 0, 328, 328, 233},
  648. { 0, 180, 180, 140},
  649. { 0, 132, 132, 105},
  650. }
  651. }
  652. };
  653. for (i = 0; i < h; i++) {
  654. for (j = 0; j < w; j+=4) {
  655. tmp[32*i+j+0] = (pix1[j+0] - pix2[j+0])<<4;
  656. tmp[32*i+j+1] = (pix1[j+1] - pix2[j+1])<<4;
  657. tmp[32*i+j+2] = (pix1[j+2] - pix2[j+2])<<4;
  658. tmp[32*i+j+3] = (pix1[j+3] - pix2[j+3])<<4;
  659. }
  660. pix1 += line_size;
  661. pix2 += line_size;
  662. }
  663. ff_spatial_dwt(tmp, w, h, 32, type, dec_count);
  664. s=0;
  665. assert(w==h);
  666. for(level=0; level<dec_count; level++){
  667. for(ori= level ? 1 : 0; ori<4; ori++){
  668. int size= w>>(dec_count-level);
  669. int sx= (ori&1) ? size : 0;
  670. int stride= 32<<(dec_count-level);
  671. int sy= (ori&2) ? stride>>1 : 0;
  672. for(i=0; i<size; i++){
  673. for(j=0; j<size; j++){
  674. int v= tmp[sx + sy + i*stride + j] * scale[type][dec_count-3][level][ori];
  675. s += FFABS(v);
  676. }
  677. }
  678. }
  679. }
  680. assert(s>=0);
  681. return s>>9;
  682. }
  683. static int w53_8_c(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int h){
  684. return w_c(v, pix1, pix2, line_size, 8, h, 1);
  685. }
  686. static int w97_8_c(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int h){
  687. return w_c(v, pix1, pix2, line_size, 8, h, 0);
  688. }
  689. static int w53_16_c(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int h){
  690. return w_c(v, pix1, pix2, line_size, 16, h, 1);
  691. }
  692. static int w97_16_c(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int h){
  693. return w_c(v, pix1, pix2, line_size, 16, h, 0);
  694. }
  695. int ff_w53_32_c(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int h){
  696. return w_c(v, pix1, pix2, line_size, 32, h, 1);
  697. }
  698. int ff_w97_32_c(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int h){
  699. return w_c(v, pix1, pix2, line_size, 32, h, 0);
  700. }
  701. void ff_dsputil_init_dwt(DSPContext *c)
  702. {
  703. c->w53[0]= w53_16_c;
  704. c->w53[1]= w53_8_c;
  705. c->w97[0]= w97_16_c;
  706. c->w97[1]= w97_8_c;
  707. }
  708. void ff_dwt_init(DWTContext *c)
  709. {
  710. c->vertical_compose97i = ff_snow_vertical_compose97i;
  711. c->horizontal_compose97i = ff_snow_horizontal_compose97i;
  712. c->inner_add_yblock = ff_snow_inner_add_yblock;
  713. if (HAVE_MMX) ff_dwt_init_x86(c);
  714. }