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.

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