2 * Copyright (c) 2008 Vitor Sessak
4 * This file is part of Libav.
6 * Libav 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 * Libav 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 Libav; 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
26 #include "libavutil/channel_layout.h"
27 #include "libavutil/common.h"
28 #include "libavutil/fifo.h"
29 #include "libavutil/imgutils.h"
30 #include "libavutil/opt.h"
31 #include "libavutil/samplefmt.h"
34 #include "buffersrc.h"
42 AVRational time_base
; ///< time_base to set in the output link
46 enum AVPixelFormat 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 int av_buffersrc_write_frame(AVFilterContext
*buffer_filter
, const AVFrame
*frame
)
74 BufferSourceContext
*c
= buffer_filter
->priv
;
75 AVFilterBufferRef
*buf
;
82 return AVERROR(EINVAL
);
84 if (!av_fifo_space(c
->fifo
) &&
85 (ret
= av_fifo_realloc2(c
->fifo
, av_fifo_size(c
->fifo
) +
89 switch (buffer_filter
->outputs
[0]->type
) {
90 case AVMEDIA_TYPE_VIDEO
:
91 CHECK_VIDEO_PARAM_CHANGE(buffer_filter
, c
, frame
->width
, frame
->height
,
93 buf
= ff_get_video_buffer(buffer_filter
->outputs
[0], AV_PERM_WRITE
,
96 return AVERROR(ENOMEM
);
98 av_image_copy(buf
->data
, buf
->linesize
, frame
->data
, frame
->linesize
,
99 c
->pix_fmt
, c
->w
, c
->h
);
101 case AVMEDIA_TYPE_AUDIO
:
102 CHECK_AUDIO_PARAM_CHANGE(buffer_filter
, c
, frame
->sample_rate
, frame
->channel_layout
,
104 buf
= ff_get_audio_buffer(buffer_filter
->outputs
[0], AV_PERM_WRITE
,
107 return AVERROR(ENOMEM
);
109 av_samples_copy(buf
->extended_data
, frame
->extended_data
,
110 0, 0, frame
->nb_samples
,
111 av_get_channel_layout_nb_channels(frame
->channel_layout
),
115 return AVERROR(EINVAL
);
118 avfilter_copy_frame_props(buf
, frame
);
120 if ((ret
= av_fifo_generic_write(c
->fifo
, &buf
, sizeof(buf
), NULL
)) < 0) {
121 avfilter_unref_buffer(buf
);
128 int av_buffersrc_buffer(AVFilterContext
*s
, AVFilterBufferRef
*buf
)
130 BufferSourceContext
*c
= s
->priv
;
137 return AVERROR(EINVAL
);
139 if (!av_fifo_space(c
->fifo
) &&
140 (ret
= av_fifo_realloc2(c
->fifo
, av_fifo_size(c
->fifo
) +
144 switch (s
->outputs
[0]->type
) {
145 case AVMEDIA_TYPE_VIDEO
:
146 CHECK_VIDEO_PARAM_CHANGE(s
, c
, buf
->video
->w
, buf
->video
->h
, buf
->format
);
148 case AVMEDIA_TYPE_AUDIO
:
149 CHECK_AUDIO_PARAM_CHANGE(s
, c
, buf
->audio
->sample_rate
, buf
->audio
->channel_layout
,
153 return AVERROR(EINVAL
);
156 if ((ret
= av_fifo_generic_write(c
->fifo
, &buf
, sizeof(buf
), NULL
)) < 0)
162 static av_cold
int init_video(AVFilterContext
*ctx
, const char *args
)
164 BufferSourceContext
*c
= ctx
->priv
;
165 char pix_fmt_str
[128];
169 (n
= sscanf(args
, "%d:%d:%127[^:]:%d:%d:%d:%d", &c
->w
, &c
->h
, pix_fmt_str
,
170 &c
->time_base
.num
, &c
->time_base
.den
,
171 &c
->pixel_aspect
.num
, &c
->pixel_aspect
.den
)) != 7) {
172 av_log(ctx
, AV_LOG_ERROR
, "Expected 7 arguments, but %d found in '%s'\n", n
, args
);
173 return AVERROR(EINVAL
);
175 if ((c
->pix_fmt
= av_get_pix_fmt(pix_fmt_str
)) == AV_PIX_FMT_NONE
) {
177 c
->pix_fmt
= strtol(pix_fmt_str
, &tail
, 10);
178 if (*tail
|| c
->pix_fmt
< 0 || c
->pix_fmt
>= AV_PIX_FMT_NB
) {
179 av_log(ctx
, AV_LOG_ERROR
, "Invalid pixel format string '%s'\n", pix_fmt_str
);
180 return AVERROR(EINVAL
);
184 if (!(c
->fifo
= av_fifo_alloc(sizeof(AVFilterBufferRef
*))))
185 return AVERROR(ENOMEM
);
187 av_log(ctx
, AV_LOG_VERBOSE
, "w:%d h:%d pixfmt:%s\n", c
->w
, c
->h
, av_get_pix_fmt_name(c
->pix_fmt
));
191 #define OFFSET(x) offsetof(BufferSourceContext, x)
192 #define A AV_OPT_FLAG_AUDIO_PARAM
193 static const AVOption audio_options
[] = {
194 { "time_base", NULL
, OFFSET(time_base
), AV_OPT_TYPE_RATIONAL
, { .dbl
= 0 }, 0, INT_MAX
, A
},
195 { "sample_rate", NULL
, OFFSET(sample_rate
), AV_OPT_TYPE_INT
, { .i64
= 0 }, 0, INT_MAX
, A
},
196 { "sample_fmt", NULL
, OFFSET(sample_fmt_str
), AV_OPT_TYPE_STRING
, .flags
= A
},
197 { "channel_layout", NULL
, OFFSET(channel_layout_str
), AV_OPT_TYPE_STRING
, .flags
= A
},
201 static const AVClass abuffer_class
= {
202 .class_name
= "abuffer source",
203 .item_name
= av_default_item_name
,
204 .option
= audio_options
,
205 .version
= LIBAVUTIL_VERSION_INT
,
208 static av_cold
int init_audio(AVFilterContext
*ctx
, const char *args
)
210 BufferSourceContext
*s
= ctx
->priv
;
213 s
->class = &abuffer_class
;
214 av_opt_set_defaults(s
);
216 if ((ret
= av_set_options_string(s
, args
, "=", ":")) < 0) {
217 av_log(ctx
, AV_LOG_ERROR
, "Error parsing options string: %s.\n", args
);
221 s
->sample_fmt
= av_get_sample_fmt(s
->sample_fmt_str
);
222 if (s
->sample_fmt
== AV_SAMPLE_FMT_NONE
) {
223 av_log(ctx
, AV_LOG_ERROR
, "Invalid sample format %s.\n",
225 ret
= AVERROR(EINVAL
);
229 s
->channel_layout
= av_get_channel_layout(s
->channel_layout_str
);
230 if (!s
->channel_layout
) {
231 av_log(ctx
, AV_LOG_ERROR
, "Invalid channel layout %s.\n",
232 s
->channel_layout_str
);
233 ret
= AVERROR(EINVAL
);
237 if (!(s
->fifo
= av_fifo_alloc(sizeof(AVFilterBufferRef
*)))) {
238 ret
= AVERROR(ENOMEM
);
242 if (!s
->time_base
.num
)
243 s
->time_base
= (AVRational
){1, s
->sample_rate
};
245 av_log(ctx
, AV_LOG_VERBOSE
, "tb:%d/%d samplefmt:%s samplerate: %d "
246 "ch layout:%s\n", s
->time_base
.num
, s
->time_base
.den
, s
->sample_fmt_str
,
247 s
->sample_rate
, s
->channel_layout_str
);
254 static av_cold
void uninit(AVFilterContext
*ctx
)
256 BufferSourceContext
*s
= ctx
->priv
;
257 while (s
->fifo
&& av_fifo_size(s
->fifo
)) {
258 AVFilterBufferRef
*buf
;
259 av_fifo_generic_read(s
->fifo
, &buf
, sizeof(buf
), NULL
);
260 avfilter_unref_buffer(buf
);
262 av_fifo_free(s
->fifo
);
266 static int query_formats(AVFilterContext
*ctx
)
268 BufferSourceContext
*c
= ctx
->priv
;
269 AVFilterChannelLayouts
*channel_layouts
= NULL
;
270 AVFilterFormats
*formats
= NULL
;
271 AVFilterFormats
*samplerates
= NULL
;
273 switch (ctx
->outputs
[0]->type
) {
274 case AVMEDIA_TYPE_VIDEO
:
275 ff_add_format(&formats
, c
->pix_fmt
);
276 ff_set_common_formats(ctx
, formats
);
278 case AVMEDIA_TYPE_AUDIO
:
279 ff_add_format(&formats
, c
->sample_fmt
);
280 ff_set_common_formats(ctx
, formats
);
282 ff_add_format(&samplerates
, c
->sample_rate
);
283 ff_set_common_samplerates(ctx
, samplerates
);
285 ff_add_channel_layout(&channel_layouts
, c
->channel_layout
);
286 ff_set_common_channel_layouts(ctx
, channel_layouts
);
289 return AVERROR(EINVAL
);
295 static int config_props(AVFilterLink
*link
)
297 BufferSourceContext
*c
= link
->src
->priv
;
299 switch (link
->type
) {
300 case AVMEDIA_TYPE_VIDEO
:
303 link
->sample_aspect_ratio
= c
->pixel_aspect
;
305 case AVMEDIA_TYPE_AUDIO
:
306 link
->channel_layout
= c
->channel_layout
;
307 link
->sample_rate
= c
->sample_rate
;
310 return AVERROR(EINVAL
);
313 link
->time_base
= c
->time_base
;
317 static int request_frame(AVFilterLink
*link
)
319 BufferSourceContext
*c
= link
->src
->priv
;
320 AVFilterBufferRef
*buf
;
323 if (!av_fifo_size(c
->fifo
)) {
326 return AVERROR(EAGAIN
);
328 av_fifo_generic_read(c
->fifo
, &buf
, sizeof(buf
), NULL
);
330 ff_filter_frame(link
, buf
);
335 static int poll_frame(AVFilterLink
*link
)
337 BufferSourceContext
*c
= link
->src
->priv
;
338 int size
= av_fifo_size(c
->fifo
);
341 return size
/sizeof(AVFilterBufferRef
*);
344 static const AVFilterPad avfilter_vsrc_buffer_outputs
[] = {
347 .type
= AVMEDIA_TYPE_VIDEO
,
348 .request_frame
= request_frame
,
349 .poll_frame
= poll_frame
,
350 .config_props
= config_props
,
355 AVFilter avfilter_vsrc_buffer
= {
357 .description
= NULL_IF_CONFIG_SMALL("Buffer video frames, and make them accessible to the filterchain."),
358 .priv_size
= sizeof(BufferSourceContext
),
359 .query_formats
= query_formats
,
365 .outputs
= avfilter_vsrc_buffer_outputs
,
368 static const AVFilterPad avfilter_asrc_abuffer_outputs
[] = {
371 .type
= AVMEDIA_TYPE_AUDIO
,
372 .request_frame
= request_frame
,
373 .poll_frame
= poll_frame
,
374 .config_props
= config_props
,
379 AVFilter avfilter_asrc_abuffer
= {
381 .description
= NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them accessible to the filterchain."),
382 .priv_size
= sizeof(BufferSourceContext
),
383 .query_formats
= query_formats
,
389 .outputs
= avfilter_asrc_abuffer_outputs
,