• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <algorithm>
17 #include "native_avmagic.h"
18 #include "avcodec_list.h"
19 #include "avcodec_info.h"
20 #include "avcodec_log.h"
21 #include "avcodec_errors.h"
22 #include "securec.h"
23 #include "native_avcapability.h"
24 
25 namespace {
26 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "NativeAVCapability"};
27 }
28 using namespace OHOS::MediaAVCodec;
29 
~OH_AVCapability()30 OH_AVCapability::~OH_AVCapability() {}
31 
OH_AVCodec_GetCapability(const char * mime,bool isEncoder)32 OH_AVCapability *OH_AVCodec_GetCapability(const char *mime, bool isEncoder)
33 {
34     CHECK_AND_RETURN_RET_LOG(mime != nullptr, nullptr, "Get capability failed: mime is nullptr");
35     CHECK_AND_RETURN_RET_LOG(strlen(mime) != 0, nullptr, "Get capability failed: mime is empty");
36     std::shared_ptr<AVCodecList> codeclist = AVCodecListFactory::CreateAVCodecList();
37     CHECK_AND_RETURN_RET_LOG(codeclist != nullptr, nullptr, "Get capability failed: CreateAVCodecList failed");
38     uint32_t sizeOfCap = sizeof(OH_AVCapability);
39     CapabilityData *capabilityData = codeclist->GetCapability(mime, isEncoder, AVCodecCategory::AVCODEC_NONE);
40     CHECK_AND_RETURN_RET_LOG(capabilityData != nullptr, nullptr,
41                              "Get capability failed: cannot find matched capability");
42     const std::string &name = capabilityData->codecName;
43     CHECK_AND_RETURN_RET_LOG(!name.empty(), nullptr, "Get capability failed: cannot find matched capability");
44     void *addr = codeclist->GetBuffer(name, sizeOfCap);
45     CHECK_AND_RETURN_RET_LOG(addr != nullptr, nullptr, "Get capability failed: malloc capability buffer failed");
46     OH_AVCapability *obj = static_cast<OH_AVCapability *>(addr);
47     obj->capabilityData_ = capabilityData;
48     obj->profiles_ = nullptr;
49     obj->levels_ = nullptr;
50     obj->pixFormats_ = nullptr;
51     obj->sampleRates_ = nullptr;
52     AVCODEC_LOGD("OH_AVCodec_GetCapability successful");
53     return obj;
54 }
55 
OH_AVCodec_GetCapabilityByCategory(const char * mime,bool isEncoder,OH_AVCodecCategory category)56 OH_AVCapability *OH_AVCodec_GetCapabilityByCategory(const char *mime, bool isEncoder, OH_AVCodecCategory category)
57 {
58     CHECK_AND_RETURN_RET_LOG(mime != nullptr, nullptr, "Get capabilityByCategory failed: mime is nullptr");
59     CHECK_AND_RETURN_RET_LOG(strlen(mime) != 0, nullptr, "Get capabilityByCategory failed: mime is empty");
60     std::shared_ptr<AVCodecList> codeclist = AVCodecListFactory::CreateAVCodecList();
61     CHECK_AND_RETURN_RET_LOG(codeclist != nullptr, nullptr,
62         "Get capabilityByCategory failed: CreateAVCodecList failed");
63     AVCodecCategory innerCategory;
64     if (category == HARDWARE) {
65         innerCategory = AVCodecCategory::AVCODEC_HARDWARE;
66     } else {
67         innerCategory = AVCodecCategory::AVCODEC_SOFTWARE;
68     }
69     uint32_t sizeOfCap = sizeof(OH_AVCapability);
70     CapabilityData *capabilityData = codeclist->GetCapability(mime, isEncoder, innerCategory);
71     CHECK_AND_RETURN_RET_LOG(capabilityData != nullptr, nullptr,
72                              "Get capabilityByCategory failed: cannot find matched capability");
73     const std::string &name = capabilityData->codecName;
74     CHECK_AND_RETURN_RET_LOG(!name.empty(), nullptr, "Get capabilityByCategory failed: cannot find matched capability");
75     void *addr = codeclist->GetBuffer(name, sizeOfCap);
76     CHECK_AND_RETURN_RET_LOG(addr != nullptr, nullptr,
77                              "Get capabilityByCategory failed: malloc capability buffer failed");
78     OH_AVCapability *obj = static_cast<OH_AVCapability *>(addr);
79     obj->capabilityData_ = capabilityData;
80     obj->profiles_ = nullptr;
81     obj->levels_ = nullptr;
82     obj->pixFormats_ = nullptr;
83     obj->sampleRates_ = nullptr;
84     AVCODEC_LOGD("OH_AVCodec_GetCapabilityByCategory successful");
85     return obj;
86 }
87 
OH_AVCapability_GetName(OH_AVCapability * capability)88 const char *OH_AVCapability_GetName(OH_AVCapability *capability)
89 {
90     if (capability == nullptr) {
91         AVCODEC_LOGE("Get name failed:  null input");
92         return "";
93     }
94     const auto &name = capability->capabilityData_->codecName;
95     return name.data();
96 }
97 
OH_AVCapability_IsHardware(OH_AVCapability * capability)98 bool OH_AVCapability_IsHardware(OH_AVCapability *capability)
99 {
100     if (capability == nullptr) {
101         AVCODEC_LOGE("Varified is hardware failed:  null input");
102         return false;
103     }
104     std::shared_ptr<AVCodecInfo> codecInfo = std::make_shared<AVCodecInfo>(capability->capabilityData_);
105     return codecInfo->IsHardwareAccelerated();
106 }
107 
OH_AVCapability_GetMaxSupportedInstances(OH_AVCapability * capability)108 int32_t OH_AVCapability_GetMaxSupportedInstances(OH_AVCapability *capability)
109 {
110     if (capability == nullptr) {
111         AVCODEC_LOGE("Get max supported instance failed: null input");
112         return 0;
113     }
114     std::shared_ptr<AVCodecInfo> codecInfo = std::make_shared<AVCodecInfo>(capability->capabilityData_);
115     return codecInfo->GetMaxSupportedInstances();
116 }
117 
OH_AVCapability_GetSupportedProfiles(OH_AVCapability * capability,const int32_t ** profiles,uint32_t * profileNum)118 OH_AVErrCode OH_AVCapability_GetSupportedProfiles(OH_AVCapability *capability, const int32_t **profiles,
119                                                   uint32_t *profileNum)
120 {
121     CHECK_AND_RETURN_RET_LOG(profileNum != nullptr && profiles != nullptr, AV_ERR_INVALID_VAL,
122                              "Get supported profiles failed: null input");
123     *profiles = nullptr;
124     *profileNum = 0;
125     if (capability == nullptr) {
126         AVCODEC_LOGE("Get supported profiles failed: null input");
127         return AV_ERR_INVALID_VAL;
128     }
129     std::shared_ptr<AudioCaps> codecInfo = std::make_shared<AudioCaps>(capability->capabilityData_);
130     const auto &vec = codecInfo->GetSupportedProfiles();
131     if (vec.size() == 0) {
132         return AV_ERR_OK;
133     }
134 
135     std::shared_ptr<AVCodecList> codeclist = AVCodecListFactory::CreateAVCodecList();
136     CHECK_AND_RETURN_RET_LOG(codeclist != nullptr, AV_ERR_UNKNOWN,
137         "Get supported profiles failed: CreateAVCodecList failed");
138     if (capability->profiles_ != nullptr) {
139         codeclist->DeleteBuffer(capability->profiles_);
140         capability->profiles_ = nullptr;
141     }
142 
143     size_t vecSize = vec.size() * sizeof(int32_t);
144     capability->profiles_ = static_cast<int32_t *>(codeclist->NewBuffer(vecSize));
145     CHECK_AND_RETURN_RET_LOG(capability->profiles_ != nullptr, AV_ERR_NO_MEMORY, "new buffer failed");
146     errno_t ret = memcpy_s(capability->profiles_, vecSize, vec.data(), vecSize);
147     CHECK_AND_RETURN_RET_LOG(ret == EOK, AV_ERR_UNKNOWN, "memcpy_s failed");
148 
149     *profiles = capability->profiles_;
150     *profileNum = vec.size();
151     return AV_ERR_OK;
152 }
153 
OH_AVCapability_GetSupportedLevelsForProfile(OH_AVCapability * capability,int32_t profile,const int32_t ** levels,uint32_t * levelNum)154 OH_AVErrCode OH_AVCapability_GetSupportedLevelsForProfile(OH_AVCapability *capability, int32_t profile,
155                                                           const int32_t **levels, uint32_t *levelNum)
156 {
157     CHECK_AND_RETURN_RET_LOG(levels != nullptr && levelNum != nullptr, AV_ERR_INVALID_VAL,
158                              "Get supported levels for profile failed: null input");
159     *levels = nullptr;
160     *levelNum = 0;
161     if (capability == nullptr) {
162         AVCODEC_LOGE("Get supported levels for profile failed: null input");
163         return AV_ERR_INVALID_VAL;
164     }
165     std::shared_ptr<AVCodecInfo> codecInfo = std::make_shared<AVCodecInfo>(capability->capabilityData_);
166     const auto &profileLevelsMap = codecInfo->GetSupportedLevelsForProfile();
167     const auto &levelsmatch = profileLevelsMap.find(profile);
168     if (levelsmatch == profileLevelsMap.end()) {
169         return AV_ERR_INVALID_VAL;
170     }
171     const auto &vec = levelsmatch->second;
172     if (vec.size() == 0) {
173         return AV_ERR_OK;
174     }
175 
176     std::shared_ptr<AVCodecList> codeclist = AVCodecListFactory::CreateAVCodecList();
177     CHECK_AND_RETURN_RET_LOG(codeclist != nullptr, AV_ERR_UNKNOWN,
178         "Get supported levels for profile failed: CreateAVCodecList failed");
179     if (capability->levels_ != nullptr) {
180         codeclist->DeleteBuffer(capability->levels_);
181         capability->levels_ = nullptr;
182     }
183 
184     size_t vecSize = vec.size() * sizeof(int32_t);
185     capability->levels_ = static_cast<int32_t *>(codeclist->NewBuffer(vecSize));
186     CHECK_AND_RETURN_RET_LOG(capability->levels_ != nullptr, AV_ERR_NO_MEMORY, "new buffer failed");
187     errno_t ret = memcpy_s(capability->levels_, vecSize, vec.data(), vecSize);
188     CHECK_AND_RETURN_RET_LOG(ret == EOK, AV_ERR_UNKNOWN, "memcpy_s failed");
189 
190     *levels = capability->levels_;
191     *levelNum = vec.size();
192     return AV_ERR_OK;
193 }
194 
OH_AVCapability_AreProfileAndLevelSupported(OH_AVCapability * capability,int32_t profile,int32_t level)195 bool OH_AVCapability_AreProfileAndLevelSupported(OH_AVCapability *capability, int32_t profile, int32_t level)
196 {
197     if (capability == nullptr) {
198         AVCODEC_LOGE("Varified profiles and level failed: null input");
199         return false;
200     }
201     std::shared_ptr<AVCodecInfo> codecInfo = std::make_shared<AVCodecInfo>(capability->capabilityData_);
202     const auto &profileLevelsMap = codecInfo->GetSupportedLevelsForProfile();
203     const auto &levels = profileLevelsMap.find(profile);
204     if (levels == profileLevelsMap.end()) {
205         return false;
206     }
207     return find(levels->second.begin(), levels->second.end(), level) != levels->second.end();
208 }
209 
OH_AVCapability_GetEncoderBitrateRange(OH_AVCapability * capability,OH_AVRange * bitrateRange)210 OH_AVErrCode OH_AVCapability_GetEncoderBitrateRange(OH_AVCapability *capability, OH_AVRange *bitrateRange)
211 {
212     CHECK_AND_RETURN_RET_LOG(bitrateRange != nullptr, AV_ERR_INVALID_VAL,
213                              "Get encoder bitrate range failed: null input");
214     if (capability == nullptr) {
215         bitrateRange->minVal = 0;
216         bitrateRange->maxVal = 0;
217         AVCODEC_LOGE("Get encoder bitrate range failed: null input");
218         return AV_ERR_INVALID_VAL;
219     }
220     std::shared_ptr<AudioCaps> codecInfo = std::make_shared<AudioCaps>(capability->capabilityData_);
221     const auto &bitrate = codecInfo->GetSupportedBitrate();
222     bitrateRange->minVal = bitrate.minVal;
223     bitrateRange->maxVal = bitrate.maxVal;
224     return AV_ERR_OK;
225 }
226 
OH_AVCapability_GetEncoderQualityRange(OH_AVCapability * capability,OH_AVRange * qualityRange)227 OH_AVErrCode OH_AVCapability_GetEncoderQualityRange(OH_AVCapability *capability, OH_AVRange *qualityRange)
228 {
229     CHECK_AND_RETURN_RET_LOG(qualityRange != nullptr, AV_ERR_INVALID_VAL, "Get encoder quality failed: null input");
230     if (capability == nullptr) {
231         qualityRange->minVal = 0;
232         qualityRange->maxVal = 0;
233         AVCODEC_LOGE("Get encoder quality failed: null input");
234         return AV_ERR_INVALID_VAL;
235     }
236     std::shared_ptr<VideoCaps> codecInfo = std::make_shared<VideoCaps>(capability->capabilityData_);
237     const auto &quality = codecInfo->GetSupportedEncodeQuality();
238     qualityRange->minVal = quality.minVal;
239     qualityRange->maxVal = quality.maxVal;
240     return AV_ERR_OK;
241 }
242 
OH_AVCapability_GetEncoderComplexityRange(OH_AVCapability * capability,OH_AVRange * complexityRange)243 OH_AVErrCode OH_AVCapability_GetEncoderComplexityRange(OH_AVCapability *capability, OH_AVRange *complexityRange)
244 {
245     CHECK_AND_RETURN_RET_LOG(complexityRange != nullptr, AV_ERR_INVALID_VAL,
246                              "Get encoder complexity range failed: null input");
247     if (capability == nullptr) {
248         complexityRange->minVal = 0;
249         complexityRange->maxVal = 0;
250         AVCODEC_LOGE("Get encoder complexity range failed: null input");
251         return AV_ERR_INVALID_VAL;
252     }
253     std::shared_ptr<VideoCaps> codecInfo = std::make_shared<VideoCaps>(capability->capabilityData_);
254     const auto &complexity = codecInfo->GetSupportedComplexity();
255     complexityRange->minVal = complexity.minVal;
256     complexityRange->maxVal = complexity.maxVal;
257     return AV_ERR_OK;
258 }
259 
OH_AVCapability_IsEncoderBitrateModeSupported(OH_AVCapability * capability,OH_BitrateMode bitrateMode)260 bool OH_AVCapability_IsEncoderBitrateModeSupported(OH_AVCapability *capability, OH_BitrateMode bitrateMode)
261 {
262     if (capability == nullptr) {
263         AVCODEC_LOGE("Varified encoder bitrate mode failed: null input");
264         return false;
265     }
266     std::shared_ptr<VideoCaps> codecInfo = std::make_shared<VideoCaps>(capability->capabilityData_);
267     const auto &bitrateModeVec = codecInfo->GetSupportedBitrateMode();
268     return find(bitrateModeVec.begin(), bitrateModeVec.end(), bitrateMode) != bitrateModeVec.end();
269 }
270 
OH_AVCapability_GetAudioSupportedSampleRates(OH_AVCapability * capability,const int32_t ** sampleRates,uint32_t * sampleRateNum)271 OH_AVErrCode OH_AVCapability_GetAudioSupportedSampleRates(OH_AVCapability *capability, const int32_t **sampleRates,
272                                                           uint32_t *sampleRateNum)
273 {
274     CHECK_AND_RETURN_RET_LOG(sampleRates != nullptr && sampleRateNum != nullptr, AV_ERR_INVALID_VAL,
275                              "Get audio supported samplerates failed: null input");
276     *sampleRates = nullptr;
277     *sampleRateNum = 0;
278     if (capability == nullptr) {
279         AVCODEC_LOGE("Get audio supported samplerates failed: null input");
280         return AV_ERR_INVALID_VAL;
281     }
282     std::shared_ptr<AudioCaps> codecInfo = std::make_shared<AudioCaps>(capability->capabilityData_);
283     const auto &vec = codecInfo->GetSupportedSampleRates();
284     if (vec.size() == 0) {
285         return AV_ERR_OK;
286     }
287 
288     std::shared_ptr<AVCodecList> codeclist = AVCodecListFactory::CreateAVCodecList();
289     CHECK_AND_RETURN_RET_LOG(codeclist != nullptr, AV_ERR_UNKNOWN,
290         "Get audio supported samplerates failed: CreateAVCodecList failed");
291     if (capability->sampleRates_ != nullptr) {
292         codeclist->DeleteBuffer(capability->sampleRates_);
293         capability->sampleRates_ = nullptr;
294     }
295 
296     size_t vecSize = vec.size() * sizeof(int32_t);
297     capability->sampleRates_ = static_cast<int32_t *>(codeclist->NewBuffer(vecSize));
298     CHECK_AND_RETURN_RET_LOG(capability->sampleRates_ != nullptr, AV_ERR_NO_MEMORY, "new buffer failed");
299     errno_t ret = memcpy_s(capability->sampleRates_, vecSize, vec.data(), vecSize);
300     CHECK_AND_RETURN_RET_LOG(ret == EOK, AV_ERR_UNKNOWN, "memcpy_s failed");
301 
302     *sampleRates = capability->sampleRates_;
303     *sampleRateNum = vec.size();
304     return AV_ERR_OK;
305 }
306 
OH_AVCapability_GetAudioChannelCountRange(OH_AVCapability * capability,OH_AVRange * channelCountRange)307 OH_AVErrCode OH_AVCapability_GetAudioChannelCountRange(OH_AVCapability *capability, OH_AVRange *channelCountRange)
308 {
309     CHECK_AND_RETURN_RET_LOG(channelCountRange != nullptr, AV_ERR_INVALID_VAL,
310                              "Get audio channel count range failed: null input");
311     if (capability == nullptr) {
312         channelCountRange->minVal = 0;
313         channelCountRange->maxVal = 0;
314         AVCODEC_LOGE("Get audio channel count range failed: null input");
315         return AV_ERR_INVALID_VAL;
316     }
317     std::shared_ptr<AudioCaps> codecInfo = std::make_shared<AudioCaps>(capability->capabilityData_);
318     const auto &channels = codecInfo->GetSupportedChannel();
319     channelCountRange->minVal = channels.minVal;
320     channelCountRange->maxVal = channels.maxVal;
321     return AV_ERR_OK;
322 }
323 
OH_AVCapability_GetVideoSupportedPixelFormats(OH_AVCapability * capability,const int32_t ** pixFormats,uint32_t * pixFormatNum)324 OH_AVErrCode OH_AVCapability_GetVideoSupportedPixelFormats(OH_AVCapability *capability, const int32_t **pixFormats,
325                                                            uint32_t *pixFormatNum)
326 {
327     CHECK_AND_RETURN_RET_LOG(pixFormats != nullptr && pixFormatNum != nullptr, AV_ERR_INVALID_VAL,
328                              "Get video supported pixel formats failed: null input");
329     *pixFormats = nullptr;
330     *pixFormatNum = 0;
331     if (capability == nullptr) {
332         AVCODEC_LOGE("Get video supported pixel formats failed: null input");
333         return AV_ERR_INVALID_VAL;
334     }
335 
336     std::shared_ptr<VideoCaps> codecInfo = std::make_shared<VideoCaps>(capability->capabilityData_);
337     const auto &vec = codecInfo->GetSupportedFormats();
338     if (vec.size() == 0) {
339         return AV_ERR_OK;
340     }
341 
342     std::shared_ptr<AVCodecList> codeclist = AVCodecListFactory::CreateAVCodecList();
343     CHECK_AND_RETURN_RET_LOG(codeclist != nullptr, AV_ERR_UNKNOWN,
344         "Get video supported pixel formats failed: CreateAVCodecList failed");
345     if (capability->pixFormats_ != nullptr) {
346         codeclist->DeleteBuffer(capability->pixFormats_);
347         capability->pixFormats_ = nullptr;
348     }
349 
350     size_t vecSize = vec.size() * sizeof(int32_t);
351     capability->pixFormats_ = static_cast<int32_t *>(codeclist->NewBuffer(vecSize));
352     CHECK_AND_RETURN_RET_LOG(capability->pixFormats_ != nullptr, AV_ERR_NO_MEMORY, "new buffer failed");
353     errno_t ret = memcpy_s(capability->pixFormats_, vecSize, vec.data(), vecSize);
354     CHECK_AND_RETURN_RET_LOG(ret == EOK, AV_ERR_UNKNOWN, "memcpy_s failed");
355     *pixFormats = capability->pixFormats_;
356     *pixFormatNum = vec.size();
357     return AV_ERR_OK;
358 }
359 
OH_AVCapability_GetVideoWidthAlignment(OH_AVCapability * capability,int32_t * widthAlignment)360 OH_AVErrCode OH_AVCapability_GetVideoWidthAlignment(OH_AVCapability *capability, int32_t *widthAlignment)
361 {
362     CHECK_AND_RETURN_RET_LOG(widthAlignment != nullptr, AV_ERR_INVALID_VAL,
363                              "Get video width alignment failed: null input");
364     if (capability == nullptr) {
365         *widthAlignment = 0;
366         AVCODEC_LOGE("Get video width alignment failed: null input");
367         return AV_ERR_INVALID_VAL;
368     }
369     std::shared_ptr<VideoCaps> codecInfo = std::make_shared<VideoCaps>(capability->capabilityData_);
370     *widthAlignment = codecInfo->GetSupportedWidthAlignment();
371     return AV_ERR_OK;
372 }
373 
OH_AVCapability_GetVideoHeightAlignment(OH_AVCapability * capability,int32_t * heightAlignment)374 OH_AVErrCode OH_AVCapability_GetVideoHeightAlignment(OH_AVCapability *capability, int32_t *heightAlignment)
375 {
376     CHECK_AND_RETURN_RET_LOG(heightAlignment != nullptr, AV_ERR_INVALID_VAL,
377                              "Get video height alignment failed: null input");
378     if (capability == nullptr) {
379         *heightAlignment = 0;
380         AVCODEC_LOGE("Get video height alignment failed: null input");
381         return AV_ERR_INVALID_VAL;
382     }
383     std::shared_ptr<VideoCaps> codecInfo = std::make_shared<VideoCaps>(capability->capabilityData_);
384     *heightAlignment = codecInfo->GetSupportedHeightAlignment();
385     return AV_ERR_OK;
386 }
387 
OH_AVCapability_GetVideoWidthRangeForHeight(OH_AVCapability * capability,int32_t height,OH_AVRange * widthRange)388 OH_AVErrCode OH_AVCapability_GetVideoWidthRangeForHeight(OH_AVCapability *capability, int32_t height,
389                                                          OH_AVRange *widthRange)
390 {
391     CHECK_AND_RETURN_RET_LOG(widthRange != nullptr, AV_ERR_INVALID_VAL,
392                              "Get video width range for height failed: null input");
393     if (capability == nullptr || height <= 0) {
394         widthRange->minVal = 0;
395         widthRange->maxVal = 0;
396         AVCODEC_LOGE("Get video width range for height failed: invalid input");
397         return AV_ERR_INVALID_VAL;
398     }
399     std::shared_ptr<VideoCaps> codecInfo = std::make_shared<VideoCaps>(capability->capabilityData_);
400     const auto &width = codecInfo->GetVideoWidthRangeForHeight(height);
401     widthRange->minVal = width.minVal;
402     widthRange->maxVal = width.maxVal;
403     if (width.minVal == 0 && width.maxVal == 0) {
404         return AV_ERR_INVALID_VAL;
405     }
406     return AV_ERR_OK;
407 }
408 
OH_AVCapability_GetVideoHeightRangeForWidth(OH_AVCapability * capability,int32_t width,OH_AVRange * heightRange)409 OH_AVErrCode OH_AVCapability_GetVideoHeightRangeForWidth(OH_AVCapability *capability, int32_t width,
410                                                          OH_AVRange *heightRange)
411 {
412     CHECK_AND_RETURN_RET_LOG(heightRange != nullptr, AV_ERR_INVALID_VAL,
413                              "Get video height range for width failed: null input");
414     if (capability == nullptr || width <= 0) {
415         heightRange->minVal = 0;
416         heightRange->maxVal = 0;
417         AVCODEC_LOGE("Get video height range for width failed: invalid input");
418         return AV_ERR_INVALID_VAL;
419     }
420     std::shared_ptr<VideoCaps> codecInfo = std::make_shared<VideoCaps>(capability->capabilityData_);
421     const auto &height = codecInfo->GetVideoHeightRangeForWidth(width);
422     heightRange->minVal = height.minVal;
423     heightRange->maxVal = height.maxVal;
424     if (height.minVal == 0 && height.maxVal == 0) {
425         return AV_ERR_INVALID_VAL;
426     }
427     return AV_ERR_OK;
428 }
429 
OH_AVCapability_GetVideoWidthRange(OH_AVCapability * capability,OH_AVRange * widthRange)430 OH_AVErrCode OH_AVCapability_GetVideoWidthRange(OH_AVCapability *capability, OH_AVRange *widthRange)
431 {
432     CHECK_AND_RETURN_RET_LOG(widthRange != nullptr, AV_ERR_INVALID_VAL, "Get video width range failed: null input");
433     if (capability == nullptr) {
434         widthRange->minVal = 0;
435         widthRange->maxVal = 0;
436         AVCODEC_LOGE("Get video width range failed: null input");
437         return AV_ERR_INVALID_VAL;
438     }
439     std::shared_ptr<VideoCaps> codecInfo = std::make_shared<VideoCaps>(capability->capabilityData_);
440     const auto &width = codecInfo->GetSupportedWidth();
441     widthRange->minVal = width.minVal;
442     widthRange->maxVal = width.maxVal;
443     return AV_ERR_OK;
444 }
445 
OH_AVCapability_GetVideoHeightRange(OH_AVCapability * capability,OH_AVRange * heightRange)446 OH_AVErrCode OH_AVCapability_GetVideoHeightRange(OH_AVCapability *capability, OH_AVRange *heightRange)
447 {
448     CHECK_AND_RETURN_RET_LOG(heightRange != nullptr, AV_ERR_INVALID_VAL, "Get video height range failed: null input");
449     if (capability == nullptr) {
450         heightRange->minVal = 0;
451         heightRange->maxVal = 0;
452         AVCODEC_LOGE("Get video height range failed: null input");
453         return AV_ERR_INVALID_VAL;
454     }
455 
456     std::shared_ptr<VideoCaps> codecInfo = std::make_shared<VideoCaps>(capability->capabilityData_);
457     const auto &height = codecInfo->GetSupportedHeight();
458     heightRange->minVal = height.minVal;
459     heightRange->maxVal = height.maxVal;
460     return AV_ERR_OK;
461 }
462 
OH_AVCapability_IsVideoSizeSupported(OH_AVCapability * capability,int32_t width,int32_t height)463 bool OH_AVCapability_IsVideoSizeSupported(OH_AVCapability *capability, int32_t width, int32_t height)
464 {
465     if (capability == nullptr) {
466         AVCODEC_LOGE("Varified is video size supported failed: null input");
467         return false;
468     }
469     std::shared_ptr<VideoCaps> videoCap = std::make_shared<VideoCaps>(capability->capabilityData_);
470     return videoCap->IsSizeSupported(width, height);
471 }
472 
OH_AVCapability_GetVideoFrameRateRange(OH_AVCapability * capability,OH_AVRange * frameRateRange)473 OH_AVErrCode OH_AVCapability_GetVideoFrameRateRange(OH_AVCapability *capability, OH_AVRange *frameRateRange)
474 {
475     CHECK_AND_RETURN_RET_LOG(frameRateRange != nullptr, AV_ERR_INVALID_VAL,
476                              "Get video framerate range failed: null input");
477     if (capability == nullptr) {
478         frameRateRange->minVal = 0;
479         frameRateRange->maxVal = 0;
480         AVCODEC_LOGE("Get video framerate range failed: null input");
481         return AV_ERR_INVALID_VAL;
482     }
483     std::shared_ptr<VideoCaps> videoCap = std::make_shared<VideoCaps>(capability->capabilityData_);
484     const auto &frameRate = videoCap->GetSupportedFrameRate();
485     frameRateRange->minVal = frameRate.minVal;
486     frameRateRange->maxVal = frameRate.maxVal;
487     return AV_ERR_OK;
488 }
489 
OH_AVCapability_GetVideoFrameRateRangeForSize(OH_AVCapability * capability,int32_t width,int32_t height,OH_AVRange * frameRateRange)490 OH_AVErrCode OH_AVCapability_GetVideoFrameRateRangeForSize(OH_AVCapability *capability, int32_t width, int32_t height,
491                                                            OH_AVRange *frameRateRange)
492 {
493     CHECK_AND_RETURN_RET_LOG(frameRateRange != nullptr, AV_ERR_INVALID_VAL,
494                              "Get video framerate range for size failed: null input");
495     if (capability == nullptr || width <= 0 || height <= 0) {
496         frameRateRange->minVal = 0;
497         frameRateRange->maxVal = 0;
498         AVCODEC_LOGE("Get video framerate range for size failed: invalid input");
499         return AV_ERR_INVALID_VAL;
500     }
501     std::shared_ptr<VideoCaps> videoCap = std::make_shared<VideoCaps>(capability->capabilityData_);
502     const auto &frameRate = videoCap->GetSupportedFrameRatesFor(width, height);
503     frameRateRange->minVal = frameRate.minVal;
504     frameRateRange->maxVal = frameRate.maxVal;
505     if (frameRate.minVal == 0 && frameRate.maxVal == 0) {
506         return AV_ERR_INVALID_VAL;
507     }
508     return AV_ERR_OK;
509 }
510 
OH_AVCapability_AreVideoSizeAndFrameRateSupported(OH_AVCapability * capability,int32_t width,int32_t height,int32_t frameRate)511 bool OH_AVCapability_AreVideoSizeAndFrameRateSupported(OH_AVCapability *capability, int32_t width, int32_t height,
512                                                        int32_t frameRate)
513 {
514     if (capability == nullptr) {
515         AVCODEC_LOGE("Varified video framerate and size failed: null input");
516         return false;
517     }
518     std::shared_ptr<VideoCaps> videoCap = std::make_shared<VideoCaps>(capability->capabilityData_);
519     return videoCap->IsSizeAndRateSupported(width, height, frameRate);
520 }