Basic email of KDMs works.
[dcpomatic.git] / src / lib / make_dcp_job.cc
index 81deb835d4df23f73a80f08f782e1c98df5caff0..37c9ca6206429d2a864e1f21e11c0c1f48805905 100644 (file)
  *  @brief A job to create DCPs.
  */
 
+#include <iostream>
 #include <boost/filesystem.hpp>
+#include <libdcp/dcp.h>
+#include <libdcp/picture_asset.h>
+#include <libdcp/sound_asset.h>
+#include <libdcp/reel.h>
 extern "C" {
 #include <libavutil/pixdesc.h>
 }
 #include "make_dcp_job.h"
-#include "film_state.h"
 #include "dcp_content_type.h"
 #include "exceptions.h"
+#include "options.h"
+#include "imagemagick_decoder.h"
+#include "film.h"
 
-using namespace std;
-using namespace boost;
+using std::string;
+using std::cout;
+using boost::shared_ptr;
 
-/** @param s FilmState of the Film we are making the DCP for.
+/** @param f Film we are making the DCP for.
  *  @param o Options.
- *  @param l Log.
  */
-MakeDCPJob::MakeDCPJob (shared_ptr<const FilmState> s, shared_ptr<const Options> o, Log* l)
-       : ShellCommandJob (s, o, l)
+MakeDCPJob::MakeDCPJob (shared_ptr<Film> f, shared_ptr<const EncodeOptions> o, shared_ptr<Job> req)
+       : Job (f, req)
+       , _opt (o)
 {
        
 }
@@ -46,49 +54,135 @@ MakeDCPJob::MakeDCPJob (shared_ptr<const FilmState> s, shared_ptr<const Options>
 string
 MakeDCPJob::name () const
 {
-       stringstream s;
-       s << "Make DCP for " << _fs->name;
-       return s.str ();
+       return String::compose ("Make DCP for %1", _film->name());
+}
+
+/** @param f DCP frame index */
+string
+MakeDCPJob::j2c_path (int f, int offset) const
+{
+       SourceFrame const s = ((f + offset) * dcp_frame_rate(_film->frames_per_second()).skip) + _film->dcp_trim_start();
+       return _opt->frame_out_path (s, false);
+}
+
+string
+MakeDCPJob::wav_path (libdcp::Channel c) const
+{
+       return _opt->multichannel_audio_out_path (int (c), false);
 }
 
 void
 MakeDCPJob::run ()
 {
-       set_progress_unknown ();
+       if (!_film->dcp_length()) {
+               throw EncodeError ("cannot make a DCP when the source length is not known");
+       }
 
-       string const dcp_path = _fs->dir (_fs->name);
+       descend (0.9);
        
-       /* Check that we have our prerequisites */
+       string const dcp_path = _film->dir (_film->dcp_name());
 
-       if (!filesystem::exists (filesystem::path (_fs->file ("video.mxf")))) {
-               throw EncodeError ("missing video.mxf");
+       /* Remove any old DCP */
+       boost::filesystem::remove_all (dcp_path);
+
+       DCPFrameRate const dfr = dcp_frame_rate (_film->frames_per_second ());
+
+       int frames = 0;
+       switch (_film->content_type ()) {
+       case VIDEO:
+               /* Source frames -> DCP frames */
+               frames = _film->dcp_length().get() / dfr.skip;
+               break;
+       case STILL:
+               frames = _film->still_duration() * 24;
+               break;
        }
 
-       bool const have_audio = filesystem::exists (filesystem::path (_fs->file ("audio.mxf")));
-
-       /* Remove any old DCP */
-       filesystem::remove_all (dcp_path);
+       libdcp::DCP dcp (_film->dir (_film->dcp_name()));
+       dcp.Progress.connect (boost::bind (&MakeDCPJob::dcp_progress, this, _1));
 
-       /* Re-make the DCP directory */
-       _fs->dir (_fs->name);
+       shared_ptr<libdcp::CPL> cpl (
+               new libdcp::CPL (_film->dir (_film->dcp_name()), _film->dcp_name(), _film->dcp_content_type()->libdcp_kind (), frames, dfr.frames_per_second)
+               );
        
-       stringstream c;
-       c << "cd \"" << dcp_path << "\" && "
-         << " opendcp_xml -d -a " << _fs->name
-         << " -t \"" << _fs->name << "\""
-         << " -k " << _fs->dcp_content_type->opendcp_name()
-         << " --reel \"" << _fs->file ("video.mxf") << "\"";
-       
-       if (have_audio) {
-               c << " \"" << _fs->file ("audio.mxf") << "\"";
+       dcp.add_cpl (cpl);
+
+       int frames_per_reel = 0;
+       if (_film->reel_size()) {
+               frames_per_reel = (_film->reel_size().get() / (_film->j2k_bandwidth() / 8)) * dfr.frames_per_second;
+       } else {
+               frames_per_reel = frames;
        }
 
-       command (c.str ());
+       int frames_done = 0;
+       int reel = 0;
+
+       while (frames_done < frames) {
+
+               descend (float (frames_per_reel) / frames);
+
+               int this_time = std::min (frames_per_reel, (frames - frames_done));
+
+               descend (0.8);
 
-       filesystem::rename (filesystem::path (_fs->file ("video.mxf")), filesystem::path (dcp_path + "/video.mxf"));
-       if (have_audio) {
-               filesystem::rename (filesystem::path (_fs->file ("audio.mxf")), filesystem::path (dcp_path + "/audio.mxf"));
+               shared_ptr<libdcp::MonoPictureAsset> pa (
+                       new libdcp::MonoPictureAsset (
+                               boost::bind (&MakeDCPJob::j2c_path, this, _1, frames_done),
+                               _film->dir (_film->dcp_name()),
+                               String::compose ("video_%1.mxf", reel),
+                               &dcp.Progress,
+                               dfr.frames_per_second,
+                               this_time,
+                               _opt->out_size.width,
+                               _opt->out_size.height,
+                               _film->encrypted()
+                               )
+                       );
+       
+               ascend ();
+               
+               shared_ptr<libdcp::SoundAsset> sa;
+               
+               if (_film->audio_channels() > 0) {
+                       descend (0.1);
+                       sa.reset (
+                               new libdcp::SoundAsset (
+                                       boost::bind (&MakeDCPJob::wav_path, this, _1),
+                                       _film->dir (_film->dcp_name()),
+                                       String::compose ("audio_%1.mxf", reel),
+                                       &dcp.Progress,
+                                       dfr.frames_per_second,
+                                       this_time,
+                                       frames_done,
+                                       dcp_audio_channels (_film->audio_channels()),
+                                       _film->encrypted()
+                                       )
+                               );
+                       ascend ();
+               }
+
+               descend (0.1);
+               cpl->add_reel (shared_ptr<libdcp::Reel> (new libdcp::Reel (pa, sa, shared_ptr<libdcp::SubtitleAsset> ())));
+               ascend ();
+               
+               frames_done += frames_per_reel;
+               ++reel;
+
+               ascend ();
        }
 
+       ascend ();
+
+       descend (0.1);
+       dcp.write_xml ();
+       ascend ();
+               
        set_progress (1);
+       set_state (FINISHED_OK);
+}
+
+void
+MakeDCPJob::dcp_progress (float p)
+{
+       set_progress (p);
 }