summaryrefslogtreecommitdiff
path: root/devices/EmulatedCamera/hwl/JpegCompressor.h
blob: c373621225763777813274ec4e324fb3865e2f11 (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
/*
 * Copyright (C) 2019 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.
 */

#ifndef HW_EMULATOR_CAMERA_JPEG_H
#define HW_EMULATOR_CAMERA_JPEG_H

#include <hwl_types.h>

#include <condition_variable>
#include <mutex>
#include <queue>
#include <thread>

#include "Base.h"

extern "C" {
#include <jpeglib.h>
}

#include "utils/ExifUtils.h"

namespace android {

using google_camera_hal::BufferStatus;
using google_camera_hal::HwlPipelineCallback;
using google_camera_hal::HwlPipelineResult;

struct JpegYUV420Input {
  uint32_t width, height;
  bool buffer_owner;
  YCbCrPlanes yuv_planes;
  int32_t color_space;

  JpegYUV420Input() : width(0), height(0), buffer_owner(false) {
  }
  ~JpegYUV420Input() {
    if ((yuv_planes.img_y != nullptr) && buffer_owner) {
      delete[] yuv_planes.img_y;
      yuv_planes = {};
    }
  }

  JpegYUV420Input(const JpegYUV420Input&) = delete;
  JpegYUV420Input& operator=(const JpegYUV420Input&) = delete;
};

struct JpegYUV420Job {
  std::unique_ptr<JpegYUV420Input> input;
  std::unique_ptr<SensorBuffer> output;
  std::unique_ptr<HalCameraMetadata> result_metadata;
  std::unique_ptr<ExifUtils> exif_utils;
};

class JpegCompressor {
 public:
  JpegCompressor();
  virtual ~JpegCompressor();

  status_t QueueYUV420(std::unique_ptr<JpegYUV420Job> job);

 private:
  std::mutex mutex_;
  std::condition_variable condition_;
  std::atomic_bool jpeg_done_ = false;
  std::thread jpeg_processing_thread_;
  std::queue<std::unique_ptr<JpegYUV420Job>> pending_yuv_jobs_;
  std::string exif_make_, exif_model_;

  j_common_ptr jpeg_error_info_;
  bool CheckError(const char* msg);
  void CompressYUV420(std::unique_ptr<JpegYUV420Job> job);
  struct YUV420Frame {
    uint8_t* output_buffer;
    size_t output_buffer_size;
    YCbCrPlanes yuv_planes;
    size_t width;
    size_t height;
    const uint8_t* app1_buffer;
    size_t app1_buffer_size;
    int32_t color_space;
  };
  size_t CompressYUV420Frame(YUV420Frame frame);
  void ThreadLoop();

  JpegCompressor(const JpegCompressor&) = delete;
  JpegCompressor& operator=(const JpegCompressor&) = delete;
};

}  // namespace android

template <>
struct std::default_delete<jpeg_compress_struct> {
  inline void operator()(jpeg_compress_struct* cinfo) const {
    if (cinfo != nullptr) {
      jpeg_destroy_compress(cinfo);
      delete cinfo;
    }
  }
};

#endif