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