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.

862 lines
28KB

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