2012-08-02 09:24:02 +03:00
|
|
|
/*
|
|
|
|
* avconv filter configuration
|
|
|
|
*
|
|
|
|
* This file is part of Libav.
|
|
|
|
*
|
|
|
|
* Libav 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.
|
|
|
|
*
|
|
|
|
* Libav 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 Libav; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
2013-11-23 23:32:55 +03:00
|
|
|
#include <stdint.h>
|
|
|
|
|
2012-08-02 09:24:02 +03:00
|
|
|
#include "avconv.h"
|
|
|
|
|
|
|
|
#include "libavfilter/avfilter.h"
|
2016-02-12 10:22:15 +02:00
|
|
|
#include "libavfilter/buffersrc.h"
|
2012-08-02 09:24:02 +03:00
|
|
|
|
2012-12-19 05:47:28 +03:00
|
|
|
#include "libavresample/avresample.h"
|
|
|
|
|
2012-08-02 09:24:02 +03:00
|
|
|
#include "libavutil/avassert.h"
|
2012-12-19 05:47:28 +03:00
|
|
|
#include "libavutil/avstring.h"
|
2012-11-10 18:00:00 +03:00
|
|
|
#include "libavutil/channel_layout.h"
|
2015-04-29 16:59:01 +02:00
|
|
|
#include "libavutil/display.h"
|
2012-12-19 05:47:28 +03:00
|
|
|
#include "libavutil/opt.h"
|
2012-08-02 09:24:02 +03:00
|
|
|
#include "libavutil/pixdesc.h"
|
|
|
|
#include "libavutil/pixfmt.h"
|
|
|
|
#include "libavutil/samplefmt.h"
|
|
|
|
|
2012-08-24 14:31:50 +03:00
|
|
|
/* Define a function for building a string containing a list of
|
|
|
|
* allowed formats. */
|
2013-03-12 21:31:08 +03:00
|
|
|
#define DEF_CHOOSE_FORMAT(type, var, supported_list, none, get_name) \
|
2012-08-02 09:24:02 +03:00
|
|
|
static char *choose_ ## var ## s(OutputStream *ost) \
|
|
|
|
{ \
|
2014-04-05 09:48:24 +03:00
|
|
|
if (ost->enc_ctx->var != none) { \
|
|
|
|
get_name(ost->enc_ctx->var); \
|
2012-08-02 09:24:02 +03:00
|
|
|
return av_strdup(name); \
|
2013-03-06 16:01:36 +03:00
|
|
|
} else if (ost->enc && ost->enc->supported_list) { \
|
2012-08-02 09:24:02 +03:00
|
|
|
const type *p; \
|
|
|
|
AVIOContext *s = NULL; \
|
|
|
|
uint8_t *ret; \
|
|
|
|
int len; \
|
|
|
|
\
|
|
|
|
if (avio_open_dyn_buf(&s) < 0) \
|
2012-09-05 10:03:56 +03:00
|
|
|
exit(1); \
|
2012-08-02 09:24:02 +03:00
|
|
|
\
|
|
|
|
for (p = ost->enc->supported_list; *p != none; p++) { \
|
|
|
|
get_name(*p); \
|
2013-03-12 21:31:08 +03:00
|
|
|
avio_printf(s, "%s|", name); \
|
2012-08-02 09:24:02 +03:00
|
|
|
} \
|
|
|
|
len = avio_close_dyn_buf(s, &ret); \
|
|
|
|
ret[len - 1] = 0; \
|
|
|
|
return ret; \
|
|
|
|
} else \
|
|
|
|
return NULL; \
|
|
|
|
}
|
|
|
|
|
2012-10-06 13:10:34 +03:00
|
|
|
DEF_CHOOSE_FORMAT(enum AVPixelFormat, pix_fmt, pix_fmts, AV_PIX_FMT_NONE,
|
2013-03-12 21:31:08 +03:00
|
|
|
GET_PIX_FMT_NAME)
|
2012-08-02 09:24:02 +03:00
|
|
|
|
|
|
|
DEF_CHOOSE_FORMAT(enum AVSampleFormat, sample_fmt, sample_fmts,
|
2013-03-12 21:31:08 +03:00
|
|
|
AV_SAMPLE_FMT_NONE, GET_SAMPLE_FMT_NAME)
|
2012-08-02 09:24:02 +03:00
|
|
|
|
|
|
|
DEF_CHOOSE_FORMAT(int, sample_rate, supported_samplerates, 0,
|
2013-03-12 21:31:08 +03:00
|
|
|
GET_SAMPLE_RATE_NAME)
|
2012-08-02 09:24:02 +03:00
|
|
|
|
|
|
|
DEF_CHOOSE_FORMAT(uint64_t, channel_layout, channel_layouts, 0,
|
2013-03-12 21:31:08 +03:00
|
|
|
GET_CH_LAYOUT_NAME)
|
2012-08-02 09:24:02 +03:00
|
|
|
|
|
|
|
FilterGraph *init_simple_filtergraph(InputStream *ist, OutputStream *ost)
|
|
|
|
{
|
|
|
|
FilterGraph *fg = av_mallocz(sizeof(*fg));
|
|
|
|
|
|
|
|
if (!fg)
|
2012-09-05 10:03:56 +03:00
|
|
|
exit(1);
|
2012-08-02 09:24:02 +03:00
|
|
|
fg->index = nb_filtergraphs;
|
|
|
|
|
2012-06-07 22:52:07 +03:00
|
|
|
GROW_ARRAY(fg->outputs, fg->nb_outputs);
|
2012-08-02 09:24:02 +03:00
|
|
|
if (!(fg->outputs[0] = av_mallocz(sizeof(*fg->outputs[0]))))
|
2012-09-05 10:03:56 +03:00
|
|
|
exit(1);
|
2012-08-02 09:24:02 +03:00
|
|
|
fg->outputs[0]->ost = ost;
|
|
|
|
fg->outputs[0]->graph = fg;
|
|
|
|
|
|
|
|
ost->filter = fg->outputs[0];
|
|
|
|
|
2012-06-07 22:52:07 +03:00
|
|
|
GROW_ARRAY(fg->inputs, fg->nb_inputs);
|
2012-08-02 09:24:02 +03:00
|
|
|
if (!(fg->inputs[0] = av_mallocz(sizeof(*fg->inputs[0]))))
|
2012-09-05 10:03:56 +03:00
|
|
|
exit(1);
|
2012-08-02 09:24:02 +03:00
|
|
|
fg->inputs[0]->ist = ist;
|
|
|
|
fg->inputs[0]->graph = fg;
|
|
|
|
|
2012-06-07 22:52:07 +03:00
|
|
|
GROW_ARRAY(ist->filters, ist->nb_filters);
|
2012-08-02 09:24:02 +03:00
|
|
|
ist->filters[ist->nb_filters - 1] = fg->inputs[0];
|
|
|
|
|
2012-06-07 22:52:07 +03:00
|
|
|
GROW_ARRAY(filtergraphs, nb_filtergraphs);
|
2012-08-02 09:24:02 +03:00
|
|
|
filtergraphs[nb_filtergraphs - 1] = fg;
|
|
|
|
|
|
|
|
return fg;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void init_input_filter(FilterGraph *fg, AVFilterInOut *in)
|
|
|
|
{
|
|
|
|
InputStream *ist = NULL;
|
|
|
|
enum AVMediaType type = avfilter_pad_get_type(in->filter_ctx->input_pads, in->pad_idx);
|
|
|
|
int i;
|
|
|
|
|
|
|
|
// TODO: support other filter types
|
|
|
|
if (type != AVMEDIA_TYPE_VIDEO && type != AVMEDIA_TYPE_AUDIO) {
|
|
|
|
av_log(NULL, AV_LOG_FATAL, "Only video and audio filters supported "
|
|
|
|
"currently.\n");
|
2012-09-05 10:03:56 +03:00
|
|
|
exit(1);
|
2012-08-02 09:24:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (in->name) {
|
|
|
|
AVFormatContext *s;
|
|
|
|
AVStream *st = NULL;
|
|
|
|
char *p;
|
|
|
|
int file_idx = strtol(in->name, &p, 0);
|
|
|
|
|
|
|
|
if (file_idx < 0 || file_idx >= nb_input_files) {
|
|
|
|
av_log(NULL, AV_LOG_FATAL, "Invalid file index %d in filtegraph description %s.\n",
|
|
|
|
file_idx, fg->graph_desc);
|
2012-09-05 10:03:56 +03:00
|
|
|
exit(1);
|
2012-08-02 09:24:02 +03:00
|
|
|
}
|
|
|
|
s = input_files[file_idx]->ctx;
|
|
|
|
|
|
|
|
for (i = 0; i < s->nb_streams; i++) {
|
2016-01-06 10:18:21 +02:00
|
|
|
if (s->streams[i]->codecpar->codec_type != type)
|
2012-08-02 09:24:02 +03:00
|
|
|
continue;
|
|
|
|
if (check_stream_specifier(s, s->streams[i], *p == ':' ? p + 1 : p) == 1) {
|
|
|
|
st = s->streams[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!st) {
|
|
|
|
av_log(NULL, AV_LOG_FATAL, "Stream specifier '%s' in filtergraph description %s "
|
|
|
|
"matches no streams.\n", p, fg->graph_desc);
|
2012-09-05 10:03:56 +03:00
|
|
|
exit(1);
|
2012-08-02 09:24:02 +03:00
|
|
|
}
|
|
|
|
ist = input_streams[input_files[file_idx]->ist_index + st->index];
|
|
|
|
} else {
|
|
|
|
/* find the first unused stream of corresponding type */
|
|
|
|
for (i = 0; i < nb_input_streams; i++) {
|
|
|
|
ist = input_streams[i];
|
2014-04-05 09:48:24 +03:00
|
|
|
if (ist->dec_ctx->codec_type == type && ist->discard)
|
2012-08-02 09:24:02 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (i == nb_input_streams) {
|
|
|
|
av_log(NULL, AV_LOG_FATAL, "Cannot find a matching stream for "
|
2013-11-20 19:58:09 +03:00
|
|
|
"unlabeled input pad %d on filter %s\n", in->pad_idx,
|
2012-08-02 09:24:02 +03:00
|
|
|
in->filter_ctx->name);
|
2012-09-05 10:03:56 +03:00
|
|
|
exit(1);
|
2012-08-02 09:24:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
av_assert0(ist);
|
|
|
|
|
|
|
|
ist->discard = 0;
|
|
|
|
ist->decoding_needed = 1;
|
|
|
|
ist->st->discard = AVDISCARD_NONE;
|
|
|
|
|
2012-06-07 22:52:07 +03:00
|
|
|
GROW_ARRAY(fg->inputs, fg->nb_inputs);
|
2012-08-02 09:24:02 +03:00
|
|
|
if (!(fg->inputs[fg->nb_inputs - 1] = av_mallocz(sizeof(*fg->inputs[0]))))
|
2012-09-05 10:03:56 +03:00
|
|
|
exit(1);
|
2012-08-02 09:24:02 +03:00
|
|
|
fg->inputs[fg->nb_inputs - 1]->ist = ist;
|
|
|
|
fg->inputs[fg->nb_inputs - 1]->graph = fg;
|
|
|
|
|
2012-06-07 22:52:07 +03:00
|
|
|
GROW_ARRAY(ist->filters, ist->nb_filters);
|
2012-08-02 09:24:02 +03:00
|
|
|
ist->filters[ist->nb_filters - 1] = fg->inputs[fg->nb_inputs - 1];
|
|
|
|
}
|
|
|
|
|
2015-07-15 19:49:24 +02:00
|
|
|
int init_complex_filtergraph(FilterGraph *fg)
|
|
|
|
{
|
|
|
|
AVFilterInOut *inputs, *outputs, *cur;
|
|
|
|
AVFilterGraph *graph;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
/* this graph is only used for determining the kinds of inputs
|
|
|
|
* and outputs we have, and is discarded on exit from this function */
|
|
|
|
graph = avfilter_graph_alloc();
|
|
|
|
if (!graph)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
ret = avfilter_graph_parse2(graph, fg->graph_desc, &inputs, &outputs);
|
|
|
|
if (ret < 0)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
for (cur = inputs; cur; cur = cur->next)
|
|
|
|
init_input_filter(fg, cur);
|
|
|
|
|
|
|
|
for (cur = outputs; cur;) {
|
|
|
|
GROW_ARRAY(fg->outputs, fg->nb_outputs);
|
|
|
|
fg->outputs[fg->nb_outputs - 1] = av_mallocz(sizeof(*fg->outputs[0]));
|
|
|
|
if (!fg->outputs[fg->nb_outputs - 1])
|
|
|
|
exit(1);
|
|
|
|
|
|
|
|
fg->outputs[fg->nb_outputs - 1]->graph = fg;
|
|
|
|
fg->outputs[fg->nb_outputs - 1]->out_tmp = cur;
|
|
|
|
fg->outputs[fg->nb_outputs - 1]->type = avfilter_pad_get_type(cur->filter_ctx->output_pads,
|
|
|
|
cur->pad_idx);
|
|
|
|
cur = cur->next;
|
|
|
|
fg->outputs[fg->nb_outputs - 1]->out_tmp->next = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
fail:
|
|
|
|
avfilter_inout_free(&inputs);
|
|
|
|
avfilter_graph_free(&graph);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-06-15 10:59:40 +03:00
|
|
|
static int insert_trim(int64_t start_time, int64_t duration,
|
|
|
|
AVFilterContext **last_filter, int *pad_idx,
|
|
|
|
const char *filter_name)
|
2013-04-10 16:02:01 +03:00
|
|
|
{
|
|
|
|
AVFilterGraph *graph = (*last_filter)->graph;
|
|
|
|
AVFilterContext *ctx;
|
|
|
|
const AVFilter *trim;
|
2013-06-15 10:59:40 +03:00
|
|
|
enum AVMediaType type = avfilter_pad_get_type((*last_filter)->output_pads, *pad_idx);
|
|
|
|
const char *name = (type == AVMEDIA_TYPE_VIDEO) ? "trim" : "atrim";
|
2013-04-10 16:02:01 +03:00
|
|
|
int ret = 0;
|
|
|
|
|
2013-06-15 10:59:40 +03:00
|
|
|
if (duration == INT64_MAX && start_time == AV_NOPTS_VALUE)
|
2013-04-10 16:02:01 +03:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
trim = avfilter_get_by_name(name);
|
|
|
|
if (!trim) {
|
|
|
|
av_log(NULL, AV_LOG_ERROR, "%s filter not present, cannot limit "
|
|
|
|
"recording time.\n", name);
|
|
|
|
return AVERROR_FILTER_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx = avfilter_graph_alloc_filter(graph, trim, filter_name);
|
|
|
|
if (!ctx)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2013-06-15 10:59:40 +03:00
|
|
|
if (duration != INT64_MAX) {
|
|
|
|
ret = av_opt_set_double(ctx, "duration", (double)duration / 1e6,
|
2013-04-10 16:02:01 +03:00
|
|
|
AV_OPT_SEARCH_CHILDREN);
|
|
|
|
}
|
2013-06-15 10:59:40 +03:00
|
|
|
if (ret >= 0 && start_time != AV_NOPTS_VALUE) {
|
|
|
|
ret = av_opt_set_double(ctx, "start", (double)start_time / 1e6,
|
2013-04-10 16:02:01 +03:00
|
|
|
AV_OPT_SEARCH_CHILDREN);
|
|
|
|
}
|
2013-04-10 16:02:01 +03:00
|
|
|
if (ret < 0) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Error configuring the %s filter", name);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = avfilter_init_str(ctx, NULL);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = avfilter_link(*last_filter, *pad_idx, ctx, 0);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
*last_filter = ctx;
|
|
|
|
*pad_idx = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-04-29 16:59:01 +02:00
|
|
|
static int insert_filter(AVFilterContext **last_filter, int *pad_idx,
|
|
|
|
const char *filter_name, const char *args)
|
|
|
|
{
|
|
|
|
AVFilterGraph *graph = (*last_filter)->graph;
|
|
|
|
AVFilterContext *ctx;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = avfilter_graph_create_filter(&ctx,
|
|
|
|
avfilter_get_by_name(filter_name),
|
|
|
|
filter_name, args, NULL, graph);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = avfilter_link(*last_filter, *pad_idx, ctx, 0);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
*last_filter = ctx;
|
|
|
|
*pad_idx = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-08-02 09:24:02 +03:00
|
|
|
static int configure_output_video_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
|
|
|
|
{
|
|
|
|
char *pix_fmts;
|
|
|
|
OutputStream *ost = ofilter->ost;
|
2013-06-15 10:59:40 +03:00
|
|
|
OutputFile *of = output_files[ost->file_index];
|
2014-04-05 09:48:24 +03:00
|
|
|
AVCodecContext *codec = ost->enc_ctx;
|
2012-08-02 09:24:02 +03:00
|
|
|
AVFilterContext *last_filter = out->filter_ctx;
|
|
|
|
int pad_idx = out->pad_idx;
|
|
|
|
int ret;
|
|
|
|
char name[255];
|
|
|
|
|
|
|
|
snprintf(name, sizeof(name), "output stream %d:%d", ost->file_index, ost->index);
|
|
|
|
ret = avfilter_graph_create_filter(&ofilter->filter,
|
|
|
|
avfilter_get_by_name("buffersink"),
|
2012-08-18 17:15:42 +03:00
|
|
|
name, NULL, NULL, fg->graph);
|
2012-08-02 09:24:02 +03:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (codec->width || codec->height) {
|
|
|
|
char args[255];
|
|
|
|
AVFilterContext *filter;
|
|
|
|
|
2013-05-10 03:37:14 +03:00
|
|
|
snprintf(args, sizeof(args), "%d:%d:0x%X",
|
2012-08-02 09:24:02 +03:00
|
|
|
codec->width,
|
|
|
|
codec->height,
|
|
|
|
(unsigned)ost->sws_flags);
|
|
|
|
snprintf(name, sizeof(name), "scaler for output stream %d:%d",
|
|
|
|
ost->file_index, ost->index);
|
|
|
|
if ((ret = avfilter_graph_create_filter(&filter, avfilter_get_by_name("scale"),
|
|
|
|
name, args, NULL, fg->graph)) < 0)
|
|
|
|
return ret;
|
|
|
|
if ((ret = avfilter_link(last_filter, pad_idx, filter, 0)) < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
last_filter = filter;
|
|
|
|
pad_idx = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((pix_fmts = choose_pix_fmts(ost))) {
|
|
|
|
AVFilterContext *filter;
|
|
|
|
snprintf(name, sizeof(name), "pixel format for output stream %d:%d",
|
|
|
|
ost->file_index, ost->index);
|
2014-10-14 18:46:44 +03:00
|
|
|
ret = avfilter_graph_create_filter(&filter,
|
|
|
|
avfilter_get_by_name("format"),
|
|
|
|
"format", pix_fmts, NULL, fg->graph);
|
|
|
|
av_freep(&pix_fmts);
|
|
|
|
if (ret < 0)
|
2012-08-02 09:24:02 +03:00
|
|
|
return ret;
|
|
|
|
if ((ret = avfilter_link(last_filter, pad_idx, filter, 0)) < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
last_filter = filter;
|
|
|
|
pad_idx = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ost->frame_rate.num) {
|
|
|
|
AVFilterContext *fps;
|
|
|
|
char args[255];
|
|
|
|
|
|
|
|
snprintf(args, sizeof(args), "fps=%d/%d", ost->frame_rate.num,
|
|
|
|
ost->frame_rate.den);
|
|
|
|
snprintf(name, sizeof(name), "fps for output stream %d:%d",
|
|
|
|
ost->file_index, ost->index);
|
|
|
|
ret = avfilter_graph_create_filter(&fps, avfilter_get_by_name("fps"),
|
|
|
|
name, args, NULL, fg->graph);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = avfilter_link(last_filter, pad_idx, fps, 0);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
last_filter = fps;
|
|
|
|
pad_idx = 0;
|
|
|
|
}
|
|
|
|
|
2013-06-15 10:59:40 +03:00
|
|
|
snprintf(name, sizeof(name), "trim for output stream %d:%d",
|
|
|
|
ost->file_index, ost->index);
|
|
|
|
ret = insert_trim(of->start_time, of->recording_time,
|
|
|
|
&last_filter, &pad_idx, name);
|
2013-04-10 16:02:01 +03:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
|
2012-08-02 09:24:02 +03:00
|
|
|
if ((ret = avfilter_link(last_filter, pad_idx, ofilter->filter, 0)) < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int configure_output_audio_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
|
|
|
|
{
|
|
|
|
OutputStream *ost = ofilter->ost;
|
2013-06-15 10:59:40 +03:00
|
|
|
OutputFile *of = output_files[ost->file_index];
|
2014-04-05 09:48:24 +03:00
|
|
|
AVCodecContext *codec = ost->enc_ctx;
|
2012-08-02 09:24:02 +03:00
|
|
|
AVFilterContext *last_filter = out->filter_ctx;
|
|
|
|
int pad_idx = out->pad_idx;
|
|
|
|
char *sample_fmts, *sample_rates, *channel_layouts;
|
|
|
|
char name[255];
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
|
|
|
|
snprintf(name, sizeof(name), "output stream %d:%d", ost->file_index, ost->index);
|
|
|
|
ret = avfilter_graph_create_filter(&ofilter->filter,
|
|
|
|
avfilter_get_by_name("abuffersink"),
|
|
|
|
name, NULL, NULL, fg->graph);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (codec->channels && !codec->channel_layout)
|
|
|
|
codec->channel_layout = av_get_default_channel_layout(codec->channels);
|
|
|
|
|
|
|
|
sample_fmts = choose_sample_fmts(ost);
|
|
|
|
sample_rates = choose_sample_rates(ost);
|
|
|
|
channel_layouts = choose_channel_layouts(ost);
|
|
|
|
if (sample_fmts || sample_rates || channel_layouts) {
|
|
|
|
AVFilterContext *format;
|
|
|
|
char args[256];
|
|
|
|
int len = 0;
|
|
|
|
|
|
|
|
if (sample_fmts)
|
|
|
|
len += snprintf(args + len, sizeof(args) - len, "sample_fmts=%s:",
|
|
|
|
sample_fmts);
|
|
|
|
if (sample_rates)
|
|
|
|
len += snprintf(args + len, sizeof(args) - len, "sample_rates=%s:",
|
|
|
|
sample_rates);
|
|
|
|
if (channel_layouts)
|
|
|
|
len += snprintf(args + len, sizeof(args) - len, "channel_layouts=%s:",
|
|
|
|
channel_layouts);
|
|
|
|
args[len - 1] = 0;
|
|
|
|
|
|
|
|
av_freep(&sample_fmts);
|
|
|
|
av_freep(&sample_rates);
|
|
|
|
av_freep(&channel_layouts);
|
|
|
|
|
|
|
|
snprintf(name, sizeof(name), "audio format for output stream %d:%d",
|
|
|
|
ost->file_index, ost->index);
|
|
|
|
ret = avfilter_graph_create_filter(&format,
|
|
|
|
avfilter_get_by_name("aformat"),
|
|
|
|
name, args, NULL, fg->graph);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = avfilter_link(last_filter, pad_idx, format, 0);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
last_filter = format;
|
|
|
|
pad_idx = 0;
|
|
|
|
}
|
|
|
|
|
2013-06-15 10:59:40 +03:00
|
|
|
snprintf(name, sizeof(name), "trim for output stream %d:%d",
|
|
|
|
ost->file_index, ost->index);
|
|
|
|
ret = insert_trim(of->start_time, of->recording_time,
|
|
|
|
&last_filter, &pad_idx, name);
|
2013-04-10 16:02:01 +03:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2012-08-02 09:24:02 +03:00
|
|
|
if ((ret = avfilter_link(last_filter, pad_idx, ofilter->filter, 0)) < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define DESCRIBE_FILTER_LINK(f, inout, in) \
|
|
|
|
{ \
|
|
|
|
AVFilterContext *ctx = inout->filter_ctx; \
|
|
|
|
AVFilterPad *pads = in ? ctx->input_pads : ctx->output_pads; \
|
2013-08-04 17:38:25 +03:00
|
|
|
int nb_pads = in ? ctx->nb_inputs : ctx->nb_outputs; \
|
2012-08-02 09:24:02 +03:00
|
|
|
AVIOContext *pb; \
|
|
|
|
\
|
|
|
|
if (avio_open_dyn_buf(&pb) < 0) \
|
2012-09-05 10:03:56 +03:00
|
|
|
exit(1); \
|
2012-08-02 09:24:02 +03:00
|
|
|
\
|
|
|
|
avio_printf(pb, "%s", ctx->filter->name); \
|
|
|
|
if (nb_pads > 1) \
|
|
|
|
avio_printf(pb, ":%s", avfilter_pad_get_name(pads, inout->pad_idx));\
|
|
|
|
avio_w8(pb, 0); \
|
|
|
|
avio_close_dyn_buf(pb, &f->name); \
|
|
|
|
}
|
|
|
|
|
|
|
|
int configure_output_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
|
|
|
|
{
|
|
|
|
av_freep(&ofilter->name);
|
|
|
|
DESCRIBE_FILTER_LINK(ofilter, out, 0);
|
|
|
|
|
|
|
|
switch (avfilter_pad_get_type(out->filter_ctx->output_pads, out->pad_idx)) {
|
|
|
|
case AVMEDIA_TYPE_VIDEO: return configure_output_video_filter(fg, ofilter, out);
|
|
|
|
case AVMEDIA_TYPE_AUDIO: return configure_output_audio_filter(fg, ofilter, out);
|
|
|
|
default: av_assert0(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int configure_input_video_filter(FilterGraph *fg, InputFilter *ifilter,
|
|
|
|
AVFilterInOut *in)
|
|
|
|
{
|
2013-06-14 19:25:30 +03:00
|
|
|
AVFilterContext *last_filter;
|
2013-06-14 19:12:23 +03:00
|
|
|
const AVFilter *buffer_filt = avfilter_get_by_name("buffer");
|
2012-08-02 09:24:02 +03:00
|
|
|
InputStream *ist = ifilter->ist;
|
2013-06-15 10:59:40 +03:00
|
|
|
InputFile *f = input_files[ist->file_index];
|
2012-08-02 09:24:02 +03:00
|
|
|
AVRational tb = ist->framerate.num ? av_inv_q(ist->framerate) :
|
|
|
|
ist->st->time_base;
|
2016-02-12 10:22:15 +02:00
|
|
|
AVBufferSrcParameters *par;
|
|
|
|
char name[255];
|
2013-06-15 10:59:40 +03:00
|
|
|
int ret, pad_idx = 0;
|
2012-08-02 09:24:02 +03:00
|
|
|
|
|
|
|
snprintf(name, sizeof(name), "graph %d input from stream %d:%d", fg->index,
|
|
|
|
ist->file_index, ist->st->index);
|
|
|
|
|
2016-02-12 10:22:15 +02:00
|
|
|
ifilter->filter = avfilter_graph_alloc_filter(fg->graph, buffer_filt, name);
|
|
|
|
if (!ifilter->filter)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
par = av_buffersrc_parameters_alloc();
|
|
|
|
if (!par)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
par->sample_aspect_ratio = ist->st->sample_aspect_ratio.num ?
|
|
|
|
ist->st->sample_aspect_ratio :
|
|
|
|
ist->dec_ctx->sample_aspect_ratio;
|
|
|
|
par->width = ist->dec_ctx->width;
|
|
|
|
par->height = ist->dec_ctx->height;
|
|
|
|
par->format = ist->hwaccel_retrieve_data ?
|
|
|
|
ist->hwaccel_retrieved_pix_fmt : ist->dec_ctx->pix_fmt;
|
|
|
|
par->time_base = tb;
|
|
|
|
|
|
|
|
ret = av_buffersrc_parameters_set(ifilter->filter, par);
|
|
|
|
av_freep(&par);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = avfilter_init_str(ifilter->filter, NULL);
|
|
|
|
if (ret < 0)
|
2012-08-02 09:24:02 +03:00
|
|
|
return ret;
|
2016-02-12 10:22:15 +02:00
|
|
|
|
2013-06-14 19:25:30 +03:00
|
|
|
last_filter = ifilter->filter;
|
2012-08-02 09:24:02 +03:00
|
|
|
|
2015-04-29 16:59:01 +02:00
|
|
|
if (ist->autorotate) {
|
|
|
|
uint8_t* displaymatrix = av_stream_get_side_data(ist->st,
|
|
|
|
AV_PKT_DATA_DISPLAYMATRIX, NULL);
|
|
|
|
if (displaymatrix) {
|
|
|
|
double rot = av_display_rotation_get((int32_t*) displaymatrix);
|
|
|
|
if (rot < -135 || rot > 135) {
|
|
|
|
ret = insert_filter(&last_filter, &pad_idx, "vflip", NULL);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
ret = insert_filter(&last_filter, &pad_idx, "hflip", NULL);
|
|
|
|
} else if (rot < -45) {
|
|
|
|
ret = insert_filter(&last_filter, &pad_idx, "transpose", "dir=clock");
|
|
|
|
} else if (rot > 45) {
|
|
|
|
ret = insert_filter(&last_filter, &pad_idx, "transpose", "dir=cclock");
|
|
|
|
}
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-02 09:24:02 +03:00
|
|
|
if (ist->framerate.num) {
|
|
|
|
AVFilterContext *setpts;
|
|
|
|
|
|
|
|
snprintf(name, sizeof(name), "force CFR for input from stream %d:%d",
|
|
|
|
ist->file_index, ist->st->index);
|
|
|
|
if ((ret = avfilter_graph_create_filter(&setpts,
|
|
|
|
avfilter_get_by_name("setpts"),
|
|
|
|
name, "N", NULL,
|
|
|
|
fg->graph)) < 0)
|
|
|
|
return ret;
|
|
|
|
|
2013-06-14 19:25:30 +03:00
|
|
|
if ((ret = avfilter_link(last_filter, 0, setpts, 0)) < 0)
|
2012-08-02 09:24:02 +03:00
|
|
|
return ret;
|
|
|
|
|
2013-06-14 19:25:30 +03:00
|
|
|
last_filter = setpts;
|
2012-08-02 09:24:02 +03:00
|
|
|
}
|
|
|
|
|
2013-06-15 10:59:40 +03:00
|
|
|
snprintf(name, sizeof(name), "trim for input stream %d:%d",
|
|
|
|
ist->file_index, ist->st->index);
|
|
|
|
ret = insert_trim(((f->start_time == AV_NOPTS_VALUE) || !f->accurate_seek) ?
|
2013-06-18 12:12:09 +03:00
|
|
|
AV_NOPTS_VALUE : 0, f->recording_time, &last_filter, &pad_idx, name);
|
2013-06-15 10:59:40 +03:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2013-06-14 19:25:30 +03:00
|
|
|
if ((ret = avfilter_link(last_filter, 0, in->filter_ctx, in->pad_idx)) < 0)
|
2012-08-02 09:24:02 +03:00
|
|
|
return ret;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int configure_input_audio_filter(FilterGraph *fg, InputFilter *ifilter,
|
|
|
|
AVFilterInOut *in)
|
|
|
|
{
|
2013-06-14 19:25:30 +03:00
|
|
|
AVFilterContext *last_filter;
|
2013-06-14 19:12:23 +03:00
|
|
|
const AVFilter *abuffer_filt = avfilter_get_by_name("abuffer");
|
2012-08-02 09:24:02 +03:00
|
|
|
InputStream *ist = ifilter->ist;
|
2013-06-15 10:59:40 +03:00
|
|
|
InputFile *f = input_files[ist->file_index];
|
2016-02-12 10:22:15 +02:00
|
|
|
AVBufferSrcParameters *par;
|
2012-08-02 09:24:02 +03:00
|
|
|
char args[255], name[255];
|
2013-06-15 10:59:40 +03:00
|
|
|
int ret, pad_idx = 0;
|
2012-08-02 09:24:02 +03:00
|
|
|
|
|
|
|
snprintf(name, sizeof(name), "graph %d input from stream %d:%d", fg->index,
|
|
|
|
ist->file_index, ist->st->index);
|
|
|
|
|
2016-02-12 10:22:15 +02:00
|
|
|
ifilter->filter = avfilter_graph_alloc_filter(fg->graph, abuffer_filt, name);
|
|
|
|
if (!ifilter->filter)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
par = av_buffersrc_parameters_alloc();
|
|
|
|
if (!par)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
par->time_base = (AVRational){ 1, ist->dec_ctx->sample_rate };
|
|
|
|
par->sample_rate = ist->dec_ctx->sample_rate;
|
|
|
|
par->format = ist->dec_ctx->sample_fmt;
|
|
|
|
par->channel_layout = ist->dec_ctx->channel_layout;
|
|
|
|
|
|
|
|
ret = av_buffersrc_parameters_set(ifilter->filter, par);
|
|
|
|
av_freep(&par);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = avfilter_init_str(ifilter->filter, NULL);
|
|
|
|
if (ret < 0)
|
2012-08-02 09:24:02 +03:00
|
|
|
return ret;
|
2013-06-14 19:25:30 +03:00
|
|
|
last_filter = ifilter->filter;
|
2012-08-02 09:24:02 +03:00
|
|
|
|
|
|
|
if (audio_sync_method > 0) {
|
|
|
|
AVFilterContext *async;
|
|
|
|
int len = 0;
|
|
|
|
|
|
|
|
av_log(NULL, AV_LOG_WARNING, "-async has been deprecated. Used the "
|
|
|
|
"asyncts audio filter instead.\n");
|
|
|
|
|
|
|
|
if (audio_sync_method > 1)
|
|
|
|
len += snprintf(args + len, sizeof(args) - len, "compensate=1:"
|
|
|
|
"max_comp=%d:", audio_sync_method);
|
|
|
|
snprintf(args + len, sizeof(args) - len, "min_delta=%f",
|
|
|
|
audio_drift_threshold);
|
|
|
|
|
|
|
|
snprintf(name, sizeof(name), "graph %d audio sync for input stream %d:%d",
|
|
|
|
fg->index, ist->file_index, ist->st->index);
|
|
|
|
ret = avfilter_graph_create_filter(&async,
|
|
|
|
avfilter_get_by_name("asyncts"),
|
|
|
|
name, args, NULL, fg->graph);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2013-06-14 19:25:30 +03:00
|
|
|
ret = avfilter_link(last_filter, 0, async, 0);
|
2012-08-02 09:24:02 +03:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2013-06-14 19:25:30 +03:00
|
|
|
last_filter = async;
|
2012-08-02 09:24:02 +03:00
|
|
|
}
|
2012-10-02 23:08:20 +03:00
|
|
|
if (audio_volume != 256) {
|
|
|
|
AVFilterContext *volume;
|
|
|
|
|
|
|
|
av_log(NULL, AV_LOG_WARNING, "-vol has been deprecated. Use the volume "
|
|
|
|
"audio filter instead.\n");
|
|
|
|
|
|
|
|
snprintf(args, sizeof(args), "volume=%f", audio_volume / 256.0);
|
|
|
|
|
|
|
|
snprintf(name, sizeof(name), "graph %d volume for input stream %d:%d",
|
|
|
|
fg->index, ist->file_index, ist->st->index);
|
|
|
|
ret = avfilter_graph_create_filter(&volume,
|
|
|
|
avfilter_get_by_name("volume"),
|
|
|
|
name, args, NULL, fg->graph);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2013-06-14 19:25:30 +03:00
|
|
|
ret = avfilter_link(last_filter, 0, volume, 0);
|
2012-10-02 23:08:20 +03:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2013-06-14 19:25:30 +03:00
|
|
|
last_filter = volume;
|
2012-10-02 23:08:20 +03:00
|
|
|
}
|
2013-06-15 10:59:40 +03:00
|
|
|
|
|
|
|
snprintf(name, sizeof(name), "trim for input stream %d:%d",
|
|
|
|
ist->file_index, ist->st->index);
|
|
|
|
ret = insert_trim(((f->start_time == AV_NOPTS_VALUE) || !f->accurate_seek) ?
|
2013-06-18 12:12:09 +03:00
|
|
|
AV_NOPTS_VALUE : 0, f->recording_time, &last_filter, &pad_idx, name);
|
2013-06-15 10:59:40 +03:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2013-06-14 19:25:30 +03:00
|
|
|
if ((ret = avfilter_link(last_filter, 0, in->filter_ctx, in->pad_idx)) < 0)
|
2012-08-02 09:24:02 +03:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int configure_input_filter(FilterGraph *fg, InputFilter *ifilter,
|
|
|
|
AVFilterInOut *in)
|
|
|
|
{
|
|
|
|
av_freep(&ifilter->name);
|
|
|
|
DESCRIBE_FILTER_LINK(ifilter, in, 1);
|
|
|
|
|
|
|
|
switch (avfilter_pad_get_type(in->filter_ctx->input_pads, in->pad_idx)) {
|
|
|
|
case AVMEDIA_TYPE_VIDEO: return configure_input_video_filter(fg, ifilter, in);
|
|
|
|
case AVMEDIA_TYPE_AUDIO: return configure_input_audio_filter(fg, ifilter, in);
|
|
|
|
default: av_assert0(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int configure_filtergraph(FilterGraph *fg)
|
|
|
|
{
|
|
|
|
AVFilterInOut *inputs, *outputs, *cur;
|
2015-07-15 19:49:24 +02:00
|
|
|
int ret, i, simple = !fg->graph_desc;
|
2012-08-02 09:24:02 +03:00
|
|
|
const char *graph_desc = simple ? fg->outputs[0]->ost->avfilter :
|
|
|
|
fg->graph_desc;
|
|
|
|
|
|
|
|
avfilter_graph_free(&fg->graph);
|
|
|
|
if (!(fg->graph = avfilter_graph_alloc()))
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
if (simple) {
|
|
|
|
OutputStream *ost = fg->outputs[0]->ost;
|
2012-12-19 05:47:28 +03:00
|
|
|
char args[512];
|
|
|
|
AVDictionaryEntry *e = NULL;
|
|
|
|
|
2012-08-02 09:24:02 +03:00
|
|
|
snprintf(args, sizeof(args), "flags=0x%X", (unsigned)ost->sws_flags);
|
|
|
|
fg->graph->scale_sws_opts = av_strdup(args);
|
2012-12-19 05:47:28 +03:00
|
|
|
|
|
|
|
args[0] = '\0';
|
|
|
|
while ((e = av_dict_get(fg->outputs[0]->ost->resample_opts, "", e,
|
|
|
|
AV_DICT_IGNORE_SUFFIX))) {
|
|
|
|
av_strlcatf(args, sizeof(args), "%s=%s:", e->key, e->value);
|
|
|
|
}
|
|
|
|
if (strlen(args))
|
|
|
|
args[strlen(args) - 1] = '\0';
|
|
|
|
fg->graph->resample_lavr_opts = av_strdup(args);
|
2012-08-02 09:24:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((ret = avfilter_graph_parse2(fg->graph, graph_desc, &inputs, &outputs)) < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (simple && (!inputs || inputs->next || !outputs || outputs->next)) {
|
|
|
|
av_log(NULL, AV_LOG_ERROR, "Simple filtergraph '%s' does not have "
|
|
|
|
"exactly one input and output.\n", graph_desc);
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (cur = inputs, i = 0; cur; cur = cur->next, i++)
|
|
|
|
if ((ret = configure_input_filter(fg, fg->inputs[i], cur)) < 0)
|
|
|
|
return ret;
|
|
|
|
avfilter_inout_free(&inputs);
|
|
|
|
|
2015-10-05 20:49:55 +02:00
|
|
|
for (cur = outputs, i = 0; cur; cur = cur->next, i++) {
|
|
|
|
OutputFilter *ofilter = fg->outputs[i];
|
|
|
|
if (ofilter->ost)
|
|
|
|
configure_output_filter(fg, ofilter, cur);
|
|
|
|
}
|
|
|
|
|
2015-07-15 19:49:24 +02:00
|
|
|
avfilter_inout_free(&outputs);
|
2012-08-02 09:24:02 +03:00
|
|
|
|
2015-07-15 19:49:24 +02:00
|
|
|
if ((ret = avfilter_graph_config(fg->graph, NULL)) < 0)
|
|
|
|
return ret;
|
2012-08-02 09:24:02 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ist_in_filtergraph(FilterGraph *fg, InputStream *ist)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < fg->nb_inputs; i++)
|
|
|
|
if (fg->inputs[i]->ist == ist)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|