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.

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