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.

866 lines
28KB

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