1 /* Copyright (c) 2012-2014, The Linux Foundataion. 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 extern "C" {
34 #include <mm_camera_interface.h>
35 #include <mm_jpeg_interface.h>
36 }
37 #include "QCamera2HWI.h"
38 
39 #define MAX_JPEG_BURST 2
40 
41 namespace qcamera {
42 
43 class QCameraExif;
44 
45 typedef struct {
46     uint32_t jobId;                  // job ID
47     uint32_t client_hdl;             // handle of jpeg client (obtained when open jpeg)
48     mm_camera_super_buf_t *src_frame;// source frame (need to be returned back to kernel
49                                      //after done)
50     mm_camera_super_buf_t *src_reproc_frame; // original source
51                                              //frame for reproc if not NULL
52     metadata_buffer_t *metadata;     // source frame metadata
53     bool reproc_frame_release;       // false release original buffer, true don't release it
54     mm_camera_buf_def_t *src_reproc_bufs;
55     QCameraExif *pJpegExifObj;
56 } qcamera_jpeg_data_t;
57 
58 
59 typedef struct {
60     int8_t reprocCount;
61     mm_camera_super_buf_t *src_frame;    // source frame that needs post process
62     mm_camera_super_buf_t *src_reproc_frame;// source frame (need to be
63                                             //returned back to kernel after done)
64 }qcamera_pp_request_t;
65 
66 typedef struct {
67     uint32_t jobId;                  // job ID
68     int8_t reprocCount;              //Current pass count
69     mm_camera_super_buf_t *src_frame;// source frame
70     bool reproc_frame_release;       // false release original buffer
71                                      // true don't release it
72     mm_camera_buf_def_t *src_reproc_bufs;
73     mm_camera_super_buf_t *src_reproc_frame;// source frame (need to be
74                                             //returned back to kernel after done)
75 } qcamera_pp_data_t;
76 
77 typedef struct {
78     uint32_t jobId;                  // job ID (obtained when start_jpeg_job)
79     jpeg_job_status_t status;        // jpeg encoding status
80     mm_jpeg_output_t out_data;         // ptr to jpeg output buf
81 } qcamera_jpeg_evt_payload_t;
82 
83 typedef struct {
84     camera_memory_t *        data;     // ptr to data memory struct
85     mm_camera_super_buf_t *  frame;    // ptr to frame
86     QCameraMemory *          streamBufs; //ptr to stream buffers
87     bool                     unlinkFile; // unlink any stored buffers on error
88 } qcamera_release_data_t;
89 
90 typedef struct {
91     int32_t                  msg_type; // msg type of data notify
92     camera_memory_t *        data;     // ptr to data memory struct
93     unsigned int             index;    // index of the buf in the whole buffer
94     camera_frame_metadata_t *metadata; // ptr to meta data
95     qcamera_release_data_t   release_data; // any data needs to be release after notify
96 } qcamera_data_argm_t;
97 
98 #define MAX_EXIF_TABLE_ENTRIES 17
99 class QCameraExif
100 {
101 public:
102     QCameraExif();
103     virtual ~QCameraExif();
104 
105     int32_t addEntry(exif_tag_id_t tagid,
106                      exif_tag_type_t type,
107                      uint32_t count,
108                      void *data);
getNumOfEntries()109     uint32_t getNumOfEntries() {return m_nNumEntries;};
getEntries()110     QEXIF_INFO_DATA *getEntries() {return m_Entries;};
111 
112 private:
113     QEXIF_INFO_DATA m_Entries[MAX_EXIF_TABLE_ENTRIES];  // exif tags for JPEG encoder
114     uint32_t  m_nNumEntries;                            // number of valid entries
115 };
116 
117 class QCameraPostProcessor
118 {
119 public:
120     QCameraPostProcessor(QCamera2HardwareInterface *cam_ctrl);
121     virtual ~QCameraPostProcessor();
122 
123     int32_t init(jpeg_encode_callback_t jpeg_cb, void *user_data);
124     int32_t deinit();
125     int32_t start(QCameraChannel *pSrcChannel);
126     int32_t stop();
127     bool validatePostProcess(mm_camera_super_buf_t *frame);
128     int32_t processData(mm_camera_super_buf_t *frame);
129     int32_t processRawData(mm_camera_super_buf_t *frame);
130     int32_t processPPData(mm_camera_super_buf_t *frame);
131     int32_t processJpegEvt(qcamera_jpeg_evt_payload_t *evt);
132     int32_t getJpegPaddingReq(cam_padding_info_t &padding_info);
133     QCameraReprocessChannel * getReprocChannel(uint8_t index);
getJpegMemOpt()134     inline bool getJpegMemOpt() {return mJpegMemOpt;}
setJpegMemOpt(bool val)135     inline void setJpegMemOpt(bool val) {mJpegMemOpt = val;}
136 private:
137     int32_t sendDataNotify(int32_t msg_type,
138                            camera_memory_t *data,
139                            uint8_t index,
140                            camera_frame_metadata_t *metadata,
141                            qcamera_release_data_t *release_data);
142     int32_t sendEvtNotify(int32_t msg_type, int32_t ext1, int32_t ext2);
143     qcamera_jpeg_data_t *findJpegJobByJobId(uint32_t jobId);
144     mm_jpeg_color_format getColorfmtFromImgFmt(cam_format_t img_fmt);
145     mm_jpeg_format_t getJpegImgTypeFromImgFmt(cam_format_t img_fmt);
146     int32_t getJpegEncodingConfig(mm_jpeg_encode_params_t& encode_parm,
147                                   QCameraStream *main_stream,
148                                   QCameraStream *thumb_stream);
149     int32_t encodeData(qcamera_jpeg_data_t *jpeg_job_data,
150                        uint8_t &needNewSess);
151     int32_t queryStreams(QCameraStream **main,
152             QCameraStream **thumb,
153             QCameraStream **reproc,
154             mm_camera_buf_def_t **main_image,
155             mm_camera_buf_def_t **thumb_image,
156             mm_camera_super_buf_t *main_frame,
157             mm_camera_super_buf_t *reproc_frame);
158     int32_t syncStreamParams(mm_camera_super_buf_t *frame,
159             mm_camera_super_buf_t *reproc_frame);
160     void releaseSuperBuf(mm_camera_super_buf_t *super_buf);
161     static void releaseNotifyData(void *user_data,
162                                   void *cookie,
163                                   int32_t cb_status);
164     void releaseJpegJobData(qcamera_jpeg_data_t *job);
165     static void releaseSaveJobData(void *data, void *user_data);
166     static void releaseRawData(void *data, void *user_data);
167     int32_t processRawImageImpl(mm_camera_super_buf_t *recvd_frame);
168 
169     static void releaseJpegData(void *data, void *user_data);
170     static void releasePPInputData(void *data, void *user_data);
171     static void releaseOngoingPPData(void *data, void *user_data);
172 
173     static void *dataProcessRoutine(void *data);
174     static void *dataSaveRoutine(void *data);
175 
176     int32_t setYUVFrameInfo(mm_camera_super_buf_t *recvd_frame);
177     static bool matchJobId(void *data, void *user_data, void *match_data);
178     static int getJpegMemory(omx_jpeg_ouput_buf_t *out_buf);
179 
180     int32_t doReprocess();
181     int32_t stopCapture();
182 
183 private:
184     QCamera2HardwareInterface *m_parent;
185     jpeg_encode_callback_t     mJpegCB;
186     void *                     mJpegUserData;
187     mm_jpeg_ops_t              mJpegHandle;
188     uint32_t                   mJpegClientHandle;
189     uint32_t                   mJpegSessionId;
190 
191     void *                     m_pJpegOutputMem[MM_JPEG_MAX_BUF];
192     QCameraExif *              m_pJpegExifObj;
193     uint32_t                   m_bThumbnailNeeded;
194 
195     int8_t                     mTotalNumReproc;
196     QCameraReprocessChannel    *mPPChannels[CAM_QCOM_FEATURE_MAX];
197 
198     camera_memory_t *          m_DataMem; // save frame mem pointer
199 
200     int8_t                     m_bInited; // if postproc is inited
201 
202     QCameraQueue m_inputPPQ;            // input queue for postproc
203     QCameraQueue m_ongoingPPQ;          // ongoing postproc queue
204     QCameraQueue m_inputJpegQ;          // input jpeg job queue
205     QCameraQueue m_ongoingJpegQ;        // ongoing jpeg job queue
206     QCameraQueue m_inputRawQ;           // input raw job queue
207     QCameraQueue m_inputSaveQ;          // input save job queue
208     QCameraCmdThread m_dataProcTh;      // thread for data processing
209     QCameraCmdThread m_saveProcTh;      // thread for storing buffers
210     uint32_t mSaveFrmCnt;               // save frame counter
211     static const char *STORE_LOCATION;  // path for storing buffers
212     bool mUseSaveProc;                  // use store thread
213     bool mUseJpegBurst;                 // use jpeg burst encoding mode
214     bool mJpegMemOpt;
215     uint32_t   m_JpegOutputMemCount;
216     uint8_t mNewJpegSessionNeeded;
217     int32_t m_bufCountPPQ;
218     Vector<mm_camera_buf_def_t *> m_InputMetadata; // store input metadata buffers for AOST cases
219     size_t m_PPindex;                   // counter for each incoming AOST buffer
220 
221 public:
222     cam_dimension_t m_dst_dim;
223 };
224 
225 }; // namespace qcamera
226 
227 #endif /* __QCAMERA_POSTPROC_H__ */
228