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