Untested use of Frame for video/audio content lengths.
[dcpomatic.git] / src / lib / ffmpeg_content.cc
index bb4e022308dc9882dff57d5a0959d0a014cf9cab..9acc883fd0c306ebb01db70206b3693940e54f6a 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2013-2014 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2013-2015 Carl Hetherington <cth@carlh.net>
 
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
 
 */
 
-extern "C" {
-#include <libavformat/avformat.h>
-}
-#include <libcxml/cxml.h>
-#include <dcp/raw_convert.h>
 #include "ffmpeg_content.h"
 #include "ffmpeg_examiner.h"
 #include "ffmpeg_subtitle_stream.h"
@@ -35,6 +30,11 @@ extern "C" {
 #include "exceptions.h"
 #include "frame_rate_change.h"
 #include "safe_stringstream.h"
+#include "raw_convert.h"
+#include <libcxml/cxml.h>
+extern "C" {
+#include <libavformat/avformat.h>
+}
 
 #include "i18n.h"
 
@@ -47,7 +47,6 @@ using std::cout;
 using std::pair;
 using boost::shared_ptr;
 using boost::dynamic_pointer_cast;
-using dcp::raw_convert;
 
 int const FFmpegContentProperty::SUBTITLE_STREAMS = 100;
 int const FFmpegContentProperty::SUBTITLE_STREAM = 101;
@@ -106,7 +105,7 @@ FFmpegContent::FFmpegContent (shared_ptr<const Film> f, vector<boost::shared_ptr
        , SubtitleContent (f, c)
 {
        shared_ptr<FFmpegContent> ref = dynamic_pointer_cast<FFmpegContent> (c[0]);
-       assert (ref);
+       DCPOMATIC_ASSERT (ref);
 
        for (size_t i = 0; i < c.size(); ++i) {
                shared_ptr<FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (c[i]);
@@ -169,11 +168,11 @@ FFmpegContent::examine (shared_ptr<Job> job)
 
        Content::examine (job);
 
-       shared_ptr<FFmpegExaminer> examiner (new FFmpegExaminer (shared_from_this ()));
+       shared_ptr<FFmpegExaminer> examiner (new FFmpegExaminer (shared_from_this (), job));
        take_from_video_examiner (examiner);
 
        shared_ptr<const Film> film = _film.lock ();
-       assert (film);
+       DCPOMATIC_ASSERT (film);
 
        {
                boost::mutex::scoped_lock lm (_mutex);
@@ -228,21 +227,6 @@ FFmpegContent::technical_summary () const
                        );
 }
 
-string
-FFmpegContent::information () const
-{
-       if (video_length() == ContentTime (0) || video_frame_rate() == 0) {
-               return "";
-       }
-       
-       SafeStringStream s;
-       
-       s << String::compose (_("%1 frames; %2 frames per second"), video_length_after_3d_combine().frames (video_frame_rate()), video_frame_rate()) << "\n";
-       s << VideoContent::information ();
-
-       return s.str ();
-}
-
 void
 FFmpegContent::set_subtitle_stream (shared_ptr<FFmpegSubtitleStream> s)
 {
@@ -265,14 +249,18 @@ FFmpegContent::set_audio_stream (shared_ptr<FFmpegAudioStream> s)
        signal_changed (FFmpegContentProperty::AUDIO_STREAM);
 }
 
-ContentTime
+Frame
 FFmpegContent::audio_length () const
 {
        if (!audio_stream ()) {
-               return ContentTime ();
+               return 0;
        }
 
-       return video_length ();
+       /* We're talking about the content's audio length here, at the content's frame
+          rate.  We assume it's the same as the video's length, and we can just convert
+          using the content's rates.
+       */
+       return (video_length () / video_frame_rate ()) * audio_frame_rate ();
 }
 
 int
@@ -315,8 +303,9 @@ DCPTime
 FFmpegContent::full_length () const
 {
        shared_ptr<const Film> film = _film.lock ();
-       assert (film);
-       return DCPTime (video_length_after_3d_combine(), FrameRateChange (video_frame_rate (), film->video_frame_rate ()));
+       DCPOMATIC_ASSERT (film);
+       FrameRateChange const frc (video_frame_rate (), film->video_frame_rate ());
+       return DCPTime::from_frames (rint (video_length_after_3d_combine() * frc.factor()), film->video_frame_rate());
 }
 
 AudioMapping
@@ -381,33 +370,22 @@ FFmpegContent::audio_analysis_path () const
           analyses for each stream.
        */
 
-       boost::filesystem::path p = film->audio_analysis_dir ();
-       string name = digest ();
+       boost::filesystem::path p = AudioContent::audio_analysis_path ();
        if (audio_stream ()) {
-               name += "_" + audio_stream()->identifier ();
+               p = p.string() + "_" + audio_stream()->identifier ();
        }
-       p /= name;
        return p;
 }
 
 list<ContentTimePeriod>
 FFmpegContent::subtitles_during (ContentTimePeriod period, bool starting) const
 {
-       list<ContentTimePeriod> d;
-       
        shared_ptr<FFmpegSubtitleStream> stream = subtitle_stream ();
        if (!stream) {
-               return d;
+               return list<ContentTimePeriod> ();
        }
 
-       /* XXX: inefficient */
-       for (vector<ContentTimePeriod>::const_iterator i = stream->periods.begin(); i != stream->periods.end(); ++i) {
-               if ((starting && period.contains (i->from)) || (!starting && period.overlaps (*i))) {
-                       d.push_back (*i);
-               }
-       }
-
-       return d;
+       return stream->subtitles_during (period, starting);
 }
 
 bool
@@ -415,3 +393,19 @@ FFmpegContent::has_subtitles () const
 {
        return !subtitle_streams().empty ();
 }
+
+void
+FFmpegContent::set_default_colour_conversion ()
+{
+       dcp::Size const s = video_size ();
+
+       boost::mutex::scoped_lock lm (_mutex);
+
+       if (s.width < 1080) {
+               _colour_conversion = PresetColourConversion::from_id ("rec601").conversion;
+       } else {
+               _colour_conversion = PresetColourConversion::from_id ("rec709").conversion;
+       }
+}
+
+