Commit 3b188666 authored by Mark Thompson's avatar Mark Thompson

vaapi_encode: Choose profiles dynamically

Previously there was one fixed choice for each codec (e.g. H.265 -> Main
profile), and using anything else then required an explicit option from
the user.  This changes to selecting the profile based on the input format
and the set of profiles actually supported by the driver (e.g. P010 input
will choose Main 10 profile for H.265 if the driver supports it).

The entrypoint and render target format are also chosen dynamically in the
same way, removing those explicit selections from the per-codec code.
parent a00763be
......@@ -2565,6 +2565,9 @@ The following standard libavcodec options are used:
@option{bf} / @option{max_b_frames}
@item
@option{profile}
If not set, this will be determined automatically from the format of the input
frames and the profiles supported by the driver.
@item
@option{level}
@item
......
This diff is collapsed.
......@@ -23,6 +23,10 @@
#include <va/va.h>
#if VA_CHECK_VERSION(1, 0, 0)
#include <va/va_str.h>
#endif
#include "libavutil/hwcontext.h"
#include "libavutil/hwcontext_vaapi.h"
......@@ -86,18 +90,32 @@ typedef struct VAAPIEncodePicture {
VAAPIEncodeSlice *slices;
} VAAPIEncodePicture;
typedef struct VAAPIEncodeProfile {
// lavc profile value (FF_PROFILE_*).
int av_profile;
// Supported bit depth.
int depth;
// Number of components.
int nb_components;
// Chroma subsampling in width dimension.
int log2_chroma_w;
// Chroma subsampling in height dimension.
int log2_chroma_h;
// VAAPI profile value.
VAProfile va_profile;
} VAAPIEncodeProfile;
typedef struct VAAPIEncodeContext {
const AVClass *class;
// Codec-specific hooks.
const struct VAAPIEncodeType *codec;
// Encoding profile (VAProfileXXX).
VAProfile va_profile;
// Encoding entrypoint (usually VAEntryointEncSlice).
VAEntrypoint va_entrypoint;
// Surface colour/sampling format (usually VA_RT_FORMAT_YUV420).
unsigned int va_rt_format;
// Global options.
// Use low power encoding mode.
int low_power;
// Rate control mode.
unsigned int va_rc_mode;
// Supported packed headers (initially the desired set, modified
......@@ -113,6 +131,14 @@ typedef struct VAAPIEncodeContext {
// Everything above this point must be set before calling
// ff_vaapi_encode_init().
// Chosen encoding profile details.
const VAAPIEncodeProfile *profile;
// Encoding profile (VAProfile*).
VAProfile va_profile;
// Encoding entrypoint (VAEntryoint*).
VAEntrypoint va_entrypoint;
// Configuration attributes to use when creating va_config.
VAConfigAttrib config_attributes[MAX_CONFIG_ATTRIBUTES];
int nb_config_attributes;
......@@ -204,8 +230,11 @@ typedef struct VAAPIEncodeContext {
int end_of_stream;
} VAAPIEncodeContext;
typedef struct VAAPIEncodeType {
// List of supported profiles and corresponding VAAPI profiles.
// (Must end with FF_PROFILE_UNKNOWN.)
const VAAPIEncodeProfile *profiles;
// Perform any extra codec-specific configuration after the
// codec context is initialised (set up the private data and
// add any necessary global parameters).
......
......@@ -866,7 +866,17 @@ static av_cold int vaapi_encode_h264_configure(AVCodecContext *avctx)
return 0;
}
static const VAAPIEncodeProfile vaapi_encode_h264_profiles[] = {
{ FF_PROFILE_H264_HIGH, 8, 3, 1, 1, VAProfileH264High },
{ FF_PROFILE_H264_MAIN, 8, 3, 1, 1, VAProfileH264Main },
{ FF_PROFILE_H264_CONSTRAINED_BASELINE,
8, 3, 1, 1, VAProfileH264ConstrainedBaseline },
{ FF_PROFILE_UNKNOWN }
};
static const VAAPIEncodeType vaapi_encode_type_h264 = {
.profiles = vaapi_encode_h264_profiles,
.configure = &vaapi_encode_h264_configure,
.sequence_params_size = sizeof(VAEncSequenceParameterBufferH264),
......@@ -899,30 +909,17 @@ static av_cold int vaapi_encode_h264_init(AVCodecContext *avctx)
if (avctx->level == FF_LEVEL_UNKNOWN)
avctx->level = priv->level;
// Reject unsupported profiles.
switch (avctx->profile) {
case FF_PROFILE_H264_BASELINE:
av_log(avctx, AV_LOG_WARNING, "H.264 baseline profile is not "
"supported, using constrained baseline profile instead.\n");
avctx->profile = FF_PROFILE_H264_CONSTRAINED_BASELINE;
case FF_PROFILE_H264_CONSTRAINED_BASELINE:
ctx->va_profile = VAProfileH264ConstrainedBaseline;
if (avctx->max_b_frames != 0) {
avctx->max_b_frames = 0;
av_log(avctx, AV_LOG_WARNING, "H.264 constrained baseline profile "
"doesn't support encoding with B frames, disabling them.\n");
}
break;
case FF_PROFILE_H264_MAIN:
ctx->va_profile = VAProfileH264Main;
break;
case FF_PROFILE_H264_EXTENDED:
av_log(avctx, AV_LOG_ERROR, "H.264 extended profile "
"is not supported.\n");
return AVERROR_PATCHWELCOME;
case FF_PROFILE_UNKNOWN:
case FF_PROFILE_H264_HIGH:
ctx->va_profile = VAProfileH264High;
break;
case FF_PROFILE_H264_HIGH_10:
case FF_PROFILE_H264_HIGH_10_INTRA:
av_log(avctx, AV_LOG_ERROR, "H.264 10-bit profiles "
......@@ -937,25 +934,9 @@ static av_cold int vaapi_encode_h264_init(AVCodecContext *avctx)
av_log(avctx, AV_LOG_ERROR, "H.264 non-4:2:0 profiles "
"are not supported.\n");
return AVERROR_PATCHWELCOME;
default:
av_log(avctx, AV_LOG_ERROR, "Unknown H.264 profile %d.\n",
avctx->profile);
return AVERROR(EINVAL);
}
if (priv->low_power) {
#if VA_CHECK_VERSION(0, 39, 2)
ctx->va_entrypoint = VAEntrypointEncSliceLP;
#else
av_log(avctx, AV_LOG_ERROR, "Low-power encoding is not "
"supported with this VAAPI version.\n");
return AVERROR(EINVAL);
#endif
} else {
ctx->va_entrypoint = VAEntrypointEncSlice;
}
// Only 8-bit encode is supported.
ctx->va_rt_format = VA_RT_FORMAT_YUV420;
ctx->low_power = priv->low_power;
if (avctx->bit_rate > 0) {
if (avctx->rc_max_rate == avctx->bit_rate)
......@@ -1022,7 +1003,7 @@ static const AVOption vaapi_encode_h264_options[] = {
{ "profile", "Set profile (profile_idc and constraint_set*_flag)",
OFFSET(profile), AV_OPT_TYPE_INT,
{ .i64 = FF_PROFILE_H264_HIGH }, 0x0000, 0xffff, FLAGS, "profile" },
{ .i64 = FF_PROFILE_UNKNOWN }, FF_PROFILE_UNKNOWN, 0xffff, FLAGS, "profile" },
#define PROFILE(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
{ .i64 = value }, 0, 0, FLAGS, "profile"
......
......@@ -1025,7 +1025,17 @@ static av_cold int vaapi_encode_h265_configure(AVCodecContext *avctx)
return 0;
}
static const VAAPIEncodeProfile vaapi_encode_h265_profiles[] = {
{ FF_PROFILE_HEVC_MAIN, 8, 3, 1, 1, VAProfileHEVCMain },
#if VA_CHECK_VERSION(0, 37, 0)
{ FF_PROFILE_HEVC_MAIN_10, 10, 3, 1, 1, VAProfileHEVCMain10 },
#endif
{ FF_PROFILE_UNKNOWN }
};
static const VAAPIEncodeType vaapi_encode_type_h265 = {
.profiles = vaapi_encode_h265_profiles,
.configure = &vaapi_encode_h265_configure,
.sequence_params_size = sizeof(VAEncSequenceParameterBufferHEVC),
......@@ -1058,29 +1068,6 @@ static av_cold int vaapi_encode_h265_init(AVCodecContext *avctx)
if (avctx->level == FF_LEVEL_UNKNOWN)
avctx->level = priv->level;
switch (avctx->profile) {
case FF_PROFILE_HEVC_MAIN:
case FF_PROFILE_UNKNOWN:
ctx->va_profile = VAProfileHEVCMain;
ctx->va_rt_format = VA_RT_FORMAT_YUV420;
break;
case FF_PROFILE_HEVC_MAIN_10:
#ifdef VA_RT_FORMAT_YUV420_10BPP
ctx->va_profile = VAProfileHEVCMain10;
ctx->va_rt_format = VA_RT_FORMAT_YUV420_10BPP;
break;
#else
av_log(avctx, AV_LOG_ERROR, "10-bit encoding is not "
"supported with this VAAPI version.\n");
return AVERROR(ENOSYS);
#endif
default:
av_log(avctx, AV_LOG_ERROR, "Unknown H.265 profile %d.\n",
avctx->profile);
return AVERROR(EINVAL);
}
ctx->va_entrypoint = VAEntrypointEncSlice;
if (avctx->bit_rate > 0) {
if (avctx->rc_max_rate == avctx->bit_rate)
ctx->va_rc_mode = VA_RC_CBR;
......@@ -1120,7 +1107,7 @@ static const AVOption vaapi_encode_h265_options[] = {
{ "profile", "Set profile (general_profile_idc)",
OFFSET(profile), AV_OPT_TYPE_INT,
{ .i64 = FF_PROFILE_HEVC_MAIN }, 0x00, 0xff, FLAGS, "profile" },
{ .i64 = FF_PROFILE_UNKNOWN }, FF_PROFILE_UNKNOWN, 0xff, FLAGS, "profile" },
#define PROFILE(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
{ .i64 = value }, 0, 0, FLAGS, "profile"
......
......@@ -359,7 +359,15 @@ static av_cold int vaapi_encode_mjpeg_configure(AVCodecContext *avctx)
return 0;
}
static const VAAPIEncodeProfile vaapi_encode_mjpeg_profiles[] = {
{ FF_PROFILE_MJPEG_HUFFMAN_BASELINE_DCT,
8, 3, 1, 1, VAProfileJPEGBaseline },
{ FF_PROFILE_UNKNOWN }
};
static const VAAPIEncodeType vaapi_encode_type_mjpeg = {
.profiles = vaapi_encode_mjpeg_profiles,
.configure = &vaapi_encode_mjpeg_configure,
.picture_params_size = sizeof(VAEncPictureParameterBufferJPEG),
......@@ -380,11 +388,6 @@ static av_cold int vaapi_encode_mjpeg_init(AVCodecContext *avctx)
ctx->codec = &vaapi_encode_type_mjpeg;
ctx->va_profile = VAProfileJPEGBaseline;
ctx->va_entrypoint = VAEntrypointEncPicture;
ctx->va_rt_format = VA_RT_FORMAT_YUV420;
ctx->va_rc_mode = VA_RC_CQP;
// The JPEG image header - see note above.
......
......@@ -552,7 +552,15 @@ static av_cold int vaapi_encode_mpeg2_configure(AVCodecContext *avctx)
return 0;
}
static const VAAPIEncodeProfile vaapi_encode_mpeg2_profiles[] = {
{ FF_PROFILE_MPEG2_MAIN, 8, 3, 1, 1, VAProfileMPEG2Main },
{ FF_PROFILE_MPEG2_SIMPLE, 8, 3, 1, 1, VAProfileMPEG2Simple },
{ FF_PROFILE_UNKNOWN }
};
static const VAAPIEncodeType vaapi_encode_type_mpeg2 = {
.profiles = vaapi_encode_mpeg2_profiles,
.configure = &vaapi_encode_mpeg2_configure,
.sequence_params_size = sizeof(VAEncSequenceParameterBufferMPEG2),
......@@ -577,31 +585,6 @@ static av_cold int vaapi_encode_mpeg2_init(AVCodecContext *avctx)
ctx->codec = &vaapi_encode_type_mpeg2;
switch (avctx->profile) {
case FF_PROFILE_MPEG2_SIMPLE:
ctx->va_profile = VAProfileMPEG2Simple;
break;
case FF_PROFILE_MPEG2_MAIN:
ctx->va_profile = VAProfileMPEG2Main;
break;
case FF_PROFILE_MPEG2_422:
av_log(avctx, AV_LOG_ERROR, "MPEG-2 4:2:2 profile "
"is not supported.\n");
return AVERROR_PATCHWELCOME;
case FF_PROFILE_MPEG2_HIGH:
av_log(avctx, AV_LOG_ERROR, "MPEG-2 high profile "
"is not supported.\n");
return AVERROR_PATCHWELCOME;
case FF_PROFILE_MPEG2_SS:
case FF_PROFILE_MPEG2_SNR_SCALABLE:
av_log(avctx, AV_LOG_ERROR, "MPEG-2 scalable profiles "
"are not supported.\n");
return AVERROR_PATCHWELCOME;
default:
av_log(avctx, AV_LOG_ERROR, "Unknown MPEG-2 profile %d.\n",
avctx->profile);
return AVERROR(EINVAL);
}
switch (avctx->level) {
case 4: // High
case 6: // High 1440
......@@ -620,8 +603,6 @@ static av_cold int vaapi_encode_mpeg2_init(AVCodecContext *avctx)
return AVERROR(EINVAL);
}
ctx->va_entrypoint = VAEntrypointEncSlice;
ctx->va_rt_format = VA_RT_FORMAT_YUV420;
ctx->va_rc_mode = VA_RC_CQP;
ctx->va_packed_headers = VA_ENC_PACKED_HEADER_SEQUENCE |
......@@ -643,7 +624,6 @@ static av_cold int vaapi_encode_mpeg2_close(AVCodecContext *avctx)
}
static const AVCodecDefault vaapi_encode_mpeg2_defaults[] = {
{ "profile", "4" },
{ "level", "4" },
{ "bf", "1" },
{ "g", "120" },
......
......@@ -175,7 +175,14 @@ static av_cold int vaapi_encode_vp8_configure(AVCodecContext *avctx)
return 0;
}
static const VAAPIEncodeProfile vaapi_encode_vp8_profiles[] = {
{ 0 /* VP8 has no profiles */, 8, 3, 1, 1, VAProfileVP8Version0_3 },
{ FF_PROFILE_UNKNOWN }
};
static const VAAPIEncodeType vaapi_encode_type_vp8 = {
.profiles = vaapi_encode_vp8_profiles,
.configure = &vaapi_encode_vp8_configure,
.sequence_params_size = sizeof(VAEncSequenceParameterBufferVP8),
......@@ -198,10 +205,6 @@ static av_cold int vaapi_encode_vp8_init(AVCodecContext *avctx)
ctx->codec = &vaapi_encode_type_vp8;
ctx->va_profile = VAProfileVP8Version0_3;
ctx->va_entrypoint = VAEntrypointEncSlice;
ctx->va_rt_format = VA_RT_FORMAT_YUV420;
if (avctx->flags & AV_CODEC_FLAG_QSCALE) {
ctx->va_rc_mode = VA_RC_CQP;
} else if (avctx->bit_rate > 0) {
......
......@@ -203,7 +203,15 @@ static av_cold int vaapi_encode_vp9_configure(AVCodecContext *avctx)
return 0;
}
static const VAAPIEncodeProfile vaapi_encode_vp9_profiles[] = {
{ FF_PROFILE_VP9_0, 8, 3, 1, 1, VAProfileVP9Profile0 },
{ FF_PROFILE_VP9_2, 10, 3, 1, 1, VAProfileVP9Profile2 },
{ FF_PROFILE_UNKNOWN }
};
static const VAAPIEncodeType vaapi_encode_type_vp9 = {
.profiles = vaapi_encode_vp9_profiles,
.configure = &vaapi_encode_vp9_configure,
.sequence_params_size = sizeof(VAEncSequenceParameterBufferVP9),
......@@ -219,31 +227,6 @@ static av_cold int vaapi_encode_vp9_init(AVCodecContext *avctx)
ctx->codec = &vaapi_encode_type_vp9;
switch (avctx->profile) {
case FF_PROFILE_VP9_0:
case FF_PROFILE_UNKNOWN:
ctx->va_profile = VAProfileVP9Profile0;
ctx->va_rt_format = VA_RT_FORMAT_YUV420;
break;
case FF_PROFILE_VP9_1:
av_log(avctx, AV_LOG_ERROR, "VP9 profile 1 is not "
"supported.\n");
return AVERROR_PATCHWELCOME;
case FF_PROFILE_VP9_2:
ctx->va_profile = VAProfileVP9Profile2;
ctx->va_rt_format = VA_RT_FORMAT_YUV420_10BPP;
break;
case FF_PROFILE_VP9_3:
av_log(avctx, AV_LOG_ERROR, "VP9 profile 3 is not "
"supported.\n");
return AVERROR_PATCHWELCOME;
default:
av_log(avctx, AV_LOG_ERROR, "Unknown VP9 profile %d.\n",
avctx->profile);
return AVERROR(EINVAL);
}
ctx->va_entrypoint = VAEntrypointEncSlice;
if (avctx->flags & AV_CODEC_FLAG_QSCALE) {
ctx->va_rc_mode = VA_RC_CQP;
} else if (avctx->bit_rate > 0) {
......@@ -276,7 +259,6 @@ static const AVOption vaapi_encode_vp9_options[] = {
};
static const AVCodecDefault vaapi_encode_vp9_defaults[] = {
{ "profile", "0" },
{ "b", "0" },
{ "bf", "0" },
{ "g", "250" },
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment