Skip to content

Commit

Permalink
Merge commit '50722b4f0cbc5940e9e6e21d113888436cc89ff5'
Browse files Browse the repository at this point in the history
* commit '50722b4f0cbc5940e9e6e21d113888436cc89ff5':
  avconv: decouple configuring filtergraphs and setting output parameters

Merged-by: Hendrik Leppkes <h.leppkes@gmail.com>
  • Loading branch information
Nevcairiel committed Nov 13, 2016
2 parents 6b449a1 + 50722b4 commit 198e8b8
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 22 deletions.
3 changes: 3 additions & 0 deletions ffmpeg.c
Expand Up @@ -479,6 +479,9 @@ static void ffmpeg_cleanup(int ret)
av_freep(&fg->inputs);
for (j = 0; j < fg->nb_outputs; j++) {
av_freep(&fg->outputs[j]->name);
av_freep(&fg->outputs[j]->formats);
av_freep(&fg->outputs[j]->channel_layouts);
av_freep(&fg->outputs[j]->sample_rates);
av_freep(&fg->outputs[j]);
}
av_freep(&fg->outputs);
Expand Down
12 changes: 12 additions & 0 deletions ffmpeg.h
Expand Up @@ -254,6 +254,18 @@ typedef struct OutputFilter {
/* temporary storage until stream maps are processed */
AVFilterInOut *out_tmp;
enum AVMediaType type;

/* desired output stream properties */
int width, height;
AVRational frame_rate;
int format;
int sample_rate;
uint64_t channel_layout;

// those are only set if no format is specified and the encoder gives us multiple options
int *formats;
uint64_t *channel_layouts;
int *sample_rates;
} OutputFilter;

typedef struct FilterGraph {
Expand Down
58 changes: 36 additions & 22 deletions ffmpeg_filter.c
Expand Up @@ -114,16 +114,16 @@ void choose_sample_fmt(AVStream *st, AVCodec *codec)
}
}

