1 /* 2 ** Copyright 2008, Google Inc. 3 ** Copyright (c) 2009-2012, The Linux Foundation. All rights reserved. 4 ** 5 ** Licensed under the Apache License, Version 2.0 (the "License"); 6 ** you may not use this file except in compliance with the License. 7 ** You may obtain a copy of the License at 8 ** 9 ** http://www.apache.org/licenses/LICENSE-2.0 10 ** 11 ** Unless required by applicable law or agreed to in writing, software 12 ** distributed under the License is distributed on an "AS IS" BASIS, 13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 ** See the License for the specific language governing permissions and 15 ** limitations under the License. 16 */ 17 18 #ifndef ANDROID_HARDWARE_QCAMERA_STREAM_H 19 #define ANDROID_HARDWARE_QCAMERA_STREAM_H 20 21 22 #include <utils/threads.h> 23 24 #include <binder/MemoryBase.h> 25 #include <binder/MemoryHeapBase.h> 26 #include <utils/threads.h> 27 28 #include "QCameraHWI.h" 29 #include "QCameraHWI_Mem.h" 30 #include "QCamera_Intf.h" 31 extern "C" { 32 #include <mm_camera_interface2.h> 33 34 #define DEFAULT_STREAM_WIDTH 320 35 #define DEFAULT_STREAM_HEIGHT 240 36 #define DEFAULT_LIVESHOT_WIDTH 2592 37 #define DEFAULT_LIVESHOT_HEIGHT 1944 38 39 #define MM_CAMERA_CH_PREVIEW_MASK (0x01 << MM_CAMERA_CH_PREVIEW) 40 #define MM_CAMERA_CH_VIDEO_MASK (0x01 << MM_CAMERA_CH_VIDEO) 41 #define MM_CAMERA_CH_SNAPSHOT_MASK (0x01 << MM_CAMERA_CH_SNAPSHOT) 42 43 } /* extern C*/ 44 45 46 47 typedef struct snap_hdr_record_t_ { 48 bool hdr_on; 49 int num_frame; 50 int num_raw_received; 51 52 /*in terms of 2^(n/6), e.g -6 means (1/2)x, while 12 is 4x*/ 53 int exp[MAX_HDR_EXP_FRAME_NUM]; 54 mm_camera_ch_data_buf_t *recvd_frame[MAX_HDR_EXP_FRAME_NUM]; 55 } snap_hdr_record_t; 56 57 58 namespace android { 59 60 class QCameraHardwareInterface; 61 62 class StreamQueue { 63 private: 64 Mutex mQueueLock; 65 Condition mQueueWait; 66 bool mInitialized; 67 68 //Vector<struct msm_frame *> mContainer; 69 Vector<void *> mContainer; 70 public: 71 StreamQueue(); 72 virtual ~StreamQueue(); 73 bool enqueue(void *element); 74 void flush(); 75 void* dequeue(); 76 void init(); 77 void deinit(); 78 bool isInitialized(); 79 bool isEmpty(); 80 }; 81 82 83 class QCameraStream { //: public virtual RefBase{ 84 85 public: 86 bool mInit; 87 bool mActive; 88 89 virtual status_t init(); 90 virtual status_t start(); 91 virtual void stop(); 92 virtual void release(); 93 94 status_t setFormat(uint8_t ch_type_mask, cam_format_t previewFmt); 95 status_t setMode(int enable); 96 97 virtual void setHALCameraControl(QCameraHardwareInterface* ctrl); 98 99 //static status_t openChannel(mm_camera_t *, mm_camera_channel_type_t ch_type); 100 virtual status_t initChannel(int cameraId, uint32_t ch_type_mask); 101 virtual status_t deinitChannel(int cameraId, mm_camera_channel_type_t ch_type); 102 virtual void releaseRecordingFrame(const void *opaque) 103 { 104 ; 105 } 106 #if 0 // mzhu 107 virtual status_t getBufferInfo(sp<IMemory>& Frame, size_t *alignedSize) 108 { 109 return NO_ERROR; 110 } 111 #endif // mzhu 112 virtual void prepareHardware() 113 { 114 ; 115 } 116 virtual sp<IMemoryHeap> getHeap() const{return NULL;} 117 virtual status_t initDisplayBuffers(){return NO_ERROR;} 118 virtual status_t initPreviewOnlyBuffers(){return NO_ERROR;} 119 virtual sp<IMemoryHeap> getRawHeap() const {return NULL;} 120 virtual void *getLastQueuedFrame(void){return NULL;} 121 virtual status_t takePictureZSL(void){return NO_ERROR;} 122 virtual status_t takeLiveSnapshot(){return NO_ERROR;} 123 virtual status_t takePictureLiveshot(mm_camera_ch_data_buf_t* recvd_frame){return NO_ERROR;} 124 virtual void setModeLiveSnapshot(bool){;} 125 virtual status_t initSnapshotBuffers(cam_ctrl_dimension_t *dim, 126 int num_of_buf){return NO_ERROR;} 127 128 virtual void setFullSizeLiveshot(bool){}; 129 /* Set the ANativeWindow */ 130 virtual int setPreviewWindow(preview_stream_ops_t* window) {return NO_ERROR;} 131 virtual void notifyROIEvent(fd_roi_t roi) {;} 132 virtual void notifyWDenoiseEvent(cam_ctrl_status_t status, void * cookie) {}; 133 virtual void resetSnapshotCounters(void ){}; 134 virtual void InitHdrInfoForSnapshot(bool HDR_on, int number_frames, int *exp ) {}; 135 virtual void notifyHdrEvent(cam_ctrl_status_t status, void * cookie) {}; 136 137 QCameraStream(); 138 QCameraStream(int, camera_mode_t); 139 virtual ~QCameraStream(); 140 QCameraHardwareInterface* mHalCamCtrl; 141 mm_camera_ch_crop_t mCrop; 142 143 int mCameraId; 144 camera_mode_t myMode; 145 146 mutable Mutex mStopCallbackLock; 147 mutable Mutex mPreviewFrameLock; 148 int mSnapshotDataCallingBack; 149 int mFreeSnapshotBufAfterDataCb; 150 private: 151 StreamQueue mBusyQueue; 152 StreamQueue mFreeQueue; 153 public: 154 friend void liveshot_callback(mm_camera_ch_data_buf_t *frame,void *user_data); 155 }; 156 157 /* 158 * Record Class 159 */ 160 class QCameraStream_record : public QCameraStream { 161 public: 162 status_t init(); 163 status_t start() ; 164 void stop() ; 165 void release() ; 166 167 static QCameraStream* createInstance(int cameraId, camera_mode_t); 168 static void deleteInstance(QCameraStream *p); 169 170 QCameraStream_record() {}; 171 virtual ~QCameraStream_record(); 172 173 status_t processRecordFrame(void *data); 174 status_t initEncodeBuffers(); 175 status_t getBufferInfo(sp<IMemory>& Frame, size_t *alignedSize); 176 //sp<IMemoryHeap> getHeap() const; 177 178 void releaseRecordingFrame(const void *opaque); 179 void debugShowVideoFPS() const; 180 181 status_t takeLiveSnapshot(); 182 private: 183 QCameraStream_record(int, camera_mode_t); 184 void releaseEncodeBuffer(); 185 186 cam_ctrl_dimension_t dim; 187 bool mDebugFps; 188 189 mm_camera_reg_buf_t mRecordBuf; 190 //int record_frame_len; 191 //static const int maxFrameCnt = 16; 192 //camera_memory_t *mCameraMemoryPtr[maxFrameCnt]; 193 //int mNumRecordFrames; 194 //sp<PmemPool> mRecordHeap[maxFrameCnt]; 195 struct msm_frame *recordframes; 196 //uint32_t record_offset[VIDEO_BUFFER_COUNT]; 197 mm_camera_ch_data_buf_t mRecordedFrames[MM_CAMERA_MAX_NUM_FRAMES]; 198 //Mutex mRecordFreeQueueLock; 199 //Vector<mm_camera_ch_data_buf_t> mRecordFreeQueue; 200 201 int mJpegMaxSize; 202 QCameraStream *mStreamSnap; 203 204 }; 205 206 class QCameraStream_preview : public QCameraStream { 207 public: 208 status_t init(); 209 status_t start() ; 210 void stop() ; 211 void release() ; 212 213 static QCameraStream* createInstance(int, camera_mode_t); 214 static void deleteInstance(QCameraStream *p); 215 216 QCameraStream_preview() {}; 217 virtual ~QCameraStream_preview(); 218 void *getLastQueuedFrame(void); 219 /*init preview buffers with display case*/ 220 status_t initDisplayBuffers(); 221 /*init preview buffers without display case*/ 222 status_t initPreviewOnlyBuffers(); 223 224 status_t processPreviewFrame(mm_camera_ch_data_buf_t *frame); 225 226 /*init preview buffers with display case*/ 227 status_t processPreviewFrameWithDisplay(mm_camera_ch_data_buf_t *frame); 228 /*init preview buffers without display case*/ 229 status_t processPreviewFrameWithOutDisplay(mm_camera_ch_data_buf_t *frame); 230 231 int setPreviewWindow(preview_stream_ops_t* window); 232 void notifyROIEvent(fd_roi_t roi); 233 friend class QCameraHardwareInterface; 234 235 private: 236 QCameraStream_preview(int cameraId, camera_mode_t); 237 /*allocate and free buffers with display case*/ 238 status_t getBufferFromSurface(); 239 status_t putBufferToSurface(); 240 241 /*allocate and free buffers without display case*/ 242 status_t getBufferNoDisplay(); 243 status_t freeBufferNoDisplay(); 244 245 void dumpFrameToFile(struct msm_frame* newFrame); 246 bool mFirstFrameRcvd; 247 248 int8_t my_id; 249 mm_camera_op_mode_type_t op_mode; 250 cam_ctrl_dimension_t dim; 251 struct msm_frame *mLastQueuedFrame; 252 mm_camera_reg_buf_t mDisplayBuf; 253 mm_cameara_stream_buf_t mDisplayStreamBuf; 254 Mutex mDisplayLock; 255 preview_stream_ops_t *mPreviewWindow; 256 static const int kPreviewBufferCount = PREVIEW_BUFFER_COUNT; 257 mm_camera_ch_data_buf_t mNotifyBuffer[16]; 258 int8_t mNumFDRcvd; 259 int mVFEOutputs; 260 int mHFRFrameCnt; 261 int mHFRFrameSkip; 262 }; 263 264 /* Snapshot Class - handle data flow*/ 265 class QCameraStream_Snapshot : public QCameraStream { 266 public: 267 status_t init(); 268 status_t start(); 269 void stop(); 270 void release(); 271 void prepareHardware(); 272 static QCameraStream* createInstance(int cameraId, camera_mode_t); 273 static void deleteInstance(QCameraStream *p); 274 275 status_t takePictureZSL(void); 276 status_t takePictureLiveshot(mm_camera_ch_data_buf_t* recvd_frame); 277 status_t receiveRawPicture(mm_camera_ch_data_buf_t* recvd_frame); 278 void receiveCompleteJpegPicture(jpeg_event_t event); 279 void jpegErrorHandler(jpeg_event_t event); 280 void receiveJpegFragment(uint8_t *ptr, uint32_t size); 281 void deInitBuffer(void); 282 sp<IMemoryHeap> getRawHeap() const; 283 int getSnapshotState(); 284 /*Temp: to be removed once event handling is enabled in mm-camera*/ 285 void runSnapshotThread(void *data); 286 bool isZSLMode(); 287 void setFullSizeLiveshot(bool); 288 void notifyWDenoiseEvent(cam_ctrl_status_t status, void * cookie); 289 friend void liveshot_callback(mm_camera_ch_data_buf_t *frame,void *user_data); 290 void resetSnapshotCounters(void ); 291 void InitHdrInfoForSnapshot(bool HDR_on, int number_frames, int *exp ); 292 void notifyHdrEvent(cam_ctrl_status_t status, void * cookie); 293 bool getSnapJpegCbState(void); 294 void setSnapJpegCbState(bool state); 295 296 private: 297 QCameraStream_Snapshot(int, camera_mode_t); 298 virtual ~QCameraStream_Snapshot(); 299 300 /* snapshot related private members */ 301 status_t initJPEGSnapshot(int num_of_snapshots); 302 status_t initRawSnapshot(int num_of_snapshots); 303 status_t initZSLSnapshot(void); 304 status_t initFullLiveshot(void); 305 status_t cancelPicture(); 306 void notifyShutter(common_crop_t *crop, 307 bool play_shutter_sound); 308 status_t initSnapshotBuffers(cam_ctrl_dimension_t *dim, 309 int num_of_buf); 310 status_t initRawSnapshotBuffers(cam_ctrl_dimension_t *dim, 311 int num_of_buf); 312 status_t deinitRawSnapshotBuffers(void); 313 status_t deinitSnapshotBuffers(void); 314 status_t initRawSnapshotChannel(cam_ctrl_dimension_t* dim, 315 int num_snapshots); 316 status_t initSnapshotFormat(cam_ctrl_dimension_t *dim); 317 status_t takePictureRaw(void); 318 status_t takePictureJPEG(void); 319 status_t startStreamZSL(void); 320 void deinitSnapshotChannel(mm_camera_channel_type_t); 321 status_t configSnapshotDimension(cam_ctrl_dimension_t* dim); 322 status_t encodeData(mm_camera_ch_data_buf_t* recvd_frame, 323 common_crop_t *crop_info, 324 int frame_len, 325 bool enqueued); 326 status_t encodeDisplayAndSave(mm_camera_ch_data_buf_t* recvd_frame, 327 bool enqueued); 328 status_t setZSLChannelAttribute(void); 329 void handleError(); 330 void setSnapshotState(int state); 331 void setModeLiveSnapshot(bool); 332 bool isLiveSnapshot(void); 333 void stopPolling(void); 334 bool isFullSizeLiveshot(void); 335 status_t doWaveletDenoise(mm_camera_ch_data_buf_t* frame); 336 status_t sendWDenoiseStartMsg(mm_camera_ch_data_buf_t * frame); 337 void lauchNextWDenoiseFromQueue(); 338 status_t doHdrProcessing( ); 339 340 /* Member variables */ 341 342 int mSnapshotFormat; 343 int mPictureWidth; 344 int mPictureHeight; 345 cam_format_t mPictureFormat; 346 int mPostviewWidth; 347 int mPostviewHeight; 348 int mThumbnailWidth; 349 int mThumbnailHeight; 350 cam_format_t mThumbnailFormat; 351 int mJpegOffset; 352 int mSnapshotState; 353 int mNumOfSnapshot; 354 int mNumOfRecievedJPEG; 355 bool mModeLiveSnapshot; 356 bool mBurstModeFlag; 357 int mActualPictureWidth; 358 int mActualPictureHeight; 359 bool mJpegDownscaling; 360 sp<AshmemPool> mJpegHeap; 361 /*TBD:Bikas: This is defined in HWI too.*/ 362 #ifdef USE_ION 363 sp<IonPool> mDisplayHeap; 364 sp<IonPool> mPostviewHeap; 365 #else 366 sp<PmemPool> mDisplayHeap; 367 sp<PmemPool> mPostviewHeap; 368 #endif 369 mm_camera_ch_data_buf_t *mCurrentFrameEncoded; 370 mm_cameara_stream_buf_t mSnapshotStreamBuf; 371 mm_cameara_stream_buf_t mPostviewStreamBuf; 372 StreamQueue mSnapshotQueue; 373 static const int mMaxSnapshotBufferCount = 16; 374 int mSnapshotBufferNum; 375 int mMainfd[mMaxSnapshotBufferCount]; 376 int mThumbfd[mMaxSnapshotBufferCount]; 377 int mMainSize; 378 int mThumbSize; 379 camera_memory_t *mCameraMemoryPtrMain[mMaxSnapshotBufferCount]; 380 camera_memory_t *mCameraMemoryPtrThumb[mMaxSnapshotBufferCount]; 381 int mJpegSessionId; 382 int dump_fd; 383 bool mFullLiveshot; 384 StreamQueue mWDNQueue; // queue to hold frames while one frame is sent out for WDN 385 bool mIsDoingWDN; // flag to indicate if WDN is going on (one frame is sent out for WDN) 386 bool mDropThumbnail; 387 int mJpegQuality; 388 snap_hdr_record_t mHdrInfo; 389 int hdrRawCount; 390 int hdrJpegCount; 391 }; // QCameraStream_Snapshot 392 393 394 }; // namespace android 395 396 #endif 397