• 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 #ifndef AVCODEC_INFO_H
17 #define AVCODEC_INFO_H
18 
19 #include <cstdint>
20 #include <memory>
21 #include <vector>
22 #include "av_common.h"
23 #include "nocopyable.h"
24 
25 namespace OHOS {
26 namespace Media {
27 /**
28  * @brief AVCodec Type
29  *
30  * @since 3.1
31  * @version 3.1
32  */
33 enum AVCodecType : int32_t {
34     AVCODEC_TYPE_NONE = -1,
35     AVCODEC_TYPE_VIDEO_ENCODER = 0,
36     AVCODEC_TYPE_VIDEO_DECODER,
37     AVCODEC_TYPE_AUDIO_ENCODER,
38     AVCODEC_TYPE_AUDIO_DECODER,
39 };
40 
41 /**
42  * @brief Range contain min and max value
43  *
44  * @since 3.1
45  * @version 3.1
46  */
47 struct Range {
48     int32_t minVal;
49     int32_t maxVal;
RangeRange50     Range() : minVal(0), maxVal(0) {}
RangeRange51     Range(const int32_t &min, const int32_t &max)
52     {
53         if (min <= max) {
54             this->minVal = min;
55             this->maxVal = max;
56         } else {
57             this->minVal = 0;
58             this->maxVal = 0;
59         }
60     }
61 
CreateRange62     Range Create(const int32_t &min, const int32_t &max)
63     {
64         return Range(min, max);
65     }
66 
IntersectRange67     Range Intersect(const int32_t &min, const int32_t &max)
68     {
69         int32_t minCmp = this->minVal > min ? this->minVal : min;
70         int32_t maxCmp = this->maxVal < max ? this->maxVal : max;
71         return this->Create(minCmp, maxCmp);
72     }
73 
IntersectRange74     Range Intersect(const Range &range)
75     {
76         int32_t minCmp = this->minVal > range.minVal ? this->minVal : range.minVal;
77         int32_t maxCmp = this->maxVal < range.maxVal ? this->maxVal : range.maxVal;
78         return this->Create(minCmp, maxCmp);
79     }
80 };
81 
82 /**
83  * @brief ImgSize contain width and height
84  *
85  * @since 3.1
86  * @version 3.1
87  */
88 struct ImgSize {
89     int32_t width;
90     int32_t height;
91 
ImgSizeImgSize92     ImgSize() : width(0), height(0) {}
93 
ImgSizeImgSize94     ImgSize(const int32_t &width, const int32_t &height)
95     {
96         this->width = width;
97         this->height = height;
98     }
99 
100     bool operator<(const ImgSize &p) const
101     {
102         return (width < p.width) || (width == p.width && height < p.height);
103     }
104 };
105 
106 /**
107  * @brief Capability Data struct of Codec, parser from config file
108  *
109  * @since 3.1
110  * @version 3.1
111  */
112 struct CapabilityData {
113     std::string codecName = "";
114     int32_t codecType = AVCODEC_TYPE_NONE;
115     std::string mimeType = "";
116     bool isVendor = false;
117     Range bitrate;
118     Range channels;
119     Range complexity;
120     Range alignment;
121     Range width;
122     Range height;
123     Range frameRate;
124     Range encodeQuality;
125     Range quality;
126     Range blockPerFrame;
127     Range blockPerSecond;
128     ImgSize blockSize;
129     std::vector<int32_t> sampleRate;
130     std::vector<int32_t> format;
131     std::vector<int32_t> profiles;
132     std::vector<int32_t> bitrateMode;
133     std::vector<int32_t> levels;
134     std::map<int32_t, std::vector<int32_t>> profileLevelsMap;
135     std::map<ImgSize, Range> measuredFrameRate;
136     bool supportSwapWidthHeight = false;
137 };
138 
139 struct LevelParams {
140     int32_t maxBlockPerFrame = 0;
141     int32_t maxBlockPerSecond = 0;
142     int32_t maxFrameRate = 0;
143     int32_t maxWidth = 0;
144     int32_t maxHeight = 0;
LevelParamsLevelParams145     LevelParams(const int32_t &blockPerFrame, const int32_t &blockPerSecond,
146                 const int32_t &frameRate, const int32_t &width, const int32_t height)
147     {
148         this->maxBlockPerFrame = blockPerFrame;
149         this->maxBlockPerSecond = blockPerSecond;
150         this->maxFrameRate = frameRate;
151         this->maxWidth = width;
152         this->maxHeight = height;
153     }
LevelParamsLevelParams154     LevelParams(const int32_t &blockPerFrame, const int32_t &blockPerSecond)
155     {
156         this->maxBlockPerFrame = blockPerFrame;
157         this->maxBlockPerSecond = blockPerSecond;
158     }
159 };
160 
161 class __attribute__((visibility("default"))) AVCodecInfo {
162 public:
163     explicit AVCodecInfo(CapabilityData &capabilityData);
164     ~AVCodecInfo();
165 
166     /**
167      * @brief Get name of this codec, used to create the codec instance.
168      * @return Returns codec name.
169      * @since 3.1
170      * @version 3.1
171      */
172     std::string GetName();
173 
174     /**
175      * @brief Get type of this codec
176      * @return Returns codec type, see {@link AVCodecType}
177      * @since 3.1
178      * @version 3.1
179      */
180     AVCodecType GetType();
181 
182     /**
183      * @brief Get mime type of this codec
184      * @return Returns codec mime type, see {@link CodecMimeType}
185      * @since 3.1
186      * @version 3.1
187      */
188     std::string GetMimeType();
189 
190     /**
191      * @brief Check whether the codec is accelerated by hardware.
192      * @return Returns true if the codec is hardware accelerated; false otherwise.
193      * @since 3.1
194      * @version 3.1
195      */
196     bool IsHardwareAccelerated();
197 
198     /**
199      * @brief Check whether the codec is software implemented only.
200      * @return Returns true if the codec is software implemented only; false otherwise.
201      * @since 3.1
202      * @version 3.1
203      */
204     bool IsSoftwareOnly();
205 
206     /**
207      * @brief Check whether the codec is provided by vendor.
208      * @return Returns true if the codec is provided by vendor; false otherwise.
209      * @since 3.1
210      * @version 3.1
211      */
212     bool IsVendor();
213 
214 private:
215     CapabilityData data_;
216 };
217 
218 class __attribute__((visibility("default"))) VideoCaps {
219 public:
220     explicit VideoCaps(CapabilityData &capabilityData);
221     ~VideoCaps();
222 
223     /**
224      * @brief Get codec information,  such as the codec name, codec type,
225      * whether hardware acceleration is supported, whether only software is supported,
226      * and whether the codec is provided by the vendor.
227      * @return Returns the pointer of {@link AVCodecInfo}.
228      * @since 3.1
229      * @version 3.1
230      */
231     std::shared_ptr<AVCodecInfo> GetCodecInfo();
232 
233     /**
234      * @brief Get supported bitrate range.
235      * @return Returns the range of supported bitrates.
236      * @since 3.1
237      * @version 3.1
238      */
239     Range GetSupportedBitrate();
240 
241     /**
242      * @brief Get supported video raw formats.
243      * @return Returns an array of supported formats. For Details, see {@link VideoPixelFormat}.
244      * @since 3.1
245      * @version 3.1
246      */
247     std::vector<int32_t> GetSupportedFormats();
248 
249     /**
250      * @brief Get supported alignment of video height, only used for video codecs.
251      * @return Returns the supported alignment of video height (in pixels).
252      * @since 3.1
253      * @version 3.1
254      */
255     int32_t GetSupportedHeightAlignment();
256 
257     /**
258      * @brief Get supported alignment of video width, only used for video codecs.
259      * @return Returns the supported alignment of video width (in pixels).
260      * @since 3.1
261      * @version 3.1
262      */
263     int32_t GetSupportedWidthAlignment();
264 
265     /**
266      * @brief Get supported width range of video.
267      * @return Returns the supported width range of video.
268      * @since 3.1
269      * @version 3.1
270      */
271     Range GetSupportedWidth();
272 
273     /**
274      * @brief Get supported height range of video.
275      * @return Returns the supported height range of video.
276      * @since 3.1
277      * @version 3.1
278      */
279     Range GetSupportedHeight();
280 
281     /**
282      * @brief Get supported profiles of this codec.
283      * @return Returns an array of supported profiles:
284      * returns {@link H263Profile} array if codec is h263,
285      * returns {@link AVCProfile} array if codec is h264,
286      * returns {@link HEVCProfile} array if codec is h265,
287      * returns {@link MPEG2Profile} array if codec is mpeg2,
288      * returns {@link MPEG4Profile} array if codec is mpeg4,
289      * returns {@link VP8Profile} array if codec is vp8.
290      * @since 3.1
291      * @version 3.1
292      */
293     std::vector<int32_t> GetSupportedProfiles();
294 
295     /**
296      * @brief Get supported codec level array.
297      * @return Returns an array of supported codec level number.
298      * @since 3.1
299      * @version 3.1
300      */
301     std::vector<int32_t> GetSupportedLevels();
302 
303     /**
304      * @brief Get supported video encode quality Range.
305      * @return Returns an array of supported video encode quality Range.
306      * @since 3.1
307      * @version 3.1
308      */
309     Range GetSupportedEncodeQuality();
310 
311     /**
312      * @brief Check whether the width and height is supported.
313      * @param width Indicates the specified video width (in pixels).
314      * @param height Indicates the specified video height (in pixels).
315      * @return Returns true if the codec supports {@link width} * {@link height} size video, false otherwise.
316      * @since 3.1
317      * @version 3.1
318      */
319     bool IsSizeSupported(int32_t width, int32_t height);
320 
321     /**
322      * @brief Get supported frameRate.
323      * @return Returns the supported frameRate range of video.
324      * @since 3.1
325      * @version 3.1
326      */
327     Range GetSupportedFrameRate();
328 
329     /**
330      * @brief Get supported frameRate range for the specified width and height.
331      * @param width Indicates the specified video width (in pixels).
332      * @param height Indicates the specified video height (in pixels).
333      * @return Returns the supported frameRate range for the specified width and height.
334      * @since 3.1
335      * @version 3.1
336      */
337     Range GetSupportedFrameRatesFor(int32_t width, int32_t height);
338 
339     /**
340      * @brief Check whether the size and frameRate is supported.
341      * @param width Indicates the specified video width (in pixels).
342      * @param height Indicates the specified video height (in pixels).
343      * @param frameRate Indicates the specified video frameRate.
344      * @return Returns true if the codec supports the specified size and frameRate; false otherwise.
345      * @since 3.1
346      * @version 3.1
347      */
348     bool IsSizeAndRateSupported(int32_t width, int32_t height, double frameRate);
349 
350     /**
351      * @brief Get preferred frameRate range for the specified width and height,
352      * these framerates can be reach the performance.
353      * @param width Indicates the specified video width (in pixels).
354      * @param height Indicates the specified video height (in pixels).
355      * @return Returns preferred frameRate range for the specified width and height.
356      * @since 3.1
357      * @version 3.1
358      */
359     Range GetPreferredFrameRate(int32_t width, int32_t height);
360 
361     /**
362      * @brief Get supported encode bitrate mode.
363      * @return Returns an array of supported encode bitrate mode. For details, see {@link VideoEncodeBitrateMode}.
364      * @since 3.1
365      * @version 3.1
366      */
367     std::vector<int32_t> GetSupportedBitrateMode();
368 
369     /**
370      * @brief Get supported encode qualit range.
371      * @return Returns supported encode qualit range.
372      * @since 3.1
373      * @version 3.1
374      */
375     Range GetSupportedQuality();
376 
377     /**
378      * @brief Get supported encode complexity range.
379      * @return Returns supported encode complexity range.
380      * @since 3.1
381      * @version 3.1
382      */
383     Range GetSupportedComplexity();
384 
385     /**
386      * @brief Check video encoder wether support request key frame dynamicly.
387      * @return Returns true if support, false not support.
388      * @since 3.1
389      * @version 3.1
390      */
391     bool IsSupportDynamicIframe();
392 
393 private:
394     CapabilityData data_;
395     int32_t blockWidth_;
396     int32_t blockHeight_;
397     Range horizontalBlockRange_;
398     Range verticalBlockRange_;
399     Range blockPerFrameRange_;
400     Range blockPerSecondRange_;
401     Range widthRange_;
402     Range heightRange_;
403     Range frameRateRange_;
404     void InitParams();
405     void UpdateParams();
406     void LoadLevelParams();
407     void LoadAVCLevelParams();
408     ImgSize MatchClosestSize(const ImgSize &imgSize);
409     int32_t DivCeil(const int32_t &dividend, const int32_t &divisor);
410     Range DivRange(const Range &range, const int32_t &divisor);
411     void UpdateBlockParams(const int32_t &blockWidth, const int32_t &blockHeight,
412                            Range &blockPerFrameRange, Range &blockPerSecondRange);
413 };
414 
415 class __attribute__((visibility("default"))) AudioCaps {
416 public:
417     explicit AudioCaps(CapabilityData &capabilityData);
418     ~AudioCaps();
419 
420     /**
421      * @brief Get codec information,  such as the codec name, codec type,
422      * whether hardware acceleration is supported, whether only software is supported,
423      * and whether the codec is provided by the vendor.
424      * @return Returns the pointer of {@link AVCodecInfo}
425      * @since 3.1
426      * @version 3.1
427      */
428     std::shared_ptr<AVCodecInfo> GetCodecInfo();
429 
430     /**
431      * @brief Get supported bitrate range.
432      * @return Returns the range of supported bitrates.
433      * @since 3.1
434      * @version 3.1
435      */
436     Range GetSupportedBitrate();
437 
438     /**
439      * @brief Get supported channel range.
440      * @return Returns the range of supported channel.
441      * @since 3.1
442      * @version 3.1
443      */
444     Range GetSupportedChannel();
445 
446     /**
447      * @brief Get supported audio raw format range.
448      * @return Returns the range of supported audio raw format. For details, see {@link AudioSampleFormat}.
449      * @since 3.1
450      * @version 3.1
451      */
452     std::vector<int32_t> GetSupportedFormats();
453 
454     /**
455      * @brief Get supported audio samplerates.
456      * @return Returns an array of supported samplerates.
457      * @since 3.1
458      * @version 3.1
459      */
460     std::vector<int32_t> GetSupportedSampleRates();
461 
462     /**
463      * @brief Get supported codec profile number.
464      * @return Returns an array of supported codec profile number. For details, see {@link AACProfile}.
465      * @since 3.1
466      * @version 3.1
467      */
468     std::vector<int32_t> GetSupportedProfiles();
469 
470     /**
471      * @brief Get supported codec level array.
472      * @return Returns an array of supported codec level number.
473      * @since 3.1
474      * @version 3.1
475      */
476     std::vector<int32_t> GetSupportedLevels();
477 
478     /**
479      * @brief Get supported encode complexity range.
480      * @return Returns supported encode complexity range.
481      * @since 3.1
482      * @version 3.1
483      */
484     Range GetSupportedComplexity();
485 
486 private:
487     CapabilityData data_;
488 };
489 
490 /**
491  * @brief Enumerates the codec mime type.
492  */
493 class CodecMimeType {
494 public:
495     static constexpr std::string_view VIDEO_H263 = "video/h263";
496     static constexpr std::string_view VIDEO_AVC = "video/avc";
497     static constexpr std::string_view VIDEO_MPEG2 = "video/mpeg2";
498     static constexpr std::string_view VIDEO_HEVC = "video/hevc";
499     static constexpr std::string_view VIDEO_MPEG4 = "video/mp4v-es";
500     static constexpr std::string_view VIDEO_VP8 = "video/x-vnd.on2.vp8";
501     static constexpr std::string_view VIDEO_VP9 = "video/x-vnd.on2.vp9";
502     static constexpr std::string_view AUDIO_AMR_NB = "audio/3gpp";
503     static constexpr std::string_view AUDIO_AMR_WB = "audio/amr-wb";
504     static constexpr std::string_view AUDIO_MPEG = "audio/mpeg";
505     static constexpr std::string_view AUDIO_AAC = "audio/mp4a-latm";
506     static constexpr std::string_view AUDIO_VORBIS = "audio/vorbis";
507     static constexpr std::string_view AUDIO_OPUS = "audio/opus";
508     static constexpr std::string_view AUDIO_FLAC = "audio/flac";
509     static constexpr std::string_view AUDIO_RAW = "audio/raw";
510 };
511 
512 /**
513  * @brief AVC Profile
514  *
515  * @since 3.1
516  * @version 3.1
517  */
518 enum AVCProfile : int32_t {
519     AVC_PROFILE_BASELINE = 0,
520     AVC_PROFILE_CONSTRAINED_BASELINE = 1,
521     AVC_PROFILE_CONSTRAINED_HIGH = 2,
522     AVC_PROFILE_EXTENDED = 3,
523     AVC_PROFILE_HIGH = 4,
524     AVC_PROFILE_HIGH_10 = 5,
525     AVC_PROFILE_HIGH_422 = 6,
526     AVC_PROFILE_HIGH_444 = 7,
527     AVC_PROFILE_MAIN = 8,
528 };
529 
530 /**
531  * @brief HEVC Profile
532  *
533  * @since 3.1
534  * @version 3.1
535  */
536 enum HEVCProfile : int32_t {
537     HEVC_PROFILE_MAIN = 0,
538     HEVC_PROFILE_MAIN_10 = 1,
539     HEVC_PROFILE_MAIN_STILL = 2,
540     HEVC_PROFILE_MAIN_10_HDR10 = 3,
541     HEVC_PROFILE_MAIN_10_HDR10_PLUS = 4,
542 };
543 
544 /**
545  * @brief MPEG2 Profile
546  *
547  * @since 3.1
548  * @version 3.1
549  */
550 enum MPEG2Profile : int32_t {
551     MPEG2_PROFILE_422 = 0,
552     MPEG2_PROFILE_HIGH = 1,
553     MPEG2_PROFILE_MAIN = 2,
554     MPEG2_PROFILE_SNR = 3,
555     MPEG2_PROFILE_SIMPLE = 4,
556     MPEG2_PROFILE_SPATIAL = 5,
557 };
558 
559 /**
560  * @brief MPEG4 Profile
561  *
562  * @since 3.1
563  * @version 3.1
564  */
565 enum MPEG4Profile : int32_t {
566     MPEG4_PROFILE_ADVANCED_CODING = 0,
567     MPEG4_PROFILE_ADVANCED_CORE = 1,
568     MPEG4_PROFILE_ADVANCED_REAL_TIME = 2,
569     MPEG4_PROFILE_ADVANCED_SCALABLE = 3,
570     MPEG4_PROFILE_ADVANCED_SIMPLE = 4,
571     MPEG4_PROFILE_BASIC_ANIMATED = 5,
572     MPEG4_PROFILE_CORE = 6,
573     MPEG4_PROFILE_CORE_SCALABLE = 7,
574     MPEG4_PROFILE_HYBRID = 8,
575     MPEG4_PROFILE_MAIN = 9,
576     MPEG4_PROFILE_NBIT = 10,
577     MPEG4_PROFILE_SCALABLE_TEXTURE = 11,
578     MPEG4_PROFILE_SIMPLE = 12,
579     MPEG4_PROFILE_SIMPLE_FBA = 13,
580     MPEG4_PROFILE_SIMPLE_FACE = 14,
581     MPEG4_PROFILE_SIMPLE_SCALABLE = 15,
582 };
583 
584 /**
585  * @brief H263 Profile
586  *
587  * @since 3.1
588  * @version 3.1
589  */
590 enum H263Profile : int32_t {
591     H263_PROFILE_BACKWARD_COMPATIBLE = 0,
592     H263_PROFILE_BASELINE = 1,
593     H263_PROFILE_H320_CODING = 2,
594     H263_PROFILE_HIGH_COMPRESSION = 3,
595     H263_PROFILE_HIGH_LATENCY = 4,
596     H263_PROFILE_ISW_V2 = 5,
597     H263_PROFILE_ISW_V3 = 6,
598     H263_PROFILE_INTERLACE = 7,
599     H263_PROFILE_INTERNET = 8,
600 };
601 
602 /**
603  * @brief
604  *
605  * @since 3.1
606  * @version 3.1
607  */
608 enum VP8Profile : int32_t {
609     VP8_PROFILE_MAIN = 0,
610 };
611 
612 /**
613  * @brief
614  *
615  * @since 3.1
616  * @version 3.1
617  */
618 enum AACProfile : int32_t {
619     AAC_PROFILE_LC = 0,
620     AAC_PROFILE_ELD = 1,
621     AAC_PROFILE_ERLC = 2,
622     AAC_PROFILE_HE = 3,
623     AAC_PROFILE_HE_V2 = 4,
624     AAC_PROFILE_LD = 5,
625     AAC_PROFILE_MAIN = 6,
626 };
627 
628 /**
629  * @brief
630  *
631  * @since 3.1
632  * @version 3.1
633  */
634 enum AVCLevel : int32_t {
635     AVC_LEVEL_1 = 0,
636     AVC_LEVEL_1b = 1,
637     AVC_LEVEL_11 = 2,
638     AVC_LEVEL_12 = 3,
639     AVC_LEVEL_13 = 4,
640     AVC_LEVEL_2 = 5,
641     AVC_LEVEL_21 = 6,
642     AVC_LEVEL_22 = 7,
643     AVC_LEVEL_3 = 8,
644     AVC_LEVEL_31 = 9,
645     AVC_LEVEL_32 = 10,
646     AVC_LEVEL_4 = 11,
647     AVC_LEVEL_41 = 12,
648     AVC_LEVEL_42 = 13,
649     AVC_LEVEL_5 = 14,
650     AVC_LEVEL_51 = 15,
651 };
652 
653 /**
654  * @brief
655  *
656  * @since 3.1
657  * @version 3.1
658  */
659 enum HEVCLevel : int32_t {
660     HEVC_LEVEL_1 = 0,
661     HEVC_LEVEL_2 = 1,
662     HEVC_LEVEL_21 = 2,
663     HEVC_LEVEL_3 = 3,
664     HEVC_LEVEL_31 = 4,
665     HEVC_LEVEL_4 = 5,
666     HEVC_LEVEL_41 = 6,
667     HEVC_LEVEL_5 = 7,
668     HEVC_LEVEL_51 = 8,
669     HEVC_LEVEL_52 = 9,
670     HEVC_LEVEL_6 = 10,
671     HEVC_LEVEL_61 = 11,
672     HEVC_LEVEL_62 = 12,
673 };
674 
675 /**
676  * @brief
677  *
678  * @since 3.1
679  * @version 3.1
680  */
681 enum MPEG2Level : int32_t {
682     MPEG2_LEVEL_LL = 0,
683     MPEG2_LEVEL_ML = 1,
684     MPEG2_LEVEL_H14 = 2,
685     MPEG2_LEVEL_HL = 3,
686 };
687 
688 /**
689  * @brief
690  *
691  * @since 3.1
692  * @version 3.1
693  */
694 enum MPEG4Level : int32_t {
695     MPEG4_LEVEL_0 = 0,
696     MPEG4_LEVEL_0B = 1,
697     MPEG4_LEVEL_1 = 2,
698     MPEG4_LEVEL_2 = 3,
699     MPEG4_LEVEL_3 = 4,
700     MPEG4_LEVEL_4 = 5,
701     MPEG4_LEVEL_4A = 6,
702     MPEG4_LEVEL_5 = 7,
703 };
704 
705 /**
706  * @brief
707  *
708  * @since 3.1
709  * @version 3.1
710  */
711 enum VideoEncodeBitrateMode : int32_t {
712     /**
713      * constant bit rate mode.
714     */
715     CBR = 0,
716     /**
717      * variable bit rate mode.
718     */
719     VBR = 1,
720     /**
721      * constant quality mode.
722     */
723     CQ = 2,
724 };
725 } // namespace Media
726 } // namespace OHOS
727 #endif // AVCODEC_INFO_H