]> 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 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 ff_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         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->input_count; i ++) {
206         AVFilterLink *link = filter->inputs[i];
207         AVFilterLink *inlink = link->src->input_count ?
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->input_count != 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                 return ret;
236
237             switch (link->type) {
238             case AVMEDIA_TYPE_VIDEO:
239                 if (!link->time_base.num && !link->time_base.den)
240                     link->time_base = inlink ? inlink->time_base : AV_TIME_BASE_Q;
241
242                 if (!link->sample_aspect_ratio.num && !link->sample_aspect_ratio.den)
243                     link->sample_aspect_ratio = inlink ?
244                         inlink->sample_aspect_ratio : (AVRational){1,1};
245
246                 if (inlink && !link->frame_rate.num && !link->frame_rate.den)
247                     link->frame_rate = inlink->frame_rate;
248
249                 if (inlink) {
250                     if (!link->w)
251                         link->w = inlink->w;
252                     if (!link->h)
253                         link->h = inlink->h;
254                 } else if (!link->w || !link->h) {
255                     av_log(link->src, AV_LOG_ERROR,
256                            "Video source filters must set their output link's "
257                            "width and height\n");
258                     return AVERROR(EINVAL);
259                 }
260                 break;
261
262             case AVMEDIA_TYPE_AUDIO:
263                 if (inlink) {
264                     if (!link->sample_rate)
265                         link->sample_rate = inlink->sample_rate;
266                     if (!link->time_base.num && !link->time_base.den)
267                         link->time_base = inlink->time_base;
268                 } else if (!link->sample_rate) {
269                     av_log(link->src, AV_LOG_ERROR,
270                            "Audio source filters must set their output link's "
271                            "sample_rate\n");
272                     return AVERROR(EINVAL);
273                 }
274
275                 if (!link->time_base.num && !link->time_base.den)
276                     link->time_base = (AVRational) {1, link->sample_rate};
277             }
278
279             if ((config_link = link->dstpad->config_props))
280                 if ((ret = config_link(link)) < 0)
281                     return ret;
282
283             link->init_state = AVLINK_INIT;
284         }
285     }
286
287     return 0;
288 }
289
290 void ff_dlog_link(void *ctx, AVFilterLink *link, int end)
291 {
292     if (link->type == AVMEDIA_TYPE_VIDEO) {
293         av_dlog(ctx,
294                 "link[%p s:%dx%d fmt:%s %s->%s]%s",
295                 link, link->w, link->h,
296                 av_pix_fmt_descriptors[link->format].name,
297                 link->src ? link->src->filter->name : "",
298                 link->dst ? link->dst->filter->name : "",
299                 end ? "\n" : "");
300     } else {
301         char buf[128];
302         av_get_channel_layout_string(buf, sizeof(buf), -1, link->channel_layout);
303
304         av_dlog(ctx,
305                 "link[%p r:%d cl:%s fmt:%s %s->%s]%s",
306                 link, (int)link->sample_rate, buf,
307                 av_get_sample_fmt_name(link->format),
308                 link->src ? link->src->filter->name : "",
309                 link->dst ? link->dst->filter->name : "",
310                 end ? "\n" : "");
311     }
312 }
313
314 int ff_request_frame(AVFilterLink *link)
315 {
316     FF_DPRINTF_START(NULL, request_frame); ff_dlog_link(NULL, link, 1);
317
318     if (link->srcpad->request_frame)
319         return link->srcpad->request_frame(link);
320     else if (link->src->inputs[0])
321         return ff_request_frame(link->src->inputs[0]);
322     else return -1;
323 }
324
325 int ff_poll_frame(AVFilterLink *link)
326 {
327     int i, min = INT_MAX;
328
329     if (link->srcpad->poll_frame)
330         return link->srcpad->poll_frame(link);
331
332     for (i = 0; i < link->src->input_count; i++) {
333         int val;
334         if (!link->src->inputs[i])
335             return -1;
336         val = ff_poll_frame(link->src->inputs[i]);
337         min = FFMIN(min, val);
338     }
339
340     return min;
341 }
342
343 void ff_update_link_current_pts(AVFilterLink *link, int64_t pts)
344 {
345     if (pts == AV_NOPTS_VALUE)
346         return;
347     link->current_pts =  pts; /* TODO use duration */
348     if (link->graph && link->age_index >= 0)
349         ff_avfilter_graph_update_heap(link->graph, link);
350 }
351
352 int avfilter_process_command(AVFilterContext *filter, const char *cmd, const char *arg, char *res, int res_len, int flags)
353 {
354     if(!strcmp(cmd, "ping")){
355         av_strlcatf(res, res_len, "pong from:%s %s\n", filter->filter->name, filter->name);
356         return 0;
357     }else if(filter->filter->process_command) {
358         return filter->filter->process_command(filter, cmd, arg, res, res_len, flags);
359     }
360     return AVERROR(ENOSYS);
361 }
362
363 #define MAX_REGISTERED_AVFILTERS_NB 128
364
365 static AVFilter *registered_avfilters[MAX_REGISTERED_AVFILTERS_NB + 1];
366
367 static int next_registered_avfilter_idx = 0;
368
369 AVFilter *avfilter_get_by_name(const char *name)
370 {
371     int i;
372
373     for (i = 0; registered_avfilters[i]; i++)
374         if (!strcmp(registered_avfilters[i]->name, name))
375             return registered_avfilters[i];
376
377     return NULL;
378 }
379
380 int avfilter_register(AVFilter *filter)
381 {
382     if (next_registered_avfilter_idx == MAX_REGISTERED_AVFILTERS_NB) {
383         av_log(NULL, AV_LOG_ERROR,
384                "Maximum number of registered filters %d reached, "
385                "impossible to register filter with name '%s'\n",
386                MAX_REGISTERED_AVFILTERS_NB, filter->name);
387         return AVERROR(ENOMEM);
388     }
389
390     registered_avfilters[next_registered_avfilter_idx++] = filter;
391     return 0;
392 }
393
394 AVFilter **av_filter_next(AVFilter **filter)
395 {
396     return filter ? ++filter : &registered_avfilters[0];
397 }
398
399 void avfilter_uninit(void)
400 {
401     memset(registered_avfilters, 0, sizeof(registered_avfilters));
402     next_registered_avfilter_idx = 0;
403 }
404
405 static int pad_count(const AVFilterPad *pads)
406 {
407     int count;
408
409     for(count = 0; pads->name; count ++) pads ++;
410     return count;
411 }
412
413 static const char *default_filter_name(void *filter_ctx)
414 {
415     AVFilterContext *ctx = filter_ctx;
416     return ctx->name ? ctx->name : ctx->filter->name;
417 }
418
419 static const AVClass avfilter_class = {
420     .class_name = "AVFilter",
421     .item_name  = default_filter_name,
422     .version    = LIBAVUTIL_VERSION_INT,
423     .category = AV_CLASS_CATEGORY_FILTER,
424 };
425
426 int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *inst_name)
427 {
428     AVFilterContext *ret;
429     *filter_ctx = NULL;
430
431     if (!filter)
432         return AVERROR(EINVAL);
433
434     ret = av_mallocz(sizeof(AVFilterContext));
435     if (!ret)
436         return AVERROR(ENOMEM);
437
438     ret->av_class = &avfilter_class;
439     ret->filter   = filter;
440     ret->name     = inst_name ? av_strdup(inst_name) : NULL;
441     if (filter->priv_size) {
442         ret->priv     = av_mallocz(filter->priv_size);
443         if (!ret->priv)
444             goto err;
445     }
446
447     ret->input_count  = pad_count(filter->inputs);
448     if (ret->input_count) {
449         ret->input_pads   = av_malloc(sizeof(AVFilterPad) * ret->input_count);
450         if (!ret->input_pads)
451             goto err;
452         memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad) * ret->input_count);
453         ret->inputs       = av_mallocz(sizeof(AVFilterLink*) * ret->input_count);
454         if (!ret->inputs)
455             goto err;
456     }
457
458     ret->output_count = pad_count(filter->outputs);
459     if (ret->output_count) {
460         ret->output_pads  = av_malloc(sizeof(AVFilterPad) * ret->output_count);
461         if (!ret->output_pads)
462             goto err;
463         memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad) * ret->output_count);
464         ret->outputs      = av_mallocz(sizeof(AVFilterLink*) * ret->output_count);
465         if (!ret->outputs)
466             goto err;
467     }
468
469     *filter_ctx = ret;
470     return 0;
471
472 err:
473     av_freep(&ret->inputs);
474     av_freep(&ret->input_pads);
475     ret->input_count = 0;
476     av_freep(&ret->outputs);
477     av_freep(&ret->output_pads);
478     ret->output_count = 0;
479     av_freep(&ret->priv);
480     av_free(ret);
481     return AVERROR(ENOMEM);
482 }
483
484 void avfilter_free(AVFilterContext *filter)
485 {
486     int i;
487     AVFilterLink *link;
488
489     if (!filter)
490         return;
491
492     if (filter->filter->uninit)
493         filter->filter->uninit(filter);
494
495     for (i = 0; i < filter->input_count; i++) {
496         if ((link = filter->inputs[i])) {
497             if (link->src)
498                 link->src->outputs[link->srcpad - link->src->output_pads] = NULL;
499             ff_formats_unref(&link->in_formats);
500             ff_formats_unref(&link->out_formats);
501             ff_formats_unref(&link->in_samplerates);
502             ff_formats_unref(&link->out_samplerates);
503             ff_channel_layouts_unref(&link->in_channel_layouts);
504             ff_channel_layouts_unref(&link->out_channel_layouts);
505         }
506         avfilter_link_free(&link);
507     }
508     for (i = 0; i < filter->output_count; i++) {
509         if ((link = filter->outputs[i])) {
510             if (link->dst)
511                 link->dst->inputs[link->dstpad - link->dst->input_pads] = NULL;
512             ff_formats_unref(&link->in_formats);
513             ff_formats_unref(&link->out_formats);
514             ff_formats_unref(&link->in_samplerates);
515             ff_formats_unref(&link->out_samplerates);
516             ff_channel_layouts_unref(&link->in_channel_layouts);
517             ff_channel_layouts_unref(&link->out_channel_layouts);
518         }
519         avfilter_link_free(&link);
520     }
521
522     av_freep(&filter->name);
523     av_freep(&filter->input_pads);
524     av_freep(&filter->output_pads);
525     av_freep(&filter->inputs);
526     av_freep(&filter->outputs);
527     av_freep(&filter->priv);
528     while(filter->command_queue){
529         ff_command_queue_pop(filter);
530     }
531     av_free(filter);
532 }
533
534 int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
535 {
536     int ret=0;
537
538     if (filter->filter->init)
539         ret = filter->filter->init(filter, args, opaque);
540     return ret;
541 }
542
543 #if FF_API_DEFAULT_CONFIG_OUTPUT_LINK
544 void avfilter_insert_pad(unsigned idx, unsigned *count, size_t padidx_off,
545                          AVFilterPad **pads, AVFilterLink ***links,
546                          AVFilterPad *newpad)
547 {
548     ff_insert_pad(idx, count, padidx_off, pads, links, newpad);
549 }
550 void avfilter_insert_inpad(AVFilterContext *f, unsigned index,
551                            AVFilterPad *p)
552 {
553     ff_insert_pad(index, &f->input_count, offsetof(AVFilterLink, dstpad),
554                   &f->input_pads, &f->inputs, p);
555 }
556 void avfilter_insert_outpad(AVFilterContext *f, unsigned index,
557                             AVFilterPad *p)
558 {
559     ff_insert_pad(index, &f->output_count, offsetof(AVFilterLink, srcpad),
560                   &f->output_pads, &f->outputs, p);
561 }
562 int avfilter_poll_frame(AVFilterLink *link)
563 {
564     return ff_poll_frame(link);
565 }
566 int avfilter_request_frame(AVFilterLink *link)
567 {
568     return ff_request_frame(link);
569 }
570 #endif