|
|
@@ -64,6 +64,31 @@ static HWDevice *hw_device_add(void) |
|
|
|
return hw_devices[nb_hw_devices++]; |
|
|
|
} |
|
|
|
|
|
|
|
static char *hw_device_default_name(enum AVHWDeviceType type) |
|
|
|
{ |
|
|
|
// Make an automatic name of the form "type%d". We arbitrarily |
|
|
|
// limit at 1000 anonymous devices of the same type - there is |
|
|
|
// probably something else very wrong if you get to this limit. |
|
|
|
const char *type_name = av_hwdevice_get_type_name(type); |
|
|
|
char *name; |
|
|
|
size_t index_pos; |
|
|
|
int index, index_limit = 1000; |
|
|
|
index_pos = strlen(type_name); |
|
|
|
name = av_malloc(index_pos + 4); |
|
|
|
if (!name) |
|
|
|
return NULL; |
|
|
|
for (index = 0; index < index_limit; index++) { |
|
|
|
snprintf(name, index_pos + 4, "%s%d", type_name, index); |
|
|
|
if (!hw_device_get_by_name(name)) |
|
|
|
break; |
|
|
|
} |
|
|
|
if (index >= index_limit) { |
|
|
|
av_freep(&name); |
|
|
|
return NULL; |
|
|
|
} |
|
|
|
return name; |
|
|
|
} |
|
|
|
|
|
|
|
int hw_device_init_from_string(const char *arg, HWDevice **dev_out) |
|
|
|
{ |
|
|
|
// "type=name:device,key=value,key2=value2" |
|
|
@@ -111,27 +136,11 @@ int hw_device_init_from_string(const char *arg, HWDevice **dev_out) |
|
|
|
|
|
|
|
p += 1 + k; |
|
|
|
} else { |
|
|
|
// Give the device an automatic name of the form "type%d". |
|
|
|
// We arbitrarily limit at 1000 anonymous devices of the same |
|
|
|
// type - there is probably something else very wrong if you |
|
|
|
// get to this limit. |
|
|
|
size_t index_pos; |
|
|
|
int index, index_limit = 1000; |
|
|
|
index_pos = strlen(type_name); |
|
|
|
name = av_malloc(index_pos + 4); |
|
|
|
name = hw_device_default_name(type); |
|
|
|
if (!name) { |
|
|
|
err = AVERROR(ENOMEM); |
|
|
|
goto fail; |
|
|
|
} |
|
|
|
for (index = 0; index < index_limit; index++) { |
|
|
|
snprintf(name, index_pos + 4, "%s%d", type_name, index); |
|
|
|
if (!hw_device_get_by_name(name)) |
|
|
|
break; |
|
|
|
} |
|
|
|
if (index >= index_limit) { |
|
|
|
errmsg = "too many devices"; |
|
|
|
goto invalid; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if (!*p) { |
|
|
@@ -214,6 +223,49 @@ fail: |
|
|
|
goto done; |
|
|
|
} |
|
|
|
|
|
|
|
static int hw_device_init_from_type(enum AVHWDeviceType type, |
|
|
|
const char *device, |
|
|
|
HWDevice **dev_out) |
|
|
|
{ |
|
|
|
AVBufferRef *device_ref = NULL; |
|
|
|
HWDevice *dev; |
|
|
|
char *name; |
|
|
|
int err; |
|
|
|
|
|
|
|
name = hw_device_default_name(type); |
|
|
|
if (!name) { |
|
|
|
err = AVERROR(ENOMEM); |
|
|
|
goto fail; |
|
|
|
} |
|
|
|
|
|
|
|
err = av_hwdevice_ctx_create(&device_ref, type, device, NULL, 0); |
|
|
|
if (err < 0) { |
|
|
|
av_log(NULL, AV_LOG_ERROR, |
|
|
|
"Device creation failed: %d.\n", err); |
|
|
|
goto fail; |
|
|
|
} |
|
|
|
|
|
|
|
dev = hw_device_add(); |
|
|
|
if (!dev) { |
|
|
|
err = AVERROR(ENOMEM); |
|
|
|
goto fail; |
|
|
|
} |
|
|
|
|
|
|
|
dev->name = name; |
|
|
|
dev->type = type; |
|
|
|
dev->device_ref = device_ref; |
|
|
|
|
|
|
|
if (dev_out) |
|
|
|
*dev_out = dev; |
|
|
|
|
|
|
|
return 0; |
|
|
|
|
|
|
|
fail: |
|
|
|
av_freep(&name); |
|
|
|
av_buffer_unref(&device_ref); |
|
|
|
return err; |
|
|
|
} |
|
|
|
|
|
|
|
void hw_device_free_all(void) |
|
|
|
{ |
|
|
|
int i; |
|
|
@@ -226,80 +278,130 @@ void hw_device_free_all(void) |
|
|
|
nb_hw_devices = 0; |
|
|
|
} |
|
|
|
|
|
|
|
static enum AVHWDeviceType hw_device_match_type_by_hwaccel(enum HWAccelID hwaccel_id) |
|
|
|
static HWDevice *hw_device_match_by_codec(const AVCodec *codec) |
|
|
|
{ |
|
|
|
const AVCodecHWConfig *config; |
|
|
|
HWDevice *dev; |
|
|
|
int i; |
|
|
|
if (hwaccel_id == HWACCEL_NONE) |
|
|
|
return AV_HWDEVICE_TYPE_NONE; |
|
|
|
for (i = 0; hwaccels[i].name; i++) { |
|
|
|
if (hwaccels[i].id == hwaccel_id) |
|
|
|
return hwaccels[i].device_type; |
|
|
|
for (i = 0;; i++) { |
|
|
|
config = avcodec_get_hw_config(codec, i); |
|
|
|
if (!config) |
|
|
|
return NULL; |
|
|
|
if (!(config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX)) |
|
|
|
continue; |
|
|
|
dev = hw_device_get_by_type(config->device_type); |
|
|
|
if (dev) |
|
|
|
return dev; |
|
|
|
} |
|
|
|
return AV_HWDEVICE_TYPE_NONE; |
|
|
|
} |
|
|
|
|
|
|
|
static enum AVHWDeviceType hw_device_match_type_in_name(const char *codec_name) |
|
|
|
{ |
|
|
|
const char *type_name; |
|
|
|
enum AVHWDeviceType type; |
|
|
|
for (type = av_hwdevice_iterate_types(AV_HWDEVICE_TYPE_NONE); |
|
|
|
type != AV_HWDEVICE_TYPE_NONE; |
|
|
|
type = av_hwdevice_iterate_types(type)) { |
|
|
|
type_name = av_hwdevice_get_type_name(type); |
|
|
|
if (strstr(codec_name, type_name)) |
|
|
|
return type; |
|
|
|
} |
|
|
|
return AV_HWDEVICE_TYPE_NONE; |
|
|
|
} |
|
|
|
|
|
|
|
int hw_device_setup_for_decode(InputStream *ist) |
|
|
|
{ |
|
|
|
const AVCodecHWConfig *config; |
|
|
|
enum AVHWDeviceType type; |
|
|
|
HWDevice *dev; |
|
|
|
int err; |
|
|
|
HWDevice *dev = NULL; |
|
|
|
int err, auto_device = 0; |
|
|
|
|
|
|
|
if (ist->hwaccel_device) { |
|
|
|
dev = hw_device_get_by_name(ist->hwaccel_device); |
|
|
|
if (!dev) { |
|
|
|
char *tmp; |
|
|
|
type = hw_device_match_type_by_hwaccel(ist->hwaccel_id); |
|
|
|
if (type == AV_HWDEVICE_TYPE_NONE) { |
|
|
|
// No match - this isn't necessarily invalid, though, |
|
|
|
// because an explicit device might not be needed or |
|
|
|
// the hwaccel setup could be handled elsewhere. |
|
|
|
if (ist->hwaccel_id == HWACCEL_AUTO) { |
|
|
|
auto_device = 1; |
|
|
|
} else if (ist->hwaccel_id == HWACCEL_GENERIC) { |
|
|
|
type = ist->hwaccel_device_type; |
|
|
|
err = hw_device_init_from_type(type, ist->hwaccel_device, |
|
|
|
&dev); |
|
|
|
} else { |
|
|
|
// This will be dealt with by API-specific initialisation |
|
|
|
// (using hwaccel_device), so nothing further needed here. |
|
|
|
return 0; |
|
|
|
} |
|
|
|
tmp = av_asprintf("%s:%s", av_hwdevice_get_type_name(type), |
|
|
|
ist->hwaccel_device); |
|
|
|
if (!tmp) |
|
|
|
return AVERROR(ENOMEM); |
|
|
|
err = hw_device_init_from_string(tmp, &dev); |
|
|
|
av_free(tmp); |
|
|
|
if (err < 0) |
|
|
|
return err; |
|
|
|
} else { |
|
|
|
if (ist->hwaccel_id == HWACCEL_AUTO) { |
|
|
|
ist->hwaccel_device_type = dev->type; |
|
|
|
} else if (ist->hwaccel_device_type != dev->type) { |
|
|
|
av_log(ist->dec_ctx, AV_LOG_ERROR, "Invalid hwaccel device " |
|
|
|
"specified for decoder: device %s of type %s is not " |
|
|
|
"usable with hwaccel %s.\n", dev->name, |
|
|
|
av_hwdevice_get_type_name(dev->type), |
|
|
|
av_hwdevice_get_type_name(ist->hwaccel_device_type)); |
|
|
|
return AVERROR(EINVAL); |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
if (ist->hwaccel_id != HWACCEL_NONE) |
|
|
|
type = hw_device_match_type_by_hwaccel(ist->hwaccel_id); |
|
|
|
else |
|
|
|
type = hw_device_match_type_in_name(ist->dec->name); |
|
|
|
if (type != AV_HWDEVICE_TYPE_NONE) { |
|
|
|
if (ist->hwaccel_id == HWACCEL_AUTO) { |
|
|
|
auto_device = 1; |
|
|
|
} else if (ist->hwaccel_id == HWACCEL_GENERIC) { |
|
|
|
type = ist->hwaccel_device_type; |
|
|
|
dev = hw_device_get_by_type(type); |
|
|
|
if (!dev) |
|
|
|
err = hw_device_init_from_type(type, NULL, &dev); |
|
|
|
} else { |
|
|
|
dev = hw_device_match_by_codec(ist->dec); |
|
|
|
if (!dev) { |
|
|
|
hw_device_init_from_string(av_hwdevice_get_type_name(type), |
|
|
|
// No device for this codec, but not using generic hwaccel |
|
|
|
// and therefore may well not need one - ignore. |
|
|
|
return 0; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if (auto_device) { |
|
|
|
int i; |
|
|
|
if (!avcodec_get_hw_config(ist->dec, 0)) { |
|
|
|
// Decoder does not support any hardware devices. |
|
|
|
return 0; |
|
|
|
} |
|
|
|
for (i = 0; !dev; i++) { |
|
|
|
config = avcodec_get_hw_config(ist->dec, i); |
|
|
|
if (!config) |
|
|
|
break; |
|
|
|
type = config->device_type; |
|
|
|
dev = hw_device_get_by_type(type); |
|
|
|
if (dev) { |
|
|
|
av_log(ist->dec_ctx, AV_LOG_INFO, "Using auto " |
|
|
|
"hwaccel type %s with existing device %s.\n", |
|
|
|
av_hwdevice_get_type_name(type), dev->name); |
|
|
|
} |
|
|
|
} |
|
|
|
for (i = 0; !dev; i++) { |
|
|
|
config = avcodec_get_hw_config(ist->dec, i); |
|
|
|
if (!config) |
|
|
|
break; |
|
|
|
type = config->device_type; |
|
|
|
// Try to make a new device of this type. |
|
|
|
err = hw_device_init_from_type(type, ist->hwaccel_device, |
|
|
|
&dev); |
|
|
|
if (err < 0) { |
|
|
|
// Can't make a device of this type. |
|
|
|
continue; |
|
|
|
} |
|
|
|
if (ist->hwaccel_device) { |
|
|
|
av_log(ist->dec_ctx, AV_LOG_INFO, "Using auto " |
|
|
|
"hwaccel type %s with new device created " |
|
|
|
"from %s.\n", av_hwdevice_get_type_name(type), |
|
|
|
ist->hwaccel_device); |
|
|
|
} else { |
|
|
|
av_log(ist->dec_ctx, AV_LOG_INFO, "Using auto " |
|
|
|
"hwaccel type %s with new default device.\n", |
|
|
|
av_hwdevice_get_type_name(type)); |
|
|
|
} |
|
|
|
} |
|
|
|
if (dev) { |
|
|
|
ist->hwaccel_device_type = type; |
|
|
|
} else { |
|
|
|
// No device required. |
|
|
|
av_log(ist->dec_ctx, AV_LOG_INFO, "Auto hwaccel " |
|
|
|
"disabled: no device found.\n"); |
|
|
|
ist->hwaccel_id = HWACCEL_NONE; |
|
|
|
return 0; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if (!dev) { |
|
|
|
av_log(ist->dec_ctx, AV_LOG_WARNING, "No device available " |
|
|
|
"for decoder (device type %s for codec %s).\n", |
|
|
|
av_log(ist->dec_ctx, AV_LOG_ERROR, "No device available " |
|
|
|
"for decoder: device type %s needed for codec %s.\n", |
|
|
|
av_hwdevice_get_type_name(type), ist->dec->name); |
|
|
|
return 0; |
|
|
|
return err; |
|
|
|
} |
|
|
|
|
|
|
|
ist->dec_ctx->hw_device_ctx = av_buffer_ref(dev->device_ref); |
|
|
@@ -311,24 +413,16 @@ int hw_device_setup_for_decode(InputStream *ist) |
|
|
|
|
|
|
|
int hw_device_setup_for_encode(OutputStream *ost) |
|
|
|
{ |
|
|
|
enum AVHWDeviceType type; |
|
|
|
HWDevice *dev; |
|
|
|
|
|
|
|
type = hw_device_match_type_in_name(ost->enc->name); |
|
|
|
if (type != AV_HWDEVICE_TYPE_NONE) { |
|
|
|
dev = hw_device_get_by_type(type); |
|
|
|
if (!dev) { |
|
|
|
av_log(ost->enc_ctx, AV_LOG_WARNING, "No device available " |
|
|
|
"for encoder (device type %s for codec %s).\n", |
|
|
|
av_hwdevice_get_type_name(type), ost->enc->name); |
|
|
|
return 0; |
|
|
|
} |
|
|
|
dev = hw_device_match_by_codec(ost->enc); |
|
|
|
if (dev) { |
|
|
|
ost->enc_ctx->hw_device_ctx = av_buffer_ref(dev->device_ref); |
|
|
|
if (!ost->enc_ctx->hw_device_ctx) |
|
|
|
return AVERROR(ENOMEM); |
|
|
|
return 0; |
|
|
|
} else { |
|
|
|
// No device required. |
|
|
|
// No device required, or no device available. |
|
|
|
return 0; |
|
|
|
} |
|
|
|
} |
|
|
|