Clumsy DCI naming dialog.
[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 <boost/date_time.hpp>
33 #include "film_state.h"
34 #include "scaler.h"
35 #include "filter.h"
36 #include "format.h"
37 #include "dcp_content_type.h"
38 #include "util.h"
39 #include "exceptions.h"
40
41 using namespace std;
42 using namespace boost;
43
44 /** Write state to a stream.
45  *  @param f Stream to write to.
46  */
47 void
48 FilmState::write_metadata (ofstream& f) const
49 {
50         /* User stuff */
51         f << "name " << name << "\n";
52         f << "use_dci_name " << use_dci_name << "\n";
53         f << "content " << content << "\n";
54         if (dcp_content_type) {
55                 f << "dcp_content_type " << dcp_content_type->pretty_name () << "\n";
56         }
57         f << "frames_per_second " << frames_per_second << "\n";
58         if (format) {
59                 f << "format " << format->as_metadata () << "\n";
60         }
61         f << "left_crop " << crop.left << "\n";
62         f << "right_crop " << crop.right << "\n";
63         f << "top_crop " << crop.top << "\n";
64         f << "bottom_crop " << crop.bottom << "\n";
65         for (vector<Filter const *>::const_iterator i = filters.begin(); i != filters.end(); ++i) {
66                 f << "filter " << (*i)->id () << "\n";
67         }
68         f << "scaler " << scaler->id () << "\n";
69         f << "dcp_frames " << dcp_frames << "\n";
70
71         f << "dcp_trim_action ";
72         switch (dcp_trim_action) {
73         case CUT:
74                 f << "cut\n";
75                 break;
76         case BLACK_OUT:
77                 f << "black_out\n";
78                 break;
79         }
80         
81         f << "dcp_ab " << (dcp_ab ? "1" : "0") << "\n";
82         f << "audio_gain " << audio_gain << "\n";
83         f << "audio_delay " << audio_delay << "\n";
84         f << "still_duration " << still_duration << "\n";
85         f << "with_subtitles " << with_subtitles << "\n";
86         f << "subtitle_offset " << subtitle_offset << "\n";
87         f << "subtitle_scale " << subtitle_scale << "\n";
88
89         /* Cached stuff; this is information about our content; we could
90            look it up each time, but that's slow.
91         */
92         for (vector<int>::const_iterator i = thumbs.begin(); i != thumbs.end(); ++i) {
93                 f << "thumb " << *i << "\n";
94         }
95         f << "width " << size.width << "\n";
96         f << "height " << size.height << "\n";
97         f << "length " << length << "\n";
98         f << "audio_channels " << audio_channels << "\n";
99         f << "audio_sample_rate " << audio_sample_rate << "\n";
100         f << "audio_sample_format " << audio_sample_format_to_string (audio_sample_format) << "\n";
101         f << "content_digest " << content_digest << "\n";
102         f << "has_subtitles " << has_subtitles << "\n";
103         f << "dci_name_prefix " << dci_name_prefix << "\n";
104         f << "audio_language " << audio_language << "\n";
105         f << "subtitle_language " << subtitle_language << "\n";
106         f << "territory " << territory << "\n";
107         f << "rating " << rating << "\n";
108         f << "studio " << studio << "\n";
109         f << "facility " << facility << "\n";
110         f << "package_type " << package_type << "\n";
111 }
112
113 /** Read state from a key / value pair.
114  *  @param k Key.
115  *  @param v Value.
116  */
117 void
118 FilmState::read_metadata (string k, string v)
119 {
120         /* User-specified stuff */
121         if (k == "name") {
122                 name = v;
123         } else if (k == "use_dci_name") {
124                 use_dci_name = (v == "1");
125         } else if (k == "content") {
126                 content = v;
127         } else if (k == "dcp_content_type") {
128                 dcp_content_type = DCPContentType::from_pretty_name (v);
129         } else if (k == "frames_per_second") {
130                 frames_per_second = atof (v.c_str ());
131         } else if (k == "format") {
132                 format = Format::from_metadata (v);
133         } else if (k == "left_crop") {
134                 crop.left = atoi (v.c_str ());
135         } else if (k == "right_crop") {
136                 crop.right = atoi (v.c_str ());
137         } else if (k == "top_crop") {
138                 crop.top = atoi (v.c_str ());
139         } else if (k == "bottom_crop") {
140                 crop.bottom = atoi (v.c_str ());
141         } else if (k == "filter") {
142                 filters.push_back (Filter::from_id (v));
143         } else if (k == "scaler") {
144                 scaler = Scaler::from_id (v);
145         } else if (k == "dcp_frames") {
146                 dcp_frames = atoi (v.c_str ());
147         } else if (k == "dcp_trim_action") {
148                 if (v == "cut") {
149                         dcp_trim_action = CUT;
150                 } else if (v == "black_out") {
151                         dcp_trim_action = BLACK_OUT;
152                 }
153         } else if (k == "dcp_ab") {
154                 dcp_ab = (v == "1");
155         } else if (k == "audio_gain") {
156                 audio_gain = atof (v.c_str ());
157         } else if (k == "audio_delay") {
158                 audio_delay = atoi (v.c_str ());
159         } else if (k == "still_duration") {
160                 still_duration = atoi (v.c_str ());
161         } else if (k == "with_subtitles") {
162                 with_subtitles = (v == "1");
163         } else if (k == "subtitle_offset") {
164                 subtitle_offset = atoi (v.c_str ());
165         } else if (k == "subtitle_scale") {
166                 subtitle_scale = atof (v.c_str ());
167         }
168         
169         /* Cached stuff */
170         if (k == "thumb") {
171                 int const n = atoi (v.c_str ());
172                 /* Only add it to the list if it still exists */
173                 if (filesystem::exists (thumb_file_for_frame (n))) {
174                         thumbs.push_back (n);
175                 }
176         } else if (k == "width") {
177                 size.width = atoi (v.c_str ());
178         } else if (k == "height") {
179                 size.height = atoi (v.c_str ());
180         } else if (k == "length") {
181                 length = atof (v.c_str ());
182         } else if (k == "audio_channels") {
183                 audio_channels = atoi (v.c_str ());
184         } else if (k == "audio_sample_rate") {
185                 audio_sample_rate = atoi (v.c_str ());
186         } else if (k == "audio_sample_format") {
187                 audio_sample_format = audio_sample_format_from_string (v);
188         } else if (k == "content_digest") {
189                 content_digest = v;
190         } else if (k == "has_subtitles") {
191                 has_subtitles = (v == "1");
192         } else if (k == "dci_name_prefix") {
193                 dci_name_prefix = v;
194         } else if (k == "audio_language") {
195                 audio_language = v;
196         } else if (k == "subtitle_language") {
197                 subtitle_language = v;
198         } else if (k == "territory") {
199                 territory = v;
200         } else if (k == "rating") {
201                 rating = v;
202         } else if (k == "studio") {
203                 studio = v;
204         } else if (k == "facility") {
205                 facility = v;
206         } else if (k == "package_type") {
207                 package_type = v;
208         }
209 }
210
211
212 /** @param n A thumb index.
213  *  @return The path to the thumb's image file.
214  */
215 string
216 FilmState::thumb_file (int n) const
217 {
218         return thumb_file_for_frame (thumb_frame (n));
219 }
220
221 /** @param n A frame index within the Film.
222  *  @return The path to the thumb's image file for this frame;
223  *  we assume that it exists.
224  */
225 string
226 FilmState::thumb_file_for_frame (int n) const
227 {
228         return thumb_base_for_frame(n) + ".png";
229 }
230
231 string
232 FilmState::thumb_base (int n) const
233 {
234         return thumb_base_for_frame (thumb_frame (n));
235 }
236
237 string
238 FilmState::thumb_base_for_frame (int n) const
239 {
240         stringstream s;
241         s.width (8);
242         s << setfill('0') << n;
243         
244         filesystem::path p;
245         p /= dir ("thumbs");
246         p /= s.str ();
247                 
248         return p.string ();
249 }
250
251
252 /** @param n A thumb index.
253  *  @return The frame within the Film that it is for.
254  */
255 int
256 FilmState::thumb_frame (int n) const
257 {
258         assert (n < int (thumbs.size ()));
259         return thumbs[n];
260 }
261
262 Size
263 FilmState::cropped_size (Size s) const
264 {
265         s.width -= crop.left + crop.right;
266         s.height -= crop.top + crop.bottom;
267         return s;
268 }
269
270 /** Given a directory name, return its full path within the Film's directory.
271  *  The directory (and its parents) will be created if they do not exist.
272  */
273 string
274 FilmState::dir (string d) const
275 {
276         filesystem::path p;
277         p /= directory;
278         p /= d;
279         filesystem::create_directories (p);
280         return p.string ();
281 }
282
283 /** Given a file or directory name, return its full path within the Film's directory */
284 string
285 FilmState::file (string f) const
286 {
287         filesystem::path p;
288         p /= directory;
289         p /= f;
290         return p.string ();
291 }
292
293 string
294 FilmState::content_path () const
295 {
296         if (filesystem::path(content).has_root_directory ()) {
297                 return content;
298         }
299
300         return file (content);
301 }
302
303 ContentType
304 FilmState::content_type () const
305 {
306 #if BOOST_FILESYSTEM_VERSION == 3
307         string ext = filesystem::path(content).extension().string();
308 #else
309         string ext = filesystem::path(content).extension();
310 #endif
311
312         transform (ext.begin(), ext.end(), ext.begin(), ::tolower);
313         
314         if (ext == ".tif" || ext == ".tiff" || ext == ".jpg" || ext == ".jpeg" || ext == ".png") {
315                 return STILL;
316         }
317
318         return VIDEO;
319 }
320
321 /** @return Number of bytes per sample of a single channel */
322 int
323 FilmState::bytes_per_sample () const
324 {
325         switch (audio_sample_format) {
326         case AV_SAMPLE_FMT_S16:
327                 return 2;
328         default:
329                 return 0;
330         }
331
332         return 0;
333 }
334
335 int
336 FilmState::target_sample_rate () const
337 {
338         /* Resample to a DCI-approved sample rate */
339         double t = dcp_audio_sample_rate (audio_sample_rate);
340
341         /* Compensate for the fact that video will be rounded to the
342            nearest integer number of frames per second.
343         */
344         if (rint (frames_per_second) != frames_per_second) {
345                 t *= frames_per_second / rint (frames_per_second);
346         }
347
348         return rint (t);
349 }
350
351 int
352 FilmState::dcp_length () const
353 {
354         if (dcp_frames) {
355                 return dcp_frames;
356         }
357
358         return length;
359 }
360
361 string
362 FilmState::dci_name () const
363 {
364         stringstream d;
365         d << dci_name_prefix << "_";
366
367         if (dcp_content_type) {
368                 d << dcp_content_type->dci_name() << "_";
369         }
370
371         if (format) {
372                 d << format->dci_name() << "_";
373         }
374
375         if (!audio_language.empty ()) {
376                 d << audio_language;
377                 if (!subtitle_language.empty ()) {
378                         d << "-" << subtitle_language;
379                 }
380                 d << "_";
381         }
382
383         if (!territory.empty ()) {
384                 d << territory;
385                 if (!rating.empty ()) {
386                         d << "-" << rating;
387                 }
388                 d << "_";
389         }
390
391         switch (audio_channels) {
392         case 1:
393                 d << "1_";
394                 break;
395         case 2:
396                 d << "2_";
397                 break;
398         case 6:
399                 d << "51_";
400                 break;
401         }
402
403         d << "2K_";
404
405         if (!studio.empty ()) {
406                 d << studio << "_";
407         }
408
409         gregorian::date today = gregorian::day_clock::local_day ();
410         d << gregorian::to_iso_extended_string (today) << "_";
411
412         if (!facility.empty ()) {
413                 d << facility << "_";
414         }
415
416         if (!package_type.empty ()) {
417                 d << package_type;
418         }
419
420         return d.str ();
421 }