ffmpeg_filter.c 43 KB

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