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