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 <limits.h>
28 #include <stddef.h>
29 
30 #include "audio_fifo.h"
31 #include "error.h"
32 #include "fifo.h"
33 #include "macros.h"
34 #include "mem.h"
35 #include "samplefmt.h"
36 
37 struct AVAudioFifo {
38  AVFifo **buf; /**< single buffer for interleaved, per-channel buffers for planar */
39  int nb_buffers; /**< number of buffers */
40  int nb_samples; /**< number of samples currently in the FIFO */
41  int allocated_samples; /**< current allocated size, in samples */
42 
43  int channels; /**< number of channels */
44  enum AVSampleFormat sample_fmt; /**< sample format */
45  int sample_size; /**< size, in bytes, of one sample in a buffer */
46 };
47 
49 {
50  if (af) {
51  if (af->buf) {
52  int i;
53  for (i = 0; i < af->nb_buffers; i++) {
54  av_fifo_freep2(&af->buf[i]);
55  }
56  av_freep(&af->buf);
57  }
58  av_free(af);
59  }
60 }
61 
63  int nb_samples)
64 {
65  AVAudioFifo *af;
66  int buf_size, i;
67 
68  /* get channel buffer size (also validates parameters) */
69  if (av_samples_get_buffer_size(&buf_size, channels, nb_samples, sample_fmt, 1) < 0)
70  return NULL;
71 
72  af = av_mallocz(sizeof(*af));
73  if (!af)
74  return NULL;
75 
76  af->channels = channels;
77  af->sample_fmt = sample_fmt;
78  af->sample_size = buf_size / nb_samples;
79  af->nb_buffers = av_sample_fmt_is_planar(sample_fmt) ? channels : 1;
80 
81  af->buf = av_calloc(af->nb_buffers, sizeof(*af->buf));
82  if (!af->buf)
83  goto error;
84 
85  for (i = 0; i < af->nb_buffers; i++) {
86  af->buf[i] = av_fifo_alloc2(buf_size, 1, 0);
87  if (!af->buf[i])
88  goto error;
89  }
90  af->allocated_samples = nb_samples;
91 
92  return af;
93 
94 error:
96  return NULL;
97 }
98 
99 int av_audio_fifo_realloc(AVAudioFifo *af, int nb_samples)
100 {
101  const size_t cur_size = av_fifo_can_read (af->buf[0]) +
102  av_fifo_can_write(af->buf[0]);
103  int i, ret, buf_size;
104 
105  if ((ret = av_samples_get_buffer_size(&buf_size, af->channels, nb_samples,
106  af->sample_fmt, 1)) < 0)
107  return ret;
108 
109  if (buf_size > cur_size) {
110  for (i = 0; i < af->nb_buffers; i++) {
111  if ((ret = av_fifo_grow2(af->buf[i], buf_size - cur_size)) < 0)
112  return ret;
113  }
114  }
115  af->allocated_samples = nb_samples;
116  return 0;
117 }
118 
119 int av_audio_fifo_write(AVAudioFifo *af, void * const *data, int nb_samples)
120 {
121  int i, ret, size;
122 
123  /* automatically reallocate buffers if needed */
124  if (av_audio_fifo_space(af) < nb_samples) {
125  int current_size = av_audio_fifo_size(af);
126  /* check for integer overflow in new size calculation */
127  if (INT_MAX / 2 - current_size < nb_samples)
128  return AVERROR(EINVAL);
129  /* reallocate buffers */
130  if ((ret = av_audio_fifo_realloc(af, 2 * (current_size + nb_samples))) < 0)
131  return ret;
132  }
133 
134  size = nb_samples * af->sample_size;
135  for (i = 0; i < af->nb_buffers; i++) {
136  ret = av_fifo_write(af->buf[i], data[i], size);
137  if (ret < 0)
138  return AVERROR_BUG;
139  }
140  af->nb_samples += nb_samples;
141 
142  return nb_samples;
143 }
144 
145 int av_audio_fifo_peek(const AVAudioFifo *af, void * const *data, int nb_samples)
146 {
147  return av_audio_fifo_peek_at(af, data, nb_samples, 0);
148 }
149 
150 int av_audio_fifo_peek_at(const AVAudioFifo *af, void * const *data,
151  int nb_samples, int offset)
152 {
153  int i, ret, size;
154 
155  if (offset < 0 || offset >= af->nb_samples)
156  return AVERROR(EINVAL);
157  if (nb_samples < 0)
158  return AVERROR(EINVAL);
159  nb_samples = FFMIN(nb_samples, af->nb_samples);
160  if (!nb_samples)
161  return 0;
162  if (offset > af->nb_samples - nb_samples)
163  return AVERROR(EINVAL);
164 
165  offset *= af->sample_size;
166  size = nb_samples * af->sample_size;
167  for (i = 0; i < af->nb_buffers; i++) {
168  if ((ret = av_fifo_peek(af->buf[i], data[i], size, offset)) < 0)
169  return AVERROR_BUG;
170  }
171 
172  return nb_samples;
173 }
174 
175 int av_audio_fifo_read(AVAudioFifo *af, void * const *data, int nb_samples)
176 {
177  int i, size;
178 
179  if (nb_samples < 0)
180  return AVERROR(EINVAL);
181  nb_samples = FFMIN(nb_samples, af->nb_samples);
182  if (!nb_samples)
183  return 0;
184 
185  size = nb_samples * af->sample_size;
186  for (i = 0; i < af->nb_buffers; i++) {
187  if (av_fifo_read(af->buf[i], data[i], size) < 0)
188  return AVERROR_BUG;
189  }
190  af->nb_samples -= nb_samples;
191 
192  return nb_samples;
193 }
194 
195 int av_audio_fifo_drain(AVAudioFifo *af, int nb_samples)
196 {
197  int i, size;
198 
199  if (nb_samples < 0)
200  return AVERROR(EINVAL);
201  nb_samples = FFMIN(nb_samples, af->nb_samples);
202 
203  if (nb_samples) {
204  size = nb_samples * af->sample_size;
205  for (i = 0; i < af->nb_buffers; i++)
206  av_fifo_drain2(af->buf[i], size);
207  af->nb_samples -= nb_samples;
208  }
209  return 0;
210 }
211 
213 {
214  int i;
215 
216  for (i = 0; i < af->nb_buffers; i++)
217  av_fifo_reset2(af->buf[i]);
218 
219  af->nb_samples = 0;
220 }
221 
223 {
224  return af->nb_samples;
225 }
226 
228 {
229  return af->allocated_samples - af->nb_samples;
230 }
error
static void error(const char *err)
Definition: target_bsf_fuzzer.c:32
av_audio_fifo_free
void av_audio_fifo_free(AVAudioFifo *af)
Free an AVAudioFifo.
Definition: audio_fifo.c:48
av_fifo_drain2
void av_fifo_drain2(AVFifo *f, size_t size)
Discard the specified amount of data from an AVFifo.
Definition: fifo.c:266
av_fifo_can_write
size_t av_fifo_can_write(const AVFifo *f)
Definition: fifo.c:94
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_audio_fifo_write
int av_audio_fifo_write(AVAudioFifo *af, void *const *data, int nb_samples)
Write data to an AVAudioFifo.
Definition: audio_fifo.c:119
av_audio_fifo_realloc
int av_audio_fifo_realloc(AVAudioFifo *af, int nb_samples)
Reallocate an AVAudioFifo.
Definition: audio_fifo.c:99
av_fifo_peek
int av_fifo_peek(const AVFifo *f, void *buf, size_t nb_elems, size_t offset)
Read data from a FIFO without modifying FIFO state.
Definition: fifo.c:255
data
const char data[16]
Definition: mxf.c:148
AVAudioFifo::nb_samples
int nb_samples
number of samples currently in the FIFO
Definition: audio_fifo.c:40
fifo.h
AVAudioFifo
Context for an Audio FIFO Buffer.
Definition: audio_fifo.c:37
av_audio_fifo_drain
int av_audio_fifo_drain(AVAudioFifo *af, int nb_samples)
Drain data from an AVAudioFifo.
Definition: audio_fifo.c:195
macros.h
av_fifo_write
int av_fifo_write(AVFifo *f, const void *buf, size_t nb_elems)
Write data into a FIFO.
Definition: fifo.c:188
av_fifo_grow2
int av_fifo_grow2(AVFifo *f, size_t inc)
Enlarge an AVFifo.
Definition: fifo.c:99
samplefmt.h
AVAudioFifo::channels
int channels
number of channels
Definition: audio_fifo.c:43
AVAudioFifo::sample_size
int sample_size
size, in bytes, of one sample in a buffer
Definition: audio_fifo.c:45
av_fifo_read
int av_fifo_read(AVFifo *f, void *buf, size_t nb_elems)
Read data from a FIFO.
Definition: fifo.c:240
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:114
channels
channels
Definition: aptx.h:31
limits.h
AVAudioFifo::buf
AVFifo ** buf
single buffer for interleaved, per-channel buffers for planar
Definition: audio_fifo.c:38
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:62
av_fifo_can_read
size_t av_fifo_can_read(const AVFifo *f)
Definition: fifo.c:87
error.h
av_fifo_reset2
void av_fifo_reset2(AVFifo *f)
Definition: fifo.c:280
AVFifo
Definition: fifo.c:35
av_audio_fifo_read
int av_audio_fifo_read(AVAudioFifo *af, void *const *data, int nb_samples)
Read data from an AVAudioFifo.
Definition: audio_fifo.c:175
size
int size
Definition: twinvq_data.h:10344
av_audio_fifo_peek
int av_audio_fifo_peek(const AVAudioFifo *af, void *const *data, int nb_samples)
Peek data from an AVAudioFifo.
Definition: audio_fifo.c:145
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:222
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:55
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
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:256
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:121
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:264
ret
ret
Definition: filter_design.txt:187
av_fifo_alloc2
AVFifo * av_fifo_alloc2(size_t nb_elems, size_t elem_size, unsigned int flags)
Allocate and initialize an AVFifo with a given element size.
Definition: fifo.c:47
AVAudioFifo::sample_fmt
enum AVSampleFormat sample_fmt
sample format
Definition: audio_fifo.c:44
AVAudioFifo::allocated_samples
int allocated_samples
current allocated size, in samples
Definition: audio_fifo.c:41
mem.h
AVAudioFifo::nb_buffers
int nb_buffers
number of buffers
Definition: audio_fifo.c:39
av_free
#define av_free(p)
Definition: tableprint_vlc.h:33
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
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:227
AVERROR_BUG
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:52
av_fifo_freep2
void av_fifo_freep2(AVFifo **f)
Free an AVFifo and reset pointer to NULL.
Definition: fifo.c:286
av_audio_fifo_reset
void av_audio_fifo_reset(AVAudioFifo *af)
Reset the AVAudioFifo buffer.
Definition: audio_fifo.c:212
av_audio_fifo_peek_at
int av_audio_fifo_peek_at(const AVAudioFifo *af, void *const *data, int nb_samples, int offset)
Peek data from an AVAudioFifo.
Definition: audio_fifo.c:150