1 /*
2 * Copyright (C) 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 #define HST_LOG_TAG "FfmpegConverter"
17
18 #include <vector>
19 #include <algorithm>
20 #include "common/log.h"
21 #include "ffmpeg_converter.h"
22 namespace {
23 constexpr int US_PER_SECOND = 1000000;
24 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_DEMUXER, "FFmpegConverter"};
25 }
26 namespace OHOS {
27 namespace Media {
28 namespace Plugins {
29 // ffmpeg channel layout to histreamer channel layout
30 const std::vector<std::pair<AudioChannelLayout, uint64_t>> g_toFFMPEGChannelLayout = {
31 {AudioChannelLayout::MONO, AV_CH_LAYOUT_MONO},
32 {AudioChannelLayout::STEREO, AV_CH_LAYOUT_STEREO},
33 {AudioChannelLayout::CH_2POINT1, AV_CH_LAYOUT_2POINT1},
34 {AudioChannelLayout::CH_2_1, AV_CH_LAYOUT_2_1},
35 {AudioChannelLayout::SURROUND, AV_CH_LAYOUT_SURROUND},
36 {AudioChannelLayout::CH_3POINT1, AV_CH_LAYOUT_3POINT1},
37 {AudioChannelLayout::CH_4POINT0, AV_CH_LAYOUT_4POINT0},
38 {AudioChannelLayout::CH_4POINT1, AV_CH_LAYOUT_4POINT1},
39 {AudioChannelLayout::CH_2_2, AV_CH_LAYOUT_2_2},
40 {AudioChannelLayout::QUAD, AV_CH_LAYOUT_QUAD},
41 {AudioChannelLayout::CH_5POINT0, AV_CH_LAYOUT_5POINT0},
42 {AudioChannelLayout::CH_5POINT1, AV_CH_LAYOUT_5POINT1},
43 {AudioChannelLayout::CH_5POINT0_BACK, AV_CH_LAYOUT_5POINT0_BACK},
44 {AudioChannelLayout::CH_5POINT1_BACK, AV_CH_LAYOUT_5POINT1_BACK},
45 {AudioChannelLayout::CH_6POINT0, AV_CH_LAYOUT_6POINT0},
46 {AudioChannelLayout::CH_6POINT0_FRONT, AV_CH_LAYOUT_6POINT0_FRONT},
47 {AudioChannelLayout::HEXAGONAL, AV_CH_LAYOUT_HEXAGONAL},
48 {AudioChannelLayout::CH_6POINT1, AV_CH_LAYOUT_6POINT1},
49 {AudioChannelLayout::CH_6POINT1_BACK, AV_CH_LAYOUT_6POINT1_BACK},
50 {AudioChannelLayout::CH_6POINT1_FRONT, AV_CH_LAYOUT_6POINT1_FRONT},
51 {AudioChannelLayout::CH_7POINT0, AV_CH_LAYOUT_7POINT0},
52 {AudioChannelLayout::CH_7POINT0_FRONT, AV_CH_LAYOUT_7POINT0_FRONT},
53 {AudioChannelLayout::CH_7POINT1, AV_CH_LAYOUT_7POINT1},
54 {AudioChannelLayout::CH_7POINT1_WIDE, AV_CH_LAYOUT_7POINT1_WIDE},
55 {AudioChannelLayout::CH_7POINT1_WIDE_BACK, AV_CH_LAYOUT_7POINT1_WIDE_BACK},
56 {AudioChannelLayout::OCTAGONAL, AV_CH_LAYOUT_OCTAGONAL},
57 {AudioChannelLayout::HEXADECAGONAL, AV_CH_LAYOUT_HEXADECAGONAL},
58 {AudioChannelLayout::STEREO_DOWNMIX, AV_CH_LAYOUT_STEREO_DOWNMIX},
59 };
60
61 const std::vector<std::pair<AudioChannelLayout, int>> g_audioVividChannelLayoutMap = {
62 {AudioChannelLayout::MONO, 1},
63 {AudioChannelLayout::STEREO, 2},
64 {AudioChannelLayout::CH_4POINT0, 4},
65 {AudioChannelLayout::CH_5POINT1, 6},
66 {AudioChannelLayout::CH_7POINT1, 8},
67 {AudioChannelLayout::CH_5POINT1POINT2, 8},
68 {AudioChannelLayout::CH_5POINT1POINT4, 10},
69 {AudioChannelLayout::CH_7POINT1POINT2, 10},
70 {AudioChannelLayout::CH_7POINT1POINT4, 12},
71 {AudioChannelLayout::HOA_ORDER1_ACN_SN3D, 4},
72 {AudioChannelLayout::HOA_ORDER2_ACN_SN3D, 9},
73 {AudioChannelLayout::HOA_ORDER3_ACN_SN3D, 16},
74 };
75
76 const std::vector<std::pair<int, AudioChannelLayout>> g_channelLayoutDefaukltMap = {
77 {2, AudioChannelLayout::STEREO}, // 2: STEREO
78 {4, AudioChannelLayout::CH_4POINT0}, // 4: CH_4POINT0
79 {6, AudioChannelLayout::CH_5POINT1}, // 6: CH_5POINT1
80 {8, AudioChannelLayout::CH_5POINT1POINT2}, // 8: CH_5POINT1POINT2
81 {9, AudioChannelLayout::HOA_ORDER2_ACN_N3D}, // 9: HOA_ORDER2_ACN_N3D
82 {10, AudioChannelLayout::CH_7POINT1POINT2}, // 10: CH_7POINT1POINT2 or CH_5POINT1POINT4 ?
83 {12, AudioChannelLayout::CH_7POINT1POINT4}, // 12: CH_7POINT1POINT4
84 {14, AudioChannelLayout::CH_9POINT1POINT4}, // 14: CH_9POINT1POINT4
85 {16, AudioChannelLayout::CH_9POINT1POINT6}, // 16: CH_9POINT1POINT6
86 {24, AudioChannelLayout::CH_22POINT2}, // 24: CH_22POINT2
87 };
88
89 const std::vector<std::pair<AVSampleFormat, AudioSampleFormat>> g_pFfSampleFmtMap = {
90 {AVSampleFormat::AV_SAMPLE_FMT_U8, AudioSampleFormat::SAMPLE_U8},
91 {AVSampleFormat::AV_SAMPLE_FMT_S16, AudioSampleFormat::SAMPLE_S16LE},
92 {AVSampleFormat::AV_SAMPLE_FMT_S32, AudioSampleFormat::SAMPLE_S32LE},
93 {AVSampleFormat::AV_SAMPLE_FMT_FLT, AudioSampleFormat::SAMPLE_F32LE},
94 {AVSampleFormat::AV_SAMPLE_FMT_U8P, AudioSampleFormat::SAMPLE_U8P},
95 {AVSampleFormat::AV_SAMPLE_FMT_S16P, AudioSampleFormat::SAMPLE_S16P},
96 {AVSampleFormat::AV_SAMPLE_FMT_S32P, AudioSampleFormat::SAMPLE_S32P},
97 {AVSampleFormat::AV_SAMPLE_FMT_FLTP, AudioSampleFormat::SAMPLE_F32P},
98 };
99
100 // align with player framework capability.
101 const std::vector<std::pair<AVCodecID, AudioSampleFormat>> g_pFfCodeIDToSampleFmtMap = {
102 {AVCodecID::AV_CODEC_ID_PCM_U8, AudioSampleFormat::SAMPLE_U8},
103 {AVCodecID::AV_CODEC_ID_PCM_S16LE, AudioSampleFormat::SAMPLE_S16LE},
104 {AVCodecID::AV_CODEC_ID_PCM_S24LE, AudioSampleFormat::SAMPLE_S24LE},
105 {AVCodecID::AV_CODEC_ID_PCM_S32LE, AudioSampleFormat::SAMPLE_S32LE},
106 {AVCodecID::AV_CODEC_ID_PCM_F32LE, AudioSampleFormat::SAMPLE_F32LE},
107 };
108
109 const std::vector<std::pair<AudioChannelLayout, std::string_view>> g_ChannelLayoutToString = {
110 {AudioChannelLayout::UNKNOWN, "UNKNOW"},
111 {AudioChannelLayout::MONO, "MONO"},
112 {AudioChannelLayout::STEREO, "STEREO"},
113 {AudioChannelLayout::CH_2POINT1, "2POINT1"},
114 {AudioChannelLayout::CH_2_1, "CH_2_1"},
115 {AudioChannelLayout::SURROUND, "SURROUND"},
116 {AudioChannelLayout::CH_3POINT1, "3POINT1"},
117 {AudioChannelLayout::CH_4POINT0, "4POINT0"},
118 {AudioChannelLayout::CH_4POINT1, "4POINT1"},
119 {AudioChannelLayout::CH_2_2, "CH_2_2"},
120 {AudioChannelLayout::QUAD, "QUAD"},
121 {AudioChannelLayout::CH_5POINT0, "5POINT0"},
122 {AudioChannelLayout::CH_5POINT1, "5POINT1"},
123 {AudioChannelLayout::CH_5POINT0_BACK, "5POINT0_BACK"},
124 {AudioChannelLayout::CH_5POINT1_BACK, "5POINT1_BACK"},
125 {AudioChannelLayout::CH_6POINT0, "6POINT0"},
126 {AudioChannelLayout::CH_6POINT0_FRONT, "6POINT0_FRONT"},
127 {AudioChannelLayout::HEXAGONAL, "HEXAGONAL"},
128 {AudioChannelLayout::CH_6POINT1, "6POINT1"},
129 {AudioChannelLayout::CH_6POINT1_BACK, "6POINT1_BACK"},
130 {AudioChannelLayout::CH_6POINT1_FRONT, "6POINT1_FRONT"},
131 {AudioChannelLayout::CH_7POINT0, "7POINT0"},
132 {AudioChannelLayout::CH_7POINT0_FRONT, "7POINT0_FRONT"},
133 {AudioChannelLayout::CH_7POINT1, "7POINT1"},
134 {AudioChannelLayout::CH_7POINT1_WIDE, "7POINT1_WIDE"},
135 {AudioChannelLayout::CH_7POINT1_WIDE_BACK, "7POINT1_WIDE_BACK"},
136 {AudioChannelLayout::CH_3POINT1POINT2, "CH_3POINT1POINT2"},
137 {AudioChannelLayout::CH_5POINT1POINT2, "CH_5POINT1POINT2"},
138 {AudioChannelLayout::CH_5POINT1POINT4, "CH_5POINT1POINT4"},
139 {AudioChannelLayout::CH_7POINT1POINT2, "CH_7POINT1POINT2"},
140 {AudioChannelLayout::CH_7POINT1POINT4, "CH_7POINT1POINT4"},
141 {AudioChannelLayout::CH_9POINT1POINT4, "CH_9POINT1POINT4"},
142 {AudioChannelLayout::CH_9POINT1POINT6, "CH_9POINT1POINT6"},
143 {AudioChannelLayout::CH_10POINT2, "CH_10POINT2"},
144 {AudioChannelLayout::CH_22POINT2, "CH_22POINT2"},
145 {AudioChannelLayout::OCTAGONAL, "OCTAGONAL"},
146 {AudioChannelLayout::HEXADECAGONAL, "HEXADECAGONAL"},
147 {AudioChannelLayout::STEREO_DOWNMIX, "STEREO_DOWNMIX"},
148 {AudioChannelLayout::CH_2POINT0POINT2, "CH_2POINT0POINT2"},
149 {AudioChannelLayout::CH_2POINT1POINT2, "CH_2POINT1POINT2"},
150 {AudioChannelLayout::CH_3POINT0POINT2, "CH_3POINT0POINT2"},
151 {AudioChannelLayout::HOA_ORDER1_ACN_N3D, "HOA_ORDER1_ACN_N3D"},
152 {AudioChannelLayout::HOA_ORDER1_ACN_SN3D, "HOA_ORDER1_ACN_SN3D"},
153 {AudioChannelLayout::HOA_ORDER1_FUMA, "HOA_ORDER1_FUMA"},
154 {AudioChannelLayout::HOA_ORDER2_ACN_N3D, "HOA_ORDER2_ACN_N3D"},
155 {AudioChannelLayout::HOA_ORDER2_ACN_SN3D, "HOA_ORDER2_ACN_SN3D"},
156 {AudioChannelLayout::HOA_ORDER2_FUMA, "HOA_ORDER2_FUMA"},
157 {AudioChannelLayout::HOA_ORDER3_ACN_N3D, "HOA_ORDER3_ACN_N3D"},
158 {AudioChannelLayout::HOA_ORDER3_ACN_SN3D, "HOA_ORDER3_ACN_SN3D"},
159 {AudioChannelLayout::HOA_ORDER3_FUMA, "HOA_ORDER3_FUMA"},
160 };
161
162 const std::vector<std::pair<AVColorPrimaries, ColorPrimary>> g_pFfColorPrimariesMap = {
163 {AVColorPrimaries::AVCOL_PRI_BT709, ColorPrimary::BT709},
164 {AVColorPrimaries::AVCOL_PRI_UNSPECIFIED, ColorPrimary::UNSPECIFIED},
165 {AVColorPrimaries::AVCOL_PRI_BT470M, ColorPrimary::BT470_M},
166 {AVColorPrimaries::AVCOL_PRI_BT470BG, ColorPrimary::BT601_625},
167 {AVColorPrimaries::AVCOL_PRI_SMPTE170M, ColorPrimary::BT601_525},
168 {AVColorPrimaries::AVCOL_PRI_SMPTE240M, ColorPrimary::SMPTE_ST240},
169 {AVColorPrimaries::AVCOL_PRI_FILM, ColorPrimary::GENERIC_FILM},
170 {AVColorPrimaries::AVCOL_PRI_BT2020, ColorPrimary::BT2020},
171 {AVColorPrimaries::AVCOL_PRI_SMPTE428, ColorPrimary::SMPTE_ST428},
172 {AVColorPrimaries::AVCOL_PRI_SMPTEST428_1, ColorPrimary::SMPTE_ST428},
173 {AVColorPrimaries::AVCOL_PRI_SMPTE431, ColorPrimary::P3DCI},
174 {AVColorPrimaries::AVCOL_PRI_SMPTE432, ColorPrimary::P3D65},
175 };
176
177 const std::vector<std::pair<AVColorTransferCharacteristic, TransferCharacteristic>> g_pFfTransferCharacteristicMap = {
178 {AVColorTransferCharacteristic::AVCOL_TRC_BT709, TransferCharacteristic::BT709},
179 {AVColorTransferCharacteristic::AVCOL_TRC_UNSPECIFIED, TransferCharacteristic::UNSPECIFIED},
180 {AVColorTransferCharacteristic::AVCOL_TRC_GAMMA22, TransferCharacteristic::GAMMA_2_2},
181 {AVColorTransferCharacteristic::AVCOL_TRC_GAMMA28, TransferCharacteristic::GAMMA_2_8},
182 {AVColorTransferCharacteristic::AVCOL_TRC_SMPTE170M, TransferCharacteristic::BT601},
183 {AVColorTransferCharacteristic::AVCOL_TRC_SMPTE240M, TransferCharacteristic::SMPTE_ST240},
184 {AVColorTransferCharacteristic::AVCOL_TRC_LINEAR, TransferCharacteristic::LINEAR},
185 {AVColorTransferCharacteristic::AVCOL_TRC_LOG, TransferCharacteristic::LOG},
186 {AVColorTransferCharacteristic::AVCOL_TRC_LOG_SQRT, TransferCharacteristic::LOG_SQRT},
187 {AVColorTransferCharacteristic::AVCOL_TRC_IEC61966_2_4, TransferCharacteristic::IEC_61966_2_4},
188 {AVColorTransferCharacteristic::AVCOL_TRC_BT1361_ECG, TransferCharacteristic::BT1361},
189 {AVColorTransferCharacteristic::AVCOL_TRC_IEC61966_2_1, TransferCharacteristic::IEC_61966_2_1},
190 {AVColorTransferCharacteristic::AVCOL_TRC_BT2020_10, TransferCharacteristic::BT2020_10BIT},
191 {AVColorTransferCharacteristic::AVCOL_TRC_BT2020_12, TransferCharacteristic::BT2020_12BIT},
192 {AVColorTransferCharacteristic::AVCOL_TRC_SMPTE2084, TransferCharacteristic::PQ},
193 {AVColorTransferCharacteristic::AVCOL_TRC_SMPTEST2084, TransferCharacteristic::PQ},
194 {AVColorTransferCharacteristic::AVCOL_TRC_SMPTE428, TransferCharacteristic::SMPTE_ST428},
195 {AVColorTransferCharacteristic::AVCOL_TRC_SMPTEST428_1, TransferCharacteristic::SMPTE_ST428},
196 {AVColorTransferCharacteristic::AVCOL_TRC_ARIB_STD_B67, TransferCharacteristic::HLG},
197 };
198
199 const std::vector<std::pair<AVColorSpace, MatrixCoefficient>> g_pFfMatrixCoefficientMap = {
200 {AVColorSpace::AVCOL_SPC_RGB, MatrixCoefficient::IDENTITY},
201 {AVColorSpace::AVCOL_SPC_BT709, MatrixCoefficient::BT709},
202 {AVColorSpace::AVCOL_SPC_UNSPECIFIED, MatrixCoefficient::UNSPECIFIED},
203 {AVColorSpace::AVCOL_SPC_FCC, MatrixCoefficient::FCC},
204 {AVColorSpace::AVCOL_SPC_BT470BG, MatrixCoefficient::BT601_625},
205 {AVColorSpace::AVCOL_SPC_SMPTE170M, MatrixCoefficient::BT601_525},
206 {AVColorSpace::AVCOL_SPC_SMPTE240M, MatrixCoefficient::SMPTE_ST240},
207 {AVColorSpace::AVCOL_SPC_YCGCO, MatrixCoefficient::YCGCO},
208 {AVColorSpace::AVCOL_SPC_YCOCG, MatrixCoefficient::YCGCO},
209 {AVColorSpace::AVCOL_SPC_BT2020_NCL, MatrixCoefficient::BT2020_NCL},
210 {AVColorSpace::AVCOL_SPC_BT2020_CL, MatrixCoefficient::BT2020_CL},
211 {AVColorSpace::AVCOL_SPC_SMPTE2085, MatrixCoefficient::SMPTE_ST2085},
212 {AVColorSpace::AVCOL_SPC_CHROMA_DERIVED_NCL, MatrixCoefficient::CHROMATICITY_NCL},
213 {AVColorSpace::AVCOL_SPC_CHROMA_DERIVED_CL, MatrixCoefficient::CHROMATICITY_CL},
214 {AVColorSpace::AVCOL_SPC_ICTCP, MatrixCoefficient::ICTCP},
215 };
216
217 const std::vector<std::pair<AVColorRange, int>> g_pFfColorRangeMap = {
218 {AVColorRange::AVCOL_RANGE_MPEG, 0},
219 {AVColorRange::AVCOL_RANGE_JPEG, 1},
220 };
221
222 const std::vector<std::pair<AVChromaLocation, ChromaLocation>> g_pFfChromaLocationMap = {
223 {AVChromaLocation::AVCHROMA_LOC_UNSPECIFIED, ChromaLocation::UNSPECIFIED},
224 {AVChromaLocation::AVCHROMA_LOC_LEFT, ChromaLocation::LEFT},
225 {AVChromaLocation::AVCHROMA_LOC_CENTER, ChromaLocation::CENTER},
226 {AVChromaLocation::AVCHROMA_LOC_TOPLEFT, ChromaLocation::TOPLEFT},
227 {AVChromaLocation::AVCHROMA_LOC_TOP, ChromaLocation::TOP},
228 {AVChromaLocation::AVCHROMA_LOC_BOTTOMLEFT, ChromaLocation::BOTTOMLEFT},
229 {AVChromaLocation::AVCHROMA_LOC_BOTTOM, ChromaLocation::BOTTOM},
230 };
231
232 const std::vector<std::pair<int, HEVCProfile>> g_pFfHEVCProfileMap = {
233 {FF_PROFILE_HEVC_MAIN, HEVCProfile::HEVC_PROFILE_MAIN},
234 {FF_PROFILE_HEVC_MAIN_10, HEVCProfile::HEVC_PROFILE_MAIN_10},
235 {FF_PROFILE_HEVC_MAIN_STILL_PICTURE, HEVCProfile::HEVC_PROFILE_MAIN_STILL},
236 };
237
238 const std::vector<std::pair<int, HEVCLevel>> g_pFfHEVCLevelMap = {
239 {30, HEVCLevel::HEVC_LEVEL_1}, {60, HEVCLevel::HEVC_LEVEL_2}, {63, HEVCLevel::HEVC_LEVEL_21},
240 {90, HEVCLevel::HEVC_LEVEL_3}, {93, HEVCLevel::HEVC_LEVEL_31}, {120, HEVCLevel::HEVC_LEVEL_4},
241 {123, HEVCLevel::HEVC_LEVEL_41}, {150, HEVCLevel::HEVC_LEVEL_5}, {153, HEVCLevel::HEVC_LEVEL_51},
242 {156, HEVCLevel::HEVC_LEVEL_52}, {180, HEVCLevel::HEVC_LEVEL_6}, {183, HEVCLevel::HEVC_LEVEL_61},
243 {186, HEVCLevel::HEVC_LEVEL_62},
244 };
245
ConvertFFMpegToOHHEVCLevel(int ffHEVCLevel)246 HEVCLevel FFMpegConverter::ConvertFFMpegToOHHEVCLevel(int ffHEVCLevel)
247 {
248 auto ite = std::find_if(g_pFfHEVCLevelMap.begin(), g_pFfHEVCLevelMap.end(),
249 [&ffHEVCLevel](const auto &item) -> bool { return item.first == ffHEVCLevel; });
250 if (ite == g_pFfHEVCLevelMap.end()) {
251 MEDIA_LOG_W("Failed: " PUBLIC_LOG_D32, ffHEVCLevel);
252 return HEVCLevel::HEVC_LEVEL_UNKNOW;
253 }
254 return ite->second;
255 }
256
ConvertFFMpegToOHHEVCProfile(int ffHEVCProfile)257 HEVCProfile FFMpegConverter::ConvertFFMpegToOHHEVCProfile(int ffHEVCProfile)
258 {
259 auto ite = std::find_if(g_pFfHEVCProfileMap.begin(), g_pFfHEVCProfileMap.end(),
260 [&ffHEVCProfile](const auto &item) -> bool { return item.first == ffHEVCProfile; });
261 if (ite == g_pFfHEVCProfileMap.end()) {
262 MEDIA_LOG_W("Failed: " PUBLIC_LOG_D32, ffHEVCProfile);
263 return HEVCProfile::HEVC_PROFILE_UNKNOW;
264 }
265 return ite->second;
266 }
267
ConvertFFMpegToOHColorPrimaries(AVColorPrimaries ffColorPrimaries)268 ColorPrimary FFMpegConverter::ConvertFFMpegToOHColorPrimaries(AVColorPrimaries ffColorPrimaries)
269 {
270 auto ite = std::find_if(g_pFfColorPrimariesMap.begin(), g_pFfColorPrimariesMap.end(),
271 [&ffColorPrimaries](const auto &item) -> bool { return item.first == ffColorPrimaries; });
272 if (ite == g_pFfColorPrimariesMap.end()) {
273 MEDIA_LOG_W("Failed: " PUBLIC_LOG_D32, static_cast<int32_t>(ffColorPrimaries));
274 return ColorPrimary::UNSPECIFIED;
275 }
276 return ite->second;
277 }
278
ConvertFFMpegToOHColorTrans(AVColorTransferCharacteristic ffColorTrans)279 TransferCharacteristic FFMpegConverter::ConvertFFMpegToOHColorTrans(AVColorTransferCharacteristic ffColorTrans)
280 {
281 auto ite = std::find_if(g_pFfTransferCharacteristicMap.begin(), g_pFfTransferCharacteristicMap.end(),
282 [&ffColorTrans](const auto &item) -> bool { return item.first == ffColorTrans; });
283 if (ite == g_pFfTransferCharacteristicMap.end()) {
284 MEDIA_LOG_W("Failed: " PUBLIC_LOG_D32, static_cast<int32_t>(ffColorTrans));
285 return TransferCharacteristic::UNSPECIFIED;
286 }
287 return ite->second;
288 }
289
ConvertFFMpegToOHColorMatrix(AVColorSpace ffColorSpace)290 MatrixCoefficient FFMpegConverter::ConvertFFMpegToOHColorMatrix(AVColorSpace ffColorSpace)
291 {
292 auto ite = std::find_if(g_pFfMatrixCoefficientMap.begin(), g_pFfMatrixCoefficientMap.end(),
293 [&ffColorSpace](const auto &item) -> bool { return item.first == ffColorSpace; });
294 if (ite == g_pFfMatrixCoefficientMap.end()) {
295 MEDIA_LOG_W("Failed: " PUBLIC_LOG_D32, static_cast<int32_t>(ffColorSpace));
296 return MatrixCoefficient::UNSPECIFIED;
297 }
298 return ite->second;
299 }
300
ConvertFFMpegToOHColorRange(AVColorRange ffColorRange)301 int FFMpegConverter::ConvertFFMpegToOHColorRange(AVColorRange ffColorRange)
302 {
303 auto ite = std::find_if(g_pFfColorRangeMap.begin(), g_pFfColorRangeMap.end(),
304 [&ffColorRange](const auto &item) -> bool { return item.first == ffColorRange; });
305 if (ite == g_pFfColorRangeMap.end()) {
306 MEDIA_LOG_W("Failed: " PUBLIC_LOG_D32, static_cast<int32_t>(ffColorRange));
307 return 0;
308 }
309 return ite->second;
310 }
311
ConvertFFMpegToOHChromaLocation(AVChromaLocation ffChromaLocation)312 ChromaLocation FFMpegConverter::ConvertFFMpegToOHChromaLocation(AVChromaLocation ffChromaLocation)
313 {
314 auto ite = std::find_if(g_pFfChromaLocationMap.begin(), g_pFfChromaLocationMap.end(),
315 [&ffChromaLocation](const auto &item) -> bool { return item.first == ffChromaLocation; });
316 if (ite == g_pFfChromaLocationMap.end()) {
317 MEDIA_LOG_W("Failed: " PUBLIC_LOG_D32, static_cast<int32_t>(ffChromaLocation));
318 return ChromaLocation::UNSPECIFIED;
319 }
320 return ite->second;
321 }
322
ConvertFFMpegAVCodecIdToOHAudioFormat(AVCodecID codecId)323 AudioSampleFormat FFMpegConverter::ConvertFFMpegAVCodecIdToOHAudioFormat(AVCodecID codecId)
324 {
325 auto ite = std::find_if(g_pFfCodeIDToSampleFmtMap.begin(), g_pFfCodeIDToSampleFmtMap.end(),
326 [&codecId](const auto &item) -> bool { return item.first == codecId; });
327 if (ite == g_pFfCodeIDToSampleFmtMap.end()) {
328 MEDIA_LOG_W("Failed: " PUBLIC_LOG_D32, static_cast<int32_t>(codecId));
329 return AudioSampleFormat::INVALID_WIDTH;
330 }
331 return ite->second;
332 }
333
ConvertFFMpegToOHAudioFormat(AVSampleFormat ffSampleFormat)334 AudioSampleFormat FFMpegConverter::ConvertFFMpegToOHAudioFormat(AVSampleFormat ffSampleFormat)
335 {
336 auto ite = std::find_if(g_pFfSampleFmtMap.begin(), g_pFfSampleFmtMap.end(),
337 [&ffSampleFormat](const auto &item) -> bool { return item.first == ffSampleFormat; });
338 if (ite == g_pFfSampleFmtMap.end()) {
339 MEDIA_LOG_W("Failed: " PUBLIC_LOG_D32, static_cast<int32_t>(ffSampleFormat));
340 return AudioSampleFormat::INVALID_WIDTH;
341 }
342 return ite->second;
343 }
344
ConvertOHAudioFormatToFFMpeg(AudioSampleFormat sampleFormat)345 AVSampleFormat FFMpegConverter::ConvertOHAudioFormatToFFMpeg(AudioSampleFormat sampleFormat)
346 {
347 auto ite = std::find_if(g_pFfSampleFmtMap.begin(), g_pFfSampleFmtMap.end(),
348 [&sampleFormat](const auto &item) -> bool { return item.second == sampleFormat; });
349 if (ite == g_pFfSampleFmtMap.end()) {
350 MEDIA_LOG_W("Failed: " PUBLIC_LOG_D32, static_cast<int32_t>(sampleFormat));
351 return AVSampleFormat::AV_SAMPLE_FMT_NONE;
352 }
353 return ite->first;
354 }
355
ConvertFFToOHAudioChannelLayout(uint64_t ffChannelLayout)356 AudioChannelLayout FFMpegConverter::ConvertFFToOHAudioChannelLayout(uint64_t ffChannelLayout)
357 {
358 auto ite = std::find_if(g_toFFMPEGChannelLayout.begin(), g_toFFMPEGChannelLayout.end(),
359 [&ffChannelLayout](const auto &item) -> bool { return item.second == ffChannelLayout; });
360 if (ite == g_toFFMPEGChannelLayout.end()) {
361 MEDIA_LOG_W("Failed: " PUBLIC_LOG_U64, ffChannelLayout);
362 return AudioChannelLayout::MONO;
363 }
364 return ite->first;
365 }
366
GetDefaultChannelLayout(int channels)367 AudioChannelLayout FFMpegConverter::GetDefaultChannelLayout(int channels)
368 {
369 AudioChannelLayout layout = AudioChannelLayout::MONO;
370 auto ite = std::find_if(g_channelLayoutDefaukltMap.begin(), g_channelLayoutDefaukltMap.end(),
371 [&channels](const auto &item) -> bool { return item.first == channels; });
372 if (ite != g_channelLayoutDefaukltMap.end()) {
373 layout = ite->second;
374 }
375 MEDIA_LOG_W("Default: " PUBLIC_LOG_S, ConvertOHAudioChannelLayoutToString(layout).data());
376 return layout;
377 }
378
ConvertFFToOHAudioChannelLayoutV2(uint64_t ffChannelLayout,int channels)379 AudioChannelLayout FFMpegConverter::ConvertFFToOHAudioChannelLayoutV2(uint64_t ffChannelLayout, int channels)
380 {
381 auto ite = std::find_if(g_toFFMPEGChannelLayout.begin(), g_toFFMPEGChannelLayout.end(),
382 [&ffChannelLayout](const auto &item) -> bool { return item.second == ffChannelLayout; });
383 if (ite == g_toFFMPEGChannelLayout.end()) {
384 MEDIA_LOG_W("Failed: " PUBLIC_LOG_U64, ffChannelLayout);
385 return GetDefaultChannelLayout(channels);
386 }
387 return ite->first;
388 }
389
ConvertOHAudioChannelLayoutToFFMpeg(AudioChannelLayout channelLayout)390 uint64_t FFMpegConverter::ConvertOHAudioChannelLayoutToFFMpeg(AudioChannelLayout channelLayout)
391 {
392 auto ite = std::find_if(g_toFFMPEGChannelLayout.begin(), g_toFFMPEGChannelLayout.end(),
393 [&channelLayout](const auto &item) -> bool { return item.first == channelLayout; });
394 if (ite == g_toFFMPEGChannelLayout.end()) {
395 MEDIA_LOG_W("Failed: " PUBLIC_LOG_D32, static_cast<int32_t>(channelLayout));
396 return AV_CH_LAYOUT_NATIVE;
397 }
398 return ite->second;
399 }
400
ConvertOHAudioChannelLayoutToString(AudioChannelLayout layout)401 std::string_view FFMpegConverter::ConvertOHAudioChannelLayoutToString(AudioChannelLayout layout)
402 {
403 auto ite = std::find_if(g_ChannelLayoutToString.begin(), g_ChannelLayoutToString.end(),
404 [&layout](const auto &item) -> bool { return item.first == layout; });
405 if (ite == g_ChannelLayoutToString.end()) {
406 MEDIA_LOG_W("Failed: " PUBLIC_LOG_D32, static_cast<int32_t>(layout));
407 return g_ChannelLayoutToString[0].second;
408 }
409 return ite->second;
410 }
411
ConvertAudioPtsToUs(int64_t pts,AVRational base)412 int64_t FFMpegConverter::ConvertAudioPtsToUs(int64_t pts, AVRational base)
413 {
414 if (pts == AV_NOPTS_VALUE) {
415 return -1;
416 }
417 AVRational us = {1, US_PER_SECOND};
418 return av_rescale_q(pts, base, us);
419 }
420
AVStrError(int errnum)421 std::string FFMpegConverter::AVStrError(int errnum)
422 {
423 char errbuf[AV_ERROR_MAX_STRING_SIZE] = {0};
424 av_strerror(errnum, errbuf, AV_ERROR_MAX_STRING_SIZE);
425 return std::string(errbuf);
426 }
427
ConvertAudioVividToOHAudioChannelLayout(uint64_t ffChannelLayout,int channels)428 AudioChannelLayout FFMpegConverter::ConvertAudioVividToOHAudioChannelLayout(uint64_t ffChannelLayout, int channels)
429 {
430 auto ite = std::find_if(g_audioVividChannelLayoutMap.begin(), g_audioVividChannelLayoutMap.end(),
431 [&ffChannelLayout](const auto &item) -> bool {
432 return static_cast<uint64_t>(item.first) == ffChannelLayout;
433 });
434 if (ite == g_audioVividChannelLayoutMap.end() || ite -> second != channels) {
435 MEDIA_LOG_W("Convert channel layout failed: " PUBLIC_LOG_U64, ffChannelLayout);
436 return GetDefaultChannelLayout(channels);
437 }
438 return ite->first;
439 }
440 } // namespace Plugins
441 } // namespace Media
442 } // namespace OHOS