encoder.c 28 KB

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