• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*--------------------------------------------------------------------------
2 Copyright (c) 2013 - 2017, The Linux Foundation. All rights reserved.
3 
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions are met:
6     * Redistributions of source code must retain the above copyright
7       notice, 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 The Linux Foundation nor
12       the names of its contributors may be used to endorse or promote
13       products derived from this software without specific prior written
14       permission.
15 
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
20 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23 OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 --------------------------------------------------------------------------*/
28 
29 #ifndef __VIDC_DEBUG_H__
30 #define __VIDC_DEBUG_H__
31 
32 #ifdef _ANDROID_
33 #include <cstdio>
34 #include <pthread.h>
35 #include <sys/mman.h>
36 
37 enum {
38    PRIO_ERROR=0x1,
39    PRIO_INFO=0x1,
40    PRIO_HIGH=0x2,
41    PRIO_LOW=0x4,
42    PRIO_TRACE_HIGH = 0x10,
43    PRIO_TRACE_LOW = 0x20,
44 };
45 
46 extern int debug_level;
47 
48 #undef DEBUG_PRINT_ERROR
49 #define DEBUG_PRINT_ERROR(fmt, args...) ({ \
50       if (debug_level & PRIO_ERROR) \
51           ALOGE(fmt,##args); \
52       })
53 #undef DEBUG_PRINT_INFO
54 #define DEBUG_PRINT_INFO(fmt, args...) ({ \
55       if (debug_level & PRIO_INFO) \
56           ALOGI(fmt,##args); \
57       })
58 #undef DEBUG_PRINT_LOW
59 #define DEBUG_PRINT_LOW(fmt, args...) ({ \
60       if (debug_level & PRIO_LOW) \
61           ALOGD(fmt,##args); \
62       })
63 #undef DEBUG_PRINT_HIGH
64 #define DEBUG_PRINT_HIGH(fmt, args...) ({ \
65       if (debug_level & PRIO_HIGH) \
66           ALOGD(fmt,##args); \
67       })
68 #else
69 #define DEBUG_PRINT_ERROR printf
70 #define DEBUG_PRINT_INFO printf
71 #define DEBUG_PRINT_LOW printf
72 #define DEBUG_PRINT_HIGH printf
73 #endif
74 
75 #define VALIDATE_OMX_PARAM_DATA(ptr, paramType)                                \
76     {                                                                          \
77         if (ptr == NULL) { return OMX_ErrorBadParameter; }                     \
78         paramType *p = reinterpret_cast<paramType *>(ptr);                     \
79         if (p->nSize < sizeof(paramType)) {                                    \
80             ALOGE("Insufficient object size(%u) v/s expected(%zu) for type %s",\
81                     (unsigned int)p->nSize, sizeof(paramType), #paramType);    \
82             return OMX_ErrorBadParameter;                                      \
83         }                                                                      \
84     }                                                                          \
85 
86 /*
87  * Validate OMX_CONFIG_ANDROID_VENDOR_EXTENSIONTYPE type param
88  * *assumes* VALIDATE_OMX_PARAM_DATA checks have passed
89  * Checks for nParamCount cannot be generalized here. it is imperative that
90  *  the calling code handles it.
91  */
92 #define VALIDATE_OMX_VENDOR_EXTENSION_PARAM_DATA(ext)                                             \
93     {                                                                                             \
94         if (ext->nParamSizeUsed < 1 || ext->nParamSizeUsed > OMX_MAX_ANDROID_VENDOR_PARAMCOUNT) { \
95             ALOGE("VendorExtension: sub-params(%u) not in expected range(%u - %u)",               \
96                     ext->nParamSizeUsed, 1, OMX_MAX_ANDROID_VENDOR_PARAMCOUNT);                   \
97             return OMX_ErrorBadParameter;                                                         \
98         }                                                                                         \
99         OMX_U32 expectedSize = (OMX_U32)sizeof(OMX_CONFIG_ANDROID_VENDOR_EXTENSIONTYPE) +         \
100                 ((ext->nParamSizeUsed - 1) * (OMX_U32)sizeof(OMX_CONFIG_ANDROID_VENDOR_PARAMTYPE));\
101         if (ext->nSize < expectedSize) {                                                          \
102             ALOGE("VendorExtension: Insifficient size(%u) v/s expected(%u)",                      \
103                     ext->nSize, expectedSize);                                                    \
104             return OMX_ErrorBadParameter;                                                         \
105         }                                                                                         \
106     }                                                                                             \
107 
108 class auto_lock {
109     public:
auto_lock(pthread_mutex_t & lock)110         auto_lock(pthread_mutex_t &lock)
111             : mLock(lock) {
112                 pthread_mutex_lock(&mLock);
113             }
~auto_lock()114         ~auto_lock() {
115             pthread_mutex_unlock(&mLock);
116         }
117     private:
118         pthread_mutex_t &mLock;
119 };
120 
121 class AutoUnmap {
122     void *vaddr;
123     int size;
124 
125     public:
AutoUnmap(void * vaddr,int size)126         AutoUnmap(void *vaddr, int size) {
127             this->vaddr = vaddr;
128             this->size = size;
129         }
130 
~AutoUnmap()131         ~AutoUnmap() {
132             if (vaddr)
133                 munmap(vaddr, size);
134         }
135 };
136 
137 class Signal {
138     bool signalled;
139     pthread_mutex_t mutex;
140     pthread_cond_t condition;
141 public:
Signal()142     Signal() {
143         signalled = false;
144         pthread_cond_init(&condition, NULL);
145         pthread_mutex_init(&mutex, NULL);
146     }
147 
~Signal()148     ~Signal() {
149             pthread_cond_destroy(&condition);
150             pthread_mutex_destroy(&mutex);
151     }
152 
signal()153     void signal() {
154         pthread_mutex_lock(&mutex);
155         signalled = true;
156         pthread_cond_signal(&condition);
157         pthread_mutex_unlock(&mutex);
158     }
159 
wait(uint64_t timeout_nsec)160     int wait(uint64_t timeout_nsec) {
161         struct timespec ts;
162 
163         pthread_mutex_lock(&mutex);
164         if (signalled) {
165             signalled = false;
166             pthread_mutex_unlock(&mutex);
167             return 0;
168         }
169         clock_gettime(CLOCK_REALTIME, &ts);
170         ts.tv_sec += timeout_nsec / 1000000000;
171         ts.tv_nsec += timeout_nsec % 1000000000;
172         if (ts.tv_nsec >= 1000000000) {
173             ts.tv_nsec -= 1000000000;
174             ts.tv_sec  += 1;
175         }
176         int ret = pthread_cond_timedwait(&condition, &mutex, &ts);
177         signalled = false;
178         pthread_mutex_unlock(&mutex);
179         return ret;
180     }
181 };
182 
183 #ifdef _ANDROID_
184 #define ATRACE_TAG ATRACE_TAG_VIDEO
185 #include <utils/Trace.h>
186 
187 class AutoTracer {
188     int mPrio;
189 public:
AutoTracer(int prio,const char * msg)190     AutoTracer(int prio, const char* msg)
191         : mPrio(prio) {
192         if (debug_level & prio) {
193             ATRACE_BEGIN(msg);
194         }
195     }
~AutoTracer()196     ~AutoTracer() {
197         if (debug_level & mPrio) {
198             ATRACE_END();
199         }
200     }
201 };
202 
203 struct __attribute__((packed)) IvfFileHeader {
204     uint8_t signature[4];
205     uint16_t version;
206     uint16_t size;
207     uint8_t fourCC[4];
208     uint16_t width;
209     uint16_t height;
210     uint32_t rate;
211     uint32_t scale;
212     uint32_t frameCount;
213     uint32_t unused;
214 
215     IvfFileHeader();
216     IvfFileHeader(bool isVp9, int width, int height,
217                 int rate, int scale, int nFrameCount);
218 };
219 
220 struct __attribute__((packed)) IvfFrameHeader {
221     uint32_t filledLen;
222     uint64_t timeStamp;
223 
224     IvfFrameHeader();
225     IvfFrameHeader(uint32_t size, uint64_t timeStamp);
226 };
227 
228 #define VIDC_TRACE_NAME_LOW(_name) AutoTracer _tracer(PRIO_TRACE_LOW, _name);
229 #define VIDC_TRACE_NAME_HIGH(_name) AutoTracer _tracer(PRIO_TRACE_HIGH, _name);
230 
231 #define VIDC_TRACE_INT_LOW(_name, _int) \
232     if (debug_level & PRIO_TRACE_LOW) { \
233         ATRACE_INT(_name, _int);        \
234     }
235 
236 #define VIDC_TRACE_INT_HIGH(_name, _int) \
237     if (debug_level & PRIO_TRACE_HIGH) { \
238         ATRACE_INT(_name, _int);        \
239     }
240 
241 #else // _ANDROID_
242 
243 #define VIDC_TRACE_NAME_LOW(_name)
244 #define VIDC_TRACE_NAME_HIGH(_name)
245 #define VIDC_TRACE_INT_LOW(_name, _int)
246 #define VIDC_TRACE_INT_HIGH(_name, _int)
247 
248 #endif // !_ANDROID_
249 
250 #endif
251