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_EXTEND_START = 0X10000, 42 }; 43 44 enum class API_VERSION : int32_t { 45 API_VERSION_10 = 10, 46 API_VERSION_11 = 11 47 }; 48 49 enum AVCodecBufferFlag : uint32_t { 50 AVCODEC_BUFFER_FLAG_NONE = 0, 51 /* This signals the end of stream */ 52 AVCODEC_BUFFER_FLAG_EOS = 1 << 0, 53 /* This indicates that the buffer contains the data for a sync frame */ 54 AVCODEC_BUFFER_FLAG_SYNC_FRAME = 1 << 1, 55 /* This indicates that the buffer only contains part of a frame */ 56 AVCODEC_BUFFER_FLAG_PARTIAL_FRAME = 1 << 2, 57 /* This indicated that the buffer contains codec specific data */ 58 AVCODEC_BUFFER_FLAG_CODEC_DATA = 1 << 3, 59 }; 60 61 struct AVCodecBufferInfo { 62 /* The presentation timestamp in microseconds for the buffer */ 63 int64_t presentationTimeUs = 0; 64 /* The amount of data (in bytes) in the buffer */ 65 int32_t size = 0; 66 /* The start-offset of the data in the buffer */ 67 int32_t offset = 0; 68 }; 69 70 class AVCodecCallback { 71 public: 72 virtual ~AVCodecCallback() = default; 73 /** 74 * Called when an error occurred. 75 * 76 * @param errorType Error type. For details, see {@link AVCodecErrorType}. 77 * @param errorCode Error code. 78 * @since 3.1 79 * @version 3.1 80 */ 81 virtual void OnError(AVCodecErrorType errorType, int32_t errorCode) = 0; 82 83 /** 84 * Called when the output format has changed. 85 * 86 * @param format The new output format. 87 * @since 3.1 88 * @version 3.1 89 */ 90 virtual void OnOutputFormatChanged(const Format &format) = 0; 91 92 /** 93 * Called when an input buffer becomes available. 94 * 95 * @param index The index of the available input buffer. 96 * @param buffer A {@link AVSharedMemory} object for a input buffer index that contains the data. 97 * @since 3.1 98 * @version 4.0 99 */ 100 virtual void OnInputBufferAvailable(uint32_t index, std::shared_ptr<AVSharedMemory> buffer) = 0; 101 102 /** 103 * Called when an output buffer becomes available. 104 * 105 * @param index The index of the available output buffer. 106 * @param info The info of the available output buffer. For details, see {@link AVCodecBufferInfo} 107 * @param flag The flag of the available output buffer. For details, see {@link AVCodecBufferFlag} 108 * @param buffer A {@link AVSharedMemory} object for a output buffer index that contains the data. 109 * @since 3.1 110 * @version 4.0 111 */ 112 virtual void OnOutputBufferAvailable(uint32_t index, AVCodecBufferInfo info, AVCodecBufferFlag flag, 113 std::shared_ptr<AVSharedMemory> buffer) = 0; 114 }; 115 116 class AVDemuxerCallback { 117 public: 118 virtual ~AVDemuxerCallback() = default; 119 120 /** 121 * Called when an drm info updated. 122 * 123 * @param drmInfo Drm Info. 124 * @since 4.1 125 * @version 4.1 126 */ 127 virtual void OnDrmInfoChanged(const std::multimap<std::string, std::vector<uint8_t>> &drmInfo) = 0; 128 }; 129 130 class MediaCodecCallback { 131 public: 132 virtual ~MediaCodecCallback() = default; 133 /** 134 * Called when an error occurred. 135 * 136 * @param errorType Error type. For details, see {@link AVCodecErrorType}. 137 * @param errorCode Error code. 138 * @since 4.1 139 */ 140 virtual void OnError(AVCodecErrorType errorType, int32_t errorCode) = 0; 141 142 /** 143 * Called when the output format has changed. 144 * 145 * @param format The new output format. 146 * @since 4.1 147 */ 148 virtual void OnOutputFormatChanged(const Format &format) = 0; 149 150 /** 151 * Called when an input buffer becomes available. 152 * 153 * @param index The index of the available input buffer. 154 * @param buffer A {@link AVBuffer} object for a input buffer index that contains the data. 155 * @since 4.1 156 */ 157 virtual void OnInputBufferAvailable(uint32_t index, std::shared_ptr<AVBuffer> buffer) = 0; 158 159 /** 160 * Called when an output buffer becomes available. 161 * 162 * @param index The index of the available output buffer. 163 * @param buffer A {@link AVBuffer} object for a output buffer index that contains the data. 164 * @since 4.1 165 */ 166 virtual void OnOutputBufferAvailable(uint32_t index, std::shared_ptr<AVBuffer> buffer) = 0; 167 }; 168 169 class SurfaceBufferExtratDataKey { 170 public: 171 /** 172 * Key for timeStamp in surface's extraData, value type is int64 173 */ 174 static constexpr std::string_view ED_KEY_TIME_STAMP = "timeStamp"; 175 176 /** 177 * Key for endOfStream in surface's extraData, value type is bool 178 */ 179 static constexpr std::string_view ED_KEY_END_OF_STREAM = "endOfStream"; 180 181 private: 182 SurfaceBufferExtratDataKey() = delete; 183 ~SurfaceBufferExtratDataKey() = delete; 184 }; 185 186 class AVSourceFormat { 187 public: 188 static constexpr std::string_view SOURCE_TITLE = "title"; // string, title 189 static constexpr std::string_view SOURCE_ARTIST = "artist"; // string, artist 190 static constexpr std::string_view SOURCE_ALBUM = "album"; // string, album 191 static constexpr std::string_view SOURCE_ALBUM_ARTIST = "album_artist"; // string, album artist 192 static constexpr std::string_view SOURCE_DATE = "date"; // string, media date, 193 // format: YYYY-MM-DD 194 static constexpr std::string_view SOURCE_COMMENT = "comment"; // string, comment 195 static constexpr std::string_view SOURCE_GENRE = "genre"; // string, genre 196 static constexpr std::string_view SOURCE_COPYRIGHT = "copyright"; // string, copyright 197 static constexpr std::string_view SOURCE_LANGUAGE = "language"; // string, language 198 static constexpr std::string_view SOURCE_DESCRIPTION = "description"; // string, description 199 static constexpr std::string_view SOURCE_LYRICS = "lyrics"; // string, cyrics 200 201 static constexpr std::string_view SOURCE_FILE_TYPE = "file_type"; // string, type 202 static constexpr std::string_view SOURCE_HAS_VIDEO = "has_video"; // bool, contain video tracks 203 static constexpr std::string_view SOURCE_HAS_AUDIO = "has_audio"; // bool, contain audio tracks 204 static constexpr std::string_view SOURCE_AUTHOR = "author"; // string, autbor 205 static constexpr std::string_view SOURCE_COMPOSER = "composer"; // string, composer 206 private: 207 AVSourceFormat() = delete; 208 ~AVSourceFormat() = delete; 209 }; 210 211 enum VideoBitStreamFormat { 212 UNKNOWN = 0, 213 AVCC, 214 HVCC, 215 ANNEXB 216 }; 217 218 struct CUVVConfigBox { 219 uint16_t cuva_version_map; 220 uint16_t terminal_provide_code; 221 uint16_t terminal_provide_oriented_code; 222 }; 223 } // namespace MediaAVCodec 224 } // namespace OHOS 225 #endif // MEDIA_AVCODEC_COMMOM_H 226