You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1212 lines
44KB

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