1 /*
2 * Copyright (C) 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 #include "codec_common.h"
17 #include "media_errors.h"
18
19 namespace OHOS {
20 namespace Media {
21 const std::map<VideoPixelFormat, std::string> PIXEL_TO_STRING = {
22 {YUVI420, "I420"},
23 {NV12, "NV12"},
24 {NV21, "NV21"},
25 {SURFACE_FORMAT, "NV21"},
26 {RGBA, "RGBA"},
27 };
28
29 const std::map<AudioStandard::AudioSampleFormat, std::string> PCM_TO_STRING = {
30 {AudioStandard::SAMPLE_U8, "U8"},
31 {AudioStandard::SAMPLE_S16LE, "S16LE"},
32 {AudioStandard::SAMPLE_S24LE, "S24LE"},
33 {AudioStandard::SAMPLE_S32LE, "S32LE"},
34 };
35
36 const std::map<MPEG4Profile, std::string> MPEG4_PROFILE_TO_STRING = {
37 {MPEG4_PROFILE_ADVANCED_CODING, "advanced-coding-efficiency"},
38 {MPEG4_PROFILE_ADVANCED_CORE, "advanced-core"},
39 {MPEG4_PROFILE_ADVANCED_REAL_TIME, "advanced-real-time"},
40 {MPEG4_PROFILE_ADVANCED_SCALABLE, "advanced-scalable-texture"},
41 {MPEG4_PROFILE_ADVANCED_SIMPLE, "advanced-simple"},
42 {MPEG4_PROFILE_BASIC_ANIMATED, "basic-animated-texture"},
43 {MPEG4_PROFILE_CORE, "core"},
44 {MPEG4_PROFILE_CORE_SCALABLE, "core-scalable"},
45 {MPEG4_PROFILE_HYBRID, "hybrid"},
46 {MPEG4_PROFILE_MAIN, "main"},
47 {MPEG4_PROFILE_NBIT, "n-bit"},
48 {MPEG4_PROFILE_SCALABLE_TEXTURE, "scalable"},
49 {MPEG4_PROFILE_SIMPLE, "simple"},
50 {MPEG4_PROFILE_SIMPLE_FBA, "simple-fba"},
51 {MPEG4_PROFILE_SIMPLE_FACE, "simple-face"},
52 {MPEG4_PROFILE_SIMPLE_SCALABLE, "simple-scalable"},
53 };
54
55 const std::map<AVCProfile, std::string> AVC_PROFILE_TO_STRING = {
56 {AVC_PROFILE_BASELINE, "baseline"},
57 {AVC_PROFILE_CONSTRAINED_BASELINE, "constrained-baseline"},
58 {AVC_PROFILE_CONSTRAINED_HIGH, "constrained-high"},
59 {AVC_PROFILE_EXTENDED, "extended"},
60 {AVC_PROFILE_HIGH, "high"},
61 {AVC_PROFILE_HIGH_10, "high-10"},
62 {AVC_PROFILE_HIGH_422, "high-4:2:2"},
63 {AVC_PROFILE_HIGH_444, "high-4:4:4"},
64 {AVC_PROFILE_MAIN, "main"},
65 };
66
67 const std::map<HEVCProfile, std::string> HEVC_PROFILE_TO_STRING = {
68 {HEVC_PROFILE_MAIN, "main"},
69 {HEVC_PROFILE_MAIN_10, "main-10"},
70 {HEVC_PROFILE_MAIN_STILL, "main-still-picture"},
71 };
72
73 const std::map<std::string_view, InnerCodecMimeType> MIME_TO_CODEC_NAME = {
74 {CodecMimeType::VIDEO_H263, CODEC_MIME_TYPE_VIDEO_H263},
75 {CodecMimeType::VIDEO_AVC, CODEC_MIME_TYPE_VIDEO_AVC},
76 {CodecMimeType::VIDEO_HEVC, CODEC_MIME_TYPE_VIDEO_HEVC},
77 {CodecMimeType::VIDEO_MPEG2, CODEC_MIME_TYPE_VIDEO_MPEG2},
78 {CodecMimeType::VIDEO_MPEG4, CODEC_MIME_TYPE_VIDEO_MPEG4},
79 {CodecMimeType::AUDIO_VORBIS, CODEC_MIME_TYPE_AUDIO_VORBIS},
80 {CodecMimeType::AUDIO_MPEG, CODEC_MIME_TYPE_AUDIO_MPEG},
81 {CodecMimeType::AUDIO_AAC, CODEC_MIME_TYPE_AUDIO_AAC},
82 {CodecMimeType::AUDIO_FLAC, CODEC_MIME_TYPE_AUDIO_FLAC},
83 {CodecMimeType::AUDIO_OPUS, CODEC_MIME_TYPE_AUDIO_OPUS},
84 };
85
PixelFormatToGst(VideoPixelFormat pixel)86 std::string PixelFormatToGst(VideoPixelFormat pixel)
87 {
88 if (PIXEL_TO_STRING.count(pixel) != 0) {
89 return PIXEL_TO_STRING.at(pixel);
90 }
91 return "Invalid";
92 }
93
MPEG4ProfileToGst(MPEG4Profile profile)94 std::string MPEG4ProfileToGst(MPEG4Profile profile)
95 {
96 if (MPEG4_PROFILE_TO_STRING.count(profile) != 0) {
97 return MPEG4_PROFILE_TO_STRING.at(profile);
98 }
99 return "Invalid";
100 }
101
AVCProfileToGst(AVCProfile profile)102 std::string AVCProfileToGst(AVCProfile profile)
103 {
104 if (AVC_PROFILE_TO_STRING.count(profile) != 0) {
105 return AVC_PROFILE_TO_STRING.at(profile);
106 }
107 return "Invalid";
108 }
109
HEVCProfileToGst(HEVCProfile profile)110 std::string HEVCProfileToGst(HEVCProfile profile)
111 {
112 if (HEVC_PROFILE_TO_STRING.count(profile) != 0) {
113 return HEVC_PROFILE_TO_STRING.at(profile);
114 }
115 return "Invalid";
116 }
117
RawAudioFormatToGst(AudioStandard::AudioSampleFormat format)118 std::string RawAudioFormatToGst(AudioStandard::AudioSampleFormat format)
119 {
120 if (PCM_TO_STRING.count(format) != 0) {
121 return PCM_TO_STRING.at(format);
122 }
123 return "Invalid";
124 }
125
MapCodecMime(const std::string & mime,InnerCodecMimeType & name)126 int32_t MapCodecMime(const std::string &mime, InnerCodecMimeType &name)
127 {
128 if (MIME_TO_CODEC_NAME.count(mime) != 0) {
129 name = MIME_TO_CODEC_NAME.at(mime);
130 return MSERR_OK;
131 }
132 return MSERR_INVALID_VAL;
133 }
134
CapsToFormat(GstCaps * caps,Format & format)135 int32_t CapsToFormat(GstCaps *caps, Format &format)
136 {
137 GstStructure *structure = gst_caps_get_structure(caps, 0);
138 if (structure == nullptr) {
139 return MSERR_UNKNOWN;
140 }
141 auto mediaType = gst_structure_get_name(structure);
142 bool isVideo = g_str_has_prefix(mediaType, "video/");
143
144 gint ret = 0;
145 if (isVideo) {
146 (void)gst_structure_get(structure, "width", G_TYPE_INT, &ret, nullptr);
147 (void)format.PutIntValue("width", ret);
148 (void)gst_structure_get(structure, "height", G_TYPE_INT, &ret, nullptr);
149 (void)format.PutIntValue("height", ret);
150 } else {
151 (void)gst_structure_get(structure, "rate", G_TYPE_INT, &ret, nullptr);
152 (void)format.PutIntValue("sample_rate", ret);
153 (void)gst_structure_get(structure, "channels", G_TYPE_INT, &ret, nullptr);
154 (void)format.PutIntValue("channel_count", ret);
155 }
156 return MSERR_OK;
157 }
158
PixelBufferSize(VideoPixelFormat pixel,uint32_t width,uint32_t height,uint32_t alignment)159 uint32_t PixelBufferSize(VideoPixelFormat pixel, uint32_t width, uint32_t height, uint32_t alignment)
160 {
161 uint32_t size = 0;
162 if (width == 0 || height == 0 || alignment == 0) {
163 return size;
164 }
165
166 switch (pixel) {
167 case YUVI420:
168 // fall-through
169 case NV12:
170 // fall-through
171 case SURFACE_FORMAT:
172 // fall-through
173 case NV21:
174 size = width * height * 3 / 2;
175 break;
176 case RGBA:
177 size = width * height * 4;
178 break;
179 default:
180 break;
181 }
182
183 if (size > 0) {
184 size = (size + alignment - 1) & ~(alignment - 1);
185 }
186
187 return size;
188 }
189 } // namespace Media
190 } // namespace OHOS