vf_decimate.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 decimate filter, ported from libmpcodecs/vf_decimate.c by
  23. * Rich Felker.
  24. */
  25. #include "libavutil/pixdesc.h"
  26. #include "libavutil/timestamp.h"
  27. #include "libavcodec/dsputil.h"
  28. #include "avfilter.h"
  29. #include "internal.h"
  30. #include "formats.h"
  31. #include "video.h"
  32. typedef struct {
  33. int lo, hi; ///< lower and higher threshold number of differences
  34. ///< values for 8x8 blocks
  35. float frac; ///< threshold of changed pixels over the total fraction
  36. int max_drop_count; ///< if positive: maximum number of sequential frames to drop
  37. ///< if negative: minimum number of frames between two drops
  38. int drop_count; ///< if positive: number of frames sequentially dropped
  39. ///< if negative: number of sequential frames which were not dropped
  40. int hsub, vsub; ///< chroma subsampling values
  41. AVFilterBufferRef *ref; ///< reference picture
  42. DSPContext dspctx; ///< context providing optimized diff routines
  43. AVCodecContext *avctx; ///< codec context required for the DSPContext
  44. } DecimateContext;
  45. /**
  46. * Return 1 if the two planes are different, 0 otherwise.
  47. */
  48. static int diff_planes(AVFilterContext *ctx,
  49. uint8_t *cur, uint8_t *ref, int linesize,
  50. int w, int h)
  51. {
  52. DecimateContext *decimate = ctx->priv;
  53. DSPContext *dspctx = &decimate->dspctx;
  54. int x, y;
  55. int d, c = 0;
  56. int t = (w/16)*(h/16)*decimate->frac;
  57. DCTELEM block[8*8];
  58. /* compute difference for blocks of 8x8 bytes */
  59. for (y = 0; y < h-7; y += 4) {
  60. for (x = 8; x < w-7; x += 4) {
  61. dspctx->diff_pixels(block,
  62. cur+x+y*linesize,
  63. ref+x+y*linesize, linesize);
  64. d = dspctx->sum_abs_dctelem(block);
  65. if (d > decimate->hi)
  66. return 1;
  67. if (d > decimate->lo) {
  68. c++;
  69. if (c > t)
  70. return 1;
  71. }
  72. }
  73. }
  74. return 0;
  75. }
  76. /**
  77. * Tell if the frame should be decimated, for example if it is no much
  78. * different with respect to the reference frame ref.
  79. */
  80. static int decimate_frame(AVFilterContext *ctx,
  81. AVFilterBufferRef *cur, AVFilterBufferRef *ref)
  82. {
  83. DecimateContext *decimate = ctx->priv;
  84. int plane;
  85. if (decimate->max_drop_count > 0 &&
  86. decimate->drop_count >= decimate->max_drop_count)
  87. return 0;
  88. if (decimate->max_drop_count < 0 &&
  89. (decimate->drop_count-1) > decimate->max_drop_count)
  90. return 0;
  91. for (plane = 0; ref->data[plane] && ref->linesize[plane]; plane++) {
  92. int vsub = plane == 1 || plane == 2 ? decimate->vsub : 0;
  93. int hsub = plane == 1 || plane == 2 ? decimate->hsub : 0;
  94. if (diff_planes(ctx,
  95. cur->data[plane], ref->data[plane], ref->linesize[plane],
  96. ref->video->w>>hsub, ref->video->h>>vsub))
  97. return 0;
  98. }
  99. return 1;
  100. }
  101. static av_cold int init(AVFilterContext *ctx, const char *args)
  102. {
  103. DecimateContext *decimate = ctx->priv;
  104. /* set default values */
  105. decimate->drop_count = decimate->max_drop_count = 0;
  106. decimate->lo = 64*5;
  107. decimate->hi = 64*12;
  108. decimate->frac = 0.33;
  109. if (args) {
  110. char c1, c2, c3, c4;
  111. int n = sscanf(args, "%d%c%d%c%d%c%f%c",
  112. &decimate->max_drop_count, &c1,
  113. &decimate->hi, &c2, &decimate->lo, &c3,
  114. &decimate->frac, &c4);
  115. if (n != 1 &&
  116. (n != 3 || c1 != ':') &&
  117. (n != 5 || c1 != ':' || c2 != ':') &&
  118. (n != 7 || c1 != ':' || c2 != ':' || c3 != ':')) {
  119. av_log(ctx, AV_LOG_ERROR,
  120. "Invalid syntax for argument '%s': "
  121. "must be in the form 'max:hi:lo:frac'\n", args);
  122. return AVERROR(EINVAL);
  123. }
  124. }
  125. av_log(ctx, AV_LOG_VERBOSE, "max_drop_count:%d hi:%d lo:%d frac:%f\n",
  126. decimate->max_drop_count, decimate->hi, decimate->lo, decimate->frac);
  127. decimate->avctx = avcodec_alloc_context3(NULL);
  128. if (!decimate->avctx)
  129. return AVERROR(ENOMEM);
  130. dsputil_init(&decimate->dspctx, decimate->avctx);
  131. return 0;
  132. }
  133. static av_cold void uninit(AVFilterContext *ctx)
  134. {
  135. DecimateContext *decimate = ctx->priv;
  136. avfilter_unref_bufferp(&decimate->ref);
  137. avcodec_close(decimate->avctx);
  138. av_freep(&decimate->avctx);
  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_NONE
  150. };
  151. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  152. return 0;
  153. }
  154. static int config_input(AVFilterLink *inlink)
  155. {
  156. AVFilterContext *ctx = inlink->dst;
  157. DecimateContext *decimate = ctx->priv;
  158. const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
  159. decimate->hsub = pix_desc->log2_chroma_w;
  160. decimate->vsub = pix_desc->log2_chroma_h;
  161. return 0;
  162. }
  163. static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *cur)
  164. {
  165. DecimateContext *decimate = inlink->dst->priv;
  166. AVFilterLink *outlink = inlink->dst->outputs[0];
  167. int ret;
  168. if (decimate->ref && decimate_frame(inlink->dst, cur, decimate->ref)) {
  169. decimate->drop_count = FFMAX(1, decimate->drop_count+1);
  170. } else {
  171. avfilter_unref_buffer(decimate->ref);
  172. decimate->ref = cur;
  173. decimate->drop_count = FFMIN(-1, decimate->drop_count-1);
  174. if (ret = ff_filter_frame(outlink, avfilter_ref_buffer(cur, ~AV_PERM_WRITE)) < 0)
  175. return ret;
  176. }
  177. av_log(inlink->dst, AV_LOG_DEBUG,
  178. "%s pts:%s pts_time:%s drop_count:%d\n",
  179. decimate->drop_count > 0 ? "drop" : "keep",
  180. av_ts2str(cur->pts), av_ts2timestr(cur->pts, &inlink->time_base),
  181. decimate->drop_count);
  182. if (decimate->drop_count > 0)
  183. avfilter_unref_buffer(cur);
  184. return 0;
  185. }
  186. static int request_frame(AVFilterLink *outlink)
  187. {
  188. DecimateContext *decimate = outlink->src->priv;
  189. AVFilterLink *inlink = outlink->src->inputs[0];
  190. int ret;
  191. do {
  192. ret = ff_request_frame(inlink);
  193. } while (decimate->drop_count > 0 && ret >= 0);
  194. return ret;
  195. }
  196. static const AVFilterPad decimate_inputs[] = {
  197. {
  198. .name = "default",
  199. .type = AVMEDIA_TYPE_VIDEO,
  200. .get_video_buffer = ff_null_get_video_buffer,
  201. .config_props = config_input,
  202. .filter_frame = filter_frame,
  203. .min_perms = AV_PERM_READ | AV_PERM_PRESERVE,
  204. },
  205. { NULL }
  206. };
  207. static const AVFilterPad decimate_outputs[] = {
  208. {
  209. .name = "default",
  210. .type = AVMEDIA_TYPE_VIDEO,
  211. .request_frame = request_frame,
  212. },
  213. { NULL }
  214. };
  215. AVFilter avfilter_vf_decimate = {
  216. .name = "decimate",
  217. .description = NULL_IF_CONFIG_SMALL("Remove near-duplicate frames."),
  218. .init = init,
  219. .uninit = uninit,
  220. .priv_size = sizeof(DecimateContext),
  221. .query_formats = query_formats,
  222. .inputs = decimate_inputs,
  223. .outputs = decimate_outputs,
  224. };