bf01119e4995d9bb520adc30348cea9765e5dfdd
[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 "dcp_content_type.h"
34 #include "exceptions.h"
35 #include "options.h"
36 #include "imagemagick_decoder.h"
37 #include "film.h"
38
39 using std::string;
40 using boost::shared_ptr;
41
42 /** @param f Film we are making the DCP for.
43  *  @param o Options.
44  */
45 MakeDCPJob::MakeDCPJob (shared_ptr<Film> f, shared_ptr<const Options> o, shared_ptr<Job> req)
46         : Job (f, req)
47         , _opt (o)
48 {
49         
50 }
51
52 string
53 MakeDCPJob::name () const
54 {
55         return String::compose ("Make DCP for %1", _film->name());
56 }
57
58 string
59 MakeDCPJob::j2c_path (int f) const
60 {
61         return _opt->frame_out_path (f, false);
62 }
63
64 string
65 MakeDCPJob::wav_path (libdcp::Channel c) const
66 {
67         return _opt->multichannel_audio_out_path (int (c), false);
68 }
69
70 void
71 MakeDCPJob::run ()
72 {
73         if (!_film->dcp_length()) {
74                 throw EncodeError ("cannot make a DCP when the source length is not known");
75         }
76         
77         string const dcp_path = _film->dir (_film->dcp_name());
78
79         /* Remove any old DCP */
80         boost::filesystem::remove_all (dcp_path);
81
82         int frames = 0;
83         switch (_film->content_type ()) {
84         case VIDEO:
85                 frames = _film->dcp_length().get();
86                 break;
87         case STILL:
88                 frames = _film->still_duration() * ImageMagickDecoder::static_frames_per_second ();
89                 break;
90         }
91         
92         libdcp::DCP dcp (_film->dir (_film->dcp_name()));
93         dcp.Progress.connect (boost::bind (&MakeDCPJob::dcp_progress, this, _1));
94
95         shared_ptr<libdcp::CPL> cpl (
96                 new libdcp::CPL (_film->dir (_film->dcp_name()), _film->dcp_name(), _film->dcp_content_type()->libdcp_kind (), frames, rint (_film->frames_per_second()))
97                 );
98         
99         dcp.add_cpl (cpl);
100
101         descend (0.9);
102         shared_ptr<libdcp::MonoPictureAsset> pa (
103                 new libdcp::MonoPictureAsset (
104                         boost::bind (&MakeDCPJob::j2c_path, this, _1),
105                         _film->dir (_film->dcp_name()),
106                         "video.mxf",
107                         &dcp.Progress,
108                         rint (_film->frames_per_second()),
109                         frames,
110                         _opt->out_size.width,
111                         _opt->out_size.height
112                         )
113                 );
114         
115         ascend ();
116
117         shared_ptr<libdcp::SoundAsset> sa;
118
119         if (_film->audio_channels() > 0) {
120                 descend (0.1);
121                 sa.reset (
122                         new libdcp::SoundAsset (
123                                 boost::bind (&MakeDCPJob::wav_path, this, _1),
124                                 _film->dir (_film->dcp_name()),
125                                 "audio.mxf",
126                                 &dcp.Progress,
127                                 rint (_film->frames_per_second()),
128                                 frames,
129                                 _film->audio_channels()
130                                 )
131                         );
132                 ascend ();
133         }
134
135         cpl->add_reel (shared_ptr<libdcp::Reel> (new libdcp::Reel (pa, sa, shared_ptr<libdcp::SubtitleAsset> ())));
136         dcp.write_xml ();
137
138         set_progress (1);
139         set_state (FINISHED_OK);
140 }
141
142 void
143 MakeDCPJob::dcp_progress (float p)
144 {
145         set_progress (p);
146 }