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.

982 lines
32KB

  1. /*
  2. * Copyright (C) 2004-2010 Michael Niedermayer <michaelni@gmx.at>
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * Libav is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with Libav; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "libavutil/attributes.h"
  21. #include "dsputil.h"
  22. #include "dwt.h"
  23. void ff_slice_buffer_init(slice_buffer *buf, int line_count,
  24. int max_allocated_lines, int line_width,
  25. IDWTELEM *base_buffer)
  26. {
  27. int i;
  28. buf->base_buffer = base_buffer;
  29. buf->line_count = line_count;
  30. buf->line_width = line_width;
  31. buf->data_count = max_allocated_lines;
  32. buf->line = av_mallocz(sizeof(IDWTELEM *) * line_count);
  33. buf->data_stack = av_malloc(sizeof(IDWTELEM *) * max_allocated_lines);
  34. for (i = 0; i < max_allocated_lines; i++)
  35. buf->data_stack[i] = av_malloc(sizeof(IDWTELEM) * line_width);
  36. buf->data_stack_top = max_allocated_lines - 1;
  37. }
  38. IDWTELEM *ff_slice_buffer_load_line(slice_buffer *buf, int line)
  39. {
  40. IDWTELEM *buffer;
  41. assert(buf->data_stack_top >= 0);
  42. // assert(!buf->line[line]);
  43. if (buf->line[line])
  44. return buf->line[line];
  45. buffer = buf->data_stack[buf->data_stack_top];
  46. buf->data_stack_top--;
  47. buf->line[line] = buffer;
  48. return buffer;
  49. }
  50. void ff_slice_buffer_release(slice_buffer *buf, int line)
  51. {
  52. IDWTELEM *buffer;
  53. assert(line >= 0 && line < buf->line_count);
  54. assert(buf->line[line]);
  55. buffer = buf->line[line];
  56. buf->data_stack_top++;
  57. buf->data_stack[buf->data_stack_top] = buffer;
  58. buf->line[line] = NULL;
  59. }
  60. void ff_slice_buffer_flush(slice_buffer *buf)
  61. {
  62. int i;
  63. for (i = 0; i < buf->line_count; i++)
  64. if (buf->line[i])
  65. ff_slice_buffer_release(buf, i);
  66. }
  67. void ff_slice_buffer_destroy(slice_buffer *buf)
  68. {
  69. int i;
  70. ff_slice_buffer_flush(buf);
  71. for (i = buf->data_count - 1; i >= 0; i--)
  72. av_freep(&buf->data_stack[i]);
  73. av_freep(&buf->data_stack);
  74. av_freep(&buf->line);
  75. }
  76. static inline int mirror(int v, int m)
  77. {
  78. while ((unsigned)v > (unsigned)m) {
  79. v = -v;
  80. if (v < 0)
  81. v += 2 * m;
  82. }
  83. return v;
  84. }
  85. static av_always_inline void lift(DWTELEM *dst, DWTELEM *src, DWTELEM *ref,
  86. int dst_step, int src_step, int ref_step,
  87. int width, int mul, int add, int shift,
  88. int highpass, int inverse)
  89. {
  90. const int mirror_left = !highpass;
  91. const int mirror_right = (width & 1) ^ highpass;
  92. const int w = (width >> 1) - 1 + (highpass & width);
  93. int i;
  94. #define LIFT(src, ref, inv) ((src) + ((inv) ? -(ref) : +(ref)))
  95. if (mirror_left) {
  96. dst[0] = LIFT(src[0], ((mul * 2 * ref[0] + add) >> shift), inverse);
  97. dst += dst_step;
  98. src += src_step;
  99. }
  100. for (i = 0; i < w; i++)
  101. dst[i * dst_step] = LIFT(src[i * src_step],
  102. ((mul * (ref[i * ref_step] +
  103. ref[(i + 1) * ref_step]) +
  104. add) >> shift),
  105. inverse);
  106. if (mirror_right)
  107. dst[w * dst_step] = LIFT(src[w * src_step],
  108. ((mul * 2 * ref[w * ref_step] + add) >> shift),
  109. inverse);
  110. }
  111. static av_always_inline void inv_lift(IDWTELEM *dst, IDWTELEM *src, IDWTELEM *ref,
  112. int dst_step, int src_step, int ref_step,
  113. int width, int mul, int add, int shift,
  114. int highpass, int inverse)
  115. {
  116. const int mirror_left = !highpass;
  117. const int mirror_right = (width & 1) ^ highpass;
  118. const int w = (width >> 1) - 1 + (highpass & width);
  119. int i;
  120. #define LIFT(src, ref, inv) ((src) + ((inv) ? -(ref) : +(ref)))
  121. if (mirror_left) {
  122. dst[0] = LIFT(src[0], ((mul * 2 * ref[0] + add) >> shift), inverse);
  123. dst += dst_step;
  124. src += src_step;
  125. }
  126. for (i = 0; i < w; i++)
  127. dst[i * dst_step] = LIFT(src[i * src_step],
  128. ((mul * (ref[i * ref_step] +
  129. ref[(i + 1) * ref_step]) +
  130. add) >> shift),
  131. inverse);
  132. if (mirror_right) {
  133. dst[w * dst_step] = LIFT(src[w * src_step],
  134. ((mul * 2 * ref[w * ref_step] + add) >> shift),
  135. inverse);
  136. }
  137. }
  138. #ifndef liftS
  139. static av_always_inline void liftS(DWTELEM *dst, DWTELEM *src, DWTELEM *ref,
  140. int dst_step, int src_step, int ref_step,
  141. int width, int mul, int add, int shift,
  142. int highpass, int inverse)
  143. {
  144. const int mirror_left = !highpass;
  145. const int mirror_right = (width & 1) ^ highpass;
  146. const int w = (width >> 1) - 1 + (highpass & width);
  147. int i;
  148. assert(shift == 4);
  149. #define LIFTS(src, ref, inv) \
  150. ((inv) ? (src) + (((ref) + 4 * (src)) >> shift) \
  151. : -((-16 * (src) + (ref) + add / \
  152. 4 + 1 + (5 << 25)) / (5 * 4) - (1 << 23)))
  153. if (mirror_left) {
  154. dst[0] = LIFTS(src[0], mul * 2 * ref[0] + add, inverse);
  155. dst += dst_step;
  156. src += src_step;
  157. }
  158. for (i = 0; i < w; i++)
  159. dst[i * dst_step] = LIFTS(src[i * src_step],
  160. mul * (ref[i * ref_step] +
  161. ref[(i + 1) * ref_step]) + add,
  162. inverse);
  163. if (mirror_right)
  164. dst[w * dst_step] = LIFTS(src[w * src_step],
  165. mul * 2 * ref[w * ref_step] + add,
  166. inverse);
  167. }
  168. static av_always_inline void inv_liftS(IDWTELEM *dst, IDWTELEM *src,
  169. IDWTELEM *ref, int dst_step,
  170. int src_step, int ref_step,
  171. int width, int mul, int add, int shift,
  172. int highpass, int inverse)
  173. {
  174. const int mirror_left = !highpass;
  175. const int mirror_right = (width & 1) ^ highpass;
  176. const int w = (width >> 1) - 1 + (highpass & width);
  177. int i;
  178. assert(shift == 4);
  179. #define LIFTS(src, ref, inv) \
  180. ((inv) ? (src) + (((ref) + 4 * (src)) >> shift) \
  181. : -((-16 * (src) + (ref) + add / \
  182. 4 + 1 + (5 << 25)) / (5 * 4) - (1 << 23)))
  183. if (mirror_left) {
  184. dst[0] = LIFTS(src[0], mul * 2 * ref[0] + add, inverse);
  185. dst += dst_step;
  186. src += src_step;
  187. }
  188. for (i = 0; i < w; i++)
  189. dst[i * dst_step] = LIFTS(src[i * src_step],
  190. mul * (ref[i * ref_step] +
  191. ref[(i + 1) * ref_step]) + add,
  192. inverse);
  193. if (mirror_right)
  194. dst[w * dst_step] = LIFTS(src[w * src_step],
  195. mul * 2 * ref[w * ref_step] + add, inverse);
  196. }
  197. #endif /* ! liftS */
  198. static void horizontal_decompose53i(DWTELEM *b, int width)
  199. {
  200. DWTELEM temp[width];
  201. const int width2 = width >> 1;
  202. int x;
  203. const int w2 = (width + 1) >> 1;
  204. for (x = 0; x < width2; x++) {
  205. temp[x] = b[2 * x];
  206. temp[x + w2] = b[2 * x + 1];
  207. }
  208. if (width & 1)
  209. temp[x] = b[2 * x];
  210. #if 0
  211. {
  212. int A1, A2, A3, A4;
  213. A2 = temp[1];
  214. A4 = temp[0];
  215. A1 = temp[0 + width2];
  216. A1 -= (A2 + A4) >> 1;
  217. A4 += (A1 + 1) >> 1;
  218. b[0 + width2] = A1;
  219. b[0] = A4;
  220. for (x = 1; x + 1 < width2; x += 2) {
  221. A3 = temp[x + width2];
  222. A4 = temp[x + 1];
  223. A3 -= (A2 + A4) >> 1;
  224. A2 += (A1 + A3 + 2) >> 2;
  225. b[x + width2] = A3;
  226. b[x] = A2;
  227. A1 = temp[x + 1 + width2];
  228. A2 = temp[x + 2];
  229. A1 -= (A2 + A4) >> 1;
  230. A4 += (A1 + A3 + 2) >> 2;
  231. b[x + 1 + width2] = A1;
  232. b[x + 1] = A4;
  233. }
  234. A3 = temp[width - 1];
  235. A3 -= A2;
  236. A2 += (A1 + A3 + 2) >> 2;
  237. b[width - 1] = A3;
  238. b[width2 - 1] = A2;
  239. }
  240. #else
  241. lift(b + w2, temp + w2, temp, 1, 1, 1, width, -1, 0, 1, 1, 0);
  242. lift(b, temp, b + w2, 1, 1, 1, width, 1, 2, 2, 0, 0);
  243. #endif /* 0 */
  244. }
  245. static void vertical_decompose53iH0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2,
  246. int width)
  247. {
  248. int i;
  249. for (i = 0; i < width; i++)
  250. b1[i] -= (b0[i] + b2[i]) >> 1;
  251. }
  252. static void vertical_decompose53iL0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2,
  253. int width)
  254. {
  255. int i;
  256. for (i = 0; i < width; i++)
  257. b1[i] += (b0[i] + b2[i] + 2) >> 2;
  258. }
  259. static void spatial_decompose53i(DWTELEM *buffer, int width, int height,
  260. int stride)
  261. {
  262. int y;
  263. DWTELEM *b0 = buffer + mirror(-2 - 1, height - 1) * stride;
  264. DWTELEM *b1 = buffer + mirror(-2, height - 1) * stride;
  265. for (y = -2; y < height; y += 2) {
  266. DWTELEM *b2 = buffer + mirror(y + 1, height - 1) * stride;
  267. DWTELEM *b3 = buffer + mirror(y + 2, height - 1) * stride;
  268. if (y + 1 < (unsigned)height)
  269. horizontal_decompose53i(b2, width);
  270. if (y + 2 < (unsigned)height)
  271. horizontal_decompose53i(b3, width);
  272. if (y + 1 < (unsigned)height)
  273. vertical_decompose53iH0(b1, b2, b3, width);
  274. if (y + 0 < (unsigned)height)
  275. vertical_decompose53iL0(b0, b1, b2, width);
  276. b0 = b2;
  277. b1 = b3;
  278. }
  279. }
  280. static void horizontal_decompose97i(DWTELEM *b, int width)
  281. {
  282. DWTELEM temp[width];
  283. const int w2 = (width + 1) >> 1;
  284. lift(temp + w2, b + 1, b, 1, 2, 2, width, W_AM, W_AO, W_AS, 1, 1);
  285. liftS(temp, b, temp + w2, 1, 2, 1, width, W_BM, W_BO, W_BS, 0, 0);
  286. lift(b + w2, temp + w2, temp, 1, 1, 1, width, W_CM, W_CO, W_CS, 1, 0);
  287. lift(b, temp, b + w2, 1, 1, 1, width, W_DM, W_DO, W_DS, 0, 0);
  288. }
  289. static void vertical_decompose97iH0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2,
  290. int width)
  291. {
  292. int i;
  293. for (i = 0; i < width; i++)
  294. b1[i] -= (W_AM * (b0[i] + b2[i]) + W_AO) >> W_AS;
  295. }
  296. static void vertical_decompose97iH1(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2,
  297. int width)
  298. {
  299. int i;
  300. for (i = 0; i < width; i++)
  301. b1[i] += (W_CM * (b0[i] + b2[i]) + W_CO) >> W_CS;
  302. }
  303. static void vertical_decompose97iL0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2,
  304. int width)
  305. {
  306. int i;
  307. for (i = 0; i < width; i++)
  308. #ifdef liftS
  309. b1[i] -= (W_BM * (b0[i] + b2[i]) + W_BO) >> W_BS;
  310. #else
  311. b1[i] = (16 * 4 * b1[i] - 4 * (b0[i] + b2[i]) + W_BO * 5 + (5 << 27)) /
  312. (5 * 16) - (1 << 23);
  313. #endif
  314. }
  315. static void vertical_decompose97iL1(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2,
  316. int width)
  317. {
  318. int i;
  319. for (i = 0; i < width; i++)
  320. b1[i] += (W_DM * (b0[i] + b2[i]) + W_DO) >> W_DS;
  321. }
  322. static void spatial_decompose97i(DWTELEM *buffer, int width, int height,
  323. int stride)
  324. {
  325. int y;
  326. DWTELEM *b0 = buffer + mirror(-4 - 1, height - 1) * stride;
  327. DWTELEM *b1 = buffer + mirror(-4, height - 1) * stride;
  328. DWTELEM *b2 = buffer + mirror(-4 + 1, height - 1) * stride;
  329. DWTELEM *b3 = buffer + mirror(-4 + 2, height - 1) * stride;
  330. for (y = -4; y < height; y += 2) {
  331. DWTELEM *b4 = buffer + mirror(y + 3, height - 1) * stride;
  332. DWTELEM *b5 = buffer + mirror(y + 4, height - 1) * stride;
  333. if (y + 3 < (unsigned)height)
  334. horizontal_decompose97i(b4, width);
  335. if (y + 4 < (unsigned)height)
  336. horizontal_decompose97i(b5, width);
  337. if (y + 3 < (unsigned)height)
  338. vertical_decompose97iH0(b3, b4, b5, width);
  339. if (y + 2 < (unsigned)height)
  340. vertical_decompose97iL0(b2, b3, b4, width);
  341. if (y + 1 < (unsigned)height)
  342. vertical_decompose97iH1(b1, b2, b3, width);
  343. if (y + 0 < (unsigned)height)
  344. vertical_decompose97iL1(b0, b1, b2, width);
  345. b0 = b2;
  346. b1 = b3;
  347. b2 = b4;
  348. b3 = b5;
  349. }
  350. }
  351. void ff_spatial_dwt(DWTELEM *buffer, int width, int height, int stride,
  352. int type, int decomposition_count)
  353. {
  354. int level;
  355. for (level = 0; level < decomposition_count; level++) {
  356. switch (type) {
  357. case DWT_97:
  358. spatial_decompose97i(buffer,
  359. width >> level, height >> level,
  360. stride << level);
  361. break;
  362. case DWT_53:
  363. spatial_decompose53i(buffer,
  364. width >> level, height >> level,
  365. stride << level);
  366. break;
  367. }
  368. }
  369. }
  370. static void horizontal_compose53i(IDWTELEM *b, int width)
  371. {
  372. IDWTELEM temp[width];
  373. const int width2 = width >> 1;
  374. const int w2 = (width + 1) >> 1;
  375. int x;
  376. for (x = 0; x < width2; x++) {
  377. temp[2 * x] = b[x];
  378. temp[2 * x + 1] = b[x + w2];
  379. }
  380. if (width & 1)
  381. temp[2 * x] = b[x];
  382. b[0] = temp[0] - ((temp[1] + 1) >> 1);
  383. for (x = 2; x < width - 1; x += 2) {
  384. b[x] = temp[x] - ((temp[x - 1] + temp[x + 1] + 2) >> 2);
  385. b[x - 1] = temp[x - 1] + ((b[x - 2] + b[x] + 1) >> 1);
  386. }
  387. if (width & 1) {
  388. b[x] = temp[x] - ((temp[x - 1] + 1) >> 1);
  389. b[x - 1] = temp[x - 1] + ((b[x - 2] + b[x] + 1) >> 1);
  390. } else
  391. b[x - 1] = temp[x - 1] + b[x - 2];
  392. }
  393. static void vertical_compose53iH0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2,
  394. int width)
  395. {
  396. int i;
  397. for (i = 0; i < width; i++)
  398. b1[i] += (b0[i] + b2[i]) >> 1;
  399. }
  400. static void vertical_compose53iL0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2,
  401. int width)
  402. {
  403. int i;
  404. for (i = 0; i < width; i++)
  405. b1[i] -= (b0[i] + b2[i] + 2) >> 2;
  406. }
  407. static void spatial_compose53i_buffered_init(DWTCompose *cs, slice_buffer *sb,
  408. int height, int stride_line)
  409. {
  410. cs->b0 = slice_buffer_get_line(sb,
  411. mirror(-1 - 1, height - 1) * stride_line);
  412. cs->b1 = slice_buffer_get_line(sb, mirror(-1, height - 1) * stride_line);
  413. cs->y = -1;
  414. }
  415. static void spatial_compose53i_init(DWTCompose *cs, IDWTELEM *buffer,
  416. int height, int stride)
  417. {
  418. cs->b0 = buffer + mirror(-1 - 1, height - 1) * stride;
  419. cs->b1 = buffer + mirror(-1, height - 1) * stride;
  420. cs->y = -1;
  421. }
  422. static void spatial_compose53i_dy_buffered(DWTCompose *cs, slice_buffer *sb,
  423. int width, int height,
  424. int stride_line)
  425. {
  426. int y = cs->y;
  427. IDWTELEM *b0 = cs->b0;
  428. IDWTELEM *b1 = cs->b1;
  429. IDWTELEM *b2 = slice_buffer_get_line(sb,
  430. mirror(y + 1, height - 1) *
  431. stride_line);
  432. IDWTELEM *b3 = slice_buffer_get_line(sb,
  433. mirror(y + 2, height - 1) *
  434. stride_line);
  435. if (y + 1 < (unsigned)height && y < (unsigned)height) {
  436. int x;
  437. for (x = 0; x < width; x++) {
  438. b2[x] -= (b1[x] + b3[x] + 2) >> 2;
  439. b1[x] += (b0[x] + b2[x]) >> 1;
  440. }
  441. } else {
  442. if (y + 1 < (unsigned)height)
  443. vertical_compose53iL0(b1, b2, b3, width);
  444. if (y + 0 < (unsigned)height)
  445. vertical_compose53iH0(b0, b1, b2, width);
  446. }
  447. if (y - 1 < (unsigned)height)
  448. horizontal_compose53i(b0, width);
  449. if (y + 0 < (unsigned)height)
  450. horizontal_compose53i(b1, width);
  451. cs->b0 = b2;
  452. cs->b1 = b3;
  453. cs->y += 2;
  454. }
  455. static void spatial_compose53i_dy(DWTCompose *cs, IDWTELEM *buffer, int width,
  456. int height, int stride)
  457. {
  458. int y = cs->y;
  459. IDWTELEM *b0 = cs->b0;
  460. IDWTELEM *b1 = cs->b1;
  461. IDWTELEM *b2 = buffer + mirror(y + 1, height - 1) * stride;
  462. IDWTELEM *b3 = buffer + mirror(y + 2, height - 1) * stride;
  463. if (y + 1 < (unsigned)height)
  464. vertical_compose53iL0(b1, b2, b3, width);
  465. if (y + 0 < (unsigned)height)
  466. vertical_compose53iH0(b0, b1, b2, width);
  467. if (y - 1 < (unsigned)height)
  468. horizontal_compose53i(b0, width);
  469. if (y + 0 < (unsigned)height)
  470. horizontal_compose53i(b1, width);
  471. cs->b0 = b2;
  472. cs->b1 = b3;
  473. cs->y += 2;
  474. }
  475. static void av_unused spatial_compose53i(IDWTELEM *buffer, int width,
  476. int height, int stride)
  477. {
  478. DWTCompose cs;
  479. spatial_compose53i_init(&cs, buffer, height, stride);
  480. while (cs.y <= height)
  481. spatial_compose53i_dy(&cs, buffer, width, height, stride);
  482. }
  483. void ff_snow_horizontal_compose97i(IDWTELEM *b, int width)
  484. {
  485. IDWTELEM temp[width];
  486. const int w2 = (width + 1) >> 1;
  487. #if 0 //maybe more understadable but slower
  488. inv_lift(temp, b, b + w2, 2, 1, 1, width, W_DM, W_DO, W_DS, 0, 1);
  489. inv_lift(temp + 1, b + w2, temp, 2, 1, 2, width, W_CM, W_CO, W_CS, 1, 1);
  490. inv_liftS(b, temp, temp + 1, 2, 2, 2, width, W_BM, W_BO, W_BS, 0, 1);
  491. inv_lift(b + 1, temp + 1, b, 2, 2, 2, width, W_AM, W_AO, W_AS, 1, 0);
  492. #else
  493. int x;
  494. temp[0] = b[0] - ((3 * b[w2] + 2) >> 2);
  495. for (x = 1; x < (width >> 1); x++) {
  496. temp[2 * x] = b[x] - ((3 * (b[x + w2 - 1] + b[x + w2]) + 4) >> 3);
  497. temp[2 * x - 1] = b[x + w2 - 1] - temp[2 * x - 2] - temp[2 * x];
  498. }
  499. if (width & 1) {
  500. temp[2 * x] = b[x] - ((3 * b[x + w2 - 1] + 2) >> 2);
  501. temp[2 * x - 1] = b[x + w2 - 1] - temp[2 * x - 2] - temp[2 * x];
  502. } else
  503. temp[2 * x - 1] = b[x + w2 - 1] - 2 * temp[2 * x - 2];
  504. b[0] = temp[0] + ((2 * temp[0] + temp[1] + 4) >> 3);
  505. for (x = 2; x < width - 1; x += 2) {
  506. b[x] = temp[x] + ((4 * temp[x] + temp[x - 1] + temp[x + 1] + 8) >> 4);
  507. b[x - 1] = temp[x - 1] + ((3 * (b[x - 2] + b[x])) >> 1);
  508. }
  509. if (width & 1) {
  510. b[x] = temp[x] + ((2 * temp[x] + temp[x - 1] + 4) >> 3);
  511. b[x - 1] = temp[x - 1] + ((3 * (b[x - 2] + b[x])) >> 1);
  512. } else
  513. b[x - 1] = temp[x - 1] + 3 * b[x - 2];
  514. #endif
  515. }
  516. static void vertical_compose97iH0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2,
  517. int width)
  518. {
  519. int i;
  520. for (i = 0; i < width; i++)
  521. b1[i] += (W_AM * (b0[i] + b2[i]) + W_AO) >> W_AS;
  522. }
  523. static void vertical_compose97iH1(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2,
  524. int width)
  525. {
  526. int i;
  527. for (i = 0; i < width; i++)
  528. b1[i] -= (W_CM * (b0[i] + b2[i]) + W_CO) >> W_CS;
  529. }
  530. static void vertical_compose97iL0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2,
  531. int width)
  532. {
  533. int i;
  534. for (i = 0; i < width; i++)
  535. #ifdef liftS
  536. b1[i] += (W_BM * (b0[i] + b2[i]) + W_BO) >> W_BS;
  537. #else
  538. b1[i] += (W_BM * (b0[i] + b2[i]) + 4 * b1[i] + W_BO) >> W_BS;
  539. #endif
  540. }
  541. static void vertical_compose97iL1(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2,
  542. int width)
  543. {
  544. int i;
  545. for (i = 0; i < width; i++)
  546. b1[i] -= (W_DM * (b0[i] + b2[i]) + W_DO) >> W_DS;
  547. }
  548. void ff_snow_vertical_compose97i(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2,
  549. IDWTELEM *b3, IDWTELEM *b4, IDWTELEM *b5,
  550. int width)
  551. {
  552. int i;
  553. for (i = 0; i < width; i++) {
  554. b4[i] -= (W_DM * (b3[i] + b5[i]) + W_DO) >> W_DS;
  555. b3[i] -= (W_CM * (b2[i] + b4[i]) + W_CO) >> W_CS;
  556. #ifdef liftS
  557. b2[i] += (W_BM * (b1[i] + b3[i]) + W_BO) >> W_BS;
  558. #else
  559. b2[i] += (W_BM * (b1[i] + b3[i]) + 4 * b2[i] + W_BO) >> W_BS;
  560. #endif
  561. b1[i] += (W_AM * (b0[i] + b2[i]) + W_AO) >> W_AS;
  562. }
  563. }
  564. static void spatial_compose97i_buffered_init(DWTCompose *cs, slice_buffer *sb,
  565. int height, int stride_line)
  566. {
  567. cs->b0 = slice_buffer_get_line(sb, mirror(-3 - 1, height - 1) * stride_line);
  568. cs->b1 = slice_buffer_get_line(sb, mirror(-3, height - 1) * stride_line);
  569. cs->b2 = slice_buffer_get_line(sb, mirror(-3 + 1, height - 1) * stride_line);
  570. cs->b3 = slice_buffer_get_line(sb, mirror(-3 + 2, height - 1) * stride_line);
  571. cs->y = -3;
  572. }
  573. static void spatial_compose97i_init(DWTCompose *cs, IDWTELEM *buffer, int height,
  574. int stride)
  575. {
  576. cs->b0 = buffer + mirror(-3 - 1, height - 1) * stride;
  577. cs->b1 = buffer + mirror(-3, height - 1) * stride;
  578. cs->b2 = buffer + mirror(-3 + 1, height - 1) * stride;
  579. cs->b3 = buffer + mirror(-3 + 2, height - 1) * stride;
  580. cs->y = -3;
  581. }
  582. static void spatial_compose97i_dy_buffered(DWTContext *dsp, DWTCompose *cs,
  583. slice_buffer *sb, int width,
  584. int height, int stride_line)
  585. {
  586. int y = cs->y;
  587. IDWTELEM *b0 = cs->b0;
  588. IDWTELEM *b1 = cs->b1;
  589. IDWTELEM *b2 = cs->b2;
  590. IDWTELEM *b3 = cs->b3;
  591. IDWTELEM *b4 = slice_buffer_get_line(sb,
  592. mirror(y + 3, height - 1) *
  593. stride_line);
  594. IDWTELEM *b5 = slice_buffer_get_line(sb,
  595. mirror(y + 4, height - 1) *
  596. stride_line);
  597. if (y > 0 && y + 4 < height) {
  598. dsp->vertical_compose97i(b0, b1, b2, b3, b4, b5, width);
  599. } else {
  600. if (y + 3 < (unsigned)height)
  601. vertical_compose97iL1(b3, b4, b5, width);
  602. if (y + 2 < (unsigned)height)
  603. vertical_compose97iH1(b2, b3, b4, width);
  604. if (y + 1 < (unsigned)height)
  605. vertical_compose97iL0(b1, b2, b3, width);
  606. if (y + 0 < (unsigned)height)
  607. vertical_compose97iH0(b0, b1, b2, width);
  608. }
  609. if (y - 1 < (unsigned)height)
  610. dsp->horizontal_compose97i(b0, width);
  611. if (y + 0 < (unsigned)height)
  612. dsp->horizontal_compose97i(b1, width);
  613. cs->b0 = b2;
  614. cs->b1 = b3;
  615. cs->b2 = b4;
  616. cs->b3 = b5;
  617. cs->y += 2;
  618. }
  619. static void spatial_compose97i_dy(DWTCompose *cs, IDWTELEM *buffer, int width,
  620. int height, int stride)
  621. {
  622. int y = cs->y;
  623. IDWTELEM *b0 = cs->b0;
  624. IDWTELEM *b1 = cs->b1;
  625. IDWTELEM *b2 = cs->b2;
  626. IDWTELEM *b3 = cs->b3;
  627. IDWTELEM *b4 = buffer + mirror(y + 3, height - 1) * stride;
  628. IDWTELEM *b5 = buffer + mirror(y + 4, height - 1) * stride;
  629. if (y + 3 < (unsigned)height)
  630. vertical_compose97iL1(b3, b4, b5, width);
  631. if (y + 2 < (unsigned)height)
  632. vertical_compose97iH1(b2, b3, b4, width);
  633. if (y + 1 < (unsigned)height)
  634. vertical_compose97iL0(b1, b2, b3, width);
  635. if (y + 0 < (unsigned)height)
  636. vertical_compose97iH0(b0, b1, b2, width);
  637. if (y - 1 < (unsigned)height)
  638. ff_snow_horizontal_compose97i(b0, width);
  639. if (y + 0 < (unsigned)height)
  640. ff_snow_horizontal_compose97i(b1, width);
  641. cs->b0 = b2;
  642. cs->b1 = b3;
  643. cs->b2 = b4;
  644. cs->b3 = b5;
  645. cs->y += 2;
  646. }
  647. static void av_unused spatial_compose97i(IDWTELEM *buffer, int width,
  648. int height, int stride)
  649. {
  650. DWTCompose cs;
  651. spatial_compose97i_init(&cs, buffer, height, stride);
  652. while (cs.y <= height)
  653. spatial_compose97i_dy(&cs, buffer, width, height, stride);
  654. }
  655. void ff_spatial_idwt_buffered_init(DWTCompose *cs, slice_buffer *sb, int width,
  656. int height, int stride_line, int type,
  657. int decomposition_count)
  658. {
  659. int level;
  660. for (level = decomposition_count - 1; level >= 0; level--) {
  661. switch (type) {
  662. case DWT_97:
  663. spatial_compose97i_buffered_init(cs + level, sb, height >> level,
  664. stride_line << level);
  665. break;
  666. case DWT_53:
  667. spatial_compose53i_buffered_init(cs + level, sb, height >> level,
  668. stride_line << level);
  669. break;
  670. }
  671. }
  672. }
  673. void ff_spatial_idwt_buffered_slice(DWTContext *dsp, DWTCompose *cs,
  674. slice_buffer *slice_buf, int width,
  675. int height, int stride_line, int type,
  676. int decomposition_count, int y)
  677. {
  678. const int support = type == 1 ? 3 : 5;
  679. int level;
  680. if (type == 2)
  681. return;
  682. for (level = decomposition_count - 1; level >= 0; level--)
  683. while (cs[level].y <= FFMIN((y >> level) + support, height >> level)) {
  684. switch (type) {
  685. case DWT_97:
  686. spatial_compose97i_dy_buffered(dsp, cs + level, slice_buf,
  687. width >> level,
  688. height >> level,
  689. stride_line << level);
  690. break;
  691. case DWT_53:
  692. spatial_compose53i_dy_buffered(cs + level, slice_buf,
  693. width >> level,
  694. height >> level,
  695. stride_line << level);
  696. break;
  697. }
  698. }
  699. }
  700. static void ff_spatial_idwt_init(DWTCompose *cs, IDWTELEM *buffer, int width,
  701. int height, int stride, int type,
  702. int decomposition_count)
  703. {
  704. int level;
  705. for (level = decomposition_count - 1; level >= 0; level--) {
  706. switch (type) {
  707. case DWT_97:
  708. spatial_compose97i_init(cs + level, buffer, height >> level,
  709. stride << level);
  710. break;
  711. case DWT_53:
  712. spatial_compose53i_init(cs + level, buffer, height >> level,
  713. stride << level);
  714. break;
  715. }
  716. }
  717. }
  718. static void ff_spatial_idwt_slice(DWTCompose *cs, IDWTELEM *buffer, int width,
  719. int height, int stride, int type,
  720. int decomposition_count, int y)
  721. {
  722. const int support = type == 1 ? 3 : 5;
  723. int level;
  724. if (type == 2)
  725. return;
  726. for (level = decomposition_count - 1; level >= 0; level--)
  727. while (cs[level].y <= FFMIN((y >> level) + support, height >> level)) {
  728. switch (type) {
  729. case DWT_97:
  730. spatial_compose97i_dy(cs + level, buffer, width >> level,
  731. height >> level, stride << level);
  732. break;
  733. case DWT_53:
  734. spatial_compose53i_dy(cs + level, buffer, width >> level,
  735. height >> level, stride << level);
  736. break;
  737. }
  738. }
  739. }
  740. void ff_spatial_idwt(IDWTELEM *buffer, int width, int height, int stride,
  741. int type, int decomposition_count)
  742. {
  743. DWTCompose cs[MAX_DECOMPOSITIONS];
  744. int y;
  745. ff_spatial_idwt_init(cs, buffer, width, height, stride, type,
  746. decomposition_count);
  747. for (y = 0; y < height; y += 4)
  748. ff_spatial_idwt_slice(cs, buffer, width, height, stride, type,
  749. decomposition_count, y);
  750. }
  751. static inline int w_c(void *v, uint8_t *pix1, uint8_t *pix2, int line_size,
  752. int w, int h, int type)
  753. {
  754. int s, i, j;
  755. const int dec_count = w == 8 ? 3 : 4;
  756. int tmp[32 * 32];
  757. int level, ori;
  758. static const int scale[2][2][4][4] = {
  759. {
  760. { // 9/7 8x8 dec=3
  761. { 268, 239, 239, 213 },
  762. { 0, 224, 224, 152 },
  763. { 0, 135, 135, 110 },
  764. },
  765. { // 9/7 16x16 or 32x32 dec=4
  766. { 344, 310, 310, 280 },
  767. { 0, 320, 320, 228 },
  768. { 0, 175, 175, 136 },
  769. { 0, 129, 129, 102 },
  770. }
  771. },
  772. {
  773. { // 5/3 8x8 dec=3
  774. { 275, 245, 245, 218 },
  775. { 0, 230, 230, 156 },
  776. { 0, 138, 138, 113 },
  777. },
  778. { // 5/3 16x16 or 32x32 dec=4
  779. { 352, 317, 317, 286 },
  780. { 0, 328, 328, 233 },
  781. { 0, 180, 180, 140 },
  782. { 0, 132, 132, 105 },
  783. }
  784. }
  785. };
  786. for (i = 0; i < h; i++) {
  787. for (j = 0; j < w; j += 4) {
  788. tmp[32 * i + j + 0] = (pix1[j + 0] - pix2[j + 0]) << 4;
  789. tmp[32 * i + j + 1] = (pix1[j + 1] - pix2[j + 1]) << 4;
  790. tmp[32 * i + j + 2] = (pix1[j + 2] - pix2[j + 2]) << 4;
  791. tmp[32 * i + j + 3] = (pix1[j + 3] - pix2[j + 3]) << 4;
  792. }
  793. pix1 += line_size;
  794. pix2 += line_size;
  795. }
  796. ff_spatial_dwt(tmp, w, h, 32, type, dec_count);
  797. s = 0;
  798. assert(w == h);
  799. for (level = 0; level < dec_count; level++)
  800. for (ori = level ? 1 : 0; ori < 4; ori++) {
  801. int size = w >> (dec_count - level);
  802. int sx = (ori & 1) ? size : 0;
  803. int stride = 32 << (dec_count - level);
  804. int sy = (ori & 2) ? stride >> 1 : 0;
  805. for (i = 0; i < size; i++)
  806. for (j = 0; j < size; j++) {
  807. int v = tmp[sx + sy + i * stride + j] *
  808. scale[type][dec_count - 3][level][ori];
  809. s += FFABS(v);
  810. }
  811. }
  812. assert(s >= 0);
  813. return s >> 9;
  814. }
  815. static int w53_8_c(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h)
  816. {
  817. return w_c(v, pix1, pix2, line_size, 8, h, 1);
  818. }
  819. static int w97_8_c(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h)
  820. {
  821. return w_c(v, pix1, pix2, line_size, 8, h, 0);
  822. }
  823. static int w53_16_c(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h)
  824. {
  825. return w_c(v, pix1, pix2, line_size, 16, h, 1);
  826. }
  827. static int w97_16_c(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h)
  828. {
  829. return w_c(v, pix1, pix2, line_size, 16, h, 0);
  830. }
  831. int ff_w53_32_c(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h)
  832. {
  833. return w_c(v, pix1, pix2, line_size, 32, h, 1);
  834. }
  835. int ff_w97_32_c(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h)
  836. {
  837. return w_c(v, pix1, pix2, line_size, 32, h, 0);
  838. }
  839. void ff_dsputil_init_dwt(DSPContext *c)
  840. {
  841. c->w53[0] = w53_16_c;
  842. c->w53[1] = w53_8_c;
  843. c->w97[0] = w97_16_c;
  844. c->w97[1] = w97_8_c;
  845. }
  846. void ff_dwt_init(DWTContext *c)
  847. {
  848. c->vertical_compose97i = ff_snow_vertical_compose97i;
  849. c->horizontal_compose97i = ff_snow_horizontal_compose97i;
  850. c->inner_add_yblock = ff_snow_inner_add_yblock;
  851. if (HAVE_MMX)
  852. ff_dwt_init_x86(c);
  853. }