1 /*
2  * Copyright (C) 2024 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 PLUGINS_COMMON_LIBS_IMAGE_LIBEXTPLUGIN_INCLUDE_HEIF_DECODER_H
17 #define PLUGINS_COMMON_LIBS_IMAGE_LIBEXTPLUGIN_INCLUDE_HEIF_DECODER_H
18 
19 #include <cstddef>
20 #include <cstdint>
21 #include <vector>
22 #include "heif_type.h"
23 #include "image_type.h"
24 
25 enum SkHeifColorFormat {
26     kHeifColorFormat_RGB565,
27     kHeifColorFormat_RGBA_8888,
28     kHeifColorFormat_BGRA_8888,
29     kHeifColorFormat_NV12,
30     kHeifColorFormat_NV21,
31     kHeifColorFormat_RGBA_1010102,
32     kHeifColorFormat_P010_NV12,
33     kHeifColorFormat_P010_NV21,
34 };
35 
36 struct HeifStream {
~HeifStreamHeifStream37     virtual ~HeifStream() {}
38 
39     virtual size_t read(void*, size_t) = 0;
40     virtual bool   rewind()            = 0;
41     virtual bool   seek(size_t)        = 0;
42     virtual bool   hasLength() const   = 0;
43     virtual size_t getLength() const   = 0;
44     virtual bool   hasPosition() const = 0;
45     virtual size_t getPosition() const = 0;
46 };
47 
48 struct HeifNclxColor {
49     uint16_t colorPrimaries = 0;
50     uint16_t transferCharacteristics = 0;;
51     uint16_t matrixCoefficients = 0;;
52     uint8_t fullRangeFlag = 0;;
53 };
54 
55 struct HeifFrameInfo {
56     uint32_t mWidth = 0;
57     uint32_t mHeight = 0;
58     int32_t  mRotationAngle = 0;           // Rotation angle, clockwise, should be multiple of 90
59     uint32_t mBytesPerPixel = 0;           // Number of bytes for one pixel
60     int64_t mDurationUs = 0;               // Duration of the frame in us
61     std::vector<uint8_t> mIccData;     // ICC data array
62     bool hasNclxColor = false;
63     HeifNclxColor nclxColor;
64 };
65 
66 enum class HeifImageHdrType {
67     UNKNOWN = 0,
68     VIVID_DUAL = 1,
69     VIVID_SINGLE,
70     ISO_DUAL,
71     ISO_SINGLE,
72 };
73 
74 struct HeifDecoder {
HeifDecoderHeifDecoder75     HeifDecoder() {}
76 
~HeifDecoderHeifDecoder77     virtual ~HeifDecoder() {}
78 
79     virtual bool init(HeifStream* stream, HeifFrameInfo* frameInfo) = 0;
80 
81     virtual bool getSequenceInfo(HeifFrameInfo* frameInfo, size_t *frameCount) = 0;
82 
83     virtual bool decode(HeifFrameInfo* frameInfo) = 0;
84 
85     virtual bool decodeSequence(int frameIndex, HeifFrameInfo* frameInfo) = 0;
86 
87     virtual bool setOutputColor(SkHeifColorFormat colorFormat) = 0;
88 
89     virtual void setDstBuffer(uint8_t *dstBuffer, size_t rowStride, void *context) = 0;
90 
91     virtual bool getScanline(uint8_t* dst) = 0;
92 
93     virtual size_t skipScanlines(int count) = 0;
94     virtual void getErrMsg(std::string& errMsg) = 0;
95     virtual bool getImageInfo(HeifFrameInfo *frameInfo) = 0;
96     virtual bool decodeGainmap() = 0;
97     virtual void setGainmapDstBuffer(uint8_t* dstBuffer, size_t rowStride) = 0;
98     virtual bool getGainmapInfo(HeifFrameInfo* frameInfo) = 0;
99     virtual bool getTmapInfo(HeifFrameInfo* frameInfo) = 0;
100     virtual HeifImageHdrType getHdrType() = 0;
101     virtual void getVividMetadata(std::vector<uint8_t>& uwaInfo, std::vector<uint8_t>& displayInfo,
102         std::vector<uint8_t>& lightInfo) = 0;
103     virtual void getISOMetadata(std::vector<uint8_t>& isoMetadata) = 0;
104 };
105 
106 #endif // PLUGINS_COMMON_LIBS_IMAGE_LIBEXTPLUGIN_INCLUDE_HEIF_DECODER_H
107