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.

448 lines
14KB

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