1 /* 2 * Copyright (C) 2024-2025 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 TRANSCODER_H 17 #define TRANSCODER_H 18 19 #include <cstdint> 20 #include <string> 21 #include <map> 22 #include <set> 23 #include <parcel.h> 24 #include "meta/format.h" 25 #include "surface.h" 26 #include "surface/native_buffer.h" 27 #include "av_common.h" 28 #include "codec_capability.h" 29 #include "media_core.h" 30 31 namespace OHOS { 32 namespace Media { 33 using ConfigMap = std::map<std::string, int32_t>; 34 35 /** 36 * @brief Enumerates transcodering error types. 37 * 38 * @since 5.0 39 * @version 5.0 40 */ 41 enum TransCoderErrorType : int32_t { 42 TRANSCODER_ERROR_INTERNAL 43 }; 44 45 enum TransCoderOnInfoType : int32_t { 46 /* return the current progress of transcoder automatically. */ 47 INFO_TYPE_TRANSCODER_COMPLETED = 0, 48 /* return the current progress of transcoder automatically. */ 49 INFO_TYPE_PROGRESS_UPDATE = 1, 50 }; 51 52 /** 53 * @brief Enumerates transcodering color space. 54 * 55 * @since 6.0 56 * @version 6.0 57 */ 58 enum TranscoderColorSpace : int32_t { 59 /** None color space */ 60 TRANSCODER_COLORSPACE_NONE = OH_NativeBuffer_ColorSpace::OH_COLORSPACE_NONE, 61 /** COLORPRIMARIES_BT601_P */ 62 TRANSCODER_COLORSPACE_BT601_EBU_FULL = OH_NativeBuffer_ColorSpace::OH_COLORSPACE_BT601_EBU_FULL, 63 /** COLORPRIMARIES_BT601_N */ 64 TRANSCODER_COLORSPACE_BT601_SMPTE_C_FULL = OH_NativeBuffer_ColorSpace::OH_COLORSPACE_BT601_SMPTE_C_FULL, 65 /** COLORPRIMARIES_BT709 */ 66 TRANSCODER_COLORSPACE_BT709_FULL = OH_NativeBuffer_ColorSpace::OH_COLORSPACE_BT709_FULL, 67 /** COLORPRIMARIES_BT2020 */ 68 TRANSCODER_COLORSPACE_BT2020_HLG_FULL = OH_NativeBuffer_ColorSpace::OH_COLORSPACE_BT2020_HLG_FULL, 69 /** COLORPRIMARIES_BT2020 */ 70 TRANSCODER_COLORSPACE_BT2020_PQ_FULL = OH_NativeBuffer_ColorSpace::OH_COLORSPACE_BT2020_PQ_FULL, 71 /** COLORPRIMARIES_BT601_P */ 72 TRANSCODER_COLORSPACE_BT601_EBU_LIMIT = OH_NativeBuffer_ColorSpace::OH_COLORSPACE_BT601_EBU_LIMIT, 73 /** COLORPRIMARIES_BT601_N */ 74 TRANSCODER_COLORSPACE_BT601_SMPTE_C_LIMIT = OH_NativeBuffer_ColorSpace::OH_COLORSPACE_BT601_SMPTE_C_LIMIT, 75 /** COLORPRIMARIES_BT709 */ 76 TRANSCODER_COLORSPACE_BT709_LIMIT = OH_NativeBuffer_ColorSpace::OH_COLORSPACE_BT709_LIMIT, 77 /** COLORPRIMARIES_BT2020 */ 78 TRANSCODER_COLORSPACE_BT2020_HLG_LIMIT = OH_NativeBuffer_ColorSpace::OH_COLORSPACE_BT2020_HLG_LIMIT, 79 /** COLORPRIMARIES_BT2020 */ 80 TRANSCODER_COLORSPACE_BT2020_PQ_LIMIT = OH_NativeBuffer_ColorSpace::OH_COLORSPACE_BT2020_PQ_LIMIT, 81 /** COLORPRIMARIES_SRGB */ 82 TRANSCODER_COLORSPACE_SRGB_FULL = OH_NativeBuffer_ColorSpace::OH_COLORSPACE_SRGB_FULL, 83 /** COLORPRIMARIES_P3_D65 */ 84 TRANSCODER_COLORSPACE_P3_FULL = OH_NativeBuffer_ColorSpace::OH_COLORSPACE_P3_FULL, 85 /** COLORPRIMARIES_P3_D65 */ 86 TRANSCODER_COLORSPACE_P3_HLG_FULL = OH_NativeBuffer_ColorSpace::OH_COLORSPACE_P3_HLG_FULL, 87 /** COLORPRIMARIES_P3_D65 */ 88 TRANSCODER_COLORSPACE_P3_PQ_FULL = OH_NativeBuffer_ColorSpace::OH_COLORSPACE_P3_PQ_FULL, 89 /** COLORPRIMARIES_ADOBERGB */ 90 TRANSCODER_COLORSPACE_ADOBERGB_FULL = OH_NativeBuffer_ColorSpace::OH_COLORSPACE_ADOBERGB_FULL, 91 /** COLORPRIMARIES_SRGB */ 92 TRANSCODER_COLORSPACE_SRGB_LIMIT = OH_NativeBuffer_ColorSpace::OH_COLORSPACE_SRGB_LIMIT, 93 /** COLORPRIMARIES_P3_D65 */ 94 TRANSCODER_COLORSPACE_P3_LIMIT = OH_NativeBuffer_ColorSpace::OH_COLORSPACE_P3_LIMIT, 95 /** COLORPRIMARIES_P3_D65 */ 96 TRANSCODER_COLORSPACE_P3_HLG_LIMIT = OH_NativeBuffer_ColorSpace::OH_COLORSPACE_P3_HLG_LIMIT, 97 /** COLORPRIMARIES_P3_D65 */ 98 TRANSCODER_COLORSPACE_P3_PQ_LIMIT = OH_NativeBuffer_ColorSpace::OH_COLORSPACE_P3_PQ_LIMIT, 99 /** COLORPRIMARIES_ADOBERGB */ 100 TRANSCODER_COLORSPACE_ADOBERGB_LIMIT = OH_NativeBuffer_ColorSpace::OH_COLORSPACE_ADOBERGB_LIMIT, 101 /** COLORPRIMARIES_SRGB */ 102 TRANSCODER_COLORSPACE_LINEAR_SRGB = OH_NativeBuffer_ColorSpace::OH_COLORSPACE_LINEAR_SRGB, 103 /** equal to OH_COLORSPACE_LINEAR_SRGB */ 104 TRANSCODER_COLORSPACE_LINEAR_BT709 = OH_NativeBuffer_ColorSpace::OH_COLORSPACE_LINEAR_BT709, 105 /** COLORPRIMARIES_P3_D65 */ 106 TRANSCODER_COLORSPACE_LINEAR_P3 = OH_NativeBuffer_ColorSpace::OH_COLORSPACE_LINEAR_P3, 107 /** COLORPRIMARIES_BT2020 */ 108 TRANSCODER_COLORSPACE_LINEAR_BT2020 = OH_NativeBuffer_ColorSpace::OH_COLORSPACE_LINEAR_BT2020, 109 /** equal to OH_COLORSPACE_SRGB_FULL */ 110 TRANSCODER_COLORSPACE_DISPLAY_SRGB = OH_NativeBuffer_ColorSpace::OH_COLORSPACE_DISPLAY_SRGB, 111 /** equal to OH_COLORSPACE_P3_FULL */ 112 TRANSCODER_COLORSPACE_DISPLAY_P3_SRGB = OH_NativeBuffer_ColorSpace::OH_COLORSPACE_DISPLAY_P3_SRGB, 113 /** equal to OH_COLORSPACE_P3_HLG_FULL */ 114 TRANSCODER_COLORSPACE_DISPLAY_P3_HLG = OH_NativeBuffer_ColorSpace::OH_COLORSPACE_DISPLAY_P3_HLG, 115 /** equal to OH_COLORSPACE_P3_PQ_FULL */ 116 TRANSCODER_COLORSPACE_DISPLAY_P3_PQ = OH_NativeBuffer_ColorSpace::OH_COLORSPACE_DISPLAY_P3_PQ, 117 /** COLORPRIMARIES_BT2020 */ 118 TRANSCODER_COLORSPACE_DISPLAY_BT2020_SRGB = OH_NativeBuffer_ColorSpace::OH_COLORSPACE_DISPLAY_BT2020_SRGB, 119 /** equal to OH_COLORSPACE_BT2020_HLG_FULL */ 120 TRANSCODER_COLORSPACE_DISPLAY_BT2020_HLG = OH_NativeBuffer_ColorSpace::OH_COLORSPACE_DISPLAY_BT2020_HLG, 121 /** equal to OH_COLORSPACE_BT2020_PQ_FULL */ 122 TRANSCODER_COLORSPACE_DISPLAY_BT2020_PQ = OH_NativeBuffer_ColorSpace::OH_COLORSPACE_DISPLAY_BT2020_PQ, 123 }; 124 125 /** 126 * @brief Provides listeners for transcodering errors and information events. 127 * 128 * @since 5.0 129 * @version 5.0 130 */ 131 class TransCoderCallback { 132 public: 133 virtual ~TransCoderCallback() = default; 134 135 /** 136 * @brief Called when an error occurs during transcodering. This callback is used to report transcodering errors. 137 * 138 * @param errorType Indicates the error type. For details, see {@link TransCoderErrorType}. 139 * @param errorCode Indicates the error code. 140 * @since 1.0 141 * @version 1.0 142 */ 143 virtual void OnError(int32_t errorCode, const std::string &errorMsg) = 0; 144 145 /** 146 * @brief Called when an information event occurs during transcodering. This callback is used to report 147 * transcodering information. 148 * 149 * @param type Indicates the information type. For details, see {@link TransCoderInfoType}. 150 * @param extra Indicates other information, for example, the start time position of a transcodering file. 151 * @since 1.0 152 * @version 1.0 153 */ 154 virtual void OnInfo(int32_t type, int32_t extra) = 0; 155 }; 156 157 /** 158 * @brief Provides functions for audio and video transcodering. 159 * 160 * @since 1.0 161 * @version 1.0 162 */ 163 class TransCoder { 164 public: 165 virtual ~TransCoder() = default; 166 167 /** 168 * @brief Sets the output file format. 169 * 170 * This function must be called before {@link Prepare} and after after all required sources have been set. After 171 * this function called, no more source settings allowed. 172 * 173 * @param format Indicates the output file format. For details, see {@link OutputFormatType}. 174 * @return Returns {@link MSERR_OK} if the setting is successful; returns an error code otherwise. 175 * @since 1.0 176 * @version 1.0 177 */ 178 virtual int32_t SetOutputFormat(OutputFormatType format) = 0; 179 180 /** 181 * @brief Sets the encoder of the video to transcoder. 182 * 183 * If this function is not called, the output file does not contain the video track when the video source is 184 * YUV or RGB. 185 * This function must be called after {@link SetOutputFormat} but before {@link Prepare}. 186 * 187 * @param encoder Indicates the video encoder to set. For details, see {@link VideoCodecFormat}. 188 * @return Returns {@link MSERR_OK} if the setting is successful; returns an error code otherwise. 189 * @since 1.0 190 * @version 1.0 191 */ 192 virtual int32_t SetVideoEncoder(VideoCodecFormat encoder) = 0; 193 194 /** 195 * @brief Sets the encoding bit rate of the video to transcoder. 196 * 197 * This function must be called after {@link SetOutputFormat} but before {@link Prepare}. 198 * 199 * @param rate Indicates the encoding bit rate to set. 200 * @return Returns {@link MSERR_OK} if the setting is successful; returns an error code otherwise. 201 * @since 1.0 202 * @version 1.0 203 */ 204 virtual int32_t SetVideoEncodingBitRate(int32_t rate) = 0; 205 206 /** 207 * @brief Sets the encoding video size of the video to transcoder. 208 * 209 * This function must be called after {@link SetOutputFormat} but before {@link Prepare}. 210 * 211 * @param rate Indicates the encoding bit rate to set. 212 * @return Returns {@link MSERR_OK} if the setting is successful; returns an error code otherwise. 213 * @since 1.0 214 * @version 1.0 215 */ 216 virtual int32_t SetVideoSize(int32_t videoFrameWidth, int32_t videoFrameHeight) = 0; 217 218 /** 219 * @brief Sets the colorspace of the video to transcoder 220 * 221 * This function must be called after {@link SetOutputFormat} but before {@link Prepare}. 222 * 223 * @param colorSpaceFormat Indicates the colorSpace format of the video to set. 224 * @return Returns {@link MSERR_OK} if the setting is successful; returns an error code otherwise. 225 * @since 1.0 226 * @version 1.0 227 */ 228 virtual int32_t SetColorSpace(TranscoderColorSpace colorSpaceFormat) = 0; 229 230 /** 231 * @brief Sets the B frame encoding to transcoder 232 * 233 * This function must be called after {@link SetOutputFormat} but before {@link Prepare}. 234 * 235 * @param enableBFrame Indicates whether to enable B frame encoding for reduce file size. 236 * The default value is false, which means B frame encoding cannot be enabled. 237 * @return Returns {@link MSERR_OK} if the setting is successful; returns an error code otherwise. 238 * @since 1.0 239 * @version 1.0 240 */ 241 virtual int32_t SetEnableBFrame(bool enableBFrame) = 0; 242 243 /** 244 * @brief Sets the encoder of the audio to transcoder. 245 * 246 * If this function is not called, the output file does not contain the audio track. 247 * This function must be called after {@link SetOutputFormat} but before {@link Prepare}. 248 * 249 * @param encoder Indicates the audio encoder to set. 250 * @return Returns {@link MSERR_OK} if the setting is successful; returns an error code otherwise. 251 * @since 1.0 252 * @version 1.0 253 */ 254 virtual int32_t SetAudioEncoder(AudioCodecFormat encoder) = 0; 255 256 /** 257 * @brief Sets the encoding bit rate of the audio to transcoder. 258 * 259 * This function must be called after {@link SetOutputFormat} but before {@link Prepare}. 260 * 261 * @param bitRate Indicates the audio encoding bit rate, in bit/s. 262 * @return Returns {@link MSERR_OK} if the setting is successful; returns an error code otherwise. 263 * @since 1.0 264 * @version 1.0 265 */ 266 virtual int32_t SetAudioEncodingBitRate(int32_t bitRate) = 0; 267 268 /** 269 * @brief Sets the file descriptor (FD) of the input file. 270 * 271 * @param fd Indicates the FD of the file. 272 * @return Returns {@link MSERR_OK} if the setting is successful; returns an error code otherwise. 273 * @since 1.0 274 * @version 1.0 275 */ 276 virtual int32_t SetInputFile(int32_t fd, int64_t offset, int64_t size) = 0; 277 278 /** 279 * @brief Sets the file descriptor (FD) of the output file. 280 * 281 * This function must be called after {@link SetOutputFormat} but before {@link Prepare} 282 * 283 * @param fd Indicates the FD of the file. 284 * @return Returns {@link MSERR_OK} if the setting is successful; returns an error code otherwise. 285 * @since 1.0 286 * @version 1.0 287 */ 288 virtual int32_t SetOutputFile(int32_t fd) = 0; 289 290 /** 291 * @brief Registers a transcodering listener. 292 * 293 * This function must be called after {@link SetOutputFormat} but before {@link Prepare} 294 * 295 * @param callback Indicates the transcodering listener to register. For details, see {@link TransCoderCallback}. 296 * @return Returns {@link MSERR_OK} if the setting is successful; returns an error code otherwise. 297 * @since 1.0 298 * @version 1.0 299 */ 300 virtual int32_t SetTransCoderCallback(const std::shared_ptr<TransCoderCallback> &callback) = 0; 301 302 /** 303 * @brief Prepares for transcodering. 304 * 305 * This function must be called before {@link Start}. 306 * 307 * @return Returns {@link MSERR_OK} if the preparation is successful; returns an error code otherwise. 308 * @since 1.0 309 * @version 1.0 310 */ 311 virtual int32_t Prepare() = 0; 312 313 /** 314 * @brief Starts transcodering. 315 * 316 * This function must be called after {@link Prepare}. 317 * 318 * @return Returns {@link MSERR_OK} if the transcodering is started; returns an error code otherwise. 319 * @since 1.0 320 * @version 1.0 321 */ 322 virtual int32_t Start() = 0; 323 324 /** 325 * @brief Pauses transcodering. 326 * 327 * After {@link Start} is called, you can call this function to pause transcodering. 328 * 329 * @return Returns {@link MSERR_OK} if the transcodering is paused; returns an error code otherwise. 330 * @since 1.0 331 * @version 1.0 332 */ 333 virtual int32_t Pause() = 0; 334 335 /** 336 * @brief Resumes transcodering. 337 * 338 * You can call this function to resume transcodering after {@link Pause} is called. 339 * 340 * @return Returns {@link MSERR_OK} if the transcodering is resumed; returns an error code otherwise. 341 * @since 1.0 342 * @version 1.0 343 */ 344 virtual int32_t Resume() = 0; 345 346 /** 347 * @brief Cancel transcodering. 348 * 349 * @param block Indicates the stop mode. The value <b>true</b> indicates that the processing stops after all caches 350 * are processed, and <b>false</b> indicates that the processing stops immediately and all caches are discarded. 351 * After the transcodering stopped, all sources and parameters must be set again to restore transcodering. 352 * The function is like to {@link Reset}, except that the block parameter is allowed to be specified. 353 * @return Returns {@link MSERR_OK} if the transcodering is stopped; returns an error code otherwise. 354 * @since 1.0 355 * @version 1.0 356 */ 357 virtual int32_t Cancel() = 0; 358 359 /** 360 * @brief Releases transcodering resources. After this function called, none of interfaces of {@link Transcoder} 361 * can be used. 362 * 363 * @return Returns {@link MSERR_OK} if the transcodering is stopped; returns an error code otherwise. 364 * @since 1.0 365 * @version 1.0 366 */ 367 virtual int32_t Release() = 0; 368 }; 369 370 class __attribute__((visibility("default"))) TransCoderFactory { 371 public: 372 #ifdef UNSUPPORT_TRANSCODER CreateTransCoder()373 static std::shared_ptr<TransCoder> CreateTransCoder() 374 { 375 return nullptr; 376 } 377 #else 378 static std::shared_ptr<TransCoder> CreateTransCoder(); 379 #endif 380 private: 381 TransCoderFactory() = default; 382 ~TransCoderFactory() = default; 383 }; 384 } // namespace Media 385 } // namespace OHOS 386 #endif // TRANSCODER_H 387