2012-03-23 21:45:48 +00:00
|
|
|
/*
|
|
|
|
* Audio FIFO
|
|
|
|
* Copyright (c) 2012 Justin Ruggles <justin.ruggles@gmail.com>
|
|
|
|
*
|
2013-08-15 21:12:51 +00:00
|
|
|
* This file is part of FFmpeg.
|
2012-03-23 21:45:48 +00:00
|
|
|
*
|
2013-08-15 21:12:51 +00:00
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2012-03-23 21:45:48 +00:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
2013-08-15 21:12:51 +00:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2012-03-23 21:45:48 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2013-08-15 21:12:51 +00:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2012-03-23 21:45:48 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* Audio FIFO
|
|
|
|
*/
|
|
|
|
|
2021-07-23 09:13:56 +00:00
|
|
|
#include <limits.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
|
2012-03-23 21:45:48 +00:00
|
|
|
#include "audio_fifo.h"
|
2021-07-23 09:13:56 +00:00
|
|
|
#include "error.h"
|
2012-03-23 21:45:48 +00:00
|
|
|
#include "fifo.h"
|
2021-07-23 09:13:56 +00:00
|
|
|
#include "macros.h"
|
2012-03-23 21:45:48 +00:00
|
|
|
#include "mem.h"
|
|
|
|
#include "samplefmt.h"
|
|
|
|
|
|
|
|
struct AVAudioFifo {
|
2022-01-07 10:41:42 +00:00
|
|
|
AVFifo **buf; /**< single buffer for interleaved, per-channel buffers for planar */
|
2012-03-23 21:45:48 +00:00
|
|
|
int nb_buffers; /**< number of buffers */
|
|
|
|
int nb_samples; /**< number of samples currently in the FIFO */
|
|
|
|
int allocated_samples; /**< current allocated size, in samples */
|
|
|
|
|
|
|
|
int channels; /**< number of channels */
|
|
|
|
enum AVSampleFormat sample_fmt; /**< sample format */
|
|
|
|
int sample_size; /**< size, in bytes, of one sample in a buffer */
|
|
|
|
};
|
|
|
|
|
|
|
|
void av_audio_fifo_free(AVAudioFifo *af)
|
|
|
|
{
|
|
|
|
if (af) {
|
|
|
|
if (af->buf) {
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < af->nb_buffers; i++) {
|
2022-01-07 10:41:42 +00:00
|
|
|
av_fifo_freep2(&af->buf[i]);
|
2012-03-23 21:45:48 +00:00
|
|
|
}
|
2014-12-27 14:49:13 +00:00
|
|
|
av_freep(&af->buf);
|
2012-03-23 21:45:48 +00:00
|
|
|
}
|
|
|
|
av_free(af);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AVAudioFifo *av_audio_fifo_alloc(enum AVSampleFormat sample_fmt, int channels,
|
|
|
|
int nb_samples)
|
|
|
|
{
|
|
|
|
AVAudioFifo *af;
|
|
|
|
int buf_size, i;
|
|
|
|
|
|
|
|
/* get channel buffer size (also validates parameters) */
|
|
|
|
if (av_samples_get_buffer_size(&buf_size, channels, nb_samples, sample_fmt, 1) < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
af = av_mallocz(sizeof(*af));
|
|
|
|
if (!af)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
af->channels = channels;
|
|
|
|
af->sample_fmt = sample_fmt;
|
|
|
|
af->sample_size = buf_size / nb_samples;
|
|
|
|
af->nb_buffers = av_sample_fmt_is_planar(sample_fmt) ? channels : 1;
|
|
|
|
|
2021-09-14 19:31:53 +00:00
|
|
|
af->buf = av_calloc(af->nb_buffers, sizeof(*af->buf));
|
2012-03-23 21:45:48 +00:00
|
|
|
if (!af->buf)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
for (i = 0; i < af->nb_buffers; i++) {
|
2022-01-07 10:41:42 +00:00
|
|
|
af->buf[i] = av_fifo_alloc2(buf_size, 1, 0);
|
2012-03-23 21:45:48 +00:00
|
|
|
if (!af->buf[i])
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
af->allocated_samples = nb_samples;
|
|
|
|
|
|
|
|
return af;
|
|
|
|
|
|
|
|
error:
|
|
|
|
av_audio_fifo_free(af);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int av_audio_fifo_realloc(AVAudioFifo *af, int nb_samples)
|
|
|
|
{
|
2022-01-07 10:41:42 +00:00
|
|
|
const size_t cur_size = av_fifo_can_read (af->buf[0]) +
|
|
|
|
av_fifo_can_write(af->buf[0]);
|
2012-03-23 21:45:48 +00:00
|
|
|
int i, ret, buf_size;
|
|
|
|
|
|
|
|
if ((ret = av_samples_get_buffer_size(&buf_size, af->channels, nb_samples,
|
|
|
|
af->sample_fmt, 1)) < 0)
|
|
|
|
return ret;
|
|
|
|
|
2022-01-07 10:41:42 +00:00
|
|
|
if (buf_size > cur_size) {
|
|
|
|
for (i = 0; i < af->nb_buffers; i++) {
|
|
|
|
if ((ret = av_fifo_grow2(af->buf[i], buf_size - cur_size)) < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
2012-03-23 21:45:48 +00:00
|
|
|
}
|
|
|
|
af->allocated_samples = nb_samples;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int av_audio_fifo_write(AVAudioFifo *af, void **data, int nb_samples)
|
|
|
|
{
|
|
|
|
int i, ret, size;
|
|
|
|
|
|
|
|
/* automatically reallocate buffers if needed */
|
|
|
|
if (av_audio_fifo_space(af) < nb_samples) {
|
|
|
|
int current_size = av_audio_fifo_size(af);
|
|
|
|
/* check for integer overflow in new size calculation */
|
|
|
|
if (INT_MAX / 2 - current_size < nb_samples)
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
/* reallocate buffers */
|
|
|
|
if ((ret = av_audio_fifo_realloc(af, 2 * (current_size + nb_samples))) < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
size = nb_samples * af->sample_size;
|
|
|
|
for (i = 0; i < af->nb_buffers; i++) {
|
2022-01-07 10:41:42 +00:00
|
|
|
ret = av_fifo_write(af->buf[i], data[i], size);
|
|
|
|
if (ret < 0)
|
2012-03-23 21:45:48 +00:00
|
|
|
return AVERROR_BUG;
|
|
|
|
}
|
|
|
|
af->nb_samples += nb_samples;
|
|
|
|
|
|
|
|
return nb_samples;
|
|
|
|
}
|
|
|
|
|
2015-08-04 12:41:35 +00:00
|
|
|
int av_audio_fifo_peek(AVAudioFifo *af, void **data, int nb_samples)
|
|
|
|
{
|
2022-01-07 10:41:42 +00:00
|
|
|
return av_audio_fifo_peek_at(af, data, nb_samples, 0);
|
2015-08-04 12:41:35 +00:00
|
|
|
}
|
|
|
|
|
2016-02-11 21:05:54 +00:00
|
|
|
int av_audio_fifo_peek_at(AVAudioFifo *af, void **data, int nb_samples, int offset)
|
|
|
|
{
|
|
|
|
int i, ret, size;
|
|
|
|
|
|
|
|
if (offset < 0 || offset >= af->nb_samples)
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
if (nb_samples < 0)
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
nb_samples = FFMIN(nb_samples, af->nb_samples);
|
|
|
|
if (!nb_samples)
|
|
|
|
return 0;
|
|
|
|
if (offset > af->nb_samples - nb_samples)
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
|
|
|
|
offset *= af->sample_size;
|
|
|
|
size = nb_samples * af->sample_size;
|
|
|
|
for (i = 0; i < af->nb_buffers; i++) {
|
2022-01-07 10:41:42 +00:00
|
|
|
if ((ret = av_fifo_peek(af->buf[i], data[i], size, offset)) < 0)
|
2016-02-11 21:05:54 +00:00
|
|
|
return AVERROR_BUG;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nb_samples;
|
|
|
|
}
|
|
|
|
|
2012-03-23 21:45:48 +00:00
|
|
|
int av_audio_fifo_read(AVAudioFifo *af, void **data, int nb_samples)
|
|
|
|
{
|
2015-03-12 23:38:07 +00:00
|
|
|
int i, size;
|
2012-03-23 21:45:48 +00:00
|
|
|
|
|
|
|
if (nb_samples < 0)
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
nb_samples = FFMIN(nb_samples, af->nb_samples);
|
|
|
|
if (!nb_samples)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
size = nb_samples * af->sample_size;
|
|
|
|
for (i = 0; i < af->nb_buffers; i++) {
|
2022-01-07 10:41:42 +00:00
|
|
|
if (av_fifo_read(af->buf[i], data[i], size) < 0)
|
2012-03-23 21:45:48 +00:00
|
|
|
return AVERROR_BUG;
|
|
|
|
}
|
|
|
|
af->nb_samples -= nb_samples;
|
|
|
|
|
|
|
|
return nb_samples;
|
|
|
|
}
|
|
|
|
|
|
|
|
int av_audio_fifo_drain(AVAudioFifo *af, int nb_samples)
|
|
|
|
{
|
|
|
|
int i, size;
|
|
|
|
|
|
|
|
if (nb_samples < 0)
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
nb_samples = FFMIN(nb_samples, af->nb_samples);
|
|
|
|
|
|
|
|
if (nb_samples) {
|
|
|
|
size = nb_samples * af->sample_size;
|
|
|
|
for (i = 0; i < af->nb_buffers; i++)
|
2022-01-07 10:41:42 +00:00
|
|
|
av_fifo_drain2(af->buf[i], size);
|
2012-03-23 21:45:48 +00:00
|
|
|
af->nb_samples -= nb_samples;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void av_audio_fifo_reset(AVAudioFifo *af)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < af->nb_buffers; i++)
|
2022-01-07 10:41:42 +00:00
|
|
|
av_fifo_reset2(af->buf[i]);
|
2012-03-23 21:45:48 +00:00
|
|
|
|
|
|
|
af->nb_samples = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int av_audio_fifo_size(AVAudioFifo *af)
|
|
|
|
{
|
|
|
|
return af->nb_samples;
|
|
|
|
}
|
|
|
|
|
|
|
|
int av_audio_fifo_space(AVAudioFifo *af)
|
|
|
|
{
|
|
|
|
return af->allocated_samples - af->nb_samples;
|
|
|
|
}
|