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