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 
18 
19 #ifndef OMX_CAMERA_ADAPTER_H
20 #define OMX_CAMERA_ADAPTER_H
21 
22 #include "CameraHal.h"
23 #include "OMX_Types.h"
24 #include "OMX_Core.h"
25 #include "OMX_CoreExt.h"
26 #include "OMX_IVCommon.h"
27 #include "OMX_Component.h"
28 #include "OMX_Index.h"
29 #include "OMX_IndexExt.h"
30 #include "OMX_TI_Index.h"
31 #include "OMX_TI_IVCommon.h"
32 #include "OMX_TI_Common.h"
33 #include "OMX_TI_Image.h"
34 #include "General3A_Settings.h"
35 #include "OMXSceneModeTables.h"
36 
37 #include "BaseCameraAdapter.h"
38 #include "Encoder_libjpeg.h"
39 #include "DebugUtils.h"
40 
41 
42 extern "C"
43 {
44 #include "timm_osal_error.h"
45 #include "timm_osal_events.h"
46 #include "timm_osal_trace.h"
47 #include "timm_osal_semaphores.h"
48 }
49 
50 
51 namespace android {
52 
53 #define Q16_OFFSET                  16
54 
55 #define OMX_CMD_TIMEOUT             3000000  //3 sec.
56 #define OMX_CAPTURE_TIMEOUT         5000000  //5 sec.
57 
58 #define FOCUS_THRESHOLD             5 //[s.]
59 
60 #define MIN_JPEG_QUALITY            1
61 #define MAX_JPEG_QUALITY            100
62 #define EXP_BRACKET_RANGE           10
63 
64 #define FOCUS_DIST_SIZE             100
65 #define FOCUS_DIST_BUFFER_SIZE      500
66 
67 #define TOUCH_DATA_SIZE             200
68 #define DEFAULT_THUMB_WIDTH         160
69 #define DEFAULT_THUMB_HEIGHT        120
70 #define FRAME_RATE_FULL_HD          27
71 #define ZOOM_STAGES                 61
72 
73 #define FACE_DETECTION_BUFFER_SIZE  0x1000
74 #define MAX_NUM_FACES_SUPPORTED     35
75 
76 #define EXIF_MODEL_SIZE             100
77 #define EXIF_MAKE_SIZE              100
78 #define EXIF_DATE_TIME_SIZE         20
79 
80 #define GPS_MIN_DIV                 60
81 #define GPS_SEC_DIV                 60
82 #define GPS_SEC_ACCURACY            1000
83 #define GPS_TIMESTAMP_SIZE          6
84 #define GPS_DATESTAMP_SIZE          11
85 #define GPS_REF_SIZE                2
86 #define GPS_MAPDATUM_SIZE           100
87 #define GPS_PROCESSING_SIZE         100
88 #define GPS_VERSION_SIZE            4
89 #define GPS_NORTH_REF               "N"
90 #define GPS_SOUTH_REF               "S"
91 #define GPS_EAST_REF                "E"
92 #define GPS_WEST_REF                "W"
93 
94 /* Default portstartnumber of Camera component */
95 #define OMX_CAMERA_DEFAULT_START_PORT_NUM 0
96 
97 /* Define number of ports for differt domains */
98 #define OMX_CAMERA_PORT_OTHER_NUM 1
99 #define OMX_CAMERA_PORT_VIDEO_NUM 4
100 #define OMX_CAMERA_PORT_IMAGE_NUM 1
101 #define OMX_CAMERA_PORT_AUDIO_NUM 0
102 #define OMX_CAMERA_NUM_PORTS (OMX_CAMERA_PORT_OTHER_NUM + OMX_CAMERA_PORT_VIDEO_NUM + OMX_CAMERA_PORT_IMAGE_NUM + OMX_CAMERA_PORT_AUDIO_NUM)
103 
104 /* Define start port number for differt domains */
105 #define OMX_CAMERA_PORT_OTHER_START OMX_CAMERA_DEFAULT_START_PORT_NUM
106 #define OMX_CAMERA_PORT_VIDEO_START (OMX_CAMERA_PORT_OTHER_START + OMX_CAMERA_PORT_OTHER_NUM)
107 #define OMX_CAMERA_PORT_IMAGE_START (OMX_CAMERA_PORT_VIDEO_START + OMX_CAMERA_PORT_VIDEO_NUM)
108 #define OMX_CAMERA_PORT_AUDIO_START (OMX_CAMERA_PORT_IMAGE_START + OMX_CAMERA_PORT_IMAGE_NUM)
109 
110 /* Port index for camera component */
111 #define OMX_CAMERA_PORT_OTHER_IN (OMX_CAMERA_PORT_OTHER_START + 0)
112 #define OMX_CAMERA_PORT_VIDEO_IN_VIDEO (OMX_CAMERA_PORT_VIDEO_START + 0)
113 #define OMX_CAMERA_PORT_VIDEO_OUT_PREVIEW (OMX_CAMERA_PORT_VIDEO_START + 1)
114 #define OMX_CAMERA_PORT_VIDEO_OUT_VIDEO (OMX_CAMERA_PORT_VIDEO_START + 2)
115 #define OMX_CAMERA_PORT_VIDEO_OUT_MEASUREMENT (OMX_CAMERA_PORT_VIDEO_START + 3)
116 #define OMX_CAMERA_PORT_IMAGE_OUT_IMAGE (OMX_CAMERA_PORT_IMAGE_START + 0)
117 
118 
119 #define OMX_INIT_STRUCT(_s_, _name_)	\
120     memset(&(_s_), 0x0, sizeof(_name_));	\
121     (_s_).nSize = sizeof(_name_);		\
122     (_s_).nVersion.s.nVersionMajor = 0x1;	\
123     (_s_).nVersion.s.nVersionMinor = 0x1;	\
124     (_s_).nVersion.s.nRevision = 0x0;		\
125     (_s_).nVersion.s.nStep = 0x0
126 
127 #define OMX_INIT_STRUCT_PTR(_s_, _name_)   \
128     memset((_s_), 0x0, sizeof(_name_));         \
129     (_s_)->nSize = sizeof(_name_);              \
130     (_s_)->nVersion.s.nVersionMajor = 0x1;      \
131     (_s_)->nVersion.s.nVersionMinor = 0x1;      \
132     (_s_)->nVersion.s.nRevision = 0x0;          \
133     (_s_)->nVersion.s.nStep = 0x0
134 
135 #define GOTO_EXIT_IF(_CONDITION,_ERROR) {                                       \
136     if ((_CONDITION)) {                                                         \
137         eError = (_ERROR);                                                      \
138         goto EXIT;                                                              \
139     }                                                                           \
140 }
141 
142 const int64_t kCameraBufferLatencyNs = 250000000LL; // 250 ms
143 
144 ///OMX Specific Functions
145 static OMX_ERRORTYPE OMXCameraAdapterEventHandler(OMX_IN OMX_HANDLETYPE hComponent,
146                                         OMX_IN OMX_PTR pAppData,
147                                         OMX_IN OMX_EVENTTYPE eEvent,
148                                         OMX_IN OMX_U32 nData1,
149                                         OMX_IN OMX_U32 nData2,
150                                         OMX_IN OMX_PTR pEventData);
151 
152 static OMX_ERRORTYPE OMXCameraAdapterEmptyBufferDone(OMX_IN OMX_HANDLETYPE hComponent,
153                                         OMX_IN OMX_PTR pAppData,
154                                         OMX_IN OMX_BUFFERHEADERTYPE* pBuffer);
155 
156 static OMX_ERRORTYPE OMXCameraAdapterFillBufferDone(OMX_IN OMX_HANDLETYPE hComponent,
157                                         OMX_IN OMX_PTR pAppData,
158                                         OMX_IN OMX_BUFFERHEADERTYPE* pBuffHeader);
159 
160 struct CapResolution {
161     size_t width, height;
162     const char *param;
163 };
164 
165 struct CapPixelformat {
166     OMX_COLOR_FORMATTYPE pixelformat;
167     const char *param;
168 };
169 
170 struct CapU32 {
171     OMX_U32 num;
172     const char *param;
173 };
174 
175 struct CapU32Pair {
176     OMX_U32 num1, num2;
177     const char *param;
178 };
179 struct CapS32 {
180     OMX_S32 num;
181     const char *param;
182 };
183 
184 typedef CapU32 CapFramerate;
185 typedef CapU32 CapISO;
186 typedef CapU32 CapSensorName;
187 typedef CapS32 CapZoom;
188 typedef CapS32 CapEVComp;
189 
190 /**
191   * Class which completely abstracts the camera hardware interaction from camera hal
192   * TODO: Need to list down here, all the message types that will be supported by this class
193                 Need to implement BufferProvider interface to use AllocateBuffer of OMX if needed
194   */
195 class OMXCameraAdapter : public BaseCameraAdapter
196 {
197 public:
198 
199     /*--------------------Constant declarations----------------------------------------*/
200     static const int32_t MAX_NO_BUFFERS = 20;
201 
202     ///@remarks OMX Camera has six ports - buffer input, time input, preview, image, video, and meta data
203     static const int MAX_NO_PORTS = 6;
204 
205     ///Five second timeout
206     static const int CAMERA_ADAPTER_TIMEOUT = 5000*1000;
207 
208     enum OMXCameraEvents
209         {
210         CAMERA_PORT_ENABLE  = 0x1,
211         CAMERA_PORT_FLUSH   = 0x2,
212         CAMERA_PORT_DISABLE = 0x4,
213         };
214 
215     enum CaptureMode
216         {
217         HIGH_SPEED = 1,
218         HIGH_QUALITY = 2,
219         VIDEO_MODE = 3,
220         HIGH_QUALITY_ZSL = 4,
221         };
222 
223     enum IPPMode
224         {
225         IPP_NULL = -1,
226         IPP_NONE = 0,
227         IPP_NSF,
228         IPP_LDC,
229         IPP_LDCNSF,
230         };
231 
232     enum CodingMode
233         {
234         CodingNone = 0,
235         CodingJPS,
236         CodingMPO,
237         CodingRAWJPEG,
238         CodingRAWMPO,
239         };
240 
241     enum Algorithm3A
242         {
243         WHITE_BALANCE_ALGO = 0x1,
244         EXPOSURE_ALGO = 0x2,
245         FOCUS_ALGO = 0x4,
246         };
247 
248     enum AlgoPriority
249         {
250         FACE_PRIORITY = 0,
251         REGION_PRIORITY,
252         };
253 
254     enum BrightnessMode
255         {
256         BRIGHTNESS_OFF = 0,
257         BRIGHTNESS_ON,
258         BRIGHTNESS_AUTO,
259         };
260 
261     enum CaptureSettingsFlags {
262         SetFormat               = 1 << 0,
263         SetThumb                = 1 << 1,
264         SetExpBracket           = 1 << 2,
265         SetQuality              = 1 << 3,
266         SetRotation             = 1 << 4,
267         ECaptureSettingMax,
268         ECapturesettingsAll = ( ((ECaptureSettingMax -1 ) << 1) -1 ) /// all possible flags raised
269     };
270 
271     class GPSData
272     {
273         public:
274                 int mLongDeg, mLongMin, mLongSec, mLongSecDiv;
275                 char mLongRef[GPS_REF_SIZE];
276                 bool mLongValid;
277                 int mLatDeg, mLatMin, mLatSec, mLatSecDiv;
278                 char mLatRef[GPS_REF_SIZE];
279                 bool mLatValid;
280                 int mAltitude;
281                 unsigned char mAltitudeRef;
282                 bool mAltitudeValid;
283                 char mMapDatum[GPS_MAPDATUM_SIZE];
284                 bool mMapDatumValid;
285                 char mVersionId[GPS_VERSION_SIZE];
286                 bool mVersionIdValid;
287                 char mProcMethod[GPS_PROCESSING_SIZE];
288                 bool mProcMethodValid;
289                 char mDatestamp[GPS_DATESTAMP_SIZE];
290                 bool mDatestampValid;
291                 uint32_t mTimeStampHour;
292                 uint32_t mTimeStampMin;
293                 uint32_t mTimeStampSec;
294                 bool mTimeStampValid;
295     };
296 
297     class EXIFData
298     {
299         public:
300             GPSData mGPSData;
301             char mMake[EXIF_MODEL_SIZE];
302             char mModel[EXIF_MAKE_SIZE];
303             unsigned int mFocalNum, mFocalDen;
304             bool mMakeValid;
305             bool mModelValid;
306     };
307 
308     ///Parameters specific to any port of the OMX Camera component
309     class OMXCameraPortParameters
310     {
311         public:
312             OMX_U32                         mHostBufaddr[MAX_NO_BUFFERS];
313             OMX_BUFFERHEADERTYPE           *mBufferHeader[MAX_NO_BUFFERS];
314             OMX_U32                         mWidth;
315             OMX_U32                         mHeight;
316             OMX_U32                         mStride;
317             OMX_U8                          mNumBufs;
318 
319             // defines maximum number of buffers our of mNumBufs
320             // queueable at given moment
321             OMX_U8                          mMaxQueueable;
322 
323             OMX_U32                         mBufSize;
324             OMX_COLOR_FORMATTYPE            mColorFormat;
325             OMX_PARAM_VIDEONOISEFILTERTYPE  mVNFMode;
326             OMX_PARAM_VIDEOYUVRANGETYPE     mYUVRange;
327             OMX_CONFIG_BOOLEANTYPE          mVidStabParam;
328             OMX_CONFIG_FRAMESTABTYPE        mVidStabConfig;
329             OMX_U32                         mCapFrame;
330             OMX_U32                         mFrameRate;
331             OMX_S32                         mMinFrameRate;
332             OMX_S32                         mMaxFrameRate;
333             CameraFrame::FrameType mImageType;
334     };
335 
336     ///Context of the OMX Camera component
337     class OMXCameraAdapterComponentContext
338     {
339         public:
340             OMX_HANDLETYPE              mHandleComp;
341             OMX_U32                     mNumPorts;
342             OMX_STATETYPE               mState ;
343             OMX_U32                     mVideoPortIndex;
344             OMX_U32                     mPrevPortIndex;
345             OMX_U32                     mImagePortIndex;
346             OMX_U32                     mMeasurementPortIndex;
347             OMXCameraPortParameters     mCameraPortParams[MAX_NO_PORTS];
348     };
349 
350 public:
351 
352     OMXCameraAdapter(size_t sensor_index);
353     ~OMXCameraAdapter();
354 
355     ///Initialzes the camera adapter creates any resources required
356     virtual status_t initialize(CameraProperties::Properties*);
357 
358     //APIs to configure Camera adapter and get the current parameter set
359     virtual status_t setParameters(const CameraParameters& params);
360     virtual void getParameters(CameraParameters& params);
361 
362     // API
363     virtual status_t UseBuffersPreview(void* bufArr, int num);
364 
365     //API to flush the buffers for preview
366     status_t flushBuffers();
367 
368     // API
369     virtual status_t setFormat(OMX_U32 port, OMXCameraPortParameters &cap);
370 
371     // Function to get and populate caps from handle
372     static status_t getCaps(CameraProperties::Properties* props, OMX_HANDLETYPE handle);
373     static const char* getLUTvalue_OMXtoHAL(int OMXValue, LUTtype LUT);
374     static int getLUTvalue_HALtoOMX(const char * HalValue, LUTtype LUT);
375 
376  OMX_ERRORTYPE OMXCameraAdapterEventHandler(OMX_IN OMX_HANDLETYPE hComponent,
377                                     OMX_IN OMX_EVENTTYPE eEvent,
378                                     OMX_IN OMX_U32 nData1,
379                                     OMX_IN OMX_U32 nData2,
380                                     OMX_IN OMX_PTR pEventData);
381 
382  OMX_ERRORTYPE OMXCameraAdapterEmptyBufferDone(OMX_IN OMX_HANDLETYPE hComponent,
383                                     OMX_IN OMX_BUFFERHEADERTYPE* pBuffer);
384 
385  OMX_ERRORTYPE OMXCameraAdapterFillBufferDone(OMX_IN OMX_HANDLETYPE hComponent,
386                                     OMX_IN OMX_BUFFERHEADERTYPE* pBuffHeader);
387 
388  static OMX_ERRORTYPE OMXCameraGetHandle(OMX_HANDLETYPE *handle, OMX_PTR pAppData=NULL);
389 
390 protected:
391 
392     //Parent class method implementation
393     virtual status_t takePicture();
394     virtual status_t stopImageCapture();
395     virtual status_t startBracketing(int range);
396     virtual status_t stopBracketing();
397     virtual status_t autoFocus();
398     virtual status_t cancelAutoFocus();
399     virtual status_t startSmoothZoom(int targetIdx);
400     virtual status_t stopSmoothZoom();
401     virtual status_t startVideoCapture();
402     virtual status_t stopVideoCapture();
403     virtual status_t startPreview();
404     virtual status_t stopPreview();
405     virtual status_t useBuffers(CameraMode mode, void* bufArr, int num, size_t length, unsigned int queueable);
406     virtual status_t fillThisBuffer(void* frameBuf, CameraFrame::FrameType frameType);
407     virtual status_t getFrameSize(size_t &width, size_t &height);
408     virtual status_t getPictureBufferSize(size_t &length, size_t bufferCount);
409     virtual status_t getFrameDataSize(size_t &dataFrameSize, size_t bufferCount);
410     virtual status_t startFaceDetection();
411     virtual status_t stopFaceDetection();
412     virtual status_t switchToExecuting();
413     virtual void onOrientationEvent(uint32_t orientation, uint32_t tilt);
414 
415 private:
416 
417     status_t doSwitchToExecuting();
418 
419     void performCleanupAfterError();
420 
421     status_t switchToLoaded();
422 
423     OMXCameraPortParameters *getPortParams(CameraFrame::FrameType frameType);
424 
425     OMX_ERRORTYPE SignalEvent(OMX_IN OMX_HANDLETYPE hComponent,
426                                                   OMX_IN OMX_EVENTTYPE eEvent,
427                                                   OMX_IN OMX_U32 nData1,
428                                                   OMX_IN OMX_U32 nData2,
429                                                   OMX_IN OMX_PTR pEventData);
430     OMX_ERRORTYPE RemoveEvent(OMX_IN OMX_HANDLETYPE hComponent,
431                               OMX_IN OMX_EVENTTYPE eEvent,
432                               OMX_IN OMX_U32 nData1,
433                               OMX_IN OMX_U32 nData2,
434                               OMX_IN OMX_PTR pEventData);
435 
436     status_t RegisterForEvent(OMX_IN OMX_HANDLETYPE hComponent,
437                                           OMX_IN OMX_EVENTTYPE eEvent,
438                                           OMX_IN OMX_U32 nData1,
439                                           OMX_IN OMX_U32 nData2,
440                                           OMX_IN Semaphore &semaphore);
441 
442     status_t setPictureRotation(unsigned int degree);
443     status_t setSensorOrientation(unsigned int degree);
444     status_t setImageQuality(unsigned int quality);
445     status_t setThumbnailParams(unsigned int width, unsigned int height, unsigned int quality);
446 
447     //EXIF
448     status_t setParametersEXIF(const CameraParameters &params,
449                                BaseCameraAdapter::AdapterState state);
450     status_t convertGPSCoord(double coord, int &deg, int &min, int &sec, int &secDivisor);
451     status_t setupEXIF();
452     status_t setupEXIF_libjpeg(ExifElementsTable*, OMX_TI_ANCILLARYDATATYPE*,
453                                OMX_TI_WHITEBALANCERESULTTYPE*);
454 
455     //Focus functionality
456     status_t doAutoFocus();
457     status_t stopAutoFocus();
458     status_t checkFocus(OMX_PARAM_FOCUSSTATUSTYPE *eFocusStatus);
459     status_t returnFocusStatus(bool timeoutReached);
460     status_t getFocusMode(OMX_IMAGE_CONFIG_FOCUSCONTROLTYPE &focusMode);
461     void handleFocusCallback();
462 
463 
464     //Focus distances
465     status_t setParametersFocus(const CameraParameters &params,
466                                 BaseCameraAdapter::AdapterState state);
467     status_t addFocusDistances(OMX_U32 &near,
468                                OMX_U32 &optimal,
469                                OMX_U32 &far,
470                                CameraParameters& params);
471     status_t encodeFocusDistance(OMX_U32 dist, char *buffer, size_t length);
472     status_t getFocusDistances(OMX_U32 &near,OMX_U32 &optimal, OMX_U32 &far);
473 
474     //VSTAB and VNF Functionality
475     status_t enableVideoNoiseFilter(bool enable);
476     status_t enableVideoStabilization(bool enable);
477 
478     //Digital zoom
479     status_t setParametersZoom(const CameraParameters &params,
480                                BaseCameraAdapter::AdapterState state);
481     status_t doZoom(int index);
482     status_t advanceZoom();
483 
484     //3A related parameters
485     status_t setParameters3A(const CameraParameters &params,
486                              BaseCameraAdapter::AdapterState state);
487 
488     // scene modes
489     status_t setScene(Gen3A_settings& Gen3A);
490     // returns pointer to SceneModesEntry from the LUT for camera given 'name' and 'scene'
491     static const SceneModesEntry* getSceneModeEntry(const char* name, OMX_SCENEMODETYPE scene);
492 
493 
494     //Flash modes
495     status_t setFlashMode(Gen3A_settings& Gen3A);
496     status_t getFlashMode(Gen3A_settings& Gen3A);
497 
498     // Focus modes
499     status_t setFocusMode(Gen3A_settings& Gen3A);
500     status_t getFocusMode(Gen3A_settings& Gen3A);
501 
502     //Exposure Modes
503     status_t setExposureMode(Gen3A_settings& Gen3A);
504     status_t setEVCompensation(Gen3A_settings& Gen3A);
505     status_t setWBMode(Gen3A_settings& Gen3A);
506     status_t setFlicker(Gen3A_settings& Gen3A);
507     status_t setBrightness(Gen3A_settings& Gen3A);
508     status_t setContrast(Gen3A_settings& Gen3A);
509     status_t setSharpness(Gen3A_settings& Gen3A);
510     status_t setSaturation(Gen3A_settings& Gen3A);
511     status_t setISO(Gen3A_settings& Gen3A);
512     status_t setEffect(Gen3A_settings& Gen3A);
513     status_t setMeteringAreas(Gen3A_settings& Gen3A);
514 
515     status_t getEVCompensation(Gen3A_settings& Gen3A);
516     status_t getWBMode(Gen3A_settings& Gen3A);
517     status_t getSharpness(Gen3A_settings& Gen3A);
518     status_t getSaturation(Gen3A_settings& Gen3A);
519     status_t getISO(Gen3A_settings& Gen3A);
520 
521     // 3A locks
522     status_t setExposureLock(Gen3A_settings& Gen3A);
523     status_t setFocusLock(Gen3A_settings& Gen3A);
524     status_t setWhiteBalanceLock(Gen3A_settings& Gen3A);
525     status_t set3ALock(OMX_BOOL toggleExp, OMX_BOOL toggleWb, OMX_BOOL toggleFocus);
526 
527     //API to set FrameRate using VFR interface
528     status_t setVFramerate(OMX_U32 minFrameRate,OMX_U32 maxFrameRate);
529 
530     status_t setParametersAlgo(const CameraParameters &params,
531                                BaseCameraAdapter::AdapterState state);
532 
533     //Noise filtering
534     status_t setNSF(OMXCameraAdapter::IPPMode mode);
535 
536     //LDC
537     status_t setLDC(OMXCameraAdapter::IPPMode mode);
538 
539     //GLBCE
540     status_t setGLBCE(OMXCameraAdapter::BrightnessMode mode);
541 
542     //GBCE
543     status_t setGBCE(OMXCameraAdapter::BrightnessMode mode);
544 
545     status_t printComponentVersion(OMX_HANDLETYPE handle);
546 
547     //Touch AF
548     status_t setTouchFocus();
549 
550     //Face detection
551     status_t setParametersFD(const CameraParameters &params,
552                              BaseCameraAdapter::AdapterState state);
553     status_t updateFocusDistances(CameraParameters &params);
554     status_t setFaceDetection(bool enable, OMX_U32 orientation);
555     status_t detectFaces(OMX_BUFFERHEADERTYPE* pBuffHeader,
556                          sp<CameraFDResult> &result,
557                          size_t previewWidth,
558                          size_t previewHeight);
559     status_t encodeFaceCoordinates(const OMX_FACEDETECTIONTYPE *faceData,
560                                    camera_frame_metadata_t **pFaces,
561                                    size_t previewWidth,
562                                    size_t previewHeight);
563     void pauseFaceDetection(bool pause);
564 
565     //3A Algorithms priority configuration
566     status_t setAlgoPriority(AlgoPriority priority, Algorithm3A algo, bool enable);
567 
568     //Sensor overclocking
569     status_t setSensorOverclock(bool enable);
570 
571     // Utility methods for OMX Capabilities
572     static status_t insertCapabilities(CameraProperties::Properties*, OMX_TI_CAPTYPE&);
573     static status_t encodeSizeCap(OMX_TI_CAPRESTYPE&, const CapResolution *, size_t, char *, size_t);
574     static status_t encodeISOCap(OMX_U32, const CapISO*, size_t, char*, size_t);
575     static size_t encodeZoomCap(OMX_S32, const CapZoom*, size_t, char*, size_t);
576     static status_t encodeFramerateCap(OMX_U32, OMX_U32, const CapFramerate*, size_t, char*, size_t);
577     static status_t encodeVFramerateCap(OMX_TI_CAPTYPE&, const CapU32Pair*, size_t, char*, char*, size_t);
578     static status_t encodePixelformatCap(OMX_COLOR_FORMATTYPE,
579                                          const CapPixelformat*,
580                                          size_t,
581                                          char*,
582                                          size_t);
583     static status_t insertImageSizes(CameraProperties::Properties*, OMX_TI_CAPTYPE&);
584     static status_t insertPreviewSizes(CameraProperties::Properties*, OMX_TI_CAPTYPE&);
585     static status_t insertThumbSizes(CameraProperties::Properties*, OMX_TI_CAPTYPE&);
586     static status_t insertZoomStages(CameraProperties::Properties*, OMX_TI_CAPTYPE&);
587     static status_t insertImageFormats(CameraProperties::Properties*, OMX_TI_CAPTYPE&);
588     static status_t insertPreviewFormats(CameraProperties::Properties*, OMX_TI_CAPTYPE&);
589     static status_t insertFramerates(CameraProperties::Properties*, OMX_TI_CAPTYPE&);
590     static status_t insertVFramerates(CameraProperties::Properties*, OMX_TI_CAPTYPE&);
591     static status_t insertEVs(CameraProperties::Properties*, OMX_TI_CAPTYPE&);
592     static status_t insertISOModes(CameraProperties::Properties*, OMX_TI_CAPTYPE&);
593     static status_t insertIPPModes(CameraProperties::Properties*, OMX_TI_CAPTYPE &);
594     static status_t insertWBModes(CameraProperties::Properties*, OMX_TI_CAPTYPE&);
595     static status_t insertEffects(CameraProperties::Properties*, OMX_TI_CAPTYPE&);
596     static status_t insertExpModes(CameraProperties::Properties*, OMX_TI_CAPTYPE&);
597     static status_t insertSceneModes(CameraProperties::Properties*, OMX_TI_CAPTYPE &);
598     static status_t insertFocusModes(CameraProperties::Properties*, OMX_TI_CAPTYPE&);
599     static status_t insertFlickerModes(CameraProperties::Properties*, OMX_TI_CAPTYPE&);
600     static status_t insertFlashModes(CameraProperties::Properties*, OMX_TI_CAPTYPE&);
601     static status_t insertSenMount(CameraProperties::Properties*, OMX_TI_CAPTYPE&);
602     static status_t insertDefaults(CameraProperties::Properties*, OMX_TI_CAPTYPE&);
603     static status_t insertLocks(CameraProperties::Properties*, OMX_TI_CAPTYPE&);
604     static status_t insertAreas(CameraProperties::Properties*, OMX_TI_CAPTYPE&);
605     static status_t insertVideoSizes(CameraProperties::Properties*, OMX_TI_CAPTYPE&);
606 
607     status_t setParametersCapture(const CameraParameters &params,
608                                   BaseCameraAdapter::AdapterState state);
609 
610     //Exposure Bracketing
611     status_t setExposureBracketing(int *evValues, size_t evCount, size_t frameCount);
612     status_t parseExpRange(const char *rangeStr, int * expRange, size_t count, size_t &validEntries);
613 
614     //Temporal Bracketing
615     status_t doBracketing(OMX_BUFFERHEADERTYPE *pBuffHeader, CameraFrame::FrameType typeOfFrame);
616     status_t sendBracketFrames();
617 
618     // Image Capture Service
619     status_t startImageCapture();
620     status_t disableImagePort();
621 
622     //Shutter callback notifications
623     status_t setShutterCallback(bool enabled);
624 
625     //Sets eithter HQ or HS mode and the frame count
626     status_t setCaptureMode(OMXCameraAdapter::CaptureMode mode);
627     status_t UseBuffersCapture(void* bufArr, int num);
628     status_t UseBuffersPreviewData(void* bufArr, int num);
629 
630     //Used for calculation of the average frame rate during preview
631     status_t recalculateFPS();
632 
633     //Helper method for initializing a CameFrame object
634     status_t initCameraFrame(CameraFrame &frame, OMX_IN OMX_BUFFERHEADERTYPE *pBuffHeader, int typeOfFrame, OMXCameraPortParameters *port);
635 
636     //Sends the incoming OMX buffer header to subscribers
637     status_t sendFrame(CameraFrame &frame);
638 
639     status_t sendCallBacks(CameraFrame frame, OMX_IN OMX_BUFFERHEADERTYPE *pBuffHeader, unsigned int mask, OMXCameraPortParameters *port);
640 
641     status_t apply3Asettings( Gen3A_settings& Gen3A );
642     status_t init3AParams(Gen3A_settings &Gen3A);
643 
644     // AutoConvergence
645     status_t setAutoConvergence(OMX_TI_AUTOCONVERGENCEMODETYPE pACMode, OMX_S32 pManualConverence);
646     status_t getAutoConvergence(OMX_TI_AUTOCONVERGENCEMODETYPE *pACMode, OMX_S32 *pManualConverence);
647 
648     status_t setExtraData(bool enable, OMX_U32, OMX_EXT_EXTRADATATYPE);
649     OMX_OTHER_EXTRADATATYPE *getExtradata(OMX_OTHER_EXTRADATATYPE *extraData, OMX_EXTRADATATYPE type);
650 
651     class CommandHandler : public Thread {
652         public:
CommandHandler(OMXCameraAdapter * ca)653             CommandHandler(OMXCameraAdapter* ca)
654                 : Thread(false), mCameraAdapter(ca) { }
655 
threadLoop()656             virtual bool threadLoop() {
657                 bool ret;
658                 ret = Handler();
659                 return ret;
660             }
661 
put(TIUTILS::Message * msg)662             status_t put(TIUTILS::Message* msg){
663                 Mutex::Autolock lock(mLock);
664                 return mCommandMsgQ.put(msg);
665             }
666 
clearCommandQ()667             void clearCommandQ()
668                 {
669                 Mutex::Autolock lock(mLock);
670                 mCommandMsgQ.clear();
671                 }
672 
673             enum {
674                 COMMAND_EXIT = -1,
675                 CAMERA_START_IMAGE_CAPTURE = 0,
676                 CAMERA_PERFORM_AUTOFOCUS = 1,
677                 CAMERA_SWITCH_TO_EXECUTING,
678             };
679 
680         private:
681             bool Handler();
682             TIUTILS::MessageQueue mCommandMsgQ;
683             OMXCameraAdapter* mCameraAdapter;
684             Mutex mLock;
685     };
686     sp<CommandHandler> mCommandHandler;
687 
688 public:
689 
690     class OMXCallbackHandler : public Thread {
691         public:
OMXCallbackHandler(OMXCameraAdapter * ca)692         OMXCallbackHandler(OMXCameraAdapter* ca)
693             : Thread(false), mCameraAdapter(ca) { }
694 
threadLoop()695         virtual bool threadLoop() {
696             bool ret;
697             ret = Handler();
698             return ret;
699         }
700 
put(TIUTILS::Message * msg)701         status_t put(TIUTILS::Message* msg){
702             Mutex::Autolock lock(mLock);
703             return mCommandMsgQ.put(msg);
704         }
705 
clearCommandQ()706         void clearCommandQ()
707             {
708             Mutex::Autolock lock(mLock);
709             mCommandMsgQ.clear();
710             }
711 
712         enum {
713             COMMAND_EXIT = -1,
714             CAMERA_FILL_BUFFER_DONE,
715             CAMERA_FOCUS_STATUS,
716         };
717 
718     private:
719         bool Handler();
720         TIUTILS::MessageQueue mCommandMsgQ;
721         OMXCameraAdapter* mCameraAdapter;
722         Mutex mLock;
723     };
724 
725     sp<OMXCallbackHandler> mOMXCallbackHandler;
726 
727 private:
728 
729     //AF callback
730     status_t setFocusCallback(bool enabled);
731 
732     //OMX Capabilities data
733     static const CapResolution mImageCapRes [];
734     static const CapResolution mPreviewRes [];
735     static const CapResolution mThumbRes [];
736     static const CapPixelformat mPixelformats [];
737     static const CapFramerate mFramerates [];
738     static const CapU32 mSensorNames[] ;
739     static const CapZoom mZoomStages [];
740     static const CapEVComp mEVCompRanges [];
741     static const CapISO mISOStages [];
742     static const CapU32Pair mVarFramerates [];
743 
744     // OMX Camera defaults
745     static const char DEFAULT_ANTIBANDING[];
746     static const char DEFAULT_BRIGHTNESS[];
747     static const char DEFAULT_CONTRAST[];
748     static const char DEFAULT_EFFECT[];
749     static const char DEFAULT_EV_COMPENSATION[];
750     static const char DEFAULT_EV_STEP[];
751     static const char DEFAULT_EXPOSURE_MODE[];
752     static const char DEFAULT_FLASH_MODE[];
753     static const char DEFAULT_FOCUS_MODE_PREFERRED[];
754     static const char DEFAULT_FOCUS_MODE[];
755     static const char DEFAULT_FRAMERATE_RANGE_IMAGE[];
756     static const char DEFAULT_FRAMERATE_RANGE_VIDEO[];
757     static const char DEFAULT_IPP[];
758     static const char DEFAULT_GBCE[];
759     static const char DEFAULT_ISO_MODE[];
760     static const char DEFAULT_JPEG_QUALITY[];
761     static const char DEFAULT_THUMBNAIL_QUALITY[];
762     static const char DEFAULT_THUMBNAIL_SIZE[];
763     static const char DEFAULT_PICTURE_FORMAT[];
764     static const char DEFAULT_PICTURE_SIZE[];
765     static const char DEFAULT_PREVIEW_FORMAT[];
766     static const char DEFAULT_FRAMERATE[];
767     static const char DEFAULT_PREVIEW_SIZE[];
768     static const char DEFAULT_NUM_PREV_BUFS[];
769     static const char DEFAULT_NUM_PIC_BUFS[];
770     static const char DEFAULT_MAX_FOCUS_AREAS[];
771     static const char DEFAULT_SATURATION[];
772     static const char DEFAULT_SCENE_MODE[];
773     static const char DEFAULT_SHARPNESS[];
774     static const char DEFAULT_VSTAB[];
775     static const char DEFAULT_VSTAB_SUPPORTED[];
776     static const char DEFAULT_WB[];
777     static const char DEFAULT_ZOOM[];
778     static const char DEFAULT_MAX_FD_HW_FACES[];
779     static const char DEFAULT_MAX_FD_SW_FACES[];
780     static const char DEFAULT_AE_LOCK[];
781     static const char DEFAULT_AWB_LOCK[];
782     static const char DEFAULT_MAX_NUM_METERING_AREAS[];
783     static const char DEFAULT_LOCK_SUPPORTED[];
784     static const char DEFAULT_LOCK_UNSUPPORTED[];
785     static const char DEFAULT_FOCAL_LENGTH_PRIMARY[];
786     static const char DEFAULT_FOCAL_LENGTH_SECONDARY[];
787     static const char DEFAULT_HOR_ANGLE[];
788     static const char DEFAULT_VER_ANGLE[];
789     static const char DEFAULT_VIDEO_SNAPSHOT_SUPPORTED[];
790     static const char DEFAULT_VIDEO_SIZE[];
791     static const char DEFAULT_PREFERRED_PREVIEW_SIZE_FOR_VIDEO[];
792     static const size_t MAX_FOCUS_AREAS;
793 
794     OMX_VERSIONTYPE mCompRevision;
795 
796     //OMX Component UUID
797     OMX_UUIDTYPE mCompUUID;
798 
799     //Current Focus distances
800     char mFocusDistNear[FOCUS_DIST_SIZE];
801     char mFocusDistOptimal[FOCUS_DIST_SIZE];
802     char mFocusDistFar[FOCUS_DIST_SIZE];
803     char mFocusDistBuffer[FOCUS_DIST_BUFFER_SIZE];
804 
805     // Current Focus areas
806     Vector< sp<CameraArea> > mFocusAreas;
807     mutable Mutex mFocusAreasLock;
808 
809     // Current Metering areas
810     Vector< sp<CameraArea> > mMeteringAreas;
811     mutable Mutex mMeteringAreasLock;
812 
813     CaptureMode mCapMode;
814     size_t mBurstFrames;
815     size_t mCapturedFrames;
816 
817     bool mMeasurementEnabled;
818 
819     //Exposure Bracketing
820     int mExposureBracketingValues[EXP_BRACKET_RANGE];
821     size_t mExposureBracketingValidEntries;
822 
823     mutable Mutex mFaceDetectionLock;
824     //Face detection status
825     bool mFaceDetectionRunning;
826     bool mFaceDetectionPaused;
827     bool mFDSwitchAlgoPriority;
828 
829     camera_face_t  faceDetectionLastOutput [MAX_NUM_FACES_SUPPORTED];
830     int faceDetectionNumFacesLastOutput;
831 
832     //Geo-tagging
833     EXIFData mEXIFData;
834 
835     //Image post-processing
836     IPPMode mIPP;
837 
838     //jpeg Picture Quality
839     unsigned int mPictureQuality;
840 
841     //thumbnail resolution
842     unsigned int mThumbWidth, mThumbHeight;
843 
844     //thumbnail quality
845     unsigned int mThumbQuality;
846 
847     //variables holding the estimated framerate
848     float mFPS, mLastFPS;
849 
850     //automatically disable AF after a given amount of frames
851     unsigned int mFocusThreshold;
852 
853     //This is needed for the CTS tests. They falsely assume, that during
854     //smooth zoom the current zoom stage will not change within the
855     //zoom callback scope, which in a real world situation is not always the
856     //case. This variable will "simulate" the expected behavior
857     unsigned int mZoomParameterIdx;
858 
859     //current zoom
860     Mutex mZoomLock;
861     unsigned int mCurrentZoomIdx, mTargetZoomIdx, mPreviousZoomIndx;
862     bool mZoomUpdating, mZoomUpdate;
863     int mZoomInc;
864     bool mReturnZoomStatus;
865     static const int32_t ZOOM_STEPS [];
866 
867      //local copy
868     OMX_VERSIONTYPE mLocalVersionParam;
869 
870     unsigned int mPending3Asettings;
871     Mutex m3ASettingsUpdateLock;
872     Gen3A_settings mParameters3A;
873     const char *mPictureFormatFromClient;
874 
875     OMX_TI_CONFIG_3A_FACE_PRIORITY mFacePriority;
876     OMX_TI_CONFIG_3A_REGION_PRIORITY mRegionPriority;
877 
878     CameraParameters mParams;
879     CameraProperties::Properties* mCapabilities;
880     unsigned int mPictureRotation;
881     bool mWaitingForSnapshot;
882     int mSnapshotCount;
883     bool mCaptureConfigured;
884     unsigned int mPendingCaptureSettings;
885     OMX_TI_ANCILLARYDATATYPE* mCaptureAncillaryData;
886     OMX_TI_WHITEBALANCERESULTTYPE* mWhiteBalanceData;
887 
888     //Temporal bracketing management data
889     mutable Mutex mBracketingLock;
890     bool *mBracketingBuffersQueued;
891     int mBracketingBuffersQueuedCount;
892     int mLastBracetingBufferIdx;
893     bool mBracketingEnabled;
894     int mBracketingRange;
895 
896     bool mIternalRecordingHint;
897 
898     CameraParameters mParameters;
899     bool mOmxInitialized;
900     OMXCameraAdapterComponentContext mCameraAdapterParameters;
901     bool mFirstTimeInit;
902 
903     ///Semaphores used internally
904     Semaphore mInitSem;
905     Semaphore mFlushSem;
906     Semaphore mUsePreviewDataSem;
907     Semaphore mUsePreviewSem;
908     Semaphore mUseCaptureSem;
909     Semaphore mStartPreviewSem;
910     Semaphore mStopPreviewSem;
911     Semaphore mStartCaptureSem;
912     Semaphore mStopCaptureSem;
913     Semaphore mSwitchToLoadedSem;
914     Semaphore mSwitchToExecSem;
915 
916     mutable Mutex mStateSwitchLock;
917 
918     Vector<struct TIUTILS::Message *> mEventSignalQ;
919     Mutex mEventLock;
920 
921     OMX_STATETYPE mComponentState;
922 
923     bool mVnfEnabled;
924     bool mVstabEnabled;
925 
926     int mSensorOrientation;
927     int mDeviceOrientation;
928     bool mSensorOverclock;
929 
930     //Indicates if we should leave
931     //OMX_Executing state during
932     //stop-/startPreview
933     bool mOMXStateSwitch;
934 
935     int mFrameCount;
936     int mLastFrameCount;
937     unsigned int mIter;
938     nsecs_t mLastFPSTime;
939     Mutex mFrameCountMutex;
940     Condition mFirstFrameCondition;
941 
942     Mutex mDoAFMutex;
943     Condition mDoAFCond;
944 
945     size_t mSensorIndex;
946     CodingMode mCodingMode;
947 
948     // Time source delta of ducati & system time
949     OMX_TICKS mTimeSourceDelta;
950     bool onlyOnce;
951 
952     Semaphore mCaptureSem;
953     bool mCaptureSignalled;
954 
955     OMX_BOOL mUserSetExpLock;
956     OMX_BOOL mUserSetWbLock;
957 
958 };
959 }; //// namespace
960 #endif //OMX_CAMERA_ADAPTER_H
961 
962