obs-ffmpeg-vaapi.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804
  1. /******************************************************************************
  2. Copyright (C) 2016 by Hugh Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include <libavutil/avutil.h>
  15. #if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(55, 27, 100)
  16. #include <util/darray.h>
  17. #include <util/dstr.h>
  18. #include <util/base.h>
  19. #include <util/platform.h>
  20. #include <media-io/video-io.h>
  21. #include <obs-module.h>
  22. #include <obs-avc.h>
  23. #include <unistd.h>
  24. #include <libavutil/opt.h>
  25. #include <libavutil/pixdesc.h>
  26. #include <libavutil/hwcontext.h>
  27. #include <libavcodec/avcodec.h>
  28. #include <libavformat/avformat.h>
  29. #include <libavfilter/avfilter.h>
  30. #include <pci/pci.h>
  31. #include "vaapi-utils.h"
  32. #include "obs-ffmpeg-formats.h"
  33. #define do_log(level, format, ...) \
  34. blog(level, "[FFmpeg VAAPI encoder: '%s'] " format, \
  35. obs_encoder_get_name(enc->encoder), ##__VA_ARGS__)
  36. #define warn(format, ...) do_log(LOG_WARNING, format, ##__VA_ARGS__)
  37. #define info(format, ...) do_log(LOG_INFO, format, ##__VA_ARGS__)
  38. #define debug(format, ...) do_log(LOG_DEBUG, format, ##__VA_ARGS__)
  39. struct vaapi_encoder {
  40. obs_encoder_t *encoder;
  41. AVBufferRef *vadevice_ref;
  42. AVBufferRef *vaframes_ref;
  43. const AVCodec *vaapi;
  44. AVCodecContext *context;
  45. AVPacket *packet;
  46. AVFrame *vframe;
  47. DARRAY(uint8_t) buffer;
  48. uint8_t *header;
  49. size_t header_size;
  50. uint8_t *sei;
  51. size_t sei_size;
  52. int height;
  53. bool first_packet;
  54. bool initialized;
  55. };
  56. static const char *vaapi_getname(void *unused)
  57. {
  58. UNUSED_PARAMETER(unused);
  59. return "FFmpeg VAAPI H.264";
  60. }
  61. static inline bool valid_format(enum video_format format)
  62. {
  63. return format == VIDEO_FORMAT_NV12;
  64. }
  65. static void vaapi_video_info(void *data, struct video_scale_info *info)
  66. {
  67. struct vaapi_encoder *enc = data;
  68. enum video_format pref_format;
  69. pref_format = obs_encoder_get_preferred_video_format(enc->encoder);
  70. if (!valid_format(pref_format)) {
  71. pref_format = valid_format(info->format) ? info->format
  72. : VIDEO_FORMAT_NV12;
  73. }
  74. info->format = pref_format;
  75. }
  76. static bool vaapi_init_codec(struct vaapi_encoder *enc, const char *path)
  77. {
  78. int ret;
  79. ret = av_hwdevice_ctx_create(&enc->vadevice_ref, AV_HWDEVICE_TYPE_VAAPI,
  80. path, NULL, 0);
  81. if (ret < 0) {
  82. warn("Failed to create VAAPI device context: %s",
  83. av_err2str(ret));
  84. return false;
  85. }
  86. enc->vaframes_ref = av_hwframe_ctx_alloc(enc->vadevice_ref);
  87. if (!enc->vaframes_ref) {
  88. warn("Failed to alloc HW frames context");
  89. return false;
  90. }
  91. AVHWFramesContext *frames_ctx =
  92. (AVHWFramesContext *)enc->vaframes_ref->data;
  93. frames_ctx->format = AV_PIX_FMT_VAAPI;
  94. frames_ctx->sw_format = AV_PIX_FMT_NV12;
  95. frames_ctx->width = enc->context->width;
  96. frames_ctx->height = enc->context->height;
  97. frames_ctx->initial_pool_size = 20;
  98. ret = av_hwframe_ctx_init(enc->vaframes_ref);
  99. if (ret < 0) {
  100. warn("Failed to init HW frames context: %s", av_err2str(ret));
  101. return false;
  102. }
  103. /* 2. Create software frame and picture */
  104. enc->vframe = av_frame_alloc();
  105. if (!enc->vframe) {
  106. warn("Failed to allocate video frame");
  107. return false;
  108. }
  109. enc->vframe->format = enc->context->pix_fmt;
  110. enc->vframe->width = enc->context->width;
  111. enc->vframe->height = enc->context->height;
  112. enc->vframe->colorspace = enc->context->colorspace;
  113. enc->vframe->color_range = enc->context->color_range;
  114. ret = av_frame_get_buffer(enc->vframe, base_get_alignment());
  115. if (ret < 0) {
  116. warn("Failed to allocate vframe: %s", av_err2str(ret));
  117. return false;
  118. }
  119. /* 3. set up codec */
  120. enc->context->pix_fmt = AV_PIX_FMT_VAAPI;
  121. enc->context->hw_frames_ctx = av_buffer_ref(enc->vaframes_ref);
  122. ret = avcodec_open2(enc->context, enc->vaapi, NULL);
  123. if (ret < 0) {
  124. warn("Failed to open VAAPI codec: %s", av_err2str(ret));
  125. return false;
  126. }
  127. enc->packet = av_packet_alloc();
  128. enc->initialized = true;
  129. return true;
  130. }
  131. /* "Allowed" options per Rate Control
  132. * See FFMPEG libavcodec/vaapi_encode.c (vaapi_encode_rc_modes)
  133. */
  134. typedef struct {
  135. const char *name;
  136. bool qp;
  137. bool bitrate;
  138. bool maxrate;
  139. } rc_mode_t;
  140. static const rc_mode_t *get_rc_mode(const char *name)
  141. {
  142. /* Set "allowed" options per Rate Control */
  143. static const rc_mode_t RC_MODES[] = {
  144. {.name = "CBR", .qp = false, .bitrate = true, .maxrate = false},
  145. {.name = "CQP", .qp = true, .bitrate = false, .maxrate = false},
  146. {.name = "VBR", .qp = false, .bitrate = true, .maxrate = true},
  147. NULL};
  148. const rc_mode_t *rc_mode = RC_MODES;
  149. while (!!rc_mode && strcmp(rc_mode->name, name) != 0)
  150. rc_mode++;
  151. return rc_mode ? rc_mode : RC_MODES;
  152. }
  153. static bool vaapi_update(void *data, obs_data_t *settings)
  154. {
  155. struct vaapi_encoder *enc = data;
  156. const char *device = obs_data_get_string(settings, "vaapi_device");
  157. const char *rate_control =
  158. obs_data_get_string(settings, "rate_control");
  159. const rc_mode_t *rc_mode = get_rc_mode(rate_control);
  160. bool cbr = strcmp(rc_mode->name, "CBR") == 0;
  161. int profile = (int)obs_data_get_int(settings, "profile");
  162. int bf = (int)obs_data_get_int(settings, "bf");
  163. int qp = rc_mode->qp ? (int)obs_data_get_int(settings, "qp") : 0;
  164. av_opt_set_int(enc->context->priv_data, "qp", qp, 0);
  165. int level = (int)obs_data_get_int(settings, "level");
  166. int bitrate = rc_mode->bitrate
  167. ? (int)obs_data_get_int(settings, "bitrate")
  168. : 0;
  169. int maxrate = rc_mode->maxrate
  170. ? (int)obs_data_get_int(settings, "maxrate")
  171. : 0;
  172. int keyint_sec = (int)obs_data_get_int(settings, "keyint_sec");
  173. /* For Rate Control which allows maxrate, FFMPEG will give
  174. * an error if maxrate > bitrate. To prevent that set maxrate
  175. * to 0.
  176. * For CBR, maxrate = bitrate
  177. */
  178. if (cbr)
  179. maxrate = bitrate;
  180. else if (rc_mode->maxrate && maxrate && maxrate < bitrate)
  181. maxrate = 0;
  182. video_t *video = obs_encoder_video(enc->encoder);
  183. const struct video_output_info *voi = video_output_get_info(video);
  184. struct video_scale_info info;
  185. info.format = voi->format;
  186. info.colorspace = voi->colorspace;
  187. info.range = voi->range;
  188. vaapi_video_info(enc, &info);
  189. enc->context->profile = profile;
  190. enc->context->max_b_frames = bf;
  191. enc->context->level = level;
  192. enc->context->bit_rate = bitrate * 1000;
  193. enc->context->rc_max_rate = maxrate * 1000;
  194. enc->context->width = obs_encoder_get_width(enc->encoder);
  195. enc->context->height = obs_encoder_get_height(enc->encoder);
  196. enc->context->time_base = (AVRational){voi->fps_den, voi->fps_num};
  197. enc->context->pix_fmt = obs_to_ffmpeg_video_format(info.format);
  198. enc->context->color_range = info.range == VIDEO_RANGE_FULL
  199. ? AVCOL_RANGE_JPEG
  200. : AVCOL_RANGE_MPEG;
  201. switch (info.colorspace) {
  202. case VIDEO_CS_601:
  203. enc->context->color_trc = AVCOL_TRC_SMPTE170M;
  204. enc->context->color_primaries = AVCOL_PRI_SMPTE170M;
  205. enc->context->colorspace = AVCOL_SPC_SMPTE170M;
  206. break;
  207. case VIDEO_CS_DEFAULT:
  208. case VIDEO_CS_709:
  209. enc->context->color_trc = AVCOL_TRC_BT709;
  210. enc->context->color_primaries = AVCOL_PRI_BT709;
  211. enc->context->colorspace = AVCOL_SPC_BT709;
  212. break;
  213. case VIDEO_CS_SRGB:
  214. enc->context->color_trc = AVCOL_TRC_IEC61966_2_1;
  215. enc->context->color_primaries = AVCOL_PRI_BT709;
  216. enc->context->colorspace = AVCOL_SPC_BT709;
  217. break;
  218. }
  219. if (keyint_sec > 0) {
  220. enc->context->gop_size =
  221. keyint_sec * voi->fps_num / voi->fps_den;
  222. } else {
  223. enc->context->gop_size = 120;
  224. }
  225. enc->height = enc->context->height;
  226. info("settings:\n"
  227. "\tdevice: %s\n"
  228. "\trate_control: %s\n"
  229. "\tprofile: %d\n"
  230. "\tlevel: %d\n"
  231. "\tqp: %d\n"
  232. "\tbitrate: %d\n"
  233. "\tmaxrate: %d\n"
  234. "\tkeyint: %d\n"
  235. "\twidth: %d\n"
  236. "\theight: %d\n"
  237. "\tb-frames: %d\n",
  238. device, rate_control, profile, level, qp, bitrate, maxrate,
  239. enc->context->gop_size, enc->context->width, enc->context->height,
  240. enc->context->max_b_frames);
  241. return vaapi_init_codec(enc, device);
  242. }
  243. static inline void flush_remaining_packets(struct vaapi_encoder *enc)
  244. {
  245. int r_pkt = 1;
  246. while (r_pkt) {
  247. #if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(57, 40, 101)
  248. if (avcodec_receive_packet(enc->context, enc->packet) < 0)
  249. break;
  250. #else
  251. if (avcodec_encode_video2(enc->context, enc->packet, NULL,
  252. &r_pkt) < 0)
  253. break;
  254. #endif
  255. if (r_pkt)
  256. av_packet_unref(enc->packet);
  257. }
  258. }
  259. static void vaapi_destroy(void *data)
  260. {
  261. struct vaapi_encoder *enc = data;
  262. if (enc->initialized)
  263. flush_remaining_packets(enc);
  264. av_packet_free(&enc->packet);
  265. avcodec_free_context(&enc->context);
  266. av_frame_unref(enc->vframe);
  267. av_frame_free(&enc->vframe);
  268. av_buffer_unref(&enc->vaframes_ref);
  269. av_buffer_unref(&enc->vadevice_ref);
  270. da_free(enc->buffer);
  271. bfree(enc->header);
  272. bfree(enc->sei);
  273. bfree(enc);
  274. }
  275. static void *vaapi_create(obs_data_t *settings, obs_encoder_t *encoder)
  276. {
  277. struct vaapi_encoder *enc;
  278. #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 9, 100)
  279. avcodec_register_all();
  280. #endif
  281. enc = bzalloc(sizeof(*enc));
  282. enc->encoder = encoder;
  283. enc->vaapi = avcodec_find_encoder_by_name("h264_vaapi");
  284. enc->first_packet = true;
  285. blog(LOG_INFO, "---------------------------------");
  286. if (!enc->vaapi) {
  287. warn("Couldn't find encoder");
  288. goto fail;
  289. }
  290. enc->context = avcodec_alloc_context3(enc->vaapi);
  291. if (!enc->context) {
  292. warn("Failed to create codec context");
  293. goto fail;
  294. }
  295. if (!vaapi_update(enc, settings))
  296. goto fail;
  297. return enc;
  298. fail:
  299. vaapi_destroy(enc);
  300. return NULL;
  301. }
  302. static inline void copy_data(AVFrame *pic, const struct encoder_frame *frame,
  303. int height, enum AVPixelFormat format)
  304. {
  305. int h_chroma_shift, v_chroma_shift;
  306. av_pix_fmt_get_chroma_sub_sample(format, &h_chroma_shift,
  307. &v_chroma_shift);
  308. for (int plane = 0; plane < MAX_AV_PLANES; plane++) {
  309. if (!frame->data[plane])
  310. continue;
  311. int frame_rowsize = (int)frame->linesize[plane];
  312. int pic_rowsize = pic->linesize[plane];
  313. int bytes = frame_rowsize < pic_rowsize ? frame_rowsize
  314. : pic_rowsize;
  315. int plane_height = height >> (plane ? v_chroma_shift : 0);
  316. for (int y = 0; y < plane_height; y++) {
  317. int pos_frame = y * frame_rowsize;
  318. int pos_pic = y * pic_rowsize;
  319. memcpy(pic->data[plane] + pos_pic,
  320. frame->data[plane] + pos_frame, bytes);
  321. }
  322. }
  323. }
  324. static bool vaapi_encode(void *data, struct encoder_frame *frame,
  325. struct encoder_packet *packet, bool *received_packet)
  326. {
  327. struct vaapi_encoder *enc = data;
  328. AVFrame *hwframe = NULL;
  329. int got_packet;
  330. int ret;
  331. hwframe = av_frame_alloc();
  332. if (!hwframe) {
  333. warn("vaapi_encode: failed to allocate hw frame");
  334. return false;
  335. }
  336. ret = av_hwframe_get_buffer(enc->vaframes_ref, hwframe, 0);
  337. if (ret < 0) {
  338. warn("vaapi_encode: failed to get buffer for hw frame: %s",
  339. av_err2str(ret));
  340. goto fail;
  341. }
  342. copy_data(enc->vframe, frame, enc->height, enc->context->pix_fmt);
  343. enc->vframe->pts = frame->pts;
  344. hwframe->pts = frame->pts;
  345. hwframe->width = enc->vframe->width;
  346. hwframe->height = enc->vframe->height;
  347. ret = av_hwframe_transfer_data(hwframe, enc->vframe, 0);
  348. if (ret < 0) {
  349. warn("vaapi_encode: failed to upload hw frame: %s",
  350. av_err2str(ret));
  351. goto fail;
  352. }
  353. ret = av_frame_copy_props(hwframe, enc->vframe);
  354. if (ret < 0) {
  355. warn("vaapi_encode: failed to copy props to hw frame: %s",
  356. av_err2str(ret));
  357. goto fail;
  358. }
  359. #if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(57, 40, 101)
  360. ret = avcodec_send_frame(enc->context, hwframe);
  361. if (ret == 0)
  362. ret = avcodec_receive_packet(enc->context, enc->packet);
  363. got_packet = (ret == 0);
  364. if (ret == AVERROR_EOF || ret == AVERROR(EAGAIN))
  365. ret = 0;
  366. #else
  367. ret = avcodec_encode_video2(enc->context, enc->packet, hwframe,
  368. &got_packet);
  369. #endif
  370. if (ret < 0) {
  371. warn("vaapi_encode: Error encoding: %s", av_err2str(ret));
  372. goto fail;
  373. }
  374. if (got_packet && enc->packet->size) {
  375. if (enc->first_packet) {
  376. uint8_t *new_packet;
  377. size_t size;
  378. enc->first_packet = false;
  379. obs_extract_avc_headers(enc->packet->data,
  380. enc->packet->size, &new_packet,
  381. &size, &enc->header,
  382. &enc->header_size, &enc->sei,
  383. &enc->sei_size);
  384. da_copy_array(enc->buffer, new_packet, size);
  385. bfree(new_packet);
  386. } else {
  387. da_copy_array(enc->buffer, enc->packet->data,
  388. enc->packet->size);
  389. }
  390. packet->pts = enc->packet->pts;
  391. packet->dts = enc->packet->dts;
  392. packet->data = enc->buffer.array;
  393. packet->size = enc->buffer.num;
  394. packet->type = OBS_ENCODER_VIDEO;
  395. packet->keyframe = obs_avc_keyframe(packet->data, packet->size);
  396. *received_packet = true;
  397. } else {
  398. *received_packet = false;
  399. }
  400. av_packet_unref(enc->packet);
  401. av_frame_free(&hwframe);
  402. return true;
  403. fail:
  404. av_frame_free(&hwframe);
  405. return false;
  406. }
  407. static void set_visible(obs_properties_t *ppts, const char *name, bool visible)
  408. {
  409. obs_property_t *p = obs_properties_get(ppts, name);
  410. obs_property_set_visible(p, visible);
  411. }
  412. static void vaapi_defaults(obs_data_t *settings)
  413. {
  414. const char *device = vaapi_get_h264_default_device();
  415. obs_data_set_default_string(settings, "vaapi_device", device);
  416. obs_data_set_default_int(settings, "profile",
  417. FF_PROFILE_H264_CONSTRAINED_BASELINE);
  418. obs_data_set_default_int(settings, "level", 40);
  419. obs_data_set_default_int(settings, "bitrate", 2500);
  420. obs_data_set_default_int(settings, "keyint_sec", 0);
  421. obs_data_set_default_int(settings, "bf", 0);
  422. obs_data_set_default_int(settings, "rendermode", 0);
  423. obs_data_set_default_int(settings, "qp", 20);
  424. obs_data_set_default_int(settings, "maxrate", 0);
  425. int drm_fd = -1;
  426. VADisplay va_dpy = vaapi_open_device(&drm_fd, device, "vaapi_defaults");
  427. if (!va_dpy)
  428. return;
  429. if (vaapi_device_rc_supported(VAProfileH264ConstrainedBaseline, va_dpy,
  430. VA_RC_CBR, device))
  431. obs_data_set_default_string(settings, "rate_control", "CBR");
  432. else if (vaapi_device_rc_supported(VAProfileH264ConstrainedBaseline,
  433. va_dpy, VA_RC_VBR, device))
  434. obs_data_set_default_string(settings, "rate_control", "VBR");
  435. else
  436. obs_data_set_default_string(settings, "rate_control", "CQP");
  437. vaapi_close_device(&drm_fd, va_dpy);
  438. }
  439. static bool vaapi_device_modified(obs_properties_t *ppts, obs_property_t *p,
  440. obs_data_t *settings)
  441. {
  442. UNUSED_PARAMETER(p);
  443. const char *device = obs_data_get_string(settings, "vaapi_device");
  444. int drm_fd = -1;
  445. VADisplay va_dpy =
  446. vaapi_open_device(&drm_fd, device, "vaapi_device_modified");
  447. int profile = obs_data_get_int(settings, "profile");
  448. obs_property_t *rc_p = obs_properties_get(ppts, "rate_control");
  449. obs_property_list_clear(rc_p);
  450. if (!va_dpy || !vaapi_display_h264_supported(va_dpy, device))
  451. goto fail;
  452. switch (profile) {
  453. case FF_PROFILE_H264_CONSTRAINED_BASELINE:
  454. profile = VAProfileH264ConstrainedBaseline;
  455. break;
  456. case FF_PROFILE_H264_MAIN:
  457. profile = VAProfileH264Main;
  458. break;
  459. case FF_PROFILE_H264_HIGH:
  460. profile = VAProfileH264High;
  461. break;
  462. }
  463. if (vaapi_device_rc_supported(profile, va_dpy, VA_RC_CBR, device))
  464. obs_property_list_add_string(rc_p, "CBR (default)", "CBR");
  465. if (vaapi_device_rc_supported(profile, va_dpy, VA_RC_VBR, device))
  466. obs_property_list_add_string(rc_p, "VBR", "VBR");
  467. if (vaapi_device_rc_supported(profile, va_dpy, VA_RC_CQP, device))
  468. obs_property_list_add_string(rc_p, "CQP", "CQP");
  469. fail:
  470. vaapi_close_device(&drm_fd, va_dpy);
  471. return true;
  472. }
  473. static bool rate_control_modified(obs_properties_t *ppts, obs_property_t *p,
  474. obs_data_t *settings)
  475. {
  476. UNUSED_PARAMETER(p);
  477. const char *rate_control =
  478. obs_data_get_string(settings, "rate_control");
  479. const rc_mode_t *rc_mode = get_rc_mode(rate_control);
  480. /* Set options visibility per Rate Control */
  481. set_visible(ppts, "qp", rc_mode->qp);
  482. set_visible(ppts, "bitrate", rc_mode->bitrate);
  483. set_visible(ppts, "maxrate", rc_mode->maxrate);
  484. return true;
  485. }
  486. static bool get_device_name_from_pci(struct pci_access *pacc, char *pci_slot,
  487. char *buf, int size)
  488. {
  489. struct pci_filter filter;
  490. struct pci_dev *dev;
  491. char *name;
  492. pci_filter_init(pacc, &filter);
  493. if (pci_filter_parse_slot(&filter, pci_slot))
  494. return false;
  495. pci_scan_bus(pacc);
  496. for (dev = pacc->devices; dev; dev = dev->next) {
  497. if (pci_filter_match(&filter, dev)) {
  498. pci_fill_info(dev, PCI_FILL_IDENT);
  499. name = pci_lookup_name(pacc, buf, size,
  500. PCI_LOOKUP_DEVICE,
  501. dev->vendor_id, dev->device_id);
  502. strcpy(buf, name);
  503. return true;
  504. }
  505. }
  506. return false;
  507. }
  508. static obs_properties_t *vaapi_properties(void *unused)
  509. {
  510. UNUSED_PARAMETER(unused);
  511. obs_properties_t *props = obs_properties_create();
  512. obs_property_t *list;
  513. list = obs_properties_add_list(props, "vaapi_device",
  514. obs_module_text("VAAPI.Device"),
  515. OBS_COMBO_TYPE_LIST,
  516. OBS_COMBO_FORMAT_STRING);
  517. if (os_file_exists("/dev/dri/by-path")) {
  518. os_dir_t *by_path_dir = os_opendir("/dev/dri/by-path");
  519. struct pci_access *pacc = pci_alloc();
  520. struct os_dirent *file;
  521. char namebuf[1024];
  522. char pci_slot[13];
  523. char *type;
  524. pci_init(pacc);
  525. while ((file = os_readdir(by_path_dir)) != NULL) {
  526. // file_name pattern: pci-<pci_slot::12>-<type::{"card","render"}>
  527. char *file_name = file->d_name;
  528. if (strcmp(file_name, ".") == 0 ||
  529. strcmp(file_name, "..") == 0)
  530. continue;
  531. char path[64] = {0};
  532. // Use the return value of snprintf to prevent truncation warning.
  533. int written = snprintf(path, 64, "/dev/dri/by-path/%s",
  534. file_name);
  535. if (written >= 64)
  536. blog(LOG_DEBUG,
  537. "obs-ffmpeg-vaapi: A format truncation may have occurred."
  538. " This can be ignored since it is quite improbable.");
  539. type = strrchr(file_name, '-');
  540. if (type == NULL)
  541. continue;
  542. else
  543. type++;
  544. if (strcmp(type, "render") == 0) {
  545. strncpy(pci_slot, file_name + 4, 12);
  546. pci_slot[12] = 0;
  547. bool name_found = get_device_name_from_pci(
  548. pacc, pci_slot, namebuf,
  549. sizeof(namebuf));
  550. if (!vaapi_device_h264_supported(path))
  551. continue;
  552. if (!name_found)
  553. obs_property_list_add_string(list, path,
  554. path);
  555. else
  556. obs_property_list_add_string(
  557. list, namebuf, path);
  558. }
  559. }
  560. pci_cleanup(pacc);
  561. os_closedir(by_path_dir);
  562. }
  563. if (obs_property_list_item_count(list) == 0) {
  564. char path[32];
  565. for (int i = 28;; i++) {
  566. snprintf(path, sizeof(path), "/dev/dri/renderD1%d", i);
  567. if (access(path, F_OK) == 0) {
  568. char card[128];
  569. int ret = snprintf(card, sizeof(card),
  570. "Card%d: %s", i - 28, path);
  571. if (ret >= sizeof(card))
  572. blog(LOG_DEBUG,
  573. "obs-ffmpeg-vaapi: A format truncation may have occurred."
  574. " This can be ignored since it is quite improbable.");
  575. if (!vaapi_device_h264_supported(path))
  576. continue;
  577. obs_property_list_add_string(list, card, path);
  578. } else {
  579. break;
  580. }
  581. }
  582. }
  583. obs_property_set_modified_callback(list, vaapi_device_modified);
  584. list = obs_properties_add_list(props, "profile",
  585. obs_module_text("Profile"),
  586. OBS_COMBO_TYPE_LIST,
  587. OBS_COMBO_FORMAT_INT);
  588. obs_property_list_add_int(list, "Constrained Baseline (default)",
  589. FF_PROFILE_H264_CONSTRAINED_BASELINE);
  590. obs_property_list_add_int(list, "Main", FF_PROFILE_H264_MAIN);
  591. obs_property_list_add_int(list, "High", FF_PROFILE_H264_HIGH);
  592. obs_property_set_modified_callback(list, vaapi_device_modified);
  593. list = obs_properties_add_list(props, "level", obs_module_text("Level"),
  594. OBS_COMBO_TYPE_LIST,
  595. OBS_COMBO_FORMAT_INT);
  596. obs_property_list_add_int(list, "Auto", FF_LEVEL_UNKNOWN);
  597. obs_property_list_add_int(list, "3.0", 30);
  598. obs_property_list_add_int(list, "3.1", 31);
  599. obs_property_list_add_int(list, "4.0 (default) (Compatibility mode)",
  600. 40);
  601. obs_property_list_add_int(list, "4.1", 41);
  602. obs_property_list_add_int(list, "4.2", 42);
  603. obs_property_list_add_int(list, "5.0", 50);
  604. obs_property_list_add_int(list, "5.1", 51);
  605. obs_property_list_add_int(list, "5.2", 52);
  606. list = obs_properties_add_list(props, "rate_control",
  607. obs_module_text("RateControl"),
  608. OBS_COMBO_TYPE_LIST,
  609. OBS_COMBO_FORMAT_STRING);
  610. obs_property_set_modified_callback(list, rate_control_modified);
  611. obs_property_t *p;
  612. p = obs_properties_add_int(props, "bitrate", obs_module_text("Bitrate"),
  613. 0, 300000, 50);
  614. obs_property_int_set_suffix(p, " Kbps");
  615. p = obs_properties_add_int(
  616. props, "maxrate", obs_module_text("MaxBitrate"), 0, 300000, 50);
  617. obs_property_int_set_suffix(p, " Kbps");
  618. obs_properties_add_int(props, "qp", "QP", 0, 51, 1);
  619. p = obs_properties_add_int(props, "keyint_sec",
  620. obs_module_text("KeyframeIntervalSec"), 0,
  621. 20, 1);
  622. obs_property_int_set_suffix(p, " s");
  623. return props;
  624. }
  625. static bool vaapi_extra_data(void *data, uint8_t **extra_data, size_t *size)
  626. {
  627. struct vaapi_encoder *enc = data;
  628. *extra_data = enc->header;
  629. *size = enc->header_size;
  630. return true;
  631. }
  632. static bool vaapi_sei_data(void *data, uint8_t **extra_data, size_t *size)
  633. {
  634. struct vaapi_encoder *enc = data;
  635. *extra_data = enc->sei;
  636. *size = enc->sei_size;
  637. return true;
  638. }
  639. struct obs_encoder_info vaapi_encoder_info = {
  640. .id = "ffmpeg_vaapi",
  641. .type = OBS_ENCODER_VIDEO,
  642. .codec = "h264",
  643. .get_name = vaapi_getname,
  644. .create = vaapi_create,
  645. .destroy = vaapi_destroy,
  646. .encode = vaapi_encode,
  647. .get_defaults = vaapi_defaults,
  648. .get_properties = vaapi_properties,
  649. .get_extra_data = vaapi_extra_data,
  650. .get_sei_data = vaapi_sei_data,
  651. .get_video_info = vaapi_video_info,
  652. };
  653. #endif