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.

731 lines
21KB

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