Home | History | Annotate | Download | only in camera
      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 private:
    148    StreamQueue mBusyQueue;
    149    StreamQueue mFreeQueue;
    150 public:
    151      friend void liveshot_callback(mm_camera_ch_data_buf_t *frame,void *user_data);
    152 };
    153 
    154 /*
    155 *   Record Class
    156 */
    157 class QCameraStream_record : public QCameraStream {
    158 public:
    159   status_t    init();
    160   status_t    start() ;
    161   void        stop()  ;
    162   void        release() ;
    163 
    164   static QCameraStream*  createInstance(int cameraId, camera_mode_t);
    165   static void            deleteInstance(QCameraStream *p);
    166 
    167   QCameraStream_record() {};
    168   virtual             ~QCameraStream_record();
    169 
    170   status_t processRecordFrame(void *data);
    171   status_t initEncodeBuffers();
    172   status_t getBufferInfo(sp<IMemory>& Frame, size_t *alignedSize);
    173   //sp<IMemoryHeap> getHeap() const;
    174 
    175   void releaseRecordingFrame(const void *opaque);
    176   void debugShowVideoFPS() const;
    177 
    178   status_t takeLiveSnapshot();
    179 private:
    180   QCameraStream_record(int, camera_mode_t);
    181   void releaseEncodeBuffer();
    182 
    183   cam_ctrl_dimension_t             dim;
    184   bool mDebugFps;
    185 
    186   mm_camera_reg_buf_t              mRecordBuf;
    187   //int                              record_frame_len;
    188   //static const int                 maxFrameCnt = 16;
    189   //camera_memory_t                 *mCameraMemoryPtr[maxFrameCnt];
    190   //int                              mNumRecordFrames;
    191   //sp<PmemPool>                     mRecordHeap[maxFrameCnt];
    192   struct msm_frame                *recordframes;
    193   //uint32_t                         record_offset[VIDEO_BUFFER_COUNT];
    194   mm_camera_ch_data_buf_t          mRecordedFrames[MM_CAMERA_MAX_NUM_FRAMES];
    195   //Mutex                            mRecordFreeQueueLock;
    196   //Vector<mm_camera_ch_data_buf_t>  mRecordFreeQueue;
    197 
    198   int mJpegMaxSize;
    199   QCameraStream *mStreamSnap;
    200 
    201 };
    202 
    203 class QCameraStream_preview : public QCameraStream {
    204 public:
    205     status_t    init();
    206     status_t    start() ;
    207     void        stop()  ;
    208     void        release() ;
    209 
    210     static QCameraStream*  createInstance(int, camera_mode_t);
    211     static void            deleteInstance(QCameraStream *p);
    212 
    213     QCameraStream_preview() {};
    214     virtual             ~QCameraStream_preview();
    215     void *getLastQueuedFrame(void);
    216     /*init preview buffers with display case*/
    217     status_t initDisplayBuffers();
    218     /*init preview buffers without display case*/
    219     status_t initPreviewOnlyBuffers();
    220 
    221     status_t processPreviewFrame(mm_camera_ch_data_buf_t *frame);
    222 
    223     /*init preview buffers with display case*/
    224     status_t processPreviewFrameWithDisplay(mm_camera_ch_data_buf_t *frame);
    225     /*init preview buffers without display case*/
    226     status_t processPreviewFrameWithOutDisplay(mm_camera_ch_data_buf_t *frame);
    227 
    228     int setPreviewWindow(preview_stream_ops_t* window);
    229     void notifyROIEvent(fd_roi_t roi);
    230     friend class QCameraHardwareInterface;
    231 
    232 private:
    233     QCameraStream_preview(int cameraId, camera_mode_t);
    234     /*allocate and free buffers with display case*/
    235     status_t                 getBufferFromSurface();
    236     status_t                 putBufferToSurface();
    237 
    238     /*allocate and free buffers without display case*/
    239     status_t                 getBufferNoDisplay();
    240     status_t                 freeBufferNoDisplay();
    241 
    242     void                     dumpFrameToFile(struct msm_frame* newFrame);
    243     bool                     mFirstFrameRcvd;
    244 
    245     int8_t                   my_id;
    246     mm_camera_op_mode_type_t op_mode;
    247     cam_ctrl_dimension_t     dim;
    248     struct msm_frame        *mLastQueuedFrame;
    249     mm_camera_reg_buf_t      mDisplayBuf;
    250     mm_cameara_stream_buf_t  mDisplayStreamBuf;
    251     Mutex                   mDisplayLock;
    252     preview_stream_ops_t   *mPreviewWindow;
    253     static const int        kPreviewBufferCount = PREVIEW_BUFFER_COUNT;
    254     mm_camera_ch_data_buf_t mNotifyBuffer[16];
    255     int8_t                  mNumFDRcvd;
    256     int                     mVFEOutputs;
    257     int                     mHFRFrameCnt;
    258     int                     mHFRFrameSkip;
    259 };
    260 
    261 /* Snapshot Class - handle data flow*/
    262 class QCameraStream_Snapshot : public QCameraStream {
    263 public:
    264     status_t    init();
    265     status_t    start();
    266     void        stop();
    267     void        release();
    268     void        prepareHardware();
    269     static QCameraStream* createInstance(int cameraId, camera_mode_t);
    270     static void deleteInstance(QCameraStream *p);
    271 
    272     status_t takePictureZSL(void);
    273     status_t takePictureLiveshot(mm_camera_ch_data_buf_t* recvd_frame);
    274     status_t receiveRawPicture(mm_camera_ch_data_buf_t* recvd_frame);
    275     void receiveCompleteJpegPicture(jpeg_event_t event);
    276 	void jpegErrorHandler(jpeg_event_t event);
    277     void receiveJpegFragment(uint8_t *ptr, uint32_t size);
    278     void deInitBuffer(void);
    279     sp<IMemoryHeap> getRawHeap() const;
    280     int getSnapshotState();
    281     /*Temp: to be removed once event handling is enabled in mm-camera*/
    282     void runSnapshotThread(void *data);
    283     bool isZSLMode();
    284     void setFullSizeLiveshot(bool);
    285     void notifyWDenoiseEvent(cam_ctrl_status_t status, void * cookie);
    286     friend void liveshot_callback(mm_camera_ch_data_buf_t *frame,void *user_data);
    287     void resetSnapshotCounters(void );
    288     void InitHdrInfoForSnapshot(bool HDR_on, int number_frames, int *exp );
    289     void notifyHdrEvent(cam_ctrl_status_t status, void * cookie);
    290 
    291 private:
    292     QCameraStream_Snapshot(int, camera_mode_t);
    293     virtual ~QCameraStream_Snapshot();
    294 
    295     /* snapshot related private members */
    296     status_t initJPEGSnapshot(int num_of_snapshots);
    297     status_t initRawSnapshot(int num_of_snapshots);
    298     status_t initZSLSnapshot(void);
    299     status_t initFullLiveshot(void);
    300 	status_t cancelPicture();
    301     void notifyShutter(common_crop_t *crop,
    302                        bool play_shutter_sound);
    303     status_t initSnapshotBuffers(cam_ctrl_dimension_t *dim,
    304                                  int num_of_buf);
    305     status_t initRawSnapshotBuffers(cam_ctrl_dimension_t *dim,
    306                                     int num_of_buf);
    307     status_t deinitRawSnapshotBuffers(void);
    308     status_t deinitSnapshotBuffers(void);
    309     status_t initRawSnapshotChannel(cam_ctrl_dimension_t* dim,
    310                                     int num_snapshots);
    311     status_t initSnapshotFormat(cam_ctrl_dimension_t *dim);
    312     status_t takePictureRaw(void);
    313     status_t takePictureJPEG(void);
    314     status_t startStreamZSL(void);
    315     void deinitSnapshotChannel(mm_camera_channel_type_t);
    316     status_t configSnapshotDimension(cam_ctrl_dimension_t* dim);
    317     status_t encodeData(mm_camera_ch_data_buf_t* recvd_frame,
    318                         common_crop_t *crop_info,
    319                         int frame_len,
    320                         bool enqueued);
    321     status_t encodeDisplayAndSave(mm_camera_ch_data_buf_t* recvd_frame,
    322                                   bool enqueued);
    323     status_t setZSLChannelAttribute(void);
    324     void handleError();
    325     void setSnapshotState(int state);
    326     void setModeLiveSnapshot(bool);
    327     bool isLiveSnapshot(void);
    328     void stopPolling(void);
    329     bool isFullSizeLiveshot(void);
    330     status_t doWaveletDenoise(mm_camera_ch_data_buf_t* frame);
    331     status_t sendWDenoiseStartMsg(mm_camera_ch_data_buf_t * frame);
    332     void lauchNextWDenoiseFromQueue();
    333     status_t doHdrProcessing( );
    334 
    335     /* Member variables */
    336 
    337     int mSnapshotFormat;
    338     int mPictureWidth;
    339     int mPictureHeight;
    340     cam_format_t mPictureFormat;
    341     int mPostviewWidth;
    342     int mPostviewHeight;
    343     int mThumbnailWidth;
    344     int mThumbnailHeight;
    345     cam_format_t mThumbnailFormat;
    346 	int mJpegOffset;
    347     int mSnapshotState;
    348     int mNumOfSnapshot;
    349 	int mNumOfRecievedJPEG;
    350     bool mModeLiveSnapshot;
    351     bool mBurstModeFlag;
    352 	int mActualPictureWidth;
    353     int mActualPictureHeight;
    354     bool mJpegDownscaling;
    355     sp<AshmemPool> mJpegHeap;
    356     /*TBD:Bikas: This is defined in HWI too.*/
    357 #ifdef USE_ION
    358     sp<IonPool>  mDisplayHeap;
    359     sp<IonPool>  mPostviewHeap;
    360 #else
    361     sp<PmemPool>  mDisplayHeap;
    362     sp<PmemPool>  mPostviewHeap;
    363 #endif
    364     mm_camera_ch_data_buf_t *mCurrentFrameEncoded;
    365     mm_cameara_stream_buf_t mSnapshotStreamBuf;
    366     mm_cameara_stream_buf_t mPostviewStreamBuf;
    367     StreamQueue             mSnapshotQueue;
    368     static const int        mMaxSnapshotBufferCount = 16;
    369     int                     mSnapshotBufferNum;
    370     int                     mMainfd[mMaxSnapshotBufferCount];
    371     int                     mThumbfd[mMaxSnapshotBufferCount];
    372     int                     mMainSize;
    373     int                     mThumbSize;
    374 	camera_memory_t        *mCameraMemoryPtrMain[mMaxSnapshotBufferCount];
    375 	camera_memory_t        *mCameraMemoryPtrThumb[mMaxSnapshotBufferCount];
    376     int                     mJpegSessionId;
    377 	int                     dump_fd;
    378     bool mFullLiveshot;
    379     StreamQueue             mWDNQueue; // queue to hold frames while one frame is sent out for WDN
    380     bool                    mIsDoingWDN; // flag to indicate if WDN is going on (one frame is sent out for WDN)
    381 	bool                    mDropThumbnail;
    382 	int                     mJpegQuality;
    383     snap_hdr_record_t       mHdrInfo;
    384     int hdrRawCount;
    385     int hdrJpegCount;
    386 }; // QCameraStream_Snapshot
    387 
    388 
    389 }; // namespace android
    390 
    391 #endif
    392