]> git.sesse.net Git - ffmpeg/blob - libavfilter/avfilter.c
Extend ff_dprintf_picref() to make it print video interlaced and
[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 "libavcodec/audioconvert.c"
25 #include "libavutil/pixdesc.h"
26 #include "libavcore/imgutils.h"
27 #include "avfilter.h"
28 #include "internal.h"
29
30 unsigned avfilter_version(void) {
31     return LIBAVFILTER_VERSION_INT;
32 }
33
34 const char *avfilter_configuration(void)
35 {
36     return FFMPEG_CONFIGURATION;
37 }
38
39 const char *avfilter_license(void)
40 {
41 #define LICENSE_PREFIX "libavfilter license: "
42     return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
43 }
44
45 /** helper macros to get the in/out pad on the dst/src filter */
46 #define link_dpad(link)     link->dst-> input_pads[link->dstpad]
47 #define link_spad(link)     link->src->output_pads[link->srcpad]
48
49 AVFilterBufferRef *avfilter_ref_buffer(AVFilterBufferRef *ref, int pmask)
50 {
51     AVFilterBufferRef *ret = av_malloc(sizeof(AVFilterBufferRef));
52     if (!ret)
53         return NULL;
54     *ret = *ref;
55     if (ref->type == AVMEDIA_TYPE_VIDEO) {
56         ret->video = av_malloc(sizeof(AVFilterBufferRefVideoProps));
57         if (!ret->video) {
58             av_free(ret);
59             return NULL;
60         }
61         *ret->video = *ref->video;
62     } else if (ref->type == AVMEDIA_TYPE_AUDIO) {
63         ret->audio = av_malloc(sizeof(AVFilterBufferRefAudioProps));
64         if (!ret->audio) {
65             av_free(ret);
66             return NULL;
67         }
68         *ret->audio = *ref->audio;
69     }
70     ret->perms &= pmask;
71     ret->buf->refcount ++;
72     return ret;
73 }
74
75 void avfilter_unref_buffer(AVFilterBufferRef *ref)
76 {
77     if (!(--ref->buf->refcount))
78         ref->buf->free(ref->buf);
79     av_free(ref->video);
80     av_free(ref->audio);
81     av_free(ref);
82 }
83
84 void avfilter_insert_pad(unsigned idx, unsigned *count, size_t padidx_off,
85                          AVFilterPad **pads, AVFilterLink ***links,
86                          AVFilterPad *newpad)
87 {
88     unsigned i;
89
90     idx = FFMIN(idx, *count);
91
92     *pads  = av_realloc(*pads,  sizeof(AVFilterPad)   * (*count + 1));
93     *links = av_realloc(*links, sizeof(AVFilterLink*) * (*count + 1));
94     memmove(*pads +idx+1, *pads +idx, sizeof(AVFilterPad)   * (*count-idx));
95     memmove(*links+idx+1, *links+idx, sizeof(AVFilterLink*) * (*count-idx));
96     memcpy(*pads+idx, newpad, sizeof(AVFilterPad));
97     (*links)[idx] = NULL;
98
99     (*count)++;
100     for (i = idx+1; i < *count; i++)
101         if (*links[i])
102             (*(unsigned *)((uint8_t *) *links[i] + padidx_off))++;
103 }
104
105 int avfilter_link(AVFilterContext *src, unsigned srcpad,
106                   AVFilterContext *dst, unsigned dstpad)
107 {
108     AVFilterLink *link;
109
110     if (src->output_count <= srcpad || dst->input_count <= dstpad ||
111         src->outputs[srcpad]        || dst->inputs[dstpad])
112         return -1;
113
114     src->outputs[srcpad] =
115     dst-> inputs[dstpad] = link = av_mallocz(sizeof(AVFilterLink));
116
117     link->src     = src;
118     link->dst     = dst;
119     link->srcpad  = srcpad;
120     link->dstpad  = dstpad;
121     link->type    = src->output_pads[srcpad].type;
122     assert(PIX_FMT_NONE == -1 && SAMPLE_FMT_NONE == -1);
123     link->format  = -1;
124
125     return 0;
126 }
127
128 int avfilter_insert_filter(AVFilterLink *link, AVFilterContext *filt,
129                            unsigned in, unsigned out)
130 {
131     av_log(link->dst, AV_LOG_INFO, "auto-inserting filter '%s' "
132            "between the filter '%s' and the filter '%s'\n",
133            filt->name, link->src->name, link->dst->name);
134
135     link->dst->inputs[link->dstpad] = NULL;
136     if (avfilter_link(filt, out, link->dst, link->dstpad)) {
137         /* failed to link output filter to new filter */
138         link->dst->inputs[link->dstpad] = link;
139         return -1;
140     }
141
142     /* re-hookup the link to the new destination filter we inserted */
143     link->dst = filt;
144     link->dstpad = in;
145     filt->inputs[in] = link;
146
147     /* if any information on supported media formats already exists on the
148      * link, we need to preserve that */
149     if (link->out_formats)
150         avfilter_formats_changeref(&link->out_formats,
151                                    &filt->outputs[out]->out_formats);
152
153     return 0;
154 }
155
156 int avfilter_config_links(AVFilterContext *filter)
157 {
158     int (*config_link)(AVFilterLink *);
159     unsigned i;
160
161     for (i = 0; i < filter->input_count; i ++) {
162         AVFilterLink *link = filter->inputs[i];
163
164         if (!link) continue;
165
166         switch (link->init_state) {
167         case AVLINK_INIT:
168             continue;
169         case AVLINK_STARTINIT:
170             av_log(filter, AV_LOG_INFO, "circular filter chain detected\n");
171             return 0;
172         case AVLINK_UNINIT:
173             link->init_state = AVLINK_STARTINIT;
174
175             if (avfilter_config_links(link->src))
176                 return -1;
177
178             if (!(config_link = link_spad(link).config_props))
179                 config_link  = avfilter_default_config_output_link;
180             if (config_link(link))
181                 return -1;
182
183             if ((config_link = link_dpad(link).config_props))
184                 if (config_link(link))
185                     return -1;
186
187             link->init_state = AVLINK_INIT;
188         }
189     }
190
191     return 0;
192 }
193
194 void ff_dprintf_picref(void *ctx, AVFilterBufferRef *picref, int end)
195 {
196     dprintf(ctx,
197             "picref[%p data:%p linesize[%d, %d, %d, %d] pts:%"PRId64" pos:%"PRId64,
198             picref,
199             picref->data[0],
200             picref->linesize[0], picref->linesize[1], picref->linesize[2], picref->linesize[3],
201             picref->pts, picref->pos);
202
203     if (picref->video) {
204         dprintf(ctx, " a:%d/%d s:%dx%d i:%c",
205                 picref->video->pixel_aspect.num, picref->video->pixel_aspect.den,
206                 picref->video->w, picref->video->h,
207                 !picref->video->interlaced     ? 'P' :         /* Progressive  */
208                 picref->video->top_field_first ? 'T' : 'B');   /* Top / Bottom */
209     }
210     dprintf(ctx, "]%s", end ? "\n" : "");
211 }
212
213 void ff_dprintf_link(void *ctx, AVFilterLink *link, int end)
214 {
215     dprintf(ctx,
216             "link[%p s:%dx%d fmt:%-16s %-16s->%-16s]%s",
217             link, link->w, link->h,
218             av_pix_fmt_descriptors[link->format].name,
219             link->src ? link->src->filter->name : "",
220             link->dst ? link->dst->filter->name : "",
221             end ? "\n" : "");
222 }
223
224 AVFilterBufferRef *avfilter_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
225 {
226     AVFilterBufferRef *ret = NULL;
227
228     FF_DPRINTF_START(NULL, get_video_buffer); ff_dprintf_link(NULL, link, 0); dprintf(NULL, " perms:%d w:%d h:%d\n", perms, w, h);
229
230     if (link_dpad(link).get_video_buffer)
231         ret = link_dpad(link).get_video_buffer(link, perms, w, h);
232
233     if (!ret)
234         ret = avfilter_default_get_video_buffer(link, perms, w, h);
235
236     if (ret)
237         ret->type = AVMEDIA_TYPE_VIDEO;
238
239     FF_DPRINTF_START(NULL, get_video_buffer); ff_dprintf_link(NULL, link, 0); dprintf(NULL, " returning "); ff_dprintf_picref(NULL, ret, 1);
240
241     return ret;
242 }
243
244 AVFilterBufferRef *avfilter_get_audio_buffer(AVFilterLink *link, int perms,
245                                              enum SampleFormat sample_fmt, int size,
246                                              int64_t channel_layout, int planar)
247 {
248     AVFilterBufferRef *ret = NULL;
249
250     if (link_dpad(link).get_audio_buffer)
251         ret = link_dpad(link).get_audio_buffer(link, perms, sample_fmt, size, channel_layout, planar);
252
253     if (!ret)
254         ret = avfilter_default_get_audio_buffer(link, perms, sample_fmt, size, channel_layout, planar);
255
256     if (ret)
257         ret->type = AVMEDIA_TYPE_AUDIO;
258
259     return ret;
260 }
261
262 int avfilter_request_frame(AVFilterLink *link)
263 {
264     FF_DPRINTF_START(NULL, request_frame); ff_dprintf_link(NULL, link, 1);
265
266     if (link_spad(link).request_frame)
267         return link_spad(link).request_frame(link);
268     else if (link->src->inputs[0])
269         return avfilter_request_frame(link->src->inputs[0]);
270     else return -1;
271 }
272
273 int avfilter_poll_frame(AVFilterLink *link)
274 {
275     int i, min = INT_MAX;
276
277     if (link_spad(link).poll_frame)
278         return link_spad(link).poll_frame(link);
279
280     for (i = 0; i < link->src->input_count; i++) {
281         int val;
282         if (!link->src->inputs[i])
283             return -1;
284         val = avfilter_poll_frame(link->src->inputs[i]);
285         min = FFMIN(min, val);
286     }
287
288     return min;
289 }
290
291 /* XXX: should we do the duplicating of the picture ref here, instead of
292  * forcing the source filter to do it? */
293 void avfilter_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
294 {
295     void (*start_frame)(AVFilterLink *, AVFilterBufferRef *);
296     AVFilterPad *dst = &link_dpad(link);
297
298     FF_DPRINTF_START(NULL, start_frame); ff_dprintf_link(NULL, link, 0); dprintf(NULL, " "); ff_dprintf_picref(NULL, picref, 1);
299
300     if (!(start_frame = dst->start_frame))
301         start_frame = avfilter_default_start_frame;
302
303     /* prepare to copy the picture if it has insufficient permissions */
304     if ((dst->min_perms & picref->perms) != dst->min_perms ||
305          dst->rej_perms & picref->perms) {
306         av_log(link->dst, AV_LOG_DEBUG,
307                 "frame copy needed (have perms %x, need %x, reject %x)\n",
308                 picref->perms,
309                 link_dpad(link).min_perms, link_dpad(link).rej_perms);
310
311         link->cur_buf = avfilter_default_get_video_buffer(link, dst->min_perms, link->w, link->h);
312         link->src_buf = picref;
313         avfilter_copy_buffer_ref_props(link->cur_buf, link->src_buf);
314     }
315     else
316         link->cur_buf = picref;
317
318     start_frame(link, link->cur_buf);
319 }
320
321 void avfilter_end_frame(AVFilterLink *link)
322 {
323     void (*end_frame)(AVFilterLink *);
324
325     if (!(end_frame = link_dpad(link).end_frame))
326         end_frame = avfilter_default_end_frame;
327
328     end_frame(link);
329
330     /* unreference the source picture if we're feeding the destination filter
331      * a copied version dues to permission issues */
332     if (link->src_buf) {
333         avfilter_unref_buffer(link->src_buf);
334         link->src_buf = NULL;
335     }
336 }
337
338 void avfilter_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
339 {
340     uint8_t *src[4], *dst[4];
341     int i, j, vsub;
342     void (*draw_slice)(AVFilterLink *, int, int, int);
343
344     FF_DPRINTF_START(NULL, draw_slice); ff_dprintf_link(NULL, link, 0); dprintf(NULL, " y:%d h:%d dir:%d\n", y, h, slice_dir);
345
346     /* copy the slice if needed for permission reasons */
347     if (link->src_buf) {
348         vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h;
349
350         for (i = 0; i < 4; i++) {
351             if (link->src_buf->data[i]) {
352                 src[i] = link->src_buf-> data[i] +
353                     (y >> (i==0 ? 0 : vsub)) * link->src_buf-> linesize[i];
354                 dst[i] = link->cur_buf->data[i] +
355                     (y >> (i==0 ? 0 : vsub)) * link->cur_buf->linesize[i];
356             } else
357                 src[i] = dst[i] = NULL;
358         }
359
360         for (i = 0; i < 4; i++) {
361             int planew =
362                 av_get_image_linesize(link->format, link->cur_buf->video->w, i);
363
364             if (!src[i]) continue;
365
366             for (j = 0; j < h >> (i==0 ? 0 : vsub); j++) {
367                 memcpy(dst[i], src[i], planew);
368                 src[i] += link->src_buf->linesize[i];
369                 dst[i] += link->cur_buf->linesize[i];
370             }
371         }
372     }
373
374     if (!(draw_slice = link_dpad(link).draw_slice))
375         draw_slice = avfilter_default_draw_slice;
376     draw_slice(link, y, h, slice_dir);
377 }
378
379 void avfilter_filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
380 {
381     void (*filter_samples)(AVFilterLink *, AVFilterBufferRef *);
382     AVFilterPad *dst = &link_dpad(link);
383
384     if (!(filter_samples = dst->filter_samples))
385         filter_samples = avfilter_default_filter_samples;
386
387     /* prepare to copy the samples if the buffer has insufficient permissions */
388     if ((dst->min_perms & samplesref->perms) != dst->min_perms ||
389         dst->rej_perms & samplesref->perms) {
390
391         av_log(link->dst, AV_LOG_DEBUG,
392                "Copying audio data in avfilter (have perms %x, need %x, reject %x)\n",
393                samplesref->perms, link_dpad(link).min_perms, link_dpad(link).rej_perms);
394
395         link->cur_buf = avfilter_default_get_audio_buffer(link, dst->min_perms,
396                                                           samplesref->format,
397                                                           samplesref->audio->size,
398                                                           samplesref->audio->channel_layout,
399                                                           samplesref->audio->planar);
400         link->cur_buf->pts                = samplesref->pts;
401         link->cur_buf->audio->sample_rate = samplesref->audio->sample_rate;
402
403         /* Copy actual data into new samples buffer */
404         memcpy(link->cur_buf->data[0], samplesref->data[0], samplesref->audio->size);
405
406         avfilter_unref_buffer(samplesref);
407     } else
408         link->cur_buf = samplesref;
409
410     filter_samples(link, link->cur_buf);
411 }
412
413 #define MAX_REGISTERED_AVFILTERS_NB 64
414
415 static AVFilter *registered_avfilters[MAX_REGISTERED_AVFILTERS_NB + 1];
416
417 static int next_registered_avfilter_idx = 0;
418
419 AVFilter *avfilter_get_by_name(const char *name)
420 {
421     int i;
422
423     for (i = 0; registered_avfilters[i]; i++)
424         if (!strcmp(registered_avfilters[i]->name, name))
425             return registered_avfilters[i];
426
427     return NULL;
428 }
429
430 int avfilter_register(AVFilter *filter)
431 {
432     if (next_registered_avfilter_idx == MAX_REGISTERED_AVFILTERS_NB)
433         return -1;
434
435     registered_avfilters[next_registered_avfilter_idx++] = filter;
436     return 0;
437 }
438
439 AVFilter **av_filter_next(AVFilter **filter)
440 {
441     return filter ? ++filter : &registered_avfilters[0];
442 }
443
444 void avfilter_uninit(void)
445 {
446     memset(registered_avfilters, 0, sizeof(registered_avfilters));
447     next_registered_avfilter_idx = 0;
448 }
449
450 static int pad_count(const AVFilterPad *pads)
451 {
452     int count;
453
454     for(count = 0; pads->name; count ++) pads ++;
455     return count;
456 }
457
458 static const char *filter_name(void *p)
459 {
460     AVFilterContext *filter = p;
461     return filter->filter->name;
462 }
463
464 static const AVClass avfilter_class = {
465     "AVFilter",
466     filter_name,
467     NULL,
468     LIBAVUTIL_VERSION_INT,
469 };
470
471 int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *inst_name)
472 {
473     AVFilterContext *ret;
474     *filter_ctx = NULL;
475
476     if (!filter)
477         return AVERROR(EINVAL);
478
479     ret = av_mallocz(sizeof(AVFilterContext));
480
481     ret->av_class = &avfilter_class;
482     ret->filter   = filter;
483     ret->name     = inst_name ? av_strdup(inst_name) : NULL;
484     ret->priv     = av_mallocz(filter->priv_size);
485
486     ret->input_count  = pad_count(filter->inputs);
487     if (ret->input_count) {
488         ret->input_pads   = av_malloc(sizeof(AVFilterPad) * ret->input_count);
489         memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad) * ret->input_count);
490         ret->inputs       = av_mallocz(sizeof(AVFilterLink*) * ret->input_count);
491     }
492
493     ret->output_count = pad_count(filter->outputs);
494     if (ret->output_count) {
495         ret->output_pads  = av_malloc(sizeof(AVFilterPad) * ret->output_count);
496         memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad) * ret->output_count);
497         ret->outputs      = av_mallocz(sizeof(AVFilterLink*) * ret->output_count);
498     }
499
500     *filter_ctx = ret;
501     return 0;
502 }
503
504 void avfilter_destroy(AVFilterContext *filter)
505 {
506     int i;
507
508     if (filter->filter->uninit)
509         filter->filter->uninit(filter);
510
511     for (i = 0; i < filter->input_count; i++) {
512         if (filter->inputs[i]) {
513             if (filter->inputs[i]->src)
514                 filter->inputs[i]->src->outputs[filter->inputs[i]->srcpad] = NULL;
515             avfilter_formats_unref(&filter->inputs[i]->in_formats);
516             avfilter_formats_unref(&filter->inputs[i]->out_formats);
517         }
518         av_freep(&filter->inputs[i]);
519     }
520     for (i = 0; i < filter->output_count; i++) {
521         if (filter->outputs[i]) {
522             if (filter->outputs[i]->dst)
523                 filter->outputs[i]->dst->inputs[filter->outputs[i]->dstpad] = NULL;
524             avfilter_formats_unref(&filter->outputs[i]->in_formats);
525             avfilter_formats_unref(&filter->outputs[i]->out_formats);
526         }
527         av_freep(&filter->outputs[i]);
528     }
529
530     av_freep(&filter->name);
531     av_freep(&filter->input_pads);
532     av_freep(&filter->output_pads);
533     av_freep(&filter->inputs);
534     av_freep(&filter->outputs);
535     av_freep(&filter->priv);
536     av_free(filter);
537 }
538
539 int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
540 {
541     int ret=0;
542
543     if (filter->filter->init)
544         ret = filter->filter->init(filter, args, opaque);
545     return ret;
546 }
547