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.

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