a9b893c1aa652ad5a7d09e0a5c6267c440697699
[libav.git] / libavfilter / buffersrc.c
1 /*
2 * Copyright (c) 2008 Vitor Sessak
3 *
4 * This file is part of Libav.
5 *
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.
10 *
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.
15 *
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
19 */
20
21 /**
22 * @file
23 * memory buffer source filter
24 */
25
26 #include <float.h>
27
28 #include "libavutil/channel_layout.h"
29 #include "libavutil/common.h"
30 #include "libavutil/fifo.h"
31 #include "libavutil/frame.h"
32 #include "libavutil/imgutils.h"
33 #include "libavutil/internal.h"
34 #include "libavutil/opt.h"
35 #include "libavutil/samplefmt.h"
36 #include "audio.h"
37 #include "avfilter.h"
38 #include "buffersrc.h"
39 #include "formats.h"
40 #include "internal.h"
41 #include "video.h"
42
43 typedef struct BufferSourceContext {
44 const AVClass *class;
45 AVFifoBuffer *fifo;
46 AVRational time_base; ///< time_base to set in the output link
47
48 /* video only */
49 int h, w;
50 enum AVPixelFormat pix_fmt;
51 char *pix_fmt_str;
52 AVRational pixel_aspect;
53
54 /* audio only */
55 int sample_rate;
56 enum AVSampleFormat sample_fmt;
57 char *sample_fmt_str;
58 uint64_t channel_layout;
59 char *channel_layout_str;
60
61 int eof;
62 } BufferSourceContext;
63
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);\
68 }
69
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);\
75 }
76
77 int attribute_align_arg av_buffersrc_write_frame(AVFilterContext *ctx, const AVFrame *frame)
78 {
79 AVFrame *copy;
80 int ret = 0;
81
82 if (!(copy = av_frame_alloc()))
83 return AVERROR(ENOMEM);
84 ret = av_frame_ref(copy, frame);
85 if (ret >= 0)
86 ret = av_buffersrc_add_frame(ctx, copy);
87
88 av_frame_free(&copy);
89 return ret;
90 }
91
92 int attribute_align_arg av_buffersrc_add_frame(AVFilterContext *ctx,
93 AVFrame *frame)
94 {
95 BufferSourceContext *s = ctx->priv;
96 AVFrame *copy;
97 int refcounted, ret;
98
99 if (!frame) {
100 s->eof = 1;
101 return 0;
102 } else if (s->eof)
103 return AVERROR(EINVAL);
104
105 refcounted = !!frame->buf[0];
106
107 switch (ctx->outputs[0]->type) {
108 case AVMEDIA_TYPE_VIDEO:
109 CHECK_VIDEO_PARAM_CHANGE(ctx, s, frame->width, frame->height,
110 frame->format);
111 break;
112 case AVMEDIA_TYPE_AUDIO:
113 CHECK_AUDIO_PARAM_CHANGE(ctx, s, frame->sample_rate, frame->channel_layout,
114 frame->format);
115 break;
116 default:
117 return AVERROR(EINVAL);
118 }
119
120 if (!av_fifo_space(s->fifo) &&
121 (ret = av_fifo_realloc2(s->fifo, av_fifo_size(s->fifo) +
122 sizeof(copy))) < 0)
123 return ret;
124
125 if (!(copy = av_frame_alloc()))
126 return AVERROR(ENOMEM);
127
128 if (refcounted) {
129 av_frame_move_ref(copy, frame);
130 } else {
131 ret = av_frame_ref(copy, frame);
132 if (ret < 0) {
133 av_frame_free(&copy);
134 return ret;
135 }
136 }
137
138 if ((ret = av_fifo_generic_write(s->fifo, &copy, sizeof(copy), NULL)) < 0) {
139 if (refcounted)
140 av_frame_move_ref(frame, copy);
141 av_frame_free(&copy);
142 return ret;
143 }
144
145 return 0;
146 }
147
148 static av_cold int init_video(AVFilterContext *ctx)
149 {
150 BufferSourceContext *c = ctx->priv;
151
152 if (!c->pix_fmt_str || !c->w || !c->h || av_q2d(c->time_base) <= 0) {
153 av_log(ctx, AV_LOG_ERROR, "Invalid parameters provided.\n");
154 return AVERROR(EINVAL);
155 }
156
157 if ((c->pix_fmt = av_get_pix_fmt(c->pix_fmt_str)) == AV_PIX_FMT_NONE) {
158 char *tail;
159 c->pix_fmt = strtol(c->pix_fmt_str, &tail, 10);
160 if (*tail || c->pix_fmt < 0 || !av_pix_fmt_desc_get(c->pix_fmt)) {
161 av_log(ctx, AV_LOG_ERROR, "Invalid pixel format string '%s'\n", c->pix_fmt_str);
162 return AVERROR(EINVAL);
163 }
164 }
165
166 if (!(c->fifo = av_fifo_alloc(sizeof(AVFrame*))))
167 return AVERROR(ENOMEM);
168
169 av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d pixfmt:%s tb:%d/%d sar:%d/%d\n",
170 c->w, c->h, av_get_pix_fmt_name(c->pix_fmt),
171 c->time_base.num, c->time_base.den,
172 c->pixel_aspect.num, c->pixel_aspect.den);
173 return 0;
174 }
175
176 #define OFFSET(x) offsetof(BufferSourceContext, x)
177 #define A AV_OPT_FLAG_AUDIO_PARAM
178 #define V AV_OPT_FLAG_VIDEO_PARAM
179
180 static const AVOption video_options[] = {
181 { "width", NULL, OFFSET(w), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
182 { "height", NULL, OFFSET(h), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
183 { "pix_fmt", NULL, OFFSET(pix_fmt_str), AV_OPT_TYPE_STRING, .flags = V },
184 #if FF_API_OLD_FILTER_OPTS
185 /* those 4 are for compatibility with the old option passing system where each filter
186 * did its own parsing */
187 { "time_base_num", "deprecated, do not use", OFFSET(time_base.num), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
188 { "time_base_den", "deprecated, do not use", OFFSET(time_base.den), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
189 { "sar_num", "deprecated, do not use", OFFSET(pixel_aspect.num), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
190 { "sar_den", "deprecated, do not use", OFFSET(pixel_aspect.den), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
191 #endif
192 { "sar", "sample aspect ratio", OFFSET(pixel_aspect), AV_OPT_TYPE_RATIONAL, { .dbl = 1 }, 0, DBL_MAX, V },
193 { "time_base", NULL, OFFSET(time_base), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, DBL_MAX, V },
194 { NULL },
195 };
196
197 static const AVClass buffer_class = {
198 .class_name = "buffer source",
199 .item_name = av_default_item_name,
200 .option = video_options,
201 .version = LIBAVUTIL_VERSION_INT,
202 };
203
204 static const AVOption audio_options[] = {
205 { "time_base", NULL, OFFSET(time_base), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, INT_MAX, A },
206 { "sample_rate", NULL, OFFSET(sample_rate), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, A },
207 { "sample_fmt", NULL, OFFSET(sample_fmt_str), AV_OPT_TYPE_STRING, .flags = A },
208 { "channel_layout", NULL, OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, .flags = A },
209 { NULL },
210 };
211
212 static const AVClass abuffer_class = {
213 .class_name = "abuffer source",
214 .item_name = av_default_item_name,
215 .option = audio_options,
216 .version = LIBAVUTIL_VERSION_INT,
217 };
218
219 static av_cold int init_audio(AVFilterContext *ctx)
220 {
221 BufferSourceContext *s = ctx->priv;
222 int ret = 0;
223
224 s->sample_fmt = av_get_sample_fmt(s->sample_fmt_str);
225 if (s->sample_fmt == AV_SAMPLE_FMT_NONE) {
226 av_log(ctx, AV_LOG_ERROR, "Invalid sample format %s.\n",
227 s->sample_fmt_str);
228 return AVERROR(EINVAL);
229 }
230
231 s->channel_layout = av_get_channel_layout(s->channel_layout_str);
232 if (!s->channel_layout) {
233 av_log(ctx, AV_LOG_ERROR, "Invalid channel layout %s.\n",
234 s->channel_layout_str);
235 return AVERROR(EINVAL);
236 }
237
238 if (!(s->fifo = av_fifo_alloc(sizeof(AVFrame*))))
239 return AVERROR(ENOMEM);
240
241 if (!s->time_base.num)
242 s->time_base = (AVRational){1, s->sample_rate};
243
244 av_log(ctx, AV_LOG_VERBOSE, "tb:%d/%d samplefmt:%s samplerate: %d "
245 "ch layout:%s\n", s->time_base.num, s->time_base.den, s->sample_fmt_str,
246 s->sample_rate, s->channel_layout_str);
247
248 return ret;
249 }
250
251 static av_cold void uninit(AVFilterContext *ctx)
252 {
253 BufferSourceContext *s = ctx->priv;
254 while (s->fifo && av_fifo_size(s->fifo)) {
255 AVFrame *frame;
256 av_fifo_generic_read(s->fifo, &frame, sizeof(frame), NULL);
257 av_frame_free(&frame);
258 }
259 av_fifo_free(s->fifo);
260 s->fifo = NULL;
261 }
262
263 static int query_formats(AVFilterContext *ctx)
264 {
265 BufferSourceContext *c = ctx->priv;
266 AVFilterChannelLayouts *channel_layouts = NULL;
267 AVFilterFormats *formats = NULL;
268 AVFilterFormats *samplerates = NULL;
269
270 switch (ctx->outputs[0]->type) {
271 case AVMEDIA_TYPE_VIDEO:
272 ff_add_format(&formats, c->pix_fmt);
273 ff_set_common_formats(ctx, formats);
274 break;
275 case AVMEDIA_TYPE_AUDIO:
276 ff_add_format(&formats, c->sample_fmt);
277 ff_set_common_formats(ctx, formats);
278
279 ff_add_format(&samplerates, c->sample_rate);
280 ff_set_common_samplerates(ctx, samplerates);
281
282 ff_add_channel_layout(&channel_layouts, c->channel_layout);
283 ff_set_common_channel_layouts(ctx, channel_layouts);
284 break;
285 default:
286 return AVERROR(EINVAL);
287 }
288
289 return 0;
290 }
291
292 static int config_props(AVFilterLink *link)
293 {
294 BufferSourceContext *c = link->src->priv;
295
296 switch (link->type) {
297 case AVMEDIA_TYPE_VIDEO:
298 link->w = c->w;
299 link->h = c->h;
300 link->sample_aspect_ratio = c->pixel_aspect;
301 break;
302 case AVMEDIA_TYPE_AUDIO:
303 link->channel_layout = c->channel_layout;
304 link->sample_rate = c->sample_rate;
305 break;
306 default:
307 return AVERROR(EINVAL);
308 }
309
310 link->time_base = c->time_base;
311 return 0;
312 }
313
314 static int request_frame(AVFilterLink *link)
315 {
316 BufferSourceContext *c = link->src->priv;
317 AVFrame *frame;
318 int ret = 0;
319
320 if (!av_fifo_size(c->fifo)) {
321 if (c->eof)
322 return AVERROR_EOF;
323 return AVERROR(EAGAIN);
324 }
325 av_fifo_generic_read(c->fifo, &frame, sizeof(frame), NULL);
326
327 ff_filter_frame(link, frame);
328
329 return ret;
330 }
331
332 static int poll_frame(AVFilterLink *link)
333 {
334 BufferSourceContext *c = link->src->priv;
335 int size = av_fifo_size(c->fifo);
336 if (!size && c->eof)
337 return AVERROR_EOF;
338 return size/sizeof(AVFrame*);
339 }
340
341 static const AVFilterPad avfilter_vsrc_buffer_outputs[] = {
342 {
343 .name = "default",
344 .type = AVMEDIA_TYPE_VIDEO,
345 .request_frame = request_frame,
346 .poll_frame = poll_frame,
347 .config_props = config_props,
348 },
349 { NULL }
350 };
351
352 AVFilter ff_vsrc_buffer = {
353 .name = "buffer",
354 .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them accessible to the filterchain."),
355 .priv_size = sizeof(BufferSourceContext),
356 .priv_class = &buffer_class,
357 .query_formats = query_formats,
358
359 .init = init_video,
360 .uninit = uninit,
361
362 .inputs = NULL,
363 .outputs = avfilter_vsrc_buffer_outputs,
364 };
365
366 static const AVFilterPad avfilter_asrc_abuffer_outputs[] = {
367 {
368 .name = "default",
369 .type = AVMEDIA_TYPE_AUDIO,
370 .request_frame = request_frame,
371 .poll_frame = poll_frame,
372 .config_props = config_props,
373 },
374 { NULL }
375 };
376
377 AVFilter ff_asrc_abuffer = {
378 .name = "abuffer",
379 .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them accessible to the filterchain."),
380 .priv_size = sizeof(BufferSourceContext),
381 .priv_class = &abuffer_class,
382 .query_formats = query_formats,
383
384 .init = init_audio,
385 .uninit = uninit,
386
387 .inputs = NULL,
388 .outputs = avfilter_asrc_abuffer_outputs,
389 };