ffmpeg_filter.c 42 KB

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