• 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_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.
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
116      *
117      * @param index The index of the input buffer.
118      * @param info The info of the input buffer. For details, see {@link AVCodecBufferInfo}
119      * @param flag The flag of the input buffer. For details, see {@link AVCodecBufferFlag}
120      * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise.
121      * @since 3.1
122      * @version 3.1
123      */
124     virtual int32_t QueueInputBuffer(uint32_t index, AVCodecBufferInfo info, AVCodecBufferFlag flag) = 0;
125 
126     /**
127      * @brief Submits input buffer to decoder.
128      *
129      * This function must be called during running
130      *
131      * @param index The index of the input buffer.
132      * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise.
133      * @since 4.1
134      */
135     virtual int32_t QueueInputBuffer(uint32_t index) = 0;
136 
137     /**
138      * @brief Gets the format of the output data.
139      *
140      * This function must be called after {@link Configure}
141      *
142      * @param format
143      * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise.
144      * @since 3.1
145      * @version 3.1
146      */
147     virtual int32_t GetOutputFormat(Format &format) = 0;
148 
149     /**
150      * @brief Returns the output buffer to the decoder.
151      *
152      * This function must be called during running
153      *
154      * @param index The index of the output buffer.
155      * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise.
156      * @since 3.1
157      * @version 3.1
158      */
159     virtual int32_t ReleaseOutputBuffer(uint32_t index, bool render) = 0;
160 
161     /**
162      * @brief Sets the parameters to the decoder.
163      *
164      * This function must be called after {@link Configure}
165      *
166      * @param format The parameters.
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 SetParameter(const Format &format) = 0;
172 
173     /**
174      * @brief Registers a decoder listener.
175      *
176      * This function must be called before {@link Configure}
177      *
178      * @param callback Indicates the decoder listener to register. For details, see {@link AVCodecCallback}.
179      * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise.
180      * @since 3.1
181      * @version 3.1
182      */
183     virtual int32_t SetCallback(const std::shared_ptr<AVCodecCallback> &callback) = 0;
184 
185     /**
186      * @brief Registers a decoder listener.
187      *
188      * This function must be called before {@link Configure}
189      *
190      * @param callback Indicates the decoder listener to register. For details, see {@link MediaCodecCallback}.
191      * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise.
192      * @since 4.1
193      */
194     virtual int32_t SetCallback(const std::shared_ptr<MediaCodecCallback> &callback) = 0;
195 
196     /*
197      * @brief Set media key session which includes a decrypt module and a svp flag for decrypt video.
198      * @param svp is the flag whether use secure decoder
199      * @return Returns AV_ERR_OK if the execution is successful,
200      * otherwise returns a specific error code, refer to {@link OH_AVErrCode}
201      * @since
202      */
SetDecryptConfig(const sptr<DrmStandard::IMediaKeySessionService> & keySession,const bool svpFlag)203     virtual int32_t SetDecryptConfig(const sptr<DrmStandard::IMediaKeySessionService> &keySession,
204         const bool svpFlag)
205     {
206         (void)keySession;
207         (void)svpFlag;
208         return 0;
209     }
210 };
211 
212 class __attribute__((visibility("default"))) VideoDecoderFactory {
213 public:
214 #ifdef UNSUPPORT_CODEC
CreateByMime(const std::string & mime)215     static std::shared_ptr<AVCodecVideoDecoder> CreateByMime(const std::string &mime)
216     {
217         (void)mime;
218         return nullptr;
219     }
220 
CreateByName(const std::string & name)221     static std::shared_ptr<AVCodecVideoDecoder> CreateByName(const std::string &name)
222     {
223         (void)name;
224         return nullptr;
225     }
226 #else
227     /**
228      * @brief Instantiate the preferred decoder of the given mime type.
229      *
230      * @param mime The mime type.
231      * @return Returns the preferred decoder.
232      * @since 3.1
233      * @version 3.1
234      */
235     static std::shared_ptr<AVCodecVideoDecoder> CreateByMime(const std::string &mime);
236 
237     /**
238      * @brief Instantiates the designated decoder.
239      *
240      * @param name The decoder's name.
241      * @return Returns the designated decoder.
242      * @since 3.1
243      * @version 3.1
244      */
245     static std::shared_ptr<AVCodecVideoDecoder> CreateByName(const std::string &name);
246 #endif
247 private:
248     VideoDecoderFactory() = default;
249     ~VideoDecoderFactory() = default;
250 };
251 } // namespace MediaAVCodec
252 } // namespace OHOS
253 #endif // MEDIA_AVCODEC_VIDEO_DECODER_H