Add Chromium-only Blender WebEngine parity work
This commit is contained in:
75
blender-5.2.0/extern/audaspace/plugins/ffmpeg/FFMPEG.cpp
vendored
Normal file
75
blender-5.2.0/extern/audaspace/plugins/ffmpeg/FFMPEG.cpp
vendored
Normal file
@@ -0,0 +1,75 @@
|
||||
/*******************************************************************************
|
||||
* Copyright 2009-2024 Jörg Müller
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
******************************************************************************/
|
||||
|
||||
#include "FFMPEG.h"
|
||||
#include "FFMPEGReader.h"
|
||||
#include "FFMPEGWriter.h"
|
||||
#include "file/FileManager.h"
|
||||
|
||||
AUD_NAMESPACE_BEGIN
|
||||
|
||||
FFMPEG::FFMPEG()
|
||||
{
|
||||
#if LIBAVCODEC_VERSION_MAJOR < 58
|
||||
av_register_all();
|
||||
#endif
|
||||
}
|
||||
|
||||
void FFMPEG::registerPlugin()
|
||||
{
|
||||
std::shared_ptr<FFMPEG> plugin = std::shared_ptr<FFMPEG>(new FFMPEG);
|
||||
FileManager::registerInput(plugin);
|
||||
FileManager::registerOutput(plugin);
|
||||
}
|
||||
|
||||
std::shared_ptr<IReader> FFMPEG::createReader(const std::string &filename, int stream)
|
||||
{
|
||||
return std::shared_ptr<IReader>(new FFMPEGReader(filename, stream));
|
||||
}
|
||||
|
||||
std::shared_ptr<IReader> FFMPEG::createReader(std::shared_ptr<Buffer> buffer, int stream)
|
||||
{
|
||||
return std::shared_ptr<IReader>(new FFMPEGReader(buffer, stream));
|
||||
}
|
||||
|
||||
std::vector<StreamInfo> FFMPEG::queryStreams(const std::string &filename)
|
||||
{
|
||||
return FFMPEGReader(filename).queryStreams();
|
||||
}
|
||||
|
||||
std::vector<StreamInfo> FFMPEG::queryStreams(std::shared_ptr<Buffer> buffer)
|
||||
{
|
||||
return FFMPEGReader(buffer).queryStreams();
|
||||
}
|
||||
|
||||
std::shared_ptr<IWriter> FFMPEG::createWriter(const std::string &filename, DeviceSpecs specs, Container format, Codec codec, unsigned int bitrate)
|
||||
{
|
||||
return std::shared_ptr<IWriter>(new FFMPEGWriter(filename, specs, format, codec, bitrate));
|
||||
}
|
||||
|
||||
#ifdef FFMPEG_PLUGIN
|
||||
extern "C" AUD_PLUGIN_API void registerPlugin()
|
||||
{
|
||||
FFMPEG::registerPlugin();
|
||||
}
|
||||
|
||||
extern "C" AUD_PLUGIN_API const char* getName()
|
||||
{
|
||||
return "FFMPEG";
|
||||
}
|
||||
#endif
|
||||
|
||||
AUD_NAMESPACE_END
|
||||
62
blender-5.2.0/extern/audaspace/plugins/ffmpeg/FFMPEG.h
vendored
Normal file
62
blender-5.2.0/extern/audaspace/plugins/ffmpeg/FFMPEG.h
vendored
Normal file
@@ -0,0 +1,62 @@
|
||||
/*******************************************************************************
|
||||
* Copyright 2009-2024 Jörg Müller
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
******************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifdef FFMPEG_PLUGIN
|
||||
#define AUD_BUILD_PLUGIN
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @file FFMPEG.h
|
||||
* @ingroup plugin
|
||||
* The FFMPEG class.
|
||||
*/
|
||||
|
||||
#include "file/IFileInput.h"
|
||||
#include "file/IFileOutput.h"
|
||||
|
||||
AUD_NAMESPACE_BEGIN
|
||||
|
||||
/**
|
||||
* This plugin class reads and writes sounds via ffmpeg.
|
||||
*/
|
||||
class AUD_PLUGIN_API FFMPEG : public IFileInput, public IFileOutput
|
||||
{
|
||||
private:
|
||||
// delete copy constructor and operator=
|
||||
FFMPEG(const FFMPEG&) = delete;
|
||||
FFMPEG& operator=(const FFMPEG&) = delete;
|
||||
|
||||
public:
|
||||
/**
|
||||
* Creates a new ffmpeg plugin.
|
||||
*/
|
||||
FFMPEG();
|
||||
|
||||
/**
|
||||
* Registers this plugin.
|
||||
*/
|
||||
static void registerPlugin();
|
||||
|
||||
virtual std::shared_ptr<IReader> createReader(const std::string &filename, int stream = 0);
|
||||
virtual std::shared_ptr<IReader> createReader(std::shared_ptr<Buffer> buffer, int stream = 0);
|
||||
virtual std::vector<StreamInfo> queryStreams(const std::string &filename);
|
||||
virtual std::vector<StreamInfo> queryStreams(std::shared_ptr<Buffer> buffer);
|
||||
virtual std::shared_ptr<IWriter> createWriter(const std::string &filename, DeviceSpecs specs, Container format, Codec codec, unsigned int bitrate);
|
||||
};
|
||||
|
||||
AUD_NAMESPACE_END
|
||||
583
blender-5.2.0/extern/audaspace/plugins/ffmpeg/FFMPEGReader.cpp
vendored
Normal file
583
blender-5.2.0/extern/audaspace/plugins/ffmpeg/FFMPEGReader.cpp
vendored
Normal file
@@ -0,0 +1,583 @@
|
||||
/*******************************************************************************
|
||||
* Copyright 2009-2024 Jörg Müller
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
******************************************************************************/
|
||||
|
||||
#include "FFMPEGReader.h"
|
||||
#include "Exception.h"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
extern "C" {
|
||||
#include <libavcodec/avcodec.h>
|
||||
#include <libavformat/avio.h>
|
||||
#include <libavutil/avutil.h>
|
||||
}
|
||||
|
||||
AUD_NAMESPACE_BEGIN
|
||||
|
||||
/* FFmpeg < 4.0 */
|
||||
#if LIBAVCODEC_VERSION_MAJOR < 58
|
||||
#define FFMPEG_OLD_CODE
|
||||
#endif
|
||||
/* FFmpeg < 5.0 */
|
||||
#if LIBAVCODEC_VERSION_MAJOR < 59
|
||||
#define FFMPEG_OLD_CH_LAYOUT
|
||||
#endif
|
||||
|
||||
SampleFormat FFMPEGReader::convertSampleFormat(AVSampleFormat format)
|
||||
{
|
||||
switch(av_get_packed_sample_fmt(format))
|
||||
{
|
||||
case AV_SAMPLE_FMT_U8:
|
||||
return FORMAT_U8;
|
||||
case AV_SAMPLE_FMT_S16:
|
||||
return FORMAT_S16;
|
||||
case AV_SAMPLE_FMT_S32:
|
||||
return FORMAT_S32;
|
||||
case AV_SAMPLE_FMT_FLT:
|
||||
return FORMAT_FLOAT32;
|
||||
case AV_SAMPLE_FMT_DBL:
|
||||
return FORMAT_FLOAT64;
|
||||
default:
|
||||
AUD_THROW(FileException, "FFMPEG sample format unknown.");
|
||||
}
|
||||
}
|
||||
|
||||
int FFMPEGReader::decode(AVPacket& packet, Buffer& buffer)
|
||||
{
|
||||
int buf_size = buffer.getSize();
|
||||
int buf_pos = 0;
|
||||
|
||||
#ifdef FFMPEG_OLD_CODE
|
||||
int got_frame;
|
||||
int read_length;
|
||||
uint8_t* orig_data = packet.data;
|
||||
int orig_size = packet.size;
|
||||
|
||||
while(packet.size > 0)
|
||||
{
|
||||
got_frame = 0;
|
||||
|
||||
read_length = avcodec_decode_audio4(m_codecCtx, m_frame, &got_frame, &packet);
|
||||
if(read_length < 0)
|
||||
break;
|
||||
|
||||
if(got_frame)
|
||||
{
|
||||
int data_size = av_samples_get_buffer_size(nullptr, m_codecCtx->channels, m_frame->nb_samples, m_codecCtx->sample_fmt, 1);
|
||||
|
||||
if(buf_size - buf_pos < data_size)
|
||||
{
|
||||
buffer.resize(buf_size + data_size, true);
|
||||
buf_size += data_size;
|
||||
}
|
||||
|
||||
if(m_tointerleave)
|
||||
{
|
||||
int single_size = data_size / m_codecCtx->channels / m_frame->nb_samples;
|
||||
for(int channel = 0; channel < m_codecCtx->channels; channel++)
|
||||
{
|
||||
for(int i = 0; i < m_frame->nb_samples; i++)
|
||||
{
|
||||
std::memcpy(((data_t*)buffer.getBuffer()) + buf_pos + ((m_codecCtx->channels * i) + channel) * single_size,
|
||||
m_frame->data[channel] + i * single_size, single_size);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
std::memcpy(((data_t*)buffer.getBuffer()) + buf_pos, m_frame->data[0], data_size);
|
||||
|
||||
buf_pos += data_size;
|
||||
}
|
||||
packet.size -= read_length;
|
||||
packet.data += read_length;
|
||||
}
|
||||
|
||||
packet.data = orig_data;
|
||||
packet.size = orig_size;
|
||||
#else
|
||||
avcodec_send_packet(m_codecCtx, &packet);
|
||||
|
||||
while(true)
|
||||
{
|
||||
auto ret = avcodec_receive_frame(m_codecCtx, m_frame);
|
||||
|
||||
if(ret != 0)
|
||||
break;
|
||||
|
||||
#ifdef FFMPEG_OLD_CH_LAYOUT
|
||||
int channels = m_codecCtx->channels;
|
||||
#else
|
||||
int channels = m_codecCtx->ch_layout.nb_channels;
|
||||
#endif
|
||||
|
||||
int data_size = av_samples_get_buffer_size(nullptr, channels, m_frame->nb_samples, m_codecCtx->sample_fmt, 1);
|
||||
|
||||
if(buf_size - buf_pos < data_size)
|
||||
{
|
||||
buffer.resize(buf_size + data_size, true);
|
||||
buf_size += data_size;
|
||||
}
|
||||
|
||||
if(m_tointerleave)
|
||||
{
|
||||
int single_size = data_size / channels / m_frame->nb_samples;
|
||||
for(int channel = 0; channel < channels; channel++)
|
||||
{
|
||||
for(int i = 0; i < m_frame->nb_samples; i++)
|
||||
{
|
||||
std::memcpy(((data_t*)buffer.getBuffer()) + buf_pos + ((channels * i) + channel) * single_size,
|
||||
m_frame->data[channel] + i * single_size, single_size);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
std::memcpy(((data_t*)buffer.getBuffer()) + buf_pos, m_frame->data[0], data_size);
|
||||
|
||||
buf_pos += data_size;
|
||||
}
|
||||
#endif
|
||||
|
||||
return buf_pos;
|
||||
}
|
||||
|
||||
void FFMPEGReader::init(int stream)
|
||||
{
|
||||
m_position = 0;
|
||||
m_pkgbuf_left = 0;
|
||||
|
||||
if(avformat_find_stream_info(m_formatCtx, nullptr) < 0)
|
||||
AUD_THROW(FileException, "File couldn't be read, ffmpeg couldn't find the stream info.");
|
||||
|
||||
// find audio stream and codec
|
||||
m_stream = -1;
|
||||
|
||||
for(unsigned int i = 0; i < m_formatCtx->nb_streams; i++)
|
||||
{
|
||||
#ifdef FFMPEG_OLD_CODE
|
||||
if((m_formatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO)
|
||||
#else
|
||||
if((m_formatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)
|
||||
#endif
|
||||
&& (m_stream < 0))
|
||||
{
|
||||
if(stream == 0)
|
||||
{
|
||||
m_stream=i;
|
||||
break;
|
||||
}
|
||||
else
|
||||
stream--;
|
||||
}
|
||||
}
|
||||
|
||||
if(m_stream == -1)
|
||||
AUD_THROW(FileException, "File couldn't be read, no audio stream found by ffmpeg.");
|
||||
|
||||
// get a decoder and open it
|
||||
#ifndef FFMPEG_OLD_CODE
|
||||
const AVCodec* aCodec = avcodec_find_decoder(m_formatCtx->streams[m_stream]->codecpar->codec_id);
|
||||
|
||||
if(!aCodec)
|
||||
AUD_THROW(FileException, "File couldn't be read, no decoder found with ffmpeg.");
|
||||
#endif
|
||||
|
||||
m_frame = av_frame_alloc();
|
||||
|
||||
if(!m_frame)
|
||||
AUD_THROW(FileException, "File couldn't be read, ffmpeg frame couldn't be allocated.");
|
||||
|
||||
#ifdef FFMPEG_OLD_CODE
|
||||
m_codecCtx = m_formatCtx->streams[m_stream]->codec;
|
||||
|
||||
AVCodec* aCodec = avcodec_find_decoder(m_codecCtx->codec_id);
|
||||
#else
|
||||
m_codecCtx = avcodec_alloc_context3(aCodec);
|
||||
#endif
|
||||
|
||||
if(!m_codecCtx)
|
||||
AUD_THROW(FileException, "File couldn't be read, ffmpeg context couldn't be allocated.");
|
||||
|
||||
#ifndef FFMPEG_OLD_CODE
|
||||
if(avcodec_parameters_to_context(m_codecCtx, m_formatCtx->streams[m_stream]->codecpar) < 0)
|
||||
AUD_THROW(FileException, "File couldn't be read, ffmpeg decoder parameters couldn't be copied to decoder context.");
|
||||
#endif
|
||||
|
||||
if(avcodec_open2(m_codecCtx, aCodec, nullptr) < 0)
|
||||
AUD_THROW(FileException, "File couldn't be read, ffmpeg codec couldn't be opened.");
|
||||
|
||||
#ifdef FFMPEG_OLD_CH_LAYOUT
|
||||
int channels = m_codecCtx->channels;
|
||||
#else
|
||||
int channels = m_codecCtx->ch_layout.nb_channels;
|
||||
#endif
|
||||
m_specs.channels = (Channels) channels;
|
||||
m_tointerleave = av_sample_fmt_is_planar(m_codecCtx->sample_fmt);
|
||||
|
||||
switch(av_get_packed_sample_fmt(m_codecCtx->sample_fmt))
|
||||
{
|
||||
case AV_SAMPLE_FMT_U8:
|
||||
m_convert = convert_u8_float;
|
||||
m_specs.format = FORMAT_U8;
|
||||
break;
|
||||
case AV_SAMPLE_FMT_S16:
|
||||
m_convert = convert_s16_float;
|
||||
m_specs.format = FORMAT_S16;
|
||||
break;
|
||||
case AV_SAMPLE_FMT_S32:
|
||||
m_convert = convert_s32_float;
|
||||
m_specs.format = FORMAT_S32;
|
||||
break;
|
||||
case AV_SAMPLE_FMT_FLT:
|
||||
m_convert = convert_copy<float>;
|
||||
m_specs.format = FORMAT_FLOAT32;
|
||||
break;
|
||||
case AV_SAMPLE_FMT_DBL:
|
||||
m_convert = convert_double_float;
|
||||
m_specs.format = FORMAT_FLOAT64;
|
||||
break;
|
||||
default:
|
||||
AUD_THROW(FileException, "File couldn't be read, ffmpeg sample format unknown.");
|
||||
}
|
||||
|
||||
m_specs.rate = (SampleRate) m_codecCtx->sample_rate;
|
||||
}
|
||||
|
||||
FFMPEGReader::FFMPEGReader(const std::string& filename, int stream) : m_pkgbuf(), m_formatCtx(nullptr), m_codecCtx(nullptr), m_frame(nullptr), m_aviocontext(nullptr)
|
||||
{
|
||||
// open file
|
||||
if(avformat_open_input(&m_formatCtx, filename.c_str(), nullptr, nullptr)!=0)
|
||||
AUD_THROW(FileException, "File couldn't be opened with ffmpeg.");
|
||||
|
||||
try
|
||||
{
|
||||
init(stream);
|
||||
}
|
||||
catch(Exception&)
|
||||
{
|
||||
avformat_close_input(&m_formatCtx);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
FFMPEGReader::FFMPEGReader(std::shared_ptr<Buffer> buffer, int stream) :
|
||||
m_pkgbuf(),
|
||||
m_codecCtx(nullptr),
|
||||
m_frame(nullptr),
|
||||
m_membuffer(buffer),
|
||||
m_membufferpos(0)
|
||||
{
|
||||
constexpr int BUFFER_SIZE{4096};
|
||||
|
||||
auto membuf = reinterpret_cast<data_t*>(av_malloc(BUFFER_SIZE));
|
||||
|
||||
m_aviocontext = avio_alloc_context(membuf, BUFFER_SIZE, 0, this, read_packet, nullptr, seek_packet);
|
||||
|
||||
if(!m_aviocontext)
|
||||
{
|
||||
av_free(membuf);
|
||||
AUD_THROW(FileException, "Buffer reading context couldn't be created with ffmpeg.");
|
||||
}
|
||||
|
||||
m_formatCtx = avformat_alloc_context();
|
||||
m_formatCtx->pb = m_aviocontext;
|
||||
if(avformat_open_input(&m_formatCtx, "", nullptr, nullptr)!=0)
|
||||
{
|
||||
if(m_aviocontext->buffer)
|
||||
av_free(m_aviocontext->buffer);
|
||||
av_free(m_aviocontext);
|
||||
AUD_THROW(FileException, "Buffer couldn't be read with ffmpeg.");
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
init(stream);
|
||||
}
|
||||
catch(Exception&)
|
||||
{
|
||||
avformat_close_input(&m_formatCtx);
|
||||
if(m_aviocontext->buffer)
|
||||
av_free(m_aviocontext->buffer);
|
||||
av_free(m_aviocontext);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
FFMPEGReader::~FFMPEGReader()
|
||||
{
|
||||
if(m_frame)
|
||||
av_frame_free(&m_frame);
|
||||
#ifdef FFMPEG_OLD_CODE
|
||||
avcodec_close(m_codecCtx);
|
||||
#else
|
||||
if(m_codecCtx)
|
||||
avcodec_free_context(&m_codecCtx);
|
||||
#endif
|
||||
if(m_aviocontext)
|
||||
{
|
||||
if(m_aviocontext->buffer)
|
||||
av_free(m_aviocontext->buffer);
|
||||
av_free(m_aviocontext);
|
||||
}
|
||||
|
||||
avformat_close_input(&m_formatCtx);
|
||||
}
|
||||
|
||||
std::vector<StreamInfo> FFMPEGReader::queryStreams()
|
||||
{
|
||||
std::vector<StreamInfo> result;
|
||||
|
||||
for(unsigned int i = 0; i < m_formatCtx->nb_streams; i++)
|
||||
{
|
||||
#ifdef FFMPEG_OLD_CODE
|
||||
if(m_formatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO)
|
||||
#else
|
||||
if(m_formatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)
|
||||
#endif
|
||||
{
|
||||
StreamInfo info;
|
||||
|
||||
double time_base = av_q2d(m_formatCtx->streams[i]->time_base);
|
||||
|
||||
if(m_formatCtx->streams[i]->start_time != AV_NOPTS_VALUE)
|
||||
info.start = m_formatCtx->streams[i]->start_time * time_base;
|
||||
else
|
||||
info.start = 0;
|
||||
|
||||
if(m_formatCtx->streams[i]->duration != AV_NOPTS_VALUE)
|
||||
info.duration = m_formatCtx->streams[i]->duration * time_base;
|
||||
else if(m_formatCtx->duration != AV_NOPTS_VALUE)
|
||||
info.duration = double(m_formatCtx->duration) / AV_TIME_BASE - info.start;
|
||||
else
|
||||
info.duration = 0;
|
||||
|
||||
#ifdef FFMPEG_OLD_CODE
|
||||
info.specs.channels = Channels(m_formatCtx->streams[i]->codec->channels);
|
||||
info.specs.rate = m_formatCtx->streams[i]->codec->sample_rate;
|
||||
info.specs.format = convertSampleFormat(m_formatCtx->streams[i]->codec->sample_fmt);
|
||||
#else
|
||||
#ifdef FFMPEG_OLD_CH_LAYOUT
|
||||
int channels = m_formatCtx->streams[i]->codecpar->channels;
|
||||
#else
|
||||
int channels = m_formatCtx->streams[i]->codecpar->ch_layout.nb_channels;
|
||||
#endif
|
||||
info.specs.channels = Channels(channels);
|
||||
info.specs.rate = m_formatCtx->streams[i]->codecpar->sample_rate;
|
||||
info.specs.format = convertSampleFormat(AVSampleFormat(m_formatCtx->streams[i]->codecpar->format));
|
||||
#endif
|
||||
|
||||
result.emplace_back(info);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
int FFMPEGReader::read_packet(void* opaque, uint8_t* buf, int buf_size)
|
||||
{
|
||||
FFMPEGReader* reader = reinterpret_cast<FFMPEGReader*>(opaque);
|
||||
|
||||
long long size = std::min(static_cast<long long>(buf_size), reader->m_membuffer->getSize() - reader->m_membufferpos);
|
||||
|
||||
if(size <= 0)
|
||||
return AVERROR_EOF;
|
||||
|
||||
std::memcpy(buf, ((data_t*)reader->m_membuffer->getBuffer()) + reader->m_membufferpos, size);
|
||||
reader->m_membufferpos += size;
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
int64_t FFMPEGReader::seek_packet(void* opaque, int64_t offset, int whence)
|
||||
{
|
||||
FFMPEGReader* reader = reinterpret_cast<FFMPEGReader*>(opaque);
|
||||
|
||||
switch(whence)
|
||||
{
|
||||
case SEEK_SET:
|
||||
reader->m_membufferpos = 0;
|
||||
break;
|
||||
case SEEK_END:
|
||||
reader->m_membufferpos = reader->m_membuffer->getSize();
|
||||
break;
|
||||
case AVSEEK_SIZE:
|
||||
return reader->m_membuffer->getSize();
|
||||
}
|
||||
|
||||
int64_t position = reader->m_membufferpos + offset;
|
||||
|
||||
if(position > reader->m_membuffer->getSize())
|
||||
position = reader->m_membuffer->getSize();
|
||||
|
||||
reader->m_membufferpos = int(position);
|
||||
|
||||
return position;
|
||||
}
|
||||
|
||||
bool FFMPEGReader::isSeekable() const
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
void FFMPEGReader::seek(int position)
|
||||
{
|
||||
if(position >= 0)
|
||||
{
|
||||
double pts_time_base = av_q2d(m_formatCtx->streams[m_stream]->time_base);
|
||||
|
||||
int64_t st_time = m_formatCtx->streams[m_stream]->start_time;
|
||||
uint64_t seek_pos = (uint64_t)(position / (pts_time_base * m_specs.rate));
|
||||
|
||||
if(st_time != AV_NOPTS_VALUE)
|
||||
seek_pos += st_time;
|
||||
|
||||
// a value < 0 tells us that seeking failed
|
||||
if(av_seek_frame(m_formatCtx, m_stream, seek_pos, AVSEEK_FLAG_BACKWARD | AVSEEK_FLAG_ANY) >= 0)
|
||||
{
|
||||
avcodec_flush_buffers(m_codecCtx);
|
||||
m_position = position;
|
||||
|
||||
AVPacket packet;
|
||||
bool search = true;
|
||||
|
||||
while(search && av_read_frame(m_formatCtx, &packet) >= 0)
|
||||
{
|
||||
// is it a frame from the audio stream?
|
||||
if(packet.stream_index == m_stream)
|
||||
{
|
||||
// decode the package
|
||||
m_pkgbuf_left = decode(packet, m_pkgbuf);
|
||||
search = false;
|
||||
|
||||
// check position
|
||||
if(packet.pts != AV_NOPTS_VALUE)
|
||||
{
|
||||
// calculate real position, and read to frame!
|
||||
m_position = (packet.pts - (st_time != AV_NOPTS_VALUE ? st_time : 0)) * pts_time_base * m_specs.rate;
|
||||
|
||||
if(m_position < position)
|
||||
{
|
||||
// read until we're at the right position
|
||||
int length = AUD_DEFAULT_BUFFER_SIZE;
|
||||
Buffer buffer(length * AUD_SAMPLE_SIZE(m_specs));
|
||||
bool eos;
|
||||
for(int len = position - m_position; len > 0; len -= AUD_DEFAULT_BUFFER_SIZE)
|
||||
{
|
||||
if(len < AUD_DEFAULT_BUFFER_SIZE)
|
||||
length = len;
|
||||
read(length, eos, buffer.getBuffer());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
av_packet_unref(&packet);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
fprintf(stderr, "seeking failed!\n");
|
||||
// Seeking failed, do nothing.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int FFMPEGReader::getLength() const
|
||||
{
|
||||
auto stream = m_formatCtx->streams[m_stream];
|
||||
|
||||
double time_base = av_q2d(stream->time_base);
|
||||
double duration;
|
||||
|
||||
if(stream->duration != AV_NOPTS_VALUE)
|
||||
duration = stream->duration * time_base;
|
||||
else if(m_formatCtx->duration != AV_NOPTS_VALUE)
|
||||
{
|
||||
duration = float(m_formatCtx->duration) / AV_TIME_BASE;
|
||||
|
||||
if(stream->start_time != AV_NOPTS_VALUE)
|
||||
duration -= stream->start_time * time_base;
|
||||
}
|
||||
else
|
||||
duration = -1;
|
||||
|
||||
// return approximated remaning size
|
||||
return (int)(duration * m_codecCtx->sample_rate) - m_position;
|
||||
}
|
||||
|
||||
int FFMPEGReader::getPosition() const
|
||||
{
|
||||
return m_position;
|
||||
}
|
||||
|
||||
Specs FFMPEGReader::getSpecs() const
|
||||
{
|
||||
return m_specs.specs;
|
||||
}
|
||||
|
||||
void FFMPEGReader::read(int& length, bool& eos, sample_t* buffer)
|
||||
{
|
||||
// read packages and decode them
|
||||
AVPacket packet = {};
|
||||
int data_size = 0;
|
||||
int pkgbuf_pos;
|
||||
int left = length;
|
||||
int sample_size = AUD_DEVICE_SAMPLE_SIZE(m_specs);
|
||||
|
||||
sample_t* buf = buffer;
|
||||
pkgbuf_pos = m_pkgbuf_left;
|
||||
m_pkgbuf_left = 0;
|
||||
|
||||
// there may still be data in the buffer from the last call
|
||||
if(pkgbuf_pos > 0)
|
||||
{
|
||||
data_size = std::min(pkgbuf_pos, left * sample_size);
|
||||
m_convert((data_t*) buf, (data_t*) m_pkgbuf.getBuffer(), data_size / AUD_FORMAT_SIZE(m_specs.format));
|
||||
buf += data_size / AUD_FORMAT_SIZE(m_specs.format);
|
||||
left -= data_size / sample_size;
|
||||
}
|
||||
|
||||
// for each frame read as long as there isn't enough data already
|
||||
while((left > 0) && (av_read_frame(m_formatCtx, &packet) >= 0))
|
||||
{
|
||||
// is it a frame from the audio stream?
|
||||
if(packet.stream_index == m_stream)
|
||||
{
|
||||
// decode the package
|
||||
pkgbuf_pos = decode(packet, m_pkgbuf);
|
||||
|
||||
// copy to output buffer
|
||||
data_size = std::min(pkgbuf_pos, left * sample_size);
|
||||
m_convert((data_t*) buf, (data_t*) m_pkgbuf.getBuffer(), data_size / AUD_FORMAT_SIZE(m_specs.format));
|
||||
buf += data_size / AUD_FORMAT_SIZE(m_specs.format);
|
||||
left -= data_size / sample_size;
|
||||
}
|
||||
av_packet_unref(&packet);
|
||||
}
|
||||
// read more data than necessary?
|
||||
if(pkgbuf_pos > data_size)
|
||||
{
|
||||
m_pkgbuf_left = pkgbuf_pos-data_size;
|
||||
memmove(m_pkgbuf.getBuffer(),
|
||||
((data_t*)m_pkgbuf.getBuffer())+data_size,
|
||||
pkgbuf_pos-data_size);
|
||||
}
|
||||
|
||||
if((eos = (left > 0)))
|
||||
length -= left;
|
||||
|
||||
m_position += length;
|
||||
}
|
||||
|
||||
AUD_NAMESPACE_END
|
||||
203
blender-5.2.0/extern/audaspace/plugins/ffmpeg/FFMPEGReader.h
vendored
Normal file
203
blender-5.2.0/extern/audaspace/plugins/ffmpeg/FFMPEGReader.h
vendored
Normal file
@@ -0,0 +1,203 @@
|
||||
/*******************************************************************************
|
||||
* Copyright 2009-2024 Jörg Müller
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
******************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifdef FFMPEG_PLUGIN
|
||||
#define AUD_BUILD_PLUGIN
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @file FFMPEGReader.h
|
||||
* @ingroup plugin
|
||||
* The FFMPEGReader class.
|
||||
*/
|
||||
|
||||
#include "respec/ConverterFunctions.h"
|
||||
#include "IReader.h"
|
||||
#include "util/Buffer.h"
|
||||
#include "file/FileInfo.h"
|
||||
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
struct AVCodecContext;
|
||||
extern "C" {
|
||||
#include <libavformat/avformat.h>
|
||||
}
|
||||
|
||||
AUD_NAMESPACE_BEGIN
|
||||
|
||||
/**
|
||||
* This class reads a sound file via ffmpeg.
|
||||
* \warning Seeking may not be accurate! Moreover the position is updated after
|
||||
* a buffer reading call. So calling getPosition right after seek
|
||||
* normally results in a wrong value.
|
||||
*/
|
||||
class AUD_PLUGIN_API FFMPEGReader : public IReader
|
||||
{
|
||||
private:
|
||||
/**
|
||||
* The current position in samples.
|
||||
*/
|
||||
int m_position;
|
||||
|
||||
/**
|
||||
* The specification of the audio data.
|
||||
*/
|
||||
DeviceSpecs m_specs;
|
||||
|
||||
/**
|
||||
* The buffer for package reading.
|
||||
*/
|
||||
Buffer m_pkgbuf;
|
||||
|
||||
/**
|
||||
* The count of samples still available from the last read package.
|
||||
*/
|
||||
int m_pkgbuf_left;
|
||||
|
||||
/**
|
||||
* The AVFormatContext structure for using ffmpeg.
|
||||
*/
|
||||
AVFormatContext* m_formatCtx;
|
||||
|
||||
/**
|
||||
* The AVCodecContext structure for using ffmpeg.
|
||||
*/
|
||||
AVCodecContext* m_codecCtx;
|
||||
|
||||
/**
|
||||
* The AVFrame structure for using ffmpeg.
|
||||
*/
|
||||
AVFrame* m_frame;
|
||||
|
||||
/**
|
||||
* The AVIOContext to read the data from.
|
||||
*/
|
||||
AVIOContext* m_aviocontext;
|
||||
|
||||
/**
|
||||
* The stream ID in the file.
|
||||
*/
|
||||
int m_stream;
|
||||
|
||||
/**
|
||||
* Converter function.
|
||||
*/
|
||||
convert_f m_convert;
|
||||
|
||||
/**
|
||||
* The memory file to read from.
|
||||
*/
|
||||
std::shared_ptr<Buffer> m_membuffer;
|
||||
|
||||
/**
|
||||
* Reading position of the buffer.
|
||||
*/
|
||||
long long m_membufferpos;
|
||||
|
||||
/**
|
||||
* Whether the audio data has to be interleaved after reading.
|
||||
*/
|
||||
bool m_tointerleave;
|
||||
|
||||
/**
|
||||
* Converts an ffmpeg sample format to an audaspace one.
|
||||
* \param format The AVSampleFormat sample format.
|
||||
* \return The sample format as SampleFormat.
|
||||
*/
|
||||
AUD_LOCAL static SampleFormat convertSampleFormat(AVSampleFormat format);
|
||||
|
||||
/**
|
||||
* Decodes a packet into the given buffer.
|
||||
* \param packet The AVPacket to decode.
|
||||
* \param buffer The target buffer.
|
||||
* \return The count of read bytes.
|
||||
*/
|
||||
AUD_LOCAL int decode(AVPacket& packet, Buffer& buffer);
|
||||
|
||||
/**
|
||||
* Initializes the object.
|
||||
* \param stream The index of the audio stream within the file if it contains multiple audio streams.
|
||||
*/
|
||||
AUD_LOCAL void init(int stream);
|
||||
|
||||
// delete copy constructor and operator=
|
||||
FFMPEGReader(const FFMPEGReader&) = delete;
|
||||
FFMPEGReader& operator=(const FFMPEGReader&) = delete;
|
||||
|
||||
public:
|
||||
/**
|
||||
* Creates a new reader.
|
||||
* \param filename The path to the file to be read.
|
||||
* \param stream The index of the audio stream within the file if it contains multiple audio streams.
|
||||
* \exception Exception Thrown if the file specified does not exist or
|
||||
* cannot be read with ffmpeg.
|
||||
*/
|
||||
FFMPEGReader(const std::string &filename, int stream = 0);
|
||||
|
||||
/**
|
||||
* Creates a new reader.
|
||||
* \param buffer The buffer to read from.
|
||||
* \param stream The index of the audio stream within the file if it contains multiple audio streams.
|
||||
* \exception Exception Thrown if the buffer specified cannot be read
|
||||
* with ffmpeg.
|
||||
*/
|
||||
FFMPEGReader(std::shared_ptr<Buffer> buffer, int stream = 0);
|
||||
|
||||
/**
|
||||
* Destroys the reader and closes the file.
|
||||
*/
|
||||
virtual ~FFMPEGReader();
|
||||
|
||||
/**
|
||||
* Queries the streams of a sound file.
|
||||
* \return A vector with as many streams as there are in the file.
|
||||
* \exception Exception Thrown if the file specified cannot be read.
|
||||
*/
|
||||
virtual std::vector<StreamInfo> queryStreams();
|
||||
|
||||
/**
|
||||
* Reads data to a memory buffer.
|
||||
* This function is used for avio only.
|
||||
* @param opaque The FFMPEGReader.
|
||||
* @param buf The buffer to read to.
|
||||
* @param buf_size The size of the buffer.
|
||||
* @return How many bytes have been read.
|
||||
*/
|
||||
static int read_packet(void* opaque, uint8_t* buf, int buf_size);
|
||||
|
||||
/**
|
||||
* Seeks within data.
|
||||
* This function is used for avio only.
|
||||
* @param opaque The FFMPEGReader.
|
||||
* @param offset The byte offset to seek to.
|
||||
* @param whence The seeking action.
|
||||
* @return The current position or the size of the data if requested.
|
||||
*/
|
||||
static int64_t seek_packet(void* opaque, int64_t offset, int whence);
|
||||
|
||||
virtual bool isSeekable() const;
|
||||
virtual void seek(int position);
|
||||
virtual int getLength() const;
|
||||
virtual int getPosition() const;
|
||||
virtual Specs getSpecs() const;
|
||||
virtual void read(int& length, bool& eos, sample_t* buffer);
|
||||
};
|
||||
|
||||
AUD_NAMESPACE_END
|
||||
552
blender-5.2.0/extern/audaspace/plugins/ffmpeg/FFMPEGWriter.cpp
vendored
Normal file
552
blender-5.2.0/extern/audaspace/plugins/ffmpeg/FFMPEGWriter.cpp
vendored
Normal file
@@ -0,0 +1,552 @@
|
||||
/*******************************************************************************
|
||||
* Copyright 2009-2024 Jörg Müller
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
******************************************************************************/
|
||||
|
||||
#include "FFMPEGWriter.h"
|
||||
#include "Exception.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstring>
|
||||
|
||||
extern "C" {
|
||||
#include <libavcodec/avcodec.h>
|
||||
#include <libavformat/avio.h>
|
||||
#if LIBAVCODEC_VERSION_MAJOR >= 59
|
||||
#include <libavutil/channel_layout.h>
|
||||
#endif
|
||||
}
|
||||
|
||||
AUD_NAMESPACE_BEGIN
|
||||
|
||||
/* FFmpeg < 4.0 */
|
||||
#if LIBAVCODEC_VERSION_MAJOR < 58
|
||||
#define FFMPEG_OLD_CODE
|
||||
#endif
|
||||
/* FFmpeg < 5.0 */
|
||||
#if LIBAVCODEC_VERSION_MAJOR < 59
|
||||
#define FFMPEG_OLD_CH_LAYOUT
|
||||
#endif
|
||||
|
||||
void FFMPEGWriter::encode()
|
||||
{
|
||||
sample_t* data = m_input_buffer.getBuffer();
|
||||
|
||||
if(m_deinterleave)
|
||||
{
|
||||
m_deinterleave_buffer.assureSize(m_input_buffer.getSize());
|
||||
|
||||
sample_t* dbuf = m_deinterleave_buffer.getBuffer();
|
||||
// deinterleave
|
||||
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++)
|
||||
{
|
||||
std::memcpy(((data_t*)dbuf) + (m_input_samples * channel + i) * single_size,
|
||||
((data_t*)data) + ((m_specs.channels * i) + channel) * single_size, single_size);
|
||||
}
|
||||
}
|
||||
|
||||
// convert first
|
||||
if(m_input_size)
|
||||
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());
|
||||
}
|
||||
else
|
||||
// convert first
|
||||
if(m_input_size)
|
||||
m_convert(reinterpret_cast<data_t*>(data), reinterpret_cast<data_t*>(data), m_input_samples * m_specs.channels);
|
||||
|
||||
#ifdef FFMPEG_OLD_CODE
|
||||
m_packet->data = nullptr;
|
||||
m_packet->size = 0;
|
||||
|
||||
av_init_packet(m_packet);
|
||||
|
||||
av_frame_unref(m_frame);
|
||||
int got_packet;
|
||||
#endif
|
||||
|
||||
m_frame->nb_samples = m_input_samples;
|
||||
m_frame->format = m_codecCtx->sample_fmt;
|
||||
#ifdef FFMPEG_OLD_CH_LAYOUT
|
||||
m_frame->channel_layout = m_codecCtx->channel_layout;
|
||||
m_frame->channels = m_specs.channels;
|
||||
#else
|
||||
if(av_channel_layout_copy(&m_frame->ch_layout, &m_codecCtx->ch_layout) < 0)
|
||||
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)
|
||||
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);
|
||||
|
||||
#ifdef FFMPEG_OLD_CODE
|
||||
if(avcodec_encode_audio2(m_codecCtx, m_packet, m_frame, &got_packet))
|
||||
{
|
||||
AUD_THROW(FileException, "File couldn't be written, audio encoding failed with ffmpeg.");
|
||||
}
|
||||
|
||||
if(got_packet)
|
||||
{
|
||||
m_packet->flags |= AV_PKT_FLAG_KEY;
|
||||
m_packet->stream_index = m_stream->index;
|
||||
if(av_write_frame(m_formatCtx, m_packet) < 0)
|
||||
{
|
||||
av_free_packet(m_packet);
|
||||
AUD_THROW(FileException, "Frame couldn't be writen to the file with ffmpeg.");
|
||||
}
|
||||
av_free_packet(m_packet);
|
||||
}
|
||||
#else
|
||||
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)
|
||||
{
|
||||
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.");
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void FFMPEGWriter::close()
|
||||
{
|
||||
#ifdef FFMPEG_OLD_CODE
|
||||
int got_packet = true;
|
||||
|
||||
while(got_packet)
|
||||
{
|
||||
m_packet->data = nullptr;
|
||||
m_packet->size = 0;
|
||||
|
||||
av_init_packet(m_packet);
|
||||
|
||||
if(avcodec_encode_audio2(m_codecCtx, m_packet, nullptr, &got_packet))
|
||||
AUD_THROW(FileException, "File end couldn't be written, audio encoding failed with ffmpeg.");
|
||||
|
||||
if(got_packet)
|
||||
{
|
||||
m_packet->flags |= AV_PKT_FLAG_KEY;
|
||||
m_packet->stream_index = m_stream->index;
|
||||
if(av_write_frame(m_formatCtx, m_packet))
|
||||
{
|
||||
av_free_packet(m_packet);
|
||||
AUD_THROW(FileException, "Final frames couldn't be writen to the file with ffmpeg.");
|
||||
}
|
||||
av_free_packet(m_packet);
|
||||
}
|
||||
}
|
||||
#else
|
||||
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)
|
||||
{
|
||||
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.");
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
FFMPEGWriter::FFMPEGWriter(const std::string &filename, DeviceSpecs specs, Container format, Codec codec, unsigned int bitrate) :
|
||||
m_position(0),
|
||||
m_specs(specs),
|
||||
m_formatCtx(nullptr),
|
||||
m_codecCtx(nullptr),
|
||||
m_stream(nullptr),
|
||||
m_packet(nullptr),
|
||||
m_frame(nullptr),
|
||||
m_input_samples(0),
|
||||
m_deinterleave(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.");
|
||||
|
||||
const AVOutputFormat* outputFmt = m_formatCtx->oformat;
|
||||
|
||||
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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
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.");
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
if(audio_codec == AV_CODEC_ID_NONE)
|
||||
AUD_THROW(FileException, "File couldn't be written, audio codec not found with ffmpeg.");
|
||||
|
||||
const AVCodec* codec = avcodec_find_encoder(audio_codec);
|
||||
if(!codec)
|
||||
AUD_THROW(FileException, "File couldn't be written, audio encoder couldn't be found with ffmpeg.");
|
||||
|
||||
m_stream = avformat_new_stream(m_formatCtx, codec);
|
||||
if(!m_stream)
|
||||
AUD_THROW(FileException, "File couldn't be written, stream creation failed with ffmpeg.");
|
||||
|
||||
m_stream->id = m_formatCtx->nb_streams - 1;
|
||||
|
||||
#ifdef FFMPEG_OLD_CODE
|
||||
m_codecCtx = m_stream->codec;
|
||||
#else
|
||||
m_codecCtx = avcodec_alloc_context3(codec);
|
||||
#endif
|
||||
|
||||
if(!m_codecCtx)
|
||||
AUD_THROW(FileException, "File couldn't be written, context creation failed with ffmpeg.");
|
||||
|
||||
switch(m_specs.format)
|
||||
{
|
||||
case FORMAT_U8:
|
||||
m_convert = convert_float_u8;
|
||||
m_codecCtx->sample_fmt = AV_SAMPLE_FMT_U8;
|
||||
break;
|
||||
case FORMAT_S16:
|
||||
m_convert = convert_float_s16;
|
||||
m_codecCtx->sample_fmt = AV_SAMPLE_FMT_S16;
|
||||
break;
|
||||
case FORMAT_S32:
|
||||
m_convert = convert_float_s32;
|
||||
m_codecCtx->sample_fmt = AV_SAMPLE_FMT_S32;
|
||||
break;
|
||||
case FORMAT_FLOAT64:
|
||||
m_convert = convert_float_double;
|
||||
m_codecCtx->sample_fmt = AV_SAMPLE_FMT_DBL;
|
||||
break;
|
||||
default:
|
||||
m_convert = convert_copy<sample_t>;
|
||||
m_codecCtx->sample_fmt = AV_SAMPLE_FMT_FLT;
|
||||
break;
|
||||
}
|
||||
|
||||
if(m_formatCtx->oformat->flags & AVFMT_GLOBALHEADER)
|
||||
m_codecCtx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
|
||||
|
||||
bool format_supported = false;
|
||||
|
||||
for(int i = 0; codec->sample_fmts[i] != -1; i++)
|
||||
{
|
||||
if(av_get_alt_sample_fmt(codec->sample_fmts[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];
|
||||
format_supported = true;
|
||||
}
|
||||
}
|
||||
|
||||
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 fmt = av_get_alt_sample_fmt(codec->sample_fmts[i], false);
|
||||
if((fmt > chosen && chosen < m_codecCtx->sample_fmt) || (fmt > m_codecCtx->sample_fmt && fmt < chosen))
|
||||
{
|
||||
chosen = fmt;
|
||||
chosen_index = i;
|
||||
}
|
||||
}
|
||||
|
||||
m_codecCtx->sample_fmt = codec->sample_fmts[chosen_index];
|
||||
m_deinterleave = av_sample_fmt_is_planar(m_codecCtx->sample_fmt);
|
||||
switch(av_get_alt_sample_fmt(m_codecCtx->sample_fmt, false))
|
||||
{
|
||||
case AV_SAMPLE_FMT_U8:
|
||||
specs.format = FORMAT_U8;
|
||||
m_convert = convert_float_u8;
|
||||
break;
|
||||
case AV_SAMPLE_FMT_S16:
|
||||
specs.format = FORMAT_S16;
|
||||
m_convert = convert_float_s16;
|
||||
break;
|
||||
case AV_SAMPLE_FMT_S32:
|
||||
specs.format = FORMAT_S32;
|
||||
m_convert = convert_float_s32;
|
||||
break;
|
||||
case AV_SAMPLE_FMT_FLT:
|
||||
specs.format = FORMAT_FLOAT32;
|
||||
m_convert = convert_copy<sample_t>;
|
||||
break;
|
||||
case AV_SAMPLE_FMT_DBL:
|
||||
specs.format = FORMAT_FLOAT64;
|
||||
m_convert = convert_float_double;
|
||||
break;
|
||||
default:
|
||||
AUD_THROW(FileException, "File couldn't be written, sample format not supported with ffmpeg.");
|
||||
}
|
||||
}
|
||||
|
||||
m_codecCtx->sample_rate = 0;
|
||||
|
||||
if(codec->supported_samplerates)
|
||||
{
|
||||
for(int i = 0; codec->supported_samplerates[i]; i++)
|
||||
{
|
||||
if(codec->supported_samplerates[i] == m_specs.rate)
|
||||
{
|
||||
m_codecCtx->sample_rate = codec->supported_samplerates[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]))
|
||||
{
|
||||
m_codecCtx->sample_rate = codec->supported_samplerates[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(m_codecCtx->sample_rate == 0)
|
||||
m_codecCtx->sample_rate = m_specs.rate;
|
||||
|
||||
m_specs.rate = m_codecCtx->sample_rate;
|
||||
|
||||
#ifdef FFMPEG_OLD_CODE
|
||||
m_codecCtx->codec_id = audio_codec;
|
||||
#endif
|
||||
|
||||
m_codecCtx->codec_type = AVMEDIA_TYPE_AUDIO;
|
||||
m_codecCtx->bit_rate = bitrate;
|
||||
#ifdef FFMPEG_OLD_CH_LAYOUT
|
||||
m_codecCtx->channel_layout = channel_layout;
|
||||
m_codecCtx->channels = m_specs.channels;
|
||||
#else
|
||||
av_channel_layout_uninit(&m_codecCtx->ch_layout);
|
||||
av_channel_layout_from_mask(&m_codecCtx->ch_layout, channel_layout);
|
||||
#endif
|
||||
m_stream->time_base.num = m_codecCtx->time_base.num = 1;
|
||||
m_stream->time_base.den = m_codecCtx->time_base.den = m_codecCtx->sample_rate;
|
||||
|
||||
if(avcodec_open2(m_codecCtx, codec, nullptr) < 0)
|
||||
AUD_THROW(FileException, "File couldn't be written, encoder couldn't be opened with ffmpeg.");
|
||||
|
||||
#ifndef FFMPEG_OLD_CODE
|
||||
if(avcodec_parameters_from_context(m_stream->codecpar, m_codecCtx) < 0)
|
||||
AUD_THROW(FileException, "File couldn't be written, codec parameters couldn't be copied to the context.");
|
||||
#endif
|
||||
|
||||
int samplesize = std::max(int(AUD_SAMPLE_SIZE(m_specs)), AUD_DEVICE_SAMPLE_SIZE(m_specs));
|
||||
|
||||
if((m_input_size = m_codecCtx->frame_size))
|
||||
m_input_buffer.resize(m_input_size * samplesize);
|
||||
|
||||
if(avio_open(&m_formatCtx->pb, filename.c_str(), AVIO_FLAG_WRITE))
|
||||
AUD_THROW(FileException, "File couldn't be written, file opening failed with ffmpeg.");
|
||||
|
||||
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);
|
||||
#endif
|
||||
avformat_free_context(m_formatCtx);
|
||||
throw;
|
||||
}
|
||||
|
||||
#ifdef FFMPEG_OLD_CODE
|
||||
m_packet = new AVPacket({});
|
||||
#else
|
||||
m_packet = av_packet_alloc();
|
||||
#endif
|
||||
|
||||
m_frame = av_frame_alloc();
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
#ifdef FFMPEG_OLD_CODE
|
||||
delete m_packet;
|
||||
#else
|
||||
av_packet_free(&m_packet);
|
||||
#endif
|
||||
}
|
||||
|
||||
#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);
|
||||
}
|
||||
|
||||
int FFMPEGWriter::getPosition() const
|
||||
{
|
||||
return m_position;
|
||||
}
|
||||
|
||||
DeviceSpecs FFMPEGWriter::getSpecs() const
|
||||
{
|
||||
return m_specs;
|
||||
}
|
||||
|
||||
void FFMPEGWriter::write(unsigned int length, sample_t* buffer)
|
||||
{
|
||||
unsigned int samplesize = AUD_SAMPLE_SIZE(m_specs);
|
||||
|
||||
if(m_input_size)
|
||||
{
|
||||
sample_t* inbuf = m_input_buffer.getBuffer();
|
||||
|
||||
while(length)
|
||||
{
|
||||
unsigned int len = std::min(m_input_size - m_input_samples, length);
|
||||
|
||||
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();
|
||||
m_convert(reinterpret_cast<data_t*>(buf), reinterpret_cast<data_t*>(buffer), length * m_specs.channels);
|
||||
|
||||
m_input_samples = length;
|
||||
|
||||
m_position += length;
|
||||
|
||||
encode();
|
||||
}
|
||||
}
|
||||
|
||||
AUD_NAMESPACE_END
|
||||
150
blender-5.2.0/extern/audaspace/plugins/ffmpeg/FFMPEGWriter.h
vendored
Normal file
150
blender-5.2.0/extern/audaspace/plugins/ffmpeg/FFMPEGWriter.h
vendored
Normal file
@@ -0,0 +1,150 @@
|
||||
/*******************************************************************************
|
||||
* Copyright 2009-2024 Jörg Müller
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
******************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifdef FFMPEG_PLUGIN
|
||||
#define AUD_BUILD_PLUGIN
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @file FFMPEGWriter.h
|
||||
* @ingroup plugin
|
||||
* The FFMPEGWriter class.
|
||||
*/
|
||||
|
||||
#include "respec/ConverterFunctions.h"
|
||||
#include "util/Buffer.h"
|
||||
#include "file/IWriter.h"
|
||||
|
||||
#include <string>
|
||||
|
||||
struct AVCodecContext;
|
||||
extern "C" {
|
||||
#include <libavformat/avformat.h>
|
||||
}
|
||||
|
||||
AUD_NAMESPACE_BEGIN
|
||||
|
||||
/**
|
||||
* This class writes a sound file via ffmpeg.
|
||||
*/
|
||||
class AUD_PLUGIN_API FFMPEGWriter : public IWriter
|
||||
{
|
||||
private:
|
||||
/**
|
||||
* The current position in samples.
|
||||
*/
|
||||
int m_position;
|
||||
|
||||
/**
|
||||
* The specification of the audio data.
|
||||
*/
|
||||
DeviceSpecs m_specs;
|
||||
|
||||
/**
|
||||
* The AVFormatContext structure for using ffmpeg.
|
||||
*/
|
||||
AVFormatContext* m_formatCtx;
|
||||
|
||||
/**
|
||||
* The AVCodecContext structure for using ffmpeg.
|
||||
*/
|
||||
AVCodecContext* m_codecCtx;
|
||||
|
||||
/**
|
||||
* The AVStream structure for using ffmpeg.
|
||||
*/
|
||||
AVStream* m_stream;
|
||||
|
||||
/**
|
||||
* The AVPacket structure for using ffmpeg.
|
||||
*/
|
||||
AVPacket* m_packet;
|
||||
|
||||
/**
|
||||
* The AVFrame structure for using ffmpeg.
|
||||
*/
|
||||
AVFrame* m_frame;
|
||||
|
||||
/**
|
||||
* The input buffer for the format converted data before encoding.
|
||||
*/
|
||||
Buffer m_input_buffer;
|
||||
|
||||
/**
|
||||
* The buffer used for deinterleaving.
|
||||
*/
|
||||
Buffer m_deinterleave_buffer;
|
||||
|
||||
/**
|
||||
* The count of input samples we have so far.
|
||||
*/
|
||||
unsigned int m_input_samples;
|
||||
|
||||
/**
|
||||
* The count of input samples necessary to encode a packet.
|
||||
*/
|
||||
unsigned int m_input_size;
|
||||
|
||||
/**
|
||||
* Whether the ouput has to be deinterleaved before writing.
|
||||
*/
|
||||
bool m_deinterleave;
|
||||
|
||||
/**
|
||||
* Converter function.
|
||||
*/
|
||||
convert_f m_convert;
|
||||
|
||||
// delete copy constructor and operator=
|
||||
FFMPEGWriter(const FFMPEGWriter&) = delete;
|
||||
FFMPEGWriter& operator=(const FFMPEGWriter&) = delete;
|
||||
|
||||
/**
|
||||
* Encodes to the output buffer.
|
||||
*/
|
||||
AUD_LOCAL void encode();
|
||||
|
||||
/**
|
||||
* Finishes writing to the file.
|
||||
*/
|
||||
AUD_LOCAL void close();
|
||||
|
||||
public:
|
||||
/**
|
||||
* Creates a new writer.
|
||||
* \param filename The path to the file to be read.
|
||||
* \param specs The file's audio specification.
|
||||
* \param format The file's container format.
|
||||
* \param codec The codec used for encoding the audio data.
|
||||
* \param bitrate The bitrate for encoding.
|
||||
* \exception Exception Thrown if the file specified does not exist or
|
||||
* cannot be read with ffmpeg.
|
||||
*/
|
||||
FFMPEGWriter(const std::string &filename, DeviceSpecs specs, Container format, Codec codec, unsigned int bitrate);
|
||||
|
||||
/**
|
||||
* Destroys the writer and closes the file.
|
||||
*/
|
||||
virtual ~FFMPEGWriter();
|
||||
|
||||
virtual int getPosition() const;
|
||||
virtual DeviceSpecs getSpecs() const;
|
||||
virtual void write(unsigned int length, sample_t* buffer);
|
||||
};
|
||||
|
||||
AUD_NAMESPACE_END
|
||||
Reference in New Issue
Block a user