encoder.c 27 KB

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