obs-ffmpeg-vaapi.c 22 KB

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