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/plugin_meta.h"
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 #include "libavcodec/avcodec.h"
31 #include "libavutil/channel_layout.h"
32 #include "libavutil/error.h"
33 #include "libavutil/frame.h"
34 #include "libavutil/imgutils.h"
35 #include "libavutil/pixdesc.h"
36 #include "libavutil/pixfmt.h"
37 #ifdef __cplusplus
38 };
39 #endif
40 
41 namespace OHOS {
42 namespace Media {
43 namespace Plugin {
44 namespace Ffmpeg {
45 std::string AVStrError(int errnum);
46 
47 /**
48  * Convert time from ffmpeg to time in HST_TIME_BASE.
49  * @param pts ffmpeg time
50  * @param base ffmpeg time_base
51  * @return time in HST_TIME_BASE
52  */
53 int64_t ConvertTimeFromFFmpeg(int64_t pts, AVRational base);
54 
55 /**
56  * Convert time in HST_TIME_BASE to ffmpeg time.
57  * @param time time in HST_TIME_BASE
58  * @param base ffmpeg time_base
59  * @return time in ffmpeg.
60  */
61 int64_t ConvertTimeToFFmpeg(int64_t timestampUs, AVRational base);
62 
63 /*
64  * Fill in pointers in an AVFrame, aligned by 4 (required by X).
65  */
66 int FillAVPicture(AVFrame* picture, uint8_t* ptr, enum AVPixelFormat pixFmt, int width, int height);
67 
68 /*
69  * Get the size of an picture
70  */
71 int GetAVPictureSize(int pixFmt, int width, int height);
72 
73 void RemoveDelimiter(char delimiter, std::string& str);
74 
75 std::string RemoveDelimiter(const char* str, char delimiter);
76 
77 void ReplaceDelimiter(const std::string& delmiters, char newDelimiter, std::string& str);
78 
79 std::vector<std::string> SplitString(const char* str, char delimiter);
80 
81 std::vector<std::string> SplitString(const std::string& str, char delimiter);
82 
83 AudioSampleFormat ConvFf2PSampleFmt(AVSampleFormat sampleFormat);
84 
85 AVSampleFormat ConvP2FfSampleFmt(AudioSampleFormat sampleFormat);
86 
87 AudioChannelLayout ConvertChannelLayoutFromFFmpeg(int channels, uint64_t ffChannelLayout);
88 
89 uint64_t ConvertChannelLayoutToFFmpeg(AudioChannelLayout channelLayout);
90 
91 bool FindAvMetaNameByTag(Tag tag, std::string& metaName);
92 
93 void InsertMediaTag(Meta& meta, AVDictionaryEntry* tag);
94 
95 AudioAacProfile ConvAacProfileFromFfmpeg (int32_t ffmpegProfile);
96 
97 int32_t ConvAacProfileToFfmpeg (AudioAacProfile profile);
98 
99 VideoPixelFormat ConvertPixelFormatFromFFmpeg(int32_t ffmpegPixelFormat);
100 
101 AVPixelFormat ConvertPixelFormatToFFmpeg(VideoPixelFormat pixelFormat);
102 
103 bool IsYuvFormat(AVPixelFormat format);
104 
105 bool IsRgbFormat(AVPixelFormat format);
106 
107 VideoH264Profile ConvH264ProfileFromFfmpeg (int32_t ffmpegProfile);
108 
109 int32_t ConvH264ProfileToFfmpeg(VideoH264Profile profile);
110 } // namespace Ffmpeg
111 } // namespace Plugin
112 } // namespace Media
113 } // namespace OHOS
114 #endif // HISTREAMER_FFMPEG_UTILS_H
115