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 #include "avcodec_log.h"
23 namespace {
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_channelLayoutDefaultMap = {
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 {AVCodecID::AV_CODEC_ID_PCM_S16BE, AudioSampleFormat::SAMPLE_S16BE},
108 {AVCodecID::AV_CODEC_ID_PCM_S24BE, AudioSampleFormat::SAMPLE_S24BE},
109 {AVCodecID::AV_CODEC_ID_PCM_S32BE, AudioSampleFormat::SAMPLE_S32BE},
110 {AVCodecID::AV_CODEC_ID_PCM_F32BE, AudioSampleFormat::SAMPLE_F32BE},
111 {AVCodecID::AV_CODEC_ID_PCM_F64BE, AudioSampleFormat::SAMPLE_F64BE},
112 };
113
114 const std::vector<std::pair<AudioChannelLayout, std::string_view>> g_ChannelLayoutToString = {
115 {AudioChannelLayout::UNKNOWN, "UNKNOW"},
116 {AudioChannelLayout::MONO, "MONO"},
117 {AudioChannelLayout::STEREO, "STEREO"},
118 {AudioChannelLayout::CH_2POINT1, "2POINT1"},
119 {AudioChannelLayout::CH_2_1, "CH_2_1"},
120 {AudioChannelLayout::SURROUND, "SURROUND"},
121 {AudioChannelLayout::CH_3POINT1, "3POINT1"},
122 {AudioChannelLayout::CH_4POINT0, "4POINT0"},
123 {AudioChannelLayout::CH_4POINT1, "4POINT1"},
124 {AudioChannelLayout::CH_2_2, "CH_2_2"},
125 {AudioChannelLayout::QUAD, "QUAD"},
126 {AudioChannelLayout::CH_5POINT0, "5POINT0"},
127 {AudioChannelLayout::CH_5POINT1, "5POINT1"},
128 {AudioChannelLayout::CH_5POINT0_BACK, "5POINT0_BACK"},
129 {AudioChannelLayout::CH_5POINT1_BACK, "5POINT1_BACK"},
130 {AudioChannelLayout::CH_6POINT0, "6POINT0"},
131 {AudioChannelLayout::CH_6POINT0_FRONT, "6POINT0_FRONT"},
132 {AudioChannelLayout::HEXAGONAL, "HEXAGONAL"},
133 {AudioChannelLayout::CH_6POINT1, "6POINT1"},
134 {AudioChannelLayout::CH_6POINT1_BACK, "6POINT1_BACK"},
135 {AudioChannelLayout::CH_6POINT1_FRONT, "6POINT1_FRONT"},
136 {AudioChannelLayout::CH_7POINT0, "7POINT0"},
137 {AudioChannelLayout::CH_7POINT0_FRONT, "7POINT0_FRONT"},
138 {AudioChannelLayout::CH_7POINT1, "7POINT1"},
139 {AudioChannelLayout::CH_7POINT1_WIDE, "7POINT1_WIDE"},
140 {AudioChannelLayout::CH_7POINT1_WIDE_BACK, "7POINT1_WIDE_BACK"},
141 {AudioChannelLayout::CH_3POINT1POINT2, "CH_3POINT1POINT2"},
142 {AudioChannelLayout::CH_5POINT1POINT2, "CH_5POINT1POINT2"},
143 {AudioChannelLayout::CH_5POINT1POINT4, "CH_5POINT1POINT4"},
144 {AudioChannelLayout::CH_7POINT1POINT2, "CH_7POINT1POINT2"},
145 {AudioChannelLayout::CH_7POINT1POINT4, "CH_7POINT1POINT4"},
146 {AudioChannelLayout::CH_9POINT1POINT4, "CH_9POINT1POINT4"},
147 {AudioChannelLayout::CH_9POINT1POINT6, "CH_9POINT1POINT6"},
148 {AudioChannelLayout::CH_10POINT2, "CH_10POINT2"},
149 {AudioChannelLayout::CH_22POINT2, "CH_22POINT2"},
150 {AudioChannelLayout::OCTAGONAL, "OCTAGONAL"},
151 {AudioChannelLayout::HEXADECAGONAL, "HEXADECAGONAL"},
152 {AudioChannelLayout::STEREO_DOWNMIX, "STEREO_DOWNMIX"},
153 {AudioChannelLayout::CH_2POINT0POINT2, "CH_2POINT0POINT2"},
154 {AudioChannelLayout::CH_2POINT1POINT2, "CH_2POINT1POINT2"},
155 {AudioChannelLayout::CH_3POINT0POINT2, "CH_3POINT0POINT2"},
156 {AudioChannelLayout::HOA_ORDER1_ACN_N3D, "HOA_ORDER1_ACN_N3D"},
157 {AudioChannelLayout::HOA_ORDER1_ACN_SN3D, "HOA_ORDER1_ACN_SN3D"},
158 {AudioChannelLayout::HOA_ORDER1_FUMA, "HOA_ORDER1_FUMA"},
159 {AudioChannelLayout::HOA_ORDER2_ACN_N3D, "HOA_ORDER2_ACN_N3D"},
160 {AudioChannelLayout::HOA_ORDER2_ACN_SN3D, "HOA_ORDER2_ACN_SN3D"},
161 {AudioChannelLayout::HOA_ORDER2_FUMA, "HOA_ORDER2_FUMA"},
162 {AudioChannelLayout::HOA_ORDER3_ACN_N3D, "HOA_ORDER3_ACN_N3D"},
163 {AudioChannelLayout::HOA_ORDER3_ACN_SN3D, "HOA_ORDER3_ACN_SN3D"},
164 {AudioChannelLayout::HOA_ORDER3_FUMA, "HOA_ORDER3_FUMA"},
165 };
166
167 const std::vector<std::pair<AVColorPrimaries, ColorPrimary>> g_pFfColorPrimariesMap = {
168 {AVColorPrimaries::AVCOL_PRI_BT709, ColorPrimary::BT709},
169 {AVColorPrimaries::AVCOL_PRI_UNSPECIFIED, ColorPrimary::UNSPECIFIED},
170 {AVColorPrimaries::AVCOL_PRI_BT470M, ColorPrimary::BT470_M},
171 {AVColorPrimaries::AVCOL_PRI_BT470BG, ColorPrimary::BT601_625},
172 {AVColorPrimaries::AVCOL_PRI_SMPTE170M, ColorPrimary::BT601_525},
173 {AVColorPrimaries::AVCOL_PRI_SMPTE240M, ColorPrimary::SMPTE_ST240},
174 {AVColorPrimaries::AVCOL_PRI_FILM, ColorPrimary::GENERIC_FILM},
175 {AVColorPrimaries::AVCOL_PRI_BT2020, ColorPrimary::BT2020},
176 {AVColorPrimaries::AVCOL_PRI_SMPTE428, ColorPrimary::SMPTE_ST428},
177 {AVColorPrimaries::AVCOL_PRI_SMPTEST428_1, ColorPrimary::SMPTE_ST428},
178 {AVColorPrimaries::AVCOL_PRI_SMPTE431, ColorPrimary::P3DCI},
179 {AVColorPrimaries::AVCOL_PRI_SMPTE432, ColorPrimary::P3D65},
180 };
181
182 const std::vector<std::pair<AVColorTransferCharacteristic, TransferCharacteristic>> g_pFfTransferCharacteristicMap = {
183 {AVColorTransferCharacteristic::AVCOL_TRC_BT709, TransferCharacteristic::BT709},
184 {AVColorTransferCharacteristic::AVCOL_TRC_UNSPECIFIED, TransferCharacteristic::UNSPECIFIED},
185 {AVColorTransferCharacteristic::AVCOL_TRC_GAMMA22, TransferCharacteristic::GAMMA_2_2},
186 {AVColorTransferCharacteristic::AVCOL_TRC_GAMMA28, TransferCharacteristic::GAMMA_2_8},
187 {AVColorTransferCharacteristic::AVCOL_TRC_SMPTE170M, TransferCharacteristic::BT601},
188 {AVColorTransferCharacteristic::AVCOL_TRC_SMPTE240M, TransferCharacteristic::SMPTE_ST240},
189 {AVColorTransferCharacteristic::AVCOL_TRC_LINEAR, TransferCharacteristic::LINEAR},
190 {AVColorTransferCharacteristic::AVCOL_TRC_LOG, TransferCharacteristic::LOG},
191 {AVColorTransferCharacteristic::AVCOL_TRC_LOG_SQRT, TransferCharacteristic::LOG_SQRT},
192 {AVColorTransferCharacteristic::AVCOL_TRC_IEC61966_2_4, TransferCharacteristic::IEC_61966_2_4},
193 {AVColorTransferCharacteristic::AVCOL_TRC_BT1361_ECG, TransferCharacteristic::BT1361},
194 {AVColorTransferCharacteristic::AVCOL_TRC_IEC61966_2_1, TransferCharacteristic::IEC_61966_2_1},
195 {AVColorTransferCharacteristic::AVCOL_TRC_BT2020_10, TransferCharacteristic::BT2020_10BIT},
196 {AVColorTransferCharacteristic::AVCOL_TRC_BT2020_12, TransferCharacteristic::BT2020_12BIT},
197 {AVColorTransferCharacteristic::AVCOL_TRC_SMPTE2084, TransferCharacteristic::PQ},
198 {AVColorTransferCharacteristic::AVCOL_TRC_SMPTEST2084, TransferCharacteristic::PQ},
199 {AVColorTransferCharacteristic::AVCOL_TRC_SMPTE428, TransferCharacteristic::SMPTE_ST428},
200 {AVColorTransferCharacteristic::AVCOL_TRC_SMPTEST428_1, TransferCharacteristic::SMPTE_ST428},
201 {AVColorTransferCharacteristic::AVCOL_TRC_ARIB_STD_B67, TransferCharacteristic::HLG},
202 };
203
204 const std::vector<std::pair<AVColorSpace, MatrixCoefficient>> g_pFfMatrixCoefficientMap = {
205 {AVColorSpace::AVCOL_SPC_RGB, MatrixCoefficient::IDENTITY},
206 {AVColorSpace::AVCOL_SPC_BT709, MatrixCoefficient::BT709},
207 {AVColorSpace::AVCOL_SPC_UNSPECIFIED, MatrixCoefficient::UNSPECIFIED},
208 {AVColorSpace::AVCOL_SPC_FCC, MatrixCoefficient::FCC},
209 {AVColorSpace::AVCOL_SPC_BT470BG, MatrixCoefficient::BT601_625},
210 {AVColorSpace::AVCOL_SPC_SMPTE170M, MatrixCoefficient::BT601_525},
211 {AVColorSpace::AVCOL_SPC_SMPTE240M, MatrixCoefficient::SMPTE_ST240},
212 {AVColorSpace::AVCOL_SPC_YCGCO, MatrixCoefficient::YCGCO},
213 {AVColorSpace::AVCOL_SPC_YCOCG, MatrixCoefficient::YCGCO},
214 {AVColorSpace::AVCOL_SPC_BT2020_NCL, MatrixCoefficient::BT2020_NCL},
215 {AVColorSpace::AVCOL_SPC_BT2020_CL, MatrixCoefficient::BT2020_CL},
216 {AVColorSpace::AVCOL_SPC_SMPTE2085, MatrixCoefficient::SMPTE_ST2085},
217 {AVColorSpace::AVCOL_SPC_CHROMA_DERIVED_NCL, MatrixCoefficient::CHROMATICITY_NCL},
218 {AVColorSpace::AVCOL_SPC_CHROMA_DERIVED_CL, MatrixCoefficient::CHROMATICITY_CL},
219 {AVColorSpace::AVCOL_SPC_ICTCP, MatrixCoefficient::ICTCP},
220 };
221
222 const std::vector<std::pair<AVColorRange, int>> g_pFfColorRangeMap = {
223 {AVColorRange::AVCOL_RANGE_MPEG, 0},
224 {AVColorRange::AVCOL_RANGE_JPEG, 1},
225 };
226
227 const std::vector<std::pair<AVChromaLocation, ChromaLocation>> g_pFfChromaLocationMap = {
228 {AVChromaLocation::AVCHROMA_LOC_UNSPECIFIED, ChromaLocation::UNSPECIFIED},
229 {AVChromaLocation::AVCHROMA_LOC_LEFT, ChromaLocation::LEFT},
230 {AVChromaLocation::AVCHROMA_LOC_CENTER, ChromaLocation::CENTER},
231 {AVChromaLocation::AVCHROMA_LOC_TOPLEFT, ChromaLocation::TOPLEFT},
232 {AVChromaLocation::AVCHROMA_LOC_TOP, ChromaLocation::TOP},
233 {AVChromaLocation::AVCHROMA_LOC_BOTTOMLEFT, ChromaLocation::BOTTOMLEFT},
234 {AVChromaLocation::AVCHROMA_LOC_BOTTOM, ChromaLocation::BOTTOM},
235 };
236
237 const std::vector<std::pair<int, HEVCProfile>> g_pFfHEVCProfileMap = {
238 {FF_PROFILE_HEVC_MAIN, HEVCProfile::HEVC_PROFILE_MAIN},
239 {FF_PROFILE_HEVC_MAIN_10, HEVCProfile::HEVC_PROFILE_MAIN_10},
240 {FF_PROFILE_HEVC_MAIN_STILL_PICTURE, HEVCProfile::HEVC_PROFILE_MAIN_STILL},
241 };
242
243 const std::vector<std::pair<int, HEVCLevel>> g_pFfHEVCLevelMap = {
244 {30, HEVCLevel::HEVC_LEVEL_1}, {60, HEVCLevel::HEVC_LEVEL_2}, {63, HEVCLevel::HEVC_LEVEL_21},
245 {90, HEVCLevel::HEVC_LEVEL_3}, {93, HEVCLevel::HEVC_LEVEL_31}, {120, HEVCLevel::HEVC_LEVEL_4},
246 {123, HEVCLevel::HEVC_LEVEL_41}, {150, HEVCLevel::HEVC_LEVEL_5}, {153, HEVCLevel::HEVC_LEVEL_51},
247 {156, HEVCLevel::HEVC_LEVEL_52}, {180, HEVCLevel::HEVC_LEVEL_6}, {183, HEVCLevel::HEVC_LEVEL_61},
248 {186, HEVCLevel::HEVC_LEVEL_62},
249 };
250
ConvertFFMpegToOHHEVCLevel(int ffHEVCLevel)251 HEVCLevel FFMpegConverter::ConvertFFMpegToOHHEVCLevel(int ffHEVCLevel)
252 {
253 auto ite = std::find_if(g_pFfHEVCLevelMap.begin(), g_pFfHEVCLevelMap.end(),
254 [&ffHEVCLevel](const auto &item) -> bool { return item.first == ffHEVCLevel; });
255 if (ite == g_pFfHEVCLevelMap.end()) {
256 MEDIA_LOG_W("Failed: " PUBLIC_LOG_D32, ffHEVCLevel);
257 return HEVCLevel::HEVC_LEVEL_UNKNOW;
258 }
259 return ite->second;
260 }
261
ConvertFFMpegToOHHEVCProfile(int ffHEVCProfile)262 HEVCProfile FFMpegConverter::ConvertFFMpegToOHHEVCProfile(int ffHEVCProfile)
263 {
264 auto ite = std::find_if(g_pFfHEVCProfileMap.begin(), g_pFfHEVCProfileMap.end(),
265 [&ffHEVCProfile](const auto &item) -> bool { return item.first == ffHEVCProfile; });
266 if (ite == g_pFfHEVCProfileMap.end()) {
267 MEDIA_LOG_W("Failed: " PUBLIC_LOG_D32, ffHEVCProfile);
268 return HEVCProfile::HEVC_PROFILE_UNKNOW;
269 }
270 return ite->second;
271 }
272
ConvertFFMpegToOHColorPrimaries(AVColorPrimaries ffColorPrimaries)273 ColorPrimary FFMpegConverter::ConvertFFMpegToOHColorPrimaries(AVColorPrimaries ffColorPrimaries)
274 {
275 auto ite = std::find_if(g_pFfColorPrimariesMap.begin(), g_pFfColorPrimariesMap.end(),
276 [&ffColorPrimaries](const auto &item) -> bool { return item.first == ffColorPrimaries; });
277 CHECK_AND_RETURN_RET_LOGW(ite != g_pFfColorPrimariesMap.end(), ColorPrimary::UNSPECIFIED,
278 "Failed: " PUBLIC_LOG_D32, static_cast<int32_t>(ffColorPrimaries));
279 return ite->second;
280 }
281
ConvertFFMpegToOHColorTrans(AVColorTransferCharacteristic ffColorTrans)282 TransferCharacteristic FFMpegConverter::ConvertFFMpegToOHColorTrans(AVColorTransferCharacteristic ffColorTrans)
283 {
284 auto ite = std::find_if(g_pFfTransferCharacteristicMap.begin(), g_pFfTransferCharacteristicMap.end(),
285 [&ffColorTrans](const auto &item) -> bool { return item.first == ffColorTrans; });
286 CHECK_AND_RETURN_RET_LOGW(ite != g_pFfTransferCharacteristicMap.end(), TransferCharacteristic::UNSPECIFIED,
287 "Failed: " PUBLIC_LOG_D32, static_cast<int32_t>(ffColorTrans));
288 return ite->second;
289 }
290
ConvertFFMpegToOHColorMatrix(AVColorSpace ffColorSpace)291 MatrixCoefficient FFMpegConverter::ConvertFFMpegToOHColorMatrix(AVColorSpace ffColorSpace)
292 {
293 auto ite = std::find_if(g_pFfMatrixCoefficientMap.begin(), g_pFfMatrixCoefficientMap.end(),
294 [&ffColorSpace](const auto &item) -> bool { return item.first == ffColorSpace; });
295 CHECK_AND_RETURN_RET_LOGW(ite != g_pFfMatrixCoefficientMap.end(), MatrixCoefficient::UNSPECIFIED,
296 "Failed: " PUBLIC_LOG_D32, static_cast<int32_t>(ffColorSpace));
297 return ite->second;
298 }
299
ConvertFFMpegToOHColorRange(AVColorRange ffColorRange)300 int FFMpegConverter::ConvertFFMpegToOHColorRange(AVColorRange ffColorRange)
301 {
302 auto ite = std::find_if(g_pFfColorRangeMap.begin(), g_pFfColorRangeMap.end(),
303 [&ffColorRange](const auto &item) -> bool { return item.first == ffColorRange; });
304 if (ite == g_pFfColorRangeMap.end()) {
305 MEDIA_LOG_W("Failed: " PUBLIC_LOG_D32, static_cast<int32_t>(ffColorRange));
306 return 0;
307 }
308 return ite->second;
309 }
310
ConvertFFMpegToOHChromaLocation(AVChromaLocation ffChromaLocation)311 ChromaLocation FFMpegConverter::ConvertFFMpegToOHChromaLocation(AVChromaLocation ffChromaLocation)
312 {
313 auto ite = std::find_if(g_pFfChromaLocationMap.begin(), g_pFfChromaLocationMap.end(),
314 [&ffChromaLocation](const auto &item) -> bool { return item.first == ffChromaLocation; });
315 CHECK_AND_RETURN_RET_LOGW(ite != g_pFfChromaLocationMap.end(), ChromaLocation::UNSPECIFIED,
316 "Failed: " PUBLIC_LOG_D32, static_cast<int32_t>(ffChromaLocation));
317 return ite->second;
318 }
319
ConvertFFMpegAVCodecIdToOHAudioFormat(AVCodecID codecId)320 AudioSampleFormat FFMpegConverter::ConvertFFMpegAVCodecIdToOHAudioFormat(AVCodecID codecId)
321 {
322 auto ite = std::find_if(g_pFfCodeIDToSampleFmtMap.begin(), g_pFfCodeIDToSampleFmtMap.end(),
323 [&codecId](const auto &item) -> bool { return item.first == codecId; });
324 if (ite == g_pFfCodeIDToSampleFmtMap.end()) {
325 MEDIA_LOG_W("Failed: " PUBLIC_LOG_D32, static_cast<int32_t>(codecId));
326 return AudioSampleFormat::INVALID_WIDTH;
327 }
328 return ite->second;
329 }
330
ConvertFFMpegToOHAudioFormat(AVSampleFormat ffSampleFormat)331 AudioSampleFormat FFMpegConverter::ConvertFFMpegToOHAudioFormat(AVSampleFormat ffSampleFormat)
332 {
333 auto ite = std::find_if(g_pFfSampleFmtMap.begin(), g_pFfSampleFmtMap.end(),
334 [&ffSampleFormat](const auto &item) -> bool { return item.first == ffSampleFormat; });
335 if (ite == g_pFfSampleFmtMap.end()) {
336 MEDIA_LOG_W("Failed: " PUBLIC_LOG_D32, static_cast<int32_t>(ffSampleFormat));
337 return AudioSampleFormat::INVALID_WIDTH;
338 }
339 return ite->second;
340 }
341
ConvertOHAudioFormatToFFMpeg(AudioSampleFormat sampleFormat)342 AVSampleFormat FFMpegConverter::ConvertOHAudioFormatToFFMpeg(AudioSampleFormat sampleFormat)
343 {
344 auto ite = std::find_if(g_pFfSampleFmtMap.begin(), g_pFfSampleFmtMap.end(),
345 [&sampleFormat](const auto &item) -> bool { return item.second == sampleFormat; });
346 CHECK_AND_RETURN_RET_LOGW(ite != g_pFfSampleFmtMap.end(), AVSampleFormat::AV_SAMPLE_FMT_NONE,
347 "Failed: " PUBLIC_LOG_D32, static_cast<int32_t>(sampleFormat));
348 return ite->first;
349 }
350
GetDefaultChannelLayout(int channels)351 AudioChannelLayout FFMpegConverter::GetDefaultChannelLayout(int channels)
352 {
353 AudioChannelLayout layout = AudioChannelLayout::MONO;
354 auto ite = std::find_if(g_channelLayoutDefaultMap.begin(), g_channelLayoutDefaultMap.end(),
355 [&channels](const auto &item) -> bool { return item.first == channels; });
356 if (ite != g_channelLayoutDefaultMap.end()) {
357 layout = ite->second;
358 }
359 MEDIA_LOG_W("Default: " PUBLIC_LOG_S, ConvertOHAudioChannelLayoutToString(layout).data());
360 return layout;
361 }
362
ConvertFFToOHAudioChannelLayoutV2(uint64_t ffChannelLayout,int channels)363 AudioChannelLayout FFMpegConverter::ConvertFFToOHAudioChannelLayoutV2(uint64_t ffChannelLayout, int channels)
364 {
365 auto ite = std::find_if(g_toFFMPEGChannelLayout.begin(), g_toFFMPEGChannelLayout.end(),
366 [&ffChannelLayout](const auto &item) -> bool { return item.second == ffChannelLayout; });
367 if (ite == g_toFFMPEGChannelLayout.end()) {
368 MEDIA_LOG_W("Failed: " PUBLIC_LOG_U64, ffChannelLayout);
369 return GetDefaultChannelLayout(channels);
370 }
371 return ite->first;
372 }
373
ConvertOHAudioChannelLayoutToFFMpeg(AudioChannelLayout channelLayout)374 uint64_t FFMpegConverter::ConvertOHAudioChannelLayoutToFFMpeg(AudioChannelLayout channelLayout)
375 {
376 auto ite = std::find_if(g_toFFMPEGChannelLayout.begin(), g_toFFMPEGChannelLayout.end(),
377 [&channelLayout](const auto &item) -> bool { return item.first == channelLayout; });
378 if (ite == g_toFFMPEGChannelLayout.end()) {
379 MEDIA_LOG_W("Failed: " PUBLIC_LOG_D32, static_cast<int32_t>(channelLayout));
380 return AV_CH_LAYOUT_NATIVE;
381 }
382 return ite->second;
383 }
384
ConvertOHAudioChannelLayoutToString(AudioChannelLayout layout)385 std::string_view FFMpegConverter::ConvertOHAudioChannelLayoutToString(AudioChannelLayout layout)
386 {
387 auto ite = std::find_if(g_ChannelLayoutToString.begin(), g_ChannelLayoutToString.end(),
388 [&layout](const auto &item) -> bool { return item.first == layout; });
389 CHECK_AND_RETURN_RET_LOGW(ite != g_ChannelLayoutToString.end(), g_ChannelLayoutToString[0].second,
390 "Failed: " PUBLIC_LOG_D32, static_cast<int32_t>(layout));
391 return ite->second;
392 }
393
ConvertAudioVividToOHAudioChannelLayout(uint64_t ffChannelLayout,int channels)394 AudioChannelLayout FFMpegConverter::ConvertAudioVividToOHAudioChannelLayout(uint64_t ffChannelLayout, int channels)
395 {
396 auto ite = std::find_if(g_audioVividChannelLayoutMap.begin(), g_audioVividChannelLayoutMap.end(),
397 [&ffChannelLayout](const auto &item) -> bool {
398 return static_cast<uint64_t>(item.first) == ffChannelLayout;
399 });
400 CHECK_AND_RETURN_RET_LOGW((ite != g_audioVividChannelLayoutMap.end()) && (ite -> second == channels),
401 GetDefaultChannelLayout(channels), "Convert channel layout failed: " PUBLIC_LOG_U64,
402 ffChannelLayout);
403 return ite->first;
404 }
405 } // namespace Plugins
406 } // namespace Media
407 } // namespace OHOS