1 /* 2 * Copyright (C) Texas Instruments - http://www.ti.com/ 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #include "CameraHal.h" 18 19 namespace Ti { 20 namespace Camera { 21 22 const char CameraHal::PARAMS_DELIMITER []= ","; 23 24 #if PPM_INSTRUMENTATION || PPM_INSTRUMENTATION_ABS 25 26 struct timeval CameraHal::ppm_start; 27 28 #endif 29 30 #if PPM_INSTRUMENTATION 31 32 /** 33 @brief PPM instrumentation 34 35 Dumps the current time offset. The time reference point 36 lies within the CameraHAL constructor. 37 38 @param str - log message 39 @return none 40 41 */ 42 void CameraHal::PPM(const char* str){ 43 struct timeval ppm; 44 45 gettimeofday(&ppm, NULL); 46 ppm.tv_sec = ppm.tv_sec - ppm_start.tv_sec; 47 ppm.tv_sec = ppm.tv_sec * 1000000; 48 ppm.tv_sec = ppm.tv_sec + ppm.tv_usec - ppm_start.tv_usec; 49 50 CAMHAL_LOGI("PPM: %s :%ld.%ld ms", str, ( ppm.tv_sec /1000 ), ( ppm.tv_sec % 1000 )); 51 } 52 53 #elif PPM_INSTRUMENTATION_ABS 54 55 /** 56 @brief PPM instrumentation 57 58 Dumps the current time offset. The time reference point 59 lies within the CameraHAL constructor. This implemetation 60 will also dump the abosolute timestamp, which is useful when 61 post calculation is done with data coming from the upper 62 layers (Camera application etc.) 63 64 @param str - log message 65 @return none 66 67 */ 68 void CameraHal::PPM(const char* str){ 69 struct timeval ppm; 70 71 unsigned long long elapsed, absolute; 72 gettimeofday(&ppm, NULL); 73 elapsed = ppm.tv_sec - ppm_start.tv_sec; 74 elapsed *= 1000000; 75 elapsed += ppm.tv_usec - ppm_start.tv_usec; 76 absolute = ppm.tv_sec; 77 absolute *= 1000; 78 absolute += ppm.tv_usec /1000; 79 80 CAMHAL_LOGI("PPM: %s :%llu.%llu ms : %llu ms", str, ( elapsed /1000 ), ( elapsed % 1000 ), absolute); 81 } 82 83 #endif 84 85 #if PPM_INSTRUMENTATION || PPM_INSTRUMENTATION_ABS 86 87 /** 88 @brief PPM instrumentation 89 90 Calculates and dumps the elapsed time using 'ppm_first' as 91 reference. 92 93 @param str - log message 94 @return none 95 96 */ 97 void CameraHal::PPM(const char* str, struct timeval* ppm_first, ...){ 98 char temp_str[256]; 99 struct timeval ppm; 100 unsigned long long absolute; 101 va_list args; 102 103 va_start(args, ppm_first); 104 vsprintf(temp_str, str, args); 105 gettimeofday(&ppm, NULL); 106 absolute = ppm.tv_sec; 107 absolute *= 1000; 108 absolute += ppm.tv_usec /1000; 109 ppm.tv_sec = ppm.tv_sec - ppm_first->tv_sec; 110 ppm.tv_sec = ppm.tv_sec * 1000000; 111 ppm.tv_sec = ppm.tv_sec + ppm.tv_usec - ppm_first->tv_usec; 112 113 CAMHAL_LOGI("PPM: %s :%ld.%ld ms : %llu ms", temp_str, ( ppm.tv_sec /1000 ), ( ppm.tv_sec % 1000 ), absolute); 114 115 va_end(args); 116 } 117 118 #endif 119 120 121 /** Common utility function definitions used all over the HAL */ 122 123 unsigned int CameraHal::getBPP(const char* format) { 124 unsigned int bytesPerPixel; 125 126 // Calculate bytes per pixel based on the pixel format 127 if (strcmp(format, android::CameraParameters::PIXEL_FORMAT_YUV422I) == 0) { 128 bytesPerPixel = 2; 129 } else if (strcmp(format, android::CameraParameters::PIXEL_FORMAT_RGB565) == 0 || 130 strcmp(format, android::CameraParameters::PIXEL_FORMAT_BAYER_RGGB) == 0) { 131 bytesPerPixel = 2; 132 } else if (strcmp(format, android::CameraParameters::PIXEL_FORMAT_YUV420SP) == 0) { 133 bytesPerPixel = 1; 134 } else { 135 bytesPerPixel = 1; 136 } 137 138 return bytesPerPixel; 139 } 140 141 void CameraHal::getXYFromOffset(unsigned int *x, unsigned int *y, 142 unsigned int offset, unsigned int stride, 143 const char* format) 144 { 145 CAMHAL_ASSERT( x && y && format && (0U < stride) ); 146 147 *x = (offset % stride) / getBPP(format); 148 *y = (offset / stride); 149 } 150 151 const char* CameraHal::getPixelFormatConstant(const char* parametersFormat) 152 { 153 const char *pixelFormat = NULL; 154 155 if ( NULL != parametersFormat ) { 156 if ( 0 == strcmp(parametersFormat, (const char *) android::CameraParameters::PIXEL_FORMAT_YUV422I) ) { 157 CAMHAL_LOGVA("CbYCrY format selected"); 158 pixelFormat = (const char *) android::CameraParameters::PIXEL_FORMAT_YUV422I; 159 } else if ( (0 == strcmp(parametersFormat, (const char *) android::CameraParameters::PIXEL_FORMAT_YUV420SP)) || 160 (0 == strcmp(parametersFormat, (const char *) android::CameraParameters::PIXEL_FORMAT_YUV420P)) ) { 161 // TODO(XXX): We are treating YV12 the same as YUV420SP 162 CAMHAL_LOGVA("YUV420SP format selected"); 163 pixelFormat = (const char *) android::CameraParameters::PIXEL_FORMAT_YUV420SP; 164 } else if ( 0 == strcmp(parametersFormat, (const char *) android::CameraParameters::PIXEL_FORMAT_RGB565) ) { 165 CAMHAL_LOGVA("RGB565 format selected"); 166 pixelFormat = (const char *) android::CameraParameters::PIXEL_FORMAT_RGB565; 167 } else if ( 0 == strcmp(parametersFormat, (const char *) android::CameraParameters::PIXEL_FORMAT_BAYER_RGGB) ) { 168 CAMHAL_LOGVA("BAYER format selected"); 169 pixelFormat = (const char *) android::CameraParameters::PIXEL_FORMAT_BAYER_RGGB; 170 } else if ( 0 == strcmp(parametersFormat, android::CameraParameters::PIXEL_FORMAT_JPEG) ) { 171 CAMHAL_LOGVA("JPEG format selected"); 172 pixelFormat = (const char *) android::CameraParameters::PIXEL_FORMAT_JPEG; 173 } else { 174 CAMHAL_LOGEA("Invalid format, NV12 format selected as default"); 175 pixelFormat = (const char *) android::CameraParameters::PIXEL_FORMAT_YUV420SP; 176 } 177 } else { 178 CAMHAL_LOGEA("Preview format is NULL, defaulting to NV12"); 179 pixelFormat = (const char *) android::CameraParameters::PIXEL_FORMAT_YUV420SP; 180 } 181 182 return pixelFormat; 183 } 184 185 size_t CameraHal::calculateBufferSize(const char* parametersFormat, int width, int height) 186 { 187 int bufferSize = -1; 188 189 if ( NULL != parametersFormat ) { 190 if ( 0 == strcmp(parametersFormat, (const char *) android::CameraParameters::PIXEL_FORMAT_YUV422I) ) { 191 bufferSize = width * height * 2; 192 } else if ( (0 == strcmp(parametersFormat, android::CameraParameters::PIXEL_FORMAT_YUV420SP)) || 193 (0 == strcmp(parametersFormat, android::CameraParameters::PIXEL_FORMAT_YUV420P)) ) { 194 bufferSize = width * height * 3 / 2; 195 } else if ( 0 == strcmp(parametersFormat, (const char *) android::CameraParameters::PIXEL_FORMAT_RGB565) ) { 196 bufferSize = width * height * 2; 197 } else if ( 0 == strcmp(parametersFormat, (const char *) android::CameraParameters::PIXEL_FORMAT_BAYER_RGGB) ) { 198 bufferSize = width * height * 2; 199 } else { 200 CAMHAL_LOGEA("Invalid format"); 201 bufferSize = 0; 202 } 203 } else { 204 CAMHAL_LOGEA("Preview format is NULL"); 205 bufferSize = 0; 206 } 207 208 return bufferSize; 209 } 210 211 212 } // namespace Camera 213 } // namespace Ti 214