1 /*
2  * Copyright (c) 2023-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 FFMPEG_UTILS_H
17 #define FFMPEG_UTILS_H
18 
19 #include <string>
20 #include "meta/media_types.h"
21 #include "meta/audio_types.h"
22 #include "meta/video_types.h"
23 #include "buffer/avbuffer.h"
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 #include "libavutil/rational.h"
28 #include "libavformat/avformat.h"
29 #include "libavcodec/avcodec.h"
30 #ifdef __cplusplus
31 };
32 #endif
33 
34 namespace OHOS {
35 namespace Media {
36 namespace Plugins {
37 namespace Ffmpeg {
38 const uint32_t MS_TO_SEC = 1000;
39 bool Mime2CodecId(const std::string &mime, AVCodecID &codecId);
40 bool Raw2CodecId(AudioSampleFormat sampleFormat, AVCodecID &codecId);
41 std::string AVStrError(int errnum);
42 int64_t ConvertTimeFromFFmpeg(int64_t pts, AVRational base);
43 int64_t ConvertPts(int64_t pts, int64_t startTime);
44 int64_t ConvertTimeToFFmpeg(int64_t timestampNs, AVRational base);
45 int64_t ConvertTimeToFFmpegByUs(int64_t timestampUs, AVRational base);
46 std::string_view ConvertFFmpegMediaTypeToString(AVMediaType mediaType);
47 bool StartWith(const char* name, const char* chars);
48 uint32_t ConvertFlagsFromFFmpeg(const AVPacket& pkt, bool memoryNotEnough);
49 int64_t CalculateTimeByFrameIndex(AVStream* avStream, int keyFrameIdx);
50 void ReplaceDelimiter(const std::string &delimiters, char newDelimiter, std::string &str);
51 
52 std::vector<std::string> SplitString(const char* str, char delimiter);
53 std::vector<std::string> SplitString(const std::string& str, char delimiter);
54 
55 std::pair<bool, AVColorPrimaries> ColorPrimary2AVColorPrimaries(ColorPrimary primary);
56 std::pair<bool, AVColorTransferCharacteristic> ColorTransfer2AVColorTransfer(TransferCharacteristic transfer);
57 std::pair<bool, AVColorSpace> ColorMatrix2AVColorSpace(MatrixCoefficient matrix);
58 
59 std::vector<uint8_t> GenerateAACCodecConfig(int32_t profile, int32_t sampleRate, int32_t channels);
60 void FfmpegLogPrint(void* avcl, int level, const char* fmt, va_list vl);
61 void SetDropTag(const AVPacket& pkt, std::shared_ptr<AVBuffer> sample, AVCodecID codecId);
62 bool IsInputFormatSupported(const char* name);
63 int64_t AvTime2Us(int64_t hTime);
64 } // namespace Ffmpeg
65 } // namespace Plugins
66 } // namespace Media
67 } // namespace OHOS
68 #endif // FFMPEG_UTILS_H
69