encoder.c 28 KB

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