• 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 AVCODEC_VIDEO_DECODER_H
17 #define AVCODEC_VIDEO_DECODER_H
18 
19 #include "avcodec_common.h"
20 #include "avcodec_info.h"
21 #include "avsharedmemory.h"
22 #include "format.h"
23 #include "surface.h"
24 
25 namespace OHOS {
26 namespace MediaAVCodec {
27 class AVCodecVideoDecoder {
28 public:
29     virtual ~AVCodecVideoDecoder() = default;
30 
31     /**
32      * @brief Configure the decoder.
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 decoder.
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 Restores the decoder to the initial state.
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 Reset() = 0;
89 
90     /**
91      * @brief Releases decoder resources. All methods are unavailable after calling this.
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 Release() = 0;
98 
99     /**
100      * @brief Sets the surface on which to render the output of this decoder.
101      *
102      * This function must be called before {@link Prepare}
103      *
104      * @param index The index of the output buffer.
105      * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise.
106      * @since 3.1
107      * @version 3.1
108      */
109     virtual int32_t SetOutputSurface(sptr<Surface> surface) = 0;
110 
111     /**
112      * @brief Submits input buffer to decoder.
113      *
114      * This function must be called during running
115      *
116      * @param index The index of the input buffer.
117      * @param info The info of the input buffer. For details, see {@link AVCodecBufferInfo}
118      * @param flag The flag of the input buffer. For details, see {@link AVCodecBufferFlag}
119      * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise.
120      * @since 3.1
121      * @version 3.1
122      */
123     virtual int32_t QueueInputBuffer(uint32_t index, AVCodecBufferInfo info, AVCodecBufferFlag flag) = 0;
124 
125     /**
126      * @brief Gets the format of the output data.
127      *
128      * This function must be called after {@link Configure}
129      *
130      * @param format
131      * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise.
132      * @since 3.1
133      * @version 3.1
134      */
135     virtual int32_t GetOutputFormat(Format &format) = 0;
136 
137     /**
138      * @brief Returns the output buffer to the decoder.
139      *
140      * This function must be called during running
141      *
142      * @param index The index of the output buffer.
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 ReleaseOutputBuffer(uint32_t index, bool render) = 0;
148 
149     /**
150      * @brief Sets the parameters to the decoder.
151      *
152      * This function must be called after {@link Configure}
153      *
154      * @param format The parameters.
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 SetParameter(const Format &format) = 0;
160 
161     /**
162      * @brief Registers a decoder listener.
163      *
164      * This function must be called before {@link Configure}
165      *
166      * @param callback Indicates the decoder listener to register. For details, see {@link AVCodecCallback}.
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 SetCallback(const std::shared_ptr<AVCodecCallback> &callback) = 0;
172 };
173 
174 class __attribute__((visibility("default"))) VideoDecoderFactory {
175 public:
176 #ifdef UNSUPPORT_CODEC
CreateByMime(const std::string & mime)177     static std::shared_ptr<AVCodecVideoDecoder> CreateByMime(const std::string &mime)
178     {
179         (void)mime;
180         return nullptr;
181     }
182 
CreateByName(const std::string & name)183     static std::shared_ptr<AVCodecVideoDecoder> CreateByName(const std::string &name)
184     {
185         (void)name;
186         return nullptr;
187     }
188 #else
189     /**
190      * @brief Instantiate the preferred decoder of the given mime type.
191      *
192      * @param mime The mime type.
193      * @return Returns the preferred decoder.
194      * @since 3.1
195      * @version 3.1
196      */
197     static std::shared_ptr<AVCodecVideoDecoder> CreateByMime(const std::string &mime);
198 
199     /**
200      * @brief Instantiates the designated decoder.
201      *
202      * @param name The decoder's name.
203      * @return Returns the designated decoder.
204      * @since 3.1
205      * @version 3.1
206      */
207     static std::shared_ptr<AVCodecVideoDecoder> CreateByName(const std::string &name);
208 #endif
209 private:
210     VideoDecoderFactory() = default;
211     ~VideoDecoderFactory() = default;
212 };
213 } // namespace MediaAVCodec
214 } // namespace OHOS
215 #endif // AVCODEC_VIDEO_DECODER_H