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.

997 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. int 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. if (!buf->line)
  34. return AVERROR(ENOMEM);
  35. buf->data_stack = av_malloc(sizeof(IDWTELEM *) * max_allocated_lines);
  36. if (!buf->data_stack) {
  37. av_free(buf->line);
  38. return AVERROR(ENOMEM);
  39. }
  40. for (i = 0; i < max_allocated_lines; i++) {
  41. buf->data_stack[i] = av_malloc(sizeof(IDWTELEM) * line_width);
  42. if (!buf->data_stack[i]) {
  43. for (i--; i >=0; i--)
  44. av_free(buf->data_stack[i]);
  45. av_free(buf->data_stack);
  46. av_free(buf->line);
  47. return AVERROR(ENOMEM);
  48. }
  49. }
  50. buf->data_stack_top = max_allocated_lines - 1;
  51. return 0;
  52. }
  53. IDWTELEM *ff_slice_buffer_load_line(slice_buffer *buf, int line)
  54. {
  55. IDWTELEM *buffer;
  56. assert(buf->data_stack_top >= 0);
  57. // assert(!buf->line[line]);
  58. if (buf->line[line])
  59. return buf->line[line];
  60. buffer = buf->data_stack[buf->data_stack_top];
  61. buf->data_stack_top--;
  62. buf->line[line] = buffer;
  63. return buffer;
  64. }
  65. void ff_slice_buffer_release(slice_buffer *buf, int line)
  66. {
  67. IDWTELEM *buffer;
  68. assert(line >= 0 && line < buf->line_count);
  69. assert(buf->line[line]);
  70. buffer = buf->line[line];
  71. buf->data_stack_top++;
  72. buf->data_stack[buf->data_stack_top] = buffer;
  73. buf->line[line] = NULL;
  74. }
  75. void ff_slice_buffer_flush(slice_buffer *buf)
  76. {
  77. int i;
  78. for (i = 0; i < buf->line_count; i++)
  79. if (buf->line[i])
  80. ff_slice_buffer_release(buf, i);
  81. }
  82. void ff_slice_buffer_destroy(slice_buffer *buf)
  83. {
  84. int i;
  85. ff_slice_buffer_flush(buf);
  86. for (i = buf->data_count - 1; i >= 0; i--)
  87. av_freep(&buf->data_stack[i]);
  88. av_freep(&buf->data_stack);
  89. av_freep(&buf->line);
  90. }
  91. static inline int mirror(int v, int m)
  92. {
  93. while ((unsigned)v > (unsigned)m) {
  94. v = -v;
  95. if (v < 0)
  96. v += 2 * m;
  97. }
  98. return v;
  99. }
  100. static av_always_inline void lift(DWTELEM *dst, DWTELEM *src, DWTELEM *ref,
  101. int dst_step, int src_step, int ref_step,
  102. int width, int mul, int add, int shift,
  103. int highpass, int inverse)
  104. {
  105. const int mirror_left = !highpass;
  106. const int mirror_right = (width & 1) ^ highpass;
  107. const int w = (width >> 1) - 1 + (highpass & width);
  108. int i;
  109. #define LIFT(src, ref, inv) ((src) + ((inv) ? -(ref) : +(ref)))
  110. if (mirror_left) {
  111. dst[0] = LIFT(src[0], ((mul * 2 * ref[0] + add) >> shift), inverse);
  112. dst += dst_step;
  113. src += src_step;
  114. }
  115. for (i = 0; i < w; i++)
  116. dst[i * dst_step] = LIFT(src[i * src_step],
  117. ((mul * (ref[i * ref_step] +
  118. ref[(i + 1) * ref_step]) +
  119. add) >> shift),
  120. inverse);
  121. if (mirror_right)
  122. dst[w * dst_step] = LIFT(src[w * src_step],
  123. ((mul * 2 * ref[w * ref_step] + add) >> shift),
  124. inverse);
  125. }
  126. static av_always_inline void inv_lift(IDWTELEM *dst, IDWTELEM *src, IDWTELEM *ref,
  127. int dst_step, int src_step, int ref_step,
  128. int width, int mul, int add, int shift,
  129. int highpass, int inverse)
  130. {
  131. const int mirror_left = !highpass;
  132. const int mirror_right = (width & 1) ^ highpass;
  133. const int w = (width >> 1) - 1 + (highpass & width);
  134. int i;
  135. #define LIFT(src, ref, inv) ((src) + ((inv) ? -(ref) : +(ref)))
  136. if (mirror_left) {
  137. dst[0] = LIFT(src[0], ((mul * 2 * ref[0] + add) >> shift), inverse);
  138. dst += dst_step;
  139. src += src_step;
  140. }
  141. for (i = 0; i < w; i++)
  142. dst[i * dst_step] = LIFT(src[i * src_step],
  143. ((mul * (ref[i * ref_step] +
  144. ref[(i + 1) * ref_step]) +
  145. add) >> shift),
  146. inverse);
  147. if (mirror_right) {
  148. dst[w * dst_step] = LIFT(src[w * src_step],
  149. ((mul * 2 * ref[w * ref_step] + add) >> shift),
  150. inverse);
  151. }
  152. }
  153. #ifndef liftS
  154. static av_always_inline void liftS(DWTELEM *dst, DWTELEM *src, DWTELEM *ref,
  155. int dst_step, int src_step, int ref_step,
  156. int width, int mul, int add, int shift,
  157. int highpass, int inverse)
  158. {
  159. const int mirror_left = !highpass;
  160. const int mirror_right = (width & 1) ^ highpass;
  161. const int w = (width >> 1) - 1 + (highpass & width);
  162. int i;
  163. assert(shift == 4);
  164. #define LIFTS(src, ref, inv) \
  165. ((inv) ? (src) + (((ref) + 4 * (src)) >> shift) \
  166. : -((-16 * (src) + (ref) + add / \
  167. 4 + 1 + (5 << 25)) / (5 * 4) - (1 << 23)))
  168. if (mirror_left) {
  169. dst[0] = LIFTS(src[0], mul * 2 * ref[0] + add, inverse);
  170. dst += dst_step;
  171. src += src_step;
  172. }
  173. for (i = 0; i < w; i++)
  174. dst[i * dst_step] = LIFTS(src[i * src_step],
  175. mul * (ref[i * ref_step] +
  176. ref[(i + 1) * ref_step]) + add,
  177. inverse);
  178. if (mirror_right)
  179. dst[w * dst_step] = LIFTS(src[w * src_step],
  180. mul * 2 * ref[w * ref_step] + add,
  181. inverse);
  182. }
  183. static av_always_inline void inv_liftS(IDWTELEM *dst, IDWTELEM *src,
  184. IDWTELEM *ref, int dst_step,
  185. int src_step, int ref_step,
  186. int width, int mul, int add, int shift,
  187. int highpass, int inverse)
  188. {
  189. const int mirror_left = !highpass;
  190. const int mirror_right = (width & 1) ^ highpass;
  191. const int w = (width >> 1) - 1 + (highpass & width);
  192. int i;
  193. assert(shift == 4);
  194. #define LIFTS(src, ref, inv) \
  195. ((inv) ? (src) + (((ref) + 4 * (src)) >> shift) \
  196. : -((-16 * (src) + (ref) + add / \
  197. 4 + 1 + (5 << 25)) / (5 * 4) - (1 << 23)))
  198. if (mirror_left) {
  199. dst[0] = LIFTS(src[0], mul * 2 * ref[0] + add, inverse);
  200. dst += dst_step;
  201. src += src_step;
  202. }
  203. for (i = 0; i < w; i++)
  204. dst[i * dst_step] = LIFTS(src[i * src_step],
  205. mul * (ref[i * ref_step] +
  206. ref[(i + 1) * ref_step]) + add,
  207. inverse);
  208. if (mirror_right)
  209. dst[w * dst_step] = LIFTS(src[w * src_step],
  210. mul * 2 * ref[w * ref_step] + add, inverse);
  211. }
  212. #endif /* ! liftS */
  213. static void horizontal_decompose53i(DWTELEM *b, int width)
  214. {
  215. DWTELEM temp[width];
  216. const int width2 = width >> 1;
  217. int x;
  218. const int w2 = (width + 1) >> 1;
  219. for (x = 0; x < width2; x++) {
  220. temp[x] = b[2 * x];
  221. temp[x + w2] = b[2 * x + 1];
  222. }
  223. if (width & 1)
  224. temp[x] = b[2 * x];
  225. #if 0
  226. {
  227. int A1, A2, A3, A4;
  228. A2 = temp[1];
  229. A4 = temp[0];
  230. A1 = temp[0 + width2];
  231. A1 -= (A2 + A4) >> 1;
  232. A4 += (A1 + 1) >> 1;
  233. b[0 + width2] = A1;
  234. b[0] = A4;
  235. for (x = 1; x + 1 < width2; x += 2) {
  236. A3 = temp[x + width2];
  237. A4 = temp[x + 1];
  238. A3 -= (A2 + A4) >> 1;
  239. A2 += (A1 + A3 + 2) >> 2;
  240. b[x + width2] = A3;
  241. b[x] = A2;
  242. A1 = temp[x + 1 + width2];
  243. A2 = temp[x + 2];
  244. A1 -= (A2 + A4) >> 1;
  245. A4 += (A1 + A3 + 2) >> 2;
  246. b[x + 1 + width2] = A1;
  247. b[x + 1] = A4;
  248. }
  249. A3 = temp[width - 1];
  250. A3 -= A2;
  251. A2 += (A1 + A3 + 2) >> 2;
  252. b[width - 1] = A3;
  253. b[width2 - 1] = A2;
  254. }
  255. #else
  256. lift(b + w2, temp + w2, temp, 1, 1, 1, width, -1, 0, 1, 1, 0);
  257. lift(b, temp, b + w2, 1, 1, 1, width, 1, 2, 2, 0, 0);
  258. #endif /* 0 */
  259. }
  260. static void vertical_decompose53iH0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2,
  261. int width)
  262. {
  263. int i;
  264. for (i = 0; i < width; i++)
  265. b1[i] -= (b0[i] + b2[i]) >> 1;
  266. }
  267. static void vertical_decompose53iL0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2,
  268. int width)
  269. {
  270. int i;
  271. for (i = 0; i < width; i++)
  272. b1[i] += (b0[i] + b2[i] + 2) >> 2;
  273. }
  274. static void spatial_decompose53i(DWTELEM *buffer, int width, int height,
  275. int stride)
  276. {
  277. int y;
  278. DWTELEM *b0 = buffer + mirror(-2 - 1, height - 1) * stride;
  279. DWTELEM *b1 = buffer + mirror(-2, height - 1) * stride;
  280. for (y = -2; y < height; y += 2) {
  281. DWTELEM *b2 = buffer + mirror(y + 1, height - 1) * stride;
  282. DWTELEM *b3 = buffer + mirror(y + 2, height - 1) * stride;
  283. if (y + 1 < (unsigned)height)
  284. horizontal_decompose53i(b2, width);
  285. if (y + 2 < (unsigned)height)
  286. horizontal_decompose53i(b3, width);
  287. if (y + 1 < (unsigned)height)
  288. vertical_decompose53iH0(b1, b2, b3, width);
  289. if (y + 0 < (unsigned)height)
  290. vertical_decompose53iL0(b0, b1, b2, width);
  291. b0 = b2;
  292. b1 = b3;
  293. }
  294. }
  295. static void horizontal_decompose97i(DWTELEM *b, int width)
  296. {
  297. DWTELEM temp[width];
  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, int width, int height,
  338. 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, width);
  350. if (y + 4 < (unsigned)height)
  351. horizontal_decompose97i(b5, 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, int width, int height, int stride,
  367. 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,
  374. width >> level, height >> level,
  375. stride << level);
  376. break;
  377. case DWT_53:
  378. spatial_decompose53i(buffer,
  379. width >> level, height >> level,
  380. stride << level);
  381. break;
  382. }
  383. }
  384. }
  385. static void horizontal_compose53i(IDWTELEM *b, int width)
  386. {
  387. IDWTELEM temp[width];
  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. 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, width);
  464. if (y + 0 < (unsigned)height)
  465. horizontal_compose53i(b1, width);
  466. cs->b0 = b2;
  467. cs->b1 = b3;
  468. cs->y += 2;
  469. }
  470. static void spatial_compose53i_dy(DWTCompose *cs, IDWTELEM *buffer, int width,
  471. int height, int stride)
  472. {
  473. int y = cs->y;
  474. IDWTELEM *b0 = cs->b0;
  475. IDWTELEM *b1 = cs->b1;
  476. IDWTELEM *b2 = buffer + mirror(y + 1, height - 1) * stride;
  477. IDWTELEM *b3 = buffer + mirror(y + 2, height - 1) * stride;
  478. if (y + 1 < (unsigned)height)
  479. vertical_compose53iL0(b1, b2, b3, width);
  480. if (y + 0 < (unsigned)height)
  481. vertical_compose53iH0(b0, b1, b2, width);
  482. if (y - 1 < (unsigned)height)
  483. horizontal_compose53i(b0, width);
  484. if (y + 0 < (unsigned)height)
  485. horizontal_compose53i(b1, width);
  486. cs->b0 = b2;
  487. cs->b1 = b3;
  488. cs->y += 2;
  489. }
  490. static void av_unused spatial_compose53i(IDWTELEM *buffer, int width,
  491. int height, int stride)
  492. {
  493. DWTCompose cs;
  494. spatial_compose53i_init(&cs, buffer, height, stride);
  495. while (cs.y <= height)
  496. spatial_compose53i_dy(&cs, buffer, width, height, stride);
  497. }
  498. void ff_snow_horizontal_compose97i(IDWTELEM *b, int width)
  499. {
  500. IDWTELEM temp[width];
  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, int width,
  599. int height, int stride_line)
  600. {
  601. int y = cs->y;
  602. IDWTELEM *b0 = cs->b0;
  603. IDWTELEM *b1 = cs->b1;
  604. IDWTELEM *b2 = cs->b2;
  605. IDWTELEM *b3 = cs->b3;
  606. IDWTELEM *b4 = slice_buffer_get_line(sb,
  607. mirror(y + 3, height - 1) *
  608. stride_line);
  609. IDWTELEM *b5 = slice_buffer_get_line(sb,
  610. mirror(y + 4, height - 1) *
  611. stride_line);
  612. if (y > 0 && y + 4 < height) {
  613. dsp->vertical_compose97i(b0, b1, b2, b3, b4, b5, width);
  614. } else {
  615. if (y + 3 < (unsigned)height)
  616. vertical_compose97iL1(b3, b4, b5, width);
  617. if (y + 2 < (unsigned)height)
  618. vertical_compose97iH1(b2, b3, b4, width);
  619. if (y + 1 < (unsigned)height)
  620. vertical_compose97iL0(b1, b2, b3, width);
  621. if (y + 0 < (unsigned)height)
  622. vertical_compose97iH0(b0, b1, b2, width);
  623. }
  624. if (y - 1 < (unsigned)height)
  625. dsp->horizontal_compose97i(b0, width);
  626. if (y + 0 < (unsigned)height)
  627. dsp->horizontal_compose97i(b1, width);
  628. cs->b0 = b2;
  629. cs->b1 = b3;
  630. cs->b2 = b4;
  631. cs->b3 = b5;
  632. cs->y += 2;
  633. }
  634. static void spatial_compose97i_dy(DWTCompose *cs, IDWTELEM *buffer, int width,
  635. int height, int stride)
  636. {
  637. int y = cs->y;
  638. IDWTELEM *b0 = cs->b0;
  639. IDWTELEM *b1 = cs->b1;
  640. IDWTELEM *b2 = cs->b2;
  641. IDWTELEM *b3 = cs->b3;
  642. IDWTELEM *b4 = buffer + mirror(y + 3, height - 1) * stride;
  643. IDWTELEM *b5 = buffer + mirror(y + 4, height - 1) * stride;
  644. if (y + 3 < (unsigned)height)
  645. vertical_compose97iL1(b3, b4, b5, width);
  646. if (y + 2 < (unsigned)height)
  647. vertical_compose97iH1(b2, b3, b4, width);
  648. if (y + 1 < (unsigned)height)
  649. vertical_compose97iL0(b1, b2, b3, width);
  650. if (y + 0 < (unsigned)height)
  651. vertical_compose97iH0(b0, b1, b2, width);
  652. if (y - 1 < (unsigned)height)
  653. ff_snow_horizontal_compose97i(b0, width);
  654. if (y + 0 < (unsigned)height)
  655. ff_snow_horizontal_compose97i(b1, width);
  656. cs->b0 = b2;
  657. cs->b1 = b3;
  658. cs->b2 = b4;
  659. cs->b3 = b5;
  660. cs->y += 2;
  661. }
  662. static void av_unused spatial_compose97i(IDWTELEM *buffer, int width,
  663. int height, int stride)
  664. {
  665. DWTCompose cs;
  666. spatial_compose97i_init(&cs, buffer, height, stride);
  667. while (cs.y <= height)
  668. spatial_compose97i_dy(&cs, buffer, width, height, stride);
  669. }
  670. void ff_spatial_idwt_buffered_init(DWTCompose *cs, slice_buffer *sb, int width,
  671. int height, int stride_line, int type,
  672. int decomposition_count)
  673. {
  674. int level;
  675. for (level = decomposition_count - 1; level >= 0; level--) {
  676. switch (type) {
  677. case DWT_97:
  678. spatial_compose97i_buffered_init(cs + level, sb, height >> level,
  679. stride_line << level);
  680. break;
  681. case DWT_53:
  682. spatial_compose53i_buffered_init(cs + level, sb, height >> level,
  683. stride_line << level);
  684. break;
  685. }
  686. }
  687. }
  688. void ff_spatial_idwt_buffered_slice(DWTContext *dsp, DWTCompose *cs,
  689. slice_buffer *slice_buf, int width,
  690. int height, int stride_line, int type,
  691. int decomposition_count, int y)
  692. {
  693. const int support = type == 1 ? 3 : 5;
  694. int level;
  695. if (type == 2)
  696. return;
  697. for (level = decomposition_count - 1; level >= 0; level--)
  698. while (cs[level].y <= FFMIN((y >> level) + support, height >> level)) {
  699. switch (type) {
  700. case DWT_97:
  701. spatial_compose97i_dy_buffered(dsp, cs + level, slice_buf,
  702. width >> level,
  703. height >> level,
  704. stride_line << level);
  705. break;
  706. case DWT_53:
  707. spatial_compose53i_dy_buffered(cs + level, slice_buf,
  708. width >> level,
  709. height >> level,
  710. stride_line << level);
  711. break;
  712. }
  713. }
  714. }
  715. static void ff_spatial_idwt_init(DWTCompose *cs, IDWTELEM *buffer, int width,
  716. int height, int stride, int type,
  717. int decomposition_count)
  718. {
  719. int level;
  720. for (level = decomposition_count - 1; level >= 0; level--) {
  721. switch (type) {
  722. case DWT_97:
  723. spatial_compose97i_init(cs + level, buffer, height >> level,
  724. stride << level);
  725. break;
  726. case DWT_53:
  727. spatial_compose53i_init(cs + level, buffer, height >> level,
  728. stride << level);
  729. break;
  730. }
  731. }
  732. }
  733. static void ff_spatial_idwt_slice(DWTCompose *cs, IDWTELEM *buffer, int width,
  734. int height, int stride, int type,
  735. int decomposition_count, int y)
  736. {
  737. const int support = type == 1 ? 3 : 5;
  738. int level;
  739. if (type == 2)
  740. return;
  741. for (level = decomposition_count - 1; level >= 0; level--)
  742. while (cs[level].y <= FFMIN((y >> level) + support, height >> level)) {
  743. switch (type) {
  744. case DWT_97:
  745. spatial_compose97i_dy(cs + level, buffer, width >> level,
  746. height >> level, stride << level);
  747. break;
  748. case DWT_53:
  749. spatial_compose53i_dy(cs + level, buffer, width >> level,
  750. height >> level, stride << level);
  751. break;
  752. }
  753. }
  754. }
  755. void ff_spatial_idwt(IDWTELEM *buffer, int width, int height, int stride,
  756. int type, int decomposition_count)
  757. {
  758. DWTCompose cs[MAX_DECOMPOSITIONS];
  759. int y;
  760. ff_spatial_idwt_init(cs, buffer, width, height, stride, type,
  761. decomposition_count);
  762. for (y = 0; y < height; y += 4)
  763. ff_spatial_idwt_slice(cs, buffer, width, height, stride, type,
  764. decomposition_count, y);
  765. }
  766. static inline int w_c(void *v, uint8_t *pix1, uint8_t *pix2, int line_size,
  767. int w, int h, int type)
  768. {
  769. int s, i, j;
  770. const int dec_count = w == 8 ? 3 : 4;
  771. int tmp[32 * 32];
  772. int level, ori;
  773. static const int scale[2][2][4][4] = {
  774. {
  775. { // 9/7 8x8 dec=3
  776. { 268, 239, 239, 213 },
  777. { 0, 224, 224, 152 },
  778. { 0, 135, 135, 110 },
  779. },
  780. { // 9/7 16x16 or 32x32 dec=4
  781. { 344, 310, 310, 280 },
  782. { 0, 320, 320, 228 },
  783. { 0, 175, 175, 136 },
  784. { 0, 129, 129, 102 },
  785. }
  786. },
  787. {
  788. { // 5/3 8x8 dec=3
  789. { 275, 245, 245, 218 },
  790. { 0, 230, 230, 156 },
  791. { 0, 138, 138, 113 },
  792. },
  793. { // 5/3 16x16 or 32x32 dec=4
  794. { 352, 317, 317, 286 },
  795. { 0, 328, 328, 233 },
  796. { 0, 180, 180, 140 },
  797. { 0, 132, 132, 105 },
  798. }
  799. }
  800. };
  801. for (i = 0; i < h; i++) {
  802. for (j = 0; j < w; j += 4) {
  803. tmp[32 * i + j + 0] = (pix1[j + 0] - pix2[j + 0]) << 4;
  804. tmp[32 * i + j + 1] = (pix1[j + 1] - pix2[j + 1]) << 4;
  805. tmp[32 * i + j + 2] = (pix1[j + 2] - pix2[j + 2]) << 4;
  806. tmp[32 * i + j + 3] = (pix1[j + 3] - pix2[j + 3]) << 4;
  807. }
  808. pix1 += line_size;
  809. pix2 += line_size;
  810. }
  811. ff_spatial_dwt(tmp, w, h, 32, type, dec_count);
  812. s = 0;
  813. assert(w == h);
  814. for (level = 0; level < dec_count; level++)
  815. for (ori = level ? 1 : 0; ori < 4; ori++) {
  816. int size = w >> (dec_count - level);
  817. int sx = (ori & 1) ? size : 0;
  818. int stride = 32 << (dec_count - level);
  819. int sy = (ori & 2) ? stride >> 1 : 0;
  820. for (i = 0; i < size; i++)
  821. for (j = 0; j < size; j++) {
  822. int v = tmp[sx + sy + i * stride + j] *
  823. scale[type][dec_count - 3][level][ori];
  824. s += FFABS(v);
  825. }
  826. }
  827. assert(s >= 0);
  828. return s >> 9;
  829. }
  830. static int w53_8_c(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h)
  831. {
  832. return w_c(v, pix1, pix2, line_size, 8, h, 1);
  833. }
  834. static int w97_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, 0);
  837. }
  838. static int w53_16_c(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h)
  839. {
  840. return w_c(v, pix1, pix2, line_size, 16, h, 1);
  841. }
  842. static int w97_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, 0);
  845. }
  846. int ff_w53_32_c(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h)
  847. {
  848. return w_c(v, pix1, pix2, line_size, 32, h, 1);
  849. }
  850. int ff_w97_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, 0);
  853. }
  854. void ff_dsputil_init_dwt(DSPContext *c)
  855. {
  856. c->w53[0] = w53_16_c;
  857. c->w53[1] = w53_8_c;
  858. c->w97[0] = w97_16_c;
  859. c->w97[1] = w97_8_c;
  860. }
  861. void ff_dwt_init(DWTContext *c)
  862. {
  863. c->vertical_compose97i = ff_snow_vertical_compose97i;
  864. c->horizontal_compose97i = ff_snow_horizontal_compose97i;
  865. c->inner_add_yblock = ff_snow_inner_add_yblock;
  866. if (HAVE_MMX)
  867. ff_dwt_init_x86(c);
  868. }