Renamed SKP_[u]int* to opus_[u]int*
[opus.git] / silk / silk_debug.h
1 /***********************************************************************\r
2 Copyright (c) 2006-2011, Skype Limited. All rights reserved. \r
3 Redistribution and use in source and binary forms, with or without \r
4 modification, (subject to the limitations in the disclaimer below) \r
5 are permitted provided that the following conditions are met:\r
6 - Redistributions of source code must retain the above copyright notice,\r
7 this list of conditions and the following disclaimer.\r
8 - Redistributions in binary form must reproduce the above copyright \r
9 notice, this list of conditions and the following disclaimer in the \r
10 documentation and/or other materials provided with the distribution.\r
11 - Neither the name of Skype Limited, nor the names of specific \r
12 contributors, may be used to endorse or promote products derived from \r
13 this software without specific prior written permission.\r
14 NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED \r
15 BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND \r
16 CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,\r
17 BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND \r
18 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE \r
19 COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, \r
20 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\r
21 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF \r
22 USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON \r
23 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT \r
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE \r
25 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
26 ***********************************************************************/\r
27 \r
28 #ifndef _SILK_DEBUG_H_\r
29 #define _SILK_DEBUG_H_\r
30 \r
31 #ifdef _WIN32\r
32 #define _CRT_SECURE_NO_DEPRECATE    1\r
33 #endif\r
34 \r
35 #include "silk_typedef.h"\r
36 #include <stdio.h>      /* file writing */\r
37 #include <string.h>     /* strcpy, strcmp */\r
38 \r
39 #ifdef  __cplusplus\r
40 extern "C"\r
41 {\r
42 #endif\r
43 \r
44 unsigned long GetHighResolutionTime(void); /* O: time in usec*/\r
45 \r
46 /* make SILK_DEBUG dependent on compiler's _DEBUG */\r
47 #if defined _WIN32\r
48     #ifdef _DEBUG\r
49         #define SILK_DEBUG  1\r
50     #else\r
51         #define SILK_DEBUG  0\r
52     #endif\r
53 \r
54     /* overrule the above */\r
55     #if 0\r
56     //  #define NO_ASSERTS\r
57     #undef  SILK_DEBUG  \r
58     #define SILK_DEBUG  1\r
59     #endif\r
60 #else\r
61     #define SILK_DEBUG  0\r
62 #endif\r
63 \r
64 /* Flag for using timers */\r
65 #define SILK_TIC_TOC 0\r
66 \r
67 #if SILK_TIC_TOC\r
68 \r
69 #if (defined(_WIN32) || defined(_WINCE)) \r
70 #include <windows.h>    /* timer */\r
71 #pragma warning( disable : 4996 )       // stop bitching about strcpy in TIC()\r
72 #else   // Linux or Mac\r
73 #include <sys/time.h>\r
74 #endif\r
75 \r
76 /*********************************/\r
77 /* timer functions for profiling */\r
78 /*********************************/\r
79 /* example:                                                         */\r
80 /*                                                                  */\r
81 /* TIC(LPC)                                                         */\r
82 /* do_LPC(in_vec, order, acoef);    // do LPC analysis              */\r
83 /* TOC(LPC)                                                         */\r
84 /*                                                                  */\r
85 /* and call the following just before exiting (from main)           */\r
86 /*                                                                  */\r
87 /* silk_TimerSave("SKP_TimingData.txt");                             */\r
88 /*                                                                  */\r
89 /* results are now in SKP_TimingData.txt                            */\r
90 \r
91 void silk_TimerSave(char *file_name);\r
92 \r
93 /* max number of timers (in different locations) */\r
94 #define SKP_NUM_TIMERS_MAX                  50\r
95 /* max length of name tags in TIC(..), TOC(..) */\r
96 #define SKP_NUM_TIMERS_MAX_TAG_LEN          30\r
97 \r
98 extern int           silk_Timer_nTimers;\r
99 extern int           silk_Timer_depth_ctr;\r
100 extern char          silk_Timer_tags[SKP_NUM_TIMERS_MAX][SKP_NUM_TIMERS_MAX_TAG_LEN];\r
101 #ifdef _WIN32\r
102 extern LARGE_INTEGER silk_Timer_start[SKP_NUM_TIMERS_MAX];\r
103 #else\r
104 extern unsigned long silk_Timer_start[SKP_NUM_TIMERS_MAX];\r
105 #endif  \r
106 extern unsigned int  silk_Timer_cnt[SKP_NUM_TIMERS_MAX];\r
107 extern opus_int64     silk_Timer_sum[SKP_NUM_TIMERS_MAX];\r
108 extern opus_int64     silk_Timer_max[SKP_NUM_TIMERS_MAX];\r
109 extern opus_int64     silk_Timer_min[SKP_NUM_TIMERS_MAX];\r
110 extern opus_int64     silk_Timer_depth[SKP_NUM_TIMERS_MAX];\r
111 \r
112 /* WARNING: TIC()/TOC can measure only up to 0.1 seconds at a time */\r
113 #ifdef _WIN32\r
114 #define TIC(TAG_NAME) {                                     \\r
115     static int init = 0;                                    \\r
116     static int ID = -1;                                     \\r
117     if( init == 0 )                                         \\r
118     {                                                       \\r
119         int k;                                              \\r
120         init = 1;                                           \\r
121         for( k = 0; k < silk_Timer_nTimers; k++ ) {          \\r
122             if( strcmp(silk_Timer_tags[k], #TAG_NAME) == 0 ) {   \\r
123                 ID = k;                                     \\r
124                 break;                                      \\r
125             }                                               \\r
126         }                                                   \\r
127         if (ID == -1) {                                     \\r
128             ID = silk_Timer_nTimers;                         \\r
129             silk_Timer_nTimers++;                            \\r
130             silk_Timer_depth[ID] = silk_Timer_depth_ctr;      \\r
131             strcpy(silk_Timer_tags[ID], #TAG_NAME);          \\r
132             silk_Timer_cnt[ID] = 0;                          \\r
133             silk_Timer_sum[ID] = 0;                          \\r
134             silk_Timer_min[ID] = 0xFFFFFFFF;                 \\r
135             silk_Timer_max[ID] = 0;                          \\r
136         }                                                   \\r
137     }                                                       \\r
138     silk_Timer_depth_ctr++;                                  \\r
139     QueryPerformanceCounter(&silk_Timer_start[ID]);          \\r
140 }\r
141 #else\r
142 #define TIC(TAG_NAME) {                                     \\r
143     static int init = 0;                                    \\r
144     static int ID = -1;                                     \\r
145     if( init == 0 )                                         \\r
146     {                                                       \\r
147         int k;                                              \\r
148         init = 1;                                           \\r
149         for( k = 0; k < silk_Timer_nTimers; k++ ) {          \\r
150         if( strcmp(silk_Timer_tags[k], #TAG_NAME) == 0 ) {   \\r
151                 ID = k;                                     \\r
152                 break;                                      \\r
153             }                                               \\r
154         }                                                   \\r
155         if (ID == -1) {                                     \\r
156             ID = silk_Timer_nTimers;                         \\r
157             silk_Timer_nTimers++;                            \\r
158             silk_Timer_depth[ID] = silk_Timer_depth_ctr;      \\r
159             strcpy(silk_Timer_tags[ID], #TAG_NAME);          \\r
160             silk_Timer_cnt[ID] = 0;                          \\r
161             silk_Timer_sum[ID] = 0;                          \\r
162             silk_Timer_min[ID] = 0xFFFFFFFF;                 \\r
163             silk_Timer_max[ID] = 0;                          \\r
164         }                                                   \\r
165     }                                                       \\r
166     silk_Timer_depth_ctr++;                                  \\r
167     silk_Timer_start[ID] = GetHighResolutionTime();          \\r
168 }\r
169 #endif\r
170 \r
171 #ifdef _WIN32\r
172 #define TOC(TAG_NAME) {                                             \\r
173     LARGE_INTEGER lpPerformanceCount;                               \\r
174     static int init = 0;                                            \\r
175     static int ID = 0;                                              \\r
176     if( init == 0 )                                                 \\r
177     {                                                               \\r
178         int k;                                                      \\r
179         init = 1;                                                   \\r
180         for( k = 0; k < silk_Timer_nTimers; k++ ) {                  \\r
181             if( strcmp(silk_Timer_tags[k], #TAG_NAME) == 0 ) {       \\r
182                 ID = k;                                             \\r
183                 break;                                              \\r
184             }                                                       \\r
185         }                                                           \\r
186     }                                                               \\r
187     QueryPerformanceCounter(&lpPerformanceCount);                   \\r
188     lpPerformanceCount.QuadPart -= silk_Timer_start[ID].QuadPart;    \\r
189     if((lpPerformanceCount.QuadPart < 100000000) &&                 \\r
190         (lpPerformanceCount.QuadPart >= 0)) {                       \\r
191         silk_Timer_cnt[ID]++;                                        \\r
192         silk_Timer_sum[ID] += lpPerformanceCount.QuadPart;           \\r
193         if( lpPerformanceCount.QuadPart > silk_Timer_max[ID] )       \\r
194             silk_Timer_max[ID] = lpPerformanceCount.QuadPart;        \\r
195         if( lpPerformanceCount.QuadPart < silk_Timer_min[ID] )       \\r
196             silk_Timer_min[ID] = lpPerformanceCount.QuadPart;        \\r
197     }                                                               \\r
198     silk_Timer_depth_ctr--;                                          \\r
199 }\r
200 #else\r
201 #define TOC(TAG_NAME) {                                             \\r
202     unsigned long endTime;                                          \\r
203     static int init = 0;                                            \\r
204     static int ID = 0;                                              \\r
205     if( init == 0 )                                                 \\r
206     {                                                               \\r
207         int k;                                                      \\r
208         init = 1;                                                   \\r
209         for( k = 0; k < silk_Timer_nTimers; k++ ) {                  \\r
210             if( strcmp(silk_Timer_tags[k], #TAG_NAME) == 0 ) {       \\r
211                 ID = k;                                             \\r
212                 break;                                              \\r
213             }                                                       \\r
214         }                                                           \\r
215     }                                                               \\r
216     endTime = GetHighResolutionTime();                              \\r
217     endTime -= silk_Timer_start[ID];                                 \\r
218     if((endTime < 100000000) &&                                     \\r
219         (endTime >= 0)) {                                           \\r
220         silk_Timer_cnt[ID]++;                                        \\r
221         silk_Timer_sum[ID] += endTime;                               \\r
222         if( endTime > silk_Timer_max[ID] )                           \\r
223             silk_Timer_max[ID] = endTime;                            \\r
224         if( endTime < silk_Timer_min[ID] )                           \\r
225             silk_Timer_min[ID] = endTime;                            \\r
226     }                                                               \\r
227         silk_Timer_depth_ctr--;                                      \\r
228 }\r
229 #endif\r
230 \r
231 #else /* SILK_TIC_TOC */\r
232 \r
233 /* define macros as empty strings */\r
234 #define TIC(TAG_NAME)\r
235 #define TOC(TAG_NAME)\r
236 #define silk_TimerSave(FILE_NAME)\r
237 \r
238 #endif /* SILK_TIC_TOC */\r
239 \r
240 \r
241 \r
242 #if SILK_DEBUG\r
243 /************************************/\r
244 /* write data to file for debugging */\r
245 /************************************/\r
246 /* opens an empty file if this file has not yet been open, then writes to the file and closes it            */\r
247 /* if file has been open previously it is opened again and the fwrite is appending, finally it is closed    */\r
248 #define SAVE_DATA( FILE_NAME, DATA_PTR, N_BYTES ) {                 \\r
249     static opus_int32 init = 0;                                      \\r
250     FILE *fp;                                                       \\r
251     if (init == 0)      {                                               \\r
252         init = 1;                                                   \\r
253         fp = fopen(#FILE_NAME, "wb");                               \\r
254     } else {                                                        \\r
255         fp = fopen(#FILE_NAME, "ab+");                              \\r
256     }                                                               \\r
257     fwrite((DATA_PTR), (N_BYTES), 1, fp);                           \\r
258     fclose(fp);                                                     \\r
259 }       \r
260 \r
261 /* Example: DEBUG_STORE_DATA(testfile.pcm, &RIN[0], 160*sizeof(opus_int16)); */\r
262 \r
263 #if 0\r
264 /* Ensure that everything is written to files when an assert breaks */\r
265 #define DEBUG_STORE_DATA(FILE_NAME, DATA_PTR, N_BYTES) SAVE_DATA(FILE_NAME, DATA_PTR, N_BYTES)\r
266 #define SILK_DEBUG_STORE_CLOSE_FILES\r
267 \r
268 #else\r
269 \r
270 #define SKP_NUM_STORES_MAX                                  100\r
271 extern FILE *silk_debug_store_fp[ SKP_NUM_STORES_MAX ];\r
272 extern int silk_debug_store_count;\r
273 \r
274 /* Faster way of storing the data */\r
275 #define DEBUG_STORE_DATA( FILE_NAME, DATA_PTR, N_BYTES ) {          \\r
276     static opus_int init = 0, cnt = 0;                               \\r
277     static FILE **fp;                                               \\r
278     if (init == 0) {                                                \\r
279         init = 1;                                                                                               \\r
280         cnt = silk_debug_store_count++;                             \\r
281         silk_debug_store_fp[ cnt ] = fopen(#FILE_NAME, "wb");       \\r
282     }                                                               \\r
283     fwrite((DATA_PTR), (N_BYTES), 1, silk_debug_store_fp[ cnt ]);   \\r
284 }\r
285 \r
286 /* Call this at the end of main() */\r
287 #define SILK_DEBUG_STORE_CLOSE_FILES {                              \\r
288     opus_int i;                                                      \\r
289     for( i = 0; i < silk_debug_store_count; i++ ) {                 \\r
290         fclose( silk_debug_store_fp[ i ] );                         \\r
291     }                                                               \\r
292 }\r
293 #endif\r
294 \r
295 /* micro sec */\r
296 #define SKP_GETTIME(void)       time = (opus_int64) silk_GetHighResolutionTime();     \r
297 \r
298 #else /* SILK_DEBUG */\r
299 \r
300 /* define macros as empty strings */\r
301 #define DEBUG_STORE_DATA(FILE_NAME, DATA_PTR, N_BYTES)\r
302 #define SAVE_DATA(FILE_NAME, DATA_PTR, N_BYTES)\r
303 #define SILK_DEBUG_STORE_CLOSE_FILES\r
304 \r
305 #endif /* SILK_DEBUG */\r
306 \r
307 #ifdef  __cplusplus\r
308 }\r
309 #endif\r
310 \r
311 #endif /* _SILK_DEBUG_H_ */\r