vf_fspp.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. /*
  2. * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
  3. * Copyright (C) 2005 Nikolaj Poroshin <porosh3@psu.ru>
  4. * Copyright (c) 2014 Arwa Arif <arwaarif1994@gmail.com>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. */
  22. /**
  23. * @file
  24. * Fast Simple Post-processing filter
  25. * This implementation is based on an algorithm described in
  26. * "Aria Nosratinia Embedded Post-Processing for
  27. * Enhancement of Compressed Images (1999)"
  28. * (http://www.utdallas.edu/~aria/papers/vlsisp99.pdf)
  29. * Further, with splitting (I)DCT into horizontal/vertical passes, one of
  30. * them can be performed once per block, not per pixel. This allows for much
  31. * higher speed.
  32. *
  33. * Originally written by Michael Niedermayer and Nikolaj for the MPlayer
  34. * project, and ported by Arwa Arif for FFmpeg.
  35. */
  36. #include "libavutil/avassert.h"
  37. #include "libavutil/imgutils.h"
  38. #include "libavutil/opt.h"
  39. #include "libavutil/pixdesc.h"
  40. #include "internal.h"
  41. #include "vf_fspp.h"
  42. #define OFFSET(x) offsetof(FSPPContext, x)
  43. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  44. static const AVOption fspp_options[] = {
  45. { "quality", "set quality", OFFSET(log2_count), AV_OPT_TYPE_INT, {.i64 = 4}, 4, MAX_LEVEL, FLAGS },
  46. { "qp", "force a constant quantizer parameter", OFFSET(qp), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 64, FLAGS },
  47. { "strength", "set filter strength", OFFSET(strength), AV_OPT_TYPE_INT, {.i64 = 0}, -15, 32, FLAGS },
  48. { "use_bframe_qp", "use B-frames' QP", OFFSET(use_bframe_qp), AV_OPT_TYPE_BOOL,{.i64 = 0}, 0, 1, FLAGS },
  49. { NULL }
  50. };
  51. AVFILTER_DEFINE_CLASS(fspp);
  52. DECLARE_ALIGNED(32, static const uint8_t, dither)[8][8] = {
  53. { 0, 48, 12, 60, 3, 51, 15, 63, },
  54. { 32, 16, 44, 28, 35, 19, 47, 31, },
  55. { 8, 56, 4, 52, 11, 59, 7, 55, },
  56. { 40, 24, 36, 20, 43, 27, 39, 23, },
  57. { 2, 50, 14, 62, 1, 49, 13, 61, },
  58. { 34, 18, 46, 30, 33, 17, 45, 29, },
  59. { 10, 58, 6, 54, 9, 57, 5, 53, },
  60. { 42, 26, 38, 22, 41, 25, 37, 21, },
  61. };
  62. static const short custom_threshold[64] = {
  63. // values (296) can't be too high
  64. // -it causes too big quant dependence
  65. // or maybe overflow(check), which results in some flashing
  66. 71, 296, 295, 237, 71, 40, 38, 19,
  67. 245, 193, 185, 121, 102, 73, 53, 27,
  68. 158, 129, 141, 107, 97, 73, 50, 26,
  69. 102, 116, 109, 98, 82, 66, 45, 23,
  70. 71, 94, 95, 81, 70, 56, 38, 20,
  71. 56, 77, 74, 66, 56, 44, 30, 15,
  72. 38, 53, 50, 45, 38, 30, 21, 11,
  73. 20, 27, 26, 23, 20, 15, 11, 5
  74. };
  75. //This func reads from 1 slice, 1 and clears 0 & 1
  76. static void store_slice_c(uint8_t *dst, int16_t *src,
  77. ptrdiff_t dst_stride, ptrdiff_t src_stride,
  78. ptrdiff_t width, ptrdiff_t height, ptrdiff_t log2_scale)
  79. {
  80. int y, x;
  81. #define STORE(pos) \
  82. temp = (src[x + pos] + (d[pos] >> log2_scale)) >> (6 - log2_scale); \
  83. src[x + pos] = src[x + pos - 8 * src_stride] = 0; \
  84. if (temp & 0x100) temp = ~(temp >> 31); \
  85. dst[x + pos] = temp;
  86. for (y = 0; y < height; y++) {
  87. const uint8_t *d = dither[y];
  88. for (x = 0; x < width; x += 8) {
  89. int temp;
  90. STORE(0);
  91. STORE(1);
  92. STORE(2);
  93. STORE(3);
  94. STORE(4);
  95. STORE(5);
  96. STORE(6);
  97. STORE(7);
  98. }
  99. src += src_stride;
  100. dst += dst_stride;
  101. }
  102. }
  103. //This func reads from 2 slices, 0 & 2 and clears 2-nd
  104. static void store_slice2_c(uint8_t *dst, int16_t *src,
  105. ptrdiff_t dst_stride, ptrdiff_t src_stride,
  106. ptrdiff_t width, ptrdiff_t height, ptrdiff_t log2_scale)
  107. {
  108. int y, x;
  109. #define STORE2(pos) \
  110. temp = (src[x + pos] + src[x + pos + 16 * src_stride] + (d[pos] >> log2_scale)) >> (6 - log2_scale); \
  111. src[x + pos + 16 * src_stride] = 0; \
  112. if (temp & 0x100) temp = ~(temp >> 31); \
  113. dst[x + pos] = temp;
  114. for (y = 0; y < height; y++) {
  115. const uint8_t *d = dither[y];
  116. for (x = 0; x < width; x += 8) {
  117. int temp;
  118. STORE2(0);
  119. STORE2(1);
  120. STORE2(2);
  121. STORE2(3);
  122. STORE2(4);
  123. STORE2(5);
  124. STORE2(6);
  125. STORE2(7);
  126. }
  127. src += src_stride;
  128. dst += dst_stride;
  129. }
  130. }
  131. static void mul_thrmat_c(int16_t *thr_adr_noq, int16_t *thr_adr, int q)
  132. {
  133. int a;
  134. for (a = 0; a < 64; a++)
  135. thr_adr[a] = q * thr_adr_noq[a];
  136. }
  137. static void filter(FSPPContext *p, uint8_t *dst, uint8_t *src,
  138. int dst_stride, int src_stride,
  139. int width, int height,
  140. uint8_t *qp_store, int qp_stride, int is_luma)
  141. {
  142. int x, x0, y, es, qy, t;
  143. const int stride = is_luma ? p->temp_stride : (width + 16);
  144. const int step = 6 - p->log2_count;
  145. const int qpsh = 4 - p->hsub * !is_luma;
  146. const int qpsv = 4 - p->vsub * !is_luma;
  147. DECLARE_ALIGNED(32, int32_t, block_align)[4 * 8 * BLOCKSZ + 4 * 8 * BLOCKSZ];
  148. int16_t *block = (int16_t *)block_align;
  149. int16_t *block3 = (int16_t *)(block_align + 4 * 8 * BLOCKSZ);
  150. memset(block3, 0, 4 * 8 * BLOCKSZ);
  151. if (!src || !dst) return;
  152. for (y = 0; y < height; y++) {
  153. int index = 8 + 8 * stride + y * stride;
  154. memcpy(p->src + index, src + y * src_stride, width);
  155. for (x = 0; x < 8; x++) {
  156. p->src[index - x - 1] = p->src[index + x ];
  157. p->src[index + width + x ] = p->src[index + width - x - 1];
  158. }
  159. }
  160. for (y = 0; y < 8; y++) {
  161. memcpy(p->src + ( 7 - y ) * stride, p->src + ( y + 8 ) * stride, stride);
  162. memcpy(p->src + (height + 8 + y) * stride, p->src + (height - y + 7) * stride, stride);
  163. }
  164. //FIXME (try edge emu)
  165. for (y = 8; y < 24; y++)
  166. memset(p->temp + 8 + y * stride, 0, width * sizeof(int16_t));
  167. for (y = step; y < height + 8; y += step) { //step= 1,2
  168. const int y1 = y - 8 + step; //l5-7 l4-6;
  169. qy = y - 4;
  170. if (qy > height - 1) qy = height - 1;
  171. if (qy < 0) qy = 0;
  172. qy = (qy >> qpsv) * qp_stride;
  173. p->row_fdct(block, p->src + y * stride + 2 - (y&1), stride, 2);
  174. for (x0 = 0; x0 < width + 8 - 8 * (BLOCKSZ - 1); x0 += 8 * (BLOCKSZ - 1)) {
  175. p->row_fdct(block + 8 * 8, p->src + y * stride + 8 + x0 + 2 - (y&1), stride, 2 * (BLOCKSZ - 1));
  176. if (p->qp)
  177. p->column_fidct((int16_t *)(&p->threshold_mtx[0]), block + 0 * 8, block3 + 0 * 8, 8 * (BLOCKSZ - 1)); //yes, this is a HOTSPOT
  178. else
  179. for (x = 0; x < 8 * (BLOCKSZ - 1); x += 8) {
  180. t = x + x0 - 2; //correct t=x+x0-2-(y&1), but its the same
  181. if (t < 0) t = 0; //t always < width-2
  182. t = qp_store[qy + (t >> qpsh)];
  183. t = ff_norm_qscale(t, p->qscale_type);
  184. if (t != p->prev_q) p->prev_q = t, p->mul_thrmat((int16_t *)(&p->threshold_mtx_noq[0]), (int16_t *)(&p->threshold_mtx[0]), t);
  185. p->column_fidct((int16_t *)(&p->threshold_mtx[0]), block + x * 8, block3 + x * 8, 8); //yes, this is a HOTSPOT
  186. }
  187. p->row_idct(block3 + 0 * 8, p->temp + (y & 15) * stride + x0 + 2 - (y & 1), stride, 2 * (BLOCKSZ - 1));
  188. memmove(block, block + (BLOCKSZ - 1) * 64, 8 * 8 * sizeof(int16_t)); //cycling
  189. memmove(block3, block3 + (BLOCKSZ - 1) * 64, 6 * 8 * sizeof(int16_t));
  190. }
  191. es = width + 8 - x0; // 8, ...
  192. if (es > 8)
  193. p->row_fdct(block + 8 * 8, p->src + y * stride + 8 + x0 + 2 - (y & 1), stride, (es - 4) >> 2);
  194. p->column_fidct((int16_t *)(&p->threshold_mtx[0]), block, block3, es&(~1));
  195. if (es > 3)
  196. p->row_idct(block3 + 0 * 8, p->temp + (y & 15) * stride + x0 + 2 - (y & 1), stride, es >> 2);
  197. if (!(y1 & 7) && y1) {
  198. if (y1 & 8)
  199. p->store_slice(dst + (y1 - 8) * dst_stride, p->temp + 8 + 8 * stride,
  200. dst_stride, stride, width, 8, 5 - p->log2_count);
  201. else
  202. p->store_slice2(dst + (y1 - 8) * dst_stride, p->temp + 8 + 0 * stride,
  203. dst_stride, stride, width, 8, 5 - p->log2_count);
  204. }
  205. }
  206. if (y & 7) { // height % 8 != 0
  207. if (y & 8)
  208. p->store_slice(dst + ((y - 8) & ~7) * dst_stride, p->temp + 8 + 8 * stride,
  209. dst_stride, stride, width, y&7, 5 - p->log2_count);
  210. else
  211. p->store_slice2(dst + ((y - 8) & ~7) * dst_stride, p->temp + 8 + 0 * stride,
  212. dst_stride, stride, width, y&7, 5 - p->log2_count);
  213. }
  214. }
  215. static void column_fidct_c(int16_t *thr_adr, int16_t *data, int16_t *output, int cnt)
  216. {
  217. int_simd16_t tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
  218. int_simd16_t tmp10, tmp11, tmp12, tmp13;
  219. int_simd16_t z1,z2,z3,z4,z5, z10, z11, z12, z13;
  220. int_simd16_t d0, d1, d2, d3, d4, d5, d6, d7;
  221. int16_t *dataptr;
  222. int16_t *wsptr;
  223. int16_t *threshold;
  224. int ctr;
  225. dataptr = data;
  226. wsptr = output;
  227. for (; cnt > 0; cnt -= 2) { //start positions
  228. threshold = (int16_t *)thr_adr;//threshold_mtx
  229. for (ctr = DCTSIZE; ctr > 0; ctr--) {
  230. // Process columns from input, add to output.
  231. tmp0 = dataptr[DCTSIZE * 0] + dataptr[DCTSIZE * 7];
  232. tmp7 = dataptr[DCTSIZE * 0] - dataptr[DCTSIZE * 7];
  233. tmp1 = dataptr[DCTSIZE * 1] + dataptr[DCTSIZE * 6];
  234. tmp6 = dataptr[DCTSIZE * 1] - dataptr[DCTSIZE * 6];
  235. tmp2 = dataptr[DCTSIZE * 2] + dataptr[DCTSIZE * 5];
  236. tmp5 = dataptr[DCTSIZE * 2] - dataptr[DCTSIZE * 5];
  237. tmp3 = dataptr[DCTSIZE * 3] + dataptr[DCTSIZE * 4];
  238. tmp4 = dataptr[DCTSIZE * 3] - dataptr[DCTSIZE * 4];
  239. // Even part of FDCT
  240. tmp10 = tmp0 + tmp3;
  241. tmp13 = tmp0 - tmp3;
  242. tmp11 = tmp1 + tmp2;
  243. tmp12 = tmp1 - tmp2;
  244. d0 = tmp10 + tmp11;
  245. d4 = tmp10 - tmp11;
  246. z1 = MULTIPLY16H((tmp12 + tmp13) << 2, FIX_0_707106781);
  247. d2 = tmp13 + z1;
  248. d6 = tmp13 - z1;
  249. // Even part of IDCT
  250. THRESHOLD(tmp0, d0, threshold[0 * 8]);
  251. THRESHOLD(tmp1, d2, threshold[2 * 8]);
  252. THRESHOLD(tmp2, d4, threshold[4 * 8]);
  253. THRESHOLD(tmp3, d6, threshold[6 * 8]);
  254. tmp0 += 2;
  255. tmp10 = (tmp0 + tmp2) >> 2;
  256. tmp11 = (tmp0 - tmp2) >> 2;
  257. tmp13 = (tmp1 + tmp3) >>2; //+2 ! (psnr decides)
  258. tmp12 = MULTIPLY16H((tmp1 - tmp3), FIX_1_414213562_A) - tmp13; //<<2
  259. tmp0 = tmp10 + tmp13; //->temps
  260. tmp3 = tmp10 - tmp13; //->temps
  261. tmp1 = tmp11 + tmp12; //->temps
  262. tmp2 = tmp11 - tmp12; //->temps
  263. // Odd part of FDCT
  264. tmp10 = tmp4 + tmp5;
  265. tmp11 = tmp5 + tmp6;
  266. tmp12 = tmp6 + tmp7;
  267. z5 = MULTIPLY16H((tmp10 - tmp12) << 2, FIX_0_382683433);
  268. z2 = MULTIPLY16H(tmp10 << 2, FIX_0_541196100) + z5;
  269. z4 = MULTIPLY16H(tmp12 << 2, FIX_1_306562965) + z5;
  270. z3 = MULTIPLY16H(tmp11 << 2, FIX_0_707106781);
  271. z11 = tmp7 + z3;
  272. z13 = tmp7 - z3;
  273. d5 = z13 + z2;
  274. d3 = z13 - z2;
  275. d1 = z11 + z4;
  276. d7 = z11 - z4;
  277. // Odd part of IDCT
  278. THRESHOLD(tmp4, d1, threshold[1 * 8]);
  279. THRESHOLD(tmp5, d3, threshold[3 * 8]);
  280. THRESHOLD(tmp6, d5, threshold[5 * 8]);
  281. THRESHOLD(tmp7, d7, threshold[7 * 8]);
  282. //Simd version uses here a shortcut for the tmp5,tmp6,tmp7 == 0
  283. z13 = tmp6 + tmp5;
  284. z10 = (tmp6 - tmp5) << 1;
  285. z11 = tmp4 + tmp7;
  286. z12 = (tmp4 - tmp7) << 1;
  287. tmp7 = (z11 + z13) >> 2; //+2 !
  288. tmp11 = MULTIPLY16H((z11 - z13) << 1, FIX_1_414213562);
  289. z5 = MULTIPLY16H(z10 + z12, FIX_1_847759065);
  290. tmp10 = MULTIPLY16H(z12, FIX_1_082392200) - z5;
  291. tmp12 = MULTIPLY16H(z10, FIX_2_613125930) + z5; // - !!
  292. tmp6 = tmp12 - tmp7;
  293. tmp5 = tmp11 - tmp6;
  294. tmp4 = tmp10 + tmp5;
  295. wsptr[DCTSIZE * 0] += (tmp0 + tmp7);
  296. wsptr[DCTSIZE * 1] += (tmp1 + tmp6);
  297. wsptr[DCTSIZE * 2] += (tmp2 + tmp5);
  298. wsptr[DCTSIZE * 3] += (tmp3 - tmp4);
  299. wsptr[DCTSIZE * 4] += (tmp3 + tmp4);
  300. wsptr[DCTSIZE * 5] += (tmp2 - tmp5);
  301. wsptr[DCTSIZE * 6] = (tmp1 - tmp6);
  302. wsptr[DCTSIZE * 7] = (tmp0 - tmp7);
  303. //
  304. dataptr++; //next column
  305. wsptr++;
  306. threshold++;
  307. }
  308. dataptr += 8; //skip each second start pos
  309. wsptr += 8;
  310. }
  311. }
  312. static void row_idct_c(int16_t *workspace, int16_t *output_adr, ptrdiff_t output_stride, int cnt)
  313. {
  314. int_simd16_t tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
  315. int_simd16_t tmp10, tmp11, tmp12, tmp13;
  316. int_simd16_t z5, z10, z11, z12, z13;
  317. int16_t *outptr;
  318. int16_t *wsptr;
  319. cnt *= 4;
  320. wsptr = workspace;
  321. outptr = output_adr;
  322. for (; cnt > 0; cnt--) {
  323. // Even part
  324. //Simd version reads 4x4 block and transposes it
  325. tmp10 = wsptr[2] + wsptr[3];
  326. tmp11 = wsptr[2] - wsptr[3];
  327. tmp13 = wsptr[0] + wsptr[1];
  328. tmp12 = (MULTIPLY16H(wsptr[0] - wsptr[1], FIX_1_414213562_A) << 2) - tmp13;//this shift order to avoid overflow
  329. tmp0 = tmp10 + tmp13; //->temps
  330. tmp3 = tmp10 - tmp13; //->temps
  331. tmp1 = tmp11 + tmp12;
  332. tmp2 = tmp11 - tmp12;
  333. // Odd part
  334. //Also transpose, with previous:
  335. // ---- ---- ||||
  336. // ---- ---- idct ||||
  337. // ---- ---- ---> ||||
  338. // ---- ---- ||||
  339. z13 = wsptr[4] + wsptr[5];
  340. z10 = wsptr[4] - wsptr[5];
  341. z11 = wsptr[6] + wsptr[7];
  342. z12 = wsptr[6] - wsptr[7];
  343. tmp7 = z11 + z13;
  344. tmp11 = MULTIPLY16H(z11 - z13, FIX_1_414213562);
  345. z5 = MULTIPLY16H(z10 + z12, FIX_1_847759065);
  346. tmp10 = MULTIPLY16H(z12, FIX_1_082392200) - z5;
  347. tmp12 = MULTIPLY16H(z10, FIX_2_613125930) + z5; // - FIX_
  348. tmp6 = (tmp12 << 3) - tmp7;
  349. tmp5 = (tmp11 << 3) - tmp6;
  350. tmp4 = (tmp10 << 3) + tmp5;
  351. // Final output stage: descale and write column
  352. outptr[0 * output_stride] += DESCALE(tmp0 + tmp7, 3);
  353. outptr[1 * output_stride] += DESCALE(tmp1 + tmp6, 3);
  354. outptr[2 * output_stride] += DESCALE(tmp2 + tmp5, 3);
  355. outptr[3 * output_stride] += DESCALE(tmp3 - tmp4, 3);
  356. outptr[4 * output_stride] += DESCALE(tmp3 + tmp4, 3);
  357. outptr[5 * output_stride] += DESCALE(tmp2 - tmp5, 3);
  358. outptr[6 * output_stride] += DESCALE(tmp1 - tmp6, 3); //no += ?
  359. outptr[7 * output_stride] += DESCALE(tmp0 - tmp7, 3); //no += ?
  360. outptr++;
  361. wsptr += DCTSIZE; // advance pointer to next row
  362. }
  363. }
  364. static void row_fdct_c(int16_t *data, const uint8_t *pixels, ptrdiff_t line_size, int cnt)
  365. {
  366. int_simd16_t tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
  367. int_simd16_t tmp10, tmp11, tmp12, tmp13;
  368. int_simd16_t z1, z2, z3, z4, z5, z11, z13;
  369. int16_t *dataptr;
  370. cnt *= 4;
  371. // Pass 1: process rows.
  372. dataptr = data;
  373. for (; cnt > 0; cnt--) {
  374. tmp0 = pixels[line_size * 0] + pixels[line_size * 7];
  375. tmp7 = pixels[line_size * 0] - pixels[line_size * 7];
  376. tmp1 = pixels[line_size * 1] + pixels[line_size * 6];
  377. tmp6 = pixels[line_size * 1] - pixels[line_size * 6];
  378. tmp2 = pixels[line_size * 2] + pixels[line_size * 5];
  379. tmp5 = pixels[line_size * 2] - pixels[line_size * 5];
  380. tmp3 = pixels[line_size * 3] + pixels[line_size * 4];
  381. tmp4 = pixels[line_size * 3] - pixels[line_size * 4];
  382. // Even part
  383. tmp10 = tmp0 + tmp3;
  384. tmp13 = tmp0 - tmp3;
  385. tmp11 = tmp1 + tmp2;
  386. tmp12 = tmp1 - tmp2;
  387. //Even columns are written first, this leads to different order of columns
  388. //in column_fidct(), but they are processed independently, so all ok.
  389. //Later in the row_idct() columns readed at the same order.
  390. dataptr[2] = tmp10 + tmp11;
  391. dataptr[3] = tmp10 - tmp11;
  392. z1 = MULTIPLY16H((tmp12 + tmp13) << 2, FIX_0_707106781);
  393. dataptr[0] = tmp13 + z1;
  394. dataptr[1] = tmp13 - z1;
  395. // Odd part
  396. tmp10 = (tmp4 + tmp5) << 2;
  397. tmp11 = (tmp5 + tmp6) << 2;
  398. tmp12 = (tmp6 + tmp7) << 2;
  399. z5 = MULTIPLY16H(tmp10 - tmp12, FIX_0_382683433);
  400. z2 = MULTIPLY16H(tmp10, FIX_0_541196100) + z5;
  401. z4 = MULTIPLY16H(tmp12, FIX_1_306562965) + z5;
  402. z3 = MULTIPLY16H(tmp11, FIX_0_707106781);
  403. z11 = tmp7 + z3;
  404. z13 = tmp7 - z3;
  405. dataptr[4] = z13 + z2;
  406. dataptr[5] = z13 - z2;
  407. dataptr[6] = z11 + z4;
  408. dataptr[7] = z11 - z4;
  409. pixels++; // advance pointer to next column
  410. dataptr += DCTSIZE;
  411. }
  412. }
  413. static int query_formats(AVFilterContext *ctx)
  414. {
  415. static const enum AVPixelFormat pix_fmts[] = {
  416. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P,
  417. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV411P,
  418. AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV440P,
  419. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ422P,
  420. AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ440P,
  421. AV_PIX_FMT_GBRP, AV_PIX_FMT_GRAY8,
  422. AV_PIX_FMT_NONE
  423. };
  424. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  425. if (!fmts_list)
  426. return AVERROR(ENOMEM);
  427. return ff_set_common_formats(ctx, fmts_list);
  428. }
  429. static int config_input(AVFilterLink *inlink)
  430. {
  431. AVFilterContext *ctx = inlink->dst;
  432. FSPPContext *fspp = ctx->priv;
  433. const int h = FFALIGN(inlink->h + 16, 16);
  434. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  435. fspp->hsub = desc->log2_chroma_w;
  436. fspp->vsub = desc->log2_chroma_h;
  437. fspp->temp_stride = FFALIGN(inlink->w + 16, 16);
  438. fspp->temp = av_malloc_array(fspp->temp_stride, h * sizeof(*fspp->temp));
  439. fspp->src = av_malloc_array(fspp->temp_stride, h * sizeof(*fspp->src));
  440. if (!fspp->temp || !fspp->src)
  441. return AVERROR(ENOMEM);
  442. if (!fspp->use_bframe_qp && !fspp->qp) {
  443. fspp->non_b_qp_alloc_size = AV_CEIL_RSHIFT(inlink->w, 4) * AV_CEIL_RSHIFT(inlink->h, 4);
  444. fspp->non_b_qp_table = av_calloc(fspp->non_b_qp_alloc_size, sizeof(*fspp->non_b_qp_table));
  445. if (!fspp->non_b_qp_table)
  446. return AVERROR(ENOMEM);
  447. }
  448. fspp->store_slice = store_slice_c;
  449. fspp->store_slice2 = store_slice2_c;
  450. fspp->mul_thrmat = mul_thrmat_c;
  451. fspp->column_fidct = column_fidct_c;
  452. fspp->row_idct = row_idct_c;
  453. fspp->row_fdct = row_fdct_c;
  454. if (ARCH_X86)
  455. ff_fspp_init_x86(fspp);
  456. return 0;
  457. }
  458. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  459. {
  460. AVFilterContext *ctx = inlink->dst;
  461. FSPPContext *fspp = ctx->priv;
  462. AVFilterLink *outlink = ctx->outputs[0];
  463. AVFrame *out = in;
  464. int qp_stride = 0;
  465. uint8_t *qp_table = NULL;
  466. int i, bias;
  467. int custom_threshold_m[64];
  468. bias = (1 << 4) + fspp->strength;
  469. for (i = 0; i < 64; i++) //FIXME: tune custom_threshold[] and remove this !
  470. custom_threshold_m[i] = (int)(custom_threshold[i] * (bias / 71.0) + 0.5);
  471. for (i = 0; i < 8; i++) {
  472. fspp->threshold_mtx_noq[2 * i] = (uint64_t)custom_threshold_m[i * 8 + 2]
  473. |(((uint64_t)custom_threshold_m[i * 8 + 6]) << 16)
  474. |(((uint64_t)custom_threshold_m[i * 8 + 0]) << 32)
  475. |(((uint64_t)custom_threshold_m[i * 8 + 4]) << 48);
  476. fspp->threshold_mtx_noq[2 * i + 1] = (uint64_t)custom_threshold_m[i * 8 + 5]
  477. |(((uint64_t)custom_threshold_m[i * 8 + 3]) << 16)
  478. |(((uint64_t)custom_threshold_m[i * 8 + 1]) << 32)
  479. |(((uint64_t)custom_threshold_m[i * 8 + 7]) << 48);
  480. }
  481. if (fspp->qp)
  482. fspp->prev_q = fspp->qp, fspp->mul_thrmat((int16_t *)(&fspp->threshold_mtx_noq[0]), (int16_t *)(&fspp->threshold_mtx[0]), fspp->qp);
  483. /* if we are not in a constant user quantizer mode and we don't want to use
  484. * the quantizers from the B-frames (B-frames often have a higher QP), we
  485. * need to save the qp table from the last non B-frame; this is what the
  486. * following code block does */
  487. if (!fspp->qp) {
  488. qp_table = av_frame_get_qp_table(in, &qp_stride, &fspp->qscale_type);
  489. if (qp_table && !fspp->use_bframe_qp && in->pict_type != AV_PICTURE_TYPE_B) {
  490. int w, h;
  491. /* if the qp stride is not set, it means the QP are only defined on
  492. * a line basis */
  493. if (!qp_stride) {
  494. w = AV_CEIL_RSHIFT(inlink->w, 4);
  495. h = 1;
  496. } else {
  497. w = qp_stride;
  498. h = AV_CEIL_RSHIFT(inlink->h, 4);
  499. }
  500. if (w * h > fspp->non_b_qp_alloc_size) {
  501. int ret = av_reallocp_array(&fspp->non_b_qp_table, w, h);
  502. if (ret < 0) {
  503. fspp->non_b_qp_alloc_size = 0;
  504. return ret;
  505. }
  506. fspp->non_b_qp_alloc_size = w * h;
  507. }
  508. av_assert0(w * h <= fspp->non_b_qp_alloc_size);
  509. memcpy(fspp->non_b_qp_table, qp_table, w * h);
  510. }
  511. }
  512. if (fspp->log2_count && !ctx->is_disabled) {
  513. if (!fspp->use_bframe_qp && fspp->non_b_qp_table)
  514. qp_table = fspp->non_b_qp_table;
  515. if (qp_table || fspp->qp) {
  516. const int cw = AV_CEIL_RSHIFT(inlink->w, fspp->hsub);
  517. const int ch = AV_CEIL_RSHIFT(inlink->h, fspp->vsub);
  518. /* get a new frame if in-place is not possible or if the dimensions
  519. * are not multiple of 8 */
  520. if (!av_frame_is_writable(in) || (inlink->w & 7) || (inlink->h & 7)) {
  521. const int aligned_w = FFALIGN(inlink->w, 8);
  522. const int aligned_h = FFALIGN(inlink->h, 8);
  523. out = ff_get_video_buffer(outlink, aligned_w, aligned_h);
  524. if (!out) {
  525. av_frame_free(&in);
  526. return AVERROR(ENOMEM);
  527. }
  528. av_frame_copy_props(out, in);
  529. out->width = in->width;
  530. out->height = in->height;
  531. }
  532. filter(fspp, out->data[0], in->data[0], out->linesize[0], in->linesize[0],
  533. inlink->w, inlink->h, qp_table, qp_stride, 1);
  534. filter(fspp, out->data[1], in->data[1], out->linesize[1], in->linesize[1],
  535. cw, ch, qp_table, qp_stride, 0);
  536. filter(fspp, out->data[2], in->data[2], out->linesize[2], in->linesize[2],
  537. cw, ch, qp_table, qp_stride, 0);
  538. emms_c();
  539. }
  540. }
  541. if (in != out) {
  542. if (in->data[3])
  543. av_image_copy_plane(out->data[3], out->linesize[3],
  544. in ->data[3], in ->linesize[3],
  545. inlink->w, inlink->h);
  546. av_frame_free(&in);
  547. }
  548. return ff_filter_frame(outlink, out);
  549. }
  550. static av_cold void uninit(AVFilterContext *ctx)
  551. {
  552. FSPPContext *fspp = ctx->priv;
  553. av_freep(&fspp->temp);
  554. av_freep(&fspp->src);
  555. av_freep(&fspp->non_b_qp_table);
  556. }
  557. static const AVFilterPad fspp_inputs[] = {
  558. {
  559. .name = "default",
  560. .type = AVMEDIA_TYPE_VIDEO,
  561. .config_props = config_input,
  562. .filter_frame = filter_frame,
  563. },
  564. { NULL }
  565. };
  566. static const AVFilterPad fspp_outputs[] = {
  567. {
  568. .name = "default",
  569. .type = AVMEDIA_TYPE_VIDEO,
  570. },
  571. { NULL }
  572. };
  573. AVFilter ff_vf_fspp = {
  574. .name = "fspp",
  575. .description = NULL_IF_CONFIG_SMALL("Apply Fast Simple Post-processing filter."),
  576. .priv_size = sizeof(FSPPContext),
  577. .uninit = uninit,
  578. .query_formats = query_formats,
  579. .inputs = fspp_inputs,
  580. .outputs = fspp_outputs,
  581. .priv_class = &fspp_class,
  582. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
  583. };