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.

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