ffmpeg_filter.c 44 KB

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