]> git.sesse.net Git - ffmpeg/blobdiff - libavfilter/vf_thumbnail.c
Merge commit '18e6f087c4a50bede8449ee164778945480be50c'
[ffmpeg] / libavfilter / vf_thumbnail.c
index 5936a1936dbf1843e4f6d007eb0548de79b4df60..0d245d9dddbbce1008e567b28f53ca67b7c4c873 100644 (file)
@@ -68,27 +68,6 @@ static av_cold int init(AVFilterContext *ctx, const char *args)
     return 0;
 }
 
-static int draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
-{
-    int i, j;
-    AVFilterContext *ctx = inlink->dst;
-    ThumbContext *thumb = ctx->priv;
-    int *hist = thumb->frames[thumb->n].histogram;
-    AVFilterBufferRef *picref = inlink->cur_buf;
-    const uint8_t *p = picref->data[0] + y * picref->linesize[0];
-
-    // update current frame RGB histogram
-    for (j = 0; j < h; j++) {
-        for (i = 0; i < inlink->w; i++) {
-            hist[0*256 + p[i*3    ]]++;
-            hist[1*256 + p[i*3 + 1]]++;
-            hist[2*256 + p[i*3 + 2]]++;
-        }
-        p += picref->linesize[0];
-    }
-    return 0;
-}
-
 /**
  * @brief        Compute Sum-square deviation to estimate "closeness".
  * @param hist   color distribution histogram
@@ -107,18 +86,29 @@ static double frame_sum_square_err(const int *hist, const double *median)
     return sum_sq_err;
 }
 
-static int  end_frame(AVFilterLink *inlink)
+static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *frame)
 {
     int i, j, best_frame_idx = 0;
     double avg_hist[HIST_SIZE] = {0}, sq_err, min_sq_err = -1;
-    AVFilterLink *outlink = inlink->dst->outputs[0];
-    ThumbContext *thumb   = inlink->dst->priv;
     AVFilterContext *ctx  = inlink->dst;
+    ThumbContext *thumb   = ctx->priv;
+    AVFilterLink *outlink = ctx->outputs[0];
     AVFilterBufferRef *picref;
+    int *hist = thumb->frames[thumb->n].histogram;
+    const uint8_t *p = frame->data[0];
 
     // keep a reference of each frame
-    thumb->frames[thumb->n].buf = inlink->cur_buf;
-    inlink->cur_buf = NULL;
+    thumb->frames[thumb->n].buf = frame;
+
+    // update current frame RGB histogram
+    for (j = 0; j < inlink->h; j++) {
+        for (i = 0; i < inlink->w; i++) {
+            hist[0*256 + p[i*3    ]]++;
+            hist[1*256 + p[i*3 + 1]]++;
+            hist[2*256 + p[i*3 + 2]]++;
+        }
+        p += frame->linesize[0];
+    }
 
     // no selection until the buffer of N frames is filled up
     if (thumb->n < thumb->n_frames - 1) {
@@ -145,8 +135,7 @@ static int  end_frame(AVFilterLink *inlink)
         memset(thumb->frames[i].histogram, 0, sizeof(thumb->frames[i].histogram));
         if (i == best_frame_idx)
             continue;
-        avfilter_unref_buffer(thumb->frames[i].buf);
-        thumb->frames[i].buf = NULL;
+        avfilter_unref_bufferp(&thumb->frames[i].buf);
     }
     thumb->n = 0;
 
@@ -154,25 +143,19 @@ static int  end_frame(AVFilterLink *inlink)
     picref = thumb->frames[best_frame_idx].buf;
     av_log(ctx, AV_LOG_INFO, "frame id #%d (pts_time=%f) selected\n",
            best_frame_idx, picref->pts * av_q2d(inlink->time_base));
-    ff_start_frame(outlink, picref);
     thumb->frames[best_frame_idx].buf = NULL;
-    ff_draw_slice(outlink, 0, inlink->h, 1);
-    return ff_end_frame(outlink);
+    return ff_filter_frame(outlink, picref);
 }
 
 static av_cold void uninit(AVFilterContext *ctx)
 {
     int i;
     ThumbContext *thumb = ctx->priv;
-    for (i = 0; i < thumb->n_frames && thumb->frames[i].buf; i++) {
-        avfilter_unref_buffer(thumb->frames[i].buf);
-        thumb->frames[i].buf = NULL;
-    }
+    for (i = 0; i < thumb->n_frames && thumb->frames[i].buf; i++)
+        avfilter_unref_bufferp(&thumb->frames[i].buf);
     av_freep(&thumb->frames);
 }
 
-static int null_start_frame(AVFilterLink *link, AVFilterBufferRef *picref) { return 0; }
-
 static int request_frame(AVFilterLink *link)
 {
     ThumbContext *thumb = link->src->priv;
@@ -219,6 +202,27 @@ static int query_formats(AVFilterContext *ctx)
     return 0;
 }
 
+static const AVFilterPad thumbnail_inputs[] = {
+    {
+        .name             = "default",
+        .type             = AVMEDIA_TYPE_VIDEO,
+        .get_video_buffer = ff_null_get_video_buffer,
+        .min_perms        = AV_PERM_PRESERVE,
+        .filter_frame     = filter_frame,
+    },
+    { NULL }
+};
+
+static const AVFilterPad thumbnail_outputs[] = {
+    {
+        .name          = "default",
+        .type          = AVMEDIA_TYPE_VIDEO,
+        .request_frame = request_frame,
+        .poll_frame    = poll_frame,
+    },
+    { NULL }
+};
+
 AVFilter avfilter_vf_thumbnail = {
     .name          = "thumbnail",
     .description   = NULL_IF_CONFIG_SMALL("Select the most representative frame in a given sequence of consecutive frames."),
@@ -226,21 +230,6 @@ AVFilter avfilter_vf_thumbnail = {
     .init          = init,
     .uninit        = uninit,
     .query_formats = query_formats,
-    .inputs        = (const AVFilterPad[]) {
-        {   .name             = "default",
-            .type             = AVMEDIA_TYPE_VIDEO,
-            .get_video_buffer = ff_null_get_video_buffer,
-            .min_perms        = AV_PERM_PRESERVE,
-            .start_frame      = null_start_frame,
-            .draw_slice       = draw_slice,
-            .end_frame        = end_frame,
-        },{ .name = NULL }
-    },
-    .outputs       = (const AVFilterPad[]) {
-        {   .name             = "default",
-            .type             = AVMEDIA_TYPE_VIDEO,
-            .request_frame    = request_frame,
-            .poll_frame       = poll_frame,
-        },{ .name = NULL }
-    },
+    .inputs        = thumbnail_inputs,
+    .outputs       = thumbnail_outputs,
 };