Some basics of AudioMapping.
[dcpomatic.git] / src / lib / ffmpeg_content.cc
index 719c4cb53ecf03488d8266954b6309205657bb5e..d730f3ecbc6057c4a65a660f962b8d3bbf590c52 100644 (file)
@@ -1,3 +1,5 @@
+/* -*- c-basic-offset: 8; default-tab-width: 8; -*- */
+
 /*
     Copyright (C) 2013 Carl Hetherington <cth@carlh.net>
 
@@ -88,6 +90,7 @@ FFmpegContent::as_xml (xmlpp::Node* node) const
        node->add_child("Type")->add_child_text ("FFmpeg");
        Content::as_xml (node);
        VideoContent::as_xml (node);
+       AudioContent::as_xml (node);
 
        boost::mutex::scoped_lock lm (_mutex);
 
@@ -206,7 +209,7 @@ FFmpegContent::audio_length () const
                 return 0;
         }
         
-        return video_frames_to_audio_frames (_video_length, audio_frame_rate(), video_frame_rate());
+        return video_frames_to_audio_frames (_video_length, content_audio_frame_rate(), video_frame_rate());
 }
 
 int
@@ -220,7 +223,7 @@ FFmpegContent::audio_channels () const
 }
 
 int
-FFmpegContent::audio_frame_rate () const
+FFmpegContent::content_audio_frame_rate () const
 {
         if (!_audio_stream) {
                 return 0;
@@ -229,6 +232,27 @@ FFmpegContent::audio_frame_rate () const
         return _audio_stream->frame_rate;
 }
 
+int
+FFmpegContent::output_audio_frame_rate (shared_ptr<const Film> film) const
+{
+       /* Resample to a DCI-approved sample rate */
+       double t = dcp_audio_frame_rate (content_audio_frame_rate ());
+
+       FrameRateConversion frc (video_frame_rate(), film->dcp_video_frame_rate());
+
+       /* Compensate if the DCP is being run at a different frame rate
+          to the source; that is, if the video is run such that it will
+          look different in the DCP compared to the source (slower or faster).
+          skip/repeat doesn't come into effect here.
+       */
+
+       if (frc.change_speed) {
+               t *= video_frame_rate() * frc.factor() / film->dcp_video_frame_rate();
+       }
+
+       return rint (t);
+}
+
 bool
 operator== (FFmpegSubtitleStream const & a, FFmpegSubtitleStream const & b)
 {
@@ -242,6 +266,7 @@ operator== (FFmpegAudioStream const & a, FFmpegAudioStream const & b)
 }
 
 FFmpegAudioStream::FFmpegAudioStream (shared_ptr<const cxml::Node> node)
+       : mapping (node->node_child ("Mapping"))
 {
        name = node->string_child ("Name");
        id = node->number_child<int> ("Id");
@@ -256,6 +281,7 @@ FFmpegAudioStream::as_xml (xmlpp::Node* root) const
        root->add_child("Id")->add_child_text (lexical_cast<string> (id));
        root->add_child("FrameRate")->add_child_text (lexical_cast<string> (frame_rate));
        root->add_child("Channels")->add_child_text (lexical_cast<string> (channels));
+       mapping.as_xml (root->add_child("Mapping"));
 }
 
 /** Construct a SubtitleStream from a value returned from to_string().
@@ -280,3 +306,20 @@ FFmpegContent::clone () const
 {
        return shared_ptr<Content> (new FFmpegContent (*this));
 }
+
+Time
+FFmpegContent::length (shared_ptr<const Film> film) const
+{
+       FrameRateConversion frc (video_frame_rate (), film->dcp_video_frame_rate ());
+       return video_length() * frc.factor() * TIME_HZ / film->dcp_video_frame_rate ();
+}
+
+AudioMapping
+FFmpegContent::audio_mapping () const
+{
+       if (!_audio_stream) {
+               return AudioMapping ();
+       }
+       
+       return _audio_stream->mapping;
+}