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
23 #include "libavutil/mem.h"
24 #include "audio_data.h"
26 static const AVClass audio_data_class = {
27 .class_name = "AudioData",
28 .item_name = av_default_item_name,
29 .version = LIBAVUTIL_VERSION_INT,
33 * Calculate alignment for data pointers.
35 static void calc_ptr_alignment(AudioData *a)
40 for (p = 0; p < a->planes; p++) {
42 while ((intptr_t)a->data[p] % cur_align)
44 if (cur_align < min_align)
45 min_align = cur_align;
47 a->ptr_align = min_align;
50 int ff_audio_data_set_channels(AudioData *a, int channels)
52 if (channels < 1 || channels > AVRESAMPLE_MAX_CHANNELS ||
53 channels > a->allocated_channels)
54 return AVERROR(EINVAL);
56 a->channels = channels;
57 a->planes = a->is_planar ? channels : 1;
59 calc_ptr_alignment(a);
64 int ff_audio_data_init(AudioData *a, void **src, int plane_size, int channels,
65 int nb_samples, enum AVSampleFormat sample_fmt,
66 int read_only, const char *name)
70 memset(a, 0, sizeof(*a));
71 a->class = &audio_data_class;
73 if (channels < 1 || channels > AVRESAMPLE_MAX_CHANNELS) {
74 av_log(a, AV_LOG_ERROR, "invalid channel count: %d\n", channels);
75 return AVERROR(EINVAL);
78 a->sample_size = av_get_bytes_per_sample(sample_fmt);
79 if (!a->sample_size) {
80 av_log(a, AV_LOG_ERROR, "invalid sample format\n");
81 return AVERROR(EINVAL);
83 a->is_planar = av_sample_fmt_is_planar(sample_fmt);
84 a->planes = a->is_planar ? channels : 1;
85 a->stride = a->sample_size * (a->is_planar ? 1 : channels);
87 for (p = 0; p < (a->is_planar ? channels : 1); p++) {
89 av_log(a, AV_LOG_ERROR, "invalid NULL pointer for src[%d]\n", p);
90 return AVERROR(EINVAL);
94 a->allocated_samples = nb_samples * !read_only;
95 a->nb_samples = nb_samples;
96 a->sample_fmt = sample_fmt;
97 a->channels = channels;
98 a->allocated_channels = channels;
99 a->read_only = read_only;
100 a->allow_realloc = 0;
101 a->name = name ? name : "{no name}";
103 calc_ptr_alignment(a);
104 a->samples_align = plane_size / a->stride;
109 AudioData *ff_audio_data_alloc(int channels, int nb_samples,
110 enum AVSampleFormat sample_fmt, const char *name)
115 if (channels < 1 || channels > AVRESAMPLE_MAX_CHANNELS)
118 a = av_mallocz(sizeof(*a));
122 a->sample_size = av_get_bytes_per_sample(sample_fmt);
123 if (!a->sample_size) {
127 a->is_planar = av_sample_fmt_is_planar(sample_fmt);
128 a->planes = a->is_planar ? channels : 1;
129 a->stride = a->sample_size * (a->is_planar ? 1 : channels);
131 a->class = &audio_data_class;
132 a->sample_fmt = sample_fmt;
133 a->channels = channels;
134 a->allocated_channels = channels;
136 a->allow_realloc = 1;
137 a->name = name ? name : "{no name}";
139 if (nb_samples > 0) {
140 ret = ff_audio_data_realloc(a, nb_samples);
147 calc_ptr_alignment(a);
152 int ff_audio_data_realloc(AudioData *a, int nb_samples)
154 int ret, new_buf_size, plane_size, p;
156 /* check if buffer is already large enough */
157 if (a->allocated_samples >= nb_samples)
160 /* validate that the output is not read-only and realloc is allowed */
161 if (a->read_only || !a->allow_realloc)
162 return AVERROR(EINVAL);
164 new_buf_size = av_samples_get_buffer_size(&plane_size,
165 a->allocated_channels, nb_samples,
167 if (new_buf_size < 0)
170 /* if there is already data in the buffer and the sample format is planar,
171 allocate a new buffer and copy the data, otherwise just realloc the
172 internal buffer and set new data pointers */
173 if (a->nb_samples > 0 && a->is_planar) {
174 uint8_t *new_data[AVRESAMPLE_MAX_CHANNELS] = { NULL };
176 ret = av_samples_alloc(new_data, &plane_size, a->allocated_channels,
177 nb_samples, a->sample_fmt, 0);
181 for (p = 0; p < a->planes; p++)
182 memcpy(new_data[p], a->data[p], a->nb_samples * a->stride);
184 av_freep(&a->buffer);
185 memcpy(a->data, new_data, sizeof(new_data));
186 a->buffer = a->data[0];
188 av_freep(&a->buffer);
189 a->buffer = av_malloc(new_buf_size);
191 return AVERROR(ENOMEM);
192 ret = av_samples_fill_arrays(a->data, &plane_size, a->buffer,
193 a->allocated_channels, nb_samples,
198 a->buffer_size = new_buf_size;
199 a->allocated_samples = nb_samples;
201 calc_ptr_alignment(a);
202 a->samples_align = plane_size / a->stride;
207 void ff_audio_data_free(AudioData **a)
211 av_free((*a)->buffer);
215 int ff_audio_data_copy(AudioData *dst, AudioData *src)
219 /* validate input/output compatibility */
220 if (dst->sample_fmt != src->sample_fmt || dst->channels < src->channels)
221 return AVERROR(EINVAL);
223 /* if the input is empty, just empty the output */
224 if (!src->nb_samples) {
229 /* reallocate output if necessary */
230 ret = ff_audio_data_realloc(dst, src->nb_samples);
235 for (p = 0; p < src->planes; p++)
236 memcpy(dst->data[p], src->data[p], src->nb_samples * src->stride);
237 dst->nb_samples = src->nb_samples;
242 int ff_audio_data_combine(AudioData *dst, int dst_offset, AudioData *src,
243 int src_offset, int nb_samples)
245 int ret, p, dst_offset2, dst_move_size;
247 /* validate input/output compatibility */
248 if (dst->sample_fmt != src->sample_fmt || dst->channels != src->channels) {
249 av_log(src, AV_LOG_ERROR, "sample format mismatch\n");
250 return AVERROR(EINVAL);
253 /* validate offsets are within the buffer bounds */
254 if (dst_offset < 0 || dst_offset > dst->nb_samples ||
255 src_offset < 0 || src_offset > src->nb_samples) {
256 av_log(src, AV_LOG_ERROR, "offset out-of-bounds: src=%d dst=%d\n",
257 src_offset, dst_offset);
258 return AVERROR(EINVAL);
261 /* check offsets and sizes to see if we can just do nothing and return */
262 if (nb_samples > src->nb_samples - src_offset)
263 nb_samples = src->nb_samples - src_offset;
267 /* validate that the output is not read-only */
268 if (dst->read_only) {
269 av_log(dst, AV_LOG_ERROR, "dst is read-only\n");
270 return AVERROR(EINVAL);
273 /* reallocate output if necessary */
274 ret = ff_audio_data_realloc(dst, dst->nb_samples + nb_samples);
276 av_log(dst, AV_LOG_ERROR, "error reallocating dst\n");
280 dst_offset2 = dst_offset + nb_samples;
281 dst_move_size = dst->nb_samples - dst_offset;
283 for (p = 0; p < src->planes; p++) {
284 if (dst_move_size > 0) {
285 memmove(dst->data[p] + dst_offset2 * dst->stride,
286 dst->data[p] + dst_offset * dst->stride,
287 dst_move_size * dst->stride);
289 memcpy(dst->data[p] + dst_offset * dst->stride,
290 src->data[p] + src_offset * src->stride,
291 nb_samples * src->stride);
293 dst->nb_samples += nb_samples;
298 void ff_audio_data_drain(AudioData *a, int nb_samples)
300 if (a->nb_samples <= nb_samples) {
301 /* drain the whole buffer */
305 int move_offset = a->stride * nb_samples;
306 int move_size = a->stride * (a->nb_samples - nb_samples);
308 for (p = 0; p < a->planes; p++)
309 memmove(a->data[p], a->data[p] + move_offset, move_size);
311 a->nb_samples -= nb_samples;
315 int ff_audio_data_add_to_fifo(AVAudioFifo *af, AudioData *a, int offset,
318 uint8_t *offset_data[AVRESAMPLE_MAX_CHANNELS];
321 if (offset >= a->nb_samples)
323 offset_size = offset * a->stride;
324 for (p = 0; p < a->planes; p++)
325 offset_data[p] = a->data[p] + offset_size;
327 return av_audio_fifo_write(af, (void **)offset_data, nb_samples);
330 int ff_audio_data_read_from_fifo(AVAudioFifo *af, AudioData *a, int nb_samples)
335 return AVERROR(EINVAL);
337 ret = ff_audio_data_realloc(a, nb_samples);
341 ret = av_audio_fifo_read(af, (void **)a->data, nb_samples);