1 /* 2 * Copyright (C) 2021 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 AVCODEC_VIDEO_DECODER_H 17 #define AVCODEC_VIDEO_DECODER_H 18 19 #include "avcodec_common.h" 20 #include "avcodec_info.h" 21 #include "buffer/avsharedmemory.h" 22 #include "meta/format.h" 23 #include "surface.h" 24 25 namespace OHOS { 26 namespace Media { 27 class AVCodecVideoDecoder { 28 public: 29 virtual ~AVCodecVideoDecoder() = default; 30 31 /** 32 * @brief Configure the decoder. 33 * 34 * @param format The format of the input data and the desired format of the output data. 35 * @return Returns {@link MSERR_OK} if success; returns an error code otherwise. 36 * @since 3.1 37 * @version 3.1 38 */ 39 virtual int32_t Configure(const Format &format) = 0; 40 41 /** 42 * @brief Prepare for decoding. 43 * 44 * This function must be called after {@link Configure} and before {@link Start} 45 * 46 * @return Returns {@link MSERR_OK} if success; returns an error code otherwise. 47 * @since 3.1 48 * @version 3.1 49 */ 50 virtual int32_t Prepare() = 0; 51 52 /** 53 * @brief Start decoding. 54 * 55 * @return Returns {@link MSERR_OK} if success; returns an error code otherwise. 56 * @since 3.1 57 * @version 3.1 58 */ 59 virtual int32_t Start() = 0; 60 61 /** 62 * @brief Stop decoding. 63 * 64 * This function must be called during running 65 * 66 * @return Returns {@link MSERR_OK} if success; returns an error code otherwise. 67 * @since 3.1 68 * @version 3.1 69 */ 70 virtual int32_t Stop() = 0; 71 72 /** 73 * @brief Flush both input and output buffers of the decoder. 74 * 75 * @return Returns {@link MSERR_OK} if success; returns an error code otherwise. 76 * @since 3.1 77 * @version 3.1 78 */ 79 virtual int32_t Flush() = 0; 80 81 /** 82 * @brief Restores the decoder to the initial state. 83 * 84 * @return Returns {@link MSERR_OK} if success; returns an error code otherwise. 85 * @since 3.1 86 * @version 3.1 87 */ 88 virtual int32_t Reset() = 0; 89 90 /** 91 * @brief Releases decoder resources. All methods are unavailable after calling this. 92 * 93 * @return Returns {@link MSERR_OK} if success; returns an error code otherwise. 94 * @since 3.1 95 * @version 3.1 96 */ 97 virtual int32_t Release() = 0; 98 99 /** 100 * @brief Sets the surface on which to render the output of this decoder. 101 * 102 * This function must be called before {@link Prepare} 103 * 104 * @param index The index of the output buffer. 105 * @return Returns {@link MSERR_OK} if success; returns an error code otherwise. 106 * @since 3.1 107 * @version 3.1 108 */ 109 virtual int32_t SetOutputSurface(sptr<Surface> surface) = 0; 110 111 /** 112 * @brief Returns a {@link AVSharedMemory} object for a input buffer index that contains the data. 113 * 114 * This function must be called during running 115 * 116 * @param index The index of the input buffer. 117 * @return Returns {@link AVSharedMemory} if success; returns nullptr otherwise. 118 * @since 3.1 119 * @version 3.1 120 */ 121 virtual std::shared_ptr<AVSharedMemory> GetInputBuffer(uint32_t index) = 0; 122 123 /** 124 * @brief Submits input buffer to decoder. 125 * 126 * This function must be called during running 127 * 128 * @param index The index of the input buffer. 129 * @param info The info of the input buffer. For details, see {@link AVCodecBufferInfo} 130 * @param flag The flag of the input buffer. For details, see {@link AVCodecBufferFlag} 131 * @return Returns {@link MSERR_OK} if success; returns an error code otherwise. 132 * @since 3.1 133 * @version 3.1 134 */ 135 virtual int32_t QueueInputBuffer(uint32_t index, AVCodecBufferInfo info, AVCodecBufferFlag flag) = 0; 136 137 /** 138 * @brief Returns a {@link AVSharedMemory} object for a output buffer index that contains the data. 139 * 140 * This function must be called during running 141 * 142 * @param index The index of the output buffer. 143 * @return Returns {@link AVSharedMemory} if success; returns nullptr otherwise. 144 * @since 3.1 145 * @version 3.1 146 */ 147 virtual std::shared_ptr<AVSharedMemory> GetOutputBuffer(uint32_t index) = 0; 148 149 /** 150 * @brief Gets the format of the output data. 151 * 152 * This function must be called after {@link Configure} 153 * 154 * @param format 155 * @return Returns {@link MSERR_OK} if success; returns an error code otherwise. 156 * @since 3.1 157 * @version 3.1 158 */ 159 virtual int32_t GetOutputFormat(Format &format) = 0; 160 161 /** 162 * @brief Returns the output buffer to the decoder. 163 * 164 * This function must be called during running 165 * 166 * @param index The index of the output buffer. 167 * @return Returns {@link MSERR_OK} if success; returns an error code otherwise. 168 * @since 3.1 169 * @version 3.1 170 */ 171 virtual int32_t ReleaseOutputBuffer(uint32_t index, bool render) = 0; 172 173 /** 174 * @brief Sets the parameters to the decoder. 175 * 176 * This function must be called after {@link Configure} 177 * 178 * @param format The parameters. 179 * @return Returns {@link MSERR_OK} if success; returns an error code otherwise. 180 * @since 3.1 181 * @version 3.1 182 */ 183 virtual int32_t SetParameter(const Format &format) = 0; 184 185 /** 186 * @brief Registers a decoder listener. 187 * 188 * This function must be called before {@link Configure} 189 * 190 * @param callback Indicates the decoder listener to register. For details, see {@link AVCodecCallback}. 191 * @return Returns {@link MSERR_OK} if success; returns an error code otherwise. 192 * @since 3.1 193 * @version 3.1 194 */ 195 virtual int32_t SetCallback(const std::shared_ptr<AVCodecCallback> &callback) = 0; 196 }; 197 198 class __attribute__((visibility("default"))) VideoDecoderFactory { 199 public: 200 #ifdef UNSUPPORT_CODEC CreateByMime(const std::string & mime)201 static std::shared_ptr<AVCodecVideoDecoder> CreateByMime(const std::string &mime) 202 { 203 (void)mime; 204 return nullptr; 205 } 206 CreateByName(const std::string & name)207 static std::shared_ptr<AVCodecVideoDecoder> CreateByName(const std::string &name) 208 { 209 (void)name; 210 return nullptr; 211 } 212 #else 213 /** 214 * @brief Instantiate the preferred decoder of the given mime type. 215 * 216 * @param mime The mime type. 217 * @return Returns the preferred decoder. 218 * @since 3.1 219 * @version 3.1 220 */ 221 static std::shared_ptr<AVCodecVideoDecoder> CreateByMime(const std::string &mime); 222 223 /** 224 * @brief Instantiates the designated decoder. 225 * 226 * @param name The decoder's name. 227 * @return Returns the designated decoder. 228 * @since 3.1 229 * @version 3.1 230 */ 231 static std::shared_ptr<AVCodecVideoDecoder> CreateByName(const std::string &name); 232 #endif 233 private: 234 VideoDecoderFactory() = default; 235 ~VideoDecoderFactory() = default; 236 }; 237 } // namespace Media 238 } // namespace OHOS 239 #endif // AVCODEC_VIDEO_DECODER_H