]> git.sesse.net Git - ffmpeg/blobdiff - libavfilter/vf_scale_vaapi.c
avfilter/vf_v360: fix some small nits
[ffmpeg] / libavfilter / vf_scale_vaapi.c
index 22e928c098871f8ca88d5d6557fe5fa86f2d305b..8298a013da43a881a4473ac8e018ebe9d6a83e8a 100644 (file)
 
 #include <string.h>
 
-#include <va/va.h>
-#include <va/va_vpp.h>
-
 #include "libavutil/avassert.h"
-#include "libavutil/hwcontext.h"
-#include "libavutil/hwcontext_vaapi.h"
 #include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "avfilter.h"
 #include "formats.h"
 #include "internal.h"
-#include "scale.h"
+#include "scale_eval.h"
 #include "video.h"
+#include "vaapi_vpp.h"
 
 typedef struct ScaleVAAPIContext {
-    const AVClass *class;
-
-    AVVAAPIDeviceContext *hwctx;
-    AVBufferRef *device_ref;
-
-    int valid_ids;
-    VAConfigID  va_config;
-    VAContextID va_context;
-
-    AVBufferRef       *input_frames_ref;
-    AVHWFramesContext *input_frames;
-
-    AVBufferRef       *output_frames_ref;
-    AVHWFramesContext *output_frames;
+    VAAPIVPPContext vpp_ctx; // must be the first field
 
     char *output_format_string;
-    enum AVPixelFormat output_format;
+
+    int   mode;
 
     char *w_expr;      // width expression string
     char *h_expr;      // height expression string
 
-    int output_width;  // computed width
-    int output_height; // computed height
-} ScaleVAAPIContext;
-
-
-static int scale_vaapi_query_formats(AVFilterContext *avctx)
-{
-    enum AVPixelFormat pix_fmts[] = {
-        AV_PIX_FMT_VAAPI, AV_PIX_FMT_NONE,
-    };
-    int err;
+    int force_original_aspect_ratio;
+    int force_divisible_by;
 
-    if ((err = ff_formats_ref(ff_make_format_list(pix_fmts),
-                              &avctx->inputs[0]->out_formats)) < 0)
-        return err;
-    if ((err = ff_formats_ref(ff_make_format_list(pix_fmts),
-                              &avctx->outputs[0]->in_formats)) < 0)
-        return err;
+    char *colour_primaries_string;
+    char *colour_transfer_string;
+    char *colour_matrix_string;
+    int   colour_range;
+    char *chroma_location_string;
 
-    return 0;
-}
+    enum AVColorPrimaries colour_primaries;
+    enum AVColorTransferCharacteristic colour_transfer;
+    enum AVColorSpace colour_matrix;
+    enum AVChromaLocation chroma_location;
+} ScaleVAAPIContext;
 
