diff --git a/plugins/decoder/ffmpeg/k3bffmpegwrapper.cpp b/plugins/decoder/ffmpeg/k3bffmpegwrapper.cpp --- a/plugins/decoder/ffmpeg/k3bffmpegwrapper.cpp +++ b/plugins/decoder/ffmpeg/k3bffmpegwrapper.cpp @@ -38,8 +38,6 @@ #include -#define FFMPEG_CODEC(s) (s->codec) - #ifndef HAVE_FFMPEG_AVFORMAT_OPEN_INPUT // this works because the parameters/options are not used # define avformat_open_input(c,s,f,o) av_open_input_file(c,s,f,0,o) @@ -70,7 +68,7 @@ #define AVCODEC_MAX_AUDIO_FRAME_SIZE 192000 #endif -K3bFFMpegWrapper* K3bFFMpegWrapper::s_instance = 0; +K3bFFMpegWrapper* K3bFFMpegWrapper::s_instance = nullptr; class K3bFFMpegFile::Private @@ -78,6 +76,7 @@ public: ::AVFormatContext* formatContext; ::AVCodec* codec; + ::AVCodecContext* codecContext; K3b::Msf length; @@ -87,8 +86,8 @@ ::AVPacket packet; quint8* packetData; int packetSize; + ::AVSampleFormat sampleFormat; bool isSpacious; - int sampleFormat; }; @@ -96,8 +95,8 @@ : m_filename(filename) { d = new Private; - d->formatContext = 0; - d->codec = 0; + d->formatContext = nullptr; + d->codec = nullptr; d->frame = ::av_frame_alloc(); } @@ -130,40 +129,42 @@ return false; } - // urgh... ugly - ::AVCodecContext* codecContext = FFMPEG_CODEC(d->formatContext->streams[0]); - if( codecContext->codec_type != AVMEDIA_TYPE_AUDIO) - { - qDebug() << "(K3bFFMpegFile) not a simple audio stream: " << m_filename; - return false; - } - // get the codec - d->codec = ::avcodec_find_decoder(codecContext->codec_id); + d->codec = ::avcodec_find_decoder( + d->formatContext->streams[0]->codecpar->codec_id); if( !d->codec ) { qDebug() << "(K3bFFMpegFile) no codec found for " << m_filename; return false; } + // get the codec context + d->codecContext = ::avcodec_alloc_context3(d->codec); + ::avcodec_parameters_to_context(d->codecContext, + d->formatContext->streams[0]->codecpar); + if(d->codecContext->codec_type != AVMEDIA_TYPE_AUDIO) { + qDebug() << "(K3bFFMpegFile) not a simple audio stream: " << m_filename; + return false; + } + // open the codec on our context qDebug() << "(K3bFFMpegFile) found codec for " << m_filename; - if( ::avcodec_open2( codecContext, d->codec, 0 ) < 0 ) { + if( ::avcodec_open2( d->codecContext, d->codec, 0 ) < 0 ) { qDebug() << "(K3bFFMpegDecoderFactory) could not open codec."; return false; } // determine the length of the stream - d->length = K3b::Msf::fromSeconds( (double)d->formatContext->duration / (double)AV_TIME_BASE ); + d->length = K3b::Msf::fromSeconds( + (double)d->formatContext->duration / (double)AV_TIME_BASE); if( d->length == 0 ) { qDebug() << "(K3bFFMpegDecoderFactory) invalid length."; return false; } - d->isSpacious = ::av_sample_fmt_is_planar( - FFMPEG_CODEC(d->formatContext->streams[0])->sample_fmt) - && FFMPEG_CODEC(d->formatContext->streams[0])->channels > 1; - d->sampleFormat = FFMPEG_CODEC(d->formatContext->streams[0])->sample_fmt; + d->sampleFormat = d->codecContext->sample_fmt; + d->isSpacious = ::av_sample_fmt_is_planar(d->sampleFormat) + && d->codecContext->channels > 1; // dump some debugging info ::av_dump_format( d->formatContext, 0, m_filename.toLocal8Bit(), 0 ); @@ -176,16 +177,18 @@ { d->outputBufferSize = 0; d->packetSize = 0; - d->packetData = 0; + d->packetData = nullptr; if( d->codec ) { - ::avcodec_close( FFMPEG_CODEC(d->formatContext->streams[0]) ); - d->codec = 0; + ::avcodec_close(d->codecContext); + d->codec = nullptr; + ::avcodec_free_context(&d->codecContext); + d->codecContext = nullptr; } if( d->formatContext ) { ::avformat_close_input( &d->formatContext ); - d->formatContext = 0; + d->formatContext = nullptr; } } @@ -198,19 +201,19 @@ int K3bFFMpegFile::sampleRate() const { - return FFMPEG_CODEC(d->formatContext->streams[0])->sample_rate; + return d->codecContext->sample_rate; } int K3bFFMpegFile::channels() const { - return FFMPEG_CODEC(d->formatContext->streams[0])->channels; + return d->codecContext->channels; } int K3bFFMpegFile::type() const { - return FFMPEG_CODEC(d->formatContext->streams[0])->codec_id; + return d->codecContext->codec_id; } @@ -313,12 +316,10 @@ } int gotFrame = 0; - int len = ::avcodec_decode_audio4( - FFMPEG_CODEC(d->formatContext->streams[0]), - d->frame, - &gotFrame, - &d->packet - ); + int len = ::avcodec_decode_audio4(d->codecContext, + d->frame, + &gotFrame, + &d->packet); if( d->packetSize <= 0 || len < 0 ) ::av_packet_unref( &d->packet ); @@ -393,7 +394,7 @@ K3bFFMpegWrapper::~K3bFFMpegWrapper() { - s_instance = 0; + s_instance = nullptr; } @@ -427,5 +428,5 @@ } delete file; - return 0; + return nullptr; }