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.

1129 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. av_free(value);
  640. av_free(parsed_key);
  641. return ret;
  642. }
  643. } else {
  644. av_dict_set(options, key, value, 0);
  645. if ((ret = av_opt_set(ctx->priv, key, value, 0)) < 0) {
  646. if (!av_opt_find(ctx->priv, key, NULL, 0, AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)) {
  647. if (ret == AVERROR_OPTION_NOT_FOUND)
  648. av_log(ctx, AV_LOG_ERROR, "Option '%s' not found\n", key);
  649. av_free(value);
  650. av_free(parsed_key);
  651. return ret;
  652. }
  653. }
  654. }
  655. av_free(value);
  656. av_free(parsed_key);
  657. count++;
  658. }
  659. if (ctx->enable_str) {
  660. ret = set_enable_expr(ctx, ctx->enable_str);
  661. if (ret < 0)
  662. return ret;
  663. }
  664. return count;
  665. }
  666. #if FF_API_AVFILTER_INIT_FILTER
  667. int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
  668. {
  669. return avfilter_init_str(filter, args);
  670. }
  671. #endif
  672. int avfilter_init_dict(AVFilterContext *ctx, AVDictionary **options)
  673. {
  674. int ret = 0;
  675. ret = av_opt_set_dict(ctx, options);
  676. if (ret < 0) {
  677. av_log(ctx, AV_LOG_ERROR, "Error applying generic filter options.\n");
  678. return ret;
  679. }
  680. if (ctx->filter->flags & AVFILTER_FLAG_SLICE_THREADS &&
  681. ctx->thread_type & ctx->graph->thread_type & AVFILTER_THREAD_SLICE &&
  682. ctx->graph->internal->thread_execute) {
  683. ctx->thread_type = AVFILTER_THREAD_SLICE;
  684. ctx->internal->execute = ctx->graph->internal->thread_execute;
  685. } else {
  686. ctx->thread_type = 0;
  687. }
  688. if (ctx->filter->priv_class) {
  689. ret = av_opt_set_dict(ctx->priv, options);
  690. if (ret < 0) {
  691. av_log(ctx, AV_LOG_ERROR, "Error applying options to the filter.\n");
  692. return ret;
  693. }
  694. }
  695. if (ctx->filter->init_opaque)
  696. ret = ctx->filter->init_opaque(ctx, NULL);
  697. else if (ctx->filter->init)
  698. ret = ctx->filter->init(ctx);
  699. else if (ctx->filter->init_dict)
  700. ret = ctx->filter->init_dict(ctx, options);
  701. return ret;
  702. }
  703. int avfilter_init_str(AVFilterContext *filter, const char *args)
  704. {
  705. AVDictionary *options = NULL;
  706. AVDictionaryEntry *e;
  707. int ret = 0;
  708. if (args && *args) {
  709. if (!filter->filter->priv_class) {
  710. av_log(filter, AV_LOG_ERROR, "This filter does not take any "
  711. "options, but options were provided: %s.\n", args);
  712. return AVERROR(EINVAL);
  713. }
  714. #if FF_API_OLD_FILTER_OPTS
  715. if ( !strcmp(filter->filter->name, "format") ||
  716. !strcmp(filter->filter->name, "noformat") ||
  717. !strcmp(filter->filter->name, "frei0r") ||
  718. !strcmp(filter->filter->name, "frei0r_src") ||
  719. !strcmp(filter->filter->name, "ocv") ||
  720. !strcmp(filter->filter->name, "pan") ||
  721. !strcmp(filter->filter->name, "pp") ||
  722. !strcmp(filter->filter->name, "aevalsrc")) {
  723. /* a hack for compatibility with the old syntax
  724. * replace colons with |s */
  725. char *copy = av_strdup(args);
  726. char *p = copy;
  727. int nb_leading = 0; // number of leading colons to skip
  728. int deprecated = 0;
  729. if (!copy) {
  730. ret = AVERROR(ENOMEM);
  731. goto fail;
  732. }
  733. if (!strcmp(filter->filter->name, "frei0r") ||
  734. !strcmp(filter->filter->name, "ocv"))
  735. nb_leading = 1;
  736. else if (!strcmp(filter->filter->name, "frei0r_src"))
  737. nb_leading = 3;
  738. while (nb_leading--) {
  739. p = strchr(p, ':');
  740. if (!p) {
  741. p = copy + strlen(copy);
  742. break;
  743. }
  744. p++;
  745. }
  746. deprecated = strchr(p, ':') != NULL;
  747. if (!strcmp(filter->filter->name, "aevalsrc")) {
  748. deprecated = 0;
  749. while ((p = strchr(p, ':')) && p[1] != ':') {
  750. const char *epos = strchr(p + 1, '=');
  751. const char *spos = strchr(p + 1, ':');
  752. const int next_token_is_opt = epos && (!spos || epos < spos);
  753. if (next_token_is_opt) {
  754. p++;
  755. break;
  756. }
  757. /* next token does not contain a '=', assume a channel expression */
  758. deprecated = 1;
  759. *p++ = '|';
  760. }
  761. if (p && *p == ':') { // double sep '::' found
  762. deprecated = 1;
  763. memmove(p, p + 1, strlen(p));
  764. }
  765. } else
  766. while ((p = strchr(p, ':')))
  767. *p++ = '|';
  768. if (deprecated)
  769. av_log(filter, AV_LOG_WARNING, "This syntax is deprecated. Use "
  770. "'|' to separate the list items.\n");
  771. av_log(filter, AV_LOG_DEBUG, "compat: called with args=[%s]\n", copy);
  772. ret = process_options(filter, &options, copy);
  773. av_freep(&copy);
  774. if (ret < 0)
  775. goto fail;
  776. #endif
  777. } else {
  778. #if CONFIG_MP_FILTER
  779. if (!strcmp(filter->filter->name, "mp")) {
  780. char *escaped;
  781. if (!strncmp(args, "filter=", 7))
  782. args += 7;
  783. ret = av_escape(&escaped, args, ":=", AV_ESCAPE_MODE_BACKSLASH, 0);
  784. if (ret < 0) {
  785. av_log(filter, AV_LOG_ERROR, "Unable to escape MPlayer filters arg '%s'\n", args);
  786. goto fail;
  787. }
  788. ret = process_options(filter, &options, escaped);
  789. av_free(escaped);
  790. } else
  791. #endif
  792. ret = process_options(filter, &options, args);
  793. if (ret < 0)
  794. goto fail;
  795. }
  796. }
  797. ret = avfilter_init_dict(filter, &options);
  798. if (ret < 0)
  799. goto fail;
  800. if ((e = av_dict_get(options, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
  801. av_log(filter, AV_LOG_ERROR, "No such option: %s.\n", e->key);
  802. ret = AVERROR_OPTION_NOT_FOUND;
  803. goto fail;
  804. }
  805. fail:
  806. av_dict_free(&options);
  807. return ret;
  808. }
  809. const char *avfilter_pad_get_name(const AVFilterPad *pads, int pad_idx)
  810. {
  811. return pads[pad_idx].name;
  812. }
  813. enum AVMediaType avfilter_pad_get_type(const AVFilterPad *pads, int pad_idx)
  814. {
  815. return pads[pad_idx].type;
  816. }
  817. static int default_filter_frame(AVFilterLink *link, AVFrame *frame)
  818. {
  819. return ff_filter_frame(link->dst->outputs[0], frame);
  820. }
  821. static int ff_filter_frame_framed(AVFilterLink *link, AVFrame *frame)
  822. {
  823. int (*filter_frame)(AVFilterLink *, AVFrame *);
  824. AVFilterContext *dstctx = link->dst;
  825. AVFilterPad *dst = link->dstpad;
  826. AVFrame *out;
  827. int ret;
  828. AVFilterCommand *cmd= link->dst->command_queue;
  829. int64_t pts;
  830. if (link->closed) {
  831. av_frame_free(&frame);
  832. return AVERROR_EOF;
  833. }
  834. if (!(filter_frame = dst->filter_frame))
  835. filter_frame = default_filter_frame;
  836. /* copy the frame if needed */
  837. if (dst->needs_writable && !av_frame_is_writable(frame)) {
  838. av_log(link->dst, AV_LOG_DEBUG, "Copying data in avfilter.\n");
  839. /* Maybe use ff_copy_buffer_ref instead? */
  840. switch (link->type) {
  841. case AVMEDIA_TYPE_VIDEO:
  842. out = ff_get_video_buffer(link, link->w, link->h);
  843. break;
  844. case AVMEDIA_TYPE_AUDIO:
  845. out = ff_get_audio_buffer(link, frame->nb_samples);
  846. break;
  847. default: return AVERROR(EINVAL);
  848. }
  849. if (!out) {
  850. av_frame_free(&frame);
  851. return AVERROR(ENOMEM);
  852. }
  853. av_frame_copy_props(out, frame);
  854. switch (link->type) {
  855. case AVMEDIA_TYPE_VIDEO:
  856. av_image_copy(out->data, out->linesize, (const uint8_t **)frame->data, frame->linesize,
  857. frame->format, frame->width, frame->height);
  858. break;
  859. case AVMEDIA_TYPE_AUDIO:
  860. av_samples_copy(out->extended_data, frame->extended_data,
  861. 0, 0, frame->nb_samples,
  862. av_get_channel_layout_nb_channels(frame->channel_layout),
  863. frame->format);
  864. break;
  865. default: return AVERROR(EINVAL);
  866. }
  867. av_frame_free(&frame);
  868. } else
  869. out = frame;
  870. while(cmd && cmd->time <= out->pts * av_q2d(link->time_base)){
  871. av_log(link->dst, AV_LOG_DEBUG,
  872. "Processing command time:%f command:%s arg:%s\n",
  873. cmd->time, cmd->command, cmd->arg);
  874. avfilter_process_command(link->dst, cmd->command, cmd->arg, 0, 0, cmd->flags);
  875. ff_command_queue_pop(link->dst);
  876. cmd= link->dst->command_queue;
  877. }
  878. pts = out->pts;
  879. if (dstctx->enable_str) {
  880. int64_t pos = av_frame_get_pkt_pos(out);
  881. dstctx->var_values[VAR_N] = link->frame_count;
  882. dstctx->var_values[VAR_T] = pts == AV_NOPTS_VALUE ? NAN : pts * av_q2d(link->time_base);
  883. dstctx->var_values[VAR_POS] = pos == -1 ? NAN : pos;
  884. dstctx->is_disabled = !av_expr_eval(dstctx->enable, dstctx->var_values, NULL);
  885. if (dstctx->is_disabled &&
  886. (dstctx->filter->flags & AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC))
  887. filter_frame = default_filter_frame;
  888. }
  889. ret = filter_frame(link, out);
  890. link->frame_count++;
  891. link->frame_requested = 0;
  892. ff_update_link_current_pts(link, pts);
  893. return ret;
  894. }
  895. static int ff_filter_frame_needs_framing(AVFilterLink *link, AVFrame *frame)
  896. {
  897. int insamples = frame->nb_samples, inpos = 0, nb_samples;
  898. AVFrame *pbuf = link->partial_buf;
  899. int nb_channels = av_frame_get_channels(frame);
  900. int ret = 0;
  901. link->flags |= FF_LINK_FLAG_REQUEST_LOOP;
  902. /* Handle framing (min_samples, max_samples) */
  903. while (insamples) {
  904. if (!pbuf) {
  905. AVRational samples_tb = { 1, link->sample_rate };
  906. pbuf = ff_get_audio_buffer(link, link->partial_buf_size);
  907. if (!pbuf) {
  908. av_log(link->dst, AV_LOG_WARNING,
  909. "Samples dropped due to memory allocation failure.\n");
  910. return 0;
  911. }
  912. av_frame_copy_props(pbuf, frame);
  913. pbuf->pts = frame->pts +
  914. av_rescale_q(inpos, samples_tb, link->time_base);
  915. pbuf->nb_samples = 0;
  916. }
  917. nb_samples = FFMIN(insamples,
  918. link->partial_buf_size - pbuf->nb_samples);
  919. av_samples_copy(pbuf->extended_data, frame->extended_data,
  920. pbuf->nb_samples, inpos,
  921. nb_samples, nb_channels, link->format);
  922. inpos += nb_samples;
  923. insamples -= nb_samples;
  924. pbuf->nb_samples += nb_samples;
  925. if (pbuf->nb_samples >= link->min_samples) {
  926. ret = ff_filter_frame_framed(link, pbuf);
  927. pbuf = NULL;
  928. }
  929. }
  930. av_frame_free(&frame);
  931. link->partial_buf = pbuf;
  932. return ret;
  933. }
  934. int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
  935. {
  936. FF_TPRINTF_START(NULL, filter_frame); ff_tlog_link(NULL, link, 1); ff_tlog(NULL, " "); ff_tlog_ref(NULL, frame, 1);
  937. /* Consistency checks */
  938. if (link->type == AVMEDIA_TYPE_VIDEO) {
  939. if (strcmp(link->dst->filter->name, "scale")) {
  940. av_assert1(frame->format == link->format);
  941. av_assert1(frame->width == link->w);
  942. av_assert1(frame->height == link->h);
  943. }
  944. } else {
  945. av_assert1(frame->format == link->format);
  946. av_assert1(av_frame_get_channels(frame) == link->channels);
  947. av_assert1(frame->channel_layout == link->channel_layout);
  948. av_assert1(frame->sample_rate == link->sample_rate);
  949. }
  950. /* Go directly to actual filtering if possible */
  951. if (link->type == AVMEDIA_TYPE_AUDIO &&
  952. link->min_samples &&
  953. (link->partial_buf ||
  954. frame->nb_samples < link->min_samples ||
  955. frame->nb_samples > link->max_samples)) {
  956. return ff_filter_frame_needs_framing(link, frame);
  957. } else {
  958. return ff_filter_frame_framed(link, frame);
  959. }
  960. }
  961. const AVClass *avfilter_get_class(void)
  962. {
  963. return &avfilter_class;
  964. }