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.

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