FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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  if (af->buf[i])
52  av_fifo_free(af->buf[i]);
53  }
54  av_freep(&af->buf);
55  }
56  av_free(af);
57  }
58 }
59 
60 AVAudioFifo *av_audio_fifo_alloc(enum AVSampleFormat sample_fmt, int channels,
61  int nb_samples)
62 {
63  AVAudioFifo *af;
64  int buf_size, i;
65 
66  /* get channel buffer size (also validates parameters) */
67  if (av_samples_get_buffer_size(&buf_size, channels, nb_samples, sample_fmt, 1) < 0)
68  return NULL;
69 
70  af = av_mallocz(sizeof(*af));
71  if (!af)
72  return NULL;
73 
74  af->channels = channels;
75  af->sample_fmt = sample_fmt;
76  af->sample_size = buf_size / nb_samples;
77  af->nb_buffers = av_sample_fmt_is_planar(sample_fmt) ? channels : 1;
78 
79  af->buf = av_mallocz_array(af->nb_buffers, sizeof(*af->buf));
80  if (!af->buf)
81  goto error;
82 
83  for (i = 0; i < af->nb_buffers; i++) {
84  af->buf[i] = av_fifo_alloc(buf_size);
85  if (!af->buf[i])
86  goto error;
87  }
88  af->allocated_samples = nb_samples;
89 
90  return af;
91 
92 error:
94  return NULL;
95 }
96 
97 int av_audio_fifo_realloc(AVAudioFifo *af, int nb_samples)
98 {
99  int i, ret, buf_size;
100 
101  if ((ret = av_samples_get_buffer_size(&buf_size, af->channels, nb_samples,
102  af->sample_fmt, 1)) < 0)
103  return ret;
104 
105  for (i = 0; i < af->nb_buffers; i++) {
106  if ((ret = av_fifo_realloc2(af->buf[i], buf_size)) < 0)
107  return ret;
108  }
109  af->allocated_samples = nb_samples;
110  return 0;
111 }
112 
113 int av_audio_fifo_write(AVAudioFifo *af, void **data, int nb_samples)
114 {
115  int i, ret, size;
116 
117  /* automatically reallocate buffers if needed */
118  if (av_audio_fifo_space(af) < nb_samples) {
119  int current_size = av_audio_fifo_size(af);
120  /* check for integer overflow in new size calculation */
121  if (INT_MAX / 2 - current_size < nb_samples)
122  return AVERROR(EINVAL);
123  /* reallocate buffers */
124  if ((ret = av_audio_fifo_realloc(af, 2 * (current_size + nb_samples))) < 0)
125  return ret;
126  }
127 
128  size = nb_samples * af->sample_size;
129  for (i = 0; i < af->nb_buffers; i++) {
130  ret = av_fifo_generic_write(af->buf[i], data[i], size, NULL);
131  if (ret != size)
132  return AVERROR_BUG;
133  }
134  af->nb_samples += nb_samples;
135 
136  return nb_samples;
137 }
138 
139 int av_audio_fifo_peek(AVAudioFifo *af, void **data, int nb_samples)
140 {
141  int i, ret, size;
142 
143  if (nb_samples < 0)
144  return AVERROR(EINVAL);
145  nb_samples = FFMIN(nb_samples, af->nb_samples);
146  if (!nb_samples)
147  return 0;
148 
149  size = nb_samples * af->sample_size;
150  for (i = 0; i < af->nb_buffers; i++) {
151  if ((ret = av_fifo_generic_peek(af->buf[i], data[i], size, NULL)) < 0)
152  return AVERROR_BUG;
153  }
154 
155  return nb_samples;
156 }
157 
158 int av_audio_fifo_peek_at(AVAudioFifo *af, void **data, int nb_samples, int offset)
159 {
160  int i, ret, size;
161 
162  if (offset < 0 || offset >= af->nb_samples)
163  return AVERROR(EINVAL);
164  if (nb_samples < 0)
165  return AVERROR(EINVAL);
166  nb_samples = FFMIN(nb_samples, af->nb_samples);
167  if (!nb_samples)
168  return 0;
169  if (offset > af->nb_samples - nb_samples)
170  return AVERROR(EINVAL);
171 
172  offset *= af->sample_size;
173  size = nb_samples * af->sample_size;
174  for (i = 0; i < af->nb_buffers; i++) {
175  if ((ret = av_fifo_generic_peek_at(af->buf[i], data[i], offset, size, NULL)) < 0)
176  return AVERROR_BUG;
177  }
178 
179  return nb_samples;
180 }
181 
182 int av_audio_fifo_read(AVAudioFifo *af, void **data, int nb_samples)
183 {
184  int i, ret, size;
185 
186  if (nb_samples < 0)
187  return AVERROR(EINVAL);
188  nb_samples = FFMIN(nb_samples, af->nb_samples);
189  if (!nb_samples)
190  return 0;
191 
192  size = nb_samples * af->sample_size;
193  for (i = 0; i < af->nb_buffers; i++) {
194  if ((ret = av_fifo_generic_read(af->buf[i], data[i], size, NULL)) < 0)
195  return AVERROR_BUG;
196  }
197  af->nb_samples -= nb_samples;
198 
199  return nb_samples;
200 }
201 
202 int av_audio_fifo_drain(AVAudioFifo *af, int nb_samples)
203 {
204  int i, size;
205 
206  if (nb_samples < 0)
207  return AVERROR(EINVAL);
208  nb_samples = FFMIN(nb_samples, af->nb_samples);
209 
210  if (nb_samples) {
211  size = nb_samples * af->sample_size;
212  for (i = 0; i < af->nb_buffers; i++)
213  av_fifo_drain(af->buf[i], size);
214  af->nb_samples -= nb_samples;
215  }
216  return 0;
217 }
218 
220 {
221  int i;
222 
223  for (i = 0; i < af->nb_buffers; i++)
224  av_fifo_reset(af->buf[i]);
225 
226  af->nb_samples = 0;
227 }
228 
230 {
231  return af->nb_samples;
232 }
233 
235 {
236  return af->allocated_samples - af->nb_samples;
237 }
#define NULL
Definition: coverity.c:32
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:182
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
memory handling functions
void av_audio_fifo_free(AVAudioFifo *af)
Free an AVAudioFifo.
Definition: audio_fifo.c:45
external API header
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
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
enum AVSampleFormat sample_fmt
sample format
Definition: audio_fifo.c:41
int allocated_samples
current allocated size, in samples
Definition: audio_fifo.c:38
void av_fifo_free(AVFifoBuffer *f)
Free an AVFifoBuffer.
Definition: fifo.c:55
ptrdiff_t size
Definition: opengl_enc.c:101
int av_sample_fmt_is_planar(enum AVSampleFormat sample_fmt)
Check if the sample format is planar.
Definition: samplefmt.c:110
int av_audio_fifo_space(AVAudioFifo *af)
Get the current number of samples in the AVAudioFifo available for writing.
Definition: audio_fifo.c:234
#define AVERROR(e)
Definition: error.h:43
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
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
Context for an Audio FIFO Buffer.
Definition: audio_fifo.c:34
int nb_buffers
number of buffers
Definition: audio_fifo.c:36
int av_audio_fifo_size(AVAudioFifo *af)
Get the current number of samples in the AVAudioFifo available for reading.
Definition: audio_fifo.c:229
int av_audio_fifo_realloc(AVAudioFifo *af, int nb_samples)
Reallocate an AVAudioFifo.
Definition: audio_fifo.c:97
#define FFMIN(a, b)
Definition: common.h:96
AVFifoBuffer ** buf
single buffer for interleaved, per-channel buffers for planar
Definition: audio_fifo.c:35
int nb_samples
number of samples currently in the FIFO
Definition: audio_fifo.c:37
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
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
int av_fifo_realloc2(AVFifoBuffer *f, unsigned int new_size)
Resize an AVFifoBuffer.
Definition: fifo.c:87
int channels
number of channels
Definition: audio_fifo.c:40
a very simple circular buffer FIFO implementation
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:50
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:117
int sample_size
size, in bytes, of one sample in a buffer
Definition: audio_fifo.c:42
int av_audio_fifo_write(AVAudioFifo *af, void **data, int nb_samples)
Write data to an AVAudioFifo.
Definition: audio_fifo.c:113
int av_audio_fifo_drain(AVAudioFifo *af, int nb_samples)
Drain data from an AVAudioFifo.
Definition: audio_fifo.c:202
common internal and external API header
#define av_free(p)
Audio FIFO Buffer.
AVFifoBuffer * av_fifo_alloc(unsigned int size)
Initialize an AVFifoBuffer.
Definition: fifo.c:43
int av_audio_fifo_peek(AVAudioFifo *af, void **data, int nb_samples)
Peek data from an AVAudioFifo.
Definition: audio_fifo.c:139
static void * av_mallocz_array(size_t nmemb, size_t size)
Definition: mem.h:229
#define av_freep(p)
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
void av_fifo_drain(AVFifoBuffer *f, int size)
Discard data from the FIFO.
Definition: fifo.c:233
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:252
int av_audio_fifo_peek_at(AVAudioFifo *af, void **data, int nb_samples, int offset)
Peek data from an AVAudioFifo.
Definition: audio_fifo.c:158
void av_audio_fifo_reset(AVAudioFifo *af)
Reset the AVAudioFifo buffer.
Definition: audio_fifo.c:219