Skip to content

Commit

Permalink
[stringapi] updated based on review
Browse files Browse the repository at this point in the history
  • Loading branch information
FocusLuo committed Jul 1, 2024
1 parent 8914b2c commit 1670f32
Show file tree
Hide file tree
Showing 7 changed files with 283 additions and 220 deletions.
5 changes: 5 additions & 0 deletions lib/ffmpeg/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ def have_ffmpeg_encoder(encoder):
result = try_call(f"{exe2os('ffmpeg')} -hide_banner -encoders | awk '{{print $2}}' | grep -w {encoder}")
return result, encoder

@memoize
def have_string_api(encoder):
result = try_call(f"{exe2os('ffmpeg')} -hide_banner -h encoder={encoder} | grep qsv_params")
return result, encoder

@memoize
def have_ffmpeg_decoder(decoder):
result = try_call(f"{exe2os('ffmpeg')} -hide_banner -decoders | awk '{{print $2}}' | grep -w {decoder}")
Expand Down
288 changes: 260 additions & 28 deletions lib/mfx/api.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,173 @@
from ...lib.codecs import Codec
from ...lib.common import memoize
from ...lib.properties import PropertyHandler
from enum import IntEnum, unique

# TODO: move string_api enums to this file
# NOTE/FIXME: these enums need to derive from IntEnum, not Enum
from ...lib.string_api import CodecProfile as MfxCodecProfile
from ...lib.string_api import CodecLevel as MfxCodecLevel
from ...lib.string_api import RateControlMethod as MfxRateControlMethod
#define come from _install/include/vpl/mfxstructures.h
class CodecProfile(IntEnum):
MFX_PROFILE_UNKNOWN =0 #/*!< Unspecified profile. */

#/* Combined with H.264 profile these flags impose additional constrains. See H.264 specification for the list of constrains. */
MFX_PROFILE_AVC_CONSTRAINT_SET0 = (0x100 << 0)
MFX_PROFILE_AVC_CONSTRAINT_SET1 = (0x100 << 1)
MFX_PROFILE_AVC_CONSTRAINT_SET2 = (0x100 << 2)
MFX_PROFILE_AVC_CONSTRAINT_SET3 = (0x100 << 3)
MFX_PROFILE_AVC_CONSTRAINT_SET4 = (0x100 << 4)
MFX_PROFILE_AVC_CONSTRAINT_SET5 = (0x100 << 5)

#/* H.264 Profiles. */
MFX_PROFILE_AVC_BASELINE =66
MFX_PROFILE_AVC_MAIN =77
MFX_PROFILE_AVC_EXTENDED =88
MFX_PROFILE_AVC_HIGH =100
MFX_PROFILE_AVC_HIGH10 =110
MFX_PROFILE_AVC_HIGH_422 =122
MFX_PROFILE_AVC_CONSTRAINED_BASELINE =MFX_PROFILE_AVC_BASELINE + MFX_PROFILE_AVC_CONSTRAINT_SET1
MFX_PROFILE_AVC_CONSTRAINED_HIGH =MFX_PROFILE_AVC_HIGH + MFX_PROFILE_AVC_CONSTRAINT_SET4 + MFX_PROFILE_AVC_CONSTRAINT_SET5
MFX_PROFILE_AVC_PROGRESSIVE_HIGH =MFX_PROFILE_AVC_HIGH + MFX_PROFILE_AVC_CONSTRAINT_SET4

#/* HEVC profiles */
MFX_PROFILE_HEVC_MAIN =1
MFX_PROFILE_HEVC_MAIN10 =2
MFX_PROFILE_HEVC_MAINSP =3
MFX_PROFILE_HEVC_REXT =4
MFX_PROFILE_HEVC_SCC =9

#/* AV1 Profiles */
MFX_PROFILE_AV1_MAIN = 1
MFX_PROFILE_AV1_HIGH = 2
MFX_PROFILE_AV1_PRO = 3

#/* VP9 Profiles */
MFX_PROFILE_VP9_0 = 1
MFX_PROFILE_VP9_1 = 2
MFX_PROFILE_VP9_2 = 3
MFX_PROFILE_VP9_3 = 4

