1 /*
2 * Copyright (c) 2012 Justin Ruggles <justin.ruggles@gmail.com>
3 *
4 * This file is part of FFmpeg.
5 *
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.
10 *
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.
15 *
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
19 */
20
21 #include <stdint.h>
22 #include <string.h>
23
26
31 };
32
33 /*
34 * Calculate alignment for data pointers.
35 */
37 {
38 int p;
39 int min_align = 128;
40
41 for (p = 0; p < a->
planes; p++) {
42 int cur_align = 128;
43 while ((intptr_t)a->
data[p] % cur_align)
44 cur_align >>= 1;
45 if (cur_align < min_align)
46 min_align = cur_align;
47 }
49 }
50
52 {
53 if (channels == 1)
54 return 1;
55 else
57 }
58
60 {
64
67
69
70 return 0;
71 }
72
74 int channels, int nb_samples,
77 {
78 int p;
79
80 memset(a, 0, sizeof(*a));
82
86 }
87
92 }
96
97 for (p = 0; p < (a->
is_planar ? channels : 1); p++) {
98 if (!src[p]) {
101 }
103 }
111 a->
name = name ? name :
"{no name}";
112
115
116 return 0;
117 }
118
121 {
123 int ret;
124
127
129 if (!a)
131
136 }
140
147 a->
name = name ? name :
"{no name}";
148
149 if (nb_samples > 0) {
151 if (ret < 0) {
154 }
156 } else {
159 }
160 }
161
163 {
164 int ret, new_buf_size, plane_size, p;
165
166 /* check if buffer is already large enough */
168 return 0;
169
170 /* validate that the output is not read-only and realloc is allowed */
173
177 if (new_buf_size < 0)
178 return new_buf_size;
179
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 */
185
188 if (ret < 0)
189 return ret;
190
191 for (p = 0; p < a->
planes; p++)
193
195 memcpy(a->
data, new_data,
sizeof(new_data));
197 } else {
205 if (ret < 0)
206 return ret;
207 }
210
213
214 return 0;
215 }
216
218 {
219 if (!*a)
220 return;
223 }
224
226 {
227 int ret, p;
228
229 /* validate input/output compatibility */
232
236 }
237
238 /* if the input is empty, just empty the output */
241 return 0;
242 }
243
244 /* reallocate output if necessary */
246 if (ret < 0)
247 return ret;
248
249 /* copy data */
250 if (map) {
252 for (p = 0; p < src->
planes; p++) {
256 }
257 }
259 for (p = 0; p < src->
planes; p++) {
266 }
267 }
268 } else {
269 for (p = 0; p < src->
planes; p++)
271 }
272
274
275 return 0;
276 }
277
279 int src_offset, int nb_samples)
280 {
281 int ret, p, dst_offset2, dst_move_size;
282
283 /* validate input/output compatibility */
287 }
288
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) {
293 src_offset, dst_offset);
295 }
296
297 /* check offsets and sizes to see if we can just do nothing and return */
298 if (nb_samples > src->
nb_samples - src_offset)
300 if (nb_samples <= 0)
301 return 0;
302
303 /* validate that the output is not read-only */
307 }
308
309 /* reallocate output if necessary */
311 if (ret < 0) {
313 return ret;
314 }
315
316 dst_offset2 = dst_offset + nb_samples;
318
319 for (p = 0; p < src->
planes; p++) {
320 if (dst_move_size > 0) {
321 memmove(dst->
data[p] + dst_offset2 * dst->
stride,
323 dst_move_size * dst->
stride);
324 }
325 memcpy(dst->
data[p] + dst_offset * dst->
stride,
327 nb_samples * src->
stride);
328 }
330
331 return 0;
332 }
333
335 {
337 /* drain the whole buffer */
339 } else {
340 int p;
341 int move_offset = a->
stride * nb_samples;
343
344 for (p = 0; p < a->
planes; p++)
345 memmove(a->
data[p], a->
data[p] + move_offset, move_size);
346
348 }
349 }
350
352 int nb_samples)
353 {
355 int offset_size, p;
356
358 return 0;
359 offset_size = offset * a->
stride;
360 for (p = 0; p < a->
planes; p++)
361 offset_data[p] = a->
data[p] + offset_size;
362
364 }
365
367 {
368 int ret;
369
372
374 if (ret < 0)
375 return ret;
376
378 if (ret >= 0)
380 return ret;
381 }
unsigned int buffer_size
allocated buffer size
static void calc_ptr_alignment(AudioData *a)
const char * name
name for debug logging
int av_audio_fifo_read(AVAudioFifo *af, void **data, int nb_samples)
Read data from an AVAudioFifo.
int ff_audio_data_realloc(AudioData *a, int nb_samples)
Reallocate AudioData.
Audio buffer used for intermediate storage between conversion phases.
#define LIBAVUTIL_VERSION_INT
memory handling functions
int ff_audio_data_add_to_fifo(AVAudioFifo *af, AudioData *a, int offset, int nb_samples)
Add samples in AudioData to an AVAudioFifo.
int do_zero
zeroing needed
AudioData * ff_audio_data_alloc(int channels, int nb_samples, enum AVSampleFormat sample_fmt, const char *name)
Allocate AudioData.
int allow_realloc
realloc is allowed
int channel_zero[AVRESAMPLE_MAX_CHANNELS]
dest index to zero
int nb_samples
current number of samples
static const AVClass audio_data_class
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
int sample_size
bytes per sample
int allocated_channels
allocated channel count
int read_only
data is read-only
int ff_sample_fmt_is_planar(enum AVSampleFormat sample_fmt, int channels)
int av_sample_fmt_is_planar(enum AVSampleFormat sample_fmt)
Check if the sample format is planar.
int ff_audio_data_set_channels(AudioData *a, int channels)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
int av_samples_set_silence(uint8_t **audio_data, int offset, int nb_samples, int nb_channels, enum AVSampleFormat sample_fmt)
Fill an audio buffer with silence.
int stride
sample byte offset within a plane
int channels
channel count
int ff_audio_data_init(AudioData *a, uint8_t *const *src, int plane_size, int channels, int nb_samples, enum AVSampleFormat sample_fmt, int read_only, const char *name)
Initialize AudioData using a given source.
static const uint8_t offset[127][2]
int ff_audio_data_read_from_fifo(AVAudioFifo *af, AudioData *a, int nb_samples)
Read samples from an AVAudioFifo to AudioData.
int is_planar
sample format is planar
uint8_t * buffer
data buffer
Context for an Audio FIFO Buffer.
const AVClass * class
AVClass for logging.
int channel_copy[AVRESAMPLE_MAX_CHANNELS]
dest index to copy from
int ff_audio_data_combine(AudioData *dst, int dst_offset, AudioData *src, int src_offset, int nb_samples)
Append data from one AudioData to the end of another.
void ff_audio_data_drain(AudioData *a, int nb_samples)
Drain samples from the start of the AudioData.
int av_samples_alloc(uint8_t **audio_data, int *linesize, int nb_channels, int nb_samples, enum AVSampleFormat sample_fmt, int align)
Allocate a samples buffer for nb_samples samples, and fill data pointers and linesize accordingly...
AVSampleFormat
Audio sample formats.
uint8_t * data[AVRESAMPLE_MAX_CHANNELS]
data plane pointers
#define AVRESAMPLE_MAX_CHANNELS
Describe the class of an AVClass context structure.
int av_samples_get_buffer_size(int *linesize, int nb_channels, int nb_samples, enum AVSampleFormat sample_fmt, int align)
Get the required buffer size for the given audio parameters.
int channel_map[AVRESAMPLE_MAX_CHANNELS]
source index of each output channel, -1 if not remapped
int av_audio_fifo_write(AVAudioFifo *af, void **data, int nb_samples)
Write data to an AVAudioFifo.
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
int samples_align
allocated samples alignment
int ff_audio_data_copy(AudioData *dst, AudioData *src, ChannelMapInfo *map)
Copy data from one AudioData to another.
int av_samples_fill_arrays(uint8_t **audio_data, int *linesize, const uint8_t *buf, int nb_channels, int nb_samples, enum AVSampleFormat sample_fmt, int align)
Fill plane data pointers and linesize for samples with sample format sample_fmt.
int allocated_samples
number of samples the buffer can hold
enum AVSampleFormat sample_fmt
sample format
void ff_audio_data_free(AudioData **a)
Free AudioData.
int ptr_align
minimum data pointer alignment
int planes
number of data planes
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...