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