]> git.sesse.net Git - ffmpeg/blob - libavfilter/avfilter.c
lavfi: simplify signature for avfilter_get_audio_buffer() and friends
[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 /* #define DEBUG */
23
24 #include "libavutil/pixdesc.h"
25 #include "libavutil/rational.h"
26 #include "libavutil/audioconvert.h"
27 #include "libavutil/imgutils.h"
28 #include "libavutil/avassert.h"
29 #include "libavutil/avstring.h"
30 #include "avfilter.h"
31 #include "internal.h"
32
33 unsigned avfilter_version(void) {
34     return LIBAVFILTER_VERSION_INT;
35 }
36
37 const char *avfilter_configuration(void)
38 {
39     return FFMPEG_CONFIGURATION;
40 }
41
42 const char *avfilter_license(void)
43 {
44 #define LICENSE_PREFIX "libavfilter license: "
45     return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
46 }
47
48 static void command_queue_pop(AVFilterContext *filter)
49 {
50     AVFilterCommand *c= filter->command_queue;
51     av_freep(&c->arg);
52     av_freep(&c->command);
53     filter->command_queue= c->next;
54     av_free(c);
55 }
56
57 AVFilterBufferRef *avfilter_ref_buffer(AVFilterBufferRef *ref, int pmask)
58 {
59     AVFilterBufferRef *ret = av_malloc(sizeof(AVFilterBufferRef));
60     if (!ret)
61         return NULL;
62     *ret = *ref;
63     if (ref->type == AVMEDIA_TYPE_VIDEO) {
64         ret->video = av_malloc(sizeof(AVFilterBufferRefVideoProps));
65         if (!ret->video) {
66             av_free(ret);
67             return NULL;
68         }
69         *ret->video = *ref->video;
70     } else if (ref->type == AVMEDIA_TYPE_AUDIO) {
71         ret->audio = av_malloc(sizeof(AVFilterBufferRefAudioProps));
72         if (!ret->audio) {
73             av_free(ret);
74             return NULL;
75         }
76         *ret->audio = *ref->audio;
77     }
78     ret->perms &= pmask;
79     ret->buf->refcount ++;
80     return ret;
81 }
82
83 static void store_in_pool(AVFilterBufferRef *ref)
84 {
85     int i;
86     AVFilterPool *pool= ref->buf->priv;
87
88     av_assert0(ref->buf->data[0]);
89
90     if (pool->count == POOL_SIZE) {
91         AVFilterBufferRef *ref1 = pool->pic[0];
92         av_freep(&ref1->video);
93         av_freep(&ref1->audio);
94         av_freep(&ref1->buf->data[0]);
95         av_freep(&ref1->buf);
96         av_free(ref1);
97         memmove(&pool->pic[0], &pool->pic[1], sizeof(void*)*(POOL_SIZE-1));
98         pool->count--;
99         pool->pic[POOL_SIZE-1] = NULL;
100     }
101
102     for (i = 0; i < POOL_SIZE; i++) {
103         if (!pool->pic[i]) {
104             pool->pic[i] = ref;
105             pool->count++;
106             break;
107         }
108     }
109 }
110
111 void avfilter_unref_buffer(AVFilterBufferRef *ref)
112 {
113     if (!ref)
114         return;
115     if (!(--ref->buf->refcount)) {
116         if (!ref->buf->free) {
117             store_in_pool(ref);
118             return;
119         }
120         ref->buf->free(ref->buf);
121     }
122     av_freep(&ref->video);
123     av_freep(&ref->audio);
124     av_free(ref);
125 }
126
127 void avfilter_insert_pad(unsigned idx, unsigned *count, size_t padidx_off,
128                          AVFilterPad **pads, AVFilterLink ***links,
129                          AVFilterPad *newpad)
130 {
131     unsigned i;
132
133     idx = FFMIN(idx, *count);
134
135     *pads  = av_realloc(*pads,  sizeof(AVFilterPad)   * (*count + 1));
136     *links = av_realloc(*links, sizeof(AVFilterLink*) * (*count + 1));
137     memmove(*pads +idx+1, *pads +idx, sizeof(AVFilterPad)   * (*count-idx));
138     memmove(*links+idx+1, *links+idx, sizeof(AVFilterLink*) * (*count-idx));
139     memcpy(*pads+idx, newpad, sizeof(AVFilterPad));
140     (*links)[idx] = NULL;
141
142     (*count)++;
143     for (i = idx+1; i < *count; i++)
144         if (*links[i])
145             (*(unsigned *)((uint8_t *) *links[i] + padidx_off))++;
146 }
147
148 int avfilter_link(AVFilterContext *src, unsigned srcpad,
149                   AVFilterContext *dst, unsigned dstpad)
150 {
151     AVFilterLink *link;
152
153     if (src->output_count <= srcpad || dst->input_count <= dstpad ||
154         src->outputs[srcpad]        || dst->inputs[dstpad])
155         return -1;
156
157     if (src->output_pads[srcpad].type != dst->input_pads[dstpad].type) {
158         av_log(src, AV_LOG_ERROR,
159                "Media type mismatch between the '%s' filter output pad %d and the '%s' filter input pad %d\n",
160                src->name, srcpad, dst->name, dstpad);
161         return AVERROR(EINVAL);
162     }
163
164     src->outputs[srcpad] =
165     dst-> inputs[dstpad] = link = av_mallocz(sizeof(AVFilterLink));
166
167     link->src     = src;
168     link->dst     = dst;
169     link->srcpad  = &src->output_pads[srcpad];
170     link->dstpad  = &dst->input_pads[dstpad];
171     link->type    = src->output_pads[srcpad].type;
172     assert(PIX_FMT_NONE == -1 && AV_SAMPLE_FMT_NONE == -1);
173     link->format  = -1;
174
175     return 0;
176 }
177
178 void avfilter_link_free(AVFilterLink **link)
179 {
180     if (!*link)
181         return;
182
183     if ((*link)->pool) {
184         int i;
185         for (i = 0; i < POOL_SIZE; i++) {
186             if ((*link)->pool->pic[i]) {
187                 AVFilterBufferRef *picref = (*link)->pool->pic[i];
188                 /* free buffer: picrefs stored in the pool are not
189                  * supposed to contain a free callback */
190                 av_freep(&picref->buf->data[0]);
191                 av_freep(&picref->buf);
192
193                 av_freep(&picref->audio);
194                 av_freep(&picref->video);
195                 av_freep(&(*link)->pool->pic[i]);
196             }
197         }
198         (*link)->pool->count = 0;
199 //        av_freep(&(*link)->pool);
200     }
201     av_freep(link);
202 }
203
204 int avfilter_insert_filter(AVFilterLink *link, AVFilterContext *filt,
205                            unsigned filt_srcpad_idx, unsigned filt_dstpad_idx)
206 {
207     int ret;
208     unsigned dstpad_idx = link->dstpad - link->dst->input_pads;
209
210     av_log(link->dst, AV_LOG_INFO, "auto-inserting filter '%s' "
211            "between the filter '%s' and the filter '%s'\n",
212            filt->name, link->src->name, link->dst->name);
213
214     link->dst->inputs[dstpad_idx] = NULL;
215     if ((ret = avfilter_link(filt, filt_dstpad_idx, link->dst, dstpad_idx)) < 0) {
216         /* failed to link output filter to new filter */
217         link->dst->inputs[dstpad_idx] = link;
218         return ret;
219     }
220
221     /* re-hookup the link to the new destination filter we inserted */
222     link->dst = filt;
223     link->dstpad = &filt->input_pads[filt_srcpad_idx];
224     filt->inputs[filt_srcpad_idx] = link;
225
226     /* if any information on supported media formats already exists on the
227      * link, we need to preserve that */
228     if (link->out_formats)
229         avfilter_formats_changeref(&link->out_formats,
230                                    &filt->outputs[filt_dstpad_idx]->out_formats);
231     if (link->out_chlayouts)
232         avfilter_formats_changeref(&link->out_chlayouts,
233                                    &filt->outputs[filt_dstpad_idx]->out_chlayouts);
234     if (link->out_packing)
235         avfilter_formats_changeref(&link->out_packing,
236                                    &filt->outputs[filt_dstpad_idx]->out_packing);
237
238     return 0;
239 }
240
241 int avfilter_config_links(AVFilterContext *filter)
242 {
243     int (*config_link)(AVFilterLink *);
244     unsigned i;
245     int ret;
246
247     for (i = 0; i < filter->input_count; i ++) {
248         AVFilterLink *link = filter->inputs[i];
249
250         if (!link) continue;
251
252         switch (link->init_state) {
253         case AVLINK_INIT:
254             continue;
255         case AVLINK_STARTINIT:
256             av_log(filter, AV_LOG_INFO, "circular filter chain detected\n");
257             return 0;
258         case AVLINK_UNINIT:
259             link->init_state = AVLINK_STARTINIT;
260
261             if ((ret = avfilter_config_links(link->src)) < 0)
262                 return ret;
263
264             if (!(config_link = link->srcpad->config_props)) {
265                 if (link->src->input_count != 1) {
266                     av_log(link->src, AV_LOG_ERROR, "Source filters and filters "
267                                                     "with more than one input "
268                                                     "must set config_props() "
269                                                     "callbacks on all outputs\n");
270                     return AVERROR(EINVAL);
271                 }
272             } else if ((ret = config_link(link)) < 0)
273                 return ret;
274
275             switch (link->type) {
276             case AVMEDIA_TYPE_VIDEO:
277                 if (!link->time_base.num && !link->time_base.den)
278                     link->time_base = link->src->input_count ?
279                         link->src->inputs[0]->time_base : AV_TIME_BASE_Q;
280
281                 if (!link->sample_aspect_ratio.num && !link->sample_aspect_ratio.den)
282                     link->sample_aspect_ratio = link->src->input_count ?
283                         link->src->inputs[0]->sample_aspect_ratio : (AVRational){1,1};
284
285                 if (link->src->input_count) {
286                     if (!link->w)
287                         link->w = link->src->inputs[0]->w;
288                     if (!link->h)
289                         link->h = link->src->inputs[0]->h;
290                 } else if (!link->w || !link->h) {
291                     av_log(link->src, AV_LOG_ERROR,
292                            "Video source filters must set their output link's "
293                            "width and height\n");
294                     return AVERROR(EINVAL);
295                 }
296                 break;
297
298             case AVMEDIA_TYPE_AUDIO:
299                 if (link->src->input_count) {
300                     if (!link->sample_rate)
301                         link->sample_rate = link->src->inputs[0]->sample_rate;
302                 } else if (!link->sample_rate) {
303                     av_log(link->src, AV_LOG_ERROR,
304                            "Audio source filters must set their output link's "
305                            "sample_rate\n");
306                     return AVERROR(EINVAL);
307                 }
308             }
309
310             if ((config_link = link->dstpad->config_props))
311                 if ((ret = config_link(link)) < 0)
312                     return ret;
313
314             link->init_state = AVLINK_INIT;
315         }
316     }
317
318     return 0;
319 }
320
321 static char *ff_get_ref_perms_string(char *buf, size_t buf_size, int perms)
322 {
323     snprintf(buf, buf_size, "%s%s%s%s%s%s",
324              perms & AV_PERM_READ      ? "r" : "",
325              perms & AV_PERM_WRITE     ? "w" : "",
326              perms & AV_PERM_PRESERVE  ? "p" : "",
327              perms & AV_PERM_REUSE     ? "u" : "",
328              perms & AV_PERM_REUSE2    ? "U" : "",
329              perms & AV_PERM_NEG_LINESIZES ? "n" : "");
330     return buf;
331 }
332
333 static void ff_dlog_ref(void *ctx, AVFilterBufferRef *ref, int end)
334 {
335     av_unused char buf[16];
336     av_dlog(ctx,
337             "ref[%p buf:%p refcount:%d perms:%s data:%p linesize[%d, %d, %d, %d] pts:%"PRId64" pos:%"PRId64,
338             ref, ref->buf, ref->buf->refcount, ff_get_ref_perms_string(buf, sizeof(buf), ref->perms), ref->data[0],
339             ref->linesize[0], ref->linesize[1], ref->linesize[2], ref->linesize[3],
340             ref->pts, ref->pos);
341
342     if (ref->video) {
343         av_dlog(ctx, " a:%d/%d s:%dx%d i:%c iskey:%d type:%c",
344                 ref->video->sample_aspect_ratio.num, ref->video->sample_aspect_ratio.den,
345                 ref->video->w, ref->video->h,
346                 !ref->video->interlaced     ? 'P' :         /* Progressive  */
347                 ref->video->top_field_first ? 'T' : 'B',    /* Top / Bottom */
348                 ref->video->key_frame,
349                 av_get_picture_type_char(ref->video->pict_type));
350     }
351     if (ref->audio) {
352         av_dlog(ctx, " cl:%"PRId64"d n:%d r:%d p:%d",
353                 ref->audio->channel_layout,
354                 ref->audio->nb_samples,
355                 ref->audio->sample_rate,
356                 ref->audio->planar);
357     }
358
359     av_dlog(ctx, "]%s", end ? "\n" : "");
360 }
361
362 static void ff_dlog_link(void *ctx, AVFilterLink *link, int end)
363 {
364     if (link->type == AVMEDIA_TYPE_VIDEO) {
365         av_dlog(ctx,
366                 "link[%p s:%dx%d fmt:%-16s %-16s->%-16s]%s",
367                 link, link->w, link->h,
368                 av_pix_fmt_descriptors[link->format].name,
369                 link->src ? link->src->filter->name : "",
370                 link->dst ? link->dst->filter->name : "",
371                 end ? "\n" : "");
372     } else {
373         char buf[128];
374         av_get_channel_layout_string(buf, sizeof(buf), -1, link->channel_layout);
375
376         av_dlog(ctx,
377                 "link[%p r:%"PRId64" cl:%s fmt:%-16s %-16s->%-16s]%s",
378                 link, link->sample_rate, buf,
379                 av_get_sample_fmt_name(link->format),
380                 link->src ? link->src->filter->name : "",
381                 link->dst ? link->dst->filter->name : "",
382                 end ? "\n" : "");
383     }
384 }
385
386 #define FF_DPRINTF_START(ctx, func) av_dlog(NULL, "%-16s: ", #func)
387
388 AVFilterBufferRef *avfilter_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
389 {
390     AVFilterBufferRef *ret = NULL;
391
392     av_unused char buf[16];
393     FF_DPRINTF_START(NULL, get_video_buffer); ff_dlog_link(NULL, link, 0);
394     av_dlog(NULL, " perms:%s w:%d h:%d\n", ff_get_ref_perms_string(buf, sizeof(buf), perms), w, h);
395
396     if (link->dstpad->get_video_buffer)
397         ret = link->dstpad->get_video_buffer(link, perms, w, h);
398
399     if (!ret)
400         ret = avfilter_default_get_video_buffer(link, perms, w, h);
401
402     if (ret)
403         ret->type = AVMEDIA_TYPE_VIDEO;
404
405     FF_DPRINTF_START(NULL, get_video_buffer); ff_dlog_link(NULL, link, 0); av_dlog(NULL, " returning "); ff_dlog_ref(NULL, ret, 1);
406
407     return ret;
408 }
409
410 AVFilterBufferRef *
411 avfilter_get_video_buffer_ref_from_arrays(uint8_t * const data[4], const int linesize[4], int perms,
412                                           int w, int h, enum PixelFormat format)
413 {
414     AVFilterBuffer *pic = av_mallocz(sizeof(AVFilterBuffer));
415     AVFilterBufferRef *picref = av_mallocz(sizeof(AVFilterBufferRef));
416
417     if (!pic || !picref)
418         goto fail;
419
420     picref->buf = pic;
421     picref->buf->free = ff_avfilter_default_free_buffer;
422     if (!(picref->video = av_mallocz(sizeof(AVFilterBufferRefVideoProps))))
423         goto fail;
424
425     pic->w = picref->video->w = w;
426     pic->h = picref->video->h = h;
427
428     /* make sure the buffer gets read permission or it's useless for output */
429     picref->perms = perms | AV_PERM_READ;
430
431     pic->refcount = 1;
432     picref->type = AVMEDIA_TYPE_VIDEO;
433     pic->format = picref->format = format;
434
435     memcpy(pic->data,        data,          sizeof(pic->data));
436     memcpy(pic->linesize,    linesize,      sizeof(pic->linesize));
437     memcpy(picref->data,     pic->data,     sizeof(picref->data));
438     memcpy(picref->linesize, pic->linesize, sizeof(picref->linesize));
439
440     return picref;
441
442 fail:
443     if (picref && picref->video)
444         av_free(picref->video);
445     av_free(picref);
446     av_free(pic);
447     return NULL;
448 }
449
450 AVFilterBufferRef *avfilter_get_audio_buffer(AVFilterLink *link,
451                                              int perms, int nb_samples)
452 {
453     AVFilterBufferRef *ret = NULL;
454
455     if (link->dstpad->get_audio_buffer)
456         ret = link->dstpad->get_audio_buffer(link, perms, nb_samples);
457
458     if (!ret)
459         ret = avfilter_default_get_audio_buffer(link, perms, nb_samples);
460
461     if (ret)
462         ret->type = AVMEDIA_TYPE_AUDIO;
463
464     return ret;
465 }
466
467 AVFilterBufferRef *
468 avfilter_get_audio_buffer_ref_from_arrays(uint8_t *data[8], int linesize[8], int perms,
469                                           int nb_samples, enum AVSampleFormat sample_fmt,
470                                           int64_t channel_layout, int planar)
471 {
472     AVFilterBuffer *samples = av_mallocz(sizeof(AVFilterBuffer));
473     AVFilterBufferRef *samplesref = av_mallocz(sizeof(AVFilterBufferRef));
474
475     if (!samples || !samplesref)
476         goto fail;
477
478     samplesref->buf = samples;
479     samplesref->buf->free = ff_avfilter_default_free_buffer;
480     if (!(samplesref->audio = av_mallocz(sizeof(AVFilterBufferRefAudioProps))))
481         goto fail;
482
483     samplesref->audio->nb_samples     = nb_samples;
484     samplesref->audio->channel_layout = channel_layout;
485     samplesref->audio->planar         = planar;
486
487     /* make sure the buffer gets read permission or it's useless for output */
488     samplesref->perms = perms | AV_PERM_READ;
489
490     samples->refcount = 1;
491     samplesref->type = AVMEDIA_TYPE_AUDIO;
492     samplesref->format = sample_fmt;
493
494     memcpy(samples->data,        data,     sizeof(samples->data));
495     memcpy(samples->linesize,    linesize, sizeof(samples->linesize));
496     memcpy(samplesref->data,     data,     sizeof(samplesref->data));
497     memcpy(samplesref->linesize, linesize, sizeof(samplesref->linesize));
498
499     return samplesref;
500
501 fail:
502     if (samplesref && samplesref->audio)
503         av_freep(&samplesref->audio);
504     av_freep(&samplesref);
505     av_freep(&samples);
506     return NULL;
507 }
508
509 int avfilter_request_frame(AVFilterLink *link)
510 {
511     FF_DPRINTF_START(NULL, request_frame); ff_dlog_link(NULL, link, 1);
512
513     if (link->srcpad->request_frame)
514         return link->srcpad->request_frame(link);
515     else if (link->src->inputs[0])
516         return avfilter_request_frame(link->src->inputs[0]);
517     else return -1;
518 }
519
520 int avfilter_poll_frame(AVFilterLink *link)
521 {
522     int i, min = INT_MAX;
523
524     if (link->srcpad->poll_frame)
525         return link->srcpad->poll_frame(link);
526
527     for (i = 0; i < link->src->input_count; i++) {
528         int val;
529         if (!link->src->inputs[i])
530             return -1;
531         val = avfilter_poll_frame(link->src->inputs[i]);
532         min = FFMIN(min, val);
533     }
534
535     return min;
536 }
537
538 /* XXX: should we do the duplicating of the picture ref here, instead of
539  * forcing the source filter to do it? */
540 void avfilter_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
541 {
542     void (*start_frame)(AVFilterLink *, AVFilterBufferRef *);
543     AVFilterPad *dst = link->dstpad;
544     int perms = picref->perms;
545     AVFilterCommand *cmd= link->dst->command_queue;
546
547     FF_DPRINTF_START(NULL, start_frame); ff_dlog_link(NULL, link, 0); av_dlog(NULL, " "); ff_dlog_ref(NULL, picref, 1);
548
549     if (!(start_frame = dst->start_frame))
550         start_frame = avfilter_default_start_frame;
551
552     if (picref->linesize[0] < 0)
553         perms |= AV_PERM_NEG_LINESIZES;
554     /* prepare to copy the picture if it has insufficient permissions */
555     if ((dst->min_perms & perms) != dst->min_perms || dst->rej_perms & perms) {
556         av_log(link->dst, AV_LOG_DEBUG,
557                 "frame copy needed (have perms %x, need %x, reject %x)\n",
558                 picref->perms,
559                 link->dstpad->min_perms, link->dstpad->rej_perms);
560
561         link->cur_buf = avfilter_get_video_buffer(link, dst->min_perms, link->w, link->h);
562         link->src_buf = picref;
563         avfilter_copy_buffer_ref_props(link->cur_buf, link->src_buf);
564     }
565     else
566         link->cur_buf = picref;
567
568     while(cmd && cmd->time <= picref->pts * av_q2d(link->time_base)){
569         avfilter_process_command(link->dst, cmd->command, cmd->arg, 0, 0, cmd->flags);
570         command_queue_pop(link->dst);
571         cmd= link->dst->command_queue;
572     }
573
574     start_frame(link, link->cur_buf);
575 }
576
577 void avfilter_end_frame(AVFilterLink *link)
578 {
579     void (*end_frame)(AVFilterLink *);
580
581     if (!(end_frame = link->dstpad->end_frame))
582         end_frame = avfilter_default_end_frame;
583
584     end_frame(link);
585
586     /* unreference the source picture if we're feeding the destination filter
587      * a copied version dues to permission issues */
588     if (link->src_buf) {
589         avfilter_unref_buffer(link->src_buf);
590         link->src_buf = NULL;
591     }
592 }
593
594 void avfilter_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
595 {
596     uint8_t *src[4], *dst[4];
597     int i, j, vsub;
598     void (*draw_slice)(AVFilterLink *, int, int, int);
599
600     FF_DPRINTF_START(NULL, draw_slice); ff_dlog_link(NULL, link, 0); av_dlog(NULL, " y:%d h:%d dir:%d\n", y, h, slice_dir);
601
602     /* copy the slice if needed for permission reasons */
603     if (link->src_buf) {
604         vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h;
605
606         for (i = 0; i < 4; i++) {
607             if (link->src_buf->data[i]) {
608                 src[i] = link->src_buf-> data[i] +
609                     (y >> (i==1 || i==2 ? vsub : 0)) * link->src_buf-> linesize[i];
610                 dst[i] = link->cur_buf->data[i] +
611                     (y >> (i==1 || i==2 ? vsub : 0)) * link->cur_buf->linesize[i];
612             } else
613                 src[i] = dst[i] = NULL;
614         }
615
616         for (i = 0; i < 4; i++) {
617             int planew =
618                 av_image_get_linesize(link->format, link->cur_buf->video->w, i);
619
620             if (!src[i]) continue;
621
622             for (j = 0; j < h >> (i==1 || i==2 ? vsub : 0); j++) {
623                 memcpy(dst[i], src[i], planew);
624                 src[i] += link->src_buf->linesize[i];
625                 dst[i] += link->cur_buf->linesize[i];
626             }
627         }
628     }
629
630     if (!(draw_slice = link->dstpad->draw_slice))
631         draw_slice = avfilter_default_draw_slice;
632     draw_slice(link, y, h, slice_dir);
633 }
634
635 int avfilter_process_command(AVFilterContext *filter, const char *cmd, const char *arg, char *res, int res_len, int flags)
636 {
637     if(!strcmp(cmd, "ping")){
638         av_strlcatf(res, res_len, "pong from:%s %s\n", filter->filter->name, filter->name);
639         return 0;
640     }else if(filter->filter->process_command) {
641         return filter->filter->process_command(filter, cmd, arg, res, res_len, flags);
642     }
643     return AVERROR(ENOSYS);
644 }
645
646 void avfilter_filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
647 {
648     void (*filter_samples)(AVFilterLink *, AVFilterBufferRef *);
649     AVFilterPad *dst = link->dstpad;
650     int i;
651
652     FF_DPRINTF_START(NULL, filter_samples); ff_dlog_link(NULL, link, 1);
653
654     if (!(filter_samples = dst->filter_samples))
655         filter_samples = avfilter_default_filter_samples;
656
657     /* prepare to copy the samples if the buffer has insufficient permissions */
658     if ((dst->min_perms & samplesref->perms) != dst->min_perms ||
659         dst->rej_perms & samplesref->perms) {
660
661         av_log(link->dst, AV_LOG_DEBUG,
662                "Copying audio data in avfilter (have perms %x, need %x, reject %x)\n",
663                samplesref->perms, link->dstpad->min_perms, link->dstpad->rej_perms);
664
665         link->cur_buf = avfilter_default_get_audio_buffer(link, dst->min_perms,
666                                                           samplesref->audio->nb_samples);
667         link->cur_buf->pts                = samplesref->pts;
668         link->cur_buf->audio->sample_rate = samplesref->audio->sample_rate;
669
670         /* Copy actual data into new samples buffer */
671         for (i = 0; samplesref->data[i]; i++)
672             memcpy(link->cur_buf->data[i], samplesref->data[i], samplesref->linesize[0]);
673
674         avfilter_unref_buffer(samplesref);
675     } else
676         link->cur_buf = samplesref;
677
678     filter_samples(link, link->cur_buf);
679 }
680
681 #define MAX_REGISTERED_AVFILTERS_NB 64
682
683 static AVFilter *registered_avfilters[MAX_REGISTERED_AVFILTERS_NB + 1];
684
685 static int next_registered_avfilter_idx = 0;
686
687 AVFilter *avfilter_get_by_name(const char *name)
688 {
689     int i;
690
691     for (i = 0; registered_avfilters[i]; i++)
692         if (!strcmp(registered_avfilters[i]->name, name))
693             return registered_avfilters[i];
694
695     return NULL;
696 }
697
698 int avfilter_register(AVFilter *filter)
699 {
700     if (next_registered_avfilter_idx == MAX_REGISTERED_AVFILTERS_NB)
701         return -1;
702
703     registered_avfilters[next_registered_avfilter_idx++] = filter;
704     return 0;
705 }
706
707 AVFilter **av_filter_next(AVFilter **filter)
708 {
709     return filter ? ++filter : &registered_avfilters[0];
710 }
711
712 void avfilter_uninit(void)
713 {
714     memset(registered_avfilters, 0, sizeof(registered_avfilters));
715     next_registered_avfilter_idx = 0;
716 }
717
718 static int pad_count(const AVFilterPad *pads)
719 {
720     int count;
721
722     for(count = 0; pads->name; count ++) pads ++;
723     return count;
724 }
725
726 static const char *filter_name(void *p)
727 {
728     AVFilterContext *filter = p;
729     return filter->filter->name;
730 }
731
732 static const AVClass avfilter_class = {
733     "AVFilter",
734     filter_name,
735     NULL,
736     LIBAVUTIL_VERSION_INT,
737 };
738
739 int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *inst_name)
740 {
741     AVFilterContext *ret;
742     *filter_ctx = NULL;
743
744     if (!filter)
745         return AVERROR(EINVAL);
746
747     ret = av_mallocz(sizeof(AVFilterContext));
748     if (!ret)
749         return AVERROR(ENOMEM);
750
751     ret->av_class = &avfilter_class;
752     ret->filter   = filter;
753     ret->name     = inst_name ? av_strdup(inst_name) : NULL;
754     if (filter->priv_size) {
755         ret->priv     = av_mallocz(filter->priv_size);
756         if (!ret->priv)
757             goto err;
758     }
759
760     ret->input_count  = pad_count(filter->inputs);
761     if (ret->input_count) {
762         ret->input_pads   = av_malloc(sizeof(AVFilterPad) * ret->input_count);
763         if (!ret->input_pads)
764             goto err;
765         memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad) * ret->input_count);
766         ret->inputs       = av_mallocz(sizeof(AVFilterLink*) * ret->input_count);
767         if (!ret->inputs)
768             goto err;
769     }
770
771     ret->output_count = pad_count(filter->outputs);
772     if (ret->output_count) {
773         ret->output_pads  = av_malloc(sizeof(AVFilterPad) * ret->output_count);
774         if (!ret->output_pads)
775             goto err;
776         memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad) * ret->output_count);
777         ret->outputs      = av_mallocz(sizeof(AVFilterLink*) * ret->output_count);
778         if (!ret->outputs)
779             goto err;
780     }
781
782     *filter_ctx = ret;
783     return 0;
784
785 err:
786     av_freep(&ret->inputs);
787     av_freep(&ret->input_pads);
788     ret->input_count = 0;
789     av_freep(&ret->outputs);
790     av_freep(&ret->output_pads);
791     ret->output_count = 0;
792     av_freep(&ret->priv);
793     av_free(ret);
794     return AVERROR(ENOMEM);
795 }
796
797 void avfilter_free(AVFilterContext *filter)
798 {
799     int i;
800     AVFilterLink *link;
801
802     if (filter->filter->uninit)
803         filter->filter->uninit(filter);
804
805     for (i = 0; i < filter->input_count; i++) {
806         if ((link = filter->inputs[i])) {
807             if (link->src)
808                 link->src->outputs[link->srcpad - link->src->output_pads] = NULL;
809             avfilter_formats_unref(&link->in_formats);
810             avfilter_formats_unref(&link->out_formats);
811         }
812         avfilter_link_free(&link);
813     }
814     for (i = 0; i < filter->output_count; i++) {
815         if ((link = filter->outputs[i])) {
816             if (link->dst)
817                 link->dst->inputs[link->dstpad - link->dst->input_pads] = NULL;
818             avfilter_formats_unref(&link->in_formats);
819             avfilter_formats_unref(&link->out_formats);
820         }
821         avfilter_link_free(&link);
822     }
823
824     av_freep(&filter->name);
825     av_freep(&filter->input_pads);
826     av_freep(&filter->output_pads);
827     av_freep(&filter->inputs);
828     av_freep(&filter->outputs);
829     av_freep(&filter->priv);
830     while(filter->command_queue){
831         command_queue_pop(filter);
832     }
833     av_free(filter);
834 }
835
836 int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
837 {
838     int ret=0;
839
840     if (filter->filter->init)
841         ret = filter->filter->init(filter, args, opaque);
842     return ret;
843 }
844