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"
40 using namespace boost;
42 /** Write state to a stream.
43 * @param f Stream to write to.
46 FilmState::write_metadata (ofstream& f) const
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";
54 f << "frames_per_second " << frames_per_second << "\n";
56 f << "format " << format->as_metadata () << "\n";
58 f << "left_crop " << left_crop << "\n";
59 f << "right_crop " << right_crop << "\n";
60 f << "top_crop " << top_crop << "\n";
61 f << "bottom_crop " << bottom_crop << "\n";
62 for (vector<Filter const *>::const_iterator i = filters.begin(); i != filters.end(); ++i) {
63 f << "filter " << (*i)->id () << "\n";
65 f << "scaler " << scaler->id () << "\n";
66 f << "dcp_frames " << dcp_frames << "\n";
68 f << "dcp_trim_action ";
69 switch (dcp_trim_action) {
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";
83 /* Cached stuff; this is information about our content; we could
84 look it up each time, but that's slow.
86 for (vector<int>::const_iterator i = thumbs.begin(); i != thumbs.end(); ++i) {
87 f << "thumb " << *i << "\n";
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";
97 /** Read state from a key / value pair.
102 FilmState::read_metadata (string k, string v)
104 /* User-specified stuff */
107 } else if (k == "content") {
109 } else if (k == "dcp_content_type") {
110 dcp_content_type = DCPContentType::from_pretty_name (v);
111 } else if (k == "frames_per_second") {
112 frames_per_second = atof (v.c_str ());
113 } else if (k == "format") {
114 format = Format::from_metadata (v);
115 } else if (k == "left_crop") {
116 left_crop = atoi (v.c_str ());
117 } else if (k == "right_crop") {
118 right_crop = atoi (v.c_str ());
119 } else if (k == "top_crop") {
120 top_crop = atoi (v.c_str ());
121 } else if (k == "bottom_crop") {
122 bottom_crop = atoi (v.c_str ());
123 } else if (k == "filter") {
124 filters.push_back (Filter::from_id (v));
125 } else if (k == "scaler") {
126 scaler = Scaler::from_id (v);
127 } else if (k == "dcp_frames") {
128 dcp_frames = atoi (v.c_str ());
129 } else if (k == "dcp_trim_action") {
131 dcp_trim_action = CUT;
132 } else if (v == "black_out") {
133 dcp_trim_action = BLACK_OUT;
135 } else if (k == "dcp_ab") {
137 } else if (k == "audio_gain") {
138 audio_gain = atof (v.c_str ());
139 } else if (k == "audio_delay") {
140 audio_delay = atoi (v.c_str ());
141 } else if (k == "still_duration") {
142 still_duration = atoi (v.c_str ());
147 int const n = atoi (v.c_str ());
148 /* Only add it to the list if it still exists */
149 if (filesystem::exists (thumb_file_for_frame (n))) {
150 thumbs.push_back (n);
152 } else if (k == "width") {
153 size.width = atoi (v.c_str ());
154 } else if (k == "height") {
155 size.height = atoi (v.c_str ());
156 } else if (k == "length") {
157 length = atof (v.c_str ());
158 } else if (k == "audio_channels") {
159 audio_channels = atoi (v.c_str ());
160 } else if (k == "audio_sample_rate") {
161 audio_sample_rate = atoi (v.c_str ());
162 } else if (k == "audio_sample_format") {
163 audio_sample_format = audio_sample_format_from_string (v);
168 /** @param n A thumb index.
169 * @return The path to the thumb's image file.
172 FilmState::thumb_file (int n) const
174 return thumb_file_for_frame (thumb_frame (n));
177 /** @param n A frame index within the Film.
178 * @return The path to the thumb's image file for this frame;
179 * we assume that it exists.
182 FilmState::thumb_file_for_frame (int n) const
185 s << dir ("thumbs") << "/";
187 s << setfill('0') << n << ".tiff";
192 /** @param n A thumb index.
193 * @return The frame within the Film that it is for.
196 FilmState::thumb_frame (int n) const
198 assert (n < int (thumbs.size ()));
203 FilmState::cropped_size (Size s) const
205 s.width -= left_crop + right_crop;
206 s.height -= top_crop + bottom_crop;
210 /** Given a directory name, return its full path within the Film's directory.
211 * The directory (and its parents) will be created if they do not exist.
214 FilmState::dir (string d) const
217 s << directory << "/" << d;
218 filesystem::create_directories (s.str ());
222 /** Given a file or directory name, return its full path within the Film's directory */
224 FilmState::file (string f) const
227 s << directory << "/" << f;
232 FilmState::content_path () const
234 if (filesystem::path(content).has_root_directory ()) {
238 return file (content);
242 FilmState::content_type () const
244 #if BOOST_FILESYSTEM_VERSION == 3
245 string const ext = filesystem::path(content).extension().string();
247 string const ext = filesystem::path(content).extension();
249 if (ext == ".tif" || ext == ".tiff" || ext == ".jpg" || ext == ".jpeg" || ext == ".png") {