blob: 96cee3d9ad8ddfa98c474f996c8a57f5505cdd54 [file] [log] [blame]
Chirayu Desai0a336cc2012-07-12 14:37:05 +05301/*
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 * JPEG DRIVER MODULE (JpegEncoder.cpp)
18 * Author : ge.lee -- initial version
19 * Date : 03 June 2010
20 * Purpose : This file implements the JPEG encoder APIs as needed by Camera HAL
21 */
22#define LOG_TAG "JpegEncoder"
23#define MAIN_DUMP 0
24#define THUMB_DUMP 0
25
26#include <utils/Log.h>
27#include <sys/mman.h>
28#include <fcntl.h>
29
30#include "JpegEncoder.h"
31
32static const char ExifAsciiPrefix[] = { 0x41, 0x53, 0x43, 0x49, 0x49, 0x0, 0x0, 0x0 };
33
34namespace android {
35JpegEncoder::JpegEncoder() : available(false)
36{
37 mArgs.mmapped_addr = (char *)MAP_FAILED;
38 mArgs.enc_param = NULL;
39 mArgs.thumb_enc_param = NULL;
40
41 mDevFd = open(JPG_DRIVER_NAME, O_RDWR);
42 if (mDevFd < 0) {
43 ALOGE("Failed to open the device");
44 return;
45 }
46
Pawit Pornkitprasan674a4f12014-01-03 19:13:37 +070047 // Must be exactly 0, legacy kernel will return 1 despite
48 // the IOCTL being invalid
49 if (ioctl(mDevFd, IOCTL_JPG_GET_INFO, &mInfo) != 0) {
50#ifdef LEGACY_SUPPORT
51 ALOGW("Unable to read driver info. Using legacy values.");
52 mInfo.frame_buf_size = JPG_FRAME_BUF_SIZE;
53 mInfo.thumb_frame_buf_size = JPG_FRAME_THUMB_BUF_SIZE;
54 mInfo.stream_buf_size = JPG_STREAM_BUF_SIZE;
55 mInfo.thumb_stream_buf_size = JPG_STREAM_THUMB_BUF_SIZE;
56 mInfo.total_buf_size = JPG_TOTAL_BUF_SIZE;
57 mInfo.max_width = MAX_JPG_WIDTH;
58 mInfo.max_height = MAX_JPG_HEIGHT;
59 mInfo.max_thumb_width = MAX_JPG_THUMBNAIL_WIDTH;
60 mInfo.max_thumb_height = MAX_JPG_THUMBNAIL_HEIGHT;
61#else
62 ALOGE("Unable to read driver info.");
63 return;
64#endif
65 }
66
Chirayu Desai0a336cc2012-07-12 14:37:05 +053067 mArgs.mmapped_addr = (char *)mmap(0,
Pawit Pornkitprasan674a4f12014-01-03 19:13:37 +070068 mInfo.total_buf_size,
Chirayu Desai0a336cc2012-07-12 14:37:05 +053069 PROT_READ | PROT_WRITE,
70 MAP_SHARED,
71 mDevFd,
72 0);
73
74 if (mArgs.mmapped_addr == MAP_FAILED) {
75 ALOGE("Failed to mmap");
76 return;
77 }
78
79 mArgs.enc_param = new jpg_enc_proc_param;
80 if (mArgs.enc_param == NULL) {
81 ALOGE("Failed to allocate the memory for enc_param");
82 return;
83 }
84 memset(mArgs.enc_param, 0, sizeof(jpg_enc_proc_param));
85
86 mArgs.thumb_enc_param = new jpg_enc_proc_param;
87 if (mArgs.thumb_enc_param == NULL) {
88 ALOGE("Failed to allocate the memory for thumb_enc_param");
89 delete mArgs.enc_param;
90 return;
91 }
92 memset(mArgs.thumb_enc_param, 0, sizeof(jpg_enc_proc_param));
93
94 mArgs.enc_param->sample_mode = JPG_420;
95 mArgs.enc_param->enc_type = JPG_MAIN;
96 mArgs.thumb_enc_param->sample_mode = JPG_420;
97 mArgs.thumb_enc_param->enc_type = JPG_THUMBNAIL;
98
99 available = true;
100}
101
102JpegEncoder::~JpegEncoder()
103{
104 if (mArgs.mmapped_addr != (char*)MAP_FAILED)
Pawit Pornkitprasan674a4f12014-01-03 19:13:37 +0700105 munmap(mArgs.mmapped_addr, mInfo.total_buf_size);
Chirayu Desai0a336cc2012-07-12 14:37:05 +0530106
107 delete mArgs.enc_param;
108
109 delete mArgs.thumb_enc_param;
110
111 if (mDevFd > 0)
112 close(mDevFd);
113}
114
115jpg_return_status JpegEncoder::setConfig(jpeg_conf type, int32_t value)
116{
117 if (!available)
118 return JPG_FAIL;
119
120 jpg_return_status ret = JPG_SUCCESS;
121
122 switch (type) {
123 case JPEG_SET_ENCODE_WIDTH:
Pawit Pornkitprasan674a4f12014-01-03 19:13:37 +0700124 if (value < 0 || value > mInfo.max_width)
Chirayu Desai0a336cc2012-07-12 14:37:05 +0530125 ret = JPG_FAIL;
126 else
127 mArgs.enc_param->width = value;
128 break;
129
130 case JPEG_SET_ENCODE_HEIGHT:
Pawit Pornkitprasan674a4f12014-01-03 19:13:37 +0700131 if (value < 0 || value > mInfo.max_height)
Chirayu Desai0a336cc2012-07-12 14:37:05 +0530132 ret = JPG_FAIL;
133 else
134 mArgs.enc_param->height = value;
135 break;
136
137 case JPEG_SET_ENCODE_QUALITY:
138 if (value < JPG_QUALITY_LEVEL_1 || value > JPG_QUALITY_LEVEL_4)
139 ret = JPG_FAIL;
140 else
141 mArgs.enc_param->quality = (image_quality_type_t)value;
142 break;
143
144 case JPEG_SET_ENCODE_IN_FORMAT:
145 if (value != JPG_MODESEL_YCBCR && value != JPG_MODESEL_RGB) {
146 ret = JPG_FAIL;
147 } else {
148 mArgs.enc_param->in_format = (in_mode_t)value;
149 mArgs.thumb_enc_param->in_format = (in_mode_t)value;
150 }
151 break;
152
153 case JPEG_SET_SAMPING_MODE:
154 if (value != JPG_420 && value != JPG_422) {
155 ret = JPG_FAIL;
156 } else {
157 mArgs.enc_param->sample_mode = (sample_mode_t)value;
158 mArgs.thumb_enc_param->sample_mode = (sample_mode_t)value;
159 }
160 break;
161
162 case JPEG_SET_THUMBNAIL_WIDTH:
Pawit Pornkitprasan674a4f12014-01-03 19:13:37 +0700163 if (value < 0 || value > mInfo.max_thumb_width)
Chirayu Desai0a336cc2012-07-12 14:37:05 +0530164 ret = JPG_FAIL;
165 else
166 mArgs.thumb_enc_param->width = value;
167 break;
168
169 case JPEG_SET_THUMBNAIL_HEIGHT:
Pawit Pornkitprasan674a4f12014-01-03 19:13:37 +0700170 if (value < 0 || value > mInfo.max_thumb_height)
Chirayu Desai0a336cc2012-07-12 14:37:05 +0530171 ret = JPG_FAIL;
172 else
173 mArgs.thumb_enc_param->height = value;
174 break;
175
176 default:
177 ALOGE("Invalid Config type");
178 ret = ERR_UNKNOWN;
179 }
180
181 if (ret == JPG_FAIL)
182 ALOGE("Invalid value(%d) for %d type", value, type);
183
184 return ret;
185}
186
187void* JpegEncoder::getInBuf(uint64_t size)
188{
189 if (!available)
190 return NULL;
191
Pawit Pornkitprasan674a4f12014-01-03 19:13:37 +0700192 if (size > mInfo.frame_buf_size) {
Chirayu Desai0a336cc2012-07-12 14:37:05 +0530193 ALOGE("The buffer size requested is too large");
194 return NULL;
195 }
196 mArgs.in_buf = (char *)ioctl(mDevFd, IOCTL_JPG_GET_FRMBUF, mArgs.mmapped_addr);
197 return (void *)(mArgs.in_buf);
198}
199
200void* JpegEncoder::getOutBuf(uint64_t *size)
201{
202 if (!available)
203 return NULL;
204
205 if (mArgs.enc_param->file_size <= 0) {
206 ALOGE("The buffer requested doesn't have data");
207 return NULL;
208 }
209 mArgs.out_buf = (char *)ioctl(mDevFd, IOCTL_JPG_GET_STRBUF, mArgs.mmapped_addr);
210 *size = mArgs.enc_param->file_size;
211 return (void *)(mArgs.out_buf);
212}
213
214void* JpegEncoder::getThumbInBuf(uint64_t size)
215{
216 if (!available)
217 return NULL;
218
Pawit Pornkitprasan674a4f12014-01-03 19:13:37 +0700219 if (size > mInfo.thumb_frame_buf_size) {
Chirayu Desai0a336cc2012-07-12 14:37:05 +0530220 ALOGE("The buffer size requested is too large");
221 return NULL;
222 }
223 mArgs.in_thumb_buf = (char *)ioctl(mDevFd, IOCTL_JPG_GET_THUMB_FRMBUF, mArgs.mmapped_addr);
224 return (void *)(mArgs.in_thumb_buf);
225}
226
227void* JpegEncoder::getThumbOutBuf(uint64_t *size)
228{
229 if (!available)
230 return NULL;
231
232 if (mArgs.thumb_enc_param->file_size <= 0) {
233 ALOGE("The buffer requested doesn't have data");
234 return NULL;
235 }
236 mArgs.out_thumb_buf = (char *)ioctl(mDevFd, IOCTL_JPG_GET_THUMB_STRBUF, mArgs.mmapped_addr);
237 *size = mArgs.thumb_enc_param->file_size;
238 return (void *)(mArgs.out_thumb_buf);
239}
240
241jpg_return_status JpegEncoder::encode(unsigned int *size, exif_attribute_t *exifInfo)
242{
243 if (!available)
244 return JPG_FAIL;
245
246 ALOGD("encode E");
247
248 jpg_return_status ret = JPG_FAIL;
249 unsigned char *exifOut = NULL;
250 jpg_enc_proc_param *param = mArgs.enc_param;
251
252 ret = checkMcu(param->sample_mode, param->width, param->height, false);
253 if (ret != JPG_SUCCESS)
254 return ret;
255
256 param->enc_type = JPG_MAIN;
257 ret = (jpg_return_status)ioctl(mDevFd, IOCTL_JPG_ENCODE, &mArgs);
258 if (ret != JPG_SUCCESS) {
259 ALOGE("Failed to encode main image");
260 return ret;
261 }
262
263 mArgs.out_buf = (char *)ioctl(mDevFd, IOCTL_JPG_GET_STRBUF, mArgs.mmapped_addr);
264
265 if (exifInfo) {
266 unsigned int thumbLen, exifLen;
267
268 uint_t bufSize = 0;
269 if (exifInfo->enableThumb) {
270 ret = encodeThumbImg(&thumbLen);
271 if (ret != JPG_SUCCESS) {
272 ALOGE("Failed to encode for thumbnail image");
273 bufSize = EXIF_FILE_SIZE;
274 exifInfo->enableThumb = false;
275 } else {
276 bufSize = EXIF_FILE_SIZE + thumbLen;
277 }
278 } else {
279 bufSize = EXIF_FILE_SIZE;
280 }
281
Pawit Pornkitprasan674a4f12014-01-03 19:13:37 +0700282 if (mArgs.enc_param->file_size + bufSize > mInfo.total_buf_size)
Chirayu Desai0a336cc2012-07-12 14:37:05 +0530283 return ret;
284
285 exifOut = new unsigned char[bufSize];
286 if (exifOut == NULL) {
287 ALOGE("Failed to allocate for exifOut");
288 return ret;
289 }
290 memset(exifOut, 0, bufSize);
291
292 ret = makeExif (exifOut, exifInfo, &exifLen);
293 if (ret != JPG_SUCCESS) {
294 ALOGE("Failed to make EXIF");
295 delete[] exifOut;
296 return ret;
297 }
298
299 memmove(&mArgs.out_buf[exifLen + 2], &mArgs.out_buf[2], param->file_size - 2);
300 memcpy(&mArgs.out_buf[2], exifOut, exifLen);
301 param->file_size += exifLen;
302 }
303
304 delete[] exifOut;
305
306 *size = param->file_size;
307
308#if MAIN_DUMP
309 FILE *fout = NULL;
310 char file_name[50] = "/data/main.jpg";
311 fout = fopen(file_name, "wb");
312 if (!fout)
313 perror(&file_name[0]);
314 size_t nwrite = fwrite(mArgs.out_buf, sizeof(char), param->file_size, fout);
315 fclose(fout);
316#endif
317
318 ALOGD("encode X");
319
320 return ret;
321}
322
323jpg_return_status JpegEncoder::encodeThumbImg(unsigned int *size, bool useMain)
324{
325 if (!available)
326 return JPG_FAIL;
327
328 ALOGD("encodeThumbImg E");
329
330 jpg_return_status ret = JPG_FAIL;
331 jpg_enc_proc_param *param = mArgs.thumb_enc_param;
332
333 if (useMain) {
334 mArgs.in_thumb_buf = (char *)getThumbInBuf(param->width*param->height*2);
335 if (mArgs.in_thumb_buf == NULL) {
336 ALOGE("Failed to get the buffer for thumbnail");
337 return JPG_FAIL;
338 }
339
340 ret = (jpg_return_status)scaleDownYuv422(mArgs.in_buf,
341 mArgs.enc_param->width,
342 mArgs.enc_param->height,
343 mArgs.in_thumb_buf,
344 param->width,
345 param->height);
346 if (ret != JPG_SUCCESS)
347 return JPG_FAIL;
348 }
349
350 ret = checkMcu(param->sample_mode, param->width, param->height, true);
351 if (ret != JPG_SUCCESS)
352 return JPG_FAIL;
353
354 mArgs.enc_param->enc_type = JPG_THUMBNAIL;
355 ret = (jpg_return_status)ioctl(mDevFd, IOCTL_JPG_ENCODE, &mArgs);
356 if (ret != JPG_SUCCESS) {
357 ALOGE("Failed to encode for thumbnail");
358 return JPG_FAIL;
359 }
360
361 mArgs.out_thumb_buf = (char *)ioctl(mDevFd, IOCTL_JPG_GET_THUMB_STRBUF, mArgs.mmapped_addr);
362
363#if THUMB_DUMP
364 FILE *fout = NULL;
365 char file_name[50] = "/data/thumb.jpg";
366 fout = fopen(file_name, "wb");
367 if (!fout)
368 perror(&file_name[0]);
369 size_t nwrite = fwrite(mArgs.out_thumb_buf, sizeof(char), param->file_size, fout);
370 fclose(fout);
371#endif
372
373 ALOGD("encodeThumbImg X");
374
375 return JPG_SUCCESS;
376}
377
378jpg_return_status JpegEncoder::makeExif (unsigned char *exifOut,
379 exif_attribute_t *exifInfo,
380 unsigned int *size,
381 bool useMainbufForThumb)
382{
383 if (!available)
384 return JPG_FAIL;
385
386 ALOGD("makeExif E");
387
388 unsigned char *pCur, *pApp1Start, *pIfdStart, *pGpsIfdPtr, *pNextIfdOffset;
389 unsigned int tmp, LongerTagOffest = 0;
390 pApp1Start = pCur = exifOut;
391
392 //2 Exif Identifier Code & TIFF Header
393 pCur += 4; // Skip 4 Byte for APP1 marker and length
394 unsigned char ExifIdentifierCode[6] = { 0x45, 0x78, 0x69, 0x66, 0x00, 0x00 };
395 memcpy(pCur, ExifIdentifierCode, 6);
396 pCur += 6;
397
398 /* Byte Order - little endian, Offset of IFD - 0x00000008.H */
399 unsigned char TiffHeader[8] = { 0x49, 0x49, 0x2A, 0x00, 0x08, 0x00, 0x00, 0x00 };
400 memcpy(pCur, TiffHeader, 8);
401 pIfdStart = pCur;
402 pCur += 8;
403
404 //2 0th IFD TIFF Tags
405 if (exifInfo->enableGps)
406 tmp = NUM_0TH_IFD_TIFF;
407 else
408 tmp = NUM_0TH_IFD_TIFF - 1;
409
410 memcpy(pCur, &tmp, NUM_SIZE);
411 pCur += NUM_SIZE;
412
413 LongerTagOffest += 8 + NUM_SIZE + tmp*IFD_SIZE + OFFSET_SIZE;
414
415 writeExifIfd(&pCur, EXIF_TAG_IMAGE_WIDTH, EXIF_TYPE_LONG,
416 1, exifInfo->width);
417 writeExifIfd(&pCur, EXIF_TAG_IMAGE_HEIGHT, EXIF_TYPE_LONG,
418 1, exifInfo->height);
419 writeExifIfd(&pCur, EXIF_TAG_MAKE, EXIF_TYPE_ASCII,
420 strlen((char *)exifInfo->maker) + 1, exifInfo->maker, &LongerTagOffest, pIfdStart);
421 writeExifIfd(&pCur, EXIF_TAG_MODEL, EXIF_TYPE_ASCII,
422 strlen((char *)exifInfo->model) + 1, exifInfo->model, &LongerTagOffest, pIfdStart);
423 writeExifIfd(&pCur, EXIF_TAG_ORIENTATION, EXIF_TYPE_SHORT,
424 1, exifInfo->orientation);
425 writeExifIfd(&pCur, EXIF_TAG_SOFTWARE, EXIF_TYPE_ASCII,
426 strlen((char *)exifInfo->software) + 1, exifInfo->software, &LongerTagOffest, pIfdStart);
427 writeExifIfd(&pCur, EXIF_TAG_DATE_TIME, EXIF_TYPE_ASCII,
428 20, exifInfo->date_time, &LongerTagOffest, pIfdStart);
429 writeExifIfd(&pCur, EXIF_TAG_YCBCR_POSITIONING, EXIF_TYPE_SHORT,
430 1, exifInfo->ycbcr_positioning);
431 writeExifIfd(&pCur, EXIF_TAG_EXIF_IFD_POINTER, EXIF_TYPE_LONG,
432 1, LongerTagOffest);
433 if (exifInfo->enableGps) {
434 pGpsIfdPtr = pCur;
435 pCur += IFD_SIZE; // Skip a ifd size for gps IFD pointer
436 }
437
438 pNextIfdOffset = pCur; // Skip a offset size for next IFD offset
439 pCur += OFFSET_SIZE;
440
441 //2 0th IFD Exif Private Tags
442 pCur = pIfdStart + LongerTagOffest;
443
444 tmp = NUM_0TH_IFD_EXIF;
445 memcpy(pCur, &tmp , NUM_SIZE);
446 pCur += NUM_SIZE;
447
448 LongerTagOffest += NUM_SIZE + NUM_0TH_IFD_EXIF*IFD_SIZE + OFFSET_SIZE;
449
450 writeExifIfd(&pCur, EXIF_TAG_EXPOSURE_TIME, EXIF_TYPE_RATIONAL,
451 1, &exifInfo->exposure_time, &LongerTagOffest, pIfdStart);
452 writeExifIfd(&pCur, EXIF_TAG_FNUMBER, EXIF_TYPE_RATIONAL,
453 1, &exifInfo->fnumber, &LongerTagOffest, pIfdStart);
454 writeExifIfd(&pCur, EXIF_TAG_EXPOSURE_PROGRAM, EXIF_TYPE_SHORT,
455 1, exifInfo->exposure_program);
456 writeExifIfd(&pCur, EXIF_TAG_ISO_SPEED_RATING, EXIF_TYPE_SHORT,
457 1, exifInfo->iso_speed_rating);
458 writeExifIfd(&pCur, EXIF_TAG_EXIF_VERSION, EXIF_TYPE_UNDEFINED,
459 4, exifInfo->exif_version);
460 writeExifIfd(&pCur, EXIF_TAG_DATE_TIME_ORG, EXIF_TYPE_ASCII,
461 20, exifInfo->date_time, &LongerTagOffest, pIfdStart);
462 writeExifIfd(&pCur, EXIF_TAG_DATE_TIME_DIGITIZE, EXIF_TYPE_ASCII,
463 20, exifInfo->date_time, &LongerTagOffest, pIfdStart);
464 writeExifIfd(&pCur, EXIF_TAG_SHUTTER_SPEED, EXIF_TYPE_SRATIONAL,
465 1, (rational_t *)&exifInfo->shutter_speed, &LongerTagOffest, pIfdStart);
466 writeExifIfd(&pCur, EXIF_TAG_APERTURE, EXIF_TYPE_RATIONAL,
467 1, &exifInfo->aperture, &LongerTagOffest, pIfdStart);
468 writeExifIfd(&pCur, EXIF_TAG_BRIGHTNESS, EXIF_TYPE_SRATIONAL,
469 1, (rational_t *)&exifInfo->brightness, &LongerTagOffest, pIfdStart);
470 writeExifIfd(&pCur, EXIF_TAG_EXPOSURE_BIAS, EXIF_TYPE_SRATIONAL,
471 1, (rational_t *)&exifInfo->exposure_bias, &LongerTagOffest, pIfdStart);
472 writeExifIfd(&pCur, EXIF_TAG_MAX_APERTURE, EXIF_TYPE_RATIONAL,
473 1, &exifInfo->max_aperture, &LongerTagOffest, pIfdStart);
474 writeExifIfd(&pCur, EXIF_TAG_METERING_MODE, EXIF_TYPE_SHORT,
475 1, exifInfo->metering_mode);
476 writeExifIfd(&pCur, EXIF_TAG_FLASH, EXIF_TYPE_SHORT,
477 1, exifInfo->flash);
478 writeExifIfd(&pCur, EXIF_TAG_FOCAL_LENGTH, EXIF_TYPE_RATIONAL,
479 1, &exifInfo->focal_length, &LongerTagOffest, pIfdStart);
480 char code[8] = { 0x00, 0x00, 0x00, 0x49, 0x49, 0x43, 0x53, 0x41 };
481 int commentsLen = strlen((char *)exifInfo->user_comment) + 1;
482 memmove(exifInfo->user_comment + sizeof(code), exifInfo->user_comment, commentsLen);
483 memcpy(exifInfo->user_comment, code, sizeof(code));
484 writeExifIfd(&pCur, EXIF_TAG_USER_COMMENT, EXIF_TYPE_UNDEFINED,
485 commentsLen + sizeof(code), exifInfo->user_comment, &LongerTagOffest, pIfdStart);
486 writeExifIfd(&pCur, EXIF_TAG_COLOR_SPACE, EXIF_TYPE_SHORT,
487 1, exifInfo->color_space);
488 writeExifIfd(&pCur, EXIF_TAG_PIXEL_X_DIMENSION, EXIF_TYPE_LONG,
489 1, exifInfo->width);
490 writeExifIfd(&pCur, EXIF_TAG_PIXEL_Y_DIMENSION, EXIF_TYPE_LONG,
491 1, exifInfo->height);
492 writeExifIfd(&pCur, EXIF_TAG_EXPOSURE_MODE, EXIF_TYPE_LONG,
493 1, exifInfo->exposure_mode);
494 writeExifIfd(&pCur, EXIF_TAG_WHITE_BALANCE, EXIF_TYPE_LONG,
495 1, exifInfo->white_balance);
496 writeExifIfd(&pCur, EXIF_TAG_SCENCE_CAPTURE_TYPE, EXIF_TYPE_LONG,
497 1, exifInfo->scene_capture_type);
498 tmp = 0;
499 memcpy(pCur, &tmp, OFFSET_SIZE); // next IFD offset
500 pCur += OFFSET_SIZE;
501
502 //2 0th IFD GPS Info Tags
503 if (exifInfo->enableGps) {
504 writeExifIfd(&pGpsIfdPtr, EXIF_TAG_GPS_IFD_POINTER, EXIF_TYPE_LONG,
505 1, LongerTagOffest); // GPS IFD pointer skipped on 0th IFD
506
507 pCur = pIfdStart + LongerTagOffest;
508
509 if (exifInfo->gps_processing_method[0] == 0) {
510 // don't create GPS_PROCESSING_METHOD tag if there isn't any
511 tmp = NUM_0TH_IFD_GPS - 1;
512 } else {
513 tmp = NUM_0TH_IFD_GPS;
514 }
515 memcpy(pCur, &tmp, NUM_SIZE);
516 pCur += NUM_SIZE;
517
518 LongerTagOffest += NUM_SIZE + tmp*IFD_SIZE + OFFSET_SIZE;
519
520 writeExifIfd(&pCur, EXIF_TAG_GPS_VERSION_ID, EXIF_TYPE_BYTE,
521 4, exifInfo->gps_version_id);
522 writeExifIfd(&pCur, EXIF_TAG_GPS_LATITUDE_REF, EXIF_TYPE_ASCII,
523 2, exifInfo->gps_latitude_ref);
524 writeExifIfd(&pCur, EXIF_TAG_GPS_LATITUDE, EXIF_TYPE_RATIONAL,
525 3, exifInfo->gps_latitude, &LongerTagOffest, pIfdStart);
526 writeExifIfd(&pCur, EXIF_TAG_GPS_LONGITUDE_REF, EXIF_TYPE_ASCII,
527 2, exifInfo->gps_longitude_ref);
528 writeExifIfd(&pCur, EXIF_TAG_GPS_LONGITUDE, EXIF_TYPE_RATIONAL,
529 3, exifInfo->gps_longitude, &LongerTagOffest, pIfdStart);
530 writeExifIfd(&pCur, EXIF_TAG_GPS_ALTITUDE_REF, EXIF_TYPE_BYTE,
531 1, exifInfo->gps_altitude_ref);
532 writeExifIfd(&pCur, EXIF_TAG_GPS_ALTITUDE, EXIF_TYPE_RATIONAL,
533 1, &exifInfo->gps_altitude, &LongerTagOffest, pIfdStart);
534 writeExifIfd(&pCur, EXIF_TAG_GPS_TIMESTAMP, EXIF_TYPE_RATIONAL,
535 3, exifInfo->gps_timestamp, &LongerTagOffest, pIfdStart);
536 tmp = strlen((char*)exifInfo->gps_processing_method);
537 if (tmp > 0) {
538 if (tmp > 100) {
539 tmp = 100;
540 }
541 unsigned char tmp_buf[100+sizeof(ExifAsciiPrefix)];
542 memcpy(tmp_buf, ExifAsciiPrefix, sizeof(ExifAsciiPrefix));
543 memcpy(&tmp_buf[sizeof(ExifAsciiPrefix)], exifInfo->gps_processing_method, tmp);
544 writeExifIfd(&pCur, EXIF_TAG_GPS_PROCESSING_METHOD, EXIF_TYPE_UNDEFINED,
545 tmp+sizeof(ExifAsciiPrefix), tmp_buf, &LongerTagOffest, pIfdStart);
546 }
547 writeExifIfd(&pCur, EXIF_TAG_GPS_DATESTAMP, EXIF_TYPE_ASCII,
548 11, exifInfo->gps_datestamp, &LongerTagOffest, pIfdStart);
549 tmp = 0;
550 memcpy(pCur, &tmp, OFFSET_SIZE); // next IFD offset
551 pCur += OFFSET_SIZE;
552 }
553
554 //2 1th IFD TIFF Tags
555 char *thumbBuf;
556 int thumbSize;
557
558 if (useMainbufForThumb) {
559 thumbBuf = mArgs.out_buf;
560 thumbSize = mArgs.enc_param->file_size;
561 } else {
562 thumbBuf = mArgs.out_thumb_buf;
563 thumbSize = mArgs.thumb_enc_param->file_size;
564 }
565
566 if (exifInfo->enableThumb && (thumbBuf != NULL) && (thumbSize > 0)) {
567 tmp = LongerTagOffest;
568 memcpy(pNextIfdOffset, &tmp, OFFSET_SIZE); // NEXT IFD offset skipped on 0th IFD
569
570 pCur = pIfdStart + LongerTagOffest;
571
572 tmp = NUM_1TH_IFD_TIFF;
573 memcpy(pCur, &tmp, NUM_SIZE);
574 pCur += NUM_SIZE;
575
576 LongerTagOffest += NUM_SIZE + NUM_1TH_IFD_TIFF*IFD_SIZE + OFFSET_SIZE;
577
578 writeExifIfd(&pCur, EXIF_TAG_IMAGE_WIDTH, EXIF_TYPE_LONG,
579 1, exifInfo->widthThumb);
580 writeExifIfd(&pCur, EXIF_TAG_IMAGE_HEIGHT, EXIF_TYPE_LONG,
581 1, exifInfo->heightThumb);
582 writeExifIfd(&pCur, EXIF_TAG_COMPRESSION_SCHEME, EXIF_TYPE_SHORT,
583 1, exifInfo->compression_scheme);
584 writeExifIfd(&pCur, EXIF_TAG_ORIENTATION, EXIF_TYPE_SHORT,
585 1, exifInfo->orientation);
586 writeExifIfd(&pCur, EXIF_TAG_X_RESOLUTION, EXIF_TYPE_RATIONAL,
587 1, &exifInfo->x_resolution, &LongerTagOffest, pIfdStart);
588 writeExifIfd(&pCur, EXIF_TAG_Y_RESOLUTION, EXIF_TYPE_RATIONAL,
589 1, &exifInfo->y_resolution, &LongerTagOffest, pIfdStart);
590 writeExifIfd(&pCur, EXIF_TAG_RESOLUTION_UNIT, EXIF_TYPE_SHORT,
591 1, exifInfo->resolution_unit);
592 writeExifIfd(&pCur, EXIF_TAG_JPEG_INTERCHANGE_FORMAT, EXIF_TYPE_LONG,
593 1, LongerTagOffest);
594 writeExifIfd(&pCur, EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LEN, EXIF_TYPE_LONG,
595 1, thumbSize);
596
597 tmp = 0;
598 memcpy(pCur, &tmp, OFFSET_SIZE); // next IFD offset
599 pCur += OFFSET_SIZE;
600
601 memcpy(pIfdStart + LongerTagOffest,
602 thumbBuf, thumbSize);
603 LongerTagOffest += thumbSize;
604 } else {
605 tmp = 0;
606 memcpy(pNextIfdOffset, &tmp, OFFSET_SIZE); // NEXT IFD offset skipped on 0th IFD
607 }
608
609 unsigned char App1Marker[2] = { 0xff, 0xe1 };
610 memcpy(pApp1Start, App1Marker, 2);
611 pApp1Start += 2;
612
613 *size = 10 + LongerTagOffest;
614 tmp = *size - 2; // APP1 Maker isn't counted
615 unsigned char size_mm[2] = {(tmp >> 8) & 0xFF, tmp & 0xFF};
616 memcpy(pApp1Start, size_mm, 2);
617
618 ALOGD("makeExif X");
619
620 return JPG_SUCCESS;
621}
622
623jpg_return_status JpegEncoder::checkMcu(sample_mode_t sampleMode,
624 uint32_t width, uint32_t height, bool isThumb)
625{
626 if (!available)
627 return JPG_FAIL;
628
629 uint32_t expectedWidth = width;
630 uint32_t expectedHeight = height;
631
632 switch (sampleMode){
633 case JPG_422:
634 if (width % 16 != 0)
635 expectedWidth = width + 16 - (width % 16);
636 if (height % 8 != 0)
637 expectedHeight = height + 8 - (height % 8);
638 break;
639
640 case JPG_420:
641 if (width % 16 != 0)
642 expectedWidth = width + 16 - (width % 16);
643 if (height % 16 != 0)
644 expectedHeight = height + 16 - (height % 16);
645 break;
646
647 default:
648 ALOGE("Invaild sample mode");
649 return JPG_FAIL;
650 }
651
652 if (expectedWidth == width && expectedHeight == height)
653 return JPG_SUCCESS;
654
655 ALOGW("The image is not matched for MCU");
656
657 uint32_t size = width*height * 2;
658 char *srcBuf, *dstBuf;
659
660 if ((srcBuf = new char[size]) == NULL) {
661 ALOGE("Failed to allocate for srcBuf");
662 return JPG_FAIL;
663 }
664
665 if (!isThumb)
666 dstBuf = mArgs.in_buf;
667 else
668 dstBuf = mArgs.in_thumb_buf;
669
670 memcpy(srcBuf, dstBuf, size);
671 bool ret = pad(srcBuf, width, height, dstBuf, expectedWidth, expectedHeight);
672
673 delete[] srcBuf;
674
675 return JPG_SUCCESS;
676}
677
678bool JpegEncoder::pad(char *srcBuf, uint32_t srcWidth, uint32_t srcHight,
679 char *dstBuf, uint32_t dstWidth, uint32_t dstHight)
680{
681 if (!available)
682 return false;
683
684 if (srcBuf == NULL || dstBuf == NULL) {
685 ALOGE("srcBuf or dstBuf is NULL");
686 return false;
687 }
688
689 int padW = dstWidth - srcWidth;
690 int padH = dstHight - srcHight;
691
692 if ((int)(dstWidth - srcWidth) < 0 ||
693 (int)(dstHight - srcHight) < 0) {
694 ALOGE("dstSize is smaller than srcSize");
695 return false;
696 }
697 memset(dstBuf, 0, dstWidth*dstHight * 2);
698
699 for (uint32_t i = 0; i < srcHight; i++)
700 memcpy(dstBuf + i * dstWidth * 2, srcBuf + i * srcWidth * 2, srcWidth * 2);
701
702 return true;
703}
704
705bool JpegEncoder::scaleDownYuv422(char *srcBuf, uint32_t srcWidth, uint32_t srcHight,
706 char *dstBuf, uint32_t dstWidth, uint32_t dstHight)
707{
708 if (!available)
709 return false;
710
711 int32_t step_x, step_y;
712 int32_t iXsrc, iXdst;
713 int32_t x, y, src_y_start_pos, dst_pos, src_pos;
714
715 if (dstWidth % 2 != 0 || dstHight % 2 != 0){
716 ALOGE("scale_down_yuv422: invalid width, height for scaling");
717 return false;
718 }
719
720 step_x = srcWidth / dstWidth;
721 step_y = srcHight / dstHight;
722
723 dst_pos = 0;
724 for (uint32_t y = 0; y < dstHight; y++) {
725 src_y_start_pos = (y * step_y * (srcWidth * 2));
726
727 for (uint32_t x = 0; x < dstWidth; x += 2) {
728 src_pos = src_y_start_pos + (x * (step_x * 2));
729
730 dstBuf[dst_pos++] = srcBuf[src_pos ];
731 dstBuf[dst_pos++] = srcBuf[src_pos + 1];
732 dstBuf[dst_pos++] = srcBuf[src_pos + 2];
733 dstBuf[dst_pos++] = srcBuf[src_pos + 3];
734 }
735 }
736
737 return true;
738}
739
740inline void JpegEncoder::writeExifIfd(unsigned char **pCur,
741 unsigned short tag,
742 unsigned short type,
743 unsigned int count,
744 uint32_t value)
745{
746 memcpy(*pCur, &tag, 2);
747 *pCur += 2;
748 memcpy(*pCur, &type, 2);
749 *pCur += 2;
750 memcpy(*pCur, &count, 4);
751 *pCur += 4;
752 memcpy(*pCur, &value, 4);
753 *pCur += 4;
754}
755
756inline void JpegEncoder::writeExifIfd(unsigned char **pCur,
757 unsigned short tag,
758 unsigned short type,
759 unsigned int count,
760 unsigned char *pValue)
761{
762 char buf[4] = { 0,};
763
764 memcpy(buf, pValue, count);
765 memcpy(*pCur, &tag, 2);
766 *pCur += 2;
767 memcpy(*pCur, &type, 2);
768 *pCur += 2;
769 memcpy(*pCur, &count, 4);
770 *pCur += 4;
771 memcpy(*pCur, buf, 4);
772 *pCur += 4;
773}
774
775
776inline void JpegEncoder::writeExifIfd(unsigned char **pCur,
777 unsigned short tag,
778 unsigned short type,
779 unsigned int count,
780 unsigned char *pValue,
781 unsigned int *offset,
782 unsigned char *start)
783{
784 memcpy(*pCur, &tag, 2);
785 *pCur += 2;
786 memcpy(*pCur, &type, 2);
787 *pCur += 2;
788 memcpy(*pCur, &count, 4);
789 *pCur += 4;
790 memcpy(*pCur, offset, 4);
791 *pCur += 4;
792 memcpy(start + *offset, pValue, count);
793 *offset += count;
794}
795
796inline void JpegEncoder::writeExifIfd(unsigned char **pCur,
797 unsigned short tag,
798 unsigned short type,
799 unsigned int count,
800 rational_t *pValue,
801 unsigned int *offset,
802 unsigned char *start)
803{
804 memcpy(*pCur, &tag, 2);
805 *pCur += 2;
806 memcpy(*pCur, &type, 2);
807 *pCur += 2;
808 memcpy(*pCur, &count, 4);
809 *pCur += 4;
810 memcpy(*pCur, offset, 4);
811 *pCur += 4;
812 memcpy(start + *offset, pValue, 8 * count);
813 *offset += 8 * count;
814}
815
816};