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_DECODER_H
17 #define MEDIA_AVCODEC_VIDEO_DECODER_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 #include "foundation/multimedia/drm_framework/services/drm_service/ipc/i_keysession_service.h"
25 
26 namespace OHOS {
27 namespace MediaAVCodec {
28 class AVCodecVideoDecoder {
29 public:
30     virtual ~AVCodecVideoDecoder() = default;
31 
32     /**
33      * @brief Configure the decoder. This interface must be called before {@link Prepare} is called.
34      *
35      * @param format The format of the input data and the desired format of the output data.
36      * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise.
37      * @since 3.1
38      * @version 3.1
39      */
40     virtual int32_t Configure(const Format &format) = 0;
41 
42     /**
43      * @brief Prepare for decoding.
44      *
45      * This function must be called after {@link Configure} and before {@link Start}
46      *
47      * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise.
48      * @since 3.1
49      * @version 3.1
50      */
51     virtual int32_t Prepare() = 0;
52 
53     /**
54      * @brief Start decoding.
55      *
56      * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise.
57      * @since 3.1
58      * @version 3.1
59      */
60     virtual int32_t Start() = 0;
61 
62     /**
63      * @brief Stop decoding.
64      *
65      * This function must be called during running
66      *
67      * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise.
68      * @since 3.1
69      * @version 3.1
70      */
71     virtual int32_t Stop() = 0;
72 
73     /**
74      * @brief Flush both input and output buffers of the decoder.
75      *
76      * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise.
77      * @since 3.1
78      * @version 3.1
79      */
80     virtual int32_t Flush() = 0;
81 
82     /**
83      * @brief Restores the decoder to the initial state.
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 Reset() = 0;
90 
91     /**
92      * @brief Releases decoder resources. All methods are unavailable after calling this.
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 Release() = 0;
99 
100     /**
101      * @brief Sets the surface on which to render the output of this decoder.
102      *
103      * This function must be called before {@link Prepare}
104      *
105      * @param index The index of the output buffer.
106      * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise.
107      * @since 3.1
108      * @version 3.1
109      */
110     virtual int32_t SetOutputSurface(sptr<Surface> surface) = 0;
111 
112     /**
113      * @brief Submits input buffer to decoder.
114      *
115      * This function must be called during running. The {@link AVCodecCallback} callback
116      * will report the available input buffer and the corresponding index value. Once the buffer with the specified
117      * index is submitted to the video decoder, the buffer cannot be accessed again until the {@link
118      * AVCodecCallback} callback is received again reporting that the buffer with the same index is available.
119      * In addition, for some decoders, it is required to input Codec-Specific-Data to the decoder at the beginning to
120      * initialize the decoding process of the decoder, such as PPS/SPS data in H264 format.
121      *
122      * @param index The index of the input buffer.
123      * @param info The info of the input buffer. For details, see {@link AVCodecBufferInfo}
124      * @param flag The flag of the input buffer. For details, see {@link AVCodecBufferFlag}
125      * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise.
126      * @since 3.1
127      * @version 3.1
128      */
129     virtual int32_t QueueInputBuffer(uint32_t index, AVCodecBufferInfo info, AVCodecBufferFlag flag) = 0;
130 
131     /**
132      * @brief Submits input buffer to decoder.
133      *
134      * This function must be called during running. The {@link MediaCodecCallback} callback
135      * will report the available input buffer and the corresponding index value. Once the buffer with the specified
136      * index is submitted to the video decoder, the buffer cannot be accessed again until the {@link
137      * MediaCodecCallback} callback is received again reporting that the buffer with the same index is available.
138      * In addition, for some decoders, it is required to input Codec-Specific-Data to the decoder at the beginning to
139      * initialize the decoding process of the decoder, such as PPS/SPS data in H264 format.
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 Gets the format of the output data.
149      *
150      * This function must be called after {@link Configure}
151      *
152      * @param format
153      * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise.
154      * @since 3.1
155      * @version 3.1
156      */
157     virtual int32_t GetOutputFormat(Format &format) = 0;
158 
159     /**
160      * @brief Returns the output buffer to the decoder.
161      *
162      * This function must be called during running, and notify the decoder to finish rendering the
163      * decoded data contained in the Buffer on the output Surface. If the output surface is not configured before,
164      * calling this interface only returns the output buffer corresponding to the specified index to the decoder.
165      *
166      * @param index The index of the output buffer.
167      * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise.
168      * @since 3.1
169      * @version 3.1
170      */
171     virtual int32_t ReleaseOutputBuffer(uint32_t index, bool render) = 0;
172 
173     /**
174      * @brief Return the processed output buffer with render timestamp to the decoder, and notify the decoder to finish
175      * rendering the decoded data contained in the buffer on the output surface. If the output surface is not
176      * configured before, calling this interface only returns the output buffer corresponding to the specified index to
177      * the decoder. The timestamp may have special meaning depending on the destination surface.
178      *
179      * This function must be called during running
180      *
181      * @param index The index of the output buffer.
182      * @param renderTimestampNs The timestamp is associated with the output buffer when it is sent to the surface. The
183      * unit is nanosecond.
184      * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise.
185      * @since 5.0
186      */
187     virtual int32_t RenderOutputBufferAtTime(uint32_t index, int64_t renderTimestampNs) = 0;
188 
189     /**
190      * @brief Sets the parameters to the decoder.
191      *
192      * This interface can only be called after the decoder is started.
193      * At the same time, incorrect parameter settings may cause decoding failure.
194      *
195      * @param format The parameters.
196      * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise.
197      * @since 3.1
198      * @version 3.1
199      */
200     virtual int32_t SetParameter(const Format &format) = 0;
201 
202     /**
203      * @brief Registers a decoder listener.
204      *
205      * This function must be called before {@link Configure}
206      *
207      * @param callback Indicates the decoder listener to register. For details, see {@link AVCodecCallback}.
208      * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise.
209      * @since 3.1
210      * @version 3.1
211      */
212     virtual int32_t SetCallback(const std::shared_ptr<AVCodecCallback> &callback) = 0;
213 
214     /**
215      * @brief Registers a decoder listener.
216      *
217      * This function must be called before {@link Configure}
218      *
219      * @param callback Indicates the decoder listener to register. For details, see {@link MediaCodecCallback}.
220      * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise.
221      * @since 4.1
222      */
223     virtual int32_t SetCallback(const std::shared_ptr<MediaCodecCallback> &callback) = 0;
224 
225     /*
226      * @brief Set media key session which includes a decrypt module and a svp flag for decrypt video.
227      * @param svp is the flag whether use secure decoder
228      * @return Returns AV_ERR_OK if the execution is successful,
229      * otherwise returns a specific error code, refer to {@link OH_AVErrCode}
230      * @since
231      */
SetDecryptConfig(const sptr<DrmStandard::IMediaKeySessionService> & keySession,const bool svpFlag)232     virtual int32_t SetDecryptConfig(const sptr<DrmStandard::IMediaKeySessionService> &keySession,
233         const bool svpFlag)
234     {
235         (void)keySession;
236         (void)svpFlag;
237         return 0;
238     }
239 };
240 
241 class __attribute__((visibility("default"))) VideoDecoderFactory {
242 public:
243 #ifdef UNSUPPORT_CODEC
CreateByMime(const std::string & mime)244     static std::shared_ptr<AVCodecVideoDecoder> CreateByMime(const std::string &mime)
245     {
246         (void)mime;
247         return nullptr;
248     }
249 
CreateByName(const std::string & name)250     static std::shared_ptr<AVCodecVideoDecoder> CreateByName(const std::string &name)
251     {
252         (void)name;
253         return nullptr;
254     }
255 
CreateByMime(const std::string & mime,Format & format,std::shared_ptr<AVCodecVideoDecoder> & decoder)256     static int32_t CreateByMime(const std::string &mime, Format &format, std::shared_ptr<AVCodecVideoDecoder> &decoder)
257     {
258         (void)name;
259         (void)format;
260         codec = nullptr;
261         return codec;
262     }
263 
CreateByName(const std::string & name,Format & format,std::shared_ptr<AVCodecVideoDecoder> & decoder)264     static int32_t CreateByName(const std::string &name, Format &format, std::shared_ptr<AVCodecVideoDecoder> &decoder)
265     {
266         (void)name;
267         (void)format;
268         codec = nullptr;
269         return codec;
270     }
271 #else
272     /**
273      * @brief Instantiate the preferred decoder of the given mime type.
274      *
275      * @param mime The mime type.
276      * @return Returns the preferred decoder.
277      * @since 3.1
278      * @version 3.1
279      */
280     static std::shared_ptr<AVCodecVideoDecoder> CreateByMime(const std::string &mime);
281 
282     /**
283      * @brief Instantiates the designated decoder.
284      *
285      * @param name The decoder's name.
286      * @return Returns the designated decoder.
287      * @since 3.1
288      * @version 3.1
289      */
290     static std::shared_ptr<AVCodecVideoDecoder> CreateByName(const std::string &name);
291 
292     /**
293      * @brief Instantiate the preferred decoder of the given mime type.
294      *
295      * @param mime The mime type.
296      * @param format Caller info
297      * @param codec The designated decoder.
298      * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise.
299      * @since 5.0
300      * @version 5.0
301      */
302     static int32_t CreateByMime(const std::string &mime, Format &format, std::shared_ptr<AVCodecVideoDecoder> &decoder);
303 
304     /**
305      * @brief Instantiate the preferred decoder of the given mime type.
306      *
307      * @param mime The mime type.
308      * @param format Caller info
309      * @param codec The designated decoder.
310      * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise.
311      * @since 5.0
312      * @version 5.0
313      */
314     static int32_t CreateByName(const std::string &name, Format &format, std::shared_ptr<AVCodecVideoDecoder> &decoder);
315 #endif
316 private:
317     VideoDecoderFactory() = default;
318     ~VideoDecoderFactory() = default;
319 };
320 } // namespace MediaAVCodec
321 } // namespace OHOS
322 #endif // MEDIA_AVCODEC_VIDEO_DECODER_H