-static int scale_vaapi_pipeline_uninit(ScaleVAAPIContext *ctx)
+static const char *scale_vaapi_mode_name(int mode)
 {
-    if (ctx->va_context != VA_INVALID_ID) {
-        vaDestroyContext(ctx->hwctx->display, ctx->va_context);
-        ctx->va_context = VA_INVALID_ID;
-    }
-
-    if (ctx->va_config != VA_INVALID_ID) {
-        vaDestroyConfig(ctx->hwctx->display, ctx->va_config);
-        ctx->va_config = VA_INVALID_ID;
+    switch (mode) {
+#define D(name) case VA_FILTER_SCALING_ ## name: return #name
+        D(DEFAULT);
+        D(FAST);
+        D(HQ);
+        D(NL_ANAMORPHIC);
+#undef D
+    default:
+        return "Invalid";
     }
-
-    av_buffer_unref(&ctx->output_frames_ref);
-    av_buffer_unref(&ctx->device_ref);
-    ctx->hwctx = 0;
-
-    return 0;
 }
 
-static int scale_vaapi_config_input(AVFilterLink *inlink)
-{
-    AVFilterContext *avctx = inlink->dst;
-    ScaleVAAPIContext *ctx = avctx->priv;
-
-    scale_vaapi_pipeline_uninit(ctx);
-
-    if (!inlink->hw_frames_ctx) {
-        av_log(avctx, AV_LOG_ERROR, "A hardware frames reference is "
-               "required to associate the processing device.\n");
-        return AVERROR(EINVAL);
-    }
-
-    ctx->input_frames_ref = av_buffer_ref(inlink->hw_frames_ctx);
-    ctx->input_frames = (AVHWFramesContext*)ctx->input_frames_ref->data;
-
-    return 0;
-}
 
 static int scale_vaapi_config_output(AVFilterLink *outlink)
 {
-    AVFilterLink *inlink = outlink->src->inputs[0];
-    AVFilterContext *avctx = outlink->src;
-    ScaleVAAPIContext *ctx = avctx->priv;
-    AVVAAPIHWConfig *hwconfig = NULL;
-    AVHWFramesConstraints *constraints = NULL;
-    AVVAAPIFramesContext *va_frames;
-    VAStatus vas;
-    int err, i;
-
-    scale_vaapi_pipeline_uninit(ctx);
-
-    ctx->device_ref = av_buffer_ref(ctx->input_frames->device_ref);
-    ctx->hwctx = ((AVHWDeviceContext*)ctx->device_ref->data)->hwctx;
-
-    av_assert0(ctx->va_config == VA_INVALID_ID);
-    vas = vaCreateConfig(ctx->hwctx->display, VAProfileNone,
-                         VAEntrypointVideoProc, 0, 0, &ctx->va_config);
-    if (vas != VA_STATUS_SUCCESS) {
-        av_log(ctx, AV_LOG_ERROR, "Failed to create processing pipeline "
-               "config: %d (%s).\n", vas, vaErrorStr(vas));
-        err = AVERROR(EIO);
-        goto fail;
-    }
-
-    hwconfig = av_hwdevice_hwconfig_alloc(ctx->device_ref);
-    if (!hwconfig) {
-        err = AVERROR(ENOMEM);
-        goto fail;
-    }
-    hwconfig->config_id = ctx->va_config;
-
-    constraints = av_hwdevice_get_hwframe_constraints(ctx->device_ref,
-                                                      hwconfig);
-    if (!constraints) {
-        err = AVERROR(ENOMEM);
-        goto fail;
-    }
-
-    if (ctx->output_format == AV_PIX_FMT_NONE)
-        ctx->output_format = ctx->input_frames->sw_format;
-    if (constraints->valid_sw_formats) {
-        for (i = 0; constraints->valid_sw_formats[i] != AV_PIX_FMT_NONE; i++) {
-            if (ctx->output_format == constraints->valid_sw_formats[i])
-                break;
-        }
-        if (constraints->valid_sw_formats[i] == AV_PIX_FMT_NONE) {
-            av_log(ctx, AV_LOG_ERROR, "Hardware does not support output "
-                   "format %s.\n", av_get_pix_fmt_name(ctx->output_format));
-            err = AVERROR(EINVAL);
-            goto fail;
-        }
-    }
+    AVFilterLink *inlink     = outlink->src->inputs[0];
+    AVFilterContext *avctx   = outlink->src;
+    VAAPIVPPContext *vpp_ctx = avctx->priv;
+    ScaleVAAPIContext *ctx   = avctx->priv;
+    int err;
 
     if ((err = ff_scale_eval_dimensions(ctx,
                                         ctx->w_expr, ctx->h_expr,
                                         inlink, outlink,
-                                        &ctx->output_width, &ctx->output_height)) < 0)
-        goto fail;
-
-    if (ctx->output_width  < constraints->min_width  ||
-        ctx->output_height < constraints->min_height ||
-        ctx->output_width  > constraints->max_width  ||
-        ctx->output_height > constraints->max_height) {
-        av_log(ctx, AV_LOG_ERROR, "Hardware does not support scaling to "
-               "size %dx%d (constraints: width %d-%d height %d-%d).\n",
-               ctx->output_width, ctx->output_height,
-               constraints->min_width,  constraints->max_width,
-               constraints->min_height, constraints->max_height);
-        err = AVERROR(EINVAL);
-        goto fail;
-    }
-
-    ctx->output_frames_ref = av_hwframe_ctx_alloc(ctx->device_ref);
-    if (!ctx->output_frames_ref) {
-        av_log(ctx, AV_LOG_ERROR, "Failed to create HW frame context "
-               "for output.\n");
-        err = AVERROR(ENOMEM);
-        goto fail;
-    }
-
-    ctx->output_frames = (AVHWFramesContext*)ctx->output_frames_ref->data;
-
-    ctx->output_frames->format    = AV_PIX_FMT_VAAPI;
-    ctx->output_frames->sw_format = ctx->output_format;
-    ctx->output_frames->width     = ctx->output_width;
-    ctx->output_frames->height    = ctx->output_height;
-
-    // The number of output frames we need is determined by what follows
-    // the filter.  If it's an encoder with complex frame reference
-    // structures then this could be very high.
-    ctx->output_frames->initial_pool_size = 10;
-
-    err = av_hwframe_ctx_init(ctx->output_frames_ref);
-    if (err < 0) {
-        av_log(ctx, AV_LOG_ERROR, "Failed to initialise VAAPI frame "
-               "context for output: %d\n", err);
-        goto fail;
-    }
+                                        &vpp_ctx->output_width, &vpp_ctx->output_height)) < 0)
+        return err;
 
