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.

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