encoder.c 31 KB

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