$OpenBSD: patch-plugins_decoder_ffmpeg_k3bffmpegwrapper_cpp,v 1.5 2014/04/23 20:14:40 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	Wed Apr 23 16:07:07 2014
@@ -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>
@@ -31,6 +31,11 @@ extern "C" {
 #define FFMPEG_BUILD_PRE_4629
 #endif
 
+#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(52, 64, 0)
+#define AVMEDIA_TYPE_AUDIO CODEC_TYPE_AUDIO
+#define AVMEDIA_TYPE_VIDEO CODEC_TYPE_VIDEO
+#define AVMEDIA_TYPE_SUBTITLE CODEC_TYPE_SUBTITLE
+#endif
 
 K3bFFMpegWrapper* K3bFFMpegWrapper::s_instance = 0;
 
@@ -44,7 +49,7 @@ class K3bFFMpegFile::Private (public)
   K3b::Msf length;
 
   // for decoding
-  char outputBuffer[AVCODEC_MAX_AUDIO_FRAME_SIZE];
+  char outputBuffer[192000];
   char* outputBufferPos;
   int outputBufferSize;
   AVPacket packet;
@@ -74,14 +79,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 +108,9 @@ bool K3bFFMpegFile::open()
 #else
   AVCodecContext* codecContext =  d->formatContext->streams[0]->codec;
 #endif
-  if( codecContext->codec_type != CODEC_TYPE_AUDIO ) {
+
+  if( codecContext->codec_type != AVMEDIA_TYPE_AUDIO)
+  {
     kdDebug() << "(K3bFFMpegFile) not a simple audio stream: " << m_filename << endl;
     return false;
   }
@@ -109,7 +124,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 +144,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 +170,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 +223,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 +240,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();
 }
 
 
@@ -287,13 +329,29 @@ int K3bFFMpegFile::fillOutputBuffer()
 
     d->outputBufferPos = d->outputBuffer;
 
+#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;
