* @see http://notbrainsurgery.livejournal.com/29773.html
*/
+#include "libavutil/opt.h"
#include "avfilter.h"
#include "internal.h"
#define HIST_SIZE (3*256)
struct thumb_frame {
- AVFilterBufferRef *buf; ///< cached frame
+ AVFrame *buf; ///< cached frame
int histogram[HIST_SIZE]; ///< RGB color distribution histogram of the frame
};
typedef struct {
+ const AVClass *class;
int n; ///< current frame
int n_frames; ///< number of frames for analysis
struct thumb_frame *frames; ///< the n_frames frames
+ AVRational tb; ///< copy of the input timebase to ease access
} ThumbContext;
-static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
+#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);
+
+static av_cold int init(AVFilterContext *ctx)
{
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);
- }
- }
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);
+ av_log(ctx, AV_LOG_VERBOSE, "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".
* @param hist color distribution histogram
return sum_sq_err;
}
-static void end_frame(AVFilterLink *inlink)
+static AVFrame *get_best_frame(AVFilterContext *ctx)
{
+ AVFrame *picref;
+ ThumbContext *thumb = ctx->priv;
int i, j, best_frame_idx = 0;
+ int nb_frames = thumb->n;
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;
- AVFilterBufferRef *picref;
-
- // 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++)
+ for (i = 0; i < nb_frames; i++)
avg_hist[j] += (double)thumb->frames[i].histogram[j];
- avg_hist[j] /= thumb->n_frames;
+ avg_hist[j] /= nb_frames;
}
// find the frame closer to the average using the sum of squared errors
- for (i = 0; i < thumb->n_frames; i++) {
+ 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 < thumb->n_frames; i++) {
+ for (i = 0; i < nb_frames; i++) {
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;
+ if (i != best_frame_idx)
+ av_frame_free(&thumb->frames[i].buf);
}
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));
- ff_start_frame(outlink, picref);
+ 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;
- ff_draw_slice(outlink, 0, inlink->h, 1);
- ff_end_frame(outlink);
+
+ return picref;
+}
+
+static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
+{
+ int i, j;
+ AVFilterContext *ctx = inlink->dst;
+ ThumbContext *thumb = ctx->priv;
+ AVFilterLink *outlink = ctx->outputs[0];
+ 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 = 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
+ thumb->n++;
+ if (thumb->n < thumb->n_frames)
+ return 0;
+
+ return ff_filter_frame(outlink, get_best_frame(ctx));
}
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++)
+ av_frame_free(&thumb->frames[i].buf);
av_freep(&thumb->frames);
}
-static void null_start_frame(AVFilterLink *link, AVFilterBufferRef *picref) { }
-
static int request_frame(AVFilterLink *link)
{
- ThumbContext *thumb = link->src->priv;
+ AVFilterContext *ctx = link->src;
+ ThumbContext *thumb = ctx->priv;
/* loop until a frame thumbnail is available (when a frame is queued,
* thumb->n is reset to zero) */
do {
- int ret = avfilter_request_frame(link->src->inputs[0]);
+ 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;
+ }
if (ret < 0)
return ret;
} while (thumb->n);
return 0;
}
-static int poll_frame(AVFilterLink *link)
+static int config_props(AVFilterLink *inlink)
{
- ThumbContext *thumb = link->src->priv;
- AVFilterLink *inlink = link->src->inputs[0];
- int ret, available_frames = ff_poll_frame(inlink);
-
- /* 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;
+ AVFilterContext *ctx = inlink->dst;
+ ThumbContext *thumb = ctx->priv;
- /* we have some frame(s) available in the input link, but not yet enough to
- * output a thumbnail, so we request more */
- ret = avfilter_request_frame(inlink);
- return ret < 0 ? ret : 0;
+ thumb->tb = inlink->time_base;
+ return 0;
}
static int query_formats(AVFilterContext *ctx)
{
- static const enum PixelFormat pix_fmts[] = {
- PIX_FMT_RGB24, PIX_FMT_BGR24,
- PIX_FMT_NONE
+ static const enum AVPixelFormat pix_fmts[] = {
+ AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
+ AV_PIX_FMT_NONE
};
ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
return 0;
}
+static const AVFilterPad thumbnail_inputs[] = {
+ {
+ .name = "default",
+ .type = AVMEDIA_TYPE_VIDEO,
+ .config_props = config_props,
+ .get_video_buffer = ff_null_get_video_buffer,
+ .filter_frame = filter_frame,
+ },
+ { NULL }
+};
+
+static const AVFilterPad thumbnail_outputs[] = {
+ {
+ .name = "default",
+ .type = AVMEDIA_TYPE_VIDEO,
+ .request_frame = request_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."),
.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,
- .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,
- .rej_perms = AV_PERM_REUSE2,
- },{ .name = NULL }
- },
+ .inputs = thumbnail_inputs,
+ .outputs = thumbnail_outputs,
+ .priv_class = &thumbnail_class,
};