obs-ffmpeg-vaapi.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386
  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. obs_enter_graphics();
  343. for (uint32_t i = 0; i < out->num_textures; ++i) {
  344. if (out->textures[i]) {
  345. gs_texture_destroy(out->textures[i]);
  346. out->textures[i] = NULL;
  347. }
  348. }
  349. obs_leave_graphics();
  350. av_frame_free(&out->frame);
  351. }
  352. static bool vaapi_create_surface(struct vaapi_encoder *enc,
  353. struct vaapi_surface *out)
  354. {
  355. int ret;
  356. VAStatus vas;
  357. VADRMPRIMESurfaceDescriptor desc;
  358. const AVPixFmtDescriptor *fmt_desc;
  359. bool ok = true;
  360. memset(out, 0, sizeof(*out));
  361. fmt_desc = av_pix_fmt_desc_get(enc->context->pix_fmt);
  362. if (!fmt_desc) {
  363. warn("Failed to get pix fmt descriptor");
  364. return false;
  365. }
  366. out->frame = av_frame_alloc();
  367. if (!out->frame) {
  368. warn("Failed to allocate hw frame");
  369. return false;
  370. }
  371. ret = av_hwframe_get_buffer(enc->vaframes_ref, out->frame, 0);
  372. if (ret < 0) {
  373. warn("Failed to get hw frame buffer: %s", av_err2str(ret));
  374. goto fail;
  375. }
  376. vas = vaExportSurfaceHandle(enc->va_dpy, (uintptr_t)out->frame->data[3],
  377. VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME_2,
  378. VA_EXPORT_SURFACE_WRITE_ONLY |
  379. VA_EXPORT_SURFACE_SEPARATE_LAYERS,
  380. &desc);
  381. if (vas != VA_STATUS_SUCCESS) {
  382. warn("Failed to export VA surface handle: %s", vaErrorStr(vas));
  383. goto fail;
  384. }
  385. obs_enter_graphics();
  386. for (uint32_t i = 0; i < desc.num_layers; ++i) {
  387. unsigned int width = desc.width;
  388. unsigned int height = desc.height;
  389. if (i) {
  390. width /= 1 << fmt_desc->log2_chroma_w;
  391. height /= 1 << fmt_desc->log2_chroma_h;
  392. }
  393. out->textures[i] = gs_texture_create_from_dmabuf(
  394. width, height, desc.layers[i].drm_format,
  395. drm_to_gs_color_format(desc.layers[i].drm_format), 1,
  396. &desc.objects[desc.layers[i].object_index[0]].fd,
  397. &desc.layers[i].pitch[0], &desc.layers[i].offset[0],
  398. &desc.objects[desc.layers[i].object_index[0]]
  399. .drm_format_modifier);
  400. if (!out->textures[i]) {
  401. warn("Failed to import VA surface texture");
  402. ok = false;
  403. }
  404. out->num_textures++;
  405. }
  406. obs_leave_graphics();
  407. for (uint32_t i = 0; i < desc.num_objects; ++i)
  408. close(desc.objects[i].fd);
  409. if (ok)
  410. return true;
  411. fail:
  412. vaapi_destroy_surface(out);
  413. return false;
  414. }
  415. static inline void flush_remaining_packets(struct vaapi_encoder *enc)
  416. {
  417. int r_pkt = 1;
  418. while (r_pkt) {
  419. if (avcodec_receive_packet(enc->context, enc->packet) < 0)
  420. break;
  421. if (r_pkt)
  422. av_packet_unref(enc->packet);
  423. }
  424. }
  425. static void vaapi_destroy(void *data)
  426. {
  427. struct vaapi_encoder *enc = data;
  428. if (enc->initialized)
  429. flush_remaining_packets(enc);
  430. av_packet_free(&enc->packet);
  431. avcodec_free_context(&enc->context);
  432. av_frame_unref(enc->vframe);
  433. av_frame_free(&enc->vframe);
  434. av_buffer_unref(&enc->vaframes_ref);
  435. av_buffer_unref(&enc->vadevice_ref);
  436. da_free(enc->buffer);
  437. bfree(enc->header);
  438. bfree(enc->sei);
  439. bfree(enc);
  440. }
  441. static inline const char *vaapi_encoder_name(enum codec_type codec)
  442. {
  443. if (codec == CODEC_H264) {
  444. return "h264_vaapi";
  445. } else if (codec == CODEC_HEVC) {
  446. return "hevc_vaapi";
  447. } else if (codec == CODEC_AV1) {
  448. return "av1_vaapi";
  449. }
  450. return NULL;
  451. }
  452. static void *vaapi_create_internal(obs_data_t *settings, obs_encoder_t *encoder,
  453. enum codec_type codec)
  454. {
  455. struct vaapi_encoder *enc;
  456. enc = bzalloc(sizeof(*enc));
  457. enc->encoder = encoder;
  458. enc->codec = codec;
  459. enc->vaapi = avcodec_find_encoder_by_name(vaapi_encoder_name(codec));
  460. enc->first_packet = true;
  461. blog(LOG_INFO, "---------------------------------");
  462. if (!enc->vaapi) {
  463. warn("Couldn't find encoder");
  464. goto fail;
  465. }
  466. enc->context = avcodec_alloc_context3(enc->vaapi);
  467. if (!enc->context) {
  468. warn("Failed to create codec context");
  469. goto fail;
  470. }
  471. if (!vaapi_update(enc, settings))
  472. goto fail;
  473. return enc;
  474. fail:
  475. vaapi_destroy(enc);
  476. return NULL;
  477. }
  478. static inline bool vaapi_test_texencode(struct vaapi_encoder *enc)
  479. {
  480. struct vaapi_surface surface;
  481. if (obs_encoder_scaling_enabled(enc->encoder) &&
  482. !obs_encoder_gpu_scaling_enabled(enc->encoder))
  483. return false;
  484. if (!vaapi_create_surface(enc, &surface))
  485. return false;
  486. vaapi_destroy_surface(&surface);
  487. return true;
  488. }
  489. static void *vaapi_create_tex_internal(obs_data_t *settings,
  490. obs_encoder_t *encoder,
  491. enum codec_type codec,
  492. const char *fallback)
  493. {
  494. void *enc = vaapi_create_internal(settings, encoder, codec);
  495. if (!enc) {
  496. return NULL;
  497. }
  498. if (!vaapi_test_texencode(enc)) {
  499. vaapi_destroy(enc);
  500. blog(LOG_WARNING, "VAAPI: Falling back to %s encoder",
  501. fallback);
  502. return obs_encoder_create_rerouted(encoder, fallback);
  503. }
  504. return enc;
  505. }
  506. static void *h264_vaapi_create(obs_data_t *settings, obs_encoder_t *encoder)
  507. {
  508. return vaapi_create_internal(settings, encoder, CODEC_H264);
  509. }
  510. static void *h264_vaapi_create_tex(obs_data_t *settings, obs_encoder_t *encoder)
  511. {
  512. return vaapi_create_tex_internal(settings, encoder, CODEC_H264,
  513. "ffmpeg_vaapi");
  514. }
  515. static void *av1_vaapi_create(obs_data_t *settings, obs_encoder_t *encoder)
  516. {
  517. return vaapi_create_internal(settings, encoder, CODEC_AV1);
  518. }
  519. static void *av1_vaapi_create_tex(obs_data_t *settings, obs_encoder_t *encoder)
  520. {
  521. return vaapi_create_tex_internal(settings, encoder, CODEC_AV1,
  522. "av1_ffmpeg_vaapi");
  523. }
  524. #ifdef ENABLE_HEVC
  525. static void *hevc_vaapi_create(obs_data_t *settings, obs_encoder_t *encoder)
  526. {
  527. return vaapi_create_internal(settings, encoder, CODEC_HEVC);
  528. }
  529. static void *hevc_vaapi_create_tex(obs_data_t *settings, obs_encoder_t *encoder)
  530. {
  531. return vaapi_create_tex_internal(settings, encoder, CODEC_HEVC,
  532. "hevc_ffmpeg_vaapi");
  533. }
  534. #endif
  535. static inline void copy_data(AVFrame *pic, const struct encoder_frame *frame,
  536. int height, enum AVPixelFormat format)
  537. {
  538. int h_chroma_shift, v_chroma_shift;
  539. av_pix_fmt_get_chroma_sub_sample(format, &h_chroma_shift,
  540. &v_chroma_shift);
  541. for (int plane = 0; plane < MAX_AV_PLANES; plane++) {
  542. if (!frame->data[plane])
  543. continue;
  544. int frame_rowsize = (int)frame->linesize[plane];
  545. int pic_rowsize = pic->linesize[plane];
  546. int bytes = frame_rowsize < pic_rowsize ? frame_rowsize
  547. : pic_rowsize;
  548. int plane_height = height >> (plane ? v_chroma_shift : 0);
  549. for (int y = 0; y < plane_height; y++) {
  550. int pos_frame = y * frame_rowsize;
  551. int pos_pic = y * pic_rowsize;
  552. memcpy(pic->data[plane] + pos_pic,
  553. frame->data[plane] + pos_frame, bytes);
  554. }
  555. }
  556. }
  557. static bool vaapi_encode_internal(struct vaapi_encoder *enc, AVFrame *frame,
  558. struct encoder_packet *packet,
  559. bool *received_packet)
  560. {
  561. int got_packet;
  562. int ret;
  563. ret = avcodec_send_frame(enc->context, frame);
  564. if (ret == 0 || ret == AVERROR(EAGAIN))
  565. ret = avcodec_receive_packet(enc->context, enc->packet);
  566. got_packet = (ret == 0);
  567. if (ret == AVERROR_EOF || ret == AVERROR(EAGAIN))
  568. ret = 0;
  569. if (ret < 0) {
  570. warn("vaapi_encode: Error encoding: %s", av_err2str(ret));
  571. goto fail;
  572. }
  573. if (got_packet && enc->packet->size) {
  574. if (enc->first_packet) {
  575. uint8_t *new_packet;
  576. size_t size;
  577. enc->first_packet = false;
  578. switch (enc->codec) {
  579. case CODEC_HEVC:
  580. #ifdef ENABLE_HEVC
  581. obs_extract_hevc_headers(
  582. enc->packet->data, enc->packet->size,
  583. &new_packet, &size, &enc->header,
  584. &enc->header_size, &enc->sei,
  585. &enc->sei_size);
  586. break;
  587. #else
  588. warn("vaapi_encode: HEVC codec is not supported");
  589. goto fail;
  590. #endif
  591. case CODEC_H264:
  592. obs_extract_avc_headers(
  593. enc->packet->data, enc->packet->size,
  594. &new_packet, &size, &enc->header,
  595. &enc->header_size, &enc->sei,
  596. &enc->sei_size);
  597. break;
  598. case CODEC_AV1:
  599. obs_extract_av1_headers(enc->packet->data,
  600. enc->packet->size,
  601. &new_packet, &size,
  602. &enc->header,
  603. &enc->header_size);
  604. break;
  605. }
  606. da_copy_array(enc->buffer, new_packet, size);
  607. bfree(new_packet);
  608. } else {
  609. da_copy_array(enc->buffer, enc->packet->data,
  610. enc->packet->size);
  611. }
  612. packet->pts = enc->packet->pts;
  613. packet->dts = enc->packet->dts;
  614. packet->data = enc->buffer.array;
  615. packet->size = enc->buffer.num;
  616. packet->type = OBS_ENCODER_VIDEO;
  617. #ifdef ENABLE_HEVC
  618. if (enc->codec == CODEC_HEVC) {
  619. packet->keyframe =
  620. obs_hevc_keyframe(packet->data, packet->size);
  621. } else
  622. #endif
  623. if (enc->codec == CODEC_H264) {
  624. packet->keyframe =
  625. obs_avc_keyframe(packet->data, packet->size);
  626. } else if (enc->codec == CODEC_AV1) {
  627. packet->keyframe =
  628. obs_av1_keyframe(packet->data, packet->size);
  629. }
  630. *received_packet = true;
  631. }
  632. av_packet_unref(enc->packet);
  633. return true;
  634. fail:
  635. av_packet_unref(enc->packet);
  636. return false;
  637. }
  638. static bool vaapi_encode_copy(void *data, struct encoder_frame *frame,
  639. struct encoder_packet *packet,
  640. bool *received_packet)
  641. {
  642. struct vaapi_encoder *enc = data;
  643. AVFrame *hwframe = NULL;
  644. int ret;
  645. *received_packet = false;
  646. hwframe = av_frame_alloc();
  647. if (!hwframe) {
  648. warn("vaapi_encode_copy: failed to allocate hw frame");
  649. return false;
  650. }
  651. ret = av_hwframe_get_buffer(enc->vaframes_ref, hwframe, 0);
  652. if (ret < 0) {
  653. warn("vaapi_encode_copy: failed to get buffer for hw frame: %s",
  654. av_err2str(ret));
  655. goto fail;
  656. }
  657. copy_data(enc->vframe, frame, enc->height, enc->context->pix_fmt);
  658. enc->vframe->pts = frame->pts;
  659. hwframe->pts = frame->pts;
  660. hwframe->width = enc->vframe->width;
  661. hwframe->height = enc->vframe->height;
  662. ret = av_hwframe_transfer_data(hwframe, enc->vframe, 0);
  663. if (ret < 0) {
  664. warn("vaapi_encode_copy: failed to upload hw frame: %s",
  665. av_err2str(ret));
  666. goto fail;
  667. }
  668. ret = av_frame_copy_props(hwframe, enc->vframe);
  669. if (ret < 0) {
  670. warn("vaapi_encode_copy: failed to copy props to hw frame: %s",
  671. av_err2str(ret));
  672. goto fail;
  673. }
  674. if (!vaapi_encode_internal(enc, hwframe, packet, received_packet))
  675. goto fail;
  676. av_frame_free(&hwframe);
  677. return true;
  678. fail:
  679. av_frame_free(&hwframe);
  680. return false;
  681. }
  682. static bool vaapi_encode_tex(void *data, struct encoder_texture *texture,
  683. int64_t pts, uint64_t lock_key, uint64_t *next_key,
  684. struct encoder_packet *packet,
  685. bool *received_packet)
  686. {
  687. UNUSED_PARAMETER(lock_key);
  688. UNUSED_PARAMETER(next_key);
  689. struct vaapi_encoder *enc = data;
  690. struct vaapi_surface surface;
  691. int ret;
  692. *received_packet = false;
  693. if (!vaapi_create_surface(enc, &surface)) {
  694. warn("vaapi_encode_tex: failed to create texture hw frame");
  695. return false;
  696. }
  697. obs_enter_graphics();
  698. for (uint32_t i = 0; i < surface.num_textures; ++i) {
  699. if (!texture->tex[i]) {
  700. warn("vaapi_encode_tex: unexpected number of textures");
  701. goto fail;
  702. }
  703. gs_copy_texture(surface.textures[i], texture->tex[i]);
  704. }
  705. gs_flush();
  706. obs_leave_graphics();
  707. enc->vframe->pts = pts;
  708. ret = av_frame_copy_props(surface.frame, enc->vframe);
  709. if (ret < 0) {
  710. warn("vaapi_encode_tex: failed to copy props to hw frame: %s",
  711. av_err2str(ret));
  712. goto fail;
  713. }
  714. if (!vaapi_encode_internal(enc, surface.frame, packet, received_packet))
  715. goto fail;
  716. vaapi_destroy_surface(&surface);
  717. return true;
  718. fail:
  719. vaapi_destroy_surface(&surface);
  720. return false;
  721. }
  722. static void set_visible(obs_properties_t *ppts, const char *name, bool visible)
  723. {
  724. obs_property_t *p = obs_properties_get(ppts, name);
  725. obs_property_set_visible(p, visible);
  726. }
  727. static inline VAProfile vaapi_profile(enum codec_type codec)
  728. {
  729. if (codec == CODEC_H264) {
  730. return VAProfileH264ConstrainedBaseline;
  731. } else if (codec == CODEC_AV1) {
  732. return VAProfileAV1Profile0;
  733. #if ENABLE_HEVC
  734. } else if (codec == CODEC_HEVC) {
  735. return VAProfileHEVCMain;
  736. #endif
  737. }
  738. return VAProfileNone;
  739. }
  740. static inline const char *vaapi_default_device(enum codec_type codec)
  741. {
  742. if (codec == CODEC_H264) {
  743. return vaapi_get_h264_default_device();
  744. } else if (codec == CODEC_AV1) {
  745. return vaapi_get_av1_default_device();
  746. #if ENABLE_HEVC
  747. } else if (codec == CODEC_HEVC) {
  748. return vaapi_get_hevc_default_device();
  749. #endif
  750. }
  751. return NULL;
  752. }
  753. static void vaapi_defaults_internal(obs_data_t *settings, enum codec_type codec)
  754. {
  755. const char *const device = vaapi_default_device(codec);
  756. obs_data_set_default_string(settings, "vaapi_device", device);
  757. #ifdef ENABLE_HEVC
  758. if (codec == CODEC_HEVC)
  759. obs_data_set_default_int(settings, "profile",
  760. FF_PROFILE_HEVC_MAIN);
  761. else
  762. #endif
  763. if (codec == CODEC_H264)
  764. obs_data_set_default_int(settings, "profile",
  765. FF_PROFILE_H264_HIGH);
  766. else if (codec == CODEC_AV1)
  767. obs_data_set_default_int(settings, "profile",
  768. FF_PROFILE_AV1_MAIN);
  769. obs_data_set_default_int(settings, "level", FF_LEVEL_UNKNOWN);
  770. obs_data_set_default_int(settings, "bitrate", 2500);
  771. obs_data_set_default_int(settings, "keyint_sec", 0);
  772. obs_data_set_default_int(settings, "bf", 0);
  773. obs_data_set_default_int(settings, "qp", 20);
  774. obs_data_set_default_int(settings, "maxrate", 0);
  775. int drm_fd = -1;
  776. VADisplay va_dpy = vaapi_open_device(&drm_fd, device, "vaapi_defaults");
  777. if (!va_dpy)
  778. return;
  779. const VAProfile profile = vaapi_profile(codec);
  780. if (vaapi_device_rc_supported(profile, va_dpy, VA_RC_CBR, device))
  781. obs_data_set_default_string(settings, "rate_control", "CBR");
  782. else if (vaapi_device_rc_supported(profile, va_dpy, VA_RC_VBR, device))
  783. obs_data_set_default_string(settings, "rate_control", "VBR");
  784. else
  785. obs_data_set_default_string(settings, "rate_control", "CQP");
  786. vaapi_close_device(&drm_fd, va_dpy);
  787. }
  788. static void h264_vaapi_defaults(obs_data_t *settings)
  789. {
  790. vaapi_defaults_internal(settings, CODEC_H264);
  791. }
  792. static void av1_vaapi_defaults(obs_data_t *settings)
  793. {
  794. vaapi_defaults_internal(settings, CODEC_AV1);
  795. }
  796. static void hevc_vaapi_defaults(obs_data_t *settings)
  797. {
  798. vaapi_defaults_internal(settings, CODEC_HEVC);
  799. }
  800. static bool vaapi_device_modified(obs_properties_t *ppts, obs_property_t *p,
  801. obs_data_t *settings)
  802. {
  803. UNUSED_PARAMETER(p);
  804. const char *device = obs_data_get_string(settings, "vaapi_device");
  805. int drm_fd = -1;
  806. VADisplay va_dpy =
  807. vaapi_open_device(&drm_fd, device, "vaapi_device_modified");
  808. int profile = obs_data_get_int(settings, "profile");
  809. obs_property_t *rc_p = obs_properties_get(ppts, "rate_control");
  810. obs_property_list_clear(rc_p);
  811. if (!va_dpy)
  812. goto fail;
  813. switch (profile) {
  814. case FF_PROFILE_H264_CONSTRAINED_BASELINE:
  815. if (!vaapi_display_h264_supported(va_dpy, device))
  816. goto fail;
  817. profile = VAProfileH264ConstrainedBaseline;
  818. break;
  819. case FF_PROFILE_H264_MAIN:
  820. if (!vaapi_display_h264_supported(va_dpy, device))
  821. goto fail;
  822. profile = VAProfileH264Main;
  823. break;
  824. case FF_PROFILE_H264_HIGH:
  825. if (!vaapi_display_h264_supported(va_dpy, device))
  826. goto fail;
  827. profile = VAProfileH264High;
  828. break;
  829. case FF_PROFILE_AV1_MAIN:
  830. if (!vaapi_display_av1_supported(va_dpy, device))
  831. goto fail;
  832. profile = VAProfileAV1Profile0;
  833. break;
  834. #ifdef ENABLE_HEVC
  835. case FF_PROFILE_HEVC_MAIN:
  836. if (!vaapi_display_hevc_supported(va_dpy, device))
  837. goto fail;
  838. profile = VAProfileHEVCMain;
  839. break;
  840. case FF_PROFILE_HEVC_MAIN_10:
  841. if (!vaapi_display_hevc_supported(va_dpy, device))
  842. goto fail;
  843. profile = VAProfileHEVCMain10;
  844. break;
  845. #endif
  846. }
  847. if (vaapi_device_rc_supported(profile, va_dpy, VA_RC_CBR, device))
  848. obs_property_list_add_string(rc_p, "CBR", "CBR");
  849. if (vaapi_device_rc_supported(profile, va_dpy, VA_RC_VBR, device))
  850. obs_property_list_add_string(rc_p, "VBR", "VBR");
  851. if (vaapi_device_rc_supported(profile, va_dpy, VA_RC_CQP, device))
  852. obs_property_list_add_string(rc_p, "CQP", "CQP");
  853. set_visible(ppts, "bf", vaapi_device_bframe_supported(profile, va_dpy));
  854. fail:
  855. vaapi_close_device(&drm_fd, va_dpy);
  856. return true;
  857. }
  858. static bool rate_control_modified(obs_properties_t *ppts, obs_property_t *p,
  859. obs_data_t *settings)
  860. {
  861. UNUSED_PARAMETER(p);
  862. const char *rate_control =
  863. obs_data_get_string(settings, "rate_control");
  864. const rc_mode_t *rc_mode = get_rc_mode(rate_control);
  865. /* Set options visibility per Rate Control */
  866. set_visible(ppts, "qp", rc_mode->qp);
  867. set_visible(ppts, "bitrate", rc_mode->bitrate);
  868. set_visible(ppts, "maxrate", rc_mode->maxrate);
  869. return true;
  870. }
  871. static bool get_device_name_from_pci(struct pci_access *pacc, char *pci_slot,
  872. char *buf, int size)
  873. {
  874. struct pci_filter filter;
  875. struct pci_dev *dev;
  876. char *name;
  877. pci_filter_init(pacc, &filter);
  878. if (pci_filter_parse_slot(&filter, pci_slot))
  879. return false;
  880. pci_scan_bus(pacc);
  881. for (dev = pacc->devices; dev; dev = dev->next) {
  882. if (pci_filter_match(&filter, dev)) {
  883. pci_fill_info(dev, PCI_FILL_IDENT);
  884. name = pci_lookup_name(pacc, buf, size,
  885. PCI_LOOKUP_DEVICE,
  886. dev->vendor_id, dev->device_id);
  887. strcpy(buf, name);
  888. return true;
  889. }
  890. }
  891. return false;
  892. }
  893. static obs_properties_t *vaapi_properties_internal(enum codec_type codec)
  894. {
  895. obs_properties_t *props = obs_properties_create();
  896. obs_property_t *list;
  897. list = obs_properties_add_list(props, "vaapi_device",
  898. obs_module_text("VAAPI.Device"),
  899. OBS_COMBO_TYPE_LIST,
  900. OBS_COMBO_FORMAT_STRING);
  901. if (os_file_exists("/dev/dri/by-path")) {
  902. os_dir_t *by_path_dir = os_opendir("/dev/dri/by-path");
  903. struct pci_access *pacc = pci_alloc();
  904. struct os_dirent *file;
  905. char namebuf[1024];
  906. char pci_slot[13];
  907. char *type;
  908. pci_init(pacc);
  909. while ((file = os_readdir(by_path_dir)) != NULL) {
  910. // file_name pattern: pci-<pci_slot::12>-<type::{"card","render"}>
  911. char *file_name = file->d_name;
  912. if (strcmp(file_name, ".") == 0 ||
  913. strcmp(file_name, "..") == 0)
  914. continue;
  915. char path[64] = {0};
  916. // Use the return value of snprintf to prevent truncation warning.
  917. int written = snprintf(path, 64, "/dev/dri/by-path/%s",
  918. file_name);
  919. if (written >= 64)
  920. blog(LOG_DEBUG,
  921. "obs-ffmpeg-vaapi: A format truncation may have occurred."
  922. " This can be ignored since it is quite improbable.");
  923. type = strrchr(file_name, '-');
  924. if (type == NULL)
  925. continue;
  926. else
  927. type++;
  928. if (strcmp(type, "render") == 0) {
  929. strncpy(pci_slot, file_name + 4, 12);
  930. pci_slot[12] = 0;
  931. bool name_found = get_device_name_from_pci(
  932. pacc, pci_slot, namebuf,
  933. sizeof(namebuf));
  934. if (!vaapi_device_h264_supported(path))
  935. continue;
  936. if (!name_found)
  937. obs_property_list_add_string(list, path,
  938. path);
  939. else
  940. obs_property_list_add_string(
  941. list, namebuf, path);
  942. }
  943. }
  944. pci_cleanup(pacc);
  945. os_closedir(by_path_dir);
  946. }
  947. if (obs_property_list_item_count(list) == 0) {
  948. char path[32];
  949. for (int i = 28;; i++) {
  950. snprintf(path, sizeof(path), "/dev/dri/renderD1%d", i);
  951. if (access(path, F_OK) == 0) {
  952. char card[128];
  953. int ret = snprintf(card, sizeof(card),
  954. "Card%d: %s", i - 28, path);
  955. if (ret >= (int)sizeof(card))
  956. blog(LOG_DEBUG,
  957. "obs-ffmpeg-vaapi: A format truncation may have occurred."
  958. " This can be ignored since it is quite improbable.");
  959. if (!vaapi_device_h264_supported(path))
  960. continue;
  961. obs_property_list_add_string(list, card, path);
  962. } else {
  963. break;
  964. }
  965. }
  966. }
  967. obs_property_set_modified_callback(list, vaapi_device_modified);
  968. list = obs_properties_add_list(props, "profile",
  969. obs_module_text("Profile"),
  970. OBS_COMBO_TYPE_LIST,
  971. OBS_COMBO_FORMAT_INT);
  972. if (codec == CODEC_HEVC) {
  973. obs_property_list_add_int(list, "Main", FF_PROFILE_HEVC_MAIN);
  974. obs_property_list_add_int(list, "Main10",
  975. FF_PROFILE_HEVC_MAIN_10);
  976. } else if (codec == CODEC_H264) {
  977. obs_property_list_add_int(list, "Constrained Baseline",
  978. FF_PROFILE_H264_CONSTRAINED_BASELINE);
  979. obs_property_list_add_int(list, "Main", FF_PROFILE_H264_MAIN);
  980. obs_property_list_add_int(list, "High", FF_PROFILE_H264_HIGH);
  981. } else if (codec == CODEC_AV1) {
  982. obs_property_list_add_int(list, "Main", FF_PROFILE_AV1_MAIN);
  983. }
  984. obs_property_set_modified_callback(list, vaapi_device_modified);
  985. list = obs_properties_add_list(props, "level", obs_module_text("Level"),
  986. OBS_COMBO_TYPE_LIST,
  987. OBS_COMBO_FORMAT_INT);
  988. obs_property_list_add_int(list, "Auto", FF_LEVEL_UNKNOWN);
  989. if (codec == CODEC_H264) {
  990. obs_property_list_add_int(list, "3.0", 30);
  991. obs_property_list_add_int(list, "3.1", 31);
  992. obs_property_list_add_int(list, "4.0", 40);
  993. obs_property_list_add_int(list, "4.1", 41);
  994. obs_property_list_add_int(list, "4.2", 42);
  995. obs_property_list_add_int(list, "5.0", 50);
  996. obs_property_list_add_int(list, "5.1", 51);
  997. obs_property_list_add_int(list, "5.2", 52);
  998. } else if (codec == CODEC_HEVC) {
  999. obs_property_list_add_int(list, "3.0", 90);
  1000. obs_property_list_add_int(list, "3.1", 93);
  1001. obs_property_list_add_int(list, "4.0", 120);
  1002. obs_property_list_add_int(list, "4.1", 123);
  1003. obs_property_list_add_int(list, "5.0", 150);
  1004. obs_property_list_add_int(list, "5.1", 153);
  1005. obs_property_list_add_int(list, "5.2", 156);
  1006. } else if (codec == CODEC_AV1) {
  1007. obs_property_list_add_int(list, "3.0", 4);
  1008. obs_property_list_add_int(list, "3.1", 5);
  1009. obs_property_list_add_int(list, "4.0", 8);
  1010. obs_property_list_add_int(list, "4.1", 9);
  1011. obs_property_list_add_int(list, "5.0", 12);
  1012. obs_property_list_add_int(list, "5.1", 13);
  1013. obs_property_list_add_int(list, "5.2", 14);
  1014. obs_property_list_add_int(list, "5.3", 15);
  1015. }
  1016. list = obs_properties_add_list(props, "rate_control",
  1017. obs_module_text("RateControl"),
  1018. OBS_COMBO_TYPE_LIST,
  1019. OBS_COMBO_FORMAT_STRING);
  1020. obs_property_set_modified_callback(list, rate_control_modified);
  1021. obs_property_t *p;
  1022. p = obs_properties_add_int(props, "bitrate", obs_module_text("Bitrate"),
  1023. 0, 300000, 50);
  1024. obs_property_int_set_suffix(p, " Kbps");
  1025. p = obs_properties_add_int(
  1026. props, "maxrate", obs_module_text("MaxBitrate"), 0, 300000, 50);
  1027. obs_property_int_set_suffix(p, " Kbps");
  1028. obs_properties_add_int(props, "qp", "QP", 0, 51, 1);
  1029. p = obs_properties_add_int(props, "keyint_sec",
  1030. obs_module_text("KeyframeIntervalSec"), 0,
  1031. 20, 1);
  1032. obs_property_int_set_suffix(p, " s");
  1033. obs_properties_add_int(props, "bf", obs_module_text("BFrames"), 0, 4,
  1034. 1);
  1035. obs_properties_add_text(props, "ffmpeg_opts",
  1036. obs_module_text("FFmpegOpts"),
  1037. OBS_TEXT_DEFAULT);
  1038. return props;
  1039. }
  1040. static obs_properties_t *h264_vaapi_properties(void *unused)
  1041. {
  1042. UNUSED_PARAMETER(unused);
  1043. return vaapi_properties_internal(CODEC_H264);
  1044. }
  1045. static obs_properties_t *av1_vaapi_properties(void *unused)
  1046. {
  1047. UNUSED_PARAMETER(unused);
  1048. return vaapi_properties_internal(CODEC_AV1);
  1049. }
  1050. #ifdef ENABLE_HEVC
  1051. static obs_properties_t *hevc_vaapi_properties(void *unused)
  1052. {
  1053. UNUSED_PARAMETER(unused);
  1054. return vaapi_properties_internal(CODEC_HEVC);
  1055. }
  1056. #endif
  1057. static bool vaapi_extra_data(void *data, uint8_t **extra_data, size_t *size)
  1058. {
  1059. struct vaapi_encoder *enc = data;
  1060. *extra_data = enc->header;
  1061. *size = enc->header_size;
  1062. return true;
  1063. }
  1064. static bool vaapi_sei_data(void *data, uint8_t **extra_data, size_t *size)
  1065. {
  1066. struct vaapi_encoder *enc = data;
  1067. *extra_data = enc->sei;
  1068. *size = enc->sei_size;
  1069. return true;
  1070. }
  1071. struct obs_encoder_info h264_vaapi_encoder_info = {
  1072. .id = "ffmpeg_vaapi",
  1073. .type = OBS_ENCODER_VIDEO,
  1074. .codec = "h264",
  1075. .get_name = h264_vaapi_getname,
  1076. .create = h264_vaapi_create,
  1077. .destroy = vaapi_destroy,
  1078. .encode = vaapi_encode_copy,
  1079. .get_defaults = h264_vaapi_defaults,
  1080. .get_properties = h264_vaapi_properties,
  1081. .get_extra_data = vaapi_extra_data,
  1082. .get_sei_data = vaapi_sei_data,
  1083. .get_video_info = vaapi_video_info,
  1084. .caps = OBS_ENCODER_CAP_INTERNAL,
  1085. };
  1086. struct obs_encoder_info h264_vaapi_encoder_tex_info = {
  1087. .id = "ffmpeg_vaapi_tex",
  1088. .type = OBS_ENCODER_VIDEO,
  1089. .codec = "h264",
  1090. .get_name = h264_vaapi_getname,
  1091. .create = h264_vaapi_create_tex,
  1092. .destroy = vaapi_destroy,
  1093. .encode_texture2 = vaapi_encode_tex,
  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_PASS_TEXTURE,
  1100. };
  1101. struct obs_encoder_info av1_vaapi_encoder_info = {
  1102. .id = "av1_ffmpeg_vaapi",
  1103. .type = OBS_ENCODER_VIDEO,
  1104. .codec = "av1",
  1105. .get_name = av1_vaapi_getname,
  1106. .create = av1_vaapi_create,
  1107. .destroy = vaapi_destroy,
  1108. .encode = vaapi_encode_copy,
  1109. .get_defaults = av1_vaapi_defaults,
  1110. .get_properties = av1_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_INTERNAL,
  1115. };
  1116. struct obs_encoder_info av1_vaapi_encoder_tex_info = {
  1117. .id = "av1_ffmpeg_vaapi_tex",
  1118. .type = OBS_ENCODER_VIDEO,
  1119. .codec = "av1",
  1120. .get_name = av1_vaapi_getname,
  1121. .create = av1_vaapi_create_tex,
  1122. .destroy = vaapi_destroy,
  1123. .encode_texture2 = vaapi_encode_tex,
  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_PASS_TEXTURE,
  1130. };
  1131. #ifdef ENABLE_HEVC
  1132. struct obs_encoder_info hevc_vaapi_encoder_info = {
  1133. .id = "hevc_ffmpeg_vaapi",
  1134. .type = OBS_ENCODER_VIDEO,
  1135. .codec = "hevc",
  1136. .get_name = hevc_vaapi_getname,
  1137. .create = hevc_vaapi_create,
  1138. .destroy = vaapi_destroy,
  1139. .encode = vaapi_encode_copy,
  1140. .get_defaults = hevc_vaapi_defaults,
  1141. .get_properties = hevc_vaapi_properties,
  1142. .get_extra_data = vaapi_extra_data,
  1143. .get_sei_data = vaapi_sei_data,
  1144. .get_video_info = vaapi_video_info,
  1145. .caps = OBS_ENCODER_CAP_INTERNAL,
  1146. };
  1147. struct obs_encoder_info hevc_vaapi_encoder_tex_info = {
  1148. .id = "hevc_ffmpeg_vaapi_tex",
  1149. .type = OBS_ENCODER_VIDEO,
  1150. .codec = "hevc",
  1151. .get_name = hevc_vaapi_getname,
  1152. .create = hevc_vaapi_create_tex,
  1153. .destroy = vaapi_destroy,
  1154. .encode_texture2 = vaapi_encode_tex,
  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_PASS_TEXTURE,
  1161. };
  1162. #endif