1 /* 2 * Copyright (C) 2023 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 MEDIA_AVCODEC_VIDEO_DECODER_H 17 #define MEDIA_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 #include "drm_i_keysession_service.h" 25 26 namespace OHOS { 27 namespace MediaAVCodec { 28 class AVCodecVideoDecoder { 29 public: 30 virtual ~AVCodecVideoDecoder() = default; 31 32 /** 33 * @brief Configure the decoder. This interface must be called before {@link Prepare} is called. 34 * 35 * @param format The format of the input data and the desired format of the output data. 36 * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise. 37 * @since 3.1 38 * @version 3.1 39 */ 40 virtual int32_t Configure(const Format &format) = 0; 41 42 /** 43 * @brief Prepare for decoding. 44 * 45 * This function must be called after {@link Configure} and before {@link Start} 46 * 47 * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise. 48 * @since 3.1 49 * @version 3.1 50 */ 51 virtual int32_t Prepare() = 0; 52 53 /** 54 * @brief Start decoding. 55 * 56 * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise. 57 * @since 3.1 58 * @version 3.1 59 */ 60 virtual int32_t Start() = 0; 61 62 /** 63 * @brief Stop decoding. 64 * 65 * This function must be called during running 66 * 67 * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise. 68 * @since 3.1 69 * @version 3.1 70 */ 71 virtual int32_t Stop() = 0; 72 73 /** 74 * @brief Flush both input and output buffers of the decoder. 75 * 76 * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise. 77 * @since 3.1 78 * @version 3.1 79 */ 80 virtual int32_t Flush() = 0; 81 82 /** 83 * @brief Restores the decoder to the initial state. 84 * 85 * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise. 86 * @since 3.1 87 * @version 3.1 88 */ 89 virtual int32_t Reset() = 0; 90 91 /** 92 * @brief Releases decoder resources. All methods are unavailable after calling this. 93 * 94 * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise. 95 * @since 3.1 96 * @version 3.1 97 */ 98 virtual int32_t Release() = 0; 99 100 /** 101 * @brief Sets the surface on which to render the output of this decoder. 102 * 103 * This function must be called before {@link Prepare} 104 * 105 * @param index The index of the output buffer. 106 * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise. 107 * @since 3.1 108 * @version 3.1 109 */ 110 virtual int32_t SetOutputSurface(sptr<Surface> surface) = 0; 111 112 /** 113 * @brief Submits input buffer to decoder. 114 * 115 * This function must be called during running. The {@link AVCodecCallback} callback 116 * will report the available input buffer and the corresponding index value. Once the buffer with the specified 117 * index is submitted to the video decoder, the buffer cannot be accessed again until the {@link 118 * AVCodecCallback} callback is received again reporting that the buffer with the same index is available. 119 * In addition, for some decoders, it is required to input Codec-Specific-Data to the decoder at the beginning to 120 * initialize the decoding process of the decoder, such as PPS/SPS data in H264 format. 121 * 122 * @param index The index of the input buffer. 123 * @param info The info of the input buffer. For details, see {@link AVCodecBufferInfo} 124 * @param flag The flag of the input buffer. For details, see {@link AVCodecBufferFlag} 125 * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise. 126 * @since 3.1 127 * @version 3.1 128 */ 129 virtual int32_t QueueInputBuffer(uint32_t index, AVCodecBufferInfo info, AVCodecBufferFlag flag) = 0; 130 131 /** 132 * @brief Submits input buffer to decoder. 133 * 134 * This function must be called during running. The {@link MediaCodecCallback} callback 135 * will report the available input buffer and the corresponding index value. Once the buffer with the specified 136 * index is submitted to the video decoder, the buffer cannot be accessed again until the {@link 137 * MediaCodecCallback} callback is received again reporting that the buffer with the same index is available. 138 * In addition, for some decoders, it is required to input Codec-Specific-Data to the decoder at the beginning to 139 * initialize the decoding process of the decoder, such as PPS/SPS data in H264 format. 140 * 141 * @param index The index of the input buffer. 142 * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise. 143 * @since 4.1 144 */ 145 virtual int32_t QueueInputBuffer(uint32_t index) = 0; 146 147 /** 148 * @brief Gets the format of the output data. 149 * 150 * This function must be called after {@link Configure} 151 * 152 * @param format 153 * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise. 154 * @since 3.1 155 * @version 3.1 156 */ 157 virtual int32_t GetOutputFormat(Format &format) = 0; 158 159 /** 160 * @brief Returns the output buffer to the decoder. 161 * 162 * This function must be called during running, and notify the decoder to finish rendering the 163 * decoded data contained in the Buffer on the output Surface. If the output surface is not configured before, 164 * calling this interface only returns the output buffer corresponding to the specified index to the decoder. 165 * 166 * @param index The index of the output buffer. 167 * @return Returns {@link AVCS_ERR_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 Return the processed output buffer with render timestamp to the decoder, and notify the decoder to finish 175 * rendering the decoded data contained in the buffer on the output surface. If the output surface is not 176 * configured before, calling this interface only returns the output buffer corresponding to the specified index to 177 * the decoder. The timestamp may have special meaning depending on the destination surface. 178 * 179 * This function must be called during running 180 * 181 * @param index The index of the output buffer. 182 * @param renderTimestampNs The timestamp is associated with the output buffer when it is sent to the surface. The 183 * unit is nanosecond. 184 * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise. 185 * @since 5.0 186 */ 187 virtual int32_t RenderOutputBufferAtTime(uint32_t index, int64_t renderTimestampNs) = 0; 188 189 /** 190 * @brief Sets the parameters to the decoder. 191 * 192 * This interface can only be called after the decoder is started. 193 * At the same time, incorrect parameter settings may cause decoding failure. 194 * 195 * @param format The parameters. 196 * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise. 197 * @since 3.1 198 * @version 3.1 199 */ 200 virtual int32_t SetParameter(const Format &format) = 0; 201 202 /** 203 * @brief Registers a decoder listener. 204 * 205 * This function must be called before {@link Configure} 206 * 207 * @param callback Indicates the decoder listener to register. For details, see {@link AVCodecCallback}. 208 * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise. 209 * @since 3.1 210 * @version 3.1 211 */ 212 virtual int32_t SetCallback(const std::shared_ptr<AVCodecCallback> &callback) = 0; 213 214 /** 215 * @brief Registers a decoder listener. 216 * 217 * This function must be called before {@link Configure} 218 * 219 * @param callback Indicates the decoder listener to register. For details, see {@link MediaCodecCallback}. 220 * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise. 221 * @since 4.1 222 */ 223 virtual int32_t SetCallback(const std::shared_ptr<MediaCodecCallback> &callback) = 0; 224 225 /* 226 * @brief Set media key session which includes a decrypt module and a svp flag for decrypt video. 227 * @param svp is the flag whether use secure decoder 228 * @return Returns AV_ERR_OK if the execution is successful, 229 * otherwise returns a specific error code, refer to {@link OH_AVErrCode} 230 * @since 231 */ SetDecryptConfig(const sptr<DrmStandard::IMediaKeySessionService> & keySession,const bool svpFlag)232 virtual int32_t SetDecryptConfig(const sptr<DrmStandard::IMediaKeySessionService> &keySession, 233 const bool svpFlag) 234 { 235 (void)keySession; 236 (void)svpFlag; 237 return 0; 238 } 239 240 /** 241 * @brief Get video Channel Id. All methods are unavailable after calling this. 242 * 243 * @param channelId channel id 244 * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise. 245 * @since 3.1 246 * @version 3.1 247 */ 248 virtual int32_t GetChannelId(int32_t &channelId) = 0; 249 250 /** 251 * @brief Sets the lpp mode to the decoder. 252 * 253 * @param isLpp lpp mode. 254 * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise. 255 * @since 3.1 256 * @version 3.1 257 */ 258 virtual int32_t SetLowPowerPlayerMode(bool isLpp) = 0; 259 260 /* 261 * @brief Recycle dma memory when decoder user has been frozen 262 * 263 * This function must be called before {@link Configure}, only provided for {@link Player} 264 * 265 * @param exchangeFlag the flag whether memory should be displaced from RAM to ROM or from ROM to RAM 266 * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise. 267 * @since 6.0 268 * @version 6.0 269 */ NotifyMemoryExchange(const bool exchangeFlag)270 virtual int32_t NotifyMemoryExchange(const bool exchangeFlag) 271 { 272 (void)exchangeFlag; 273 return 0; 274 } 275 276 /** 277 * @brief Query available input buffer from decoder 278 * 279 * This function blocks until an input buffer becomes available or timeout occurs. 280 * 281 * @param index [out] Reference to store the index of available input buffer 282 * @param timeoutUs Timeout duration in microseconds (negative value means infinite wait) 283 * @return Returns {@link AVCS_ERR_OK} if buffer is available; 284 * returns error code if timeout or other failures occur. 285 * @since 6.0 286 * @version 6.0 287 */ QueryInputBuffer(uint32_t & index,int64_t timeoutUs)288 virtual int32_t QueryInputBuffer(uint32_t &index, int64_t timeoutUs) 289 { 290 (void)index; 291 (void)timeoutUs; 292 return 0; 293 } 294 295 /** 296 * @brief Query available output buffer from decoder 297 * 298 * This function blocks until an output buffer with decoded data becomes available or timeout occurs. 299 * 300 * @param index [out] Reference to store the index of available output buffer 301 * @param timeoutUs Timeout duration in microseconds (negative value means infinite wait) 302 * @return Returns {@link AVCS_ERR_OK} if buffer is available; 303 * returns error code if timeout or other failures occur. 304 * @since 6.0 305 * @version 6.0 306 */ QueryOutputBuffer(uint32_t & index,int64_t timeoutUs)307 virtual int32_t QueryOutputBuffer(uint32_t &index, int64_t timeoutUs) 308 { 309 (void)index; 310 (void)timeoutUs; 311 return 0; 312 } 313 314 /** 315 * @brief Get input buffer object by index 316 * 317 * Caller should use {@link QueryInputBuffer} to get valid index before calling this function. 318 * 319 * @param index Index of the input buffer obtained from {@link QueryInputBuffer} 320 * @return Shared pointer to {@link AVBuffer} if index is valid; 321 * returns nullptr if index is invalid or buffer unavailable. 322 * @since 6.0 323 * @version 6.0 324 */ GetInputBuffer(uint32_t index)325 virtual std::shared_ptr<AVBuffer> GetInputBuffer(uint32_t index) 326 { 327 (void)index; 328 return nullptr; 329 } 330 331 /** 332 * @brief Get output buffer object by index 333 * 334 * Caller should use {@link QueryOutputBuffer} to get valid index before calling this function. 335 * 336 * @param index Index of the output buffer obtained from {@link QueryOutputBuffer} 337 * @return Shared pointer to {@link AVBuffer} containing decoded data if index is valid; 338 * returns nullptr if index is invalid or buffer unavailable. 339 * @since 6.0 340 * @version 6.0 341 */ GetOutputBuffer(uint32_t index)342 virtual std::shared_ptr<AVBuffer> GetOutputBuffer(uint32_t index) 343 { 344 (void)index; 345 return nullptr; 346 } 347 }; 348 349 class __attribute__((visibility("default"))) VideoDecoderFactory { 350 public: 351 #ifdef UNSUPPORT_CODEC CreateByMime(const std::string & mime)352 static std::shared_ptr<AVCodecVideoDecoder> CreateByMime(const std::string &mime) 353 { 354 (void)mime; 355 return nullptr; 356 } 357 CreateByName(const std::string & name)358 static std::shared_ptr<AVCodecVideoDecoder> CreateByName(const std::string &name) 359 { 360 (void)name; 361 return nullptr; 362 } 363 CreateByMime(const std::string & mime,Format & format,std::shared_ptr<AVCodecVideoDecoder> & decoder)364 static int32_t CreateByMime(const std::string &mime, Format &format, std::shared_ptr<AVCodecVideoDecoder> &decoder) 365 { 366 (void)name; 367 (void)format; 368 codec = nullptr; 369 return codec; 370 } 371 CreateByName(const std::string & name,Format & format,std::shared_ptr<AVCodecVideoDecoder> & decoder)372 static int32_t CreateByName(const std::string &name, Format &format, std::shared_ptr<AVCodecVideoDecoder> &decoder) 373 { 374 (void)name; 375 (void)format; 376 codec = nullptr; 377 return codec; 378 } 379 #else 380 /** 381 * @brief Instantiate the preferred decoder of the given mime type. 382 * 383 * @param mime The mime type. 384 * @return Returns the preferred decoder. 385 * @since 3.1 386 * @version 3.1 387 */ 388 static std::shared_ptr<AVCodecVideoDecoder> CreateByMime(const std::string &mime); 389 390 /** 391 * @brief Instantiates the designated decoder. 392 * 393 * @param name The decoder's name. 394 * @return Returns the designated decoder. 395 * @since 3.1 396 * @version 3.1 397 */ 398 static std::shared_ptr<AVCodecVideoDecoder> CreateByName(const std::string &name); 399 400 /** 401 * @brief Instantiate the preferred decoder of the given mime type. 402 * 403 * @param mime The mime type. 404 * @param format Caller info 405 * @param codec The designated decoder. 406 * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise. 407 * @since 5.0 408 * @version 5.0 409 */ 410 static int32_t CreateByMime(const std::string &mime, Format &format, std::shared_ptr<AVCodecVideoDecoder> &decoder); 411 412 /** 413 * @brief Instantiate the preferred decoder of the given mime type. 414 * 415 * @param mime The mime type. 416 * @param format Caller info 417 * @param codec The designated decoder. 418 * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise. 419 * @since 5.0 420 * @version 5.0 421 */ 422 static int32_t CreateByName(const std::string &name, Format &format, std::shared_ptr<AVCodecVideoDecoder> &decoder); 423 #endif 424 private: 425 VideoDecoderFactory() = default; 426 ~VideoDecoderFactory() = default; 427 }; 428 } // namespace MediaAVCodec 429 } // namespace OHOS 430 #endif // MEDIA_AVCODEC_VIDEO_DECODER_H