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