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.

1385 lines
44KB

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