From dad0617556cf68f03d730e2a2823d96973f506ec Mon Sep 17 00:00:00 2001 From: Tobias Gahleitner Date: Wed, 4 May 2022 12:02:28 +0200 Subject: [PATCH 1/4] made framerate available for python --- nvcodec-python.cpp | 8 ++++---- src/encoder.cpp | 7 ++++--- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/nvcodec-python.cpp b/nvcodec-python.cpp index 52695f4..b54e798 100644 --- a/nvcodec-python.cpp +++ b/nvcodec-python.cpp @@ -256,12 +256,12 @@ static PyObject* VideoEncoder_Repr(NvCodec* Self) static void VideoEncoder_init(NvCodec* Self, PyObject* pArgs) { - unsigned int width,height; - if(!PyArg_ParseTuple(pArgs, "II", &width, &height)){ - PyErr_SetString(PyExc_ValueError, "Parse the argument FAILED! You should pass width and height!"); + unsigned int width,height,frameRate; + if(!PyArg_ParseTuple(pArgs, "III", &width, &height, &frameRate)){ + PyErr_SetString(PyExc_ValueError, "Parse the argument FAILED! You should pass width, height and framerate!"); return; } - Self->m_handle = (long long)(videoEncoder_init(width, height)); + Self->m_handle = (long long)(videoEncoder_init(width, height, frameRate)); } static PyTypeObject VideoEncoder_ClassInfo = diff --git a/src/encoder.cpp b/src/encoder.cpp index 47587f3..b46e82c 100644 --- a/src/encoder.cpp +++ b/src/encoder.cpp @@ -9,12 +9,13 @@ #define ENC(handle) ((NvEncoderCuda*)(handle->enc)) -void _InitializeEncoder(NvEncoder* pEnc, NvEncoderInitParam encodeCLIOptions, NV_ENC_BUFFER_FORMAT eFormat) +void _InitializeEncoder(NvEncoder* pEnc, NvEncoderInitParam encodeCLIOptions, NV_ENC_BUFFER_FORMAT eFormat, int frameRate) { NV_ENC_INITIALIZE_PARAMS initializeParams = { NV_ENC_INITIALIZE_PARAMS_VER }; NV_ENC_CONFIG encodeConfig = { NV_ENC_CONFIG_VER }; initializeParams.encodeConfig = &encodeConfig; + initializeParams.frameRateDen = frameRate; pEnc->CreateDefaultEncoderParams(&initializeParams, encodeCLIOptions.GetEncodeGUID(), encodeCLIOptions.GetPresetGUID(), encodeCLIOptions.GetTuningInfo()); encodeCLIOptions.SetInitParams(&initializeParams, eFormat); @@ -23,7 +24,7 @@ void _InitializeEncoder(NvEncoder* pEnc, NvEncoderInitParam encodeCLIOptions, NV } -videoEncoderHandle videoEncoder_init(int width, int height){ +videoEncoderHandle videoEncoder_init(int width, int height, int frameRate){ videoEncoderHandle handle = (videoEncoderHandle)malloc(sizeof(videoEncoder)); ck(cuInit(0)); handle->cuContext = nullptr; @@ -32,7 +33,7 @@ videoEncoderHandle videoEncoder_init(int width, int height){ NV_ENC_BUFFER_FORMAT eFormat = NV_ENC_BUFFER_FORMAT_ARGB; NvEncoderInitParam encodeCLIOptions; - _InitializeEncoder(ENC(handle), encodeCLIOptions, eFormat); + _InitializeEncoder(ENC(handle), encodeCLIOptions, eFormat, frameRate); return handle; } From 1a5750128251d4a855f3d248c673e9bcefb54863 Mon Sep 17 00:00:00 2001 From: twotothreebeers <104898523+twotothreebeers@users.noreply.github.com> Date: Wed, 4 May 2022 12:54:15 +0200 Subject: [PATCH 2/4] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 4f83187..bedf65b 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,6 @@ frames = decoder.decode(h264_data, 1) #### 3. Use VideoEncoder ```python -encoder = VideoEncoder(width, height) +encoder = VideoEncoder(width, height, frameRate) h264_data = encoder.encode(frame) ``` From afa4a5a2b5917df83d87f435598a7d6d5c17bba4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20Gr=C3=B6mer?= Date: Thu, 5 May 2022 08:56:42 +0000 Subject: [PATCH 3/4] Add encoder cli params --- nvcodec-python.cpp | 7 ++++--- src/encoder.cpp | 9 ++++----- src/encoder.h | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/nvcodec-python.cpp b/nvcodec-python.cpp index b54e798..660f9b0 100644 --- a/nvcodec-python.cpp +++ b/nvcodec-python.cpp @@ -256,12 +256,13 @@ static PyObject* VideoEncoder_Repr(NvCodec* Self) static void VideoEncoder_init(NvCodec* Self, PyObject* pArgs) { - unsigned int width,height,frameRate; - if(!PyArg_ParseTuple(pArgs, "III", &width, &height, &frameRate)){ + unsigned int width,height; + char* params; + if(!PyArg_ParseTuple(pArgs, "IIs", &width, &height, ¶ms)){ PyErr_SetString(PyExc_ValueError, "Parse the argument FAILED! You should pass width, height and framerate!"); return; } - Self->m_handle = (long long)(videoEncoder_init(width, height, frameRate)); + Self->m_handle = (long long)(videoEncoder_init(width, height, params)); } static PyTypeObject VideoEncoder_ClassInfo = diff --git a/src/encoder.cpp b/src/encoder.cpp index b46e82c..1ae086a 100644 --- a/src/encoder.cpp +++ b/src/encoder.cpp @@ -9,13 +9,12 @@ #define ENC(handle) ((NvEncoderCuda*)(handle->enc)) -void _InitializeEncoder(NvEncoder* pEnc, NvEncoderInitParam encodeCLIOptions, NV_ENC_BUFFER_FORMAT eFormat, int frameRate) +void _InitializeEncoder(NvEncoder* pEnc, NvEncoderInitParam encodeCLIOptions, NV_ENC_BUFFER_FORMAT eFormat) { NV_ENC_INITIALIZE_PARAMS initializeParams = { NV_ENC_INITIALIZE_PARAMS_VER }; NV_ENC_CONFIG encodeConfig = { NV_ENC_CONFIG_VER }; initializeParams.encodeConfig = &encodeConfig; - initializeParams.frameRateDen = frameRate; pEnc->CreateDefaultEncoderParams(&initializeParams, encodeCLIOptions.GetEncodeGUID(), encodeCLIOptions.GetPresetGUID(), encodeCLIOptions.GetTuningInfo()); encodeCLIOptions.SetInitParams(&initializeParams, eFormat); @@ -24,7 +23,7 @@ void _InitializeEncoder(NvEncoder* pEnc, NvEncoderInitParam encodeCLIOptions, NV } -videoEncoderHandle videoEncoder_init(int width, int height, int frameRate){ +videoEncoderHandle videoEncoder_init(int width, int height, char* params){ videoEncoderHandle handle = (videoEncoderHandle)malloc(sizeof(videoEncoder)); ck(cuInit(0)); handle->cuContext = nullptr; @@ -32,8 +31,8 @@ videoEncoderHandle videoEncoder_init(int width, int height, int frameRate){ handle->enc = new NvEncoderCuda(handle->cuContext, width, height, NV_ENC_BUFFER_FORMAT_ARGB); NV_ENC_BUFFER_FORMAT eFormat = NV_ENC_BUFFER_FORMAT_ARGB; - NvEncoderInitParam encodeCLIOptions; - _InitializeEncoder(ENC(handle), encodeCLIOptions, eFormat, frameRate); + NvEncoderInitParam encodeCLIOptions(params); + _InitializeEncoder(ENC(handle), encodeCLIOptions, eFormat); return handle; } diff --git a/src/encoder.h b/src/encoder.h index fec3a3a..6ebf581 100644 --- a/src/encoder.h +++ b/src/encoder.h @@ -30,7 +30,7 @@ typedef struct }videoEncodedBuffer; -videoEncoderHandle videoEncoder_init(int width, int height); +videoEncoderHandle videoEncoder_init(int width, int height, char* params); int videoEncoder_destroy(videoEncoderHandle handle); videoEncodedBuffer* videoEncoder_encode(videoEncoderHandle handle, u_int8_t* in); videoEncodedBuffer* videoEncoder_encode_end(videoEncoderHandle handle); From 08ae618c0a3c46e50ef254b46880791cd57b067c Mon Sep 17 00:00:00 2001 From: twotothreebeers <104898523+twotothreebeers@users.noreply.github.com> Date: Thu, 5 May 2022 11:02:02 +0200 Subject: [PATCH 4/4] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index bedf65b..7a95a14 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,6 @@ frames = decoder.decode(h264_data, 1) #### 3. Use VideoEncoder ```python -encoder = VideoEncoder(width, height, frameRate) +encoder = VideoEncoder(width, height, cliParams) h264_data = encoder.encode(frame) ```