static char *choose_pix_fmts(OutputStream *ost)
static char *choose_pix_fmts(OutputFilter *ofilter)
{
OutputStream *ost = ofilter->ost;
AVDictionaryEntry *strict_dict = av_dict_get(ost->encoder_opts, "strict", NULL, 0);
if (strict_dict)
// used by choose_pixel_fmt() and below
av_opt_set(ost->enc_ctx, "strict", strict_dict->value, 0);

if (ost->keep_pix_fmt) {
if (ost->filter)
avfilter_graph_set_auto_convert(ost->filter->graph->graph,
avfilter_graph_set_auto_convert(ofilter->graph->graph,
AVFILTER_AUTO_CONVERT_NONE);
if (ost->enc_ctx->pix_fmt == AV_PIX_FMT_NONE)
return NULL;
Expand Down Expand Up @@ -158,13 +158,13 @@ static char *choose_pix_fmts(OutputStream *ost)

/* Define a function for building a string containing a list of
* allowed formats. */
#define DEF_CHOOSE_FORMAT(type, var, supported_list, none, get_name) \
static char *choose_ ## var ## s(OutputStream *ost) \
#define DEF_CHOOSE_FORMAT(suffix, type, var, supported_list, none, get_name) \
static char *choose_ ## suffix (OutputFilter *ofilter) \
{ \
if (ost->enc_ctx->var != none) { \
get_name(ost->enc_ctx->var); \
if (ofilter->var != none) { \
get_name(ofilter->var); \
return av_strdup(name); \
} else if (ost->enc && ost->enc->supported_list) { \
} else if (ofilter->supported_list) { \
const type *p; \
AVIOContext *s = NULL; \
uint8_t *ret; \
Expand All @@ -173,7 +173,7 @@ static char *choose_ ## var ## s(OutputStream *ost) \
if (avio_open_dyn_buf(&s) < 0) \
exit_program(1); \
\
for (p = ost->enc->supported_list; *p != none; p++) { \
for (p = ofilter->supported_list; *p != none; p++) { \
get_name(*p); \
avio_printf(s, "%s|", name); \
} \
Expand All @@ -184,16 +184,16 @@ static char *choose_ ## var ## s(OutputStream *ost) \
return NULL; \
}

// DEF_CHOOSE_FORMAT(enum AVPixelFormat, pix_fmt, pix_fmts, AV_PIX_FMT_NONE,
// GET_PIX_FMT_NAME)
//DEF_CHOOSE_FORMAT(pix_fmts, enum AVPixelFormat, format, formats, AV_PIX_FMT_NONE,
// GET_PIX_FMT_NAME)

DEF_CHOOSE_FORMAT(enum AVSampleFormat, sample_fmt, sample_fmts,
DEF_CHOOSE_FORMAT(sample_fmts, enum AVSampleFormat, format, formats,
AV_SAMPLE_FMT_NONE, GET_SAMPLE_FMT_NAME)

DEF_CHOOSE_FORMAT(int, sample_rate, supported_samplerates, 0,
DEF_CHOOSE_FORMAT(sample_rates, int, sample_rate, sample_rates, 0,
GET_SAMPLE_RATE_NAME)

DEF_CHOOSE_FORMAT(uint64_t, channel_layout, channel_layouts, 0,
DEF_CHOOSE_FORMAT(channel_layouts, uint64_t, channel_layout, channel_layouts, 0,
GET_CH_LAYOUT_NAME)

int init_simple_filtergraph(InputStream *ist, OutputStream *ost)
Expand All @@ -209,6 +209,7 @@ int init_simple_filtergraph(InputStream *ist, OutputStream *ost)
exit_program(1);
fg->outputs[0]->ost = ost;
fg->outputs[0]->graph = fg;
fg->outputs[0]->format = -1;

ost->filter = fg->outputs[0];

Expand Down Expand Up @@ -419,7 +420,6 @@ static int configure_output_video_filter(FilterGraph *fg, OutputFilter *ofilter,
char *pix_fmts;
OutputStream *ost = ofilter->ost;
OutputFile *of = output_files[ost->file_index];
AVCodecContext *codec = ost->enc_ctx;
AVFilterContext *last_filter = out->filter_ctx;
int pad_idx = out->pad_idx;
int ret;
Expand All @@ -433,14 +433,13 @@ static int configure_output_video_filter(FilterGraph *fg, OutputFilter *ofilter,
if (ret < 0)
return ret;

if (!hw_device_ctx && (codec->width || codec->height)) {
if (!hw_device_ctx && (ofilter->width || ofilter->height)) {
char args[255];
AVFilterContext *filter;
AVDictionaryEntry *e = NULL;

snprintf(args, sizeof(args), "%d:%d",
codec->width,
codec->height);
ofilter->width, ofilter->height);

while ((e = av_dict_get(ost->sws_dict, "", e,
AV_DICT_IGNORE_SUFFIX))) {
Expand All @@ -459,7 +458,7 @@ static int configure_output_video_filter(FilterGraph *fg, OutputFilter *ofilter,
pad_idx = 0;
}

if ((pix_fmts = choose_pix_fmts(ost))) {
if ((pix_fmts = choose_pix_fmts(ofilter))) {
AVFilterContext *filter;
snprintf(name, sizeof(name), "pixel format for output stream %d:%d",
ost->file_index, ost->index);
Expand Down Expand Up @@ -566,9 +565,9 @@ static int configure_output_audio_filter(FilterGraph *fg, OutputFilter *ofilter,
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);
sample_fmts = choose_sample_fmts(ofilter);
sample_rates = choose_sample_rates(ofilter);
channel_layouts = choose_channel_layouts(ofilter);
if (sample_fmts || sample_rates || channel_layouts) {
AVFilterContext *format;
char args[256];
Expand Down Expand Up @@ -1079,6 +1078,21 @@ int configure_filtergraph(FilterGraph *fg)
if ((ret = avfilter_graph_config(fg->graph, NULL)) < 0)
return ret;

/* limit the lists of allowed formats to the ones selected, to
* make sure they stay the same if the filtergraph is reconfigured later */
for (i = 0; i < fg->nb_outputs; i++) {
OutputFilter *ofilter = fg->outputs[i];
AVFilterLink *link = ofilter->filter->inputs[0];

ofilter->format = link->format;

ofilter->width = link->w;
ofilter->height = link->h;

ofilter->sample_rate = link->sample_rate;
ofilter->channel_layout = link->channel_layout;
}

fg->reconfiguration = 1;

for (i = 0; i < fg->nb_outputs; i++) {
Expand Down
62 changes: 62 additions & 0 deletions ffmpeg_opt.c
Expand Up @@ -1971,6 +1971,7 @@ static void init_output_filter(OutputFilter *ofilter, OptionsContext *o,
ost->filter = ofilter;

ofilter->ost = ost;
ofilter->format = -1;

if (ost->stream_copy) {
av_log(NULL, AV_LOG_ERROR, "Streamcopy requested for output stream %d:%d, "
Expand Down Expand Up @@ -2415,6 +2416,67 @@ static int open_output_file(OptionsContext *o, const char *filename)
}
}
}

/* set the filter output constraints */
if (ost->filter) {
OutputFilter *f = ost->filter;
int count;
switch (ost->enc_ctx->codec_type) {
case AVMEDIA_TYPE_VIDEO:
f->frame_rate = ost->frame_rate;
f->width = ost->enc_ctx->width;
f->height = ost->enc_ctx->height;
if (ost->enc_ctx->pix_fmt != AV_PIX_FMT_NONE) {
f->format = ost->enc_ctx->pix_fmt;
} else if (ost->enc->pix_fmts) {
count = 0;
while (ost->enc->pix_fmts[count] != AV_PIX_FMT_NONE)
count++;
f->formats = av_mallocz_array(count + 1, sizeof(*f->formats));
if (!f->formats)
exit_program(1);
memcpy(f->formats, ost->enc->pix_fmts, (count + 1) * sizeof(*f->formats));
}
break;
case AVMEDIA_TYPE_AUDIO:
if (ost->enc_ctx->sample_fmt != AV_SAMPLE_FMT_NONE) {
f->format = ost->enc_ctx->sample_fmt;
} else if (ost->enc->sample_fmts) {
count = 0;
while (ost->enc->sample_fmts[count] != AV_SAMPLE_FMT_NONE)
count++;
f->formats = av_mallocz_array(count + 1, sizeof(*f->formats));
if (!f->formats)
exit_program(1);
memcpy(f->formats, ost->enc->sample_fmts, (count + 1) * sizeof(*f->formats));
}
if (ost->enc_ctx->sample_rate) {
f->sample_rate = ost->enc_ctx->sample_rate;
} else if (ost->enc->supported_samplerates) {
count = 0;
while (ost->enc->supported_samplerates[count])
count++;
f->sample_rates = av_mallocz_array(count + 1, sizeof(*f->sample_rates));
if (!f->sample_rates)
exit_program(1);
memcpy(f->sample_rates, ost->enc->supported_samplerates,
(count + 1) * sizeof(*f->sample_rates));
}
if (ost->enc_ctx->channels) {
f->channel_layout = av_get_default_channel_layout(ost->enc_ctx->channels);
} else if (ost->enc->channel_layouts) {
count = 0;
while (ost->enc->channel_layouts[count])
count++;
f->channel_layouts = av_mallocz_array(count + 1, sizeof(*f->channel_layouts));
if (!f->channel_layouts)
exit_program(1);
memcpy(f->channel_layouts, ost->enc->channel_layouts,
(count + 1) * sizeof(*f->channel_layouts));
}
break;
}
}
}

/* check filename in case of an image number is expected */
Expand Down

0 comments on commit 198e8b8

Please sign in to comment.