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.

797 lines
23KB

  1. /*
  2. * filter layer
  3. * Copyright (c) 2007 Bobby Bingham
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/avstring.h"
  22. #include "libavutil/channel_layout.h"
  23. #include "libavutil/common.h"
  24. #include "libavutil/imgutils.h"
  25. #include "libavutil/internal.h"
  26. #include "libavutil/opt.h"
  27. #include "libavutil/pixdesc.h"
  28. #include "libavutil/rational.h"
  29. #include "libavutil/samplefmt.h"
  30. #include "audio.h"
  31. #include "avfilter.h"
  32. #include "formats.h"
  33. #include "internal.h"
  34. #include "video.h"
  35. unsigned avfilter_version(void)
  36. {
  37. return LIBAVFILTER_VERSION_INT;
  38. }
  39. const char *avfilter_configuration(void)
  40. {
  41. return LIBAV_CONFIGURATION;
  42. }
  43. const char *avfilter_license(void)
  44. {
  45. #define LICENSE_PREFIX "libavfilter license: "
  46. return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  47. }
  48. void ff_insert_pad(unsigned idx, unsigned *count, size_t padidx_off,
  49. AVFilterPad **pads, AVFilterLink ***links,
  50. AVFilterPad *newpad)
  51. {
  52. unsigned i;
  53. idx = FFMIN(idx, *count);
  54. *pads = av_realloc(*pads, sizeof(AVFilterPad) * (*count + 1));
  55. *links = av_realloc(*links, sizeof(AVFilterLink*) * (*count + 1));
  56. memmove(*pads + idx + 1, *pads + idx, sizeof(AVFilterPad) * (*count - idx));
  57. memmove(*links + idx + 1, *links + idx, sizeof(AVFilterLink*) * (*count - idx));
  58. memcpy(*pads + idx, newpad, sizeof(AVFilterPad));
  59. (*links)[idx] = NULL;
  60. (*count)++;
  61. for (i = idx + 1; i < *count; i++)
  62. if (*links[i])
  63. (*(unsigned *)((uint8_t *) *links[i] + padidx_off))++;
  64. }
  65. int avfilter_link(AVFilterContext *src, unsigned srcpad,
  66. AVFilterContext *dst, unsigned dstpad)
  67. {
  68. AVFilterLink *link;
  69. if (src->nb_outputs <= srcpad || dst->nb_inputs <= dstpad ||
  70. src->outputs[srcpad] || dst->inputs[dstpad])
  71. return -1;
  72. if (src->output_pads[srcpad].type != dst->input_pads[dstpad].type) {
  73. av_log(src, AV_LOG_ERROR,
  74. "Media type mismatch between the '%s' filter output pad %d and the '%s' filter input pad %d\n",
  75. src->name, srcpad, dst->name, dstpad);
  76. return AVERROR(EINVAL);
  77. }
  78. link = av_mallocz(sizeof(*link));
  79. if (!link)
  80. return AVERROR(ENOMEM);
  81. src->outputs[srcpad] = dst->inputs[dstpad] = link;
  82. link->src = src;
  83. link->dst = dst;
  84. link->srcpad = &src->output_pads[srcpad];
  85. link->dstpad = &dst->input_pads[dstpad];
  86. link->type = src->output_pads[srcpad].type;
  87. assert(AV_PIX_FMT_NONE == -1 && AV_SAMPLE_FMT_NONE == -1);
  88. link->format = -1;
  89. return 0;
  90. }
  91. int avfilter_insert_filter(AVFilterLink *link, AVFilterContext *filt,
  92. unsigned filt_srcpad_idx, unsigned filt_dstpad_idx)
  93. {
  94. int ret;
  95. unsigned dstpad_idx = link->dstpad - link->dst->input_pads;
  96. av_log(link->dst, AV_LOG_VERBOSE, "auto-inserting filter '%s' "
  97. "between the filter '%s' and the filter '%s'\n",
  98. filt->name, link->src->name, link->dst->name);
  99. link->dst->inputs[dstpad_idx] = NULL;
  100. if ((ret = avfilter_link(filt, filt_dstpad_idx, link->dst, dstpad_idx)) < 0) {
  101. /* failed to link output filter to new filter */
  102. link->dst->inputs[dstpad_idx] = link;
  103. return ret;
  104. }
  105. /* re-hookup the link to the new destination filter we inserted */
  106. link->dst = filt;
  107. link->dstpad = &filt->input_pads[filt_srcpad_idx];
  108. filt->inputs[filt_srcpad_idx] = link;
  109. /* if any information on supported media formats already exists on the
  110. * link, we need to preserve that */
  111. if (link->out_formats)
  112. ff_formats_changeref(&link->out_formats,
  113. &filt->outputs[filt_dstpad_idx]->out_formats);
  114. if (link->out_samplerates)
  115. ff_formats_changeref(&link->out_samplerates,
  116. &filt->outputs[filt_dstpad_idx]->out_samplerates);
  117. if (link->out_channel_layouts)
  118. ff_channel_layouts_changeref(&link->out_channel_layouts,
  119. &filt->outputs[filt_dstpad_idx]->out_channel_layouts);
  120. return 0;
  121. }
  122. int avfilter_config_links(AVFilterContext *filter)
  123. {
  124. int (*config_link)(AVFilterLink *);
  125. unsigned i;
  126. int ret;
  127. for (i = 0; i < filter->nb_inputs; i ++) {
  128. AVFilterLink *link = filter->inputs[i];
  129. if (!link) continue;
  130. switch (link->init_state) {
  131. case AVLINK_INIT:
  132. continue;
  133. case AVLINK_STARTINIT:
  134. av_log(filter, AV_LOG_INFO, "circular filter chain detected\n");
  135. return 0;
  136. case AVLINK_UNINIT:
  137. link->init_state = AVLINK_STARTINIT;
  138. if ((ret = avfilter_config_links(link->src)) < 0)
  139. return ret;
  140. if (!(config_link = link->srcpad->config_props)) {
  141. if (link->src->nb_inputs != 1) {
  142. av_log(link->src, AV_LOG_ERROR, "Source filters and filters "
  143. "with more than one input "
  144. "must set config_props() "
  145. "callbacks on all outputs\n");
  146. return AVERROR(EINVAL);
  147. }
  148. } else if ((ret = config_link(link)) < 0) {
  149. av_log(link->src, AV_LOG_ERROR,
  150. "Failed to configure output pad on %s\n",
  151. link->src->name);
  152. return ret;
  153. }
  154. if (link->time_base.num == 0 && link->time_base.den == 0)
  155. link->time_base = link->src && link->src->nb_inputs ?
  156. link->src->inputs[0]->time_base : AV_TIME_BASE_Q;
  157. if (link->type == AVMEDIA_TYPE_VIDEO) {
  158. if (!link->sample_aspect_ratio.num && !link->sample_aspect_ratio.den)
  159. link->sample_aspect_ratio = link->src->nb_inputs ?
  160. link->src->inputs[0]->sample_aspect_ratio : (AVRational){1,1};
  161. if (link->src->nb_inputs) {
  162. if (!link->w)
  163. link->w = link->src->inputs[0]->w;
  164. if (!link->h)
  165. link->h = link->src->inputs[0]->h;
  166. } else if (!link->w || !link->h) {
  167. av_log(link->src, AV_LOG_ERROR,
  168. "Video source filters must set their output link's "
  169. "width and height\n");
  170. return AVERROR(EINVAL);
  171. }
  172. }
  173. if ((config_link = link->dstpad->config_props))
  174. if ((ret = config_link(link)) < 0) {
  175. av_log(link->dst, AV_LOG_ERROR,
  176. "Failed to configure input pad on %s\n",
  177. link->dst->name);
  178. return ret;
  179. }
  180. link->init_state = AVLINK_INIT;
  181. }
  182. }
  183. return 0;
  184. }
  185. void ff_dlog_link(void *ctx, AVFilterLink *link, int end)
  186. {
  187. if (link->type == AVMEDIA_TYPE_VIDEO) {
  188. av_dlog(ctx,
  189. "link[%p s:%dx%d fmt:%-16s %-16s->%-16s]%s",
  190. link, link->w, link->h,
  191. av_get_pix_fmt_name(link->format),
  192. link->src ? link->src->filter->name : "",
  193. link->dst ? link->dst->filter->name : "",
  194. end ? "\n" : "");
  195. } else {
  196. char buf[128];
  197. av_get_channel_layout_string(buf, sizeof(buf), -1, link->channel_layout);
  198. av_dlog(ctx,
  199. "link[%p r:%d cl:%s fmt:%-16s %-16s->%-16s]%s",
  200. link, link->sample_rate, buf,
  201. av_get_sample_fmt_name(link->format),
  202. link->src ? link->src->filter->name : "",
  203. link->dst ? link->dst->filter->name : "",
  204. end ? "\n" : "");
  205. }
  206. }
  207. int ff_request_frame(AVFilterLink *link)
  208. {
  209. FF_DPRINTF_START(NULL, request_frame); ff_dlog_link(NULL, link, 1);
  210. if (link->srcpad->request_frame)
  211. return link->srcpad->request_frame(link);
  212. else if (link->src->inputs[0])
  213. return ff_request_frame(link->src->inputs[0]);
  214. else return -1;
  215. }
  216. int ff_poll_frame(AVFilterLink *link)
  217. {
  218. int i, min = INT_MAX;
  219. if (link->srcpad->poll_frame)
  220. return link->srcpad->poll_frame(link);
  221. for (i = 0; i < link->src->nb_inputs; i++) {
  222. int val;
  223. if (!link->src->inputs[i])
  224. return -1;
  225. val = ff_poll_frame(link->src->inputs[i]);
  226. min = FFMIN(min, val);
  227. }
  228. return min;
  229. }
  230. static AVFilter *first_filter;
  231. #if !FF_API_NOCONST_GET_NAME
  232. const
  233. #endif
  234. AVFilter *avfilter_get_by_name(const char *name)
  235. {
  236. const AVFilter *f = NULL;
  237. if (!name)
  238. return NULL;
  239. while ((f = avfilter_next(f)))
  240. if (!strcmp(f->name, name))
  241. return f;
  242. return NULL;
  243. }
  244. int avfilter_register(AVFilter *filter)
  245. {
  246. AVFilter **f = &first_filter;
  247. while (*f)
  248. f = &(*f)->next;
  249. *f = filter;
  250. filter->next = NULL;
  251. return 0;
  252. }
  253. const AVFilter *avfilter_next(const AVFilter *prev)
  254. {
  255. return prev ? prev->next : first_filter;
  256. }
  257. #if FF_API_OLD_FILTER_REGISTER
  258. AVFilter **av_filter_next(AVFilter **filter)
  259. {
  260. return filter ? &(*filter)->next : &first_filter;
  261. }
  262. void avfilter_uninit(void)
  263. {
  264. }
  265. #endif
  266. int avfilter_pad_count(const AVFilterPad *pads)
  267. {
  268. int count;
  269. if (!pads)
  270. return 0;
  271. for (count = 0; pads->name; count++)
  272. pads++;
  273. return count;
  274. }
  275. static const char *filter_name(void *p)
  276. {
  277. AVFilterContext *filter = p;
  278. return filter->filter->name;
  279. }
  280. static void *filter_child_next(void *obj, void *prev)
  281. {
  282. AVFilterContext *ctx = obj;
  283. if (!prev && ctx->filter && ctx->filter->priv_class && ctx->priv)
  284. return ctx->priv;
  285. return NULL;
  286. }
  287. static const AVClass *filter_child_class_next(const AVClass *prev)
  288. {
  289. const AVFilter *f = NULL;
  290. while (prev && (f = avfilter_next(f)))
  291. if (f->priv_class == prev)
  292. break;
  293. while ((f = avfilter_next(f)))
  294. if (f->priv_class)
  295. return f->priv_class;
  296. return NULL;
  297. }
  298. #define OFFSET(x) offsetof(AVFilterContext, x)
  299. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM
  300. static const AVOption avfilter_options[] = {
  301. { "thread_type", "Allowed thread types", OFFSET(thread_type), AV_OPT_TYPE_FLAGS,
  302. { .i64 = AVFILTER_THREAD_SLICE }, 0, INT_MAX, FLAGS, "thread_type" },
  303. { "slice", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AVFILTER_THREAD_SLICE }, .unit = "thread_type" },
  304. { NULL },
  305. };
  306. static const AVClass avfilter_class = {
  307. .class_name = "AVFilter",
  308. .item_name = filter_name,
  309. .version = LIBAVUTIL_VERSION_INT,
  310. .child_next = filter_child_next,
  311. .child_class_next = filter_child_class_next,
  312. .option = avfilter_options,
  313. };
  314. static int default_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg,
  315. int *ret, int nb_jobs)
  316. {
  317. int i;
  318. for (i = 0; i < nb_jobs; i++) {
  319. int r = func(ctx, arg, i, nb_jobs);
  320. if (ret)
  321. ret[i] = r;
  322. }
  323. return 0;
  324. }
  325. AVFilterContext *ff_filter_alloc(const AVFilter *filter, const char *inst_name)
  326. {
  327. AVFilterContext *ret;
  328. if (!filter)
  329. return NULL;
  330. ret = av_mallocz(sizeof(AVFilterContext));
  331. if (!ret)
  332. return NULL;
  333. ret->av_class = &avfilter_class;
  334. ret->filter = filter;
  335. ret->name = inst_name ? av_strdup(inst_name) : NULL;
  336. if (filter->priv_size) {
  337. ret->priv = av_mallocz(filter->priv_size);
  338. if (!ret->priv)
  339. goto err;
  340. }
  341. av_opt_set_defaults(ret);
  342. if (filter->priv_class) {
  343. *(const AVClass**)ret->priv = filter->priv_class;
  344. av_opt_set_defaults(ret->priv);
  345. }
  346. ret->internal = av_mallocz(sizeof(*ret->internal));
  347. if (!ret->internal)
  348. goto err;
  349. ret->internal->execute = default_execute;
  350. ret->nb_inputs = avfilter_pad_count(filter->inputs);
  351. if (ret->nb_inputs ) {
  352. ret->input_pads = av_malloc(sizeof(AVFilterPad) * ret->nb_inputs);
  353. if (!ret->input_pads)
  354. goto err;
  355. memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad) * ret->nb_inputs);
  356. ret->inputs = av_mallocz(sizeof(AVFilterLink*) * ret->nb_inputs);
  357. if (!ret->inputs)
  358. goto err;
  359. }
  360. ret->nb_outputs = avfilter_pad_count(filter->outputs);
  361. if (ret->nb_outputs) {
  362. ret->output_pads = av_malloc(sizeof(AVFilterPad) * ret->nb_outputs);
  363. if (!ret->output_pads)
  364. goto err;
  365. memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad) * ret->nb_outputs);
  366. ret->outputs = av_mallocz(sizeof(AVFilterLink*) * ret->nb_outputs);
  367. if (!ret->outputs)
  368. goto err;
  369. }
  370. #if FF_API_FOO_COUNT
  371. FF_DISABLE_DEPRECATION_WARNINGS
  372. ret->output_count = ret->nb_outputs;
  373. ret->input_count = ret->nb_inputs;
  374. FF_ENABLE_DEPRECATION_WARNINGS
  375. #endif
  376. return ret;
  377. err:
  378. av_freep(&ret->inputs);
  379. av_freep(&ret->input_pads);
  380. ret->nb_inputs = 0;
  381. av_freep(&ret->outputs);
  382. av_freep(&ret->output_pads);
  383. ret->nb_outputs = 0;
  384. av_freep(&ret->priv);
  385. av_freep(&ret->internal);
  386. av_free(ret);
  387. return NULL;
  388. }
  389. #if FF_API_AVFILTER_OPEN
  390. int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *inst_name)
  391. {
  392. *filter_ctx = ff_filter_alloc(filter, inst_name);
  393. return *filter_ctx ? 0 : AVERROR(ENOMEM);
  394. }
  395. #endif
  396. static void free_link(AVFilterLink *link)
  397. {
  398. if (!link)
  399. return;
  400. if (link->src)
  401. link->src->outputs[link->srcpad - link->src->output_pads] = NULL;
  402. if (link->dst)
  403. link->dst->inputs[link->dstpad - link->dst->input_pads] = NULL;
  404. ff_formats_unref(&link->in_formats);
  405. ff_formats_unref(&link->out_formats);
  406. ff_formats_unref(&link->in_samplerates);
  407. ff_formats_unref(&link->out_samplerates);
  408. ff_channel_layouts_unref(&link->in_channel_layouts);
  409. ff_channel_layouts_unref(&link->out_channel_layouts);
  410. av_freep(&link);
  411. }
  412. void avfilter_free(AVFilterContext *filter)
  413. {
  414. int i;
  415. if (filter->graph)
  416. ff_filter_graph_remove_filter(filter->graph, filter);
  417. if (filter->filter->uninit)
  418. filter->filter->uninit(filter);
  419. for (i = 0; i < filter->nb_inputs; i++) {
  420. free_link(filter->inputs[i]);
  421. }
  422. for (i = 0; i < filter->nb_outputs; i++) {
  423. free_link(filter->outputs[i]);
  424. }
  425. if (filter->filter->priv_class)
  426. av_opt_free(filter->priv);
  427. av_freep(&filter->name);
  428. av_freep(&filter->input_pads);
  429. av_freep(&filter->output_pads);
  430. av_freep(&filter->inputs);
  431. av_freep(&filter->outputs);
  432. av_freep(&filter->priv);
  433. av_freep(&filter->internal);
  434. av_free(filter);
  435. }
  436. /* process a list of value1:value2:..., each value corresponding
  437. * to subsequent AVOption, in the order they are declared */
  438. static int process_unnamed_options(AVFilterContext *ctx, AVDictionary **options,
  439. const char *args)
  440. {
  441. const AVOption *o = NULL;
  442. const char *p = args;
  443. char *val;
  444. while (*p) {
  445. o = av_opt_next(ctx->priv, o);
  446. if (!o) {
  447. av_log(ctx, AV_LOG_ERROR, "More options provided than "
  448. "this filter supports.\n");
  449. return AVERROR(EINVAL);
  450. }
  451. if (o->type == AV_OPT_TYPE_CONST)
  452. continue;
  453. val = av_get_token(&p, ":");
  454. if (!val)
  455. return AVERROR(ENOMEM);
  456. av_dict_set(options, o->name, val, 0);
  457. av_freep(&val);
  458. if (*p)
  459. p++;
  460. }
  461. return 0;
  462. }
  463. #if FF_API_AVFILTER_INIT_FILTER
  464. int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
  465. {
  466. return avfilter_init_str(filter, args);
  467. }
  468. #endif
  469. int avfilter_init_dict(AVFilterContext *ctx, AVDictionary **options)
  470. {
  471. int ret = 0;
  472. ret = av_opt_set_dict(ctx, options);
  473. if (ret < 0) {
  474. av_log(ctx, AV_LOG_ERROR, "Error applying generic filter options.\n");
  475. return ret;
  476. }
  477. if (ctx->filter->flags & AVFILTER_FLAG_SLICE_THREADS &&
  478. ctx->thread_type & ctx->graph->thread_type & AVFILTER_THREAD_SLICE &&
  479. ctx->graph->internal->thread_execute) {
  480. ctx->thread_type = AVFILTER_THREAD_SLICE;
  481. ctx->internal->execute = ctx->graph->internal->thread_execute;
  482. } else {
  483. ctx->thread_type = 0;
  484. }
  485. if (ctx->filter->priv_class) {
  486. ret = av_opt_set_dict(ctx->priv, options);
  487. if (ret < 0) {
  488. av_log(ctx, AV_LOG_ERROR, "Error applying options to the filter.\n");
  489. return ret;
  490. }
  491. }
  492. if (ctx->filter->init)
  493. ret = ctx->filter->init(ctx);
  494. else if (ctx->filter->init_dict)
  495. ret = ctx->filter->init_dict(ctx, options);
  496. return ret;
  497. }
  498. int avfilter_init_str(AVFilterContext *filter, const char *args)
  499. {
  500. AVDictionary *options = NULL;
  501. AVDictionaryEntry *e;
  502. int ret = 0;
  503. if (args && *args) {
  504. if (!filter->filter->priv_class) {
  505. av_log(filter, AV_LOG_ERROR, "This filter does not take any "
  506. "options, but options were provided: %s.\n", args);
  507. return AVERROR(EINVAL);
  508. }
  509. #if FF_API_OLD_FILTER_OPTS
  510. if (!strcmp(filter->filter->name, "scale") &&
  511. strchr(args, ':') && strchr(args, ':') < strchr(args, '=')) {
  512. /* old w:h:flags=<flags> syntax */
  513. char *copy = av_strdup(args);
  514. char *p;
  515. av_log(filter, AV_LOG_WARNING, "The <w>:<h>:flags=<flags> option "
  516. "syntax is deprecated. Use either <w>:<h>:<flags> or "
  517. "w=<w>:h=<h>:flags=<flags>.\n");
  518. if (!copy) {
  519. ret = AVERROR(ENOMEM);
  520. goto fail;
  521. }
  522. p = strrchr(copy, ':');
  523. if (p) {
  524. *p++ = 0;
  525. ret = av_dict_parse_string(&options, p, "=", ":", 0);
  526. }
  527. if (ret >= 0)
  528. ret = process_unnamed_options(filter, &options, copy);
  529. av_freep(&copy);
  530. if (ret < 0)
  531. goto fail;
  532. } else
  533. #endif
  534. if (strchr(args, '=')) {
  535. /* assume a list of key1=value1:key2=value2:... */
  536. ret = av_dict_parse_string(&options, args, "=", ":", 0);
  537. if (ret < 0)
  538. goto fail;
  539. #if FF_API_OLD_FILTER_OPTS
  540. } else if (!strcmp(filter->filter->name, "format") ||
  541. !strcmp(filter->filter->name, "noformat") ||
  542. !strcmp(filter->filter->name, "frei0r") ||
  543. !strcmp(filter->filter->name, "frei0r_src") ||
  544. !strcmp(filter->filter->name, "ocv")) {
  545. /* a hack for compatibility with the old syntax
  546. * replace colons with |s */
  547. char *copy = av_strdup(args);
  548. char *p = copy;
  549. int nb_leading = 0; // number of leading colons to skip
  550. if (!copy) {
  551. ret = AVERROR(ENOMEM);
  552. goto fail;
  553. }
  554. if (!strcmp(filter->filter->name, "frei0r") ||
  555. !strcmp(filter->filter->name, "ocv"))
  556. nb_leading = 1;
  557. else if (!strcmp(filter->filter->name, "frei0r_src"))
  558. nb_leading = 3;
  559. while (nb_leading--) {
  560. p = strchr(p, ':');
  561. if (!p) {
  562. p = copy + strlen(copy);
  563. break;
  564. }
  565. p++;
  566. }
  567. if (strchr(p, ':')) {
  568. av_log(filter, AV_LOG_WARNING, "This syntax is deprecated. Use "
  569. "'|' to separate the list items.\n");
  570. }
  571. while ((p = strchr(p, ':')))
  572. *p++ = '|';
  573. ret = process_unnamed_options(filter, &options, copy);
  574. av_freep(&copy);
  575. if (ret < 0)
  576. goto fail;
  577. #endif
  578. } else {
  579. ret = process_unnamed_options(filter, &options, args);
  580. if (ret < 0)
  581. goto fail;
  582. }
  583. }
  584. ret = avfilter_init_dict(filter, &options);
  585. if (ret < 0)
  586. goto fail;
  587. if ((e = av_dict_get(options, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
  588. av_log(filter, AV_LOG_ERROR, "No such option: %s.\n", e->key);
  589. ret = AVERROR_OPTION_NOT_FOUND;
  590. goto fail;
  591. }
  592. fail:
  593. av_dict_free(&options);
  594. return ret;
  595. }
  596. const char *avfilter_pad_get_name(const AVFilterPad *pads, int pad_idx)
  597. {
  598. return pads[pad_idx].name;
  599. }
  600. enum AVMediaType avfilter_pad_get_type(const AVFilterPad *pads, int pad_idx)
  601. {
  602. return pads[pad_idx].type;
  603. }
  604. static int default_filter_frame(AVFilterLink *link, AVFrame *frame)
  605. {
  606. return ff_filter_frame(link->dst->outputs[0], frame);
  607. }
  608. int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
  609. {
  610. int (*filter_frame)(AVFilterLink *, AVFrame *);
  611. AVFilterPad *dst = link->dstpad;
  612. AVFrame *out = NULL;
  613. int ret;
  614. FF_DPRINTF_START(NULL, filter_frame);
  615. ff_dlog_link(NULL, link, 1);
  616. if (!(filter_frame = dst->filter_frame))
  617. filter_frame = default_filter_frame;
  618. /* copy the frame if needed */
  619. if (dst->needs_writable && !av_frame_is_writable(frame)) {
  620. av_log(link->dst, AV_LOG_DEBUG, "Copying data in avfilter.\n");
  621. switch (link->type) {
  622. case AVMEDIA_TYPE_VIDEO:
  623. out = ff_get_video_buffer(link, link->w, link->h);
  624. break;
  625. case AVMEDIA_TYPE_AUDIO:
  626. out = ff_get_audio_buffer(link, frame->nb_samples);
  627. break;
  628. default:
  629. ret = AVERROR(EINVAL);
  630. goto fail;
  631. }
  632. if (!out) {
  633. ret = AVERROR(ENOMEM);
  634. goto fail;
  635. }
  636. ret = av_frame_copy_props(out, frame);
  637. if (ret < 0)
  638. goto fail;
  639. switch (link->type) {
  640. case AVMEDIA_TYPE_VIDEO:
  641. av_image_copy(out->data, out->linesize, frame->data, frame->linesize,
  642. frame->format, frame->width, frame->height);
  643. break;
  644. case AVMEDIA_TYPE_AUDIO:
  645. av_samples_copy(out->extended_data, frame->extended_data,
  646. 0, 0, frame->nb_samples,
  647. av_get_channel_layout_nb_channels(frame->channel_layout),
  648. frame->format);
  649. break;
  650. default:
  651. ret = AVERROR(EINVAL);
  652. goto fail;
  653. }
  654. av_frame_free(&frame);
  655. } else
  656. out = frame;
  657. return filter_frame(link, out);
  658. fail:
  659. av_frame_free(&out);
  660. av_frame_free(&frame);
  661. return ret;
  662. }
  663. const AVClass *avfilter_get_class(void)
  664. {
  665. return &avfilter_class;
  666. }