Fixes a minor bug introduced in 43a0de4af15
[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 #if SILK_TIC_TOC
68
69 #if (defined(_WIN32) || defined(_WINCE))
70 #include <windows.h>    /* timer */
71 #pragma warning( disable : 4996 )       /* stop bitching about strcpy in TIC()*/
72 #else   /* Linux or Mac*/
73 #include <sys/time.h>
74 #endif
75
76 /*********************************/
77 /* timer functions for profiling */
78 /*********************************/
79 /* example:                                                         */
80 /*                                                                  */
81 /* TIC(LPC)                                                         */
82 /* do_LPC(in_vec, order, acoef);    // do LPC analysis              */
83 /* TOC(LPC)                                                         */
84 /*                                                                  */
85 /* and call the following just before exiting (from main)           */
86 /*                                                                  */
87 /* silk_TimerSave("silk_TimingData.txt");                             */
88 /*                                                                  */
89 /* results are now in silk_TimingData.txt                            */
90
91 void silk_TimerSave(char *file_name);
92
93 /* max number of timers (in different locations) */
94 #define silk_NUM_TIMERS_MAX                  50
95 /* max length of name tags in TIC(..), TOC(..) */
96 #define silk_NUM_TIMERS_MAX_TAG_LEN          30
97
98 extern int           silk_Timer_nTimers;
99 extern int           silk_Timer_depth_ctr;
100 extern char          silk_Timer_tags[silk_NUM_TIMERS_MAX][silk_NUM_TIMERS_MAX_TAG_LEN];
101 #ifdef _WIN32
102 extern LARGE_INTEGER silk_Timer_start[silk_NUM_TIMERS_MAX];
103 #else
104 extern unsigned long silk_Timer_start[silk_NUM_TIMERS_MAX];
105 #endif
106 extern unsigned int  silk_Timer_cnt[silk_NUM_TIMERS_MAX];
107 extern opus_int64     silk_Timer_sum[silk_NUM_TIMERS_MAX];
108 extern opus_int64     silk_Timer_max[silk_NUM_TIMERS_MAX];
109 extern opus_int64     silk_Timer_min[silk_NUM_TIMERS_MAX];
110 extern opus_int64     silk_Timer_depth[silk_NUM_TIMERS_MAX];
111
112 /* WARNING: TIC()/TOC can measure only up to 0.1 seconds at a time */
113 #ifdef _WIN32
114 #define TIC(TAG_NAME) {                                     \
115     static int init = 0;                                    \
116     static int ID = -1;                                     \
117     if( init == 0 )                                         \
118     {                                                       \
119         int k;                                              \
120         init = 1;                                           \
121         for( k = 0; k < silk_Timer_nTimers; k++ ) {          \
122             if( strcmp(silk_Timer_tags[k], #TAG_NAME) == 0 ) {   \
123                 ID = k;                                     \
124                 break;                                      \
125             }                                               \
126         }                                                   \
127         if (ID == -1) {                                     \
128             ID = silk_Timer_nTimers;                         \
129             silk_Timer_nTimers++;                            \
130             silk_Timer_depth[ID] = silk_Timer_depth_ctr;      \
131             strcpy(silk_Timer_tags[ID], #TAG_NAME);          \
132             silk_Timer_cnt[ID] = 0;                          \
133             silk_Timer_sum[ID] = 0;                          \
134             silk_Timer_min[ID] = 0xFFFFFFFF;                 \
135             silk_Timer_max[ID] = 0;                          \
136         }                                                   \
137     }                                                       \
138     silk_Timer_depth_ctr++;                                  \
139     QueryPerformanceCounter(&silk_Timer_start[ID]);          \
140 }
141 #else
142 #define TIC(TAG_NAME) {                                     \
143     static int init = 0;                                    \
144     static int ID = -1;                                     \
145     if( init == 0 )                                         \
146     {                                                       \
147         int k;                                              \
148         init = 1;                                           \
149         for( k = 0; k < silk_Timer_nTimers; k++ ) {          \
150         if( strcmp(silk_Timer_tags[k], #TAG_NAME) == 0 ) {   \
151                 ID = k;                                     \
152                 break;                                      \
153             }                                               \
154         }                                                   \
155         if (ID == -1) {                                     \
156             ID = silk_Timer_nTimers;                         \
157             silk_Timer_nTimers++;                            \
158             silk_Timer_depth[ID] = silk_Timer_depth_ctr;      \
159             strcpy(silk_Timer_tags[ID], #TAG_NAME);          \
160             silk_Timer_cnt[ID] = 0;                          \
161             silk_Timer_sum[ID] = 0;                          \
162             silk_Timer_min[ID] = 0xFFFFFFFF;                 \
163             silk_Timer_max[ID] = 0;                          \
164         }                                                   \
165     }                                                       \
166     silk_Timer_depth_ctr++;                                  \
167     silk_Timer_start[ID] = GetHighResolutionTime();          \
168 }
169 #endif
170
171 #ifdef _WIN32
172 #define TOC(TAG_NAME) {                                             \
173     LARGE_INTEGER lpPerformanceCount;                               \
174     static int init = 0;                                            \
175     static int ID = 0;                                              \
176     if( init == 0 )                                                 \
177     {                                                               \
178         int k;                                                      \
179         init = 1;                                                   \
180         for( k = 0; k < silk_Timer_nTimers; k++ ) {                  \
181             if( strcmp(silk_Timer_tags[k], #TAG_NAME) == 0 ) {       \
182                 ID = k;                                             \
183                 break;                                              \
184             }                                                       \
185         }                                                           \
186     }                                                               \
187     QueryPerformanceCounter(&lpPerformanceCount);                   \
188     lpPerformanceCount.QuadPart -= silk_Timer_start[ID].QuadPart;    \
189     if((lpPerformanceCount.QuadPart < 100000000) &&                 \
190         (lpPerformanceCount.QuadPart >= 0)) {                       \
191         silk_Timer_cnt[ID]++;                                        \
192         silk_Timer_sum[ID] += lpPerformanceCount.QuadPart;           \
193         if( lpPerformanceCount.QuadPart > silk_Timer_max[ID] )       \
194             silk_Timer_max[ID] = lpPerformanceCount.QuadPart;        \
195         if( lpPerformanceCount.QuadPart < silk_Timer_min[ID] )       \
196             silk_Timer_min[ID] = lpPerformanceCount.QuadPart;        \
197     }                                                               \
198     silk_Timer_depth_ctr--;                                          \
199 }
200 #else
201 #define TOC(TAG_NAME) {                                             \
202     unsigned long endTime;                                          \
203     static int init = 0;                                            \
204     static int ID = 0;                                              \
205     if( init == 0 )                                                 \
206     {                                                               \
207         int k;                                                      \
208         init = 1;                                                   \
209         for( k = 0; k < silk_Timer_nTimers; k++ ) {                  \
210             if( strcmp(silk_Timer_tags[k], #TAG_NAME) == 0 ) {       \
211                 ID = k;                                             \
212                 break;                                              \
213             }                                                       \
214         }                                                           \
215     }                                                               \
216     endTime = GetHighResolutionTime();                              \
217     endTime -= silk_Timer_start[ID];                                 \
218     if((endTime < 100000000) &&                                     \
219         (endTime >= 0)) {                                           \
220         silk_Timer_cnt[ID]++;                                        \
221         silk_Timer_sum[ID] += endTime;                               \
222         if( endTime > silk_Timer_max[ID] )                           \
223             silk_Timer_max[ID] = endTime;                            \
224         if( endTime < silk_Timer_min[ID] )                           \
225             silk_Timer_min[ID] = endTime;                            \
226     }                                                               \
227         silk_Timer_depth_ctr--;                                      \
228 }
229 #endif
230
231 #else /* SILK_TIC_TOC */
232
233 /* define macros as empty strings */
234 #define TIC(TAG_NAME)
235 #define TOC(TAG_NAME)
236 #define silk_TimerSave(FILE_NAME)
237
238 #endif /* SILK_TIC_TOC */
239
240
241
242 #if SILK_DEBUG
243 /************************************/
244 /* write data to file for debugging */
245 /************************************/
246 /* opens an empty file if this file has not yet been open, then writes to the file and closes it            */
247 /* if file has been open previously it is opened again and the fwrite is appending, finally it is closed    */
248 #define SAVE_DATA( FILE_NAME, DATA_PTR, N_BYTES ) {                 \
249     static opus_int32 init = 0;                                      \
250     FILE *fp;                                                       \
251     if (init == 0)    {                                               \
252         init = 1;                                                   \
253         fp = fopen(#FILE_NAME, "wb");                               \
254     } else {                                                        \
255         fp = fopen(#FILE_NAME, "ab+");                              \
256     }                                                                \
257     fwrite((DATA_PTR), (N_BYTES), 1, fp);                           \
258     fclose(fp);                                                     \
259 }
260
261 /* Example: DEBUG_STORE_DATA(testfile.pcm, &RIN[0], 160*sizeof(opus_int16)); */
262
263 #if 0
264 /* Ensure that everything is written to files when an assert breaks */
265 #define DEBUG_STORE_DATA(FILE_NAME, DATA_PTR, N_BYTES) SAVE_DATA(FILE_NAME, DATA_PTR, N_BYTES)
266 #define SILK_DEBUG_STORE_CLOSE_FILES
267
268 #else
269
270 #define silk_NUM_STORES_MAX                                  100
271 extern FILE *silk_debug_store_fp[ silk_NUM_STORES_MAX ];
272 extern int silk_debug_store_count;
273
274 /* Faster way of storing the data */
275 #define DEBUG_STORE_DATA( FILE_NAME, DATA_PTR, N_BYTES ) {          \
276     static opus_int init = 0, cnt = 0;                               \
277     static FILE **fp;                                               \
278     if (init == 0) {                                                \
279         init = 1;                                                    \
280         cnt = silk_debug_store_count++;                             \
281         silk_debug_store_fp[ cnt ] = fopen(#FILE_NAME, "wb");       \
282     }                                                               \
283     fwrite((DATA_PTR), (N_BYTES), 1, silk_debug_store_fp[ cnt ]);   \
284 }
285
286 /* Call this at the end of main() */
287 #define SILK_DEBUG_STORE_CLOSE_FILES {                              \
288     opus_int i;                                                      \
289     for( i = 0; i < silk_debug_store_count; i++ ) {                 \
290         fclose( silk_debug_store_fp[ i ] );                         \
291     }                                                               \
292 }
293 #endif
294
295 /* micro sec */
296 #define silk_GETTIME(void)       time = (opus_int64) silk_GetHighResolutionTime();
297
298 #else /* SILK_DEBUG */
299
300 /* define macros as empty strings */
301 #define DEBUG_STORE_DATA(FILE_NAME, DATA_PTR, N_BYTES)
302 #define SAVE_DATA(FILE_NAME, DATA_PTR, N_BYTES)
303 #define SILK_DEBUG_STORE_CLOSE_FILES
304
305 #endif /* SILK_DEBUG */
306
307 #ifdef  __cplusplus
308 }
309 #endif
310
311 #endif /* _SILK_DEBUG_H_ */