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.

579 lines
19KB

  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. int change;
  286. do{
  287. change = 0;
  288. for (i = 0; i < graph->filter_count; i++) {
  289. AVFilterContext *filter = graph->filters[i];
  290. if (filter->input_count){
  291. for (j = 0; j < filter->input_count; j++){
  292. if(filter->inputs[j]->in_formats && filter->inputs[j]->in_formats->format_count == 1) {
  293. pick_format(filter->inputs[j], NULL);
  294. change = 1;
  295. }
  296. }
  297. }
  298. if (filter->output_count){
  299. for (j = 0; j < filter->output_count; j++){
  300. if(filter->outputs[j]->in_formats && filter->outputs[j]->in_formats->format_count == 1) {
  301. pick_format(filter->outputs[j], NULL);
  302. change = 1;
  303. }
  304. }
  305. }
  306. if (filter->input_count && filter->output_count && filter->inputs[0]->format>=0) {
  307. for (j = 0; j < filter->output_count; j++) {
  308. if(filter->outputs[j]->format<0) {
  309. pick_format(filter->outputs[j], filter->inputs[0]);
  310. change = 1;
  311. }
  312. }
  313. }
  314. }
  315. }while(change);
  316. for (i = 0; i < graph->filter_count; i++) {
  317. AVFilterContext *filter = graph->filters[i];
  318. if (1) {
  319. for (j = 0; j < filter->input_count; j++)
  320. pick_format(filter->inputs[j], NULL);
  321. for (j = 0; j < filter->output_count; j++)
  322. pick_format(filter->outputs[j], NULL);
  323. }
  324. }
  325. }
  326. int ff_avfilter_graph_config_formats(AVFilterGraph *graph, AVClass *log_ctx)
  327. {
  328. int ret;
  329. /* find supported formats from sub-filters, and merge along links */
  330. if ((ret = query_formats(graph, log_ctx)) < 0)
  331. return ret;
  332. /* Once everything is merged, it's possible that we'll still have
  333. * multiple valid media format choices. We try to minimize the amount
  334. * of format conversion inside filters */
  335. reduce_formats(graph);
  336. pick_formats(graph);
  337. return 0;
  338. }
  339. static int ff_avfilter_graph_config_pointers(AVFilterGraph *graph,
  340. AVClass *log_ctx)
  341. {
  342. unsigned i, j;
  343. int sink_links_count = 0, n = 0;
  344. AVFilterContext *f;
  345. AVFilterLink **sinks;
  346. for (i = 0; i < graph->filter_count; i++) {
  347. f = graph->filters[i];
  348. for (j = 0; j < f->input_count; j++) {
  349. f->inputs[j]->graph = graph;
  350. f->inputs[j]->age_index = -1;
  351. }
  352. for (j = 0; j < f->output_count; j++) {
  353. f->outputs[j]->graph = graph;
  354. f->outputs[j]->age_index= -1;
  355. }
  356. if (!f->output_count) {
  357. if (f->input_count > INT_MAX - sink_links_count)
  358. return AVERROR(EINVAL);
  359. sink_links_count += f->input_count;
  360. }
  361. }
  362. sinks = av_calloc(sink_links_count, sizeof(*sinks));
  363. if (!sinks)
  364. return AVERROR(ENOMEM);
  365. for (i = 0; i < graph->filter_count; i++) {
  366. f = graph->filters[i];
  367. if (!f->output_count) {
  368. for (j = 0; j < f->input_count; j++) {
  369. sinks[n] = f->inputs[j];
  370. f->inputs[j]->age_index = n++;
  371. }
  372. }
  373. }
  374. av_assert0(n == sink_links_count);
  375. graph->sink_links = sinks;
  376. graph->sink_links_count = sink_links_count;
  377. return 0;
  378. }
  379. int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
  380. {
  381. int ret;
  382. if ((ret = ff_avfilter_graph_check_validity(graphctx, log_ctx)))
  383. return ret;
  384. if ((ret = ff_avfilter_graph_config_formats(graphctx, log_ctx)))
  385. return ret;
  386. if ((ret = ff_avfilter_graph_config_links(graphctx, log_ctx)))
  387. return ret;
  388. if ((ret = ff_avfilter_graph_config_pointers(graphctx, log_ctx)))
  389. return ret;
  390. return 0;
  391. }
  392. int avfilter_graph_send_command(AVFilterGraph *graph, const char *target, const char *cmd, const char *arg, char *res, int res_len, int flags)
  393. {
  394. int i, r = AVERROR(ENOSYS);
  395. if(!graph)
  396. return r;
  397. if((flags & AVFILTER_CMD_FLAG_ONE) && !(flags & AVFILTER_CMD_FLAG_FAST)) {
  398. r=avfilter_graph_send_command(graph, target, cmd, arg, res, res_len, flags | AVFILTER_CMD_FLAG_FAST);
  399. if(r != AVERROR(ENOSYS))
  400. return r;
  401. }
  402. if(res_len && res)
  403. res[0]= 0;
  404. for (i = 0; i < graph->filter_count; i++) {
  405. AVFilterContext *filter = graph->filters[i];
  406. if(!strcmp(target, "all") || (filter->name && !strcmp(target, filter->name)) || !strcmp(target, filter->filter->name)){
  407. r = avfilter_process_command(filter, cmd, arg, res, res_len, flags);
  408. if(r != AVERROR(ENOSYS)) {
  409. if((flags & AVFILTER_CMD_FLAG_ONE) || r<0)
  410. return r;
  411. }
  412. }
  413. }
  414. return r;
  415. }
  416. int avfilter_graph_queue_command(AVFilterGraph *graph, const char *target, const char *command, const char *arg, int flags, double ts)
  417. {
  418. int i;
  419. if(!graph)
  420. return 0;
  421. for (i = 0; i < graph->filter_count; i++) {
  422. AVFilterContext *filter = graph->filters[i];
  423. if(filter && (!strcmp(target, "all") || !strcmp(target, filter->name) || !strcmp(target, filter->filter->name))){
  424. AVFilterCommand **que = &filter->command_queue, *next;
  425. while(*que && (*que)->time <= ts)
  426. que = &(*que)->next;
  427. next= *que;
  428. *que= av_mallocz(sizeof(AVFilterCommand));
  429. (*que)->command = av_strdup(command);
  430. (*que)->arg = av_strdup(arg);
  431. (*que)->time = ts;
  432. (*que)->flags = flags;
  433. (*que)->next = next;
  434. if(flags & AVFILTER_CMD_FLAG_ONE)
  435. return 0;
  436. }
  437. }
  438. return 0;
  439. }
  440. static void heap_bubble_up(AVFilterGraph *graph,
  441. AVFilterLink *link, int index)
  442. {
  443. AVFilterLink **links = graph->sink_links;
  444. while (index) {
  445. int parent = (index - 1) >> 1;
  446. if (links[parent]->current_pts >= link->current_pts)
  447. break;
  448. links[index] = links[parent];
  449. links[index]->age_index = index;
  450. index = parent;
  451. }
  452. links[index] = link;
  453. link->age_index = index;
  454. }
  455. static void heap_bubble_down(AVFilterGraph *graph,
  456. AVFilterLink *link, int index)
  457. {
  458. AVFilterLink **links = graph->sink_links;
  459. while (1) {
  460. int child = 2 * index + 1;
  461. if (child >= graph->sink_links_count)
  462. break;
  463. if (child + 1 < graph->sink_links_count &&
  464. links[child + 1]->current_pts < links[child]->current_pts)
  465. child++;
  466. if (link->current_pts < links[child]->current_pts)
  467. break;
  468. links[index] = links[child];
  469. links[index]->age_index = index;
  470. index = child;
  471. }
  472. links[index] = link;
  473. link->age_index = index;
  474. }
  475. void ff_avfilter_graph_update_heap(AVFilterGraph *graph, AVFilterLink *link)
  476. {
  477. heap_bubble_up (graph, link, link->age_index);
  478. heap_bubble_down(graph, link, link->age_index);
  479. }
  480. int avfilter_graph_request_oldest(AVFilterGraph *graph)
  481. {
  482. while (graph->sink_links_count) {
  483. AVFilterLink *oldest = graph->sink_links[0];
  484. int r = avfilter_request_frame(oldest);
  485. if (r != AVERROR_EOF)
  486. return r;
  487. /* EOF: remove the link from the heap */
  488. if (oldest->age_index < --graph->sink_links_count)
  489. heap_bubble_down(graph, graph->sink_links[graph->sink_links_count],
  490. oldest->age_index);
  491. oldest->age_index = -1;
  492. }
  493. return AVERROR_EOF;
  494. }