yolobs-studio/plugins/coreaudio-encoder/encoder.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

1430 lines
36 KiB
C++
Raw Permalink Normal View History

2016-02-23 23:16:51 +00:00
#include <util/dstr.hpp>
#include <obs-module.h>
#include <algorithm>
#include <cstdlib>
#include <initializer_list>
#include <memory>
#include <mutex>
#include <vector>
#ifndef _WIN32
#include <AudioToolbox/AudioToolbox.h>
2019-07-27 12:47:10 +00:00
#include <util/apple/cfstring-utils.h>
2016-02-23 23:16:51 +00:00
#endif
#define CA_LOG(level, format, ...) \
blog(level, "[CoreAudio encoder]: " format, ##__VA_ARGS__)
2019-09-22 21:19:10 +00:00
#define CA_LOG_ENCODER(format_name, encoder, level, format, ...) \
blog(level, "[CoreAudio %s: '%s']: " format, format_name, \
obs_encoder_get_name(encoder), ##__VA_ARGS__)
#define CA_BLOG(level, format, ...) \
2016-02-23 23:16:51 +00:00
CA_LOG_ENCODER(ca->format_name, ca->encoder, level, format, \
2019-09-22 21:19:10 +00:00
##__VA_ARGS__)
#define CA_CO_LOG(level, format, ...) \
do { \
if (ca) \
2016-02-23 23:16:51 +00:00
CA_BLOG(level, format, ##__VA_ARGS__); \
2019-09-22 21:19:10 +00:00
else \
CA_LOG(level, format, ##__VA_ARGS__); \
2016-02-23 23:16:51 +00:00
} while (false)
#ifdef _WIN32
#include "windows-imports.h"
#endif
using namespace std;
namespace {
struct asbd_builder {
AudioStreamBasicDescription asbd;
asbd_builder &sample_rate(Float64 rate)
{
asbd.mSampleRate = rate;
return *this;
}
asbd_builder &format_id(UInt32 format)
{
asbd.mFormatID = format;
return *this;
}
asbd_builder &format_flags(UInt32 flags)
{
asbd.mFormatFlags = flags;
return *this;
}
asbd_builder &bytes_per_packet(UInt32 bytes)
{
asbd.mBytesPerPacket = bytes;
return *this;
}
asbd_builder &frames_per_packet(UInt32 frames)
{
asbd.mFramesPerPacket = frames;
return *this;
}
asbd_builder &bytes_per_frame(UInt32 bytes)
{
asbd.mBytesPerFrame = bytes;
return *this;
}
asbd_builder &channels_per_frame(UInt32 channels)
{
asbd.mChannelsPerFrame = channels;
return *this;
}
asbd_builder &bits_per_channel(UInt32 bits)
{
asbd.mBitsPerChannel = bits;
return *this;
}
};
struct ca_encoder {
2019-09-22 21:19:10 +00:00
obs_encoder_t *encoder = nullptr;
const char *format_name = nullptr;
UInt32 format_id = 0;
2016-02-23 23:16:51 +00:00
const initializer_list<UInt32> *allowed_formats = nullptr;
AudioConverterRef converter = nullptr;
2019-09-22 21:19:10 +00:00
size_t output_buffer_size = 0;
vector<uint8_t> output_buffer;
2016-02-23 23:16:51 +00:00
2019-09-22 21:19:10 +00:00
size_t out_frames_per_packet = 0;
2016-02-23 23:16:51 +00:00
2019-09-22 21:19:10 +00:00
size_t in_packets = 0;
size_t in_frame_size = 0;
size_t in_bytes_required = 0;
2016-02-23 23:16:51 +00:00
2019-09-22 21:19:10 +00:00
vector<uint8_t> input_buffer;
vector<uint8_t> encode_buffer;
2016-02-23 23:16:51 +00:00
2019-09-22 21:19:10 +00:00
uint64_t total_samples = 0;
uint64_t samples_per_second = 0;
2016-02-23 23:16:51 +00:00
2019-09-22 21:19:10 +00:00
vector<uint8_t> extra_data;
2016-02-23 23:16:51 +00:00
2019-09-22 21:19:10 +00:00
size_t channels = 0;
2016-02-23 23:16:51 +00:00
~ca_encoder()
{
if (converter)
AudioConverterDispose(converter);
}
};
typedef struct ca_encoder ca_encoder;
}
namespace std {
#ifndef _WIN32
2019-09-22 21:19:10 +00:00
template<> struct default_delete<remove_pointer<CFErrorRef>::type> {
2016-02-23 23:16:51 +00:00
void operator()(remove_pointer<CFErrorRef>::type *err)
{
CFRelease(err);
}
};
2019-09-22 21:19:10 +00:00
template<> struct default_delete<remove_pointer<CFStringRef>::type> {
2016-02-23 23:16:51 +00:00
void operator()(remove_pointer<CFStringRef>::type *str)
{
CFRelease(str);
}
};
#endif
2019-09-22 21:19:10 +00:00
template<> struct default_delete<remove_pointer<AudioConverterRef>::type> {
2016-02-23 23:16:51 +00:00
void operator()(AudioConverterRef converter)
{
AudioConverterDispose(converter);
}
};
}
2019-09-22 21:19:10 +00:00
template<typename T>
2016-02-23 23:16:51 +00:00
using cf_ptr = unique_ptr<typename remove_pointer<T>::type>;
#ifndef _MSC_VER
__attribute__((__format__(__printf__, 3, 4)))
#endif
2019-09-22 21:19:10 +00:00
static void
log_to_dstr(DStr &str, ca_encoder *ca, const char *fmt, ...)
2016-02-23 23:16:51 +00:00
{
2019-09-22 21:19:10 +00:00
dstr prev_str = *static_cast<dstr *>(str);
2016-02-23 23:16:51 +00:00
va_list args;
va_start(args, fmt);
dstr_vcatf(str, fmt, args);
va_end(args);
if (str->array)
return;
char array[4096];
va_start(args, fmt);
vsnprintf(array, 4096, fmt, args);
va_end(args);
array[4095] = 0;
if (!prev_str.array && !prev_str.len)
2019-09-22 21:19:10 +00:00
CA_CO_LOG(LOG_ERROR,
"Could not allocate buffer for logging:"
"\n'%s'",
array);
2016-02-23 23:16:51 +00:00
else
2019-09-22 21:19:10 +00:00
CA_CO_LOG(LOG_ERROR,
"Could not allocate buffer for logging:"
"\n'%s'\nPrevious log entries:\n%s",
array, prev_str.array);
2016-02-23 23:16:51 +00:00
bfree(prev_str.array);
}
static const char *flush_log(DStr &log)
{
if (!log->array || !log->len)
return "";
if (log->array[log->len - 1] == '\n') {
log->array[log->len - 1] = 0; //Get rid of last newline
log->len -= 1;
}
return log->array;
}
#define CA_CO_DLOG_(level, format) \
2019-09-22 21:19:10 +00:00
CA_CO_LOG(level, format "%s%s", log->array ? ":\n" : "", flush_log(log))
#define CA_CO_DLOG(level, format, ...) \
2016-02-23 23:16:51 +00:00
CA_CO_LOG(level, format "%s%s", ##__VA_ARGS__, \
2019-09-22 21:19:10 +00:00
log->array ? ":\n" : "", flush_log(log))
2016-02-23 23:16:51 +00:00
2019-09-22 21:19:10 +00:00
static const char *aac_get_name(void *)
2016-02-23 23:16:51 +00:00
{
return obs_module_text("CoreAudioAAC");
}
static const char *code_to_str(OSStatus code)
{
switch (code) {
2019-09-22 21:19:10 +00:00
#define HANDLE_CODE(c) \
case c: \
return #c
HANDLE_CODE(kAudio_UnimplementedError);
HANDLE_CODE(kAudio_FileNotFoundError);
HANDLE_CODE(kAudio_FilePermissionError);
HANDLE_CODE(kAudio_TooManyFilesOpenError);
HANDLE_CODE(kAudio_BadFilePathError);
HANDLE_CODE(kAudio_ParamError);
HANDLE_CODE(kAudio_MemFullError);
HANDLE_CODE(kAudioConverterErr_FormatNotSupported);
HANDLE_CODE(kAudioConverterErr_OperationNotSupported);
HANDLE_CODE(kAudioConverterErr_PropertyNotSupported);
HANDLE_CODE(kAudioConverterErr_InvalidInputSize);
HANDLE_CODE(kAudioConverterErr_InvalidOutputSize);
HANDLE_CODE(kAudioConverterErr_UnspecifiedError);
HANDLE_CODE(kAudioConverterErr_BadPropertySizeError);
HANDLE_CODE(kAudioConverterErr_RequiresPacketDescriptionsError);
HANDLE_CODE(kAudioConverterErr_InputSampleRateOutOfRange);
HANDLE_CODE(kAudioConverterErr_OutputSampleRateOutOfRange);
2016-02-23 23:16:51 +00:00
#undef HANDLE_CODE
2019-09-22 21:19:10 +00:00
default:
break;
2016-02-23 23:16:51 +00:00
}
return NULL;
}
static DStr osstatus_to_dstr(OSStatus code)
{
DStr result;
#ifndef _WIN32
2019-09-22 21:19:10 +00:00
cf_ptr<CFErrorRef> err{CFErrorCreate(
kCFAllocatorDefault, kCFErrorDomainOSStatus, code, NULL)};
2016-02-23 23:16:51 +00:00
cf_ptr<CFStringRef> str{CFErrorCopyDescription(err.get())};
2019-07-27 12:47:10 +00:00
if (cfstr_copy_dstr(str.get(), kCFStringEncodingUTF8, result))
2016-02-23 23:16:51 +00:00
return result;
#endif
const char *code_str = code_to_str(code);
2019-09-22 21:19:10 +00:00
dstr_printf(result, "%s%s%d%s", code_str ? code_str : "",
code_str ? " (" : "", static_cast<int>(code),
code_str ? ")" : "");
2016-02-23 23:16:51 +00:00
return result;
}
static void log_osstatus(int log_level, ca_encoder *ca, const char *context,
2019-09-22 21:19:10 +00:00
OSStatus code)
2016-02-23 23:16:51 +00:00
{
DStr str = osstatus_to_dstr(code);
if (ca)
CA_BLOG(log_level, "Error in %s: %s", context, str->array);
else
CA_LOG(log_level, "Error in %s: %s", context, str->array);
}
static const char *format_id_to_str(UInt32 format_id)
{
2019-09-22 21:19:10 +00:00
#define FORMAT_TO_STR(x) \
case x: \
return #x
2016-02-23 23:16:51 +00:00
switch (format_id) {
2019-09-22 21:19:10 +00:00
FORMAT_TO_STR(kAudioFormatLinearPCM);
FORMAT_TO_STR(kAudioFormatAC3);
FORMAT_TO_STR(kAudioFormat60958AC3);
FORMAT_TO_STR(kAudioFormatAppleIMA4);
FORMAT_TO_STR(kAudioFormatMPEG4AAC);
FORMAT_TO_STR(kAudioFormatMPEG4CELP);
FORMAT_TO_STR(kAudioFormatMPEG4HVXC);
FORMAT_TO_STR(kAudioFormatMPEG4TwinVQ);
FORMAT_TO_STR(kAudioFormatMACE3);
FORMAT_TO_STR(kAudioFormatMACE6);
FORMAT_TO_STR(kAudioFormatULaw);
FORMAT_TO_STR(kAudioFormatALaw);
FORMAT_TO_STR(kAudioFormatQDesign);
FORMAT_TO_STR(kAudioFormatQDesign2);
FORMAT_TO_STR(kAudioFormatQUALCOMM);
FORMAT_TO_STR(kAudioFormatMPEGLayer1);
FORMAT_TO_STR(kAudioFormatMPEGLayer2);
FORMAT_TO_STR(kAudioFormatMPEGLayer3);
FORMAT_TO_STR(kAudioFormatTimeCode);
FORMAT_TO_STR(kAudioFormatMIDIStream);
FORMAT_TO_STR(kAudioFormatParameterValueStream);
FORMAT_TO_STR(kAudioFormatAppleLossless);
FORMAT_TO_STR(kAudioFormatMPEG4AAC_HE);
FORMAT_TO_STR(kAudioFormatMPEG4AAC_LD);
FORMAT_TO_STR(kAudioFormatMPEG4AAC_ELD);
FORMAT_TO_STR(kAudioFormatMPEG4AAC_ELD_SBR);
FORMAT_TO_STR(kAudioFormatMPEG4AAC_HE_V2);
FORMAT_TO_STR(kAudioFormatMPEG4AAC_Spatial);
FORMAT_TO_STR(kAudioFormatAMR);
FORMAT_TO_STR(kAudioFormatAudible);
FORMAT_TO_STR(kAudioFormatiLBC);
FORMAT_TO_STR(kAudioFormatDVIIntelIMA);
FORMAT_TO_STR(kAudioFormatMicrosoftGSM);
FORMAT_TO_STR(kAudioFormatAES3);
2016-02-23 23:16:51 +00:00
}
#undef FORMAT_TO_STR
return "Unknown format";
}
static void aac_destroy(void *data)
{
2019-09-22 21:19:10 +00:00
ca_encoder *ca = static_cast<ca_encoder *>(data);
2016-02-23 23:16:51 +00:00
delete ca;
}
2019-09-22 21:19:10 +00:00
template<typename Func>
2016-02-23 23:16:51 +00:00
static bool query_converter_property_raw(DStr &log, ca_encoder *ca,
2019-09-22 21:19:10 +00:00
AudioFormatPropertyID property,
const char *get_property_info,
const char *get_property,
AudioConverterRef converter,
Func &&func)
2016-02-23 23:16:51 +00:00
{
UInt32 size = 0;
OSStatus code = AudioConverterGetPropertyInfo(converter, property,
2019-09-22 21:19:10 +00:00
&size, nullptr);
2016-02-23 23:16:51 +00:00
if (code) {
log_to_dstr(log, ca, "%s: %s\n", get_property_info,
2019-09-22 21:19:10 +00:00
osstatus_to_dstr(code)->array);
2016-02-23 23:16:51 +00:00
return false;
}
if (!size) {
log_to_dstr(log, ca, "%s returned 0 size\n", get_property_info);
return false;
}
vector<uint8_t> buffer;
2019-09-22 21:19:10 +00:00
2016-02-23 23:16:51 +00:00
try {
buffer.resize(size);
} catch (...) {
log_to_dstr(log, ca, "Failed to allocate %u bytes for %s\n",
2019-09-22 21:19:10 +00:00
static_cast<uint32_t>(size), get_property);
2016-02-23 23:16:51 +00:00
return false;
}
code = AudioConverterGetProperty(converter, property, &size,
2019-09-22 21:19:10 +00:00
buffer.data());
2016-02-23 23:16:51 +00:00
if (code) {
log_to_dstr(log, ca, "%s: %s\n", get_property,
2019-09-22 21:19:10 +00:00
osstatus_to_dstr(code)->array);
2016-02-23 23:16:51 +00:00
return false;
}
2019-09-22 21:19:10 +00:00
func(size, static_cast<void *>(buffer.data()));
2016-02-23 23:16:51 +00:00
return true;
}
2019-09-22 21:19:10 +00:00
#define EXPAND_CONVERTER_NAMES(x) \
x, "AudioConverterGetPropertyInfo(" #x ")", \
"AudioConverterGetProperty(" #x ")"
2016-02-23 23:16:51 +00:00
2019-09-22 21:19:10 +00:00
template<typename Func>
2016-02-23 23:16:51 +00:00
static bool enumerate_bitrates(DStr &log, ca_encoder *ca,
2019-09-22 21:19:10 +00:00
AudioConverterRef converter, Func &&func)
2016-02-23 23:16:51 +00:00
{
2019-09-22 21:19:10 +00:00
auto helper = [&](UInt32 size, void *data) {
auto range = static_cast<AudioValueRange *>(data);
2016-02-23 23:16:51 +00:00
size_t num_ranges = size / sizeof(AudioValueRange);
for (size_t i = 0; i < num_ranges; i++)
func(static_cast<UInt32>(range[i].mMinimum),
2019-09-22 21:19:10 +00:00
static_cast<UInt32>(range[i].mMaximum));
2016-02-23 23:16:51 +00:00
};
2019-09-22 21:19:10 +00:00
return query_converter_property_raw(
log, ca,
EXPAND_CONVERTER_NAMES(kAudioConverterApplicableEncodeBitRates),
converter, helper);
2016-02-23 23:16:51 +00:00
}
static bool bitrate_valid(DStr &log, ca_encoder *ca,
2019-09-22 21:19:10 +00:00
AudioConverterRef converter, UInt32 bitrate)
2016-02-23 23:16:51 +00:00
{
bool valid = false;
2019-09-22 21:19:10 +00:00
auto helper = [&](UInt32 min_, UInt32 max_) {
2016-02-23 23:16:51 +00:00
if (min_ == bitrate || max_ == bitrate)
valid = true;
};
enumerate_bitrates(log, ca, converter, helper);
return valid;
}
static bool create_encoder(DStr &log, ca_encoder *ca,
2019-09-22 21:19:10 +00:00
AudioStreamBasicDescription *in,
AudioStreamBasicDescription *out, UInt32 format_id,
UInt32 bitrate, UInt32 samplerate,
UInt32 rate_control)
2016-02-23 23:16:51 +00:00
{
2019-09-22 21:19:10 +00:00
#define STATUS_CHECK(c) \
code = c; \
if (code) { \
log_to_dstr(log, ca, #c " returned %s", \
osstatus_to_dstr(code)->array); \
return false; \
2016-02-23 23:16:51 +00:00
}
2019-09-22 21:19:10 +00:00
Float64 srate = samplerate ? (Float64)samplerate
: (Float64)ca->samples_per_second;
2016-02-23 23:16:51 +00:00
auto out_ = asbd_builder()
2019-09-22 21:19:10 +00:00
.sample_rate(srate)
.channels_per_frame((UInt32)ca->channels)
.format_id(format_id)
.asbd;
2016-02-23 23:16:51 +00:00
UInt32 size = sizeof(*out);
OSStatus code;
2019-09-22 21:19:10 +00:00
STATUS_CHECK(AudioFormatGetProperty(kAudioFormatProperty_FormatInfo, 0,
NULL, &size, &out_));
2016-02-23 23:16:51 +00:00
*out = out_;
STATUS_CHECK(AudioConverterNew(in, out, &ca->converter))
2019-09-22 21:19:10 +00:00
STATUS_CHECK(AudioConverterSetProperty(
ca->converter, kAudioCodecPropertyBitRateControlMode,
sizeof(rate_control), &rate_control));
2016-02-23 23:16:51 +00:00
if (!bitrate_valid(log, ca, ca->converter, bitrate)) {
2019-09-22 21:19:10 +00:00
log_to_dstr(log, ca,
"Encoder does not support bitrate %u "
"for format %s (0x%x)\n",
(uint32_t)bitrate, format_id_to_str(format_id),
(uint32_t)format_id);
2016-02-23 23:16:51 +00:00
return false;
}
ca->format_id = format_id;
return true;
#undef STATUS_CHECK
}
static const initializer_list<UInt32> aac_formats = {
kAudioFormatMPEG4AAC_HE_V2,
kAudioFormatMPEG4AAC_HE,
kAudioFormatMPEG4AAC,
};
static const initializer_list<UInt32> aac_lc_formats = {
kAudioFormatMPEG4AAC,
};
static void *aac_create(obs_data_t *settings, obs_encoder_t *encoder)
{
2019-09-22 21:19:10 +00:00
#define STATUS_CHECK(c) \
code = c; \
if (code) { \
2016-02-23 23:16:51 +00:00
log_osstatus(LOG_ERROR, ca.get(), #c, code); \
2019-09-22 21:19:10 +00:00
return nullptr; \
2016-02-23 23:16:51 +00:00
}
UInt32 bitrate = (UInt32)obs_data_get_int(settings, "bitrate") * 1000;
if (!bitrate) {
CA_LOG_ENCODER("AAC", encoder, LOG_ERROR,
2019-09-22 21:19:10 +00:00
"Invalid bitrate specified");
2016-02-23 23:16:51 +00:00
return NULL;
}
const enum audio_format format = AUDIO_FORMAT_FLOAT;
if (is_audio_planar(format)) {
CA_LOG_ENCODER("AAC", encoder, LOG_ERROR,
2019-09-22 21:19:10 +00:00
"Got non-interleaved audio format %d", format);
2016-02-23 23:16:51 +00:00
return NULL;
}
unique_ptr<ca_encoder> ca;
try {
ca.reset(new ca_encoder());
} catch (...) {
CA_LOG_ENCODER("AAC", encoder, LOG_ERROR,
2019-09-22 21:19:10 +00:00
"Could not allocate encoder");
2016-02-23 23:16:51 +00:00
return nullptr;
}
ca->encoder = encoder;
ca->format_name = "AAC";
audio_t *audio = obs_encoder_audio(encoder);
const struct audio_output_info *aoi = audio_output_get_info(audio);
ca->channels = audio_output_get_channels(audio);
ca->samples_per_second = audio_output_get_sample_rate(audio);
2019-09-22 21:19:10 +00:00
size_t bytes_per_frame = get_audio_size(format, aoi->speakers, 1);
2016-02-23 23:16:51 +00:00
size_t bits_per_channel = get_audio_bytes_per_channel(format) * 8;
auto in = asbd_builder()
2019-09-22 21:19:10 +00:00
.sample_rate((Float64)ca->samples_per_second)
.channels_per_frame((UInt32)ca->channels)
.bytes_per_frame((UInt32)bytes_per_frame)
.frames_per_packet(1)
.bytes_per_packet((UInt32)(1 * bytes_per_frame))
.bits_per_channel((UInt32)bits_per_channel)
.format_id(kAudioFormatLinearPCM)
.format_flags(kAudioFormatFlagsNativeEndian |
kAudioFormatFlagIsPacked |
kAudioFormatFlagIsFloat | 0)
.asbd;
2016-02-23 23:16:51 +00:00
AudioStreamBasicDescription out;
UInt32 rate_control = kAudioCodecBitRateControlMode_Constant;
2018-02-19 19:54:37 +00:00
if (obs_data_get_bool(settings, "allow he-aac") && ca->channels != 3) {
2016-02-23 23:16:51 +00:00
ca->allowed_formats = &aac_formats;
} else {
ca->allowed_formats = &aac_lc_formats;
}
auto samplerate =
static_cast<UInt32>(obs_data_get_int(settings, "samplerate"));
DStr log;
bool encoder_created = false;
for (UInt32 format_id : *ca->allowed_formats) {
log_to_dstr(log, ca.get(), "Trying format %s (0x%x)\n",
2019-09-22 21:19:10 +00:00
format_id_to_str(format_id), (uint32_t)format_id);
2016-02-23 23:16:51 +00:00
if (!create_encoder(log, ca.get(), &in, &out, format_id,
2019-09-22 21:19:10 +00:00
bitrate, samplerate, rate_control))
2016-02-23 23:16:51 +00:00
continue;
encoder_created = true;
break;
}
if (!encoder_created) {
2019-09-22 21:19:10 +00:00
CA_CO_DLOG(LOG_ERROR,
"Could not create encoder for "
"selected format%s",
ca->allowed_formats->size() == 1 ? "" : "s");
2016-02-23 23:16:51 +00:00
return nullptr;
}
if (log->len)
CA_CO_DLOG_(LOG_DEBUG, "Encoder created");
OSStatus code;
UInt32 converter_quality = kAudioConverterQuality_Max;
2019-09-22 21:19:10 +00:00
STATUS_CHECK(AudioConverterSetProperty(
ca->converter, kAudioConverterCodecQuality,
sizeof(converter_quality), &converter_quality));
2016-02-23 23:16:51 +00:00
STATUS_CHECK(AudioConverterSetProperty(ca->converter,
2019-09-22 21:19:10 +00:00
kAudioConverterEncodeBitRate,
sizeof(bitrate), &bitrate));
2016-02-23 23:16:51 +00:00
UInt32 size = sizeof(in);
2019-09-22 21:19:10 +00:00
STATUS_CHECK(AudioConverterGetProperty(
ca->converter, kAudioConverterCurrentInputStreamDescription,
&size, &in));
2016-02-23 23:16:51 +00:00
size = sizeof(out);
2019-09-22 21:19:10 +00:00
STATUS_CHECK(AudioConverterGetProperty(
ca->converter, kAudioConverterCurrentOutputStreamDescription,
&size, &out));
2016-02-23 23:16:51 +00:00
2018-02-19 19:54:37 +00:00
/*
* Fix channel map differences between CoreAudio AAC, FFmpeg, Wav
2020-10-01 20:15:25 +00:00
* New channel mappings below assume 2.1, 4.0, 4.1, 5.1, 7.1 resp.
2018-02-19 19:54:37 +00:00
*/
2020-10-01 20:15:25 +00:00
2018-02-19 19:54:37 +00:00
if (ca->channels == 3) {
SInt32 channelMap3[3] = {2, 0, 1};
AudioConverterSetProperty(ca->converter,
2019-09-22 21:19:10 +00:00
kAudioConverterChannelMap,
sizeof(channelMap3), channelMap3);
2018-02-19 19:54:37 +00:00
2020-10-01 20:15:25 +00:00
} else if (ca->channels == 4) {
/*
* For four channels coreaudio encoder has default channel "quad"
* instead of 4.0. So explicitly set channel layout to
* kAudioChannelLayoutTag_MPEG_4_0_B = (116L << 16) | 4.
*/
AudioChannelLayout inAcl = {0};
inAcl.mChannelLayoutTag = (116L << 16) | 4;
AudioConverterSetProperty(ca->converter,
kAudioConverterInputChannelLayout,
sizeof(inAcl), &inAcl);
AudioConverterSetProperty(ca->converter,
kAudioConverterOutputChannelLayout,
sizeof(inAcl), &inAcl);
SInt32 channelMap4[4] = {2, 0, 1, 3};
AudioConverterSetProperty(ca->converter,
kAudioConverterChannelMap,
sizeof(channelMap4), channelMap4);
2018-02-19 19:54:37 +00:00
} else if (ca->channels == 5) {
SInt32 channelMap5[5] = {2, 0, 1, 3, 4};
AudioConverterSetProperty(ca->converter,
2019-09-22 21:19:10 +00:00
kAudioConverterChannelMap,
sizeof(channelMap5), channelMap5);
2018-02-19 19:54:37 +00:00
} else if (ca->channels == 6) {
SInt32 channelMap6[6] = {2, 0, 1, 4, 5, 3};
AudioConverterSetProperty(ca->converter,
2019-09-22 21:19:10 +00:00
kAudioConverterChannelMap,
sizeof(channelMap6), channelMap6);
2018-02-19 19:54:37 +00:00
} else if (ca->channels == 8) {
SInt32 channelMap8[8] = {2, 0, 1, 6, 7, 4, 5, 3};
AudioConverterSetProperty(ca->converter,
2019-09-22 21:19:10 +00:00
kAudioConverterChannelMap,
sizeof(channelMap8), channelMap8);
2018-02-19 19:54:37 +00:00
}
2019-09-22 21:19:10 +00:00
ca->in_frame_size = in.mBytesPerFrame;
ca->in_packets = out.mFramesPerPacket / in.mFramesPerPacket;
2016-02-23 23:16:51 +00:00
ca->in_bytes_required = ca->in_packets * ca->in_frame_size;
ca->out_frames_per_packet = out.mFramesPerPacket;
ca->output_buffer_size = out.mBytesPerPacket;
if (out.mBytesPerPacket == 0) {
UInt32 max_packet_size = 0;
size = sizeof(max_packet_size);
2019-09-22 21:19:10 +00:00
code = AudioConverterGetProperty(
ca->converter,
kAudioConverterPropertyMaximumOutputPacketSize, &size,
&max_packet_size);
2016-02-23 23:16:51 +00:00
if (code) {
log_osstatus(LOG_WARNING, ca.get(),
2019-09-22 21:19:10 +00:00
"AudioConverterGetProperty(PacketSz)",
code);
2016-02-23 23:16:51 +00:00
ca->output_buffer_size = 32768;
} else {
ca->output_buffer_size = max_packet_size;
}
}
try {
ca->output_buffer.resize(ca->output_buffer_size);
} catch (...) {
CA_BLOG(LOG_ERROR, "Failed to allocate output buffer");
return nullptr;
}
const char *format_name =
2019-09-22 21:19:10 +00:00
out.mFormatID == kAudioFormatMPEG4AAC_HE_V2
? "HE-AAC v2"
: out.mFormatID == kAudioFormatMPEG4AAC_HE ? "HE-AAC"
: "AAC";
CA_BLOG(LOG_INFO,
"settings:\n"
"\tmode: %s\n"
"\tbitrate: %u\n"
"\tsample rate: %llu\n"
"\tcbr: %s\n"
"\toutput buffer: %lu",
format_name, (unsigned int)bitrate / 1000,
ca->samples_per_second,
rate_control == kAudioCodecBitRateControlMode_Constant ? "on"
: "off",
(unsigned long)ca->output_buffer_size);
2016-02-23 23:16:51 +00:00
return ca.release();
#undef STATUS_CHECK
}
2019-09-22 21:19:10 +00:00
static OSStatus
complex_input_data_proc(AudioConverterRef inAudioConverter,
UInt32 *ioNumberDataPackets, AudioBufferList *ioData,
AudioStreamPacketDescription **outDataPacketDescription,
void *inUserData)
2016-02-23 23:16:51 +00:00
{
UNUSED_PARAMETER(inAudioConverter);
UNUSED_PARAMETER(outDataPacketDescription);
2019-09-22 21:19:10 +00:00
ca_encoder *ca = static_cast<ca_encoder *>(inUserData);
2016-02-23 23:16:51 +00:00
if (ca->input_buffer.size() < ca->in_bytes_required) {
*ioNumberDataPackets = 0;
ioData->mBuffers[0].mData = NULL;
return 1;
}
auto start = begin(ca->input_buffer);
2019-09-22 21:19:10 +00:00
auto stop = begin(ca->input_buffer) + ca->in_bytes_required;
2016-02-23 23:16:51 +00:00
ca->encode_buffer.assign(start, stop);
ca->input_buffer.erase(start, stop);
*ioNumberDataPackets =
(UInt32)(ca->in_bytes_required / ca->in_frame_size);
ioData->mNumberBuffers = 1;
ioData->mBuffers[0].mData = ca->encode_buffer.data();
ioData->mBuffers[0].mNumberChannels = (UInt32)ca->channels;
ioData->mBuffers[0].mDataByteSize = (UInt32)ca->in_bytes_required;
return 0;
}
#ifdef _MSC_VER
// disable warning that recommends if ((foo = bar > 0) == false) over
// if (!(foo = bar > 0))
#pragma warning(push)
2019-09-22 21:19:10 +00:00
#pragma warning(disable : 4706)
2016-02-23 23:16:51 +00:00
#endif
static bool aac_encode(void *data, struct encoder_frame *frame,
2019-09-22 21:19:10 +00:00
struct encoder_packet *packet, bool *received_packet)
2016-02-23 23:16:51 +00:00
{
2019-09-22 21:19:10 +00:00
ca_encoder *ca = static_cast<ca_encoder *>(data);
2016-02-23 23:16:51 +00:00
2019-09-22 21:19:10 +00:00
ca->input_buffer.insert(end(ca->input_buffer), frame->data[0],
frame->data[0] + frame->linesize[0]);
2016-02-23 23:16:51 +00:00
if (ca->input_buffer.size() < ca->in_bytes_required)
return true;
UInt32 packets = 1;
2019-09-22 21:19:10 +00:00
AudioBufferList buffer_list = {0};
2016-02-23 23:16:51 +00:00
buffer_list.mNumberBuffers = 1;
buffer_list.mBuffers[0].mNumberChannels = (UInt32)ca->channels;
buffer_list.mBuffers[0].mDataByteSize = (UInt32)ca->output_buffer_size;
buffer_list.mBuffers[0].mData = ca->output_buffer.data();
2019-09-22 21:19:10 +00:00
AudioStreamPacketDescription out_desc = {0};
2016-02-23 23:16:51 +00:00
2019-09-22 21:19:10 +00:00
OSStatus code = AudioConverterFillComplexBuffer(
ca->converter, complex_input_data_proc, ca, &packets,
&buffer_list, &out_desc);
2016-02-23 23:16:51 +00:00
if (code && code != 1) {
log_osstatus(LOG_ERROR, ca, "AudioConverterFillComplexBuffer",
2019-09-22 21:19:10 +00:00
code);
2016-02-23 23:16:51 +00:00
return false;
}
if (!(*received_packet = packets > 0))
return true;
packet->pts = ca->total_samples;
packet->dts = ca->total_samples;
packet->timebase_num = 1;
packet->timebase_den = (uint32_t)ca->samples_per_second;
packet->type = OBS_ENCODER_AUDIO;
packet->size = out_desc.mDataByteSize;
2019-09-22 21:19:10 +00:00
packet->data = (uint8_t *)buffer_list.mBuffers[0].mData +
out_desc.mStartOffset;
2016-02-23 23:16:51 +00:00
ca->total_samples += ca->in_bytes_required / ca->in_frame_size;
return true;
}
#ifdef _MSC_VER
#pragma warning(pop)
#endif
static void aac_audio_info(void *data, struct audio_convert_info *info)
{
UNUSED_PARAMETER(data);
info->format = AUDIO_FORMAT_FLOAT;
}
static size_t aac_frame_size(void *data)
{
2019-09-22 21:19:10 +00:00
ca_encoder *ca = static_cast<ca_encoder *>(data);
2016-02-23 23:16:51 +00:00
return ca->out_frames_per_packet;
}
/* The following code was extracted from encca_aac.c in HandBrake's libhb */
2019-09-22 21:19:10 +00:00
#define MP4ESDescrTag 0x03
#define MP4DecConfigDescrTag 0x04
#define MP4DecSpecificDescrTag 0x05
2016-02-23 23:16:51 +00:00
// based off of mov_mp4_read_descr_len from mov.c in ffmpeg's libavformat
static int read_descr_len(uint8_t **buffer)
{
int len = 0;
int count = 4;
2019-09-22 21:19:10 +00:00
while (count--) {
2016-02-23 23:16:51 +00:00
int c = *(*buffer)++;
len = (len << 7) | (c & 0x7f);
if (!(c & 0x80))
break;
}
return len;
}
// based off of mov_mp4_read_descr from mov.c in ffmpeg's libavformat
static int read_descr(uint8_t **buffer, int *tag)
{
*tag = *(*buffer)++;
return read_descr_len(buffer);
}
// based off of mov_read_esds from mov.c in ffmpeg's libavformat
2019-09-22 21:19:10 +00:00
static void read_esds_desc_ext(uint8_t *desc_ext, vector<uint8_t> &buffer,
bool version_flags)
2016-02-23 23:16:51 +00:00
{
uint8_t *esds = desc_ext;
int tag, len;
if (version_flags)
esds += 4; // version + flags
read_descr(&esds, &tag);
2019-09-22 21:19:10 +00:00
esds += 2; // ID
2016-02-23 23:16:51 +00:00
if (tag == MP4ESDescrTag)
2019-09-22 21:19:10 +00:00
esds++; // priority
2016-02-23 23:16:51 +00:00
read_descr(&esds, &tag);
if (tag == MP4DecConfigDescrTag) {
esds++; // object type id
esds++; // stream type
esds += 3; // buffer size db
esds += 4; // max bitrate
esds += 4; // average bitrate
len = read_descr(&esds, &tag);
if (tag == MP4DecSpecificDescrTag)
try {
buffer.assign(esds, esds + len);
} catch (...) {
//leave buffer empty
}
}
}
/* extracted code ends here */
static void query_extra_data(ca_encoder *ca)
{
UInt32 size = 0;
OSStatus code;
2019-09-22 21:19:10 +00:00
code = AudioConverterGetPropertyInfo(
ca->converter, kAudioConverterCompressionMagicCookie, &size,
NULL);
2016-02-23 23:16:51 +00:00
if (code) {
log_osstatus(LOG_ERROR, ca,
2019-09-22 21:19:10 +00:00
"AudioConverterGetPropertyInfo(magic_cookie)",
code);
2016-02-23 23:16:51 +00:00
return;
}
if (!size) {
CA_BLOG(LOG_WARNING, "Got 0 data size info for magic_cookie");
return;
}
vector<uint8_t> extra_data;
2019-09-22 21:19:10 +00:00
2016-02-23 23:16:51 +00:00
try {
extra_data.resize(size);
} catch (...) {
CA_BLOG(LOG_WARNING, "Could not allocate extra data buffer");
return;
}
code = AudioConverterGetProperty(ca->converter,
2019-09-22 21:19:10 +00:00
kAudioConverterCompressionMagicCookie,
&size, extra_data.data());
2016-02-23 23:16:51 +00:00
if (code) {
log_osstatus(LOG_ERROR, ca,
2019-09-22 21:19:10 +00:00
"AudioConverterGetProperty(magic_cookie)", code);
2016-02-23 23:16:51 +00:00
return;
}
if (!size) {
CA_BLOG(LOG_WARNING, "Got 0 data size for magic_cookie");
return;
}
read_esds_desc_ext(extra_data.data(), ca->extra_data, false);
}
static bool aac_extra_data(void *data, uint8_t **extra_data, size_t *size)
{
2019-09-22 21:19:10 +00:00
ca_encoder *ca = static_cast<ca_encoder *>(data);
2016-02-23 23:16:51 +00:00
if (!ca->extra_data.size())
query_extra_data(ca);
if (!ca->extra_data.size())
return false;
*extra_data = ca->extra_data.data();
*size = ca->extra_data.size();
return true;
}
static asbd_builder fill_common_asbd_fields(asbd_builder builder,
2019-09-22 21:19:10 +00:00
bool in = false,
UInt32 channels = 2)
2016-02-23 23:16:51 +00:00
{
2018-02-19 19:54:37 +00:00
UInt32 bytes_per_frame = sizeof(float) * channels;
2016-02-23 23:16:51 +00:00
UInt32 bits_per_channel = bytes_per_frame / channels * 8;
builder.channels_per_frame(channels);
if (in) {
2019-09-22 21:19:10 +00:00
builder.bytes_per_frame(bytes_per_frame)
2016-02-23 23:16:51 +00:00
.frames_per_packet(1)
.bytes_per_packet(1 * bytes_per_frame)
.bits_per_channel(bits_per_channel);
}
return builder;
}
static AudioStreamBasicDescription get_default_in_asbd()
{
return fill_common_asbd_fields(asbd_builder(), true)
.sample_rate(44100)
.format_id(kAudioFormatLinearPCM)
.format_flags(kAudioFormatFlagsNativeEndian |
2019-09-22 21:19:10 +00:00
kAudioFormatFlagIsPacked |
kAudioFormatFlagIsFloat | 0)
2016-02-23 23:16:51 +00:00
.asbd;
}
2018-02-19 19:54:37 +00:00
static asbd_builder get_default_out_asbd_builder(UInt32 channels)
2016-02-23 23:16:51 +00:00
{
2018-02-19 19:54:37 +00:00
return fill_common_asbd_fields(asbd_builder(), false, channels)
2016-02-23 23:16:51 +00:00
.sample_rate(44100);
}
2019-09-22 21:19:10 +00:00
static cf_ptr<AudioConverterRef>
get_converter(DStr &log, ca_encoder *ca, AudioStreamBasicDescription out,
AudioStreamBasicDescription in = get_default_in_asbd())
2016-02-23 23:16:51 +00:00
{
UInt32 size = sizeof(out);
OSStatus code;
2019-09-22 21:19:10 +00:00
#define STATUS_CHECK(x) \
code = x; \
if (code) { \
log_to_dstr(log, ca, "%s: %s\n", #x, \
osstatus_to_dstr(code)->array); \
return nullptr; \
2016-02-23 23:16:51 +00:00
}
2019-09-22 21:19:10 +00:00
STATUS_CHECK(AudioFormatGetProperty(kAudioFormatProperty_FormatInfo, 0,
NULL, &size, &out));
2016-02-23 23:16:51 +00:00
AudioConverterRef converter;
STATUS_CHECK(AudioConverterNew(&in, &out, &converter));
return cf_ptr<AudioConverterRef>{converter};
#undef STATUS_CHECK
}
static bool find_best_match(DStr &log, ca_encoder *ca, UInt32 bitrate,
2019-09-22 21:19:10 +00:00
UInt32 &best_match)
2016-02-23 23:16:51 +00:00
{
UInt32 actual_bitrate = bitrate * 1000;
bool found_match = false;
2019-09-22 21:19:10 +00:00
auto handle_bitrate = [&](UInt32 candidate) {
2016-02-23 23:16:51 +00:00
if (abs(static_cast<intmax_t>(actual_bitrate - candidate)) <
abs(static_cast<intmax_t>(actual_bitrate - best_match))) {
log_to_dstr(log, ca, "Found new best match %u\n",
2019-09-22 21:19:10 +00:00
static_cast<uint32_t>(candidate));
2016-02-23 23:16:51 +00:00
found_match = true;
best_match = candidate;
}
};
2019-09-22 21:19:10 +00:00
auto helper = [&](UInt32 min_, UInt32 max_) {
2016-02-23 23:16:51 +00:00
handle_bitrate(min_);
if (min_ == max_)
return;
log_to_dstr(log, ca, "Got actual bit rate range: %u<->%u\n",
2019-09-22 21:19:10 +00:00
static_cast<uint32_t>(min_),
static_cast<uint32_t>(max_));
2016-02-23 23:16:51 +00:00
handle_bitrate(max_);
};
for (UInt32 format_id : aac_formats) {
log_to_dstr(log, ca, "Trying %s (0x%x)\n",
2019-09-22 21:19:10 +00:00
format_id_to_str(format_id), format_id);
2016-02-23 23:16:51 +00:00
2018-02-19 19:54:37 +00:00
auto out = get_default_out_asbd_builder(2)
2019-09-22 21:19:10 +00:00
.format_id(format_id)
.asbd;
2016-02-23 23:16:51 +00:00
auto converter = get_converter(log, ca, out);
if (converter)
2019-09-22 21:19:10 +00:00
enumerate_bitrates(log, ca, converter.get(), helper);
2016-02-23 23:16:51 +00:00
else
log_to_dstr(log, ca, "Could not get converter\n");
}
best_match /= 1000;
return found_match;
}
static UInt32 find_matching_bitrate(UInt32 bitrate)
{
static UInt32 match = bitrate;
static once_flag once;
2019-09-22 21:19:10 +00:00
call_once(once, [&]() {
2016-02-23 23:16:51 +00:00
DStr log;
ca_encoder *ca = nullptr;
if (!find_best_match(log, ca, bitrate, match)) {
2019-09-22 21:19:10 +00:00
CA_CO_DLOG(LOG_ERROR,
"No matching bitrates found for "
"target bitrate %u",
static_cast<uint32_t>(bitrate));
2016-02-23 23:16:51 +00:00
match = bitrate;
return;
}
if (match != bitrate) {
2019-09-22 21:19:10 +00:00
CA_CO_DLOG(LOG_INFO,
"Default bitrate (%u) isn't "
"supported, returning %u as closest match",
static_cast<uint32_t>(bitrate),
static_cast<uint32_t>(match));
2016-02-23 23:16:51 +00:00
return;
}
if (log->len)
2019-09-22 21:19:10 +00:00
CA_CO_DLOG(LOG_DEBUG,
"Default bitrate matching log "
"for bitrate %u",
static_cast<uint32_t>(bitrate));
2016-02-23 23:16:51 +00:00
});
return match;
}
static void aac_defaults(obs_data_t *settings)
{
obs_data_set_default_int(settings, "samplerate", 0); //match input
obs_data_set_default_int(settings, "bitrate",
2019-09-22 21:19:10 +00:00
find_matching_bitrate(128));
2016-02-23 23:16:51 +00:00
obs_data_set_default_bool(settings, "allow he-aac", true);
}
2019-09-22 21:19:10 +00:00
template<typename Func>
static bool
query_property_raw(DStr &log, ca_encoder *ca, AudioFormatPropertyID property,
const char *get_property_info, const char *get_property,
AudioStreamBasicDescription &desc, Func &&func)
2016-02-23 23:16:51 +00:00
{
UInt32 size = 0;
2019-09-22 21:19:10 +00:00
OSStatus code = AudioFormatGetPropertyInfo(
property, sizeof(AudioStreamBasicDescription), &desc, &size);
2016-02-23 23:16:51 +00:00
if (code) {
log_to_dstr(log, ca, "%s: %s\n", get_property_info,
2019-09-22 21:19:10 +00:00
osstatus_to_dstr(code)->array);
2016-02-23 23:16:51 +00:00
return false;
}
if (!size) {
log_to_dstr(log, ca, "%s returned 0 size\n", get_property_info);
return false;
}
vector<uint8_t> buffer;
2019-09-22 21:19:10 +00:00
2016-02-23 23:16:51 +00:00
try {
buffer.resize(size);
} catch (...) {
log_to_dstr(log, ca, "Failed to allocate %u bytes for %s\n",
2019-09-22 21:19:10 +00:00
static_cast<uint32_t>(size), get_property);
2016-02-23 23:16:51 +00:00
return false;
}
code = AudioFormatGetProperty(property,
2019-09-22 21:19:10 +00:00
sizeof(AudioStreamBasicDescription),
&desc, &size, buffer.data());
2016-02-23 23:16:51 +00:00
if (code) {
log_to_dstr(log, ca, "%s: %s\n", get_property,
2019-09-22 21:19:10 +00:00
osstatus_to_dstr(code)->array);
2016-02-23 23:16:51 +00:00
return false;
}
2019-09-22 21:19:10 +00:00
func(size, static_cast<void *>(buffer.data()));
2016-02-23 23:16:51 +00:00
return true;
}
2019-09-22 21:19:10 +00:00
#define EXPAND_PROPERTY_NAMES(x) \
x, "AudioFormatGetPropertyInfo(" #x ")", \
"AudioFormatGetProperty(" #x ")"
2016-02-23 23:16:51 +00:00
2019-09-22 21:19:10 +00:00
template<typename Func>
2016-02-23 23:16:51 +00:00
static bool enumerate_samplerates(DStr &log, ca_encoder *ca,
2019-09-22 21:19:10 +00:00
AudioStreamBasicDescription &desc,
Func &&func)
2016-02-23 23:16:51 +00:00
{
2019-09-22 21:19:10 +00:00
auto helper = [&](UInt32 size, void *data) {
auto range = static_cast<AudioValueRange *>(data);
2016-02-23 23:16:51 +00:00
size_t num_ranges = size / sizeof(AudioValueRange);
for (size_t i = 0; i < num_ranges; i++)
func(range[i]);
};
2019-09-22 21:19:10 +00:00
return query_property_raw(
log, ca,
EXPAND_PROPERTY_NAMES(
2016-02-23 23:16:51 +00:00
kAudioFormatProperty_AvailableEncodeSampleRates),
2019-09-22 21:19:10 +00:00
desc, helper);
2016-02-23 23:16:51 +00:00
}
#if 0
// Unused because it returns bitrates that aren't actually usable, i.e.
// Available bitrates vs Applicable bitrates
template <typename Func>
static bool enumerate_bitrates(DStr &log, ca_encoder *ca,
AudioStreamBasicDescription &desc, Func &&func)
{
auto helper = [&](UInt32 size, void *data)
{
auto range = static_cast<AudioValueRange*>(data);
size_t num_ranges = size / sizeof(AudioValueRange);
for (size_t i = 0; i < num_ranges; i++)
func(range[i]);
};
return query_property_raw(log, ca, EXPAND_PROPERTY_NAMES(
kAudioFormatProperty_AvailableEncodeBitRates),
desc, helper);
}
#endif
static vector<UInt32> get_samplerates(DStr &log, ca_encoder *ca)
{
vector<UInt32> samplerates;
2019-09-22 21:19:10 +00:00
auto handle_samplerate = [&](UInt32 rate) {
2016-02-23 23:16:51 +00:00
if (find(begin(samplerates), end(samplerates), rate) ==
2019-09-22 21:19:10 +00:00
end(samplerates)) {
2016-02-23 23:16:51 +00:00
log_to_dstr(log, ca, "Adding sample rate %u\n",
2019-09-22 21:19:10 +00:00
static_cast<uint32_t>(rate));
2016-02-23 23:16:51 +00:00
samplerates.push_back(rate);
} else {
log_to_dstr(log, ca, "Sample rate %u already added\n",
2019-09-22 21:19:10 +00:00
static_cast<uint32_t>(rate));
2016-02-23 23:16:51 +00:00
}
};
2019-09-22 21:19:10 +00:00
auto helper = [&](const AudioValueRange &range) {
2016-02-23 23:16:51 +00:00
auto min_ = static_cast<UInt32>(range.mMinimum);
auto max_ = static_cast<UInt32>(range.mMaximum);
handle_samplerate(min_);
if (min_ == max_)
return;
log_to_dstr(log, ca, "Got actual sample rate range: %u<->%u\n",
2019-09-22 21:19:10 +00:00
static_cast<uint32_t>(min_),
static_cast<uint32_t>(max_));
2016-02-23 23:16:51 +00:00
handle_samplerate(max_);
};
for (UInt32 format : (ca ? *ca->allowed_formats : aac_formats)) {
log_to_dstr(log, ca, "Trying %s (0x%x)\n",
2019-09-22 21:19:10 +00:00
format_id_to_str(format),
static_cast<uint32_t>(format));
2016-02-23 23:16:51 +00:00
2019-09-22 21:19:10 +00:00
auto asbd = asbd_builder().format_id(format).asbd;
2016-02-23 23:16:51 +00:00
enumerate_samplerates(log, ca, asbd, helper);
}
return samplerates;
}
static void add_samplerates(obs_property_t *prop, ca_encoder *ca)
{
2019-09-22 21:19:10 +00:00
obs_property_list_add_int(prop, obs_module_text("UseInputSampleRate"),
0);
2016-02-23 23:16:51 +00:00
DStr log;
auto samplerates = get_samplerates(log, ca);
if (!samplerates.size()) {
CA_CO_DLOG_(LOG_ERROR, "Couldn't find available sample rates");
return;
}
if (log->len)
CA_CO_DLOG_(LOG_DEBUG, "Sample rate enumeration log");
sort(begin(samplerates), end(samplerates));
DStr buffer;
for (UInt32 samplerate : samplerates) {
dstr_printf(buffer, "%d", static_cast<uint32_t>(samplerate));
obs_property_list_add_int(prop, buffer->array, samplerate);
}
}
#define NBSP "\xC2\xA0"
static vector<UInt32> get_bitrates(DStr &log, ca_encoder *ca,
2019-09-22 21:19:10 +00:00
Float64 samplerate)
2016-02-23 23:16:51 +00:00
{
vector<UInt32> bitrates;
2018-02-19 19:54:37 +00:00
struct obs_audio_info aoi;
int channels;
obs_get_audio_info(&aoi);
channels = get_audio_channels(aoi.speakers);
2016-02-23 23:16:51 +00:00
2019-09-22 21:19:10 +00:00
auto handle_bitrate = [&](UInt32 bitrate) {
2016-02-23 23:16:51 +00:00
if (find(begin(bitrates), end(bitrates), bitrate) ==
2019-09-22 21:19:10 +00:00
end(bitrates)) {
2016-02-23 23:16:51 +00:00
log_to_dstr(log, ca, "Adding bitrate %u\n",
2019-09-22 21:19:10 +00:00
static_cast<uint32_t>(bitrate));
2016-02-23 23:16:51 +00:00
bitrates.push_back(bitrate);
} else {
log_to_dstr(log, ca, "Bitrate %u already added\n",
2019-09-22 21:19:10 +00:00
static_cast<uint32_t>(bitrate));
2016-02-23 23:16:51 +00:00
}
};
2019-09-22 21:19:10 +00:00
auto helper = [&](UInt32 min_, UInt32 max_) {
2016-02-23 23:16:51 +00:00
handle_bitrate(min_);
if (min_ == max_)
return;
log_to_dstr(log, ca, "Got actual bitrate range: %u<->%u\n",
2019-09-22 21:19:10 +00:00
static_cast<uint32_t>(min_),
static_cast<uint32_t>(max_));
2016-02-23 23:16:51 +00:00
handle_bitrate(max_);
};
for (UInt32 format_id : (ca ? *ca->allowed_formats : aac_formats)) {
log_to_dstr(log, ca, "Trying %s (0x%x) at %g" NBSP "hz\n",
2019-09-22 21:19:10 +00:00
format_id_to_str(format_id),
static_cast<uint32_t>(format_id), samplerate);
2016-02-23 23:16:51 +00:00
2018-02-19 19:54:37 +00:00
auto out = get_default_out_asbd_builder(channels)
2019-09-22 21:19:10 +00:00
.format_id(format_id)
.sample_rate(samplerate)
.asbd;
2016-02-23 23:16:51 +00:00
auto converter = get_converter(log, ca, out);
if (converter)
enumerate_bitrates(log, ca, converter.get(), helper);
}
return bitrates;
}
static void add_bitrates(obs_property_t *prop, ca_encoder *ca,
2019-09-22 21:19:10 +00:00
Float64 samplerate = 44100.,
UInt32 *selected = nullptr)
2016-02-23 23:16:51 +00:00
{
obs_property_list_clear(prop);
DStr log;
auto bitrates = get_bitrates(log, ca, samplerate);
if (!bitrates.size()) {
CA_CO_DLOG_(LOG_ERROR, "Couldn't find available bitrates");
return;
}
if (log->len)
CA_CO_DLOG_(LOG_DEBUG, "Bitrate enumeration log");
bool selected_in_range = true;
if (selected) {
selected_in_range = find(begin(bitrates), end(bitrates),
2019-09-22 21:19:10 +00:00
*selected * 1000) != end(bitrates);
2016-02-23 23:16:51 +00:00
if (!selected_in_range)
bitrates.push_back(*selected * 1000);
}
sort(begin(bitrates), end(bitrates));
DStr buffer;
for (UInt32 bitrate : bitrates) {
dstr_printf(buffer, "%u", (uint32_t)bitrate / 1000);
size_t idx = obs_property_list_add_int(prop, buffer->array,
2019-09-22 21:19:10 +00:00
bitrate / 1000);
2016-02-23 23:16:51 +00:00
if (selected_in_range || bitrate / 1000 != *selected)
continue;
obs_property_list_item_disable(prop, idx, true);
}
}
static bool samplerate_updated(obs_properties_t *props, obs_property_t *prop,
2019-09-22 21:19:10 +00:00
obs_data_t *settings)
2016-02-23 23:16:51 +00:00
{
auto samplerate =
static_cast<UInt32>(obs_data_get_int(settings, "samplerate"));
if (!samplerate)
samplerate = 44100;
prop = obs_properties_get(props, "bitrate");
if (prop) {
auto bitrate = static_cast<UInt32>(
2019-09-22 21:19:10 +00:00
obs_data_get_int(settings, "bitrate"));
2016-02-23 23:16:51 +00:00
add_bitrates(prop, nullptr, samplerate, &bitrate);
return true;
}
return false;
}
static obs_properties_t *aac_properties(void *data)
{
2019-09-22 21:19:10 +00:00
ca_encoder *ca = static_cast<ca_encoder *>(data);
2016-02-23 23:16:51 +00:00
obs_properties_t *props = obs_properties_create();
2019-09-22 21:19:10 +00:00
obs_property_t *p = obs_properties_add_list(
props, "samplerate", obs_module_text("OutputSamplerate"),
OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
2016-02-23 23:16:51 +00:00
add_samplerates(p, ca);
obs_property_set_modified_callback(p, samplerate_updated);
p = obs_properties_add_list(props, "bitrate",
2019-09-22 21:19:10 +00:00
obs_module_text("Bitrate"),
OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
2016-02-23 23:16:51 +00:00
add_bitrates(p, ca);
obs_properties_add_bool(props, "allow he-aac",
2019-09-22 21:19:10 +00:00
obs_module_text("AllowHEAAC"));
2016-02-23 23:16:51 +00:00
return props;
}
OBS_DECLARE_MODULE()
OBS_MODULE_USE_DEFAULT_LOCALE("coreaudio-encoder", "en-US")
2019-07-27 12:47:10 +00:00
MODULE_EXPORT const char *obs_module_description(void)
{
return "Apple CoreAudio based encoder";
}
2016-02-23 23:16:51 +00:00
bool obs_module_load(void)
{
#ifdef _WIN32
if (!load_core_audio()) {
2016-08-28 12:07:43 +00:00
CA_LOG(LOG_WARNING, "CoreAudio AAC encoder not installed on "
2019-09-22 21:19:10 +00:00
"the system or couldn't be loaded");
2016-02-23 23:16:51 +00:00
return true;
}
CA_LOG(LOG_INFO, "Adding CoreAudio AAC encoder");
#endif
2019-09-22 21:19:10 +00:00
struct obs_encoder_info aac_info {
};
2016-02-23 23:16:51 +00:00
aac_info.id = "CoreAudio_AAC";
aac_info.type = OBS_ENCODER_AUDIO;
aac_info.codec = "AAC";
aac_info.get_name = aac_get_name;
aac_info.destroy = aac_destroy;
aac_info.create = aac_create;
aac_info.encode = aac_encode;
aac_info.get_frame_size = aac_frame_size;
aac_info.get_audio_info = aac_audio_info;
aac_info.get_extra_data = aac_extra_data;
aac_info.get_defaults = aac_defaults;
aac_info.get_properties = aac_properties;
obs_register_encoder(&aac_info);
return true;
}
#ifdef _WIN32
void obs_module_unload(void)
{
unload_core_audio();
}
#endif