2 * Copyright (c) 2012 Justin Ruggles <justin.ruggles@gmail.com>
4 * This file is part of FFmpeg.
6 * FFmpeg 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 * FFmpeg 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 FFmpeg; 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_sample_fmt_is_planar(enum AVSampleFormat sample_fmt, int channels)
56 return av_sample_fmt_is_planar(sample_fmt);
59 int ff_audio_data_set_channels(AudioData *a, int channels)
61 if (channels < 1 || channels > AVRESAMPLE_MAX_CHANNELS ||
62 channels > a->allocated_channels)
63 return AVERROR(EINVAL);
65 a->channels = channels;
66 a->planes = a->is_planar ? channels : 1;
68 calc_ptr_alignment(a);
73 int ff_audio_data_init(AudioData *a, uint8_t * const *src, int plane_size,
74 int channels, int nb_samples,
75 enum AVSampleFormat sample_fmt, int read_only,
80 memset(a, 0, sizeof(*a));
81 a->class = &audio_data_class;
83 if (channels < 1 || channels > AVRESAMPLE_MAX_CHANNELS) {
84 av_log(a, AV_LOG_ERROR, "invalid channel count: %d\n", channels);
85 return AVERROR(EINVAL);
88 a->sample_size = av_get_bytes_per_sample(sample_fmt);
89 if (!a->sample_size) {
90 av_log(a, AV_LOG_ERROR, "invalid sample format\n");
91 return AVERROR(EINVAL);
93 a->is_planar = ff_sample_fmt_is_planar(sample_fmt, channels);
94 a->planes = a->is_planar ? channels : 1;
95 a->stride = a->sample_size * (a->is_planar ? 1 : channels);
97 for (p = 0; p < (a->is_planar ? channels : 1); p++) {
99 av_log(a, AV_LOG_ERROR, "invalid NULL pointer for src[%d]\n", p);
100 return AVERROR(EINVAL);
104 a->allocated_samples = nb_samples * !read_only;
105 a->nb_samples = nb_samples;
106 a->sample_fmt = sample_fmt;
107 a->channels = channels;
108 a->allocated_channels = channels;
109 a->read_only = read_only;
110 a->allow_realloc = 0;
111 a->name = name ? name : "{no name}";
113 calc_ptr_alignment(a);
114 a->samples_align = plane_size / a->stride;
119 AudioData *ff_audio_data_alloc(int channels, int nb_samples,
120 enum AVSampleFormat sample_fmt, const char *name)
125 if (channels < 1 || channels > AVRESAMPLE_MAX_CHANNELS)
128 a = av_mallocz(sizeof(*a));
132 a->sample_size = av_get_bytes_per_sample(sample_fmt);
133 if (!a->sample_size) {
137 a->is_planar = ff_sample_fmt_is_planar(sample_fmt, channels);
138 a->planes = a->is_planar ? channels : 1;
139 a->stride = a->sample_size * (a->is_planar ? 1 : channels);
141 a->class = &audio_data_class;
142 a->sample_fmt = sample_fmt;
143 a->channels = channels;
144 a->allocated_channels = channels;
146 a->allow_realloc = 1;
147 a->name = name ? name : "{no name}";
149 if (nb_samples > 0) {
150 ret = ff_audio_data_realloc(a, nb_samples);
157 calc_ptr_alignment(a);
162 int ff_audio_data_realloc(AudioData *a, int nb_samples)
164 int ret, new_buf_size, plane_size, p;
166 /* check if buffer is already large enough */
167 if (a->allocated_samples >= nb_samples)
170 /* validate that the output is not read-only and realloc is allowed */
171 if (a->read_only || !a->allow_realloc)
172 return AVERROR(EINVAL);
174 new_buf_size = av_samples_get_buffer_size(&plane_size,
175 a->allocated_channels, nb_samples,
177 if (new_buf_size < 0)
180 /* if there is already data in the buffer and the sample format is planar,
181 allocate a new buffer and copy the data, otherwise just realloc the
182 internal buffer and set new data pointers */
183 if (a->nb_samples > 0 && a->is_planar) {
184 uint8_t *new_data[AVRESAMPLE_MAX_CHANNELS] = { NULL };
186 ret = av_samples_alloc(new_data, &plane_size, a->allocated_channels,
187 nb_samples, a->sample_fmt, 0);
191 for (p = 0; p < a->planes; p++)
192 memcpy(new_data[p], a->data[p], a->nb_samples * a->stride);
194 av_freep(&a->buffer);
195 memcpy(a->data, new_data, sizeof(new_data));
196 a->buffer = a->data[0];
198 av_freep(&a->buffer);
199 a->buffer = av_malloc(new_buf_size);
201 return AVERROR(ENOMEM);
202 ret = av_samples_fill_arrays(a->data, &plane_size, a->buffer,
203 a->allocated_channels, nb_samples,
208 a->buffer_size = new_buf_size;
209 a->allocated_samples = nb_samples;
211 calc_ptr_alignment(a);
212 a->samples_align = plane_size / a->stride;
217 void ff_audio_data_free(AudioData **a)
221 av_free((*a)->buffer);
225 int ff_audio_data_copy(AudioData *dst, AudioData *src, ChannelMapInfo *map)
229 /* validate input/output compatibility */
230 if (dst->sample_fmt != src->sample_fmt || dst->channels < src->channels)
231 return AVERROR(EINVAL);
233 if (map && !src->is_planar) {
234 av_log(src, AV_LOG_ERROR, "cannot remap packed format during copy\n");
235 return AVERROR(EINVAL);
238 /* if the input is empty, just empty the output */
239 if (!src->nb_samples) {
244 /* reallocate output if necessary */
245 ret = ff_audio_data_realloc(dst, src->nb_samples);
252 for (p = 0; p < src->planes; p++) {
253 if (map->channel_map[p] >= 0)
254 memcpy(dst->data[p], src->data[map->channel_map[p]],
255 src->nb_samples * src->stride);
258 if (map->do_copy || map->do_zero) {
259 for (p = 0; p < src->planes; p++) {
260 if (map->channel_copy[p])
261 memcpy(dst->data[p], dst->data[map->channel_copy[p]],
262 src->nb_samples * src->stride);
263 else if (map->channel_zero[p])
264 av_samples_set_silence(&dst->data[p], 0, src->nb_samples,
269 for (p = 0; p < src->planes; p++)
270 memcpy(dst->data[p], src->data[p], src->nb_samples * src->stride);
273 dst->nb_samples = src->nb_samples;
278 int ff_audio_data_combine(AudioData *dst, int dst_offset, AudioData *src,
279 int src_offset, int nb_samples)
281 int ret, p, dst_offset2, dst_move_size;
283 /* validate input/output compatibility */
284 if (dst->sample_fmt != src->sample_fmt || dst->channels != src->channels) {
285 av_log(src, AV_LOG_ERROR, "sample format mismatch\n");
286 return AVERROR(EINVAL);
289 /* validate offsets are within the buffer bounds */
290 if (dst_offset < 0 || dst_offset > dst->nb_samples ||
291 src_offset < 0 || src_offset > src->nb_samples) {
292 av_log(src, AV_LOG_ERROR, "offset out-of-bounds: src=%d dst=%d\n",
293 src_offset, dst_offset);
294 return AVERROR(EINVAL);
297 /* check offsets and sizes to see if we can just do nothing and return */
298 if (nb_samples > src->nb_samples - src_offset)
299 nb_samples = src->nb_samples - src_offset;
303 /* validate that the output is not read-only */
304 if (dst->read_only) {
305 av_log(dst, AV_LOG_ERROR, "dst is read-only\n");
306 return AVERROR(EINVAL);
309 /* reallocate output if necessary */
310 ret = ff_audio_data_realloc(dst, dst->nb_samples + nb_samples);
312 av_log(dst, AV_LOG_ERROR, "error reallocating dst\n");
316 dst_offset2 = dst_offset + nb_samples;
317 dst_move_size = dst->nb_samples - dst_offset;
319 for (p = 0; p < src->planes; p++) {
320 if (dst_move_size > 0) {
321 memmove(dst->data[p] + dst_offset2 * dst->stride,
322 dst->data[p] + dst_offset * dst->stride,
323 dst_move_size * dst->stride);
325 memcpy(dst->data[p] + dst_offset * dst->stride,
326 src->data[p] + src_offset * src->stride,
327 nb_samples * src->stride);
329 dst->nb_samples += nb_samples;
334 void ff_audio_data_drain(AudioData *a, int nb_samples)
336 if (a->nb_samples <= nb_samples) {
337 /* drain the whole buffer */
341 int move_offset = a->stride * nb_samples;
342 int move_size = a->stride * (a->nb_samples - nb_samples);
344 for (p = 0; p < a->planes; p++)
345 memmove(a->data[p], a->data[p] + move_offset, move_size);
347 a->nb_samples -= nb_samples;
351 int ff_audio_data_add_to_fifo(AVAudioFifo *af, AudioData *a, int offset,
354 uint8_t *offset_data[AVRESAMPLE_MAX_CHANNELS];
357 if (offset >= a->nb_samples)
359 offset_size = offset * a->stride;
360 for (p = 0; p < a->planes; p++)
361 offset_data[p] = a->data[p] + offset_size;
363 return av_audio_fifo_write(af, (void **)offset_data, nb_samples);
366 int ff_audio_data_read_from_fifo(AVAudioFifo *af, AudioData *a, int nb_samples)
371 return AVERROR(EINVAL);
373 ret = ff_audio_data_realloc(a, nb_samples);
377 ret = av_audio_fifo_read(af, (void **)a->data, nb_samples);