ffmpeg_filter.c 43 KB

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