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.

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