Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2023 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 | // #define LOG_NDEBUG 0 |
| 17 | #define LOG_TAG "JpegUtil" |
| 18 | #include "JpegUtil.h" |
| 19 | |
| 20 | #include <cstddef> |
| 21 | #include <cstdint> |
| 22 | #include <memory> |
| 23 | |
| 24 | #include "android/hardware_buffer.h" |
| 25 | #include "jpeglib.h" |
| 26 | #include "log/log.h" |
| 27 | #include "ui/GraphicBuffer.h" |
| 28 | #include "ui/GraphicBufferMapper.h" |
| 29 | #include "utils/Errors.h" |
| 30 | |
| 31 | namespace android { |
| 32 | namespace companion { |
| 33 | namespace virtualcamera { |
| 34 | namespace { |
| 35 | |
| 36 | constexpr int kJpegQuality = 80; |
| 37 | |
| 38 | class LibJpegContext { |
| 39 | public: |
| 40 | LibJpegContext(int width, int height, const android_ycbcr& ycbcr, |
| 41 | const size_t outBufferSize, void* outBuffer) |
| 42 | : mYCbCr(ycbcr), |
| 43 | mWidth(width), |
| 44 | mHeight(height), |
| 45 | mDstBufferSize(outBufferSize), |
| 46 | mDstBuffer(outBuffer) { |
| 47 | // Initialize error handling for libjpeg. |
| 48 | // We call jpeg_std_error to initialize standard error |
| 49 | // handling and then override: |
| 50 | // * output_message not to print to stderr, but use ALOG instead. |
| 51 | // * error_exit not to terminate the process, but failure flag instead. |
| 52 | mCompressStruct.err = jpeg_std_error(&mErrorMgr); |
| 53 | mCompressStruct.err->output_message = onOutputError; |
| 54 | mCompressStruct.err->error_exit = onErrorExit; |
| 55 | jpeg_create_compress(&mCompressStruct); |
| 56 | |
| 57 | // Configure input image parameters. |
| 58 | mCompressStruct.image_width = width; |
| 59 | mCompressStruct.image_height = height; |
| 60 | mCompressStruct.input_components = 3; |
| 61 | mCompressStruct.in_color_space = JCS_YCbCr; |
| 62 | // We pass pointer to this instance as a client data so we can |
| 63 | // access this object from the static callbacks invoked by |
| 64 | // libjpeg. |
| 65 | mCompressStruct.client_data = this; |
| 66 | |
| 67 | // Configure destination manager for libjpeg. |
| 68 | mCompressStruct.dest = &mDestinationMgr; |
| 69 | mDestinationMgr.init_destination = onInitDestination; |
| 70 | mDestinationMgr.empty_output_buffer = onEmptyOutputBuffer; |
| 71 | mDestinationMgr.term_destination = onTermDestination; |
| 72 | mDestinationMgr.next_output_byte = reinterpret_cast<JOCTET*>(mDstBuffer); |
| 73 | mDestinationMgr.free_in_buffer = mDstBufferSize; |
| 74 | |
| 75 | // Configure everything else based on input configuration above. |
| 76 | jpeg_set_defaults(&mCompressStruct); |
| 77 | |
| 78 | // Set quality and colorspace. |
| 79 | jpeg_set_quality(&mCompressStruct, kJpegQuality, 1); |
| 80 | jpeg_set_colorspace(&mCompressStruct, JCS_YCbCr); |
| 81 | |
| 82 | // Configure RAW input mode - this let's libjpeg know we're providing raw, |
| 83 | // subsampled YCbCr data. |
| 84 | mCompressStruct.raw_data_in = 1; |
| 85 | mCompressStruct.dct_method = JDCT_IFAST; |
| 86 | |
| 87 | // Configure sampling factors - this states that every 2 Y |
| 88 | // samples share 1 Cb & 1 Cr component vertically & horizontally (YUV420). |
| 89 | mCompressStruct.comp_info[0].h_samp_factor = 2; |
| 90 | mCompressStruct.comp_info[0].v_samp_factor = 2; |
| 91 | mCompressStruct.comp_info[1].h_samp_factor = 1; |
| 92 | mCompressStruct.comp_info[1].v_samp_factor = 1; |
| 93 | mCompressStruct.comp_info[2].h_samp_factor = 1; |
| 94 | mCompressStruct.comp_info[2].v_samp_factor = 1; |
| 95 | } |
| 96 | |
| 97 | bool compress() { |
| 98 | // Prepare arrays of pointers to scanlines of each plane. |
| 99 | std::vector<JSAMPROW> yLines(mHeight); |
| 100 | std::vector<JSAMPROW> cbLines(mHeight / 2); |
| 101 | std::vector<JSAMPROW> crLines(mHeight / 2); |
| 102 | |
| 103 | uint8_t* y = static_cast<uint8_t*>(mYCbCr.y); |
| 104 | uint8_t* cb = static_cast<uint8_t*>(mYCbCr.cb); |
| 105 | uint8_t* cr = static_cast<uint8_t*>(mYCbCr.cr); |
| 106 | |
| 107 | // Since UV samples might be interleaved (semiplanar) we need to copy |
| 108 | // them to separate planes, since libjpeg doesn't directly |
| 109 | // support processing semiplanar YUV. |
| 110 | const int c_samples = (mWidth / 2) * (mHeight / 2); |
| 111 | std::vector<uint8_t> cb_plane(c_samples); |
| 112 | std::vector<uint8_t> cr_plane(c_samples); |
| 113 | |
| 114 | // TODO(b/301023410) - Use libyuv or ARM SIMD for "unzipping" the data. |
| 115 | for (int i = 0; i < c_samples; ++i) { |
| 116 | cb_plane[i] = *cb; |
| 117 | cr_plane[i] = *cr; |
| 118 | cb += mYCbCr.chroma_step; |
| 119 | cr += mYCbCr.chroma_step; |
| 120 | } |
| 121 | |
| 122 | // Collect pointers to individual scanline of each plane. |
| 123 | for (int i = 0; i < mHeight; ++i) { |
| 124 | yLines[i] = y + i * mYCbCr.ystride; |
| 125 | } |
| 126 | for (int i = 0; i < (mHeight / 2); ++i) { |
| 127 | cbLines[i] = cb_plane.data() + i * (mWidth / 2); |
| 128 | crLines[i] = cr_plane.data() + i * (mWidth / 2); |
| 129 | } |
| 130 | |
| 131 | // Perform actual compression. |
| 132 | jpeg_start_compress(&mCompressStruct, TRUE); |
| 133 | |
| 134 | while (mCompressStruct.next_scanline < mCompressStruct.image_height) { |
| 135 | const uint32_t batchSize = DCTSIZE * 2; |
| 136 | const uint32_t nl = mCompressStruct.next_scanline; |
| 137 | JSAMPARRAY planes[3]{&yLines[nl], &cbLines[nl / 2], &crLines[nl / 2]}; |
| 138 | |
| 139 | uint32_t done = jpeg_write_raw_data(&mCompressStruct, planes, batchSize); |
| 140 | |
| 141 | if (done != batchSize) { |
| 142 | ALOGE("%s: compressed %u lines, expected %u (total %u/%u)", |
| 143 | __FUNCTION__, done, batchSize, mCompressStruct.next_scanline, |
| 144 | mCompressStruct.image_height); |
| 145 | return false; |
| 146 | } |
| 147 | } |
| 148 | jpeg_finish_compress(&mCompressStruct); |
| 149 | return mSuccess; |
| 150 | } |
| 151 | |
| 152 | private: |
| 153 | void setSuccess(const boolean success) { |
| 154 | mSuccess = success; |
| 155 | } |
| 156 | |
| 157 | void initDestination() { |
| 158 | mDestinationMgr.next_output_byte = reinterpret_cast<JOCTET*>(mDstBuffer); |
| 159 | mDestinationMgr.free_in_buffer = mDstBufferSize; |
| 160 | ALOGV("%s:%d jpeg start: %p [%zu]", __FUNCTION__, __LINE__, mDstBuffer, |
| 161 | mDstBufferSize); |
| 162 | } |
| 163 | |
| 164 | void termDestination() { |
| 165 | mEncodedSize = mDstBufferSize - mDestinationMgr.free_in_buffer; |
| 166 | ALOGV("%s:%d Done with jpeg: %zu", __FUNCTION__, __LINE__, mEncodedSize); |
| 167 | } |
| 168 | |
| 169 | // === libjpeg callbacks below === |
| 170 | |
| 171 | static void onOutputError(j_common_ptr cinfo) { |
| 172 | char buffer[JMSG_LENGTH_MAX]; |
| 173 | (*cinfo->err->format_message)(cinfo, buffer); |
| 174 | ALOGE("libjpeg error: %s", buffer); |
| 175 | }; |
| 176 | |
| 177 | static void onErrorExit(j_common_ptr cinfo) { |
| 178 | static_cast<LibJpegContext*>(cinfo->client_data)->setSuccess(false); |
| 179 | }; |
| 180 | |
| 181 | static void onInitDestination(j_compress_ptr cinfo) { |
| 182 | static_cast<LibJpegContext*>(cinfo->client_data)->initDestination(); |
| 183 | } |
| 184 | |
| 185 | static int onEmptyOutputBuffer(j_compress_ptr cinfo __unused) { |
| 186 | ALOGV("%s:%d Out of buffer", __FUNCTION__, __LINE__); |
| 187 | return 0; |
| 188 | } |
| 189 | |
| 190 | static void onTermDestination(j_compress_ptr cinfo) { |
| 191 | static_cast<LibJpegContext*>(cinfo->client_data)->termDestination(); |
| 192 | } |
| 193 | |
| 194 | jpeg_compress_struct mCompressStruct; |
| 195 | jpeg_error_mgr mErrorMgr; |
| 196 | jpeg_destination_mgr mDestinationMgr; |
| 197 | |
| 198 | // Layout of the input image. |
| 199 | android_ycbcr mYCbCr; |
| 200 | |
| 201 | // Dimensions of the input image. |
| 202 | int mWidth; |
| 203 | int mHeight; |
| 204 | |
| 205 | // Destination buffer and it's capacity. |
| 206 | size_t mDstBufferSize; |
| 207 | void* mDstBuffer; |
| 208 | |
| 209 | // This will be set to size of encoded data |
| 210 | // written to the outputBuffer when encoding finishes. |
| 211 | size_t mEncodedSize; |
| 212 | // Set to true/false based on whether the encoding |
| 213 | // was successful. |
| 214 | boolean mSuccess = true; |
| 215 | }; |
| 216 | |
| 217 | } // namespace |
| 218 | |
| 219 | // Returns true if the EGL is in an error state and logs the error. |
| 220 | bool compressJpeg(int width, int height, const android_ycbcr& ycbcr, |
| 221 | size_t outBufferSize, void* outBuffer) { |
| 222 | return LibJpegContext(width, height, ycbcr, outBufferSize, outBuffer) |
| 223 | .compress(); |
| 224 | } |
| 225 | |
| 226 | } // namespace virtualcamera |
| 227 | } // namespace companion |
| 228 | } // namespace android |