1ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala/*
2ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala * Copyright (C) 2012 The Android Open Source Project
3ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala *
4ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala * Licensed under the Apache License, Version 2.0 (the "License");
5ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala * you may not use this file except in compliance with the License.
6ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala * You may obtain a copy of the License at
7ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala *
8ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala *      http://www.apache.org/licenses/LICENSE-2.0
9ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala *
10ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala * Unless required by applicable law or agreed to in writing, software
11ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala * distributed under the License is distributed on an "AS IS" BASIS,
12ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala * See the License for the specific language governing permissions and
14ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala * limitations under the License.
15ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala */
16ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala
174bb8118816874c696d9f1adab48490df1da365f7Eino-Ville Talvala#define LOG_TAG "Camera2-JpegProcessor"
18ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala#define ATRACE_TAG ATRACE_TAG_CAMERA
19ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala//#define LOG_NDEBUG 0
20ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala
21a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray#include <netinet/in.h>
22a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray
2373bbd1f1c493835f191ea2b0b72439292496b40aEino-Ville Talvala#include <binder/MemoryBase.h>
2473bbd1f1c493835f191ea2b0b72439292496b40aEino-Ville Talvala#include <binder/MemoryHeapBase.h>
25ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala#include <utils/Log.h>
26ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala#include <utils/Trace.h>
271a2952aee048ca7b1765e2bc09ebe9aeddaeafa3Mathias Agopian#include <gui/Surface.h>
28ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala
297b82efe7a376c882f8f938e1c41b8311a8cdda4aEino-Ville Talvala#include "common/CameraDeviceBase.h"
307b82efe7a376c882f8f938e1c41b8311a8cdda4aEino-Ville Talvala#include "api1/Camera2Client.h"
317b82efe7a376c882f8f938e1c41b8311a8cdda4aEino-Ville Talvala#include "api1/client2/Camera2Heap.h"
327b82efe7a376c882f8f938e1c41b8311a8cdda4aEino-Ville Talvala#include "api1/client2/CaptureSequencer.h"
337b82efe7a376c882f8f938e1c41b8311a8cdda4aEino-Ville Talvala#include "api1/client2/JpegProcessor.h"
34ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala
35ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalanamespace android {
36ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalanamespace camera2 {
37ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala
38da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville TalvalaJpegProcessor::JpegProcessor(
39d09801b99503b57c35e321ad9afa7e861e012813Eino-Ville Talvala    sp<Camera2Client> client,
40da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala    wp<CaptureSequencer> sequencer):
41ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala        Thread(false),
42d09801b99503b57c35e321ad9afa7e861e012813Eino-Ville Talvala        mDevice(client->getCameraDevice()),
43da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala        mSequencer(sequencer),
44d09801b99503b57c35e321ad9afa7e861e012813Eino-Ville Talvala        mId(client->getCameraId()),
45b0fdc1ed2182fe851ef7ca98a1b4a552b53b3033Shuzhen Wang        mCaptureDone(false),
46b0fdc1ed2182fe851ef7ca98a1b4a552b53b3033Shuzhen Wang        mCaptureSuccess(false),
47ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala        mCaptureStreamId(NO_STREAM) {
48ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala}
49ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala
50da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville TalvalaJpegProcessor::~JpegProcessor() {
51ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala    ALOGV("%s: Exit", __FUNCTION__);
52cf70d3469332445dc3ffd09729da3538612b1bb2Eino-Ville Talvala    deleteStream();
53ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala}
54ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala
5504f101c35eaa90b1f95939afac30674ec1611e6fDan Stozavoid JpegProcessor::onFrameAvailable(const BufferItem& /*item*/) {
56ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala    Mutex::Autolock l(mInputMutex);
57b0fdc1ed2182fe851ef7ca98a1b4a552b53b3033Shuzhen Wang    ALOGV("%s", __FUNCTION__);
58b0fdc1ed2182fe851ef7ca98a1b4a552b53b3033Shuzhen Wang    if (!mCaptureDone) {
59b0fdc1ed2182fe851ef7ca98a1b4a552b53b3033Shuzhen Wang        mCaptureDone = true;
60b0fdc1ed2182fe851ef7ca98a1b4a552b53b3033Shuzhen Wang        mCaptureSuccess = true;
61b0fdc1ed2182fe851ef7ca98a1b4a552b53b3033Shuzhen Wang        mCaptureDoneSignal.signal();
62b0fdc1ed2182fe851ef7ca98a1b4a552b53b3033Shuzhen Wang    }
63b0fdc1ed2182fe851ef7ca98a1b4a552b53b3033Shuzhen Wang}
64b0fdc1ed2182fe851ef7ca98a1b4a552b53b3033Shuzhen Wang
65b0fdc1ed2182fe851ef7ca98a1b4a552b53b3033Shuzhen Wangvoid JpegProcessor::onBufferAcquired(const BufferInfo& /*bufferInfo*/) {
66b0fdc1ed2182fe851ef7ca98a1b4a552b53b3033Shuzhen Wang    // Intentionally left empty
67b0fdc1ed2182fe851ef7ca98a1b4a552b53b3033Shuzhen Wang}
68b0fdc1ed2182fe851ef7ca98a1b4a552b53b3033Shuzhen Wang
69b0fdc1ed2182fe851ef7ca98a1b4a552b53b3033Shuzhen Wangvoid JpegProcessor::onBufferReleased(const BufferInfo& bufferInfo) {
70b0fdc1ed2182fe851ef7ca98a1b4a552b53b3033Shuzhen Wang    Mutex::Autolock l(mInputMutex);
71b0fdc1ed2182fe851ef7ca98a1b4a552b53b3033Shuzhen Wang    ALOGV("%s", __FUNCTION__);
72b0fdc1ed2182fe851ef7ca98a1b4a552b53b3033Shuzhen Wang
73b0fdc1ed2182fe851ef7ca98a1b4a552b53b3033Shuzhen Wang    if (bufferInfo.mError) {
74b0fdc1ed2182fe851ef7ca98a1b4a552b53b3033Shuzhen Wang        mCaptureDone = true;
75b0fdc1ed2182fe851ef7ca98a1b4a552b53b3033Shuzhen Wang        mCaptureSuccess = false;
76b0fdc1ed2182fe851ef7ca98a1b4a552b53b3033Shuzhen Wang        mCaptureDoneSignal.signal();
77ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala    }
78ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala}
79ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala
80da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvalastatus_t JpegProcessor::updateStream(const Parameters &params) {
81ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala    ATRACE_CALL();
82ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala    ALOGV("%s", __FUNCTION__);
83ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala    status_t res;
84ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala
85ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala    Mutex::Autolock l(mInputMutex);
86ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala
87d09801b99503b57c35e321ad9afa7e861e012813Eino-Ville Talvala    sp<CameraDeviceBase> device = mDevice.promote();
88d09801b99503b57c35e321ad9afa7e861e012813Eino-Ville Talvala    if (device == 0) {
89d09801b99503b57c35e321ad9afa7e861e012813Eino-Ville Talvala        ALOGE("%s: Camera %d: Device does not exist", __FUNCTION__, mId);
90d09801b99503b57c35e321ad9afa7e861e012813Eino-Ville Talvala        return INVALID_OPERATION;
91d09801b99503b57c35e321ad9afa7e861e012813Eino-Ville Talvala    }
92ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala
93ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala    // Find out buffer size for JPEG
9428c9b6f298134624cb52b1af4ed8716dddb983d3Zhijun He    ssize_t maxJpegSize = device->getJpegBufferSize(params.pictureWidth, params.pictureHeight);
9528c9b6f298134624cb52b1af4ed8716dddb983d3Zhijun He    if (maxJpegSize <= 0) {
9628c9b6f298134624cb52b1af4ed8716dddb983d3Zhijun He        ALOGE("%s: Camera %d: Jpeg buffer size (%zu) is invalid ",
9728c9b6f298134624cb52b1af4ed8716dddb983d3Zhijun He                __FUNCTION__, mId, maxJpegSize);
98ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala        return INVALID_OPERATION;
99ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala    }
100ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala
101ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala    if (mCaptureConsumer == 0) {
102ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala        // Create CPU buffer queue endpoint
1038aa0f0619ea867e8fb240cf27913d4f8ae767385Dan Stoza        sp<IGraphicBufferProducer> producer;
1048aa0f0619ea867e8fb240cf27913d4f8ae767385Dan Stoza        sp<IGraphicBufferConsumer> consumer;
1058aa0f0619ea867e8fb240cf27913d4f8ae767385Dan Stoza        BufferQueue::createBufferQueue(&producer, &consumer);
1068aa0f0619ea867e8fb240cf27913d4f8ae767385Dan Stoza        mCaptureConsumer = new CpuConsumer(consumer, 1);
107ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala        mCaptureConsumer->setFrameAvailableListener(this);
108727d172137b4f32681c098de8e2623c0b65a6406Eino-Ville Talvala        mCaptureConsumer->setName(String8("Camera2-JpegConsumer"));
1098aa0f0619ea867e8fb240cf27913d4f8ae767385Dan Stoza        mCaptureWindow = new Surface(producer);
1102d077d0acdbb26eeb1357489c999ec875f6ce297Eino-Ville Talvala    }
1112d077d0acdbb26eeb1357489c999ec875f6ce297Eino-Ville Talvala
1122d077d0acdbb26eeb1357489c999ec875f6ce297Eino-Ville Talvala    // Since ashmem heaps are rounded up to page size, don't reallocate if
1132d077d0acdbb26eeb1357489c999ec875f6ce297Eino-Ville Talvala    // the capture heap isn't exactly the same size as the required JPEG buffer
1142d077d0acdbb26eeb1357489c999ec875f6ce297Eino-Ville Talvala    const size_t HEAP_SLACK_FACTOR = 2;
1152d077d0acdbb26eeb1357489c999ec875f6ce297Eino-Ville Talvala    if (mCaptureHeap == 0 ||
1162d077d0acdbb26eeb1357489c999ec875f6ce297Eino-Ville Talvala            (mCaptureHeap->getSize() < static_cast<size_t>(maxJpegSize)) ||
1172d077d0acdbb26eeb1357489c999ec875f6ce297Eino-Ville Talvala            (mCaptureHeap->getSize() >
1182d077d0acdbb26eeb1357489c999ec875f6ce297Eino-Ville Talvala                    static_cast<size_t>(maxJpegSize) * HEAP_SLACK_FACTOR) ) {
119ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala        // Create memory for API consumption
1202d077d0acdbb26eeb1357489c999ec875f6ce297Eino-Ville Talvala        mCaptureHeap.clear();
1212d077d0acdbb26eeb1357489c999ec875f6ce297Eino-Ville Talvala        mCaptureHeap =
1222d077d0acdbb26eeb1357489c999ec875f6ce297Eino-Ville Talvala                new MemoryHeapBase(maxJpegSize, 0, "Camera2Client::CaptureHeap");
123a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray        if (mCaptureHeap->getSize() == 0) {
124ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala            ALOGE("%s: Camera %d: Unable to allocate memory for capture",
125d09801b99503b57c35e321ad9afa7e861e012813Eino-Ville Talvala                    __FUNCTION__, mId);
126ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala            return NO_MEMORY;
127ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala        }
128ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala    }
129d309fb9c8a2c4564d88fffba19c4e3688e4b862bEino-Ville Talvala    ALOGV("%s: Camera %d: JPEG capture heap now %zu bytes; requested %zd bytes",
1302d077d0acdbb26eeb1357489c999ec875f6ce297Eino-Ville Talvala            __FUNCTION__, mId, mCaptureHeap->getSize(), maxJpegSize);
131ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala
132ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala    if (mCaptureStreamId != NO_STREAM) {
133ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala        // Check if stream parameters have to change
134ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala        uint32_t currentWidth, currentHeight;
135ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala        res = device->getStreamInfo(mCaptureStreamId,
136d46a6b9fd8b2a4f9098757384711e2cd03a91651Eino-Ville Talvala                &currentWidth, &currentHeight, 0, 0);
137ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala        if (res != OK) {
138ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala            ALOGE("%s: Camera %d: Error querying capture output stream info: "
139ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala                    "%s (%d)", __FUNCTION__,
140d09801b99503b57c35e321ad9afa7e861e012813Eino-Ville Talvala                    mId, strerror(-res), res);
141ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala            return res;
142ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala        }
143ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala        if (currentWidth != (uint32_t)params.pictureWidth ||
144ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala                currentHeight != (uint32_t)params.pictureHeight) {
145ecf17e82505fdb60d59e00b6dd59036df93de655Igor Murashkin            ALOGV("%s: Camera %d: Deleting stream %d since the buffer dimensions changed",
146d09801b99503b57c35e321ad9afa7e861e012813Eino-Ville Talvala                __FUNCTION__, mId, mCaptureStreamId);
147ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala            res = device->deleteStream(mCaptureStreamId);
148dca254aef0c09867e50fe6613c6fad405de72842Igor Murashkin            if (res == -EBUSY) {
149dca254aef0c09867e50fe6613c6fad405de72842Igor Murashkin                ALOGV("%s: Camera %d: Device is busy, call updateStream again "
150dca254aef0c09867e50fe6613c6fad405de72842Igor Murashkin                      " after it becomes idle", __FUNCTION__, mId);
151dca254aef0c09867e50fe6613c6fad405de72842Igor Murashkin                return res;
152dca254aef0c09867e50fe6613c6fad405de72842Igor Murashkin            } else if (res != OK) {
153ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala                ALOGE("%s: Camera %d: Unable to delete old output stream "
154ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala                        "for capture: %s (%d)", __FUNCTION__,
155d09801b99503b57c35e321ad9afa7e861e012813Eino-Ville Talvala                        mId, strerror(-res), res);
156ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala                return res;
157ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala            }
158ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala            mCaptureStreamId = NO_STREAM;
159ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala        }
160ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala    }
161ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala
162ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala    if (mCaptureStreamId == NO_STREAM) {
163ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala        // Create stream for HAL production
164ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala        res = device->createStream(mCaptureWindow,
165ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala                params.pictureWidth, params.pictureHeight,
1662cbf6cea23539bfe99e36d1d221de62255452e86Eino-Ville Talvala                HAL_PIXEL_FORMAT_BLOB, HAL_DATASPACE_V0_JFIF,
167b97babb8c08969b55af3b6456d15f764c8873d3fYin-Chia Yeh                CAMERA3_STREAM_ROTATION_0, &mCaptureStreamId);
168ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala        if (res != OK) {
169ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala            ALOGE("%s: Camera %d: Can't create output stream for capture: "
170d09801b99503b57c35e321ad9afa7e861e012813Eino-Ville Talvala                    "%s (%d)", __FUNCTION__, mId,
171ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala                    strerror(-res), res);
172ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala            return res;
173ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala        }
174ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala
175b0fdc1ed2182fe851ef7ca98a1b4a552b53b3033Shuzhen Wang        res = device->addBufferListenerForStream(mCaptureStreamId, this);
176b0fdc1ed2182fe851ef7ca98a1b4a552b53b3033Shuzhen Wang        if (res != OK) {
177b0fdc1ed2182fe851ef7ca98a1b4a552b53b3033Shuzhen Wang              ALOGE("%s: Camera %d: Can't add buffer listeneri: %s (%d)",
178b0fdc1ed2182fe851ef7ca98a1b4a552b53b3033Shuzhen Wang                    __FUNCTION__, mId, strerror(-res), res);
179b0fdc1ed2182fe851ef7ca98a1b4a552b53b3033Shuzhen Wang              return res;
180b0fdc1ed2182fe851ef7ca98a1b4a552b53b3033Shuzhen Wang        }
181ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala    }
182ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala    return OK;
183ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala}
184ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala
185da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvalastatus_t JpegProcessor::deleteStream() {
186ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala    ATRACE_CALL();
187ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala
188ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala    Mutex::Autolock l(mInputMutex);
189ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala
190ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala    if (mCaptureStreamId != NO_STREAM) {
191d09801b99503b57c35e321ad9afa7e861e012813Eino-Ville Talvala        sp<CameraDeviceBase> device = mDevice.promote();
192d09801b99503b57c35e321ad9afa7e861e012813Eino-Ville Talvala        if (device == 0) {
193d09801b99503b57c35e321ad9afa7e861e012813Eino-Ville Talvala            ALOGE("%s: Camera %d: Device does not exist", __FUNCTION__, mId);
194d09801b99503b57c35e321ad9afa7e861e012813Eino-Ville Talvala            return INVALID_OPERATION;
195d09801b99503b57c35e321ad9afa7e861e012813Eino-Ville Talvala        }
196ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala
197ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala        device->deleteStream(mCaptureStreamId);
198cf70d3469332445dc3ffd09729da3538612b1bb2Eino-Ville Talvala
199cf70d3469332445dc3ffd09729da3538612b1bb2Eino-Ville Talvala        mCaptureHeap.clear();
200cf70d3469332445dc3ffd09729da3538612b1bb2Eino-Ville Talvala        mCaptureWindow.clear();
201cf70d3469332445dc3ffd09729da3538612b1bb2Eino-Ville Talvala        mCaptureConsumer.clear();
202cf70d3469332445dc3ffd09729da3538612b1bb2Eino-Ville Talvala
203ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala        mCaptureStreamId = NO_STREAM;
204ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala    }
205ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala    return OK;
206ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala}
207ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala
208da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvalaint JpegProcessor::getStreamId() const {
209ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala    Mutex::Autolock l(mInputMutex);
210ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala    return mCaptureStreamId;
211ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala}
212ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala
213ddf3c5025e2f6f35a4c188c19f30142c64a092c4Igor Murashkinvoid JpegProcessor::dump(int /*fd*/, const Vector<String16>& /*args*/) const {
214ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala}
215ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala
216da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvalabool JpegProcessor::threadLoop() {
217ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala    status_t res;
218ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala
219b0fdc1ed2182fe851ef7ca98a1b4a552b53b3033Shuzhen Wang    bool captureSuccess = false;
220ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala    {
221ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala        Mutex::Autolock l(mInputMutex);
222b0fdc1ed2182fe851ef7ca98a1b4a552b53b3033Shuzhen Wang
223b0fdc1ed2182fe851ef7ca98a1b4a552b53b3033Shuzhen Wang        while (!mCaptureDone) {
224b0fdc1ed2182fe851ef7ca98a1b4a552b53b3033Shuzhen Wang            res = mCaptureDoneSignal.waitRelative(mInputMutex,
225ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala                    kWaitDuration);
226ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala            if (res == TIMED_OUT) return true;
227ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala        }
228b0fdc1ed2182fe851ef7ca98a1b4a552b53b3033Shuzhen Wang
229b0fdc1ed2182fe851ef7ca98a1b4a552b53b3033Shuzhen Wang        captureSuccess = mCaptureSuccess;
230b0fdc1ed2182fe851ef7ca98a1b4a552b53b3033Shuzhen Wang        mCaptureDone = false;
231ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala    }
232ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala
233b0fdc1ed2182fe851ef7ca98a1b4a552b53b3033Shuzhen Wang    res = processNewCapture(captureSuccess);
234ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala
235ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala    return true;
236ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala}
237ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala
238b0fdc1ed2182fe851ef7ca98a1b4a552b53b3033Shuzhen Wangstatus_t JpegProcessor::processNewCapture(bool captureSuccess) {
239ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala    ATRACE_CALL();
240ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala    status_t res;
241ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala    sp<Camera2Heap> captureHeap;
242138851bd3cadfb60238f87567e24808925731837Zhijun He    sp<MemoryBase> captureBuffer;
243ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala
244ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala    CpuConsumer::LockedBuffer imgBuffer;
245ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala
246b0fdc1ed2182fe851ef7ca98a1b4a552b53b3033Shuzhen Wang    if (captureSuccess) {
247138851bd3cadfb60238f87567e24808925731837Zhijun He        Mutex::Autolock l(mInputMutex);
248138851bd3cadfb60238f87567e24808925731837Zhijun He        if (mCaptureStreamId == NO_STREAM) {
249138851bd3cadfb60238f87567e24808925731837Zhijun He            ALOGW("%s: Camera %d: No stream is available", __FUNCTION__, mId);
250138851bd3cadfb60238f87567e24808925731837Zhijun He            return INVALID_OPERATION;
251ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala        }
252ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala
253138851bd3cadfb60238f87567e24808925731837Zhijun He        res = mCaptureConsumer->lockNextBuffer(&imgBuffer);
254138851bd3cadfb60238f87567e24808925731837Zhijun He        if (res != OK) {
255138851bd3cadfb60238f87567e24808925731837Zhijun He            if (res != BAD_VALUE) {
256138851bd3cadfb60238f87567e24808925731837Zhijun He                ALOGE("%s: Camera %d: Error receiving still image buffer: "
257138851bd3cadfb60238f87567e24808925731837Zhijun He                        "%s (%d)", __FUNCTION__,
258138851bd3cadfb60238f87567e24808925731837Zhijun He                        mId, strerror(-res), res);
259138851bd3cadfb60238f87567e24808925731837Zhijun He            }
260138851bd3cadfb60238f87567e24808925731837Zhijun He            return res;
261138851bd3cadfb60238f87567e24808925731837Zhijun He        }
262ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala
263138851bd3cadfb60238f87567e24808925731837Zhijun He        ALOGV("%s: Camera %d: Still capture available", __FUNCTION__,
264138851bd3cadfb60238f87567e24808925731837Zhijun He                mId);
265ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala
266138851bd3cadfb60238f87567e24808925731837Zhijun He        if (imgBuffer.format != HAL_PIXEL_FORMAT_BLOB) {
267138851bd3cadfb60238f87567e24808925731837Zhijun He            ALOGE("%s: Camera %d: Unexpected format for still image: "
268138851bd3cadfb60238f87567e24808925731837Zhijun He                    "%x, expected %x", __FUNCTION__, mId,
269138851bd3cadfb60238f87567e24808925731837Zhijun He                    imgBuffer.format,
270138851bd3cadfb60238f87567e24808925731837Zhijun He                    HAL_PIXEL_FORMAT_BLOB);
271138851bd3cadfb60238f87567e24808925731837Zhijun He            mCaptureConsumer->unlockBuffer(imgBuffer);
272138851bd3cadfb60238f87567e24808925731837Zhijun He            return OK;
273138851bd3cadfb60238f87567e24808925731837Zhijun He        }
274a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray
275138851bd3cadfb60238f87567e24808925731837Zhijun He        // Find size of JPEG image
276138851bd3cadfb60238f87567e24808925731837Zhijun He        size_t jpegSize = findJpegSize(imgBuffer.data, imgBuffer.width);
277138851bd3cadfb60238f87567e24808925731837Zhijun He        if (jpegSize == 0) { // failed to find size, default to whole buffer
278138851bd3cadfb60238f87567e24808925731837Zhijun He            jpegSize = imgBuffer.width;
279138851bd3cadfb60238f87567e24808925731837Zhijun He        }
280138851bd3cadfb60238f87567e24808925731837Zhijun He        size_t heapSize = mCaptureHeap->getSize();
281138851bd3cadfb60238f87567e24808925731837Zhijun He        if (jpegSize > heapSize) {
282138851bd3cadfb60238f87567e24808925731837Zhijun He            ALOGW("%s: JPEG image is larger than expected, truncating "
2835c679d41714e42f153f6aeb492df54510a195bc1Colin Cross                    "(got %zu, expected at most %zu bytes)",
284138851bd3cadfb60238f87567e24808925731837Zhijun He                    __FUNCTION__, jpegSize, heapSize);
285138851bd3cadfb60238f87567e24808925731837Zhijun He            jpegSize = heapSize;
286138851bd3cadfb60238f87567e24808925731837Zhijun He        }
287138851bd3cadfb60238f87567e24808925731837Zhijun He
288138851bd3cadfb60238f87567e24808925731837Zhijun He        // TODO: Optimize this to avoid memcopy
289138851bd3cadfb60238f87567e24808925731837Zhijun He        captureBuffer = new MemoryBase(mCaptureHeap, 0, jpegSize);
290138851bd3cadfb60238f87567e24808925731837Zhijun He        void* captureMemory = mCaptureHeap->getBase();
291138851bd3cadfb60238f87567e24808925731837Zhijun He        memcpy(captureMemory, imgBuffer.data, jpegSize);
292ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala
293138851bd3cadfb60238f87567e24808925731837Zhijun He        mCaptureConsumer->unlockBuffer(imgBuffer);
294138851bd3cadfb60238f87567e24808925731837Zhijun He    }
295ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala
296fe580e57081885dd6059e0d407a6cd96a1be7e51Eino-Ville Talvala    sp<CaptureSequencer> sequencer = mSequencer.promote();
297fe580e57081885dd6059e0d407a6cd96a1be7e51Eino-Ville Talvala    if (sequencer != 0) {
298b0fdc1ed2182fe851ef7ca98a1b4a552b53b3033Shuzhen Wang        sequencer->onCaptureAvailable(imgBuffer.timestamp, captureBuffer, !captureSuccess);
299ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala    }
300fe580e57081885dd6059e0d407a6cd96a1be7e51Eino-Ville Talvala
301ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala    return OK;
302ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala}
303ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala
304a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray/*
305a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray * JPEG FILE FORMAT OVERVIEW.
306a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray * http://www.jpeg.org/public/jfif.pdf
307a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray * (JPEG is the image compression algorithm, actual file format is called JFIF)
308a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray *
309a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray * "Markers" are 2-byte patterns used to distinguish parts of JFIF files.  The
310a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray * first byte is always 0xFF, and the second byte is between 0x01 and 0xFE
311a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray * (inclusive).  Because every marker begins with the same byte, they are
312a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray * referred to by the second byte's value.
313a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray *
314a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray * JFIF files all begin with the Start of Image (SOI) marker, which is 0xD8.
315a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray * Following it, "segment" sections begin with other markers, followed by a
316a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray * 2-byte length (in network byte order), then the segment data.
317a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray *
318a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray * For our purposes we will ignore the data, and just use the length to skip to
319a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray * the next segment.  This is necessary because the data inside segments are
320a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray * allowed to contain the End of Image marker (0xFF 0xD9), preventing us from
321a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray * naievely scanning until the end.
322a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray *
323a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray * After all the segments are processed, the jpeg compressed image stream begins.
324a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray * This can be considered an opaque format with one requirement: all 0xFF bytes
325a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray * in this stream must be followed with a 0x00 byte.  This prevents any of the
326a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray * image data to be interpreted as a segment.  The only exception to this is at
327a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray * the end of the image stream there is an End of Image (EOI) marker, which is
328a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray * 0xFF followed by a non-zero (0xD9) byte.
329a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray */
330a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray
331a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Rayconst uint8_t MARK = 0xFF; // First byte of marker
332a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Rayconst uint8_t SOI = 0xD8; // Start of Image
333a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Rayconst uint8_t EOI = 0xD9; // End of Image
334a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Rayconst size_t MARKER_LENGTH = 2; // length of a marker
335a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray
336a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray#pragma pack(push)
337a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray#pragma pack(1)
338a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Raytypedef struct segment {
339a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray    uint8_t marker[MARKER_LENGTH];
340a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray    uint16_t length;
341a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray} segment_t;
342a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray#pragma pack(pop)
343a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray
344a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray/* HELPER FUNCTIONS */
345a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray
346a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray// check for Start of Image marker
347a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Raybool checkJpegStart(uint8_t* buf) {
348a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray    return buf[0] == MARK && buf[1] == SOI;
349a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray}
350a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray// check for End of Image marker
351a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Raybool checkJpegEnd(uint8_t *buf) {
352a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray    return buf[0] == MARK && buf[1] == EOI;
353a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray}
354a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray// check for arbitrary marker, returns marker type (second byte)
355a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray// returns 0 if no marker found. Note: 0x00 is not a valid marker type
356a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Rayuint8_t checkJpegMarker(uint8_t *buf) {
357a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray    if (buf[0] == MARK && buf[1] > 0 && buf[1] < 0xFF) {
358a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray        return buf[1];
359a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray    }
360a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray    return 0;
361a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray}
362a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray
363a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray// Return the size of the JPEG, 0 indicates failure
3640fa1e760f7093d025c977e5813e8d3a04c863ce3Alex Raysize_t JpegProcessor::findJpegSize(uint8_t* jpegBuffer, size_t maxSize) {
365a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray    size_t size;
366a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray
3670fa1e760f7093d025c977e5813e8d3a04c863ce3Alex Ray    // First check for JPEG transport header at the end of the buffer
3680fa1e760f7093d025c977e5813e8d3a04c863ce3Alex Ray    uint8_t *header = jpegBuffer + (maxSize - sizeof(struct camera2_jpeg_blob));
3690fa1e760f7093d025c977e5813e8d3a04c863ce3Alex Ray    struct camera2_jpeg_blob *blob = (struct camera2_jpeg_blob*)(header);
370a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray    if (blob->jpeg_blob_id == CAMERA2_JPEG_BLOB_ID) {
371a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray        size = blob->jpeg_size;
372a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray        if (size > 0 && size <= maxSize - sizeof(struct camera2_jpeg_blob)) {
373a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray            // Verify SOI and EOI markers
374a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray            size_t offset = size - MARKER_LENGTH;
3750fa1e760f7093d025c977e5813e8d3a04c863ce3Alex Ray            uint8_t *end = jpegBuffer + offset;
3760fa1e760f7093d025c977e5813e8d3a04c863ce3Alex Ray            if (checkJpegStart(jpegBuffer) && checkJpegEnd(end)) {
377e5729fac81c8a984e984fefc90afc64135817d4fColin Cross                ALOGV("Found JPEG transport header, img size %zu", size);
378a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray                return size;
379a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray            } else {
380a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray                ALOGW("Found JPEG transport header with bad Image Start/End");
381a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray            }
382a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray        } else {
383e5729fac81c8a984e984fefc90afc64135817d4fColin Cross            ALOGW("Found JPEG transport header with bad size %zu", size);
384a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray        }
385a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray    }
386a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray
3870fa1e760f7093d025c977e5813e8d3a04c863ce3Alex Ray    // Check Start of Image
3880fa1e760f7093d025c977e5813e8d3a04c863ce3Alex Ray    if ( !checkJpegStart(jpegBuffer) ) {
389a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray        ALOGE("Could not find start of JPEG marker");
390a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray        return 0;
391a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray    }
392a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray
393a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray    // Read JFIF segment markers, skip over segment data
394a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray    size = 0;
395a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray    while (size <= maxSize - MARKER_LENGTH) {
3960fa1e760f7093d025c977e5813e8d3a04c863ce3Alex Ray        segment_t *segment = (segment_t*)(jpegBuffer + size);
397a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray        uint8_t type = checkJpegMarker(segment->marker);
398a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray        if (type == 0) { // invalid marker, no more segments, begin JPEG data
399e5729fac81c8a984e984fefc90afc64135817d4fColin Cross            ALOGV("JPEG stream found beginning at offset %zu", size);
400a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray            break;
401a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray        }
402a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray        if (type == EOI || size > maxSize - sizeof(segment_t)) {
403e5729fac81c8a984e984fefc90afc64135817d4fColin Cross            ALOGE("Got premature End before JPEG data, offset %zu", size);
404a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray            return 0;
405a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray        }
406a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray        size_t length = ntohs(segment->length);
407e5729fac81c8a984e984fefc90afc64135817d4fColin Cross        ALOGV("JFIF Segment, type %x length %zx", type, length);
408a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray        size += length + MARKER_LENGTH;
409a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray    }
410a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray
411a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray    // Find End of Image
412a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray    // Scan JPEG buffer until End of Image (EOI)
413a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray    bool foundEnd = false;
414ddf3c5025e2f6f35a4c188c19f30142c64a092c4Igor Murashkin    for ( ; size <= maxSize - MARKER_LENGTH; size++) {
4150fa1e760f7093d025c977e5813e8d3a04c863ce3Alex Ray        if ( checkJpegEnd(jpegBuffer + size) ) {
416a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray            foundEnd = true;
417a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray            size += MARKER_LENGTH;
418a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray            break;
419a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray        }
420a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray    }
421a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray    if (!foundEnd) {
422a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray        ALOGE("Could not find end of JPEG marker");
423a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray        return 0;
424a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray    }
425a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray
426a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray    if (size > maxSize) {
427e5729fac81c8a984e984fefc90afc64135817d4fColin Cross        ALOGW("JPEG size %zu too large, reducing to maxSize %zu", size, maxSize);
428a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray        size = maxSize;
429a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray    }
430e5729fac81c8a984e984fefc90afc64135817d4fColin Cross    ALOGV("Final JPEG size %zu", size);
431a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray    return size;
432a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray}
433a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray
434ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala}; // namespace camera2
435ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala}; // namespace android
436