encoder.c 48 KB

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