summaryrefslogtreecommitdiffstats
path: root/camera/fake-pipeline2/JpegCompressor.cpp
blob: 51de0c02b7d22f69df8784eab8461c09f50d4f39 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

//#define LOG_NDEBUG 0
#define LOG_TAG "EmulatedCamera2_JpegCompressor"

#include <log/log.h>

#include "gralloc_cb.h"
#include "JpegCompressor.h"
#include "../EmulatedFakeCamera2.h"
#include "../EmulatedFakeCamera3.h"
#include "../Exif.h"
#include "../Thumbnail.h"
#include "hardware/camera3.h"

namespace android {

JpegCompressor::JpegCompressor():
        Thread(false),
        mIsBusy(false),
        mSynchronous(false),
        mBuffers(NULL),
        mListener(NULL) {
}

JpegCompressor::~JpegCompressor() {
    Mutex::Autolock lock(mMutex);
}

status_t JpegCompressor::reserve() {
    Mutex::Autolock busyLock(mBusyMutex);
    if (mIsBusy) {
        ALOGE("%s: Already processing a buffer!", __FUNCTION__);
        return INVALID_OPERATION;
    }
    mIsBusy = true;
    return OK;
}

status_t JpegCompressor::start(Buffers *buffers, JpegListener *listener, CameraMetadata* settings) {
    if (listener == NULL) {
        ALOGE("%s: NULL listener not allowed!", __FUNCTION__);
        return BAD_VALUE;
    }
    Mutex::Autolock lock(mMutex);
    {
        Mutex::Autolock busyLock(mBusyMutex);

        if (!mIsBusy) {
            ALOGE("Called start without reserve() first!");
            return INVALID_OPERATION;
        }
        mSynchronous = false;
        mBuffers = buffers;
        mListener = listener;
        if (settings) {
              mSettings = *settings;
        }
    }

    status_t res;
    res = run("EmulatedFakeCamera2::JpegCompressor");
    if (res != OK) {
        ALOGE("%s: Unable to start up compression thread: %s (%d)",
                __FUNCTION__, strerror(-res), res);
        delete mBuffers;
    }
    return res;
}

status_t JpegCompressor::compressSynchronous(Buffers *buffers) {
    status_t res;

    Mutex::Autolock lock(mMutex);
    {
        Mutex::Autolock busyLock(mBusyMutex);

        if (mIsBusy) {
            ALOGE("%s: Already processing a buffer!", __FUNCTION__);
            return INVALID_OPERATION;
        }

        mIsBusy = true;
        mSynchronous = true;
        mBuffers = buffers;
    }

    res = compress();

    cleanUp();

    return res;
}

status_t JpegCompressor::cancel() {
    requestExitAndWait();
    return OK;
}

status_t JpegCompressor::readyToRun() {
    return OK;
}

bool JpegCompressor::threadLoop() {
    status_t res;
    ALOGV("%s: Starting compression thread", __FUNCTION__);

    res = compress();

    mListener->onJpegDone(mJpegBuffer, res == OK);

    cleanUp();

    return false;
}

status_t JpegCompressor::compress() {
    // Find source and target buffers. Assumes only one buffer matches
    // each condition!
    bool mFoundAux = false;
    int thumbWidth = 0, thumbHeight = 0;
    unsigned char thumbJpegQuality = 90;
    unsigned char jpegQuality = 90;
    camera_metadata_entry_t entry;

    for (size_t i = 0; i < mBuffers->size(); i++) {
        const StreamBuffer &b = (*mBuffers)[i];
        if (b.format == HAL_PIXEL_FORMAT_BLOB) {
            mJpegBuffer = b;
            mFoundJpeg = true;
        } else if (b.streamId <= 0) {
            mAuxBuffer = b;
            mFoundAux = true;
        }
        if (mFoundJpeg && mFoundAux) break;
    }
    if (!mFoundJpeg || !mFoundAux) {
        ALOGE("%s: Unable to find buffers for JPEG source/destination",
                __FUNCTION__);
        return BAD_VALUE;
    }

    // Create EXIF data and compress thumbnail
    ExifData* exifData = createExifData(mSettings, mAuxBuffer.width, mAuxBuffer.height);
    entry = mSettings.find(ANDROID_JPEG_THUMBNAIL_SIZE);
    if (entry.count > 0) {
        thumbWidth = entry.data.i32[0];
        thumbHeight = entry.data.i32[1];
    }
    entry = mSettings.find(ANDROID_JPEG_THUMBNAIL_QUALITY);
    if (entry.count > 0) {
        thumbJpegQuality = entry.data.u8[0];
    }
    if (thumbWidth > 0 && thumbHeight > 0) {
        createThumbnail(static_cast<const unsigned char*>(mAuxBuffer.img),
                        mAuxBuffer.width, mAuxBuffer.height,
                        thumbWidth, thumbHeight,
                        thumbJpegQuality, exifData);
    }

    // Compress the image
    entry = mSettings.find(ANDROID_JPEG_QUALITY);
    if (entry.count > 0) {
        jpegQuality = entry.data.u8[0];
    }
    NV21JpegCompressor nV21JpegCompressor;
    nV21JpegCompressor.compressRawImage((void*)mAuxBuffer.img,
                                         mAuxBuffer.width,
                                         mAuxBuffer.height,
                                         jpegQuality, exifData);
    nV21JpegCompressor.getCompressedImage((void*)mJpegBuffer.img);

    // Refer to /hardware/libhardware/include/hardware/camera3.h
    // Transport header for compressed JPEG buffers in output streams.
    camera3_jpeg_blob_t jpeg_blob;
    cb_handle_t *cb = (cb_handle_t *)(*mJpegBuffer.buffer);
    jpeg_blob.jpeg_blob_id = CAMERA3_JPEG_BLOB_ID;
    jpeg_blob.jpeg_size = nV21JpegCompressor.getCompressedSize();
    memcpy(mJpegBuffer.img + cb->width - sizeof(camera3_jpeg_blob_t),
           &jpeg_blob, sizeof(camera3_jpeg_blob_t));

    freeExifData(exifData);
    return OK;
}

bool JpegCompressor::isBusy() {
    Mutex::Autolock busyLock(mBusyMutex);
    return mIsBusy;
}

bool JpegCompressor::isStreamInUse(uint32_t id) {
    Mutex::Autolock lock(mBusyMutex);

    if (mBuffers && mIsBusy) {
        for (size_t i = 0; i < mBuffers->size(); i++) {
            if ( (*mBuffers)[i].streamId == (int)id ) return true;
        }
    }
    return false;
}

bool JpegCompressor::waitForDone(nsecs_t timeout) {
    Mutex::Autolock lock(mBusyMutex);
    while (mIsBusy) {
        status_t res = mDone.waitRelative(mBusyMutex, timeout);
        if (res != OK) return false;
    }
    return true;
}

void JpegCompressor::cleanUp() {
    Mutex::Autolock lock(mBusyMutex);

    if (mFoundAux) {
        if (mAuxBuffer.streamId == 0) {
            delete[] mAuxBuffer.img;
        } else if (!mSynchronous) {
            mListener->onJpegInputDone(mAuxBuffer);
        }
    }
    if (!mSynchronous) {
        delete mBuffers;
    }

    mBuffers = NULL;

    mIsBusy = false;
    mDone.signal();
}

JpegCompressor::JpegListener::~JpegListener() {
}

} // namespace android