encoder.c 46 KB

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