• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 encoder 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 encoder 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 encoder 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 encoder 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      * @brief Query available input buffer from encoder
266      *
267      * This function blocks until an input buffer becomes available or timeout occurs.
268      *
269      * @param index [out] Reference to store the index of available input buffer
270      * @param timeoutUs Timeout duration in microseconds (negative value means infinite wait)
271      * @return Returns {@link AVCS_ERR_OK} if buffer is available;
272      *         returns error code if timeout or other failures occur.
273      * @since 6.0
274      * @version 6.0
275      */
QueryInputBuffer(uint32_t & index,int64_t timeoutUs)276     virtual int32_t QueryInputBuffer(uint32_t &index, int64_t timeoutUs)
277     {
278         (void)index;
279         (void)timeoutUs;
280         return 0;
281     }
282 
283     /**
284      * @brief Query available output buffer from encoder
285      *
286      * This function blocks until an output buffer with decoded data becomes available or timeout occurs.
287      *
288      * @param index [out] Reference to store the index of available output buffer
289      * @param timeoutUs Timeout duration in microseconds (negative value means infinite wait)
290      * @return Returns {@link AVCS_ERR_OK} if buffer is available;
291      *         returns error code if timeout or other failures occur.
292      * @since 6.0
293      * @version 6.0
294      */
QueryOutputBuffer(uint32_t & index,int64_t timeoutUs)295     virtual int32_t QueryOutputBuffer(uint32_t &index, int64_t timeoutUs)
296     {
297         (void)index;
298         (void)timeoutUs;
299         return 0;
300     }
301 
302     /**
303      * @brief Get input buffer object by index
304      *
305      * Caller should use {@link QueryInputBuffer} to get valid index before calling this function.
306      *
307      * @param index Index of the input buffer obtained from {@link QueryInputBuffer}
308      * @return Shared pointer to {@link AVBuffer} if index is valid;
309      *         returns nullptr if index is invalid or buffer unavailable.
310      * @since 6.0
311      * @version 6.0
312      */
GetInputBuffer(uint32_t index)313     virtual std::shared_ptr<AVBuffer> GetInputBuffer(uint32_t index)
314     {
315         (void)index;
316         return nullptr;
317     }
318 
319     /**
320      * @brief Get output buffer object by index
321      *
322      * Caller should use {@link QueryOutputBuffer} to get valid index before calling this function.
323      *
324      * @param index Index of the output buffer obtained from {@link QueryOutputBuffer}
325      * @return Shared pointer to {@link AVBuffer} containing decoded data if index is valid;
326      *         returns nullptr if index is invalid or buffer unavailable.
327      * @since 6.0
328      * @version 6.0
329      */
GetOutputBuffer(uint32_t index)330     virtual std::shared_ptr<AVBuffer> GetOutputBuffer(uint32_t index)
331     {
332         (void)index;
333         return nullptr;
334     }
335 };
336 
337 class __attribute__((visibility("default"))) VideoEncoderFactory {
338 public:
339 #ifdef UNSUPPORT_CODEC
CreateByMime(const std::string & mime)340     static std::shared_ptr<AVCodecVideoEncoder> CreateByMime(const std::string &mime)
341     {
342         (void)mime;
343         return nullptr;
344     }
345 
CreateByName(const std::string & name)346     static std::shared_ptr<AVCodecVideoEncoder> CreateByName(const std::string &name)
347     {
348         (void)name;
349         return nullptr;
350     }
351 
CreateByMime(const std::string & mime,Format & format,std::shared_ptr<AVCodecVideoEncoder> & encodec)352     static int32_t CreateByMime(const std::string &mime, Format &format, std::shared_ptr<AVCodecVideoEncoder> &encodec)
353     {
354         (void)name;
355         (void)format;
356         codec = nullptr;
357         return codec;
358     }
359 
CreateByName(const std::string & name,Format & format,std::shared_ptr<AVCodecVideoEncoder> & encodec)360     static int32_t CreateByName(const std::string &name, Format &format, std::shared_ptr<AVCodecVideoEncoder> &encodec)
361     {
362         (void)name;
363         (void)format;
364         codec = nullptr;
365         return codec;
366     }
367 #else
368     /**
369      * @brief Instantiate the preferred encoder of the given mime type.
370      *
371      * @param mime The mime type.
372      * @return Returns the preferred encoder.
373      * @since 3.1
374      * @version 3.1
375      */
376     static std::shared_ptr<AVCodecVideoEncoder> CreateByMime(const std::string &mime);
377 
378     /**
379      * @brief Instantiates the designated encoder.
380      *
381      * @param name The encoder's name.
382      * @return Returns the designated encoder.
383      * @since 3.1
384      * @version 3.1
385      */
386     static std::shared_ptr<AVCodecVideoEncoder> CreateByName(const std::string &name);
387 
388     /**
389      * @brief Instantiate the preferred encoder of the given mime type.
390      *
391      * @param mime The mime type.
392      * @param format Caller info
393      * @param codec The designated encoder.
394      * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise.
395      * @since 5.0
396      * @version 5.0
397      */
398     static int32_t CreateByMime(const std::string &mime, Format &format, std::shared_ptr<AVCodecVideoEncoder> &encodec);
399 
400     /**
401      * @brief Instantiate the preferred encoder of the given mime type.
402      *
403      * @param mime The mime type.
404      * @param format Caller info
405      * @param codec The designated encoder.
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 CreateByName(const std::string &name, Format &format, std::shared_ptr<AVCodecVideoEncoder> &encodec);
411 #endif
412 private:
413     VideoEncoderFactory() = default;
414     ~VideoEncoderFactory() = default;
415 };
416 } // namespace MediaAVCodec
417 } // namespace OHOS
418 #endif // MEDIA_AVCODEC_VIDEO_ENCODER_H