• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "typedef.h"
32 #include <stdio.h>      /* file writing */
33 #include <string.h>     /* strcpy, strcmp */
34 
35 #ifdef  __cplusplus
36 extern "C"
37 {
38 #endif
39 
40 unsigned long GetHighResolutionTime(void); /* O  time in usec*/
41 
42 /* Set to 1 to enable DEBUG_STORE_DATA() macros for dumping
43  * intermediate signals from the codec.
44  */
45 #define SILK_DEBUG 0
46 
47 /* Flag for using timers */
48 #define SILK_TIC_TOC    0
49 
50 
51 #if SILK_TIC_TOC
52 
53 #if (defined(_WIN32) || defined(_WINCE))
54 #include <windows.h>    /* timer */
55 #else   /* Linux or Mac*/
56 #include <sys/time.h>
57 #endif
58 
59 /*********************************/
60 /* timer functions for profiling */
61 /*********************************/
62 /* example:                                                         */
63 /*                                                                  */
64 /* TIC(LPC)                                                         */
65 /* do_LPC(in_vec, order, acoef);    // do LPC analysis              */
66 /* TOC(LPC)                                                         */
67 /*                                                                  */
68 /* and call the following just before exiting (from main)           */
69 /*                                                                  */
70 /* silk_TimerSave("silk_TimingData.txt");                           */
71 /*                                                                  */
72 /* results are now in silk_TimingData.txt                           */
73 
74 void silk_TimerSave(char *file_name);
75 
76 /* max number of timers (in different locations) */
77 #define silk_NUM_TIMERS_MAX                  50
78 /* max length of name tags in TIC(..), TOC(..) */
79 #define silk_NUM_TIMERS_MAX_TAG_LEN          30
80 
81 extern int           silk_Timer_nTimers;
82 extern int           silk_Timer_depth_ctr;
83 extern char          silk_Timer_tags[silk_NUM_TIMERS_MAX][silk_NUM_TIMERS_MAX_TAG_LEN];
84 #ifdef _WIN32
85 extern LARGE_INTEGER silk_Timer_start[silk_NUM_TIMERS_MAX];
86 #else
87 extern unsigned long silk_Timer_start[silk_NUM_TIMERS_MAX];
88 #endif
89 extern unsigned int  silk_Timer_cnt[silk_NUM_TIMERS_MAX];
90 extern opus_int64    silk_Timer_sum[silk_NUM_TIMERS_MAX];
91 extern opus_int64    silk_Timer_max[silk_NUM_TIMERS_MAX];
92 extern opus_int64    silk_Timer_min[silk_NUM_TIMERS_MAX];
93 extern opus_int64    silk_Timer_depth[silk_NUM_TIMERS_MAX];
94 
95 /* WARNING: TIC()/TOC can measure only up to 0.1 seconds at a time */
96 #ifdef _WIN32
97 #define TIC(TAG_NAME) {                                     \
98     static int init = 0;                                    \
99     static int ID = -1;                                     \
100     if( init == 0 )                                         \
101     {                                                       \
102         int k;                                              \
103         init = 1;                                           \
104         for( k = 0; k < silk_Timer_nTimers; k++ ) {         \
105             if( strcmp(silk_Timer_tags[k], #TAG_NAME) == 0 ) { \
106                 ID = k;                                     \
107                 break;                                      \
108             }                                               \
109         }                                                   \
110         if (ID == -1) {                                     \
111             ID = silk_Timer_nTimers;                        \
112             silk_Timer_nTimers++;                           \
113             silk_Timer_depth[ID] = silk_Timer_depth_ctr;    \
114             strcpy(silk_Timer_tags[ID], #TAG_NAME);         \
115             silk_Timer_cnt[ID] = 0;                         \
116             silk_Timer_sum[ID] = 0;                         \
117             silk_Timer_min[ID] = 0xFFFFFFFF;                \
118             silk_Timer_max[ID] = 0;                         \
119         }                                                   \
120     }                                                       \
121     silk_Timer_depth_ctr++;                                 \
122     QueryPerformanceCounter(&silk_Timer_start[ID]);         \
123 }
124 #else
125 #define TIC(TAG_NAME) {                                     \
126     static int init = 0;                                    \
127     static int ID = -1;                                     \
128     if( init == 0 )                                         \
129     {                                                       \
130         int k;                                              \
131         init = 1;                                           \
132         for( k = 0; k < silk_Timer_nTimers; k++ ) {         \
133         if( strcmp(silk_Timer_tags[k], #TAG_NAME) == 0 ) {  \
134                 ID = k;                                     \
135                 break;                                      \
136             }                                               \
137         }                                                   \
138         if (ID == -1) {                                     \
139             ID = silk_Timer_nTimers;                        \
140             silk_Timer_nTimers++;                           \
141             silk_Timer_depth[ID] = silk_Timer_depth_ctr;    \
142             strcpy(silk_Timer_tags[ID], #TAG_NAME);         \
143             silk_Timer_cnt[ID] = 0;                         \
144             silk_Timer_sum[ID] = 0;                         \
145             silk_Timer_min[ID] = 0xFFFFFFFF;                \
146             silk_Timer_max[ID] = 0;                         \
147         }                                                   \
148     }                                                       \
149     silk_Timer_depth_ctr++;                                 \
150     silk_Timer_start[ID] = GetHighResolutionTime();         \
151 }
152 #endif
153 
154 #ifdef _WIN32
155 #define TOC(TAG_NAME) {                                             \
156     LARGE_INTEGER lpPerformanceCount;                               \
157     static int init = 0;                                            \
158     static int ID = 0;                                              \
159     if( init == 0 )                                                 \
160     {                                                               \
161         int k;                                                      \
162         init = 1;                                                   \
163         for( k = 0; k < silk_Timer_nTimers; k++ ) {                 \
164             if( strcmp(silk_Timer_tags[k], #TAG_NAME) == 0 ) {      \
165                 ID = k;                                             \
166                 break;                                              \
167             }                                                       \
168         }                                                           \
169     }                                                               \
170     QueryPerformanceCounter(&lpPerformanceCount);                   \
171     lpPerformanceCount.QuadPart -= silk_Timer_start[ID].QuadPart;   \
172     if((lpPerformanceCount.QuadPart < 100000000) &&                 \
173         (lpPerformanceCount.QuadPart >= 0)) {                       \
174         silk_Timer_cnt[ID]++;                                       \
175         silk_Timer_sum[ID] += lpPerformanceCount.QuadPart;          \
176         if( lpPerformanceCount.QuadPart > silk_Timer_max[ID] )      \
177             silk_Timer_max[ID] = lpPerformanceCount.QuadPart;       \
178         if( lpPerformanceCount.QuadPart < silk_Timer_min[ID] )      \
179             silk_Timer_min[ID] = lpPerformanceCount.QuadPart;       \
180     }                                                               \
181     silk_Timer_depth_ctr--;                                         \
182 }
183 #else
184 #define TOC(TAG_NAME) {                                             \
185     unsigned long endTime;                                          \
186     static int init = 0;                                            \
187     static int ID = 0;                                              \
188     if( init == 0 )                                                 \
189     {                                                               \
190         int k;                                                      \
191         init = 1;                                                   \
192         for( k = 0; k < silk_Timer_nTimers; k++ ) {                 \
193             if( strcmp(silk_Timer_tags[k], #TAG_NAME) == 0 ) {      \
194                 ID = k;                                             \
195                 break;                                              \
196             }                                                       \
197         }                                                           \
198     }                                                               \
199     endTime = GetHighResolutionTime();                              \
200     endTime -= silk_Timer_start[ID];                                \
201     if((endTime < 100000000) &&                                     \
202         (endTime >= 0)) {                                           \
203         silk_Timer_cnt[ID]++;                                       \
204         silk_Timer_sum[ID] += endTime;                              \
205         if( endTime > silk_Timer_max[ID] )                          \
206             silk_Timer_max[ID] = endTime;                           \
207         if( endTime < silk_Timer_min[ID] )                          \
208             silk_Timer_min[ID] = endTime;                           \
209     }                                                               \
210         silk_Timer_depth_ctr--;                                     \
211 }
212 #endif
213 
214 #else /* SILK_TIC_TOC */
215 
216 /* define macros as empty strings */
217 #define TIC(TAG_NAME)
218 #define TOC(TAG_NAME)
219 #define silk_TimerSave(FILE_NAME)
220 
221 #endif /* SILK_TIC_TOC */
222 
223 
224 #if SILK_DEBUG
225 /************************************/
226 /* write data to file for debugging */
227 /************************************/
228 /* Example: DEBUG_STORE_DATA(testfile.pcm, &RIN[0], 160*sizeof(opus_int16)); */
229 
230 #define silk_NUM_STORES_MAX                                  100
231 extern FILE *silk_debug_store_fp[ silk_NUM_STORES_MAX ];
232 extern int silk_debug_store_count;
233 
234 /* Faster way of storing the data */
235 #define DEBUG_STORE_DATA( FILE_NAME, DATA_PTR, N_BYTES ) {          \
236     static opus_int init = 0, cnt = 0;                              \
237     static FILE **fp;                                               \
238     if (init == 0) {                                                \
239         init = 1;                                                   \
240         cnt = silk_debug_store_count++;                             \
241         silk_debug_store_fp[ cnt ] = fopen(#FILE_NAME, "wb");       \
242     }                                                               \
243     fwrite((DATA_PTR), (N_BYTES), 1, silk_debug_store_fp[ cnt ]);   \
244 }
245 
246 /* Call this at the end of main() */
247 #define SILK_DEBUG_STORE_CLOSE_FILES {                              \
248     opus_int i;                                                     \
249     for( i = 0; i < silk_debug_store_count; i++ ) {                 \
250         fclose( silk_debug_store_fp[ i ] );                         \
251     }                                                               \
252 }
253 
254 #else /* SILK_DEBUG */
255 
256 /* define macros as empty strings */
257 #define DEBUG_STORE_DATA(FILE_NAME, DATA_PTR, N_BYTES)
258 #define SILK_DEBUG_STORE_CLOSE_FILES
259 
260 #endif /* SILK_DEBUG */
261 
262 #ifdef  __cplusplus
263 }
264 #endif
265 
266 #endif /* SILK_DEBUG_H */
267