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