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_ENCODER_H 17 #define MEDIA_AVCODEC_VIDEO_ENCODER_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 MediaAVCodec { 27 class AVCodecVideoEncoder { 28 public: 29 virtual ~AVCodecVideoEncoder() = default; 30 31 /** 32 * @brief Configure the encoder. This interface must be called before {@link Prepare} is called. 33 * 34 * @param format The format of the input data and the desired format of the output data. 35 * @return Returns {@link AVCS_ERR_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 AVCS_ERR_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 AVCS_ERR_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 AVCS_ERR_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 encoder. 74 * 75 * @return Returns {@link AVCS_ERR_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 Notify eos of the encoder. It is recommended to use this interface to notify 83 * the encoder of the end of the stream in surface mode. 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 NotifyEos() = 0; 90 91 /** 92 * @brief Restores the encoder to the initial state. 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 Reset() = 0; 99 100 /** 101 * @brief Releases encoder resources. All methods are unavailable after calling this. 102 * 103 * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise. 104 * @since 3.1 105 * @version 3.1 106 */ 107 virtual int32_t Release() = 0; 108 109 /** 110 * @brief Obtains the surface from encoder. 111 * 112 * This function can only be called after {@link Configure} and before {@link Prepare} 113 * 114 * @return Returns the pointer to the surface. 115 * @since 3.1 116 * @version 3.1 117 */ 118 virtual sptr<Surface> CreateInputSurface() = 0; 119 120 /** 121 * @brief Submits input buffer to encoder. 122 * 123 * This function must be called during running. The {@link AVCodecCallback} callback 124 * will report the available input buffer and the corresponding index value. 125 * 126 * @param index The index of the input buffer. 127 * @param info The info of the input buffer. For details, see {@link AVCodecBufferInfo} 128 * @param flag The flag of the input buffer. For details, see {@link AVCodecBufferFlag} 129 * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise. 130 * @since 3.1 131 * @version 3.1 132 */ 133 virtual int32_t QueueInputBuffer(uint32_t index, AVCodecBufferInfo info, AVCodecBufferFlag flag) = 0; 134 135 /** 136 * @brief Submits input buffer to encoder. 137 * 138 * This function must be called during running. The {@link MediaCodecCallback} callback 139 * will report the available input buffer and the corresponding index value. 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 Submits input parameter to encoder. 149 * 150 * This function must be called during running. The {@link MediaCodecParameterCallback} callback 151 * will report the available input buffer and the corresponding index value. 152 * 153 * @param index The index of the input parameter. 154 * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise. 155 * @since 5.0 156 */ 157 virtual int32_t QueueInputParameter(uint32_t index) = 0; 158 159 /** 160 * @brief Gets the format of the output data. 161 * 162 * This function must be called after {@link Configure} 163 * 164 * @param format 165 * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise. 166 * @since 3.1 167 * @version 3.1 168 */ 169 virtual int32_t GetOutputFormat(Format &format) = 0; 170 171 /** 172 * @brief Returns the output buffer to the encoder. 173 * 174 * This function must be called during running 175 * 176 * @param index The index of the output buffer. 177 * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise. 178 * @since 3.1 179 * @version 3.1 180 */ 181 virtual int32_t ReleaseOutputBuffer(uint32_t index) = 0; 182 183 /** 184 * @brief Sets the parameters to the encoder. 185 * 186 * This interface can only be called after the decoder is started. 187 * At the same time, incorrect parameter settings may cause decoding failure. 188 * 189 * @param format The parameters. 190 * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise. 191 * @since 3.1 192 * @version 3.1 193 */ 194 virtual int32_t SetParameter(const Format &format) = 0; 195 196 /** 197 * @brief Registers a encoder listener. 198 * 199 * This function must be called before {@link Configure} 200 * 201 * @param callback Indicates the encoder listener to register. For details, see {@link AVCodecCallback}. 202 * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise. 203 * @since 3.1 204 * @version 3.1 205 */ 206 virtual int32_t SetCallback(const std::shared_ptr<AVCodecCallback> &callback) = 0; 207 208 /** 209 * @brief Registers a encoder listener. 210 * 211 * This function must be called before {@link Configure} 212 * 213 * @param callback Indicates the decoder listener to register. For details, see {@link MediaCodecCallback}. 214 * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise. 215 * @since 4.1 216 */ 217 virtual int32_t SetCallback(const std::shared_ptr<MediaCodecCallback> &callback) = 0; 218 219 /** 220 * @brief Registers a encoder listener. 221 * 222 * This function must be called before {@link Configure} 223 * 224 * @param callback Indicates the decoder listener to register. For details, see {@link MediaCodecParameterCallback}. 225 * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise. 226 * @since 5.0 227 */ 228 virtual int32_t SetCallback(const std::shared_ptr<MediaCodecParameterCallback> &callback) = 0; 229 230 /** 231 * @brief Registers a encoder listener. 232 * 233 * This function must be called before {@link Configure} 234 * 235 * @param callback Indicates the decoder listener to register. For details, see {@link 236 * MediaCodecParameterWithAttrCallback}. 237 * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise. 238 * @since 5.0 239 */ 240 virtual int32_t SetCallback(const std::shared_ptr<MediaCodecParameterWithAttrCallback> &callback) = 0; 241 242 /** 243 * @brief Gets the format of the input data that accepted by the video encoder. 244 * 245 * This function must be called after {@link Configure} 246 * 247 * @param format 248 * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise. 249 * @since 4.0 250 * @version 4.0 251 */ 252 virtual int32_t GetInputFormat(Format &format) = 0; 253 254 /** 255 * @brief Set custom buffer. If this interface is used, it must be invoked after {@link Configure} 256 * and before {@link Start}. 257 * 258 * @param buffer The buffer of the custom input image data, such as a watermark image. 259 * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise. 260 * @since 5.0 261 */ 262 virtual int32_t SetCustomBuffer(std::shared_ptr<AVBuffer> buffer) = 0; 263 }; 264 265 class __attribute__((visibility("default"))) VideoEncoderFactory { 266 public: 267 #ifdef UNSUPPORT_CODEC CreateByMime(const std::string & mime)268 static std::shared_ptr<AVCodecVideoEncoder> CreateByMime(const std::string &mime) 269 { 270 (void)mime; 271 return nullptr; 272 } 273 CreateByName(const std::string & name)274 static std::shared_ptr<AVCodecVideoEncoder> CreateByName(const std::string &name) 275 { 276 (void)name; 277 return nullptr; 278 } 279 CreateByMime(const std::string & mime,Format & format,std::shared_ptr<AVCodecVideoEncoder> & encodec)280 static int32_t CreateByMime(const std::string &mime, Format &format, std::shared_ptr<AVCodecVideoEncoder> &encodec) 281 { 282 (void)name; 283 (void)format; 284 codec = nullptr; 285 return codec; 286 } 287 CreateByName(const std::string & name,Format & format,std::shared_ptr<AVCodecVideoEncoder> & encodec)288 static int32_t CreateByName(const std::string &name, Format &format, std::shared_ptr<AVCodecVideoEncoder> &encodec) 289 { 290 (void)name; 291 (void)format; 292 codec = nullptr; 293 return codec; 294 } 295 #else 296 /** 297 * @brief Instantiate the preferred encoder of the given mime type. 298 * 299 * @param mime The mime type. 300 * @return Returns the preferred encoder. 301 * @since 3.1 302 * @version 3.1 303 */ 304 static std::shared_ptr<AVCodecVideoEncoder> CreateByMime(const std::string &mime); 305 306 /** 307 * @brief Instantiates the designated encoder. 308 * 309 * @param name The encoder's name. 310 * @return Returns the designated encoder. 311 * @since 3.1 312 * @version 3.1 313 */ 314 static std::shared_ptr<AVCodecVideoEncoder> CreateByName(const std::string &name); 315 316 /** 317 * @brief Instantiate the preferred decoder of the given mime type. 318 * 319 * @param mime The mime type. 320 * @param format Caller info 321 * @param codec The designated decoder. 322 * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise. 323 * @since 5.0 324 * @version 5.0 325 */ 326 static int32_t CreateByMime(const std::string &mime, Format &format, std::shared_ptr<AVCodecVideoEncoder> &encodec); 327 328 /** 329 * @brief Instantiate the preferred decoder of the given mime type. 330 * 331 * @param mime The mime type. 332 * @param format Caller info 333 * @param codec The designated decoder. 334 * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise. 335 * @since 5.0 336 * @version 5.0 337 */ 338 static int32_t CreateByName(const std::string &name, Format &format, std::shared_ptr<AVCodecVideoEncoder> &encodec); 339 #endif 340 private: 341 VideoEncoderFactory() = default; 342 ~VideoEncoderFactory() = default; 343 }; 344 } // namespace MediaAVCodec 345 } // namespace OHOS 346 #endif // MEDIA_AVCODEC_VIDEO_ENCODER_H