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.

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