class CodecLevel(IntEnum):
MFX_LEVEL_UNKNOWN =0 #/*!< Unspecified level. */
#/* H.264 level 1-1.3 */
MFX_LEVEL_AVC_1 =10
MFX_LEVEL_AVC_1b =9
MFX_LEVEL_AVC_11 =11
MFX_LEVEL_AVC_12 =12
MFX_LEVEL_AVC_13 =13
#/* H.264 level 2-2.2 */
MFX_LEVEL_AVC_2 =20
MFX_LEVEL_AVC_21 =21
MFX_LEVEL_AVC_22 =22
#/* H.264 level 3-3.2 */
MFX_LEVEL_AVC_3 =30
MFX_LEVEL_AVC_31 =31
MFX_LEVEL_AVC_32 =32
#/* H.264 level 4-4.2 */
MFX_LEVEL_AVC_4 =40
MFX_LEVEL_AVC_41 =41
MFX_LEVEL_AVC_42 =42
#/* H.264 level 5-5.2 */
MFX_LEVEL_AVC_5 =50
MFX_LEVEL_AVC_51 =51
MFX_LEVEL_AVC_52 =52
#/* H.264 level 6-6.2 */
MFX_LEVEL_AVC_6 =60
MFX_LEVEL_AVC_61 =61
MFX_LEVEL_AVC_62 =62

#/* HEVC levels */
MFX_LEVEL_HEVC_1 = 10
MFX_LEVEL_HEVC_2 = 20
MFX_LEVEL_HEVC_21 = 21
MFX_LEVEL_HEVC_3 = 30
MFX_LEVEL_HEVC_31 = 31
MFX_LEVEL_HEVC_4 = 40
MFX_LEVEL_HEVC_41 = 41
MFX_LEVEL_HEVC_5 = 50
MFX_LEVEL_HEVC_51 = 51
MFX_LEVEL_HEVC_52 = 52
MFX_LEVEL_HEVC_6 = 60
MFX_LEVEL_HEVC_61 = 61
MFX_LEVEL_HEVC_62 = 62

#/* AV1 Levels */
MFX_LEVEL_AV1_2 = 20
MFX_LEVEL_AV1_21 = 21
MFX_LEVEL_AV1_22 = 22
MFX_LEVEL_AV1_23 = 23
MFX_LEVEL_AV1_3 = 30
MFX_LEVEL_AV1_31 = 31
MFX_LEVEL_AV1_32 = 32
MFX_LEVEL_AV1_33 = 33
MFX_LEVEL_AV1_4 = 40
MFX_LEVEL_AV1_41 = 41
MFX_LEVEL_AV1_42 = 42
MFX_LEVEL_AV1_43 = 43
MFX_LEVEL_AV1_5 = 50
MFX_LEVEL_AV1_51 = 51
MFX_LEVEL_AV1_52 = 52
MFX_LEVEL_AV1_53 = 53
MFX_LEVEL_AV1_6 = 60
MFX_LEVEL_AV1_61 = 61
MFX_LEVEL_AV1_62 = 62
MFX_LEVEL_AV1_63 = 63
MFX_LEVEL_AV1_7 = 70
MFX_LEVEL_AV1_71 = 71
MFX_LEVEL_AV1_72 = 72
MFX_LEVEL_AV1_73 = 73

class RateControlMethod(IntEnum):
#/*! The RateControlMethod enumerator itemizes bitrate control methods. */
MFX_RATECONTROL_CBR =1 #/*!< Use the constant bitrate control algorithm. */
MFX_RATECONTROL_VBR =2 #/*!< Use the variable bitrate control algorithm. */
MFX_RATECONTROL_CQP =3 #/*!< Use the constant quantization parameter algorithm. */
MFX_RATECONTROL_AVBR =4 #/*!< Use the average variable bitrate control algorithm. */
MFX_RATECONTROL_RESERVED1 =5
MFX_RATECONTROL_RESERVED2 =6
MFX_RATECONTROL_RESERVED3 =100
MFX_RATECONTROL_RESERVED4 =7
#/*!
# Use the VBR algorithm with look ahead. It is a special bitrate control mode in the AVC encoder that has been designed
# to improve encoding quality. It works by performing extensive analysis of several dozen frames before the actual encoding and as a side
# effect significantly increases encoding delay and memory consumption.

# The only available rate control parameter in this mode is mfxInfoMFX::TargetKbps. Two other parameters, MaxKbps and InitialDelayInKB,
# are ignored. To control LA depth the application can use mfxExtCodingOption2::LookAheadDepth parameter.

# This method is not HRD compliant.
#*/
MFX_RATECONTROL_LA =8
#/*!
# Use the Intelligent Constant Quality algorithm. This algorithm improves subjective video quality of encoded stream. Depending on content,
# it may or may not decrease objective video quality. Only one control parameter is used - quality factor, specified by mfxInfoMFX::ICQQuality.
#*/
MFX_RATECONTROL_ICQ =9
#/*!
# Use the Video Conferencing Mode algorithm. This algorithm is similar to the VBR and uses the same set of parameters mfxInfoMFX::InitialDelayInKB,
# TargetKbpsandMaxKbps. It is tuned for IPPP GOP pattern and streams with strong temporal correlation between frames.
# It produces better objective and subjective video quality in these conditions than other bitrate control algorithms.
# It does not support interlaced content, B-frames and produced stream is not HRD compliant.
#*/
MFX_RATECONTROL_VCM =10
#/*!
# Use Intelligent Constant Quality algorithm with look ahead. Quality factor is specified by mfxInfoMFX::ICQQuality.
# To control LA depth the application can use mfxExtCodingOption2::LookAheadDepth parameter.
#
# This method is not HRD compliant.
#*/
MFX_RATECONTROL_LA_ICQ =11
#/*!
# MFX_RATECONTROL_LA_EXT has been removed
#*/

