]> git.sesse.net Git - ffmpeg/blob - libavfilter/buffersrc.c
movenc: Don't write the 'wave' atom or its child 'enda' for lpcm audio.
[ffmpeg] / 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 "audio.h"
27 #include "avfilter.h"
28 #include "buffersrc.h"
29 #include "formats.h"
30 #include "vsrc_buffer.h"
31
32 #include "libavutil/audioconvert.h"
33 #include "libavutil/fifo.h"
34 #include "libavutil/imgutils.h"
35 #include "libavutil/opt.h"
36 #include "libavutil/samplefmt.h"
37
38 typedef struct {
39     const AVClass    *class;
40     AVFifoBuffer     *fifo;
41     AVRational        time_base;     ///< time_base to set in the output link
42
43     /* video only */
44     int               h, w;
45     enum PixelFormat  pix_fmt;
46     AVRational        pixel_aspect;
47
48     /* audio only */
49     int sample_rate;
50     enum AVSampleFormat sample_fmt;
51     char               *sample_fmt_str;
52     uint64_t channel_layout;
53     char    *channel_layout_str;
54
55     int eof;
56 } BufferSourceContext;
57
58 #define CHECK_VIDEO_PARAM_CHANGE(s, c, width, height, format)\
59     if (c->w != width || c->h != height || c->pix_fmt != format) {\
60         av_log(s, AV_LOG_ERROR, "Changing frame properties on the fly is not supported.\n");\
61         return AVERROR(EINVAL);\
62     }
63
64 #define CHECK_AUDIO_PARAM_CHANGE(s, c, srate, ch_layout, format)\
65     if (c->sample_fmt != format || c->sample_rate != srate ||\
66         c->channel_layout != ch_layout) {\
67         av_log(s, AV_LOG_ERROR, "Changing frame properties on the fly is not supported.\n");\
68         return AVERROR(EINVAL);\
69     }
70
71 #if FF_API_VSRC_BUFFER_ADD_FRAME
72 int av_vsrc_buffer_add_frame(AVFilterContext *buffer_filter, AVFrame *frame,
73                              int64_t pts, AVRational pixel_aspect)
74 {
75     int64_t orig_pts = frame->pts;
76     AVRational orig_sar = frame->sample_aspect_ratio;
77     int ret;
78
79     frame->pts = pts;
80     frame->sample_aspect_ratio = pixel_aspect;
81     if ((ret = av_buffersrc_write_frame(buffer_filter, frame)) < 0)
82         return ret;
83     frame->pts = orig_pts;
84     frame->sample_aspect_ratio = orig_sar;
85
86     return 0;
87 }
88 #endif
89
90 int av_buffersrc_write_frame(AVFilterContext *buffer_filter, AVFrame *frame)
91 {
92     BufferSourceContext *c = buffer_filter->priv;
93     AVFilterBufferRef *buf;
94     int ret;
95
96     if (!frame) {
97         c->eof = 1;
98         return 0;
99     } else if (c->eof)
100         return AVERROR(EINVAL);
101
102     if (!av_fifo_space(c->fifo) &&
103         (ret = av_fifo_realloc2(c->fifo, av_fifo_size(c->fifo) +
104                                          sizeof(buf))) < 0)
105         return ret;
106
107     switch (buffer_filter->outputs[0]->type) {
108     case AVMEDIA_TYPE_VIDEO:
109         CHECK_VIDEO_PARAM_CHANGE(buffer_filter, c, frame->width, frame->height,
110                                  frame->format);
111         buf = avfilter_get_video_buffer(buffer_filter->outputs[0], AV_PERM_WRITE,
112                                         c->w, c->h);
113         av_image_copy(buf->data, buf->linesize, frame->data, frame->linesize,
114                       c->pix_fmt, c->w, c->h);
115         break;
116     case AVMEDIA_TYPE_AUDIO:
117         CHECK_AUDIO_PARAM_CHANGE(buffer_filter, c, frame->sample_rate, frame->channel_layout,
118                                  frame->format);
119         buf = ff_get_audio_buffer(buffer_filter->outputs[0], AV_PERM_WRITE,
120                                   frame->nb_samples);
121         av_samples_copy(buf->extended_data, frame->extended_data,
122                         0, 0, frame->nb_samples,
123                         av_get_channel_layout_nb_channels(frame->channel_layout),
124                         frame->format);
125         break;
126     default:
127         return AVERROR(EINVAL);
128     }
129
130     avfilter_copy_frame_props(buf, frame);
131
132     if ((ret = av_fifo_generic_write(c->fifo, &buf, sizeof(buf), NULL)) < 0) {
133         avfilter_unref_buffer(buf);
134         return ret;
135     }
136
137     return 0;
138 }
139
140 int av_buffersrc_buffer(AVFilterContext *s, AVFilterBufferRef *buf)
141 {
142     BufferSourceContext *c = s->priv;
143     int ret;
144
145     if (!buf) {
146         c->eof = 1;
147         return 0;
148     } else if (c->eof)
149         return AVERROR(EINVAL);
150
151     if (!av_fifo_space(c->fifo) &&
152         (ret = av_fifo_realloc2(c->fifo, av_fifo_size(c->fifo) +
153                                          sizeof(buf))) < 0)
154         return ret;
155
156     switch (s->outputs[0]->type) {
157     case AVMEDIA_TYPE_VIDEO:
158         CHECK_VIDEO_PARAM_CHANGE(s, c, buf->video->w, buf->video->h, buf->format);
159         break;
160     case AVMEDIA_TYPE_AUDIO:
161         CHECK_AUDIO_PARAM_CHANGE(s, c, buf->audio->sample_rate, buf->audio->channel_layout,
162                                  buf->format);
163         break;
164     default:
165         return AVERROR(EINVAL);
166     }
167
168     if ((ret = av_fifo_generic_write(c->fifo, &buf, sizeof(buf), NULL)) < 0)
169         return ret;
170
171     return 0;
172 }
173
174 static av_cold int init_video(AVFilterContext *ctx, const char *args, void *opaque)
175 {
176     BufferSourceContext *c = ctx->priv;
177     char pix_fmt_str[128];
178     int n = 0;
179
180     if (!args ||
181         (n = sscanf(args, "%d:%d:%127[^:]:%d:%d:%d:%d", &c->w, &c->h, pix_fmt_str,
182                     &c->time_base.num, &c->time_base.den,
183                     &c->pixel_aspect.num, &c->pixel_aspect.den)) != 7) {
184         av_log(ctx, AV_LOG_ERROR, "Expected 7 arguments, but %d found in '%s'\n", n, args);
185         return AVERROR(EINVAL);
186     }
187     if ((c->pix_fmt = av_get_pix_fmt(pix_fmt_str)) == PIX_FMT_NONE) {
188         char *tail;
189         c->pix_fmt = strtol(pix_fmt_str, &tail, 10);
190         if (*tail || c->pix_fmt < 0 || c->pix_fmt >= PIX_FMT_NB) {
191             av_log(ctx, AV_LOG_ERROR, "Invalid pixel format string '%s'\n", pix_fmt_str);
192             return AVERROR(EINVAL);
193         }
194     }
195
196     if (!(c->fifo = av_fifo_alloc(sizeof(AVFilterBufferRef*))))
197         return AVERROR(ENOMEM);
198
199     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);
200     return 0;
201 }
202
203 #define OFFSET(x) offsetof(BufferSourceContext, x)
204 #define A AV_OPT_FLAG_AUDIO_PARAM
205 static const AVOption audio_options[] = {
206     { "time_base",      NULL, OFFSET(time_base),           AV_OPT_TYPE_RATIONAL, { 0 }, 0, INT_MAX, A },
207     { "sample_rate",    NULL, OFFSET(sample_rate),         AV_OPT_TYPE_INT,      { 0 }, 0, INT_MAX, A },
208     { "sample_fmt",     NULL, OFFSET(sample_fmt_str),      AV_OPT_TYPE_STRING,             .flags = A },
209     { "channel_layout", NULL, OFFSET(channel_layout_str),  AV_OPT_TYPE_STRING,             .flags = A },
210     { NULL },
211 };
212
213 static const AVClass abuffer_class = {
214     .class_name = "abuffer source",
215     .item_name  = av_default_item_name,
216     .option     = audio_options,
217     .version    = LIBAVUTIL_VERSION_INT,
218 };
219
220 static av_cold int init_audio(AVFilterContext *ctx, const char *args, void *opaque)
221 {
222     BufferSourceContext *s = ctx->priv;
223     int ret = 0;
224
225     s->class = &abuffer_class;
226     av_opt_set_defaults(s);
227
228     if ((ret = av_set_options_string(s, args, "=", ":")) < 0) {
229         av_log(ctx, AV_LOG_ERROR, "Error parsing options string: %s.\n", args);
230         goto fail;
231     }
232
233     s->sample_fmt = av_get_sample_fmt(s->sample_fmt_str);
234     if (s->sample_fmt == AV_SAMPLE_FMT_NONE) {
235         av_log(ctx, AV_LOG_ERROR, "Invalid sample format %s.\n",
236                s->sample_fmt_str);
237         ret = AVERROR(EINVAL);
238         goto fail;
239     }
240
241     s->channel_layout = av_get_channel_layout(s->channel_layout_str);
242     if (!s->channel_layout) {
243         av_log(ctx, AV_LOG_ERROR, "Invalid channel layout %s.\n",
244                s->channel_layout_str);
245         ret = AVERROR(EINVAL);
246         goto fail;
247     }
248
249     if (!(s->fifo = av_fifo_alloc(sizeof(AVFilterBufferRef*)))) {
250         ret = AVERROR(ENOMEM);
251         goto fail;
252     }
253
254     if (!s->time_base.num)
255         s->time_base = (AVRational){1, s->sample_rate};
256
257     av_log(ctx, AV_LOG_VERBOSE, "tb:%d/%d samplefmt:%s samplerate: %d "
258            "ch layout:%s\n", s->time_base.num, s->time_base.den, s->sample_fmt_str,
259            s->sample_rate, s->channel_layout_str);
260
261 fail:
262     av_opt_free(s);
263     return ret;
264 }
265
266 static av_cold void uninit(AVFilterContext *ctx)
267 {
268     BufferSourceContext *s = ctx->priv;
269     while (s->fifo && av_fifo_size(s->fifo)) {
270         AVFilterBufferRef *buf;
271         av_fifo_generic_read(s->fifo, &buf, sizeof(buf), NULL);
272         avfilter_unref_buffer(buf);
273     }
274     av_fifo_free(s->fifo);
275     s->fifo = NULL;
276 }
277
278 static int query_formats(AVFilterContext *ctx)
279 {
280     BufferSourceContext *c = ctx->priv;
281     AVFilterChannelLayouts *channel_layouts = NULL;
282     AVFilterFormats *formats = NULL;
283     AVFilterFormats *samplerates = NULL;
284
285     switch (ctx->outputs[0]->type) {
286     case AVMEDIA_TYPE_VIDEO:
287         avfilter_add_format(&formats, c->pix_fmt);
288         avfilter_set_common_formats(ctx, formats);
289         break;
290     case AVMEDIA_TYPE_AUDIO:
291         avfilter_add_format(&formats,           c->sample_fmt);
292         avfilter_set_common_formats(ctx, formats);
293
294         avfilter_add_format(&samplerates,       c->sample_rate);
295         ff_set_common_samplerates(ctx, samplerates);
296
297         ff_add_channel_layout(&channel_layouts, c->channel_layout);
298         ff_set_common_channel_layouts(ctx, channel_layouts);
299         break;
300     default:
301         return AVERROR(EINVAL);
302     }
303
304     return 0;
305 }
306
307 static int config_props(AVFilterLink *link)
308 {
309     BufferSourceContext *c = link->src->priv;
310
311     switch (link->type) {
312     case AVMEDIA_TYPE_VIDEO:
313         link->w = c->w;
314         link->h = c->h;
315         link->sample_aspect_ratio = c->pixel_aspect;
316         break;
317     case AVMEDIA_TYPE_AUDIO:
318         link->channel_layout = c->channel_layout;
319         link->sample_rate    = c->sample_rate;
320         break;
321     default:
322         return AVERROR(EINVAL);
323     }
324
325     link->time_base = c->time_base;
326     return 0;
327 }
328
329 static int request_frame(AVFilterLink *link)
330 {
331     BufferSourceContext *c = link->src->priv;
332     AVFilterBufferRef *buf;
333
334     if (!av_fifo_size(c->fifo)) {
335         if (c->eof)
336             return AVERROR_EOF;
337         return AVERROR(EAGAIN);
338     }
339     av_fifo_generic_read(c->fifo, &buf, sizeof(buf), NULL);
340
341     switch (link->type) {
342     case AVMEDIA_TYPE_VIDEO:
343         avfilter_start_frame(link, avfilter_ref_buffer(buf, ~0));
344         avfilter_draw_slice(link, 0, link->h, 1);
345         avfilter_end_frame(link);
346         break;
347     case AVMEDIA_TYPE_AUDIO:
348         ff_filter_samples(link, avfilter_ref_buffer(buf, ~0));
349         break;
350     default:
351         return AVERROR(EINVAL);
352     }
353
354     avfilter_unref_buffer(buf);
355
356     return 0;
357 }
358
359 static int poll_frame(AVFilterLink *link)
360 {
361     BufferSourceContext *c = link->src->priv;
362     int size = av_fifo_size(c->fifo);
363     if (!size && c->eof)
364         return AVERROR_EOF;
365     return size/sizeof(AVFilterBufferRef*);
366 }
367
368 AVFilter avfilter_vsrc_buffer = {
369     .name      = "buffer",
370     .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them accessible to the filterchain."),
371     .priv_size = sizeof(BufferSourceContext),
372     .query_formats = query_formats,
373
374     .init      = init_video,
375     .uninit    = uninit,
376
377     .inputs    = (AVFilterPad[]) {{ .name = NULL }},
378     .outputs   = (AVFilterPad[]) {{ .name            = "default",
379                                     .type            = AVMEDIA_TYPE_VIDEO,
380                                     .request_frame   = request_frame,
381                                     .poll_frame      = poll_frame,
382                                     .config_props    = config_props, },
383                                   { .name = NULL}},
384 };
385
386 AVFilter avfilter_asrc_abuffer = {
387     .name          = "abuffer",
388     .description   = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them accessible to the filterchain."),
389     .priv_size     = sizeof(BufferSourceContext),
390     .query_formats = query_formats,
391
392     .init      = init_audio,
393     .uninit    = uninit,
394
395     .inputs    = (AVFilterPad[]) {{ .name = NULL }},
396     .outputs   = (AVFilterPad[]) {{ .name            = "default",
397                                     .type            = AVMEDIA_TYPE_AUDIO,
398                                     .request_frame   = request_frame,
399                                     .poll_frame      = poll_frame,
400                                     .config_props    = config_props, },
401                                   { .name = NULL}},
402 };