You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1305 lines
45KB

  1. /*
  2. * filter graphs
  3. * Copyright (c) 2008 Vitor Sessak
  4. * Copyright (c) 2007 Bobby Bingham
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "config.h"
  23. #include <string.h>
  24. #include "libavutil/avassert.h"
  25. #include "libavutil/avstring.h"
  26. #include "libavutil/bprint.h"
  27. #include "libavutil/channel_layout.h"
  28. #include "libavutil/internal.h"
  29. #include "libavutil/opt.h"
  30. #include "libavutil/pixdesc.h"
  31. #include "libavcodec/avcodec.h" // avcodec_find_best_pix_fmt_of_2()
  32. #include "avfilter.h"
  33. #include "formats.h"
  34. #include "internal.h"
  35. #include "thread.h"
  36. #define OFFSET(x) offsetof(AVFilterGraph, x)
  37. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  38. static const AVOption filtergraph_options[] = {
  39. { "thread_type", "Allowed thread types", OFFSET(thread_type), AV_OPT_TYPE_FLAGS,
  40. { .i64 = AVFILTER_THREAD_SLICE }, 0, INT_MAX, FLAGS, "thread_type" },
  41. { "slice", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AVFILTER_THREAD_SLICE }, .flags = FLAGS, .unit = "thread_type" },
  42. { "threads", "Maximum number of threads", OFFSET(nb_threads),
  43. AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
  44. {"scale_sws_opts" , "default scale filter options" , OFFSET(scale_sws_opts) ,
  45. AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
  46. {"aresample_swr_opts" , "default aresample filter options" , OFFSET(aresample_swr_opts) ,
  47. AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
  48. { NULL },
  49. };
  50. static const AVClass filtergraph_class = {
  51. .class_name = "AVFilterGraph",
  52. .item_name = av_default_item_name,
  53. .version = LIBAVUTIL_VERSION_INT,
  54. .option = filtergraph_options,
  55. .category = AV_CLASS_CATEGORY_FILTER,
  56. };
  57. #if !HAVE_THREADS
  58. void ff_graph_thread_free(AVFilterGraph *graph)
  59. {
  60. }
  61. int ff_graph_thread_init(AVFilterGraph *graph)
  62. {
  63. graph->thread_type = 0;
  64. graph->nb_threads = 1;
  65. return 0;
  66. }
  67. #endif
  68. AVFilterGraph *avfilter_graph_alloc(void)
  69. {
  70. AVFilterGraph *ret = av_mallocz(sizeof(*ret));
  71. if (!ret)
  72. return NULL;
  73. ret->internal = av_mallocz(sizeof(*ret->internal));
  74. if (!ret->internal) {
  75. av_freep(&ret);
  76. return NULL;
  77. }
  78. ret->av_class = &filtergraph_class;
  79. av_opt_set_defaults(ret);
  80. return ret;
  81. }
  82. void ff_filter_graph_remove_filter(AVFilterGraph *graph, AVFilterContext *filter)
  83. {
  84. int i;
  85. for (i = 0; i < graph->nb_filters; i++) {
  86. if (graph->filters[i] == filter) {
  87. FFSWAP(AVFilterContext*, graph->filters[i],
  88. graph->filters[graph->nb_filters - 1]);
  89. graph->nb_filters--;
  90. return;
  91. }
  92. }
  93. }
  94. void avfilter_graph_free(AVFilterGraph **graph)
  95. {
  96. if (!*graph)
  97. return;
  98. while ((*graph)->nb_filters)
  99. avfilter_free((*graph)->filters[0]);
  100. ff_graph_thread_free(*graph);
  101. av_freep(&(*graph)->sink_links);
  102. av_freep(&(*graph)->scale_sws_opts);
  103. av_freep(&(*graph)->aresample_swr_opts);
  104. av_freep(&(*graph)->resample_lavr_opts);
  105. av_freep(&(*graph)->filters);
  106. av_freep(&(*graph)->internal);
  107. av_freep(graph);
  108. }
  109. #if FF_API_AVFILTER_OPEN
  110. int avfilter_graph_add_filter(AVFilterGraph *graph, AVFilterContext *filter)
  111. {
  112. AVFilterContext **filters = av_realloc(graph->filters,
  113. sizeof(*filters) * (graph->nb_filters + 1));
  114. if (!filters)
  115. return AVERROR(ENOMEM);
  116. graph->filters = filters;
  117. graph->filters[graph->nb_filters++] = filter;
  118. #if FF_API_FOO_COUNT
  119. FF_DISABLE_DEPRECATION_WARNINGS
  120. graph->filter_count_unused = graph->nb_filters;
  121. FF_ENABLE_DEPRECATION_WARNINGS
  122. #endif
  123. filter->graph = graph;
  124. return 0;
  125. }
  126. #endif
  127. int avfilter_graph_create_filter(AVFilterContext **filt_ctx, AVFilter *filt,
  128. const char *name, const char *args, void *opaque,
  129. AVFilterGraph *graph_ctx)
  130. {
  131. int ret;
  132. *filt_ctx = avfilter_graph_alloc_filter(graph_ctx, filt, name);
  133. if (!*filt_ctx)
  134. return AVERROR(ENOMEM);
  135. ret = avfilter_init_str(*filt_ctx, args);
  136. if (ret < 0)
  137. goto fail;
  138. return 0;
  139. fail:
  140. if (*filt_ctx)
  141. avfilter_free(*filt_ctx);
  142. *filt_ctx = NULL;
  143. return ret;
  144. }
  145. void avfilter_graph_set_auto_convert(AVFilterGraph *graph, unsigned flags)
  146. {
  147. graph->disable_auto_convert = flags;
  148. }
  149. AVFilterContext *avfilter_graph_alloc_filter(AVFilterGraph *graph,
  150. const AVFilter *filter,
  151. const char *name)
  152. {
  153. AVFilterContext **filters, *s;
  154. if (graph->thread_type && !graph->internal->thread) {
  155. int ret = ff_graph_thread_init(graph);
  156. if (ret < 0) {
  157. av_log(graph, AV_LOG_ERROR, "Error initializing threading.\n");
  158. return NULL;
  159. }
  160. }
  161. s = ff_filter_alloc(filter, name);
  162. if (!s)
  163. return NULL;
  164. filters = av_realloc(graph->filters, sizeof(*filters) * (graph->nb_filters + 1));
  165. if (!filters) {
  166. avfilter_free(s);
  167. return NULL;
  168. }
  169. graph->filters = filters;
  170. graph->filters[graph->nb_filters++] = s;
  171. #if FF_API_FOO_COUNT
  172. FF_DISABLE_DEPRECATION_WARNINGS
  173. graph->filter_count_unused = graph->nb_filters;
  174. FF_ENABLE_DEPRECATION_WARNINGS
  175. #endif
  176. s->graph = graph;
  177. return s;
  178. }
  179. /**
  180. * Check for the validity of graph.
  181. *
  182. * A graph is considered valid if all its input and output pads are
  183. * connected.
  184. *
  185. * @return 0 in case of success, a negative value otherwise
  186. */
  187. static int graph_check_validity(AVFilterGraph *graph, AVClass *log_ctx)
  188. {
  189. AVFilterContext *filt;
  190. int i, j;
  191. for (i = 0; i < graph->nb_filters; i++) {
  192. const AVFilterPad *pad;
  193. filt = graph->filters[i];
  194. for (j = 0; j < filt->nb_inputs; j++) {
  195. if (!filt->inputs[j] || !filt->inputs[j]->src) {
  196. pad = &filt->input_pads[j];
  197. av_log(log_ctx, AV_LOG_ERROR,
  198. "Input pad \"%s\" with type %s of the filter instance \"%s\" of %s not connected to any source\n",
  199. pad->name, av_get_media_type_string(pad->type), filt->name, filt->filter->name);
  200. return AVERROR(EINVAL);
  201. }
  202. }
  203. for (j = 0; j < filt->nb_outputs; j++) {
  204. if (!filt->outputs[j] || !filt->outputs[j]->dst) {
  205. pad = &filt->output_pads[j];
  206. av_log(log_ctx, AV_LOG_ERROR,
  207. "Output pad \"%s\" with type %s of the filter instance \"%s\" of %s not connected to any destination\n",
  208. pad->name, av_get_media_type_string(pad->type), filt->name, filt->filter->name);
  209. return AVERROR(EINVAL);
  210. }
  211. }
  212. }
  213. return 0;
  214. }
  215. /**
  216. * Configure all the links of graphctx.
  217. *
  218. * @return 0 in case of success, a negative value otherwise
  219. */
  220. static int graph_config_links(AVFilterGraph *graph, AVClass *log_ctx)
  221. {
  222. AVFilterContext *filt;
  223. int i, ret;
  224. for (i = 0; i < graph->nb_filters; i++) {
  225. filt = graph->filters[i];
  226. if (!filt->nb_outputs) {
  227. if ((ret = avfilter_config_links(filt)))
  228. return ret;
  229. }
  230. }
  231. return 0;
  232. }
  233. AVFilterContext *avfilter_graph_get_filter(AVFilterGraph *graph, char *name)
  234. {
  235. int i;
  236. for (i = 0; i < graph->nb_filters; i++)
  237. if (graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
  238. return graph->filters[i];
  239. return NULL;
  240. }
  241. static void sanitize_channel_layouts(void *log, AVFilterChannelLayouts *l)
  242. {
  243. if (!l)
  244. return;
  245. if (l->nb_channel_layouts) {
  246. if (l->all_layouts || l->all_counts)
  247. av_log(log, AV_LOG_WARNING, "All layouts set on non-empty list\n");
  248. l->all_layouts = l->all_counts = 0;
  249. } else {
  250. if (l->all_counts && !l->all_layouts)
  251. av_log(log, AV_LOG_WARNING, "All counts without all layouts\n");
  252. l->all_layouts = 1;
  253. }
  254. }
  255. static int filter_query_formats(AVFilterContext *ctx)
  256. {
  257. int ret, i;
  258. AVFilterFormats *formats;
  259. AVFilterChannelLayouts *chlayouts;
  260. AVFilterFormats *samplerates;
  261. enum AVMediaType type = ctx->inputs && ctx->inputs [0] ? ctx->inputs [0]->type :
  262. ctx->outputs && ctx->outputs[0] ? ctx->outputs[0]->type :
  263. AVMEDIA_TYPE_VIDEO;
  264. if ((ret = ctx->filter->query_formats(ctx)) < 0) {
  265. if (ret != AVERROR(EAGAIN))
  266. av_log(ctx, AV_LOG_ERROR, "Query format failed for '%s': %s\n",
  267. ctx->name, av_err2str(ret));
  268. return ret;
  269. }
  270. for (i = 0; i < ctx->nb_inputs; i++)
  271. sanitize_channel_layouts(ctx, ctx->inputs[i]->out_channel_layouts);
  272. for (i = 0; i < ctx->nb_outputs; i++)
  273. sanitize_channel_layouts(ctx, ctx->outputs[i]->in_channel_layouts);
  274. formats = ff_all_formats(type);
  275. if (!formats)
  276. return AVERROR(ENOMEM);
  277. ff_set_common_formats(ctx, formats);
  278. if (type == AVMEDIA_TYPE_AUDIO) {
  279. samplerates = ff_all_samplerates();
  280. if (!samplerates)
  281. return AVERROR(ENOMEM);
  282. ff_set_common_samplerates(ctx, samplerates);
  283. chlayouts = ff_all_channel_layouts();
  284. if (!chlayouts)
  285. return AVERROR(ENOMEM);
  286. ff_set_common_channel_layouts(ctx, chlayouts);
  287. }
  288. return 0;
  289. }
  290. static int formats_declared(AVFilterContext *f)
  291. {
  292. int i;
  293. for (i = 0; i < f->nb_inputs; i++) {
  294. if (!f->inputs[i]->out_formats)
  295. return 0;
  296. if (f->inputs[i]->type == AVMEDIA_TYPE_AUDIO &&
  297. !(f->inputs[i]->out_samplerates &&
  298. f->inputs[i]->out_channel_layouts))
  299. return 0;
  300. }
  301. for (i = 0; i < f->nb_outputs; i++) {
  302. if (!f->outputs[i]->in_formats)
  303. return 0;
  304. if (f->outputs[i]->type == AVMEDIA_TYPE_AUDIO &&
  305. !(f->outputs[i]->in_samplerates &&
  306. f->outputs[i]->in_channel_layouts))
  307. return 0;
  308. }
  309. return 1;
  310. }
  311. static AVFilterFormats *clone_filter_formats(AVFilterFormats *arg)
  312. {
  313. AVFilterFormats *a = av_memdup(arg, sizeof(*arg));
  314. if (a) {
  315. a->refcount = 0;
  316. a->refs = NULL;
  317. a->formats = av_memdup(a->formats, sizeof(*a->formats) * a->nb_formats);
  318. if (!a->formats && arg->formats)
  319. av_freep(&a);
  320. }
  321. return a;
  322. }
  323. static int can_merge_formats(AVFilterFormats *a_arg,
  324. AVFilterFormats *b_arg,
  325. enum AVMediaType type,
  326. int is_sample_rate)
  327. {
  328. AVFilterFormats *a, *b, *ret;
  329. if (a_arg == b_arg)
  330. return 1;
  331. a = clone_filter_formats(a_arg);
  332. b = clone_filter_formats(b_arg);
  333. if (is_sample_rate) {
  334. ret = ff_merge_samplerates(a, b);
  335. } else {
  336. ret = ff_merge_formats(a, b, type);
  337. }
  338. if (ret) {
  339. av_freep(&ret->formats);
  340. av_freep(&ret->refs);
  341. av_freep(&ret);
  342. return 1;
  343. } else {
  344. av_freep(&a->formats);
  345. av_freep(&b->formats);
  346. av_freep(&a);
  347. av_freep(&b);
  348. return 0;
  349. }
  350. }
  351. /**
  352. * Perform one round of query_formats() and merging formats lists on the
  353. * filter graph.
  354. * @return >=0 if all links formats lists could be queried and merged;
  355. * AVERROR(EAGAIN) some progress was made in the queries or merging
  356. * and a later call may succeed;
  357. * AVERROR(EIO) (may be changed) plus a log message if no progress
  358. * was made and the negotiation is stuck;
  359. * a negative error code if some other error happened
  360. */
  361. static int query_formats(AVFilterGraph *graph, AVClass *log_ctx)
  362. {
  363. int i, j, ret;
  364. int scaler_count = 0, resampler_count = 0;
  365. int count_queried = 0; /* successful calls to query_formats() */
  366. int count_merged = 0; /* successful merge of formats lists */
  367. int count_already_merged = 0; /* lists already merged */
  368. int count_delayed = 0; /* lists that need to be merged later */
  369. for (i = 0; i < graph->nb_filters; i++) {
  370. AVFilterContext *f = graph->filters[i];
  371. if (formats_declared(f))
  372. continue;
  373. if (f->filter->query_formats)
  374. ret = filter_query_formats(f);
  375. else
  376. ret = ff_default_query_formats(f);
  377. if (ret < 0 && ret != AVERROR(EAGAIN))
  378. return ret;
  379. /* note: EAGAIN could indicate a partial success, not counted yet */
  380. count_queried += ret >= 0;
  381. }
  382. /* go through and merge as many format lists as possible */
  383. for (i = 0; i < graph->nb_filters; i++) {
  384. AVFilterContext *filter = graph->filters[i];
  385. for (j = 0; j < filter->nb_inputs; j++) {
  386. AVFilterLink *link = filter->inputs[j];
  387. int convert_needed = 0;
  388. if (!link)
  389. continue;
  390. if (link->in_formats != link->out_formats
  391. && link->in_formats && link->out_formats)
  392. if (!can_merge_formats(link->in_formats, link->out_formats,
  393. link->type, 0))
  394. convert_needed = 1;
  395. if (link->type == AVMEDIA_TYPE_AUDIO) {
  396. if (link->in_samplerates != link->out_samplerates
  397. && link->in_samplerates && link->out_samplerates)
  398. if (!can_merge_formats(link->in_samplerates,
  399. link->out_samplerates,
  400. 0, 1))
  401. convert_needed = 1;
  402. }
  403. #define MERGE_DISPATCH(field, statement) \
  404. if (!(link->in_ ## field && link->out_ ## field)) { \
  405. count_delayed++; \
  406. } else if (link->in_ ## field == link->out_ ## field) { \
  407. count_already_merged++; \
  408. } else if (!convert_needed) { \
  409. count_merged++; \
  410. statement \
  411. }
  412. if (link->type == AVMEDIA_TYPE_AUDIO) {
  413. MERGE_DISPATCH(channel_layouts,
  414. if (!ff_merge_channel_layouts(link->in_channel_layouts,
  415. link->out_channel_layouts))
  416. convert_needed = 1;
  417. )
  418. MERGE_DISPATCH(samplerates,
  419. if (!ff_merge_samplerates(link->in_samplerates,
  420. link->out_samplerates))
  421. convert_needed = 1;
  422. )
  423. }
  424. MERGE_DISPATCH(formats,
  425. if (!ff_merge_formats(link->in_formats, link->out_formats,
  426. link->type))
  427. convert_needed = 1;
  428. )
  429. #undef MERGE_DISPATCH
  430. if (convert_needed) {
  431. AVFilterContext *convert;
  432. AVFilter *filter;
  433. AVFilterLink *inlink, *outlink;
  434. char scale_args[256];
  435. char inst_name[30];
  436. /* couldn't merge format lists. auto-insert conversion filter */
  437. switch (link->type) {
  438. case AVMEDIA_TYPE_VIDEO:
  439. if (!(filter = avfilter_get_by_name("scale"))) {
  440. av_log(log_ctx, AV_LOG_ERROR, "'scale' filter "
  441. "not present, cannot convert pixel formats.\n");
  442. return AVERROR(EINVAL);
  443. }
  444. snprintf(inst_name, sizeof(inst_name), "auto-inserted scaler %d",
  445. scaler_count++);
  446. if ((ret = avfilter_graph_create_filter(&convert, filter,
  447. inst_name, graph->scale_sws_opts, NULL,
  448. graph)) < 0)
  449. return ret;
  450. break;
  451. case AVMEDIA_TYPE_AUDIO:
  452. if (!(filter = avfilter_get_by_name("aresample"))) {
  453. av_log(log_ctx, AV_LOG_ERROR, "'aresample' filter "
  454. "not present, cannot convert audio formats.\n");
  455. return AVERROR(EINVAL);
  456. }
  457. snprintf(inst_name, sizeof(inst_name), "auto-inserted resampler %d",
  458. resampler_count++);
  459. scale_args[0] = '\0';
  460. if (graph->aresample_swr_opts)
  461. snprintf(scale_args, sizeof(scale_args), "%s",
  462. graph->aresample_swr_opts);
  463. if ((ret = avfilter_graph_create_filter(&convert, filter,
  464. inst_name, graph->aresample_swr_opts,
  465. NULL, graph)) < 0)
  466. return ret;
  467. break;
  468. default:
  469. return AVERROR(EINVAL);
  470. }
  471. if ((ret = avfilter_insert_filter(link, convert, 0, 0)) < 0)
  472. return ret;
  473. filter_query_formats(convert);
  474. inlink = convert->inputs[0];
  475. outlink = convert->outputs[0];
  476. if (!ff_merge_formats( inlink->in_formats, inlink->out_formats, inlink->type) ||
  477. !ff_merge_formats(outlink->in_formats, outlink->out_formats, outlink->type))
  478. ret |= AVERROR(ENOSYS);
  479. if (inlink->type == AVMEDIA_TYPE_AUDIO &&
  480. (!ff_merge_samplerates(inlink->in_samplerates,
  481. inlink->out_samplerates) ||
  482. !ff_merge_channel_layouts(inlink->in_channel_layouts,
  483. inlink->out_channel_layouts)))
  484. ret |= AVERROR(ENOSYS);
  485. if (outlink->type == AVMEDIA_TYPE_AUDIO &&
  486. (!ff_merge_samplerates(outlink->in_samplerates,
  487. outlink->out_samplerates) ||
  488. !ff_merge_channel_layouts(outlink->in_channel_layouts,
  489. outlink->out_channel_layouts)))
  490. ret |= AVERROR(ENOSYS);
  491. if (ret < 0) {
  492. av_log(log_ctx, AV_LOG_ERROR,
  493. "Impossible to convert between the formats supported by the filter "
  494. "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
  495. return ret;
  496. }
  497. }
  498. }
  499. }
  500. av_log(graph, AV_LOG_DEBUG, "query_formats: "
  501. "%d queried, %d merged, %d already done, %d delayed\n",
  502. count_queried, count_merged, count_already_merged, count_delayed);
  503. if (count_delayed) {
  504. AVBPrint bp;
  505. /* if count_queried > 0, one filter at least did set its formats,
  506. that will give additional information to its neighbour;
  507. if count_merged > 0, one pair of formats lists at least was merged,
  508. that will give additional information to all connected filters;
  509. in both cases, progress was made and a new round must be done */
  510. if (count_queried || count_merged)
  511. return AVERROR(EAGAIN);
  512. av_bprint_init(&bp, 0, AV_BPRINT_SIZE_AUTOMATIC);
  513. for (i = 0; i < graph->nb_filters; i++)
  514. if (!formats_declared(graph->filters[i]))
  515. av_bprintf(&bp, "%s%s", bp.len ? ", " : "",
  516. graph->filters[i]->name);
  517. av_log(graph, AV_LOG_ERROR,
  518. "The following filters could not choose their formats: %s\n"
  519. "Consider inserting the (a)format filter near their input or "
  520. "output.\n", bp.str);
  521. return AVERROR(EIO);
  522. }
  523. return 0;
  524. }
  525. static int pick_format(AVFilterLink *link, AVFilterLink *ref)
  526. {
  527. if (!link || !link->in_formats)
  528. return 0;
  529. if (link->type == AVMEDIA_TYPE_VIDEO) {
  530. if(ref && ref->type == AVMEDIA_TYPE_VIDEO){
  531. int has_alpha= av_pix_fmt_desc_get(ref->format)->nb_components % 2 == 0;
  532. enum AVPixelFormat best= AV_PIX_FMT_NONE;
  533. int i;
  534. for (i=0; i<link->in_formats->nb_formats; i++) {
  535. enum AVPixelFormat p = link->in_formats->formats[i];
  536. best= avcodec_find_best_pix_fmt_of_2(best, p, ref->format, has_alpha, NULL);
  537. }
  538. av_log(link->src,AV_LOG_DEBUG, "picking %s out of %d ref:%s alpha:%d\n",
  539. av_get_pix_fmt_name(best), link->in_formats->nb_formats,
  540. av_get_pix_fmt_name(ref->format), has_alpha);
  541. link->in_formats->formats[0] = best;
  542. }
  543. }
  544. link->in_formats->nb_formats = 1;
  545. link->format = link->in_formats->formats[0];
  546. if (link->type == AVMEDIA_TYPE_AUDIO) {
  547. if (!link->in_samplerates->nb_formats) {
  548. av_log(link->src, AV_LOG_ERROR, "Cannot select sample rate for"
  549. " the link between filters %s and %s.\n", link->src->name,
  550. link->dst->name);
  551. return AVERROR(EINVAL);
  552. }
  553. link->in_samplerates->nb_formats = 1;
  554. link->sample_rate = link->in_samplerates->formats[0];
  555. if (link->in_channel_layouts->all_layouts) {
  556. av_log(link->src, AV_LOG_ERROR, "Cannot select channel layout for"
  557. " the link between filters %s and %s.\n", link->src->name,
  558. link->dst->name);
  559. return AVERROR(EINVAL);
  560. }
  561. link->in_channel_layouts->nb_channel_layouts = 1;
  562. link->channel_layout = link->in_channel_layouts->channel_layouts[0];
  563. if ((link->channels = FF_LAYOUT2COUNT(link->channel_layout)))
  564. link->channel_layout = 0;
  565. else
  566. link->channels = av_get_channel_layout_nb_channels(link->channel_layout);
  567. }
  568. ff_formats_unref(&link->in_formats);
  569. ff_formats_unref(&link->out_formats);
  570. ff_formats_unref(&link->in_samplerates);
  571. ff_formats_unref(&link->out_samplerates);
  572. ff_channel_layouts_unref(&link->in_channel_layouts);
  573. ff_channel_layouts_unref(&link->out_channel_layouts);
  574. return 0;
  575. }
  576. #define REDUCE_FORMATS(fmt_type, list_type, list, var, nb, add_format) \
  577. do { \
  578. for (i = 0; i < filter->nb_inputs; i++) { \
  579. AVFilterLink *link = filter->inputs[i]; \
  580. fmt_type fmt; \
  581. \
  582. if (!link->out_ ## list || link->out_ ## list->nb != 1) \
  583. continue; \
  584. fmt = link->out_ ## list->var[0]; \
  585. \
  586. for (j = 0; j < filter->nb_outputs; j++) { \
  587. AVFilterLink *out_link = filter->outputs[j]; \
  588. list_type *fmts; \
  589. \
  590. if (link->type != out_link->type || \
  591. out_link->in_ ## list->nb == 1) \
  592. continue; \
  593. fmts = out_link->in_ ## list; \
  594. \
  595. if (!out_link->in_ ## list->nb) { \
  596. add_format(&out_link->in_ ##list, fmt); \
  597. ret = 1; \
  598. break; \
  599. } \
  600. \
  601. for (k = 0; k < out_link->in_ ## list->nb; k++) \
  602. if (fmts->var[k] == fmt) { \
  603. fmts->var[0] = fmt; \
  604. fmts->nb = 1; \
  605. ret = 1; \
  606. break; \
  607. } \
  608. } \
  609. } \
  610. } while (0)
  611. static int reduce_formats_on_filter(AVFilterContext *filter)
  612. {
  613. int i, j, k, ret = 0;
  614. REDUCE_FORMATS(int, AVFilterFormats, formats, formats,
  615. nb_formats, ff_add_format);
  616. REDUCE_FORMATS(int, AVFilterFormats, samplerates, formats,
  617. nb_formats, ff_add_format);
  618. /* reduce channel layouts */
  619. for (i = 0; i < filter->nb_inputs; i++) {
  620. AVFilterLink *inlink = filter->inputs[i];
  621. uint64_t fmt;
  622. if (!inlink->out_channel_layouts ||
  623. inlink->out_channel_layouts->nb_channel_layouts != 1)
  624. continue;
  625. fmt = inlink->out_channel_layouts->channel_layouts[0];
  626. for (j = 0; j < filter->nb_outputs; j++) {
  627. AVFilterLink *outlink = filter->outputs[j];
  628. AVFilterChannelLayouts *fmts;
  629. fmts = outlink->in_channel_layouts;
  630. if (inlink->type != outlink->type || fmts->nb_channel_layouts == 1)
  631. continue;
  632. if (fmts->all_layouts) {
  633. /* Turn the infinite list into a singleton */
  634. fmts->all_layouts = fmts->all_counts = 0;
  635. ff_add_channel_layout(&outlink->in_channel_layouts, fmt);
  636. break;
  637. }
  638. for (k = 0; k < outlink->in_channel_layouts->nb_channel_layouts; k++) {
  639. if (fmts->channel_layouts[k] == fmt) {
  640. fmts->channel_layouts[0] = fmt;
  641. fmts->nb_channel_layouts = 1;
  642. ret = 1;
  643. break;
  644. }
  645. }
  646. }
  647. }
  648. return ret;
  649. }
  650. static void reduce_formats(AVFilterGraph *graph)
  651. {
  652. int i, reduced;
  653. do {
  654. reduced = 0;
  655. for (i = 0; i < graph->nb_filters; i++)
  656. reduced |= reduce_formats_on_filter(graph->filters[i]);
  657. } while (reduced);
  658. }
  659. static void swap_samplerates_on_filter(AVFilterContext *filter)
  660. {
  661. AVFilterLink *link = NULL;
  662. int sample_rate;
  663. int i, j;
  664. for (i = 0; i < filter->nb_inputs; i++) {
  665. link = filter->inputs[i];
  666. if (link->type == AVMEDIA_TYPE_AUDIO &&
  667. link->out_samplerates->nb_formats== 1)
  668. break;
  669. }
  670. if (i == filter->nb_inputs)
  671. return;
  672. sample_rate = link->out_samplerates->formats[0];
  673. for (i = 0; i < filter->nb_outputs; i++) {
  674. AVFilterLink *outlink = filter->outputs[i];
  675. int best_idx, best_diff = INT_MAX;
  676. if (outlink->type != AVMEDIA_TYPE_AUDIO ||
  677. outlink->in_samplerates->nb_formats < 2)
  678. continue;
  679. for (j = 0; j < outlink->in_samplerates->nb_formats; j++) {
  680. int diff = abs(sample_rate - outlink->in_samplerates->formats[j]);
  681. if (diff < best_diff) {
  682. best_diff = diff;
  683. best_idx = j;
  684. }
  685. }
  686. FFSWAP(int, outlink->in_samplerates->formats[0],
  687. outlink->in_samplerates->formats[best_idx]);
  688. }
  689. }
  690. static void swap_samplerates(AVFilterGraph *graph)
  691. {
  692. int i;
  693. for (i = 0; i < graph->nb_filters; i++)
  694. swap_samplerates_on_filter(graph->filters[i]);
  695. }
  696. #define CH_CENTER_PAIR (AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER)
  697. #define CH_FRONT_PAIR (AV_CH_FRONT_LEFT | AV_CH_FRONT_RIGHT)
  698. #define CH_STEREO_PAIR (AV_CH_STEREO_LEFT | AV_CH_STEREO_RIGHT)
  699. #define CH_WIDE_PAIR (AV_CH_WIDE_LEFT | AV_CH_WIDE_RIGHT)
  700. #define CH_SIDE_PAIR (AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT)
  701. #define CH_DIRECT_PAIR (AV_CH_SURROUND_DIRECT_LEFT | AV_CH_SURROUND_DIRECT_RIGHT)
  702. #define CH_BACK_PAIR (AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT)
  703. /* allowable substitutions for channel pairs when comparing layouts,
  704. * ordered by priority for both values */
  705. static const uint64_t ch_subst[][2] = {
  706. { CH_FRONT_PAIR, CH_CENTER_PAIR },
  707. { CH_FRONT_PAIR, CH_WIDE_PAIR },
  708. { CH_FRONT_PAIR, AV_CH_FRONT_CENTER },
  709. { CH_CENTER_PAIR, CH_FRONT_PAIR },
  710. { CH_CENTER_PAIR, CH_WIDE_PAIR },
  711. { CH_CENTER_PAIR, AV_CH_FRONT_CENTER },
  712. { CH_WIDE_PAIR, CH_FRONT_PAIR },
  713. { CH_WIDE_PAIR, CH_CENTER_PAIR },
  714. { CH_WIDE_PAIR, AV_CH_FRONT_CENTER },
  715. { AV_CH_FRONT_CENTER, CH_FRONT_PAIR },
  716. { AV_CH_FRONT_CENTER, CH_CENTER_PAIR },
  717. { AV_CH_FRONT_CENTER, CH_WIDE_PAIR },
  718. { CH_SIDE_PAIR, CH_DIRECT_PAIR },
  719. { CH_SIDE_PAIR, CH_BACK_PAIR },
  720. { CH_SIDE_PAIR, AV_CH_BACK_CENTER },
  721. { CH_BACK_PAIR, CH_DIRECT_PAIR },
  722. { CH_BACK_PAIR, CH_SIDE_PAIR },
  723. { CH_BACK_PAIR, AV_CH_BACK_CENTER },
  724. { AV_CH_BACK_CENTER, CH_BACK_PAIR },
  725. { AV_CH_BACK_CENTER, CH_DIRECT_PAIR },
  726. { AV_CH_BACK_CENTER, CH_SIDE_PAIR },
  727. };
  728. static void swap_channel_layouts_on_filter(AVFilterContext *filter)
  729. {
  730. AVFilterLink *link = NULL;
  731. int i, j, k;
  732. for (i = 0; i < filter->nb_inputs; i++) {
  733. link = filter->inputs[i];
  734. if (link->type == AVMEDIA_TYPE_AUDIO &&
  735. link->out_channel_layouts->nb_channel_layouts == 1)
  736. break;
  737. }
  738. if (i == filter->nb_inputs)
  739. return;
  740. for (i = 0; i < filter->nb_outputs; i++) {
  741. AVFilterLink *outlink = filter->outputs[i];
  742. int best_idx = -1, best_score = INT_MIN, best_count_diff = INT_MAX;
  743. if (outlink->type != AVMEDIA_TYPE_AUDIO ||
  744. outlink->in_channel_layouts->nb_channel_layouts < 2)
  745. continue;
  746. for (j = 0; j < outlink->in_channel_layouts->nb_channel_layouts; j++) {
  747. uint64_t in_chlayout = link->out_channel_layouts->channel_layouts[0];
  748. uint64_t out_chlayout = outlink->in_channel_layouts->channel_layouts[j];
  749. int in_channels = av_get_channel_layout_nb_channels(in_chlayout);
  750. int out_channels = av_get_channel_layout_nb_channels(out_chlayout);
  751. int count_diff = out_channels - in_channels;
  752. int matched_channels, extra_channels;
  753. int score = 100000;
  754. if (FF_LAYOUT2COUNT(in_chlayout) || FF_LAYOUT2COUNT(out_chlayout)) {
  755. /* Compute score in case the input or output layout encodes
  756. a channel count; in this case the score is not altered by
  757. the computation afterwards, as in_chlayout and
  758. out_chlayout have both been set to 0 */
  759. if (FF_LAYOUT2COUNT(in_chlayout))
  760. in_channels = FF_LAYOUT2COUNT(in_chlayout);
  761. if (FF_LAYOUT2COUNT(out_chlayout))
  762. out_channels = FF_LAYOUT2COUNT(out_chlayout);
  763. score -= 10000 + FFABS(out_channels - in_channels) +
  764. (in_channels > out_channels ? 10000 : 0);
  765. in_chlayout = out_chlayout = 0;
  766. /* Let the remaining computation run, even if the score
  767. value is not altered */
  768. }
  769. /* channel substitution */
  770. for (k = 0; k < FF_ARRAY_ELEMS(ch_subst); k++) {
  771. uint64_t cmp0 = ch_subst[k][0];
  772. uint64_t cmp1 = ch_subst[k][1];
  773. if (( in_chlayout & cmp0) && (!(out_chlayout & cmp0)) &&
  774. (out_chlayout & cmp1) && (!( in_chlayout & cmp1))) {
  775. in_chlayout &= ~cmp0;
  776. out_chlayout &= ~cmp1;
  777. /* add score for channel match, minus a deduction for
  778. having to do the substitution */
  779. score += 10 * av_get_channel_layout_nb_channels(cmp1) - 2;
  780. }
  781. }
  782. /* no penalty for LFE channel mismatch */
  783. if ( (in_chlayout & AV_CH_LOW_FREQUENCY) &&
  784. (out_chlayout & AV_CH_LOW_FREQUENCY))
  785. score += 10;
  786. in_chlayout &= ~AV_CH_LOW_FREQUENCY;
  787. out_chlayout &= ~AV_CH_LOW_FREQUENCY;
  788. matched_channels = av_get_channel_layout_nb_channels(in_chlayout &
  789. out_chlayout);
  790. extra_channels = av_get_channel_layout_nb_channels(out_chlayout &
  791. (~in_chlayout));
  792. score += 10 * matched_channels - 5 * extra_channels;
  793. if (score > best_score ||
  794. (count_diff < best_count_diff && score == best_score)) {
  795. best_score = score;
  796. best_idx = j;
  797. best_count_diff = count_diff;
  798. }
  799. }
  800. av_assert0(best_idx >= 0);
  801. FFSWAP(uint64_t, outlink->in_channel_layouts->channel_layouts[0],
  802. outlink->in_channel_layouts->channel_layouts[best_idx]);
  803. }
  804. }
  805. static void swap_channel_layouts(AVFilterGraph *graph)
  806. {
  807. int i;
  808. for (i = 0; i < graph->nb_filters; i++)
  809. swap_channel_layouts_on_filter(graph->filters[i]);
  810. }
  811. static void swap_sample_fmts_on_filter(AVFilterContext *filter)
  812. {
  813. AVFilterLink *link = NULL;
  814. int format, bps;
  815. int i, j;
  816. for (i = 0; i < filter->nb_inputs; i++) {
  817. link = filter->inputs[i];
  818. if (link->type == AVMEDIA_TYPE_AUDIO &&
  819. link->out_formats->nb_formats == 1)
  820. break;
  821. }
  822. if (i == filter->nb_inputs)
  823. return;
  824. format = link->out_formats->formats[0];
  825. bps = av_get_bytes_per_sample(format);
  826. for (i = 0; i < filter->nb_outputs; i++) {
  827. AVFilterLink *outlink = filter->outputs[i];
  828. int best_idx = -1, best_score = INT_MIN;
  829. if (outlink->type != AVMEDIA_TYPE_AUDIO ||
  830. outlink->in_formats->nb_formats < 2)
  831. continue;
  832. for (j = 0; j < outlink->in_formats->nb_formats; j++) {
  833. int out_format = outlink->in_formats->formats[j];
  834. int out_bps = av_get_bytes_per_sample(out_format);
  835. int score;
  836. if (av_get_packed_sample_fmt(out_format) == format ||
  837. av_get_planar_sample_fmt(out_format) == format) {
  838. best_idx = j;
  839. break;
  840. }
  841. /* for s32 and float prefer double to prevent loss of information */
  842. if (bps == 4 && out_bps == 8) {
  843. best_idx = j;
  844. break;
  845. }
  846. /* prefer closest higher or equal bps */
  847. score = -abs(out_bps - bps);
  848. if (out_bps >= bps)
  849. score += INT_MAX/2;
  850. if (score > best_score) {
  851. best_score = score;
  852. best_idx = j;
  853. }
  854. }
  855. av_assert0(best_idx >= 0);
  856. FFSWAP(int, outlink->in_formats->formats[0],
  857. outlink->in_formats->formats[best_idx]);
  858. }
  859. }
  860. static void swap_sample_fmts(AVFilterGraph *graph)
  861. {
  862. int i;
  863. for (i = 0; i < graph->nb_filters; i++)
  864. swap_sample_fmts_on_filter(graph->filters[i]);
  865. }
  866. static int pick_formats(AVFilterGraph *graph)
  867. {
  868. int i, j, ret;
  869. int change;
  870. do{
  871. change = 0;
  872. for (i = 0; i < graph->nb_filters; i++) {
  873. AVFilterContext *filter = graph->filters[i];
  874. if (filter->nb_inputs){
  875. for (j = 0; j < filter->nb_inputs; j++){
  876. if(filter->inputs[j]->in_formats && filter->inputs[j]->in_formats->nb_formats == 1) {
  877. if ((ret = pick_format(filter->inputs[j], NULL)) < 0)
  878. return ret;
  879. change = 1;
  880. }
  881. }
  882. }
  883. if (filter->nb_outputs){
  884. for (j = 0; j < filter->nb_outputs; j++){
  885. if(filter->outputs[j]->in_formats && filter->outputs[j]->in_formats->nb_formats == 1) {
  886. if ((ret = pick_format(filter->outputs[j], NULL)) < 0)
  887. return ret;
  888. change = 1;
  889. }
  890. }
  891. }
  892. if (filter->nb_inputs && filter->nb_outputs && filter->inputs[0]->format>=0) {
  893. for (j = 0; j < filter->nb_outputs; j++) {
  894. if(filter->outputs[j]->format<0) {
  895. if ((ret = pick_format(filter->outputs[j], filter->inputs[0])) < 0)
  896. return ret;
  897. change = 1;
  898. }
  899. }
  900. }
  901. }
  902. }while(change);
  903. for (i = 0; i < graph->nb_filters; i++) {
  904. AVFilterContext *filter = graph->filters[i];
  905. for (j = 0; j < filter->nb_inputs; j++)
  906. if ((ret = pick_format(filter->inputs[j], NULL)) < 0)
  907. return ret;
  908. for (j = 0; j < filter->nb_outputs; j++)
  909. if ((ret = pick_format(filter->outputs[j], NULL)) < 0)
  910. return ret;
  911. }
  912. return 0;
  913. }
  914. /**
  915. * Configure the formats of all the links in the graph.
  916. */
  917. static int graph_config_formats(AVFilterGraph *graph, AVClass *log_ctx)
  918. {
  919. int ret;
  920. /* find supported formats from sub-filters, and merge along links */
  921. while ((ret = query_formats(graph, log_ctx)) == AVERROR(EAGAIN))
  922. av_log(graph, AV_LOG_DEBUG, "query_formats not finished\n");
  923. if (ret < 0)
  924. return ret;
  925. /* Once everything is merged, it's possible that we'll still have
  926. * multiple valid media format choices. We try to minimize the amount
  927. * of format conversion inside filters */
  928. reduce_formats(graph);
  929. /* for audio filters, ensure the best format, sample rate and channel layout
  930. * is selected */
  931. swap_sample_fmts(graph);
  932. swap_samplerates(graph);
  933. swap_channel_layouts(graph);
  934. if ((ret = pick_formats(graph)) < 0)
  935. return ret;
  936. return 0;
  937. }
  938. static int ff_avfilter_graph_config_pointers(AVFilterGraph *graph,
  939. AVClass *log_ctx)
  940. {
  941. unsigned i, j;
  942. int sink_links_count = 0, n = 0;
  943. AVFilterContext *f;
  944. AVFilterLink **sinks;
  945. for (i = 0; i < graph->nb_filters; i++) {
  946. f = graph->filters[i];
  947. for (j = 0; j < f->nb_inputs; j++) {
  948. f->inputs[j]->graph = graph;
  949. f->inputs[j]->age_index = -1;
  950. }
  951. for (j = 0; j < f->nb_outputs; j++) {
  952. f->outputs[j]->graph = graph;
  953. f->outputs[j]->age_index= -1;
  954. }
  955. if (!f->nb_outputs) {
  956. if (f->nb_inputs > INT_MAX - sink_links_count)
  957. return AVERROR(EINVAL);
  958. sink_links_count += f->nb_inputs;
  959. }
  960. }
  961. sinks = av_calloc(sink_links_count, sizeof(*sinks));
  962. if (!sinks)
  963. return AVERROR(ENOMEM);
  964. for (i = 0; i < graph->nb_filters; i++) {
  965. f = graph->filters[i];
  966. if (!f->nb_outputs) {
  967. for (j = 0; j < f->nb_inputs; j++) {
  968. sinks[n] = f->inputs[j];
  969. f->inputs[j]->age_index = n++;
  970. }
  971. }
  972. }
  973. av_assert0(n == sink_links_count);
  974. graph->sink_links = sinks;
  975. graph->sink_links_count = sink_links_count;
  976. return 0;
  977. }
  978. static int graph_insert_fifos(AVFilterGraph *graph, AVClass *log_ctx)
  979. {
  980. AVFilterContext *f;
  981. int i, j, ret;
  982. int fifo_count = 0;
  983. for (i = 0; i < graph->nb_filters; i++) {
  984. f = graph->filters[i];
  985. for (j = 0; j < f->nb_inputs; j++) {
  986. AVFilterLink *link = f->inputs[j];
  987. AVFilterContext *fifo_ctx;
  988. AVFilter *fifo;
  989. char name[32];
  990. if (!link->dstpad->needs_fifo)
  991. continue;
  992. fifo = f->inputs[j]->type == AVMEDIA_TYPE_VIDEO ?
  993. avfilter_get_by_name("fifo") :
  994. avfilter_get_by_name("afifo");
  995. snprintf(name, sizeof(name), "auto-inserted fifo %d", fifo_count++);
  996. ret = avfilter_graph_create_filter(&fifo_ctx, fifo, name, NULL,
  997. NULL, graph);
  998. if (ret < 0)
  999. return ret;
  1000. ret = avfilter_insert_filter(link, fifo_ctx, 0, 0);
  1001. if (ret < 0)
  1002. return ret;
  1003. }
  1004. }
  1005. return 0;
  1006. }
  1007. int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
  1008. {
  1009. int ret;
  1010. if ((ret = graph_check_validity(graphctx, log_ctx)))
  1011. return ret;
  1012. if ((ret = graph_insert_fifos(graphctx, log_ctx)) < 0)
  1013. return ret;
  1014. if ((ret = graph_config_formats(graphctx, log_ctx)))
  1015. return ret;
  1016. if ((ret = graph_config_links(graphctx, log_ctx)))
  1017. return ret;
  1018. if ((ret = ff_avfilter_graph_config_pointers(graphctx, log_ctx)))
  1019. return ret;
  1020. return 0;
  1021. }
  1022. int avfilter_graph_send_command(AVFilterGraph *graph, const char *target, const char *cmd, const char *arg, char *res, int res_len, int flags)
  1023. {
  1024. int i, r = AVERROR(ENOSYS);
  1025. if (!graph)
  1026. return r;
  1027. if ((flags & AVFILTER_CMD_FLAG_ONE) && !(flags & AVFILTER_CMD_FLAG_FAST)) {
  1028. r = avfilter_graph_send_command(graph, target, cmd, arg, res, res_len, flags | AVFILTER_CMD_FLAG_FAST);
  1029. if (r != AVERROR(ENOSYS))
  1030. return r;
  1031. }
  1032. if (res_len && res)
  1033. res[0] = 0;
  1034. for (i = 0; i < graph->nb_filters; i++) {
  1035. AVFilterContext *filter = graph->filters[i];
  1036. if (!strcmp(target, "all") || (filter->name && !strcmp(target, filter->name)) || !strcmp(target, filter->filter->name)) {
  1037. r = avfilter_process_command(filter, cmd, arg, res, res_len, flags);
  1038. if (r != AVERROR(ENOSYS)) {
  1039. if ((flags & AVFILTER_CMD_FLAG_ONE) || r < 0)
  1040. return r;
  1041. }
  1042. }
  1043. }
  1044. return r;
  1045. }
  1046. int avfilter_graph_queue_command(AVFilterGraph *graph, const char *target, const char *command, const char *arg, int flags, double ts)
  1047. {
  1048. int i;
  1049. if(!graph)
  1050. return 0;
  1051. for (i = 0; i < graph->nb_filters; i++) {
  1052. AVFilterContext *filter = graph->filters[i];
  1053. if(filter && (!strcmp(target, "all") || !strcmp(target, filter->name) || !strcmp(target, filter->filter->name))){
  1054. AVFilterCommand **queue = &filter->command_queue, *next;
  1055. while (*queue && (*queue)->time <= ts)
  1056. queue = &(*queue)->next;
  1057. next = *queue;
  1058. *queue = av_mallocz(sizeof(AVFilterCommand));
  1059. (*queue)->command = av_strdup(command);
  1060. (*queue)->arg = av_strdup(arg);
  1061. (*queue)->time = ts;
  1062. (*queue)->flags = flags;
  1063. (*queue)->next = next;
  1064. if(flags & AVFILTER_CMD_FLAG_ONE)
  1065. return 0;
  1066. }
  1067. }
  1068. return 0;
  1069. }
  1070. static void heap_bubble_up(AVFilterGraph *graph,
  1071. AVFilterLink *link, int index)
  1072. {
  1073. AVFilterLink **links = graph->sink_links;
  1074. while (index) {
  1075. int parent = (index - 1) >> 1;
  1076. if (links[parent]->current_pts >= link->current_pts)
  1077. break;
  1078. links[index] = links[parent];
  1079. links[index]->age_index = index;
  1080. index = parent;
  1081. }
  1082. links[index] = link;
  1083. link->age_index = index;
  1084. }
  1085. static void heap_bubble_down(AVFilterGraph *graph,
  1086. AVFilterLink *link, int index)
  1087. {
  1088. AVFilterLink **links = graph->sink_links;
  1089. while (1) {
  1090. int child = 2 * index + 1;
  1091. if (child >= graph->sink_links_count)
  1092. break;
  1093. if (child + 1 < graph->sink_links_count &&
  1094. links[child + 1]->current_pts < links[child]->current_pts)
  1095. child++;
  1096. if (link->current_pts < links[child]->current_pts)
  1097. break;
  1098. links[index] = links[child];
  1099. links[index]->age_index = index;
  1100. index = child;
  1101. }
  1102. links[index] = link;
  1103. link->age_index = index;
  1104. }
  1105. void ff_avfilter_graph_update_heap(AVFilterGraph *graph, AVFilterLink *link)
  1106. {
  1107. heap_bubble_up (graph, link, link->age_index);
  1108. heap_bubble_down(graph, link, link->age_index);
  1109. }
  1110. int avfilter_graph_request_oldest(AVFilterGraph *graph)
  1111. {
  1112. while (graph->sink_links_count) {
  1113. AVFilterLink *oldest = graph->sink_links[0];
  1114. int r = ff_request_frame(oldest);
  1115. if (r != AVERROR_EOF)
  1116. return r;
  1117. av_log(oldest->dst, AV_LOG_DEBUG, "EOF on sink link %s:%s.\n",
  1118. oldest->dst ? oldest->dst->name : "unknown",
  1119. oldest->dstpad ? oldest->dstpad->name : "unknown");
  1120. /* EOF: remove the link from the heap */
  1121. if (oldest->age_index < --graph->sink_links_count)
  1122. heap_bubble_down(graph, graph->sink_links[graph->sink_links_count],
  1123. oldest->age_index);
  1124. oldest->age_index = -1;
  1125. }
  1126. return AVERROR_EOF;
  1127. }