TODO.
[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)
47         : Job (s, o, l)
48 {
49         
50 }
51
52 string
53 MakeDCPJob::name () const
54 {
55         stringstream s;
56         s << "Make DCP for " << _fs->name;
57         return s.str ();
58 }
59
60 string
61 MakeDCPJob::j2c_path (int f) const
62 {
63         return _opt->frame_out_path (f, false);
64 }
65
66 string
67 MakeDCPJob::wav_path (libdcp::Channel c) const
68 {
69         return _opt->multichannel_audio_out_path (int (c), false);
70 }
71
72 void
73 MakeDCPJob::run ()
74 {
75         string const dcp_path = _fs->dir (_fs->name);
76
77         /* Remove any old DCP */
78         filesystem::remove_all (dcp_path);
79
80         int frames = 0;
81         switch (_fs->content_type ()) {
82         case VIDEO:
83                 frames = _fs->dcp_frames ? _fs->dcp_frames : _fs->length;
84                 break;
85         case STILL:
86                 frames = _fs->still_duration * ImageMagickDecoder::static_frames_per_second ();
87                 break;
88         }
89         
90         libdcp::DCP dcp (_fs->dir (_fs->name), _fs->name, _fs->dcp_content_type->libdcp_kind (), rint (_fs->frames_per_second), frames);
91         dcp.Progress.connect (sigc::mem_fun (*this, &MakeDCPJob::dcp_progress));
92
93         descend (0.9);
94         shared_ptr<libdcp::MonoPictureAsset> pa (
95                 new libdcp::MonoPictureAsset (
96                         sigc::mem_fun (*this, &MakeDCPJob::j2c_path),
97                         _fs->dir (_fs->name),
98                         "video.mxf",
99                         &dcp.Progress,
100                         rint (_fs->frames_per_second),
101                         frames,
102                         _opt->out_size.width,
103                         _opt->out_size.height
104                         )
105                 );
106         
107         ascend ();
108
109         shared_ptr<libdcp::SoundAsset> sa;
110
111         if (_fs->audio_channels > 0) {
112                 descend (0.1);
113                 sa.reset (
114                         new libdcp::SoundAsset (
115                                 sigc::mem_fun (*this, &MakeDCPJob::wav_path),
116                                 _fs->dir (_fs->name),
117                                 "audio.mxf",
118                                 &dcp.Progress,
119                                 rint (_fs->frames_per_second),
120                                 frames,
121                                 _fs->audio_channels
122                                 )
123                         );
124                 ascend ();
125         }
126
127         dcp.add_reel (shared_ptr<libdcp::Reel> (new libdcp::Reel (pa, sa, shared_ptr<libdcp::SubtitleAsset> ())));
128         dcp.write_xml ();
129
130         set_progress (1);
131         set_state (FINISHED_OK);
132 }
133
134 void
135 MakeDCPJob::dcp_progress (float p)
136 {
137         set_progress (p);
138 }