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