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