-    va_frames = ctx->output_frames->hwctx;
-
-    av_assert0(ctx->va_context == VA_INVALID_ID);
-    vas = vaCreateContext(ctx->hwctx->display, ctx->va_config,
-                          ctx->output_width, ctx->output_height,
-                          VA_PROGRESSIVE,
-                          va_frames->surface_ids, va_frames->nb_surfaces,
-                          &ctx->va_context);
-    if (vas != VA_STATUS_SUCCESS) {
-        av_log(ctx, AV_LOG_ERROR, "Failed to create processing pipeline "
-               "context: %d (%s).\n", vas, vaErrorStr(vas));
-        return AVERROR(EIO);
-    }
+    ff_scale_adjust_dimensions(inlink, &vpp_ctx->output_width, &vpp_ctx->output_height,
+                               ctx->force_original_aspect_ratio, ctx->force_divisible_by);
 
-    outlink->w = ctx->output_width;
-    outlink->h = ctx->output_height;
+    err = ff_vaapi_vpp_config_output(outlink);
+    if (err < 0)
+        return err;
 
-    outlink->hw_frames_ctx = av_buffer_ref(ctx->output_frames_ref);
-    if (!outlink->hw_frames_ctx) {
-        err = AVERROR(ENOMEM);
-        goto fail;
-    }
+    if (inlink->sample_aspect_ratio.num)
+        outlink->sample_aspect_ratio = av_mul_q((AVRational){outlink->h * inlink->w, outlink->w * inlink->h}, inlink->sample_aspect_ratio);
+    else
+        outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
 
-    av_freep(&hwconfig);
-    av_hwframe_constraints_free(&constraints);
     return 0;
