Skip to content

Commit

Permalink
lavd: Allow hw accel type to be explicitly specified
Browse files Browse the repository at this point in the history
  • Loading branch information
mpiatka committed Aug 14, 2023
1 parent a5d0d2b commit 625821f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
31 changes: 31 additions & 0 deletions src/hwaccel_libav_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,37 @@
#include "hwaccel_libav_common.h"
#include "libavcodec/lavc_common.h"

struct {
const char *name;
enum hw_accel_type type;
} accel_str_map[] = {
{"vdpau", HWACCEL_VDPAU},
{"vaapi", HWACCEL_VAAPI},
{"videotoolbox", HWACCEL_VIDEOTOOLBOX},
{"rpi4", HWACCEL_RPI4},
{"cuda", HWACCEL_CUDA},
{"vulkan", HWACCEL_VULKAN},
};

enum hw_accel_type hw_accel_from_str(const char *str){
for(unsigned i = 0; i < sizeof(accel_str_map) / sizeof(accel_str_map[0]); i++){
if(strcmp(str, accel_str_map[i].name) == 0){
return accel_str_map[i].type;
}
}

return HWACCEL_NONE;
}
const char *hw_accel_to_str(enum hw_accel_type type){
for(unsigned i = 0; i < sizeof(accel_str_map) / sizeof(accel_str_map[0]); i++){
if(type == accel_str_map[i].type){
return accel_str_map[i].name;
}
}

return "unknown";
}

void hwaccel_state_init(struct hw_accel_state *hwaccel){
hwaccel->type = HWACCEL_NONE;
hwaccel->copy = false;
Expand Down
3 changes: 3 additions & 0 deletions src/hwaccel_libav_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ enum hw_accel_type {
HWACCEL_COUNT
};

enum hw_accel_type hw_accel_from_str(const char *str);
const char *hw_accel_to_str(enum hw_accel_type type);

/**
* hw_accel_state describes the current state of hw. accleration
*/
Expand Down
17 changes: 16 additions & 1 deletion src/video_decompress/libavcodec.c
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ static enum AVPixelFormat get_format_callback(struct AVCodecContext *s, const en
}

struct state_libavcodec_decompress *state = (struct state_libavcodec_decompress *) s->opaque;
bool hwaccel = get_commandline_param("use-hw-accel") != NULL;
const char *hwaccel = get_commandline_param("use-hw-accel");
#ifdef HWACC_COMMON_IMPL
hwaccel_state_reset(&state->hwaccel);

Expand Down Expand Up @@ -543,10 +543,21 @@ static enum AVPixelFormat get_format_callback(struct AVCodecContext *s, const en
"but incoming video has not 4:2:0 subsampling, "
"which is usually not supported by hw. accelerators\n");
}
enum hw_accel_type forced_hwaccel = HWACCEL_NONE;
if(strlen(hwaccel) > 0){
forced_hwaccel = hw_accel_from_str(hwaccel);
if(forced_hwaccel == HWACCEL_NONE){
log_msg(LOG_LEVEL_ERROR, MOD_NAME "Unknown accel %s\n", hwaccel);
return AV_PIX_FMT_NONE;
}
}
for(const enum AVPixelFormat *it = fmt; *it != AV_PIX_FMT_NONE; it++){
for(unsigned i = 0; i < sizeof(accels) / sizeof(accels[0]); i++){
if(*it == accels[i].pix_fmt && !state->block_accel[accels[i].accel_type])
{
if(forced_hwaccel != HWACCEL_NONE && accels[i].accel_type != forced_hwaccel){
break;
}
int ret = accels[i].init_func(s, &state->hwaccel, state->out_codec);
if(ret < 0){
hwaccel_state_reset(&state->hwaccel);
Expand All @@ -556,6 +567,10 @@ static enum AVPixelFormat get_format_callback(struct AVCodecContext *s, const en
}
}
}
if(forced_hwaccel != HWACCEL_NONE){
log_msg(LOG_LEVEL_ERROR, MOD_NAME "Requested hw accel \"%s\" is not available\n", hwaccel);
return AV_PIX_FMT_NONE;
}
log_msg(LOG_LEVEL_WARNING, "[lavd] Falling back to software decoding!\n");
if (state->out_codec == HW_VDPAU) {
return AV_PIX_FMT_NONE;
Expand Down

0 comments on commit 625821f

Please sign in to comment.