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.

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