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.

1326 lines
46KB

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