• 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.
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 Submits input buffer to encoder.
135      *
136      * This function must be called during running
137      *
138      * @param index The index of the input buffer.
139      * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise.
140      * @since 4.1
141      */
142     virtual int32_t QueueInputBuffer(uint32_t index) = 0;
143 
144     /**
145      * @brief Gets the format of the output data.
146      *
147      * This function must be called after {@link Configure}
148      *
149      * @param format
150      * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise.
151      * @since 3.1
152      * @version 3.1
153      */
154     virtual int32_t GetOutputFormat(Format &format) = 0;
155 
156     /**
157      * @brief Returns the output buffer to the encoder.
158      *
159      * This function must be called during running
160      *
161      * @param index The index of the output buffer.
162      * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise.
163      * @since 3.1
164      * @version 3.1
165      */
166     virtual int32_t ReleaseOutputBuffer(uint32_t index) = 0;
167 
168     /**
169      * @brief Sets the parameters to the encoder.
170      *
171      * This function must be called after {@link Configure}
172      *
173      * @param format The parameters.
174      * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise.
175      * @since 3.1
176      * @version 3.1
177      */
178     virtual int32_t SetParameter(const Format &format) = 0;
179 
180     /**
181      * @brief Registers a encoder listener.
182      *
183      * This function must be called before {@link Configure}
184      *
185      * @param callback Indicates the encoder listener to register. For details, see {@link AVCodecCallback}.
186      * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise.
187      * @since 3.1
188      * @version 3.1
189      */
190     virtual int32_t SetCallback(const std::shared_ptr<AVCodecCallback> &callback) = 0;
191 
192     /**
193      * @brief Registers a encoder listener.
194      *
195      * This function must be called before {@link Configure}
196      *
197      * @param callback Indicates the decoder listener to register. For details, see {@link MediaCodecCallback}.
198      * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise.
199      * @since 4.1
200      */
201     virtual int32_t SetCallback(const std::shared_ptr<MediaCodecCallback> &callback) = 0;
202 
203     /**
204      * @brief Gets the format of the input data that accepted by the video encoder.
205      *
206      * This function must be called after {@link Configure}
207      *
208      * @param format
209      * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise.
210      * @since 4.0
211      * @version 4.0
212      */
213     virtual int32_t GetInputFormat(Format &format) = 0;
214 };
215 
216 class __attribute__((visibility("default"))) VideoEncoderFactory {
217 public:
218 #ifdef UNSUPPORT_CODEC
CreateByMime(const std::string & mime)219     static std::shared_ptr<AVCodecVideoEncoder> CreateByMime(const std::string &mime)
220     {
221         (void)mime;
222         return nullptr;
223     }
224 
CreateByName(const std::string & name)225     static std::shared_ptr<AVCodecVideoEncoder> CreateByName(const std::string &name)
226     {
227         (void)name;
228         return nullptr;
229     }
230 #else
231     /**
232      * @brief Instantiate the preferred encoder of the given mime type.
233      *
234      * @param mime The mime type.
235      * @return Returns the preferred encoder.
236      * @since 3.1
237      * @version 3.1
238      */
239     static std::shared_ptr<AVCodecVideoEncoder> CreateByMime(const std::string &mime);
240 
241     /**
242      * @brief Instantiates the designated encoder.
243      *
244      * @param name The encoder's name.
245      * @return Returns the designated encoder.
246      * @since 3.1
247      * @version 3.1
248      */
249     static std::shared_ptr<AVCodecVideoEncoder> CreateByName(const std::string &name);
250 #endif
251 private:
252     VideoEncoderFactory() = default;
253     ~VideoEncoderFactory() = default;
254 };
255 } // namespace MediaAVCodec
256 } // namespace OHOS
257 #endif // MEDIA_AVCODEC_VIDEO_ENCODER_H