-
-fail:
-    av_buffer_unref(&ctx->output_frames_ref);
-    av_freep(&hwconfig);
-    av_hwframe_constraints_free(&constraints);
-    return err;
-}
-
-static int vaapi_proc_colour_standard(enum AVColorSpace av_cs)
-{
-    switch(av_cs) {
-#define CS(av, va) case AVCOL_SPC_ ## av: return VAProcColorStandard ## va;
-        CS(BT709,     BT709);
-        CS(BT470BG,   BT601);
-        CS(SMPTE170M, SMPTE170M);
-        CS(SMPTE240M, SMPTE240M);
-#undef CS
-    default:
-        return VAProcColorStandardNone;
-    }
 }
 
 static int scale_vaapi_filter_frame(AVFilterLink *inlink, AVFrame *input_frame)
 {
-    AVFilterContext *avctx = inlink->dst;
-    AVFilterLink *outlink = avctx->outputs[0];
-    ScaleVAAPIContext *ctx = avctx->priv;
-    AVFrame *output_frame = NULL;
-    VASurfaceID input_surface, output_surface;
+    AVFilterContext *avctx   = inlink->dst;
+    AVFilterLink *outlink    = avctx->outputs[0];
+    VAAPIVPPContext *vpp_ctx = avctx->priv;
+    ScaleVAAPIContext *ctx   = avctx->priv;
+    AVFrame *output_frame    = NULL;
     VAProcPipelineParameterBuffer params;
-    VABufferID params_id;
-    VARectangle input_region;
-    VAStatus vas;
     int err;
 
-    av_log(ctx, AV_LOG_DEBUG, "Filter input: %s, %ux%u (%"PRId64").\n",
+    av_log(avctx, AV_LOG_DEBUG, "Filter input: %s, %ux%u (%"PRId64").\n",
            av_get_pix_fmt_name(input_frame->format),
            input_frame->width, input_frame->height, input_frame->pts);
 
-    if (ctx->va_context == VA_INVALID_ID)
+    if (vpp_ctx->va_context == VA_INVALID_ID)
         return AVERROR(EINVAL);
 
-    input_surface = (VASurfaceID)(uintptr_t)input_frame->data[3];
-    av_log(ctx, AV_LOG_DEBUG, "Using surface %#x for scale input.\n",
-           input_surface);
-
-    output_frame = ff_get_video_buffer(outlink, ctx->output_width,
-                                       ctx->output_height);
+    output_frame = ff_get_video_buffer(outlink, vpp_ctx->output_width,
+                                       vpp_ctx->output_height);
     if (!output_frame) {
         err = AVERROR(ENOMEM);
         goto fail;
     }
 
-    output_surface = (VASurfaceID)(uintptr_t)output_frame->data[3];
-    av_log(ctx, AV_LOG_DEBUG, "Using surface %#x for scale output.\n",
-           output_surface);
-
-    memset(&params, 0, sizeof(params));
-
-    // If there were top/left cropping, it could be taken into
-    // account here.
-    input_region = (VARectangle) {
-        .x      = 0,
-        .y      = 0,
-        .width  = input_frame->width,
-        .height = input_frame->height,
-    };
-
-    params.surface = input_surface;
-    params.surface_region = &input_region;
-    params.surface_color_standard =
-        vaapi_proc_colour_standard(input_frame->colorspace);
-
-    params.output_region = 0;
-    params.output_background_color = 0xff000000;
-    params.output_color_standard = params.surface_color_standard;
-
-    params.pipeline_flags = 0;
-    params.filter_flags = VA_FILTER_SCALING_HQ;
-
-    vas = vaBeginPicture(ctx->hwctx->display,
-                         ctx->va_context, output_surface);
-    if (vas != VA_STATUS_SUCCESS) {
-        av_log(ctx, AV_LOG_ERROR, "Failed to attach new picture: "
-               "%d (%s).\n", vas, vaErrorStr(vas));
-        err = AVERROR(EIO);
-        goto fail;
-    }
+    err = av_frame_copy_props(output_frame, input_frame);
+    if (err < 0)
+        return err;
 
-    vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
-                         VAProcPipelineParameterBufferType,
-                         sizeof(params), 1, &params, &params_id);
-    if (vas != VA_STATUS_SUCCESS) {
-        av_log(ctx, AV_LOG_ERROR, "Failed to create parameter buffer: "
-               "%d (%s).\n", vas, vaErrorStr(vas));
-        err = AVERROR(EIO);
-        goto fail_after_begin;
-    }
-    av_log(ctx, AV_LOG_DEBUG, "Pipeline parameter buffer is %#x.\n",
-           params_id);
-
-    vas = vaRenderPicture(ctx->hwctx->display, ctx->va_context,
-                          &params_id, 1);
-    if (vas != VA_STATUS_SUCCESS) {
-        av_log(ctx, AV_LOG_ERROR, "Failed to render parameter buffer: "
-               "%d (%s).\n", vas, vaErrorStr(vas));
-        err = AVERROR(EIO);
-        goto fail_after_begin;
-    }
+    if (ctx->colour_primaries != AVCOL_PRI_UNSPECIFIED)
+        output_frame->color_primaries = ctx->colour_primaries;
+    if (ctx->colour_transfer != AVCOL_TRC_UNSPECIFIED)
+        output_frame->color_trc = ctx->colour_transfer;
+    if (ctx->colour_matrix != AVCOL_SPC_UNSPECIFIED)
+        output_frame->colorspace = ctx->colour_matrix;
+    if (ctx->colour_range != AVCOL_RANGE_UNSPECIFIED)
+        output_frame->color_range = ctx->colour_range;
+    if (ctx->chroma_location != AVCHROMA_LOC_UNSPECIFIED)
+        output_frame->chroma_location = ctx->chroma_location;
+
+    err = ff_vaapi_vpp_init_params(avctx, &params,
+                                   input_frame, output_frame);
+    if (err < 0)
+        goto fail;
 
-    vas = vaEndPicture(ctx->hwctx->display, ctx->va_context);
-    if (vas != VA_STATUS_SUCCESS) {
-        av_log(ctx, AV_LOG_ERROR, "Failed to start picture processing: "
-               "%d (%s).\n", vas, vaErrorStr(vas));
-        err = AVERROR(EIO);
-        goto fail_after_render;
-    }
+    params.filter_flags |= ctx->mode;
 
-    if (CONFIG_VAAPI_1 || ctx->hwctx->driver_quirks &
-        AV_VAAPI_DRIVER_QUIRK_RENDER_PARAM_BUFFERS) {
-        vas = vaDestroyBuffer(ctx->hwctx->display, params_id);
-        if (vas != VA_STATUS_SUCCESS) {
-            av_log(ctx, AV_LOG_ERROR, "Failed to free parameter buffer: "
-                   "%d (%s).\n", vas, vaErrorStr(vas));
-            // And ignore.
-        }
-    }
+    err = ff_vaapi_vpp_render_picture(avctx, &params, output_frame);
+    if (err < 0)
+        goto fail;
 
-    av_frame_copy_props(output_frame, input_frame);
     av_frame_free(&input_frame);
 
-    av_log(ctx, AV_LOG_DEBUG, "Filter output: %s, %ux%u (%"PRId64").\n",
+    av_log(avctx, AV_LOG_DEBUG, "Filter output: %s, %ux%u (%"PRId64"), mode: %s.\n",
            av_get_pix_fmt_name(output_frame->format),
-           output_frame->width, output_frame->height, output_frame->pts);
+           output_frame->width, output_frame->height, output_frame->pts,
+           scale_vaapi_mode_name(ctx->mode));
 
     return ff_filter_frame(outlink, output_frame);
 
-    // We want to make sure that if vaBeginPicture has been called, we also
-    // call vaRenderPicture and vaEndPicture.  These calls may well fail or
-    // do something else nasty, but once we're in this failure case there
-    // isn't much else we can do.
-fail_after_begin:
-    vaRenderPicture(ctx->hwctx->display, ctx->va_context, &params_id, 1);
-fail_after_render:
-    vaEndPicture(ctx->hwctx->display, ctx->va_context);
 fail:
     av_frame_free(&input_frame);
     av_frame_free(&output_frame);
@@ -396,39 +166,44 @@ fail:
 
 static av_cold int scale_vaapi_init(AVFilterContext *avctx)
 {
-    ScaleVAAPIContext *ctx = avctx->priv;
+    VAAPIVPPContext *vpp_ctx = avctx->priv;
+    ScaleVAAPIContext *ctx   = avctx->priv;
 
-    ctx->va_config  = VA_INVALID_ID;
-    ctx->va_context = VA_INVALID_ID;
-    ctx->valid_ids  = 1;
+    ff_vaapi_vpp_ctx_init(avctx);
+    vpp_ctx->pipeline_uninit = ff_vaapi_vpp_pipeline_uninit;
 
     if (ctx->output_format_string) {
-        ctx->output_format = av_get_pix_fmt(ctx->output_format_string);
-        if (ctx->output_format == AV_PIX_FMT_NONE) {
-            av_log(ctx, AV_LOG_ERROR, "Invalid output format.\n");
+        vpp_ctx->output_format = av_get_pix_fmt(ctx->output_format_string);
+        if (vpp_ctx->output_format == AV_PIX_FMT_NONE) {
+            av_log(avctx, AV_LOG_ERROR, "Invalid output format.\n");
             return AVERROR(EINVAL);
         }
     } else {
         // Use the input format once that is configured.
-        ctx->output_format = AV_PIX_FMT_NONE;
+        vpp_ctx->output_format = AV_PIX_FMT_NONE;
     }
 
-    return 0;
-}
-
-static av_cold void scale_vaapi_uninit(AVFilterContext *avctx)
-{
-    ScaleVAAPIContext *ctx = avctx->priv;
+#define STRING_OPTION(var_name, func_name, default_value) do { \
+        if (ctx->var_name ## _string) { \
+            int var = av_ ## func_name ## _from_name(ctx->var_name ## _string); \
+            if (var < 0) { \
+                av_log(avctx, AV_LOG_ERROR, "Invalid %s.\n", #var_name); \
+                return AVERROR(EINVAL); \
+            } \
+            ctx->var_name = var; \
+        } else { \
+            ctx->var_name = default_value; \
+        } \
+    } while (0)
+
+    STRING_OPTION(colour_primaries, color_primaries, AVCOL_PRI_UNSPECIFIED);
+    STRING_OPTION(colour_transfer,  color_transfer,  AVCOL_TRC_UNSPECIFIED);
+    STRING_OPTION(colour_matrix,    color_space,     AVCOL_SPC_UNSPECIFIED);
+    STRING_OPTION(chroma_location,  chroma_location, AVCHROMA_LOC_UNSPECIFIED);
 
-    if (ctx->valid_ids)
-        scale_vaapi_pipeline_uninit(ctx);
-
-    av_buffer_unref(&ctx->input_frames_ref);
-    av_buffer_unref(&ctx->output_frames_ref);
-    av_buffer_unref(&ctx->device_ref);
+    return 0;
 }
 
-
 #define OFFSET(x) offsetof(ScaleVAAPIContext, x)
 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM)
 static const AVOption scale_vaapi_options[] = {
@@ -438,22 +213,63 @@ static const AVOption scale_vaapi_options[] = {
       OFFSET(h_expr), AV_OPT_TYPE_STRING, {.str = "ih"}, .flags = FLAGS },
     { "format", "Output video format (software format of hardware frames)",
       OFFSET(output_format_string), AV_OPT_TYPE_STRING, .flags = FLAGS },
+    { "mode", "Scaling mode",
+      OFFSET(mode), AV_OPT_TYPE_INT, { .i64 = VA_FILTER_SCALING_HQ },
+      0, VA_FILTER_SCALING_NL_ANAMORPHIC, FLAGS, "mode" },
+        { "default", "Use the default (depend on the driver) scaling algorithm",
+          0, AV_OPT_TYPE_CONST, { .i64 = VA_FILTER_SCALING_DEFAULT }, 0, 0, FLAGS, "mode" },
+        { "fast", "Use fast scaling algorithm",
+          0, AV_OPT_TYPE_CONST, { .i64 = VA_FILTER_SCALING_FAST }, 0, 0, FLAGS, "mode" },
+        { "hq", "Use high quality scaling algorithm",
+          0, AV_OPT_TYPE_CONST, { .i64 = VA_FILTER_SCALING_HQ }, 0, 0, FLAGS,  "mode" },
+        { "nl_anamorphic", "Use nolinear anamorphic scaling algorithm",
+          0, AV_OPT_TYPE_CONST, { .i64 = VA_FILTER_SCALING_NL_ANAMORPHIC }, 0, 0, FLAGS,  "mode" },
+
+    // These colour properties match the ones of the same name in vf_scale.
+    { "out_color_matrix", "Output colour matrix coefficient set",
+      OFFSET(colour_matrix_string), AV_OPT_TYPE_STRING, { .str = NULL }, .flags = FLAGS },
+    { "out_range", "Output colour range",
+      OFFSET(colour_range), AV_OPT_TYPE_INT, { .i64 = AVCOL_RANGE_UNSPECIFIED },
+      AVCOL_RANGE_UNSPECIFIED, AVCOL_RANGE_JPEG, FLAGS, "range" },
+    { "full",    "Full range",
+      0, AV_OPT_TYPE_CONST, { .i64 = AVCOL_RANGE_JPEG }, 0, 0, FLAGS, "range" },
+    { "limited", "Limited range",
+      0, AV_OPT_TYPE_CONST, { .i64 = AVCOL_RANGE_MPEG }, 0, 0, FLAGS, "range" },
+    { "jpeg",    "Full range",
+      0, AV_OPT_TYPE_CONST, { .i64 = AVCOL_RANGE_JPEG }, 0, 0, FLAGS, "range" },
+    { "mpeg",    "Limited range",
+      0, AV_OPT_TYPE_CONST, { .i64 = AVCOL_RANGE_MPEG }, 0, 0, FLAGS, "range" },
+    { "tv",      "Limited range",
+      0, AV_OPT_TYPE_CONST, { .i64 = AVCOL_RANGE_MPEG }, 0, 0, FLAGS, "range" },
+    { "pc",      "Full range",
+      0, AV_OPT_TYPE_CONST, { .i64 = AVCOL_RANGE_JPEG }, 0, 0, FLAGS, "range" },
+    // These colour properties are new here.
+    { "out_color_primaries", "Output colour primaries",
+      OFFSET(colour_primaries_string), AV_OPT_TYPE_STRING,
+      { .str = NULL }, .flags = FLAGS },
+    { "out_color_transfer", "Output colour transfer characteristics",
+      OFFSET(colour_transfer_string),  AV_OPT_TYPE_STRING,
+      { .str = NULL }, .flags = FLAGS },
+    { "out_chroma_location", "Output chroma sample location",
+      OFFSET(chroma_location_string),  AV_OPT_TYPE_STRING,
+      { .str = NULL }, .flags = FLAGS },
+    { "force_original_aspect_ratio", "decrease or increase w/h if necessary to keep the original AR", OFFSET(force_original_aspect_ratio), AV_OPT_TYPE_INT, { .i64 = 0}, 0, 2, FLAGS, "force_oar" },
+    { "disable",  NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 0 }, 0, 0, FLAGS, "force_oar" },
+    { "decrease", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 1 }, 0, 0, FLAGS, "force_oar" },
+    { "increase", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 2 }, 0, 0, FLAGS, "force_oar" },
+    { "force_divisible_by", "enforce that the output resolution is divisible by a defined integer when force_original_aspect_ratio is used", OFFSET(force_divisible_by), AV_OPT_TYPE_INT, { .i64 = 1}, 1, 256, FLAGS },
+
     { NULL },
 };
 
