4
* Copyright (C) 2004-2008 Sebastian Trueg <trueg@k3b.org>
6
* This file is part of the K3b project.
7
* Copyright (C) 1998-2008 Sebastian Trueg <trueg@k3b.org>
9
* This program is free software; you can redistribute it and/or modify
10
* it under the terms of the GNU General Public License as published by
11
* the Free Software Foundation; either version 2 of the License, or
12
* (at your option) any later version.
13
* See the file "COPYING" for the exact licensing terms.
15
#include "k3bffmpegwrapper.h"
17
#include <config-k3b.h>
21
Recent versions of FFmepg uses C99 constant macros which are not presebt in C++ standard.
22
The macro __STDC_CONSTANT_MACROS allow C++ to use these macros. Altough it's not defined by C++ standard
23
it's supported by many implementations.
24
See bug 236036 and discussion: http://lists.mplayerhq.hu/pipermail/ffmpeg-devel/2010-May/088074.html
26
#define __STDC_CONSTANT_MACROS
27
#ifdef NEWFFMPEGAVCODECPATH
28
#include <libavcodec/avcodec.h>
29
#include <libavformat/avformat.h>
31
#include <ffmpeg/avcodec.h>
32
#include <ffmpeg/avformat.h>
41
#if LIBAVFORMAT_BUILD < 4629
42
#define FFMPEG_BUILD_PRE_4629
46
K3bFFMpegWrapper* K3bFFMpegWrapper::s_instance = 0;
49
class K3bFFMpegFile::Private
52
::AVFormatContext* formatContext;
57
// for decoding. ffmpeg requires 16-byte alignment.
58
char outputBuffer[AVCODEC_MAX_AUDIO_FRAME_SIZE + 15];
59
char* alignedOutputBuffer;
60
char* outputBufferPos;
68
K3bFFMpegFile::K3bFFMpegFile( const QString& filename )
69
: m_filename(filename)
74
int offset = 0x10 - (reinterpret_cast<intptr_t>(&d->outputBuffer) & 0xf);
75
d->alignedOutputBuffer = &d->outputBuffer[offset];
79
K3bFFMpegFile::~K3bFFMpegFile()
86
bool K3bFFMpegFile::open()
91
int err = ::av_open_input_file( &d->formatContext, m_filename.toLocal8Bit(), 0, 0, 0 );
93
kDebug() << "(K3bFFMpegFile) unable to open " << m_filename << " with error " << err;
97
// analyze the streams
98
::av_find_stream_info( d->formatContext );
100
// we only handle files containing one audio stream
101
if( d->formatContext->nb_streams != 1 ) {
102
kDebug() << "(K3bFFMpegFile) more than one stream in " << m_filename;
107
#ifdef FFMPEG_BUILD_PRE_4629
108
::AVCodecContext* codecContext = &d->formatContext->streams[0]->codec;
110
::AVCodecContext* codecContext = d->formatContext->streams[0]->codec;
112
if( codecContext->codec_type != CODEC_TYPE_AUDIO ) {
113
kDebug() << "(K3bFFMpegFile) not a simple audio stream: " << m_filename;
118
d->codec = ::avcodec_find_decoder(codecContext->codec_id);
120
kDebug() << "(K3bFFMpegFile) no codec found for " << m_filename;
124
// open the codec on our context
125
kDebug() << "(K3bFFMpegFile) found codec for " << m_filename;
126
if( ::avcodec_open( codecContext, d->codec ) < 0 ) {
127
kDebug() << "(K3bFFMpegDecoderFactory) could not open codec.";
131
// determine the length of the stream
132
d->length = K3b::Msf::fromSeconds( (double)d->formatContext->duration / (double)AV_TIME_BASE );
134
if( d->length == 0 ) {
135
kDebug() << "(K3bFFMpegDecoderFactory) invalid length.";
139
// dump some debugging info
140
::dump_format( d->formatContext, 0, m_filename.toLocal8Bit(), 0 );
146
void K3bFFMpegFile::close()
148
d->outputBufferSize = 0;
153
#ifdef FFMPEG_BUILD_PRE_4629
154
::avcodec_close( &d->formatContext->streams[0]->codec );
156
::avcodec_close( d->formatContext->streams[0]->codec );
161
if( d->formatContext ) {
162
::av_close_input_file( d->formatContext );
163
d->formatContext = 0;
168
K3b::Msf K3bFFMpegFile::length() const
174
int K3bFFMpegFile::sampleRate() const
176
#ifdef FFMPEG_BUILD_PRE_4629
177
return d->formatContext->streams[0]->codec.sample_rate;
179
return d->formatContext->streams[0]->codec->sample_rate;
184
int K3bFFMpegFile::channels() const
186
#ifdef FFMPEG_BUILD_PRE_4629
187
return d->formatContext->streams[0]->codec.channels;
189
return d->formatContext->streams[0]->codec->channels;
194
int K3bFFMpegFile::type() const
196
#ifdef FFMPEG_BUILD_PRE_4629
197
return d->formatContext->streams[0]->codec.codec_id;
199
return d->formatContext->streams[0]->codec->codec_id;
204
QString K3bFFMpegFile::typeComment() const
208
return i18n("Windows Media v1");
210
return i18n("Windows Media v2");
211
#if LIBAVCODEC_VERSION_MAJOR < 52
212
case CODEC_ID_MP3LAME:
216
return i18n("MPEG 1 Layer III");
218
return i18n("Advanced Audio Coding (AAC)");
220
return QString::fromLocal8Bit( d->codec->name );
225
QString K3bFFMpegFile::title() const
227
// FIXME: is this UTF8 or something??
228
if( d->formatContext->title[0] != '\0' )
229
return QString::fromLocal8Bit( d->formatContext->title );
235
QString K3bFFMpegFile::author() const
237
// FIXME: is this UTF8 or something??
238
if( d->formatContext->author[0] != '\0' )
239
return QString::fromLocal8Bit( d->formatContext->author );
245
QString K3bFFMpegFile::comment() const
247
// FIXME: is this UTF8 or something??
248
if( d->formatContext->comment[0] != '\0' )
249
return QString::fromLocal8Bit( d->formatContext->comment );
255
int K3bFFMpegFile::read( char* buf, int bufLen )
257
int ret = fillOutputBuffer();
262
int len = qMin(bufLen, d->outputBufferSize);
263
::memcpy( buf, d->outputBufferPos, len );
265
// TODO: only swap if needed
266
for( int i = 0; i < len-1; i+=2 ) {
272
d->outputBufferPos += len;
273
d->outputBufferSize -= len;
278
// fill d->packetData with data to decode
279
int K3bFFMpegFile::readPacket()
281
if( d->packetSize <= 0 ) {
282
::av_init_packet( &d->packet );
284
if( ::av_read_frame( d->formatContext, &d->packet ) < 0 ) {
287
d->packetSize = d->packet.size;
288
d->packetData = d->packet.data;
291
return d->packetSize;
295
// decode data in d->packetData and fill d->outputBuffer
296
int K3bFFMpegFile::fillOutputBuffer()
298
// decode if the output buffer is empty
299
if( d->outputBufferSize <= 0 ) {
301
// make sure we have data to decode
302
if( readPacket() == 0 ) {
306
d->outputBufferPos = d->alignedOutputBuffer;
307
d->outputBufferSize = AVCODEC_MAX_AUDIO_FRAME_SIZE;
309
#if LIBAVCODEC_VERSION_MAJOR < 52
310
int len = ::avcodec_decode_audio(
312
int len = ::avcodec_decode_audio2(
314
#ifdef FFMPEG_BUILD_PRE_4629
315
&d->formatContext->streams[0]->codec,
317
d->formatContext->streams[0]->codec,
319
(short*)d->alignedOutputBuffer,
320
&d->outputBufferSize,
321
d->packetData, d->packetSize );
323
if( d->packetSize <= 0 || len < 0 )
324
::av_free_packet( &d->packet );
326
kDebug() << "(K3bFFMpegFile) decoding failed for " << m_filename;
330
d->packetSize -= len;
331
d->packetData += len;
334
// if it is still empty try again
335
if( d->outputBufferSize <= 0 )
336
return fillOutputBuffer();
338
return d->outputBufferSize;
342
bool K3bFFMpegFile::seek( const K3b::Msf& msf )
344
d->outputBufferSize = 0;
347
double seconds = (double)msf.totalFrames()/75.0;
348
quint64 timestamp = (quint64)(seconds * (double)AV_TIME_BASE);
350
// FIXME: do we really need the start_time and why?
351
#if LIBAVFORMAT_BUILD >= 4619
352
return ( ::av_seek_frame( d->formatContext, -1, timestamp + d->formatContext->start_time, 0 ) >= 0 );
354
return ( ::av_seek_frame( d->formatContext, -1, timestamp + d->formatContext->start_time ) >= 0 );
363
K3bFFMpegWrapper::K3bFFMpegWrapper()
369
K3bFFMpegWrapper::~K3bFFMpegWrapper()
375
K3bFFMpegWrapper* K3bFFMpegWrapper::instance()
378
s_instance = new K3bFFMpegWrapper();
385
K3bFFMpegFile* K3bFFMpegWrapper::open( const QString& filename ) const
387
K3bFFMpegFile* file = new K3bFFMpegFile( filename );
389
#ifndef K3B_FFMPEG_ALL_CODECS
391
// only allow tested formats. ffmpeg seems not to be too reliable with every format.
392
// mp3 being one of them sadly. Most importantly: allow the libsndfile decoder to do
395
if( file->type() == CODEC_ID_WMAV1 ||
396
file->type() == CODEC_ID_WMAV2 ||
397
file->type() == CODEC_ID_AAC )