2 * Copyright (c) 2012 Justin Ruggles <justin.ruggles@gmail.com>
4 * This file is part of Libav.
6 * Libav is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * Libav is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with Libav; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "libavutil/mem.h"
25 #include "audio_data.h"
27 static const AVClass audio_data_class = {
28 .class_name = "AudioData",
29 .item_name = av_default_item_name,
30 .version = LIBAVUTIL_VERSION_INT,
34 * Calculate alignment for data pointers.
36 static void calc_ptr_alignment(AudioData *a)
41 for (p = 0; p < a->planes; p++) {
43 while ((intptr_t)a->data[p] % cur_align)
45 if (cur_align < min_align)
46 min_align = cur_align;
48 a->ptr_align = min_align;
51 int ff_audio_data_set_channels(AudioData *a, int channels)
53 if (channels < 1 || channels > AVRESAMPLE_MAX_CHANNELS ||
54 channels > a->allocated_channels)
55 return AVERROR(EINVAL);
57 a->channels = channels;
58 a->planes = a->is_planar ? channels : 1;
60 calc_ptr_alignment(a);
65 int ff_audio_data_init(AudioData *a, void **src, int plane_size, int channels,
66 int nb_samples, enum AVSampleFormat sample_fmt,
67 int read_only, const char *name)
71 memset(a, 0, sizeof(*a));
72 a->class = &audio_data_class;
74 if (channels < 1 || channels > AVRESAMPLE_MAX_CHANNELS) {
75 av_log(a, AV_LOG_ERROR, "invalid channel count: %d\n", channels);
76 return AVERROR(EINVAL);
79 a->sample_size = av_get_bytes_per_sample(sample_fmt);
80 if (!a->sample_size) {
81 av_log(a, AV_LOG_ERROR, "invalid sample format\n");
82 return AVERROR(EINVAL);
84 a->is_planar = av_sample_fmt_is_planar(sample_fmt);
85 a->planes = a->is_planar ? channels : 1;
86 a->stride = a->sample_size * (a->is_planar ? 1 : channels);
88 for (p = 0; p < (a->is_planar ? channels : 1); p++) {
90 av_log(a, AV_LOG_ERROR, "invalid NULL pointer for src[%d]\n", p);
91 return AVERROR(EINVAL);
95 a->allocated_samples = nb_samples * !read_only;
96 a->nb_samples = nb_samples;
97 a->sample_fmt = sample_fmt;
98 a->channels = channels;
99 a->allocated_channels = channels;
100 a->read_only = read_only;
101 a->allow_realloc = 0;
102 a->name = name ? name : "{no name}";
104 calc_ptr_alignment(a);
105 a->samples_align = plane_size / a->stride;
110 AudioData *ff_audio_data_alloc(int channels, int nb_samples,
111 enum AVSampleFormat sample_fmt, const char *name)
116 if (channels < 1 || channels > AVRESAMPLE_MAX_CHANNELS)
119 a = av_mallocz(sizeof(*a));
123 a->sample_size = av_get_bytes_per_sample(sample_fmt);
124 if (!a->sample_size) {
128 a->is_planar = av_sample_fmt_is_planar(sample_fmt);
129 a->planes = a->is_planar ? channels : 1;
130 a->stride = a->sample_size * (a->is_planar ? 1 : channels);
132 a->class = &audio_data_class;
133 a->sample_fmt = sample_fmt;
134 a->channels = channels;
135 a->allocated_channels = channels;
137 a->allow_realloc = 1;
138 a->name = name ? name : "{no name}";
140 if (nb_samples > 0) {
141 ret = ff_audio_data_realloc(a, nb_samples);
148 calc_ptr_alignment(a);
153 int ff_audio_data_realloc(AudioData *a, int nb_samples)
155 int ret, new_buf_size, plane_size, p;
157 /* check if buffer is already large enough */
158 if (a->allocated_samples >= nb_samples)
161 /* validate that the output is not read-only and realloc is allowed */
162 if (a->read_only || !a->allow_realloc)
163 return AVERROR(EINVAL);
165 new_buf_size = av_samples_get_buffer_size(&plane_size,
166 a->allocated_channels, nb_samples,
168 if (new_buf_size < 0)
171 /* if there is already data in the buffer and the sample format is planar,
172 allocate a new buffer and copy the data, otherwise just realloc the
173 internal buffer and set new data pointers */
174 if (a->nb_samples > 0 && a->is_planar) {
175 uint8_t *new_data[AVRESAMPLE_MAX_CHANNELS] = { NULL };
177 ret = av_samples_alloc(new_data, &plane_size, a->allocated_channels,
178 nb_samples, a->sample_fmt, 0);
182 for (p = 0; p < a->planes; p++)
183 memcpy(new_data[p], a->data[p], a->nb_samples * a->stride);
185 av_freep(&a->buffer);
186 memcpy(a->data, new_data, sizeof(new_data));
187 a->buffer = a->data[0];
189 av_freep(&a->buffer);
190 a->buffer = av_malloc(new_buf_size);
192 return AVERROR(ENOMEM);
193 ret = av_samples_fill_arrays(a->data, &plane_size, a->buffer,
194 a->allocated_channels, nb_samples,
199 a->buffer_size = new_buf_size;
200 a->allocated_samples = nb_samples;
202 calc_ptr_alignment(a);
203 a->samples_align = plane_size / a->stride;
208 void ff_audio_data_free(AudioData **a)
212 av_free((*a)->buffer);
216 int ff_audio_data_copy(AudioData *dst, AudioData *src)
220 /* validate input/output compatibility */
221 if (dst->sample_fmt != src->sample_fmt || dst->channels < src->channels)
222 return AVERROR(EINVAL);
224 /* if the input is empty, just empty the output */
225 if (!src->nb_samples) {
230 /* reallocate output if necessary */
231 ret = ff_audio_data_realloc(dst, src->nb_samples);
236 for (p = 0; p < src->planes; p++)
237 memcpy(dst->data[p], src->data[p], src->nb_samples * src->stride);
238 dst->nb_samples = src->nb_samples;
243 int ff_audio_data_combine(AudioData *dst, int dst_offset, AudioData *src,
244 int src_offset, int nb_samples)
246 int ret, p, dst_offset2, dst_move_size;
248 /* validate input/output compatibility */
249 if (dst->sample_fmt != src->sample_fmt || dst->channels != src->channels) {
250 av_log(src, AV_LOG_ERROR, "sample format mismatch\n");
251 return AVERROR(EINVAL);
254 /* validate offsets are within the buffer bounds */
255 if (dst_offset < 0 || dst_offset > dst->nb_samples ||
256 src_offset < 0 || src_offset > src->nb_samples) {
257 av_log(src, AV_LOG_ERROR, "offset out-of-bounds: src=%d dst=%d\n",
258 src_offset, dst_offset);
259 return AVERROR(EINVAL);
262 /* check offsets and sizes to see if we can just do nothing and return */
263 if (nb_samples > src->nb_samples - src_offset)
264 nb_samples = src->nb_samples - src_offset;
268 /* validate that the output is not read-only */
269 if (dst->read_only) {
270 av_log(dst, AV_LOG_ERROR, "dst is read-only\n");
271 return AVERROR(EINVAL);
274 /* reallocate output if necessary */
275 ret = ff_audio_data_realloc(dst, dst->nb_samples + nb_samples);
277 av_log(dst, AV_LOG_ERROR, "error reallocating dst\n");
281 dst_offset2 = dst_offset + nb_samples;
282 dst_move_size = dst->nb_samples - dst_offset;
284 for (p = 0; p < src->planes; p++) {
285 if (dst_move_size > 0) {
286 memmove(dst->data[p] + dst_offset2 * dst->stride,
287 dst->data[p] + dst_offset * dst->stride,
288 dst_move_size * dst->stride);
290 memcpy(dst->data[p] + dst_offset * dst->stride,
291 src->data[p] + src_offset * src->stride,
292 nb_samples * src->stride);
294 dst->nb_samples += nb_samples;
299 void ff_audio_data_drain(AudioData *a, int nb_samples)
301 if (a->nb_samples <= nb_samples) {
302 /* drain the whole buffer */
306 int move_offset = a->stride * nb_samples;
307 int move_size = a->stride * (a->nb_samples - nb_samples);
309 for (p = 0; p < a->planes; p++)
310 memmove(a->data[p], a->data[p] + move_offset, move_size);
312 a->nb_samples -= nb_samples;
316 int ff_audio_data_add_to_fifo(AVAudioFifo *af, AudioData *a, int offset,
319 uint8_t *offset_data[AVRESAMPLE_MAX_CHANNELS];
322 if (offset >= a->nb_samples)
324 offset_size = offset * a->stride;
325 for (p = 0; p < a->planes; p++)
326 offset_data[p] = a->data[p] + offset_size;
328 return av_audio_fifo_write(af, (void **)offset_data, nb_samples);
331 int ff_audio_data_read_from_fifo(AVAudioFifo *af, AudioData *a, int nb_samples)
336 return AVERROR(EINVAL);
338 ret = ff_audio_data_realloc(a, nb_samples);
342 ret = av_audio_fifo_read(af, (void **)a->data, nb_samples);