• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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 #include <stdio.h>  // stdio.h must appear before jpeglib.h
18 #include <jpeglib.h>
19 
20 #include <android-base/logging.h>
21 #include "host/frontend/vnc_server/jpeg_compressor.h"
22 #include "host/frontend/vnc_server/vnc_utils.h"
23 #include "host/libs/screen_connector/screen_connector.h"
24 
25 using cuttlefish::vnc::JpegCompressor;
26 
27 namespace {
InitCinfo(jpeg_compress_struct * cinfo,jpeg_error_mgr * err,std::uint16_t width,std::uint16_t height,int jpeg_quality)28 void InitCinfo(jpeg_compress_struct* cinfo, jpeg_error_mgr* err,
29                std::uint16_t width, std::uint16_t height, int jpeg_quality) {
30   cinfo->err = jpeg_std_error(err);
31   jpeg_create_compress(cinfo);
32 
33   cinfo->image_width = width;
34   cinfo->image_height = height;
35   cinfo->input_components = cuttlefish::ScreenConnectorInfo::BytesPerPixel();
36   cinfo->in_color_space = JCS_EXT_RGBX;
37 
38   jpeg_set_defaults(cinfo);
39   jpeg_set_quality(cinfo, jpeg_quality, true);
40 }
41 }  // namespace
42 
Compress(const Message & frame,int jpeg_quality,std::uint16_t x,std::uint16_t y,std::uint16_t width,std::uint16_t height,int stride)43 cuttlefish::Message JpegCompressor::Compress(const Message& frame,
44                                       int jpeg_quality, std::uint16_t x,
45                                       std::uint16_t y, std::uint16_t width,
46                                       std::uint16_t height,
47                                       int stride) {
48   jpeg_compress_struct cinfo{};
49   jpeg_error_mgr err{};
50   InitCinfo(&cinfo, &err, width, height, jpeg_quality);
51 
52   auto* compression_buffer = buffer_.get();
53   auto compression_buffer_size = buffer_capacity_;
54   jpeg_mem_dest(&cinfo, &compression_buffer, &compression_buffer_size);
55   jpeg_start_compress(&cinfo, true);
56 
57   while (cinfo.next_scanline < cinfo.image_height) {
58     auto row = static_cast<JSAMPROW>(const_cast<std::uint8_t*>(
59         &frame[(y * stride) +
60                (cinfo.next_scanline * stride) +
61                (x * cuttlefish::ScreenConnectorInfo::BytesPerPixel())]));
62     jpeg_write_scanlines(&cinfo, &row, 1);
63   }
64   jpeg_finish_compress(&cinfo);
65   jpeg_destroy_compress(&cinfo);
66 
67   UpdateBuffer(compression_buffer, compression_buffer_size);
68   return {compression_buffer, compression_buffer + compression_buffer_size};
69 }
70 
UpdateBuffer(std::uint8_t * compression_buffer,unsigned long compression_buffer_size)71 void JpegCompressor::UpdateBuffer(std::uint8_t* compression_buffer,
72                                   unsigned long compression_buffer_size) {
73   if (buffer_.get() != compression_buffer) {
74     buffer_capacity_ = compression_buffer_size;
75     buffer_.reset(compression_buffer);
76   }
77 }
78