Commit | Line | Data |
---|---|---|
5763fb39 T |
1 | /* |
2 | * Copyright Samsung Electronics Co.,LTD. | |
3 | * Copyright (C) 2015 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 | // To prevent build conflict with the previous libhwjpeg | |
19 | #ifdef USES_UNIVERSAL_LIBHWJPEG | |
20 | ||
21 | #ifndef __HARDWARE_EXYNOS_EXYNOS_JPEG_API_H__ | |
22 | #define __HARDWARE_EXYNOS_EXYNOS_JPEG_API_H__ | |
23 | ||
24 | // Exynos HAL defines another version of videodev2 apart from the original one | |
25 | // This prevents conflict from the Exynos HAL from the original videodev2.h | |
26 | /* | |
27 | #ifndef v4l2_fourcc | |
a4e95a20 | 28 | #include <linux/videodev2.h> |
5763fb39 T |
29 | #endif |
30 | */ | |
31 | ||
32 | #include <exynos-hwjpeg.h> | |
33 | ||
34 | #ifndef JPEG_CACHE_ON | |
35 | #define JPEG_CACHE_ON 1 | |
36 | #endif | |
37 | ||
38 | #define JPEG_BUF_TYPE_USER_PTR 1 | |
39 | #define JPEG_BUF_TYPE_DMA_BUF 2 | |
40 | ||
41 | // CUSTOM V4L2 4CC FORMATS FOR LEGACY JPEG LIBRARY AND DRIVERS | |
42 | #ifndef V4L2_PIX_FMT_JPEG_444 | |
43 | #define V4L2_PIX_FMT_JPEG_444 v4l2_fourcc('J', 'P', 'G', '4') | |
44 | #endif | |
45 | #ifndef V4L2_PIX_FMT_JPEG_422 | |
46 | #define V4L2_PIX_FMT_JPEG_422 v4l2_fourcc('J', 'P', 'G', '2') | |
47 | #endif | |
48 | #ifndef V4L2_PIX_FMT_JPEG_420 | |
49 | #define V4L2_PIX_FMT_JPEG_420 v4l2_fourcc('J', 'P', 'G', '0') | |
50 | #endif | |
51 | #ifndef V4L2_PIX_FMT_JPEG_GRAY | |
52 | #define V4L2_PIX_FMT_JPEG_GRAY v4l2_fourcc('J', 'P', 'G', 'G') | |
53 | #endif | |
54 | #ifndef V4L2_PIX_FMT_JPEG_422V | |
55 | #define V4L2_PIX_FMT_JPEG_422V v4l2_fourcc('J', 'P', 'G', '5') | |
56 | #endif | |
57 | #ifndef V4L2_PIX_FMT_JPEG_411 | |
58 | #define V4L2_PIX_FMT_JPEG_411 v4l2_fourcc('J', 'P', 'G', '1') | |
59 | #endif | |
60 | ||
61 | class ExynosJpegEncoder { | |
62 | /* | |
63 | * ExynosJpedgEncoder class is actually a derived class of | |
64 | * CHWJpegV4L2Compressor. But it is not derived from CHWJpegV4L2Compressor | |
65 | * because it has a lot of virtual functions which require extra memory for | |
66 | * vtables. Moreover, ExynosJpegEncoder class implements no virtual function | |
67 | * of CHWJpegV4L2Compressor. | |
68 | */ | |
69 | CHWJpegV4L2Compressor m_hwjpeg; | |
70 | ||
71 | char m_iInBufType; | |
72 | char m_iOutBufType; | |
73 | ||
74 | unsigned int m_uiState; | |
75 | ||
76 | int m_nQFactor; | |
77 | int m_nWidth; | |
78 | int m_nHeight; | |
79 | int m_v4l2Format; | |
80 | int m_jpegFormat; | |
81 | int m_nStreamSize; | |
82 | ||
83 | bool __EnsureFormatIsApplied(); | |
84 | protected: | |
85 | enum { | |
86 | STATE_SIZE_CHANGED = 1 << 0, | |
87 | STATE_PIXFMT_CHANGED = 1 << 1, | |
88 | STATE_BASE_MAX = 1 << 16, | |
89 | }; | |
90 | ||
91 | unsigned int GetDeviceCapabilities() { return m_hwjpeg.GetDeviceCapabilities(); } | |
92 | CHWJpegCompressor &GetCompressor() { return m_hwjpeg; } | |
93 | unsigned int GetHWDelay() { return m_hwjpeg.GetHWDelay(); } | |
94 | ||
95 | void SetState(unsigned int state) { m_uiState |= state; } | |
96 | void ClearState(unsigned int state) { m_uiState &= ~state; } | |
97 | bool TestState(unsigned int state) { return (m_uiState & state) == state; } | |
98 | bool TestStateEither(unsigned int state) { return (m_uiState & state) != 0; } | |
99 | ||
100 | virtual bool EnsureFormatIsApplied() { return __EnsureFormatIsApplied(); } | |
101 | public: | |
102 | ExynosJpegEncoder(): m_hwjpeg(), | |
103 | m_iInBufType(JPEG_BUF_TYPE_USER_PTR), m_iOutBufType(JPEG_BUF_TYPE_USER_PTR), m_uiState(0), | |
104 | m_nQFactor(0), m_nWidth(0), m_nHeight(0), m_v4l2Format(0), m_jpegFormat(0), m_nStreamSize(0) | |
105 | { | |
106 | /* To detect setInBuf() call without format setting */ | |
107 | SetState(STATE_SIZE_CHANGED | STATE_PIXFMT_CHANGED); | |
108 | } | |
109 | virtual ~ExynosJpegEncoder() { destroy(); } | |
110 | ||
111 | // Return 0 on success, -1 on error | |
112 | int flagCreate() { return m_hwjpeg.Okay() ? 0 : -1; } | |
113 | virtual int create(void) { return flagCreate(); } | |
114 | virtual int destroy(void) { return 0; } | |
115 | int updateConfig(void) { return 0; } | |
6f7be8c2 | 116 | int setCache(int val __unused) { return 0; } |
5763fb39 T |
117 | |
118 | void *getJpegConfig() { return reinterpret_cast<void *>(this); } | |
119 | int setJpegConfig(void* pConfig); | |
120 | ||
121 | int checkInBufType(void) { return m_iInBufType; } | |
122 | int checkOutBufType(void) { return m_iOutBufType; } | |
123 | ||
124 | int getInBuf(int *piBuf, int *piInputSize, int iSize); | |
125 | int getOutBuf(int *piBuf, int *piOutputSize); | |
126 | int getInBuf(char **pcBuf, int *piInputSize, int iSize); | |
127 | int getOutBuf(char **pcBuf, int *piOutputSize); | |
128 | ||
129 | int setInBuf(int *piBuf, int *iSize); | |
130 | int setOutBuf(int iBuf, int iSize); | |
131 | int setInBuf(char **pcBuf, int *iSize); | |
132 | int setOutBuf(char *pcBuf, int iSize); | |
133 | ||
134 | int getSize(int *piWidth, int *piHeight) { | |
135 | *piWidth = m_nWidth; | |
136 | *piHeight = m_nHeight; | |
137 | return 0; | |
138 | } | |
139 | ||
140 | int setSize(int iW, int iH) { | |
141 | if ((m_nWidth != iW) || (m_nHeight != iH)) { | |
142 | m_nWidth = iW; | |
143 | m_nHeight = iH; | |
144 | SetState(STATE_SIZE_CHANGED); | |
145 | } | |
146 | return 0; | |
147 | } | |
148 | ||
149 | int setJpegFormat(int iV4l2JpegFormat); | |
150 | int getColorFormat(void) { return m_v4l2Format; } | |
151 | int setColorFormat(int iV4l2ColorFormat) { | |
152 | if (iV4l2ColorFormat != m_v4l2Format) { | |
153 | m_v4l2Format = iV4l2ColorFormat; | |
154 | SetState(STATE_PIXFMT_CHANGED); | |
155 | } | |
156 | return 0; | |
157 | } | |
158 | ||
159 | int setQuality(int iQuality) { | |
160 | if (m_nQFactor != iQuality) { | |
161 | if (!m_hwjpeg.SetQuality(static_cast<unsigned int>(iQuality))) | |
162 | return -1; | |
163 | m_nQFactor = iQuality; | |
164 | } | |
165 | return 0; | |
166 | } | |
167 | ||
168 | int setQuality(const unsigned char q_table[]); | |
169 | ||
170 | int setColorBufSize(int *piBufSize, int iSize); | |
171 | int getJpegSize(void) { return m_nStreamSize; } | |
172 | ||
173 | int encode(void) { | |
174 | if (!__EnsureFormatIsApplied()) | |
175 | return false; | |
176 | ||
177 | m_nStreamSize = static_cast<int>(m_hwjpeg.Compress()); | |
178 | return (m_nStreamSize < 0) ? -1 : 0; | |
179 | } | |
180 | ||
181 | }; | |
182 | ||
183 | #endif //__HARDWARE_EXYNOS_EXYNOS_JPEG_API_H__ | |
184 | ||
185 | #endif //USES_UNIVERSAL_LIBHWJPEG |