|
|
@ -3,7 +3,7 @@
|
|
|
|
#include <memory>
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <sstream>
|
|
|
|
#include <iostream>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
|
|
|
|
#include <components/vfs/manager.hpp>
|
|
|
|
#include <components/vfs/manager.hpp>
|
|
|
@ -11,11 +11,6 @@
|
|
|
|
namespace MWSound
|
|
|
|
namespace MWSound
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
void FFmpeg_Decoder::fail(const std::string &msg)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
throw std::runtime_error("FFmpeg exception: "+msg);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int FFmpeg_Decoder::readPacket(void *user_data, uint8_t *buf, int buf_size)
|
|
|
|
int FFmpeg_Decoder::readPacket(void *user_data, uint8_t *buf, int buf_size)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
try
|
|
|
|
try
|
|
|
@ -33,7 +28,8 @@ int FFmpeg_Decoder::readPacket(void *user_data, uint8_t *buf, int buf_size)
|
|
|
|
|
|
|
|
|
|
|
|
int FFmpeg_Decoder::writePacket(void *, uint8_t *, int)
|
|
|
|
int FFmpeg_Decoder::writePacket(void *, uint8_t *, int)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
throw std::runtime_error("can't write to read-only stream");
|
|
|
|
std::cerr<< "can't write to read-only stream" <<std::endl;
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int64_t FFmpeg_Decoder::seek(void *user_data, int64_t offset, int whence)
|
|
|
|
int64_t FFmpeg_Decoder::seek(void *user_data, int64_t offset, int whence)
|
|
|
@ -180,13 +176,15 @@ size_t FFmpeg_Decoder::readAVAudioData(void *data, size_t length)
|
|
|
|
return dec;
|
|
|
|
return dec;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void FFmpeg_Decoder::open(const std::string &fname)
|
|
|
|
bool FFmpeg_Decoder::open(const std::string &fname)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
close();
|
|
|
|
close();
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
|
|
{
|
|
|
|
mDataStream = mResourceMgr->get(fname);
|
|
|
|
mDataStream = mResourceMgr->get(fname);
|
|
|
|
|
|
|
|
|
|
|
|
if((mFormatCtx=avformat_alloc_context()) == NULL)
|
|
|
|
if((mFormatCtx=avformat_alloc_context()) == NULL)
|
|
|
|
fail("Failed to allocate context");
|
|
|
|
throw std::runtime_error("Failed to allocate context");
|
|
|
|
|
|
|
|
|
|
|
|
mFormatCtx->pb = avio_alloc_context(NULL, 0, 0, this, readPacket, writePacket, seek);
|
|
|
|
mFormatCtx->pb = avio_alloc_context(NULL, 0, 0, this, readPacket, writePacket, seek);
|
|
|
|
if(!mFormatCtx->pb || avformat_open_input(&mFormatCtx, fname.c_str(), NULL, NULL) != 0)
|
|
|
|
if(!mFormatCtx->pb || avformat_open_input(&mFormatCtx, fname.c_str(), NULL, NULL) != 0)
|
|
|
@ -207,13 +205,11 @@ void FFmpeg_Decoder::open(const std::string &fname)
|
|
|
|
avformat_free_context(mFormatCtx);
|
|
|
|
avformat_free_context(mFormatCtx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mFormatCtx = NULL;
|
|
|
|
mFormatCtx = NULL;
|
|
|
|
fail("Failed to allocate input stream");
|
|
|
|
throw std::runtime_error("Failed to allocate input stream");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if(avformat_find_stream_info(mFormatCtx, NULL) < 0)
|
|
|
|
if(avformat_find_stream_info(mFormatCtx, NULL) < 0)
|
|
|
|
fail("Failed to find stream info in "+fname);
|
|
|
|
throw std::runtime_error("Failed to find stream info in "+fname);
|
|
|
|
|
|
|
|
|
|
|
|
for(size_t j = 0;j < mFormatCtx->nb_streams;j++)
|
|
|
|
for(size_t j = 0;j < mFormatCtx->nb_streams;j++)
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -224,23 +220,45 @@ void FFmpeg_Decoder::open(const std::string &fname)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!mStream)
|
|
|
|
if(!mStream)
|
|
|
|
fail("No audio streams in "+fname);
|
|
|
|
throw std::runtime_error("No audio streams in "+fname);
|
|
|
|
|
|
|
|
|
|
|
|
(*mStream)->codec->request_sample_fmt = (*mStream)->codec->sample_fmt;
|
|
|
|
(*mStream)->codec->request_sample_fmt = (*mStream)->codec->sample_fmt;
|
|
|
|
|
|
|
|
|
|
|
|
AVCodec *codec = avcodec_find_decoder((*mStream)->codec->codec_id);
|
|
|
|
AVCodec *codec = avcodec_find_decoder((*mStream)->codec->codec_id);
|
|
|
|
if(!codec)
|
|
|
|
if(!codec)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
std::stringstream ss("No codec found for id ");
|
|
|
|
std::string ss = "No codec found for id " +
|
|
|
|
ss << (*mStream)->codec->codec_id;
|
|
|
|
std::to_string((*mStream)->codec->codec_id);
|
|
|
|
fail(ss.str());
|
|
|
|
throw std::runtime_error(ss);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(avcodec_open2((*mStream)->codec, codec, NULL) < 0)
|
|
|
|
if(avcodec_open2((*mStream)->codec, codec, NULL) < 0)
|
|
|
|
fail("Failed to open audio codec " + std::string(codec->long_name));
|
|
|
|
throw std::runtime_error(std::string("Failed to open audio codec ") +
|
|
|
|
|
|
|
|
codec->long_name);
|
|
|
|
|
|
|
|
|
|
|
|
mFrame = av_frame_alloc();
|
|
|
|
mFrame = av_frame_alloc();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if((*mStream)->codec->sample_fmt == AV_SAMPLE_FMT_FLT ||
|
|
|
|
|
|
|
|
(*mStream)->codec->sample_fmt == AV_SAMPLE_FMT_FLTP)
|
|
|
|
|
|
|
|
mOutputSampleFormat = AV_SAMPLE_FMT_S16; // FIXME: Check for AL_EXT_FLOAT32 support
|
|
|
|
|
|
|
|
else if((*mStream)->codec->sample_fmt == AV_SAMPLE_FMT_U8P)
|
|
|
|
|
|
|
|
mOutputSampleFormat = AV_SAMPLE_FMT_U8;
|
|
|
|
|
|
|
|
else if((*mStream)->codec->sample_fmt == AV_SAMPLE_FMT_S16P)
|
|
|
|
|
|
|
|
mOutputSampleFormat = AV_SAMPLE_FMT_S16;
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
mOutputSampleFormat = AV_SAMPLE_FMT_S16;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mOutputChannelLayout = (*mStream)->codec->channel_layout;
|
|
|
|
|
|
|
|
if(mOutputChannelLayout == 0)
|
|
|
|
|
|
|
|
mOutputChannelLayout = av_get_default_channel_layout((*mStream)->codec->channels);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
catch(std::exception &e)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
std::cerr<< "Could not open audio file: "<<e.what() <<std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(std::exception&)
|
|
|
|
|
|
|
|
|
|
|
|
if(mFormatCtx)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if(mStream)
|
|
|
|
if(mStream)
|
|
|
|
avcodec_close((*mStream)->codec);
|
|
|
|
avcodec_close((*mStream)->codec);
|
|
|
@ -255,8 +273,8 @@ void FFmpeg_Decoder::open(const std::string &fname)
|
|
|
|
mFormatCtx->pb = NULL;
|
|
|
|
mFormatCtx->pb = NULL;
|
|
|
|
|
|
|
|
|
|
|
|
avformat_close_input(&mFormatCtx);
|
|
|
|
avformat_close_input(&mFormatCtx);
|
|
|
|
throw;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void FFmpeg_Decoder::close()
|
|
|
|
void FFmpeg_Decoder::close()
|
|
|
@ -298,19 +316,13 @@ std::string FFmpeg_Decoder::getName()
|
|
|
|
return mFormatCtx->filename;
|
|
|
|
return mFormatCtx->filename;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void FFmpeg_Decoder::getInfo(int *samplerate, ChannelConfig *chans, SampleType *type)
|
|
|
|
bool FFmpeg_Decoder::getInfo(int *samplerate, ChannelConfig *chans, SampleType *type)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if(!mStream)
|
|
|
|
if(!mStream)
|
|
|
|
fail("No audio stream info");
|
|
|
|
{
|
|
|
|
|
|
|
|
std::cerr<< "No audio stream info" <<std::endl;
|
|
|
|
if((*mStream)->codec->sample_fmt == AV_SAMPLE_FMT_FLT || (*mStream)->codec->sample_fmt == AV_SAMPLE_FMT_FLTP)
|
|
|
|
return false;
|
|
|
|
mOutputSampleFormat = AV_SAMPLE_FMT_S16; // FIXME: Check for AL_EXT_FLOAT32 support
|
|
|
|
}
|
|
|
|
else if((*mStream)->codec->sample_fmt == AV_SAMPLE_FMT_U8P)
|
|
|
|
|
|
|
|
mOutputSampleFormat = AV_SAMPLE_FMT_U8;
|
|
|
|
|
|
|
|
else if((*mStream)->codec->sample_fmt == AV_SAMPLE_FMT_S16P)
|
|
|
|
|
|
|
|
mOutputSampleFormat = AV_SAMPLE_FMT_S16;
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
mOutputSampleFormat = AV_SAMPLE_FMT_S16;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(mOutputSampleFormat == AV_SAMPLE_FMT_U8)
|
|
|
|
if(mOutputSampleFormat == AV_SAMPLE_FMT_U8)
|
|
|
|
*type = SampleType_UInt8;
|
|
|
|
*type = SampleType_UInt8;
|
|
|
@ -319,19 +331,6 @@ void FFmpeg_Decoder::getInfo(int *samplerate, ChannelConfig *chans, SampleType *
|
|
|
|
else if(mOutputSampleFormat == AV_SAMPLE_FMT_FLT)
|
|
|
|
else if(mOutputSampleFormat == AV_SAMPLE_FMT_FLT)
|
|
|
|
*type = SampleType_Float32;
|
|
|
|
*type = SampleType_Float32;
|
|
|
|
|
|
|
|
|
|
|
|
int64_t ch_layout = (*mStream)->codec->channel_layout;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(ch_layout == 0)
|
|
|
|
|
|
|
|
ch_layout = av_get_default_channel_layout((*mStream)->codec->channels);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mOutputChannelLayout = ch_layout;
|
|
|
|
|
|
|
|
if (ch_layout == AV_CH_LAYOUT_5POINT1 || ch_layout == AV_CH_LAYOUT_7POINT1
|
|
|
|
|
|
|
|
|| ch_layout == AV_CH_LAYOUT_QUAD) // FIXME: check for AL_EXT_MCFORMATS support
|
|
|
|
|
|
|
|
mOutputChannelLayout = AV_CH_LAYOUT_STEREO;
|
|
|
|
|
|
|
|
else if (ch_layout != AV_CH_LAYOUT_MONO
|
|
|
|
|
|
|
|
&& ch_layout != AV_CH_LAYOUT_STEREO)
|
|
|
|
|
|
|
|
mOutputChannelLayout = AV_CH_LAYOUT_STEREO;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(mOutputChannelLayout == AV_CH_LAYOUT_MONO)
|
|
|
|
if(mOutputChannelLayout == AV_CH_LAYOUT_MONO)
|
|
|
|
*chans = ChannelConfig_Mono;
|
|
|
|
*chans = ChannelConfig_Mono;
|
|
|
|
else if(mOutputChannelLayout == AV_CH_LAYOUT_STEREO)
|
|
|
|
else if(mOutputChannelLayout == AV_CH_LAYOUT_STEREO)
|
|
|
@ -347,13 +346,27 @@ void FFmpeg_Decoder::getInfo(int *samplerate, ChannelConfig *chans, SampleType *
|
|
|
|
char str[1024];
|
|
|
|
char str[1024];
|
|
|
|
av_get_channel_layout_string(str, sizeof(str), (*mStream)->codec->channels,
|
|
|
|
av_get_channel_layout_string(str, sizeof(str), (*mStream)->codec->channels,
|
|
|
|
(*mStream)->codec->channel_layout);
|
|
|
|
(*mStream)->codec->channel_layout);
|
|
|
|
fail(std::string("Unsupported channel layout: ")+str);
|
|
|
|
std::cerr<< "Unsupported channel layout: "<<str <<std::endl;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if((*mStream)->codec->channels == 1)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
mOutputChannelLayout = AV_CH_LAYOUT_MONO;
|
|
|
|
|
|
|
|
*chans = ChannelConfig_Mono;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
mOutputChannelLayout = AV_CH_LAYOUT_STEREO;
|
|
|
|
|
|
|
|
*chans = ChannelConfig_Stereo;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
*samplerate = (*mStream)->codec->sample_rate;
|
|
|
|
*samplerate = (*mStream)->codec->sample_rate;
|
|
|
|
|
|
|
|
int64_t ch_layout = (*mStream)->codec->channel_layout;
|
|
|
|
|
|
|
|
if(ch_layout == 0)
|
|
|
|
|
|
|
|
ch_layout = av_get_default_channel_layout((*mStream)->codec->channels);
|
|
|
|
|
|
|
|
|
|
|
|
if(mOutputSampleFormat != (*mStream)->codec->sample_fmt
|
|
|
|
if(mOutputSampleFormat != (*mStream)->codec->sample_fmt ||
|
|
|
|
|| mOutputChannelLayout != ch_layout)
|
|
|
|
mOutputChannelLayout != ch_layout)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
mSwr = swr_alloc_set_opts(mSwr, // SwrContext
|
|
|
|
mSwr = swr_alloc_set_opts(mSwr, // SwrContext
|
|
|
|
mOutputChannelLayout, // output ch layout
|
|
|
|
mOutputChannelLayout, // output ch layout
|
|
|
@ -365,24 +378,37 @@ void FFmpeg_Decoder::getInfo(int *samplerate, ChannelConfig *chans, SampleType *
|
|
|
|
0, // logging level offset
|
|
|
|
0, // logging level offset
|
|
|
|
NULL); // log context
|
|
|
|
NULL); // log context
|
|
|
|
if(!mSwr)
|
|
|
|
if(!mSwr)
|
|
|
|
fail(std::string("Couldn't allocate SwrContext"));
|
|
|
|
{
|
|
|
|
|
|
|
|
std::cerr<< "Couldn't allocate SwrContext" <<std::endl;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
if(swr_init(mSwr) < 0)
|
|
|
|
if(swr_init(mSwr) < 0)
|
|
|
|
fail(std::string("Couldn't initialize SwrContext"));
|
|
|
|
{
|
|
|
|
|
|
|
|
std::cerr<< "Couldn't initialize SwrContext" <<std::endl;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
size_t FFmpeg_Decoder::read(char *buffer, size_t bytes)
|
|
|
|
size_t FFmpeg_Decoder::read(char *buffer, size_t bytes)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if(!mStream)
|
|
|
|
if(!mStream)
|
|
|
|
fail("No audio stream");
|
|
|
|
{
|
|
|
|
|
|
|
|
std::cerr<< "No audio stream" <<std::endl;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
return readAVAudioData(buffer, bytes);
|
|
|
|
return readAVAudioData(buffer, bytes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void FFmpeg_Decoder::readAll(std::vector<char> &output)
|
|
|
|
void FFmpeg_Decoder::readAll(std::vector<char> &output)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if(!mStream)
|
|
|
|
if(!mStream)
|
|
|
|
fail("No audio stream");
|
|
|
|
{
|
|
|
|
|
|
|
|
std::cerr<< "No audio stream" <<std::endl;
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
while(getAVAudioData())
|
|
|
|
while(getAVAudioData())
|
|
|
|
{
|
|
|
|
{
|
|
|
|