ffmpeg_filter.c 44 KB

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