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.

1086 lines
34KB

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