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