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";
84 /* Cached stuff; this is information about our content; we could
85 look it up each time, but that's slow.
87 for (vector<int>::const_iterator i = thumbs.begin(); i != thumbs.end(); ++i) {
88 f << "thumb " << *i << "\n";
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";
99 /** Read state from a key / value pair.
104 FilmState::read_metadata (string k, string v)
106 /* User-specified stuff */
109 } else if (k == "content") {
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") {
133 dcp_trim_action = CUT;
134 } else if (v == "black_out") {
135 dcp_trim_action = BLACK_OUT;
137 } else if (k == "dcp_ab") {
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 ());
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);
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") {
172 /** @param n A thumb index.
173 * @return The path to the thumb's image file.
176 FilmState::thumb_file (int n) const
178 return thumb_file_for_frame (thumb_frame (n));
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.
186 FilmState::thumb_file_for_frame (int n) const
190 s << setfill('0') << n << ".tiff";
200 /** @param n A thumb index.
201 * @return The frame within the Film that it is for.
204 FilmState::thumb_frame (int n) const
206 assert (n < int (thumbs.size ()));
211 FilmState::cropped_size (Size s) const
213 s.width -= crop.left + crop.right;
214 s.height -= crop.top + crop.bottom;
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.
222 FilmState::dir (string d) const
227 filesystem::create_directories (p);
231 /** Given a file or directory name, return its full path within the Film's directory */
233 FilmState::file (string f) const
242 FilmState::content_path () const
244 if (filesystem::path(content).has_root_directory ()) {
248 return file (content);
252 FilmState::content_type () const
254 #if BOOST_FILESYSTEM_VERSION == 3
255 string ext = filesystem::path(content).extension().string();
257 string ext = filesystem::path(content).extension();
260 transform (ext.begin(), ext.end(), ext.begin(), ::tolower);
262 if (ext == ".tif" || ext == ".tiff" || ext == ".jpg" || ext == ".jpeg" || ext == ".png") {
269 /** @return Number of bytes per sample of a single channel */
271 FilmState::bytes_per_sample () const
273 switch (audio_sample_format) {
274 case AV_SAMPLE_FMT_S16:
284 FilmState::target_sample_rate () const
286 double t = dcp_audio_sample_rate (audio_sample_rate);
287 if (rint (frames_per_second) != frames_per_second) {
288 if (fabs (frames_per_second - 23.976) < 1e-6 || (fabs (frames_per_second - 29.97) < 1e-6)) {
289 /* 24fps or 30fps drop-frame ie {24,30} * 1000 / 1001 frames per second;
290 hence we need to resample the audio to dcp_audio_sample_rate * 1000 / 1001
291 so that when we play it back at dcp_audio_sample_rate it is sped up
292 by the same amount that the video is
294 t *= double(1000) / 1001;
296 throw EncodeError ("unknown fractional frame rate");