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.

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