2018-03-29 17:00:53 +00:00
|
|
|
#ifdef MAKE_FFMPEG_PLAYER
|
2013-06-18 01:11:21 +00:00
|
|
|
|
2013-05-05 10:22:12 +00:00
|
|
|
#include "ffmpegaudio.hh"
|
|
|
|
|
|
|
|
#include <math.h>
|
2013-05-14 14:24:19 +00:00
|
|
|
#include <errno.h>
|
2013-05-05 10:22:12 +00:00
|
|
|
|
|
|
|
#ifndef INT64_C
|
|
|
|
#define INT64_C(c) (c ## LL)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef UINT64_C
|
|
|
|
#define UINT64_C(c) (c ## ULL)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <ao/ao.h>
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
#include <libavcodec/avcodec.h>
|
|
|
|
#include <libavformat/avformat.h>
|
|
|
|
#include <libavutil/avutil.h>
|
2020-07-18 12:42:29 +00:00
|
|
|
#include "libswresample/swresample.h"
|
2013-05-05 10:22:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#include <QString>
|
|
|
|
#include <QDataStream>
|
2013-06-23 09:53:10 +00:00
|
|
|
#include <QDebug>
|
2013-05-05 10:22:12 +00:00
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
2021-11-27 07:17:33 +00:00
|
|
|
#include "utils.hh"
|
2013-05-30 13:24:21 +00:00
|
|
|
|
2013-05-05 10:22:12 +00:00
|
|
|
using std::vector;
|
|
|
|
|
|
|
|
namespace Ffmpeg
|
|
|
|
{
|
|
|
|
|
|
|
|
QMutex DecoderThread::deviceMutex_;
|
|
|
|
|
|
|
|
static inline QString avErrorString( int errnum )
|
|
|
|
{
|
|
|
|
char buf[64];
|
|
|
|
av_strerror( errnum, buf, 64 );
|
|
|
|
return QString::fromLatin1( buf );
|
|
|
|
}
|
|
|
|
|
2018-03-21 17:49:34 +00:00
|
|
|
AudioService & AudioService::instance()
|
2013-05-05 10:22:12 +00:00
|
|
|
{
|
2018-03-21 17:49:34 +00:00
|
|
|
static AudioService a;
|
2013-05-05 10:22:12 +00:00
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
2018-03-21 17:49:34 +00:00
|
|
|
AudioService::AudioService()
|
2013-05-05 10:22:12 +00:00
|
|
|
{
|
2019-09-27 14:00:52 +00:00
|
|
|
#if LIBAVFORMAT_VERSION_MAJOR < 58 || ( LIBAVFORMAT_VERSION_MAJOR == 58 && LIBAVFORMAT_VERSION_MINOR < 9 )
|
2013-05-05 10:22:12 +00:00
|
|
|
av_register_all();
|
2019-09-27 14:00:52 +00:00
|
|
|
#endif
|
2013-05-05 10:22:12 +00:00
|
|
|
ao_initialize();
|
|
|
|
}
|
|
|
|
|
2018-03-21 17:49:34 +00:00
|
|
|
AudioService::~AudioService()
|
2013-05-05 10:22:12 +00:00
|
|
|
{
|
|
|
|
emit cancelPlaying( true );
|
|
|
|
ao_shutdown();
|
|
|
|
}
|
|
|
|
|
2018-03-21 17:49:34 +00:00
|
|
|
void AudioService::playMemory( const char * ptr, int size )
|
2013-05-05 10:22:12 +00:00
|
|
|
{
|
|
|
|
emit cancelPlaying( false );
|
2018-03-21 16:51:18 +00:00
|
|
|
QByteArray audioData( ptr, size );
|
2013-05-05 10:22:12 +00:00
|
|
|
DecoderThread * thread = new DecoderThread( audioData, this );
|
|
|
|
|
|
|
|
connect( thread, SIGNAL( error( QString ) ), this, SIGNAL( error( QString ) ) );
|
|
|
|
connect( this, SIGNAL( cancelPlaying( bool ) ), thread, SLOT( cancel( bool ) ), Qt::DirectConnection );
|
|
|
|
connect( thread, SIGNAL( finished() ), thread, SLOT( deleteLater() ) );
|
|
|
|
|
|
|
|
thread->start();
|
|
|
|
}
|
|
|
|
|
2018-03-21 17:49:34 +00:00
|
|
|
void AudioService::stop()
|
2013-06-23 09:53:10 +00:00
|
|
|
{
|
|
|
|
emit cancelPlaying( false );
|
|
|
|
}
|
|
|
|
|
2013-05-05 10:22:12 +00:00
|
|
|
struct DecoderContext
|
|
|
|
{
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
kBufferSize = 32768
|
|
|
|
};
|
|
|
|
|
|
|
|
static QMutex deviceMutex_;
|
|
|
|
QAtomicInt & isCancelled_;
|
|
|
|
QByteArray audioData_;
|
|
|
|
QDataStream audioDataStream_;
|
|
|
|
AVFormatContext * formatContext_;
|
2018-03-30 19:53:24 +00:00
|
|
|
AVCodec * codec_;
|
2013-05-05 10:22:12 +00:00
|
|
|
AVCodecContext * codecContext_;
|
|
|
|
AVIOContext * avioContext_;
|
|
|
|
AVStream * audioStream_;
|
|
|
|
ao_device * aoDevice_;
|
|
|
|
bool avformatOpened_;
|
|
|
|
|
2020-07-18 12:42:29 +00:00
|
|
|
SwrContext *swr_;
|
|
|
|
|
2013-05-05 10:22:12 +00:00
|
|
|
DecoderContext( QByteArray const & audioData, QAtomicInt & isCancelled );
|
|
|
|
~DecoderContext();
|
|
|
|
|
|
|
|
bool openCodec( QString & errorString );
|
|
|
|
void closeCodec();
|
|
|
|
bool openOutputDevice( QString & errorString );
|
|
|
|
void closeOutputDevice();
|
|
|
|
bool play( QString & errorString );
|
|
|
|
bool normalizeAudio( AVFrame * frame, vector<char> & samples );
|
|
|
|
void playFrame( AVFrame * frame );
|
|
|
|
};
|
|
|
|
|
|
|
|
DecoderContext::DecoderContext( QByteArray const & audioData, QAtomicInt & isCancelled ):
|
|
|
|
isCancelled_( isCancelled ),
|
|
|
|
audioData_( audioData ),
|
|
|
|
audioDataStream_( audioData_ ),
|
|
|
|
formatContext_( NULL ),
|
2018-03-30 19:53:24 +00:00
|
|
|
codec_( NULL ),
|
2013-05-05 10:22:12 +00:00
|
|
|
codecContext_( NULL ),
|
|
|
|
avioContext_( NULL ),
|
|
|
|
audioStream_( NULL ),
|
|
|
|
aoDevice_( NULL ),
|
2020-07-18 12:42:29 +00:00
|
|
|
avformatOpened_( false ),
|
|
|
|
swr_( NULL )
|
2013-05-05 10:22:12 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
DecoderContext::~DecoderContext()
|
|
|
|
{
|
|
|
|
closeOutputDevice();
|
|
|
|
closeCodec();
|
|
|
|
}
|
|
|
|
|
|
|
|
static int readAudioData( void * opaque, unsigned char * buffer, int bufferSize )
|
|
|
|
{
|
|
|
|
QDataStream * pStream = ( QDataStream * )opaque;
|
|
|
|
return pStream->readRawData( ( char * )buffer, bufferSize );
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DecoderContext::openCodec( QString & errorString )
|
|
|
|
{
|
|
|
|
formatContext_ = avformat_alloc_context();
|
|
|
|
if ( !formatContext_ )
|
|
|
|
{
|
2013-05-14 16:00:31 +00:00
|
|
|
errorString = QObject::tr( "avformat_alloc_context() failed." );
|
2013-05-05 10:22:12 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-03-30 19:53:24 +00:00
|
|
|
#if LIBAVCODEC_VERSION_MAJOR < 56 || ( LIBAVCODEC_VERSION_MAJOR == 56 && LIBAVCODEC_VERSION_MINOR < 56 )
|
2013-05-05 10:22:12 +00:00
|
|
|
unsigned char * avioBuffer = ( unsigned char * )av_malloc( kBufferSize + FF_INPUT_BUFFER_PADDING_SIZE );
|
2018-03-30 19:53:24 +00:00
|
|
|
#else
|
|
|
|
unsigned char * avioBuffer = ( unsigned char * )av_malloc( kBufferSize + AV_INPUT_BUFFER_PADDING_SIZE );
|
|
|
|
#endif
|
2013-05-05 10:22:12 +00:00
|
|
|
if ( !avioBuffer )
|
|
|
|
{
|
2013-05-14 16:00:31 +00:00
|
|
|
errorString = QObject::tr( "av_malloc() failed." );
|
2013-05-05 10:22:12 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't free buffer allocated here (if succeeded), it will be cleaned up automatically.
|
|
|
|
avioContext_ = avio_alloc_context( avioBuffer, kBufferSize, 0, &audioDataStream_, readAudioData, NULL, NULL );
|
|
|
|
if ( !avioContext_ )
|
|
|
|
{
|
|
|
|
av_free( avioBuffer );
|
2013-05-14 16:00:31 +00:00
|
|
|
errorString = QObject::tr( "avio_alloc_context() failed." );
|
2013-05-05 10:22:12 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
avioContext_->seekable = 0;
|
|
|
|
avioContext_->write_flag = 0;
|
|
|
|
|
|
|
|
// If pb not set, avformat_open_input() simply crash.
|
|
|
|
formatContext_->pb = avioContext_;
|
|
|
|
formatContext_->flags |= AVFMT_FLAG_CUSTOM_IO;
|
|
|
|
|
|
|
|
int ret = 0;
|
|
|
|
avformatOpened_ = true;
|
|
|
|
|
|
|
|
ret = avformat_open_input( &formatContext_, "_STREAM_", NULL, NULL );
|
|
|
|
if ( ret < 0 )
|
|
|
|
{
|
2013-05-14 16:00:31 +00:00
|
|
|
errorString = QObject::tr( "avformat_open_input() failed: %1." ).arg( avErrorString( ret ) );
|
2013-05-05 10:22:12 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = avformat_find_stream_info( formatContext_, NULL );
|
|
|
|
if ( ret < 0 )
|
|
|
|
{
|
2013-05-14 16:00:31 +00:00
|
|
|
errorString = QObject::tr( "avformat_find_stream_info() failed: %1." ).arg( avErrorString( ret ) );
|
2013-05-05 10:22:12 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find audio stream, use the first audio stream if available
|
|
|
|
for ( unsigned i = 0; i < formatContext_->nb_streams; i++ )
|
|
|
|
{
|
2018-03-30 19:53:24 +00:00
|
|
|
#if LIBAVCODEC_VERSION_MAJOR < 57 || ( LIBAVCODEC_VERSION_MAJOR == 57 && LIBAVCODEC_VERSION_MINOR < 33 )
|
2013-05-05 10:22:12 +00:00
|
|
|
if ( formatContext_->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO )
|
2018-03-30 19:53:24 +00:00
|
|
|
#else
|
|
|
|
if ( formatContext_->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO )
|
|
|
|
#endif
|
2013-05-05 10:22:12 +00:00
|
|
|
{
|
|
|
|
audioStream_ = formatContext_->streams[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( !audioStream_ )
|
|
|
|
{
|
2013-05-14 16:00:31 +00:00
|
|
|
errorString = QObject::tr( "Could not find audio stream." );
|
2013-05-05 10:22:12 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-03-30 19:53:24 +00:00
|
|
|
#if LIBAVCODEC_VERSION_MAJOR < 57 || ( LIBAVCODEC_VERSION_MAJOR == 57 && LIBAVCODEC_VERSION_MINOR < 33 )
|
2013-05-05 10:22:12 +00:00
|
|
|
codecContext_ = audioStream_->codec;
|
2018-03-30 19:53:24 +00:00
|
|
|
codec_ = avcodec_find_decoder( codecContext_->codec_id );
|
|
|
|
if ( !codec_ )
|
2013-05-05 10:22:12 +00:00
|
|
|
{
|
2013-05-14 16:00:31 +00:00
|
|
|
errorString = QObject::tr( "Codec [id: %1] not found." ).arg( codecContext_->codec_id );
|
2013-05-05 10:22:12 +00:00
|
|
|
return false;
|
|
|
|
}
|
2018-03-30 19:53:24 +00:00
|
|
|
#else
|
|
|
|
codec_ = avcodec_find_decoder( audioStream_->codecpar->codec_id );
|
|
|
|
if ( !codec_ )
|
|
|
|
{
|
|
|
|
errorString = QObject::tr( "Codec [id: %1] not found." ).arg( audioStream_->codecpar->codec_id );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
codecContext_ = avcodec_alloc_context3( codec_ );
|
|
|
|
if ( !codecContext_ )
|
|
|
|
{
|
|
|
|
errorString = QObject::tr( "avcodec_alloc_context3() failed." );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
avcodec_parameters_to_context( codecContext_, audioStream_->codecpar );
|
|
|
|
#endif
|
2013-05-05 10:22:12 +00:00
|
|
|
|
2018-03-30 19:53:24 +00:00
|
|
|
ret = avcodec_open2( codecContext_, codec_, NULL );
|
2013-05-05 10:22:12 +00:00
|
|
|
if ( ret < 0 )
|
|
|
|
{
|
2013-05-14 16:00:31 +00:00
|
|
|
errorString = QObject::tr( "avcodec_open2() failed: %1." ).arg( avErrorString( ret ) );
|
2013-05-05 10:22:12 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-03-30 19:53:24 +00:00
|
|
|
av_log( NULL, AV_LOG_INFO, "Codec open: %s: channels: %d, rate: %d, format: %s\n", codec_->long_name,
|
2013-05-05 10:22:12 +00:00
|
|
|
codecContext_->channels, codecContext_->sample_rate, av_get_sample_fmt_name( codecContext_->sample_fmt ) );
|
2020-07-18 12:42:29 +00:00
|
|
|
|
|
|
|
if ( codecContext_->sample_fmt == AV_SAMPLE_FMT_S32 ||
|
|
|
|
codecContext_->sample_fmt == AV_SAMPLE_FMT_S32P ||
|
|
|
|
codecContext_->sample_fmt == AV_SAMPLE_FMT_FLT ||
|
|
|
|
codecContext_->sample_fmt == AV_SAMPLE_FMT_FLTP ||
|
|
|
|
codecContext_->sample_fmt == AV_SAMPLE_FMT_DBL ||
|
|
|
|
codecContext_->sample_fmt == AV_SAMPLE_FMT_DBLP )
|
|
|
|
{
|
|
|
|
swr_ = swr_alloc_set_opts( NULL,
|
|
|
|
codecContext_->channel_layout,
|
|
|
|
AV_SAMPLE_FMT_S16,
|
|
|
|
codecContext_->sample_rate,
|
|
|
|
codecContext_->channel_layout,
|
|
|
|
codecContext_->sample_fmt,
|
|
|
|
codecContext_->sample_rate,
|
|
|
|
0,
|
|
|
|
NULL );
|
|
|
|
swr_init( swr_ );
|
|
|
|
}
|
|
|
|
|
2013-05-05 10:22:12 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DecoderContext::closeCodec()
|
|
|
|
{
|
2020-07-18 12:42:29 +00:00
|
|
|
if ( swr_ )
|
|
|
|
{
|
|
|
|
swr_free( &swr_ );
|
|
|
|
}
|
|
|
|
|
2013-05-05 10:22:12 +00:00
|
|
|
if ( !formatContext_ )
|
|
|
|
{
|
|
|
|
if ( avioContext_ )
|
|
|
|
{
|
|
|
|
av_free( avioContext_->buffer );
|
|
|
|
avioContext_ = NULL;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// avformat_open_input() is not called, just free the buffer associated with
|
|
|
|
// the AVIOContext, and the AVFormatContext
|
|
|
|
if ( !avformatOpened_ )
|
|
|
|
{
|
|
|
|
if ( formatContext_ )
|
|
|
|
{
|
|
|
|
avformat_free_context( formatContext_ );
|
|
|
|
formatContext_ = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( avioContext_ )
|
|
|
|
{
|
|
|
|
av_free( avioContext_->buffer );
|
|
|
|
avioContext_ = NULL;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
avformatOpened_ = false;
|
|
|
|
|
|
|
|
// Closing a codec context without prior avcodec_open2() will result in
|
|
|
|
// a crash in ffmpeg
|
2018-03-30 19:53:24 +00:00
|
|
|
if ( audioStream_ && codecContext_ && codec_ )
|
2013-05-05 10:22:12 +00:00
|
|
|
{
|
|
|
|
audioStream_->discard = AVDISCARD_ALL;
|
2018-03-30 19:53:24 +00:00
|
|
|
avcodec_close( codecContext_ );
|
|
|
|
#if LIBAVCODEC_VERSION_MAJOR > 57 || ( LIBAVCODEC_VERSION_MAJOR == 57 && LIBAVCODEC_VERSION_MINOR >= 33 )
|
|
|
|
avcodec_free_context( &codecContext_ );
|
|
|
|
#endif
|
2013-05-05 10:22:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
avformat_close_input( &formatContext_ );
|
|
|
|
av_free( avioContext_->buffer );
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DecoderContext::openOutputDevice( QString & errorString )
|
|
|
|
{
|
|
|
|
// Prepare for audio output
|
|
|
|
int aoDriverId = ao_default_driver_id();
|
2013-05-14 14:24:19 +00:00
|
|
|
ao_info * aoDrvInfo = ao_driver_info( aoDriverId );
|
|
|
|
|
|
|
|
if ( aoDriverId < 0 || !aoDrvInfo )
|
2013-05-05 10:22:12 +00:00
|
|
|
{
|
2013-05-14 16:00:31 +00:00
|
|
|
errorString = QObject::tr( "Cannot find usable audio output device." );
|
2013-05-05 10:22:12 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ao_sample_format aoSampleFormat;
|
2020-07-18 12:42:29 +00:00
|
|
|
memset (&aoSampleFormat, 0, sizeof(aoSampleFormat) );
|
2013-05-05 10:22:12 +00:00
|
|
|
aoSampleFormat.channels = codecContext_->channels;
|
|
|
|
aoSampleFormat.rate = codecContext_->sample_rate;
|
|
|
|
aoSampleFormat.byte_format = AO_FMT_NATIVE;
|
|
|
|
aoSampleFormat.matrix = 0;
|
2020-07-18 12:42:29 +00:00
|
|
|
aoSampleFormat.bits = qMin( 16, av_get_bytes_per_sample( codecContext_->sample_fmt ) << 3 );
|
2013-05-05 10:22:12 +00:00
|
|
|
|
|
|
|
if ( aoSampleFormat.bits == 0 )
|
|
|
|
{
|
2013-05-14 16:00:31 +00:00
|
|
|
errorString = QObject::tr( "Unsupported sample format." );
|
2013-05-05 10:22:12 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-05-14 14:24:19 +00:00
|
|
|
av_log( NULL, AV_LOG_INFO, "ao_open_live(): %s: channels: %d, rate: %d, bits: %d\n",
|
|
|
|
aoDrvInfo->name, aoSampleFormat.channels, aoSampleFormat.rate, aoSampleFormat.bits );
|
|
|
|
|
2013-05-05 10:22:12 +00:00
|
|
|
aoDevice_ = ao_open_live( aoDriverId, &aoSampleFormat, NULL );
|
|
|
|
if ( !aoDevice_ )
|
|
|
|
{
|
2013-05-14 16:00:31 +00:00
|
|
|
errorString = QObject::tr( "ao_open_live() failed: " );
|
2013-05-05 10:22:12 +00:00
|
|
|
|
2013-05-14 14:24:19 +00:00
|
|
|
switch ( errno )
|
|
|
|
{
|
|
|
|
case AO_ENODRIVER:
|
2013-05-14 16:00:31 +00:00
|
|
|
errorString += QObject::tr( "No driver." );
|
2013-05-14 14:24:19 +00:00
|
|
|
break;
|
|
|
|
case AO_ENOTLIVE:
|
2013-05-14 16:00:31 +00:00
|
|
|
errorString += QObject::tr( "This driver is not a live output device." );
|
2013-05-14 14:24:19 +00:00
|
|
|
break;
|
|
|
|
case AO_EBADOPTION:
|
2013-05-14 16:00:31 +00:00
|
|
|
errorString += QObject::tr( "A valid option key has an invalid value." );
|
2013-05-14 14:24:19 +00:00
|
|
|
break;
|
|
|
|
case AO_EOPENDEVICE:
|
2013-05-14 16:00:31 +00:00
|
|
|
errorString += QObject::tr( "Cannot open the device: %1, channels: %2, rate: %3, bits: %4." )
|
2013-05-14 14:24:19 +00:00
|
|
|
.arg( aoDrvInfo->short_name )
|
|
|
|
.arg( aoSampleFormat.channels )
|
|
|
|
.arg( aoSampleFormat.rate )
|
|
|
|
.arg( aoSampleFormat.bits );
|
|
|
|
break;
|
|
|
|
default:
|
2013-05-14 16:00:31 +00:00
|
|
|
errorString += QObject::tr( "Unknown error." );
|
2013-05-14 14:24:19 +00:00
|
|
|
break;
|
|
|
|
}
|
2013-05-05 10:22:12 +00:00
|
|
|
|
2013-05-14 14:24:19 +00:00
|
|
|
return false;
|
2013-05-05 10:22:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DecoderContext::closeOutputDevice()
|
|
|
|
{
|
|
|
|
// ao_close() is synchronous, it will wait until all audio streams flushed
|
|
|
|
if ( aoDevice_ )
|
|
|
|
{
|
|
|
|
ao_close( aoDevice_ );
|
|
|
|
aoDevice_ = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DecoderContext::play( QString & errorString )
|
|
|
|
{
|
2016-02-17 14:37:23 +00:00
|
|
|
#if LIBAVCODEC_VERSION_MAJOR < 55 || ( LIBAVCODEC_VERSION_MAJOR == 55 && LIBAVCODEC_VERSION_MINOR < 28 )
|
2013-05-05 10:22:12 +00:00
|
|
|
AVFrame * frame = avcodec_alloc_frame();
|
2016-02-17 14:37:23 +00:00
|
|
|
#else
|
|
|
|
AVFrame * frame = av_frame_alloc();
|
|
|
|
#endif
|
2013-05-05 10:22:12 +00:00
|
|
|
if ( !frame )
|
|
|
|
{
|
2013-05-14 16:00:31 +00:00
|
|
|
errorString = QObject::tr( "avcodec_alloc_frame() failed." );
|
2013-05-05 10:22:12 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
AVPacket packet;
|
|
|
|
av_init_packet( &packet );
|
|
|
|
|
2021-11-27 07:17:33 +00:00
|
|
|
while ( !Utils::AtomicInt::loadAcquire( isCancelled_ ) &&
|
2013-05-30 13:24:21 +00:00
|
|
|
av_read_frame( formatContext_, &packet ) >= 0 )
|
2013-05-05 10:22:12 +00:00
|
|
|
{
|
|
|
|
if ( packet.stream_index == audioStream_->index )
|
|
|
|
{
|
2014-07-15 14:47:08 +00:00
|
|
|
AVPacket pack = packet;
|
2018-03-30 19:53:24 +00:00
|
|
|
#if LIBAVCODEC_VERSION_MAJOR < 57 || ( LIBAVCODEC_VERSION_MAJOR == 57 && LIBAVCODEC_VERSION_MINOR < 37 )
|
2013-05-05 10:22:12 +00:00
|
|
|
int gotFrame = 0;
|
2014-07-15 14:47:08 +00:00
|
|
|
do
|
2013-05-05 10:22:12 +00:00
|
|
|
{
|
2014-07-15 14:47:08 +00:00
|
|
|
int len = avcodec_decode_audio4( codecContext_, frame, &gotFrame, &pack );
|
2021-11-27 07:17:33 +00:00
|
|
|
if ( !Utils::AtomicInt::loadAcquire( isCancelled_ ) && gotFrame )
|
2014-07-15 14:47:08 +00:00
|
|
|
{
|
|
|
|
playFrame( frame );
|
|
|
|
}
|
2021-11-27 07:17:33 +00:00
|
|
|
if( len <= 0 || Utils::AtomicInt::loadAcquire( isCancelled_ ) )
|
2014-07-15 14:47:08 +00:00
|
|
|
break;
|
|
|
|
pack.size -= len;
|
|
|
|
pack.data += len;
|
2013-05-05 10:22:12 +00:00
|
|
|
}
|
2014-07-15 14:47:08 +00:00
|
|
|
while( pack.size > 0 );
|
2018-03-30 19:53:24 +00:00
|
|
|
#else
|
|
|
|
int ret = avcodec_send_packet( codecContext_, &pack );
|
|
|
|
/* read all the output frames (in general there may be any number of them) */
|
|
|
|
while( ret >= 0 )
|
|
|
|
{
|
|
|
|
ret = avcodec_receive_frame( codecContext_, frame);
|
|
|
|
|
2021-11-27 07:17:33 +00:00
|
|
|
if ( Utils::AtomicInt::loadAcquire( isCancelled_ ) || ret < 0 )
|
2018-03-30 19:53:24 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
playFrame( frame );
|
|
|
|
}
|
|
|
|
#endif
|
2013-05-05 10:22:12 +00:00
|
|
|
}
|
|
|
|
// av_free_packet() must be called after each call to av_read_frame()
|
2016-02-17 14:37:23 +00:00
|
|
|
#if LIBAVCODEC_VERSION_MAJOR < 57 || ( LIBAVCODEC_VERSION_MAJOR == 57 && LIBAVCODEC_VERSION_MINOR < 7 )
|
2013-05-05 10:22:12 +00:00
|
|
|
av_free_packet( &packet );
|
2016-02-17 14:37:23 +00:00
|
|
|
#else
|
|
|
|
av_packet_unref( &packet );
|
|
|
|
#endif
|
2013-05-05 10:22:12 +00:00
|
|
|
}
|
|
|
|
|
2018-03-30 19:53:24 +00:00
|
|
|
#if LIBAVCODEC_VERSION_MAJOR < 57 || ( LIBAVCODEC_VERSION_MAJOR == 57 && LIBAVCODEC_VERSION_MINOR < 37 )
|
2021-11-27 07:17:33 +00:00
|
|
|
if ( !Utils::AtomicInt::loadAcquire( isCancelled_ ) &&
|
2013-05-30 13:24:21 +00:00
|
|
|
codecContext_->codec->capabilities & CODEC_CAP_DELAY )
|
2013-05-05 10:22:12 +00:00
|
|
|
{
|
|
|
|
av_init_packet( &packet );
|
|
|
|
int gotFrame = 0;
|
|
|
|
while ( avcodec_decode_audio4( codecContext_, frame, &gotFrame, &packet ) >= 0 && gotFrame )
|
|
|
|
{
|
2021-11-27 07:17:33 +00:00
|
|
|
if ( Utils::AtomicInt::loadAcquire( isCancelled_ ) )
|
2013-05-05 10:22:12 +00:00
|
|
|
break;
|
|
|
|
playFrame( frame );
|
|
|
|
}
|
|
|
|
}
|
2018-03-30 19:53:24 +00:00
|
|
|
#else
|
|
|
|
/* flush the decoder */
|
|
|
|
av_init_packet( &packet );
|
2019-09-27 14:00:52 +00:00
|
|
|
packet.data = NULL;
|
|
|
|
packet.size = 0;
|
2018-03-30 19:53:24 +00:00
|
|
|
int ret = avcodec_send_packet(codecContext_, &packet );
|
|
|
|
while( ret >= 0 )
|
|
|
|
{
|
|
|
|
ret = avcodec_receive_frame(codecContext_, frame);
|
2021-11-27 07:17:33 +00:00
|
|
|
if ( Utils::AtomicInt::loadAcquire( isCancelled_ ) || ret < 0 )
|
2018-03-30 19:53:24 +00:00
|
|
|
break;
|
|
|
|
playFrame( frame );
|
|
|
|
}
|
|
|
|
#endif
|
2013-05-05 10:22:12 +00:00
|
|
|
|
|
|
|
#if LIBAVCODEC_VERSION_MAJOR < 54
|
|
|
|
av_free( frame );
|
2016-02-17 14:37:23 +00:00
|
|
|
#elif LIBAVCODEC_VERSION_MAJOR < 55 || ( LIBAVCODEC_VERSION_MAJOR == 55 && LIBAVCODEC_VERSION_MINOR < 28 )
|
2013-05-05 10:22:12 +00:00
|
|
|
avcodec_free_frame( &frame );
|
2016-02-17 14:37:23 +00:00
|
|
|
#else
|
|
|
|
av_frame_free( &frame );
|
2013-05-05 10:22:12 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int32_t toInt32( double v )
|
|
|
|
{
|
|
|
|
if ( v >= 1.0 )
|
|
|
|
return 0x7fffffffL;
|
|
|
|
else if ( v <= -1.0 )
|
|
|
|
return 0x80000000L;
|
|
|
|
return floor( v * 2147483648.0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DecoderContext::normalizeAudio( AVFrame * frame, vector<char> & samples )
|
|
|
|
{
|
|
|
|
int lineSize = 0;
|
|
|
|
int dataSize = av_samples_get_buffer_size( &lineSize, codecContext_->channels,
|
|
|
|
frame->nb_samples, codecContext_->sample_fmt, 1 );
|
|
|
|
|
|
|
|
// Portions from: https://code.google.com/p/lavfilters/source/browse/decoder/LAVAudio/LAVAudio.cpp
|
|
|
|
// But this one use 8, 16, 32 bits integer, respectively.
|
|
|
|
switch ( codecContext_->sample_fmt )
|
|
|
|
{
|
|
|
|
case AV_SAMPLE_FMT_U8:
|
|
|
|
case AV_SAMPLE_FMT_S16:
|
|
|
|
{
|
|
|
|
samples.resize( dataSize );
|
2013-05-24 08:15:07 +00:00
|
|
|
memcpy( &samples.front(), frame->data[0], lineSize );
|
2013-05-05 10:22:12 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
// Planar
|
|
|
|
case AV_SAMPLE_FMT_U8P:
|
|
|
|
{
|
|
|
|
samples.resize( dataSize );
|
|
|
|
|
|
|
|
uint8_t * out = ( uint8_t * )&samples.front();
|
|
|
|
for ( int i = 0; i < frame->nb_samples; i++ )
|
|
|
|
{
|
|
|
|
for ( int ch = 0; ch < codecContext_->channels; ch++ )
|
|
|
|
{
|
|
|
|
*out++ = ( ( uint8_t * )frame->extended_data[ch] )[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case AV_SAMPLE_FMT_S16P:
|
|
|
|
{
|
|
|
|
samples.resize( dataSize );
|
|
|
|
|
|
|
|
int16_t * out = ( int16_t * )&samples.front();
|
|
|
|
for ( int i = 0; i < frame->nb_samples; i++ )
|
|
|
|
{
|
|
|
|
for ( int ch = 0; ch < codecContext_->channels; ch++ )
|
|
|
|
{
|
|
|
|
*out++ = ( ( int16_t * )frame->extended_data[ch] )[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2020-07-18 12:42:29 +00:00
|
|
|
case AV_SAMPLE_FMT_S32:
|
|
|
|
/* Pass through */
|
2013-05-05 10:22:12 +00:00
|
|
|
case AV_SAMPLE_FMT_S32P:
|
2020-07-18 12:42:29 +00:00
|
|
|
/* Pass through */
|
|
|
|
case AV_SAMPLE_FMT_FLT:
|
|
|
|
/* Pass through */
|
2013-05-05 10:22:12 +00:00
|
|
|
case AV_SAMPLE_FMT_FLTP:
|
2020-07-18 12:42:29 +00:00
|
|
|
/* Pass through */
|
2013-05-05 10:22:12 +00:00
|
|
|
{
|
2020-07-18 12:42:29 +00:00
|
|
|
samples.resize( dataSize / 2 );
|
2013-05-05 10:22:12 +00:00
|
|
|
|
2020-07-18 12:42:29 +00:00
|
|
|
uint8_t *out = ( uint8_t * )&samples.front();
|
|
|
|
swr_convert( swr_, &out, frame->nb_samples, (const uint8_t**)frame->extended_data, frame->nb_samples );
|
2013-05-05 10:22:12 +00:00
|
|
|
}
|
|
|
|
break;
|
2020-07-18 12:42:29 +00:00
|
|
|
case AV_SAMPLE_FMT_DBL:
|
2013-05-05 10:22:12 +00:00
|
|
|
case AV_SAMPLE_FMT_DBLP:
|
|
|
|
{
|
2020-07-18 12:42:29 +00:00
|
|
|
samples.resize( dataSize / 4 );
|
2013-05-05 10:22:12 +00:00
|
|
|
|
2020-07-18 12:42:29 +00:00
|
|
|
uint8_t *out = ( uint8_t * )&samples.front();
|
|
|
|
swr_convert( swr_, &out, frame->nb_samples, (const uint8_t**)frame->extended_data, frame->nb_samples );
|
2013-05-05 10:22:12 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DecoderContext::playFrame( AVFrame * frame )
|
|
|
|
{
|
|
|
|
if ( !frame )
|
|
|
|
return;
|
|
|
|
|
|
|
|
vector<char> samples;
|
|
|
|
if ( normalizeAudio( frame, samples ) )
|
|
|
|
ao_play( aoDevice_, &samples.front(), samples.size() );
|
|
|
|
}
|
|
|
|
|
|
|
|
DecoderThread::DecoderThread( QByteArray const & audioData, QObject * parent ) :
|
|
|
|
QThread( parent ),
|
|
|
|
isCancelled_( 0 ),
|
|
|
|
audioData_( audioData )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
DecoderThread::~DecoderThread()
|
|
|
|
{
|
|
|
|
isCancelled_.ref();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DecoderThread::run()
|
|
|
|
{
|
|
|
|
QString errorString;
|
|
|
|
DecoderContext d( audioData_, isCancelled_ );
|
|
|
|
|
|
|
|
if ( !d.openCodec( errorString ) )
|
|
|
|
{
|
|
|
|
emit error( errorString );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ( !deviceMutex_.tryLock( 100 ) )
|
|
|
|
{
|
2021-11-27 07:17:33 +00:00
|
|
|
if ( Utils::AtomicInt::loadAcquire( isCancelled_ ) )
|
2013-05-05 10:22:12 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !d.openOutputDevice( errorString ) )
|
|
|
|
emit error( errorString );
|
|
|
|
else if ( !d.play( errorString ) )
|
|
|
|
emit error( errorString );
|
|
|
|
|
|
|
|
d.closeOutputDevice();
|
|
|
|
deviceMutex_.unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DecoderThread::cancel( bool waitUntilFinished )
|
|
|
|
{
|
|
|
|
isCancelled_.ref();
|
|
|
|
if ( waitUntilFinished )
|
|
|
|
this->wait();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2013-06-18 01:11:21 +00:00
|
|
|
|
2018-03-29 17:00:53 +00:00
|
|
|
#endif // MAKE_FFMPEG_PLAYER
|