1
0
mirror of https://github.com/FFmpeg/FFmpeg.git synced 2024-12-28 20:53:54 +02:00
FFmpeg/libavfilter/vf_thumbnail.c

236 lines
7.4 KiB
C
Raw Normal View History

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>.
* @see http://notbrainsurgery.livejournal.com/29773.html
2011-10-24 18:11:10 +03:00
*/
#include "avfilter.h"
#include "internal.h"
2011-10-24 18:11:10 +03:00
#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)
2011-10-24 18:11:10 +03:00
{
ThumbContext *thumb = ctx->priv;
if (!args) {
thumb->n_frames = 100;
} else {
int n = sscanf(args, "%d", &thumb->n_frames);
if (n != 1 || thumb->n_frames < 2) {
thumb->n_frames = 0;
av_log(ctx, AV_LOG_ERROR,
"Invalid number of frames specified (minimum is 2).\n");
return AVERROR(EINVAL);
2011-10-24 18:11:10 +03:00
}
}
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_VERBOSE, "batch size: %d frames\n", thumb->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;
}
static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *frame)
2011-10-24 18:11:10 +03:00
{
int i, j, best_frame_idx = 0;
double avg_hist[HIST_SIZE] = {0}, sq_err, min_sq_err = -1;
AVFilterContext *ctx = inlink->dst;
ThumbContext *thumb = ctx->priv;
AVFilterLink *outlink = ctx->outputs[0];
2011-10-24 18:11:10 +03:00
AVFilterBufferRef *picref;
int *hist = thumb->frames[thumb->n].histogram;
const uint8_t *p = frame->data[0];
2011-10-24 18:11:10 +03:00
// keep a reference of each frame
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 18:11:10 +03:00
// no selection until the buffer of N frames is filled up
if (thumb->n < thumb->n_frames - 1) {
thumb->n++;
return 0;
2011-10-24 18:11:10 +03:00
}
// 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_idx = 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_idx)
continue;
avfilter_unref_bufferp(&thumb->frames[i].buf);
2011-10-24 18:11:10 +03: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\n",
best_frame_idx, picref->pts * av_q2d(inlink->time_base));
thumb->frames[best_frame_idx].buf = NULL;
return ff_filter_frame(outlink, picref);
2011-10-24 18:11:10 +03:00
}
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_bufferp(&thumb->frames[i].buf);
2011-10-24 18:11:10 +03:00
av_freep(&thumb->frames);
}
static int request_frame(AVFilterLink *link)
{
ThumbContext *thumb = link->src->priv;
/* loop until a frame thumbnail is available (when a frame is queued,
* thumb->n is reset to zero) */
do {
int ret = ff_request_frame(link->src->inputs[0]);
2011-10-24 18:11:10 +03:00
if (ret < 0)
return ret;
} while (thumb->n);
2011-10-24 18:11:10 +03:00
return 0;
}
static int poll_frame(AVFilterLink *link)
{
ThumbContext *thumb = link->src->priv;
AVFilterLink *inlink = link->src->inputs[0];
int ret, available_frames = ff_poll_frame(inlink);
2011-10-24 18:11:10 +03:00
/* If the input link is not able to provide any frame, we can't do anything
* at the moment and thus have zero thumbnail available. */
if (!available_frames)
return 0;
/* Since at least one frame is available and the next frame will allow us
* to compute a thumbnail, we can return 1 frame. */
if (thumb->n == thumb->n_frames - 1)
return 1;
/* we have some frame(s) available in the input link, but not yet enough to
* output a thumbnail, so we request more */
ret = ff_request_frame(inlink);
2011-10-24 18:11:10 +03:00
return ret < 0 ? ret : 0;
}
static int query_formats(AVFilterContext *ctx)
{
Merge commit '716d413c13981da15323c7a3821860536eefdbbb' * commit '716d413c13981da15323c7a3821860536eefdbbb': Replace PIX_FMT_* -> AV_PIX_FMT_*, PixelFormat -> AVPixelFormat Conflicts: doc/examples/muxing.c ffmpeg.h ffmpeg_filter.c ffmpeg_opt.c ffplay.c ffprobe.c libavcodec/8bps.c libavcodec/aasc.c libavcodec/aura.c libavcodec/avcodec.h libavcodec/avs.c libavcodec/bfi.c libavcodec/bmp.c libavcodec/bmpenc.c libavcodec/c93.c libavcodec/cscd.c libavcodec/cyuv.c libavcodec/dpx.c libavcodec/dpxenc.c libavcodec/eatgv.c libavcodec/escape124.c libavcodec/ffv1.c libavcodec/flashsv.c libavcodec/fraps.c libavcodec/h264.c libavcodec/huffyuv.c libavcodec/iff.c libavcodec/imgconvert.c libavcodec/indeo3.c libavcodec/kmvc.c libavcodec/libopenjpegdec.c libavcodec/libopenjpegenc.c libavcodec/libx264.c libavcodec/ljpegenc.c libavcodec/mjpegdec.c libavcodec/mjpegenc.c libavcodec/motionpixels.c libavcodec/mpeg12.c libavcodec/mpeg12enc.c libavcodec/mpeg4videodec.c libavcodec/mpegvideo_enc.c libavcodec/pamenc.c libavcodec/pcxenc.c libavcodec/pgssubdec.c libavcodec/pngdec.c libavcodec/pngenc.c libavcodec/pnm.c libavcodec/pnmdec.c libavcodec/pnmenc.c libavcodec/ptx.c libavcodec/qdrw.c libavcodec/qpeg.c libavcodec/qtrleenc.c libavcodec/raw.c libavcodec/rawdec.c libavcodec/rl2.c libavcodec/sgidec.c libavcodec/sgienc.c libavcodec/snowdec.c libavcodec/snowenc.c libavcodec/sunrast.c libavcodec/targa.c libavcodec/targaenc.c libavcodec/tiff.c libavcodec/tiffenc.c libavcodec/tmv.c libavcodec/truemotion2.c libavcodec/utils.c libavcodec/vb.c libavcodec/vp3.c libavcodec/wnv1.c libavcodec/xl.c libavcodec/xwddec.c libavcodec/xwdenc.c libavcodec/yop.c libavdevice/v4l2.c libavdevice/x11grab.c libavfilter/avfilter.c libavfilter/avfilter.h libavfilter/buffersrc.c libavfilter/drawutils.c libavfilter/formats.c libavfilter/src_movie.c libavfilter/vf_ass.c libavfilter/vf_drawtext.c libavfilter/vf_fade.c libavfilter/vf_format.c libavfilter/vf_hflip.c libavfilter/vf_lut.c libavfilter/vf_overlay.c libavfilter/vf_pad.c libavfilter/vf_scale.c libavfilter/vf_transpose.c libavfilter/vf_yadif.c libavfilter/video.c libavfilter/vsrc_testsrc.c libavformat/movenc.c libavformat/mxf.h libavformat/utils.c libavformat/yuv4mpeg.c libavutil/imgutils.c libavutil/pixdesc.c libswscale/input.c libswscale/output.c libswscale/swscale_internal.h libswscale/swscale_unscaled.c libswscale/utils.c libswscale/x86/swscale_template.c libswscale/x86/yuv2rgb.c libswscale/x86/yuv2rgb_template.c libswscale/yuv2rgb.c Merged-by: Michael Niedermayer <michaelni@gmx.at>
2012-10-08 21:54:00 +03:00
static const enum AVPixelFormat pix_fmts[] = {
AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
AV_PIX_FMT_NONE
2011-10-24 18:11:10 +03:00
};
ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
2011-10-24 18:11:10 +03:00
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 }
};
2011-10-24 18:11:10 +03: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,
.inputs = thumbnail_inputs,
.outputs = thumbnail_outputs,
2011-10-24 18:11:10 +03:00
};