h264_metadata: Add option to delete filler data
[libav.git] / libavfilter / buffersrc.c
CommitLineData
cf13f204 1/*
cf13f204
MN
2 * Copyright (c) 2008 Vitor Sessak
3 *
2912e87a 4 * This file is part of Libav.
cf13f204 5 *
2912e87a 6 * Libav is free software; you can redistribute it and/or
cf13f204
MN
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 *
2912e87a 11 * Libav is distributed in the hope that it will be useful,
cf13f204
MN
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
2912e87a 17 * License along with Libav; if not, write to the Free Software
cf13f204
MN
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
b5634e45
SS
21/**
22 * @file
23 * memory buffer source filter
24 */
25
d28cb849
AK
26#include <float.h>
27
a903f8f0
JR
28#include "libavutil/channel_layout.h"
29#include "libavutil/common.h"
30#include "libavutil/fifo.h"
7e350379 31#include "libavutil/frame.h"
a903f8f0 32#include "libavutil/imgutils.h"
7950e519 33#include "libavutil/internal.h"
a903f8f0
JR
34#include "libavutil/opt.h"
35#include "libavutil/samplefmt.h"
4c66c407 36#include "audio.h"
cf13f204 37#include "avfilter.h"
e1d9dbf2 38#include "buffersrc.h"
4c66c407 39#include "formats.h"
9d0bfc50 40#include "internal.h"
803391f7 41#include "video.h"
4c66c407 42
58400ac1 43typedef struct BufferSourceContext {
4c66c407 44 const AVClass *class;
95587d29 45 AVFifoBuffer *fifo;
4c66c407 46 AVRational time_base; ///< time_base to set in the output link
61fb67dc 47 AVRational frame_rate; ///< frame_rate to set in the output link
4c66c407
AK
48
49 /* video only */
9164afcb 50 int h, w;
716d413c 51 enum AVPixelFormat pix_fmt;
d28cb849 52 char *pix_fmt_str;
cf13f204 53 AVRational pixel_aspect;
4c66c407 54
b3dd30db
AK
55 AVBufferRef *hw_frames_ctx;
56
4c66c407
AK
57 /* audio only */
58 int sample_rate;
59 enum AVSampleFormat sample_fmt;
60 char *sample_fmt_str;
61 uint64_t channel_layout;
62 char *channel_layout_str;
63
b3dd30db 64 int got_format_from_params;
7ae7c414 65 int eof;
cf13f204
MN
66} BufferSourceContext;
67
4c66c407 68#define CHECK_VIDEO_PARAM_CHANGE(s, c, width, height, format)\
5d25140f
AK
69 if (c->w != width || c->h != height || c->pix_fmt != format) {\
70 av_log(s, AV_LOG_ERROR, "Changing frame properties on the fly is not supported.\n");\
71 return AVERROR(EINVAL);\
72 }
73
4c66c407
AK
74#define CHECK_AUDIO_PARAM_CHANGE(s, c, srate, ch_layout, format)\
75 if (c->sample_fmt != format || c->sample_rate != srate ||\
76 c->channel_layout != ch_layout) {\
77 av_log(s, AV_LOG_ERROR, "Changing frame properties on the fly is not supported.\n");\
78 return AVERROR(EINVAL);\
79 }
80
b3dd30db
AK
81AVBufferSrcParameters *av_buffersrc_parameters_alloc(void)
82{
83 AVBufferSrcParameters *par = av_mallocz(sizeof(*par));
84 if (!par)
85 return NULL;
86
87 par->format = -1;
88
89 return par;
90}
91
92int av_buffersrc_parameters_set(AVFilterContext *ctx, AVBufferSrcParameters *param)
93{
94 BufferSourceContext *s = ctx->priv;
95
96 if (param->time_base.num > 0 && param->time_base.den > 0)
97 s->time_base = param->time_base;
98
99 switch (ctx->filter->outputs[0].type) {
100 case AVMEDIA_TYPE_VIDEO:
101 if (param->format != AV_PIX_FMT_NONE) {
102 s->got_format_from_params = 1;
103 s->pix_fmt = param->format;
104 }
105 if (param->width > 0)
106 s->w = param->width;
107 if (param->height > 0)
108 s->h = param->height;
109 if (param->sample_aspect_ratio.num > 0 && param->sample_aspect_ratio.den > 0)
110 s->pixel_aspect = param->sample_aspect_ratio;
111 if (param->frame_rate.num > 0 && param->frame_rate.den > 0)
112 s->frame_rate = param->frame_rate;
113 if (param->hw_frames_ctx) {
114 av_buffer_unref(&s->hw_frames_ctx);
115 s->hw_frames_ctx = av_buffer_ref(param->hw_frames_ctx);
116 if (!s->hw_frames_ctx)
117 return AVERROR(ENOMEM);
118 }
119 break;
120 case AVMEDIA_TYPE_AUDIO:
121 if (param->format != AV_SAMPLE_FMT_NONE) {
122 s->got_format_from_params = 1;
123 s->sample_fmt = param->format;
124 }
125 if (param->sample_rate > 0)
126 s->sample_rate = param->sample_rate;
127 if (param->channel_layout)
128 s->channel_layout = param->channel_layout;
129 break;
130 default:
131 return AVERROR_BUG;
132 }
133
134 return 0;
135}
136
779e6c2b 137int attribute_align_arg av_buffersrc_write_frame(AVFilterContext *ctx, const AVFrame *frame)
720c6b78 138{
7e350379
AK
139 AVFrame *copy;
140 int ret = 0;
141
142 if (!(copy = av_frame_alloc()))
143 return AVERROR(ENOMEM);
144 ret = av_frame_ref(copy, frame);
145 if (ret >= 0)
146 ret = av_buffersrc_add_frame(ctx, copy);
147
148 av_frame_free(&copy);
149 return ret;
150}
151
20c86571
HL
152int attribute_align_arg av_buffersrc_add_frame(AVFilterContext *ctx,
153 AVFrame *frame)
7e350379
AK
154{
155 BufferSourceContext *s = ctx->priv;
156 AVFrame *copy;
104a97be 157 int refcounted, ret;
cf13f204 158
7bf9e339 159 if (!frame) {
7e350379 160 s->eof = 1;
7ae7c414 161 return 0;
7e350379 162 } else if (s->eof)
7ae7c414
AK
163 return AVERROR(EINVAL);
164
104a97be
AK
165 refcounted = !!frame->buf[0];
166
7e350379 167 switch (ctx->outputs[0]->type) {
4c66c407 168 case AVMEDIA_TYPE_VIDEO:
7e350379 169 CHECK_VIDEO_PARAM_CHANGE(ctx, s, frame->width, frame->height,
4c66c407 170 frame->format);
4c66c407
AK
171 break;
172 case AVMEDIA_TYPE_AUDIO:
7e350379 173 CHECK_AUDIO_PARAM_CHANGE(ctx, s, frame->sample_rate, frame->channel_layout,
4c66c407 174 frame->format);
4c66c407
AK
175 break;
176 default:
177 return AVERROR(EINVAL);
178 }
e1d9dbf2 179
7e350379
AK
180 if (!av_fifo_space(s->fifo) &&
181 (ret = av_fifo_realloc2(s->fifo, av_fifo_size(s->fifo) +
182 sizeof(copy))) < 0)
183 return ret;
184
185 if (!(copy = av_frame_alloc()))
186 return AVERROR(ENOMEM);
104a97be
AK
187
188 if (refcounted) {
189 av_frame_move_ref(copy, frame);
190 } else {
191 ret = av_frame_ref(copy, frame);
192 if (ret < 0) {
193 av_frame_free(&copy);
194 return ret;
195 }
196 }
95587d29 197
7e350379 198 if ((ret = av_fifo_generic_write(s->fifo, &copy, sizeof(copy), NULL)) < 0) {
104a97be
AK
199 if (refcounted)
200 av_frame_move_ref(frame, copy);
7e350379 201 av_frame_free(&copy);
95587d29
AK
202 return ret;
203 }
e1d9dbf2
AK
204
205 return 0;
206}
207
c43a7eca 208static av_cold int init_video(AVFilterContext *ctx)
cf13f204
MN
209{
210 BufferSourceContext *c = ctx->priv;
d28cb849 211
b3dd30db
AK
212 if (!(c->pix_fmt_str || c->got_format_from_params) || !c->w || !c->h ||
213 av_q2d(c->time_base) <= 0) {
d28cb849 214 av_log(ctx, AV_LOG_ERROR, "Invalid parameters provided.\n");
b8dddebf
SS
215 return AVERROR(EINVAL);
216 }
d28cb849 217
b3dd30db
AK
218 if (c->pix_fmt_str) {
219 if ((c->pix_fmt = av_get_pix_fmt(c->pix_fmt_str)) == AV_PIX_FMT_NONE) {
220 char *tail;
221 c->pix_fmt = strtol(c->pix_fmt_str, &tail, 10);
222 if (*tail || c->pix_fmt < 0 || !av_pix_fmt_desc_get(c->pix_fmt)) {
223 av_log(ctx, AV_LOG_ERROR, "Invalid pixel format string '%s'\n", c->pix_fmt_str);
224 return AVERROR(EINVAL);
225 }
59775b3c
SS
226 }
227 }
cf13f204 228
7e350379 229 if (!(c->fifo = av_fifo_alloc(sizeof(AVFrame*))))
95587d29
AK
230 return AVERROR(ENOMEM);
231
dc54c78c
VG
232 av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d pixfmt:%s tb:%d/%d sar:%d/%d\n",
233 c->w, c->h, av_get_pix_fmt_name(c->pix_fmt),
234 c->time_base.num, c->time_base.den,
235 c->pixel_aspect.num, c->pixel_aspect.den);
b8dddebf 236 return 0;
cf13f204
MN
237}
238
4c66c407
AK
239#define OFFSET(x) offsetof(BufferSourceContext, x)
240#define A AV_OPT_FLAG_AUDIO_PARAM
d28cb849
AK
241#define V AV_OPT_FLAG_VIDEO_PARAM
242
243static const AVOption video_options[] = {
244 { "width", NULL, OFFSET(w), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
245 { "height", NULL, OFFSET(h), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
246 { "pix_fmt", NULL, OFFSET(pix_fmt_str), AV_OPT_TYPE_STRING, .flags = V },
c084d6d2 247 { "sar", "sample aspect ratio", OFFSET(pixel_aspect), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, DBL_MAX, V },
d28cb849 248 { "time_base", NULL, OFFSET(time_base), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, DBL_MAX, V },
61fb67dc 249 { "frame_rate", NULL, OFFSET(frame_rate), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, DBL_MAX, V },
d28cb849
AK
250 { NULL },
251};
252
253static const AVClass buffer_class = {
254 .class_name = "buffer source",
255 .item_name = av_default_item_name,
256 .option = video_options,
257 .version = LIBAVUTIL_VERSION_INT,
258};
259
4c66c407 260static const AVOption audio_options[] = {
21bc4403 261 { "time_base", NULL, OFFSET(time_base), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, INT_MAX, A },
e6153f17 262 { "sample_rate", NULL, OFFSET(sample_rate), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, A },
4c66c407
AK
263 { "sample_fmt", NULL, OFFSET(sample_fmt_str), AV_OPT_TYPE_STRING, .flags = A },
264 { "channel_layout", NULL, OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, .flags = A },
265 { NULL },
266};
267
268static const AVClass abuffer_class = {
269 .class_name = "abuffer source",
270 .item_name = av_default_item_name,
271 .option = audio_options,
272 .version = LIBAVUTIL_VERSION_INT,
273};
274
c43a7eca 275static av_cold int init_audio(AVFilterContext *ctx)
4c66c407
AK
276{
277 BufferSourceContext *s = ctx->priv;
278 int ret = 0;
279
b3dd30db
AK
280 if (!(s->sample_fmt_str || s->got_format_from_params)) {
281 av_log(ctx, AV_LOG_ERROR, "Sample format not provided\n");
282 return AVERROR(EINVAL);
283 }
284 if (s->sample_fmt_str)
285 s->sample_fmt = av_get_sample_fmt(s->sample_fmt_str);
286
4c66c407
AK
287 if (s->sample_fmt == AV_SAMPLE_FMT_NONE) {
288 av_log(ctx, AV_LOG_ERROR, "Invalid sample format %s.\n",
289 s->sample_fmt_str);
d28cb849 290 return AVERROR(EINVAL);
4c66c407
AK
291 }
292
b3dd30db
AK
293 if (s->channel_layout_str)
294 s->channel_layout = av_get_channel_layout(s->channel_layout_str);
295
4c66c407
AK
296 if (!s->channel_layout) {
297 av_log(ctx, AV_LOG_ERROR, "Invalid channel layout %s.\n",
298 s->channel_layout_str);
d28cb849 299 return AVERROR(EINVAL);
4c66c407
AK
300 }
301
d28cb849
AK
302 if (!(s->fifo = av_fifo_alloc(sizeof(AVFrame*))))
303 return AVERROR(ENOMEM);
4c66c407
AK
304
305 if (!s->time_base.num)
306 s->time_base = (AVRational){1, s->sample_rate};
307
308 av_log(ctx, AV_LOG_VERBOSE, "tb:%d/%d samplefmt:%s samplerate: %d "
309 "ch layout:%s\n", s->time_base.num, s->time_base.den, s->sample_fmt_str,
310 s->sample_rate, s->channel_layout_str);
311
4c66c407
AK
312 return ret;
313}
314
43fe6a29
AK
315static av_cold void uninit(AVFilterContext *ctx)
316{
317 BufferSourceContext *s = ctx->priv;
8b05e13d 318 while (s->fifo && av_fifo_size(s->fifo)) {
7e350379
AK
319 AVFrame *frame;
320 av_fifo_generic_read(s->fifo, &frame, sizeof(frame), NULL);
321 av_frame_free(&frame);
95587d29 322 }
b3dd30db 323 av_buffer_unref(&s->hw_frames_ctx);
95587d29
AK
324 av_fifo_free(s->fifo);
325 s->fifo = NULL;
43fe6a29
AK
326}
327
cf13f204
MN
328static int query_formats(AVFilterContext *ctx)
329{
330 BufferSourceContext *c = ctx->priv;
4c66c407
AK
331 AVFilterChannelLayouts *channel_layouts = NULL;
332 AVFilterFormats *formats = NULL;
333 AVFilterFormats *samplerates = NULL;
334
335 switch (ctx->outputs[0]->type) {
336 case AVMEDIA_TYPE_VIDEO:
b74a1da4
AK
337 ff_add_format(&formats, c->pix_fmt);
338 ff_set_common_formats(ctx, formats);
4c66c407
AK
339 break;
340 case AVMEDIA_TYPE_AUDIO:
b74a1da4
AK
341 ff_add_format(&formats, c->sample_fmt);
342 ff_set_common_formats(ctx, formats);
4c66c407 343
b74a1da4 344 ff_add_format(&samplerates, c->sample_rate);
4c66c407
AK
345 ff_set_common_samplerates(ctx, samplerates);
346
347 ff_add_channel_layout(&channel_layouts, c->channel_layout);
348 ff_set_common_channel_layouts(ctx, channel_layouts);
349 break;
350 default:
351 return AVERROR(EINVAL);
352 }
cf13f204 353
cf13f204
MN
354 return 0;
355}
356
357static int config_props(AVFilterLink *link)
358{
359 BufferSourceContext *c = link->src->priv;
360
4c66c407
AK
361 switch (link->type) {
362 case AVMEDIA_TYPE_VIDEO:
363 link->w = c->w;
364 link->h = c->h;
365 link->sample_aspect_ratio = c->pixel_aspect;
b3dd30db
AK
366
367 if (c->hw_frames_ctx) {
368 link->hw_frames_ctx = av_buffer_ref(c->hw_frames_ctx);
369 if (!link->hw_frames_ctx)
370 return AVERROR(ENOMEM);
371 }
4c66c407
AK
372 break;
373 case AVMEDIA_TYPE_AUDIO:
374 link->channel_layout = c->channel_layout;
375 link->sample_rate = c->sample_rate;
376 break;
377 default:
378 return AVERROR(EINVAL);
379 }
cf13f204 380
4c66c407 381 link->time_base = c->time_base;
61fb67dc 382 link->frame_rate = c->frame_rate;
cf13f204
MN
383 return 0;
384}
385
cf13f204
MN
386static int request_frame(AVFilterLink *link)
387{
388 BufferSourceContext *c = link->src->priv;
7e350379 389 AVFrame *frame;
cd991462 390 int ret = 0;
cf13f204 391
95587d29 392 if (!av_fifo_size(c->fifo)) {
7ae7c414
AK
393 if (c->eof)
394 return AVERROR_EOF;
5cb4f1a1 395 return AVERROR(EAGAIN);
cf13f204 396 }
7e350379 397 av_fifo_generic_read(c->fifo, &frame, sizeof(frame), NULL);
cf13f204 398
fb25d99b 399 ret = ff_filter_frame(link, frame);
4c66c407 400
cd991462 401 return ret;
cf13f204
MN
402}
403
404static int poll_frame(AVFilterLink *link)
405{
406 BufferSourceContext *c = link->src->priv;
7ae7c414
AK
407 int size = av_fifo_size(c->fifo);
408 if (!size && c->eof)
409 return AVERROR_EOF;
7e350379 410 return size/sizeof(AVFrame*);
cf13f204
MN
411}
412
568c70e7
MR
413static const AVFilterPad avfilter_vsrc_buffer_outputs[] = {
414 {
415 .name = "default",
416 .type = AVMEDIA_TYPE_VIDEO,
417 .request_frame = request_frame,
418 .poll_frame = poll_frame,
419 .config_props = config_props,
420 },
421 { NULL }
422};
423
cd43ca04 424AVFilter ff_vsrc_buffer = {
cf13f204 425 .name = "buffer",
ce1f8536 426 .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them accessible to the filterchain."),
cf13f204 427 .priv_size = sizeof(BufferSourceContext),
d28cb849 428 .priv_class = &buffer_class,
cf13f204
MN
429 .query_formats = query_formats,
430
4c66c407 431 .init = init_video,
43fe6a29 432 .uninit = uninit,
cf13f204 433
1fce361d 434 .inputs = NULL,
568c70e7
MR
435 .outputs = avfilter_vsrc_buffer_outputs,
436};
437
438static const AVFilterPad avfilter_asrc_abuffer_outputs[] = {
439 {
440 .name = "default",
441 .type = AVMEDIA_TYPE_AUDIO,
442 .request_frame = request_frame,
443 .poll_frame = poll_frame,
444 .config_props = config_props,
445 },
446 { NULL }
cf13f204 447};
4c66c407 448
cd43ca04 449AVFilter ff_asrc_abuffer = {
4c66c407
AK
450 .name = "abuffer",
451 .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them accessible to the filterchain."),
452 .priv_size = sizeof(BufferSourceContext),
d28cb849 453 .priv_class = &abuffer_class,
4c66c407
AK
454 .query_formats = query_formats,
455
456 .init = init_audio,
457 .uninit = uninit,
458
1fce361d 459 .inputs = NULL,
568c70e7 460 .outputs = avfilter_asrc_abuffer_outputs,
4c66c407 461};