Basics of noting subtitle times in FFmpegSubtitleStreams.
[dcpomatic.git] / src / lib / ffmpeg_decoder.cc
index 0a4624569f9239a952ca0c6824a5adf09f16ab55..d668deb6f6bbeee617128acbe05489e30874f9a4 100644 (file)
@@ -27,7 +27,6 @@
 #include <iomanip>
 #include <iostream>
 #include <stdint.h>
-#include <boost/lexical_cast.hpp>
 #include <sndfile.h>
 extern "C" {
 #include <libavcodec/avcodec.h>
@@ -39,9 +38,12 @@ extern "C" {
 #include "util.h"
 #include "log.h"
 #include "ffmpeg_decoder.h"
+#include "ffmpeg_audio_stream.h"
+#include "ffmpeg_subtitle_stream.h"
 #include "filter_graph.h"
 #include "audio_buffers.h"
 #include "ffmpeg_content.h"
+#include "image_proxy.h"
 
 #include "i18n.h"
 
@@ -60,13 +62,12 @@ using dcp::Size;
 FFmpegDecoder::FFmpegDecoder (shared_ptr<const FFmpegContent> c, shared_ptr<Log> log)
        : VideoDecoder (c)
        , AudioDecoder (c)
+       , SubtitleDecoder (c)
        , FFmpeg (c)
        , _log (log)
        , _subtitle_codec_context (0)
        , _subtitle_codec (0)
 {
-       setup_subtitle ();
-
        /* Audio and video frame PTS values may not start with 0.  We want
           to fiddle them so that:
 
@@ -485,7 +486,7 @@ FFmpegDecoder::decode_video_packet ()
                
                if (i->second != AV_NOPTS_VALUE) {
                        double const pts = i->second * av_q2d (_format_context->streams[_video_stream]->time_base) + _pts_offset.seconds ();
-                       video (image, rint (pts * _ffmpeg_content->video_frame_rate ()));
+                       video (shared_ptr<ImageProxy> (new RawImageProxy (image)), rint (pts * _ffmpeg_content->video_frame_rate ()));
                } else {
                        _log->log ("Dropping frame without PTS");
                }
@@ -493,33 +494,7 @@ FFmpegDecoder::decode_video_packet ()
 
        return true;
 }
-
-       
-void
-FFmpegDecoder::setup_subtitle ()
-{
-       boost::mutex::scoped_lock lm (_mutex);
-       
-       if (!_ffmpeg_content->subtitle_stream()) {
-               return;
-       }
-
-       _subtitle_codec_context = _ffmpeg_content->subtitle_stream()->stream(_format_context)->codec;
-       if (_subtitle_codec_context == 0) {
-               throw DecodeError (N_("could not find subtitle stream"));
-       }
-
-       _subtitle_codec = avcodec_find_decoder (_subtitle_codec_context->codec_id);
-
-       if (_subtitle_codec == 0) {
-               throw DecodeError (N_("could not find subtitle decoder"));
-       }
        
-       if (avcodec_open2 (_subtitle_codec_context, _subtitle_codec, 0) < 0) {
-               throw DecodeError (N_("could not open subtitle decoder"));
-       }
-}
-
 void
 FFmpegDecoder::decode_subtitle_packet ()
 {
@@ -533,7 +508,7 @@ FFmpegDecoder::decode_subtitle_packet ()
           indicate that the previous subtitle should stop.
        */
        if (sub.num_rects <= 0) {
-               image_subtitle (ContentTime (), ContentTime (), shared_ptr<Image> (), dcpomatic::Rect<double> ());
+               image_subtitle (ContentTimePeriod (), shared_ptr<Image> (), dcpomatic::Rect<double> ());
                return;
        } else if (sub.num_rects > 1) {
                throw DecodeError (_("multi-part subtitles not yet supported"));
@@ -542,11 +517,7 @@ FFmpegDecoder::decode_subtitle_packet ()
        /* Subtitle PTS (within the source, not taking into account any of the
           source that we may have chopped off for the DCP)
        */
-       ContentTime packet_time = ContentTime::from_seconds (static_cast<double> (sub.pts) / AV_TIME_BASE) + _pts_offset;
-       
-       /* hence start time for this sub */
-       ContentTime const from = packet_time + ContentTime::from_seconds (sub.start_display_time / 1e3);
-       ContentTime const to = packet_time + ContentTime::from_seconds (sub.end_display_time / 1e3);
+       ContentTimePeriod period = subtitle_period (sub) + _pts_offset;
 
        AVSubtitleRect const * rect = sub.rects[0];
 
@@ -584,8 +555,7 @@ FFmpegDecoder::decode_subtitle_packet ()
        dcp::Size const vs = _ffmpeg_content->video_size ();
 
        image_subtitle (
-               from,
-               to,
+               period,
                image,
                dcpomatic::Rect<double> (
                        static_cast<double> (rect->x) / vs.width,