Try to fix crash.
[dcpomatic.git] / src / lib / ffmpeg_examiner.cc
index 4409526dc4872015897357ffa385a28259a6acf2..6d5e33238913c0f1e1afb79659e005ebb42f099b 100644 (file)
@@ -40,6 +40,7 @@ using boost::optional;
 /** @param job job that the examiner is operating in, or 0 */
 FFmpegExaminer::FFmpegExaminer (shared_ptr<const FFmpegContent> c, shared_ptr<Job> job)
        : FFmpeg (c)
+       , _video_length (0)
        , _need_video_length (false)
 {
        /* Find audio and subtitle streams */
@@ -70,12 +71,16 @@ FFmpegExaminer::FFmpegExaminer (shared_ptr<const FFmpegContent> c, shared_ptr<Jo
        /* See if the header has duration information in it */
        _need_video_length = _format_context->duration == AV_NOPTS_VALUE;
        if (!_need_video_length) {
-               _video_length = ContentTime::from_seconds (double (_format_context->duration) / AV_TIME_BASE);
+               _video_length = (double (_format_context->duration) / AV_TIME_BASE) * video_frame_rate().get ();
        } else if (job) {
                job->sub (_("Finding length"));
                job->set_progress_unknown ();
        }
 
+       if (job) {
+               job->sub (_("Finding subtitles"));
+       }
+
        /* Run through until we find:
         *   - the first video.
         *   - the first audio for each stream.
@@ -91,6 +96,10 @@ FFmpegExaminer::FFmpegExaminer (shared_ptr<const FFmpegContent> c, shared_ptr<Jo
                        break;
                }
 
+               if (job) {
+                       job->set_progress_unknown ();
+               }
+
                AVCodecContext* context = _format_context->streams[_packet.stream_index]->codec;
 
                if (_packet.stream_index == _video_stream) {
@@ -126,7 +135,9 @@ FFmpegExaminer::video_packet (AVCodecContext* context)
                        _first_video = frame_time (_format_context->streams[_video_stream]);
                }
                if (_need_video_length) {
-                       _video_length = frame_time (_format_context->streams[_video_stream]).get_value_or (ContentTime ());
+                       _video_length = frame_time (
+                               _format_context->streams[_video_stream]
+                               ).get_value_or (ContentTime ()).frames (video_frame_rate().get ());
                }
        }
 }
@@ -150,13 +161,18 @@ FFmpegExaminer::subtitle_packet (AVCodecContext* context, shared_ptr<FFmpegSubti
        int frame_finished;
        AVSubtitle sub;
        if (avcodec_decode_subtitle2 (context, &sub, &frame_finished, &_packet) >= 0 && frame_finished) {
-               ContentTimePeriod const period = subtitle_period (sub);
-               if (sub.num_rects == 0 && !stream->periods.empty () && stream->periods.back().to > period.from) {
-                       /* Finish the last subtitle */
-                       stream->periods.back().to = period.from;
+               FFmpegSubtitlePeriod const period = subtitle_period (sub);
+               if (sub.num_rects <= 0 && _last_subtitle_start) {
+                       stream->add_subtitle (ContentTimePeriod (_last_subtitle_start.get (), period.from));
+                       _last_subtitle_start = optional<ContentTime> ();
                } else if (sub.num_rects == 1) {
-                       stream->periods.push_back (period);
+                       if (period.to) {
+                               stream->add_subtitle (ContentTimePeriod (period.from, period.to.get ()));
+                       } else {
+                               _last_subtitle_start = period.from;
+                       }
                }
+               avsubtitle_free (&sub);
        }
 }
 
@@ -190,11 +206,10 @@ FFmpegExaminer::video_size () const
 }
 
 /** @return Length according to our content's header */
-ContentTime
+Frame
 FFmpegExaminer::video_length () const
 {
-       ContentTime const length = ContentTime::from_seconds (double (_format_context->duration) / AV_TIME_BASE);
-       return ContentTime (max (ContentTime (1), _video_length));
+       return max (Frame (1), _video_length);
 }
 
 optional<float>