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.

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