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"
34 #include "libavutil/audioconvert.h"
35 #include "libavutil/fifo.h"
36 #include "libavutil/imgutils.h"
37 #include "libavutil/opt.h"
38 #include "libavutil/samplefmt.h"
43 AVRational time_base; ///< time_base to set in the output link
44 AVRational frame_rate; ///< frame_rate to set in the output link
45 unsigned nb_failed_requests;
46 unsigned warning_limit;
50 enum PixelFormat pix_fmt;
51 AVRational pixel_aspect;
56 enum AVSampleFormat sample_fmt;
58 uint64_t channel_layout;
59 char *channel_layout_str;
62 } BufferSourceContext;
64 #define CHECK_VIDEO_PARAM_CHANGE(s, c, width, height, format)\
65 if (c->w != width || c->h != height || c->pix_fmt != format) {\
66 av_log(s, AV_LOG_ERROR, "Changing frame properties on the fly is not supported.\n");\
67 return AVERROR(EINVAL);\
70 #define CHECK_AUDIO_PARAM_CHANGE(s, c, srate, ch_layout, format)\
71 if (c->sample_fmt != format || c->sample_rate != srate ||\
72 c->channel_layout != ch_layout) {\
73 av_log(s, AV_LOG_ERROR, "Changing frame properties on the fly is not supported.\n");\
74 return AVERROR(EINVAL);\
77 int av_buffersrc_add_frame(AVFilterContext *buffer_src,
78 const AVFrame *frame, int flags)
80 AVFilterBufferRef *picref;
83 if (!frame) /* NULL for EOF */
84 return av_buffersrc_add_ref(buffer_src, NULL, flags);
86 picref = avfilter_get_buffer_ref_from_frame(buffer_src->outputs[0]->type,
87 frame, AV_PERM_WRITE);
89 return AVERROR(ENOMEM);
90 ret = av_buffersrc_add_ref(buffer_src, picref, flags);
91 picref->buf->data[0] = NULL;
92 avfilter_unref_buffer(picref);
96 int av_buffersrc_write_frame(AVFilterContext *buffer_filter, AVFrame *frame)
98 return av_buffersrc_add_frame(buffer_filter, frame, 0);
101 int av_buffersrc_add_ref(AVFilterContext *s, AVFilterBufferRef *buf, int flags)
103 BufferSourceContext *c = s->priv;
104 AVFilterBufferRef *to_free = NULL;
111 return AVERROR(EINVAL);
113 if (!av_fifo_space(c->fifo) &&
114 (ret = av_fifo_realloc2(c->fifo, av_fifo_size(c->fifo) +
118 if (!(flags & AV_BUFFERSRC_FLAG_NO_CHECK_FORMAT)) {
119 switch (s->outputs[0]->type) {
120 case AVMEDIA_TYPE_VIDEO:
121 CHECK_VIDEO_PARAM_CHANGE(s, c, buf->video->w, buf->video->h, buf->format);
123 case AVMEDIA_TYPE_AUDIO:
124 CHECK_AUDIO_PARAM_CHANGE(s, c, buf->audio->sample_rate, buf->audio->channel_layout,
128 return AVERROR(EINVAL);
131 if (!(flags & AV_BUFFERSRC_FLAG_NO_COPY))
132 to_free = buf = ff_copy_buffer_ref(s->outputs[0], buf);
136 if ((ret = av_fifo_generic_write(c->fifo, &buf, sizeof(buf), NULL)) < 0) {
137 avfilter_unref_buffer(to_free);
140 c->nb_failed_requests = 0;
141 if (c->warning_limit &&
142 av_fifo_size(c->fifo) / sizeof(buf) >= c->warning_limit) {
143 av_log(s, AV_LOG_WARNING,
144 "%d buffers queued in %s, something may be wrong.\n",
146 (char *)av_x_if_null(s->name, s->filter->name));
147 c->warning_limit *= 10;
153 #ifdef FF_API_BUFFERSRC_BUFFER
154 int av_buffersrc_buffer(AVFilterContext *s, AVFilterBufferRef *buf)
156 return av_buffersrc_add_ref(s, buf, AV_BUFFERSRC_FLAG_NO_COPY);
160 unsigned av_buffersrc_get_nb_failed_requests(AVFilterContext *buffer_src)
162 return ((BufferSourceContext *)buffer_src->priv)->nb_failed_requests;
165 #define OFFSET(x) offsetof(BufferSourceContext, x)
166 #define V AV_OPT_FLAG_VIDEO_PARAM
167 static const AVOption buffer_options[] = {
168 { "time_base", NULL, OFFSET(time_base), AV_OPT_TYPE_RATIONAL, { 0 }, 0, INT_MAX, V },
169 { "frame_rate", NULL, OFFSET(frame_rate), AV_OPT_TYPE_RATIONAL, { 0 }, 0, INT_MAX, V },
170 { "video_size", NULL, OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, .flags = V },
171 { "pix_fmt", NULL, OFFSET(pix_fmt), AV_OPT_TYPE_PIXEL_FMT, .flags = V },
172 { "pixel_aspect", NULL, OFFSET(pixel_aspect), AV_OPT_TYPE_RATIONAL, { 0 }, 0, INT_MAX, V },
173 { "sws_param", NULL, OFFSET(sws_param), AV_OPT_TYPE_STRING, .flags = V },
178 AVFILTER_DEFINE_CLASS(buffer);
180 static av_cold int init_video(AVFilterContext *ctx, const char *args)
182 BufferSourceContext *c = ctx->priv;
183 char pix_fmt_str[128], sws_param[256] = "", *colon, *equal;
186 c->class = &buffer_class;
189 av_log(ctx, AV_LOG_ERROR, "Arguments required\n");
190 return AVERROR(EINVAL);
192 colon = strchr(args, ':');
193 equal = strchr(args, '=');
194 if (equal && (!colon || equal < colon)) {
195 av_opt_set_defaults(c);
196 ret = av_set_options_string(c, args, "=", ":");
198 av_log(ctx, AV_LOG_ERROR, "Error parsing options string: %s\n", args);
202 if ((n = sscanf(args, "%d:%d:%127[^:]:%d:%d:%d:%d:%255c", &c->w, &c->h, pix_fmt_str,
203 &c->time_base.num, &c->time_base.den,
204 &c->pixel_aspect.num, &c->pixel_aspect.den, sws_param)) < 7) {
205 av_log(ctx, AV_LOG_ERROR, "Expected at least 7 arguments, but only %d found in '%s'\n", n, args);
206 ret = AVERROR(EINVAL);
209 av_log(ctx, AV_LOG_WARNING, "Flat options syntax is deprecated, use key=value pairs\n");
211 if ((ret = ff_parse_pixel_format(&c->pix_fmt, pix_fmt_str, ctx)) < 0)
213 c->sws_param = av_strdup(sws_param);
215 ret = AVERROR(ENOMEM);
220 if (!(c->fifo = av_fifo_alloc(sizeof(AVFilterBufferRef*)))) {
221 ret = AVERROR(ENOMEM);
225 av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d pixfmt:%s tb:%d/%d fr:%d/%d sar:%d/%d sws_param:%s\n",
226 c->w, c->h, av_pix_fmt_descriptors[c->pix_fmt].name,
227 c->time_base.num, c->time_base.den, c->frame_rate.num, c->frame_rate.den,
228 c->pixel_aspect.num, c->pixel_aspect.den, (char *)av_x_if_null(c->sws_param, ""));
229 c->warning_limit = 100;
237 #define A AV_OPT_FLAG_AUDIO_PARAM
238 static const AVOption abuffer_options[] = {
239 { "time_base", NULL, OFFSET(time_base), AV_OPT_TYPE_RATIONAL, { 0 }, 0, INT_MAX, A },
240 { "sample_rate", NULL, OFFSET(sample_rate), AV_OPT_TYPE_INT, { 0 }, 0, INT_MAX, A },
241 { "sample_fmt", NULL, OFFSET(sample_fmt_str), AV_OPT_TYPE_STRING, .flags = A },
242 { "channel_layout", NULL, OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, .flags = A },
246 AVFILTER_DEFINE_CLASS(abuffer);
248 static av_cold int init_audio(AVFilterContext *ctx, const char *args)
250 BufferSourceContext *s = ctx->priv;
253 s->class = &abuffer_class;
254 av_opt_set_defaults(s);
256 if ((ret = av_set_options_string(s, args, "=", ":")) < 0) {
257 av_log(ctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
261 s->sample_fmt = av_get_sample_fmt(s->sample_fmt_str);
262 if (s->sample_fmt == AV_SAMPLE_FMT_NONE) {
263 av_log(ctx, AV_LOG_ERROR, "Invalid sample format '%s'\n",
265 ret = AVERROR(EINVAL);
269 s->channel_layout = av_get_channel_layout(s->channel_layout_str);
270 if (!s->channel_layout) {
271 av_log(ctx, AV_LOG_ERROR, "Invalid channel layout '%s'\n",
272 s->channel_layout_str);
273 ret = AVERROR(EINVAL);
277 if (!(s->fifo = av_fifo_alloc(sizeof(AVFilterBufferRef*)))) {
278 ret = AVERROR(ENOMEM);
282 if (!s->time_base.num)
283 s->time_base = (AVRational){1, s->sample_rate};
285 av_log(ctx, AV_LOG_VERBOSE,
286 "tb:%d/%d samplefmt:%s samplerate:%d chlayout:%s\n",
287 s->time_base.num, s->time_base.den, s->sample_fmt_str,
288 s->sample_rate, s->channel_layout_str);
289 s->warning_limit = 100;
296 static av_cold void uninit(AVFilterContext *ctx)
298 BufferSourceContext *s = ctx->priv;
299 while (s->fifo && av_fifo_size(s->fifo)) {
300 AVFilterBufferRef *buf;
301 av_fifo_generic_read(s->fifo, &buf, sizeof(buf), NULL);
302 avfilter_unref_buffer(buf);
304 av_fifo_free(s->fifo);
306 av_freep(&s->sws_param);
309 static int query_formats(AVFilterContext *ctx)
311 BufferSourceContext *c = ctx->priv;
312 AVFilterChannelLayouts *channel_layouts = NULL;
313 AVFilterFormats *formats = NULL;
314 AVFilterFormats *samplerates = NULL;
316 switch (ctx->outputs[0]->type) {
317 case AVMEDIA_TYPE_VIDEO:
318 ff_add_format(&formats, c->pix_fmt);
319 ff_set_common_formats(ctx, formats);
321 case AVMEDIA_TYPE_AUDIO:
322 ff_add_format(&formats, c->sample_fmt);
323 ff_set_common_formats(ctx, formats);
325 ff_add_format(&samplerates, c->sample_rate);
326 ff_set_common_samplerates(ctx, samplerates);
328 ff_add_channel_layout(&channel_layouts, c->channel_layout);
329 ff_set_common_channel_layouts(ctx, channel_layouts);
332 return AVERROR(EINVAL);
338 static int config_props(AVFilterLink *link)
340 BufferSourceContext *c = link->src->priv;
342 switch (link->type) {
343 case AVMEDIA_TYPE_VIDEO:
346 link->sample_aspect_ratio = c->pixel_aspect;
348 case AVMEDIA_TYPE_AUDIO:
349 link->channel_layout = c->channel_layout;
350 link->sample_rate = c->sample_rate;
353 return AVERROR(EINVAL);
356 link->time_base = c->time_base;
357 link->frame_rate = c->frame_rate;
361 static int request_frame(AVFilterLink *link)
363 BufferSourceContext *c = link->src->priv;
364 AVFilterBufferRef *buf;
367 if (!av_fifo_size(c->fifo)) {
370 c->nb_failed_requests++;
371 return AVERROR(EAGAIN);
373 av_fifo_generic_read(c->fifo, &buf, sizeof(buf), NULL);
375 switch (link->type) {
376 case AVMEDIA_TYPE_VIDEO:
377 if ((ret = ff_start_frame(link, buf)) < 0 ||
378 (ret = ff_draw_slice(link, 0, link->h, 1)) < 0 ||
379 (ret = ff_end_frame(link)) < 0)
382 case AVMEDIA_TYPE_AUDIO:
383 ret = ff_filter_samples(link, buf);
386 avfilter_unref_bufferp(&buf);
387 return AVERROR(EINVAL);
393 static int poll_frame(AVFilterLink *link)
395 BufferSourceContext *c = link->src->priv;
396 int size = av_fifo_size(c->fifo);
399 return size/sizeof(AVFilterBufferRef*);
402 AVFilter avfilter_vsrc_buffer = {
404 .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them accessible to the filterchain."),
405 .priv_size = sizeof(BufferSourceContext),
406 .query_formats = query_formats,
411 .inputs = (const AVFilterPad[]) {{ .name = NULL }},
412 .outputs = (const AVFilterPad[]) {{ .name = "default",
413 .type = AVMEDIA_TYPE_VIDEO,
414 .request_frame = request_frame,
415 .poll_frame = poll_frame,
416 .config_props = config_props, },
420 AVFilter avfilter_asrc_abuffer = {
422 .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them accessible to the filterchain."),
423 .priv_size = sizeof(BufferSourceContext),
424 .query_formats = query_formats,
429 .inputs = (const AVFilterPad[]) {{ .name = NULL }},
430 .outputs = (const AVFilterPad[]) {{ .name = "default",
431 .type = AVMEDIA_TYPE_AUDIO,
432 .request_frame = request_frame,
433 .poll_frame = poll_frame,
434 .config_props = config_props, },