]> git.sesse.net Git - ffmpeg/blob - libavfilter/avfilter.c
Merge remote-tracking branch 'qatar/master'
[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/channel_layout.h"
25 #include "libavutil/common.h"
26 #include "libavutil/imgutils.h"
27 #include "libavutil/pixdesc.h"
28 #include "libavutil/rational.h"
29 #include "libavutil/samplefmt.h"
30
31 #include "audio.h"
32 #include "avfilter.h"
33 #include "formats.h"
34 #include "internal.h"
35 #include "audio.h"
36
37 static int ff_filter_frame_framed(AVFilterLink *link, AVFrame *frame);
38
39 void ff_tlog_ref(void *ctx, AVFrame *ref, int end)
40 {
41     av_unused char buf[16];
42     ff_tlog(ctx,
43             "ref[%p buf:%p data:%p linesize[%d, %d, %d, %d] pts:%"PRId64" pos:%"PRId64,
44             ref, ref->buf, ref->data[0],
45             ref->linesize[0], ref->linesize[1], ref->linesize[2], ref->linesize[3],
46             ref->pts, av_frame_get_pkt_pos(ref));
47
48     if (ref->width) {
49         ff_tlog(ctx, " a:%d/%d s:%dx%d i:%c iskey:%d type:%c",
50                 ref->sample_aspect_ratio.num, ref->sample_aspect_ratio.den,
51                 ref->width, ref->height,
52                 !ref->interlaced_frame     ? 'P' :         /* Progressive  */
53                 ref->top_field_first ? 'T' : 'B',    /* Top / Bottom */
54                 ref->key_frame,
55                 av_get_picture_type_char(ref->pict_type));
56     }
57     if (ref->nb_samples) {
58         ff_tlog(ctx, " cl:%"PRId64"d n:%d r:%d",
59                 ref->channel_layout,
60                 ref->nb_samples,
61                 ref->sample_rate);
62     }
63
64     ff_tlog(ctx, "]%s", end ? "\n" : "");
65 }
66
67 unsigned avfilter_version(void) {
68     av_assert0(LIBAVFILTER_VERSION_MICRO >= 100);
69     return LIBAVFILTER_VERSION_INT;
70 }
71
72 const char *avfilter_configuration(void)
73 {
74     return FFMPEG_CONFIGURATION;
75 }
76
77 const char *avfilter_license(void)
78 {
79 #define LICENSE_PREFIX "libavfilter license: "
80     return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
81 }
82
83 void ff_command_queue_pop(AVFilterContext *filter)
84 {
85     AVFilterCommand *c= filter->command_queue;
86     av_freep(&c->arg);
87     av_freep(&c->command);
88     filter->command_queue= c->next;
89     av_free(c);
90 }
91
92 void ff_insert_pad(unsigned idx, unsigned *count, size_t padidx_off,
93                    AVFilterPad **pads, AVFilterLink ***links,
94                    AVFilterPad *newpad)
95 {
96     unsigned i;
97
98     idx = FFMIN(idx, *count);
99
100     *pads  = av_realloc(*pads,  sizeof(AVFilterPad)   * (*count + 1));
101     *links = av_realloc(*links, sizeof(AVFilterLink*) * (*count + 1));
102     memmove(*pads +idx+1, *pads +idx, sizeof(AVFilterPad)   * (*count-idx));
103     memmove(*links+idx+1, *links+idx, sizeof(AVFilterLink*) * (*count-idx));
104     memcpy(*pads+idx, newpad, sizeof(AVFilterPad));
105     (*links)[idx] = NULL;
106
107     (*count)++;
108     for (i = idx+1; i < *count; i++)
109         if (*links[i])
110             (*(unsigned *)((uint8_t *) *links[i] + padidx_off))++;
111 }
112
113 int avfilter_link(AVFilterContext *src, unsigned srcpad,
114                   AVFilterContext *dst, unsigned dstpad)
115 {
116     AVFilterLink *link;
117
118     if (src->nb_outputs <= srcpad || dst->nb_inputs <= dstpad ||
119         src->outputs[srcpad]      || dst->inputs[dstpad])
120         return -1;
121
122     if (src->output_pads[srcpad].type != dst->input_pads[dstpad].type) {
123         av_log(src, AV_LOG_ERROR,
124                "Media type mismatch between the '%s' filter output pad %d (%s) and the '%s' filter input pad %d (%s)\n",
125                src->name, srcpad, (char *)av_x_if_null(av_get_media_type_string(src->output_pads[srcpad].type), "?"),
126                dst->name, dstpad, (char *)av_x_if_null(av_get_media_type_string(dst-> input_pads[dstpad].type), "?"));
127         return AVERROR(EINVAL);
128     }
129
130     src->outputs[srcpad] =
131     dst-> inputs[dstpad] = link = av_mallocz(sizeof(AVFilterLink));
132
133     link->src     = src;
134     link->dst     = dst;
135     link->srcpad  = &src->output_pads[srcpad];
136     link->dstpad  = &dst->input_pads[dstpad];
137     link->type    = src->output_pads[srcpad].type;
138     av_assert0(AV_PIX_FMT_NONE == -1 && AV_SAMPLE_FMT_NONE == -1);
139     link->format  = -1;
140
141     return 0;
142 }
143
144 void avfilter_link_free(AVFilterLink **link)
145 {
146     if (!*link)
147         return;
148
149     av_frame_free(&(*link)->partial_buf);
150
151     av_freep(link);
152 }
153
154 int avfilter_link_get_channels(AVFilterLink *link)
155 {
156     return link->channels;
157 }
158
159 void avfilter_link_set_closed(AVFilterLink *link, int closed)
160 {
161     link->closed = closed;
162 }
163
164 int avfilter_insert_filter(AVFilterLink *link, AVFilterContext *filt,
165                            unsigned filt_srcpad_idx, unsigned filt_dstpad_idx)
166 {
167     int ret;
168     unsigned dstpad_idx = link->dstpad - link->dst->input_pads;
169
170     av_log(link->dst, AV_LOG_VERBOSE, "auto-inserting filter '%s' "
171            "between the filter '%s' and the filter '%s'\n",
172            filt->name, link->src->name, link->dst->name);
173
174     link->dst->inputs[dstpad_idx] = NULL;
175     if ((ret = avfilter_link(filt, filt_dstpad_idx, link->dst, dstpad_idx)) < 0) {
176         /* failed to link output filter to new filter */
177         link->dst->inputs[dstpad_idx] = link;
178         return ret;
179     }
180
181     /* re-hookup the link to the new destination filter we inserted */
182     link->dst = filt;
183     link->dstpad = &filt->input_pads[filt_srcpad_idx];
184     filt->inputs[filt_srcpad_idx] = link;
185
186     /* if any information on supported media formats already exists on the
187      * link, we need to preserve that */
188     if (link->out_formats)
189         ff_formats_changeref(&link->out_formats,
190                                    &filt->outputs[filt_dstpad_idx]->out_formats);
191
192     if (link->out_samplerates)
193         ff_formats_changeref(&link->out_samplerates,
194                                    &filt->outputs[filt_dstpad_idx]->out_samplerates);
195     if (link->out_channel_layouts)
196         ff_channel_layouts_changeref(&link->out_channel_layouts,
197                                      &filt->outputs[filt_dstpad_idx]->out_channel_layouts);
198
199     return 0;
200 }
201
202 int avfilter_config_links(AVFilterContext *filter)
203 {
204     int (*config_link)(AVFilterLink *);
205     unsigned i;
206     int ret;
207
208     for (i = 0; i < filter->nb_inputs; i ++) {
209         AVFilterLink *link = filter->inputs[i];
210         AVFilterLink *inlink;
211
212         if (!link) continue;
213
214         inlink = link->src->nb_inputs ? link->src->inputs[0] : NULL;
215         link->current_pts = AV_NOPTS_VALUE;
216
217         switch (link->init_state) {
218         case AVLINK_INIT:
219             continue;
220         case AVLINK_STARTINIT:
221             av_log(filter, AV_LOG_INFO, "circular filter chain detected\n");
222             return 0;
223         case AVLINK_UNINIT:
224             link->init_state = AVLINK_STARTINIT;
225
226             if ((ret = avfilter_config_links(link->src)) < 0)
227                 return ret;
228
229             if (!(config_link = link->srcpad->config_props)) {
230                 if (link->src->nb_inputs != 1) {
231                     av_log(link->src, AV_LOG_ERROR, "Source filters and filters "
232                                                     "with more than one input "
233                                                     "must set config_props() "
234                                                     "callbacks on all outputs\n");
235                     return AVERROR(EINVAL);
236                 }
237             } else if ((ret = config_link(link)) < 0) {
238                 av_log(link->src, AV_LOG_ERROR,
239                        "Failed to configure output pad on %s\n",
240                        link->src->name);
241                 return ret;
242             }
243
244             switch (link->type) {
245             case AVMEDIA_TYPE_VIDEO:
246                 if (!link->time_base.num && !link->time_base.den)
247                     link->time_base = inlink ? inlink->time_base : AV_TIME_BASE_Q;
248
249                 if (!link->sample_aspect_ratio.num && !link->sample_aspect_ratio.den)
250                     link->sample_aspect_ratio = inlink ?
251                         inlink->sample_aspect_ratio : (AVRational){1,1};
252
253                 if (inlink && !link->frame_rate.num && !link->frame_rate.den)
254                     link->frame_rate = inlink->frame_rate;
255
256                 if (inlink) {
257                     if (!link->w)
258                         link->w = inlink->w;
259                     if (!link->h)
260                         link->h = inlink->h;
261                 } else if (!link->w || !link->h) {
262                     av_log(link->src, AV_LOG_ERROR,
263                            "Video source filters must set their output link's "
264                            "width and height\n");
265                     return AVERROR(EINVAL);
266                 }
267                 break;
268
269             case AVMEDIA_TYPE_AUDIO:
270                 if (inlink) {
271                     if (!link->time_base.num && !link->time_base.den)
272                         link->time_base = inlink->time_base;
273                 }
274
275                 if (!link->time_base.num && !link->time_base.den)
276                     link->time_base = (AVRational) {1, link->sample_rate};
277             }
278
279             if ((config_link = link->dstpad->config_props))
280                 if ((ret = config_link(link)) < 0) {
281                     av_log(link->src, AV_LOG_ERROR,
282                            "Failed to configure input pad on %s\n",
283                            link->dst->name);
284                     return ret;
285                 }
286
287             link->init_state = AVLINK_INIT;
288         }
289     }
290
291     return 0;
292 }
293
294 void ff_tlog_link(void *ctx, AVFilterLink *link, int end)
295 {
296     if (link->type == AVMEDIA_TYPE_VIDEO) {
297         ff_tlog(ctx,
298                 "link[%p s:%dx%d fmt:%s %s->%s]%s",
299                 link, link->w, link->h,
300                 av_get_pix_fmt_name(link->format),
301                 link->src ? link->src->filter->name : "",
302                 link->dst ? link->dst->filter->name : "",
303                 end ? "\n" : "");
304     } else {
305         char buf[128];
306         av_get_channel_layout_string(buf, sizeof(buf), -1, link->channel_layout);
307
308         ff_tlog(ctx,
309                 "link[%p r:%d cl:%s fmt:%s %s->%s]%s",
310                 link, (int)link->sample_rate, buf,
311                 av_get_sample_fmt_name(link->format),
312                 link->src ? link->src->filter->name : "",
313                 link->dst ? link->dst->filter->name : "",
314                 end ? "\n" : "");
315     }
316 }
317
318 int ff_request_frame(AVFilterLink *link)
319 {
320     int ret = -1;
321     FF_TPRINTF_START(NULL, request_frame); ff_tlog_link(NULL, link, 1);
322
323     if (link->closed)
324         return AVERROR_EOF;
325     if (link->srcpad->request_frame)
326         ret = link->srcpad->request_frame(link);
327     else if (link->src->inputs[0])
328         ret = ff_request_frame(link->src->inputs[0]);
329     if (ret == AVERROR_EOF && link->partial_buf) {
330         AVFrame *pbuf = link->partial_buf;
331         link->partial_buf = NULL;
332         ret = ff_filter_frame_framed(link, pbuf);
333     }
334     if (ret == AVERROR_EOF)
335         link->closed = 1;
336     return ret;
337 }
338
339 int ff_poll_frame(AVFilterLink *link)
340 {
341     int i, min = INT_MAX;
342
343     if (link->srcpad->poll_frame)
344         return link->srcpad->poll_frame(link);
345
346     for (i = 0; i < link->src->nb_inputs; i++) {
347         int val;
348         if (!link->src->inputs[i])
349             return -1;
350         val = ff_poll_frame(link->src->inputs[i]);
351         min = FFMIN(min, val);
352     }
353
354     return min;
355 }
356
357 void ff_update_link_current_pts(AVFilterLink *link, int64_t pts)
358 {
359     if (pts == AV_NOPTS_VALUE)
360         return;
361     link->current_pts = av_rescale_q(pts, link->time_base, AV_TIME_BASE_Q);
362     /* TODO use duration */
363     if (link->graph && link->age_index >= 0)
364         ff_avfilter_graph_update_heap(link->graph, link);
365 }
366
367 int avfilter_process_command(AVFilterContext *filter, const char *cmd, const char *arg, char *res, int res_len, int flags)
368 {
369     if(!strcmp(cmd, "ping")){
370         av_strlcatf(res, res_len, "pong from:%s %s\n", filter->filter->name, filter->name);
371         return 0;
372     }else if(filter->filter->process_command) {
373         return filter->filter->process_command(filter, cmd, arg, res, res_len, flags);
374     }
375     return AVERROR(ENOSYS);
376 }
377
378 #define MAX_REGISTERED_AVFILTERS_NB 256
379
380 static AVFilter *registered_avfilters[MAX_REGISTERED_AVFILTERS_NB + 1];
381
382 static int next_registered_avfilter_idx = 0;
383
384 AVFilter *avfilter_get_by_name(const char *name)
385 {
386     int i;
387
388     for (i = 0; registered_avfilters[i]; i++)
389         if (!strcmp(registered_avfilters[i]->name, name))
390             return registered_avfilters[i];
391
392     return NULL;
393 }
394
395 int avfilter_register(AVFilter *filter)
396 {
397     int i;
398
399     if (next_registered_avfilter_idx == MAX_REGISTERED_AVFILTERS_NB) {
400         av_log(NULL, AV_LOG_ERROR,
401                "Maximum number of registered filters %d reached, "
402                "impossible to register filter with name '%s'\n",
403                MAX_REGISTERED_AVFILTERS_NB, filter->name);
404         return AVERROR(ENOMEM);
405     }
406
407     for(i=0; filter->inputs && filter->inputs[i].name; i++) {
408         const AVFilterPad *input = &filter->inputs[i];
409         av_assert0(     !input->filter_frame
410                     || (!input->start_frame && !input->end_frame));
411     }
412
413     registered_avfilters[next_registered_avfilter_idx++] = filter;
414     return 0;
415 }
416
417 AVFilter **av_filter_next(AVFilter **filter)
418 {
419     return filter ? ++filter : &registered_avfilters[0];
420 }
421
422 void avfilter_uninit(void)
423 {
424     memset(registered_avfilters, 0, sizeof(registered_avfilters));
425     next_registered_avfilter_idx = 0;
426 }
427
428 static int pad_count(const AVFilterPad *pads)
429 {
430     int count;
431
432     if (!pads)
433         return 0;
434
435     for(count = 0; pads->name; count ++) pads ++;
436     return count;
437 }
438
439 static const char *default_filter_name(void *filter_ctx)
440 {
441     AVFilterContext *ctx = filter_ctx;
442     return ctx->name ? ctx->name : ctx->filter->name;
443 }
444
445 static void *filter_child_next(void *obj, void *prev)
446 {
447     AVFilterContext *ctx = obj;
448     if (!prev && ctx->filter && ctx->filter->priv_class)
449         return ctx->priv;
450     return NULL;
451 }
452
453 static const AVClass *filter_child_class_next(const AVClass *prev)
454 {
455     AVFilter **filter_ptr = NULL;
456
457     /* find the filter that corresponds to prev */
458     while (prev && *(filter_ptr = av_filter_next(filter_ptr)))
459         if ((*filter_ptr)->priv_class == prev)
460             break;
461
462     /* could not find filter corresponding to prev */
463     if (prev && !(*filter_ptr))
464         return NULL;
465
466     /* find next filter with specific options */
467     while (*(filter_ptr = av_filter_next(filter_ptr)))
468         if ((*filter_ptr)->priv_class)
469             return (*filter_ptr)->priv_class;
470     return NULL;
471 }
472
473 static const AVClass avfilter_class = {
474     .class_name = "AVFilter",
475     .item_name  = default_filter_name,
476     .version    = LIBAVUTIL_VERSION_INT,
477     .category   = AV_CLASS_CATEGORY_FILTER,
478     .child_next = filter_child_next,
479     .child_class_next = filter_child_class_next,
480 };
481
482 const AVClass *avfilter_get_class(void)
483 {
484     return &avfilter_class;
485 }
486
487 int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *inst_name)
488 {
489     AVFilterContext *ret;
490     *filter_ctx = NULL;
491
492     if (!filter)
493         return AVERROR(EINVAL);
494
495     ret = av_mallocz(sizeof(AVFilterContext));
496     if (!ret)
497         return AVERROR(ENOMEM);
498
499     ret->av_class = &avfilter_class;
500     ret->filter   = filter;
501     ret->name     = inst_name ? av_strdup(inst_name) : NULL;
502     if (filter->priv_size) {
503         ret->priv     = av_mallocz(filter->priv_size);
504         if (!ret->priv)
505             goto err;
506     }
507
508     ret->nb_inputs = pad_count(filter->inputs);
509     if (ret->nb_inputs ) {
510         ret->input_pads   = av_malloc(sizeof(AVFilterPad) * ret->nb_inputs);
511         if (!ret->input_pads)
512             goto err;
513         memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad) * ret->nb_inputs);
514         ret->inputs       = av_mallocz(sizeof(AVFilterLink*) * ret->nb_inputs);
515         if (!ret->inputs)
516             goto err;
517     }
518
519     ret->nb_outputs = pad_count(filter->outputs);
520     if (ret->nb_outputs) {
521         ret->output_pads  = av_malloc(sizeof(AVFilterPad) * ret->nb_outputs);
522         if (!ret->output_pads)
523             goto err;
524         memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad) * ret->nb_outputs);
525         ret->outputs      = av_mallocz(sizeof(AVFilterLink*) * ret->nb_outputs);
526         if (!ret->outputs)
527             goto err;
528     }
529 #if FF_API_FOO_COUNT
530     ret->output_count = ret->nb_outputs;
531     ret->input_count  = ret->nb_inputs;
532 #endif
533
534     *filter_ctx = ret;
535     return 0;
536
537 err:
538     av_freep(&ret->inputs);
539     av_freep(&ret->input_pads);
540     ret->nb_inputs = 0;
541     av_freep(&ret->outputs);
542     av_freep(&ret->output_pads);
543     ret->nb_outputs = 0;
544     av_freep(&ret->priv);
545     av_free(ret);
546     return AVERROR(ENOMEM);
547 }
548
549 void avfilter_free(AVFilterContext *filter)
550 {
551     int i;
552     AVFilterLink *link;
553
554     if (!filter)
555         return;
556
557     if (filter->filter->uninit)
558         filter->filter->uninit(filter);
559
560     for (i = 0; i < filter->nb_inputs; i++) {
561         if ((link = filter->inputs[i])) {
562             if (link->src)
563                 link->src->outputs[link->srcpad - link->src->output_pads] = NULL;
564             ff_formats_unref(&link->in_formats);
565             ff_formats_unref(&link->out_formats);
566             ff_formats_unref(&link->in_samplerates);
567             ff_formats_unref(&link->out_samplerates);
568             ff_channel_layouts_unref(&link->in_channel_layouts);
569             ff_channel_layouts_unref(&link->out_channel_layouts);
570         }
571         avfilter_link_free(&link);
572     }
573     for (i = 0; i < filter->nb_outputs; i++) {
574         if ((link = filter->outputs[i])) {
575             if (link->dst)
576                 link->dst->inputs[link->dstpad - link->dst->input_pads] = NULL;
577             ff_formats_unref(&link->in_formats);
578             ff_formats_unref(&link->out_formats);
579             ff_formats_unref(&link->in_samplerates);
580             ff_formats_unref(&link->out_samplerates);
581             ff_channel_layouts_unref(&link->in_channel_layouts);
582             ff_channel_layouts_unref(&link->out_channel_layouts);
583         }
584         avfilter_link_free(&link);
585     }
586
587     av_freep(&filter->name);
588     av_freep(&filter->input_pads);
589     av_freep(&filter->output_pads);
590     av_freep(&filter->inputs);
591     av_freep(&filter->outputs);
592     av_freep(&filter->priv);
593     while(filter->command_queue){
594         ff_command_queue_pop(filter);
595     }
596     av_free(filter);
597 }
598
599 int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
600 {
601     int ret=0;
602
603     if (filter->filter->init_opaque)
604         ret = filter->filter->init_opaque(filter, args, opaque);
605     else if (filter->filter->init)
606         ret = filter->filter->init(filter, args);
607     return ret;
608 }
609
610 const char *avfilter_pad_get_name(AVFilterPad *pads, int pad_idx)
611 {
612     return pads[pad_idx].name;
613 }
614
615 enum AVMediaType avfilter_pad_get_type(AVFilterPad *pads, int pad_idx)
616 {
617     return pads[pad_idx].type;
618 }
619
620 static int default_filter_frame(AVFilterLink *link, AVFrame *frame)
621 {
622     return ff_filter_frame(link->dst->outputs[0], frame);
623 }
624
625 static int ff_filter_frame_framed(AVFilterLink *link, AVFrame *frame)
626 {
627     int (*filter_frame)(AVFilterLink *, AVFrame *);
628     AVFilterPad *dst = link->dstpad;
629     AVFrame *out;
630     int ret;
631     AVFilterCommand *cmd= link->dst->command_queue;
632     int64_t pts;
633
634     if (link->closed) {
635         av_frame_free(&frame);
636         return AVERROR_EOF;
637     }
638
639     if (!(filter_frame = dst->filter_frame))
640         filter_frame = default_filter_frame;
641
642     /* copy the frame if needed */
643     if (dst->needs_writable && !av_frame_is_writable(frame)) {
644         av_log(link->dst, AV_LOG_DEBUG, "Copying data in avfilter.\n");
645
646         /* Maybe use ff_copy_buffer_ref instead? */
647         switch (link->type) {
648         case AVMEDIA_TYPE_VIDEO:
649             out = ff_get_video_buffer(link, link->w, link->h);
650             break;
651         case AVMEDIA_TYPE_AUDIO:
652             out = ff_get_audio_buffer(link, frame->nb_samples);
653             break;
654         default: return AVERROR(EINVAL);
655         }
656         if (!out) {
657             av_frame_free(&frame);
658             return AVERROR(ENOMEM);
659         }
660         av_frame_copy_props(out, frame);
661
662         switch (link->type) {
663         case AVMEDIA_TYPE_VIDEO:
664             av_image_copy(out->data, out->linesize, (const uint8_t **)frame->data, frame->linesize,
665                           frame->format, frame->width, frame->height);
666             break;
667         case AVMEDIA_TYPE_AUDIO:
668             av_samples_copy(out->extended_data, frame->extended_data,
669                             0, 0, frame->nb_samples,
670                             av_get_channel_layout_nb_channels(frame->channel_layout),
671                             frame->format);
672             break;
673         default: return AVERROR(EINVAL);
674         }
675
676         av_frame_free(&frame);
677     } else
678         out = frame;
679
680     while(cmd && cmd->time <= out->pts * av_q2d(link->time_base)){
681         av_log(link->dst, AV_LOG_DEBUG,
682                "Processing command time:%f command:%s arg:%s\n",
683                cmd->time, cmd->command, cmd->arg);
684         avfilter_process_command(link->dst, cmd->command, cmd->arg, 0, 0, cmd->flags);
685         ff_command_queue_pop(link->dst);
686         cmd= link->dst->command_queue;
687     }
688
689     pts = out->pts;
690     ret = filter_frame(link, out);
691     ff_update_link_current_pts(link, pts);
692     return ret;
693 }
694
695 static int ff_filter_frame_needs_framing(AVFilterLink *link, AVFrame *frame)
696 {
697     int insamples = frame->nb_samples, inpos = 0, nb_samples;
698     AVFrame *pbuf = link->partial_buf;
699     int nb_channels = av_frame_get_channels(frame);
700     int ret = 0;
701
702     /* Handle framing (min_samples, max_samples) */
703     while (insamples) {
704         if (!pbuf) {
705             AVRational samples_tb = { 1, link->sample_rate };
706             pbuf = ff_get_audio_buffer(link, link->partial_buf_size);
707             if (!pbuf) {
708                 av_log(link->dst, AV_LOG_WARNING,
709                        "Samples dropped due to memory allocation failure.\n");
710                 return 0;
711             }
712             av_frame_copy_props(pbuf, frame);
713             pbuf->pts = frame->pts +
714                         av_rescale_q(inpos, samples_tb, link->time_base);
715             pbuf->nb_samples = 0;
716         }
717         nb_samples = FFMIN(insamples,
718                            link->partial_buf_size - pbuf->nb_samples);
719         av_samples_copy(pbuf->extended_data, frame->extended_data,
720                         pbuf->nb_samples, inpos,
721                         nb_samples, nb_channels, link->format);
722         inpos                   += nb_samples;
723         insamples               -= nb_samples;
724         pbuf->nb_samples += nb_samples;
725         if (pbuf->nb_samples >= link->min_samples) {
726             ret = ff_filter_frame_framed(link, pbuf);
727             pbuf = NULL;
728         }
729     }
730     av_frame_free(&frame);
731     link->partial_buf = pbuf;
732     return ret;
733 }
734
735 int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
736 {
737     FF_TPRINTF_START(NULL, filter_frame); ff_tlog_link(NULL, link, 1); ff_tlog(NULL, " "); ff_tlog_ref(NULL, frame, 1);
738
739     /* Consistency checks */
740     if (link->type == AVMEDIA_TYPE_VIDEO) {
741         if (strcmp(link->dst->filter->name, "scale")) {
742             av_assert1(frame->format                 == link->format);
743             av_assert1(frame->width               == link->w);
744             av_assert1(frame->height               == link->h);
745         }
746     } else {
747         av_assert1(frame->format                == link->format);
748         av_assert1(av_frame_get_channels(frame) == link->channels);
749         av_assert1(frame->channel_layout        == link->channel_layout);
750         av_assert1(frame->sample_rate           == link->sample_rate);
751     }
752
753     /* Go directly to actual filtering if possible */
754     if (link->type == AVMEDIA_TYPE_AUDIO &&
755         link->min_samples &&
756         (link->partial_buf ||
757          frame->nb_samples < link->min_samples ||
758          frame->nb_samples > link->max_samples)) {
759         return ff_filter_frame_needs_framing(link, frame);
760     } else {
761         return ff_filter_frame_framed(link, frame);
762     }
763 }