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.

1403 lines
45KB

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