Merge master.
[dcpomatic.git] / src / lib / ffmpeg_examiner.cc
index 013799d036c2fd20a3b60cb4539868b2e025b34f..949062f5bd5c3a9e5b1eace869ffc4474007560f 100644 (file)
@@ -25,6 +25,7 @@ extern "C" {
 #include "ffmpeg_content.h"
 #include "ffmpeg_audio_stream.h"
 #include "ffmpeg_subtitle_stream.h"
+#include "util.h"
 
 #include "i18n.h"
 
@@ -63,9 +64,16 @@ FFmpegExaminer::FFmpegExaminer (shared_ptr<const FFmpegContent> c)
                }
        }
 
-       /* Run through until we find the first audio (for each stream) and video */
-
-       while (1) {
+       /* Run through until we find:
+        *   - the first video.
+        *   - the first audio for each stream.
+        *   - the subtitle periods for each stream.
+        *
+        * We have to note subtitle periods as otherwise we have no way of knowing
+        * where we should look for subtitles (video and audio are always present,
+        * so they are ok).
+        */
+       while (true) {
                int r = av_read_frame (_format_context, &_packet);
                if (r < 0) {
                        break;
@@ -122,7 +130,17 @@ FFmpegExaminer::audio_packet (AVCodecContext* context, shared_ptr<FFmpegAudioStr
 void
 FFmpegExaminer::subtitle_packet (AVCodecContext* context, shared_ptr<FFmpegSubtitleStream> stream)
 {
-
+       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;
+               } else if (sub.num_rects == 1) {
+                       stream->periods.push_back (period);
+               }
+       }
 }
 
 optional<ContentTime>
@@ -158,7 +176,7 @@ FFmpegExaminer::video_size () const
 ContentTime
 FFmpegExaminer::video_length () const
 {
-       ContentTime const length = ContentTime::from_seconds (double (_format_context->duration) / AV_TIME_BASE);
+       ContentTime const length = ContentTime::from_seconds (double (_format_context->duration - _format_context->start_time) / AV_TIME_BASE);
        return ContentTime (max (int64_t (1), length.get ()));
 }