obs-ffmpeg-vaapi.c 22 KB

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