[FFmpeg-devel] [PATCH] lavfi/select: add scene detection.
Clément Bœsch
ubitux at gmail.com
Sun Jun 3 00:48:57 CEST 2012
On Sun, Jun 03, 2012 at 12:07:09AM +0200, Clément Bœsch wrote:
> On Sat, May 26, 2012 at 12:41:01PM +0200, Clément Bœsch wrote:
> > eg: ffmpeg -i ~/samples/GoneNutty.avi -vf scale=160:120,select='gt(scene_score\,60)*gt(scene_diff\,60)',tile -frames:v 1 out.png
> >
> > TODO: documentation
> >
> > Based on the shotdetect algorithm.
> > ---
> > libavfilter/vf_select.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++
> > 1 file changed, 63 insertions(+)
> >
>
> New version attached.
>
> Here are some random results with the threshold values given in the
> documentation: http://imgur.com/a/icpfk
>
> I'm not fond of the 2 threshold values the user need to specify, but I
> don't see any simpler solution (except having a variable set to 0 or 1,
> but with no threshold configuration possible).
>
Solved that issue with the attached patch.
[...]
--
Clément B.
-------------- next part --------------
From 143fae0acf9d32ef87ec0960d09dc2f96fc619cc Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Cl=C3=A9ment=20B=C5=93sch?= <ubitux at gmail.com>
Date: Sat, 26 May 2012 12:38:59 +0200
Subject: [PATCH] lavfi/select: add scene detection.
Based on the shotdetect algorithm (http://shotdetect.nonutc.fr/ by Johan
MATHE johan.mathe tremplin-utc.net).
---
configure | 1 +
doc/filters.texi | 11 +++++++
libavfilter/vf_select.c | 75 +++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 87 insertions(+)
diff --git a/configure b/configure
index de56044..9ae7127 100755
--- a/configure
+++ b/configure
@@ -1698,6 +1698,7 @@ ocv_filter_deps="libopencv"
pan_filter_deps="swresample"
removelogo_filter_deps="avcodec avformat swscale"
scale_filter_deps="swscale"
+select_filter_deps="avcodec"
super2xsai_filter_deps="gpl"
tinterlace_filter_deps="gpl"
yadif_filter_deps="gpl"
diff --git a/doc/filters.texi b/doc/filters.texi
index 324a154..c066194 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -2603,6 +2603,11 @@ the frame is bottom-field-first
@item pos
the position in the file of the filtered frame, -1 if the information
is not available (e.g. for synthetic video)
+
+ at item scene
+Threshold value between 0 and 1 for scene detection (see examples), a lower
+value is more tolerant, and a higher less tolerant
+
@end table
The default value of the select expression is "1".
@@ -2635,6 +2640,12 @@ select='gte(t\,10)*lte(t\,20)*eq(pict_type\,I)'
select='isnan(prev_selected_t)+gte(t-prev_selected_t\,10)'
@end example
+Complete example to create a mosaic of the first scenes:
+
+ at example
+ffmpeg -i video.avi -vf select='gt(scene\,0.16)',scale=160:120,tile -frames:v 1 preview.png
+ at end example
+
@section setdar, setsar
The @code{setdar} filter sets the Display Aspect Ratio for the filter
diff --git a/libavfilter/vf_select.c b/libavfilter/vf_select.c
index d3f649c..f823ac4 100644
--- a/libavfilter/vf_select.c
+++ b/libavfilter/vf_select.c
@@ -25,7 +25,9 @@
#include "libavutil/eval.h"
#include "libavutil/fifo.h"
+#include "libavcodec/dsputil.h"
#include "avfilter.h"
+#include "formats.h"
#include "video.h"
static const char *const var_names[] = {
@@ -62,6 +64,8 @@ static const char *const var_names[] = {
"key", ///< tell if the frame is a key frame
"pos", ///< original position in the file of the frame
+ "scene",
+
NULL
};
@@ -99,6 +103,8 @@ enum var_name {
VAR_KEY,
VAR_POS,
+ VAR_SCENE,
+
VAR_VARS_NB
};
@@ -107,6 +113,11 @@ enum var_name {
typedef struct {
AVExpr *expr;
double var_values[VAR_VARS_NB];
+ int do_scene_detect; ///< 1 if the expression requires scene detection variables, 0 otherwise
+ AVCodecContext *avctx; ///< codec context required for the DSPContext (scene detect only)
+ DSPContext c; ///< context providing optimized SAD methods (scene detect only)
+ int64_t prev_mafd; ///< previous MAFD (scene detect only)
+ AVFilterBufferRef *prev_picref; ///< previous frame (scene detect only)
double select;
int cache_frames;
AVFifoBuffer *pending_frames; ///< FIFO buffer of video frames
@@ -128,6 +139,8 @@ static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
av_log(ctx, AV_LOG_ERROR, "Failed to allocate pending frames buffer.\n");
return AVERROR(ENOMEM);
}
+
+ select->do_scene_detect = args && strstr(args, "scene");
return 0;
}
@@ -160,9 +173,46 @@ static int config_input(AVFilterLink *inlink)
select->var_values[VAR_INTERLACE_TYPE_T] = INTERLACE_TYPE_T;
select->var_values[VAR_INTERLACE_TYPE_B] = INTERLACE_TYPE_B;
+ if (select->do_scene_detect) {
+ select->avctx = avcodec_alloc_context3(NULL);
+ if (!select->avctx)
+ return AVERROR(ENOMEM);
+ ff_dsputil_init(&select->c, select->avctx);
+ }
return 0;
}
+static void set_scene_values(AVFilterContext *ctx, AVFilterBufferRef *picref)
+{
+ SelectContext *select = ctx->priv;
+ AVFilterBufferRef *prev_picref = select->prev_picref;
+
+ if (prev_picref &&
+ picref->video->h == prev_picref->video->h &&
+ picref->video->w == prev_picref->video->w &&
+ picref->linesize[0] == prev_picref->linesize[0]) {
+ int x, y;
+ int64_t mafd, diff;
+ uint8_t *p1 = picref->data[0];
+ uint8_t *p2 = prev_picref->data[0];
+ const int linesize = picref->linesize[0];
+
+ for (mafd = y = 0; y < picref->video->h; y += 8)
+ for (x = 0; x < linesize; x += 8)
+ mafd += select->c.sad[1](select,
+ p1 + y * linesize + x,
+ p2 + y * linesize + x,
+ linesize, 8);
+ emms_c();
+ mafd /= picref->video->h * picref->video->w * 3;
+ diff = llabs(mafd - select->prev_mafd);
+ select->var_values[VAR_SCENE] = FFMIN(mafd, diff) / 255.;
+ select->prev_mafd = mafd;
+ avfilter_unref_buffer(prev_picref);
+ }
+ select->prev_picref = avfilter_ref_buffer(picref, ~0);
+}
+
#define D2TS(d) (isnan(d) ? AV_NOPTS_VALUE : (int64_t)(d))
#define TS2D(ts) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts))
@@ -172,6 +222,8 @@ static int select_frame(AVFilterContext *ctx, AVFilterBufferRef *picref)
AVFilterLink *inlink = ctx->inputs[0];
double res;
+ if (select->do_scene_detect)
+ set_scene_values(ctx, picref);
if (isnan(select->var_values[VAR_START_PTS]))
select->var_values[VAR_START_PTS] = TS2D(picref->pts);
if (isnan(select->var_values[VAR_START_T]))
@@ -315,6 +367,28 @@ static av_cold void uninit(AVFilterContext *ctx)
avfilter_unref_buffer(picref);
av_fifo_free(select->pending_frames);
select->pending_frames = NULL;
+
+ if (select->do_scene_detect) {
+ avfilter_unref_bufferp(&select->prev_picref);
+ avcodec_close(select->avctx);
+ av_freep(&select->avctx);
+ }
+}
+
+static int query_formats(AVFilterContext *ctx)
+{
+ SelectContext *select = ctx->priv;
+
+ if (!select->do_scene_detect) {
+ return ff_default_query_formats(ctx);
+ } else {
+ static const enum PixelFormat pix_fmts[] = {
+ PIX_FMT_RGB24, PIX_FMT_BGR24,
+ PIX_FMT_NONE
+ };
+ avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
+ }
+ return 0;
}
AVFilter avfilter_vf_select = {
@@ -322,6 +396,7 @@ AVFilter avfilter_vf_select = {
.description = NULL_IF_CONFIG_SMALL("Select frames to pass in output."),
.init = init,
.uninit = uninit,
+ .query_formats = query_formats,
.priv_size = sizeof(SelectContext),
--
1.7.10.2
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 490 bytes
Desc: not available
URL: <http://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20120603/89b7c5c2/attachment.asc>
More information about the ffmpeg-devel
mailing list