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.

1355 lines
49KB

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