2 * Copyright (c) 2002 Fabrice Bellard
3 * Copyright (c) 2012 Justin Ruggles <justin.ruggles@gmail.com>
5 * This file is part of FFmpeg.
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.
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.
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
25 #include "libavutil/avstring.h"
26 #include "libavutil/common.h"
27 #include "libavutil/lfg.h"
28 #include "libavutil/libm.h"
29 #include "libavutil/log.h"
30 #include "libavutil/mem.h"
31 #include "libavutil/opt.h"
32 #include "libavutil/samplefmt.h"
33 #include "avresample.h"
35 static double dbl_rand(AVLFG
*lfg
)
37 return 2.0 * (av_lfg_get(lfg
) / (double)UINT_MAX
) - 1.0;
40 #define PUT_FUNC(name, fmt, type, expr) \
41 static void put_sample_ ## name(void **data, enum AVSampleFormat sample_fmt,\
42 int channels, int sample, int ch, \
46 type **out = (type **)data; \
47 if (av_sample_fmt_is_planar(sample_fmt)) \
48 out[ch][sample] = v; \
50 out[0][sample * channels + ch] = v; \
53 PUT_FUNC(u8
, AV_SAMPLE_FMT_U8
, uint8_t, av_clip_uint8 ( lrint(v_dbl
* (1 << 7)) + 128))
54 PUT_FUNC(s16
, AV_SAMPLE_FMT_S16
, int16_t, av_clip_int16 ( lrint(v_dbl
* (1 << 15))))
55 PUT_FUNC(s32
, AV_SAMPLE_FMT_S32
, int32_t, av_clipl_int32(llrint(v_dbl
* (1U << 31))))
56 PUT_FUNC(flt
, AV_SAMPLE_FMT_FLT
, float, v_dbl
)
57 PUT_FUNC(dbl
, AV_SAMPLE_FMT_DBL
, double, v_dbl
)
59 static void put_sample(void **data
, enum AVSampleFormat sample_fmt
,
60 int channels
, int sample
, int ch
, double v_dbl
)
62 switch (av_get_packed_sample_fmt(sample_fmt
)) {
63 case AV_SAMPLE_FMT_U8
:
64 put_sample_u8(data
, sample_fmt
, channels
, sample
, ch
, v_dbl
);
66 case AV_SAMPLE_FMT_S16
:
67 put_sample_s16(data
, sample_fmt
, channels
, sample
, ch
, v_dbl
);
69 case AV_SAMPLE_FMT_S32
:
70 put_sample_s32(data
, sample_fmt
, channels
, sample
, ch
, v_dbl
);
72 case AV_SAMPLE_FMT_FLT
:
73 put_sample_flt(data
, sample_fmt
, channels
, sample
, ch
, v_dbl
);
75 case AV_SAMPLE_FMT_DBL
:
76 put_sample_dbl(data
, sample_fmt
, channels
, sample
, ch
, v_dbl
);
81 static void audiogen(AVLFG
*rnd
, void **data
, enum AVSampleFormat sample_fmt
,
82 int channels
, int sample_rate
, int nb_samples
)
86 double tabf1
[AVRESAMPLE_MAX_CHANNELS
];
87 double tabf2
[AVRESAMPLE_MAX_CHANNELS
];
88 double taba
[AVRESAMPLE_MAX_CHANNELS
];
90 #define PUT_SAMPLE put_sample(data, sample_fmt, channels, k, ch, v);
94 /* 1 second of single freq sine at 1000 Hz */
96 for (i
= 0; i
< 1 * sample_rate
&& k
< nb_samples
; i
++, k
++) {
98 for (ch
= 0; ch
< channels
; ch
++)
100 a
+= M_PI
* 1000.0 * 2.0 / sample_rate
;
103 /* 1 second of varying frequency between 100 and 10000 Hz */
105 for (i
= 0; i
< 1 * sample_rate
&& k
< nb_samples
; i
++, k
++) {
107 for (ch
= 0; ch
< channels
; ch
++)
109 f
= 100.0 + (((10000.0 - 100.0) * i
) / sample_rate
);
110 a
+= M_PI
* f
* 2.0 / sample_rate
;
113 /* 0.5 second of low amplitude white noise */
114 for (i
= 0; i
< sample_rate
/ 2 && k
< nb_samples
; i
++, k
++) {
115 v
= dbl_rand(rnd
) * 0.30;
116 for (ch
= 0; ch
< channels
; ch
++)
120 /* 0.5 second of high amplitude white noise */
121 for (i
= 0; i
< sample_rate
/ 2 && k
< nb_samples
; i
++, k
++) {
123 for (ch
= 0; ch
< channels
; ch
++)
127 /* 1 second of unrelated ramps for each channel */
128 for (ch
= 0; ch
< channels
; ch
++) {
130 tabf1
[ch
] = 100 + av_lfg_get(rnd
) % 5000;
131 tabf2
[ch
] = 100 + av_lfg_get(rnd
) % 5000;
133 for (i
= 0; i
< 1 * sample_rate
&& k
< nb_samples
; i
++, k
++) {
134 for (ch
= 0; ch
< channels
; ch
++) {
135 v
= sin(taba
[ch
]) * 0.30;
137 f
= tabf1
[ch
] + (((tabf2
[ch
] - tabf1
[ch
]) * i
) / sample_rate
);
138 taba
[ch
] += M_PI
* f
* 2.0 / sample_rate
;
142 /* 2 seconds of 500 Hz with varying volume */
145 for (i
= 0; i
< 2 * sample_rate
&& k
< nb_samples
; i
++, k
++) {
146 for (ch
= 0; ch
< channels
; ch
++) {
147 double amp
= (1.0 + sin(ampa
)) * 0.15;
152 a
+= M_PI
* 500.0 * 2.0 / sample_rate
;
153 ampa
+= M_PI
* 2.0 / sample_rate
;
158 /* formats, rates, and layouts are ordered for priority in testing.
159 e.g. 'avresample-test 4 2 2' will test all input/output combinations of
160 S16/FLTP/S16P/FLT, 48000/44100, and stereo/mono */
162 static const enum AVSampleFormat formats
[] = {
175 static const int rates
[] = {
181 static const uint64_t layouts
[] = {
184 AV_CH_LAYOUT_5POINT1
,
185 AV_CH_LAYOUT_7POINT1
,
188 int main(int argc
, char **argv
)
190 AVAudioResampleContext
*s
;
193 uint8_t *in_buf
= NULL
;
194 uint8_t *out_buf
= NULL
;
195 unsigned int in_buf_size
;
196 unsigned int out_buf_size
;
197 uint8_t *in_data
[AVRESAMPLE_MAX_CHANNELS
] = { 0 };
198 uint8_t *out_data
[AVRESAMPLE_MAX_CHANNELS
] = { 0 };
201 uint64_t in_ch_layout
;
203 enum AVSampleFormat in_fmt
;
205 uint64_t out_ch_layout
;
207 enum AVSampleFormat out_fmt
;
209 int num_formats
, num_rates
, num_layouts
;
210 int i
, j
, k
, l
, m
, n
;
216 if (!av_strncasecmp(argv
[1], "-h", 3)) {
217 av_log(NULL
, AV_LOG_INFO
, "Usage: avresample-test [<num formats> "
218 "[<num sample rates> [<num channel layouts>]]]\n"
219 "Default is 2 2 2\n");
222 num_formats
= strtol(argv
[1], NULL
, 0);
223 num_formats
= av_clip(num_formats
, 1, FF_ARRAY_ELEMS(formats
));
226 num_rates
= strtol(argv
[2], NULL
, 0);
227 num_rates
= av_clip(num_rates
, 1, FF_ARRAY_ELEMS(rates
));
230 num_layouts
= strtol(argv
[3], NULL
, 0);
231 num_layouts
= av_clip(num_layouts
, 1, FF_ARRAY_ELEMS(layouts
));
234 av_log_set_level(AV_LOG_DEBUG
);
236 av_lfg_init(&rnd
, 0xC0FFEE);
238 in_buf_size
= av_samples_get_buffer_size(&in_linesize
, 8, 48000 * 6,
239 AV_SAMPLE_FMT_DBLP
, 0);
240 out_buf_size
= in_buf_size
;
242 in_buf
= av_malloc(in_buf_size
);
245 out_buf
= av_malloc(out_buf_size
);
249 s
= avresample_alloc_context();
251 av_log(NULL
, AV_LOG_ERROR
, "Error allocating AVAudioResampleContext\n");
256 for (i
= 0; i
< num_formats
; i
++) {
258 for (k
= 0; k
< num_layouts
; k
++) {
259 in_ch_layout
= layouts
[k
];
260 in_channels
= av_get_channel_layout_nb_channels(in_ch_layout
);
261 for (m
= 0; m
< num_rates
; m
++) {
264 ret
= av_samples_fill_arrays(in_data
, &in_linesize
, in_buf
,
265 in_channels
, in_rate
* 6,
268 av_log(s
, AV_LOG_ERROR
, "failed in_data fill arrays\n");
271 audiogen(&rnd
, (void **)in_data
, in_fmt
, in_channels
, in_rate
, in_rate
* 6);
273 for (j
= 0; j
< num_formats
; j
++) {
274 out_fmt
= formats
[j
];
275 for (l
= 0; l
< num_layouts
; l
++) {
276 out_ch_layout
= layouts
[l
];
277 out_channels
= av_get_channel_layout_nb_channels(out_ch_layout
);
278 for (n
= 0; n
< num_rates
; n
++) {
281 av_log(NULL
, AV_LOG_INFO
, "%s to %s, %d to %d channels, %d Hz to %d Hz\n",
282 av_get_sample_fmt_name(in_fmt
), av_get_sample_fmt_name(out_fmt
),
283 in_channels
, out_channels
, in_rate
, out_rate
);
285 ret
= av_samples_fill_arrays(out_data
, &out_linesize
,
286 out_buf
, out_channels
,
287 out_rate
* 6, out_fmt
, 0);
289 av_log(s
, AV_LOG_ERROR
, "failed out_data fill arrays\n");
293 av_opt_set_int(s
, "in_channel_layout", in_ch_layout
, 0);
294 av_opt_set_int(s
, "in_sample_fmt", in_fmt
, 0);
295 av_opt_set_int(s
, "in_sample_rate", in_rate
, 0);
296 av_opt_set_int(s
, "out_channel_layout", out_ch_layout
, 0);
297 av_opt_set_int(s
, "out_sample_fmt", out_fmt
, 0);
298 av_opt_set_int(s
, "out_sample_rate", out_rate
, 0);
300 av_opt_set_int(s
, "internal_sample_fmt", AV_SAMPLE_FMT_FLTP
, 0);
302 ret
= avresample_open(s
);
304 av_log(s
, AV_LOG_ERROR
, "Error opening context\n");
308 ret
= avresample_convert(s
, out_data
, out_linesize
, out_rate
* 6,
309 in_data
, in_linesize
, in_rate
* 6);
312 av_strerror(ret
, errbuf
, sizeof(errbuf
));
313 av_log(NULL
, AV_LOG_ERROR
, "%s\n", errbuf
);
316 av_log(NULL
, AV_LOG_INFO
, "Converted %d samples to %d samples\n",
318 if (avresample_get_delay(s
) > 0)
319 av_log(NULL
, AV_LOG_INFO
, "%d delay samples not converted\n",
320 avresample_get_delay(s
));
321 if (avresample_available(s
) > 0)
322 av_log(NULL
, AV_LOG_INFO
, "%d samples available for output\n",
323 avresample_available(s
));
324 av_log(NULL
, AV_LOG_INFO
, "\n");