Use PNG for thumbs so that we get alpha blending in wxwidgets.
[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         return thumb_base_for_frame(n) + ".png";
195 }
196
197 string
198 FilmState::thumb_base (int n) const
199 {
200         return thumb_base_for_frame (thumb_frame (n));
201 }
202
203 string
204 FilmState::thumb_base_for_frame (int n) const
205 {
206         stringstream s;
207         s.width (8);
208         s << setfill('0') << n;
209         
210         filesystem::path p;
211         p /= dir ("thumbs");
212         p /= s.str ();
213                 
214         return p.string ();
215 }
216
217
218 /** @param n A thumb index.
219  *  @return The frame within the Film that it is for.
220  */
221 int
222 FilmState::thumb_frame (int n) const
223 {
224         assert (n < int (thumbs.size ()));
225         return thumbs[n];
226 }
227
228 Size
229 FilmState::cropped_size (Size s) const
230 {
231         s.width -= crop.left + crop.right;
232         s.height -= crop.top + crop.bottom;
233         return s;
234 }
235
236 /** Given a directory name, return its full path within the Film's directory.
237  *  The directory (and its parents) will be created if they do not exist.
238  */
239 string
240 FilmState::dir (string d) const
241 {
242         filesystem::path p;
243         p /= directory;
244         p /= d;
245         filesystem::create_directories (p);
246         return p.string ();
247 }
248
249 /** Given a file or directory name, return its full path within the Film's directory */
250 string
251 FilmState::file (string f) const
252 {
253         filesystem::path p;
254         p /= directory;
255         p /= f;
256         return p.string ();
257 }
258
259 string
260 FilmState::content_path () const
261 {
262         if (filesystem::path(content).has_root_directory ()) {
263                 return content;
264         }
265
266         return file (content);
267 }
268
269 ContentType
270 FilmState::content_type () const
271 {
272 #if BOOST_FILESYSTEM_VERSION == 3
273         string ext = filesystem::path(content).extension().string();
274 #else
275         string ext = filesystem::path(content).extension();
276 #endif
277
278         transform (ext.begin(), ext.end(), ext.begin(), ::tolower);
279         
280         if (ext == ".tif" || ext == ".tiff" || ext == ".jpg" || ext == ".jpeg" || ext == ".png") {
281                 return STILL;
282         }
283
284         return VIDEO;
285 }
286
287 /** @return Number of bytes per sample of a single channel */
288 int
289 FilmState::bytes_per_sample () const
290 {
291         switch (audio_sample_format) {
292         case AV_SAMPLE_FMT_S16:
293                 return 2;
294         default:
295                 return 0;
296         }
297
298         return 0;
299 }
300
301 int
302 FilmState::target_sample_rate () const
303 {
304         /* Resample to a DCI-approved sample rate */
305         double t = dcp_audio_sample_rate (audio_sample_rate);
306
307         /* Compensate for the fact that video will be rounded to the
308            nearest integer number of frames per second.
309         */
310         if (rint (frames_per_second) != frames_per_second) {
311                 t *= frames_per_second / rint (frames_per_second);
312         }
313
314         return rint (t);
315 }
316
317 int
318 FilmState::dcp_length () const
319 {
320         if (dcp_frames) {
321                 return dcp_frames;
322         }
323
324         return length;
325 }
326
327