$OpenBSD: patch-plugins_decoder_ffmpeg_k3bffmpegwrapper_cpp,v 1.4 2013/01/19 09:51:30 brad Exp $

Update for newer FFmpeg API.

--- plugins/decoder/ffmpeg/k3bffmpegwrapper.cpp.orig	Fri Nov  2 05:55:03 2007
+++ plugins/decoder/ffmpeg/k3bffmpegwrapper.cpp	Fri Jan 18 17:15:18 2013
@@ -18,8 +18,8 @@
 #include "k3bffmpegwrapper.h"
 
 extern "C" {
-#include <ffmpeg/avcodec.h>
-#include <ffmpeg/avformat.h>
+#include <libavcodec/avcodec.h>
+#include <libavformat/avformat.h>
 }
 
 #include <string.h>
@@ -43,8 +43,9 @@ class K3bFFMpegFile::Private (public)
 
   K3b::Msf length;
 
-  // for decoding
-  char outputBuffer[AVCODEC_MAX_AUDIO_FRAME_SIZE];
+  // for decoding. ffmpeg requires 16-byte alignment.
+  char outputBuffer[AVCODEC_MAX_AUDIO_FRAME_SIZE + 15];
+  char* alignedOutputBuffer;
   char* outputBufferPos;
   int outputBufferSize;
   AVPacket packet;
@@ -59,6 +60,8 @@ K3bFFMpegFile::K3bFFMpegFile( const QString& filename 
   d = new Private;
   d->formatContext = 0;
   d->codec = 0;
+  int offset = 0x10 - (reinterpret_cast<intptr_t>(&d->outputBuffer) & 0xf);
+  d->alignedOutputBuffer = &d->outputBuffer[offset];
 }
 
 
@@ -74,14 +77,22 @@ bool K3bFFMpegFile::open()
   close();
 
   // open the file
+# if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(53, 2, 0)
+  int err = avformat_open_input( &d->formatContext, m_filename.local8Bit(), 0, 0);
+# else
   int err = av_open_input_file( &d->formatContext, m_filename.local8Bit(), 0, 0, 0 );
+# endif
   if( err < 0 ) {
     kdDebug() << "(K3bFFMpegFile) unable to open " << m_filename << " with error " << err << endl;
     return false;
   }
 
   // analyze the streams
+# if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(53, 6, 0)
+  avformat_find_stream_info( d->formatContext, NULL );
+# else
   av_find_stream_info( d->formatContext );
+# endif
 
   // we only handle files containing one audio stream
   if( d->formatContext->nb_streams != 1 ) {
@@ -95,7 +106,14 @@ bool K3bFFMpegFile::open()
 #else
   AVCodecContext* codecContext =  d->formatContext->streams[0]->codec;
 #endif
-  if( codecContext->codec_type != CODEC_TYPE_AUDIO ) {
+
+  if( codecContext->codec_type != 
+#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(52, 64, 0)
+        AVMEDIA_TYPE_AUDIO)
+#else
+        CODEC_TYPE_AUDIO)
+#endif
+  {
     kdDebug() << "(K3bFFMpegFile) not a simple audio stream: " << m_filename << endl;
     return false;
   }
@@ -109,7 +127,13 @@ bool K3bFFMpegFile::open()
 
   // open the codec on our context
   kdDebug() << "(K3bFFMpegFile) found codec for " << m_filename << endl;
-  if( avcodec_open( codecContext, d->codec ) < 0 ) {
+  if(
+#    if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(53, 8, 0)
+     avcodec_open2( codecContext, d->codec, NULL ) < 0
+#    else
+     avcodec_open( codecContext, d->codec ) < 0
+#    endif
+    ) {
     kdDebug() << "(K3bFFMpegDecoderFactory) could not open codec." << endl;
     return false;
   }
@@ -123,7 +147,11 @@ bool K3bFFMpegFile::open()
   }
 
   // dump some debugging info
+# if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(52, 101, 0)
+  av_dump_format( d->formatContext, 0, m_filename.local8Bit(), 0 );
+# else
   dump_format( d->formatContext, 0, m_filename.local8Bit(), 0 );
+# endif
 
   return true;
 }
