Use SafeStringStream instead of std::stringstream to try to fix random crashes on...
[dcpomatic.git] / src / lib / ffmpeg_decoder.cc
index 6351c7e2200edaa73c14e3bc100a865c755df71e..d40b798baafb77058f019e2bc6cf71817b5bd2f8 100644 (file)
@@ -23,7 +23,6 @@
 
 #include <stdexcept>
 #include <vector>
-#include <sstream>
 #include <iomanip>
 #include <iostream>
 #include <stdint.h>
@@ -46,14 +45,13 @@ extern "C" {
 
 #include "i18n.h"
 
-#define LOG_GENERAL(...) film->log()->log (String::compose (__VA_ARGS__), Log::GENERAL);
-#define LOG_ERROR(...) film->log()->log (String::compose (__VA_ARGS__), Log::ERROR);
-#define LOG_WARNING(...) film->log()->log (__VA_ARGS__, Log::WARNING);
+#define LOG_GENERAL(...) film->log()->log (String::compose (__VA_ARGS__), Log::TYPE_GENERAL);
+#define LOG_ERROR(...) film->log()->log (String::compose (__VA_ARGS__), Log::TYPE_ERROR);
+#define LOG_WARNING(...) film->log()->log (__VA_ARGS__, Log::TYPE_WARNING);
 
 using std::cout;
 using std::string;
 using std::vector;
-using std::stringstream;
 using std::list;
 using std::min;
 using std::pair;
@@ -334,7 +332,7 @@ FFmpegDecoder::seek (VideoContent::Frame frame, bool accurate)
        }
 
        /* Initial seek time in the stream's timebase */
-       int64_t const initial_vt = ((initial / _ffmpeg_content->video_frame_rate()) - _pts_offset) / time_base;
+       int64_t const initial_vt = ((initial / _ffmpeg_content->original_video_frame_rate()) - _pts_offset) / time_base;
 
        av_seek_frame (_format_context, _video_stream, initial_vt, AVSEEK_FLAG_BACKWARD);
 
@@ -360,7 +358,7 @@ FFmpegDecoder::seek (VideoContent::Frame frame, bool accurate)
                return;
        }
 
-       while (1) {
+       while (true) {
                int r = av_read_frame (_format_context, &_packet);
                if (r < 0) {
                        return;
@@ -375,7 +373,7 @@ FFmpegDecoder::seek (VideoContent::Frame frame, bool accurate)
                r = avcodec_decode_video2 (video_codec_context(), _frame, &finished, &_packet);
                if (r >= 0 && finished) {
                        _video_position = rint (
-                               (av_frame_get_best_effort_timestamp (_frame) * time_base + _pts_offset) * _ffmpeg_content->video_frame_rate()
+                               (av_frame_get_best_effort_timestamp (_frame) * time_base + _pts_offset) * _ffmpeg_content->original_video_frame_rate()
                                );
 
                        if (_video_position >= (frame - 1)) {
@@ -386,6 +384,11 @@ FFmpegDecoder::seek (VideoContent::Frame frame, bool accurate)
                
                av_free_packet (&_packet);
        }
+
+       /* _video_position should be the next thing to be emitted, which will the one after the thing
+          we just saw.
+       */
+       _video_position++;
 }
 
 void
@@ -417,15 +420,18 @@ FFmpegDecoder::decode_audio_packet ()
 
                                if (pts > 0) {
                                        /* Emit some silence */
-                                       shared_ptr<AudioBuffers> silence (
-                                               new AudioBuffers (
-                                                       _ffmpeg_content->audio_channels(),
-                                                       pts * _ffmpeg_content->content_audio_frame_rate()
-                                                       )
-                                               );
+                                       int64_t frames = pts * _ffmpeg_content->content_audio_frame_rate ();
+                                       while (frames > 0) {
+                                               int64_t const this_time = min (frames, (int64_t) _ffmpeg_content->content_audio_frame_rate() / 2);
+                                               
+                                               shared_ptr<AudioBuffers> silence (
+                                                       new AudioBuffers (_ffmpeg_content->audio_channels(), this_time)
+                                                       );
                                        
-                                       silence->make_silent ();
-                                       audio (silence, _audio_position);
+                                               silence->make_silent ();
+                                               audio (silence, _audio_position);
+                                               frames -= this_time;
+                                       }
                                }
                        }
                        
@@ -487,12 +493,12 @@ FFmpegDecoder::decode_video_packet ()
                                /* We just did a seek, so disable any attempts to correct for where we
                                   are / should be.
                                */
-                               _video_position = rint (pts * _ffmpeg_content->video_frame_rate ());
+                               _video_position = rint (pts * _ffmpeg_content->original_video_frame_rate ());
                                _just_sought = false;
                        }
 
-                       double const next = _video_position / _ffmpeg_content->video_frame_rate();
-                       double const one_frame = 1 / _ffmpeg_content->video_frame_rate ();
+                       double const next = _video_position / _ffmpeg_content->original_video_frame_rate();
+                       double const one_frame = 1 / _ffmpeg_content->original_video_frame_rate ();
                        double delta = pts - next;
 
                        while (delta > one_frame) {