From 116655c7c57d073c7410df79cc3b8b10bd36fc94 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Fri, 27 Feb 2009 17:00:28 +0000 Subject: [PATCH] Ported across from using boost pointers, and prepped for integration of audio interface into core OSG --- src/osgPlugins/ffmpeg/AudioSinkInterface.hpp | 6 +-- src/osgPlugins/ffmpeg/FFmpegDecoder.cpp | 2 +- src/osgPlugins/ffmpeg/FFmpegDecoder.hpp | 48 ++++++++++++++++++-- src/osgPlugins/ffmpeg/FFmpegDecoderAudio.cpp | 2 +- src/osgPlugins/ffmpeg/FFmpegDecoderAudio.hpp | 4 +- src/osgPlugins/ffmpeg/FFmpegDecoderVideo.cpp | 4 +- src/osgPlugins/ffmpeg/FFmpegDecoderVideo.hpp | 40 +++++++++++++++- src/osgPlugins/ffmpeg/FFmpegImageStream.cpp | 3 +- src/osgPlugins/ffmpeg/FFmpegImageStream.hpp | 3 +- 9 files changed, 96 insertions(+), 16 deletions(-) diff --git a/src/osgPlugins/ffmpeg/AudioSinkInterface.hpp b/src/osgPlugins/ffmpeg/AudioSinkInterface.hpp index ed7fd0f31..8279fd27a 100644 --- a/src/osgPlugins/ffmpeg/AudioSinkInterface.hpp +++ b/src/osgPlugins/ffmpeg/AudioSinkInterface.hpp @@ -1,12 +1,12 @@ -#ifndef HEADER_GUARD_OSGFFMPEG_AUDIO_SINK_INTERFACE_H -#define HEADER_GUARD_OSGFFMPEG_AUDIO_SINK_INTERFACE_H +#ifndef OSG_AUDIOSINKINTERFACE_H +#define OSG_AUDIOSINKINTERFACE_H #include -namespace osgFFmpeg +namespace osg { class AudioSinkInterface : public osg::Object diff --git a/src/osgPlugins/ffmpeg/FFmpegDecoder.cpp b/src/osgPlugins/ffmpeg/FFmpegDecoder.cpp index 8b4a008ed..6c74fe443 100644 --- a/src/osgPlugins/ffmpeg/FFmpegDecoder.cpp +++ b/src/osgPlugins/ffmpeg/FFmpegDecoder.cpp @@ -46,7 +46,7 @@ bool FFmpegDecoder::open(const std::string & filename) if (av_open_input_file(&p_format_context, filename.c_str(), 0, 0, 0) != 0) throw std::runtime_error("av_open_input_file() failed"); - m_format_context.reset(p_format_context, av_close_input_file); + m_format_context.reset(p_format_context); // Retrieve stream info if (av_find_stream_info(p_format_context) < 0) diff --git a/src/osgPlugins/ffmpeg/FFmpegDecoder.hpp b/src/osgPlugins/ffmpeg/FFmpegDecoder.hpp index 7eda082dc..917db6de8 100644 --- a/src/osgPlugins/ffmpeg/FFmpegDecoder.hpp +++ b/src/osgPlugins/ffmpeg/FFmpegDecoder.hpp @@ -2,15 +2,58 @@ #ifndef HEADER_GUARD_OSGFFMPEG_FFMPEG_DECODER_H #define HEADER_GUARD_OSGFFMPEG_FFMPEG_DECODER_H -#include - #include "FFmpegDecoderAudio.hpp" #include "FFmpegDecoderVideo.hpp" +#include namespace osgFFmpeg { +class FormatContextPtr +{ + public: + + typedef AVFormatContext T; + + explicit FormatContextPtr() : _ptr(0) {} + explicit FormatContextPtr(T* ptr) : _ptr(ptr) {} + + ~FormatContextPtr() + { + cleanup(); + } + + T* get() { return _ptr; } + + T * operator-> () const // never throws + { + return _ptr; + } + + void reset(T* ptr) + { + if (ptr==_ptr) return; + cleanup(); + _ptr = ptr; + } + + void cleanup() + { + if (_ptr) + { + osg::notify(osg::NOTICE)<<"Calling av_close_input_file("<<_ptr<<")"< FormatContextPtr; typedef BoundedMessageQueue PacketQueue; void findAudioStream(); diff --git a/src/osgPlugins/ffmpeg/FFmpegDecoderAudio.cpp b/src/osgPlugins/ffmpeg/FFmpegDecoderAudio.cpp index 03e0a048c..b5804deac 100644 --- a/src/osgPlugins/ffmpeg/FFmpegDecoderAudio.cpp +++ b/src/osgPlugins/ffmpeg/FFmpegDecoderAudio.cpp @@ -106,7 +106,7 @@ void FFmpegDecoderAudio::run() -void FFmpegDecoderAudio::setAudioSink(osg::ref_ptr audio_sink) +void FFmpegDecoderAudio::setAudioSink(osg::ref_ptr audio_sink) { // The FFmpegDecoderAudio object takes the responsability of destroying the audio_sink. m_audio_sink = audio_sink; diff --git a/src/osgPlugins/ffmpeg/FFmpegDecoderAudio.hpp b/src/osgPlugins/ffmpeg/FFmpegDecoderAudio.hpp index afc84bafe..47d9a4ae8 100644 --- a/src/osgPlugins/ffmpeg/FFmpegDecoderAudio.hpp +++ b/src/osgPlugins/ffmpeg/FFmpegDecoderAudio.hpp @@ -30,7 +30,7 @@ public: void open(AVStream * stream); virtual void run(); - void setAudioSink(osg::ref_ptr audio_sink); + void setAudioSink(osg::ref_ptr audio_sink); void fillBuffer(void * buffer, size_t size); bool validContext() const; @@ -41,7 +41,7 @@ public: private: //typedef boost::shared_ptr FramePtr; - typedef osg::ref_ptr SinkPtr; + typedef osg::ref_ptr SinkPtr; typedef std::vector Buffer; void decodeLoop(); diff --git a/src/osgPlugins/ffmpeg/FFmpegDecoderVideo.cpp b/src/osgPlugins/ffmpeg/FFmpegDecoderVideo.cpp index 114bb13e4..75a42ed2b 100644 --- a/src/osgPlugins/ffmpeg/FFmpegDecoderVideo.cpp +++ b/src/osgPlugins/ffmpeg/FFmpegDecoderVideo.cpp @@ -72,10 +72,10 @@ void FFmpegDecoderVideo::open(AVStream * const stream) throw std::runtime_error("avcodec_open() failed"); // Allocate video frame - m_frame.reset(avcodec_alloc_frame(), av_free); + m_frame.reset(avcodec_alloc_frame()); // Allocate converted RGB frame - m_frame_rgba.reset(avcodec_alloc_frame(), av_free); + m_frame_rgba.reset(avcodec_alloc_frame()); m_buffer_rgba.resize(avpicture_get_size(PIX_FMT_RGB32, width(), height())); m_buffer_rgba_public.resize(m_buffer_rgba.size()); diff --git a/src/osgPlugins/ffmpeg/FFmpegDecoderVideo.hpp b/src/osgPlugins/ffmpeg/FFmpegDecoderVideo.hpp index 0fa6d60d3..2d1a0ef3e 100644 --- a/src/osgPlugins/ffmpeg/FFmpegDecoderVideo.hpp +++ b/src/osgPlugins/ffmpeg/FFmpegDecoderVideo.hpp @@ -13,7 +13,46 @@ namespace osgFFmpeg { +class FramePtr +{ + public: + + typedef AVFrame T; + + explicit FramePtr() : _ptr(0) {} + explicit FramePtr(T* ptr) : _ptr(ptr) {} + + ~FramePtr() + { + cleanup(); + } + + T* get() { return _ptr; } + T * operator-> () const // never throws + { + return _ptr; + } + + void reset(T* ptr) + { + if (ptr==_ptr) return; + cleanup(); + _ptr = ptr; + } + + void cleanup() + { + if (_ptr) av_free(_ptr); + _ptr = 0; + } + + + + protected: + + T* _ptr; +}; class FFmpegDecoderVideo : public OpenThreads::Thread { @@ -40,7 +79,6 @@ public: private: - typedef boost::shared_ptr FramePtr; typedef std::vector Buffer; void decodeLoop(); diff --git a/src/osgPlugins/ffmpeg/FFmpegImageStream.cpp b/src/osgPlugins/ffmpeg/FFmpegImageStream.cpp index b952e66a2..2ac08a328 100644 --- a/src/osgPlugins/ffmpeg/FFmpegImageStream.cpp +++ b/src/osgPlugins/ffmpeg/FFmpegImageStream.cpp @@ -121,13 +121,12 @@ void FFmpegImageStream::quit(bool waitForThreadToExit) -void FFmpegImageStream::setAudioSink(osg::ref_ptr audio_sink) +void FFmpegImageStream::setAudioSink(osg::AudioSinkInterface* audio_sink) { m_decoder->audio_decoder().setAudioSink(audio_sink); } - void FFmpegImageStream::fillAudioBuffer(void * const buffer, const size_t size) { m_decoder->audio_decoder().fillBuffer(buffer, size); diff --git a/src/osgPlugins/ffmpeg/FFmpegImageStream.hpp b/src/osgPlugins/ffmpeg/FFmpegImageStream.hpp index 8c615b42a..5211cbfaf 100644 --- a/src/osgPlugins/ffmpeg/FFmpegImageStream.hpp +++ b/src/osgPlugins/ffmpeg/FFmpegImageStream.hpp @@ -53,7 +53,8 @@ namespace osgFFmpeg virtual void rewind(); virtual void quit(bool waitForThreadToExit = true); - void setAudioSink(osg::ref_ptr audio_sink); + virtual void setAudioSink(osg::AudioSinkInterface* audio_sink); + void fillAudioBuffer(void * const buffer, const size_t size); double duration() const;