1 /* Copyright (c) 2012-2016, The Linux Foundation. All rights reserved.
2  *
3  * Redistribution and use in source and binary forms, with or without
4  * modification, are permitted provided that the following conditions are
5  * met:
6  *     * Redistributions of source code must retain the above copyright
7  *       notice, this list of conditions and the following disclaimer.
8  *     * Redistributions in binary form must reproduce the above
9  *       copyright notice, this list of conditions and the following
10  *       disclaimer in the documentation and/or other materials provided
11  *       with the distribution.
12  *     * Neither the name of The Linux Foundation nor the names of its
13  *       contributors may be used to endorse or promote products derived
14  *       from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  */
29 
30 #ifndef __QCAMERA_POSTPROC_H__
31 #define __QCAMERA_POSTPROC_H__
32 
33 // Camera dependencies
34 #include "QCamera2HWI.h"
35 
36 extern "C" {
37 #include "mm_camera_interface.h"
38 #include "mm_jpeg_interface.h"
39 }
40 
41 #define MAX_JPEG_BURST 2
42 #define HAL_PP_NUM_BUFS 2
43 #define CAM_PP_CHANNEL_MAX 8
44 
45 namespace qcamera {
46 
47 class QCameraExif;
48 class QCamera2HardwareInterface;
49 class QCameraHALPP;
50 
51 typedef struct {
52     uint32_t jobId;                  // job ID
53     uint32_t client_hdl;             // handle of jpeg client (obtained when open jpeg)
54     mm_camera_super_buf_t *src_frame;// source frame (need to be returned back to kernel
55                                      //after done)
56     mm_camera_super_buf_t *src_reproc_frame; // original source
57                                              //frame for reproc if not NULL
58     metadata_buffer_t *metadata;     // source frame metadata
59     bool reproc_frame_release;       // false release original buffer, true don't release it
60     mm_camera_buf_def_t *src_reproc_bufs;
61     QCameraExif *pJpegExifObj;
62     uint8_t offline_buffer;
63     mm_camera_buf_def_t *offline_reproc_buf; //HAL processed buffer
64     bool halPPAllocatedBuf;            // true if src frame buffer is allocated by HAL PP block
65     mm_camera_buf_def_t *hal_pp_bufs;  // bufs allocates for HAL PP
66     QCameraHeapMemory *snapshot_heap; // heap memory of snapshot buffer
67     QCameraHeapMemory *metadata_heap; // heap memory of metadata buffer
68 } qcamera_jpeg_data_t;
69 
70 
71 typedef struct {
72     int8_t reprocCount;
73     mm_camera_super_buf_t *src_frame;    // source frame that needs post process
74     mm_camera_super_buf_t *src_reproc_frame;// source frame (need to be
75                                             //returned back to kernel after done)
76 }qcamera_pp_request_t;
77 
78 typedef struct {
79     uint32_t jobId;                  // job ID
80     int8_t reprocCount;              //Current pass count
81     int8_t ppChannelIndex;           //Reprocess channel object index
82     mm_camera_super_buf_t *src_frame;// source frame
83     bool reproc_frame_release;       // false release original buffer
84                                      // true don't release it
85     mm_camera_buf_def_t *src_reproc_bufs;
86     mm_camera_super_buf_t *src_reproc_frame;// source frame (need to be
87                                             //returned back to kernel after done)
88     uint8_t offline_buffer;
89     mm_camera_buf_def_t *offline_reproc_buf; //HAL processed buffer
90 } qcamera_pp_data_t;
91 
92 typedef struct {
93     mm_camera_super_buf_t *frame; // source frame
94     mm_camera_buf_def_t   *bufs;  // source buf_defs
95     uint32_t frameIndex;          // source frame index
96     bool halPPAllocatedBuf;       // true if src frame buffer is allocated by HAL PP block
97     QCameraHeapMemory    *snapshot_heap;    // output image heap buffer
98     QCameraHeapMemory    *metadata_heap;    // metadata heap buffer
99 
100     /* buffer in qcamera_pp_data_t need to be release when done */
101     bool reproc_frame_release;       // false release original buffer
102                                      // true don't release it
103     mm_camera_buf_def_t *src_reproc_bufs;
104     mm_camera_super_buf_t *src_reproc_frame;// source frame (need to be
105                                             //returned back to kernel after done)
106     uint8_t offline_buffer;
107     mm_camera_buf_def_t *offline_reproc_buf; //HAL processed buffer
108 } qcamera_hal_pp_data_t;
109 
110 typedef struct {
111     uint32_t jobId;                  // job ID (obtained when start_jpeg_job)
112     jpeg_job_status_t status;        // jpeg encoding status
113     mm_jpeg_output_t out_data;         // ptr to jpeg output buf
114 } qcamera_jpeg_evt_payload_t;
115 
116 typedef struct {
117     camera_memory_t *        data;     // ptr to data memory struct
118     mm_camera_super_buf_t *  frame;    // ptr to frame
119     QCameraMemory *          streamBufs; //ptr to stream buffers
120     bool                     unlinkFile; // unlink any stored buffers on error
121 } qcamera_release_data_t;
122 
123 typedef struct {
124     int32_t                  msg_type; // msg type of data notify
125     camera_memory_t *        data;     // ptr to data memory struct
126     unsigned int             index;    // index of the buf in the whole buffer
127     camera_frame_metadata_t *metadata; // ptr to meta data
128     qcamera_release_data_t   release_data; // any data needs to be release after notify
129 } qcamera_data_argm_t;
130 
131 typedef enum {
132     QCAMERA_HAL_PP_TYPE_UNDEFINED = 0,       // default undefined type
133     QCAMERA_HAL_PP_TYPE_DUAL_FOV,            // dual camera Wide+Tele Dual FOV blending
134     QCAMERA_HAL_PP_TYPE_BOKEH,               // dual camera Wide+Tele Snapshot Bokeh
135     QCAMERA_HAL_PP_TYPE_CLEARSIGHT,          // dual camera Bayer+Mono Clearsight
136     QCAMERA_HAL_PP_TYPE_MAX
137 } HALPPType;
138 
139 #define MAX_EXIF_TABLE_ENTRIES 17
140 class QCameraExif
141 {
142 public:
143     QCameraExif();
144     virtual ~QCameraExif();
145 
146     int32_t addEntry(exif_tag_id_t tagid,
147                      exif_tag_type_t type,
148                      uint32_t count,
149                      void *data);
getNumOfEntries()150     uint32_t getNumOfEntries() {return m_nNumEntries;};
getEntries()151     QEXIF_INFO_DATA *getEntries() {return m_Entries;};
152 
153 private:
154     QEXIF_INFO_DATA m_Entries[MAX_EXIF_TABLE_ENTRIES];  // exif tags for JPEG encoder
155     uint32_t  m_nNumEntries;                            // number of valid entries
156 };
157 
158 class QCameraPostProcessor
159 {
160 public:
161     QCameraPostProcessor(QCamera2HardwareInterface *cam_ctrl);
162     virtual ~QCameraPostProcessor();
163 
164     int32_t init(jpeg_encode_callback_t jpeg_cb, void *user_data);
165     int32_t deinit();
166     int32_t start(QCameraChannel *pSrcChannel);
167     int32_t stop();
168     bool validatePostProcess(mm_camera_super_buf_t *frame);
169     int32_t processData(mm_camera_super_buf_t *frame);
170     int32_t processRawData(mm_camera_super_buf_t *frame);
171     int32_t processPPData(mm_camera_super_buf_t *frame);
172     int32_t processJpegEvt(qcamera_jpeg_evt_payload_t *evt);
173     int32_t getJpegPaddingReq(cam_padding_info_t &padding_info);
174     QCameraReprocessChannel * getReprocChannel(uint8_t index);
getJpegMemOpt()175     inline bool getJpegMemOpt() {return mJpegMemOpt;}
setJpegMemOpt(bool val)176     inline void setJpegMemOpt(bool val) {mJpegMemOpt = val;}
177     int32_t setJpegHandle(mm_jpeg_ops_t *pJpegHandle,
178             mm_jpeg_mpo_ops_t* pJpegMpoHandle, uint32_t clientHandle);
179     int32_t createJpegSession(QCameraChannel *pSrcChannel);
180 
getPPChannelCount()181     int8_t getPPChannelCount() {return mPPChannelCount;};
182     mm_camera_buf_def_t *getOfflinePPInputBuffer(
183             mm_camera_super_buf_t *src_frame);
184     static void processHalPPDataCB(qcamera_hal_pp_data_t *pOutput, void* pUserData);
185     static void getHalPPOutputBufferCB(uint32_t frameIndex, void* pUserData);
186     QCameraMemory *mOfflineDataBufs;
187     QCameraChannel *getChannelByHandle(uint32_t channelHandle);
isHalPPEnabled()188     bool isHalPPEnabled() { return (m_halPP != NULL);}
189     void releaseSuperBuf(mm_camera_super_buf_t *super_buf);
190     QCamera2HardwareInterface *m_parent;
191 private:
192     int32_t sendDataNotify(int32_t msg_type,
193             camera_memory_t *data,
194             uint8_t index,
195             camera_frame_metadata_t *metadata,
196             qcamera_release_data_t *release_data,
197             uint32_t super_buf_frame_idx = 0);
198     int32_t sendEvtNotify(int32_t msg_type, int32_t ext1, int32_t ext2);
199     qcamera_jpeg_data_t *findJpegJobByJobId(uint32_t jobId);
200     mm_jpeg_color_format getColorfmtFromImgFmt(cam_format_t img_fmt);
201     mm_jpeg_format_t getJpegImgTypeFromImgFmt(cam_format_t img_fmt);
202     int32_t getJpegEncodingConfig(mm_jpeg_encode_params_t& encode_parm,
203                                   QCameraStream *main_stream,
204                                   QCameraStream *thumb_stream,
205                                   const mm_camera_super_buf_t *halpp_out_buf = NULL);
206     int32_t encodeData(qcamera_jpeg_data_t *jpeg_job_data,
207                        uint8_t &needNewSess);
208     int32_t queryStreams(QCameraStream **main,
209             QCameraStream **thumb,
210             QCameraStream **reproc,
211             mm_camera_buf_def_t **main_image,
212             mm_camera_buf_def_t **thumb_image,
213             mm_camera_super_buf_t *main_frame,
214             mm_camera_super_buf_t *reproc_frame);
215     int32_t syncStreamParams(mm_camera_super_buf_t *frame,
216             mm_camera_super_buf_t *reproc_frame);
217     void releaseSuperBuf(mm_camera_super_buf_t *super_buf,
218             cam_stream_type_t stream_type);
219     static void releaseNotifyData(void *user_data,
220                                   void *cookie,
221                                   int32_t cb_status);
222     void releaseJpegJobData(qcamera_jpeg_data_t *job);
223     static void releaseSaveJobData(void *data, void *user_data);
224     static void releaseRawData(void *data, void *user_data);
225     int32_t processRawImageImpl(mm_camera_super_buf_t *recvd_frame);
226 
227     static void releaseJpegData(void *data, void *user_data);
228     static void releasePPInputData(void *data, void *user_data);
229     static void releaseOngoingPPData(void *data, void *user_data);
230 
231     static void *dataProcessRoutine(void *data);
232     static void *dataSaveRoutine(void *data);
233 
234     int32_t setYUVFrameInfo(mm_camera_super_buf_t *recvd_frame);
235     static bool matchJobId(void *data, void *user_data, void *match_data);
236     static int getJpegMemory(omx_jpeg_ouput_buf_t *out_buf);
237     static int releaseJpegMemory(omx_jpeg_ouput_buf_t *out_buf);
238     int32_t processHalPPData(qcamera_hal_pp_data_t *pData);
239     void getHalPPOutputBuffer(uint32_t frameIndex);
240     int32_t doReprocess();
241     int32_t stopCapture();
242     int32_t initHALPP();
243 private:
244     jpeg_encode_callback_t     mJpegCB;
245     void *                     mJpegUserData;
246     mm_jpeg_ops_t              mJpegHandle;
247     mm_jpeg_mpo_ops_t          mJpegMpoHandle; // handle for mpo composition for dualcam
248     uint32_t                   mJpegClientHandle;
249     uint32_t                   mJpegSessionId;
250     uint32_t                   mJpegSessionIdHalPP;
251 
252     void *                     m_pJpegOutputMem[MM_JPEG_MAX_BUF];
253     void *                     m_pJpegOutputMemHalPP[MM_JPEG_MAX_BUF];
254     QCameraExif *              m_pJpegExifObj;
255     uint32_t                   m_bThumbnailNeeded;
256 
257     int8_t                     mPPChannelCount;
258     QCameraReprocessChannel    *mPPChannels[CAM_PP_CHANNEL_MAX];
259 
260     camera_memory_t *          m_DataMem; // save frame mem pointer
261 
262     int8_t                     m_bInited; // if postproc is inited
263 
264     QCameraQueue m_inputPPQ;            // input queue for postproc
265     QCameraQueue m_ongoingPPQ;          // ongoing postproc queue
266     QCameraQueue m_inputJpegQ;          // input jpeg job queue
267     QCameraQueue m_ongoingJpegQ;        // ongoing jpeg job queue
268     QCameraQueue m_inputRawQ;           // input raw job queue
269     QCameraQueue m_inputSaveQ;          // input save job queue
270     QCameraCmdThread m_dataProcTh;      // thread for data processing
271     QCameraCmdThread m_saveProcTh;      // thread for storing buffers
272     uint32_t mSaveFrmCnt;               // save frame counter
273     static const char *STORE_LOCATION;  // path for storing buffers
274     bool mUseSaveProc;                  // use store thread
275     bool mUseJpegBurst;                 // use jpeg burst encoding mode
276     bool mUseJpegBurstHalPP;
277     bool mJpegMemOpt;
278     uint32_t   m_JpegOutputMemCount;
279     uint32_t   m_JpegOutputMemCountHALPP;
280     QCameraStream *pJpegSrcStream;
281     uint8_t mNewJpegSessionNeeded;
282     uint8_t mNewJpegSessionNeededHalPP;
283     int32_t m_bufCountPPQ;
284     Vector<mm_camera_buf_def_t *> m_InputMetadata; // store input metadata buffers for AOST cases
285     size_t m_PPindex;                   // counter for each incoming AOST buffer
286     pthread_mutex_t m_reprocess_lock;   // lock to ensure reprocess job is not freed early.
287     HALPPType m_halPPType;              // HAL Post process type
288     QCameraHALPP *m_halPP;              // HAL Post process block
289 
290 public:
291     cam_dimension_t m_dst_dim;
292 };
293 
294 }; // namespace qcamera
295 
296 #endif /* __QCAMERA_POSTPROC_H__ */
297