Skip to content

Commit

Permalink
[*] fixed packet loss at EOS. fixed issue mentioned in jocover#131
Browse files Browse the repository at this point in the history
  • Loading branch information
Keylost committed Mar 23, 2023
1 parent 9a724aa commit 8bb3200
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 66 deletions.
2 changes: 1 addition & 1 deletion ffmpeg_dev/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/ffmpeg4.2
/ffmpeg4.4
/ffmpeg6.0

/*custom.sh
21 changes: 12 additions & 9 deletions ffmpeg_dev/common/libavcodec/nvmpi_enc.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ static int ff_nvmpi_send_frame(AVCodecContext *avctx,const AVFrame *frame)
if (nvmpi_context->encoder_flushing)
return AVERROR_EOF;

if(frame){

if(frame)
{
_nvframe.payload[0]=frame->data[0];
_nvframe.payload[1]=frame->data[1];
_nvframe.payload[2]=frame->data[2];
Expand All @@ -162,7 +162,10 @@ static int ff_nvmpi_send_frame(AVCodecContext *avctx,const AVFrame *frame)
return res;
}
else
{
nvmpi_context->encoder_flushing = 1;
nvmpi_encoder_put_frame(nvmpi_context->ctx,NULL);
}

return 0;
}
Expand All @@ -172,12 +175,12 @@ static int ff_nvmpi_receive_packet(AVCodecContext *avctx, AVPacket *pkt)
nvmpiEncodeContext * nvmpi_context = avctx->priv_data;
nvPacket packet={0};
int res;

if(nvmpi_encoder_get_packet(nvmpi_context->ctx,&packet)<0)

res = nvmpi_encoder_get_packet(nvmpi_context->ctx,&packet);
if(res<0)
{
if (nvmpi_context->encoder_flushing)
return AVERROR_EOF; //we don't really wait so we may miss some last packets

//If the encoder is in flushing state, then get_packet will block and return either a packet or EOF
if(nvmpi_context->encoder_flushing) return AVERROR_EOF;
return AVERROR(EAGAIN); //nvmpi get_packet returns -1 if no packets are pending
}
#if LIBAVCODEC_VERSION_MAJOR >= 60
Expand Down Expand Up @@ -219,8 +222,8 @@ static int ff_nvmpi_encode_frame(AVCodecContext *avctx, AVPacket *pkt,const AVFr
return 0;
}

static av_cold int nvmpi_encode_close(AVCodecContext *avctx){

static av_cold int nvmpi_encode_close(AVCodecContext *avctx)
{
nvmpiEncodeContext *nvmpi_context = avctx->priv_data;
nvmpi_encoder_close(nvmpi_context->ctx);

Expand Down
25 changes: 14 additions & 11 deletions ffmpeg_patches/ffmpeg4.2_nvmpi.patch
Original file line number Diff line number Diff line change
Expand Up @@ -385,10 +385,10 @@ index 0000000..9cfd876
+
diff --git a/libavcodec/nvmpi_enc.c b/libavcodec/nvmpi_enc.c
new file mode 100644
index 0000000..c28a6a5
index 0000000..b445f86
--- /dev/null
+++ b/libavcodec/nvmpi_enc.c
@@ -0,0 +1,346 @@
@@ -0,0 +1,349 @@
+#include <nvmpi.h>
+#include "avcodec.h"
+#include "internal.h"
Expand Down Expand Up @@ -531,8 +531,8 @@ index 0000000..c28a6a5
+ if (nvmpi_context->encoder_flushing)
+ return AVERROR_EOF;
+
+ if(frame){
+
+ if(frame)
+ {
+ _nvframe.payload[0]=frame->data[0];
+ _nvframe.payload[1]=frame->data[1];
+ _nvframe.payload[2]=frame->data[2];
Expand All @@ -553,7 +553,10 @@ index 0000000..c28a6a5
+ return res;
+ }
+ else
+ {
+ nvmpi_context->encoder_flushing = 1;
+ nvmpi_encoder_put_frame(nvmpi_context->ctx,NULL);
+ }
+
+ return 0;
+}
Expand All @@ -563,12 +566,12 @@ index 0000000..c28a6a5
+ nvmpiEncodeContext * nvmpi_context = avctx->priv_data;
+ nvPacket packet={0};
+ int res;
+
+ if(nvmpi_encoder_get_packet(nvmpi_context->ctx,&packet)<0)
+
+ res = nvmpi_encoder_get_packet(nvmpi_context->ctx,&packet);
+ if(res<0)
+ {
+ if (nvmpi_context->encoder_flushing)
+ return AVERROR_EOF; //we don't really wait so we may miss some last packets
+
+ //If the encoder is in flushing state, then get_packet will block and return either a packet or EOF
+ if(nvmpi_context->encoder_flushing) return AVERROR_EOF;
+ return AVERROR(EAGAIN); //nvmpi get_packet returns -1 if no packets are pending
+ }
+#if LIBAVCODEC_VERSION_MAJOR >= 60
Expand Down Expand Up @@ -610,8 +613,8 @@ index 0000000..c28a6a5
+ return 0;
+}
+
+static av_cold int nvmpi_encode_close(AVCodecContext *avctx){
+
+static av_cold int nvmpi_encode_close(AVCodecContext *avctx)
+{
+ nvmpiEncodeContext *nvmpi_context = avctx->priv_data;
+ nvmpi_encoder_close(nvmpi_context->ctx);
+
Expand Down
25 changes: 14 additions & 11 deletions ffmpeg_patches/ffmpeg4.4_nvmpi.patch
Original file line number Diff line number Diff line change
Expand Up @@ -388,10 +388,10 @@ index 0000000..9cfd876
+
diff --git a/libavcodec/nvmpi_enc.c b/libavcodec/nvmpi_enc.c
new file mode 100644
index 0000000..c28a6a5
index 0000000..b445f86
--- /dev/null
+++ b/libavcodec/nvmpi_enc.c
@@ -0,0 +1,346 @@
@@ -0,0 +1,349 @@
+#include <nvmpi.h>
+#include "avcodec.h"
+#include "internal.h"
Expand Down Expand Up @@ -534,8 +534,8 @@ index 0000000..c28a6a5
+ if (nvmpi_context->encoder_flushing)
+ return AVERROR_EOF;
+
+ if(frame){
+
+ if(frame)
+ {
+ _nvframe.payload[0]=frame->data[0];
+ _nvframe.payload[1]=frame->data[1];
+ _nvframe.payload[2]=frame->data[2];
Expand All @@ -556,7 +556,10 @@ index 0000000..c28a6a5
+ return res;
+ }
+ else
+ {
+ nvmpi_context->encoder_flushing = 1;
+ nvmpi_encoder_put_frame(nvmpi_context->ctx,NULL);
+ }
+
+ return 0;
+}
Expand All @@ -566,12 +569,12 @@ index 0000000..c28a6a5
+ nvmpiEncodeContext * nvmpi_context = avctx->priv_data;
+ nvPacket packet={0};
+ int res;
+
+ if(nvmpi_encoder_get_packet(nvmpi_context->ctx,&packet)<0)
+
+ res = nvmpi_encoder_get_packet(nvmpi_context->ctx,&packet);
+ if(res<0)
+ {
+ if (nvmpi_context->encoder_flushing)
+ return AVERROR_EOF; //we don't really wait so we may miss some last packets
+
+ //If the encoder is in flushing state, then get_packet will block and return either a packet or EOF
+ if(nvmpi_context->encoder_flushing) return AVERROR_EOF;
+ return AVERROR(EAGAIN); //nvmpi get_packet returns -1 if no packets are pending
+ }
+#if LIBAVCODEC_VERSION_MAJOR >= 60
Expand Down Expand Up @@ -613,8 +616,8 @@ index 0000000..c28a6a5
+ return 0;
+}
+
+static av_cold int nvmpi_encode_close(AVCodecContext *avctx){
+
+static av_cold int nvmpi_encode_close(AVCodecContext *avctx)
+{
+ nvmpiEncodeContext *nvmpi_context = avctx->priv_data;
+ nvmpi_encoder_close(nvmpi_context->ctx);
+
Expand Down
25 changes: 14 additions & 11 deletions ffmpeg_patches/ffmpeg6.0_nvmpi.patch
Original file line number Diff line number Diff line change
Expand Up @@ -389,10 +389,10 @@ index 0000000..9cfd876
+
diff --git a/libavcodec/nvmpi_enc.c b/libavcodec/nvmpi_enc.c
new file mode 100644
index 0000000..c28a6a5
index 0000000..b445f86
--- /dev/null
+++ b/libavcodec/nvmpi_enc.c
@@ -0,0 +1,346 @@
@@ -0,0 +1,349 @@
+#include <nvmpi.h>
+#include "avcodec.h"
+#include "internal.h"
Expand Down Expand Up @@ -535,8 +535,8 @@ index 0000000..c28a6a5
+ if (nvmpi_context->encoder_flushing)
+ return AVERROR_EOF;
+
+ if(frame){
+
+ if(frame)
+ {
+ _nvframe.payload[0]=frame->data[0];
+ _nvframe.payload[1]=frame->data[1];
+ _nvframe.payload[2]=frame->data[2];
Expand All @@ -557,7 +557,10 @@ index 0000000..c28a6a5
+ return res;
+ }
+ else
+ {
+ nvmpi_context->encoder_flushing = 1;
+ nvmpi_encoder_put_frame(nvmpi_context->ctx,NULL);
+ }
+
+ return 0;
+}
Expand All @@ -567,12 +570,12 @@ index 0000000..c28a6a5
+ nvmpiEncodeContext * nvmpi_context = avctx->priv_data;
+ nvPacket packet={0};
+ int res;
+
+ if(nvmpi_encoder_get_packet(nvmpi_context->ctx,&packet)<0)
+
+ res = nvmpi_encoder_get_packet(nvmpi_context->ctx,&packet);
+ if(res<0)
+ {
+ if (nvmpi_context->encoder_flushing)
+ return AVERROR_EOF; //we don't really wait so we may miss some last packets
+
+ //If the encoder is in flushing state, then get_packet will block and return either a packet or EOF
+ if(nvmpi_context->encoder_flushing) return AVERROR_EOF;
+ return AVERROR(EAGAIN); //nvmpi get_packet returns -1 if no packets are pending
+ }
+#if LIBAVCODEC_VERSION_MAJOR >= 60
Expand Down Expand Up @@ -614,8 +617,8 @@ index 0000000..c28a6a5
+ return 0;
+}
+
+static av_cold int nvmpi_encode_close(AVCodecContext *avctx){
+
+static av_cold int nvmpi_encode_close(AVCodecContext *avctx)
+{
+ nvmpiEncodeContext *nvmpi_context = avctx->priv_data;
+ nvmpi_encoder_close(nvmpi_context->ctx);
+
Expand Down
Loading

0 comments on commit 8bb3200

Please sign in to comment.