-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathTweak.xm
More file actions
349 lines (281 loc) · 10.1 KB
/
Tweak.xm
File metadata and controls
349 lines (281 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#import <CoreMedia/CoreMedia.h>
#import <Foundation/NSProcessInfo.h>
#import <Foundation/NSString.h>
#import <HBLog.h>
#import <substrate.h>
#ifdef SIDELOAD
#import <libundirect/libundirect_dynamic.h>
#else
#import <libundirect/libundirect.h>
#endif
#import <sys/sysctl.h>
#import <version.h>
#import "Header.h"
typedef struct {
const unsigned int *data;
uint64_t length;
} Span;
extern "C" {
BOOL UseVP9();
BOOL AllVP9();
BOOL DisableServerABR();
int DecodeThreads();
BOOL SkipLoopFilter();
BOOL LoopFilterOptimization();
BOOL RowThreading();
}
// Remove any <= 1080p VP9 formats if AllVP9 is disabled
NSArray <MLFormat *> *filteredFormats(NSArray <MLFormat *> *formats) {
if (AllVP9()) return formats;
NSPredicate *predicate = [NSPredicate predicateWithBlock:^BOOL(MLFormat *format, NSDictionary *bindings) {
if (![format isKindOfClass:%c(MLFormat)]) return YES;
NSString *qualityLabel = [format qualityLabel];
BOOL isHighRes = [qualityLabel hasPrefix:@"2160p"] || [qualityLabel hasPrefix:@"1440p"];
BOOL isVP9orAV1 = [[format MIMEType] videoCodec] == 'vp09' || [[format MIMEType] videoCodec] == 'av01';
return (isHighRes && isVP9orAV1) || !isVP9orAV1;
}];
return [formats filteredArrayUsingPredicate:predicate];
}
static void hookFormatsBase(YTIHamplayerConfig *config) {
if ([config.videoAbrConfig respondsToSelector:@selector(setPreferSoftwareHdrOverHardwareSdr:)])
config.videoAbrConfig.preferSoftwareHdrOverHardwareSdr = YES;
if ([config respondsToSelector:@selector(setDisableResolveOverlappingQualitiesByCodec:)])
config.disableResolveOverlappingQualitiesByCodec = NO;
YTIHamplayerStreamFilter *filter = config.streamFilter;
filter.enableVideoCodecSplicing = YES;
filter.av1.maxArea = MAX_PIXELS;
filter.av1.maxFps = MAX_FPS;
filter.vp9.maxArea = MAX_PIXELS;
filter.vp9.maxFps = MAX_FPS;
}
static void hookFormats(MLABRPolicy *self) {
hookFormatsBase([self valueForKey:@"_hamplayerConfig"]);
}
%hook MLABRPolicy
- (void)setFormats:(NSArray *)formats {
hookFormats(self);
%orig(filteredFormats(formats));
}
%end
%hook MLABRPolicyOld
- (void)setFormats:(NSArray *)formats {
hookFormats(self);
%orig(filteredFormats(formats));
}
%end
%hook MLABRPolicyNew
- (void)setFormats:(NSArray *)formats {
hookFormats(self);
%orig(filteredFormats(formats));
}
%end
%hook MLHAMPlayerItem
- (void)load {
hookFormatsBase([self valueForKey:@"_hamplayerConfig"]);
%orig;
}
- (void)loadWithInitialSeekRequired:(BOOL)initialSeekRequired initialSeekTime:(double)initialSeekTime {
hookFormatsBase([self valueForKey:@"_hamplayerConfig"]);
%orig;
}
%end
%hook YTIHamplayerHotConfig
%new(i@:)
- (int)libvpxDecodeThreads {
return DecodeThreads();
}
%new(B@:)
- (BOOL)libvpxRowThreading {
return RowThreading();
}
%new(B@:)
- (BOOL)libvpxSkipLoopFilter {
return SkipLoopFilter();
}
%new(B@:)
- (BOOL)libvpxLoopFilterOptimization {
return LoopFilterOptimization();
}
%end
%hook YTColdConfig
- (BOOL)iosPlayerClientSharedConfigPopulateSwAv1MediaCapabilities {
return YES;
}
- (BOOL)iosPlayerClientSharedConfigDisableLibvpxDecoder {
return NO;
}
%end
%hook YTHotConfig
- (BOOL)iosPlayerClientSharedConfigDisableServerDrivenAbr {
return DisableServerABR() ? YES : %orig;
}
- (BOOL)iosPlayerClientSharedConfigPostponeCabrPreferredFormatFiltering {
return YES;
}
- (BOOL)iosPlayerClientSharedConfigHamplayerPrepareVideoDecoderForAvsbdl {
return YES;
}
- (BOOL)iosPlayerClientSharedConfigHamplayerAlwaysEnqueueDecodedSampleBuffersToAvsbdl {
return YES;
}
%end
%hook HAMDefaultABRPolicy
- (id)getSelectableFormatDataAndReturnError:(NSError **)error {
[self setValue:@(NO) forKey:@"_postponePreferredFormatFiltering"];
// @try {
// HAMDefaultABRPolicyConfig config = MSHookIvar<HAMDefaultABRPolicyConfig>(self, "_config");
// config.softwareAV1Filter.maxArea = MAX_PIXELS;
// config.softwareAV1Filter.maxFPS = MAX_FPS;
// config.softwareVP9Filter.maxArea = MAX_PIXELS;
// config.softwareVP9Filter.maxFPS = MAX_FPS;
// MSHookIvar<HAMDefaultABRPolicyConfig>(self, "_config") = config;
// } @catch (id ex) {}
return filteredFormats(%orig);
}
- (void)setFormats:(NSArray *)formats {
[self setValue:@(YES) forKey:@"_postponePreferredFormatFiltering"];
// @try {
// HAMDefaultABRPolicyConfig config = MSHookIvar<HAMDefaultABRPolicyConfig>(self, "_config");
// config.softwareAV1Filter.maxArea = MAX_PIXELS;
// config.softwareAV1Filter.maxFPS = MAX_FPS;
// config.softwareVP9Filter.maxArea = MAX_PIXELS;
// config.softwareVP9Filter.maxFPS = MAX_FPS;
// MSHookIvar<HAMDefaultABRPolicyConfig>(self, "_config") = config;
// } @catch (id ex) {}
%orig(filteredFormats(formats));
}
%end
%hook MLHLSStreamSelector
- (void)didLoadHLSMasterPlaylist:(id)arg1 {
%orig;
MLHLSMasterPlaylist *playlist = [self valueForKey:@"_completeMasterPlaylist"];
NSArray *remotePlaylists = [playlist remotePlaylists];
[[self delegate] streamSelectorHasSelectableVideoFormats:remotePlaylists];
}
%end
BOOL overrideSupportsCodec = NO;
%hook MLVideoDecoderFactory
- (id)videoDecoderWithDelegate:(id)delegate delegateQueue:(id)delegateQueue formatDescription:(id)formatDescription pixelBufferAttributes:(id)pixelBufferAttributes preferredOutputFormats:(Span)preferredOutputFormats error:(NSError **)error {
HBLogDebug(@"YTUHD - MLVideoDecoderFactory videoDecoderWithDelegate called");
overrideSupportsCodec = YES;
id decoder = %orig;
overrideSupportsCodec = NO;
return decoder;
}
- (id)videoDecoderWithDelegate:(id)delegate delegateQueue:(id)delegateQueue formatDescription:(id)formatDescription pixelBufferAttributes:(id)pixelBufferAttributes setPixelBufferTypeOnlyIfEmpty:(BOOL)setPixelBufferTypeOnlyIfEmpty error:(NSError **)error {
HBLogDebug(@"YTUHD - MLVideoDecoderFactory videoDecoderWithDelegate called");
overrideSupportsCodec = YES;
id decoder = %orig;
overrideSupportsCodec = NO;
return decoder;
}
- (id)videoDecoderWithDelegate:(id)delegate delegateQueue:(id)delegateQueue formatDescription:(id)formatDescription pixelBufferAttributes:(id)pixelBufferAttributes error:(NSError **)error {
HBLogDebug(@"YTUHD - MLVideoDecoderFactory videoDecoderWithDelegate called");
overrideSupportsCodec = YES;
id decoder = %orig;
overrideSupportsCodec = NO;
return decoder;
}
%end
%hook HAMDefaultVideoDecoderFactory
- (id)videoDecoderWithDelegate:(id)delegate delegateQueue:(id)delegateQueue formatDescription:(id)formatDescription pixelBufferAttributes:(id)pixelBufferAttributes preferredOutputFormats:(Span)preferredOutputFormats error:(NSError **)error {
HBLogDebug(@"YTUHD - HAMDefaultVideoDecoderFactory videoDecoderWithDelegate called");
overrideSupportsCodec = YES;
id decoder = %orig;
overrideSupportsCodec = NO;
return decoder;
}
- (id)videoDecoderWithDelegate:(id)delegate delegateQueue:(id)delegateQueue formatDescription:(id)formatDescription pixelBufferAttributes:(id)pixelBufferAttributes setPixelBufferTypeOnlyIfEmpty:(BOOL)setPixelBufferTypeOnlyIfEmpty error:(NSError **)error {
HBLogDebug(@"YTUHD - HAMDefaultVideoDecoderFactory videoDecoderWithDelegate called");
overrideSupportsCodec = YES;
id decoder = %orig;
overrideSupportsCodec = NO;
return decoder;
}
- (id)videoDecoderWithDelegate:(id)delegate delegateQueue:(id)delegateQueue formatDescription:(id)formatDescription pixelBufferAttributes:(id)pixelBufferAttributes error:(NSError **)error {
HBLogDebug(@"YTUHD - HAMDefaultVideoDecoderFactory videoDecoderWithDelegate called");
overrideSupportsCodec = YES;
id decoder = %orig;
overrideSupportsCodec = NO;
return decoder;
}
%end
%hook YTIIosOnesieHotConfig
%new(B@:)
- (BOOL)prepareVideoDecoder { return YES; }
%end
%group Codec
BOOL (*SupportsCodec)(CMVideoCodecType codec) = NULL;
%hookf(BOOL, SupportsCodec, CMVideoCodecType codec) {
if (overrideSupportsCodec && (codec == kCMVideoCodecType_VP9 || codec == kCMVideoCodecType_AV1)) {
HBLogDebug(@"YTUHD - SupportsCodec called for codec: %d, returning NO", codec);
return NO;
}
return YES;
}
%end
%group Spoofing
%hook UIDevice
- (NSString *)systemVersion {
return @"15.8.6";
}
%end
%hook NSProcessInfo
- (NSOperatingSystemVersion)operatingSystemVersion {
NSOperatingSystemVersion version;
version.majorVersion = 15;
version.minorVersion = 8;
version.patchVersion = 6;
return version;
}
%end
%hookf(int, sysctlbyname, const char *name, void *oldp, size_t *oldlenp, void *newp, size_t newlen) {
if (strcmp(name, "kern.osversion") == 0) {
int ret = %orig;
if (oldp) {
strcpy((char *)oldp, IOS_BUILD);
*oldlenp = strlen(IOS_BUILD);
}
return ret;
}
return %orig;
}
%end
%ctor {
[[NSUserDefaults standardUserDefaults] registerDefaults:@{
DecodeThreadsKey: @2
}];
if (!UseVP9()) return;
%init;
uint8_t pattern1[] = {
0x28, 0x66, 0x8c, 0x52,
0xc8, 0x2e, 0xac, 0x72,
0x1f, 0x00, 0x08, 0x6b,
0x61, 0x00, 0x00, 0x54
};
uint8_t pattern2[] = {
0xf4, 0x4f, 0xbe, 0xa9,
0xfd, 0x7b, 0x01, 0xa9,
0xfd, 0x43, 0x00, 0x91,
0x28, 0x66, 0x8c, 0x52,
0xc8, 0x2e, 0xac, 0x72
};
NSString *bundlePath = [NSString stringWithFormat:@"%@/Frameworks/Module_Framework.framework", NSBundle.mainBundle.bundlePath];
NSBundle *bundle = [NSBundle bundleWithPath:bundlePath];
NSString *binary;
if (bundle) {
[bundle load];
binary = @"Module_Framework";
} else
binary = @"YouTube";
SupportsCodec = (BOOL (*)(CMVideoCodecType))libundirect_find(binary, pattern1, sizeof(pattern1), 0x28);
if (SupportsCodec == NULL)
SupportsCodec = (BOOL (*)(CMVideoCodecType))libundirect_find(binary, pattern2, sizeof(pattern2), 0xf4);
HBLogDebug(@"YTUHD: SupportsCodec: %d", SupportsCodec != NULL);
if (SupportsCodec) {
%init(Codec);
}
if (!IS_IOS_OR_NEWER(iOS_15_0)) {
%init(Spoofing);
}
}