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