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.

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