2626#include "libavutil/error.h"
2727#include "libavutil/hwcontext.h"
2828#include "libavutil/hwcontext_cuda_internal.h"
29+ #include "libavutil/cuda_check.h"
2930#include "libavutil/pixdesc.h"
3031#include "libavutil/pixfmt.h"
3132
@@ -50,6 +51,8 @@ typedef struct NVDECFramePool {
5051 unsigned int nb_allocated ;
5152} NVDECFramePool ;
5253
54+ #define CHECK_CU (x ) FF_CUDA_CHECK_DL(logctx, decoder->cudl, x)
55+
5356static int map_avcodec_id (enum AVCodecID id )
5457{
5558 switch (id ) {
@@ -86,7 +89,7 @@ static int map_chroma_format(enum AVPixelFormat pix_fmt)
8689static int nvdec_test_capabilities (NVDECDecoder * decoder ,
8790 CUVIDDECODECREATEINFO * params , void * logctx )
8891{
89- CUresult err ;
92+ int ret ;
9093 CUVIDDECODECAPS caps = { 0 };
9194
9295 caps .eCodecType = params -> CodecType ;
@@ -105,11 +108,9 @@ static int nvdec_test_capabilities(NVDECDecoder *decoder,
105108 return 0 ;
106109 }
107110
108- err = decoder -> cvdl -> cuvidGetDecoderCaps (& caps );
109- if (err != CUDA_SUCCESS ) {
110- av_log (logctx , AV_LOG_ERROR , "Failed querying decoder capabilities\n" );
111- return AVERROR_UNKNOWN ;
112- }
111+ ret = CHECK_CU (decoder -> cvdl -> cuvidGetDecoderCaps (& caps ));
112+ if (ret < 0 )
113+ return ret ;
113114
114115 av_log (logctx , AV_LOG_VERBOSE , "NVDEC capabilities:\n" );
115116 av_log (logctx , AV_LOG_VERBOSE , "format supported: %s, max_mb_count: %d\n" ,
@@ -150,10 +151,11 @@ static void nvdec_decoder_free(void *opaque, uint8_t *data)
150151 NVDECDecoder * decoder = (NVDECDecoder * )data ;
151152
152153 if (decoder -> decoder ) {
154+ void * logctx = decoder -> hw_device_ref -> data ;
153155 CUcontext dummy ;
154- decoder -> cudl -> cuCtxPushCurrent (decoder -> cuda_ctx );
155- decoder -> cvdl -> cuvidDestroyDecoder (decoder -> decoder );
156- decoder -> cudl -> cuCtxPopCurrent (& dummy );
156+ CHECK_CU ( decoder -> cudl -> cuCtxPushCurrent (decoder -> cuda_ctx ) );
157+ CHECK_CU ( decoder -> cvdl -> cuvidDestroyDecoder (decoder -> decoder ) );
158+ CHECK_CU ( decoder -> cudl -> cuCtxPopCurrent (& dummy ) );
157159 }
158160
159161 av_buffer_unref (& decoder -> hw_device_ref );
@@ -173,7 +175,6 @@ static int nvdec_decoder_create(AVBufferRef **out, AVBufferRef *hw_device_ref,
173175 NVDECDecoder * decoder ;
174176
175177 CUcontext dummy ;
176- CUresult err ;
177178 int ret ;
178179
179180 decoder = av_mallocz (sizeof (* decoder ));
@@ -202,25 +203,21 @@ static int nvdec_decoder_create(AVBufferRef **out, AVBufferRef *hw_device_ref,
202203 goto fail ;
203204 }
204205
205- err = decoder -> cudl -> cuCtxPushCurrent (decoder -> cuda_ctx );
206- if (err != CUDA_SUCCESS ) {
207- ret = AVERROR_UNKNOWN ;
206+ ret = CHECK_CU (decoder -> cudl -> cuCtxPushCurrent (decoder -> cuda_ctx ));
207+ if (ret < 0 )
208208 goto fail ;
209- }
210209
211210 ret = nvdec_test_capabilities (decoder , params , logctx );
212211 if (ret < 0 ) {
213- decoder -> cudl -> cuCtxPopCurrent (& dummy );
212+ CHECK_CU ( decoder -> cudl -> cuCtxPopCurrent (& dummy ) );
214213 goto fail ;
215214 }
216215
217- err = decoder -> cvdl -> cuvidCreateDecoder (& decoder -> decoder , params );
216+ ret = CHECK_CU ( decoder -> cvdl -> cuvidCreateDecoder (& decoder -> decoder , params ) );
218217
219- decoder -> cudl -> cuCtxPopCurrent (& dummy );
218+ CHECK_CU ( decoder -> cudl -> cuCtxPopCurrent (& dummy ) );
220219
221- if (err != CUDA_SUCCESS ) {
222- av_log (logctx , AV_LOG_ERROR , "Error creating a NVDEC decoder: %d\n" , err );
223- ret = AVERROR_UNKNOWN ;
220+ if (ret < 0 ) {
224221 goto fail ;
225222 }
226223
@@ -364,21 +361,18 @@ static void nvdec_unmap_mapped_frame(void *opaque, uint8_t *data)
364361{
365362 NVDECFrame * unmap_data = (NVDECFrame * )data ;
366363 NVDECDecoder * decoder = (NVDECDecoder * )unmap_data -> decoder_ref -> data ;
364+ void * logctx = decoder -> hw_device_ref -> data ;
367365 CUdeviceptr devptr = (CUdeviceptr )opaque ;
368- CUresult err ;
366+ int ret ;
369367 CUcontext dummy ;
370368
371- err = decoder -> cudl -> cuCtxPushCurrent (decoder -> cuda_ctx );
372- if (err != CUDA_SUCCESS ) {
373- av_log (NULL , AV_LOG_ERROR , "cuCtxPushCurrent failed\n" );
369+ ret = CHECK_CU (decoder -> cudl -> cuCtxPushCurrent (decoder -> cuda_ctx ));
370+ if (ret < 0 )
374371 goto finish ;
375- }
376372
377- err = decoder -> cvdl -> cuvidUnmapVideoFrame (decoder -> decoder , devptr );
378- if (err != CUDA_SUCCESS )
379- av_log (NULL , AV_LOG_ERROR , "cuvidUnmapVideoFrame failed\n" );
373+ CHECK_CU (decoder -> cvdl -> cuvidUnmapVideoFrame (decoder -> decoder , devptr ));
380374
381- decoder -> cudl -> cuCtxPopCurrent (& dummy );
375+ CHECK_CU ( decoder -> cudl -> cuCtxPopCurrent (& dummy ) );
382376
383377finish :
384378 av_buffer_unref (& unmap_data -> idx_ref );
@@ -395,7 +389,6 @@ static int nvdec_retrieve_data(void *logctx, AVFrame *frame)
395389 CUVIDPROCPARAMS vpp = { 0 };
396390 NVDECFrame * unmap_data = NULL ;
397391
398- CUresult err ;
399392 CUcontext dummy ;
400393 CUdeviceptr devptr ;
401394
@@ -406,18 +399,15 @@ static int nvdec_retrieve_data(void *logctx, AVFrame *frame)
406399 vpp .progressive_frame = 1 ;
407400 vpp .output_stream = decoder -> stream ;
408401
409- err = decoder -> cudl -> cuCtxPushCurrent (decoder -> cuda_ctx );
410- if (err != CUDA_SUCCESS )
411- return AVERROR_UNKNOWN ;
402+ ret = CHECK_CU ( decoder -> cudl -> cuCtxPushCurrent (decoder -> cuda_ctx ) );
403+ if (ret < 0 )
404+ return ret ;
412405
413- err = decoder -> cvdl -> cuvidMapVideoFrame (decoder -> decoder , cf -> idx , & devptr ,
414- & pitch , & vpp );
415- if (err != CUDA_SUCCESS ) {
416- av_log (logctx , AV_LOG_ERROR , "Error mapping a picture with CUVID: %d\n" ,
417- err );
418- ret = AVERROR_UNKNOWN ;
406+ ret = CHECK_CU (decoder -> cvdl -> cuvidMapVideoFrame (decoder -> decoder ,
407+ cf -> idx , & devptr ,
408+ & pitch , & vpp ));
409+ if (ret < 0 )
419410 goto finish ;
420- }
421411
422412 unmap_data = av_mallocz (sizeof (* unmap_data ));
423413 if (!unmap_data ) {
@@ -447,14 +437,14 @@ static int nvdec_retrieve_data(void *logctx, AVFrame *frame)
447437
448438copy_fail :
449439 if (!frame -> buf [1 ]) {
450- decoder -> cvdl -> cuvidUnmapVideoFrame (decoder -> decoder , devptr );
440+ CHECK_CU ( decoder -> cvdl -> cuvidUnmapVideoFrame (decoder -> decoder , devptr ) );
451441 av_freep (& unmap_data );
452442 } else {
453443 av_buffer_unref (& frame -> buf [1 ]);
454444 }
455445
456446finish :
457- decoder -> cudl -> cuCtxPopCurrent (& dummy );
447+ CHECK_CU ( decoder -> cudl -> cuCtxPopCurrent (& dummy ) );
458448 return ret ;
459449}
460450
@@ -504,9 +494,9 @@ int ff_nvdec_end_frame(AVCodecContext *avctx)
504494{
505495 NVDECContext * ctx = avctx -> internal -> hwaccel_priv_data ;
506496 NVDECDecoder * decoder = (NVDECDecoder * )ctx -> decoder_ref -> data ;
497+ void * logctx = avctx ;
507498 CUVIDPICPARAMS * pp = & ctx -> pic_params ;
508499
509- CUresult err ;
510500 CUcontext dummy ;
511501
512502 int ret = 0 ;
@@ -516,20 +506,16 @@ int ff_nvdec_end_frame(AVCodecContext *avctx)
516506 pp -> nNumSlices = ctx -> nb_slices ;
517507 pp -> pSliceDataOffsets = ctx -> slice_offsets ;
518508
519- err = decoder -> cudl -> cuCtxPushCurrent (decoder -> cuda_ctx );
520- if (err != CUDA_SUCCESS )
521- return AVERROR_UNKNOWN ;
509+ ret = CHECK_CU ( decoder -> cudl -> cuCtxPushCurrent (decoder -> cuda_ctx ) );
510+ if (ret < 0 )
511+ return ret ;
522512
523- err = decoder -> cvdl -> cuvidDecodePicture (decoder -> decoder , & ctx -> pic_params );
524- if (err != CUDA_SUCCESS ) {
525- av_log (avctx , AV_LOG_ERROR , "Error decoding a picture with NVDEC: %d\n" ,
526- err );
527- ret = AVERROR_UNKNOWN ;
513+ ret = CHECK_CU (decoder -> cvdl -> cuvidDecodePicture (decoder -> decoder , & ctx -> pic_params ));
514+ if (ret < 0 )
528515 goto finish ;
529- }
530516
531517finish :
532- decoder -> cudl -> cuCtxPopCurrent (& dummy );
518+ CHECK_CU ( decoder -> cudl -> cuCtxPopCurrent (& dummy ) );
533519
534520 return ret ;
535521}
0 commit comments