Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDalai Felinto <dfelinto@gmail.com>2018-02-01 14:11:04 +0300
committerDalai Felinto <dfelinto@gmail.com>2018-02-01 14:22:57 +0300
commit781dd5edb5b74ecb417a16c28eab31483fc3d11a (patch)
treee5c142f2eef1cbcb1ed6dc243ec9707fe64f8d5f /extern/audaspace/plugins
parentcd317fab65e1b6c1652ba7a58786a8cc5bd11843 (diff)
Fix audaspace mess in 2.8
All these files were removed since accidental commit, revert and merge in 2.8. ea31f0ac3b877e + 0a4e170c28cec + 11f9a23a286c17f + 7b27b10fa6a6
Diffstat (limited to 'extern/audaspace/plugins')
-rw-r--r--extern/audaspace/plugins/ffmpeg/FFMPEG.cpp63
-rw-r--r--extern/audaspace/plugins/ffmpeg/FFMPEG.h60
-rw-r--r--extern/audaspace/plugins/ffmpeg/FFMPEGReader.cpp397
-rw-r--r--extern/audaspace/plugins/ffmpeg/FFMPEGReader.h184
-rw-r--r--extern/audaspace/plugins/ffmpeg/FFMPEGWriter.cpp427
-rw-r--r--extern/audaspace/plugins/ffmpeg/FFMPEGWriter.h145
6 files changed, 1276 insertions, 0 deletions
diff --git a/extern/audaspace/plugins/ffmpeg/FFMPEG.cpp b/extern/audaspace/plugins/ffmpeg/FFMPEG.cpp
new file mode 100644
index 00000000000..7f9b762f816
--- /dev/null
+++ b/extern/audaspace/plugins/ffmpeg/FFMPEG.cpp
@@ -0,0 +1,63 @@
+/*******************************************************************************
+ * Copyright 2009-2016 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()
+{
+ av_register_all();
+}
+
+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(std::string filename)
+{
+ return std::shared_ptr<IReader>(new FFMPEGReader(filename));
+}
+
+std::shared_ptr<IReader> FFMPEG::createReader(std::shared_ptr<Buffer> buffer)
+{
+ return std::shared_ptr<IReader>(new FFMPEGReader(buffer));
+}
+
+std::shared_ptr<IWriter> FFMPEG::createWriter(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
diff --git a/extern/audaspace/plugins/ffmpeg/FFMPEG.h b/extern/audaspace/plugins/ffmpeg/FFMPEG.h
new file mode 100644
index 00000000000..108ba547e0f
--- /dev/null
+++ b/extern/audaspace/plugins/ffmpeg/FFMPEG.h
@@ -0,0 +1,60 @@
+/*******************************************************************************
+ * Copyright 2009-2016 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(std::string filename);
+ virtual std::shared_ptr<IReader> createReader(std::shared_ptr<Buffer> buffer);
+ virtual std::shared_ptr<IWriter> createWriter(std::string filename, DeviceSpecs specs, Container format, Codec codec, unsigned int bitrate);
+};
+
+AUD_NAMESPACE_END
diff --git a/extern/audaspace/plugins/ffmpeg/FFMPEGReader.cpp b/extern/audaspace/plugins/ffmpeg/FFMPEGReader.cpp
new file mode 100644
index 00000000000..6b79cc5abfd
--- /dev/null
+++ b/extern/audaspace/plugins/ffmpeg/FFMPEGReader.cpp
@@ -0,0 +1,397 @@
+/*******************************************************************************
+ * Copyright 2009-2016 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>
+}
+
+AUD_NAMESPACE_BEGIN
+
+int FFMPEGReader::decode(AVPacket& packet, Buffer& buffer)
+{
+ AVFrame* frame = nullptr;
+ int got_frame;
+ int read_length;
+ uint8_t* orig_data = packet.data;
+ int orig_size = packet.size;
+
+ int buf_size = buffer.getSize();
+ int buf_pos = 0;
+
+ while(packet.size > 0)
+ {
+ got_frame = 0;
+
+ if(!frame)
+ frame = av_frame_alloc();
+ else
+ av_frame_unref(frame);
+
+ read_length = avcodec_decode_audio4(m_codecCtx, frame, &got_frame, &packet);
+ if(read_length < 0)
+ break;
+
+ if(got_frame)
+ {
+ int data_size = av_samples_get_buffer_size(nullptr, m_codecCtx->channels, 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 / frame->nb_samples;
+ for(int channel = 0; channel < m_codecCtx->channels; channel++)
+ {
+ for(int i = 0; i < frame->nb_samples; i++)
+ {
+ std::memcpy(((data_t*)buffer.getBuffer()) + buf_pos + ((m_codecCtx->channels * i) + channel) * single_size,
+ frame->data[channel] + i * single_size, single_size);
+ }
+ }
+ }
+ else
+ std::memcpy(((data_t*)buffer.getBuffer()) + buf_pos, 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;
+ av_free(frame);
+
+ return buf_pos;
+}
+
+void FFMPEGReader::init()
+{
+ 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++)
+ {
+ if((m_formatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO)
+ && (m_stream < 0))
+ {
+ m_stream=i;
+ break;
+ }
+ }
+
+ if(m_stream == -1)
+ AUD_THROW(FileException, "File couldn't be read, no audio stream found by ffmpeg.");
+
+ m_codecCtx = m_formatCtx->streams[m_stream]->codec;
+
+ // get a decoder and open it
+ AVCodec* aCodec = avcodec_find_decoder(m_codecCtx->codec_id);
+ if(!aCodec)
+ AUD_THROW(FileException, "File couldn't be read, no decoder found with ffmpeg.");
+
+ if(avcodec_open2(m_codecCtx, aCodec, nullptr) < 0)
+ AUD_THROW(FileException, "File couldn't be read, ffmpeg codec couldn't be opened.");
+
+ m_specs.channels = (Channels) m_codecCtx->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(std::string filename) :
+ m_pkgbuf(),
+ m_formatCtx(nullptr),
+ m_aviocontext(nullptr),
+ m_membuf(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();
+ }
+ catch(Exception&)
+ {
+ avformat_close_input(&m_formatCtx);
+ throw;
+ }
+}
+
+FFMPEGReader::FFMPEGReader(std::shared_ptr<Buffer> buffer) :
+ m_pkgbuf(),
+ m_membuffer(buffer),
+ m_membufferpos(0)
+{
+ m_membuf = reinterpret_cast<data_t*>(av_malloc(FF_MIN_BUFFER_SIZE + FF_INPUT_BUFFER_PADDING_SIZE));
+
+ m_aviocontext = avio_alloc_context(m_membuf, FF_MIN_BUFFER_SIZE, 0, this, read_packet, nullptr, seek_packet);
+
+ if(!m_aviocontext)
+ {
+ av_free(m_aviocontext);
+ 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)
+ {
+ av_free(m_aviocontext);
+ AUD_THROW(FileException, "Buffer couldn't be read with ffmpeg.");
+ }
+
+ try
+ {
+ init();
+ }
+ catch(Exception&)
+ {
+ avformat_close_input(&m_formatCtx);
+ av_free(m_aviocontext);
+ throw;
+ }
+}
+
+FFMPEGReader::~FFMPEGReader()
+{
+ avcodec_close(m_codecCtx);
+ avformat_close_input(&m_formatCtx);
+}
+
+int FFMPEGReader::read_packet(void* opaque, uint8_t* buf, int buf_size)
+{
+ FFMPEGReader* reader = reinterpret_cast<FFMPEGReader*>(opaque);
+
+ int size = std::min(buf_size, int(reader->m_membuffer->getSize() - reader->m_membufferpos));
+
+ if(size < 0)
+ return -1;
+
+ 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();
+ }
+
+ return (reader->m_membufferpos += offset);
+}
+
+bool FFMPEGReader::isSeekable() const
+{
+ return true;
+}
+
+void FFMPEGReader::seek(int position)
+{
+ if(position >= 0)
+ {
+ uint64_t st_time = m_formatCtx->start_time;
+ uint64_t seek_pos = ((uint64_t)position) * ((uint64_t)AV_TIME_BASE) / ((uint64_t)m_specs.rate);
+
+ if(st_time != AV_NOPTS_VALUE) {
+ seek_pos += st_time;
+ }
+
+ double pts_time_base =
+ av_q2d(m_formatCtx->streams[m_stream]->time_base);
+ uint64_t pts_st_time =
+ ((st_time != AV_NOPTS_VALUE) ? st_time : 0)
+ / pts_time_base / (uint64_t) AV_TIME_BASE;
+
+ // a value < 0 tells us that seeking failed
+ if(av_seek_frame(m_formatCtx, -1, 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 - pts_st_time) * 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_free_packet(&packet);
+ }
+ }
+ else
+ {
+ fprintf(stderr, "seeking failed!\n");
+ // Seeking failed, do nothing.
+ }
+ }
+}
+
+int FFMPEGReader::getLength() const
+{
+ // return approximated remaning size
+ return (int)((m_formatCtx->duration * m_codecCtx->sample_rate)
+ / AV_TIME_BASE)-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_free_packet(&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
diff --git a/extern/audaspace/plugins/ffmpeg/FFMPEGReader.h b/extern/audaspace/plugins/ffmpeg/FFMPEGReader.h
new file mode 100644
index 00000000000..e2ae959912d
--- /dev/null
+++ b/extern/audaspace/plugins/ffmpeg/FFMPEGReader.h
@@ -0,0 +1,184 @@
+/*******************************************************************************
+ * Copyright 2009-2016 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 <string>
+#include <memory>
+
+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 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;
+
+ /**
+ * The buffer to read with.
+ */
+ data_t* m_membuf;
+
+ /**
+ * Reading position of the buffer.
+ */
+ int64_t m_membufferpos;
+
+ /**
+ * Whether the audio data has to be interleaved after reading.
+ */
+ bool m_tointerleave;
+
+ /**
+ * 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.
+ */
+ AUD_LOCAL void init();
+
+ // 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.
+ * \exception Exception Thrown if the file specified does not exist or
+ * cannot be read with ffmpeg.
+ */
+ FFMPEGReader(std::string filename);
+
+ /**
+ * Creates a new reader.
+ * \param buffer The buffer to read from.
+ * \exception Exception Thrown if the buffer specified cannot be read
+ * with ffmpeg.
+ */
+ FFMPEGReader(std::shared_ptr<Buffer> buffer);
+
+ /**
+ * Destroys the reader and closes the file.
+ */
+ virtual ~FFMPEGReader();
+
+ /**
+ * 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
diff --git a/extern/audaspace/plugins/ffmpeg/FFMPEGWriter.cpp b/extern/audaspace/plugins/ffmpeg/FFMPEGWriter.cpp
new file mode 100644
index 00000000000..f79f0f7fc6b
--- /dev/null
+++ b/extern/audaspace/plugins/ffmpeg/FFMPEGWriter.cpp
@@ -0,0 +1,427 @@
+/*******************************************************************************
+ * Copyright 2009-2016 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>
+}
+
+AUD_NAMESPACE_BEGIN
+
+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);
+
+ AVPacket packet;
+
+ packet.data = nullptr;
+ packet.size = 0;
+
+ av_init_packet(&packet);
+
+ AVFrame* frame = av_frame_alloc();
+ av_frame_unref(frame);
+ int got_packet;
+
+ frame->nb_samples = m_input_samples;
+ frame->format = m_codecCtx->sample_fmt;
+ frame->channel_layout = m_codecCtx->channel_layout;
+
+ if(avcodec_fill_audio_frame(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) };
+ frame->pts = av_rescale_q(m_position - m_input_samples, m_codecCtx->time_base, sample_time);
+
+ if(avcodec_encode_audio2(m_codecCtx, &packet, frame, &got_packet))
+ {
+ av_frame_free(&frame);
+ AUD_THROW(FileException, "File couldn't be written, audio encoding failed with ffmpeg.");
+ }
+
+ if(got_packet)
+ {
+ packet.flags |= AV_PKT_FLAG_KEY;
+ packet.stream_index = m_stream->index;
+ if(av_write_frame(m_formatCtx, &packet) < 0)
+ {
+ av_free_packet(&packet);
+ av_frame_free(&frame);
+ AUD_THROW(FileException, "Frame couldn't be writen to the file with ffmpeg.");
+ }
+ av_free_packet(&packet);
+ }
+
+ av_frame_free(&frame);
+}
+
+void FFMPEGWriter::close()
+{
+ int got_packet = true;
+
+ while(got_packet)
+ {
+ AVPacket packet;
+
+ packet.data = nullptr;
+ packet.size = 0;
+
+ av_init_packet(&packet);
+
+ if(avcodec_encode_audio2(m_codecCtx, &packet, nullptr, &got_packet))
+ AUD_THROW(FileException, "File end couldn't be written, audio encoding failed with ffmpeg.");
+
+ if(got_packet)
+ {
+ packet.flags |= AV_PKT_FLAG_KEY;
+ packet.stream_index = m_stream->index;
+ if(av_write_frame(m_formatCtx, &packet))
+ {
+ av_free_packet(&packet);
+ AUD_THROW(FileException, "Final frames couldn't be writen to the file with ffmpeg.");
+ }
+ av_free_packet(&packet);
+ }
+ }
+}
+
+FFMPEGWriter::FFMPEGWriter(std::string filename, DeviceSpecs specs, Container format, Codec codec, unsigned int bitrate) :
+ m_position(0),
+ m_specs(specs),
+ m_input_samples(0),
+ m_deinterleave(false)
+{
+ static const char* formats[] = { nullptr, "ac3", "flac", "matroska", "mp2", "mp3", "ogg", "wav" };
+
+ 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.");
+
+ m_outputFmt = m_formatCtx->oformat;
+
+ if(!m_outputFmt) {
+ avformat_free_context(m_formatCtx);
+ AUD_THROW(FileException, "File couldn't be written, output format couldn't be found with ffmpeg.");
+ }
+
+ m_outputFmt->audio_codec = AV_CODEC_ID_NONE;
+
+ switch(codec)
+ {
+ case CODEC_AAC:
+ m_outputFmt->audio_codec = AV_CODEC_ID_AAC;
+ break;
+ case CODEC_AC3:
+ m_outputFmt->audio_codec = AV_CODEC_ID_AC3;
+ break;
+ case CODEC_FLAC:
+ m_outputFmt->audio_codec = AV_CODEC_ID_FLAC;
+ break;
+ case CODEC_MP2:
+ m_outputFmt->audio_codec = AV_CODEC_ID_MP2;
+ break;
+ case CODEC_MP3:
+ m_outputFmt->audio_codec = AV_CODEC_ID_MP3;
+ break;
+ case CODEC_OPUS:
+ m_outputFmt->audio_codec = AV_CODEC_ID_OPUS;
+ break;
+ case CODEC_PCM:
+ switch(specs.format)
+ {
+ case FORMAT_U8:
+ m_outputFmt->audio_codec = AV_CODEC_ID_PCM_U8;
+ break;
+ case FORMAT_S16:
+ m_outputFmt->audio_codec = AV_CODEC_ID_PCM_S16LE;
+ break;
+ case FORMAT_S24:
+ m_outputFmt->audio_codec = AV_CODEC_ID_PCM_S24LE;
+ break;
+ case FORMAT_S32:
+ m_outputFmt->audio_codec = AV_CODEC_ID_PCM_S32LE;
+ break;
+ case FORMAT_FLOAT32:
+ m_outputFmt->audio_codec = AV_CODEC_ID_PCM_F32LE;
+ break;
+ case FORMAT_FLOAT64:
+ m_outputFmt->audio_codec = AV_CODEC_ID_PCM_F64LE;
+ break;
+ default:
+ m_outputFmt->audio_codec = AV_CODEC_ID_NONE;
+ break;
+ }
+ break;
+ case CODEC_VORBIS:
+ m_outputFmt->audio_codec = AV_CODEC_ID_VORBIS;
+ break;
+ default:
+ m_outputFmt->audio_codec = AV_CODEC_ID_NONE;
+ break;
+ }
+
+ try
+ {
+ if(m_outputFmt->audio_codec == AV_CODEC_ID_NONE)
+ AUD_THROW(FileException, "File couldn't be written, audio codec not found with ffmpeg.");
+
+ AVCodec* codec = avcodec_find_encoder(m_outputFmt->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;
+
+ m_codecCtx = m_stream->codec;
+
+ 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 |= 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;
+
+ m_codecCtx->codec_id = m_outputFmt->audio_codec;
+ m_codecCtx->codec_type = AVMEDIA_TYPE_AUDIO;
+ m_codecCtx->bit_rate = bitrate;
+ m_codecCtx->channels = m_specs.channels;
+ 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.");
+
+ 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.");
+
+ avformat_write_header(m_formatCtx, nullptr);
+ }
+ catch(Exception&)
+ {
+ avformat_free_context(m_formatCtx);
+ throw;
+ }
+}
+
+FFMPEGWriter::~FFMPEGWriter()
+{
+ // writte missing data
+ if(m_input_samples)
+ encode();
+
+ close();
+
+ av_write_trailer(m_formatCtx);
+
+ avcodec_close(m_codecCtx);
+
+ avio_close(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
diff --git a/extern/audaspace/plugins/ffmpeg/FFMPEGWriter.h b/extern/audaspace/plugins/ffmpeg/FFMPEGWriter.h
new file mode 100644
index 00000000000..690185deb64
--- /dev/null
+++ b/extern/audaspace/plugins/ffmpeg/FFMPEGWriter.h
@@ -0,0 +1,145 @@
+/*******************************************************************************
+ * Copyright 2009-2016 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 AVOutputFormat structure for using ffmpeg.
+ */
+ AVOutputFormat* m_outputFmt;
+
+ /**
+ * The AVStream structure for using ffmpeg.
+ */
+ AVStream* m_stream;
+
+ /**
+ * 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(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