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.

682 lines
25KB

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