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.

1421 lines
46KB

  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. static void av_unused spatial_compose53i(IDWTELEM *buffer, IDWTELEM *temp,
  400. int width, int height, int stride)
  401. {
  402. DWTCompose cs;
  403. spatial_compose53i_init(&cs, buffer, height, stride);
  404. while (cs.y <= height)
  405. spatial_compose53i_dy(&cs, buffer, temp, width, height, stride);
  406. }
  407. void ff_snow_horizontal_compose97i(IDWTELEM *b, IDWTELEM *temp, int width)
  408. {
  409. const int w2 = (width + 1) >> 1;
  410. int x;
  411. temp[0] = b[0] - ((3 * b[w2] + 2) >> 2);
  412. for (x = 1; x < (width >> 1); x++) {
  413. temp[2 * x] = b[x] - ((3 * (b[x + w2 - 1] + b[x + w2]) + 4) >> 3);
  414. temp[2 * x - 1] = b[x + w2 - 1] - temp[2 * x - 2] - temp[2 * x];
  415. }
  416. if (width & 1) {
  417. temp[2 * x] = b[x] - ((3 * b[x + w2 - 1] + 2) >> 2);
  418. temp[2 * x - 1] = b[x + w2 - 1] - temp[2 * x - 2] - temp[2 * x];
  419. } else
  420. temp[2 * x - 1] = b[x + w2 - 1] - 2 * temp[2 * x - 2];
  421. b[0] = temp[0] + ((2 * temp[0] + temp[1] + 4) >> 3);
  422. for (x = 2; x < width - 1; x += 2) {
  423. b[x] = temp[x] + ((4 * temp[x] + temp[x - 1] + temp[x + 1] + 8) >> 4);
  424. b[x - 1] = temp[x - 1] + ((3 * (b[x - 2] + b[x])) >> 1);
  425. }
  426. if (width & 1) {
  427. b[x] = temp[x] + ((2 * temp[x] + temp[x - 1] + 4) >> 3);
  428. b[x - 1] = temp[x - 1] + ((3 * (b[x - 2] + b[x])) >> 1);
  429. } else
  430. b[x - 1] = temp[x - 1] + 3 * b[x - 2];
  431. }
  432. static void vertical_compose97iH0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2,
  433. int width)
  434. {
  435. int i;
  436. for (i = 0; i < width; i++)
  437. b1[i] += (W_AM * (b0[i] + b2[i]) + W_AO) >> W_AS;
  438. }
  439. static void vertical_compose97iH1(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2,
  440. int width)
  441. {
  442. int i;
  443. for (i = 0; i < width; i++)
  444. b1[i] -= (W_CM * (b0[i] + b2[i]) + W_CO) >> W_CS;
  445. }
  446. static void vertical_compose97iL0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2,
  447. int width)
  448. {
  449. int i;
  450. for (i = 0; i < width; i++)
  451. b1[i] += (W_BM * (b0[i] + b2[i]) + 4 * b1[i] + W_BO) >> W_BS;
  452. }
  453. static void vertical_compose97iL1(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2,
  454. int width)
  455. {
  456. int i;
  457. for (i = 0; i < width; i++)
  458. b1[i] -= (W_DM * (b0[i] + b2[i]) + W_DO) >> W_DS;
  459. }
  460. void ff_snow_vertical_compose97i(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2,
  461. IDWTELEM *b3, IDWTELEM *b4, IDWTELEM *b5,
  462. int width)
  463. {
  464. int i;
  465. for (i = 0; i < width; i++) {
  466. b4[i] -= (W_DM * (b3[i] + b5[i]) + W_DO) >> W_DS;
  467. b3[i] -= (W_CM * (b2[i] + b4[i]) + W_CO) >> W_CS;
  468. b2[i] += (W_BM * (b1[i] + b3[i]) + 4 * b2[i] + W_BO) >> W_BS;
  469. b1[i] += (W_AM * (b0[i] + b2[i]) + W_AO) >> W_AS;
  470. }
  471. }
  472. static void spatial_compose97i_buffered_init(DWTCompose *cs, slice_buffer *sb,
  473. int height, int stride_line)
  474. {
  475. cs->b0 = slice_buffer_get_line(sb, mirror(-3 - 1, height - 1) * stride_line);
  476. cs->b1 = slice_buffer_get_line(sb, mirror(-3, height - 1) * stride_line);
  477. cs->b2 = slice_buffer_get_line(sb, mirror(-3 + 1, height - 1) * stride_line);
  478. cs->b3 = slice_buffer_get_line(sb, mirror(-3 + 2, height - 1) * stride_line);
  479. cs->y = -3;
  480. }
  481. static void spatial_compose97i_init(DWTCompose *cs, IDWTELEM *buffer, int height,
  482. int stride)
  483. {
  484. cs->b0 = buffer + mirror(-3 - 1, height - 1) * stride;
  485. cs->b1 = buffer + mirror(-3, height - 1) * stride;
  486. cs->b2 = buffer + mirror(-3 + 1, height - 1) * stride;
  487. cs->b3 = buffer + mirror(-3 + 2, height - 1) * stride;
  488. cs->y = -3;
  489. }
  490. static void spatial_compose97i_dy_buffered(DWTContext *dsp, DWTCompose *cs,
  491. slice_buffer * sb, IDWTELEM *temp,
  492. int width, int height,
  493. int stride_line)
  494. {
  495. int y = cs->y;
  496. IDWTELEM *b0 = cs->b0;
  497. IDWTELEM *b1 = cs->b1;
  498. IDWTELEM *b2 = cs->b2;
  499. IDWTELEM *b3 = cs->b3;
  500. IDWTELEM *b4 = slice_buffer_get_line(sb,
  501. mirror(y + 3, height - 1) *
  502. stride_line);
  503. IDWTELEM *b5 = slice_buffer_get_line(sb,
  504. mirror(y + 4, height - 1) *
  505. stride_line);
  506. if (y > 0 && y + 4 < height) {
  507. dsp->vertical_compose97i(b0, b1, b2, b3, b4, b5, width);
  508. } else {
  509. if (y + 3 < (unsigned)height)
  510. vertical_compose97iL1(b3, b4, b5, width);
  511. if (y + 2 < (unsigned)height)
  512. vertical_compose97iH1(b2, b3, b4, width);
  513. if (y + 1 < (unsigned)height)
  514. vertical_compose97iL0(b1, b2, b3, width);
  515. if (y + 0 < (unsigned)height)
  516. vertical_compose97iH0(b0, b1, b2, width);
  517. }
  518. if (y - 1 < (unsigned)height)
  519. dsp->horizontal_compose97i(b0, temp, width);
  520. if (y + 0 < (unsigned)height)
  521. dsp->horizontal_compose97i(b1, temp, width);
  522. cs->b0 = b2;
  523. cs->b1 = b3;
  524. cs->b2 = b4;
  525. cs->b3 = b5;
  526. cs->y += 2;
  527. }
  528. static void spatial_compose97i_dy(DWTCompose *cs, IDWTELEM *buffer,
  529. IDWTELEM *temp, int width, int height,
  530. int stride)
  531. {
  532. int y = cs->y;
  533. IDWTELEM *b0 = cs->b0;
  534. IDWTELEM *b1 = cs->b1;
  535. IDWTELEM *b2 = cs->b2;
  536. IDWTELEM *b3 = cs->b3;
  537. IDWTELEM *b4 = buffer + mirror(y + 3, height - 1) * stride;
  538. IDWTELEM *b5 = buffer + mirror(y + 4, height - 1) * stride;
  539. if (y + 3 < (unsigned)height)
  540. vertical_compose97iL1(b3, b4, b5, width);
  541. if (y + 2 < (unsigned)height)
  542. vertical_compose97iH1(b2, b3, b4, width);
  543. if (y + 1 < (unsigned)height)
  544. vertical_compose97iL0(b1, b2, b3, width);
  545. if (y + 0 < (unsigned)height)
  546. vertical_compose97iH0(b0, b1, b2, width);
  547. if (y - 1 < (unsigned)height)
  548. ff_snow_horizontal_compose97i(b0, temp, width);
  549. if (y + 0 < (unsigned)height)
  550. ff_snow_horizontal_compose97i(b1, temp, width);
  551. cs->b0 = b2;
  552. cs->b1 = b3;
  553. cs->b2 = b4;
  554. cs->b3 = b5;
  555. cs->y += 2;
  556. }
  557. static void av_unused spatial_compose97i(IDWTELEM *buffer, IDWTELEM *temp,
  558. int width, int height, int stride)
  559. {
  560. DWTCompose cs;
  561. spatial_compose97i_init(&cs, buffer, height, stride);
  562. while (cs.y <= height)
  563. spatial_compose97i_dy(&cs, buffer, temp, width, height, stride);
  564. }
  565. void ff_spatial_idwt_buffered_init(DWTCompose *cs, slice_buffer *sb, int width,
  566. int height, int stride_line, int type,
  567. int decomposition_count)
  568. {
  569. int level;
  570. for (level = decomposition_count - 1; level >= 0; level--) {
  571. switch (type) {
  572. case DWT_97:
  573. spatial_compose97i_buffered_init(cs + level, sb, height >> level,
  574. stride_line << level);
  575. break;
  576. case DWT_53:
  577. spatial_compose53i_buffered_init(cs + level, sb, height >> level,
  578. stride_line << level);
  579. break;
  580. }
  581. }
  582. }
  583. void ff_spatial_idwt_buffered_slice(DWTContext *dsp, DWTCompose *cs,
  584. slice_buffer *slice_buf, IDWTELEM *temp,
  585. int width, int height, int stride_line,
  586. int type, int decomposition_count, int y)
  587. {
  588. const int support = type == 1 ? 3 : 5;
  589. int level;
  590. if (type == 2)
  591. return;
  592. for (level = decomposition_count - 1; level >= 0; level--)
  593. while (cs[level].y <= FFMIN((y >> level) + support, height >> level)) {
  594. switch (type) {
  595. case DWT_97:
  596. spatial_compose97i_dy_buffered(dsp, cs + level, slice_buf, temp,
  597. width >> level,
  598. height >> level,
  599. stride_line << level);
  600. break;
  601. case DWT_53:
  602. spatial_compose53i_dy_buffered(cs + level, slice_buf, temp,
  603. width >> level,
  604. height >> level,
  605. stride_line << level);
  606. break;
  607. }
  608. }
  609. }
  610. static void ff_spatial_idwt_init(DWTCompose *cs, IDWTELEM *buffer, int width,
  611. int height, int stride, int type,
  612. int decomposition_count)
  613. {
  614. int level;
  615. for (level = decomposition_count - 1; level >= 0; level--) {
  616. switch (type) {
  617. case DWT_97:
  618. spatial_compose97i_init(cs + level, buffer, height >> level,
  619. stride << level);
  620. break;
  621. case DWT_53:
  622. spatial_compose53i_init(cs + level, buffer, height >> level,
  623. stride << level);
  624. break;
  625. }
  626. }
  627. }
  628. static void ff_spatial_idwt_slice(DWTCompose *cs, IDWTELEM *buffer,
  629. IDWTELEM *temp, int width, int height,
  630. int stride, int type,
  631. int decomposition_count, int y)
  632. {
  633. const int support = type == 1 ? 3 : 5;
  634. int level;
  635. if (type == 2)
  636. return;
  637. for (level = decomposition_count - 1; level >= 0; level--)
  638. while (cs[level].y <= FFMIN((y >> level) + support, height >> level)) {
  639. switch (type) {
  640. case DWT_97:
  641. spatial_compose97i_dy(cs + level, buffer, temp, width >> level,
  642. height >> level, stride << level);
  643. break;
  644. case DWT_53:
  645. spatial_compose53i_dy(cs + level, buffer, temp, width >> level,
  646. height >> level, stride << level);
  647. break;
  648. }
  649. }
  650. }
  651. void ff_spatial_idwt(IDWTELEM *buffer, IDWTELEM *temp, int width, int height,
  652. int stride, int type, int decomposition_count)
  653. {
  654. DWTCompose cs[MAX_DECOMPOSITIONS];
  655. int y;
  656. ff_spatial_idwt_init(cs, buffer, width, height, stride, type,
  657. decomposition_count);
  658. for (y = 0; y < height; y += 4)
  659. ff_spatial_idwt_slice(cs, buffer, temp, width, height, stride, type,
  660. decomposition_count, y);
  661. }
  662. static inline int w_c(void *v, uint8_t *pix1, uint8_t *pix2, int line_size,
  663. int w, int h, int type)
  664. {
  665. int s, i, j;
  666. const int dec_count = w == 8 ? 3 : 4;
  667. int tmp[32 * 32], tmp2[32];
  668. int level, ori;
  669. static const int scale[2][2][4][4] = {
  670. {
  671. { // 9/7 8x8 dec=3
  672. { 268, 239, 239, 213 },
  673. { 0, 224, 224, 152 },
  674. { 0, 135, 135, 110 },
  675. },
  676. { // 9/7 16x16 or 32x32 dec=4
  677. { 344, 310, 310, 280 },
  678. { 0, 320, 320, 228 },
  679. { 0, 175, 175, 136 },
  680. { 0, 129, 129, 102 },
  681. }
  682. },
  683. {
  684. { // 5/3 8x8 dec=3
  685. { 275, 245, 245, 218 },
  686. { 0, 230, 230, 156 },
  687. { 0, 138, 138, 113 },
  688. },
  689. { // 5/3 16x16 or 32x32 dec=4
  690. { 352, 317, 317, 286 },
  691. { 0, 328, 328, 233 },
  692. { 0, 180, 180, 140 },
  693. { 0, 132, 132, 105 },
  694. }
  695. }
  696. };
  697. for (i = 0; i < h; i++) {
  698. for (j = 0; j < w; j += 4) {
  699. tmp[32 * i + j + 0] = (pix1[j + 0] - pix2[j + 0]) << 4;
  700. tmp[32 * i + j + 1] = (pix1[j + 1] - pix2[j + 1]) << 4;
  701. tmp[32 * i + j + 2] = (pix1[j + 2] - pix2[j + 2]) << 4;
  702. tmp[32 * i + j + 3] = (pix1[j + 3] - pix2[j + 3]) << 4;
  703. }
  704. pix1 += line_size;
  705. pix2 += line_size;
  706. }
  707. ff_spatial_dwt(tmp, tmp2, w, h, 32, type, dec_count);
  708. s = 0;
  709. av_assert1(w == h);
  710. for (level = 0; level < dec_count; level++)
  711. for (ori = level ? 1 : 0; ori < 4; ori++) {
  712. int size = w >> (dec_count - level);
  713. int sx = (ori & 1) ? size : 0;
  714. int stride = 32 << (dec_count - level);
  715. int sy = (ori & 2) ? stride >> 1 : 0;
  716. for (i = 0; i < size; i++)
  717. for (j = 0; j < size; j++) {
  718. int v = tmp[sx + sy + i * stride + j] *
  719. scale[type][dec_count - 3][level][ori];
  720. s += FFABS(v);
  721. }
  722. }
  723. av_assert1(s >= 0);
  724. return s >> 9;
  725. }
  726. static int w53_8_c(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h)
  727. {
  728. return w_c(v, pix1, pix2, line_size, 8, h, 1);
  729. }
  730. static int w97_8_c(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h)
  731. {
  732. return w_c(v, pix1, pix2, line_size, 8, h, 0);
  733. }
  734. static int w53_16_c(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h)
  735. {
  736. return w_c(v, pix1, pix2, line_size, 16, h, 1);
  737. }
  738. static int w97_16_c(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h)
  739. {
  740. return w_c(v, pix1, pix2, line_size, 16, h, 0);
  741. }
  742. int ff_w53_32_c(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h)
  743. {
  744. return w_c(v, pix1, pix2, line_size, 32, h, 1);
  745. }
  746. int ff_w97_32_c(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h)
  747. {
  748. return w_c(v, pix1, pix2, line_size, 32, h, 0);
  749. }
  750. void ff_dsputil_init_dwt(DSPContext *c)
  751. {
  752. c->w53[0] = w53_16_c;
  753. c->w53[1] = w53_8_c;
  754. c->w97[0] = w97_16_c;
  755. c->w97[1] = w97_8_c;
  756. }
  757. void ff_dwt_init(DWTContext *c)
  758. {
  759. c->vertical_compose97i = ff_snow_vertical_compose97i;
  760. c->horizontal_compose97i = ff_snow_horizontal_compose97i;
  761. c->inner_add_yblock = ff_snow_inner_add_yblock;
  762. if (HAVE_MMX)
  763. ff_dwt_init_x86(c);
  764. }
  765. static av_always_inline
  766. void interleave(IDWTELEM *dst, IDWTELEM *src0, IDWTELEM *src1, int w2, int add, int shift)
  767. {
  768. int i;
  769. for (i = 0; i < w2; i++) {
  770. dst[2*i ] = (src0[i] + add) >> shift;
  771. dst[2*i+1] = (src1[i] + add) >> shift;
  772. }
  773. }
  774. static void horizontal_compose_dirac53i(IDWTELEM *b, IDWTELEM *temp, int w)
  775. {
  776. const int w2 = w >> 1;
  777. int x;
  778. temp[0] = COMPOSE_53iL0(b[w2], b[0], b[w2]);
  779. for (x = 1; x < w2; x++) {
  780. temp[x ] = COMPOSE_53iL0 (b[x+w2-1], b[x ], b[x+w2]);
  781. temp[x+w2-1] = COMPOSE_DIRAC53iH0(temp[x-1], b[x+w2-1], temp[x]);
  782. }
  783. temp[w-1] = COMPOSE_DIRAC53iH0(temp[w2-1], b[w-1], temp[w2-1]);
  784. interleave(b, temp, temp+w2, w2, 1, 1);
  785. }
  786. static void horizontal_compose_dd97i(IDWTELEM *b, IDWTELEM *tmp, int w)
  787. {
  788. const int w2 = w >> 1;
  789. int x;
  790. tmp[0] = COMPOSE_53iL0(b[w2], b[0], b[w2]);
  791. for (x = 1; x < w2; x++)
  792. tmp[x] = COMPOSE_53iL0(b[x+w2-1], b[x], b[x+w2]);
  793. // extend the edges
  794. tmp[-1] = tmp[0];
  795. tmp[w2+1] = tmp[w2] = tmp[w2-1];
  796. for (x = 0; x < w2; x++) {
  797. b[2*x ] = (tmp[x] + 1)>>1;
  798. b[2*x+1] = (COMPOSE_DD97iH0(tmp[x-1], tmp[x], b[x+w2], tmp[x+1], tmp[x+2]) + 1)>>1;
  799. }
  800. }
  801. static void horizontal_compose_dd137i(IDWTELEM *b, IDWTELEM *tmp, int w)
  802. {
  803. const int w2 = w >> 1;
  804. int x;
  805. tmp[0] = COMPOSE_DD137iL0(b[w2], b[w2], b[0], b[w2 ], b[w2+1]);
  806. tmp[1] = COMPOSE_DD137iL0(b[w2], b[w2], b[1], b[w2+1], b[w2+2]);
  807. for (x = 2; x < w2-1; x++)
  808. tmp[x] = COMPOSE_DD137iL0(b[x+w2-2], b[x+w2-1], b[x], b[x+w2], b[x+w2+1]);
  809. tmp[w2-1] = COMPOSE_DD137iL0(b[w-3], b[w-2], b[w2-1], b[w-1], b[w-1]);
  810. // extend the edges
  811. tmp[-1] = tmp[0];
  812. tmp[w2+1] = tmp[w2] = tmp[w2-1];
  813. for (x = 0; x < w2; x++) {
  814. b[2*x ] = (tmp[x] + 1)>>1;
  815. b[2*x+1] = (COMPOSE_DD97iH0(tmp[x-1], tmp[x], b[x+w2], tmp[x+1], tmp[x+2]) + 1)>>1;
  816. }
  817. }
  818. static av_always_inline
  819. void horizontal_compose_haari(IDWTELEM *b, IDWTELEM *temp, int w, int shift)
  820. {
  821. const int w2 = w >> 1;
  822. int x;
  823. for (x = 0; x < w2; x++) {
  824. temp[x ] = COMPOSE_HAARiL0(b[x ], b[x+w2]);
  825. temp[x+w2] = COMPOSE_HAARiH0(b[x+w2], temp[x]);
  826. }
  827. interleave(b, temp, temp+w2, w2, shift, shift);
  828. }
  829. static void horizontal_compose_haar0i(IDWTELEM *b, IDWTELEM *temp, int w)
  830. {
  831. horizontal_compose_haari(b, temp, w, 0);
  832. }
  833. static void horizontal_compose_haar1i(IDWTELEM *b, IDWTELEM *temp, int w)
  834. {
  835. horizontal_compose_haari(b, temp, w, 1);
  836. }
  837. static void horizontal_compose_fidelityi(IDWTELEM *b, IDWTELEM *tmp, int w)
  838. {
  839. const int w2 = w >> 1;
  840. int i, x;
  841. IDWTELEM v[8];
  842. for (x = 0; x < w2; x++) {
  843. for (i = 0; i < 8; i++)
  844. v[i] = b[av_clip(x-3+i, 0, w2-1)];
  845. tmp[x] = COMPOSE_FIDELITYiH0(v[0], v[1], v[2], v[3], b[x+w2], v[4], v[5], v[6], v[7]);
  846. }
  847. for (x = 0; x < w2; x++) {
  848. for (i = 0; i < 8; i++)
  849. v[i] = tmp[av_clip(x-4+i, 0, w2-1)];
  850. tmp[x+w2] = COMPOSE_FIDELITYiL0(v[0], v[1], v[2], v[3], b[x], v[4], v[5], v[6], v[7]);
  851. }
  852. interleave(b, tmp+w2, tmp, w2, 0, 0);
  853. }
  854. static void horizontal_compose_daub97i(IDWTELEM *b, IDWTELEM *temp, int w)
  855. {
  856. const int w2 = w >> 1;
  857. int x, b0, b1, b2;
  858. temp[0] = COMPOSE_DAUB97iL1(b[w2], b[0], b[w2]);
  859. for (x = 1; x < w2; x++) {
  860. temp[x ] = COMPOSE_DAUB97iL1(b[x+w2-1], b[x ], b[x+w2]);
  861. temp[x+w2-1] = COMPOSE_DAUB97iH1(temp[x-1], b[x+w2-1], temp[x]);
  862. }
  863. temp[w-1] = COMPOSE_DAUB97iH1(temp[w2-1], b[w-1], temp[w2-1]);
  864. // second stage combined with interleave and shift
  865. b0 = b2 = COMPOSE_DAUB97iL0(temp[w2], temp[0], temp[w2]);
  866. b[0] = (b0 + 1) >> 1;
  867. for (x = 1; x < w2; x++) {
  868. b2 = COMPOSE_DAUB97iL0(temp[x+w2-1], temp[x ], temp[x+w2]);
  869. b1 = COMPOSE_DAUB97iH0( b0, temp[x+w2-1], b2 );
  870. b[2*x-1] = (b1 + 1) >> 1;
  871. b[2*x ] = (b2 + 1) >> 1;
  872. b0 = b2;
  873. }
  874. b[w-1] = (COMPOSE_DAUB97iH0(b2, temp[w-1], b2) + 1) >> 1;
  875. }
  876. static void vertical_compose_dirac53iH0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width)
  877. {
  878. int i;
  879. for(i=0; i<width; i++){
  880. b1[i] = COMPOSE_DIRAC53iH0(b0[i], b1[i], b2[i]);
  881. }
  882. }
  883. static void vertical_compose_dd97iH0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2,
  884. IDWTELEM *b3, IDWTELEM *b4, int width)
  885. {
  886. int i;
  887. for(i=0; i<width; i++){
  888. b2[i] = COMPOSE_DD97iH0(b0[i], b1[i], b2[i], b3[i], b4[i]);
  889. }
  890. }
  891. static void vertical_compose_dd137iL0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2,
  892. IDWTELEM *b3, IDWTELEM *b4, int width)
  893. {
  894. int i;
  895. for(i=0; i<width; i++){
  896. b2[i] = COMPOSE_DD137iL0(b0[i], b1[i], b2[i], b3[i], b4[i]);
  897. }
  898. }
  899. static void vertical_compose_haar(IDWTELEM *b0, IDWTELEM *b1, int width)
  900. {
  901. int i;
  902. for (i = 0; i < width; i++) {
  903. b0[i] = COMPOSE_HAARiL0(b0[i], b1[i]);
  904. b1[i] = COMPOSE_HAARiH0(b1[i], b0[i]);
  905. }
  906. }
  907. static void vertical_compose_fidelityiH0(IDWTELEM *dst, IDWTELEM *b[8], int width)
  908. {
  909. int i;
  910. for(i=0; i<width; i++){
  911. 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]);
  912. }
  913. }
  914. static void vertical_compose_fidelityiL0(IDWTELEM *dst, IDWTELEM *b[8], int width)
  915. {
  916. int i;
  917. for(i=0; i<width; i++){
  918. 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]);
  919. }
  920. }
  921. static void vertical_compose_daub97iH0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width)
  922. {
  923. int i;
  924. for(i=0; i<width; i++){
  925. b1[i] = COMPOSE_DAUB97iH0(b0[i], b1[i], b2[i]);
  926. }
  927. }
  928. static void vertical_compose_daub97iH1(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width)
  929. {
  930. int i;
  931. for(i=0; i<width; i++){
  932. b1[i] = COMPOSE_DAUB97iH1(b0[i], b1[i], b2[i]);
  933. }
  934. }
  935. static void vertical_compose_daub97iL0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width)
  936. {
  937. int i;
  938. for(i=0; i<width; i++){
  939. b1[i] = COMPOSE_DAUB97iL0(b0[i], b1[i], b2[i]);
  940. }
  941. }
  942. static void vertical_compose_daub97iL1(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width)
  943. {
  944. int i;
  945. for(i=0; i<width; i++){
  946. b1[i] = COMPOSE_DAUB97iL1(b0[i], b1[i], b2[i]);
  947. }
  948. }
  949. static void spatial_compose_dd97i_dy(DWTContext *d, int level, int width, int height, int stride)
  950. {
  951. vertical_compose_3tap vertical_compose_l0 = (void*)d->vertical_compose_l0;
  952. vertical_compose_5tap vertical_compose_h0 = (void*)d->vertical_compose_h0;
  953. DWTCompose *cs = d->cs + level;
  954. int i, y = cs->y;
  955. IDWTELEM *b[8];
  956. for (i = 0; i < 6; i++)
  957. b[i] = cs->b[i];
  958. b[6] = d->buffer + av_clip(y+5, 0, height-2)*stride;
  959. b[7] = d->buffer + av_clip(y+6, 1, height-1)*stride;
  960. if(y+5<(unsigned)height) vertical_compose_l0( b[5], b[6], b[7], width);
  961. if(y+1<(unsigned)height) vertical_compose_h0(b[0], b[2], b[3], b[4], b[6], width);
  962. if(y-1<(unsigned)height) d->horizontal_compose(b[0], d->temp, width);
  963. if(y+0<(unsigned)height) d->horizontal_compose(b[1], d->temp, width);
  964. for (i = 0; i < 6; i++)
  965. cs->b[i] = b[i+2];
  966. cs->y += 2;
  967. }
  968. static void spatial_compose_dirac53i_dy(DWTContext *d, int level, int width, int height, int stride)
  969. {
  970. vertical_compose_3tap vertical_compose_l0 = (void*)d->vertical_compose_l0;
  971. vertical_compose_3tap vertical_compose_h0 = (void*)d->vertical_compose_h0;
  972. DWTCompose *cs = d->cs + level;
  973. int y= cs->y;
  974. IDWTELEM *b[4] = { cs->b[0], cs->b[1] };
  975. b[2] = d->buffer + mirror(y+1, height-1)*stride;
  976. b[3] = d->buffer + mirror(y+2, height-1)*stride;
  977. if(y+1<(unsigned)height) vertical_compose_l0(b[1], b[2], b[3], width);
  978. if(y+0<(unsigned)height) vertical_compose_h0(b[0], b[1], b[2], width);
  979. if(y-1<(unsigned)height) d->horizontal_compose(b[0], d->temp, width);
  980. if(y+0<(unsigned)height) d->horizontal_compose(b[1], d->temp, width);
  981. cs->b[0] = b[2];
  982. cs->b[1] = b[3];
  983. cs->y += 2;
  984. }
  985. static void spatial_compose_dd137i_dy(DWTContext *d, int level, int width, int height, int stride)
  986. {
  987. vertical_compose_5tap vertical_compose_l0 = (void*)d->vertical_compose_l0;
  988. vertical_compose_5tap vertical_compose_h0 = (void*)d->vertical_compose_h0;
  989. DWTCompose *cs = d->cs + level;
  990. int i, y = cs->y;
  991. IDWTELEM *b[10];
  992. for (i = 0; i < 8; i++)
  993. b[i] = cs->b[i];
  994. b[8] = d->buffer + av_clip(y+7, 0, height-2)*stride;
  995. b[9] = d->buffer + av_clip(y+8, 1, height-1)*stride;
  996. if(y+5<(unsigned)height) vertical_compose_l0(b[3], b[5], b[6], b[7], b[9], width);
  997. if(y+1<(unsigned)height) vertical_compose_h0(b[0], b[2], b[3], b[4], b[6], width);
  998. if(y-1<(unsigned)height) d->horizontal_compose(b[0], d->temp, width);
  999. if(y+0<(unsigned)height) d->horizontal_compose(b[1], d->temp, width);
  1000. for (i = 0; i < 8; i++)
  1001. cs->b[i] = b[i+2];
  1002. cs->y += 2;
  1003. }
  1004. // haar makes the assumption that height is even (always true for dirac)
  1005. static void spatial_compose_haari_dy(DWTContext *d, int level, int width, int height, int stride)
  1006. {
  1007. vertical_compose_2tap vertical_compose = (void*)d->vertical_compose;
  1008. int y = d->cs[level].y;
  1009. IDWTELEM *b0 = d->buffer + (y-1)*stride;
  1010. IDWTELEM *b1 = d->buffer + (y )*stride;
  1011. vertical_compose(b0, b1, width);
  1012. d->horizontal_compose(b0, d->temp, width);
  1013. d->horizontal_compose(b1, d->temp, width);
  1014. d->cs[level].y += 2;
  1015. }
  1016. // Don't do sliced idwt for fidelity; the 9 tap filter makes it a bit annoying
  1017. // Fortunately, this filter isn't used in practice.
  1018. static void spatial_compose_fidelity(DWTContext *d, int level, int width, int height, int stride)
  1019. {
  1020. vertical_compose_9tap vertical_compose_l0 = (void*)d->vertical_compose_l0;
  1021. vertical_compose_9tap vertical_compose_h0 = (void*)d->vertical_compose_h0;
  1022. int i, y;
  1023. IDWTELEM *b[8];
  1024. for (y = 1; y < height; y += 2) {
  1025. for (i = 0; i < 8; i++)
  1026. b[i] = d->buffer + av_clip((y-7 + 2*i), 0, height-2)*stride;
  1027. vertical_compose_h0(d->buffer + y*stride, b, width);
  1028. }
  1029. for (y = 0; y < height; y += 2) {
  1030. for (i = 0; i < 8; i++)
  1031. b[i] = d->buffer + av_clip((y-7 + 2*i), 1, height-1)*stride;
  1032. vertical_compose_l0(d->buffer + y*stride, b, width);
  1033. }
  1034. for (y = 0; y < height; y++)
  1035. d->horizontal_compose(d->buffer + y*stride, d->temp, width);
  1036. d->cs[level].y = height+1;
  1037. }
  1038. static void spatial_compose_daub97i_dy(DWTContext *d, int level, int width, int height, int stride)
  1039. {
  1040. vertical_compose_3tap vertical_compose_l0 = (void*)d->vertical_compose_l0;
  1041. vertical_compose_3tap vertical_compose_h0 = (void*)d->vertical_compose_h0;
  1042. vertical_compose_3tap vertical_compose_l1 = (void*)d->vertical_compose_l1;
  1043. vertical_compose_3tap vertical_compose_h1 = (void*)d->vertical_compose_h1;
  1044. DWTCompose *cs = d->cs + level;
  1045. int i, y = cs->y;
  1046. IDWTELEM *b[6];
  1047. for (i = 0; i < 4; i++)
  1048. b[i] = cs->b[i];
  1049. b[4] = d->buffer + mirror(y+3, height-1)*stride;
  1050. b[5] = d->buffer + mirror(y+4, height-1)*stride;
  1051. if(y+3<(unsigned)height) vertical_compose_l1(b[3], b[4], b[5], width);
  1052. if(y+2<(unsigned)height) vertical_compose_h1(b[2], b[3], b[4], width);
  1053. if(y+1<(unsigned)height) vertical_compose_l0(b[1], b[2], b[3], width);
  1054. if(y+0<(unsigned)height) vertical_compose_h0(b[0], b[1], b[2], width);
  1055. if(y-1<(unsigned)height) d->horizontal_compose(b[0], d->temp, width);
  1056. if(y+0<(unsigned)height) d->horizontal_compose(b[1], d->temp, width);
  1057. for (i = 0; i < 4; i++)
  1058. cs->b[i] = b[i+2];
  1059. cs->y += 2;
  1060. }
  1061. static void spatial_compose97i_init2(DWTCompose *cs, IDWTELEM *buffer, int height, int stride)
  1062. {
  1063. cs->b[0] = buffer + mirror(-3-1, height-1)*stride;
  1064. cs->b[1] = buffer + mirror(-3 , height-1)*stride;
  1065. cs->b[2] = buffer + mirror(-3+1, height-1)*stride;
  1066. cs->b[3] = buffer + mirror(-3+2, height-1)*stride;
  1067. cs->y = -3;
  1068. }
  1069. static void spatial_compose53i_init2(DWTCompose *cs, IDWTELEM *buffer, int height, int stride)
  1070. {
  1071. cs->b[0] = buffer + mirror(-1-1, height-1)*stride;
  1072. cs->b[1] = buffer + mirror(-1 , height-1)*stride;
  1073. cs->y = -1;
  1074. }
  1075. static void spatial_compose_dd97i_init(DWTCompose *cs, IDWTELEM *buffer, int height, int stride)
  1076. {
  1077. cs->b[0] = buffer + av_clip(-5-1, 0, height-2)*stride;
  1078. cs->b[1] = buffer + av_clip(-5 , 1, height-1)*stride;
  1079. cs->b[2] = buffer + av_clip(-5+1, 0, height-2)*stride;
  1080. cs->b[3] = buffer + av_clip(-5+2, 1, height-1)*stride;
  1081. cs->b[4] = buffer + av_clip(-5+3, 0, height-2)*stride;
  1082. cs->b[5] = buffer + av_clip(-5+4, 1, height-1)*stride;
  1083. cs->y = -5;
  1084. }
  1085. static void spatial_compose_dd137i_init(DWTCompose *cs, IDWTELEM *buffer, int height, int stride)
  1086. {
  1087. cs->b[0] = buffer + av_clip(-5-1, 0, height-2)*stride;
  1088. cs->b[1] = buffer + av_clip(-5 , 1, height-1)*stride;
  1089. cs->b[2] = buffer + av_clip(-5+1, 0, height-2)*stride;
  1090. cs->b[3] = buffer + av_clip(-5+2, 1, height-1)*stride;
  1091. cs->b[4] = buffer + av_clip(-5+3, 0, height-2)*stride;
  1092. cs->b[5] = buffer + av_clip(-5+4, 1, height-1)*stride;
  1093. cs->b[6] = buffer + av_clip(-5+5, 0, height-2)*stride;
  1094. cs->b[7] = buffer + av_clip(-5+6, 1, height-1)*stride;
  1095. cs->y = -5;
  1096. }
  1097. int ff_spatial_idwt_init2(DWTContext *d, IDWTELEM *buffer, int width, int height,
  1098. int stride, enum dwt_type type, int decomposition_count,
  1099. IDWTELEM *temp)
  1100. {
  1101. int level;
  1102. d->buffer = buffer;
  1103. d->width = width;
  1104. d->height = height;
  1105. d->stride = stride;
  1106. d->decomposition_count = decomposition_count;
  1107. d->temp = temp + 8;
  1108. for(level=decomposition_count-1; level>=0; level--){
  1109. int hl = height >> level;
  1110. int stride_l = stride << level;
  1111. switch(type){
  1112. case DWT_DIRAC_DD9_7:
  1113. spatial_compose_dd97i_init(d->cs+level, buffer, hl, stride_l);
  1114. break;
  1115. case DWT_DIRAC_LEGALL5_3:
  1116. spatial_compose53i_init2(d->cs+level, buffer, hl, stride_l);
  1117. break;
  1118. case DWT_DIRAC_DD13_7:
  1119. spatial_compose_dd137i_init(d->cs+level, buffer, hl, stride_l);
  1120. break;
  1121. case DWT_DIRAC_HAAR0:
  1122. case DWT_DIRAC_HAAR1:
  1123. d->cs[level].y = 1;
  1124. break;
  1125. case DWT_DIRAC_DAUB9_7:
  1126. spatial_compose97i_init2(d->cs+level, buffer, hl, stride_l);
  1127. break;
  1128. default:
  1129. d->cs[level].y = 0;
  1130. break;
  1131. }
  1132. }
  1133. switch (type) {
  1134. case DWT_DIRAC_DD9_7:
  1135. d->spatial_compose = spatial_compose_dd97i_dy;
  1136. d->vertical_compose_l0 = (void*)vertical_compose53iL0;
  1137. d->vertical_compose_h0 = (void*)vertical_compose_dd97iH0;
  1138. d->horizontal_compose = horizontal_compose_dd97i;
  1139. d->support = 7;
  1140. break;
  1141. case DWT_DIRAC_LEGALL5_3:
  1142. d->spatial_compose = spatial_compose_dirac53i_dy;
  1143. d->vertical_compose_l0 = (void*)vertical_compose53iL0;
  1144. d->vertical_compose_h0 = (void*)vertical_compose_dirac53iH0;
  1145. d->horizontal_compose = horizontal_compose_dirac53i;
  1146. d->support = 3;
  1147. break;
  1148. case DWT_DIRAC_DD13_7:
  1149. d->spatial_compose = spatial_compose_dd137i_dy;
  1150. d->vertical_compose_l0 = (void*)vertical_compose_dd137iL0;
  1151. d->vertical_compose_h0 = (void*)vertical_compose_dd97iH0;
  1152. d->horizontal_compose = horizontal_compose_dd137i;
  1153. d->support = 7;
  1154. break;
  1155. case DWT_DIRAC_HAAR0:
  1156. case DWT_DIRAC_HAAR1:
  1157. d->spatial_compose = spatial_compose_haari_dy;
  1158. d->vertical_compose = (void*)vertical_compose_haar;
  1159. if (type == DWT_DIRAC_HAAR0)
  1160. d->horizontal_compose = horizontal_compose_haar0i;
  1161. else
  1162. d->horizontal_compose = horizontal_compose_haar1i;
  1163. d->support = 1;
  1164. break;
  1165. case DWT_DIRAC_FIDELITY:
  1166. d->spatial_compose = spatial_compose_fidelity;
  1167. d->vertical_compose_l0 = (void*)vertical_compose_fidelityiL0;
  1168. d->vertical_compose_h0 = (void*)vertical_compose_fidelityiH0;
  1169. d->horizontal_compose = horizontal_compose_fidelityi;
  1170. break;
  1171. case DWT_DIRAC_DAUB9_7:
  1172. d->spatial_compose = spatial_compose_daub97i_dy;
  1173. d->vertical_compose_l0 = (void*)vertical_compose_daub97iL0;
  1174. d->vertical_compose_h0 = (void*)vertical_compose_daub97iH0;
  1175. d->vertical_compose_l1 = (void*)vertical_compose_daub97iL1;
  1176. d->vertical_compose_h1 = (void*)vertical_compose_daub97iH1;
  1177. d->horizontal_compose = horizontal_compose_daub97i;
  1178. d->support = 5;
  1179. break;
  1180. default:
  1181. av_log(NULL, AV_LOG_ERROR, "Unknown wavelet type %d\n", type);
  1182. return -1;
  1183. }
  1184. if (HAVE_MMX) ff_spatial_idwt_init_mmx(d, type);
  1185. return 0;
  1186. }
  1187. void ff_spatial_idwt_slice2(DWTContext *d, int y)
  1188. {
  1189. int level, support = d->support;
  1190. for (level = d->decomposition_count-1; level >= 0; level--) {
  1191. int wl = d->width >> level;
  1192. int hl = d->height >> level;
  1193. int stride_l = d->stride << level;
  1194. while (d->cs[level].y <= FFMIN((y>>level)+support, hl))
  1195. d->spatial_compose(d, level, wl, hl, stride_l);
  1196. }
  1197. }
  1198. int ff_spatial_idwt2(IDWTELEM *buffer, int width, int height, int stride,
  1199. enum dwt_type type, int decomposition_count, IDWTELEM *temp)
  1200. {
  1201. DWTContext d;
  1202. int y;
  1203. if (ff_spatial_idwt_init2(&d, buffer, width, height, stride, type, decomposition_count, temp))
  1204. return -1;
  1205. for (y = 0; y < d.height; y += 4)
  1206. ff_spatial_idwt_slice2(&d, y);
  1207. return 0;
  1208. }