obs-ffmpeg-vaapi.c 19 KB

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