Libav
buffersink.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011 Stefano Sabatini
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 
26 #include "libavutil/audio_fifo.h"
27 #include "libavutil/avassert.h"
29 #include "libavutil/common.h"
30 #include "libavutil/internal.h"
31 #include "libavutil/mathematics.h"
32 
33 #include "audio.h"
34 #include "avfilter.h"
35 #include "buffersink.h"
36 #include "internal.h"
37 
38 typedef struct BufferSinkContext {
41  int64_t next_pts;
43 
44 static av_cold void uninit(AVFilterContext *ctx)
45 {
46  BufferSinkContext *sink = ctx->priv;
47 
48  if (sink->audio_fifo)
50 }
51 
52 static int filter_frame(AVFilterLink *link, AVFrame *frame)
53 {
54  BufferSinkContext *s = link->dst->priv;
55 
56  av_assert0(!s->cur_frame);
57  s->cur_frame = frame;
58 
59  return 0;
60 }
61 
63  AVFrame *frame)
64 {
65  BufferSinkContext *s = ctx->priv;
66  AVFilterLink *link = ctx->inputs[0];
67  int ret;
68 
69  if ((ret = ff_request_frame(link)) < 0)
70  return ret;
71 
72  if (!s->cur_frame)
73  return AVERROR(EINVAL);
74 
75  av_frame_move_ref(frame, s->cur_frame);
77 
78  return 0;
79 }
80 
81 static int read_from_fifo(AVFilterContext *ctx, AVFrame *frame,
82  int nb_samples)
83 {
84  BufferSinkContext *s = ctx->priv;
85  AVFilterLink *link = ctx->inputs[0];
86  AVFrame *tmp;
87 
88  if (!(tmp = ff_get_audio_buffer(link, nb_samples)))
89  return AVERROR(ENOMEM);
90  av_audio_fifo_read(s->audio_fifo, (void**)tmp->extended_data, nb_samples);
91 
92  tmp->pts = s->next_pts;
93  s->next_pts += av_rescale_q(nb_samples, (AVRational){1, link->sample_rate},
94  link->time_base);
95 
96  av_frame_move_ref(frame, tmp);
97  av_frame_free(&tmp);
98 
99  return 0;
100 }
101 
103  AVFrame *frame, int nb_samples)
104 {
105  BufferSinkContext *s = ctx->priv;
106  AVFilterLink *link = ctx->inputs[0];
107  int ret = 0;
108 
109  if (!s->audio_fifo) {
111  if (!(s->audio_fifo = av_audio_fifo_alloc(link->format, nb_channels, nb_samples)))
112  return AVERROR(ENOMEM);
113  }
114 
115  while (ret >= 0) {
116  if (av_audio_fifo_size(s->audio_fifo) >= nb_samples)
117  return read_from_fifo(ctx, frame, nb_samples);
118 
119  ret = ff_request_frame(link);
120  if (ret == AVERROR_EOF && av_audio_fifo_size(s->audio_fifo))
121  return read_from_fifo(ctx, frame, av_audio_fifo_size(s->audio_fifo));
122  else if (ret < 0)
123  return ret;
124 
125  if (s->cur_frame->pts != AV_NOPTS_VALUE) {
126  s->next_pts = s->cur_frame->pts -
128  (AVRational){ 1, link->sample_rate },
129  link->time_base);
130  }
131 
133  s->cur_frame->nb_samples);
135  }
136 
137  return ret;
138 }
139 
140 #if FF_API_AVFILTERBUFFER
142 static void compat_free_buffer(AVFilterBuffer *buf)
143 {
144  AVFrame *frame = buf->priv;
145  av_frame_free(&frame);
146  av_free(buf);
147 }
148 
149 static int compat_read(AVFilterContext *ctx,
150  AVFilterBufferRef **pbuf, int nb_samples)
151 {
152  AVFilterBufferRef *buf;
153  AVFrame *frame;
154  int ret;
155 
156  if (!pbuf)
157  return ff_poll_frame(ctx->inputs[0]);
158 
159  frame = av_frame_alloc();
160  if (!frame)
161  return AVERROR(ENOMEM);
162 
163  if (!nb_samples)
164  ret = av_buffersink_get_frame(ctx, frame);
165  else
166  ret = av_buffersink_get_samples(ctx, frame, nb_samples);
167 
168  if (ret < 0)
169  goto fail;
170 
171  if (ctx->inputs[0]->type == AVMEDIA_TYPE_VIDEO) {
173  AV_PERM_READ,
174  frame->width, frame->height,
175  frame->format);
176  } else {
177  buf = avfilter_get_audio_buffer_ref_from_arrays(frame->extended_data,
178  frame->linesize[0], AV_PERM_READ,
179  frame->nb_samples,
180  frame->format,
181  frame->channel_layout);
182  }
183  if (!buf) {
184  ret = AVERROR(ENOMEM);
185  goto fail;
186  }
187 
188  avfilter_copy_frame_props(buf, frame);
189 
190  buf->buf->priv = frame;
191  buf->buf->free = compat_free_buffer;
192 
193  *pbuf = buf;
194 
195  return 0;
196 fail:
197  av_frame_free(&frame);
198  return ret;
199 }
200 
201 int attribute_align_arg av_buffersink_read(AVFilterContext *ctx, AVFilterBufferRef **buf)
202 {
203  return compat_read(ctx, buf, 0);
204 }
205 
206 int attribute_align_arg av_buffersink_read_samples(AVFilterContext *ctx, AVFilterBufferRef **buf,
207  int nb_samples)
208 {
209  return compat_read(ctx, buf, nb_samples);
210 }
212 #endif
213 
215  {
216  .name = "default",
217  .type = AVMEDIA_TYPE_VIDEO,
218  .filter_frame = filter_frame,
219  .needs_fifo = 1
220  },
221  { NULL }
222 };
223 
225  .name = "buffersink",
226  .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
227  .priv_size = sizeof(BufferSinkContext),
228  .uninit = uninit,
229 
230  .inputs = avfilter_vsink_buffer_inputs,
231  .outputs = NULL,
232 };
233 
235  {
236  .name = "default",
237  .type = AVMEDIA_TYPE_AUDIO,
238  .filter_frame = filter_frame,
239  .needs_fifo = 1
240  },
241  { NULL }
242 };
243 
245  .name = "abuffersink",
246  .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
247  .priv_size = sizeof(BufferSinkContext),
248  .uninit = uninit,
249 
250  .inputs = avfilter_asink_abuffer_inputs,
251  .outputs = NULL,
252 };
AVAudioFifo * av_audio_fifo_alloc(enum AVSampleFormat sample_fmt, int channels, int nb_samples)
Allocate an AVAudioFifo.
Definition: audio_fifo.c:60
int av_audio_fifo_read(AVAudioFifo *af, void **data, int nb_samples)
Read data from an AVAudioFifo.
Definition: audio_fifo.c:139
This structure describes decoded (raw) audio or video data.
Definition: frame.h:135
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:232
static av_cold void uninit(AVFilterContext *ctx)
Definition: buffersink.c:44
Main libavfilter public API header.
static int read_from_fifo(AVFilterContext *ctx, AVFrame *frame, int nb_samples)
Definition: buffersink.c:81
AVBufferRef * buf[AV_NUM_DATA_POINTERS]
AVBuffer references backing the data for this frame.
Definition: frame.h:393
void av_audio_fifo_free(AVAudioFifo *af)
Free an AVAudioFifo.
Definition: audio_fifo.c:45
void av_frame_move_ref(AVFrame *dst, AVFrame *src)
Move everythnig contained in src to dst and reset src.
Definition: frame.c:300
memory buffer sink API
const char * name
Pad name.
Definition: internal.h:42
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:571
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
AVAudioFifo * audio_fifo
FIFO for audio samples.
Definition: buffersink.c:40
#define av_cold
Definition: attributes.h:66
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:57
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:211
#define AVERROR_EOF
End of file.
Definition: error.h:51
A filter pad used for either input or output.
Definition: internal.h:36
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:129
int width
width and height of the video frame
Definition: frame.h:174
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:186
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
Definition: audio.c:57
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:69
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:150
void * priv
private data for use by the filter
Definition: avfilter.h:584
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
simple assert() macros that are a bit more flexible than ISO C assert().
static const AVFilterPad avfilter_asink_abuffer_inputs[]
Definition: buffersink.c:234
AVFilter ff_vsink_buffer
Definition: buffersink.c:224
Context for an Audio FIFO Buffer.
Definition: audio_fifo.c:34
uint64_t channel_layout
Channel layout of the audio data.
Definition: frame.h:381
common internal API header
int av_audio_fifo_size(AVAudioFifo *af)
Get the current number of samples in the AVAudioFifo available for reading.
Definition: audio_fifo.c:186
audio channel layout utility functions
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...
Definition: buffersink.c:102
AVFilter ff_asink_abuffer
Definition: buffersink.c:244
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:186
#define attribute_align_arg
Definition: internal.h:54
NULL
Definition: eval.c:55
AVFrame * cur_frame
last frame delivered on the sink
Definition: buffersink.c:39
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:153
Filter definition.
Definition: avfilter.h:421
static const AVFilterPad inputs[]
Definition: af_ashowinfo.c:221
rational number numerator/denominator
Definition: rational.h:43
const char * name
Filter name.
Definition: avfilter.h:425
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:141
int av_audio_fifo_write(AVAudioFifo *af, void **data, int nb_samples)
Write data to an AVAudioFifo.
Definition: audio_fifo.c:113
int64_t next_pts
interpolating audio pts
Definition: buffersink.c:41
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:76
common internal and external API header
AVFilterBufferRef * avfilter_get_video_buffer_ref_from_arrays(uint8_t *data[4], int linesize[4], int perms, int w, int h, enum AVPixelFormat format)
Definition: video.c:59
Audio FIFO Buffer.
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:77
static int filter_frame(AVFilterLink *link, AVFrame *frame)
Definition: buffersink.c:52
An instance of a filter.
Definition: avfilter.h:563
int height
Definition: frame.h:174
static const AVFilterPad avfilter_vsink_buffer_inputs[]
Definition: buffersink.c:214
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
Definition: avfilter.c:249
int nb_channels
internal API functions
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:169
int ff_poll_frame(AVFilterLink *link)
Poll a frame from the filter chain.
Definition: avfilter.c:260
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.
Definition: buffersink.c:62
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:179
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:228
static void compat_free_buffer(void *opaque, uint8_t *data)
Definition: utils.c:544