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.

1384 lines
43KB

  1. /*
  2. * Copyright (C) 2004-2010 Michael Niedermayer <michaelni@gmx.at>
  3. * Copyright (C) 2008 David Conrad
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/attributes.h"
  22. #include "dsputil.h"
  23. #include "dwt.h"
  24. #include "libavcodec/x86/dwt.h"
  25. void ff_slice_buffer_init(slice_buffer * buf, int line_count, int max_allocated_lines, int line_width, IDWTELEM * base_buffer)
  26. {
  27. int i;
  28. buf->base_buffer = base_buffer;
  29. buf->line_count = line_count;
  30. buf->line_width = line_width;
  31. buf->data_count = max_allocated_lines;
  32. buf->line = av_mallocz (sizeof(IDWTELEM *) * line_count);
  33. buf->data_stack = av_malloc (sizeof(IDWTELEM *) * max_allocated_lines);
  34. for(i = 0; i < max_allocated_lines; i++){
  35. buf->data_stack[i] = av_malloc (sizeof(IDWTELEM) * line_width);
  36. }
  37. buf->data_stack_top = max_allocated_lines - 1;
  38. }
  39. IDWTELEM * ff_slice_buffer_load_line(slice_buffer * buf, int line)
  40. {
  41. IDWTELEM * buffer;
  42. assert(buf->data_stack_top >= 0);
  43. // assert(!buf->line[line]);
  44. if (buf->line[line])
  45. return buf->line[line];
  46. buffer = buf->data_stack[buf->data_stack_top];
  47. buf->data_stack_top--;
  48. buf->line[line] = buffer;
  49. return buffer;
  50. }
  51. void ff_slice_buffer_release(slice_buffer * buf, int line)
  52. {
  53. IDWTELEM * buffer;
  54. assert(line >= 0 && line < buf->line_count);
  55. assert(buf->line[line]);
  56. buffer = buf->line[line];
  57. buf->data_stack_top++;
  58. buf->data_stack[buf->data_stack_top] = buffer;
  59. buf->line[line] = NULL;
  60. }
  61. void ff_slice_buffer_flush(slice_buffer * buf)
  62. {
  63. int i;
  64. for(i = 0; i < buf->line_count; i++){
  65. if (buf->line[i])
  66. ff_slice_buffer_release(buf, i);
  67. }
  68. }
  69. void ff_slice_buffer_destroy(slice_buffer * buf)
  70. {
  71. int i;
  72. ff_slice_buffer_flush(buf);
  73. for(i = buf->data_count - 1; i >= 0; i--){
  74. av_freep(&buf->data_stack[i]);
  75. }
  76. av_freep(&buf->data_stack);
  77. av_freep(&buf->line);
  78. }
  79. static inline int mirror(int v, int m){
  80. while((unsigned)v > (unsigned)m){
  81. v=-v;
  82. if(v<0) v+= 2*m;
  83. }
  84. return v;
  85. }
  86. static av_always_inline void
  87. lift(DWTELEM *dst, DWTELEM *src, DWTELEM *ref,
  88. int dst_step, int src_step, int ref_step,
  89. int width, int mul, int add, int shift,
  90. int highpass, int inverse){
  91. const int mirror_left= !highpass;
  92. const int mirror_right= (width&1) ^ highpass;
  93. const int w= (width>>1) - 1 + (highpass & width);
  94. int i;
  95. #define LIFT(src, ref, inv) ((src) + ((inv) ? - (ref) : + (ref)))
  96. if(mirror_left){
  97. dst[0] = LIFT(src[0], ((mul*2*ref[0]+add)>>shift), inverse);
  98. dst += dst_step;
  99. src += src_step;
  100. }
  101. for(i=0; i<w; i++){
  102. dst[i*dst_step] =
  103. LIFT(src[i*src_step],
  104. ((mul*(ref[i*ref_step] + ref[(i+1)*ref_step])+add)>>shift),
  105. inverse);
  106. }
  107. if(mirror_right){
  108. dst[w*dst_step] =
  109. LIFT(src[w*src_step],
  110. ((mul*2*ref[w*ref_step]+add)>>shift),
  111. inverse);
  112. }
  113. }
  114. static av_always_inline void
  115. inv_lift(IDWTELEM *dst, IDWTELEM *src, IDWTELEM *ref,
  116. int dst_step, int src_step, int ref_step,
  117. int width, int mul, int add, int shift,
  118. int highpass, int inverse){
  119. const int mirror_left= !highpass;
  120. const int mirror_right= (width&1) ^ highpass;
  121. const int w= (width>>1) - 1 + (highpass & width);
  122. int i;
  123. #define LIFT(src, ref, inv) ((src) + ((inv) ? - (ref) : + (ref)))
  124. if(mirror_left){
  125. dst[0] = LIFT(src[0], ((mul*2*ref[0]+add)>>shift), inverse);
  126. dst += dst_step;
  127. src += src_step;
  128. }
  129. for(i=0; i<w; i++){
  130. dst[i*dst_step] =
  131. LIFT(src[i*src_step],
  132. ((mul*(ref[i*ref_step] + ref[(i+1)*ref_step])+add)>>shift),
  133. inverse);
  134. }
  135. if(mirror_right){
  136. dst[w*dst_step] =
  137. LIFT(src[w*src_step],
  138. ((mul*2*ref[w*ref_step]+add)>>shift),
  139. inverse);
  140. }
  141. }
  142. #ifndef liftS
  143. static av_always_inline void
  144. liftS(DWTELEM *dst, DWTELEM *src, DWTELEM *ref,
  145. int dst_step, int src_step, int ref_step,
  146. int width, int mul, int add, int shift,
  147. int highpass, int inverse){
  148. const int mirror_left= !highpass;
  149. const int mirror_right= (width&1) ^ highpass;
  150. const int w= (width>>1) - 1 + (highpass & width);
  151. int i;
  152. assert(shift == 4);
  153. #define LIFTS(src, ref, inv) \
  154. ((inv) ? \
  155. (src) + (((ref) + 4*(src))>>shift): \
  156. -((-16*(src) + (ref) + add/4 + 1 + (5<<25))/(5*4) - (1<<23)))
  157. if(mirror_left){
  158. dst[0] = LIFTS(src[0], mul*2*ref[0]+add, inverse);
  159. dst += dst_step;
  160. src += src_step;
  161. }
  162. for(i=0; i<w; i++){
  163. dst[i*dst_step] =
  164. LIFTS(src[i*src_step],
  165. mul*(ref[i*ref_step] + ref[(i+1)*ref_step])+add,
  166. inverse);
  167. }
  168. if(mirror_right){
  169. dst[w*dst_step] =
  170. LIFTS(src[w*src_step], mul*2*ref[w*ref_step]+add, inverse);
  171. }
  172. }
  173. static av_always_inline void
  174. inv_liftS(IDWTELEM *dst, IDWTELEM *src, IDWTELEM *ref,
  175. int dst_step, int src_step, int ref_step,
  176. int width, int mul, int add, int shift,
  177. int highpass, int inverse){
  178. const int mirror_left= !highpass;
  179. const int mirror_right= (width&1) ^ highpass;
  180. const int w= (width>>1) - 1 + (highpass & width);
  181. int i;
  182. assert(shift == 4);
  183. #define LIFTS(src, ref, inv) \
  184. ((inv) ? \
  185. (src) + (((ref) + 4*(src))>>shift): \
  186. -((-16*(src) + (ref) + add/4 + 1 + (5<<25))/(5*4) - (1<<23)))
  187. if(mirror_left){
  188. dst[0] = LIFTS(src[0], mul*2*ref[0]+add, inverse);
  189. dst += dst_step;
  190. src += src_step;
  191. }
  192. for(i=0; i<w; i++){
  193. dst[i*dst_step] =
  194. LIFTS(src[i*src_step],
  195. mul*(ref[i*ref_step] + ref[(i+1)*ref_step])+add,
  196. inverse);
  197. }
  198. if(mirror_right){
  199. dst[w*dst_step] =
  200. LIFTS(src[w*src_step], mul*2*ref[w*ref_step]+add, inverse);
  201. }
  202. }
  203. #endif /* ! liftS */
  204. static void horizontal_decompose53i(DWTELEM *b, int width){
  205. DWTELEM temp[width];
  206. const int width2= width>>1;
  207. int x;
  208. const int w2= (width+1)>>1;
  209. for(x=0; x<width2; x++){
  210. temp[x ]= b[2*x ];
  211. temp[x+w2]= b[2*x + 1];
  212. }
  213. if(width&1)
  214. temp[x ]= b[2*x ];
  215. #if 0
  216. {
  217. int A1,A2,A3,A4;
  218. A2= temp[1 ];
  219. A4= temp[0 ];
  220. A1= temp[0+width2];
  221. A1 -= (A2 + A4)>>1;
  222. A4 += (A1 + 1)>>1;
  223. b[0+width2] = A1;
  224. b[0 ] = A4;
  225. for(x=1; x+1<width2; x+=2){
  226. A3= temp[x+width2];
  227. A4= temp[x+1 ];
  228. A3 -= (A2 + A4)>>1;
  229. A2 += (A1 + A3 + 2)>>2;
  230. b[x+width2] = A3;
  231. b[x ] = A2;
  232. A1= temp[x+1+width2];
  233. A2= temp[x+2 ];
  234. A1 -= (A2 + A4)>>1;
  235. A4 += (A1 + A3 + 2)>>2;
  236. b[x+1+width2] = A1;
  237. b[x+1 ] = A4;
  238. }
  239. A3= temp[width-1];
  240. A3 -= A2;
  241. A2 += (A1 + A3 + 2)>>2;
  242. b[width -1] = A3;
  243. b[width2-1] = A2;
  244. }
  245. #else
  246. lift(b+w2, temp+w2, temp, 1, 1, 1, width, -1, 0, 1, 1, 0);
  247. lift(b , temp , b+w2, 1, 1, 1, width, 1, 2, 2, 0, 0);
  248. #endif /* 0 */
  249. }
  250. static void vertical_decompose53iH0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
  251. int i;
  252. for(i=0; i<width; i++){
  253. b1[i] -= (b0[i] + b2[i])>>1;
  254. }
  255. }
  256. static void vertical_decompose53iL0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
  257. int i;
  258. for(i=0; i<width; i++){
  259. b1[i] += (b0[i] + b2[i] + 2)>>2;
  260. }
  261. }
  262. static void spatial_decompose53i(DWTELEM *buffer, int width, int height, int stride){
  263. int y;
  264. DWTELEM *b0= buffer + mirror(-2-1, height-1)*stride;
  265. DWTELEM *b1= buffer + mirror(-2 , height-1)*stride;
  266. for(y=-2; y<height; y+=2){
  267. DWTELEM *b2= buffer + mirror(y+1, height-1)*stride;
  268. DWTELEM *b3= buffer + mirror(y+2, height-1)*stride;
  269. if(y+1<(unsigned)height) horizontal_decompose53i(b2, width);
  270. if(y+2<(unsigned)height) horizontal_decompose53i(b3, width);
  271. if(y+1<(unsigned)height) vertical_decompose53iH0(b1, b2, b3, width);
  272. if(y+0<(unsigned)height) vertical_decompose53iL0(b0, b1, b2, width);
  273. b0=b2;
  274. b1=b3;
  275. }
  276. }
  277. static void horizontal_decompose97i(DWTELEM *b, int width){
  278. DWTELEM temp[width];
  279. const int w2= (width+1)>>1;
  280. lift (temp+w2, b +1, b , 1, 2, 2, width, W_AM, W_AO, W_AS, 1, 1);
  281. liftS(temp , b , temp+w2, 1, 2, 1, width, W_BM, W_BO, W_BS, 0, 0);
  282. lift (b +w2, temp+w2, temp , 1, 1, 1, width, W_CM, W_CO, W_CS, 1, 0);
  283. lift (b , temp , b +w2, 1, 1, 1, width, W_DM, W_DO, W_DS, 0, 0);
  284. }
  285. static void vertical_decompose97iH0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
  286. int i;
  287. for(i=0; i<width; i++){
  288. b1[i] -= (W_AM*(b0[i] + b2[i])+W_AO)>>W_AS;
  289. }
  290. }
  291. static void vertical_decompose97iH1(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
  292. int i;
  293. for(i=0; i<width; i++){
  294. b1[i] += (W_CM*(b0[i] + b2[i])+W_CO)>>W_CS;
  295. }
  296. }
  297. static void vertical_decompose97iL0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
  298. int i;
  299. for(i=0; i<width; i++){
  300. #ifdef liftS
  301. b1[i] -= (W_BM*(b0[i] + b2[i])+W_BO)>>W_BS;
  302. #else
  303. b1[i] = (16*4*b1[i] - 4*(b0[i] + b2[i]) + W_BO*5 + (5<<27)) / (5*16) - (1<<23);
  304. #endif
  305. }
  306. }
  307. static void vertical_decompose97iL1(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
  308. int i;
  309. for(i=0; i<width; i++){
  310. b1[i] += (W_DM*(b0[i] + b2[i])+W_DO)>>W_DS;
  311. }
  312. }
  313. static void spatial_decompose97i(DWTELEM *buffer, int width, int height, int stride){
  314. int y;
  315. DWTELEM *b0= buffer + mirror(-4-1, height-1)*stride;
  316. DWTELEM *b1= buffer + mirror(-4 , height-1)*stride;
  317. DWTELEM *b2= buffer + mirror(-4+1, height-1)*stride;
  318. DWTELEM *b3= buffer + mirror(-4+2, height-1)*stride;
  319. for(y=-4; y<height; y+=2){
  320. DWTELEM *b4= buffer + mirror(y+3, height-1)*stride;
  321. DWTELEM *b5= buffer + mirror(y+4, height-1)*stride;
  322. if(y+3<(unsigned)height) horizontal_decompose97i(b4, width);
  323. if(y+4<(unsigned)height) horizontal_decompose97i(b5, width);
  324. if(y+3<(unsigned)height) vertical_decompose97iH0(b3, b4, b5, width);
  325. if(y+2<(unsigned)height) vertical_decompose97iL0(b2, b3, b4, width);
  326. if(y+1<(unsigned)height) vertical_decompose97iH1(b1, b2, b3, width);
  327. if(y+0<(unsigned)height) vertical_decompose97iL1(b0, b1, b2, width);
  328. b0=b2;
  329. b1=b3;
  330. b2=b4;
  331. b3=b5;
  332. }
  333. }
  334. void ff_spatial_dwt(DWTELEM *buffer, int width, int height, int stride, int type, int decomposition_count){
  335. int level;
  336. for(level=0; level<decomposition_count; level++){
  337. switch(type){
  338. case DWT_97: spatial_decompose97i(buffer, width>>level, height>>level, stride<<level); break;
  339. case DWT_53: spatial_decompose53i(buffer, width>>level, height>>level, stride<<level); break;
  340. }
  341. }
  342. }
  343. static void horizontal_compose53i(IDWTELEM *b, int width){
  344. IDWTELEM temp[width];
  345. const int width2= width>>1;
  346. const int w2= (width+1)>>1;
  347. int x;
  348. for(x=0; x<width2; x++){
  349. temp[2*x ]= b[x ];
  350. temp[2*x + 1]= b[x+w2];
  351. }
  352. if(width&1)
  353. temp[2*x ]= b[x ];
  354. b[0] = temp[0] - ((temp[1]+1)>>1);
  355. for(x=2; x<width-1; x+=2){
  356. b[x ] = temp[x ] - ((temp[x-1] + temp[x+1]+2)>>2);
  357. b[x-1] = temp[x-1] + ((b [x-2] + b [x ]+1)>>1);
  358. }
  359. if(width&1){
  360. b[x ] = temp[x ] - ((temp[x-1]+1)>>1);
  361. b[x-1] = temp[x-1] + ((b [x-2] + b [x ]+1)>>1);
  362. }else
  363. b[x-1] = temp[x-1] + b[x-2];
  364. }
  365. static void vertical_compose53iH0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width){
  366. int i;
  367. for(i=0; i<width; i++){
  368. b1[i] += (b0[i] + b2[i])>>1;
  369. }
  370. }
  371. static void vertical_compose53iL0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width){
  372. int i;
  373. for(i=0; i<width; i++){
  374. b1[i] -= (b0[i] + b2[i] + 2)>>2;
  375. }
  376. }
  377. static void spatial_compose53i_buffered_init(DWTCompose *cs, slice_buffer * sb, int height, int stride_line){
  378. cs->b0 = slice_buffer_get_line(sb, mirror(-1-1, height-1) * stride_line);
  379. cs->b1 = slice_buffer_get_line(sb, mirror(-1 , height-1) * stride_line);
  380. cs->y = -1;
  381. }
  382. static void spatial_compose53i_init(DWTCompose *cs, IDWTELEM *buffer, int height, int stride){
  383. cs->b0 = buffer + mirror(-1-1, height-1)*stride;
  384. cs->b1 = buffer + mirror(-1 , height-1)*stride;
  385. cs->y = -1;
  386. }
  387. static void spatial_compose53i_dy_buffered(DWTCompose *cs, slice_buffer * sb, int width, int height, int stride_line){
  388. int y= cs->y;
  389. IDWTELEM *b0= cs->b0;
  390. IDWTELEM *b1= cs->b1;
  391. IDWTELEM *b2= slice_buffer_get_line(sb, mirror(y+1, height-1) * stride_line);
  392. IDWTELEM *b3= slice_buffer_get_line(sb, mirror(y+2, height-1) * stride_line);
  393. if(y+1<(unsigned)height && y<(unsigned)height){
  394. int x;
  395. for(x=0; x<width; x++){
  396. b2[x] -= (b1[x] + b3[x] + 2)>>2;
  397. b1[x] += (b0[x] + b2[x])>>1;
  398. }
  399. }else{
  400. if(y+1<(unsigned)height) vertical_compose53iL0(b1, b2, b3, width);
  401. if(y+0<(unsigned)height) vertical_compose53iH0(b0, b1, b2, width);
  402. }
  403. if(y-1<(unsigned)height) horizontal_compose53i(b0, width);
  404. if(y+0<(unsigned)height) horizontal_compose53i(b1, width);
  405. cs->b0 = b2;
  406. cs->b1 = b3;
  407. cs->y += 2;
  408. }
  409. static void spatial_compose53i_dy(DWTCompose *cs, IDWTELEM *buffer, int width, int height, int stride){
  410. int y= cs->y;
  411. IDWTELEM *b0= cs->b0;
  412. IDWTELEM *b1= cs->b1;
  413. IDWTELEM *b2= buffer + mirror(y+1, height-1)*stride;
  414. IDWTELEM *b3= buffer + mirror(y+2, height-1)*stride;
  415. if(y+1<(unsigned)height) vertical_compose53iL0(b1, b2, b3, width);
  416. if(y+0<(unsigned)height) vertical_compose53iH0(b0, b1, b2, width);
  417. if(y-1<(unsigned)height) horizontal_compose53i(b0, width);
  418. if(y+0<(unsigned)height) horizontal_compose53i(b1, width);
  419. cs->b0 = b2;
  420. cs->b1 = b3;
  421. cs->y += 2;
  422. }
  423. static void av_unused spatial_compose53i(IDWTELEM *buffer, int width, int height, int stride){
  424. DWTCompose cs;
  425. spatial_compose53i_init(&cs, buffer, height, stride);
  426. while(cs.y <= height)
  427. spatial_compose53i_dy(&cs, buffer, width, height, stride);
  428. }
  429. void ff_snow_horizontal_compose97i(IDWTELEM *b, int width){
  430. IDWTELEM temp[width];
  431. const int w2= (width+1)>>1;
  432. #if 0 //maybe more understadable but slower
  433. inv_lift (temp , b , b +w2, 2, 1, 1, width, W_DM, W_DO, W_DS, 0, 1);
  434. inv_lift (temp+1 , b +w2, temp , 2, 1, 2, width, W_CM, W_CO, W_CS, 1, 1);
  435. inv_liftS(b , temp , temp+1 , 2, 2, 2, width, W_BM, W_BO, W_BS, 0, 1);
  436. inv_lift (b+1 , temp+1 , b , 2, 2, 2, width, W_AM, W_AO, W_AS, 1, 0);
  437. #else
  438. int x;
  439. temp[0] = b[0] - ((3*b[w2]+2)>>2);
  440. for(x=1; x<(width>>1); x++){
  441. temp[2*x ] = b[x ] - ((3*(b [x+w2-1] + b[x+w2])+4)>>3);
  442. temp[2*x-1] = b[x+w2-1] - temp[2*x-2] - temp[2*x];
  443. }
  444. if(width&1){
  445. temp[2*x ] = b[x ] - ((3*b [x+w2-1]+2)>>2);
  446. temp[2*x-1] = b[x+w2-1] - temp[2*x-2] - temp[2*x];
  447. }else
  448. temp[2*x-1] = b[x+w2-1] - 2*temp[2*x-2];
  449. b[0] = temp[0] + ((2*temp[0] + temp[1]+4)>>3);
  450. for(x=2; x<width-1; x+=2){
  451. b[x ] = temp[x ] + ((4*temp[x ] + temp[x-1] + temp[x+1]+8)>>4);
  452. b[x-1] = temp[x-1] + ((3*(b [x-2] + b [x ] ))>>1);
  453. }
  454. if(width&1){
  455. b[x ] = temp[x ] + ((2*temp[x ] + temp[x-1]+4)>>3);
  456. b[x-1] = temp[x-1] + ((3*(b [x-2] + b [x ] ))>>1);
  457. }else
  458. b[x-1] = temp[x-1] + 3*b [x-2];
  459. #endif
  460. }
  461. static void vertical_compose97iH0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width){
  462. int i;
  463. for(i=0; i<width; i++){
  464. b1[i] += (W_AM*(b0[i] + b2[i])+W_AO)>>W_AS;
  465. }
  466. }
  467. static void vertical_compose97iH1(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width){
  468. int i;
  469. for(i=0; i<width; i++){
  470. b1[i] -= (W_CM*(b0[i] + b2[i])+W_CO)>>W_CS;
  471. }
  472. }
  473. static void vertical_compose97iL0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width){
  474. int i;
  475. for(i=0; i<width; i++){
  476. #ifdef liftS
  477. b1[i] += (W_BM*(b0[i] + b2[i])+W_BO)>>W_BS;
  478. #else
  479. b1[i] += (W_BM*(b0[i] + b2[i])+4*b1[i]+W_BO)>>W_BS;
  480. #endif
  481. }
  482. }
  483. static void vertical_compose97iL1(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width){
  484. int i;
  485. for(i=0; i<width; i++){
  486. b1[i] -= (W_DM*(b0[i] + b2[i])+W_DO)>>W_DS;
  487. }
  488. }
  489. void ff_snow_vertical_compose97i(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, IDWTELEM *b3, IDWTELEM *b4, IDWTELEM *b5, int width){
  490. int i;
  491. for(i=0; i<width; i++){
  492. b4[i] -= (W_DM*(b3[i] + b5[i])+W_DO)>>W_DS;
  493. b3[i] -= (W_CM*(b2[i] + b4[i])+W_CO)>>W_CS;
  494. #ifdef liftS
  495. b2[i] += (W_BM*(b1[i] + b3[i])+W_BO)>>W_BS;
  496. #else
  497. b2[i] += (W_BM*(b1[i] + b3[i])+4*b2[i]+W_BO)>>W_BS;
  498. #endif
  499. b1[i] += (W_AM*(b0[i] + b2[i])+W_AO)>>W_AS;
  500. }
  501. }
  502. static void spatial_compose97i_buffered_init(DWTCompose *cs, slice_buffer * sb, int height, int stride_line){
  503. cs->b0 = slice_buffer_get_line(sb, mirror(-3-1, height-1) * stride_line);
  504. cs->b1 = slice_buffer_get_line(sb, mirror(-3 , height-1) * stride_line);
  505. cs->b2 = slice_buffer_get_line(sb, mirror(-3+1, height-1) * stride_line);
  506. cs->b3 = slice_buffer_get_line(sb, mirror(-3+2, height-1) * stride_line);
  507. cs->y = -3;
  508. }
  509. static void spatial_compose97i_init(DWTCompose *cs, IDWTELEM *buffer, int height, int stride){
  510. cs->b0 = buffer + mirror(-3-1, height-1)*stride;
  511. cs->b1 = buffer + mirror(-3 , height-1)*stride;
  512. cs->b2 = buffer + mirror(-3+1, height-1)*stride;
  513. cs->b3 = buffer + mirror(-3+2, height-1)*stride;
  514. cs->y = -3;
  515. }
  516. static void spatial_compose97i_dy_buffered(DWTContext *dsp, DWTCompose *cs, slice_buffer * sb, int width, int height, int stride_line){
  517. int y = cs->y;
  518. IDWTELEM *b0= cs->b0;
  519. IDWTELEM *b1= cs->b1;
  520. IDWTELEM *b2= cs->b2;
  521. IDWTELEM *b3= cs->b3;
  522. IDWTELEM *b4= slice_buffer_get_line(sb, mirror(y + 3, height - 1) * stride_line);
  523. IDWTELEM *b5= slice_buffer_get_line(sb, mirror(y + 4, height - 1) * stride_line);
  524. if(y>0 && y+4<height){
  525. dsp->vertical_compose97i(b0, b1, b2, b3, b4, b5, width);
  526. }else{
  527. if(y+3<(unsigned)height) vertical_compose97iL1(b3, b4, b5, width);
  528. if(y+2<(unsigned)height) vertical_compose97iH1(b2, b3, b4, width);
  529. if(y+1<(unsigned)height) vertical_compose97iL0(b1, b2, b3, width);
  530. if(y+0<(unsigned)height) vertical_compose97iH0(b0, b1, b2, width);
  531. }
  532. if(y-1<(unsigned)height) dsp->horizontal_compose97i(b0, width);
  533. if(y+0<(unsigned)height) dsp->horizontal_compose97i(b1, width);
  534. cs->b0=b2;
  535. cs->b1=b3;
  536. cs->b2=b4;
  537. cs->b3=b5;
  538. cs->y += 2;
  539. }
  540. static void spatial_compose97i_dy(DWTCompose *cs, IDWTELEM *buffer, int width, int height, int stride){
  541. int y = cs->y;
  542. IDWTELEM *b0= cs->b0;
  543. IDWTELEM *b1= cs->b1;
  544. IDWTELEM *b2= cs->b2;
  545. IDWTELEM *b3= cs->b3;
  546. IDWTELEM *b4= buffer + mirror(y+3, height-1)*stride;
  547. IDWTELEM *b5= buffer + mirror(y+4, height-1)*stride;
  548. if(y+3<(unsigned)height) vertical_compose97iL1(b3, b4, b5, width);
  549. if(y+2<(unsigned)height) vertical_compose97iH1(b2, b3, b4, width);
  550. if(y+1<(unsigned)height) vertical_compose97iL0(b1, b2, b3, width);
  551. if(y+0<(unsigned)height) vertical_compose97iH0(b0, b1, b2, width);
  552. if(y-1<(unsigned)height) ff_snow_horizontal_compose97i(b0, width);
  553. if(y+0<(unsigned)height) ff_snow_horizontal_compose97i(b1, width);
  554. cs->b0=b2;
  555. cs->b1=b3;
  556. cs->b2=b4;
  557. cs->b3=b5;
  558. cs->y += 2;
  559. }
  560. static void av_unused spatial_compose97i(IDWTELEM *buffer, int width, int height, int stride){
  561. DWTCompose cs;
  562. spatial_compose97i_init(&cs, buffer, height, stride);
  563. while(cs.y <= height)
  564. spatial_compose97i_dy(&cs, buffer, width, height, stride);
  565. }
  566. void ff_spatial_idwt_buffered_init(DWTCompose *cs, slice_buffer * sb, int width, int height, int stride_line, int type, int decomposition_count){
  567. int level;
  568. for(level=decomposition_count-1; level>=0; level--){
  569. switch(type){
  570. case DWT_97: spatial_compose97i_buffered_init(cs+level, sb, height>>level, stride_line<<level); break;
  571. case DWT_53: spatial_compose53i_buffered_init(cs+level, sb, height>>level, stride_line<<level); break;
  572. }
  573. }
  574. }
  575. 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){
  576. const int support = type==1 ? 3 : 5;
  577. int level;
  578. if(type==2) return;
  579. for(level=decomposition_count-1; level>=0; level--){
  580. while(cs[level].y <= FFMIN((y>>level)+support, height>>level)){
  581. switch(type){
  582. case DWT_97: spatial_compose97i_dy_buffered(dsp, cs+level, slice_buf, width>>level, height>>level, stride_line<<level);
  583. break;
  584. case DWT_53: spatial_compose53i_dy_buffered(cs+level, slice_buf, width>>level, height>>level, stride_line<<level);
  585. break;
  586. }
  587. }
  588. }
  589. }
  590. static void ff_spatial_idwt_init(DWTCompose *cs, IDWTELEM *buffer, int width, int height, int stride, int type, int decomposition_count){
  591. int level;
  592. for(level=decomposition_count-1; level>=0; level--){
  593. switch(type){
  594. case DWT_97: spatial_compose97i_init(cs+level, buffer, height>>level, stride<<level); break;
  595. case DWT_53: spatial_compose53i_init(cs+level, buffer, height>>level, stride<<level); break;
  596. }
  597. }
  598. }
  599. static void ff_spatial_idwt_slice(DWTCompose *cs, IDWTELEM *buffer, int width, int height, int stride, int type, int decomposition_count, int y){
  600. const int support = type==1 ? 3 : 5;
  601. int level;
  602. if(type==2) return;
  603. for(level=decomposition_count-1; level>=0; level--){
  604. while(cs[level].y <= FFMIN((y>>level)+support, height>>level)){
  605. switch(type){
  606. case DWT_97: spatial_compose97i_dy(cs+level, buffer, width>>level, height>>level, stride<<level);
  607. break;
  608. case DWT_53: spatial_compose53i_dy(cs+level, buffer, width>>level, height>>level, stride<<level);
  609. break;
  610. }
  611. }
  612. }
  613. }
  614. void ff_spatial_idwt(IDWTELEM *buffer, int width, int height, int stride, int type, int decomposition_count){
  615. DWTCompose cs[MAX_DECOMPOSITIONS];
  616. int y;
  617. ff_spatial_idwt_init(cs, buffer, width, height, stride, type, decomposition_count);
  618. for(y=0; y<height; y+=4)
  619. ff_spatial_idwt_slice(cs, buffer, width, height, stride, type, decomposition_count, y);
  620. }
  621. static inline int w_c(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int w, int h, int type){
  622. int s, i, j;
  623. const int dec_count= w==8 ? 3 : 4;
  624. int tmp[32*32];
  625. int level, ori;
  626. static const int scale[2][2][4][4]={
  627. {
  628. {
  629. // 9/7 8x8 dec=3
  630. {268, 239, 239, 213},
  631. { 0, 224, 224, 152},
  632. { 0, 135, 135, 110},
  633. },{
  634. // 9/7 16x16 or 32x32 dec=4
  635. {344, 310, 310, 280},
  636. { 0, 320, 320, 228},
  637. { 0, 175, 175, 136},
  638. { 0, 129, 129, 102},
  639. }
  640. },{
  641. {
  642. // 5/3 8x8 dec=3
  643. {275, 245, 245, 218},
  644. { 0, 230, 230, 156},
  645. { 0, 138, 138, 113},
  646. },{
  647. // 5/3 16x16 or 32x32 dec=4
  648. {352, 317, 317, 286},
  649. { 0, 328, 328, 233},
  650. { 0, 180, 180, 140},
  651. { 0, 132, 132, 105},
  652. }
  653. }
  654. };
  655. for (i = 0; i < h; i++) {
  656. for (j = 0; j < w; j+=4) {
  657. tmp[32*i+j+0] = (pix1[j+0] - pix2[j+0])<<4;
  658. tmp[32*i+j+1] = (pix1[j+1] - pix2[j+1])<<4;
  659. tmp[32*i+j+2] = (pix1[j+2] - pix2[j+2])<<4;
  660. tmp[32*i+j+3] = (pix1[j+3] - pix2[j+3])<<4;
  661. }
  662. pix1 += line_size;
  663. pix2 += line_size;
  664. }
  665. ff_spatial_dwt(tmp, w, h, 32, type, dec_count);
  666. s=0;
  667. assert(w==h);
  668. for(level=0; level<dec_count; level++){
  669. for(ori= level ? 1 : 0; ori<4; ori++){
  670. int size= w>>(dec_count-level);
  671. int sx= (ori&1) ? size : 0;
  672. int stride= 32<<(dec_count-level);
  673. int sy= (ori&2) ? stride>>1 : 0;
  674. for(i=0; i<size; i++){
  675. for(j=0; j<size; j++){
  676. int v= tmp[sx + sy + i*stride + j] * scale[type][dec_count-3][level][ori];
  677. s += FFABS(v);
  678. }
  679. }
  680. }
  681. }
  682. assert(s>=0);
  683. return s>>9;
  684. }
  685. static int w53_8_c(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int h){
  686. return w_c(v, pix1, pix2, line_size, 8, h, 1);
  687. }
  688. static int w97_8_c(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int h){
  689. return w_c(v, pix1, pix2, line_size, 8, h, 0);
  690. }
  691. static int w53_16_c(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int h){
  692. return w_c(v, pix1, pix2, line_size, 16, h, 1);
  693. }
  694. static int w97_16_c(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int h){
  695. return w_c(v, pix1, pix2, line_size, 16, h, 0);
  696. }
  697. int ff_w53_32_c(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int h){
  698. return w_c(v, pix1, pix2, line_size, 32, h, 1);
  699. }
  700. int ff_w97_32_c(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int h){
  701. return w_c(v, pix1, pix2, line_size, 32, h, 0);
  702. }
  703. void ff_dsputil_init_dwt(DSPContext *c)
  704. {
  705. c->w53[0]= w53_16_c;
  706. c->w53[1]= w53_8_c;
  707. c->w97[0]= w97_16_c;
  708. c->w97[1]= w97_8_c;
  709. }
  710. void ff_dwt_init(DWTContext *c)
  711. {
  712. c->vertical_compose97i = ff_snow_vertical_compose97i;
  713. c->horizontal_compose97i = ff_snow_horizontal_compose97i;
  714. c->inner_add_yblock = ff_snow_inner_add_yblock;
  715. if (HAVE_MMX) ff_dwt_init_x86(c);
  716. }
  717. static av_always_inline
  718. void interleave(IDWTELEM *dst, IDWTELEM *src0, IDWTELEM *src1, int w2, int add, int shift)
  719. {
  720. int i;
  721. for (i = 0; i < w2; i++) {
  722. dst[2*i ] = (src0[i] + add) >> shift;
  723. dst[2*i+1] = (src1[i] + add) >> shift;
  724. }
  725. }
  726. static void horizontal_compose_dirac53i(IDWTELEM *b, IDWTELEM *temp, int w)
  727. {
  728. const int w2 = w >> 1;
  729. int x;
  730. temp[0] = COMPOSE_53iL0(b[w2], b[0], b[w2]);
  731. for (x = 1; x < w2; x++) {
  732. temp[x ] = COMPOSE_53iL0 (b[x+w2-1], b[x ], b[x+w2]);
  733. temp[x+w2-1] = COMPOSE_DIRAC53iH0(temp[x-1], b[x+w2-1], temp[x]);
  734. }
  735. temp[w-1] = COMPOSE_DIRAC53iH0(temp[w2-1], b[w-1], temp[w2-1]);
  736. interleave(b, temp, temp+w2, w2, 1, 1);
  737. }
  738. static void horizontal_compose_dd97i(IDWTELEM *b, IDWTELEM *tmp, int w)
  739. {
  740. const int w2 = w >> 1;
  741. int x;
  742. tmp[0] = COMPOSE_53iL0(b[w2], b[0], b[w2]);
  743. for (x = 1; x < w2; x++)
  744. tmp[x] = COMPOSE_53iL0(b[x+w2-1], b[x], b[x+w2]);
  745. // extend the edges
  746. tmp[-1] = tmp[0];
  747. tmp[w2+1] = tmp[w2] = tmp[w2-1];
  748. for (x = 0; x < w2; x++) {
  749. b[2*x ] = (tmp[x] + 1)>>1;
  750. b[2*x+1] = (COMPOSE_DD97iH0(tmp[x-1], tmp[x], b[x+w2], tmp[x+1], tmp[x+2]) + 1)>>1;
  751. }
  752. }
  753. static void horizontal_compose_dd137i(IDWTELEM *b, IDWTELEM *tmp, int w)
  754. {
  755. const int w2 = w >> 1;
  756. int x;
  757. tmp[0] = COMPOSE_DD137iL0(b[w2], b[w2], b[0], b[w2 ], b[w2+1]);
  758. tmp[1] = COMPOSE_DD137iL0(b[w2], b[w2], b[1], b[w2+1], b[w2+2]);
  759. for (x = 2; x < w2-1; x++)
  760. tmp[x] = COMPOSE_DD137iL0(b[x+w2-2], b[x+w2-1], b[x], b[x+w2], b[x+w2+1]);
  761. tmp[w2-1] = COMPOSE_DD137iL0(b[w-3], b[w-2], b[w2-1], b[w-1], b[w-1]);
  762. // extend the edges
  763. tmp[-1] = tmp[0];
  764. tmp[w2+1] = tmp[w2] = tmp[w2-1];
  765. for (x = 0; x < w2; x++) {
  766. b[2*x ] = (tmp[x] + 1)>>1;
  767. b[2*x+1] = (COMPOSE_DD97iH0(tmp[x-1], tmp[x], b[x+w2], tmp[x+1], tmp[x+2]) + 1)>>1;
  768. }
  769. }
  770. static av_always_inline
  771. void horizontal_compose_haari(IDWTELEM *b, IDWTELEM *temp, int w, int shift)
  772. {
  773. const int w2 = w >> 1;
  774. int x;
  775. for (x = 0; x < w2; x++) {
  776. temp[x ] = COMPOSE_HAARiL0(b[x ], b[x+w2]);
  777. temp[x+w2] = COMPOSE_HAARiH0(b[x+w2], temp[x]);
  778. }
  779. interleave(b, temp, temp+w2, w2, shift, shift);
  780. }
  781. static void horizontal_compose_haar0i(IDWTELEM *b, IDWTELEM *temp, int w)
  782. {
  783. horizontal_compose_haari(b, temp, w, 0);
  784. }
  785. static void horizontal_compose_haar1i(IDWTELEM *b, IDWTELEM *temp, int w)
  786. {
  787. horizontal_compose_haari(b, temp, w, 1);
  788. }
  789. static void horizontal_compose_fidelityi(IDWTELEM *b, IDWTELEM *tmp, int w)
  790. {
  791. const int w2 = w >> 1;
  792. int i, x;
  793. IDWTELEM v[8];
  794. for (x = 0; x < w2; x++) {
  795. for (i = 0; i < 8; i++)
  796. v[i] = b[av_clip(x-3+i, 0, w2-1)];
  797. tmp[x] = COMPOSE_FIDELITYiH0(v[0], v[1], v[2], v[3], b[x+w2], v[4], v[5], v[6], v[7]);
  798. }
  799. for (x = 0; x < w2; x++) {
  800. for (i = 0; i < 8; i++)
  801. v[i] = tmp[av_clip(x-4+i, 0, w2-1)];
  802. tmp[x+w2] = COMPOSE_FIDELITYiL0(v[0], v[1], v[2], v[3], b[x], v[4], v[5], v[6], v[7]);
  803. }
  804. interleave(b, tmp+w2, tmp, w2, 0, 0);
  805. }
  806. static void horizontal_compose_daub97i(IDWTELEM *b, IDWTELEM *temp, int w)
  807. {
  808. const int w2 = w >> 1;
  809. int x, b0, b1, b2;
  810. temp[0] = COMPOSE_DAUB97iL1(b[w2], b[0], b[w2]);
  811. for (x = 1; x < w2; x++) {
  812. temp[x ] = COMPOSE_DAUB97iL1(b[x+w2-1], b[x ], b[x+w2]);
  813. temp[x+w2-1] = COMPOSE_DAUB97iH1(temp[x-1], b[x+w2-1], temp[x]);
  814. }
  815. temp[w-1] = COMPOSE_DAUB97iH1(temp[w2-1], b[w-1], temp[w2-1]);
  816. // second stage combined with interleave and shift
  817. b0 = b2 = COMPOSE_DAUB97iL0(temp[w2], temp[0], temp[w2]);
  818. b[0] = (b0 + 1) >> 1;
  819. for (x = 1; x < w2; x++) {
  820. b2 = COMPOSE_DAUB97iL0(temp[x+w2-1], temp[x ], temp[x+w2]);
  821. b1 = COMPOSE_DAUB97iH0( b0, temp[x+w2-1], b2 );
  822. b[2*x-1] = (b1 + 1) >> 1;
  823. b[2*x ] = (b2 + 1) >> 1;
  824. b0 = b2;
  825. }
  826. b[w-1] = (COMPOSE_DAUB97iH0(b2, temp[w-1], b2) + 1) >> 1;
  827. }
  828. static void vertical_compose_dirac53iH0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width)
  829. {
  830. int i;
  831. for(i=0; i<width; i++){
  832. b1[i] = COMPOSE_DIRAC53iH0(b0[i], b1[i], b2[i]);
  833. }
  834. }
  835. static void vertical_compose_dd97iH0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2,
  836. IDWTELEM *b3, IDWTELEM *b4, int width)
  837. {
  838. int i;
  839. for(i=0; i<width; i++){
  840. b2[i] = COMPOSE_DD97iH0(b0[i], b1[i], b2[i], b3[i], b4[i]);
  841. }
  842. }
  843. static void vertical_compose_dd137iL0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2,
  844. IDWTELEM *b3, IDWTELEM *b4, int width)
  845. {
  846. int i;
  847. for(i=0; i<width; i++){
  848. b2[i] = COMPOSE_DD137iL0(b0[i], b1[i], b2[i], b3[i], b4[i]);
  849. }
  850. }
  851. static void vertical_compose_haar(IDWTELEM *b0, IDWTELEM *b1, int width)
  852. {
  853. int i;
  854. for (i = 0; i < width; i++) {
  855. b0[i] = COMPOSE_HAARiL0(b0[i], b1[i]);
  856. b1[i] = COMPOSE_HAARiH0(b1[i], b0[i]);
  857. }
  858. }
  859. static void vertical_compose_fidelityiH0(IDWTELEM *dst, IDWTELEM *b[8], int width)
  860. {
  861. int i;
  862. for(i=0; i<width; i++){
  863. 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]);
  864. }
  865. }
  866. static void vertical_compose_fidelityiL0(IDWTELEM *dst, IDWTELEM *b[8], int width)
  867. {
  868. int i;
  869. for(i=0; i<width; i++){
  870. 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]);
  871. }
  872. }
  873. static void vertical_compose_daub97iH0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width)
  874. {
  875. int i;
  876. for(i=0; i<width; i++){
  877. b1[i] = COMPOSE_DAUB97iH0(b0[i], b1[i], b2[i]);
  878. }
  879. }
  880. static void vertical_compose_daub97iH1(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width)
  881. {
  882. int i;
  883. for(i=0; i<width; i++){
  884. b1[i] = COMPOSE_DAUB97iH1(b0[i], b1[i], b2[i]);
  885. }
  886. }
  887. static void vertical_compose_daub97iL0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width)
  888. {
  889. int i;
  890. for(i=0; i<width; i++){
  891. b1[i] = COMPOSE_DAUB97iL0(b0[i], b1[i], b2[i]);
  892. }
  893. }
  894. static void vertical_compose_daub97iL1(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width)
  895. {
  896. int i;
  897. for(i=0; i<width; i++){
  898. b1[i] = COMPOSE_DAUB97iL1(b0[i], b1[i], b2[i]);
  899. }
  900. }
  901. static void spatial_compose_dd97i_dy(DWTContext *d, int level, int width, int height, int stride)
  902. {
  903. vertical_compose_3tap vertical_compose_l0 = d->vertical_compose_l0;
  904. vertical_compose_5tap vertical_compose_h0 = d->vertical_compose_h0;
  905. DWTCompose *cs = d->cs + level;
  906. int i, y = cs->y;
  907. IDWTELEM *b[8];
  908. for (i = 0; i < 6; i++)
  909. b[i] = cs->b[i];
  910. b[6] = d->buffer + av_clip(y+5, 0, height-2)*stride;
  911. b[7] = d->buffer + av_clip(y+6, 1, height-1)*stride;
  912. if(y+5<(unsigned)height) vertical_compose_l0( b[5], b[6], b[7], width);
  913. if(y+1<(unsigned)height) vertical_compose_h0(b[0], b[2], b[3], b[4], b[6], width);
  914. if(y-1<(unsigned)height) d->horizontal_compose(b[0], d->temp, width);
  915. if(y+0<(unsigned)height) d->horizontal_compose(b[1], d->temp, width);
  916. for (i = 0; i < 6; i++)
  917. cs->b[i] = b[i+2];
  918. cs->y += 2;
  919. }
  920. static void spatial_compose_dirac53i_dy(DWTContext *d, int level, int width, int height, int stride)
  921. {
  922. vertical_compose_3tap vertical_compose_l0 = d->vertical_compose_l0;
  923. vertical_compose_3tap vertical_compose_h0 = d->vertical_compose_h0;
  924. DWTCompose *cs = d->cs + level;
  925. int y= cs->y;
  926. IDWTELEM *b[4] = { cs->b[0], cs->b[1] };
  927. b[2] = d->buffer + mirror(y+1, height-1)*stride;
  928. b[3] = d->buffer + mirror(y+2, height-1)*stride;
  929. if(y+1<(unsigned)height) vertical_compose_l0(b[1], b[2], b[3], width);
  930. if(y+0<(unsigned)height) vertical_compose_h0(b[0], b[1], b[2], width);
  931. if(y-1<(unsigned)height) d->horizontal_compose(b[0], d->temp, width);
  932. if(y+0<(unsigned)height) d->horizontal_compose(b[1], d->temp, width);
  933. cs->b[0] = b[2];
  934. cs->b[1] = b[3];
  935. cs->y += 2;
  936. }
  937. static void spatial_compose_dd137i_dy(DWTContext *d, int level, int width, int height, int stride)
  938. {
  939. vertical_compose_5tap vertical_compose_l0 = d->vertical_compose_l0;
  940. vertical_compose_5tap vertical_compose_h0 = d->vertical_compose_h0;
  941. DWTCompose *cs = d->cs + level;
  942. int i, y = cs->y;
  943. IDWTELEM *b[10];
  944. for (i = 0; i < 8; i++)
  945. b[i] = cs->b[i];
  946. b[8] = d->buffer + av_clip(y+7, 0, height-2)*stride;
  947. b[9] = d->buffer + av_clip(y+8, 1, height-1)*stride;
  948. if(y+5<(unsigned)height) vertical_compose_l0(b[3], b[5], b[6], b[7], b[9], width);
  949. if(y+1<(unsigned)height) vertical_compose_h0(b[0], b[2], b[3], b[4], b[6], width);
  950. if(y-1<(unsigned)height) d->horizontal_compose(b[0], d->temp, width);
  951. if(y+0<(unsigned)height) d->horizontal_compose(b[1], d->temp, width);
  952. for (i = 0; i < 8; i++)
  953. cs->b[i] = b[i+2];
  954. cs->y += 2;
  955. }
  956. // haar makes the assumption that height is even (always true for dirac)
  957. static void spatial_compose_haari_dy(DWTContext *d, int level, int width, int height, int stride)
  958. {
  959. vertical_compose_2tap vertical_compose = d->vertical_compose;
  960. int y = d->cs[level].y;
  961. IDWTELEM *b0 = d->buffer + (y-1)*stride;
  962. IDWTELEM *b1 = d->buffer + (y )*stride;
  963. vertical_compose(b0, b1, width);
  964. d->horizontal_compose(b0, d->temp, width);
  965. d->horizontal_compose(b1, d->temp, width);
  966. d->cs[level].y += 2;
  967. }
  968. // Don't do sliced idwt for fidelity; the 9 tap filter makes it a bit annoying
  969. // Fortunately, this filter isn't used in practice.
  970. static void spatial_compose_fidelity(DWTContext *d, int level, int width, int height, int stride)
  971. {
  972. vertical_compose_9tap vertical_compose_l0 = d->vertical_compose_l0;
  973. vertical_compose_9tap vertical_compose_h0 = d->vertical_compose_h0;
  974. int i, y;
  975. IDWTELEM *b[8];
  976. for (y = 1; y < height; y += 2) {
  977. for (i = 0; i < 8; i++)
  978. b[i] = d->buffer + av_clip((y-7 + 2*i), 0, height-2)*stride;
  979. vertical_compose_h0(d->buffer + y*stride, b, width);
  980. }
  981. for (y = 0; y < height; y += 2) {
  982. for (i = 0; i < 8; i++)
  983. b[i] = d->buffer + av_clip((y-7 + 2*i), 1, height-1)*stride;
  984. vertical_compose_l0(d->buffer + y*stride, b, width);
  985. }
  986. for (y = 0; y < height; y++)
  987. d->horizontal_compose(d->buffer + y*stride, d->temp, width);
  988. d->cs[level].y = height+1;
  989. }
  990. static void spatial_compose_daub97i_dy(DWTContext *d, int level, int width, int height, int stride)
  991. {
  992. vertical_compose_3tap vertical_compose_l0 = d->vertical_compose_l0;
  993. vertical_compose_3tap vertical_compose_h0 = d->vertical_compose_h0;
  994. vertical_compose_3tap vertical_compose_l1 = d->vertical_compose_l1;
  995. vertical_compose_3tap vertical_compose_h1 = d->vertical_compose_h1;
  996. DWTCompose *cs = d->cs + level;
  997. int i, y = cs->y;
  998. IDWTELEM *b[6];
  999. for (i = 0; i < 4; i++)
  1000. b[i] = cs->b[i];
  1001. b[4] = d->buffer + mirror(y+3, height-1)*stride;
  1002. b[5] = d->buffer + mirror(y+4, height-1)*stride;
  1003. if(y+3<(unsigned)height) vertical_compose_l1(b[3], b[4], b[5], width);
  1004. if(y+2<(unsigned)height) vertical_compose_h1(b[2], b[3], b[4], width);
  1005. if(y+1<(unsigned)height) vertical_compose_l0(b[1], b[2], b[3], width);
  1006. if(y+0<(unsigned)height) vertical_compose_h0(b[0], b[1], b[2], width);
  1007. if(y-1<(unsigned)height) d->horizontal_compose(b[0], d->temp, width);
  1008. if(y+0<(unsigned)height) d->horizontal_compose(b[1], d->temp, width);
  1009. for (i = 0; i < 4; i++)
  1010. cs->b[i] = b[i+2];
  1011. cs->y += 2;
  1012. }
  1013. static void spatial_compose97i_init2(DWTCompose *cs, IDWTELEM *buffer, int height, int stride)
  1014. {
  1015. cs->b[0] = buffer + mirror(-3-1, height-1)*stride;
  1016. cs->b[1] = buffer + mirror(-3 , height-1)*stride;
  1017. cs->b[2] = buffer + mirror(-3+1, height-1)*stride;
  1018. cs->b[3] = buffer + mirror(-3+2, height-1)*stride;
  1019. cs->y = -3;
  1020. }
  1021. static void spatial_compose53i_init2(DWTCompose *cs, IDWTELEM *buffer, int height, int stride)
  1022. {
  1023. cs->b[0] = buffer + mirror(-1-1, height-1)*stride;
  1024. cs->b[1] = buffer + mirror(-1 , height-1)*stride;
  1025. cs->y = -1;
  1026. }
  1027. static void spatial_compose_dd97i_init(DWTCompose *cs, IDWTELEM *buffer, int height, int stride)
  1028. {
  1029. cs->b[0] = buffer + av_clip(-5-1, 0, height-2)*stride;
  1030. cs->b[1] = buffer + av_clip(-5 , 1, height-1)*stride;
  1031. cs->b[2] = buffer + av_clip(-5+1, 0, height-2)*stride;
  1032. cs->b[3] = buffer + av_clip(-5+2, 1, height-1)*stride;
  1033. cs->b[4] = buffer + av_clip(-5+3, 0, height-2)*stride;
  1034. cs->b[5] = buffer + av_clip(-5+4, 1, height-1)*stride;
  1035. cs->y = -5;
  1036. }
  1037. static void spatial_compose_dd137i_init(DWTCompose *cs, IDWTELEM *buffer, int height, int stride)
  1038. {
  1039. cs->b[0] = buffer + av_clip(-5-1, 0, height-2)*stride;
  1040. cs->b[1] = buffer + av_clip(-5 , 1, height-1)*stride;
  1041. cs->b[2] = buffer + av_clip(-5+1, 0, height-2)*stride;
  1042. cs->b[3] = buffer + av_clip(-5+2, 1, height-1)*stride;
  1043. cs->b[4] = buffer + av_clip(-5+3, 0, height-2)*stride;
  1044. cs->b[5] = buffer + av_clip(-5+4, 1, height-1)*stride;
  1045. cs->b[6] = buffer + av_clip(-5+5, 0, height-2)*stride;
  1046. cs->b[7] = buffer + av_clip(-5+6, 1, height-1)*stride;
  1047. cs->y = -5;
  1048. }
  1049. int ff_spatial_idwt_init2(DWTContext *d, IDWTELEM *buffer, int width, int height,
  1050. int stride, enum dwt_type type, int decomposition_count,
  1051. IDWTELEM *temp)
  1052. {
  1053. int level;
  1054. d->buffer = buffer;
  1055. d->width = width;
  1056. d->height = height;
  1057. d->stride = stride;
  1058. d->decomposition_count = decomposition_count;
  1059. d->temp = temp + 8;
  1060. for(level=decomposition_count-1; level>=0; level--){
  1061. int hl = height >> level;
  1062. int stride_l = stride << level;
  1063. switch(type){
  1064. case DWT_DIRAC_DD9_7:
  1065. spatial_compose_dd97i_init(d->cs+level, buffer, hl, stride_l);
  1066. break;
  1067. case DWT_DIRAC_LEGALL5_3:
  1068. spatial_compose53i_init2(d->cs+level, buffer, hl, stride_l);
  1069. break;
  1070. case DWT_DIRAC_DD13_7:
  1071. spatial_compose_dd137i_init(d->cs+level, buffer, hl, stride_l);
  1072. break;
  1073. case DWT_DIRAC_HAAR0:
  1074. case DWT_DIRAC_HAAR1:
  1075. d->cs[level].y = 1;
  1076. break;
  1077. case DWT_DIRAC_DAUB9_7:
  1078. spatial_compose97i_init2(d->cs+level, buffer, hl, stride_l);
  1079. break;
  1080. default:
  1081. d->cs[level].y = 0;
  1082. break;
  1083. }
  1084. }
  1085. switch (type) {
  1086. case DWT_DIRAC_DD9_7:
  1087. d->spatial_compose = spatial_compose_dd97i_dy;
  1088. d->vertical_compose_l0 = vertical_compose53iL0;
  1089. d->vertical_compose_h0 = vertical_compose_dd97iH0;
  1090. d->horizontal_compose = horizontal_compose_dd97i;
  1091. d->support = 7;
  1092. break;
  1093. case DWT_DIRAC_LEGALL5_3:
  1094. d->spatial_compose = spatial_compose_dirac53i_dy;
  1095. d->vertical_compose_l0 = vertical_compose53iL0;
  1096. d->vertical_compose_h0 = vertical_compose_dirac53iH0;
  1097. d->horizontal_compose = horizontal_compose_dirac53i;
  1098. d->support = 3;
  1099. break;
  1100. case DWT_DIRAC_DD13_7:
  1101. d->spatial_compose = spatial_compose_dd137i_dy;
  1102. d->vertical_compose_l0 = vertical_compose_dd137iL0;
  1103. d->vertical_compose_h0 = vertical_compose_dd97iH0;
  1104. d->horizontal_compose = horizontal_compose_dd137i;
  1105. d->support = 7;
  1106. break;
  1107. case DWT_DIRAC_HAAR0:
  1108. case DWT_DIRAC_HAAR1:
  1109. d->spatial_compose = spatial_compose_haari_dy;
  1110. d->vertical_compose = vertical_compose_haar;
  1111. if (type == DWT_DIRAC_HAAR0)
  1112. d->horizontal_compose = horizontal_compose_haar0i;
  1113. else
  1114. d->horizontal_compose = horizontal_compose_haar1i;
  1115. d->support = 1;
  1116. break;
  1117. case DWT_DIRAC_FIDELITY:
  1118. d->spatial_compose = spatial_compose_fidelity;
  1119. d->vertical_compose_l0 = vertical_compose_fidelityiL0;
  1120. d->vertical_compose_h0 = vertical_compose_fidelityiH0;
  1121. d->horizontal_compose = horizontal_compose_fidelityi;
  1122. break;
  1123. case DWT_DIRAC_DAUB9_7:
  1124. d->spatial_compose = spatial_compose_daub97i_dy;
  1125. d->vertical_compose_l0 = vertical_compose_daub97iL0;
  1126. d->vertical_compose_h0 = vertical_compose_daub97iH0;
  1127. d->vertical_compose_l1 = vertical_compose_daub97iL1;
  1128. d->vertical_compose_h1 = vertical_compose_daub97iH1;
  1129. d->horizontal_compose = horizontal_compose_daub97i;
  1130. d->support = 5;
  1131. break;
  1132. default:
  1133. av_log(NULL, AV_LOG_ERROR, "Unknown wavelet type %d\n", type);
  1134. return -1;
  1135. }
  1136. if (HAVE_MMX) ff_spatial_idwt_init_mmx(d, type);
  1137. return 0;
  1138. }
  1139. void ff_spatial_idwt_slice2(DWTContext *d, int y)
  1140. {
  1141. int level, support = d->support;
  1142. for (level = d->decomposition_count-1; level >= 0; level--) {
  1143. int wl = d->width >> level;
  1144. int hl = d->height >> level;
  1145. int stride_l = d->stride << level;
  1146. while (d->cs[level].y <= FFMIN((y>>level)+support, hl))
  1147. d->spatial_compose(d, level, wl, hl, stride_l);
  1148. }
  1149. }
  1150. int ff_spatial_idwt2(IDWTELEM *buffer, int width, int height, int stride,
  1151. enum dwt_type type, int decomposition_count, IDWTELEM *temp)
  1152. {
  1153. DWTContext d;
  1154. int y;
  1155. if (ff_spatial_idwt_init2(&d, buffer, width, height, stride, type, decomposition_count, temp))
  1156. return -1;
  1157. for (y = 0; y < d.height; y += 4)
  1158. ff_spatial_idwt_slice2(&d, y);
  1159. return 0;
  1160. }