2 * Copyright (c) 2008 Vitor Sessak
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * memory buffer source filter
28 #include "buffersrc.h"
30 #include "vsrc_buffer.h"
33 #include "libavutil/audioconvert.h"
34 #include "libavutil/fifo.h"
35 #include "libavutil/imgutils.h"
36 #include "libavutil/opt.h"
37 #include "libavutil/samplefmt.h"
42 AVRational time_base; ///< time_base to set in the output link
46 enum PixelFormat pix_fmt;
47 AVRational pixel_aspect;
51 enum AVSampleFormat sample_fmt;
53 uint64_t channel_layout;
54 char *channel_layout_str;
57 } BufferSourceContext;
59 #define CHECK_VIDEO_PARAM_CHANGE(s, c, width, height, format)\
60 if (c->w != width || c->h != height || c->pix_fmt != format) {\
61 av_log(s, AV_LOG_ERROR, "Changing frame properties on the fly is not supported.\n");\
62 return AVERROR(EINVAL);\
65 #define CHECK_AUDIO_PARAM_CHANGE(s, c, srate, ch_layout, format)\
66 if (c->sample_fmt != format || c->sample_rate != srate ||\
67 c->channel_layout != ch_layout) {\
68 av_log(s, AV_LOG_ERROR, "Changing frame properties on the fly is not supported.\n");\
69 return AVERROR(EINVAL);\
72 #if FF_API_VSRC_BUFFER_ADD_FRAME
73 static int av_vsrc_buffer_add_frame_alt(AVFilterContext *buffer_filter, AVFrame *frame,
74 int64_t pts, AVRational pixel_aspect)
76 int64_t orig_pts = frame->pts;
77 AVRational orig_sar = frame->sample_aspect_ratio;
81 frame->sample_aspect_ratio = pixel_aspect;
82 if ((ret = av_buffersrc_write_frame(buffer_filter, frame)) < 0)
84 frame->pts = orig_pts;
85 frame->sample_aspect_ratio = orig_sar;
91 int av_buffersrc_write_frame(AVFilterContext *buffer_filter, AVFrame *frame)
93 BufferSourceContext *c = buffer_filter->priv;
94 AVFilterBufferRef *buf;
101 return AVERROR(EINVAL);
103 if (!av_fifo_space(c->fifo) &&
104 (ret = av_fifo_realloc2(c->fifo, av_fifo_size(c->fifo) +
108 switch (buffer_filter->outputs[0]->type) {
109 case AVMEDIA_TYPE_VIDEO:
110 CHECK_VIDEO_PARAM_CHANGE(buffer_filter, c, frame->width, frame->height,
112 buf = avfilter_get_video_buffer(buffer_filter->outputs[0], AV_PERM_WRITE,
114 av_image_copy(buf->data, buf->linesize, frame->data, frame->linesize,
115 c->pix_fmt, c->w, c->h);
117 case AVMEDIA_TYPE_AUDIO:
118 CHECK_AUDIO_PARAM_CHANGE(buffer_filter, c, frame->sample_rate, frame->channel_layout,
120 buf = ff_get_audio_buffer(buffer_filter->outputs[0], AV_PERM_WRITE,
122 av_samples_copy(buf->extended_data, frame->extended_data,
123 0, 0, frame->nb_samples,
124 av_get_channel_layout_nb_channels(frame->channel_layout),
128 return AVERROR(EINVAL);
131 avfilter_copy_frame_props(buf, frame);
133 if ((ret = av_fifo_generic_write(c->fifo, &buf, sizeof(buf), NULL)) < 0) {
134 avfilter_unref_buffer(buf);
141 int av_buffersrc_buffer(AVFilterContext *s, AVFilterBufferRef *buf)
143 BufferSourceContext *c = s->priv;
150 return AVERROR(EINVAL);
152 if (!av_fifo_space(c->fifo) &&
153 (ret = av_fifo_realloc2(c->fifo, av_fifo_size(c->fifo) +
157 switch (s->outputs[0]->type) {
158 case AVMEDIA_TYPE_VIDEO:
159 CHECK_VIDEO_PARAM_CHANGE(s, c, buf->video->w, buf->video->h, buf->format);
161 case AVMEDIA_TYPE_AUDIO:
162 CHECK_AUDIO_PARAM_CHANGE(s, c, buf->audio->sample_rate, buf->audio->channel_layout,
166 return AVERROR(EINVAL);
169 if ((ret = av_fifo_generic_write(c->fifo, &buf, sizeof(buf), NULL)) < 0)
175 static av_cold int init_video(AVFilterContext *ctx, const char *args, void *opaque)
177 BufferSourceContext *c = ctx->priv;
178 char pix_fmt_str[128];
182 (n = sscanf(args, "%d:%d:%127[^:]:%d:%d:%d:%d", &c->w, &c->h, pix_fmt_str,
183 &c->time_base.num, &c->time_base.den,
184 &c->pixel_aspect.num, &c->pixel_aspect.den)) != 7) {
185 av_log(ctx, AV_LOG_ERROR, "Expected 7 arguments, but %d found in '%s'\n", n, args);
186 return AVERROR(EINVAL);
188 if ((c->pix_fmt = av_get_pix_fmt(pix_fmt_str)) == PIX_FMT_NONE) {
190 c->pix_fmt = strtol(pix_fmt_str, &tail, 10);
191 if (*tail || c->pix_fmt < 0 || c->pix_fmt >= PIX_FMT_NB) {
192 av_log(ctx, AV_LOG_ERROR, "Invalid pixel format string '%s'\n", pix_fmt_str);
193 return AVERROR(EINVAL);
197 if (!(c->fifo = av_fifo_alloc(sizeof(AVFilterBufferRef*))))
198 return AVERROR(ENOMEM);
200 av_log(ctx, AV_LOG_INFO, "w:%d h:%d pixfmt:%s\n", c->w, c->h, av_pix_fmt_descriptors[c->pix_fmt].name);
204 #define OFFSET(x) offsetof(BufferSourceContext, x)
205 #define A AV_OPT_FLAG_AUDIO_PARAM
206 static const AVOption audio_options[] = {
207 { "time_base", NULL, OFFSET(time_base), AV_OPT_TYPE_RATIONAL, { 0 }, 0, INT_MAX, A },
208 { "sample_rate", NULL, OFFSET(sample_rate), AV_OPT_TYPE_INT, { 0 }, 0, INT_MAX, A },
209 { "sample_fmt", NULL, OFFSET(sample_fmt_str), AV_OPT_TYPE_STRING, .flags = A },
210 { "channel_layout", NULL, OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, .flags = A },
214 static const AVClass abuffer_class = {
215 .class_name = "abuffer source",
216 .item_name = av_default_item_name,
217 .option = audio_options,
218 .version = LIBAVUTIL_VERSION_INT,
221 static av_cold int init_audio(AVFilterContext *ctx, const char *args, void *opaque)
223 BufferSourceContext *s = ctx->priv;
226 s->class = &abuffer_class;
227 av_opt_set_defaults(s);
229 if ((ret = av_set_options_string(s, args, "=", ":")) < 0) {
230 av_log(ctx, AV_LOG_ERROR, "Error parsing options string: %s.\n", args);
234 s->sample_fmt = av_get_sample_fmt(s->sample_fmt_str);
235 if (s->sample_fmt == AV_SAMPLE_FMT_NONE) {
236 av_log(ctx, AV_LOG_ERROR, "Invalid sample format %s.\n",
238 ret = AVERROR(EINVAL);
242 s->channel_layout = av_get_channel_layout(s->channel_layout_str);
243 if (!s->channel_layout) {
244 av_log(ctx, AV_LOG_ERROR, "Invalid channel layout %s.\n",
245 s->channel_layout_str);
246 ret = AVERROR(EINVAL);
250 if (!(s->fifo = av_fifo_alloc(sizeof(AVFilterBufferRef*)))) {
251 ret = AVERROR(ENOMEM);
255 if (!s->time_base.num)
256 s->time_base = (AVRational){1, s->sample_rate};
258 av_log(ctx, AV_LOG_VERBOSE, "tb:%d/%d samplefmt:%s samplerate: %d "
259 "ch layout:%s\n", s->time_base.num, s->time_base.den, s->sample_fmt_str,
260 s->sample_rate, s->channel_layout_str);
267 static av_cold void uninit(AVFilterContext *ctx)
269 BufferSourceContext *s = ctx->priv;
270 while (s->fifo && av_fifo_size(s->fifo)) {
271 AVFilterBufferRef *buf;
272 av_fifo_generic_read(s->fifo, &buf, sizeof(buf), NULL);
273 avfilter_unref_buffer(buf);
275 av_fifo_free(s->fifo);
279 static int query_formats(AVFilterContext *ctx)
281 BufferSourceContext *c = ctx->priv;
282 AVFilterChannelLayouts *channel_layouts = NULL;
283 AVFilterFormats *formats = NULL;
284 AVFilterFormats *samplerates = NULL;
286 switch (ctx->outputs[0]->type) {
287 case AVMEDIA_TYPE_VIDEO:
288 avfilter_add_format(&formats, c->pix_fmt);
289 avfilter_set_common_formats(ctx, formats);
291 case AVMEDIA_TYPE_AUDIO:
292 avfilter_add_format(&formats, c->sample_fmt);
293 avfilter_set_common_formats(ctx, formats);
295 avfilter_add_format(&samplerates, c->sample_rate);
296 ff_set_common_samplerates(ctx, samplerates);
298 ff_add_channel_layout(&channel_layouts, c->channel_layout);
299 ff_set_common_channel_layouts(ctx, channel_layouts);
302 return AVERROR(EINVAL);
308 static int config_props(AVFilterLink *link)
310 BufferSourceContext *c = link->src->priv;
312 switch (link->type) {
313 case AVMEDIA_TYPE_VIDEO:
316 link->sample_aspect_ratio = c->pixel_aspect;
318 case AVMEDIA_TYPE_AUDIO:
319 link->channel_layout = c->channel_layout;
320 link->sample_rate = c->sample_rate;
323 return AVERROR(EINVAL);
326 link->time_base = c->time_base;
330 static int request_frame(AVFilterLink *link)
332 BufferSourceContext *c = link->src->priv;
333 AVFilterBufferRef *buf;
335 if (!av_fifo_size(c->fifo)) {
338 return AVERROR(EAGAIN);
340 av_fifo_generic_read(c->fifo, &buf, sizeof(buf), NULL);
342 switch (link->type) {
343 case AVMEDIA_TYPE_VIDEO:
344 avfilter_start_frame(link, avfilter_ref_buffer(buf, ~0));
345 avfilter_draw_slice(link, 0, link->h, 1);
346 avfilter_end_frame(link);
348 case AVMEDIA_TYPE_AUDIO:
349 ff_filter_samples(link, avfilter_ref_buffer(buf, ~0));
352 return AVERROR(EINVAL);
355 avfilter_unref_buffer(buf);
360 static int poll_frame(AVFilterLink *link)
362 BufferSourceContext *c = link->src->priv;
363 int size = av_fifo_size(c->fifo);
366 return size/sizeof(AVFilterBufferRef*);
369 AVFilter avfilter_vsrc_buffer = {
371 .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them accessible to the filterchain."),
372 .priv_size = sizeof(BufferSourceContext),
373 .query_formats = query_formats,
378 .inputs = (AVFilterPad[]) {{ .name = NULL }},
379 .outputs = (AVFilterPad[]) {{ .name = "default",
380 .type = AVMEDIA_TYPE_VIDEO,
381 .request_frame = request_frame,
382 .poll_frame = poll_frame,
383 .config_props = config_props, },
387 AVFilter avfilter_asrc_abuffer = {
389 .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them accessible to the filterchain."),
390 .priv_size = sizeof(BufferSourceContext),
391 .query_formats = query_formats,
396 .inputs = (AVFilterPad[]) {{ .name = NULL }},
397 .outputs = (AVFilterPad[]) {{ .name = "default",
398 .type = AVMEDIA_TYPE_AUDIO,
399 .request_frame = request_frame,
400 .poll_frame = poll_frame,
401 .config_props = config_props, },