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.

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