[FFmpeg-devel,4/5] avutil/hwcontext_cuda: explicitly synchronize cuMemcpy calls

Submitted by Timo Rothenpieler on May 8, 2018, 6:36 p.m.

Details

Message ID 20180508183640.13605-4-timo@rothenpieler.org
State New
Headers show

Commit Message

Timo Rothenpieler May 8, 2018, 6:36 p.m.
---
 libavutil/hwcontext_cuda.c | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)

Patch hide | download patch | download mbox

diff --git a/libavutil/hwcontext_cuda.c b/libavutil/hwcontext_cuda.c
index f3e81680e9..668293bffc 100644
--- a/libavutil/hwcontext_cuda.c
+++ b/libavutil/hwcontext_cuda.c
@@ -258,13 +258,19 @@  static int cuda_transfer_data_from(AVHWFramesContext *ctx, AVFrame *dst,
             .Height        = src->height >> (i ? priv->shift_height : 0),
         };
 
-        err = cu->cuMemcpy2D(&cpy);
+        err = cu->cuMemcpy2DAsync(&cpy, device_hwctx->stream);
         if (err != CUDA_SUCCESS) {
             av_log(ctx, AV_LOG_ERROR, "Error transferring the data from the CUDA frame\n");
             return AVERROR_UNKNOWN;
         }
     }
 
+    err = cu->cuStreamSynchronize(device_hwctx->stream);
+    if (err != CUDA_SUCCESS) {
+        av_log(ctx, AV_LOG_ERROR, "Error synchronizing CUDA stream\n");
+        return AVERROR_UNKNOWN;
+    }
+
     cu->cuCtxPopCurrent(&dummy);
 
     return 0;
@@ -297,13 +303,19 @@  static int cuda_transfer_data_to(AVHWFramesContext *ctx, AVFrame *dst,
             .Height        = src->height >> (i ? priv->shift_height : 0),
         };
 
-        err = cu->cuMemcpy2D(&cpy);
+        err = cu->cuMemcpy2DAsync(&cpy, device_hwctx->stream);
         if (err != CUDA_SUCCESS) {
-            av_log(ctx, AV_LOG_ERROR, "Error transferring the data from the CUDA frame\n");
+            av_log(ctx, AV_LOG_ERROR, "Error transferring the data to the CUDA frame\n");
             return AVERROR_UNKNOWN;
         }
     }
 
+    err = cu->cuStreamSynchronize(device_hwctx->stream);
+    if (err != CUDA_SUCCESS) {
+        av_log(ctx, AV_LOG_ERROR, "Error synchronizing CUDA stream\n");
+        return AVERROR_UNKNOWN;
+    }
+
     cu->cuCtxPopCurrent(&dummy);
 
     return 0;