vf_decimate.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 PixelFormat pix_fmts[] = {
  143. PIX_FMT_YUV444P, PIX_FMT_YUV422P,
  144. PIX_FMT_YUV420P, PIX_FMT_YUV411P,
  145. PIX_FMT_YUV410P, PIX_FMT_YUV440P,
  146. PIX_FMT_YUVJ444P, PIX_FMT_YUVJ422P,
  147. PIX_FMT_YUVJ420P, PIX_FMT_YUVJ440P,
  148. PIX_FMT_YUVA420P,
  149. 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_descriptors[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 start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref) { return 0; }
  164. static int draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir) { return 0; }
  165. static int end_frame(AVFilterLink *inlink)
  166. {
  167. DecimateContext *decimate = inlink->dst->priv;
  168. AVFilterBufferRef *cur = inlink->cur_buf;
  169. AVFilterLink *outlink = inlink->dst->outputs[0];
  170. int ret;
  171. if (decimate->ref && decimate_frame(inlink->dst, cur, decimate->ref)) {
  172. decimate->drop_count = FFMAX(1, decimate->drop_count+1);
  173. } else {
  174. avfilter_unref_buffer(decimate->ref);
  175. decimate->ref = cur;
  176. inlink->cur_buf = NULL;
  177. decimate->drop_count = FFMIN(-1, decimate->drop_count-1);
  178. if ((ret = ff_start_frame(outlink,
  179. avfilter_ref_buffer(cur, ~AV_PERM_WRITE)) < 0) ||
  180. (ret = ff_draw_slice(outlink, 0, inlink->h, 1)) < 0 ||
  181. (ret = ff_end_frame(outlink)) < 0)
  182. return ret;
  183. }
  184. av_log(inlink->dst, AV_LOG_DEBUG,
  185. "%s pts:%s pts_time:%s drop_count:%d\n",
  186. decimate->drop_count > 0 ? "drop" : "keep",
  187. av_ts2str(cur->pts), av_ts2timestr(cur->pts, &inlink->time_base),
  188. decimate->drop_count);
  189. return 0;
  190. }
  191. static int request_frame(AVFilterLink *outlink)
  192. {
  193. DecimateContext *decimate = outlink->src->priv;
  194. AVFilterLink *inlink = outlink->src->inputs[0];
  195. int ret;
  196. do {
  197. ret = ff_request_frame(inlink);
  198. } while (decimate->drop_count > 0 && ret >= 0);
  199. return ret;
  200. }
  201. AVFilter avfilter_vf_decimate = {
  202. .name = "decimate",
  203. .description = NULL_IF_CONFIG_SMALL("Remove near-duplicate frames."),
  204. .init = init,
  205. .uninit = uninit,
  206. .priv_size = sizeof(DecimateContext),
  207. .query_formats = query_formats,
  208. .inputs = (const AVFilterPad[]) {
  209. {
  210. .name = "default",
  211. .type = AVMEDIA_TYPE_VIDEO,
  212. .get_video_buffer = ff_null_get_video_buffer,
  213. .config_props = config_input,
  214. .start_frame = start_frame,
  215. .draw_slice = draw_slice,
  216. .end_frame = end_frame,
  217. .min_perms = AV_PERM_READ | AV_PERM_PRESERVE,
  218. },
  219. { .name = NULL }
  220. },
  221. .outputs = (const AVFilterPad[]) {
  222. {
  223. .name = "default",
  224. .type = AVMEDIA_TYPE_VIDEO,
  225. .request_frame = request_frame,
  226. },
  227. { .name = NULL }
  228. },
  229. };