encoder.c 49 KB

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