snow_dwt.c 28 KB

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