Rather than query_formats(). Init is a more appropriate place, as query_formats() is supposed to be free of side-effects.
Unified · Split
| @@ -39,35 +39,41 @@ typedef struct HWUploadContext { · 38 unmodified lines | ||
| 39 | 39 | char *device_type; |
| 40 | 40 | } HWUploadContext; |
| 41 | 41 | |
| 42 | −static int hwupload_query_formats(AVFilterContext *avctx) | |
| 42 | +static int hwupload_init(AVFilterContext *avctx) | |
| 43 | 43 | { |
| 44 | 44 | HWUploadContext *ctx = avctx->priv; |
| 45 | − AVHWFramesConstraints *constraints = NULL; | |
| 46 | − const enum AVPixelFormat *input_pix_fmts, *output_pix_fmts; | |
| 47 | − AVFilterFormats *input_formats = NULL; | |
| 48 | − int err, i; | |
| 45 | + int err; | |
| 49 | 46 | |
| 50 | − if (ctx->hwdevice_ref) { | |
| 51 | − /* We already have a specified device. */ | |
| 52 | − } else if (avctx->hw_device_ctx) { | |
| 53 | − if (ctx->device_type) { | |
| 54 | − err = av_hwdevice_ctx_create_derived( | |
| 55 | − &ctx->hwdevice_ref, | |
| 56 | − av_hwdevice_find_type_by_name(ctx->device_type), | |
| 57 | − avctx->hw_device_ctx, 0); | |
| 58 | − if (err < 0) | |
| 59 | − return err; | |
| 60 | − } else { | |
| 61 | − ctx->hwdevice_ref = av_buffer_ref(avctx->hw_device_ctx); | |
| 62 | − if (!ctx->hwdevice_ref) | |
| 63 | − return AVERROR(ENOMEM); | |
| 64 | − } | |
| 65 | − } else { | |
| 47 | + if (!avctx->hw_device_ctx) { | |
| 66 | 48 | av_log(ctx, AV_LOG_ERROR, "A hardware device reference is required " |
| 67 | 49 | "to upload frames to.\n"); |
| 68 | 50 | return AVERROR(EINVAL); |
| 69 | 51 | } |
| 70 | 52 | |
| 53 | + if (ctx->device_type) { | |
| 54 | + err = av_hwdevice_ctx_create_derived( | |
| 55 | + &ctx->hwdevice_ref, | |
| 56 | + av_hwdevice_find_type_by_name(ctx->device_type), | |
| 57 | + avctx->hw_device_ctx, 0); | |
| 58 | + if (err < 0) | |
| 59 | + return err; | |
| 60 | + } else { | |
| 61 | + ctx->hwdevice_ref = av_buffer_ref(avctx->hw_device_ctx); | |
| 62 | + if (!ctx->hwdevice_ref) | |
| 63 | + return AVERROR(ENOMEM); | |
| 64 | + } | |
| 65 | + | |
| 66 | + return 0; | |
| 67 | +} | |
| 68 | + | |
| 69 | +static int hwupload_query_formats(AVFilterContext *avctx) | |
| 70 | +{ | |
| 71 | + HWUploadContext *ctx = avctx->priv; | |
| 72 | + AVHWFramesConstraints *constraints = NULL; | |
| 73 | + const enum AVPixelFormat *input_pix_fmts, *output_pix_fmts; | |
| 74 | + AVFilterFormats *input_formats = NULL; | |
| 75 | + int err, i; | |
| 76 | + | |
| 71 | 77 | constraints = av_hwdevice_get_hwframe_constraints(ctx->hwdevice_ref, NULL); |
| 72 | 78 | if (!constraints) { |
| 73 | 79 | err = AVERROR(EINVAL); |
| @@ -253,6 +259,7 @@ static const AVFilterPad hwupload_outputs[] = { · 179 unmodified lines | ||
| 253 | 259 | const AVFilter ff_vf_hwupload = { |
| 254 | 260 | .name = "hwupload", |
| 255 | 261 | .description = NULL_IF_CONFIG_SMALL("Upload a normal frame to a hardware frame"), |
| 262 | + .init = hwupload_init, | |
| 256 | 263 | .uninit = hwupload_uninit, |
| 257 | 264 | .priv_size = sizeof(HWUploadContext), |
| 258 | 265 | .priv_class = &hwupload_class, |