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:
authorJoerg Mueller <nexyon@gmail.com>2010-08-16 15:41:07 +0400
committerJoerg Mueller <nexyon@gmail.com>2010-08-16 15:41:07 +0400
commit25fec1592efade86233c0ba71212dc973b618ad1 (patch)
tree8c6eccaf24d7bf4fa72c5cfc1ca86511d2df0c9d /intern/audaspace/ffmpeg
parenta91d538f47171a40463016b91c22d39d694d923a (diff)
parent2b7a774ab0dfd3bc66240b387a586b5122ab2661 (diff)
Audaspace (GSoC): First merging commit
* All audaspace changes from the GSoC branch including the aud Python module * This commit also includes some minor changes in source/gameengine/Ketsji/KX_PythonInit.cpp: - Fixing names of some constants - removing outdated stopDSP() python function - Autoinclusion of bge instead of GameLogic - Fix for some error messages: GameLogic -> bge.logic
Diffstat (limited to 'intern/audaspace/ffmpeg')
-rw-r--r--intern/audaspace/ffmpeg/AUD_FFMPEGFactory.cpp36
-rw-r--r--intern/audaspace/ffmpeg/AUD_FFMPEGFactory.h19
-rw-r--r--intern/audaspace/ffmpeg/AUD_FFMPEGReader.cpp140
-rw-r--r--intern/audaspace/ffmpeg/AUD_FFMPEGReader.h26
4 files changed, 111 insertions, 110 deletions
diff --git a/intern/audaspace/ffmpeg/AUD_FFMPEGFactory.cpp b/intern/audaspace/ffmpeg/AUD_FFMPEGFactory.cpp
index 8e71c97baec..cad64d70790 100644
--- a/intern/audaspace/ffmpeg/AUD_FFMPEGFactory.cpp
+++ b/intern/audaspace/ffmpeg/AUD_FFMPEGFactory.cpp
@@ -24,45 +24,29 @@
*/
// needed for INT64_C
+#ifndef __STDC_CONSTANT_MACROS
#define __STDC_CONSTANT_MACROS
+#endif
#include "AUD_FFMPEGFactory.h"
#include "AUD_FFMPEGReader.h"
#include "AUD_Buffer.h"
-AUD_FFMPEGFactory::AUD_FFMPEGFactory(const char* filename)
+AUD_FFMPEGFactory::AUD_FFMPEGFactory(std::string filename) :
+ m_filename(filename)
{
- if(filename != NULL)
- {
- m_filename = new char[strlen(filename)+1]; AUD_NEW("string")
- strcpy(m_filename, filename);
- }
- else
- m_filename = NULL;
}
-AUD_FFMPEGFactory::AUD_FFMPEGFactory(unsigned char* buffer, int size)
+AUD_FFMPEGFactory::AUD_FFMPEGFactory(const data_t* buffer, int size) :
+ m_buffer(new AUD_Buffer(size))
{
- m_filename = NULL;
- m_buffer = AUD_Reference<AUD_Buffer>(new AUD_Buffer(size));
memcpy(m_buffer.get()->getBuffer(), buffer, size);
}
-AUD_FFMPEGFactory::~AUD_FFMPEGFactory()
-{
- if(m_filename)
- {
- delete[] m_filename; AUD_DELETE("string")
- }
-}
-
-AUD_IReader* AUD_FFMPEGFactory::createReader()
+AUD_IReader* AUD_FFMPEGFactory::createReader() const
{
- AUD_IReader* reader;
- if(m_filename)
- reader = new AUD_FFMPEGReader(m_filename);
+ if(m_buffer.get())
+ return new AUD_FFMPEGReader(m_buffer);
else
- reader = new AUD_FFMPEGReader(m_buffer);
- AUD_NEW("reader")
- return reader;
+ return new AUD_FFMPEGReader(m_filename);
}
diff --git a/intern/audaspace/ffmpeg/AUD_FFMPEGFactory.h b/intern/audaspace/ffmpeg/AUD_FFMPEGFactory.h
index 22560303a73..43a6ce68ca7 100644
--- a/intern/audaspace/ffmpeg/AUD_FFMPEGFactory.h
+++ b/intern/audaspace/ffmpeg/AUD_FFMPEGFactory.h
@@ -30,6 +30,8 @@
#include "AUD_Reference.h"
class AUD_Buffer;
+#include <string>
+
/**
* This factory reads a sound file via ffmpeg.
* \warning Notice that the needed formats and codecs have to be registered
@@ -41,33 +43,32 @@ private:
/**
* The filename of the sound source file.
*/
- char* m_filename;
+ const std::string m_filename;
/**
* The buffer to read from.
*/
AUD_Reference<AUD_Buffer> m_buffer;
+ // hide copy constructor and operator=
+ AUD_FFMPEGFactory(const AUD_FFMPEGFactory&);
+ AUD_FFMPEGFactory& operator=(const AUD_FFMPEGFactory&);
+
public:
/**
* Creates a new factory.
* \param filename The sound file path.
*/
- AUD_FFMPEGFactory(const char* filename);
+ AUD_FFMPEGFactory(std::string filename);
/**
* Creates a new factory.
* \param buffer The buffer to read from.
* \param size The size of the buffer.
*/
- AUD_FFMPEGFactory(unsigned char* buffer, int size);
-
- /**
- * Destroys the factory.
- */
- ~AUD_FFMPEGFactory();
+ AUD_FFMPEGFactory(const data_t* buffer, int size);
- virtual AUD_IReader* createReader();
+ virtual AUD_IReader* createReader() const;
};
#endif //AUD_FFMPEGFACTORY
diff --git a/intern/audaspace/ffmpeg/AUD_FFMPEGReader.cpp b/intern/audaspace/ffmpeg/AUD_FFMPEGReader.cpp
index 313ea52e892..b9d5d304368 100644
--- a/intern/audaspace/ffmpeg/AUD_FFMPEGReader.cpp
+++ b/intern/audaspace/ffmpeg/AUD_FFMPEGReader.cpp
@@ -24,23 +24,24 @@
*/
// needed for INT64_C
+#ifndef __STDC_CONSTANT_MACROS
#define __STDC_CONSTANT_MACROS
+#endif
#include "AUD_FFMPEGReader.h"
-#include "AUD_Buffer.h"
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
}
-int AUD_FFMPEGReader::decode(AVPacket* packet, AUD_Buffer* buffer)
+int AUD_FFMPEGReader::decode(AVPacket* packet, AUD_Buffer& buffer)
{
// save packet parameters
uint8_t *audio_pkg_data = packet->data;
int audio_pkg_size = packet->size;
- int buf_size = buffer->getSize();
+ int buf_size = buffer.getSize();
int buf_pos = 0;
int read_length, data_size;
@@ -51,21 +52,16 @@ int AUD_FFMPEGReader::decode(AVPacket* packet, AUD_Buffer* buffer)
// resize buffer if needed
if(buf_size - buf_pos < AVCODEC_MAX_AUDIO_FRAME_SIZE)
{
- buffer->resize(buf_size + AVCODEC_MAX_AUDIO_FRAME_SIZE, true);
+ buffer.resize(buf_size + AVCODEC_MAX_AUDIO_FRAME_SIZE, true);
buf_size += AVCODEC_MAX_AUDIO_FRAME_SIZE;
}
// read samples from the packet
data_size = buf_size - buf_pos;
- /*read_length = avcodec_decode_audio3(m_codecCtx,
- (int16_t*)(((data_t*)buffer->getBuffer())+buf_pos),
+ read_length = avcodec_decode_audio3(m_codecCtx,
+ (int16_t*)(((data_t*)buffer.getBuffer())+buf_pos),
&data_size,
- packet);*/
- read_length = avcodec_decode_audio2(m_codecCtx,
- (int16_t*)(((data_t*)buffer->getBuffer())+buf_pos),
- &data_size,
- audio_pkg_data,
- audio_pkg_size);
+ packet);
// read error, next packet!
if(read_length < 0)
@@ -81,36 +77,50 @@ int AUD_FFMPEGReader::decode(AVPacket* packet, AUD_Buffer* buffer)
return buf_pos;
}
+static const char* streaminfo_error = "AUD_FFMPEGReader: Stream info couldn't "
+ "be found.";
+static const char* noaudio_error = "AUD_FFMPEGReader: File doesn't include an "
+ "audio stream.";
+static const char* nodecoder_error = "AUD_FFMPEGReader: No decoder found for "
+ "the audio stream.";
+static const char* codecopen_error = "AUD_FFMPEGReader: Codec couldn't be "
+ "opened.";
+static const char* format_error = "AUD_FFMPEGReader: Unsupported sample "
+ "format.";
+
void AUD_FFMPEGReader::init()
{
m_position = 0;
m_pkgbuf_left = 0;
if(av_find_stream_info(m_formatCtx)<0)
- AUD_THROW(AUD_ERROR_FFMPEG);
+ AUD_THROW(AUD_ERROR_FFMPEG, streaminfo_error);
// 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 == CODEC_TYPE_AUDIO)
&& (m_stream < 0))
{
m_stream=i;
break;
}
+ }
+
if(m_stream == -1)
- AUD_THROW(AUD_ERROR_FFMPEG);
+ AUD_THROW(AUD_ERROR_FFMPEG, noaudio_error);
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(AUD_ERROR_FFMPEG);
+ AUD_THROW(AUD_ERROR_FFMPEG, nodecoder_error);
if(avcodec_open(m_codecCtx, aCodec)<0)
- AUD_THROW(AUD_ERROR_FFMPEG);
+ AUD_THROW(AUD_ERROR_FFMPEG, codecopen_error);
// XXX this prints file information to stdout:
//dump_format(m_formatCtx, 0, NULL, 0);
@@ -140,45 +150,49 @@ void AUD_FFMPEGReader::init()
m_specs.format = AUD_FORMAT_FLOAT64;
break;
default:
- AUD_THROW(AUD_ERROR_FILE);
+ AUD_THROW(AUD_ERROR_FFMPEG, format_error);
}
m_specs.rate = (AUD_SampleRate) m_codecCtx->sample_rate;
-
- // last but not least if there hasn't been any error, create the buffers
- m_buffer = new AUD_Buffer(); AUD_NEW("buffer")
- m_pkgbuf = new AUD_Buffer(AVCODEC_MAX_AUDIO_FRAME_SIZE<<1);
- AUD_NEW("buffer")
}
-AUD_FFMPEGReader::AUD_FFMPEGReader(const char* filename)
-{
- m_byteiocontext = NULL;
+static const char* fileopen_error = "AUD_FFMPEGReader: File couldn't be "
+ "opened.";
+AUD_FFMPEGReader::AUD_FFMPEGReader(std::string filename) :
+ m_pkgbuf(AVCODEC_MAX_AUDIO_FRAME_SIZE<<1),
+ m_byteiocontext(NULL)
+{
// open file
- if(av_open_input_file(&m_formatCtx, filename, NULL, 0, NULL)!=0)
- AUD_THROW(AUD_ERROR_FILE);
+ if(av_open_input_file(&m_formatCtx, filename.c_str(), NULL, 0, NULL)!=0)
+ AUD_THROW(AUD_ERROR_FILE, fileopen_error);
try
{
init();
}
- catch(AUD_Exception)
+ catch(AUD_Exception&)
{
av_close_input_file(m_formatCtx);
throw;
}
}
-AUD_FFMPEGReader::AUD_FFMPEGReader(AUD_Reference<AUD_Buffer> buffer)
+static const char* streamopen_error = "AUD_FFMPEGReader: Stream couldn't be "
+ "opened.";
+
+AUD_FFMPEGReader::AUD_FFMPEGReader(AUD_Reference<AUD_Buffer> buffer) :
+ m_pkgbuf(AVCODEC_MAX_AUDIO_FRAME_SIZE<<1),
+ m_membuffer(buffer)
{
m_byteiocontext = (ByteIOContext*)av_mallocz(sizeof(ByteIOContext));
- AUD_NEW("byteiocontext")
- m_membuffer = buffer;
if(init_put_byte(m_byteiocontext, (data_t*)buffer.get()->getBuffer(),
buffer.get()->getSize(), 0, NULL, NULL, NULL, NULL) != 0)
- AUD_THROW(AUD_ERROR_FILE);
+ {
+ av_free(m_byteiocontext);
+ AUD_THROW(AUD_ERROR_FILE, fileopen_error);
+ }
AVProbeData probe_data;
probe_data.filename = "";
@@ -188,16 +202,19 @@ AUD_FFMPEGReader::AUD_FFMPEGReader(AUD_Reference<AUD_Buffer> buffer)
// open stream
if(av_open_input_stream(&m_formatCtx, m_byteiocontext, "", fmt, NULL)!=0)
- AUD_THROW(AUD_ERROR_FILE);
+ {
+ av_free(m_byteiocontext);
+ AUD_THROW(AUD_ERROR_FILE, streamopen_error);
+ }
try
{
init();
}
- catch(AUD_Exception)
+ catch(AUD_Exception&)
{
av_close_input_stream(m_formatCtx);
- av_free(m_byteiocontext); AUD_DELETE("byteiocontext")
+ av_free(m_byteiocontext);
throw;
}
}
@@ -209,16 +226,13 @@ AUD_FFMPEGReader::~AUD_FFMPEGReader()
if(m_byteiocontext)
{
av_close_input_stream(m_formatCtx);
- av_free(m_byteiocontext); AUD_DELETE("byteiocontext")
+ av_free(m_byteiocontext);
}
else
av_close_input_file(m_formatCtx);
-
- delete m_buffer; AUD_DELETE("buffer")
- delete m_pkgbuf; AUD_DELETE("buffer")
}
-bool AUD_FFMPEGReader::isSeekable()
+bool AUD_FFMPEGReader::isSeekable() const
{
return true;
}
@@ -260,9 +274,17 @@ void AUD_FFMPEGReader::seek(int position)
if(m_position < position)
{
- sample_t* buf;
- int length = position - m_position;
- read(length, buf);
+ // read until we're at the right position
+ int length = AUD_DEFAULT_BUFFER_SIZE;
+ sample_t* buffer;
+ for(int len = position - m_position;
+ length == AUD_DEFAULT_BUFFER_SIZE;
+ len -= AUD_DEFAULT_BUFFER_SIZE)
+ {
+ if(len < AUD_DEFAULT_BUFFER_SIZE)
+ length = len;
+ read(length, buffer);
+ }
}
}
}
@@ -276,33 +298,23 @@ void AUD_FFMPEGReader::seek(int position)
}
}
-int AUD_FFMPEGReader::getLength()
+int AUD_FFMPEGReader::getLength() const
{
// return approximated remaning size
return (int)((m_formatCtx->duration * m_codecCtx->sample_rate)
/ AV_TIME_BASE)-m_position;
}
-int AUD_FFMPEGReader::getPosition()
+int AUD_FFMPEGReader::getPosition() const
{
return m_position;
}
-AUD_Specs AUD_FFMPEGReader::getSpecs()
+AUD_Specs AUD_FFMPEGReader::getSpecs() const
{
return m_specs.specs;
}
-AUD_ReaderType AUD_FFMPEGReader::getType()
-{
- return AUD_TYPE_STREAM;
-}
-
-bool AUD_FFMPEGReader::notify(AUD_Message &message)
-{
- return false;
-}
-
void AUD_FFMPEGReader::read(int & length, sample_t* & buffer)
{
// read packages and decode them
@@ -313,10 +325,10 @@ void AUD_FFMPEGReader::read(int & length, sample_t* & buffer)
int sample_size = AUD_DEVICE_SAMPLE_SIZE(m_specs);
// resize output buffer if necessary
- if(m_buffer->getSize() < length * AUD_SAMPLE_SIZE(m_specs))
- m_buffer->resize(length * AUD_SAMPLE_SIZE(m_specs));
+ if(m_buffer.getSize() < length * AUD_SAMPLE_SIZE(m_specs))
+ m_buffer.resize(length * AUD_SAMPLE_SIZE(m_specs));
- buffer = m_buffer->getBuffer();
+ buffer = m_buffer.getBuffer();
pkgbuf_pos = m_pkgbuf_left;
m_pkgbuf_left = 0;
@@ -324,7 +336,7 @@ void AUD_FFMPEGReader::read(int & length, sample_t* & buffer)
if(pkgbuf_pos > 0)
{
data_size = AUD_MIN(pkgbuf_pos, left * sample_size);
- m_convert((data_t*) buffer, (data_t*) m_pkgbuf->getBuffer(),
+ m_convert((data_t*) buffer, (data_t*) m_pkgbuf.getBuffer(),
data_size / AUD_FORMAT_SIZE(m_specs.format));
buffer += data_size / AUD_FORMAT_SIZE(m_specs.format);
left -= data_size/sample_size;
@@ -341,7 +353,7 @@ void AUD_FFMPEGReader::read(int & length, sample_t* & buffer)
// copy to output buffer
data_size = AUD_MIN(pkgbuf_pos, left * sample_size);
- m_convert((data_t*) buffer, (data_t*) m_pkgbuf->getBuffer(),
+ m_convert((data_t*) buffer, (data_t*) m_pkgbuf.getBuffer(),
data_size / AUD_FORMAT_SIZE(m_specs.format));
buffer += data_size / AUD_FORMAT_SIZE(m_specs.format);
left -= data_size/sample_size;
@@ -352,12 +364,12 @@ void AUD_FFMPEGReader::read(int & length, sample_t* & buffer)
if(pkgbuf_pos > data_size)
{
m_pkgbuf_left = pkgbuf_pos-data_size;
- memmove(m_pkgbuf->getBuffer(),
- ((data_t*)m_pkgbuf->getBuffer())+data_size,
+ memmove(m_pkgbuf.getBuffer(),
+ ((data_t*)m_pkgbuf.getBuffer())+data_size,
pkgbuf_pos-data_size);
}
- buffer = m_buffer->getBuffer();
+ buffer = m_buffer.getBuffer();
if(left > 0)
length -= left;
diff --git a/intern/audaspace/ffmpeg/AUD_FFMPEGReader.h b/intern/audaspace/ffmpeg/AUD_FFMPEGReader.h
index f992fdf7a34..8ebf5b45cdc 100644
--- a/intern/audaspace/ffmpeg/AUD_FFMPEGReader.h
+++ b/intern/audaspace/ffmpeg/AUD_FFMPEGReader.h
@@ -29,7 +29,9 @@
#include "AUD_ConverterFunctions.h"
#include "AUD_IReader.h"
#include "AUD_Reference.h"
-class AUD_Buffer;
+#include "AUD_Buffer.h"
+
+#include <string>
struct AVCodecContext;
extern "C" {
@@ -55,7 +57,7 @@ private:
/**
* The playback buffer.
*/
- AUD_Buffer *m_buffer;
+ AUD_Buffer m_buffer;
/**
* The specification of the audio data.
@@ -65,7 +67,7 @@ private:
/**
* The buffer for package reading.
*/
- AUD_Buffer *m_pkgbuf;
+ AUD_Buffer m_pkgbuf;
/**
* The count of samples still available from the last read package.
@@ -108,13 +110,17 @@ private:
* \param buffer The target buffer.
* \return The count of read bytes.
*/
- int decode(AVPacket* packet, AUD_Buffer* buffer);
+ int decode(AVPacket* packet, AUD_Buffer& buffer);
/**
* Initializes the object.
*/
void init();
+ // hide copy constructor and operator=
+ AUD_FFMPEGReader(const AUD_FFMPEGReader&);
+ AUD_FFMPEGReader& operator=(const AUD_FFMPEGReader&);
+
public:
/**
* Creates a new reader.
@@ -122,7 +128,7 @@ public:
* \exception AUD_Exception Thrown if the file specified does not exist or
* cannot be read with ffmpeg.
*/
- AUD_FFMPEGReader(const char* filename);
+ AUD_FFMPEGReader(std::string filename);
/**
* Creates a new reader.
@@ -137,13 +143,11 @@ public:
*/
virtual ~AUD_FFMPEGReader();
- virtual bool isSeekable();
+ virtual bool isSeekable() const;
virtual void seek(int position);
- virtual int getLength();
- virtual int getPosition();
- virtual AUD_Specs getSpecs();
- virtual AUD_ReaderType getType();
- virtual bool notify(AUD_Message &message);
+ virtual int getLength() const;
+ virtual int getPosition() const;
+ virtual AUD_Specs getSpecs() const;
virtual void read(int & length, sample_t* & buffer);
};