encoder.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104
  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_BITRATE obs_module_text("Bitrate")
  737. #define TEXT_QUALITY obs_module_text("Quality")
  738. #define TEXT_USE_MAX_BITRATE obs_module_text("UseMaxBitrate")
  739. #define TEXT_MAX_BITRATE obs_module_text("MaxBitrate")
  740. #define TEXT_MAX_BITRATE_WINDOW obs_module_text("MaxBitrateWindow")
  741. #define TEXT_KEYINT_SEC obs_module_text("KeyframeIntervalSec")
  742. #define TEXT_PROFILE obs_module_text("Profile")
  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, "baseline", "baseline");
  824. obs_property_list_add_string(p, "main", "main");
  825. obs_property_list_add_string(p, "high", "high");
  826. obs_properties_add_bool(props, "bframes", TEXT_BFRAMES);
  827. return props;
  828. }
  829. static void vt_defaults(obs_data_t *settings, void *data)
  830. {
  831. struct vt_encoder_type_data *type_data = data;
  832. obs_data_set_default_string(settings, "rate_control", "ABR");
  833. if (__builtin_available(macOS 13.0, *))
  834. if (type_data->hardware_accelerated
  835. #ifndef __aarch64__
  836. && (os_get_emulation_status() == true)
  837. #endif
  838. )
  839. obs_data_set_default_string(settings, "rate_control",
  840. "CBR");
  841. obs_data_set_default_int(settings, "bitrate", 2500);
  842. obs_data_set_default_int(settings, "quality", 60);
  843. obs_data_set_default_bool(settings, "limit_bitrate", false);
  844. obs_data_set_default_int(settings, "max_bitrate", 2500);
  845. obs_data_set_default_double(settings, "max_bitrate_window", 1.5f);
  846. obs_data_set_default_int(settings, "keyint_sec", 0);
  847. obs_data_set_default_string(settings, "profile", "main");
  848. obs_data_set_default_bool(settings, "bframes", true);
  849. }
  850. static void vt_free_type_data(void *data)
  851. {
  852. struct vt_encoder_type_data *type_data = data;
  853. bfree((char *)type_data->disp_name);
  854. bfree((char *)type_data->id);
  855. bfree(type_data);
  856. }
  857. OBS_DECLARE_MODULE()
  858. OBS_MODULE_USE_DEFAULT_LOCALE("mac-videotoolbox", "en-US")
  859. bool obs_module_load(void)
  860. {
  861. struct obs_encoder_info info = {
  862. .type = OBS_ENCODER_VIDEO,
  863. .codec = "h264",
  864. .get_name = vt_getname,
  865. .create = vt_create,
  866. .destroy = vt_destroy,
  867. .encode = vt_encode,
  868. .update = vt_update,
  869. .get_properties2 = vt_properties,
  870. .get_defaults2 = vt_defaults,
  871. .get_extra_data = vt_extra_data,
  872. .free_type_data = vt_free_type_data,
  873. .caps = OBS_ENCODER_CAP_DYN_BITRATE,
  874. };
  875. CFArrayRef encoder_list;
  876. VTCopyVideoEncoderList(NULL, &encoder_list);
  877. CFIndex size = CFArrayGetCount(encoder_list);
  878. for (CFIndex i = 0; i < size; i++) {
  879. CFDictionaryRef encoder_dict =
  880. CFArrayGetValueAtIndex(encoder_list, i);
  881. #define VT_DICTSTR(key, name) \
  882. CFStringRef name##_ref = CFDictionaryGetValue(encoder_dict, key); \
  883. CFIndex name##_len = CFStringGetLength(name##_ref); \
  884. char *name = bzalloc(name##_len + 1); \
  885. CFStringGetFileSystemRepresentation(name##_ref, name, name##_len);
  886. VT_DICTSTR(kVTVideoEncoderList_CodecName, codec_name);
  887. if (strcmp("H.264", codec_name) != 0) {
  888. bfree(codec_name);
  889. continue;
  890. }
  891. bfree(codec_name);
  892. VT_DICTSTR(kVTVideoEncoderList_EncoderID, id);
  893. VT_DICTSTR(kVTVideoEncoderList_DisplayName, disp_name);
  894. CFBooleanRef hardware_ref = CFDictionaryGetValue(
  895. encoder_dict,
  896. kVTVideoEncoderList_IsHardwareAccelerated);
  897. bool hardware_accelerated =
  898. (hardware_ref) ? CFBooleanGetValue(hardware_ref)
  899. : false;
  900. info.id = id;
  901. struct vt_encoder_type_data *type_data =
  902. bzalloc(sizeof(struct vt_encoder_type_data));
  903. type_data->disp_name = disp_name;
  904. type_data->id = id;
  905. type_data->hardware_accelerated = hardware_accelerated;
  906. info.type_data = type_data;
  907. obs_register_encoder(&info);
  908. #undef VT_DICTSTR
  909. }
  910. CFRelease(encoder_list);
  911. VT_LOG(LOG_INFO, "Adding VideoToolbox encoders");
  912. return true;
  913. }