• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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<std::string_view, InnerCodecMimeType> MIME_TO_CODEC_NAME = {
68     {CodecMimeType::VIDEO_H263, CODEC_MIMIE_TYPE_VIDEO_H263},
69     {CodecMimeType::VIDEO_AVC, CODEC_MIMIE_TYPE_VIDEO_AVC},
70     {CodecMimeType::VIDEO_HEVC, CODEC_MIMIE_TYPE_VIDEO_HEVC},
71     {CodecMimeType::VIDEO_MPEG2, CODEC_MIMIE_TYPE_VIDEO_MPEG2},
72     {CodecMimeType::VIDEO_MPEG4, CODEC_MIMIE_TYPE_VIDEO_MPEG4},
73     {CodecMimeType::AUDIO_VORBIS, CODEC_MIMIE_TYPE_AUDIO_VORBIS},
74     {CodecMimeType::AUDIO_MPEG, CODEC_MIMIE_TYPE_AUDIO_MPEG},
75     {CodecMimeType::AUDIO_AAC, CODEC_MIMIE_TYPE_AUDIO_AAC},
76     {CodecMimeType::AUDIO_FLAC, CODEC_MIMIE_TYPE_AUDIO_FLAC},
77     {CodecMimeType::AUDIO_OPUS, CODEC_MIMIE_TYPE_AUDIO_OPUS},
78 };
79 
PixelFormatToGst(VideoPixelFormat pixel)80 std::string PixelFormatToGst(VideoPixelFormat pixel)
81 {
82     if (PIXEL_TO_STRING.count(pixel) != 0) {
83         return PIXEL_TO_STRING.at(pixel);
84     }
85     return "Invalid";
86 }
87 
MPEG4ProfileToGst(MPEG4Profile profile)88 std::string MPEG4ProfileToGst(MPEG4Profile profile)
89 {
90     if (MPEG4_PROFILE_TO_STRING.count(profile) != 0) {
91         return MPEG4_PROFILE_TO_STRING.at(profile);
92     }
93     return "Invalid";
94 }
95 
AVCProfileToGst(AVCProfile profile)96 std::string AVCProfileToGst(AVCProfile profile)
97 {
98     if (AVC_PROFILE_TO_STRING.count(profile) != 0) {
99         return AVC_PROFILE_TO_STRING.at(profile);
100     }
101     return "Invalid";
102 }
103 
RawAudioFormatToGst(AudioStandard::AudioSampleFormat format)104 std::string RawAudioFormatToGst(AudioStandard::AudioSampleFormat format)
105 {
106     if (PCM_TO_STRING.count(format) != 0) {
107         return PCM_TO_STRING.at(format);
108     }
109     return "Invalid";
110 }
111 
MapCodecMime(const std::string & mime,InnerCodecMimeType & name)112 int32_t MapCodecMime(const std::string &mime, InnerCodecMimeType &name)
113 {
114     if (MIME_TO_CODEC_NAME.count(mime) != 0) {
115         name =  MIME_TO_CODEC_NAME.at(mime);
116         return MSERR_OK;
117     }
118     return MSERR_INVALID_VAL;
119 }
120 
CapsToFormat(GstCaps * caps,Format & format)121 int32_t CapsToFormat(GstCaps *caps, Format &format)
122 {
123     GstStructure *structure = gst_caps_get_structure(caps, 0);
124     if (structure == nullptr) {
125         return MSERR_UNKNOWN;
126     }
127     auto mediaType = gst_structure_get_name(structure);
128     bool isVideo = g_str_has_prefix(mediaType, "video/");
129 
130     gint ret = 0;
131     if (isVideo) {
132         (void)gst_structure_get(structure, "width", G_TYPE_INT, &ret, nullptr);
133         (void)format.PutIntValue("width", ret);
134         (void)gst_structure_get(structure, "height", G_TYPE_INT, &ret, nullptr);
135         (void)format.PutIntValue("height", ret);
136     } else {
137         (void)gst_structure_get(structure, "rate", G_TYPE_INT, &ret, nullptr);
138         (void)format.PutIntValue("sample_rate", ret);
139         (void)gst_structure_get(structure, "channels", G_TYPE_INT, &ret, nullptr);
140         (void)format.PutIntValue("channel_count", ret);
141     }
142     return MSERR_OK;
143 }
144 
PixelBufferSize(VideoPixelFormat pixel,uint32_t width,uint32_t height,uint32_t alignment)145 uint32_t PixelBufferSize(VideoPixelFormat pixel, uint32_t width, uint32_t height, uint32_t alignment)
146 {
147     uint32_t size = 0;
148     if (width == 0 || height == 0 || alignment == 0) {
149         return size;
150     }
151 
152     switch (pixel) {
153         case YUVI420:
154             // fall-through
155         case NV12:
156             // fall-through
157         case SURFACE_FORMAT:
158             // fall-through
159         case NV21:
160             size = width * height * 3 / 2;
161             break;
162         case RGBA:
163             size = width * height * 4;
164         default:
165             break;
166     }
167 
168     if (size > 0) {
169         size = (size + alignment - 1) & ~(alignment - 1);
170     }
171 
172     return size;
173 }
174 
CompressedBufSize(uint32_t width,uint32_t height,bool isEncoder,InnerCodecMimeType type)175 uint32_t CompressedBufSize(uint32_t width, uint32_t height, bool isEncoder, InnerCodecMimeType type)
176 {
177     if (width == 0 || height == 0) {
178         return 0;
179     }
180 
181     uint32_t compressRatio = 7;
182 
183     if (isEncoder && type == CODEC_MIMIE_TYPE_VIDEO_MPEG4) {
184         compressRatio = 3;
185     }
186 
187     constexpr uint32_t maxSize = 3150000; // 3MB
188     if ((UINT32_MAX / width) <= (height / compressRatio)) {
189         return maxSize;
190     }
191 
192     return height / compressRatio * width;
193 }
194 } // namespace Media
195 } // namespace OHOS