Merge branch '0.70-support'
[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) const
63 {
64         SourceFrame const s = (f * dcp_frame_rate(_film->frames_per_second()).skip) + _film->dcp_trim_start();
65         return _opt->frame_out_path (s, false);
66 }
67
68 string
69 MakeDCPJob::wav_path (libdcp::Channel c) const
70 {
71         return _opt->multichannel_audio_out_path (int (c), false);
72 }
73
74 void
75 MakeDCPJob::run ()
76 {
77         if (!_film->dcp_length()) {
78                 throw EncodeError ("cannot make a DCP when the source length is not known");
79         }
80
81         descend (0.9);
82         
83         string const dcp_path = _film->dir (_film->dcp_name());
84
85         /* Remove any old DCP */
86         boost::filesystem::remove_all (dcp_path);
87
88         DCPFrameRate const dfr = dcp_frame_rate (_film->frames_per_second ());
89
90         int frames = 0;
91         switch (_film->content_type ()) {
92         case VIDEO:
93                 /* Source frames -> DCP frames */
94                 frames = _film->dcp_length().get() / dfr.skip;
95                 break;
96         case STILL:
97                 frames = _film->still_duration() * 24;
98                 break;
99         }
100
101         libdcp::DCP dcp (_film->dir (_film->dcp_name()));
102         dcp.Progress.connect (boost::bind (&MakeDCPJob::dcp_progress, this, _1));
103
104         shared_ptr<libdcp::CPL> cpl (
105                 new libdcp::CPL (_film->dir (_film->dcp_name()), _film->dcp_name(), _film->dcp_content_type()->libdcp_kind (), frames, dfr.frames_per_second)
106                 );
107         
108         dcp.add_cpl (cpl);
109
110         descend (0.8);
111
112         shared_ptr<libdcp::MonoPictureAsset> pa (
113                 new libdcp::MonoPictureAsset (
114                         boost::bind (&MakeDCPJob::j2c_path, this, _1),
115                         _film->dir (_film->dcp_name()),
116                         "video.mxf",
117                         &dcp.Progress,
118                         dfr.frames_per_second,
119                         frames,
120                         _opt->out_size
121                         )
122                 );
123         
124         ascend ();
125         
126         shared_ptr<libdcp::SoundAsset> sa;
127         
128         if (_film->audio_channels() > 0) {
129                 descend (0.1);
130                 sa.reset (
131                         new libdcp::SoundAsset (
132                                 boost::bind (&MakeDCPJob::wav_path, this, _1),
133                                 _film->dir (_film->dcp_name()),
134                                 "audio.mxf",
135                                 &dcp.Progress,
136                                 dfr.frames_per_second,
137                                 frames,
138                                 dcp_audio_channels (_film->audio_channels())
139                                 )
140                         );
141                 ascend ();
142         }
143
144         descend (0.05);
145         cpl->add_reel (shared_ptr<libdcp::Reel> (new libdcp::Reel (pa, sa, shared_ptr<libdcp::SubtitleAsset> ())));
146         ascend ();
147                 
148         descend (0.05);
149         dcp.write_xml ();
150         ascend ();
151                 
152         set_progress (1);
153         set_state (FINISHED_OK);
154 }
155
156 void
157 MakeDCPJob::dcp_progress (float p)
158 {
159         set_progress (p);
160 }