obs-ffmpeg-vaapi.c 36 KB

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