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.

1126 lines
35KB

  1. /*
  2. * filter layer
  3. * Copyright (c) 2007 Bobby Bingham
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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/channel_layout.h"
  24. #include "libavutil/common.h"
  25. #include "libavutil/eval.h"
  26. #include "libavutil/imgutils.h"
  27. #include "libavutil/opt.h"
  28. #include "libavutil/pixdesc.h"
  29. #include "libavutil/rational.h"
  30. #include "libavutil/samplefmt.h"
  31. #include "audio.h"
  32. #include "avfilter.h"
  33. #include "formats.h"
  34. #include "internal.h"
  35. #include "audio.h"
  36. static int ff_filter_frame_framed(AVFilterLink *link, AVFrame *frame);
  37. void ff_tlog_ref(void *ctx, AVFrame *ref, int end)
  38. {
  39. av_unused char buf[16];
  40. ff_tlog(ctx,
  41. "ref[%p buf:%p data:%p linesize[%d, %d, %d, %d] pts:%"PRId64" pos:%"PRId64,
  42. ref, ref->buf, ref->data[0],
  43. ref->linesize[0], ref->linesize[1], ref->linesize[2], ref->linesize[3],
  44. ref->pts, av_frame_get_pkt_pos(ref));
  45. if (ref->width) {
  46. ff_tlog(ctx, " a:%d/%d s:%dx%d i:%c iskey:%d type:%c",
  47. ref->sample_aspect_ratio.num, ref->sample_aspect_ratio.den,
  48. ref->width, ref->height,
  49. !ref->interlaced_frame ? 'P' : /* Progressive */
  50. ref->top_field_first ? 'T' : 'B', /* Top / Bottom */
  51. ref->key_frame,
  52. av_get_picture_type_char(ref->pict_type));
  53. }
  54. if (ref->nb_samples) {
  55. ff_tlog(ctx, " cl:%"PRId64"d n:%d r:%d",
  56. ref->channel_layout,
  57. ref->nb_samples,
  58. ref->sample_rate);
  59. }
  60. ff_tlog(ctx, "]%s", end ? "\n" : "");
  61. }
  62. unsigned avfilter_version(void)
  63. {
  64. av_assert0(LIBAVFILTER_VERSION_MICRO >= 100);
  65. return LIBAVFILTER_VERSION_INT;
  66. }
  67. const char *avfilter_configuration(void)
  68. {
  69. return FFMPEG_CONFIGURATION;
  70. }
  71. const char *avfilter_license(void)
  72. {
  73. #define LICENSE_PREFIX "libavfilter license: "
  74. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  75. }
  76. void ff_command_queue_pop(AVFilterContext *filter)
  77. {
  78. AVFilterCommand *c= filter->command_queue;
  79. av_freep(&c->arg);
  80. av_freep(&c->command);
  81. filter->command_queue= c->next;
  82. av_free(c);
  83. }
  84. void ff_insert_pad(unsigned idx, unsigned *count, size_t padidx_off,
  85. AVFilterPad **pads, AVFilterLink ***links,
  86. AVFilterPad *newpad)
  87. {
  88. unsigned i;
  89. idx = FFMIN(idx, *count);
  90. *pads = av_realloc(*pads, sizeof(AVFilterPad) * (*count + 1));
  91. *links = av_realloc(*links, sizeof(AVFilterLink*) * (*count + 1));
  92. memmove(*pads + idx + 1, *pads + idx, sizeof(AVFilterPad) * (*count - idx));
  93. memmove(*links + idx + 1, *links + idx, sizeof(AVFilterLink*) * (*count - idx));
  94. memcpy(*pads + idx, newpad, sizeof(AVFilterPad));
  95. (*links)[idx] = NULL;
  96. (*count)++;
  97. for (i = idx + 1; i < *count; i++)
  98. if (*links[i])
  99. (*(unsigned *)((uint8_t *) *links[i] + padidx_off))++;
  100. }
  101. int avfilter_link(AVFilterContext *src, unsigned srcpad,
  102. AVFilterContext *dst, unsigned dstpad)
  103. {
  104. AVFilterLink *link;
  105. if (src->nb_outputs <= srcpad || dst->nb_inputs <= dstpad ||
  106. src->outputs[srcpad] || dst->inputs[dstpad])
  107. return -1;
  108. if (src->output_pads[srcpad].type != dst->input_pads[dstpad].type) {
  109. av_log(src, AV_LOG_ERROR,
  110. "Media type mismatch between the '%s' filter output pad %d (%s) and the '%s' filter input pad %d (%s)\n",
  111. src->name, srcpad, (char *)av_x_if_null(av_get_media_type_string(src->output_pads[srcpad].type), "?"),
  112. dst->name, dstpad, (char *)av_x_if_null(av_get_media_type_string(dst-> input_pads[dstpad].type), "?"));
  113. return AVERROR(EINVAL);
  114. }
  115. link = av_mallocz(sizeof(*link));
  116. if (!link)
  117. return AVERROR(ENOMEM);
  118. src->outputs[srcpad] = dst->inputs[dstpad] = link;
  119. link->src = src;
  120. link->dst = dst;
  121. link->srcpad = &src->output_pads[srcpad];
  122. link->dstpad = &dst->input_pads[dstpad];
  123. link->type = src->output_pads[srcpad].type;
  124. av_assert0(AV_PIX_FMT_NONE == -1 && AV_SAMPLE_FMT_NONE == -1);
  125. link->format = -1;
  126. return 0;
  127. }
  128. void avfilter_link_free(AVFilterLink **link)
  129. {
  130. if (!*link)
  131. return;
  132. av_frame_free(&(*link)->partial_buf);
  133. av_freep(link);
  134. }
  135. int avfilter_link_get_channels(AVFilterLink *link)
  136. {
  137. return link->channels;
  138. }
  139. void avfilter_link_set_closed(AVFilterLink *link, int closed)
  140. {
  141. link->closed = closed;
  142. }
  143. int avfilter_insert_filter(AVFilterLink *link, AVFilterContext *filt,
  144. unsigned filt_srcpad_idx, unsigned filt_dstpad_idx)
  145. {
  146. int ret;
  147. unsigned dstpad_idx = link->dstpad - link->dst->input_pads;
  148. av_log(link->dst, AV_LOG_VERBOSE, "auto-inserting filter '%s' "
  149. "between the filter '%s' and the filter '%s'\n",
  150. filt->name, link->src->name, link->dst->name);
  151. link->dst->inputs[dstpad_idx] = NULL;
  152. if ((ret = avfilter_link(filt, filt_dstpad_idx, link->dst, dstpad_idx)) < 0) {
  153. /* failed to link output filter to new filter */
  154. link->dst->inputs[dstpad_idx] = link;
  155. return ret;
  156. }
  157. /* re-hookup the link to the new destination filter we inserted */
  158. link->dst = filt;
  159. link->dstpad = &filt->input_pads[filt_srcpad_idx];
  160. filt->inputs[filt_srcpad_idx] = link;
  161. /* if any information on supported media formats already exists on the
  162. * link, we need to preserve that */
  163. if (link->out_formats)
  164. ff_formats_changeref(&link->out_formats,
  165. &filt->outputs[filt_dstpad_idx]->out_formats);
  166. if (link->out_samplerates)
  167. ff_formats_changeref(&link->out_samplerates,
  168. &filt->outputs[filt_dstpad_idx]->out_samplerates);
  169. if (link->out_channel_layouts)
  170. ff_channel_layouts_changeref(&link->out_channel_layouts,
  171. &filt->outputs[filt_dstpad_idx]->out_channel_layouts);
  172. return 0;
  173. }
  174. int avfilter_config_links(AVFilterContext *filter)
  175. {
  176. int (*config_link)(AVFilterLink *);
  177. unsigned i;
  178. int ret;
  179. for (i = 0; i < filter->nb_inputs; i ++) {
  180. AVFilterLink *link = filter->inputs[i];
  181. AVFilterLink *inlink;
  182. if (!link) continue;
  183. inlink = link->src->nb_inputs ? link->src->inputs[0] : NULL;
  184. link->current_pts = AV_NOPTS_VALUE;
  185. switch (link->init_state) {
  186. case AVLINK_INIT:
  187. continue;
  188. case AVLINK_STARTINIT:
  189. av_log(filter, AV_LOG_INFO, "circular filter chain detected\n");
  190. return 0;
  191. case AVLINK_UNINIT:
  192. link->init_state = AVLINK_STARTINIT;
  193. if ((ret = avfilter_config_links(link->src)) < 0)
  194. return ret;
  195. if (!(config_link = link->srcpad->config_props)) {
  196. if (link->src->nb_inputs != 1) {
  197. av_log(link->src, AV_LOG_ERROR, "Source filters and filters "
  198. "with more than one input "
  199. "must set config_props() "
  200. "callbacks on all outputs\n");
  201. return AVERROR(EINVAL);
  202. }
  203. } else if ((ret = config_link(link)) < 0) {
  204. av_log(link->src, AV_LOG_ERROR,
  205. "Failed to configure output pad on %s\n",
  206. link->src->name);
  207. return ret;
  208. }
  209. switch (link->type) {
  210. case AVMEDIA_TYPE_VIDEO:
  211. if (!link->time_base.num && !link->time_base.den)
  212. link->time_base = inlink ? inlink->time_base : AV_TIME_BASE_Q;
  213. if (!link->sample_aspect_ratio.num && !link->sample_aspect_ratio.den)
  214. link->sample_aspect_ratio = inlink ?
  215. inlink->sample_aspect_ratio : (AVRational){1,1};
  216. if (inlink && !link->frame_rate.num && !link->frame_rate.den)
  217. link->frame_rate = inlink->frame_rate;
  218. if (inlink) {
  219. if (!link->w)
  220. link->w = inlink->w;
  221. if (!link->h)
  222. link->h = inlink->h;
  223. } else if (!link->w || !link->h) {
  224. av_log(link->src, AV_LOG_ERROR,
  225. "Video source filters must set their output link's "
  226. "width and height\n");
  227. return AVERROR(EINVAL);
  228. }
  229. break;
  230. case AVMEDIA_TYPE_AUDIO:
  231. if (inlink) {
  232. if (!link->time_base.num && !link->time_base.den)
  233. link->time_base = inlink->time_base;
  234. }
  235. if (!link->time_base.num && !link->time_base.den)
  236. link->time_base = (AVRational) {1, link->sample_rate};
  237. }
  238. if ((config_link = link->dstpad->config_props))
  239. if ((ret = config_link(link)) < 0) {
  240. av_log(link->src, AV_LOG_ERROR,
  241. "Failed to configure input pad on %s\n",
  242. link->dst->name);
  243. return ret;
  244. }
  245. link->init_state = AVLINK_INIT;
  246. }
  247. }
  248. return 0;
  249. }
  250. void ff_tlog_link(void *ctx, AVFilterLink *link, int end)
  251. {
  252. if (link->type == AVMEDIA_TYPE_VIDEO) {
  253. ff_tlog(ctx,
  254. "link[%p s:%dx%d fmt:%s %s->%s]%s",
  255. link, link->w, link->h,
  256. av_get_pix_fmt_name(link->format),
  257. link->src ? link->src->filter->name : "",
  258. link->dst ? link->dst->filter->name : "",
  259. end ? "\n" : "");
  260. } else {
  261. char buf[128];
  262. av_get_channel_layout_string(buf, sizeof(buf), -1, link->channel_layout);
  263. ff_tlog(ctx,
  264. "link[%p r:%d cl:%s fmt:%s %s->%s]%s",
  265. link, (int)link->sample_rate, buf,
  266. av_get_sample_fmt_name(link->format),
  267. link->src ? link->src->filter->name : "",
  268. link->dst ? link->dst->filter->name : "",
  269. end ? "\n" : "");
  270. }
  271. }
  272. int ff_request_frame(AVFilterLink *link)
  273. {
  274. int ret = -1;
  275. FF_TPRINTF_START(NULL, request_frame); ff_tlog_link(NULL, link, 1);
  276. if (link->closed)
  277. return AVERROR_EOF;
  278. av_assert0(!link->frame_requested);
  279. link->frame_requested = 1;
  280. while (link->frame_requested) {
  281. if (link->srcpad->request_frame)
  282. ret = link->srcpad->request_frame(link);
  283. else if (link->src->inputs[0])
  284. ret = ff_request_frame(link->src->inputs[0]);
  285. if (ret == AVERROR_EOF && link->partial_buf) {
  286. AVFrame *pbuf = link->partial_buf;
  287. link->partial_buf = NULL;
  288. ret = ff_filter_frame_framed(link, pbuf);
  289. }
  290. if (ret < 0) {
  291. link->frame_requested = 0;
  292. if (ret == AVERROR_EOF)
  293. link->closed = 1;
  294. } else {
  295. av_assert0(!link->frame_requested ||
  296. link->flags & FF_LINK_FLAG_REQUEST_LOOP);
  297. }
  298. }
  299. return ret;
  300. }
  301. int ff_poll_frame(AVFilterLink *link)
  302. {
  303. int i, min = INT_MAX;
  304. if (link->srcpad->poll_frame)
  305. return link->srcpad->poll_frame(link);
  306. for (i = 0; i < link->src->nb_inputs; i++) {
  307. int val;
  308. if (!link->src->inputs[i])
  309. return -1;
  310. val = ff_poll_frame(link->src->inputs[i]);
  311. min = FFMIN(min, val);
  312. }
  313. return min;
  314. }
  315. static const char *const var_names[] = { "t", "n", "pos", NULL };
  316. enum { VAR_T, VAR_N, VAR_POS, VAR_VARS_NB };
  317. static int set_enable_expr(AVFilterContext *ctx, const char *expr)
  318. {
  319. int ret;
  320. char *expr_dup;
  321. AVExpr *old = ctx->enable;
  322. if (!(ctx->filter->flags & AVFILTER_FLAG_SUPPORT_TIMELINE)) {
  323. av_log(ctx, AV_LOG_ERROR, "Timeline ('enable' option) not supported "
  324. "with filter '%s'\n", ctx->filter->name);
  325. return AVERROR_PATCHWELCOME;
  326. }
  327. expr_dup = av_strdup(expr);
  328. if (!expr_dup)
  329. return AVERROR(ENOMEM);
  330. if (!ctx->var_values) {
  331. ctx->var_values = av_calloc(VAR_VARS_NB, sizeof(*ctx->var_values));
  332. if (!ctx->var_values) {
  333. av_free(expr_dup);
  334. return AVERROR(ENOMEM);
  335. }
  336. }
  337. ret = av_expr_parse((AVExpr**)&ctx->enable, expr_dup, var_names,
  338. NULL, NULL, NULL, NULL, 0, ctx->priv);
  339. if (ret < 0) {
  340. av_log(ctx->priv, AV_LOG_ERROR,
  341. "Error when evaluating the expression '%s' for enable\n",
  342. expr_dup);
  343. av_free(expr_dup);
  344. return ret;
  345. }
  346. av_expr_free(old);
  347. av_free(ctx->enable_str);
  348. ctx->enable_str = expr_dup;
  349. return 0;
  350. }
  351. void ff_update_link_current_pts(AVFilterLink *link, int64_t pts)
  352. {
  353. if (pts == AV_NOPTS_VALUE)
  354. return;
  355. link->current_pts = av_rescale_q(pts, link->time_base, AV_TIME_BASE_Q);
  356. /* TODO use duration */
  357. if (link->graph && link->age_index >= 0)
  358. ff_avfilter_graph_update_heap(link->graph, link);
  359. }
  360. int avfilter_process_command(AVFilterContext *filter, const char *cmd, const char *arg, char *res, int res_len, int flags)
  361. {
  362. if(!strcmp(cmd, "ping")){
  363. av_strlcatf(res, res_len, "pong from:%s %s\n", filter->filter->name, filter->name);
  364. return 0;
  365. }else if(!strcmp(cmd, "enable")) {
  366. return set_enable_expr(filter, arg);
  367. }else if(filter->filter->process_command) {
  368. return filter->filter->process_command(filter, cmd, arg, res, res_len, flags);
  369. }
  370. return AVERROR(ENOSYS);
  371. }
  372. static AVFilter *first_filter;
  373. AVFilter *avfilter_get_by_name(const char *name)
  374. {
  375. const AVFilter *f = NULL;
  376. if (!name)
  377. return NULL;
  378. while ((f = avfilter_next(f)))
  379. if (!strcmp(f->name, name))
  380. return (AVFilter *)f;
  381. return NULL;
  382. }
  383. int avfilter_register(AVFilter *filter)
  384. {
  385. AVFilter **f = &first_filter;
  386. int i;
  387. /* the filter must select generic or internal exclusively */
  388. av_assert0((filter->flags & AVFILTER_FLAG_SUPPORT_TIMELINE) != AVFILTER_FLAG_SUPPORT_TIMELINE);
  389. for(i=0; filter->inputs && filter->inputs[i].name; i++) {
  390. const AVFilterPad *input = &filter->inputs[i];
  391. av_assert0( !input->filter_frame
  392. || (!input->start_frame && !input->end_frame));
  393. }
  394. while (*f)
  395. f = &(*f)->next;
  396. *f = filter;
  397. filter->next = NULL;
  398. return 0;
  399. }
  400. const AVFilter *avfilter_next(const AVFilter *prev)
  401. {
  402. return prev ? prev->next : first_filter;
  403. }
  404. #if FF_API_OLD_FILTER_REGISTER
  405. AVFilter **av_filter_next(AVFilter **filter)
  406. {
  407. return filter ? &(*filter)->next : &first_filter;
  408. }
  409. void avfilter_uninit(void)
  410. {
  411. }
  412. #endif
  413. int avfilter_pad_count(const AVFilterPad *pads)
  414. {
  415. int count;
  416. if (!pads)
  417. return 0;
  418. for (count = 0; pads->name; count++)
  419. pads++;
  420. return count;
  421. }
  422. static const char *default_filter_name(void *filter_ctx)
  423. {
  424. AVFilterContext *ctx = filter_ctx;
  425. return ctx->name ? ctx->name : ctx->filter->name;
  426. }
  427. static void *filter_child_next(void *obj, void *prev)
  428. {
  429. AVFilterContext *ctx = obj;
  430. if (!prev && ctx->filter && ctx->filter->priv_class && ctx->priv)
  431. return ctx->priv;
  432. return NULL;
  433. }
  434. static const AVClass *filter_child_class_next(const AVClass *prev)
  435. {
  436. const AVFilter *f = NULL;
  437. /* find the filter that corresponds to prev */
  438. while (prev && (f = avfilter_next(f)))
  439. if (f->priv_class == prev)
  440. break;
  441. /* could not find filter corresponding to prev */
  442. if (prev && !f)
  443. return NULL;
  444. /* find next filter with specific options */
  445. while ((f = avfilter_next(f)))
  446. if (f->priv_class)
  447. return f->priv_class;
  448. return NULL;
  449. }
  450. #define OFFSET(x) offsetof(AVFilterContext, x)
  451. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM
  452. static const AVOption options[] = {
  453. { "thread_type", "Allowed thread types", OFFSET(thread_type), AV_OPT_TYPE_FLAGS,
  454. { .i64 = AVFILTER_THREAD_SLICE }, 0, INT_MAX, FLAGS, "thread_type" },
  455. { "slice", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AVFILTER_THREAD_SLICE }, .unit = "thread_type" },
  456. { "enable", "set enable expression", OFFSET(enable_str), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  457. { NULL },
  458. };
  459. static const AVClass avfilter_class = {
  460. .class_name = "AVFilter",
  461. .item_name = default_filter_name,
  462. .version = LIBAVUTIL_VERSION_INT,
  463. .category = AV_CLASS_CATEGORY_FILTER,
  464. .child_next = filter_child_next,
  465. .child_class_next = filter_child_class_next,
  466. .option = options,
  467. };
  468. static int default_execute(AVFilterContext *ctx, action_func *func, void *arg,
  469. int *ret, int nb_jobs)
  470. {
  471. int i;
  472. for (i = 0; i < nb_jobs; i++) {
  473. int r = func(ctx, arg, i, nb_jobs);
  474. if (ret)
  475. ret[i] = r;
  476. }
  477. return 0;
  478. }
  479. AVFilterContext *ff_filter_alloc(const AVFilter *filter, const char *inst_name)
  480. {
  481. AVFilterContext *ret;
  482. if (!filter)
  483. return NULL;
  484. ret = av_mallocz(sizeof(AVFilterContext));
  485. if (!ret)
  486. return NULL;
  487. ret->av_class = &avfilter_class;
  488. ret->filter = filter;
  489. ret->name = inst_name ? av_strdup(inst_name) : NULL;
  490. if (filter->priv_size) {
  491. ret->priv = av_mallocz(filter->priv_size);
  492. if (!ret->priv)
  493. goto err;
  494. }
  495. av_opt_set_defaults(ret);
  496. if (filter->priv_class) {
  497. *(const AVClass**)ret->priv = filter->priv_class;
  498. av_opt_set_defaults(ret->priv);
  499. }
  500. ret->internal = av_mallocz(sizeof(*ret->internal));
  501. if (!ret->internal)
  502. goto err;
  503. ret->internal->execute = default_execute;
  504. ret->nb_inputs = avfilter_pad_count(filter->inputs);
  505. if (ret->nb_inputs ) {
  506. ret->input_pads = av_malloc(sizeof(AVFilterPad) * ret->nb_inputs);
  507. if (!ret->input_pads)
  508. goto err;
  509. memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad) * ret->nb_inputs);
  510. ret->inputs = av_mallocz(sizeof(AVFilterLink*) * ret->nb_inputs);
  511. if (!ret->inputs)
  512. goto err;
  513. }
  514. ret->nb_outputs = avfilter_pad_count(filter->outputs);
  515. if (ret->nb_outputs) {
  516. ret->output_pads = av_malloc(sizeof(AVFilterPad) * ret->nb_outputs);
  517. if (!ret->output_pads)
  518. goto err;
  519. memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad) * ret->nb_outputs);
  520. ret->outputs = av_mallocz(sizeof(AVFilterLink*) * ret->nb_outputs);
  521. if (!ret->outputs)
  522. goto err;
  523. }
  524. #if FF_API_FOO_COUNT
  525. ret->output_count = ret->nb_outputs;
  526. ret->input_count = ret->nb_inputs;
  527. #endif
  528. return ret;
  529. err:
  530. av_freep(&ret->inputs);
  531. av_freep(&ret->input_pads);
  532. ret->nb_inputs = 0;
  533. av_freep(&ret->outputs);
  534. av_freep(&ret->output_pads);
  535. ret->nb_outputs = 0;
  536. av_freep(&ret->priv);
  537. av_freep(&ret->internal);
  538. av_free(ret);
  539. return NULL;
  540. }
  541. #if FF_API_AVFILTER_OPEN
  542. int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *inst_name)
  543. {
  544. *filter_ctx = ff_filter_alloc(filter, inst_name);
  545. return *filter_ctx ? 0 : AVERROR(ENOMEM);
  546. }
  547. #endif
  548. static void free_link(AVFilterLink *link)
  549. {
  550. if (!link)
  551. return;
  552. if (link->src)
  553. link->src->outputs[link->srcpad - link->src->output_pads] = NULL;
  554. if (link->dst)
  555. link->dst->inputs[link->dstpad - link->dst->input_pads] = NULL;
  556. ff_formats_unref(&link->in_formats);
  557. ff_formats_unref(&link->out_formats);
  558. ff_formats_unref(&link->in_samplerates);
  559. ff_formats_unref(&link->out_samplerates);
  560. ff_channel_layouts_unref(&link->in_channel_layouts);
  561. ff_channel_layouts_unref(&link->out_channel_layouts);
  562. avfilter_link_free(&link);
  563. }
  564. void avfilter_free(AVFilterContext *filter)
  565. {
  566. int i;
  567. if (!filter)
  568. return;
  569. if (filter->graph)
  570. ff_filter_graph_remove_filter(filter->graph, filter);
  571. if (filter->filter->uninit)
  572. filter->filter->uninit(filter);
  573. for (i = 0; i < filter->nb_inputs; i++) {
  574. free_link(filter->inputs[i]);
  575. }
  576. for (i = 0; i < filter->nb_outputs; i++) {
  577. free_link(filter->outputs[i]);
  578. }
  579. if (filter->filter->priv_class)
  580. av_opt_free(filter->priv);
  581. av_freep(&filter->name);
  582. av_freep(&filter->input_pads);
  583. av_freep(&filter->output_pads);
  584. av_freep(&filter->inputs);
  585. av_freep(&filter->outputs);
  586. av_freep(&filter->priv);
  587. while(filter->command_queue){
  588. ff_command_queue_pop(filter);
  589. }
  590. av_opt_free(filter);
  591. av_expr_free(filter->enable);
  592. filter->enable = NULL;
  593. av_freep(&filter->var_values);
  594. av_freep(&filter->internal);
  595. av_free(filter);
  596. }
  597. static int process_options(AVFilterContext *ctx, AVDictionary **options,
  598. const char *args)
  599. {
  600. const AVOption *o = NULL;
  601. int ret, count = 0;
  602. char *av_uninit(parsed_key), *av_uninit(value);
  603. const char *key;
  604. int offset= -1;
  605. if (!args)
  606. return 0;
  607. while (*args) {
  608. const char *shorthand = NULL;
  609. o = av_opt_next(ctx->priv, o);
  610. if (o) {
  611. if (o->type == AV_OPT_TYPE_CONST || o->offset == offset)
  612. continue;
  613. offset = o->offset;
  614. shorthand = o->name;
  615. }
  616. ret = av_opt_get_key_value(&args, "=", ":",
  617. shorthand ? AV_OPT_FLAG_IMPLICIT_KEY : 0,
  618. &parsed_key, &value);
  619. if (ret < 0) {
  620. if (ret == AVERROR(EINVAL))
  621. av_log(ctx, AV_LOG_ERROR, "No option name near '%s'\n", args);
  622. else
  623. av_log(ctx, AV_LOG_ERROR, "Unable to parse '%s': %s\n", args,
  624. av_err2str(ret));
  625. return ret;
  626. }
  627. if (*args)
  628. args++;
  629. if (parsed_key) {
  630. key = parsed_key;
  631. while ((o = av_opt_next(ctx->priv, o))); /* discard all remaining shorthand */
  632. } else {
  633. key = shorthand;
  634. }
  635. av_log(ctx, AV_LOG_DEBUG, "Setting '%s' to value '%s'\n", key, value);
  636. if (av_opt_find(ctx, key, NULL, 0, 0)) {
  637. ret = av_opt_set(ctx, key, value, 0);
  638. if (ret < 0)
  639. return ret;
  640. } else {
  641. av_dict_set(options, key, value, 0);
  642. if ((ret = av_opt_set(ctx->priv, key, value, 0)) < 0) {
  643. if (!av_opt_find(ctx->priv, key, NULL, 0, AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)) {
  644. if (ret == AVERROR_OPTION_NOT_FOUND)
  645. av_log(ctx, AV_LOG_ERROR, "Option '%s' not found\n", key);
  646. av_free(value);
  647. av_free(parsed_key);
  648. return ret;
  649. }
  650. }
  651. }
  652. av_free(value);
  653. av_free(parsed_key);
  654. count++;
  655. }
  656. if (ctx->enable_str) {
  657. ret = set_enable_expr(ctx, ctx->enable_str);
  658. if (ret < 0)
  659. return ret;
  660. }
  661. return count;
  662. }
  663. #if FF_API_AVFILTER_INIT_FILTER
  664. int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
  665. {
  666. return avfilter_init_str(filter, args);
  667. }
  668. #endif
  669. int avfilter_init_dict(AVFilterContext *ctx, AVDictionary **options)
  670. {
  671. int ret = 0;
  672. ret = av_opt_set_dict(ctx, options);
  673. if (ret < 0) {
  674. av_log(ctx, AV_LOG_ERROR, "Error applying generic filter options.\n");
  675. return ret;
  676. }
  677. if (ctx->filter->flags & AVFILTER_FLAG_SLICE_THREADS &&
  678. ctx->thread_type & ctx->graph->thread_type & AVFILTER_THREAD_SLICE &&
  679. ctx->graph->internal->thread_execute) {
  680. ctx->thread_type = AVFILTER_THREAD_SLICE;
  681. ctx->internal->execute = ctx->graph->internal->thread_execute;
  682. } else {
  683. ctx->thread_type = 0;
  684. }
  685. if (ctx->filter->priv_class) {
  686. ret = av_opt_set_dict(ctx->priv, options);
  687. if (ret < 0) {
  688. av_log(ctx, AV_LOG_ERROR, "Error applying options to the filter.\n");
  689. return ret;
  690. }
  691. }
  692. if (ctx->filter->init_opaque)
  693. ret = ctx->filter->init_opaque(ctx, NULL);
  694. else if (ctx->filter->init)
  695. ret = ctx->filter->init(ctx);
  696. else if (ctx->filter->init_dict)
  697. ret = ctx->filter->init_dict(ctx, options);
  698. return ret;
  699. }
  700. int avfilter_init_str(AVFilterContext *filter, const char *args)
  701. {
  702. AVDictionary *options = NULL;
  703. AVDictionaryEntry *e;
  704. int ret = 0;
  705. if (args && *args) {
  706. if (!filter->filter->priv_class) {
  707. av_log(filter, AV_LOG_ERROR, "This filter does not take any "
  708. "options, but options were provided: %s.\n", args);
  709. return AVERROR(EINVAL);
  710. }
  711. #if FF_API_OLD_FILTER_OPTS
  712. if ( !strcmp(filter->filter->name, "format") ||
  713. !strcmp(filter->filter->name, "noformat") ||
  714. !strcmp(filter->filter->name, "frei0r") ||
  715. !strcmp(filter->filter->name, "frei0r_src") ||
  716. !strcmp(filter->filter->name, "ocv") ||
  717. !strcmp(filter->filter->name, "pan") ||
  718. !strcmp(filter->filter->name, "pp") ||
  719. !strcmp(filter->filter->name, "aevalsrc")) {
  720. /* a hack for compatibility with the old syntax
  721. * replace colons with |s */
  722. char *copy = av_strdup(args);
  723. char *p = copy;
  724. int nb_leading = 0; // number of leading colons to skip
  725. int deprecated = 0;
  726. if (!copy) {
  727. ret = AVERROR(ENOMEM);
  728. goto fail;
  729. }
  730. if (!strcmp(filter->filter->name, "frei0r") ||
  731. !strcmp(filter->filter->name, "ocv"))
  732. nb_leading = 1;
  733. else if (!strcmp(filter->filter->name, "frei0r_src"))
  734. nb_leading = 3;
  735. while (nb_leading--) {
  736. p = strchr(p, ':');
  737. if (!p) {
  738. p = copy + strlen(copy);
  739. break;
  740. }
  741. p++;
  742. }
  743. deprecated = strchr(p, ':') != NULL;
  744. if (!strcmp(filter->filter->name, "aevalsrc")) {
  745. deprecated = 0;
  746. while ((p = strchr(p, ':')) && p[1] != ':') {
  747. const char *epos = strchr(p + 1, '=');
  748. const char *spos = strchr(p + 1, ':');
  749. const int next_token_is_opt = epos && (!spos || epos < spos);
  750. if (next_token_is_opt) {
  751. p++;
  752. break;
  753. }
  754. /* next token does not contain a '=', assume a channel expression */
  755. deprecated = 1;
  756. *p++ = '|';
  757. }
  758. if (p && *p == ':') { // double sep '::' found
  759. deprecated = 1;
  760. memmove(p, p + 1, strlen(p));
  761. }
  762. } else
  763. while ((p = strchr(p, ':')))
  764. *p++ = '|';
  765. if (deprecated)
  766. av_log(filter, AV_LOG_WARNING, "This syntax is deprecated. Use "
  767. "'|' to separate the list items.\n");
  768. av_log(filter, AV_LOG_DEBUG, "compat: called with args=[%s]\n", copy);
  769. ret = process_options(filter, &options, copy);
  770. av_freep(&copy);
  771. if (ret < 0)
  772. goto fail;
  773. #endif
  774. } else {
  775. #if CONFIG_MP_FILTER
  776. if (!strcmp(filter->filter->name, "mp")) {
  777. char *escaped;
  778. if (!strncmp(args, "filter=", 7))
  779. args += 7;
  780. ret = av_escape(&escaped, args, ":=", AV_ESCAPE_MODE_BACKSLASH, 0);
  781. if (ret < 0) {
  782. av_log(filter, AV_LOG_ERROR, "Unable to escape MPlayer filters arg '%s'\n", args);
  783. goto fail;
  784. }
  785. ret = process_options(filter, &options, escaped);
  786. av_free(escaped);
  787. } else
  788. #endif
  789. ret = process_options(filter, &options, args);
  790. if (ret < 0)
  791. goto fail;
  792. }
  793. }
  794. ret = avfilter_init_dict(filter, &options);
  795. if (ret < 0)
  796. goto fail;
  797. if ((e = av_dict_get(options, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
  798. av_log(filter, AV_LOG_ERROR, "No such option: %s.\n", e->key);
  799. ret = AVERROR_OPTION_NOT_FOUND;
  800. goto fail;
  801. }
  802. fail:
  803. av_dict_free(&options);
  804. return ret;
  805. }
  806. const char *avfilter_pad_get_name(const AVFilterPad *pads, int pad_idx)
  807. {
  808. return pads[pad_idx].name;
  809. }
  810. enum AVMediaType avfilter_pad_get_type(const AVFilterPad *pads, int pad_idx)
  811. {
  812. return pads[pad_idx].type;
  813. }
  814. static int default_filter_frame(AVFilterLink *link, AVFrame *frame)
  815. {
  816. return ff_filter_frame(link->dst->outputs[0], frame);
  817. }
  818. static int ff_filter_frame_framed(AVFilterLink *link, AVFrame *frame)
  819. {
  820. int (*filter_frame)(AVFilterLink *, AVFrame *);
  821. AVFilterContext *dstctx = link->dst;
  822. AVFilterPad *dst = link->dstpad;
  823. AVFrame *out;
  824. int ret;
  825. AVFilterCommand *cmd= link->dst->command_queue;
  826. int64_t pts;
  827. if (link->closed) {
  828. av_frame_free(&frame);
  829. return AVERROR_EOF;
  830. }
  831. if (!(filter_frame = dst->filter_frame))
  832. filter_frame = default_filter_frame;
  833. /* copy the frame if needed */
  834. if (dst->needs_writable && !av_frame_is_writable(frame)) {
  835. av_log(link->dst, AV_LOG_DEBUG, "Copying data in avfilter.\n");
  836. /* Maybe use ff_copy_buffer_ref instead? */
  837. switch (link->type) {
  838. case AVMEDIA_TYPE_VIDEO:
  839. out = ff_get_video_buffer(link, link->w, link->h);
  840. break;
  841. case AVMEDIA_TYPE_AUDIO:
  842. out = ff_get_audio_buffer(link, frame->nb_samples);
  843. break;
  844. default: return AVERROR(EINVAL);
  845. }
  846. if (!out) {
  847. av_frame_free(&frame);
  848. return AVERROR(ENOMEM);
  849. }
  850. av_frame_copy_props(out, frame);
  851. switch (link->type) {
  852. case AVMEDIA_TYPE_VIDEO:
  853. av_image_copy(out->data, out->linesize, (const uint8_t **)frame->data, frame->linesize,
  854. frame->format, frame->width, frame->height);
  855. break;
  856. case AVMEDIA_TYPE_AUDIO:
  857. av_samples_copy(out->extended_data, frame->extended_data,
  858. 0, 0, frame->nb_samples,
  859. av_get_channel_layout_nb_channels(frame->channel_layout),
  860. frame->format);
  861. break;
  862. default: return AVERROR(EINVAL);
  863. }
  864. av_frame_free(&frame);
  865. } else
  866. out = frame;
  867. while(cmd && cmd->time <= out->pts * av_q2d(link->time_base)){
  868. av_log(link->dst, AV_LOG_DEBUG,
  869. "Processing command time:%f command:%s arg:%s\n",
  870. cmd->time, cmd->command, cmd->arg);
  871. avfilter_process_command(link->dst, cmd->command, cmd->arg, 0, 0, cmd->flags);
  872. ff_command_queue_pop(link->dst);
  873. cmd= link->dst->command_queue;
  874. }
  875. pts = out->pts;
  876. if (dstctx->enable_str) {
  877. int64_t pos = av_frame_get_pkt_pos(out);
  878. dstctx->var_values[VAR_N] = link->frame_count;
  879. dstctx->var_values[VAR_T] = pts == AV_NOPTS_VALUE ? NAN : pts * av_q2d(link->time_base);
  880. dstctx->var_values[VAR_POS] = pos == -1 ? NAN : pos;
  881. dstctx->is_disabled = !av_expr_eval(dstctx->enable, dstctx->var_values, NULL);
  882. if (dstctx->is_disabled &&
  883. (dstctx->filter->flags & AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC))
  884. filter_frame = default_filter_frame;
  885. }
  886. ret = filter_frame(link, out);
  887. link->frame_count++;
  888. link->frame_requested = 0;
  889. ff_update_link_current_pts(link, pts);
  890. return ret;
  891. }
  892. static int ff_filter_frame_needs_framing(AVFilterLink *link, AVFrame *frame)
  893. {
  894. int insamples = frame->nb_samples, inpos = 0, nb_samples;
  895. AVFrame *pbuf = link->partial_buf;
  896. int nb_channels = av_frame_get_channels(frame);
  897. int ret = 0;
  898. link->flags |= FF_LINK_FLAG_REQUEST_LOOP;
  899. /* Handle framing (min_samples, max_samples) */
  900. while (insamples) {
  901. if (!pbuf) {
  902. AVRational samples_tb = { 1, link->sample_rate };
  903. pbuf = ff_get_audio_buffer(link, link->partial_buf_size);
  904. if (!pbuf) {
  905. av_log(link->dst, AV_LOG_WARNING,
  906. "Samples dropped due to memory allocation failure.\n");
  907. return 0;
  908. }
  909. av_frame_copy_props(pbuf, frame);
  910. pbuf->pts = frame->pts +
  911. av_rescale_q(inpos, samples_tb, link->time_base);
  912. pbuf->nb_samples = 0;
  913. }
  914. nb_samples = FFMIN(insamples,
  915. link->partial_buf_size - pbuf->nb_samples);
  916. av_samples_copy(pbuf->extended_data, frame->extended_data,
  917. pbuf->nb_samples, inpos,
  918. nb_samples, nb_channels, link->format);
  919. inpos += nb_samples;
  920. insamples -= nb_samples;
  921. pbuf->nb_samples += nb_samples;
  922. if (pbuf->nb_samples >= link->min_samples) {
  923. ret = ff_filter_frame_framed(link, pbuf);
  924. pbuf = NULL;
  925. }
  926. }
  927. av_frame_free(&frame);
  928. link->partial_buf = pbuf;
  929. return ret;
  930. }
  931. int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
  932. {
  933. FF_TPRINTF_START(NULL, filter_frame); ff_tlog_link(NULL, link, 1); ff_tlog(NULL, " "); ff_tlog_ref(NULL, frame, 1);
  934. /* Consistency checks */
  935. if (link->type == AVMEDIA_TYPE_VIDEO) {
  936. if (strcmp(link->dst->filter->name, "scale")) {
  937. av_assert1(frame->format == link->format);
  938. av_assert1(frame->width == link->w);
  939. av_assert1(frame->height == link->h);
  940. }
  941. } else {
  942. av_assert1(frame->format == link->format);
  943. av_assert1(av_frame_get_channels(frame) == link->channels);
  944. av_assert1(frame->channel_layout == link->channel_layout);
  945. av_assert1(frame->sample_rate == link->sample_rate);
  946. }
  947. /* Go directly to actual filtering if possible */
  948. if (link->type == AVMEDIA_TYPE_AUDIO &&
  949. link->min_samples &&
  950. (link->partial_buf ||
  951. frame->nb_samples < link->min_samples ||
  952. frame->nb_samples > link->max_samples)) {
  953. return ff_filter_frame_needs_framing(link, frame);
  954. } else {
  955. return ff_filter_frame_framed(link, frame);
  956. }
  957. }
  958. const AVClass *avfilter_get_class(void)
  959. {
  960. return &avfilter_class;
  961. }