2011-05-07 00:06:25 +00: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 10:58:56 +00:00
|
|
|
#include "libavutil/avstring.h"
|
2011-05-07 00:06:25 +00:00
|
|
|
#include "libavutil/eval.h"
|
|
|
|
#include "libavutil/fifo.h"
|
2019-07-20 23:24:18 +00:00
|
|
|
#include "libavutil/imgutils.h"
|
2012-08-06 13:49:32 +00:00
|
|
|
#include "libavutil/internal.h"
|
2012-12-11 23:21:26 +00:00
|
|
|
#include "libavutil/opt.h"
|
2019-07-20 23:24:18 +00:00
|
|
|
#include "libavutil/pixdesc.h"
|
2011-05-07 00:06:25 +00:00
|
|
|
#include "avfilter.h"
|
2012-12-11 00:19:30 +00:00
|
|
|
#include "audio.h"
|
2012-05-26 10:38:59 +00:00
|
|
|
#include "formats.h"
|
2012-05-30 09:20:32 +00:00
|
|
|
#include "internal.h"
|
2012-05-19 08:37:56 +00:00
|
|
|
#include "video.h"
|
2018-04-04 23:47:32 +00:00
|
|
|
#include "scene_sad.h"
|
2011-05-07 00:06:25 +00:00
|
|
|
|
2012-02-20 08:42:33 +00:00
|
|
|
static const char *const var_names[] = {
|
2011-05-07 00:06:25 +00: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
|
|
|
|
|
2014-08-05 19:15:13 +00:00
|
|
|
"t", ///< timestamp expressed in seconds
|
2011-05-07 00:06:25 +00:00
|
|
|
"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 00:06:25 +00:00
|
|
|
"I",
|
|
|
|
"P",
|
|
|
|
"B",
|
|
|
|
"S",
|
|
|
|
"SI",
|
|
|
|
"SP",
|
|
|
|
"BI",
|
2013-05-10 17:36:07 +00:00
|
|
|
"PICT_TYPE_I",
|
|
|
|
"PICT_TYPE_P",
|
|
|
|
"PICT_TYPE_B",
|
|
|
|
"PICT_TYPE_S",
|
|
|
|
"PICT_TYPE_SI",
|
|
|
|
"PICT_TYPE_SP",
|
|
|
|
"PICT_TYPE_BI",
|
2011-05-07 00:06:25 +00:00
|
|
|
|
|
|
|
"interlace_type", ///< the frame interlace type
|
2011-05-07 00:06:25 +00:00
|
|
|
"PROGRESSIVE",
|
|
|
|
"TOPFIRST",
|
|
|
|
"BOTTOMFIRST",
|
2011-05-07 00:06:25 +00:00
|
|
|
|
2012-12-11 00:19:30 +00: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 00:06:25 +00: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 10:38:59 +00:00
|
|
|
"scene",
|
|
|
|
|
2015-10-24 19:06:38 +00:00
|
|
|
"concatdec_select", ///< frame is within the interval set by the concat demuxer
|
|
|
|
|
2011-05-07 00:06:25 +00: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,
|
2013-05-10 17:36:07 +00:00
|
|
|
VAR_I,
|
|
|
|
VAR_P,
|
|
|
|
VAR_B,
|
|
|
|
VAR_S,
|
|
|
|
VAR_SI,
|
|
|
|
VAR_SP,
|
|
|
|
VAR_BI,
|
2011-05-07 00:06:25 +00:00
|
|
|
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 00:19:30 +00:00
|
|
|
VAR_CONSUMED_SAMPLES_N,
|
|
|
|
VAR_SAMPLES_N,
|
|
|
|
VAR_SAMPLE_RATE,
|
|
|
|
|
2011-05-07 00:06:25 +00:00
|
|
|
VAR_N,
|
|
|
|
VAR_SELECTED_N,
|
|
|
|
VAR_PREV_SELECTED_N,
|
|
|
|
|
|
|
|
VAR_KEY,
|
|
|
|
VAR_POS,
|
|
|
|
|
2012-05-26 10:38:59 +00:00
|
|
|
VAR_SCENE,
|
|
|
|
|
2015-10-24 19:06:38 +00:00
|
|
|
VAR_CONCATDEC_SELECT,
|
|
|
|
|
2011-05-07 00:06:25 +00:00
|
|
|
VAR_VARS_NB
|
|
|
|
};
|
|
|
|
|
2014-04-19 16:20:17 +00:00
|
|
|
typedef struct SelectContext {
|
2012-12-11 23:21:26 +00:00
|
|
|
const AVClass *class;
|
|
|
|
char *expr_str;
|
2013-04-10 21:18:18 +00:00
|
|
|
AVExpr *expr;
|
2011-05-07 00:06:25 +00:00
|
|
|
double var_values[VAR_VARS_NB];
|
2019-07-20 23:24:18 +00:00
|
|
|
int bitdepth;
|
|
|
|
int nb_planes;
|
|
|
|
ptrdiff_t width[4];
|
|
|
|
ptrdiff_t height[4];
|
2012-05-26 10:38:59 +00:00
|
|
|
int do_scene_detect; ///< 1 if the expression requires scene detection variables, 0 otherwise
|
2018-04-04 23:47:32 +00:00
|
|
|
ff_scene_sad_fn sad; ///< Sum of the absolute difference function (scene detect only)
|
2014-08-14 16:52:47 +00:00
|
|
|
double prev_mafd; ///< previous MAFD (scene detect only)
|
|
|
|
AVFrame *prev_picref; ///< previous frame (scene detect only)
|
2011-05-07 00:06:25 +00:00
|
|
|
double select;
|
2013-04-08 10:58:56 +00:00
|
|
|
int select_out; ///< mark the selected output pad index
|
|
|
|
int nb_outputs;
|
2011-05-07 00:06:25 +00:00
|
|
|
} SelectContext;
|
|
|
|
|
2013-04-15 19:02:14 +00: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 10:58:56 +00:00
|
|
|
static int request_frame(AVFilterLink *outlink);
|
2012-12-11 23:21:26 +00:00
|
|
|
|
2013-04-12 09:13:33 +00:00
|
|
|
static av_cold int init(AVFilterContext *ctx)
|
2011-05-07 00:06:25 +00:00
|
|
|
{
|
|
|
|
SelectContext *select = ctx->priv;
|
2013-04-08 10:58:56 +00:00
|
|
|
int i, ret;
|
2011-05-07 00:06:25 +00:00
|
|
|
|
2012-12-11 23:21:26 +00:00
|
|
|
if ((ret = av_expr_parse(&select->expr, select->expr_str,
|
2011-05-07 00:06:25 +00:00
|
|
|
var_names, NULL, NULL, NULL, NULL, 0, ctx)) < 0) {
|
2013-04-10 21:18:18 +00:00
|
|
|
av_log(ctx, AV_LOG_ERROR, "Error while parsing expression '%s'\n",
|
|
|
|
select->expr_str);
|
2011-05-07 00:06:25 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2012-12-11 23:21:26 +00:00
|
|
|
select->do_scene_detect = !!strstr(select->expr_str, "scene");
|
2011-05-07 00:06:25 +00:00
|
|
|
|
2013-04-08 10:58:56 +00: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;
|
2017-08-25 08:04:28 +00:00
|
|
|
if ((ret = ff_insert_outpad(ctx, i, &pad)) < 0) {
|
|
|
|
av_freep(&pad.name);
|
|
|
|
return ret;
|
|
|
|
}
|
2013-04-08 10:58:56 +00:00
|
|
|
}
|
|
|
|
|
2011-05-07 00:06:25 +00: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;
|
2019-07-20 23:24:18 +00:00
|
|
|
const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
|
2019-08-13 01:39:47 +00:00
|
|
|
int is_yuv = !(desc->flags & AV_PIX_FMT_FLAG_RGB) &&
|
|
|
|
(desc->flags & AV_PIX_FMT_FLAG_PLANAR) &&
|
|
|
|
desc->nb_components >= 3;
|
2019-07-20 23:24:18 +00:00
|
|
|
|
|
|
|
select->bitdepth = desc->comp[0].depth;
|
2019-08-13 01:39:47 +00:00
|
|
|
select->nb_planes = is_yuv ? 1 : av_pix_fmt_count_planes(inlink->format);
|
|
|
|
|
2019-07-20 23:24:18 +00:00
|
|
|
for (int plane = 0; plane < select->nb_planes; plane++) {
|
|
|
|
ptrdiff_t line_size = av_image_get_linesize(inlink->format, inlink->w, plane);
|
|
|
|
int vsub = desc->log2_chroma_h;
|
|
|
|
|
|
|
|
select->width[plane] = line_size >> (select->bitdepth > 8);
|
|
|
|
select->height[plane] = plane == 1 || plane == 2 ? AV_CEIL_RSHIFT(inlink->h, vsub) : inlink->h;
|
|
|
|
}
|
2011-05-07 00:06:25 +00:00
|
|
|
|
|
|
|
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 20:16:11 +00:00
|
|
|
select->var_values[VAR_PREV_T] = NAN;
|
2011-05-07 00:06:25 +00:00
|
|
|
select->var_values[VAR_START_PTS] = NAN;
|
|
|
|
select->var_values[VAR_START_T] = NAN;
|
|
|
|
|
2013-05-10 17:36:07 +00:00
|
|
|
select->var_values[VAR_I] = AV_PICTURE_TYPE_I;
|
|
|
|
select->var_values[VAR_P] = AV_PICTURE_TYPE_P;
|
|
|
|
select->var_values[VAR_B] = AV_PICTURE_TYPE_B;
|
|
|
|
select->var_values[VAR_SI] = AV_PICTURE_TYPE_SI;
|
|
|
|
select->var_values[VAR_SP] = AV_PICTURE_TYPE_SP;
|
|
|
|
select->var_values[VAR_BI] = AV_PICTURE_TYPE_BI;
|
2011-05-07 00:06:25 +00:00
|
|
|
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;
|
2013-05-10 17:37:50 +00:00
|
|
|
select->var_values[VAR_PICT_TYPE_BI] = AV_PICTURE_TYPE_BI;
|
2011-05-07 00:06:25 +00:00
|
|
|
|
|
|
|
select->var_values[VAR_INTERLACE_TYPE_P] = INTERLACE_TYPE_P;
|
|
|
|
select->var_values[VAR_INTERLACE_TYPE_T] = INTERLACE_TYPE_T;
|
2011-11-16 06:48:23 +00:00
|
|
|
select->var_values[VAR_INTERLACE_TYPE_B] = INTERLACE_TYPE_B;
|
2011-05-07 00:06:25 +00:00
|
|
|
|
2012-12-11 23:28:09 +00: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 00:19:30 +00:00
|
|
|
select->var_values[VAR_SAMPLE_RATE] =
|
|
|
|
inlink->type == AVMEDIA_TYPE_AUDIO ? inlink->sample_rate : NAN;
|
|
|
|
|
2019-01-10 19:10:50 +00:00
|
|
|
if (CONFIG_SELECT_FILTER && select->do_scene_detect) {
|
2019-07-20 23:24:18 +00:00
|
|
|
select->sad = ff_scene_sad_get_fn(select->bitdepth == 8 ? 8 : 16);
|
2014-08-02 16:59:02 +00:00
|
|
|
if (!select->sad)
|
|
|
|
return AVERROR(EINVAL);
|
2012-05-26 10:38:59 +00:00
|
|
|
}
|
2011-05-07 00:06:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-03-10 00:30:30 +00:00
|
|
|
static double get_scene_score(AVFilterContext *ctx, AVFrame *frame)
|
2012-05-26 10:38:59 +00:00
|
|
|
{
|
|
|
|
double ret = 0;
|
|
|
|
SelectContext *select = ctx->priv;
|
2013-03-10 00:30:30 +00:00
|
|
|
AVFrame *prev_picref = select->prev_picref;
|
2012-05-26 10:38:59 +00:00
|
|
|
|
|
|
|
if (prev_picref &&
|
2014-08-02 16:59:02 +00:00
|
|
|
frame->height == prev_picref->height &&
|
|
|
|
frame->width == prev_picref->width) {
|
2019-07-20 23:24:18 +00:00
|
|
|
uint64_t sad = 0;
|
2012-05-26 10:38:59 +00:00
|
|
|
double mafd, diff;
|
2019-07-20 23:24:18 +00:00
|
|
|
uint64_t count = 0;
|
|
|
|
|
|
|
|
for (int plane = 0; plane < select->nb_planes; plane++) {
|
|
|
|
uint64_t plane_sad;
|
|
|
|
select->sad(prev_picref->data[plane], prev_picref->linesize[plane],
|
|
|
|
frame->data[plane], frame->linesize[plane],
|
|
|
|
select->width[plane], select->height[plane], &plane_sad);
|
|
|
|
sad += plane_sad;
|
|
|
|
count += select->width[plane] * select->height[plane];
|
|
|
|
}
|
2018-04-04 23:47:32 +00:00
|
|
|
|
2012-05-26 10:38:59 +00:00
|
|
|
emms_c();
|
2019-07-20 23:24:18 +00:00
|
|
|
mafd = (double)sad / count / (1ULL << (select->bitdepth - 8));
|
2012-06-04 22:17:38 +00:00
|
|
|
diff = fabs(mafd - select->prev_mafd);
|
2012-05-26 10:38:59 +00:00
|
|
|
ret = av_clipf(FFMIN(mafd, diff) / 100., 0, 1);
|
|
|
|
select->prev_mafd = mafd;
|
2013-03-10 00:30:30 +00:00
|
|
|
av_frame_free(&prev_picref);
|
2012-05-26 10:38:59 +00:00
|
|
|
}
|
2013-03-10 00:30:30 +00:00
|
|
|
select->prev_picref = av_frame_clone(frame);
|
2012-05-26 10:38:59 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-10-24 19:06:38 +00:00
|
|
|
static double get_concatdec_select(AVFrame *frame, int64_t pts)
|
|
|
|
{
|
2017-04-22 08:57:18 +00:00
|
|
|
AVDictionary *metadata = frame->metadata;
|
2015-10-24 19:06:38 +00:00
|
|
|
AVDictionaryEntry *start_time_entry = av_dict_get(metadata, "lavf.concatdec.start_time", NULL, 0);
|
|
|
|
AVDictionaryEntry *duration_entry = av_dict_get(metadata, "lavf.concatdec.duration", NULL, 0);
|
|
|
|
if (start_time_entry) {
|
|
|
|
int64_t start_time = strtoll(start_time_entry->value, NULL, 10);
|
|
|
|
if (pts >= start_time) {
|
|
|
|
if (duration_entry) {
|
|
|
|
int64_t duration = strtoll(duration_entry->value, NULL, 10);
|
|
|
|
if (pts < start_time + duration)
|
|
|
|
return -1;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return NAN;
|
|
|
|
}
|
|
|
|
|
2013-04-15 16:28:57 +00:00
|
|
|
static void select_frame(AVFilterContext *ctx, AVFrame *frame)
|
2011-05-07 00:06:25 +00:00
|
|
|
{
|
|
|
|
SelectContext *select = ctx->priv;
|
|
|
|
AVFilterLink *inlink = ctx->inputs[0];
|
|
|
|
double res;
|
|
|
|
|
|
|
|
if (isnan(select->var_values[VAR_START_PTS]))
|
2013-03-10 00:30:30 +00:00
|
|
|
select->var_values[VAR_START_PTS] = TS2D(frame->pts);
|
2011-08-11 13:40:03 +00:00
|
|
|
if (isnan(select->var_values[VAR_START_T]))
|
2013-03-10 00:30:30 +00:00
|
|
|
select->var_values[VAR_START_T] = TS2D(frame->pts) * av_q2d(inlink->time_base);
|
2011-05-07 00:06:25 +00:00
|
|
|
|
2016-08-30 13:28:41 +00:00
|
|
|
select->var_values[VAR_N ] = inlink->frame_count_out;
|
2013-03-10 00:30:30 +00:00
|
|
|
select->var_values[VAR_PTS] = TS2D(frame->pts);
|
|
|
|
select->var_values[VAR_T ] = TS2D(frame->pts) * av_q2d(inlink->time_base);
|
2017-04-22 08:57:18 +00:00
|
|
|
select->var_values[VAR_POS] = frame->pkt_pos == -1 ? NAN : frame->pkt_pos;
|
2014-07-30 15:48:33 +00:00
|
|
|
select->var_values[VAR_KEY] = frame->key_frame;
|
2015-10-24 19:06:38 +00:00
|
|
|
select->var_values[VAR_CONCATDEC_SELECT] = get_concatdec_select(frame, av_rescale_q(frame->pts, inlink->time_base, AV_TIME_BASE_Q));
|
2011-05-07 00:06:25 +00:00
|
|
|
|
2012-12-11 00:19:30 +00:00
|
|
|
switch (inlink->type) {
|
|
|
|
case AVMEDIA_TYPE_AUDIO:
|
2013-03-10 00:30:30 +00:00
|
|
|
select->var_values[VAR_SAMPLES_N] = frame->nb_samples;
|
2012-12-11 00:19:30 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case AVMEDIA_TYPE_VIDEO:
|
|
|
|
select->var_values[VAR_INTERLACE_TYPE] =
|
2013-03-10 00:30:30 +00: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 04:17:17 +00:00
|
|
|
if (select->do_scene_detect) {
|
2012-12-11 00:19:30 +00:00
|
|
|
char buf[32];
|
2013-03-10 00:30:30 +00:00
|
|
|
select->var_values[VAR_SCENE] = get_scene_score(ctx, frame);
|
2012-12-11 00:19:30 +00:00
|
|
|
// TODO: document metadata
|
|
|
|
snprintf(buf, sizeof(buf), "%f", select->var_values[VAR_SCENE]);
|
2017-04-22 08:57:18 +00:00
|
|
|
av_dict_set(&frame->metadata, "lavfi.scene_score", buf, 0);
|
2012-12-11 00:19:30 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2011-05-07 00:06:25 +00:00
|
|
|
|
2013-04-15 16:28:57 +00:00
|
|
|
select->select = res = av_expr_eval(select->expr, select->var_values, NULL);
|
2011-05-07 00:06:25 +00:00
|
|
|
av_log(inlink->dst, AV_LOG_DEBUG,
|
2013-03-10 00:30:30 +00:00
|
|
|
"n:%f pts:%f t:%f key:%d",
|
2013-02-07 20:00:54 +00:00
|
|
|
select->var_values[VAR_N],
|
|
|
|
select->var_values[VAR_PTS],
|
2011-05-07 00:06:25 +00:00
|
|
|
select->var_values[VAR_T],
|
2014-07-30 15:50:35 +00:00
|
|
|
frame->key_frame);
|
2012-12-11 00:19:30 +00:00
|
|
|
|
|
|
|
switch (inlink->type) {
|
|
|
|
case AVMEDIA_TYPE_VIDEO:
|
2012-12-11 23:28:09 +00:00
|
|
|
av_log(inlink->dst, AV_LOG_DEBUG, " interlace_type:%c pict_type:%c scene:%f",
|
2014-07-30 15:37:09 +00:00
|
|
|
(!frame->interlaced_frame) ? 'P' :
|
|
|
|
frame->top_field_first ? 'T' : 'B',
|
|
|
|
av_get_picture_type_char(frame->pict_type),
|
2012-12-11 23:28:09 +00:00
|
|
|
select->var_values[VAR_SCENE]);
|
2012-12-11 00:19:30 +00:00
|
|
|
break;
|
|
|
|
case AVMEDIA_TYPE_AUDIO:
|
2014-07-30 15:50:35 +00:00
|
|
|
av_log(inlink->dst, AV_LOG_DEBUG, " samples_n:%d consumed_samples_n:%f",
|
|
|
|
frame->nb_samples,
|
|
|
|
select->var_values[VAR_CONSUMED_SAMPLES_N]);
|
2012-12-11 00:19:30 +00:00
|
|
|
break;
|
|
|
|
}
|
2011-05-07 00:06:25 +00:00
|
|
|
|
2013-04-08 10:58:56 +00: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 00:06:25 +00: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 00:19:30 +00:00
|
|
|
if (inlink->type == AVMEDIA_TYPE_AUDIO)
|
2013-03-10 00:30:30 +00:00
|
|
|
select->var_values[VAR_CONSUMED_SAMPLES_N] += frame->nb_samples;
|
2011-05-07 00:06:25 +00:00
|
|
|
}
|
2012-12-11 00:19:30 +00:00
|
|
|
|
2013-02-07 20:16:11 +00: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 00:06:25 +00:00
|
|
|
}
|
|
|
|
|
2013-03-10 00:30:30 +00:00
|
|
|
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
|
2011-05-07 00:06:25 +00:00
|
|
|
{
|
2013-04-08 10:58:56 +00:00
|
|
|
AVFilterContext *ctx = inlink->dst;
|
|
|
|
SelectContext *select = ctx->priv;
|
2011-05-07 00:06:25 +00:00
|
|
|
|
2013-04-08 10:58:56 +00:00
|
|
|
select_frame(ctx, frame);
|
2012-12-10 23:49:37 +00:00
|
|
|
if (select->select)
|
2013-04-08 10:58:56 +00:00
|
|
|
return ff_filter_frame(ctx->outputs[select->select_out], frame);
|
2012-07-08 15:29:42 +00:00
|
|
|
|
2013-03-10 00:30:30 +00:00
|
|
|
av_frame_free(&frame);
|
2012-07-14 07:25:33 +00:00
|
|
|
return 0;
|
2011-05-07 00:06:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int request_frame(AVFilterLink *outlink)
|
|
|
|
{
|
|
|
|
AVFilterLink *inlink = outlink->src->inputs[0];
|
2015-10-02 14:29:08 +00:00
|
|
|
int ret = ff_request_frame(inlink);
|
|
|
|
return ret;
|
2011-05-07 00:06:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold void uninit(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
SelectContext *select = ctx->priv;
|
2013-04-08 10:58:56 +00:00
|
|
|
int i;
|
2011-05-07 00:06:25 +00:00
|
|
|
|
2011-05-07 00:06:25 +00:00
|
|
|
av_expr_free(select->expr);
|
2011-05-07 00:06:25 +00:00
|
|
|
select->expr = NULL;
|
|
|
|
|
2013-04-08 10:58:56 +00:00
|
|
|
for (i = 0; i < ctx->nb_outputs; i++)
|
|
|
|
av_freep(&ctx->output_pads[i].name);
|
|
|
|
|
2012-05-26 10:38:59 +00:00
|
|
|
if (select->do_scene_detect) {
|
2013-03-10 00:30:30 +00:00
|
|
|
av_frame_free(&select->prev_picref);
|
2012-05-26 10:38:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-11 00:19:30 +00:00
|
|
|
#if CONFIG_ASELECT_FILTER
|
2012-12-11 23:21:26 +00:00
|
|
|
|
2013-04-15 19:02:14 +00:00
|
|
|
DEFINE_OPTIONS(aselect, AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM);
|
2012-12-11 23:21:26 +00:00
|
|
|
AVFILTER_DEFINE_CLASS(aselect);
|
|
|
|
|
2013-04-12 09:13:33 +00:00
|
|
|
static av_cold int aselect_init(AVFilterContext *ctx)
|
2012-12-11 23:21:26 +00:00
|
|
|
{
|
|
|
|
SelectContext *select = ctx->priv;
|
|
|
|
int ret;
|
|
|
|
|
2013-04-12 09:13:33 +00:00
|
|
|
if ((ret = init(ctx)) < 0)
|
2012-12-11 23:21:26 +00: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 00:19:30 +00:00
|
|
|
static const AVFilterPad avfilter_af_aselect_inputs[] = {
|
|
|
|
{
|
2013-09-07 12:13:50 +00:00
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
.config_props = config_input,
|
|
|
|
.filter_frame = filter_frame,
|
2012-12-11 00:19:30 +00:00
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
2021-04-19 16:33:56 +00:00
|
|
|
const AVFilter ff_af_aselect = {
|
2013-09-07 12:13:50 +00:00
|
|
|
.name = "aselect",
|
2012-12-11 00:19:30 +00:00
|
|
|
.description = NULL_IF_CONFIG_SMALL("Select audio frames to pass in output."),
|
2013-09-07 12:13:50 +00:00
|
|
|
.init = aselect_init,
|
|
|
|
.uninit = uninit,
|
|
|
|
.priv_size = sizeof(SelectContext),
|
|
|
|
.inputs = avfilter_af_aselect_inputs,
|
|
|
|
.priv_class = &aselect_class,
|
|
|
|
.flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS,
|
2012-12-11 00:19:30 +00:00
|
|
|
};
|
|
|
|
#endif /* CONFIG_ASELECT_FILTER */
|
|
|
|
|
|
|
|
#if CONFIG_SELECT_FILTER
|
2012-12-11 23:21:26 +00:00
|
|
|
|
2019-01-10 19:14:08 +00:00
|
|
|
static int query_formats(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
SelectContext *select = ctx->priv;
|
|
|
|
|
|
|
|
if (!select->do_scene_detect) {
|
|
|
|
return ff_default_query_formats(ctx);
|
|
|
|
} else {
|
|
|
|
int ret;
|
|
|
|
static const enum AVPixelFormat pix_fmts[] = {
|
avformat/f_select: add support for more pixel formats for scene change score calculations
This avoids automatic conversions to rgb if scene change score is used in the expression.
Below is the tested results for the new added pixel format without autoscale to rgb24:
1. AV_PIX_FMT_YUVJ420P
time ./ffprobe -of compact=p=0 -show_entries frame=pkt_pts:frame_tags -bitexact -f lavfi \
"sws_flags=+accurate_rnd+bitexact;movie=../fate-suite/svq3/Vertical400kbit.sorenson3.mov,select=gt(scene\,.25)"
master:
pkt_pts=1620|tag:lavfi.scene_score=1.000000
pkt_pts=4140|tag:lavfi.scene_score=0.875036
pkt_pts=5800|tag:lavfi.scene_score=1.000000
pkt_pts=6720|tag:lavfi.scene_score=0.461625
pkt_pts=8160|tag:lavfi.scene_score=1.000000
pkt_pts=9760|tag:lavfi.scene_score=1.000000
pkt_pts=14080|tag:lavfi.scene_score=0.838916
pkt_pts=15700|tag:lavfi.scene_score=1.000000
pkt_pts=18500|tag:lavfi.scene_score=0.474948
pkt_pts=20040|tag:lavfi.scene_score=0.379700
pkt_pts=21760|tag:lavfi.scene_score=1.000000
./ffprobe -of compact=p=0 -show_entries frame=pkt_pts:frame_tags -bitexact -f 0.71s user 0.01s system 99% cpu 0.721 total
patch applied:
pkt_pts=1620|tag:lavfi.scene_score=1.000000
pkt_pts=4140|tag:lavfi.scene_score=0.668643
pkt_pts=5800|tag:lavfi.scene_score=0.996721
pkt_pts=6720|tag:lavfi.scene_score=0.357390
pkt_pts=8160|tag:lavfi.scene_score=0.886268
pkt_pts=9760|tag:lavfi.scene_score=0.926219
pkt_pts=14080|tag:lavfi.scene_score=0.650033
pkt_pts=15700|tag:lavfi.scene_score=1.000000
pkt_pts=18500|tag:lavfi.scene_score=0.316402
pkt_pts=20040|tag:lavfi.scene_score=0.269509
pkt_pts=21760|tag:lavfi.scene_score=1.000000
./ffprobe -of compact=p=0 -show_entries frame=pkt_pts:frame_tags -bitexact -f 0.19s user 0.01s system 81% cpu 0.240 total
2. AV_PIX_FMT_YUV420P
time ./ffprobe -of compact=p=0 -show_entries frame=pkt_pts:frame_tags -bitexact -f lavfi \
"sws_flags=+accurate_rnd+bitexact;movie=../Passengers_Breakfast_1080-sdr.mkv,select=gt(scene\,.2)"
master:
pkt_pts=3587|tag:lavfi.scene_score=0.462364
pkt_pts=4838|tag:lavfi.scene_score=0.419519
pkt_pts=6548|tag:lavfi.scene_score=0.397027
pkt_pts=9968|tag:lavfi.scene_score=0.419245
pkt_pts=12471|tag:lavfi.scene_score=0.413084
pkt_pts=16225|tag:lavfi.scene_score=0.506370
pkt_pts=19645|tag:lavfi.scene_score=0.507538
pkt_pts=22314|tag:lavfi.scene_score=0.504319
pkt_pts=24817|tag:lavfi.scene_score=0.417544
pkt_pts=25651|tag:lavfi.scene_score=0.413916
pkt_pts=26652|tag:lavfi.scene_score=0.487707
18.58s user 0.07s system 99% cpu 18.663 total
patch applied:
pkt_pts=3587|tag:lavfi.scene_score=0.272173
pkt_pts=4838|tag:lavfi.scene_score=0.247841
pkt_pts=6548|tag:lavfi.scene_score=0.233134
pkt_pts=9968|tag:lavfi.scene_score=0.247253
pkt_pts=12471|tag:lavfi.scene_score=0.244129
pkt_pts=16225|tag:lavfi.scene_score=0.302531
pkt_pts=19645|tag:lavfi.scene_score=0.303560
pkt_pts=22314|tag:lavfi.scene_score=0.301861
pkt_pts=24817|tag:lavfi.scene_score=0.249331
pkt_pts=25651|tag:lavfi.scene_score=0.247096
pkt_pts=26652|tag:lavfi.scene_score=0.287728
10.90s user 0.06s system 99% cpu 10.967 total
3. AV_PIX_FMT_YUV422P
time ./ffprobe -of compact=p=0 -show_entries frame=pkt_pts:frame_tags -bitexact -f lavfi \
"sws_flags=+accurate_rnd+bitexact;movie=../Passengers_Breakfast_1080-sdr.mkv,format=yuv422p,select=gt(scene\,.2)"
master:
patched applied:
pkt_pts=3587|tag:lavfi.scene_score=0.224017
pkt_pts=4838|tag:lavfi.scene_score=0.204225
pkt_pts=9968|tag:lavfi.scene_score=0.204636
pkt_pts=12471|tag:lavfi.scene_score=0.202772
pkt_pts=16225|tag:lavfi.scene_score=0.248765
pkt_pts=19645|tag:lavfi.scene_score=0.250144
pkt_pts=22314|tag:lavfi.scene_score=0.248802
pkt_pts=24817|tag:lavfi.scene_score=0.208362
pkt_pts=25651|tag:lavfi.scene_score=0.205777
pkt_pts=26652|tag:lavfi.scene_score=0.230742
4. AV_PIX_FMT_YUV420P10
time ./ffprobe -of compact=p=0 -show_entries frame=pkt_pts:frame_tags -bitexact -f lavfi \
"sws_flags=+accurate_rnd+bitexact;movie=../Passengers_Breakfast_4k.mkv,select=gt(scene\,.2)"
master:
pkt_pts=3587|tag:lavfi.scene_score=0.269890
pkt_pts=4838|tag:lavfi.scene_score=0.248957
pkt_pts=6548|tag:lavfi.scene_score=0.234619
pkt_pts=9969|tag:lavfi.scene_score=0.224912
pkt_pts=12471|tag:lavfi.scene_score=0.225158
pkt_pts=16225|tag:lavfi.scene_score=0.289809
pkt_pts=19645|tag:lavfi.scene_score=0.285013
pkt_pts=22314|tag:lavfi.scene_score=0.280295
pkt_pts=24817|tag:lavfi.scene_score=0.206486
pkt_pts=25651|tag:lavfi.scene_score=0.208556
pkt_pts=26652|tag:lavfi.scene_score=0.249577
./ffprobe -of compact=p=0 -show_entries frame=pkt_pts:frame_tags -bitexact -f 76.03s user 0.22s system 99% cpu 1:16.27 total
patch applied
pkt_pts=3587|tag:lavfi.scene_score=0.269890
pkt_pts=4838|tag:lavfi.scene_score=0.248957
pkt_pts=6548|tag:lavfi.scene_score=0.234619
pkt_pts=9969|tag:lavfi.scene_score=0.224912
pkt_pts=12471|tag:lavfi.scene_score=0.225158
pkt_pts=16225|tag:lavfi.scene_score=0.289809
pkt_pts=19645|tag:lavfi.scene_score=0.285013
pkt_pts=22314|tag:lavfi.scene_score=0.280295
pkt_pts=24817|tag:lavfi.scene_score=0.206486
pkt_pts=25651|tag:lavfi.scene_score=0.208556
pkt_pts=26652|tag:lavfi.scene_score=0.249577
./ffprobe -of compact=p=0 -show_entries frame=pkt_pts:frame_tags -bitexact -f 50.27s user 0.20s system 99% cpu 50.476 total
5. AV_PIX_FMT_RGBA, AV_PIX_FMT_ABGR, AV_PIX_FMT_BGRA, AV_PIX_FMT_GRAY8
are tested by format= with the fate sample: Vertical400kbit.sorenson3.mov like below:
time ./ffprobe -of compact=p=0 -show_entries frame=pkt_pts:frame_tags -bitexact -f lavfi \
"sws_flags=+accurate_rnd+bitexact;movie=../fate-suite/svq3/Vertical400kbit.sorenson3.mov,format=rgba,select=gt(scene\,.25)"
patch applied:
pkt_pts=1620|tag:lavfi.scene_score=1.000000
pkt_pts=4140|tag:lavfi.scene_score=0.656277
pkt_pts=5800|tag:lavfi.scene_score=1.000000
pkt_pts=6720|tag:lavfi.scene_score=0.346218
pkt_pts=8160|tag:lavfi.scene_score=0.987686
pkt_pts=9760|tag:lavfi.scene_score=1.000000
pkt_pts=14080|tag:lavfi.scene_score=0.629187
pkt_pts=15700|tag:lavfi.scene_score=1.000000
pkt_pts=18500|tag:lavfi.scene_score=0.356211
pkt_pts=20040|tag:lavfi.scene_score=0.284775
pkt_pts=21760|tag:lavfi.scene_score=1.000000
6. AV_PIX_FMT_YUVJ422P
time ./ffprobe -of compact=p=0 -show_entries frame=pkt_pts:frame_tags -bitexact -f lavfi \
"sws_flags=+accurate_rnd+bitexact;movie=../fate-suite/svq3/Vertical400kbit.sorenson3.mov,format=yuvj422p,select=gt(scene\,.25)"
patch applied:
pkt_pts=1620|tag:lavfi.scene_score=0.838281
pkt_pts=4140|tag:lavfi.scene_score=0.541382
pkt_pts=5800|tag:lavfi.scene_score=0.780588
pkt_pts=6720|tag:lavfi.scene_score=0.298274
pkt_pts=8160|tag:lavfi.scene_score=0.699106
pkt_pts=9760|tag:lavfi.scene_score=0.730136
pkt_pts=14080|tag:lavfi.scene_score=0.537742
pkt_pts=15700|tag:lavfi.scene_score=0.811946
pkt_pts=18500|tag:lavfi.scene_score=0.263382
pkt_pts=21760|tag:lavfi.scene_score=0.880773
Reviewed-by: Marton Balint <cus@passwd.hu>
Signed-off-by: Limin Wang <lance.lmwang@gmail.com>
2019-07-20 23:27:20 +00:00
|
|
|
AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24, AV_PIX_FMT_RGBA,
|
|
|
|
AV_PIX_FMT_ABGR, AV_PIX_FMT_BGRA, AV_PIX_FMT_GRAY8,
|
|
|
|
AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVJ420P,
|
|
|
|
AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVJ422P,
|
|
|
|
AV_PIX_FMT_YUV420P10,
|
2019-01-10 19:14:08 +00:00
|
|
|
AV_PIX_FMT_NONE
|
|
|
|
};
|
|
|
|
AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
|
|
|
|
|
|
|
|
if (!fmts_list)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
ret = ff_set_common_formats(ctx, fmts_list);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-04-15 19:02:14 +00:00
|
|
|
DEFINE_OPTIONS(select, AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM);
|
2012-12-11 23:21:26 +00:00
|
|
|
AVFILTER_DEFINE_CLASS(select);
|
|
|
|
|
2013-04-12 09:13:33 +00:00
|
|
|
static av_cold int select_init(AVFilterContext *ctx)
|
2012-12-11 23:21:26 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2013-04-12 09:13:33 +00:00
|
|
|
if ((ret = init(ctx)) < 0)
|
2012-12-11 23:21:26 +00:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-07-24 13:14:01 +00:00
|
|
|
static const AVFilterPad avfilter_vf_select_inputs[] = {
|
|
|
|
{
|
2013-09-07 12:13:50 +00:00
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.config_props = config_input,
|
|
|
|
.filter_frame = filter_frame,
|
2012-07-24 13:14:01 +00:00
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
2021-04-19 16:33:56 +00:00
|
|
|
const AVFilter ff_vf_select = {
|
2013-09-07 12:13:50 +00:00
|
|
|
.name = "select",
|
|
|
|
.description = NULL_IF_CONFIG_SMALL("Select video frames to pass in output."),
|
|
|
|
.init = select_init,
|
|
|
|
.uninit = uninit,
|
2012-05-26 10:38:59 +00:00
|
|
|
.query_formats = query_formats,
|
2013-09-07 12:13:50 +00:00
|
|
|
.priv_size = sizeof(SelectContext),
|
|
|
|
.priv_class = &select_class,
|
|
|
|
.inputs = avfilter_vf_select_inputs,
|
|
|
|
.flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS,
|
2011-05-07 00:06:25 +00:00
|
|
|
};
|
2012-12-11 00:19:30 +00:00
|
|
|
#endif /* CONFIG_SELECT_FILTER */
|