mirror of
https://git.eden-emu.dev/eden-emu/eden.git
synced 2026-11-01 11:58:19 +00:00
@@ -62,48 +62,60 @@ public:
|
|||||||
// However, native opus can also work with swrescale:
|
// However, native opus can also work with swrescale:
|
||||||
// it uses planarfloat, we can resample to s16
|
// it uses planarfloat, we can resample to s16
|
||||||
AVCodec const* codec = avcodec_find_decoder_by_name("libopus");
|
AVCodec const* codec = avcodec_find_decoder_by_name("libopus");
|
||||||
bool is_libopus = codec != nullptr;
|
bool is_libopus = false;//codec != nullptr;
|
||||||
if (!codec) {
|
if (!codec) {
|
||||||
LOG_WARNING(Audio_DSP, "using ffmpeg native opus decoder");
|
LOG_WARNING(Audio_DSP, "using ffmpeg native opus decoder");
|
||||||
codec = avcodec_find_decoder(AV_CODEC_ID_OPUS);
|
codec = avcodec_find_decoder(AV_CODEC_ID_OPUS);
|
||||||
}
|
}
|
||||||
if (codec) {
|
// handles null codec
|
||||||
if ((avc = avc ? avc : avcodec_alloc_context3(codec))) {
|
avcodec_free_context(&avc);
|
||||||
if (is_libopus) {
|
if ((avc = avcodec_alloc_context3(codec))) {
|
||||||
const std::array<u8, 2> mapping_arr{0, 1};
|
if (is_libopus) {
|
||||||
mappings = mappings ? mappings : mapping_arr.data();
|
const std::array<u8, 2> mapping_arr{0, 1};
|
||||||
|
mappings = mappings ? mappings : mapping_arr.data();
|
||||||
|
|
||||||
// freed by avcodec_context_free()
|
// freed by avcodec_context_free()
|
||||||
u8 *edata = reinterpret_cast<u8*>(av_mallocz(OPUS_HEAD_SIZE + 2 * OPUS_MAX_CHANNELS + AV_INPUT_BUFFER_PADDING_SIZE));
|
u8 *edata = reinterpret_cast<u8*>(av_mallocz(OPUS_HEAD_SIZE + 2 * OPUS_MAX_CHANNELS + AV_INPUT_BUFFER_PADDING_SIZE));
|
||||||
ASSERT(edata);
|
ASSERT(edata);
|
||||||
edata[9] = u8(channel_count); //channels
|
edata[9] = u8(channel_count); //channels
|
||||||
edata[10] = u8(0); //opus->pre_skip
|
edata[10] = u8(0); //opus->pre_skip
|
||||||
edata[16] = u8(0); //gain_db
|
edata[16] = u8(0); //gain_db
|
||||||
edata[18] = u8(0); //channel_map
|
edata[18] = u8(0); //channel_map
|
||||||
edata[OPUS_HEAD_SIZE + 0] = u8(total_stream_count);
|
edata[OPUS_HEAD_SIZE + 0] = u8(total_stream_count);
|
||||||
edata[OPUS_HEAD_SIZE + 1] = u8(stereo_stream_count);
|
edata[OPUS_HEAD_SIZE + 1] = u8(stereo_stream_count);
|
||||||
if (channel_count >= 1) edata[OPUS_HEAD_SIZE + 2] = mappings[0];
|
if (channel_count >= 1) edata[OPUS_HEAD_SIZE + 2] = mappings[0];
|
||||||
if (channel_count >= 2) edata[OPUS_HEAD_SIZE + 3] = mappings[1];
|
if (channel_count >= 2) edata[OPUS_HEAD_SIZE + 3] = mappings[1];
|
||||||
avc->extradata = edata;
|
avc->extradata = edata;
|
||||||
avc->extradata_size = OPUS_HEAD_SIZE + 2 * channel_count;
|
avc->extradata_size = OPUS_HEAD_SIZE + 2 * channel_count;
|
||||||
}
|
|
||||||
|
|
||||||
// FFmpeg hardcodes sample rate
|
|
||||||
avc->sample_rate = sample_rate;
|
|
||||||
avc->request_sample_fmt = AV_SAMPLE_FMT_S16;
|
|
||||||
av_channel_layout_default(&avc->ch_layout, channel_count);
|
|
||||||
if (avcodec_open2(avc, codec, nullptr) >= 0) {
|
|
||||||
pkt = av_packet_alloc();
|
|
||||||
frame = av_frame_alloc();
|
|
||||||
return ResultSuccess;
|
|
||||||
}
|
|
||||||
avcodec_free_context(&avc);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FFmpeg hardcodes sample rate
|
||||||
|
avc->sample_rate = sample_rate;
|
||||||
|
avc->request_sample_fmt = AV_SAMPLE_FMT_S16;
|
||||||
|
av_channel_layout_default(&avc->ch_layout, channel_count);
|
||||||
|
if (avcodec_open2(avc, codec, nullptr) >= 0) {
|
||||||
|
pkt = av_packet_alloc();
|
||||||
|
frame = av_frame_alloc();
|
||||||
|
if (swr_alloc_set_opts2(
|
||||||
|
&swr,
|
||||||
|
&avc->ch_layout, AV_SAMPLE_FMT_S16, 48000,
|
||||||
|
&avc->ch_layout, (enum AVSampleFormat)frame->format, 48000,
|
||||||
|
0, nullptr) >= 0) {
|
||||||
|
if (swr_init(swr) >= 0) {
|
||||||
|
return ResultSuccess;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
swr_free(&swr);
|
||||||
|
av_frame_free(&frame);
|
||||||
|
av_packet_free(&pkt);
|
||||||
|
avcodec_free_context(&avc);
|
||||||
}
|
}
|
||||||
return Service::Audio::ResultLibOpusInternalError;
|
return Service::Audio::ResultLibOpusInternalError;
|
||||||
}
|
}
|
||||||
|
|
||||||
Result Shutdown() {
|
Result Shutdown() {
|
||||||
|
swr_free(&swr);
|
||||||
av_frame_free(&frame);
|
av_frame_free(&frame);
|
||||||
av_packet_free(&pkt);
|
av_packet_free(&pkt);
|
||||||
avcodec_free_context(&avc);
|
avcodec_free_context(&avc);
|
||||||
@@ -127,44 +139,28 @@ public:
|
|||||||
if (r == AVERROR(EAGAIN)) {
|
if (r == AVERROR(EAGAIN)) {
|
||||||
av_frame_unref(frame);
|
av_frame_unref(frame);
|
||||||
av_packet_unref(pkt);
|
av_packet_unref(pkt);
|
||||||
if ((r = av_new_packet(pkt, int(input_data_size))) >= 0) {
|
if ((r = av_new_packet(pkt, int(input_data_size))) < 0)
|
||||||
std::memcpy(pkt->data, reinterpret_cast<const u8*>(input_data), input_data_size);
|
break;
|
||||||
r = avcodec_send_packet(avc, pkt);
|
std::memcpy(pkt->data, reinterpret_cast<const u8*>(input_data), input_data_size);
|
||||||
ASSERT(r >= 0);
|
if ((r = avcodec_send_packet(avc, pkt)) < 0)
|
||||||
} else {
|
break;
|
||||||
return Service::Audio::ResultLibOpusInvalidState;
|
|
||||||
}
|
|
||||||
} else if (r == AVERROR_EOF) {
|
} else if (r == AVERROR_EOF) {
|
||||||
av_frame_unref(frame);
|
av_frame_unref(frame);
|
||||||
break;
|
break;
|
||||||
} else if (r >= 0) {
|
} else if (r >= 0) {
|
||||||
auto const input_bsize = av_samples_get_buffer_size(frame->linesize, frame->ch_layout.nb_channels, frame->nb_samples, AV_SAMPLE_FMT_S16, 1);
|
auto const input_bsize = av_samples_get_buffer_size(frame->linesize, frame->ch_layout.nb_channels, frame->nb_samples, AV_SAMPLE_FMT_S16, 1);
|
||||||
if (frame->format == AV_SAMPLE_FMT_S16) {
|
if (frame->format == AV_SAMPLE_FMT_S16) {
|
||||||
|
// input_bsize == output_bsize
|
||||||
ASSERT(size_t(int(output_data_size) - rem_output_bytes + input_bsize) <= size_t(output_data_size));
|
ASSERT(size_t(int(output_data_size) - rem_output_bytes + input_bsize) <= size_t(output_data_size));
|
||||||
std::memcpy(reinterpret_cast<s16*>(output_data) + (int(output_data_size) - rem_output_bytes), frame->data[0], size_t(input_bsize));
|
std::memcpy(reinterpret_cast<s16*>(output_data + (int(output_data_size) - rem_output_bytes)), frame->data[0], size_t(input_bsize));
|
||||||
} else {
|
} else {
|
||||||
SwrContext *swr = nullptr;
|
int out_samples = int(av_rescale_rnd(swr_get_delay(swr, frame->sample_rate) + frame->nb_samples, frame->sample_rate, frame->sample_rate, AV_ROUND_UP));
|
||||||
if (swr_alloc_set_opts2(
|
uint8_t *output = nullptr;
|
||||||
&swr,
|
av_samples_alloc(&output, frame->linesize, frame->ch_layout.nb_channels, out_samples, AV_SAMPLE_FMT_S16, 0);
|
||||||
&avc->ch_layout,
|
out_samples = swr_convert(swr, &output, out_samples, (const uint8_t **)frame->data, frame->nb_samples);
|
||||||
AV_SAMPLE_FMT_S16,
|
auto const output_bsize = av_samples_get_buffer_size(frame->linesize, frame->ch_layout.nb_channels, out_samples, AV_SAMPLE_FMT_S16, 1);
|
||||||
48000,
|
std::memcpy(reinterpret_cast<s16*>(output_data + (int(output_data_size) - rem_output_bytes)), output, size_t(output_bsize));
|
||||||
&avc->ch_layout,
|
av_freep(&output);
|
||||||
(enum AVSampleFormat)frame->format,
|
|
||||||
48000,
|
|
||||||
0,
|
|
||||||
nullptr
|
|
||||||
) >= 0) {
|
|
||||||
if (swr_init(swr) >= 0) {
|
|
||||||
int out_samples = int(av_rescale_rnd(swr_get_delay(swr, frame->sample_rate) + frame->nb_samples, frame->sample_rate, frame->sample_rate, AV_ROUND_UP));
|
|
||||||
uint8_t *output = nullptr;
|
|
||||||
av_samples_alloc(&output, frame->linesize, frame->ch_layout.nb_channels, out_samples, AV_SAMPLE_FMT_S16, 0);
|
|
||||||
out_samples = swr_convert(swr, &output, out_samples, (const uint8_t **)frame->data, frame->nb_samples);
|
|
||||||
std::memcpy(reinterpret_cast<s16*>(output_data) + (int(output_data_size) - rem_output_bytes), output, size_t(input_bsize));
|
|
||||||
av_freep(&output);
|
|
||||||
}
|
|
||||||
swr_free(&swr);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
out_sample_count += frame->nb_samples;
|
out_sample_count += frame->nb_samples;
|
||||||
rem_output_bytes -= input_bsize;
|
rem_output_bytes -= input_bsize;
|
||||||
@@ -183,6 +179,7 @@ public:
|
|||||||
AVCodecContext* avc = nullptr;
|
AVCodecContext* avc = nullptr;
|
||||||
AVPacket* pkt = nullptr;
|
AVPacket* pkt = nullptr;
|
||||||
AVFrame* frame = nullptr;
|
AVFrame* frame = nullptr;
|
||||||
|
SwrContext* swr = nullptr;
|
||||||
};
|
};
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user