Coverage Report

Created: 2026-02-14 06:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavutil/audio_fifo.c
Line
Count
Source
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
48
void av_audio_fifo_free(AVAudioFifo *af)
49
199k
{
50
199k
    if (af) {
51
191k
        if (af->buf) {
52
191k
            int i;
53
407k
            for (i = 0; i < af->nb_buffers; i++) {
54
215k
                av_fifo_freep2(&af->buf[i]);
55
215k
            }
56
191k
            av_freep(&af->buf);
57
191k
        }
58
191k
        av_free(af);
59
191k
    }
60
199k
}
61
62
AVAudioFifo *av_audio_fifo_alloc(enum AVSampleFormat sample_fmt, int channels,
63
                                 int nb_samples)
64
191k
{
65
191k
    AVAudioFifo *af;
66
191k
    int buf_size, i;
67
68
    /* get channel buffer size (also validates parameters) */
69
191k
    if (av_samples_get_buffer_size(&buf_size, channels, nb_samples, sample_fmt, 1) < 0)
70
0
        return NULL;
71
72
191k
    af = av_mallocz(sizeof(*af));
73
191k
    if (!af)
74
0
        return NULL;
75
76
191k
    af->channels    = channels;
77
191k
    af->sample_fmt  = sample_fmt;
78
191k
    af->sample_size = buf_size / nb_samples;
79
191k
    af->nb_buffers  = av_sample_fmt_is_planar(sample_fmt) ? channels : 1;
80
81
191k
    af->buf = av_calloc(af->nb_buffers, sizeof(*af->buf));
82
191k
    if (!af->buf)
83
0
        goto error;
84
85
407k
    for (i = 0; i < af->nb_buffers; i++) {
86
215k
        af->buf[i] = av_fifo_alloc2(buf_size, 1, 0);
87
215k
        if (!af->buf[i])
88
0
            goto error;
89
215k
    }
90
191k
    af->allocated_samples = nb_samples;
91
92
191k
    return af;
93
94
0
error:
95
0
    av_audio_fifo_free(af);
96
0
    return NULL;
97
191k
}
98
99
int av_audio_fifo_realloc(AVAudioFifo *af, int nb_samples)
100
5.01k
{
101
5.01k
    const size_t cur_size = av_fifo_can_read (af->buf[0]) +
102
5.01k
                            av_fifo_can_write(af->buf[0]);
103
5.01k
    int i, ret, buf_size;
104
105
5.01k
    if ((ret = av_samples_get_buffer_size(&buf_size, af->channels, nb_samples,
106
5.01k
                                          af->sample_fmt, 1)) < 0)
107
0
        return ret;
108
109
5.01k
    if (buf_size > cur_size) {
110
10.0k
        for (i = 0; i < af->nb_buffers; i++) {
111
5.02k
            if ((ret = av_fifo_grow2(af->buf[i], buf_size - cur_size)) < 0)
112
0
                return ret;
113
5.02k
        }
114
5.01k
    }
115
5.01k
    af->allocated_samples = nb_samples;
116
5.01k
    return 0;
117
5.01k
}
118
119
int av_audio_fifo_write(AVAudioFifo *af, void * const *data, int nb_samples)
120
100M
{
121
100M
    int i, ret, size;
122
123
    /* automatically reallocate buffers if needed */
124
100M
    if (av_audio_fifo_space(af) < nb_samples) {
125
5.01k
        int current_size = av_audio_fifo_size(af);
126
        /* check for integer overflow in new size calculation */
127
5.01k
        if (INT_MAX / 2 - current_size < nb_samples)
128
0
            return AVERROR(EINVAL);
129
        /* reallocate buffers */
130
5.01k
        if ((ret = av_audio_fifo_realloc(af, 2 * (current_size + nb_samples))) < 0)
131
0
            return ret;
132
5.01k
    }
133
134
100M
    size = nb_samples * af->sample_size;
135
201M
    for (i = 0; i < af->nb_buffers; i++) {
136
100M
        ret = av_fifo_write(af->buf[i], data[i], size);
137
100M
        if (ret < 0)
138
0
            return AVERROR_BUG;
139
100M
    }
140
100M
    af->nb_samples += nb_samples;
141
142
100M
    return nb_samples;
143
100M
}
144
145
int av_audio_fifo_peek(const AVAudioFifo *af, void * const *data, int nb_samples)
146
0
{
147
0
    return av_audio_fifo_peek_at(af, data, nb_samples, 0);
148
0
}
149
150
int av_audio_fifo_peek_at(const AVAudioFifo *af, void * const *data,
151
                          int nb_samples, int offset)
152
0
{
153
0
    int i, ret, size;
154
155
0
    if (offset < 0 || offset >= af->nb_samples)
156
0
        return AVERROR(EINVAL);
157
0
    if (nb_samples < 0)
158
0
        return AVERROR(EINVAL);
159
0
    nb_samples = FFMIN(nb_samples, af->nb_samples);
160
0
    if (!nb_samples)
161
0
        return 0;
162
0
    if (offset > af->nb_samples - nb_samples)
163
0
        return AVERROR(EINVAL);
164
165
0
    offset *= af->sample_size;
166
0
    size = nb_samples * af->sample_size;
167
0
    for (i = 0; i < af->nb_buffers; i++) {
168
0
        if ((ret = av_fifo_peek(af->buf[i], data[i], size, offset)) < 0)
169
0
            return AVERROR_BUG;
170
0
    }
171
172
0
    return nb_samples;
173
0
}
174
175
int av_audio_fifo_read(AVAudioFifo *af, void * const *data, int nb_samples)
176
2.89M
{
177
2.89M
    int i, size;
178
179
2.89M
    if (nb_samples < 0)
180
0
        return AVERROR(EINVAL);
181
2.89M
    nb_samples = FFMIN(nb_samples, af->nb_samples);
182
2.89M
    if (!nb_samples)
183
2.46M
        return 0;
184
185
425k
    size = nb_samples * af->sample_size;
186
853k
    for (i = 0; i < af->nb_buffers; i++) {
187
427k
        if (av_fifo_read(af->buf[i], data[i], size) < 0)
188
0
            return AVERROR_BUG;
189
427k
    }
190
425k
    af->nb_samples -= nb_samples;
191
192
425k
    return nb_samples;
193
425k
}
194
195
int av_audio_fifo_drain(AVAudioFifo *af, int nb_samples)
196
0
{
197
0
    int i, size;
198
199
0
    if (nb_samples < 0)
200
0
        return AVERROR(EINVAL);
201
0
    nb_samples = FFMIN(nb_samples, af->nb_samples);
202
203
0
    if (nb_samples) {
204
0
        size = nb_samples * af->sample_size;
205
0
        for (i = 0; i < af->nb_buffers; i++)
206
0
            av_fifo_drain2(af->buf[i], size);
207
0
        af->nb_samples -= nb_samples;
208
0
    }
209
0
    return 0;
210
0
}
211
212
void av_audio_fifo_reset(AVAudioFifo *af)
213
6.65M
{
214
6.65M
    int i;
215
216
13.6M
    for (i = 0; i < af->nb_buffers; i++)
217
7.00M
        av_fifo_reset2(af->buf[i]);
218
219
6.65M
    af->nb_samples = 0;
220
6.65M
}
221
222
int av_audio_fifo_size(AVAudioFifo *af)
223
14.3M
{
224
14.3M
    return af->nb_samples;
225
14.3M
}
226
227
int av_audio_fifo_space(AVAudioFifo *af)
228
100M
{
229
100M
    return af->allocated_samples - af->nb_samples;
230
100M
}