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.

880 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. static void av_unused spatial_compose53i(IDWTELEM *buffer, IDWTELEM *temp,
  397. int width, int height, int stride)
  398. {
  399. DWTCompose cs;
  400. spatial_compose53i_init(&cs, buffer, height, stride);
  401. while (cs.y <= height)
  402. spatial_compose53i_dy(&cs, buffer, temp, width, height, stride);
  403. }
  404. void ff_snow_horizontal_compose97i(IDWTELEM *b, IDWTELEM *temp, int width)
  405. {
  406. const int w2 = (width + 1) >> 1;
  407. int x;
  408. temp[0] = b[0] - ((3 * b[w2] + 2) >> 2);
  409. for (x = 1; x < (width >> 1); x++) {
  410. temp[2 * x] = b[x] - ((3 * (b[x + w2 - 1] + b[x + w2]) + 4) >> 3);
  411. temp[2 * x - 1] = b[x + w2 - 1] - temp[2 * x - 2] - temp[2 * x];
  412. }
  413. if (width & 1) {
  414. temp[2 * x] = b[x] - ((3 * b[x + w2 - 1] + 2) >> 2);
  415. temp[2 * x - 1] = b[x + w2 - 1] - temp[2 * x - 2] - temp[2 * x];
  416. } else
  417. temp[2 * x - 1] = b[x + w2 - 1] - 2 * temp[2 * x - 2];
  418. b[0] = temp[0] + ((2 * temp[0] + temp[1] + 4) >> 3);
  419. for (x = 2; x < width - 1; x += 2) {
  420. b[x] = temp[x] + ((4 * temp[x] + temp[x - 1] + temp[x + 1] + 8) >> 4);
  421. b[x - 1] = temp[x - 1] + ((3 * (b[x - 2] + b[x])) >> 1);
  422. }
  423. if (width & 1) {
  424. b[x] = temp[x] + ((2 * temp[x] + temp[x - 1] + 4) >> 3);
  425. b[x - 1] = temp[x - 1] + ((3 * (b[x - 2] + b[x])) >> 1);
  426. } else
  427. b[x - 1] = temp[x - 1] + 3 * b[x - 2];
  428. }
  429. static void vertical_compose97iH0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2,
  430. int width)
  431. {
  432. int i;
  433. for (i = 0; i < width; i++)
  434. b1[i] += (W_AM * (b0[i] + b2[i]) + W_AO) >> W_AS;
  435. }
  436. static void vertical_compose97iH1(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2,
  437. int width)
  438. {
  439. int i;
  440. for (i = 0; i < width; i++)
  441. b1[i] -= (W_CM * (b0[i] + b2[i]) + W_CO) >> W_CS;
  442. }
  443. static void vertical_compose97iL0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2,
  444. int width)
  445. {
  446. int i;
  447. for (i = 0; i < width; i++)
  448. b1[i] += (W_BM * (b0[i] + b2[i]) + 4 * b1[i] + W_BO) >> W_BS;
  449. }
  450. static void vertical_compose97iL1(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2,
  451. int width)
  452. {
  453. int i;
  454. for (i = 0; i < width; i++)
  455. b1[i] -= (W_DM * (b0[i] + b2[i]) + W_DO) >> W_DS;
  456. }
  457. void ff_snow_vertical_compose97i(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2,
  458. IDWTELEM *b3, IDWTELEM *b4, IDWTELEM *b5,
  459. int width)
  460. {
  461. int i;
  462. for (i = 0; i < width; i++) {
  463. b4[i] -= (W_DM * (b3[i] + b5[i]) + W_DO) >> W_DS;
  464. b3[i] -= (W_CM * (b2[i] + b4[i]) + W_CO) >> W_CS;
  465. b2[i] += (W_BM * (b1[i] + b3[i]) + 4 * b2[i] + W_BO) >> W_BS;
  466. b1[i] += (W_AM * (b0[i] + b2[i]) + W_AO) >> W_AS;
  467. }
  468. }
  469. static void spatial_compose97i_buffered_init(DWTCompose *cs, slice_buffer *sb,
  470. int height, int stride_line)
  471. {
  472. cs->b0 = slice_buffer_get_line(sb, mirror(-3 - 1, height - 1) * stride_line);
  473. cs->b1 = slice_buffer_get_line(sb, mirror(-3, height - 1) * stride_line);
  474. cs->b2 = slice_buffer_get_line(sb, mirror(-3 + 1, height - 1) * stride_line);
  475. cs->b3 = slice_buffer_get_line(sb, mirror(-3 + 2, height - 1) * stride_line);
  476. cs->y = -3;
  477. }
  478. static void spatial_compose97i_init(DWTCompose *cs, IDWTELEM *buffer, int height,
  479. int stride)
  480. {
  481. cs->b0 = buffer + mirror(-3 - 1, height - 1) * stride;
  482. cs->b1 = buffer + mirror(-3, height - 1) * stride;
  483. cs->b2 = buffer + mirror(-3 + 1, height - 1) * stride;
  484. cs->b3 = buffer + mirror(-3 + 2, height - 1) * stride;
  485. cs->y = -3;
  486. }
  487. static void spatial_compose97i_dy_buffered(DWTContext *dsp, DWTCompose *cs,
  488. slice_buffer * sb, IDWTELEM *temp,
  489. int width, int height,
  490. int stride_line)
  491. {
  492. int y = cs->y;
  493. IDWTELEM *b0 = cs->b0;
  494. IDWTELEM *b1 = cs->b1;
  495. IDWTELEM *b2 = cs->b2;
  496. IDWTELEM *b3 = cs->b3;
  497. IDWTELEM *b4 = slice_buffer_get_line(sb,
  498. mirror(y + 3, height - 1) *
  499. stride_line);
  500. IDWTELEM *b5 = slice_buffer_get_line(sb,
  501. mirror(y + 4, height - 1) *
  502. stride_line);
  503. if (y > 0 && y + 4 < height) {
  504. dsp->vertical_compose97i(b0, b1, b2, b3, b4, b5, width);
  505. } else {
  506. if (y + 3 < (unsigned)height)
  507. vertical_compose97iL1(b3, b4, b5, width);
  508. if (y + 2 < (unsigned)height)
  509. vertical_compose97iH1(b2, b3, b4, width);
  510. if (y + 1 < (unsigned)height)
  511. vertical_compose97iL0(b1, b2, b3, width);
  512. if (y + 0 < (unsigned)height)
  513. vertical_compose97iH0(b0, b1, b2, width);
  514. }
  515. if (y - 1 < (unsigned)height)
  516. dsp->horizontal_compose97i(b0, temp, width);
  517. if (y + 0 < (unsigned)height)
  518. dsp->horizontal_compose97i(b1, temp, width);
  519. cs->b0 = b2;
  520. cs->b1 = b3;
  521. cs->b2 = b4;
  522. cs->b3 = b5;
  523. cs->y += 2;
  524. }
  525. static void spatial_compose97i_dy(DWTCompose *cs, IDWTELEM *buffer,
  526. IDWTELEM *temp, int width, int height,
  527. int stride)
  528. {
  529. int y = cs->y;
  530. IDWTELEM *b0 = cs->b0;
  531. IDWTELEM *b1 = cs->b1;
  532. IDWTELEM *b2 = cs->b2;
  533. IDWTELEM *b3 = cs->b3;
  534. IDWTELEM *b4 = buffer + mirror(y + 3, height - 1) * stride;
  535. IDWTELEM *b5 = buffer + mirror(y + 4, height - 1) * stride;
  536. if (y + 3 < (unsigned)height)
  537. vertical_compose97iL1(b3, b4, b5, width);
  538. if (y + 2 < (unsigned)height)
  539. vertical_compose97iH1(b2, b3, b4, width);
  540. if (y + 1 < (unsigned)height)
  541. vertical_compose97iL0(b1, b2, b3, width);
  542. if (y + 0 < (unsigned)height)
  543. vertical_compose97iH0(b0, b1, b2, width);
  544. if (y - 1 < (unsigned)height)
  545. ff_snow_horizontal_compose97i(b0, temp, width);
  546. if (y + 0 < (unsigned)height)
  547. ff_snow_horizontal_compose97i(b1, temp, width);
  548. cs->b0 = b2;
  549. cs->b1 = b3;
  550. cs->b2 = b4;
  551. cs->b3 = b5;
  552. cs->y += 2;
  553. }
  554. static void av_unused spatial_compose97i(IDWTELEM *buffer, IDWTELEM *temp,
  555. int width, int height, int stride)
  556. {
  557. DWTCompose cs;
  558. spatial_compose97i_init(&cs, buffer, height, stride);
  559. while (cs.y <= height)
  560. spatial_compose97i_dy(&cs, buffer, temp, width, height, stride);
  561. }
  562. void ff_spatial_idwt_buffered_init(DWTCompose *cs, slice_buffer *sb, int width,
  563. int height, int stride_line, int type,
  564. int decomposition_count)
  565. {
  566. int level;
  567. for (level = decomposition_count - 1; level >= 0; level--) {
  568. switch (type) {
  569. case DWT_97:
  570. spatial_compose97i_buffered_init(cs + level, sb, height >> level,
  571. stride_line << level);
  572. break;
  573. case DWT_53:
  574. spatial_compose53i_buffered_init(cs + level, sb, height >> level,
  575. stride_line << level);
  576. break;
  577. }
  578. }
  579. }
  580. void ff_spatial_idwt_buffered_slice(DWTContext *dsp, DWTCompose *cs,
  581. slice_buffer *slice_buf, IDWTELEM *temp,
  582. int width, int height, int stride_line,
  583. int type, int decomposition_count, int y)
  584. {
  585. const int support = type == 1 ? 3 : 5;
  586. int level;
  587. if (type == 2)
  588. return;
  589. for (level = decomposition_count - 1; level >= 0; level--)
  590. while (cs[level].y <= FFMIN((y >> level) + support, height >> level)) {
  591. switch (type) {
  592. case DWT_97:
  593. spatial_compose97i_dy_buffered(dsp, cs + level, slice_buf, temp,
  594. width >> level,
  595. height >> level,
  596. stride_line << level);
  597. break;
  598. case DWT_53:
  599. spatial_compose53i_dy_buffered(cs + level, slice_buf, temp,
  600. width >> level,
  601. height >> level,
  602. stride_line << level);
  603. break;
  604. }
  605. }
  606. }
  607. static void ff_spatial_idwt_init(DWTCompose *cs, IDWTELEM *buffer, int width,
  608. int height, int stride, int type,
  609. int decomposition_count)
  610. {
  611. int level;
  612. for (level = decomposition_count - 1; level >= 0; level--) {
  613. switch (type) {
  614. case DWT_97:
  615. spatial_compose97i_init(cs + level, buffer, height >> level,
  616. stride << level);
  617. break;
  618. case DWT_53:
  619. spatial_compose53i_init(cs + level, buffer, height >> level,
  620. stride << level);
  621. break;
  622. }
  623. }
  624. }
  625. static void ff_spatial_idwt_slice(DWTCompose *cs, IDWTELEM *buffer,
  626. IDWTELEM *temp, int width, int height,
  627. int stride, int type,
  628. int decomposition_count, int y)
  629. {
  630. const int support = type == 1 ? 3 : 5;
  631. int level;
  632. if (type == 2)
  633. return;
  634. for (level = decomposition_count - 1; level >= 0; level--)
  635. while (cs[level].y <= FFMIN((y >> level) + support, height >> level)) {
  636. switch (type) {
  637. case DWT_97:
  638. spatial_compose97i_dy(cs + level, buffer, temp, width >> level,
  639. height >> level, stride << level);
  640. break;
  641. case DWT_53:
  642. spatial_compose53i_dy(cs + level, buffer, temp, width >> level,
  643. height >> level, stride << level);
  644. break;
  645. }
  646. }
  647. }
  648. void ff_spatial_idwt(IDWTELEM *buffer, IDWTELEM *temp, int width, int height,
  649. int stride, int type, int decomposition_count)
  650. {
  651. DWTCompose cs[MAX_DECOMPOSITIONS];
  652. int y;
  653. ff_spatial_idwt_init(cs, buffer, width, height, stride, type,
  654. decomposition_count);
  655. for (y = 0; y < height; y += 4)
  656. ff_spatial_idwt_slice(cs, buffer, temp, width, height, stride, type,
  657. decomposition_count, y);
  658. }
  659. static inline int w_c(void *v, uint8_t *pix1, uint8_t *pix2, int line_size,
  660. int w, int h, int type)
  661. {
  662. int s, i, j;
  663. const int dec_count = w == 8 ? 3 : 4;
  664. int tmp[32 * 32], tmp2[32];
  665. int level, ori;
  666. static const int scale[2][2][4][4] = {
  667. {
  668. { // 9/7 8x8 dec=3
  669. { 268, 239, 239, 213 },
  670. { 0, 224, 224, 152 },
  671. { 0, 135, 135, 110 },
  672. },
  673. { // 9/7 16x16 or 32x32 dec=4
  674. { 344, 310, 310, 280 },
  675. { 0, 320, 320, 228 },
  676. { 0, 175, 175, 136 },
  677. { 0, 129, 129, 102 },
  678. }
  679. },
  680. {
  681. { // 5/3 8x8 dec=3
  682. { 275, 245, 245, 218 },
  683. { 0, 230, 230, 156 },
  684. { 0, 138, 138, 113 },
  685. },
  686. { // 5/3 16x16 or 32x32 dec=4
  687. { 352, 317, 317, 286 },
  688. { 0, 328, 328, 233 },
  689. { 0, 180, 180, 140 },
  690. { 0, 132, 132, 105 },
  691. }
  692. }
  693. };
  694. for (i = 0; i < h; i++) {
  695. for (j = 0; j < w; j += 4) {
  696. tmp[32 * i + j + 0] = (pix1[j + 0] - pix2[j + 0]) << 4;
  697. tmp[32 * i + j + 1] = (pix1[j + 1] - pix2[j + 1]) << 4;
  698. tmp[32 * i + j + 2] = (pix1[j + 2] - pix2[j + 2]) << 4;
  699. tmp[32 * i + j + 3] = (pix1[j + 3] - pix2[j + 3]) << 4;
  700. }
  701. pix1 += line_size;
  702. pix2 += line_size;
  703. }
  704. ff_spatial_dwt(tmp, tmp2, w, h, 32, type, dec_count);
  705. s = 0;
  706. assert(w == h);
  707. for (level = 0; level < dec_count; level++)
  708. for (ori = level ? 1 : 0; ori < 4; ori++) {
  709. int size = w >> (dec_count - level);
  710. int sx = (ori & 1) ? size : 0;
  711. int stride = 32 << (dec_count - level);
  712. int sy = (ori & 2) ? stride >> 1 : 0;
  713. for (i = 0; i < size; i++)
  714. for (j = 0; j < size; j++) {
  715. int v = tmp[sx + sy + i * stride + j] *
  716. scale[type][dec_count - 3][level][ori];
  717. s += FFABS(v);
  718. }
  719. }
  720. assert(s >= 0);
  721. return s >> 9;
  722. }
  723. static int w53_8_c(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h)
  724. {
  725. return w_c(v, pix1, pix2, line_size, 8, h, 1);
  726. }
  727. static int w97_8_c(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h)
  728. {
  729. return w_c(v, pix1, pix2, line_size, 8, h, 0);
  730. }
  731. static int w53_16_c(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h)
  732. {
  733. return w_c(v, pix1, pix2, line_size, 16, h, 1);
  734. }
  735. static int w97_16_c(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h)
  736. {
  737. return w_c(v, pix1, pix2, line_size, 16, h, 0);
  738. }
  739. int ff_w53_32_c(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h)
  740. {
  741. return w_c(v, pix1, pix2, line_size, 32, h, 1);
  742. }
  743. int ff_w97_32_c(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h)
  744. {
  745. return w_c(v, pix1, pix2, line_size, 32, h, 0);
  746. }
  747. void ff_dsputil_init_dwt(DSPContext *c)
  748. {
  749. c->w53[0] = w53_16_c;
  750. c->w53[1] = w53_8_c;
  751. c->w97[0] = w97_16_c;
  752. c->w97[1] = w97_8_c;
  753. }
  754. void ff_dwt_init(DWTContext *c)
  755. {
  756. c->vertical_compose97i = ff_snow_vertical_compose97i;
  757. c->horizontal_compose97i = ff_snow_horizontal_compose97i;
  758. c->inner_add_yblock = ff_snow_inner_add_yblock;
  759. if (HAVE_MMX)
  760. ff_dwt_init_x86(c);
  761. }