encoder.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114
  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, level, format, ...) \
  14. blog(level, "[VideoToolbox %s: 'h264']: " format, \
  15. obs_encoder_get_name(encoder), ##__VA_ARGS__)
  16. #define VT_BLOG(level, format, ...) \
  17. VT_LOG_ENCODER(enc->encoder, level, format, ##__VA_ARGS__)
  18. struct vt_encoder_type_data {
  19. const char *disp_name;
  20. const char *id;
  21. bool hardware_accelerated;
  22. };
  23. struct vt_encoder {
  24. obs_encoder_t *encoder;
  25. const char *vt_encoder_id;
  26. uint32_t width;
  27. uint32_t height;
  28. uint32_t keyint;
  29. uint32_t fps_num;
  30. uint32_t fps_den;
  31. const char *rate_control;
  32. uint32_t bitrate;
  33. float quality;
  34. bool limit_bitrate;
  35. uint32_t rc_max_bitrate;
  36. float rc_max_bitrate_window;
  37. const char *profile;
  38. bool bframes;
  39. enum video_format obs_pix_fmt;
  40. int vt_pix_fmt;
  41. enum video_colorspace colorspace;
  42. bool fullrange;
  43. VTCompressionSessionRef session;
  44. CMSimpleQueueRef queue;
  45. bool hw_enc;
  46. DARRAY(uint8_t) packet_data;
  47. DARRAY(uint8_t) extra_data;
  48. };
  49. static void log_osstatus(int log_level, struct vt_encoder *enc,
  50. const char *context, OSStatus code)
  51. {
  52. char *c_str = NULL;
  53. CFErrorRef err = CFErrorCreate(kCFAllocatorDefault,
  54. kCFErrorDomainOSStatus, code, NULL);
  55. CFStringRef str = CFErrorCopyDescription(err);
  56. c_str = cfstr_copy_cstr(str, kCFStringEncodingUTF8);
  57. if (c_str) {
  58. if (enc)
  59. VT_BLOG(log_level, "Error in %s: %s", context, c_str);
  60. else
  61. VT_LOG(log_level, "Error in %s: %s", context, c_str);
  62. }
  63. bfree(c_str);
  64. CFRelease(str);
  65. CFRelease(err);
  66. }
  67. static CFStringRef obs_to_vt_profile(const char *profile)
  68. {
  69. if (strcmp(profile, "baseline") == 0)
  70. return kVTProfileLevel_H264_Baseline_AutoLevel;
  71. else if (strcmp(profile, "main") == 0)
  72. return kVTProfileLevel_H264_Main_AutoLevel;
  73. else if (strcmp(profile, "high") == 0)
  74. return kVTProfileLevel_H264_High_AutoLevel;
  75. else
  76. return kVTProfileLevel_H264_Main_AutoLevel;
  77. }
  78. static CFStringRef obs_to_vt_colorspace(enum video_colorspace cs)
  79. {
  80. if (cs == VIDEO_CS_709)
  81. return kCVImageBufferYCbCrMatrix_ITU_R_709_2;
  82. else if (cs == VIDEO_CS_601)
  83. return kCVImageBufferYCbCrMatrix_ITU_R_601_4;
  84. return NULL;
  85. }
  86. #define STATUS_CHECK(c) \
  87. code = c; \
  88. if (code) { \
  89. log_osstatus(LOG_ERROR, enc, #c, code); \
  90. goto fail; \
  91. }
  92. #define SESSION_CHECK(x) \
  93. if ((code = (x)) != noErr) \
  94. return code;
  95. static OSStatus session_set_prop_float(VTCompressionSessionRef session,
  96. CFStringRef key, float val)
  97. {
  98. CFNumberRef n = CFNumberCreate(NULL, kCFNumberFloat32Type, &val);
  99. OSStatus code = VTSessionSetProperty(session, key, n);
  100. CFRelease(n);
  101. return code;
  102. }
  103. static OSStatus session_set_prop_int(VTCompressionSessionRef session,
  104. CFStringRef key, int32_t val)
  105. {
  106. CFNumberRef n = CFNumberCreate(NULL, kCFNumberSInt32Type, &val);
  107. OSStatus code = VTSessionSetProperty(session, key, n);
  108. CFRelease(n);
  109. return code;
  110. }
  111. static OSStatus session_set_prop_str(VTCompressionSessionRef session,
  112. CFStringRef key, char *val)
  113. {
  114. CFStringRef s = CFStringCreateWithFileSystemRepresentation(NULL, val);
  115. OSStatus code = VTSessionSetProperty(session, key, s);
  116. CFRelease(s);
  117. return code;
  118. }
  119. static OSStatus session_set_prop(VTCompressionSessionRef session,
  120. CFStringRef key, CFTypeRef val)
  121. {
  122. return VTSessionSetProperty(session, key, val);
  123. }
  124. static OSStatus session_set_bitrate(VTCompressionSessionRef session,
  125. const char *rate_control, int new_bitrate,
  126. float quality, bool limit_bitrate,
  127. int max_bitrate, float max_bitrate_window)
  128. {
  129. OSStatus code;
  130. bool can_limit_bitrate;
  131. CFStringRef compressionPropertyKey;
  132. if (strcmp(rate_control, "CBR") == 0) {
  133. compressionPropertyKey =
  134. kVTCompressionPropertyKey_AverageBitRate;
  135. can_limit_bitrate = true;
  136. if (__builtin_available(macOS 13.0, *)) {
  137. #if __MAC_OS_X_VERSION_MAX_ALLOWED >= 130000
  138. #ifdef __aarch64__
  139. if (true) {
  140. #else
  141. if (os_get_emulation_status() == true) {
  142. #endif
  143. compressionPropertyKey =
  144. kVTCompressionPropertyKey_ConstantBitRate;
  145. can_limit_bitrate = false;
  146. } else {
  147. VT_LOG(LOG_WARNING,
  148. "CBR support for VideoToolbox encoder requires Apple Silicon. "
  149. "Will use ABR instead.");
  150. }
  151. #else
  152. VT_LOG(LOG_WARNING,
  153. "CBR support for VideoToolbox not available in this build of OBS. "
  154. "Will use ABR instead.");
  155. #endif
  156. } else {
  157. VT_LOG(LOG_WARNING,
  158. "CBR support for VideoToolbox encoder requires macOS 13 or newer. "
  159. "Will use ABR instead.");
  160. }
  161. } else if (strcmp(rate_control, "ABR") == 0) {
  162. compressionPropertyKey =
  163. kVTCompressionPropertyKey_AverageBitRate;
  164. can_limit_bitrate = true;
  165. } else if (strcmp(rate_control, "CRF") == 0) {
  166. #ifdef __aarch64__
  167. if (true) {
  168. #else
  169. if (os_get_emulation_status() == true) {
  170. #endif
  171. compressionPropertyKey =
  172. kVTCompressionPropertyKey_Quality;
  173. SESSION_CHECK(session_set_prop_float(
  174. session, compressionPropertyKey, quality));
  175. } else {
  176. VT_LOG(LOG_WARNING,
  177. "CRF support for VideoToolbox encoder requires Apple Silicon. "
  178. "Will use ABR instead.");
  179. compressionPropertyKey =
  180. kVTCompressionPropertyKey_AverageBitRate;
  181. }
  182. can_limit_bitrate = true;
  183. } else {
  184. VT_LOG(LOG_ERROR,
  185. "Selected rate control method is not supported: %s",
  186. rate_control);
  187. return kVTParameterErr;
  188. }
  189. if (compressionPropertyKey != kVTCompressionPropertyKey_Quality) {
  190. SESSION_CHECK(session_set_prop_int(
  191. session, compressionPropertyKey, new_bitrate * 1000));
  192. }
  193. if (limit_bitrate && can_limit_bitrate) {
  194. int32_t cpb_size = max_bitrate * 125 * max_bitrate_window;
  195. CFNumberRef cf_cpb_size =
  196. CFNumberCreate(NULL, kCFNumberIntType, &cpb_size);
  197. CFNumberRef cf_cpb_window_s = CFNumberCreate(
  198. NULL, kCFNumberFloatType, &max_bitrate_window);
  199. CFMutableArrayRef rate_control = CFArrayCreateMutable(
  200. kCFAllocatorDefault, 2, &kCFTypeArrayCallBacks);
  201. CFArrayAppendValue(rate_control, cf_cpb_size);
  202. CFArrayAppendValue(rate_control, cf_cpb_window_s);
  203. code = session_set_prop(
  204. session, kVTCompressionPropertyKey_DataRateLimits,
  205. rate_control);
  206. CFRelease(cf_cpb_size);
  207. CFRelease(cf_cpb_window_s);
  208. CFRelease(rate_control);
  209. if (code == kVTPropertyNotSupportedErr) {
  210. log_osstatus(LOG_WARNING, NULL,
  211. "setting DataRateLimits on session", code);
  212. return noErr;
  213. }
  214. }
  215. return noErr;
  216. }
  217. static OSStatus session_set_colorspace(VTCompressionSessionRef session,
  218. enum video_colorspace cs)
  219. {
  220. CFStringRef matrix = obs_to_vt_colorspace(cs);
  221. OSStatus code;
  222. if (matrix != NULL) {
  223. SESSION_CHECK(session_set_prop(
  224. session, kVTCompressionPropertyKey_ColorPrimaries,
  225. kCVImageBufferColorPrimaries_ITU_R_709_2));
  226. SESSION_CHECK(session_set_prop(
  227. session, kVTCompressionPropertyKey_TransferFunction,
  228. kCVImageBufferTransferFunction_ITU_R_709_2));
  229. SESSION_CHECK(session_set_prop(
  230. session, kVTCompressionPropertyKey_YCbCrMatrix,
  231. matrix));
  232. }
  233. return noErr;
  234. }
  235. #undef SESSION_CHECK
  236. void sample_encoded_callback(void *data, void *source, OSStatus status,
  237. VTEncodeInfoFlags info_flags,
  238. CMSampleBufferRef buffer)
  239. {
  240. UNUSED_PARAMETER(status);
  241. UNUSED_PARAMETER(info_flags);
  242. CMSimpleQueueRef queue = data;
  243. CVPixelBufferRef pixbuf = source;
  244. if (buffer != NULL) {
  245. CFRetain(buffer);
  246. CMSimpleQueueEnqueue(queue, buffer);
  247. }
  248. CFRelease(pixbuf);
  249. }
  250. #define ENCODER_ID kVTVideoEncoderSpecification_EncoderID
  251. #define ENABLE_HW_ACCEL \
  252. kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder
  253. #define REQUIRE_HW_ACCEL \
  254. kVTVideoEncoderSpecification_RequireHardwareAcceleratedVideoEncoder
  255. static inline CFMutableDictionaryRef
  256. create_encoder_spec(const char *vt_encoder_id)
  257. {
  258. CFMutableDictionaryRef encoder_spec = CFDictionaryCreateMutable(
  259. kCFAllocatorDefault, 3, &kCFTypeDictionaryKeyCallBacks,
  260. &kCFTypeDictionaryValueCallBacks);
  261. CFStringRef id =
  262. CFStringCreateWithFileSystemRepresentation(NULL, vt_encoder_id);
  263. CFDictionaryAddValue(encoder_spec, ENCODER_ID, id);
  264. CFRelease(id);
  265. CFDictionaryAddValue(encoder_spec, ENABLE_HW_ACCEL, kCFBooleanTrue);
  266. CFDictionaryAddValue(encoder_spec, REQUIRE_HW_ACCEL, kCFBooleanFalse);
  267. return encoder_spec;
  268. }
  269. #undef ENCODER_ID
  270. #undef REQUIRE_HW_ACCEL
  271. #undef ENABLE_HW_ACCEL
  272. static inline CFMutableDictionaryRef create_pixbuf_spec(struct vt_encoder *enc)
  273. {
  274. CFMutableDictionaryRef pixbuf_spec = CFDictionaryCreateMutable(
  275. kCFAllocatorDefault, 3, &kCFTypeDictionaryKeyCallBacks,
  276. &kCFTypeDictionaryValueCallBacks);
  277. CFNumberRef n =
  278. CFNumberCreate(NULL, kCFNumberSInt32Type, &enc->vt_pix_fmt);
  279. CFDictionaryAddValue(pixbuf_spec, kCVPixelBufferPixelFormatTypeKey, n);
  280. CFRelease(n);
  281. n = CFNumberCreate(NULL, kCFNumberSInt32Type, &enc->width);
  282. CFDictionaryAddValue(pixbuf_spec, kCVPixelBufferWidthKey, n);
  283. CFRelease(n);
  284. n = CFNumberCreate(NULL, kCFNumberSInt32Type, &enc->height);
  285. CFDictionaryAddValue(pixbuf_spec, kCVPixelBufferHeightKey, n);
  286. CFRelease(n);
  287. return pixbuf_spec;
  288. }
  289. static bool create_encoder(struct vt_encoder *enc)
  290. {
  291. OSStatus code;
  292. VTCompressionSessionRef s;
  293. CFDictionaryRef encoder_spec = create_encoder_spec(enc->vt_encoder_id);
  294. CFDictionaryRef pixbuf_spec = create_pixbuf_spec(enc);
  295. STATUS_CHECK(VTCompressionSessionCreate(
  296. kCFAllocatorDefault, enc->width, enc->height,
  297. kCMVideoCodecType_H264, encoder_spec, pixbuf_spec, NULL,
  298. &sample_encoded_callback, enc->queue, &s));
  299. CFRelease(encoder_spec);
  300. CFRelease(pixbuf_spec);
  301. CFBooleanRef b = NULL;
  302. code = VTSessionCopyProperty(
  303. s,
  304. kVTCompressionPropertyKey_UsingHardwareAcceleratedVideoEncoder,
  305. NULL, &b);
  306. if (code == noErr && (enc->hw_enc = CFBooleanGetValue(b)))
  307. VT_BLOG(LOG_INFO, "session created with hardware encoding");
  308. else
  309. enc->hw_enc = false;
  310. if (b != NULL)
  311. CFRelease(b);
  312. STATUS_CHECK(session_set_prop_int(
  313. s, kVTCompressionPropertyKey_MaxKeyFrameIntervalDuration,
  314. enc->keyint));
  315. STATUS_CHECK(session_set_prop_int(
  316. s, kVTCompressionPropertyKey_MaxKeyFrameInterval,
  317. enc->keyint * ((float)enc->fps_num / enc->fps_den)));
  318. STATUS_CHECK(session_set_prop_float(
  319. s, kVTCompressionPropertyKey_ExpectedFrameRate,
  320. (float)enc->fps_num / enc->fps_den));
  321. STATUS_CHECK(session_set_prop(
  322. s, kVTCompressionPropertyKey_AllowFrameReordering,
  323. enc->bframes ? kCFBooleanTrue : kCFBooleanFalse));
  324. // This can fail depending on hardware configuration
  325. code = session_set_prop(s, kVTCompressionPropertyKey_RealTime,
  326. kCFBooleanFalse);
  327. if (code != noErr)
  328. log_osstatus(
  329. LOG_WARNING, enc,
  330. "setting kVTCompressionPropertyKey_RealTime failed, "
  331. "frame delay might be increased",
  332. code);
  333. STATUS_CHECK(session_set_prop(s, kVTCompressionPropertyKey_ProfileLevel,
  334. obs_to_vt_profile(enc->profile)));
  335. STATUS_CHECK(session_set_bitrate(s, enc->rate_control, enc->bitrate,
  336. enc->quality, enc->limit_bitrate,
  337. enc->rc_max_bitrate,
  338. enc->rc_max_bitrate_window));
  339. STATUS_CHECK(session_set_colorspace(s, enc->colorspace));
  340. STATUS_CHECK(VTCompressionSessionPrepareToEncodeFrames(s));
  341. enc->session = s;
  342. return true;
  343. fail:
  344. if (encoder_spec != NULL)
  345. CFRelease(encoder_spec);
  346. if (pixbuf_spec != NULL)
  347. CFRelease(pixbuf_spec);
  348. return false;
  349. }
  350. static void vt_destroy(void *data)
  351. {
  352. struct vt_encoder *enc = data;
  353. if (enc) {
  354. if (enc->session != NULL) {
  355. VTCompressionSessionInvalidate(enc->session);
  356. CFRelease(enc->session);
  357. }
  358. da_free(enc->packet_data);
  359. da_free(enc->extra_data);
  360. bfree(enc);
  361. }
  362. }
  363. static void dump_encoder_info(struct vt_encoder *enc)
  364. {
  365. VT_BLOG(LOG_INFO,
  366. "settings:\n"
  367. "\tvt_encoder_id %s\n"
  368. "\trate_control: %s\n"
  369. "\tbitrate: %d (kbps)\n"
  370. "\tquality: %f\n"
  371. "\tfps_num: %d\n"
  372. "\tfps_den: %d\n"
  373. "\twidth: %d\n"
  374. "\theight: %d\n"
  375. "\tkeyint: %d (s)\n"
  376. "\tlimit_bitrate: %s\n"
  377. "\trc_max_bitrate: %d (kbps)\n"
  378. "\trc_max_bitrate_window: %f (s)\n"
  379. "\thw_enc: %s\n"
  380. "\tprofile: %s\n",
  381. enc->vt_encoder_id, enc->rate_control, enc->bitrate,
  382. enc->quality, enc->fps_num, enc->fps_den, enc->width,
  383. enc->height, enc->keyint, enc->limit_bitrate ? "on" : "off",
  384. enc->rc_max_bitrate, enc->rc_max_bitrate_window,
  385. enc->hw_enc ? "on" : "off",
  386. (enc->profile != NULL && !!strlen(enc->profile)) ? enc->profile
  387. : "default");
  388. }
  389. static void vt_video_info(void *data, struct video_scale_info *info)
  390. {
  391. struct vt_encoder *enc = data;
  392. if (info->format == VIDEO_FORMAT_I420) {
  393. enc->obs_pix_fmt = info->format;
  394. enc->vt_pix_fmt =
  395. enc->fullrange
  396. ? kCVPixelFormatType_420YpCbCr8PlanarFullRange
  397. : kCVPixelFormatType_420YpCbCr8Planar;
  398. return;
  399. }
  400. if (info->format == VIDEO_FORMAT_I444)
  401. VT_BLOG(LOG_WARNING, "I444 color format not supported");
  402. // Anything else, return default
  403. enc->obs_pix_fmt = VIDEO_FORMAT_NV12;
  404. enc->vt_pix_fmt =
  405. enc->fullrange
  406. ? kCVPixelFormatType_420YpCbCr8BiPlanarFullRange
  407. : kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange;
  408. info->format = enc->obs_pix_fmt;
  409. }
  410. static void update_params(struct vt_encoder *enc, obs_data_t *settings)
  411. {
  412. video_t *video = obs_encoder_video(enc->encoder);
  413. const struct video_output_info *voi = video_output_get_info(video);
  414. struct video_scale_info info = {.format = voi->format};
  415. enc->fullrange = voi->range == VIDEO_RANGE_FULL;
  416. // also sets the enc->vt_pix_fmt
  417. vt_video_info(enc, &info);
  418. enc->colorspace = voi->colorspace;
  419. enc->width = obs_encoder_get_width(enc->encoder);
  420. enc->height = obs_encoder_get_height(enc->encoder);
  421. enc->fps_num = voi->fps_num;
  422. enc->fps_den = voi->fps_den;
  423. enc->keyint = (uint32_t)obs_data_get_int(settings, "keyint_sec");
  424. enc->rate_control = obs_data_get_string(settings, "rate_control");
  425. enc->bitrate = (uint32_t)obs_data_get_int(settings, "bitrate");
  426. enc->quality = ((float)obs_data_get_int(settings, "quality")) / 100;
  427. enc->profile = obs_data_get_string(settings, "profile");
  428. enc->limit_bitrate = obs_data_get_bool(settings, "limit_bitrate");
  429. enc->rc_max_bitrate = obs_data_get_int(settings, "max_bitrate");
  430. enc->rc_max_bitrate_window =
  431. obs_data_get_double(settings, "max_bitrate_window");
  432. enc->bframes = obs_data_get_bool(settings, "bframes");
  433. }
  434. static bool vt_update(void *data, obs_data_t *settings)
  435. {
  436. struct vt_encoder *enc = data;
  437. uint32_t old_bitrate = enc->bitrate;
  438. bool old_limit_bitrate = enc->limit_bitrate;
  439. update_params(enc, settings);
  440. if (old_bitrate == enc->bitrate &&
  441. old_limit_bitrate == enc->limit_bitrate)
  442. return true;
  443. OSStatus code = session_set_bitrate(enc->session, enc->rate_control,
  444. enc->bitrate, enc->quality,
  445. enc->limit_bitrate,
  446. enc->rc_max_bitrate,
  447. enc->rc_max_bitrate_window);
  448. if (code != noErr)
  449. VT_BLOG(LOG_WARNING, "Failed to set bitrate to session");
  450. dump_encoder_info(enc);
  451. return true;
  452. }
  453. static void *vt_create(obs_data_t *settings, obs_encoder_t *encoder)
  454. {
  455. struct vt_encoder *enc = bzalloc(sizeof(struct vt_encoder));
  456. OSStatus code;
  457. enc->encoder = encoder;
  458. enc->vt_encoder_id = obs_encoder_get_id(encoder);
  459. update_params(enc, settings);
  460. STATUS_CHECK(CMSimpleQueueCreate(NULL, 100, &enc->queue));
  461. if (!create_encoder(enc))
  462. goto fail;
  463. dump_encoder_info(enc);
  464. return enc;
  465. fail:
  466. vt_destroy(enc);
  467. return NULL;
  468. }
  469. static const uint8_t annexb_startcode[4] = {0, 0, 0, 1};
  470. static void packet_put(struct darray *packet, const uint8_t *buf, size_t size)
  471. {
  472. darray_push_back_array(sizeof(uint8_t), packet, buf, size);
  473. }
  474. static void packet_put_startcode(struct darray *packet, int size)
  475. {
  476. assert(size == 3 || size == 4);
  477. packet_put(packet, &annexb_startcode[4 - size], size);
  478. }
  479. static void convert_block_nals_to_annexb(struct vt_encoder *enc,
  480. struct darray *packet,
  481. CMBlockBufferRef block,
  482. int nal_length_bytes)
  483. {
  484. size_t block_size;
  485. uint8_t *block_buf;
  486. CMBlockBufferGetDataPointer(block, 0, NULL, &block_size,
  487. (char **)&block_buf);
  488. size_t bytes_remaining = block_size;
  489. while (bytes_remaining > 0) {
  490. uint32_t nal_size;
  491. if (nal_length_bytes == 1)
  492. nal_size = block_buf[0];
  493. else if (nal_length_bytes == 2)
  494. nal_size = CFSwapInt16BigToHost(
  495. ((uint16_t *)block_buf)[0]);
  496. else if (nal_length_bytes == 4)
  497. nal_size = CFSwapInt32BigToHost(
  498. ((uint32_t *)block_buf)[0]);
  499. else
  500. return;
  501. bytes_remaining -= nal_length_bytes;
  502. block_buf += nal_length_bytes;
  503. if (bytes_remaining < nal_size) {
  504. VT_BLOG(LOG_ERROR, "invalid nal block");
  505. return;
  506. }
  507. packet_put_startcode(packet, 3);
  508. packet_put(packet, block_buf, nal_size);
  509. bytes_remaining -= nal_size;
  510. block_buf += nal_size;
  511. }
  512. }
  513. static bool handle_keyframe(struct vt_encoder *enc,
  514. CMFormatDescriptionRef format_desc,
  515. size_t param_count, struct darray *packet,
  516. struct darray *extra_data)
  517. {
  518. OSStatus code;
  519. const uint8_t *param;
  520. size_t param_size;
  521. for (size_t i = 0; i < param_count; i++) {
  522. code = CMVideoFormatDescriptionGetH264ParameterSetAtIndex(
  523. format_desc, i, &param, &param_size, NULL, NULL);
  524. if (code != noErr) {
  525. log_osstatus(LOG_ERROR, enc,
  526. "getting NAL parameter "
  527. "at index",
  528. code);
  529. return false;
  530. }
  531. packet_put_startcode(packet, 4);
  532. packet_put(packet, param, param_size);
  533. }
  534. // if we were passed an extra_data array, fill it with
  535. // SPS, PPS, etc.
  536. if (extra_data != NULL)
  537. packet_put(extra_data, packet->array, packet->num);
  538. return true;
  539. }
  540. static bool convert_sample_to_annexb(struct vt_encoder *enc,
  541. struct darray *packet,
  542. struct darray *extra_data,
  543. CMSampleBufferRef buffer, bool keyframe)
  544. {
  545. OSStatus code;
  546. CMFormatDescriptionRef format_desc =
  547. CMSampleBufferGetFormatDescription(buffer);
  548. size_t param_count;
  549. int nal_length_bytes;
  550. code = CMVideoFormatDescriptionGetH264ParameterSetAtIndex(
  551. format_desc, 0, NULL, NULL, &param_count, &nal_length_bytes);
  552. // it is not clear what errors this function can return
  553. // so we check the two most reasonable
  554. if (code == kCMFormatDescriptionBridgeError_InvalidParameter ||
  555. code == kCMFormatDescriptionError_InvalidParameter) {
  556. VT_BLOG(LOG_WARNING, "assuming 2 parameter sets "
  557. "and 4 byte NAL length header");
  558. param_count = 2;
  559. nal_length_bytes = 4;
  560. } else if (code != noErr) {
  561. log_osstatus(LOG_ERROR, enc,
  562. "getting parameter count from sample", code);
  563. return false;
  564. }
  565. if (keyframe &&
  566. !handle_keyframe(enc, format_desc, param_count, packet, extra_data))
  567. return false;
  568. CMBlockBufferRef block = CMSampleBufferGetDataBuffer(buffer);
  569. convert_block_nals_to_annexb(enc, packet, block, nal_length_bytes);
  570. return true;
  571. }
  572. static bool is_sample_keyframe(CMSampleBufferRef buffer)
  573. {
  574. CFArrayRef attachments =
  575. CMSampleBufferGetSampleAttachmentsArray(buffer, false);
  576. if (attachments != NULL) {
  577. CFDictionaryRef attachment;
  578. CFBooleanRef has_dependencies;
  579. attachment =
  580. (CFDictionaryRef)CFArrayGetValueAtIndex(attachments, 0);
  581. has_dependencies = (CFBooleanRef)CFDictionaryGetValue(
  582. attachment, kCMSampleAttachmentKey_DependsOnOthers);
  583. return has_dependencies == kCFBooleanFalse;
  584. }
  585. return false;
  586. }
  587. static bool parse_sample(struct vt_encoder *enc, CMSampleBufferRef buffer,
  588. struct encoder_packet *packet, CMTime off)
  589. {
  590. int type;
  591. CMTime pts = CMSampleBufferGetPresentationTimeStamp(buffer);
  592. CMTime dts = CMSampleBufferGetDecodeTimeStamp(buffer);
  593. if (CMTIME_IS_INVALID(dts))
  594. dts = pts;
  595. // imitate x264's negative dts when bframes might have pts < dts
  596. else if (enc->bframes)
  597. dts = CMTimeSubtract(dts, off);
  598. pts = CMTimeMultiply(pts, enc->fps_num);
  599. dts = CMTimeMultiply(dts, enc->fps_num);
  600. bool keyframe = is_sample_keyframe(buffer);
  601. da_resize(enc->packet_data, 0);
  602. // If we are still looking for extra data
  603. struct darray *extra_data = NULL;
  604. if (enc->extra_data.num == 0)
  605. extra_data = &enc->extra_data.da;
  606. if (!convert_sample_to_annexb(enc, &enc->packet_data.da, extra_data,
  607. buffer, keyframe))
  608. goto fail;
  609. packet->type = OBS_ENCODER_VIDEO;
  610. packet->pts = (int64_t)(CMTimeGetSeconds(pts));
  611. packet->dts = (int64_t)(CMTimeGetSeconds(dts));
  612. packet->data = enc->packet_data.array;
  613. packet->size = enc->packet_data.num;
  614. packet->keyframe = keyframe;
  615. // VideoToolbox produces packets with priority lower than the RTMP code
  616. // expects, which causes it to be unable to recover from frame drops.
  617. // Fix this by manually adjusting the priority.
  618. uint8_t *start = enc->packet_data.array;
  619. uint8_t *end = start + enc->packet_data.num;
  620. start = (uint8_t *)obs_avc_find_startcode(start, end);
  621. while (true) {
  622. while (start < end && !*(start++))
  623. ;
  624. if (start == end)
  625. break;
  626. type = start[0] & 0x1F;
  627. if (type == OBS_NAL_SLICE_IDR || type == OBS_NAL_SLICE) {
  628. uint8_t prev_type = (start[0] >> 5) & 0x3;
  629. start[0] &= ~(3 << 5);
  630. if (type == OBS_NAL_SLICE_IDR)
  631. start[0] |= OBS_NAL_PRIORITY_HIGHEST << 5;
  632. else if (type == OBS_NAL_SLICE &&
  633. prev_type != OBS_NAL_PRIORITY_DISPOSABLE)
  634. start[0] |= OBS_NAL_PRIORITY_HIGH << 5;
  635. else
  636. start[0] |= prev_type << 5;
  637. }
  638. start = (uint8_t *)obs_avc_find_startcode(start, end);
  639. }
  640. CFRelease(buffer);
  641. return true;
  642. fail:
  643. CFRelease(buffer);
  644. return false;
  645. }
  646. bool get_cached_pixel_buffer(struct vt_encoder *enc, CVPixelBufferRef *buf)
  647. {
  648. OSStatus code;
  649. CVPixelBufferPoolRef pool =
  650. VTCompressionSessionGetPixelBufferPool(enc->session);
  651. if (!pool)
  652. return kCVReturnError;
  653. CVPixelBufferRef pixbuf;
  654. STATUS_CHECK(CVPixelBufferPoolCreatePixelBuffer(NULL, pool, &pixbuf));
  655. // Why aren't these already set on the pixel buffer?
  656. // I would have expected pixel buffers from the session's
  657. // pool to have the correct color space stuff set
  658. CFStringRef matrix = obs_to_vt_colorspace(enc->colorspace);
  659. CVBufferSetAttachment(pixbuf, kCVImageBufferYCbCrMatrixKey, matrix,
  660. kCVAttachmentMode_ShouldPropagate);
  661. CVBufferSetAttachment(pixbuf, kCVImageBufferColorPrimariesKey,
  662. kCVImageBufferColorPrimaries_ITU_R_709_2,
  663. kCVAttachmentMode_ShouldPropagate);
  664. CVBufferSetAttachment(pixbuf, kCVImageBufferTransferFunctionKey,
  665. kCVImageBufferTransferFunction_ITU_R_709_2,
  666. kCVAttachmentMode_ShouldPropagate);
  667. *buf = pixbuf;
  668. return true;
  669. fail:
  670. return false;
  671. }
  672. static bool vt_encode(void *data, struct encoder_frame *frame,
  673. struct encoder_packet *packet, bool *received_packet)
  674. {
  675. struct vt_encoder *enc = data;
  676. OSStatus code;
  677. CMTime dur = CMTimeMake(enc->fps_den, enc->fps_num);
  678. CMTime off = CMTimeMultiply(dur, 2);
  679. CMTime pts = CMTimeMake(frame->pts, enc->fps_num);
  680. CVPixelBufferRef pixbuf = NULL;
  681. if (!get_cached_pixel_buffer(enc, &pixbuf)) {
  682. VT_BLOG(LOG_ERROR, "Unable to create pixel buffer");
  683. goto fail;
  684. }
  685. STATUS_CHECK(CVPixelBufferLockBaseAddress(pixbuf, 0));
  686. for (int i = 0; i < MAX_AV_PLANES; i++) {
  687. if (frame->data[i] == NULL)
  688. break;
  689. uint8_t *p = (uint8_t *)CVPixelBufferGetBaseAddressOfPlane(
  690. pixbuf, i);
  691. uint8_t *f = frame->data[i];
  692. size_t plane_linesize =
  693. CVPixelBufferGetBytesPerRowOfPlane(pixbuf, i);
  694. size_t plane_height = CVPixelBufferGetHeightOfPlane(pixbuf, i);
  695. for (size_t j = 0; j < plane_height; j++) {
  696. memcpy(p, f, frame->linesize[i]);
  697. p += plane_linesize;
  698. f += frame->linesize[i];
  699. }
  700. }
  701. STATUS_CHECK(CVPixelBufferUnlockBaseAddress(pixbuf, 0));
  702. STATUS_CHECK(VTCompressionSessionEncodeFrame(enc->session, pixbuf, pts,
  703. dur, NULL, pixbuf, NULL));
  704. CMSampleBufferRef buffer =
  705. (CMSampleBufferRef)CMSimpleQueueDequeue(enc->queue);
  706. // No samples waiting in the queue
  707. if (buffer == NULL)
  708. return true;
  709. *received_packet = true;
  710. return parse_sample(enc, buffer, packet, off);
  711. fail:
  712. return false;
  713. }
  714. #undef STATUS_CHECK
  715. #undef CFNUM_INT
  716. static bool vt_extra_data(void *data, uint8_t **extra_data, size_t *size)
  717. {
  718. struct vt_encoder *enc = (struct vt_encoder *)data;
  719. *extra_data = enc->extra_data.array;
  720. *size = enc->extra_data.num;
  721. return true;
  722. }
  723. static const char *vt_getname(void *data)
  724. {
  725. struct vt_encoder_type_data *type_data = data;
  726. if (strcmp("Apple H.264 (HW)", type_data->disp_name) == 0) {
  727. return obs_module_text("VTH264EncHW");
  728. } else if (strcmp("Apple H.264 (SW)", type_data->disp_name) == 0) {
  729. return obs_module_text("VTH264EncSW");
  730. }
  731. return type_data->disp_name;
  732. }
  733. #define TEXT_VT_ENCODER obs_module_text("VTEncoder")
  734. #define TEXT_BITRATE obs_module_text("Bitrate")
  735. #define TEXT_QUALITY obs_module_text("Quality")
  736. #define TEXT_USE_MAX_BITRATE obs_module_text("UseMaxBitrate")
  737. #define TEXT_MAX_BITRATE obs_module_text("MaxBitrate")
  738. #define TEXT_MAX_BITRATE_WINDOW obs_module_text("MaxBitrateWindow")
  739. #define TEXT_KEYINT_SEC obs_module_text("KeyframeIntervalSec")
  740. #define TEXT_PROFILE obs_module_text("Profile")
  741. #define TEXT_NONE obs_module_text("None")
  742. #define TEXT_DEFAULT obs_module_text("DefaultEncoder")
  743. #define TEXT_BFRAMES obs_module_text("UseBFrames")
  744. #define TEXT_RATE_CONTROL obs_module_text("RateControl")
  745. static bool rate_control_limit_bitrate_modified(obs_properties_t *ppts,
  746. obs_property_t *p,
  747. obs_data_t *settings)
  748. {
  749. bool has_bitrate = true;
  750. bool can_limit_bitrate = true;
  751. bool use_limit_bitrate = obs_data_get_bool(settings, "limit_bitrate");
  752. const char *rate_control =
  753. obs_data_get_string(settings, "rate_control");
  754. if (strcmp(rate_control, "CBR") == 0) {
  755. can_limit_bitrate = false;
  756. has_bitrate = true;
  757. } else if (strcmp(rate_control, "CRF") == 0) {
  758. can_limit_bitrate = true;
  759. has_bitrate = false;
  760. } else if (strcmp(rate_control, "ABR") == 0) {
  761. can_limit_bitrate = true;
  762. has_bitrate = true;
  763. }
  764. p = obs_properties_get(ppts, "limit_bitrate");
  765. obs_property_set_visible(p, can_limit_bitrate);
  766. p = obs_properties_get(ppts, "max_bitrate");
  767. obs_property_set_visible(p, can_limit_bitrate && use_limit_bitrate);
  768. p = obs_properties_get(ppts, "max_bitrate_window");
  769. obs_property_set_visible(p, can_limit_bitrate && use_limit_bitrate);
  770. p = obs_properties_get(ppts, "bitrate");
  771. obs_property_set_visible(p, has_bitrate);
  772. p = obs_properties_get(ppts, "quality");
  773. obs_property_set_visible(p, !has_bitrate);
  774. return true;
  775. }
  776. static obs_properties_t *vt_properties(void *unused, void *data)
  777. {
  778. UNUSED_PARAMETER(unused);
  779. struct vt_encoder_type_data *type_data = data;
  780. obs_properties_t *props = obs_properties_create();
  781. obs_property_t *p;
  782. p = obs_properties_add_list(props, "rate_control", TEXT_RATE_CONTROL,
  783. OBS_COMBO_TYPE_LIST,
  784. OBS_COMBO_FORMAT_STRING);
  785. if (__builtin_available(macOS 13.0, *))
  786. if (type_data->hardware_accelerated
  787. #ifndef __aarch64__
  788. && (os_get_emulation_status() == true)
  789. #endif
  790. )
  791. obs_property_list_add_string(p, "CBR", "CBR");
  792. obs_property_list_add_string(p, "ABR", "ABR");
  793. if (type_data->hardware_accelerated
  794. #ifndef __aarch64__
  795. && (os_get_emulation_status() == true)
  796. #endif
  797. )
  798. obs_property_list_add_string(p, "CRF", "CRF");
  799. obs_property_set_modified_callback(p,
  800. rate_control_limit_bitrate_modified);
  801. p = obs_properties_add_int(props, "bitrate", TEXT_BITRATE, 50, 10000000,
  802. 50);
  803. obs_property_int_set_suffix(p, " Kbps");
  804. obs_properties_add_int_slider(props, "quality", TEXT_QUALITY, 0, 100,
  805. 1);
  806. p = obs_properties_add_bool(props, "limit_bitrate",
  807. TEXT_USE_MAX_BITRATE);
  808. obs_property_set_modified_callback(p,
  809. rate_control_limit_bitrate_modified);
  810. p = obs_properties_add_int(props, "max_bitrate", TEXT_MAX_BITRATE, 50,
  811. 10000000, 50);
  812. obs_property_int_set_suffix(p, " Kbps");
  813. p = obs_properties_add_float(props, "max_bitrate_window",
  814. TEXT_MAX_BITRATE_WINDOW, 0.10f, 10.0f,
  815. 0.25f);
  816. obs_property_float_set_suffix(p, " s");
  817. p = obs_properties_add_int(props, "keyint_sec", TEXT_KEYINT_SEC, 0, 20,
  818. 1);
  819. obs_property_int_set_suffix(p, " s");
  820. p = obs_properties_add_list(props, "profile", TEXT_PROFILE,
  821. OBS_COMBO_TYPE_LIST,
  822. OBS_COMBO_FORMAT_STRING);
  823. obs_property_list_add_string(p, TEXT_NONE, "");
  824. obs_property_list_add_string(p, "baseline", "baseline");
  825. obs_property_list_add_string(p, "main", "main");
  826. obs_property_list_add_string(p, "high", "high");
  827. obs_properties_add_bool(props, "bframes", TEXT_BFRAMES);
  828. return props;
  829. }
  830. static void vt_defaults(obs_data_t *settings, void *data)
  831. {
  832. struct vt_encoder_type_data *type_data = data;
  833. obs_data_set_default_string(settings, "rate_control", "ABR");
  834. if (__builtin_available(macOS 13.0, *))
  835. if (type_data->hardware_accelerated
  836. #ifndef __aarch64__
  837. && (os_get_emulation_status() == true)
  838. #endif
  839. )
  840. obs_data_set_default_string(settings, "rate_control",
  841. "CBR");
  842. obs_data_set_default_int(settings, "bitrate", 2500);
  843. obs_data_set_default_int(settings, "quality", 60);
  844. obs_data_set_default_bool(settings, "limit_bitrate", false);
  845. obs_data_set_default_int(settings, "max_bitrate", 2500);
  846. obs_data_set_default_double(settings, "max_bitrate_window", 1.5f);
  847. obs_data_set_default_int(settings, "keyint_sec", 0);
  848. obs_data_set_default_string(settings, "profile", "");
  849. obs_data_set_default_bool(settings, "bframes", true);
  850. }
  851. static void vt_free_type_data(void *data)
  852. {
  853. struct vt_encoder_type_data *type_data = data;
  854. bfree((char *)type_data->disp_name);
  855. bfree((char *)type_data->id);
  856. bfree(type_data);
  857. }
  858. OBS_DECLARE_MODULE()
  859. OBS_MODULE_USE_DEFAULT_LOCALE("mac-videotoolbox", "en-US")
  860. bool obs_module_load(void)
  861. {
  862. struct obs_encoder_info info = {
  863. .type = OBS_ENCODER_VIDEO,
  864. .codec = "h264",
  865. .get_name = vt_getname,
  866. .create = vt_create,
  867. .destroy = vt_destroy,
  868. .encode = vt_encode,
  869. .update = vt_update,
  870. .get_properties2 = vt_properties,
  871. .get_defaults2 = vt_defaults,
  872. .get_video_info = vt_video_info,
  873. .get_extra_data = vt_extra_data,
  874. .free_type_data = vt_free_type_data,
  875. .caps = OBS_ENCODER_CAP_DYN_BITRATE,
  876. };
  877. CFArrayRef encoder_list;
  878. VTCopyVideoEncoderList(NULL, &encoder_list);
  879. CFIndex size = CFArrayGetCount(encoder_list);
  880. for (CFIndex i = 0; i < size; i++) {
  881. CFDictionaryRef encoder_dict =
  882. CFArrayGetValueAtIndex(encoder_list, i);
  883. #define VT_DICTSTR(key, name) \
  884. CFStringRef name##_ref = CFDictionaryGetValue(encoder_dict, key); \
  885. CFIndex name##_len = CFStringGetLength(name##_ref); \
  886. char *name = bzalloc(name##_len + 1); \
  887. CFStringGetFileSystemRepresentation(name##_ref, name, name##_len);
  888. VT_DICTSTR(kVTVideoEncoderList_CodecName, codec_name);
  889. if (strcmp("H.264", codec_name) != 0) {
  890. bfree(codec_name);
  891. continue;
  892. }
  893. bfree(codec_name);
  894. VT_DICTSTR(kVTVideoEncoderList_EncoderID, id);
  895. VT_DICTSTR(kVTVideoEncoderList_DisplayName, disp_name);
  896. CFBooleanRef hardware_ref = CFDictionaryGetValue(
  897. encoder_dict,
  898. kVTVideoEncoderList_IsHardwareAccelerated);
  899. bool hardware_accelerated =
  900. (hardware_ref) ? CFBooleanGetValue(hardware_ref)
  901. : false;
  902. info.id = id;
  903. struct vt_encoder_type_data *type_data =
  904. bzalloc(sizeof(struct vt_encoder_type_data));
  905. type_data->disp_name = disp_name;
  906. type_data->id = id;
  907. type_data->hardware_accelerated = hardware_accelerated;
  908. info.type_data = type_data;
  909. obs_register_encoder(&info);
  910. #undef VT_DICTSTR
  911. }
  912. CFRelease(encoder_list);
  913. VT_LOG(LOG_INFO, "Adding VideoToolbox encoders");
  914. return true;
  915. }