]> git.sesse.net Git - ffmpeg/blob - libavfilter/avfilter.c
x86: Fix assembly with NASM
[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(AV_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_get_pix_fmt_name(link->format),
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     if (!pads)
440         return 0;
441
442     for(count = 0; pads->name; count ++) pads ++;
443     return count;
444 }
445
446 static const char *default_filter_name(void *filter_ctx)
447 {
448     AVFilterContext *ctx = filter_ctx;
449     return ctx->name ? ctx->name : ctx->filter->name;
450 }
451
452 static void *filter_child_next(void *obj, void *prev)
453 {
454     AVFilterContext *ctx = obj;
455     if (!prev && ctx->filter && ctx->filter->priv_class)
456         return ctx->priv;
457     return NULL;
458 }
459
460 static const AVClass *filter_child_class_next(const AVClass *prev)
461 {
462     AVFilter **filter_ptr = NULL;
463
464     /* find the filter that corresponds to prev */
465     while (prev && *(filter_ptr = av_filter_next(filter_ptr)))
466         if ((*filter_ptr)->priv_class == prev)
467             break;
468
469     /* could not find filter corresponding to prev */
470     if (prev && !(*filter_ptr))
471         return NULL;
472
473     /* find next filter with specific options */
474     while (*(filter_ptr = av_filter_next(filter_ptr)))
475         if ((*filter_ptr)->priv_class)
476             return (*filter_ptr)->priv_class;
477     return NULL;
478 }
479
480 static const AVClass avfilter_class = {
481     .class_name = "AVFilter",
482     .item_name  = default_filter_name,
483     .version    = LIBAVUTIL_VERSION_INT,
484     .category   = AV_CLASS_CATEGORY_FILTER,
485     .child_next = filter_child_next,
486     .child_class_next = filter_child_class_next,
487 };
488
489 const AVClass *avfilter_get_class(void)
490 {
491     return &avfilter_class;
492 }
493
494 int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *inst_name)
495 {
496     AVFilterContext *ret;
497     *filter_ctx = NULL;
498
499     if (!filter)
500         return AVERROR(EINVAL);
501
502     ret = av_mallocz(sizeof(AVFilterContext));
503     if (!ret)
504         return AVERROR(ENOMEM);
505
506     ret->av_class = &avfilter_class;
507     ret->filter   = filter;
508     ret->name     = inst_name ? av_strdup(inst_name) : NULL;
509     if (filter->priv_size) {
510         ret->priv     = av_mallocz(filter->priv_size);
511         if (!ret->priv)
512             goto err;
513     }
514
515     ret->nb_inputs = pad_count(filter->inputs);
516     if (ret->nb_inputs ) {
517         ret->input_pads   = av_malloc(sizeof(AVFilterPad) * ret->nb_inputs);
518         if (!ret->input_pads)
519             goto err;
520         memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad) * ret->nb_inputs);
521         ret->inputs       = av_mallocz(sizeof(AVFilterLink*) * ret->nb_inputs);
522         if (!ret->inputs)
523             goto err;
524     }
525
526     ret->nb_outputs = pad_count(filter->outputs);
527     if (ret->nb_outputs) {
528         ret->output_pads  = av_malloc(sizeof(AVFilterPad) * ret->nb_outputs);
529         if (!ret->output_pads)
530             goto err;
531         memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad) * ret->nb_outputs);
532         ret->outputs      = av_mallocz(sizeof(AVFilterLink*) * ret->nb_outputs);
533         if (!ret->outputs)
534             goto err;
535     }
536 #if FF_API_FOO_COUNT
537     ret->output_count = ret->nb_outputs;
538     ret->input_count  = ret->nb_inputs;
539 #endif
540
541     *filter_ctx = ret;
542     return 0;
543
544 err:
545     av_freep(&ret->inputs);
546     av_freep(&ret->input_pads);
547     ret->nb_inputs = 0;
548     av_freep(&ret->outputs);
549     av_freep(&ret->output_pads);
550     ret->nb_outputs = 0;
551     av_freep(&ret->priv);
552     av_free(ret);
553     return AVERROR(ENOMEM);
554 }
555
556 void avfilter_free(AVFilterContext *filter)
557 {
558     int i;
559     AVFilterLink *link;
560
561     if (!filter)
562         return;
563
564     if (filter->filter->uninit)
565         filter->filter->uninit(filter);
566
567     for (i = 0; i < filter->nb_inputs; i++) {
568         if ((link = filter->inputs[i])) {
569             if (link->src)
570                 link->src->outputs[link->srcpad - link->src->output_pads] = NULL;
571             ff_formats_unref(&link->in_formats);
572             ff_formats_unref(&link->out_formats);
573             ff_formats_unref(&link->in_samplerates);
574             ff_formats_unref(&link->out_samplerates);
575             ff_channel_layouts_unref(&link->in_channel_layouts);
576             ff_channel_layouts_unref(&link->out_channel_layouts);
577         }
578         avfilter_link_free(&link);
579     }
580     for (i = 0; i < filter->nb_outputs; i++) {
581         if ((link = filter->outputs[i])) {
582             if (link->dst)
583                 link->dst->inputs[link->dstpad - link->dst->input_pads] = NULL;
584             ff_formats_unref(&link->in_formats);
585             ff_formats_unref(&link->out_formats);
586             ff_formats_unref(&link->in_samplerates);
587             ff_formats_unref(&link->out_samplerates);
588             ff_channel_layouts_unref(&link->in_channel_layouts);
589             ff_channel_layouts_unref(&link->out_channel_layouts);
590         }
591         avfilter_link_free(&link);
592     }
593
594     av_freep(&filter->name);
595     av_freep(&filter->input_pads);
596     av_freep(&filter->output_pads);
597     av_freep(&filter->inputs);
598     av_freep(&filter->outputs);
599     av_freep(&filter->priv);
600     while(filter->command_queue){
601         ff_command_queue_pop(filter);
602     }
603     av_free(filter);
604 }
605
606 int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
607 {
608     int ret=0;
609
610     if (filter->filter->init_opaque)
611         ret = filter->filter->init_opaque(filter, args, opaque);
612     else if (filter->filter->init)
613         ret = filter->filter->init(filter, args);
614     return ret;
615 }
616
617 const char *avfilter_pad_get_name(AVFilterPad *pads, int pad_idx)
618 {
619     return pads[pad_idx].name;
620 }
621
622 enum AVMediaType avfilter_pad_get_type(AVFilterPad *pads, int pad_idx)
623 {
624     return pads[pad_idx].type;
625 }