67a2d8b13e35e8f16d1565660a37b068bf61fa65
[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 <iostream>
25 #include <boost/filesystem.hpp>
26 #include <libdcp/dcp.h>
27 #include <libdcp/picture_asset.h>
28 #include <libdcp/sound_asset.h>
29 #include <libdcp/reel.h>
30 extern "C" {
31 #include <libavutil/pixdesc.h>
32 }
33 #include "make_dcp_job.h"
34 #include "dcp_content_type.h"
35 #include "exceptions.h"
36 #include "options.h"
37 #include "imagemagick_decoder.h"
38 #include "film.h"
39
40 using std::string;
41 using std::cout;
42 using boost::shared_ptr;
43
44 /** @param f Film we are making the DCP for.
45  *  @param o Options.
46  */
47 MakeDCPJob::MakeDCPJob (shared_ptr<Film> f, shared_ptr<const EncodeOptions> o, shared_ptr<Job> req)
48         : Job (f, req)
49         , _opt (o)
50 {
51         
52 }
53
54 string
55 MakeDCPJob::name () const
56 {
57         return String::compose ("Make DCP for %1", _film->name());
58 }
59
60 /** @param f DCP frame index */
61 string
62 MakeDCPJob::j2c_path (int f, int offset) const
63 {
64         DCPFrameRate dfr (_film->frames_per_second());
65         int const mult = dfr.skip ? 2 : 1;
66         SourceFrame const s = ((f + offset) * mult) + _film->dcp_trim_start();
67         return _opt->frame_out_path (s, false);
68 }
69
70 string
71 MakeDCPJob::wav_path (libdcp::Channel c) const
72 {
73         return _opt->multichannel_audio_out_path (int (c), false);
74 }
75
76 void
77 MakeDCPJob::run ()
78 {
79         if (!_film->dcp_length()) {
80                 throw EncodeError ("cannot make a DCP when the source length is not known");
81         }
82
83         descend (0.9);
84         
85         string const dcp_path = _film->dir (_film->dcp_name());
86
87         /* Remove any old DCP */
88         boost::filesystem::remove_all (dcp_path);
89
90         DCPFrameRate const dfr (_film->frames_per_second ());
91
92         int frames = 0;
93         switch (_film->content_type ()) {
94         case VIDEO:
95                 /* Source frames -> DCP frames */
96                 frames = _film->dcp_length().get();
97                 if (dfr.skip) {
98                         frames /= 2;
99                 }
100                 break;
101         case STILL:
102                 frames = _film->still_duration() * 24;
103                 break;
104         }
105
106         libdcp::DCP dcp (_film->dir (_film->dcp_name()));
107         dcp.Progress.connect (boost::bind (&MakeDCPJob::dcp_progress, this, _1));
108
109         shared_ptr<libdcp::CPL> cpl (
110                 new libdcp::CPL (_film->dir (_film->dcp_name()), _film->dcp_name(), _film->dcp_content_type()->libdcp_kind (), frames, dfr.frames_per_second)
111                 );
112         
113         dcp.add_cpl (cpl);
114
115         int frames_per_reel = 0;
116         if (_film->reel_size()) {
117                 frames_per_reel = (_film->reel_size().get() / (_film->j2k_bandwidth() / 8)) * dfr.frames_per_second;
118         } else {
119                 frames_per_reel = frames;
120         }
121
122         int frames_done = 0;
123         int reel = 0;
124
125         while (frames_done < frames) {
126
127                 descend (float (frames_per_reel) / frames);
128
129                 int this_time = std::min (frames_per_reel, (frames - frames_done));
130
131                 descend (0.8);
132
133                 shared_ptr<libdcp::MonoPictureAsset> pa (
134                         new libdcp::MonoPictureAsset (
135                                 boost::bind (&MakeDCPJob::j2c_path, this, _1, frames_done),
136                                 _film->dir (_film->dcp_name()),
137                                 String::compose ("video_%1.mxf", reel),
138                                 &dcp.Progress,
139                                 dfr.frames_per_second,
140                                 this_time,
141                                 _opt->out_size.width,
142                                 _opt->out_size.height
143                                 )
144                         );
145         
146                 ascend ();
147                 
148                 shared_ptr<libdcp::SoundAsset> sa;
149                 
150                 if (_film->audio_channels() > 0) {
151                         descend (0.1);
152                         sa.reset (
153                                 new libdcp::SoundAsset (
154                                         boost::bind (&MakeDCPJob::wav_path, this, _1),
155                                         _film->dir (_film->dcp_name()),
156                                         String::compose ("audio_%1.mxf", reel),
157                                         &dcp.Progress,
158                                         dfr.frames_per_second,
159                                         this_time,
160                                         frames_done,
161                                         dcp_audio_channels (_film->audio_channels())
162                                         )
163                                 );
164                         ascend ();
165                 }
166
167                 descend (0.1);
168                 cpl->add_reel (shared_ptr<libdcp::Reel> (new libdcp::Reel (pa, sa, shared_ptr<libdcp::SubtitleAsset> ())));
169                 ascend ();
170                 
171                 frames_done += frames_per_reel;
172                 ++reel;
173
174                 ascend ();
175         }
176
177         ascend ();
178
179         descend (0.1);
180         dcp.write_xml ();
181         ascend ();
182                 
183         set_progress (1);
184         set_state (FINISHED_OK);
185 }
186
187 void
188 MakeDCPJob::dcp_progress (float p)
189 {
190         set_progress (p);
191 }