1 /*-------------------------------------------------------------------------- 2 Copyright (c) 2013 - 2019, 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 34 #include <cstdio> 35 #include <string.h> 36 #include <pthread.h> 37 #include <sys/mman.h> 38 extern "C" { 39 #include <utils/Log.h> 40 } 41 42 #include <linux/videodev2.h> 43 #include "OMX_Core.h" 44 #include "vidc_common.h" 45 #include <color_metadata.h> 46 #define STRINGIFY_ENUMS 47 #include "media/hardware/VideoAPI.h" 48 #include "media/msm_vidc_utils.h" 49 50 using android::ColorAspects; 51 using android::HDRStaticInfo; 52 53 enum { 54 PRIO_ERROR=0x1, 55 PRIO_INFO=0x1, 56 PRIO_HIGH=0x2, 57 PRIO_LOW=0x4, 58 PRIO_TRACE_HIGH = 0x10, 59 PRIO_TRACE_LOW = 0x20, 60 }; 61 62 extern int debug_level; 63 64 #undef DEBUG_PRINT_ERROR 65 #define DEBUG_PRINT_ERROR(fmt, args...) ({ \ 66 if (debug_level & PRIO_ERROR) \ 67 ALOGE(fmt,##args); \ 68 }) 69 #undef DEBUG_PRINT_INFO 70 #define DEBUG_PRINT_INFO(fmt, args...) ({ \ 71 if (debug_level & PRIO_INFO) \ 72 ALOGI(fmt,##args); \ 73 }) 74 #undef DEBUG_PRINT_LOW 75 #define DEBUG_PRINT_LOW(fmt, args...) ({ \ 76 if (debug_level & PRIO_LOW) \ 77 ALOGD(fmt,##args); \ 78 }) 79 #undef DEBUG_PRINT_HIGH 80 #define DEBUG_PRINT_HIGH(fmt, args...) ({ \ 81 if (debug_level & PRIO_HIGH) \ 82 ALOGD(fmt,##args); \ 83 }) 84 #else 85 #define DEBUG_PRINT_ERROR printf 86 #define DEBUG_PRINT_INFO printf 87 #define DEBUG_PRINT_LOW printf 88 #define DEBUG_PRINT_HIGH printf 89 #endif 90 91 struct debug_cap { 92 bool in_buffer_log; 93 bool out_buffer_log; 94 bool out_cc_buffer_log; 95 bool out_meta_buffer_log; 96 char infile_name[PROPERTY_VALUE_MAX + 36]; 97 char outfile_name[PROPERTY_VALUE_MAX + 36]; 98 char ccoutfile_name[PROPERTY_VALUE_MAX + 36]; 99 char out_ymetafile_name[PROPERTY_VALUE_MAX + 36]; 100 char out_uvmetafile_name[PROPERTY_VALUE_MAX + 36]; 101 char log_loc[PROPERTY_VALUE_MAX]; 102 FILE *infile; 103 FILE *outfile; 104 FILE *ccoutfile; 105 FILE *out_ymeta_file; 106 FILE *out_uvmeta_file; 107 int64_t session_id; 108 int seq_count; 109 }; 110 111 #define VALIDATE_OMX_PARAM_DATA(ptr, paramType) \ 112 { \ 113 if (ptr == NULL) { return OMX_ErrorBadParameter; } \ 114 paramType *p = reinterpret_cast<paramType *>(ptr); \ 115 if (p->nSize < sizeof(paramType)) { \ 116 ALOGE("Insufficient object size(%u) v/s expected(%zu) for type %s",\ 117 (unsigned int)p->nSize, sizeof(paramType), #paramType); \ 118 return OMX_ErrorBadParameter; \ 119 } \ 120 } \ 121 122 /* 123 * Validate OMX_CONFIG_ANDROID_VENDOR_EXTENSIONTYPE type param 124 * *assumes* VALIDATE_OMX_PARAM_DATA checks have passed 125 * Checks for nParamCount cannot be generalized here. it is imperative that 126 * the calling code handles it. 127 */ 128 #define VALIDATE_OMX_VENDOR_EXTENSION_PARAM_DATA(ext) \ 129 { \ 130 if (ext->nParamSizeUsed < 1 || ext->nParamSizeUsed > OMX_MAX_ANDROID_VENDOR_PARAMCOUNT) { \ 131 ALOGE("VendorExtension: sub-params(%u) not in expected range(%u - %u)", \ 132 ext->nParamSizeUsed, 1, OMX_MAX_ANDROID_VENDOR_PARAMCOUNT); \ 133 return OMX_ErrorBadParameter; \ 134 } \ 135 OMX_U32 expectedSize = (OMX_U32)sizeof(OMX_CONFIG_ANDROID_VENDOR_EXTENSIONTYPE) + \ 136 ((ext->nParamSizeUsed - 1) * (OMX_U32)sizeof(OMX_CONFIG_ANDROID_VENDOR_PARAMTYPE));\ 137 if (ext->nSize < expectedSize) { \ 138 ALOGE("VendorExtension: Insifficient size(%u) v/s expected(%u)", \ 139 ext->nSize, expectedSize); \ 140 return OMX_ErrorBadParameter; \ 141 } \ 142 } \ 143 144 void print_omx_buffer(const char *str, OMX_BUFFERHEADERTYPE *pHeader); 145 void print_v4l2_buffer(const char *str, struct v4l2_buffer *v4l2); 146 void print_debug_color_aspects(ColorAspects *a, const char *prefix); 147 void print_debug_hdr_color_info(HDRStaticInfo *hdr_info, const char *prefix); 148 void print_debug_hdr_color_info_mdata(ColorMetaData* color_mdata); 149 void print_debug_hdr10plus_metadata(ColorMetaData& color_mdata); 150 151 class auto_lock { 152 public: auto_lock(pthread_mutex_t & lock)153 auto_lock(pthread_mutex_t &lock) 154 : mLock(lock) { 155 pthread_mutex_lock(&mLock); 156 } ~auto_lock()157 ~auto_lock() { 158 pthread_mutex_unlock(&mLock); 159 } 160 private: 161 pthread_mutex_t &mLock; 162 }; 163 164 class Signal { 165 bool signalled; 166 pthread_mutex_t mutex; 167 pthread_cond_t condition; 168 public: Signal()169 Signal() { 170 signalled = false; 171 pthread_cond_init(&condition, NULL); 172 pthread_mutex_init(&mutex, NULL); 173 } 174 ~Signal()175 ~Signal() { 176 pthread_cond_destroy(&condition); 177 pthread_mutex_destroy(&mutex); 178 } 179 signal()180 void signal() { 181 pthread_mutex_lock(&mutex); 182 signalled = true; 183 pthread_cond_signal(&condition); 184 pthread_mutex_unlock(&mutex); 185 } 186 wait(uint64_t timeout_nsec)187 int wait(uint64_t timeout_nsec) { 188 struct timespec ts; 189 190 pthread_mutex_lock(&mutex); 191 if (signalled) { 192 signalled = false; 193 pthread_mutex_unlock(&mutex); 194 return 0; 195 } 196 clock_gettime(CLOCK_REALTIME, &ts); 197 ts.tv_sec += timeout_nsec / 1000000000; 198 ts.tv_nsec += timeout_nsec % 1000000000; 199 if (ts.tv_nsec >= 1000000000) { 200 ts.tv_nsec -= 1000000000; 201 ts.tv_sec += 1; 202 } 203 int ret = pthread_cond_timedwait(&condition, &mutex, &ts); 204 //as the mutex lock is released inside timedwait first 205 //the singalled variant maybe changed by the main thread in some rare cases 206 //meanwhile still returns wait time out 207 //need to double check it and return 0 to process the last cmd/event during time out 208 if (signalled) 209 ret = 0; 210 signalled = false; 211 pthread_mutex_unlock(&mutex); 212 return ret; 213 } 214 }; 215 216 #ifdef _ANDROID_ 217 #define ATRACE_TAG ATRACE_TAG_VIDEO 218 #include <cutils/trace.h> 219 #include <utils/Trace.h> 220 221 class AutoTracer { 222 int mPrio; 223 public: AutoTracer(int prio,const char * msg)224 AutoTracer(int prio, const char* msg) 225 : mPrio(prio) { 226 if (debug_level & prio) { 227 ATRACE_BEGIN(msg); 228 } 229 } ~AutoTracer()230 ~AutoTracer() { 231 if (debug_level & mPrio) { 232 ATRACE_END(); 233 } 234 } 235 }; 236 237 struct __attribute__((packed)) IvfFileHeader { 238 uint8_t signature[4]; 239 uint16_t version; 240 uint16_t size; 241 uint8_t fourCC[4]; 242 uint16_t width; 243 uint16_t height; 244 uint32_t rate; 245 uint32_t scale; 246 uint32_t frameCount; 247 uint32_t unused; 248 249 IvfFileHeader(); 250 IvfFileHeader(bool isVp9, int width, int height, 251 int rate, int scale, int nFrameCount); 252 }; 253 254 struct __attribute__((packed)) IvfFrameHeader { 255 uint32_t filledLen; 256 uint64_t timeStamp; 257 258 IvfFrameHeader(); 259 IvfFrameHeader(uint32_t size, uint64_t timeStamp); 260 }; 261 262 #define VIDC_TRACE_NAME_LOW(_name) AutoTracer _tracer(PRIO_TRACE_LOW, _name); 263 #define VIDC_TRACE_NAME_HIGH(_name) AutoTracer _tracer(PRIO_TRACE_HIGH, _name); 264 265 #define VIDC_TRACE_INT_LOW(_name, _int) \ 266 if (debug_level & PRIO_TRACE_LOW) { \ 267 ATRACE_INT(_name, _int); \ 268 } 269 270 #define VIDC_TRACE_INT_HIGH(_name, _int) \ 271 if (debug_level & PRIO_TRACE_HIGH) { \ 272 ATRACE_INT(_name, _int); \ 273 } 274 275 #else // _ANDROID_ 276 277 #define VIDC_TRACE_NAME_LOW(_name) 278 #define VIDC_TRACE_NAME_HIGH(_name) 279 #define VIDC_TRACE_INT_LOW(_name, _int) 280 #define VIDC_TRACE_INT_HIGH(_name, _int) 281 282 #endif // !_ANDROID_ 283 284 #endif 285