2011-05-07 02:06:25 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2011 Stefano Sabatini
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* filter for selecting which frame passes in the filterchain
|
|
|
|
*/
|
|
|
|
|
2013-04-08 12:58:56 +02:00
|
|
|
#include "libavutil/avstring.h"
|
2011-05-07 02:06:25 +02:00
|
|
|
#include "libavutil/eval.h"
|
|
|
|
#include "libavutil/fifo.h"
|
2012-08-06 15:49:32 +02:00
|
|
|
#include "libavutil/internal.h"
|
2012-12-12 00:21:26 +01:00
|
|
|
#include "libavutil/opt.h"
|
2011-05-07 02:06:25 +02:00
|
|
|
#include "avfilter.h"
|
2012-12-11 01:19:30 +01:00
|
|
|
#include "audio.h"
|
2012-05-26 12:38:59 +02:00
|
|
|
#include "formats.h"
|
2012-05-30 11:20:32 +02:00
|
|
|
#include "internal.h"
|
2012-05-19 10:37:56 +02:00
|
|
|
#include "video.h"
|
2011-05-07 02:06:25 +02:00
|
|
|
|
2012-06-29 19:13:13 +02:00
|
|
|
#if CONFIG_AVCODEC
|
|
|
|
#include "libavcodec/dsputil.h"
|
|
|
|
#endif
|
|
|
|
|
2012-02-20 09:42:33 +01:00
|
|
|
static const char *const var_names[] = {
|
2011-05-07 02:06:25 +02:00
|
|
|
"TB", ///< timebase
|
|
|
|
|
|
|
|
"pts", ///< original pts in the file of the frame
|
|
|
|
"start_pts", ///< first PTS in the stream, expressed in TB units
|
|
|
|
"prev_pts", ///< previous frame PTS
|
|
|
|
"prev_selected_pts", ///< previous selected frame PTS
|
|
|
|
|
|
|
|
"t", ///< first PTS in seconds
|
|
|
|
"start_t", ///< first PTS in the stream, expressed in seconds
|
|
|
|
"prev_t", ///< previous frame time
|
|
|
|
"prev_selected_t", ///< previously selected time
|
|
|
|
|
|
|
|
"pict_type", ///< the type of picture in the movie
|
2011-05-07 02:06:25 +02:00
|
|
|
"I",
|
|
|
|
"P",
|
|
|
|
"B",
|
|
|
|
"S",
|
|
|
|
"SI",
|
|
|
|
"SP",
|
|
|
|
"BI",
|
2011-05-07 02:06:25 +02:00
|
|
|
|
|
|
|
"interlace_type", ///< the frame interlace type
|
2011-05-07 02:06:25 +02:00
|
|
|
"PROGRESSIVE",
|
|
|
|
"TOPFIRST",
|
|
|
|
"BOTTOMFIRST",
|
2011-05-07 02:06:25 +02:00
|
|
|
|
2012-12-11 01:19:30 +01:00
|
|
|
"consumed_samples_n",///< number of samples consumed by the filter (only audio)
|
|
|
|
"samples_n", ///< number of samples in the current frame (only audio)
|
|
|
|
"sample_rate", ///< sample rate (only audio)
|
|
|
|
|
2011-05-07 02:06:25 +02:00
|
|
|
"n", ///< frame number (starting from zero)
|
|
|
|
"selected_n", ///< selected frame number (starting from zero)
|
|
|
|
"prev_selected_n", ///< number of the last selected frame
|
|
|
|
|
|
|
|
"key", ///< tell if the frame is a key frame
|
|
|
|
"pos", ///< original position in the file of the frame
|
|
|
|
|
2012-05-26 12:38:59 +02:00
|
|
|
"scene",
|
|
|
|
|
2011-05-07 02:06:25 +02:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
enum var_name {
|
|
|
|
VAR_TB,
|
|
|
|
|
|
|
|
VAR_PTS,
|
|
|
|
VAR_START_PTS,
|
|
|
|
VAR_PREV_PTS,
|
|
|
|
VAR_PREV_SELECTED_PTS,
|
|
|
|
|
|
|
|
VAR_T,
|
|
|
|
VAR_START_T,
|
|
|
|
VAR_PREV_T,
|
|
|
|
VAR_PREV_SELECTED_T,
|
|
|
|
|
|
|
|
VAR_PICT_TYPE,
|
|
|
|
VAR_PICT_TYPE_I,
|
|
|
|
VAR_PICT_TYPE_P,
|
|
|
|
VAR_PICT_TYPE_B,
|
|
|
|
VAR_PICT_TYPE_S,
|
|
|
|
VAR_PICT_TYPE_SI,
|
|
|
|
VAR_PICT_TYPE_SP,
|
|
|
|
VAR_PICT_TYPE_BI,
|
|
|
|
|
|
|
|
VAR_INTERLACE_TYPE,
|
|
|
|
VAR_INTERLACE_TYPE_P,
|
|
|
|
VAR_INTERLACE_TYPE_T,
|
|
|
|
VAR_INTERLACE_TYPE_B,
|
|
|
|
|
2012-12-11 01:19:30 +01:00
|
|
|
VAR_CONSUMED_SAMPLES_N,
|
|
|
|
VAR_SAMPLES_N,
|
|
|
|
VAR_SAMPLE_RATE,
|
|
|
|
|
2011-05-07 02:06:25 +02:00
|
|
|
VAR_N,
|
|
|
|
VAR_SELECTED_N,
|
|
|
|
VAR_PREV_SELECTED_N,
|
|
|
|
|
|
|
|
VAR_KEY,
|
|
|
|
VAR_POS,
|
|
|
|
|
2012-05-26 12:38:59 +02:00
|
|
|
VAR_SCENE,
|
|
|
|
|
2011-05-07 02:06:25 +02:00
|
|
|
VAR_VARS_NB
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct {
|
2012-12-12 00:21:26 +01:00
|
|
|
const AVClass *class;
|
|
|
|
char *expr_str;
|
2013-04-10 23:18:18 +02:00
|
|
|
AVExpr *expr;
|
2011-05-07 02:06:25 +02:00
|
|
|
double var_values[VAR_VARS_NB];
|
2012-05-26 12:38:59 +02:00
|
|
|
int do_scene_detect; ///< 1 if the expression requires scene detection variables, 0 otherwise
|
2012-06-29 19:13:13 +02:00
|
|
|
#if CONFIG_AVCODEC
|
2012-05-26 12:38:59 +02:00
|
|
|
AVCodecContext *avctx; ///< codec context required for the DSPContext (scene detect only)
|
|
|
|
DSPContext c; ///< context providing optimized SAD methods (scene detect only)
|
|
|
|
double prev_mafd; ///< previous MAFD (scene detect only)
|
2012-06-29 19:13:13 +02:00
|
|
|
#endif
|
2013-03-10 01:30:30 +01:00
|
|
|
AVFrame *prev_picref; ///< previous frame (scene detect only)
|
2011-05-07 02:06:25 +02:00
|
|
|
double select;
|
2013-04-08 12:58:56 +02:00
|
|
|
int select_out; ///< mark the selected output pad index
|
|
|
|
int nb_outputs;
|
2011-05-07 02:06:25 +02:00
|
|
|
} SelectContext;
|
|
|
|
|
2013-04-15 21:02:14 +02:00
|
|
|
#define OFFSET(x) offsetof(SelectContext, x)
|
|
|
|
#define DEFINE_OPTIONS(filt_name, FLAGS) \
|
|
|
|
static const AVOption filt_name##_options[] = { \
|
|
|
|
{ "expr", "set an expression to use for selecting frames", OFFSET(expr_str), AV_OPT_TYPE_STRING, { .str = "1" }, .flags=FLAGS }, \
|
|
|
|
{ "e", "set an expression to use for selecting frames", OFFSET(expr_str), AV_OPT_TYPE_STRING, { .str = "1" }, .flags=FLAGS }, \
|
|
|
|
{ "outputs", "set the number of outputs", OFFSET(nb_outputs), AV_OPT_TYPE_INT, {.i64 = 1}, 1, INT_MAX, .flags=FLAGS }, \
|
|
|
|
{ "n", "set the number of outputs", OFFSET(nb_outputs), AV_OPT_TYPE_INT, {.i64 = 1}, 1, INT_MAX, .flags=FLAGS }, \
|
|
|
|
{ NULL } \
|
|
|
|
}
|
|
|
|
|
2013-04-08 12:58:56 +02:00
|
|
|
static int request_frame(AVFilterLink *outlink);
|
2012-12-12 00:21:26 +01:00
|
|
|
|
2013-04-12 11:13:33 +02:00
|
|
|
static av_cold int init(AVFilterContext *ctx)
|
2011-05-07 02:06:25 +02:00
|
|
|
{
|
|
|
|
SelectContext *select = ctx->priv;
|
2013-04-08 12:58:56 +02:00
|
|
|
int i, ret;
|
2011-05-07 02:06:25 +02:00
|
|
|
|
2012-12-12 00:21:26 +01:00
|
|
|
if ((ret = av_expr_parse(&select->expr, select->expr_str,
|
2011-05-07 02:06:25 +02:00
|
|
|
var_names, NULL, NULL, NULL, NULL, 0, ctx)) < 0) {
|
2013-04-10 23:18:18 +02:00
|
|
|
av_log(ctx, AV_LOG_ERROR, "Error while parsing expression '%s'\n",
|
|
|
|
select->expr_str);
|
2011-05-07 02:06:25 +02:00
|
|
|
return ret;
|
|
|
|
}
|
2012-12-12 00:21:26 +01:00
|
|
|
select->do_scene_detect = !!strstr(select->expr_str, "scene");
|
2011-05-07 02:06:25 +02:00
|
|
|
|
2013-04-08 12:58:56 +02:00
|
|
|
for (i = 0; i < select->nb_outputs; i++) {
|
|
|
|
AVFilterPad pad = { 0 };
|
|
|
|
|
|
|
|
pad.name = av_asprintf("output%d", i);
|
|
|
|
if (!pad.name)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
pad.type = ctx->filter->inputs[0].type;
|
|
|
|
pad.request_frame = request_frame;
|
|
|
|
ff_insert_outpad(ctx, i, &pad);
|
|
|
|
}
|
|
|
|
|
2011-05-07 02:06:25 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define INTERLACE_TYPE_P 0
|
|
|
|
#define INTERLACE_TYPE_T 1
|
|
|
|
#define INTERLACE_TYPE_B 2
|
|
|
|
|
|
|
|
static int config_input(AVFilterLink *inlink)
|
|
|
|
{
|
|
|
|
SelectContext *select = inlink->dst->priv;
|
|
|
|
|
|
|
|
select->var_values[VAR_N] = 0.0;
|
|
|
|
select->var_values[VAR_SELECTED_N] = 0.0;
|
|
|
|
|
|
|
|
select->var_values[VAR_TB] = av_q2d(inlink->time_base);
|
|
|
|
|
|
|
|
select->var_values[VAR_PREV_PTS] = NAN;
|
|
|
|
select->var_values[VAR_PREV_SELECTED_PTS] = NAN;
|
|
|
|
select->var_values[VAR_PREV_SELECTED_T] = NAN;
|
2013-02-07 21:16:11 +01:00
|
|
|
select->var_values[VAR_PREV_T] = NAN;
|
2011-05-07 02:06:25 +02:00
|
|
|
select->var_values[VAR_START_PTS] = NAN;
|
|
|
|
select->var_values[VAR_START_T] = NAN;
|
|
|
|
|
|
|
|
select->var_values[VAR_PICT_TYPE_I] = AV_PICTURE_TYPE_I;
|
|
|
|
select->var_values[VAR_PICT_TYPE_P] = AV_PICTURE_TYPE_P;
|
|
|
|
select->var_values[VAR_PICT_TYPE_B] = AV_PICTURE_TYPE_B;
|
|
|
|
select->var_values[VAR_PICT_TYPE_SI] = AV_PICTURE_TYPE_SI;
|
|
|
|
select->var_values[VAR_PICT_TYPE_SP] = AV_PICTURE_TYPE_SP;
|
|
|
|
|
|
|
|
select->var_values[VAR_INTERLACE_TYPE_P] = INTERLACE_TYPE_P;
|
|
|
|
select->var_values[VAR_INTERLACE_TYPE_T] = INTERLACE_TYPE_T;
|
2011-11-16 07:48:23 +01:00
|
|
|
select->var_values[VAR_INTERLACE_TYPE_B] = INTERLACE_TYPE_B;
|
2011-05-07 02:06:25 +02:00
|
|
|
|
2012-12-12 00:28:09 +01:00
|
|
|
select->var_values[VAR_PICT_TYPE] = NAN;
|
|
|
|
select->var_values[VAR_INTERLACE_TYPE] = NAN;
|
|
|
|
select->var_values[VAR_SCENE] = NAN;
|
|
|
|
select->var_values[VAR_CONSUMED_SAMPLES_N] = NAN;
|
|
|
|
select->var_values[VAR_SAMPLES_N] = NAN;
|
|
|
|
|
2012-12-11 01:19:30 +01:00
|
|
|
select->var_values[VAR_SAMPLE_RATE] =
|
|
|
|
inlink->type == AVMEDIA_TYPE_AUDIO ? inlink->sample_rate : NAN;
|
|
|
|
|
2012-12-20 05:17:17 +01:00
|
|
|
#if CONFIG_AVCODEC
|
|
|
|
if (select->do_scene_detect) {
|
2012-05-26 12:38:59 +02:00
|
|
|
select->avctx = avcodec_alloc_context3(NULL);
|
|
|
|
if (!select->avctx)
|
|
|
|
return AVERROR(ENOMEM);
|
2013-04-14 04:34:06 +02:00
|
|
|
avpriv_dsputil_init(&select->c, select->avctx);
|
2012-05-26 12:38:59 +02:00
|
|
|
}
|
2012-12-20 05:17:17 +01:00
|
|
|
#endif
|
2011-05-07 02:06:25 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-06-29 19:13:13 +02:00
|
|
|
#if CONFIG_AVCODEC
|
2013-03-10 01:30:30 +01:00
|
|
|
static double get_scene_score(AVFilterContext *ctx, AVFrame *frame)
|
2012-05-26 12:38:59 +02:00
|
|
|
{
|
|
|
|
double ret = 0;
|
|
|
|
SelectContext *select = ctx->priv;
|
2013-03-10 01:30:30 +01:00
|
|
|
AVFrame *prev_picref = select->prev_picref;
|
2012-05-26 12:38:59 +02:00
|
|
|
|
|
|
|
if (prev_picref &&
|
2013-03-10 01:30:30 +01:00
|
|
|
frame->height == prev_picref->height &&
|
|
|
|
frame->width == prev_picref->width &&
|
|
|
|
frame->linesize[0] == prev_picref->linesize[0]) {
|
2012-10-13 18:19:29 +02:00
|
|
|
int x, y, nb_sad = 0;
|
2012-10-13 18:10:28 +02:00
|
|
|
int64_t sad = 0;
|
2012-05-26 12:38:59 +02:00
|
|
|
double mafd, diff;
|
2013-03-10 01:30:30 +01:00
|
|
|
uint8_t *p1 = frame->data[0];
|
2012-05-26 12:38:59 +02:00
|
|
|
uint8_t *p2 = prev_picref->data[0];
|
2013-03-10 01:30:30 +01:00
|
|
|
const int linesize = frame->linesize[0];
|
2012-05-26 12:38:59 +02:00
|
|
|
|
2013-03-10 01:30:30 +01:00
|
|
|
for (y = 0; y < frame->height - 8; y += 8) {
|
|
|
|
for (x = 0; x < frame->width*3 - 8; x += 8) {
|
2012-10-14 02:09:05 +02:00
|
|
|
sad += select->c.sad[1](select, p1 + x, p2 + x,
|
2012-05-26 12:38:59 +02:00
|
|
|
linesize, 8);
|
2012-10-13 18:19:29 +02:00
|
|
|
nb_sad += 8 * 8;
|
|
|
|
}
|
2012-10-14 02:09:05 +02:00
|
|
|
p1 += 8 * linesize;
|
|
|
|
p2 += 8 * linesize;
|
2012-10-13 18:19:29 +02:00
|
|
|
}
|
2012-05-26 12:38:59 +02:00
|
|
|
emms_c();
|
2012-10-13 18:19:29 +02:00
|
|
|
mafd = nb_sad ? sad / nb_sad : 0;
|
2012-06-05 00:17:38 +02:00
|
|
|
diff = fabs(mafd - select->prev_mafd);
|
2012-05-26 12:38:59 +02:00
|
|
|
ret = av_clipf(FFMIN(mafd, diff) / 100., 0, 1);
|
|
|
|
select->prev_mafd = mafd;
|
2013-03-10 01:30:30 +01:00
|
|
|
av_frame_free(&prev_picref);
|
2012-05-26 12:38:59 +02:00
|
|
|
}
|
2013-03-10 01:30:30 +01:00
|
|
|
select->prev_picref = av_frame_clone(frame);
|
2012-05-26 12:38:59 +02:00
|
|
|
return ret;
|
|
|
|
}
|
2012-06-29 19:13:13 +02:00
|
|
|
#endif
|
2012-05-26 12:38:59 +02:00
|
|
|
|
2011-05-07 02:06:25 +02:00
|
|
|
#define D2TS(d) (isnan(d) ? AV_NOPTS_VALUE : (int64_t)(d))
|
|
|
|
#define TS2D(ts) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts))
|
|
|
|
|
2013-04-15 18:28:57 +02:00
|
|
|
static void select_frame(AVFilterContext *ctx, AVFrame *frame)
|
2011-05-07 02:06:25 +02:00
|
|
|
{
|
|
|
|
SelectContext *select = ctx->priv;
|
|
|
|
AVFilterLink *inlink = ctx->inputs[0];
|
|
|
|
double res;
|
|
|
|
|
|
|
|
if (isnan(select->var_values[VAR_START_PTS]))
|
2013-03-10 01:30:30 +01:00
|
|
|
select->var_values[VAR_START_PTS] = TS2D(frame->pts);
|
2011-08-11 15:40:03 +02:00
|
|
|
if (isnan(select->var_values[VAR_START_T]))
|
2013-03-10 01:30:30 +01:00
|
|
|
select->var_values[VAR_START_T] = TS2D(frame->pts) * av_q2d(inlink->time_base);
|
2011-05-07 02:06:25 +02:00
|
|
|
|
2013-04-15 17:42:04 +02:00
|
|
|
select->var_values[VAR_N ] = inlink->frame_count;
|
2013-03-10 01:30:30 +01:00
|
|
|
select->var_values[VAR_PTS] = TS2D(frame->pts);
|
|
|
|
select->var_values[VAR_T ] = TS2D(frame->pts) * av_q2d(inlink->time_base);
|
|
|
|
select->var_values[VAR_POS] = av_frame_get_pkt_pos(frame) == -1 ? NAN : av_frame_get_pkt_pos(frame);
|
2011-05-07 02:06:25 +02:00
|
|
|
|
2012-12-11 01:19:30 +01:00
|
|
|
switch (inlink->type) {
|
|
|
|
case AVMEDIA_TYPE_AUDIO:
|
2013-03-10 01:30:30 +01:00
|
|
|
select->var_values[VAR_SAMPLES_N] = frame->nb_samples;
|
2012-12-11 01:19:30 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case AVMEDIA_TYPE_VIDEO:
|
|
|
|
select->var_values[VAR_INTERLACE_TYPE] =
|
2013-03-10 01:30:30 +01:00
|
|
|
!frame->interlaced_frame ? INTERLACE_TYPE_P :
|
|
|
|
frame->top_field_first ? INTERLACE_TYPE_T : INTERLACE_TYPE_B;
|
|
|
|
select->var_values[VAR_PICT_TYPE] = frame->pict_type;
|
2012-12-20 05:17:17 +01:00
|
|
|
#if CONFIG_AVCODEC
|
|
|
|
if (select->do_scene_detect) {
|
2012-12-11 01:19:30 +01:00
|
|
|
char buf[32];
|
2013-03-10 01:30:30 +01:00
|
|
|
select->var_values[VAR_SCENE] = get_scene_score(ctx, frame);
|
2012-12-11 01:19:30 +01:00
|
|
|
// TODO: document metadata
|
|
|
|
snprintf(buf, sizeof(buf), "%f", select->var_values[VAR_SCENE]);
|
2013-04-11 18:30:19 +02:00
|
|
|
av_dict_set(avpriv_frame_get_metadatap(frame), "lavfi.scene_score", buf, 0);
|
2012-12-11 01:19:30 +01:00
|
|
|
}
|
2012-12-20 05:17:17 +01:00
|
|
|
#endif
|
2012-12-11 01:19:30 +01:00
|
|
|
break;
|
|
|
|
}
|
2011-05-07 02:06:25 +02:00
|
|
|
|
2013-04-15 18:28:57 +02:00
|
|
|
select->select = res = av_expr_eval(select->expr, select->var_values, NULL);
|
2011-05-07 02:06:25 +02:00
|
|
|
av_log(inlink->dst, AV_LOG_DEBUG,
|
2013-03-10 01:30:30 +01:00
|
|
|
"n:%f pts:%f t:%f key:%d",
|
2013-02-07 21:00:54 +01:00
|
|
|
select->var_values[VAR_N],
|
|
|
|
select->var_values[VAR_PTS],
|
2011-05-07 02:06:25 +02:00
|
|
|
select->var_values[VAR_T],
|
2012-12-11 01:19:30 +01:00
|
|
|
(int)select->var_values[VAR_KEY]);
|
|
|
|
|
|
|
|
switch (inlink->type) {
|
|
|
|
case AVMEDIA_TYPE_VIDEO:
|
2012-12-12 00:28:09 +01:00
|
|
|
av_log(inlink->dst, AV_LOG_DEBUG, " interlace_type:%c pict_type:%c scene:%f",
|
2012-12-11 01:19:30 +01:00
|
|
|
select->var_values[VAR_INTERLACE_TYPE] == INTERLACE_TYPE_P ? 'P' :
|
|
|
|
select->var_values[VAR_INTERLACE_TYPE] == INTERLACE_TYPE_T ? 'T' :
|
|
|
|
select->var_values[VAR_INTERLACE_TYPE] == INTERLACE_TYPE_B ? 'B' : '?',
|
2012-12-12 00:28:09 +01:00
|
|
|
av_get_picture_type_char(select->var_values[VAR_PICT_TYPE]),
|
|
|
|
select->var_values[VAR_SCENE]);
|
2012-12-11 01:19:30 +01:00
|
|
|
break;
|
|
|
|
case AVMEDIA_TYPE_AUDIO:
|
|
|
|
av_log(inlink->dst, AV_LOG_DEBUG, " samples_n:%d consumed_samples_n:%d",
|
|
|
|
(int)select->var_values[VAR_SAMPLES_N],
|
|
|
|
(int)select->var_values[VAR_CONSUMED_SAMPLES_N]);
|
|
|
|
break;
|
|
|
|
}
|
2011-05-07 02:06:25 +02:00
|
|
|
|
2013-04-08 12:58:56 +02:00
|
|
|
if (res == 0) {
|
|
|
|
select->select_out = -1; /* drop */
|
|
|
|
} else if (isnan(res) || res < 0) {
|
|
|
|
select->select_out = 0; /* first output */
|
|
|
|
} else {
|
|
|
|
select->select_out = FFMIN(ceilf(res)-1, select->nb_outputs-1); /* other outputs */
|
|
|
|
}
|
|
|
|
|
|
|
|
av_log(inlink->dst, AV_LOG_DEBUG, " -> select:%f select_out:%d\n", res, select->select_out);
|
2011-05-07 02:06:25 +02:00
|
|
|
|
|
|
|
if (res) {
|
|
|
|
select->var_values[VAR_PREV_SELECTED_N] = select->var_values[VAR_N];
|
|
|
|
select->var_values[VAR_PREV_SELECTED_PTS] = select->var_values[VAR_PTS];
|
|
|
|
select->var_values[VAR_PREV_SELECTED_T] = select->var_values[VAR_T];
|
|
|
|
select->var_values[VAR_SELECTED_N] += 1.0;
|
2012-12-11 01:19:30 +01:00
|
|
|
if (inlink->type == AVMEDIA_TYPE_AUDIO)
|
2013-03-10 01:30:30 +01:00
|
|
|
select->var_values[VAR_CONSUMED_SAMPLES_N] += frame->nb_samples;
|
2011-05-07 02:06:25 +02:00
|
|
|
}
|
2012-12-11 01:19:30 +01:00
|
|
|
|
2013-02-07 21:16:11 +01:00
|
|
|
select->var_values[VAR_PREV_PTS] = select->var_values[VAR_PTS];
|
|
|
|
select->var_values[VAR_PREV_T] = select->var_values[VAR_T];
|
2011-05-07 02:06:25 +02:00
|
|
|
}
|
|
|
|
|
2013-03-10 01:30:30 +01:00
|
|
|
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
|
2011-05-07 02:06:25 +02:00
|
|
|
{
|
2013-04-08 12:58:56 +02:00
|
|
|
AVFilterContext *ctx = inlink->dst;
|
|
|
|
SelectContext *select = ctx->priv;
|
2011-05-07 02:06:25 +02:00
|
|
|
|
2013-04-08 12:58:56 +02:00
|
|
|
select_frame(ctx, frame);
|
2012-12-11 00:49:37 +01:00
|
|
|
if (select->select)
|
2013-04-08 12:58:56 +02:00
|
|
|
return ff_filter_frame(ctx->outputs[select->select_out], frame);
|
2012-07-08 17:29:42 +02:00
|
|
|
|
2013-03-10 01:30:30 +01:00
|
|
|
av_frame_free(&frame);
|
2012-07-14 09:25:33 +02:00
|
|
|
return 0;
|
2011-05-07 02:06:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int request_frame(AVFilterLink *outlink)
|
|
|
|
{
|
|
|
|
AVFilterContext *ctx = outlink->src;
|
|
|
|
SelectContext *select = ctx->priv;
|
|
|
|
AVFilterLink *inlink = outlink->src->inputs[0];
|
2013-04-08 12:58:56 +02:00
|
|
|
int out_no = FF_OUTLINK_IDX(outlink);
|
2011-05-07 02:06:25 +02:00
|
|
|
|
2012-12-11 00:49:37 +01:00
|
|
|
do {
|
2012-05-30 11:20:32 +02:00
|
|
|
int ret = ff_request_frame(inlink);
|
2011-05-07 02:06:25 +02:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2013-04-08 12:58:56 +02:00
|
|
|
} while (select->select_out != out_no);
|
2011-05-07 02:06:25 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold void uninit(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
SelectContext *select = ctx->priv;
|
2013-04-08 12:58:56 +02:00
|
|
|
int i;
|
2011-05-07 02:06:25 +02:00
|
|
|
|
2011-05-07 02:06:25 +02:00
|
|
|
av_expr_free(select->expr);
|
2011-05-07 02:06:25 +02:00
|
|
|
select->expr = NULL;
|
|
|
|
|
2013-04-08 12:58:56 +02:00
|
|
|
for (i = 0; i < ctx->nb_outputs; i++)
|
|
|
|
av_freep(&ctx->output_pads[i].name);
|
|
|
|
|
2012-12-20 05:17:17 +01:00
|
|
|
#if CONFIG_AVCODEC
|
2012-05-26 12:38:59 +02:00
|
|
|
if (select->do_scene_detect) {
|
2013-03-10 01:30:30 +01:00
|
|
|
av_frame_free(&select->prev_picref);
|
2012-09-18 18:49:09 +02:00
|
|
|
if (select->avctx) {
|
|
|
|
avcodec_close(select->avctx);
|
|
|
|
av_freep(&select->avctx);
|
|
|
|
}
|
2012-05-26 12:38:59 +02:00
|
|
|
}
|
2012-12-20 05:17:17 +01:00
|
|
|
#endif
|
2012-05-26 12:38:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int query_formats(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
SelectContext *select = ctx->priv;
|
|
|
|
|
|
|
|
if (!select->do_scene_detect) {
|
|
|
|
return ff_default_query_formats(ctx);
|
|
|
|
} else {
|
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
|
2012-05-26 12:38:59 +02:00
|
|
|
};
|
2012-06-16 11:47:46 +02:00
|
|
|
ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
|
2012-05-26 12:38:59 +02:00
|
|
|
}
|
|
|
|
return 0;
|
2011-05-07 02:06:25 +02:00
|
|
|
}
|
|
|
|
|
2012-12-11 01:19:30 +01:00
|
|
|
#if CONFIG_ASELECT_FILTER
|
2012-12-12 00:21:26 +01:00
|
|
|
|
2013-04-15 21:02:14 +02:00
|
|
|
DEFINE_OPTIONS(aselect, AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM);
|
2012-12-12 00:21:26 +01:00
|
|
|
AVFILTER_DEFINE_CLASS(aselect);
|
|
|
|
|
2013-04-12 11:13:33 +02:00
|
|
|
static av_cold int aselect_init(AVFilterContext *ctx)
|
2012-12-12 00:21:26 +01:00
|
|
|
{
|
|
|
|
SelectContext *select = ctx->priv;
|
|
|
|
int ret;
|
|
|
|
|
2013-04-12 11:13:33 +02:00
|
|
|
if ((ret = init(ctx)) < 0)
|
2012-12-12 00:21:26 +01:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (select->do_scene_detect) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Scene detection is ignored in aselect filter\n");
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-12-11 01:19:30 +01:00
|
|
|
static const AVFilterPad avfilter_af_aselect_inputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
.get_audio_buffer = ff_null_get_audio_buffer,
|
|
|
|
.config_props = config_input,
|
|
|
|
.filter_frame = filter_frame,
|
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
AVFilter avfilter_af_aselect = {
|
|
|
|
.name = "aselect",
|
|
|
|
.description = NULL_IF_CONFIG_SMALL("Select audio frames to pass in output."),
|
2012-12-12 00:21:26 +01:00
|
|
|
.init = aselect_init,
|
2012-12-11 01:19:30 +01:00
|
|
|
.uninit = uninit,
|
|
|
|
.priv_size = sizeof(SelectContext),
|
|
|
|
.inputs = avfilter_af_aselect_inputs,
|
2012-12-12 00:21:26 +01:00
|
|
|
.priv_class = &aselect_class,
|
2013-04-08 12:58:56 +02:00
|
|
|
.flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS,
|
2012-12-11 01:19:30 +01:00
|
|
|
};
|
|
|
|
#endif /* CONFIG_ASELECT_FILTER */
|
|
|
|
|
|
|
|
#if CONFIG_SELECT_FILTER
|
2012-12-12 00:21:26 +01:00
|
|
|
|
2013-04-15 21:02:14 +02:00
|
|
|
DEFINE_OPTIONS(select, AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM);
|
2012-12-12 00:21:26 +01:00
|
|
|
AVFILTER_DEFINE_CLASS(select);
|
|
|
|
|
2013-04-12 11:13:33 +02:00
|
|
|
static av_cold int select_init(AVFilterContext *ctx)
|
2012-12-12 00:21:26 +01:00
|
|
|
{
|
|
|
|
SelectContext *select = ctx->priv;
|
|
|
|
int ret;
|
|
|
|
|
2013-04-12 11:13:33 +02:00
|
|
|
if ((ret = init(ctx)) < 0)
|
2012-12-12 00:21:26 +01:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (select->do_scene_detect && !CONFIG_AVCODEC) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Scene detection is not available without libavcodec.\n");
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-07-24 15:14:01 +02:00
|
|
|
static const AVFilterPad avfilter_vf_select_inputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.get_video_buffer = ff_null_get_video_buffer,
|
|
|
|
.config_props = config_input,
|
2012-11-28 21:02:09 +01:00
|
|
|
.filter_frame = filter_frame,
|
2012-07-24 15:14:01 +02:00
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
2011-05-07 02:06:25 +02:00
|
|
|
AVFilter avfilter_vf_select = {
|
|
|
|
.name = "select",
|
2012-12-11 01:19:30 +01:00
|
|
|
.description = NULL_IF_CONFIG_SMALL("Select video frames to pass in output."),
|
2012-12-12 00:21:26 +01:00
|
|
|
.init = select_init,
|
2011-05-07 02:06:25 +02:00
|
|
|
.uninit = uninit,
|
2012-05-26 12:38:59 +02:00
|
|
|
.query_formats = query_formats,
|
2011-05-07 02:06:25 +02:00
|
|
|
|
|
|
|
.priv_size = sizeof(SelectContext),
|
2013-04-10 23:18:18 +02:00
|
|
|
.priv_class = &select_class,
|
2011-05-07 02:06:25 +02:00
|
|
|
|
2012-07-24 15:14:01 +02:00
|
|
|
.inputs = avfilter_vf_select_inputs,
|
2013-04-08 12:58:56 +02:00
|
|
|
.flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS,
|
2011-05-07 02:06:25 +02:00
|
|
|
};
|
2012-12-11 01:19:30 +01:00
|
|
|
#endif /* CONFIG_SELECT_FILTER */
|