ffmpeg_filter.c 41 KB

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