1 /* 2 ** 3 ** Copyright 2008, The Android Open Source Project 4 ** Copyright 2010, Samsung Electronics Co. LTD 5 ** 6 ** Licensed under the Apache License, Version 2.0 (the "License"); 7 ** you may not use this file except in compliance with the License. 8 ** You may obtain a copy of the License at 9 ** 10 ** http://www.apache.org/licenses/LICENSE-2.0 11 ** 12 ** Unless required by applicable law or agreed to in writing, software 13 ** distributed under the License is distributed on an "AS IS" BASIS, 14 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 ** See the License for the specific language governing permissions and 16 ** limitations under the License. 17 */ 18 19 #ifndef ANDROID_HARDWARE_CAMERA_SEC_H 20 #define ANDROID_HARDWARE_CAMERA_SEC_H 21 22 #include <stdio.h> 23 #include <string.h> 24 #include <stdlib.h> 25 26 #include <fcntl.h> 27 #include <unistd.h> 28 #include <errno.h> 29 #include <signal.h> 30 #include <sys/mman.h> 31 #include <sys/time.h> 32 #include <sys/ioctl.h> 33 #include <sys/poll.h> 34 #include <sys/stat.h> 35 36 #include <linux/videodev2.h> 37 #include <videodev2_samsung.h> 38 39 #include "JpegEncoder.h" 40 41 #include <camera/CameraHardwareInterface.h> 42 43 namespace android { 44 45 #define ENABLE_ESD_PREVIEW_CHECK 46 47 #if defined(LOG_NDEBUG) && LOG_NDEBUG == 0 48 #define LOG_CAMERA LOGD 49 #define LOG_CAMERA_PREVIEW LOGD 50 51 #define LOG_TIME_DEFINE(n) \ 52 struct timeval time_start_##n, time_stop_##n; unsigned long log_time_##n = 0; 53 54 #define LOG_TIME_START(n) \ 55 gettimeofday(&time_start_##n, NULL); 56 57 #define LOG_TIME_END(n) \ 58 gettimeofday(&time_stop_##n, NULL); log_time_##n = measure_time(&time_start_##n, &time_stop_##n); 59 60 #define LOG_TIME(n) \ 61 log_time_##n 62 63 #else 64 #define LOG_CAMERA(...) 65 #define LOG_CAMERA_PREVIEW(...) 66 #define LOG_TIME_DEFINE(n) 67 #define LOG_TIME_START(n) 68 #define LOG_TIME_END(n) 69 #define LOG_TIME(n) 70 #endif 71 72 #define JOIN(x, y) JOIN_AGAIN(x, y) 73 #define JOIN_AGAIN(x, y) x ## y 74 75 #define FRONT_CAM VGA 76 #define BACK_CAM S5K4ECGX 77 78 #if !defined (FRONT_CAM) || !defined(BACK_CAM) 79 #error "Please define the Camera module" 80 #endif 81 82 #define S5K4ECGX_PREVIEW_WIDTH 720 83 #define S5K4ECGX_PREVIEW_HEIGHT 480 84 #define S5K4ECGX_SNAPSHOT_WIDTH 2560 85 #define S5K4ECGX_SNAPSHOT_HEIGHT 1920 86 87 #define S5K4ECGX_POSTVIEW_WIDTH 640 88 #define S5K4ECGX_POSTVIEW_WIDE_WIDTH 800 89 #define S5K4ECGX_POSTVIEW_HEIGHT 480 90 #define S5K4ECGX_POSTVIEW_BPP 16 91 92 #define S5K4ECGX_THUMBNAIL_WIDTH 320 93 #define S5K4ECGX_THUMBNAIL_HEIGHT 240 94 #define S5K4ECGX_THUMBNAIL_BPP 16 95 96 /* focal length of 3.43mm */ 97 #define S5K4ECGX_FOCAL_LENGTH 343 98 99 #define VGA_PREVIEW_WIDTH 640 100 #define VGA_PREVIEW_HEIGHT 480 101 #define VGA_SNAPSHOT_WIDTH 640 102 #define VGA_SNAPSHOT_HEIGHT 480 103 104 #define VGA_THUMBNAIL_WIDTH 160 105 #define VGA_THUMBNAIL_HEIGHT 120 106 #define VGA_THUMBNAIL_BPP 16 107 108 /* focal length of 0.9mm */ 109 #define VGA_FOCAL_LENGTH 90 110 111 #define MAX_BACK_CAMERA_PREVIEW_WIDTH JOIN(BACK_CAM,_PREVIEW_WIDTH) 112 #define MAX_BACK_CAMERA_PREVIEW_HEIGHT JOIN(BACK_CAM,_PREVIEW_HEIGHT) 113 #define MAX_BACK_CAMERA_SNAPSHOT_WIDTH JOIN(BACK_CAM,_SNAPSHOT_WIDTH) 114 #define MAX_BACK_CAMERA_SNAPSHOT_HEIGHT JOIN(BACK_CAM,_SNAPSHOT_HEIGHT) 115 #define BACK_CAMERA_POSTVIEW_WIDTH JOIN(BACK_CAM,_POSTVIEW_WIDTH) 116 #define BACK_CAMERA_POSTVIEW_WIDE_WIDTH JOIN(BACK_CAM,_POSTVIEW_WIDE_WIDTH) 117 #define BACK_CAMERA_POSTVIEW_HEIGHT JOIN(BACK_CAM,_POSTVIEW_HEIGHT) 118 #define BACK_CAMERA_POSTVIEW_BPP JOIN(BACK_CAM,_POSTVIEW_BPP) 119 #define BACK_CAMERA_THUMBNAIL_WIDTH JOIN(BACK_CAM,_THUMBNAIL_WIDTH) 120 #define BACK_CAMERA_THUMBNAIL_HEIGHT JOIN(BACK_CAM,_THUMBNAIL_HEIGHT) 121 #define BACK_CAMERA_THUMBNAIL_BPP JOIN(BACK_CAM,_THUMBNAIL_BPP) 122 123 #define BACK_CAMERA_FOCAL_LENGTH JOIN(BACK_CAM,_FOCAL_LENGTH) 124 125 #define MAX_FRONT_CAMERA_PREVIEW_WIDTH JOIN(FRONT_CAM,_PREVIEW_WIDTH) 126 #define MAX_FRONT_CAMERA_PREVIEW_HEIGHT JOIN(FRONT_CAM,_PREVIEW_HEIGHT) 127 #define MAX_FRONT_CAMERA_SNAPSHOT_WIDTH JOIN(FRONT_CAM,_SNAPSHOT_WIDTH) 128 #define MAX_FRONT_CAMERA_SNAPSHOT_HEIGHT JOIN(FRONT_CAM,_SNAPSHOT_HEIGHT) 129 130 #define FRONT_CAMERA_THUMBNAIL_WIDTH JOIN(FRONT_CAM,_THUMBNAIL_WIDTH) 131 #define FRONT_CAMERA_THUMBNAIL_HEIGHT JOIN(FRONT_CAM,_THUMBNAIL_HEIGHT) 132 #define FRONT_CAMERA_THUMBNAIL_BPP JOIN(FRONT_CAM,_THUMBNAIL_BPP) 133 #define FRONT_CAMERA_FOCAL_LENGTH JOIN(FRONT_CAM,_FOCAL_LENGTH) 134 135 #define DEFAULT_JPEG_THUMBNAIL_WIDTH 256 136 #define DEFAULT_JPEG_THUMBNAIL_HEIGHT 192 137 138 #define CAMERA_DEV_NAME "/dev/video0" 139 #define CAMERA_DEV_NAME2 "/dev/video2" 140 141 #define CAMERA_DEV_NAME_TEMP "/data/videotmp_000" 142 #define CAMERA_DEV_NAME2_TEMP "/data/videotemp_002" 143 144 145 #define BPP 2 146 #define MIN(x, y) (((x) < (y)) ? (x) : (y)) 147 #define MAX_BUFFERS 11 148 149 /* 150 * V 4 L 2 F I M C E X T E N S I O N S 151 * 152 */ 153 #define V4L2_CID_ROTATION (V4L2_CID_PRIVATE_BASE + 0) 154 #define V4L2_CID_PADDR_Y (V4L2_CID_PRIVATE_BASE + 1) 155 #define V4L2_CID_PADDR_CB (V4L2_CID_PRIVATE_BASE + 2) 156 #define V4L2_CID_PADDR_CR (V4L2_CID_PRIVATE_BASE + 3) 157 #define V4L2_CID_PADDR_CBCR (V4L2_CID_PRIVATE_BASE + 4) 158 #define V4L2_CID_STREAM_PAUSE (V4L2_CID_PRIVATE_BASE + 53) 159 160 #define V4L2_CID_CAM_JPEG_MAIN_SIZE (V4L2_CID_PRIVATE_BASE + 32) 161 #define V4L2_CID_CAM_JPEG_MAIN_OFFSET (V4L2_CID_PRIVATE_BASE + 33) 162 #define V4L2_CID_CAM_JPEG_THUMB_SIZE (V4L2_CID_PRIVATE_BASE + 34) 163 #define V4L2_CID_CAM_JPEG_THUMB_OFFSET (V4L2_CID_PRIVATE_BASE + 35) 164 #define V4L2_CID_CAM_JPEG_POSTVIEW_OFFSET (V4L2_CID_PRIVATE_BASE + 36) 165 #define V4L2_CID_CAM_JPEG_QUALITY (V4L2_CID_PRIVATE_BASE + 37) 166 167 #define TPATTERN_COLORBAR 1 168 #define TPATTERN_HORIZONTAL 2 169 #define TPATTERN_VERTICAL 3 170 171 #define V4L2_PIX_FMT_YVYU v4l2_fourcc('Y', 'V', 'Y', 'U') 172 173 /* FOURCC for FIMC specific */ 174 #define V4L2_PIX_FMT_VYUY v4l2_fourcc('V', 'Y', 'U', 'Y') 175 #define V4L2_PIX_FMT_NV16 v4l2_fourcc('N', 'V', '1', '6') 176 #define V4L2_PIX_FMT_NV61 v4l2_fourcc('N', 'V', '6', '1') 177 #define V4L2_PIX_FMT_NV12T v4l2_fourcc('T', 'V', '1', '2') 178 /* 179 * U S E R D E F I N E D T Y P E S 180 * 181 */ 182 183 struct fimc_buffer { 184 void *start; 185 size_t length; 186 }; 187 188 struct yuv_fmt_list { 189 const char *name; 190 const char *desc; 191 unsigned int fmt; 192 int depth; 193 int planes; 194 }; 195 196 //s1 [Apply factory standard] 197 struct camsensor_date_info { 198 unsigned int year; 199 unsigned int month; 200 unsigned int date; 201 }; 202 203 204 class SecCamera { 205 public: 206 207 enum CAMERA_ID { 208 CAMERA_ID_BACK = 0, 209 CAMERA_ID_FRONT = 1, 210 }; 211 212 enum JPEG_QUALITY { 213 JPEG_QUALITY_ECONOMY = 0, 214 JPEG_QUALITY_NORMAL = 50, 215 JPEG_QUALITY_SUPERFINE = 100, 216 JPEG_QUALITY_MAX, 217 }; 218 219 enum OBJECT_TRACKING { 220 OBJECT_TRACKING_OFF, 221 OBJECT_TRACKING_ON, 222 OBJECT_TRACKING_MAX, 223 }; 224 225 /*VT call*/ 226 enum VT_MODE { 227 VT_MODE_OFF, 228 VT_MODE_ON, 229 VT_MODE_MAX, 230 }; 231 232 /*Camera sensor mode - Camcorder fix fps*/ 233 enum SENSOR_MODE { 234 SENSOR_MODE_CAMERA, 235 SENSOR_MODE_MOVIE, 236 }; 237 238 /*Camera Shot mode*/ 239 enum SHOT_MODE { 240 SHOT_MODE_SINGLE = 0, 241 SHOT_MODE_CONTINUOUS = 1, 242 SHOT_MODE_PANORAMA = 2, 243 SHOT_MODE_SMILE = 3, 244 SHOT_MODE_SELF = 6, 245 }; 246 247 enum CHK_DATALINE { 248 CHK_DATALINE_OFF, 249 CHK_DATALINE_ON, 250 CHK_DATALINE_MAX, 251 }; 252 253 int m_touch_af_start_stop; 254 255 struct gps_info_latiude { 256 unsigned int north_south; 257 unsigned int dgree; 258 unsigned int minute; 259 unsigned int second; 260 } gpsInfoLatitude; 261 struct gps_info_longitude { 262 unsigned int east_west; 263 unsigned int dgree; 264 unsigned int minute; 265 unsigned int second; 266 } gpsInfoLongitude; 267 struct gps_info_altitude { 268 unsigned int plus_minus; 269 unsigned int dgree; 270 unsigned int minute; 271 unsigned int second; 272 } gpsInfoAltitude; 273 274 SecCamera(); 275 ~SecCamera(); 276 277 static SecCamera* createInstance(void) 278 { 279 static SecCamera singleton; 280 return &singleton; 281 } 282 status_t dump(int fd, const Vector<String16>& args); 283 284 int flagCreate(void) const; 285 286 287 int getCameraId(void); 288 289 int startPreview(void); 290 int stopPreview(void); 291 292 int startRecord(void); 293 int stopRecord(void); 294 int getRecordFrame(void); 295 int releaseRecordFrame(int index); 296 unsigned int getRecPhyAddrY(int); 297 unsigned int getRecPhyAddrC(int); 298 299 int getPreview(void); 300 int setPreviewSize(int width, int height, int pixel_format); 301 int getPreviewSize(int *width, int *height, int *frame_size); 302 int getPreviewMaxSize(int *width, int *height); 303 int getPreviewPixelFormat(void); 304 int setPreviewImage(int index, unsigned char *buffer, int size); 305 306 int setSnapshotSize(int width, int height); 307 int getSnapshotSize(int *width, int *height, int *frame_size); 308 int getSnapshotMaxSize(int *width, int *height); 309 int setSnapshotPixelFormat(int pixel_format); 310 int getSnapshotPixelFormat(void); 311 312 unsigned char* getJpeg(unsigned char *snapshot_data, int snapshot_size, int *size); 313 unsigned char* yuv2Jpeg(unsigned char *raw_data, int raw_size, 314 int *jpeg_size, 315 int width, int height, int pixel_format); 316 317 int setJpegThumbnailSize(int width, int height); 318 int getJpegThumbnailSize(int *width, int *height); 319 320 int setAutofocus(void); 321 int zoomIn(void); 322 int zoomOut(void); 323 324 int SetRotate(int angle); 325 int getRotate(void); 326 327 int setVerticalMirror(void); 328 int setHorizontalMirror(void); 329 330 int setWhiteBalance(int white_balance); 331 int getWhiteBalance(void); 332 333 int setBrightness(int brightness); 334 int getBrightness(void); 335 336 int setImageEffect(int image_effect); 337 int getImageEffect(void); 338 339 int setSceneMode(int scene_mode); 340 int getSceneMode(void); 341 342 int setFlashMode(int flash_mode); 343 int getFlashMode(void); 344 345 int setMetering(int metering_value); 346 int getMetering(void); 347 348 int setISO(int iso_value); 349 int getISO(void); 350 351 int setContrast(int contrast_value); 352 int getContrast(void); 353 354 int setSaturation(int saturation_value); 355 int getSaturation(void); 356 357 int setSharpness(int sharpness_value); 358 int getSharpness(void); 359 360 int setWDR(int wdr_value); 361 int getWDR(void); 362 363 int setAntiShake(int anti_shake); 364 int getAntiShake(void); 365 366 int setJpegQuality(int jpeg_qality); 367 int getJpegQuality(void); 368 369 int setZoom(int zoom_level); 370 int getZoom(void); 371 372 int setObjectTracking(int object_tracking); 373 int getObjectTracking(void); 374 int getObjectTrackingStatus(void); 375 376 int setSmartAuto(int smart_auto); 377 int getSmartAuto(void); 378 int getAutosceneStatus(void); 379 380 int setBeautyShot(int beauty_shot); 381 int getBeautyShot(void); 382 383 int setVintageMode(int vintage_mode); 384 int getVintageMode(void); 385 386 int setFocusMode(int focus_mode); 387 int getFocusMode(void); 388 389 int setFaceDetect(int face_detect); 390 int getFaceDetect(void); 391 392 int setGPSLatitude(const char *gps_latitude); 393 int setGPSLongitude(const char *gps_longitude); 394 int setGPSAltitude(const char *gps_altitude); 395 int setGPSTimeStamp(const char *gps_timestamp); 396 int setGPSProcessingMethod(const char *gps_timestamp); 397 int cancelAutofocus(void); 398 int setFaceDetectLockUnlock(int facedetect_lockunlock); 399 int setObjectPosition(int x, int y); 400 int setObjectTrackingStartStop(int start_stop); 401 int setTouchAFStartStop(int start_stop); 402 int setCAFStatus(int on_off); 403 int getAutoFocusResult(void); 404 int setAntiBanding(int anti_banding); 405 int getPostview(void); 406 int setRecordingSize(int width, int height); 407 int setGamma(int gamma); 408 int setSlowAE(int slow_ae); 409 int setExifOrientationInfo(int orientationInfo); 410 int setBatchReflection(void); 411 int setSnapshotCmd(void); 412 int endSnapshot(void); 413 int setCameraSensorReset(void); 414 int setSensorMode(int sensor_mode); /* Camcorder fix fps */ 415 int setShotMode(int shot_mode); /* Shot mode */ 416 /*VT call*/ 417 int setVTmode(int vtmode); 418 int getVTmode(void); 419 int setBlur(int blur_level); 420 int getBlur(void); 421 int setDataLineCheck(int chk_dataline); 422 int getDataLineCheck(void); 423 int setDataLineCheckStop(void); 424 int setDefultIMEI(int imei); 425 int getDefultIMEI(void); 426 const __u8* getCameraSensorName(void); 427 int previewPoll(bool preview); 428 #ifdef ENABLE_ESD_PREVIEW_CHECK 429 int getCameraSensorESDStatus(void); 430 #endif // ENABLE_ESD_PREVIEW_CHECK 431 432 int setFrameRate(int frame_rate); 433 unsigned char* getJpeg(int*, unsigned int*); 434 int getSnapshotAndJpeg(unsigned char *yuv_buf, unsigned char *jpeg_buf, 435 unsigned int *output_size); 436 int getExif(unsigned char *pExifDst, unsigned char *pThumbSrc); 437 438 void getPostViewConfig(int*, int*, int*); 439 void getThumbnailConfig(int *width, int *height, int *size); 440 441 int getPostViewOffset(void); 442 int getCameraFd(void); 443 int getJpegFd(void); 444 void SetJpgAddr(unsigned char *addr); 445 unsigned int getPhyAddrY(int); 446 unsigned int getPhyAddrC(int); 447 void pausePreview(); 448 int initCamera(int index); 449 void DeinitCamera(); 450 static void setJpegRatio(double ratio) 451 { 452 if((ratio < 0) || (ratio > 1)) 453 return; 454 455 jpeg_ratio = ratio; 456 } 457 458 static double getJpegRatio() 459 { 460 return jpeg_ratio; 461 } 462 463 static void setInterleaveDataSize(int x) 464 { 465 interleaveDataSize = x; 466 } 467 468 static int getInterleaveDataSize() 469 { 470 return interleaveDataSize; 471 } 472 473 static void setJpegLineLength(int x) 474 { 475 jpegLineLength = x; 476 } 477 478 static int getJpegLineLength() 479 { 480 return jpegLineLength; 481 } 482 483 484 private: 485 v4l2_streamparm m_streamparm; 486 struct sec_cam_parm *m_params; 487 int m_flag_init; 488 489 int m_camera_id; 490 491 int m_cam_fd; 492 493 int m_cam_fd_temp; 494 int m_cam_fd2_temp; 495 496 int m_cam_fd2; 497 struct pollfd m_events_c2; 498 int m_flag_record_start; 499 500 int m_preview_v4lformat; 501 int m_preview_width; 502 int m_preview_height; 503 int m_preview_max_width; 504 int m_preview_max_height; 505 506 int m_snapshot_v4lformat; 507 int m_snapshot_width; 508 int m_snapshot_height; 509 int m_snapshot_max_width; 510 int m_snapshot_max_height; 511 512 int m_angle; 513 int m_anti_banding; 514 int m_wdr; 515 int m_anti_shake; 516 int m_zoom_level; 517 int m_object_tracking; 518 int m_smart_auto; 519 int m_beauty_shot; 520 int m_vintage_mode; 521 int m_face_detect; 522 int m_object_tracking_start_stop; 523 int m_recording_width; 524 int m_recording_height; 525 long m_gps_latitude; 526 long m_gps_longitude; 527 long m_gps_altitude; 528 long m_gps_timestamp; 529 int m_vtmode; 530 int m_sensor_mode; /*Camcorder fix fps */ 531 int m_shot_mode; /* Shot mode */ 532 int m_exif_orientation; 533 int m_blur_level; 534 int m_chk_dataline; 535 int m_video_gamma; 536 int m_slow_ae; 537 int m_caf_on_off; 538 int m_default_imei; 539 int m_camera_af_flag; 540 541 int m_flag_camera_start; 542 543 int m_jpeg_fd; 544 int m_jpeg_thumbnail_width; 545 int m_jpeg_thumbnail_height; 546 int m_jpeg_quality; 547 548 int m_postview_offset; 549 550 #ifdef ENABLE_ESD_PREVIEW_CHECK 551 int m_esd_check_count; 552 #endif // ENABLE_ESD_PREVIEW_CHECK 553 554 exif_attribute_t mExifInfo; 555 556 struct fimc_buffer m_capture_buf; 557 struct pollfd m_events_c; 558 559 inline int m_frameSize(int format, int width, int height); 560 561 void setExifChangedAttribute(); 562 void setExifFixedAttribute(); 563 void resetCamera(); 564 565 static double jpeg_ratio; 566 static int interleaveDataSize; 567 static int jpegLineLength; 568 }; 569 570 extern unsigned long measure_time(struct timeval *start, struct timeval *stop); 571 572 }; // namespace android 573 574 #endif // ANDROID_HARDWARE_CAMERA_SEC_H 575