]> git.sesse.net Git - ffmpeg/blobdiff - libavfilter/vf_select.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavfilter / vf_select.c
index d3f649cbffcbbd9fd8dd4b5b91e42475fbb6e27e..4d19b33913acf6b4ba9622b579e3bc0389798b39 100644 (file)
 
 #include "libavutil/eval.h"
 #include "libavutil/fifo.h"
+#include "libavcodec/dsputil.h"
 #include "avfilter.h"
+#include "formats.h"
+#include "internal.h"
 #include "video.h"
 
 static const char *const var_names[] = {
@@ -62,6 +65,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 +104,8 @@ enum var_name {
     VAR_KEY,
     VAR_POS,
 
+    VAR_SCENE,
+
     VAR_VARS_NB
 };
 
@@ -107,12 +114,17 @@ 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)
+    double 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
 } SelectContext;
 
-static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
+static av_cold int init(AVFilterContext *ctx, const char *args)
 {
     SelectContext *select = ctx->priv;
     int ret;
@@ -128,6 +140,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 +174,49 @@ 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);
+        dsputil_init(&select->c, select->avctx);
+    }
     return 0;
 }
 
+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;
+}
+
 #define D2TS(d)  (isnan(d) ? AV_NOPTS_VALUE : (int64_t)(d))
 #define TS2D(ts) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts))
 
@@ -172,6 +226,8 @@ static int select_frame(AVFilterContext *ctx, AVFilterBufferRef *picref)
     AVFilterLink *inlink = ctx->inputs[0];
     double res;
 
+    if (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]))
@@ -213,12 +269,13 @@ static int select_frame(AVFilterContext *ctx, AVFilterBufferRef *picref)
     return res;
 }
 
-static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
+static int start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
 {
     SelectContext *select = inlink->dst->priv;
 
     select->select = select_frame(inlink->dst, picref);
     if (select->select) {
+        AVFilterBufferRef *buf_out;
         /* frame was requested through poll_frame */
         if (select->cache_frames) {
             if (!av_fifo_space(select->pending_frames))
@@ -227,31 +284,36 @@ static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
             else
                 av_fifo_generic_write(select->pending_frames, &picref,
                                       sizeof(picref), NULL);
-            return;
+            return 0;
         }
-        avfilter_start_frame(inlink->dst->outputs[0], avfilter_ref_buffer(picref, ~0));
+        buf_out = avfilter_ref_buffer(picref, ~0);
+        if (!buf_out)
+            return AVERROR(ENOMEM);
+        return ff_start_frame(inlink->dst->outputs[0], buf_out);
     }
+
+    return 0;
 }
 
-static void draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
+static int draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
 {
     SelectContext *select = inlink->dst->priv;
 
     if (select->select && !select->cache_frames)
-        avfilter_draw_slice(inlink->dst->outputs[0], y, h, slice_dir);
+        return ff_draw_slice(inlink->dst->outputs[0], y, h, slice_dir);
+    return 0;
 }
 
-static void end_frame(AVFilterLink *inlink)
+static int end_frame(AVFilterLink *inlink)
 {
     SelectContext *select = inlink->dst->priv;
-    AVFilterBufferRef *picref = inlink->cur_buf;
 
     if (select->select) {
         if (select->cache_frames)
-            return;
-        avfilter_end_frame(inlink->dst->outputs[0]);
+            return 0;
+        return ff_end_frame(inlink->dst->outputs[0]);
     }
-    avfilter_unref_buffer(picref);
+    return 0;
 }
 
 static int request_frame(AVFilterLink *outlink)
@@ -263,16 +325,18 @@ static int request_frame(AVFilterLink *outlink)
 
     if (av_fifo_size(select->pending_frames)) {
         AVFilterBufferRef *picref;
+        int ret;
+
         av_fifo_generic_read(select->pending_frames, &picref, sizeof(picref), NULL);
-        avfilter_start_frame(outlink, avfilter_ref_buffer(picref, ~0));
-        avfilter_draw_slice(outlink, 0, outlink->h, 1);
-        avfilter_end_frame(outlink);
-        avfilter_unref_buffer(picref);
-        return 0;
+        if ((ret = ff_start_frame(outlink, picref)) < 0 ||
+            (ret = ff_draw_slice(outlink, 0, outlink->h, 1)) < 0 ||
+            (ret = ff_end_frame(outlink)) < 0);
+
+        return ret;
     }
 
     while (!select->select) {
-        int ret = avfilter_request_frame(inlink);
+        int ret = ff_request_frame(inlink);
         if (ret < 0)
             return ret;
     }
@@ -287,12 +351,12 @@ static int poll_frame(AVFilterLink *outlink)
     int count, ret;
 
     if (!av_fifo_size(select->pending_frames)) {
-        if ((count = avfilter_poll_frame(inlink)) <= 0)
+        if ((count = ff_poll_frame(inlink)) <= 0)
             return count;
         /* request frame from input, and apply select condition to it */
         select->cache_frames = 1;
         while (count-- && av_fifo_space(select->pending_frames)) {
-            ret = avfilter_request_frame(inlink);
+            ret = ff_request_frame(inlink);
             if (ret < 0)
                 break;
         }
@@ -315,6 +379,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
+        };
+        ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
+    }
+    return 0;
 }
 
 AVFilter avfilter_vf_select = {
@@ -322,20 +408,21 @@ 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),
 
-    .inputs    = (const AVFilterPad[]) {{ .name       = "default",
-                                    .type             = AVMEDIA_TYPE_VIDEO,
-                                    .get_video_buffer = ff_null_get_video_buffer,
-                                    .config_props     = config_input,
-                                    .start_frame      = start_frame,
-                                    .draw_slice       = draw_slice,
-                                    .end_frame        = end_frame },
-                                  { .name = NULL }},
-    .outputs   = (const AVFilterPad[]) {{ .name       = "default",
-                                    .type             = AVMEDIA_TYPE_VIDEO,
-                                    .poll_frame       = poll_frame,
-                                    .request_frame    = request_frame, },
-                                  { .name = NULL}},
+    .inputs    = (const AVFilterPad[]) {{ .name             = "default",
+                                          .type             = AVMEDIA_TYPE_VIDEO,
+                                          .get_video_buffer = ff_null_get_video_buffer,
+                                          .config_props     = config_input,
+                                          .start_frame      = start_frame,
+                                          .draw_slice       = draw_slice,
+                                          .end_frame        = end_frame },
+                                        { .name = NULL }},
+    .outputs   = (const AVFilterPad[]) {{ .name             = "default",
+                                          .type             = AVMEDIA_TYPE_VIDEO,
+                                          .poll_frame       = poll_frame,
+                                          .request_frame    = request_frame, },
+                                        { .name = NULL}},
 };