vf_hwmap: Add device derivation
Also refactor a little and improve error messages to make failure cases easier to understand. (cherry picked from commit 38cb05f1c89cae1862b360d4e7e3f0cd2b5bbb67)
This commit is contained in:
parent
d59c6a3aeb
commit
b2ef1f42ba
@ -30,10 +30,10 @@
|
|||||||
typedef struct HWMapContext {
|
typedef struct HWMapContext {
|
||||||
const AVClass *class;
|
const AVClass *class;
|
||||||
|
|
||||||
AVBufferRef *hwdevice_ref;
|
|
||||||
AVBufferRef *hwframes_ref;
|
AVBufferRef *hwframes_ref;
|
||||||
|
|
||||||
int mode;
|
int mode;
|
||||||
|
char *derive_device_type;
|
||||||
int map_backwards;
|
int map_backwards;
|
||||||
} HWMapContext;
|
} HWMapContext;
|
||||||
|
|
||||||
@ -56,6 +56,7 @@ static int hwmap_config_output(AVFilterLink *outlink)
|
|||||||
HWMapContext *ctx = avctx->priv;
|
HWMapContext *ctx = avctx->priv;
|
||||||
AVFilterLink *inlink = avctx->inputs[0];
|
AVFilterLink *inlink = avctx->inputs[0];
|
||||||
AVHWFramesContext *hwfc;
|
AVHWFramesContext *hwfc;
|
||||||
|
AVBufferRef *device;
|
||||||
const AVPixFmtDescriptor *desc;
|
const AVPixFmtDescriptor *desc;
|
||||||
int err;
|
int err;
|
||||||
|
|
||||||
@ -63,30 +64,58 @@ static int hwmap_config_output(AVFilterLink *outlink)
|
|||||||
av_get_pix_fmt_name(inlink->format),
|
av_get_pix_fmt_name(inlink->format),
|
||||||
av_get_pix_fmt_name(outlink->format));
|
av_get_pix_fmt_name(outlink->format));
|
||||||
|
|
||||||
|
av_buffer_unref(&ctx->hwframes_ref);
|
||||||
|
|
||||||
|
device = avctx->hw_device_ctx;
|
||||||
|
|
||||||
if (inlink->hw_frames_ctx) {
|
if (inlink->hw_frames_ctx) {
|
||||||
hwfc = (AVHWFramesContext*)inlink->hw_frames_ctx->data;
|
hwfc = (AVHWFramesContext*)inlink->hw_frames_ctx->data;
|
||||||
|
|
||||||
|
if (ctx->derive_device_type) {
|
||||||
|
enum AVHWDeviceType type;
|
||||||
|
|
||||||
|
type = av_hwdevice_find_type_by_name(ctx->derive_device_type);
|
||||||
|
if (type == AV_HWDEVICE_TYPE_NONE) {
|
||||||
|
av_log(avctx, AV_LOG_ERROR, "Invalid device type.\n");
|
||||||
|
goto fail;
|
||||||
|
}
|
||||||
|
|
||||||
|
err = av_hwdevice_ctx_create_derived(&device, type,
|
||||||
|
hwfc->device_ref, 0);
|
||||||
|
if (err < 0) {
|
||||||
|
av_log(avctx, AV_LOG_ERROR, "Failed to created derived "
|
||||||
|
"device context: %d.\n", err);
|
||||||
|
goto fail;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
desc = av_pix_fmt_desc_get(outlink->format);
|
desc = av_pix_fmt_desc_get(outlink->format);
|
||||||
if (!desc)
|
if (!desc) {
|
||||||
return AVERROR(EINVAL);
|
err = AVERROR(EINVAL);
|
||||||
|
goto fail;
|
||||||
|
}
|
||||||
|
|
||||||
if (inlink->format == hwfc->format &&
|
if (inlink->format == hwfc->format &&
|
||||||
(desc->flags & AV_PIX_FMT_FLAG_HWACCEL)) {
|
(desc->flags & AV_PIX_FMT_FLAG_HWACCEL)) {
|
||||||
// Map between two hardware formats (including the case of
|
// Map between two hardware formats (including the case of
|
||||||
// undoing an existing mapping).
|
// undoing an existing mapping).
|
||||||
|
|
||||||
ctx->hwdevice_ref = av_buffer_ref(avctx->hw_device_ctx);
|
if (!device) {
|
||||||
if (!ctx->hwdevice_ref) {
|
av_log(avctx, AV_LOG_ERROR, "A device reference is "
|
||||||
err = AVERROR(ENOMEM);
|
"required to map to a hardware format.\n");
|
||||||
|
err = AVERROR(EINVAL);
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
|
|
||||||
err = av_hwframe_ctx_create_derived(&ctx->hwframes_ref,
|
err = av_hwframe_ctx_create_derived(&ctx->hwframes_ref,
|
||||||
outlink->format,
|
outlink->format,
|
||||||
ctx->hwdevice_ref,
|
device,
|
||||||
inlink->hw_frames_ctx, 0);
|
inlink->hw_frames_ctx, 0);
|
||||||
if (err < 0)
|
if (err < 0) {
|
||||||
|
av_log(avctx, AV_LOG_ERROR, "Failed to create derived "
|
||||||
|
"frames context: %d.\n", err);
|
||||||
goto fail;
|
goto fail;
|
||||||
|
}
|
||||||
|
|
||||||
} else if ((outlink->format == hwfc->format &&
|
} else if ((outlink->format == hwfc->format &&
|
||||||
inlink->format == hwfc->sw_format) ||
|
inlink->format == hwfc->sw_format) ||
|
||||||
@ -94,8 +123,6 @@ static int hwmap_config_output(AVFilterLink *outlink)
|
|||||||
// Map from a hardware format to a software format, or
|
// Map from a hardware format to a software format, or
|
||||||
// undo an existing such mapping.
|
// undo an existing such mapping.
|
||||||
|
|
||||||
ctx->hwdevice_ref = NULL;
|
|
||||||
|
|
||||||
ctx->hwframes_ref = av_buffer_ref(inlink->hw_frames_ctx);
|
ctx->hwframes_ref = av_buffer_ref(inlink->hw_frames_ctx);
|
||||||
if (!ctx->hwframes_ref) {
|
if (!ctx->hwframes_ref) {
|
||||||
err = AVERROR(ENOMEM);
|
err = AVERROR(ENOMEM);
|
||||||
@ -119,15 +146,17 @@ static int hwmap_config_output(AVFilterLink *outlink)
|
|||||||
// returns frames mapped from that to the previous link in
|
// returns frames mapped from that to the previous link in
|
||||||
// order to fill them without an additional copy.
|
// order to fill them without an additional copy.
|
||||||
|
|
||||||
ctx->map_backwards = 1;
|
if (!device) {
|
||||||
|
av_log(avctx, AV_LOG_ERROR, "A device reference is "
|
||||||
ctx->hwdevice_ref = av_buffer_ref(avctx->hw_device_ctx);
|
"required to create new frames with backwards "
|
||||||
if (!ctx->hwdevice_ref) {
|
"mapping.\n");
|
||||||
err = AVERROR(ENOMEM);
|
err = AVERROR(EINVAL);
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx->hwframes_ref = av_hwframe_ctx_alloc(ctx->hwdevice_ref);
|
ctx->map_backwards = 1;
|
||||||
|
|
||||||
|
ctx->hwframes_ref = av_hwframe_ctx_alloc(device);
|
||||||
if (!ctx->hwframes_ref) {
|
if (!ctx->hwframes_ref) {
|
||||||
err = AVERROR(ENOMEM);
|
err = AVERROR(ENOMEM);
|
||||||
goto fail;
|
goto fail;
|
||||||
@ -165,7 +194,6 @@ static int hwmap_config_output(AVFilterLink *outlink)
|
|||||||
|
|
||||||
fail:
|
fail:
|
||||||
av_buffer_unref(&ctx->hwframes_ref);
|
av_buffer_unref(&ctx->hwframes_ref);
|
||||||
av_buffer_unref(&ctx->hwdevice_ref);
|
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -273,7 +301,6 @@ static av_cold void hwmap_uninit(AVFilterContext *avctx)
|
|||||||
HWMapContext *ctx = avctx->priv;
|
HWMapContext *ctx = avctx->priv;
|
||||||
|
|
||||||
av_buffer_unref(&ctx->hwframes_ref);
|
av_buffer_unref(&ctx->hwframes_ref);
|
||||||
av_buffer_unref(&ctx->hwdevice_ref);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#define OFFSET(x) offsetof(HWMapContext, x)
|
#define OFFSET(x) offsetof(HWMapContext, x)
|
||||||
@ -297,6 +324,10 @@ static const AVOption hwmap_options[] = {
|
|||||||
0, AV_OPT_TYPE_CONST, { .i64 = AV_HWFRAME_MAP_DIRECT },
|
0, AV_OPT_TYPE_CONST, { .i64 = AV_HWFRAME_MAP_DIRECT },
|
||||||
INT_MIN, INT_MAX, FLAGS, "mode" },
|
INT_MIN, INT_MAX, FLAGS, "mode" },
|
||||||
|
|
||||||
|
{ "derive_device", "Derive a new device of this type",
|
||||||
|
OFFSET(derive_device_type), AV_OPT_TYPE_STRING,
|
||||||
|
{ .str = NULL }, 0, 0, FLAGS },
|
||||||
|
|
||||||
{ NULL }
|
{ NULL }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user