2011-10-24 17:11:10 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2011 Smartjog S.A.S, Clément Bœsch <clement.boesch@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@crocodile.org>.
|
2012-02-03 00:55:37 +01:00
|
|
|
* @see http://notbrainsurgery.livejournal.com/29773.html
|
2011-10-24 17:11:10 +02:00
|
|
|
*/
|
|
|
|
|
2013-03-24 20:32:59 +01:00
|
|
|
#include "libavutil/opt.h"
|
2011-10-24 17:11:10 +02:00
|
|
|
#include "avfilter.h"
|
2012-06-16 11:47:46 +02:00
|
|
|
#include "internal.h"
|
2011-10-24 17:11:10 +02:00
|
|
|
|
|
|
|
#define HIST_SIZE (3*256)
|
|
|
|
|
|
|
|
struct thumb_frame {
|
2013-03-10 01:30:30 +01:00
|
|
|
AVFrame *buf; ///< cached frame
|
2011-10-24 17:11:10 +02:00
|
|
|
int histogram[HIST_SIZE]; ///< RGB color distribution histogram of the frame
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct {
|
2013-03-24 20:32:59 +01:00
|
|
|
const AVClass *class;
|
2011-10-24 17:11:10 +02:00
|
|
|
int n; ///< current frame
|
|
|
|
int n_frames; ///< number of frames for analysis
|
|
|
|
struct thumb_frame *frames; ///< the n_frames frames
|
2013-03-13 23:59:25 +01:00
|
|
|
AVRational tb; ///< copy of the input timebase to ease access
|
2011-10-24 17:11:10 +02:00
|
|
|
} ThumbContext;
|
|
|
|
|
2013-03-24 20:32:59 +01:00
|
|
|
#define OFFSET(x) offsetof(ThumbContext, x)
|
|
|
|
#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
|
|
|
|
|
|
|
|
static const AVOption thumbnail_options[] = {
|
|
|
|
{ "n", "set the frames batch size", OFFSET(n_frames), AV_OPT_TYPE_INT, {.i64=100}, 2, INT_MAX, FLAGS },
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
AVFILTER_DEFINE_CLASS(thumbnail);
|
|
|
|
|
2013-04-12 11:13:33 +02:00
|
|
|
static av_cold int init(AVFilterContext *ctx)
|
2011-10-24 17:11:10 +02:00
|
|
|
{
|
|
|
|
ThumbContext *thumb = ctx->priv;
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2012-07-14 18:26:04 +02:00
|
|
|
av_log(ctx, AV_LOG_VERBOSE, "batch size: %d frames\n", thumb->n_frames);
|
2011-10-24 17:11:10 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Compute Sum-square deviation to estimate "closeness".
|
|
|
|
* @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;
|
|
|
|
}
|
|
|
|
|
2013-03-13 23:59:25 +01:00
|
|
|
static AVFrame *get_best_frame(AVFilterContext *ctx)
|
2011-10-24 17:11:10 +02:00
|
|
|
{
|
2013-03-13 23:59:25 +01:00
|
|
|
AVFrame *picref;
|
|
|
|
ThumbContext *thumb = ctx->priv;
|
2011-10-24 17:11:10 +02:00
|
|
|
int i, j, best_frame_idx = 0;
|
2013-03-13 23:59:25 +01:00
|
|
|
int nb_frames = thumb->n;
|
2011-10-24 17:11:10 +02:00
|
|
|
double avg_hist[HIST_SIZE] = {0}, sq_err, min_sq_err = -1;
|
2013-03-13 23:59:25 +01:00
|
|
|
|
|
|
|
// average histogram of the N frames
|
|
|
|
for (j = 0; j < FF_ARRAY_ELEMS(avg_hist); j++) {
|
|
|
|
for (i = 0; i < nb_frames; i++)
|
|
|
|
avg_hist[j] += (double)thumb->frames[i].histogram[j];
|
|
|
|
avg_hist[j] /= nb_frames;
|
|
|
|
}
|
|
|
|
|
|
|
|
// find the frame closer to the average using the sum of squared errors
|
|
|
|
for (i = 0; i < nb_frames; i++) {
|
|
|
|
sq_err = frame_sum_square_err(thumb->frames[i].histogram, avg_hist);
|
|
|
|
if (i == 0 || sq_err < min_sq_err)
|
|
|
|
best_frame_idx = i, min_sq_err = sq_err;
|
|
|
|
}
|
|
|
|
|
|
|
|
// free and reset everything (except the best frame buffer)
|
|
|
|
for (i = 0; i < nb_frames; i++) {
|
|
|
|
memset(thumb->frames[i].histogram, 0, sizeof(thumb->frames[i].histogram));
|
|
|
|
if (i != best_frame_idx)
|
2013-03-17 01:46:22 +01:00
|
|
|
av_frame_free(&thumb->frames[i].buf);
|
2013-03-13 23:59:25 +01:00
|
|
|
}
|
|
|
|
thumb->n = 0;
|
|
|
|
|
|
|
|
// raise the chosen one
|
|
|
|
picref = thumb->frames[best_frame_idx].buf;
|
|
|
|
av_log(ctx, AV_LOG_INFO, "frame id #%d (pts_time=%f) selected "
|
|
|
|
"from a set of %d images\n", best_frame_idx,
|
|
|
|
picref->pts * av_q2d(thumb->tb), nb_frames);
|
|
|
|
thumb->frames[best_frame_idx].buf = NULL;
|
|
|
|
|
|
|
|
return picref;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
|
|
|
|
{
|
|
|
|
int i, j;
|
2011-10-24 17:11:10 +02:00
|
|
|
AVFilterContext *ctx = inlink->dst;
|
2012-11-29 01:43:28 +01:00
|
|
|
ThumbContext *thumb = ctx->priv;
|
|
|
|
AVFilterLink *outlink = ctx->outputs[0];
|
2012-11-29 01:33:25 +01:00
|
|
|
int *hist = thumb->frames[thumb->n].histogram;
|
|
|
|
const uint8_t *p = frame->data[0];
|
2011-10-24 17:11:10 +02:00
|
|
|
|
|
|
|
// keep a reference of each frame
|
2012-11-29 01:33:25 +01:00
|
|
|
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];
|
|
|
|
}
|
2011-10-24 17:11:10 +02:00
|
|
|
|
|
|
|
// no selection until the buffer of N frames is filled up
|
2013-03-13 23:59:25 +01:00
|
|
|
thumb->n++;
|
|
|
|
if (thumb->n < thumb->n_frames)
|
2012-07-22 22:57:02 +02:00
|
|
|
return 0;
|
2011-10-24 17:11:10 +02:00
|
|
|
|
2013-03-13 23:59:25 +01:00
|
|
|
return ff_filter_frame(outlink, get_best_frame(ctx));
|
2011-10-24 17:11:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold void uninit(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
ThumbContext *thumb = ctx->priv;
|
2012-11-29 01:38:08 +01:00
|
|
|
for (i = 0; i < thumb->n_frames && thumb->frames[i].buf; i++)
|
2013-03-17 01:46:22 +01:00
|
|
|
av_frame_free(&thumb->frames[i].buf);
|
2011-10-24 17:11:10 +02:00
|
|
|
av_freep(&thumb->frames);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int request_frame(AVFilterLink *link)
|
|
|
|
{
|
2013-03-13 23:59:25 +01:00
|
|
|
AVFilterContext *ctx = link->src;
|
|
|
|
ThumbContext *thumb = ctx->priv;
|
2011-10-24 17:11:10 +02:00
|
|
|
|
|
|
|
/* loop until a frame thumbnail is available (when a frame is queued,
|
|
|
|
* thumb->n is reset to zero) */
|
2012-03-11 00:19:50 +01:00
|
|
|
do {
|
2013-03-13 23:59:25 +01:00
|
|
|
int ret = ff_request_frame(ctx->inputs[0]);
|
|
|
|
if (ret == AVERROR_EOF && thumb->n) {
|
|
|
|
ret = ff_filter_frame(link, get_best_frame(ctx));
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
ret = AVERROR_EOF;
|
|
|
|
}
|
2011-10-24 17:11:10 +02:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2012-03-11 00:19:50 +01:00
|
|
|
} while (thumb->n);
|
2011-10-24 17:11:10 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-03-13 23:59:25 +01:00
|
|
|
static int config_props(AVFilterLink *inlink)
|
|
|
|
{
|
|
|
|
AVFilterContext *ctx = inlink->dst;
|
|
|
|
ThumbContext *thumb = ctx->priv;
|
|
|
|
|
|
|
|
thumb->tb = inlink->time_base;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-10-24 17:11:10 +02:00
|
|
|
static int query_formats(AVFilterContext *ctx)
|
|
|
|
{
|
2012-10-08 20:54:00 +02:00
|
|
|
static const enum AVPixelFormat pix_fmts[] = {
|
|
|
|
AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
|
|
|
|
AV_PIX_FMT_NONE
|
2011-10-24 17:11:10 +02:00
|
|
|
};
|
2012-06-16 11:47:46 +02:00
|
|
|
ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
|
2011-10-24 17:11:10 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-11-28 20:01:59 +01:00
|
|
|
static const AVFilterPad thumbnail_inputs[] = {
|
|
|
|
{
|
2013-09-07 14:13:50 +02:00
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.config_props = config_props,
|
|
|
|
.filter_frame = filter_frame,
|
2012-11-28 20:01:59 +01:00
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVFilterPad thumbnail_outputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.request_frame = request_frame,
|
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
2011-10-24 17:11:10 +02:00
|
|
|
AVFilter avfilter_vf_thumbnail = {
|
|
|
|
.name = "thumbnail",
|
|
|
|
.description = NULL_IF_CONFIG_SMALL("Select the most representative frame in a given sequence of consecutive frames."),
|
|
|
|
.priv_size = sizeof(ThumbContext),
|
|
|
|
.init = init,
|
|
|
|
.uninit = uninit,
|
|
|
|
.query_formats = query_formats,
|
2012-11-28 20:01:59 +01:00
|
|
|
.inputs = thumbnail_inputs,
|
|
|
|
.outputs = thumbnail_outputs,
|
2013-03-24 20:32:59 +01:00
|
|
|
.priv_class = &thumbnail_class,
|
2011-10-24 17:11:10 +02:00
|
|
|
};
|