2011-10-24 18:11:10 +03: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 02:55:37 +03:00
|
|
|
* @see http://notbrainsurgery.livejournal.com/29773.html
|
2011-10-24 18:11:10 +03:00
|
|
|
*/
|
|
|
|
|
2013-03-24 22:32:59 +03:00
|
|
|
#include "libavutil/opt.h"
|
2021-02-06 22:59:50 +02:00
|
|
|
#include "libavutil/pixdesc.h"
|
2011-10-24 18:11:10 +03:00
|
|
|
#include "avfilter.h"
|
2012-06-16 12:47:46 +03:00
|
|
|
#include "internal.h"
|
2011-10-24 18:11:10 +03:00
|
|
|
|
|
|
|
#define HIST_SIZE (3*256)
|
|
|
|
|
|
|
|
struct thumb_frame {
|
2013-03-10 03:30:30 +03:00
|
|
|
AVFrame *buf; ///< cached frame
|
2011-10-24 18:11:10 +03:00
|
|
|
int histogram[HIST_SIZE]; ///< RGB color distribution histogram of the frame
|
|
|
|
};
|
|
|
|
|
2017-05-12 20:00:49 +02:00
|
|
|
typedef struct ThumbContext {
|
2013-03-24 22:32:59 +03:00
|
|
|
const AVClass *class;
|
2011-10-24 18:11:10 +03:00
|
|
|
int n; ///< current frame
|
|
|
|
int n_frames; ///< number of frames for analysis
|
|
|
|
struct thumb_frame *frames; ///< the n_frames frames
|
2013-03-14 01:59:25 +03:00
|
|
|
AVRational tb; ///< copy of the input timebase to ease access
|
2021-02-06 22:59:50 +02:00
|
|
|
|
|
|
|
int planewidth[4];
|
|
|
|
int planeheight[4];
|
2011-10-24 18:11:10 +03:00
|
|
|
} ThumbContext;
|
|
|
|
|
2013-03-24 22:32:59 +03: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 12:13:33 +03:00
|
|
|
static av_cold int init(AVFilterContext *ctx)
|
2011-10-24 18:11:10 +03:00
|
|
|
{
|
2015-09-04 17:20:53 +02:00
|
|
|
ThumbContext *s = ctx->priv;
|
2011-10-24 18:11:10 +03:00
|
|
|
|
2015-09-04 17:20:53 +02:00
|
|
|
s->frames = av_calloc(s->n_frames, sizeof(*s->frames));
|
|
|
|
if (!s->frames) {
|
2011-10-24 18:11:10 +03:00
|
|
|
av_log(ctx, AV_LOG_ERROR,
|
|
|
|
"Allocation failure, try to lower the number of frames\n");
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
2015-09-04 17:20:53 +02:00
|
|
|
av_log(ctx, AV_LOG_VERBOSE, "batch size: %d frames\n", s->n_frames);
|
2011-10-24 18:11:10 +03: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-14 01:59:25 +03:00
|
|
|
static AVFrame *get_best_frame(AVFilterContext *ctx)
|
2011-10-24 18:11:10 +03:00
|
|
|
{
|
2013-03-14 01:59:25 +03:00
|
|
|
AVFrame *picref;
|
2015-09-04 17:20:53 +02:00
|
|
|
ThumbContext *s = ctx->priv;
|
2011-10-24 18:11:10 +03:00
|
|
|
int i, j, best_frame_idx = 0;
|
2015-09-04 17:20:53 +02:00
|
|
|
int nb_frames = s->n;
|
2011-10-24 18:11:10 +03:00
|
|
|
double avg_hist[HIST_SIZE] = {0}, sq_err, min_sq_err = -1;
|
2013-03-14 01:59:25 +03:00
|
|
|
|
|
|
|
// average histogram of the N frames
|
|
|
|
for (j = 0; j < FF_ARRAY_ELEMS(avg_hist); j++) {
|
|
|
|
for (i = 0; i < nb_frames; i++)
|
2015-09-04 17:20:53 +02:00
|
|
|
avg_hist[j] += (double)s->frames[i].histogram[j];
|
2013-03-14 01:59:25 +03:00
|
|
|
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++) {
|
2015-09-04 17:20:53 +02:00
|
|
|
sq_err = frame_sum_square_err(s->frames[i].histogram, avg_hist);
|
2013-03-14 01:59:25 +03:00
|
|
|
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++) {
|
2015-09-04 17:20:53 +02:00
|
|
|
memset(s->frames[i].histogram, 0, sizeof(s->frames[i].histogram));
|
2013-03-14 01:59:25 +03:00
|
|
|
if (i != best_frame_idx)
|
2015-09-04 17:20:53 +02:00
|
|
|
av_frame_free(&s->frames[i].buf);
|
2013-03-14 01:59:25 +03:00
|
|
|
}
|
2015-09-04 17:20:53 +02:00
|
|
|
s->n = 0;
|
2013-03-14 01:59:25 +03:00
|
|
|
|
|
|
|
// raise the chosen one
|
2015-09-04 17:20:53 +02:00
|
|
|
picref = s->frames[best_frame_idx].buf;
|
2013-03-14 01:59:25 +03:00
|
|
|
av_log(ctx, AV_LOG_INFO, "frame id #%d (pts_time=%f) selected "
|
|
|
|
"from a set of %d images\n", best_frame_idx,
|
2015-09-04 17:20:53 +02:00
|
|
|
picref->pts * av_q2d(s->tb), nb_frames);
|
|
|
|
s->frames[best_frame_idx].buf = NULL;
|
2013-03-14 01:59:25 +03:00
|
|
|
|
|
|
|
return picref;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
|
|
|
|
{
|
|
|
|
int i, j;
|
2011-10-24 18:11:10 +03:00
|
|
|
AVFilterContext *ctx = inlink->dst;
|
2015-09-04 17:20:53 +02:00
|
|
|
ThumbContext *s = ctx->priv;
|
2012-11-29 03:43:28 +03:00
|
|
|
AVFilterLink *outlink = ctx->outputs[0];
|
2015-09-04 17:20:53 +02:00
|
|
|
int *hist = s->frames[s->n].histogram;
|
2012-11-29 03:33:25 +03:00
|
|
|
const uint8_t *p = frame->data[0];
|
2011-10-24 18:11:10 +03:00
|
|
|
|
|
|
|
// keep a reference of each frame
|
2015-09-04 17:20:53 +02:00
|
|
|
s->frames[s->n].buf = frame;
|
2012-11-29 03:33:25 +03:00
|
|
|
|
2021-02-06 22:59:50 +02:00
|
|
|
// update current frame histogram
|
|
|
|
switch (inlink->format) {
|
|
|
|
case AV_PIX_FMT_RGB24:
|
|
|
|
case AV_PIX_FMT_BGR24:
|
|
|
|
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];
|
|
|
|
}
|
|
|
|
break;
|
2021-02-08 13:58:12 +02:00
|
|
|
case AV_PIX_FMT_RGB0:
|
|
|
|
case AV_PIX_FMT_BGR0:
|
|
|
|
case AV_PIX_FMT_RGBA:
|
|
|
|
case AV_PIX_FMT_BGRA:
|
|
|
|
for (j = 0; j < inlink->h; j++) {
|
|
|
|
for (i = 0; i < inlink->w; i++) {
|
|
|
|
hist[0*256 + p[i*4 ]]++;
|
|
|
|
hist[1*256 + p[i*4 + 1]]++;
|
|
|
|
hist[2*256 + p[i*4 + 2]]++;
|
|
|
|
}
|
|
|
|
p += frame->linesize[0];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case AV_PIX_FMT_0RGB:
|
|
|
|
case AV_PIX_FMT_0BGR:
|
|
|
|
case AV_PIX_FMT_ARGB:
|
|
|
|
case AV_PIX_FMT_ABGR:
|
|
|
|
for (j = 0; j < inlink->h; j++) {
|
|
|
|
for (i = 0; i < inlink->w; i++) {
|
|
|
|
hist[0*256 + p[i*4 + 1]]++;
|
|
|
|
hist[1*256 + p[i*4 + 2]]++;
|
|
|
|
hist[2*256 + p[i*4 + 3]]++;
|
|
|
|
}
|
|
|
|
p += frame->linesize[0];
|
|
|
|
}
|
|
|
|
break;
|
2021-02-06 22:59:50 +02:00
|
|
|
default:
|
|
|
|
for (int plane = 0; plane < 3; plane++) {
|
|
|
|
const uint8_t *p = frame->data[plane];
|
|
|
|
for (j = 0; j < s->planeheight[plane]; j++) {
|
|
|
|
for (i = 0; i < s->planewidth[plane]; i++)
|
|
|
|
hist[256*plane + p[i]]++;
|
|
|
|
p += frame->linesize[plane];
|
|
|
|
}
|
2012-11-29 03:33:25 +03:00
|
|
|
}
|
2021-02-06 22:59:50 +02:00
|
|
|
break;
|
2012-11-29 03:33:25 +03:00
|
|
|
}
|
2011-10-24 18:11:10 +03:00
|
|
|
|
|
|
|
// no selection until the buffer of N frames is filled up
|
2015-09-04 17:20:53 +02:00
|
|
|
s->n++;
|
|
|
|
if (s->n < s->n_frames)
|
2012-07-22 23:57:02 +03:00
|
|
|
return 0;
|
2011-10-24 18:11:10 +03:00
|
|
|
|
2013-03-14 01:59:25 +03:00
|
|
|
return ff_filter_frame(outlink, get_best_frame(ctx));
|
2011-10-24 18:11:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold void uninit(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
int i;
|
2015-09-04 17:20:53 +02:00
|
|
|
ThumbContext *s = ctx->priv;
|
2020-02-10 16:42:52 +02:00
|
|
|
for (i = 0; i < s->n_frames && s->frames && s->frames[i].buf; i++)
|
2015-09-04 17:20:53 +02:00
|
|
|
av_frame_free(&s->frames[i].buf);
|
|
|
|
av_freep(&s->frames);
|
2011-10-24 18:11:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int request_frame(AVFilterLink *link)
|
|
|
|
{
|
2013-03-14 01:59:25 +03:00
|
|
|
AVFilterContext *ctx = link->src;
|
2015-09-04 17:20:53 +02:00
|
|
|
ThumbContext *s = ctx->priv;
|
2015-10-02 16:20:17 +02:00
|
|
|
int ret = ff_request_frame(ctx->inputs[0]);
|
2011-10-24 18:11:10 +03:00
|
|
|
|
2015-10-02 16:20:17 +02:00
|
|
|
if (ret == AVERROR_EOF && s->n) {
|
|
|
|
ret = ff_filter_frame(link, get_best_frame(ctx));
|
2011-10-24 18:11:10 +03:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2015-10-02 16:20:17 +02:00
|
|
|
ret = AVERROR_EOF;
|
|
|
|
}
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2011-10-24 18:11:10 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-03-14 01:59:25 +03:00
|
|
|
static int config_props(AVFilterLink *inlink)
|
|
|
|
{
|
|
|
|
AVFilterContext *ctx = inlink->dst;
|
2015-09-04 17:20:53 +02:00
|
|
|
ThumbContext *s = ctx->priv;
|
2021-02-06 22:59:50 +02:00
|
|
|
const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
|
2013-03-14 01:59:25 +03:00
|
|
|
|
2015-09-04 17:20:53 +02:00
|
|
|
s->tb = inlink->time_base;
|
2021-02-06 22:59:50 +02:00
|
|
|
s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
|
|
|
|
s->planewidth[0] = s->planewidth[3] = inlink->w;
|
|
|
|
s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
|
|
|
|
s->planeheight[0] = s->planeheight[3] = inlink->h;
|
|
|
|
|
2013-03-14 01:59:25 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-08 21:54:00 +03:00
|
|
|
static const enum AVPixelFormat pix_fmts[] = {
|
|
|
|
AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
|
2021-02-08 13:58:12 +02:00
|
|
|
AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA,
|
|
|
|
AV_PIX_FMT_RGB0, AV_PIX_FMT_BGR0,
|
|
|
|
AV_PIX_FMT_ABGR, AV_PIX_FMT_ARGB,
|
|
|
|
AV_PIX_FMT_0BGR, AV_PIX_FMT_0RGB,
|
2021-02-06 22:59:50 +02:00
|
|
|
AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
|
|
|
|
AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
|
|
|
|
AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
|
|
|
|
AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
|
|
|
|
AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P,
|
|
|
|
AV_PIX_FMT_YUVJ411P,
|
|
|
|
AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
|
|
|
|
AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
|
2012-10-08 21:54:00 +03:00
|
|
|
AV_PIX_FMT_NONE
|
2011-10-24 18:11:10 +03:00
|
|
|
};
|
|
|
|
|
2012-11-28 22:01:59 +03:00
|
|
|
static const AVFilterPad thumbnail_inputs[] = {
|
|
|
|
{
|
2013-09-07 15:13:50 +03:00
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.config_props = config_props,
|
|
|
|
.filter_frame = filter_frame,
|
2012-11-28 22:01:59 +03:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVFilterPad thumbnail_outputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.request_frame = request_frame,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2021-04-19 18:33:56 +02:00
|
|
|
const AVFilter ff_vf_thumbnail = {
|
2011-10-24 18:11:10 +03:00
|
|
|
.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,
|
2021-08-12 13:05:31 +02:00
|
|
|
FILTER_INPUTS(thumbnail_inputs),
|
|
|
|
FILTER_OUTPUTS(thumbnail_outputs),
|
2021-09-27 17:32:40 +02:00
|
|
|
FILTER_PIXFMTS_ARRAY(pix_fmts),
|
2013-03-24 22:32:59 +03:00
|
|
|
.priv_class = &thumbnail_class,
|
2020-02-10 16:42:27 +02:00
|
|
|
.flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
|
2011-10-24 18:11:10 +03:00
|
|
|
};
|