FFmpeg  2.1.1
audio_data.h
Go to the documentation of this file.
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 #ifndef AVRESAMPLE_AUDIO_DATA_H
22 #define AVRESAMPLE_AUDIO_DATA_H
23 
24 #include <stdint.h>
25 
26 #include "libavutil/audio_fifo.h"
27 #include "libavutil/log.h"
28 #include "libavutil/samplefmt.h"
29 #include "avresample.h"
30 #include "internal.h"
31 
32 /**
33  * Audio buffer used for intermediate storage between conversion phases.
34  */
35 struct AudioData {
36  const AVClass *class; /**< AVClass for logging */
37  uint8_t *data[AVRESAMPLE_MAX_CHANNELS]; /**< data plane pointers */
38  uint8_t *buffer; /**< data buffer */
39  unsigned int buffer_size; /**< allocated buffer size */
40  int allocated_samples; /**< number of samples the buffer can hold */
41  int nb_samples; /**< current number of samples */
42  enum AVSampleFormat sample_fmt; /**< sample format */
43  int channels; /**< channel count */
44  int allocated_channels; /**< allocated channel count */
45  int is_planar; /**< sample format is planar */
46  int planes; /**< number of data planes */
47  int sample_size; /**< bytes per sample */
48  int stride; /**< sample byte offset within a plane */
49  int read_only; /**< data is read-only */
50  int allow_realloc; /**< realloc is allowed */
51  int ptr_align; /**< minimum data pointer alignment */
52  int samples_align; /**< allocated samples alignment */
53  const char *name; /**< name for debug logging */
54 };
55 
56 int ff_audio_data_set_channels(AudioData *a, int channels);
57 
58 /**
59  * Initialize AudioData using a given source.
60  *
61  * This does not allocate an internal buffer. It only sets the data pointers
62  * and audio parameters.
63  *
64  * @param a AudioData struct
65  * @param src source data pointers
66  * @param plane_size plane size, in bytes.
67  * This can be 0 if unknown, but that will lead to
68  * optimized functions not being used in many cases,
69  * which could slow down some conversions.
70  * @param channels channel count
71  * @param nb_samples number of samples in the source data
72  * @param sample_fmt sample format
73  * @param read_only indicates if buffer is read only or read/write
74  * @param name name for debug logging (can be NULL)
75  * @return 0 on success, negative AVERROR value on error
76  */
77 int ff_audio_data_init(AudioData *a, uint8_t **src, int plane_size, int channels,
78  int nb_samples, enum AVSampleFormat sample_fmt,
79  int read_only, const char *name);
80 
81 /**
82  * Allocate AudioData.
83  *
84  * This allocates an internal buffer and sets audio parameters.
85  *
86  * @param channels channel count
87  * @param nb_samples number of samples to allocate space for
88  * @param sample_fmt sample format
89  * @param name name for debug logging (can be NULL)
90  * @return newly allocated AudioData struct, or NULL on error
91  */
92 AudioData *ff_audio_data_alloc(int channels, int nb_samples,
93  enum AVSampleFormat sample_fmt,
94  const char *name);
95 
96 /**
97  * Reallocate AudioData.
98  *
99  * The AudioData must have been previously allocated with ff_audio_data_alloc().
100  *
101  * @param a AudioData struct
102  * @param nb_samples number of samples to allocate space for
103  * @return 0 on success, negative AVERROR value on error
104  */
105 int ff_audio_data_realloc(AudioData *a, int nb_samples);
106 
107 /**
108  * Free AudioData.
109  *
110  * The AudioData must have been previously allocated with ff_audio_data_alloc().
111  *
112  * @param a AudioData struct
113  */
115 
116 /**
117  * Copy data from one AudioData to another.
118  *
119  * @param out output AudioData
120  * @param in input AudioData
121  * @param map channel map, NULL if not remapping
122  * @return 0 on success, negative AVERROR value on error
123  */
125 
126 /**
127  * Append data from one AudioData to the end of another.
128  *
129  * @param dst destination AudioData
130  * @param dst_offset offset, in samples, to start writing, relative to the
131  * start of dst
132  * @param src source AudioData
133  * @param src_offset offset, in samples, to start copying, relative to the
134  * start of the src
135  * @param nb_samples number of samples to copy
136  * @return 0 on success, negative AVERROR value on error
137  */
138 int ff_audio_data_combine(AudioData *dst, int dst_offset, AudioData *src,
139  int src_offset, int nb_samples);
140 
141 /**
142  * Drain samples from the start of the AudioData.
143  *
144  * Remaining samples are shifted to the start of the AudioData.
145  *
146  * @param a AudioData struct
147  * @param nb_samples number of samples to drain
148  */
149 void ff_audio_data_drain(AudioData *a, int nb_samples);
150 
151 /**
152  * Add samples in AudioData to an AVAudioFifo.
153  *
154  * @param af Audio FIFO Buffer
155  * @param a AudioData struct
156  * @param offset number of samples to skip from the start of the data
157  * @param nb_samples number of samples to add to the FIFO
158  * @return number of samples actually added to the FIFO, or
159  * negative AVERROR code on error
160  */
162  int nb_samples);
163 
164 /**
165  * Read samples from an AVAudioFifo to AudioData.
166  *
167  * @param af Audio FIFO Buffer
168  * @param a AudioData struct
169  * @param nb_samples number of samples to read from the FIFO
170  * @return number of samples actually read from the FIFO, or
171  * negative AVERROR code on error
172  */
173 int ff_audio_data_read_from_fifo(AVAudioFifo *af, AudioData *a, int nb_samples);
174 
175 #endif /* AVRESAMPLE_AUDIO_DATA_H */
const char * name
Definition: avisynth_c.h:675
unsigned int buffer_size
allocated buffer size
Definition: audio_data.h:39
const char * name
name for debug logging
Definition: audio_data.h:53
int ff_audio_data_realloc(AudioData *a, int nb_samples)
Reallocate AudioData.
Definition: audio_data.c:153
Audio buffer used for intermediate storage between conversion phases.
Definition: oss_audio.c:47
int ff_audio_data_add_to_fifo(AVAudioFifo *af, AudioData *a, int offset, int nb_samples)
Add samples in AudioData to an AVAudioFifo.
Definition: audio_data.c:342
AudioData * ff_audio_data_alloc(int channels, int nb_samples, enum AVSampleFormat sample_fmt, const char *name)
Allocate AudioData.
Definition: audio_data.c:110
int allow_realloc
realloc is allowed
Definition: audio_data.h:50
int nb_samples
current number of samples
Definition: audio_data.h:41
int sample_size
bytes per sample
Definition: audio_data.h:47
int allocated_channels
allocated channel count
Definition: audio_data.h:44
uint8_t
static const uint8_t offset[511][2]
Definition: vf_uspp.c:58
int read_only
data is read-only
Definition: audio_data.h:49
int ff_audio_data_set_channels(AudioData *a, int channels)
Definition: audio_data.c:51
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=av_sample_fmt_is_planar(in_fmt);out_planar=av_sample_fmt_is_planar(out_fmt);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);ff_audio_convert_init_arm(ac);ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_dlog(ac->avr,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> in
int stride
sample byte offset within a plane
Definition: audio_data.h:48
int channels
channel count
Definition: oss_audio.c:51
int ff_audio_data_read_from_fifo(AVAudioFifo *af, AudioData *a, int nb_samples)
Read samples from an AVAudioFifo to AudioData.
Definition: audio_data.c:357
int is_planar
sample format is planar
Definition: audio_data.h:45
uint8_t * buffer
data buffer
Definition: audio_data.h:38
Context for an Audio FIFO Buffer.
Definition: audio_fifo.c:34
external API header
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.
Definition: audio_data.c:269
void ff_audio_data_drain(AudioData *a, int nb_samples)
Drain samples from the start of the AudioData.
Definition: audio_data.c:325
int ff_audio_data_init(AudioData *a, uint8_t **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.
Definition: audio_data.c:65
AVSampleFormat
Audio Sample Formats.
Definition: samplefmt.h:49
AVS_Value src
Definition: avisynth_c.h:523
uint8_t * data[AVRESAMPLE_MAX_CHANNELS]
data plane pointers
Definition: audio_data.h:37
#define AVRESAMPLE_MAX_CHANNELS
Definition: avresample.h:103
Describe the class of an AVClass context structure.
Definition: log.h:50
int samples_align
allocated samples alignment
Definition: audio_data.h:52
int ff_audio_data_copy(AudioData *dst, AudioData *src, ChannelMapInfo *map)
Copy data from one AudioData to another.
Definition: audio_data.c:216
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=av_sample_fmt_is_planar(in_fmt);out_planar=av_sample_fmt_is_planar(out_fmt);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);ff_audio_convert_init_arm(ac);ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_dlog(ac->avr,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> out
int allocated_samples
number of samples the buffer can hold
Definition: audio_data.h:40
enum AVSampleFormat sample_fmt
sample format
Definition: audio_data.h:42
void ff_audio_data_free(AudioData **a)
Free AudioData.
Definition: audio_data.c:208
int ptr_align
minimum data pointer alignment
Definition: audio_data.h:51
int planes
number of data planes
Definition: audio_data.h:46