1 /*
2 * Copyright (c) 2011 Stefano Sabatini
3 *
4 * This file is part of FFmpeg.
5 *
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.
10 *
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.
15 *
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
19 */
20
21 /**
22 * @file
23 * buffer sink
24 */
25
33
38
43
44 /* only used for video */
47
48 /* only used for audio */
56 int *
sample_rates;
///< list of accepted sample rates, terminated by -1
58
59 /* only used for compat API */
63
64 #define NB_ITEMS(list) (list ## _size / sizeof(*list))
65
67 {
70
73
78 }
80 }
81 }
82
84 {
86
88 /* realloc fifo size */
91 "Cannot buffer more frames. Consume some available frames "
92 "before adding new ones.\n");
94 }
95 }
96
97 /* cache frame */
99 return 0;
100 }
101
103 {
107
113 "%d buffers queued in %s, something may be wrong.\n",
117 }
118 return 0;
119 }
120
122 {
124 }
125
127 {
132
133 /* no picref available, fetch it from the filterchain */
141 }
142
145
150 } else {
154 }
155
156 return 0;
157 }
158
160 int nb_samples)
161 {
165
169
174
177
178 return 0;
179 }
180
183 {
188
193 }
194
195 while (ret >= 0) {
198
205 } else if (ret < 0) {
208 }
209
215 }
216
220 }
221
223 }
224
226 {
229 if (!params)
231
234 }
235
237 {
239
240 if (!params)
243 }
244
245 #define FIFO_INIT_SIZE 8
246
248 {
250
255 }
258 return 0;
259 }
260
262 {
264
267 }
268
269 #if FF_API_AVFILTERBUFFER
272 {
276 }
277
279 AVFilterBufferRef **pbuf,
int nb_samples,
int flags)
280 {
281 AVFilterBufferRef *
buf;
284
285 if (!pbuf)
287
289 if (!frame)
291
292 if (!nb_samples)
294 else
296
297 if (ret < 0)
298 goto fail;
299
303 AV_PERM_READ,
306 } else {
307 buf = avfilter_get_audio_buffer_ref_from_arrays(frame->
extended_data,
312 }
313 if (!buf) {
315 goto fail;
316 }
317
318 avfilter_copy_frame_props(buf, frame);
319 )
320
321 buf->buf->priv = frame;
323
324 *pbuf = buf;
325
326 return 0;
327 fail:
330 }
331
332 int attribute_align_arg av_buffersink_read(
AVFilterContext *ctx, AVFilterBufferRef **buf)
333 {
334 return compat_read(ctx, buf, 0, 0);
335 }
336
337 int attribute_align_arg av_buffersink_read_samples(
AVFilterContext *ctx, AVFilterBufferRef **buf,
338 int nb_samples)
339 {
340 return compat_read(ctx, buf, nb_samples, 0);
341 }
342
343 int attribute_align_arg av_buffersink_get_buffer_ref(
AVFilterContext *ctx,
344 AVFilterBufferRef **bufref, int flags)
345 {
347
351 || !strcmp(ctx->
filter->
name,
"ffabuffersink"));
352
353 return compat_read(ctx, bufref, 0, flags);
354 }
356 #endif
357
359 {
361 || !strcmp(ctx->
filter->
name,
"ffbuffersink"));
362
364 }
365
367 {
370
374 || !strcmp(ctx->
filter->
name,
"ffabuffersink"));
375
377 }
378
380 {
384
385 if (params) {
388 }
389
391 }
392
393 #define CHECK_LIST_SIZE(field) \
394 if (buf->field ## _size % sizeof(*buf->field)) { \
395 av_log(ctx, AV_LOG_ERROR, "Invalid size for " #field ": %d, " \
396 "should be multiple of %d\n", \
397 buf->field ## _size, (int)sizeof(*buf->field)); \
398 return AVERROR(EINVAL); \
399 }
401 {
404 unsigned i;
406
413 }
415 } else {
417 }
418
419 return 0;
420 }
421
423 {
427
428 if (params) {
435 }
437 }
438
440 {
444 unsigned i;
446
451
457 }
459 }
460
467 }
472 }
474 if (layouts)
476 "Conflicting all_channel_counts and list in options\n");
479 }
481 }
482
489 }
491 }
492
493 return 0;
494 }
495
496 #define OFFSET(x) offsetof(BufferSinkContext, x)
497 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
501 };
502 #undef FLAGS
503 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
511 };
512 #undef FLAGS
513
516
517 #if FF_API_AVFILTERBUFFER
518
519 #define ffbuffersink_options buffersink_options
520 #define ffabuffersink_options abuffersink_options
523
525 {
529 },
531 };
532
534 .
name =
"ffbuffersink",
535 .description =
NULL_IF_CONFIG_SMALL(
"Buffer video frames, and make them available to the end of the filter graph."),
537 .priv_class = &ffbuffersink_class,
540
542 .
inputs = ffbuffersink_inputs,
544 };
545
546 static const AVFilterPad ffabuffersink_inputs[] = {
547 {
551 },
553 };
554
556 .
name =
"ffabuffersink",
557 .description =
NULL_IF_CONFIG_SMALL(
"Buffer audio frames, and make them available to the end of the filter graph."),
561 .priv_class = &ffabuffersink_class,
563 .
inputs = ffabuffersink_inputs,
565 };
566 #endif /* FF_API_AVFILTERBUFFER */
567
569 {
573 },
575 };
576
578 .
name =
"buffersink",
579 .description =
NULL_IF_CONFIG_SMALL(
"Buffer video frames, and make them available to the end of the filter graph."),
581 .priv_class = &buffersink_class,
584
586 .
inputs = avfilter_vsink_buffer_inputs,
588 };
589
591 {
595 },
597 };
598
600 .
name =
"abuffersink",
601 .description =
NULL_IF_CONFIG_SMALL(
"Buffer audio frames, and make them available to the end of the filter graph."),
602 .priv_class = &abuffersink_class,
606
608 .
inputs = avfilter_asink_abuffer_inputs,
610 };
AVAudioFifo * av_audio_fifo_alloc(enum AVSampleFormat sample_fmt, int channels, int nb_samples)
Allocate an AVAudioFifo.
int av_audio_fifo_read(AVAudioFifo *af, void **data, int nb_samples)
Read data from an AVAudioFifo.
enum AVSampleFormat * sample_fmts
list of accepted sample formats, terminated by AV_SAMPLE_FMT_NONE
This structure describes decoded (raw) audio or video data.
AVBufferSinkParams * av_buffersink_params_alloc(void)
Create an AVBufferSinkParams structure.
#define AV_LOG_WARNING
Something somehow does not look correct.
static const AVFilterPad outputs[]
#define AV_NOWARN_DEPRECATED(code)
Disable warnings about deprecated features This is useful for sections of code kept for backward comp...
static av_cold void uninit(AVFilterContext *ctx)
Main libavfilter public API header.
static int read_from_fifo(AVFilterContext *ctx, AVFrame *frame, int nb_samples)
const int * channel_counts
list of allowed channel counts, terminated by -1
AVBufferRef * buf[AV_NUM_DATA_POINTERS]
AVBuffer references backing the data for this frame.
AVABufferSinkParams * av_abuffersink_params_alloc(void)
Create an AVABufferSinkParams structure.
int max_samples
Maximum number of samples to filter at once.
#define av_opt_set_int_list(obj, name, val, term, flags)
Set a binary option to an integer list.
void av_audio_fifo_free(AVAudioFifo *af)
Free an AVAudioFifo.
int * sample_rates
list of accepted sample rates, terminated by -1
static const AVOption abuffersink_options[]
int * channel_counts
list of accepted channel counts, terminated by -1
void av_frame_move_ref(AVFrame *dst, AVFrame *src)
Move everything contained in src to dst and reset src.
static enum AVSampleFormat formats[]
int av_fifo_generic_write(AVFifoBuffer *f, void *src, int size, int(*func)(void *, void *, int))
Feed data from a user-supplied callback to an AVFifoBuffer.
memory buffer sink API for audio and video
enum AVSampleFormat * sample_fmts
list of allowed sample formats, terminated by AV_SAMPLE_FMT_NONE
const char * name
Pad name.
AVFilterLink ** inputs
array of pointers to input links
char * name
name of this filter instance
#define av_assert0(cond)
assert() equivalent, that is always enabled.
static av_cold int vsink_init(AVFilterContext *ctx, void *opaque)
AVAudioFifo * audio_fifo
FIFO for audio samples.
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
int av_fifo_space(const AVFifoBuffer *f)
Return the amount of space in bytes in the AVFifoBuffer, that is the amount of data you can write int...
static av_cold int asink_init(AVFilterContext *ctx, void *opaque)
static const AVOption buffersink_options[]
#define AVERROR_EOF
End of file.
#define CHECK_LIST_SIZE(field)
#define AV_BUFFERSINK_FLAG_NO_REQUEST
Tell av_buffersink_get_buffer_ref() not to request a frame from its input.
A filter pad used for either input or output.
static void * av_x_if_null(const void *p, const void *x)
Return x default pointer in case p is NULL.
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
AVFILTER_DEFINE_CLASS(buffersink)
A link between two filters.
static int vsink_query_formats(AVFilterContext *ctx)
int width
width and height of the video frame
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
static uint8_t * av_fifo_peek2(const AVFifoBuffer *f, int offs)
Return a pointer to the data stored in a FIFO buffer at a certain offset.
int closed
True if the link is closed.
int min_samples
Minimum number of samples to filter at once.
AVRational frame_rate
Frame rate of the stream on the link, or 1/0 if unknown; if left to 0/0, will be automatically be cop...
int sample_rate
samples per second
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
int attribute_align_arg av_buffersink_poll_frame(AVFilterContext *ctx)
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
int * sample_rates
list of allowed sample rates, terminated by -1
void * priv
private data for use by the filter
int av_fifo_generic_read(AVFifoBuffer *f, void *dest, int buf_size, void(*func)(void *, void *, int))
Feed data from an AVFifoBuffer to a user-supplied callback.
int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
AVRational time_base
Define the time base used by the PTS of the frames/samples which will pass through this link...
simple assert() macros that are a bit more flexible than ISO C assert().
static const AVFilterPad avfilter_asink_abuffer_inputs[]
int attribute_align_arg av_buffersink_get_frame_flags(AVFilterContext *ctx, AVFrame *frame, int flags)
Get a frame with filtered data from sink and put it in frame.
static const int sample_rates[]
Context for an Audio FIFO Buffer.
uint64_t channel_layout
Channel layout of the audio data.
common internal API header
int64_t * channel_layouts
list of accepted channel layouts, terminated by -1
int av_audio_fifo_size(AVAudioFifo *af)
Get the current number of samples in the AVAudioFifo available for reading.
audio channel layout utility functions
Struct to use for initializing an abuffersink context.
int attribute_align_arg av_buffersink_get_samples(AVFilterContext *ctx, AVFrame *frame, int nb_samples)
Same as av_buffersink_get_frame(), but with the ability to specify the number of samples read...
Struct to use for initializing a buffersink context.
int partial_buf_size
Size of the partial buffer to allocate.
enum AVPixelFormat * pixel_fmts
list of accepted pixel formats, must be terminated with -1
int format
agreed upon media format
offset must point to a pointer immediately followed by an int for the length
AVFilter ff_asink_abuffer
A list of supported channel layouts.
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
AVSampleFormat
Audio sample formats.
int av_fifo_size(const AVFifoBuffer *f)
Return the amount of data in bytes in the AVFifoBuffer, that is the amount of data you can read from ...
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
int av_fifo_realloc2(AVFifoBuffer *f, unsigned int new_size)
Resize an AVFifoBuffer.
int all_channel_counts
if not 0, accept any channel count or layout
Describe the class of an AVClass context structure.
static const AVFilterPad inputs[]
rational number numerator/denominator
AVFilterBufferRef * avfilter_get_video_buffer_ref_from_arrays(uint8_t *const data[4], const int linesize[4], int perms, int w, int h, enum AVPixelFormat format)
const char * name
Filter name.
enum MovChannelLayoutTag * layouts
AVRational av_buffersink_get_frame_rate(AVFilterContext *ctx)
Get the frame rate of the input.
AVFifoBuffer * fifo
FIFO buffer of video frame references.
AVFifoBuffer * av_fifo_alloc_array(size_t nmemb, size_t size)
Initialize an AVFifoBuffer.
enum AVMediaType type
filter media type
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
int av_audio_fifo_write(AVAudioFifo *af, void **data, int nb_samples)
Write data to an AVAudioFifo.
int64_t next_pts
interpolating audio pts
#define FF_DISABLE_DEPRECATION_WARNINGS
static int query_formats(AVFilterContext *ctx)
common internal and external API header
static int add_buffer_ref(AVFilterContext *ctx, AVFrame *ref)
enum AVPixelFormat * pixel_fmts
list of allowed pixel formats, terminated by AV_PIX_FMT_NONE
void av_buffersink_set_frame_size(AVFilterContext *ctx, unsigned frame_size)
Set the frame size for an audio buffer sink.
int channels
Number of channels.
static av_cold int common_init(AVFilterContext *ctx)
#define AV_BUFFERSINK_FLAG_PEEK
Tell av_buffersink_get_buffer_ref() to read video/samples buffer reference, but not remove it from th...
#define FF_ENABLE_DEPRECATION_WARNINGS
static int asink_query_formats(AVFilterContext *ctx)
static int filter_frame(AVFilterLink *link, AVFrame *frame)
AVFilterContext * dst
dest filter
static enum AVSampleFormat sample_fmts[]
static const AVFilterPad avfilter_vsink_buffer_inputs[]
void av_fifo_freep(AVFifoBuffer **f)
Free an AVFifoBuffer and reset pointer to NULL.
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
uint8_t ** extended_data
pointers to the data planes/channels.
int ff_poll_frame(AVFilterLink *link)
Poll a frame from the filter chain.
int attribute_align_arg av_buffersink_get_frame(AVFilterContext *ctx, AVFrame *frame)
Get a frame with filtered data from sink and put it in frame.
const int64_t * channel_layouts
list of allowed channel layouts, terminated by -1
AVPixelFormat
Pixel format.
int nb_samples
number of audio samples (per channel) described by this frame
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
const AVFilter * filter
the AVFilter of which this is an instance
#define AV_NOPTS_VALUE
Undefined timestamp value.
static void compat_free_buffer(void *opaque, uint8_t *data)