00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "libavutil/avassert.h"
00022 #include "libavutil/common.h"
00023 #include "libavutil/audioconvert.h"
00024 #include "swresample.h"
00025 #undef fprintf
00026
00027 #define SAMPLES 1000
00028
00029 #define ASSERT_LEVEL 2
00030
00031 static double get(uint8_t *a[], int ch, int index, int ch_count, enum AVSampleFormat f){
00032 const uint8_t *p;
00033 if(av_sample_fmt_is_planar(f)){
00034 f= av_get_alt_sample_fmt(f, 0);
00035 p= a[ch];
00036 }else{
00037 p= a[0];
00038 index= ch + index*ch_count;
00039 }
00040
00041 switch(f){
00042 case AV_SAMPLE_FMT_U8 : return ((const uint8_t*)p)[index]/255.0*2-1.0;
00043 case AV_SAMPLE_FMT_S16: return ((const int16_t*)p)[index]/32767.0;
00044 case AV_SAMPLE_FMT_S32: return ((const int32_t*)p)[index]/2147483647.0;
00045 case AV_SAMPLE_FMT_FLT: return ((const float *)p)[index];
00046 case AV_SAMPLE_FMT_DBL: return ((const double *)p)[index];
00047 default: av_assert0(0);
00048 }
00049 }
00050
00051 static void set(uint8_t *a[], int ch, int index, int ch_count, enum AVSampleFormat f, double v){
00052 uint8_t *p;
00053 if(av_sample_fmt_is_planar(f)){
00054 f= av_get_alt_sample_fmt(f, 0);
00055 p= a[ch];
00056 }else{
00057 p= a[0];
00058 index= ch + index*ch_count;
00059 }
00060 switch(f){
00061 case AV_SAMPLE_FMT_U8 : ((uint8_t*)p)[index]= (v+1.0)*255.0/2; break;
00062 case AV_SAMPLE_FMT_S16: ((int16_t*)p)[index]= v*32767; break;
00063 case AV_SAMPLE_FMT_S32: ((int32_t*)p)[index]= v*2147483647; break;
00064 case AV_SAMPLE_FMT_FLT: ((float *)p)[index]= v; break;
00065 case AV_SAMPLE_FMT_DBL: ((double *)p)[index]= v; break;
00066 default: av_assert2(0);
00067 }
00068 }
00069
00070 uint64_t layouts[]={
00071 AV_CH_LAYOUT_MONO ,
00072 AV_CH_LAYOUT_STEREO ,
00073 AV_CH_LAYOUT_2_1 ,
00074 AV_CH_LAYOUT_SURROUND ,
00075 AV_CH_LAYOUT_4POINT0 ,
00076 AV_CH_LAYOUT_2_2 ,
00077 AV_CH_LAYOUT_QUAD ,
00078 AV_CH_LAYOUT_5POINT0 ,
00079 AV_CH_LAYOUT_5POINT1 ,
00080 AV_CH_LAYOUT_5POINT0_BACK ,
00081 AV_CH_LAYOUT_5POINT1_BACK ,
00082 AV_CH_LAYOUT_7POINT0 ,
00083 AV_CH_LAYOUT_7POINT1 ,
00084 AV_CH_LAYOUT_7POINT1_WIDE ,
00085 0
00086 };
00087
00088 static void setup_array(uint8_t *out[SWR_CH_MAX], uint8_t *in, enum AVSampleFormat format, int samples){
00089 if(av_sample_fmt_is_planar(format)){
00090 int i;
00091 int plane_size= av_get_bytes_per_sample(format&0xFF)*samples;
00092 format&=0xFF;
00093 for(i=0; i<SWR_CH_MAX; i++){
00094 out[i]= in + i*plane_size;
00095 }
00096 }else{
00097 out[0]= in;
00098 }
00099 }
00100
00101 int main(int argc, char **argv){
00102 int in_sample_rate, out_sample_rate, ch ,i, in_ch_layout_index, out_ch_layout_index, osr, flush_count;
00103 uint64_t in_ch_layout, out_ch_layout;
00104 enum AVSampleFormat in_sample_fmt, out_sample_fmt;
00105 int sample_rates[]={8000,11025,16000,22050,32000};
00106 uint8_t array_in[SAMPLES*8*8];
00107 uint8_t array_mid[SAMPLES*8*8*3];
00108 uint8_t array_out[SAMPLES*8*8+100];
00109 uint8_t *ain[SWR_CH_MAX];
00110 uint8_t *aout[SWR_CH_MAX];
00111 uint8_t *amid[SWR_CH_MAX];
00112
00113 struct SwrContext * forw_ctx= NULL;
00114 struct SwrContext *backw_ctx= NULL;
00115
00116 in_sample_rate=16000;
00117 for(osr=0; osr<5; osr++){
00118 out_sample_rate= sample_rates[osr];
00119 for(in_sample_fmt= AV_SAMPLE_FMT_U8; in_sample_fmt<=AV_SAMPLE_FMT_DBL; in_sample_fmt++){
00120 for(out_sample_fmt= AV_SAMPLE_FMT_U8; out_sample_fmt<=AV_SAMPLE_FMT_DBL; out_sample_fmt++){
00121 for(in_ch_layout_index=0; layouts[in_ch_layout_index]; in_ch_layout_index++){
00122 int in_ch_count;
00123 in_ch_layout= layouts[in_ch_layout_index];
00124 in_ch_count= av_get_channel_layout_nb_channels(in_ch_layout);
00125 for(out_ch_layout_index=0; layouts[out_ch_layout_index]; out_ch_layout_index++){
00126 int out_count, mid_count, out_ch_count;
00127 out_ch_layout= layouts[out_ch_layout_index];
00128 out_ch_count= av_get_channel_layout_nb_channels(out_ch_layout);
00129 fprintf(stderr, "ch %d->%d, rate:%5d->%5d, fmt:%s->%s",
00130 in_ch_count, out_ch_count,
00131 in_sample_rate, out_sample_rate,
00132 av_get_sample_fmt_name(in_sample_fmt), av_get_sample_fmt_name(out_sample_fmt));
00133 forw_ctx = swr_alloc_set_opts(forw_ctx, out_ch_layout, av_get_alt_sample_fmt(out_sample_fmt, 1), out_sample_rate,
00134 in_ch_layout, av_get_alt_sample_fmt( in_sample_fmt, 1), in_sample_rate,
00135 0, 0);
00136 backw_ctx = swr_alloc_set_opts(backw_ctx, in_ch_layout, in_sample_fmt, in_sample_rate,
00137 out_ch_layout, av_get_alt_sample_fmt(out_sample_fmt, 1), out_sample_rate,
00138 0, 0);
00139 if(swr_init( forw_ctx) < 0)
00140 fprintf(stderr, "swr_init(->) failed\n");
00141 if(swr_init(backw_ctx) < 0)
00142 fprintf(stderr, "swr_init(<-) failed\n");
00143 if(!forw_ctx)
00144 fprintf(stderr, "Failed to init forw_cts\n");
00145 if(!backw_ctx)
00146 fprintf(stderr, "Failed to init backw_ctx\n");
00147
00148 setup_array(ain , array_in , av_get_alt_sample_fmt( in_sample_fmt, 1), SAMPLES);
00149 setup_array(amid, array_mid, av_get_alt_sample_fmt(out_sample_fmt, 1), 3*SAMPLES);
00150 setup_array(aout, array_out, in_sample_fmt , SAMPLES);
00151 for(ch=0; ch<in_ch_count; ch++){
00152 for(i=0; i<SAMPLES; i++)
00153 set(ain, ch, i, in_ch_count, av_get_alt_sample_fmt(in_sample_fmt, 1), sin(i*i*3/SAMPLES));
00154 }
00155 mid_count= swr_convert(forw_ctx, amid, 3*SAMPLES, ain, SAMPLES);
00156 out_count= swr_convert(backw_ctx,aout, SAMPLES, amid, mid_count);
00157
00158 for(ch=0; ch<in_ch_count; ch++){
00159 double sse, x, maxdiff=0;
00160 double sum_a= 0;
00161 double sum_b= 0;
00162 double sum_aa= 0;
00163 double sum_bb= 0;
00164 double sum_ab= 0;
00165 for(i=0; i<out_count; i++){
00166 double a= get(ain , ch, i, in_ch_count, av_get_alt_sample_fmt(in_sample_fmt, 1));
00167 double b= get(aout, ch, i, in_ch_count, in_sample_fmt);
00168 sum_a += a;
00169 sum_b += b;
00170 sum_aa+= a*a;
00171 sum_bb+= b*b;
00172 sum_ab+= a*b;
00173 maxdiff= FFMAX(maxdiff, FFABS(a-b));
00174 }
00175 x = sum_ab/sum_bb;
00176 sse= sum_aa + sum_bb*x*x - 2*x*sum_ab;
00177
00178 fprintf(stderr, "[%f %f %f] len:%5d\n", sqrt(sse/out_count), x, maxdiff, out_count);
00179 }
00180
00181 flush_count=swr_convert(backw_ctx,aout, SAMPLES, 0, 0);
00182 if(flush_count){
00183 for(ch=0; ch<in_ch_count; ch++){
00184 double sse, x, maxdiff=0;
00185 double sum_a= 0;
00186 double sum_b= 0;
00187 double sum_aa= 0;
00188 double sum_bb= 0;
00189 double sum_ab= 0;
00190 for(i=0; i<flush_count; i++){
00191 double a= get(ain , ch, i+out_count, in_ch_count, av_get_alt_sample_fmt(in_sample_fmt, 1));
00192 double b= get(aout, ch, i, in_ch_count, in_sample_fmt);
00193 sum_a += a;
00194 sum_b += b;
00195 sum_aa+= a*a;
00196 sum_bb+= b*b;
00197 sum_ab+= a*b;
00198 maxdiff= FFMAX(maxdiff, FFABS(a-b));
00199 }
00200 x = sum_ab/sum_bb;
00201 sse= sum_aa + sum_bb*x*x - 2*x*sum_ab;
00202
00203 fprintf(stderr, "[%f %f %f] len:%5d\n", sqrt(sse/flush_count), x, maxdiff, flush_count);
00204 }
00205 }
00206
00207
00208 fprintf(stderr, "\n");
00209 }
00210 }
00211 }
00212 }
00213 }
00214
00215 return 0;
00216 }