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