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."
This commit is contained in:
Robert Osfield
2013-02-06 12:46:03 +00:00
parent 19bfa92c91
commit 29eb65c77d
10 changed files with 202 additions and 188 deletions

View File

@@ -28,7 +28,12 @@
namespace osgFFmpeg {
static std::string AvStrError(int errnum)
{
char buf[128];
av_strerror(errnum, buf, sizeof(buf));
return std::string(buf);
}
FFmpegDecoder::FFmpegDecoder() :
m_audio_stream(0),
@@ -60,25 +65,20 @@ bool FFmpegDecoder::open(const std::string & filename, FFmpegParameters* paramet
if (filename.compare(0, 5, "/dev/")==0)
{
#ifdef ANDROID
throw std::runtime_error("Device not supported on Android");
#else
avdevice_register_all();
OSG_NOTICE<<"Attempting to stream "<<filename<<std::endl;
AVFormatParameters formatParams;
memset(&formatParams, 0, sizeof(AVFormatParameters));
AVInputFormat *iformat;
formatParams.channel = 0;
formatParams.standard = 0;
#if 1
formatParams.width = 320;
formatParams.height = 240;
av_dict_set(parameters->getOptions(), "video_size", "320x240", 0);
#else
formatParams.width = 640;
formatParams.height = 480;
av_dict_set(parameters->getOptions(), "video_size", "640x480", 0);
#endif
formatParams.time_base.num = 1;
formatParams.time_base.den = 30;
av_dict_set(parameters->getOptions(), "framerate", "1:30", 0);
std::string format = "video4linux2";
iformat = av_find_input_format(format.c_str());
@@ -92,7 +92,7 @@ bool FFmpegDecoder::open(const std::string & filename, FFmpegParameters* paramet
OSG_NOTICE<<"Failed to find input format: "<<format<<std::endl;
}
int error = av_open_input_file(&p_format_context, filename.c_str(), iformat, 0, &formatParams);
int error = avformat_open_input(&p_format_context, filename.c_str(), iformat, parameters->getOptions());
if (error != 0)
{
std::string error_str;
@@ -112,34 +112,53 @@ bool FFmpegDecoder::open(const std::string & filename, FFmpegParameters* paramet
throw std::runtime_error("av_open_input_file() failed : " + error_str);
}
#endif
}
else
{
AVInputFormat* av_format = (parameters ? parameters->getFormat() : 0);
AVFormatParameters* av_params = (parameters ? parameters->getFormatParameter() : 0);
if (av_open_input_file(&p_format_context, filename.c_str(), av_format, 0, av_params) !=0 )
AVInputFormat* iformat = (parameters ? parameters->getFormat() : 0);
AVIOContext* context = parameters->getContext();
if (context != NULL)
{
p_format_context = avformat_alloc_context();
p_format_context->pb = context;
}
if (avformat_open_input(&p_format_context, filename.c_str(), iformat, parameters->getOptions()) != 0)
throw std::runtime_error("av_open_input_file() failed");
}
m_format_context.reset(p_format_context);
// Retrieve stream info
if (av_find_stream_info(p_format_context) < 0)
// Only buffer up to one and a half seconds
p_format_context->max_analyze_duration = AV_TIME_BASE * 1.5f;
if (avformat_find_stream_info(p_format_context, NULL) < 0)
throw std::runtime_error("av_find_stream_info() failed");
m_duration = double(m_format_context->duration) / AV_TIME_BASE;
m_start = double(m_format_context->start_time) / AV_TIME_BASE;
if (m_format_context->start_time != AV_NOPTS_VALUE)
m_start = double(m_format_context->start_time) / AV_TIME_BASE;
else
m_start = 0;
// TODO move this elsewhere
m_clocks.reset(m_start);
// Dump info to stderr
dump_format(p_format_context, 0, filename.c_str(), false);
av_dump_format(p_format_context, 0, filename.c_str(), false);
// Find and open the first video and audio streams (note that audio stream is optional and only opened if possible)
if ((m_video_index = av_find_best_stream(m_format_context.get(), AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0)) < 0)
throw std::runtime_error("Could not open video stream");
m_video_stream = m_format_context->streams[m_video_index];
findVideoStream();
findAudioStream();
if ((m_audio_index = av_find_best_stream(m_format_context.get(), AVMEDIA_TYPE_AUDIO, -1, -1, NULL, 0)) >= 0)
m_audio_stream = m_format_context->streams[m_audio_index];
else
{
m_audio_stream = 0;
m_audio_index = std::numeric_limits<unsigned int>::max();
}
m_video_decoder.open(m_video_stream);
@@ -196,6 +215,7 @@ bool FFmpegDecoder::readNextPacket()
return readNextPacketSeeking();
default:
OSG_FATAL << "unknown decoder state " << m_state << std::endl;
assert(false);
return false;
}
@@ -230,40 +250,6 @@ void FFmpegDecoder::pause()
m_state = PAUSE;
}
void FFmpegDecoder::findAudioStream()
{
for (unsigned int i = 0; i < m_format_context->nb_streams; ++i)
{
if (m_format_context->streams[i]->codec->codec_type == CODEC_TYPE_AUDIO)
{
m_audio_stream = m_format_context->streams[i];
m_audio_index = i;
return;
}
}
m_audio_stream = 0;
m_audio_index = std::numeric_limits<unsigned int>::max();
}
void FFmpegDecoder::findVideoStream()
{
for (unsigned int i = 0; i < m_format_context->nb_streams; ++i)
{
if (m_format_context->streams[i]->codec->codec_type == CODEC_TYPE_VIDEO)
{
m_video_stream = m_format_context->streams[i];
m_video_index = i;
return;
}
}
throw std::runtime_error("could not find a video stream");
}
inline void FFmpegDecoder::flushAudioQueue()
{
@@ -290,12 +276,15 @@ bool FFmpegDecoder::readNextPacketNormal()
bool end_of_stream = false;
// Read the next frame packet
if (av_read_frame(m_format_context.get(), &packet) < 0)
int error = av_read_frame(m_format_context.get(), &packet);
if (error < 0)
{
if (url_ferror(m_format_context->pb) == 0)
if (error == AVERROR_EOF || url_feof(m_format_context.get()->pb))
end_of_stream = true;
else
else {
OSG_FATAL << "av_read_frame() returned " << AvStrError(error) << std::endl;
throw std::runtime_error("av_read_frame() failed");
}
}
if (end_of_stream)
@@ -314,8 +303,10 @@ bool FFmpegDecoder::readNextPacketNormal()
else
{
// Make the packet data available beyond av_read_frame() logical scope.
if (av_dup_packet(&packet) < 0)
if ((error = av_dup_packet(&packet)) < 0) {
OSG_FATAL << "av_dup_packet() returned " << AvStrError(error) << std::endl;
throw std::runtime_error("av_dup_packet() failed");
}
m_pending_packet = FFmpegPacket(packet);
}
@@ -381,8 +372,11 @@ void FFmpegDecoder::rewindButDontFlushQueues()
const int64_t pos = int64_t(m_clocks.getStartTime() * double(AV_TIME_BASE));
const int64_t seek_target = av_rescale_q(pos, AvTimeBaseQ, m_video_stream->time_base);
if (av_seek_frame(m_format_context.get(), m_video_index, seek_target, 0/*AVSEEK_FLAG_BYTE |*/ /*AVSEEK_FLAG_BACKWARD*/) < 0)
int error = 0;
if ((error = av_seek_frame(m_format_context.get(), m_video_index, seek_target, 0/*AVSEEK_FLAG_BYTE |*/ /*AVSEEK_FLAG_BACKWARD*/)) < 0) {
OSG_FATAL << "av_seek_frame returned " << AvStrError(error) << std::endl;
throw std::runtime_error("av_seek_frame failed()");
}
m_clocks.rewind();
m_state = REWINDING;
@@ -407,8 +401,11 @@ void FFmpegDecoder::seekButDontFlushQueues(double time)
m_clocks.setSeekTime(time);
if (av_seek_frame(m_format_context.get(), m_video_index, seek_target, 0/*AVSEEK_FLAG_BYTE |*/ /*AVSEEK_FLAG_BACKWARD*/) < 0)
int error = 0;
if ((error = av_seek_frame(m_format_context.get(), m_video_index, seek_target, 0/*AVSEEK_FLAG_BYTE |*/ /*AVSEEK_FLAG_BACKWARD*/)) < 0) {
OSG_FATAL << "av_seek_frame() returned " << AvStrError(error) << std::endl;
throw std::runtime_error("av_seek_frame failed()");
}
m_clocks.seek(time);
m_state = SEEKING;