1 /*
2 * This file is part of FFmpeg.
3 *
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #ifndef AVUTIL_SAMPLEFMT_H
20 #define AVUTIL_SAMPLEFMT_H
21
22 #include <stdint.h>
23
26
27 /**
28 * @addtogroup lavu_audio
29 * @{
30 *
31 * @defgroup lavu_sampfmts Audio sample formats
32 *
33 * Audio sample format enumeration and related convenience functions.
34 * @{
35 */
36
37 /**
38 * Audio sample formats
39 *
40 * - The data described by the sample format is always in native-endian order.
41 * Sample values can be expressed by native C types, hence the lack of a signed
42 * 24-bit sample format even though it is a common raw audio data format.
43 *
44 * - The floating-point formats are based on full volume being in the range
45 * [-1.0, 1.0]. Any values outside this range are beyond full volume level.
46 *
47 * - The data layout as used in av_samples_fill_arrays() and elsewhere in FFmpeg
48 * (such as AVFrame in libavcodec) is as follows:
49 *
50 * @par
51 * For planar sample formats, each audio channel is in a separate data plane,
52 * and linesize is the buffer size, in bytes, for a single plane. All data
53 * planes must be the same size. For packed sample formats, only the first data
54 * plane is used, and samples for each channel are interleaved. In this case,
55 * linesize is the buffer size, in bytes, for the 1 plane.
56 *
57 */
65
73
75 };
76
77 /**
78 * Return the name of sample_fmt, or NULL if sample_fmt is not
79 * recognized.
80 */
82
83 /**
84 * Return a sample format corresponding to name, or AV_SAMPLE_FMT_NONE
85 * on error.
86 */
88
89 /**
90 * Return the planar<->packed alternative form of the given sample format, or
91 * AV_SAMPLE_FMT_NONE on error. If the passed sample_fmt is already in the
92 * requested planar/packed format, the format returned is the same as the
93 * input.
94 */
96
97 /**
98 * Get the packed alternative form of the given sample format.
99 *
100 * If the passed sample_fmt is already in packed format, the format returned is
101 * the same as the input.
102 *
103 * @return the packed alternative form of the given sample format or
104 AV_SAMPLE_FMT_NONE on error.
105 */
107
108 /**
109 * Get the planar alternative form of the given sample format.
110 *
111 * If the passed sample_fmt is already in planar format, the format returned is
112 * the same as the input.
113 *
114 * @return the planar alternative form of the given sample format or
115 AV_SAMPLE_FMT_NONE on error.
116 */
118
119 /**
120 * Generate a string corresponding to the sample format with
121 * sample_fmt, or a header if sample_fmt is negative.
122 *
123 * @param buf the buffer where to write the string
124 * @param buf_size the size of buf
125 * @param sample_fmt the number of the sample format to print the
126 * corresponding info string, or a negative value to print the
127 * corresponding header.
128 * @return the pointer to the filled buffer or NULL if sample_fmt is
129 * unknown or in case of other errors
130 */
132
133 /**
134 * Return number of bytes per sample.
135 *
136 * @param sample_fmt the sample format
137 * @return number of bytes per sample or zero if unknown for the given
138 * sample format
139 */
141
142 /**
143 * Check if the sample format is planar.
144 *
145 * @param sample_fmt the sample format to inspect
146 * @return 1 if the sample format is planar, 0 if it is interleaved
147 */
149
150 /**
151 * Get the required buffer size for the given audio parameters.
152 *
153 * @param[out] linesize calculated linesize, may be NULL
154 * @param nb_channels the number of channels
155 * @param nb_samples the number of samples in a single channel
156 * @param sample_fmt the sample format
157 * @param align buffer size alignment (0 = default, 1 = no alignment)
158 * @return required buffer size, or negative error code on failure
159 */
162
163 /**
164 * @}
165 *
166 * @defgroup lavu_sampmanip Samples manipulation
167 *
168 * Functions that manipulate audio samples
169 * @{
170 */
171
172 /**
173 * Fill plane data pointers and linesize for samples with sample
174 * format sample_fmt.
175 *
176 * The audio_data array is filled with the pointers to the samples data planes:
177 * for planar, set the start point of each channel's data within the buffer,
178 * for packed, set the start point of the entire buffer only.
179 *
180 * The value pointed to by linesize is set to the aligned size of each
181 * channel's data buffer for planar layout, or to the aligned size of the
182 * buffer for all channels for packed layout.
183 *
184 * The buffer in buf must be big enough to contain all the samples
185 * (use av_samples_get_buffer_size() to compute its minimum size),
186 * otherwise the audio_data pointers will point to invalid data.
187 *
188 * @see enum AVSampleFormat
189 * The documentation for AVSampleFormat describes the data layout.
190 *
191 * @param[out] audio_data array to be filled with the pointer for each channel
192 * @param[out] linesize calculated linesize, may be NULL
193 * @param buf the pointer to a buffer containing the samples
194 * @param nb_channels the number of channels
195 * @param nb_samples the number of samples in a single channel
196 * @param sample_fmt the sample format
197 * @param align buffer size alignment (0 = default, 1 = no alignment)
198 * @return >=0 on success or a negative error code on failure
199 * @todo return minimum size in bytes required for the buffer in case
200 * of success at the next bump
201 */
206
207 /**
208 * Allocate a samples buffer for nb_samples samples, and fill data pointers and
209 * linesize accordingly.
210 * The allocated samples buffer can be freed by using av_freep(&audio_data[0])
211 * Allocated data will be initialized to silence.
212 *
213 * @see enum AVSampleFormat
214 * The documentation for AVSampleFormat describes the data layout.
215 *
216 * @param[out] audio_data array to be filled with the pointer for each channel
217 * @param[out] linesize aligned size for audio buffer(s), may be NULL
218 * @param nb_channels number of audio channels
219 * @param nb_samples number of samples per channel
220 * @param align buffer size alignment (0 = default, 1 = no alignment)
221 * @return >=0 on success or a negative error code on failure
222 * @todo return the size of the allocated buffer in case of success at the next bump
223 * @see av_samples_fill_arrays()
224 * @see av_samples_alloc_array_and_samples()
225 */
228
229 /**
230 * Allocate a data pointers array, samples buffer for nb_samples
231 * samples, and fill data pointers and linesize accordingly.
232 *
233 * This is the same as av_samples_alloc(), but also allocates the data
234 * pointers array.
235 *
236 * @see av_samples_alloc()
237 */
240
241 /**
242 * Copy samples from src to dst.
243 *
244 * @param dst destination array of pointers to data planes
245 * @param src source array of pointers to data planes
246 * @param dst_offset offset in samples at which the data will be written to dst
247 * @param src_offset offset in samples at which the data will be read from src
248 * @param nb_samples number of samples to be copied
249 * @param nb_channels number of audio channels
250 * @param sample_fmt audio sample format
251 */
255
256 /**
257 * Fill an audio buffer with silence.
258 *
259 * @param audio_data array of pointers to data planes
260 * @param offset offset in samples at which to start filling
261 * @param nb_samples number of samples to fill
262 * @param nb_channels number of audio channels
263 * @param sample_fmt audio sample format
264 */
267
268 /**
269 * @}
270 * @}
271 */
272 #endif /* AVUTIL_SAMPLEFMT_H */
Number of sample formats. DO NOT USE if linking dynamically.
char * av_get_sample_fmt_string(char *buf, int buf_size, enum AVSampleFormat sample_fmt)
Generate a string corresponding to the sample format with sample_fmt, or a header if sample_fmt is ne...
Convenience header that includes libavutil's core.
int av_samples_alloc_array_and_samples(uint8_t ***audio_data, int *linesize, int nb_channels, int nb_samples, enum AVSampleFormat sample_fmt, int align)
Allocate a data pointers array, samples buffer for nb_samples samples, and fill data pointers and lin...
Macro definitions for various function/variable attributes.
enum AVSampleFormat av_get_planar_sample_fmt(enum AVSampleFormat sample_fmt)
Get the planar alternative form of the given sample format.
int av_sample_fmt_is_planar(enum AVSampleFormat sample_fmt)
Check if the sample format is planar.
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.
const char * av_get_sample_fmt_name(enum AVSampleFormat sample_fmt)
Return the name of sample_fmt, or NULL if sample_fmt is not recognized.
static const uint8_t offset[127][2]
const AVS_VideoInfo int align
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 pi<< 24) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_U8,(uint64_t)((*(constuint8_t *) pi-0x80U))<< 56) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S16,(uint64_t)(*(constint16_t *) pi)<< 48) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S32,(uint64_t)(*(constint32_t *) pi)<< 32) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S64,(*(constint64_t *) pi >>56)+0x80) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S64,*(constint64_t *) pi *(1.0f/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S64,*(constint64_t *) pi *(1.0/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_FLT, llrintf(*(constfloat *) pi *(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_DBL, llrint(*(constdouble *) pi *(INT64_C(1)<< 63)))#defineFMT_PAIR_FUNC(out, in) staticconv_func_type *constfmt_pair_to_conv_functions[AV_SAMPLE_FMT_NB *AV_SAMPLE_FMT_NB]={FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S16), FMT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FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S64),};staticvoidcpy1(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, len);}staticvoidcpy2(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 2 *len);}staticvoidcpy4(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 4 *len);}staticvoidcpy8(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 8 *len);}AudioConvert *swri_audio_convert_alloc(enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, constint *ch_map, intflags){AudioConvert *ctx;conv_func_type *f=fmt_pair_to_conv_functions[av_get_packed_sample_fmt(out_fmt)+AV_SAMPLE_FMT_NB *av_get_packed_sample_fmt(in_fmt)];if(!f) returnNULL;ctx=av_mallocz(sizeof(*ctx));if(!ctx) returnNULL;if(channels==1){in_fmt=av_get_planar_sample_fmt(in_fmt);out_fmt=av_get_planar_sample_fmt(out_fmt);}ctx->channels=channels;ctx->conv_f=f;ctx->ch_map=ch_map;if(in_fmt==AV_SAMPLE_FMT_U8||in_fmt==AV_SAMPLE_FMT_U8P) memset(ctx->silence, 0x80, sizeof(ctx->silence));if(out_fmt==in_fmt &&!ch_map){switch(av_get_bytes_per_sample(in_fmt)){case1:ctx->simd_f=cpy1;break;case2:ctx->simd_f=cpy2;break;case4:ctx->simd_f=cpy4;break;case8:ctx->simd_f=cpy8;break;}}if(HAVE_X86ASM &&1) swri_audio_convert_init_x86(ctx, out_fmt, in_fmt, channels);if(ARCH_ARM) swri_audio_convert_init_arm(ctx, out_fmt, in_fmt, channels);if(ARCH_AARCH64) swri_audio_convert_init_aarch64(ctx, out_fmt, in_fmt, channels);returnctx;}voidswri_audio_convert_free(AudioConvert **ctx){av_freep(ctx);}intswri_audio_convert(AudioConvert *ctx, AudioData *out, AudioData *in, intlen){intch;intoff=0;constintos=(out->planar?1:out->ch_count)*out->bps;unsignedmisaligned=0;av_assert0(ctx->channels==out->ch_count);if(ctx->in_simd_align_mask){intplanes=in->planar?in->ch_count:1;unsignedm=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) in->ch[ch];misaligned|=m &ctx->in_simd_align_mask;}if(ctx->out_simd_align_mask){intplanes=out->planar?out->ch_count:1;unsignedm=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) out->ch[ch];misaligned|=m &ctx->out_simd_align_mask;}if(ctx->simd_f &&!ctx->ch_map &&!misaligned){off=len &~15;av_assert1(off >=0);av_assert1(off<=len);av_assert2(ctx->channels==SWR_CH_MAX||!in->ch[ctx->channels]);if(off >0){if(out->planar==in->planar){intplanes=out->planar?out->ch_count:1;for(ch=0;ch< planes;ch++){ctx->simd_f(out-> ch const uint8_t **in ch off *out planar
int av_samples_copy(uint8_t **dst, uint8_t *const *src, int dst_offset, int src_offset, int nb_samples, int nb_channels, enum AVSampleFormat sample_fmt)
Copy samples from src to dst.
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.
enum AVSampleFormat av_get_sample_fmt(const char *name)
Return a sample format corresponding to name, or AV_SAMPLE_FMT_NONE on error.
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
enum AVSampleFormat av_get_alt_sample_fmt(enum AVSampleFormat sample_fmt, int planar)
Return the planar<->packed alternative form of the given sample format, or AV_SAMPLE_FMT_NONE on erro...
enum AVSampleFormat av_get_packed_sample_fmt(enum AVSampleFormat sample_fmt)
Get the packed alternative form of the given sample format.
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.