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.

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