]> 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/avassert.h"
28 #include "libavutil/avstring.h"
29
30 #include "avfilter.h"
31 #include "formats.h"
32 #include "internal.h"
33
34 char *ff_get_ref_perms_string(char *buf, size_t buf_size, int perms)
35 {
36     snprintf(buf, buf_size, "%s%s%s%s%s%s",
37              perms & AV_PERM_READ      ? "r" : "",
38              perms & AV_PERM_WRITE     ? "w" : "",
39              perms & AV_PERM_PRESERVE  ? "p" : "",
40              perms & AV_PERM_REUSE     ? "u" : "",
41              perms & AV_PERM_REUSE2    ? "U" : "",
42              perms & AV_PERM_NEG_LINESIZES ? "n" : "");
43     return buf;
44 }
45
46 void ff_dlog_ref(void *ctx, AVFilterBufferRef *ref, int end)
47 {
48     av_unused char buf[16];
49     av_dlog(ctx,
50             "ref[%p buf:%p refcount:%d perms:%s data:%p linesize[%d, %d, %d, %d] pts:%"PRId64" pos:%"PRId64,
51             ref, ref->buf, ref->buf->refcount, ff_get_ref_perms_string(buf, sizeof(buf), ref->perms), ref->data[0],
52             ref->linesize[0], ref->linesize[1], ref->linesize[2], ref->linesize[3],
53             ref->pts, ref->pos);
54
55     if (ref->video) {
56         av_dlog(ctx, " a:%d/%d s:%dx%d i:%c iskey:%d type:%c",
57                 ref->video->sample_aspect_ratio.num, ref->video->sample_aspect_ratio.den,
58                 ref->video->w, ref->video->h,
59                 !ref->video->interlaced     ? 'P' :         /* Progressive  */
60                 ref->video->top_field_first ? 'T' : 'B',    /* Top / Bottom */
61                 ref->video->key_frame,
62                 av_get_picture_type_char(ref->video->pict_type));
63     }
64     if (ref->audio) {
65         av_dlog(ctx, " cl:%"PRId64"d n:%d r:%d",
66                 ref->audio->channel_layout,
67                 ref->audio->nb_samples,
68                 ref->audio->sample_rate);
69     }
70
71     av_dlog(ctx, "]%s", end ? "\n" : "");
72 }
73
74 unsigned avfilter_version(void) {
75     av_assert0(LIBAVFILTER_VERSION_MICRO >= 100);
76     return LIBAVFILTER_VERSION_INT;
77 }
78
79 const char *avfilter_configuration(void)
80 {
81     return FFMPEG_CONFIGURATION;
82 }
83
84 const char *avfilter_license(void)
85 {
86 #define LICENSE_PREFIX "libavfilter license: "
87     return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
88 }
89
90 void ff_command_queue_pop(AVFilterContext *filter)
91 {
92     AVFilterCommand *c= filter->command_queue;
93     av_freep(&c->arg);
94     av_freep(&c->command);
95     filter->command_queue= c->next;
96     av_free(c);
97 }
98
99 void ff_insert_pad(unsigned idx, unsigned *count, size_t padidx_off,
100                    AVFilterPad **pads, AVFilterLink ***links,
101                    AVFilterPad *newpad)
102 {
103     unsigned i;
104
105     idx = FFMIN(idx, *count);
106
107     *pads  = av_realloc(*pads,  sizeof(AVFilterPad)   * (*count + 1));
108     *links = av_realloc(*links, sizeof(AVFilterLink*) * (*count + 1));
109     memmove(*pads +idx+1, *pads +idx, sizeof(AVFilterPad)   * (*count-idx));
110     memmove(*links+idx+1, *links+idx, sizeof(AVFilterLink*) * (*count-idx));
111     memcpy(*pads+idx, newpad, sizeof(AVFilterPad));
112     (*links)[idx] = NULL;
113
114     (*count)++;
115     for (i = idx+1; i < *count; i++)
116         if (*links[i])
117             (*(unsigned *)((uint8_t *) *links[i] + padidx_off))++;
118 }
119
120 int avfilter_link(AVFilterContext *src, unsigned srcpad,
121                   AVFilterContext *dst, unsigned dstpad)
122 {
123     AVFilterLink *link;
124
125     if (src->output_count <= srcpad || dst->input_count <= dstpad ||
126         src->outputs[srcpad]        || dst->inputs[dstpad])
127         return -1;
128
129     if (src->output_pads[srcpad].type != dst->input_pads[dstpad].type) {
130         av_log(src, AV_LOG_ERROR,
131                "Media type mismatch between the '%s' filter output pad %d and the '%s' filter input pad %d\n",
132                src->name, srcpad, dst->name, dstpad);
133         return AVERROR(EINVAL);
134     }
135
136     src->outputs[srcpad] =
137     dst-> inputs[dstpad] = link = av_mallocz(sizeof(AVFilterLink));
138
139     link->src     = src;
140     link->dst     = dst;
141     link->srcpad  = &src->output_pads[srcpad];
142     link->dstpad  = &dst->input_pads[dstpad];
143     link->type    = src->output_pads[srcpad].type;
144     assert(PIX_FMT_NONE == -1 && AV_SAMPLE_FMT_NONE == -1);
145     link->format  = -1;
146
147     return 0;
148 }
149
150 void avfilter_link_free(AVFilterLink **link)
151 {
152     if (!*link)
153         return;
154
155     if ((*link)->pool)
156         ff_free_pool((*link)->pool);
157
158     av_freep(link);
159 }
160
161 int avfilter_insert_filter(AVFilterLink *link, AVFilterContext *filt,
162                            unsigned filt_srcpad_idx, unsigned filt_dstpad_idx)
163 {
164     int ret;
165     unsigned dstpad_idx = link->dstpad - link->dst->input_pads;
166
167     av_log(link->dst, AV_LOG_INFO, "auto-inserting filter '%s' "
168            "between the filter '%s' and the filter '%s'\n",
169            filt->name, link->src->name, link->dst->name);
170
171     link->dst->inputs[dstpad_idx] = NULL;
172     if ((ret = avfilter_link(filt, filt_dstpad_idx, link->dst, dstpad_idx)) < 0) {
173         /* failed to link output filter to new filter */
174         link->dst->inputs[dstpad_idx] = link;
175         return ret;
176     }
177
178     /* re-hookup the link to the new destination filter we inserted */
179     link->dst = filt;
180     link->dstpad = &filt->input_pads[filt_srcpad_idx];
181     filt->inputs[filt_srcpad_idx] = link;
182
183     /* if any information on supported media formats already exists on the
184      * link, we need to preserve that */
185     if (link->out_formats)
186         ff_formats_changeref(&link->out_formats,
187                                    &filt->outputs[filt_dstpad_idx]->out_formats);
188
189     if (link->out_samplerates)
190         ff_formats_changeref(&link->out_samplerates,
191                                    &filt->outputs[filt_dstpad_idx]->out_samplerates);
192     if (link->out_channel_layouts)
193         ff_channel_layouts_changeref(&link->out_channel_layouts,
194                                      &filt->outputs[filt_dstpad_idx]->out_channel_layouts);
195
196     return 0;
197 }
198
199 int avfilter_config_links(AVFilterContext *filter)
200 {
201     int (*config_link)(AVFilterLink *);
202     unsigned i;
203     int ret;
204
205     for (i = 0; i < filter->input_count; i ++) {
206         AVFilterLink *link = filter->inputs[i];
207         AVFilterLink *inlink = link->src->input_count ?
208             link->src->inputs[0] : NULL;
209
210         if (!link) continue;
211
212         link->current_pts = AV_NOPTS_VALUE;
213
214         switch (link->init_state) {
215         case AVLINK_INIT:
216             continue;
217         case AVLINK_STARTINIT:
218             av_log(filter, AV_LOG_INFO, "circular filter chain detected\n");
219             return 0;
220         case AVLINK_UNINIT:
221             link->init_state = AVLINK_STARTINIT;
222
223             if ((ret = avfilter_config_links(link->src)) < 0)
224                 return ret;
225
226             if (!(config_link = link->srcpad->config_props)) {
227                 if (link->src->input_count != 1) {
228                     av_log(link->src, AV_LOG_ERROR, "Source filters and filters "
229                                                     "with more than one input "
230                                                     "must set config_props() "
231                                                     "callbacks on all outputs\n");
232                     return AVERROR(EINVAL);
233                 }
234             } else if ((ret = config_link(link)) < 0)
235                 return ret;
236
237             switch (link->type) {
238             case AVMEDIA_TYPE_VIDEO:
239                 if (!link->time_base.num && !link->time_base.den)
240                     link->time_base = inlink ? inlink->time_base : AV_TIME_BASE_Q;
241
242                 if (!link->sample_aspect_ratio.num && !link->sample_aspect_ratio.den)
243                     link->sample_aspect_ratio = inlink ?
244                         inlink->sample_aspect_ratio : (AVRational){1,1};
245
246                 if (inlink) {
247                     if (!link->w)
248                         link->w = inlink->w;
249                     if (!link->h)
250                         link->h = inlink->h;
251                 } else if (!link->w || !link->h) {
252                     av_log(link->src, AV_LOG_ERROR,
253                            "Video source filters must set their output link's "
254                            "width and height\n");
255                     return AVERROR(EINVAL);
256                 }
257                 break;
258
259             case AVMEDIA_TYPE_AUDIO:
260                 if (inlink) {
261                     if (!link->sample_rate)
262                         link->sample_rate = inlink->sample_rate;
263                     if (!link->time_base.num && !link->time_base.den)
264                         link->time_base = inlink->time_base;
265                 } else if (!link->sample_rate) {
266                     av_log(link->src, AV_LOG_ERROR,
267                            "Audio source filters must set their output link's "
268                            "sample_rate\n");
269                     return AVERROR(EINVAL);
270                 }
271
272                 if (!link->time_base.num && !link->time_base.den)
273                     link->time_base = (AVRational) {1, link->sample_rate};
274             }
275
276             if ((config_link = link->dstpad->config_props))
277                 if ((ret = config_link(link)) < 0)
278                     return ret;
279
280             link->init_state = AVLINK_INIT;
281         }
282     }
283
284     return 0;
285 }
286
287 void ff_dlog_link(void *ctx, AVFilterLink *link, int end)
288 {
289     if (link->type == AVMEDIA_TYPE_VIDEO) {
290         av_dlog(ctx,
291                 "link[%p s:%dx%d fmt:%s %s->%s]%s",
292                 link, link->w, link->h,
293                 av_pix_fmt_descriptors[link->format].name,
294                 link->src ? link->src->filter->name : "",
295                 link->dst ? link->dst->filter->name : "",
296                 end ? "\n" : "");
297     } else {
298         char buf[128];
299         av_get_channel_layout_string(buf, sizeof(buf), -1, link->channel_layout);
300
301         av_dlog(ctx,
302                 "link[%p r:%d cl:%s fmt:%s %s->%s]%s",
303                 link, (int)link->sample_rate, buf,
304                 av_get_sample_fmt_name(link->format),
305                 link->src ? link->src->filter->name : "",
306                 link->dst ? link->dst->filter->name : "",
307                 end ? "\n" : "");
308     }
309 }
310
311 int ff_request_frame(AVFilterLink *link)
312 {
313     FF_DPRINTF_START(NULL, request_frame); ff_dlog_link(NULL, link, 1);
314
315     if (link->srcpad->request_frame)
316         return link->srcpad->request_frame(link);
317     else if (link->src->inputs[0])
318         return ff_request_frame(link->src->inputs[0]);
319     else return -1;
320 }
321
322 int ff_poll_frame(AVFilterLink *link)
323 {
324     int i, min = INT_MAX;
325
326     if (link->srcpad->poll_frame)
327         return link->srcpad->poll_frame(link);
328
329     for (i = 0; i < link->src->input_count; i++) {
330         int val;
331         if (!link->src->inputs[i])
332             return -1;
333         val = ff_poll_frame(link->src->inputs[i]);
334         min = FFMIN(min, val);
335     }
336
337     return min;
338 }
339
340 void ff_update_link_current_pts(AVFilterLink *link, int64_t pts)
341 {
342     if (pts == AV_NOPTS_VALUE)
343         return;
344     link->current_pts =  pts; /* TODO use duration */
345     if (link->graph && link->age_index >= 0)
346         ff_avfilter_graph_update_heap(link->graph, link);
347 }
348
349 int avfilter_process_command(AVFilterContext *filter, const char *cmd, const char *arg, char *res, int res_len, int flags)
350 {
351     if(!strcmp(cmd, "ping")){
352         av_strlcatf(res, res_len, "pong from:%s %s\n", filter->filter->name, filter->name);
353         return 0;
354     }else if(filter->filter->process_command) {
355         return filter->filter->process_command(filter, cmd, arg, res, res_len, flags);
356     }
357     return AVERROR(ENOSYS);
358 }
359
360 #define MAX_REGISTERED_AVFILTERS_NB 128
361
362 static AVFilter *registered_avfilters[MAX_REGISTERED_AVFILTERS_NB + 1];
363
364 static int next_registered_avfilter_idx = 0;
365
366 AVFilter *avfilter_get_by_name(const char *name)
367 {
368     int i;
369
370     for (i = 0; registered_avfilters[i]; i++)
371         if (!strcmp(registered_avfilters[i]->name, name))
372             return registered_avfilters[i];
373
374     return NULL;
375 }
376
377 int avfilter_register(AVFilter *filter)
378 {
379     if (next_registered_avfilter_idx == MAX_REGISTERED_AVFILTERS_NB) {
380         av_log(NULL, AV_LOG_ERROR,
381                "Maximum number of registered filters %d reached, "
382                "impossible to register filter with name '%s'\n",
383                MAX_REGISTERED_AVFILTERS_NB, filter->name);
384         return AVERROR(ENOMEM);
385     }
386
387     registered_avfilters[next_registered_avfilter_idx++] = filter;
388     return 0;
389 }
390
391 AVFilter **av_filter_next(AVFilter **filter)
392 {
393     return filter ? ++filter : &registered_avfilters[0];
394 }
395
396 void avfilter_uninit(void)
397 {
398     memset(registered_avfilters, 0, sizeof(registered_avfilters));
399     next_registered_avfilter_idx = 0;
400 }
401
402 static int pad_count(const AVFilterPad *pads)
403 {
404     int count;
405
406     for(count = 0; pads->name; count ++) pads ++;
407     return count;
408 }
409
410 static char *default_filter_name(void *filter_ctx)
411 {
412     AVFilterContext *ctx = filter_ctx;
413     return ctx->name ? ctx->name : ctx->filter->name;
414 }
415
416 static const AVClass avfilter_class = {
417     .class_name = "AVFilter",
418     .item_name  = default_filter_name,
419     .version    = LIBAVUTIL_VERSION_INT,
420     .category = AV_CLASS_CATEGORY_FILTER,
421 };
422
423 int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *inst_name)
424 {
425     AVFilterContext *ret;
426     *filter_ctx = NULL;
427
428     if (!filter)
429         return AVERROR(EINVAL);
430
431     ret = av_mallocz(sizeof(AVFilterContext));
432     if (!ret)
433         return AVERROR(ENOMEM);
434
435     ret->av_class = &avfilter_class;
436     ret->filter   = filter;
437     ret->name     = inst_name ? av_strdup(inst_name) : NULL;
438     if (filter->priv_size) {
439         ret->priv     = av_mallocz(filter->priv_size);
440         if (!ret->priv)
441             goto err;
442     }
443
444     ret->input_count  = pad_count(filter->inputs);
445     if (ret->input_count) {
446         ret->input_pads   = av_malloc(sizeof(AVFilterPad) * ret->input_count);
447         if (!ret->input_pads)
448             goto err;
449         memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad) * ret->input_count);
450         ret->inputs       = av_mallocz(sizeof(AVFilterLink*) * ret->input_count);
451         if (!ret->inputs)
452             goto err;
453     }
454
455     ret->output_count = pad_count(filter->outputs);
456     if (ret->output_count) {
457         ret->output_pads  = av_malloc(sizeof(AVFilterPad) * ret->output_count);
458         if (!ret->output_pads)
459             goto err;
460         memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad) * ret->output_count);
461         ret->outputs      = av_mallocz(sizeof(AVFilterLink*) * ret->output_count);
462         if (!ret->outputs)
463             goto err;
464     }
465
466     *filter_ctx = ret;
467     return 0;
468
469 err:
470     av_freep(&ret->inputs);
471     av_freep(&ret->input_pads);
472     ret->input_count = 0;
473     av_freep(&ret->outputs);
474     av_freep(&ret->output_pads);
475     ret->output_count = 0;
476     av_freep(&ret->priv);
477     av_free(ret);
478     return AVERROR(ENOMEM);
479 }
480
481 void avfilter_free(AVFilterContext *filter)
482 {
483     int i;
484     AVFilterLink *link;
485
486     if (!filter)
487         return;
488
489     if (filter->filter->uninit)
490         filter->filter->uninit(filter);
491
492     for (i = 0; i < filter->input_count; i++) {
493         if ((link = filter->inputs[i])) {
494             if (link->src)
495                 link->src->outputs[link->srcpad - link->src->output_pads] = NULL;
496             ff_formats_unref(&link->in_formats);
497             ff_formats_unref(&link->out_formats);
498             ff_formats_unref(&link->in_samplerates);
499             ff_formats_unref(&link->out_samplerates);
500             ff_channel_layouts_unref(&link->in_channel_layouts);
501             ff_channel_layouts_unref(&link->out_channel_layouts);
502         }
503         avfilter_link_free(&link);
504     }
505     for (i = 0; i < filter->output_count; i++) {
506         if ((link = filter->outputs[i])) {
507             if (link->dst)
508                 link->dst->inputs[link->dstpad - link->dst->input_pads] = NULL;
509             ff_formats_unref(&link->in_formats);
510             ff_formats_unref(&link->out_formats);
511             ff_formats_unref(&link->in_samplerates);
512             ff_formats_unref(&link->out_samplerates);
513             ff_channel_layouts_unref(&link->in_channel_layouts);
514             ff_channel_layouts_unref(&link->out_channel_layouts);
515         }
516         avfilter_link_free(&link);
517     }
518
519     av_freep(&filter->name);
520     av_freep(&filter->input_pads);
521     av_freep(&filter->output_pads);
522     av_freep(&filter->inputs);
523     av_freep(&filter->outputs);
524     av_freep(&filter->priv);
525     while(filter->command_queue){
526         ff_command_queue_pop(filter);
527     }
528     av_free(filter);
529 }
530
531 int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
532 {
533     int ret=0;
534
535     if (filter->filter->init)
536         ret = filter->filter->init(filter, args, opaque);
537     return ret;
538 }
539
540 #if FF_API_DEFAULT_CONFIG_OUTPUT_LINK
541 void avfilter_insert_pad(unsigned idx, unsigned *count, size_t padidx_off,
542                          AVFilterPad **pads, AVFilterLink ***links,
543                          AVFilterPad *newpad)
544 {
545     ff_insert_pad(idx, count, padidx_off, pads, links, newpad);
546 }
547 void avfilter_insert_inpad(AVFilterContext *f, unsigned index,
548                            AVFilterPad *p)
549 {
550     ff_insert_pad(index, &f->input_count, offsetof(AVFilterLink, dstpad),
551                   &f->input_pads, &f->inputs, p);
552 }
553 void avfilter_insert_outpad(AVFilterContext *f, unsigned index,
554                             AVFilterPad *p)
555 {
556     ff_insert_pad(index, &f->output_count, offsetof(AVFilterLink, srcpad),
557                   &f->output_pads, &f->outputs, p);
558 }
559 int avfilter_poll_frame(AVFilterLink *link)
560 {
561     return ff_poll_frame(link);
562 }
563 int avfilter_request_frame(AVFilterLink *link)
564 {
565     return ff_request_frame(link);
566 }
567 #endif