1 /*
2 ** Copyright 2008, Google Inc.
3 ** Copyright (c) 2009-2012,2015, 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);
releaseRecordingFrame(const void * opaque)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
prepareHardware()112     virtual void prepareHardware()
113     {
114       ;
115     }
getHeap()116     virtual sp<IMemoryHeap> getHeap() const{return NULL;}
initDisplayBuffers()117     virtual status_t    initDisplayBuffers(){return NO_ERROR;}
initPreviewOnlyBuffers()118     virtual status_t initPreviewOnlyBuffers(){return NO_ERROR;}
getRawHeap()119     virtual sp<IMemoryHeap> getRawHeap() const {return NULL;}
getLastQueuedFrame(void)120     virtual void *getLastQueuedFrame(void){return NULL;}
takePictureZSL(void)121     virtual status_t takePictureZSL(void){return NO_ERROR;}
takeLiveSnapshot()122     virtual status_t takeLiveSnapshot(){return NO_ERROR;}
takePictureLiveshot(mm_camera_ch_data_buf_t * recvd_frame)123     virtual status_t takePictureLiveshot(mm_camera_ch_data_buf_t* recvd_frame){return NO_ERROR;}
setModeLiveSnapshot(bool)124 	virtual void setModeLiveSnapshot(bool){;}
initSnapshotBuffers(cam_ctrl_dimension_t * dim,int num_of_buf)125     virtual status_t initSnapshotBuffers(cam_ctrl_dimension_t *dim,
126                                  int num_of_buf){return NO_ERROR;}
127 
setFullSizeLiveshot(bool)128     virtual void setFullSizeLiveshot(bool){};
129     /* Set the ANativeWindow */
setPreviewWindow(preview_stream_ops_t * window)130     virtual int setPreviewWindow(preview_stream_ops_t* window) {return NO_ERROR;}
notifyROIEvent(fd_roi_t roi)131     virtual void notifyROIEvent(fd_roi_t roi) {;}
notifyWDenoiseEvent(cam_ctrl_status_t status,void * cookie)132     virtual void notifyWDenoiseEvent(cam_ctrl_status_t status, void * cookie) {};
resetSnapshotCounters(void)133     virtual void resetSnapshotCounters(void ){};
InitHdrInfoForSnapshot(bool HDR_on,int number_frames,int * exp)134     virtual void InitHdrInfoForSnapshot(bool HDR_on, int number_frames, int *exp ) {};
notifyHdrEvent(cam_ctrl_status_t status,void * cookie)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 
QCameraStream_record()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 
QCameraStream_preview()216     QCameraStream_preview() {};
217     virtual             ~QCameraStream_preview();
218     void *getLastQueuedFrame(void);
219     /*init preview buffers with display case*/
220     status_t initDisplayBuffers();
221 
222     status_t processPreviewFrame(mm_camera_ch_data_buf_t *frame);
223 
224     /*init preview buffers with display case*/
225     status_t processPreviewFrameWithDisplay(mm_camera_ch_data_buf_t *frame);
226 
227     int setPreviewWindow(preview_stream_ops_t* window);
228     void notifyROIEvent(fd_roi_t roi);
229     friend class QCameraHardwareInterface;
230 
231 private:
232     QCameraStream_preview(int cameraId, camera_mode_t);
233     /*allocate and free buffers with display case*/
234     status_t                 getBufferFromSurface();
235     status_t                 putBufferToSurface();
236 
237     void                     dumpFrameToFile(struct msm_frame* newFrame);
238     bool                     mFirstFrameRcvd;
239 
240     int8_t                   my_id;
241     mm_camera_op_mode_type_t op_mode;
242     cam_ctrl_dimension_t     dim;
243     struct msm_frame        *mLastQueuedFrame;
244     mm_camera_reg_buf_t      mDisplayBuf;
245     mm_cameara_stream_buf_t  mDisplayStreamBuf;
246     Mutex                   mDisplayLock;
247     preview_stream_ops_t   *mPreviewWindow;
248     static const int        kPreviewBufferCount = PREVIEW_BUFFER_COUNT;
249     int8_t                  mNumFDRcvd;
250     int                     mVFEOutputs;
251     int                     mHFRFrameCnt;
252     int                     mHFRFrameSkip;
253 };
254 
255 /* Snapshot Class - handle data flow*/
256 class QCameraStream_Snapshot : public QCameraStream {
257 public:
258     status_t    init();
259     status_t    start();
260     void        stop();
261     void        release();
262     void        prepareHardware();
263     static QCameraStream* createInstance(int cameraId, camera_mode_t);
264     static void deleteInstance(QCameraStream *p);
265 
266     status_t takePictureZSL(void);
267     status_t takePictureLiveshot(mm_camera_ch_data_buf_t* recvd_frame);
268     status_t receiveRawPicture(mm_camera_ch_data_buf_t* recvd_frame);
269     void receiveCompleteJpegPicture(jpeg_event_t event);
270 	void jpegErrorHandler(jpeg_event_t event);
271     void receiveJpegFragment(uint8_t *ptr, uint32_t size);
272     void deInitBuffer(void);
273     sp<IMemoryHeap> getRawHeap() const;
274     int getSnapshotState();
275     /*Temp: to be removed once event handling is enabled in mm-camera*/
276     void runSnapshotThread(void *data);
277     bool isZSLMode();
278     void setFullSizeLiveshot(bool);
279     void notifyWDenoiseEvent(cam_ctrl_status_t status, void * cookie);
280     friend void liveshot_callback(mm_camera_ch_data_buf_t *frame,void *user_data);
281     void resetSnapshotCounters(void );
282     void InitHdrInfoForSnapshot(bool HDR_on, int number_frames, int *exp );
283     void notifyHdrEvent(cam_ctrl_status_t status, void * cookie);
284     bool getSnapJpegCbState(void);
285     void setSnapJpegCbState(bool state);
286 
287 private:
288     QCameraStream_Snapshot(int, camera_mode_t);
289     virtual ~QCameraStream_Snapshot();
290 
291     /* snapshot related private members */
292     status_t initJPEGSnapshot(int num_of_snapshots);
293     status_t initRawSnapshot(int num_of_snapshots);
294     status_t initZSLSnapshot(void);
295     status_t initFullLiveshot(void);
296 	status_t cancelPicture();
297     void notifyShutter(common_crop_t *crop,
298                        bool play_shutter_sound);
299     status_t initSnapshotBuffers(cam_ctrl_dimension_t *dim,
300                                  int num_of_buf);
301     status_t initRawSnapshotBuffers(cam_ctrl_dimension_t *dim,
302                                     int num_of_buf);
303     status_t deinitRawSnapshotBuffers(void);
304     status_t deinitSnapshotBuffers(void);
305     status_t initRawSnapshotChannel(cam_ctrl_dimension_t* dim,
306                                     int num_snapshots);
307     status_t initSnapshotFormat(cam_ctrl_dimension_t *dim);
308     status_t takePictureRaw(void);
309     status_t takePictureJPEG(void);
310     status_t startStreamZSL(void);
311     void deinitSnapshotChannel(mm_camera_channel_type_t);
312     status_t configSnapshotDimension(cam_ctrl_dimension_t* dim);
313     status_t encodeData(mm_camera_ch_data_buf_t* recvd_frame,
314                         common_crop_t *crop_info,
315                         int frame_len,
316                         bool enqueued);
317     status_t encodeDisplayAndSave(mm_camera_ch_data_buf_t* recvd_frame,
318                                   bool enqueued);
319     status_t setZSLChannelAttribute(void);
320     void handleError();
321     void setSnapshotState(int state);
322     void setModeLiveSnapshot(bool);
323     bool isLiveSnapshot(void);
324     void stopPolling(void);
325     bool isFullSizeLiveshot(void);
326     status_t doWaveletDenoise(mm_camera_ch_data_buf_t* frame);
327     status_t sendWDenoiseStartMsg(mm_camera_ch_data_buf_t * frame);
328     void lauchNextWDenoiseFromQueue();
329     status_t doHdrProcessing( );
330 
331     /* Member variables */
332 
333     int mSnapshotFormat;
334     int mPictureWidth;
335     int mPictureHeight;
336     cam_format_t mPictureFormat;
337     int mPostviewWidth;
338     int mPostviewHeight;
339     int mThumbnailWidth;
340     int mThumbnailHeight;
341     cam_format_t mThumbnailFormat;
342 	int mJpegOffset;
343     int mSnapshotState;
344     int mNumOfSnapshot;
345 	int mNumOfRecievedJPEG;
346     bool mModeLiveSnapshot;
347     bool mBurstModeFlag;
348 	int mActualPictureWidth;
349     int mActualPictureHeight;
350     bool mJpegDownscaling;
351     sp<AshmemPool> mJpegHeap;
352     /*TBD:Bikas: This is defined in HWI too.*/
353 #ifdef USE_ION
354     sp<IonPool>  mDisplayHeap;
355     sp<IonPool>  mPostviewHeap;
356 #else
357     sp<PmemPool>  mDisplayHeap;
358     sp<PmemPool>  mPostviewHeap;
359 #endif
360     mm_camera_ch_data_buf_t *mCurrentFrameEncoded;
361     mm_cameara_stream_buf_t mSnapshotStreamBuf;
362     mm_cameara_stream_buf_t mPostviewStreamBuf;
363     StreamQueue             mSnapshotQueue;
364     static const int        mMaxSnapshotBufferCount = 16;
365     int                     mSnapshotBufferNum;
366     int                     mMainfd[mMaxSnapshotBufferCount];
367     int                     mThumbfd[mMaxSnapshotBufferCount];
368     int                     mMainSize;
369     int                     mThumbSize;
370 	camera_memory_t        *mCameraMemoryPtrMain[mMaxSnapshotBufferCount];
371 	camera_memory_t        *mCameraMemoryPtrThumb[mMaxSnapshotBufferCount];
372     int                     mJpegSessionId;
373 	int                     dump_fd;
374     bool mFullLiveshot;
375     StreamQueue             mWDNQueue; // queue to hold frames while one frame is sent out for WDN
376     bool                    mIsDoingWDN; // flag to indicate if WDN is going on (one frame is sent out for WDN)
377 	bool                    mDropThumbnail;
378 	int                     mJpegQuality;
379     snap_hdr_record_t       mHdrInfo;
380     int hdrRawCount;
381     int hdrJpegCount;
382 }; // QCameraStream_Snapshot
383 
384 
385 }; // namespace android
386 
387 #endif
388