Move things round a bit.
[dcpomatic.git] / src / lib / film_state.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/film_state.cc
21  *  @brief The state of a Film.  This is separate from Film so that
22  *  state can easily be copied and kept around for reference
23  *  by long-running jobs.  This avoids the jobs getting confused
24  *  by the user changing Film settings during their run.
25  */
26
27 #include <fstream>
28 #include <string>
29 #include <iomanip>
30 #include <sstream>
31 #include <boost/filesystem.hpp>
32 #include "film_state.h"
33 #include "scaler.h"
34 #include "filter.h"
35 #include "format.h"
36 #include "dcp_content_type.h"
37 #include "util.h"
38
39 using namespace std;
40 using namespace boost;
41
42 /** Write state to a stream.
43  *  @param f Stream to write to.
44  */
45 void
46 FilmState::write_metadata (ofstream& f) const
47 {
48         /* User stuff */
49         f << "name " << name << "\n";
50         f << "content " << content << "\n";
51         if (dcp_content_type) {
52                 f << "dcp_content_type " << dcp_content_type->pretty_name () << "\n";
53         }
54         f << "frames_per_second " << frames_per_second << "\n";
55         if (format) {
56                 f << "format " << format->as_metadata () << "\n";
57         }
58         f << "left_crop " << left_crop << "\n";
59         f << "right_crop " << right_crop << "\n";
60         f << "top_crop " << top_crop << "\n";
61         f << "bottom_crop " << bottom_crop << "\n";
62         for (vector<Filter const *>::const_iterator i = filters.begin(); i != filters.end(); ++i) {
63                 f << "filter " << (*i)->id () << "\n";
64         }
65         f << "scaler " << scaler->id () << "\n";
66         f << "dcp_frames " << dcp_frames << "\n";
67
68         f << "dcp_trim_action ";
69         switch (dcp_trim_action) {
70         case CUT:
71                 f << "cut\n";
72                 break;
73         case BLACK_OUT:
74                 f << "black_out\n";
75                 break;
76         }
77         
78         f << "dcp_ab " << (dcp_ab ? "1" : "0") << "\n";
79         f << "audio_gain " << audio_gain << "\n";
80         f << "audio_delay " << audio_delay << "\n";
81         f << "still_duration " << still_duration << "\n";
82
83         /* Cached stuff; this is information about our content; we could
84            look it up each time, but that's slow.
85         */
86         for (vector<int>::const_iterator i = thumbs.begin(); i != thumbs.end(); ++i) {
87                 f << "thumb " << *i << "\n";
88         }
89         f << "width " << size.width << "\n";
90         f << "height " << size.height << "\n";
91         f << "length " << length << "\n";
92         f << "audio_channels " << audio_channels << "\n";
93         f << "audio_sample_rate " << audio_sample_rate << "\n";
94         f << "audio_sample_format " << audio_sample_format_to_string (audio_sample_format) << "\n";
95 }
96
97 /** Read state from a key / value pair.
98  *  @param k Key.
99  *  @param v Value.
100  */
101 void
102 FilmState::read_metadata (string k, string v)
103 {
104         /* User-specified stuff */
105         if (k == "name") {
106                 name = v;
107         } else if (k == "content") {
108                 content = v;
109         } else if (k == "dcp_content_type") {
110                 dcp_content_type = DCPContentType::from_pretty_name (v);
111         } else if (k == "frames_per_second") {
112                 frames_per_second = atof (v.c_str ());
113         } else if (k == "format") {
114                 format = Format::from_metadata (v);
115         } else if (k == "left_crop") {
116                 left_crop = atoi (v.c_str ());
117         } else if (k == "right_crop") {
118                 right_crop = atoi (v.c_str ());
119         } else if (k == "top_crop") {
120                 top_crop = atoi (v.c_str ());
121         } else if (k == "bottom_crop") {
122                 bottom_crop = atoi (v.c_str ());
123         } else if (k == "filter") {
124                 filters.push_back (Filter::from_id (v));
125         } else if (k == "scaler") {
126                 scaler = Scaler::from_id (v);
127         } else if (k == "dcp_frames") {
128                 dcp_frames = atoi (v.c_str ());
129         } else if (k == "dcp_trim_action") {
130                 if (v == "cut") {
131                         dcp_trim_action = CUT;
132                 } else if (v == "black_out") {
133                         dcp_trim_action = BLACK_OUT;
134                 }
135         } else if (k == "dcp_ab") {
136                 dcp_ab = (v == "1");
137         } else if (k == "audio_gain") {
138                 audio_gain = atof (v.c_str ());
139         } else if (k == "audio_delay") {
140                 audio_delay = atoi (v.c_str ());
141         } else if (k == "still_duration") {
142                 still_duration = atoi (v.c_str ());
143         }
144         
145         /* Cached stuff */
146         if (k == "thumb") {
147                 int const n = atoi (v.c_str ());
148                 /* Only add it to the list if it still exists */
149                 if (filesystem::exists (thumb_file_for_frame (n))) {
150                         thumbs.push_back (n);
151                 }
152         } else if (k == "width") {
153                 size.width = atoi (v.c_str ());
154         } else if (k == "height") {
155                 size.height = atoi (v.c_str ());
156         } else if (k == "length") {
157                 length = atof (v.c_str ());
158         } else if (k == "audio_channels") {
159                 audio_channels = atoi (v.c_str ());
160         } else if (k == "audio_sample_rate") {
161                 audio_sample_rate = atoi (v.c_str ());
162         } else if (k == "audio_sample_format") {
163                 audio_sample_format = audio_sample_format_from_string (v);
164         }
165 }
166
167
168 /** @param n A thumb index.
169  *  @return The path to the thumb's image file.
170  */
171 string
172 FilmState::thumb_file (int n) const
173 {
174         return thumb_file_for_frame (thumb_frame (n));
175 }
176
177 /** @param n A frame index within the Film.
178  *  @return The path to the thumb's image file for this frame;
179  *  we assume that it exists.
180  */
181 string
182 FilmState::thumb_file_for_frame (int n) const
183 {
184         stringstream s;
185         s << dir ("thumbs") << "/";
186         s.width (8);
187         s << setfill('0') << n << ".tiff";
188         return s.str ();
189 }
190
191
192 /** @param n A thumb index.
193  *  @return The frame within the Film that it is for.
194  */
195 int
196 FilmState::thumb_frame (int n) const
197 {
198         assert (n < int (thumbs.size ()));
199         return thumbs[n];
200 }
201
202 Size
203 FilmState::cropped_size (Size s) const
204 {
205         s.width -= left_crop + right_crop;
206         s.height -= top_crop + bottom_crop;
207         return s;
208 }
209
210 /** Given a directory name, return its full path within the Film's directory.
211  *  The directory (and its parents) will be created if they do not exist.
212  */
213 string
214 FilmState::dir (string d) const
215 {
216         stringstream s;
217         s << directory << "/" << d;
218         filesystem::create_directories (s.str ());
219         return s.str ();
220 }
221
222 /** Given a file or directory name, return its full path within the Film's directory */
223 string
224 FilmState::file (string f) const
225 {
226         stringstream s;
227         s << directory << "/" << f;
228         return s.str ();
229 }
230
231 string
232 FilmState::content_path () const
233 {
234         if (filesystem::path(content).has_root_directory ()) {
235                 return content;
236         }
237
238         return file (content);
239 }
240
241 ContentType
242 FilmState::content_type () const
243 {
244 #if BOOST_FILESYSTEM_VERSION == 3
245         string const ext = filesystem::path(content).extension().string();
246 #else
247         string const ext = filesystem::path(content).extension();
248 #endif
249         if (ext == ".tif" || ext == ".tiff" || ext == ".jpg" || ext == ".jpeg" || ext == ".png") {
250                 return STILL;
251         }
252
253         return VIDEO;
254 }