encoder.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534
  1. #include <obs-module.h>
  2. #include <util/darray.h>
  3. #include <util/platform.h>
  4. #include <obs-avc.h>
  5. #include <CoreFoundation/CoreFoundation.h>
  6. #include <VideoToolbox/VideoToolbox.h>
  7. #include <VideoToolbox/VTVideoEncoderList.h>
  8. #include <CoreMedia/CoreMedia.h>
  9. #include <util/apple/cfstring-utils.h>
  10. #include <assert.h>
  11. #define VT_LOG(level, format, ...) blog(level, "[VideoToolbox encoder]: " format, ##__VA_ARGS__)
  12. #define VT_LOG_ENCODER(encoder, codec_type, level, format, ...) \
  13. blog(level, "[VideoToolbox %s: '%s']: " format, obs_encoder_get_name(encoder), \
  14. codec_type_to_print_fmt(codec_type), ##__VA_ARGS__)
  15. #define VT_BLOG(level, format, ...) VT_LOG_ENCODER(enc->encoder, enc->codec_type, level, format, ##__VA_ARGS__)
  16. enum aq_mode {
  17. AQ_INVALID = 0,
  18. AQ_AUTO,
  19. AQ_DISABLED,
  20. AQ_ENABLED,
  21. };
  22. struct vt_encoder_type_data {
  23. const char *disp_name;
  24. const char *id;
  25. CMVideoCodecType codec_type;
  26. bool hardware_accelerated;
  27. };
  28. struct vt_prores_encoder_data {
  29. FourCharCode codec_type;
  30. CFStringRef encoder_id;
  31. };
  32. static DARRAY(struct vt_prores_encoder_data) vt_prores_hardware_encoder_list;
  33. static DARRAY(struct vt_prores_encoder_data) vt_prores_software_encoder_list;
  34. #ifdef __aarch64__
  35. bool is_apple_silicon = true;
  36. #else
  37. bool is_apple_silicon = false;
  38. #endif
  39. struct vt_encoder {
  40. obs_encoder_t *encoder;
  41. const char *vt_encoder_id;
  42. uint32_t width;
  43. uint32_t height;
  44. uint32_t keyint;
  45. uint32_t fps_num;
  46. uint32_t fps_den;
  47. const char *rate_control;
  48. uint32_t bitrate;
  49. float quality;
  50. bool limit_bitrate;
  51. uint32_t rc_max_bitrate;
  52. double rc_max_bitrate_window;
  53. const char *profile;
  54. CMVideoCodecType codec_type;
  55. bool bframes;
  56. bool spatial_aq;
  57. int vt_pix_fmt;
  58. enum video_colorspace colorspace;
  59. VTCompressionSessionRef session;
  60. CMSimpleQueueRef queue;
  61. bool hw_enc;
  62. DARRAY(uint8_t) packet_data;
  63. DARRAY(uint8_t) extra_data;
  64. };
  65. static const char *codec_type_to_print_fmt(CMVideoCodecType codec_type)
  66. {
  67. switch (codec_type) {
  68. case kCMVideoCodecType_H264:
  69. return "h264";
  70. case kCMVideoCodecType_HEVC:
  71. return "hevc";
  72. case kCMVideoCodecType_AppleProRes4444XQ:
  73. return "ap4x";
  74. case kCMVideoCodecType_AppleProRes4444:
  75. return "ap4h";
  76. case kCMVideoCodecType_AppleProRes422Proxy:
  77. return "apco";
  78. case kCMVideoCodecType_AppleProRes422LT:
  79. return "apcs";
  80. case kCMVideoCodecType_AppleProRes422:
  81. return "apcn";
  82. case kCMVideoCodecType_AppleProRes422HQ:
  83. return "apch";
  84. default:
  85. return "";
  86. }
  87. }
  88. static void log_osstatus(int log_level, struct vt_encoder *enc, const char *context, OSStatus code)
  89. {
  90. char *c_str = NULL;
  91. CFErrorRef err = CFErrorCreate(kCFAllocatorDefault, kCFErrorDomainOSStatus, code, NULL);
  92. CFStringRef str = CFErrorCopyDescription(err);
  93. c_str = cfstr_copy_cstr(str, kCFStringEncodingUTF8);
  94. if (c_str) {
  95. if (enc)
  96. VT_BLOG(log_level, "Error in %s: %s", context, c_str);
  97. else
  98. VT_LOG(log_level, "Error in %s: %s", context, c_str);
  99. }
  100. bfree(c_str);
  101. CFRelease(str);
  102. CFRelease(err);
  103. }
  104. static CFStringRef obs_to_vt_profile(CMVideoCodecType codec_type, const char *profile, enum video_format format)
  105. {
  106. if (codec_type == kCMVideoCodecType_H264) {
  107. if (strcmp(profile, "baseline") == 0)
  108. return kVTProfileLevel_H264_Baseline_AutoLevel;
  109. else if (strcmp(profile, "main") == 0)
  110. return kVTProfileLevel_H264_Main_AutoLevel;
  111. else if (strcmp(profile, "high") == 0)
  112. return kVTProfileLevel_H264_High_AutoLevel;
  113. else
  114. return kVTProfileLevel_H264_Main_AutoLevel;
  115. #ifdef ENABLE_HEVC
  116. } else if (codec_type == kCMVideoCodecType_HEVC) {
  117. if (strcmp(profile, "main") == 0) {
  118. if (format == VIDEO_FORMAT_P010) {
  119. VT_LOG(LOG_WARNING, "Forcing main10 for P010");
  120. return kVTProfileLevel_HEVC_Main10_AutoLevel;
  121. } else {
  122. return kVTProfileLevel_HEVC_Main_AutoLevel;
  123. }
  124. }
  125. if (strcmp(profile, "main10") == 0)
  126. return kVTProfileLevel_HEVC_Main10_AutoLevel;
  127. if (__builtin_available(macOS 12.3, *)) {
  128. if (strcmp(profile, "main42210") == 0)
  129. return kVTProfileLevel_HEVC_Main42210_AutoLevel;
  130. }
  131. return kVTProfileLevel_HEVC_Main_AutoLevel;
  132. #else
  133. (void)format;
  134. #endif // ENABLE_HEVC
  135. } else {
  136. return kVTProfileLevel_H264_Baseline_AutoLevel;
  137. }
  138. }
  139. static CFStringRef obs_to_vt_colorspace(enum video_colorspace cs)
  140. {
  141. switch (cs) {
  142. case VIDEO_CS_601:
  143. return kCVImageBufferYCbCrMatrix_ITU_R_601_4;
  144. case VIDEO_CS_2100_PQ:
  145. case VIDEO_CS_2100_HLG:
  146. return kCVImageBufferYCbCrMatrix_ITU_R_2020;
  147. default:
  148. return kCVImageBufferYCbCrMatrix_ITU_R_709_2;
  149. }
  150. }
  151. static CFStringRef obs_to_vt_primaries(enum video_colorspace cs)
  152. {
  153. switch (cs) {
  154. case VIDEO_CS_601:
  155. return kCVImageBufferColorPrimaries_SMPTE_C;
  156. case VIDEO_CS_2100_PQ:
  157. case VIDEO_CS_2100_HLG:
  158. return kCVImageBufferColorPrimaries_ITU_R_2020;
  159. default:
  160. return kCVImageBufferColorPrimaries_ITU_R_709_2;
  161. }
  162. }
  163. static CFStringRef obs_to_vt_transfer(enum video_colorspace cs)
  164. {
  165. switch (cs) {
  166. case VIDEO_CS_SRGB:
  167. return kCVImageBufferTransferFunction_sRGB;
  168. case VIDEO_CS_2100_PQ:
  169. return kCVImageBufferTransferFunction_SMPTE_ST_2084_PQ;
  170. case VIDEO_CS_2100_HLG:
  171. return kCVImageBufferTransferFunction_ITU_R_2100_HLG;
  172. default:
  173. return kCVImageBufferTransferFunction_ITU_R_709_2;
  174. }
  175. }
  176. /* Adapted from Chromium GenerateMasteringDisplayColorVolume */
  177. static CFDataRef obs_to_vt_masteringdisplay(uint32_t hdr_nominal_peak_level)
  178. {
  179. struct mastering_display_colour_volume {
  180. uint16_t display_primaries[3][2];
  181. uint16_t white_point[2];
  182. uint32_t max_display_mastering_luminance;
  183. uint32_t min_display_mastering_luminance;
  184. };
  185. static_assert(sizeof(struct mastering_display_colour_volume) == 24, "May need to adjust struct packing");
  186. struct mastering_display_colour_volume mdcv;
  187. mdcv.display_primaries[0][0] = __builtin_bswap16(13250);
  188. mdcv.display_primaries[0][1] = __builtin_bswap16(34500);
  189. mdcv.display_primaries[1][0] = __builtin_bswap16(7500);
  190. mdcv.display_primaries[1][1] = __builtin_bswap16(3000);
  191. mdcv.display_primaries[2][0] = __builtin_bswap16(34000);
  192. mdcv.display_primaries[2][1] = __builtin_bswap16(16000);
  193. mdcv.white_point[0] = __builtin_bswap16(15635);
  194. mdcv.white_point[1] = __builtin_bswap16(16450);
  195. mdcv.max_display_mastering_luminance = __builtin_bswap32(hdr_nominal_peak_level * 10000);
  196. mdcv.min_display_mastering_luminance = 0;
  197. UInt8 bytes[sizeof(struct mastering_display_colour_volume)];
  198. memcpy(bytes, &mdcv, sizeof(bytes));
  199. return CFDataCreate(kCFAllocatorDefault, bytes, sizeof(bytes));
  200. }
  201. /* Adapted from Chromium GenerateContentLightLevelInfo */
  202. static CFDataRef obs_to_vt_contentlightlevelinfo(uint16_t hdr_nominal_peak_level)
  203. {
  204. struct content_light_level_info {
  205. uint16_t max_content_light_level;
  206. uint16_t max_pic_average_light_level;
  207. };
  208. static_assert(sizeof(struct content_light_level_info) == 4, "May need to adjust struct packing");
  209. struct content_light_level_info clli;
  210. clli.max_content_light_level = __builtin_bswap16(hdr_nominal_peak_level);
  211. clli.max_pic_average_light_level = __builtin_bswap16(hdr_nominal_peak_level);
  212. UInt8 bytes[sizeof(struct content_light_level_info)];
  213. memcpy(bytes, &clli, sizeof(bytes));
  214. return CFDataCreate(kCFAllocatorDefault, bytes, sizeof(bytes));
  215. }
  216. static OSStatus session_set_prop_float(VTCompressionSessionRef session, CFStringRef key, float val)
  217. {
  218. CFNumberRef n = CFNumberCreate(NULL, kCFNumberFloat32Type, &val);
  219. OSStatus code = VTSessionSetProperty(session, key, n);
  220. CFRelease(n);
  221. return code;
  222. }
  223. static OSStatus session_set_prop_int(VTCompressionSessionRef session, CFStringRef key, int32_t val)
  224. {
  225. CFNumberRef n = CFNumberCreate(NULL, kCFNumberSInt32Type, &val);
  226. OSStatus code = VTSessionSetProperty(session, key, n);
  227. CFRelease(n);
  228. return code;
  229. }
  230. static OSStatus session_set_prop_str(VTCompressionSessionRef session, CFStringRef key, char *val)
  231. {
  232. CFStringRef s = CFStringCreateWithFileSystemRepresentation(NULL, val);
  233. OSStatus code = VTSessionSetProperty(session, key, s);
  234. CFRelease(s);
  235. return code;
  236. }
  237. static OSStatus session_set_prop(VTCompressionSessionRef session, CFStringRef key, CFTypeRef val)
  238. {
  239. return VTSessionSetProperty(session, key, val);
  240. }
  241. static OSStatus session_set_bitrate(VTCompressionSessionRef session, const char *rate_control, int new_bitrate,
  242. float quality, bool limit_bitrate, int max_bitrate, double max_bitrate_window)
  243. {
  244. OSStatus code;
  245. bool can_limit_bitrate;
  246. CFStringRef compressionPropertyKey;
  247. if (strcmp(rate_control, "CBR") == 0) {
  248. compressionPropertyKey = kVTCompressionPropertyKey_AverageBitRate;
  249. can_limit_bitrate = true;
  250. if (__builtin_available(macOS 13.0, *)) {
  251. if (is_apple_silicon) {
  252. compressionPropertyKey = kVTCompressionPropertyKey_ConstantBitRate;
  253. can_limit_bitrate = false;
  254. } else {
  255. VT_LOG(LOG_WARNING, "CBR support for VideoToolbox encoder requires Apple Silicon. "
  256. "Will use ABR instead.");
  257. }
  258. } else {
  259. VT_LOG(LOG_WARNING, "CBR support for VideoToolbox encoder requires macOS 13 or newer. "
  260. "Will use ABR instead.");
  261. }
  262. } else if (strcmp(rate_control, "ABR") == 0) {
  263. compressionPropertyKey = kVTCompressionPropertyKey_AverageBitRate;
  264. can_limit_bitrate = true;
  265. } else if (strcmp(rate_control, "CRF") == 0) {
  266. if (is_apple_silicon) {
  267. compressionPropertyKey = kVTCompressionPropertyKey_Quality;
  268. code = session_set_prop_float(session, compressionPropertyKey, quality);
  269. if (code != noErr) {
  270. return code;
  271. }
  272. } else {
  273. VT_LOG(LOG_WARNING, "CRF support for VideoToolbox encoder requires Apple Silicon. "
  274. "Will use ABR instead.");
  275. compressionPropertyKey = kVTCompressionPropertyKey_AverageBitRate;
  276. }
  277. can_limit_bitrate = true;
  278. } else {
  279. VT_LOG(LOG_ERROR, "Selected rate control method is not supported: %s", rate_control);
  280. return kVTParameterErr;
  281. }
  282. if (compressionPropertyKey != kVTCompressionPropertyKey_Quality) {
  283. code = session_set_prop_int(session, compressionPropertyKey, new_bitrate * 1000);
  284. if (code != noErr) {
  285. return code;
  286. }
  287. }
  288. if (limit_bitrate && can_limit_bitrate) {
  289. double cpb_size = max_bitrate * 125 * max_bitrate_window;
  290. CFNumberRef cf_cpb_size = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &cpb_size);
  291. CFNumberRef cf_cpb_window_size =
  292. CFNumberCreate(kCFAllocatorDefault, kCFNumberFloatType, &max_bitrate_window);
  293. CFTypeRef values[2] = {cf_cpb_size, cf_cpb_window_size};
  294. CFArrayRef rate_control_data = CFArrayCreate(kCFAllocatorDefault, values, 2, &kCFTypeArrayCallBacks);
  295. code = session_set_prop(session, kVTCompressionPropertyKey_DataRateLimits, rate_control_data);
  296. CFRelease(cf_cpb_size);
  297. CFRelease(cf_cpb_window_size);
  298. CFRelease(rate_control_data);
  299. if (code == kVTPropertyNotSupportedErr) {
  300. log_osstatus(LOG_WARNING, NULL, "setting DataRateLimits on session", code);
  301. return noErr;
  302. }
  303. }
  304. return noErr;
  305. }
  306. static OSStatus session_set_colorspace(VTCompressionSessionRef session, enum video_colorspace cs)
  307. {
  308. OSStatus code;
  309. CFTypeRef keys[5] = {kVTCompressionPropertyKey_ColorPrimaries, kVTCompressionPropertyKey_TransferFunction,
  310. kVTCompressionPropertyKey_YCbCrMatrix, NULL, NULL};
  311. CFTypeRef values[5] = {obs_to_vt_primaries(cs), obs_to_vt_transfer(cs), obs_to_vt_colorspace(cs), NULL, NULL};
  312. CFDataRef masteringDisplayColorVolume = NULL;
  313. CFDataRef contentLightLevel = NULL;
  314. if (cs == VIDEO_CS_2100_PQ) {
  315. const uint16_t hdr_nominal_peak_level = (uint16_t)obs_get_video_hdr_nominal_peak_level();
  316. masteringDisplayColorVolume = obs_to_vt_masteringdisplay(hdr_nominal_peak_level);
  317. contentLightLevel = obs_to_vt_contentlightlevelinfo(hdr_nominal_peak_level);
  318. keys[3] = kVTCompressionPropertyKey_MasteringDisplayColorVolume;
  319. keys[4] = kVTCompressionPropertyKey_ContentLightLevelInfo;
  320. values[3] = masteringDisplayColorVolume;
  321. values[4] = contentLightLevel;
  322. } else if (cs == VIDEO_CS_2100_HLG) {
  323. masteringDisplayColorVolume = obs_to_vt_masteringdisplay(1000);
  324. contentLightLevel = obs_to_vt_contentlightlevelinfo(1000);
  325. keys[3] = kVTCompressionPropertyKey_MasteringDisplayColorVolume;
  326. keys[4] = kVTCompressionPropertyKey_ContentLightLevelInfo;
  327. values[3] = masteringDisplayColorVolume;
  328. values[4] = contentLightLevel;
  329. }
  330. CFDictionaryRef session_properties = CFDictionaryCreate(
  331. kCFAllocatorDefault, keys, values, 5, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
  332. code = VTSessionSetProperties(session, session_properties);
  333. CFRelease(session_properties);
  334. if (masteringDisplayColorVolume != NULL) {
  335. CFRelease(masteringDisplayColorVolume);
  336. }
  337. if (contentLightLevel != NULL) {
  338. CFRelease(contentLightLevel);
  339. }
  340. return code;
  341. }
  342. void sample_encoded_callback(void *data, void *source, OSStatus status, VTEncodeInfoFlags info_flags,
  343. CMSampleBufferRef buffer)
  344. {
  345. if (status != noErr) {
  346. log_osstatus(LOG_ERROR, NULL, "encoder callback", status);
  347. return;
  348. }
  349. if (info_flags == kVTEncodeInfo_FrameDropped) {
  350. VT_LOG(LOG_INFO, "Frame dropped by encoder");
  351. }
  352. CMSimpleQueueRef queue = data;
  353. CVPixelBufferRef pixbuf = source;
  354. if (buffer != NULL) {
  355. CFRetain(buffer);
  356. CMSimpleQueueEnqueue(queue, buffer);
  357. }
  358. CFRelease(pixbuf);
  359. }
  360. static inline CFDictionaryRef create_encoder_spec(const char *vt_encoder_id)
  361. {
  362. CFStringRef id = CFStringCreateWithFileSystemRepresentation(NULL, vt_encoder_id);
  363. CFTypeRef keys[1] = {kVTVideoEncoderSpecification_EncoderID};
  364. CFTypeRef values[1] = {id};
  365. CFDictionaryRef encoder_spec = CFDictionaryCreate(
  366. kCFAllocatorDefault, keys, values, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
  367. CFRelease(id);
  368. return encoder_spec;
  369. }
  370. static inline CFDictionaryRef create_prores_encoder_spec(CMVideoCodecType target_codec_type, bool hardware_accelerated)
  371. {
  372. CFStringRef encoder_id = NULL;
  373. size_t size = 0;
  374. struct vt_prores_encoder_data *encoder_list = NULL;
  375. if (hardware_accelerated) {
  376. size = vt_prores_hardware_encoder_list.num;
  377. encoder_list = vt_prores_hardware_encoder_list.array;
  378. } else {
  379. size = vt_prores_software_encoder_list.num;
  380. encoder_list = vt_prores_software_encoder_list.array;
  381. }
  382. for (size_t i = 0; i < size; ++i) {
  383. if (target_codec_type == encoder_list[i].codec_type) {
  384. encoder_id = encoder_list[i].encoder_id;
  385. }
  386. }
  387. CFTypeRef keys[1] = {kVTVideoEncoderSpecification_EncoderID};
  388. CFTypeRef values[1] = {encoder_id};
  389. CFDictionaryRef encoder_spec = CFDictionaryCreate(
  390. kCFAllocatorDefault, keys, values, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
  391. return encoder_spec;
  392. }
  393. static inline CFDictionaryRef create_pixbuf_spec(struct vt_encoder *enc)
  394. {
  395. CFNumberRef PixelFormat = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &enc->vt_pix_fmt);
  396. CFNumberRef Width = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &enc->width);
  397. CFNumberRef Height = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &enc->height);
  398. CFTypeRef keys[3] = {kCVPixelBufferPixelFormatTypeKey, kCVPixelBufferWidthKey, kCVPixelBufferHeightKey};
  399. CFTypeRef values[3] = {PixelFormat, Width, Height};
  400. CFDictionaryRef pixbuf_spec = CFDictionaryCreate(
  401. kCFAllocatorDefault, keys, values, 3, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
  402. CFRelease(PixelFormat);
  403. CFRelease(Width);
  404. CFRelease(Height);
  405. return pixbuf_spec;
  406. }
  407. static OSStatus create_encoder(struct vt_encoder *enc)
  408. {
  409. OSStatus code;
  410. VTCompressionSessionRef s;
  411. const char *codec_name = obs_encoder_get_codec(enc->encoder);
  412. CFDictionaryRef encoder_spec;
  413. if (strcmp(codec_name, "prores") == 0) {
  414. struct vt_encoder_type_data *type_data =
  415. (struct vt_encoder_type_data *)obs_encoder_get_type_data(enc->encoder);
  416. encoder_spec = create_prores_encoder_spec(enc->codec_type, type_data->hardware_accelerated);
  417. } else {
  418. encoder_spec = create_encoder_spec(enc->vt_encoder_id);
  419. }
  420. CFDictionaryRef pixbuf_spec = create_pixbuf_spec(enc);
  421. code = VTCompressionSessionCreate(kCFAllocatorDefault, enc->width, enc->height, enc->codec_type, encoder_spec,
  422. pixbuf_spec, NULL, &sample_encoded_callback, enc->queue, &s);
  423. if (code != noErr) {
  424. log_osstatus(LOG_ERROR, enc, "VTCompressionSessionCreate", code);
  425. }
  426. CFRelease(encoder_spec);
  427. CFRelease(pixbuf_spec);
  428. CFBooleanRef b = NULL;
  429. code = VTSessionCopyProperty(s, kVTCompressionPropertyKey_UsingHardwareAcceleratedVideoEncoder, NULL, &b);
  430. if (code == noErr && (enc->hw_enc = CFBooleanGetValue(b)))
  431. VT_BLOG(LOG_INFO, "session created with hardware encoding");
  432. else
  433. enc->hw_enc = false;
  434. if (b != NULL)
  435. CFRelease(b);
  436. if (enc->codec_type == kCMVideoCodecType_H264 || enc->codec_type == kCMVideoCodecType_HEVC) {
  437. // This can fail when using GPU hardware encoding
  438. code = session_set_prop_int(s, kVTCompressionPropertyKey_MaxKeyFrameIntervalDuration, enc->keyint);
  439. if (code != noErr)
  440. log_osstatus(LOG_WARNING, enc,
  441. "setting kVTCompressionPropertyKey_MaxKeyFrameIntervalDuration failed, "
  442. "keyframe interval might be incorrect",
  443. code);
  444. CFTypeRef session_keys[4] = {kVTCompressionPropertyKey_MaxKeyFrameInterval,
  445. kVTCompressionPropertyKey_ExpectedFrameRate,
  446. kVTCompressionPropertyKey_AllowFrameReordering,
  447. kVTCompressionPropertyKey_ProfileLevel};
  448. SInt32 key_frame_interval = (SInt32)(enc->keyint * ((float)enc->fps_num / enc->fps_den));
  449. float expected_framerate = (float)enc->fps_num / enc->fps_den;
  450. CFNumberRef MaxKeyFrameInterval =
  451. CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &key_frame_interval);
  452. CFNumberRef ExpectedFrameRate =
  453. CFNumberCreate(kCFAllocatorDefault, kCFNumberFloat32Type, &expected_framerate);
  454. CFTypeRef AllowFrameReordering = enc->bframes ? kCFBooleanTrue : kCFBooleanFalse;
  455. video_t *video = obs_encoder_video(enc->encoder);
  456. const struct video_output_info *voi = video_output_get_info(video);
  457. CFTypeRef ProfileLevel = obs_to_vt_profile(enc->codec_type, enc->profile, voi->format);
  458. CFTypeRef session_values[4] = {MaxKeyFrameInterval, ExpectedFrameRate, AllowFrameReordering,
  459. ProfileLevel};
  460. CFDictionaryRef session_properties =
  461. CFDictionaryCreate(kCFAllocatorDefault, session_keys, session_values, 4,
  462. &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
  463. code = VTSessionSetProperties(s, session_properties);
  464. CFRelease(MaxKeyFrameInterval);
  465. CFRelease(ExpectedFrameRate);
  466. CFRelease(AllowFrameReordering);
  467. CFRelease(ProfileLevel);
  468. CFRelease(session_properties);
  469. if (code != noErr) {
  470. return code;
  471. }
  472. code = session_set_bitrate(s, enc->rate_control, enc->bitrate, enc->quality, enc->limit_bitrate,
  473. enc->rc_max_bitrate, enc->rc_max_bitrate_window);
  474. if (code != noErr) {
  475. return code;
  476. }
  477. if (__builtin_available(macOS 15.0, *)) {
  478. int spatial_aq = enc->spatial_aq ? kVTQPModulationLevel_Default : kVTQPModulationLevel_Disable;
  479. CFNumberRef spatialAQ = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &spatial_aq);
  480. code = VTSessionSetProperty(s, kVTCompressionPropertyKey_SpatialAdaptiveQPLevel, spatialAQ);
  481. if (code != noErr) {
  482. log_osstatus(LOG_WARNING, enc,
  483. "setting kVTCompressionPropertyKey_SpatialAdaptiveQPLevel failed", code);
  484. }
  485. CFRelease(spatialAQ);
  486. }
  487. }
  488. // This can fail depending on hardware configuration
  489. code = session_set_prop(s, kVTCompressionPropertyKey_RealTime, kCFBooleanFalse);
  490. if (code != noErr)
  491. log_osstatus(LOG_WARNING, enc,
  492. "setting kVTCompressionPropertyKey_RealTime failed, "
  493. "frame delay might be increased",
  494. code);
  495. code = session_set_colorspace(s, enc->colorspace);
  496. if (code != noErr) {
  497. return code;
  498. }
  499. code = VTCompressionSessionPrepareToEncodeFrames(s);
  500. if (code != noErr) {
  501. return code;
  502. }
  503. enc->session = s;
  504. return noErr;
  505. }
  506. static void vt_destroy(void *data)
  507. {
  508. struct vt_encoder *enc = data;
  509. if (enc) {
  510. if (enc->session != NULL) {
  511. VTCompressionSessionInvalidate(enc->session);
  512. CFRelease(enc->session);
  513. }
  514. da_free(enc->packet_data);
  515. da_free(enc->extra_data);
  516. bfree(enc);
  517. }
  518. }
  519. static void dump_encoder_info(struct vt_encoder *enc)
  520. {
  521. VT_BLOG(LOG_INFO,
  522. "settings:\n"
  523. "\tvt_encoder_id %s\n"
  524. "\trate_control: %s\n"
  525. "\tbitrate: %d (kbps)\n"
  526. "\tquality: %f\n"
  527. "\tfps_num: %d\n"
  528. "\tfps_den: %d\n"
  529. "\twidth: %d\n"
  530. "\theight: %d\n"
  531. "\tkeyint: %d (s)\n"
  532. "\tlimit_bitrate: %s\n"
  533. "\trc_max_bitrate: %d (kbps)\n"
  534. "\trc_max_bitrate_window: %f (s)\n"
  535. "\thw_enc: %s\n"
  536. "\tspatial_aq: %s\n"
  537. "\tprofile: %s\n"
  538. "\tcodec_type: %.4s\n",
  539. enc->vt_encoder_id, enc->rate_control, enc->bitrate, enc->quality, enc->fps_num, enc->fps_den,
  540. enc->width, enc->height, enc->keyint, enc->limit_bitrate ? "on" : "off", enc->rc_max_bitrate,
  541. enc->rc_max_bitrate_window, enc->hw_enc ? "on" : "off", enc->spatial_aq ? "on" : "off",
  542. (enc->profile != NULL && !!strlen(enc->profile)) ? enc->profile : "default",
  543. codec_type_to_print_fmt(enc->codec_type));
  544. }
  545. typedef enum {
  546. kResultSuccess = 0,
  547. kResultColorFormatUnsupported = 1,
  548. kResultFullRangeUnsupported = 2,
  549. } SetVideoFormatResult;
  550. static SetVideoFormatResult set_video_format(struct vt_encoder *enc, enum video_format format,
  551. enum video_range_type range)
  552. {
  553. bool full_range = range == VIDEO_RANGE_FULL;
  554. switch (format) {
  555. case VIDEO_FORMAT_I420:
  556. enc->vt_pix_fmt = full_range ? kCVPixelFormatType_420YpCbCr8PlanarFullRange
  557. : kCVPixelFormatType_420YpCbCr8Planar;
  558. return kResultSuccess;
  559. case VIDEO_FORMAT_NV12:
  560. enc->vt_pix_fmt = full_range ? kCVPixelFormatType_420YpCbCr8BiPlanarFullRange
  561. : kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange;
  562. return kResultSuccess;
  563. case VIDEO_FORMAT_P010:
  564. if (enc->codec_type == kCMVideoCodecType_HEVC) {
  565. enc->vt_pix_fmt = full_range ? kCVPixelFormatType_420YpCbCr10BiPlanarFullRange
  566. : kCVPixelFormatType_420YpCbCr10BiPlanarVideoRange;
  567. return kResultSuccess;
  568. }
  569. break;
  570. case VIDEO_FORMAT_P216:
  571. if (!full_range) {
  572. enc->vt_pix_fmt = kCVPixelFormatType_422YpCbCr16BiPlanarVideoRange;
  573. return kResultSuccess;
  574. } else {
  575. return kResultFullRangeUnsupported;
  576. }
  577. break;
  578. case VIDEO_FORMAT_P416:
  579. if (!full_range) {
  580. enc->vt_pix_fmt = kCVPixelFormatType_444YpCbCr16BiPlanarVideoRange;
  581. return kResultSuccess;
  582. } else {
  583. return kResultFullRangeUnsupported;
  584. }
  585. break;
  586. default:
  587. return kResultColorFormatUnsupported;
  588. }
  589. return kResultColorFormatUnsupported;
  590. }
  591. static bool update_params(struct vt_encoder *enc, obs_data_t *settings)
  592. {
  593. video_t *video = obs_encoder_video(enc->encoder);
  594. const struct video_output_info *voi = video_output_get_info(video);
  595. const char *codec = obs_encoder_get_codec(enc->encoder);
  596. if (strcmp(codec, "h264") == 0) {
  597. enc->codec_type = kCMVideoCodecType_H264;
  598. obs_data_set_int(settings, "codec_type", enc->codec_type);
  599. #ifdef ENABLE_HEVC
  600. } else if (strcmp(codec, "hevc") == 0) {
  601. enc->codec_type = kCMVideoCodecType_HEVC;
  602. obs_data_set_int(settings, "codec_type", enc->codec_type);
  603. #endif
  604. } else {
  605. enc->codec_type = (CMVideoCodecType)obs_data_get_int(settings, "codec_type");
  606. }
  607. SetVideoFormatResult res = set_video_format(enc, voi->format, voi->range);
  608. if (res == kResultColorFormatUnsupported) {
  609. obs_encoder_set_last_error(enc->encoder, obs_module_text("ColorFormatUnsupported"));
  610. VT_BLOG(LOG_WARNING, "Unsupported color format selected");
  611. return false;
  612. } else if (res == kResultFullRangeUnsupported) {
  613. obs_encoder_set_last_error(enc->encoder, obs_module_text("FullRangeUnsupported"));
  614. VT_BLOG(LOG_WARNING, "Unsupported color range (full) selected");
  615. return false;
  616. }
  617. enc->colorspace = voi->colorspace;
  618. enc->width = obs_encoder_get_width(enc->encoder);
  619. enc->height = obs_encoder_get_height(enc->encoder);
  620. enc->fps_num = voi->fps_num;
  621. enc->fps_den = voi->fps_den;
  622. enc->keyint = (uint32_t)obs_data_get_int(settings, "keyint_sec");
  623. enc->rate_control = obs_data_get_string(settings, "rate_control");
  624. enc->bitrate = (uint32_t)obs_data_get_int(settings, "bitrate");
  625. enc->quality = ((float)obs_data_get_int(settings, "quality")) / 100;
  626. enc->profile = obs_data_get_string(settings, "profile");
  627. enc->limit_bitrate = obs_data_get_bool(settings, "limit_bitrate");
  628. enc->rc_max_bitrate = (uint32_t)obs_data_get_int(settings, "max_bitrate");
  629. enc->rc_max_bitrate_window = obs_data_get_double(settings, "max_bitrate_window");
  630. enc->bframes = obs_data_get_bool(settings, "bframes");
  631. enum aq_mode spatial_aq_mode = obs_data_get_int(settings, "spatial_aq_mode");
  632. if (spatial_aq_mode == AQ_AUTO) {
  633. /* Only enable by default in CRF mode. */
  634. enc->spatial_aq = strcmp(enc->rate_control, "CRF") == 0;
  635. } else {
  636. enc->spatial_aq = spatial_aq_mode == AQ_ENABLED;
  637. }
  638. return true;
  639. }
  640. static bool vt_update(void *data, obs_data_t *settings)
  641. {
  642. struct vt_encoder *enc = data;
  643. uint32_t old_bitrate = enc->bitrate;
  644. bool old_limit_bitrate = enc->limit_bitrate;
  645. update_params(enc, settings);
  646. if (old_bitrate == enc->bitrate && old_limit_bitrate == enc->limit_bitrate)
  647. return true;
  648. OSStatus code = session_set_bitrate(enc->session, enc->rate_control, enc->bitrate, enc->quality,
  649. enc->limit_bitrate, enc->rc_max_bitrate, enc->rc_max_bitrate_window);
  650. if (code != noErr)
  651. VT_BLOG(LOG_WARNING, "Failed to set bitrate to session");
  652. dump_encoder_info(enc);
  653. return true;
  654. }
  655. static void *vt_create(obs_data_t *settings, obs_encoder_t *encoder)
  656. {
  657. struct vt_encoder *enc = bzalloc(sizeof(struct vt_encoder));
  658. OSStatus code;
  659. enc->encoder = encoder;
  660. enc->vt_encoder_id = obs_encoder_get_id(encoder);
  661. if (!update_params(enc, settings))
  662. goto fail;
  663. code = CMSimpleQueueCreate(NULL, 100, &enc->queue);
  664. if (code != noErr) {
  665. goto fail;
  666. }
  667. code = create_encoder(enc);
  668. if (code != noErr) {
  669. goto fail;
  670. }
  671. dump_encoder_info(enc);
  672. return enc;
  673. fail:
  674. vt_destroy(enc);
  675. return NULL;
  676. }
  677. static const uint8_t annexb_startcode[4] = {0, 0, 0, 1};
  678. static void packet_put(struct darray *packet, const uint8_t *buf, size_t size)
  679. {
  680. darray_push_back_array(sizeof(uint8_t), packet, buf, size);
  681. }
  682. static void packet_put_startcode(struct darray *packet, int size)
  683. {
  684. assert(size == 3 || size == 4);
  685. packet_put(packet, &annexb_startcode[4 - size], size);
  686. }
  687. static bool handle_prores_packet(struct vt_encoder *enc, CMSampleBufferRef buffer)
  688. {
  689. OSStatus err = 0;
  690. size_t block_size = 0;
  691. uint8_t *block_buf = NULL;
  692. CMBlockBufferRef block = CMSampleBufferGetDataBuffer(buffer);
  693. if (block == NULL) {
  694. VT_BLOG(LOG_ERROR, "Failed to get block buffer for ProRes frame.");
  695. return false;
  696. }
  697. err = CMBlockBufferGetDataPointer(block, 0, NULL, &block_size, (char **)&block_buf);
  698. if (err != 0) {
  699. VT_BLOG(LOG_ERROR, "Failed to get data buffer pointer for ProRes frame.");
  700. return false;
  701. }
  702. packet_put(&enc->packet_data.da, block_buf, block_size);
  703. return true;
  704. }
  705. static void convert_block_nals_to_annexb(struct vt_encoder *enc, struct darray *packet, CMBlockBufferRef block,
  706. int nal_length_bytes)
  707. {
  708. size_t block_size;
  709. uint8_t *block_buf;
  710. CMBlockBufferGetDataPointer(block, 0, NULL, &block_size, (char **)&block_buf);
  711. size_t bytes_remaining = block_size;
  712. while (bytes_remaining > 0) {
  713. uint32_t nal_size;
  714. if (nal_length_bytes == 1)
  715. nal_size = block_buf[0];
  716. else if (nal_length_bytes == 2)
  717. nal_size = CFSwapInt16BigToHost(((uint16_t *)block_buf)[0]);
  718. else if (nal_length_bytes == 4)
  719. nal_size = CFSwapInt32BigToHost(((uint32_t *)block_buf)[0]);
  720. else
  721. return;
  722. bytes_remaining -= nal_length_bytes;
  723. block_buf += nal_length_bytes;
  724. if (bytes_remaining < nal_size) {
  725. VT_BLOG(LOG_ERROR, "invalid nal block");
  726. return;
  727. }
  728. packet_put_startcode(packet, 3);
  729. packet_put(packet, block_buf, nal_size);
  730. bytes_remaining -= nal_size;
  731. block_buf += nal_size;
  732. }
  733. }
  734. static bool handle_keyframe(struct vt_encoder *enc, CMFormatDescriptionRef format_desc, size_t param_count,
  735. struct darray *packet, struct darray *extra_data)
  736. {
  737. OSStatus code;
  738. const uint8_t *param;
  739. size_t param_size;
  740. for (size_t i = 0; i < param_count; i++) {
  741. if (enc->codec_type == kCMVideoCodecType_H264) {
  742. code = CMVideoFormatDescriptionGetH264ParameterSetAtIndex(format_desc, i, &param, &param_size,
  743. NULL, NULL);
  744. #ifdef ENABLE_HEVC
  745. } else if (enc->codec_type == kCMVideoCodecType_HEVC) {
  746. code = CMVideoFormatDescriptionGetHEVCParameterSetAtIndex(format_desc, i, &param, &param_size,
  747. NULL, NULL);
  748. #endif
  749. }
  750. if (code != noErr) {
  751. log_osstatus(LOG_ERROR, enc,
  752. "getting NAL parameter "
  753. "at index",
  754. code);
  755. return false;
  756. }
  757. packet_put_startcode(packet, 4);
  758. packet_put(packet, param, param_size);
  759. }
  760. // if we were passed an extra_data array, fill it with
  761. // SPS, PPS, etc.
  762. if (extra_data != NULL)
  763. packet_put(extra_data, packet->array, packet->num);
  764. return true;
  765. }
  766. static bool convert_sample_to_annexb(struct vt_encoder *enc, struct darray *packet, struct darray *extra_data,
  767. CMSampleBufferRef buffer, bool keyframe)
  768. {
  769. OSStatus code;
  770. CMFormatDescriptionRef format_desc = CMSampleBufferGetFormatDescription(buffer);
  771. size_t param_count;
  772. int nal_length_bytes;
  773. if (enc->codec_type == kCMVideoCodecType_H264) {
  774. code = CMVideoFormatDescriptionGetH264ParameterSetAtIndex(format_desc, 0, NULL, NULL, &param_count,
  775. &nal_length_bytes);
  776. #ifdef ENABLE_HEVC
  777. } else if (enc->codec_type == kCMVideoCodecType_HEVC) {
  778. code = CMVideoFormatDescriptionGetHEVCParameterSetAtIndex(format_desc, 0, NULL, NULL, &param_count,
  779. &nal_length_bytes);
  780. #endif
  781. } else {
  782. log_osstatus(LOG_ERROR, enc, "invalid codec type", kCMFormatDescriptionError_ValueNotAvailable);
  783. return false;
  784. }
  785. // it is not clear what errors this function can return
  786. // so we check the two most reasonable
  787. if (code == kCMFormatDescriptionBridgeError_InvalidParameter ||
  788. code == kCMFormatDescriptionError_InvalidParameter) {
  789. VT_BLOG(LOG_WARNING, "assuming 2 parameter sets "
  790. "and 4 byte NAL length header");
  791. param_count = 2;
  792. nal_length_bytes = 4;
  793. } else if (code != noErr) {
  794. log_osstatus(LOG_ERROR, enc, "getting parameter count from sample", code);
  795. return false;
  796. }
  797. if (keyframe && !handle_keyframe(enc, format_desc, param_count, packet, extra_data))
  798. return false;
  799. CMBlockBufferRef block = CMSampleBufferGetDataBuffer(buffer);
  800. convert_block_nals_to_annexb(enc, packet, block, nal_length_bytes);
  801. return true;
  802. }
  803. static bool is_sample_keyframe(CMSampleBufferRef buffer)
  804. {
  805. CFArrayRef attachments = CMSampleBufferGetSampleAttachmentsArray(buffer, false);
  806. if (attachments != NULL) {
  807. CFDictionaryRef attachment;
  808. CFBooleanRef has_dependencies;
  809. attachment = (CFDictionaryRef)CFArrayGetValueAtIndex(attachments, 0);
  810. has_dependencies =
  811. (CFBooleanRef)CFDictionaryGetValue(attachment, kCMSampleAttachmentKey_DependsOnOthers);
  812. return has_dependencies == kCFBooleanFalse;
  813. }
  814. return false;
  815. }
  816. static bool parse_sample(struct vt_encoder *enc, CMSampleBufferRef buffer, struct encoder_packet *packet, CMTime off)
  817. {
  818. CMTime pts = CMSampleBufferGetPresentationTimeStamp(buffer);
  819. CMTime dts = CMSampleBufferGetDecodeTimeStamp(buffer);
  820. if (CMTIME_IS_INVALID(dts))
  821. dts = pts;
  822. // imitate x264's negative dts when bframes might have pts < dts
  823. else if (enc->bframes)
  824. dts = CMTimeSubtract(dts, off);
  825. pts = CMTimeMultiply(pts, enc->fps_num);
  826. dts = CMTimeMultiply(dts, enc->fps_num);
  827. const bool is_avc = enc->codec_type == kCMVideoCodecType_H264;
  828. const bool has_annexb = is_avc || (enc->codec_type == kCMVideoCodecType_HEVC);
  829. // All ProRes frames are "keyframes"
  830. const bool keyframe = !has_annexb || is_sample_keyframe(buffer);
  831. da_resize(enc->packet_data, 0);
  832. // If we are still looking for extra data
  833. struct darray *extra_data = NULL;
  834. if (enc->extra_data.num == 0)
  835. extra_data = &enc->extra_data.da;
  836. if (has_annexb) {
  837. if (!convert_sample_to_annexb(enc, &enc->packet_data.da, extra_data, buffer, keyframe))
  838. goto fail;
  839. } else {
  840. if (!handle_prores_packet(enc, buffer))
  841. goto fail;
  842. }
  843. packet->type = OBS_ENCODER_VIDEO;
  844. packet->pts = (int64_t)(CMTimeGetSeconds(pts));
  845. packet->dts = (int64_t)(CMTimeGetSeconds(dts));
  846. packet->data = enc->packet_data.array;
  847. packet->size = enc->packet_data.num;
  848. packet->keyframe = keyframe;
  849. if (is_avc) {
  850. // VideoToolbox produces packets with priority lower than the RTMP code
  851. // expects, which causes it to be unable to recover from frame drops.
  852. // Fix this by manually adjusting the priority.
  853. uint8_t *start = enc->packet_data.array;
  854. uint8_t *end = start + enc->packet_data.num;
  855. start = (uint8_t *)obs_avc_find_startcode(start, end);
  856. while (true) {
  857. while (start < end && !*(start++))
  858. ;
  859. if (start == end)
  860. break;
  861. const int type = start[0] & 0x1F;
  862. if (type == OBS_NAL_SLICE_IDR || type == OBS_NAL_SLICE) {
  863. uint8_t prev_type = (start[0] >> 5) & 0x3;
  864. start[0] &= ~(3 << 5);
  865. if (type == OBS_NAL_SLICE_IDR)
  866. start[0] |= OBS_NAL_PRIORITY_HIGHEST << 5;
  867. else if (type == OBS_NAL_SLICE && prev_type != OBS_NAL_PRIORITY_DISPOSABLE)
  868. start[0] |= OBS_NAL_PRIORITY_HIGH << 5;
  869. else
  870. start[0] |= prev_type << 5;
  871. }
  872. start = (uint8_t *)obs_avc_find_startcode(start, end);
  873. }
  874. }
  875. CFRelease(buffer);
  876. return true;
  877. fail:
  878. CFRelease(buffer);
  879. return false;
  880. }
  881. bool get_cached_pixel_buffer(struct vt_encoder *enc, CVPixelBufferRef *buf)
  882. {
  883. OSStatus code;
  884. CVPixelBufferPoolRef pool = VTCompressionSessionGetPixelBufferPool(enc->session);
  885. if (!pool)
  886. return kCVReturnError;
  887. CVPixelBufferRef pixbuf;
  888. code = CVPixelBufferPoolCreatePixelBuffer(NULL, pool, &pixbuf);
  889. if (code != noErr) {
  890. goto fail;
  891. }
  892. // Why aren't these already set on the pixel buffer?
  893. // I would have expected pixel buffers from the session's
  894. // pool to have the correct color space stuff set
  895. const enum video_colorspace cs = enc->colorspace;
  896. CVBufferSetAttachment(pixbuf, kCVImageBufferYCbCrMatrixKey, obs_to_vt_colorspace(cs),
  897. kCVAttachmentMode_ShouldPropagate);
  898. CVBufferSetAttachment(pixbuf, kCVImageBufferColorPrimariesKey, obs_to_vt_primaries(cs),
  899. kCVAttachmentMode_ShouldPropagate);
  900. CVBufferSetAttachment(pixbuf, kCVImageBufferTransferFunctionKey, obs_to_vt_transfer(cs),
  901. kCVAttachmentMode_ShouldPropagate);
  902. const bool pq = cs == VIDEO_CS_2100_PQ;
  903. const bool hlg = cs == VIDEO_CS_2100_HLG;
  904. if (pq || hlg) {
  905. const uint16_t hdr_nominal_peak_level = pq ? (uint16_t)obs_get_video_hdr_nominal_peak_level()
  906. : (hlg ? 1000 : 0);
  907. CFDataRef masteringDisplayColorVolume = obs_to_vt_masteringdisplay(hdr_nominal_peak_level);
  908. CFDataRef contentLightLevel = obs_to_vt_contentlightlevelinfo(hdr_nominal_peak_level);
  909. CVBufferSetAttachment(pixbuf, kCVImageBufferMasteringDisplayColorVolumeKey, masteringDisplayColorVolume,
  910. kCVAttachmentMode_ShouldPropagate);
  911. CVBufferSetAttachment(pixbuf, kCVImageBufferContentLightLevelInfoKey, contentLightLevel,
  912. kCVAttachmentMode_ShouldPropagate);
  913. CFRelease(masteringDisplayColorVolume);
  914. CFRelease(contentLightLevel);
  915. }
  916. *buf = pixbuf;
  917. return true;
  918. fail:
  919. return false;
  920. }
  921. static bool vt_encode(void *data, struct encoder_frame *frame, struct encoder_packet *packet, bool *received_packet)
  922. {
  923. struct vt_encoder *enc = data;
  924. OSStatus code;
  925. CMTime dur = CMTimeMake(enc->fps_den, enc->fps_num);
  926. CMTime off = CMTimeMultiply(dur, 2);
  927. CMTime pts = CMTimeMake(frame->pts, enc->fps_num);
  928. CVPixelBufferRef pixbuf = NULL;
  929. if (!get_cached_pixel_buffer(enc, &pixbuf)) {
  930. VT_BLOG(LOG_ERROR, "Unable to create pixel buffer");
  931. goto fail;
  932. }
  933. code = CVPixelBufferLockBaseAddress(pixbuf, 0);
  934. if (code != noErr) {
  935. goto fail;
  936. }
  937. for (int i = 0; i < MAX_AV_PLANES; i++) {
  938. if (frame->data[i] == NULL)
  939. break;
  940. uint8_t *p = (uint8_t *)CVPixelBufferGetBaseAddressOfPlane(pixbuf, i);
  941. uint8_t *f = frame->data[i];
  942. size_t plane_linesize = CVPixelBufferGetBytesPerRowOfPlane(pixbuf, i);
  943. size_t plane_height = CVPixelBufferGetHeightOfPlane(pixbuf, i);
  944. for (size_t j = 0; j < plane_height; j++) {
  945. memcpy(p, f, frame->linesize[i]);
  946. p += plane_linesize;
  947. f += frame->linesize[i];
  948. }
  949. }
  950. code = CVPixelBufferUnlockBaseAddress(pixbuf, 0);
  951. if (code != noErr) {
  952. goto fail;
  953. }
  954. code = VTCompressionSessionEncodeFrame(enc->session, pixbuf, pts, dur, NULL, pixbuf, NULL);
  955. if (code != noErr) {
  956. goto fail;
  957. }
  958. CMSampleBufferRef buffer = (CMSampleBufferRef)CMSimpleQueueDequeue(enc->queue);
  959. // No samples waiting in the queue
  960. if (buffer == NULL)
  961. return true;
  962. *received_packet = true;
  963. return parse_sample(enc, buffer, packet, off);
  964. fail:
  965. return false;
  966. }
  967. static bool vt_extra_data(void *data, uint8_t **extra_data, size_t *size)
  968. {
  969. struct vt_encoder *enc = (struct vt_encoder *)data;
  970. *extra_data = enc->extra_data.array;
  971. *size = enc->extra_data.num;
  972. return true;
  973. }
  974. static const char *vt_getname(void *data)
  975. {
  976. struct vt_encoder_type_data *type_data = data;
  977. if (strcmp("Apple H.264 (HW)", type_data->disp_name) == 0) {
  978. return obs_module_text("VTH264EncHW");
  979. } else if (strcmp("Apple H.264 (SW)", type_data->disp_name) == 0) {
  980. return obs_module_text("VTH264EncSW");
  981. #ifdef ENABLE_HEVC
  982. } else if (strcmp("Apple HEVC (HW)", type_data->disp_name) == 0) {
  983. return obs_module_text("VTHEVCEncHW");
  984. } else if (strcmp("Apple HEVC (AVE)", type_data->disp_name) == 0) {
  985. return obs_module_text("VTHEVCEncT2");
  986. } else if (strcmp("Apple HEVC (SW)", type_data->disp_name) == 0) {
  987. return obs_module_text("VTHEVCEncSW");
  988. #endif
  989. } else if (strncmp("AppleProResHW", type_data->disp_name, 13) == 0) {
  990. return obs_module_text("VTProResEncHW");
  991. } else if (strncmp("Apple ProRes", type_data->disp_name, 12) == 0) {
  992. return obs_module_text("VTProResEncSW");
  993. }
  994. return type_data->disp_name;
  995. }
  996. static bool rate_control_limit_bitrate_modified(obs_properties_t *ppts, obs_property_t *p, obs_data_t *settings)
  997. {
  998. bool has_bitrate = true;
  999. bool can_limit_bitrate = true;
  1000. bool use_limit_bitrate = obs_data_get_bool(settings, "limit_bitrate");
  1001. const char *rate_control = obs_data_get_string(settings, "rate_control");
  1002. if (strcmp(rate_control, "CBR") == 0) {
  1003. can_limit_bitrate = false;
  1004. has_bitrate = true;
  1005. } else if (strcmp(rate_control, "CRF") == 0) {
  1006. can_limit_bitrate = true;
  1007. has_bitrate = false;
  1008. } else if (strcmp(rate_control, "ABR") == 0) {
  1009. can_limit_bitrate = true;
  1010. has_bitrate = true;
  1011. }
  1012. p = obs_properties_get(ppts, "limit_bitrate");
  1013. obs_property_set_visible(p, can_limit_bitrate);
  1014. p = obs_properties_get(ppts, "max_bitrate");
  1015. obs_property_set_visible(p, can_limit_bitrate && use_limit_bitrate);
  1016. p = obs_properties_get(ppts, "max_bitrate_window");
  1017. obs_property_set_visible(p, can_limit_bitrate && use_limit_bitrate);
  1018. p = obs_properties_get(ppts, "bitrate");
  1019. obs_property_set_visible(p, has_bitrate);
  1020. p = obs_properties_get(ppts, "quality");
  1021. obs_property_set_visible(p, !has_bitrate);
  1022. return true;
  1023. }
  1024. static obs_properties_t *vt_properties_h26x(void *data __unused, void *type_data)
  1025. {
  1026. struct vt_encoder_type_data *encoder_type_data = type_data;
  1027. obs_properties_t *props = obs_properties_create();
  1028. obs_property_t *p;
  1029. p = obs_properties_add_list(props, "rate_control", obs_module_text("RateControl"), OBS_COMBO_TYPE_LIST,
  1030. OBS_COMBO_FORMAT_STRING);
  1031. if (__builtin_available(macOS 13.0, *)) {
  1032. if (encoder_type_data->hardware_accelerated && is_apple_silicon) {
  1033. obs_property_list_add_string(p, "CBR", "CBR");
  1034. }
  1035. }
  1036. obs_property_list_add_string(p, "ABR", "ABR");
  1037. if (encoder_type_data->hardware_accelerated && is_apple_silicon) {
  1038. obs_property_list_add_string(p, "CRF", "CRF");
  1039. }
  1040. obs_property_set_modified_callback(p, rate_control_limit_bitrate_modified);
  1041. p = obs_properties_add_int(props, "bitrate", obs_module_text("Bitrate"), 50, 10000000, 50);
  1042. obs_property_int_set_suffix(p, " Kbps");
  1043. obs_properties_add_int_slider(props, "quality", obs_module_text("Quality"), 0, 100, 1);
  1044. p = obs_properties_add_bool(props, "limit_bitrate", obs_module_text("UseMaxBitrate"));
  1045. obs_property_set_modified_callback(p, rate_control_limit_bitrate_modified);
  1046. p = obs_properties_add_int(props, "max_bitrate", obs_module_text("MaxBitrate"), 50, 10000000, 50);
  1047. obs_property_int_set_suffix(p, " Kbps");
  1048. p = obs_properties_add_float(props, "max_bitrate_window", obs_module_text("MaxBitrateWindow"), 0.10f, 10.0f,
  1049. 0.25f);
  1050. obs_property_float_set_suffix(p, " s");
  1051. p = obs_properties_add_int(props, "keyint_sec", obs_module_text("KeyframeIntervalSec"), 0, 20, 1);
  1052. obs_property_int_set_suffix(p, " s");
  1053. p = obs_properties_add_list(props, "profile", obs_module_text("Profile"), OBS_COMBO_TYPE_LIST,
  1054. OBS_COMBO_FORMAT_STRING);
  1055. if (encoder_type_data->codec_type == kCMVideoCodecType_H264) {
  1056. obs_property_list_add_string(p, "baseline", "baseline");
  1057. obs_property_list_add_string(p, "main", "main");
  1058. obs_property_list_add_string(p, "high", "high");
  1059. #ifdef ENABLE_HEVC
  1060. } else if (encoder_type_data->codec_type == kCMVideoCodecType_HEVC) {
  1061. obs_property_list_add_string(p, "main", "main");
  1062. obs_property_list_add_string(p, "main10", "main10");
  1063. if (__builtin_available(macOS 12.3, *)) {
  1064. obs_property_list_add_string(p, "main 4:2:2 10", "main42210");
  1065. }
  1066. #endif
  1067. }
  1068. obs_properties_add_bool(props, "bframes", obs_module_text("UseBFrames"));
  1069. if (__builtin_available(macOS 15.0, *)) {
  1070. p = obs_properties_add_list(props, "spatial_aq_mode", obs_module_text("SpatialAQ"), OBS_COMBO_TYPE_LIST,
  1071. OBS_COMBO_FORMAT_INT);
  1072. obs_property_list_add_int(p, obs_module_text("SpatialAQ.Auto"), AQ_AUTO);
  1073. obs_property_list_add_int(p, obs_module_text("SpatialAQ.Disabled"), AQ_DISABLED);
  1074. obs_property_list_add_int(p, obs_module_text("SpatialAQ.Enabled"), AQ_ENABLED);
  1075. }
  1076. return props;
  1077. }
  1078. static obs_properties_t *vt_properties_prores(void *data __unused, void *type_data)
  1079. {
  1080. struct vt_encoder_type_data *encoder_type_data = type_data;
  1081. obs_properties_t *props = obs_properties_create();
  1082. obs_property_t *p;
  1083. p = obs_properties_add_list(props, "codec_type", obs_module_text("ProResCodec"), OBS_COMBO_TYPE_LIST,
  1084. OBS_COMBO_FORMAT_INT);
  1085. uint32_t codec_availability_flags = 0;
  1086. size_t size = 0;
  1087. struct vt_prores_encoder_data *encoder_list = NULL;
  1088. if (encoder_type_data->hardware_accelerated) {
  1089. size = vt_prores_hardware_encoder_list.num;
  1090. encoder_list = vt_prores_hardware_encoder_list.array;
  1091. } else {
  1092. size = vt_prores_software_encoder_list.num;
  1093. encoder_list = vt_prores_software_encoder_list.array;
  1094. }
  1095. for (size_t i = 0; i < size; ++i) {
  1096. switch (encoder_list[i].codec_type) {
  1097. case kCMVideoCodecType_AppleProRes4444XQ:
  1098. codec_availability_flags |= (1 << 0);
  1099. break;
  1100. case kCMVideoCodecType_AppleProRes4444:
  1101. codec_availability_flags |= (1 << 1);
  1102. break;
  1103. case kCMVideoCodecType_AppleProRes422Proxy:
  1104. codec_availability_flags |= (1 << 2);
  1105. break;
  1106. case kCMVideoCodecType_AppleProRes422LT:
  1107. codec_availability_flags |= (1 << 3);
  1108. break;
  1109. case kCMVideoCodecType_AppleProRes422:
  1110. codec_availability_flags |= (1 << 4);
  1111. break;
  1112. case kCMVideoCodecType_AppleProRes422HQ:
  1113. codec_availability_flags |= (1 << 5);
  1114. break;
  1115. }
  1116. }
  1117. if (codec_availability_flags & (1 << 0))
  1118. obs_property_list_add_int(p, obs_module_text("ProRes4444XQ"), kCMVideoCodecType_AppleProRes4444XQ);
  1119. if (codec_availability_flags & (1 << 1))
  1120. obs_property_list_add_int(p, obs_module_text("ProRes4444"), kCMVideoCodecType_AppleProRes4444);
  1121. if (codec_availability_flags & (1 << 2))
  1122. obs_property_list_add_int(p, obs_module_text("ProRes422Proxy"), kCMVideoCodecType_AppleProRes422Proxy);
  1123. if (codec_availability_flags & (1 << 3))
  1124. obs_property_list_add_int(p, obs_module_text("ProRes422LT"), kCMVideoCodecType_AppleProRes422LT);
  1125. if (codec_availability_flags & (1 << 4))
  1126. obs_property_list_add_int(p, obs_module_text("ProRes422"), kCMVideoCodecType_AppleProRes422);
  1127. if (codec_availability_flags & (1 << 5))
  1128. obs_property_list_add_int(p, obs_module_text("ProRes422HQ"), kCMVideoCodecType_AppleProRes422HQ);
  1129. return props;
  1130. }
  1131. static void vt_defaults(obs_data_t *settings, void *data)
  1132. {
  1133. struct vt_encoder_type_data *type_data = data;
  1134. obs_data_set_default_string(settings, "rate_control", "ABR");
  1135. if (__builtin_available(macOS 13.0, *)) {
  1136. if (type_data->hardware_accelerated && is_apple_silicon) {
  1137. obs_data_set_default_string(settings, "rate_control", "CBR");
  1138. }
  1139. }
  1140. obs_data_set_default_int(settings, "bitrate", 2500);
  1141. obs_data_set_default_int(settings, "quality", 60);
  1142. obs_data_set_default_bool(settings, "limit_bitrate", false);
  1143. obs_data_set_default_int(settings, "max_bitrate", 2500);
  1144. obs_data_set_default_double(settings, "max_bitrate_window", 1.5f);
  1145. obs_data_set_default_int(settings, "keyint_sec", 2);
  1146. obs_data_set_default_string(settings, "profile",
  1147. type_data->codec_type == kCMVideoCodecType_H264 ? "high" : "main");
  1148. obs_data_set_default_int(settings, "codec_type", kCMVideoCodecType_AppleProRes422);
  1149. obs_data_set_default_bool(settings, "bframes", true);
  1150. obs_data_set_default_int(settings, "spatial_aq_mode", AQ_AUTO);
  1151. }
  1152. static void vt_free_type_data(void *data)
  1153. {
  1154. struct vt_encoder_type_data *type_data = data;
  1155. bfree((char *)type_data->disp_name);
  1156. bfree((char *)type_data->id);
  1157. bfree(type_data);
  1158. }
  1159. static inline void vt_add_prores_encoder_data_to_list(CFDictionaryRef encoder_dict, FourCharCode codec_type)
  1160. {
  1161. struct vt_prores_encoder_data *encoder_data = NULL;
  1162. CFBooleanRef hardware_accelerated =
  1163. CFDictionaryGetValue(encoder_dict, kVTVideoEncoderList_IsHardwareAccelerated);
  1164. if (hardware_accelerated == kCFBooleanTrue)
  1165. encoder_data = da_push_back_new(vt_prores_hardware_encoder_list);
  1166. else
  1167. encoder_data = da_push_back_new(vt_prores_software_encoder_list);
  1168. encoder_data->encoder_id = CFDictionaryGetValue(encoder_dict, kVTVideoEncoderList_EncoderID);
  1169. encoder_data->codec_type = codec_type;
  1170. }
  1171. OBS_DECLARE_MODULE()
  1172. OBS_MODULE_USE_DEFAULT_LOCALE("mac-videotoolbox", "en-US")
  1173. dispatch_group_t encoder_list_dispatch_group;
  1174. CFArrayRef encoder_list;
  1175. bool obs_module_load(void)
  1176. {
  1177. dispatch_queue_t queue = dispatch_queue_create("Encoder list load queue", NULL);
  1178. encoder_list_dispatch_group = dispatch_group_create();
  1179. dispatch_group_async(encoder_list_dispatch_group, queue, ^{
  1180. VTCopyVideoEncoderList(NULL, &encoder_list);
  1181. });
  1182. // The group dispatch keeps a reference until it's finished
  1183. dispatch_release(queue);
  1184. #ifndef __aarch64__
  1185. is_apple_silicon = os_get_emulation_status();
  1186. #endif
  1187. return true;
  1188. }
  1189. void obs_module_post_load(void)
  1190. {
  1191. struct obs_encoder_info info = {
  1192. .type = OBS_ENCODER_VIDEO,
  1193. .get_name = vt_getname,
  1194. .create = vt_create,
  1195. .destroy = vt_destroy,
  1196. .encode = vt_encode,
  1197. .update = vt_update,
  1198. .get_defaults2 = vt_defaults,
  1199. .get_extra_data = vt_extra_data,
  1200. .free_type_data = vt_free_type_data,
  1201. .caps = OBS_ENCODER_CAP_DYN_BITRATE,
  1202. };
  1203. da_init(vt_prores_hardware_encoder_list);
  1204. da_init(vt_prores_software_encoder_list);
  1205. dispatch_group_wait(encoder_list_dispatch_group, DISPATCH_TIME_FOREVER);
  1206. dispatch_release(encoder_list_dispatch_group);
  1207. CFIndex size = CFArrayGetCount(encoder_list);
  1208. for (CFIndex i = 0; i < size; i++) {
  1209. CFDictionaryRef encoder_dict = CFArrayGetValueAtIndex(encoder_list, i);
  1210. CMVideoCodecType codec_type = 0;
  1211. {
  1212. CFNumberRef codec_type_num = CFDictionaryGetValue(encoder_dict, kVTVideoEncoderList_CodecType);
  1213. CFNumberGetValue(codec_type_num, kCFNumberSInt32Type, &codec_type);
  1214. }
  1215. switch (codec_type) {
  1216. case kCMVideoCodecType_H264:
  1217. info.get_properties2 = vt_properties_h26x;
  1218. info.codec = "h264";
  1219. break;
  1220. #ifdef ENABLE_HEVC
  1221. case kCMVideoCodecType_HEVC:
  1222. info.get_properties2 = vt_properties_h26x;
  1223. info.codec = "hevc";
  1224. break;
  1225. #endif
  1226. // 422 is used as a marker for all ProRes types,
  1227. // since the type is stored as a profile
  1228. case kCMVideoCodecType_AppleProRes422:
  1229. info.get_properties2 = vt_properties_prores;
  1230. info.codec = "prores";
  1231. vt_add_prores_encoder_data_to_list(encoder_dict, codec_type);
  1232. break;
  1233. case kCMVideoCodecType_AppleProRes4444XQ:
  1234. case kCMVideoCodecType_AppleProRes4444:
  1235. case kCMVideoCodecType_AppleProRes422Proxy:
  1236. case kCMVideoCodecType_AppleProRes422LT:
  1237. case kCMVideoCodecType_AppleProRes422HQ:
  1238. vt_add_prores_encoder_data_to_list(encoder_dict, codec_type);
  1239. continue;
  1240. default:
  1241. continue;
  1242. }
  1243. CFStringRef EncoderID = CFDictionaryGetValue(encoder_dict, kVTVideoEncoderList_EncoderID);
  1244. CFIndex id_len = CFStringGetMaximumSizeOfFileSystemRepresentation(EncoderID);
  1245. char *id = bzalloc(id_len + 1);
  1246. CFStringGetFileSystemRepresentation(EncoderID, id, id_len);
  1247. CFStringRef DisplayName = CFDictionaryGetValue(encoder_dict, kVTVideoEncoderList_DisplayName);
  1248. CFIndex disp_name_len = CFStringGetMaximumSizeOfFileSystemRepresentation(DisplayName);
  1249. char *disp_name = bzalloc(disp_name_len + 1);
  1250. CFStringGetFileSystemRepresentation(DisplayName, disp_name, disp_name_len);
  1251. CFBooleanRef hardware_ref =
  1252. CFDictionaryGetValue(encoder_dict, kVTVideoEncoderList_IsHardwareAccelerated);
  1253. bool hardware_accelerated = (hardware_ref) ? CFBooleanGetValue(hardware_ref) : false;
  1254. info.id = id;
  1255. struct vt_encoder_type_data *type_data = bzalloc(sizeof(struct vt_encoder_type_data));
  1256. type_data->disp_name = disp_name;
  1257. type_data->id = id;
  1258. type_data->codec_type = codec_type;
  1259. type_data->hardware_accelerated = hardware_accelerated;
  1260. info.type_data = type_data;
  1261. obs_register_encoder(&info);
  1262. }
  1263. CFRelease(encoder_list);
  1264. VT_LOG(LOG_INFO, "Added VideoToolbox encoders");
  1265. }
  1266. void obs_module_unload(void)
  1267. {
  1268. da_free(vt_prores_hardware_encoder_list);
  1269. da_free(vt_prores_software_encoder_list);
  1270. }