Conditionalize #pragma warn for MSVC.
[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, are permitted provided that the following conditions
5 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 Internet Society, IETF or IETF Trust, nor the 
12 names of specific contributors, may be used to endorse or promote
13 products derived from this software without specific prior written
14 permission.
15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 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 #if defined(_MSC_VER)
73 #pragma warning( disable : 4996 )       /* stop bitching about strcpy in TIC()*/
74 #endif
75 #else   /* Linux or Mac*/
76 #include <sys/time.h>
77 #endif
78
79 /*********************************/
80 /* timer functions for profiling */
81 /*********************************/
82 /* example:                                                         */
83 /*                                                                  */
84 /* TIC(LPC)                                                         */
85 /* do_LPC(in_vec, order, acoef);    // do LPC analysis              */
86 /* TOC(LPC)                                                         */
87 /*                                                                  */
88 /* and call the following just before exiting (from main)           */
89 /*                                                                  */
90 /* silk_TimerSave("silk_TimingData.txt");                           */
91 /*                                                                  */
92 /* results are now in silk_TimingData.txt                           */
93
94 void silk_TimerSave(char *file_name);
95
96 /* max number of timers (in different locations) */
97 #define silk_NUM_TIMERS_MAX                  50
98 /* max length of name tags in TIC(..), TOC(..) */
99 #define silk_NUM_TIMERS_MAX_TAG_LEN          30
100
101 extern int           silk_Timer_nTimers;
102 extern int           silk_Timer_depth_ctr;
103 extern char          silk_Timer_tags[silk_NUM_TIMERS_MAX][silk_NUM_TIMERS_MAX_TAG_LEN];
104 #ifdef _WIN32
105 extern LARGE_INTEGER silk_Timer_start[silk_NUM_TIMERS_MAX];
106 #else
107 extern unsigned long silk_Timer_start[silk_NUM_TIMERS_MAX];
108 #endif
109 extern unsigned int  silk_Timer_cnt[silk_NUM_TIMERS_MAX];
110 extern opus_int64    silk_Timer_sum[silk_NUM_TIMERS_MAX];
111 extern opus_int64    silk_Timer_max[silk_NUM_TIMERS_MAX];
112 extern opus_int64    silk_Timer_min[silk_NUM_TIMERS_MAX];
113 extern opus_int64    silk_Timer_depth[silk_NUM_TIMERS_MAX];
114
115 /* WARNING: TIC()/TOC can measure only up to 0.1 seconds at a time */
116 #ifdef _WIN32
117 #define TIC(TAG_NAME) {                                     \
118     static int init = 0;                                    \
119     static int ID = -1;                                     \
120     if( init == 0 )                                         \
121     {                                                       \
122         int k;                                              \
123         init = 1;                                           \
124         for( k = 0; k < silk_Timer_nTimers; k++ ) {         \
125             if( strcmp(silk_Timer_tags[k], #TAG_NAME) == 0 ) { \
126                 ID = k;                                     \
127                 break;                                      \
128             }                                               \
129         }                                                   \
130         if (ID == -1) {                                     \
131             ID = silk_Timer_nTimers;                        \
132             silk_Timer_nTimers++;                           \
133             silk_Timer_depth[ID] = silk_Timer_depth_ctr;    \
134             strcpy(silk_Timer_tags[ID], #TAG_NAME);         \
135             silk_Timer_cnt[ID] = 0;                         \
136             silk_Timer_sum[ID] = 0;                         \
137             silk_Timer_min[ID] = 0xFFFFFFFF;                \
138             silk_Timer_max[ID] = 0;                         \
139         }                                                   \
140     }                                                       \
141     silk_Timer_depth_ctr++;                                 \
142     QueryPerformanceCounter(&silk_Timer_start[ID]);         \
143 }
144 #else
145 #define TIC(TAG_NAME) {                                     \
146     static int init = 0;                                    \
147     static int ID = -1;                                     \
148     if( init == 0 )                                         \
149     {                                                       \
150         int k;                                              \
151         init = 1;                                           \
152         for( k = 0; k < silk_Timer_nTimers; k++ ) {         \
153         if( strcmp(silk_Timer_tags[k], #TAG_NAME) == 0 ) {  \
154                 ID = k;                                     \
155                 break;                                      \
156             }                                               \
157         }                                                   \
158         if (ID == -1) {                                     \
159             ID = silk_Timer_nTimers;                        \
160             silk_Timer_nTimers++;                           \
161             silk_Timer_depth[ID] = silk_Timer_depth_ctr;    \
162             strcpy(silk_Timer_tags[ID], #TAG_NAME);         \
163             silk_Timer_cnt[ID] = 0;                         \
164             silk_Timer_sum[ID] = 0;                         \
165             silk_Timer_min[ID] = 0xFFFFFFFF;                \
166             silk_Timer_max[ID] = 0;                         \
167         }                                                   \
168     }                                                       \
169     silk_Timer_depth_ctr++;                                 \
170     silk_Timer_start[ID] = GetHighResolutionTime();         \
171 }
172 #endif
173
174 #ifdef _WIN32
175 #define TOC(TAG_NAME) {                                             \
176     LARGE_INTEGER lpPerformanceCount;                               \
177     static int init = 0;                                            \
178     static int ID = 0;                                              \
179     if( init == 0 )                                                 \
180     {                                                               \
181         int k;                                                      \
182         init = 1;                                                   \
183         for( k = 0; k < silk_Timer_nTimers; k++ ) {                 \
184             if( strcmp(silk_Timer_tags[k], #TAG_NAME) == 0 ) {      \
185                 ID = k;                                             \
186                 break;                                              \
187             }                                                       \
188         }                                                           \
189     }                                                               \
190     QueryPerformanceCounter(&lpPerformanceCount);                   \
191     lpPerformanceCount.QuadPart -= silk_Timer_start[ID].QuadPart;   \
192     if((lpPerformanceCount.QuadPart < 100000000) &&                 \
193         (lpPerformanceCount.QuadPart >= 0)) {                       \
194         silk_Timer_cnt[ID]++;                                       \
195         silk_Timer_sum[ID] += lpPerformanceCount.QuadPart;          \
196         if( lpPerformanceCount.QuadPart > silk_Timer_max[ID] )      \
197             silk_Timer_max[ID] = lpPerformanceCount.QuadPart;       \
198         if( lpPerformanceCount.QuadPart < silk_Timer_min[ID] )      \
199             silk_Timer_min[ID] = lpPerformanceCount.QuadPart;       \
200     }                                                               \
201     silk_Timer_depth_ctr--;                                         \
202 }
203 #else
204 #define TOC(TAG_NAME) {                                             \
205     unsigned long endTime;                                          \
206     static int init = 0;                                            \
207     static int ID = 0;                                              \
208     if( init == 0 )                                                 \
209     {                                                               \
210         int k;                                                      \
211         init = 1;                                                   \
212         for( k = 0; k < silk_Timer_nTimers; k++ ) {                 \
213             if( strcmp(silk_Timer_tags[k], #TAG_NAME) == 0 ) {      \
214                 ID = k;                                             \
215                 break;                                              \
216             }                                                       \
217         }                                                           \
218     }                                                               \
219     endTime = GetHighResolutionTime();                              \
220     endTime -= silk_Timer_start[ID];                                \
221     if((endTime < 100000000) &&                                     \
222         (endTime >= 0)) {                                           \
223         silk_Timer_cnt[ID]++;                                       \
224         silk_Timer_sum[ID] += endTime;                              \
225         if( endTime > silk_Timer_max[ID] )                          \
226             silk_Timer_max[ID] = endTime;                           \
227         if( endTime < silk_Timer_min[ID] )                          \
228             silk_Timer_min[ID] = endTime;                           \
229     }                                                               \
230         silk_Timer_depth_ctr--;                                     \
231 }
232 #endif
233
234 #else /* SILK_TIC_TOC */
235
236 /* define macros as empty strings */
237 #define TIC(TAG_NAME)
238 #define TOC(TAG_NAME)
239 #define silk_TimerSave(FILE_NAME)
240
241 #endif /* SILK_TIC_TOC */
242
243
244 #if SILK_DEBUG
245 /************************************/
246 /* write data to file for debugging */
247 /************************************/
248 /* Example: DEBUG_STORE_DATA(testfile.pcm, &RIN[0], 160*sizeof(opus_int16)); */
249
250 #define silk_NUM_STORES_MAX                                  100
251 extern FILE *silk_debug_store_fp[ silk_NUM_STORES_MAX ];
252 extern int silk_debug_store_count;
253
254 /* Faster way of storing the data */
255 #define DEBUG_STORE_DATA( FILE_NAME, DATA_PTR, N_BYTES ) {          \
256     static opus_int init = 0, cnt = 0;                              \
257     static FILE **fp;                                               \
258     if (init == 0) {                                                \
259         init = 1;                                                   \
260         cnt = silk_debug_store_count++;                             \
261         silk_debug_store_fp[ cnt ] = fopen(#FILE_NAME, "wb");       \
262     }                                                               \
263     fwrite((DATA_PTR), (N_BYTES), 1, silk_debug_store_fp[ cnt ]);   \
264 }
265
266 /* Call this at the end of main() */
267 #define SILK_DEBUG_STORE_CLOSE_FILES {                              \
268     opus_int i;                                                     \
269     for( i = 0; i < silk_debug_store_count; i++ ) {                 \
270         fclose( silk_debug_store_fp[ i ] );                         \
271     }                                                               \
272 }
273
274 #else /* SILK_DEBUG */
275
276 /* define macros as empty strings */
277 #define DEBUG_STORE_DATA(FILE_NAME, DATA_PTR, N_BYTES)
278 #define SILK_DEBUG_STORE_CLOSE_FILES
279
280 #endif /* SILK_DEBUG */
281
282 #ifdef  __cplusplus
283 }
284 #endif
285
286 #endif /* SILK_DEBUG_H */