-static const AVClass scale_vaapi_class = {
-    .class_name = "scale_vaapi",
-    .item_name  = av_default_item_name,
-    .option     = scale_vaapi_options,
-    .version    = LIBAVUTIL_VERSION_INT,
-};
+AVFILTER_DEFINE_CLASS(scale_vaapi);
 
 static const AVFilterPad scale_vaapi_inputs[] = {
     {
         .name         = "default",
         .type         = AVMEDIA_TYPE_VIDEO,
         .filter_frame = &scale_vaapi_filter_frame,
-        .config_props = &scale_vaapi_config_input,
+        .config_props = &ff_vaapi_vpp_config_input,
     },
     { NULL }
 };
@@ -472,8 +288,8 @@ AVFilter ff_vf_scale_vaapi = {
     .description   = NULL_IF_CONFIG_SMALL("Scale to/from VAAPI surfaces."),
     .priv_size     = sizeof(ScaleVAAPIContext),
     .init          = &scale_vaapi_init,
-    .uninit        = &scale_vaapi_uninit,
-    .query_formats = &scale_vaapi_query_formats,
+    .uninit        = &ff_vaapi_vpp_ctx_uninit,
+    .query_formats = &ff_vaapi_vpp_query_formats,
     .inputs        = scale_vaapi_inputs,
     .outputs       = scale_vaapi_outputs,
     .priv_class    = &scale_vaapi_class,