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 #include "native_avformat.h"
17
18 #include "securec.h"
19 #include "native_avmagic.h"
20 #include "avcodec_log.h"
21
22 namespace {
23 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "OH_AVFormat"};
24 constexpr uint32_t MAX_STRING_LENGTH = 256;
25 constexpr uint32_t MAX_DUMP_LENGTH = 1024;
26 }
27
28 using namespace OHOS::MediaAVCodec;
29
OH_AVFormat()30 OH_AVFormat::OH_AVFormat()
31 : AVObjectMagic(AVMagic::AVCODEC_MAGIC_FORMAT)
32 {
33 }
34
OH_AVFormat(const Format & fmt)35 OH_AVFormat::OH_AVFormat(const Format &fmt)
36 : AVObjectMagic(AVMagic::AVCODEC_MAGIC_FORMAT), format_(fmt)
37 {
38 }
39
~OH_AVFormat()40 OH_AVFormat::~OH_AVFormat()
41 {
42 if (outString_ != nullptr) {
43 free(outString_);
44 outString_ = nullptr;
45 }
46 if (dumpInfo_ != nullptr) {
47 free(dumpInfo_);
48 dumpInfo_ = nullptr;
49 }
50 }
51
OH_AVFormat_Create(void)52 struct OH_AVFormat *OH_AVFormat_Create(void)
53 {
54 return new(std::nothrow) OH_AVFormat();
55 }
56
OH_AVFormat_CreateAudioFormat(const char * mimeType,int32_t sampleRate,int32_t channelCount)57 struct OH_AVFormat *OH_AVFormat_CreateAudioFormat(const char *mimeType,
58 int32_t sampleRate,
59 int32_t channelCount)
60 {
61 CHECK_AND_RETURN_RET_LOG(mimeType != nullptr, nullptr, "mimeType is nullptr!");
62 OH_AVFormat *audioFormat = new(std::nothrow) OH_AVFormat();
63 CHECK_AND_RETURN_RET_LOG(audioFormat != nullptr, nullptr, "new format is nullptr!");
64 audioFormat->format_.PutStringValue("codec_mime", mimeType);
65 audioFormat->format_.PutIntValue("sample_rate", sampleRate);
66 audioFormat->format_.PutIntValue("channel_count", channelCount);
67 return audioFormat;
68 }
69
OH_AVFormat_CreateVideoFormat(const char * mimeType,int32_t width,int32_t height)70 struct OH_AVFormat *OH_AVFormat_CreateVideoFormat(const char *mimeType,
71 int32_t width,
72 int32_t height)
73 {
74 CHECK_AND_RETURN_RET_LOG(mimeType != nullptr, nullptr, "mimeType is nullptr!");
75 OH_AVFormat *videoFormat = new(std::nothrow) OH_AVFormat();
76 CHECK_AND_RETURN_RET_LOG(videoFormat != nullptr, nullptr, "new format is nullptr!");
77 videoFormat->format_.PutStringValue("codec_mime", mimeType);
78 videoFormat->format_.PutIntValue("width", width);
79 videoFormat->format_.PutIntValue("height", height);
80 return videoFormat;
81 }
82
OH_AVFormat_Destroy(struct OH_AVFormat * format)83 void OH_AVFormat_Destroy(struct OH_AVFormat *format)
84 {
85 delete format;
86 }
87
OH_AVFormat_Copy(struct OH_AVFormat * to,struct OH_AVFormat * from)88 bool OH_AVFormat_Copy(struct OH_AVFormat *to, struct OH_AVFormat *from)
89 {
90 CHECK_AND_RETURN_RET_LOG(to != nullptr, false, "to format is nullptr!");
91 CHECK_AND_RETURN_RET_LOG(to->magic_ == AVMagic::AVCODEC_MAGIC_FORMAT, false, "magic error!");
92 CHECK_AND_RETURN_RET_LOG(from != nullptr, false, "from format is nullptr!");
93 CHECK_AND_RETURN_RET_LOG(from->magic_ == AVMagic::AVCODEC_MAGIC_FORMAT, false, "magic error!");
94
95 to->format_ = from->format_;
96 return true;
97 }
98
OH_AVFormat_SetIntValue(struct OH_AVFormat * format,const char * key,int32_t value)99 bool OH_AVFormat_SetIntValue(struct OH_AVFormat *format, const char *key, int32_t value)
100 {
101 CHECK_AND_RETURN_RET_LOG(format != nullptr, false, "input format is nullptr!");
102 CHECK_AND_RETURN_RET_LOG(format->magic_ == AVMagic::AVCODEC_MAGIC_FORMAT, false, "magic error!");
103 CHECK_AND_RETURN_RET_LOG(key != nullptr, false, "key is nullptr!");
104
105 return format->format_.PutIntValue(key, value);
106 }
107
OH_AVFormat_SetLongValue(struct OH_AVFormat * format,const char * key,int64_t value)108 bool OH_AVFormat_SetLongValue(struct OH_AVFormat *format, const char *key, int64_t value)
109 {
110 CHECK_AND_RETURN_RET_LOG(format != nullptr, false, "input format is nullptr!");
111 CHECK_AND_RETURN_RET_LOG(format->magic_ == AVMagic::AVCODEC_MAGIC_FORMAT, false, "magic error!");
112 CHECK_AND_RETURN_RET_LOG(key != nullptr, false, "key is nullptr!");
113
114 return format->format_.PutLongValue(key, value);
115 }
116
OH_AVFormat_SetFloatValue(struct OH_AVFormat * format,const char * key,float value)117 bool OH_AVFormat_SetFloatValue(struct OH_AVFormat *format, const char *key, float value)
118 {
119 CHECK_AND_RETURN_RET_LOG(format != nullptr, false, "input format is nullptr!");
120 CHECK_AND_RETURN_RET_LOG(format->magic_ == AVMagic::AVCODEC_MAGIC_FORMAT, false, "magic error!");
121 CHECK_AND_RETURN_RET_LOG(key != nullptr, false, "key is nullptr!");
122
123 return format->format_.PutFloatValue(key, value);
124 }
125
OH_AVFormat_SetDoubleValue(struct OH_AVFormat * format,const char * key,double value)126 bool OH_AVFormat_SetDoubleValue(struct OH_AVFormat *format, const char *key, double value)
127 {
128 CHECK_AND_RETURN_RET_LOG(format != nullptr, false, "input format is nullptr!");
129 CHECK_AND_RETURN_RET_LOG(format->magic_ == AVMagic::AVCODEC_MAGIC_FORMAT, false, "magic error!");
130 CHECK_AND_RETURN_RET_LOG(key != nullptr, false, "key is nullptr!");
131
132 return format->format_.PutDoubleValue(key, value);
133 }
134
OH_AVFormat_SetStringValue(struct OH_AVFormat * format,const char * key,const char * value)135 bool OH_AVFormat_SetStringValue(struct OH_AVFormat *format, const char *key, const char *value)
136 {
137 CHECK_AND_RETURN_RET_LOG(format != nullptr, false, "input format is nullptr!");
138 CHECK_AND_RETURN_RET_LOG(format->magic_ == AVMagic::AVCODEC_MAGIC_FORMAT, false, "magic error!");
139 CHECK_AND_RETURN_RET_LOG(key != nullptr, false, "key is nullptr!");
140 CHECK_AND_RETURN_RET_LOG(value != nullptr, false, "value is nullptr!");
141
142 return format->format_.PutStringValue(key, value);
143 }
144
OH_AVFormat_SetBuffer(struct OH_AVFormat * format,const char * key,const uint8_t * addr,size_t size)145 bool OH_AVFormat_SetBuffer(struct OH_AVFormat *format, const char *key, const uint8_t *addr, size_t size)
146 {
147 CHECK_AND_RETURN_RET_LOG(format != nullptr, false, "input format is nullptr!");
148 CHECK_AND_RETURN_RET_LOG(format->magic_ == AVMagic::AVCODEC_MAGIC_FORMAT, false, "magic error!");
149 CHECK_AND_RETURN_RET_LOG(key != nullptr, false, "key is nullptr!");
150 CHECK_AND_RETURN_RET_LOG(addr != nullptr, false, "addr is nullptr!");
151 CHECK_AND_RETURN_RET_LOG(size != 0, false, "size is zero!");
152
153 return format->format_.PutBuffer(key, addr, size);
154 }
155
OH_AVFormat_GetIntValue(struct OH_AVFormat * format,const char * key,int32_t * out)156 bool OH_AVFormat_GetIntValue(struct OH_AVFormat *format, const char *key, int32_t *out)
157 {
158 CHECK_AND_RETURN_RET_LOG(format != nullptr, false, "input format is nullptr!");
159 CHECK_AND_RETURN_RET_LOG(format->magic_ == AVMagic::AVCODEC_MAGIC_FORMAT, false, "magic error!");
160 CHECK_AND_RETURN_RET_LOG(key != nullptr, false, "key is nullptr!");
161 CHECK_AND_RETURN_RET_LOG(out != nullptr, false, "out is nullptr!");
162
163 return format->format_.GetIntValue(key, *out);
164 }
165
OH_AVFormat_GetLongValue(struct OH_AVFormat * format,const char * key,int64_t * out)166 bool OH_AVFormat_GetLongValue(struct OH_AVFormat *format, const char *key, int64_t *out)
167 {
168 CHECK_AND_RETURN_RET_LOG(format != nullptr, false, "input format is nullptr!");
169 CHECK_AND_RETURN_RET_LOG(format->magic_ == AVMagic::AVCODEC_MAGIC_FORMAT, false, "magic error!");
170 CHECK_AND_RETURN_RET_LOG(key != nullptr, false, "key is nullptr!");
171 CHECK_AND_RETURN_RET_LOG(out != nullptr, false, "out is nullptr!");
172
173 return format->format_.GetLongValue(key, *out);
174 }
175
OH_AVFormat_GetFloatValue(struct OH_AVFormat * format,const char * key,float * out)176 bool OH_AVFormat_GetFloatValue(struct OH_AVFormat *format, const char *key, float *out)
177 {
178 CHECK_AND_RETURN_RET_LOG(format != nullptr, false, "input format is nullptr!");
179 CHECK_AND_RETURN_RET_LOG(format->magic_ == AVMagic::AVCODEC_MAGIC_FORMAT, false, "magic error!");
180 CHECK_AND_RETURN_RET_LOG(key != nullptr, false, "key is nullptr!");
181 CHECK_AND_RETURN_RET_LOG(out != nullptr, false, "out is nullptr!");
182
183 return format->format_.GetFloatValue(key, *out);
184 }
185
OH_AVFormat_GetDoubleValue(struct OH_AVFormat * format,const char * key,double * out)186 bool OH_AVFormat_GetDoubleValue(struct OH_AVFormat *format, const char *key, double *out)
187 {
188 CHECK_AND_RETURN_RET_LOG(format != nullptr, false, "input format is nullptr!");
189 CHECK_AND_RETURN_RET_LOG(format->magic_ == AVMagic::AVCODEC_MAGIC_FORMAT, false, "magic error!");
190 CHECK_AND_RETURN_RET_LOG(key != nullptr, false, "key is nullptr!");
191 CHECK_AND_RETURN_RET_LOG(out != nullptr, false, "out is nullptr!");
192
193 return format->format_.GetDoubleValue(key, *out);
194 }
195
OH_AVFormat_GetStringValue(struct OH_AVFormat * format,const char * key,const char ** out)196 bool OH_AVFormat_GetStringValue(struct OH_AVFormat *format, const char *key, const char **out)
197 {
198 CHECK_AND_RETURN_RET_LOG(format != nullptr, false, "input format is nullptr!");
199 CHECK_AND_RETURN_RET_LOG(format->magic_ == AVMagic::AVCODEC_MAGIC_FORMAT, false, "magic error!");
200 CHECK_AND_RETURN_RET_LOG(key != nullptr, false, "key is nullptr!");
201 CHECK_AND_RETURN_RET_LOG(out != nullptr, false, "out is nullptr!");
202
203 if (format->outString_ != nullptr) {
204 free(format->outString_);
205 format->outString_ = nullptr;
206 }
207
208 std::string str;
209 bool ret = format->format_.GetStringValue(key, str);
210 if (!ret) {
211 return false;
212 }
213 uint32_t bufLength = str.size() > MAX_STRING_LENGTH ? MAX_STRING_LENGTH : str.size();
214
215 format->outString_ = static_cast<char *>(malloc((bufLength + 1) * sizeof(char)));
216 CHECK_AND_RETURN_RET_LOG(format->outString_ != nullptr, false, "malloc out string nullptr!");
217
218 if (strcpy_s(format->outString_, bufLength + 1, str.c_str()) != EOK) {
219 AVCODEC_LOGE("Failed to strcpy_s");
220 free(format->outString_);
221 format->outString_ = nullptr;
222 return false;
223 }
224
225 *out = format->outString_;
226 return true;
227 }
228
OH_AVFormat_GetBuffer(struct OH_AVFormat * format,const char * key,uint8_t ** addr,size_t * size)229 bool OH_AVFormat_GetBuffer(struct OH_AVFormat *format, const char *key, uint8_t **addr, size_t *size)
230 {
231 CHECK_AND_RETURN_RET_LOG(format != nullptr, false, "input format is nullptr!");
232 CHECK_AND_RETURN_RET_LOG(format->magic_ == AVMagic::AVCODEC_MAGIC_FORMAT, false, "magic error!");
233 CHECK_AND_RETURN_RET_LOG(key != nullptr, false, "key is nullptr!");
234 CHECK_AND_RETURN_RET_LOG(addr != nullptr, false, "addr is nullptr!");
235 CHECK_AND_RETURN_RET_LOG(size != nullptr, false, "size is nullptr!");
236
237 return format->format_.GetBuffer(key, addr, *size);
238 }
239
OH_AVFormat_DumpInfo(struct OH_AVFormat * format)240 const char *OH_AVFormat_DumpInfo(struct OH_AVFormat *format)
241 {
242 CHECK_AND_RETURN_RET_LOG(format != nullptr, nullptr, "input format is nullptr!");
243 if (format->dumpInfo_ != nullptr) {
244 free(format->dumpInfo_);
245 format->dumpInfo_ = nullptr;
246 }
247 std::string info = format->format_.Stringify();
248 if (info.empty()) {
249 return nullptr;
250 }
251 uint32_t bufLength = info.size() > MAX_DUMP_LENGTH ? MAX_DUMP_LENGTH : info.size();
252 format->dumpInfo_ = static_cast<char *>(malloc((bufLength + 1) * sizeof(char)));
253 CHECK_AND_RETURN_RET_LOG(format->dumpInfo_ != nullptr, nullptr, "malloc dump info nullptr!");
254 if (strcpy_s(format->dumpInfo_, bufLength + 1, info.c_str()) != EOK) {
255 AVCODEC_LOGE("Failed to strcpy_s");
256 free(format->dumpInfo_);
257 format->dumpInfo_ = nullptr;
258 }
259 return format->dumpInfo_;
260 }
261