blob: 42780e501113fda0051bfe936ad4d021a3a3258a
1 | /* |
2 | * Copyright (C) 2011 The Android Open Source Project |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. |
15 | */ |
16 | |
17 | /** |
18 | * @file Encoder_libjpeg.h |
19 | * |
20 | * This defines API for camerahal to encode YUV using libjpeg |
21 | * |
22 | */ |
23 | |
24 | #ifndef ANDROID_CAMERA_HARDWARE_ENCODER_LIBJPEG_H |
25 | #define ANDROID_CAMERA_HARDWARE_ENCODER_LIBJPEG_H |
26 | |
27 | #include <utils/threads.h> |
28 | #include <utils/RefBase.h> |
29 | |
30 | #ifdef AMLOGIC_HW_JPEGENC |
31 | #include "jpegenc.h" |
32 | #endif |
33 | |
34 | extern "C" { |
35 | #include "jhead.h" |
36 | } |
37 | namespace android { |
38 | /** |
39 | * libjpeg encoder class - uses libjpeg to encode yuv |
40 | */ |
41 | |
42 | #define MAX_EXIF_TAGS_SUPPORTED 30 |
43 | typedef void (*encoder_libjpeg_callback_t) (void* main_jpeg, |
44 | void* thumb_jpeg, |
45 | CameraFrame::FrameType type, |
46 | void* cookie1, |
47 | void* cookie2, |
48 | void* cookie3); |
49 | |
50 | static const char TAG_MODEL[] = "Model"; |
51 | static const char TAG_MAKE[] = "Make"; |
52 | static const char TAG_FOCALLENGTH[] = "FocalLength"; |
53 | static const char TAG_DATETIME[] = "DateTime"; |
54 | static const char TAG_IMAGE_WIDTH[] = "ImageWidth"; |
55 | static const char TAG_IMAGE_LENGTH[] = "ImageLength"; |
56 | static const char TAG_GPS_LAT[] = "GPSLatitude"; |
57 | static const char TAG_GPS_LAT_REF[] = "GPSLatitudeRef"; |
58 | static const char TAG_GPS_LONG[] = "GPSLongitude"; |
59 | static const char TAG_GPS_LONG_REF[] = "GPSLongitudeRef"; |
60 | static const char TAG_GPS_ALT[] = "GPSAltitude"; |
61 | static const char TAG_GPS_ALT_REF[] = "GPSAltitudeRef"; |
62 | static const char TAG_GPS_MAP_DATUM[] = "GPSMapDatum"; |
63 | static const char TAG_GPS_PROCESSING_METHOD[] = "GPSProcessingMethod"; |
64 | static const char TAG_GPS_VERSION_ID[] = "GPSVersionID"; |
65 | static const char TAG_GPS_TIMESTAMP[] = "GPSTimeStamp"; |
66 | static const char TAG_GPS_DATESTAMP[] = "GPSDateStamp"; |
67 | static const char TAG_ORIENTATION[] = "Orientation"; |
68 | |
69 | class ExifElementsTable { |
70 | public: |
71 | ExifElementsTable() : |
72 | gps_tag_count(0), exif_tag_count(0), position(0), |
73 | jpeg_opened(false) { } |
74 | ~ExifElementsTable(); |
75 | |
76 | status_t insertElement(const char* tag, const char* value); |
77 | void insertExifToJpeg(unsigned char* jpeg, size_t jpeg_size); |
78 | status_t insertExifThumbnailImage(const char*, int); |
79 | void saveJpeg(unsigned char* picture, size_t jpeg_size); |
80 | static const char* degreesToExifOrientation(const char*); |
81 | static void stringToRational(const char*, unsigned int*, unsigned int*); |
82 | static bool isAsciiTag(const char* tag); |
83 | private: |
84 | ExifElement_t table[MAX_EXIF_TAGS_SUPPORTED]; |
85 | unsigned int gps_tag_count; |
86 | unsigned int exif_tag_count; |
87 | unsigned int position; |
88 | bool jpeg_opened; |
89 | }; |
90 | |
91 | class Encoder_libjpeg : public Thread { |
92 | /* public member types and variables */ |
93 | public: |
94 | struct params { |
95 | uint8_t* src; |
96 | int src_size; |
97 | uint8_t* dst; |
98 | int dst_size; |
99 | int quality; |
100 | int in_width; |
101 | int in_height; |
102 | int out_width; |
103 | int out_height; |
104 | const char* format; |
105 | size_t jpeg_size; |
106 | }; |
107 | /* private member types and variables */ |
108 | public: |
109 | enum format { |
110 | YUV420SP, |
111 | YUV422I, |
112 | RGB24, |
113 | }; |
114 | /* public member functions */ |
115 | public: |
116 | Encoder_libjpeg(params* main_jpeg, |
117 | params* tn_jpeg, |
118 | encoder_libjpeg_callback_t cb, |
119 | CameraFrame::FrameType type, |
120 | void* cookie1, |
121 | void* cookie2, |
122 | void* cookie3) |
123 | : Thread(false), mMainInput(main_jpeg), mThumbnailInput(tn_jpeg), mCb(cb), |
124 | mCancelEncoding(false), mCookie1(cookie1), mCookie2(cookie2), mCookie3(cookie3), |
125 | mType(type), mThumb(NULL) { |
126 | this->incStrong(this); |
127 | } |
128 | |
129 | ~Encoder_libjpeg() { |
130 | CAMHAL_LOGVB("~Encoder_libjpeg(%p)", this); |
131 | } |
132 | |
133 | virtual bool threadLoop() { |
134 | size_t size = 0; |
135 | sp<Encoder_libjpeg> tn = NULL; |
136 | if (mThumbnailInput) { |
137 | // start thread to encode thumbnail |
138 | mThumb = new Encoder_libjpeg(mThumbnailInput, NULL, NULL, mType, NULL, NULL, NULL); |
139 | mThumb->run(); |
140 | } |
141 | |
142 | // encode our main image |
143 | size = encode(mMainInput); |
144 | |
145 | // check if it is main jpeg thread |
146 | if(mThumb.get()) { |
147 | // wait until tn jpeg thread exits. |
148 | mThumb->join(); |
149 | mThumb.clear(); |
150 | mThumb = NULL; |
151 | } |
152 | |
153 | if(mCb) { |
154 | mCb(mMainInput, mThumbnailInput, mType, mCookie1, mCookie2, mCookie3); |
155 | } |
156 | |
157 | // encoder thread runs, self-destructs, and then exits |
158 | this->decStrong(this); |
159 | return false; |
160 | } |
161 | |
162 | void cancel() { |
163 | if (mThumb.get()) { |
164 | mThumb->cancel(); |
165 | } |
166 | mCancelEncoding = true; |
167 | } |
168 | |
169 | private: |
170 | params* mMainInput; |
171 | params* mThumbnailInput; |
172 | encoder_libjpeg_callback_t mCb; |
173 | bool mCancelEncoding; |
174 | void* mCookie1; |
175 | void* mCookie2; |
176 | void* mCookie3; |
177 | CameraFrame::FrameType mType; |
178 | sp<Encoder_libjpeg> mThumb; |
179 | |
180 | size_t encode(params*); |
181 | #ifdef AMLOGIC_HW_JPEGENC |
182 | hw_jpegenc_t hw_info; |
183 | #endif |
184 | }; |
185 | |
186 | } |
187 | |
188 | #endif |
189 |