1 /* 2 * Copyright (C) 2022 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 NATIVE_AVCODEC_VIDEOENCODER_H 17 #define NATIVE_AVCODEC_VIDEOENCODER_H 18 19 #include <stdint.h> 20 #include <stdio.h> 21 #include "native_averrors.h" 22 #include "native_avformat.h" 23 #include "native_avmemory.h" 24 #include "native_avcodec_base.h" 25 26 #ifdef __cplusplus 27 extern "C" { 28 #endif 29 30 /** 31 * @brief Creates a video encoder instance from the mime type, which is recommended in most cases. 32 * @syscap SystemCapability.Multimedia.Media.VideoEncoder 33 * @param mime mime type description string, refer to {@link AVCODEC_MIME_TYPE} 34 * @return Returns a Pointer to an OH_AVCodec instance 35 * @since 9 36 * @version 1.0 37 */ 38 OH_AVCodec *OH_VideoEncoder_CreateByMime(const char *mime); 39 40 /** 41 * @brief Create a video encoder instance through the video encoder name. The premise of using this interface is to 42 * know the exact name of the encoder. 43 * @syscap SystemCapability.Multimedia.Media.VideoEncoder 44 * @param name Video encoder name 45 * @return Returns a Pointer to an OH_AVCodec instance 46 * @since 9 47 * @version 1.0 48 */ 49 OH_AVCodec *OH_VideoEncoder_CreateByName(const char *name); 50 51 /** 52 * @brief Clear the internal resources of the encoder and destroy the encoder instance 53 * @syscap SystemCapability.Multimedia.Media.VideoEncoder 54 * @param codec Pointer to an OH_AVCodec instance 55 * @return Returns AV_ERR_OK if the execution is successful, 56 * otherwise returns a specific error code, refer to {@link OH_AVErrCode} 57 * @since 9 58 * @version 1.0 59 */ 60 OH_AVErrCode OH_VideoEncoder_Destroy(OH_AVCodec *codec); 61 62 /** 63 * @brief Set the asynchronous callback function so that your application can respond to the events generated by the 64 * video encoder. This interface must be called before Prepare is called 65 * @syscap SystemCapability.Multimedia.Media.VideoEncoder 66 * @param codec Pointer to an OH_AVCodec instance 67 * @param callback A collection of all callback functions, see {@link OH_AVCodecAsyncCallback} 68 * @param userData User specific data 69 * @return Returns AV_ERR_OK if the execution is successful, 70 * otherwise returns a specific error code, refer to {@link OH_AVErrCode} 71 * @since 9 72 * @version 1.0 73 */ 74 OH_AVErrCode OH_VideoEncoder_SetCallback(OH_AVCodec *codec, OH_AVCodecAsyncCallback callback, void *userData); 75 76 /** 77 * @brief To configure the video encoder, typically, you need to configure the description information of the 78 * encoded video track. This interface must be called before Prepare is called. 79 * @syscap SystemCapability.Multimedia.Media.VideoEncoder 80 * @param codec Pointer to an OH_AVCodec instance 81 * @param format A pointer to an OH_AVFormat that gives the description of the video track to be encoded 82 * @return Returns AV_ERR_OK if the execution is successful, 83 * otherwise returns a specific error code, refer to {@link OH_AVErrCode} 84 * @since 9 85 * @version 1.0 86 */ 87 OH_AVErrCode OH_VideoEncoder_Configure(OH_AVCodec *codec, OH_AVFormat *format); 88 89 /** 90 * @brief To prepare the internal resources of the encoder, the Configure interface must be called before 91 * calling this interface. 92 * @syscap SystemCapability.Multimedia.Media.VideoEncoder 93 * @param codec Pointer to an OH_AVCodec instance 94 * @return Returns AV_ERR_OK if the execution is successful, 95 * otherwise returns a specific error code, refer to {@link OH_AVErrCode} 96 * @since 9 97 * @version 1.0 98 */ 99 OH_AVErrCode OH_VideoEncoder_Prepare(OH_AVCodec *codec); 100 101 /** 102 * @brief Start the encoder, this interface must be called after the Prepare is successful. After being 103 * successfully started, the encoder will start reporting NeedInputData events. 104 * @syscap SystemCapability.Multimedia.Media.VideoEncoder 105 * @param codec Pointer to an OH_AVCodec instance 106 * @return Returns AV_ERR_OK if the execution is successful, 107 * otherwise returns a specific error code, refer to {@link OH_AVErrCode} 108 * @since 9 109 * @version 1.0 110 */ 111 OH_AVErrCode OH_VideoEncoder_Start(OH_AVCodec *codec); 112 113 /** 114 * @brief Stop the encoder. After stopping, you can re-enter the Started state through Start. 115 * @syscap SystemCapability.Multimedia.Media.VideoEncoder 116 * @param codec Pointer to an OH_AVCodec instance 117 * @return Returns AV_ERR_OK if the execution is successful, 118 * otherwise returns a specific error code, refer to {@link OH_AVErrCode} 119 * @since 9 120 * @version 1.0 121 */ 122 OH_AVErrCode OH_VideoEncoder_Stop(OH_AVCodec *codec); 123 124 /** 125 * @brief Clear the input and output data buffered in the encoder. After this interface is called, all the Buffer 126 * indexes previously reported through the asynchronous callback will be invalidated, make sure not to access the 127 * Buffers corresponding to these indexes. 128 * @syscap SystemCapability.Multimedia.Media.VideoEncoder 129 * @param codec Pointer to an OH_AVCodec instance 130 * @return Returns AV_ERR_OK if the execution is successful, 131 * otherwise returns a specific error code, refer to {@link OH_AVErrCode} 132 * @since 9 133 * @version 1.0 134 */ 135 OH_AVErrCode OH_VideoEncoder_Flush(OH_AVCodec *codec); 136 137 /** 138 * @brief Reset the encoder. To continue coding, you need to call the Configure interface again to 139 * configure the encoder instance. 140 * @syscap SystemCapability.Multimedia.Media.VideoEncoder 141 * @param codec Pointer to an OH_AVCodec instance 142 * @return Returns AV_ERR_OK if the execution is successful, 143 * otherwise returns a specific error code, refer to {@link OH_AVErrCode} 144 * @since 9 145 * @version 1.0 146 */ 147 OH_AVErrCode OH_VideoEncoder_Reset(OH_AVCodec *codec); 148 149 /** 150 * @brief Get the description information of the output data of the encoder, refer to {@link OH_AVFormat} for details. 151 * It should be noted that the life cycle of the OH_AVFormat instance pointed to by the return value * needs to 152 * be manually released by the caller. 153 * @syscap SystemCapability.Multimedia.Media.VideoEncoder 154 * @param codec Pointer to an OH_AVCodec instance 155 * @return Returns a pointer to an OH_AVFormat instance 156 * @since 9 157 * @version 1.0 158 */ 159 OH_AVFormat *OH_VideoEncoder_GetOutputDescription(OH_AVCodec *codec); 160 161 /** 162 * @brief Set dynamic parameters to the encoder. Note: This interface can only be called after the encoder is started. 163 * At the same time, incorrect parameter settings may cause the encoding to fail. 164 * @syscap SystemCapability.Multimedia.Media.VideoEncoder 165 * @param codec Pointer to an OH_AVCodec instance 166 * @param format OH_AVFormat handle pointer 167 * @return Returns AV_ERR_OK if the execution is successful, 168 * otherwise returns a specific error code, refer to {@link OH_AVErrCode} 169 * @since 9 170 * @version 1.0 171 */ 172 OH_AVErrCode OH_VideoEncoder_SetParameter(OH_AVCodec *codec, OH_AVFormat *format); 173 174 /** 175 * @brief Get the input Surface from the video encoder, this interface must be called before Prepare is called. 176 * @syscap SystemCapability.Multimedia.Media.VideoEncoder 177 * @param codec Pointer to an OH_AVCodec instance 178 * @param window A pointer to a OHNativeWindow instance, see {@link OHNativeWindow} 179 * @return Returns AV_ERR_OK if the execution is successful, 180 * otherwise returns a specific error code, refer to {@link OH_AVErrCode} 181 * @since 9 182 * @version 1.0 183 */ 184 OH_AVErrCode OH_VideoEncoder_GetSurface(OH_AVCodec *codec, OHNativeWindow **window); 185 186 /** 187 * @brief Return the processed output Buffer to the encoder. 188 * @syscap SystemCapability.Multimedia.Media.VideoEncoder 189 * @param codec Pointer to an OH_AVCodec instance 190 * @param index The index value corresponding to the output Buffer 191 * @return Returns AV_ERR_OK if the execution is successful, 192 * otherwise returns a specific error code, refer to {@link OH_AVErrCode} 193 * @since 9 194 * @version 1.0 195 */ 196 OH_AVErrCode OH_VideoEncoder_FreeOutputData(OH_AVCodec *codec, uint32_t index); 197 198 /** 199 * @brief Notifies the video encoder that the input stream has ended. It is recommended to use this interface to notify 200 * the encoder of the end of the stream in surface mode 201 * @syscap SystemCapability.Multimedia.Media.VideoEncoder 202 * @param codec Pointer to an OH_AVCodec instance 203 * @return Returns AV_ERR_OK if the execution is successful, 204 * otherwise returns a specific error code, refer to {@link OH_AVErrCode} 205 * @since 9 206 * @version 1.0 207 */ 208 OH_AVErrCode OH_VideoEncoder_NotifyEndOfStream(OH_AVCodec *codec); 209 210 /** 211 * @brief The bitrate mode of video encoder. 212 * @syscap SystemCapability.Multimedia.Media.VideoEncoder 213 * @since 9 214 * @version 1.0 215 */ 216 typedef enum OH_VideoEncodeBitrateMode { 217 /* constant bit rate mode. */ 218 CBR = 0, 219 /* variable bit rate mode. */ 220 VBR = 1, 221 /* constant quality mode. */ 222 CQ = 2, 223 } OH_VideoEncodeBitrateMode; 224 225 #ifdef __cplusplus 226 } 227 #endif 228 229 #endif // NATIVE_AVCODEC_VIDEOENCODER_H