Fix audio analysis; make sure we don't decode video and let it pile up unwanted.
[dcpomatic.git] / src / lib / player.cc
index 94bbf0c3d9b9c3cac552d19680ef34de4676a770..9c7c6471d124342828b1fbe71466f0df7372d831 100644 (file)
@@ -50,6 +50,8 @@
 #include <stdint.h>
 #include <algorithm>
 
+#include "i18n.h"
+
 #define LOG_GENERAL(...) _film->log()->log (String::compose (__VA_ARGS__), Log::TYPE_GENERAL);
 
 using std::list;
@@ -71,6 +73,7 @@ Player::Player (shared_ptr<const Film> f, shared_ptr<const Playlist> p)
        , _playlist (p)
        , _have_valid_pieces (false)
        , _approximate_size (false)
+       , _ignore_video (false)
 {
        _playlist_changed_connection = _playlist->Changed.connect (bind (&Player::playlist_changed, this));
        _playlist_content_changed_connection = _playlist->ContentChanged.connect (bind (&Player::content_changed, this, _1, _2, _3));
@@ -128,7 +131,7 @@ Player::setup_pieces ()
 
                shared_ptr<const DCPContent> dc = dynamic_pointer_cast<const DCPContent> (*i);
                if (dc) {
-                       decoder.reset (new DCPDecoder (dc, _film->log ()));
+                       decoder.reset (new DCPDecoder (dc));
                        frc = FrameRateChange (dc->video_frame_rate(), _film->video_frame_rate());
                }
 
@@ -171,6 +174,11 @@ Player::setup_pieces ()
                        frc = best_overlap_frc;
                }
 
+               shared_ptr<VideoDecoder> vd = dynamic_pointer_cast<VideoDecoder> (decoder);
+               if (vd && _ignore_video) {
+                       vd->set_ignore_video ();
+               }
+
                _pieces.push_back (shared_ptr<Piece> (new Piece (*i, decoder, frc.get ())));
        }
 
@@ -298,7 +306,7 @@ Player::black_player_video_frame (DCPTime time) const
 {
        return shared_ptr<PlayerVideo> (
                new PlayerVideo (
-                       shared_ptr<const ImageProxy> (new RawImageProxy (_black_image, _film->log ())),
+                       shared_ptr<const ImageProxy> (new RawImageProxy (_black_image)),
                        time,
                        Crop (),
                        optional<float> (),
@@ -319,7 +327,7 @@ Player::get_video (DCPTime time, bool accurate)
        if (!_have_valid_pieces) {
                setup_pieces ();
        }
-       
+
        list<shared_ptr<Piece> > ov = overlaps<VideoContent> (
                time,
                time + DCPTime::from_frames (1, _film->video_frame_rate ())
@@ -335,9 +343,9 @@ Player::get_video (DCPTime time, bool accurate)
 
                shared_ptr<Piece> piece = ov.back ();
                shared_ptr<VideoDecoder> decoder = dynamic_pointer_cast<VideoDecoder> (piece->decoder);
-               assert (decoder);
+               DCPOMATIC_ASSERT (decoder);
                shared_ptr<VideoContent> content = dynamic_pointer_cast<VideoContent> (piece->content);
-               assert (content);
+               DCPOMATIC_ASSERT (content);
 
                list<ContentVideo> content_video = decoder->get_video (dcp_to_content_video (piece, time), accurate);
                if (content_video.empty ()) {
@@ -413,9 +421,9 @@ Player::get_audio (DCPTime time, DCPTime length, bool accurate)
        for (list<shared_ptr<Piece> >::iterator i = ov.begin(); i != ov.end(); ++i) {
 
                shared_ptr<AudioContent> content = dynamic_pointer_cast<AudioContent> ((*i)->content);
-               assert (content);
+               DCPOMATIC_ASSERT (content);
                shared_ptr<AudioDecoder> decoder = dynamic_pointer_cast<AudioDecoder> ((*i)->decoder);
-               assert (decoder);
+               DCPOMATIC_ASSERT (decoder);
 
                if (content->audio_frame_rate() == 0) {
                        /* This AudioContent has no audio (e.g. if it is an FFmpegContent with no
@@ -486,13 +494,13 @@ Player::dcp_to_content_video (shared_ptr<const Piece> piece, DCPTime t) const
        s = DCPTime (min (piece->content->length_after_trim().get(), s.get()));
 
        /* Convert this to the content frame */
-       return DCPTime (s + piece->content->trim_start()).frames (_film->video_frame_rate()) * piece->frc.factor ();
+       return DCPTime (s + piece->content->trim_start()).frames (_film->video_frame_rate()) / piece->frc.factor ();
 }
 
 DCPTime
 Player::content_video_to_dcp (shared_ptr<const Piece> piece, VideoFrame f) const
 {
-       DCPTime t = DCPTime::from_frames (f / piece->frc.factor (), _film->video_frame_rate()) - piece->content->trim_start () + piece->content->position ();
+       DCPTime t = DCPTime::from_frames (f * piece->frc.factor (), _film->video_frame_rate()) - piece->content->trim_start () + piece->content->position ();
        if (t < DCPTime ()) {
                t = DCPTime ();
        }
@@ -576,6 +584,7 @@ Player::get_subtitles (DCPTime time, DCPTime length, bool starting)
                BOOST_FOREACH (ContentTextSubtitle& ts, text) {
                        BOOST_FOREACH (dcp::SubtitleString& s, ts.subs) {
                                s.set_v_position (s.v_position() + subtitle_content->subtitle_y_offset ());
+                               s.set_size (s.size() * max (subtitle_content->subtitle_x_scale(), subtitle_content->subtitle_y_scale()));
                                ps.text.push_back (s);
                        }
                }
@@ -583,3 +592,32 @@ Player::get_subtitles (DCPTime time, DCPTime length, bool starting)
 
        return ps;
 }
+
+list<shared_ptr<Font> >
+Player::get_subtitle_fonts ()
+{
+       if (!_have_valid_pieces) {
+               setup_pieces ();
+       }
+
+       list<shared_ptr<Font> > fonts;
+       BOOST_FOREACH (shared_ptr<Piece>& p, _pieces) {
+               shared_ptr<SubtitleContent> sc = dynamic_pointer_cast<SubtitleContent> (p->content);
+               if (sc) {
+                       /* XXX: things may go wrong if there are duplicate font IDs
+                          with different font files.
+                       */
+                       list<shared_ptr<Font> > f = sc->fonts ();
+                       copy (f.begin(), f.end(), back_inserter (fonts));
+               }
+       }
+
+       return fonts;
+}
+
+/** Set this player never to produce any video data */
+void
+Player::set_ignore_video ()
+{
+       _ignore_video = true;
+}