New upstream version 24.0.1+dfsg1

This commit is contained in:
Sebastian Ramacher 2019-09-22 23:19:10 +02:00
parent b14f9eae6d
commit 5a730d6ec3
842 changed files with 42245 additions and 33385 deletions

View file

@ -26,68 +26,82 @@
#include "obs-ffmpeg-formats.h"
#include "obs-ffmpeg-compat.h"
#define do_log(level, format, ...) \
blog(level, "[FFmpeg %s encoder: '%s'] " format, \
enc->type, \
obs_encoder_get_name(enc->encoder), \
##__VA_ARGS__)
#define do_log(level, format, ...) \
blog(level, "[FFmpeg %s encoder: '%s'] " format, enc->type, \
obs_encoder_get_name(enc->encoder), ##__VA_ARGS__)
#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__)
#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__)
struct enc_encoder {
obs_encoder_t *encoder;
obs_encoder_t *encoder;
const char *type;
const char *type;
AVCodec *codec;
AVCodecContext *context;
AVCodec *codec;
AVCodecContext *context;
uint8_t *samples[MAX_AV_PLANES];
AVFrame *aframe;
int64_t total_samples;
uint8_t *samples[MAX_AV_PLANES];
AVFrame *aframe;
int64_t total_samples;
DARRAY(uint8_t) packet_buffer;
DARRAY(uint8_t) packet_buffer;
size_t audio_planes;
size_t audio_size;
size_t audio_planes;
size_t audio_size;
int frame_size; /* pretty much always 1024 for AAC */
int frame_size_bytes;
int frame_size; /* pretty much always 1024 for AAC */
int frame_size_bytes;
};
static inline uint64_t convert_speaker_layout(enum speaker_layout layout)
{
switch (layout) {
case SPEAKERS_UNKNOWN: return 0;
case SPEAKERS_MONO: return AV_CH_LAYOUT_MONO;
case SPEAKERS_STEREO: return AV_CH_LAYOUT_STEREO;
case SPEAKERS_2POINT1: return AV_CH_LAYOUT_SURROUND;
case SPEAKERS_4POINT0: return AV_CH_LAYOUT_4POINT0;
case SPEAKERS_4POINT1: return AV_CH_LAYOUT_4POINT1;
case SPEAKERS_5POINT1: return AV_CH_LAYOUT_5POINT1_BACK;
case SPEAKERS_7POINT1: return AV_CH_LAYOUT_7POINT1;
case SPEAKERS_UNKNOWN:
return 0;
case SPEAKERS_MONO:
return AV_CH_LAYOUT_MONO;
case SPEAKERS_STEREO:
return AV_CH_LAYOUT_STEREO;
case SPEAKERS_2POINT1:
return AV_CH_LAYOUT_SURROUND;
case SPEAKERS_4POINT0:
return AV_CH_LAYOUT_4POINT0;
case SPEAKERS_4POINT1:
return AV_CH_LAYOUT_4POINT1;
case SPEAKERS_5POINT1:
return AV_CH_LAYOUT_5POINT1_BACK;
case SPEAKERS_7POINT1:
return AV_CH_LAYOUT_7POINT1;
}
/* shouldn't get here */
return 0;
}
static inline enum speaker_layout convert_ff_channel_layout(uint64_t channel_layout)
static inline enum speaker_layout
convert_ff_channel_layout(uint64_t channel_layout)
{
switch (channel_layout) {
case AV_CH_LAYOUT_MONO: return SPEAKERS_MONO;
case AV_CH_LAYOUT_STEREO: return SPEAKERS_STEREO;
case AV_CH_LAYOUT_SURROUND: return SPEAKERS_2POINT1;
case AV_CH_LAYOUT_4POINT0: return SPEAKERS_4POINT0;
case AV_CH_LAYOUT_4POINT1: return SPEAKERS_4POINT1;
case AV_CH_LAYOUT_5POINT1_BACK: return SPEAKERS_5POINT1;
case AV_CH_LAYOUT_7POINT1: return SPEAKERS_7POINT1;
case AV_CH_LAYOUT_MONO:
return SPEAKERS_MONO;
case AV_CH_LAYOUT_STEREO:
return SPEAKERS_STEREO;
case AV_CH_LAYOUT_SURROUND:
return SPEAKERS_2POINT1;
case AV_CH_LAYOUT_4POINT0:
return SPEAKERS_4POINT0;
case AV_CH_LAYOUT_4POINT1:
return SPEAKERS_4POINT1;
case AV_CH_LAYOUT_5POINT1_BACK:
return SPEAKERS_5POINT1;
case AV_CH_LAYOUT_7POINT1:
return SPEAKERS_7POINT1;
}
/* shouldn't get here */
return SPEAKERS_UNKNOWN;
return SPEAKERS_UNKNOWN;
}
static const char *aac_getname(void *unused)
@ -121,7 +135,7 @@ static bool initialize_codec(struct enc_encoder *enc)
{
int ret;
enc->aframe = av_frame_alloc();
enc->aframe = av_frame_alloc();
if (!enc->aframe) {
warn("Failed to allocate audio frame");
return false;
@ -144,7 +158,7 @@ static bool initialize_codec(struct enc_encoder *enc)
enc->frame_size_bytes = enc->frame_size * (int)enc->audio_size;
ret = av_samples_alloc(enc->samples, NULL, enc->context->channels,
enc->frame_size, enc->context->sample_fmt, 0);
enc->frame_size, enc->context->sample_fmt, 0);
if (ret < 0) {
warn("Failed to create audio buffer: %s", av_err2str(ret));
return false;
@ -158,11 +172,11 @@ static void init_sizes(struct enc_encoder *enc, audio_t *audio)
const struct audio_output_info *aoi;
enum audio_format format;
aoi = audio_output_get_info(audio);
aoi = audio_output_get_info(audio);
format = convert_ffmpeg_sample_format(enc->context->sample_fmt);
enc->audio_planes = get_audio_planes(format, aoi->speakers);
enc->audio_size = get_audio_size(format, aoi->speakers, 1);
enc->audio_size = get_audio_size(format, aoi->speakers, 1);
}
#ifndef MIN
@ -170,22 +184,24 @@ static void init_sizes(struct enc_encoder *enc, audio_t *audio)
#endif
static void *enc_create(obs_data_t *settings, obs_encoder_t *encoder,
const char *type, const char *alt)
const char *type, const char *alt)
{
struct enc_encoder *enc;
int bitrate = (int)obs_data_get_int(settings, "bitrate");
audio_t *audio = obs_encoder_audio(encoder);
int bitrate = (int)obs_data_get_int(settings, "bitrate");
audio_t *audio = obs_encoder_audio(encoder);
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 9, 100)
avcodec_register_all();
#endif
enc = bzalloc(sizeof(struct enc_encoder));
enc = bzalloc(sizeof(struct enc_encoder));
enc->encoder = encoder;
enc->codec = avcodec_find_encoder_by_name(type);
enc->type = type;
enc->codec = avcodec_find_encoder_by_name(type);
enc->type = type;
if (!enc->codec && alt) {
enc->codec = avcodec_find_encoder_by_name(alt);
enc->type = alt;
enc->type = alt;
}
blog(LOG_INFO, "---------------------------------");
@ -206,14 +222,15 @@ static void *enc_create(obs_data_t *settings, obs_encoder_t *encoder,
goto fail;
}
enc->context->bit_rate = bitrate * 1000;
enc->context->bit_rate = bitrate * 1000;
const struct audio_output_info *aoi;
aoi = audio_output_get_info(audio);
enc->context->channels = (int)audio_output_get_channels(audio);
enc->context->channels = (int)audio_output_get_channels(audio);
enc->context->channel_layout = convert_speaker_layout(aoi->speakers);
enc->context->sample_rate = audio_output_get_sample_rate(audio);
enc->context->sample_fmt = enc->codec->sample_fmts ?
enc->codec->sample_fmts[0] : AV_SAMPLE_FMT_FLTP;
enc->context->sample_fmt = enc->codec->sample_fmts
? enc->codec->sample_fmts[0]
: AV_SAMPLE_FMT_FLTP;
/* check to make sure sample rate is supported */
if (enc->codec->supported_samplerates) {
@ -239,9 +256,9 @@ static void *enc_create(obs_data_t *settings, obs_encoder_t *encoder,
}
info("bitrate: %" PRId64 ", channels: %d, channel_layout: %x\n",
(int64_t)enc->context->bit_rate / 1000,
(int)enc->context->channels,
(unsigned int)enc->context->channel_layout);
(int64_t)enc->context->bit_rate / 1000,
(int)enc->context->channels,
(unsigned int)enc->context->channel_layout);
init_sizes(enc, audio);
@ -268,22 +285,23 @@ static void *opus_create(obs_data_t *settings, obs_encoder_t *encoder)
return enc_create(settings, encoder, "libopus", "opus");
}
static bool do_encode(struct enc_encoder *enc,
struct encoder_packet *packet, bool *received_packet)
static bool do_encode(struct enc_encoder *enc, struct encoder_packet *packet,
bool *received_packet)
{
AVRational time_base = {1, enc->context->sample_rate};
AVPacket avpacket = {0};
int got_packet;
int ret;
AVPacket avpacket = {0};
int got_packet;
int ret;
enc->aframe->nb_samples = enc->frame_size;
enc->aframe->pts = av_rescale_q(enc->total_samples,
(AVRational){1, enc->context->sample_rate},
enc->context->time_base);
enc->aframe->pts = av_rescale_q(
enc->total_samples, (AVRational){1, enc->context->sample_rate},
enc->context->time_base);
ret = avcodec_fill_audio_frame(enc->aframe, enc->context->channels,
enc->context->sample_fmt, enc->samples[0],
enc->frame_size_bytes * enc->context->channels, 1);
ret = avcodec_fill_audio_frame(
enc->aframe, enc->context->channels, enc->context->sample_fmt,
enc->samples[0], enc->frame_size_bytes * enc->context->channels,
1);
if (ret < 0) {
warn("avcodec_fill_audio_frame failed: %s", av_err2str(ret));
return false;
@ -302,7 +320,7 @@ static bool do_encode(struct enc_encoder *enc,
ret = 0;
#else
ret = avcodec_encode_audio2(enc->context, &avpacket, enc->aframe,
&got_packet);
&got_packet);
#endif
if (ret < 0) {
warn("avcodec_encode_audio2 failed: %s", av_err2str(ret));
@ -316,8 +334,8 @@ static bool do_encode(struct enc_encoder *enc,
da_resize(enc->packet_buffer, 0);
da_push_back_array(enc->packet_buffer, avpacket.data, avpacket.size);
packet->pts = rescale_ts(avpacket.pts, enc->context, time_base);
packet->dts = rescale_ts(avpacket.dts, enc->context, time_base);
packet->pts = rescale_ts(avpacket.pts, enc->context, time_base);
packet->dts = rescale_ts(avpacket.dts, enc->context, time_base);
packet->data = enc->packet_buffer.array;
packet->size = avpacket.size;
packet->type = OBS_ENCODER_AUDIO;
@ -328,7 +346,7 @@ static bool do_encode(struct enc_encoder *enc,
}
static bool enc_encode(void *data, struct encoder_frame *frame,
struct encoder_packet *packet, bool *received_packet)
struct encoder_packet *packet, bool *received_packet)
{
struct enc_encoder *enc = data;
@ -348,8 +366,8 @@ static obs_properties_t *enc_properties(void *unused)
UNUSED_PARAMETER(unused);
obs_properties_t *props = obs_properties_create();
obs_properties_add_int(props, "bitrate",
obs_module_text("Bitrate"), 64, 1024, 32);
obs_properties_add_int(props, "bitrate", obs_module_text("Bitrate"), 64,
1024, 32);
return props;
}
@ -358,7 +376,7 @@ static bool enc_extra_data(void *data, uint8_t **extra_data, size_t *size)
struct enc_encoder *enc = data;
*extra_data = enc->context->extradata;
*size = enc->context->extradata_size;
*size = enc->context->extradata_size;
return true;
}
@ -367,41 +385,42 @@ static void enc_audio_info(void *data, struct audio_convert_info *info)
struct enc_encoder *enc = data;
info->format = convert_ffmpeg_sample_format(enc->context->sample_fmt);
info->samples_per_sec = (uint32_t)enc->context->sample_rate;
info->speakers = convert_ff_channel_layout(enc->context->channel_layout);
info->speakers =
convert_ff_channel_layout(enc->context->channel_layout);
}
static size_t enc_frame_size(void *data)
{
struct enc_encoder *enc =data;
struct enc_encoder *enc = data;
return enc->frame_size;
}
struct obs_encoder_info aac_encoder_info = {
.id = "ffmpeg_aac",
.type = OBS_ENCODER_AUDIO,
.codec = "AAC",
.get_name = aac_getname,
.create = aac_create,
.destroy = enc_destroy,
.encode = enc_encode,
.id = "ffmpeg_aac",
.type = OBS_ENCODER_AUDIO,
.codec = "AAC",
.get_name = aac_getname,
.create = aac_create,
.destroy = enc_destroy,
.encode = enc_encode,
.get_frame_size = enc_frame_size,
.get_defaults = enc_defaults,
.get_defaults = enc_defaults,
.get_properties = enc_properties,
.get_extra_data = enc_extra_data,
.get_audio_info = enc_audio_info
.get_audio_info = enc_audio_info,
};
struct obs_encoder_info opus_encoder_info = {
.id = "ffmpeg_opus",
.type = OBS_ENCODER_AUDIO,
.codec = "opus",
.get_name = opus_getname,
.create = opus_create,
.destroy = enc_destroy,
.encode = enc_encode,
.id = "ffmpeg_opus",
.type = OBS_ENCODER_AUDIO,
.codec = "opus",
.get_name = opus_getname,
.create = opus_create,
.destroy = enc_destroy,
.encode = enc_encode,
.get_frame_size = enc_frame_size,
.get_defaults = enc_defaults,
.get_defaults = enc_defaults,
.get_properties = enc_properties,
.get_extra_data = enc_extra_data,
.get_audio_info = enc_audio_info
.get_audio_info = enc_audio_info,
};