Hand-apply 6a3cd511559433554ab40ed72ff94b7d8dc2c5bd from master;
[dcpomatic.git] / src / lib / ffmpeg_examiner.cc
index 62b909b1d65f3708293ca8dcbe91fe4a371d4608..1d01981f60c80dd55dc19f185bc8fda2b3e0dfe0 100644 (file)
@@ -26,13 +26,13 @@ extern "C" {
 #include "ffmpeg_audio_stream.h"
 #include "ffmpeg_subtitle_stream.h"
 #include "util.h"
+#include "safe_stringstream.h"
 
 #include "i18n.h"
 
 using std::string;
 using std::cout;
 using std::max;
-using std::stringstream;
 using boost::shared_ptr;
 using boost::optional;
 
@@ -156,7 +156,7 @@ FFmpegExaminer::frame_time (AVStream* s) const
        return t;
 }
 
-float
+optional<float>
 FFmpegExaminer::video_frame_rate () const
 {
        /* This use of r_frame_rate is debateable; there's a few different
@@ -176,14 +176,25 @@ FFmpegExaminer::video_size () const
 ContentTime
 FFmpegExaminer::video_length () const
 {
-       ContentTime const length = ContentTime::from_seconds (double (_format_context->duration - _format_context->start_time) / AV_TIME_BASE);
+       ContentTime const length = ContentTime::from_seconds (double (_format_context->duration) / AV_TIME_BASE);
        return ContentTime (max (ContentTime::Type (1), length.get ()));
 }
 
+optional<float>
+FFmpegExaminer::sample_aspect_ratio () const
+{
+       AVRational sar = av_guess_sample_aspect_ratio (_format_context, _format_context->streams[_video_stream], 0);
+       if (sar.num == 0) {
+               /* I assume this means that we don't know */
+               return optional<float> ();
+       }
+       return float (sar.num) / sar.den;
+}
+
 string
 FFmpegExaminer::audio_stream_name (AVStream* s) const
 {
-       stringstream n;
+       SafeStringStream n;
 
        n << stream_name (s);
 
@@ -199,7 +210,7 @@ FFmpegExaminer::audio_stream_name (AVStream* s) const
 string
 FFmpegExaminer::subtitle_stream_name (AVStream* s) const
 {
-       stringstream n;
+       SafeStringStream n;
 
        n << stream_name (s);
 
@@ -213,7 +224,7 @@ FFmpegExaminer::subtitle_stream_name (AVStream* s) const
 string
 FFmpegExaminer::stream_name (AVStream* s) const
 {
-       stringstream n;
+       SafeStringStream n;
 
        if (s->metadata) {
                AVDictionaryEntry const * lang = av_dict_get (s->metadata, "language", 0, 0);