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.

1222 lines
45KB

  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 (audio_volume != 256 && 0) {
  556. char args[256];
  557. snprintf(args, sizeof(args), "%f", audio_volume / 256.);
  558. AUTO_INSERT_FILTER("-vol", "volume", args);
  559. }
  560. if (ost->apad && of->shortest) {
  561. char args[256];
  562. int i;
  563. for (i=0; i<of->ctx->nb_streams; i++)
  564. if (of->ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
  565. break;
  566. if (i<of->ctx->nb_streams) {
  567. snprintf(args, sizeof(args), "%s", ost->apad);
  568. AUTO_INSERT_FILTER("-apad", "apad", args);
  569. }
  570. }
  571. snprintf(name, sizeof(name), "trim for output stream %d:%d",
  572. ost->file_index, ost->index);
  573. ret = insert_trim(of->start_time, of->recording_time,
  574. &last_filter, &pad_idx, name);
  575. if (ret < 0)
  576. return ret;
  577. if ((ret = avfilter_link(last_filter, pad_idx, ofilter->filter, 0)) < 0)
  578. return ret;
  579. return 0;
  580. }
  581. int configure_output_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
  582. {
  583. if (!ofilter->ost) {
  584. av_log(NULL, AV_LOG_FATAL, "Filter %s has an unconnected output\n", ofilter->name);
  585. exit_program(1);
  586. }
  587. switch (avfilter_pad_get_type(out->filter_ctx->output_pads, out->pad_idx)) {
  588. case AVMEDIA_TYPE_VIDEO: return configure_output_video_filter(fg, ofilter, out);
  589. case AVMEDIA_TYPE_AUDIO: return configure_output_audio_filter(fg, ofilter, out);
  590. default: av_assert0(0);
  591. }
  592. }
  593. void check_filter_outputs(void)
  594. {
  595. int i;
  596. for (i = 0; i < nb_filtergraphs; i++) {
  597. int n;
  598. for (n = 0; n < filtergraphs[i]->nb_outputs; n++) {
  599. OutputFilter *output = filtergraphs[i]->outputs[n];
  600. if (!output->ost) {
  601. av_log(NULL, AV_LOG_FATAL, "Filter %s has an unconnected output\n", output->name);
  602. exit_program(1);
  603. }
  604. }
  605. }
  606. }
  607. static int sub2video_prepare(InputStream *ist, InputFilter *ifilter)
  608. {
  609. AVFormatContext *avf = input_files[ist->file_index]->ctx;
  610. int i, w, h;
  611. /* Compute the size of the canvas for the subtitles stream.
  612. If the subtitles codecpar has set a size, use it. Otherwise use the
  613. maximum dimensions of the video streams in the same file. */
  614. w = ifilter->width;
  615. h = ifilter->height;
  616. if (!(w && h)) {
  617. for (i = 0; i < avf->nb_streams; i++) {
  618. if (avf->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
  619. w = FFMAX(w, avf->streams[i]->codecpar->width);
  620. h = FFMAX(h, avf->streams[i]->codecpar->height);
  621. }
  622. }
  623. if (!(w && h)) {
  624. w = FFMAX(w, 720);
  625. h = FFMAX(h, 576);
  626. }
  627. av_log(avf, AV_LOG_INFO, "sub2video: using %dx%d canvas\n", w, h);
  628. }
  629. ist->sub2video.w = ifilter->width = w;
  630. ist->sub2video.h = ifilter->height = h;
  631. ifilter->width = ist->dec_ctx->width ? ist->dec_ctx->width : ist->sub2video.w;
  632. ifilter->height = ist->dec_ctx->height ? ist->dec_ctx->height : ist->sub2video.h;
  633. /* rectangles are AV_PIX_FMT_PAL8, but we have no guarantee that the
  634. palettes for all rectangles are identical or compatible */
  635. ifilter->format = AV_PIX_FMT_RGB32;
  636. ist->sub2video.frame = av_frame_alloc();
  637. if (!ist->sub2video.frame)
  638. return AVERROR(ENOMEM);
  639. ist->sub2video.last_pts = INT64_MIN;
  640. ist->sub2video.end_pts = INT64_MIN;
  641. /* sub2video structure has been (re-)initialized.
  642. Mark it as such so that the system will be
  643. initialized with the first received heartbeat. */
  644. ist->sub2video.initialize = 1;
  645. return 0;
  646. }
  647. static int configure_input_video_filter(FilterGraph *fg, InputFilter *ifilter,
  648. AVFilterInOut *in)
  649. {
  650. AVFilterContext *last_filter;
  651. const AVFilter *buffer_filt = avfilter_get_by_name("buffer");
  652. InputStream *ist = ifilter->ist;
  653. InputFile *f = input_files[ist->file_index];
  654. AVRational tb = ist->framerate.num ? av_inv_q(ist->framerate) :
  655. ist->st->time_base;
  656. AVRational fr = ist->framerate;
  657. AVRational sar;
  658. AVBPrint args;
  659. char name[255];
  660. int ret, pad_idx = 0;
  661. int64_t tsoffset = 0;
  662. AVBufferSrcParameters *par = av_buffersrc_parameters_alloc();
  663. if (!par)
  664. return AVERROR(ENOMEM);
  665. memset(par, 0, sizeof(*par));
  666. par->format = AV_PIX_FMT_NONE;
  667. if (ist->dec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) {
  668. av_log(NULL, AV_LOG_ERROR, "Cannot connect video filter to audio input\n");
  669. ret = AVERROR(EINVAL);
  670. goto fail;
  671. }
  672. if (!fr.num)
  673. fr = av_guess_frame_rate(input_files[ist->file_index]->ctx, ist->st, NULL);
  674. if (ist->dec_ctx->codec_type == AVMEDIA_TYPE_SUBTITLE) {
  675. ret = sub2video_prepare(ist, ifilter);
  676. if (ret < 0)
  677. goto fail;
  678. }
  679. sar = ifilter->sample_aspect_ratio;
  680. if(!sar.den)
  681. sar = (AVRational){0,1};
  682. av_bprint_init(&args, 0, AV_BPRINT_SIZE_AUTOMATIC);
  683. av_bprintf(&args,
  684. "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:"
  685. "pixel_aspect=%d/%d",
  686. ifilter->width, ifilter->height, ifilter->format,
  687. tb.num, tb.den, sar.num, sar.den);
  688. if (fr.num && fr.den)
  689. av_bprintf(&args, ":frame_rate=%d/%d", fr.num, fr.den);
  690. snprintf(name, sizeof(name), "graph %d input from stream %d:%d", fg->index,
  691. ist->file_index, ist->st->index);
  692. if ((ret = avfilter_graph_create_filter(&ifilter->filter, buffer_filt, name,
  693. args.str, NULL, fg->graph)) < 0)
  694. goto fail;
  695. par->hw_frames_ctx = ifilter->hw_frames_ctx;
  696. ret = av_buffersrc_parameters_set(ifilter->filter, par);
  697. if (ret < 0)
  698. goto fail;
  699. av_freep(&par);
  700. last_filter = ifilter->filter;
  701. if (ist->autorotate) {
  702. double theta = get_rotation(ist->st);
  703. if (fabs(theta - 90) < 1.0) {
  704. ret = insert_filter(&last_filter, &pad_idx, "transpose", "clock");
  705. } else if (fabs(theta - 180) < 1.0) {
  706. ret = insert_filter(&last_filter, &pad_idx, "hflip", NULL);
  707. if (ret < 0)
  708. return ret;
  709. ret = insert_filter(&last_filter, &pad_idx, "vflip", NULL);
  710. } else if (fabs(theta - 270) < 1.0) {
  711. ret = insert_filter(&last_filter, &pad_idx, "transpose", "cclock");
  712. } else if (fabs(theta) > 1.0) {
  713. char rotate_buf[64];
  714. snprintf(rotate_buf, sizeof(rotate_buf), "%f*PI/180", theta);
  715. ret = insert_filter(&last_filter, &pad_idx, "rotate", rotate_buf);
  716. }
  717. if (ret < 0)
  718. return ret;
  719. }
  720. if (do_deinterlace) {
  721. AVFilterContext *yadif;
  722. snprintf(name, sizeof(name), "deinterlace_in_%d_%d",
  723. ist->file_index, ist->st->index);
  724. if ((ret = avfilter_graph_create_filter(&yadif,
  725. avfilter_get_by_name("yadif"),
  726. name, "", NULL,
  727. fg->graph)) < 0)
  728. return ret;
  729. if ((ret = avfilter_link(last_filter, 0, yadif, 0)) < 0)
  730. return ret;
  731. last_filter = yadif;
  732. }
  733. snprintf(name, sizeof(name), "trim_in_%d_%d",
  734. ist->file_index, ist->st->index);
  735. if (copy_ts) {
  736. tsoffset = f->start_time == AV_NOPTS_VALUE ? 0 : f->start_time;
  737. if (!start_at_zero && f->ctx->start_time != AV_NOPTS_VALUE)
  738. tsoffset += f->ctx->start_time;
  739. }
  740. ret = insert_trim(((f->start_time == AV_NOPTS_VALUE) || !f->accurate_seek) ?
  741. AV_NOPTS_VALUE : tsoffset, f->recording_time,
  742. &last_filter, &pad_idx, name);
  743. if (ret < 0)
  744. return ret;
  745. if ((ret = avfilter_link(last_filter, 0, in->filter_ctx, in->pad_idx)) < 0)
  746. return ret;
  747. return 0;
  748. fail:
  749. av_freep(&par);
  750. return ret;
  751. }
  752. static int configure_input_audio_filter(FilterGraph *fg, InputFilter *ifilter,
  753. AVFilterInOut *in)
  754. {
  755. AVFilterContext *last_filter;
  756. const AVFilter *abuffer_filt = avfilter_get_by_name("abuffer");
  757. InputStream *ist = ifilter->ist;
  758. InputFile *f = input_files[ist->file_index];
  759. AVBPrint args;
  760. char name[255];
  761. int ret, pad_idx = 0;
  762. int64_t tsoffset = 0;
  763. if (ist->dec_ctx->codec_type != AVMEDIA_TYPE_AUDIO) {
  764. av_log(NULL, AV_LOG_ERROR, "Cannot connect audio filter to non audio input\n");
  765. return AVERROR(EINVAL);
  766. }
  767. av_bprint_init(&args, 0, AV_BPRINT_SIZE_AUTOMATIC);
  768. av_bprintf(&args, "time_base=%d/%d:sample_rate=%d:sample_fmt=%s",
  769. 1, ifilter->sample_rate,
  770. ifilter->sample_rate,
  771. av_get_sample_fmt_name(ifilter->format));
  772. if (ifilter->channel_layout)
  773. av_bprintf(&args, ":channel_layout=0x%"PRIx64,
  774. ifilter->channel_layout);
  775. else
  776. av_bprintf(&args, ":channels=%d", ifilter->channels);
  777. snprintf(name, sizeof(name), "graph_%d_in_%d_%d", fg->index,
  778. ist->file_index, ist->st->index);
  779. if ((ret = avfilter_graph_create_filter(&ifilter->filter, abuffer_filt,
  780. name, args.str, NULL,
  781. fg->graph)) < 0)
  782. return ret;
  783. last_filter = ifilter->filter;
  784. #define AUTO_INSERT_FILTER_INPUT(opt_name, filter_name, arg) do { \
  785. AVFilterContext *filt_ctx; \
  786. \
  787. av_log(NULL, AV_LOG_INFO, opt_name " is forwarded to lavfi " \
  788. "similarly to -af " filter_name "=%s.\n", arg); \
  789. \
  790. snprintf(name, sizeof(name), "graph_%d_%s_in_%d_%d", \
  791. fg->index, filter_name, ist->file_index, ist->st->index); \
  792. ret = avfilter_graph_create_filter(&filt_ctx, \
  793. avfilter_get_by_name(filter_name), \
  794. name, arg, NULL, fg->graph); \
  795. if (ret < 0) \
  796. return ret; \
  797. \
  798. ret = avfilter_link(last_filter, 0, filt_ctx, 0); \
  799. if (ret < 0) \
  800. return ret; \
  801. \
  802. last_filter = filt_ctx; \
  803. } while (0)
  804. if (audio_sync_method > 0) {
  805. char args[256] = {0};
  806. av_strlcatf(args, sizeof(args), "async=%d", audio_sync_method);
  807. if (audio_drift_threshold != 0.1)
  808. av_strlcatf(args, sizeof(args), ":min_hard_comp=%f", audio_drift_threshold);
  809. if (!fg->reconfiguration)
  810. av_strlcatf(args, sizeof(args), ":first_pts=0");
  811. AUTO_INSERT_FILTER_INPUT("-async", "aresample", args);
  812. }
  813. // if (ost->audio_channels_mapped) {
  814. // int i;
  815. // AVBPrint pan_buf;
  816. // av_bprint_init(&pan_buf, 256, 8192);
  817. // av_bprintf(&pan_buf, "0x%"PRIx64,
  818. // av_get_default_channel_layout(ost->audio_channels_mapped));
  819. // for (i = 0; i < ost->audio_channels_mapped; i++)
  820. // if (ost->audio_channels_map[i] != -1)
  821. // av_bprintf(&pan_buf, ":c%d=c%d", i, ost->audio_channels_map[i]);
  822. // AUTO_INSERT_FILTER_INPUT("-map_channel", "pan", pan_buf.str);
  823. // av_bprint_finalize(&pan_buf, NULL);
  824. // }
  825. if (audio_volume != 256) {
  826. char args[256];
  827. av_log(NULL, AV_LOG_WARNING, "-vol has been deprecated. Use the volume "
  828. "audio filter instead.\n");
  829. snprintf(args, sizeof(args), "%f", audio_volume / 256.);
  830. AUTO_INSERT_FILTER_INPUT("-vol", "volume", args);
  831. }
  832. snprintf(name, sizeof(name), "trim for input stream %d:%d",
  833. ist->file_index, ist->st->index);
  834. if (copy_ts) {
  835. tsoffset = f->start_time == AV_NOPTS_VALUE ? 0 : f->start_time;
  836. if (!start_at_zero && f->ctx->start_time != AV_NOPTS_VALUE)
  837. tsoffset += f->ctx->start_time;
  838. }
  839. ret = insert_trim(((f->start_time == AV_NOPTS_VALUE) || !f->accurate_seek) ?
  840. AV_NOPTS_VALUE : tsoffset, f->recording_time,
  841. &last_filter, &pad_idx, name);
  842. if (ret < 0)
  843. return ret;
  844. if ((ret = avfilter_link(last_filter, 0, in->filter_ctx, in->pad_idx)) < 0)
  845. return ret;
  846. return 0;
  847. }
  848. static int configure_input_filter(FilterGraph *fg, InputFilter *ifilter,
  849. AVFilterInOut *in)
  850. {
  851. if (!ifilter->ist->dec) {
  852. av_log(NULL, AV_LOG_ERROR,
  853. "No decoder for stream #%d:%d, filtering impossible\n",
  854. ifilter->ist->file_index, ifilter->ist->st->index);
  855. return AVERROR_DECODER_NOT_FOUND;
  856. }
  857. switch (avfilter_pad_get_type(in->filter_ctx->input_pads, in->pad_idx)) {
  858. case AVMEDIA_TYPE_VIDEO: return configure_input_video_filter(fg, ifilter, in);
  859. case AVMEDIA_TYPE_AUDIO: return configure_input_audio_filter(fg, ifilter, in);
  860. default: av_assert0(0);
  861. }
  862. }
  863. static void cleanup_filtergraph(FilterGraph *fg)
  864. {
  865. int i;
  866. for (i = 0; i < fg->nb_outputs; i++)
  867. fg->outputs[i]->filter = (AVFilterContext *)NULL;
  868. for (i = 0; i < fg->nb_inputs; i++)
  869. fg->inputs[i]->filter = (AVFilterContext *)NULL;
  870. avfilter_graph_free(&fg->graph);
  871. }
  872. int configure_filtergraph(FilterGraph *fg)
  873. {
  874. AVFilterInOut *inputs, *outputs, *cur;
  875. int ret, i, simple = filtergraph_is_simple(fg);
  876. const char *graph_desc = simple ? fg->outputs[0]->ost->avfilter :
  877. fg->graph_desc;
  878. cleanup_filtergraph(fg);
  879. if (!(fg->graph = avfilter_graph_alloc()))
  880. return AVERROR(ENOMEM);
  881. if (simple) {
  882. OutputStream *ost = fg->outputs[0]->ost;
  883. char args[512];
  884. AVDictionaryEntry *e = NULL;
  885. fg->graph->nb_threads = filter_nbthreads;
  886. args[0] = 0;
  887. while ((e = av_dict_get(ost->sws_dict, "", e,
  888. AV_DICT_IGNORE_SUFFIX))) {
  889. av_strlcatf(args, sizeof(args), "%s=%s:", e->key, e->value);
  890. }
  891. if (strlen(args))
  892. args[strlen(args)-1] = 0;
  893. fg->graph->scale_sws_opts = av_strdup(args);
  894. args[0] = 0;
  895. while ((e = av_dict_get(ost->swr_opts, "", e,
  896. AV_DICT_IGNORE_SUFFIX))) {
  897. av_strlcatf(args, sizeof(args), "%s=%s:", e->key, e->value);
  898. }
  899. if (strlen(args))
  900. args[strlen(args)-1] = 0;
  901. av_opt_set(fg->graph, "aresample_swr_opts", args, 0);
  902. args[0] = '\0';
  903. while ((e = av_dict_get(fg->outputs[0]->ost->resample_opts, "", e,
  904. AV_DICT_IGNORE_SUFFIX))) {
  905. av_strlcatf(args, sizeof(args), "%s=%s:", e->key, e->value);
  906. }
  907. if (strlen(args))
  908. args[strlen(args) - 1] = '\0';
  909. e = av_dict_get(ost->encoder_opts, "threads", NULL, 0);
  910. if (e)
  911. av_opt_set(fg->graph, "threads", e->value, 0);
  912. } else {
  913. fg->graph->nb_threads = filter_complex_nbthreads;
  914. }
  915. if ((ret = avfilter_graph_parse2(fg->graph, graph_desc, &inputs, &outputs)) < 0)
  916. goto fail;
  917. ret = hw_device_setup_for_filter(fg);
  918. if (ret < 0)
  919. goto fail;
  920. if (simple && (!inputs || inputs->next || !outputs || outputs->next)) {
  921. const char *num_inputs;
  922. const char *num_outputs;
  923. if (!outputs) {
  924. num_outputs = "0";
  925. } else if (outputs->next) {
  926. num_outputs = ">1";
  927. } else {
  928. num_outputs = "1";
  929. }
  930. if (!inputs) {
  931. num_inputs = "0";
  932. } else if (inputs->next) {
  933. num_inputs = ">1";
  934. } else {
  935. num_inputs = "1";
  936. }
  937. av_log(NULL, AV_LOG_ERROR, "Simple filtergraph '%s' was expected "
  938. "to have exactly 1 input and 1 output."
  939. " However, it had %s input(s) and %s output(s)."
  940. " Please adjust, or use a complex filtergraph (-filter_complex) instead.\n",
  941. graph_desc, num_inputs, num_outputs);
  942. ret = AVERROR(EINVAL);
  943. goto fail;
  944. }
  945. for (cur = inputs, i = 0; cur; cur = cur->next, i++)
  946. if ((ret = configure_input_filter(fg, fg->inputs[i], cur)) < 0) {
  947. avfilter_inout_free(&inputs);
  948. avfilter_inout_free(&outputs);
  949. goto fail;
  950. }
  951. avfilter_inout_free(&inputs);
  952. for (cur = outputs, i = 0; cur; cur = cur->next, i++)
  953. configure_output_filter(fg, fg->outputs[i], cur);
  954. avfilter_inout_free(&outputs);
  955. if (!auto_conversion_filters)
  956. avfilter_graph_set_auto_convert(fg->graph, AVFILTER_AUTO_CONVERT_NONE);
  957. if ((ret = avfilter_graph_config(fg->graph, NULL)) < 0)
  958. goto fail;
  959. /* limit the lists of allowed formats to the ones selected, to
  960. * make sure they stay the same if the filtergraph is reconfigured later */
  961. for (i = 0; i < fg->nb_outputs; i++) {
  962. OutputFilter *ofilter = fg->outputs[i];
  963. AVFilterContext *sink = ofilter->filter;
  964. ofilter->format = av_buffersink_get_format(sink);
  965. ofilter->width = av_buffersink_get_w(sink);
  966. ofilter->height = av_buffersink_get_h(sink);
  967. ofilter->sample_rate = av_buffersink_get_sample_rate(sink);
  968. ofilter->channel_layout = av_buffersink_get_channel_layout(sink);
  969. }
  970. fg->reconfiguration = 1;
  971. for (i = 0; i < fg->nb_outputs; i++) {
  972. OutputStream *ost = fg->outputs[i]->ost;
  973. if (!ost->enc) {
  974. /* identical to the same check in ffmpeg.c, needed because
  975. complex filter graphs are initialized earlier */
  976. av_log(NULL, AV_LOG_ERROR, "Encoder (codec %s) not found for output stream #%d:%d\n",
  977. avcodec_get_name(ost->st->codecpar->codec_id), ost->file_index, ost->index);
  978. ret = AVERROR(EINVAL);
  979. goto fail;
  980. }
  981. if (ost->enc->type == AVMEDIA_TYPE_AUDIO &&
  982. !(ost->enc->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE))
  983. av_buffersink_set_frame_size(ost->filter->filter,
  984. ost->enc_ctx->frame_size);
  985. }
  986. for (i = 0; i < fg->nb_inputs; i++) {
  987. while (av_fifo_size(fg->inputs[i]->frame_queue)) {
  988. AVFrame *tmp;
  989. av_fifo_generic_read(fg->inputs[i]->frame_queue, &tmp, sizeof(tmp), NULL);
  990. ret = av_buffersrc_add_frame(fg->inputs[i]->filter, tmp);
  991. av_frame_free(&tmp);
  992. if (ret < 0)
  993. goto fail;
  994. }
  995. }
  996. /* send the EOFs for the finished inputs */
  997. for (i = 0; i < fg->nb_inputs; i++) {
  998. if (fg->inputs[i]->eof) {
  999. ret = av_buffersrc_add_frame(fg->inputs[i]->filter, NULL);
  1000. if (ret < 0)
  1001. goto fail;
  1002. }
  1003. }
  1004. /* process queued up subtitle packets */
  1005. for (i = 0; i < fg->nb_inputs; i++) {
  1006. InputStream *ist = fg->inputs[i]->ist;
  1007. if (ist->sub2video.sub_queue && ist->sub2video.frame) {
  1008. while (av_fifo_size(ist->sub2video.sub_queue)) {
  1009. AVSubtitle tmp;
  1010. av_fifo_generic_read(ist->sub2video.sub_queue, &tmp, sizeof(tmp), NULL);
  1011. sub2video_update(ist, INT64_MIN, &tmp);
  1012. avsubtitle_free(&tmp);
  1013. }
  1014. }
  1015. }
  1016. return 0;
  1017. fail:
  1018. cleanup_filtergraph(fg);
  1019. return ret;
  1020. }
  1021. int ifilter_parameters_from_frame(InputFilter *ifilter, const AVFrame *frame)
  1022. {
  1023. av_buffer_unref(&ifilter->hw_frames_ctx);
  1024. ifilter->format = frame->format;
  1025. ifilter->width = frame->width;
  1026. ifilter->height = frame->height;
  1027. ifilter->sample_aspect_ratio = frame->sample_aspect_ratio;
  1028. ifilter->sample_rate = frame->sample_rate;
  1029. ifilter->channels = frame->channels;
  1030. ifilter->channel_layout = frame->channel_layout;
  1031. if (frame->hw_frames_ctx) {
  1032. ifilter->hw_frames_ctx = av_buffer_ref(frame->hw_frames_ctx);
  1033. if (!ifilter->hw_frames_ctx)
  1034. return AVERROR(ENOMEM);
  1035. }
  1036. return 0;
  1037. }
  1038. int ist_in_filtergraph(FilterGraph *fg, InputStream *ist)
  1039. {
  1040. int i;
  1041. for (i = 0; i < fg->nb_inputs; i++)
  1042. if (fg->inputs[i]->ist == ist)
  1043. return 1;
  1044. return 0;
  1045. }
  1046. int filtergraph_is_simple(FilterGraph *fg)
  1047. {
  1048. return !fg->graph_desc;
  1049. }