• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 ConvertTimeToFFmpeg(int64_t timestampNs, AVRational base);
44 int64_t ConvertTimeToFFmpegByUs(int64_t timestampUs, AVRational base);
45 bool StartWith(const char* name, const char* chars);
46 uint32_t ConvertFlagsFromFFmpeg(const AVPacket& pkt, bool memoryNotEnough);
47 int64_t CalculateTimeByFrameIndex(AVStream* avStream, int keyFrameIdx);
48 void ReplaceDelimiter(const std::string &delimiters, char newDelimiter, std::string &str);
49 
50 std::vector<std::string> SplitString(const char* str, char delimiter);
51 std::vector<std::string> SplitString(const std::string& str, char delimiter);
52 
53 std::pair<bool, AVColorPrimaries> ColorPrimary2AVColorPrimaries(ColorPrimary primary);
54 std::pair<bool, AVColorTransferCharacteristic> ColorTransfer2AVColorTransfer(TransferCharacteristic transfer);
55 std::pair<bool, AVColorSpace> ColorMatrix2AVColorSpace(MatrixCoefficient matrix);
56 
57 std::vector<uint8_t> GenerateAACCodecConfig(int32_t profile, int32_t sampleRate, int32_t channels);
58 void SetDropTag(const AVPacket& pkt, std::shared_ptr<AVBuffer> sample, AVCodecID codecId);
59 int64_t AvTime2Us(int64_t hTime);
60 std::string hexEncode(const std::vector<uint8_t>& data);
61 
62 struct FlacCodecConfig {
63     bool GenerateCodecConfig(const std::shared_ptr<Meta> &trackDesc);
64     void UpdateNewConfig(uint8_t *data, size_t size);
65     void UpdatePerFrame(uint8_t* data, size_t size);
66     void UpdateBitPerSample(uint8_t byte);
67     bool Update();
68     uint64_t mTotalSample = 0;
69     uint16_t mBlockSize = 0;
70     bool mIsFirstDataFrame = {true};
71     bool mIsUpdateExtraData = {false};
72     uint32_t mMaxFrameSize = 0;
73     uint32_t mMinFrameSize = 0;
74     int32_t mSampleRate = 0;
75     int32_t mChannels = 0;
76     uint8_t mBitPerSample = 0;
77     std::vector<uint8_t> mCodecConfig;
78 };
79 } // namespace Ffmpeg
80 } // namespace Plugins
81 } // namespace Media
82 } // namespace OHOS
83 #endif // FFMPEG_UTILS_H
84