blob: aee6b43bb0f39e786843a6feacbcfeae8cd20033
1 | /* |
2 | * Copyright (C) 2011 The Android Open Source Project |
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 | |
18 | |
19 | #ifndef V4L_CAMERA_ADAPTER_H |
20 | #define V4L_CAMERA_ADAPTER_H |
21 | |
22 | #include "CameraHal.h" |
23 | #include "BaseCameraAdapter.h" |
24 | #include "DebugUtils.h" |
25 | #include "Encoder_libjpeg.h" |
26 | #include <ion/ion.h> |
27 | |
28 | namespace android { |
29 | |
30 | #ifdef AMLOGIC_USB_CAMERA_SUPPORT |
31 | |
32 | #define DEFAULT_PREVIEW_PIXEL_FORMAT V4L2_PIX_FMT_NV21 |
33 | //#define DEFAULT_PREVIEW_PIXEL_FORMAT V4L2_PIX_FMT_YUYV |
34 | #define DEFAULT_IMAGE_CAPTURE_PIXEL_FORMAT V4L2_PIX_FMT_RGB24 |
35 | //#define DEFAULT_IMAGE_CAPTURE_PIXEL_FORMAT V4L2_PIX_FMT_NV21 |
36 | #else |
37 | #define DEFAULT_PREVIEW_PIXEL_FORMAT V4L2_PIX_FMT_NV21 |
38 | #define DEFAULT_IMAGE_CAPTURE_PIXEL_FORMAT V4L2_PIX_FMT_RGB24 |
39 | //#define DEFAULT_IMAGE_CAPTURE_PIXEL_FORMAT V4L2_PIX_FMT_NV21 |
40 | #endif |
41 | #define NB_BUFFER 6 |
42 | |
43 | #define MAX_LIMITED_RATE_NUM 6 |
44 | //#define ENCODE_TIME_DEBUG |
45 | //#define PRODUCE_TIME_DEBUG |
46 | |
47 | typedef enum device_type_e{ |
48 | DEV_MMAP = 0, |
49 | DEV_ION, |
50 | DEV_ION_MPLANE, |
51 | DEV_DMA, |
52 | DEV_CANVAS_MODE, |
53 | DEV_USB, |
54 | }device_type_t; |
55 | |
56 | struct VideoInfo { |
57 | struct v4l2_capability cap; |
58 | struct v4l2_format format; |
59 | struct v4l2_buffer buf; |
60 | struct v4l2_requestbuffers rb; |
61 | void *mem[NB_BUFFER]; |
62 | unsigned int canvas[NB_BUFFER]; |
63 | bool isStreaming; |
64 | bool canvas_mode; |
65 | int width; |
66 | int height; |
67 | int formatIn; |
68 | int framesizeIn; |
69 | uint32_t idVendor; |
70 | uint32_t idProduct; |
71 | }; |
72 | |
73 | typedef enum camera_mode_e{ |
74 | CAM_PREVIEW = 0, |
75 | CAM_CAPTURE, |
76 | CAM_RECORD, |
77 | }camera_mode_t; |
78 | |
79 | struct camera_fmt { |
80 | uint32_t pixelfmt; |
81 | int support; |
82 | } camera_fmt_t; |
83 | |
84 | typedef enum camera_light_mode_e { |
85 | ADVANCED_AWB = 0, |
86 | SIMPLE_AWB, |
87 | MANUAL_DAY, |
88 | MANUAL_A, |
89 | MANUAL_CWF, |
90 | MANUAL_CLOUDY, |
91 | }camera_light_mode_t; |
92 | |
93 | typedef enum camera_saturation_e { |
94 | SATURATION_N4_STEP = 0, |
95 | SATURATION_N3_STEP, |
96 | SATURATION_N2_STEP, |
97 | SATURATION_N1_STEP, |
98 | SATURATION_0_STEP, |
99 | SATURATION_P1_STEP, |
100 | SATURATION_P2_STEP, |
101 | SATURATION_P3_STEP, |
102 | SATURATION_P4_STEP, |
103 | }camera_saturation_t; |
104 | |
105 | |
106 | typedef enum camera_brightness_e { |
107 | BRIGHTNESS_N4_STEP = 0, |
108 | BRIGHTNESS_N3_STEP, |
109 | BRIGHTNESS_N2_STEP, |
110 | BRIGHTNESS_N1_STEP, |
111 | BRIGHTNESS_0_STEP, |
112 | BRIGHTNESS_P1_STEP, |
113 | BRIGHTNESS_P2_STEP, |
114 | BRIGHTNESS_P3_STEP, |
115 | BRIGHTNESS_P4_STEP, |
116 | }camera_brightness_t; |
117 | |
118 | typedef enum camera_contrast_e { |
119 | CONTRAST_N4_STEP = 0, |
120 | CONTRAST_N3_STEP, |
121 | CONTRAST_N2_STEP, |
122 | CONTRAST_N1_STEP, |
123 | CONTRAST_0_STEP, |
124 | CONTRAST_P1_STEP, |
125 | CONTRAST_P2_STEP, |
126 | CONTRAST_P3_STEP, |
127 | CONTRAST_P4_STEP, |
128 | }camera_contrast_t; |
129 | |
130 | typedef enum camera_hue_e { |
131 | HUE_N180_DEGREE = 0, |
132 | HUE_N150_DEGREE, |
133 | HUE_N120_DEGREE, |
134 | HUE_N90_DEGREE, |
135 | HUE_N60_DEGREE, |
136 | HUE_N30_DEGREE, |
137 | HUE_0_DEGREE, |
138 | HUE_P30_DEGREE, |
139 | HUE_P60_DEGREE, |
140 | HUE_P90_DEGREE, |
141 | HUE_P120_DEGREE, |
142 | HUE_P150_DEGREE, |
143 | }camera_hue_t; |
144 | |
145 | typedef enum camera_special_effect_e { |
146 | SPECIAL_EFFECT_NORMAL = 0, |
147 | SPECIAL_EFFECT_BW, |
148 | SPECIAL_EFFECT_BLUISH, |
149 | SPECIAL_EFFECT_SEPIA, |
150 | SPECIAL_EFFECT_REDDISH, |
151 | SPECIAL_EFFECT_GREENISH, |
152 | SPECIAL_EFFECT_NEGATIVE, |
153 | }camera_special_effect_t; |
154 | |
155 | typedef enum camera_exposure_e { |
156 | EXPOSURE_N4_STEP = 0, |
157 | EXPOSURE_N3_STEP, |
158 | EXPOSURE_N2_STEP, |
159 | EXPOSURE_N1_STEP, |
160 | EXPOSURE_0_STEP, |
161 | EXPOSURE_P1_STEP, |
162 | EXPOSURE_P2_STEP, |
163 | EXPOSURE_P3_STEP, |
164 | EXPOSURE_P4_STEP, |
165 | }camera_exposure_t; |
166 | |
167 | |
168 | typedef enum camera_sharpness_e { |
169 | SHARPNESS_1_STEP = 0, |
170 | SHARPNESS_2_STEP, |
171 | SHARPNESS_3_STEP, |
172 | SHARPNESS_4_STEP, |
173 | SHARPNESS_5_STEP, |
174 | SHARPNESS_6_STEP, |
175 | SHARPNESS_7_STEP, |
176 | SHARPNESS_8_STEP, |
177 | SHARPNESS_AUTO_STEP, |
178 | }camera_sharpness_t; |
179 | |
180 | typedef enum camera_mirror_flip_e { |
181 | MF_NORMAL = 0, |
182 | MF_MIRROR, |
183 | MF_FLIP, |
184 | MF_MIRROR_FLIP, |
185 | }camera_mirror_flip_t; |
186 | |
187 | |
188 | typedef enum camera_wb_flip_e { |
189 | CAM_WB_AUTO = 0, |
190 | CAM_WB_CLOUD, |
191 | CAM_WB_DAYLIGHT, |
192 | CAM_WB_INCANDESCENCE, |
193 | CAM_WB_TUNGSTEN, |
194 | CAM_WB_FLUORESCENT, |
195 | CAM_WB_MANUAL, |
196 | CAM_WB_SHADE, |
197 | CAM_WB_TWILIGHT, |
198 | CAM_WB_WARM_FLUORESCENT, |
199 | }camera_wb_flip_t; |
200 | typedef enum camera_night_mode_flip_e { |
201 | CAM_NM_AUTO = 0, |
202 | CAM_NM_ENABLE, |
203 | }camera_night_mode_flip_t; |
204 | typedef enum camera_banding_mode_flip_e { |
205 | CAM_ANTIBANDING_DISABLED= V4L2_CID_POWER_LINE_FREQUENCY_DISABLED, |
206 | CAM_ANTIBANDING_50HZ = V4L2_CID_POWER_LINE_FREQUENCY_50HZ, |
207 | CAM_ANTIBANDING_60HZ = V4L2_CID_POWER_LINE_FREQUENCY_60HZ, |
208 | CAM_ANTIBANDING_AUTO, |
209 | CAM_ANTIBANDING_OFF, |
210 | }camera_banding_mode_flip_t; |
211 | |
212 | typedef enum camera_effect_flip_e { |
213 | CAM_EFFECT_ENC_NORMAL = 0, |
214 | CAM_EFFECT_ENC_GRAYSCALE, |
215 | CAM_EFFECT_ENC_SEPIA, |
216 | CAM_EFFECT_ENC_SEPIAGREEN, |
217 | CAM_EFFECT_ENC_SEPIABLUE, |
218 | CAM_EFFECT_ENC_COLORINV, |
219 | }camera_effect_flip_t; |
220 | |
221 | typedef enum camera_flashlight_status_e{ |
222 | FLASHLIGHT_AUTO = 0, |
223 | FLASHLIGHT_ON, |
224 | FLASHLIGHT_OFF, |
225 | FLASHLIGHT_TORCH, |
226 | FLASHLIGHT_RED_EYE, |
227 | }camera_flashlight_status_t; |
228 | |
229 | typedef enum camera_focus_mode_e { |
230 | CAM_FOCUS_MODE_RELEASE = 0, |
231 | CAM_FOCUS_MODE_FIXED, |
232 | CAM_FOCUS_MODE_INFINITY, |
233 | CAM_FOCUS_MODE_AUTO, |
234 | CAM_FOCUS_MODE_MACRO, |
235 | CAM_FOCUS_MODE_EDOF, |
236 | CAM_FOCUS_MODE_CONTI_VID, |
237 | CAM_FOCUS_MODE_CONTI_PIC, |
238 | }camera_focus_mode_t; |
239 | |
240 | typedef struct cam_cache_buf{ |
241 | char *bufPtr; |
242 | int index; |
243 | unsigned canvas; |
244 | }cache_buf_t; |
245 | |
246 | typedef struct cam_LimitedRate_Item{ |
247 | int width; |
248 | int height; |
249 | int framerate; |
250 | }RateInfo_t; |
251 | |
252 | typedef struct cam_LimitedRate_Info{ |
253 | int num; |
254 | RateInfo_t arg[MAX_LIMITED_RATE_NUM]; |
255 | }LimitedRate_t; |
256 | |
257 | #define V4L2_ROTATE_ID 0x980922 //V4L2_CID_ROTATE |
258 | |
259 | #define V4L2_CID_AUTO_FOCUS_STATUS (V4L2_CID_CAMERA_CLASS_BASE+30) |
260 | #define V4L2_AUTO_FOCUS_STATUS_IDLE (0 << 0) |
261 | #define V4L2_AUTO_FOCUS_STATUS_BUSY (1 << 0) |
262 | #define V4L2_AUTO_FOCUS_STATUS_REACHED (1 << 1) |
263 | #define V4L2_AUTO_FOCUS_STATUS_FAILED (1 << 2) |
264 | |
265 | |
266 | #define IOCTL_MASK_HFLIP (1<<0) |
267 | #define IOCTL_MASK_ZOOM (1<<1) |
268 | #define IOCTL_MASK_FLASH (1<<2) |
269 | #define IOCTL_MASK_FOCUS (1<<3) |
270 | #define IOCTL_MASK_WB (1<<4) |
271 | #define IOCTL_MASK_EXPOSURE (1<<5) |
272 | #define IOCTL_MASK_EFFECT (1<<6) |
273 | #define IOCTL_MASK_BANDING (1<<7) |
274 | #define IOCTL_MASK_ROTATE (1<<8) |
275 | #define IOCTL_MASK_FOCUS_MOVE (1<<9) |
276 | |
277 | /** |
278 | * Class which completely abstracts the camera hardware interaction from camera hal |
279 | * TODO: Need to list down here, all the message types that will be supported by this class |
280 | */ |
281 | class V4LCameraAdapter : public BaseCameraAdapter |
282 | { |
283 | public: |
284 | |
285 | /*--------------------Constant declarations----------------------------------------*/ |
286 | static const int32_t MAX_NO_BUFFERS = 20; |
287 | |
288 | //static const int MAX_NO_PORTS = 6; |
289 | |
290 | ///Five second timeout |
291 | static const int CAMERA_ADAPTER_TIMEOUT = 5000*1000; |
292 | |
293 | public: |
294 | |
295 | V4LCameraAdapter(size_t sensor_index); |
296 | ~V4LCameraAdapter(); |
297 | |
298 | int SetExposure(int camera_fd,const char *sbn); |
299 | int SetExposureMode(int camera_fd, unsigned int mode); |
300 | int set_white_balance(int camera_fd,const char *swb); |
301 | int set_focus_area(int camera_fd, const char *focusarea); |
302 | int set_banding(int camera_fd,const char *snm); |
303 | status_t allocImageIONBuf(CameraProperties::Properties* caps); |
304 | |
305 | ///Initialzes the camera adapter creates any resources required |
306 | virtual status_t initialize(CameraProperties::Properties*); |
307 | //virtual status_t initialize(CameraProperties::Properties*, int sensor_index=0); |
308 | |
309 | //APIs to configure Camera adapter and get the current parameter set |
310 | virtual status_t setParameters(const CameraParameters& params); |
311 | virtual void getParameters(CameraParameters& params); |
312 | virtual status_t sendCommand(CameraCommands operation, int value1 = 0, int value2 = 0, int value3 = 0 ); |
313 | // API |
314 | virtual status_t UseBuffersPreview(void* bufArr, int num); |
315 | virtual status_t UseBuffersCapture(void* bufArr, int num); |
316 | |
317 | //API to flush the buffers for preview |
318 | status_t flushBuffers(); |
319 | |
320 | protected: |
321 | |
322 | //----------Parent class method implementation------------------------------------ |
323 | virtual status_t takePicture(); |
324 | virtual status_t autoFocus(); |
325 | virtual status_t cancelAutoFocus(); |
326 | virtual status_t startPreview(); |
327 | virtual status_t stopPreview(); |
328 | virtual status_t useBuffers(CameraMode mode, void* bufArr, int num, size_t length, unsigned int queueable); |
329 | virtual status_t fillThisBuffer(void* frameBuf, CameraFrame::FrameType frameType); |
330 | virtual status_t getFrameSize(size_t &width, size_t &height); |
331 | virtual status_t getPictureBufferSize(size_t &length, size_t bufferCount); |
332 | virtual status_t getFrameDataSize(size_t &dataFrameSize, size_t bufferCount); |
333 | virtual void onOrientationEvent(uint32_t orientation, uint32_t tilt); |
334 | //----------------------------------------------------------------------------- |
335 | status_t disableMirror(bool bDisable); |
336 | status_t setMirrorEffect(); |
337 | status_t getFocusMoveStatus(); |
338 | |
339 | private: |
340 | |
341 | class PreviewThread : public Thread { |
342 | V4LCameraAdapter* mAdapter; |
343 | public: |
344 | PreviewThread(V4LCameraAdapter* hw) : |
345 | Thread(false), mAdapter(hw) { } |
346 | virtual void onFirstRef() { |
347 | run("CameraPreviewThread", PRIORITY_URGENT_DISPLAY); |
348 | } |
349 | virtual bool threadLoop() { |
350 | mAdapter->previewThread(); |
351 | // loop until we need to quit |
352 | return true; |
353 | } |
354 | }; |
355 | |
356 | status_t setBuffersFormat(int width, int height, int pixelformat); |
357 | status_t tryBuffersFormat(int width, int height, int pixelformat); |
358 | status_t setCrop(int width, int height); |
359 | status_t getBuffersFormat(int &width, int &height, int &pixelformat); |
360 | |
361 | //Used for calculation of the average frame rate during preview |
362 | status_t recalculateFPS(); |
363 | |
364 | char * GetFrame(int &index, unsigned int* canvas); |
365 | |
366 | int previewThread(); |
367 | |
368 | static int beginPictureThread(void *cookie); |
369 | int pictureThread(); |
370 | |
371 | static int beginAutoFocusThread(void *cookie); |
372 | |
373 | int GenExif(ExifElementsTable* exiftable); |
374 | |
375 | status_t IoctlStateProbe(); |
376 | |
377 | status_t force_reset_sensor(); |
378 | |
379 | public: |
380 | |
381 | private: |
382 | int mPreviewBufferCount; |
383 | KeyedVector<int, int> mPreviewBufs; |
384 | KeyedVector<int, int> mPreviewIdxs; |
385 | mutable Mutex mPreviewBufsLock; |
386 | |
387 | //TODO use members from BaseCameraAdapter |
388 | camera_memory_t *mCaptureBuf; |
389 | int mImageFd; |
390 | int mIonFd; |
391 | ion_user_handle_t mIonHnd; |
392 | |
393 | CameraParameters mParams; |
394 | |
395 | int mPreviewWidth; |
396 | int mPreviewHeight; |
397 | int mCaptureWidth; |
398 | int mCaptureHeight; |
399 | int mPreviewOriation; |
400 | int mCaptureOriation; |
401 | bool mPreviewing; |
402 | bool mCapturing; |
403 | Mutex mLock; |
404 | |
405 | int mFrameCount; |
406 | int mLastFrameCount; |
407 | unsigned int mIter; |
408 | nsecs_t mLastFPSTime; |
409 | |
410 | //variables holding the estimated framerate |
411 | float mFPS, mLastFPS; |
412 | |
413 | int mSensorIndex; |
414 | bool mbFrontCamera; |
415 | bool mbDisableMirror; |
416 | |
417 | // protected by mLock |
418 | sp<PreviewThread> mPreviewThread; |
419 | |
420 | struct VideoInfo *mVideoInfo; |
421 | int mCameraHandle; |
422 | enum device_type_e m_eDeviceType; |
423 | enum v4l2_memory m_eV4l2Memory; |
424 | |
425 | #ifdef AMLOGIC_TWO_CH_UVC |
426 | int mCamEncodeHandle; |
427 | int mCamEncodeIndex; |
428 | #endif |
429 | |
430 | int nQueued; |
431 | int nDequeued; |
432 | |
433 | int mZoomlevel; |
434 | unsigned int mPixelFormat; |
435 | unsigned int mSensorFormat; |
436 | |
437 | #ifdef AMLOGIC_USB_CAMERA_SUPPORT |
438 | bool mIsDequeuedEIOError; |
439 | #endif |
440 | //int maxQueueable;//the max queued buffers in v4l |
441 | |
442 | camera_focus_mode_t cur_focus_mode; |
443 | camera_focus_mode_t cur_focus_mode_for_conti; |
444 | bool bFocusMoveState; |
445 | |
446 | bool mEnableContiFocus; |
447 | camera_flashlight_status_t mFlashMode; |
448 | unsigned int mIoctlSupport; |
449 | |
450 | int mWhiteBalance; |
451 | int mEV; |
452 | int mEVdef; |
453 | int mEVmin; |
454 | int mEVmax; |
455 | int mAntiBanding; |
456 | int mFocusWaitCount; |
457 | //suppose every 17frames to check the focus is running; |
458 | //in continuous mode |
459 | static const int FOCUS_PROCESS_FRAMES = 17; |
460 | |
461 | #ifdef AMLOGIC_CAMERA_NONBLOCK_SUPPORT |
462 | struct timeval previewTime1, previewTime2; |
463 | bool mFirstBuff; |
464 | int mFrameInvAdjust; |
465 | int mFrameInv; |
466 | cache_buf_t mCache; |
467 | #endif |
468 | #ifndef AMLOGIC_USB_CAMERA_SUPPORT |
469 | int mRotateValue; |
470 | #endif |
471 | LimitedRate_t LimitedRate; |
472 | #ifdef PREVIEW_TIME_DEBUG |
473 | DurationTimer preTimer; |
474 | int precount; |
475 | #endif |
476 | int mLimitedFrameRate; |
477 | unsigned mExpectedFrameInv; |
478 | bool mUseMJPEG; |
479 | bool mSupportMJPEG; |
480 | bool mDebugMJPEG; |
481 | int mFramerate; |
482 | unsigned mFailedCnt; |
483 | unsigned mEagainCnt; |
484 | uint32_t *mPreviewCache; |
485 | uint32_t mResetTH; |
486 | struct timeval mStartTime; |
487 | struct timeval mEndTime; |
488 | struct timeval mEagainStartTime; |
489 | struct timeval mEagainEndTime; |
490 | bool mEnableDump; |
491 | int mDumpCnt; |
492 | }; |
493 | }; //// namespace |
494 | #endif //V4L_CAMERA_ADAPTER_H |
495 | |
496 |