obs-ffmpeg-vaapi.c 27 KB

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