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