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.

999 lines
32KB

  1. /*
  2. * Copyright (C) 2004-2010 Michael Niedermayer <michaelni@gmx.at>
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * Libav is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with Libav; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "libavutil/attributes.h"
  21. #include "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 inv_lift(IDWTELEM *dst, IDWTELEM *src, IDWTELEM *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. #define LIFT(src, ref, inv) ((src) + ((inv) ? -(ref) : +(ref)))
  137. if (mirror_left) {
  138. dst[0] = LIFT(src[0], ((mul * 2 * ref[0] + add) >> shift), inverse);
  139. dst += dst_step;
  140. src += src_step;
  141. }
  142. for (i = 0; i < w; i++)
  143. dst[i * dst_step] = LIFT(src[i * src_step],
  144. ((mul * (ref[i * ref_step] +
  145. ref[(i + 1) * ref_step]) +
  146. add) >> shift),
  147. inverse);
  148. if (mirror_right) {
  149. dst[w * dst_step] = LIFT(src[w * src_step],
  150. ((mul * 2 * ref[w * ref_step] + add) >> shift),
  151. inverse);
  152. }
  153. }
  154. #ifndef liftS
  155. static av_always_inline void liftS(DWTELEM *dst, DWTELEM *src, DWTELEM *ref,
  156. int dst_step, int src_step, int ref_step,
  157. int width, int mul, int add, int shift,
  158. int highpass, int inverse)
  159. {
  160. const int mirror_left = !highpass;
  161. const int mirror_right = (width & 1) ^ highpass;
  162. const int w = (width >> 1) - 1 + (highpass & width);
  163. int i;
  164. assert(shift == 4);
  165. #define LIFTS(src, ref, inv) \
  166. ((inv) ? (src) + (((ref) + 4 * (src)) >> shift) \
  167. : -((-16 * (src) + (ref) + add / \
  168. 4 + 1 + (5 << 25)) / (5 * 4) - (1 << 23)))
  169. if (mirror_left) {
  170. dst[0] = LIFTS(src[0], mul * 2 * ref[0] + add, inverse);
  171. dst += dst_step;
  172. src += src_step;
  173. }
  174. for (i = 0; i < w; i++)
  175. dst[i * dst_step] = LIFTS(src[i * src_step],
  176. mul * (ref[i * ref_step] +
  177. ref[(i + 1) * ref_step]) + add,
  178. inverse);
  179. if (mirror_right)
  180. dst[w * dst_step] = LIFTS(src[w * src_step],
  181. mul * 2 * ref[w * ref_step] + add,
  182. inverse);
  183. }
  184. static av_always_inline void inv_liftS(IDWTELEM *dst, IDWTELEM *src,
  185. IDWTELEM *ref, int dst_step,
  186. int src_step, int ref_step,
  187. int width, int mul, int add, int shift,
  188. int highpass, int inverse)
  189. {
  190. const int mirror_left = !highpass;
  191. const int mirror_right = (width & 1) ^ highpass;
  192. const int w = (width >> 1) - 1 + (highpass & width);
  193. int i;
  194. assert(shift == 4);
  195. #define LIFTS(src, ref, inv) \
  196. ((inv) ? (src) + (((ref) + 4 * (src)) >> shift) \
  197. : -((-16 * (src) + (ref) + add / \
  198. 4 + 1 + (5 << 25)) / (5 * 4) - (1 << 23)))
  199. if (mirror_left) {
  200. dst[0] = LIFTS(src[0], mul * 2 * ref[0] + add, inverse);
  201. dst += dst_step;
  202. src += src_step;
  203. }
  204. for (i = 0; i < w; i++)
  205. dst[i * dst_step] = LIFTS(src[i * src_step],
  206. mul * (ref[i * ref_step] +
  207. ref[(i + 1) * ref_step]) + add,
  208. inverse);
  209. if (mirror_right)
  210. dst[w * dst_step] = LIFTS(src[w * src_step],
  211. mul * 2 * ref[w * ref_step] + add, inverse);
  212. }
  213. #endif /* ! liftS */
  214. static void horizontal_decompose53i(DWTELEM *b, DWTELEM *temp, int width)
  215. {
  216. const int width2 = width >> 1;
  217. int x;
  218. const int w2 = (width + 1) >> 1;
  219. for (x = 0; x < width2; x++) {
  220. temp[x] = b[2 * x];
  221. temp[x + w2] = b[2 * x + 1];
  222. }
  223. if (width & 1)
  224. temp[x] = b[2 * x];
  225. #if 0
  226. {
  227. int A1, A2, A3, A4;
  228. A2 = temp[1];
  229. A4 = temp[0];
  230. A1 = temp[0 + width2];
  231. A1 -= (A2 + A4) >> 1;
  232. A4 += (A1 + 1) >> 1;
  233. b[0 + width2] = A1;
  234. b[0] = A4;
  235. for (x = 1; x + 1 < width2; x += 2) {
  236. A3 = temp[x + width2];
  237. A4 = temp[x + 1];
  238. A3 -= (A2 + A4) >> 1;
  239. A2 += (A1 + A3 + 2) >> 2;
  240. b[x + width2] = A3;
  241. b[x] = A2;
  242. A1 = temp[x + 1 + width2];
  243. A2 = temp[x + 2];
  244. A1 -= (A2 + A4) >> 1;
  245. A4 += (A1 + A3 + 2) >> 2;
  246. b[x + 1 + width2] = A1;
  247. b[x + 1] = A4;
  248. }
  249. A3 = temp[width - 1];
  250. A3 -= A2;
  251. A2 += (A1 + A3 + 2) >> 2;
  252. b[width - 1] = A3;
  253. b[width2 - 1] = A2;
  254. }
  255. #else
  256. lift(b + w2, temp + w2, temp, 1, 1, 1, width, -1, 0, 1, 1, 0);
  257. lift(b, temp, b + w2, 1, 1, 1, width, 1, 2, 2, 0, 0);
  258. #endif /* 0 */
  259. }
  260. static void vertical_decompose53iH0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2,
  261. int width)
  262. {
  263. int i;
  264. for (i = 0; i < width; i++)
  265. b1[i] -= (b0[i] + b2[i]) >> 1;
  266. }
  267. static void vertical_decompose53iL0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2,
  268. int width)
  269. {
  270. int i;
  271. for (i = 0; i < width; i++)
  272. b1[i] += (b0[i] + b2[i] + 2) >> 2;
  273. }
  274. static void spatial_decompose53i(DWTELEM *buffer, DWTELEM *temp,
  275. int width, int height, int stride)
  276. {
  277. int y;
  278. DWTELEM *b0 = buffer + mirror(-2 - 1, height - 1) * stride;
  279. DWTELEM *b1 = buffer + mirror(-2, height - 1) * stride;
  280. for (y = -2; y < height; y += 2) {
  281. DWTELEM *b2 = buffer + mirror(y + 1, height - 1) * stride;
  282. DWTELEM *b3 = buffer + mirror(y + 2, height - 1) * stride;
  283. if (y + 1 < (unsigned)height)
  284. horizontal_decompose53i(b2, temp, width);
  285. if (y + 2 < (unsigned)height)
  286. horizontal_decompose53i(b3, temp, width);
  287. if (y + 1 < (unsigned)height)
  288. vertical_decompose53iH0(b1, b2, b3, width);
  289. if (y + 0 < (unsigned)height)
  290. vertical_decompose53iL0(b0, b1, b2, width);
  291. b0 = b2;
  292. b1 = b3;
  293. }
  294. }
  295. static void horizontal_decompose97i(DWTELEM *b, DWTELEM *temp, int width)
  296. {
  297. const int w2 = (width + 1) >> 1;
  298. lift(temp + w2, b + 1, b, 1, 2, 2, width, W_AM, W_AO, W_AS, 1, 1);
  299. liftS(temp, b, temp + w2, 1, 2, 1, width, W_BM, W_BO, W_BS, 0, 0);
  300. lift(b + w2, temp + w2, temp, 1, 1, 1, width, W_CM, W_CO, W_CS, 1, 0);
  301. lift(b, temp, b + w2, 1, 1, 1, width, W_DM, W_DO, W_DS, 0, 0);
  302. }
  303. static void vertical_decompose97iH0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2,
  304. int width)
  305. {
  306. int i;
  307. for (i = 0; i < width; i++)
  308. b1[i] -= (W_AM * (b0[i] + b2[i]) + W_AO) >> W_AS;
  309. }
  310. static void vertical_decompose97iH1(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2,
  311. int width)
  312. {
  313. int i;
  314. for (i = 0; i < width; i++)
  315. b1[i] += (W_CM * (b0[i] + b2[i]) + W_CO) >> W_CS;
  316. }
  317. static void vertical_decompose97iL0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2,
  318. int width)
  319. {
  320. int i;
  321. for (i = 0; i < width; i++)
  322. #ifdef liftS
  323. b1[i] -= (W_BM * (b0[i] + b2[i]) + W_BO) >> W_BS;
  324. #else
  325. b1[i] = (16 * 4 * b1[i] - 4 * (b0[i] + b2[i]) + W_BO * 5 + (5 << 27)) /
  326. (5 * 16) - (1 << 23);
  327. #endif
  328. }
  329. static void vertical_decompose97iL1(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2,
  330. int width)
  331. {
  332. int i;
  333. for (i = 0; i < width; i++)
  334. b1[i] += (W_DM * (b0[i] + b2[i]) + W_DO) >> W_DS;
  335. }
  336. static void spatial_decompose97i(DWTELEM *buffer, DWTELEM *temp,
  337. int width, int height, int stride)
  338. {
  339. int y;
  340. DWTELEM *b0 = buffer + mirror(-4 - 1, height - 1) * stride;
  341. DWTELEM *b1 = buffer + mirror(-4, height - 1) * stride;
  342. DWTELEM *b2 = buffer + mirror(-4 + 1, height - 1) * stride;
  343. DWTELEM *b3 = buffer + mirror(-4 + 2, height - 1) * stride;
  344. for (y = -4; y < height; y += 2) {
  345. DWTELEM *b4 = buffer + mirror(y + 3, height - 1) * stride;
  346. DWTELEM *b5 = buffer + mirror(y + 4, height - 1) * stride;
  347. if (y + 3 < (unsigned)height)
  348. horizontal_decompose97i(b4, temp, width);
  349. if (y + 4 < (unsigned)height)
  350. horizontal_decompose97i(b5, temp, width);
  351. if (y + 3 < (unsigned)height)
  352. vertical_decompose97iH0(b3, b4, b5, width);
  353. if (y + 2 < (unsigned)height)
  354. vertical_decompose97iL0(b2, b3, b4, width);
  355. if (y + 1 < (unsigned)height)
  356. vertical_decompose97iH1(b1, b2, b3, width);
  357. if (y + 0 < (unsigned)height)
  358. vertical_decompose97iL1(b0, b1, b2, width);
  359. b0 = b2;
  360. b1 = b3;
  361. b2 = b4;
  362. b3 = b5;
  363. }
  364. }
  365. void ff_spatial_dwt(DWTELEM *buffer, DWTELEM *temp, int width, int height,
  366. int stride, int type, int decomposition_count)
  367. {
  368. int level;
  369. for (level = 0; level < decomposition_count; level++) {
  370. switch (type) {
  371. case DWT_97:
  372. spatial_decompose97i(buffer, temp,
  373. width >> level, height >> level,
  374. stride << level);
  375. break;
  376. case DWT_53:
  377. spatial_decompose53i(buffer, temp,
  378. width >> level, height >> level,
  379. stride << level);
  380. break;
  381. }
  382. }
  383. }
  384. static void horizontal_compose53i(IDWTELEM *b, IDWTELEM *temp, int width)
  385. {
  386. const int width2 = width >> 1;
  387. const int w2 = (width + 1) >> 1;
  388. int x;
  389. for (x = 0; x < width2; x++) {
  390. temp[2 * x] = b[x];
  391. temp[2 * x + 1] = b[x + w2];
  392. }
  393. if (width & 1)
  394. temp[2 * x] = b[x];
  395. b[0] = temp[0] - ((temp[1] + 1) >> 1);
  396. for (x = 2; x < width - 1; x += 2) {
  397. b[x] = temp[x] - ((temp[x - 1] + temp[x + 1] + 2) >> 2);
  398. b[x - 1] = temp[x - 1] + ((b[x - 2] + b[x] + 1) >> 1);
  399. }
  400. if (width & 1) {
  401. b[x] = temp[x] - ((temp[x - 1] + 1) >> 1);
  402. b[x - 1] = temp[x - 1] + ((b[x - 2] + b[x] + 1) >> 1);
  403. } else
  404. b[x - 1] = temp[x - 1] + b[x - 2];
  405. }
  406. static void vertical_compose53iH0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2,
  407. int width)
  408. {
  409. int i;
  410. for (i = 0; i < width; i++)
  411. b1[i] += (b0[i] + b2[i]) >> 1;
  412. }
  413. static void vertical_compose53iL0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2,
  414. int width)
  415. {
  416. int i;
  417. for (i = 0; i < width; i++)
  418. b1[i] -= (b0[i] + b2[i] + 2) >> 2;
  419. }
  420. static void spatial_compose53i_buffered_init(DWTCompose *cs, slice_buffer *sb,
  421. int height, int stride_line)
  422. {
  423. cs->b0 = slice_buffer_get_line(sb,
  424. mirror(-1 - 1, height - 1) * stride_line);
  425. cs->b1 = slice_buffer_get_line(sb, mirror(-1, height - 1) * stride_line);
  426. cs->y = -1;
  427. }
  428. static void spatial_compose53i_init(DWTCompose *cs, IDWTELEM *buffer,
  429. int height, int stride)
  430. {
  431. cs->b0 = buffer + mirror(-1 - 1, height - 1) * stride;
  432. cs->b1 = buffer + mirror(-1, height - 1) * stride;
  433. cs->y = -1;
  434. }
  435. static void spatial_compose53i_dy_buffered(DWTCompose *cs, slice_buffer *sb,
  436. IDWTELEM *temp,
  437. int width, int height,
  438. int stride_line)
  439. {
  440. int y = cs->y;
  441. IDWTELEM *b0 = cs->b0;
  442. IDWTELEM *b1 = cs->b1;
  443. IDWTELEM *b2 = slice_buffer_get_line(sb,
  444. mirror(y + 1, height - 1) *
  445. stride_line);
  446. IDWTELEM *b3 = slice_buffer_get_line(sb,
  447. mirror(y + 2, height - 1) *
  448. stride_line);
  449. if (y + 1 < (unsigned)height && y < (unsigned)height) {
  450. int x;
  451. for (x = 0; x < width; x++) {
  452. b2[x] -= (b1[x] + b3[x] + 2) >> 2;
  453. b1[x] += (b0[x] + b2[x]) >> 1;
  454. }
  455. } else {
  456. if (y + 1 < (unsigned)height)
  457. vertical_compose53iL0(b1, b2, b3, width);
  458. if (y + 0 < (unsigned)height)
  459. vertical_compose53iH0(b0, b1, b2, width);
  460. }
  461. if (y - 1 < (unsigned)height)
  462. horizontal_compose53i(b0, temp, width);
  463. if (y + 0 < (unsigned)height)
  464. horizontal_compose53i(b1, temp, width);
  465. cs->b0 = b2;
  466. cs->b1 = b3;
  467. cs->y += 2;
  468. }
  469. static void spatial_compose53i_dy(DWTCompose *cs, IDWTELEM *buffer,
  470. IDWTELEM *temp, int width, int height,
  471. int stride)
  472. {
  473. int y = cs->y;
  474. IDWTELEM *b0 = cs->b0;
  475. IDWTELEM *b1 = cs->b1;
  476. IDWTELEM *b2 = buffer + mirror(y + 1, height - 1) * stride;
  477. IDWTELEM *b3 = buffer + mirror(y + 2, height - 1) * stride;
  478. if (y + 1 < (unsigned)height)
  479. vertical_compose53iL0(b1, b2, b3, width);
  480. if (y + 0 < (unsigned)height)
  481. vertical_compose53iH0(b0, b1, b2, width);
  482. if (y - 1 < (unsigned)height)
  483. horizontal_compose53i(b0, temp, width);
  484. if (y + 0 < (unsigned)height)
  485. horizontal_compose53i(b1, temp, width);
  486. cs->b0 = b2;
  487. cs->b1 = b3;
  488. cs->y += 2;
  489. }
  490. static void av_unused spatial_compose53i(IDWTELEM *buffer, IDWTELEM *temp,
  491. int width, int height, int stride)
  492. {
  493. DWTCompose cs;
  494. spatial_compose53i_init(&cs, buffer, height, stride);
  495. while (cs.y <= height)
  496. spatial_compose53i_dy(&cs, buffer, temp, width, height, stride);
  497. }
  498. void ff_snow_horizontal_compose97i(IDWTELEM *b, IDWTELEM *temp, int width)
  499. {
  500. const int w2 = (width + 1) >> 1;
  501. #if 0 //maybe more understadable but slower
  502. inv_lift(temp, b, b + w2, 2, 1, 1, width, W_DM, W_DO, W_DS, 0, 1);
  503. inv_lift(temp + 1, b + w2, temp, 2, 1, 2, width, W_CM, W_CO, W_CS, 1, 1);
  504. inv_liftS(b, temp, temp + 1, 2, 2, 2, width, W_BM, W_BO, W_BS, 0, 1);
  505. inv_lift(b + 1, temp + 1, b, 2, 2, 2, width, W_AM, W_AO, W_AS, 1, 0);
  506. #else
  507. int x;
  508. temp[0] = b[0] - ((3 * b[w2] + 2) >> 2);
  509. for (x = 1; x < (width >> 1); x++) {
  510. temp[2 * x] = b[x] - ((3 * (b[x + w2 - 1] + b[x + w2]) + 4) >> 3);
  511. temp[2 * x - 1] = b[x + w2 - 1] - temp[2 * x - 2] - temp[2 * x];
  512. }
  513. if (width & 1) {
  514. temp[2 * x] = b[x] - ((3 * b[x + w2 - 1] + 2) >> 2);
  515. temp[2 * x - 1] = b[x + w2 - 1] - temp[2 * x - 2] - temp[2 * x];
  516. } else
  517. temp[2 * x - 1] = b[x + w2 - 1] - 2 * temp[2 * x - 2];
  518. b[0] = temp[0] + ((2 * temp[0] + temp[1] + 4) >> 3);
  519. for (x = 2; x < width - 1; x += 2) {
  520. b[x] = temp[x] + ((4 * temp[x] + temp[x - 1] + temp[x + 1] + 8) >> 4);
  521. b[x - 1] = temp[x - 1] + ((3 * (b[x - 2] + b[x])) >> 1);
  522. }
  523. if (width & 1) {
  524. b[x] = temp[x] + ((2 * temp[x] + temp[x - 1] + 4) >> 3);
  525. b[x - 1] = temp[x - 1] + ((3 * (b[x - 2] + b[x])) >> 1);
  526. } else
  527. b[x - 1] = temp[x - 1] + 3 * b[x - 2];
  528. #endif
  529. }
  530. static void vertical_compose97iH0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2,
  531. int width)
  532. {
  533. int i;
  534. for (i = 0; i < width; i++)
  535. b1[i] += (W_AM * (b0[i] + b2[i]) + W_AO) >> W_AS;
  536. }
  537. static void vertical_compose97iH1(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2,
  538. int width)
  539. {
  540. int i;
  541. for (i = 0; i < width; i++)
  542. b1[i] -= (W_CM * (b0[i] + b2[i]) + W_CO) >> W_CS;
  543. }
  544. static void vertical_compose97iL0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2,
  545. int width)
  546. {
  547. int i;
  548. for (i = 0; i < width; i++)
  549. #ifdef liftS
  550. b1[i] += (W_BM * (b0[i] + b2[i]) + W_BO) >> W_BS;
  551. #else
  552. b1[i] += (W_BM * (b0[i] + b2[i]) + 4 * b1[i] + W_BO) >> W_BS;
  553. #endif
  554. }
  555. static void vertical_compose97iL1(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2,
  556. int width)
  557. {
  558. int i;
  559. for (i = 0; i < width; i++)
  560. b1[i] -= (W_DM * (b0[i] + b2[i]) + W_DO) >> W_DS;
  561. }
  562. void ff_snow_vertical_compose97i(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2,
  563. IDWTELEM *b3, IDWTELEM *b4, IDWTELEM *b5,
  564. int width)
  565. {
  566. int i;
  567. for (i = 0; i < width; i++) {
  568. b4[i] -= (W_DM * (b3[i] + b5[i]) + W_DO) >> W_DS;
  569. b3[i] -= (W_CM * (b2[i] + b4[i]) + W_CO) >> W_CS;
  570. #ifdef liftS
  571. b2[i] += (W_BM * (b1[i] + b3[i]) + W_BO) >> W_BS;
  572. #else
  573. b2[i] += (W_BM * (b1[i] + b3[i]) + 4 * b2[i] + W_BO) >> W_BS;
  574. #endif
  575. b1[i] += (W_AM * (b0[i] + b2[i]) + W_AO) >> W_AS;
  576. }
  577. }
  578. static void spatial_compose97i_buffered_init(DWTCompose *cs, slice_buffer *sb,
  579. int height, int stride_line)
  580. {
  581. cs->b0 = slice_buffer_get_line(sb, mirror(-3 - 1, height - 1) * stride_line);
  582. cs->b1 = slice_buffer_get_line(sb, mirror(-3, height - 1) * stride_line);
  583. cs->b2 = slice_buffer_get_line(sb, mirror(-3 + 1, height - 1) * stride_line);
  584. cs->b3 = slice_buffer_get_line(sb, mirror(-3 + 2, height - 1) * stride_line);
  585. cs->y = -3;
  586. }
  587. static void spatial_compose97i_init(DWTCompose *cs, IDWTELEM *buffer, int height,
  588. int stride)
  589. {
  590. cs->b0 = buffer + mirror(-3 - 1, height - 1) * stride;
  591. cs->b1 = buffer + mirror(-3, height - 1) * stride;
  592. cs->b2 = buffer + mirror(-3 + 1, height - 1) * stride;
  593. cs->b3 = buffer + mirror(-3 + 2, height - 1) * stride;
  594. cs->y = -3;
  595. }
  596. static void spatial_compose97i_dy_buffered(DWTContext *dsp, DWTCompose *cs,
  597. slice_buffer * sb, IDWTELEM *temp,
  598. int width, int height,
  599. int stride_line)
  600. {
  601. int y = cs->y;
  602. IDWTELEM *b0 = cs->b0;
  603. IDWTELEM *b1 = cs->b1;
  604. IDWTELEM *b2 = cs->b2;
  605. IDWTELEM *b3 = cs->b3;
  606. IDWTELEM *b4 = slice_buffer_get_line(sb,
  607. mirror(y + 3, height - 1) *
  608. stride_line);
  609. IDWTELEM *b5 = slice_buffer_get_line(sb,
  610. mirror(y + 4, height - 1) *
  611. stride_line);
  612. if (y > 0 && y + 4 < height) {
  613. dsp->vertical_compose97i(b0, b1, b2, b3, b4, b5, width);
  614. } else {
  615. if (y + 3 < (unsigned)height)
  616. vertical_compose97iL1(b3, b4, b5, width);
  617. if (y + 2 < (unsigned)height)
  618. vertical_compose97iH1(b2, b3, b4, width);
  619. if (y + 1 < (unsigned)height)
  620. vertical_compose97iL0(b1, b2, b3, width);
  621. if (y + 0 < (unsigned)height)
  622. vertical_compose97iH0(b0, b1, b2, width);
  623. }
  624. if (y - 1 < (unsigned)height)
  625. dsp->horizontal_compose97i(b0, temp, width);
  626. if (y + 0 < (unsigned)height)
  627. dsp->horizontal_compose97i(b1, temp, width);
  628. cs->b0 = b2;
  629. cs->b1 = b3;
  630. cs->b2 = b4;
  631. cs->b3 = b5;
  632. cs->y += 2;
  633. }
  634. static void spatial_compose97i_dy(DWTCompose *cs, IDWTELEM *buffer,
  635. IDWTELEM *temp, int width, int height,
  636. int stride)
  637. {
  638. int y = cs->y;
  639. IDWTELEM *b0 = cs->b0;
  640. IDWTELEM *b1 = cs->b1;
  641. IDWTELEM *b2 = cs->b2;
  642. IDWTELEM *b3 = cs->b3;
  643. IDWTELEM *b4 = buffer + mirror(y + 3, height - 1) * stride;
  644. IDWTELEM *b5 = buffer + mirror(y + 4, height - 1) * stride;
  645. if (y + 3 < (unsigned)height)
  646. vertical_compose97iL1(b3, b4, b5, width);
  647. if (y + 2 < (unsigned)height)
  648. vertical_compose97iH1(b2, b3, b4, width);
  649. if (y + 1 < (unsigned)height)
  650. vertical_compose97iL0(b1, b2, b3, width);
  651. if (y + 0 < (unsigned)height)
  652. vertical_compose97iH0(b0, b1, b2, width);
  653. if (y - 1 < (unsigned)height)
  654. ff_snow_horizontal_compose97i(b0, temp, width);
  655. if (y + 0 < (unsigned)height)
  656. ff_snow_horizontal_compose97i(b1, temp, width);
  657. cs->b0 = b2;
  658. cs->b1 = b3;
  659. cs->b2 = b4;
  660. cs->b3 = b5;
  661. cs->y += 2;
  662. }
  663. static void av_unused spatial_compose97i(IDWTELEM *buffer, IDWTELEM *temp,
  664. int width, int height, int stride)
  665. {
  666. DWTCompose cs;
  667. spatial_compose97i_init(&cs, buffer, height, stride);
  668. while (cs.y <= height)
  669. spatial_compose97i_dy(&cs, buffer, temp, width, height, stride);
  670. }
  671. void ff_spatial_idwt_buffered_init(DWTCompose *cs, slice_buffer *sb, int width,
  672. int height, int stride_line, int type,
  673. int decomposition_count)
  674. {
  675. int level;
  676. for (level = decomposition_count - 1; level >= 0; level--) {
  677. switch (type) {
  678. case DWT_97:
  679. spatial_compose97i_buffered_init(cs + level, sb, height >> level,
  680. stride_line << level);
  681. break;
  682. case DWT_53:
  683. spatial_compose53i_buffered_init(cs + level, sb, height >> level,
  684. stride_line << level);
  685. break;
  686. }
  687. }
  688. }
  689. void ff_spatial_idwt_buffered_slice(DWTContext *dsp, DWTCompose *cs,
  690. slice_buffer *slice_buf, IDWTELEM *temp,
  691. int width, int height, int stride_line,
  692. int type, int decomposition_count, int y)
  693. {
  694. const int support = type == 1 ? 3 : 5;
  695. int level;
  696. if (type == 2)
  697. return;
  698. for (level = decomposition_count - 1; level >= 0; level--)
  699. while (cs[level].y <= FFMIN((y >> level) + support, height >> level)) {
  700. switch (type) {
  701. case DWT_97:
  702. spatial_compose97i_dy_buffered(dsp, cs + level, slice_buf, temp,
  703. width >> level,
  704. height >> level,
  705. stride_line << level);
  706. break;
  707. case DWT_53:
  708. spatial_compose53i_dy_buffered(cs + level, slice_buf, temp,
  709. width >> level,
  710. height >> level,
  711. stride_line << level);
  712. break;
  713. }
  714. }
  715. }
  716. static void ff_spatial_idwt_init(DWTCompose *cs, IDWTELEM *buffer, int width,
  717. int height, int stride, int type,
  718. int decomposition_count)
  719. {
  720. int level;
  721. for (level = decomposition_count - 1; level >= 0; level--) {
  722. switch (type) {
  723. case DWT_97:
  724. spatial_compose97i_init(cs + level, buffer, height >> level,
  725. stride << level);
  726. break;
  727. case DWT_53:
  728. spatial_compose53i_init(cs + level, buffer, height >> level,
  729. stride << level);
  730. break;
  731. }
  732. }
  733. }
  734. static void ff_spatial_idwt_slice(DWTCompose *cs, IDWTELEM *buffer,
  735. IDWTELEM *temp, int width, int height,
  736. int stride, int type,
  737. int decomposition_count, int y)
  738. {
  739. const int support = type == 1 ? 3 : 5;
  740. int level;
  741. if (type == 2)
  742. return;
  743. for (level = decomposition_count - 1; level >= 0; level--)
  744. while (cs[level].y <= FFMIN((y >> level) + support, height >> level)) {
  745. switch (type) {
  746. case DWT_97:
  747. spatial_compose97i_dy(cs + level, buffer, temp, width >> level,
  748. height >> level, stride << level);
  749. break;
  750. case DWT_53:
  751. spatial_compose53i_dy(cs + level, buffer, temp, width >> level,
  752. height >> level, stride << level);
  753. break;
  754. }
  755. }
  756. }
  757. void ff_spatial_idwt(IDWTELEM *buffer, IDWTELEM *temp, int width, int height,
  758. int stride, int type, int decomposition_count)
  759. {
  760. DWTCompose cs[MAX_DECOMPOSITIONS];
  761. int y;
  762. ff_spatial_idwt_init(cs, buffer, width, height, stride, type,
  763. decomposition_count);
  764. for (y = 0; y < height; y += 4)
  765. ff_spatial_idwt_slice(cs, buffer, temp, width, height, stride, type,
  766. decomposition_count, y);
  767. }
  768. static inline int w_c(void *v, uint8_t *pix1, uint8_t *pix2, int line_size,
  769. int w, int h, int type)
  770. {
  771. int s, i, j;
  772. const int dec_count = w == 8 ? 3 : 4;
  773. int tmp[32 * 32], tmp2[32];
  774. int level, ori;
  775. static const int scale[2][2][4][4] = {
  776. {
  777. { // 9/7 8x8 dec=3
  778. { 268, 239, 239, 213 },
  779. { 0, 224, 224, 152 },
  780. { 0, 135, 135, 110 },
  781. },
  782. { // 9/7 16x16 or 32x32 dec=4
  783. { 344, 310, 310, 280 },
  784. { 0, 320, 320, 228 },
  785. { 0, 175, 175, 136 },
  786. { 0, 129, 129, 102 },
  787. }
  788. },
  789. {
  790. { // 5/3 8x8 dec=3
  791. { 275, 245, 245, 218 },
  792. { 0, 230, 230, 156 },
  793. { 0, 138, 138, 113 },
  794. },
  795. { // 5/3 16x16 or 32x32 dec=4
  796. { 352, 317, 317, 286 },
  797. { 0, 328, 328, 233 },
  798. { 0, 180, 180, 140 },
  799. { 0, 132, 132, 105 },
  800. }
  801. }
  802. };
  803. for (i = 0; i < h; i++) {
  804. for (j = 0; j < w; j += 4) {
  805. tmp[32 * i + j + 0] = (pix1[j + 0] - pix2[j + 0]) << 4;
  806. tmp[32 * i + j + 1] = (pix1[j + 1] - pix2[j + 1]) << 4;
  807. tmp[32 * i + j + 2] = (pix1[j + 2] - pix2[j + 2]) << 4;
  808. tmp[32 * i + j + 3] = (pix1[j + 3] - pix2[j + 3]) << 4;
  809. }
  810. pix1 += line_size;
  811. pix2 += line_size;
  812. }
  813. ff_spatial_dwt(tmp, tmp2, w, h, 32, type, dec_count);
  814. s = 0;
  815. assert(w == h);
  816. for (level = 0; level < dec_count; level++)
  817. for (ori = level ? 1 : 0; ori < 4; ori++) {
  818. int size = w >> (dec_count - level);
  819. int sx = (ori & 1) ? size : 0;
  820. int stride = 32 << (dec_count - level);
  821. int sy = (ori & 2) ? stride >> 1 : 0;
  822. for (i = 0; i < size; i++)
  823. for (j = 0; j < size; j++) {
  824. int v = tmp[sx + sy + i * stride + j] *
  825. scale[type][dec_count - 3][level][ori];
  826. s += FFABS(v);
  827. }
  828. }
  829. assert(s >= 0);
  830. return s >> 9;
  831. }
  832. static int w53_8_c(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h)
  833. {
  834. return w_c(v, pix1, pix2, line_size, 8, h, 1);
  835. }
  836. static int w97_8_c(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h)
  837. {
  838. return w_c(v, pix1, pix2, line_size, 8, h, 0);
  839. }
  840. static int w53_16_c(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h)
  841. {
  842. return w_c(v, pix1, pix2, line_size, 16, h, 1);
  843. }
  844. static int w97_16_c(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h)
  845. {
  846. return w_c(v, pix1, pix2, line_size, 16, h, 0);
  847. }
  848. int ff_w53_32_c(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h)
  849. {
  850. return w_c(v, pix1, pix2, line_size, 32, h, 1);
  851. }
  852. int ff_w97_32_c(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h)
  853. {
  854. return w_c(v, pix1, pix2, line_size, 32, h, 0);
  855. }
  856. void ff_dsputil_init_dwt(DSPContext *c)
  857. {
  858. c->w53[0] = w53_16_c;
  859. c->w53[1] = w53_8_c;
  860. c->w97[0] = w97_16_c;
  861. c->w97[1] = w97_8_c;
  862. }
  863. void ff_dwt_init(DWTContext *c)
  864. {
  865. c->vertical_compose97i = ff_snow_vertical_compose97i;
  866. c->horizontal_compose97i = ff_snow_horizontal_compose97i;
  867. c->inner_add_yblock = ff_snow_inner_add_yblock;
  868. if (HAVE_MMX)
  869. ff_dwt_init_x86(c);
  870. }