2 Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
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.
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.
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.
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.
31 #include <boost/filesystem.hpp>
32 #include "film_state.h"
36 #include "dcp_content_type.h"
38 #include "exceptions.h"
41 using namespace boost;
43 /** Write state to a stream.
44 * @param f Stream to write to.
47 FilmState::write_metadata (ofstream& f) const
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";
55 f << "frames_per_second " << frames_per_second << "\n";
57 f << "format " << format->as_metadata () << "\n";
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";
66 f << "scaler " << scaler->id () << "\n";
67 f << "dcp_frames " << dcp_frames << "\n";
69 f << "dcp_trim_action ";
70 switch (dcp_trim_action) {
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";
85 /* Cached stuff; this is information about our content; we could
86 look it up each time, but that's slow.
88 for (vector<int>::const_iterator i = thumbs.begin(); i != thumbs.end(); ++i) {
89 f << "thumb " << *i << "\n";
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";
101 /** Read state from a key / value pair.
106 FilmState::read_metadata (string k, string v)
108 /* User-specified stuff */
111 } else if (k == "content") {
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") {
135 dcp_trim_action = CUT;
136 } else if (v == "black_out") {
137 dcp_trim_action = BLACK_OUT;
139 } else if (k == "dcp_ab") {
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");
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);
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") {
172 } else if (k == "has_subtitles") {
173 has_subtitles = (v == "1");
178 /** @param n A thumb index.
179 * @return The path to the thumb's image file.
182 FilmState::thumb_file (int n) const
184 return thumb_file_for_frame (thumb_frame (n));
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.
192 FilmState::thumb_file_for_frame (int n) const
194 return thumb_base_for_frame(n) + ".png";
198 FilmState::thumb_base (int n) const
200 return thumb_base_for_frame (thumb_frame (n));
204 FilmState::thumb_base_for_frame (int n) const
208 s << setfill('0') << n;
218 /** @param n A thumb index.
219 * @return The frame within the Film that it is for.
222 FilmState::thumb_frame (int n) const
224 assert (n < int (thumbs.size ()));
229 FilmState::cropped_size (Size s) const
231 s.width -= crop.left + crop.right;
232 s.height -= crop.top + crop.bottom;
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.
240 FilmState::dir (string d) const
245 filesystem::create_directories (p);
249 /** Given a file or directory name, return its full path within the Film's directory */
251 FilmState::file (string f) const
260 FilmState::content_path () const
262 if (filesystem::path(content).has_root_directory ()) {
266 return file (content);
270 FilmState::content_type () const
272 #if BOOST_FILESYSTEM_VERSION == 3
273 string ext = filesystem::path(content).extension().string();
275 string ext = filesystem::path(content).extension();
278 transform (ext.begin(), ext.end(), ext.begin(), ::tolower);
280 if (ext == ".tif" || ext == ".tiff" || ext == ".jpg" || ext == ".jpeg" || ext == ".png") {
287 /** @return Number of bytes per sample of a single channel */
289 FilmState::bytes_per_sample () const
291 switch (audio_sample_format) {
292 case AV_SAMPLE_FMT_S16:
302 FilmState::target_sample_rate () const
304 /* Resample to a DCI-approved sample rate */
305 double t = dcp_audio_sample_rate (audio_sample_rate);
307 /* Compensate for the fact that video will be rounded to the
308 nearest integer number of frames per second.
310 if (rint (frames_per_second) != frames_per_second) {
311 t *= frames_per_second / rint (frames_per_second);
318 FilmState::dcp_length () const