Make subtitle addition optional.
[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 #include "exceptions.h"
39
40 using namespace std;
41 using namespace boost;
42
43 /** Write state to a stream.
44  *  @param f Stream to write to.
45  */
46 void
47 FilmState::write_metadata (ofstream& f) const
48 {
49         /* User stuff */
50         f << "name " << name << "\n";
51         f << "content " << content << "\n";
52         if (dcp_content_type) {
53                 f << "dcp_content_type " << dcp_content_type->pretty_name () << "\n";
54         }
55         f << "frames_per_second " << frames_per_second << "\n";
56         if (format) {
57                 f << "format " << format->as_metadata () << "\n";
58         }
59         f << "left_crop " << crop.left << "\n";
60         f << "right_crop " << crop.right << "\n";
61         f << "top_crop " << crop.top << "\n";
62         f << "bottom_crop " << crop.bottom << "\n";
63         for (vector<Filter const *>::const_iterator i = filters.begin(); i != filters.end(); ++i) {
64                 f << "filter " << (*i)->id () << "\n";
65         }
66         f << "scaler " << scaler->id () << "\n";
67         f << "dcp_frames " << dcp_frames << "\n";
68
69         f << "dcp_trim_action ";
70         switch (dcp_trim_action) {
71         case CUT:
72                 f << "cut\n";
73                 break;
74         case BLACK_OUT:
75                 f << "black_out\n";
76                 break;
77         }
78         
79         f << "dcp_ab " << (dcp_ab ? "1" : "0") << "\n";
80         f << "audio_gain " << audio_gain << "\n";
81         f << "audio_delay " << audio_delay << "\n";
82         f << "still_duration " << still_duration << "\n";
83         f << "with_subtitles " << with_subtitles << "\n";
84
85         /* Cached stuff; this is information about our content; we could
86            look it up each time, but that's slow.
87         */
88         for (vector<int>::const_iterator i = thumbs.begin(); i != thumbs.end(); ++i) {
89                 f << "thumb " << *i << "\n";
90         }
91         f << "width " << size.width << "\n";
92         f << "height " << size.height << "\n";
93         f << "length " << length << "\n";
94         f << "audio_channels " << audio_channels << "\n";
95         f << "audio_sample_rate " << audio_sample_rate << "\n";
96         f << "audio_sample_format " << audio_sample_format_to_string (audio_sample_format) << "\n";
97         f << "content_digest " << content_digest << "\n";
98         f << "has_subtitles " << has_subtitles << "\n";
99 }
100
101 /** Read state from a key / value pair.
102  *  @param k Key.
103  *  @param v Value.
104  */
105 void
106 FilmState::read_metadata (string k, string v)
107 {
108         /* User-specified stuff */
109         if (k == "name") {
110                 name = v;
111         } else if (k == "content") {
112                 content = v;
113         } else if (k == "dcp_content_type") {
114                 dcp_content_type = DCPContentType::from_pretty_name (v);
115         } else if (k == "frames_per_second") {
116                 frames_per_second = atof (v.c_str ());
117         } else if (k == "format") {
118                 format = Format::from_metadata (v);
119         } else if (k == "left_crop") {
120                 crop.left = atoi (v.c_str ());
121         } else if (k == "right_crop") {
122                 crop.right = atoi (v.c_str ());
123         } else if (k == "top_crop") {
124                 crop.top = atoi (v.c_str ());
125         } else if (k == "bottom_crop") {
126                 crop.bottom = atoi (v.c_str ());
127         } else if (k == "filter") {
128                 filters.push_back (Filter::from_id (v));
129         } else if (k == "scaler") {
130                 scaler = Scaler::from_id (v);
131         } else if (k == "dcp_frames") {
132                 dcp_frames = atoi (v.c_str ());
133         } else if (k == "dcp_trim_action") {
134                 if (v == "cut") {
135                         dcp_trim_action = CUT;
136                 } else if (v == "black_out") {
137                         dcp_trim_action = BLACK_OUT;
138                 }
139         } else if (k == "dcp_ab") {
140                 dcp_ab = (v == "1");
141         } else if (k == "audio_gain") {
142                 audio_gain = atof (v.c_str ());
143         } else if (k == "audio_delay") {
144                 audio_delay = atoi (v.c_str ());
145         } else if (k == "still_duration") {
146                 still_duration = atoi (v.c_str ());
147         } else if (k == "with_subtitles") {
148                 with_subtitles = (v == "1");
149         }
150         
151         /* Cached stuff */
152         if (k == "thumb") {
153                 int const n = atoi (v.c_str ());
154                 /* Only add it to the list if it still exists */
155                 if (filesystem::exists (thumb_file_for_frame (n))) {
156                         thumbs.push_back (n);
157                 }
158         } else if (k == "width") {
159                 size.width = atoi (v.c_str ());
160         } else if (k == "height") {
161                 size.height = atoi (v.c_str ());
162         } else if (k == "length") {
163                 length = atof (v.c_str ());
164         } else if (k == "audio_channels") {
165                 audio_channels = atoi (v.c_str ());
166         } else if (k == "audio_sample_rate") {
167                 audio_sample_rate = atoi (v.c_str ());
168         } else if (k == "audio_sample_format") {
169                 audio_sample_format = audio_sample_format_from_string (v);
170         } else if (k == "content_digest") {
171                 content_digest = v;
172         } else if (k == "has_subtitles") {
173                 has_subtitles = (v == "1");
174         }
175 }
176
177
178 /** @param n A thumb index.
179  *  @return The path to the thumb's image file.
180  */
181 string
182 FilmState::thumb_file (int n) const
183 {
184         return thumb_file_for_frame (thumb_frame (n));
185 }
186
187 /** @param n A frame index within the Film.
188  *  @return The path to the thumb's image file for this frame;
189  *  we assume that it exists.
190  */
191 string
192 FilmState::thumb_file_for_frame (int n) const
193 {
194         stringstream s;
195         s.width (8);
196         s << setfill('0') << n << ".tiff";
197         
198         filesystem::path p;
199         p /= dir ("thumbs");
200         p /= s.str ();
201                 
202         return p.string ();
203 }
204
205
206 /** @param n A thumb index.
207  *  @return The frame within the Film that it is for.
208  */
209 int
210 FilmState::thumb_frame (int n) const
211 {
212         assert (n < int (thumbs.size ()));
213         return thumbs[n];
214 }
215
216 Size
217 FilmState::cropped_size (Size s) const
218 {
219         s.width -= crop.left + crop.right;
220         s.height -= crop.top + crop.bottom;
221         return s;
222 }
223
224 /** Given a directory name, return its full path within the Film's directory.
225  *  The directory (and its parents) will be created if they do not exist.
226  */
227 string
228 FilmState::dir (string d) const
229 {
230         filesystem::path p;
231         p /= directory;
232         p /= d;
233         filesystem::create_directories (p);
234         return p.string ();
235 }
236
237 /** Given a file or directory name, return its full path within the Film's directory */
238 string
239 FilmState::file (string f) const
240 {
241         filesystem::path p;
242         p /= directory;
243         p /= f;
244         return p.string ();
245 }
246
247 string
248 FilmState::content_path () const
249 {
250         if (filesystem::path(content).has_root_directory ()) {
251                 return content;
252         }
253
254         return file (content);
255 }
256
257 ContentType
258 FilmState::content_type () const
259 {
260 #if BOOST_FILESYSTEM_VERSION == 3
261         string ext = filesystem::path(content).extension().string();
262 #else
263         string ext = filesystem::path(content).extension();
264 #endif
265
266         transform (ext.begin(), ext.end(), ext.begin(), ::tolower);
267         
268         if (ext == ".tif" || ext == ".tiff" || ext == ".jpg" || ext == ".jpeg" || ext == ".png") {
269                 return STILL;
270         }
271
272         return VIDEO;
273 }
274
275 /** @return Number of bytes per sample of a single channel */
276 int
277 FilmState::bytes_per_sample () const
278 {
279         switch (audio_sample_format) {
280         case AV_SAMPLE_FMT_S16:
281                 return 2;
282         default:
283                 return 0;
284         }
285
286         return 0;
287 }
288
289 int
290 FilmState::target_sample_rate () const
291 {
292         double t = dcp_audio_sample_rate (audio_sample_rate);
293         if (rint (frames_per_second) != frames_per_second) {
294                 if (fabs (frames_per_second - 23.976) < 1e-6 || (fabs (frames_per_second - 29.97) < 1e-6)) {
295                         /* 24fps or 30fps drop-frame ie {24,30} * 1000 / 1001 frames per second;
296                            hence we need to resample the audio to dcp_audio_sample_rate * 1000 / 1001
297                            so that when we play it back at dcp_audio_sample_rate it is sped up
298                            by the same amount that the video is
299                         */
300                         t *= double(1000) / 1001;
301                 } else {
302                         throw EncodeError ("unknown fractional frame rate");
303                 }
304         }
305
306         return rint (t);
307 }
308
309 int
310 FilmState::dcp_length () const
311 {
312         if (dcp_frames) {
313                 return dcp_frames;
314         }
315
316         return length;
317 }
318
319