vf_mpdecimate.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. av_log(ctx, AV_LOG_DEBUG, "%d>=hi ", d);
  76. return 1;
  77. }
  78. if (d > decimate->lo) {
  79. c++;
  80. if (c > t) {
  81. av_log(ctx, AV_LOG_DEBUG, "lo:%d>=%d ", c, t);
  82. return 1;
  83. }
  84. }
  85. }
  86. }
  87. av_log(ctx, AV_LOG_DEBUG, "lo:%d<%d ", c, t);
  88. return 0;
  89. }
  90. /**
  91. * Tell if the frame should be decimated, for example if it is no much
  92. * different with respect to the reference frame ref.
  93. */
  94. static int decimate_frame(AVFilterContext *ctx,
  95. AVFrame *cur, AVFrame *ref)
  96. {
  97. DecimateContext *decimate = ctx->priv;
  98. int plane;
  99. if (decimate->max_drop_count > 0 &&
  100. decimate->drop_count >= decimate->max_drop_count)
  101. return 0;
  102. if (decimate->max_drop_count < 0 &&
  103. (decimate->drop_count-1) > decimate->max_drop_count)
  104. return 0;
  105. for (plane = 0; ref->data[plane] && ref->linesize[plane]; plane++) {
  106. /* use 8x8 SAD even on subsampled planes. The blocks won't match up with
  107. * luma blocks, but hopefully nobody is depending on this to catch
  108. * localized chroma changes that wouldn't exceed the thresholds when
  109. * diluted by using what's effectively a larger block size.
  110. */
  111. int vsub = plane == 1 || plane == 2 ? decimate->vsub : 0;
  112. int hsub = plane == 1 || plane == 2 ? decimate->hsub : 0;
  113. if (diff_planes(ctx,
  114. cur->data[plane], cur->linesize[plane],
  115. ref->data[plane], ref->linesize[plane],
  116. AV_CEIL_RSHIFT(ref->width, hsub),
  117. AV_CEIL_RSHIFT(ref->height, vsub))) {
  118. emms_c();
  119. return 0;
  120. }
  121. }
  122. emms_c();
  123. return 1;
  124. }
  125. static av_cold int init(AVFilterContext *ctx)
  126. {
  127. DecimateContext *decimate = ctx->priv;
  128. decimate->sad = av_pixelutils_get_sad_fn(3, 3, 0, ctx); // 8x8, not aligned on blocksize
  129. if (!decimate->sad)
  130. return AVERROR(EINVAL);
  131. av_log(ctx, AV_LOG_VERBOSE, "max_drop_count:%d hi:%d lo:%d frac:%f\n",
  132. decimate->max_drop_count, decimate->hi, decimate->lo, decimate->frac);
  133. return 0;
  134. }
  135. static av_cold void uninit(AVFilterContext *ctx)
  136. {
  137. DecimateContext *decimate = ctx->priv;
  138. av_frame_free(&decimate->ref);
  139. }
  140. static int query_formats(AVFilterContext *ctx)
  141. {
  142. static const enum AVPixelFormat pix_fmts[] = {
  143. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P,
  144. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV411P,
  145. AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV440P,
  146. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ422P,
  147. AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ440P,
  148. AV_PIX_FMT_YUVA420P,
  149. AV_PIX_FMT_GBRP,
  150. AV_PIX_FMT_YUVA444P,
  151. AV_PIX_FMT_YUVA422P,
  152. AV_PIX_FMT_NONE
  153. };
  154. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  155. if (!fmts_list)
  156. return AVERROR(ENOMEM);
  157. return ff_set_common_formats(ctx, fmts_list);
  158. }
  159. static int config_input(AVFilterLink *inlink)
  160. {
  161. AVFilterContext *ctx = inlink->dst;
  162. DecimateContext *decimate = ctx->priv;
  163. const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
  164. decimate->hsub = pix_desc->log2_chroma_w;
  165. decimate->vsub = pix_desc->log2_chroma_h;
  166. return 0;
  167. }
  168. static int filter_frame(AVFilterLink *inlink, AVFrame *cur)
  169. {
  170. DecimateContext *decimate = inlink->dst->priv;
  171. AVFilterLink *outlink = inlink->dst->outputs[0];
  172. int ret;
  173. if (decimate->ref && decimate_frame(inlink->dst, cur, decimate->ref)) {
  174. decimate->drop_count = FFMAX(1, decimate->drop_count+1);
  175. } else {
  176. av_frame_free(&decimate->ref);
  177. decimate->ref = cur;
  178. decimate->drop_count = FFMIN(-1, decimate->drop_count-1);
  179. if ((ret = ff_filter_frame(outlink, av_frame_clone(cur))) < 0)
  180. return ret;
  181. }
  182. av_log(inlink->dst, AV_LOG_DEBUG,
  183. "%s pts:%s pts_time:%s drop_count:%d\n",
  184. decimate->drop_count > 0 ? "drop" : "keep",
  185. av_ts2str(cur->pts), av_ts2timestr(cur->pts, &inlink->time_base),
  186. decimate->drop_count);
  187. if (decimate->drop_count > 0)
  188. av_frame_free(&cur);
  189. return 0;
  190. }
  191. static const AVFilterPad mpdecimate_inputs[] = {
  192. {
  193. .name = "default",
  194. .type = AVMEDIA_TYPE_VIDEO,
  195. .config_props = config_input,
  196. .filter_frame = filter_frame,
  197. },
  198. { NULL }
  199. };
  200. static const AVFilterPad mpdecimate_outputs[] = {
  201. {
  202. .name = "default",
  203. .type = AVMEDIA_TYPE_VIDEO,
  204. },
  205. { NULL }
  206. };
  207. AVFilter ff_vf_mpdecimate = {
  208. .name = "mpdecimate",
  209. .description = NULL_IF_CONFIG_SMALL("Remove near-duplicate frames."),
  210. .init = init,
  211. .uninit = uninit,
  212. .priv_size = sizeof(DecimateContext),
  213. .priv_class = &mpdecimate_class,
  214. .query_formats = query_formats,
  215. .inputs = mpdecimate_inputs,
  216. .outputs = mpdecimate_outputs,
  217. };