Skip to content

Commit 76c1b42

Browse files
committed
mac-videotoolbox: Add Spatial AQ option (macOS 15)
1 parent 449755e commit 76c1b42

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

plugins/mac-videotoolbox/data/locale/en-US.ini

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ MaxBitrateWindow="Maximum bitrate window"
1313
KeyframeIntervalSec="Keyframe Interval (0=auto)"
1414
Profile="Profile"
1515
UseBFrames="Use B-Frames"
16+
SpatialAQ="Spatial AQ"
17+
SpatialAQ.Auto="Automatic"
18+
SpatialAQ.Disabled="Disabled"
19+
SpatialAQ.Enabled="Enabled"
1620
RateControl="Rate Control"
1721
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."
1822
FullRangeUnsupported="Full range color is not supported by 16-bit Apple VT encoders. Select limited range color in Settings -> Advanced."

plugins/mac-videotoolbox/encoder.c

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@
1818
codec_type_to_print_fmt(codec_type), ##__VA_ARGS__)
1919
#define VT_BLOG(level, format, ...) VT_LOG_ENCODER(enc->encoder, enc->codec_type, level, format, ##__VA_ARGS__)
2020

21+
enum aq_mode {
22+
AQ_INVALID = 0,
23+
AQ_AUTO,
24+
AQ_DISABLED,
25+
AQ_ENABLED,
26+
};
27+
2128
struct vt_encoder_type_data {
2229
const char *disp_name;
2330
const char *id;
@@ -56,6 +63,7 @@ struct vt_encoder {
5663
const char *profile;
5764
CMVideoCodecType codec_type;
5865
bool bframes;
66+
bool spatial_aq;
5967

6068
int vt_pix_fmt;
6169
enum video_colorspace colorspace;
@@ -567,6 +575,20 @@ static OSStatus create_encoder(struct vt_encoder *enc)
567575
if (code != noErr) {
568576
return code;
569577
}
578+
579+
if (__builtin_available(macOS 15.0, *)) {
580+
int spatial_aq = enc->spatial_aq ? kVTQPModulationLevel_Default : kVTQPModulationLevel_Disable;
581+
CFNumberRef spatialAQ = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &spatial_aq);
582+
583+
code = VTSessionSetProperty(s, kVTCompressionPropertyKey_SpatialAdaptiveQPLevel, spatialAQ);
584+
585+
if (code != noErr) {
586+
log_osstatus(LOG_WARNING, enc,
587+
"setting kVTCompressionPropertyKey_SpatialAdaptiveQPLevel failed", code);
588+
}
589+
590+
CFRelease(spatialAQ);
591+
}
570592
}
571593

572594
// This can fail depending on hardware configuration
@@ -624,11 +646,12 @@ static void dump_encoder_info(struct vt_encoder *enc)
624646
"\trc_max_bitrate: %d (kbps)\n"
625647
"\trc_max_bitrate_window: %f (s)\n"
626648
"\thw_enc: %s\n"
649+
"\tspatial_aq: %s\n"
627650
"\tprofile: %s\n"
628651
"\tcodec_type: %.4s\n",
629652
enc->vt_encoder_id, enc->rate_control, enc->bitrate, enc->quality, enc->fps_num, enc->fps_den,
630653
enc->width, enc->height, enc->keyint, enc->limit_bitrate ? "on" : "off", enc->rc_max_bitrate,
631-
enc->rc_max_bitrate_window, enc->hw_enc ? "on" : "off",
654+
enc->rc_max_bitrate_window, enc->hw_enc ? "on" : "off", enc->spatial_aq ? "on" : "off",
632655
(enc->profile != NULL && !!strlen(enc->profile)) ? enc->profile : "default",
633656
codec_type_to_print_fmt(enc->codec_type));
634657
}
@@ -724,6 +747,14 @@ static bool update_params(struct vt_encoder *enc, obs_data_t *settings)
724747
enc->rc_max_bitrate_window = obs_data_get_double(settings, "max_bitrate_window");
725748
enc->bframes = obs_data_get_bool(settings, "bframes");
726749

750+
enum aq_mode spatial_aq_mode = obs_data_get_int(settings, "spatial_aq_mode");
751+
if (spatial_aq_mode == AQ_AUTO) {
752+
/* Only enable by default in CRF mode. */
753+
enc->spatial_aq = strcmp(enc->rate_control, "CRF") == 0;
754+
} else {
755+
enc->spatial_aq = spatial_aq_mode == AQ_ENABLED;
756+
}
757+
727758
return true;
728759
}
729760

@@ -1261,6 +1292,14 @@ static obs_properties_t *vt_properties_h26x(void *data __unused, void *type_data
12611292

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

1295+
if (__builtin_available(macOS 15.0, *)) {
1296+
p = obs_properties_add_list(props, "spatial_aq_mode", obs_module_text("SpatialAQ"), OBS_COMBO_TYPE_LIST,
1297+
OBS_COMBO_FORMAT_INT);
1298+
obs_property_list_add_int(p, obs_module_text("SpatialAQ.Auto"), AQ_AUTO);
1299+
obs_property_list_add_int(p, obs_module_text("SpatialAQ.Disabled"), AQ_DISABLED);
1300+
obs_property_list_add_int(p, obs_module_text("SpatialAQ.Enabled"), AQ_ENABLED);
1301+
}
1302+
12641303
return props;
12651304
}
12661305

@@ -1346,6 +1385,7 @@ static void vt_defaults(obs_data_t *settings, void *data)
13461385
type_data->codec_type == kCMVideoCodecType_H264 ? "high" : "main");
13471386
obs_data_set_default_int(settings, "codec_type", kCMVideoCodecType_AppleProRes422);
13481387
obs_data_set_default_bool(settings, "bframes", true);
1388+
obs_data_set_default_int(settings, "spatial_aq_mode", AQ_AUTO);
13491389
}
13501390

13511391
static void vt_free_type_data(void *data)

0 commit comments

Comments
 (0)