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.

1537 lines
49KB

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