1 /* 2 * Copyright Samsung Electronics Co.,LTD. 3 * Copyright (C) 2011 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 #include <stdio.h> 19 #include <stdlib.h> 20 #include <sys/types.h> 21 #include <sys/stat.h> 22 #include <sys/ioctl.h> 23 #include <fcntl.h> 24 #include <ctype.h> 25 #include <unistd.h> 26 #include <sys/mman.h> 27 #include <string.h> 28 #include <errno.h> 29 #include <signal.h> 30 #include <math.h> 31 #include <sys/poll.h> 32 33 #include <cutils/log.h> 34 35 #include <utils/Log.h> 36 37 #include "ExynosJpegApi.h" 38 39 #define JPEG_ERROR_LOG(fmt,...) 40 41 #define NUM_JPEG_ENC_IN_PLANES (1) 42 #define NUM_JPEG_ENC_OUT_PLANES (1) 43 44 #define NUM_JPEG_ENC_IN_BUFS (1) 45 #define NUM_JPEG_ENC_OUT_BUFS (1) 46 47 ExynosJpegEncoder::ExynosJpegEncoder() 48 { 49 t_iJpegFd = -1; 50 t_bFlagCreate = false; 51 } 52 53 ExynosJpegEncoder::~ExynosJpegEncoder() 54 { 55 if (t_bFlagCreate == true) { 56 this->destroy(); 57 } 58 } 59 60 int ExynosJpegEncoder::create(void) 61 { 62 return ExynosJpegBase::create(MODE_ENCODE); 63 } 64 65 int ExynosJpegEncoder::destroy(void) 66 { 67 return ExynosJpegBase::destroy(NUM_JPEG_ENC_IN_BUFS, NUM_JPEG_ENC_OUT_BUFS); 68 } 69 70 int ExynosJpegEncoder::setJpegConfig(void *pConfig) 71 { 72 return ExynosJpegBase::setJpegConfig(MODE_ENCODE, pConfig); 73 } 74 75 int ExynosJpegEncoder::getInBuf(int *piBuf, int *piInputSize, int iSize) 76 { 77 return getBuf(t_bFlagCreateInBuf, &t_stJpegInbuf, piBuf, piInputSize, iSize, t_iPlaneNum); 78 } 79 80 int ExynosJpegEncoder::getOutBuf(int *piBuf, int *piOutputSize) 81 { 82 return getBuf(t_bFlagCreateOutBuf, &t_stJpegOutbuf, piBuf, piOutputSize, \ 83 NUM_JPEG_ENC_OUT_PLANES, NUM_JPEG_ENC_OUT_PLANES); 84 } 85 86 int ExynosJpegEncoder::setInBuf(int *piBuf, int *iSize) 87 { 88 int iRet = ERROR_NONE; 89 iRet = setBuf(&t_stJpegInbuf, piBuf, iSize, t_iPlaneNum); 90 91 if (iRet == ERROR_NONE) { 92 t_bFlagCreateInBuf = true; 93 } 94 95 return iRet; 96 } 97 98 int ExynosJpegEncoder::setOutBuf(int iBuf, int iSize) 99 { 100 int iRet = ERROR_NONE; 101 iRet = setBuf(&t_stJpegOutbuf, &iBuf, &iSize, NUM_JPEG_ENC_OUT_PLANES); 102 103 if (iRet == ERROR_NONE) { 104 t_bFlagCreateOutBuf = true; 105 } 106 107 return iRet; 108 } 109 110 int ExynosJpegEncoder::getSize(int *piW, int *piH) 111 { 112 if (t_bFlagCreate == false) { 113 return ERROR_JPEG_DEVICE_NOT_CREATE_YET; 114 } 115 116 if (t_stJpegConfig.width == 0 && t_stJpegConfig.height == 0) { 117 return ERROR_SIZE_NOT_SET_YET; 118 } 119 120 *piW = t_stJpegConfig.width; 121 *piH = t_stJpegConfig.height; 122 123 return ERROR_NONE; 124 } 125 126 int ExynosJpegEncoder::getColorFormat(void) 127 { 128 return t_stJpegConfig.pix.enc_fmt.in_fmt; 129 } 130 131 int ExynosJpegEncoder::setColorFormat(int iV4l2ColorFormat) 132 { 133 return ExynosJpegBase::setColorFormat(MODE_ENCODE, iV4l2ColorFormat); 134 } 135 136 int ExynosJpegEncoder::setJpegFormat(int iV4l2JpegFormat) 137 { 138 return ExynosJpegBase::setJpegFormat(MODE_ENCODE, iV4l2JpegFormat); 139 } 140 141 int ExynosJpegEncoder::setColorBufSize(int *piBufSize, int iSize) 142 { 143 return ExynosJpegBase::setColorBufSize(MODE_ENCODE, piBufSize, iSize); 144 } 145 146 int ExynosJpegEncoder::updateConfig(void) 147 { 148 return ExynosJpegBase::updateConfig(MODE_ENCODE, \ 149 NUM_JPEG_ENC_IN_BUFS, NUM_JPEG_ENC_OUT_BUFS, \ 150 NUM_JPEG_ENC_IN_PLANES, NUM_JPEG_ENC_OUT_PLANES); 151 } 152 153 int ExynosJpegEncoder::setQuality(int iV4l2Quality) 154 { 155 if (t_bFlagCreate == false) { 156 return ERROR_JPEG_DEVICE_NOT_CREATE_YET; 157 } 158 159 if (iV4l2Quality >= 90) 160 t_stJpegConfig.enc_qual = QUALITY_LEVEL_1; 161 else if (iV4l2Quality >= 80) 162 t_stJpegConfig.enc_qual = QUALITY_LEVEL_2; 163 else if (iV4l2Quality >= 70) 164 t_stJpegConfig.enc_qual = QUALITY_LEVEL_3; 165 else 166 t_stJpegConfig.enc_qual = QUALITY_LEVEL_4; 167 168 return ERROR_NONE; 169 } 170 171 int ExynosJpegEncoder::getJpegSize(void) 172 { 173 if (t_bFlagCreate == false) { 174 return 0; 175 } 176 177 int iSize = -1; 178 #ifdef KERNEL_33_JPEG_API 179 iSize = t_stJpegConfig.sizeJpeg; 180 #else 181 iSize = t_v4l2GetCtrl(t_iJpegFd, V4L2_CID_CAM_JPEG_ENCODEDSIZE); 182 #endif 183 184 if (iSize < 0) { 185 JPEG_ERROR_LOG("%s::Fail to JPEG output buffer!!\n", __func__); 186 return 0; 187 } 188 189 return iSize; 190 } 191 192 int ExynosJpegEncoder::encode(void) 193 { 194 return ExynosJpegBase::execute(t_iPlaneNum, NUM_JPEG_ENC_OUT_PLANES); 195 } 196 197