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.

420 lines
13KB

  1. /*
  2. * Copyright (c) 2005 Michael Niedermayer <michaelni@gmx.at>
  3. * Copyright (c) 2014 Arwa Arif <arwaarif1994@gmail.com>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (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
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. */
  21. /**
  22. * @file
  23. * Postprocessing filter - 7
  24. *
  25. * Originally written by Michael Niedermayer for the MPlayer
  26. * project, and ported by Arwa Arif for FFmpeg.
  27. */
  28. #include "libavutil/avassert.h"
  29. #include "libavutil/imgutils.h"
  30. #include "libavutil/opt.h"
  31. #include "libavutil/pixdesc.h"
  32. #include "internal.h"
  33. #include "vf_pp7.h"
  34. #include "libavcodec/avcodec.h"
  35. enum mode {
  36. MODE_HARD,
  37. MODE_SOFT,
  38. MODE_MEDIUM
  39. };
  40. #define OFFSET(x) offsetof(PP7Context, x)
  41. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  42. static const AVOption pp7_options[] = {
  43. { "qp", "force a constant quantizer parameter", OFFSET(qp), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 64, FLAGS },
  44. { "mode", "set thresholding mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64 = MODE_MEDIUM}, 0, 2, FLAGS, "mode" },
  45. { "hard", "hard thresholding", 0, AV_OPT_TYPE_CONST, {.i64 = MODE_HARD}, INT_MIN, INT_MAX, FLAGS, "mode" },
  46. { "soft", "soft thresholding", 0, AV_OPT_TYPE_CONST, {.i64 = MODE_SOFT}, INT_MIN, INT_MAX, FLAGS, "mode" },
  47. { "medium", "medium thresholding", 0, AV_OPT_TYPE_CONST, {.i64 = MODE_MEDIUM}, INT_MIN, INT_MAX, FLAGS, "mode" },
  48. { NULL }
  49. };
  50. AVFILTER_DEFINE_CLASS(pp7);
  51. DECLARE_ALIGNED(8, static const uint8_t, dither)[8][8] = {
  52. { 0, 48, 12, 60, 3, 51, 15, 63, },
  53. { 32, 16, 44, 28, 35, 19, 47, 31, },
  54. { 8, 56, 4, 52, 11, 59, 7, 55, },
  55. { 40, 24, 36, 20, 43, 27, 39, 23, },
  56. { 2, 50, 14, 62, 1, 49, 13, 61, },
  57. { 34, 18, 46, 30, 33, 17, 45, 29, },
  58. { 10, 58, 6, 54, 9, 57, 5, 53, },
  59. { 42, 26, 38, 22, 41, 25, 37, 21, },
  60. };
  61. #define N0 4
  62. #define N1 5
  63. #define N2 10
  64. #define SN0 2
  65. #define SN1 2.2360679775
  66. #define SN2 3.16227766017
  67. #define N (1 << 16)
  68. static const int factor[16] = {
  69. N / (N0 * N0), N / (N0 * N1), N / (N0 * N0), N / (N0 * N2),
  70. N / (N1 * N0), N / (N1 * N1), N / (N1 * N0), N / (N1 * N2),
  71. N / (N0 * N0), N / (N0 * N1), N / (N0 * N0), N / (N0 * N2),
  72. N / (N2 * N0), N / (N2 * N1), N / (N2 * N0), N / (N2 * N2),
  73. };
  74. static const int thres[16] = {
  75. N / (SN0 * SN0), N / (SN0 * SN2), N / (SN0 * SN0), N / (SN0 * SN2),
  76. N / (SN2 * SN0), N / (SN2 * SN2), N / (SN2 * SN0), N / (SN2 * SN2),
  77. N / (SN0 * SN0), N / (SN0 * SN2), N / (SN0 * SN0), N / (SN0 * SN2),
  78. N / (SN2 * SN0), N / (SN2 * SN2), N / (SN2 * SN0), N / (SN2 * SN2),
  79. };
  80. static inline int norm_qscale(int qscale, int type)
  81. {
  82. switch (type) {
  83. case FF_QSCALE_TYPE_MPEG1: return qscale;
  84. case FF_QSCALE_TYPE_MPEG2: return qscale >> 1;
  85. case FF_QSCALE_TYPE_H264: return qscale >> 2;
  86. case FF_QSCALE_TYPE_VP56: return (63 - qscale + 2) >> 2;
  87. }
  88. return qscale;
  89. }
  90. static void init_thres2(PP7Context *p)
  91. {
  92. int qp, i;
  93. int bias = 0; //FIXME
  94. for (qp = 0; qp < 99; qp++) {
  95. for (i = 0; i < 16; i++) {
  96. p->thres2[qp][i] = ((i&1) ? SN2 : SN0) * ((i&4) ? SN2 : SN0) * FFMAX(1, qp) * (1<<2) - 1 - bias;
  97. }
  98. }
  99. }
  100. static inline void dctA_c(int16_t *dst, uint8_t *src, int stride)
  101. {
  102. int i;
  103. for (i = 0; i < 4; i++) {
  104. int s0 = src[0 * stride] + src[6 * stride];
  105. int s1 = src[1 * stride] + src[5 * stride];
  106. int s2 = src[2 * stride] + src[4 * stride];
  107. int s3 = src[3 * stride];
  108. int s = s3 + s3;
  109. s3 = s - s0;
  110. s0 = s + s0;
  111. s = s2 + s1;
  112. s2 = s2 - s1;
  113. dst[0] = s0 + s;
  114. dst[2] = s0 - s;
  115. dst[1] = 2 * s3 + s2;
  116. dst[3] = s3 - 2 * s2;
  117. src++;
  118. dst += 4;
  119. }
  120. }
  121. static void dctB_c(int16_t *dst, int16_t *src)
  122. {
  123. int i;
  124. for (i = 0; i < 4; i++) {
  125. int s0 = src[0 * 4] + src[6 * 4];
  126. int s1 = src[1 * 4] + src[5 * 4];
  127. int s2 = src[2 * 4] + src[4 * 4];
  128. int s3 = src[3 * 4];
  129. int s = s3 + s3;
  130. s3 = s - s0;
  131. s0 = s + s0;
  132. s = s2 + s1;
  133. s2 = s2 - s1;
  134. dst[0 * 4] = s0 + s;
  135. dst[2 * 4] = s0 - s;
  136. dst[1 * 4] = 2 * s3 + s2;
  137. dst[3 * 4] = s3 - 2 * s2;
  138. src++;
  139. dst++;
  140. }
  141. }
  142. static int hardthresh_c(PP7Context *p, int16_t *src, int qp)
  143. {
  144. int i;
  145. int a;
  146. a = src[0] * factor[0];
  147. for (i = 1; i < 16; i++) {
  148. unsigned int threshold1 = p->thres2[qp][i];
  149. unsigned int threshold2 = threshold1 << 1;
  150. int level = src[i];
  151. if (((unsigned)(level + threshold1)) > threshold2)
  152. a += level * factor[i];
  153. }
  154. return (a + (1 << 11)) >> 12;
  155. }
  156. static int mediumthresh_c(PP7Context *p, int16_t *src, int qp)
  157. {
  158. int i;
  159. int a;
  160. a = src[0] * factor[0];
  161. for (i = 1; i < 16; i++) {
  162. unsigned int threshold1 = p->thres2[qp][i];
  163. unsigned int threshold2 = threshold1 << 1;
  164. int level = src[i];
  165. if (((unsigned)(level + threshold1)) > threshold2) {
  166. if (((unsigned)(level + 2 * threshold1)) > 2 * threshold2)
  167. a += level * factor[i];
  168. else {
  169. if (level > 0)
  170. a += 2 * (level - (int)threshold1) * factor[i];
  171. else
  172. a += 2 * (level + (int)threshold1) * factor[i];
  173. }
  174. }
  175. }
  176. return (a + (1 << 11)) >> 12;
  177. }
  178. static int softthresh_c(PP7Context *p, int16_t *src, int qp)
  179. {
  180. int i;
  181. int a;
  182. a = src[0] * factor[0];
  183. for (i = 1; i < 16; i++) {
  184. unsigned int threshold1 = p->thres2[qp][i];
  185. unsigned int threshold2 = threshold1 << 1;
  186. int level = src[i];
  187. if (((unsigned)(level + threshold1)) > threshold2) {
  188. if (level > 0)
  189. a += (level - (int)threshold1) * factor[i];
  190. else
  191. a += (level + (int)threshold1) * factor[i];
  192. }
  193. }
  194. return (a + (1 << 11)) >> 12;
  195. }
  196. static void filter(PP7Context *p, uint8_t *dst, uint8_t *src,
  197. int dst_stride, int src_stride,
  198. int width, int height,
  199. uint8_t *qp_store, int qp_stride, int is_luma)
  200. {
  201. int x, y;
  202. const int stride = is_luma ? p->temp_stride : ((width + 16 + 15) & (~15));
  203. uint8_t *p_src = p->src + 8 * stride;
  204. int16_t *block = (int16_t *)p->src;
  205. int16_t *temp = (int16_t *)(p->src + 32);
  206. if (!src || !dst) return;
  207. for (y = 0; y < height; y++) {
  208. int index = 8 + 8 * stride + y * stride;
  209. memcpy(p_src + index, src + y * src_stride, width);
  210. for (x = 0; x < 8; x++) {
  211. p_src[index - x - 1]= p_src[index + x ];
  212. p_src[index + width + x ]= p_src[index + width - x - 1];
  213. }
  214. }
  215. for (y = 0; y < 8; y++) {
  216. memcpy(p_src + ( 7 - y ) * stride, p_src + ( y + 8 ) * stride, stride);
  217. memcpy(p_src + (height + 8 + y) * stride, p_src + (height - y + 7) * stride, stride);
  218. }
  219. //FIXME (try edge emu)
  220. for (y = 0; y < height; y++) {
  221. for (x = -8; x < 0; x += 4) {
  222. const int index = x + y * stride + (8 - 3) * (1 + stride) + 8; //FIXME silly offset
  223. uint8_t *src = p_src + index;
  224. int16_t *tp = temp + 4 * x;
  225. dctA_c(tp + 4 * 8, src, stride);
  226. }
  227. for (x = 0; x < width; ) {
  228. const int qps = 3 + is_luma;
  229. int qp;
  230. int end = FFMIN(x + 8, width);
  231. if (p->qp)
  232. qp = p->qp;
  233. else {
  234. qp = qp_store[ (FFMIN(x, width - 1) >> qps) + (FFMIN(y, height - 1) >> qps) * qp_stride];
  235. qp = norm_qscale(qp, p->qscale_type);
  236. }
  237. for (; x < end; x++) {
  238. const int index = x + y * stride + (8 - 3) * (1 + stride) + 8; //FIXME silly offset
  239. uint8_t *src = p_src + index;
  240. int16_t *tp = temp + 4 * x;
  241. int v;
  242. if ((x & 3) == 0)
  243. dctA_c(tp + 4 * 8, src, stride);
  244. p->dctB(block, tp);
  245. v = p->requantize(p, block, qp);
  246. v = (v + dither[y & 7][x & 7]) >> 6;
  247. if ((unsigned)v > 255)
  248. v = (-v) >> 31;
  249. dst[x + y * dst_stride] = v;
  250. }
  251. }
  252. }
  253. }
  254. static int query_formats(AVFilterContext *ctx)
  255. {
  256. static const enum PixelFormat pix_fmts[] = {
  257. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P,
  258. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV411P,
  259. AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV440P,
  260. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ422P,
  261. AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ440P,
  262. AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE
  263. };
  264. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  265. return 0;
  266. }
  267. static int config_input(AVFilterLink *inlink)
  268. {
  269. AVFilterContext *ctx = inlink->dst;
  270. PP7Context *pp7 = ctx->priv;
  271. const int h = FFALIGN(inlink->h + 16, 16);
  272. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  273. pp7->hsub = desc->log2_chroma_w;
  274. pp7->vsub = desc->log2_chroma_h;
  275. pp7->temp_stride = FFALIGN(inlink->w + 16, 16);
  276. pp7->src = av_malloc(pp7->temp_stride * (h + 8) * sizeof(uint8_t));
  277. if (!pp7->src)
  278. return AVERROR(ENOMEM);
  279. init_thres2(pp7);
  280. switch (pp7->mode) {
  281. case 0: pp7->requantize = hardthresh_c; break;
  282. case 1: pp7->requantize = softthresh_c; break;
  283. default:
  284. case 2: pp7->requantize = mediumthresh_c; break;
  285. }
  286. pp7->dctB = dctB_c;
  287. if (ARCH_X86)
  288. ff_pp7_init_x86(pp7);
  289. return 0;
  290. }
  291. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  292. {
  293. AVFilterContext *ctx = inlink->dst;
  294. PP7Context *pp7 = ctx->priv;
  295. AVFilterLink *outlink = ctx->outputs[0];
  296. AVFrame *out = in;
  297. int qp_stride = 0;
  298. uint8_t *qp_table = NULL;
  299. if (!pp7->qp)
  300. qp_table = av_frame_get_qp_table(in, &qp_stride, &pp7->qscale_type);
  301. if (!ctx->is_disabled) {
  302. const int cw = FF_CEIL_RSHIFT(inlink->w, pp7->hsub);
  303. const int ch = FF_CEIL_RSHIFT(inlink->h, pp7->vsub);
  304. /* get a new frame if in-place is not possible or if the dimensions
  305. * are not multiple of 8 */
  306. if (!av_frame_is_writable(in) || (inlink->w & 7) || (inlink->h & 7)) {
  307. const int aligned_w = FFALIGN(inlink->w, 8);
  308. const int aligned_h = FFALIGN(inlink->h, 8);
  309. out = ff_get_video_buffer(outlink, aligned_w, aligned_h);
  310. if (!out) {
  311. av_frame_free(&in);
  312. return AVERROR(ENOMEM);
  313. }
  314. av_frame_copy_props(out, in);
  315. }
  316. if (qp_table || pp7->qp) {
  317. filter(pp7, out->data[0], in->data[0], out->linesize[0], in->linesize[0],
  318. inlink->w, inlink->h, qp_table, qp_stride, 1);
  319. filter(pp7, out->data[1], in->data[1], out->linesize[1], in->linesize[1],
  320. cw, ch, qp_table, qp_stride, 0);
  321. filter(pp7, out->data[2], in->data[2], out->linesize[2], in->linesize[2],
  322. cw, ch, qp_table, qp_stride, 0);
  323. emms_c();
  324. }
  325. }
  326. if (in != out) {
  327. if (in->data[3])
  328. av_image_copy_plane(out->data[3], out->linesize[3],
  329. in ->data[3], in ->linesize[3],
  330. inlink->w, inlink->h);
  331. av_frame_free(&in);
  332. }
  333. return ff_filter_frame(outlink, out);
  334. }
  335. static av_cold void uninit(AVFilterContext *ctx)
  336. {
  337. PP7Context *pp7 = ctx->priv;
  338. av_freep(&pp7->src);
  339. }
  340. static const AVFilterPad pp7_inputs[] = {
  341. {
  342. .name = "default",
  343. .type = AVMEDIA_TYPE_VIDEO,
  344. .config_props = config_input,
  345. .filter_frame = filter_frame,
  346. },
  347. { NULL }
  348. };
  349. static const AVFilterPad pp7_outputs[] = {
  350. {
  351. .name = "default",
  352. .type = AVMEDIA_TYPE_VIDEO,
  353. },
  354. { NULL }
  355. };
  356. AVFilter ff_vf_pp7 = {
  357. .name = "pp7",
  358. .description = NULL_IF_CONFIG_SMALL("Apply Postprocessing 7 filter."),
  359. .priv_size = sizeof(PP7Context),
  360. .uninit = uninit,
  361. .query_formats = query_formats,
  362. .inputs = pp7_inputs,
  363. .outputs = pp7_outputs,
  364. .priv_class = &pp7_class,
  365. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
  366. };