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.

250 lines
7.9KB

  1. /*
  2. * Copyright (c) 2003 Rich Felker
  3. * Copyright (c) 2012 Stefano Sabatini
  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 mpdecimate filter, ported from libmpcodecs/vf_decimate.c by
  23. * Rich Felker.
  24. */
  25. #include "libavutil/opt.h"
  26. #include "libavutil/pixdesc.h"
  27. #include "libavutil/pixelutils.h"
  28. #include "libavutil/timestamp.h"
  29. #include "avfilter.h"
  30. #include "internal.h"
  31. #include "formats.h"
  32. #include "video.h"
  33. typedef struct {
  34. const AVClass *class;
  35. int lo, hi; ///< lower and higher threshold number of differences
  36. ///< values for 8x8 blocks
  37. float frac; ///< threshold of changed pixels over the total fraction
  38. int max_drop_count; ///< if positive: maximum number of sequential frames to drop
  39. ///< if negative: minimum number of frames between two drops
  40. int drop_count; ///< if positive: number of frames sequentially dropped
  41. ///< if negative: number of sequential frames which were not dropped
  42. int hsub, vsub; ///< chroma subsampling values
  43. AVFrame *ref; ///< reference picture
  44. av_pixelutils_sad_fn sad; ///< sum of absolute difference function
  45. } DecimateContext;
  46. #define OFFSET(x) offsetof(DecimateContext, x)
  47. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  48. static const AVOption mpdecimate_options[] = {
  49. { "max", "set the maximum number of consecutive dropped frames (positive), or the minimum interval between dropped frames (negative)",
  50. OFFSET(max_drop_count), AV_OPT_TYPE_INT, {.i64=0}, INT_MIN, INT_MAX, FLAGS },
  51. { "hi", "set high dropping threshold", OFFSET(hi), AV_OPT_TYPE_INT, {.i64=64*12}, INT_MIN, INT_MAX, FLAGS },
  52. { "lo", "set low dropping threshold", OFFSET(lo), AV_OPT_TYPE_INT, {.i64=64*5}, INT_MIN, INT_MAX, FLAGS },
  53. { "frac", "set fraction dropping threshold", OFFSET(frac), AV_OPT_TYPE_FLOAT, {.dbl=0.33}, 0, 1, FLAGS },
  54. { NULL }
  55. };
  56. AVFILTER_DEFINE_CLASS(mpdecimate);
  57. /**
  58. * Return 1 if the two planes are different, 0 otherwise.
  59. */
  60. static int diff_planes(AVFilterContext *ctx,
  61. uint8_t *cur, int cur_linesize,
  62. uint8_t *ref, int ref_linesize,
  63. int w, int h)
  64. {
  65. DecimateContext *decimate = ctx->priv;
  66. int x, y;
  67. int d, c = 0;
  68. int t = (w/16)*(h/16)*decimate->frac;
  69. /* compute difference for blocks of 8x8 bytes */
  70. for (y = 0; y < h-7; y += 4) {
  71. for (x = 8; x < w-7; x += 4) {
  72. d = decimate->sad(cur + y*cur_linesize + x, cur_linesize,
  73. ref + y*ref_linesize + x, ref_linesize);
  74. if (d > decimate->hi)
  75. return 1;
  76. if (d > decimate->lo) {
  77. c++;
  78. if (c > t)
  79. return 1;
  80. }
  81. }
  82. }
  83. return 0;
  84. }
  85. /**
  86. * Tell if the frame should be decimated, for example if it is no much
  87. * different with respect to the reference frame ref.
  88. */
  89. static int decimate_frame(AVFilterContext *ctx,
  90. AVFrame *cur, AVFrame *ref)
  91. {
  92. DecimateContext *decimate = ctx->priv;
  93. int plane;
  94. if (decimate->max_drop_count > 0 &&
  95. decimate->drop_count >= decimate->max_drop_count)
  96. return 0;
  97. if (decimate->max_drop_count < 0 &&
  98. (decimate->drop_count-1) > decimate->max_drop_count)
  99. return 0;
  100. for (plane = 0; ref->data[plane] && ref->linesize[plane]; plane++) {
  101. int vsub = plane == 1 || plane == 2 ? decimate->vsub : 0;
  102. int hsub = plane == 1 || plane == 2 ? decimate->hsub : 0;
  103. if (diff_planes(ctx,
  104. cur->data[plane], cur->linesize[plane],
  105. ref->data[plane], ref->linesize[plane],
  106. FF_CEIL_RSHIFT(ref->width, hsub),
  107. FF_CEIL_RSHIFT(ref->height, vsub)))
  108. return 0;
  109. }
  110. return 1;
  111. }
  112. static av_cold int init(AVFilterContext *ctx)
  113. {
  114. DecimateContext *decimate = ctx->priv;
  115. decimate->sad = av_pixelutils_get_sad_fn(3, 3, 0, decimate); // 8x8, not aligned on blocksize
  116. if (!decimate->sad)
  117. return AVERROR(EINVAL);
  118. av_log(ctx, AV_LOG_VERBOSE, "max_drop_count:%d hi:%d lo:%d frac:%f\n",
  119. decimate->max_drop_count, decimate->hi, decimate->lo, decimate->frac);
  120. return 0;
  121. }
  122. static av_cold void uninit(AVFilterContext *ctx)
  123. {
  124. DecimateContext *decimate = ctx->priv;
  125. av_frame_free(&decimate->ref);
  126. }
  127. static int query_formats(AVFilterContext *ctx)
  128. {
  129. static const enum AVPixelFormat pix_fmts[] = {
  130. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P,
  131. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV411P,
  132. AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV440P,
  133. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ422P,
  134. AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ440P,
  135. AV_PIX_FMT_YUVA420P,
  136. AV_PIX_FMT_NONE
  137. };
  138. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  139. return 0;
  140. }
  141. static int config_input(AVFilterLink *inlink)
  142. {
  143. AVFilterContext *ctx = inlink->dst;
  144. DecimateContext *decimate = ctx->priv;
  145. const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
  146. decimate->hsub = pix_desc->log2_chroma_w;
  147. decimate->vsub = pix_desc->log2_chroma_h;
  148. return 0;
  149. }
  150. static int filter_frame(AVFilterLink *inlink, AVFrame *cur)
  151. {
  152. DecimateContext *decimate = inlink->dst->priv;
  153. AVFilterLink *outlink = inlink->dst->outputs[0];
  154. int ret;
  155. if (decimate->ref && decimate_frame(inlink->dst, cur, decimate->ref)) {
  156. decimate->drop_count = FFMAX(1, decimate->drop_count+1);
  157. } else {
  158. av_frame_free(&decimate->ref);
  159. decimate->ref = cur;
  160. decimate->drop_count = FFMIN(-1, decimate->drop_count-1);
  161. if (ret = ff_filter_frame(outlink, av_frame_clone(cur)) < 0)
  162. return ret;
  163. }
  164. av_log(inlink->dst, AV_LOG_DEBUG,
  165. "%s pts:%s pts_time:%s drop_count:%d\n",
  166. decimate->drop_count > 0 ? "drop" : "keep",
  167. av_ts2str(cur->pts), av_ts2timestr(cur->pts, &inlink->time_base),
  168. decimate->drop_count);
  169. if (decimate->drop_count > 0)
  170. av_frame_free(&cur);
  171. return 0;
  172. }
  173. static int request_frame(AVFilterLink *outlink)
  174. {
  175. DecimateContext *decimate = outlink->src->priv;
  176. AVFilterLink *inlink = outlink->src->inputs[0];
  177. int ret;
  178. do {
  179. ret = ff_request_frame(inlink);
  180. } while (decimate->drop_count > 0 && ret >= 0);
  181. return ret;
  182. }
  183. static const AVFilterPad mpdecimate_inputs[] = {
  184. {
  185. .name = "default",
  186. .type = AVMEDIA_TYPE_VIDEO,
  187. .config_props = config_input,
  188. .filter_frame = filter_frame,
  189. },
  190. { NULL }
  191. };
  192. static const AVFilterPad mpdecimate_outputs[] = {
  193. {
  194. .name = "default",
  195. .type = AVMEDIA_TYPE_VIDEO,
  196. .request_frame = request_frame,
  197. },
  198. { NULL }
  199. };
  200. AVFilter ff_vf_mpdecimate = {
  201. .name = "mpdecimate",
  202. .description = NULL_IF_CONFIG_SMALL("Remove near-duplicate frames."),
  203. .init = init,
  204. .uninit = uninit,
  205. .priv_size = sizeof(DecimateContext),
  206. .priv_class = &mpdecimate_class,
  207. .query_formats = query_formats,
  208. .inputs = mpdecimate_inputs,
  209. .outputs = mpdecimate_outputs,
  210. };