• 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 #ifndef MEDIA_AVCODEC_COMMOM_H
16 #define MEDIA_AVCODEC_COMMOM_H
17 
18 #include <string>
19 #include <vector>
20 #include <map>
21 #include "av_common.h"
22 #include "buffer/avbuffer.h"
23 #include "meta/format.h"
24 
25 namespace OHOS {
26 namespace MediaAVCodec {
27 using AVBuffer = OHOS::Media::AVBuffer;
28 using AVSharedMemory = OHOS::Media::AVSharedMemory;
29 using Format = OHOS::Media::Format;
30 /**
31  * @brief Error type of AVCodec
32  *
33  * @since 3.1
34  * @version 3.1
35  */
36 enum AVCodecErrorType : int32_t {
37     /* internal errors, error code passed by the errorCode, and definition see "AVCodecServiceErrCode" */
38     AVCODEC_ERROR_INTERNAL,
39     /* extend error start. The extension error code agreed upon by the plug-in and
40        the application will be transparently transmitted by the service. */
41     AVCODEC_ERROR_DECRYTION_FAILED,
42     /* internal errors, the extension error codes within the framework. */
43     AVCODEC_ERROR_FRAMEAORK_FAILED,
44     AVCODEC_ERROR_EXTEND_START = 0X10000,
45 };
46 
47 enum class API_VERSION : int32_t {
48     API_VERSION_10 = 10,
49     API_VERSION_11 = 11
50 };
51 
52 /**
53  * @brief Flag of AVCodecBuffer.
54  *
55  * @since 3.1
56  */
57 enum AVCodecBufferFlag : uint32_t {
58     AVCODEC_BUFFER_FLAG_NONE = 0,
59     /** This signals the end of stream. */
60     AVCODEC_BUFFER_FLAG_EOS = 1 << 0,
61     /** This indicates that the buffer contains the data for a sync frame. */
62     AVCODEC_BUFFER_FLAG_SYNC_FRAME = 1 << 1,
63     /** This indicates that the buffer only contains part of a frame. */
64     AVCODEC_BUFFER_FLAG_PARTIAL_FRAME = 1 << 2,
65     /** This indicated that the buffer contains codec specific data. */
66     AVCODEC_BUFFER_FLAG_CODEC_DATA = 1 << 3,
67     /** Flag is used to discard packets which are required to maintain valid decoder state but are not required
68      * for output and should be dropped after decoding.
69      * @since 12
70      */
71     AVCODEC_BUFFER_FLAG_DISCARD = 1 << 4,
72     /** Flag is used to indicate packets that contain frames that can be discarded by the decoder,
73      * I.e. Non-reference frames.
74      * @since 12
75      */
76     AVCODEC_BUFFER_FLAG_DISPOSABLE = 1 << 5,
77     /** Indicates that the frame is an extended discardable frame. It is not on the main reference path and
78      * is referenced only by discardable frames or extended discardable frames. When subsequent frames on the branch
79      * reference path are discarded by decoder, the frame can be further discarded.
80      * @since 12
81      */
82     AVCODEC_BUFFER_FLAG_DISPOSABLE_EXT = 1 << 6,
83 
84     /** This indicated that the buffer contains mul frame for lpp */
85     AVCODEC_BUFFER_FLAG_MUL_FRAME = 1 << 7,
86 };
87 
88 struct AVCodecBufferInfo {
89     /* The presentation timestamp in microseconds for the buffer */
90     int64_t presentationTimeUs = 0;
91     /* The amount of data (in bytes) in the buffer */
92     int32_t size = 0;
93     /* The start-offset of the data in the buffer */
94     int32_t offset = 0;
95 };
96 
97 class AVCodecCallback {
98 public:
99     virtual ~AVCodecCallback() = default;
100     /**
101      * Called when an error occurred.
102      *
103      * @param errorType Error type. For details, see {@link AVCodecErrorType}.
104      * @param errorCode Error code.
105      * @since 3.1
106      * @version 3.1
107      */
108     virtual void OnError(AVCodecErrorType errorType, int32_t errorCode) = 0;
109 
110     /**
111      * Called when the output format has changed.
112      *
113      * @param format The new output format.
114      * @since 3.1
115      * @version 3.1
116      */
117     virtual void OnOutputFormatChanged(const Format &format) = 0;
118 
119     /**
120      * Called when an input buffer becomes available.
121      *
122      * @param index The index of the available input buffer.
123      * @param buffer A {@link AVSharedMemory} object for a input buffer index that contains the data.
124      * @since 3.1
125      * @version 4.0
126      */
127     virtual void OnInputBufferAvailable(uint32_t index, std::shared_ptr<AVSharedMemory> buffer) = 0;
128 
129     /**
130      * Called when an output buffer becomes available.
131      *
132      * @param index The index of the available output buffer.
133      * @param info The info of the available output buffer. For details, see {@link AVCodecBufferInfo}
134      * @param flag The flag of the available output buffer. For details, see {@link AVCodecBufferFlag}
135      * @param buffer A {@link AVSharedMemory} object for a output buffer index that contains the data.
136      * @since 3.1
137      * @version 4.0
138      */
139     virtual void OnOutputBufferAvailable(uint32_t index, AVCodecBufferInfo info, AVCodecBufferFlag flag,
140                                          std::shared_ptr<AVSharedMemory> buffer) = 0;
141 };
142 
143 class AVDemuxerCallback {
144 public:
145     virtual ~AVDemuxerCallback() = default;
146 
147     /**
148      * Called when an drm info updated.
149      *
150      * @param drmInfo Drm Info.
151      * @since 4.1
152      * @version 4.1
153      */
154     virtual void OnDrmInfoChanged(const std::multimap<std::string, std::vector<uint8_t>> &drmInfo) = 0;
155 };
156 
157 class MediaCodecCallback {
158 public:
159     virtual ~MediaCodecCallback() = default;
160     /**
161      * Called when an error occurred.
162      *
163      * @param errorType Error type. For details, see {@link AVCodecErrorType}.
164      * @param errorCode Error code.
165      * @since 4.1
166      */
167     virtual void OnError(AVCodecErrorType errorType, int32_t errorCode) = 0;
168 
169     /**
170      * Called when the output format has changed.
171      *
172      * @param format The new output format.
173      * @since 4.1
174      */
175     virtual void OnOutputFormatChanged(const Format &format) = 0;
176 
177     /**
178      * Called when an input buffer becomes available.
179      *
180      * @param index The index of the available input buffer.
181      * @param buffer A {@link AVBuffer} object for a input buffer index that contains the data.
182      * @since 4.1
183      */
184     virtual void OnInputBufferAvailable(uint32_t index, std::shared_ptr<AVBuffer> buffer) = 0;
185 
186     /**
187      * Called when an output buffer becomes available.
188      *
189      * @param index The index of the available output buffer.
190      * @param buffer A {@link AVBuffer} object for a output buffer index that contains the data.
191      * @since 4.1
192      */
193     virtual void OnOutputBufferAvailable(uint32_t index, std::shared_ptr<AVBuffer> buffer) = 0;
194 
OnOutputBufferBinded(std::map<uint32_t,sptr<SurfaceBuffer>> & bufferMap)195     virtual void OnOutputBufferBinded(std::map<uint32_t, sptr<SurfaceBuffer>> &bufferMap)
196     {
197         (void)bufferMap;
198     }
199 
OnOutputBufferUnbinded()200     virtual void OnOutputBufferUnbinded()
201     {
202     }
203 };
204 
205 class MediaCodecParameterCallback {
206 public:
207     virtual ~MediaCodecParameterCallback() = default;
208     /**
209      * Called when an input parameter becomes available.
210      *
211      * @param index The index of the available input parameter.
212      * @param parameter A {@link Format} object containing the corresponding index input parameter.
213      * @since 5.0
214      */
215     virtual void OnInputParameterAvailable(uint32_t index, std::shared_ptr<Format> parameter) = 0;
216 };
217 
218 class MediaCodecParameterWithAttrCallback {
219 public:
220     virtual ~MediaCodecParameterWithAttrCallback() = default;
221     /**
222      * Called when an input parameter with attribute becomes available.
223      *
224      * @param index The index of the available input parameter.
225      * @param parameter A {@link Format} object containing the corresponding index input parameter.
226      * @param attribute A read only {@link Format} object containing the corresponding index input attribute.
227      * @since 5.0
228      */
229     virtual void OnInputParameterWithAttrAvailable(uint32_t index, std::shared_ptr<Format> attribute,
230                                                    std::shared_ptr<Format> parameter) = 0;
231 };
232 
233 class SurfaceBufferExtratDataKey {
234 public:
235     /**
236      * Key for timeStamp in surface's extraData, value type is int64
237      */
238     static constexpr std::string_view ED_KEY_TIME_STAMP = "timeStamp";
239 
240     /**
241      * Key for endOfStream in surface's extraData, value type is bool
242      */
243     static constexpr std::string_view ED_KEY_END_OF_STREAM = "endOfStream";
244 
245 private:
246     SurfaceBufferExtratDataKey() = delete;
247     ~SurfaceBufferExtratDataKey() = delete;
248 };
249 
250 class AVSourceFormat {
251 public:
252     static constexpr std::string_view SOURCE_TITLE         = "title";            // string, title
253     static constexpr std::string_view SOURCE_ARTIST        = "artist";           // string, artist
254     static constexpr std::string_view SOURCE_ALBUM         = "album";            // string, album
255     static constexpr std::string_view SOURCE_ALBUM_ARTIST  = "album_artist";     // string, album artist
256     static constexpr std::string_view SOURCE_DATE          = "date";             // string, media date,
257                                                                                  // format: YYYY-MM-DD
258     static constexpr std::string_view SOURCE_COMMENT       = "comment";          // string, comment
259     static constexpr std::string_view SOURCE_GENRE         = "genre";            // string, genre
260     static constexpr std::string_view SOURCE_COPYRIGHT     = "copyright";        // string, copyright
261     static constexpr std::string_view SOURCE_LANGUAGE      = "language";         // string, language
262     static constexpr std::string_view SOURCE_DESCRIPTION   = "description";      // string, description
263     static constexpr std::string_view SOURCE_LYRICS        = "lyrics";           // string, cyrics
264 
265     static constexpr std::string_view SOURCE_FILE_TYPE     = "file_type";        // string, type
266     static constexpr std::string_view SOURCE_HAS_VIDEO     = "has_video";        // bool, contain video tracks
267     static constexpr std::string_view SOURCE_HAS_AUDIO     = "has_audio";        // bool, contain audio tracks
268     static constexpr std::string_view SOURCE_HAS_TIMEDMETA = "has_timed_meta";   // bool, contain timed metadata tracks
269     static constexpr std::string_view SOURCE_HAS_SUBTITLE  = "has_subtitle";     // bool, contain subtitle tracks
270     static constexpr std::string_view SOURCE_AUTHOR        = "author";           // string, autbor
271     static constexpr std::string_view SOURCE_COMPOSER      = "composer";         // string, composer
272 private:
273     AVSourceFormat() = delete;
274     ~AVSourceFormat() = delete;
275 };
276 
277 enum VideoBitStreamFormat {
278     UNKNOWN = 0,
279     AVCC,
280     HVCC,
281     ANNEXB
282 };
283 
284 struct CUVVConfigBox {
285     uint16_t cuva_version_map;
286     uint16_t terminal_provide_code;
287     uint16_t terminal_provide_oriented_code;
288 };
289 } // namespace MediaAVCodec
290 } // namespace OHOS
291 #endif // MEDIA_AVCODEC_COMMOM_H
292