3 * Copyright (c) 2012 Justin Ruggles <justin.ruggles@gmail.com>
5 * This file is part of FFmpeg.
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.
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.
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
28 #include "audio_fifo.h"
32 #include "samplefmt.h"
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 */
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 */
45 void av_audio_fifo_free(AVAudioFifo *af)
50 for (i = 0; i < af->nb_buffers; i++) {
51 av_fifo_freep(&af->buf[i]);
59 AVAudioFifo *av_audio_fifo_alloc(enum AVSampleFormat sample_fmt, int channels,
65 /* get channel buffer size (also validates parameters) */
66 if (av_samples_get_buffer_size(&buf_size, channels, nb_samples, sample_fmt, 1) < 0)
69 af = av_mallocz(sizeof(*af));
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;
78 af->buf = av_mallocz_array(af->nb_buffers, sizeof(*af->buf));
82 for (i = 0; i < af->nb_buffers; i++) {
83 af->buf[i] = av_fifo_alloc(buf_size);
87 af->allocated_samples = nb_samples;
92 av_audio_fifo_free(af);
96 int av_audio_fifo_realloc(AVAudioFifo *af, int nb_samples)
100 if ((ret = av_samples_get_buffer_size(&buf_size, af->channels, nb_samples,
101 af->sample_fmt, 1)) < 0)
104 for (i = 0; i < af->nb_buffers; i++) {
105 if ((ret = av_fifo_realloc2(af->buf[i], buf_size)) < 0)
108 af->allocated_samples = nb_samples;
112 int av_audio_fifo_write(AVAudioFifo *af, void **data, int nb_samples)
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)
127 size = nb_samples * af->sample_size;
128 for (i = 0; i < af->nb_buffers; i++) {
129 ret = av_fifo_generic_write(af->buf[i], data[i], size, NULL);
133 af->nb_samples += nb_samples;
138 int av_audio_fifo_peek(AVAudioFifo *af, void **data, int nb_samples)
143 return AVERROR(EINVAL);
144 nb_samples = FFMIN(nb_samples, af->nb_samples);
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)
157 int av_audio_fifo_peek_at(AVAudioFifo *af, void **data, int nb_samples, int offset)
161 if (offset < 0 || offset >= af->nb_samples)
162 return AVERROR(EINVAL);
164 return AVERROR(EINVAL);
165 nb_samples = FFMIN(nb_samples, af->nb_samples);
168 if (offset > af->nb_samples - nb_samples)
169 return AVERROR(EINVAL);
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)
181 int av_audio_fifo_read(AVAudioFifo *af, void **data, int nb_samples)
186 return AVERROR(EINVAL);
187 nb_samples = FFMIN(nb_samples, af->nb_samples);
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)
196 af->nb_samples -= nb_samples;
201 int av_audio_fifo_drain(AVAudioFifo *af, int nb_samples)
206 return AVERROR(EINVAL);
207 nb_samples = FFMIN(nb_samples, af->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;
218 void av_audio_fifo_reset(AVAudioFifo *af)
222 for (i = 0; i < af->nb_buffers; i++)
223 av_fifo_reset(af->buf[i]);
228 int av_audio_fifo_size(AVAudioFifo *af)
230 return af->nb_samples;
233 int av_audio_fifo_space(AVAudioFifo *af)
235 return af->allocated_samples - af->nb_samples;