1 /*
2  * Copyright Samsung Electronics Co.,LTD.
3  * Copyright (C) 2010 The Android Open Source Project
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 EXYNOS_JPEG_ENCODER_FOR_CAMERA_H_
19 #define EXYNOS_JPEG_ENCODER_FOR_CAMERA_H_
20 
21 #include "ExynosExif.h"
22 
23 #include "ExynosJpegApi.h"
24 
25 #include <sys/mman.h>
26 #include "ion.h"
27 
28 #define JPEG_THUMBNAIL_QUALITY 60
29 #define EXIF_LIMIT_SIZE 64*1024
30 //#define JPEG_WA_FOR_PAGEFAULT
31 #define JPEG_WA_BUFFER_SIZE 64
32 
33 class ExynosJpegEncoderForCamera {
34 public :
35     ;
36     enum ERROR {
37         ERROR_ALREADY_CREATE = -0x200,
38         ERROR_CANNOT_CREATE_EXYNOS_JPEG_ENC_HAL,
39         ERROR_NOT_YET_CREATED,
40         ERROR_ALREADY_DESTROY,
41         ERROR_INPUT_DATA_SIZE_TOO_LARGE,
42         ERROR_OUT_BUFFER_SIZE_TOO_SMALL,
43         ERROR_EXIFOUT_ALLOC_FAIL,
44         ERROR_MAKE_EXIF_FAIL,
45         ERROR_INVALID_SCALING_WIDTH_HEIGHT,
46         ERROR_CANNOT_CREATE_SEC_THUMB,
47         ERROR_THUMB_JPEG_SIZE_TOO_SMALL,
48         ERROR_IMPLEMENT_NOT_YET,
49         ERROR_JPEG_DEVICE_ALREADY_CREATE = -0x100,
50         ERROR_CANNOT_OPEN_JPEG_DEVICE,
51         ERROR_JPEG_DEVICE_ALREADY_CLOSED,
52         ERROR_JPEG_DEVICE_ALREADY_DESTROY,
53         ERROR_JPEG_DEVICE_NOT_CREATE_YET,
54         ERROR_INVALID_COLOR_FORMAT,
55         ERROR_INVALID_JPEG_FORMAT,
56         ERROR_JPEG_CONFIG_POINTER_NULL,
57         ERROR_INVALID_JPEG_CONFIG,
58         ERROR_IN_BUFFER_CREATE_FAIL,
59         ERROR_OUT_BUFFER_CREATE_FAIL,
60         ERROR_EXCUTE_FAIL,
61         ERROR_JPEG_SIZE_TOO_SMALL,
62         ERROR_CANNOT_CHANGE_CACHE_SETTING,
63         ERROR_SIZE_NOT_SET_YET,
64         ERROR_BUFFR_IS_NULL,
65         ERROR_BUFFER_TOO_SMALL,
66         ERROR_GET_SIZE_FAIL,
67         ERROR_REQBUF_FAIL,
68         ERROR_INVALID_V4l2_BUF_TYPE = -0x80,
69         ERROR_MMAP_FAILED,
70         ERROR_FAIL,
71         ERROR_NONE = 0
72     };
73 
74     ExynosJpegEncoderForCamera();
75     virtual ~ExynosJpegEncoderForCamera();
76 
77     bool   flagCreate();
78     int     create(void);
79     int     destroy(void);
80 
81     int     setSize(int w, int h);
82     int     setQuality(int quality);
83     int     setColorFormat(int colorFormat);
84     int     setJpegFormat(int jpegFormat);
85 
86     int     updateConfig(void);
87 
88     int     setInBuf(char **buf, int *size);
89     int     setOutBuf(char *buf, int size);
90 
91     int     encode(int *size, exif_attribute_t *exifInfo);
92 
93     int     setThumbnailSize(int w, int h);
94     int     setThumbnailQuality(int quality);
95 
96     int     makeExif(unsigned char *exifOut,
97                                exif_attribute_t *exifIn,
98                                unsigned int *size,
99                                bool useMainbufForThumb = false);
100 
101 private:
102     inline void writeExifIfd(unsigned char **pCur,
103                                          unsigned short tag,
104                                          unsigned short type,
105                                          unsigned int count,
106                                          uint32_t value);
107     inline void writeExifIfd(unsigned char **pCur,
108                                         unsigned short tag,
109                                         unsigned short type,
110                                         unsigned int count,
111                                         unsigned char *pValue);
112     inline void writeExifIfd(unsigned char **pCur,
113                                          unsigned short tag,
114                                          unsigned short type,
115                                          unsigned int count,
116                                          rational_t *pValue,
117                                          unsigned int *offset,
118                                          unsigned char *start);
119     inline void writeExifIfd(unsigned char **pCur,
120                                          unsigned short tag,
121                                          unsigned short type,
122                                          unsigned int count,
123                                          unsigned char *pValue,
124                                          unsigned int *offset,
125                                          unsigned char *start);
126     int     scaleDownYuv422(char **srcBuf, unsigned int srcW, unsigned int srcH,
127                                                 char **dstBuf, unsigned int dstW, unsigned int dstH);
128     int     scaleDownYuv422_2p(char **srcBuf, unsigned int srcW, unsigned int srcH,
129                                                         char **dstBuf, unsigned int dstW, unsigned int dstH);
130     // thumbnail
131     int     encodeThumbnail(unsigned int *size, bool useMain = true);
132 
133     int     allocJpegIonMemory(ion_client ionClient, ion_buffer *ionBuffer, char **buffer, int size);
134     void    freeJpegIonMemory(ion_client ionClient, ion_buffer *ionBuffer, char **buffer, int size);
135 
136     bool     m_flagCreate;
137 
138     ExynosJpegEncoder *m_jpegMain;
139     ExynosJpegEncoder *m_jpegThumb;
140 
141     char *m_pThumbInputBuffer;
142     char *m_pThumbOutputBuffer;
143 #ifdef JPEG_WA_FOR_PAGEFAULT
144     char *m_pExtInBuf;
145     char *m_pJpegInputBuffer;
146     int m_iInBufSize;
147 #endif // JPEG_WA_FOR_PAGEFAULT
148     ion_client m_ionJpegClient;
149     ion_buffer m_ionThumbInBuffer, m_ionThumbOutBuffer;
150 #ifdef JPEG_WA_FOR_PAGEFAULT
151     ion_buffer m_ionJpegInBuffer;
152 #endif // JPEG_WA_FOR_PAGEFAULT
153 
154     int m_thumbnailW;
155     int m_thumbnailH;
156     int m_thumbnailQuality;
157 };
158 
159 #endif /* __SEC_JPG_ENC_H__ */
160