avfilter.c 24 KB

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