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 " << 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";
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";
95 f << "content_digest " << content_digest << "\n";
98 /** Read state from a key / value pair.
103 FilmState::read_metadata (string k, string v)
105 /* User-specified stuff */
108 } else if (k == "content") {
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") {
132 dcp_trim_action = CUT;
133 } else if (v == "black_out") {
134 dcp_trim_action = BLACK_OUT;
136 } else if (k == "dcp_ab") {
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 ());
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);
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") {
169 /* Itsy bitsy hack: compute digest here if don't have one (for backwards compatibility) */
170 if (content_digest.empty() && !content.empty()) {
171 content_digest = md5_digest (content_path ());
176 /** @param n A thumb index.
177 * @return The path to the thumb's image file.
180 FilmState::thumb_file (int n) const
182 return thumb_file_for_frame (thumb_frame (n));
185 /** @param n A frame index within the Film.
186 * @return The path to the thumb's image file for this frame;
187 * we assume that it exists.
190 FilmState::thumb_file_for_frame (int n) const
194 s << setfill('0') << n << ".tiff";
204 /** @param n A thumb index.
205 * @return The frame within the Film that it is for.
208 FilmState::thumb_frame (int n) const
210 assert (n < int (thumbs.size ()));
215 FilmState::cropped_size (Size s) const
217 s.width -= crop.left + crop.right;
218 s.height -= crop.top + crop.bottom;
222 /** Given a directory name, return its full path within the Film's directory.
223 * The directory (and its parents) will be created if they do not exist.
226 FilmState::dir (string d) const
231 filesystem::create_directories (p);
235 /** Given a file or directory name, return its full path within the Film's directory */
237 FilmState::file (string f) const
246 FilmState::content_path () const
248 if (filesystem::path(content).has_root_directory ()) {
252 return file (content);
256 FilmState::content_type () const
258 #if BOOST_FILESYSTEM_VERSION == 3
259 string const ext = filesystem::path(content).extension().string();
261 string const ext = filesystem::path(content).extension();
263 if (ext == ".tif" || ext == ".tiff" || ext == ".jpg" || ext == ".jpeg" || ext == ".png") {
270 /** @return Number of bytes per sample of a single channel */
272 FilmState::bytes_per_sample () const
274 switch (audio_sample_format) {
275 case AV_SAMPLE_FMT_S16: