From e1629299045c79455a238759644548810ad89aa7 Mon Sep 17 00:00:00 2001
From: Alexis Ballier <aballier@gentoo.org>
Date: Wed, 13 Feb 2013 14:38:44 -0300
Subject: [PATCH] Fix build with latest FFmpeg.

---
 libmedia/ffmpeg/AudioDecoderFfmpeg.cpp | 13 +++++--------
 libmedia/ffmpeg/MediaParserFfmpeg.cpp  | 34 ++++++++++++++--------------------
 libmedia/ffmpeg/MediaParserFfmpeg.h    |  4 ++--
 libmedia/ffmpeg/VideoDecoderFfmpeg.cpp |  4 +---
 4 files changed, 22 insertions(+), 33 deletions(-)

diff --git a/libmedia/ffmpeg/AudioDecoderFfmpeg.cpp b/libmedia/ffmpeg/AudioDecoderFfmpeg.cpp
index 067e418..23424f3 100644
--- a/libmedia/ffmpeg/AudioDecoderFfmpeg.cpp
+++ b/libmedia/ffmpeg/AudioDecoderFfmpeg.cpp
@@ -84,9 +84,7 @@ AudioDecoderFfmpeg::~AudioDecoderFfmpeg()
 
 void AudioDecoderFfmpeg::setup(SoundInfo& info)
 {
-    // Init the avdecoder-decoder
-    avcodec_init();
-    avcodec_register_all();// change this to only register need codec?
+    avcodec_register_all();// change this to only register need codec? no - it is required for initializing the library
 
     enum CodecID codec_id;
 
@@ -158,14 +156,14 @@ void AudioDecoderFfmpeg::setup(SoundInfo& info)
             case CODEC_ID_PCM_U16LE:
                 _audioCodecCtx->channels = (info.isStereo() ? 2 : 1);
                 _audioCodecCtx->sample_rate = info.getSampleRate();
-                _audioCodecCtx->sample_fmt = SAMPLE_FMT_S16; // ?! arbitrary ?
+                _audioCodecCtx->sample_fmt = AV_SAMPLE_FMT_S16; // ?! arbitrary ?
                 _audioCodecCtx->frame_size = 1; 
                 break;
 
             default:
                 _audioCodecCtx->channels = (info.isStereo() ? 2 : 1);
                 _audioCodecCtx->sample_rate = info.getSampleRate();
-                _audioCodecCtx->sample_fmt = SAMPLE_FMT_S16; // ?! arbitrary ?
+                _audioCodecCtx->sample_fmt = AV_SAMPLE_FMT_S16; // ?! arbitrary ?
                 break;
     }
 }
@@ -173,8 +171,7 @@ void AudioDecoderFfmpeg::setup(SoundInfo& info)
 void AudioDecoderFfmpeg::setup(const AudioInfo& info)
 {
     // Init the avdecoder-decoder
-    avcodec_init();
-    avcodec_register_all();// change this to only register need codec?
+    avcodec_register_all();// change this to only register need codec? no - it is required for initializing the library
 
     enum CodecID codec_id = CODEC_ID_NONE;
 
@@ -297,7 +294,7 @@ void AudioDecoderFfmpeg::setup(const AudioInfo& info)
                 _audioCodecCtx->channels = (info.stereo ? 2 : 1);
                 _audioCodecCtx->sample_rate = info.sampleRate;
                 // was commented out (why?):
-                _audioCodecCtx->sample_fmt = SAMPLE_FMT_S16; 
+                _audioCodecCtx->sample_fmt = AV_SAMPLE_FMT_S16; 
                 break;
     }
 
diff --git a/libmedia/ffmpeg/MediaParserFfmpeg.cpp b/libmedia/ffmpeg/MediaParserFfmpeg.cpp
index d6e6902..6e120e2 100644
--- a/libmedia/ffmpeg/MediaParserFfmpeg.cpp
+++ b/libmedia/ffmpeg/MediaParserFfmpeg.cpp
@@ -344,8 +344,6 @@ MediaParserFfmpeg::initializeParser()
 {
     av_register_all(); // TODO: needs to be invoked only once ?
 
-    _byteIOCxt.buffer = NULL;
-    
     _inputFmt = probeStream();
 
 #ifdef GNASH_ALLOW_VCODEC_ENV	
@@ -366,7 +364,7 @@ MediaParserFfmpeg::initializeParser()
     // which isn't needed.
     _byteIOBuffer.reset(new unsigned char[byteIOBufferSize]);
 
-    init_put_byte(&_byteIOCxt,
+    _avIOCxt = avio_alloc_context(
 		  _byteIOBuffer.get(), // buffer
 		  byteIOBufferSize, // buffer size
 		  0, // write flags
@@ -376,7 +374,7 @@ MediaParserFfmpeg::initializeParser()
 		  MediaParserFfmpeg::seekMediaWrapper // seeker callback
 		  );
     
-    _byteIOCxt.is_streamed = 1;
+    _avIOCxt->seekable = 0;
 
 #if !defined(LIBAVCODEC_VERSION_MAJOR) || LIBAVCODEC_VERSION_MAJOR < 52
     // Needed for Lenny.
@@ -386,13 +384,9 @@ MediaParserFfmpeg::initializeParser()
 #endif
 
     assert(_formatCtx);
+    _formatCtx->pb = _avIOCxt;
 
-    // Otherwise av_open_input_stream will reallocate the context.
-    AVFormatParameters ap;
-    std::memset(&ap, 0, sizeof ap);
-    ap.prealloced_context = 1;
-
-    if (av_open_input_stream(&_formatCtx, &_byteIOCxt, "", _inputFmt, &ap) < 0)
+    if (avformat_open_input(&_formatCtx, "", _inputFmt, NULL) < 0)
     {
         throw IOException("MediaParserFfmpeg couldn't open input stream");
     }
@@ -400,10 +394,10 @@ MediaParserFfmpeg::initializeParser()
 #if defined(LIBAVCODEC_VERSION_MAJOR) && LIBAVCODEC_VERSION_MAJOR >= 52
     // Note: in at least some versions of ffmpeg, av_open_input_stream does
     // not parse metadata; not sure why.
-    AVMetadata* md = _formatCtx->metadata;
+    AVDictionary* md = _formatCtx->metadata;
     if (md) {
-        AVMetadataTag* tag = av_metadata_get(md, "album", 0,
-                AV_METADATA_MATCH_CASE);
+        AVDictionaryEntry* tag = av_dict_get(md, "album", 0,
+                AV_DICT_MATCH_CASE);
         if (tag && tag->value) {
             setId3Info(&Id3Info::album, std::string(tag->value),
                     _id3Object);
@@ -620,27 +614,27 @@ MediaParserFfmpeg::seekMedia(boost::int64_t offset, int whence)
 }
 
 boost::uint16_t
-MediaParserFfmpeg::SampleFormatToSampleSize(SampleFormat fmt)
+MediaParserFfmpeg::SampleFormatToSampleSize(AVSampleFormat fmt)
 {
 	switch (fmt)
 	{
-		case SAMPLE_FMT_U8: // unsigned 8 bits
+		case AV_SAMPLE_FMT_U8: // unsigned 8 bits
 			return 1;
 
-		case SAMPLE_FMT_S16: // signed 16 bits
-		case SAMPLE_FMT_FLT: // float
+		case AV_SAMPLE_FMT_S16: // signed 16 bits
+		case AV_SAMPLE_FMT_FLT: // float
 			return 2;
 
 #if !defined (LIBAVCODEC_VERSION_MAJOR) || LIBAVCODEC_VERSION_MAJOR < 52
 // Was dropped for version 52.0.0
-		case SAMPLE_FMT_S24: // signed 24 bits
+		case AV_SAMPLE_FMT_S24: // signed 24 bits
 			return 3;
 #endif
 
-		case SAMPLE_FMT_S32: // signed 32 bits
+		case AV_SAMPLE_FMT_S32: // signed 32 bits
 			return 4;
 
-		case SAMPLE_FMT_NONE:
+		case AV_SAMPLE_FMT_NONE:
 		default:
 			return 8; // arbitrary value
 	}
diff --git a/libmedia/ffmpeg/MediaParserFfmpeg.h b/libmedia/ffmpeg/MediaParserFfmpeg.h
index 4e410d8..2d817c8 100644
--- a/libmedia/ffmpeg/MediaParserFfmpeg.h
+++ b/libmedia/ffmpeg/MediaParserFfmpeg.h
@@ -154,7 +154,7 @@ private:
 	AVStream* _audioStream;
 
 	/// ?
-	ByteIOContext _byteIOCxt;
+        AVIOContext* _avIOCxt;
 
 	/// Size of the ByteIO context buffer
 	//
@@ -172,7 +172,7 @@ private:
 	//
 	/// TODO: move somewhere in ffmpeg utils..
 	///
-	boost::uint16_t SampleFormatToSampleSize(SampleFormat fmt);
+	boost::uint16_t SampleFormatToSampleSize(AVSampleFormat fmt);
 
 	/// Make an EncodedVideoFrame from an AVPacket and push to buffer
 	//
diff --git a/libmedia/ffmpeg/VideoDecoderFfmpeg.cpp b/libmedia/ffmpeg/VideoDecoderFfmpeg.cpp
index 40a5c80..eb187a0 100644
--- a/libmedia/ffmpeg/VideoDecoderFfmpeg.cpp
+++ b/libmedia/ffmpeg/VideoDecoderFfmpeg.cpp
@@ -171,8 +171,7 @@ VideoDecoderFfmpeg::init(enum CodecID codecId, int /*width*/, int /*height*/,
         boost::uint8_t* extradata, int extradataSize)
 {
     // Init the avdecoder-decoder
-    avcodec_init();
-    avcodec_register_all();// change this to only register need codec?
+    avcodec_register_all();// change this to only register need codec? no - it is required for initializing the library
 
     _videoCodec = avcodec_find_decoder(codecId); 
 
@@ -529,7 +528,6 @@ get_buffer(AVCodecContext* avctx, AVFrame* pic)
 
     static unsigned int pic_num = 0;
     pic->type = FF_BUFFER_TYPE_USER;
-    pic->age  = ++pic_num - surface->getPicNum();
     surface->setPicNum(pic_num);
     return 0;
 #endif
-- 
1.8.1.2

