2 * Copyright (C) 2011 Michael Niedermayer (michaelni@gmx.at)
4 * This file is part of libswresample
6 * libswresample is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * libswresample 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
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with libswresample; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "libavutil/avassert.h"
22 #include "libavutil/common.h"
23 #include "libavutil/audioconvert.h"
24 #include "swresample.h"
29 #define ASSERT_LEVEL 2
31 static double get(const void *p, int index, enum AVSampleFormat f){
33 case AV_SAMPLE_FMT_U8 : return ((const uint8_t*)p)[index]/255.0*2-1.0;
34 case AV_SAMPLE_FMT_S16: return ((const int16_t*)p)[index]/32767.0;
35 case AV_SAMPLE_FMT_S32: return ((const int32_t*)p)[index]/2147483647.0;
36 case AV_SAMPLE_FMT_FLT: return ((const float *)p)[index];
37 case AV_SAMPLE_FMT_DBL: return ((const double *)p)[index];
38 default: av_assert2(0);
42 static void set(void *p, int index, enum AVSampleFormat f, double v){
44 case AV_SAMPLE_FMT_U8 : ((uint8_t*)p)[index]= (v+1.0)*255.0/2; break;
45 case AV_SAMPLE_FMT_S16: ((int16_t*)p)[index]= v*32767; break;
46 case AV_SAMPLE_FMT_S32: ((int32_t*)p)[index]= v*2147483647; break;
47 case AV_SAMPLE_FMT_FLT: ((float *)p)[index]= v; break;
48 case AV_SAMPLE_FMT_DBL: ((double *)p)[index]= v; break;
49 default: av_assert2(0);
57 AV_CH_LAYOUT_SURROUND ,
58 AV_CH_LAYOUT_4POINT0 ,
61 AV_CH_LAYOUT_5POINT0 ,
62 AV_CH_LAYOUT_5POINT1 ,
63 AV_CH_LAYOUT_5POINT0_BACK ,
64 AV_CH_LAYOUT_5POINT1_BACK ,
65 AV_CH_LAYOUT_7POINT0 ,
66 AV_CH_LAYOUT_7POINT1 ,
67 AV_CH_LAYOUT_7POINT1_WIDE ,
71 int main(int argc, char **argv){
72 int in_sample_rate, out_sample_rate, ch ,i, in_ch_layout_index, out_ch_layout_index, osr;
73 uint64_t in_ch_layout, out_ch_layout;
74 enum AVSampleFormat in_sample_fmt, out_sample_fmt;
75 int sample_rates[]={8000,11025,16000,22050,32000};
76 uint8_t array_in[SAMPLES*8*8];
77 uint8_t array_mid[SAMPLES*8*8*3];
78 uint8_t array_out[SAMPLES*8*8+100];
79 struct SwrContext * forw_ctx= NULL;
80 struct SwrContext *backw_ctx= NULL;
83 for(osr=0; osr<5; osr++){
84 out_sample_rate= sample_rates[osr];
85 for(in_sample_fmt= AV_SAMPLE_FMT_U8; in_sample_fmt<=AV_SAMPLE_FMT_DBL; in_sample_fmt++){
86 for(out_sample_fmt= AV_SAMPLE_FMT_U8; out_sample_fmt<=AV_SAMPLE_FMT_DBL; out_sample_fmt++){
87 for(in_ch_layout_index=0; layouts[in_ch_layout_index]; in_ch_layout_index++){
88 in_ch_layout= layouts[in_ch_layout_index];
89 int in_ch_count= av_get_channel_layout_nb_channels(in_ch_layout);
90 for(out_ch_layout_index=0; layouts[out_ch_layout_index]; out_ch_layout_index++){
91 int out_count, mid_count;
92 out_ch_layout= layouts[out_ch_layout_index];
93 int out_ch_count= av_get_channel_layout_nb_channels(out_ch_layout);
94 fprintf(stderr, "ch %d->%d, rate:%5d->%5d, fmt:%s->%s",
95 in_ch_count, out_ch_count,
96 in_sample_rate, out_sample_rate,
97 av_get_sample_fmt_name(in_sample_fmt), av_get_sample_fmt_name(out_sample_fmt));
98 forw_ctx = swr_alloc2(forw_ctx, out_ch_layout, out_sample_fmt, out_sample_rate,
99 in_ch_layout, in_sample_fmt, in_sample_rate, 0, 0);
100 backw_ctx = swr_alloc2(backw_ctx,in_ch_layout, in_sample_fmt, in_sample_rate,
101 out_ch_layout, out_sample_fmt, out_sample_rate, 0, 0);
102 if(swr_init( forw_ctx) < 0)
103 fprintf(stderr, "swr_init(->) failed\n");
104 if(swr_init(backw_ctx) < 0)
105 fprintf(stderr, "swr_init(<-) failed\n");
107 fprintf(stderr, "Failed to init forw_cts\n");
109 fprintf(stderr, "Failed to init backw_ctx\n");
111 for(ch=0; ch<in_ch_count; ch++){
112 for(i=0; i<SAMPLES; i++)
113 set(array_in, ch + i*in_ch_count, in_sample_fmt, sin(i*i*3/SAMPLES));
115 mid_count= swr_convert(forw_ctx, ( uint8_t*[]){array_mid}, 3*SAMPLES,
116 (const uint8_t*[]){array_in }, SAMPLES);
117 out_count= swr_convert(backw_ctx,( uint8_t*[]){array_out}, 3*SAMPLES,
118 (const uint8_t*[]){array_mid}, mid_count);
119 for(ch=0; ch<in_ch_count; ch++){
120 double sse, x, maxdiff=0;
126 for(i=0; i<SAMPLES; i++){
127 double a= get(array_in , ch + i*in_ch_count, in_sample_fmt);
128 double b= get(array_out, ch + i*in_ch_count, in_sample_fmt);
134 maxdiff= FFMAX(maxdiff, FFABS(a-b));
137 sse= sum_aa + sum_bb*x*x - 2*x*sum_ab;
139 fprintf(stderr, "[%f %f %f] len:%5d\n", sqrt(sse/SAMPLES), x, maxdiff, out_count);
141 fprintf(stderr, "\n");