]> git.sesse.net Git - ffmpeg/blob - libavfilter/avfilter.c
Merge commit '0db2d94280e260af5f3ad7993c5a6357462f17c9'
[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/common.h"
23 #include "libavutil/pixdesc.h"
24 #include "libavutil/rational.h"
25 #include "libavutil/audioconvert.h"
26 #include "libavutil/avassert.h"
27 #include "libavutil/avstring.h"
28
29 #include "avfilter.h"
30 #include "formats.h"
31 #include "internal.h"
32 #include "audio.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_tlog_ref(void *ctx, AVFilterBufferRef *ref, int end)
47 {
48     av_unused char buf[16];
49     ff_tlog(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         ff_tlog(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         ff_tlog(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     ff_tlog(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->nb_outputs <= srcpad || dst->nb_inputs <= 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 (%s) and the '%s' filter input pad %d (%s)\n",
132                src->name, srcpad, (char *)av_x_if_null(av_get_media_type_string(src->output_pads[srcpad].type), "?"),
133                dst->name, dstpad, (char *)av_x_if_null(av_get_media_type_string(dst-> input_pads[dstpad].type), "?"));
134         return AVERROR(EINVAL);
135     }
136
137     src->outputs[srcpad] =
138     dst-> inputs[dstpad] = link = av_mallocz(sizeof(AVFilterLink));
139
140     link->src     = src;
141     link->dst     = dst;
142     link->srcpad  = &src->output_pads[srcpad];
143     link->dstpad  = &dst->input_pads[dstpad];
144     link->type    = src->output_pads[srcpad].type;
145     av_assert0(PIX_FMT_NONE == -1 && AV_SAMPLE_FMT_NONE == -1);
146     link->format  = -1;
147
148     return 0;
149 }
150
151 void avfilter_link_free(AVFilterLink **link)
152 {
153     if (!*link)
154         return;
155
156     if ((*link)->pool)
157         ff_free_pool((*link)->pool);
158
159     avfilter_unref_bufferp(&(*link)->partial_buf);
160
161     av_freep(link);
162 }
163
164 void avfilter_link_set_closed(AVFilterLink *link, int closed)
165 {
166     link->closed = closed;
167 }
168
169 int avfilter_insert_filter(AVFilterLink *link, AVFilterContext *filt,
170                            unsigned filt_srcpad_idx, unsigned filt_dstpad_idx)
171 {
172     int ret;
173     unsigned dstpad_idx = link->dstpad - link->dst->input_pads;
174
175     av_log(link->dst, AV_LOG_VERBOSE, "auto-inserting filter '%s' "
176            "between the filter '%s' and the filter '%s'\n",
177            filt->name, link->src->name, link->dst->name);
178
179     link->dst->inputs[dstpad_idx] = NULL;
180     if ((ret = avfilter_link(filt, filt_dstpad_idx, link->dst, dstpad_idx)) < 0) {
181         /* failed to link output filter to new filter */
182         link->dst->inputs[dstpad_idx] = link;
183         return ret;
184     }
185
186     /* re-hookup the link to the new destination filter we inserted */
187     link->dst = filt;
188     link->dstpad = &filt->input_pads[filt_srcpad_idx];
189     filt->inputs[filt_srcpad_idx] = link;
190
191     /* if any information on supported media formats already exists on the
192      * link, we need to preserve that */
193     if (link->out_formats)
194         ff_formats_changeref(&link->out_formats,
195                                    &filt->outputs[filt_dstpad_idx]->out_formats);
196
197     if (link->out_samplerates)
198         ff_formats_changeref(&link->out_samplerates,
199                                    &filt->outputs[filt_dstpad_idx]->out_samplerates);
200     if (link->out_channel_layouts)
201         ff_channel_layouts_changeref(&link->out_channel_layouts,
202                                      &filt->outputs[filt_dstpad_idx]->out_channel_layouts);
203
204     return 0;
205 }
206
207 int avfilter_config_links(AVFilterContext *filter)
208 {
209     int (*config_link)(AVFilterLink *);
210     unsigned i;
211     int ret;
212
213     for (i = 0; i < filter->nb_inputs; i ++) {
214         AVFilterLink *link = filter->inputs[i];
215         AVFilterLink *inlink = link->src->nb_inputs ?
216             link->src->inputs[0] : NULL;
217
218         if (!link) continue;
219
220         link->current_pts = AV_NOPTS_VALUE;
221
222         switch (link->init_state) {
223         case AVLINK_INIT:
224             continue;
225         case AVLINK_STARTINIT:
226             av_log(filter, AV_LOG_INFO, "circular filter chain detected\n");
227             return 0;
228         case AVLINK_UNINIT:
229             link->init_state = AVLINK_STARTINIT;
230
231             if ((ret = avfilter_config_links(link->src)) < 0)
232                 return ret;
233
234             if (!(config_link = link->srcpad->config_props)) {
235                 if (link->src->nb_inputs != 1) {
236                     av_log(link->src, AV_LOG_ERROR, "Source filters and filters "
237                                                     "with more than one input "
238                                                     "must set config_props() "
239                                                     "callbacks on all outputs\n");
240                     return AVERROR(EINVAL);
241                 }
242             } else if ((ret = config_link(link)) < 0) {
243                 av_log(link->src, AV_LOG_ERROR,
244                        "Failed to configure output pad on %s\n",
245                        link->src->name);
246                 return ret;
247             }
248
249             switch (link->type) {
250             case AVMEDIA_TYPE_VIDEO:
251                 if (!link->time_base.num && !link->time_base.den)
252                     link->time_base = inlink ? inlink->time_base : AV_TIME_BASE_Q;
253
254                 if (!link->sample_aspect_ratio.num && !link->sample_aspect_ratio.den)
255                     link->sample_aspect_ratio = inlink ?
256                         inlink->sample_aspect_ratio : (AVRational){1,1};
257
258                 if (inlink && !link->frame_rate.num && !link->frame_rate.den)
259                     link->frame_rate = inlink->frame_rate;
260
261                 if (inlink) {
262                     if (!link->w)
263                         link->w = inlink->w;
264                     if (!link->h)
265                         link->h = inlink->h;
266                 } else if (!link->w || !link->h) {
267                     av_log(link->src, AV_LOG_ERROR,
268                            "Video source filters must set their output link's "
269                            "width and height\n");
270                     return AVERROR(EINVAL);
271                 }
272                 break;
273
274             case AVMEDIA_TYPE_AUDIO:
275                 if (inlink) {
276                     if (!link->sample_rate)
277                         link->sample_rate = inlink->sample_rate;
278                     if (!link->time_base.num && !link->time_base.den)
279                         link->time_base = inlink->time_base;
280                     if (!link->channel_layout)
281                         link->channel_layout = inlink->channel_layout;
282                 } else if (!link->sample_rate) {
283                     av_log(link->src, AV_LOG_ERROR,
284                            "Audio source filters must set their output link's "
285                            "sample_rate\n");
286                     return AVERROR(EINVAL);
287                 }
288
289                 if (!link->time_base.num && !link->time_base.den)
290                     link->time_base = (AVRational) {1, link->sample_rate};
291             }
292
293             if ((config_link = link->dstpad->config_props))
294                 if ((ret = config_link(link)) < 0) {
295                     av_log(link->src, AV_LOG_ERROR,
296                            "Failed to configure input pad on %s\n",
297                            link->dst->name);
298                     return ret;
299                 }
300
301             link->init_state = AVLINK_INIT;
302         }
303     }
304
305     return 0;
306 }
307
308 void ff_tlog_link(void *ctx, AVFilterLink *link, int end)
309 {
310     if (link->type == AVMEDIA_TYPE_VIDEO) {
311         ff_tlog(ctx,
312                 "link[%p s:%dx%d fmt:%s %s->%s]%s",
313                 link, link->w, link->h,
314                 av_pix_fmt_descriptors[link->format].name,
315                 link->src ? link->src->filter->name : "",
316                 link->dst ? link->dst->filter->name : "",
317                 end ? "\n" : "");
318     } else {
319         char buf[128];
320         av_get_channel_layout_string(buf, sizeof(buf), -1, link->channel_layout);
321
322         ff_tlog(ctx,
323                 "link[%p r:%d cl:%s fmt:%s %s->%s]%s",
324                 link, (int)link->sample_rate, buf,
325                 av_get_sample_fmt_name(link->format),
326                 link->src ? link->src->filter->name : "",
327                 link->dst ? link->dst->filter->name : "",
328                 end ? "\n" : "");
329     }
330 }
331
332 int ff_request_frame(AVFilterLink *link)
333 {
334     int ret = -1;
335     FF_TPRINTF_START(NULL, request_frame); ff_tlog_link(NULL, link, 1);
336
337     if (link->closed)
338         return AVERROR_EOF;
339     if (link->srcpad->request_frame)
340         ret = link->srcpad->request_frame(link);
341     else if (link->src->inputs[0])
342         ret = ff_request_frame(link->src->inputs[0]);
343     if (ret == AVERROR_EOF && link->partial_buf) {
344         AVFilterBufferRef *pbuf = link->partial_buf;
345         link->partial_buf = NULL;
346         ff_filter_samples_framed(link, pbuf);
347         return 0;
348     }
349     if (ret == AVERROR_EOF)
350         link->closed = 1;
351     return ret;
352 }
353
354 int ff_poll_frame(AVFilterLink *link)
355 {
356     int i, min = INT_MAX;
357
358     if (link->srcpad->poll_frame)
359         return link->srcpad->poll_frame(link);
360
361     for (i = 0; i < link->src->nb_inputs; i++) {
362         int val;
363         if (!link->src->inputs[i])
364             return -1;
365         val = ff_poll_frame(link->src->inputs[i]);
366         min = FFMIN(min, val);
367     }
368
369     return min;
370 }
371
372 void ff_update_link_current_pts(AVFilterLink *link, int64_t pts)
373 {
374     if (pts == AV_NOPTS_VALUE)
375         return;
376     link->current_pts = av_rescale_q(pts, link->time_base, AV_TIME_BASE_Q);
377     /* TODO use duration */
378     if (link->graph && link->age_index >= 0)
379         ff_avfilter_graph_update_heap(link->graph, link);
380 }
381
382 int avfilter_process_command(AVFilterContext *filter, const char *cmd, const char *arg, char *res, int res_len, int flags)
383 {
384     if(!strcmp(cmd, "ping")){
385         av_strlcatf(res, res_len, "pong from:%s %s\n", filter->filter->name, filter->name);
386         return 0;
387     }else if(filter->filter->process_command) {
388         return filter->filter->process_command(filter, cmd, arg, res, res_len, flags);
389     }
390     return AVERROR(ENOSYS);
391 }
392
393 #define MAX_REGISTERED_AVFILTERS_NB 128
394
395 static AVFilter *registered_avfilters[MAX_REGISTERED_AVFILTERS_NB + 1];
396
397 static int next_registered_avfilter_idx = 0;
398
399 AVFilter *avfilter_get_by_name(const char *name)
400 {
401     int i;
402
403     for (i = 0; registered_avfilters[i]; i++)
404         if (!strcmp(registered_avfilters[i]->name, name))
405             return registered_avfilters[i];
406
407     return NULL;
408 }
409
410 int avfilter_register(AVFilter *filter)
411 {
412     if (next_registered_avfilter_idx == MAX_REGISTERED_AVFILTERS_NB) {
413         av_log(NULL, AV_LOG_ERROR,
414                "Maximum number of registered filters %d reached, "
415                "impossible to register filter with name '%s'\n",
416                MAX_REGISTERED_AVFILTERS_NB, filter->name);
417         return AVERROR(ENOMEM);
418     }
419
420     registered_avfilters[next_registered_avfilter_idx++] = filter;
421     return 0;
422 }
423
424 AVFilter **av_filter_next(AVFilter **filter)
425 {
426     return filter ? ++filter : &registered_avfilters[0];
427 }
428
429 void avfilter_uninit(void)
430 {
431     memset(registered_avfilters, 0, sizeof(registered_avfilters));
432     next_registered_avfilter_idx = 0;
433 }
434
435 static int pad_count(const AVFilterPad *pads)
436 {
437     int count;
438
439     for(count = 0; pads->name; count ++) pads ++;
440     return count;
441 }
442
443 static const char *default_filter_name(void *filter_ctx)
444 {
445     AVFilterContext *ctx = filter_ctx;
446     return ctx->name ? ctx->name : ctx->filter->name;
447 }
448
449 static void *filter_child_next(void *obj, void *prev)
450 {
451     AVFilterContext *ctx = obj;
452     if (!prev && ctx->filter && ctx->filter->priv_class)
453         return ctx->priv;
454     return NULL;
455 }
456
457 static const AVClass *filter_child_class_next(const AVClass *prev)
458 {
459     AVFilter **filter_ptr = NULL;
460
461     /* find the filter that corresponds to prev */
462     while (prev && *(filter_ptr = av_filter_next(filter_ptr)))
463         if ((*filter_ptr)->priv_class == prev)
464             break;
465
466     /* could not find filter corresponding to prev */
467     if (prev && !(*filter_ptr))
468         return NULL;
469
470     /* find next filter with specific options */
471     while (*(filter_ptr = av_filter_next(filter_ptr)))
472         if ((*filter_ptr)->priv_class)
473             return (*filter_ptr)->priv_class;
474     return NULL;
475 }
476
477 static const AVClass avfilter_class = {
478     .class_name = "AVFilter",
479     .item_name  = default_filter_name,
480     .version    = LIBAVUTIL_VERSION_INT,
481     .category   = AV_CLASS_CATEGORY_FILTER,
482     .child_next = filter_child_next,
483     .child_class_next = filter_child_class_next,
484 };
485
486 const AVClass *avfilter_get_class(void)
487 {
488     return &avfilter_class;
489 }
490
491 int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *inst_name)
492 {
493     AVFilterContext *ret;
494     *filter_ctx = NULL;
495
496     if (!filter)
497         return AVERROR(EINVAL);
498
499     ret = av_mallocz(sizeof(AVFilterContext));
500     if (!ret)
501         return AVERROR(ENOMEM);
502
503     ret->av_class = &avfilter_class;
504     ret->filter   = filter;
505     ret->name     = inst_name ? av_strdup(inst_name) : NULL;
506     if (filter->priv_size) {
507         ret->priv     = av_mallocz(filter->priv_size);
508         if (!ret->priv)
509             goto err;
510     }
511
512     ret->nb_inputs = pad_count(filter->inputs);
513     if (ret->nb_inputs ) {
514         ret->input_pads   = av_malloc(sizeof(AVFilterPad) * ret->nb_inputs);
515         if (!ret->input_pads)
516             goto err;
517         memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad) * ret->nb_inputs);
518         ret->inputs       = av_mallocz(sizeof(AVFilterLink*) * ret->nb_inputs);
519         if (!ret->inputs)
520             goto err;
521     }
522
523     ret->nb_outputs = pad_count(filter->outputs);
524     if (ret->nb_outputs) {
525         ret->output_pads  = av_malloc(sizeof(AVFilterPad) * ret->nb_outputs);
526         if (!ret->output_pads)
527             goto err;
528         memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad) * ret->nb_outputs);
529         ret->outputs      = av_mallocz(sizeof(AVFilterLink*) * ret->nb_outputs);
530         if (!ret->outputs)
531             goto err;
532     }
533 #if FF_API_FOO_COUNT
534     ret->output_count = ret->nb_outputs;
535     ret->input_count  = ret->nb_inputs;
536 #endif
537
538     *filter_ctx = ret;
539     return 0;
540
541 err:
542     av_freep(&ret->inputs);
543     av_freep(&ret->input_pads);
544     ret->nb_inputs = 0;
545     av_freep(&ret->outputs);
546     av_freep(&ret->output_pads);
547     ret->nb_outputs = 0;
548     av_freep(&ret->priv);
549     av_free(ret);
550     return AVERROR(ENOMEM);
551 }
552
553 void avfilter_free(AVFilterContext *filter)
554 {
555     int i;
556     AVFilterLink *link;
557
558     if (!filter)
559         return;
560
561     if (filter->filter->uninit)
562         filter->filter->uninit(filter);
563
564     for (i = 0; i < filter->nb_inputs; i++) {
565         if ((link = filter->inputs[i])) {
566             if (link->src)
567                 link->src->outputs[link->srcpad - link->src->output_pads] = NULL;
568             ff_formats_unref(&link->in_formats);
569             ff_formats_unref(&link->out_formats);
570             ff_formats_unref(&link->in_samplerates);
571             ff_formats_unref(&link->out_samplerates);
572             ff_channel_layouts_unref(&link->in_channel_layouts);
573             ff_channel_layouts_unref(&link->out_channel_layouts);
574         }
575         avfilter_link_free(&link);
576     }
577     for (i = 0; i < filter->nb_outputs; i++) {
578         if ((link = filter->outputs[i])) {
579             if (link->dst)
580                 link->dst->inputs[link->dstpad - link->dst->input_pads] = NULL;
581             ff_formats_unref(&link->in_formats);
582             ff_formats_unref(&link->out_formats);
583             ff_formats_unref(&link->in_samplerates);
584             ff_formats_unref(&link->out_samplerates);
585             ff_channel_layouts_unref(&link->in_channel_layouts);
586             ff_channel_layouts_unref(&link->out_channel_layouts);
587         }
588         avfilter_link_free(&link);
589     }
590
591     av_freep(&filter->name);
592     av_freep(&filter->input_pads);
593     av_freep(&filter->output_pads);
594     av_freep(&filter->inputs);
595     av_freep(&filter->outputs);
596     av_freep(&filter->priv);
597     while(filter->command_queue){
598         ff_command_queue_pop(filter);
599     }
600     av_free(filter);
601 }
602
603 int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
604 {
605     int ret=0;
606
607     if (filter->filter->init_opaque)
608         ret = filter->filter->init_opaque(filter, args, opaque);
609     else if (filter->filter->init)
610         ret = filter->filter->init(filter, args);
611     return ret;
612 }
613
614 const char *avfilter_pad_get_name(AVFilterPad *pads, int pad_idx)
615 {
616     return pads[pad_idx].name;
617 }
618
619 enum AVMediaType avfilter_pad_get_type(AVFilterPad *pads, int pad_idx)
620 {
621     return pads[pad_idx].type;
622 }