encoder.c 30 KB

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