]> git.sesse.net Git - ffmpeg/blob - libavutil/audio_fifo.c
97c51a72c13a0b6ce0ca9b3e3df9d64651154281
[ffmpeg] / libavutil / audio_fifo.c
1 /*
2  * Audio FIFO
3  * Copyright (c) 2012 Justin Ruggles <justin.ruggles@gmail.com>
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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  * Libav 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 Libav; 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 "avutil.h"
28 #include "audio_fifo.h"
29 #include "fifo.h"
30 #include "mem.h"
31 #include "samplefmt.h"
32
33 struct AVAudioFifo {
34     AVFifoBuffer **buf;             /**< single buffer for interleaved, per-channel buffers for planar */
35     int nb_buffers;                 /**< number of buffers */
36     int nb_samples;                 /**< number of samples currently in the FIFO */
37     int allocated_samples;          /**< current allocated size, in samples */
38
39     int channels;                   /**< number of channels */
40     enum AVSampleFormat sample_fmt; /**< sample format */
41     int sample_size;                /**< size, in bytes, of one sample in a buffer */
42 };
43
44 void av_audio_fifo_free(AVAudioFifo *af)
45 {
46     if (af) {
47         if (af->buf) {
48             int i;
49             for (i = 0; i < af->nb_buffers; i++) {
50                 if (af->buf[i])
51                     av_fifo_free(af->buf[i]);
52             }
53             av_free(af->buf);
54         }
55         av_free(af);
56     }
57 }
58
59 AVAudioFifo *av_audio_fifo_alloc(enum AVSampleFormat sample_fmt, int channels,
60                                  int nb_samples)
61 {
62     AVAudioFifo *af;
63     int buf_size, i;
64
65     /* get channel buffer size (also validates parameters) */
66     if (av_samples_get_buffer_size(&buf_size, channels, nb_samples, sample_fmt, 1) < 0)
67         return NULL;
68
69     af = av_mallocz(sizeof(*af));
70     if (!af)
71         return NULL;
72
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;
77
78     af->buf = av_mallocz(af->nb_buffers * sizeof(*af->buf));
79     if (!af->buf)
80         goto error;
81
82     for (i = 0; i < af->nb_buffers; i++) {
83         af->buf[i] = av_fifo_alloc(buf_size);
84         if (!af->buf[i])
85             goto error;
86     }
87     af->allocated_samples = nb_samples;
88
89     return af;
90
91 error:
92     av_audio_fifo_free(af);
93     return NULL;
94 }
95
96 int av_audio_fifo_realloc(AVAudioFifo *af, int nb_samples)
97 {
98     int i, ret, buf_size;
99
100     if ((ret = av_samples_get_buffer_size(&buf_size, af->channels, nb_samples,
101                                           af->sample_fmt, 1)) < 0)
102         return ret;
103
104     for (i = 0; i < af->nb_buffers; i++) {
105         if ((ret = av_fifo_realloc2(af->buf[i], buf_size)) < 0)
106             return ret;
107     }
108     af->allocated_samples = nb_samples;
109     return 0;
110 }
111
112 int av_audio_fifo_write(AVAudioFifo *af, void **data, int nb_samples)
113 {
114     int i, ret, size;
115
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)
124             return ret;
125     }
126
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);
130         if (ret != size)
131             return AVERROR_BUG;
132     }
133     af->nb_samples += nb_samples;
134
135     return nb_samples;
136 }
137
138 int av_audio_fifo_read(AVAudioFifo *af, void **data, int nb_samples)
139 {
140     int i, ret, size;
141
142     if (nb_samples < 0)
143         return AVERROR(EINVAL);
144     nb_samples = FFMIN(nb_samples, af->nb_samples);
145     if (!nb_samples)
146         return 0;
147
148     size = nb_samples * af->sample_size;
149     for (i = 0; i < af->nb_buffers; i++) {
150         if ((ret = av_fifo_generic_read(af->buf[i], data[i], size, NULL)) < 0)
151             return AVERROR_BUG;
152     }
153     af->nb_samples -= nb_samples;
154
155     return nb_samples;
156 }
157
158 int av_audio_fifo_drain(AVAudioFifo *af, int nb_samples)
159 {
160     int i, size;
161
162     if (nb_samples < 0)
163         return AVERROR(EINVAL);
164     nb_samples = FFMIN(nb_samples, af->nb_samples);
165
166     if (nb_samples) {
167         size = nb_samples * af->sample_size;
168         for (i = 0; i < af->nb_buffers; i++)
169             av_fifo_drain(af->buf[i], size);
170         af->nb_samples -= nb_samples;
171     }
172     return 0;
173 }
174
175 void av_audio_fifo_reset(AVAudioFifo *af)
176 {
177     int i;
178
179     for (i = 0; i < af->nb_buffers; i++)
180         av_fifo_reset(af->buf[i]);
181
182     af->nb_samples = 0;
183 }
184
185 int av_audio_fifo_size(AVAudioFifo *af)
186 {
187     return af->nb_samples;
188 }
189
190 int av_audio_fifo_space(AVAudioFifo *af)
191 {
192     return af->allocated_samples - af->nb_samples;
193 }