]> git.sesse.net Git - ffmpeg/blob - libavfilter/avfilter.c
Drop unnecessary 'l' length modifier when printfing double values.
[ffmpeg] / libavfilter / avfilter.c
1 /*
2  * filter layer
3  * Copyright (c) 2007 Bobby Bingham
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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  * Libav 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 Libav; 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/channel_layout.h"
25 #include "libavutil/common.h"
26 #include "libavutil/imgutils.h"
27 #include "libavutil/pixdesc.h"
28 #include "libavutil/rational.h"
29 #include "libavutil/samplefmt.h"
30
31 #include "audio.h"
32 #include "avfilter.h"
33 #include "formats.h"
34 #include "internal.h"
35 #include "video.h"
36
37 unsigned avfilter_version(void) {
38     return LIBAVFILTER_VERSION_INT;
39 }
40
41 const char *avfilter_configuration(void)
42 {
43     return LIBAV_CONFIGURATION;
44 }
45
46 const char *avfilter_license(void)
47 {
48 #define LICENSE_PREFIX "libavfilter license: "
49     return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
50 }
51
52 void ff_insert_pad(unsigned idx, unsigned *count, size_t padidx_off,
53                    AVFilterPad **pads, AVFilterLink ***links,
54                    AVFilterPad *newpad)
55 {
56     unsigned i;
57
58     idx = FFMIN(idx, *count);
59
60     *pads  = av_realloc(*pads,  sizeof(AVFilterPad)   * (*count + 1));
61     *links = av_realloc(*links, sizeof(AVFilterLink*) * (*count + 1));
62     memmove(*pads +idx+1, *pads +idx, sizeof(AVFilterPad)   * (*count-idx));
63     memmove(*links+idx+1, *links+idx, sizeof(AVFilterLink*) * (*count-idx));
64     memcpy(*pads+idx, newpad, sizeof(AVFilterPad));
65     (*links)[idx] = NULL;
66
67     (*count)++;
68     for (i = idx+1; i < *count; i++)
69         if (*links[i])
70             (*(unsigned *)((uint8_t *) *links[i] + padidx_off))++;
71 }
72
73 int avfilter_link(AVFilterContext *src, unsigned srcpad,
74                   AVFilterContext *dst, unsigned dstpad)
75 {
76     AVFilterLink *link;
77
78     if (src->nb_outputs <= srcpad || dst->nb_inputs <= dstpad ||
79         src->outputs[srcpad]      || dst->inputs[dstpad])
80         return -1;
81
82     if (src->output_pads[srcpad].type != dst->input_pads[dstpad].type) {
83         av_log(src, AV_LOG_ERROR,
84                "Media type mismatch between the '%s' filter output pad %d and the '%s' filter input pad %d\n",
85                src->name, srcpad, dst->name, dstpad);
86         return AVERROR(EINVAL);
87     }
88
89     src->outputs[srcpad] =
90     dst-> inputs[dstpad] = link = av_mallocz(sizeof(AVFilterLink));
91
92     link->src     = src;
93     link->dst     = dst;
94     link->srcpad  = &src->output_pads[srcpad];
95     link->dstpad  = &dst->input_pads[dstpad];
96     link->type    = src->output_pads[srcpad].type;
97     assert(AV_PIX_FMT_NONE == -1 && AV_SAMPLE_FMT_NONE == -1);
98     link->format  = -1;
99
100     return 0;
101 }
102
103 int avfilter_insert_filter(AVFilterLink *link, AVFilterContext *filt,
104                            unsigned filt_srcpad_idx, unsigned filt_dstpad_idx)
105 {
106     int ret;
107     unsigned dstpad_idx = link->dstpad - link->dst->input_pads;
108
109     av_log(link->dst, AV_LOG_VERBOSE, "auto-inserting filter '%s' "
110            "between the filter '%s' and the filter '%s'\n",
111            filt->name, link->src->name, link->dst->name);
112
113     link->dst->inputs[dstpad_idx] = NULL;
114     if ((ret = avfilter_link(filt, filt_dstpad_idx, link->dst, dstpad_idx)) < 0) {
115         /* failed to link output filter to new filter */
116         link->dst->inputs[dstpad_idx] = link;
117         return ret;
118     }
119
120     /* re-hookup the link to the new destination filter we inserted */
121     link->dst = filt;
122     link->dstpad = &filt->input_pads[filt_srcpad_idx];
123     filt->inputs[filt_srcpad_idx] = link;
124
125     /* if any information on supported media formats already exists on the
126      * link, we need to preserve that */
127     if (link->out_formats)
128         ff_formats_changeref(&link->out_formats,
129                                    &filt->outputs[filt_dstpad_idx]->out_formats);
130     if (link->out_samplerates)
131         ff_formats_changeref(&link->out_samplerates,
132                                    &filt->outputs[filt_dstpad_idx]->out_samplerates);
133     if (link->out_channel_layouts)
134         ff_channel_layouts_changeref(&link->out_channel_layouts,
135                                      &filt->outputs[filt_dstpad_idx]->out_channel_layouts);
136
137     return 0;
138 }
139
140 int avfilter_config_links(AVFilterContext *filter)
141 {
142     int (*config_link)(AVFilterLink *);
143     unsigned i;
144     int ret;
145
146     for (i = 0; i < filter->nb_inputs; i ++) {
147         AVFilterLink *link = filter->inputs[i];
148
149         if (!link) continue;
150
151         switch (link->init_state) {
152         case AVLINK_INIT:
153             continue;
154         case AVLINK_STARTINIT:
155             av_log(filter, AV_LOG_INFO, "circular filter chain detected\n");
156             return 0;
157         case AVLINK_UNINIT:
158             link->init_state = AVLINK_STARTINIT;
159
160             if ((ret = avfilter_config_links(link->src)) < 0)
161                 return ret;
162
163             if (!(config_link = link->srcpad->config_props)) {
164                 if (link->src->nb_inputs != 1) {
165                     av_log(link->src, AV_LOG_ERROR, "Source filters and filters "
166                                                     "with more than one input "
167                                                     "must set config_props() "
168                                                     "callbacks on all outputs\n");
169                     return AVERROR(EINVAL);
170                 }
171             } else if ((ret = config_link(link)) < 0) {
172                 av_log(link->src, AV_LOG_ERROR,
173                        "Failed to configure output pad on %s\n",
174                        link->src->name);
175                 return ret;
176             }
177
178             if (link->time_base.num == 0 && link->time_base.den == 0)
179                 link->time_base = link->src && link->src->nb_inputs ?
180                     link->src->inputs[0]->time_base : AV_TIME_BASE_Q;
181
182             if (link->type == AVMEDIA_TYPE_VIDEO) {
183                 if (!link->sample_aspect_ratio.num && !link->sample_aspect_ratio.den)
184                     link->sample_aspect_ratio = link->src->nb_inputs ?
185                         link->src->inputs[0]->sample_aspect_ratio : (AVRational){1,1};
186
187                 if (link->src->nb_inputs) {
188                     if (!link->w)
189                         link->w = link->src->inputs[0]->w;
190                     if (!link->h)
191                         link->h = link->src->inputs[0]->h;
192                 } else if (!link->w || !link->h) {
193                     av_log(link->src, AV_LOG_ERROR,
194                            "Video source filters must set their output link's "
195                            "width and height\n");
196                     return AVERROR(EINVAL);
197                 }
198             }
199
200             if ((config_link = link->dstpad->config_props))
201                 if ((ret = config_link(link)) < 0) {
202                     av_log(link->src, AV_LOG_ERROR,
203                            "Failed to configure input pad on %s\n",
204                            link->dst->name);
205                     return ret;
206                 }
207
208             link->init_state = AVLINK_INIT;
209         }
210     }
211
212     return 0;
213 }
214
215 void ff_dlog_link(void *ctx, AVFilterLink *link, int end)
216 {
217     if (link->type == AVMEDIA_TYPE_VIDEO) {
218         av_dlog(ctx,
219                 "link[%p s:%dx%d fmt:%-16s %-16s->%-16s]%s",
220                 link, link->w, link->h,
221                 av_get_pix_fmt_name(link->format),
222                 link->src ? link->src->filter->name : "",
223                 link->dst ? link->dst->filter->name : "",
224                 end ? "\n" : "");
225     } else {
226         char buf[128];
227         av_get_channel_layout_string(buf, sizeof(buf), -1, link->channel_layout);
228
229         av_dlog(ctx,
230                 "link[%p r:%d cl:%s fmt:%-16s %-16s->%-16s]%s",
231                 link, link->sample_rate, buf,
232                 av_get_sample_fmt_name(link->format),
233                 link->src ? link->src->filter->name : "",
234                 link->dst ? link->dst->filter->name : "",
235                 end ? "\n" : "");
236     }
237 }
238
239 int ff_request_frame(AVFilterLink *link)
240 {
241     FF_DPRINTF_START(NULL, request_frame); ff_dlog_link(NULL, link, 1);
242
243     if (link->srcpad->request_frame)
244         return link->srcpad->request_frame(link);
245     else if (link->src->inputs[0])
246         return ff_request_frame(link->src->inputs[0]);
247     else return -1;
248 }
249
250 int ff_poll_frame(AVFilterLink *link)
251 {
252     int i, min = INT_MAX;
253
254     if (link->srcpad->poll_frame)
255         return link->srcpad->poll_frame(link);
256
257     for (i = 0; i < link->src->nb_inputs; i++) {
258         int val;
259         if (!link->src->inputs[i])
260             return -1;
261         val = ff_poll_frame(link->src->inputs[i]);
262         min = FFMIN(min, val);
263     }
264
265     return min;
266 }
267
268 #define MAX_REGISTERED_AVFILTERS_NB 64
269
270 static AVFilter *registered_avfilters[MAX_REGISTERED_AVFILTERS_NB + 1];
271
272 static int next_registered_avfilter_idx = 0;
273
274 AVFilter *avfilter_get_by_name(const char *name)
275 {
276     int i;
277
278     for (i = 0; registered_avfilters[i]; i++)
279         if (!strcmp(registered_avfilters[i]->name, name))
280             return registered_avfilters[i];
281
282     return NULL;
283 }
284
285 int avfilter_register(AVFilter *filter)
286 {
287     if (next_registered_avfilter_idx == MAX_REGISTERED_AVFILTERS_NB)
288         return -1;
289
290     registered_avfilters[next_registered_avfilter_idx++] = filter;
291     return 0;
292 }
293
294 AVFilter **av_filter_next(AVFilter **filter)
295 {
296     return filter ? ++filter : &registered_avfilters[0];
297 }
298
299 void avfilter_uninit(void)
300 {
301     memset(registered_avfilters, 0, sizeof(registered_avfilters));
302     next_registered_avfilter_idx = 0;
303 }
304
305 static int pad_count(const AVFilterPad *pads)
306 {
307     int count;
308
309     if (!pads)
310         return 0;
311
312     for(count = 0; pads->name; count ++) pads ++;
313     return count;
314 }
315
316 static const char *filter_name(void *p)
317 {
318     AVFilterContext *filter = p;
319     return filter->filter->name;
320 }
321
322 static const AVClass avfilter_class = {
323     "AVFilter",
324     filter_name,
325     NULL,
326     LIBAVUTIL_VERSION_INT,
327 };
328
329 int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *inst_name)
330 {
331     AVFilterContext *ret;
332     *filter_ctx = NULL;
333
334     if (!filter)
335         return AVERROR(EINVAL);
336
337     ret = av_mallocz(sizeof(AVFilterContext));
338     if (!ret)
339         return AVERROR(ENOMEM);
340
341     ret->av_class = &avfilter_class;
342     ret->filter   = filter;
343     ret->name     = inst_name ? av_strdup(inst_name) : NULL;
344     if (filter->priv_size) {
345         ret->priv     = av_mallocz(filter->priv_size);
346         if (!ret->priv)
347             goto err;
348     }
349
350     ret->nb_inputs = pad_count(filter->inputs);
351     if (ret->nb_inputs ) {
352         ret->input_pads   = av_malloc(sizeof(AVFilterPad) * ret->nb_inputs);
353         if (!ret->input_pads)
354             goto err;
355         memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad) * ret->nb_inputs);
356         ret->inputs       = av_mallocz(sizeof(AVFilterLink*) * ret->nb_inputs);
357         if (!ret->inputs)
358             goto err;
359     }
360
361     ret->nb_outputs = pad_count(filter->outputs);
362     if (ret->nb_outputs) {
363         ret->output_pads  = av_malloc(sizeof(AVFilterPad) * ret->nb_outputs);
364         if (!ret->output_pads)
365             goto err;
366         memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad) * ret->nb_outputs);
367         ret->outputs      = av_mallocz(sizeof(AVFilterLink*) * ret->nb_outputs);
368         if (!ret->outputs)
369             goto err;
370     }
371 #if FF_API_FOO_COUNT
372     ret->output_count = ret->nb_outputs;
373     ret->input_count  = ret->nb_inputs;
374 #endif
375
376     *filter_ctx = ret;
377     return 0;
378
379 err:
380     av_freep(&ret->inputs);
381     av_freep(&ret->input_pads);
382     ret->nb_inputs = 0;
383     av_freep(&ret->outputs);
384     av_freep(&ret->output_pads);
385     ret->nb_outputs = 0;
386     av_freep(&ret->priv);
387     av_free(ret);
388     return AVERROR(ENOMEM);
389 }
390
391 void avfilter_free(AVFilterContext *filter)
392 {
393     int i;
394     AVFilterLink *link;
395
396     if (filter->filter->uninit)
397         filter->filter->uninit(filter);
398
399     for (i = 0; i < filter->nb_inputs; i++) {
400         if ((link = filter->inputs[i])) {
401             if (link->src)
402                 link->src->outputs[link->srcpad - link->src->output_pads] = NULL;
403             ff_formats_unref(&link->in_formats);
404             ff_formats_unref(&link->out_formats);
405             ff_formats_unref(&link->in_samplerates);
406             ff_formats_unref(&link->out_samplerates);
407             ff_channel_layouts_unref(&link->in_channel_layouts);
408             ff_channel_layouts_unref(&link->out_channel_layouts);
409         }
410         av_freep(&link);
411     }
412     for (i = 0; i < filter->nb_outputs; i++) {
413         if ((link = filter->outputs[i])) {
414             if (link->dst)
415                 link->dst->inputs[link->dstpad - link->dst->input_pads] = NULL;
416             ff_formats_unref(&link->in_formats);
417             ff_formats_unref(&link->out_formats);
418             ff_formats_unref(&link->in_samplerates);
419             ff_formats_unref(&link->out_samplerates);
420             ff_channel_layouts_unref(&link->in_channel_layouts);
421             ff_channel_layouts_unref(&link->out_channel_layouts);
422         }
423         av_freep(&link);
424     }
425
426     av_freep(&filter->name);
427     av_freep(&filter->input_pads);
428     av_freep(&filter->output_pads);
429     av_freep(&filter->inputs);
430     av_freep(&filter->outputs);
431     av_freep(&filter->priv);
432     av_free(filter);
433 }
434
435 int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
436 {
437     int ret=0;
438
439     if (filter->filter->init)
440         ret = filter->filter->init(filter, args);
441     return ret;
442 }
443
444 const char *avfilter_pad_get_name(AVFilterPad *pads, int pad_idx)
445 {
446     return pads[pad_idx].name;
447 }
448
449 enum AVMediaType avfilter_pad_get_type(AVFilterPad *pads, int pad_idx)
450 {
451     return pads[pad_idx].type;
452 }
453
454 static int default_filter_frame(AVFilterLink *link, AVFilterBufferRef *frame)
455 {
456     return ff_filter_frame(link->dst->outputs[0], frame);
457 }
458
459 int ff_filter_frame(AVFilterLink *link, AVFilterBufferRef *frame)
460 {
461     int (*filter_frame)(AVFilterLink *, AVFilterBufferRef *);
462     AVFilterPad *dst = link->dstpad;
463     AVFilterBufferRef *out;
464     int perms = frame->perms;
465
466     FF_DPRINTF_START(NULL, filter_frame);
467     ff_dlog_link(NULL, link, 1);
468
469     if (!(filter_frame = dst->filter_frame))
470         filter_frame = default_filter_frame;
471
472     if (frame->linesize[0] < 0)
473         perms |= AV_PERM_NEG_LINESIZES;
474     /* prepare to copy the frame if the buffer has insufficient permissions */
475     if ((dst->min_perms & perms) != dst->min_perms ||
476         dst->rej_perms & perms) {
477         av_log(link->dst, AV_LOG_DEBUG,
478                "Copying data in avfilter (have perms %x, need %x, reject %x)\n",
479                perms, link->dstpad->min_perms, link->dstpad->rej_perms);
480
481         switch (link->type) {
482         case AVMEDIA_TYPE_VIDEO:
483             out = ff_get_video_buffer(link, dst->min_perms,
484                                       link->w, link->h);
485             break;
486         case AVMEDIA_TYPE_AUDIO:
487             out = ff_get_audio_buffer(link, dst->min_perms,
488                                       frame->audio->nb_samples);
489             break;
490         default: return AVERROR(EINVAL);
491         }
492         if (!out) {
493             avfilter_unref_buffer(frame);
494             return AVERROR(ENOMEM);
495         }
496         avfilter_copy_buffer_ref_props(out, frame);
497
498         switch (link->type) {
499         case AVMEDIA_TYPE_VIDEO:
500             av_image_copy(out->data, out->linesize, frame->data, frame->linesize,
501                           frame->format, frame->video->w, frame->video->h);
502             break;
503         case AVMEDIA_TYPE_AUDIO:
504             av_samples_copy(out->extended_data, frame->extended_data,
505                             0, 0, frame->audio->nb_samples,
506                             av_get_channel_layout_nb_channels(frame->audio->channel_layout),
507                             frame->format);
508             break;
509         default: return AVERROR(EINVAL);
510         }
511
512         avfilter_unref_buffer(frame);
513     } else
514         out = frame;
515
516     return filter_frame(link, out);
517 }