1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
|
/*
* Copyright (c) 2012 Justin Ruggles <justin.ruggles@gmail.com>
*
* This file is part of Libav.
*
* Libav is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* Libav is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with Libav; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdint.h>
#include <string.h>
#include "libavutil/mem.h"
#include "audio_data.h"
static const AVClass audio_data_class = {
.class_name = "AudioData",
.item_name = av_default_item_name,
.version = LIBAVUTIL_VERSION_INT,
};
/*
* Calculate alignment for data pointers.
*/
static void calc_ptr_alignment(AudioData *a)
{
int p;
int min_align = 128;
for (p = 0; p < a->planes; p++) {
int cur_align = 128;
while ((intptr_t)a->data[p] % cur_align)
cur_align >>= 1;
if (cur_align < min_align)
min_align = cur_align;
}
a->ptr_align = min_align;
}
int ff_audio_data_set_channels(AudioData *a, int channels)
{
if (channels < 1 || channels > AVRESAMPLE_MAX_CHANNELS ||
channels > a->allocated_channels)
return AVERROR(EINVAL);
a->channels = channels;
a->planes = a->is_planar ? channels : 1;
calc_ptr_alignment(a);
return 0;
}
int ff_audio_data_init(AudioData *a, void **src, int plane_size, int channels,
int nb_samples, enum AVSampleFormat sample_fmt,
int read_only, const char *name)
{
int p;
memset(a, 0, sizeof(*a));
a->class = &audio_data_class;
if (channels < 1 || channels > AVRESAMPLE_MAX_CHANNELS) {
av_log(a, AV_LOG_ERROR, "invalid channel count: %d\n", channels);
return AVERROR(EINVAL);
}
a->sample_size = av_get_bytes_per_sample(sample_fmt);
if (!a->sample_size) {
av_log(a, AV_LOG_ERROR, "invalid sample format\n");
return AVERROR(EINVAL);
}
a->is_planar = av_sample_fmt_is_planar(sample_fmt);
a->planes = a->is_planar ? channels : 1;
a->stride = a->sample_size * (a->is_planar ? 1 : channels);
for (p = 0; p < (a->is_planar ? channels : 1); p++) {
if (!src[p]) {
av_log(a, AV_LOG_ERROR, "invalid NULL pointer for src[%d]\n", p);
return AVERROR(EINVAL);
}
a->data[p] = src[p];
}
a->allocated_samples = nb_samples * !read_only;
a->nb_samples = nb_samples;
a->sample_fmt = sample_fmt;
a->channels = channels;
a->allocated_channels = channels;
a->read_only = read_only;
a->allow_realloc = 0;
a->name = name ? name : "{no name}";
calc_ptr_alignment(a);
a->samples_align = plane_size / a->stride;
return 0;
}
AudioData *ff_audio_data_alloc(int channels, int nb_samples,
enum AVSampleFormat sample_fmt, const char *name)
{
AudioData *a;
int ret;
if (channels < 1 || channels > AVRESAMPLE_MAX_CHANNELS)
return NULL;
a = av_mallocz(sizeof(*a));
if (!a)
return NULL;
a->sample_size = av_get_bytes_per_sample(sample_fmt);
if (!a->sample_size) {
av_free(a);
return NULL;
}
a->is_planar = av_sample_fmt_is_planar(sample_fmt);
a->planes = a->is_planar ? channels : 1;
a->stride = a->sample_size * (a->is_planar ? 1 : channels);
a->class = &audio_data_class;
a->sample_fmt = sample_fmt;
a->channels = channels;
a->allocated_channels = channels;
a->read_only = 0;
a->allow_realloc = 1;
a->name = name ? name : "{no name}";
if (nb_samples > 0) {
ret = ff_audio_data_realloc(a, nb_samples);
if (ret < 0) {
av_free(a);
return NULL;
}
return a;
} else {
calc_ptr_alignment(a);
return a;
}
}
int ff_audio_data_realloc(AudioData *a, int nb_samples)
{
int ret, new_buf_size, plane_size, p;
/* check if buffer is already large enough */
if (a->allocated_samples >= nb_samples)
return 0;
/* validate that the output is not read-only and realloc is allowed */
if (a->read_only || !a->allow_realloc)
return AVERROR(EINVAL);
new_buf_size = av_samples_get_buffer_size(&plane_size,
a->allocated_channels, nb_samples,
a->sample_fmt, 0);
if (new_buf_size < 0)
return new_buf_size;
/* if there is already data in the buffer and the sample format is planar,
allocate a new buffer and copy the data, otherwise just realloc the
internal buffer and set new data pointers */
if (a->nb_samples > 0 && a->is_planar) {
uint8_t *new_data[AVRESAMPLE_MAX_CHANNELS] = { NULL };
ret = av_samples_alloc(new_data, &plane_size, a->allocated_channels,
nb_samples, a->sample_fmt, 0);
if (ret < 0)
return ret;
for (p = 0; p < a->planes; p++)
memcpy(new_data[p], a->data[p], a->nb_samples * a->stride);
av_freep(&a->buffer);
memcpy(a->data, new_data, sizeof(new_data));
a->buffer = a->data[0];
} else {
av_freep(&a->buffer);
a->buffer = av_malloc(new_buf_size);
if (!a->buffer)
return AVERROR(ENOMEM);
ret = av_samples_fill_arrays(a->data, &plane_size, a->buffer,
a->allocated_channels, nb_samples,
a->sample_fmt, 0);
if (ret < 0)
return ret;
}
a->buffer_size = new_buf_size;
a->allocated_samples = nb_samples;
calc_ptr_alignment(a);
a->samples_align = plane_size / a->stride;
return 0;
}
void ff_audio_data_free(AudioData **a)
{
if (!*a)
return;
av_free((*a)->buffer);
av_freep(a);
}
int ff_audio_data_copy(AudioData *dst, AudioData *src)
{
int ret, p;
/* validate input/output compatibility */
if (dst->sample_fmt != src->sample_fmt || dst->channels < src->channels)
return AVERROR(EINVAL);
/* if the input is empty, just empty the output */
if (!src->nb_samples) {
dst->nb_samples = 0;
return 0;
}
/* reallocate output if necessary */
ret = ff_audio_data_realloc(dst, src->nb_samples);
if (ret < 0)
return ret;
/* copy data */
for (p = 0; p < src->planes; p++)
memcpy(dst->data[p], src->data[p], src->nb_samples * src->stride);
dst->nb_samples = src->nb_samples;
return 0;
}
int ff_audio_data_combine(AudioData *dst, int dst_offset, AudioData *src,
int src_offset, int nb_samples)
{
int ret, p, dst_offset2, dst_move_size;
/* validate input/output compatibility */
if (dst->sample_fmt != src->sample_fmt || dst->channels != src->channels) {
av_log(src, AV_LOG_ERROR, "sample format mismatch\n");
return AVERROR(EINVAL);
}
/* validate offsets are within the buffer bounds */
if (dst_offset < 0 || dst_offset > dst->nb_samples ||
src_offset < 0 || src_offset > src->nb_samples) {
av_log(src, AV_LOG_ERROR, "offset out-of-bounds: src=%d dst=%d\n",
src_offset, dst_offset);
return AVERROR(EINVAL);
}
/* check offsets and sizes to see if we can just do nothing and return */
if (nb_samples > src->nb_samples - src_offset)
nb_samples = src->nb_samples - src_offset;
if (nb_samples <= 0)
return 0;
/* validate that the output is not read-only */
if (dst->read_only) {
av_log(dst, AV_LOG_ERROR, "dst is read-only\n");
return AVERROR(EINVAL);
}
/* reallocate output if necessary */
ret = ff_audio_data_realloc(dst, dst->nb_samples + nb_samples);
if (ret < 0) {
av_log(dst, AV_LOG_ERROR, "error reallocating dst\n");
return ret;
}
dst_offset2 = dst_offset + nb_samples;
dst_move_size = dst->nb_samples - dst_offset;
for (p = 0; p < src->planes; p++) {
if (dst_move_size > 0) {
memmove(dst->data[p] + dst_offset2 * dst->stride,
dst->data[p] + dst_offset * dst->stride,
dst_move_size * dst->stride);
}
memcpy(dst->data[p] + dst_offset * dst->stride,
src->data[p] + src_offset * src->stride,
nb_samples * src->stride);
}
dst->nb_samples += nb_samples;
return 0;
}
void ff_audio_data_drain(AudioData *a, int nb_samples)
{
if (a->nb_samples <= nb_samples) {
/* drain the whole buffer */
a->nb_samples = 0;
} else {
int p;
int move_offset = a->stride * nb_samples;
int move_size = a->stride * (a->nb_samples - nb_samples);
for (p = 0; p < a->planes; p++)
memmove(a->data[p], a->data[p] + move_offset, move_size);
a->nb_samples -= nb_samples;
}
}
int ff_audio_data_add_to_fifo(AVAudioFifo *af, AudioData *a, int offset,
int nb_samples)
{
uint8_t *offset_data[AVRESAMPLE_MAX_CHANNELS];
int offset_size, p;
if (offset >= a->nb_samples)
return 0;
offset_size = offset * a->stride;
for (p = 0; p < a->planes; p++)
offset_data[p] = a->data[p] + offset_size;
return av_audio_fifo_write(af, (void **)offset_data, nb_samples);
}
int ff_audio_data_read_from_fifo(AVAudioFifo *af, AudioData *a, int nb_samples)
{
int ret;
if (a->read_only)
return AVERROR(EINVAL);
ret = ff_audio_data_realloc(a, nb_samples);
if (ret < 0)
return ret;
ret = av_audio_fifo_read(af, (void **)a->data, nb_samples);
if (ret >= 0)
a->nb_samples = ret;
return ret;
}
|