@@ -145,7 +173,11 @@ void K3bFFMpegFile::close()
   }
 
   if( d->formatContext ) {
+#   if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(53, 17, 0)
+    avformat_close_input( &d->formatContext );
+#   else
     av_close_input_file( d->formatContext );
+#   endif
     d->formatContext = 0;
   }
 }
@@ -194,7 +226,11 @@ QString K3bFFMpegFile::typeComment() const
     return i18n("Windows Media v1");
   case CODEC_ID_WMAV2:
     return i18n("Windows Media v2");
+#if LIBAVCODEC_VERSION_MAJOR < 52
   case CODEC_ID_MP3LAME:
+#else
+  case CODEC_ID_MP3:
+#endif
     return i18n("MPEG 1 Layer III");
   case CODEC_ID_AAC:
     return i18n("Advanced Audio Coding (AAC)");
@@ -207,30 +243,39 @@ QString K3bFFMpegFile::typeComment() const
 QString K3bFFMpegFile::title() const
 {
   // FIXME: is this UTF8 or something??
-  if( d->formatContext->title[0] != '\0' )
-    return QString::fromLocal8Bit( d->formatContext->title );
+  AVDictionaryEntry *ade = av_dict_get( d->formatContext->metadata, "TITLE", NULL, 0 );
+  if( ade == NULL )
+    return QString();
+  if( ade->value != '\0' )
+    return QString::fromLocal8Bit( ade->value );
   else
-    return QString::null;
+    return QString();
 }
 
 
 QString K3bFFMpegFile::author() const
 {
   // FIXME: is this UTF8 or something??
-  if( d->formatContext->author[0] != '\0' )
-    return QString::fromLocal8Bit( d->formatContext->author );
+  AVDictionaryEntry *ade = av_dict_get( d->formatContext->metadata, "ARTIST", NULL, 0 );
+  if( ade == NULL )
+    return QString();
+  if( ade->value != '\0' )
+    return QString::fromLocal8Bit( ade->value );
   else
-    return QString::null;
+    return QString();
 }
 
 
 QString K3bFFMpegFile::comment() const
 {
   // FIXME: is this UTF8 or something??
-  if( d->formatContext->comment[0] != '\0' )
-    return QString::fromLocal8Bit( d->formatContext->comment );
+  AVDictionaryEntry *ade = av_dict_get( d->formatContext->metadata, "COMMENT", NULL, 0 );
+  if( ade == NULL )
+    return QString();
+  if( ade->value != '\0' )
+    return QString::fromLocal8Bit( ade->value );
   else
-    return QString::null;
+    return QString();
 }
 
 
@@ -285,15 +330,32 @@ int K3bFFMpegFile::fillOutputBuffer()
       return 0;
     }
 
-    d->outputBufferPos = d->outputBuffer;
+    d->outputBufferPos = d->alignedOutputBuffer;
+    d->outputBufferSize = AVCODEC_MAX_AUDIO_FRAME_SIZE;
 
+#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(52, 64, 0)
+    AVPacket avp;
+    av_init_packet( &avp );
+    avp.data = d->packetData;
+    avp.size = d->packetSize;
+#   if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(53, 25, 0)
+      int len = avcodec_decode_audio4( d->formatContext->streams[0]->codec,
+                                          (AVFrame*)d->outputBuffer, &d->outputBufferSize,
+                                          &avp );
+#   else
+      int len = avcodec_decode_audio3( d->formatContext->streams[0]->codec,
+                                          (short*)d->outputBuffer, &d->outputBufferSize,
+                                          &avp );
+#   endif
+#else
 #ifdef FFMPEG_BUILD_PRE_4629
-    int len = avcodec_decode_audio( &d->formatContext->streams[0]->codec,
+    int len = avcodec_decode_audio2( &d->formatContext->streams[0]->codec,
 #else
-    int len = avcodec_decode_audio( d->formatContext->streams[0]->codec,
+    int len = avcodec_decode_audio2( d->formatContext->streams[0]->codec,
 #endif
 				    (short*)d->outputBuffer, &d->outputBufferSize,
 				    d->packetData, d->packetSize );
+#endif
 
     d->packetSize -= len;
     d->packetData += len;
