ffmpeg_filter.c 44 KB

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