]> git.sesse.net Git - ffmpeg/blob - libswresample/swresample_test.c
diracdec: Check dirac_unpack_idwt_params parameters before storing them.
[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 uint64_t layouts[]={
71 AV_CH_LAYOUT_MONO                    ,
72 AV_CH_LAYOUT_STEREO                  ,
73 AV_CH_LAYOUT_2_1                     ,
74 AV_CH_LAYOUT_SURROUND                ,
75 AV_CH_LAYOUT_4POINT0                 ,
76 AV_CH_LAYOUT_2_2                     ,
77 AV_CH_LAYOUT_QUAD                    ,
78 AV_CH_LAYOUT_5POINT0                 ,
79 AV_CH_LAYOUT_5POINT1                 ,
80 AV_CH_LAYOUT_5POINT0_BACK            ,
81 AV_CH_LAYOUT_5POINT1_BACK            ,
82 AV_CH_LAYOUT_7POINT0                 ,
83 AV_CH_LAYOUT_7POINT1                 ,
84 AV_CH_LAYOUT_7POINT1_WIDE            ,
85 0
86 };
87
88 static void setup_array(uint8_t *out[SWR_CH_MAX], uint8_t *in, enum AVSampleFormat format, int samples){
89     if(av_sample_fmt_is_planar(format)){
90         int i;
91         int plane_size= av_get_bytes_per_sample(format&0xFF)*samples;
92         format&=0xFF;
93         for(i=0; i<SWR_CH_MAX; i++){
94             out[i]= in + i*plane_size;
95         }
96     }else{
97         out[0]= in;
98     }
99 }
100
101 int main(int argc, char **argv){
102     int in_sample_rate, out_sample_rate, ch ,i, in_ch_layout_index, out_ch_layout_index, osr, flush_count;
103     uint64_t in_ch_layout, out_ch_layout;
104     enum AVSampleFormat in_sample_fmt, out_sample_fmt;
105     int sample_rates[]={8000,11025,16000,22050,32000};
106     uint8_t array_in[SAMPLES*8*8];
107     uint8_t array_mid[SAMPLES*8*8*3];
108     uint8_t array_out[SAMPLES*8*8+100];
109     uint8_t *ain[SWR_CH_MAX];
110     uint8_t *aout[SWR_CH_MAX];
111     uint8_t *amid[SWR_CH_MAX];
112
113     struct SwrContext * forw_ctx= NULL;
114     struct SwrContext *backw_ctx= NULL;
115
116     in_sample_rate=16000;
117     for(osr=0; osr<5; osr++){
118         out_sample_rate= sample_rates[osr];
119         for(in_sample_fmt= AV_SAMPLE_FMT_U8; in_sample_fmt<=AV_SAMPLE_FMT_DBL; in_sample_fmt++){
120             for(out_sample_fmt= AV_SAMPLE_FMT_U8; out_sample_fmt<=AV_SAMPLE_FMT_DBL; out_sample_fmt++){
121                 for(in_ch_layout_index=0; layouts[in_ch_layout_index]; in_ch_layout_index++){
122                     int in_ch_count;
123                     in_ch_layout= layouts[in_ch_layout_index];
124                     in_ch_count= av_get_channel_layout_nb_channels(in_ch_layout);
125                     for(out_ch_layout_index=0; layouts[out_ch_layout_index]; out_ch_layout_index++){
126                         int out_count, mid_count, out_ch_count;
127                         out_ch_layout= layouts[out_ch_layout_index];
128                         out_ch_count= av_get_channel_layout_nb_channels(out_ch_layout);
129                         fprintf(stderr, "ch %d->%d, rate:%5d->%5d, fmt:%s->%s",
130                                in_ch_count, out_ch_count,
131                                in_sample_rate, out_sample_rate,
132                                av_get_sample_fmt_name(in_sample_fmt), av_get_sample_fmt_name(out_sample_fmt));
133                         forw_ctx  = swr_alloc_set_opts(forw_ctx, out_ch_layout, av_get_alt_sample_fmt(out_sample_fmt, 1), out_sample_rate,
134                                                                   in_ch_layout, av_get_alt_sample_fmt( in_sample_fmt, 1),  in_sample_rate,
135                                                        0, 0);
136                         backw_ctx = swr_alloc_set_opts(backw_ctx, in_ch_layout,  in_sample_fmt,             in_sample_rate,
137                                                                  out_ch_layout, av_get_alt_sample_fmt(out_sample_fmt, 1), out_sample_rate,
138                                                        0, 0);
139                         if(swr_init( forw_ctx) < 0)
140                             fprintf(stderr, "swr_init(->) failed\n");
141                         if(swr_init(backw_ctx) < 0)
142                             fprintf(stderr, "swr_init(<-) failed\n");
143                         if(!forw_ctx)
144                             fprintf(stderr, "Failed to init forw_cts\n");
145                         if(!backw_ctx)
146                             fprintf(stderr, "Failed to init backw_ctx\n");
147                                //FIXME test planar
148                         setup_array(ain , array_in , av_get_alt_sample_fmt( in_sample_fmt, 1),   SAMPLES);
149                         setup_array(amid, array_mid, av_get_alt_sample_fmt(out_sample_fmt, 1), 3*SAMPLES);
150                         setup_array(aout, array_out,  in_sample_fmt           ,   SAMPLES);
151                         for(ch=0; ch<in_ch_count; ch++){
152                             for(i=0; i<SAMPLES; i++)
153                                 set(ain, ch, i, in_ch_count, av_get_alt_sample_fmt(in_sample_fmt, 1), sin(i*i*3/SAMPLES));
154                         }
155                         mid_count= swr_convert(forw_ctx, amid, 3*SAMPLES, ain, SAMPLES);
156                         out_count= swr_convert(backw_ctx,aout, SAMPLES, amid, mid_count);
157
158                         for(ch=0; ch<in_ch_count; ch++){
159                             double sse, x, maxdiff=0;
160                             double sum_a= 0;
161                             double sum_b= 0;
162                             double sum_aa= 0;
163                             double sum_bb= 0;
164                             double sum_ab= 0;
165                             for(i=0; i<out_count; i++){
166                                 double a= get(ain , ch, i, in_ch_count, av_get_alt_sample_fmt(in_sample_fmt, 1));
167                                 double b= get(aout, ch, i, in_ch_count, in_sample_fmt);
168                                 sum_a += a;
169                                 sum_b += b;
170                                 sum_aa+= a*a;
171                                 sum_bb+= b*b;
172                                 sum_ab+= a*b;
173                                 maxdiff= FFMAX(maxdiff, FFABS(a-b));
174                             }
175                             x = sum_ab/sum_bb;
176                             sse= sum_aa + sum_bb*x*x - 2*x*sum_ab;
177
178                             fprintf(stderr, "[%f %f %f] len:%5d\n", sqrt(sse/out_count), x, maxdiff, out_count);
179                         }
180
181                         flush_count=swr_convert(backw_ctx,aout, SAMPLES, 0, 0);
182                         if(flush_count){
183                             for(ch=0; ch<in_ch_count; ch++){
184                                 double sse, x, maxdiff=0;
185                                 double sum_a= 0;
186                                 double sum_b= 0;
187                                 double sum_aa= 0;
188                                 double sum_bb= 0;
189                                 double sum_ab= 0;
190                                 for(i=0; i<flush_count; i++){
191                                     double a= get(ain , ch, i+out_count, in_ch_count, av_get_alt_sample_fmt(in_sample_fmt, 1));
192                                     double b= get(aout, ch, i, in_ch_count, in_sample_fmt);
193                                     sum_a += a;
194                                     sum_b += b;
195                                     sum_aa+= a*a;
196                                     sum_bb+= b*b;
197                                     sum_ab+= a*b;
198                                     maxdiff= FFMAX(maxdiff, FFABS(a-b));
199                                 }
200                                 x = sum_ab/sum_bb;
201                                 sse= sum_aa + sum_bb*x*x - 2*x*sum_ab;
202
203                                 fprintf(stderr, "[%f %f %f] len:%5d\n", sqrt(sse/flush_count), x, maxdiff, flush_count);
204                             }
205                         }
206
207
208                         fprintf(stderr, "\n");
209                     }
210                 }
211             }
212         }
213     }
214
215     return 0;
216 }