blob: fa51aeff706b609ac0ebeb7a6c88a6b02a1c7130 [file] [log] [blame]
Chong Zhangea280cb2017-08-02 11:10:10 -07001/*
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#ifndef _HEIF_DECODER_API_
18#define _HEIF_DECODER_API_
19
Chong Zhang8541d302019-07-12 11:19:47 -070020#include <vector>
Chong Zhangea280cb2017-08-02 11:10:10 -070021
22/*
23 * The output color pixel format of heif decoder.
24 */
25typedef enum {
Dichen Zhang1b3441a2021-11-17 11:54:43 -080026 kHeifColorFormat_RGB565 = 0,
27 kHeifColorFormat_RGBA_8888 = 1,
28 kHeifColorFormat_BGRA_8888 = 2,
29 kHeifColorFormat_RGBA_1010102 = 3,
Chong Zhangea280cb2017-08-02 11:10:10 -070030} HeifColorFormat;
31
32/*
33 * The color spaces encoded in the heif image.
34 */
35typedef enum {
36 kHeifEncodedColor_RGB = 0,
37 kHeifEncodedColor_YUV = 1,
38 kHeifEncodedColor_CMYK = 2,
39} HeifEncodedColor;
40
41/*
42 * Represents a color converted (RGB-based) video frame
43 */
Chong Zhang8541d302019-07-12 11:19:47 -070044struct HeifFrameInfo {
Chong Zhangea280cb2017-08-02 11:10:10 -070045 uint32_t mWidth;
46 uint32_t mHeight;
47 int32_t mRotationAngle; // Rotation angle, clockwise, should be multiple of 90
48 uint32_t mBytesPerPixel; // Number of bytes for one pixel
Chong Zhang8541d302019-07-12 11:19:47 -070049 int64_t mDurationUs; // Duration of the frame in us
50 std::vector<uint8_t> mIccData; // ICC data array
Chong Zhangea280cb2017-08-02 11:10:10 -070051};
52
53/*
54 * Abstract interface to provide data to HeifDecoder.
55 */
56struct HeifStream {
57 HeifStream() {}
58
59 virtual ~HeifStream() {}
60
61 /*
62 * Reads or skips size number of bytes. return the number of bytes actually
63 * read or skipped.
64 * If |buffer| == NULL, skip size bytes, return how many were skipped.
65 * If |buffer| != NULL, copy size bytes into buffer, return how many were copied.
66 */
67 virtual size_t read(void* buffer, size_t size) = 0;
68
69 /*
70 * Rewinds to the beginning of the stream. Returns true if the stream is known
71 * to be at the beginning after this call returns.
72 */
73 virtual bool rewind() = 0;
74
75 /*
76 * Seeks to an absolute position in the stream. If this cannot be done, returns false.
77 * If an attempt is made to seek past the end of the stream, the position will be set
78 * to the end of the stream.
79 */
80 virtual bool seek(size_t /*position*/) = 0;
81
82 /** Returns true if this stream can report its total length. */
83 virtual bool hasLength() const = 0;
84
85 /** Returns the total length of the stream. If this cannot be done, returns 0. */
86 virtual size_t getLength() const = 0;
87
88private:
Chong Zhang8541d302019-07-12 11:19:47 -070089 HeifStream(const HeifStream&) = delete;
90 HeifStream& operator=(const HeifStream&) = delete;
Chong Zhangea280cb2017-08-02 11:10:10 -070091};
92
93/*
94 * Abstract interface to decode heif images from a HeifStream data source.
95 */
96struct HeifDecoder {
97 HeifDecoder() {}
98
99 virtual ~HeifDecoder() {}
100
101 /*
102 * Returns true if it successfully sets outColor to the encoded color,
103 * and false otherwise.
104 */
105 virtual bool getEncodedColor(HeifEncodedColor* outColor) const = 0;
106
107 /*
108 * Returns true if it successfully sets the output color format to color,
109 * and false otherwise.
110 */
111 virtual bool setOutputColor(HeifColorFormat color) = 0;
112
113 /*
114 * Returns true if it successfully initialize heif decoder with source,
115 * and false otherwise. |frameInfo| will be filled with information of
116 * the primary picture upon success and unmodified upon failure.
117 * Takes ownership of |stream| regardless of result.
118 */
119 virtual bool init(HeifStream* stream, HeifFrameInfo* frameInfo) = 0;
120
121 /*
Chong Zhang8541d302019-07-12 11:19:47 -0700122 * Returns true if the stream contains an image sequence and false otherwise.
123 * |frameInfo| will be filled with information of pictures in the sequence
124 * and |frameCount| the length of the sequence upon success and unmodified
125 * upon failure.
126 */
127 virtual bool getSequenceInfo(HeifFrameInfo* frameInfo, size_t *frameCount) = 0;
128
129 /*
Chong Zhangea280cb2017-08-02 11:10:10 -0700130 * Decode the picture internally, returning whether it succeeded. |frameInfo|
131 * will be filled with information of the primary picture upon success and
132 * unmodified upon failure.
133 *
134 * After this succeeded, getScanline can be called to read the scanlines
135 * that were decoded.
136 */
137 virtual bool decode(HeifFrameInfo* frameInfo) = 0;
138
139 /*
Chong Zhang8541d302019-07-12 11:19:47 -0700140 * Decode the picture from the image sequence at index |frameIndex|.
141 * |frameInfo| will be filled with information of the decoded picture upon
142 * success and unmodified upon failure.
143 *
144 * |frameIndex| is the 0-based index of the video frame to retrieve. The frame
145 * index must be that of a valid frame. The total number of frames available for
146 * retrieval was reported via getSequenceInfo().
147 *
148 * After this succeeded, getScanline can be called to read the scanlines
149 * that were decoded.
150 */
151 virtual bool decodeSequence(int frameIndex, HeifFrameInfo* frameInfo) = 0;
152
153 /*
Chong Zhangea280cb2017-08-02 11:10:10 -0700154 * Read the next scanline (in top-down order), returns true upon success
155 * and false otherwise.
156 */
157 virtual bool getScanline(uint8_t* dst) = 0;
158
159 /*
160 * Skip the next |count| scanlines, returns true upon success and
161 * false otherwise.
162 */
163 virtual size_t skipScanlines(size_t count) = 0;
164
165private:
166 HeifDecoder(const HeifFrameInfo&) = delete;
167 HeifDecoder& operator=(const HeifFrameInfo&) = delete;
168};
169
170/*
171 * Creates a HeifDecoder. Returns a HeifDecoder instance upon success, or NULL
172 * if the creation has failed.
173 */
174HeifDecoder* createHeifDecoder();
175
176#endif // _HEIF_DECODER_API_