obs-ffmpeg-vaapi.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397
  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. #include <util/darray.h>
  16. #include <util/dstr.h>
  17. #include <util/base.h>
  18. #include <util/platform.h>
  19. #include <media-io/video-io.h>
  20. #include <obs-module.h>
  21. #include <obs-avc.h>
  22. #include <obs-av1.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 <libavutil/hwcontext_vaapi.h>
  32. #include <libavcodec/avcodec.h>
  33. #include <libavformat/avformat.h>
  34. #include <libavfilter/avfilter.h>
  35. #include <pci/pci.h>
  36. #include "vaapi-utils.h"
  37. #include "obs-ffmpeg-formats.h"
  38. #include <va/va_drmcommon.h>
  39. #include <libdrm/drm_fourcc.h>
  40. #define do_log(level, format, ...) \
  41. blog(level, "[FFmpeg VAAPI encoder: '%s'] " format, \
  42. obs_encoder_get_name(enc->encoder), ##__VA_ARGS__)
  43. #define warn(format, ...) do_log(LOG_WARNING, format, ##__VA_ARGS__)
  44. #define info(format, ...) do_log(LOG_INFO, format, ##__VA_ARGS__)
  45. #define debug(format, ...) do_log(LOG_DEBUG, format, ##__VA_ARGS__)
  46. enum codec_type {
  47. CODEC_H264,
  48. CODEC_HEVC,
  49. CODEC_AV1,
  50. };
  51. struct vaapi_surface {
  52. AVFrame *frame;
  53. gs_texture_t *textures[4];
  54. uint32_t num_textures;
  55. };
  56. struct vaapi_encoder {
  57. obs_encoder_t *encoder;
  58. enum codec_type codec;
  59. AVBufferRef *vadevice_ref;
  60. AVBufferRef *vaframes_ref;
  61. VADisplay va_dpy;
  62. const AVCodec *vaapi;
  63. AVCodecContext *context;
  64. AVPacket *packet;
  65. AVFrame *vframe;
  66. DARRAY(uint8_t) buffer;
  67. uint8_t *header;
  68. size_t header_size;
  69. uint8_t *sei;
  70. size_t sei_size;
  71. int height;
  72. bool first_packet;
  73. bool initialized;
  74. };
  75. static const char *h264_vaapi_getname(void *unused)
  76. {
  77. UNUSED_PARAMETER(unused);
  78. return "FFmpeg VAAPI H.264";
  79. }
  80. static const char *av1_vaapi_getname(void *unused)
  81. {
  82. UNUSED_PARAMETER(unused);
  83. return "FFmpeg VAAPI AV1";
  84. }
  85. #ifdef ENABLE_HEVC
  86. static const char *hevc_vaapi_getname(void *unused)
  87. {
  88. UNUSED_PARAMETER(unused);
  89. return "FFmpeg VAAPI HEVC";
  90. }
  91. #endif
  92. static inline bool vaapi_valid_format(struct vaapi_encoder *enc,
  93. enum video_format format)
  94. {
  95. if (enc->codec == CODEC_H264) {
  96. return format == VIDEO_FORMAT_NV12;
  97. } else if (enc->codec == CODEC_HEVC || enc->codec == CODEC_AV1) {
  98. return (format == VIDEO_FORMAT_NV12) ||
  99. (format == VIDEO_FORMAT_P010);
  100. } else {
  101. return false;
  102. }
  103. }
  104. static void vaapi_video_info(void *data, struct video_scale_info *info)
  105. {
  106. struct vaapi_encoder *enc = data;
  107. enum video_format pref_format;
  108. pref_format = obs_encoder_get_preferred_video_format(enc->encoder);
  109. if (!vaapi_valid_format(enc, pref_format)) {
  110. pref_format = vaapi_valid_format(enc, info->format)
  111. ? info->format
  112. : VIDEO_FORMAT_NV12;
  113. }
  114. info->format = pref_format;
  115. }
  116. static bool vaapi_init_codec(struct vaapi_encoder *enc, const char *path)
  117. {
  118. int ret;
  119. ret = av_hwdevice_ctx_create(&enc->vadevice_ref, AV_HWDEVICE_TYPE_VAAPI,
  120. path, NULL, 0);
  121. if (ret < 0) {
  122. warn("Failed to create VAAPI device context: %s",
  123. av_err2str(ret));
  124. return false;
  125. }
  126. AVHWDeviceContext *vahwctx =
  127. (AVHWDeviceContext *)enc->vadevice_ref->data;
  128. AVVAAPIDeviceContext *vadevctx = vahwctx->hwctx;
  129. enc->va_dpy = vadevctx->display;
  130. enc->vaframes_ref = av_hwframe_ctx_alloc(enc->vadevice_ref);
  131. if (!enc->vaframes_ref) {
  132. warn("Failed to alloc HW frames context");
  133. return false;
  134. }
  135. AVHWFramesContext *frames_ctx =
  136. (AVHWFramesContext *)enc->vaframes_ref->data;
  137. frames_ctx->format = AV_PIX_FMT_VAAPI;
  138. frames_ctx->sw_format = enc->context->pix_fmt;
  139. frames_ctx->width = enc->context->width;
  140. frames_ctx->height = enc->context->height;
  141. ret = av_hwframe_ctx_init(enc->vaframes_ref);
  142. if (ret < 0) {
  143. warn("Failed to init HW frames context: %s", av_err2str(ret));
  144. return false;
  145. }
  146. /* 2. Create software frame and picture */
  147. enc->vframe = av_frame_alloc();
  148. if (!enc->vframe) {
  149. warn("Failed to allocate video frame");
  150. return false;
  151. }
  152. enc->vframe->format = enc->context->pix_fmt;
  153. enc->vframe->width = enc->context->width;
  154. enc->vframe->height = enc->context->height;
  155. enc->vframe->colorspace = enc->context->colorspace;
  156. enc->vframe->color_range = enc->context->color_range;
  157. enc->vframe->chroma_location = enc->context->chroma_sample_location;
  158. ret = av_frame_get_buffer(enc->vframe, base_get_alignment());
  159. if (ret < 0) {
  160. warn("Failed to allocate vframe: %s", av_err2str(ret));
  161. return false;
  162. }
  163. /* 3. set up codec */
  164. enc->context->pix_fmt = AV_PIX_FMT_VAAPI;
  165. enc->context->hw_frames_ctx = av_buffer_ref(enc->vaframes_ref);
  166. ret = avcodec_open2(enc->context, enc->vaapi, NULL);
  167. if (ret < 0) {
  168. warn("Failed to open VAAPI codec: %s", av_err2str(ret));
  169. return false;
  170. }
  171. enc->packet = av_packet_alloc();
  172. enc->initialized = true;
  173. return true;
  174. }
  175. /* "Allowed" options per Rate Control
  176. * See FFMPEG libavcodec/vaapi_encode.c (vaapi_encode_rc_modes)
  177. */
  178. typedef struct {
  179. const char *name;
  180. bool qp;
  181. bool bitrate;
  182. bool maxrate;
  183. } rc_mode_t;
  184. static const rc_mode_t *get_rc_mode(const char *name)
  185. {
  186. /* Set "allowed" options per Rate Control */
  187. static const rc_mode_t RC_MODES[] = {
  188. {.name = "CBR", .qp = false, .bitrate = true, .maxrate = false},
  189. {.name = "CQP", .qp = true, .bitrate = false, .maxrate = false},
  190. {.name = "VBR", .qp = false, .bitrate = true, .maxrate = true},
  191. {0}};
  192. const rc_mode_t *rc_mode = RC_MODES;
  193. while (!!rc_mode->name && strcmp(rc_mode->name, name) != 0)
  194. rc_mode++;
  195. return rc_mode ? rc_mode : RC_MODES;
  196. }
  197. static bool vaapi_update(void *data, obs_data_t *settings)
  198. {
  199. struct vaapi_encoder *enc = data;
  200. const char *device = obs_data_get_string(settings, "vaapi_device");
  201. const char *rate_control =
  202. obs_data_get_string(settings, "rate_control");
  203. const rc_mode_t *rc_mode = get_rc_mode(rate_control);
  204. bool cbr = strcmp(rc_mode->name, "CBR") == 0;
  205. int profile = (int)obs_data_get_int(settings, "profile");
  206. int bf = (int)obs_data_get_int(settings, "bf");
  207. int qp = rc_mode->qp ? (int)obs_data_get_int(settings, "qp") : 0;
  208. enc->context->global_quality = enc->codec == CODEC_AV1 ? qp * 5 : qp;
  209. int level = (int)obs_data_get_int(settings, "level");
  210. int bitrate = rc_mode->bitrate
  211. ? (int)obs_data_get_int(settings, "bitrate")
  212. : 0;
  213. int maxrate = rc_mode->maxrate
  214. ? (int)obs_data_get_int(settings, "maxrate")
  215. : 0;
  216. int keyint_sec = (int)obs_data_get_int(settings, "keyint_sec");
  217. /* For Rate Control which allows maxrate, FFMPEG will give
  218. * an error if maxrate > bitrate. To prevent that set maxrate
  219. * to 0.
  220. * For CBR, maxrate = bitrate
  221. */
  222. if (cbr)
  223. maxrate = bitrate;
  224. else if (rc_mode->maxrate && maxrate && maxrate < bitrate)
  225. maxrate = 0;
  226. video_t *video = obs_encoder_video(enc->encoder);
  227. const struct video_output_info *voi = video_output_get_info(video);
  228. struct video_scale_info info;
  229. info.format = voi->format;
  230. info.colorspace = voi->colorspace;
  231. info.range = voi->range;
  232. #ifdef ENABLE_HEVC
  233. if (enc->codec == CODEC_HEVC) {
  234. if ((profile == FF_PROFILE_HEVC_MAIN) &&
  235. (info.format == VIDEO_FORMAT_P010)) {
  236. warn("Forcing Main10 for P010");
  237. profile = FF_PROFILE_HEVC_MAIN_10;
  238. }
  239. }
  240. #endif
  241. vaapi_video_info(enc, &info);
  242. enc->context->profile = profile;
  243. enc->context->max_b_frames = bf;
  244. enc->context->level = level;
  245. enc->context->bit_rate = bitrate * 1000;
  246. enc->context->rc_max_rate = maxrate * 1000;
  247. enc->context->rc_initial_buffer_occupancy =
  248. (maxrate ? maxrate : bitrate) * 1000;
  249. enc->context->width = obs_encoder_get_width(enc->encoder);
  250. enc->context->height = obs_encoder_get_height(enc->encoder);
  251. enc->context->time_base = (AVRational){voi->fps_den, voi->fps_num};
  252. const enum AVPixelFormat pix_fmt =
  253. obs_to_ffmpeg_video_format(info.format);
  254. enc->context->pix_fmt = pix_fmt;
  255. enc->context->color_range = info.range == VIDEO_RANGE_FULL
  256. ? AVCOL_RANGE_JPEG
  257. : AVCOL_RANGE_MPEG;
  258. enum AVColorSpace colorspace = AVCOL_SPC_UNSPECIFIED;
  259. switch (info.colorspace) {
  260. case VIDEO_CS_601:
  261. enc->context->color_primaries = AVCOL_PRI_SMPTE170M;
  262. enc->context->color_trc = AVCOL_TRC_SMPTE170M;
  263. colorspace = AVCOL_SPC_SMPTE170M;
  264. break;
  265. case VIDEO_CS_DEFAULT:
  266. case VIDEO_CS_709:
  267. enc->context->color_primaries = AVCOL_PRI_BT709;
  268. enc->context->color_trc = AVCOL_TRC_BT709;
  269. colorspace = AVCOL_SPC_BT709;
  270. break;
  271. case VIDEO_CS_SRGB:
  272. enc->context->color_primaries = AVCOL_PRI_BT709;
  273. enc->context->color_trc = AVCOL_TRC_IEC61966_2_1;
  274. colorspace = AVCOL_SPC_BT709;
  275. break;
  276. case VIDEO_CS_2100_PQ:
  277. enc->context->color_primaries = AVCOL_PRI_BT2020;
  278. enc->context->color_trc = AVCOL_TRC_SMPTE2084;
  279. colorspace = AVCOL_SPC_BT2020_NCL;
  280. break;
  281. case VIDEO_CS_2100_HLG:
  282. enc->context->color_primaries = AVCOL_PRI_BT2020;
  283. enc->context->color_trc = AVCOL_TRC_ARIB_STD_B67;
  284. colorspace = AVCOL_SPC_BT2020_NCL;
  285. break;
  286. default:
  287. break;
  288. }
  289. enc->context->colorspace = colorspace;
  290. enc->context->chroma_sample_location =
  291. determine_chroma_location(pix_fmt, colorspace);
  292. if (keyint_sec > 0) {
  293. enc->context->gop_size =
  294. keyint_sec * voi->fps_num / voi->fps_den;
  295. } else {
  296. enc->context->gop_size = 120;
  297. }
  298. enc->height = enc->context->height;
  299. const char *ffmpeg_opts = obs_data_get_string(settings, "ffmpeg_opts");
  300. struct obs_options opts = obs_parse_options(ffmpeg_opts);
  301. for (size_t i = 0; i < opts.count; i++) {
  302. struct obs_option *opt = &opts.options[i];
  303. av_opt_set(enc->context->priv_data, opt->name, opt->value, 0);
  304. }
  305. obs_free_options(opts);
  306. info("settings:\n"
  307. "\tdevice: %s\n"
  308. "\trate_control: %s\n"
  309. "\tprofile: %d\n"
  310. "\tlevel: %d\n"
  311. "\tqp: %d\n"
  312. "\tbitrate: %d\n"
  313. "\tmaxrate: %d\n"
  314. "\tkeyint: %d\n"
  315. "\twidth: %d\n"
  316. "\theight: %d\n"
  317. "\tb-frames: %d\n"
  318. "\tffmpeg opts: %s\n",
  319. device, rate_control, profile, level, qp, bitrate, maxrate,
  320. enc->context->gop_size, enc->context->width, enc->context->height,
  321. enc->context->max_b_frames, ffmpeg_opts);
  322. return vaapi_init_codec(enc, device);
  323. }
  324. static inline enum gs_color_format drm_to_gs_color_format(int format)
  325. {
  326. switch (format) {
  327. case DRM_FORMAT_R8:
  328. return GS_R8;
  329. case DRM_FORMAT_R16:
  330. return GS_R16;
  331. case DRM_FORMAT_GR88:
  332. return GS_R8G8;
  333. case DRM_FORMAT_GR1616:
  334. return GS_RG16;
  335. default:
  336. blog(LOG_ERROR, "Unsupported DRM format %d", format);
  337. return GS_UNKNOWN;
  338. }
  339. }
  340. static void vaapi_destroy_surface(struct vaapi_surface *out)
  341. {
  342. for (uint32_t i = 0; i < out->num_textures; ++i) {
  343. if (out->textures[i]) {
  344. gs_texture_destroy(out->textures[i]);
  345. out->textures[i] = NULL;
  346. }
  347. }
  348. av_frame_free(&out->frame);
  349. }
  350. static bool vaapi_create_surface(struct vaapi_encoder *enc,
  351. struct vaapi_surface *out)
  352. {
  353. int ret;
  354. VAStatus vas;
  355. VADRMPRIMESurfaceDescriptor desc;
  356. const AVPixFmtDescriptor *fmt_desc;
  357. bool ok = true;
  358. memset(out, 0, sizeof(*out));
  359. fmt_desc = av_pix_fmt_desc_get(enc->context->pix_fmt);
  360. if (!fmt_desc) {
  361. warn("Failed to get pix fmt descriptor");
  362. return false;
  363. }
  364. out->frame = av_frame_alloc();
  365. if (!out->frame) {
  366. warn("Failed to allocate hw frame");
  367. return false;
  368. }
  369. ret = av_hwframe_get_buffer(enc->vaframes_ref, out->frame, 0);
  370. if (ret < 0) {
  371. warn("Failed to get hw frame buffer: %s", av_err2str(ret));
  372. goto fail;
  373. }
  374. vas = vaExportSurfaceHandle(enc->va_dpy, (uintptr_t)out->frame->data[3],
  375. VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME_2,
  376. VA_EXPORT_SURFACE_WRITE_ONLY |
  377. VA_EXPORT_SURFACE_SEPARATE_LAYERS,
  378. &desc);
  379. if (vas != VA_STATUS_SUCCESS) {
  380. warn("Failed to export VA surface handle: %s", vaErrorStr(vas));
  381. goto fail;
  382. }
  383. for (uint32_t i = 0; i < desc.num_layers; ++i) {
  384. unsigned int width = desc.width;
  385. unsigned int height = desc.height;
  386. if (i) {
  387. width /= 1 << fmt_desc->log2_chroma_w;
  388. height /= 1 << fmt_desc->log2_chroma_h;
  389. }
  390. out->textures[i] = gs_texture_create_from_dmabuf(
  391. width, height, desc.layers[i].drm_format,
  392. drm_to_gs_color_format(desc.layers[i].drm_format), 1,
  393. &desc.objects[desc.layers[i].object_index[0]].fd,
  394. &desc.layers[i].pitch[0], &desc.layers[i].offset[0],
  395. &desc.objects[desc.layers[i].object_index[0]]
  396. .drm_format_modifier);
  397. if (!out->textures[i]) {
  398. warn("Failed to import VA surface texture");
  399. ok = false;
  400. }
  401. out->num_textures++;
  402. }
  403. for (uint32_t i = 0; i < desc.num_objects; ++i)
  404. close(desc.objects[i].fd);
  405. if (ok)
  406. return true;
  407. fail:
  408. vaapi_destroy_surface(out);
  409. return false;
  410. }
  411. static inline void flush_remaining_packets(struct vaapi_encoder *enc)
  412. {
  413. int r_pkt = 1;
  414. while (r_pkt) {
  415. if (avcodec_receive_packet(enc->context, enc->packet) < 0)
  416. break;
  417. if (r_pkt)
  418. av_packet_unref(enc->packet);
  419. }
  420. }
  421. static void vaapi_destroy(void *data)
  422. {
  423. struct vaapi_encoder *enc = data;
  424. if (enc->initialized)
  425. flush_remaining_packets(enc);
  426. av_packet_free(&enc->packet);
  427. avcodec_free_context(&enc->context);
  428. av_frame_unref(enc->vframe);
  429. av_frame_free(&enc->vframe);
  430. av_buffer_unref(&enc->vaframes_ref);
  431. av_buffer_unref(&enc->vadevice_ref);
  432. da_free(enc->buffer);
  433. bfree(enc->header);
  434. bfree(enc->sei);
  435. bfree(enc);
  436. }
  437. static inline const char *vaapi_encoder_name(enum codec_type codec)
  438. {
  439. if (codec == CODEC_H264) {
  440. return "h264_vaapi";
  441. } else if (codec == CODEC_HEVC) {
  442. return "hevc_vaapi";
  443. } else if (codec == CODEC_AV1) {
  444. return "av1_vaapi";
  445. }
  446. return NULL;
  447. }
  448. static void *vaapi_create_internal(obs_data_t *settings, obs_encoder_t *encoder,
  449. enum codec_type codec)
  450. {
  451. struct vaapi_encoder *enc;
  452. enc = bzalloc(sizeof(*enc));
  453. enc->encoder = encoder;
  454. enc->codec = codec;
  455. enc->vaapi = avcodec_find_encoder_by_name(vaapi_encoder_name(codec));
  456. enc->first_packet = true;
  457. blog(LOG_INFO, "---------------------------------");
  458. if (!enc->vaapi) {
  459. warn("Couldn't find encoder");
  460. goto fail;
  461. }
  462. enc->context = avcodec_alloc_context3(enc->vaapi);
  463. if (!enc->context) {
  464. warn("Failed to create codec context");
  465. goto fail;
  466. }
  467. if (!vaapi_update(enc, settings))
  468. goto fail;
  469. return enc;
  470. fail:
  471. vaapi_destroy(enc);
  472. return NULL;
  473. }
  474. static inline bool vaapi_test_texencode(struct vaapi_encoder *enc)
  475. {
  476. struct vaapi_surface surface;
  477. if (obs_encoder_scaling_enabled(enc->encoder) &&
  478. !obs_encoder_gpu_scaling_enabled(enc->encoder))
  479. return false;
  480. obs_enter_graphics();
  481. bool success = vaapi_create_surface(enc, &surface);
  482. vaapi_destroy_surface(&surface);
  483. obs_leave_graphics();
  484. return success;
  485. }
  486. static void *vaapi_create_tex_internal(obs_data_t *settings,
  487. obs_encoder_t *encoder,
  488. enum codec_type codec,
  489. const char *fallback)
  490. {
  491. void *enc = vaapi_create_internal(settings, encoder, codec);
  492. if (!enc) {
  493. return NULL;
  494. }
  495. if (!vaapi_test_texencode(enc)) {
  496. vaapi_destroy(enc);
  497. blog(LOG_WARNING, "VAAPI: Falling back to %s encoder",
  498. fallback);
  499. return obs_encoder_create_rerouted(encoder, fallback);
  500. }
  501. return enc;
  502. }
  503. static void *h264_vaapi_create(obs_data_t *settings, obs_encoder_t *encoder)
  504. {
  505. return vaapi_create_internal(settings, encoder, CODEC_H264);
  506. }
  507. static void *h264_vaapi_create_tex(obs_data_t *settings, obs_encoder_t *encoder)
  508. {
  509. return vaapi_create_tex_internal(settings, encoder, CODEC_H264,
  510. "ffmpeg_vaapi");
  511. }
  512. static void *av1_vaapi_create(obs_data_t *settings, obs_encoder_t *encoder)
  513. {
  514. return vaapi_create_internal(settings, encoder, CODEC_AV1);
  515. }
  516. static void *av1_vaapi_create_tex(obs_data_t *settings, obs_encoder_t *encoder)
  517. {
  518. return vaapi_create_tex_internal(settings, encoder, CODEC_AV1,
  519. "av1_ffmpeg_vaapi");
  520. }
  521. #ifdef ENABLE_HEVC
  522. static void *hevc_vaapi_create(obs_data_t *settings, obs_encoder_t *encoder)
  523. {
  524. return vaapi_create_internal(settings, encoder, CODEC_HEVC);
  525. }
  526. static void *hevc_vaapi_create_tex(obs_data_t *settings, obs_encoder_t *encoder)
  527. {
  528. return vaapi_create_tex_internal(settings, encoder, CODEC_HEVC,
  529. "hevc_ffmpeg_vaapi");
  530. }
  531. #endif
  532. static inline void copy_data(AVFrame *pic, const struct encoder_frame *frame,
  533. int height, enum AVPixelFormat format)
  534. {
  535. int h_chroma_shift, v_chroma_shift;
  536. av_pix_fmt_get_chroma_sub_sample(format, &h_chroma_shift,
  537. &v_chroma_shift);
  538. for (int plane = 0; plane < MAX_AV_PLANES; plane++) {
  539. if (!frame->data[plane])
  540. continue;
  541. int frame_rowsize = (int)frame->linesize[plane];
  542. int pic_rowsize = pic->linesize[plane];
  543. int bytes = frame_rowsize < pic_rowsize ? frame_rowsize
  544. : pic_rowsize;
  545. int plane_height = height >> (plane ? v_chroma_shift : 0);
  546. for (int y = 0; y < plane_height; y++) {
  547. int pos_frame = y * frame_rowsize;
  548. int pos_pic = y * pic_rowsize;
  549. memcpy(pic->data[plane] + pos_pic,
  550. frame->data[plane] + pos_frame, bytes);
  551. }
  552. }
  553. }
  554. static bool vaapi_encode_internal(struct vaapi_encoder *enc, AVFrame *frame,
  555. struct encoder_packet *packet,
  556. bool *received_packet)
  557. {
  558. int got_packet;
  559. int ret;
  560. ret = avcodec_send_frame(enc->context, frame);
  561. if (ret == 0 || ret == AVERROR(EAGAIN))
  562. ret = avcodec_receive_packet(enc->context, enc->packet);
  563. got_packet = (ret == 0);
  564. if (ret == AVERROR_EOF || ret == AVERROR(EAGAIN))
  565. ret = 0;
  566. if (ret < 0) {
  567. warn("vaapi_encode: Error encoding: %s", av_err2str(ret));
  568. goto fail;
  569. }
  570. if (got_packet && enc->packet->size) {
  571. if (enc->first_packet) {
  572. uint8_t *new_packet;
  573. size_t size;
  574. enc->first_packet = false;
  575. switch (enc->codec) {
  576. case CODEC_HEVC:
  577. #ifdef ENABLE_HEVC
  578. obs_extract_hevc_headers(
  579. enc->packet->data, enc->packet->size,
  580. &new_packet, &size, &enc->header,
  581. &enc->header_size, &enc->sei,
  582. &enc->sei_size);
  583. break;
  584. #else
  585. warn("vaapi_encode: HEVC codec is not supported");
  586. goto fail;
  587. #endif
  588. case CODEC_H264:
  589. obs_extract_avc_headers(
  590. enc->packet->data, enc->packet->size,
  591. &new_packet, &size, &enc->header,
  592. &enc->header_size, &enc->sei,
  593. &enc->sei_size);
  594. break;
  595. case CODEC_AV1:
  596. obs_extract_av1_headers(enc->packet->data,
  597. enc->packet->size,
  598. &new_packet, &size,
  599. &enc->header,
  600. &enc->header_size);
  601. break;
  602. }
  603. da_copy_array(enc->buffer, new_packet, size);
  604. bfree(new_packet);
  605. } else {
  606. da_copy_array(enc->buffer, enc->packet->data,
  607. enc->packet->size);
  608. }
  609. packet->pts = enc->packet->pts;
  610. packet->dts = enc->packet->dts;
  611. packet->data = enc->buffer.array;
  612. packet->size = enc->buffer.num;
  613. packet->type = OBS_ENCODER_VIDEO;
  614. #ifdef ENABLE_HEVC
  615. if (enc->codec == CODEC_HEVC) {
  616. packet->keyframe =
  617. obs_hevc_keyframe(packet->data, packet->size);
  618. } else
  619. #endif
  620. if (enc->codec == CODEC_H264) {
  621. packet->keyframe =
  622. obs_avc_keyframe(packet->data, packet->size);
  623. } else if (enc->codec == CODEC_AV1) {
  624. packet->keyframe =
  625. obs_av1_keyframe(packet->data, packet->size);
  626. }
  627. *received_packet = true;
  628. }
  629. av_packet_unref(enc->packet);
  630. return true;
  631. fail:
  632. av_packet_unref(enc->packet);
  633. return false;
  634. }
  635. static bool vaapi_encode_copy(void *data, struct encoder_frame *frame,
  636. struct encoder_packet *packet,
  637. bool *received_packet)
  638. {
  639. struct vaapi_encoder *enc = data;
  640. AVFrame *hwframe = NULL;
  641. int ret;
  642. *received_packet = false;
  643. hwframe = av_frame_alloc();
  644. if (!hwframe) {
  645. warn("vaapi_encode_copy: failed to allocate hw frame");
  646. return false;
  647. }
  648. ret = av_hwframe_get_buffer(enc->vaframes_ref, hwframe, 0);
  649. if (ret < 0) {
  650. warn("vaapi_encode_copy: failed to get buffer for hw frame: %s",
  651. av_err2str(ret));
  652. goto fail;
  653. }
  654. copy_data(enc->vframe, frame, enc->height, enc->context->pix_fmt);
  655. enc->vframe->pts = frame->pts;
  656. hwframe->pts = frame->pts;
  657. hwframe->width = enc->vframe->width;
  658. hwframe->height = enc->vframe->height;
  659. ret = av_hwframe_transfer_data(hwframe, enc->vframe, 0);
  660. if (ret < 0) {
  661. warn("vaapi_encode_copy: failed to upload hw frame: %s",
  662. av_err2str(ret));
  663. goto fail;
  664. }
  665. ret = av_frame_copy_props(hwframe, enc->vframe);
  666. if (ret < 0) {
  667. warn("vaapi_encode_copy: failed to copy props to hw frame: %s",
  668. av_err2str(ret));
  669. goto fail;
  670. }
  671. if (!vaapi_encode_internal(enc, hwframe, packet, received_packet))
  672. goto fail;
  673. av_frame_free(&hwframe);
  674. return true;
  675. fail:
  676. av_frame_free(&hwframe);
  677. return false;
  678. }
  679. static bool vaapi_encode_tex(void *data, struct encoder_texture *texture,
  680. int64_t pts, uint64_t lock_key, uint64_t *next_key,
  681. struct encoder_packet *packet,
  682. bool *received_packet)
  683. {
  684. UNUSED_PARAMETER(lock_key);
  685. UNUSED_PARAMETER(next_key);
  686. struct vaapi_encoder *enc = data;
  687. struct vaapi_surface surface;
  688. int ret;
  689. *received_packet = false;
  690. obs_enter_graphics();
  691. // Destroyed piecemeal to avoid taking the graphics lock again.
  692. if (!vaapi_create_surface(enc, &surface)) {
  693. warn("vaapi_encode_tex: failed to create texture hw frame");
  694. obs_leave_graphics();
  695. return false;
  696. }
  697. for (uint32_t i = 0; i < surface.num_textures; ++i) {
  698. if (!texture->tex[i]) {
  699. warn("vaapi_encode_tex: unexpected number of textures");
  700. obs_leave_graphics();
  701. goto fail;
  702. }
  703. gs_copy_texture(surface.textures[i], texture->tex[i]);
  704. gs_texture_destroy(surface.textures[i]);
  705. }
  706. gs_flush();
  707. obs_leave_graphics();
  708. enc->vframe->pts = pts;
  709. ret = av_frame_copy_props(surface.frame, enc->vframe);
  710. if (ret < 0) {
  711. warn("vaapi_encode_tex: failed to copy props to hw frame: %s",
  712. av_err2str(ret));
  713. goto fail;
  714. }
  715. if (!vaapi_encode_internal(enc, surface.frame, packet, received_packet))
  716. goto fail;
  717. av_frame_free(&surface.frame);
  718. return true;
  719. fail:
  720. av_frame_free(&surface.frame);
  721. return false;
  722. }
  723. static void set_visible(obs_properties_t *ppts, const char *name, bool visible)
  724. {
  725. obs_property_t *p = obs_properties_get(ppts, name);
  726. obs_property_set_visible(p, visible);
  727. }
  728. static inline VAProfile vaapi_profile(enum codec_type codec)
  729. {
  730. if (codec == CODEC_H264) {
  731. return VAProfileH264ConstrainedBaseline;
  732. } else if (codec == CODEC_AV1) {
  733. return VAProfileAV1Profile0;
  734. #if ENABLE_HEVC
  735. } else if (codec == CODEC_HEVC) {
  736. return VAProfileHEVCMain;
  737. #endif
  738. }
  739. return VAProfileNone;
  740. }
  741. static inline const char *vaapi_default_device(enum codec_type codec)
  742. {
  743. if (codec == CODEC_H264) {
  744. return vaapi_get_h264_default_device();
  745. } else if (codec == CODEC_AV1) {
  746. return vaapi_get_av1_default_device();
  747. #if ENABLE_HEVC
  748. } else if (codec == CODEC_HEVC) {
  749. return vaapi_get_hevc_default_device();
  750. #endif
  751. }
  752. return NULL;
  753. }
  754. static void vaapi_defaults_internal(obs_data_t *settings, enum codec_type codec)
  755. {
  756. const char *const device = vaapi_default_device(codec);
  757. obs_data_set_default_string(settings, "vaapi_device", device);
  758. #ifdef ENABLE_HEVC
  759. if (codec == CODEC_HEVC)
  760. obs_data_set_default_int(settings, "profile",
  761. FF_PROFILE_HEVC_MAIN);
  762. else
  763. #endif
  764. if (codec == CODEC_H264)
  765. obs_data_set_default_int(settings, "profile",
  766. FF_PROFILE_H264_HIGH);
  767. else if (codec == CODEC_AV1)
  768. obs_data_set_default_int(settings, "profile",
  769. FF_PROFILE_AV1_MAIN);
  770. obs_data_set_default_int(settings, "level", FF_LEVEL_UNKNOWN);
  771. obs_data_set_default_int(settings, "bitrate", 2500);
  772. obs_data_set_default_int(settings, "keyint_sec", 0);
  773. obs_data_set_default_int(settings, "bf", 0);
  774. obs_data_set_default_int(settings, "qp", 20);
  775. obs_data_set_default_int(settings, "maxrate", 0);
  776. int drm_fd = -1;
  777. VADisplay va_dpy = vaapi_open_device(&drm_fd, device, "vaapi_defaults");
  778. if (!va_dpy)
  779. return;
  780. const VAProfile profile = vaapi_profile(codec);
  781. if (vaapi_device_rc_supported(profile, va_dpy, VA_RC_CBR, device))
  782. obs_data_set_default_string(settings, "rate_control", "CBR");
  783. else if (vaapi_device_rc_supported(profile, va_dpy, VA_RC_VBR, device))
  784. obs_data_set_default_string(settings, "rate_control", "VBR");
  785. else
  786. obs_data_set_default_string(settings, "rate_control", "CQP");
  787. vaapi_close_device(&drm_fd, va_dpy);
  788. }
  789. static void h264_vaapi_defaults(obs_data_t *settings)
  790. {
  791. vaapi_defaults_internal(settings, CODEC_H264);
  792. }
  793. static void av1_vaapi_defaults(obs_data_t *settings)
  794. {
  795. vaapi_defaults_internal(settings, CODEC_AV1);
  796. }
  797. static void hevc_vaapi_defaults(obs_data_t *settings)
  798. {
  799. vaapi_defaults_internal(settings, CODEC_HEVC);
  800. }
  801. static bool vaapi_device_modified(obs_properties_t *ppts, obs_property_t *p,
  802. obs_data_t *settings)
  803. {
  804. UNUSED_PARAMETER(p);
  805. const char *device = obs_data_get_string(settings, "vaapi_device");
  806. int drm_fd = -1;
  807. VADisplay va_dpy =
  808. vaapi_open_device(&drm_fd, device, "vaapi_device_modified");
  809. int profile = obs_data_get_int(settings, "profile");
  810. obs_property_t *rc_p = obs_properties_get(ppts, "rate_control");
  811. obs_property_list_clear(rc_p);
  812. if (!va_dpy)
  813. goto fail;
  814. switch (profile) {
  815. case FF_PROFILE_H264_CONSTRAINED_BASELINE:
  816. if (!vaapi_display_h264_supported(va_dpy, device))
  817. goto fail;
  818. profile = VAProfileH264ConstrainedBaseline;
  819. break;
  820. case FF_PROFILE_H264_MAIN:
  821. if (!vaapi_display_h264_supported(va_dpy, device))
  822. goto fail;
  823. profile = VAProfileH264Main;
  824. break;
  825. case FF_PROFILE_H264_HIGH:
  826. if (!vaapi_display_h264_supported(va_dpy, device))
  827. goto fail;
  828. profile = VAProfileH264High;
  829. break;
  830. case FF_PROFILE_AV1_MAIN:
  831. if (!vaapi_display_av1_supported(va_dpy, device))
  832. goto fail;
  833. profile = VAProfileAV1Profile0;
  834. break;
  835. #ifdef ENABLE_HEVC
  836. case FF_PROFILE_HEVC_MAIN:
  837. if (!vaapi_display_hevc_supported(va_dpy, device))
  838. goto fail;
  839. profile = VAProfileHEVCMain;
  840. break;
  841. case FF_PROFILE_HEVC_MAIN_10:
  842. if (!vaapi_display_hevc_supported(va_dpy, device))
  843. goto fail;
  844. profile = VAProfileHEVCMain10;
  845. break;
  846. #endif
  847. }
  848. if (vaapi_device_rc_supported(profile, va_dpy, VA_RC_CBR, device))
  849. obs_property_list_add_string(rc_p, "CBR", "CBR");
  850. if (vaapi_device_rc_supported(profile, va_dpy, VA_RC_VBR, device))
  851. obs_property_list_add_string(rc_p, "VBR", "VBR");
  852. if (vaapi_device_rc_supported(profile, va_dpy, VA_RC_CQP, device))
  853. obs_property_list_add_string(rc_p, "CQP", "CQP");
  854. set_visible(ppts, "bf", vaapi_device_bframe_supported(profile, va_dpy));
  855. fail:
  856. vaapi_close_device(&drm_fd, va_dpy);
  857. return true;
  858. }
  859. static bool rate_control_modified(obs_properties_t *ppts, obs_property_t *p,
  860. obs_data_t *settings)
  861. {
  862. UNUSED_PARAMETER(p);
  863. const char *rate_control =
  864. obs_data_get_string(settings, "rate_control");
  865. const rc_mode_t *rc_mode = get_rc_mode(rate_control);
  866. /* Set options visibility per Rate Control */
  867. set_visible(ppts, "qp", rc_mode->qp);
  868. set_visible(ppts, "bitrate", rc_mode->bitrate);
  869. set_visible(ppts, "maxrate", rc_mode->maxrate);
  870. return true;
  871. }
  872. static bool get_device_name_from_pci(struct pci_access *pacc, char *pci_slot,
  873. char *buf, int size)
  874. {
  875. struct pci_filter filter;
  876. struct pci_dev *dev;
  877. char *name;
  878. pci_filter_init(pacc, &filter);
  879. if (pci_filter_parse_slot(&filter, pci_slot))
  880. return false;
  881. pci_scan_bus(pacc);
  882. for (dev = pacc->devices; dev; dev = dev->next) {
  883. if (pci_filter_match(&filter, dev)) {
  884. pci_fill_info(dev, PCI_FILL_IDENT);
  885. name = pci_lookup_name(pacc, buf, size,
  886. PCI_LOOKUP_DEVICE,
  887. dev->vendor_id, dev->device_id);
  888. strcpy(buf, name);
  889. return true;
  890. }
  891. }
  892. return false;
  893. }
  894. static bool vaapi_device_codec_supported(const char *path,
  895. enum codec_type codec)
  896. {
  897. switch (codec) {
  898. case CODEC_H264:
  899. return vaapi_device_h264_supported(path);
  900. case CODEC_HEVC:
  901. return vaapi_device_hevc_supported(path);
  902. case CODEC_AV1:
  903. return vaapi_device_av1_supported(path);
  904. default:
  905. return false;
  906. }
  907. }
  908. static obs_properties_t *vaapi_properties_internal(enum codec_type codec)
  909. {
  910. obs_properties_t *props = obs_properties_create();
  911. obs_property_t *list;
  912. list = obs_properties_add_list(props, "vaapi_device",
  913. obs_module_text("VAAPI.Device"),
  914. OBS_COMBO_TYPE_LIST,
  915. OBS_COMBO_FORMAT_STRING);
  916. if (os_file_exists("/dev/dri/by-path")) {
  917. os_dir_t *by_path_dir = os_opendir("/dev/dri/by-path");
  918. struct pci_access *pacc = pci_alloc();
  919. struct os_dirent *file;
  920. char namebuf[1024];
  921. char pci_slot[13];
  922. char *type;
  923. pci_init(pacc);
  924. while ((file = os_readdir(by_path_dir)) != NULL) {
  925. // file_name pattern: pci-<pci_slot::12>-<type::{"card","render"}>
  926. char *file_name = file->d_name;
  927. if (strcmp(file_name, ".") == 0 ||
  928. strcmp(file_name, "..") == 0)
  929. continue;
  930. char path[64] = {0};
  931. // Use the return value of snprintf to prevent truncation warning.
  932. int written = snprintf(path, 64, "/dev/dri/by-path/%s",
  933. file_name);
  934. if (written >= 64)
  935. blog(LOG_DEBUG,
  936. "obs-ffmpeg-vaapi: A format truncation may have occurred."
  937. " This can be ignored since it is quite improbable.");
  938. type = strrchr(file_name, '-');
  939. if (type == NULL)
  940. continue;
  941. else
  942. type++;
  943. if (strcmp(type, "render") == 0) {
  944. strncpy(pci_slot, file_name + 4, 12);
  945. pci_slot[12] = 0;
  946. bool name_found = get_device_name_from_pci(
  947. pacc, pci_slot, namebuf,
  948. sizeof(namebuf));
  949. if (!vaapi_device_codec_supported(path, codec))
  950. continue;
  951. if (!name_found)
  952. obs_property_list_add_string(list, path,
  953. path);
  954. else
  955. obs_property_list_add_string(
  956. list, namebuf, path);
  957. }
  958. }
  959. pci_cleanup(pacc);
  960. os_closedir(by_path_dir);
  961. }
  962. if (obs_property_list_item_count(list) == 0) {
  963. char path[32];
  964. for (int i = 28;; i++) {
  965. snprintf(path, sizeof(path), "/dev/dri/renderD1%d", i);
  966. if (access(path, F_OK) == 0) {
  967. char card[128];
  968. int ret = snprintf(card, sizeof(card),
  969. "Card%d: %s", i - 28, path);
  970. if (ret >= (int)sizeof(card))
  971. blog(LOG_DEBUG,
  972. "obs-ffmpeg-vaapi: A format truncation may have occurred."
  973. " This can be ignored since it is quite improbable.");
  974. if (!vaapi_device_h264_supported(path))
  975. continue;
  976. obs_property_list_add_string(list, card, path);
  977. } else {
  978. break;
  979. }
  980. }
  981. }
  982. obs_property_set_modified_callback(list, vaapi_device_modified);
  983. list = obs_properties_add_list(props, "profile",
  984. obs_module_text("Profile"),
  985. OBS_COMBO_TYPE_LIST,
  986. OBS_COMBO_FORMAT_INT);
  987. if (codec == CODEC_HEVC) {
  988. obs_property_list_add_int(list, "Main", FF_PROFILE_HEVC_MAIN);
  989. obs_property_list_add_int(list, "Main10",
  990. FF_PROFILE_HEVC_MAIN_10);
  991. } else if (codec == CODEC_H264) {
  992. obs_property_list_add_int(list, "Constrained Baseline",
  993. FF_PROFILE_H264_CONSTRAINED_BASELINE);
  994. obs_property_list_add_int(list, "Main", FF_PROFILE_H264_MAIN);
  995. obs_property_list_add_int(list, "High", FF_PROFILE_H264_HIGH);
  996. } else if (codec == CODEC_AV1) {
  997. obs_property_list_add_int(list, "Main", FF_PROFILE_AV1_MAIN);
  998. }
  999. obs_property_set_modified_callback(list, vaapi_device_modified);
  1000. list = obs_properties_add_list(props, "level", obs_module_text("Level"),
  1001. OBS_COMBO_TYPE_LIST,
  1002. OBS_COMBO_FORMAT_INT);
  1003. obs_property_list_add_int(list, "Auto", FF_LEVEL_UNKNOWN);
  1004. if (codec == CODEC_H264) {
  1005. obs_property_list_add_int(list, "3.0", 30);
  1006. obs_property_list_add_int(list, "3.1", 31);
  1007. obs_property_list_add_int(list, "4.0", 40);
  1008. obs_property_list_add_int(list, "4.1", 41);
  1009. obs_property_list_add_int(list, "4.2", 42);
  1010. obs_property_list_add_int(list, "5.0", 50);
  1011. obs_property_list_add_int(list, "5.1", 51);
  1012. obs_property_list_add_int(list, "5.2", 52);
  1013. } else if (codec == CODEC_HEVC) {
  1014. obs_property_list_add_int(list, "3.0", 90);
  1015. obs_property_list_add_int(list, "3.1", 93);
  1016. obs_property_list_add_int(list, "4.0", 120);
  1017. obs_property_list_add_int(list, "4.1", 123);
  1018. obs_property_list_add_int(list, "5.0", 150);
  1019. obs_property_list_add_int(list, "5.1", 153);
  1020. obs_property_list_add_int(list, "5.2", 156);
  1021. } else if (codec == CODEC_AV1) {
  1022. obs_property_list_add_int(list, "3.0", 4);
  1023. obs_property_list_add_int(list, "3.1", 5);
  1024. obs_property_list_add_int(list, "4.0", 8);
  1025. obs_property_list_add_int(list, "4.1", 9);
  1026. obs_property_list_add_int(list, "5.0", 12);
  1027. obs_property_list_add_int(list, "5.1", 13);
  1028. obs_property_list_add_int(list, "5.2", 14);
  1029. obs_property_list_add_int(list, "5.3", 15);
  1030. }
  1031. list = obs_properties_add_list(props, "rate_control",
  1032. obs_module_text("RateControl"),
  1033. OBS_COMBO_TYPE_LIST,
  1034. OBS_COMBO_FORMAT_STRING);
  1035. obs_property_set_modified_callback(list, rate_control_modified);
  1036. obs_property_t *p;
  1037. p = obs_properties_add_int(props, "bitrate", obs_module_text("Bitrate"),
  1038. 0, 300000, 50);
  1039. obs_property_int_set_suffix(p, " Kbps");
  1040. p = obs_properties_add_int(
  1041. props, "maxrate", obs_module_text("MaxBitrate"), 0, 300000, 50);
  1042. obs_property_int_set_suffix(p, " Kbps");
  1043. obs_properties_add_int(props, "qp", "QP", 0, 51, 1);
  1044. p = obs_properties_add_int(props, "keyint_sec",
  1045. obs_module_text("KeyframeIntervalSec"), 0,
  1046. 20, 1);
  1047. obs_property_int_set_suffix(p, " s");
  1048. obs_properties_add_int(props, "bf", obs_module_text("BFrames"), 0, 4,
  1049. 1);
  1050. obs_properties_add_text(props, "ffmpeg_opts",
  1051. obs_module_text("FFmpegOpts"),
  1052. OBS_TEXT_DEFAULT);
  1053. return props;
  1054. }
  1055. static obs_properties_t *h264_vaapi_properties(void *unused)
  1056. {
  1057. UNUSED_PARAMETER(unused);
  1058. return vaapi_properties_internal(CODEC_H264);
  1059. }
  1060. static obs_properties_t *av1_vaapi_properties(void *unused)
  1061. {
  1062. UNUSED_PARAMETER(unused);
  1063. return vaapi_properties_internal(CODEC_AV1);
  1064. }
  1065. #ifdef ENABLE_HEVC
  1066. static obs_properties_t *hevc_vaapi_properties(void *unused)
  1067. {
  1068. UNUSED_PARAMETER(unused);
  1069. return vaapi_properties_internal(CODEC_HEVC);
  1070. }
  1071. #endif
  1072. static bool vaapi_extra_data(void *data, uint8_t **extra_data, size_t *size)
  1073. {
  1074. struct vaapi_encoder *enc = data;
  1075. *extra_data = enc->header;
  1076. *size = enc->header_size;
  1077. return true;
  1078. }
  1079. static bool vaapi_sei_data(void *data, uint8_t **extra_data, size_t *size)
  1080. {
  1081. struct vaapi_encoder *enc = data;
  1082. *extra_data = enc->sei;
  1083. *size = enc->sei_size;
  1084. return true;
  1085. }
  1086. struct obs_encoder_info h264_vaapi_encoder_info = {
  1087. .id = "ffmpeg_vaapi",
  1088. .type = OBS_ENCODER_VIDEO,
  1089. .codec = "h264",
  1090. .get_name = h264_vaapi_getname,
  1091. .create = h264_vaapi_create,
  1092. .destroy = vaapi_destroy,
  1093. .encode = vaapi_encode_copy,
  1094. .get_defaults = h264_vaapi_defaults,
  1095. .get_properties = h264_vaapi_properties,
  1096. .get_extra_data = vaapi_extra_data,
  1097. .get_sei_data = vaapi_sei_data,
  1098. .get_video_info = vaapi_video_info,
  1099. .caps = OBS_ENCODER_CAP_INTERNAL,
  1100. };
  1101. struct obs_encoder_info h264_vaapi_encoder_tex_info = {
  1102. .id = "ffmpeg_vaapi_tex",
  1103. .type = OBS_ENCODER_VIDEO,
  1104. .codec = "h264",
  1105. .get_name = h264_vaapi_getname,
  1106. .create = h264_vaapi_create_tex,
  1107. .destroy = vaapi_destroy,
  1108. .encode_texture2 = vaapi_encode_tex,
  1109. .get_defaults = h264_vaapi_defaults,
  1110. .get_properties = h264_vaapi_properties,
  1111. .get_extra_data = vaapi_extra_data,
  1112. .get_sei_data = vaapi_sei_data,
  1113. .get_video_info = vaapi_video_info,
  1114. .caps = OBS_ENCODER_CAP_PASS_TEXTURE,
  1115. };
  1116. struct obs_encoder_info av1_vaapi_encoder_info = {
  1117. .id = "av1_ffmpeg_vaapi",
  1118. .type = OBS_ENCODER_VIDEO,
  1119. .codec = "av1",
  1120. .get_name = av1_vaapi_getname,
  1121. .create = av1_vaapi_create,
  1122. .destroy = vaapi_destroy,
  1123. .encode = vaapi_encode_copy,
  1124. .get_defaults = av1_vaapi_defaults,
  1125. .get_properties = av1_vaapi_properties,
  1126. .get_extra_data = vaapi_extra_data,
  1127. .get_sei_data = vaapi_sei_data,
  1128. .get_video_info = vaapi_video_info,
  1129. .caps = OBS_ENCODER_CAP_INTERNAL,
  1130. };
  1131. struct obs_encoder_info av1_vaapi_encoder_tex_info = {
  1132. .id = "av1_ffmpeg_vaapi_tex",
  1133. .type = OBS_ENCODER_VIDEO,
  1134. .codec = "av1",
  1135. .get_name = av1_vaapi_getname,
  1136. .create = av1_vaapi_create_tex,
  1137. .destroy = vaapi_destroy,
  1138. .encode_texture2 = vaapi_encode_tex,
  1139. .get_defaults = av1_vaapi_defaults,
  1140. .get_properties = av1_vaapi_properties,
  1141. .get_extra_data = vaapi_extra_data,
  1142. .get_sei_data = vaapi_sei_data,
  1143. .get_video_info = vaapi_video_info,
  1144. .caps = OBS_ENCODER_CAP_PASS_TEXTURE,
  1145. };
  1146. #ifdef ENABLE_HEVC
  1147. struct obs_encoder_info hevc_vaapi_encoder_info = {
  1148. .id = "hevc_ffmpeg_vaapi",
  1149. .type = OBS_ENCODER_VIDEO,
  1150. .codec = "hevc",
  1151. .get_name = hevc_vaapi_getname,
  1152. .create = hevc_vaapi_create,
  1153. .destroy = vaapi_destroy,
  1154. .encode = vaapi_encode_copy,
  1155. .get_defaults = hevc_vaapi_defaults,
  1156. .get_properties = hevc_vaapi_properties,
  1157. .get_extra_data = vaapi_extra_data,
  1158. .get_sei_data = vaapi_sei_data,
  1159. .get_video_info = vaapi_video_info,
  1160. .caps = OBS_ENCODER_CAP_INTERNAL,
  1161. };
  1162. struct obs_encoder_info hevc_vaapi_encoder_tex_info = {
  1163. .id = "hevc_ffmpeg_vaapi_tex",
  1164. .type = OBS_ENCODER_VIDEO,
  1165. .codec = "hevc",
  1166. .get_name = hevc_vaapi_getname,
  1167. .create = hevc_vaapi_create_tex,
  1168. .destroy = vaapi_destroy,
  1169. .encode_texture2 = vaapi_encode_tex,
  1170. .get_defaults = hevc_vaapi_defaults,
  1171. .get_properties = hevc_vaapi_properties,
  1172. .get_extra_data = vaapi_extra_data,
  1173. .get_sei_data = vaapi_sei_data,
  1174. .get_video_info = vaapi_video_info,
  1175. .caps = OBS_ENCODER_CAP_PASS_TEXTURE,
  1176. };
  1177. #endif