1
-
2
1
#include " FFmpegDecoder.hpp"
3
2
#include " FFmpegParameters.hpp"
4
3
@@ -36,8 +35,10 @@ static std::string AvStrError(int errnum)
36
35
}
37
36
38
37
FFmpegDecoder::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 ),
41
42
m_audio_queue (100 ),
42
43
m_video_queue (100 ),
43
44
m_audio_decoder (m_audio_queue, m_clocks),
@@ -61,10 +62,10 @@ bool FFmpegDecoder::open(const std::string & filename, FFmpegParameters* paramet
61
62
try
62
63
{
63
64
// Open video file
64
- AVFormatContext * p_format_context = 0 ;
65
- AVInputFormat *iformat = 0 ;
65
+ AVFormatContext * p_format_context = nullptr ;
66
+ AVInputFormat *iformat = nullptr ;
66
67
67
- if (filename.compare (0 , 5 , " /dev/" )== 0 )
68
+ if (filename.compare (0 , 5 , " /dev/" ) == 0 )
68
69
{
69
70
#ifdef ANDROID
70
71
throw std::runtime_error (" Device not supported on Android" );
@@ -78,24 +79,24 @@ bool FFmpegDecoder::open(const std::string & filename, FFmpegParameters* paramet
78
79
}
79
80
80
81
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 () ));
82
83
83
84
if (iformat)
84
85
{
85
- OSG_INFO<< " Found input format: " << format<< std::endl;
86
+ OSG_INFO << " Found input format: " << format << std::endl;
86
87
}
87
88
else
88
89
{
89
- OSG_INFO<< " Failed to find input format: " << format<< std::endl;
90
+ OSG_INFO << " Failed to find input format: " << format << std::endl;
90
91
}
91
92
92
93
#endif
93
94
}
94
95
else
95
96
{
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 )
99
100
{
100
101
p_format_context = avformat_alloc_context ();
101
102
p_format_context->pb = context;
@@ -105,38 +106,23 @@ bool FFmpegDecoder::open(const std::string & filename, FFmpegParameters* paramet
105
106
int error = avformat_open_input (&p_format_context, filename.c_str (), iformat, parameters->getOptions ());
106
107
if (error != 0 )
107
108
{
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));
124
110
}
125
111
126
112
m_format_context.reset (p_format_context);
127
113
128
114
// Retrieve stream info
129
115
// Only buffer up to one and a half seconds by default
130
116
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) {
133
119
max_analyze_duration = atof (mad->value );
134
120
}
135
121
p_format_context->max_analyze_duration = AV_TIME_BASE * max_analyze_duration;
136
122
// p_format_context->probesize = 100000;
137
123
138
124
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" );
140
126
141
127
m_duration = double (m_format_context->duration ) / AV_TIME_BASE;
142
128
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
159
145
m_audio_stream = m_format_context->streams [m_audio_index];
160
146
else
161
147
{
162
- m_audio_stream = 0 ;
148
+ m_audio_stream = nullptr ;
163
149
m_audio_index = std::numeric_limits<unsigned int >::max ();
164
150
}
165
151
@@ -271,18 +257,18 @@ bool FFmpegDecoder::readNextPacketNormal()
271
257
{
272
258
AVPacket packet;
273
259
274
- if (! m_pending_packet)
260
+ if (!m_pending_packet)
275
261
{
276
262
bool end_of_stream = false ;
277
263
278
264
// Read the next frame packet
279
265
int error = av_read_frame (m_format_context.get (), &packet);
280
266
if (error < 0 )
281
267
{
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 )
284
269
end_of_stream = true ;
285
- else {
270
+ else
271
+ {
286
272
OSG_FATAL << " av_read_frame() returned " << AvStrError (error) << std::endl;
287
273
throw std::runtime_error (" av_read_frame() failed" );
288
274
}
@@ -303,12 +289,6 @@ bool FFmpegDecoder::readNextPacketNormal()
303
289
}
304
290
else
305
291
{
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
-
312
292
m_pending_packet = FFmpegPacket (packet);
313
293
}
314
294
}
@@ -340,8 +320,6 @@ bool FFmpegDecoder::readNextPacketNormal()
340
320
return false ;
341
321
}
342
322
343
-
344
-
345
323
bool FFmpegDecoder::readNextPacketEndOfStream ()
346
324
{
347
325
const FFmpegPacket packet (FFmpegPacket::PACKET_END_OF_STREAM);
@@ -352,8 +330,6 @@ bool FFmpegDecoder::readNextPacketEndOfStream()
352
330
return false ;
353
331
}
354
332
355
-
356
-
357
333
bool FFmpegDecoder::readNextPacketRewinding ()
358
334
{
359
335
const FFmpegPacket packet (FFmpegPacket::PACKET_FLUSH);
@@ -364,8 +340,6 @@ bool FFmpegDecoder::readNextPacketRewinding()
364
340
return false ;
365
341
}
366
342
367
-
368
-
369
343
void FFmpegDecoder::rewindButDontFlushQueues ()
370
344
{
371
345
const AVRational AvTimeBaseQ = { 1 , AV_TIME_BASE }; // = AV_TIME_BASE_Q
@@ -374,7 +348,8 @@ void FFmpegDecoder::rewindButDontFlushQueues()
374
348
const int64_t seek_target = av_rescale_q (pos, AvTimeBaseQ, m_video_stream->time_base );
375
349
376
350
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
+ {
378
353
OSG_FATAL << " av_seek_frame returned " << AvStrError (error) << std::endl;
379
354
throw std::runtime_error (" av_seek_frame failed()" );
380
355
}
@@ -397,13 +372,14 @@ void FFmpegDecoder::seekButDontFlushQueues(double time)
397
372
{
398
373
const AVRational AvTimeBaseQ = { 1 , AV_TIME_BASE }; // = AV_TIME_BASE_Q
399
374
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));
401
376
const int64_t seek_target = av_rescale_q (pos, AvTimeBaseQ, m_video_stream->time_base );
402
377
403
378
m_clocks.setSeekTime (time);
404
379
405
380
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
+ {
407
383
OSG_FATAL << " av_seek_frame() returned " << AvStrError (error) << std::endl;
408
384
throw std::runtime_error (" av_seek_frame failed()" );
409
385
}
0 commit comments