]> git.sesse.net Git - ffmpeg/blob - libavfilter/avfilter.c
lavfi: move tracing utilities definition from video.c to avfilter.c
[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 avfilter_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         avfilter_formats_changeref(&link->out_formats,
187                                    &filt->outputs[filt_dstpad_idx]->out_formats);
188     if (link->out_channel_layouts)
189         ff_channel_layouts_changeref(&link->out_channel_layouts,
190                                      &filt->outputs[filt_dstpad_idx]->out_channel_layouts);
191     if (link->out_samplerates)
192         avfilter_formats_changeref(&link->out_samplerates,
193                                    &filt->outputs[filt_dstpad_idx]->out_samplerates);
194
195     return 0;
196 }
197
198 int avfilter_config_links(AVFilterContext *filter)
199 {
200     int (*config_link)(AVFilterLink *);
201     unsigned i;
202     int ret;
203
204     for (i = 0; i < filter->input_count; i ++) {
205         AVFilterLink *link = filter->inputs[i];
206         AVFilterLink *inlink = link->src->input_count ?
207             link->src->inputs[0] : NULL;
208
209         if (!link) continue;
210
211         link->current_pts = AV_NOPTS_VALUE;
212
213         switch (link->init_state) {
214         case AVLINK_INIT:
215             continue;
216         case AVLINK_STARTINIT:
217             av_log(filter, AV_LOG_INFO, "circular filter chain detected\n");
218             return 0;
219         case AVLINK_UNINIT:
220             link->init_state = AVLINK_STARTINIT;
221
222             if ((ret = avfilter_config_links(link->src)) < 0)
223                 return ret;
224
225             if (!(config_link = link->srcpad->config_props)) {
226                 if (link->src->input_count != 1) {
227                     av_log(link->src, AV_LOG_ERROR, "Source filters and filters "
228                                                     "with more than one input "
229                                                     "must set config_props() "
230                                                     "callbacks on all outputs\n");
231                     return AVERROR(EINVAL);
232                 }
233             } else if ((ret = config_link(link)) < 0)
234                 return ret;
235
236             switch (link->type) {
237             case AVMEDIA_TYPE_VIDEO:
238                 if (!link->time_base.num && !link->time_base.den)
239                     link->time_base = inlink ? inlink->time_base : AV_TIME_BASE_Q;
240
241                 if (!link->sample_aspect_ratio.num && !link->sample_aspect_ratio.den)
242                     link->sample_aspect_ratio = inlink ?
243                         inlink->sample_aspect_ratio : (AVRational){1,1};
244
245                 if (inlink) {
246                     if (!link->w)
247                         link->w = inlink->w;
248                     if (!link->h)
249                         link->h = inlink->h;
250                 } else if (!link->w || !link->h) {
251                     av_log(link->src, AV_LOG_ERROR,
252                            "Video source filters must set their output link's "
253                            "width and height\n");
254                     return AVERROR(EINVAL);
255                 }
256                 break;
257
258             case AVMEDIA_TYPE_AUDIO:
259                 if (inlink) {
260                     if (!link->sample_rate)
261                         link->sample_rate = inlink->sample_rate;
262                     if (!link->time_base.num && !link->time_base.den)
263                         link->time_base = inlink->time_base;
264                 } else if (!link->sample_rate) {
265                     av_log(link->src, AV_LOG_ERROR,
266                            "Audio source filters must set their output link's "
267                            "sample_rate\n");
268                     return AVERROR(EINVAL);
269                 }
270
271                 if (!link->time_base.num && !link->time_base.den)
272                     link->time_base = (AVRational) {1, link->sample_rate};
273             }
274
275             if ((config_link = link->dstpad->config_props))
276                 if ((ret = config_link(link)) < 0)
277                     return ret;
278
279             link->init_state = AVLINK_INIT;
280         }
281     }
282
283     return 0;
284 }
285
286 void ff_dlog_link(void *ctx, AVFilterLink *link, int end)
287 {
288     if (link->type == AVMEDIA_TYPE_VIDEO) {
289         av_dlog(ctx,
290                 "link[%p s:%dx%d fmt:%s %s->%s]%s",
291                 link, link->w, link->h,
292                 av_pix_fmt_descriptors[link->format].name,
293                 link->src ? link->src->filter->name : "",
294                 link->dst ? link->dst->filter->name : "",
295                 end ? "\n" : "");
296     } else {
297         char buf[128];
298         av_get_channel_layout_string(buf, sizeof(buf), -1, link->channel_layout);
299
300         av_dlog(ctx,
301                 "link[%p r:%d cl:%s fmt:%s %s->%s]%s",
302                 link, (int)link->sample_rate, buf,
303                 av_get_sample_fmt_name(link->format),
304                 link->src ? link->src->filter->name : "",
305                 link->dst ? link->dst->filter->name : "",
306                 end ? "\n" : "");
307     }
308 }
309
310 int avfilter_request_frame(AVFilterLink *link)
311 {
312     FF_DPRINTF_START(NULL, request_frame); ff_dlog_link(NULL, link, 1);
313
314     if (link->srcpad->request_frame)
315         return link->srcpad->request_frame(link);
316     else if (link->src->inputs[0])
317         return avfilter_request_frame(link->src->inputs[0]);
318     else return -1;
319 }
320
321 int avfilter_poll_frame(AVFilterLink *link)
322 {
323     int i, min = INT_MAX;
324
325     if (link->srcpad->poll_frame)
326         return link->srcpad->poll_frame(link);
327
328     for (i = 0; i < link->src->input_count; i++) {
329         int val;
330         if (!link->src->inputs[i])
331             return -1;
332         val = avfilter_poll_frame(link->src->inputs[i]);
333         min = FFMIN(min, val);
334     }
335
336     return min;
337 }
338
339 void ff_update_link_current_pts(AVFilterLink *link, int64_t pts)
340 {
341     if (pts == AV_NOPTS_VALUE)
342         return;
343     link->current_pts =  pts; /* TODO use duration */
344     if (link->graph && link->age_index >= 0)
345         ff_avfilter_graph_update_heap(link->graph, link);
346 }
347
348 int avfilter_process_command(AVFilterContext *filter, const char *cmd, const char *arg, char *res, int res_len, int flags)
349 {
350     if(!strcmp(cmd, "ping")){
351         av_strlcatf(res, res_len, "pong from:%s %s\n", filter->filter->name, filter->name);
352         return 0;
353     }else if(filter->filter->process_command) {
354         return filter->filter->process_command(filter, cmd, arg, res, res_len, flags);
355     }
356     return AVERROR(ENOSYS);
357 }
358
359 #define MAX_REGISTERED_AVFILTERS_NB 128
360
361 static AVFilter *registered_avfilters[MAX_REGISTERED_AVFILTERS_NB + 1];
362
363 static int next_registered_avfilter_idx = 0;
364
365 AVFilter *avfilter_get_by_name(const char *name)
366 {
367     int i;
368
369     for (i = 0; registered_avfilters[i]; i++)
370         if (!strcmp(registered_avfilters[i]->name, name))
371             return registered_avfilters[i];
372
373     return NULL;
374 }
375
376 int avfilter_register(AVFilter *filter)
377 {
378     if (next_registered_avfilter_idx == MAX_REGISTERED_AVFILTERS_NB) {
379         av_log(NULL, AV_LOG_ERROR,
380                "Maximum number of registered filters %d reached, "
381                "impossible to register filter with name '%s'\n",
382                MAX_REGISTERED_AVFILTERS_NB, filter->name);
383         return AVERROR(ENOMEM);
384     }
385
386     registered_avfilters[next_registered_avfilter_idx++] = filter;
387     return 0;
388 }
389
390 AVFilter **av_filter_next(AVFilter **filter)
391 {
392     return filter ? ++filter : &registered_avfilters[0];
393 }
394
395 void avfilter_uninit(void)
396 {
397     memset(registered_avfilters, 0, sizeof(registered_avfilters));
398     next_registered_avfilter_idx = 0;
399 }
400
401 static int pad_count(const AVFilterPad *pads)
402 {
403     int count;
404
405     for(count = 0; pads->name; count ++) pads ++;
406     return count;
407 }
408
409 const char *avfilter_default_filter_name(void *filter_ctx)
410 {
411     AVFilterContext *ctx = filter_ctx;
412     return ctx->name ? ctx->name : ctx->filter->name;
413 }
414
415 static const AVClass avfilter_class = {
416     .class_name = "AVFilter",
417     .item_name  = avfilter_default_filter_name,
418     .version    = LIBAVUTIL_VERSION_INT,
419     .category = AV_CLASS_CATEGORY_FILTER,
420 };
421
422 int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *inst_name)
423 {
424     AVFilterContext *ret;
425     *filter_ctx = NULL;
426
427     if (!filter)
428         return AVERROR(EINVAL);
429
430     ret = av_mallocz(sizeof(AVFilterContext));
431     if (!ret)
432         return AVERROR(ENOMEM);
433
434     ret->av_class = &avfilter_class;
435     ret->filter   = filter;
436     ret->name     = inst_name ? av_strdup(inst_name) : NULL;
437     if (filter->priv_size) {
438         ret->priv     = av_mallocz(filter->priv_size);
439         if (!ret->priv)
440             goto err;
441     }
442
443     ret->input_count  = pad_count(filter->inputs);
444     if (ret->input_count) {
445         ret->input_pads   = av_malloc(sizeof(AVFilterPad) * ret->input_count);
446         if (!ret->input_pads)
447             goto err;
448         memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad) * ret->input_count);
449         ret->inputs       = av_mallocz(sizeof(AVFilterLink*) * ret->input_count);
450         if (!ret->inputs)
451             goto err;
452     }
453
454     ret->output_count = pad_count(filter->outputs);
455     if (ret->output_count) {
456         ret->output_pads  = av_malloc(sizeof(AVFilterPad) * ret->output_count);
457         if (!ret->output_pads)
458             goto err;
459         memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad) * ret->output_count);
460         ret->outputs      = av_mallocz(sizeof(AVFilterLink*) * ret->output_count);
461         if (!ret->outputs)
462             goto err;
463     }
464
465     *filter_ctx = ret;
466     return 0;
467
468 err:
469     av_freep(&ret->inputs);
470     av_freep(&ret->input_pads);
471     ret->input_count = 0;
472     av_freep(&ret->outputs);
473     av_freep(&ret->output_pads);
474     ret->output_count = 0;
475     av_freep(&ret->priv);
476     av_free(ret);
477     return AVERROR(ENOMEM);
478 }
479
480 void avfilter_free(AVFilterContext *filter)
481 {
482     int i;
483     AVFilterLink *link;
484
485     if (!filter)
486         return;
487
488     if (filter->filter->uninit)
489         filter->filter->uninit(filter);
490
491     for (i = 0; i < filter->input_count; i++) {
492         if ((link = filter->inputs[i])) {
493             if (link->src)
494                 link->src->outputs[link->srcpad - link->src->output_pads] = NULL;
495             avfilter_formats_unref(&link->in_formats);
496             avfilter_formats_unref(&link->out_formats);
497             avfilter_formats_unref(&link->in_samplerates);
498             avfilter_formats_unref(&link->out_samplerates);
499             ff_channel_layouts_unref(&link->in_channel_layouts);
500             ff_channel_layouts_unref(&link->out_channel_layouts);
501         }
502         avfilter_link_free(&link);
503     }
504     for (i = 0; i < filter->output_count; i++) {
505         if ((link = filter->outputs[i])) {
506             if (link->dst)
507                 link->dst->inputs[link->dstpad - link->dst->input_pads] = NULL;
508             avfilter_formats_unref(&link->in_formats);
509             avfilter_formats_unref(&link->out_formats);
510             avfilter_formats_unref(&link->in_samplerates);
511             avfilter_formats_unref(&link->out_samplerates);
512             ff_channel_layouts_unref(&link->in_channel_layouts);
513             ff_channel_layouts_unref(&link->out_channel_layouts);
514         }
515         avfilter_link_free(&link);
516     }
517
518     av_freep(&filter->name);
519     av_freep(&filter->input_pads);
520     av_freep(&filter->output_pads);
521     av_freep(&filter->inputs);
522     av_freep(&filter->outputs);
523     av_freep(&filter->priv);
524     while(filter->command_queue){
525         ff_command_queue_pop(filter);
526     }
527     av_free(filter);
528 }
529
530 int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
531 {
532     int ret=0;
533
534     if (filter->filter->init)
535         ret = filter->filter->init(filter, args, opaque);
536     return ret;
537 }