encoder.c 49 KB

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