]> git.sesse.net Git - ffmpeg/blob - libswresample/swresample_test.c
swr-test: add rates tables containing a new set of sample rates.
[ffmpeg] / libswresample / swresample_test.c
1 /*
2  * Copyright (C) 2011 Michael Niedermayer (michaelni@gmx.at)
3  *
4  * This file is part of libswresample
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "libavutil/avassert.h"
22 #include "libavutil/common.h"
23 #include "libavutil/audioconvert.h"
24 #include "libavutil/opt.h"
25 #include "swresample.h"
26 #undef fprintf
27
28 #define SAMPLES 1000
29
30 #define ASSERT_LEVEL 2
31
32 static double get(uint8_t *a[], int ch, int index, int ch_count, enum AVSampleFormat f){
33     const uint8_t *p;
34     if(av_sample_fmt_is_planar(f)){
35         f= av_get_alt_sample_fmt(f, 0);
36         p= a[ch];
37     }else{
38         p= a[0];
39         index= ch + index*ch_count;
40     }
41
42     switch(f){
43     case AV_SAMPLE_FMT_U8 : return ((const uint8_t*)p)[index]/255.0*2-1.0;
44     case AV_SAMPLE_FMT_S16: return ((const int16_t*)p)[index]/32767.0;
45     case AV_SAMPLE_FMT_S32: return ((const int32_t*)p)[index]/2147483647.0;
46     case AV_SAMPLE_FMT_FLT: return ((const float  *)p)[index];
47     case AV_SAMPLE_FMT_DBL: return ((const double *)p)[index];
48     default: av_assert0(0);
49     }
50 }
51
52 static void  set(uint8_t *a[], int ch, int index, int ch_count, enum AVSampleFormat f, double v){
53     uint8_t *p;
54     if(av_sample_fmt_is_planar(f)){
55         f= av_get_alt_sample_fmt(f, 0);
56         p= a[ch];
57     }else{
58         p= a[0];
59         index= ch + index*ch_count;
60     }
61     switch(f){
62     case AV_SAMPLE_FMT_U8 : ((uint8_t*)p)[index]= (v+1.0)*255.0/2; break;
63     case AV_SAMPLE_FMT_S16: ((int16_t*)p)[index]= v*32767;         break;
64     case AV_SAMPLE_FMT_S32: ((int32_t*)p)[index]= v*2147483647;    break;
65     case AV_SAMPLE_FMT_FLT: ((float  *)p)[index]= v;               break;
66     case AV_SAMPLE_FMT_DBL: ((double *)p)[index]= v;               break;
67     default: av_assert2(0);
68     }
69 }
70
71 static void shift(uint8_t *a[], int index, int ch_count, enum AVSampleFormat f){
72     int ch;
73
74     if(av_sample_fmt_is_planar(f)){
75         f= av_get_alt_sample_fmt(f, 0);
76         for(ch= 0; ch<ch_count; ch++)
77             a[ch] += index*av_get_bytes_per_sample(f);
78     }else{
79         a[0] += index*ch_count*av_get_bytes_per_sample(f);
80     }
81 }
82
83 static const enum AVSampleFormat formats[] = {
84     AV_SAMPLE_FMT_S16,
85     AV_SAMPLE_FMT_FLTP,
86     AV_SAMPLE_FMT_S16P,
87     AV_SAMPLE_FMT_FLT,
88     AV_SAMPLE_FMT_S32P,
89     AV_SAMPLE_FMT_S32,
90     AV_SAMPLE_FMT_U8P,
91     AV_SAMPLE_FMT_U8,
92     AV_SAMPLE_FMT_DBLP,
93     AV_SAMPLE_FMT_DBL,
94 };
95
96 static const int rates[] = {
97     8000,
98     11025,
99     16000,
100     22050,
101     32000,
102     48000,
103 };
104
105 uint64_t layouts[]={
106     AV_CH_LAYOUT_MONO                    ,
107     AV_CH_LAYOUT_STEREO                  ,
108     AV_CH_LAYOUT_2_1                     ,
109     AV_CH_LAYOUT_SURROUND                ,
110     AV_CH_LAYOUT_4POINT0                 ,
111     AV_CH_LAYOUT_2_2                     ,
112     AV_CH_LAYOUT_QUAD                    ,
113     AV_CH_LAYOUT_5POINT0                 ,
114     AV_CH_LAYOUT_5POINT1                 ,
115     AV_CH_LAYOUT_5POINT0_BACK            ,
116     AV_CH_LAYOUT_5POINT1_BACK            ,
117     AV_CH_LAYOUT_7POINT0                 ,
118     AV_CH_LAYOUT_7POINT1                 ,
119     AV_CH_LAYOUT_7POINT1_WIDE            ,
120 0
121 };
122
123 static void setup_array(uint8_t *out[SWR_CH_MAX], uint8_t *in, enum AVSampleFormat format, int samples){
124     if(av_sample_fmt_is_planar(format)){
125         int i;
126         int plane_size= av_get_bytes_per_sample(format&0xFF)*samples;
127         format&=0xFF;
128         for(i=0; i<SWR_CH_MAX; i++){
129             out[i]= in + i*plane_size;
130         }
131     }else{
132         out[0]= in;
133     }
134 }
135
136 int main(int argc, char **argv){
137     int in_sample_rate, out_sample_rate, ch ,i, in_ch_layout_index, out_ch_layout_index, osr, flush_count;
138     uint64_t in_ch_layout, out_ch_layout;
139     enum AVSampleFormat in_sample_fmt, out_sample_fmt;
140     uint8_t array_in[SAMPLES*8*8];
141     uint8_t array_mid[SAMPLES*8*8*3];
142     uint8_t array_out[SAMPLES*8*8+100];
143     uint8_t *ain[SWR_CH_MAX];
144     uint8_t *aout[SWR_CH_MAX];
145     uint8_t *amid[SWR_CH_MAX];
146     int flush_i=0;
147     int mode = 0;
148
149     struct SwrContext * forw_ctx= NULL;
150     struct SwrContext *backw_ctx= NULL;
151
152     in_sample_rate=16000;
153     for(osr=0; osr<FF_ARRAY_ELEMS(rates); osr++){
154         out_sample_rate= rates[osr];
155         for(in_sample_fmt= AV_SAMPLE_FMT_U8; in_sample_fmt<=AV_SAMPLE_FMT_DBL; in_sample_fmt++){
156             for(out_sample_fmt= AV_SAMPLE_FMT_U8; out_sample_fmt<=AV_SAMPLE_FMT_DBL; out_sample_fmt++){
157                 for(in_ch_layout_index=0; layouts[in_ch_layout_index]; in_ch_layout_index++){
158                     int in_ch_count;
159                     in_ch_layout= layouts[in_ch_layout_index];
160                     in_ch_count= av_get_channel_layout_nb_channels(in_ch_layout);
161                     for(out_ch_layout_index=0; layouts[out_ch_layout_index]; out_ch_layout_index++){
162                         int out_count, mid_count, out_ch_count;
163                         out_ch_layout= layouts[out_ch_layout_index];
164                         out_ch_count= av_get_channel_layout_nb_channels(out_ch_layout);
165                         fprintf(stderr, "ch %d->%d, rate:%5d->%5d, fmt:%s->%s\n",
166                                in_ch_count, out_ch_count,
167                                in_sample_rate, out_sample_rate,
168                                av_get_sample_fmt_name(in_sample_fmt), av_get_sample_fmt_name(out_sample_fmt));
169                         forw_ctx  = swr_alloc_set_opts(forw_ctx, out_ch_layout, av_get_alt_sample_fmt(out_sample_fmt, 1), out_sample_rate,
170                                                                   in_ch_layout, av_get_alt_sample_fmt( in_sample_fmt, 1),  in_sample_rate,
171                                                        0, 0);
172                         backw_ctx = swr_alloc_set_opts(backw_ctx, in_ch_layout,  in_sample_fmt,             in_sample_rate,
173                                                                  out_ch_layout, av_get_alt_sample_fmt(out_sample_fmt, 1), out_sample_rate,
174                                                        0, 0);
175                         if(swr_init( forw_ctx) < 0)
176                             fprintf(stderr, "swr_init(->) failed\n");
177                         if(swr_init(backw_ctx) < 0)
178                             fprintf(stderr, "swr_init(<-) failed\n");
179                         if(!forw_ctx)
180                             fprintf(stderr, "Failed to init forw_cts\n");
181                         if(!backw_ctx)
182                             fprintf(stderr, "Failed to init backw_ctx\n");
183                                //FIXME test planar
184                         setup_array(ain , array_in , av_get_alt_sample_fmt( in_sample_fmt, 1),   SAMPLES);
185                         setup_array(amid, array_mid, av_get_alt_sample_fmt(out_sample_fmt, 1), 3*SAMPLES);
186                         setup_array(aout, array_out,  in_sample_fmt           ,   SAMPLES);
187                         for(ch=0; ch<in_ch_count; ch++){
188                             for(i=0; i<SAMPLES; i++)
189                                 set(ain, ch, i, in_ch_count, av_get_alt_sample_fmt(in_sample_fmt, 1), sin(i*i*3/SAMPLES));
190                         }
191                         mode++;
192                         mode%=3;
193                         if(mode==0 /*|| out_sample_rate == in_sample_rate*/) {
194                             mid_count= swr_convert(forw_ctx, amid, 3*SAMPLES, ain, SAMPLES);
195                         } else if(mode==1){
196                             mid_count= swr_convert(forw_ctx, amid,         0, ain, SAMPLES);
197                             mid_count+=swr_convert(forw_ctx, amid, 3*SAMPLES, ain,       0);
198                         } else {
199                             int tmp_count;
200                             mid_count= swr_convert(forw_ctx, amid,         0, ain,       1);
201                             av_assert0(mid_count==0);
202                             shift(ain,  1, in_ch_count, av_get_alt_sample_fmt(in_sample_fmt, 1));
203                             mid_count+=swr_convert(forw_ctx, amid, 3*SAMPLES, ain,       0);
204                             shift(amid,  mid_count, out_ch_count, av_get_alt_sample_fmt(out_sample_fmt, 1)); tmp_count = mid_count;
205                             mid_count+=swr_convert(forw_ctx, amid,         2, ain,       2);
206                             shift(amid,  mid_count-tmp_count, out_ch_count, av_get_alt_sample_fmt(out_sample_fmt, 1)); tmp_count = mid_count;
207                             shift(ain,  2, in_ch_count, av_get_alt_sample_fmt(in_sample_fmt, 1));
208                             mid_count+=swr_convert(forw_ctx, amid,         1, ain, SAMPLES-3);
209                             shift(amid,  mid_count-tmp_count, out_ch_count, av_get_alt_sample_fmt(out_sample_fmt, 1)); tmp_count = mid_count;
210                             shift(ain, -3, in_ch_count, av_get_alt_sample_fmt(in_sample_fmt, 1));
211                             mid_count+=swr_convert(forw_ctx, amid, 3*SAMPLES, ain,       0);
212                             shift(amid,  -tmp_count, out_ch_count, av_get_alt_sample_fmt(out_sample_fmt, 1));
213                         }
214                         out_count= swr_convert(backw_ctx,aout, SAMPLES, amid, mid_count);
215
216                         for(ch=0; ch<in_ch_count; ch++){
217                             double sse, x, maxdiff=0;
218                             double sum_a= 0;
219                             double sum_b= 0;
220                             double sum_aa= 0;
221                             double sum_bb= 0;
222                             double sum_ab= 0;
223                             for(i=0; i<out_count; i++){
224                                 double a= get(ain , ch, i, in_ch_count, av_get_alt_sample_fmt(in_sample_fmt, 1));
225                                 double b= get(aout, ch, i, in_ch_count, in_sample_fmt);
226                                 sum_a += a;
227                                 sum_b += b;
228                                 sum_aa+= a*a;
229                                 sum_bb+= b*b;
230                                 sum_ab+= a*b;
231                                 maxdiff= FFMAX(maxdiff, FFABS(a-b));
232                             }
233                             x = sum_ab/sum_bb;
234                             sse= sum_aa + sum_bb*x*x - 2*x*sum_ab;
235
236                             fprintf(stderr, "[%f %f %f] len:%5d\n", sqrt(sse/out_count), x, maxdiff, out_count);
237                         }
238
239                         flush_i++;
240                         flush_i%=21;
241                         flush_count = swr_convert(backw_ctx,aout, flush_i, 0, 0);
242                         shift(aout,  flush_i, in_ch_count, in_sample_fmt);
243                         flush_count+= swr_convert(backw_ctx,aout, SAMPLES-flush_i, 0, 0);
244                         shift(aout, -flush_i, in_ch_count, in_sample_fmt);
245                         if(flush_count){
246                             for(ch=0; ch<in_ch_count; ch++){
247                                 double sse, x, maxdiff=0;
248                                 double sum_a= 0;
249                                 double sum_b= 0;
250                                 double sum_aa= 0;
251                                 double sum_bb= 0;
252                                 double sum_ab= 0;
253                                 for(i=0; i<flush_count; i++){
254                                     double a= get(ain , ch, i+out_count, in_ch_count, av_get_alt_sample_fmt(in_sample_fmt, 1));
255                                     double b= get(aout, ch, i, in_ch_count, in_sample_fmt);
256                                     sum_a += a;
257                                     sum_b += b;
258                                     sum_aa+= a*a;
259                                     sum_bb+= b*b;
260                                     sum_ab+= a*b;
261                                     maxdiff= FFMAX(maxdiff, FFABS(a-b));
262                                 }
263                                 x = sum_ab/sum_bb;
264                                 sse= sum_aa + sum_bb*x*x - 2*x*sum_ab;
265
266                                 fprintf(stderr, "[%f %f %f] len:%5d F:%3d\n", sqrt(sse/flush_count), x, maxdiff, flush_count, flush_i);
267                             }
268                         }
269
270
271                         fprintf(stderr, "\n");
272                     }
273                 }
274             }
275         }
276     }
277
278     return 0;
279 }