yading@10: /* yading@10: * Copyright (c) 2003 Rich Felker yading@10: * Copyright (c) 2012 Stefano Sabatini yading@10: * yading@10: * This file is part of FFmpeg. yading@10: * yading@10: * FFmpeg is free software; you can redistribute it and/or modify yading@10: * it under the terms of the GNU General Public License as published by yading@10: * the Free Software Foundation; either version 2 of the License, or yading@10: * (at your option) any later version. yading@10: * yading@10: * FFmpeg is distributed in the hope that it will be useful, yading@10: * but WITHOUT ANY WARRANTY; without even the implied warranty of yading@10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the yading@10: * GNU General Public License for more details. yading@10: * yading@10: * You should have received a copy of the GNU General Public License along yading@10: * with FFmpeg; if not, write to the Free Software Foundation, Inc., yading@10: * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. yading@10: */ yading@10: yading@10: /** yading@10: * @file mpdecimate filter, ported from libmpcodecs/vf_decimate.c by yading@10: * Rich Felker. yading@10: */ yading@10: yading@10: #include "libavutil/opt.h" yading@10: #include "libavutil/pixdesc.h" yading@10: #include "libavutil/timestamp.h" yading@10: #include "libavcodec/dsputil.h" yading@10: #include "avfilter.h" yading@10: #include "internal.h" yading@10: #include "formats.h" yading@10: #include "video.h" yading@10: yading@10: typedef struct { yading@10: const AVClass *class; yading@10: int lo, hi; ///< lower and higher threshold number of differences yading@10: ///< values for 8x8 blocks yading@10: yading@10: float frac; ///< threshold of changed pixels over the total fraction yading@10: yading@10: int max_drop_count; ///< if positive: maximum number of sequential frames to drop yading@10: ///< if negative: minimum number of frames between two drops yading@10: yading@10: int drop_count; ///< if positive: number of frames sequentially dropped yading@10: ///< if negative: number of sequential frames which were not dropped yading@10: yading@10: int hsub, vsub; ///< chroma subsampling values yading@10: AVFrame *ref; ///< reference picture yading@10: DSPContext dspctx; ///< context providing optimized diff routines yading@10: AVCodecContext *avctx; ///< codec context required for the DSPContext yading@10: } DecimateContext; yading@10: yading@10: #define OFFSET(x) offsetof(DecimateContext, x) yading@10: #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM yading@10: yading@10: static const AVOption mpdecimate_options[] = { yading@10: { "max", "set the maximum number of consecutive dropped frames (positive), or the minimum interval between dropped frames (negative)", yading@10: OFFSET(max_drop_count), AV_OPT_TYPE_INT, {.i64=0}, INT_MIN, INT_MAX, FLAGS }, yading@10: { "hi", "set high dropping threshold", OFFSET(hi), AV_OPT_TYPE_INT, {.i64=64*12}, INT_MIN, INT_MAX, FLAGS }, yading@10: { "lo", "set low dropping threshold", OFFSET(lo), AV_OPT_TYPE_INT, {.i64=64*5}, INT_MIN, INT_MAX, FLAGS }, yading@10: { "frac", "set fraction dropping threshold", OFFSET(frac), AV_OPT_TYPE_FLOAT, {.dbl=0.33}, 0, 1, FLAGS }, yading@10: { NULL } yading@10: }; yading@10: yading@10: AVFILTER_DEFINE_CLASS(mpdecimate); yading@10: yading@10: /** yading@10: * Return 1 if the two planes are different, 0 otherwise. yading@10: */ yading@10: static int diff_planes(AVFilterContext *ctx, yading@10: uint8_t *cur, uint8_t *ref, int linesize, yading@10: int w, int h) yading@10: { yading@10: DecimateContext *decimate = ctx->priv; yading@10: DSPContext *dspctx = &decimate->dspctx; yading@10: yading@10: int x, y; yading@10: int d, c = 0; yading@10: int t = (w/16)*(h/16)*decimate->frac; yading@10: int16_t block[8*8]; yading@10: yading@10: /* compute difference for blocks of 8x8 bytes */ yading@10: for (y = 0; y < h-7; y += 4) { yading@10: for (x = 8; x < w-7; x += 4) { yading@10: dspctx->diff_pixels(block, yading@10: cur+x+y*linesize, yading@10: ref+x+y*linesize, linesize); yading@10: d = dspctx->sum_abs_dctelem(block); yading@10: if (d > decimate->hi) yading@10: return 1; yading@10: if (d > decimate->lo) { yading@10: c++; yading@10: if (c > t) yading@10: return 1; yading@10: } yading@10: } yading@10: } yading@10: return 0; yading@10: } yading@10: yading@10: /** yading@10: * Tell if the frame should be decimated, for example if it is no much yading@10: * different with respect to the reference frame ref. yading@10: */ yading@10: static int decimate_frame(AVFilterContext *ctx, yading@10: AVFrame *cur, AVFrame *ref) yading@10: { yading@10: DecimateContext *decimate = ctx->priv; yading@10: int plane; yading@10: yading@10: if (decimate->max_drop_count > 0 && yading@10: decimate->drop_count >= decimate->max_drop_count) yading@10: return 0; yading@10: if (decimate->max_drop_count < 0 && yading@10: (decimate->drop_count-1) > decimate->max_drop_count) yading@10: return 0; yading@10: yading@10: for (plane = 0; ref->data[plane] && ref->linesize[plane]; plane++) { yading@10: int vsub = plane == 1 || plane == 2 ? decimate->vsub : 0; yading@10: int hsub = plane == 1 || plane == 2 ? decimate->hsub : 0; yading@10: if (diff_planes(ctx, yading@10: cur->data[plane], ref->data[plane], ref->linesize[plane], yading@10: ref->width>>hsub, ref->height>>vsub)) yading@10: return 0; yading@10: } yading@10: yading@10: return 1; yading@10: } yading@10: yading@10: static av_cold int init(AVFilterContext *ctx) yading@10: { yading@10: DecimateContext *decimate = ctx->priv; yading@10: yading@10: av_log(ctx, AV_LOG_VERBOSE, "max_drop_count:%d hi:%d lo:%d frac:%f\n", yading@10: decimate->max_drop_count, decimate->hi, decimate->lo, decimate->frac); yading@10: yading@10: decimate->avctx = avcodec_alloc_context3(NULL); yading@10: if (!decimate->avctx) yading@10: return AVERROR(ENOMEM); yading@10: avpriv_dsputil_init(&decimate->dspctx, decimate->avctx); yading@10: yading@10: return 0; yading@10: } yading@10: yading@10: static av_cold void uninit(AVFilterContext *ctx) yading@10: { yading@10: DecimateContext *decimate = ctx->priv; yading@10: av_frame_free(&decimate->ref); yading@10: if (decimate->avctx) { yading@10: avcodec_close(decimate->avctx); yading@10: av_freep(&decimate->avctx); yading@10: } yading@10: } yading@10: yading@10: static int query_formats(AVFilterContext *ctx) yading@10: { yading@10: static const enum AVPixelFormat pix_fmts[] = { yading@10: AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, yading@10: AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV411P, yading@10: AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV440P, yading@10: AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ422P, yading@10: AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ440P, yading@10: AV_PIX_FMT_YUVA420P, yading@10: AV_PIX_FMT_NONE yading@10: }; yading@10: yading@10: ff_set_common_formats(ctx, ff_make_format_list(pix_fmts)); yading@10: yading@10: return 0; yading@10: } yading@10: yading@10: static int config_input(AVFilterLink *inlink) yading@10: { yading@10: AVFilterContext *ctx = inlink->dst; yading@10: DecimateContext *decimate = ctx->priv; yading@10: const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format); yading@10: decimate->hsub = pix_desc->log2_chroma_w; yading@10: decimate->vsub = pix_desc->log2_chroma_h; yading@10: yading@10: return 0; yading@10: } yading@10: yading@10: static int filter_frame(AVFilterLink *inlink, AVFrame *cur) yading@10: { yading@10: DecimateContext *decimate = inlink->dst->priv; yading@10: AVFilterLink *outlink = inlink->dst->outputs[0]; yading@10: int ret; yading@10: yading@10: if (decimate->ref && decimate_frame(inlink->dst, cur, decimate->ref)) { yading@10: decimate->drop_count = FFMAX(1, decimate->drop_count+1); yading@10: } else { yading@10: av_frame_free(&decimate->ref); yading@10: decimate->ref = cur; yading@10: decimate->drop_count = FFMIN(-1, decimate->drop_count-1); yading@10: yading@10: if (ret = ff_filter_frame(outlink, av_frame_clone(cur)) < 0) yading@10: return ret; yading@10: } yading@10: yading@10: av_log(inlink->dst, AV_LOG_DEBUG, yading@10: "%s pts:%s pts_time:%s drop_count:%d\n", yading@10: decimate->drop_count > 0 ? "drop" : "keep", yading@10: av_ts2str(cur->pts), av_ts2timestr(cur->pts, &inlink->time_base), yading@10: decimate->drop_count); yading@10: yading@10: if (decimate->drop_count > 0) yading@10: av_frame_free(&cur); yading@10: yading@10: return 0; yading@10: } yading@10: yading@10: static int request_frame(AVFilterLink *outlink) yading@10: { yading@10: DecimateContext *decimate = outlink->src->priv; yading@10: AVFilterLink *inlink = outlink->src->inputs[0]; yading@10: int ret; yading@10: yading@10: do { yading@10: ret = ff_request_frame(inlink); yading@10: } while (decimate->drop_count > 0 && ret >= 0); yading@10: yading@10: return ret; yading@10: } yading@10: yading@10: static const AVFilterPad mpdecimate_inputs[] = { yading@10: { yading@10: .name = "default", yading@10: .type = AVMEDIA_TYPE_VIDEO, yading@10: .get_video_buffer = ff_null_get_video_buffer, yading@10: .config_props = config_input, yading@10: .filter_frame = filter_frame, yading@10: }, yading@10: { NULL } yading@10: }; yading@10: yading@10: static const AVFilterPad mpdecimate_outputs[] = { yading@10: { yading@10: .name = "default", yading@10: .type = AVMEDIA_TYPE_VIDEO, yading@10: .request_frame = request_frame, yading@10: }, yading@10: { NULL } yading@10: }; yading@10: yading@10: AVFilter avfilter_vf_mpdecimate = { yading@10: .name = "mpdecimate", yading@10: .description = NULL_IF_CONFIG_SMALL("Remove near-duplicate frames."), yading@10: .init = init, yading@10: .uninit = uninit, yading@10: yading@10: .priv_size = sizeof(DecimateContext), yading@10: .query_formats = query_formats, yading@10: .inputs = mpdecimate_inputs, yading@10: .outputs = mpdecimate_outputs, yading@10: .priv_class = &mpdecimate_class, yading@10: };