b0b8a2a1af0d5a959d1c74f26a53dbc79a273605
[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 extern "C" {
27 #include <libavutil/pixdesc.h>
28 }
29 #include "make_dcp_job.h"
30 #include "film_state.h"
31 #include "dcp_content_type.h"
32 #include "exceptions.h"
33 #include "options.h"
34
35 using namespace std;
36 using namespace boost;
37
38 /** @param s FilmState of the Film we are making the DCP for.
39  *  @param o Options.
40  *  @param l Log.
41  */
42 MakeDCPJob::MakeDCPJob (shared_ptr<const FilmState> s, shared_ptr<const Options> o, Log* l)
43         : Job (s, o, l)
44 {
45         
46 }
47
48 string
49 MakeDCPJob::name () const
50 {
51         stringstream s;
52         s << "Make DCP for " << _fs->name;
53         return s.str ();
54 }
55
56 void
57 MakeDCPJob::run ()
58 {
59         string const dcp_path = _fs->dir (_fs->name);
60
61         /* Remove any old DCP */
62         filesystem::remove_all (dcp_path);
63
64         libdcp::DCP dcp (_fs->dir (_fs->name), _fs->name, _fs->dcp_content_type->libdcp_type (), rint (_fs->frames_per_second), _fs->length);
65         dcp.Progress.connect (sigc::mem_fun (*this, &MakeDCPJob::dcp_progress));
66
67         list<string> j2cs;
68         int f = _fs->dcp_frames ? _fs->dcp_frames : _fs->length;
69         for (int i = 0; i < f; ++i) {
70                 j2cs.push_back (_opt->frame_out_path (i, false));
71         }
72
73         descend (0.9);
74         dcp.add_picture_asset (j2cs, _opt->out_size.width, _opt->out_size.height);
75         ascend ();
76
77         list<string> wavs;
78         for (int i = 0; i < _fs->audio_channels; ++i) {
79                 wavs.push_back (_opt->multichannel_audio_out_path (i, false));
80         }
81
82         if (!wavs.empty ()) {
83                 descend (0.1);
84                 dcp.add_sound_asset (wavs);
85                 ascend ();
86         }
87
88         dcp.write_xml ();
89
90         set_progress (1);
91         set_state (FINISHED_OK);
92 }
93
94 void
95 MakeDCPJob::dcp_progress (float p)
96 {
97         set_progress (p);
98 }