summaryrefslogtreecommitdiffstats
path: root/libavfilter/vf_select.c
diff options
context:
space:
mode:
Diffstat (limited to 'libavfilter/vf_select.c')
-rw-r--r--libavfilter/vf_select.c115
1 files changed, 97 insertions, 18 deletions
diff --git a/libavfilter/vf_select.c b/libavfilter/vf_select.c
index 25c6a14..143e8cc 100644
--- a/libavfilter/vf_select.c
+++ b/libavfilter/vf_select.c
@@ -1,20 +1,20 @@
/*
* Copyright (c) 2011 Stefano Sabatini
*
- * This file is part of Libav.
+ * This file is part of FFmpeg.
*
- * Libav is free software; you can redistribute it and/or
+ * FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
- * Libav is distributed in the hope that it will be useful,
+ * FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
- * License along with Libav; if not, write to the Free Software
+ * License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
@@ -26,16 +26,16 @@
#include "libavutil/eval.h"
#include "libavutil/fifo.h"
#include "libavutil/internal.h"
-#include "libavutil/mathematics.h"
#include "avfilter.h"
+#include "formats.h"
#include "internal.h"
#include "video.h"
-static const char *const var_names[] = {
- "E", ///< Euler number
- "PHI", ///< golden ratio
- "PI", ///< greek pi
+#if CONFIG_AVCODEC
+#include "libavcodec/dsputil.h"
+#endif
+static const char *const var_names[] = {
"TB", ///< timebase
"pts", ///< original pts in the file of the frame
@@ -69,14 +69,12 @@ 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
};
enum var_name {
- VAR_E,
- VAR_PHI,
- VAR_PI,
-
VAR_TB,
VAR_PTS,
@@ -110,6 +108,8 @@ enum var_name {
VAR_KEY,
VAR_POS,
+ VAR_SCENE,
+
VAR_VARS_NB
};
@@ -118,6 +118,13 @@ 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
+#if CONFIG_AVCODEC
+ AVCodecContext *avctx; ///< codec context required for the DSPContext (scene detect only)
+ DSPContext c; ///< context providing optimized SAD methods (scene detect only)
+ double prev_mafd; ///< previous MAFD (scene detect only)
+#endif
+ AVFilterBufferRef *prev_picref; ///< previous frame (scene detect only)
double select;
int cache_frames;
AVFifoBuffer *pending_frames; ///< FIFO buffer of video frames
@@ -139,6 +146,12 @@ static av_cold int init(AVFilterContext *ctx, const char *args)
av_log(ctx, AV_LOG_ERROR, "Failed to allocate pending frames buffer.\n");
return AVERROR(ENOMEM);
}
+
+ select->do_scene_detect = args && strstr(args, "scene");
+ if (select->do_scene_detect && !CONFIG_AVCODEC) {
+ av_log(ctx, AV_LOG_ERROR, "Scene detection is not available without libavcodec.\n");
+ return AVERROR(EINVAL);
+ }
return 0;
}
@@ -150,10 +163,6 @@ static int config_input(AVFilterLink *inlink)
{
SelectContext *select = inlink->dst->priv;
- select->var_values[VAR_E] = M_E;
- select->var_values[VAR_PHI] = M_PHI;
- select->var_values[VAR_PI] = M_PI;
-
select->var_values[VAR_N] = 0.0;
select->var_values[VAR_SELECTED_N] = 0.0;
@@ -173,11 +182,53 @@ static int config_input(AVFilterLink *inlink)
select->var_values[VAR_INTERLACE_TYPE_P] = INTERLACE_TYPE_P;
select->var_values[VAR_INTERLACE_TYPE_T] = INTERLACE_TYPE_T;
- select->var_values[VAR_INTERLACE_TYPE_B] = INTERLACE_TYPE_B;;
+ select->var_values[VAR_INTERLACE_TYPE_B] = INTERLACE_TYPE_B;
+ if (CONFIG_AVCODEC && select->do_scene_detect) {
+ select->avctx = avcodec_alloc_context3(NULL);
+ if (!select->avctx)
+ return AVERROR(ENOMEM);
+ dsputil_init(&select->c, select->avctx);
+ }
return 0;
}
+#if CONFIG_AVCODEC
+static double get_scene_score(AVFilterContext *ctx, AVFilterBufferRef *picref)
+{
+ double ret = 0;
+ 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 sad;
+ double mafd, diff;
+ uint8_t *p1 = picref->data[0];
+ uint8_t *p2 = prev_picref->data[0];
+ const int linesize = picref->linesize[0];
+
+ for (sad = y = 0; y < picref->video->h; y += 8)
+ for (x = 0; x < linesize; x += 8)
+ sad += select->c.sad[1](select,
+ p1 + y * linesize + x,
+ p2 + y * linesize + x,
+ linesize, 8);
+ emms_c();
+ mafd = sad / (picref->video->h * picref->video->w * 3);
+ diff = fabs(mafd - select->prev_mafd);
+ ret = av_clipf(FFMIN(mafd, diff) / 100., 0, 1);
+ select->prev_mafd = mafd;
+ avfilter_unref_buffer(prev_picref);
+ }
+ select->prev_picref = avfilter_ref_buffer(picref, ~0);
+ return ret;
+}
+#endif
+
#define D2TS(d) (isnan(d) ? AV_NOPTS_VALUE : (int64_t)(d))
#define TS2D(ts) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts))
@@ -187,6 +238,8 @@ static int select_frame(AVFilterContext *ctx, AVFilterBufferRef *picref)
AVFilterLink *inlink = ctx->inputs[0];
double res;
+ if (CONFIG_AVCODEC && select->do_scene_detect)
+ select->var_values[VAR_SCENE] = get_scene_score(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]))
@@ -338,6 +391,30 @@ 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);
+ if (select->avctx) {
+ 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 AVPixelFormat pix_fmts[] = {
+ AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
+ AV_PIX_FMT_NONE
+ };
+ ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
+ }
+ return 0;
}
static const AVFilterPad avfilter_vf_select_inputs[] = {
@@ -345,6 +422,7 @@ static const AVFilterPad avfilter_vf_select_inputs[] = {
.name = "default",
.type = AVMEDIA_TYPE_VIDEO,
.get_video_buffer = ff_null_get_video_buffer,
+ .min_perms = AV_PERM_PRESERVE,
.config_props = config_input,
.start_frame = start_frame,
.draw_slice = draw_slice,
@@ -368,6 +446,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),
OpenPOWER on IntegriCloud