History

avcodec/cuviddec: correctly handle buffer size and status when deinterlacing

Timo Rothenpieler committed 1 year ago

Commit 99e2af41 parent 330c8f81 file · +13 −11Browse files

Unified · Split

libavcodec/cuviddec.c+13−11
@@ -131,7 +131,7 @@ static int CUDAAPI cuvid_handle_video_sequence(void *opaque, CUVIDEOFORMAT* form · 130 unmodified lines
131131 CUVIDDECODECREATEINFO cuinfo;
132132 int surface_fmt;
133133 int chroma_444;
134− int fifo_size_inc;
134+ int old_nb_surfaces, fifo_size_inc, fifo_size_mul = 1;
135135
136136 int old_width = avctx->width;
137137 int old_height = avctx->height;
@@ -349,20 +349,24 @@ static int CUDAAPI cuvid_handle_video_sequence(void *opaque, CUVIDEOFORMAT* form · 211 unmodified lines
349349 return 0;
350350 }
351351
352− fifo_size_inc = ctx->nb_surfaces;
353− ctx->nb_surfaces = FFMAX(ctx->nb_surfaces, format->min_num_decode_surfaces + 3);
352+ if (ctx->deint_mode_current != cudaVideoDeinterlaceMode_Weave && !ctx->drop_second_field) {
353+ avctx->framerate = av_mul_q(avctx->framerate, (AVRational){2, 1});
354+ fifo_size_mul = 2;
355+ }
354356
357+ old_nb_surfaces = ctx->nb_surfaces;
358+ ctx->nb_surfaces = FFMAX(ctx->nb_surfaces, format->min_num_decode_surfaces + 3);
355359 if (avctx->extra_hw_frames > 0)
356360 ctx->nb_surfaces += avctx->extra_hw_frames;
357361
358− fifo_size_inc = ctx->nb_surfaces - fifo_size_inc;
362+ fifo_size_inc = ctx->nb_surfaces * fifo_size_mul - av_fifo_can_read(ctx->frame_queue) - av_fifo_can_write(ctx->frame_queue);
359363 if (fifo_size_inc > 0 && av_fifo_grow2(ctx->frame_queue, fifo_size_inc) < 0) {
360364 av_log(avctx, AV_LOG_ERROR, "Failed to grow frame queue on video sequence callback\n");
361365 ctx->internal_error = AVERROR(ENOMEM);
362366 return 0;
363367 }
364368
365− if (fifo_size_inc > 0 && av_reallocp_array(&ctx->key_frame, ctx->nb_surfaces, sizeof(int)) < 0) {
369+ if (ctx->nb_surfaces > old_nb_surfaces && av_reallocp_array(&ctx->key_frame, ctx->nb_surfaces, sizeof(int)) < 0) {
366370 av_log(avctx, AV_LOG_ERROR, "Failed to grow key frame array on video sequence callback\n");
367371 ctx->internal_error = AVERROR(ENOMEM);
368372 return 0;
@@ -374,9 +378,6 @@ static int CUDAAPI cuvid_handle_video_sequence(void *opaque, CUVIDEOFORMAT* form · 5 unmodified lines
374378 cuinfo.bitDepthMinus8 = format->bit_depth_luma_minus8;
375379 cuinfo.DeinterlaceMode = ctx->deint_mode_current;
376380
377− if (ctx->deint_mode_current != cudaVideoDeinterlaceMode_Weave && !ctx->drop_second_field)
378− avctx->framerate = av_mul_q(avctx->framerate, (AVRational){2, 1});
379−
380381 ctx->internal_error = CHECK_CU(ctx->cvdl->cuvidCreateDecoder(&ctx->cudecoder, &cuinfo));
381382 if (ctx->internal_error < 0)
382383 return 0;
@@ -448,11 +449,12 @@ static int cuvid_is_buffer_full(AVCodecContext *avctx) · 65 unmodified lines
448449 {
449450 CuvidContext *ctx = avctx->priv_data;
450451
451− int delay = ctx->cuparseinfo.ulMaxDisplayDelay;
452+ int shift = 0;
452453 if (ctx->deint_mode != cudaVideoDeinterlaceMode_Weave && !ctx->drop_second_field)
453− delay *= 2;
454+ shift = 1;
454455
455− return av_fifo_can_read(ctx->frame_queue) + delay >= ctx->nb_surfaces;
456+ // shift/divide frame count to ensure the buffer is still signalled full if one half-frame has already been returned when deinterlacing.
457+ return ((av_fifo_can_read(ctx->frame_queue) + shift) >> shift) + ctx->cuparseinfo.ulMaxDisplayDelay >= ctx->nb_surfaces;
456458 }
457459
458460 static int cuvid_decode_packet(AVCodecContext *avctx, const AVPacket *avpkt)

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t