obs-ffmpeg-vaapi.c 36 KB

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