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.

733 lines
22KB

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