ffmpeg_filter.c 45 KB

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