.github
CMakeModules
dist
externals
SDL
Vulkan-Headers
cmake-modules
cpp-httplib
cubeb
discord-rpc
dynarmic
ffmpeg
compat
doc
ffbuild
fftools
libavcodec
libavdevice
libavfilter
libavformat
libavresample
libavutil
aarch64
arm
avr32
bfin
mips
ppc
sh4
tests
tomi
x86
.gitignore
Makefile
adler32.c
adler32.h
aes.c
aes.h
aes_ctr.c
aes_ctr.h
aes_internal.h
attributes.h
audio_fifo.c
audio_fifo.h
avassert.h
avsscanf.c
avstring.c
avstring.h
avutil.h
avutilres.rc
base64.c
base64.h
blowfish.c
blowfish.h
bprint.c
bprint.h
bswap.h
buffer.c
buffer.h
buffer_internal.h
camellia.c
camellia.h
cast5.c
cast5.h
channel_layout.c
channel_layout.h
color_utils.c
color_utils.h
colorspace.h
common.h
cpu.c
cpu.h
cpu_internal.h
crc.c
crc.h
cuda_check.h
des.c
des.h
dict.c
dict.h
display.c
display.h
dovi_meta.c
dovi_meta.h
downmix_info.c
downmix_info.h
dynarray.h
encryption_info.c
encryption_info.h
error.c
error.h
eval.c
eval.h
ffmath.h
fifo.c
fifo.h
file.c
file.h
file_open.c
fixed_dsp.c
fixed_dsp.h
float_dsp.c
float_dsp.h
frame.c
frame.h
hash.c
hash.h
hdr_dynamic_metadata.c
hdr_dynamic_metadata.h
hmac.c
hmac.h
hwcontext.c
hwcontext.h
hwcontext_cuda.c
hwcontext_cuda.h
hwcontext_cuda_internal.h
hwcontext_d3d11va.c
hwcontext_d3d11va.h
hwcontext_drm.c
hwcontext_drm.h
hwcontext_dxva2.c
hwcontext_dxva2.h
hwcontext_internal.h
hwcontext_mediacodec.c
hwcontext_mediacodec.h
hwcontext_opencl.c
hwcontext_opencl.h
hwcontext_qsv.c
hwcontext_qsv.h
hwcontext_vaapi.c
hwcontext_vaapi.h
hwcontext_vdpau.c
hwcontext_vdpau.h
hwcontext_videotoolbox.c
hwcontext_videotoolbox.h
hwcontext_vulkan.c
hwcontext_vulkan.h
imgutils.c
imgutils.h
imgutils_internal.h
integer.c
integer.h
internal.h
intfloat.h
intmath.c
intmath.h
intreadwrite.h
lfg.c
lfg.h
libavutil.v
libm.h
lls.c
lls.h
log.c
log.h
log2_tab.c
lzo.c
lzo.h
macros.h
mastering_display_metadata.c
mastering_display_metadata.h
mathematics.c
mathematics.h
md5.c
md5.h
mem.c
mem.h
mem_internal.h
motion_vector.h
murmur3.c
murmur3.h
opt.c
opt.h
parseutils.c
parseutils.h
pca.c
pca.h
pixdesc.c
pixdesc.h
pixelutils.c
pixelutils.h
pixfmt.h
qsort.h
random_seed.c
random_seed.h
rational.c
rational.h
rc4.c
rc4.h
replaygain.h
reverse.c
reverse.h
ripemd.c
ripemd.h
samplefmt.c
samplefmt.h
sha.c
sha.h
sha512.c
sha512.h
slicethread.c
slicethread.h
softfloat.h
softfloat_ieee754.h
softfloat_tables.h
spherical.c
spherical.h
stereo3d.c
stereo3d.h
tablegen.h
tea.c
tea.h
thread.h
threadmessage.c
threadmessage.h
time.c
time.h
time_internal.h
timecode.c
timecode.h
timer.h
timestamp.h
tree.c
tree.h
twofish.c
twofish.h
tx.c
tx.h
tx_double.c
tx_float.c
tx_int32.c
tx_priv.h
tx_template.c
utils.c
version.h
video_enc_params.c
video_enc_params.h
wchar_filename.h
xga_font_data.c
xga_font_data.h
xtea.c
xtea.h
libpostproc
libswresample
libswscale
presets
tests
tools
.gitattributes
.gitignore
.mailmap
.travis.yml
CONTRIBUTING.md
COPYING.GPLv2
COPYING.GPLv3
COPYING.LGPLv2.1
COPYING.LGPLv3
CREDITS
Changelog
INSTALL.md
LICENSE.md
MAINTAINERS
Makefile
README.md
RELEASE
RELEASE_NOTES
configure
find-modules
getopt
glad
httplib
inih
libressl
libusb
libzip
mbedtls
microprofile
opus
sirit
soundtouch
xbyak
CMakeLists.txt
patches
src
CMakeLists.txt
LICENSE
README.md
license.txt
237 lines
6.3 KiB
C
237 lines
6.3 KiB
C
![]() |
/*
|
||
|
* Audio FIFO
|
||
|
* Copyright (c) 2012 Justin Ruggles <justin.ruggles@gmail.com>
|
||
|
*
|
||
|
* This file is part of FFmpeg.
|
||
|
*
|
||
|
* FFmpeg is free software; you can redistribute it and/or
|
||
|
* 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.
|
||
|
*
|
||
|
* FFmpeg is distributed in the hope that it will be useful,
|
||
|
* 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
|
||
|
* License along with FFmpeg; if not, write to the Free Software
|
||
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||
|
*/
|
||
|
|
||
|
/**
|
||
|
* @file
|
||
|
* Audio FIFO
|
||
|
*/
|
||
|
|
||
|
#include "avutil.h"
|
||
|
#include "audio_fifo.h"
|
||
|
#include "common.h"
|
||
|
#include "fifo.h"
|
||
|
#include "mem.h"
|
||
|
#include "samplefmt.h"
|
||
|
|
||
|
struct AVAudioFifo {
|
||
|
AVFifoBuffer **buf; /**< single buffer for interleaved, per-channel buffers for planar */
|
||
|
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++) {
|
||
|
av_fifo_freep(&af->buf[i]);
|
||
|
}
|
||
|
av_freep(&af->buf);
|
||
|
}
|
||
|
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;
|
||
|
|
||
|
af->buf = av_mallocz_array(af->nb_buffers, sizeof(*af->buf));
|
||
|
if (!af->buf)
|
||
|
goto error;
|
||
|
|
||
|
for (i = 0; i < af->nb_buffers; i++) {
|
||
|
af->buf[i] = av_fifo_alloc(buf_size);
|
||
|
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)
|
||
|
{
|
||
|
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;
|
||
|
|
||
|
for (i = 0; i < af->nb_buffers; i++) {
|
||
|
if ((ret = av_fifo_realloc2(af->buf[i], buf_size)) < 0)
|
||
|
return ret;
|
||
|
}
|
||
|
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++) {
|
||
|
ret = av_fifo_generic_write(af->buf[i], data[i], size, NULL);
|
||
|
if (ret != size)
|
||
|
return AVERROR_BUG;
|
||
|
}
|
||
|
af->nb_samples += nb_samples;
|
||
|
|
||
|
return nb_samples;
|
||
|
}
|
||
|
|
||
|
int av_audio_fifo_peek(AVAudioFifo *af, void **data, int nb_samples)
|
||
|
{
|
||
|
int i, ret, size;
|
||
|
|
||
|
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++) {
|
||
|
if ((ret = av_fifo_generic_peek(af->buf[i], data[i], size, NULL)) < 0)
|
||
|
return AVERROR_BUG;
|
||
|
}
|
||
|
|
||
|
return nb_samples;
|
||
|
}
|
||
|
|
||
|
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++) {
|
||
|
if ((ret = av_fifo_generic_peek_at(af->buf[i], data[i], offset, size, NULL)) < 0)
|
||
|
return AVERROR_BUG;
|
||
|
}
|
||
|
|
||
|
return nb_samples;
|
||
|
}
|
||
|
|
||
|
int av_audio_fifo_read(AVAudioFifo *af, void **data, int nb_samples)
|
||
|
{
|
||
|
int i, size;
|
||
|
|
||
|
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++) {
|
||
|
if (av_fifo_generic_read(af->buf[i], data[i], size, NULL) < 0)
|
||
|
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++)
|
||
|
av_fifo_drain(af->buf[i], size);
|
||
|
af->nb_samples -= nb_samples;
|
||
|
}
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
void av_audio_fifo_reset(AVAudioFifo *af)
|
||
|
{
|
||
|
int i;
|
||
|
|
||
|
for (i = 0; i < af->nb_buffers; i++)
|
||
|
av_fifo_reset(af->buf[i]);
|
||
|
|
||
|
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;
|
||
|
}
|