ffmpeg_filter.c 44 KB

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