1 /*
2  * ffmpeg filter configuration
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include <stdint.h>
22 
23 #include "ffmpeg.h"
24 
25 #include "libavfilter/avfilter.h"
26 #include "libavfilter/buffersink.h"
27 #include "libavfilter/buffersrc.h"
28 
29 #include "libavutil/avassert.h"
30 #include "libavutil/avstring.h"
31 #include "libavutil/bprint.h"
32 #include "libavutil/channel_layout.h"
33 #include "libavutil/display.h"
34 #include "libavutil/opt.h"
35 #include "libavutil/pixdesc.h"
36 #include "libavutil/pixfmt.h"
37 #include "libavutil/imgutils.h"
38 #include "libavutil/samplefmt.h"
39 
40 // FIXME: YUV420P etc. are actually supported with full color range,
41 // yet the latter information isn't available here.
get_compliance_normal_pix_fmts(const AVCodec *codec, const enum AVPixelFormat default_formats[])42 static const enum AVPixelFormat *get_compliance_normal_pix_fmts(const AVCodec *codec, const enum AVPixelFormat default_formats[])
43 {
44     static const enum AVPixelFormat mjpeg_formats[] =
45         { AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P,
46           AV_PIX_FMT_NONE };
47 
48     if (!strcmp(codec->name, "mjpeg")) {
49         return mjpeg_formats;
50     } else {
51         return default_formats;
52     }
53 }
54 
choose_pixel_fmt(AVStream *st, AVCodecContext *enc_ctx, const AVCodec *codec, enum AVPixelFormat target)55 static enum AVPixelFormat choose_pixel_fmt(AVStream *st, AVCodecContext *enc_ctx,
56                                     const AVCodec *codec, enum AVPixelFormat target)
57 {
58     if (codec && codec->pix_fmts) {
59         const enum AVPixelFormat *p = codec->pix_fmts;
60         const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(target);
61         //FIXME: This should check for AV_PIX_FMT_FLAG_ALPHA after PAL8 pixel format without alpha is implemented
62         int has_alpha = desc ? desc->nb_components % 2 == 0 : 0;
63         enum AVPixelFormat best= AV_PIX_FMT_NONE;
64 
65         if (enc_ctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL) {
66             p = get_compliance_normal_pix_fmts(codec, p);
67         }
68         for (; *p != AV_PIX_FMT_NONE; p++) {
69             best = av_find_best_pix_fmt_of_2(best, *p, target, has_alpha, NULL);
70             if (*p == target)
71                 break;
72         }
73         if (*p == AV_PIX_FMT_NONE) {
74             if (target != AV_PIX_FMT_NONE)
75                 av_log(NULL, AV_LOG_WARNING,
76                        "Incompatible pixel format '%s' for codec '%s', auto-selecting format '%s'\n",
77                        av_get_pix_fmt_name(target),
78                        codec->name,
79                        av_get_pix_fmt_name(best));
80             return best;
81         }
82     }
83     return target;
84 }
85 
86 /* May return NULL (no pixel format found), a static string or a string
87  * backed by the bprint. Nothing has been written to the AVBPrint in case
88  * NULL is returned. The AVBPrint provided should be clean. */
choose_pix_fmts(OutputFilter *ofilter, AVBPrint *bprint)89 static const char *choose_pix_fmts(OutputFilter *ofilter, AVBPrint *bprint)
90 {
91     OutputStream *ost = ofilter->ost;
92     const AVDictionaryEntry *strict_dict = av_dict_get(ost->encoder_opts, "strict", NULL, 0);
93     if (strict_dict)
94         // used by choose_pixel_fmt() and below
95         av_opt_set(ost->enc_ctx, "strict", strict_dict->value, 0);
96 
97      if (ost->keep_pix_fmt) {
98         avfilter_graph_set_auto_convert(ofilter->graph->graph,
99                                             AVFILTER_AUTO_CONVERT_NONE);
100         if (ost->enc_ctx->pix_fmt == AV_PIX_FMT_NONE)
101             return NULL;
102         return av_get_pix_fmt_name(ost->enc_ctx->pix_fmt);
103     }
104     if (ost->enc_ctx->pix_fmt != AV_PIX_FMT_NONE) {
105         return av_get_pix_fmt_name(choose_pixel_fmt(ost->st, ost->enc_ctx, ost->enc, ost->enc_ctx->pix_fmt));
106     } else if (ost->enc && ost->enc->pix_fmts) {
107         const enum AVPixelFormat *p;
108 
109         p = ost->enc->pix_fmts;
110         if (ost->enc_ctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL) {
111             p = get_compliance_normal_pix_fmts(ost->enc, p);
112         }
113 
114         for (; *p != AV_PIX_FMT_NONE; p++) {
115             const char *name = av_get_pix_fmt_name(*p);
116             av_bprintf(bprint, "%s%c", name, p[1] == AV_PIX_FMT_NONE ? '\0' : '|');
117         }
118         if (!av_bprint_is_complete(bprint))
119             exit_program(1);
120         return bprint->str;
121     } else
122         return NULL;
123 }
124 
125 /* Define a function for appending a list of allowed formats
126  * to an AVBPrint. If nonempty, the list will have a header. */
127 #define DEF_CHOOSE_FORMAT(name, type, var, supported_list, none, printf_format, get_name) \
128 static void choose_ ## name (OutputFilter *ofilter, AVBPrint *bprint)          \
129 {                                                                              \
130     if (ofilter->var == none && !ofilter->supported_list)                      \
131         return;                                                                \
132     av_bprintf(bprint, #name "=");                                             \
133     if (ofilter->var != none) {                                                \
134         av_bprintf(bprint, printf_format, get_name(ofilter->var));             \
135     } else {                                                                   \
136         const type *p;                                                         \
137                                                                                \
138         for (p = ofilter->supported_list; *p != none; p++) {                   \
139             av_bprintf(bprint, printf_format "|", get_name(*p));               \
140         }                                                                      \
141         if (bprint->len > 0)                                                   \
142             bprint->str[--bprint->len] = '\0';                                 \
143     }                                                                          \
144     av_bprint_chars(bprint, ':', 1);                                           \
145 }
146 
147 //DEF_CHOOSE_FORMAT(pix_fmts, enum AVPixelFormat, format, formats, AV_PIX_FMT_NONE,
148 //                  GET_PIX_FMT_NAME)
149 
150 DEF_CHOOSE_FORMAT(sample_fmts, enum AVSampleFormat, format, formats,
151                   AV_SAMPLE_FMT_NONE, "%s", av_get_sample_fmt_name)
152 
153 DEF_CHOOSE_FORMAT(sample_rates, int, sample_rate, sample_rates, 0,
154                   "%d", )
155 
choose_channel_layouts(OutputFilter *ofilter, AVBPrint *bprint)156 static void choose_channel_layouts(OutputFilter *ofilter, AVBPrint *bprint)
157 {
158     if (av_channel_layout_check(&ofilter->ch_layout)) {
159         av_bprintf(bprint, "channel_layouts=");
160         av_channel_layout_describe_bprint(&ofilter->ch_layout, bprint);
161     } else if (ofilter->ch_layouts) {
162         const AVChannelLayout *p;
163 
164         av_bprintf(bprint, "channel_layouts=");
165         for (p = ofilter->ch_layouts; p->nb_channels; p++) {
166             av_channel_layout_describe_bprint(p, bprint);
167             av_bprintf(bprint, "|");
168         }
169         if (bprint->len > 0)
170             bprint->str[--bprint->len] = '\0';
171     } else
172         return;
173     av_bprint_chars(bprint, ':', 1);
174 }
175 
init_simple_filtergraph(InputStream *ist, OutputStream *ost)176 int init_simple_filtergraph(InputStream *ist, OutputStream *ost)
177 {
178     FilterGraph *fg = av_mallocz(sizeof(*fg));
179     OutputFilter *ofilter;
180     InputFilter  *ifilter;
181 
182     if (!fg)
183         exit_program(1);
184     fg->index = nb_filtergraphs;
185 
186     ofilter = ALLOC_ARRAY_ELEM(fg->outputs, fg->nb_outputs);
187     ofilter->ost    = ost;
188     ofilter->graph  = fg;
189     ofilter->format = -1;
190 
191     ost->filter = ofilter;
192 
193     ifilter = ALLOC_ARRAY_ELEM(fg->inputs, fg->nb_inputs);
194     ifilter->ist    = ist;
195     ifilter->graph  = fg;
196     ifilter->format = -1;
197 
198     ifilter->frame_queue = av_fifo_alloc2(8, sizeof(AVFrame*), AV_FIFO_FLAG_AUTO_GROW);
199     if (!ifilter->frame_queue)
200         exit_program(1);
201 
202     GROW_ARRAY(ist->filters, ist->nb_filters);
203     ist->filters[ist->nb_filters - 1] = ifilter;
204 
205     GROW_ARRAY(filtergraphs, nb_filtergraphs);
206     filtergraphs[nb_filtergraphs - 1] = fg;
207 
208     return 0;
209 }
210 
describe_filter_link(FilterGraph *fg, AVFilterInOut *inout, int in)211 static char *describe_filter_link(FilterGraph *fg, AVFilterInOut *inout, int in)
212 {
213     AVFilterContext *ctx = inout->filter_ctx;
214     AVFilterPad *pads = in ? ctx->input_pads  : ctx->output_pads;
215     int       nb_pads = in ? ctx->nb_inputs   : ctx->nb_outputs;
216     char *res;
217 
218     if (nb_pads > 1)
219         res = av_strdup(ctx->filter->name);
220     else
221         res = av_asprintf("%s:%s", ctx->filter->name,
222                           avfilter_pad_get_name(pads, inout->pad_idx));
223     if (!res)
224         exit_program(1);
225     return res;
226 }
227 
init_input_filter(FilterGraph *fg, AVFilterInOut *in)228 static void init_input_filter(FilterGraph *fg, AVFilterInOut *in)
229 {
230     InputStream *ist = NULL;
231     enum AVMediaType type = avfilter_pad_get_type(in->filter_ctx->input_pads, in->pad_idx);
232     InputFilter *ifilter;
233     int i;
234 
235     // TODO: support other filter types
236     if (type != AVMEDIA_TYPE_VIDEO && type != AVMEDIA_TYPE_AUDIO) {
237         av_log(NULL, AV_LOG_FATAL, "Only video and audio filters supported "
238                "currently.\n");
239         exit_program(1);
240     }
241 
242     if (in->name) {
243         AVFormatContext *s;
244         AVStream       *st = NULL;
245         char *p;
246         int file_idx = strtol(in->name, &p, 0);
247 
248         if (file_idx < 0 || file_idx >= nb_input_files) {
249             av_log(NULL, AV_LOG_FATAL, "Invalid file index %d in filtergraph description %s.\n",
250                    file_idx, fg->graph_desc);
251             exit_program(1);
252         }
253         s = input_files[file_idx]->ctx;
254 
255         for (i = 0; i < s->nb_streams; i++) {
256             enum AVMediaType stream_type = s->streams[i]->codecpar->codec_type;
257             if (stream_type != type &&
258                 !(stream_type == AVMEDIA_TYPE_SUBTITLE &&
259                   type == AVMEDIA_TYPE_VIDEO /* sub2video hack */))
260                 continue;
261             if (check_stream_specifier(s, s->streams[i], *p == ':' ? p + 1 : p) == 1) {
262                 st = s->streams[i];
263                 break;
264             }
265         }
266         if (!st) {
267             av_log(NULL, AV_LOG_FATAL, "Stream specifier '%s' in filtergraph description %s "
268                    "matches no streams.\n", p, fg->graph_desc);
269             exit_program(1);
270         }
271         ist = input_streams[input_files[file_idx]->ist_index + st->index];
272         if (ist->user_set_discard == AVDISCARD_ALL) {
273             av_log(NULL, AV_LOG_FATAL, "Stream specifier '%s' in filtergraph description %s "
274                    "matches a disabled input stream.\n", p, fg->graph_desc);
275             exit_program(1);
276         }
277     } else {
278         /* find the first unused stream of corresponding type */
279         for (i = 0; i < nb_input_streams; i++) {
280             ist = input_streams[i];
281             if (ist->user_set_discard == AVDISCARD_ALL)
282                 continue;
283             if (ist->dec_ctx->codec_type == type && ist->discard)
284                 break;
285         }
286         if (i == nb_input_streams) {
287             av_log(NULL, AV_LOG_FATAL, "Cannot find a matching stream for "
288                    "unlabeled input pad %d on filter %s\n", in->pad_idx,
289                    in->filter_ctx->name);
290             exit_program(1);
291         }
292     }
293     av_assert0(ist);
294 
295     ist->discard         = 0;
296     ist->decoding_needed |= DECODING_FOR_FILTER;
297     ist->processing_needed = 1;
298     ist->st->discard = AVDISCARD_NONE;
299 
300     ifilter = ALLOC_ARRAY_ELEM(fg->inputs, fg->nb_inputs);
301     ifilter->ist    = ist;
302     ifilter->graph  = fg;
303     ifilter->format = -1;
304     ifilter->type   = ist->st->codecpar->codec_type;
305     ifilter->name   = describe_filter_link(fg, in, 1);
306 
307     ifilter->frame_queue = av_fifo_alloc2(8, sizeof(AVFrame*), AV_FIFO_FLAG_AUTO_GROW);
308     if (!ifilter->frame_queue)
309         exit_program(1);
310 
311     GROW_ARRAY(ist->filters, ist->nb_filters);
312     ist->filters[ist->nb_filters - 1] = ifilter;
313 }
314 
init_complex_filtergraph(FilterGraph *fg)315 int init_complex_filtergraph(FilterGraph *fg)
316 {
317     AVFilterInOut *inputs, *outputs, *cur;
318     AVFilterGraph *graph;
319     int ret = 0;
320 
321     /* this graph is only used for determining the kinds of inputs
322      * and outputs we have, and is discarded on exit from this function */
323     graph = avfilter_graph_alloc();
324     if (!graph)
325         return AVERROR(ENOMEM);
326     graph->nb_threads = 1;
327 
328     ret = avfilter_graph_parse2(graph, fg->graph_desc, &inputs, &outputs);
329     if (ret < 0)
330         goto fail;
331 
332     for (cur = inputs; cur; cur = cur->next)
333         init_input_filter(fg, cur);
334 
335     for (cur = outputs; cur;) {
336         OutputFilter *const ofilter = ALLOC_ARRAY_ELEM(fg->outputs, fg->nb_outputs);
337 
338         ofilter->graph   = fg;
339         ofilter->out_tmp = cur;
340         ofilter->type    = avfilter_pad_get_type(cur->filter_ctx->output_pads,
341                                                                          cur->pad_idx);
342         ofilter->name    = describe_filter_link(fg, cur, 0);
343         cur = cur->next;
344         ofilter->out_tmp->next = NULL;
345     }
346 
347 fail:
348     avfilter_inout_free(&inputs);
349     avfilter_graph_free(&graph);
350     return ret;
351 }
352 
insert_trim(int64_t start_time, int64_t duration, AVFilterContext **last_filter, int *pad_idx, const char *filter_name)353 static int insert_trim(int64_t start_time, int64_t duration,
354                        AVFilterContext **last_filter, int *pad_idx,
355                        const char *filter_name)
356 {
357     AVFilterGraph *graph = (*last_filter)->graph;
358     AVFilterContext *ctx;
359     const AVFilter *trim;
360     enum AVMediaType type = avfilter_pad_get_type((*last_filter)->output_pads, *pad_idx);
361     const char *name = (type == AVMEDIA_TYPE_VIDEO) ? "trim" : "atrim";
362     int ret = 0;
363 
364     if (duration == INT64_MAX && start_time == AV_NOPTS_VALUE)
365         return 0;
366 
367     trim = avfilter_get_by_name(name);
368     if (!trim) {
369         av_log(NULL, AV_LOG_ERROR, "%s filter not present, cannot limit "
370                "recording time.\n", name);
371         return AVERROR_FILTER_NOT_FOUND;
372     }
373 
374     ctx = avfilter_graph_alloc_filter(graph, trim, filter_name);
375     if (!ctx)
376         return AVERROR(ENOMEM);
377 
378     if (duration != INT64_MAX) {
379         ret = av_opt_set_int(ctx, "durationi", duration,
380                                 AV_OPT_SEARCH_CHILDREN);
381     }
382     if (ret >= 0 && start_time != AV_NOPTS_VALUE) {
383         ret = av_opt_set_int(ctx, "starti", start_time,
384                                 AV_OPT_SEARCH_CHILDREN);
385     }
386     if (ret < 0) {
387         av_log(ctx, AV_LOG_ERROR, "Error configuring the %s filter", name);
388         return ret;
389     }
390 
391     ret = avfilter_init_str(ctx, NULL);
392     if (ret < 0)
393         return ret;
394 
395     ret = avfilter_link(*last_filter, *pad_idx, ctx, 0);
396     if (ret < 0)
397         return ret;
398 
399     *last_filter = ctx;
400     *pad_idx     = 0;
401     return 0;
402 }
403 
insert_filter(AVFilterContext **last_filter, int *pad_idx, const char *filter_name, const char *args)404 static int insert_filter(AVFilterContext **last_filter, int *pad_idx,
405                          const char *filter_name, const char *args)
406 {
407     AVFilterGraph *graph = (*last_filter)->graph;
408     AVFilterContext *ctx;
409     int ret;
410 
411     ret = avfilter_graph_create_filter(&ctx,
412                                        avfilter_get_by_name(filter_name),
413                                        filter_name, args, NULL, graph);
414     if (ret < 0)
415         return ret;
416 
417     ret = avfilter_link(*last_filter, *pad_idx, ctx, 0);
418     if (ret < 0)
419         return ret;
420 
421     *last_filter = ctx;
422     *pad_idx     = 0;
423     return 0;
424 }
425 
configure_output_video_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)426 static int configure_output_video_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
427 {
428     OutputStream *ost = ofilter->ost;
429     OutputFile    *of = output_files[ost->file_index];
430     AVFilterContext *last_filter = out->filter_ctx;
431     AVBPrint bprint;
432     int pad_idx = out->pad_idx;
433     int ret;
434     const char *pix_fmts;
435     char name[255];
436 
437     snprintf(name, sizeof(name), "out_%d_%d", ost->file_index, ost->index);
438     ret = avfilter_graph_create_filter(&ofilter->filter,
439                                        avfilter_get_by_name("buffersink"),
440                                        name, NULL, NULL, fg->graph);
441 
442     if (ret < 0)
443         return ret;
444 
445     if ((ofilter->width || ofilter->height) && ofilter->ost->autoscale) {
446         char args[255];
447         AVFilterContext *filter;
448         const AVDictionaryEntry *e = NULL;
449 
450         snprintf(args, sizeof(args), "%d:%d",
451                  ofilter->width, ofilter->height);
452 
453         while ((e = av_dict_get(ost->sws_dict, "", e,
454                                 AV_DICT_IGNORE_SUFFIX))) {
455             av_strlcatf(args, sizeof(args), ":%s=%s", e->key, e->value);
456         }
457 
458         snprintf(name, sizeof(name), "scaler_out_%d_%d",
459                  ost->file_index, ost->index);
460         if ((ret = avfilter_graph_create_filter(&filter, avfilter_get_by_name("scale"),
461                                                 name, args, NULL, fg->graph)) < 0)
462             return ret;
463         if ((ret = avfilter_link(last_filter, pad_idx, filter, 0)) < 0)
464             return ret;
465 
466         last_filter = filter;
467         pad_idx = 0;
468     }
469 
470     av_bprint_init(&bprint, 0, AV_BPRINT_SIZE_UNLIMITED);
471     if ((pix_fmts = choose_pix_fmts(ofilter, &bprint))) {
472         AVFilterContext *filter;
473 
474         ret = avfilter_graph_create_filter(&filter,
475                                            avfilter_get_by_name("format"),
476                                            "format", pix_fmts, NULL, fg->graph);
477         av_bprint_finalize(&bprint, NULL);
478         if (ret < 0)
479             return ret;
480         if ((ret = avfilter_link(last_filter, pad_idx, filter, 0)) < 0)
481             return ret;
482 
483         last_filter = filter;
484         pad_idx     = 0;
485     }
486 
487     if (ost->frame_rate.num && 0) {
488         AVFilterContext *fps;
489         char args[255];
490 
491         snprintf(args, sizeof(args), "fps=%d/%d", ost->frame_rate.num,
492                  ost->frame_rate.den);
493         snprintf(name, sizeof(name), "fps_out_%d_%d",
494                  ost->file_index, ost->index);
495         ret = avfilter_graph_create_filter(&fps, avfilter_get_by_name("fps"),
496                                            name, args, NULL, fg->graph);
497         if (ret < 0)
498             return ret;
499 
500         ret = avfilter_link(last_filter, pad_idx, fps, 0);
501         if (ret < 0)
502             return ret;
503         last_filter = fps;
504         pad_idx = 0;
505     }
506 
507     snprintf(name, sizeof(name), "trim_out_%d_%d",
508              ost->file_index, ost->index);
509     ret = insert_trim(of->start_time, of->recording_time,
510                       &last_filter, &pad_idx, name);
511     if (ret < 0)
512         return ret;
513 
514 
515     if ((ret = avfilter_link(last_filter, pad_idx, ofilter->filter, 0)) < 0)
516         return ret;
517 
518     return 0;
519 }
520 
configure_output_audio_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)521 static int configure_output_audio_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
522 {
523     OutputStream *ost = ofilter->ost;
524     OutputFile    *of = output_files[ost->file_index];
525     AVCodecContext *codec  = ost->enc_ctx;
526     AVFilterContext *last_filter = out->filter_ctx;
527     int pad_idx = out->pad_idx;
528     AVBPrint args;
529     char name[255];
530     int ret;
531 
532     snprintf(name, sizeof(name), "out_%d_%d", ost->file_index, ost->index);
533     ret = avfilter_graph_create_filter(&ofilter->filter,
534                                        avfilter_get_by_name("abuffersink"),
535                                        name, NULL, NULL, fg->graph);
536     if (ret < 0)
537         return ret;
538     if ((ret = av_opt_set_int(ofilter->filter, "all_channel_counts", 1, AV_OPT_SEARCH_CHILDREN)) < 0)
539         return ret;
540 
541 #define AUTO_INSERT_FILTER(opt_name, filter_name, arg) do {                 \
542     AVFilterContext *filt_ctx;                                              \
543                                                                             \
544     av_log(NULL, AV_LOG_INFO, opt_name " is forwarded to lavfi "            \
545            "similarly to -af " filter_name "=%s.\n", arg);                  \
546                                                                             \
547     ret = avfilter_graph_create_filter(&filt_ctx,                           \
548                                        avfilter_get_by_name(filter_name),   \
549                                        filter_name, arg, NULL, fg->graph);  \
550     if (ret < 0)                                                            \
551         goto fail;                                                          \
552                                                                             \
553     ret = avfilter_link(last_filter, pad_idx, filt_ctx, 0);                 \
554     if (ret < 0)                                                            \
555         goto fail;                                                          \
556                                                                             \
557     last_filter = filt_ctx;                                                 \
558     pad_idx = 0;                                                            \
559 } while (0)
560     av_bprint_init(&args, 0, AV_BPRINT_SIZE_UNLIMITED);
561     if (ost->audio_channels_mapped) {
562         AVChannelLayout mapped_layout = { 0 };
563         int i;
564         av_channel_layout_default(&mapped_layout, ost->audio_channels_mapped);
565         av_channel_layout_describe_bprint(&mapped_layout, &args);
566         for (i = 0; i < ost->audio_channels_mapped; i++)
567             if (ost->audio_channels_map[i] != -1)
568                 av_bprintf(&args, "|c%d=c%d", i, ost->audio_channels_map[i]);
569 
570         AUTO_INSERT_FILTER("-map_channel", "pan", args.str);
571         av_bprint_clear(&args);
572     }
573 
574     if (codec->ch_layout.order == AV_CHANNEL_ORDER_UNSPEC)
575         av_channel_layout_default(&codec->ch_layout, codec->ch_layout.nb_channels);
576 
577     choose_sample_fmts(ofilter,     &args);
578     choose_sample_rates(ofilter,    &args);
579     choose_channel_layouts(ofilter, &args);
580     if (!av_bprint_is_complete(&args)) {
581         ret = AVERROR(ENOMEM);
582         goto fail;
583     }
584     if (args.len) {
585         AVFilterContext *format;
586 
587         snprintf(name, sizeof(name), "format_out_%d_%d",
588                  ost->file_index, ost->index);
589         ret = avfilter_graph_create_filter(&format,
590                                            avfilter_get_by_name("aformat"),
591                                            name, args.str, NULL, fg->graph);
592         if (ret < 0)
593             goto fail;
594 
595         ret = avfilter_link(last_filter, pad_idx, format, 0);
596         if (ret < 0)
597             goto fail;
598 
599         last_filter = format;
600         pad_idx = 0;
601     }
602 
603     if (ost->apad && of->shortest) {
604         int i;
605 
606         for (i=0; i<of->ctx->nb_streams; i++)
607             if (of->ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
608                 break;
609 
610         if (i<of->ctx->nb_streams) {
611             AUTO_INSERT_FILTER("-apad", "apad", ost->apad);
612         }
613     }
614 
615     snprintf(name, sizeof(name), "trim for output stream %d:%d",
616              ost->file_index, ost->index);
617     ret = insert_trim(of->start_time, of->recording_time,
618                       &last_filter, &pad_idx, name);
619     if (ret < 0)
620         goto fail;
621 
622     if ((ret = avfilter_link(last_filter, pad_idx, ofilter->filter, 0)) < 0)
623         goto fail;
624 fail:
625     av_bprint_finalize(&args, NULL);
626 
627     return ret;
628 }
629 
configure_output_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)630 static int configure_output_filter(FilterGraph *fg, OutputFilter *ofilter,
631                                    AVFilterInOut *out)
632 {
633     if (!ofilter->ost) {
634         av_log(NULL, AV_LOG_FATAL, "Filter %s has an unconnected output\n", ofilter->name);
635         exit_program(1);
636     }
637 
638     switch (avfilter_pad_get_type(out->filter_ctx->output_pads, out->pad_idx)) {
639     case AVMEDIA_TYPE_VIDEO: return configure_output_video_filter(fg, ofilter, out);
640     case AVMEDIA_TYPE_AUDIO: return configure_output_audio_filter(fg, ofilter, out);
641     default: av_assert0(0); return 0;
642     }
643 }
644 
check_filter_outputs(void)645 void check_filter_outputs(void)
646 {
647     int i;
648     for (i = 0; i < nb_filtergraphs; i++) {
649         int n;
650         for (n = 0; n < filtergraphs[i]->nb_outputs; n++) {
651             OutputFilter *output = filtergraphs[i]->outputs[n];
652             if (!output->ost) {
653                 av_log(NULL, AV_LOG_FATAL, "Filter %s has an unconnected output\n", output->name);
654                 exit_program(1);
655             }
656         }
657     }
658 }
659 
sub2video_prepare(InputStream *ist, InputFilter *ifilter)660 static int sub2video_prepare(InputStream *ist, InputFilter *ifilter)
661 {
662     AVFormatContext *avf = input_files[ist->file_index]->ctx;
663     int i, w, h;
664 
665     /* Compute the size of the canvas for the subtitles stream.
666        If the subtitles codecpar has set a size, use it. Otherwise use the
667        maximum dimensions of the video streams in the same file. */
668     w = ifilter->width;
669     h = ifilter->height;
670     if (!(w && h)) {
671         for (i = 0; i < avf->nb_streams; i++) {
672             if (avf->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
673                 w = FFMAX(w, avf->streams[i]->codecpar->width);
674                 h = FFMAX(h, avf->streams[i]->codecpar->height);
675             }
676         }
677         if (!(w && h)) {
678             w = FFMAX(w, 720);
679             h = FFMAX(h, 576);
680         }
681         av_log(avf, AV_LOG_INFO, "sub2video: using %dx%d canvas\n", w, h);
682     }
683     ist->sub2video.w = ifilter->width  = w;
684     ist->sub2video.h = ifilter->height = h;
685 
686     ifilter->width  = ist->dec_ctx->width  ? ist->dec_ctx->width  : ist->sub2video.w;
687     ifilter->height = ist->dec_ctx->height ? ist->dec_ctx->height : ist->sub2video.h;
688 
689     /* rectangles are AV_PIX_FMT_PAL8, but we have no guarantee that the
690        palettes for all rectangles are identical or compatible */
691     ifilter->format = AV_PIX_FMT_RGB32;
692 
693     ist->sub2video.frame = av_frame_alloc();
694     if (!ist->sub2video.frame)
695         return AVERROR(ENOMEM);
696     ist->sub2video.last_pts = INT64_MIN;
697     ist->sub2video.end_pts  = INT64_MIN;
698 
699     /* sub2video structure has been (re-)initialized.
700        Mark it as such so that the system will be
701        initialized with the first received heartbeat. */
702     ist->sub2video.initialize = 1;
703 
704     return 0;
705 }
706 
configure_input_video_filter(FilterGraph *fg, InputFilter *ifilter, AVFilterInOut *in)707 static int configure_input_video_filter(FilterGraph *fg, InputFilter *ifilter,
708                                         AVFilterInOut *in)
709 {
710     AVFilterContext *last_filter;
711     const AVFilter *buffer_filt = avfilter_get_by_name("buffer");
712     const AVPixFmtDescriptor *desc;
713     InputStream *ist = ifilter->ist;
714     InputFile     *f = input_files[ist->file_index];
715     AVRational tb = ist->framerate.num ? av_inv_q(ist->framerate) :
716                                          ist->st->time_base;
717     AVRational fr = ist->framerate;
718     AVRational sar;
719     AVBPrint args;
720     char name[255];
721     int ret, pad_idx = 0;
722     int64_t tsoffset = 0;
723     AVBufferSrcParameters *par = av_buffersrc_parameters_alloc();
724 
725     if (!par)
726         return AVERROR(ENOMEM);
727     memset(par, 0, sizeof(*par));
728     par->format = AV_PIX_FMT_NONE;
729 
730     if (ist->dec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) {
731         av_log(NULL, AV_LOG_ERROR, "Cannot connect video filter to audio input\n");
732         ret = AVERROR(EINVAL);
733         goto fail;
734     }
735 
736     if (!fr.num)
737         fr = av_guess_frame_rate(input_files[ist->file_index]->ctx, ist->st, NULL);
738 
739     if (ist->dec_ctx->codec_type == AVMEDIA_TYPE_SUBTITLE) {
740         ret = sub2video_prepare(ist, ifilter);
741         if (ret < 0)
742             goto fail;
743     }
744 
745     sar = ifilter->sample_aspect_ratio;
746     if(!sar.den)
747         sar = (AVRational){0,1};
748     av_bprint_init(&args, 0, AV_BPRINT_SIZE_AUTOMATIC);
749     av_bprintf(&args,
750              "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:"
751              "pixel_aspect=%d/%d",
752              ifilter->width, ifilter->height, ifilter->format,
753              tb.num, tb.den, sar.num, sar.den);
754     if (fr.num && fr.den)
755         av_bprintf(&args, ":frame_rate=%d/%d", fr.num, fr.den);
756     snprintf(name, sizeof(name), "graph %d input from stream %d:%d", fg->index,
757              ist->file_index, ist->st->index);
758 
759 
760     if ((ret = avfilter_graph_create_filter(&ifilter->filter, buffer_filt, name,
761                                             args.str, NULL, fg->graph)) < 0)
762         goto fail;
763     par->hw_frames_ctx = ifilter->hw_frames_ctx;
764     ret = av_buffersrc_parameters_set(ifilter->filter, par);
765     if (ret < 0)
766         goto fail;
767     av_freep(&par);
768     last_filter = ifilter->filter;
769 
770     desc = av_pix_fmt_desc_get(ifilter->format);
771     av_assert0(desc);
772 
773     // TODO: insert hwaccel enabled filters like transpose_vaapi into the graph
774     if (ist->autorotate && !(desc->flags & AV_PIX_FMT_FLAG_HWACCEL)) {
775         int32_t *displaymatrix = ifilter->displaymatrix;
776         double theta;
777 
778         if (!displaymatrix)
779             displaymatrix = (int32_t *)av_stream_get_side_data(ist->st, AV_PKT_DATA_DISPLAYMATRIX, NULL);
780         theta = get_rotation(displaymatrix);
781 
782         if (fabs(theta - 90) < 1.0) {
783             ret = insert_filter(&last_filter, &pad_idx, "transpose",
784                                 displaymatrix[3] > 0 ? "cclock_flip" : "clock");
785         } else if (fabs(theta - 180) < 1.0) {
786             if (displaymatrix[0] < 0) {
787                 ret = insert_filter(&last_filter, &pad_idx, "hflip", NULL);
788                 if (ret < 0)
789                     return ret;
790             }
791             if (displaymatrix[4] < 0) {
792                 ret = insert_filter(&last_filter, &pad_idx, "vflip", NULL);
793             }
794         } else if (fabs(theta - 270) < 1.0) {
795             ret = insert_filter(&last_filter, &pad_idx, "transpose",
796                                 displaymatrix[3] < 0 ? "clock_flip" : "cclock");
797         } else if (fabs(theta) > 1.0) {
798             char rotate_buf[64];
799             snprintf(rotate_buf, sizeof(rotate_buf), "%f*PI/180", theta);
800             ret = insert_filter(&last_filter, &pad_idx, "rotate", rotate_buf);
801         } else if (fabs(theta) < 1.0) {
802             if (displaymatrix && displaymatrix[4] < 0) {
803                 ret = insert_filter(&last_filter, &pad_idx, "vflip", NULL);
804             }
805         }
806         if (ret < 0)
807             return ret;
808     }
809 
810     snprintf(name, sizeof(name), "trim_in_%d_%d",
811              ist->file_index, ist->st->index);
812     if (copy_ts) {
813         tsoffset = f->start_time == AV_NOPTS_VALUE ? 0 : f->start_time;
814         if (!start_at_zero && f->ctx->start_time != AV_NOPTS_VALUE)
815             tsoffset += f->ctx->start_time;
816     }
817     ret = insert_trim(((f->start_time == AV_NOPTS_VALUE) || !f->accurate_seek) ?
818                       AV_NOPTS_VALUE : tsoffset, f->recording_time,
819                       &last_filter, &pad_idx, name);
820     if (ret < 0)
821         return ret;
822 
823     if ((ret = avfilter_link(last_filter, 0, in->filter_ctx, in->pad_idx)) < 0)
824         return ret;
825     return 0;
826 fail:
827     av_freep(&par);
828 
829     return ret;
830 }
831 
configure_input_audio_filter(FilterGraph *fg, InputFilter *ifilter, AVFilterInOut *in)832 static int configure_input_audio_filter(FilterGraph *fg, InputFilter *ifilter,
833                                         AVFilterInOut *in)
834 {
835     AVFilterContext *last_filter;
836     const AVFilter *abuffer_filt = avfilter_get_by_name("abuffer");
837     InputStream *ist = ifilter->ist;
838     InputFile     *f = input_files[ist->file_index];
839     AVBPrint args;
840     char name[255];
841     int ret, pad_idx = 0;
842     int64_t tsoffset = 0;
843 
844     if (ist->dec_ctx->codec_type != AVMEDIA_TYPE_AUDIO) {
845         av_log(NULL, AV_LOG_ERROR, "Cannot connect audio filter to non audio input\n");
846         return AVERROR(EINVAL);
847     }
848 
849     av_bprint_init(&args, 0, AV_BPRINT_SIZE_AUTOMATIC);
850     av_bprintf(&args, "time_base=%d/%d:sample_rate=%d:sample_fmt=%s",
851              1, ifilter->sample_rate,
852              ifilter->sample_rate,
853              av_get_sample_fmt_name(ifilter->format));
854     if (av_channel_layout_check(&ifilter->ch_layout) &&
855         ifilter->ch_layout.order != AV_CHANNEL_ORDER_UNSPEC) {
856         av_bprintf(&args, ":channel_layout=");
857         av_channel_layout_describe_bprint(&ifilter->ch_layout, &args);
858     } else
859         av_bprintf(&args, ":channels=%d", ifilter->ch_layout.nb_channels);
860     snprintf(name, sizeof(name), "graph_%d_in_%d_%d", fg->index,
861              ist->file_index, ist->st->index);
862 
863     if ((ret = avfilter_graph_create_filter(&ifilter->filter, abuffer_filt,
864                                             name, args.str, NULL,
865                                             fg->graph)) < 0)
866         return ret;
867     last_filter = ifilter->filter;
868 
869 #define AUTO_INSERT_FILTER_INPUT(opt_name, filter_name, arg) do {                 \
870     AVFilterContext *filt_ctx;                                              \
871                                                                             \
872     av_log(NULL, AV_LOG_INFO, opt_name " is forwarded to lavfi "            \
873            "similarly to -af " filter_name "=%s.\n", arg);                  \
874                                                                             \
875     snprintf(name, sizeof(name), "graph_%d_%s_in_%d_%d",      \
876                 fg->index, filter_name, ist->file_index, ist->st->index);   \
877     ret = avfilter_graph_create_filter(&filt_ctx,                           \
878                                        avfilter_get_by_name(filter_name),   \
879                                        name, arg, NULL, fg->graph);         \
880     if (ret < 0)                                                            \
881         return ret;                                                         \
882                                                                             \
883     ret = avfilter_link(last_filter, 0, filt_ctx, 0);                       \
884     if (ret < 0)                                                            \
885         return ret;                                                         \
886                                                                             \
887     last_filter = filt_ctx;                                                 \
888 } while (0)
889 
890     if (audio_sync_method > 0) {
891         char args[256] = {0};
892 
893         av_strlcatf(args, sizeof(args), "async=%d", audio_sync_method);
894         if (audio_drift_threshold != 0.1)
895             av_strlcatf(args, sizeof(args), ":min_hard_comp=%f", audio_drift_threshold);
896         if (!fg->reconfiguration)
897             av_strlcatf(args, sizeof(args), ":first_pts=0");
898         AUTO_INSERT_FILTER_INPUT("-async", "aresample", args);
899     }
900 
901 //     if (ost->audio_channels_mapped) {
902 //         int i;
903 //         AVBPrint pan_buf;
904 //         av_bprint_init(&pan_buf, 256, 8192);
905 //         av_bprintf(&pan_buf, "0x%"PRIx64,
906 //                    av_get_default_channel_layout(ost->audio_channels_mapped));
907 //         for (i = 0; i < ost->audio_channels_mapped; i++)
908 //             if (ost->audio_channels_map[i] != -1)
909 //                 av_bprintf(&pan_buf, ":c%d=c%d", i, ost->audio_channels_map[i]);
910 //         AUTO_INSERT_FILTER_INPUT("-map_channel", "pan", pan_buf.str);
911 //         av_bprint_finalize(&pan_buf, NULL);
912 //     }
913 
914     if (audio_volume != 256) {
915         char args[256];
916 
917         av_log(NULL, AV_LOG_WARNING, "-vol has been deprecated. Use the volume "
918                "audio filter instead.\n");
919 
920         snprintf(args, sizeof(args), "%f", audio_volume / 256.);
921         AUTO_INSERT_FILTER_INPUT("-vol", "volume", args);
922     }
923 
924     snprintf(name, sizeof(name), "trim for input stream %d:%d",
925              ist->file_index, ist->st->index);
926     if (copy_ts) {
927         tsoffset = f->start_time == AV_NOPTS_VALUE ? 0 : f->start_time;
928         if (!start_at_zero && f->ctx->start_time != AV_NOPTS_VALUE)
929             tsoffset += f->ctx->start_time;
930     }
931     ret = insert_trim(((f->start_time == AV_NOPTS_VALUE) || !f->accurate_seek) ?
932                       AV_NOPTS_VALUE : tsoffset, f->recording_time,
933                       &last_filter, &pad_idx, name);
934     if (ret < 0)
935         return ret;
936 
937     if ((ret = avfilter_link(last_filter, 0, in->filter_ctx, in->pad_idx)) < 0)
938         return ret;
939 
940     return 0;
941 }
942 
configure_input_filter(FilterGraph *fg, InputFilter *ifilter, AVFilterInOut *in)943 static int configure_input_filter(FilterGraph *fg, InputFilter *ifilter,
944                                   AVFilterInOut *in)
945 {
946     if (!ifilter->ist->dec) {
947         av_log(NULL, AV_LOG_ERROR,
948                "No decoder for stream #%d:%d, filtering impossible\n",
949                ifilter->ist->file_index, ifilter->ist->st->index);
950         return AVERROR_DECODER_NOT_FOUND;
951     }
952     switch (avfilter_pad_get_type(in->filter_ctx->input_pads, in->pad_idx)) {
953     case AVMEDIA_TYPE_VIDEO: return configure_input_video_filter(fg, ifilter, in);
954     case AVMEDIA_TYPE_AUDIO: return configure_input_audio_filter(fg, ifilter, in);
955     default: av_assert0(0); return 0;
956     }
957 }
958 
cleanup_filtergraph(FilterGraph *fg)959 static void cleanup_filtergraph(FilterGraph *fg)
960 {
961     int i;
962     for (i = 0; i < fg->nb_outputs; i++)
963         fg->outputs[i]->filter = (AVFilterContext *)NULL;
964     for (i = 0; i < fg->nb_inputs; i++)
965         fg->inputs[i]->filter = (AVFilterContext *)NULL;
966     avfilter_graph_free(&fg->graph);
967 }
968 
filter_is_buffersrc(const AVFilterContext *f)969 static int filter_is_buffersrc(const AVFilterContext *f)
970 {
971     return f->nb_inputs == 0 &&
972            (!strcmp(f->filter->name, "buffer") ||
973             !strcmp(f->filter->name, "abuffer"));
974 }
975 
graph_is_meta(AVFilterGraph *graph)976 static int graph_is_meta(AVFilterGraph *graph)
977 {
978     for (unsigned i = 0; i < graph->nb_filters; i++) {
979         const AVFilterContext *f = graph->filters[i];
980 
981         /* in addition to filters flagged as meta, also
982          * disregard sinks and buffersources (but not other sources,
983          * since they introduce data we are not aware of)
984          */
985         if (!((f->filter->flags & AVFILTER_FLAG_METADATA_ONLY) ||
986               f->nb_outputs == 0                               ||
987               filter_is_buffersrc(f)))
988             return 0;
989     }
990     return 1;
991 }
992 
configure_filtergraph(FilterGraph *fg)993 int configure_filtergraph(FilterGraph *fg)
994 {
995     AVFilterInOut *inputs, *outputs, *cur;
996     int ret, i, simple = filtergraph_is_simple(fg);
997     const char *graph_desc = simple ? fg->outputs[0]->ost->avfilter :
998                                       fg->graph_desc;
999 
1000     cleanup_filtergraph(fg);
1001     if (!(fg->graph = avfilter_graph_alloc()))
1002         return AVERROR(ENOMEM);
1003 
1004     if (simple) {
1005         OutputStream *ost = fg->outputs[0]->ost;
1006         char args[512];
1007         const AVDictionaryEntry *e = NULL;
1008 
1009         if (filter_nbthreads) {
1010             ret = av_opt_set(fg->graph, "threads", filter_nbthreads, 0);
1011             if (ret < 0)
1012                 goto fail;
1013         } else {
1014             e = av_dict_get(ost->encoder_opts, "threads", NULL, 0);
1015             if (e)
1016                 av_opt_set(fg->graph, "threads", e->value, 0);
1017         }
1018 
1019         args[0] = 0;
1020         e       = NULL;
1021         while ((e = av_dict_get(ost->sws_dict, "", e,
1022                                 AV_DICT_IGNORE_SUFFIX))) {
1023             av_strlcatf(args, sizeof(args), "%s=%s:", e->key, e->value);
1024         }
1025         if (strlen(args)) {
1026             args[strlen(args)-1] = 0;
1027             fg->graph->scale_sws_opts = av_strdup(args);
1028         }
1029 
1030         args[0] = 0;
1031         e       = NULL;
1032         while ((e = av_dict_get(ost->swr_opts, "", e,
1033                                 AV_DICT_IGNORE_SUFFIX))) {
1034             av_strlcatf(args, sizeof(args), "%s=%s:", e->key, e->value);
1035         }
1036         if (strlen(args))
1037             args[strlen(args)-1] = 0;
1038         av_opt_set(fg->graph, "aresample_swr_opts", args, 0);
1039     } else {
1040         fg->graph->nb_threads = filter_complex_nbthreads;
1041     }
1042 
1043     if ((ret = avfilter_graph_parse2(fg->graph, graph_desc, &inputs, &outputs)) < 0)
1044         goto fail;
1045 
1046     ret = hw_device_setup_for_filter(fg);
1047     if (ret < 0)
1048         goto fail;
1049 
1050     if (simple && (!inputs || inputs->next || !outputs || outputs->next)) {
1051         const char *num_inputs;
1052         const char *num_outputs;
1053         if (!outputs) {
1054             num_outputs = "0";
1055         } else if (outputs->next) {
1056             num_outputs = ">1";
1057         } else {
1058             num_outputs = "1";
1059         }
1060         if (!inputs) {
1061             num_inputs = "0";
1062         } else if (inputs->next) {
1063             num_inputs = ">1";
1064         } else {
1065             num_inputs = "1";
1066         }
1067         av_log(NULL, AV_LOG_ERROR, "Simple filtergraph '%s' was expected "
1068                "to have exactly 1 input and 1 output."
1069                " However, it had %s input(s) and %s output(s)."
1070                " Please adjust, or use a complex filtergraph (-filter_complex) instead.\n",
1071                graph_desc, num_inputs, num_outputs);
1072         ret = AVERROR(EINVAL);
1073         goto fail;
1074     }
1075 
1076     for (cur = inputs, i = 0; cur; cur = cur->next, i++)
1077         if ((ret = configure_input_filter(fg, fg->inputs[i], cur)) < 0) {
1078             avfilter_inout_free(&inputs);
1079             avfilter_inout_free(&outputs);
1080             goto fail;
1081         }
1082     avfilter_inout_free(&inputs);
1083 
1084     for (cur = outputs, i = 0; cur; cur = cur->next, i++)
1085         configure_output_filter(fg, fg->outputs[i], cur);
1086     avfilter_inout_free(&outputs);
1087 
1088     if (!auto_conversion_filters)
1089         avfilter_graph_set_auto_convert(fg->graph, AVFILTER_AUTO_CONVERT_NONE);
1090     if ((ret = avfilter_graph_config(fg->graph, NULL)) < 0)
1091         goto fail;
1092 
1093     fg->is_meta = graph_is_meta(fg->graph);
1094 
1095     /* limit the lists of allowed formats to the ones selected, to
1096      * make sure they stay the same if the filtergraph is reconfigured later */
1097     for (i = 0; i < fg->nb_outputs; i++) {
1098         OutputFilter *ofilter = fg->outputs[i];
1099         AVFilterContext *sink = ofilter->filter;
1100 
1101         ofilter->format = av_buffersink_get_format(sink);
1102 
1103         ofilter->width  = av_buffersink_get_w(sink);
1104         ofilter->height = av_buffersink_get_h(sink);
1105 
1106         ofilter->sample_rate    = av_buffersink_get_sample_rate(sink);
1107         av_channel_layout_uninit(&ofilter->ch_layout);
1108         ret = av_buffersink_get_ch_layout(sink, &ofilter->ch_layout);
1109         if (ret < 0)
1110             goto fail;
1111     }
1112 
1113     fg->reconfiguration = 1;
1114 
1115     for (i = 0; i < fg->nb_outputs; i++) {
1116         OutputStream *ost = fg->outputs[i]->ost;
1117         if (!ost->enc) {
1118             /* identical to the same check in ffmpeg.c, needed because
1119                complex filter graphs are initialized earlier */
1120             av_log(NULL, AV_LOG_ERROR, "Encoder (codec %s) not found for output stream #%d:%d\n",
1121                      avcodec_get_name(ost->st->codecpar->codec_id), ost->file_index, ost->index);
1122             ret = AVERROR(EINVAL);
1123             goto fail;
1124         }
1125         if (ost->enc->type == AVMEDIA_TYPE_AUDIO &&
1126             !(ost->enc->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE))
1127             av_buffersink_set_frame_size(ost->filter->filter,
1128                                          ost->enc_ctx->frame_size);
1129     }
1130 
1131     for (i = 0; i < fg->nb_inputs; i++) {
1132         AVFrame *tmp;
1133         while (av_fifo_read(fg->inputs[i]->frame_queue, &tmp, 1) >= 0) {
1134             ret = av_buffersrc_add_frame(fg->inputs[i]->filter, tmp);
1135             av_frame_free(&tmp);
1136             if (ret < 0)
1137                 goto fail;
1138         }
1139     }
1140 
1141     /* send the EOFs for the finished inputs */
1142     for (i = 0; i < fg->nb_inputs; i++) {
1143         if (fg->inputs[i]->eof) {
1144             ret = av_buffersrc_add_frame(fg->inputs[i]->filter, NULL);
1145             if (ret < 0)
1146                 goto fail;
1147         }
1148     }
1149 
1150     /* process queued up subtitle packets */
1151     for (i = 0; i < fg->nb_inputs; i++) {
1152         InputStream *ist = fg->inputs[i]->ist;
1153         if (ist->sub2video.sub_queue && ist->sub2video.frame) {
1154             AVSubtitle tmp;
1155             while (av_fifo_read(ist->sub2video.sub_queue, &tmp, 1) >= 0) {
1156                 sub2video_update(ist, INT64_MIN, &tmp);
1157                 avsubtitle_free(&tmp);
1158             }
1159         }
1160     }
1161 
1162     return 0;
1163 
1164 fail:
1165     cleanup_filtergraph(fg);
1166     return ret;
1167 }
1168 
ifilter_parameters_from_frame(InputFilter *ifilter, const AVFrame *frame)1169 int ifilter_parameters_from_frame(InputFilter *ifilter, const AVFrame *frame)
1170 {
1171     AVFrameSideData *sd;
1172     int ret;
1173 
1174     av_buffer_unref(&ifilter->hw_frames_ctx);
1175 
1176     ifilter->format = frame->format;
1177 
1178     ifilter->width               = frame->width;
1179     ifilter->height              = frame->height;
1180     ifilter->sample_aspect_ratio = frame->sample_aspect_ratio;
1181 
1182     ifilter->sample_rate         = frame->sample_rate;
1183     ret = av_channel_layout_copy(&ifilter->ch_layout, &frame->ch_layout);
1184     if (ret < 0)
1185         return ret;
1186 
1187     av_freep(&ifilter->displaymatrix);
1188     sd = av_frame_get_side_data(frame, AV_FRAME_DATA_DISPLAYMATRIX);
1189     if (sd)
1190         ifilter->displaymatrix = av_memdup(sd->data, sizeof(int32_t) * 9);
1191 
1192     if (frame->hw_frames_ctx) {
1193         ifilter->hw_frames_ctx = av_buffer_ref(frame->hw_frames_ctx);
1194         if (!ifilter->hw_frames_ctx)
1195             return AVERROR(ENOMEM);
1196     }
1197 
1198     return 0;
1199 }
1200 
filtergraph_is_simple(FilterGraph *fg)1201 int filtergraph_is_simple(FilterGraph *fg)
1202 {
1203     return !fg->graph_desc;
1204 }
1205