Files
OpenSceneGraph/src/osgPlugins/ffmpeg/FFmpegDecoder.hpp
Robert Osfield 29eb65c77d From David Longest, "I have updated the FFmpeg plugin to support the 1.0 release version of FFmpeg. The files attached were modified in order to facilitate the update. Below are the details for all changes made.
Header update

FindFFmpeg.cmake has been changed in order to support the new header include format for FFmpeg. In the 1.0 release, a new file had been added with the name “time.h” in the avutil library. The previous method of adding includes caused conflicts with the ANSI C “time.h” file. Now the include directive will only use the main include folder. All files using the old include format have been updated to reflect the change.



Added __STDC_CONSTANT_MACROS define to CMakeLists.txt

Since there is no guarantee that FFmpegHeaders.hpp will be included before stdint.h is included, the define has been moved from FFmpegHeaders.hpp to be part of the CMakeLists.txt for the FFmpeg plugin. This will allow the define to work on all compilers regardless of include order.



Replaced AVFormatParameters with AVDictionary

AVFormatParameters is no longer supported in FFmpeg and has been replaced with a key/value map of strings for each setting. FFmpegParameters and FFmpegDecoder has been updated to reflect this.



Replaced av_open_input_file with avformat_open_input

FFmpeg now opens files using avformat_open_input. Since the av_open_input_file method is deprecated, the FFmpegDecoder class has been updated to reflect this change.



Added custom AVIOContext field to options

Since some formats and inputs may not be supported by FFmpeg, I have added a new parameter that allows a user to allocate their own AVIOContext. This class will allow for creating a read, seek, and write callback if they desire.



Checking for start_time validity

It is possible for some file formats to not provide a start_time to FFmpeg. This would cause stuttering in the video since the clocks class would be incorrect.



Removed findVideoStream and findAudioStream

The new FFmpeg release already has a function that will find the best audio and video stream. The code has been replaced with this function.



Updated error reporting

Some functions would not log an error when opening a file or modifying a file failed. New logs have been added as well as a function to convert error numbers to their string descriptions.



decode_video has been replaced

The old decode_video function would remove extra data that some decoders use in order to properly decode a packet. Now av_codec_decode_video2 has replaced that function.



Picture format changed from RGBA32 to RGB24

Since most video will not contain an alpha channel, using a 24 bit texture will use less memory."
2013-02-06 12:46:03 +00:00

202 lines
3.9 KiB
C++

#ifndef HEADER_GUARD_OSGFFMPEG_FFMPEG_DECODER_H
#define HEADER_GUARD_OSGFFMPEG_FFMPEG_DECODER_H
#include "FFmpegDecoderAudio.hpp"
#include "FFmpegDecoderVideo.hpp"
#include <osg/Notify>
namespace osgFFmpeg {
class FFmpegParameters;
class FormatContextPtr
{
public:
typedef AVFormatContext T;
explicit FormatContextPtr() : _ptr(0) {}
explicit FormatContextPtr(T* ptr) : _ptr(ptr) {}
~FormatContextPtr()
{
cleanup();
}
T* get() { return _ptr; }
operator bool() const { return _ptr != 0; }
T * operator-> () const // never throws
{
return _ptr;
}
void reset(T* ptr)
{
if (ptr==_ptr) return;
cleanup();
_ptr = ptr;
}
void cleanup()
{
if (_ptr)
{
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(53, 17, 0)
OSG_NOTICE<<"Calling avformat_close_input("<<&_ptr<<")"<<std::endl;
avformat_close_input(&_ptr);
#else
OSG_NOTICE<<"Calling av_close_input_file("<<_ptr<<")"<<std::endl;
av_close_input_file(_ptr);
#endif
}
_ptr = 0;
}
protected:
T* _ptr;
};
class FFmpegDecoder : public osg::Referenced
{
public:
FFmpegDecoder();
~FFmpegDecoder();
bool open(const std::string & filename, FFmpegParameters* parameters);
void close(bool waitForThreadToExit);
bool readNextPacket();
void rewind();
void seek(double time);
void pause();
void loop(bool loop);
bool loop() const;
double creation_time() const;
double duration() const;
double reference();
FFmpegDecoderAudio & audio_decoder();
FFmpegDecoderVideo & video_decoder();
FFmpegDecoderAudio const & audio_decoder() const;
FFmpegDecoderVideo const & video_decoder() const;
protected:
enum State
{
NORMAL,
PAUSE,
END_OF_STREAM,
REWINDING,
SEEKING
};
typedef BoundedMessageQueue<FFmpegPacket> PacketQueue;
void flushAudioQueue();
void flushVideoQueue();
bool readNextPacketNormal();
bool readNextPacketEndOfStream();
bool readNextPacketRewinding();
bool readNextPacketSeeking();
bool readNextPacketPause();
void rewindButDontFlushQueues();
void seekButDontFlushQueues(double time);
FormatContextPtr m_format_context;
AVStream * m_audio_stream;
AVStream * m_video_stream;
int m_audio_index;
int m_video_index;
FFmpegClocks m_clocks;
FFmpegPacket m_pending_packet;
PacketQueue m_audio_queue;
PacketQueue m_video_queue;
FFmpegDecoderAudio m_audio_decoder;
FFmpegDecoderVideo m_video_decoder;
double m_duration;
double m_start;
State m_state;
bool m_loop;
};
inline void FFmpegDecoder::loop(const bool loop)
{
m_loop = loop;
}
inline bool FFmpegDecoder::loop() const
{
return m_loop;
}
inline double FFmpegDecoder::creation_time() const
{
if(m_format_context) return m_format_context->start_time;
else return HUGE_VAL;
}
inline double FFmpegDecoder::duration() const
{
return double(m_format_context->duration) / AV_TIME_BASE;
}
inline double FFmpegDecoder::reference()
{
return m_clocks.getCurrentTime();
}
inline FFmpegDecoderAudio & FFmpegDecoder::audio_decoder()
{
return m_audio_decoder;
}
inline FFmpegDecoderVideo & FFmpegDecoder::video_decoder()
{
return m_video_decoder;
}
inline FFmpegDecoderAudio const & FFmpegDecoder::audio_decoder() const
{
return m_audio_decoder;
}
inline FFmpegDecoderVideo const & FFmpegDecoder::video_decoder() const
{
return m_video_decoder;
}
} // namespace osgFFmpeg
#endif // HEADER_GUARD_OSGFFMPEG_FFMPEG_DECODER_H