xpra icon
Bug tracker and wiki

This bug tracker and wiki are being discontinued
please use https://github.com/Xpra-org/xpra instead.


Ticket #370: nvenc-sdkv2.patch

File nvenc-sdkv2.patch, 22.3 KB (added by Antoine Martin, 7 years ago)

changes needed to build against the NVENC SDK version 2

  • xpra/codecs/nvenc/encoder.pyx

     
    1616
    1717from libc.stdint cimport uint8_t, uint16_t, uint32_t, int32_t, uint64_t
    1818
     19USE_OPENENCODESESSIONEX = True
    1920FORCE = os.environ.get("XPRA_NVENC_FORCE", "0")=="1"
    2021CLIENT_KEY = os.environ.get("XPRA_NVENC_CLIENT_KEY", "")
    2122
     
    235236        uint32_t    ltrFrame            #[out]: Flag indicating this frame is marked as LTR frame
    236237        uint32_t    reservedBitFields   #[in]: Reserved bit fields and must be set to 0
    237238        void*       outputBitstream     #[in]: Pointer to the bitstream buffer being locked.
    238         uint32_t*   sliceOffsets        #[in,out]: Array which receives the slice offsets. Currently used only when NV_ENC_CONFIG_H264::sliceMode == 3. Array size must be equal to NV_ENC_CONFIG_H264::sliceModeData.
     239        #uint32_t*   sliceOffsets        #[in,out]: Array which receives the slice offsets. Currently used only when NV_ENC_CONFIG_H264::sliceMode == 3. Array size must be equal to NV_ENC_CONFIG_H264::sliceModeData.
    239240        uint32_t    frameIdx            #[out]: Frame no. for which the bitstream is being retrieved.
    240241        uint32_t    hwEncodeStatus      #[out]: The NvEncodeAPI interface status for the locked picture.
    241242        uint32_t    numSlices           #[out]: Number of slices in the encoded picture. Will be reported only if NV_ENC_INITIALIZE_PARAMS::reportSliceOffsets set to 1.
     
    319320    GUID NV_ENC_H264_PROFILE_HIGH_GUID
    320321    GUID NV_ENC_H264_PROFILE_STEREO_GUID
    321322    GUID NV_ENC_H264_PROFILE_SVC_TEMPORAL_SCALABILTY
    322     GUID NV_ENC_H264_PROFILE_CONSTRAINED_HIGH_GUID
     323    #GUID NV_ENC_H264_PROFILE_CONSTRAINED_HIGH_GUID
    323324    GUID NV_ENC_MPEG2_PROFILE_SIMPLE_GUID
    324325    GUID NV_ENC_MPEG2_PROFILE_MAIN_GUID
    325326    GUID NV_ENC_MPEG2_PROFILE_HIGH_GUID
     
    334335    GUID NV_ENC_PRESET_HP_GUID
    335336    GUID NV_ENC_PRESET_HQ_GUID
    336337    GUID NV_ENC_PRESET_BD_GUID
    337     GUID NV_ENC_PRESET_LOW_LATENCY_DEFAULT_GUID
    338     GUID NV_ENC_PRESET_LOW_LATENCY_HQ_GUID
    339     GUID NV_ENC_PRESET_LOW_LATENCY_HP_GUID
     338    #V3 ONLY:
     339    #GUID NV_ENC_PRESET_LOW_LATENCY_DEFAULT_GUID
     340    #GUID NV_ENC_PRESET_LOW_LATENCY_HQ_GUID
     341    #GUID NV_ENC_PRESET_LOW_LATENCY_HP_GUID
    340342    #NV_ENC_CODEC_MPEG2_GUID, etc..
     343    #V2 ONLY:
     344    GUID NV_ENC_PRESET_VC_GUID
     345    GUID NV_ENC_PRESET_WIDI_GUID
     346    GUID NV_ENC_PRESET_CAMERA_GUID
     347    GUID NV_ENC_PRESET_AVCHD_GUID
     348    GUID NV_ENC_PRESET_IPOD_GUID
     349    GUID NV_ENC_PRESET_PSP_GUID
     350    GUID NV_ENC_PRESET_GAME_CAPTURE_GUID
     351    GUID NV_ENC_PRESET_CLOUD_GAMING_720p60_GUID
     352    GUID NV_ENC_PRESET_CLOUD_GAMING_720p30_GUID
     353    GUID NV_ENC_PRESET_DESKTOP_CAPTURE_GUID
     354    GUID NV_ENC_PRESET_MVC_STEREO_GUID
     355    GUID NV_ENC_PRESET_CLOUD_GAMING_720p60_GUID
     356    GUID NV_ENC_PRESET_CLOUD_GAMING_720p30_GUID
     357    GUID NV_ENC_PRESET_DESKTOP_CAPTURE_GUID
     358    GUID NV_ENC_PRESET_MVC_STEREO_GUID
    341359
    342360    ctypedef struct NV_ENC_CAPS_PARAM:
    343361        uint32_t    version
     
    451469                                        #The low latency application which wants to invalidate reference frame as an error resilience tool
    452470                                        #is recommended to use a large DPB size so that the encoder can keep old reference frames which can be used if recent
    453471                                        #frames are invalidated.
    454         uint32_t    sliceMode           #[in]: This parameter in conjunction with sliceModeData specifies the way in which the picture is divided into slices
     472        #uint32_t    sliceMode           #[in]: This parameter in conjunction with sliceModeData specifies the way in which the picture is divided into slices
    455473                                        #sliceMode = 0 MB based slices, sliceMode = 1 Byte based slices, sliceMode = 2 MB row based slices, sliceMode = 3, numSlices in Picture
    456474                                        #When forceIntraRefreshWithFrameCnt is set it will have priority over sliceMode setting
    457475                                        #When sliceMode == 0 and sliceModeData == 0 whole picture will be coded with one slice
    458         uint32_t    sliceModeData       #[in]: Specifies the parameter needed for sliceMode. For:
     476        #uint32_t    sliceModeData       #[in]: Specifies the parameter needed for sliceMode. For:
    459477                                        #sliceMode = 0, sliceModeData specifies # of MBs in each slice (except last slice)
    460478                                        #sliceMode = 1, sliceModeData specifies maximum # of bytes in each slice (except last slice)
    461479                                        #sliceMode = 2, sliceModeData specifies # of MB rows in each slice (except last slice)
     
    627645                                        #forceIntraRefreshWithFrameCnt cannot be used if B frames are used in the GOP structure specified
    628646        uint32_t    constrainedFrame    #[in]: Set to 1 if client wants to encode this frame with each slice completely independent of other slices in the frame.
    629647                                        #NV_ENC_INITIALIZE_PARAMS::enableConstrainedEncoding should be set to 1
    630         uint32_t    sliceModeDataUpdate #[in]: Set to 1 if client wants to change the sliceModeData field to speficy new sliceSize Parameter
     648        #uint32_t    sliceModeDataUpdate #[in]: Set to 1 if client wants to change the sliceModeData field to speficy new sliceSize Parameter
    631649                                        #When forceIntraRefreshWithFrameCnt is set it will have priority over sliceMode setting
    632650        uint32_t    ltrMarkFrame        #[in]: Set to 1 if client wants to mark this frame as LTR
    633651        uint32_t    ltrUseFrames        #[in]: Set to 1 if client allows encoding this frame using the LTR frames specified in ltrFrameBitmap
     
    639657        uint32_t    sliceTypeArrayCnt   #[in]: Client should set this to the number of elements allocated in sliceTypeData array. If sliceTypeData is NULL then this should be set to 0
    640658        uint32_t    seiPayloadArrayCnt  #[in]: Specifies the number of elements allocated in  seiPayloadArray array.
    641659        NV_ENC_H264_SEI_PAYLOAD *seiPayloadArray    #[in]: Array of SEI payloads which will be inserted for this frame.
    642         uint32_t    sliceMode           #[in]: This parameter in conjunction with sliceModeData specifies the way in which the picture is divided into slices
     660        #uint32_t    sliceMode           #[in]: This parameter in conjunction with sliceModeData specifies the way in which the picture is divided into slices
    643661                                        #sliceMode = 0 MB based slices, sliceMode = 1 Byte based slices, sliceMode = 2 MB row based slices, sliceMode = 3, numSlices in Picture
    644662                                        #When forceIntraRefreshWithFrameCnt is set it will have priority over sliceMode setting
    645663                                        #When sliceMode == 0 and sliceModeData == 0 whole picture will be coded with one slice
    646         uint32_t    sliceModeData       #[in]: Specifies the parameter needed for sliceMode. For:
     664        #uint32_t    sliceModeData       #[in]: Specifies the parameter needed for sliceMode. For:
    647665                                        #sliceMode = 0, sliceModeData specifies # of MBs in each slice (except last slice)
    648666                                        #sliceMode = 1, sliceModeData specifies maximum # of bytes in each slice (except last slice)
    649667                                        #sliceMode = 2, sliceModeData specifies # of MB rows in each slice (except last slice)
     
    888906    #log.info("guidstr(%s)=%s", guid, s)
    889907    return s
    890908
    891 cdef GUID parseguid(s):
     909cdef GUID c_parseguid(src):
    892910    #just as ugly as above - shoot me now
    893911    #only this format is allowed:
    894912    sample_key = "CE788D20-AAA9-4318-92BB-AC7E858C8D36"
    895     if len(s)!=len(sample_key):
    896         raise Exception("invalid GUID format: expected %s characters but got %s" % (len(sample_key), len(s)))
     913    if len(src)!=len(sample_key):
     914        raise Exception("invalid GUID format: expected %s characters but got %s" % (len(sample_key), len(src)))
    897915    for i in range(len(sample_key)):
    898916        if sample_key[i]=="-":
    899917            #dash must be in the same place:
    900             if s[i]!="-":
     918            if src[i]!="-":
    901919                raise Exception("invalid GUID format: character at position %s is not '-'" % i)
    902920        else:
    903921            #must be an hex number:
    904             if s.upper()[i] not in ("0123456789ABCDEF"):
     922            if src.upper()[i] not in ("0123456789ABCDEF"):
    905923                raise Exception("invalid GUID format: character at position %s is not in hex" % i)
    906     parts = s.split("-")    #ie: ["CE788D20", "AAA9", ...]
     924    parts = src.split("-")    #ie: ["CE788D20", "AAA9", ...]
    907925    nparts = []
    908926    for i, s in (0, 4), (1, 2), (2, 2), (3, 2), (4, 6):
    909927        b = bytearray(binascii.unhexlify(parts[i]))
     
    920938        guid.Data4[i] = (v>>((7-i)*8)) % 256
    921939    return guid
    922940
     941def parseguid(s):
     942    return c_parseguid(s)
     943
    923944def test_parse():
    924945    sample_key = "CE788D20-AAA9-4318-92BB-AC7E858C8D36"
    925     x = parseguid(sample_key)
     946    x = c_parseguid(sample_key)
    926947    v = guidstr(x)
    927948    assert v==sample_key, "expected %s but got %s" % (sample_key, v)
    928949test_parse()
     
    931952memset(&CLIENT_KEY_GUID, 0, sizeof(GUID))
    932953if CLIENT_KEY:
    933954    try:
    934         CLIENT_KEY_GUID = parseguid(CLIENT_KEY)
     955        CLIENT_KEY_GUID = c_parseguid(CLIENT_KEY)
    935956    except Exception, e:
    936957        log.error("invalid client key specified: %s", e)
    937958
     
    952973        guidstr(NV_ENC_H264_PROFILE_HIGH_GUID)              : "high",
    953974        guidstr(NV_ENC_H264_PROFILE_STEREO_GUID)            : "stereo",
    954975        guidstr(NV_ENC_H264_PROFILE_SVC_TEMPORAL_SCALABILTY): "temporal",
    955         guidstr(NV_ENC_H264_PROFILE_CONSTRAINED_HIGH_GUID)  : "constrained-high",
     976        #guidstr(NV_ENC_H264_PROFILE_CONSTRAINED_HIGH_GUID)  : "constrained-high",
    956977        },
    957978    guidstr(NV_ENC_CODEC_MPEG2_GUID) : {
    958979        guidstr(NV_ENC_CODEC_PROFILE_AUTOSELECT_GUID)       : "auto",
     
    9801001    guidstr(NV_ENC_PRESET_HP_GUID)                          : "hp",
    9811002    guidstr(NV_ENC_PRESET_HQ_GUID)                          : "hq",
    9821003    guidstr(NV_ENC_PRESET_BD_GUID)                          : "bd",
    983     guidstr(NV_ENC_PRESET_LOW_LATENCY_DEFAULT_GUID)         : "low-latency",
    984     guidstr(NV_ENC_PRESET_LOW_LATENCY_HQ_GUID)              : "low-latency-hq",
    985     guidstr(NV_ENC_PRESET_LOW_LATENCY_HP_GUID)              : "low-latency-hp",
     1004    #V3 only:
     1005    #guidstr(NV_ENC_PRESET_LOW_LATENCY_DEFAULT_GUID)         : "low-latency",
     1006    #guidstr(NV_ENC_PRESET_LOW_LATENCY_HQ_GUID)              : "low-latency-hq",
     1007    #guidstr(NV_ENC_PRESET_LOW_LATENCY_HP_GUID)              : "low-latency-hp",
     1008    #V2 only:
     1009    guidstr(NV_ENC_PRESET_VC_GUID)                          : "vc",
     1010    guidstr(NV_ENC_PRESET_WIDI_GUID)                        : "widi",
     1011    guidstr(NV_ENC_PRESET_CAMERA_GUID)                      : "camera",
     1012    guidstr(NV_ENC_PRESET_AVCHD_GUID)                       : "avchd",
     1013    guidstr(NV_ENC_PRESET_IPOD_GUID)                        : "ipod",
     1014    guidstr(NV_ENC_PRESET_PSP_GUID)                         : "psp",
     1015    guidstr(NV_ENC_PRESET_GAME_CAPTURE_GUID)                : "game-capture",
     1016    guidstr(NV_ENC_PRESET_CLOUD_GAMING_720p60_GUID)         : "gaming 720p60",
     1017    guidstr(NV_ENC_PRESET_CLOUD_GAMING_720p30_GUID)         : "gaming 720p30",
     1018    guidstr(NV_ENC_PRESET_DESKTOP_CAPTURE_GUID)             : "desktop-capture",
     1019    guidstr(NV_ENC_PRESET_MVC_STEREO_GUID)                  : "mvc-stereo",
    9861020    }
    9871021
    9881022BUFFER_FORMAT = {
     
    10521086        debug(" max block sizes: (%s, %s, %s)", d.get_attribute(da.MAX_BLOCK_DIM_X), d.get_attribute(da.MAX_BLOCK_DIM_Y), d.get_attribute(da.MAX_BLOCK_DIM_Z))
    10531087        debug(" max grid sizes: (%s, %s, %s)", d.get_attribute(da.MAX_GRID_DIM_X), d.get_attribute(da.MAX_GRID_DIM_Y), d.get_attribute(da.MAX_GRID_DIM_Z))
    10541088        #SMmajor, SMminor = d.compute_cabability()
     1089        #FORCE flag here!
    10551090        SMmajor, SMminor = 0xFFFF, 0xFFFF
    10561091        has_nvenc = ((SMmajor<<4) + SMminor) >= 0x30
    10571092        pre = "-"
     
    11061141def get_BGRA2NV12():
    11071142    from xpra.codecs.nvenc.CUDA_rgb2nv12 import BGRA2NV12_kernel
    11081143    from pycuda.compiler import SourceModule
    1109     log.info("BGRA2NV12=%s", BGRA2NV12_kernel)
     1144    debug("BGRA2NV12=%s", BGRA2NV12_kernel)
    11101145    mod = SourceModule(BGRA2NV12_kernel)
    11111146    BGRA2NV12_function = mod.get_function("BGRA2NV12")
    11121147    return BGRA2NV12_function
    11131148
     1149API_V2_WARNING = False
    11141150
     1151
    11151152cdef class Encoder:
    11161153    cdef int width
    11171154    cdef int height
     
    11411178    cdef object preset_name
    11421179    cdef double time
    11431180    cdef int frames
     1181    cdef int api_warning
    11441182
    11451183    cdef GUID get_codec(self):
    11461184        codecs = self.query_codecs()
    11471185        #codecs={'H264': '6BC82762-4E63-4CA4-AA85-1E50F321F6BF'}
    11481186        assert self.codec_name in codecs, "%s not supported!?" % self.codec_name
    1149         return parseguid(codecs.get(self.codec_name))
     1187        return c_parseguid(codecs.get(self.codec_name))
    11501188
    11511189    cdef GUID get_preset(self, GUID codec):
    11521190        #PRESET:
    11531191        presets = self.query_presets(codec)
    11541192        #presets={'low-latency': '49DF21C5-6DFA-4FEB-9787-6ACC9EFFB726', 'bd': '82E3E450-BDBB-4E40-989C-82A90DF9EF32', 'default': 'B2DFB705-4EBD-4C49-9B5F-24A777D3E587', 'hp': '60E4C59F-E846-4484-A56D-CD45BE9FDDF6', 'hq': '34DBA71D-A77B-4B8F-9C3E-B6D5DA24C012', 'low-latency-hp': '67082A44-4BAD-48FA-98EA-93056D150A58', 'low-latency-hq': 'C5F733B9-EA97-4CF9-BEC2-BF78A74FD105'}
    11551193        self.preset_name = None
    1156         for x in ("low-latency-hq", "low-latency", "low-latency-hp"):
     1194        DESIRED_PRESET_LIST = (
     1195                  #in V3, the presets are called "low-latency":
     1196                  "low-latency-hq", "low-latency", "low-latency-hp",
     1197                  #in V2, they are called gaming:
     1198                  "game-capture", "gaming 720p60", "gaming 720p30")
     1199        for x in DESIRED_PRESET_LIST:
    11571200            if x in presets:
    11581201                self.preset_name = x
    1159                 return parseguid(presets.get(x))
     1202                return c_parseguid(presets.get(x))
    11601203        raise Exception("no low-latency presets available for '%s'!?" % self.codec_name)
    11611204
    11621205
     
    12381281        input_formats = self.query_input_formats(codec)
    12391282        assert input_format in input_formats, "%s does not support %s (only: %s)" %  (self.codec_name, input_format, input_formats)
    12401283        try:
    1241             presetConfig = self.get_preset_config(codec, preset)
     1284            presetConfig = self.get_preset_config(self.preset_name, codec, preset)
    12421285
    12431286            #PROFILE
    12441287            profiles = self.query_profiles(NV_ENC_CODEC_H264_GUID)
    1245             presetConfig.presetCfg.encodeCodecConfig.h264Config.enableVFR = 1
    12461288            #self.gopLength = presetConfig.presetCfg.gopLength
    12471289
    12481290            memset(&params, 0, sizeof(NV_ENC_INITIALIZE_PARAMS))
     
    12551297            params.darHeight = self.encoder_height
    12561298            params.enableEncodeAsync = 0            #not supported on Linux
    12571299            params.enablePTD = 0                    #not supported in sync mode!?
    1258             params.encodeConfig = &presetConfig.presetCfg
     1300            if presetConfig!=NULL:
     1301                #presetConfig.presetCfg.encodeCodecConfig.h264Config.enableVFR = 1
     1302                params.encodeConfig = &presetConfig.presetCfg
    12591303            raiseNVENC(self.functionList.nvEncInitializeEncoder(self.context, &params), "initializing encoder")
    12601304            debug("NVENC initialized with '%s' codec and '%s' preset" % (self.codec_name, self.preset_name))
    12611305
     
    12821326            self.bitstreamBuffer = createBitstreamBufferParams.bitstreamBuffer
    12831327            debug("bitstreamBuffer=%s", hex(<long> self.bitstreamBuffer))
    12841328        finally:
    1285             free(presetConfig)
     1329            if presetConfig!=NULL:
     1330                free(presetConfig)
    12861331
    12871332    def get_info(self):
    12881333        cdef float pps
     
    14531498                picParams.pictureType = NV_ENC_PIC_TYPE_P
    14541499            picParams.codecPicParams.h264PicParams.displayPOCSyntax = 2*self.frames
    14551500            picParams.codecPicParams.h264PicParams.refPicFlag = 1
    1456             picParams.codecPicParams.h264PicParams.sliceMode = 3            #sliceModeData specifies the number of slices
    1457             picParams.codecPicParams.h264PicParams.sliceModeData = 1        #1 slice!
     1501            #picParams.codecPicParams.h264PicParams.sliceMode = 3            #sliceModeData specifies the number of slices
     1502            #picParams.codecPicParams.h264PicParams.sliceModeData = 1        #1 slice!
    14581503            #picParams.encodePicFlags = NV_ENC_PIC_FLAG_FORCEINTRA
    14591504            #picParams.inputTimeStamp = int(1000.0 * time.time())
    14601505            #inputDuration = 0      #FIXME: use frame delay?
     
    14901535        return pixels, {}
    14911536
    14921537
    1493     cdef NV_ENC_PRESET_CONFIG *get_preset_config(self, GUID encode_GUID, GUID preset_GUID):
     1538    cdef NV_ENC_PRESET_CONFIG *get_preset_config(self, name, GUID encode_GUID, GUID preset_GUID):
    14941539        """ you must free it after use! """
    14951540        cdef NV_ENC_PRESET_CONFIG *presetConfig     #@DuplicatedSignature
     1541        cdef int ret
    14961542        presetConfig = <NV_ENC_PRESET_CONFIG*> malloc(sizeof(NV_ENC_PRESET_CONFIG))
    14971543        assert presetConfig!=NULL, "failed to allocate memory for preset config"
    14981544        memset(presetConfig, 0, sizeof(NV_ENC_PRESET_CONFIG))
    14991545        presetConfig.version = NV_ENC_PRESET_CONFIG_VER
    1500         raiseNVENC(self.functionList.nvEncGetEncodePresetConfig(self.context, encode_GUID, preset_GUID, presetConfig), "getting preset config for %s" % guidstr(preset_GUID))
     1546        ret = self.functionList.nvEncGetEncodePresetConfig(self.context, encode_GUID, preset_GUID, presetConfig)
     1547        if ret!=0:
     1548            global API_V2_WARNING
     1549            #API version 2.0 fails every time
     1550            #we call nvEncGetEncodePresetConfig and I don't know why
     1551            #so warn just once:
     1552            if NVENCAPI_VERSION==0x20:
     1553                debug("failed to get preset config for %s", name)
     1554                if not API_V2_WARNING:
     1555                    log.warn("API version %s fails on nvEncGetEncodePresetConfig (no further warnings will be shown)", hex(NVENCAPI_VERSION))
     1556                API_V2_WARNING = True
     1557            else:
     1558                log.warn("failed to get preset config for %s (%s / %s): %s", name, guidstr(encode_GUID), guidstr(preset_GUID), NV_ENC_STATUS_TXT.get(ret, ret))
     1559            return NULL
    15011560        return presetConfig
    15021561
    15031562    cdef object query_presets(self, GUID encode_GUID):
     
    15211580                preset_GUID = preset_GUIDs[x]
    15221581                preset_name = CODEC_PRESETS_GUIDS.get(guidstr(preset_GUID))
    15231582                debug("* %s : %s", guidstr(preset_GUID), preset_name)
    1524                 try:
    1525                     presetConfig = self.get_preset_config(encode_GUID, preset_GUID)
    1526                     encConfig = presetConfig.presetCfg
    1527                     debug("   gopLength=%s, frameIntervalP=%s", encConfig.gopLength, encConfig.frameIntervalP)
    1528                 finally:
    1529                     free(presetConfig)
    1530                 presets[preset_name] = guidstr(preset_GUID)
     1583                presetConfig = self.get_preset_config(preset_name, encode_GUID, preset_GUID)
     1584                if presetConfig!=NULL:
     1585                    try:
     1586                        encConfig = presetConfig.presetCfg
     1587                        #debug("presetConfig.presetCfg=%s", <long> encConfig)
     1588                        debug("   gopLength=%s, frameIntervalP=%s", encConfig.gopLength, encConfig.frameIntervalP)
     1589                    finally:
     1590                        free(presetConfig)
     1591                if preset_name is None:
     1592                    log.warn("unknown preset found: %s", guidstr(preset_GUID))
     1593                else:
     1594                    presets[preset_name] = guidstr(preset_GUID)
    15311595        finally:
    15321596            free(preset_GUIDs)
     1597        debug("query_presets(%s)=%s", guidstr(encode_GUID), presets)
    15331598        return presets
    15341599
    15351600    cdef object query_profiles(self, GUID encode_GUID):
     
    16461711        memset(&self.functionList, 0, sizeof(NV_ENCODE_API_FUNCTION_LIST))
    16471712        self.functionList.version = NV_ENCODE_API_FUNCTION_LIST_VER
    16481713        raiseNVENC(NvEncodeAPICreateInstance(&self.functionList), "getting API function list")
    1649         assert self.functionList.nvEncOpenEncodeSessionEx!=NULL, "looks like NvEncodeAPICreateInstance failed!"
    16501714
    16511715        #get the CUDA context (C pointer):
    16521716        cdef CUcontext cuda_context
     
    16541718        result = cuCtxGetCurrent(&cuda_context)
    16551719        assert result==0, "failed to get current cuda context"
    16561720
    1657         #NVENC init:
    1658         memset(&params, 0, sizeof(NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS))
    1659         params.version = NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS_VER
    1660         params.deviceType = NV_ENC_DEVICE_TYPE_CUDA
    1661         params.device = <void*> cuda_context
    1662         params.clientKeyPtr = &CLIENT_KEY_GUID
    1663         params.apiVersion = NVENCAPI_VERSION
    1664         #params.clientKeyPtr = client_key
    1665         debug("calling nvEncOpenEncodeSessionEx @ %s", hex(<long> self.functionList.nvEncOpenEncodeSessionEx))
    1666         raiseNVENC(self.functionList.nvEncOpenEncodeSessionEx(&params, &self.context), "opening session")
     1721        if USE_OPENENCODESESSIONEX:
     1722            assert self.functionList.nvEncOpenEncodeSessionEx!=NULL, "looks like NvEncodeAPICreateInstance failed!"
     1723            #NVENC init:
     1724            memset(&params, 0, sizeof(NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS))
     1725            params.version = NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS_VER
     1726            params.deviceType = NV_ENC_DEVICE_TYPE_CUDA
     1727            params.device = <void*> cuda_context
     1728            params.clientKeyPtr = &CLIENT_KEY_GUID
     1729            params.apiVersion = NVENCAPI_VERSION
     1730            #params.clientKeyPtr = client_key
     1731            debug("calling nvEncOpenEncodeSessionEx @ %s", hex(<long> self.functionList.nvEncOpenEncodeSessionEx))
     1732            raiseNVENC(self.functionList.nvEncOpenEncodeSessionEx(&params, &self.context), "opening session")
     1733        else:
     1734            assert self.functionList.nvEncOpenEncodeSession!=NULL, "looks like NvEncodeAPICreateInstance failed!"
     1735            #ctypedef NVENCSTATUS (*PNVENCOPENENCODESESSION)         (void* device, uint32_t deviceType, void** encoder)
     1736            raiseNVENC(self.functionList.nvEncOpenEncodeSession(<void*> cuda_context, NV_ENC_DEVICE_TYPE_CUDA, &self.context), "opening session")
     1737
    16671738        debug("success, encoder context=%s", hex(<long> self.context))
    16681739
    16691740