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.
28 #include <boost/filesystem.hpp>
29 #include <boost/algorithm/string.hpp>
32 #include "tiff_encoder.h"
35 #include "transcoder.h"
37 #include "job_manager.h"
38 #include "ab_transcode_job.h"
39 #include "transcode_job.h"
40 #include "scp_dcp_job.h"
41 #include "copy_from_dvd_job.h"
42 #include "make_dcp_job.h"
43 #include "film_state.h"
46 #include "exceptions.h"
47 #include "examine_content_job.h"
49 #include "decoder_factory.h"
53 using namespace boost;
55 /** Construct a Film object in a given directory, reading any metadata
56 * file that exists in that directory. An exception will be thrown if
57 * must_exist is true, and the specified directory does not exist.
59 * @param d Film directory.
60 * @param must_exist true to throw an exception if does not exist.
63 Film::Film (string d, bool must_exist)
66 /* Make _state.directory a complete path without ..s (where possible)
67 (Code swiped from Adam Bowen on stackoverflow)
70 filesystem::path p (filesystem::system_complete (d));
71 filesystem::path result;
72 for(filesystem::path::iterator i = p.begin(); i != p.end(); ++i) {
74 if (filesystem::is_symlink (result) || result.filename() == "..") {
77 result = result.parent_path ();
79 } else if (*i != ".") {
84 _state.directory = result.string ();
86 if (must_exist && !filesystem::exists (_state.directory)) {
87 throw OpenFileError (_state.directory);
92 _log = new FileLog (_state.file ("log"));
95 /** Copy constructor */
96 Film::Film (Film const & other)
97 : _state (other._state)
98 , _dirty (other._dirty)
108 /** Read the `metadata' file inside this Film's directory, and fill the
109 * object's data with its content.
113 Film::read_metadata ()
115 ifstream f (metadata_file().c_str ());
117 while (getline (f, line)) {
122 if (line[0] == '#') {
126 size_t const s = line.find (' ');
127 if (s == string::npos) {
131 _state.read_metadata (line.substr (0, s), line.substr (s + 1));
137 /** Write our state to a file `metadata' inside the Film's directory */
139 Film::write_metadata () const
141 filesystem::create_directories (_state.directory);
143 ofstream f (metadata_file().c_str ());
145 throw CreateFileError (metadata_file ());
148 _state.write_metadata (f);
153 /** Set the name by which DVD-o-matic refers to this Film */
155 Film::set_name (string n)
158 signal_changed (NAME);
161 /** Set the content file for this film.
162 * @param c New content file; if specified as an absolute path, the content should
163 * be within the film's _state.directory; if specified as a relative path, the content
164 * will be assumed to be within the film's _state.directory.
167 Film::set_content (string c)
169 string check = _state.directory;
171 #if BOOST_FILESYSTEM_VERSION == 3
172 filesystem::path slash ("/");
173 string platform_slash = slash.make_preferred().string ();
175 #ifdef DVDOMATIC_WINDOWS
176 string platform_slash = "\\";
178 string platform_slash = "/";
182 if (!ends_with (check, platform_slash)) {
183 check += platform_slash;
186 if (filesystem::path(c).has_root_directory () && starts_with (c, check)) {
187 c = c.substr (_state.directory.length() + 1);
190 if (c == _state.content) {
194 /* Create a temporary decoder so that we can get information
197 shared_ptr<FilmState> s = state_copy ();
199 shared_ptr<Options> o (new Options ("", "", ""));
200 o->out_size = Size (1024, 1024);
202 shared_ptr<Decoder> d = decoder_factory (s, o, 0, _log);
204 _state.size = d->native_size ();
205 _state.length = d->length_in_frames ();
206 _state.frames_per_second = d->frames_per_second ();
207 _state.audio_channels = d->audio_channels ();
208 _state.audio_sample_rate = d->audio_sample_rate ();
209 _state.audio_sample_format = d->audio_sample_format ();
211 _state.content_digest = md5_digest (s->content_path ());
214 signal_changed (SIZE);
215 signal_changed (LENGTH);
216 signal_changed (FRAMES_PER_SECOND);
217 signal_changed (AUDIO_CHANNELS);
218 signal_changed (AUDIO_SAMPLE_RATE);
219 signal_changed (CONTENT);
222 /** Set the format that this Film should be shown in */
224 Film::set_format (Format const * f)
227 signal_changed (FORMAT);
230 /** Set the type to specify the DCP as having
231 * (feature, trailer etc.)
234 Film::set_dcp_content_type (DCPContentType const * t)
236 _state.dcp_content_type = t;
237 signal_changed (DCP_CONTENT_TYPE);
240 /** Set the number of pixels by which to crop the left of the source video */
242 Film::set_left_crop (int c)
244 if (c == _state.crop.left) {
248 _state.crop.left = c;
249 signal_changed (CROP);
252 /** Set the number of pixels by which to crop the right of the source video */
254 Film::set_right_crop (int c)
256 if (c == _state.crop.right) {
260 _state.crop.right = c;
261 signal_changed (CROP);
264 /** Set the number of pixels by which to crop the top of the source video */
266 Film::set_top_crop (int c)
268 if (c == _state.crop.top) {
273 signal_changed (CROP);
276 /** Set the number of pixels by which to crop the bottom of the source video */
278 Film::set_bottom_crop (int c)
280 if (c == _state.crop.bottom) {
284 _state.crop.bottom = c;
285 signal_changed (CROP);
288 /** Set the filters to apply to the image when generating thumbnails
292 Film::set_filters (vector<Filter const *> const & f)
295 signal_changed (FILTERS);
298 /** Set the number of frames to put in any generated DCP (from
299 * the start of the film). 0 indicates that all frames should
303 Film::set_dcp_frames (int n)
305 _state.dcp_frames = n;
306 signal_changed (DCP_FRAMES);
310 Film::set_dcp_trim_action (TrimAction a)
312 _state.dcp_trim_action = a;
313 signal_changed (DCP_TRIM_ACTION);
316 /** Set whether or not to generate a A/B comparison DCP.
317 * Such a DCP has the left half of its frame as the Film
318 * content without any filtering or post-processing; the
319 * right half is rendered with filters and post-processing.
322 Film::set_dcp_ab (bool a)
325 signal_changed (DCP_AB);
329 Film::set_audio_gain (float g)
331 _state.audio_gain = g;
332 signal_changed (AUDIO_GAIN);
336 Film::set_audio_delay (int d)
338 _state.audio_delay = d;
339 signal_changed (AUDIO_DELAY);
342 /** @return path of metadata file */
344 Film::metadata_file () const
346 return _state.file ("metadata");
349 /** @return full path of the content (actual video) file
353 Film::content () const
355 return _state.content_path ();
358 /** The pre-processing GUI part of a thumbs update.
359 * Must be called from the GUI thread.
362 Film::update_thumbs_pre_gui ()
364 _state.thumbs.clear ();
365 filesystem::remove_all (_state.dir ("thumbs"));
367 /* This call will recreate the directory */
368 _state.dir ("thumbs");
371 /** The post-processing GUI part of a thumbs update.
372 * Must be called from the GUI thread.
375 Film::update_thumbs_post_gui ()
377 string const tdir = _state.dir ("thumbs");
379 for (filesystem::directory_iterator i = filesystem::directory_iterator (tdir); i != filesystem::directory_iterator(); ++i) {
381 /* Aah, the sweet smell of progress */
382 #if BOOST_FILESYSTEM_VERSION == 3
383 string const l = filesystem::path(*i).leaf().generic_string();
385 string const l = i->leaf ();
388 size_t const d = l.find (".tiff");
389 if (d != string::npos) {
390 _state.thumbs.push_back (atoi (l.substr (0, d).c_str()));
394 sort (_state.thumbs.begin(), _state.thumbs.end());
397 signal_changed (THUMBS);
400 /** @return the number of thumbnail images that we have */
402 Film::num_thumbs () const
404 return _state.thumbs.size ();
407 /** @param n A thumb index.
408 * @return The frame within the Film that it is for.
411 Film::thumb_frame (int n) const
413 return _state.thumb_frame (n);
416 /** @param n A thumb index.
417 * @return The path to the thumb's image file.
420 Film::thumb_file (int n) const
422 return _state.thumb_file (n);
425 /** @return The path to the directory to write JPEG2000 files to */
427 Film::j2k_dir () const
436 pair<string, string> f = Filter::ffmpeg_strings (filters ());
438 /* Write stuff to specify the filter / post-processing settings that are in use,
439 so that we don't get confused about J2K files generated using different
443 s << _state.format->id()
444 << "_" << _state.content_digest
445 << "_" << crop().left << "_" << crop().right << "_" << crop().top << "_" << crop().bottom
446 << "_" << f.first << "_" << f.second
447 << "_" << _state.scaler->id();
451 /* Similarly for the A/B case */
454 pair<string, string> fa = Filter::ffmpeg_strings (Config::instance()->reference_filters());
455 s << "ab_" << Config::instance()->reference_scaler()->id() << "_" << fa.first << "_" << fa.second;
459 return _state.dir (p.string ());
462 /** Handle a change to the Film's metadata */
464 Film::signal_changed (Property p)
470 /** Add suitable Jobs to the JobManager to create a DCP for this Film.
471 * @param true to transcode, false to use the WAV and J2K files that are already there.
474 Film::make_dcp (bool transcode, int freq)
476 string const t = name ();
477 if (t.find ("/") != string::npos) {
478 throw BadSettingError ("name", "cannot contain slashes");
483 s << "DVD-o-matic " << DVDOMATIC_VERSION << " using " << dependency_version_summary ();
484 log()->log (s.str ());
489 gethostname (buffer, sizeof (buffer));
491 s << "Starting to make a DCP on " << buffer;
492 log()->log (s.str ());
496 throw MissingSettingError ("format");
499 if (content().empty ()) {
500 throw MissingSettingError ("content");
503 if (dcp_content_type() == 0) {
504 throw MissingSettingError ("content type");
507 if (name().empty()) {
508 throw MissingSettingError ("name");
511 shared_ptr<const FilmState> fs = state_copy ();
512 shared_ptr<Options> o (new Options (j2k_dir(), ".j2c", _state.dir ("wavs")));
513 o->out_size = format()->dcp_size ();
514 if (dcp_frames() == 0) {
515 /* Decode the whole film, no blacking */
519 switch (dcp_trim_action()) {
521 /* Decode only part of the film, no blacking */
522 o->num_frames = dcp_frames ();
526 /* Decode the whole film, but black some frames out */
528 o->black_after = dcp_frames ();
532 o->decode_video_frequency = freq;
533 o->padding = format()->dcp_padding ();
534 o->ratio = format()->ratio_as_float ();
538 JobManager::instance()->add (shared_ptr<Job> (new ABTranscodeJob (fs, o, log ())));
540 JobManager::instance()->add (shared_ptr<Job> (new TranscodeJob (fs, o, log ())));
544 JobManager::instance()->add (shared_ptr<Job> (new MakeDCPJob (fs, o, log ())));
547 shared_ptr<FilmState>
548 Film::state_copy () const
550 return shared_ptr<FilmState> (new FilmState (_state));
554 Film::copy_from_dvd_post_gui ()
556 const string dvd_dir = _state.dir ("dvd");
559 uintmax_t largest_size = 0;
560 for (filesystem::directory_iterator i = filesystem::directory_iterator (dvd_dir); i != filesystem::directory_iterator(); ++i) {
561 uintmax_t const s = filesystem::file_size (*i);
562 if (s > largest_size) {
564 #if BOOST_FILESYSTEM_VERSION == 3
565 largest_file = filesystem::path(*i).generic_string();
567 largest_file = i->string ();
573 set_content (largest_file);
577 Film::examine_content ()
579 if (_examine_content_job) {
583 _examine_content_job.reset (new ExamineContentJob (state_copy (), log ()));
584 _examine_content_job->Finished.connect (sigc::mem_fun (*this, &Film::examine_content_post_gui));
585 JobManager::instance()->add (_examine_content_job);
589 Film::examine_content_post_gui ()
591 _state.length = _examine_content_job->last_video_frame ();
592 signal_changed (LENGTH);
594 _examine_content_job.reset ();
598 Film::set_scaler (Scaler const * s)
601 signal_changed (SCALER);
604 /** @return full paths to any audio files that this Film has */
606 Film::audio_files () const
609 for (filesystem::directory_iterator i = filesystem::directory_iterator (_state.dir("wavs")); i != filesystem::directory_iterator(); ++i) {
610 f.push_back (i->path().string ());
617 Film::content_type () const
619 return _state.content_type ();
623 Film::set_still_duration (int d)
625 _state.still_duration = d;
626 signal_changed (STILL_DURATION);
630 Film::send_dcp_to_tms ()
632 shared_ptr<Job> j (new SCPDCPJob (state_copy (), log ()));
633 JobManager::instance()->add (j);
637 Film::copy_from_dvd ()
639 shared_ptr<Job> j (new CopyFromDVDJob (state_copy (), log ()));
640 j->Finished.connect (sigc::mem_fun (*this, &Film::copy_from_dvd_post_gui));
641 JobManager::instance()->add (j);
645 Film::encoded_frames () const
651 return distance (filesystem::directory_iterator (j2k_dir()), filesystem::directory_iterator ());