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.

556 lines
18KB

  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 <ctype.h>
  23. #include <string.h>
  24. #include "libavutil/audioconvert.h"
  25. #include "libavutil/avassert.h"
  26. #include "libavutil/pixdesc.h"
  27. #include "avfilter.h"
  28. #include "avfiltergraph.h"
  29. #include "internal.h"
  30. #include "libavutil/log.h"
  31. static const AVClass filtergraph_class = {
  32. .class_name = "AVFilterGraph",
  33. .item_name = av_default_item_name,
  34. .version = LIBAVUTIL_VERSION_INT,
  35. };
  36. AVFilterGraph *avfilter_graph_alloc(void)
  37. {
  38. AVFilterGraph *ret = av_mallocz(sizeof(AVFilterGraph));
  39. if (!ret)
  40. return NULL;
  41. #if FF_API_GRAPH_AVCLASS
  42. ret->av_class = &filtergraph_class;
  43. #endif
  44. return ret;
  45. }
  46. void avfilter_graph_free(AVFilterGraph **graph)
  47. {
  48. if (!*graph)
  49. return;
  50. for (; (*graph)->filter_count > 0; (*graph)->filter_count--)
  51. avfilter_free((*graph)->filters[(*graph)->filter_count - 1]);
  52. av_freep(&(*graph)->sink_links);
  53. av_freep(&(*graph)->scale_sws_opts);
  54. av_freep(&(*graph)->filters);
  55. av_freep(graph);
  56. }
  57. int avfilter_graph_add_filter(AVFilterGraph *graph, AVFilterContext *filter)
  58. {
  59. AVFilterContext **filters = av_realloc(graph->filters,
  60. sizeof(AVFilterContext*) * (graph->filter_count+1));
  61. if (!filters)
  62. return AVERROR(ENOMEM);
  63. graph->filters = filters;
  64. graph->filters[graph->filter_count++] = filter;
  65. return 0;
  66. }
  67. int avfilter_graph_create_filter(AVFilterContext **filt_ctx, AVFilter *filt,
  68. const char *name, const char *args, void *opaque,
  69. AVFilterGraph *graph_ctx)
  70. {
  71. int ret;
  72. if ((ret = avfilter_open(filt_ctx, filt, name)) < 0)
  73. goto fail;
  74. if ((ret = avfilter_init_filter(*filt_ctx, args, opaque)) < 0)
  75. goto fail;
  76. if ((ret = avfilter_graph_add_filter(graph_ctx, *filt_ctx)) < 0)
  77. goto fail;
  78. return 0;
  79. fail:
  80. if (*filt_ctx)
  81. avfilter_free(*filt_ctx);
  82. *filt_ctx = NULL;
  83. return ret;
  84. }
  85. int ff_avfilter_graph_check_validity(AVFilterGraph *graph, AVClass *log_ctx)
  86. {
  87. AVFilterContext *filt;
  88. int i, j;
  89. for (i = 0; i < graph->filter_count; i++) {
  90. filt = graph->filters[i];
  91. for (j = 0; j < filt->input_count; j++) {
  92. if (!filt->inputs[j] || !filt->inputs[j]->src) {
  93. av_log(log_ctx, AV_LOG_ERROR,
  94. "Input pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any source\n",
  95. filt->input_pads[j].name, filt->name, filt->filter->name);
  96. return AVERROR(EINVAL);
  97. }
  98. }
  99. for (j = 0; j < filt->output_count; j++) {
  100. if (!filt->outputs[j] || !filt->outputs[j]->dst) {
  101. av_log(log_ctx, AV_LOG_ERROR,
  102. "Output pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any destination\n",
  103. filt->output_pads[j].name, filt->name, filt->filter->name);
  104. return AVERROR(EINVAL);
  105. }
  106. }
  107. }
  108. return 0;
  109. }
  110. int ff_avfilter_graph_config_links(AVFilterGraph *graph, AVClass *log_ctx)
  111. {
  112. AVFilterContext *filt;
  113. int i, ret;
  114. for (i=0; i < graph->filter_count; i++) {
  115. filt = graph->filters[i];
  116. if (!filt->output_count) {
  117. if ((ret = avfilter_config_links(filt)))
  118. return ret;
  119. }
  120. }
  121. return 0;
  122. }
  123. AVFilterContext *avfilter_graph_get_filter(AVFilterGraph *graph, char *name)
  124. {
  125. int i;
  126. for (i = 0; i < graph->filter_count; i++)
  127. if (graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
  128. return graph->filters[i];
  129. return NULL;
  130. }
  131. static int insert_conv_filter(AVFilterGraph *graph, AVFilterLink *link,
  132. const char *filt_name, const char *filt_args)
  133. {
  134. static int auto_count = 0, ret;
  135. char inst_name[32];
  136. AVFilterContext *filt_ctx;
  137. snprintf(inst_name, sizeof(inst_name), "auto-inserted %s %d",
  138. filt_name, auto_count++);
  139. if ((ret = avfilter_graph_create_filter(&filt_ctx,
  140. avfilter_get_by_name(filt_name),
  141. inst_name, filt_args, NULL, graph)) < 0)
  142. return ret;
  143. if ((ret = avfilter_insert_filter(link, filt_ctx, 0, 0)) < 0)
  144. return ret;
  145. filt_ctx->filter->query_formats(filt_ctx);
  146. if ( ((link = filt_ctx-> inputs[0]) &&
  147. !avfilter_merge_formats(link->in_formats, link->out_formats)) ||
  148. ((link = filt_ctx->outputs[0]) &&
  149. !avfilter_merge_formats(link->in_formats, link->out_formats))
  150. ) {
  151. av_log(NULL, AV_LOG_ERROR,
  152. "Impossible to convert between the formats supported by the filter "
  153. "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
  154. return AVERROR(EINVAL);
  155. }
  156. if (link->type == AVMEDIA_TYPE_AUDIO &&
  157. (((link = filt_ctx-> inputs[0]) &&
  158. (!avfilter_merge_formats(link->in_chlayouts, link->out_chlayouts) ||
  159. !avfilter_merge_formats(link->in_packing, link->out_packing))) ||
  160. ((link = filt_ctx->outputs[0]) &&
  161. (!avfilter_merge_formats(link->in_chlayouts, link->out_chlayouts) ||
  162. !avfilter_merge_formats(link->in_packing, link->out_packing))))
  163. ) {
  164. av_log(NULL, AV_LOG_ERROR,
  165. "Impossible to convert between the channel layouts/packing formats supported by the filter "
  166. "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
  167. return AVERROR(EINVAL);
  168. }
  169. return 0;
  170. }
  171. static int query_formats(AVFilterGraph *graph, AVClass *log_ctx)
  172. {
  173. int i, j, ret;
  174. char filt_args[128];
  175. AVFilterFormats *formats, *chlayouts, *packing;
  176. /* ask all the sub-filters for their supported media formats */
  177. for (i = 0; i < graph->filter_count; i++) {
  178. if (graph->filters[i]->filter->query_formats)
  179. graph->filters[i]->filter->query_formats(graph->filters[i]);
  180. else
  181. avfilter_default_query_formats(graph->filters[i]);
  182. }
  183. /* go through and merge as many format lists as possible */
  184. for (i = 0; i < graph->filter_count; i++) {
  185. AVFilterContext *filter = graph->filters[i];
  186. for (j = 0; j < filter->input_count; j++) {
  187. AVFilterLink *link = filter->inputs[j];
  188. if (!link) continue;
  189. if (!link->in_formats || !link->out_formats)
  190. return AVERROR(EINVAL);
  191. if (link->type == AVMEDIA_TYPE_VIDEO &&
  192. !avfilter_merge_formats(link->in_formats, link->out_formats)) {
  193. /* couldn't merge format lists, auto-insert scale filter */
  194. snprintf(filt_args, sizeof(filt_args), "0:0:%s",
  195. graph->scale_sws_opts);
  196. if (ret = insert_conv_filter(graph, link, "scale", filt_args))
  197. return ret;
  198. }
  199. else if (link->type == AVMEDIA_TYPE_AUDIO) {
  200. if (!link->in_chlayouts || !link->out_chlayouts ||
  201. !link->in_packing || !link->out_packing)
  202. return AVERROR(EINVAL);
  203. /* Merge all three list before checking: that way, in all
  204. * three categories, aconvert will use a common format
  205. * whenever possible. */
  206. formats = avfilter_merge_formats(link->in_formats, link->out_formats);
  207. chlayouts = avfilter_merge_formats(link->in_chlayouts, link->out_chlayouts);
  208. packing = avfilter_merge_formats(link->in_packing, link->out_packing);
  209. if (!formats || !chlayouts || !packing)
  210. if (ret = insert_conv_filter(graph, link, "aconvert", NULL))
  211. return ret;
  212. }
  213. }
  214. }
  215. return 0;
  216. }
  217. static void pick_format(AVFilterLink *link, AVFilterLink *ref)
  218. {
  219. if (!link || !link->in_formats)
  220. return;
  221. if (link->type == AVMEDIA_TYPE_VIDEO) {
  222. if(ref && ref->type == AVMEDIA_TYPE_VIDEO){
  223. int has_alpha= av_pix_fmt_descriptors[ref->format].nb_components % 2 == 0;
  224. enum PixelFormat best= PIX_FMT_NONE;
  225. int i;
  226. for (i=0; i<link->in_formats->format_count; i++) {
  227. enum PixelFormat p = link->in_formats->formats[i];
  228. best= avcodec_find_best_pix_fmt2(best, p, ref->format, has_alpha, NULL);
  229. }
  230. link->in_formats->formats[0] = best;
  231. }
  232. }
  233. link->in_formats->format_count = 1;
  234. link->format = link->in_formats->formats[0];
  235. avfilter_formats_unref(&link->in_formats);
  236. avfilter_formats_unref(&link->out_formats);
  237. if (link->type == AVMEDIA_TYPE_AUDIO) {
  238. link->in_chlayouts->format_count = 1;
  239. link->channel_layout = link->in_chlayouts->formats[0];
  240. avfilter_formats_unref(&link->in_chlayouts);
  241. avfilter_formats_unref(&link->out_chlayouts);
  242. link->in_packing->format_count = 1;
  243. link->planar = link->in_packing->formats[0] == AVFILTER_PLANAR;
  244. avfilter_formats_unref(&link->in_packing);
  245. avfilter_formats_unref(&link->out_packing);
  246. }
  247. }
  248. static int reduce_formats_on_filter(AVFilterContext *filter)
  249. {
  250. int i, j, k, ret = 0;
  251. for (i = 0; i < filter->input_count; i++) {
  252. AVFilterLink *link = filter->inputs[i];
  253. int format = link->out_formats->formats[0];
  254. if (link->out_formats->format_count != 1)
  255. continue;
  256. for (j = 0; j < filter->output_count; j++) {
  257. AVFilterLink *out_link = filter->outputs[j];
  258. AVFilterFormats *fmts = out_link->in_formats;
  259. if (link->type != out_link->type ||
  260. out_link->in_formats->format_count == 1)
  261. continue;
  262. for (k = 0; k < out_link->in_formats->format_count; k++)
  263. if (fmts->formats[k] == format) {
  264. fmts->formats[0] = format;
  265. fmts->format_count = 1;
  266. ret = 1;
  267. break;
  268. }
  269. }
  270. }
  271. return ret;
  272. }
  273. static void reduce_formats(AVFilterGraph *graph)
  274. {
  275. int i, reduced;
  276. do {
  277. reduced = 0;
  278. for (i = 0; i < graph->filter_count; i++)
  279. reduced |= reduce_formats_on_filter(graph->filters[i]);
  280. } while (reduced);
  281. }
  282. static void pick_formats(AVFilterGraph *graph)
  283. {
  284. int i, j;
  285. for (i = 0; i < graph->filter_count; i++) {
  286. AVFilterContext *filter = graph->filters[i];
  287. if (filter->input_count && filter->output_count) {
  288. for (j = 0; j < filter->input_count; j++)
  289. pick_format(filter->inputs[j], NULL);
  290. for (j = 0; j < filter->output_count; j++)
  291. pick_format(filter->outputs[j], filter->inputs[0]);
  292. }
  293. }
  294. for (i = 0; i < graph->filter_count; i++) {
  295. AVFilterContext *filter = graph->filters[i];
  296. if (!(filter->input_count && filter->output_count)) {
  297. for (j = 0; j < filter->input_count; j++)
  298. pick_format(filter->inputs[j], NULL);
  299. for (j = 0; j < filter->output_count; j++)
  300. pick_format(filter->outputs[j], NULL);
  301. }
  302. }
  303. }
  304. int ff_avfilter_graph_config_formats(AVFilterGraph *graph, AVClass *log_ctx)
  305. {
  306. int ret;
  307. /* find supported formats from sub-filters, and merge along links */
  308. if ((ret = query_formats(graph, log_ctx)) < 0)
  309. return ret;
  310. /* Once everything is merged, it's possible that we'll still have
  311. * multiple valid media format choices. We try to minimize the amount
  312. * of format conversion inside filters */
  313. reduce_formats(graph);
  314. pick_formats(graph);
  315. return 0;
  316. }
  317. static int ff_avfilter_graph_config_pointers(AVFilterGraph *graph,
  318. AVClass *log_ctx)
  319. {
  320. unsigned i, j;
  321. int sink_links_count = 0, n = 0;
  322. AVFilterContext *f;
  323. AVFilterLink **sinks;
  324. for (i = 0; i < graph->filter_count; i++) {
  325. f = graph->filters[i];
  326. for (j = 0; j < f->input_count; j++) {
  327. f->inputs[j]->graph = graph;
  328. f->inputs[j]->age_index = -1;
  329. }
  330. for (j = 0; j < f->output_count; j++) {
  331. f->outputs[j]->graph = graph;
  332. f->outputs[j]->age_index= -1;
  333. }
  334. if (!f->output_count) {
  335. if (f->input_count > INT_MAX - sink_links_count)
  336. return AVERROR(EINVAL);
  337. sink_links_count += f->input_count;
  338. }
  339. }
  340. sinks = av_calloc(sink_links_count, sizeof(*sinks));
  341. if (!sinks)
  342. return AVERROR(ENOMEM);
  343. for (i = 0; i < graph->filter_count; i++) {
  344. f = graph->filters[i];
  345. if (!f->output_count) {
  346. for (j = 0; j < f->input_count; j++) {
  347. sinks[n] = f->inputs[j];
  348. f->inputs[j]->age_index = n++;
  349. }
  350. }
  351. }
  352. av_assert0(n == sink_links_count);
  353. graph->sink_links = sinks;
  354. graph->sink_links_count = sink_links_count;
  355. return 0;
  356. }
  357. int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
  358. {
  359. int ret;
  360. if ((ret = ff_avfilter_graph_check_validity(graphctx, log_ctx)))
  361. return ret;
  362. if ((ret = ff_avfilter_graph_config_formats(graphctx, log_ctx)))
  363. return ret;
  364. if ((ret = ff_avfilter_graph_config_links(graphctx, log_ctx)))
  365. return ret;
  366. if ((ret = ff_avfilter_graph_config_pointers(graphctx, log_ctx)))
  367. return ret;
  368. return 0;
  369. }
  370. int avfilter_graph_send_command(AVFilterGraph *graph, const char *target, const char *cmd, const char *arg, char *res, int res_len, int flags)
  371. {
  372. int i, r = AVERROR(ENOSYS);
  373. if(!graph)
  374. return r;
  375. if((flags & AVFILTER_CMD_FLAG_ONE) && !(flags & AVFILTER_CMD_FLAG_FAST)) {
  376. r=avfilter_graph_send_command(graph, target, cmd, arg, res, res_len, flags | AVFILTER_CMD_FLAG_FAST);
  377. if(r != AVERROR(ENOSYS))
  378. return r;
  379. }
  380. if(res_len && res)
  381. res[0]= 0;
  382. for (i = 0; i < graph->filter_count; i++) {
  383. AVFilterContext *filter = graph->filters[i];
  384. if(!strcmp(target, "all") || (filter->name && !strcmp(target, filter->name)) || !strcmp(target, filter->filter->name)){
  385. r = avfilter_process_command(filter, cmd, arg, res, res_len, flags);
  386. if(r != AVERROR(ENOSYS)) {
  387. if((flags & AVFILTER_CMD_FLAG_ONE) || r<0)
  388. return r;
  389. }
  390. }
  391. }
  392. return r;
  393. }
  394. int avfilter_graph_queue_command(AVFilterGraph *graph, const char *target, const char *command, const char *arg, int flags, double ts)
  395. {
  396. int i;
  397. if(!graph)
  398. return 0;
  399. for (i = 0; i < graph->filter_count; i++) {
  400. AVFilterContext *filter = graph->filters[i];
  401. if(filter && (!strcmp(target, "all") || !strcmp(target, filter->name) || !strcmp(target, filter->filter->name))){
  402. AVFilterCommand **que = &filter->command_queue, *next;
  403. while(*que && (*que)->time <= ts)
  404. que = &(*que)->next;
  405. next= *que;
  406. *que= av_mallocz(sizeof(AVFilterCommand));
  407. (*que)->command = av_strdup(command);
  408. (*que)->arg = av_strdup(arg);
  409. (*que)->time = ts;
  410. (*que)->flags = flags;
  411. (*que)->next = next;
  412. if(flags & AVFILTER_CMD_FLAG_ONE)
  413. return 0;
  414. }
  415. }
  416. return 0;
  417. }
  418. static void heap_bubble_up(AVFilterGraph *graph,
  419. AVFilterLink *link, int index)
  420. {
  421. AVFilterLink **links = graph->sink_links;
  422. while (index) {
  423. int parent = (index - 1) >> 1;
  424. if (links[parent]->current_pts >= link->current_pts)
  425. break;
  426. links[index] = links[parent];
  427. links[index]->age_index = index;
  428. index = parent;
  429. }
  430. links[index] = link;
  431. link->age_index = index;
  432. }
  433. static void heap_bubble_down(AVFilterGraph *graph,
  434. AVFilterLink *link, int index)
  435. {
  436. AVFilterLink **links = graph->sink_links;
  437. while (1) {
  438. int child = 2 * index + 1;
  439. if (child >= graph->sink_links_count)
  440. break;
  441. if (child + 1 < graph->sink_links_count &&
  442. links[child + 1]->current_pts < links[child]->current_pts)
  443. child++;
  444. if (link->current_pts < links[child]->current_pts)
  445. break;
  446. links[index] = links[child];
  447. links[index]->age_index = index;
  448. index = child;
  449. }
  450. links[index] = link;
  451. link->age_index = index;
  452. }
  453. void ff_avfilter_graph_update_heap(AVFilterGraph *graph, AVFilterLink *link)
  454. {
  455. heap_bubble_up (graph, link, link->age_index);
  456. heap_bubble_down(graph, link, link->age_index);
  457. }
  458. int avfilter_graph_request_oldest(AVFilterGraph *graph)
  459. {
  460. while (graph->sink_links_count) {
  461. AVFilterLink *oldest = graph->sink_links[0];
  462. int r = avfilter_request_frame(oldest);
  463. if (r != AVERROR_EOF)
  464. return r;
  465. /* EOF: remove the link from the heap */
  466. if (oldest->age_index < --graph->sink_links_count)
  467. heap_bubble_down(graph, graph->sink_links[graph->sink_links_count],
  468. oldest->age_index);
  469. oldest->age_index = -1;
  470. }
  471. return AVERROR_EOF;
  472. }