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