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.

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