Back to home page

Android Cross Reference

 
 

    


0001 /*
0002  * Copyright Samsung Electronics Co.,LTD.
0003  * Copyright (C) 2010 The Android Open Source Project
0004  *
0005  * Licensed under the Apache License, Version 2.0 (the "License");
0006  * you may not use this file except in compliance with the License.
0007  * You may obtain a copy of the License at
0008  *
0009  *     http://www.apache.org/licenses/LICENSE-2.0
0010  *
0011  * Unless required by applicable law or agreed to in writing, software
0012  * distributed under the License is distributed on an "AS IS" BASIS,
0013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014  * See the License for the specific language governing permissions and
0015  * limitations under the License.
0016  *
0017  * JPEG DRIVER MODULE (JpegEncoder.cpp)
0018  * Author  : ge.lee       -- initial version
0019  * Date    : 03 June 2010
0020  * Purpose : This file implements the JPEG encoder APIs as needed by Camera HAL
0021  */
0022 #define LOG_TAG "JpegEncoder"
0023 #define MAIN_DUMP  0
0024 #define THUMB_DUMP 0
0025 
0026 #include <utils/Log.h>
0027 #include <sys/mman.h>
0028 #include <fcntl.h>
0029 
0030 #include "JpegEncoder.h"
0031 
0032 static const char ExifAsciiPrefix[] = { 0x41, 0x53, 0x43, 0x49, 0x49, 0x0, 0x0, 0x0 };
0033 
0034 namespace android {
0035 JpegEncoder::JpegEncoder() : available(false)
0036 {
0037     mArgs.mmapped_addr = (char *)MAP_FAILED;
0038     mArgs.enc_param       = NULL;
0039     mArgs.thumb_enc_param = NULL;
0040 
0041     mDevFd = open(JPG_DRIVER_NAME, O_RDWR);
0042     if (mDevFd < 0) {
0043         ALOGE("Failed to open the device");
0044         return;
0045     }
0046 
0047     mArgs.mmapped_addr = (char *)mmap(0,
0048                                       JPG_TOTAL_BUF_SIZE,
0049                                       PROT_READ | PROT_WRITE,
0050                                       MAP_SHARED,
0051                                       mDevFd,
0052                                       0);
0053 
0054     if (mArgs.mmapped_addr == MAP_FAILED) {
0055         ALOGE("Failed to mmap");
0056         return;
0057     }
0058 
0059     mArgs.enc_param = new jpg_enc_proc_param;
0060     if (mArgs.enc_param == NULL) {
0061         ALOGE("Failed to allocate the memory for enc_param");
0062         return;
0063     }
0064     memset(mArgs.enc_param, 0, sizeof(jpg_enc_proc_param));
0065 
0066     mArgs.thumb_enc_param = new jpg_enc_proc_param;
0067     if (mArgs.thumb_enc_param == NULL) {
0068         ALOGE("Failed to allocate the memory for thumb_enc_param");
0069     delete mArgs.enc_param;
0070         return;
0071     }
0072     memset(mArgs.thumb_enc_param, 0, sizeof(jpg_enc_proc_param));
0073 
0074     mArgs.enc_param->sample_mode = JPG_420;
0075     mArgs.enc_param->enc_type = JPG_MAIN;
0076     mArgs.thumb_enc_param->sample_mode = JPG_420;
0077     mArgs.thumb_enc_param->enc_type = JPG_THUMBNAIL;
0078 
0079     available = true;
0080 }
0081 
0082 JpegEncoder::~JpegEncoder()
0083 {
0084     if (mArgs.mmapped_addr != (char*)MAP_FAILED)
0085         munmap(mArgs.mmapped_addr, JPG_TOTAL_BUF_SIZE);
0086 
0087     delete mArgs.enc_param;
0088 
0089     delete mArgs.thumb_enc_param;
0090 
0091     if (mDevFd > 0)
0092         close(mDevFd);
0093 }
0094 
0095 jpg_return_status JpegEncoder::setConfig(jpeg_conf type, int32_t value)
0096 {
0097     if (!available)
0098         return JPG_FAIL;
0099 
0100     jpg_return_status ret = JPG_SUCCESS;
0101 
0102     switch (type) {
0103     case JPEG_SET_ENCODE_WIDTH:
0104         if (value < 0 || value > MAX_JPG_WIDTH)
0105             ret = JPG_FAIL;
0106         else
0107             mArgs.enc_param->width = value;
0108         break;
0109 
0110     case JPEG_SET_ENCODE_HEIGHT:
0111         if (value < 0 || value > MAX_JPG_HEIGHT)
0112             ret = JPG_FAIL;
0113         else
0114             mArgs.enc_param->height = value;
0115         break;
0116 
0117     case JPEG_SET_ENCODE_QUALITY:
0118         if (value < JPG_QUALITY_LEVEL_1 || value > JPG_QUALITY_LEVEL_4)
0119             ret = JPG_FAIL;
0120         else
0121             mArgs.enc_param->quality = (image_quality_type_t)value;
0122         break;
0123 
0124     case JPEG_SET_ENCODE_IN_FORMAT:
0125         if (value != JPG_MODESEL_YCBCR && value != JPG_MODESEL_RGB) {
0126             ret = JPG_FAIL;
0127         } else {
0128             mArgs.enc_param->in_format = (in_mode_t)value;
0129             mArgs.thumb_enc_param->in_format = (in_mode_t)value;
0130         }
0131         break;
0132 
0133     case JPEG_SET_SAMPING_MODE:
0134         if (value != JPG_420 && value != JPG_422) {
0135             ret = JPG_FAIL;
0136         } else {
0137             mArgs.enc_param->sample_mode = (sample_mode_t)value;
0138             mArgs.thumb_enc_param->sample_mode = (sample_mode_t)value;
0139         }
0140         break;
0141 
0142     case JPEG_SET_THUMBNAIL_WIDTH:
0143         if (value < 0 || value > MAX_JPG_THUMBNAIL_WIDTH)
0144             ret = JPG_FAIL;
0145         else
0146             mArgs.thumb_enc_param->width = value;
0147         break;
0148 
0149     case JPEG_SET_THUMBNAIL_HEIGHT:
0150         if (value < 0 || value > MAX_JPG_THUMBNAIL_HEIGHT)
0151             ret = JPG_FAIL;
0152         else
0153             mArgs.thumb_enc_param->height = value;
0154         break;
0155 
0156     default:
0157         ALOGE("Invalid Config type");
0158         ret = ERR_UNKNOWN;
0159     }
0160 
0161     if (ret == JPG_FAIL)
0162         ALOGE("Invalid value(%d) for %d type", value, type);
0163 
0164     return ret;
0165 }
0166 
0167 void* JpegEncoder::getInBuf(uint64_t size)
0168 {
0169     if (!available)
0170         return NULL;
0171 
0172     if (size > JPG_FRAME_BUF_SIZE) {
0173         ALOGE("The buffer size requested is too large");
0174         return NULL;
0175     }
0176     mArgs.in_buf = (char *)ioctl(mDevFd, IOCTL_JPG_GET_FRMBUF, mArgs.mmapped_addr);
0177     return (void *)(mArgs.in_buf);
0178 }
0179 
0180 void* JpegEncoder::getOutBuf(uint64_t *size)
0181 {
0182     if (!available)
0183         return NULL;
0184 
0185     if (mArgs.enc_param->file_size <= 0) {
0186         ALOGE("The buffer requested doesn't have data");
0187         return NULL;
0188     }
0189     mArgs.out_buf = (char *)ioctl(mDevFd, IOCTL_JPG_GET_STRBUF, mArgs.mmapped_addr);
0190     *size = mArgs.enc_param->file_size;
0191     return (void *)(mArgs.out_buf);
0192 }
0193 
0194 void* JpegEncoder::getThumbInBuf(uint64_t size)
0195 {
0196     if (!available)
0197         return NULL;
0198 
0199     if (size > JPG_FRAME_THUMB_BUF_SIZE) {
0200         ALOGE("The buffer size requested is too large");
0201         return NULL;
0202     }
0203     mArgs.in_thumb_buf = (char *)ioctl(mDevFd, IOCTL_JPG_GET_THUMB_FRMBUF, mArgs.mmapped_addr);
0204     return (void *)(mArgs.in_thumb_buf);
0205 }
0206 
0207 void* JpegEncoder::getThumbOutBuf(uint64_t *size)
0208 {
0209     if (!available)
0210         return NULL;
0211 
0212     if (mArgs.thumb_enc_param->file_size <= 0) {
0213         ALOGE("The buffer requested doesn't have data");
0214         return NULL;
0215     }
0216     mArgs.out_thumb_buf = (char *)ioctl(mDevFd, IOCTL_JPG_GET_THUMB_STRBUF, mArgs.mmapped_addr);
0217     *size = mArgs.thumb_enc_param->file_size;
0218     return (void *)(mArgs.out_thumb_buf);
0219 }
0220 
0221 jpg_return_status JpegEncoder::encode(unsigned int *size, exif_attribute_t *exifInfo)
0222 {
0223     if (!available)
0224         return JPG_FAIL;
0225 
0226     ALOGD("encode E");
0227 
0228     jpg_return_status ret = JPG_FAIL;
0229     unsigned char *exifOut = NULL;
0230     jpg_enc_proc_param *param = mArgs.enc_param;
0231 
0232     ret = checkMcu(param->sample_mode, param->width, param->height, false);
0233     if (ret != JPG_SUCCESS)
0234         return ret;
0235 
0236     param->enc_type = JPG_MAIN;
0237     ret = (jpg_return_status)ioctl(mDevFd, IOCTL_JPG_ENCODE, &mArgs);
0238     if (ret != JPG_SUCCESS) {
0239         ALOGE("Failed to encode main image");
0240         return ret;
0241     }
0242 
0243     mArgs.out_buf = (char *)ioctl(mDevFd, IOCTL_JPG_GET_STRBUF, mArgs.mmapped_addr);
0244 
0245     if (exifInfo) {
0246         unsigned int thumbLen, exifLen;
0247 
0248         uint_t bufSize = 0;
0249         if (exifInfo->enableThumb) {
0250             ret = encodeThumbImg(&thumbLen);
0251             if (ret != JPG_SUCCESS) {
0252                 ALOGE("Failed to encode for thumbnail image");
0253                 bufSize = EXIF_FILE_SIZE;
0254                 exifInfo->enableThumb = false;
0255             } else {
0256                 bufSize = EXIF_FILE_SIZE + thumbLen;
0257             }
0258         } else {
0259             bufSize = EXIF_FILE_SIZE;
0260         }
0261 
0262         if (mArgs.enc_param->file_size + bufSize > JPG_TOTAL_BUF_SIZE)
0263             return ret;
0264 
0265         exifOut = new unsigned char[bufSize];
0266         if (exifOut == NULL) {
0267             ALOGE("Failed to allocate for exifOut");
0268             return ret;
0269         }
0270         memset(exifOut, 0, bufSize);
0271 
0272         ret = makeExif (exifOut, exifInfo, &exifLen);
0273         if (ret != JPG_SUCCESS) {
0274             ALOGE("Failed to make EXIF");
0275             delete[] exifOut;
0276             return ret;
0277         }
0278 
0279         memmove(&mArgs.out_buf[exifLen + 2], &mArgs.out_buf[2], param->file_size - 2);
0280         memcpy(&mArgs.out_buf[2], exifOut, exifLen);
0281         param->file_size += exifLen;
0282     }
0283 
0284     delete[] exifOut;
0285 
0286     *size = param->file_size;
0287 
0288 #if MAIN_DUMP
0289     FILE *fout = NULL;
0290     char file_name[50] = "/data/main.jpg";
0291     fout = fopen(file_name, "wb");
0292     if (!fout)
0293         perror(&file_name[0]);
0294     size_t nwrite = fwrite(mArgs.out_buf, sizeof(char), param->file_size, fout);
0295     fclose(fout);
0296 #endif
0297 
0298     ALOGD("encode X");
0299 
0300     return ret;
0301 }
0302 
0303 jpg_return_status JpegEncoder::encodeThumbImg(unsigned int *size, bool useMain)
0304 {
0305     if (!available)
0306         return JPG_FAIL;
0307 
0308     ALOGD("encodeThumbImg E");
0309 
0310     jpg_return_status ret = JPG_FAIL;
0311     jpg_enc_proc_param *param = mArgs.thumb_enc_param;
0312 
0313     if (useMain) {
0314         mArgs.in_thumb_buf = (char *)getThumbInBuf(param->width*param->height*2);
0315         if (mArgs.in_thumb_buf == NULL) {
0316             ALOGE("Failed to get the buffer for thumbnail");
0317             return JPG_FAIL;
0318         }
0319 
0320         ret = (jpg_return_status)scaleDownYuv422(mArgs.in_buf,
0321                                                  mArgs.enc_param->width,
0322                                                  mArgs.enc_param->height,
0323                                                  mArgs.in_thumb_buf,
0324                                                  param->width,
0325                                                  param->height);
0326         if (ret != JPG_SUCCESS)
0327             return JPG_FAIL;
0328     }
0329 
0330     ret = checkMcu(param->sample_mode, param->width, param->height, true);
0331     if (ret != JPG_SUCCESS)
0332         return JPG_FAIL;
0333 
0334     mArgs.enc_param->enc_type = JPG_THUMBNAIL;
0335     ret = (jpg_return_status)ioctl(mDevFd, IOCTL_JPG_ENCODE, &mArgs);
0336     if (ret != JPG_SUCCESS) {
0337         ALOGE("Failed to encode for thumbnail");
0338         return JPG_FAIL;
0339     }
0340 
0341     mArgs.out_thumb_buf = (char *)ioctl(mDevFd, IOCTL_JPG_GET_THUMB_STRBUF, mArgs.mmapped_addr);
0342 
0343 #if THUMB_DUMP
0344     FILE *fout = NULL;
0345     char file_name[50] = "/data/thumb.jpg";
0346     fout = fopen(file_name, "wb");
0347     if (!fout)
0348         perror(&file_name[0]);
0349     size_t nwrite = fwrite(mArgs.out_thumb_buf, sizeof(char), param->file_size, fout);
0350     fclose(fout);
0351 #endif
0352 
0353     ALOGD("encodeThumbImg X");
0354 
0355     return JPG_SUCCESS;
0356 }
0357 
0358 jpg_return_status JpegEncoder::makeExif (unsigned char *exifOut,
0359                                         exif_attribute_t *exifInfo,
0360                                         unsigned int *size,
0361                                         bool useMainbufForThumb)
0362 {
0363     if (!available)
0364         return JPG_FAIL;
0365 
0366     ALOGD("makeExif E");
0367 
0368     unsigned char *pCur, *pApp1Start, *pIfdStart, *pGpsIfdPtr, *pNextIfdOffset;
0369     unsigned int tmp, LongerTagOffest = 0;
0370     pApp1Start = pCur = exifOut;
0371 
0372     //2 Exif Identifier Code & TIFF Header
0373     pCur += 4;  // Skip 4 Byte for APP1 marker and length
0374     unsigned char ExifIdentifierCode[6] = { 0x45, 0x78, 0x69, 0x66, 0x00, 0x00 };
0375     memcpy(pCur, ExifIdentifierCode, 6);
0376     pCur += 6;
0377 
0378     /* Byte Order - little endian, Offset of IFD - 0x00000008.H */
0379     unsigned char TiffHeader[8] = { 0x49, 0x49, 0x2A, 0x00, 0x08, 0x00, 0x00, 0x00 };
0380     memcpy(pCur, TiffHeader, 8);
0381     pIfdStart = pCur;
0382     pCur += 8;
0383 
0384     //2 0th IFD TIFF Tags
0385     if (exifInfo->enableGps)
0386         tmp = NUM_0TH_IFD_TIFF;
0387     else
0388         tmp = NUM_0TH_IFD_TIFF - 1;
0389 
0390     memcpy(pCur, &tmp, NUM_SIZE);
0391     pCur += NUM_SIZE;
0392 
0393     LongerTagOffest += 8 + NUM_SIZE + tmp*IFD_SIZE + OFFSET_SIZE;
0394 
0395     writeExifIfd(&pCur, EXIF_TAG_IMAGE_WIDTH, EXIF_TYPE_LONG,
0396                  1, exifInfo->width);
0397     writeExifIfd(&pCur, EXIF_TAG_IMAGE_HEIGHT, EXIF_TYPE_LONG,
0398                  1, exifInfo->height);
0399     writeExifIfd(&pCur, EXIF_TAG_MAKE, EXIF_TYPE_ASCII,
0400                  strlen((char *)exifInfo->maker) + 1, exifInfo->maker, &LongerTagOffest, pIfdStart);
0401     writeExifIfd(&pCur, EXIF_TAG_MODEL, EXIF_TYPE_ASCII,
0402                  strlen((char *)exifInfo->model) + 1, exifInfo->model, &LongerTagOffest, pIfdStart);
0403     writeExifIfd(&pCur, EXIF_TAG_ORIENTATION, EXIF_TYPE_SHORT,
0404                  1, exifInfo->orientation);
0405     writeExifIfd(&pCur, EXIF_TAG_SOFTWARE, EXIF_TYPE_ASCII,
0406                  strlen((char *)exifInfo->software) + 1, exifInfo->software, &LongerTagOffest, pIfdStart);
0407     writeExifIfd(&pCur, EXIF_TAG_DATE_TIME, EXIF_TYPE_ASCII,
0408                  20, exifInfo->date_time, &LongerTagOffest, pIfdStart);
0409     writeExifIfd(&pCur, EXIF_TAG_YCBCR_POSITIONING, EXIF_TYPE_SHORT,
0410                  1, exifInfo->ycbcr_positioning);
0411     writeExifIfd(&pCur, EXIF_TAG_EXIF_IFD_POINTER, EXIF_TYPE_LONG,
0412                  1, LongerTagOffest);
0413     if (exifInfo->enableGps) {
0414         pGpsIfdPtr = pCur;
0415         pCur += IFD_SIZE;   // Skip a ifd size for gps IFD pointer
0416     }
0417 
0418     pNextIfdOffset = pCur;  // Skip a offset size for next IFD offset
0419     pCur += OFFSET_SIZE;
0420 
0421     //2 0th IFD Exif Private Tags
0422     pCur = pIfdStart + LongerTagOffest;
0423 
0424     tmp = NUM_0TH_IFD_EXIF;
0425     memcpy(pCur, &tmp , NUM_SIZE);
0426     pCur += NUM_SIZE;
0427 
0428     LongerTagOffest += NUM_SIZE + NUM_0TH_IFD_EXIF*IFD_SIZE + OFFSET_SIZE;
0429 
0430     writeExifIfd(&pCur, EXIF_TAG_EXPOSURE_TIME, EXIF_TYPE_RATIONAL,
0431                  1, &exifInfo->exposure_time, &LongerTagOffest, pIfdStart);
0432     writeExifIfd(&pCur, EXIF_TAG_FNUMBER, EXIF_TYPE_RATIONAL,
0433                  1, &exifInfo->fnumber, &LongerTagOffest, pIfdStart);
0434     writeExifIfd(&pCur, EXIF_TAG_EXPOSURE_PROGRAM, EXIF_TYPE_SHORT,
0435                  1, exifInfo->exposure_program);
0436     writeExifIfd(&pCur, EXIF_TAG_ISO_SPEED_RATING, EXIF_TYPE_SHORT,
0437                  1, exifInfo->iso_speed_rating);
0438     writeExifIfd(&pCur, EXIF_TAG_EXIF_VERSION, EXIF_TYPE_UNDEFINED,
0439                  4, exifInfo->exif_version);
0440     writeExifIfd(&pCur, EXIF_TAG_DATE_TIME_ORG, EXIF_TYPE_ASCII,
0441                  20, exifInfo->date_time, &LongerTagOffest, pIfdStart);
0442     writeExifIfd(&pCur, EXIF_TAG_DATE_TIME_DIGITIZE, EXIF_TYPE_ASCII,
0443                  20, exifInfo->date_time, &LongerTagOffest, pIfdStart);
0444     writeExifIfd(&pCur, EXIF_TAG_SHUTTER_SPEED, EXIF_TYPE_SRATIONAL,
0445                  1, (rational_t *)&exifInfo->shutter_speed, &LongerTagOffest, pIfdStart);
0446     writeExifIfd(&pCur, EXIF_TAG_APERTURE, EXIF_TYPE_RATIONAL,
0447                  1, &exifInfo->aperture, &LongerTagOffest, pIfdStart);
0448     writeExifIfd(&pCur, EXIF_TAG_BRIGHTNESS, EXIF_TYPE_SRATIONAL,
0449                  1, (rational_t *)&exifInfo->brightness, &LongerTagOffest, pIfdStart);
0450     writeExifIfd(&pCur, EXIF_TAG_EXPOSURE_BIAS, EXIF_TYPE_SRATIONAL,
0451                  1, (rational_t *)&exifInfo->exposure_bias, &LongerTagOffest, pIfdStart);
0452     writeExifIfd(&pCur, EXIF_TAG_MAX_APERTURE, EXIF_TYPE_RATIONAL,
0453                  1, &exifInfo->max_aperture, &LongerTagOffest, pIfdStart);
0454     writeExifIfd(&pCur, EXIF_TAG_METERING_MODE, EXIF_TYPE_SHORT,
0455                  1, exifInfo->metering_mode);
0456     writeExifIfd(&pCur, EXIF_TAG_FLASH, EXIF_TYPE_SHORT,
0457                  1, exifInfo->flash);
0458     writeExifIfd(&pCur, EXIF_TAG_FOCAL_LENGTH, EXIF_TYPE_RATIONAL,
0459                  1, &exifInfo->focal_length, &LongerTagOffest, pIfdStart);
0460     char code[8] = { 0x00, 0x00, 0x00, 0x49, 0x49, 0x43, 0x53, 0x41 };
0461     int commentsLen = strlen((char *)exifInfo->user_comment) + 1;
0462     memmove(exifInfo->user_comment + sizeof(code), exifInfo->user_comment, commentsLen);
0463     memcpy(exifInfo->user_comment, code, sizeof(code));
0464     writeExifIfd(&pCur, EXIF_TAG_USER_COMMENT, EXIF_TYPE_UNDEFINED,
0465                  commentsLen + sizeof(code), exifInfo->user_comment, &LongerTagOffest, pIfdStart);
0466     writeExifIfd(&pCur, EXIF_TAG_COLOR_SPACE, EXIF_TYPE_SHORT,
0467                  1, exifInfo->color_space);
0468     writeExifIfd(&pCur, EXIF_TAG_PIXEL_X_DIMENSION, EXIF_TYPE_LONG,
0469                  1, exifInfo->width);
0470     writeExifIfd(&pCur, EXIF_TAG_PIXEL_Y_DIMENSION, EXIF_TYPE_LONG,
0471                  1, exifInfo->height);
0472     writeExifIfd(&pCur, EXIF_TAG_EXPOSURE_MODE, EXIF_TYPE_LONG,
0473                  1, exifInfo->exposure_mode);
0474     writeExifIfd(&pCur, EXIF_TAG_WHITE_BALANCE, EXIF_TYPE_LONG,
0475                  1, exifInfo->white_balance);
0476     writeExifIfd(&pCur, EXIF_TAG_SCENCE_CAPTURE_TYPE, EXIF_TYPE_LONG,
0477                  1, exifInfo->scene_capture_type);
0478     tmp = 0;
0479     memcpy(pCur, &tmp, OFFSET_SIZE); // next IFD offset
0480     pCur += OFFSET_SIZE;
0481 
0482     //2 0th IFD GPS Info Tags
0483     if (exifInfo->enableGps) {
0484         writeExifIfd(&pGpsIfdPtr, EXIF_TAG_GPS_IFD_POINTER, EXIF_TYPE_LONG,
0485                      1, LongerTagOffest); // GPS IFD pointer skipped on 0th IFD
0486 
0487         pCur = pIfdStart + LongerTagOffest;
0488 
0489         if (exifInfo->gps_processing_method[0] == 0) {
0490             // don't create GPS_PROCESSING_METHOD tag if there isn't any
0491             tmp = NUM_0TH_IFD_GPS - 1;
0492         } else {
0493             tmp = NUM_0TH_IFD_GPS;
0494         }
0495         memcpy(pCur, &tmp, NUM_SIZE);
0496         pCur += NUM_SIZE;
0497 
0498         LongerTagOffest += NUM_SIZE + tmp*IFD_SIZE + OFFSET_SIZE;
0499 
0500         writeExifIfd(&pCur, EXIF_TAG_GPS_VERSION_ID, EXIF_TYPE_BYTE,
0501                      4, exifInfo->gps_version_id);
0502         writeExifIfd(&pCur, EXIF_TAG_GPS_LATITUDE_REF, EXIF_TYPE_ASCII,
0503                      2, exifInfo->gps_latitude_ref);
0504         writeExifIfd(&pCur, EXIF_TAG_GPS_LATITUDE, EXIF_TYPE_RATIONAL,
0505                      3, exifInfo->gps_latitude, &LongerTagOffest, pIfdStart);
0506         writeExifIfd(&pCur, EXIF_TAG_GPS_LONGITUDE_REF, EXIF_TYPE_ASCII,
0507                      2, exifInfo->gps_longitude_ref);
0508         writeExifIfd(&pCur, EXIF_TAG_GPS_LONGITUDE, EXIF_TYPE_RATIONAL,
0509                      3, exifInfo->gps_longitude, &LongerTagOffest, pIfdStart);
0510         writeExifIfd(&pCur, EXIF_TAG_GPS_ALTITUDE_REF, EXIF_TYPE_BYTE,
0511                      1, exifInfo->gps_altitude_ref);
0512         writeExifIfd(&pCur, EXIF_TAG_GPS_ALTITUDE, EXIF_TYPE_RATIONAL,
0513                      1, &exifInfo->gps_altitude, &LongerTagOffest, pIfdStart);
0514         writeExifIfd(&pCur, EXIF_TAG_GPS_TIMESTAMP, EXIF_TYPE_RATIONAL,
0515                      3, exifInfo->gps_timestamp, &LongerTagOffest, pIfdStart);
0516         tmp = strlen((char*)exifInfo->gps_processing_method);
0517     if (tmp > 0) {
0518             if (tmp > 100) {
0519                 tmp = 100;
0520             }
0521             unsigned char tmp_buf[100+sizeof(ExifAsciiPrefix)];
0522             memcpy(tmp_buf, ExifAsciiPrefix, sizeof(ExifAsciiPrefix));
0523             memcpy(&tmp_buf[sizeof(ExifAsciiPrefix)], exifInfo->gps_processing_method, tmp);
0524             writeExifIfd(&pCur, EXIF_TAG_GPS_PROCESSING_METHOD, EXIF_TYPE_UNDEFINED,
0525                          tmp+sizeof(ExifAsciiPrefix), tmp_buf, &LongerTagOffest, pIfdStart);
0526         }
0527         writeExifIfd(&pCur, EXIF_TAG_GPS_DATESTAMP, EXIF_TYPE_ASCII,
0528                      11, exifInfo->gps_datestamp, &LongerTagOffest, pIfdStart);
0529         tmp = 0;
0530         memcpy(pCur, &tmp, OFFSET_SIZE); // next IFD offset
0531         pCur += OFFSET_SIZE;
0532     }
0533 
0534     //2 1th IFD TIFF Tags
0535     char *thumbBuf;
0536     int thumbSize;
0537 
0538     if (useMainbufForThumb) {
0539         thumbBuf = mArgs.out_buf;
0540         thumbSize = mArgs.enc_param->file_size;
0541     } else {
0542         thumbBuf = mArgs.out_thumb_buf;
0543         thumbSize = mArgs.thumb_enc_param->file_size;
0544     }
0545 
0546     if (exifInfo->enableThumb && (thumbBuf != NULL) && (thumbSize > 0)) {
0547         tmp = LongerTagOffest;
0548         memcpy(pNextIfdOffset, &tmp, OFFSET_SIZE);  // NEXT IFD offset skipped on 0th IFD
0549 
0550         pCur = pIfdStart + LongerTagOffest;
0551 
0552         tmp = NUM_1TH_IFD_TIFF;
0553         memcpy(pCur, &tmp, NUM_SIZE);
0554         pCur += NUM_SIZE;
0555 
0556         LongerTagOffest += NUM_SIZE + NUM_1TH_IFD_TIFF*IFD_SIZE + OFFSET_SIZE;
0557 
0558         writeExifIfd(&pCur, EXIF_TAG_IMAGE_WIDTH, EXIF_TYPE_LONG,
0559                      1, exifInfo->widthThumb);
0560         writeExifIfd(&pCur, EXIF_TAG_IMAGE_HEIGHT, EXIF_TYPE_LONG,
0561                      1, exifInfo->heightThumb);
0562         writeExifIfd(&pCur, EXIF_TAG_COMPRESSION_SCHEME, EXIF_TYPE_SHORT,
0563                      1, exifInfo->compression_scheme);
0564         writeExifIfd(&pCur, EXIF_TAG_ORIENTATION, EXIF_TYPE_SHORT,
0565                      1, exifInfo->orientation);
0566         writeExifIfd(&pCur, EXIF_TAG_X_RESOLUTION, EXIF_TYPE_RATIONAL,
0567                      1, &exifInfo->x_resolution, &LongerTagOffest, pIfdStart);
0568         writeExifIfd(&pCur, EXIF_TAG_Y_RESOLUTION, EXIF_TYPE_RATIONAL,
0569                      1, &exifInfo->y_resolution, &LongerTagOffest, pIfdStart);
0570         writeExifIfd(&pCur, EXIF_TAG_RESOLUTION_UNIT, EXIF_TYPE_SHORT,
0571                      1, exifInfo->resolution_unit);
0572         writeExifIfd(&pCur, EXIF_TAG_JPEG_INTERCHANGE_FORMAT, EXIF_TYPE_LONG,
0573                      1, LongerTagOffest);
0574         writeExifIfd(&pCur, EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LEN, EXIF_TYPE_LONG,
0575                      1, thumbSize);
0576 
0577         tmp = 0;
0578         memcpy(pCur, &tmp, OFFSET_SIZE); // next IFD offset
0579         pCur += OFFSET_SIZE;
0580 
0581         memcpy(pIfdStart + LongerTagOffest,
0582                thumbBuf, thumbSize);
0583         LongerTagOffest += thumbSize;
0584     } else {
0585         tmp = 0;
0586         memcpy(pNextIfdOffset, &tmp, OFFSET_SIZE);  // NEXT IFD offset skipped on 0th IFD
0587     }
0588 
0589     unsigned char App1Marker[2] = { 0xff, 0xe1 };
0590     memcpy(pApp1Start, App1Marker, 2);
0591     pApp1Start += 2;
0592 
0593     *size = 10 + LongerTagOffest;
0594     tmp = *size - 2;    // APP1 Maker isn't counted
0595     unsigned char size_mm[2] = {(tmp >> 8) & 0xFF, tmp & 0xFF};
0596     memcpy(pApp1Start, size_mm, 2);
0597 
0598     ALOGD("makeExif X");
0599 
0600     return JPG_SUCCESS;
0601 }
0602 
0603 jpg_return_status JpegEncoder::checkMcu(sample_mode_t sampleMode,
0604                                         uint32_t width, uint32_t height, bool isThumb)
0605 {
0606     if (!available)
0607         return JPG_FAIL;
0608 
0609     uint32_t expectedWidth = width;
0610     uint32_t expectedHeight = height;
0611 
0612     switch (sampleMode){
0613     case JPG_422:
0614         if (width % 16 != 0)
0615             expectedWidth = width + 16 - (width % 16);
0616         if (height % 8 != 0)
0617             expectedHeight = height + 8 - (height % 8);
0618         break;
0619 
0620     case JPG_420:
0621         if (width % 16 != 0)
0622             expectedWidth = width + 16 - (width % 16);
0623         if (height % 16 != 0)
0624             expectedHeight = height + 16 - (height % 16);
0625         break;
0626 
0627     default:
0628         ALOGE("Invaild sample mode");
0629         return JPG_FAIL;
0630     }
0631 
0632     if (expectedWidth == width && expectedHeight == height)
0633         return JPG_SUCCESS;
0634 
0635     ALOGW("The image is not matched for MCU");
0636 
0637     uint32_t size = width*height * 2;
0638     char *srcBuf, *dstBuf;
0639 
0640     if ((srcBuf = new char[size]) == NULL) {
0641         ALOGE("Failed to allocate for srcBuf");
0642         return JPG_FAIL;
0643     }
0644 
0645     if (!isThumb)
0646         dstBuf = mArgs.in_buf;
0647     else
0648         dstBuf = mArgs.in_thumb_buf;
0649 
0650     memcpy(srcBuf, dstBuf, size);
0651     bool ret = pad(srcBuf, width, height, dstBuf, expectedWidth, expectedHeight);
0652 
0653     delete[] srcBuf;
0654 
0655     return JPG_SUCCESS;
0656 }
0657 
0658 bool JpegEncoder::pad(char *srcBuf, uint32_t srcWidth, uint32_t srcHight,
0659                         char *dstBuf, uint32_t dstWidth, uint32_t dstHight)
0660 {
0661     if (!available)
0662         return false;
0663 
0664     if (srcBuf == NULL || dstBuf == NULL) {
0665         ALOGE("srcBuf or dstBuf is NULL");
0666         return false;
0667     }
0668 
0669     int padW = dstWidth - srcWidth;
0670     int padH = dstHight - srcHight;
0671 
0672     if ((int)(dstWidth - srcWidth) < 0 ||
0673             (int)(dstHight - srcHight) < 0) {
0674         ALOGE("dstSize is smaller than srcSize");
0675         return false;
0676     }
0677     memset(dstBuf, 0, dstWidth*dstHight * 2);
0678 
0679     for (uint32_t i = 0; i < srcHight; i++)
0680         memcpy(dstBuf + i * dstWidth * 2, srcBuf + i * srcWidth * 2, srcWidth * 2);
0681 
0682     return true;
0683 }
0684 
0685 bool JpegEncoder::scaleDownYuv422(char *srcBuf, uint32_t srcWidth, uint32_t srcHight,
0686                                   char *dstBuf, uint32_t dstWidth, uint32_t dstHight)
0687 {
0688     if (!available)
0689         return false;
0690 
0691     int32_t step_x, step_y;
0692     int32_t iXsrc, iXdst;
0693     int32_t x, y, src_y_start_pos, dst_pos, src_pos;
0694 
0695     if (dstWidth % 2 != 0 || dstHight % 2 != 0){
0696         ALOGE("scale_down_yuv422: invalid width, height for scaling");
0697         return false;
0698     }
0699 
0700     step_x = srcWidth / dstWidth;
0701     step_y = srcHight / dstHight;
0702 
0703     dst_pos = 0;
0704     for (uint32_t y = 0; y < dstHight; y++) {
0705         src_y_start_pos = (y * step_y * (srcWidth * 2));
0706 
0707         for (uint32_t x = 0; x < dstWidth; x += 2) {
0708             src_pos = src_y_start_pos + (x * (step_x * 2));
0709 
0710             dstBuf[dst_pos++] = srcBuf[src_pos    ];
0711             dstBuf[dst_pos++] = srcBuf[src_pos + 1];
0712             dstBuf[dst_pos++] = srcBuf[src_pos + 2];
0713             dstBuf[dst_pos++] = srcBuf[src_pos + 3];
0714         }
0715     }
0716 
0717     return true;
0718 }
0719 
0720 inline void JpegEncoder::writeExifIfd(unsigned char **pCur,
0721                                          unsigned short tag,
0722                                          unsigned short type,
0723                                          unsigned int count,
0724                                          uint32_t value)
0725 {
0726     memcpy(*pCur, &tag, 2);
0727     *pCur += 2;
0728     memcpy(*pCur, &type, 2);
0729     *pCur += 2;
0730     memcpy(*pCur, &count, 4);
0731     *pCur += 4;
0732     memcpy(*pCur, &value, 4);
0733     *pCur += 4;
0734 }
0735 
0736 inline void JpegEncoder::writeExifIfd(unsigned char **pCur,
0737                                          unsigned short tag,
0738                                          unsigned short type,
0739                                          unsigned int count,
0740                                          unsigned char *pValue)
0741 {
0742     char buf[4] = { 0,};
0743 
0744     memcpy(buf, pValue, count);
0745     memcpy(*pCur, &tag, 2);
0746     *pCur += 2;
0747     memcpy(*pCur, &type, 2);
0748     *pCur += 2;
0749     memcpy(*pCur, &count, 4);
0750     *pCur += 4;
0751     memcpy(*pCur, buf, 4);
0752     *pCur += 4;
0753 }
0754 
0755 
0756 inline void JpegEncoder::writeExifIfd(unsigned char **pCur,
0757                                          unsigned short tag,
0758                                          unsigned short type,
0759                                          unsigned int count,
0760                                          unsigned char *pValue,
0761                                          unsigned int *offset,
0762                                          unsigned char *start)
0763 {
0764     memcpy(*pCur, &tag, 2);
0765     *pCur += 2;
0766     memcpy(*pCur, &type, 2);
0767     *pCur += 2;
0768     memcpy(*pCur, &count, 4);
0769     *pCur += 4;
0770     memcpy(*pCur, offset, 4);
0771     *pCur += 4;
0772     memcpy(start + *offset, pValue, count);
0773     *offset += count;
0774 }
0775 
0776 inline void JpegEncoder::writeExifIfd(unsigned char **pCur,
0777                                          unsigned short tag,
0778                                          unsigned short type,
0779                                          unsigned int count,
0780                                          rational_t *pValue,
0781                                          unsigned int *offset,
0782                                          unsigned char *start)
0783 {
0784     memcpy(*pCur, &tag, 2);
0785     *pCur += 2;
0786     memcpy(*pCur, &type, 2);
0787     *pCur += 2;
0788     memcpy(*pCur, &count, 4);
0789     *pCur += 4;
0790     memcpy(*pCur, offset, 4);
0791     *pCur += 4;
0792     memcpy(start + *offset, pValue, 8 * count);
0793     *offset += 8 * count;
0794 }
0795 
0796 };