3 * Copyright (c) 2007 Bobby Bingham
5 * This file is part of FFmpeg.
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.
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.
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
24 #include "libavutil/pixdesc.h"
25 #include "libavutil/rational.h"
26 #include "libavutil/audioconvert.h"
27 #include "libavutil/imgutils.h"
28 #include "libavutil/avassert.h"
32 unsigned avfilter_version(void) {
33 return LIBAVFILTER_VERSION_INT;
36 const char *avfilter_configuration(void)
38 return FFMPEG_CONFIGURATION;
41 const char *avfilter_license(void)
43 #define LICENSE_PREFIX "libavfilter license: "
44 return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
47 AVFilterBufferRef *avfilter_ref_buffer(AVFilterBufferRef *ref, int pmask)
49 AVFilterBufferRef *ret = av_malloc(sizeof(AVFilterBufferRef));
53 if (ref->type == AVMEDIA_TYPE_VIDEO) {
54 ret->video = av_malloc(sizeof(AVFilterBufferRefVideoProps));
59 *ret->video = *ref->video;
60 } else if (ref->type == AVMEDIA_TYPE_AUDIO) {
61 ret->audio = av_malloc(sizeof(AVFilterBufferRefAudioProps));
66 *ret->audio = *ref->audio;
69 ret->buf->refcount ++;
73 static void store_in_pool(AVFilterBufferRef *ref)
76 AVFilterPool *pool= ref->buf->priv;
78 av_assert0(ref->buf->data[0]);
80 if (pool->count == POOL_SIZE) {
81 AVFilterBufferRef *ref1 = pool->pic[0];
82 av_freep(&ref1->video);
83 av_freep(&ref1->audio);
84 av_freep(&ref1->buf->data[0]);
87 memmove(&pool->pic[0], &pool->pic[1], sizeof(void*)*(POOL_SIZE-1));
89 pool->pic[POOL_SIZE-1] = NULL;
92 for (i = 0; i < POOL_SIZE; i++) {
101 void avfilter_unref_buffer(AVFilterBufferRef *ref)
105 if (!(--ref->buf->refcount)) {
106 if (!ref->buf->free) {
110 ref->buf->free(ref->buf);
112 av_freep(&ref->video);
113 av_freep(&ref->audio);
117 void avfilter_insert_pad(unsigned idx, unsigned *count, size_t padidx_off,
118 AVFilterPad **pads, AVFilterLink ***links,
123 idx = FFMIN(idx, *count);
125 *pads = av_realloc(*pads, sizeof(AVFilterPad) * (*count + 1));
126 *links = av_realloc(*links, sizeof(AVFilterLink*) * (*count + 1));
127 memmove(*pads +idx+1, *pads +idx, sizeof(AVFilterPad) * (*count-idx));
128 memmove(*links+idx+1, *links+idx, sizeof(AVFilterLink*) * (*count-idx));
129 memcpy(*pads+idx, newpad, sizeof(AVFilterPad));
130 (*links)[idx] = NULL;
133 for (i = idx+1; i < *count; i++)
135 (*(unsigned *)((uint8_t *) *links[i] + padidx_off))++;
138 int avfilter_link(AVFilterContext *src, unsigned srcpad,
139 AVFilterContext *dst, unsigned dstpad)
143 if (src->output_count <= srcpad || dst->input_count <= dstpad ||
144 src->outputs[srcpad] || dst->inputs[dstpad])
147 if (src->output_pads[srcpad].type != dst->input_pads[dstpad].type) {
148 av_log(src, AV_LOG_ERROR,
149 "Media type mismatch between the '%s' filter output pad %d and the '%s' filter input pad %d\n",
150 src->name, srcpad, dst->name, dstpad);
151 return AVERROR(EINVAL);
154 src->outputs[srcpad] =
155 dst-> inputs[dstpad] = link = av_mallocz(sizeof(AVFilterLink));
159 link->srcpad = &src->output_pads[srcpad];
160 link->dstpad = &dst->input_pads[dstpad];
161 link->type = src->output_pads[srcpad].type;
162 assert(PIX_FMT_NONE == -1 && AV_SAMPLE_FMT_NONE == -1);
168 void avfilter_link_free(AVFilterLink **link)
175 for (i = 0; i < POOL_SIZE; i++) {
176 if ((*link)->pool->pic[i]) {
177 AVFilterBufferRef *picref = (*link)->pool->pic[i];
178 /* free buffer: picrefs stored in the pool are not
179 * supposed to contain a free callback */
180 av_freep(&picref->buf->data[0]);
181 av_freep(&picref->buf);
183 av_freep(&picref->audio);
184 av_freep(&picref->video);
185 av_freep(&(*link)->pool->pic[i]);
188 (*link)->pool->count = 0;
189 // av_freep(&(*link)->pool);
194 int avfilter_insert_filter(AVFilterLink *link, AVFilterContext *filt,
195 unsigned filt_srcpad_idx, unsigned filt_dstpad_idx)
198 unsigned dstpad_idx = link->dstpad - link->dst->input_pads;
200 av_log(link->dst, AV_LOG_INFO, "auto-inserting filter '%s' "
201 "between the filter '%s' and the filter '%s'\n",
202 filt->name, link->src->name, link->dst->name);
204 link->dst->inputs[dstpad_idx] = NULL;
205 if ((ret = avfilter_link(filt, filt_dstpad_idx, link->dst, dstpad_idx)) < 0) {
206 /* failed to link output filter to new filter */
207 link->dst->inputs[dstpad_idx] = link;
211 /* re-hookup the link to the new destination filter we inserted */
213 link->dstpad = &filt->input_pads[filt_srcpad_idx];
214 filt->inputs[filt_srcpad_idx] = link;
216 /* if any information on supported media formats already exists on the
217 * link, we need to preserve that */
218 if (link->out_formats)
219 avfilter_formats_changeref(&link->out_formats,
220 &filt->outputs[filt_dstpad_idx]->out_formats);
221 if (link->out_chlayouts)
222 avfilter_formats_changeref(&link->out_chlayouts,
223 &filt->outputs[filt_dstpad_idx]->out_chlayouts);
228 int avfilter_config_links(AVFilterContext *filter)
230 int (*config_link)(AVFilterLink *);
234 for (i = 0; i < filter->input_count; i ++) {
235 AVFilterLink *link = filter->inputs[i];
239 switch (link->init_state) {
242 case AVLINK_STARTINIT:
243 av_log(filter, AV_LOG_INFO, "circular filter chain detected\n");
246 link->init_state = AVLINK_STARTINIT;
248 if ((ret = avfilter_config_links(link->src)) < 0)
251 if (!(config_link = link->srcpad->config_props))
252 config_link = avfilter_default_config_output_link;
253 if ((ret = config_link(link)) < 0)
256 if (link->time_base.num == 0 && link->time_base.den == 0)
257 link->time_base = link->src && link->src->input_count ?
258 link->src->inputs[0]->time_base : AV_TIME_BASE_Q;
260 if (link->sample_aspect_ratio.num == 0 && link->sample_aspect_ratio.den == 0)
261 link->sample_aspect_ratio = link->src->input_count ?
262 link->src->inputs[0]->sample_aspect_ratio : (AVRational){1,1};
264 if (link->sample_rate == 0 && link->src && link->src->input_count)
265 link->sample_rate = link->src->inputs[0]->sample_rate;
267 if (link->channel_layout == 0 && link->src && link->src->input_count)
268 link->channel_layout = link->src->inputs[0]->channel_layout;
270 if ((config_link = link->dstpad->config_props))
271 if ((ret = config_link(link)) < 0)
274 link->init_state = AVLINK_INIT;
281 static char *ff_get_ref_perms_string(char *buf, size_t buf_size, int perms)
283 snprintf(buf, buf_size, "%s%s%s%s%s%s",
284 perms & AV_PERM_READ ? "r" : "",
285 perms & AV_PERM_WRITE ? "w" : "",
286 perms & AV_PERM_PRESERVE ? "p" : "",
287 perms & AV_PERM_REUSE ? "u" : "",
288 perms & AV_PERM_REUSE2 ? "U" : "",
289 perms & AV_PERM_NEG_LINESIZES ? "n" : "");
293 static void ff_dlog_ref(void *ctx, AVFilterBufferRef *ref, int end)
295 av_unused char buf[16];
297 "ref[%p buf:%p refcount:%d perms:%s data:%p linesize[%d, %d, %d, %d] pts:%"PRId64" pos:%"PRId64,
298 ref, ref->buf, ref->buf->refcount, ff_get_ref_perms_string(buf, sizeof(buf), ref->perms), ref->data[0],
299 ref->linesize[0], ref->linesize[1], ref->linesize[2], ref->linesize[3],
303 av_dlog(ctx, " a:%d/%d s:%dx%d i:%c iskey:%d type:%c",
304 ref->video->sample_aspect_ratio.num, ref->video->sample_aspect_ratio.den,
305 ref->video->w, ref->video->h,
306 !ref->video->interlaced ? 'P' : /* Progressive */
307 ref->video->top_field_first ? 'T' : 'B', /* Top / Bottom */
308 ref->video->key_frame,
309 av_get_picture_type_char(ref->video->pict_type));
312 av_dlog(ctx, " cl:%"PRId64"d n:%d r:%d p:%d",
313 ref->audio->channel_layout,
314 ref->audio->nb_samples,
315 ref->audio->sample_rate,
319 av_dlog(ctx, "]%s", end ? "\n" : "");
322 static void ff_dlog_link(void *ctx, AVFilterLink *link, int end)
324 if (link->type == AVMEDIA_TYPE_VIDEO) {
326 "link[%p s:%dx%d fmt:%-16s %-16s->%-16s]%s",
327 link, link->w, link->h,
328 av_pix_fmt_descriptors[link->format].name,
329 link->src ? link->src->filter->name : "",
330 link->dst ? link->dst->filter->name : "",
334 av_get_channel_layout_string(buf, sizeof(buf), -1, link->channel_layout);
337 "link[%p r:%"PRId64" cl:%s fmt:%-16s %-16s->%-16s]%s",
338 link, link->sample_rate, buf,
339 av_get_sample_fmt_name(link->format),
340 link->src ? link->src->filter->name : "",
341 link->dst ? link->dst->filter->name : "",
346 #define FF_DPRINTF_START(ctx, func) av_dlog(NULL, "%-16s: ", #func)
348 AVFilterBufferRef *avfilter_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
350 AVFilterBufferRef *ret = NULL;
352 av_unused char buf[16];
353 FF_DPRINTF_START(NULL, get_video_buffer); ff_dlog_link(NULL, link, 0);
354 av_dlog(NULL, " perms:%s w:%d h:%d\n", ff_get_ref_perms_string(buf, sizeof(buf), perms), w, h);
356 if (link->dstpad->get_video_buffer)
357 ret = link->dstpad->get_video_buffer(link, perms, w, h);
360 ret = avfilter_default_get_video_buffer(link, perms, w, h);
363 ret->type = AVMEDIA_TYPE_VIDEO;
365 FF_DPRINTF_START(NULL, get_video_buffer); ff_dlog_link(NULL, link, 0); av_dlog(NULL, " returning "); ff_dlog_ref(NULL, ret, 1);
371 avfilter_get_video_buffer_ref_from_arrays(uint8_t * const data[4], const int linesize[4], int perms,
372 int w, int h, enum PixelFormat format)
374 AVFilterBuffer *pic = av_mallocz(sizeof(AVFilterBuffer));
375 AVFilterBufferRef *picref = av_mallocz(sizeof(AVFilterBufferRef));
381 picref->buf->free = ff_avfilter_default_free_buffer;
382 if (!(picref->video = av_mallocz(sizeof(AVFilterBufferRefVideoProps))))
385 pic->w = picref->video->w = w;
386 pic->h = picref->video->h = h;
388 /* make sure the buffer gets read permission or it's useless for output */
389 picref->perms = perms | AV_PERM_READ;
392 picref->type = AVMEDIA_TYPE_VIDEO;
393 pic->format = picref->format = format;
395 memcpy(pic->data, data, sizeof(pic->data));
396 memcpy(pic->linesize, linesize, sizeof(pic->linesize));
397 memcpy(picref->data, pic->data, sizeof(picref->data));
398 memcpy(picref->linesize, pic->linesize, sizeof(picref->linesize));
403 if (picref && picref->video)
404 av_free(picref->video);
410 AVFilterBufferRef *avfilter_get_audio_buffer(AVFilterLink *link, int perms,
411 enum AVSampleFormat sample_fmt, int nb_samples,
412 int64_t channel_layout, int planar)
414 AVFilterBufferRef *ret = NULL;
416 if (link->dstpad->get_audio_buffer)
417 ret = link->dstpad->get_audio_buffer(link, perms, sample_fmt, nb_samples, channel_layout, planar);
420 ret = avfilter_default_get_audio_buffer(link, perms, sample_fmt, nb_samples, channel_layout, planar);
423 ret->type = AVMEDIA_TYPE_AUDIO;
429 avfilter_get_audio_buffer_ref_from_arrays(uint8_t *data[8], int linesize[8], int perms,
430 int nb_samples, enum AVSampleFormat sample_fmt,
431 int64_t channel_layout, int planar)
433 AVFilterBuffer *samples = av_mallocz(sizeof(AVFilterBuffer));
434 AVFilterBufferRef *samplesref = av_mallocz(sizeof(AVFilterBufferRef));
436 if (!samples || !samplesref)
439 samplesref->buf = samples;
440 samplesref->buf->free = ff_avfilter_default_free_buffer;
441 if (!(samplesref->audio = av_mallocz(sizeof(AVFilterBufferRefAudioProps))))
444 samplesref->audio->nb_samples = nb_samples;
445 samplesref->audio->channel_layout = channel_layout;
446 samplesref->audio->planar = planar;
448 /* make sure the buffer gets read permission or it's useless for output */
449 samplesref->perms = perms | AV_PERM_READ;
451 samples->refcount = 1;
452 samplesref->type = AVMEDIA_TYPE_AUDIO;
453 samplesref->format = sample_fmt;
455 memcpy(samples->data, data, sizeof(samples->data));
456 memcpy(samples->linesize, linesize, sizeof(samples->linesize));
457 memcpy(samplesref->data, data, sizeof(samplesref->data));
458 memcpy(samplesref->linesize, linesize, sizeof(samplesref->linesize));
463 if (samplesref && samplesref->audio)
464 av_freep(&samplesref->audio);
465 av_freep(&samplesref);
470 int avfilter_request_frame(AVFilterLink *link)
472 FF_DPRINTF_START(NULL, request_frame); ff_dlog_link(NULL, link, 1);
474 if (link->srcpad->request_frame)
475 return link->srcpad->request_frame(link);
476 else if (link->src->inputs[0])
477 return avfilter_request_frame(link->src->inputs[0]);
481 int avfilter_poll_frame(AVFilterLink *link)
483 int i, min = INT_MAX;
485 if (link->srcpad->poll_frame)
486 return link->srcpad->poll_frame(link);
488 for (i = 0; i < link->src->input_count; i++) {
490 if (!link->src->inputs[i])
492 val = avfilter_poll_frame(link->src->inputs[i]);
493 min = FFMIN(min, val);
499 /* XXX: should we do the duplicating of the picture ref here, instead of
500 * forcing the source filter to do it? */
501 void avfilter_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
503 void (*start_frame)(AVFilterLink *, AVFilterBufferRef *);
504 AVFilterPad *dst = link->dstpad;
505 int perms = picref->perms;
507 FF_DPRINTF_START(NULL, start_frame); ff_dlog_link(NULL, link, 0); av_dlog(NULL, " "); ff_dlog_ref(NULL, picref, 1);
509 if (!(start_frame = dst->start_frame))
510 start_frame = avfilter_default_start_frame;
512 if (picref->linesize[0] < 0)
513 perms |= AV_PERM_NEG_LINESIZES;
514 /* prepare to copy the picture if it has insufficient permissions */
515 if ((dst->min_perms & perms) != dst->min_perms || dst->rej_perms & perms) {
516 av_log(link->dst, AV_LOG_DEBUG,
517 "frame copy needed (have perms %x, need %x, reject %x)\n",
519 link->dstpad->min_perms, link->dstpad->rej_perms);
521 link->cur_buf = avfilter_get_video_buffer(link, dst->min_perms, link->w, link->h);
522 link->src_buf = picref;
523 avfilter_copy_buffer_ref_props(link->cur_buf, link->src_buf);
526 link->cur_buf = picref;
528 start_frame(link, link->cur_buf);
531 void avfilter_end_frame(AVFilterLink *link)
533 void (*end_frame)(AVFilterLink *);
535 if (!(end_frame = link->dstpad->end_frame))
536 end_frame = avfilter_default_end_frame;
540 /* unreference the source picture if we're feeding the destination filter
541 * a copied version dues to permission issues */
543 avfilter_unref_buffer(link->src_buf);
544 link->src_buf = NULL;
548 void avfilter_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
550 uint8_t *src[4], *dst[4];
552 void (*draw_slice)(AVFilterLink *, int, int, int);
554 FF_DPRINTF_START(NULL, draw_slice); ff_dlog_link(NULL, link, 0); av_dlog(NULL, " y:%d h:%d dir:%d\n", y, h, slice_dir);
556 /* copy the slice if needed for permission reasons */
558 vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h;
560 for (i = 0; i < 4; i++) {
561 if (link->src_buf->data[i]) {
562 src[i] = link->src_buf-> data[i] +
563 (y >> (i==1 || i==2 ? vsub : 0)) * link->src_buf-> linesize[i];
564 dst[i] = link->cur_buf->data[i] +
565 (y >> (i==1 || i==2 ? vsub : 0)) * link->cur_buf->linesize[i];
567 src[i] = dst[i] = NULL;
570 for (i = 0; i < 4; i++) {
572 av_image_get_linesize(link->format, link->cur_buf->video->w, i);
574 if (!src[i]) continue;
576 for (j = 0; j < h >> (i==1 || i==2 ? vsub : 0); j++) {
577 memcpy(dst[i], src[i], planew);
578 src[i] += link->src_buf->linesize[i];
579 dst[i] += link->cur_buf->linesize[i];
584 if (!(draw_slice = link->dstpad->draw_slice))
585 draw_slice = avfilter_default_draw_slice;
586 draw_slice(link, y, h, slice_dir);
589 void avfilter_filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
591 void (*filter_samples)(AVFilterLink *, AVFilterBufferRef *);
592 AVFilterPad *dst = link->dstpad;
595 FF_DPRINTF_START(NULL, filter_samples); ff_dlog_link(NULL, link, 1);
597 if (!(filter_samples = dst->filter_samples))
598 filter_samples = avfilter_default_filter_samples;
600 /* prepare to copy the samples if the buffer has insufficient permissions */
601 if ((dst->min_perms & samplesref->perms) != dst->min_perms ||
602 dst->rej_perms & samplesref->perms) {
604 av_log(link->dst, AV_LOG_DEBUG,
605 "Copying audio data in avfilter (have perms %x, need %x, reject %x)\n",
606 samplesref->perms, link->dstpad->min_perms, link->dstpad->rej_perms);
608 link->cur_buf = avfilter_default_get_audio_buffer(link, dst->min_perms,
610 samplesref->audio->nb_samples,
611 samplesref->audio->channel_layout,
612 samplesref->audio->planar);
613 link->cur_buf->pts = samplesref->pts;
614 link->cur_buf->audio->sample_rate = samplesref->audio->sample_rate;
616 /* Copy actual data into new samples buffer */
617 for (i = 0; samplesref->data[i]; i++)
618 memcpy(link->cur_buf->data[i], samplesref->data[i], samplesref->linesize[0]);
620 avfilter_unref_buffer(samplesref);
622 link->cur_buf = samplesref;
624 filter_samples(link, link->cur_buf);
627 #define MAX_REGISTERED_AVFILTERS_NB 64
629 static AVFilter *registered_avfilters[MAX_REGISTERED_AVFILTERS_NB + 1];
631 static int next_registered_avfilter_idx = 0;
633 AVFilter *avfilter_get_by_name(const char *name)
637 for (i = 0; registered_avfilters[i]; i++)
638 if (!strcmp(registered_avfilters[i]->name, name))
639 return registered_avfilters[i];
644 int avfilter_register(AVFilter *filter)
646 if (next_registered_avfilter_idx == MAX_REGISTERED_AVFILTERS_NB)
649 registered_avfilters[next_registered_avfilter_idx++] = filter;
653 AVFilter **av_filter_next(AVFilter **filter)
655 return filter ? ++filter : ®istered_avfilters[0];
658 void avfilter_uninit(void)
660 memset(registered_avfilters, 0, sizeof(registered_avfilters));
661 next_registered_avfilter_idx = 0;
664 static int pad_count(const AVFilterPad *pads)
668 for(count = 0; pads->name; count ++) pads ++;
672 static const char *filter_name(void *p)
674 AVFilterContext *filter = p;
675 return filter->filter->name;
678 static const AVClass avfilter_class = {
682 LIBAVUTIL_VERSION_INT,
685 int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *inst_name)
687 AVFilterContext *ret;
691 return AVERROR(EINVAL);
693 ret = av_mallocz(sizeof(AVFilterContext));
695 return AVERROR(ENOMEM);
697 ret->av_class = &avfilter_class;
698 ret->filter = filter;
699 ret->name = inst_name ? av_strdup(inst_name) : NULL;
700 if (filter->priv_size) {
701 ret->priv = av_mallocz(filter->priv_size);
706 ret->input_count = pad_count(filter->inputs);
707 if (ret->input_count) {
708 ret->input_pads = av_malloc(sizeof(AVFilterPad) * ret->input_count);
709 if (!ret->input_pads)
711 memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad) * ret->input_count);
712 ret->inputs = av_mallocz(sizeof(AVFilterLink*) * ret->input_count);
717 ret->output_count = pad_count(filter->outputs);
718 if (ret->output_count) {
719 ret->output_pads = av_malloc(sizeof(AVFilterPad) * ret->output_count);
720 if (!ret->output_pads)
722 memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad) * ret->output_count);
723 ret->outputs = av_mallocz(sizeof(AVFilterLink*) * ret->output_count);
732 av_freep(&ret->inputs);
733 av_freep(&ret->input_pads);
734 ret->input_count = 0;
735 av_freep(&ret->outputs);
736 av_freep(&ret->output_pads);
737 ret->output_count = 0;
738 av_freep(&ret->priv);
740 return AVERROR(ENOMEM);
743 void avfilter_free(AVFilterContext *filter)
748 if (filter->filter->uninit)
749 filter->filter->uninit(filter);
751 for (i = 0; i < filter->input_count; i++) {
752 if ((link = filter->inputs[i])) {
754 link->src->outputs[link->srcpad - link->src->output_pads] = NULL;
755 avfilter_formats_unref(&link->in_formats);
756 avfilter_formats_unref(&link->out_formats);
758 avfilter_link_free(&link);
760 for (i = 0; i < filter->output_count; i++) {
761 if ((link = filter->outputs[i])) {
763 link->dst->inputs[link->dstpad - link->dst->input_pads] = NULL;
764 avfilter_formats_unref(&link->in_formats);
765 avfilter_formats_unref(&link->out_formats);
767 avfilter_link_free(&link);
770 av_freep(&filter->name);
771 av_freep(&filter->input_pads);
772 av_freep(&filter->output_pads);
773 av_freep(&filter->inputs);
774 av_freep(&filter->outputs);
775 av_freep(&filter->priv);
779 int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
783 if (filter->filter->init)
784 ret = filter->filter->init(filter, args, opaque);