• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2021 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 HISTREAMER_FFMPEG_UTILS_H
17 #define HISTREAMER_FFMPEG_UTILS_H
18 
19 #include <string>
20 #include <type_traits>
21 #include <vector>
22 #include "plugin/common/plugin_tags.h"
23 #include "plugin/common/plugin_audio_tags.h"
24 #include "plugin/common/plugin_video_tags.h"
25 #include "plugin/common/tag_map.h"
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 #include "libavutil/error.h"
31 #include "libavutil/frame.h"
32 #include "libavutil/pixdesc.h"
33 #include "libavcodec/avcodec.h"
34 #include "libswresample/swresample.h"
35 #ifdef __cplusplus
36 };
37 #endif
38 
39 namespace OHOS {
40 namespace Media {
41 namespace Plugin {
42 namespace Ffmpeg {
43 std::string AVStrError(int errnum);
44 
45 /**
46  * Convert time from ffmpeg to time in HST_TIME_BASE.
47  * @param pts ffmpeg time
48  * @param base ffmpeg time_base
49  * @return time in HST_TIME_BASE
50  */
51 int64_t ConvertTimeFromFFmpeg(int64_t pts, AVRational base);
52 
53 /**
54  * Convert time in HST_TIME_BASE to ffmpeg time.
55  * @param time time in HST_TIME_BASE
56  * @param base ffmpeg time_base
57  * @return time in ffmpeg.
58  */
59 int64_t ConvertTimeToFFmpeg(int64_t timestampUs, AVRational base);
60 
61 /*
62  * Fill in pointers in an AVFrame, aligned by 4 (required by X).
63  */
64 int FillAVPicture(AVFrame* picture, uint8_t* ptr, enum AVPixelFormat pixFmt, int width, int height);
65 
66 /*
67  * Get the size of an picture
68  */
69 int GetAVPictureSize(int pixFmt, int width, int height);
70 
71 void RemoveDelimiter(char delimiter, std::string& str);
72 
73 std::string RemoveDelimiter(const char* str, char delimiter);
74 
75 void ReplaceDelimiter(const std::string& delmiters, char newDelimiter, std::string& str);
76 
77 std::vector<std::string> SplitString(const char* str, char delimiter);
78 
79 std::vector<std::string> SplitString(const std::string& str, char delimiter);
80 
81 AudioSampleFormat ConvFf2PSampleFmt(AVSampleFormat sampleFormat);
82 
83 AVSampleFormat ConvP2FfSampleFmt(AudioSampleFormat sampleFormat);
84 
85 AudioChannelLayout ConvertChannelLayoutFromFFmpeg(int channels, uint64_t ffChannelLayout);
86 
87 uint64_t ConvertChannelLayoutToFFmpeg(AudioChannelLayout channelLayout);
88 
89 bool FindAvMetaNameByTag(Tag tag, std::string& metaName);
90 
91 void InsertMediaTag(TagMap& meta, AVDictionaryEntry* tag);
92 
93 AudioAacProfile ConvAacProfileFromFfmpeg (int32_t ffmpegProfile);
94 
95 int32_t ConvAacProfileToFfmpeg (AudioAacProfile profile);
96 
97 VideoPixelFormat ConvertPixelFormatFromFFmpeg(int32_t ffmpegPixelFormat);
98 
99 AVPixelFormat ConvertPixelFormatToFFmpeg(VideoPixelFormat pixelFormat);
100 
101 bool IsYuvFormat(AVPixelFormat format);
102 
103 bool IsRgbFormat(AVPixelFormat format);
104 
105 VideoH264Profile ConvH264ProfileFromFfmpeg (int32_t ffmpegProfile);
106 
107 int32_t ConvH264ProfileToFfmpeg(VideoH264Profile profile);
108 
109 struct ResamplePara {
110     uint32_t channels {2}; // 2: STEREO
111     uint32_t sampleRate {0};
112     uint32_t bitsPerSample {0};
113     int64_t channelLayout {0};
114     AVSampleFormat srcFfFmt {AV_SAMPLE_FMT_NONE};
115     uint32_t destSamplesPerFrame {0};
116     AVSampleFormat destFmt {AV_SAMPLE_FMT_S16};
117 };
118 
119 class Resample {
120 public:
121     Status Init(const ResamplePara& resamplePara);
122     Status Convert(const uint8_t* srcBuffer, const size_t srcLength, uint8_t*& destBuffer, size_t& destLength);
123 private:
124     ResamplePara resamplePara_ {};
125 #if defined(_WIN32) || !defined(OHOS_LITE)
126     std::vector<uint8_t> resampleCache_ {};
127     std::vector<uint8_t*> resampleChannelAddr_ {};
128     std::shared_ptr<SwrContext> swrCtx_ {nullptr};
129 #endif
130 };
131 } // namespace Ffmpeg
132 } // namespace Plugin
133 } // namespace Media
134 } // namespace OHOS
135 #endif // HISTREAMER_FFMPEG_UTILS_H
136