obs-ffmpeg-vaapi.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  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. #ifdef ENABLE_HEVC
  24. #include <obs-hevc.h>
  25. #endif
  26. #include <unistd.h>
  27. #include <libavutil/opt.h>
  28. #include <libavutil/pixdesc.h>
  29. #include <libavutil/hwcontext.h>
  30. #include <libavcodec/avcodec.h>
  31. #include <libavformat/avformat.h>
  32. #include <libavfilter/avfilter.h>
  33. #include <pci/pci.h>
  34. #include "vaapi-utils.h"
  35. #include "obs-ffmpeg-formats.h"
  36. #define do_log(level, format, ...) \
  37. blog(level, "[FFmpeg VAAPI encoder: '%s'] " format, \
  38. obs_encoder_get_name(enc->encoder), ##__VA_ARGS__)
  39. #define warn(format, ...) do_log(LOG_WARNING, format, ##__VA_ARGS__)
  40. #define info(format, ...) do_log(LOG_INFO, format, ##__VA_ARGS__)
  41. #define debug(format, ...) do_log(LOG_DEBUG, format, ##__VA_ARGS__)
  42. struct vaapi_encoder {
  43. obs_encoder_t *encoder;
  44. AVBufferRef *vadevice_ref;
  45. AVBufferRef *vaframes_ref;
  46. const AVCodec *vaapi;
  47. AVCodecContext *context;
  48. AVPacket *packet;
  49. AVFrame *vframe;
  50. DARRAY(uint8_t) buffer;
  51. uint8_t *header;
  52. size_t header_size;
  53. uint8_t *sei;
  54. size_t sei_size;
  55. int height;
  56. bool first_packet;
  57. bool initialized;
  58. };
  59. static const char *h264_vaapi_getname(void *unused)
  60. {
  61. UNUSED_PARAMETER(unused);
  62. return "FFmpeg VAAPI H.264";
  63. }
  64. #ifdef ENABLE_HEVC
  65. static const char *hevc_vaapi_getname(void *unused)
  66. {
  67. UNUSED_PARAMETER(unused);
  68. return "FFmpeg VAAPI HEVC";
  69. }
  70. #endif
  71. static inline bool h264_valid_format(enum video_format format)
  72. {
  73. return format == VIDEO_FORMAT_NV12;
  74. }
  75. #ifdef ENABLE_HEVC
  76. static inline bool hevc_valid_format(enum video_format format)
  77. {
  78. return (format == VIDEO_FORMAT_NV12) || (format == VIDEO_FORMAT_P010);
  79. }
  80. #endif
  81. static void h264_vaapi_video_info(void *data, struct video_scale_info *info)
  82. {
  83. struct vaapi_encoder *enc = data;
  84. enum video_format pref_format;
  85. pref_format = obs_encoder_get_preferred_video_format(enc->encoder);
  86. if (!h264_valid_format(pref_format)) {
  87. pref_format = h264_valid_format(info->format)
  88. ? info->format
  89. : VIDEO_FORMAT_NV12;
  90. }
  91. info->format = pref_format;
  92. }
  93. #ifdef ENABLE_HEVC
  94. static void hevc_vaapi_video_info(void *data, struct video_scale_info *info)
  95. {
  96. struct vaapi_encoder *enc = data;
  97. enum video_format pref_format;
  98. pref_format = obs_encoder_get_preferred_video_format(enc->encoder);
  99. if (!hevc_valid_format(pref_format)) {
  100. pref_format = hevc_valid_format(info->format)
  101. ? info->format
  102. : VIDEO_FORMAT_NV12;
  103. }
  104. info->format = pref_format;
  105. }
  106. #endif
  107. static bool vaapi_init_codec(struct vaapi_encoder *enc, const char *path)
  108. {
  109. int ret;
  110. ret = av_hwdevice_ctx_create(&enc->vadevice_ref, AV_HWDEVICE_TYPE_VAAPI,
  111. path, NULL, 0);
  112. if (ret < 0) {
  113. warn("Failed to create VAAPI device context: %s",
  114. av_err2str(ret));
  115. return false;
  116. }
  117. enc->vaframes_ref = av_hwframe_ctx_alloc(enc->vadevice_ref);
  118. if (!enc->vaframes_ref) {
  119. warn("Failed to alloc HW frames context");
  120. return false;
  121. }
  122. AVHWFramesContext *frames_ctx =
  123. (AVHWFramesContext *)enc->vaframes_ref->data;
  124. frames_ctx->format = AV_PIX_FMT_VAAPI;
  125. frames_ctx->sw_format = enc->context->pix_fmt;
  126. frames_ctx->width = enc->context->width;
  127. frames_ctx->height = enc->context->height;
  128. frames_ctx->initial_pool_size = 20;
  129. ret = av_hwframe_ctx_init(enc->vaframes_ref);
  130. if (ret < 0) {
  131. warn("Failed to init HW frames context: %s", av_err2str(ret));
  132. return false;
  133. }
  134. /* 2. Create software frame and picture */
  135. enc->vframe = av_frame_alloc();
  136. if (!enc->vframe) {
  137. warn("Failed to allocate video frame");
  138. return false;
  139. }
  140. enc->vframe->format = enc->context->pix_fmt;
  141. enc->vframe->width = enc->context->width;
  142. enc->vframe->height = enc->context->height;
  143. enc->vframe->colorspace = enc->context->colorspace;
  144. enc->vframe->color_range = enc->context->color_range;
  145. enc->vframe->chroma_location = enc->context->chroma_sample_location;
  146. ret = av_frame_get_buffer(enc->vframe, base_get_alignment());
  147. if (ret < 0) {
  148. warn("Failed to allocate vframe: %s", av_err2str(ret));
  149. return false;
  150. }
  151. /* 3. set up codec */
  152. enc->context->pix_fmt = AV_PIX_FMT_VAAPI;
  153. enc->context->hw_frames_ctx = av_buffer_ref(enc->vaframes_ref);
  154. ret = avcodec_open2(enc->context, enc->vaapi, NULL);
  155. if (ret < 0) {
  156. warn("Failed to open VAAPI codec: %s", av_err2str(ret));
  157. return false;
  158. }
  159. enc->packet = av_packet_alloc();
  160. enc->initialized = true;
  161. return true;
  162. }
  163. /* "Allowed" options per Rate Control
  164. * See FFMPEG libavcodec/vaapi_encode.c (vaapi_encode_rc_modes)
  165. */
  166. typedef struct {
  167. const char *name;
  168. bool qp;
  169. bool bitrate;
  170. bool maxrate;
  171. } rc_mode_t;
  172. static const rc_mode_t *get_rc_mode(const char *name)
  173. {
  174. /* Set "allowed" options per Rate Control */
  175. static const rc_mode_t RC_MODES[] = {
  176. {.name = "CBR", .qp = false, .bitrate = true, .maxrate = false},
  177. {.name = "CQP", .qp = true, .bitrate = false, .maxrate = false},
  178. {.name = "VBR", .qp = false, .bitrate = true, .maxrate = true},
  179. {0}};
  180. const rc_mode_t *rc_mode = RC_MODES;
  181. while (!!rc_mode->name && strcmp(rc_mode->name, name) != 0)
  182. rc_mode++;
  183. return rc_mode ? rc_mode : RC_MODES;
  184. }
  185. static bool vaapi_update(void *data, obs_data_t *settings, bool hevc)
  186. {
  187. struct vaapi_encoder *enc = data;
  188. const char *device = obs_data_get_string(settings, "vaapi_device");
  189. const char *rate_control =
  190. obs_data_get_string(settings, "rate_control");
  191. const rc_mode_t *rc_mode = get_rc_mode(rate_control);
  192. bool cbr = strcmp(rc_mode->name, "CBR") == 0;
  193. int profile = (int)obs_data_get_int(settings, "profile");
  194. int bf = (int)obs_data_get_int(settings, "bf");
  195. int qp = rc_mode->qp ? (int)obs_data_get_int(settings, "qp") : 0;
  196. av_opt_set_int(enc->context->priv_data, "qp", qp, 0);
  197. int level = (int)obs_data_get_int(settings, "level");
  198. int bitrate = rc_mode->bitrate
  199. ? (int)obs_data_get_int(settings, "bitrate")
  200. : 0;
  201. int maxrate = rc_mode->maxrate
  202. ? (int)obs_data_get_int(settings, "maxrate")
  203. : 0;
  204. int keyint_sec = (int)obs_data_get_int(settings, "keyint_sec");
  205. /* For Rate Control which allows maxrate, FFMPEG will give
  206. * an error if maxrate > bitrate. To prevent that set maxrate
  207. * to 0.
  208. * For CBR, maxrate = bitrate
  209. */
  210. if (cbr)
  211. maxrate = bitrate;
  212. else if (rc_mode->maxrate && maxrate && maxrate < bitrate)
  213. maxrate = 0;
  214. video_t *video = obs_encoder_video(enc->encoder);
  215. const struct video_output_info *voi = video_output_get_info(video);
  216. struct video_scale_info info;
  217. info.format = voi->format;
  218. info.colorspace = voi->colorspace;
  219. info.range = voi->range;
  220. #ifdef ENABLE_HEVC
  221. if (hevc) {
  222. if ((profile == FF_PROFILE_HEVC_MAIN) &&
  223. (info.format == VIDEO_FORMAT_P010)) {
  224. warn("Forcing Main10 for P010");
  225. profile = FF_PROFILE_HEVC_MAIN_10;
  226. }
  227. hevc_vaapi_video_info(enc, &info);
  228. } else
  229. #endif
  230. {
  231. h264_vaapi_video_info(enc, &info);
  232. }
  233. enc->context->profile = profile;
  234. enc->context->max_b_frames = bf;
  235. enc->context->level = level;
  236. enc->context->bit_rate = bitrate * 1000;
  237. enc->context->rc_max_rate = maxrate * 1000;
  238. enc->context->width = obs_encoder_get_width(enc->encoder);
  239. enc->context->height = obs_encoder_get_height(enc->encoder);
  240. enc->context->time_base = (AVRational){voi->fps_den, voi->fps_num};
  241. const enum AVPixelFormat pix_fmt =
  242. obs_to_ffmpeg_video_format(info.format);
  243. enc->context->pix_fmt = pix_fmt;
  244. enc->context->color_range = info.range == VIDEO_RANGE_FULL
  245. ? AVCOL_RANGE_JPEG
  246. : AVCOL_RANGE_MPEG;
  247. enum AVColorSpace colorspace = AVCOL_SPC_UNSPECIFIED;
  248. switch (info.colorspace) {
  249. case VIDEO_CS_601:
  250. enc->context->color_primaries = AVCOL_PRI_SMPTE170M;
  251. enc->context->color_trc = AVCOL_TRC_SMPTE170M;
  252. colorspace = AVCOL_SPC_SMPTE170M;
  253. break;
  254. case VIDEO_CS_DEFAULT:
  255. case VIDEO_CS_709:
  256. enc->context->color_primaries = AVCOL_PRI_BT709;
  257. enc->context->color_trc = AVCOL_TRC_BT709;
  258. colorspace = AVCOL_SPC_BT709;
  259. break;
  260. case VIDEO_CS_SRGB:
  261. enc->context->color_primaries = AVCOL_PRI_BT709;
  262. enc->context->color_trc = AVCOL_TRC_IEC61966_2_1;
  263. colorspace = AVCOL_SPC_BT709;
  264. break;
  265. case VIDEO_CS_2100_PQ:
  266. enc->context->color_primaries = AVCOL_PRI_BT2020;
  267. enc->context->color_trc = AVCOL_TRC_SMPTE2084;
  268. colorspace = AVCOL_SPC_BT2020_NCL;
  269. break;
  270. case VIDEO_CS_2100_HLG:
  271. enc->context->color_primaries = AVCOL_PRI_BT2020;
  272. enc->context->color_trc = AVCOL_TRC_ARIB_STD_B67;
  273. colorspace = AVCOL_SPC_BT2020_NCL;
  274. break;
  275. default:
  276. break;
  277. }
  278. enc->context->colorspace = colorspace;
  279. enc->context->chroma_sample_location =
  280. determine_chroma_location(pix_fmt, colorspace);
  281. if (keyint_sec > 0) {
  282. enc->context->gop_size =
  283. keyint_sec * voi->fps_num / voi->fps_den;
  284. } else {
  285. enc->context->gop_size = 120;
  286. }
  287. enc->height = enc->context->height;
  288. info("settings:\n"
  289. "\tdevice: %s\n"
  290. "\trate_control: %s\n"
  291. "\tprofile: %d\n"
  292. "\tlevel: %d\n"
  293. "\tqp: %d\n"
  294. "\tbitrate: %d\n"
  295. "\tmaxrate: %d\n"
  296. "\tkeyint: %d\n"
  297. "\twidth: %d\n"
  298. "\theight: %d\n"
  299. "\tb-frames: %d\n",
  300. device, rate_control, profile, level, qp, bitrate, maxrate,
  301. enc->context->gop_size, enc->context->width, enc->context->height,
  302. enc->context->max_b_frames);
  303. return vaapi_init_codec(enc, device);
  304. }
  305. static inline void flush_remaining_packets(struct vaapi_encoder *enc)
  306. {
  307. int r_pkt = 1;
  308. while (r_pkt) {
  309. #if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(57, 40, 101)
  310. if (avcodec_receive_packet(enc->context, enc->packet) < 0)
  311. break;
  312. #else
  313. if (avcodec_encode_video2(enc->context, enc->packet, NULL,
  314. &r_pkt) < 0)
  315. break;
  316. #endif
  317. if (r_pkt)
  318. av_packet_unref(enc->packet);
  319. }
  320. }
  321. static void vaapi_destroy(void *data)
  322. {
  323. struct vaapi_encoder *enc = data;
  324. if (enc->initialized)
  325. flush_remaining_packets(enc);
  326. av_packet_free(&enc->packet);
  327. avcodec_free_context(&enc->context);
  328. av_frame_unref(enc->vframe);
  329. av_frame_free(&enc->vframe);
  330. av_buffer_unref(&enc->vaframes_ref);
  331. av_buffer_unref(&enc->vadevice_ref);
  332. da_free(enc->buffer);
  333. bfree(enc->header);
  334. bfree(enc->sei);
  335. bfree(enc);
  336. }
  337. static void *vaapi_create_internal(obs_data_t *settings, obs_encoder_t *encoder,
  338. bool hevc)
  339. {
  340. struct vaapi_encoder *enc;
  341. #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 9, 100)
  342. avcodec_register_all();
  343. #endif
  344. enc = bzalloc(sizeof(*enc));
  345. enc->encoder = encoder;
  346. const char *const name = hevc ? "hevc_vaapi" : "h264_vaapi";
  347. enc->vaapi = avcodec_find_encoder_by_name(name);
  348. enc->first_packet = true;
  349. blog(LOG_INFO, "---------------------------------");
  350. if (!enc->vaapi) {
  351. warn("Couldn't find encoder");
  352. goto fail;
  353. }
  354. enc->context = avcodec_alloc_context3(enc->vaapi);
  355. if (!enc->context) {
  356. warn("Failed to create codec context");
  357. goto fail;
  358. }
  359. if (!vaapi_update(enc, settings, hevc))
  360. goto fail;
  361. return enc;
  362. fail:
  363. vaapi_destroy(enc);
  364. return NULL;
  365. }
  366. static void *h264_vaapi_create(obs_data_t *settings, obs_encoder_t *encoder)
  367. {
  368. return vaapi_create_internal(settings, encoder, false);
  369. }
  370. #ifdef ENABLE_HEVC
  371. static void *hevc_vaapi_create(obs_data_t *settings, obs_encoder_t *encoder)
  372. {
  373. return vaapi_create_internal(settings, encoder, true);
  374. }
  375. #endif
  376. static inline void copy_data(AVFrame *pic, const struct encoder_frame *frame,
  377. int height, enum AVPixelFormat format)
  378. {
  379. int h_chroma_shift, v_chroma_shift;
  380. av_pix_fmt_get_chroma_sub_sample(format, &h_chroma_shift,
  381. &v_chroma_shift);
  382. for (int plane = 0; plane < MAX_AV_PLANES; plane++) {
  383. if (!frame->data[plane])
  384. continue;
  385. int frame_rowsize = (int)frame->linesize[plane];
  386. int pic_rowsize = pic->linesize[plane];
  387. int bytes = frame_rowsize < pic_rowsize ? frame_rowsize
  388. : pic_rowsize;
  389. int plane_height = height >> (plane ? v_chroma_shift : 0);
  390. for (int y = 0; y < plane_height; y++) {
  391. int pos_frame = y * frame_rowsize;
  392. int pos_pic = y * pic_rowsize;
  393. memcpy(pic->data[plane] + pos_pic,
  394. frame->data[plane] + pos_frame, bytes);
  395. }
  396. }
  397. }
  398. static bool vaapi_encode_internal(void *data, struct encoder_frame *frame,
  399. struct encoder_packet *packet,
  400. bool *received_packet, bool hevc)
  401. {
  402. struct vaapi_encoder *enc = data;
  403. AVFrame *hwframe = NULL;
  404. int got_packet;
  405. int ret;
  406. hwframe = av_frame_alloc();
  407. if (!hwframe) {
  408. warn("vaapi_encode: failed to allocate hw frame");
  409. return false;
  410. }
  411. ret = av_hwframe_get_buffer(enc->vaframes_ref, hwframe, 0);
  412. if (ret < 0) {
  413. warn("vaapi_encode: failed to get buffer for hw frame: %s",
  414. av_err2str(ret));
  415. goto fail;
  416. }
  417. copy_data(enc->vframe, frame, enc->height, enc->context->pix_fmt);
  418. enc->vframe->pts = frame->pts;
  419. hwframe->pts = frame->pts;
  420. hwframe->width = enc->vframe->width;
  421. hwframe->height = enc->vframe->height;
  422. ret = av_hwframe_transfer_data(hwframe, enc->vframe, 0);
  423. if (ret < 0) {
  424. warn("vaapi_encode: failed to upload hw frame: %s",
  425. av_err2str(ret));
  426. goto fail;
  427. }
  428. ret = av_frame_copy_props(hwframe, enc->vframe);
  429. if (ret < 0) {
  430. warn("vaapi_encode: failed to copy props to hw frame: %s",
  431. av_err2str(ret));
  432. goto fail;
  433. }
  434. #if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(57, 40, 101)
  435. ret = avcodec_send_frame(enc->context, hwframe);
  436. if (ret == 0)
  437. ret = avcodec_receive_packet(enc->context, enc->packet);
  438. got_packet = (ret == 0);
  439. if (ret == AVERROR_EOF || ret == AVERROR(EAGAIN))
  440. ret = 0;
  441. #else
  442. ret = avcodec_encode_video2(enc->context, enc->packet, hwframe,
  443. &got_packet);
  444. #endif
  445. if (ret < 0) {
  446. warn("vaapi_encode: Error encoding: %s", av_err2str(ret));
  447. goto fail;
  448. }
  449. if (got_packet && enc->packet->size) {
  450. if (enc->first_packet) {
  451. uint8_t *new_packet;
  452. size_t size;
  453. enc->first_packet = false;
  454. #ifdef ENABLE_HEVC
  455. if (hevc) {
  456. obs_extract_hevc_headers(
  457. enc->packet->data, enc->packet->size,
  458. &new_packet, &size, &enc->header,
  459. &enc->header_size, &enc->sei,
  460. &enc->sei_size);
  461. } else
  462. #endif
  463. {
  464. obs_extract_avc_headers(
  465. enc->packet->data, enc->packet->size,
  466. &new_packet, &size, &enc->header,
  467. &enc->header_size, &enc->sei,
  468. &enc->sei_size);
  469. }
  470. da_copy_array(enc->buffer, new_packet, size);
  471. bfree(new_packet);
  472. } else {
  473. da_copy_array(enc->buffer, enc->packet->data,
  474. enc->packet->size);
  475. }
  476. packet->pts = enc->packet->pts;
  477. packet->dts = enc->packet->dts;
  478. packet->data = enc->buffer.array;
  479. packet->size = enc->buffer.num;
  480. packet->type = OBS_ENCODER_VIDEO;
  481. #ifdef ENABLE_HEVC
  482. if (hevc) {
  483. packet->keyframe =
  484. obs_hevc_keyframe(packet->data, packet->size);
  485. } else
  486. #endif
  487. {
  488. packet->keyframe =
  489. obs_avc_keyframe(packet->data, packet->size);
  490. }
  491. *received_packet = true;
  492. } else {
  493. *received_packet = false;
  494. }
  495. av_packet_unref(enc->packet);
  496. av_frame_free(&hwframe);
  497. return true;
  498. fail:
  499. av_frame_free(&hwframe);
  500. return false;
  501. }
  502. static bool h264_vaapi_encode(void *data, struct encoder_frame *frame,
  503. struct encoder_packet *packet,
  504. bool *received_packet)
  505. {
  506. return vaapi_encode_internal(data, frame, packet, received_packet,
  507. false);
  508. }
  509. #ifdef ENABLE_HEVC
  510. static bool hevc_vaapi_encode(void *data, struct encoder_frame *frame,
  511. struct encoder_packet *packet,
  512. bool *received_packet)
  513. {
  514. return vaapi_encode_internal(data, frame, packet, received_packet,
  515. true);
  516. }
  517. #endif
  518. static void set_visible(obs_properties_t *ppts, const char *name, bool visible)
  519. {
  520. obs_property_t *p = obs_properties_get(ppts, name);
  521. obs_property_set_visible(p, visible);
  522. }
  523. static void vaapi_defaults_internal(obs_data_t *settings, bool hevc)
  524. {
  525. #ifdef ENABLE_HEVC
  526. const char *device = hevc ? vaapi_get_hevc_default_device()
  527. : vaapi_get_h264_default_device();
  528. #else
  529. const char *const device = vaapi_get_h264_default_device();
  530. #endif
  531. obs_data_set_default_string(settings, "vaapi_device", device);
  532. #ifdef ENABLE_HEVC
  533. if (hevc) {
  534. obs_data_set_default_int(settings, "profile",
  535. FF_PROFILE_HEVC_MAIN);
  536. } else
  537. #endif
  538. {
  539. obs_data_set_default_int(settings, "profile",
  540. FF_PROFILE_H264_CONSTRAINED_BASELINE);
  541. }
  542. obs_data_set_default_int(settings, "level", 40);
  543. obs_data_set_default_int(settings, "bitrate", 2500);
  544. obs_data_set_default_int(settings, "keyint_sec", 0);
  545. obs_data_set_default_int(settings, "bf", 0);
  546. obs_data_set_default_int(settings, "rendermode", 0);
  547. obs_data_set_default_int(settings, "qp", 20);
  548. obs_data_set_default_int(settings, "maxrate", 0);
  549. int drm_fd = -1;
  550. VADisplay va_dpy = vaapi_open_device(&drm_fd, device, "vaapi_defaults");
  551. if (!va_dpy)
  552. return;
  553. #ifdef ENABLE_HEVC
  554. const VAProfile profile = hevc ? VAProfileHEVCMain
  555. : VAProfileH264ConstrainedBaseline;
  556. #else
  557. const VAProfile profile = VAProfileH264ConstrainedBaseline;
  558. #endif
  559. if (vaapi_device_rc_supported(profile, va_dpy, VA_RC_CBR, device))
  560. obs_data_set_default_string(settings, "rate_control", "CBR");
  561. else if (vaapi_device_rc_supported(profile, va_dpy, VA_RC_VBR, device))
  562. obs_data_set_default_string(settings, "rate_control", "VBR");
  563. else
  564. obs_data_set_default_string(settings, "rate_control", "CQP");
  565. vaapi_close_device(&drm_fd, va_dpy);
  566. }
  567. static void h264_vaapi_defaults(obs_data_t *settings)
  568. {
  569. vaapi_defaults_internal(settings, false);
  570. }
  571. static void hevc_vaapi_defaults(obs_data_t *settings)
  572. {
  573. vaapi_defaults_internal(settings, true);
  574. }
  575. static bool vaapi_device_modified(obs_properties_t *ppts, obs_property_t *p,
  576. obs_data_t *settings)
  577. {
  578. UNUSED_PARAMETER(p);
  579. const char *device = obs_data_get_string(settings, "vaapi_device");
  580. int drm_fd = -1;
  581. VADisplay va_dpy =
  582. vaapi_open_device(&drm_fd, device, "vaapi_device_modified");
  583. int profile = obs_data_get_int(settings, "profile");
  584. obs_property_t *rc_p = obs_properties_get(ppts, "rate_control");
  585. obs_property_list_clear(rc_p);
  586. if (!va_dpy)
  587. goto fail;
  588. switch (profile) {
  589. case FF_PROFILE_H264_CONSTRAINED_BASELINE:
  590. if (!vaapi_display_h264_supported(va_dpy, device))
  591. goto fail;
  592. profile = VAProfileH264ConstrainedBaseline;
  593. break;
  594. case FF_PROFILE_H264_MAIN:
  595. if (!vaapi_display_h264_supported(va_dpy, device))
  596. goto fail;
  597. profile = VAProfileH264Main;
  598. break;
  599. case FF_PROFILE_H264_HIGH:
  600. if (!vaapi_display_h264_supported(va_dpy, device))
  601. goto fail;
  602. profile = VAProfileH264High;
  603. break;
  604. #ifdef ENABLE_HEVC
  605. case FF_PROFILE_HEVC_MAIN:
  606. if (!vaapi_display_hevc_supported(va_dpy, device))
  607. goto fail;
  608. profile = VAProfileHEVCMain;
  609. break;
  610. case FF_PROFILE_HEVC_MAIN_10:
  611. if (!vaapi_display_hevc_supported(va_dpy, device))
  612. goto fail;
  613. profile = VAProfileHEVCMain10;
  614. break;
  615. #endif
  616. }
  617. if (vaapi_device_rc_supported(profile, va_dpy, VA_RC_CBR, device))
  618. obs_property_list_add_string(rc_p, "CBR (default)", "CBR");
  619. if (vaapi_device_rc_supported(profile, va_dpy, VA_RC_VBR, device))
  620. obs_property_list_add_string(rc_p, "VBR", "VBR");
  621. if (vaapi_device_rc_supported(profile, va_dpy, VA_RC_CQP, device))
  622. obs_property_list_add_string(rc_p, "CQP", "CQP");
  623. fail:
  624. vaapi_close_device(&drm_fd, va_dpy);
  625. return true;
  626. }
  627. static bool rate_control_modified(obs_properties_t *ppts, obs_property_t *p,
  628. obs_data_t *settings)
  629. {
  630. UNUSED_PARAMETER(p);
  631. const char *rate_control =
  632. obs_data_get_string(settings, "rate_control");
  633. const rc_mode_t *rc_mode = get_rc_mode(rate_control);
  634. /* Set options visibility per Rate Control */
  635. set_visible(ppts, "qp", rc_mode->qp);
  636. set_visible(ppts, "bitrate", rc_mode->bitrate);
  637. set_visible(ppts, "maxrate", rc_mode->maxrate);
  638. return true;
  639. }
  640. static bool get_device_name_from_pci(struct pci_access *pacc, char *pci_slot,
  641. char *buf, int size)
  642. {
  643. struct pci_filter filter;
  644. struct pci_dev *dev;
  645. char *name;
  646. pci_filter_init(pacc, &filter);
  647. if (pci_filter_parse_slot(&filter, pci_slot))
  648. return false;
  649. pci_scan_bus(pacc);
  650. for (dev = pacc->devices; dev; dev = dev->next) {
  651. if (pci_filter_match(&filter, dev)) {
  652. pci_fill_info(dev, PCI_FILL_IDENT);
  653. name = pci_lookup_name(pacc, buf, size,
  654. PCI_LOOKUP_DEVICE,
  655. dev->vendor_id, dev->device_id);
  656. strcpy(buf, name);
  657. return true;
  658. }
  659. }
  660. return false;
  661. }
  662. static obs_properties_t *vaapi_properties_internal(bool hevc)
  663. {
  664. obs_properties_t *props = obs_properties_create();
  665. obs_property_t *list;
  666. list = obs_properties_add_list(props, "vaapi_device",
  667. obs_module_text("VAAPI.Device"),
  668. OBS_COMBO_TYPE_LIST,
  669. OBS_COMBO_FORMAT_STRING);
  670. if (os_file_exists("/dev/dri/by-path")) {
  671. os_dir_t *by_path_dir = os_opendir("/dev/dri/by-path");
  672. struct pci_access *pacc = pci_alloc();
  673. struct os_dirent *file;
  674. char namebuf[1024];
  675. char pci_slot[13];
  676. char *type;
  677. pci_init(pacc);
  678. while ((file = os_readdir(by_path_dir)) != NULL) {
  679. // file_name pattern: pci-<pci_slot::12>-<type::{"card","render"}>
  680. char *file_name = file->d_name;
  681. if (strcmp(file_name, ".") == 0 ||
  682. strcmp(file_name, "..") == 0)
  683. continue;
  684. char path[64] = {0};
  685. // Use the return value of snprintf to prevent truncation warning.
  686. int written = snprintf(path, 64, "/dev/dri/by-path/%s",
  687. file_name);
  688. if (written >= 64)
  689. blog(LOG_DEBUG,
  690. "obs-ffmpeg-vaapi: A format truncation may have occurred."
  691. " This can be ignored since it is quite improbable.");
  692. type = strrchr(file_name, '-');
  693. if (type == NULL)
  694. continue;
  695. else
  696. type++;
  697. if (strcmp(type, "render") == 0) {
  698. strncpy(pci_slot, file_name + 4, 12);
  699. pci_slot[12] = 0;
  700. bool name_found = get_device_name_from_pci(
  701. pacc, pci_slot, namebuf,
  702. sizeof(namebuf));
  703. if (!vaapi_device_h264_supported(path))
  704. continue;
  705. if (!name_found)
  706. obs_property_list_add_string(list, path,
  707. path);
  708. else
  709. obs_property_list_add_string(
  710. list, namebuf, path);
  711. }
  712. }
  713. pci_cleanup(pacc);
  714. os_closedir(by_path_dir);
  715. }
  716. if (obs_property_list_item_count(list) == 0) {
  717. char path[32];
  718. for (int i = 28;; i++) {
  719. snprintf(path, sizeof(path), "/dev/dri/renderD1%d", i);
  720. if (access(path, F_OK) == 0) {
  721. char card[128];
  722. int ret = snprintf(card, sizeof(card),
  723. "Card%d: %s", i - 28, path);
  724. if (ret >= (int)sizeof(card))
  725. blog(LOG_DEBUG,
  726. "obs-ffmpeg-vaapi: A format truncation may have occurred."
  727. " This can be ignored since it is quite improbable.");
  728. if (!vaapi_device_h264_supported(path))
  729. continue;
  730. obs_property_list_add_string(list, card, path);
  731. } else {
  732. break;
  733. }
  734. }
  735. }
  736. obs_property_set_modified_callback(list, vaapi_device_modified);
  737. list = obs_properties_add_list(props, "profile",
  738. obs_module_text("Profile"),
  739. OBS_COMBO_TYPE_LIST,
  740. OBS_COMBO_FORMAT_INT);
  741. if (hevc) {
  742. obs_property_list_add_int(list, "Main", FF_PROFILE_HEVC_MAIN);
  743. obs_property_list_add_int(list, "Main10",
  744. FF_PROFILE_HEVC_MAIN_10);
  745. } else {
  746. obs_property_list_add_int(list,
  747. "Constrained Baseline (default)",
  748. FF_PROFILE_H264_CONSTRAINED_BASELINE);
  749. obs_property_list_add_int(list, "Main", FF_PROFILE_H264_MAIN);
  750. obs_property_list_add_int(list, "High", FF_PROFILE_H264_HIGH);
  751. }
  752. obs_property_set_modified_callback(list, vaapi_device_modified);
  753. list = obs_properties_add_list(props, "level", obs_module_text("Level"),
  754. OBS_COMBO_TYPE_LIST,
  755. OBS_COMBO_FORMAT_INT);
  756. obs_property_list_add_int(list, "Auto", FF_LEVEL_UNKNOWN);
  757. obs_property_list_add_int(list, "3.0", 30);
  758. obs_property_list_add_int(list, "3.1", 31);
  759. obs_property_list_add_int(list, "4.0 (default) (Compatibility mode)",
  760. 40);
  761. obs_property_list_add_int(list, "4.1", 41);
  762. obs_property_list_add_int(list, "4.2", 42);
  763. obs_property_list_add_int(list, "5.0", 50);
  764. obs_property_list_add_int(list, "5.1", 51);
  765. obs_property_list_add_int(list, "5.2", 52);
  766. list = obs_properties_add_list(props, "rate_control",
  767. obs_module_text("RateControl"),
  768. OBS_COMBO_TYPE_LIST,
  769. OBS_COMBO_FORMAT_STRING);
  770. obs_property_set_modified_callback(list, rate_control_modified);
  771. obs_property_t *p;
  772. p = obs_properties_add_int(props, "bitrate", obs_module_text("Bitrate"),
  773. 0, 300000, 50);
  774. obs_property_int_set_suffix(p, " Kbps");
  775. p = obs_properties_add_int(
  776. props, "maxrate", obs_module_text("MaxBitrate"), 0, 300000, 50);
  777. obs_property_int_set_suffix(p, " Kbps");
  778. obs_properties_add_int(props, "qp", "QP", 0, 51, 1);
  779. p = obs_properties_add_int(props, "keyint_sec",
  780. obs_module_text("KeyframeIntervalSec"), 0,
  781. 20, 1);
  782. obs_property_int_set_suffix(p, " s");
  783. return props;
  784. }
  785. static obs_properties_t *h264_vaapi_properties(void *unused)
  786. {
  787. UNUSED_PARAMETER(unused);
  788. return vaapi_properties_internal(false);
  789. }
  790. #ifdef ENABLE_HEVC
  791. static obs_properties_t *hevc_vaapi_properties(void *unused)
  792. {
  793. UNUSED_PARAMETER(unused);
  794. return vaapi_properties_internal(true);
  795. }
  796. #endif
  797. static bool vaapi_extra_data(void *data, uint8_t **extra_data, size_t *size)
  798. {
  799. struct vaapi_encoder *enc = data;
  800. *extra_data = enc->header;
  801. *size = enc->header_size;
  802. return true;
  803. }
  804. static bool vaapi_sei_data(void *data, uint8_t **extra_data, size_t *size)
  805. {
  806. struct vaapi_encoder *enc = data;
  807. *extra_data = enc->sei;
  808. *size = enc->sei_size;
  809. return true;
  810. }
  811. struct obs_encoder_info h264_vaapi_encoder_info = {
  812. .id = "ffmpeg_vaapi",
  813. .type = OBS_ENCODER_VIDEO,
  814. .codec = "h264",
  815. .get_name = h264_vaapi_getname,
  816. .create = h264_vaapi_create,
  817. .destroy = vaapi_destroy,
  818. .encode = h264_vaapi_encode,
  819. .get_defaults = h264_vaapi_defaults,
  820. .get_properties = h264_vaapi_properties,
  821. .get_extra_data = vaapi_extra_data,
  822. .get_sei_data = vaapi_sei_data,
  823. .get_video_info = h264_vaapi_video_info,
  824. };
  825. #ifdef ENABLE_HEVC
  826. struct obs_encoder_info hevc_vaapi_encoder_info = {
  827. .id = "hevc_ffmpeg_vaapi",
  828. .type = OBS_ENCODER_VIDEO,
  829. .codec = "hevc",
  830. .get_name = hevc_vaapi_getname,
  831. .create = hevc_vaapi_create,
  832. .destroy = vaapi_destroy,
  833. .encode = hevc_vaapi_encode,
  834. .get_defaults = hevc_vaapi_defaults,
  835. .get_properties = hevc_vaapi_properties,
  836. .get_extra_data = vaapi_extra_data,
  837. .get_sei_data = vaapi_sei_data,
  838. .get_video_info = hevc_vaapi_video_info,
  839. };
  840. #endif
  841. #endif