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