Simplify and test audio sample rate alteration.
[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
84         /* Cached stuff; this is information about our content; we could
85            look it up each time, but that's slow.
86         */
87         for (vector<int>::const_iterator i = thumbs.begin(); i != thumbs.end(); ++i) {
88                 f << "thumb " << *i << "\n";
89         }
90         f << "width " << size.width << "\n";
91         f << "height " << size.height << "\n";
92         f << "length " << length << "\n";
93         f << "audio_channels " << audio_channels << "\n";
94         f << "audio_sample_rate " << audio_sample_rate << "\n";
95         f << "audio_sample_format " << audio_sample_format_to_string (audio_sample_format) << "\n";
96         f << "content_digest " << content_digest << "\n";
97 }
98
99 /** Read state from a key / value pair.
100  *  @param k Key.
101  *  @param v Value.
102  */
103 void
104 FilmState::read_metadata (string k, string v)
105 {
106         /* User-specified stuff */
107         if (k == "name") {
108                 name = v;
109         } else if (k == "content") {
110                 content = v;
111         } else if (k == "dcp_content_type") {
112                 dcp_content_type = DCPContentType::from_pretty_name (v);
113         } else if (k == "frames_per_second") {
114                 frames_per_second = atof (v.c_str ());
115         } else if (k == "format") {
116                 format = Format::from_metadata (v);
117         } else if (k == "left_crop") {
118                 crop.left = atoi (v.c_str ());
119         } else if (k == "right_crop") {
120                 crop.right = atoi (v.c_str ());
121         } else if (k == "top_crop") {
122                 crop.top = atoi (v.c_str ());
123         } else if (k == "bottom_crop") {
124                 crop.bottom = atoi (v.c_str ());
125         } else if (k == "filter") {
126                 filters.push_back (Filter::from_id (v));
127         } else if (k == "scaler") {
128                 scaler = Scaler::from_id (v);
129         } else if (k == "dcp_frames") {
130                 dcp_frames = atoi (v.c_str ());
131         } else if (k == "dcp_trim_action") {
132                 if (v == "cut") {
133                         dcp_trim_action = CUT;
134                 } else if (v == "black_out") {
135                         dcp_trim_action = BLACK_OUT;
136                 }
137         } else if (k == "dcp_ab") {
138                 dcp_ab = (v == "1");
139         } else if (k == "audio_gain") {
140                 audio_gain = atof (v.c_str ());
141         } else if (k == "audio_delay") {
142                 audio_delay = atoi (v.c_str ());
143         } else if (k == "still_duration") {
144                 still_duration = atoi (v.c_str ());
145         }
146         
147         /* Cached stuff */
148         if (k == "thumb") {
149                 int const n = atoi (v.c_str ());
150                 /* Only add it to the list if it still exists */
151                 if (filesystem::exists (thumb_file_for_frame (n))) {
152                         thumbs.push_back (n);
153                 }
154         } else if (k == "width") {
155                 size.width = atoi (v.c_str ());
156         } else if (k == "height") {
157                 size.height = atoi (v.c_str ());
158         } else if (k == "length") {
159                 length = atof (v.c_str ());
160         } else if (k == "audio_channels") {
161                 audio_channels = atoi (v.c_str ());
162         } else if (k == "audio_sample_rate") {
163                 audio_sample_rate = atoi (v.c_str ());
164         } else if (k == "audio_sample_format") {
165                 audio_sample_format = audio_sample_format_from_string (v);
166         } else if (k == "content_digest") {
167                 content_digest = v;
168         }
169 }
170
171
172 /** @param n A thumb index.
173  *  @return The path to the thumb's image file.
174  */
175 string
176 FilmState::thumb_file (int n) const
177 {
178         return thumb_file_for_frame (thumb_frame (n));
179 }
180
181 /** @param n A frame index within the Film.
182  *  @return The path to the thumb's image file for this frame;
183  *  we assume that it exists.
184  */
185 string
186 FilmState::thumb_file_for_frame (int n) const
187 {
188         stringstream s;
189         s.width (8);
190         s << setfill('0') << n << ".tiff";
191         
192         filesystem::path p;
193         p /= dir ("thumbs");
194         p /= s.str ();
195                 
196         return p.string ();
197 }
198
199
200 /** @param n A thumb index.
201  *  @return The frame within the Film that it is for.
202  */
203 int
204 FilmState::thumb_frame (int n) const
205 {
206         assert (n < int (thumbs.size ()));
207         return thumbs[n];
208 }
209
210 Size
211 FilmState::cropped_size (Size s) const
212 {
213         s.width -= crop.left + crop.right;
214         s.height -= crop.top + crop.bottom;
215         return s;
216 }
217
218 /** Given a directory name, return its full path within the Film's directory.
219  *  The directory (and its parents) will be created if they do not exist.
220  */
221 string
222 FilmState::dir (string d) const
223 {
224         filesystem::path p;
225         p /= directory;
226         p /= d;
227         filesystem::create_directories (p);
228         return p.string ();
229 }
230
231 /** Given a file or directory name, return its full path within the Film's directory */
232 string
233 FilmState::file (string f) const
234 {
235         filesystem::path p;
236         p /= directory;
237         p /= f;
238         return p.string ();
239 }
240
241 string
242 FilmState::content_path () const
243 {
244         if (filesystem::path(content).has_root_directory ()) {
245                 return content;
246         }
247
248         return file (content);
249 }
250
251 ContentType
252 FilmState::content_type () const
253 {
254 #if BOOST_FILESYSTEM_VERSION == 3
255         string ext = filesystem::path(content).extension().string();
256 #else
257         string ext = filesystem::path(content).extension();
258 #endif
259
260         transform (ext.begin(), ext.end(), ext.begin(), ::tolower);
261         
262         if (ext == ".tif" || ext == ".tiff" || ext == ".jpg" || ext == ".jpeg" || ext == ".png") {
263                 return STILL;
264         }
265
266         return VIDEO;
267 }
268
269 /** @return Number of bytes per sample of a single channel */
270 int
271 FilmState::bytes_per_sample () const
272 {
273         switch (audio_sample_format) {
274         case AV_SAMPLE_FMT_S16:
275                 return 2;
276         default:
277                 return 0;
278         }
279
280         return 0;
281 }
282
283 int
284 FilmState::target_sample_rate () const
285 {
286         /* Resample to a DCI-approved sample rate */
287         double t = dcp_audio_sample_rate (audio_sample_rate);
288
289         /* Compensate for the fact that video will be rounded to the
290            nearest integer number of frames per second.
291         */
292         if (rint (frames_per_second) != frames_per_second) {
293                 t *= frames_per_second / rint (frames_per_second);
294         }
295
296         return rint (t);
297 }
298
299 int
300 FilmState::dcp_length () const
301 {
302         if (dcp_frames) {
303                 return dcp_frames;
304         }
305
306         return length;
307 }
308
309