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.

259 lines
8.2KB

  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/timestamp.h"
  28. #include "libavcodec/dsputil.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. DSPContext dspctx; ///< context providing optimized diff routines
  45. AVCodecContext *avctx; ///< codec context required for the DSPContext
  46. } DecimateContext;
  47. #define OFFSET(x) offsetof(DecimateContext, x)
  48. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  49. static const AVOption mpdecimate_options[] = {
  50. { "max", "set the maximum number of consecutive dropped frames (positive), or the minimum interval between dropped frames (negative)",
  51. OFFSET(max_drop_count), AV_OPT_TYPE_INT, {.i64=0}, INT_MIN, INT_MAX, FLAGS },
  52. { "hi", "set high dropping threshold", OFFSET(hi), AV_OPT_TYPE_INT, {.i64=64*12}, INT_MIN, INT_MAX, FLAGS },
  53. { "lo", "set low dropping threshold", OFFSET(lo), AV_OPT_TYPE_INT, {.i64=64*5}, INT_MIN, INT_MAX, FLAGS },
  54. { "frac", "set fraction dropping threshold", OFFSET(frac), AV_OPT_TYPE_FLOAT, {.dbl=0.33}, 0, 1, FLAGS },
  55. { NULL }
  56. };
  57. AVFILTER_DEFINE_CLASS(mpdecimate);
  58. /**
  59. * Return 1 if the two planes are different, 0 otherwise.
  60. */
  61. static int diff_planes(AVFilterContext *ctx,
  62. uint8_t *cur, uint8_t *ref, int linesize,
  63. int w, int h)
  64. {
  65. DecimateContext *decimate = ctx->priv;
  66. DSPContext *dspctx = &decimate->dspctx;
  67. int x, y;
  68. int d, c = 0;
  69. int t = (w/16)*(h/16)*decimate->frac;
  70. int16_t block[8*8];
  71. /* compute difference for blocks of 8x8 bytes */
  72. for (y = 0; y < h-7; y += 4) {
  73. for (x = 8; x < w-7; x += 4) {
  74. dspctx->diff_pixels(block,
  75. cur+x+y*linesize,
  76. ref+x+y*linesize, linesize);
  77. d = dspctx->sum_abs_dctelem(block);
  78. if (d > decimate->hi)
  79. return 1;
  80. if (d > decimate->lo) {
  81. c++;
  82. if (c > t)
  83. return 1;
  84. }
  85. }
  86. }
  87. return 0;
  88. }
  89. /**
  90. * Tell if the frame should be decimated, for example if it is no much
  91. * different with respect to the reference frame ref.
  92. */
  93. static int decimate_frame(AVFilterContext *ctx,
  94. AVFrame *cur, AVFrame *ref)
  95. {
  96. DecimateContext *decimate = ctx->priv;
  97. int plane;
  98. if (decimate->max_drop_count > 0 &&
  99. decimate->drop_count >= decimate->max_drop_count)
  100. return 0;
  101. if (decimate->max_drop_count < 0 &&
  102. (decimate->drop_count-1) > decimate->max_drop_count)
  103. return 0;
  104. for (plane = 0; ref->data[plane] && ref->linesize[plane]; plane++) {
  105. int vsub = plane == 1 || plane == 2 ? decimate->vsub : 0;
  106. int hsub = plane == 1 || plane == 2 ? decimate->hsub : 0;
  107. if (diff_planes(ctx,
  108. cur->data[plane], ref->data[plane], ref->linesize[plane],
  109. ref->width>>hsub, ref->height>>vsub))
  110. return 0;
  111. }
  112. return 1;
  113. }
  114. static av_cold int init(AVFilterContext *ctx)
  115. {
  116. DecimateContext *decimate = ctx->priv;
  117. av_log(ctx, AV_LOG_VERBOSE, "max_drop_count:%d hi:%d lo:%d frac:%f\n",
  118. decimate->max_drop_count, decimate->hi, decimate->lo, decimate->frac);
  119. decimate->avctx = avcodec_alloc_context3(NULL);
  120. if (!decimate->avctx)
  121. return AVERROR(ENOMEM);
  122. avpriv_dsputil_init(&decimate->dspctx, decimate->avctx);
  123. return 0;
  124. }
  125. static av_cold void uninit(AVFilterContext *ctx)
  126. {
  127. DecimateContext *decimate = ctx->priv;
  128. av_frame_free(&decimate->ref);
  129. if (decimate->avctx) {
  130. avcodec_close(decimate->avctx);
  131. av_freep(&decimate->avctx);
  132. }
  133. }
  134. static int query_formats(AVFilterContext *ctx)
  135. {
  136. static const enum AVPixelFormat pix_fmts[] = {
  137. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P,
  138. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV411P,
  139. AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV440P,
  140. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ422P,
  141. AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ440P,
  142. AV_PIX_FMT_YUVA420P,
  143. AV_PIX_FMT_NONE
  144. };
  145. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  146. return 0;
  147. }
  148. static int config_input(AVFilterLink *inlink)
  149. {
  150. AVFilterContext *ctx = inlink->dst;
  151. DecimateContext *decimate = ctx->priv;
  152. const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
  153. decimate->hsub = pix_desc->log2_chroma_w;
  154. decimate->vsub = pix_desc->log2_chroma_h;
  155. return 0;
  156. }
  157. static int filter_frame(AVFilterLink *inlink, AVFrame *cur)
  158. {
  159. DecimateContext *decimate = inlink->dst->priv;
  160. AVFilterLink *outlink = inlink->dst->outputs[0];
  161. int ret;
  162. if (decimate->ref && decimate_frame(inlink->dst, cur, decimate->ref)) {
  163. decimate->drop_count = FFMAX(1, decimate->drop_count+1);
  164. } else {
  165. av_frame_free(&decimate->ref);
  166. decimate->ref = cur;
  167. decimate->drop_count = FFMIN(-1, decimate->drop_count-1);
  168. if (ret = ff_filter_frame(outlink, av_frame_clone(cur)) < 0)
  169. return ret;
  170. }
  171. av_log(inlink->dst, AV_LOG_DEBUG,
  172. "%s pts:%s pts_time:%s drop_count:%d\n",
  173. decimate->drop_count > 0 ? "drop" : "keep",
  174. av_ts2str(cur->pts), av_ts2timestr(cur->pts, &inlink->time_base),
  175. decimate->drop_count);
  176. if (decimate->drop_count > 0)
  177. av_frame_free(&cur);
  178. return 0;
  179. }
  180. static int request_frame(AVFilterLink *outlink)
  181. {
  182. DecimateContext *decimate = outlink->src->priv;
  183. AVFilterLink *inlink = outlink->src->inputs[0];
  184. int ret;
  185. do {
  186. ret = ff_request_frame(inlink);
  187. } while (decimate->drop_count > 0 && ret >= 0);
  188. return ret;
  189. }
  190. static const AVFilterPad mpdecimate_inputs[] = {
  191. {
  192. .name = "default",
  193. .type = AVMEDIA_TYPE_VIDEO,
  194. .get_video_buffer = ff_null_get_video_buffer,
  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. .request_frame = request_frame,
  205. },
  206. { NULL }
  207. };
  208. AVFilter avfilter_vf_mpdecimate = {
  209. .name = "mpdecimate",
  210. .description = NULL_IF_CONFIG_SMALL("Remove near-duplicate frames."),
  211. .init = init,
  212. .uninit = uninit,
  213. .priv_size = sizeof(DecimateContext),
  214. .query_formats = query_formats,
  215. .inputs = mpdecimate_inputs,
  216. .outputs = mpdecimate_outputs,
  217. .priv_class = &mpdecimate_class,
  218. };