1
0

obs-ffmpeg-vaapi.c 22 KB

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