Skip to content

Commit

Permalink
mac-videotoolbox: Add Spatial AQ option (macOS 15)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsaedtler committed Dec 14, 2024
1 parent d2045e6 commit 08df8d1
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
4 changes: 4 additions & 0 deletions plugins/mac-videotoolbox/data/locale/en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ MaxBitrateWindow="Maximum bitrate window"
KeyframeIntervalSec="Keyframe Interval (0=auto)"
Profile="Profile"
UseBFrames="Use B-Frames"
SpatialAQ="Spatial AQ"
SpatialAQ.Auto="Automatic"
SpatialAQ.Disabled="Disabled"
SpatialAQ.Enabled="Enabled"
RateControl="Rate Control"
ColorFormatUnsupported="The selected color format is not supported by the selected Apple VT encoder. Select a compatible color format in Settings -> Advanced or use a different encoder."
FullRangeUnsupported="Full range color is not supported by 16-bit Apple VT encoders. Select limited range color in Settings -> Advanced."
Expand Down
41 changes: 40 additions & 1 deletion plugins/mac-videotoolbox/encoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
codec_type_to_print_fmt(codec_type), ##__VA_ARGS__)
#define VT_BLOG(level, format, ...) VT_LOG_ENCODER(enc->encoder, enc->codec_type, level, format, ##__VA_ARGS__)

enum aq_mode {
AQ_AUTO = -1,
AQ_DISABLED,
AQ_ENABLED,
};

struct vt_encoder_type_data {
const char *disp_name;
const char *id;
Expand Down Expand Up @@ -56,6 +62,7 @@ struct vt_encoder {
const char *profile;
CMVideoCodecType codec_type;
bool bframes;
bool spatial_aq;

int vt_pix_fmt;
enum video_colorspace colorspace;
Expand Down Expand Up @@ -567,6 +574,20 @@ static OSStatus create_encoder(struct vt_encoder *enc)
if (code != noErr) {
return code;
}

if (__builtin_available(macOS 15.0, *)) {
int spatialAq = enc->spatial_aq ? kVTQPModulationLevel_Default : kVTQPModulationLevel_Disable;
CFNumberRef SpatialAQ = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &spatialAq);

code = VTSessionSetProperty(s, kVTCompressionPropertyKey_SpatialAdaptiveQPLevel, SpatialAQ);

if (code != noErr) {
log_osstatus(LOG_WARNING, enc,
"setting kVTCompressionPropertyKey_SpatialAdaptiveQPLevel failed", code);
}

CFRelease(SpatialAQ);
}
}

// This can fail depending on hardware configuration
Expand Down Expand Up @@ -624,11 +645,12 @@ static void dump_encoder_info(struct vt_encoder *enc)
"\trc_max_bitrate: %d (kbps)\n"
"\trc_max_bitrate_window: %f (s)\n"
"\thw_enc: %s\n"
"\tspatial_aq: %s\n"
"\tprofile: %s\n"
"\tcodec_type: %.4s\n",
enc->vt_encoder_id, enc->rate_control, enc->bitrate, enc->quality, enc->fps_num, enc->fps_den,
enc->width, enc->height, enc->keyint, enc->limit_bitrate ? "on" : "off", enc->rc_max_bitrate,
enc->rc_max_bitrate_window, enc->hw_enc ? "on" : "off",
enc->rc_max_bitrate_window, enc->hw_enc ? "on" : "off", enc->spatial_aq ? "on" : "off",
(enc->profile != NULL && !!strlen(enc->profile)) ? enc->profile : "default",
codec_type_to_print_fmt(enc->codec_type));
}
Expand Down Expand Up @@ -724,6 +746,14 @@ static bool update_params(struct vt_encoder *enc, obs_data_t *settings)
enc->rc_max_bitrate_window = obs_data_get_double(settings, "max_bitrate_window");
enc->bframes = obs_data_get_bool(settings, "bframes");

enum aq_mode spatialAqMode = obs_data_get_int(settings, "spatial_aq_mode");
if (spatialAqMode == AQ_AUTO) {
/* Only enable by default in CRF mode. */
enc->spatial_aq = strcmp(enc->rate_control, "CRF") == 0;
} else {
enc->spatial_aq = spatialAqMode == AQ_ENABLED;
}

return true;
}

Expand Down Expand Up @@ -1261,6 +1291,14 @@ static obs_properties_t *vt_properties_h26x(void *data __unused, void *type_data

obs_properties_add_bool(props, "bframes", obs_module_text("UseBFrames"));

if (__builtin_available(macOS 15.0, *)) {
p = obs_properties_add_list(props, "spatial_aq_mode", obs_module_text("SpatialAQ"), OBS_COMBO_TYPE_LIST,
OBS_COMBO_FORMAT_INT);
obs_property_list_add_int(p, obs_module_text("SpatialAQ.Auto"), AQ_AUTO);
obs_property_list_add_int(p, obs_module_text("SpatialAQ.Disabled"), AQ_DISABLED);
obs_property_list_add_int(p, obs_module_text("SpatialAQ.Enabled"), AQ_ENABLED);
}

return props;
}

Expand Down Expand Up @@ -1346,6 +1384,7 @@ static void vt_defaults(obs_data_t *settings, void *data)
type_data->codec_type == kCMVideoCodecType_H264 ? "high" : "main");
obs_data_set_default_int(settings, "codec_type", kCMVideoCodecType_AppleProRes422);
obs_data_set_default_bool(settings, "bframes", true);
obs_data_set_default_int(settings, "spatial_aq_mode", AQ_AUTO);
}

static void vt_free_type_data(void *data)
Expand Down

0 comments on commit 08df8d1

Please sign in to comment.