#/*! Use HRD compliant look ahead rate control algorithm. */
MFX_RATECONTROL_LA_HRD =13
#/*!
# Use the variable bitrate control algorithm with constant quality. This algorithm trying to achieve the target subjective quality with
# the minimum number of bits, while the bitrate constraint and HRD compliance are satisfied. It uses the same set of parameters
# as VBR and quality factor specified by mfxExtCodingOption3::QVBRQuality.
#*/
MFX_RATECONTROL_QVBR =14

class StringAPI(PropertyHandler):
level = property(lambda s: s.ifprop("level", "CodecLevel={level}"))
gop = property(lambda s: s.ifprop("gop", "GopPicSize={gop}"))
bframes = property(lambda s: s.ifprop("bframes", "GopRefDist={bframes}"))
slices = property(lambda s: s.ifprop("slices", "NumSlice={slices}"))
Expand Down Expand Up @@ -75,43 +233,117 @@ def map_profile(cls, codec, profile):
# then call MfxCodecProfile.lookup(codec, profile)
return {
Codec.AVC : {
"high" : MfxCodecProfile.MFX_PROFILE_AVC_HIGH,
"main" : MfxCodecProfile.MFX_PROFILE_AVC_MAIN,
"baseline" : MfxCodecProfile.MFX_PROFILE_AVC_BASELINE,
"unknown" : MfxCodecProfile.MFX_PROFILE_UNKNOWN
"high" : CodecProfile.MFX_PROFILE_AVC_HIGH,
"main" : CodecProfile.MFX_PROFILE_AVC_MAIN,
"baseline" : CodecProfile.MFX_PROFILE_AVC_BASELINE,
"unknown" : CodecProfile.MFX_PROFILE_UNKNOWN
},
Codec.HEVC : {
"main" : MfxCodecProfile.MFX_PROFILE_HEVC_MAIN,
"main444" : MfxCodecProfile.MFX_PROFILE_HEVC_REXT,
"scc" : MfxCodecProfile.MFX_PROFILE_HEVC_SCC,
"scc-444" : MfxCodecProfile.MFX_PROFILE_HEVC_SCC,
"mainsp" : MfxCodecProfile.MFX_PROFILE_HEVC_MAINSP,
"main10" : MfxCodecProfile.MFX_PROFILE_HEVC_MAIN10,
"main10sp" : MfxCodecProfile.MFX_PROFILE_HEVC_MAINSP,
"main444-10" : MfxCodecProfile.MFX_PROFILE_HEVC_REXT,
"unknown" : MfxCodecProfile.MFX_PROFILE_UNKNOWN
"main" : CodecProfile.MFX_PROFILE_HEVC_MAIN,
"main444" : CodecProfile.MFX_PROFILE_HEVC_REXT,
"scc" : CodecProfile.MFX_PROFILE_HEVC_SCC,
"scc-444" : CodecProfile.MFX_PROFILE_HEVC_SCC,
"mainsp" : CodecProfile.MFX_PROFILE_HEVC_MAINSP,
"main10" : CodecProfile.MFX_PROFILE_HEVC_MAIN10,
"main10sp" : CodecProfile.MFX_PROFILE_HEVC_MAINSP,
"main444-10" : CodecProfile.MFX_PROFILE_HEVC_REXT,
"unknown" : CodecProfile.MFX_PROFILE_UNKNOWN
},
Codec.AV1 : {
"main" : MfxCodecProfile.MFX_PROFILE_AV1_MAIN,
"main" : CodecProfile.MFX_PROFILE_AV1_MAIN,
},
Codec.VP9 : {
"profile0" : MfxCodecProfile.MFX_PROFILE_VP9_0,
"profile1" : MfxCodecProfile.MFX_PROFILE_VP9_1,
"profile2" : MfxCodecProfile.MFX_PROFILE_VP9_2,
"profile3" : MfxCodecProfile.MFX_PROFILE_VP9_3
"profile0" : CodecProfile.MFX_PROFILE_VP9_0,
"profile1" : CodecProfile.MFX_PROFILE_VP9_1,
"profile2" : CodecProfile.MFX_PROFILE_VP9_2,
"profile3" : CodecProfile.MFX_PROFILE_VP9_3
},
}[codec][profile]

@classmethod
@memoize
def map_level(cls, codec, level):
# TODO: we could move this method to the enum class as "lookup"
# then call MfxCodecProfile.lookup(codec, profile)
return {
#/* H264 levels */
Codec.AVC : {
10 : MFX_LEVEL_AV1_1,
9 : MFX_LEVEL_AV1_1b,
11 : MFX_LEVEL_AV1_11,
12 : MFX_LEVEL_AV1_12,
13 : MFX_LEVEL_AV1_13,
20 : MFX_LEVEL_AV1_2,
21 : MFX_LEVEL_AV1_21,
22 : MFX_LEVEL_AV1_22,
30 : MFX_LEVEL_AV1_3,
31 : MFX_LEVEL_AV1_31,
32 : MFX_LEVEL_AV1_32,
40 : MFX_LEVEL_AV1_4,
41 : MFX_LEVEL_AV1_41,
42 : MFX_LEVEL_AV1_42,
50 : MFX_LEVEL_AV1_5,
51 : MFX_LEVEL_AV1_51,
52 : MFX_LEVEL_AV1_52,
60 : MFX_LEVEL_AV1_6,
61 : MFX_LEVEL_AV1_61,
62 : MFX_LEVEL_AV1_62,
},
#/* HEVC levels */
Codec.HEVC : {
10 : MFX_LEVEL_HEVC_1,
20 : MFX_LEVEL_HEVC_2,
21 : MFX_LEVEL_HEVC_21,
30 : MFX_LEVEL_HEVC_3,
31 : MFX_LEVEL_HEVC_31,
40 : MFX_LEVEL_HEVC_4,
41 : MFX_LEVEL_HEVC_41,
50 : MFX_LEVEL_HEVC_5,
51 : MFX_LEVEL_HEVC_51,
52 : MFX_LEVEL_HEVC_52,
60 : MFX_LEVEL_HEVC_6,
61 : MFX_LEVEL_HEVC_61,
62 : MFX_LEVEL_HEVC_62,
},
#/* AV1 Levels */
Codec.AV1 : {
20 : MFX_LEVEL_AV1_2,
21 : MFX_LEVEL_AV1_21,
22 : MFX_LEVEL_AV1_22,
23 : MFX_LEVEL_AV1_23,
30 : MFX_LEVEL_AV1_3,
31 : MFX_LEVEL_AV1_31,
32 : MFX_LEVEL_AV1_32,
33 : MFX_LEVEL_AV1_33,
40 : MFX_LEVEL_AV1_4,
41 : MFX_LEVEL_AV1_41,
42 : MFX_LEVEL_AV1_42,
43 : MFX_LEVEL_AV1_43,
50 : MFX_LEVEL_AV1_5,
51 : MFX_LEVEL_AV1_51,
52 : MFX_LEVEL_AV1_52,
53 : MFX_LEVEL_AV1_53,
60 : MFX_LEVEL_AV1_6,
61 : MFX_LEVEL_AV1_61,
62 : MFX_LEVEL_AV1_62,
63 : MFX_LEVEL_AV1_63,
70 : MFX_LEVEL_AV1_7,
71 : MFX_LEVEL_AV1_71,
72 : MFX_LEVEL_AV1_72,
73 : MFX_LEVEL_AV1_73,
},
}[codec][level]

@classmethod
@memoize
def map_rcmode(cls, rcmode):
# TODO: we could move this method to the enum class as "lookup"
# then call MfxRateControlMethod.lookup(rcmode)
return {
"CQP" : MfxRateControlMethod.MFX_RATECONTROL_CQP,
"CBR" : MfxRateControlMethod.MFX_RATECONTROL_CBR,
"VBR" : MfxRateControlMethod.MFX_RATECONTROL_VBR,
"ICQ" : MfxRateControlMethod.MFX_RATECONTROL_ICQ,
"CQP" : RateControlMethod.MFX_RATECONTROL_CQP,
"CBR" : RateControlMethod.MFX_RATECONTROL_CBR,
"VBR" : RateControlMethod.MFX_RATECONTROL_VBR,
"ICQ" : RateControlMethod.MFX_RATECONTROL_ICQ,
}[rcmode.upper()]

# Example:
Expand Down
7 changes: 0 additions & 7 deletions lib/platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,6 @@ def have_caps(*args):
)
return get_caps(*args) is not None, failmsg

@memoize
def have_string_api():
from .common import get_media
has_strapi = False
has_strapi = get_media()._get_platform_stringapi_enable()
return has_strapi is not None, False

@memoize
def load_capsinfo_file(infofile):
namespace = dict()
Expand Down
Loading

0 comments on commit 1670f32

Please sign in to comment.