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