ffmpeg_filter.c 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995
  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. #include "libavutil/timestamp.h"
  36. typedef struct FilterGraphPriv {
  37. FilterGraph fg;
  38. int is_simple;
  39. const char *graph_desc;
  40. // frame for temporarily holding output from the filtergraph
  41. AVFrame *frame;
  42. } FilterGraphPriv;
  43. static FilterGraphPriv *fgp_from_fg(FilterGraph *fg)
  44. {
  45. return (FilterGraphPriv*)fg;
  46. }
  47. static const FilterGraphPriv *cfgp_from_cfg(const FilterGraph *fg)
  48. {
  49. return (const FilterGraphPriv*)fg;
  50. }
  51. typedef struct InputFilterPriv {
  52. InputFilter ifilter;
  53. AVFilterContext *filter;
  54. InputStream *ist;
  55. // used to hold submitted input
  56. AVFrame *frame;
  57. /* for filters that are not yet bound to an input stream,
  58. * this stores the input linklabel, if any */
  59. uint8_t *linklabel;
  60. // filter data type
  61. enum AVMediaType type;
  62. // source data type: AVMEDIA_TYPE_SUBTITLE for sub2video,
  63. // same as type otherwise
  64. enum AVMediaType type_src;
  65. int eof;
  66. // parameters configured for this input
  67. int format;
  68. int width, height;
  69. AVRational sample_aspect_ratio;
  70. int sample_rate;
  71. AVChannelLayout ch_layout;
  72. AVRational time_base;
  73. AVFifo *frame_queue;
  74. AVBufferRef *hw_frames_ctx;
  75. int displaymatrix_present;
  76. int32_t displaymatrix[9];
  77. // fallback parameters to use when no input is ever sent
  78. struct {
  79. int format;
  80. int width;
  81. int height;
  82. AVRational sample_aspect_ratio;
  83. int sample_rate;
  84. AVChannelLayout ch_layout;
  85. } fallback;
  86. struct {
  87. ///< queue of AVSubtitle* before filter init
  88. AVFifo *queue;
  89. AVFrame *frame;
  90. int64_t last_pts;
  91. int64_t end_pts;
  92. ///< marks if sub2video_update should force an initialization
  93. unsigned int initialize;
  94. } sub2video;
  95. } InputFilterPriv;
  96. static InputFilterPriv *ifp_from_ifilter(InputFilter *ifilter)
  97. {
  98. return (InputFilterPriv*)ifilter;
  99. }
  100. static int sub2video_get_blank_frame(InputFilterPriv *ifp)
  101. {
  102. AVFrame *frame = ifp->sub2video.frame;
  103. int ret;
  104. av_frame_unref(frame);
  105. frame->width = ifp->width;
  106. frame->height = ifp->height;
  107. frame->format = ifp->format;
  108. ret = av_frame_get_buffer(frame, 0);
  109. if (ret < 0)
  110. return ret;
  111. memset(frame->data[0], 0, frame->height * frame->linesize[0]);
  112. return 0;
  113. }
  114. static void sub2video_copy_rect(uint8_t *dst, int dst_linesize, int w, int h,
  115. AVSubtitleRect *r)
  116. {
  117. uint32_t *pal, *dst2;
  118. uint8_t *src, *src2;
  119. int x, y;
  120. if (r->type != SUBTITLE_BITMAP) {
  121. av_log(NULL, AV_LOG_WARNING, "sub2video: non-bitmap subtitle\n");
  122. return;
  123. }
  124. if (r->x < 0 || r->x + r->w > w || r->y < 0 || r->y + r->h > h) {
  125. av_log(NULL, AV_LOG_WARNING, "sub2video: rectangle (%d %d %d %d) overflowing %d %d\n",
  126. r->x, r->y, r->w, r->h, w, h
  127. );
  128. return;
  129. }
  130. dst += r->y * dst_linesize + r->x * 4;
  131. src = r->data[0];
  132. pal = (uint32_t *)r->data[1];
  133. for (y = 0; y < r->h; y++) {
  134. dst2 = (uint32_t *)dst;
  135. src2 = src;
  136. for (x = 0; x < r->w; x++)
  137. *(dst2++) = pal[*(src2++)];
  138. dst += dst_linesize;
  139. src += r->linesize[0];
  140. }
  141. }
  142. static void sub2video_push_ref(InputFilterPriv *ifp, int64_t pts)
  143. {
  144. AVFrame *frame = ifp->sub2video.frame;
  145. int ret;
  146. av_assert1(frame->data[0]);
  147. ifp->sub2video.last_pts = frame->pts = pts;
  148. ret = av_buffersrc_add_frame_flags(ifp->filter, frame,
  149. AV_BUFFERSRC_FLAG_KEEP_REF |
  150. AV_BUFFERSRC_FLAG_PUSH);
  151. if (ret != AVERROR_EOF && ret < 0)
  152. av_log(NULL, AV_LOG_WARNING, "Error while add the frame to buffer source(%s).\n",
  153. av_err2str(ret));
  154. }
  155. static void sub2video_update(InputFilterPriv *ifp, int64_t heartbeat_pts,
  156. const AVSubtitle *sub)
  157. {
  158. AVFrame *frame = ifp->sub2video.frame;
  159. int8_t *dst;
  160. int dst_linesize;
  161. int num_rects, i;
  162. int64_t pts, end_pts;
  163. if (sub) {
  164. pts = av_rescale_q(sub->pts + sub->start_display_time * 1000LL,
  165. AV_TIME_BASE_Q, ifp->time_base);
  166. end_pts = av_rescale_q(sub->pts + sub->end_display_time * 1000LL,
  167. AV_TIME_BASE_Q, ifp->time_base);
  168. num_rects = sub->num_rects;
  169. } else {
  170. /* If we are initializing the system, utilize current heartbeat
  171. PTS as the start time, and show until the following subpicture
  172. is received. Otherwise, utilize the previous subpicture's end time
  173. as the fall-back value. */
  174. pts = ifp->sub2video.initialize ?
  175. heartbeat_pts : ifp->sub2video.end_pts;
  176. end_pts = INT64_MAX;
  177. num_rects = 0;
  178. }
  179. if (sub2video_get_blank_frame(ifp) < 0) {
  180. av_log(NULL, AV_LOG_ERROR,
  181. "Impossible to get a blank canvas.\n");
  182. return;
  183. }
  184. dst = frame->data [0];
  185. dst_linesize = frame->linesize[0];
  186. for (i = 0; i < num_rects; i++)
  187. sub2video_copy_rect(dst, dst_linesize, frame->width, frame->height, sub->rects[i]);
  188. sub2video_push_ref(ifp, pts);
  189. ifp->sub2video.end_pts = end_pts;
  190. ifp->sub2video.initialize = 0;
  191. }
  192. // FIXME: YUV420P etc. are actually supported with full color range,
  193. // yet the latter information isn't available here.
  194. static const enum AVPixelFormat *get_compliance_normal_pix_fmts(const AVCodec *codec, const enum AVPixelFormat default_formats[])
  195. {
  196. static const enum AVPixelFormat mjpeg_formats[] =
  197. { AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P,
  198. AV_PIX_FMT_NONE };
  199. if (!strcmp(codec->name, "mjpeg")) {
  200. return mjpeg_formats;
  201. } else {
  202. return default_formats;
  203. }
  204. }
  205. static enum AVPixelFormat
  206. choose_pixel_fmt(const AVCodec *codec, enum AVPixelFormat target,
  207. int strict_std_compliance)
  208. {
  209. if (codec && codec->pix_fmts) {
  210. const enum AVPixelFormat *p = codec->pix_fmts;
  211. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(target);
  212. //FIXME: This should check for AV_PIX_FMT_FLAG_ALPHA after PAL8 pixel format without alpha is implemented
  213. int has_alpha = desc ? desc->nb_components % 2 == 0 : 0;
  214. enum AVPixelFormat best= AV_PIX_FMT_NONE;
  215. if (strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL) {
  216. p = get_compliance_normal_pix_fmts(codec, p);
  217. }
  218. for (; *p != AV_PIX_FMT_NONE; p++) {
  219. best = av_find_best_pix_fmt_of_2(best, *p, target, has_alpha, NULL);
  220. if (*p == target)
  221. break;
  222. }
  223. if (*p == AV_PIX_FMT_NONE) {
  224. if (target != AV_PIX_FMT_NONE)
  225. av_log(NULL, AV_LOG_WARNING,
  226. "Incompatible pixel format '%s' for codec '%s', auto-selecting format '%s'\n",
  227. av_get_pix_fmt_name(target),
  228. codec->name,
  229. av_get_pix_fmt_name(best));
  230. return best;
  231. }
  232. }
  233. return target;
  234. }
  235. /* May return NULL (no pixel format found), a static string or a string
  236. * backed by the bprint. Nothing has been written to the AVBPrint in case
  237. * NULL is returned. The AVBPrint provided should be clean. */
  238. static const char *choose_pix_fmts(OutputFilter *ofilter, AVBPrint *bprint)
  239. {
  240. OutputStream *ost = ofilter->ost;
  241. AVCodecContext *enc = ost->enc_ctx;
  242. const AVDictionaryEntry *strict_dict = av_dict_get(ost->encoder_opts, "strict", NULL, 0);
  243. if (strict_dict)
  244. // used by choose_pixel_fmt() and below
  245. av_opt_set(ost->enc_ctx, "strict", strict_dict->value, 0);
  246. if (ost->keep_pix_fmt) {
  247. avfilter_graph_set_auto_convert(ofilter->graph->graph,
  248. AVFILTER_AUTO_CONVERT_NONE);
  249. if (ost->enc_ctx->pix_fmt == AV_PIX_FMT_NONE)
  250. return NULL;
  251. return av_get_pix_fmt_name(ost->enc_ctx->pix_fmt);
  252. }
  253. if (ost->enc_ctx->pix_fmt != AV_PIX_FMT_NONE) {
  254. return av_get_pix_fmt_name(choose_pixel_fmt(enc->codec, enc->pix_fmt,
  255. ost->enc_ctx->strict_std_compliance));
  256. } else if (enc->codec->pix_fmts) {
  257. const enum AVPixelFormat *p;
  258. p = enc->codec->pix_fmts;
  259. if (ost->enc_ctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL) {
  260. p = get_compliance_normal_pix_fmts(enc->codec, p);
  261. }
  262. for (; *p != AV_PIX_FMT_NONE; p++) {
  263. const char *name = av_get_pix_fmt_name(*p);
  264. av_bprintf(bprint, "%s%c", name, p[1] == AV_PIX_FMT_NONE ? '\0' : '|');
  265. }
  266. if (!av_bprint_is_complete(bprint))
  267. report_and_exit(AVERROR(ENOMEM));
  268. return bprint->str;
  269. } else
  270. return NULL;
  271. }
  272. /* Define a function for appending a list of allowed formats
  273. * to an AVBPrint. If nonempty, the list will have a header. */
  274. #define DEF_CHOOSE_FORMAT(name, type, var, supported_list, none, printf_format, get_name) \
  275. static void choose_ ## name (OutputFilter *ofilter, AVBPrint *bprint) \
  276. { \
  277. if (ofilter->var == none && !ofilter->supported_list) \
  278. return; \
  279. av_bprintf(bprint, #name "="); \
  280. if (ofilter->var != none) { \
  281. av_bprintf(bprint, printf_format, get_name(ofilter->var)); \
  282. } else { \
  283. const type *p; \
  284. \
  285. for (p = ofilter->supported_list; *p != none; p++) { \
  286. av_bprintf(bprint, printf_format "|", get_name(*p)); \
  287. } \
  288. if (bprint->len > 0) \
  289. bprint->str[--bprint->len] = '\0'; \
  290. } \
  291. av_bprint_chars(bprint, ':', 1); \
  292. }
  293. //DEF_CHOOSE_FORMAT(pix_fmts, enum AVPixelFormat, format, formats, AV_PIX_FMT_NONE,
  294. // GET_PIX_FMT_NAME)
  295. DEF_CHOOSE_FORMAT(sample_fmts, enum AVSampleFormat, format, formats,
  296. AV_SAMPLE_FMT_NONE, "%s", av_get_sample_fmt_name)
  297. DEF_CHOOSE_FORMAT(sample_rates, int, sample_rate, sample_rates, 0,
  298. "%d", )
  299. static void choose_channel_layouts(OutputFilter *ofilter, AVBPrint *bprint)
  300. {
  301. if (av_channel_layout_check(&ofilter->ch_layout)) {
  302. av_bprintf(bprint, "channel_layouts=");
  303. av_channel_layout_describe_bprint(&ofilter->ch_layout, bprint);
  304. } else if (ofilter->ch_layouts) {
  305. const AVChannelLayout *p;
  306. av_bprintf(bprint, "channel_layouts=");
  307. for (p = ofilter->ch_layouts; p->nb_channels; p++) {
  308. av_channel_layout_describe_bprint(p, bprint);
  309. av_bprintf(bprint, "|");
  310. }
  311. if (bprint->len > 0)
  312. bprint->str[--bprint->len] = '\0';
  313. } else
  314. return;
  315. av_bprint_chars(bprint, ':', 1);
  316. }
  317. static int read_binary(const char *path, uint8_t **data, int *len)
  318. {
  319. AVIOContext *io = NULL;
  320. int64_t fsize;
  321. int ret;
  322. *data = NULL;
  323. *len = 0;
  324. ret = avio_open2(&io, path, AVIO_FLAG_READ, &int_cb, NULL);
  325. if (ret < 0) {
  326. av_log(NULL, AV_LOG_ERROR, "Cannot open file '%s': %s\n",
  327. path, av_err2str(ret));
  328. return ret;
  329. }
  330. fsize = avio_size(io);
  331. if (fsize < 0 || fsize > INT_MAX) {
  332. av_log(NULL, AV_LOG_ERROR, "Cannot obtain size of file %s\n", path);
  333. ret = AVERROR(EIO);
  334. goto fail;
  335. }
  336. *data = av_malloc(fsize);
  337. if (!*data) {
  338. ret = AVERROR(ENOMEM);
  339. goto fail;
  340. }
  341. ret = avio_read(io, *data, fsize);
  342. if (ret != fsize) {
  343. av_log(NULL, AV_LOG_ERROR, "Error reading file %s\n", path);
  344. ret = ret < 0 ? ret : AVERROR(EIO);
  345. goto fail;
  346. }
  347. *len = fsize;
  348. ret = 0;
  349. fail:
  350. avio_close(io);
  351. if (ret < 0) {
  352. av_freep(data);
  353. *len = 0;
  354. }
  355. return ret;
  356. }
  357. static int filter_opt_apply(AVFilterContext *f, const char *key, const char *val)
  358. {
  359. const AVOption *o = NULL;
  360. int ret;
  361. ret = av_opt_set(f, key, val, AV_OPT_SEARCH_CHILDREN);
  362. if (ret >= 0)
  363. return 0;
  364. if (ret == AVERROR_OPTION_NOT_FOUND && key[0] == '/')
  365. o = av_opt_find(f, key + 1, NULL, 0, AV_OPT_SEARCH_CHILDREN);
  366. if (!o)
  367. goto err_apply;
  368. // key is a valid option name prefixed with '/'
  369. // interpret value as a path from which to load the actual option value
  370. key++;
  371. if (o->type == AV_OPT_TYPE_BINARY) {
  372. uint8_t *data;
  373. int len;
  374. ret = read_binary(val, &data, &len);
  375. if (ret < 0)
  376. goto err_load;
  377. ret = av_opt_set_bin(f, key, data, len, AV_OPT_SEARCH_CHILDREN);
  378. av_freep(&data);
  379. } else {
  380. char *data = file_read(val);
  381. if (!data) {
  382. ret = AVERROR(EIO);
  383. goto err_load;
  384. }
  385. ret = av_opt_set(f, key, data, AV_OPT_SEARCH_CHILDREN);
  386. av_freep(&data);
  387. }
  388. if (ret < 0)
  389. goto err_apply;
  390. return 0;
  391. err_apply:
  392. av_log(NULL, AV_LOG_ERROR,
  393. "Error applying option '%s' to filter '%s': %s\n",
  394. key, f->filter->name, av_err2str(ret));
  395. return ret;
  396. err_load:
  397. av_log(NULL, AV_LOG_ERROR,
  398. "Error loading value for option '%s' from file '%s'\n",
  399. key, val);
  400. return ret;
  401. }
  402. static int graph_opts_apply(AVFilterGraphSegment *seg)
  403. {
  404. for (size_t i = 0; i < seg->nb_chains; i++) {
  405. AVFilterChain *ch = seg->chains[i];
  406. for (size_t j = 0; j < ch->nb_filters; j++) {
  407. AVFilterParams *p = ch->filters[j];
  408. const AVDictionaryEntry *e = NULL;
  409. av_assert0(p->filter);
  410. while ((e = av_dict_iterate(p->opts, e))) {
  411. int ret = filter_opt_apply(p->filter, e->key, e->value);
  412. if (ret < 0)
  413. return ret;
  414. }
  415. av_dict_free(&p->opts);
  416. }
  417. }
  418. return 0;
  419. }
  420. static int graph_parse(AVFilterGraph *graph, const char *desc,
  421. AVFilterInOut **inputs, AVFilterInOut **outputs,
  422. AVBufferRef *hw_device)
  423. {
  424. AVFilterGraphSegment *seg;
  425. int ret;
  426. *inputs = NULL;
  427. *outputs = NULL;
  428. ret = avfilter_graph_segment_parse(graph, desc, 0, &seg);
  429. if (ret < 0)
  430. return ret;
  431. ret = avfilter_graph_segment_create_filters(seg, 0);
  432. if (ret < 0)
  433. goto fail;
  434. if (hw_device) {
  435. for (int i = 0; i < graph->nb_filters; i++) {
  436. AVFilterContext *f = graph->filters[i];
  437. if (!(f->filter->flags & AVFILTER_FLAG_HWDEVICE))
  438. continue;
  439. f->hw_device_ctx = av_buffer_ref(hw_device);
  440. if (!f->hw_device_ctx) {
  441. ret = AVERROR(ENOMEM);
  442. goto fail;
  443. }
  444. }
  445. }
  446. ret = graph_opts_apply(seg);
  447. if (ret < 0)
  448. goto fail;
  449. ret = avfilter_graph_segment_apply(seg, 0, inputs, outputs);
  450. fail:
  451. avfilter_graph_segment_free(&seg);
  452. return ret;
  453. }
  454. // Filters can be configured only if the formats of all inputs are known.
  455. static int ifilter_has_all_input_formats(FilterGraph *fg)
  456. {
  457. int i;
  458. for (i = 0; i < fg->nb_inputs; i++) {
  459. InputFilterPriv *ifp = ifp_from_ifilter(fg->inputs[i]);
  460. if (ifp->format < 0)
  461. return 0;
  462. }
  463. return 1;
  464. }
  465. static char *describe_filter_link(FilterGraph *fg, AVFilterInOut *inout, int in)
  466. {
  467. AVFilterContext *ctx = inout->filter_ctx;
  468. AVFilterPad *pads = in ? ctx->input_pads : ctx->output_pads;
  469. int nb_pads = in ? ctx->nb_inputs : ctx->nb_outputs;
  470. char *res;
  471. if (nb_pads > 1)
  472. res = av_strdup(ctx->filter->name);
  473. else
  474. res = av_asprintf("%s:%s", ctx->filter->name,
  475. avfilter_pad_get_name(pads, inout->pad_idx));
  476. if (!res)
  477. report_and_exit(AVERROR(ENOMEM));
  478. return res;
  479. }
  480. static OutputFilter *ofilter_alloc(FilterGraph *fg)
  481. {
  482. OutputFilter *ofilter;
  483. ofilter = ALLOC_ARRAY_ELEM(fg->outputs, fg->nb_outputs);
  484. ofilter->graph = fg;
  485. ofilter->format = -1;
  486. ofilter->last_pts = AV_NOPTS_VALUE;
  487. return ofilter;
  488. }
  489. static int ifilter_bind_ist(InputFilter *ifilter, InputStream *ist)
  490. {
  491. InputFilterPriv *ifp = ifp_from_ifilter(ifilter);
  492. int ret;
  493. ifp->ist = ist;
  494. ifp->type_src = ist->st->codecpar->codec_type;
  495. ret = ist_filter_add(ist, ifilter, filtergraph_is_simple(ifilter->graph));
  496. if (ret < 0)
  497. return ret;
  498. if (ifp->type_src == AVMEDIA_TYPE_SUBTITLE) {
  499. ifp->sub2video.frame = av_frame_alloc();
  500. if (!ifp->sub2video.frame)
  501. return AVERROR(ENOMEM);
  502. }
  503. return 0;
  504. }
  505. static void set_channel_layout(OutputFilter *f, OutputStream *ost)
  506. {
  507. const AVCodec *c = ost->enc_ctx->codec;
  508. int i, err;
  509. if (ost->enc_ctx->ch_layout.order != AV_CHANNEL_ORDER_UNSPEC) {
  510. /* Pass the layout through for all orders but UNSPEC */
  511. err = av_channel_layout_copy(&f->ch_layout, &ost->enc_ctx->ch_layout);
  512. if (err < 0)
  513. report_and_exit(AVERROR(ENOMEM));
  514. return;
  515. }
  516. /* Requested layout is of order UNSPEC */
  517. if (!c->ch_layouts) {
  518. /* Use the default native layout for the requested amount of channels when the
  519. encoder doesn't have a list of supported layouts */
  520. av_channel_layout_default(&f->ch_layout, ost->enc_ctx->ch_layout.nb_channels);
  521. return;
  522. }
  523. /* Encoder has a list of supported layouts. Pick the first layout in it with the
  524. same amount of channels as the requested layout */
  525. for (i = 0; c->ch_layouts[i].nb_channels; i++) {
  526. if (c->ch_layouts[i].nb_channels == ost->enc_ctx->ch_layout.nb_channels)
  527. break;
  528. }
  529. if (c->ch_layouts[i].nb_channels) {
  530. /* Use it if one is found */
  531. err = av_channel_layout_copy(&f->ch_layout, &c->ch_layouts[i]);
  532. if (err < 0)
  533. report_and_exit(AVERROR(ENOMEM));
  534. return;
  535. }
  536. /* If no layout for the amount of channels requested was found, use the default
  537. native layout for it. */
  538. av_channel_layout_default(&f->ch_layout, ost->enc_ctx->ch_layout.nb_channels);
  539. }
  540. void ofilter_bind_ost(OutputFilter *ofilter, OutputStream *ost)
  541. {
  542. FilterGraph *fg = ofilter->graph;
  543. const AVCodec *c = ost->enc_ctx->codec;
  544. ofilter->ost = ost;
  545. av_freep(&ofilter->linklabel);
  546. switch (ost->enc_ctx->codec_type) {
  547. case AVMEDIA_TYPE_VIDEO:
  548. ofilter->width = ost->enc_ctx->width;
  549. ofilter->height = ost->enc_ctx->height;
  550. if (ost->enc_ctx->pix_fmt != AV_PIX_FMT_NONE) {
  551. ofilter->format = ost->enc_ctx->pix_fmt;
  552. } else {
  553. ofilter->formats = c->pix_fmts;
  554. }
  555. break;
  556. case AVMEDIA_TYPE_AUDIO:
  557. if (ost->enc_ctx->sample_fmt != AV_SAMPLE_FMT_NONE) {
  558. ofilter->format = ost->enc_ctx->sample_fmt;
  559. } else {
  560. ofilter->formats = c->sample_fmts;
  561. }
  562. if (ost->enc_ctx->sample_rate) {
  563. ofilter->sample_rate = ost->enc_ctx->sample_rate;
  564. } else {
  565. ofilter->sample_rates = c->supported_samplerates;
  566. }
  567. if (ost->enc_ctx->ch_layout.nb_channels) {
  568. set_channel_layout(ofilter, ost);
  569. } else if (c->ch_layouts) {
  570. ofilter->ch_layouts = c->ch_layouts;
  571. }
  572. break;
  573. }
  574. // if we have all input parameters and all outputs are bound,
  575. // the graph can now be configured
  576. if (ifilter_has_all_input_formats(fg)) {
  577. int ret;
  578. for (int i = 0; i < fg->nb_outputs; i++)
  579. if (!fg->outputs[i]->ost)
  580. return;
  581. ret = configure_filtergraph(fg);
  582. if (ret < 0) {
  583. av_log(NULL, AV_LOG_ERROR, "Error configuring filter graph: %s\n",
  584. av_err2str(ret));
  585. exit_program(1);
  586. }
  587. }
  588. }
  589. static InputFilter *ifilter_alloc(FilterGraph *fg)
  590. {
  591. InputFilterPriv *ifp = allocate_array_elem(&fg->inputs, sizeof(*ifp),
  592. &fg->nb_inputs);
  593. InputFilter *ifilter = &ifp->ifilter;
  594. ifilter->graph = fg;
  595. ifp->frame = av_frame_alloc();
  596. if (!ifp->frame)
  597. report_and_exit(AVERROR(ENOMEM));
  598. ifp->format = -1;
  599. ifp->fallback.format = -1;
  600. ifp->frame_queue = av_fifo_alloc2(8, sizeof(AVFrame*), AV_FIFO_FLAG_AUTO_GROW);
  601. if (!ifp->frame_queue)
  602. report_and_exit(AVERROR(ENOMEM));
  603. return ifilter;
  604. }
  605. void fg_free(FilterGraph **pfg)
  606. {
  607. FilterGraph *fg = *pfg;
  608. FilterGraphPriv *fgp;
  609. if (!fg)
  610. return;
  611. fgp = fgp_from_fg(fg);
  612. avfilter_graph_free(&fg->graph);
  613. for (int j = 0; j < fg->nb_inputs; j++) {
  614. InputFilter *ifilter = fg->inputs[j];
  615. InputFilterPriv *ifp = ifp_from_ifilter(ifilter);
  616. if (ifp->frame_queue) {
  617. AVFrame *frame;
  618. while (av_fifo_read(ifp->frame_queue, &frame, 1) >= 0)
  619. av_frame_free(&frame);
  620. av_fifo_freep2(&ifp->frame_queue);
  621. }
  622. if (ifp->sub2video.queue) {
  623. AVSubtitle sub;
  624. while (av_fifo_read(ifp->sub2video.queue, &sub, 1) >= 0)
  625. avsubtitle_free(&sub);
  626. av_fifo_freep2(&ifp->sub2video.queue);
  627. }
  628. av_frame_free(&ifp->sub2video.frame);
  629. av_channel_layout_uninit(&ifp->fallback.ch_layout);
  630. av_frame_free(&ifp->frame);
  631. av_buffer_unref(&ifp->hw_frames_ctx);
  632. av_freep(&ifp->linklabel);
  633. av_freep(&ifilter->name);
  634. av_freep(&fg->inputs[j]);
  635. }
  636. av_freep(&fg->inputs);
  637. for (int j = 0; j < fg->nb_outputs; j++) {
  638. OutputFilter *ofilter = fg->outputs[j];
  639. av_freep(&ofilter->linklabel);
  640. av_freep(&ofilter->name);
  641. av_channel_layout_uninit(&ofilter->ch_layout);
  642. av_freep(&fg->outputs[j]);
  643. }
  644. av_freep(&fg->outputs);
  645. av_freep(&fgp->graph_desc);
  646. av_frame_free(&fgp->frame);
  647. av_freep(pfg);
  648. }
  649. FilterGraph *fg_create(char *graph_desc)
  650. {
  651. FilterGraphPriv *fgp = allocate_array_elem(&filtergraphs, sizeof(*fgp), &nb_filtergraphs);
  652. FilterGraph *fg = &fgp->fg;
  653. AVFilterInOut *inputs, *outputs;
  654. AVFilterGraph *graph;
  655. int ret = 0;
  656. fg->index = nb_filtergraphs - 1;
  657. fgp->graph_desc = graph_desc;
  658. fgp->frame = av_frame_alloc();
  659. if (!fgp->frame)
  660. report_and_exit(AVERROR(ENOMEM));
  661. /* this graph is only used for determining the kinds of inputs
  662. * and outputs we have, and is discarded on exit from this function */
  663. graph = avfilter_graph_alloc();
  664. if (!graph)
  665. report_and_exit(AVERROR(ENOMEM));
  666. graph->nb_threads = 1;
  667. ret = graph_parse(graph, fgp->graph_desc, &inputs, &outputs, NULL);
  668. if (ret < 0)
  669. goto fail;
  670. for (AVFilterInOut *cur = inputs; cur; cur = cur->next) {
  671. InputFilter *const ifilter = ifilter_alloc(fg);
  672. InputFilterPriv *ifp = ifp_from_ifilter(ifilter);
  673. ifp->linklabel = cur->name;
  674. cur->name = NULL;
  675. ifp->type = avfilter_pad_get_type(cur->filter_ctx->input_pads,
  676. cur->pad_idx);
  677. ifilter->name = describe_filter_link(fg, cur, 1);
  678. }
  679. for (AVFilterInOut *cur = outputs; cur; cur = cur->next) {
  680. OutputFilter *const ofilter = ofilter_alloc(fg);
  681. ofilter->linklabel = cur->name;
  682. cur->name = NULL;
  683. ofilter->type = avfilter_pad_get_type(cur->filter_ctx->output_pads,
  684. cur->pad_idx);
  685. ofilter->name = describe_filter_link(fg, cur, 0);
  686. }
  687. fail:
  688. avfilter_inout_free(&inputs);
  689. avfilter_inout_free(&outputs);
  690. avfilter_graph_free(&graph);
  691. if (ret < 0)
  692. report_and_exit(ret);
  693. return fg;
  694. }
  695. int init_simple_filtergraph(InputStream *ist, OutputStream *ost,
  696. char *graph_desc)
  697. {
  698. FilterGraph *fg;
  699. FilterGraphPriv *fgp;
  700. int ret;
  701. fg = fg_create(graph_desc);
  702. if (!fg)
  703. report_and_exit(AVERROR(ENOMEM));
  704. fgp = fgp_from_fg(fg);
  705. fgp->is_simple = 1;
  706. if (fg->nb_inputs != 1 || fg->nb_outputs != 1) {
  707. av_log(NULL, AV_LOG_ERROR, "Simple filtergraph '%s' was expected "
  708. "to have exactly 1 input and 1 output. "
  709. "However, it had %d input(s) and %d output(s). Please adjust, "
  710. "or use a complex filtergraph (-filter_complex) instead.\n",
  711. graph_desc, fg->nb_inputs, fg->nb_outputs);
  712. return AVERROR(EINVAL);
  713. }
  714. ost->filter = fg->outputs[0];
  715. ret = ifilter_bind_ist(fg->inputs[0], ist);
  716. if (ret < 0)
  717. return ret;
  718. ofilter_bind_ost(fg->outputs[0], ost);
  719. return 0;
  720. }
  721. static void init_input_filter(FilterGraph *fg, InputFilter *ifilter)
  722. {
  723. FilterGraphPriv *fgp = fgp_from_fg(fg);
  724. InputFilterPriv *ifp = ifp_from_ifilter(ifilter);
  725. InputStream *ist = NULL;
  726. enum AVMediaType type = ifp->type;
  727. int i, ret;
  728. // TODO: support other filter types
  729. if (type != AVMEDIA_TYPE_VIDEO && type != AVMEDIA_TYPE_AUDIO) {
  730. av_log(NULL, AV_LOG_FATAL, "Only video and audio filters supported "
  731. "currently.\n");
  732. exit_program(1);
  733. }
  734. if (ifp->linklabel) {
  735. AVFormatContext *s;
  736. AVStream *st = NULL;
  737. char *p;
  738. int file_idx = strtol(ifp->linklabel, &p, 0);
  739. if (file_idx < 0 || file_idx >= nb_input_files) {
  740. av_log(NULL, AV_LOG_FATAL, "Invalid file index %d in filtergraph description %s.\n",
  741. file_idx, fgp->graph_desc);
  742. exit_program(1);
  743. }
  744. s = input_files[file_idx]->ctx;
  745. for (i = 0; i < s->nb_streams; i++) {
  746. enum AVMediaType stream_type = s->streams[i]->codecpar->codec_type;
  747. if (stream_type != type &&
  748. !(stream_type == AVMEDIA_TYPE_SUBTITLE &&
  749. type == AVMEDIA_TYPE_VIDEO /* sub2video hack */))
  750. continue;
  751. if (check_stream_specifier(s, s->streams[i], *p == ':' ? p + 1 : p) == 1) {
  752. st = s->streams[i];
  753. break;
  754. }
  755. }
  756. if (!st) {
  757. av_log(NULL, AV_LOG_FATAL, "Stream specifier '%s' in filtergraph description %s "
  758. "matches no streams.\n", p, fgp->graph_desc);
  759. exit_program(1);
  760. }
  761. ist = input_files[file_idx]->streams[st->index];
  762. } else {
  763. ist = ist_find_unused(type);
  764. if (!ist) {
  765. av_log(NULL, AV_LOG_FATAL, "Cannot find a matching stream for "
  766. "unlabeled input pad %s\n", ifilter->name);
  767. exit_program(1);
  768. }
  769. }
  770. av_assert0(ist);
  771. ret = ifilter_bind_ist(ifilter, ist);
  772. if (ret < 0) {
  773. av_log(NULL, AV_LOG_ERROR,
  774. "Error binding an input stream to complex filtergraph input %s.\n",
  775. ifilter->name);
  776. exit_program(1);
  777. }
  778. }
  779. int init_complex_filtergraph(FilterGraph *fg)
  780. {
  781. // bind filtergraph inputs to input streams
  782. for (int i = 0; i < fg->nb_inputs; i++)
  783. init_input_filter(fg, fg->inputs[i]);
  784. return 0;
  785. }
  786. static int insert_trim(int64_t start_time, int64_t duration,
  787. AVFilterContext **last_filter, int *pad_idx,
  788. const char *filter_name)
  789. {
  790. AVFilterGraph *graph = (*last_filter)->graph;
  791. AVFilterContext *ctx;
  792. const AVFilter *trim;
  793. enum AVMediaType type = avfilter_pad_get_type((*last_filter)->output_pads, *pad_idx);
  794. const char *name = (type == AVMEDIA_TYPE_VIDEO) ? "trim" : "atrim";
  795. int ret = 0;
  796. if (duration == INT64_MAX && start_time == AV_NOPTS_VALUE)
  797. return 0;
  798. trim = avfilter_get_by_name(name);
  799. if (!trim) {
  800. av_log(NULL, AV_LOG_ERROR, "%s filter not present, cannot limit "
  801. "recording time.\n", name);
  802. return AVERROR_FILTER_NOT_FOUND;
  803. }
  804. ctx = avfilter_graph_alloc_filter(graph, trim, filter_name);
  805. if (!ctx)
  806. return AVERROR(ENOMEM);
  807. if (duration != INT64_MAX) {
  808. ret = av_opt_set_int(ctx, "durationi", duration,
  809. AV_OPT_SEARCH_CHILDREN);
  810. }
  811. if (ret >= 0 && start_time != AV_NOPTS_VALUE) {
  812. ret = av_opt_set_int(ctx, "starti", start_time,
  813. AV_OPT_SEARCH_CHILDREN);
  814. }
  815. if (ret < 0) {
  816. av_log(ctx, AV_LOG_ERROR, "Error configuring the %s filter", name);
  817. return ret;
  818. }
  819. ret = avfilter_init_str(ctx, NULL);
  820. if (ret < 0)
  821. return ret;
  822. ret = avfilter_link(*last_filter, *pad_idx, ctx, 0);
  823. if (ret < 0)
  824. return ret;
  825. *last_filter = ctx;
  826. *pad_idx = 0;
  827. return 0;
  828. }
  829. static int insert_filter(AVFilterContext **last_filter, int *pad_idx,
  830. const char *filter_name, const char *args)
  831. {
  832. AVFilterGraph *graph = (*last_filter)->graph;
  833. AVFilterContext *ctx;
  834. int ret;
  835. ret = avfilter_graph_create_filter(&ctx,
  836. avfilter_get_by_name(filter_name),
  837. filter_name, args, NULL, graph);
  838. if (ret < 0)
  839. return ret;
  840. ret = avfilter_link(*last_filter, *pad_idx, ctx, 0);
  841. if (ret < 0)
  842. return ret;
  843. *last_filter = ctx;
  844. *pad_idx = 0;
  845. return 0;
  846. }
  847. static int configure_output_video_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
  848. {
  849. OutputStream *ost = ofilter->ost;
  850. OutputFile *of = output_files[ost->file_index];
  851. AVFilterContext *last_filter = out->filter_ctx;
  852. AVBPrint bprint;
  853. int pad_idx = out->pad_idx;
  854. int ret;
  855. const char *pix_fmts;
  856. char name[255];
  857. snprintf(name, sizeof(name), "out_%d_%d", ost->file_index, ost->index);
  858. ret = avfilter_graph_create_filter(&ofilter->filter,
  859. avfilter_get_by_name("buffersink"),
  860. name, NULL, NULL, fg->graph);
  861. if (ret < 0)
  862. return ret;
  863. if ((ofilter->width || ofilter->height) && ofilter->ost->autoscale) {
  864. char args[255];
  865. AVFilterContext *filter;
  866. const AVDictionaryEntry *e = NULL;
  867. snprintf(args, sizeof(args), "%d:%d",
  868. ofilter->width, ofilter->height);
  869. while ((e = av_dict_iterate(ost->sws_dict, e))) {
  870. av_strlcatf(args, sizeof(args), ":%s=%s", e->key, e->value);
  871. }
  872. snprintf(name, sizeof(name), "scaler_out_%d_%d",
  873. ost->file_index, ost->index);
  874. if ((ret = avfilter_graph_create_filter(&filter, avfilter_get_by_name("scale"),
  875. name, args, NULL, fg->graph)) < 0)
  876. return ret;
  877. if ((ret = avfilter_link(last_filter, pad_idx, filter, 0)) < 0)
  878. return ret;
  879. last_filter = filter;
  880. pad_idx = 0;
  881. }
  882. av_bprint_init(&bprint, 0, AV_BPRINT_SIZE_UNLIMITED);
  883. if ((pix_fmts = choose_pix_fmts(ofilter, &bprint))) {
  884. AVFilterContext *filter;
  885. ret = avfilter_graph_create_filter(&filter,
  886. avfilter_get_by_name("format"),
  887. "format", pix_fmts, NULL, fg->graph);
  888. av_bprint_finalize(&bprint, NULL);
  889. if (ret < 0)
  890. return ret;
  891. if ((ret = avfilter_link(last_filter, pad_idx, filter, 0)) < 0)
  892. return ret;
  893. last_filter = filter;
  894. pad_idx = 0;
  895. }
  896. snprintf(name, sizeof(name), "trim_out_%d_%d",
  897. ost->file_index, ost->index);
  898. ret = insert_trim(of->start_time, of->recording_time,
  899. &last_filter, &pad_idx, name);
  900. if (ret < 0)
  901. return ret;
  902. if ((ret = avfilter_link(last_filter, pad_idx, ofilter->filter, 0)) < 0)
  903. return ret;
  904. return 0;
  905. }
  906. static int configure_output_audio_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
  907. {
  908. OutputStream *ost = ofilter->ost;
  909. OutputFile *of = output_files[ost->file_index];
  910. AVFilterContext *last_filter = out->filter_ctx;
  911. int pad_idx = out->pad_idx;
  912. AVBPrint args;
  913. char name[255];
  914. int ret;
  915. snprintf(name, sizeof(name), "out_%d_%d", ost->file_index, ost->index);
  916. ret = avfilter_graph_create_filter(&ofilter->filter,
  917. avfilter_get_by_name("abuffersink"),
  918. name, NULL, NULL, fg->graph);
  919. if (ret < 0)
  920. return ret;
  921. if ((ret = av_opt_set_int(ofilter->filter, "all_channel_counts", 1, AV_OPT_SEARCH_CHILDREN)) < 0)
  922. return ret;
  923. #define AUTO_INSERT_FILTER(opt_name, filter_name, arg) do { \
  924. AVFilterContext *filt_ctx; \
  925. \
  926. av_log(NULL, AV_LOG_INFO, opt_name " is forwarded to lavfi " \
  927. "similarly to -af " filter_name "=%s.\n", arg); \
  928. \
  929. ret = avfilter_graph_create_filter(&filt_ctx, \
  930. avfilter_get_by_name(filter_name), \
  931. filter_name, arg, NULL, fg->graph); \
  932. if (ret < 0) \
  933. goto fail; \
  934. \
  935. ret = avfilter_link(last_filter, pad_idx, filt_ctx, 0); \
  936. if (ret < 0) \
  937. goto fail; \
  938. \
  939. last_filter = filt_ctx; \
  940. pad_idx = 0; \
  941. } while (0)
  942. av_bprint_init(&args, 0, AV_BPRINT_SIZE_UNLIMITED);
  943. #if FFMPEG_OPT_MAP_CHANNEL
  944. if (ost->audio_channels_mapped) {
  945. AVChannelLayout mapped_layout = { 0 };
  946. int i;
  947. av_channel_layout_default(&mapped_layout, ost->audio_channels_mapped);
  948. av_channel_layout_describe_bprint(&mapped_layout, &args);
  949. for (i = 0; i < ost->audio_channels_mapped; i++)
  950. if (ost->audio_channels_map[i] != -1)
  951. av_bprintf(&args, "|c%d=c%d", i, ost->audio_channels_map[i]);
  952. AUTO_INSERT_FILTER("-map_channel", "pan", args.str);
  953. av_bprint_clear(&args);
  954. }
  955. #endif
  956. choose_sample_fmts(ofilter, &args);
  957. choose_sample_rates(ofilter, &args);
  958. choose_channel_layouts(ofilter, &args);
  959. if (!av_bprint_is_complete(&args)) {
  960. ret = AVERROR(ENOMEM);
  961. goto fail;
  962. }
  963. if (args.len) {
  964. AVFilterContext *format;
  965. snprintf(name, sizeof(name), "format_out_%d_%d",
  966. ost->file_index, ost->index);
  967. ret = avfilter_graph_create_filter(&format,
  968. avfilter_get_by_name("aformat"),
  969. name, args.str, NULL, fg->graph);
  970. if (ret < 0)
  971. goto fail;
  972. ret = avfilter_link(last_filter, pad_idx, format, 0);
  973. if (ret < 0)
  974. goto fail;
  975. last_filter = format;
  976. pad_idx = 0;
  977. }
  978. if (ost->apad && of->shortest) {
  979. int i;
  980. for (i = 0; i < of->nb_streams; i++)
  981. if (of->streams[i]->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
  982. break;
  983. if (i < of->nb_streams) {
  984. AUTO_INSERT_FILTER("-apad", "apad", ost->apad);
  985. }
  986. }
  987. snprintf(name, sizeof(name), "trim for output stream %d:%d",
  988. ost->file_index, ost->index);
  989. ret = insert_trim(of->start_time, of->recording_time,
  990. &last_filter, &pad_idx, name);
  991. if (ret < 0)
  992. goto fail;
  993. if ((ret = avfilter_link(last_filter, pad_idx, ofilter->filter, 0)) < 0)
  994. goto fail;
  995. fail:
  996. av_bprint_finalize(&args, NULL);
  997. return ret;
  998. }
  999. static int configure_output_filter(FilterGraph *fg, OutputFilter *ofilter,
  1000. AVFilterInOut *out)
  1001. {
  1002. if (!ofilter->ost) {
  1003. av_log(NULL, AV_LOG_FATAL, "Filter %s has an unconnected output\n", ofilter->name);
  1004. exit_program(1);
  1005. }
  1006. switch (avfilter_pad_get_type(out->filter_ctx->output_pads, out->pad_idx)) {
  1007. case AVMEDIA_TYPE_VIDEO: return configure_output_video_filter(fg, ofilter, out);
  1008. case AVMEDIA_TYPE_AUDIO: return configure_output_audio_filter(fg, ofilter, out);
  1009. default: av_assert0(0); return 0;
  1010. }
  1011. }
  1012. void check_filter_outputs(void)
  1013. {
  1014. int i;
  1015. for (i = 0; i < nb_filtergraphs; i++) {
  1016. int n;
  1017. for (n = 0; n < filtergraphs[i]->nb_outputs; n++) {
  1018. OutputFilter *output = filtergraphs[i]->outputs[n];
  1019. if (!output->ost) {
  1020. av_log(NULL, AV_LOG_FATAL, "Filter %s has an unconnected output\n", output->name);
  1021. exit_program(1);
  1022. }
  1023. }
  1024. }
  1025. }
  1026. static void sub2video_prepare(InputFilterPriv *ifp)
  1027. {
  1028. ifp->sub2video.last_pts = INT64_MIN;
  1029. ifp->sub2video.end_pts = INT64_MIN;
  1030. /* sub2video structure has been (re-)initialized.
  1031. Mark it as such so that the system will be
  1032. initialized with the first received heartbeat. */
  1033. ifp->sub2video.initialize = 1;
  1034. }
  1035. static int configure_input_video_filter(FilterGraph *fg, InputFilter *ifilter,
  1036. AVFilterInOut *in)
  1037. {
  1038. InputFilterPriv *ifp = ifp_from_ifilter(ifilter);
  1039. AVFilterContext *last_filter;
  1040. const AVFilter *buffer_filt = avfilter_get_by_name("buffer");
  1041. const AVPixFmtDescriptor *desc;
  1042. InputStream *ist = ifp->ist;
  1043. InputFile *f = input_files[ist->file_index];
  1044. AVRational fr = ist->framerate;
  1045. AVRational sar;
  1046. AVBPrint args;
  1047. char name[255];
  1048. int ret, pad_idx = 0;
  1049. int64_t tsoffset = 0;
  1050. AVBufferSrcParameters *par = av_buffersrc_parameters_alloc();
  1051. if (!par)
  1052. return AVERROR(ENOMEM);
  1053. memset(par, 0, sizeof(*par));
  1054. par->format = AV_PIX_FMT_NONE;
  1055. if (ist->dec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) {
  1056. av_log(NULL, AV_LOG_ERROR, "Cannot connect video filter to audio input\n");
  1057. ret = AVERROR(EINVAL);
  1058. goto fail;
  1059. }
  1060. if (!fr.num)
  1061. fr = ist->framerate_guessed;
  1062. if (ifp->type_src == AVMEDIA_TYPE_SUBTITLE)
  1063. sub2video_prepare(ifp);
  1064. ifp->time_base = ist->framerate.num ? av_inv_q(ist->framerate) :
  1065. ist->st->time_base;
  1066. sar = ifp->sample_aspect_ratio;
  1067. if(!sar.den)
  1068. sar = (AVRational){0,1};
  1069. av_bprint_init(&args, 0, AV_BPRINT_SIZE_AUTOMATIC);
  1070. av_bprintf(&args,
  1071. "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:"
  1072. "pixel_aspect=%d/%d",
  1073. ifp->width, ifp->height, ifp->format,
  1074. ifp->time_base.num, ifp->time_base.den, sar.num, sar.den);
  1075. if (fr.num && fr.den)
  1076. av_bprintf(&args, ":frame_rate=%d/%d", fr.num, fr.den);
  1077. snprintf(name, sizeof(name), "graph %d input from stream %d:%d", fg->index,
  1078. ist->file_index, ist->index);
  1079. if ((ret = avfilter_graph_create_filter(&ifp->filter, buffer_filt, name,
  1080. args.str, NULL, fg->graph)) < 0)
  1081. goto fail;
  1082. par->hw_frames_ctx = ifp->hw_frames_ctx;
  1083. ret = av_buffersrc_parameters_set(ifp->filter, par);
  1084. if (ret < 0)
  1085. goto fail;
  1086. av_freep(&par);
  1087. last_filter = ifp->filter;
  1088. desc = av_pix_fmt_desc_get(ifp->format);
  1089. av_assert0(desc);
  1090. // TODO: insert hwaccel enabled filters like transpose_vaapi into the graph
  1091. if (ist->autorotate && !(desc->flags & AV_PIX_FMT_FLAG_HWACCEL)) {
  1092. int32_t *displaymatrix = ifp->displaymatrix;
  1093. double theta;
  1094. if (!ifp->displaymatrix_present)
  1095. displaymatrix = (int32_t *)av_stream_get_side_data(ist->st, AV_PKT_DATA_DISPLAYMATRIX, NULL);
  1096. theta = get_rotation(displaymatrix);
  1097. if (fabs(theta - 90) < 1.0) {
  1098. ret = insert_filter(&last_filter, &pad_idx, "transpose",
  1099. displaymatrix[3] > 0 ? "cclock_flip" : "clock");
  1100. } else if (fabs(theta - 180) < 1.0) {
  1101. if (displaymatrix[0] < 0) {
  1102. ret = insert_filter(&last_filter, &pad_idx, "hflip", NULL);
  1103. if (ret < 0)
  1104. return ret;
  1105. }
  1106. if (displaymatrix[4] < 0) {
  1107. ret = insert_filter(&last_filter, &pad_idx, "vflip", NULL);
  1108. }
  1109. } else if (fabs(theta - 270) < 1.0) {
  1110. ret = insert_filter(&last_filter, &pad_idx, "transpose",
  1111. displaymatrix[3] < 0 ? "clock_flip" : "cclock");
  1112. } else if (fabs(theta) > 1.0) {
  1113. char rotate_buf[64];
  1114. snprintf(rotate_buf, sizeof(rotate_buf), "%f*PI/180", theta);
  1115. ret = insert_filter(&last_filter, &pad_idx, "rotate", rotate_buf);
  1116. } else if (fabs(theta) < 1.0) {
  1117. if (displaymatrix && displaymatrix[4] < 0) {
  1118. ret = insert_filter(&last_filter, &pad_idx, "vflip", NULL);
  1119. }
  1120. }
  1121. if (ret < 0)
  1122. return ret;
  1123. }
  1124. snprintf(name, sizeof(name), "trim_in_%d_%d",
  1125. ist->file_index, ist->index);
  1126. if (copy_ts) {
  1127. tsoffset = f->start_time == AV_NOPTS_VALUE ? 0 : f->start_time;
  1128. if (!start_at_zero && f->ctx->start_time != AV_NOPTS_VALUE)
  1129. tsoffset += f->ctx->start_time;
  1130. }
  1131. ret = insert_trim(((f->start_time == AV_NOPTS_VALUE) || !f->accurate_seek) ?
  1132. AV_NOPTS_VALUE : tsoffset, f->recording_time,
  1133. &last_filter, &pad_idx, name);
  1134. if (ret < 0)
  1135. return ret;
  1136. if ((ret = avfilter_link(last_filter, 0, in->filter_ctx, in->pad_idx)) < 0)
  1137. return ret;
  1138. return 0;
  1139. fail:
  1140. av_freep(&par);
  1141. return ret;
  1142. }
  1143. static int configure_input_audio_filter(FilterGraph *fg, InputFilter *ifilter,
  1144. AVFilterInOut *in)
  1145. {
  1146. InputFilterPriv *ifp = ifp_from_ifilter(ifilter);
  1147. AVFilterContext *last_filter;
  1148. const AVFilter *abuffer_filt = avfilter_get_by_name("abuffer");
  1149. InputStream *ist = ifp->ist;
  1150. InputFile *f = input_files[ist->file_index];
  1151. AVBPrint args;
  1152. char name[255];
  1153. int ret, pad_idx = 0;
  1154. int64_t tsoffset = 0;
  1155. if (ist->dec_ctx->codec_type != AVMEDIA_TYPE_AUDIO) {
  1156. av_log(NULL, AV_LOG_ERROR, "Cannot connect audio filter to non audio input\n");
  1157. return AVERROR(EINVAL);
  1158. }
  1159. ifp->time_base = (AVRational){ 1, ifp->sample_rate };
  1160. av_bprint_init(&args, 0, AV_BPRINT_SIZE_AUTOMATIC);
  1161. av_bprintf(&args, "time_base=%d/%d:sample_rate=%d:sample_fmt=%s",
  1162. ifp->time_base.num, ifp->time_base.den,
  1163. ifp->sample_rate,
  1164. av_get_sample_fmt_name(ifp->format));
  1165. if (av_channel_layout_check(&ifp->ch_layout) &&
  1166. ifp->ch_layout.order != AV_CHANNEL_ORDER_UNSPEC) {
  1167. av_bprintf(&args, ":channel_layout=");
  1168. av_channel_layout_describe_bprint(&ifp->ch_layout, &args);
  1169. } else
  1170. av_bprintf(&args, ":channels=%d", ifp->ch_layout.nb_channels);
  1171. snprintf(name, sizeof(name), "graph_%d_in_%d_%d", fg->index,
  1172. ist->file_index, ist->index);
  1173. if ((ret = avfilter_graph_create_filter(&ifp->filter, abuffer_filt,
  1174. name, args.str, NULL,
  1175. fg->graph)) < 0)
  1176. return ret;
  1177. last_filter = ifp->filter;
  1178. snprintf(name, sizeof(name), "trim for input stream %d:%d",
  1179. ist->file_index, ist->index);
  1180. if (copy_ts) {
  1181. tsoffset = f->start_time == AV_NOPTS_VALUE ? 0 : f->start_time;
  1182. if (!start_at_zero && f->ctx->start_time != AV_NOPTS_VALUE)
  1183. tsoffset += f->ctx->start_time;
  1184. }
  1185. ret = insert_trim(((f->start_time == AV_NOPTS_VALUE) || !f->accurate_seek) ?
  1186. AV_NOPTS_VALUE : tsoffset, f->recording_time,
  1187. &last_filter, &pad_idx, name);
  1188. if (ret < 0)
  1189. return ret;
  1190. if ((ret = avfilter_link(last_filter, 0, in->filter_ctx, in->pad_idx)) < 0)
  1191. return ret;
  1192. return 0;
  1193. }
  1194. static int configure_input_filter(FilterGraph *fg, InputFilter *ifilter,
  1195. AVFilterInOut *in)
  1196. {
  1197. switch (ifp_from_ifilter(ifilter)->type) {
  1198. case AVMEDIA_TYPE_VIDEO: return configure_input_video_filter(fg, ifilter, in);
  1199. case AVMEDIA_TYPE_AUDIO: return configure_input_audio_filter(fg, ifilter, in);
  1200. default: av_assert0(0); return 0;
  1201. }
  1202. }
  1203. static void cleanup_filtergraph(FilterGraph *fg)
  1204. {
  1205. int i;
  1206. for (i = 0; i < fg->nb_outputs; i++)
  1207. fg->outputs[i]->filter = (AVFilterContext *)NULL;
  1208. for (i = 0; i < fg->nb_inputs; i++)
  1209. ifp_from_ifilter(fg->inputs[i])->filter = NULL;
  1210. avfilter_graph_free(&fg->graph);
  1211. }
  1212. static int filter_is_buffersrc(const AVFilterContext *f)
  1213. {
  1214. return f->nb_inputs == 0 &&
  1215. (!strcmp(f->filter->name, "buffer") ||
  1216. !strcmp(f->filter->name, "abuffer"));
  1217. }
  1218. static int graph_is_meta(AVFilterGraph *graph)
  1219. {
  1220. for (unsigned i = 0; i < graph->nb_filters; i++) {
  1221. const AVFilterContext *f = graph->filters[i];
  1222. /* in addition to filters flagged as meta, also
  1223. * disregard sinks and buffersources (but not other sources,
  1224. * since they introduce data we are not aware of)
  1225. */
  1226. if (!((f->filter->flags & AVFILTER_FLAG_METADATA_ONLY) ||
  1227. f->nb_outputs == 0 ||
  1228. filter_is_buffersrc(f)))
  1229. return 0;
  1230. }
  1231. return 1;
  1232. }
  1233. int configure_filtergraph(FilterGraph *fg)
  1234. {
  1235. FilterGraphPriv *fgp = fgp_from_fg(fg);
  1236. AVBufferRef *hw_device;
  1237. AVFilterInOut *inputs, *outputs, *cur;
  1238. int ret, i, simple = filtergraph_is_simple(fg);
  1239. const char *graph_desc = fgp->graph_desc;
  1240. cleanup_filtergraph(fg);
  1241. if (!(fg->graph = avfilter_graph_alloc()))
  1242. return AVERROR(ENOMEM);
  1243. if (simple) {
  1244. OutputStream *ost = fg->outputs[0]->ost;
  1245. if (filter_nbthreads) {
  1246. ret = av_opt_set(fg->graph, "threads", filter_nbthreads, 0);
  1247. if (ret < 0)
  1248. goto fail;
  1249. } else {
  1250. const AVDictionaryEntry *e = NULL;
  1251. e = av_dict_get(ost->encoder_opts, "threads", NULL, 0);
  1252. if (e)
  1253. av_opt_set(fg->graph, "threads", e->value, 0);
  1254. }
  1255. if (av_dict_count(ost->sws_dict)) {
  1256. ret = av_dict_get_string(ost->sws_dict,
  1257. &fg->graph->scale_sws_opts,
  1258. '=', ':');
  1259. if (ret < 0)
  1260. goto fail;
  1261. }
  1262. if (av_dict_count(ost->swr_opts)) {
  1263. char *args;
  1264. ret = av_dict_get_string(ost->swr_opts, &args, '=', ':');
  1265. if (ret < 0)
  1266. goto fail;
  1267. av_opt_set(fg->graph, "aresample_swr_opts", args, 0);
  1268. av_free(args);
  1269. }
  1270. } else {
  1271. fg->graph->nb_threads = filter_complex_nbthreads;
  1272. }
  1273. hw_device = hw_device_for_filter();
  1274. if ((ret = graph_parse(fg->graph, graph_desc, &inputs, &outputs, hw_device)) < 0)
  1275. goto fail;
  1276. for (cur = inputs, i = 0; cur; cur = cur->next, i++)
  1277. if ((ret = configure_input_filter(fg, fg->inputs[i], cur)) < 0) {
  1278. avfilter_inout_free(&inputs);
  1279. avfilter_inout_free(&outputs);
  1280. goto fail;
  1281. }
  1282. avfilter_inout_free(&inputs);
  1283. for (cur = outputs, i = 0; cur; cur = cur->next, i++)
  1284. configure_output_filter(fg, fg->outputs[i], cur);
  1285. avfilter_inout_free(&outputs);
  1286. if (!auto_conversion_filters)
  1287. avfilter_graph_set_auto_convert(fg->graph, AVFILTER_AUTO_CONVERT_NONE);
  1288. if ((ret = avfilter_graph_config(fg->graph, NULL)) < 0)
  1289. goto fail;
  1290. fg->is_meta = graph_is_meta(fg->graph);
  1291. /* limit the lists of allowed formats to the ones selected, to
  1292. * make sure they stay the same if the filtergraph is reconfigured later */
  1293. for (i = 0; i < fg->nb_outputs; i++) {
  1294. OutputFilter *ofilter = fg->outputs[i];
  1295. AVFilterContext *sink = ofilter->filter;
  1296. ofilter->format = av_buffersink_get_format(sink);
  1297. ofilter->width = av_buffersink_get_w(sink);
  1298. ofilter->height = av_buffersink_get_h(sink);
  1299. ofilter->sample_rate = av_buffersink_get_sample_rate(sink);
  1300. av_channel_layout_uninit(&ofilter->ch_layout);
  1301. ret = av_buffersink_get_ch_layout(sink, &ofilter->ch_layout);
  1302. if (ret < 0)
  1303. goto fail;
  1304. }
  1305. for (i = 0; i < fg->nb_inputs; i++) {
  1306. InputFilterPriv *ifp = ifp_from_ifilter(fg->inputs[i]);
  1307. AVFrame *tmp;
  1308. while (av_fifo_read(ifp->frame_queue, &tmp, 1) >= 0) {
  1309. ret = av_buffersrc_add_frame(ifp->filter, tmp);
  1310. av_frame_free(&tmp);
  1311. if (ret < 0)
  1312. goto fail;
  1313. }
  1314. }
  1315. /* send the EOFs for the finished inputs */
  1316. for (i = 0; i < fg->nb_inputs; i++) {
  1317. InputFilterPriv *ifp = ifp_from_ifilter(fg->inputs[i]);
  1318. if (ifp->eof) {
  1319. ret = av_buffersrc_add_frame(ifp->filter, NULL);
  1320. if (ret < 0)
  1321. goto fail;
  1322. }
  1323. }
  1324. /* process queued up subtitle packets */
  1325. for (i = 0; i < fg->nb_inputs; i++) {
  1326. InputFilter *ifilter = fg->inputs[i];
  1327. InputFilterPriv *ifp = ifp_from_ifilter(ifilter);
  1328. if (ifp->type_src == AVMEDIA_TYPE_SUBTITLE && ifp->sub2video.queue) {
  1329. AVSubtitle tmp;
  1330. while (av_fifo_read(ifp->sub2video.queue, &tmp, 1) >= 0) {
  1331. sub2video_update(ifp, INT64_MIN, &tmp);
  1332. avsubtitle_free(&tmp);
  1333. }
  1334. }
  1335. }
  1336. return 0;
  1337. fail:
  1338. cleanup_filtergraph(fg);
  1339. return ret;
  1340. }
  1341. int ifilter_parameters_from_dec(InputFilter *ifilter, const AVCodecContext *dec)
  1342. {
  1343. InputFilterPriv *ifp = ifp_from_ifilter(ifilter);
  1344. if (dec->codec_type == AVMEDIA_TYPE_VIDEO) {
  1345. ifp->fallback.format = dec->pix_fmt;
  1346. ifp->fallback.width = dec->width;
  1347. ifp->fallback.height = dec->height;
  1348. ifp->fallback.sample_aspect_ratio = dec->sample_aspect_ratio;
  1349. } else if (dec->codec_type == AVMEDIA_TYPE_AUDIO) {
  1350. int ret;
  1351. ifp->fallback.format = dec->sample_fmt;
  1352. ifp->fallback.sample_rate = dec->sample_rate;
  1353. ret = av_channel_layout_copy(&ifp->fallback.ch_layout, &dec->ch_layout);
  1354. if (ret < 0)
  1355. return ret;
  1356. } else {
  1357. // for subtitles (i.e. sub2video) we set the actual parameters,
  1358. // rather than just fallback
  1359. ifp->width = ifp->ist->sub2video.w;
  1360. ifp->height = ifp->ist->sub2video.h;
  1361. /* rectangles are AV_PIX_FMT_PAL8, but we have no guarantee that the
  1362. palettes for all rectangles are identical or compatible */
  1363. ifp->format = AV_PIX_FMT_RGB32;
  1364. av_log(NULL, AV_LOG_VERBOSE, "sub2video: using %dx%d canvas\n", ifp->width, ifp->height);
  1365. }
  1366. return 0;
  1367. }
  1368. static int ifilter_parameters_from_frame(InputFilter *ifilter, const AVFrame *frame)
  1369. {
  1370. InputFilterPriv *ifp = ifp_from_ifilter(ifilter);
  1371. AVFrameSideData *sd;
  1372. int ret;
  1373. ret = av_buffer_replace(&ifp->hw_frames_ctx, frame->hw_frames_ctx);
  1374. if (ret < 0)
  1375. return ret;
  1376. ifp->format = frame->format;
  1377. ifp->width = frame->width;
  1378. ifp->height = frame->height;
  1379. ifp->sample_aspect_ratio = frame->sample_aspect_ratio;
  1380. ifp->sample_rate = frame->sample_rate;
  1381. ret = av_channel_layout_copy(&ifp->ch_layout, &frame->ch_layout);
  1382. if (ret < 0)
  1383. return ret;
  1384. sd = av_frame_get_side_data(frame, AV_FRAME_DATA_DISPLAYMATRIX);
  1385. if (sd)
  1386. memcpy(ifp->displaymatrix, sd->data, sizeof(ifp->displaymatrix));
  1387. ifp->displaymatrix_present = !!sd;
  1388. return 0;
  1389. }
  1390. int filtergraph_is_simple(const FilterGraph *fg)
  1391. {
  1392. const FilterGraphPriv *fgp = cfgp_from_cfg(fg);
  1393. return fgp->is_simple;
  1394. }
  1395. int reap_filters(int flush)
  1396. {
  1397. /* Reap all buffers present in the buffer sinks */
  1398. for (OutputStream *ost = ost_iter(NULL); ost; ost = ost_iter(ost)) {
  1399. FilterGraphPriv *fgp;
  1400. AVFrame *filtered_frame;
  1401. AVFilterContext *filter;
  1402. int ret = 0;
  1403. if (!ost->filter || !ost->filter->graph->graph)
  1404. continue;
  1405. filter = ost->filter->filter;
  1406. fgp = fgp_from_fg(ost->filter->graph);
  1407. filtered_frame = fgp->frame;
  1408. while (1) {
  1409. ret = av_buffersink_get_frame_flags(filter, filtered_frame,
  1410. AV_BUFFERSINK_FLAG_NO_REQUEST);
  1411. if (ret < 0) {
  1412. if (ret != AVERROR(EAGAIN) && ret != AVERROR_EOF) {
  1413. av_log(NULL, AV_LOG_WARNING,
  1414. "Error in av_buffersink_get_frame_flags(): %s\n", av_err2str(ret));
  1415. } else if (flush && ret == AVERROR_EOF) {
  1416. if (av_buffersink_get_type(filter) == AVMEDIA_TYPE_VIDEO)
  1417. enc_frame(ost, NULL);
  1418. }
  1419. break;
  1420. }
  1421. if (ost->finished) {
  1422. av_frame_unref(filtered_frame);
  1423. continue;
  1424. }
  1425. if (filtered_frame->pts != AV_NOPTS_VALUE) {
  1426. AVRational tb = av_buffersink_get_time_base(filter);
  1427. ost->filter->last_pts = av_rescale_q(filtered_frame->pts, tb,
  1428. AV_TIME_BASE_Q);
  1429. filtered_frame->time_base = tb;
  1430. if (debug_ts)
  1431. av_log(NULL, AV_LOG_INFO, "filter_raw -> pts:%s pts_time:%s time_base:%d/%d\n",
  1432. av_ts2str(filtered_frame->pts),
  1433. av_ts2timestr(filtered_frame->pts, &tb),
  1434. tb.num, tb.den);
  1435. }
  1436. if (ost->type == AVMEDIA_TYPE_VIDEO) {
  1437. FrameData *fd = frame_data(filtered_frame);
  1438. if (!fd) {
  1439. av_frame_unref(filtered_frame);
  1440. report_and_exit(AVERROR(ENOMEM));
  1441. }
  1442. fd->frame_rate_filter = av_buffersink_get_frame_rate(filter);
  1443. }
  1444. enc_frame(ost, filtered_frame);
  1445. av_frame_unref(filtered_frame);
  1446. }
  1447. }
  1448. return 0;
  1449. }
  1450. void ifilter_sub2video_heartbeat(InputFilter *ifilter, int64_t pts, AVRational tb)
  1451. {
  1452. InputFilterPriv *ifp = ifp_from_ifilter(ifilter);
  1453. int64_t pts2;
  1454. if (!ifilter->graph->graph)
  1455. return;
  1456. /* subtitles seem to be usually muxed ahead of other streams;
  1457. if not, subtracting a larger time here is necessary */
  1458. pts2 = av_rescale_q(pts, tb, ifp->time_base) - 1;
  1459. /* do not send the heartbeat frame if the subtitle is already ahead */
  1460. if (pts2 <= ifp->sub2video.last_pts)
  1461. return;
  1462. if (pts2 >= ifp->sub2video.end_pts || ifp->sub2video.initialize)
  1463. /* if we have hit the end of the current displayed subpicture,
  1464. or if we need to initialize the system, update the
  1465. overlayed subpicture and its start/end times */
  1466. sub2video_update(ifp, pts2 + 1, NULL);
  1467. if (av_buffersrc_get_nb_failed_requests(ifp->filter))
  1468. sub2video_push_ref(ifp, pts2);
  1469. }
  1470. int ifilter_sub2video(InputFilter *ifilter, const AVSubtitle *subtitle)
  1471. {
  1472. InputFilterPriv *ifp = ifp_from_ifilter(ifilter);
  1473. int ret;
  1474. if (ifilter->graph->graph) {
  1475. if (!subtitle) {
  1476. if (ifp->sub2video.end_pts < INT64_MAX)
  1477. sub2video_update(ifp, INT64_MAX, NULL);
  1478. return av_buffersrc_add_frame(ifp->filter, NULL);
  1479. }
  1480. sub2video_update(ifp, INT64_MIN, subtitle);
  1481. } else if (subtitle) {
  1482. AVSubtitle sub;
  1483. if (!ifp->sub2video.queue)
  1484. ifp->sub2video.queue = av_fifo_alloc2(8, sizeof(AVSubtitle), AV_FIFO_FLAG_AUTO_GROW);
  1485. if (!ifp->sub2video.queue)
  1486. return AVERROR(ENOMEM);
  1487. ret = copy_av_subtitle(&sub, subtitle);
  1488. if (ret < 0)
  1489. return ret;
  1490. ret = av_fifo_write(ifp->sub2video.queue, &sub, 1);
  1491. if (ret < 0) {
  1492. avsubtitle_free(&sub);
  1493. return ret;
  1494. }
  1495. }
  1496. return 0;
  1497. }
  1498. int ifilter_send_eof(InputFilter *ifilter, int64_t pts, AVRational tb)
  1499. {
  1500. InputFilterPriv *ifp = ifp_from_ifilter(ifilter);
  1501. int ret;
  1502. ifp->eof = 1;
  1503. if (ifp->filter) {
  1504. pts = av_rescale_q_rnd(pts, tb, ifp->time_base,
  1505. AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX);
  1506. ret = av_buffersrc_close(ifp->filter, pts, AV_BUFFERSRC_FLAG_PUSH);
  1507. if (ret < 0)
  1508. return ret;
  1509. } else {
  1510. if (ifp->format < 0) {
  1511. // the filtergraph was never configured, use the fallback parameters
  1512. ifp->format = ifp->fallback.format;
  1513. ifp->sample_rate = ifp->fallback.sample_rate;
  1514. ifp->width = ifp->fallback.width;
  1515. ifp->height = ifp->fallback.height;
  1516. ifp->sample_aspect_ratio = ifp->fallback.sample_aspect_ratio;
  1517. ret = av_channel_layout_copy(&ifp->ch_layout,
  1518. &ifp->fallback.ch_layout);
  1519. if (ret < 0)
  1520. return ret;
  1521. if (ifilter_has_all_input_formats(ifilter->graph)) {
  1522. ret = configure_filtergraph(ifilter->graph);
  1523. if (ret < 0) {
  1524. av_log(NULL, AV_LOG_ERROR, "Error initializing filters!\n");
  1525. return ret;
  1526. }
  1527. }
  1528. }
  1529. if (ifp->format < 0) {
  1530. av_log(NULL, AV_LOG_ERROR,
  1531. "Cannot determine format of input stream %d:%d after EOF\n",
  1532. ifp->ist->file_index, ifp->ist->index);
  1533. return AVERROR_INVALIDDATA;
  1534. }
  1535. }
  1536. return 0;
  1537. }
  1538. int ifilter_send_frame(InputFilter *ifilter, AVFrame *frame, int keep_reference)
  1539. {
  1540. InputFilterPriv *ifp = ifp_from_ifilter(ifilter);
  1541. FilterGraph *fg = ifilter->graph;
  1542. AVFrameSideData *sd;
  1543. int need_reinit, ret;
  1544. /* determine if the parameters for this input changed */
  1545. need_reinit = ifp->format != frame->format;
  1546. switch (ifp->type) {
  1547. case AVMEDIA_TYPE_AUDIO:
  1548. need_reinit |= ifp->sample_rate != frame->sample_rate ||
  1549. av_channel_layout_compare(&ifp->ch_layout, &frame->ch_layout);
  1550. break;
  1551. case AVMEDIA_TYPE_VIDEO:
  1552. need_reinit |= ifp->width != frame->width ||
  1553. ifp->height != frame->height;
  1554. break;
  1555. }
  1556. if (!ifp->ist->reinit_filters && fg->graph)
  1557. need_reinit = 0;
  1558. if (!!ifp->hw_frames_ctx != !!frame->hw_frames_ctx ||
  1559. (ifp->hw_frames_ctx && ifp->hw_frames_ctx->data != frame->hw_frames_ctx->data))
  1560. need_reinit = 1;
  1561. if (sd = av_frame_get_side_data(frame, AV_FRAME_DATA_DISPLAYMATRIX)) {
  1562. if (!ifp->displaymatrix_present ||
  1563. memcmp(sd->data, ifp->displaymatrix, sizeof(ifp->displaymatrix)))
  1564. need_reinit = 1;
  1565. } else if (ifp->displaymatrix_present)
  1566. need_reinit = 1;
  1567. if (need_reinit) {
  1568. ret = ifilter_parameters_from_frame(ifilter, frame);
  1569. if (ret < 0)
  1570. return ret;
  1571. }
  1572. /* (re)init the graph if possible, otherwise buffer the frame and return */
  1573. if (need_reinit || !fg->graph) {
  1574. if (!ifilter_has_all_input_formats(fg)) {
  1575. AVFrame *tmp = av_frame_clone(frame);
  1576. if (!tmp)
  1577. return AVERROR(ENOMEM);
  1578. ret = av_fifo_write(ifp->frame_queue, &tmp, 1);
  1579. if (ret < 0)
  1580. av_frame_free(&tmp);
  1581. return ret;
  1582. }
  1583. ret = reap_filters(0);
  1584. if (ret < 0 && ret != AVERROR_EOF) {
  1585. av_log(NULL, AV_LOG_ERROR, "Error while filtering: %s\n", av_err2str(ret));
  1586. return ret;
  1587. }
  1588. ret = configure_filtergraph(fg);
  1589. if (ret < 0) {
  1590. av_log(NULL, AV_LOG_ERROR, "Error reinitializing filters!\n");
  1591. return ret;
  1592. }
  1593. }
  1594. if (keep_reference) {
  1595. ret = av_frame_ref(ifp->frame, frame);
  1596. if (ret < 0)
  1597. return ret;
  1598. } else
  1599. av_frame_move_ref(ifp->frame, frame);
  1600. frame = ifp->frame;
  1601. frame->pts = av_rescale_q(frame->pts, frame->time_base, ifp->time_base);
  1602. frame->duration = av_rescale_q(frame->duration, frame->time_base, ifp->time_base);
  1603. frame->time_base = ifp->time_base;
  1604. #if LIBAVUTIL_VERSION_MAJOR < 59
  1605. AV_NOWARN_DEPRECATED(
  1606. frame->pkt_duration = frame->duration;
  1607. )
  1608. #endif
  1609. ret = av_buffersrc_add_frame_flags(ifp->filter, frame,
  1610. AV_BUFFERSRC_FLAG_PUSH);
  1611. if (ret < 0) {
  1612. av_frame_unref(frame);
  1613. if (ret != AVERROR_EOF)
  1614. av_log(NULL, AV_LOG_ERROR, "Error while filtering: %s\n", av_err2str(ret));
  1615. return ret;
  1616. }
  1617. return 0;
  1618. }
  1619. int fg_transcode_step(FilterGraph *graph, InputStream **best_ist)
  1620. {
  1621. int i, ret;
  1622. int nb_requests, nb_requests_max = 0;
  1623. InputStream *ist;
  1624. if (!graph->graph) {
  1625. for (int i = 0; i < graph->nb_inputs; i++) {
  1626. InputFilter *ifilter = graph->inputs[i];
  1627. InputFilterPriv *ifp = ifp_from_ifilter(ifilter);
  1628. if (ifp->format < 0 && !ifp->eof) {
  1629. *best_ist = ifp->ist;
  1630. return 0;
  1631. }
  1632. }
  1633. // graph not configured, but all inputs are either initialized or EOF
  1634. for (int i = 0; i < graph->nb_outputs; i++)
  1635. graph->outputs[i]->ost->inputs_done = 1;
  1636. return 0;
  1637. }
  1638. *best_ist = NULL;
  1639. ret = avfilter_graph_request_oldest(graph->graph);
  1640. if (ret >= 0)
  1641. return reap_filters(0);
  1642. if (ret == AVERROR_EOF) {
  1643. ret = reap_filters(1);
  1644. for (i = 0; i < graph->nb_outputs; i++)
  1645. close_output_stream(graph->outputs[i]->ost);
  1646. return ret;
  1647. }
  1648. if (ret != AVERROR(EAGAIN))
  1649. return ret;
  1650. for (i = 0; i < graph->nb_inputs; i++) {
  1651. InputFilter *ifilter = graph->inputs[i];
  1652. InputFilterPriv *ifp = ifp_from_ifilter(ifilter);
  1653. ist = ifp->ist;
  1654. if (input_files[ist->file_index]->eagain || ifp->eof)
  1655. continue;
  1656. nb_requests = av_buffersrc_get_nb_failed_requests(ifp->filter);
  1657. if (nb_requests > nb_requests_max) {
  1658. nb_requests_max = nb_requests;
  1659. *best_ist = ist;
  1660. }
  1661. }
  1662. if (!*best_ist)
  1663. for (i = 0; i < graph->nb_outputs; i++)
  1664. graph->outputs[i]->ost->unavailable = 1;
  1665. return 0;
  1666. }