1 /* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef JPEG_HARDWARE_DECODER_H 17 #define JPEG_HARDWARE_DECODER_H 18 19 #include <cinttypes> 20 #include "v2_0/icodec_image.h" 21 #include "v1_0/include/idisplay_buffer.h" 22 #include "image/image_plugin_type.h" 23 #include "image/input_data_stream.h" 24 #include "include/codec/SkCodec.h" 25 #include "image_log.h" 26 #include "jpeglib.h" 27 28 #undef LOG_DOMAIN 29 #define LOG_DOMAIN LOG_TAG_DOMAIN_ID_PLUGIN 30 31 #undef LOG_TAG 32 #define LOG_TAG "JPEGHWDECODER" 33 34 #ifdef __FILE_NAME__ 35 #define FILENAME __FILE_NAME__ 36 #else 37 #define FILENAME __FILE__ 38 #endif 39 40 #define LOG_FMT "[%{public}s][%{public}s %{public}d] " 41 #define JPEG_HW_LOGE(x, ...) \ 42 IMAGE_LOGE(LOG_FMT x, FILENAME, __FUNCTION__, __LINE__, ##__VA_ARGS__) 43 #define JPEG_HW_LOGW(x, ...) \ 44 IMAGE_LOGW(LOG_FMT x, FILENAME, __FUNCTION__, __LINE__, ##__VA_ARGS__) 45 #define JPEG_HW_LOGI(x, ...) \ 46 IMAGE_LOGI(LOG_FMT x, FILENAME, __FUNCTION__, __LINE__, ##__VA_ARGS__) 47 #define JPEG_HW_LOGD(x, ...) \ 48 IMAGE_LOGD(LOG_FMT x, FILENAME, __FUNCTION__, __LINE__, ##__VA_ARGS__) 49 50 namespace OHOS { 51 namespace ImagePlugin { 52 class JpegHardwareDecoder { 53 public: 54 JpegHardwareDecoder(); 55 ~JpegHardwareDecoder(); 56 57 bool IsHardwareDecodeSupported(const std::string& srcImgFormat, OHOS::Media::Size srcImgSize); 58 uint32_t Decode(SkCodec *codec, ImagePlugin::InputDataStream *srcStream, OHOS::Media::Size srcImgSize, 59 uint32_t sampleSize, OHOS::HDI::Codec::Image::V2_0::CodecImageBuffer& outputBuffer); 60 private: 61 class LifeSpanTimer { 62 public: 63 explicit LifeSpanTimer(std::string desc); 64 ~LifeSpanTimer(); 65 private: 66 int64_t GetCurrentTimeInUs(); 67 private: 68 int64_t startTimeInUs_; 69 std::string desc_; 70 }; 71 private: CombineTwoBytes(const uint8_t * data,unsigned int pos)72 inline uint16_t CombineTwoBytes(const uint8_t* data, unsigned int pos) 73 { 74 static constexpr int BITS_OFFSET = 8; 75 static constexpr unsigned int BYTE_OFFSET = 1; 76 return static_cast<uint16_t>((data[pos] << BITS_OFFSET) + data[pos + BYTE_OFFSET]); 77 } 78 bool InitDecoder(); 79 bool AssembleComponentInfo(jpeg_decompress_struct* jpegCompressInfo); 80 bool HuffmanTblTransform(JHUFF_TBL* huffTbl, OHOS::HDI::Codec::Image::V2_0::CodecJpegHuffTable& tbl); 81 void AssembleHuffmanTable(jpeg_decompress_struct* jpegCompressInfo); 82 void AssembleQuantizationTable(jpeg_decompress_struct* jpegCompressInfo); 83 bool AssembleJpegImgHeader(jpeg_decompress_struct* jpegCompressInfo); 84 bool CopySrcImgToDecodeInputBuffer(ImagePlugin::InputDataStream *srcStream); 85 bool IsStandAloneJpegMarker(uint16_t marker); 86 bool JumpOverCurrentJpegMarker(const uint8_t* data, unsigned int& curPos, unsigned int totalLen, uint16_t marker); 87 bool GetCompressedDataStart(); 88 bool PrepareInputData(SkCodec *codec, ImagePlugin::InputDataStream *srcStream); 89 bool DoDecode(OHOS::HDI::Codec::Image::V2_0::CodecImageBuffer& outputBufferHandle); 90 void RecycleAllocatedResource(); 91 static OHOS::HDI::Display::Buffer::V1_0::IDisplayBuffer* GetBufferMgr(); 92 bool CheckInputColorFmt(SkCodec *codec); 93 private: 94 static constexpr char JPEG_FORMAT_DESC[] = "image/jpeg"; 95 OHOS::sptr<OHOS::HDI::Codec::Image::V2_0::ICodecImage> hwDecoder_; 96 OHOS::HDI::Display::Buffer::V1_0::IDisplayBuffer* bufferMgr_; 97 OHOS::HDI::Codec::Image::V2_0::CodecImageBuffer inputBuffer_; 98 OHOS::HDI::Codec::Image::V2_0::CodecJpegDecInfo decodeInfo_; 99 }; 100 } // namespace ImagePlugin 101 } // namespace OHOS 102 103 #endif // JPEG_HARDWARE_DECODER_H