Update for newer libdcp; add start of manual.
[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 #include "imagemagick_decoder.h"
35
36 using namespace std;
37 using namespace boost;
38
39 /** @param s FilmState of the Film we are making the DCP for.
40  *  @param o Options.
41  *  @param l Log.
42  */
43 MakeDCPJob::MakeDCPJob (shared_ptr<const FilmState> s, shared_ptr<const Options> o, Log* l)
44         : Job (s, o, l)
45 {
46         
47 }
48
49 string
50 MakeDCPJob::name () const
51 {
52         stringstream s;
53         s << "Make DCP for " << _fs->name;
54         return s.str ();
55 }
56
57 string
58 MakeDCPJob::j2c_path (int f) const
59 {
60         return _opt->frame_out_path (f, false);
61 }
62
63 string
64 MakeDCPJob::wav_path (libdcp::Channel c) const
65 {
66         return _opt->multichannel_audio_out_path (int (c), false);
67 }
68
69 void
70 MakeDCPJob::run ()
71 {
72         string const dcp_path = _fs->dir (_fs->name);
73
74         /* Remove any old DCP */
75         filesystem::remove_all (dcp_path);
76
77         int frames = 0;
78         switch (_fs->content_type ()) {
79         case VIDEO:
80                 frames = _fs->dcp_frames ? _fs->dcp_frames : _fs->length;
81                 break;
82         case STILL:
83                 frames = _fs->still_duration * ImageMagickDecoder::static_frames_per_second ();
84                 break;
85         }
86         
87         libdcp::DCP dcp (_fs->dir (_fs->name), _fs->name, _fs->dcp_content_type->libdcp_kind (), rint (_fs->frames_per_second), frames);
88         dcp.Progress.connect (sigc::mem_fun (*this, &MakeDCPJob::dcp_progress));
89
90         descend (0.9);
91         dcp.add_picture_asset (sigc::mem_fun (*this, &MakeDCPJob::j2c_path), _opt->out_size.width, _opt->out_size.height);
92         ascend ();
93
94         if (_fs->audio_channels > 0) {
95                 descend (0.1);
96                 dcp.add_sound_asset (sigc::mem_fun (*this, &MakeDCPJob::wav_path), _fs->audio_channels);
97                 ascend ();
98         }
99
100         dcp.write_xml ();
101
102         set_progress (1);
103         set_state (FINISHED_OK);
104 }
105
106 void
107 MakeDCPJob::dcp_progress (float p)
108 {
109         set_progress (p);
110 }