Update SILK code using the CELT range coder
[opus.git] / interface / SKP_debug.h
1 /***********************************************************************\r
2 Copyright (c) 2006-2010, 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 \r
29 /*                                                                      *\r
30  * SKP_debug.h                                                          *\r
31  *                                                                      *\r
32  * This contains code to help debugging                                 *\r
33  *                                                                      *\r
34  * Copyright 2009 (c), Skype Limited                                    *\r
35  * Date: 090629                                                         *\r
36  *                                                                      */\r
37 #ifndef _SKP_DEBUG_H_\r
38 #define _SKP_DEBUG_H_\r
39 \r
40 #ifdef _WIN32\r
41 #define _CRT_SECURE_NO_DEPRECATE    1\r
42 #endif\r
43 \r
44 #include "SKP_Silk_typedef.h"\r
45 #include <stdio.h>      /* file writing */\r
46 #include <string.h>     /* strcpy, strcmp */\r
47 \r
48 #ifdef  __cplusplus\r
49 extern "C"\r
50 {\r
51 #endif\r
52 \r
53 unsigned long GetHighResolutionTime(); /* O: time in usec*/\r
54 \r
55 /* make SKP_DEBUG dependent on compiler's _DEBUG */\r
56 #if defined _WIN32\r
57     #ifdef _DEBUG\r
58         #define SKP_DEBUG  1\r
59     #else\r
60         #define SKP_DEBUG  0\r
61     #endif\r
62 \r
63     /* overrule the above */\r
64     #if 0\r
65     //  #define NO_ASSERTS\r
66     #undef  SKP_DEBUG  \r
67     #define SKP_DEBUG  1\r
68     #endif\r
69 #else\r
70     #define SKP_DEBUG  0\r
71 #endif\r
72 \r
73 /* Flag for using timers */\r
74 #define SKP_TIC_TOC 1\r
75 \r
76 #if SKP_TIC_TOC\r
77 \r
78 #if (defined(_WIN32) || defined(_WINCE)) \r
79 #include <windows.h>    /* timer */\r
80 #pragma warning( disable : 4996 )       // stop bitching about strcpy in TIC()\r
81 #else   // Linux or Mac\r
82 #include <sys/time.h>\r
83 #endif\r
84 \r
85 /*********************************/\r
86 /* timer functions for profiling */\r
87 /*********************************/\r
88 /* example:                                                         */\r
89 /*                                                                  */\r
90 /* TIC(LPC)                                                         */\r
91 /* do_LPC(in_vec, order, acoef);    // do LPC analysis              */\r
92 /* TOC(LPC)                                                         */\r
93 /*                                                                  */\r
94 /* and call the following just before exiting (from main)           */\r
95 /*                                                                  */\r
96 /* SKP_TimerSave("SKP_TimingData.txt");                             */\r
97 /*                                                                  */\r
98 /* results are now in SKP_TimingData.txt                            */\r
99 \r
100 void SKP_TimerSave(char *file_name);\r
101 \r
102 /* max number of timers (in different locations) */\r
103 #define SKP_NUM_TIMERS_MAX                  50\r
104 /* max length of name tags in TIC(..), TOC(..) */\r
105 #define SKP_NUM_TIMERS_MAX_TAG_LEN          30\r
106 \r
107 extern int           SKP_Timer_nTimers;\r
108 extern int           SKP_Timer_depth_ctr;\r
109 extern char          SKP_Timer_tags[SKP_NUM_TIMERS_MAX][SKP_NUM_TIMERS_MAX_TAG_LEN];\r
110 #ifdef _WIN32\r
111 extern LARGE_INTEGER SKP_Timer_start[SKP_NUM_TIMERS_MAX];\r
112 #else\r
113 extern unsigned long SKP_Timer_start[SKP_NUM_TIMERS_MAX];\r
114 #endif  \r
115 extern unsigned int  SKP_Timer_cnt[SKP_NUM_TIMERS_MAX];\r
116 extern SKP_int64     SKP_Timer_sum[SKP_NUM_TIMERS_MAX];\r
117 extern SKP_int64     SKP_Timer_max[SKP_NUM_TIMERS_MAX];\r
118 extern SKP_int64     SKP_Timer_min[SKP_NUM_TIMERS_MAX];\r
119 extern SKP_int64     SKP_Timer_depth[SKP_NUM_TIMERS_MAX];\r
120 \r
121 /* WARNING: TIC()/TOC can measure only up to 0.1 seconds at a time */\r
122 #ifdef _WIN32\r
123 #define TIC(TAG_NAME) {                                     \\r
124     static int init = 0;                                    \\r
125     static int ID = -1;                                     \\r
126     if( init == 0 )                                         \\r
127     {                                                       \\r
128         int k;                                              \\r
129         init = 1;                                           \\r
130         for( k = 0; k < SKP_Timer_nTimers; k++ ) {          \\r
131             if( strcmp(SKP_Timer_tags[k], #TAG_NAME) == 0 ) {   \\r
132                 ID = k;                                     \\r
133                 break;                                      \\r
134             }                                               \\r
135         }                                                   \\r
136         if (ID == -1) {                                     \\r
137             ID = SKP_Timer_nTimers;                         \\r
138             SKP_Timer_nTimers++;                            \\r
139             SKP_Timer_depth[ID] = SKP_Timer_depth_ctr;      \\r
140             strcpy(SKP_Timer_tags[ID], #TAG_NAME);          \\r
141             SKP_Timer_cnt[ID] = 0;                          \\r
142             SKP_Timer_sum[ID] = 0;                          \\r
143             SKP_Timer_min[ID] = 0xFFFFFFFF;                 \\r
144             SKP_Timer_max[ID] = 0;                          \\r
145         }                                                   \\r
146     }                                                       \\r
147     SKP_Timer_depth_ctr++;                                  \\r
148     QueryPerformanceCounter(&SKP_Timer_start[ID]);          \\r
149 }\r
150 #else\r
151 #define TIC(TAG_NAME) {                                     \\r
152     static int init = 0;                                    \\r
153     static int ID = -1;                                     \\r
154     if( init == 0 )                                         \\r
155     {                                                       \\r
156         int k;                                              \\r
157         init = 1;                                           \\r
158         for( k = 0; k < SKP_Timer_nTimers; k++ ) {          \\r
159         if( strcmp(SKP_Timer_tags[k], #TAG_NAME) == 0 ) {   \\r
160                 ID = k;                                     \\r
161                 break;                                      \\r
162             }                                               \\r
163         }                                                   \\r
164         if (ID == -1) {                                     \\r
165             ID = SKP_Timer_nTimers;                         \\r
166             SKP_Timer_nTimers++;                            \\r
167             SKP_Timer_depth[ID] = SKP_Timer_depth_ctr;      \\r
168             strcpy(SKP_Timer_tags[ID], #TAG_NAME);          \\r
169             SKP_Timer_cnt[ID] = 0;                          \\r
170             SKP_Timer_sum[ID] = 0;                          \\r
171             SKP_Timer_min[ID] = 0xFFFFFFFF;                 \\r
172             SKP_Timer_max[ID] = 0;                          \\r
173         }                                                   \\r
174     }                                                       \\r
175     SKP_Timer_depth_ctr++;                                  \\r
176     SKP_Timer_start[ID] = GetHighResolutionTime();          \\r
177 }\r
178 #endif\r
179 \r
180 #ifdef _WIN32\r
181 #define TOC(TAG_NAME) {                                             \\r
182     LARGE_INTEGER lpPerformanceCount;                               \\r
183     static int init = 0;                                            \\r
184     static int ID = 0;                                              \\r
185     if( init == 0 )                                                 \\r
186     {                                                               \\r
187         int k;                                                      \\r
188         init = 1;                                                   \\r
189         for( k = 0; k < SKP_Timer_nTimers; k++ ) {                  \\r
190             if( strcmp(SKP_Timer_tags[k], #TAG_NAME) == 0 ) {       \\r
191                 ID = k;                                             \\r
192                 break;                                              \\r
193             }                                                       \\r
194         }                                                           \\r
195     }                                                               \\r
196     QueryPerformanceCounter(&lpPerformanceCount);                   \\r
197     lpPerformanceCount.QuadPart -= SKP_Timer_start[ID].QuadPart;    \\r
198     if((lpPerformanceCount.QuadPart < 100000000) &&                 \\r
199         (lpPerformanceCount.QuadPart >= 0)) {                       \\r
200         SKP_Timer_cnt[ID]++;                                        \\r
201         SKP_Timer_sum[ID] += lpPerformanceCount.QuadPart;           \\r
202         if( lpPerformanceCount.QuadPart > SKP_Timer_max[ID] )       \\r
203             SKP_Timer_max[ID] = lpPerformanceCount.QuadPart;        \\r
204         if( lpPerformanceCount.QuadPart < SKP_Timer_min[ID] )       \\r
205             SKP_Timer_min[ID] = lpPerformanceCount.QuadPart;        \\r
206     }                                                               \\r
207     SKP_Timer_depth_ctr--;                                          \\r
208 }\r
209 #else\r
210 #define TOC(TAG_NAME) {                                             \\r
211     unsigned long endTime;                                          \\r
212     static int init = 0;                                            \\r
213     static int ID = 0;                                              \\r
214     if( init == 0 )                                                 \\r
215     {                                                               \\r
216         int k;                                                      \\r
217         init = 1;                                                   \\r
218         for( k = 0; k < SKP_Timer_nTimers; k++ ) {                  \\r
219             if( strcmp(SKP_Timer_tags[k], #TAG_NAME) == 0 ) {       \\r
220                 ID = k;                                             \\r
221                 break;                                              \\r
222             }                                                       \\r
223         }                                                           \\r
224     }                                                               \\r
225     endTime = GetHighResolutionTime();                              \\r
226     endTime -= SKP_Timer_start[ID];                                 \\r
227     if((endTime < 100000000) &&                                     \\r
228         (endTime >= 0)) {                                           \\r
229         SKP_Timer_cnt[ID]++;                                        \\r
230         SKP_Timer_sum[ID] += endTime;                               \\r
231         if( endTime > SKP_Timer_max[ID] )                           \\r
232             SKP_Timer_max[ID] = endTime;                            \\r
233         if( endTime < SKP_Timer_min[ID] )                           \\r
234             SKP_Timer_min[ID] = endTime;                            \\r
235     }                                                               \\r
236         SKP_Timer_depth_ctr--;                                      \\r
237 }\r
238 #endif\r
239 \r
240 #else /* SKP_TIC_TOC */\r
241 \r
242 /* define macros as empty strings */\r
243 #define TIC(TAG_NAME)\r
244 #define TOC(TAG_NAME)\r
245 #define SKP_TimerSave(FILE_NAME)\r
246 \r
247 #endif /* SKP_TIC_TOC */\r
248 \r
249 \r
250 \r
251 #if SKP_DEBUG\r
252 /************************************/\r
253 /* write data to file for debugging */\r
254 /************************************/\r
255 /* opens an empty file if this file has not yet been open, then writes to the file and closes it            */\r
256 /* if file has been open previously it is opened again and the fwrite is appending, finally it is closed    */\r
257 #define SAVE_DATA(FILE_NAME, DATA_PTR, N_BYTES) { static SKP_int32 init = 0;FILE *fp;if (init == 0) {init = 1;fp = fopen(#FILE_NAME, "wb");}else {fp = fopen(#FILE_NAME, "ab+");}   fwrite((DATA_PTR), (N_BYTES), 1, fp);fclose(fp);}   \r
258 \r
259 /* Example: DEBUG_STORE_DATA(testfile.pcm, &RIN[0], 160*sizeof(SKP_int16)); */\r
260 \r
261 #if 0\r
262 /* Ensure that everything is written to files when an assert breaks */\r
263 #define DEBUG_STORE_DATA(FILE_NAME, DATA_PTR, N_BYTES) SAVE_DATA(FILE_NAME, DATA_PTR, N_BYTES)\r
264 #else\r
265 /* Faster way of storing the data */\r
266 #define DEBUG_STORE_DATA(FILE_NAME, DATA_PTR, N_BYTES) {    \\r
267     static SKP_int32 init = 0;                              \\r
268     static FILE *fp;                                        \\r
269     if (init == 0)                                          \\r
270     {                                                       \\r
271         init = 1;                                           \\r
272         fp = fopen(#FILE_NAME, "wb");                       \\r
273     }                                                       \\r
274     fwrite((DATA_PTR), (N_BYTES), 1, fp);                   \\r
275 }\r
276 #endif\r
277 \r
278 /* micro sec */\r
279 #define SKP_GETTIME(void)       time = (SKP_int64) GetHighResolutionTime();     \r
280 \r
281 #else /* SKP_DEBUG */\r
282 \r
283 /* define macros as empty strings */\r
284 #define DEBUG_STORE_DATA(FILE_NAME, DATA_PTR, N_BYTES)\r
285 #define SAVE_DATA(FILE_NAME, DATA_PTR, N_BYTES)\r
286 \r
287 #endif /* SKP_DEBUG */\r
288 \r
289 #ifdef  __cplusplus\r
290 }\r
291 #endif\r
292 \r
293 #endif /* _SKP_DEBUG_H_ */\r