Fix date format in DCP names.
[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 << "audio_language " << audio_language << "\n";
104         f << "subtitle_language " << subtitle_language << "\n";
105         f << "territory " << territory << "\n";
106         f << "rating " << rating << "\n";
107         f << "studio " << studio << "\n";
108         f << "facility " << facility << "\n";
109         f << "package_type " << package_type << "\n";
110 }
111
112 /** Read state from a key / value pair.
113  *  @param k Key.
114  *  @param v Value.
115  */
116 void
117 FilmState::read_metadata (string k, string v)
118 {
119         /* User-specified stuff */
120         if (k == "name") {
121                 name = v;
122         } else if (k == "use_dci_name") {
123                 use_dci_name = (v == "1");
124         } else if (k == "content") {
125                 content = v;
126         } else if (k == "dcp_content_type") {
127                 dcp_content_type = DCPContentType::from_pretty_name (v);
128         } else if (k == "frames_per_second") {
129                 frames_per_second = atof (v.c_str ());
130         } else if (k == "format") {
131                 format = Format::from_metadata (v);
132         } else if (k == "left_crop") {
133                 crop.left = atoi (v.c_str ());
134         } else if (k == "right_crop") {
135                 crop.right = atoi (v.c_str ());
136         } else if (k == "top_crop") {
137                 crop.top = atoi (v.c_str ());
138         } else if (k == "bottom_crop") {
139                 crop.bottom = atoi (v.c_str ());
140         } else if (k == "filter") {
141                 filters.push_back (Filter::from_id (v));
142         } else if (k == "scaler") {
143                 scaler = Scaler::from_id (v);
144         } else if (k == "dcp_frames") {
145                 dcp_frames = atoi (v.c_str ());
146         } else if (k == "dcp_trim_action") {
147                 if (v == "cut") {
148                         dcp_trim_action = CUT;
149                 } else if (v == "black_out") {
150                         dcp_trim_action = BLACK_OUT;
151                 }
152         } else if (k == "dcp_ab") {
153                 dcp_ab = (v == "1");
154         } else if (k == "audio_gain") {
155                 audio_gain = atof (v.c_str ());
156         } else if (k == "audio_delay") {
157                 audio_delay = atoi (v.c_str ());
158         } else if (k == "still_duration") {
159                 still_duration = atoi (v.c_str ());
160         } else if (k == "with_subtitles") {
161                 with_subtitles = (v == "1");
162         } else if (k == "subtitle_offset") {
163                 subtitle_offset = atoi (v.c_str ());
164         } else if (k == "subtitle_scale") {
165                 subtitle_scale = atof (v.c_str ());
166         }
167         
168         /* Cached stuff */
169         if (k == "thumb") {
170                 int const n = atoi (v.c_str ());
171                 /* Only add it to the list if it still exists */
172                 if (filesystem::exists (thumb_file_for_frame (n))) {
173                         thumbs.push_back (n);
174                 }
175         } else if (k == "width") {
176                 size.width = atoi (v.c_str ());
177         } else if (k == "height") {
178                 size.height = atoi (v.c_str ());
179         } else if (k == "length") {
180                 length = atof (v.c_str ());
181         } else if (k == "audio_channels") {
182                 audio_channels = atoi (v.c_str ());
183         } else if (k == "audio_sample_rate") {
184                 audio_sample_rate = atoi (v.c_str ());
185         } else if (k == "audio_sample_format") {
186                 audio_sample_format = audio_sample_format_from_string (v);
187         } else if (k == "content_digest") {
188                 content_digest = v;
189         } else if (k == "has_subtitles") {
190                 has_subtitles = (v == "1");
191         } else if (k == "audio_language") {
192                 audio_language = v;
193         } else if (k == "subtitle_language") {
194                 subtitle_language = v;
195         } else if (k == "territory") {
196                 territory = v;
197         } else if (k == "rating") {
198                 rating = v;
199         } else if (k == "studio") {
200                 studio = v;
201         } else if (k == "facility") {
202                 facility = v;
203         } else if (k == "package_type") {
204                 package_type = v;
205         }
206 }
207
208
209 /** @param n A thumb index.
210  *  @return The path to the thumb's image file.
211  */
212 string
213 FilmState::thumb_file (int n) const
214 {
215         return thumb_file_for_frame (thumb_frame (n));
216 }
217
218 /** @param n A frame index within the Film.
219  *  @return The path to the thumb's image file for this frame;
220  *  we assume that it exists.
221  */
222 string
223 FilmState::thumb_file_for_frame (int n) const
224 {
225         return thumb_base_for_frame(n) + ".png";
226 }
227
228 string
229 FilmState::thumb_base (int n) const
230 {
231         return thumb_base_for_frame (thumb_frame (n));
232 }
233
234 string
235 FilmState::thumb_base_for_frame (int n) const
236 {
237         stringstream s;
238         s.width (8);
239         s << setfill('0') << n;
240         
241         filesystem::path p;
242         p /= dir ("thumbs");
243         p /= s.str ();
244                 
245         return p.string ();
246 }
247
248
249 /** @param n A thumb index.
250  *  @return The frame within the Film that it is for.
251  */
252 int
253 FilmState::thumb_frame (int n) const
254 {
255         assert (n < int (thumbs.size ()));
256         return thumbs[n];
257 }
258
259 Size
260 FilmState::cropped_size (Size s) const
261 {
262         s.width -= crop.left + crop.right;
263         s.height -= crop.top + crop.bottom;
264         return s;
265 }
266
267 /** Given a directory name, return its full path within the Film's directory.
268  *  The directory (and its parents) will be created if they do not exist.
269  */
270 string
271 FilmState::dir (string d) const
272 {
273         filesystem::path p;
274         p /= directory;
275         p /= d;
276         filesystem::create_directories (p);
277         return p.string ();
278 }
279
280 /** Given a file or directory name, return its full path within the Film's directory */
281 string
282 FilmState::file (string f) const
283 {
284         filesystem::path p;
285         p /= directory;
286         p /= f;
287         return p.string ();
288 }
289
290 string
291 FilmState::content_path () const
292 {
293         if (filesystem::path(content).has_root_directory ()) {
294                 return content;
295         }
296
297         return file (content);
298 }
299
300 ContentType
301 FilmState::content_type () const
302 {
303 #if BOOST_FILESYSTEM_VERSION == 3
304         string ext = filesystem::path(content).extension().string();
305 #else
306         string ext = filesystem::path(content).extension();
307 #endif
308
309         transform (ext.begin(), ext.end(), ext.begin(), ::tolower);
310         
311         if (ext == ".tif" || ext == ".tiff" || ext == ".jpg" || ext == ".jpeg" || ext == ".png") {
312                 return STILL;
313         }
314
315         return VIDEO;
316 }
317
318 /** @return Number of bytes per sample of a single channel */
319 int
320 FilmState::bytes_per_sample () const
321 {
322         switch (audio_sample_format) {
323         case AV_SAMPLE_FMT_S16:
324                 return 2;
325         default:
326                 return 0;
327         }
328
329         return 0;
330 }
331
332 int
333 FilmState::target_sample_rate () const
334 {
335         /* Resample to a DCI-approved sample rate */
336         double t = dcp_audio_sample_rate (audio_sample_rate);
337
338         /* Compensate for the fact that video will be rounded to the
339            nearest integer number of frames per second.
340         */
341         if (rint (frames_per_second) != frames_per_second) {
342                 t *= frames_per_second / rint (frames_per_second);
343         }
344
345         return rint (t);
346 }
347
348 int
349 FilmState::dcp_length () const
350 {
351         if (dcp_frames) {
352                 return dcp_frames;
353         }
354
355         return length;
356 }
357
358 string
359 FilmState::dci_name () const
360 {
361         stringstream d;
362
363         string fixed_name = to_upper_copy (name);
364         for (size_t i = 0; i < fixed_name.length(); ++i) {
365                 if (fixed_name[i] == ' ') {
366                         fixed_name[i] = '-';
367                 }
368         }
369
370         d << fixed_name << "_";
371
372         if (dcp_content_type) {
373                 d << dcp_content_type->dci_name() << "_";
374         }
375
376         if (format) {
377                 d << format->dci_name() << "_";
378         }
379
380         if (!audio_language.empty ()) {
381                 d << audio_language;
382                 if (with_subtitles) {
383                         if (!subtitle_language.empty ()) {
384                                 d << "-" << subtitle_language;
385                         } else {
386                                 d << "-XX";
387                         }
388                 }
389                         
390                 d << "_";
391         }
392
393         if (!territory.empty ()) {
394                 d << territory;
395                 if (!rating.empty ()) {
396                         d << "-" << rating;
397                 }
398                 d << "_";
399         }
400
401         switch (audio_channels) {
402         case 1:
403                 d << "10_";
404                 break;
405         case 2:
406                 d << "20_";
407                 break;
408         case 6:
409                 d << "51_";
410                 break;
411         }
412
413         d << "2K_";
414
415         if (!studio.empty ()) {
416                 d << studio << "_";
417         }
418
419         gregorian::date today = gregorian::day_clock::local_day ();
420         d << gregorian::to_iso_string (today) << "_";
421
422         if (!facility.empty ()) {
423                 d << facility << "_";
424         }
425
426         if (!package_type.empty ()) {
427                 d << package_type;
428         }
429
430         return d.str ();
431 }
432
433 /** @return name to give the DCP */
434 string
435 FilmState::dcp_name () const
436 {
437         if (use_dci_name) {
438                 return dci_name ();
439         }
440
441         return name;
442 }
443
444