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.

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