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