obs-ffmpeg-vaapi.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  1. /******************************************************************************
  2. Copyright (C) 2016 by Hugh Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include <libavutil/avutil.h>
  15. #if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(55, 27, 100)
  16. #include <util/darray.h>
  17. #include <util/dstr.h>
  18. #include <util/base.h>
  19. #include <util/platform.h>
  20. #include <media-io/video-io.h>
  21. #include <obs-module.h>
  22. #include <obs-avc.h>
  23. #include <unistd.h>
  24. #include <libavutil/opt.h>
  25. #include <libavutil/pixdesc.h>
  26. #include <libavutil/hwcontext.h>
  27. #include <libavcodec/avcodec.h>
  28. #include <libavformat/avformat.h>
  29. #include <libavfilter/avfilter.h>
  30. #include <pci/pci.h>
  31. #include "obs-ffmpeg-formats.h"
  32. #define do_log(level, format, ...) \
  33. blog(level, "[FFMPEG VAAPI encoder: '%s'] " format, \
  34. obs_encoder_get_name(enc->encoder), ##__VA_ARGS__)
  35. #define warn(format, ...) do_log(LOG_WARNING, format, ##__VA_ARGS__)
  36. #define info(format, ...) do_log(LOG_INFO, format, ##__VA_ARGS__)
  37. #define debug(format, ...) do_log(LOG_DEBUG, format, ##__VA_ARGS__)
  38. struct vaapi_encoder {
  39. obs_encoder_t *encoder;
  40. AVBufferRef *vadevice_ref;
  41. AVBufferRef *vaframes_ref;
  42. const AVCodec *vaapi;
  43. AVCodecContext *context;
  44. AVPacket *packet;
  45. AVFrame *vframe;
  46. DARRAY(uint8_t) buffer;
  47. uint8_t *header;
  48. size_t header_size;
  49. uint8_t *sei;
  50. size_t sei_size;
  51. int height;
  52. bool first_packet;
  53. bool initialized;
  54. };
  55. static const char *vaapi_getname(void *unused)
  56. {
  57. UNUSED_PARAMETER(unused);
  58. return "FFMPEG VAAPI H.264";
  59. }
  60. static inline bool valid_format(enum video_format format)
  61. {
  62. return format == VIDEO_FORMAT_NV12;
  63. }
  64. static void vaapi_video_info(void *data, struct video_scale_info *info)
  65. {
  66. struct vaapi_encoder *enc = data;
  67. enum video_format pref_format;
  68. pref_format = obs_encoder_get_preferred_video_format(enc->encoder);
  69. if (!valid_format(pref_format)) {
  70. pref_format = valid_format(info->format) ? info->format
  71. : VIDEO_FORMAT_NV12;
  72. }
  73. info->format = pref_format;
  74. }
  75. static bool vaapi_init_codec(struct vaapi_encoder *enc, const char *path)
  76. {
  77. int ret;
  78. ret = av_hwdevice_ctx_create(&enc->vadevice_ref, AV_HWDEVICE_TYPE_VAAPI,
  79. path, NULL, 0);
  80. if (ret < 0) {
  81. warn("Failed to create VAAPI device context: %s",
  82. av_err2str(ret));
  83. return false;
  84. }
  85. enc->vaframes_ref = av_hwframe_ctx_alloc(enc->vadevice_ref);
  86. if (!enc->vaframes_ref) {
  87. warn("Failed to alloc HW frames context");
  88. return false;
  89. }
  90. AVHWFramesContext *frames_ctx =
  91. (AVHWFramesContext *)enc->vaframes_ref->data;
  92. frames_ctx->format = AV_PIX_FMT_VAAPI;
  93. frames_ctx->sw_format = AV_PIX_FMT_NV12;
  94. frames_ctx->width = enc->context->width;
  95. frames_ctx->height = enc->context->height;
  96. frames_ctx->initial_pool_size = 20;
  97. ret = av_hwframe_ctx_init(enc->vaframes_ref);
  98. if (ret < 0) {
  99. warn("Failed to init HW frames context: %s", av_err2str(ret));
  100. return false;
  101. }
  102. /* 2. Create software frame and picture */
  103. enc->vframe = av_frame_alloc();
  104. if (!enc->vframe) {
  105. warn("Failed to allocate video frame");
  106. return false;
  107. }
  108. enc->vframe->format = enc->context->pix_fmt;
  109. enc->vframe->width = enc->context->width;
  110. enc->vframe->height = enc->context->height;
  111. enc->vframe->colorspace = enc->context->colorspace;
  112. enc->vframe->color_range = enc->context->color_range;
  113. ret = av_frame_get_buffer(enc->vframe, base_get_alignment());
  114. if (ret < 0) {
  115. warn("Failed to allocate vframe: %s", av_err2str(ret));
  116. return false;
  117. }
  118. /* 3. set up codec */
  119. enc->context->pix_fmt = AV_PIX_FMT_VAAPI;
  120. enc->context->hw_frames_ctx = av_buffer_ref(enc->vaframes_ref);
  121. ret = avcodec_open2(enc->context, enc->vaapi, NULL);
  122. if (ret < 0) {
  123. warn("Failed to open VAAPI codec: %s", av_err2str(ret));
  124. return false;
  125. }
  126. enc->packet = av_packet_alloc();
  127. enc->initialized = true;
  128. return true;
  129. }
  130. /* "Allowed" options per Rate Control
  131. * See FFMPEG libavcodec/vaapi_encode.c (vaapi_encode_rc_modes)
  132. */
  133. typedef struct {
  134. const char *name;
  135. bool qp;
  136. bool bitrate;
  137. bool maxrate;
  138. } rc_mode_t;
  139. static const rc_mode_t *get_rc_mode(const char *name)
  140. {
  141. /* Set "allowed" options per Rate Control */
  142. static const rc_mode_t RC_MODES[] = {
  143. {.name = "CBR", .qp = false, .bitrate = true, .maxrate = false},
  144. {.name = "CQP", .qp = true, .bitrate = false, .maxrate = false},
  145. {.name = "VBR", .qp = false, .bitrate = true, .maxrate = true},
  146. NULL};
  147. const rc_mode_t *rc_mode = RC_MODES;
  148. while (!!rc_mode && strcmp(rc_mode->name, name) != 0)
  149. rc_mode++;
  150. return rc_mode ? rc_mode : RC_MODES;
  151. }
  152. static bool vaapi_update(void *data, obs_data_t *settings)
  153. {
  154. struct vaapi_encoder *enc = data;
  155. const char *device = obs_data_get_string(settings, "vaapi_device");
  156. const char *rate_control =
  157. obs_data_get_string(settings, "rate_control");
  158. const rc_mode_t *rc_mode = get_rc_mode(rate_control);
  159. bool cbr = strcmp(rc_mode->name, "CBR") == 0;
  160. int profile = (int)obs_data_get_int(settings, "profile");
  161. int bf = (int)obs_data_get_int(settings, "bf");
  162. int qp = rc_mode->qp ? (int)obs_data_get_int(settings, "qp") : 0;
  163. av_opt_set_int(enc->context->priv_data, "qp", qp, 0);
  164. int level = (int)obs_data_get_int(settings, "level");
  165. int bitrate = rc_mode->bitrate
  166. ? (int)obs_data_get_int(settings, "bitrate")
  167. : 0;
  168. int maxrate = rc_mode->maxrate
  169. ? (int)obs_data_get_int(settings, "maxrate")
  170. : 0;
  171. int keyint_sec = (int)obs_data_get_int(settings, "keyint_sec");
  172. /* For Rate Control which allows maxrate, FFMPEG will give
  173. * an error if maxrate > bitrate. To prevent that set maxrate
  174. * to 0.
  175. * For CBR, maxrate = bitrate
  176. */
  177. if (cbr)
  178. maxrate = bitrate;
  179. else if (rc_mode->maxrate && maxrate && maxrate < bitrate)
  180. maxrate = 0;
  181. video_t *video = obs_encoder_video(enc->encoder);
  182. const struct video_output_info *voi = video_output_get_info(video);
  183. struct video_scale_info info;
  184. info.format = voi->format;
  185. info.colorspace = voi->colorspace;
  186. info.range = voi->range;
  187. vaapi_video_info(enc, &info);
  188. enc->context->profile = profile;
  189. enc->context->max_b_frames = bf;
  190. enc->context->level = level;
  191. enc->context->bit_rate = bitrate * 1000;
  192. enc->context->rc_max_rate = maxrate * 1000;
  193. enc->context->width = obs_encoder_get_width(enc->encoder);
  194. enc->context->height = obs_encoder_get_height(enc->encoder);
  195. enc->context->time_base = (AVRational){voi->fps_den, voi->fps_num};
  196. enc->context->pix_fmt = obs_to_ffmpeg_video_format(info.format);
  197. enc->context->color_range = info.range == VIDEO_RANGE_FULL
  198. ? AVCOL_RANGE_JPEG
  199. : AVCOL_RANGE_MPEG;
  200. switch (info.colorspace) {
  201. case VIDEO_CS_601:
  202. enc->context->color_trc = AVCOL_TRC_SMPTE170M;
  203. enc->context->color_primaries = AVCOL_PRI_SMPTE170M;
  204. enc->context->colorspace = AVCOL_SPC_SMPTE170M;
  205. break;
  206. case VIDEO_CS_DEFAULT:
  207. case VIDEO_CS_709:
  208. enc->context->color_trc = AVCOL_TRC_BT709;
  209. enc->context->color_primaries = AVCOL_PRI_BT709;
  210. enc->context->colorspace = AVCOL_SPC_BT709;
  211. break;
  212. case VIDEO_CS_SRGB:
  213. enc->context->color_trc = AVCOL_TRC_IEC61966_2_1;
  214. enc->context->color_primaries = AVCOL_PRI_BT709;
  215. enc->context->colorspace = AVCOL_SPC_BT709;
  216. break;
  217. }
  218. if (keyint_sec > 0) {
  219. enc->context->gop_size =
  220. keyint_sec * voi->fps_num / voi->fps_den;
  221. } else {
  222. enc->context->gop_size = 120;
  223. }
  224. enc->height = enc->context->height;
  225. info("settings:\n"
  226. "\tdevice: %s\n"
  227. "\trate_control: %s\n"
  228. "\tprofile: %d\n"
  229. "\tlevel: %d\n"
  230. "\tqp: %d\n"
  231. "\tbitrate: %d\n"
  232. "\tmaxrate: %d\n"
  233. "\tkeyint: %d\n"
  234. "\twidth: %d\n"
  235. "\theight: %d\n"
  236. "\tb-frames: %d\n",
  237. device, rate_control, profile, level, qp, bitrate, maxrate,
  238. enc->context->gop_size, enc->context->width, enc->context->height,
  239. enc->context->max_b_frames);
  240. return vaapi_init_codec(enc, device);
  241. }
  242. static inline void flush_remaining_packets(struct vaapi_encoder *enc)
  243. {
  244. int r_pkt = 1;
  245. while (r_pkt) {
  246. #if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(57, 40, 101)
  247. if (avcodec_receive_packet(enc->context, enc->packet) < 0)
  248. break;
  249. #else
  250. if (avcodec_encode_video2(enc->context, enc->packet, NULL,
  251. &r_pkt) < 0)
  252. break;
  253. #endif
  254. if (r_pkt)
  255. av_packet_unref(enc->packet);
  256. }
  257. }
  258. static void vaapi_destroy(void *data)
  259. {
  260. struct vaapi_encoder *enc = data;
  261. if (enc->initialized)
  262. flush_remaining_packets(enc);
  263. av_packet_free(&enc->packet);
  264. avcodec_free_context(&enc->context);
  265. av_frame_unref(enc->vframe);
  266. av_frame_free(&enc->vframe);
  267. av_buffer_unref(&enc->vaframes_ref);
  268. av_buffer_unref(&enc->vadevice_ref);
  269. da_free(enc->buffer);
  270. bfree(enc->header);
  271. bfree(enc->sei);
  272. bfree(enc);
  273. }
  274. static void *vaapi_create(obs_data_t *settings, obs_encoder_t *encoder)
  275. {
  276. struct vaapi_encoder *enc;
  277. #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 9, 100)
  278. avcodec_register_all();
  279. #endif
  280. enc = bzalloc(sizeof(*enc));
  281. enc->encoder = encoder;
  282. enc->vaapi = avcodec_find_encoder_by_name("h264_vaapi");
  283. enc->first_packet = true;
  284. blog(LOG_INFO, "---------------------------------");
  285. if (!enc->vaapi) {
  286. warn("Couldn't find encoder");
  287. goto fail;
  288. }
  289. enc->context = avcodec_alloc_context3(enc->vaapi);
  290. if (!enc->context) {
  291. warn("Failed to create codec context");
  292. goto fail;
  293. }
  294. if (!vaapi_update(enc, settings))
  295. goto fail;
  296. return enc;
  297. fail:
  298. vaapi_destroy(enc);
  299. return NULL;
  300. }
  301. static inline void copy_data(AVFrame *pic, const struct encoder_frame *frame,
  302. int height, enum AVPixelFormat format)
  303. {
  304. int h_chroma_shift, v_chroma_shift;
  305. av_pix_fmt_get_chroma_sub_sample(format, &h_chroma_shift,
  306. &v_chroma_shift);
  307. for (int plane = 0; plane < MAX_AV_PLANES; plane++) {
  308. if (!frame->data[plane])
  309. continue;
  310. int frame_rowsize = (int)frame->linesize[plane];
  311. int pic_rowsize = pic->linesize[plane];
  312. int bytes = frame_rowsize < pic_rowsize ? frame_rowsize
  313. : pic_rowsize;
  314. int plane_height = height >> (plane ? v_chroma_shift : 0);
  315. for (int y = 0; y < plane_height; y++) {
  316. int pos_frame = y * frame_rowsize;
  317. int pos_pic = y * pic_rowsize;
  318. memcpy(pic->data[plane] + pos_pic,
  319. frame->data[plane] + pos_frame, bytes);
  320. }
  321. }
  322. }
  323. static bool vaapi_encode(void *data, struct encoder_frame *frame,
  324. struct encoder_packet *packet, bool *received_packet)
  325. {
  326. struct vaapi_encoder *enc = data;
  327. AVFrame *hwframe = NULL;
  328. int got_packet;
  329. int ret;
  330. hwframe = av_frame_alloc();
  331. if (!hwframe) {
  332. warn("vaapi_encode: failed to allocate hw frame");
  333. return false;
  334. }
  335. ret = av_hwframe_get_buffer(enc->vaframes_ref, hwframe, 0);
  336. if (ret < 0) {
  337. warn("vaapi_encode: failed to get buffer for hw frame: %s",
  338. av_err2str(ret));
  339. goto fail;
  340. }
  341. copy_data(enc->vframe, frame, enc->height, enc->context->pix_fmt);
  342. enc->vframe->pts = frame->pts;
  343. hwframe->pts = frame->pts;
  344. hwframe->width = enc->vframe->width;
  345. hwframe->height = enc->vframe->height;
  346. ret = av_hwframe_transfer_data(hwframe, enc->vframe, 0);
  347. if (ret < 0) {
  348. warn("vaapi_encode: failed to upload hw frame: %s",
  349. av_err2str(ret));
  350. goto fail;
  351. }
  352. ret = av_frame_copy_props(hwframe, enc->vframe);
  353. if (ret < 0) {
  354. warn("vaapi_encode: failed to copy props to hw frame: %s",
  355. av_err2str(ret));
  356. goto fail;
  357. }
  358. #if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(57, 40, 101)
  359. ret = avcodec_send_frame(enc->context, hwframe);
  360. if (ret == 0)
  361. ret = avcodec_receive_packet(enc->context, enc->packet);
  362. got_packet = (ret == 0);
  363. if (ret == AVERROR_EOF || ret == AVERROR(EAGAIN))
  364. ret = 0;
  365. #else
  366. ret = avcodec_encode_video2(enc->context, enc->packet, hwframe,
  367. &got_packet);
  368. #endif
  369. if (ret < 0) {
  370. warn("vaapi_encode: Error encoding: %s", av_err2str(ret));
  371. goto fail;
  372. }
  373. if (got_packet && enc->packet->size) {
  374. if (enc->first_packet) {
  375. uint8_t *new_packet;
  376. size_t size;
  377. enc->first_packet = false;
  378. obs_extract_avc_headers(enc->packet->data,
  379. enc->packet->size, &new_packet,
  380. &size, &enc->header,
  381. &enc->header_size, &enc->sei,
  382. &enc->sei_size);
  383. da_copy_array(enc->buffer, new_packet, size);
  384. bfree(new_packet);
  385. } else {
  386. da_copy_array(enc->buffer, enc->packet->data,
  387. enc->packet->size);
  388. }
  389. packet->pts = enc->packet->pts;
  390. packet->dts = enc->packet->dts;
  391. packet->data = enc->buffer.array;
  392. packet->size = enc->buffer.num;
  393. packet->type = OBS_ENCODER_VIDEO;
  394. packet->keyframe = obs_avc_keyframe(packet->data, packet->size);
  395. *received_packet = true;
  396. } else {
  397. *received_packet = false;
  398. }
  399. av_packet_unref(enc->packet);
  400. av_frame_free(&hwframe);
  401. return true;
  402. fail:
  403. av_frame_free(&hwframe);
  404. return false;
  405. }
  406. static void set_visible(obs_properties_t *ppts, const char *name, bool visible)
  407. {
  408. obs_property_t *p = obs_properties_get(ppts, name);
  409. obs_property_set_visible(p, visible);
  410. }
  411. static void vaapi_defaults(obs_data_t *settings)
  412. {
  413. obs_data_set_default_string(settings, "vaapi_device",
  414. "/dev/dri/renderD128");
  415. obs_data_set_default_int(settings, "profile",
  416. FF_PROFILE_H264_CONSTRAINED_BASELINE);
  417. obs_data_set_default_int(settings, "level", 40);
  418. obs_data_set_default_int(settings, "bitrate", 2500);
  419. obs_data_set_default_int(settings, "keyint_sec", 0);
  420. obs_data_set_default_int(settings, "bf", 0);
  421. obs_data_set_default_int(settings, "rendermode", 0);
  422. obs_data_set_default_string(settings, "rate_control", "CBR");
  423. obs_data_set_default_int(settings, "qp", 20);
  424. obs_data_set_default_int(settings, "maxrate", 0);
  425. }
  426. static bool rate_control_modified(obs_properties_t *ppts, obs_property_t *p,
  427. obs_data_t *settings)
  428. {
  429. UNUSED_PARAMETER(p);
  430. const char *rate_control =
  431. obs_data_get_string(settings, "rate_control");
  432. const rc_mode_t *rc_mode = get_rc_mode(rate_control);
  433. /* Set options visibility per Rate Control */
  434. set_visible(ppts, "qp", rc_mode->qp);
  435. set_visible(ppts, "bitrate", rc_mode->bitrate);
  436. set_visible(ppts, "maxrate", rc_mode->maxrate);
  437. return true;
  438. }
  439. static bool get_device_name_from_pci(struct pci_access *pacc, char *pci_slot,
  440. char *buf, int size)
  441. {
  442. struct pci_filter filter;
  443. struct pci_dev *dev;
  444. char *name;
  445. pci_filter_init(pacc, &filter);
  446. if (pci_filter_parse_slot(&filter, pci_slot))
  447. return false;
  448. pci_scan_bus(pacc);
  449. for (dev = pacc->devices; dev; dev = dev->next) {
  450. if (pci_filter_match(&filter, dev)) {
  451. pci_fill_info(dev, PCI_FILL_IDENT);
  452. name = pci_lookup_name(pacc, buf, size,
  453. PCI_LOOKUP_DEVICE,
  454. dev->vendor_id, dev->device_id);
  455. strcpy(buf, name);
  456. return true;
  457. }
  458. }
  459. return false;
  460. }
  461. static obs_properties_t *vaapi_properties(void *unused)
  462. {
  463. UNUSED_PARAMETER(unused);
  464. obs_properties_t *props = obs_properties_create();
  465. obs_property_t *list;
  466. list = obs_properties_add_list(props, "vaapi_device",
  467. obs_module_text("VAAPI.Device"),
  468. OBS_COMBO_TYPE_LIST,
  469. OBS_COMBO_FORMAT_STRING);
  470. if (os_file_exists("/dev/dri/by-path")) {
  471. os_dir_t *by_path_dir = os_opendir("/dev/dri/by-path");
  472. struct pci_access *pacc = pci_alloc();
  473. struct os_dirent *file;
  474. char namebuf[1024];
  475. char pci_slot[13];
  476. char *type;
  477. pci_init(pacc);
  478. while ((file = os_readdir(by_path_dir)) != NULL) {
  479. // file_name pattern: pci-<pci_slot::12>-<type::{"card","render"}>
  480. char *file_name = file->d_name;
  481. if (strcmp(file_name, ".") == 0 ||
  482. strcmp(file_name, "..") == 0)
  483. continue;
  484. char path[64] = {0};
  485. // Use the return value of snprintf to prevent truncation warning.
  486. int written = snprintf(path, 64, "/dev/dri/by-path/%s",
  487. file_name);
  488. if (written >= 64)
  489. blog(LOG_DEBUG,
  490. "obs-ffmpeg-vaapi: A format truncation may have occurred."
  491. " This can be ignored since it is quite improbable.");
  492. type = strrchr(file_name, '-');
  493. if (type == NULL)
  494. continue;
  495. else
  496. type++;
  497. if (strcmp(type, "render") == 0) {
  498. strncpy(pci_slot, file_name + 4, 12);
  499. pci_slot[12] = 0;
  500. bool name_found = get_device_name_from_pci(
  501. pacc, pci_slot, namebuf,
  502. sizeof(namebuf));
  503. if (!name_found)
  504. obs_property_list_add_string(list, path,
  505. path);
  506. else
  507. obs_property_list_add_string(
  508. list, namebuf, path);
  509. }
  510. }
  511. pci_cleanup(pacc);
  512. os_closedir(by_path_dir);
  513. }
  514. if (obs_property_list_item_count(list) == 0) {
  515. char path[32];
  516. for (int i = 28;; i++) {
  517. snprintf(path, sizeof(path), "/dev/dri/renderD1%d", i);
  518. if (access(path, F_OK) == 0) {
  519. char card[128];
  520. int ret = snprintf(card, sizeof(card),
  521. "Card%d: %s", i - 28, path);
  522. if (ret >= sizeof(card))
  523. blog(LOG_DEBUG,
  524. "obs-ffmpeg-vaapi: A format truncation may have occurred."
  525. " This can be ignored since it is quite improbable.");
  526. obs_property_list_add_string(list, card, path);
  527. } else {
  528. break;
  529. }
  530. }
  531. }
  532. list = obs_properties_add_list(props, "profile",
  533. obs_module_text("Profile"),
  534. OBS_COMBO_TYPE_LIST,
  535. OBS_COMBO_FORMAT_INT);
  536. obs_property_list_add_int(list, "Constrained Baseline (default)",
  537. FF_PROFILE_H264_CONSTRAINED_BASELINE);
  538. obs_property_list_add_int(list, "Main", FF_PROFILE_H264_MAIN);
  539. obs_property_list_add_int(list, "High", FF_PROFILE_H264_HIGH);
  540. list = obs_properties_add_list(props, "level", obs_module_text("Level"),
  541. OBS_COMBO_TYPE_LIST,
  542. OBS_COMBO_FORMAT_INT);
  543. obs_property_list_add_int(list, "Auto", FF_LEVEL_UNKNOWN);
  544. obs_property_list_add_int(list, "3.0", 30);
  545. obs_property_list_add_int(list, "3.1", 31);
  546. obs_property_list_add_int(list, "4.0 (default) (Compatibility mode)",
  547. 40);
  548. obs_property_list_add_int(list, "4.1", 41);
  549. obs_property_list_add_int(list, "4.2", 42);
  550. obs_property_list_add_int(list, "5.0", 50);
  551. obs_property_list_add_int(list, "5.1", 51);
  552. obs_property_list_add_int(list, "5.2", 52);
  553. list = obs_properties_add_list(props, "rate_control",
  554. obs_module_text("RateControl"),
  555. OBS_COMBO_TYPE_LIST,
  556. OBS_COMBO_FORMAT_STRING);
  557. obs_property_list_add_string(list, "CBR (default)", "CBR");
  558. obs_property_list_add_string(list, "CQP", "CQP");
  559. obs_property_list_add_string(list, "VBR", "VBR");
  560. obs_property_set_modified_callback(list, rate_control_modified);
  561. obs_property_t *p;
  562. p = obs_properties_add_int(props, "bitrate", obs_module_text("Bitrate"),
  563. 0, 300000, 50);
  564. obs_property_int_set_suffix(p, " Kbps");
  565. p = obs_properties_add_int(
  566. props, "maxrate", obs_module_text("MaxBitrate"), 0, 300000, 50);
  567. obs_property_int_set_suffix(p, " Kbps");
  568. obs_properties_add_int(props, "qp", "QP", 0, 51, 1);
  569. p = obs_properties_add_int(props, "keyint_sec",
  570. obs_module_text("KeyframeIntervalSec"), 0,
  571. 20, 1);
  572. obs_property_int_set_suffix(p, " s");
  573. return props;
  574. }
  575. static bool vaapi_extra_data(void *data, uint8_t **extra_data, size_t *size)
  576. {
  577. struct vaapi_encoder *enc = data;
  578. *extra_data = enc->header;
  579. *size = enc->header_size;
  580. return true;
  581. }
  582. static bool vaapi_sei_data(void *data, uint8_t **extra_data, size_t *size)
  583. {
  584. struct vaapi_encoder *enc = data;
  585. *extra_data = enc->sei;
  586. *size = enc->sei_size;
  587. return true;
  588. }
  589. struct obs_encoder_info vaapi_encoder_info = {
  590. .id = "ffmpeg_vaapi",
  591. .type = OBS_ENCODER_VIDEO,
  592. .codec = "h264",
  593. .get_name = vaapi_getname,
  594. .create = vaapi_create,
  595. .destroy = vaapi_destroy,
  596. .encode = vaapi_encode,
  597. .get_defaults = vaapi_defaults,
  598. .get_properties = vaapi_properties,
  599. .get_extra_data = vaapi_extra_data,
  600. .get_sei_data = vaapi_sei_data,
  601. .get_video_info = vaapi_video_info,
  602. };
  603. #endif