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_ENCODER_H 17 #define 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 Media { 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 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 encoder. 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 Notify eos of the encoder. 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 NotifyEos() = 0; 89 90 /** 91 * @brief Restores the encoder to the initial state. 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 Reset() = 0; 98 99 /** 100 * @brief Releases encoder resources. All methods are unavailable after calling this. 101 * 102 * @return Returns {@link MSERR_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 Returns a {@link AVSharedMemory} object for a input buffer index that contains the data. 121 * 122 * This function must be called during running 123 * 124 * @param index The index of the input buffer. 125 * @return Returns {@link AVSharedMemory} if success; returns nullptr otherwise. 126 * @since 3.1 127 * @version 3.1 128 */ 129 virtual std::shared_ptr<AVSharedMemory> GetInputBuffer(uint32_t index) = 0; 130 131 /** 132 * @brief Submits input buffer to encoder. 133 * 134 * This function must be called during running 135 * 136 * @param index The index of the input buffer. 137 * @param info The info of the input buffer. For details, see {@link AVCodecBufferInfo} 138 * @param flag The flag of the input buffer. For details, see {@link AVCodecBufferFlag} 139 * @return Returns {@link MSERR_OK} if success; returns an error code otherwise. 140 * @since 3.1 141 * @version 3.1 142 */ 143 virtual int32_t QueueInputBuffer(uint32_t index, AVCodecBufferInfo info, AVCodecBufferFlag flag) = 0; 144 145 /** 146 * @brief Returns a {@link AVSharedMemory} object for a output buffer index that contains the data. 147 * 148 * This function must be called during running 149 * 150 * @param index The index of the output buffer. 151 * @return Returns {@link AVSharedMemory} if success; returns nullptr otherwise. 152 * @since 3.1 153 * @version 3.1 154 */ 155 virtual std::shared_ptr<AVSharedMemory> GetOutputBuffer(uint32_t index) = 0; 156 157 /** 158 * @brief Gets the format of the output data. 159 * 160 * This function must be called after {@link Configure} 161 * 162 * @param format 163 * @return Returns {@link MSERR_OK} if success; returns an error code otherwise. 164 * @since 3.1 165 * @version 3.1 166 */ 167 virtual int32_t GetOutputFormat(Format &format) = 0; 168 169 /** 170 * @brief Returns the output buffer to the encoder. 171 * 172 * This function must be called during running 173 * 174 * @param index The index of the output buffer. 175 * @return Returns {@link MSERR_OK} if success; returns an error code otherwise. 176 * @since 3.1 177 * @version 3.1 178 */ 179 virtual int32_t ReleaseOutputBuffer(uint32_t index) = 0; 180 181 /** 182 * @brief Sets the parameters to the encoder. 183 * 184 * This function must be called after {@link Configure} 185 * 186 * @param format The parameters. 187 * @return Returns {@link MSERR_OK} if success; returns an error code otherwise. 188 * @since 3.1 189 * @version 3.1 190 */ 191 virtual int32_t SetParameter(const Format &format) = 0; 192 193 /** 194 * @brief Registers a encoder listener. 195 * 196 * This function must be called before {@link Configure} 197 * 198 * @param callback Indicates the encoder listener to register. For details, see {@link AVCodecCallback}. 199 * @return Returns {@link MSERR_OK} if success; returns an error code otherwise. 200 * @since 3.1 201 * @version 3.1 202 */ 203 virtual int32_t SetCallback(const std::shared_ptr<AVCodecCallback> &callback) = 0; 204 }; 205 206 class __attribute__((visibility("default"))) VideoEncoderFactory { 207 public: 208 #ifdef UNSUPPORT_CODEC CreateByMime(const std::string & mime)209 static std::shared_ptr<AVCodecVideoEncoder> CreateByMime(const std::string &mime) 210 { 211 (void)mime; 212 return nullptr; 213 } 214 CreateByName(const std::string & name)215 static std::shared_ptr<AVCodecVideoEncoder> CreateByName(const std::string &name) 216 { 217 (void)name; 218 return nullptr; 219 } 220 #else 221 /** 222 * @brief Instantiate the preferred encoder of the given mime type. 223 * 224 * @param mime The mime type. 225 * @return Returns the preferred encoder. 226 * @since 3.1 227 * @version 3.1 228 */ 229 static std::shared_ptr<AVCodecVideoEncoder> CreateByMime(const std::string &mime); 230 231 /** 232 * @brief Instantiates the designated encoder. 233 * 234 * @param name The encoder's name. 235 * @return Returns the designated encoder. 236 * @since 3.1 237 * @version 3.1 238 */ 239 static std::shared_ptr<AVCodecVideoEncoder> CreateByName(const std::string &name); 240 #endif 241 private: 242 VideoEncoderFactory() = default; 243 ~VideoEncoderFactory() = default; 244 }; 245 } // namespace Media 246 } // namespace OHOS 247 #endif // AVCODEC_VIDEO_ENCODER_H