encoder.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718
  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 bool 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. /* Apple's documentation states that a keyframe interval of 0 will result in
  501. * the encoder automatically picking times to insert them; However, Apple's
  502. * encoder, when in CRF mode, will never actually insert any keyframes past
  503. * the very first one, rendering the files near-unusable in editors or
  504. * video players. So to avoid that happening, enforce a reasonable default
  505. * of 10 seconds in CRF mode. */
  506. if (enc->keyint == 0 && strcmp(enc->rate_control, "CRF") == 0) {
  507. VT_BLOG(LOG_INFO,
  508. "Enforcing non-zero keyframe interval in CRF mode");
  509. enc->keyint = 10;
  510. }
  511. // This can fail when using GPU hardware encoding
  512. code = session_set_prop_int(
  513. s,
  514. kVTCompressionPropertyKey_MaxKeyFrameIntervalDuration,
  515. enc->keyint);
  516. if (code != noErr)
  517. log_osstatus(
  518. LOG_WARNING, enc,
  519. "setting kVTCompressionPropertyKey_MaxKeyFrameIntervalDuration failed, "
  520. "keyframe interval might be incorrect",
  521. code);
  522. CFTypeRef session_keys[4] = {
  523. kVTCompressionPropertyKey_MaxKeyFrameInterval,
  524. kVTCompressionPropertyKey_ExpectedFrameRate,
  525. kVTCompressionPropertyKey_AllowFrameReordering,
  526. kVTCompressionPropertyKey_ProfileLevel};
  527. SInt32 key_frame_interval =
  528. (SInt32)(enc->keyint *
  529. ((float)enc->fps_num / enc->fps_den));
  530. float expected_framerate = (float)enc->fps_num / enc->fps_den;
  531. CFNumberRef MaxKeyFrameInterval =
  532. CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type,
  533. &key_frame_interval);
  534. CFNumberRef ExpectedFrameRate = CFNumberCreate(
  535. kCFAllocatorDefault, kCFNumberFloat32Type,
  536. &expected_framerate);
  537. CFTypeRef AllowFrameReordering = enc->bframes ? kCFBooleanTrue
  538. : kCFBooleanFalse;
  539. video_t *video = obs_encoder_video(enc->encoder);
  540. const struct video_output_info *voi =
  541. video_output_get_info(video);
  542. CFTypeRef ProfileLevel = obs_to_vt_profile(
  543. enc->codec_type, enc->profile, voi->format);
  544. CFTypeRef session_values[4] = {MaxKeyFrameInterval,
  545. ExpectedFrameRate,
  546. AllowFrameReordering,
  547. ProfileLevel};
  548. CFDictionaryRef session_properties = CFDictionaryCreate(
  549. kCFAllocatorDefault, session_keys, session_values, 4,
  550. &kCFTypeDictionaryKeyCallBacks,
  551. &kCFTypeDictionaryValueCallBacks);
  552. code = VTSessionSetProperties(s, session_properties);
  553. CFRelease(MaxKeyFrameInterval);
  554. CFRelease(ExpectedFrameRate);
  555. CFRelease(AllowFrameReordering);
  556. CFRelease(ProfileLevel);
  557. CFRelease(session_properties);
  558. if (code != noErr) {
  559. return code;
  560. }
  561. code = session_set_bitrate(s, enc->rate_control, enc->bitrate,
  562. enc->quality, enc->limit_bitrate,
  563. enc->rc_max_bitrate,
  564. enc->rc_max_bitrate_window);
  565. if (code != noErr) {
  566. return code;
  567. }
  568. }
  569. // This can fail depending on hardware configuration
  570. code = session_set_prop(s, kVTCompressionPropertyKey_RealTime,
  571. kCFBooleanFalse);
  572. if (code != noErr)
  573. log_osstatus(
  574. LOG_WARNING, enc,
  575. "setting kVTCompressionPropertyKey_RealTime failed, "
  576. "frame delay might be increased",
  577. code);
  578. code = session_set_colorspace(s, enc->colorspace);
  579. if (code != noErr) {
  580. return code;
  581. }
  582. code = VTCompressionSessionPrepareToEncodeFrames(s);
  583. if (code != noErr) {
  584. return code;
  585. }
  586. enc->session = s;
  587. return true;
  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. typedef enum {
  631. kResultSuccess = 0,
  632. kResultColorFormatUnsupported = 1,
  633. kResultFullRangeUnsupported = 2,
  634. } SetVideoFormatResult;
  635. static SetVideoFormatResult set_video_format(struct vt_encoder *enc,
  636. enum video_format format,
  637. enum video_range_type range)
  638. {
  639. bool full_range = range == VIDEO_RANGE_FULL;
  640. switch (format) {
  641. case VIDEO_FORMAT_I420:
  642. enc->vt_pix_fmt =
  643. full_range
  644. ? kCVPixelFormatType_420YpCbCr8PlanarFullRange
  645. : kCVPixelFormatType_420YpCbCr8Planar;
  646. return kResultSuccess;
  647. case VIDEO_FORMAT_NV12:
  648. enc->vt_pix_fmt =
  649. full_range
  650. ? kCVPixelFormatType_420YpCbCr8BiPlanarFullRange
  651. : kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange;
  652. return kResultSuccess;
  653. case VIDEO_FORMAT_P010:
  654. if (enc->codec_type == kCMVideoCodecType_HEVC) {
  655. enc->vt_pix_fmt =
  656. full_range
  657. ? kCVPixelFormatType_420YpCbCr10BiPlanarFullRange
  658. : kCVPixelFormatType_420YpCbCr10BiPlanarVideoRange;
  659. return kResultSuccess;
  660. }
  661. break;
  662. case VIDEO_FORMAT_P216:
  663. if (!full_range) {
  664. enc->vt_pix_fmt =
  665. kCVPixelFormatType_422YpCbCr16BiPlanarVideoRange;
  666. return kResultSuccess;
  667. } else {
  668. return kResultFullRangeUnsupported;
  669. }
  670. break;
  671. case VIDEO_FORMAT_P416:
  672. if (!full_range) {
  673. enc->vt_pix_fmt =
  674. kCVPixelFormatType_444YpCbCr16BiPlanarVideoRange;
  675. return kResultSuccess;
  676. } else {
  677. return kResultFullRangeUnsupported;
  678. }
  679. break;
  680. default:
  681. return kResultColorFormatUnsupported;
  682. }
  683. return kResultColorFormatUnsupported;
  684. }
  685. static bool update_params(struct vt_encoder *enc, obs_data_t *settings)
  686. {
  687. video_t *video = obs_encoder_video(enc->encoder);
  688. const struct video_output_info *voi = video_output_get_info(video);
  689. const char *codec = obs_encoder_get_codec(enc->encoder);
  690. if (strcmp(codec, "h264") == 0) {
  691. enc->codec_type = kCMVideoCodecType_H264;
  692. obs_data_set_int(settings, "codec_type", enc->codec_type);
  693. #ifdef ENABLE_HEVC
  694. } else if (strcmp(codec, "hevc") == 0) {
  695. enc->codec_type = kCMVideoCodecType_HEVC;
  696. obs_data_set_int(settings, "codec_type", enc->codec_type);
  697. #endif
  698. } else {
  699. enc->codec_type = (CMVideoCodecType)obs_data_get_int(
  700. settings, "codec_type");
  701. }
  702. SetVideoFormatResult res =
  703. set_video_format(enc, voi->format, voi->range);
  704. if (res == kResultColorFormatUnsupported) {
  705. obs_encoder_set_last_error(
  706. enc->encoder,
  707. obs_module_text("ColorFormatUnsupported"));
  708. VT_BLOG(LOG_WARNING, "Unsupported color format selected");
  709. return false;
  710. } else if (res == kResultFullRangeUnsupported) {
  711. obs_encoder_set_last_error(
  712. enc->encoder, obs_module_text("FullRangeUnsupported"));
  713. VT_BLOG(LOG_WARNING, "Unsupported color range (full) selected");
  714. return false;
  715. }
  716. enc->colorspace = voi->colorspace;
  717. enc->width = obs_encoder_get_width(enc->encoder);
  718. enc->height = obs_encoder_get_height(enc->encoder);
  719. enc->fps_num = voi->fps_num;
  720. enc->fps_den = voi->fps_den;
  721. enc->keyint = (uint32_t)obs_data_get_int(settings, "keyint_sec");
  722. enc->rate_control = obs_data_get_string(settings, "rate_control");
  723. enc->bitrate = (uint32_t)obs_data_get_int(settings, "bitrate");
  724. enc->quality = ((float)obs_data_get_int(settings, "quality")) / 100;
  725. enc->profile = obs_data_get_string(settings, "profile");
  726. enc->limit_bitrate = obs_data_get_bool(settings, "limit_bitrate");
  727. enc->rc_max_bitrate =
  728. (uint32_t)obs_data_get_int(settings, "max_bitrate");
  729. enc->rc_max_bitrate_window =
  730. obs_data_get_double(settings, "max_bitrate_window");
  731. enc->bframes = obs_data_get_bool(settings, "bframes");
  732. return true;
  733. }
  734. static bool vt_update(void *data, obs_data_t *settings)
  735. {
  736. struct vt_encoder *enc = data;
  737. uint32_t old_bitrate = enc->bitrate;
  738. bool old_limit_bitrate = enc->limit_bitrate;
  739. update_params(enc, settings);
  740. if (old_bitrate == enc->bitrate &&
  741. old_limit_bitrate == enc->limit_bitrate)
  742. return true;
  743. OSStatus code = session_set_bitrate(enc->session, enc->rate_control,
  744. enc->bitrate, enc->quality,
  745. enc->limit_bitrate,
  746. enc->rc_max_bitrate,
  747. enc->rc_max_bitrate_window);
  748. if (code != noErr)
  749. VT_BLOG(LOG_WARNING, "Failed to set bitrate to session");
  750. dump_encoder_info(enc);
  751. return true;
  752. }
  753. static void *vt_create(obs_data_t *settings, obs_encoder_t *encoder)
  754. {
  755. struct vt_encoder *enc = bzalloc(sizeof(struct vt_encoder));
  756. OSStatus code;
  757. enc->encoder = encoder;
  758. enc->vt_encoder_id = obs_encoder_get_id(encoder);
  759. if (!update_params(enc, settings))
  760. goto fail;
  761. code = CMSimpleQueueCreate(NULL, 100, &enc->queue);
  762. if (code != noErr) {
  763. goto fail;
  764. }
  765. if (!create_encoder(enc))
  766. goto fail;
  767. dump_encoder_info(enc);
  768. return enc;
  769. fail:
  770. vt_destroy(enc);
  771. return NULL;
  772. }
  773. static const uint8_t annexb_startcode[4] = {0, 0, 0, 1};
  774. static void packet_put(struct darray *packet, const uint8_t *buf, size_t size)
  775. {
  776. darray_push_back_array(sizeof(uint8_t), packet, buf, size);
  777. }
  778. static void packet_put_startcode(struct darray *packet, int size)
  779. {
  780. assert(size == 3 || size == 4);
  781. packet_put(packet, &annexb_startcode[4 - size], size);
  782. }
  783. static bool handle_prores_packet(struct vt_encoder *enc,
  784. CMSampleBufferRef buffer)
  785. {
  786. OSStatus err = 0;
  787. size_t block_size = 0;
  788. uint8_t *block_buf = NULL;
  789. CMBlockBufferRef block = CMSampleBufferGetDataBuffer(buffer);
  790. if (block == NULL) {
  791. VT_BLOG(LOG_ERROR,
  792. "Failed to get block buffer for ProRes frame.");
  793. return false;
  794. }
  795. err = CMBlockBufferGetDataPointer(block, 0, NULL, &block_size,
  796. (char **)&block_buf);
  797. if (err != 0) {
  798. VT_BLOG(LOG_ERROR,
  799. "Failed to get data buffer pointer for ProRes frame.");
  800. return false;
  801. }
  802. packet_put(&enc->packet_data.da, block_buf, block_size);
  803. return true;
  804. }
  805. static void convert_block_nals_to_annexb(struct vt_encoder *enc,
  806. struct darray *packet,
  807. CMBlockBufferRef block,
  808. int nal_length_bytes)
  809. {
  810. size_t block_size;
  811. uint8_t *block_buf;
  812. CMBlockBufferGetDataPointer(block, 0, NULL, &block_size,
  813. (char **)&block_buf);
  814. size_t bytes_remaining = block_size;
  815. while (bytes_remaining > 0) {
  816. uint32_t nal_size;
  817. if (nal_length_bytes == 1)
  818. nal_size = block_buf[0];
  819. else if (nal_length_bytes == 2)
  820. nal_size = CFSwapInt16BigToHost(
  821. ((uint16_t *)block_buf)[0]);
  822. else if (nal_length_bytes == 4)
  823. nal_size = CFSwapInt32BigToHost(
  824. ((uint32_t *)block_buf)[0]);
  825. else
  826. return;
  827. bytes_remaining -= nal_length_bytes;
  828. block_buf += nal_length_bytes;
  829. if (bytes_remaining < nal_size) {
  830. VT_BLOG(LOG_ERROR, "invalid nal block");
  831. return;
  832. }
  833. packet_put_startcode(packet, 3);
  834. packet_put(packet, block_buf, nal_size);
  835. bytes_remaining -= nal_size;
  836. block_buf += nal_size;
  837. }
  838. }
  839. static bool handle_keyframe(struct vt_encoder *enc,
  840. CMFormatDescriptionRef format_desc,
  841. size_t param_count, struct darray *packet,
  842. struct darray *extra_data)
  843. {
  844. OSStatus code;
  845. const uint8_t *param;
  846. size_t param_size;
  847. for (size_t i = 0; i < param_count; i++) {
  848. if (enc->codec_type == kCMVideoCodecType_H264) {
  849. code = CMVideoFormatDescriptionGetH264ParameterSetAtIndex(
  850. format_desc, i, &param, &param_size, NULL,
  851. NULL);
  852. #ifdef ENABLE_HEVC
  853. } else if (enc->codec_type == kCMVideoCodecType_HEVC) {
  854. code = CMVideoFormatDescriptionGetHEVCParameterSetAtIndex(
  855. format_desc, i, &param, &param_size, NULL,
  856. NULL);
  857. #endif
  858. }
  859. if (code != noErr) {
  860. log_osstatus(LOG_ERROR, enc,
  861. "getting NAL parameter "
  862. "at index",
  863. code);
  864. return false;
  865. }
  866. packet_put_startcode(packet, 4);
  867. packet_put(packet, param, param_size);
  868. }
  869. // if we were passed an extra_data array, fill it with
  870. // SPS, PPS, etc.
  871. if (extra_data != NULL)
  872. packet_put(extra_data, packet->array, packet->num);
  873. return true;
  874. }
  875. static bool convert_sample_to_annexb(struct vt_encoder *enc,
  876. struct darray *packet,
  877. struct darray *extra_data,
  878. CMSampleBufferRef buffer, bool keyframe)
  879. {
  880. OSStatus code;
  881. CMFormatDescriptionRef format_desc =
  882. CMSampleBufferGetFormatDescription(buffer);
  883. size_t param_count;
  884. int nal_length_bytes;
  885. if (enc->codec_type == kCMVideoCodecType_H264) {
  886. code = CMVideoFormatDescriptionGetH264ParameterSetAtIndex(
  887. format_desc, 0, NULL, NULL, &param_count,
  888. &nal_length_bytes);
  889. #ifdef ENABLE_HEVC
  890. } else if (enc->codec_type == kCMVideoCodecType_HEVC) {
  891. code = CMVideoFormatDescriptionGetHEVCParameterSetAtIndex(
  892. format_desc, 0, NULL, NULL, &param_count,
  893. &nal_length_bytes);
  894. #endif
  895. } else {
  896. log_osstatus(LOG_ERROR, enc, "invalid codec type",
  897. kCMFormatDescriptionError_ValueNotAvailable);
  898. return false;
  899. }
  900. // it is not clear what errors this function can return
  901. // so we check the two most reasonable
  902. if (code == kCMFormatDescriptionBridgeError_InvalidParameter ||
  903. code == kCMFormatDescriptionError_InvalidParameter) {
  904. VT_BLOG(LOG_WARNING, "assuming 2 parameter sets "
  905. "and 4 byte NAL length header");
  906. param_count = 2;
  907. nal_length_bytes = 4;
  908. } else if (code != noErr) {
  909. log_osstatus(LOG_ERROR, enc,
  910. "getting parameter count from sample", code);
  911. return false;
  912. }
  913. if (keyframe &&
  914. !handle_keyframe(enc, format_desc, param_count, packet, extra_data))
  915. return false;
  916. CMBlockBufferRef block = CMSampleBufferGetDataBuffer(buffer);
  917. convert_block_nals_to_annexb(enc, packet, block, nal_length_bytes);
  918. return true;
  919. }
  920. static bool is_sample_keyframe(CMSampleBufferRef buffer)
  921. {
  922. CFArrayRef attachments =
  923. CMSampleBufferGetSampleAttachmentsArray(buffer, false);
  924. if (attachments != NULL) {
  925. CFDictionaryRef attachment;
  926. CFBooleanRef has_dependencies;
  927. attachment =
  928. (CFDictionaryRef)CFArrayGetValueAtIndex(attachments, 0);
  929. has_dependencies = (CFBooleanRef)CFDictionaryGetValue(
  930. attachment, kCMSampleAttachmentKey_DependsOnOthers);
  931. return has_dependencies == kCFBooleanFalse;
  932. }
  933. return false;
  934. }
  935. static bool parse_sample(struct vt_encoder *enc, CMSampleBufferRef buffer,
  936. struct encoder_packet *packet, CMTime off)
  937. {
  938. CMTime pts = CMSampleBufferGetPresentationTimeStamp(buffer);
  939. CMTime dts = CMSampleBufferGetDecodeTimeStamp(buffer);
  940. if (CMTIME_IS_INVALID(dts))
  941. dts = pts;
  942. // imitate x264's negative dts when bframes might have pts < dts
  943. else if (enc->bframes)
  944. dts = CMTimeSubtract(dts, off);
  945. pts = CMTimeMultiply(pts, enc->fps_num);
  946. dts = CMTimeMultiply(dts, enc->fps_num);
  947. const bool is_avc = enc->codec_type == kCMVideoCodecType_H264;
  948. const bool has_annexb = is_avc ||
  949. (enc->codec_type == kCMVideoCodecType_HEVC);
  950. // All ProRes frames are "keyframes"
  951. const bool keyframe = !has_annexb || is_sample_keyframe(buffer);
  952. da_resize(enc->packet_data, 0);
  953. // If we are still looking for extra data
  954. struct darray *extra_data = NULL;
  955. if (enc->extra_data.num == 0)
  956. extra_data = &enc->extra_data.da;
  957. if (has_annexb) {
  958. if (!convert_sample_to_annexb(enc, &enc->packet_data.da,
  959. extra_data, buffer, keyframe))
  960. goto fail;
  961. } else {
  962. if (!handle_prores_packet(enc, buffer))
  963. goto fail;
  964. }
  965. packet->type = OBS_ENCODER_VIDEO;
  966. packet->pts = (int64_t)(CMTimeGetSeconds(pts));
  967. packet->dts = (int64_t)(CMTimeGetSeconds(dts));
  968. packet->data = enc->packet_data.array;
  969. packet->size = enc->packet_data.num;
  970. packet->keyframe = keyframe;
  971. if (is_avc) {
  972. // VideoToolbox produces packets with priority lower than the RTMP code
  973. // expects, which causes it to be unable to recover from frame drops.
  974. // Fix this by manually adjusting the priority.
  975. uint8_t *start = enc->packet_data.array;
  976. uint8_t *end = start + enc->packet_data.num;
  977. start = (uint8_t *)obs_avc_find_startcode(start, end);
  978. while (true) {
  979. while (start < end && !*(start++))
  980. ;
  981. if (start == end)
  982. break;
  983. const int type = start[0] & 0x1F;
  984. if (type == OBS_NAL_SLICE_IDR ||
  985. type == OBS_NAL_SLICE) {
  986. uint8_t prev_type = (start[0] >> 5) & 0x3;
  987. start[0] &= ~(3 << 5);
  988. if (type == OBS_NAL_SLICE_IDR)
  989. start[0] |= OBS_NAL_PRIORITY_HIGHEST
  990. << 5;
  991. else if (type == OBS_NAL_SLICE &&
  992. prev_type !=
  993. OBS_NAL_PRIORITY_DISPOSABLE)
  994. start[0] |= OBS_NAL_PRIORITY_HIGH << 5;
  995. else
  996. start[0] |= prev_type << 5;
  997. }
  998. start = (uint8_t *)obs_avc_find_startcode(start, end);
  999. }
  1000. }
  1001. CFRelease(buffer);
  1002. return true;
  1003. fail:
  1004. CFRelease(buffer);
  1005. return false;
  1006. }
  1007. bool get_cached_pixel_buffer(struct vt_encoder *enc, CVPixelBufferRef *buf)
  1008. {
  1009. OSStatus code;
  1010. CVPixelBufferPoolRef pool =
  1011. VTCompressionSessionGetPixelBufferPool(enc->session);
  1012. if (!pool)
  1013. return kCVReturnError;
  1014. CVPixelBufferRef pixbuf;
  1015. code = CVPixelBufferPoolCreatePixelBuffer(NULL, pool, &pixbuf);
  1016. if (code != noErr) {
  1017. goto fail;
  1018. }
  1019. // Why aren't these already set on the pixel buffer?
  1020. // I would have expected pixel buffers from the session's
  1021. // pool to have the correct color space stuff set
  1022. const enum video_colorspace cs = enc->colorspace;
  1023. CVBufferSetAttachment(pixbuf, kCVImageBufferYCbCrMatrixKey,
  1024. obs_to_vt_colorspace(cs),
  1025. kCVAttachmentMode_ShouldPropagate);
  1026. CVBufferSetAttachment(pixbuf, kCVImageBufferColorPrimariesKey,
  1027. obs_to_vt_primaries(cs),
  1028. kCVAttachmentMode_ShouldPropagate);
  1029. CVBufferSetAttachment(pixbuf, kCVImageBufferTransferFunctionKey,
  1030. obs_to_vt_transfer(cs),
  1031. kCVAttachmentMode_ShouldPropagate);
  1032. const bool pq = cs == VIDEO_CS_2100_PQ;
  1033. const bool hlg = cs == VIDEO_CS_2100_HLG;
  1034. if (pq || hlg) {
  1035. const uint16_t hdr_nominal_peak_level =
  1036. pq ? (uint16_t)obs_get_video_hdr_nominal_peak_level()
  1037. : (hlg ? 1000 : 0);
  1038. CFDataRef masteringDisplayColorVolume =
  1039. obs_to_vt_masteringdisplay(hdr_nominal_peak_level);
  1040. CFDataRef contentLightLevel =
  1041. obs_to_vt_contentlightlevelinfo(hdr_nominal_peak_level);
  1042. CVBufferSetAttachment(
  1043. pixbuf, kCVImageBufferMasteringDisplayColorVolumeKey,
  1044. masteringDisplayColorVolume,
  1045. kCVAttachmentMode_ShouldPropagate);
  1046. CVBufferSetAttachment(pixbuf,
  1047. kCVImageBufferContentLightLevelInfoKey,
  1048. contentLightLevel,
  1049. kCVAttachmentMode_ShouldPropagate);
  1050. CFRelease(masteringDisplayColorVolume);
  1051. CFRelease(contentLightLevel);
  1052. }
  1053. *buf = pixbuf;
  1054. return true;
  1055. fail:
  1056. return false;
  1057. }
  1058. static bool vt_encode(void *data, struct encoder_frame *frame,
  1059. struct encoder_packet *packet, bool *received_packet)
  1060. {
  1061. struct vt_encoder *enc = data;
  1062. OSStatus code;
  1063. CMTime dur = CMTimeMake(enc->fps_den, enc->fps_num);
  1064. CMTime off = CMTimeMultiply(dur, 2);
  1065. CMTime pts = CMTimeMake(frame->pts, enc->fps_num);
  1066. CVPixelBufferRef pixbuf = NULL;
  1067. if (!get_cached_pixel_buffer(enc, &pixbuf)) {
  1068. VT_BLOG(LOG_ERROR, "Unable to create pixel buffer");
  1069. goto fail;
  1070. }
  1071. code = CVPixelBufferLockBaseAddress(pixbuf, 0);
  1072. if (code != noErr) {
  1073. goto fail;
  1074. }
  1075. for (int i = 0; i < MAX_AV_PLANES; i++) {
  1076. if (frame->data[i] == NULL)
  1077. break;
  1078. uint8_t *p = (uint8_t *)CVPixelBufferGetBaseAddressOfPlane(
  1079. pixbuf, i);
  1080. uint8_t *f = frame->data[i];
  1081. size_t plane_linesize =
  1082. CVPixelBufferGetBytesPerRowOfPlane(pixbuf, i);
  1083. size_t plane_height = CVPixelBufferGetHeightOfPlane(pixbuf, i);
  1084. for (size_t j = 0; j < plane_height; j++) {
  1085. memcpy(p, f, frame->linesize[i]);
  1086. p += plane_linesize;
  1087. f += frame->linesize[i];
  1088. }
  1089. }
  1090. code = CVPixelBufferUnlockBaseAddress(pixbuf, 0);
  1091. if (code != noErr) {
  1092. goto fail;
  1093. }
  1094. code = VTCompressionSessionEncodeFrame(enc->session, pixbuf, pts, dur,
  1095. NULL, pixbuf, NULL);
  1096. if (code != noErr) {
  1097. goto fail;
  1098. }
  1099. CMSampleBufferRef buffer =
  1100. (CMSampleBufferRef)CMSimpleQueueDequeue(enc->queue);
  1101. // No samples waiting in the queue
  1102. if (buffer == NULL)
  1103. return true;
  1104. *received_packet = true;
  1105. return parse_sample(enc, buffer, packet, off);
  1106. fail:
  1107. return false;
  1108. }
  1109. static bool vt_extra_data(void *data, uint8_t **extra_data, size_t *size)
  1110. {
  1111. struct vt_encoder *enc = (struct vt_encoder *)data;
  1112. *extra_data = enc->extra_data.array;
  1113. *size = enc->extra_data.num;
  1114. return true;
  1115. }
  1116. static const char *vt_getname(void *data)
  1117. {
  1118. struct vt_encoder_type_data *type_data = data;
  1119. if (strcmp("Apple H.264 (HW)", type_data->disp_name) == 0) {
  1120. return obs_module_text("VTH264EncHW");
  1121. } else if (strcmp("Apple H.264 (SW)", type_data->disp_name) == 0) {
  1122. return obs_module_text("VTH264EncSW");
  1123. #ifdef ENABLE_HEVC
  1124. } else if (strcmp("Apple HEVC (HW)", type_data->disp_name) == 0) {
  1125. return obs_module_text("VTHEVCEncHW");
  1126. } else if (strcmp("Apple HEVC (AVE)", type_data->disp_name) == 0) {
  1127. return obs_module_text("VTHEVCEncT2");
  1128. } else if (strcmp("Apple HEVC (SW)", type_data->disp_name) == 0) {
  1129. return obs_module_text("VTHEVCEncSW");
  1130. #endif
  1131. } else if (strncmp("AppleProResHW", type_data->disp_name, 13) == 0) {
  1132. return obs_module_text("VTProResEncHW");
  1133. } else if (strncmp("Apple ProRes", type_data->disp_name, 12) == 0) {
  1134. return obs_module_text("VTProResEncSW");
  1135. }
  1136. return type_data->disp_name;
  1137. }
  1138. static bool rate_control_limit_bitrate_modified(obs_properties_t *ppts,
  1139. obs_property_t *p,
  1140. obs_data_t *settings)
  1141. {
  1142. bool has_bitrate = true;
  1143. bool can_limit_bitrate = true;
  1144. bool use_limit_bitrate = obs_data_get_bool(settings, "limit_bitrate");
  1145. const char *rate_control =
  1146. obs_data_get_string(settings, "rate_control");
  1147. if (strcmp(rate_control, "CBR") == 0) {
  1148. can_limit_bitrate = false;
  1149. has_bitrate = true;
  1150. } else if (strcmp(rate_control, "CRF") == 0) {
  1151. can_limit_bitrate = true;
  1152. has_bitrate = false;
  1153. } else if (strcmp(rate_control, "ABR") == 0) {
  1154. can_limit_bitrate = true;
  1155. has_bitrate = true;
  1156. }
  1157. p = obs_properties_get(ppts, "limit_bitrate");
  1158. obs_property_set_visible(p, can_limit_bitrate);
  1159. p = obs_properties_get(ppts, "max_bitrate");
  1160. obs_property_set_visible(p, can_limit_bitrate && use_limit_bitrate);
  1161. p = obs_properties_get(ppts, "max_bitrate_window");
  1162. obs_property_set_visible(p, can_limit_bitrate && use_limit_bitrate);
  1163. p = obs_properties_get(ppts, "bitrate");
  1164. obs_property_set_visible(p, has_bitrate);
  1165. p = obs_properties_get(ppts, "quality");
  1166. obs_property_set_visible(p, !has_bitrate);
  1167. return true;
  1168. }
  1169. static obs_properties_t *vt_properties_h26x(void *data __unused,
  1170. void *type_data)
  1171. {
  1172. struct vt_encoder_type_data *encoder_type_data = type_data;
  1173. obs_properties_t *props = obs_properties_create();
  1174. obs_property_t *p;
  1175. p = obs_properties_add_list(props, "rate_control",
  1176. obs_module_text("RateControl"),
  1177. OBS_COMBO_TYPE_LIST,
  1178. OBS_COMBO_FORMAT_STRING);
  1179. if (__builtin_available(macOS 13.0, *)) {
  1180. if (encoder_type_data->hardware_accelerated &&
  1181. is_apple_silicon) {
  1182. obs_property_list_add_string(p, "CBR", "CBR");
  1183. }
  1184. }
  1185. obs_property_list_add_string(p, "ABR", "ABR");
  1186. if (encoder_type_data->hardware_accelerated && is_apple_silicon) {
  1187. obs_property_list_add_string(p, "CRF", "CRF");
  1188. }
  1189. obs_property_set_modified_callback(p,
  1190. rate_control_limit_bitrate_modified);
  1191. p = obs_properties_add_int(props, "bitrate", obs_module_text("Bitrate"),
  1192. 50, 10000000, 50);
  1193. obs_property_int_set_suffix(p, " Kbps");
  1194. obs_properties_add_int_slider(props, "quality",
  1195. obs_module_text("Quality"), 0, 100, 1);
  1196. p = obs_properties_add_bool(props, "limit_bitrate",
  1197. obs_module_text("UseMaxBitrate"));
  1198. obs_property_set_modified_callback(p,
  1199. rate_control_limit_bitrate_modified);
  1200. p = obs_properties_add_int(props, "max_bitrate",
  1201. obs_module_text("MaxBitrate"), 50, 10000000,
  1202. 50);
  1203. obs_property_int_set_suffix(p, " Kbps");
  1204. p = obs_properties_add_float(props, "max_bitrate_window",
  1205. obs_module_text("MaxBitrateWindow"), 0.10f,
  1206. 10.0f, 0.25f);
  1207. obs_property_float_set_suffix(p, " s");
  1208. p = obs_properties_add_int(props, "keyint_sec",
  1209. obs_module_text("KeyframeIntervalSec"), 0,
  1210. 20, 1);
  1211. obs_property_int_set_suffix(p, " s");
  1212. p = obs_properties_add_list(props, "profile",
  1213. obs_module_text("Profile"),
  1214. OBS_COMBO_TYPE_LIST,
  1215. OBS_COMBO_FORMAT_STRING);
  1216. if (encoder_type_data->codec_type == kCMVideoCodecType_H264) {
  1217. obs_property_list_add_string(p, "baseline", "baseline");
  1218. obs_property_list_add_string(p, "main", "main");
  1219. obs_property_list_add_string(p, "high", "high");
  1220. #ifdef ENABLE_HEVC
  1221. } else if (encoder_type_data->codec_type == kCMVideoCodecType_HEVC) {
  1222. obs_property_list_add_string(p, "main", "main");
  1223. obs_property_list_add_string(p, "main10", "main10");
  1224. if (__builtin_available(macOS 12.3, *)) {
  1225. obs_property_list_add_string(p, "main 4:2:2 10",
  1226. "main42210");
  1227. }
  1228. #endif
  1229. }
  1230. obs_properties_add_bool(props, "bframes",
  1231. obs_module_text("UseBFrames"));
  1232. return props;
  1233. }
  1234. static obs_properties_t *vt_properties_prores(void *data __unused,
  1235. void *type_data)
  1236. {
  1237. struct vt_encoder_type_data *encoder_type_data = type_data;
  1238. obs_properties_t *props = obs_properties_create();
  1239. obs_property_t *p;
  1240. p = obs_properties_add_list(props, "codec_type",
  1241. obs_module_text("ProResCodec"),
  1242. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  1243. uint32_t codec_availability_flags = 0;
  1244. size_t size = 0;
  1245. struct vt_prores_encoder_data *encoder_list = NULL;
  1246. if (encoder_type_data->hardware_accelerated) {
  1247. size = vt_prores_hardware_encoder_list.num;
  1248. encoder_list = vt_prores_hardware_encoder_list.array;
  1249. } else {
  1250. size = vt_prores_software_encoder_list.num;
  1251. encoder_list = vt_prores_software_encoder_list.array;
  1252. }
  1253. for (size_t i = 0; i < size; ++i) {
  1254. switch (encoder_list[i].codec_type) {
  1255. case kCMVideoCodecType_AppleProRes4444XQ:
  1256. codec_availability_flags |= (1 << 0);
  1257. break;
  1258. case kCMVideoCodecType_AppleProRes4444:
  1259. codec_availability_flags |= (1 << 1);
  1260. break;
  1261. case kCMVideoCodecType_AppleProRes422Proxy:
  1262. codec_availability_flags |= (1 << 2);
  1263. break;
  1264. case kCMVideoCodecType_AppleProRes422LT:
  1265. codec_availability_flags |= (1 << 3);
  1266. break;
  1267. case kCMVideoCodecType_AppleProRes422:
  1268. codec_availability_flags |= (1 << 4);
  1269. break;
  1270. case kCMVideoCodecType_AppleProRes422HQ:
  1271. codec_availability_flags |= (1 << 5);
  1272. break;
  1273. }
  1274. }
  1275. if (codec_availability_flags & (1 << 0))
  1276. obs_property_list_add_int(p, obs_module_text("ProRes4444XQ"),
  1277. kCMVideoCodecType_AppleProRes4444XQ);
  1278. if (codec_availability_flags & (1 << 1))
  1279. obs_property_list_add_int(p, obs_module_text("ProRes4444"),
  1280. kCMVideoCodecType_AppleProRes4444);
  1281. if (codec_availability_flags & (1 << 2))
  1282. obs_property_list_add_int(
  1283. p, obs_module_text("ProRes422Proxy"),
  1284. kCMVideoCodecType_AppleProRes422Proxy);
  1285. if (codec_availability_flags & (1 << 3))
  1286. obs_property_list_add_int(p, obs_module_text("ProRes422LT"),
  1287. kCMVideoCodecType_AppleProRes422LT);
  1288. if (codec_availability_flags & (1 << 4))
  1289. obs_property_list_add_int(p, obs_module_text("ProRes422"),
  1290. kCMVideoCodecType_AppleProRes422);
  1291. if (codec_availability_flags & (1 << 5))
  1292. obs_property_list_add_int(p, obs_module_text("ProRes422HQ"),
  1293. kCMVideoCodecType_AppleProRes422HQ);
  1294. return props;
  1295. }
  1296. static void vt_defaults(obs_data_t *settings, void *data)
  1297. {
  1298. struct vt_encoder_type_data *type_data = data;
  1299. obs_data_set_default_string(settings, "rate_control", "ABR");
  1300. if (__builtin_available(macOS 13.0, *)) {
  1301. if (type_data->hardware_accelerated && is_apple_silicon) {
  1302. obs_data_set_default_string(settings, "rate_control",
  1303. "CBR");
  1304. }
  1305. }
  1306. obs_data_set_default_int(settings, "bitrate", 2500);
  1307. obs_data_set_default_int(settings, "quality", 60);
  1308. obs_data_set_default_bool(settings, "limit_bitrate", false);
  1309. obs_data_set_default_int(settings, "max_bitrate", 2500);
  1310. obs_data_set_default_double(settings, "max_bitrate_window", 1.5f);
  1311. obs_data_set_default_int(settings, "keyint_sec", 0);
  1312. obs_data_set_default_string(
  1313. settings, "profile",
  1314. type_data->codec_type == kCMVideoCodecType_H264 ? "high"
  1315. : "main");
  1316. obs_data_set_default_int(settings, "codec_type",
  1317. kCMVideoCodecType_AppleProRes422);
  1318. obs_data_set_default_bool(settings, "bframes", true);
  1319. }
  1320. static void vt_free_type_data(void *data)
  1321. {
  1322. struct vt_encoder_type_data *type_data = data;
  1323. bfree((char *)type_data->disp_name);
  1324. bfree((char *)type_data->id);
  1325. bfree(type_data);
  1326. }
  1327. static inline void
  1328. vt_add_prores_encoder_data_to_list(CFDictionaryRef encoder_dict,
  1329. FourCharCode codec_type)
  1330. {
  1331. struct vt_prores_encoder_data *encoder_data = NULL;
  1332. CFBooleanRef hardware_accelerated = CFDictionaryGetValue(
  1333. encoder_dict, kVTVideoEncoderList_IsHardwareAccelerated);
  1334. if (hardware_accelerated == kCFBooleanTrue)
  1335. encoder_data =
  1336. da_push_back_new(vt_prores_hardware_encoder_list);
  1337. else
  1338. encoder_data =
  1339. da_push_back_new(vt_prores_software_encoder_list);
  1340. encoder_data->encoder_id = CFDictionaryGetValue(
  1341. encoder_dict, kVTVideoEncoderList_EncoderID);
  1342. encoder_data->codec_type = codec_type;
  1343. }
  1344. OBS_DECLARE_MODULE()
  1345. OBS_MODULE_USE_DEFAULT_LOCALE("mac-videotoolbox", "en-US")
  1346. dispatch_group_t encoder_list_dispatch_group;
  1347. CFArrayRef encoder_list;
  1348. bool obs_module_load(void)
  1349. {
  1350. dispatch_queue_t queue =
  1351. dispatch_queue_create("Encoder list load queue", NULL);
  1352. encoder_list_dispatch_group = dispatch_group_create();
  1353. dispatch_group_async(encoder_list_dispatch_group, queue, ^{
  1354. VTCopyVideoEncoderList(NULL, &encoder_list);
  1355. });
  1356. // The group dispatch keeps a reference until it's finished
  1357. dispatch_release(queue);
  1358. #ifndef __aarch64__
  1359. is_apple_silicon = os_get_emulation_status();
  1360. #endif
  1361. return true;
  1362. }
  1363. void obs_module_post_load(void)
  1364. {
  1365. struct obs_encoder_info info = {
  1366. .type = OBS_ENCODER_VIDEO,
  1367. .get_name = vt_getname,
  1368. .create = vt_create,
  1369. .destroy = vt_destroy,
  1370. .encode = vt_encode,
  1371. .update = vt_update,
  1372. .get_defaults2 = vt_defaults,
  1373. .get_extra_data = vt_extra_data,
  1374. .free_type_data = vt_free_type_data,
  1375. .caps = OBS_ENCODER_CAP_DYN_BITRATE,
  1376. };
  1377. da_init(vt_prores_hardware_encoder_list);
  1378. da_init(vt_prores_software_encoder_list);
  1379. dispatch_group_wait(encoder_list_dispatch_group, DISPATCH_TIME_FOREVER);
  1380. dispatch_release(encoder_list_dispatch_group);
  1381. CFIndex size = CFArrayGetCount(encoder_list);
  1382. for (CFIndex i = 0; i < size; i++) {
  1383. CFDictionaryRef encoder_dict =
  1384. CFArrayGetValueAtIndex(encoder_list, i);
  1385. CMVideoCodecType codec_type = 0;
  1386. {
  1387. CFNumberRef codec_type_num = CFDictionaryGetValue(
  1388. encoder_dict, kVTVideoEncoderList_CodecType);
  1389. CFNumberGetValue(codec_type_num, kCFNumberSInt32Type,
  1390. &codec_type);
  1391. }
  1392. switch (codec_type) {
  1393. case kCMVideoCodecType_H264:
  1394. info.get_properties2 = vt_properties_h26x;
  1395. info.codec = "h264";
  1396. break;
  1397. #ifdef ENABLE_HEVC
  1398. case kCMVideoCodecType_HEVC:
  1399. info.get_properties2 = vt_properties_h26x;
  1400. info.codec = "hevc";
  1401. break;
  1402. #endif
  1403. // 422 is used as a marker for all ProRes types,
  1404. // since the type is stored as a profile
  1405. case kCMVideoCodecType_AppleProRes422:
  1406. info.get_properties2 = vt_properties_prores;
  1407. info.codec = "prores";
  1408. vt_add_prores_encoder_data_to_list(encoder_dict,
  1409. codec_type);
  1410. break;
  1411. case kCMVideoCodecType_AppleProRes4444XQ:
  1412. case kCMVideoCodecType_AppleProRes4444:
  1413. case kCMVideoCodecType_AppleProRes422Proxy:
  1414. case kCMVideoCodecType_AppleProRes422LT:
  1415. case kCMVideoCodecType_AppleProRes422HQ:
  1416. vt_add_prores_encoder_data_to_list(encoder_dict,
  1417. codec_type);
  1418. continue;
  1419. default:
  1420. continue;
  1421. }
  1422. CFStringRef EncoderID = CFDictionaryGetValue(
  1423. encoder_dict, kVTVideoEncoderList_EncoderID);
  1424. CFIndex id_len =
  1425. CFStringGetMaximumSizeOfFileSystemRepresentation(
  1426. EncoderID);
  1427. char *id = bzalloc(id_len + 1);
  1428. CFStringGetFileSystemRepresentation(EncoderID, id, id_len);
  1429. CFStringRef DisplayName = CFDictionaryGetValue(
  1430. encoder_dict, kVTVideoEncoderList_DisplayName);
  1431. CFIndex disp_name_len =
  1432. CFStringGetMaximumSizeOfFileSystemRepresentation(
  1433. DisplayName);
  1434. char *disp_name = bzalloc(disp_name_len + 1);
  1435. CFStringGetFileSystemRepresentation(DisplayName, disp_name,
  1436. disp_name_len);
  1437. CFBooleanRef hardware_ref = CFDictionaryGetValue(
  1438. encoder_dict,
  1439. kVTVideoEncoderList_IsHardwareAccelerated);
  1440. bool hardware_accelerated =
  1441. (hardware_ref) ? CFBooleanGetValue(hardware_ref)
  1442. : false;
  1443. info.id = id;
  1444. struct vt_encoder_type_data *type_data =
  1445. bzalloc(sizeof(struct vt_encoder_type_data));
  1446. type_data->disp_name = disp_name;
  1447. type_data->id = id;
  1448. type_data->codec_type = codec_type;
  1449. type_data->hardware_accelerated = hardware_accelerated;
  1450. info.type_data = type_data;
  1451. obs_register_encoder(&info);
  1452. }
  1453. CFRelease(encoder_list);
  1454. VT_LOG(LOG_INFO, "Added VideoToolbox encoders");
  1455. }
  1456. void obs_module_unload(void)
  1457. {
  1458. da_free(vt_prores_hardware_encoder_list);
  1459. da_free(vt_prores_software_encoder_list);
  1460. }