Skip to content

Commit 419afb0

Browse files
committed
lavd: set GPU if decoder is cuvid
1 parent 63279ee commit 419afb0

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

src/libavcodec/lavc_common.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,17 @@ void print_libav_error(int verbosity, const char *msg, int rc) {
159159
log_msg(verbosity, "%s: %s\n", msg, errbuf);
160160
}
161161

162+
void printf_libav_error(int verbosity, int rc, const char *msg, ...) {
163+
char message[1024];
164+
165+
va_list ap;
166+
va_start(ap, msg);
167+
vsnprintf(message, sizeof message, msg, ap);
168+
va_end(ap);
169+
170+
print_libav_error(verbosity, message, rc);
171+
}
172+
162173
bool libav_codec_has_extradata(codec_t codec) {
163174
return codec == HFYU || codec == FFV1;
164175
}

src/libavcodec/lavc_common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ inline static bool pixfmt_list_has_420_subsampling(const enum AVPixelFormat *fmt
219219
}
220220

221221
void print_libav_error(int verbosity, const char *msg, int rc);
222+
void printf_libav_error(int verbosity, int rc, const char *msg, ...) ATTRIBUTE(format (printf, 3, 4));
222223
bool libav_codec_has_extradata(codec_t codec);
223224

224225
codec_t get_av_to_ug_codec(enum AVCodecID av_codec);

src/video_decompress/libavcodec.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,14 @@ static void deconfigure(struct state_libavcodec_decompress *s)
148148
#endif // defined HAVE_SWSCALE
149149
}
150150

151+
static int check_av_opt_set(void *state, const char *key, const char *val) {
152+
int ret = av_opt_set(state, key, val, 0);
153+
if (ret != 0) {
154+
printf_libav_error(LOG_LEVEL_WARNING, ret, MOD_NAME "Unable to set %s to %s", key, val);
155+
}
156+
return ret;
157+
}
158+
151159
ADD_TO_PARAM("lavd-thread-count", "* lavd-thread-count=<thread_count>[F][S][n]\n"
152160
" Use <thread_count> decoding threads (0 is usually auto).\n"
153161
" Flag 'F' enables frame parallelism (disabled by default), 'S' slice based, can be both (default slice), 'n' for none\n");
@@ -198,13 +206,17 @@ static void set_codec_context_params(struct state_libavcodec_decompress *s)
198206
}
199207

200208
s->codec_ctx->flags2 |= AV_CODEC_FLAG2_FAST;
201-
202209
// set by decoder
203210
s->codec_ctx->pix_fmt = AV_PIX_FMT_NONE;
204211
// callback to negotiate pixel format that is supported by UG
205212
s->codec_ctx->get_format = get_format_callback;
206-
207213
s->codec_ctx->opaque = s;
214+
215+
if (strstr(s->codec_ctx->codec->name, "cuvid") != NULL) {
216+
char gpu[3];
217+
snprintf(gpu, sizeof gpu, "%u", cuda_devices[0]);
218+
check_av_opt_set(s->codec_ctx->priv_data, "gpu", gpu);
219+
}
208220
}
209221

210222
static void jpeg_callback(void)

0 commit comments

Comments
 (0)