[FFmpeg-devel] [PATCH] lavfi: add thumb video filter.
ubitux
ubitux at gmail.com
Tue Dec 27 11:10:09 CET 2011
On Sat, Dec 24, 2011 at 11:39 AM, Stefano Sabatini <stefasab at gmail.com> wrote:
> On date Tuesday 2011-12-20 16:33:44 +0100, Clément Bœsch encoded:
>> On Tue, Dec 20, 2011 at 03:11:13PM +0100, Stefano Sabatini wrote:
> [...]
>> Date: Mon, 24 Oct 2011 17:11:10 +0200
>> Subject: [PATCH] lavfi: add thumbnail video filter.
>>
>> ---
>> Changelog | 1 +
>> doc/filters.texi | 20 ++++
>> libavfilter/Makefile | 1 +
>> libavfilter/allfilters.c | 1 +
>> libavfilter/avfilter.h | 2 +-
>> libavfilter/vf_thumbnail.c | 238 ++++++++++++++++++++++++++++++++++++++++++++
>> 6 files changed, 262 insertions(+), 1 deletions(-)
>> create mode 100644 libavfilter/vf_thumbnail.c
>>
>> diff --git a/Changelog b/Changelog
>> index ad7fa8d..590752b 100644
>> --- a/Changelog
>> +++ b/Changelog
>> @@ -139,6 +139,7 @@ easier to use. The changes are:
>> - SBaGen (SBG) binaural beats script demuxer
>> - OpenMG Audio muxer
>> - Simple segmenting muxer
>> +- Thumbnails support (see thumbnail video filter)
>
> Nit++: I tend to prefer the simpler "thumbnail video filter", but then
> it is just me
>
Changed.
>> version 0.8:
>> diff --git a/doc/filters.texi b/doc/filters.texi
>> index 699e0c1..37fd9cd 100644
>> --- a/doc/filters.texi
>> +++ b/doc/filters.texi
>> @@ -2400,6 +2400,26 @@ For example:
>> will create two separate outputs from the same input, one cropped and
>> one padded.
>>
>> + at section thumbnail
>> +Select the most representative frame in a given sequence of consecutive frames.
>> +
>> +It accepts as argument the frames batch size to analyze (default @var{N}=100);
>> +in a set of @var{N} frames, the filter will pick one of them, and then handle
>> +the next batch of @var{N} frames until the end.
>> +
>> +Since the filter keeps track of the whole frames sequence, a bigger @var{N}
>> +value will result in a higher memory usage, so a high value is not recommended.
>> +
>> +The following example extract one picture each 50 frames:
>> + at example
>> +thumbnail=50
>> + at end example
>> +
>> +Complete example of a thumbnail creation with @command{ffmpeg}:
>> + at example
>> +ffmpeg -i in.avi -vf thumbnail,scale=300:200 -frames:v 1 out.png
>> + at end example
>> +
>> @section transpose
>>
>> Transpose rows with columns in the input video and optionally flip it.
>> diff --git a/libavfilter/Makefile b/libavfilter/Makefile
>> index a6fd18f..758ebc7 100644
>> --- a/libavfilter/Makefile
>> +++ b/libavfilter/Makefile
>> @@ -78,6 +78,7 @@ OBJS-$(CONFIG_SETTB_FILTER) += vf_settb.o
>> OBJS-$(CONFIG_SHOWINFO_FILTER) += vf_showinfo.o
>> OBJS-$(CONFIG_SLICIFY_FILTER) += vf_slicify.o
>> OBJS-$(CONFIG_SPLIT_FILTER) += vf_split.o
>> +OBJS-$(CONFIG_THUMBNAIL_FILTER) += vf_thumbnail.o
>> OBJS-$(CONFIG_TRANSPOSE_FILTER) += vf_transpose.o
>> OBJS-$(CONFIG_UNSHARP_FILTER) += vf_unsharp.o
>> OBJS-$(CONFIG_VFLIP_FILTER) += vf_vflip.o
>> diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
>> index fb50a3a..71b0381 100644
>> --- a/libavfilter/allfilters.c
>> +++ b/libavfilter/allfilters.c
>> @@ -89,6 +89,7 @@ void avfilter_register_all(void)
>> REGISTER_FILTER (SHOWINFO, showinfo, vf);
>> REGISTER_FILTER (SLICIFY, slicify, vf);
>> REGISTER_FILTER (SPLIT, split, vf);
>> + REGISTER_FILTER (THUMBNAIL, thumbnail, vf);
>> REGISTER_FILTER (TRANSPOSE, transpose, vf);
>> REGISTER_FILTER (UNSHARP, unsharp, vf);
>> REGISTER_FILTER (VFLIP, vflip, vf);
>> diff --git a/libavfilter/avfilter.h b/libavfilter/avfilter.h
>> index 4aceebd..c827458 100644
>> --- a/libavfilter/avfilter.h
>> +++ b/libavfilter/avfilter.h
>> @@ -29,7 +29,7 @@
>> #include "libavutil/rational.h"
>>
>> #define LIBAVFILTER_VERSION_MAJOR 2
>> -#define LIBAVFILTER_VERSION_MINOR 53
>> +#define LIBAVFILTER_VERSION_MINOR 54
>> #define LIBAVFILTER_VERSION_MICRO 0
>>
>> #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \
>> diff --git a/libavfilter/vf_thumbnail.c b/libavfilter/vf_thumbnail.c
>> new file mode 100644
>> index 0000000..462b114
>> --- /dev/null
>> +++ b/libavfilter/vf_thumbnail.c
>> @@ -0,0 +1,238 @@
>> +/*
>> + * Copyright (c) 2011 Smartjog S.A.S, Clément Bœsch <clement.boesch at smartjog.com>
>> + *
>> + * This file is part of FFmpeg.
>> + *
>> + * 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.
>> + *
>> + * 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 FFmpeg; if not, write to the Free Software
>> + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
>> + */
>> +
>> +/**
>> + * @file
>> + * Potential thumbnail lookup filter to reduce the risk of an inappropriate
>> + * selection (such as a black frame) we could get with an absolute seek.
>> + *
>> + * Simplified version of algorithm by Vadim Zaliva <lord at crocodile.org>.
>> + * @url http://notbrainsurgery.livejournal.com/29773.html
>> + */
>> +
>> +#include "avfilter.h"
>> +
>> +#define HIST_SIZE (3*256)
>> +
>> +struct thumb_frame {
>> + AVFilterBufferRef *buf; ///< cached frame
>> + int histogram[HIST_SIZE]; ///< RGB color distribution histogram of the frame
>> +};
>> +
>> +typedef struct {
>> + int n; ///< current frame
>> + int n_frames; ///< number of frames for analysis
>> + struct thumb_frame *frames; ///< the n_frames frames
>> +} ThumbContext;
>> +
>> +static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
>> +{
>> + ThumbContext *thumb = ctx->priv;
>> +
>> + if (args)
>> + thumb->n_frames = strtol(args, NULL, 10);
>
>> + if (thumb->n_frames < 2) {
>
> why < 2?
>
thumbnail=1 means each frame will be returned, so it's likely no the
desired usage. Also, it breaks the "frame ready" heuristic, so it
won't work with 1 frame.
>> + thumb->n_frames = 100;
>> + if (args)
>> + av_log(ctx, AV_LOG_WARNING,
>> + "Invalid number of frames specified, fallback to %d\n",
>> + thumb->n_frames);
>> + }
>
> Also I don't like very well implicit behavior, for example with "foo"
> this will result in warning and n=100, my opinion is that a
> complain+fail approach is usually less painful as it is easy to miss a
> warning.
ok, changed.
> The *tail in output from strtol should be checked, alternatively you
> may use sscanf.
>
ok, changed to sscanf
>> + thumb->frames = av_calloc(thumb->n_frames, sizeof(*thumb->frames));
>> + if (!thumb->frames) {
>> + av_log(ctx, AV_LOG_ERROR,
>> + "Allocation failure, try to lower the number of frames\n");
>> + return AVERROR(ENOMEM);
>> + }
>> + av_log(ctx, AV_LOG_INFO, "batch size: %d frames\n", thumb->n_frames);
>> + return 0;
>> +}
>> +
>> +static void 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];
>> + }
>> +}
>> +
>> +/**
>> + * @brief compute Sum-square deviation to estimate "closeness"
>
> nit++: @brief is almost never used
Almost 200 times, and I think it's appropriate here...
> nit++: Compute sum-square ... final dot.
>
ok.
>> + * @param hist color distribution histogram
>> + * @param median average color distribution histogram
>> + * @return sum of squared errors
>> + */
>> +static double frame_sum_square_err(const int *hist, const double *median)
>> +{
>> + int i;
>> + double err, sum_sq_err = 0;
>> +
>> + for (i = 0; i < HIST_SIZE; i++) {
>> + err = median[i] - (double)hist[i];
>> + sum_sq_err += err*err;
>> + }
>> + return sum_sq_err;
>> +}
>> +
>> +static void end_frame(AVFilterLink *inlink)
>> +{
>> + int i, j, best_frame = 0;
>
> Nit: best_frame -> best_frame_nb or idx (it's not a frame, it's an index)
>
Renamed to best_frame_idx.
>> + 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;
>> +
>> + // keep a reference of each frame
>> + thumb->frames[thumb->n].buf = inlink->cur_buf;
>> +
>> + // no selection until the buffer of N frames is filled up
>> + if (thumb->n < thumb->n_frames - 1) {
>> + thumb->n++;
>> + return;
>> + }
>> +
>> + // average histogram of the N frames
>> + for (j = 0; j < FF_ARRAY_ELEMS(avg_hist); j++) {
>> + for (i = 0; i < thumb->n_frames; i++)
>> + avg_hist[j] += (double)thumb->frames[i].histogram[j];
>> + avg_hist[j] /= thumb->n_frames;
>> + }
>> +
>> + // find the frame closer to the average using the sum of squared errors
>> + for (i = 0; i < thumb->n_frames; i++) {
>> + sq_err = frame_sum_square_err(thumb->frames[i].histogram, avg_hist);
>> + if (i == 0 || sq_err < min_sq_err)
>> + best_frame = i, min_sq_err = sq_err;
>> + }
>> +
>> + // free and reset everything (except the best frame buffer)
>> + for (i = 0; i < thumb->n_frames; i++) {
>> + memset(thumb->frames[i].histogram, 0, sizeof(thumb->frames[i].histogram));
>> + if (i == best_frame)
>> + continue;
>> + avfilter_unref_buffer(thumb->frames[i].buf);
>> + thumb->frames[i].buf = NULL;
>> + }
>> + thumb->n = 0;
>> +
>> + // raise the chosen one
>> + av_log(ctx, AV_LOG_INFO, "frame id #%d selected\n", best_frame);
>
> maybe print also the PTS here
>
Added pts_time.
> [...]
>
> Looks fine otherwise.
Thanks, pushed.
I'll eventually try to make the algorithm more effective for some
corner case later.
--
ubitux
More information about the ffmpeg-devel
mailing list