]> git.sesse.net Git - ffmpeg/blob - libavfilter/avfilter.c
cf3b4e461d0d582be443bb4cb7e822f470db9490
[ffmpeg] / libavfilter / avfilter.c
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"
24 #include "libavutil/buffer.h"
25 #include "libavutil/channel_layout.h"
26 #include "libavutil/common.h"
27 #include "libavutil/eval.h"
28 #include "libavutil/hwcontext.h"
29 #include "libavutil/imgutils.h"
30 #include "libavutil/internal.h"
31 #include "libavutil/opt.h"
32 #include "libavutil/pixdesc.h"
33 #include "libavutil/rational.h"
34 #include "libavutil/samplefmt.h"
35 #include "libavutil/thread.h"
36
37 #define FF_INTERNAL_FIELDS 1
38 #include "framequeue.h"
39
40 #include "audio.h"
41 #include "avfilter.h"
42 #include "filters.h"
43 #include "formats.h"
44 #include "internal.h"
45
46 #include "libavutil/ffversion.h"
47 const char av_filter_ffversion[] = "FFmpeg version " FFMPEG_VERSION;
48
49 void ff_tlog_ref(void *ctx, AVFrame *ref, int end)
50 {
51     av_unused char buf[16];
52     ff_tlog(ctx,
53             "ref[%p buf:%p data:%p linesize[%d, %d, %d, %d] pts:%"PRId64" pos:%"PRId64,
54             ref, ref->buf, ref->data[0],
55             ref->linesize[0], ref->linesize[1], ref->linesize[2], ref->linesize[3],
56             ref->pts, ref->pkt_pos);
57
58     if (ref->width) {
59         ff_tlog(ctx, " a:%d/%d s:%dx%d i:%c iskey:%d type:%c",
60                 ref->sample_aspect_ratio.num, ref->sample_aspect_ratio.den,
61                 ref->width, ref->height,
62                 !ref->interlaced_frame     ? 'P' :         /* Progressive  */
63                 ref->top_field_first ? 'T' : 'B',    /* Top / Bottom */
64                 ref->key_frame,
65                 av_get_picture_type_char(ref->pict_type));
66     }
67     if (ref->nb_samples) {
68         ff_tlog(ctx, " cl:%"PRId64"d n:%d r:%d",
69                 ref->channel_layout,
70                 ref->nb_samples,
71                 ref->sample_rate);
72     }
73
74     ff_tlog(ctx, "]%s", end ? "\n" : "");
75 }
76
77 unsigned avfilter_version(void)
78 {
79     av_assert0(LIBAVFILTER_VERSION_MICRO >= 100);
80     return LIBAVFILTER_VERSION_INT;
81 }
82
83 const char *avfilter_configuration(void)
84 {
85     return FFMPEG_CONFIGURATION;
86 }
87
88 const char *avfilter_license(void)
89 {
90 #define LICENSE_PREFIX "libavfilter license: "
91     return &LICENSE_PREFIX FFMPEG_LICENSE[sizeof(LICENSE_PREFIX) - 1];
92 }
93
94 void ff_command_queue_pop(AVFilterContext *filter)
95 {
96     AVFilterCommand *c= filter->command_queue;
97     av_freep(&c->arg);
98     av_freep(&c->command);
99     filter->command_queue= c->next;
100     av_free(c);
101 }
102
103 int ff_insert_pad(unsigned idx, unsigned *count, size_t padidx_off,
104                    AVFilterPad **pads, AVFilterLink ***links,
105                    AVFilterPad *newpad)
106 {
107     AVFilterLink **newlinks;
108     AVFilterPad *newpads;
109     unsigned i;
110
111     idx = FFMIN(idx, *count);
112
113     newpads  = av_realloc_array(*pads,  *count + 1, sizeof(AVFilterPad));
114     newlinks = av_realloc_array(*links, *count + 1, sizeof(AVFilterLink*));
115     if (newpads)
116         *pads  = newpads;
117     if (newlinks)
118         *links = newlinks;
119     if (!newpads || !newlinks)
120         return AVERROR(ENOMEM);
121
122     memmove(*pads  + idx + 1, *pads  + idx, sizeof(AVFilterPad)   * (*count - idx));
123     memmove(*links + idx + 1, *links + idx, sizeof(AVFilterLink*) * (*count - idx));
124     memcpy(*pads + idx, newpad, sizeof(AVFilterPad));
125     (*links)[idx] = NULL;
126
127     (*count)++;
128     for (i = idx + 1; i < *count; i++)
129         if ((*links)[i])
130             (*(unsigned *)((uint8_t *) (*links)[i] + padidx_off))++;
131
132     return 0;
133 }
134
135 int avfilter_link(AVFilterContext *src, unsigned srcpad,
136                   AVFilterContext *dst, unsigned dstpad)
137 {
138     AVFilterLink *link;
139
140     av_assert0(src->graph);
141     av_assert0(dst->graph);
142     av_assert0(src->graph == dst->graph);
143
144     if (src->nb_outputs <= srcpad || dst->nb_inputs <= dstpad ||
145         src->outputs[srcpad]      || dst->inputs[dstpad])
146         return AVERROR(EINVAL);
147
148     if (src->output_pads[srcpad].type != dst->input_pads[dstpad].type) {
149         av_log(src, AV_LOG_ERROR,
150                "Media type mismatch between the '%s' filter output pad %d (%s) and the '%s' filter input pad %d (%s)\n",
151                src->name, srcpad, (char *)av_x_if_null(av_get_media_type_string(src->output_pads[srcpad].type), "?"),
152                dst->name, dstpad, (char *)av_x_if_null(av_get_media_type_string(dst-> input_pads[dstpad].type), "?"));
153         return AVERROR(EINVAL);
154     }
155
156     link = av_mallocz(sizeof(*link));
157     if (!link)
158         return AVERROR(ENOMEM);
159
160     src->outputs[srcpad] = dst->inputs[dstpad] = link;
161
162     link->src     = src;
163     link->dst     = dst;
164     link->srcpad  = &src->output_pads[srcpad];
165     link->dstpad  = &dst->input_pads[dstpad];
166     link->type    = src->output_pads[srcpad].type;
167     av_assert0(AV_PIX_FMT_NONE == -1 && AV_SAMPLE_FMT_NONE == -1);
168     link->format  = -1;
169     ff_framequeue_init(&link->fifo, &src->graph->internal->frame_queues);
170
171     return 0;
172 }
173
174 void avfilter_link_free(AVFilterLink **link)
175 {
176     if (!*link)
177         return;
178
179     av_frame_free(&(*link)->partial_buf);
180     ff_framequeue_free(&(*link)->fifo);
181     ff_frame_pool_uninit((FFFramePool**)&(*link)->frame_pool);
182
183     av_freep(link);
184 }
185
186 #if FF_API_FILTER_GET_SET
187 int avfilter_link_get_channels(AVFilterLink *link)
188 {
189     return link->channels;
190 }
191 #endif
192
193 void ff_filter_set_ready(AVFilterContext *filter, unsigned priority)
194 {
195     filter->ready = FFMAX(filter->ready, priority);
196 }
197
198 /**
199  * Clear frame_blocked_in on all outputs.
200  * This is necessary whenever something changes on input.
201  */
202 static void filter_unblock(AVFilterContext *filter)
203 {
204     unsigned i;
205
206     for (i = 0; i < filter->nb_outputs; i++)
207         filter->outputs[i]->frame_blocked_in = 0;
208 }
209
210
211 void ff_avfilter_link_set_in_status(AVFilterLink *link, int status, int64_t pts)
212 {
213     if (link->status_in == status)
214         return;
215     av_assert0(!link->status_in);
216     link->status_in = status;
217     link->status_in_pts = pts;
218     link->frame_wanted_out = 0;
219     link->frame_blocked_in = 0;
220     filter_unblock(link->dst);
221     ff_filter_set_ready(link->dst, 200);
222 }
223
224 void ff_avfilter_link_set_out_status(AVFilterLink *link, int status, int64_t pts)
225 {
226     av_assert0(!link->frame_wanted_out);
227     av_assert0(!link->status_out);
228     link->status_out = status;
229     if (pts != AV_NOPTS_VALUE)
230         ff_update_link_current_pts(link, pts);
231     filter_unblock(link->dst);
232     ff_filter_set_ready(link->src, 200);
233 }
234
235 void avfilter_link_set_closed(AVFilterLink *link, int closed)
236 {
237     ff_avfilter_link_set_out_status(link, closed ? AVERROR_EOF : 0, AV_NOPTS_VALUE);
238 }
239
240 int avfilter_insert_filter(AVFilterLink *link, AVFilterContext *filt,
241                            unsigned filt_srcpad_idx, unsigned filt_dstpad_idx)
242 {
243     int ret;
244     unsigned dstpad_idx = link->dstpad - link->dst->input_pads;
245
246     av_log(link->dst, AV_LOG_VERBOSE, "auto-inserting filter '%s' "
247            "between the filter '%s' and the filter '%s'\n",
248            filt->name, link->src->name, link->dst->name);
249
250     link->dst->inputs[dstpad_idx] = NULL;
251     if ((ret = avfilter_link(filt, filt_dstpad_idx, link->dst, dstpad_idx)) < 0) {
252         /* failed to link output filter to new filter */
253         link->dst->inputs[dstpad_idx] = link;
254         return ret;
255     }
256
257     /* re-hookup the link to the new destination filter we inserted */
258     link->dst                     = filt;
259     link->dstpad                  = &filt->input_pads[filt_srcpad_idx];
260     filt->inputs[filt_srcpad_idx] = link;
261
262     /* if any information on supported media formats already exists on the
263      * link, we need to preserve that */
264     if (link->out_formats)
265         ff_formats_changeref(&link->out_formats,
266                              &filt->outputs[filt_dstpad_idx]->out_formats);
267     if (link->out_samplerates)
268         ff_formats_changeref(&link->out_samplerates,
269                              &filt->outputs[filt_dstpad_idx]->out_samplerates);
270     if (link->out_channel_layouts)
271         ff_channel_layouts_changeref(&link->out_channel_layouts,
272                                      &filt->outputs[filt_dstpad_idx]->out_channel_layouts);
273
274     return 0;
275 }
276
277 int avfilter_config_links(AVFilterContext *filter)
278 {
279     int (*config_link)(AVFilterLink *);
280     unsigned i;
281     int ret;
282
283     for (i = 0; i < filter->nb_inputs; i ++) {
284         AVFilterLink *link = filter->inputs[i];
285         AVFilterLink *inlink;
286
287         if (!link) continue;
288         if (!link->src || !link->dst) {
289             av_log(filter, AV_LOG_ERROR,
290                    "Not all input and output are properly linked (%d).\n", i);
291             return AVERROR(EINVAL);
292         }
293
294         inlink = link->src->nb_inputs ? link->src->inputs[0] : NULL;
295         link->current_pts =
296         link->current_pts_us = AV_NOPTS_VALUE;
297
298         switch (link->init_state) {
299         case AVLINK_INIT:
300             continue;
301         case AVLINK_STARTINIT:
302             av_log(filter, AV_LOG_INFO, "circular filter chain detected\n");
303             return 0;
304         case AVLINK_UNINIT:
305             link->init_state = AVLINK_STARTINIT;
306
307             if ((ret = avfilter_config_links(link->src)) < 0)
308                 return ret;
309
310             if (!(config_link = link->srcpad->config_props)) {
311                 if (link->src->nb_inputs != 1) {
312                     av_log(link->src, AV_LOG_ERROR, "Source filters and filters "
313                                                     "with more than one input "
314                                                     "must set config_props() "
315                                                     "callbacks on all outputs\n");
316                     return AVERROR(EINVAL);
317                 }
318             } else if ((ret = config_link(link)) < 0) {
319                 av_log(link->src, AV_LOG_ERROR,
320                        "Failed to configure output pad on %s\n",
321                        link->src->name);
322                 return ret;
323             }
324
325             switch (link->type) {
326             case AVMEDIA_TYPE_VIDEO:
327                 if (!link->time_base.num && !link->time_base.den)
328                     link->time_base = inlink ? inlink->time_base : AV_TIME_BASE_Q;
329
330                 if (!link->sample_aspect_ratio.num && !link->sample_aspect_ratio.den)
331                     link->sample_aspect_ratio = inlink ?
332                         inlink->sample_aspect_ratio : (AVRational){1,1};
333
334                 if (inlink) {
335                     if (!link->frame_rate.num && !link->frame_rate.den)
336                         link->frame_rate = inlink->frame_rate;
337                     if (!link->w)
338                         link->w = inlink->w;
339                     if (!link->h)
340                         link->h = inlink->h;
341                 } else if (!link->w || !link->h) {
342                     av_log(link->src, AV_LOG_ERROR,
343                            "Video source filters must set their output link's "
344                            "width and height\n");
345                     return AVERROR(EINVAL);
346                 }
347                 break;
348
349             case AVMEDIA_TYPE_AUDIO:
350                 if (inlink) {
351                     if (!link->time_base.num && !link->time_base.den)
352                         link->time_base = inlink->time_base;
353                 }
354
355                 if (!link->time_base.num && !link->time_base.den)
356                     link->time_base = (AVRational) {1, link->sample_rate};
357             }
358
359             if (link->src->nb_inputs && link->src->inputs[0]->hw_frames_ctx &&
360                 !(link->src->filter->flags_internal & FF_FILTER_FLAG_HWFRAME_AWARE)) {
361                 av_assert0(!link->hw_frames_ctx &&
362                            "should not be set by non-hwframe-aware filter");
363                 link->hw_frames_ctx = av_buffer_ref(link->src->inputs[0]->hw_frames_ctx);
364                 if (!link->hw_frames_ctx)
365                     return AVERROR(ENOMEM);
366             }
367
368             if ((config_link = link->dstpad->config_props))
369                 if ((ret = config_link(link)) < 0) {
370                     av_log(link->dst, AV_LOG_ERROR,
371                            "Failed to configure input pad on %s\n",
372                            link->dst->name);
373                     return ret;
374                 }
375
376             link->init_state = AVLINK_INIT;
377         }
378     }
379
380     return 0;
381 }
382
383 void ff_tlog_link(void *ctx, AVFilterLink *link, int end)
384 {
385     if (link->type == AVMEDIA_TYPE_VIDEO) {
386         ff_tlog(ctx,
387                 "link[%p s:%dx%d fmt:%s %s->%s]%s",
388                 link, link->w, link->h,
389                 av_get_pix_fmt_name(link->format),
390                 link->src ? link->src->filter->name : "",
391                 link->dst ? link->dst->filter->name : "",
392                 end ? "\n" : "");
393     } else {
394         char buf[128];
395         av_get_channel_layout_string(buf, sizeof(buf), -1, link->channel_layout);
396
397         ff_tlog(ctx,
398                 "link[%p r:%d cl:%s fmt:%s %s->%s]%s",
399                 link, (int)link->sample_rate, buf,
400                 av_get_sample_fmt_name(link->format),
401                 link->src ? link->src->filter->name : "",
402                 link->dst ? link->dst->filter->name : "",
403                 end ? "\n" : "");
404     }
405 }
406
407 int ff_request_frame(AVFilterLink *link)
408 {
409     FF_TPRINTF_START(NULL, request_frame); ff_tlog_link(NULL, link, 1);
410
411     av_assert1(!link->dst->filter->activate);
412     if (link->status_out)
413         return link->status_out;
414     if (link->status_in) {
415         if (ff_framequeue_queued_frames(&link->fifo)) {
416             av_assert1(!link->frame_wanted_out);
417             av_assert1(link->dst->ready >= 300);
418             return 0;
419         } else {
420             /* Acknowledge status change. Filters using ff_request_frame() will
421                handle the change automatically. Filters can also check the
422                status directly but none do yet. */
423             ff_avfilter_link_set_out_status(link, link->status_in, link->status_in_pts);
424             return link->status_out;
425         }
426     }
427     link->frame_wanted_out = 1;
428     ff_filter_set_ready(link->src, 100);
429     return 0;
430 }
431
432 static int64_t guess_status_pts(AVFilterContext *ctx, int status, AVRational link_time_base)
433 {
434     unsigned i;
435     int64_t r = INT64_MAX;
436
437     for (i = 0; i < ctx->nb_inputs; i++)
438         if (ctx->inputs[i]->status_out == status)
439             r = FFMIN(r, av_rescale_q(ctx->inputs[i]->current_pts, ctx->inputs[i]->time_base, link_time_base));
440     if (r < INT64_MAX)
441         return r;
442     av_log(ctx, AV_LOG_WARNING, "EOF timestamp not reliable\n");
443     for (i = 0; i < ctx->nb_inputs; i++)
444         r = FFMIN(r, av_rescale_q(ctx->inputs[i]->status_in_pts, ctx->inputs[i]->time_base, link_time_base));
445     if (r < INT64_MAX)
446         return r;
447     return AV_NOPTS_VALUE;
448 }
449
450 static int ff_request_frame_to_filter(AVFilterLink *link)
451 {
452     int ret = -1;
453
454     FF_TPRINTF_START(NULL, request_frame_to_filter); ff_tlog_link(NULL, link, 1);
455     /* Assume the filter is blocked, let the method clear it if not */
456     link->frame_blocked_in = 1;
457     if (link->srcpad->request_frame)
458         ret = link->srcpad->request_frame(link);
459     else if (link->src->inputs[0])
460         ret = ff_request_frame(link->src->inputs[0]);
461     if (ret < 0) {
462         if (ret != AVERROR(EAGAIN) && ret != link->status_in)
463             ff_avfilter_link_set_in_status(link, ret, guess_status_pts(link->src, ret, link->time_base));
464         if (ret == AVERROR_EOF)
465             ret = 0;
466     }
467     return ret;
468 }
469
470 static const char *const var_names[] = {
471     "t",
472     "n",
473     "pos",
474     "w",
475     "h",
476     NULL
477 };
478
479 enum {
480     VAR_T,
481     VAR_N,
482     VAR_POS,
483     VAR_W,
484     VAR_H,
485     VAR_VARS_NB
486 };
487
488 static int set_enable_expr(AVFilterContext *ctx, const char *expr)
489 {
490     int ret;
491     char *expr_dup;
492     AVExpr *old = ctx->enable;
493
494     if (!(ctx->filter->flags & AVFILTER_FLAG_SUPPORT_TIMELINE)) {
495         av_log(ctx, AV_LOG_ERROR, "Timeline ('enable' option) not supported "
496                "with filter '%s'\n", ctx->filter->name);
497         return AVERROR_PATCHWELCOME;
498     }
499
500     expr_dup = av_strdup(expr);
501     if (!expr_dup)
502         return AVERROR(ENOMEM);
503
504     if (!ctx->var_values) {
505         ctx->var_values = av_calloc(VAR_VARS_NB, sizeof(*ctx->var_values));
506         if (!ctx->var_values) {
507             av_free(expr_dup);
508             return AVERROR(ENOMEM);
509         }
510     }
511
512     ret = av_expr_parse((AVExpr**)&ctx->enable, expr_dup, var_names,
513                         NULL, NULL, NULL, NULL, 0, ctx->priv);
514     if (ret < 0) {
515         av_log(ctx->priv, AV_LOG_ERROR,
516                "Error when evaluating the expression '%s' for enable\n",
517                expr_dup);
518         av_free(expr_dup);
519         return ret;
520     }
521
522     av_expr_free(old);
523     av_free(ctx->enable_str);
524     ctx->enable_str = expr_dup;
525     return 0;
526 }
527
528 void ff_update_link_current_pts(AVFilterLink *link, int64_t pts)
529 {
530     if (pts == AV_NOPTS_VALUE)
531         return;
532     link->current_pts = pts;
533     link->current_pts_us = av_rescale_q(pts, link->time_base, AV_TIME_BASE_Q);
534     /* TODO use duration */
535     if (link->graph && link->age_index >= 0)
536         ff_avfilter_graph_update_heap(link->graph, link);
537 }
538
539 int avfilter_process_command(AVFilterContext *filter, const char *cmd, const char *arg, char *res, int res_len, int flags)
540 {
541     if(!strcmp(cmd, "ping")){
542         char local_res[256] = {0};
543
544         if (!res) {
545             res = local_res;
546             res_len = sizeof(local_res);
547         }
548         av_strlcatf(res, res_len, "pong from:%s %s\n", filter->filter->name, filter->name);
549         if (res == local_res)
550             av_log(filter, AV_LOG_INFO, "%s", res);
551         return 0;
552     }else if(!strcmp(cmd, "enable")) {
553         return set_enable_expr(filter, arg);
554     }else if(filter->filter->process_command) {
555         return filter->filter->process_command(filter, cmd, arg, res, res_len, flags);
556     }
557     return AVERROR(ENOSYS);
558 }
559
560 int avfilter_pad_count(const AVFilterPad *pads)
561 {
562     int count;
563
564     if (!pads)
565         return 0;
566
567     for (count = 0; pads->name; count++)
568         pads++;
569     return count;
570 }
571
572 static const char *default_filter_name(void *filter_ctx)
573 {
574     AVFilterContext *ctx = filter_ctx;
575     return ctx->name ? ctx->name : ctx->filter->name;
576 }
577
578 static void *filter_child_next(void *obj, void *prev)
579 {
580     AVFilterContext *ctx = obj;
581     if (!prev && ctx->filter && ctx->filter->priv_class && ctx->priv)
582         return ctx->priv;
583     return NULL;
584 }
585
586 #if FF_API_CHILD_CLASS_NEXT
587 static const AVClass *filter_child_class_next(const AVClass *prev)
588 {
589     void *opaque = NULL;
590     const AVFilter *f = NULL;
591
592     /* find the filter that corresponds to prev */
593     while (prev && (f = av_filter_iterate(&opaque)))
594         if (f->priv_class == prev)
595             break;
596
597     /* could not find filter corresponding to prev */
598     if (prev && !f)
599         return NULL;
600
601     /* find next filter with specific options */
602     while ((f = av_filter_iterate(&opaque)))
603         if (f->priv_class)
604             return f->priv_class;
605
606     return NULL;
607 }
608 #endif
609
610 static const AVClass *filter_child_class_iterate(void **iter)
611 {
612     const AVFilter *f;
613
614     while ((f = av_filter_iterate(iter)))
615         if (f->priv_class)
616             return f->priv_class;
617
618     return NULL;
619 }
620
621 #define OFFSET(x) offsetof(AVFilterContext, x)
622 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM
623 static const AVOption avfilter_options[] = {
624     { "thread_type", "Allowed thread types", OFFSET(thread_type), AV_OPT_TYPE_FLAGS,
625         { .i64 = AVFILTER_THREAD_SLICE }, 0, INT_MAX, FLAGS, "thread_type" },
626         { "slice", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AVFILTER_THREAD_SLICE }, .flags = FLAGS, .unit = "thread_type" },
627     { "enable", "set enable expression", OFFSET(enable_str), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
628     { "threads", "Allowed number of threads", OFFSET(nb_threads), AV_OPT_TYPE_INT,
629         { .i64 = 0 }, 0, INT_MAX, FLAGS },
630     { "extra_hw_frames", "Number of extra hardware frames to allocate for the user",
631         OFFSET(extra_hw_frames), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
632     { NULL },
633 };
634
635 static const AVClass avfilter_class = {
636     .class_name = "AVFilter",
637     .item_name  = default_filter_name,
638     .version    = LIBAVUTIL_VERSION_INT,
639     .category   = AV_CLASS_CATEGORY_FILTER,
640     .child_next = filter_child_next,
641 #if FF_API_CHILD_CLASS_NEXT
642     .child_class_next = filter_child_class_next,
643 #endif
644     .child_class_iterate = filter_child_class_iterate,
645     .option           = avfilter_options,
646 };
647
648 static int default_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg,
649                            int *ret, int nb_jobs)
650 {
651     int i;
652
653     for (i = 0; i < nb_jobs; i++) {
654         int r = func(ctx, arg, i, nb_jobs);
655         if (ret)
656             ret[i] = r;
657     }
658     return 0;
659 }
660
661 AVFilterContext *ff_filter_alloc(const AVFilter *filter, const char *inst_name)
662 {
663     AVFilterContext *ret;
664     int preinited = 0;
665
666     if (!filter)
667         return NULL;
668
669     ret = av_mallocz(sizeof(AVFilterContext));
670     if (!ret)
671         return NULL;
672
673     ret->av_class = &avfilter_class;
674     ret->filter   = filter;
675     ret->name     = inst_name ? av_strdup(inst_name) : NULL;
676     if (filter->priv_size) {
677         ret->priv     = av_mallocz(filter->priv_size);
678         if (!ret->priv)
679             goto err;
680     }
681     if (filter->preinit) {
682         if (filter->preinit(ret) < 0)
683             goto err;
684         preinited = 1;
685     }
686
687     av_opt_set_defaults(ret);
688     if (filter->priv_class) {
689         *(const AVClass**)ret->priv = filter->priv_class;
690         av_opt_set_defaults(ret->priv);
691     }
692
693     ret->internal = av_mallocz(sizeof(*ret->internal));
694     if (!ret->internal)
695         goto err;
696     ret->internal->execute = default_execute;
697
698     ret->nb_inputs = avfilter_pad_count(filter->inputs);
699     if (ret->nb_inputs ) {
700         ret->input_pads   = av_malloc_array(ret->nb_inputs, sizeof(AVFilterPad));
701         if (!ret->input_pads)
702             goto err;
703         memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad) * ret->nb_inputs);
704         ret->inputs       = av_mallocz_array(ret->nb_inputs, sizeof(AVFilterLink*));
705         if (!ret->inputs)
706             goto err;
707     }
708
709     ret->nb_outputs = avfilter_pad_count(filter->outputs);
710     if (ret->nb_outputs) {
711         ret->output_pads  = av_malloc_array(ret->nb_outputs, sizeof(AVFilterPad));
712         if (!ret->output_pads)
713             goto err;
714         memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad) * ret->nb_outputs);
715         ret->outputs      = av_mallocz_array(ret->nb_outputs, sizeof(AVFilterLink*));
716         if (!ret->outputs)
717             goto err;
718     }
719
720     return ret;
721
722 err:
723     if (preinited)
724         filter->uninit(ret);
725     av_freep(&ret->inputs);
726     av_freep(&ret->input_pads);
727     ret->nb_inputs = 0;
728     av_freep(&ret->outputs);
729     av_freep(&ret->output_pads);
730     ret->nb_outputs = 0;
731     av_freep(&ret->priv);
732     av_freep(&ret->internal);
733     av_free(ret);
734     return NULL;
735 }
736
737 static void free_link(AVFilterLink *link)
738 {
739     if (!link)
740         return;
741
742     if (link->src)
743         link->src->outputs[link->srcpad - link->src->output_pads] = NULL;
744     if (link->dst)
745         link->dst->inputs[link->dstpad - link->dst->input_pads] = NULL;
746
747     av_buffer_unref(&link->hw_frames_ctx);
748
749     ff_formats_unref(&link->in_formats);
750     ff_formats_unref(&link->out_formats);
751     ff_formats_unref(&link->in_samplerates);
752     ff_formats_unref(&link->out_samplerates);
753     ff_channel_layouts_unref(&link->in_channel_layouts);
754     ff_channel_layouts_unref(&link->out_channel_layouts);
755     avfilter_link_free(&link);
756 }
757
758 void avfilter_free(AVFilterContext *filter)
759 {
760     int i;
761
762     if (!filter)
763         return;
764
765     if (filter->graph)
766         ff_filter_graph_remove_filter(filter->graph, filter);
767
768     if (filter->filter->uninit)
769         filter->filter->uninit(filter);
770
771     for (i = 0; i < filter->nb_inputs; i++) {
772         free_link(filter->inputs[i]);
773     }
774     for (i = 0; i < filter->nb_outputs; i++) {
775         free_link(filter->outputs[i]);
776     }
777
778     if (filter->filter->priv_class)
779         av_opt_free(filter->priv);
780
781     av_buffer_unref(&filter->hw_device_ctx);
782
783     av_freep(&filter->name);
784     av_freep(&filter->input_pads);
785     av_freep(&filter->output_pads);
786     av_freep(&filter->inputs);
787     av_freep(&filter->outputs);
788     av_freep(&filter->priv);
789     while(filter->command_queue){
790         ff_command_queue_pop(filter);
791     }
792     av_opt_free(filter);
793     av_expr_free(filter->enable);
794     filter->enable = NULL;
795     av_freep(&filter->var_values);
796     av_freep(&filter->internal);
797     av_free(filter);
798 }
799
800 int ff_filter_get_nb_threads(AVFilterContext *ctx)
801 {
802     if (ctx->nb_threads > 0)
803         return FFMIN(ctx->nb_threads, ctx->graph->nb_threads);
804     return ctx->graph->nb_threads;
805 }
806
807 static int process_options(AVFilterContext *ctx, AVDictionary **options,
808                            const char *args)
809 {
810     const AVOption *o = NULL;
811     int ret, count = 0;
812     char *av_uninit(parsed_key), *av_uninit(value);
813     const char *key;
814     int offset= -1;
815
816     if (!args)
817         return 0;
818
819     while (*args) {
820         const char *shorthand = NULL;
821
822         o = av_opt_next(ctx->priv, o);
823         if (o) {
824             if (o->type == AV_OPT_TYPE_CONST || o->offset == offset)
825                 continue;
826             offset = o->offset;
827             shorthand = o->name;
828         }
829
830         ret = av_opt_get_key_value(&args, "=", ":",
831                                    shorthand ? AV_OPT_FLAG_IMPLICIT_KEY : 0,
832                                    &parsed_key, &value);
833         if (ret < 0) {
834             if (ret == AVERROR(EINVAL))
835                 av_log(ctx, AV_LOG_ERROR, "No option name near '%s'\n", args);
836             else
837                 av_log(ctx, AV_LOG_ERROR, "Unable to parse '%s': %s\n", args,
838                        av_err2str(ret));
839             return ret;
840         }
841         if (*args)
842             args++;
843         if (parsed_key) {
844             key = parsed_key;
845             while ((o = av_opt_next(ctx->priv, o))); /* discard all remaining shorthand */
846         } else {
847             key = shorthand;
848         }
849
850         av_log(ctx, AV_LOG_DEBUG, "Setting '%s' to value '%s'\n", key, value);
851
852         if (av_opt_find(ctx, key, NULL, 0, 0)) {
853             ret = av_opt_set(ctx, key, value, 0);
854             if (ret < 0) {
855                 av_free(value);
856                 av_free(parsed_key);
857                 return ret;
858             }
859         } else {
860             av_dict_set(options, key, value, 0);
861             if ((ret = av_opt_set(ctx->priv, key, value, AV_OPT_SEARCH_CHILDREN)) < 0) {
862                 if (!av_opt_find(ctx->priv, key, NULL, 0, AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)) {
863                     if (ret == AVERROR_OPTION_NOT_FOUND)
864                         av_log(ctx, AV_LOG_ERROR, "Option '%s' not found\n", key);
865                     av_free(value);
866                     av_free(parsed_key);
867                     return ret;
868                 }
869             }
870         }
871
872         av_free(value);
873         av_free(parsed_key);
874         count++;
875     }
876
877     if (ctx->enable_str) {
878         ret = set_enable_expr(ctx, ctx->enable_str);
879         if (ret < 0)
880             return ret;
881     }
882     return count;
883 }
884
885 int ff_filter_process_command(AVFilterContext *ctx, const char *cmd,
886                               const char *arg, char *res, int res_len, int flags)
887 {
888     const AVOption *o;
889
890     if (!ctx->filter->priv_class)
891         return 0;
892     o = av_opt_find2(ctx->priv, cmd, NULL, AV_OPT_FLAG_RUNTIME_PARAM | AV_OPT_FLAG_FILTERING_PARAM, AV_OPT_SEARCH_CHILDREN, NULL);
893     if (!o)
894         return AVERROR(ENOSYS);
895     return av_opt_set(ctx->priv, cmd, arg, 0);
896 }
897
898 int avfilter_init_dict(AVFilterContext *ctx, AVDictionary **options)
899 {
900     int ret = 0;
901
902     ret = av_opt_set_dict(ctx, options);
903     if (ret < 0) {
904         av_log(ctx, AV_LOG_ERROR, "Error applying generic filter options.\n");
905         return ret;
906     }
907
908     if (ctx->filter->flags & AVFILTER_FLAG_SLICE_THREADS &&
909         ctx->thread_type & ctx->graph->thread_type & AVFILTER_THREAD_SLICE &&
910         ctx->graph->internal->thread_execute) {
911         ctx->thread_type       = AVFILTER_THREAD_SLICE;
912         ctx->internal->execute = ctx->graph->internal->thread_execute;
913     } else {
914         ctx->thread_type = 0;
915     }
916
917     if (ctx->filter->priv_class) {
918         ret = av_opt_set_dict2(ctx->priv, options, AV_OPT_SEARCH_CHILDREN);
919         if (ret < 0) {
920             av_log(ctx, AV_LOG_ERROR, "Error applying options to the filter.\n");
921             return ret;
922         }
923     }
924
925     if (ctx->filter->init_opaque)
926         ret = ctx->filter->init_opaque(ctx, NULL);
927     else if (ctx->filter->init)
928         ret = ctx->filter->init(ctx);
929     else if (ctx->filter->init_dict)
930         ret = ctx->filter->init_dict(ctx, options);
931
932     return ret;
933 }
934
935 int avfilter_init_str(AVFilterContext *filter, const char *args)
936 {
937     AVDictionary *options = NULL;
938     AVDictionaryEntry *e;
939     int ret = 0;
940
941     if (args && *args) {
942         if (!filter->filter->priv_class) {
943             av_log(filter, AV_LOG_ERROR, "This filter does not take any "
944                    "options, but options were provided: %s.\n", args);
945             return AVERROR(EINVAL);
946         }
947
948 #if FF_API_OLD_FILTER_OPTS_ERROR
949             if (   !strcmp(filter->filter->name, "format")     ||
950                    !strcmp(filter->filter->name, "noformat")   ||
951                    !strcmp(filter->filter->name, "frei0r")     ||
952                    !strcmp(filter->filter->name, "frei0r_src") ||
953                    !strcmp(filter->filter->name, "ocv")        ||
954                    !strcmp(filter->filter->name, "pan")        ||
955                    !strcmp(filter->filter->name, "pp")         ||
956                    !strcmp(filter->filter->name, "aevalsrc")) {
957             /* a hack for compatibility with the old syntax
958              * replace colons with |s */
959             char *copy = av_strdup(args);
960             char *p    = copy;
961             int nb_leading = 0; // number of leading colons to skip
962             int deprecated = 0;
963
964             if (!copy) {
965                 ret = AVERROR(ENOMEM);
966                 goto fail;
967             }
968
969             if (!strcmp(filter->filter->name, "frei0r") ||
970                 !strcmp(filter->filter->name, "ocv"))
971                 nb_leading = 1;
972             else if (!strcmp(filter->filter->name, "frei0r_src"))
973                 nb_leading = 3;
974
975             while (nb_leading--) {
976                 p = strchr(p, ':');
977                 if (!p) {
978                     p = copy + strlen(copy);
979                     break;
980                 }
981                 p++;
982             }
983
984             deprecated = strchr(p, ':') != NULL;
985
986             if (!strcmp(filter->filter->name, "aevalsrc")) {
987                 deprecated = 0;
988                 while ((p = strchr(p, ':')) && p[1] != ':') {
989                     const char *epos = strchr(p + 1, '=');
990                     const char *spos = strchr(p + 1, ':');
991                     const int next_token_is_opt = epos && (!spos || epos < spos);
992                     if (next_token_is_opt) {
993                         p++;
994                         break;
995                     }
996                     /* next token does not contain a '=', assume a channel expression */
997                     deprecated = 1;
998                     *p++ = '|';
999                 }
1000                 if (p && *p == ':') { // double sep '::' found
1001                     deprecated = 1;
1002                     memmove(p, p + 1, strlen(p));
1003                 }
1004             } else
1005             while ((p = strchr(p, ':')))
1006                 *p++ = '|';
1007
1008             if (deprecated) {
1009                 av_log(filter, AV_LOG_ERROR, "This syntax is deprecated. Use "
1010                        "'|' to separate the list items ('%s' instead of '%s')\n",
1011                        copy, args);
1012                 ret = AVERROR(EINVAL);
1013             } else {
1014                 ret = process_options(filter, &options, copy);
1015             }
1016             av_freep(&copy);
1017
1018             if (ret < 0)
1019                 goto fail;
1020         } else
1021 #endif
1022         {
1023             ret = process_options(filter, &options, args);
1024             if (ret < 0)
1025                 goto fail;
1026         }
1027     }
1028
1029     ret = avfilter_init_dict(filter, &options);
1030     if (ret < 0)
1031         goto fail;
1032
1033     if ((e = av_dict_get(options, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
1034         av_log(filter, AV_LOG_ERROR, "No such option: %s.\n", e->key);
1035         ret = AVERROR_OPTION_NOT_FOUND;
1036         goto fail;
1037     }
1038
1039 fail:
1040     av_dict_free(&options);
1041
1042     return ret;
1043 }
1044
1045 const char *avfilter_pad_get_name(const AVFilterPad *pads, int pad_idx)
1046 {
1047     return pads[pad_idx].name;
1048 }
1049
1050 enum AVMediaType avfilter_pad_get_type(const AVFilterPad *pads, int pad_idx)
1051 {
1052     return pads[pad_idx].type;
1053 }
1054
1055 static int default_filter_frame(AVFilterLink *link, AVFrame *frame)
1056 {
1057     return ff_filter_frame(link->dst->outputs[0], frame);
1058 }
1059
1060 static int ff_filter_frame_framed(AVFilterLink *link, AVFrame *frame)
1061 {
1062     int (*filter_frame)(AVFilterLink *, AVFrame *);
1063     AVFilterContext *dstctx = link->dst;
1064     AVFilterPad *dst = link->dstpad;
1065     int ret;
1066
1067     if (!(filter_frame = dst->filter_frame))
1068         filter_frame = default_filter_frame;
1069
1070     if (dst->needs_writable) {
1071         ret = ff_inlink_make_frame_writable(link, &frame);
1072         if (ret < 0)
1073             goto fail;
1074     }
1075
1076     ff_inlink_process_commands(link, frame);
1077     dstctx->is_disabled = !ff_inlink_evaluate_timeline_at_frame(link, frame);
1078
1079     if (dstctx->is_disabled &&
1080         (dstctx->filter->flags & AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC))
1081         filter_frame = default_filter_frame;
1082     ret = filter_frame(link, frame);
1083     link->frame_count_out++;
1084     return ret;
1085
1086 fail:
1087     av_frame_free(&frame);
1088     return ret;
1089 }
1090
1091 int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
1092 {
1093     int ret;
1094     FF_TPRINTF_START(NULL, filter_frame); ff_tlog_link(NULL, link, 1); ff_tlog(NULL, " "); ff_tlog_ref(NULL, frame, 1);
1095
1096     /* Consistency checks */
1097     if (link->type == AVMEDIA_TYPE_VIDEO) {
1098         if (strcmp(link->dst->filter->name, "buffersink") &&
1099             strcmp(link->dst->filter->name, "format") &&
1100             strcmp(link->dst->filter->name, "idet") &&
1101             strcmp(link->dst->filter->name, "null") &&
1102             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         if (frame->format != link->format) {
1109             av_log(link->dst, AV_LOG_ERROR, "Format change is not supported\n");
1110             goto error;
1111         }
1112         if (frame->channels != link->channels) {
1113             av_log(link->dst, AV_LOG_ERROR, "Channel count change is not supported\n");
1114             goto error;
1115         }
1116         if (frame->channel_layout != link->channel_layout) {
1117             av_log(link->dst, AV_LOG_ERROR, "Channel layout change is not supported\n");
1118             goto error;
1119         }
1120         if (frame->sample_rate != link->sample_rate) {
1121             av_log(link->dst, AV_LOG_ERROR, "Sample rate change is not supported\n");
1122             goto error;
1123         }
1124     }
1125
1126     link->frame_blocked_in = link->frame_wanted_out = 0;
1127     link->frame_count_in++;
1128     filter_unblock(link->dst);
1129     ret = ff_framequeue_add(&link->fifo, frame);
1130     if (ret < 0) {
1131         av_frame_free(&frame);
1132         return ret;
1133     }
1134     ff_filter_set_ready(link->dst, 300);
1135     return 0;
1136
1137 error:
1138     av_frame_free(&frame);
1139     return AVERROR_PATCHWELCOME;
1140 }
1141
1142 static int samples_ready(AVFilterLink *link, unsigned min)
1143 {
1144     return ff_framequeue_queued_frames(&link->fifo) &&
1145            (ff_framequeue_queued_samples(&link->fifo) >= min ||
1146             link->status_in);
1147 }
1148
1149 static int take_samples(AVFilterLink *link, unsigned min, unsigned max,
1150                         AVFrame **rframe)
1151 {
1152     AVFrame *frame0, *frame, *buf;
1153     unsigned nb_samples, nb_frames, i, p;
1154     int ret;
1155
1156     /* Note: this function relies on no format changes and must only be
1157        called with enough samples. */
1158     av_assert1(samples_ready(link, link->min_samples));
1159     frame0 = frame = ff_framequeue_peek(&link->fifo, 0);
1160     if (!link->fifo.samples_skipped && frame->nb_samples >= min && frame->nb_samples <= max) {
1161         *rframe = ff_framequeue_take(&link->fifo);
1162         return 0;
1163     }
1164     nb_frames = 0;
1165     nb_samples = 0;
1166     while (1) {
1167         if (nb_samples + frame->nb_samples > max) {
1168             if (nb_samples < min)
1169                 nb_samples = max;
1170             break;
1171         }
1172         nb_samples += frame->nb_samples;
1173         nb_frames++;
1174         if (nb_frames == ff_framequeue_queued_frames(&link->fifo))
1175             break;
1176         frame = ff_framequeue_peek(&link->fifo, nb_frames);
1177     }
1178
1179     buf = ff_get_audio_buffer(link, nb_samples);
1180     if (!buf)
1181         return AVERROR(ENOMEM);
1182     ret = av_frame_copy_props(buf, frame0);
1183     if (ret < 0) {
1184         av_frame_free(&buf);
1185         return ret;
1186     }
1187     buf->pts = frame0->pts;
1188
1189     p = 0;
1190     for (i = 0; i < nb_frames; i++) {
1191         frame = ff_framequeue_take(&link->fifo);
1192         av_samples_copy(buf->extended_data, frame->extended_data, p, 0,
1193                         frame->nb_samples, link->channels, link->format);
1194         p += frame->nb_samples;
1195         av_frame_free(&frame);
1196     }
1197     if (p < nb_samples) {
1198         unsigned n = nb_samples - p;
1199         frame = ff_framequeue_peek(&link->fifo, 0);
1200         av_samples_copy(buf->extended_data, frame->extended_data, p, 0, n,
1201                         link->channels, link->format);
1202         ff_framequeue_skip_samples(&link->fifo, n, link->time_base);
1203     }
1204
1205     *rframe = buf;
1206     return 0;
1207 }
1208
1209 static int ff_filter_frame_to_filter(AVFilterLink *link)
1210 {
1211     AVFrame *frame = NULL;
1212     AVFilterContext *dst = link->dst;
1213     int ret;
1214
1215     av_assert1(ff_framequeue_queued_frames(&link->fifo));
1216     ret = link->min_samples ?
1217           ff_inlink_consume_samples(link, link->min_samples, link->max_samples, &frame) :
1218           ff_inlink_consume_frame(link, &frame);
1219     av_assert1(ret);
1220     if (ret < 0) {
1221         av_assert1(!frame);
1222         return ret;
1223     }
1224     /* The filter will soon have received a new frame, that may allow it to
1225        produce one or more: unblock its outputs. */
1226     filter_unblock(dst);
1227     /* AVFilterPad.filter_frame() expect frame_count_out to have the value
1228        before the frame; ff_filter_frame_framed() will re-increment it. */
1229     link->frame_count_out--;
1230     ret = ff_filter_frame_framed(link, frame);
1231     if (ret < 0 && ret != link->status_out) {
1232         ff_avfilter_link_set_out_status(link, ret, AV_NOPTS_VALUE);
1233     } else {
1234         /* Run once again, to see if several frames were available, or if
1235            the input status has also changed, or any other reason. */
1236         ff_filter_set_ready(dst, 300);
1237     }
1238     return ret;
1239 }
1240
1241 static int forward_status_change(AVFilterContext *filter, AVFilterLink *in)
1242 {
1243     unsigned out = 0, progress = 0;
1244     int ret;
1245
1246     av_assert0(!in->status_out);
1247     if (!filter->nb_outputs) {
1248         /* not necessary with the current API and sinks */
1249         return 0;
1250     }
1251     while (!in->status_out) {
1252         if (!filter->outputs[out]->status_in) {
1253             progress++;
1254             ret = ff_request_frame_to_filter(filter->outputs[out]);
1255             if (ret < 0)
1256                 return ret;
1257         }
1258         if (++out == filter->nb_outputs) {
1259             if (!progress) {
1260                 /* Every output already closed: input no longer interesting
1261                    (example: overlay in shortest mode, other input closed). */
1262                 ff_avfilter_link_set_out_status(in, in->status_in, in->status_in_pts);
1263                 return 0;
1264             }
1265             progress = 0;
1266             out = 0;
1267         }
1268     }
1269     ff_filter_set_ready(filter, 200);
1270     return 0;
1271 }
1272
1273 static int ff_filter_activate_default(AVFilterContext *filter)
1274 {
1275     unsigned i;
1276
1277     for (i = 0; i < filter->nb_inputs; i++) {
1278         if (samples_ready(filter->inputs[i], filter->inputs[i]->min_samples)) {
1279             return ff_filter_frame_to_filter(filter->inputs[i]);
1280         }
1281     }
1282     for (i = 0; i < filter->nb_inputs; i++) {
1283         if (filter->inputs[i]->status_in && !filter->inputs[i]->status_out) {
1284             av_assert1(!ff_framequeue_queued_frames(&filter->inputs[i]->fifo));
1285             return forward_status_change(filter, filter->inputs[i]);
1286         }
1287     }
1288     for (i = 0; i < filter->nb_outputs; i++) {
1289         if (filter->outputs[i]->frame_wanted_out &&
1290             !filter->outputs[i]->frame_blocked_in) {
1291             return ff_request_frame_to_filter(filter->outputs[i]);
1292         }
1293     }
1294     return FFERROR_NOT_READY;
1295 }
1296
1297 /*
1298    Filter scheduling and activation
1299
1300    When a filter is activated, it must:
1301    - if possible, output a frame;
1302    - else, if relevant, forward the input status change;
1303    - else, check outputs for wanted frames and forward the requests.
1304
1305    The following AVFilterLink fields are used for activation:
1306
1307    - frame_wanted_out:
1308
1309      This field indicates if a frame is needed on this input of the
1310      destination filter. A positive value indicates that a frame is needed
1311      to process queued frames or internal data or to satisfy the
1312      application; a zero value indicates that a frame is not especially
1313      needed but could be processed anyway; a negative value indicates that a
1314      frame would just be queued.
1315
1316      It is set by filters using ff_request_frame() or ff_request_no_frame(),
1317      when requested by the application through a specific API or when it is
1318      set on one of the outputs.
1319
1320      It is cleared when a frame is sent from the source using
1321      ff_filter_frame().
1322
1323      It is also cleared when a status change is sent from the source using
1324      ff_avfilter_link_set_in_status().
1325
1326    - frame_blocked_in:
1327
1328      This field means that the source filter can not generate a frame as is.
1329      Its goal is to avoid repeatedly calling the request_frame() method on
1330      the same link.
1331
1332      It is set by the framework on all outputs of a filter before activating it.
1333
1334      It is automatically cleared by ff_filter_frame().
1335
1336      It is also automatically cleared by ff_avfilter_link_set_in_status().
1337
1338      It is also cleared on all outputs (using filter_unblock()) when
1339      something happens on an input: processing a frame or changing the
1340      status.
1341
1342    - fifo:
1343
1344      Contains the frames queued on a filter input. If it contains frames and
1345      frame_wanted_out is not set, then the filter can be activated. If that
1346      result in the filter not able to use these frames, the filter must set
1347      frame_wanted_out to ask for more frames.
1348
1349    - status_in and status_in_pts:
1350
1351      Status (EOF or error code) of the link and timestamp of the status
1352      change (in link time base, same as frames) as seen from the input of
1353      the link. The status change is considered happening after the frames
1354      queued in fifo.
1355
1356      It is set by the source filter using ff_avfilter_link_set_in_status().
1357
1358    - status_out:
1359
1360      Status of the link as seen from the output of the link. The status
1361      change is considered having already happened.
1362
1363      It is set by the destination filter using
1364      ff_avfilter_link_set_out_status().
1365
1366    Filters are activated according to the ready field, set using the
1367    ff_filter_set_ready(). Eventually, a priority queue will be used.
1368    ff_filter_set_ready() is called whenever anything could cause progress to
1369    be possible. Marking a filter ready when it is not is not a problem,
1370    except for the small overhead it causes.
1371
1372    Conditions that cause a filter to be marked ready are:
1373
1374    - frames added on an input link;
1375
1376    - changes in the input or output status of an input link;
1377
1378    - requests for a frame on an output link;
1379
1380    - after any actual processing using the legacy methods (filter_frame(),
1381      and request_frame() to acknowledge status changes), to run once more
1382      and check if enough input was present for several frames.
1383
1384    Examples of scenarios to consider:
1385
1386    - buffersrc: activate if frame_wanted_out to notify the application;
1387      activate when the application adds a frame to push it immediately.
1388
1389    - testsrc: activate only if frame_wanted_out to produce and push a frame.
1390
1391    - concat (not at stitch points): can process a frame on any output.
1392      Activate if frame_wanted_out on output to forward on the corresponding
1393      input. Activate when a frame is present on input to process it
1394      immediately.
1395
1396    - framesync: needs at least one frame on each input; extra frames on the
1397      wrong input will accumulate. When a frame is first added on one input,
1398      set frame_wanted_out<0 on it to avoid getting more (would trigger
1399      testsrc) and frame_wanted_out>0 on the other to allow processing it.
1400
1401    Activation of old filters:
1402
1403    In order to activate a filter implementing the legacy filter_frame() and
1404    request_frame() methods, perform the first possible of the following
1405    actions:
1406
1407    - If an input has frames in fifo and frame_wanted_out == 0, dequeue a
1408      frame and call filter_frame().
1409
1410      Rationale: filter frames as soon as possible instead of leaving them
1411      queued; frame_wanted_out < 0 is not possible since the old API does not
1412      set it nor provides any similar feedback; frame_wanted_out > 0 happens
1413      when min_samples > 0 and there are not enough samples queued.
1414
1415    - If an input has status_in set but not status_out, try to call
1416      request_frame() on one of the outputs in the hope that it will trigger
1417      request_frame() on the input with status_in and acknowledge it. This is
1418      awkward and fragile, filters with several inputs or outputs should be
1419      updated to direct activation as soon as possible.
1420
1421    - If an output has frame_wanted_out > 0 and not frame_blocked_in, call
1422      request_frame().
1423
1424      Rationale: checking frame_blocked_in is necessary to avoid requesting
1425      repeatedly on a blocked input if another is not blocked (example:
1426      [buffersrc1][testsrc1][buffersrc2][testsrc2]concat=v=2).
1427
1428      TODO: respect needs_fifo and remove auto-inserted fifos.
1429
1430  */
1431
1432 int ff_filter_activate(AVFilterContext *filter)
1433 {
1434     int ret;
1435
1436     /* Generic timeline support is not yet implemented but should be easy */
1437     av_assert1(!(filter->filter->flags & AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC &&
1438                  filter->filter->activate));
1439     filter->ready = 0;
1440     ret = filter->filter->activate ? filter->filter->activate(filter) :
1441           ff_filter_activate_default(filter);
1442     if (ret == FFERROR_NOT_READY)
1443         ret = 0;
1444     return ret;
1445 }
1446
1447 int ff_inlink_acknowledge_status(AVFilterLink *link, int *rstatus, int64_t *rpts)
1448 {
1449     *rpts = link->current_pts;
1450     if (ff_framequeue_queued_frames(&link->fifo))
1451         return *rstatus = 0;
1452     if (link->status_out)
1453         return *rstatus = link->status_out;
1454     if (!link->status_in)
1455         return *rstatus = 0;
1456     *rstatus = link->status_out = link->status_in;
1457     ff_update_link_current_pts(link, link->status_in_pts);
1458     *rpts = link->current_pts;
1459     return 1;
1460 }
1461
1462 size_t ff_inlink_queued_frames(AVFilterLink *link)
1463 {
1464     return ff_framequeue_queued_frames(&link->fifo);
1465 }
1466
1467 int ff_inlink_check_available_frame(AVFilterLink *link)
1468 {
1469     return ff_framequeue_queued_frames(&link->fifo) > 0;
1470 }
1471
1472 int ff_inlink_queued_samples(AVFilterLink *link)
1473 {
1474     return ff_framequeue_queued_samples(&link->fifo);
1475 }
1476
1477 int ff_inlink_check_available_samples(AVFilterLink *link, unsigned min)
1478 {
1479     uint64_t samples = ff_framequeue_queued_samples(&link->fifo);
1480     av_assert1(min);
1481     return samples >= min || (link->status_in && samples);
1482 }
1483
1484 static void consume_update(AVFilterLink *link, const AVFrame *frame)
1485 {
1486     ff_update_link_current_pts(link, frame->pts);
1487     ff_inlink_process_commands(link, frame);
1488     link->dst->is_disabled = !ff_inlink_evaluate_timeline_at_frame(link, frame);
1489     link->frame_count_out++;
1490 }
1491
1492 int ff_inlink_consume_frame(AVFilterLink *link, AVFrame **rframe)
1493 {
1494     AVFrame *frame;
1495
1496     *rframe = NULL;
1497     if (!ff_inlink_check_available_frame(link))
1498         return 0;
1499
1500     if (link->fifo.samples_skipped) {
1501         frame = ff_framequeue_peek(&link->fifo, 0);
1502         return ff_inlink_consume_samples(link, frame->nb_samples, frame->nb_samples, rframe);
1503     }
1504
1505     frame = ff_framequeue_take(&link->fifo);
1506     consume_update(link, frame);
1507     *rframe = frame;
1508     return 1;
1509 }
1510
1511 int ff_inlink_consume_samples(AVFilterLink *link, unsigned min, unsigned max,
1512                             AVFrame **rframe)
1513 {
1514     AVFrame *frame;
1515     int ret;
1516
1517     av_assert1(min);
1518     *rframe = NULL;
1519     if (!ff_inlink_check_available_samples(link, min))
1520         return 0;
1521     if (link->status_in)
1522         min = FFMIN(min, ff_framequeue_queued_samples(&link->fifo));
1523     ret = take_samples(link, min, max, &frame);
1524     if (ret < 0)
1525         return ret;
1526     consume_update(link, frame);
1527     *rframe = frame;
1528     return 1;
1529 }
1530
1531 AVFrame *ff_inlink_peek_frame(AVFilterLink *link, size_t idx)
1532 {
1533     return ff_framequeue_peek(&link->fifo, idx);
1534 }
1535
1536 int ff_inlink_make_frame_writable(AVFilterLink *link, AVFrame **rframe)
1537 {
1538     AVFrame *frame = *rframe;
1539     AVFrame *out;
1540     int ret;
1541
1542     if (av_frame_is_writable(frame))
1543         return 0;
1544     av_log(link->dst, AV_LOG_DEBUG, "Copying data in avfilter.\n");
1545
1546     switch (link->type) {
1547     case AVMEDIA_TYPE_VIDEO:
1548         out = ff_get_video_buffer(link, link->w, link->h);
1549         break;
1550     case AVMEDIA_TYPE_AUDIO:
1551         out = ff_get_audio_buffer(link, frame->nb_samples);
1552         break;
1553     default:
1554         return AVERROR(EINVAL);
1555     }
1556     if (!out)
1557         return AVERROR(ENOMEM);
1558
1559     ret = av_frame_copy_props(out, frame);
1560     if (ret < 0) {
1561         av_frame_free(&out);
1562         return ret;
1563     }
1564
1565     switch (link->type) {
1566     case AVMEDIA_TYPE_VIDEO:
1567         av_image_copy(out->data, out->linesize, (const uint8_t **)frame->data, frame->linesize,
1568                       frame->format, frame->width, frame->height);
1569         break;
1570     case AVMEDIA_TYPE_AUDIO:
1571         av_samples_copy(out->extended_data, frame->extended_data,
1572                         0, 0, frame->nb_samples,
1573                         frame->channels,
1574                         frame->format);
1575         break;
1576     default:
1577         av_assert0(!"reached");
1578     }
1579
1580     av_frame_free(&frame);
1581     *rframe = out;
1582     return 0;
1583 }
1584
1585 int ff_inlink_process_commands(AVFilterLink *link, const AVFrame *frame)
1586 {
1587     AVFilterCommand *cmd = link->dst->command_queue;
1588
1589     while(cmd && cmd->time <= frame->pts * av_q2d(link->time_base)){
1590         av_log(link->dst, AV_LOG_DEBUG,
1591                "Processing command time:%f command:%s arg:%s\n",
1592                cmd->time, cmd->command, cmd->arg);
1593         avfilter_process_command(link->dst, cmd->command, cmd->arg, 0, 0, cmd->flags);
1594         ff_command_queue_pop(link->dst);
1595         cmd= link->dst->command_queue;
1596     }
1597     return 0;
1598 }
1599
1600 int ff_inlink_evaluate_timeline_at_frame(AVFilterLink *link, const AVFrame *frame)
1601 {
1602     AVFilterContext *dstctx = link->dst;
1603     int64_t pts = frame->pts;
1604     int64_t pos = frame->pkt_pos;
1605
1606     if (!dstctx->enable_str)
1607         return 1;
1608
1609     dstctx->var_values[VAR_N] = link->frame_count_out;
1610     dstctx->var_values[VAR_T] = pts == AV_NOPTS_VALUE ? NAN : pts * av_q2d(link->time_base);
1611     dstctx->var_values[VAR_W] = link->w;
1612     dstctx->var_values[VAR_H] = link->h;
1613     dstctx->var_values[VAR_POS] = pos == -1 ? NAN : pos;
1614
1615     return fabs(av_expr_eval(dstctx->enable, dstctx->var_values, NULL)) >= 0.5;
1616 }
1617
1618 void ff_inlink_request_frame(AVFilterLink *link)
1619 {
1620     av_assert1(!link->status_in);
1621     av_assert1(!link->status_out);
1622     link->frame_wanted_out = 1;
1623     ff_filter_set_ready(link->src, 100);
1624 }
1625
1626 void ff_inlink_set_status(AVFilterLink *link, int status)
1627 {
1628     if (link->status_out)
1629         return;
1630     link->frame_wanted_out = 0;
1631     link->frame_blocked_in = 0;
1632     ff_avfilter_link_set_out_status(link, status, AV_NOPTS_VALUE);
1633     while (ff_framequeue_queued_frames(&link->fifo)) {
1634            AVFrame *frame = ff_framequeue_take(&link->fifo);
1635            av_frame_free(&frame);
1636     }
1637     if (!link->status_in)
1638         link->status_in = status;
1639 }
1640
1641 int ff_outlink_get_status(AVFilterLink *link)
1642 {
1643     return link->status_in;
1644 }
1645
1646 const AVClass *avfilter_get_class(void)
1647 {
1648     return &avfilter_class;
1649 }
1650
1651 int ff_filter_init_hw_frames(AVFilterContext *avctx, AVFilterLink *link,
1652                              int default_pool_size)
1653 {
1654     AVHWFramesContext *frames;
1655
1656     // Must already be set by caller.
1657     av_assert0(link->hw_frames_ctx);
1658
1659     frames = (AVHWFramesContext*)link->hw_frames_ctx->data;
1660
1661     if (frames->initial_pool_size == 0) {
1662         // Dynamic allocation is necessarily supported.
1663     } else if (avctx->extra_hw_frames >= 0) {
1664         frames->initial_pool_size += avctx->extra_hw_frames;
1665     } else {
1666         frames->initial_pool_size = default_pool_size;
1667     }
1668
1669     return 0;
1670 }