提交 38cb05f1 编写于 作者: M Mark Thompson

vf_hwmap: Add device derivation

Also refactor a little and improve error messages to make failure
cases easier to understand.
上级 c5714b51
...@@ -30,10 +30,10 @@ ...@@ -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;
...@@ -52,6 +52,7 @@ static int hwmap_config_output(AVFilterLink *outlink) ...@@ -52,6 +52,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;
...@@ -59,30 +60,58 @@ static int hwmap_config_output(AVFilterLink *outlink) ...@@ -59,30 +60,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) ||
...@@ -90,8 +119,6 @@ static int hwmap_config_output(AVFilterLink *outlink) ...@@ -90,8 +119,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);
...@@ -115,15 +142,17 @@ static int hwmap_config_output(AVFilterLink *outlink) ...@@ -115,15 +142,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;
...@@ -161,7 +190,6 @@ static int hwmap_config_output(AVFilterLink *outlink) ...@@ -161,7 +190,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;
} }
...@@ -269,7 +297,6 @@ static av_cold void hwmap_uninit(AVFilterContext *avctx) ...@@ -269,7 +297,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)
...@@ -293,6 +320,10 @@ static const AVOption hwmap_options[] = { ...@@ -293,6 +320,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 },
}; };
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册