2 * Copyright (c) 2008 Vitor Sessak
3 * Copyright (c) 2010 S.N. Hemanth Meenakshisundaram
4 * Copyright (c) 2011 Mina Nagy Zaki
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 * memory buffer source filter
31 #include "buffersrc.h"
32 #include "vsrc_buffer.h"
33 #include "asrc_abuffer.h"
34 #include "libavutil/audioconvert.h"
35 #include "libavutil/avstring.h"
36 #include "libavutil/fifo.h"
37 #include "libavutil/imgutils.h"
41 AVRational time_base; ///< time_base to set in the output link
43 unsigned nb_failed_requests;
46 AVFilterContext *scale;
48 enum PixelFormat pix_fmt;
49 AVRational sample_aspect_ratio;
53 // Audio format of incoming buffers
55 unsigned int sample_format;
56 int64_t channel_layout;
59 // Normalization filters
60 AVFilterContext *aconvert;
61 AVFilterContext *aresample;
62 } BufferSourceContext;
66 #define CHECK_PARAM_CHANGE(s, c, width, height, format)\
67 if (c->w != width || c->h != height || c->pix_fmt != format) {\
68 av_log(s, AV_LOG_ERROR, "Changing frame properties on the fly is not supported.\n");\
69 return AVERROR(EINVAL);\
72 static void buf_free(AVFilterBuffer *ptr)
78 static void set_link_source(AVFilterContext *src, AVFilterLink *link)
81 link->srcpad = &(src->output_pads[0]);
82 src->outputs[0] = link;
85 static int reconfigure_filter(BufferSourceContext *abuffer, AVFilterContext *filt_ctx)
88 AVFilterLink * const inlink = filt_ctx->inputs[0];
89 AVFilterLink * const outlink = filt_ctx->outputs[0];
91 inlink->format = abuffer->sample_format;
92 inlink->channel_layout = abuffer->channel_layout;
93 inlink->planar = abuffer->packing_format;
94 inlink->sample_rate = abuffer->sample_rate;
96 filt_ctx->filter->uninit(filt_ctx);
97 memset(filt_ctx->priv, 0, filt_ctx->filter->priv_size);
98 if ((ret = filt_ctx->filter->init(filt_ctx, NULL , NULL)) < 0)
100 if ((ret = inlink->srcpad->config_props(inlink)) < 0)
102 return outlink->srcpad->config_props(outlink);
105 static int insert_filter(BufferSourceContext *abuffer,
106 AVFilterLink *link, AVFilterContext **filt_ctx,
107 const char *filt_name)
111 if ((ret = avfilter_open(filt_ctx, avfilter_get_by_name(filt_name), NULL)) < 0)
114 link->src->outputs[0] = NULL;
115 if ((ret = avfilter_link(link->src, 0, *filt_ctx, 0)) < 0) {
116 link->src->outputs[0] = link;
120 set_link_source(*filt_ctx, link);
122 if ((ret = reconfigure_filter(abuffer, *filt_ctx)) < 0) {
123 avfilter_free(*filt_ctx);
130 static void remove_filter(AVFilterContext **filt_ctx)
132 AVFilterLink *outlink = (*filt_ctx)->outputs[0];
133 AVFilterContext *src = (*filt_ctx)->inputs[0]->src;
135 (*filt_ctx)->outputs[0] = NULL;
136 avfilter_free(*filt_ctx);
139 set_link_source(src, outlink);
142 static inline void log_input_change(void *ctx, AVFilterLink *link, AVFilterBufferRef *ref)
144 char old_layout_str[16], new_layout_str[16];
145 av_get_channel_layout_string(old_layout_str, sizeof(old_layout_str),
146 -1, link->channel_layout);
147 av_get_channel_layout_string(new_layout_str, sizeof(new_layout_str),
148 -1, ref->audio->channel_layout);
149 av_log(ctx, AV_LOG_INFO,
150 "Audio input format changed: "
151 "%s:%s:%d -> %s:%s:%d, normalizing\n",
152 av_get_sample_fmt_name(link->format),
153 old_layout_str, (int)link->sample_rate,
154 av_get_sample_fmt_name(ref->format),
155 new_layout_str, ref->audio->sample_rate);
158 static int check_format_change_video(AVFilterContext *buffer_filter,
159 AVFilterBufferRef *picref)
161 BufferSourceContext *c = buffer_filter->priv;
164 if (picref->video->w != c->w || picref->video->h != c->h || picref->format != c->pix_fmt) {
165 AVFilterContext *scale = buffer_filter->outputs[0]->dst;
167 char scale_param[1024];
169 av_log(buffer_filter, AV_LOG_INFO,
170 "Buffer video input changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s\n",
171 c->w, c->h, av_pix_fmt_descriptors[c->pix_fmt].name,
172 picref->video->w, picref->video->h, av_pix_fmt_descriptors[picref->format].name);
174 if (!scale || strcmp(scale->filter->name, "scale")) {
175 AVFilter *f = avfilter_get_by_name("scale");
177 av_log(buffer_filter, AV_LOG_INFO, "Inserting scaler filter\n");
178 if ((ret = avfilter_open(&scale, f, "Input equalizer")) < 0)
183 snprintf(scale_param, sizeof(scale_param)-1, "%d:%d:%s", c->w, c->h, c->sws_param);
184 if ((ret = avfilter_init_filter(scale, scale_param, NULL)) < 0) {
188 if ((ret = avfilter_insert_filter(buffer_filter->outputs[0], scale, 0, 0)) < 0) {
191 scale->outputs[0]->time_base = scale->inputs[0]->time_base;
193 scale->outputs[0]->format= c->pix_fmt;
194 } else if (!strcmp(scale->filter->name, "scale")) {
195 snprintf(scale_param, sizeof(scale_param)-1, "%d:%d:%s",
196 scale->outputs[0]->w, scale->outputs[0]->h, c->sws_param);
197 scale->filter->init(scale, scale_param, NULL);
200 c->pix_fmt = scale->inputs[0]->format = picref->format;
201 c->w = scale->inputs[0]->w = picref->video->w;
202 c->h = scale->inputs[0]->h = picref->video->h;
204 link = scale->outputs[0];
205 if ((ret = link->srcpad->config_props(link)) < 0)
211 static int check_format_change_audio(AVFilterContext *ctx,
212 AVFilterBufferRef *samplesref)
214 BufferSourceContext *abuffer = ctx->priv;
218 link = ctx->outputs[0];
219 if (samplesref->audio->sample_rate != link->sample_rate) {
221 log_input_change(ctx, link, samplesref);
224 abuffer->sample_rate = samplesref->audio->sample_rate;
226 if (!abuffer->aresample) {
227 ret = insert_filter(abuffer, link, &abuffer->aresample, "aresample");
228 if (ret < 0) return ret;
230 link = abuffer->aresample->outputs[0];
231 if (samplesref->audio->sample_rate == link->sample_rate)
232 remove_filter(&abuffer->aresample);
234 if ((ret = reconfigure_filter(abuffer, abuffer->aresample)) < 0)
239 link = ctx->outputs[0];
240 if (samplesref->format != link->format ||
241 samplesref->audio->channel_layout != link->channel_layout ||
242 samplesref->audio->planar != link->planar) {
244 if (!logged) log_input_change(ctx, link, samplesref);
246 abuffer->sample_format = samplesref->format;
247 abuffer->channel_layout = samplesref->audio->channel_layout;
248 abuffer->packing_format = samplesref->audio->planar;
250 if (!abuffer->aconvert) {
251 ret = insert_filter(abuffer, link, &abuffer->aconvert, "aconvert");
252 if (ret < 0) return ret;
254 link = abuffer->aconvert->outputs[0];
255 if (samplesref->format == link->format &&
256 samplesref->audio->channel_layout == link->channel_layout &&
257 samplesref->audio->planar == link->planar
259 remove_filter(&abuffer->aconvert);
261 if ((ret = reconfigure_filter(abuffer, abuffer->aconvert)) < 0)
269 static int check_format_change(AVFilterContext *buffer_filter,
270 AVFilterBufferRef *picref)
272 switch (buffer_filter->outputs[0]->type) {
273 case AVMEDIA_TYPE_VIDEO:
274 return check_format_change_video(buffer_filter, picref);
275 case AVMEDIA_TYPE_AUDIO:
276 return check_format_change_audio(buffer_filter, picref);
278 return AVERROR(ENOSYS);
282 static AVFilterBufferRef *copy_buffer_ref(AVFilterContext *ctx,
283 AVFilterBufferRef *ref)
285 AVFilterLink *outlink = ctx->outputs[0];
286 AVFilterBufferRef *buf;
287 int channels, data_size, i;
289 switch (outlink->type) {
291 case AVMEDIA_TYPE_VIDEO:
292 buf = avfilter_get_video_buffer(outlink, AV_PERM_WRITE,
293 ref->video->w, ref->video->h);
294 av_image_copy(buf->data, buf->linesize,
295 (void*)ref->data, ref->linesize,
296 ref->format, ref->video->w, ref->video->h);
299 case AVMEDIA_TYPE_AUDIO:
300 buf = avfilter_get_audio_buffer(outlink, AV_PERM_WRITE,
301 ref->audio->nb_samples);
302 channels = av_get_channel_layout_nb_channels(ref->audio->channel_layout);
303 data_size = av_samples_get_buffer_size(NULL, channels,
304 ref->audio->nb_samples,
306 for (i = 0; i < FF_ARRAY_ELEMS(ref->buf->data) && ref->buf->data[i]; i++)
307 memcpy(buf->buf->data[i], ref->buf->data[i], data_size);
313 avfilter_copy_buffer_ref_props(buf, ref);
317 int av_buffersrc_add_ref(AVFilterContext *buffer_filter,
318 AVFilterBufferRef *picref, int flags)
320 BufferSourceContext *c = buffer_filter->priv;
321 AVFilterBufferRef *buf;
328 return AVERROR(EINVAL);
330 if (!av_fifo_space(c->fifo) &&
331 (ret = av_fifo_realloc2(c->fifo, av_fifo_size(c->fifo) +
335 if (!(flags & AV_BUFFERSRC_FLAG_NO_CHECK_FORMAT)) {
336 ret = check_format_change(buffer_filter, picref);
340 if (flags & AV_BUFFERSRC_FLAG_NO_COPY)
343 buf = copy_buffer_ref(buffer_filter, picref);
345 if ((ret = av_fifo_generic_write(c->fifo, &buf, sizeof(buf), NULL)) < 0) {
347 avfilter_unref_buffer(buf);
350 c->nb_failed_requests = 0;
355 int av_vsrc_buffer_add_video_buffer_ref(AVFilterContext *buffer_filter,
356 AVFilterBufferRef *picref, int flags)
358 return av_buffersrc_add_ref(buffer_filter, picref, 0);
364 int av_buffersrc_add_frame(AVFilterContext *buffer_src,
365 const AVFrame *frame, int flags)
367 AVFilterBufferRef *picref;
370 if (!frame) /* NULL for EOF */
371 return av_buffersrc_add_ref(buffer_src, NULL, flags);
373 switch (buffer_src->outputs[0]->type) {
374 case AVMEDIA_TYPE_VIDEO:
375 picref = avfilter_get_video_buffer_ref_from_frame(frame, AV_PERM_WRITE);
377 case AVMEDIA_TYPE_AUDIO:
378 picref = avfilter_get_audio_buffer_ref_from_frame(frame, AV_PERM_WRITE);
381 return AVERROR(ENOSYS);
384 return AVERROR(ENOMEM);
385 ret = av_buffersrc_add_ref(buffer_src, picref, flags);
386 picref->buf->data[0] = NULL;
387 avfilter_unref_buffer(picref);
391 int av_vsrc_buffer_add_frame(AVFilterContext *buffer_src,
392 const AVFrame *frame, int flags)
394 return av_buffersrc_add_frame(buffer_src, frame, 0);
398 unsigned av_buffersrc_get_nb_failed_requests(AVFilterContext *buffer_src)
400 return ((BufferSourceContext *)buffer_src->priv)->nb_failed_requests;
403 unsigned av_vsrc_buffer_get_nb_failed_requests(AVFilterContext *buffer_src)
405 return ((BufferSourceContext *)buffer_src->priv)->nb_failed_requests;
408 static av_cold int init_video(AVFilterContext *ctx, const char *args, void *opaque)
410 BufferSourceContext *c = ctx->priv;
411 char pix_fmt_str[128];
416 (n = sscanf(args, "%d:%d:%127[^:]:%d:%d:%d:%d:%255c", &c->w, &c->h, pix_fmt_str,
417 &c->time_base.num, &c->time_base.den,
418 &c->sample_aspect_ratio.num, &c->sample_aspect_ratio.den, c->sws_param)) < 7) {
419 av_log(ctx, AV_LOG_ERROR, "Expected at least 7 arguments, but only %d found in '%s'\n", n, args);
420 return AVERROR(EINVAL);
423 if ((ret = ff_parse_pixel_format(&c->pix_fmt, pix_fmt_str, ctx)) < 0)
426 if (!(c->fifo = av_fifo_alloc(sizeof(AVFilterBufferRef*))))
427 return AVERROR(ENOMEM);
429 av_log(ctx, AV_LOG_INFO, "w:%d h:%d pixfmt:%s tb:%d/%d sar:%d/%d sws_param:%s\n",
430 c->w, c->h, av_pix_fmt_descriptors[c->pix_fmt].name,
431 c->time_base.num, c->time_base.den,
432 c->sample_aspect_ratio.num, c->sample_aspect_ratio.den, c->sws_param);
436 static av_cold int init_audio(AVFilterContext *ctx, const char *args0, void *opaque)
438 BufferSourceContext *abuffer = ctx->priv;
439 char *arg = NULL, *ptr, chlayout_str[16];
440 char *args = av_strdup(args0);
443 arg = av_strtok(args, ":", &ptr);
445 #define ADD_FORMAT(fmt_name) \
448 if ((ret = ff_parse_##fmt_name(&abuffer->fmt_name, arg, ctx)) < 0) { \
453 arg = av_strtok(NULL, ":", &ptr)
455 ADD_FORMAT(sample_rate);
456 ADD_FORMAT(sample_format);
457 ADD_FORMAT(channel_layout);
458 ADD_FORMAT(packing_format);
460 abuffer->fifo = av_fifo_alloc(FIFO_SIZE*sizeof(AVFilterBufferRef*));
461 if (!abuffer->fifo) {
462 av_log(ctx, AV_LOG_ERROR, "Failed to allocate fifo, filter init failed.\n");
463 return AVERROR(ENOMEM);
466 av_get_channel_layout_string(chlayout_str, sizeof(chlayout_str),
467 -1, abuffer->channel_layout);
468 av_log(ctx, AV_LOG_INFO, "format:%s layout:%s rate:%d\n",
469 av_get_sample_fmt_name(abuffer->sample_format), chlayout_str,
470 abuffer->sample_rate);
476 av_log(ctx, AV_LOG_ERROR, "Invalid arguments, must be of the form "
477 "sample_rate:sample_fmt:channel_layout:packing\n");
479 return AVERROR(EINVAL);
482 static av_cold void uninit(AVFilterContext *ctx)
484 BufferSourceContext *s = ctx->priv;
485 while (s->fifo && av_fifo_size(s->fifo)) {
486 AVFilterBufferRef *buf;
487 av_fifo_generic_read(s->fifo, &buf, sizeof(buf), NULL);
488 avfilter_unref_buffer(buf);
490 av_fifo_free(s->fifo);
492 avfilter_free(s->scale);
496 static int query_formats_video(AVFilterContext *ctx)
498 BufferSourceContext *c = ctx->priv;
499 enum PixelFormat pix_fmts[] = { c->pix_fmt, PIX_FMT_NONE };
501 avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
505 static int query_formats_audio(AVFilterContext *ctx)
507 BufferSourceContext *abuffer = ctx->priv;
508 AVFilterFormats *formats;
511 avfilter_add_format(&formats, abuffer->sample_format);
512 avfilter_set_common_sample_formats(ctx, formats);
515 avfilter_add_format(&formats, abuffer->channel_layout);
516 avfilter_set_common_channel_layouts(ctx, formats);
519 avfilter_add_format(&formats, abuffer->packing_format);
520 avfilter_set_common_packing_formats(ctx, formats);
525 static int config_output_video(AVFilterLink *link)
527 BufferSourceContext *c = link->src->priv;
531 link->sample_aspect_ratio = c->sample_aspect_ratio;
532 link->time_base = c->time_base;
537 static int config_output_audio(AVFilterLink *outlink)
539 BufferSourceContext *abuffer = outlink->src->priv;
540 outlink->sample_rate = abuffer->sample_rate;
544 static int request_frame(AVFilterLink *link)
546 BufferSourceContext *c = link->src->priv;
547 AVFilterBufferRef *buf;
549 if (!av_fifo_size(c->fifo)) {
552 c->nb_failed_requests++;
553 return AVERROR(EAGAIN);
555 av_fifo_generic_read(c->fifo, &buf, sizeof(buf), NULL);
557 switch (link->type) {
558 case AVMEDIA_TYPE_VIDEO:
559 avfilter_start_frame(link, avfilter_ref_buffer(buf, ~0));
560 avfilter_draw_slice(link, 0, link->h, 1);
561 avfilter_end_frame(link);
562 avfilter_unref_buffer(buf);
564 case AVMEDIA_TYPE_AUDIO:
565 avfilter_filter_samples(link, avfilter_ref_buffer(buf, ~0));
566 avfilter_unref_buffer(buf);
569 return AVERROR(ENOSYS);
574 static int poll_frame(AVFilterLink *link)
576 BufferSourceContext *c = link->src->priv;
577 int size = av_fifo_size(c->fifo);
580 return size/sizeof(AVFilterBufferRef*);
583 int av_asrc_buffer_add_audio_buffer_ref(AVFilterContext *ctx,
584 AVFilterBufferRef *samplesref,
587 return av_buffersrc_add_ref(ctx, samplesref, AV_BUFFERSRC_FLAG_NO_COPY);
590 int av_asrc_buffer_add_samples(AVFilterContext *ctx,
591 uint8_t *data[8], int linesize[8],
592 int nb_samples, int sample_rate,
593 int sample_fmt, int64_t channel_layout, int planar,
594 int64_t pts, int av_unused flags)
596 AVFilterBufferRef *samplesref;
598 samplesref = avfilter_get_audio_buffer_ref_from_arrays(
599 data, linesize, AV_PERM_WRITE,
601 sample_fmt, channel_layout, planar);
603 return AVERROR(ENOMEM);
605 samplesref->buf->free = buf_free;
606 samplesref->pts = pts;
607 samplesref->audio->sample_rate = sample_rate;
609 AV_NOWARN_DEPRECATED(
610 return av_asrc_buffer_add_audio_buffer_ref(ctx, samplesref, 0);
614 int av_asrc_buffer_add_buffer(AVFilterContext *ctx,
615 uint8_t *buf, int buf_size, int sample_rate,
616 int sample_fmt, int64_t channel_layout, int planar,
617 int64_t pts, int av_unused flags)
619 uint8_t *data[8] = {0};
621 int nb_channels = av_get_channel_layout_nb_channels(channel_layout),
622 nb_samples = buf_size / nb_channels / av_get_bytes_per_sample(sample_fmt);
624 av_samples_fill_arrays(data, linesize,
625 buf, nb_channels, nb_samples,
628 AV_NOWARN_DEPRECATED(
629 return av_asrc_buffer_add_samples(ctx,
630 data, linesize, nb_samples,
632 sample_fmt, channel_layout, planar,
637 AVFilter avfilter_vsrc_buffer = {
639 .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them accessible to the filterchain."),
640 .priv_size = sizeof(BufferSourceContext),
641 .query_formats = query_formats_video,
646 .inputs = (const AVFilterPad[]) {{ .name = NULL }},
647 .outputs = (const AVFilterPad[]) {{ .name = "default",
648 .type = AVMEDIA_TYPE_VIDEO,
649 .request_frame = request_frame,
650 .poll_frame = poll_frame,
651 .config_props = config_output_video, },
655 #ifdef CONFIG_ABUFFER_FILTER
657 AVFilter avfilter_asrc_abuffer = {
659 .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them accessible to the filterchain."),
660 .priv_size = sizeof(BufferSourceContext),
661 .query_formats = query_formats_audio,
666 .inputs = (const AVFilterPad[]) {{ .name = NULL }},
667 .outputs = (const AVFilterPad[]) {{ .name = "default",
668 .type = AVMEDIA_TYPE_AUDIO,
669 .request_frame = request_frame,
670 .poll_frame = poll_frame,
671 .config_props = config_output_audio, },