1 /*
2 * Copyright (c) 2002 Fabrice Bellard
3 * Copyright (c) 2012 Justin Ruggles <justin.ruggles@gmail.com>
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include <stdint.h>
23 #include <stdio.h>
24
33
35
37 {
38 return 2.0 * (
av_lfg_get(lfg) / (double)UINT_MAX) - 1.0;
39 }
40
41 #define PUT_FUNC(name, fmt, type, expr) \
42 static void put_sample_ ## name(void **data, enum AVSampleFormat sample_fmt,\
43 int channels, int sample, int ch, \
44 double v_dbl) \
45 { \
46 type v = expr; \
47 type **out = (type **)data; \
48 if (av_sample_fmt_is_planar(sample_fmt)) \
49 out[ch][sample] = v; \
50 else \
51 out[0][sample * channels + ch] = v; \
52 }
53
59
62 {
65 put_sample_u8(data, sample_fmt, channels, sample, ch, v_dbl);
66 break;
67 case AV_SAMPLE_FMT_S16:
68 put_sample_s16(data, sample_fmt, channels, sample, ch, v_dbl);
69 break;
70 case AV_SAMPLE_FMT_S32:
71 put_sample_s32(data, sample_fmt, channels, sample, ch, v_dbl);
72 break;
73 case AV_SAMPLE_FMT_FLT:
74 put_sample_flt(data, sample_fmt, channels, sample, ch, v_dbl);
75 break;
76 case AV_SAMPLE_FMT_DBL:
77 put_sample_dbl(data, sample_fmt, channels, sample, ch, v_dbl);
78 break;
79 }
80 }
81
84 {
90
91 #define PUT_SAMPLE put_sample(data, sample_fmt, channels, k, ch, v);
92
93 k = 0;
94
95 /* 1 second of single freq sine at 1000 Hz */
96 a = 0;
97 for (i = 0; i < 1 * sample_rate && k < nb_samples; i++, k++) {
98 v = sin(a) * 0.30;
101 a +=
M_PI * 1000.0 * 2.0 / sample_rate;
102 }
103
104 /* 1 second of varying frequency between 100 and 10000 Hz */
105 a = 0;
106 for (i = 0; i < 1 * sample_rate && k < nb_samples; i++, k++) {
107 v = sin(a) * 0.30;
110 f = 100.0 + (((10000.0 - 100.0) * i) / sample_rate);
112 }
113
114 /* 0.5 second of low amplitude white noise */
115 for (i = 0; i < sample_rate / 2 && k < nb_samples; i++, k++) {
119 }
120
121 /* 0.5 second of high amplitude white noise */
122 for (i = 0; i < sample_rate / 2 && k < nb_samples; i++, k++) {
126 }
127
128 /* 1 second of unrelated ramps for each channel */
133 }
134 for (i = 0; i < 1 * sample_rate && k < nb_samples; i++, k++) {
136 v = sin(taba[ch]) * 0.30;
140 }
141 }
142
143 /* 2 seconds of 500 Hz with varying volume */
144 a = 0;
145 ampa = 0;
146 for (i = 0; i < 2 * sample_rate && k < nb_samples; i++, k++) {
148 double amp = (1.0 + sin(ampa)) * 0.15;
149 if (ch & 1)
150 amp = 0.30 - amp;
151 v = sin(a) * amp;
155 }
156 }
157 }
158
159 /* formats, rates, and layouts are ordered for priority in testing.
160 e.g. 'avresample-test 4 2 2' will test all input/output combinations of
161 S16/FLTP/S16P/FLT, 48000/44100, and stereo/mono */
162
174 };
175
177 48000,
178 44100,
179 16000
180 };
181
187 };
188
189 int main(
int argc,
char **argv)
190 {
193 int ret = 0;
196 unsigned int in_buf_size;
197 unsigned int out_buf_size;
200 int in_linesize;
201 int out_linesize;
202 uint64_t in_ch_layout;
203 int in_channels;
205 int in_rate;
206 uint64_t out_ch_layout;
207 int out_channels;
209 int out_rate;
210 int num_formats, num_rates, num_layouts;
211 int i, j, k, l, m,
n;
212
213 num_formats = 2;
214 num_rates = 2;
215 num_layouts = 2;
216 if (argc > 1) {
219 "[<num sample rates> [<num channel layouts>]]]\n"
220 "Default is 2 2 2\n");
221 return 0;
222 }
223 num_formats = strtol(argv[1],
NULL, 0);
225 }
226 if (argc > 2) {
227 num_rates = strtol(argv[2],
NULL, 0);
229 }
230 if (argc > 3) {
231 num_layouts = strtol(argv[3],
NULL, 0);
233 }
234
236
238
241 out_buf_size = in_buf_size;
242
244 if (!in_buf)
247 if (!out_buf)
249
251 if (!s) {
253 ret = 1;
255 }
256
257 for (i = 0; i < num_formats; i++) {
258 in_fmt = formats[i];
259 for (k = 0; k < num_layouts; k++) {
260 in_ch_layout = layouts[k];
262 for (m = 0; m < num_rates; m++) {
263 in_rate = rates[m];
264
266 in_channels, in_rate * 6,
267 in_fmt, 0);
268 if (ret < 0) {
271 }
272 audiogen(&rnd, (
void **)in_data, in_fmt, in_channels, in_rate, in_rate * 6);
273
274 for (j = 0; j < num_formats; j++) {
275 out_fmt = formats[j];
276 for (l = 0; l < num_layouts; l++) {
277 out_ch_layout = layouts[l];
279 for (n = 0; n < num_rates; n++) {
281
284 in_channels, out_channels, in_rate, out_rate);
285
287 out_buf, out_channels,
288 out_rate * 6, out_fmt, 0);
289 if (ret < 0) {
292 }
293
300
302
304 if (ret < 0) {
307 }
308
310 in_data, in_linesize, in_rate * 6);
311 if (ret < 0) {
312 char errbuf[256];
316 }
318 in_rate * 6, ret);
326
328 }
329 }
330 }
331 }
332 }
333 }
334
335 ret = 0;
336
341 return ret;
342 }
#define AV_CH_LAYOUT_7POINT1
attribute_deprecated int avresample_open(AVAudioResampleContext *avr)
ptrdiff_t const GLvoid * data
attribute_deprecated int avresample_get_delay(AVAudioResampleContext *avr)
Memory handling functions.
void av_log_set_level(int level)
Set the log level.
attribute_deprecated int avresample_convert(AVAudioResampleContext *avr, uint8_t **output, int out_plane_size, int out_samples, uint8_t *const *input, int in_plane_size, int in_samples)
int av_strncasecmp(const char *a, const char *b, size_t n)
Locale-independent case-insensitive compare.
#define AV_CH_LAYOUT_STEREO
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
static av_cold int end(AVCodecContext *avctx)
attribute_deprecated void avresample_close(AVAudioResampleContext *avr)
#define AV_CH_LAYOUT_5POINT1
static double dbl_rand(AVLFG *lfg)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
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.
attribute_deprecated int avresample_available(AVAudioResampleContext *avr)
static const uint64_t layouts[]
#define FF_ARRAY_ELEMS(a)
int main(int argc, char **argv)
#define AV_LOG_INFO
Standard information.
AVSampleFormat
Audio sample formats.
#define AVRESAMPLE_MAX_CHANNELS
static unsigned int av_lfg_get(AVLFG *c)
Get the next random unsigned 32-bit number using an ALFG.
Replacements for frequently missing libm functions.
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.
av_cold void av_lfg_init(AVLFG *c, unsigned int seed)
attribute_deprecated AVAudioResampleContext * avresample_alloc_context(void)
static void audiogen(AVLFG *rnd, void **data, enum AVSampleFormat sample_fmt, int channels, int sample_rate, int nb_samples)
int av_strerror(int errnum, char *errbuf, size_t errbuf_size)
Put a description of the AVERROR code errnum in errbuf.
common internal and external API header
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.
attribute_deprecated void avresample_free(AVAudioResampleContext **avr)
#define AV_CH_LAYOUT_MONO
#define PUT_FUNC(name, fmt, type, expr)
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_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LT, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S64), 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 ch