JpegCompressor.h revision e538206d15282afbc5b168d60b1026a5dfcd13c0
1/*
2 * Copyright (C) 2012 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/**
19 * This class simulates a hardware JPEG compressor.  It receives image buffers
20 * in RGBA_8888 format, processes them in a worker thread, and then pushes them
21 * out to their destination stream.
22 */
23
24#ifndef ANDROID_SERVERS_CAMERA_JPEGCOMPRESSOR_H
25#define ANDROID_SERVERS_CAMERA_JPEGCOMPRESSOR_H
26
27#include "utils/Thread.h"
28#include "utils/Mutex.h"
29#include "utils/Timers.h"
30#include "utils/Vector.h"
31//#include "Base.h"
32#include <stdio.h>
33#include <gui/CpuConsumer.h>
34
35extern "C" {
36#include <jpeglib.h>
37}
38
39
40namespace android {
41namespace camera2 {
42
43class JpegCompressor: private Thread, public virtual RefBase {
44  public:
45
46    JpegCompressor();
47    ~JpegCompressor();
48
49    // Start compressing COMPRESSED format buffers; JpegCompressor takes
50    // ownership of the Buffers vector.
51    status_t start(Vector<CpuConsumer::LockedBuffer*> buffers,
52            nsecs_t captureTime);
53
54    status_t cancel();
55
56    bool isBusy();
57    bool isStreamInUse(uint32_t id);
58
59    bool waitForDone(nsecs_t timeout);
60
61    // TODO: Measure this
62    static const size_t kMaxJpegSize = 300000;
63
64  private:
65    Mutex mBusyMutex;
66    Mutex mMutex;
67    bool mIsBusy;
68    Condition mDone;
69    nsecs_t mCaptureTime;
70
71    Vector<CpuConsumer::LockedBuffer*> mBuffers;
72    CpuConsumer::LockedBuffer *mJpegBuffer;
73    CpuConsumer::LockedBuffer *mAuxBuffer;
74    bool mFoundJpeg, mFoundAux;
75
76    jpeg_compress_struct mCInfo;
77
78    struct JpegError : public jpeg_error_mgr {
79        JpegCompressor *parent;
80    };
81    j_common_ptr mJpegErrorInfo;
82
83    struct JpegDestination : public jpeg_destination_mgr {
84        JpegCompressor *parent;
85    };
86
87    static void jpegErrorHandler(j_common_ptr cinfo);
88
89    static void jpegInitDestination(j_compress_ptr cinfo);
90    static boolean jpegEmptyOutputBuffer(j_compress_ptr cinfo);
91    static void jpegTermDestination(j_compress_ptr cinfo);
92
93    bool checkError(const char *msg);
94    void cleanUp();
95
96    /**
97     * Inherited Thread virtual overrides
98     */
99  private:
100    virtual status_t readyToRun();
101    virtual bool threadLoop();
102};
103
104}; // namespace camera2
105}; // namespace android
106
107#endif
108