Skip to content

Commit 1c915df

Browse files
committed
avutil/hwcontext_cuda: Define and use common CHECK_CU()
We have a pattern of wrapping CUDA calls to print errors and normalise return values that is used in a couple of places. To avoid duplication and increase consistency, let's put the wrapper implementation in a shared place and use it everywhere.
1 parent c305e13 commit 1c915df

File tree

12 files changed

+303
-387
lines changed

12 files changed

+303
-387
lines changed

doc/APIchanges

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ libavutil: 2017-10-21
1515

1616
API changes, most recent first:
1717

18+
2018-11-11 - xxxxxxxxxx - lavu 56.24.100 - hwcontext_cuda.h
19+
Add av_cuda_check().
20+
1821
-------- 8< --------- FFmpeg 4.1 was cut here -------- 8< ---------
1922

2023
2018-10-27 - 718044dc19 - lavu 56.21.100 - pixdesc.h

libavcodec/cuviddec.c

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -95,29 +95,7 @@ typedef struct CuvidParsedFrame
9595
int is_deinterlacing;
9696
} CuvidParsedFrame;
9797

98-
static int check_cu(AVCodecContext *avctx, CUresult err, const char *func)
99-
{
100-
CuvidContext *ctx = avctx->priv_data;
101-
const char *err_name;
102-
const char *err_string;
103-
104-
av_log(avctx, AV_LOG_TRACE, "Calling %s\n", func);
105-
106-
if (err == CUDA_SUCCESS)
107-
return 0;
108-
109-
ctx->cudl->cuGetErrorName(err, &err_name);
110-
ctx->cudl->cuGetErrorString(err, &err_string);
111-
112-
av_log(avctx, AV_LOG_ERROR, "%s failed", func);
113-
if (err_name && err_string)
114-
av_log(avctx, AV_LOG_ERROR, " -> %s: %s", err_name, err_string);
115-
av_log(avctx, AV_LOG_ERROR, "\n");
116-
117-
return AVERROR_EXTERNAL;
118-
}
119-
120-
#define CHECK_CU(x) check_cu(avctx, (x), #x)
98+
#define CHECK_CU(x) AV_CUDA_CHECK_DL(avctx, ctx->cudl, x)
12199

