]> git.sesse.net Git - ffmpeg/blob - libavfilter/avfilter.c
Merge commit '1470ce21cec5ee26e106e2a884c26bbf84e5aaea'
[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 #include "libavutil/pixdesc.h"
23 #include "libavutil/rational.h"
24 #include "libavutil/audioconvert.h"
25 #include "libavutil/avassert.h"
26 #include "libavutil/avstring.h"
27
28 #include "avfilter.h"
29 #include "formats.h"
30 #include "internal.h"
31 #include "audio.h"
32
33 char *ff_get_ref_perms_string(char *buf, size_t buf_size, int perms)
34 {
35     snprintf(buf, buf_size, "%s%s%s%s%s%s",
36              perms & AV_PERM_READ      ? "r" : "",
37              perms & AV_PERM_WRITE     ? "w" : "",
38              perms & AV_PERM_PRESERVE  ? "p" : "",
39              perms & AV_PERM_REUSE     ? "u" : "",
40              perms & AV_PERM_REUSE2    ? "U" : "",
41              perms & AV_PERM_NEG_LINESIZES ? "n" : "");
42     return buf;
43 }
44
45 void ff_tlog_ref(void *ctx, AVFilterBufferRef *ref, int end)
46 {
47     av_unused char buf[16];
48     ff_tlog(ctx,
49             "ref[%p buf:%p refcount:%d perms:%s data:%p linesize[%d, %d, %d, %d] pts:%"PRId64" pos:%"PRId64,
50             ref, ref->buf, ref->buf->refcount, ff_get_ref_perms_string(buf, sizeof(buf), ref->perms), ref->data[0],
51             ref->linesize[0], ref->linesize[1], ref->linesize[2], ref->linesize[3],
52             ref->pts, ref->pos);
53
54     if (ref->video) {
55         ff_tlog(ctx, " a:%d/%d s:%dx%d i:%c iskey:%d type:%c",
56                 ref->video->sample_aspect_ratio.num, ref->video->sample_aspect_ratio.den,
57                 ref->video->w, ref->video->h,
58                 !ref->video->interlaced     ? 'P' :         /* Progressive  */
59                 ref->video->top_field_first ? 'T' : 'B',    /* Top / Bottom */
60                 ref->video->key_frame,
61                 av_get_picture_type_char(ref->video->pict_type));
62     }
63     if (ref->audio) {
64         ff_tlog(ctx, " cl:%"PRId64"d n:%d r:%d",
65                 ref->audio->channel_layout,
66                 ref->audio->nb_samples,
67                 ref->audio->sample_rate);
68     }
69
70     ff_tlog(ctx, "]%s", end ? "\n" : "");
71 }
72
73 unsigned avfilter_version(void) {
74     av_assert0(LIBAVFILTER_VERSION_MICRO >= 100);
75     return LIBAVFILTER_VERSION_INT;
76 }
77
78 const char *avfilter_configuration(void)
79 {
80     return FFMPEG_CONFIGURATION;
81 }
82
83 const char *avfilter_license(void)
84 {
85 #define LICENSE_PREFIX "libavfilter license: "
86     return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
87 }
88
89 void ff_command_queue_pop(AVFilterContext *filter)
90 {
91     AVFilterCommand *c= filter->command_queue;
92     av_freep(&c->arg);
93     av_freep(&c->command);
94     filter->command_queue= c->next;
95     av_free(c);
96 }
97
98 void ff_insert_pad(unsigned idx, unsigned *count, size_t padidx_off,
99                    AVFilterPad **pads, AVFilterLink ***links,
100                    AVFilterPad *newpad)
101 {
102     unsigned i;
103
104     idx = FFMIN(idx, *count);
105
106     *pads  = av_realloc(*pads,  sizeof(AVFilterPad)   * (*count + 1));
107     *links = av_realloc(*links, sizeof(AVFilterLink*) * (*count + 1));
108     memmove(*pads +idx+1, *pads +idx, sizeof(AVFilterPad)   * (*count-idx));
109     memmove(*links+idx+1, *links+idx, sizeof(AVFilterLink*) * (*count-idx));
110     memcpy(*pads+idx, newpad, sizeof(AVFilterPad));
111     (*links)[idx] = NULL;
112
113     (*count)++;
114     for (i = idx+1; i < *count; i++)
115         if (*links[i])
116             (*(unsigned *)((uint8_t *) *links[i] + padidx_off))++;
117 }
118
119 int avfilter_link(AVFilterContext *src, unsigned srcpad,
120                   AVFilterContext *dst, unsigned dstpad)
121 {
122     AVFilterLink *link;
123
124     if (src->nb_outputs <= srcpad || dst->nb_inputs <= dstpad ||
125         src->outputs[srcpad]      || dst->inputs[dstpad])
126         return -1;
127
128     if (src->output_pads[srcpad].type != dst->input_pads[dstpad].type) {
129         av_log(src, AV_LOG_ERROR,
130                "Media type mismatch between the '%s' filter output pad %d (%s) and the '%s' filter input pad %d (%s)\n",
131                src->name, srcpad, (char *)av_x_if_null(av_get_media_type_string(src->output_pads[srcpad].type), "?"),
132                dst->name, dstpad, (char *)av_x_if_null(av_get_media_type_string(dst-> input_pads[dstpad].type), "?"));
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     av_assert0(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_VERBOSE, "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->nb_inputs; i ++) {
206         AVFilterLink *link = filter->inputs[i];
207         AVFilterLink *inlink = link->src->nb_inputs ?
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->nb_inputs != 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                 av_log(link->src, AV_LOG_ERROR,
236                        "Failed to configure output pad on %s\n",
237                        link->src->name);
238                 return ret;
239             }
240
241             switch (link->type) {
242             case AVMEDIA_TYPE_VIDEO:
243                 if (!link->time_base.num && !link->time_base.den)
244                     link->time_base = inlink ? inlink->time_base : AV_TIME_BASE_Q;
245
246                 if (!link->sample_aspect_ratio.num && !link->sample_aspect_ratio.den)
247                     link->sample_aspect_ratio = inlink ?
248                         inlink->sample_aspect_ratio : (AVRational){1,1};
249
250                 if (inlink && !link->frame_rate.num && !link->frame_rate.den)
251                     link->frame_rate = inlink->frame_rate;
252
253                 if (inlink) {
254                     if (!link->w)
255                         link->w = inlink->w;
256                     if (!link->h)
257                         link->h = inlink->h;
258                 } else if (!link->w || !link->h) {
259                     av_log(link->src, AV_LOG_ERROR,
260                            "Video source filters must set their output link's "
261                            "width and height\n");
262                     return AVERROR(EINVAL);
263                 }
264                 break;
265
266             case AVMEDIA_TYPE_AUDIO:
267                 if (inlink) {
268                     if (!link->sample_rate)
269                         link->sample_rate = inlink->sample_rate;
270                     if (!link->time_base.num && !link->time_base.den)
271                         link->time_base = inlink->time_base;
272                 } else if (!link->sample_rate) {
273                     av_log(link->src, AV_LOG_ERROR,
274                            "Audio source filters must set their output link's "
275                            "sample_rate\n");
276                     return AVERROR(EINVAL);
277                 }
278
279                 if (!link->time_base.num && !link->time_base.den)
280                     link->time_base = (AVRational) {1, link->sample_rate};
281             }
282
283             if ((config_link = link->dstpad->config_props))
284                 if ((ret = config_link(link)) < 0) {
285                     av_log(link->src, AV_LOG_ERROR,
286                            "Failed to configure input pad on %s\n",
287                            link->dst->name);
288                     return ret;
289                 }
290
291             link->init_state = AVLINK_INIT;
292         }
293     }
294
295     return 0;
296 }
297
298 void ff_tlog_link(void *ctx, AVFilterLink *link, int end)
299 {
300     if (link->type == AVMEDIA_TYPE_VIDEO) {
301         ff_tlog(ctx,
302                 "link[%p s:%dx%d fmt:%s %s->%s]%s",
303                 link, link->w, link->h,
304                 av_pix_fmt_descriptors[link->format].name,
305                 link->src ? link->src->filter->name : "",
306                 link->dst ? link->dst->filter->name : "",
307                 end ? "\n" : "");
308     } else {
309         char buf[128];
310         av_get_channel_layout_string(buf, sizeof(buf), -1, link->channel_layout);
311
312         ff_tlog(ctx,
313                 "link[%p r:%d cl:%s fmt:%s %s->%s]%s",
314                 link, (int)link->sample_rate, buf,
315                 av_get_sample_fmt_name(link->format),
316                 link->src ? link->src->filter->name : "",
317                 link->dst ? link->dst->filter->name : "",
318                 end ? "\n" : "");
319     }
320 }
321
322 int ff_request_frame(AVFilterLink *link)
323 {
324     int ret = -1;
325     FF_TPRINTF_START(NULL, request_frame); ff_tlog_link(NULL, link, 1);
326
327     if (link->srcpad->request_frame)
328         ret = link->srcpad->request_frame(link);
329     else if (link->src->inputs[0])
330         ret = ff_request_frame(link->src->inputs[0]);
331     if (ret == AVERROR_EOF && link->partial_buf) {
332         AVFilterBufferRef *pbuf = link->partial_buf;
333         link->partial_buf = NULL;
334         ff_filter_samples_framed(link, pbuf);
335         return 0;
336     }
337     return ret;
338 }
339
340 int ff_poll_frame(AVFilterLink *link)
341 {
342     int i, min = INT_MAX;
343
344     if (link->srcpad->poll_frame)
345         return link->srcpad->poll_frame(link);
346
347     for (i = 0; i < link->src->nb_inputs; i++) {
348         int val;
349         if (!link->src->inputs[i])
350             return -1;
351         val = ff_poll_frame(link->src->inputs[i]);
352         min = FFMIN(min, val);
353     }
354
355     return min;
356 }
357
358 void ff_update_link_current_pts(AVFilterLink *link, int64_t pts)
359 {
360     if (pts == AV_NOPTS_VALUE)
361         return;
362     link->current_pts = av_rescale_q(pts, link->time_base, AV_TIME_BASE_Q);
363     /* TODO use duration */
364     if (link->graph && link->age_index >= 0)
365         ff_avfilter_graph_update_heap(link->graph, link);
366 }
367
368 int avfilter_process_command(AVFilterContext *filter, const char *cmd, const char *arg, char *res, int res_len, int flags)
369 {
370     if(!strcmp(cmd, "ping")){
371         av_strlcatf(res, res_len, "pong from:%s %s\n", filter->filter->name, filter->name);
372         return 0;
373     }else if(filter->filter->process_command) {
374         return filter->filter->process_command(filter, cmd, arg, res, res_len, flags);
375     }
376     return AVERROR(ENOSYS);
377 }
378
379 #define MAX_REGISTERED_AVFILTERS_NB 128
380
381 static AVFilter *registered_avfilters[MAX_REGISTERED_AVFILTERS_NB + 1];
382
383 static int next_registered_avfilter_idx = 0;
384
385 AVFilter *avfilter_get_by_name(const char *name)
386 {
387     int i;
388
389     for (i = 0; registered_avfilters[i]; i++)
390         if (!strcmp(registered_avfilters[i]->name, name))
391             return registered_avfilters[i];
392
393     return NULL;
394 }
395
396 int avfilter_register(AVFilter *filter)
397 {
398     if (next_registered_avfilter_idx == MAX_REGISTERED_AVFILTERS_NB) {
399         av_log(NULL, AV_LOG_ERROR,
400                "Maximum number of registered filters %d reached, "
401                "impossible to register filter with name '%s'\n",
402                MAX_REGISTERED_AVFILTERS_NB, filter->name);
403         return AVERROR(ENOMEM);
404     }
405
406     registered_avfilters[next_registered_avfilter_idx++] = filter;
407     return 0;
408 }
409
410 AVFilter **av_filter_next(AVFilter **filter)
411 {
412     return filter ? ++filter : &registered_avfilters[0];
413 }
414
415 void avfilter_uninit(void)
416 {
417     memset(registered_avfilters, 0, sizeof(registered_avfilters));
418     next_registered_avfilter_idx = 0;
419 }
420
421 static int pad_count(const AVFilterPad *pads)
422 {
423     int count;
424
425     for(count = 0; pads->name; count ++) pads ++;
426     return count;
427 }
428
429 static const char *default_filter_name(void *filter_ctx)
430 {
431     AVFilterContext *ctx = filter_ctx;
432     return ctx->name ? ctx->name : ctx->filter->name;
433 }
434
435 static const AVClass avfilter_class = {
436     .class_name = "AVFilter",
437     .item_name  = default_filter_name,
438     .version    = LIBAVUTIL_VERSION_INT,
439     .category   = AV_CLASS_CATEGORY_FILTER,
440 };
441
442 int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *inst_name)
443 {
444     AVFilterContext *ret;
445     *filter_ctx = NULL;
446
447     if (!filter)
448         return AVERROR(EINVAL);
449
450     ret = av_mallocz(sizeof(AVFilterContext));
451     if (!ret)
452         return AVERROR(ENOMEM);
453
454     ret->av_class = &avfilter_class;
455     ret->filter   = filter;
456     ret->name     = inst_name ? av_strdup(inst_name) : NULL;
457     if (filter->priv_size) {
458         ret->priv     = av_mallocz(filter->priv_size);
459         if (!ret->priv)
460             goto err;
461     }
462
463     ret->nb_inputs = pad_count(filter->inputs);
464     if (ret->nb_inputs ) {
465         ret->input_pads   = av_malloc(sizeof(AVFilterPad) * ret->nb_inputs);
466         if (!ret->input_pads)
467             goto err;
468         memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad) * ret->nb_inputs);
469         ret->inputs       = av_mallocz(sizeof(AVFilterLink*) * ret->nb_inputs);
470         if (!ret->inputs)
471             goto err;
472     }
473
474     ret->nb_outputs = pad_count(filter->outputs);
475     if (ret->nb_outputs) {
476         ret->output_pads  = av_malloc(sizeof(AVFilterPad) * ret->nb_outputs);
477         if (!ret->output_pads)
478             goto err;
479         memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad) * ret->nb_outputs);
480         ret->outputs      = av_mallocz(sizeof(AVFilterLink*) * ret->nb_outputs);
481         if (!ret->outputs)
482             goto err;
483     }
484 #if FF_API_FOO_COUNT
485     ret->output_count = ret->nb_outputs;
486     ret->input_count  = ret->nb_inputs;
487 #endif
488
489     *filter_ctx = ret;
490     return 0;
491
492 err:
493     av_freep(&ret->inputs);
494     av_freep(&ret->input_pads);
495     ret->nb_inputs = 0;
496     av_freep(&ret->outputs);
497     av_freep(&ret->output_pads);
498     ret->nb_outputs = 0;
499     av_freep(&ret->priv);
500     av_free(ret);
501     return AVERROR(ENOMEM);
502 }
503
504 void avfilter_free(AVFilterContext *filter)
505 {
506     int i;
507     AVFilterLink *link;
508
509     if (!filter)
510         return;
511
512     if (filter->filter->uninit)
513         filter->filter->uninit(filter);
514
515     for (i = 0; i < filter->nb_inputs; i++) {
516         if ((link = filter->inputs[i])) {
517             if (link->src)
518                 link->src->outputs[link->srcpad - link->src->output_pads] = NULL;
519             ff_formats_unref(&link->in_formats);
520             ff_formats_unref(&link->out_formats);
521             ff_formats_unref(&link->in_samplerates);
522             ff_formats_unref(&link->out_samplerates);
523             ff_channel_layouts_unref(&link->in_channel_layouts);
524             ff_channel_layouts_unref(&link->out_channel_layouts);
525         }
526         avfilter_link_free(&link);
527     }
528     for (i = 0; i < filter->nb_outputs; i++) {
529         if ((link = filter->outputs[i])) {
530             if (link->dst)
531                 link->dst->inputs[link->dstpad - link->dst->input_pads] = NULL;
532             ff_formats_unref(&link->in_formats);
533             ff_formats_unref(&link->out_formats);
534             ff_formats_unref(&link->in_samplerates);
535             ff_formats_unref(&link->out_samplerates);
536             ff_channel_layouts_unref(&link->in_channel_layouts);
537             ff_channel_layouts_unref(&link->out_channel_layouts);
538         }
539         avfilter_link_free(&link);
540     }
541
542     av_freep(&filter->name);
543     av_freep(&filter->input_pads);
544     av_freep(&filter->output_pads);
545     av_freep(&filter->inputs);
546     av_freep(&filter->outputs);
547     av_freep(&filter->priv);
548     while(filter->command_queue){
549         ff_command_queue_pop(filter);
550     }
551     av_free(filter);
552 }
553
554 int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
555 {
556     int ret=0;
557
558     if (filter->filter->init_opaque)
559         ret = filter->filter->init_opaque(filter, args, opaque);
560     else if (filter->filter->init)
561         ret = filter->filter->init(filter, args);
562     return ret;
563 }
564
565 const char *avfilter_pad_get_name(AVFilterPad *pads, int pad_idx)
566 {
567     return pads[pad_idx].name;
568 }
569
570 enum AVMediaType avfilter_pad_get_type(AVFilterPad *pads, int pad_idx)
571 {
572     return pads[pad_idx].type;
573 }