2016-05-24 19:53:01 +00:00
|
|
|
/******************************************************************************
|
|
|
|
Copyright (C) 2016 by Hugh Bailey <obs.jim@gmail.com>
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
#include <util/darray.h>
|
|
|
|
#include <util/dstr.h>
|
|
|
|
#include <util/base.h>
|
|
|
|
#include <media-io/video-io.h>
|
|
|
|
#include <obs-module.h>
|
|
|
|
#include <obs-avc.h>
|
|
|
|
|
|
|
|
#include <libavutil/opt.h>
|
2017-04-19 19:54:15 +00:00
|
|
|
#include <libavutil/pixdesc.h>
|
2016-05-24 19:53:01 +00:00
|
|
|
#include <libavformat/avformat.h>
|
|
|
|
|
|
|
|
#include "obs-ffmpeg-formats.h"
|
|
|
|
|
2019-09-22 21:19:10 +00:00
|
|
|
#define do_log(level, format, ...) \
|
2016-05-24 19:53:01 +00:00
|
|
|
blog(level, "[NVENC encoder: '%s'] " format, \
|
2019-09-22 21:19:10 +00:00
|
|
|
obs_encoder_get_name(enc->encoder), ##__VA_ARGS__)
|
2016-05-24 19:53:01 +00:00
|
|
|
|
2019-09-22 21:19:10 +00:00
|
|
|
#define warn(format, ...) do_log(LOG_WARNING, format, ##__VA_ARGS__)
|
|
|
|
#define info(format, ...) do_log(LOG_INFO, format, ##__VA_ARGS__)
|
|
|
|
#define debug(format, ...) do_log(LOG_DEBUG, format, ##__VA_ARGS__)
|
2016-05-24 19:53:01 +00:00
|
|
|
|
|
|
|
struct nvenc_encoder {
|
2019-09-22 21:19:10 +00:00
|
|
|
obs_encoder_t *encoder;
|
2016-05-24 19:53:01 +00:00
|
|
|
|
2019-09-22 21:19:10 +00:00
|
|
|
AVCodec *nvenc;
|
|
|
|
AVCodecContext *context;
|
2016-05-24 19:53:01 +00:00
|
|
|
|
2019-09-22 21:19:10 +00:00
|
|
|
AVFrame *vframe;
|
2016-05-24 19:53:01 +00:00
|
|
|
|
2019-09-22 21:19:10 +00:00
|
|
|
DARRAY(uint8_t) buffer;
|
2016-05-24 19:53:01 +00:00
|
|
|
|
2019-09-22 21:19:10 +00:00
|
|
|
uint8_t *header;
|
|
|
|
size_t header_size;
|
2016-05-24 19:53:01 +00:00
|
|
|
|
2019-09-22 21:19:10 +00:00
|
|
|
uint8_t *sei;
|
|
|
|
size_t sei_size;
|
2016-05-24 19:53:01 +00:00
|
|
|
|
2019-09-22 21:19:10 +00:00
|
|
|
int height;
|
|
|
|
bool first_packet;
|
|
|
|
bool initialized;
|
2016-05-24 19:53:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const char *nvenc_getname(void *unused)
|
|
|
|
{
|
|
|
|
UNUSED_PARAMETER(unused);
|
2019-07-27 12:47:10 +00:00
|
|
|
return "NVIDIA NVENC H.264";
|
2016-05-24 19:53:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool valid_format(enum video_format format)
|
|
|
|
{
|
2019-09-22 21:19:10 +00:00
|
|
|
return format == VIDEO_FORMAT_I420 || format == VIDEO_FORMAT_NV12 ||
|
2016-05-24 19:53:01 +00:00
|
|
|
format == VIDEO_FORMAT_I444;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void nvenc_video_info(void *data, struct video_scale_info *info)
|
|
|
|
{
|
|
|
|
struct nvenc_encoder *enc = data;
|
|
|
|
enum video_format pref_format;
|
|
|
|
|
|
|
|
pref_format = obs_encoder_get_preferred_video_format(enc->encoder);
|
|
|
|
|
|
|
|
if (!valid_format(pref_format)) {
|
2019-09-22 21:19:10 +00:00
|
|
|
pref_format = valid_format(info->format) ? info->format
|
|
|
|
: VIDEO_FORMAT_NV12;
|
2016-05-24 19:53:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
info->format = pref_format;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool nvenc_init_codec(struct nvenc_encoder *enc)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = avcodec_open2(enc->context, enc->nvenc, NULL);
|
|
|
|
if (ret < 0) {
|
|
|
|
warn("Failed to open NVENC codec: %s", av_err2str(ret));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
enc->vframe = av_frame_alloc();
|
|
|
|
if (!enc->vframe) {
|
|
|
|
warn("Failed to allocate video frame");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
enc->vframe->format = enc->context->pix_fmt;
|
|
|
|
enc->vframe->width = enc->context->width;
|
|
|
|
enc->vframe->height = enc->context->height;
|
|
|
|
enc->vframe->colorspace = enc->context->colorspace;
|
|
|
|
enc->vframe->color_range = enc->context->color_range;
|
|
|
|
|
2018-02-19 19:54:37 +00:00
|
|
|
ret = av_frame_get_buffer(enc->vframe, base_get_alignment());
|
2016-05-24 19:53:01 +00:00
|
|
|
if (ret < 0) {
|
2018-02-19 19:54:37 +00:00
|
|
|
warn("Failed to allocate vframe: %s", av_err2str(ret));
|
2016-05-24 19:53:01 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-10-10 19:01:40 +00:00
|
|
|
enc->initialized = true;
|
2016-05-24 19:53:01 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-09-22 21:19:10 +00:00
|
|
|
enum RC_MODE { RC_MODE_CBR, RC_MODE_VBR, RC_MODE_CQP, RC_MODE_LOSSLESS };
|
2016-05-24 19:53:01 +00:00
|
|
|
|
|
|
|
static bool nvenc_update(void *data, obs_data_t *settings)
|
|
|
|
{
|
|
|
|
struct nvenc_encoder *enc = data;
|
|
|
|
|
|
|
|
const char *rc = obs_data_get_string(settings, "rate_control");
|
|
|
|
int bitrate = (int)obs_data_get_int(settings, "bitrate");
|
|
|
|
int cqp = (int)obs_data_get_int(settings, "cqp");
|
|
|
|
int keyint_sec = (int)obs_data_get_int(settings, "keyint_sec");
|
|
|
|
const char *preset = obs_data_get_string(settings, "preset");
|
|
|
|
const char *profile = obs_data_get_string(settings, "profile");
|
|
|
|
int gpu = (int)obs_data_get_int(settings, "gpu");
|
|
|
|
bool cbr_override = obs_data_get_bool(settings, "cbr");
|
2016-10-10 19:01:40 +00:00
|
|
|
int bf = (int)obs_data_get_int(settings, "bf");
|
2016-05-24 19:53:01 +00:00
|
|
|
|
|
|
|
video_t *video = obs_encoder_video(enc->encoder);
|
|
|
|
const struct video_output_info *voi = video_output_get_info(video);
|
|
|
|
struct video_scale_info info;
|
|
|
|
|
|
|
|
/* XXX: "cbr" setting has been deprecated */
|
|
|
|
if (cbr_override) {
|
|
|
|
warn("\"cbr\" setting has been deprecated for all encoders! "
|
|
|
|
"Please set \"rate_control\" to \"CBR\" instead. "
|
|
|
|
"Forcing CBR mode. "
|
|
|
|
"(Note to all: this is why you shouldn't use strings for "
|
|
|
|
"common settings)");
|
|
|
|
rc = "CBR";
|
|
|
|
}
|
|
|
|
|
|
|
|
info.format = voi->format;
|
|
|
|
info.colorspace = voi->colorspace;
|
|
|
|
info.range = voi->range;
|
|
|
|
|
2019-07-27 12:47:10 +00:00
|
|
|
bool twopass = false;
|
|
|
|
|
|
|
|
if (astrcmpi(preset, "mq") == 0) {
|
|
|
|
twopass = true;
|
|
|
|
preset = "hq";
|
|
|
|
}
|
|
|
|
|
2016-05-24 19:53:01 +00:00
|
|
|
nvenc_video_info(enc, &info);
|
|
|
|
av_opt_set_int(enc->context->priv_data, "cbr", false, 0);
|
2016-10-10 19:01:40 +00:00
|
|
|
av_opt_set(enc->context->priv_data, "profile", profile, 0);
|
2016-08-28 12:07:43 +00:00
|
|
|
av_opt_set(enc->context->priv_data, "preset", preset, 0);
|
|
|
|
|
2016-05-24 19:53:01 +00:00
|
|
|
if (astrcmpi(rc, "cqp") == 0) {
|
|
|
|
bitrate = 0;
|
|
|
|
enc->context->global_quality = cqp;
|
|
|
|
|
|
|
|
} else if (astrcmpi(rc, "lossless") == 0) {
|
|
|
|
bitrate = 0;
|
|
|
|
cqp = 0;
|
|
|
|
|
|
|
|
bool hp = (astrcmpi(preset, "hp") == 0 ||
|
2019-09-22 21:19:10 +00:00
|
|
|
astrcmpi(preset, "llhp") == 0);
|
2016-05-24 19:53:01 +00:00
|
|
|
|
2016-08-28 12:07:43 +00:00
|
|
|
av_opt_set(enc->context->priv_data, "preset",
|
2019-09-22 21:19:10 +00:00
|
|
|
hp ? "losslesshp" : "lossless", 0);
|
2016-05-24 19:53:01 +00:00
|
|
|
|
|
|
|
} else if (astrcmpi(rc, "vbr") != 0) { /* CBR by default */
|
|
|
|
av_opt_set_int(enc->context->priv_data, "cbr", true, 0);
|
|
|
|
enc->context->rc_max_rate = bitrate * 1000;
|
|
|
|
enc->context->rc_min_rate = bitrate * 1000;
|
|
|
|
cqp = 0;
|
|
|
|
}
|
|
|
|
|
2019-07-27 12:47:10 +00:00
|
|
|
av_opt_set(enc->context->priv_data, "level", "auto", 0);
|
2016-05-24 19:53:01 +00:00
|
|
|
av_opt_set_int(enc->context->priv_data, "2pass", twopass, 0);
|
|
|
|
av_opt_set_int(enc->context->priv_data, "gpu", gpu, 0);
|
|
|
|
|
|
|
|
enc->context->bit_rate = bitrate * 1000;
|
|
|
|
enc->context->rc_buffer_size = bitrate * 1000;
|
|
|
|
enc->context->width = obs_encoder_get_width(enc->encoder);
|
|
|
|
enc->context->height = obs_encoder_get_height(enc->encoder);
|
|
|
|
enc->context->time_base = (AVRational){voi->fps_den, voi->fps_num};
|
|
|
|
enc->context->pix_fmt = obs_to_ffmpeg_video_format(info.format);
|
2019-09-22 21:19:10 +00:00
|
|
|
enc->context->colorspace = info.colorspace == VIDEO_CS_709
|
|
|
|
? AVCOL_SPC_BT709
|
|
|
|
: AVCOL_SPC_BT470BG;
|
|
|
|
enc->context->color_range = info.range == VIDEO_RANGE_FULL
|
|
|
|
? AVCOL_RANGE_JPEG
|
|
|
|
: AVCOL_RANGE_MPEG;
|
2016-10-10 19:01:40 +00:00
|
|
|
enc->context->max_b_frames = bf;
|
2016-05-24 19:53:01 +00:00
|
|
|
|
|
|
|
if (keyint_sec)
|
2019-09-22 21:19:10 +00:00
|
|
|
enc->context->gop_size =
|
|
|
|
keyint_sec * voi->fps_num / voi->fps_den;
|
2016-05-24 19:53:01 +00:00
|
|
|
else
|
|
|
|
enc->context->gop_size = 250;
|
|
|
|
|
|
|
|
enc->height = enc->context->height;
|
|
|
|
|
|
|
|
info("settings:\n"
|
|
|
|
"\trate_control: %s\n"
|
|
|
|
"\tbitrate: %d\n"
|
|
|
|
"\tcqp: %d\n"
|
|
|
|
"\tkeyint: %d\n"
|
|
|
|
"\tpreset: %s\n"
|
|
|
|
"\tprofile: %s\n"
|
|
|
|
"\twidth: %d\n"
|
|
|
|
"\theight: %d\n"
|
|
|
|
"\t2-pass: %s\n"
|
2017-04-19 19:54:15 +00:00
|
|
|
"\tb-frames: %d\n"
|
2016-05-24 19:53:01 +00:00
|
|
|
"\tGPU: %d\n",
|
2019-09-22 21:19:10 +00:00
|
|
|
rc, bitrate, cqp, enc->context->gop_size, preset, profile,
|
2016-05-24 19:53:01 +00:00
|
|
|
enc->context->width, enc->context->height,
|
2019-09-22 21:19:10 +00:00
|
|
|
twopass ? "true" : "false", enc->context->max_b_frames, gpu);
|
2016-05-24 19:53:01 +00:00
|
|
|
|
|
|
|
return nvenc_init_codec(enc);
|
|
|
|
}
|
|
|
|
|
2019-09-22 21:19:10 +00:00
|
|
|
static bool nvenc_reconfigure(void *data, obs_data_t *settings)
|
|
|
|
{
|
|
|
|
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(58, 19, 101)
|
|
|
|
struct nvenc_encoder *enc = data;
|
|
|
|
|
|
|
|
int bitrate = (int)obs_data_get_int(settings, "bitrate");
|
|
|
|
const char *rc = obs_data_get_string(settings, "rate_control");
|
|
|
|
bool cbr = astrcmpi(rc, "CBR") == 0;
|
|
|
|
bool vbr = astrcmpi(rc, "VBR") == 0;
|
|
|
|
if (cbr || vbr) {
|
|
|
|
enc->context->bit_rate = bitrate * 1000;
|
|
|
|
enc->context->rc_max_rate = bitrate * 1000;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-05-24 19:53:01 +00:00
|
|
|
static void nvenc_destroy(void *data)
|
|
|
|
{
|
|
|
|
struct nvenc_encoder *enc = data;
|
|
|
|
|
2016-10-10 19:01:40 +00:00
|
|
|
if (enc->initialized) {
|
|
|
|
AVPacket pkt = {0};
|
|
|
|
int r_pkt = 1;
|
|
|
|
|
|
|
|
while (r_pkt) {
|
2018-02-19 19:54:37 +00:00
|
|
|
#if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(57, 40, 101)
|
|
|
|
if (avcodec_receive_packet(enc->context, &pkt) < 0)
|
|
|
|
break;
|
|
|
|
#else
|
2016-10-10 19:01:40 +00:00
|
|
|
if (avcodec_encode_video2(enc->context, &pkt, NULL,
|
2019-09-22 21:19:10 +00:00
|
|
|
&r_pkt) < 0)
|
2016-10-10 19:01:40 +00:00
|
|
|
break;
|
2018-02-19 19:54:37 +00:00
|
|
|
#endif
|
2016-10-10 19:01:40 +00:00
|
|
|
|
|
|
|
if (r_pkt)
|
2018-02-19 19:54:37 +00:00
|
|
|
av_packet_unref(&pkt);
|
2016-10-10 19:01:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-24 19:53:01 +00:00
|
|
|
avcodec_close(enc->context);
|
2018-02-19 19:54:37 +00:00
|
|
|
av_frame_unref(enc->vframe);
|
2016-05-24 19:53:01 +00:00
|
|
|
av_frame_free(&enc->vframe);
|
|
|
|
da_free(enc->buffer);
|
|
|
|
bfree(enc->header);
|
|
|
|
bfree(enc->sei);
|
|
|
|
|
|
|
|
bfree(enc);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *nvenc_create(obs_data_t *settings, obs_encoder_t *encoder)
|
|
|
|
{
|
|
|
|
struct nvenc_encoder *enc;
|
|
|
|
|
2019-09-22 21:19:10 +00:00
|
|
|
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 9, 100)
|
2016-05-24 19:53:01 +00:00
|
|
|
avcodec_register_all();
|
2019-09-22 21:19:10 +00:00
|
|
|
#endif
|
2016-05-24 19:53:01 +00:00
|
|
|
|
|
|
|
enc = bzalloc(sizeof(*enc));
|
|
|
|
enc->encoder = encoder;
|
2017-04-19 19:54:15 +00:00
|
|
|
enc->nvenc = avcodec_find_encoder_by_name("h264_nvenc");
|
|
|
|
if (!enc->nvenc)
|
|
|
|
enc->nvenc = avcodec_find_encoder_by_name("nvenc_h264");
|
2016-05-24 19:53:01 +00:00
|
|
|
enc->first_packet = true;
|
|
|
|
|
|
|
|
blog(LOG_INFO, "---------------------------------");
|
|
|
|
|
|
|
|
if (!enc->nvenc) {
|
|
|
|
warn("Couldn't find encoder");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
enc->context = avcodec_alloc_context3(enc->nvenc);
|
|
|
|
if (!enc->context) {
|
|
|
|
warn("Failed to create codec context");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!nvenc_update(enc, settings))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
return enc;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
nvenc_destroy(enc);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-02-19 19:54:37 +00:00
|
|
|
static inline void copy_data(AVFrame *pic, const struct encoder_frame *frame,
|
2019-09-22 21:19:10 +00:00
|
|
|
int height, enum AVPixelFormat format)
|
2016-05-24 19:53:01 +00:00
|
|
|
{
|
2017-04-19 19:54:15 +00:00
|
|
|
int h_chroma_shift, v_chroma_shift;
|
2019-09-22 21:19:10 +00:00
|
|
|
av_pix_fmt_get_chroma_sub_sample(format, &h_chroma_shift,
|
|
|
|
&v_chroma_shift);
|
2016-05-24 19:53:01 +00:00
|
|
|
for (int plane = 0; plane < MAX_AV_PLANES; plane++) {
|
|
|
|
if (!frame->data[plane])
|
|
|
|
continue;
|
|
|
|
|
|
|
|
int frame_rowsize = (int)frame->linesize[plane];
|
2019-09-22 21:19:10 +00:00
|
|
|
int pic_rowsize = pic->linesize[plane];
|
|
|
|
int bytes = frame_rowsize < pic_rowsize ? frame_rowsize
|
|
|
|
: pic_rowsize;
|
2017-04-19 19:54:15 +00:00
|
|
|
int plane_height = height >> (plane ? v_chroma_shift : 0);
|
2016-05-24 19:53:01 +00:00
|
|
|
|
|
|
|
for (int y = 0; y < plane_height; y++) {
|
|
|
|
int pos_frame = y * frame_rowsize;
|
2019-09-22 21:19:10 +00:00
|
|
|
int pos_pic = y * pic_rowsize;
|
2016-05-24 19:53:01 +00:00
|
|
|
|
|
|
|
memcpy(pic->data[plane] + pos_pic,
|
2019-09-22 21:19:10 +00:00
|
|
|
frame->data[plane] + pos_frame, bytes);
|
2016-05-24 19:53:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool nvenc_encode(void *data, struct encoder_frame *frame,
|
2019-09-22 21:19:10 +00:00
|
|
|
struct encoder_packet *packet, bool *received_packet)
|
2016-05-24 19:53:01 +00:00
|
|
|
{
|
|
|
|
struct nvenc_encoder *enc = data;
|
|
|
|
AVPacket av_pkt = {0};
|
|
|
|
int got_packet;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
av_init_packet(&av_pkt);
|
|
|
|
|
2018-02-19 19:54:37 +00:00
|
|
|
copy_data(enc->vframe, frame, enc->height, enc->context->pix_fmt);
|
2016-05-24 19:53:01 +00:00
|
|
|
|
|
|
|
enc->vframe->pts = frame->pts;
|
2018-02-19 19:54:37 +00:00
|
|
|
#if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(57, 40, 101)
|
|
|
|
ret = avcodec_send_frame(enc->context, enc->vframe);
|
|
|
|
if (ret == 0)
|
|
|
|
ret = avcodec_receive_packet(enc->context, &av_pkt);
|
|
|
|
|
|
|
|
got_packet = (ret == 0);
|
|
|
|
|
|
|
|
if (ret == AVERROR_EOF || ret == AVERROR(EAGAIN))
|
|
|
|
ret = 0;
|
|
|
|
#else
|
2016-05-24 19:53:01 +00:00
|
|
|
ret = avcodec_encode_video2(enc->context, &av_pkt, enc->vframe,
|
2019-09-22 21:19:10 +00:00
|
|
|
&got_packet);
|
2018-02-19 19:54:37 +00:00
|
|
|
#endif
|
2016-05-24 19:53:01 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
warn("nvenc_encode: Error encoding: %s", av_err2str(ret));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (got_packet && av_pkt.size) {
|
|
|
|
if (enc->first_packet) {
|
|
|
|
uint8_t *new_packet;
|
|
|
|
size_t size;
|
|
|
|
|
|
|
|
enc->first_packet = false;
|
|
|
|
obs_extract_avc_headers(av_pkt.data, av_pkt.size,
|
2019-09-22 21:19:10 +00:00
|
|
|
&new_packet, &size,
|
|
|
|
&enc->header, &enc->header_size,
|
|
|
|
&enc->sei, &enc->sei_size);
|
2016-05-24 19:53:01 +00:00
|
|
|
|
|
|
|
da_copy_array(enc->buffer, new_packet, size);
|
|
|
|
bfree(new_packet);
|
|
|
|
} else {
|
|
|
|
da_copy_array(enc->buffer, av_pkt.data, av_pkt.size);
|
|
|
|
}
|
|
|
|
|
|
|
|
packet->pts = av_pkt.pts;
|
|
|
|
packet->dts = av_pkt.dts;
|
|
|
|
packet->data = enc->buffer.array;
|
|
|
|
packet->size = enc->buffer.num;
|
|
|
|
packet->type = OBS_ENCODER_VIDEO;
|
|
|
|
packet->keyframe = obs_avc_keyframe(packet->data, packet->size);
|
|
|
|
*received_packet = true;
|
|
|
|
} else {
|
|
|
|
*received_packet = false;
|
|
|
|
}
|
|
|
|
|
2018-02-19 19:54:37 +00:00
|
|
|
av_packet_unref(&av_pkt);
|
2016-05-24 19:53:01 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-07-27 12:47:10 +00:00
|
|
|
void nvenc_defaults(obs_data_t *settings)
|
2016-05-24 19:53:01 +00:00
|
|
|
{
|
|
|
|
obs_data_set_default_int(settings, "bitrate", 2500);
|
2019-07-27 12:47:10 +00:00
|
|
|
obs_data_set_default_int(settings, "max_bitrate", 5000);
|
2016-05-24 19:53:01 +00:00
|
|
|
obs_data_set_default_int(settings, "keyint_sec", 0);
|
2019-07-27 12:47:10 +00:00
|
|
|
obs_data_set_default_int(settings, "cqp", 20);
|
2016-05-24 19:53:01 +00:00
|
|
|
obs_data_set_default_string(settings, "rate_control", "CBR");
|
2019-07-27 12:47:10 +00:00
|
|
|
obs_data_set_default_string(settings, "preset", "hq");
|
|
|
|
obs_data_set_default_string(settings, "profile", "high");
|
|
|
|
obs_data_set_default_bool(settings, "psycho_aq", true);
|
2016-05-24 19:53:01 +00:00
|
|
|
obs_data_set_default_int(settings, "gpu", 0);
|
2016-10-10 19:01:40 +00:00
|
|
|
obs_data_set_default_int(settings, "bf", 2);
|
2016-05-24 19:53:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool rate_control_modified(obs_properties_t *ppts, obs_property_t *p,
|
2019-09-22 21:19:10 +00:00
|
|
|
obs_data_t *settings)
|
2016-05-24 19:53:01 +00:00
|
|
|
{
|
|
|
|
const char *rc = obs_data_get_string(settings, "rate_control");
|
|
|
|
bool cqp = astrcmpi(rc, "CQP") == 0;
|
2019-07-27 12:47:10 +00:00
|
|
|
bool vbr = astrcmpi(rc, "VBR") == 0;
|
2016-05-24 19:53:01 +00:00
|
|
|
bool lossless = astrcmpi(rc, "lossless") == 0;
|
|
|
|
size_t count;
|
|
|
|
|
|
|
|
p = obs_properties_get(ppts, "bitrate");
|
|
|
|
obs_property_set_visible(p, !cqp && !lossless);
|
2019-07-27 12:47:10 +00:00
|
|
|
p = obs_properties_get(ppts, "max_bitrate");
|
|
|
|
obs_property_set_visible(p, vbr);
|
2016-05-24 19:53:01 +00:00
|
|
|
p = obs_properties_get(ppts, "cqp");
|
|
|
|
obs_property_set_visible(p, cqp);
|
|
|
|
|
|
|
|
p = obs_properties_get(ppts, "preset");
|
|
|
|
count = obs_property_list_item_count(p);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < count; i++) {
|
2019-07-27 12:47:10 +00:00
|
|
|
bool compatible = (i == 0 || i == 3);
|
2016-05-24 19:53:01 +00:00
|
|
|
obs_property_list_item_disable(p, i, lossless && !compatible);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-07-27 12:47:10 +00:00
|
|
|
obs_properties_t *nvenc_properties_internal(bool ffmpeg)
|
2016-05-24 19:53:01 +00:00
|
|
|
{
|
|
|
|
obs_properties_t *props = obs_properties_create();
|
|
|
|
obs_property_t *p;
|
|
|
|
|
|
|
|
p = obs_properties_add_list(props, "rate_control",
|
2019-09-22 21:19:10 +00:00
|
|
|
obs_module_text("RateControl"),
|
|
|
|
OBS_COMBO_TYPE_LIST,
|
|
|
|
OBS_COMBO_FORMAT_STRING);
|
2016-05-24 19:53:01 +00:00
|
|
|
obs_property_list_add_string(p, "CBR", "CBR");
|
|
|
|
obs_property_list_add_string(p, "CQP", "CQP");
|
2019-07-27 12:47:10 +00:00
|
|
|
obs_property_list_add_string(p, "VBR", "VBR");
|
2016-05-24 19:53:01 +00:00
|
|
|
obs_property_list_add_string(p, obs_module_text("Lossless"),
|
2019-09-22 21:19:10 +00:00
|
|
|
"lossless");
|
2016-05-24 19:53:01 +00:00
|
|
|
|
|
|
|
obs_property_set_modified_callback(p, rate_control_modified);
|
|
|
|
|
2019-09-22 21:19:10 +00:00
|
|
|
p = obs_properties_add_int(props, "bitrate", obs_module_text("Bitrate"),
|
|
|
|
50, 300000, 50);
|
2019-07-27 12:47:10 +00:00
|
|
|
obs_property_int_set_suffix(p, " Kbps");
|
|
|
|
p = obs_properties_add_int(props, "max_bitrate",
|
2019-09-22 21:19:10 +00:00
|
|
|
obs_module_text("MaxBitrate"), 50, 300000,
|
|
|
|
50);
|
2019-07-27 12:47:10 +00:00
|
|
|
obs_property_int_set_suffix(p, " Kbps");
|
2016-05-24 19:53:01 +00:00
|
|
|
|
2019-07-27 12:47:10 +00:00
|
|
|
obs_properties_add_int(props, "cqp", obs_module_text("NVENC.CQLevel"),
|
2019-09-22 21:19:10 +00:00
|
|
|
1, 30, 1);
|
2016-05-24 19:53:01 +00:00
|
|
|
|
|
|
|
obs_properties_add_int(props, "keyint_sec",
|
2019-09-22 21:19:10 +00:00
|
|
|
obs_module_text("KeyframeIntervalSec"), 0, 10,
|
|
|
|
1);
|
2016-05-24 19:53:01 +00:00
|
|
|
|
|
|
|
p = obs_properties_add_list(props, "preset", obs_module_text("Preset"),
|
2019-09-22 21:19:10 +00:00
|
|
|
OBS_COMBO_TYPE_LIST,
|
|
|
|
OBS_COMBO_FORMAT_STRING);
|
2016-05-24 19:53:01 +00:00
|
|
|
|
2019-09-22 21:19:10 +00:00
|
|
|
#define add_preset(val) \
|
2016-05-24 19:53:01 +00:00
|
|
|
obs_property_list_add_string(p, obs_module_text("NVENC.Preset." val), \
|
2019-09-22 21:19:10 +00:00
|
|
|
val)
|
2019-07-27 12:47:10 +00:00
|
|
|
add_preset("mq");
|
2016-05-24 19:53:01 +00:00
|
|
|
add_preset("hq");
|
2019-07-27 12:47:10 +00:00
|
|
|
add_preset("default");
|
2016-05-24 19:53:01 +00:00
|
|
|
add_preset("hp");
|
|
|
|
add_preset("ll");
|
|
|
|
add_preset("llhq");
|
|
|
|
add_preset("llhp");
|
|
|
|
#undef add_preset
|
|
|
|
|
2019-09-22 21:19:10 +00:00
|
|
|
p = obs_properties_add_list(props, "profile",
|
|
|
|
obs_module_text("Profile"),
|
|
|
|
OBS_COMBO_TYPE_LIST,
|
|
|
|
OBS_COMBO_FORMAT_STRING);
|
2016-05-24 19:53:01 +00:00
|
|
|
|
2019-09-22 21:19:10 +00:00
|
|
|
#define add_profile(val) obs_property_list_add_string(p, val, val)
|
2016-05-24 19:53:01 +00:00
|
|
|
add_profile("high");
|
|
|
|
add_profile("main");
|
|
|
|
add_profile("baseline");
|
|
|
|
#undef add_profile
|
|
|
|
|
2019-07-27 12:47:10 +00:00
|
|
|
if (!ffmpeg) {
|
|
|
|
p = obs_properties_add_bool(props, "lookahead",
|
2019-09-22 21:19:10 +00:00
|
|
|
obs_module_text("NVENC.LookAhead"));
|
|
|
|
obs_property_set_long_description(
|
|
|
|
p, obs_module_text("NVENC.LookAhead.ToolTip"));
|
|
|
|
|
|
|
|
p = obs_properties_add_bool(
|
|
|
|
props, "psycho_aq",
|
|
|
|
obs_module_text("NVENC.PsychoVisualTuning"));
|
|
|
|
obs_property_set_long_description(
|
|
|
|
p, obs_module_text("NVENC.PsychoVisualTuning.ToolTip"));
|
2019-07-27 12:47:10 +00:00
|
|
|
}
|
|
|
|
|
2016-05-24 19:53:01 +00:00
|
|
|
obs_properties_add_int(props, "gpu", obs_module_text("GPU"), 0, 8, 1);
|
|
|
|
|
2019-09-22 21:19:10 +00:00
|
|
|
obs_properties_add_int(props, "bf", obs_module_text("BFrames"), 0, 4,
|
|
|
|
1);
|
2016-10-10 19:01:40 +00:00
|
|
|
|
2016-05-24 19:53:01 +00:00
|
|
|
return props;
|
|
|
|
}
|
|
|
|
|
2019-07-27 12:47:10 +00:00
|
|
|
obs_properties_t *nvenc_properties(void *unused)
|
|
|
|
{
|
|
|
|
UNUSED_PARAMETER(unused);
|
|
|
|
return nvenc_properties_internal(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
obs_properties_t *nvenc_properties_ffmpeg(void *unused)
|
|
|
|
{
|
|
|
|
UNUSED_PARAMETER(unused);
|
|
|
|
return nvenc_properties_internal(true);
|
|
|
|
}
|
|
|
|
|
2016-05-24 19:53:01 +00:00
|
|
|
static bool nvenc_extra_data(void *data, uint8_t **extra_data, size_t *size)
|
|
|
|
{
|
|
|
|
struct nvenc_encoder *enc = data;
|
|
|
|
|
|
|
|
*extra_data = enc->header;
|
2019-09-22 21:19:10 +00:00
|
|
|
*size = enc->header_size;
|
2016-05-24 19:53:01 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool nvenc_sei_data(void *data, uint8_t **extra_data, size_t *size)
|
|
|
|
{
|
|
|
|
struct nvenc_encoder *enc = data;
|
|
|
|
|
|
|
|
*extra_data = enc->sei;
|
2019-09-22 21:19:10 +00:00
|
|
|
*size = enc->sei_size;
|
2016-05-24 19:53:01 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct obs_encoder_info nvenc_encoder_info = {
|
2019-09-22 21:19:10 +00:00
|
|
|
.id = "ffmpeg_nvenc",
|
|
|
|
.type = OBS_ENCODER_VIDEO,
|
|
|
|
.codec = "h264",
|
|
|
|
.get_name = nvenc_getname,
|
|
|
|
.create = nvenc_create,
|
|
|
|
.destroy = nvenc_destroy,
|
|
|
|
.encode = nvenc_encode,
|
|
|
|
.update = nvenc_reconfigure,
|
|
|
|
.get_defaults = nvenc_defaults,
|
2019-07-27 12:47:10 +00:00
|
|
|
.get_properties = nvenc_properties_ffmpeg,
|
2016-05-24 19:53:01 +00:00
|
|
|
.get_extra_data = nvenc_extra_data,
|
2019-09-22 21:19:10 +00:00
|
|
|
.get_sei_data = nvenc_sei_data,
|
|
|
|
.get_video_info = nvenc_video_info,
|
|
|
|
.caps = OBS_ENCODER_CAP_DYN_BITRATE,
|
2016-05-24 19:53:01 +00:00
|
|
|
};
|