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