2 * Copyright (C) 2012 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 //#define LOG_NDEBUG 0
18 #define LOG_TAG "EmulatedCamera2_JpegCompressor"
20 #include <utils/Log.h>
21 #include <ui/GraphicBufferMapper.h>
23 #include "JpegCompressor.h"
24 #include "../EmulatedFakeCamera2.h"
25 #include "../EmulatedFakeCamera3.h"
29 JpegCompressor::JpegCompressor():
37 JpegCompressor::~JpegCompressor() {
38 Mutex::Autolock lock(mMutex);
41 status_t JpegCompressor::start(Buffers *buffers, JpegListener *listener) {
42 if (listener == NULL) {
43 ALOGE("%s: NULL listener not allowed!", __FUNCTION__);
46 Mutex::Autolock lock(mMutex);
48 Mutex::Autolock busyLock(mBusyMutex);
51 ALOGE("%s: Already processing a buffer!", __FUNCTION__);
52 return INVALID_OPERATION;
62 res = run("EmulatedFakeCamera2::JpegCompressor");
64 ALOGE("%s: Unable to start up compression thread: %s (%d)",
65 __FUNCTION__, strerror(-res), res);
71 status_t JpegCompressor::compressSynchronous(Buffers *buffers) {
74 Mutex::Autolock lock(mMutex);
76 Mutex::Autolock busyLock(mBusyMutex);
79 ALOGE("%s: Already processing a buffer!", __FUNCTION__);
80 return INVALID_OPERATION;
95 status_t JpegCompressor::cancel() {
100 status_t JpegCompressor::readyToRun() {
104 bool JpegCompressor::threadLoop() {
106 ALOGV("%s: Starting compression thread", __FUNCTION__);
110 mListener->onJpegDone(mJpegBuffer, res == OK);
117 status_t JpegCompressor::compress() {
118 // Find source and target buffers. Assumes only one buffer matches
121 bool foundJpeg = false, mFoundAux = false;
122 for (size_t i = 0; i < mBuffers->size(); i++) {
123 const StreamBuffer &b = (*mBuffers)[i];
124 if (b.format == HAL_PIXEL_FORMAT_BLOB) {
127 } else if (b.streamId <= 0) {
131 if (mFoundJpeg && mFoundAux) break;
133 if (!mFoundJpeg || !mFoundAux) {
134 ALOGE("%s: Unable to find buffers for JPEG source/destination",
139 // Set up error management
141 mJpegErrorInfo = NULL;
145 mCInfo.err = jpeg_std_error(&error);
146 mCInfo.err->error_exit = jpegErrorHandler;
148 jpeg_create_compress(&mCInfo);
149 if (checkError("Error initializing compression")) return NO_INIT;
151 // Route compressed data straight to output stream buffer
153 JpegDestination jpegDestMgr;
154 jpegDestMgr.parent = this;
155 jpegDestMgr.init_destination = jpegInitDestination;
156 jpegDestMgr.empty_output_buffer = jpegEmptyOutputBuffer;
157 jpegDestMgr.term_destination = jpegTermDestination;
159 mCInfo.dest = &jpegDestMgr;
161 // Set up compression parameters
163 mCInfo.image_width = mAuxBuffer.width;
164 mCInfo.image_height = mAuxBuffer.height;
165 mCInfo.input_components = 3;
166 mCInfo.in_color_space = JCS_RGB;
168 jpeg_set_defaults(&mCInfo);
169 if (checkError("Error configuring defaults")) return NO_INIT;
173 jpeg_start_compress(&mCInfo, TRUE);
174 if (checkError("Error starting compression")) return NO_INIT;
176 size_t rowStride = mAuxBuffer.stride * 3;
177 const size_t kChunkSize = 32;
178 while (mCInfo.next_scanline < mCInfo.image_height) {
179 JSAMPROW chunk[kChunkSize];
180 for (size_t i = 0 ; i < kChunkSize; i++) {
181 chunk[i] = (JSAMPROW)
182 (mAuxBuffer.img + (i + mCInfo.next_scanline) * rowStride);
184 jpeg_write_scanlines(&mCInfo, chunk, kChunkSize);
185 if (checkError("Error while compressing")) return NO_INIT;
187 ALOGV("%s: Cancel called, exiting early", __FUNCTION__);
192 jpeg_finish_compress(&mCInfo);
193 if (checkError("Error while finishing compression")) return NO_INIT;
200 bool JpegCompressor::isBusy() {
201 Mutex::Autolock busyLock(mBusyMutex);
205 bool JpegCompressor::isStreamInUse(uint32_t id) {
206 Mutex::Autolock lock(mBusyMutex);
208 if (mBuffers && mIsBusy) {
209 for (size_t i = 0; i < mBuffers->size(); i++) {
210 if ( (*mBuffers)[i].streamId == (int)id ) return true;
216 bool JpegCompressor::waitForDone(nsecs_t timeout) {
217 Mutex::Autolock lock(mBusyMutex);
220 res = mDone.waitRelative(mBusyMutex, timeout);
225 bool JpegCompressor::checkError(const char *msg) {
226 if (mJpegErrorInfo) {
227 char errBuffer[JMSG_LENGTH_MAX];
228 mJpegErrorInfo->err->format_message(mJpegErrorInfo, errBuffer);
230 __FUNCTION__, msg, errBuffer);
231 mJpegErrorInfo = NULL;
237 void JpegCompressor::cleanUp() {
239 jpeg_destroy_compress(&mCInfo);
240 Mutex::Autolock lock(mBusyMutex);
243 if (mAuxBuffer.streamId == 0) {
244 delete[] mAuxBuffer.img;
245 } else if (!mSynchronous) {
246 mListener->onJpegInputDone(mAuxBuffer);
259 void JpegCompressor::jpegErrorHandler(j_common_ptr cinfo) {
260 JpegError *error = static_cast<JpegError*>(cinfo->err);
261 error->parent->mJpegErrorInfo = cinfo;
264 void JpegCompressor::jpegInitDestination(j_compress_ptr cinfo) {
265 JpegDestination *dest= static_cast<JpegDestination*>(cinfo->dest);
266 ALOGV("%s: Setting destination to %p, size %zu",
267 __FUNCTION__, dest->parent->mJpegBuffer.img, kMaxJpegSize);
268 dest->next_output_byte = (JOCTET*)(dest->parent->mJpegBuffer.img);
269 dest->free_in_buffer = kMaxJpegSize;
272 boolean JpegCompressor::jpegEmptyOutputBuffer(j_compress_ptr cinfo) {
273 ALOGE("%s: JPEG destination buffer overflow!",
278 void JpegCompressor::jpegTermDestination(j_compress_ptr cinfo) {
279 ALOGV("%s: Done writing JPEG data. %zu bytes left in buffer",
280 __FUNCTION__, cinfo->dest->free_in_buffer);
283 JpegCompressor::JpegListener::~JpegListener() {
286 } // namespace android