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