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.

1540 lines
50KB

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