fcfce4b07bccc6ada73799e79e2f68e8dc6064db
[dcpomatic.git] / src / lib / make_dcp_job.cc
1 /*
2     Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 /** @file src/make_dcp_job.cc
21  *  @brief A job to create DCPs.
22  */
23
24 #include <boost/filesystem.hpp>
25 #include <libdcp/dcp.h>
26 #include <libdcp/picture_asset.h>
27 #include <libdcp/sound_asset.h>
28 #include <libdcp/reel.h>
29 extern "C" {
30 #include <libavutil/pixdesc.h>
31 }
32 #include "make_dcp_job.h"
33 #include "film_state.h"
34 #include "dcp_content_type.h"
35 #include "exceptions.h"
36 #include "options.h"
37 #include "imagemagick_decoder.h"
38
39 using namespace std;
40 using namespace boost;
41
42 /** @param s FilmState of the Film we are making the DCP for.
43  *  @param o Options.
44  *  @param l Log.
45  */
46 MakeDCPJob::MakeDCPJob (shared_ptr<const FilmState> s, shared_ptr<const Options> o, Log* l, shared_ptr<Job> req)
47         : Job (s, l, req)
48         , _opt (o)
49 {
50         
51 }
52
53 string
54 MakeDCPJob::name () const
55 {
56         return String::compose ("Make DCP for %1", _fs->name());
57 }
58
59 string
60 MakeDCPJob::j2c_path (int f) const
61 {
62         return _opt->frame_out_path (f, false);
63 }
64
65 string
66 MakeDCPJob::wav_path (libdcp::Channel c) const
67 {
68         return _opt->multichannel_audio_out_path (int (c), false);
69 }
70
71 void
72 MakeDCPJob::run ()
73 {
74         string const dcp_path = _fs->dir (_fs->dcp_name());
75
76         /* Remove any old DCP */
77         filesystem::remove_all (dcp_path);
78
79         int frames = 0;
80         switch (_fs->content_type ()) {
81         case VIDEO:
82                 frames = _fs->dcp_length ();
83                 break;
84         case STILL:
85                 frames = _fs->still_duration() * ImageMagickDecoder::static_frames_per_second ();
86                 break;
87         }
88         
89         libdcp::DCP dcp (_fs->dir (_fs->dcp_name()));
90         dcp.Progress.connect (sigc::mem_fun (*this, &MakeDCPJob::dcp_progress));
91
92         shared_ptr<libdcp::CPL> cpl (
93                 new libdcp::CPL (_fs->dir (_fs->dcp_name()), _fs->dcp_name(), _fs->dcp_content_type()->libdcp_kind (), frames, rint (_fs->frames_per_second()))
94                 );
95         
96         dcp.add_cpl (cpl);
97
98         descend (0.9);
99         shared_ptr<libdcp::MonoPictureAsset> pa (
100                 new libdcp::MonoPictureAsset (
101                         sigc::mem_fun (*this, &MakeDCPJob::j2c_path),
102                         _fs->dir (_fs->dcp_name()),
103                         "video.mxf",
104                         &dcp.Progress,
105                         rint (_fs->frames_per_second()),
106                         frames,
107                         _opt->out_size.width,
108                         _opt->out_size.height
109                         )
110                 );
111         
112         ascend ();
113
114         shared_ptr<libdcp::SoundAsset> sa;
115
116         if (_fs->audio_channels() > 0) {
117                 descend (0.1);
118                 sa.reset (
119                         new libdcp::SoundAsset (
120                                 sigc::mem_fun (*this, &MakeDCPJob::wav_path),
121                                 _fs->dir (_fs->dcp_name()),
122                                 "audio.mxf",
123                                 &dcp.Progress,
124                                 rint (_fs->frames_per_second()),
125                                 frames,
126                                 _fs->audio_channels()
127                                 )
128                         );
129                 ascend ();
130         }
131
132         cpl->add_reel (shared_ptr<libdcp::Reel> (new libdcp::Reel (pa, sa, shared_ptr<libdcp::SubtitleAsset> ())));
133         dcp.write_xml ();
134
135         set_progress (1);
136         set_state (FINISHED_OK);
137 }
138
139 void
140 MakeDCPJob::dcp_progress (float p)
141 {
142         set_progress (p);
143 }