summaryrefslogtreecommitdiffstats
path: root/camera/SwFrameDecoder.cpp
blob: 2ce2c0fb5cd7f8a0d1562bb095d62da72e939faf (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
/*
 * Copyright (C) Texas Instruments - http://www.ti.com/
 *
 * 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.
 */

#include "Common.h"
#include "SwFrameDecoder.h"

namespace Ti {
namespace Camera {

SwFrameDecoder::SwFrameDecoder()
: mjpegWithHdrSize(0), mJpegWithHeaderBuffer(NULL) {
}

SwFrameDecoder::~SwFrameDecoder() {
    delete [] mJpegWithHeaderBuffer;
    mJpegWithHeaderBuffer = NULL;
}


void SwFrameDecoder::doConfigure(const DecoderParameters& params) {
    LOG_FUNCTION_NAME;

    mjpegWithHdrSize = (mParams.width * mParams.height / 2) +
            mJpgdecoder.readDHTSize();
    if (mJpegWithHeaderBuffer != NULL) {
        delete [] mJpegWithHeaderBuffer;
        mJpegWithHeaderBuffer = NULL;
    }
    mJpegWithHeaderBuffer = new unsigned char[mjpegWithHdrSize];

    LOG_FUNCTION_NAME_EXIT;
}


void SwFrameDecoder::doProcessInputBuffer() {
    LOG_FUNCTION_NAME;
    nsecs_t timestamp = 0;

    CAMHAL_LOGV("Will add header to MJPEG");
    int final_jpg_sz = 0;
    {
        int inIndex = mInQueue.itemAt(0);
        android::sp<MediaBuffer>& inBuffer = mInBuffers->editItemAt(inIndex);
        android::AutoMutex lock(inBuffer->getLock());
        timestamp = inBuffer->getTimestamp();
        final_jpg_sz = mJpgdecoder.appendDHT(
                reinterpret_cast<unsigned char*>(inBuffer->buffer),
                inBuffer->filledLen, mJpegWithHeaderBuffer, mjpegWithHdrSize);
        inBuffer->setStatus(BufferStatus_InDecoded);
    }
    CAMHAL_LOGV("Added header to MJPEG");
    {
        int outIndex = mOutQueue.itemAt(0);
        android::sp<MediaBuffer>& outBuffer = mOutBuffers->editItemAt(outIndex);
        android::AutoMutex lock(outBuffer->getLock());
        CameraBuffer* buffer = reinterpret_cast<CameraBuffer*>(outBuffer->buffer);
        if (!mJpgdecoder.decode(mJpegWithHeaderBuffer, final_jpg_sz,
                reinterpret_cast<unsigned char*>(buffer->mapped), 4096)) {
            CAMHAL_LOGEA("Error while decoding JPEG");
            return;
        }
        outBuffer->setTimestamp(timestamp);
        outBuffer->setStatus(BufferStatus_OutFilled);
    }
    CAMHAL_LOGV("JPEG decoded!");

    LOG_FUNCTION_NAME_EXIT;
}


}  // namespace Camera
}  // namespace Ti