FFmpeg
Loading...
Searching...
No Matches
swresample.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2011-2012 Michael Niedermayer (michaelni@gmx.at)
3 * Copyright (c) 2002 Fabrice Bellard
4 *
5 * This file is part of libswresample
6 *
7 * libswresample is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * libswresample 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
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with libswresample; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#include "libavutil/avassert.h"
24#include "libavutil/common.h"
25#include "libavutil/opt.h"
26
28
29#include <time.h>
30
31#define SAMPLES 1000
32
33#define SWR_CH_MAX 32
34
35static double get(uint8_t *a[], int ch, int index, int ch_count, enum AVSampleFormat f){
36 const uint8_t *p;
39 p= a[ch];
40 }else{
41 p= a[0];
42 index= ch + index*ch_count;
43 }
44
45 switch(f){
46 case AV_SAMPLE_FMT_U8 : return ((const uint8_t*)p)[index]/127.0-1.0;
47 case AV_SAMPLE_FMT_S16: return ((const int16_t*)p)[index]/32767.0;
48 case AV_SAMPLE_FMT_S32: return ((const int32_t*)p)[index]/2147483647.0;
49 case AV_SAMPLE_FMT_FLT: return ((const float *)p)[index];
50 case AV_SAMPLE_FMT_DBL: return ((const double *)p)[index];
51 default: av_assert0(0);
52 }
53}
54
55static void set(uint8_t *a[], int ch, int index, int ch_count, enum AVSampleFormat f, double v){
56 uint8_t *p;
59 p= a[ch];
60 }else{
61 p= a[0];
62 index= ch + index*ch_count;
63 }
64 switch(f){
65 case AV_SAMPLE_FMT_U8 : ((uint8_t*)p)[index]= av_clip_uint8 (lrint((v+1.0)*127)); break;
66 case AV_SAMPLE_FMT_S16: ((int16_t*)p)[index]= av_clip_int16 (lrint(v*32767)); break;
67 case AV_SAMPLE_FMT_S32: ((int32_t*)p)[index]= av_clipl_int32(llrint(v*2147483647)); break;
68 case AV_SAMPLE_FMT_FLT: ((float *)p)[index]= v; break;
69 case AV_SAMPLE_FMT_DBL: ((double *)p)[index]= v; break;
70 default: av_assert0(0);
71 }
72}
73
74static void shift(uint8_t *a[], int index, int ch_count, enum AVSampleFormat f){
75 int ch;
76
79 for(ch= 0; ch<ch_count; ch++)
81 }else{
82 a[0] += index*ch_count*av_get_bytes_per_sample(f);
83 }
84}
85
98
99static const int rates[] = {
100 8000,
101 11025,
102 16000,
103 22050,
104 32000,
105 48000,
106};
107
124
125static void setup_array(uint8_t *out[SWR_CH_MAX], uint8_t *in, enum AVSampleFormat format, int samples){
127 int i;
128 int plane_size= av_get_bytes_per_sample(format&0xFF)*samples;
129 format&=0xFF;
130 for(i=0; i<SWR_CH_MAX; i++){
131 out[i]= in + i*plane_size;
132 }
133 }else{
134 out[0]= in;
135 }
136}
137
138static int cmp(const void *a, const void *b){
139 return *(const int *)a - *(const int *)b;
140}
141
142static void audiogen(void *data, enum AVSampleFormat sample_fmt,
143 int channels, int sample_rate, int nb_samples)
144{
145 int i, ch, k;
146 double v, f, a, ampa;
147 double tabf1[SWR_CH_MAX];
148 double tabf2[SWR_CH_MAX];
149 double taba[SWR_CH_MAX];
150 unsigned static rnd;
151
152#define PUT_SAMPLE set(data, ch, k, channels, sample_fmt, v);
153#define uint_rand(x) ((x) = (x) * 1664525 + 1013904223)
154#define dbl_rand(x) (uint_rand(x)*2.0 / (double)UINT_MAX - 1)
155 k = 0;
156
157 /* 1 second of single freq sinus at 1000 Hz */
158 a = 0;
159 for (i = 0; i < 1 * sample_rate && k < nb_samples; i++, k++) {
160 v = sin(a) * 0.30;
161 for (ch = 0; ch < channels; ch++)
163 a += M_PI * 1000.0 * 2.0 / sample_rate;
164 }
165
166 /* 1 second of varying frequency between 100 and 10000 Hz */
167 a = 0;
168 for (i = 0; i < 1 * sample_rate && k < nb_samples; i++, k++) {
169 v = sin(a) * 0.30;
170 for (ch = 0; ch < channels; ch++)
172 f = 100.0 + (((10000.0 - 100.0) * i) / sample_rate);
173 a += M_PI * f * 2.0 / sample_rate;
174 }
175
176 /* 0.5 second of low amplitude white noise */
177 for (i = 0; i < sample_rate / 2 && k < nb_samples; i++, k++) {
178 v = dbl_rand(rnd) * 0.30;
179 for (ch = 0; ch < channels; ch++)
181 }
182
183 /* 0.5 second of high amplitude white noise */
184 for (i = 0; i < sample_rate / 2 && k < nb_samples; i++, k++) {
185 v = dbl_rand(rnd);
186 for (ch = 0; ch < channels; ch++)
188 }
189
190 /* 1 second of unrelated ramps for each channel */
191 for (ch = 0; ch < channels; ch++) {
192 taba[ch] = 0;
193 tabf1[ch] = 100 + uint_rand(rnd) % 5000;
194 tabf2[ch] = 100 + uint_rand(rnd) % 5000;
195 }
196 for (i = 0; i < 1 * sample_rate && k < nb_samples; i++, k++) {
197 for (ch = 0; ch < channels; ch++) {
198 v = sin(taba[ch]) * 0.30;
200 f = tabf1[ch] + (((tabf2[ch] - tabf1[ch]) * i) / sample_rate);
201 taba[ch] += M_PI * f * 2.0 / sample_rate;
202 }
203 }
204
205 /* 2 seconds of 500 Hz with varying volume */
206 a = 0;
207 ampa = 0;
208 for (i = 0; i < 2 * sample_rate && k < nb_samples; i++, k++) {
209 for (ch = 0; ch < channels; ch++) {
210 double amp = (1.0 + sin(ampa)) * 0.15;
211 if (ch & 1)
212 amp = 0.30 - amp;
213 v = sin(a) * amp;
215 a += M_PI * 500.0 * 2.0 / sample_rate;
216 ampa += M_PI * 2.0 / sample_rate;
217 }
218 }
219}
220
221int main(int argc, char **argv){
222 int in_sample_rate, out_sample_rate, ch ,i, flush_count;
223 AVChannelLayout in_ch_layout = { 0 }, out_ch_layout = { 0 };
224 enum AVSampleFormat in_sample_fmt, out_sample_fmt;
225 uint8_t array_in[SAMPLES*8*8];
226 uint8_t array_mid[SAMPLES*8*8*3];
227 uint8_t array_out[SAMPLES*8*8+100];
228 uint8_t *ain[SWR_CH_MAX];
229 uint8_t *aout[SWR_CH_MAX];
230 uint8_t *amid[SWR_CH_MAX];
231 int flush_i=0;
232 int mode;
233 int num_tests = 10000;
234 uint32_t seed = 0;
235 uint32_t rand_seed = 0;
237 int max_tests = FF_ARRAY_ELEMS(remaining_tests);
238 int test;
239 int specific_test= -1;
240
241 struct SwrContext * forw_ctx= NULL;
242 struct SwrContext *backw_ctx= NULL;
243
244 if (argc > 1) {
245 if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
246 av_log(NULL, AV_LOG_INFO, "Usage: swresample-test [<num_tests>[ <test>]] \n"
247 "num_tests Default is %d\n", num_tests);
248 return 0;
249 }
250 num_tests = strtol(argv[1], NULL, 0);
251 if(num_tests < 0) {
252 num_tests = -num_tests;
253 rand_seed = time(0);
254 }
255 if(num_tests<= 0 || num_tests>max_tests)
256 num_tests = max_tests;
257 if(argc > 2) {
258 specific_test = strtol(argv[1], NULL, 0);
259 }
260 }
261
262 for(i=0; i<max_tests; i++)
263 remaining_tests[i] = i;
264
265 for(test=0; test<num_tests; test++){
266 unsigned r;
268 r = (seed * (uint64_t)(max_tests - test)) >>32;
269 FFSWAP(int, remaining_tests[r], remaining_tests[max_tests - test - 1]);
270 }
271 qsort(remaining_tests + max_tests - num_tests, num_tests, sizeof(remaining_tests[0]), cmp);
272 in_sample_rate=16000;
273 for(test=0; test<num_tests; test++){
274 char in_layout_string[256];
275 char out_layout_string[256];
276 unsigned vector= remaining_tests[max_tests - test - 1];
277 int in_ch_count;
278 int out_count, mid_count, out_ch_count;
279
285 av_assert0(!vector);
286
287 if(specific_test == 0){
289 continue;
290 }
291
292 in_ch_count= in_ch_layout.nb_channels;
293 out_ch_count= out_ch_layout.nb_channels;
294 av_channel_layout_describe(&in_ch_layout, in_layout_string, sizeof( in_layout_string));
295 av_channel_layout_describe(&out_ch_layout, out_layout_string, sizeof(out_layout_string));
296 fprintf(stderr, "TEST: %s->%s, rate:%5d->%5d, fmt:%s->%s\n",
297 in_layout_string, out_layout_string,
302 0, 0) < 0) {
303 fprintf(stderr, "Failed to init forw_cts\n");
304 return 1;
305 }
308 0, 0) < 0) {
309 fprintf(stderr, "Failed to init backw_ctx\n");
310 return 1;
311 }
312
313 if(swr_init( forw_ctx) < 0)
314 fprintf(stderr, "swr_init(->) failed\n");
315 if(swr_init(backw_ctx) < 0)
316 fprintf(stderr, "swr_init(<-) failed\n");
317 //FIXME test planar
318 setup_array(ain , array_in , in_sample_fmt, SAMPLES);
319 setup_array(amid, array_mid, out_sample_fmt, 3*SAMPLES);
320 setup_array(aout, array_out, in_sample_fmt , SAMPLES);
321#if 0
322 for(ch=0; ch<in_ch_count; ch++){
323 for(i=0; i<SAMPLES; i++)
324 set(ain, ch, i, in_ch_count, in_sample_fmt, sin(i*i*3/SAMPLES));
325 }
326#else
327 audiogen(ain, in_sample_fmt, in_ch_count, SAMPLES/6+1, SAMPLES);
328#endif
329 mode = uint_rand(rand_seed) % 3;
330 if(mode==0 /*|| out_sample_rate == in_sample_rate*/) {
331 mid_count= swr_convert(forw_ctx, amid, 3*SAMPLES, (const uint8_t **)ain, SAMPLES);
332 } else if(mode==1){
333 mid_count= swr_convert(forw_ctx, amid, 0, (const uint8_t **)ain, SAMPLES);
334 mid_count+=swr_convert(forw_ctx, amid, 3*SAMPLES, (const uint8_t **)ain, 0);
335 } else {
336 int tmp_count;
337 mid_count= swr_convert(forw_ctx, amid, 0, (const uint8_t **)ain, 1);
338 av_assert0(mid_count==0);
339 shift(ain, 1, in_ch_count, in_sample_fmt);
340 mid_count+=swr_convert(forw_ctx, amid, 3*SAMPLES, (const uint8_t **)ain, 0);
341 shift(amid, mid_count, out_ch_count, out_sample_fmt); tmp_count = mid_count;
342 mid_count+=swr_convert(forw_ctx, amid, 2, (const uint8_t **)ain, 2);
343 shift(amid, mid_count-tmp_count, out_ch_count, out_sample_fmt); tmp_count = mid_count;
344 shift(ain, 2, in_ch_count, in_sample_fmt);
345 mid_count+=swr_convert(forw_ctx, amid, 1, (const uint8_t **)ain, SAMPLES-3);
346 shift(amid, mid_count-tmp_count, out_ch_count, out_sample_fmt); tmp_count = mid_count;
347 shift(ain, -3, in_ch_count, in_sample_fmt);
348 mid_count+=swr_convert(forw_ctx, amid, 3*SAMPLES, (const uint8_t **)ain, 0);
349 shift(amid, -tmp_count, out_ch_count, out_sample_fmt);
350 }
351 out_count= swr_convert(backw_ctx,aout, SAMPLES, (const uint8_t **)amid, mid_count);
352
353 for(ch=0; ch<in_ch_count; ch++){
354 double sse, maxdiff=0;
355 double sum_aa= 0;
356 double sum_bb= 0;
357 double sum_ab= 0;
358 for(i=0; i<out_count; i++){
359 double a= get(ain , ch, i, in_ch_count, in_sample_fmt);
360 double b= get(aout, ch, i, in_ch_count, in_sample_fmt);
361 sum_aa+= a*a;
362 sum_bb+= b*b;
363 sum_ab+= a*b;
364 maxdiff= FFMAX(maxdiff, fabs(a-b));
365 }
366 sse= sum_aa + sum_bb - 2*sum_ab;
367 if(sse < 0 && sse > -0.00001) sse=0; //fix rounding error
368
369 fprintf(stderr, "[e:%f c:%f max:%f] len:%5d\n", out_count ? sqrt(sse/out_count) : 0, sum_ab/(sqrt(sum_aa*sum_bb)), maxdiff, out_count);
370 }
371
372 flush_i++;
373 flush_i%=21;
374 flush_count = swr_convert(backw_ctx,aout, flush_i, 0, 0);
375 shift(aout, flush_i, in_ch_count, in_sample_fmt);
376 flush_count+= swr_convert(backw_ctx,aout, SAMPLES-flush_i, 0, 0);
377 shift(aout, -flush_i, in_ch_count, in_sample_fmt);
378 if(flush_count){
379 for(ch=0; ch<in_ch_count; ch++){
380 double sse, maxdiff=0;
381 double sum_aa= 0;
382 double sum_bb= 0;
383 double sum_ab= 0;
384 for(i=0; i<flush_count; i++){
385 double a= get(ain , ch, i+out_count, in_ch_count, in_sample_fmt);
386 double b= get(aout, ch, i, in_ch_count, in_sample_fmt);
387 sum_aa+= a*a;
388 sum_bb+= b*b;
389 sum_ab+= a*b;
390 maxdiff= FFMAX(maxdiff, fabs(a-b));
391 }
392 sse= sum_aa + sum_bb - 2*sum_ab;
393 if(sse < 0 && sse > -0.00001) sse=0; //fix rounding error
394
395 fprintf(stderr, "[e:%f c:%f max:%f] len:%5d F:%3d\n", sqrt(sse/flush_count), sum_ab/(sqrt(sum_aa*sum_bb)), maxdiff, flush_count, flush_i);
396 }
397 }
398
399
400 fprintf(stderr, "\n");
401 }
402
403 return 0;
404}
static const char *const format[]
Definition af_aiir.c:444
#define SWR_CH_MAX
Definition af_amerge.c:37
static FILE * out
channels
Definition aptx.h:31
int32_t
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define f(width, name)
Definition cbs_vp8.c:236
Public libavutil channel layout APIs header.
#define rnd
Definition checkasm.h:135
common internal and external API header
#define av_clipl_int32
Definition common.h:118
#define av_clip_int16
Definition common.h:115
#define av_clip_uint8
Definition common.h:106
#define NULL
Definition coverity.c:32
static __device__ float fabs(float a)
int main
Definition dovi_rpuenc.c:38
#define AV_CHANNEL_LAYOUT_4POINT0
#define AV_CHANNEL_LAYOUT_5POINT1_BACK
#define AV_CHANNEL_LAYOUT_7POINT1_WIDE
#define AV_CHANNEL_LAYOUT_5POINT0
#define AV_CHANNEL_LAYOUT_STEREO
int av_channel_layout_compare(const AVChannelLayout *chl, const AVChannelLayout *chl1)
Check whether two channel layouts are semantically the same, i.e.
#define AV_CHANNEL_LAYOUT_2_2
#define AV_CHANNEL_LAYOUT_5POINT0_BACK
#define AV_CHANNEL_LAYOUT_5POINT1
#define AV_CHANNEL_LAYOUT_MONO
#define AV_CHANNEL_LAYOUT_7POINT0
#define AV_CHANNEL_LAYOUT_SURROUND
#define AV_CHANNEL_LAYOUT_2_1
int av_channel_layout_describe(const AVChannelLayout *channel_layout, char *buf, size_t buf_size)
Get a human-readable string describing the channel layout properties.
#define AV_CHANNEL_LAYOUT_7POINT1
int av_channel_layout_copy(AVChannelLayout *dst, const AVChannelLayout *src)
Make a copy of a channel layout.
#define AV_CHANNEL_LAYOUT_QUAD
#define AV_LOG_INFO
Standard information.
Definition log.h:221
int av_sample_fmt_is_planar(enum AVSampleFormat sample_fmt)
Check if the sample format is planar.
Definition samplefmt.c:114
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
Definition samplefmt.c:108
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...
Definition samplefmt.c:68
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.
Definition samplefmt.c:51
AVSampleFormat
Audio sample formats.
Definition samplefmt.h:55
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition samplefmt.h:66
@ AV_SAMPLE_FMT_S16P
signed 16 bits, planar
Definition samplefmt.h:64
@ AV_SAMPLE_FMT_FLT
float
Definition samplefmt.h:60
@ AV_SAMPLE_FMT_U8P
unsigned 8 bits, planar
Definition samplefmt.h:63
@ AV_SAMPLE_FMT_S32P
signed 32 bits, planar
Definition samplefmt.h:65
@ AV_SAMPLE_FMT_S32
signed 32 bits
Definition samplefmt.h:59
@ AV_SAMPLE_FMT_U8
unsigned 8 bits
Definition samplefmt.h:57
@ AV_SAMPLE_FMT_DBLP
double, planar
Definition samplefmt.h:67
@ AV_SAMPLE_FMT_DBL
double
Definition samplefmt.h:61
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition samplefmt.h:58
int swr_alloc_set_opts2(struct SwrContext **ps, const AVChannelLayout *out_ch_layout, enum AVSampleFormat out_sample_fmt, int out_sample_rate, const AVChannelLayout *in_ch_layout, enum AVSampleFormat in_sample_fmt, int in_sample_rate, int log_offset, void *log_ctx)
Allocate SwrContext if needed and set/reset common parameters.
Definition swresample.c:54
int attribute_align_arg swr_convert(struct SwrContext *s, uint8_t *const *out_arg, int out_count, const uint8_t *const *in_arg, int in_count)
Convert audio.
Definition swresample.c:725
av_cold int swr_init(struct SwrContext *s)
Initialize context after user parameters have been set.
Definition swresample.c:156
int index
Definition gxfenc.c:90
int a
#define r
Definition input.c:42
#define b
Definition input.c:43
#define llrint(x)
Definition libm.h:396
#define FFSWAP(type, a, b)
Definition macros.h:52
#define FFMAX(a, b)
Definition macros.h:47
#define M_PI
Definition mathematics.h:67
static av_always_inline int cmp(MPVEncContext *const s, const int x, const int y, const int subx, const int suby, const int size, const int h, int ref_index, int src_index, me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, const int flags)
compares a block (either a full macroblock or a partition thereof) against a proposed motion-compensa...
Definition motion_est.c:263
enum MovChannelLayoutTag * layouts
Definition mov_chan.c:335
static int sse(const MPVEncContext *const s, const uint8_t *src1, const uint8_t *src2, int w, int h, int stride)
const char data[16]
Definition mxf.c:149
AVOptions.
formats
Definition signature.h:47
#define FF_ARRAY_ELEMS(a)
An AVChannelLayout holds information about the channel layout of audio data.
int nb_channels
Number of channels in this layout.
The libswresample context.
int out_sample_rate
output sample rate
int in_sample_rate
input sample rate
AVChannelLayout out_ch_layout
output channel layout
enum AVSampleFormat in_sample_fmt
input sample format
AVChannelLayout in_ch_layout
input channel layout
enum AVSampleFormat out_sample_fmt
output sample format
Definition swscale.c:71
Definition idctdsp.c:35
libswresample public header
#define lrint
Definition tablegen.h:53
#define av_log(a,...)
static void setup_array(uint8_t *out[SWR_CH_MAX], uint8_t *in, enum AVSampleFormat format, int samples)
Definition swresample.c:125
static void audiogen(void *data, enum AVSampleFormat sample_fmt, int channels, int sample_rate, int nb_samples)
Definition swresample.c:142
#define PUT_SAMPLE
static double get(uint8_t *a[], int ch, int index, int ch_count, enum AVSampleFormat f)
Definition swresample.c:35
static int cmp(const void *a, const void *b)
Definition swresample.c:138
#define dbl_rand(x)
static void shift(uint8_t *a[], int index, int ch_count, enum AVSampleFormat f)
Definition swresample.c:74
static const int rates[]
Definition swresample.c:99
#define uint_rand(x)
static void set(uint8_t *a[], int ch, int index, int ch_count, enum AVSampleFormat f, double v)
Definition swresample.c:55
#define SAMPLES
static unsigned int seed
Definition videogen.c:78