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.

799 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 AVERROR(EINVAL);
  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. if (!link->src || !link->dst) {
  131. av_log(filter, AV_LOG_ERROR,
  132. "Not all input and output are properly linked (%d).\n", i);
  133. return AVERROR(EINVAL);
  134. }
  135. switch (link->init_state) {
  136. case AVLINK_INIT:
  137. continue;
  138. case AVLINK_STARTINIT:
  139. av_log(filter, AV_LOG_INFO, "circular filter chain detected\n");
  140. return 0;
  141. case AVLINK_UNINIT:
  142. link->init_state = AVLINK_STARTINIT;
  143. if ((ret = avfilter_config_links(link->src)) < 0)
  144. return ret;
  145. if (!(config_link = link->srcpad->config_props)) {
  146. if (link->src->nb_inputs != 1) {
  147. av_log(link->src, AV_LOG_ERROR, "Source filters and filters "
  148. "with more than one input "
  149. "must set config_props() "
  150. "callbacks on all outputs\n");
  151. return AVERROR(EINVAL);
  152. }
  153. } else if ((ret = config_link(link)) < 0) {
  154. av_log(link->src, AV_LOG_ERROR,
  155. "Failed to configure output pad on %s\n",
  156. link->src->name);
  157. return ret;
  158. }
  159. if (link->time_base.num == 0 && link->time_base.den == 0)
  160. link->time_base = link->src->nb_inputs ?
  161. link->src->inputs[0]->time_base : AV_TIME_BASE_Q;
  162. if (link->type == AVMEDIA_TYPE_VIDEO) {
  163. if (!link->sample_aspect_ratio.num && !link->sample_aspect_ratio.den)
  164. link->sample_aspect_ratio = link->src->nb_inputs ?
  165. link->src->inputs[0]->sample_aspect_ratio : (AVRational){1,1};
  166. if (link->src->nb_inputs) {
  167. if (!link->frame_rate.num && !link->frame_rate.den)
  168. link->frame_rate = link->src->inputs[0]->frame_rate;
  169. if (!link->w)
  170. link->w = link->src->inputs[0]->w;
  171. if (!link->h)
  172. link->h = link->src->inputs[0]->h;
  173. } else if (!link->w || !link->h) {
  174. av_log(link->src, AV_LOG_ERROR,
  175. "Video source filters must set their output link's "
  176. "width and height\n");
  177. return AVERROR(EINVAL);
  178. }
  179. }
  180. if ((config_link = link->dstpad->config_props))
  181. if ((ret = config_link(link)) < 0) {
  182. av_log(link->dst, AV_LOG_ERROR,
  183. "Failed to configure input pad on %s\n",
  184. link->dst->name);
  185. return ret;
  186. }
  187. link->init_state = AVLINK_INIT;
  188. }
  189. }
  190. return 0;
  191. }
  192. void ff_dlog_link(void *ctx, AVFilterLink *link, int end)
  193. {
  194. if (link->type == AVMEDIA_TYPE_VIDEO) {
  195. av_log(ctx, AV_LOG_TRACE,
  196. "link[%p s:%dx%d fmt:%-16s %-16s->%-16s]%s",
  197. link, link->w, link->h,
  198. av_get_pix_fmt_name(link->format),
  199. link->src ? link->src->filter->name : "",
  200. link->dst ? link->dst->filter->name : "",
  201. end ? "\n" : "");
  202. } else {
  203. char buf[128];
  204. av_get_channel_layout_string(buf, sizeof(buf), -1, link->channel_layout);
  205. av_log(ctx, AV_LOG_TRACE,
  206. "link[%p r:%d cl:%s fmt:%-16s %-16s->%-16s]%s",
  207. link, link->sample_rate, buf,
  208. av_get_sample_fmt_name(link->format),
  209. link->src ? link->src->filter->name : "",
  210. link->dst ? link->dst->filter->name : "",
  211. end ? "\n" : "");
  212. }
  213. }
  214. int ff_request_frame(AVFilterLink *link)
  215. {
  216. FF_DPRINTF_START(NULL, request_frame); ff_dlog_link(NULL, link, 1);
  217. if (link->srcpad->request_frame)
  218. return link->srcpad->request_frame(link);
  219. else if (link->src->inputs[0])
  220. return ff_request_frame(link->src->inputs[0]);
  221. else
  222. return AVERROR(EINVAL);
  223. }
  224. int ff_poll_frame(AVFilterLink *link)
  225. {
  226. int i, min = INT_MAX;
  227. if (link->srcpad->poll_frame)
  228. return link->srcpad->poll_frame(link);
  229. for (i = 0; i < link->src->nb_inputs; i++) {
  230. int val;
  231. if (!link->src->inputs[i])
  232. return AVERROR(EINVAL);
  233. val = ff_poll_frame(link->src->inputs[i]);
  234. min = FFMIN(min, val);
  235. }
  236. return min;
  237. }
  238. static AVFilter *first_filter;
  239. #if !FF_API_NOCONST_GET_NAME
  240. const
  241. #endif
  242. AVFilter *avfilter_get_by_name(const char *name)
  243. {
  244. const AVFilter *f = NULL;
  245. if (!name)
  246. return NULL;
  247. while ((f = avfilter_next(f)))
  248. if (!strcmp(f->name, name))
  249. return f;
  250. return NULL;
  251. }
  252. int avfilter_register(AVFilter *filter)
  253. {
  254. AVFilter **f = &first_filter;
  255. while (*f)
  256. f = &(*f)->next;
  257. *f = filter;
  258. filter->next = NULL;
  259. return 0;
  260. }
  261. const AVFilter *avfilter_next(const AVFilter *prev)
  262. {
  263. return prev ? prev->next : first_filter;
  264. }
  265. #if FF_API_OLD_FILTER_REGISTER
  266. AVFilter **av_filter_next(AVFilter **filter)
  267. {
  268. return filter ? &(*filter)->next : &first_filter;
  269. }
  270. void avfilter_uninit(void)
  271. {
  272. }
  273. #endif
  274. int avfilter_pad_count(const AVFilterPad *pads)
  275. {
  276. int count;
  277. if (!pads)
  278. return 0;
  279. for (count = 0; pads->name; count++)
  280. pads++;
  281. return count;
  282. }
  283. static const char *filter_name(void *p)
  284. {
  285. AVFilterContext *filter = p;
  286. return filter->filter->name;
  287. }
  288. static void *filter_child_next(void *obj, void *prev)
  289. {
  290. AVFilterContext *ctx = obj;
  291. if (!prev && ctx->filter && ctx->filter->priv_class && ctx->priv)
  292. return ctx->priv;
  293. return NULL;
  294. }
  295. static const AVClass *filter_child_class_next(const AVClass *prev)
  296. {
  297. const AVFilter *f = NULL;
  298. while (prev && (f = avfilter_next(f)))
  299. if (f->priv_class == prev)
  300. break;
  301. while ((f = avfilter_next(f)))
  302. if (f->priv_class)
  303. return f->priv_class;
  304. return NULL;
  305. }
  306. #define OFFSET(x) offsetof(AVFilterContext, x)
  307. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM
  308. static const AVOption avfilter_options[] = {
  309. { "thread_type", "Allowed thread types", OFFSET(thread_type), AV_OPT_TYPE_FLAGS,
  310. { .i64 = AVFILTER_THREAD_SLICE }, 0, INT_MAX, FLAGS, "thread_type" },
  311. { "slice", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AVFILTER_THREAD_SLICE }, .unit = "thread_type" },
  312. { NULL },
  313. };
  314. static const AVClass avfilter_class = {
  315. .class_name = "AVFilter",
  316. .item_name = filter_name,
  317. .version = LIBAVUTIL_VERSION_INT,
  318. .child_next = filter_child_next,
  319. .child_class_next = filter_child_class_next,
  320. .option = avfilter_options,
  321. };
  322. static int default_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg,
  323. int *ret, int nb_jobs)
  324. {
  325. int i;
  326. for (i = 0; i < nb_jobs; i++) {
  327. int r = func(ctx, arg, i, nb_jobs);
  328. if (ret)
  329. ret[i] = r;
  330. }
  331. return 0;
  332. }
  333. AVFilterContext *ff_filter_alloc(const AVFilter *filter, const char *inst_name)
  334. {
  335. AVFilterContext *ret;
  336. if (!filter)
  337. return NULL;
  338. ret = av_mallocz(sizeof(AVFilterContext));
  339. if (!ret)
  340. return NULL;
  341. ret->av_class = &avfilter_class;
  342. ret->filter = filter;
  343. ret->name = inst_name ? av_strdup(inst_name) : NULL;
  344. if (filter->priv_size) {
  345. ret->priv = av_mallocz(filter->priv_size);
  346. if (!ret->priv)
  347. goto err;
  348. }
  349. av_opt_set_defaults(ret);
  350. if (filter->priv_class) {
  351. *(const AVClass**)ret->priv = filter->priv_class;
  352. av_opt_set_defaults(ret->priv);
  353. }
  354. ret->internal = av_mallocz(sizeof(*ret->internal));
  355. if (!ret->internal)
  356. goto err;
  357. ret->internal->execute = default_execute;
  358. ret->nb_inputs = avfilter_pad_count(filter->inputs);
  359. if (ret->nb_inputs ) {
  360. ret->input_pads = av_malloc(sizeof(AVFilterPad) * ret->nb_inputs);
  361. if (!ret->input_pads)
  362. goto err;
  363. memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad) * ret->nb_inputs);
  364. ret->inputs = av_mallocz(sizeof(AVFilterLink*) * ret->nb_inputs);
  365. if (!ret->inputs)
  366. goto err;
  367. }
  368. ret->nb_outputs = avfilter_pad_count(filter->outputs);
  369. if (ret->nb_outputs) {
  370. ret->output_pads = av_malloc(sizeof(AVFilterPad) * ret->nb_outputs);
  371. if (!ret->output_pads)
  372. goto err;
  373. memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad) * ret->nb_outputs);
  374. ret->outputs = av_mallocz(sizeof(AVFilterLink*) * ret->nb_outputs);
  375. if (!ret->outputs)
  376. goto err;
  377. }
  378. return ret;
  379. err:
  380. av_freep(&ret->inputs);
  381. av_freep(&ret->input_pads);
  382. ret->nb_inputs = 0;
  383. av_freep(&ret->outputs);
  384. av_freep(&ret->output_pads);
  385. ret->nb_outputs = 0;
  386. av_freep(&ret->priv);
  387. av_freep(&ret->internal);
  388. av_free(ret);
  389. return NULL;
  390. }
  391. #if FF_API_AVFILTER_OPEN
  392. int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *inst_name)
  393. {
  394. *filter_ctx = ff_filter_alloc(filter, inst_name);
  395. return *filter_ctx ? 0 : AVERROR(ENOMEM);
  396. }
  397. #endif
  398. static void free_link(AVFilterLink *link)
  399. {
  400. if (!link)
  401. return;
  402. if (link->src)
  403. link->src->outputs[link->srcpad - link->src->output_pads] = NULL;
  404. if (link->dst)
  405. link->dst->inputs[link->dstpad - link->dst->input_pads] = NULL;
  406. ff_formats_unref(&link->in_formats);
  407. ff_formats_unref(&link->out_formats);
  408. ff_formats_unref(&link->in_samplerates);
  409. ff_formats_unref(&link->out_samplerates);
  410. ff_channel_layouts_unref(&link->in_channel_layouts);
  411. ff_channel_layouts_unref(&link->out_channel_layouts);
  412. av_freep(&link);
  413. }
  414. void avfilter_free(AVFilterContext *filter)
  415. {
  416. int i;
  417. if (filter->graph)
  418. ff_filter_graph_remove_filter(filter->graph, filter);
  419. if (filter->filter->uninit)
  420. filter->filter->uninit(filter);
  421. for (i = 0; i < filter->nb_inputs; i++) {
  422. free_link(filter->inputs[i]);
  423. }
  424. for (i = 0; i < filter->nb_outputs; i++) {
  425. free_link(filter->outputs[i]);
  426. }
  427. if (filter->filter->priv_class)
  428. av_opt_free(filter->priv);
  429. av_freep(&filter->name);
  430. av_freep(&filter->input_pads);
  431. av_freep(&filter->output_pads);
  432. av_freep(&filter->inputs);
  433. av_freep(&filter->outputs);
  434. av_freep(&filter->priv);
  435. av_freep(&filter->internal);
  436. av_free(filter);
  437. }
  438. /* process a list of value1:value2:..., each value corresponding
  439. * to subsequent AVOption, in the order they are declared */
  440. static int process_unnamed_options(AVFilterContext *ctx, AVDictionary **options,
  441. const char *args)
  442. {
  443. const AVOption *o = NULL;
  444. const char *p = args;
  445. char *val;
  446. while (*p) {
  447. o = av_opt_next(ctx->priv, o);
  448. if (!o) {
  449. av_log(ctx, AV_LOG_ERROR, "More options provided than "
  450. "this filter supports.\n");
  451. return AVERROR(EINVAL);
  452. }
  453. if (o->type == AV_OPT_TYPE_CONST)
  454. continue;
  455. val = av_get_token(&p, ":");
  456. if (!val)
  457. return AVERROR(ENOMEM);
  458. av_dict_set(options, o->name, val, 0);
  459. av_freep(&val);
  460. if (*p)
  461. p++;
  462. }
  463. return 0;
  464. }
  465. #if FF_API_AVFILTER_INIT_FILTER
  466. int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
  467. {
  468. return avfilter_init_str(filter, args);
  469. }
  470. #endif
  471. int avfilter_init_dict(AVFilterContext *ctx, AVDictionary **options)
  472. {
  473. int ret = 0;
  474. ret = av_opt_set_dict(ctx, options);
  475. if (ret < 0) {
  476. av_log(ctx, AV_LOG_ERROR, "Error applying generic filter options.\n");
  477. return ret;
  478. }
  479. if (ctx->filter->flags & AVFILTER_FLAG_SLICE_THREADS &&
  480. ctx->thread_type & ctx->graph->thread_type & AVFILTER_THREAD_SLICE &&
  481. ctx->graph->internal->thread_execute) {
  482. ctx->thread_type = AVFILTER_THREAD_SLICE;
  483. ctx->internal->execute = ctx->graph->internal->thread_execute;
  484. } else {
  485. ctx->thread_type = 0;
  486. }
  487. if (ctx->filter->priv_class) {
  488. ret = av_opt_set_dict(ctx->priv, options);
  489. if (ret < 0) {
  490. av_log(ctx, AV_LOG_ERROR, "Error applying options to the filter.\n");
  491. return ret;
  492. }
  493. }
  494. if (ctx->filter->init)
  495. ret = ctx->filter->init(ctx);
  496. else if (ctx->filter->init_dict)
  497. ret = ctx->filter->init_dict(ctx, options);
  498. return ret;
  499. }
  500. int avfilter_init_str(AVFilterContext *filter, const char *args)
  501. {
  502. AVDictionary *options = NULL;
  503. AVDictionaryEntry *e;
  504. int ret = 0;
  505. if (args && *args) {
  506. if (!filter->filter->priv_class) {
  507. av_log(filter, AV_LOG_ERROR, "This filter does not take any "
  508. "options, but options were provided: %s.\n", args);
  509. return AVERROR(EINVAL);
  510. }
  511. #if FF_API_OLD_FILTER_OPTS
  512. if (!strcmp(filter->filter->name, "scale") &&
  513. strchr(args, ':') && strchr(args, ':') < strchr(args, '=')) {
  514. /* old w:h:flags=<flags> syntax */
  515. char *copy = av_strdup(args);
  516. char *p;
  517. av_log(filter, AV_LOG_WARNING, "The <w>:<h>:flags=<flags> option "
  518. "syntax is deprecated. Use either <w>:<h>:<flags> or "
  519. "w=<w>:h=<h>:flags=<flags>.\n");
  520. if (!copy) {
  521. ret = AVERROR(ENOMEM);
  522. goto fail;
  523. }
  524. p = strrchr(copy, ':');
  525. if (p) {
  526. *p++ = 0;
  527. ret = av_dict_parse_string(&options, p, "=", ":", 0);
  528. }
  529. if (ret >= 0)
  530. ret = process_unnamed_options(filter, &options, copy);
  531. av_freep(&copy);
  532. if (ret < 0)
  533. goto fail;
  534. } else
  535. #endif
  536. if (strchr(args, '=')) {
  537. /* assume a list of key1=value1:key2=value2:... */
  538. ret = av_dict_parse_string(&options, args, "=", ":", 0);
  539. if (ret < 0)
  540. goto fail;
  541. #if FF_API_OLD_FILTER_OPTS
  542. } else if (!strcmp(filter->filter->name, "format") ||
  543. !strcmp(filter->filter->name, "noformat") ||
  544. !strcmp(filter->filter->name, "frei0r") ||
  545. !strcmp(filter->filter->name, "frei0r_src") ||
  546. !strcmp(filter->filter->name, "ocv")) {
  547. /* a hack for compatibility with the old syntax
  548. * replace colons with |s */
  549. char *copy = av_strdup(args);
  550. char *p = copy;
  551. int nb_leading = 0; // number of leading colons to skip
  552. if (!copy) {
  553. ret = AVERROR(ENOMEM);
  554. goto fail;
  555. }
  556. if (!strcmp(filter->filter->name, "frei0r") ||
  557. !strcmp(filter->filter->name, "ocv"))
  558. nb_leading = 1;
  559. else if (!strcmp(filter->filter->name, "frei0r_src"))
  560. nb_leading = 3;
  561. while (nb_leading--) {
  562. p = strchr(p, ':');
  563. if (!p) {
  564. p = copy + strlen(copy);
  565. break;
  566. }
  567. p++;
  568. }
  569. if (strchr(p, ':')) {
  570. av_log(filter, AV_LOG_WARNING, "This syntax is deprecated. Use "
  571. "'|' to separate the list items.\n");
  572. }
  573. while ((p = strchr(p, ':')))
  574. *p++ = '|';
  575. ret = process_unnamed_options(filter, &options, copy);
  576. av_freep(&copy);
  577. if (ret < 0)
  578. goto fail;
  579. #endif
  580. } else {
  581. ret = process_unnamed_options(filter, &options, args);
  582. if (ret < 0)
  583. goto fail;
  584. }
  585. }
  586. ret = avfilter_init_dict(filter, &options);
  587. if (ret < 0)
  588. goto fail;
  589. if ((e = av_dict_get(options, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
  590. av_log(filter, AV_LOG_ERROR, "No such option: %s.\n", e->key);
  591. ret = AVERROR_OPTION_NOT_FOUND;
  592. goto fail;
  593. }
  594. fail:
  595. av_dict_free(&options);
  596. return ret;
  597. }
  598. const char *avfilter_pad_get_name(const AVFilterPad *pads, int pad_idx)
  599. {
  600. return pads[pad_idx].name;
  601. }
  602. enum AVMediaType avfilter_pad_get_type(const AVFilterPad *pads, int pad_idx)
  603. {
  604. return pads[pad_idx].type;
  605. }
  606. static int default_filter_frame(AVFilterLink *link, AVFrame *frame)
  607. {
  608. return ff_filter_frame(link->dst->outputs[0], frame);
  609. }
  610. int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
  611. {
  612. int (*filter_frame)(AVFilterLink *, AVFrame *);
  613. AVFilterPad *dst = link->dstpad;
  614. AVFrame *out = NULL;
  615. int ret;
  616. FF_DPRINTF_START(NULL, filter_frame);
  617. ff_dlog_link(NULL, link, 1);
  618. if (!(filter_frame = dst->filter_frame))
  619. filter_frame = default_filter_frame;
  620. /* copy the frame if needed */
  621. if (dst->needs_writable && !av_frame_is_writable(frame)) {
  622. av_log(link->dst, AV_LOG_DEBUG, "Copying data in avfilter.\n");
  623. switch (link->type) {
  624. case AVMEDIA_TYPE_VIDEO:
  625. out = ff_get_video_buffer(link, link->w, link->h);
  626. break;
  627. case AVMEDIA_TYPE_AUDIO:
  628. out = ff_get_audio_buffer(link, frame->nb_samples);
  629. break;
  630. default:
  631. ret = AVERROR(EINVAL);
  632. goto fail;
  633. }
  634. if (!out) {
  635. ret = AVERROR(ENOMEM);
  636. goto fail;
  637. }
  638. ret = av_frame_copy_props(out, frame);
  639. if (ret < 0)
  640. goto fail;
  641. switch (link->type) {
  642. case AVMEDIA_TYPE_VIDEO:
  643. av_image_copy(out->data, out->linesize, frame->data, frame->linesize,
  644. frame->format, frame->width, frame->height);
  645. break;
  646. case AVMEDIA_TYPE_AUDIO:
  647. av_samples_copy(out->extended_data, frame->extended_data,
  648. 0, 0, frame->nb_samples,
  649. av_get_channel_layout_nb_channels(frame->channel_layout),
  650. frame->format);
  651. break;
  652. default:
  653. ret = AVERROR(EINVAL);
  654. goto fail;
  655. }
  656. av_frame_free(&frame);
  657. } else
  658. out = frame;
  659. return filter_frame(link, out);
  660. fail:
  661. av_frame_free(&out);
  662. av_frame_free(&frame);
  663. return ret;
  664. }
  665. const AVClass *avfilter_get_class(void)
  666. {
  667. return &avfilter_class;
  668. }