1-
21#include " FFmpegDecoder.hpp"
32#include " FFmpegParameters.hpp"
43
@@ -36,8 +35,10 @@ static std::string AvStrError(int errnum)
3635}
3736
3837FFmpegDecoder::FFmpegDecoder () :
39- m_audio_stream (0 ),
40- m_video_stream (0 ),
38+ m_audio_stream (nullptr ),
39+ m_video_stream (nullptr ),
40+ m_audio_index (-1 ),
41+ m_video_index (-1 ),
4142 m_audio_queue (100 ),
4243 m_video_queue (100 ),
4344 m_audio_decoder (m_audio_queue, m_clocks),
@@ -61,10 +62,10 @@ bool FFmpegDecoder::open(const std::string & filename, FFmpegParameters* paramet
6162 try
6263 {
6364 // Open video file
64- AVFormatContext * p_format_context = 0 ;
65- AVInputFormat *iformat = 0 ;
65+ AVFormatContext * p_format_context = nullptr ;
66+ AVInputFormat *iformat = nullptr ;
6667
67- if (filename.compare (0 , 5 , " /dev/" )== 0 )
68+ if (filename.compare (0 , 5 , " /dev/" ) == 0 )
6869 {
6970#ifdef ANDROID
7071 throw std::runtime_error (" Device not supported on Android" );
@@ -78,24 +79,24 @@ bool FFmpegDecoder::open(const std::string & filename, FFmpegParameters* paramet
7879 }
7980
8081 std::string format = " video4linux2" ;
81- iformat = av_find_input_format (format.c_str ());
82+ iformat = const_cast <AVInputFormat*>( av_find_input_format (format.c_str () ));
8283
8384 if (iformat)
8485 {
85- OSG_INFO<< " Found input format: " << format<< std::endl;
86+ OSG_INFO << " Found input format: " << format << std::endl;
8687 }
8788 else
8889 {
89- OSG_INFO<< " Failed to find input format: " << format<< std::endl;
90+ OSG_INFO << " Failed to find input format: " << format << std::endl;
9091 }
9192
9293#endif
9394 }
9495 else
9596 {
96- iformat = parameters ? parameters->getFormat () : 0 ;
97- AVIOContext* context = parameters ? parameters->getContext () : 0 ;
98- if (context != NULL )
97+ iformat = parameters ? const_cast <AVInputFormat*>( parameters->getFormat ()) : nullptr ;
98+ AVIOContext* context = parameters ? parameters->getContext () : nullptr ;
99+ if (context != nullptr )
99100 {
100101 p_format_context = avformat_alloc_context ();
101102 p_format_context->pb = context;
@@ -105,38 +106,23 @@ bool FFmpegDecoder::open(const std::string & filename, FFmpegParameters* paramet
105106 int error = avformat_open_input (&p_format_context, filename.c_str (), iformat, parameters->getOptions ());
106107 if (error != 0 )
107108 {
108- std::string error_str;
109- switch (error)
110- {
111- // case AVERROR_UNKNOWN: error_str = "AVERROR_UNKNOWN"; break; // same value as AVERROR_INVALIDDATA
112- case AVERROR_IO: error_str = " AVERROR_IO" ; break ;
113- case AVERROR_NUMEXPECTED: error_str = " AVERROR_NUMEXPECTED" ; break ;
114- case AVERROR_INVALIDDATA: error_str = " AVERROR_INVALIDDATA" ; break ;
115- case AVERROR_NOMEM: error_str = " AVERROR_NOMEM" ; break ;
116- case AVERROR_NOFMT: error_str = " AVERROR_NOFMT" ; break ;
117- case AVERROR_NOTSUPP: error_str = " AVERROR_NOTSUPP" ; break ;
118- case AVERROR_NOENT: error_str = " AVERROR_NOENT" ; break ;
119- case AVERROR_PATCHWELCOME: error_str = " AVERROR_PATCHWELCOME" ; break ;
120- default : error_str = " Unknown error" ; break ;
121- }
122-
123- throw std::runtime_error (" av_open_input_file() failed : " + error_str);
109+ throw std::runtime_error (" avformat_open_input() failed: " + AvStrError (error));
124110 }
125111
126112 m_format_context.reset (p_format_context);
127113
128114 // Retrieve stream info
129115 // Only buffer up to one and a half seconds by default
130116 float max_analyze_duration = 1.5 ;
131- AVDictionaryEntry *mad = av_dict_get ( *parameters->getOptions (), " mad" , NULL , 0 );
132- if ( mad ) {
117+ AVDictionaryEntry *mad = av_dict_get (*parameters->getOptions (), " mad" , NULL , 0 );
118+ if (mad) {
133119 max_analyze_duration = atof (mad->value );
134120 }
135121 p_format_context->max_analyze_duration = AV_TIME_BASE * max_analyze_duration;
136122// p_format_context->probesize = 100000;
137123
138124 if (avformat_find_stream_info (p_format_context, NULL ) < 0 )
139- throw std::runtime_error (" av_find_stream_info () failed" );
125+ throw std::runtime_error (" avformat_find_stream_info () failed" );
140126
141127 m_duration = double (m_format_context->duration ) / AV_TIME_BASE;
142128 if (m_format_context->start_time != static_cast <int64_t >(AV_NOPTS_VALUE))
@@ -159,7 +145,7 @@ bool FFmpegDecoder::open(const std::string & filename, FFmpegParameters* paramet
159145 m_audio_stream = m_format_context->streams [m_audio_index];
160146 else
161147 {
162- m_audio_stream = 0 ;
148+ m_audio_stream = nullptr ;
163149 m_audio_index = std::numeric_limits<unsigned int >::max ();
164150 }
165151
@@ -271,18 +257,18 @@ bool FFmpegDecoder::readNextPacketNormal()
271257{
272258 AVPacket packet;
273259
274- if (! m_pending_packet)
260+ if (!m_pending_packet)
275261 {
276262 bool end_of_stream = false ;
277263
278264 // Read the next frame packet
279265 int error = av_read_frame (m_format_context.get (), &packet);
280266 if (error < 0 )
281267 {
282- if (error == static_cast <int >(AVERROR_EOF) ||
283- m_format_context.get ()->pb ->eof_reached )
268+ if (error == static_cast <int >(AVERROR_EOF) || m_format_context.get ()->pb ->eof_reached )
284269 end_of_stream = true ;
285- else {
270+ else
271+ {
286272 OSG_FATAL << " av_read_frame() returned " << AvStrError (error) << std::endl;
287273 throw std::runtime_error (" av_read_frame() failed" );
288274 }
@@ -303,12 +289,6 @@ bool FFmpegDecoder::readNextPacketNormal()
303289 }
304290 else
305291 {
306- // Make the packet data available beyond av_read_frame() logical scope.
307- if ((error = av_dup_packet (&packet)) < 0 ) {
308- OSG_FATAL << " av_dup_packet() returned " << AvStrError (error) << std::endl;
309- throw std::runtime_error (" av_dup_packet() failed" );
310- }
311-
312292 m_pending_packet = FFmpegPacket (packet);
313293 }
314294 }
@@ -340,8 +320,6 @@ bool FFmpegDecoder::readNextPacketNormal()
340320 return false ;
341321}
342322
343-
344-
345323bool FFmpegDecoder::readNextPacketEndOfStream ()
346324{
347325 const FFmpegPacket packet (FFmpegPacket::PACKET_END_OF_STREAM);
@@ -352,8 +330,6 @@ bool FFmpegDecoder::readNextPacketEndOfStream()
352330 return false ;
353331}
354332
355-
356-
357333bool FFmpegDecoder::readNextPacketRewinding ()
358334{
359335 const FFmpegPacket packet (FFmpegPacket::PACKET_FLUSH);
@@ -364,8 +340,6 @@ bool FFmpegDecoder::readNextPacketRewinding()
364340 return false ;
365341}
366342
367-
368-
369343void FFmpegDecoder::rewindButDontFlushQueues ()
370344{
371345 const AVRational AvTimeBaseQ = { 1 , AV_TIME_BASE }; // = AV_TIME_BASE_Q
@@ -374,7 +348,8 @@ void FFmpegDecoder::rewindButDontFlushQueues()
374348 const int64_t seek_target = av_rescale_q (pos, AvTimeBaseQ, m_video_stream->time_base );
375349
376350 int error = 0 ;
377- if ((error = av_seek_frame (m_format_context.get (), m_video_index, seek_target, 0 /* AVSEEK_FLAG_BYTE |*/ /* AVSEEK_FLAG_BACKWARD*/ )) < 0 ) {
351+ if ((error = av_seek_frame (m_format_context.get (), m_video_index, seek_target, 0 )) < 0 )
352+ {
378353 OSG_FATAL << " av_seek_frame returned " << AvStrError (error) << std::endl;
379354 throw std::runtime_error (" av_seek_frame failed()" );
380355 }
@@ -397,13 +372,14 @@ void FFmpegDecoder::seekButDontFlushQueues(double time)
397372{
398373 const AVRational AvTimeBaseQ = { 1 , AV_TIME_BASE }; // = AV_TIME_BASE_Q
399374
400- const int64_t pos = int64_t (m_clocks.getStartTime ()+ time * double (AV_TIME_BASE));
375+ const int64_t pos = int64_t (m_clocks.getStartTime () + time * double (AV_TIME_BASE));
401376 const int64_t seek_target = av_rescale_q (pos, AvTimeBaseQ, m_video_stream->time_base );
402377
403378 m_clocks.setSeekTime (time);
404379
405380 int error = 0 ;
406- if ((error = av_seek_frame (m_format_context.get (), m_video_index, seek_target, 0 /* AVSEEK_FLAG_BYTE |*/ /* AVSEEK_FLAG_BACKWARD*/ )) < 0 ) {
381+ if ((error = av_seek_frame (m_format_context.get (), m_video_index, seek_target, 0 )) < 0 )
382+ {
407383 OSG_FATAL << " av_seek_frame() returned " << AvStrError (error) << std::endl;
408384 throw std::runtime_error (" av_seek_frame failed()" );
409385 }
0 commit comments