chore: checkpoint web Blender migration source, tools and docs
Accumulated uncommitted work: GHOST Web backend, WebGL/GPU web layer, web workflow engine and protocol, tooling, docs and task records. Large generated evidence, user-testing archives, build snapshots and imported test assets remain untracked. Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
@@ -52,7 +52,7 @@ void FFMPEGWriter::encode()
|
||||
int single_size = sizeof(sample_t);
|
||||
for(int channel = 0; channel < m_specs.channels; channel++)
|
||||
{
|
||||
for(int i = 0; i < m_input_buffer.getSize() / AUD_SAMPLE_SIZE(m_specs); i++)
|
||||
for(int i = 0; i < m_input_samples; i++)
|
||||
{
|
||||
std::memcpy(((data_t*)dbuf) + (m_input_samples * channel + i) * single_size,
|
||||
((data_t*)data) + ((m_specs.channels * i) + channel) * single_size, single_size);
|
||||
@@ -60,14 +60,14 @@ void FFMPEGWriter::encode()
|
||||
}
|
||||
|
||||
// convert first
|
||||
if(m_input_size)
|
||||
if(m_convert != convert_copy<sample_t>)
|
||||
m_convert(reinterpret_cast<data_t*>(data), reinterpret_cast<data_t*>(dbuf), m_input_samples * m_specs.channels);
|
||||
else
|
||||
std::memcpy(data, dbuf, m_input_buffer.getSize());
|
||||
std::memcpy(data, dbuf, m_input_samples * AUD_SAMPLE_SIZE(m_specs));
|
||||
}
|
||||
else
|
||||
// convert first
|
||||
if(m_input_size)
|
||||
if(m_convert != convert_copy<sample_t>)
|
||||
m_convert(reinterpret_cast<data_t*>(data), reinterpret_cast<data_t*>(data), m_input_samples * m_specs.channels);
|
||||
|
||||
#ifdef FFMPEG_OLD_CODE
|
||||
@@ -82,6 +82,7 @@ void FFMPEGWriter::encode()
|
||||
|
||||
m_frame->nb_samples = m_input_samples;
|
||||
m_frame->format = m_codecCtx->sample_fmt;
|
||||
m_frame->sample_rate = m_codecCtx->sample_rate;
|
||||
#ifdef FFMPEG_OLD_CH_LAYOUT
|
||||
m_frame->channel_layout = m_codecCtx->channel_layout;
|
||||
m_frame->channels = m_specs.channels;
|
||||
@@ -90,11 +91,11 @@ void FFMPEGWriter::encode()
|
||||
AUD_THROW(FileException, "File couldn't be written, couldn't copy audio channel layout.");
|
||||
#endif
|
||||
|
||||
if(avcodec_fill_audio_frame(m_frame, m_specs.channels, m_codecCtx->sample_fmt, reinterpret_cast<data_t*>(data), m_input_buffer.getSize(), 0) < 0)
|
||||
if(avcodec_fill_audio_frame(m_frame, m_specs.channels, m_codecCtx->sample_fmt, reinterpret_cast<data_t*>(data), m_input_buffer.getSize(), 1) < 0)
|
||||
AUD_THROW(FileException, "File couldn't be written, filling the audio frame failed with ffmpeg.");
|
||||
|
||||
AVRational sample_time = { 1, static_cast<int>(m_specs.rate) };
|
||||
m_frame->pts = av_rescale_q(m_position - m_input_samples, m_codecCtx->time_base, sample_time);
|
||||
m_frame->pts = av_rescale_q(m_position - m_input_samples, sample_time, m_codecCtx->time_base);
|
||||
|
||||
#ifdef FFMPEG_OLD_CODE
|
||||
if(avcodec_encode_audio2(m_codecCtx, m_packet, m_frame, &got_packet))
|
||||
@@ -117,13 +118,18 @@ void FFMPEGWriter::encode()
|
||||
if(avcodec_send_frame(m_codecCtx, m_frame) < 0)
|
||||
AUD_THROW(FileException, "File couldn't be written, audio encoding failed with ffmpeg.");
|
||||
|
||||
while(avcodec_receive_packet(m_codecCtx, m_packet) == 0)
|
||||
int result;
|
||||
while((result = avcodec_receive_packet(m_codecCtx, m_packet)) == 0)
|
||||
{
|
||||
m_packet->stream_index = m_stream->index;
|
||||
av_packet_rescale_ts(m_packet, m_codecCtx->time_base, m_stream->time_base);
|
||||
if(av_write_frame(m_formatCtx, m_packet) < 0)
|
||||
AUD_THROW(FileException, "Frame couldn't be writen to the file with ffmpeg.");
|
||||
const int written = av_write_frame(m_formatCtx, m_packet);
|
||||
av_packet_unref(m_packet);
|
||||
if(written < 0)
|
||||
AUD_THROW(FileException, "Frame couldn't be written to the file with ffmpeg.");
|
||||
}
|
||||
if(result != AVERROR(EAGAIN) && result != AVERROR_EOF)
|
||||
AUD_THROW(FileException, "FFmpeg failed to receive an encoded audio packet.");
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -158,13 +164,18 @@ void FFMPEGWriter::close()
|
||||
if(avcodec_send_frame(m_codecCtx, nullptr) < 0)
|
||||
AUD_THROW(FileException, "File couldn't be written, audio encoding failed with ffmpeg.");
|
||||
|
||||
while(avcodec_receive_packet(m_codecCtx, m_packet) == 0)
|
||||
int result;
|
||||
while((result = avcodec_receive_packet(m_codecCtx, m_packet)) == 0)
|
||||
{
|
||||
m_packet->stream_index = m_stream->index;
|
||||
av_packet_rescale_ts(m_packet, m_codecCtx->time_base, m_stream->time_base);
|
||||
if(av_write_frame(m_formatCtx, m_packet) < 0)
|
||||
AUD_THROW(FileException, "Frame couldn't be writen to the file with ffmpeg.");
|
||||
const int written = av_write_frame(m_formatCtx, m_packet);
|
||||
av_packet_unref(m_packet);
|
||||
if(written < 0)
|
||||
AUD_THROW(FileException, "Frame couldn't be written to the file with ffmpeg.");
|
||||
}
|
||||
if(result != AVERROR(EAGAIN) && result != AVERROR_EOF)
|
||||
AUD_THROW(FileException, "FFmpeg failed to receive an encoded audio packet.");
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -177,110 +188,119 @@ FFMPEGWriter::FFMPEGWriter(const std::string &filename, DeviceSpecs specs, Conta
|
||||
m_packet(nullptr),
|
||||
m_frame(nullptr),
|
||||
m_input_samples(0),
|
||||
m_deinterleave(false)
|
||||
m_deinterleave(false),
|
||||
m_finished(false),
|
||||
m_failed(false)
|
||||
{
|
||||
static const char* formats[] = { nullptr, "ac3", "flac", "matroska", "mp2", "mp3", "ogg", "wav", "adts" };
|
||||
|
||||
if(avformat_alloc_output_context2(&m_formatCtx, nullptr, formats[format], filename.c_str()) < 0)
|
||||
AUD_THROW(FileException, "File couldn't be written, format couldn't be found with ffmpeg.");
|
||||
if(format <= CONTAINER_INVALID || format > CONTAINER_AAC)
|
||||
AUD_THROW(FileException, "Invalid audio container.");
|
||||
|
||||
const AVOutputFormat* outputFmt = m_formatCtx->oformat;
|
||||
if((format == CONTAINER_AAC && codec != CODEC_AAC) ||
|
||||
(format == CONTAINER_AC3 && codec != CODEC_AC3) ||
|
||||
(format == CONTAINER_MP2 && codec != CODEC_MP2))
|
||||
AUD_THROW(FileException, "The audio codec does not match the selected container.");
|
||||
|
||||
if(!outputFmt) {
|
||||
avformat_free_context(m_formatCtx);
|
||||
AUD_THROW(FileException, "File couldn't be written, output format couldn't be found with ffmpeg.");
|
||||
}
|
||||
|
||||
AVCodecID audio_codec = AV_CODEC_ID_NONE;
|
||||
|
||||
switch(codec)
|
||||
try
|
||||
{
|
||||
case CODEC_AAC:
|
||||
audio_codec = AV_CODEC_ID_AAC;
|
||||
break;
|
||||
case CODEC_AC3:
|
||||
audio_codec = AV_CODEC_ID_AC3;
|
||||
break;
|
||||
case CODEC_FLAC:
|
||||
audio_codec = AV_CODEC_ID_FLAC;
|
||||
break;
|
||||
case CODEC_MP2:
|
||||
audio_codec = AV_CODEC_ID_MP2;
|
||||
break;
|
||||
case CODEC_MP3:
|
||||
audio_codec = AV_CODEC_ID_MP3;
|
||||
break;
|
||||
case CODEC_OPUS:
|
||||
audio_codec = AV_CODEC_ID_OPUS;
|
||||
break;
|
||||
case CODEC_PCM:
|
||||
switch(specs.format)
|
||||
if(avformat_alloc_output_context2(&m_formatCtx, nullptr, formats[format], filename.c_str()) < 0)
|
||||
AUD_THROW(FileException, "File couldn't be written, format couldn't be found with ffmpeg.");
|
||||
|
||||
const AVOutputFormat* outputFmt = m_formatCtx->oformat;
|
||||
|
||||
if(!outputFmt) {
|
||||
AUD_THROW(FileException, "File couldn't be written, output format couldn't be found with ffmpeg.");
|
||||
}
|
||||
|
||||
AVCodecID audio_codec = AV_CODEC_ID_NONE;
|
||||
|
||||
switch(codec)
|
||||
{
|
||||
case FORMAT_U8:
|
||||
audio_codec = AV_CODEC_ID_PCM_U8;
|
||||
case CODEC_AAC:
|
||||
audio_codec = AV_CODEC_ID_AAC;
|
||||
break;
|
||||
case FORMAT_S16:
|
||||
audio_codec = AV_CODEC_ID_PCM_S16LE;
|
||||
case CODEC_AC3:
|
||||
audio_codec = AV_CODEC_ID_AC3;
|
||||
break;
|
||||
case FORMAT_S24:
|
||||
audio_codec = AV_CODEC_ID_PCM_S24LE;
|
||||
case CODEC_FLAC:
|
||||
audio_codec = AV_CODEC_ID_FLAC;
|
||||
break;
|
||||
case FORMAT_S32:
|
||||
audio_codec = AV_CODEC_ID_PCM_S32LE;
|
||||
case CODEC_MP2:
|
||||
audio_codec = AV_CODEC_ID_MP2;
|
||||
break;
|
||||
case FORMAT_FLOAT32:
|
||||
audio_codec = AV_CODEC_ID_PCM_F32LE;
|
||||
case CODEC_MP3:
|
||||
audio_codec = AV_CODEC_ID_MP3;
|
||||
break;
|
||||
case FORMAT_FLOAT64:
|
||||
audio_codec = AV_CODEC_ID_PCM_F64LE;
|
||||
case CODEC_OPUS:
|
||||
audio_codec = AV_CODEC_ID_OPUS;
|
||||
break;
|
||||
case CODEC_PCM:
|
||||
switch(specs.format)
|
||||
{
|
||||
case FORMAT_U8:
|
||||
audio_codec = AV_CODEC_ID_PCM_U8;
|
||||
break;
|
||||
case FORMAT_S16:
|
||||
audio_codec = AV_CODEC_ID_PCM_S16LE;
|
||||
break;
|
||||
case FORMAT_S24:
|
||||
audio_codec = AV_CODEC_ID_PCM_S24LE;
|
||||
break;
|
||||
case FORMAT_S32:
|
||||
audio_codec = AV_CODEC_ID_PCM_S32LE;
|
||||
break;
|
||||
case FORMAT_FLOAT32:
|
||||
audio_codec = AV_CODEC_ID_PCM_F32LE;
|
||||
break;
|
||||
case FORMAT_FLOAT64:
|
||||
audio_codec = AV_CODEC_ID_PCM_F64LE;
|
||||
break;
|
||||
default:
|
||||
audio_codec = AV_CODEC_ID_NONE;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case CODEC_VORBIS:
|
||||
audio_codec = AV_CODEC_ID_VORBIS;
|
||||
break;
|
||||
default:
|
||||
audio_codec = AV_CODEC_ID_NONE;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case CODEC_VORBIS:
|
||||
audio_codec = AV_CODEC_ID_VORBIS;
|
||||
break;
|
||||
default:
|
||||
audio_codec = AV_CODEC_ID_NONE;
|
||||
break;
|
||||
}
|
||||
|
||||
uint64_t channel_layout = 0;
|
||||
uint64_t channel_layout = 0;
|
||||
|
||||
switch(m_specs.channels)
|
||||
{
|
||||
case CHANNELS_MONO:
|
||||
channel_layout = AV_CH_LAYOUT_MONO;
|
||||
break;
|
||||
case CHANNELS_STEREO:
|
||||
channel_layout = AV_CH_LAYOUT_STEREO;
|
||||
break;
|
||||
case CHANNELS_STEREO_LFE:
|
||||
channel_layout = AV_CH_LAYOUT_2POINT1;
|
||||
break;
|
||||
case CHANNELS_SURROUND4:
|
||||
channel_layout = AV_CH_LAYOUT_QUAD;
|
||||
break;
|
||||
case CHANNELS_SURROUND5:
|
||||
channel_layout = AV_CH_LAYOUT_5POINT0_BACK;
|
||||
break;
|
||||
case CHANNELS_SURROUND51:
|
||||
channel_layout = AV_CH_LAYOUT_5POINT1_BACK;
|
||||
break;
|
||||
case CHANNELS_SURROUND61:
|
||||
channel_layout = AV_CH_LAYOUT_6POINT1_BACK;
|
||||
break;
|
||||
case CHANNELS_SURROUND71:
|
||||
channel_layout = AV_CH_LAYOUT_7POINT1;
|
||||
break;
|
||||
default:
|
||||
AUD_THROW(FileException, "File couldn't be written, channel layout not supported.");
|
||||
}
|
||||
switch(m_specs.channels)
|
||||
{
|
||||
case CHANNELS_MONO:
|
||||
channel_layout = AV_CH_LAYOUT_MONO;
|
||||
break;
|
||||
case CHANNELS_STEREO:
|
||||
channel_layout = AV_CH_LAYOUT_STEREO;
|
||||
break;
|
||||
case CHANNELS_STEREO_LFE:
|
||||
channel_layout = AV_CH_LAYOUT_2POINT1;
|
||||
break;
|
||||
case CHANNELS_SURROUND4:
|
||||
channel_layout = AV_CH_LAYOUT_QUAD;
|
||||
break;
|
||||
case CHANNELS_SURROUND5:
|
||||
channel_layout = AV_CH_LAYOUT_5POINT0_BACK;
|
||||
break;
|
||||
case CHANNELS_SURROUND51:
|
||||
channel_layout = AV_CH_LAYOUT_5POINT1_BACK;
|
||||
break;
|
||||
case CHANNELS_SURROUND61:
|
||||
channel_layout = AV_CH_LAYOUT_6POINT1_BACK;
|
||||
break;
|
||||
case CHANNELS_SURROUND71:
|
||||
channel_layout = AV_CH_LAYOUT_7POINT1;
|
||||
break;
|
||||
default:
|
||||
AUD_THROW(FileException, "File couldn't be written, channel layout not supported.");
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
if(audio_codec == AV_CODEC_ID_NONE)
|
||||
AUD_THROW(FileException, "File couldn't be written, audio codec not found with ffmpeg.");
|
||||
|
||||
@@ -330,14 +350,30 @@ FFMPEGWriter::FFMPEGWriter(const std::string &filename, DeviceSpecs specs, Conta
|
||||
if(m_formatCtx->oformat->flags & AVFMT_GLOBALHEADER)
|
||||
m_codecCtx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
|
||||
|
||||
bool format_supported = false;
|
||||
const AVSampleFormat* sample_formats = nullptr;
|
||||
const int* sample_rates = nullptr;
|
||||
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(61, 19, 100)
|
||||
const void* configurations = nullptr;
|
||||
if(avcodec_get_supported_config(m_codecCtx, codec, AV_CODEC_CONFIG_SAMPLE_FORMAT,
|
||||
0, &configurations, nullptr) < 0)
|
||||
AUD_THROW(FileException, "Couldn't query FFmpeg sample formats.");
|
||||
sample_formats = static_cast<const AVSampleFormat*>(configurations);
|
||||
if(avcodec_get_supported_config(m_codecCtx, codec, AV_CODEC_CONFIG_SAMPLE_RATE,
|
||||
0, &configurations, nullptr) < 0)
|
||||
AUD_THROW(FileException, "Couldn't query FFmpeg sample rates.");
|
||||
sample_rates = static_cast<const int*>(configurations);
|
||||
#else
|
||||
sample_formats = codec->sample_fmts;
|
||||
sample_rates = codec->supported_samplerates;
|
||||
#endif
|
||||
bool format_supported = sample_formats == nullptr;
|
||||
|
||||
for(int i = 0; codec->sample_fmts[i] != -1; i++)
|
||||
for(int i = 0; sample_formats && sample_formats[i] != -1; i++)
|
||||
{
|
||||
if(av_get_alt_sample_fmt(codec->sample_fmts[i], false) == m_codecCtx->sample_fmt)
|
||||
if(av_get_alt_sample_fmt(sample_formats[i], false) == m_codecCtx->sample_fmt)
|
||||
{
|
||||
m_deinterleave = av_sample_fmt_is_planar(codec->sample_fmts[i]);
|
||||
m_codecCtx->sample_fmt = codec->sample_fmts[i];
|
||||
m_deinterleave = av_sample_fmt_is_planar(sample_formats[i]);
|
||||
m_codecCtx->sample_fmt = sample_formats[i];
|
||||
format_supported = true;
|
||||
}
|
||||
}
|
||||
@@ -345,10 +381,10 @@ FFMPEGWriter::FFMPEGWriter(const std::string &filename, DeviceSpecs specs, Conta
|
||||
if(!format_supported)
|
||||
{
|
||||
int chosen_index = 0;
|
||||
auto chosen = av_get_alt_sample_fmt(codec->sample_fmts[chosen_index], false);
|
||||
for(int i = 1; codec->sample_fmts[i] != -1; i++)
|
||||
auto chosen = av_get_alt_sample_fmt(sample_formats[chosen_index], false);
|
||||
for(int i = 1; sample_formats[i] != -1; i++)
|
||||
{
|
||||
auto fmt = av_get_alt_sample_fmt(codec->sample_fmts[i], false);
|
||||
auto fmt = av_get_alt_sample_fmt(sample_formats[i], false);
|
||||
if((fmt > chosen && chosen < m_codecCtx->sample_fmt) || (fmt > m_codecCtx->sample_fmt && fmt < chosen))
|
||||
{
|
||||
chosen = fmt;
|
||||
@@ -356,7 +392,7 @@ FFMPEGWriter::FFMPEGWriter(const std::string &filename, DeviceSpecs specs, Conta
|
||||
}
|
||||
}
|
||||
|
||||
m_codecCtx->sample_fmt = codec->sample_fmts[chosen_index];
|
||||
m_codecCtx->sample_fmt = sample_formats[chosen_index];
|
||||
m_deinterleave = av_sample_fmt_is_planar(m_codecCtx->sample_fmt);
|
||||
switch(av_get_alt_sample_fmt(m_codecCtx->sample_fmt, false))
|
||||
{
|
||||
@@ -385,21 +421,22 @@ FFMPEGWriter::FFMPEGWriter(const std::string &filename, DeviceSpecs specs, Conta
|
||||
}
|
||||
}
|
||||
|
||||
m_specs.format = specs.format;
|
||||
m_codecCtx->sample_rate = 0;
|
||||
|
||||
if(codec->supported_samplerates)
|
||||
if(sample_rates)
|
||||
{
|
||||
for(int i = 0; codec->supported_samplerates[i]; i++)
|
||||
for(int i = 0; sample_rates[i]; i++)
|
||||
{
|
||||
if(codec->supported_samplerates[i] == m_specs.rate)
|
||||
if(sample_rates[i] == m_specs.rate)
|
||||
{
|
||||
m_codecCtx->sample_rate = codec->supported_samplerates[i];
|
||||
m_codecCtx->sample_rate = sample_rates[i];
|
||||
break;
|
||||
}
|
||||
else if((codec->supported_samplerates[i] > m_codecCtx->sample_rate && m_specs.rate > m_codecCtx->sample_rate) ||
|
||||
(codec->supported_samplerates[i] < m_codecCtx->sample_rate && m_specs.rate < codec->supported_samplerates[i]))
|
||||
else if((sample_rates[i] > m_codecCtx->sample_rate && m_specs.rate > m_codecCtx->sample_rate) ||
|
||||
(sample_rates[i] < m_codecCtx->sample_rate && m_specs.rate < sample_rates[i]))
|
||||
{
|
||||
m_codecCtx->sample_rate = codec->supported_samplerates[i];
|
||||
m_codecCtx->sample_rate = sample_rates[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -407,6 +444,10 @@ FFMPEGWriter::FFMPEGWriter(const std::string &filename, DeviceSpecs specs, Conta
|
||||
if(m_codecCtx->sample_rate == 0)
|
||||
m_codecCtx->sample_rate = m_specs.rate;
|
||||
|
||||
#ifdef __EMSCRIPTEN__
|
||||
if(m_specs.rate != m_codecCtx->sample_rate)
|
||||
AUD_THROW(FileException, "The selected sample rate is not supported by this audio encoder.");
|
||||
#endif
|
||||
m_specs.rate = m_codecCtx->sample_rate;
|
||||
|
||||
#ifdef FFMPEG_OLD_CODE
|
||||
@@ -443,57 +484,93 @@ FFMPEGWriter::FFMPEGWriter(const std::string &filename, DeviceSpecs specs, Conta
|
||||
|
||||
if(avformat_write_header(m_formatCtx, nullptr) < 0)
|
||||
AUD_THROW(FileException, "File couldn't be written, writing the header failed.");
|
||||
}
|
||||
catch(Exception&)
|
||||
{
|
||||
#ifndef FFMPEG_OLD_CODE
|
||||
if(m_codecCtx)
|
||||
avcodec_free_context(&m_codecCtx);
|
||||
#ifdef FFMPEG_OLD_CODE
|
||||
m_packet = new AVPacket({});
|
||||
#else
|
||||
m_packet = av_packet_alloc();
|
||||
#endif
|
||||
avformat_free_context(m_formatCtx);
|
||||
m_frame = av_frame_alloc();
|
||||
if(!m_packet || !m_frame)
|
||||
AUD_THROW(FileException, "Cannot allocate FFmpeg audio frame/packet.");
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
release();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
void FFMPEGWriter::release() noexcept
|
||||
{
|
||||
av_frame_free(&m_frame);
|
||||
#ifdef FFMPEG_OLD_CODE
|
||||
m_packet = new AVPacket({});
|
||||
delete m_packet;
|
||||
m_packet = nullptr;
|
||||
if(m_codecCtx)
|
||||
avcodec_close(m_codecCtx);
|
||||
#else
|
||||
m_packet = av_packet_alloc();
|
||||
av_packet_free(&m_packet);
|
||||
avcodec_free_context(&m_codecCtx);
|
||||
#endif
|
||||
if(m_formatCtx)
|
||||
{
|
||||
avio_closep(&m_formatCtx->pb);
|
||||
avformat_free_context(m_formatCtx);
|
||||
m_formatCtx = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
m_frame = av_frame_alloc();
|
||||
void FFMPEGWriter::finish()
|
||||
{
|
||||
if(m_failed)
|
||||
AUD_THROW(FileException, "FFmpeg audio output previously failed.");
|
||||
if(m_finished)
|
||||
return;
|
||||
try
|
||||
{
|
||||
if(m_input_samples)
|
||||
{
|
||||
// Fixed-size encoders without small-last-frame support need zero padding.
|
||||
if(m_input_size && m_input_samples < m_input_size &&
|
||||
!(m_codecCtx->codec->capabilities & AV_CODEC_CAP_SMALL_LAST_FRAME))
|
||||
{
|
||||
std::memset(m_input_buffer.getBuffer() + m_input_samples * m_specs.channels, 0,
|
||||
(m_input_size - m_input_samples) * AUD_SAMPLE_SIZE(m_specs));
|
||||
m_position += m_input_size - m_input_samples;
|
||||
m_input_samples = m_input_size;
|
||||
}
|
||||
encode();
|
||||
m_input_samples = 0;
|
||||
}
|
||||
close();
|
||||
if(av_write_trailer(m_formatCtx) < 0)
|
||||
AUD_THROW(FileException, "FFmpeg could not write the audio trailer.");
|
||||
avio_flush(m_formatCtx->pb);
|
||||
const int io_error = m_formatCtx->pb->error;
|
||||
const int close_error = avio_closep(&m_formatCtx->pb);
|
||||
if(io_error < 0 || close_error < 0)
|
||||
AUD_THROW(FileException, "FFmpeg could not finalize the audio file.");
|
||||
m_finished = true;
|
||||
release();
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
m_failed = true;
|
||||
release();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
FFMPEGWriter::~FFMPEGWriter()
|
||||
{
|
||||
// writte missing data
|
||||
if(m_input_samples)
|
||||
encode();
|
||||
|
||||
close();
|
||||
|
||||
av_write_trailer(m_formatCtx);
|
||||
|
||||
if(m_frame)
|
||||
av_frame_free(&m_frame);
|
||||
|
||||
if(m_packet)
|
||||
// Existing desktop callers still finalize on destruction. Web mixdown calls
|
||||
// finish explicitly while failure can abort the staged-file transaction.
|
||||
if(!m_finished && !m_failed)
|
||||
{
|
||||
#ifdef FFMPEG_OLD_CODE
|
||||
delete m_packet;
|
||||
#else
|
||||
av_packet_free(&m_packet);
|
||||
#endif
|
||||
try { finish(); }
|
||||
catch(...) {}
|
||||
}
|
||||
|
||||
#ifdef FFMPEG_OLD_CODE
|
||||
avcodec_close(m_codecCtx);
|
||||
#else
|
||||
if(m_codecCtx)
|
||||
avcodec_free_context(&m_codecCtx);
|
||||
#endif
|
||||
|
||||
avio_closep(&m_formatCtx->pb);
|
||||
avformat_free_context(m_formatCtx);
|
||||
release();
|
||||
}
|
||||
|
||||
int FFMPEGWriter::getPosition() const
|
||||
@@ -508,44 +585,56 @@ DeviceSpecs FFMPEGWriter::getSpecs() const
|
||||
|
||||
void FFMPEGWriter::write(unsigned int length, sample_t* buffer)
|
||||
{
|
||||
unsigned int samplesize = AUD_SAMPLE_SIZE(m_specs);
|
||||
|
||||
if(m_input_size)
|
||||
if(m_finished || m_failed)
|
||||
AUD_THROW(FileException, "Cannot write to closed FFmpeg audio output.");
|
||||
try
|
||||
{
|
||||
sample_t* inbuf = m_input_buffer.getBuffer();
|
||||
unsigned int samplesize = AUD_SAMPLE_SIZE(m_specs);
|
||||
|
||||
while(length)
|
||||
if(m_input_size)
|
||||
{
|
||||
unsigned int len = std::min(m_input_size - m_input_samples, length);
|
||||
sample_t* inbuf = m_input_buffer.getBuffer();
|
||||
|
||||
std::memcpy(inbuf + m_input_samples * m_specs.channels, buffer, len * samplesize);
|
||||
|
||||
buffer += len * m_specs.channels;
|
||||
m_input_samples += len;
|
||||
m_position += len;
|
||||
length -= len;
|
||||
|
||||
if(m_input_samples == m_input_size)
|
||||
while(length)
|
||||
{
|
||||
encode();
|
||||
unsigned int len = std::min(m_input_size - m_input_samples, length);
|
||||
|
||||
m_input_samples = 0;
|
||||
std::memcpy(inbuf + m_input_samples * m_specs.channels, buffer, len * samplesize);
|
||||
|
||||
buffer += len * m_specs.channels;
|
||||
m_input_samples += len;
|
||||
m_position += len;
|
||||
length -= len;
|
||||
|
||||
if(m_input_samples == m_input_size)
|
||||
{
|
||||
encode();
|
||||
|
||||
m_input_samples = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
else // PCM data, can write directly!
|
||||
{
|
||||
int samplesize = AUD_SAMPLE_SIZE(m_specs);
|
||||
m_input_buffer.assureSize(length * std::max(AUD_DEVICE_SAMPLE_SIZE(m_specs), samplesize));
|
||||
|
||||
sample_t* buf = m_input_buffer.getBuffer();
|
||||
std::memcpy(buf, buffer, length * samplesize);
|
||||
|
||||
m_input_samples = length;
|
||||
|
||||
m_position += length;
|
||||
|
||||
encode();
|
||||
m_input_samples = 0;
|
||||
}
|
||||
}
|
||||
else // PCM data, can write directly!
|
||||
catch(...)
|
||||
{
|
||||
int samplesize = AUD_SAMPLE_SIZE(m_specs);
|
||||
m_input_buffer.assureSize(length * std::max(AUD_DEVICE_SAMPLE_SIZE(m_specs), samplesize));
|
||||
|
||||
sample_t* buf = m_input_buffer.getBuffer();
|
||||
m_convert(reinterpret_cast<data_t*>(buf), reinterpret_cast<data_t*>(buffer), length * m_specs.channels);
|
||||
|
||||
m_input_samples = length;
|
||||
|
||||
m_position += length;
|
||||
|
||||
encode();
|
||||
m_failed = true;
|
||||
release();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user