FFmpeg
audio_fifo.c
Go to the documentation of this file.
1 /*
2  * Audio FIFO
3  * Copyright (c) 2012 Justin Ruggles <justin.ruggles@gmail.com>
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 /**
23  * @file
24  * Audio FIFO
25  */
26 
27 #include "avutil.h"
28 #include "audio_fifo.h"
29 #include "common.h"
30 #include "fifo.h"
31 #include "mem.h"
32 #include "samplefmt.h"
33 
34 struct AVAudioFifo {
35  AVFifoBuffer **buf; /**< single buffer for interleaved, per-channel buffers for planar */
36  int nb_buffers; /**< number of buffers */
37  int nb_samples; /**< number of samples currently in the FIFO */
38  int allocated_samples; /**< current allocated size, in samples */
39 
40  int channels; /**< number of channels */
41  enum AVSampleFormat sample_fmt; /**< sample format */
42  int sample_size; /**< size, in bytes, of one sample in a buffer */
43 };
44 
46 {
47  if (af) {
48  if (af->buf) {
49  int i;
50  for (i = 0; i < af->nb_buffers; i++) {
51  av_fifo_freep(&af->buf[i]);
52  }
53  av_freep(&af->buf);
54  }
55  av_free(af);
56  }
57 }
58 
60  int nb_samples)
61 {
62  AVAudioFifo *af;
63  int buf_size, i;
64 
65  /* get channel buffer size (also validates parameters) */
66  if (av_samples_get_buffer_size(&buf_size, channels, nb_samples, sample_fmt, 1) < 0)
67  return NULL;
68 
69  af = av_mallocz(sizeof(*af));
70  if (!af)
71  return NULL;
72 
73  af->channels = channels;
74  af->sample_fmt = sample_fmt;
75  af->sample_size = buf_size / nb_samples;
76  af->nb_buffers = av_sample_fmt_is_planar(sample_fmt) ? channels : 1;
77 
78  af->buf = av_mallocz_array(af->nb_buffers, sizeof(*af->buf));
79  if (!af->buf)
80  goto error;
81 
82  for (i = 0; i < af->nb_buffers; i++) {
83  af->buf[i] = av_fifo_alloc(buf_size);
84  if (!af->buf[i])
85  goto error;
86  }
87  af->allocated_samples = nb_samples;
88 
89  return af;
90 
91 error:
93  return NULL;
94 }
95 
96 int av_audio_fifo_realloc(AVAudioFifo *af, int nb_samples)
97 {
98  int i, ret, buf_size;
99 
100  if ((ret = av_samples_get_buffer_size(&buf_size, af->channels, nb_samples,
101  af->sample_fmt, 1)) < 0)
102  return ret;
103 
104  for (i = 0; i < af->nb_buffers; i++) {
105  if ((ret = av_fifo_realloc2(af->buf[i], buf_size)) < 0)
106  return ret;
107  }
108  af->allocated_samples = nb_samples;
109  return 0;
110 }
111 
112 int av_audio_fifo_write(AVAudioFifo *af, void **data, int nb_samples)
113 {
114  int i, ret, size;
115 
116  /* automatically reallocate buffers if needed */
117  if (av_audio_fifo_space(af) < nb_samples) {
118  int current_size = av_audio_fifo_size(af);
119  /* check for integer overflow in new size calculation */
120  if (INT_MAX / 2 - current_size < nb_samples)
121  return AVERROR(EINVAL);
122  /* reallocate buffers */
123  if ((ret = av_audio_fifo_realloc(af, 2 * (current_size + nb_samples))) < 0)
124  return ret;
125  }
126 
127  size = nb_samples * af->sample_size;
128  for (i = 0; i < af->nb_buffers; i++) {
130  if (ret != size)
131  return AVERROR_BUG;
132  }
133  af->nb_samples += nb_samples;
134 
135  return nb_samples;
136 }
137 
138 int av_audio_fifo_peek(AVAudioFifo *af, void **data, int nb_samples)
139 {
140  int i, ret, size;
141 
142  if (nb_samples < 0)
143  return AVERROR(EINVAL);
144  nb_samples = FFMIN(nb_samples, af->nb_samples);
145  if (!nb_samples)
146  return 0;
147 
148  size = nb_samples * af->sample_size;
149  for (i = 0; i < af->nb_buffers; i++) {
150  if ((ret = av_fifo_generic_peek(af->buf[i], data[i], size, NULL)) < 0)
151  return AVERROR_BUG;
152  }
153 
154  return nb_samples;
155 }
156 
157 int av_audio_fifo_peek_at(AVAudioFifo *af, void **data, int nb_samples, int offset)
158 {
159  int i, ret, size;
160 
161  if (offset < 0 || offset >= af->nb_samples)
162  return AVERROR(EINVAL);
163  if (nb_samples < 0)
164  return AVERROR(EINVAL);
165  nb_samples = FFMIN(nb_samples, af->nb_samples);
166  if (!nb_samples)
167  return 0;
168  if (offset > af->nb_samples - nb_samples)
169  return AVERROR(EINVAL);
170 
171  offset *= af->sample_size;
172  size = nb_samples * af->sample_size;
173  for (i = 0; i < af->nb_buffers; i++) {
174  if ((ret = av_fifo_generic_peek_at(af->buf[i], data[i], offset, size, NULL)) < 0)
175  return AVERROR_BUG;
176  }
177 
178  return nb_samples;
179 }
180 
181 int av_audio_fifo_read(AVAudioFifo *af, void **data, int nb_samples)
182 {
183  int i, size;
184 
185  if (nb_samples < 0)
186  return AVERROR(EINVAL);
187  nb_samples = FFMIN(nb_samples, af->nb_samples);
188  if (!nb_samples)
189  return 0;
190 
191  size = nb_samples * af->sample_size;
192  for (i = 0; i < af->nb_buffers; i++) {
193  if (av_fifo_generic_read(af->buf[i], data[i], size, NULL) < 0)
194  return AVERROR_BUG;
195  }
196  af->nb_samples -= nb_samples;
197 
198  return nb_samples;
199 }
200 
201 int av_audio_fifo_drain(AVAudioFifo *af, int nb_samples)
202 {
203  int i, size;
204 
205  if (nb_samples < 0)
206  return AVERROR(EINVAL);
207  nb_samples = FFMIN(nb_samples, af->nb_samples);
208 
209  if (nb_samples) {
210  size = nb_samples * af->sample_size;
211  for (i = 0; i < af->nb_buffers; i++)
212  av_fifo_drain(af->buf[i], size);
213  af->nb_samples -= nb_samples;
214  }
215  return 0;
216 }
217 
219 {
220  int i;
221 
222  for (i = 0; i < af->nb_buffers; i++)
223  av_fifo_reset(af->buf[i]);
224 
225  af->nb_samples = 0;
226 }
227 
229 {
230  return af->nb_samples;
231 }
232 
234 {
235  return af->allocated_samples - af->nb_samples;
236 }
error
static void error(const char *err)
Definition: target_bsf_fuzzer.c:29
av_audio_fifo_free
void av_audio_fifo_free(AVAudioFifo *af)
Free an AVAudioFifo.
Definition: audio_fifo.c:45
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
av_fifo_generic_write
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.
Definition: fifo.c:122
av_audio_fifo_realloc
int av_audio_fifo_realloc(AVAudioFifo *af, int nb_samples)
Reallocate an AVAudioFifo.
Definition: audio_fifo.c:96
data
const char data[16]
Definition: mxf.c:91
av_fifo_generic_peek
int av_fifo_generic_peek(AVFifoBuffer *f, void *dest, int buf_size, void(*func)(void *, void *, int))
Feed data from an AVFifoBuffer to a user-supplied callback.
Definition: fifo.c:189
av_mallocz_array
void * av_mallocz_array(size_t nmemb, size_t size)
Definition: mem.c:190
av_fifo_generic_read
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.
Definition: fifo.c:213
av_fifo_reset
void av_fifo_reset(AVFifoBuffer *f)
Reset the AVFifoBuffer to the state right after av_fifo_alloc, in particular it is emptied.
Definition: fifo.c:71
av_fifo_drain
void av_fifo_drain(AVFifoBuffer *f, int size)
Discard data from the FIFO.
Definition: fifo.c:233
AVFifoBuffer
Definition: fifo.h:31
AVAudioFifo::nb_samples
int nb_samples
number of samples currently in the FIFO
Definition: audio_fifo.c:37
fifo.h
AVAudioFifo
Context for an Audio FIFO Buffer.
Definition: audio_fifo.c:34
av_audio_fifo_drain
int av_audio_fifo_drain(AVAudioFifo *af, int nb_samples)
Drain data from an AVAudioFifo.
Definition: audio_fifo.c:201
samplefmt.h
AVAudioFifo::channels
int channels
number of channels
Definition: audio_fifo.c:40
AVAudioFifo::sample_size
int sample_size
size, in bytes, of one sample in a buffer
Definition: audio_fifo.c:42
av_audio_fifo_write
int av_audio_fifo_write(AVAudioFifo *af, void **data, int nb_samples)
Write data to an AVAudioFifo.
Definition: audio_fifo.c:112
av_sample_fmt_is_planar
int av_sample_fmt_is_planar(enum AVSampleFormat sample_fmt)
Check if the sample format is planar.
Definition: samplefmt.c:112
channels
channels
Definition: aptx.h:33
av_fifo_realloc2
int av_fifo_realloc2(AVFifoBuffer *f, unsigned int new_size)
Resize an AVFifoBuffer.
Definition: fifo.c:87
NULL
#define NULL
Definition: coverity.c:32
av_audio_fifo_alloc
AVAudioFifo * av_audio_fifo_alloc(enum AVSampleFormat sample_fmt, int channels, int nb_samples)
Allocate an AVAudioFifo.
Definition: audio_fifo.c:59
av_audio_fifo_peek_at
int av_audio_fifo_peek_at(AVAudioFifo *af, void **data, int nb_samples, int offset)
Peek data from an AVAudioFifo.
Definition: audio_fifo.c:157
size
int size
Definition: twinvq_data.h:11134
FFMIN
#define FFMIN(a, b)
Definition: common.h:96
offset
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Definition: writing_filters.txt:86
av_audio_fifo_size
int av_audio_fifo_size(AVAudioFifo *af)
Get the current number of samples in the AVAudioFifo available for reading.
Definition: audio_fifo.c:228
av_audio_fifo_read
int av_audio_fifo_read(AVAudioFifo *af, void **data, int nb_samples)
Read data from an AVAudioFifo.
Definition: audio_fifo.c:181
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
common.h
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:237
audio_fifo.h
av_samples_get_buffer_size
int av_samples_get_buffer_size(int *linesize, int nb_channels, int nb_samples, enum AVSampleFormat sample_fmt, int align)
Get the required buffer size for the given audio parameters.
Definition: samplefmt.c:119
ret
ret
Definition: filter_design.txt:187
AVAudioFifo::sample_fmt
enum AVSampleFormat sample_fmt
sample format
Definition: audio_fifo.c:41
AVAudioFifo::allocated_samples
int allocated_samples
current allocated size, in samples
Definition: audio_fifo.c:38
avutil.h
mem.h
AVAudioFifo::nb_buffers
int nb_buffers
number of buffers
Definition: audio_fifo.c:36
av_fifo_generic_peek_at
int av_fifo_generic_peek_at(AVFifoBuffer *f, void *dest, int offset, int buf_size, void(*func)(void *, void *, int))
Feed data at specific position from an AVFifoBuffer to a user-supplied callback.
Definition: fifo.c:151
av_fifo_freep
void av_fifo_freep(AVFifoBuffer **f)
Free an AVFifoBuffer and reset pointer to NULL.
Definition: fifo.c:63
av_free
#define av_free(p)
Definition: tableprint_vlc.h:34
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
av_audio_fifo_space
int av_audio_fifo_space(AVAudioFifo *af)
Get the current number of samples in the AVAudioFifo available for writing.
Definition: audio_fifo.c:233
av_fifo_alloc
AVFifoBuffer * av_fifo_alloc(unsigned int size)
Initialize an AVFifoBuffer.
Definition: fifo.c:43
AVERROR_BUG
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:50
av_audio_fifo_reset
void av_audio_fifo_reset(AVAudioFifo *af)
Reset the AVAudioFifo buffer.
Definition: audio_fifo.c:218
av_audio_fifo_peek
int av_audio_fifo_peek(AVAudioFifo *af, void **data, int nb_samples)
Peek data from an AVAudioFifo.
Definition: audio_fifo.c:138
AVAudioFifo::buf
AVFifoBuffer ** buf
single buffer for interleaved, per-channel buffers for planar
Definition: audio_fifo.c:35