122100
static int CUDAAPI cuvid_handle_video_sequence(void *opaque, CUVIDEOFORMAT* format)
123101
{

libavcodec/nvdec.c

Lines changed: 38 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ typedef struct NVDECFramePool {
5050
unsigned int nb_allocated;
5151
} NVDECFramePool;
5252

53+
#define CHECK_CU(x) AV_CUDA_CHECK_DL(logctx, decoder->cudl, x)
54+
5355
static int map_avcodec_id(enum AVCodecID id)
5456
{
5557
switch (id) {
@@ -86,7 +88,7 @@ static int map_chroma_format(enum AVPixelFormat pix_fmt)
8688
static int nvdec_test_capabilities(NVDECDecoder *decoder,
8789
CUVIDDECODECREATEINFO *params, void *logctx)
8890
{
89-
CUresult err;
91+
int ret;
9092
CUVIDDECODECAPS caps = { 0 };
9193

9294
caps.eCodecType = params->CodecType;
@@ -105,11 +107,9 @@ static int nvdec_test_capabilities(NVDECDecoder *decoder,
105107
return 0;
106108
}
107109

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-
}
110+
ret = CHECK_CU(decoder->cvdl->cuvidGetDecoderCaps(&caps));
111+
if (ret < 0)
112+
return ret;
113113

114114
av_log(logctx, AV_LOG_VERBOSE, "NVDEC capabilities:\n");
115115
av_log(logctx, AV_LOG_VERBOSE, "format supported: %s, max_mb_count: %d\n",
@@ -150,10 +150,11 @@ static void nvdec_decoder_free(void *opaque, uint8_t *data)
150150
NVDECDecoder *decoder = (NVDECDecoder*)data;
151151

152152
if (decoder->decoder) {
153+
void *logctx = decoder->hw_device_ref->data;
153154
CUcontext dummy;
154-
decoder->cudl->cuCtxPushCurrent(decoder->cuda_ctx);
155-
decoder->cvdl->cuvidDestroyDecoder(decoder->decoder);
156-
decoder->cudl->cuCtxPopCurrent(&dummy);
155+
CHECK_CU(decoder->cudl->cuCtxPushCurrent(decoder->cuda_ctx));
156+
CHECK_CU(decoder->cvdl->cuvidDestroyDecoder(decoder->decoder));
157+
CHECK_CU(decoder->cudl->cuCtxPopCurrent(&dummy));
157158
}
158159

159160
av_buffer_unref(&decoder->hw_device_ref);
@@ -173,7 +174,6 @@ static int nvdec_decoder_create(AVBufferRef **out, AVBufferRef *hw_device_ref,
173174
NVDECDecoder *decoder;
174175

175176
CUcontext dummy;
176-
CUresult err;
177177
int ret;
178178

179179
decoder = av_mallocz(sizeof(*decoder));
@@ -202,25 +202,21 @@ static int nvdec_decoder_create(AVBufferRef **out, AVBufferRef *hw_device_ref,
202202
goto fail;
203203
}
204204

205-
err = decoder->cudl->cuCtxPushCurrent(decoder->cuda_ctx);
206-
if (err != CUDA_SUCCESS) {
207-
ret = AVERROR_UNKNOWN;
205+
ret = CHECK_CU(decoder->cudl->cuCtxPushCurrent(decoder->cuda_ctx));
206+
if (ret < 0)
208207
goto fail;
209-
}
210208

211209
ret = nvdec_test_capabilities(decoder, params, logctx);
212210
if (ret < 0) {
213-
decoder->cudl->cuCtxPopCurrent(&dummy);
211+
CHECK_CU(decoder->cudl->cuCtxPopCurrent(&dummy));
214212
goto fail;
215213
}
216214

217-
err = decoder->cvdl->cuvidCreateDecoder(&decoder->decoder, params);
215+
ret = CHECK_CU(decoder->cvdl->cuvidCreateDecoder(&decoder->decoder, params));
218216

219-
decoder->cudl->cuCtxPopCurrent(&dummy);
217+
CHECK_CU(decoder->cudl->cuCtxPopCurrent(&dummy));
220218

221-
if (err != CUDA_SUCCESS) {
222-
av_log(logctx, AV_LOG_ERROR, "Error creating a NVDEC decoder: %d\n", err);
223-
ret = AVERROR_UNKNOWN;
219+
if (ret < 0) {
224220
goto fail;
225221
}
226222

@@ -364,21 +360,18 @@ static void nvdec_unmap_mapped_frame(void *opaque, uint8_t *data)
364360
{
365361
NVDECFrame *unmap_data = (NVDECFrame*)data;
366362
NVDECDecoder *decoder = (NVDECDecoder*)unmap_data->decoder_ref->data;
363+
void *logctx = decoder->hw_device_ref->data;
367364
CUdeviceptr devptr = (CUdeviceptr)opaque;
368-
CUresult err;
365+
int ret;
369366
CUcontext dummy;
370367

371-
err = decoder->cudl->cuCtxPushCurrent(decoder->cuda_ctx);
372-
if (err != CUDA_SUCCESS) {
373-
av_log(NULL, AV_LOG_ERROR, "cuCtxPushCurrent failed\n");
368+
ret = CHECK_CU(decoder->cudl->cuCtxPushCurrent(decoder->cuda_ctx));
369+
if (ret < 0)
374370
goto finish;
375-
}
376371

377-
err = decoder->cvdl->cuvidUnmapVideoFrame(decoder->decoder, devptr);
378-
if (err != CUDA_SUCCESS)
379-
av_log(NULL, AV_LOG_ERROR, "cuvidUnmapVideoFrame failed\n");
372+
CHECK_CU(decoder->cvdl->cuvidUnmapVideoFrame(decoder->decoder, devptr));
380373

381-
decoder->cudl->cuCtxPopCurrent(&dummy);
374+
CHECK_CU(decoder->cudl->cuCtxPopCurrent(&dummy));
382375

383376
finish:
384377
av_buffer_unref(&unmap_data->idx_ref);
@@ -395,7 +388,6 @@ static int nvdec_retrieve_data(void *logctx, AVFrame *frame)
395388
CUVIDPROCPARAMS vpp = { 0 };
396389
NVDECFrame *unmap_data = NULL;
397390

398-
CUresult err;
399391
CUcontext dummy;
400392
CUdeviceptr devptr;
401393

@@ -406,18 +398,15 @@ static int nvdec_retrieve_data(void *logctx, AVFrame *frame)
406398
vpp.progressive_frame = 1;
407399
vpp.output_stream = decoder->stream;
408400

409-
err = decoder->cudl->cuCtxPushCurrent(decoder->cuda_ctx);
410-
if (err != CUDA_SUCCESS)
411-
return AVERROR_UNKNOWN;
401+
ret = CHECK_CU(decoder->cudl->cuCtxPushCurrent(decoder->cuda_ctx));
402+
if (ret < 0)
403+
return ret;
412404

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;
405+
ret = CHECK_CU(decoder->cvdl->cuvidMapVideoFrame(decoder->decoder,
406+
cf->idx, &devptr,
407+
&pitch, &vpp));
408+
if (ret < 0)
419409
goto finish;
420-
}
421410

422411
unmap_data = av_mallocz(sizeof(*unmap_data));
423412
if (!unmap_data) {
@@ -447,14 +436,14 @@ static int nvdec_retrieve_data(void *logctx, AVFrame *frame)
447436

448437
copy_fail:
449438
if (!frame->buf[1]) {
450-
decoder->cvdl->cuvidUnmapVideoFrame(decoder->decoder, devptr);
439+
CHECK_CU(decoder->cvdl->cuvidUnmapVideoFrame(decoder->decoder, devptr));
451440
av_freep(&unmap_data);
452441
} else {
453442
av_buffer_unref(&frame->buf[1]);
454443
}
455444

456445
finish:
457-
decoder->cudl->cuCtxPopCurrent(&dummy);
446+
CHECK_CU(decoder->cudl->cuCtxPopCurrent(&dummy));
458447
return ret;
459448
}
460449

@@ -504,9 +493,9 @@ int ff_nvdec_end_frame(AVCodecContext *avctx)
504493
{
505494
NVDECContext *ctx = avctx->internal->hwaccel_priv_data;
506495
NVDECDecoder *decoder = (NVDECDecoder*)ctx->decoder_ref->data;
496+
void *logctx = avctx;
507497
CUVIDPICPARAMS *pp = &ctx->pic_params;
508498

509-
CUresult err;
510499
CUcontext dummy;
511500

512501
int ret = 0;
@@ -516,20 +505,16 @@ int ff_nvdec_end_frame(AVCodecContext *avctx)
516505
pp->nNumSlices = ctx->nb_slices;
517506
pp->pSliceDataOffsets = ctx->slice_offsets;
518507

519-
err = decoder->cudl->cuCtxPushCurrent(decoder->cuda_ctx);
520-
if (err != CUDA_SUCCESS)
521-
return AVERROR_UNKNOWN;
508+
ret = CHECK_CU(decoder->cudl->cuCtxPushCurrent(decoder->cuda_ctx));
509+
if (ret < 0)
510+
return ret;
522511

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;
512+
ret = CHECK_CU(decoder->cvdl->cuvidDecodePicture(decoder->decoder, &ctx->pic_params));
513+
if (ret < 0)
528514
goto finish;
529-
}
530515

531516
finish:
532-
decoder->cudl->cuCtxPopCurrent(&dummy);
517+
CHECK_CU(decoder->cudl->cuCtxPopCurrent(&dummy));
533518

534519
return ret;
535520
}

libavcodec/nvenc.c

Lines changed: 19 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
#include "libavutil/pixdesc.h"
3232
#include "internal.h"
3333

34+
#define CHECK_CU(x) AV_CUDA_CHECK_DL(avctx, dl_fn->cuda_dl, x)
35+
3436
#define NVENC_CAP 0x30
3537
#define IS_CBR(rc) (rc == NV_ENC_PARAMS_RC_CBR || \
3638
rc == NV_ENC_PARAMS_RC_CBR_LOWDELAY_HQ || \
@@ -183,37 +185,23 @@ static int nvenc_push_context(AVCodecContext *avctx)
183185
{
184186
NvencContext *ctx = avctx->priv_data;
185187
NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
186-
CUresult cu_res;
187188

188189
if (ctx->d3d11_device)
189190
return 0;
190191

191-
cu_res = dl_fn->cuda_dl->cuCtxPushCurrent(ctx->cu_context);
192-
if (cu_res != CUDA_SUCCESS) {
193-
av_log(avctx, AV_LOG_ERROR, "cuCtxPushCurrent failed\n");
194-
return AVERROR_EXTERNAL;
195-
}
196-
197-
return 0;
192+
return CHECK_CU(dl_fn->cuda_dl->cuCtxPushCurrent(ctx->cu_context));
198193
}
199194

200195
static int nvenc_pop_context(AVCodecContext *avctx)
201196
{
202197
NvencContext *ctx = avctx->priv_data;
203198
NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
204-
CUresult cu_res;
205199
CUcontext dummy;
206200

207201
if (ctx->d3d11_device)
208202
return 0;
209203

210-
cu_res = dl_fn->cuda_dl->cuCtxPopCurrent(&dummy);
211-
if (cu_res != CUDA_SUCCESS) {
212-
av_log(avctx, AV_LOG_ERROR, "cuCtxPopCurrent failed\n");
213-
return AVERROR_EXTERNAL;
214-
}
215-
216-
return 0;
204+
return CHECK_CU(dl_fn->cuda_dl->cuCtxPopCurrent(&dummy));
217205
}
218206

219207
static av_cold int nvenc_open_session(AVCodecContext *avctx)
@@ -406,32 +394,23 @@ static av_cold int nvenc_check_device(AVCodecContext *avctx, int idx)
406394
NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
407395
char name[128] = { 0};
408396
int major, minor, ret;
409-
CUresult cu_res;
410397
CUdevice cu_device;
411398
int loglevel = AV_LOG_VERBOSE;
412399

413400
if (ctx->device == LIST_DEVICES)
414401
loglevel = AV_LOG_INFO;
415402

416-
cu_res = dl_fn->cuda_dl->cuDeviceGet(&cu_device, idx);
417-
if (cu_res != CUDA_SUCCESS) {
418-
av_log(avctx, AV_LOG_ERROR,
419-
"Cannot access the CUDA device %d\n",
420-
idx);
421-
return -1;
422-
}
403+
ret = CHECK_CU(dl_fn->cuda_dl->cuDeviceGet(&cu_device, idx));
404+
if (ret < 0)
405+
return ret;
423406

424-
cu_res = dl_fn->cuda_dl->cuDeviceGetName(name, sizeof(name), cu_device);
425-
if (cu_res != CUDA_SUCCESS) {
426-
av_log(avctx, AV_LOG_ERROR, "cuDeviceGetName failed on device %d\n", idx);
427-
return -1;
428-
}
407+
ret = CHECK_CU(dl_fn->cuda_dl->cuDeviceGetName(name, sizeof(name), cu_device));
408+
if (ret < 0)
409+
return ret;
429410

430-
cu_res = dl_fn->cuda_dl->cuDeviceComputeCapability(&major, &minor, cu_device);
431-
if (cu_res != CUDA_SUCCESS) {
432-
av_log(avctx, AV_LOG_ERROR, "cuDeviceComputeCapability failed on device %d\n", idx);
433-
return -1;
434-
}
411+
ret = CHECK_CU(dl_fn->cuda_dl->cuDeviceComputeCapability(&major, &minor, cu_device));
412+
if (ret < 0)
413+
return ret;
435414

436415
av_log(avctx, loglevel, "[ GPU #%d - < %s > has Compute SM %d.%d ]\n", idx, name, major, minor);
437416
if (((major << 4) | minor) < NVENC_CAP) {
@@ -442,11 +421,9 @@ static av_cold int nvenc_check_device(AVCodecContext *avctx, int idx)
442421
if (ctx->device != idx && ctx->device != ANY_DEVICE)
443422
return -1;
444423

445-
cu_res = dl_fn->cuda_dl->cuCtxCreate(&ctx->cu_context_internal, 0, cu_device);
446-
if (cu_res != CUDA_SUCCESS) {
447-
av_log(avctx, AV_LOG_FATAL, "Failed creating CUDA context for NVENC: 0x%x\n", (int)cu_res);
424+
ret = CHECK_CU(dl_fn->cuda_dl->cuCtxCreate(&ctx->cu_context_internal, 0, cu_device));
425+
if (ret < 0)
448426
goto fail;
449-
}
450427

451428
ctx->cu_context = ctx->cu_context_internal;
452429

@@ -477,7 +454,7 @@ static av_cold int nvenc_check_device(AVCodecContext *avctx, int idx)
477454
return ret;
478455

479456
fail2:
480-
dl_fn->cuda_dl->cuCtxDestroy(ctx->cu_context_internal);
457+
CHECK_CU(dl_fn->cuda_dl->cuCtxDestroy(ctx->cu_context_internal));
481458
ctx->cu_context_internal = NULL;
482459

483460
fail:
@@ -555,17 +532,11 @@ static av_cold int nvenc_setup_device(AVCodecContext *avctx)
555532
} else {
556533
int i, nb_devices = 0;
557534

558-
if ((dl_fn->cuda_dl->cuInit(0)) != CUDA_SUCCESS) {
559-
av_log(avctx, AV_LOG_ERROR,
560-
"Cannot init CUDA\n");
535+
if (CHECK_CU(dl_fn->cuda_dl->cuInit(0)) < 0)
561536
return AVERROR_UNKNOWN;
562-
}
563537

564-
if ((dl_fn->cuda_dl->cuDeviceGetCount(&nb_devices)) != CUDA_SUCCESS) {
565-
av_log(avctx, AV_LOG_ERROR,
566-
"Cannot enumerate the CUDA devices\n");
538+
if (CHECK_CU(dl_fn->cuda_dl->cuDeviceGetCount(&nb_devices)) < 0)
567539
return AVERROR_UNKNOWN;
568-
}
569540

570541
if (!nb_devices) {
571542
av_log(avctx, AV_LOG_FATAL, "No CUDA capable devices found\n");
@@ -1460,7 +1431,7 @@ av_cold int ff_nvenc_encode_close(AVCodecContext *avctx)
14601431
ctx->nvencoder = NULL;
14611432

14621433
if (ctx->cu_context_internal)
1463-
dl_fn->cuda_dl->cuCtxDestroy(ctx->cu_context_internal);
1434+
CHECK_CU(dl_fn->cuda_dl->cuCtxDestroy(ctx->cu_context_internal));
14641435
ctx->cu_context = ctx->cu_context_internal = NULL;
14651436

14661437
#if CONFIG_D3D11VA

0 commit comments

Comments
 (0)