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 AVCODEC_VIDEO_ENCODER_H 17 #define AVCODEC_VIDEO_ENCODER_H 18 19 #include "avcodec_common.h" 20 #include "avcodec_info.h" 21 #include "avsharedmemory.h" 22 #include "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. 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. 83 * 84 * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise. 85 * @since 3.1 86 * @version 3.1 87 */ 88 virtual int32_t NotifyEos() = 0; 89 90 /** 91 * @brief Restores the encoder to the initial state. 92 * 93 * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise. 94 * @since 3.1 95 * @version 3.1 96 */ 97 virtual int32_t Reset() = 0; 98 99 /** 100 * @brief Releases encoder resources. All methods are unavailable after calling this. 101 * 102 * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise. 103 * @since 3.1 104 * @version 3.1 105 */ 106 virtual int32_t Release() = 0; 107 108 /** 109 * @brief Obtains the surface from encoder. 110 * 111 * This function can only be called after {@link Configure} and before {@link Prepare} 112 * 113 * @return Returns the pointer to the surface. 114 * @since 3.1 115 * @version 3.1 116 */ 117 virtual sptr<Surface> CreateInputSurface() = 0; 118 119 /** 120 * @brief Submits input buffer to encoder. 121 * 122 * This function must be called during running 123 * 124 * @param index The index of the input buffer. 125 * @param info The info of the input buffer. For details, see {@link AVCodecBufferInfo} 126 * @param flag The flag of the input buffer. For details, see {@link AVCodecBufferFlag} 127 * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise. 128 * @since 3.1 129 * @version 3.1 130 */ 131 virtual int32_t QueueInputBuffer(uint32_t index, AVCodecBufferInfo info, AVCodecBufferFlag flag) = 0; 132 133 /** 134 * @brief Gets the format of the output data. 135 * 136 * This function must be called after {@link Configure} 137 * 138 * @param format 139 * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise. 140 * @since 3.1 141 * @version 3.1 142 */ 143 virtual int32_t GetOutputFormat(Format &format) = 0; 144 145 /** 146 * @brief Returns the output buffer to the encoder. 147 * 148 * This function must be called during running 149 * 150 * @param index The index of the output buffer. 151 * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise. 152 * @since 3.1 153 * @version 3.1 154 */ 155 virtual int32_t ReleaseOutputBuffer(uint32_t index) = 0; 156 157 /** 158 * @brief Sets the parameters to the encoder. 159 * 160 * This function must be called after {@link Configure} 161 * 162 * @param format The parameters. 163 * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise. 164 * @since 3.1 165 * @version 3.1 166 */ 167 virtual int32_t SetParameter(const Format &format) = 0; 168 169 /** 170 * @brief Registers a encoder listener. 171 * 172 * This function must be called before {@link Configure} 173 * 174 * @param callback Indicates the encoder listener to register. For details, see {@link AVCodecCallback}. 175 * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise. 176 * @since 3.1 177 * @version 3.1 178 */ 179 virtual int32_t SetCallback(const std::shared_ptr<AVCodecCallback> &callback) = 0; 180 181 /** 182 * @brief Gets the format of the input data that accepted by the video encoder. 183 * 184 * This function must be called after {@link Configure} 185 * 186 * @param format 187 * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise. 188 * @since 4.0 189 * @version 4.0 190 */ 191 virtual int32_t GetInputFormat(Format &format) = 0; 192 }; 193 194 class __attribute__((visibility("default"))) VideoEncoderFactory { 195 public: 196 #ifdef UNSUPPORT_CODEC CreateByMime(const std::string & mime)197 static std::shared_ptr<AVCodecVideoEncoder> CreateByMime(const std::string &mime) 198 { 199 (void)mime; 200 return nullptr; 201 } 202 CreateByName(const std::string & name)203 static std::shared_ptr<AVCodecVideoEncoder> CreateByName(const std::string &name) 204 { 205 (void)name; 206 return nullptr; 207 } 208 #else 209 /** 210 * @brief Instantiate the preferred encoder of the given mime type. 211 * 212 * @param mime The mime type. 213 * @return Returns the preferred encoder. 214 * @since 3.1 215 * @version 3.1 216 */ 217 static std::shared_ptr<AVCodecVideoEncoder> CreateByMime(const std::string &mime); 218 219 /** 220 * @brief Instantiates the designated encoder. 221 * 222 * @param name The encoder's name. 223 * @return Returns the designated encoder. 224 * @since 3.1 225 * @version 3.1 226 */ 227 static std::shared_ptr<AVCodecVideoEncoder> CreateByName(const std::string &name); 228 #endif 229 private: 230 VideoEncoderFactory() = default; 231 ~VideoEncoderFactory() = default; 232 }; 233 } // namespace MediaAVCodec 234 } // namespace OHOS 235 #endif // AVCODEC_VIDEO_ENCODER_H