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.
27 #include <boost/filesystem.hpp>
28 #include <boost/algorithm/string.hpp>
31 #include "tiff_encoder.h"
34 #include "transcoder.h"
36 #include "job_manager.h"
37 #include "ab_transcode_job.h"
38 #include "transcode_job.h"
39 #include "scp_dcp_job.h"
40 #include "copy_from_dvd_job.h"
41 #include "make_dcp_job.h"
42 #include "film_state.h"
45 #include "exceptions.h"
46 #include "examine_content_job.h"
48 #include "decoder_factory.h"
52 using namespace boost;
54 /** Construct a Film object in a given directory, reading any metadata
55 * file that exists in that directory. An exception will be thrown if
56 * must_exist is true, and the specified directory does not exist.
58 * @param d Film directory.
59 * @param must_exist true to throw an exception if does not exist.
62 Film::Film (string d, bool must_exist)
65 /* Make _state.directory a complete path without ..s (where possible)
66 (Code swiped from Adam Bowen on stackoverflow)
69 filesystem::path p (filesystem::system_complete (d));
70 filesystem::path result;
71 for(filesystem::path::iterator i = p.begin(); i != p.end(); ++i) {
73 if (filesystem::is_symlink (result) || result.filename() == "..") {
76 result = result.parent_path ();
78 } else if (*i != ".") {
83 _state.directory = result.string ();
85 if (must_exist && !filesystem::exists (_state.directory)) {
86 throw OpenFileError (_state.directory);
91 _log = new Log (_state.file ("log"));
94 /** Copy constructor */
95 Film::Film (Film const & other)
96 : _state (other._state)
97 , _dirty (other._dirty)
107 /** Read the `metadata' file inside this Film's directory, and fill the
108 * object's data with its content.
112 Film::read_metadata ()
114 ifstream f (metadata_file().c_str ());
116 while (getline (f, line)) {
121 if (line[0] == '#') {
125 size_t const s = line.find (' ');
126 if (s == string::npos) {
130 _state.read_metadata (line.substr (0, s), line.substr (s + 1));
136 /** Write our state to a file `metadata' inside the Film's directory */
138 Film::write_metadata () const
140 filesystem::create_directories (_state.directory);
142 ofstream f (metadata_file().c_str ());
144 throw CreateFileError (metadata_file ());
147 _state.write_metadata (f);
152 /** Set the name by which DVD-o-matic refers to this Film */
154 Film::set_name (string n)
157 signal_changed (NAME);
160 /** Set the content file for this film.
161 * @param c New content file; if specified as an absolute path, the content should
162 * be within the film's _state.directory; if specified as a relative path, the content
163 * will be assumed to be within the film's _state.directory.
166 Film::set_content (string c)
168 if (filesystem::path(c).has_root_directory () && starts_with (c, _state.directory)) {
169 c = c.substr (_state.directory.length() + 1);
172 if (c == _state.content) {
176 /* Create a temporary decoder so that we can get information
179 shared_ptr<FilmState> s = state_copy ();
181 shared_ptr<Options> o (new Options ("", "", ""));
182 o->out_size = Size (1024, 1024);
184 shared_ptr<Decoder> d = decoder_factory (s, o, 0, _log);
186 _state.size = d->native_size ();
187 _state.length = d->length_in_frames ();
188 _state.frames_per_second = d->frames_per_second ();
189 _state.audio_channels = d->audio_channels ();
190 _state.audio_sample_rate = d->audio_sample_rate ();
191 _state.audio_sample_format = d->audio_sample_format ();
193 _state.content_digest = md5_digest (c);
196 signal_changed (SIZE);
197 signal_changed (LENGTH);
198 signal_changed (FRAMES_PER_SECOND);
199 signal_changed (AUDIO_CHANNELS);
200 signal_changed (AUDIO_SAMPLE_RATE);
201 signal_changed (CONTENT);
204 /** Set the format that this Film should be shown in */
206 Film::set_format (Format const * f)
209 signal_changed (FORMAT);
212 /** Set the type to specify the DCP as having
213 * (feature, trailer etc.)
216 Film::set_dcp_content_type (DCPContentType const * t)
218 _state.dcp_content_type = t;
219 signal_changed (DCP_CONTENT_TYPE);
222 /** Set the number of pixels by which to crop the left of the source video */
224 Film::set_left_crop (int c)
226 if (c == _state.left_crop) {
230 _state.left_crop = c;
231 signal_changed (LEFT_CROP);
234 /** Set the number of pixels by which to crop the right of the source video */
236 Film::set_right_crop (int c)
238 if (c == _state.right_crop) {
242 _state.right_crop = c;
243 signal_changed (RIGHT_CROP);
246 /** Set the number of pixels by which to crop the top of the source video */
248 Film::set_top_crop (int c)
250 if (c == _state.top_crop) {
255 signal_changed (TOP_CROP);
258 /** Set the number of pixels by which to crop the bottom of the source video */
260 Film::set_bottom_crop (int c)
262 if (c == _state.bottom_crop) {
266 _state.bottom_crop = c;
267 signal_changed (BOTTOM_CROP);
270 /** Set the filters to apply to the image when generating thumbnails
274 Film::set_filters (vector<Filter const *> const & f)
277 signal_changed (FILTERS);
280 /** Set the number of frames to put in any generated DCP (from
281 * the start of the film). 0 indicates that all frames should
285 Film::set_dcp_frames (int n)
287 _state.dcp_frames = n;
288 signal_changed (DCP_FRAMES);
292 Film::set_dcp_trim_action (TrimAction a)
294 _state.dcp_trim_action = a;
295 signal_changed (DCP_TRIM_ACTION);
298 /** Set whether or not to generate a A/B comparison DCP.
299 * Such a DCP has the left half of its frame as the Film
300 * content without any filtering or post-processing; the
301 * right half is rendered with filters and post-processing.
304 Film::set_dcp_ab (bool a)
307 signal_changed (DCP_AB);
311 Film::set_audio_gain (float g)
313 _state.audio_gain = g;
314 signal_changed (AUDIO_GAIN);
318 Film::set_audio_delay (int d)
320 _state.audio_delay = d;
321 signal_changed (AUDIO_DELAY);
324 /** @return path of metadata file */
326 Film::metadata_file () const
328 return _state.file ("metadata");
331 /** @return full path of the content (actual video) file
335 Film::content () const
337 return _state.content_path ();
340 /** The pre-processing GUI part of a thumbs update.
341 * Must be called from the GUI thread.
344 Film::update_thumbs_pre_gui ()
346 _state.thumbs.clear ();
347 filesystem::remove_all (_state.dir ("thumbs"));
349 /* This call will recreate the directory */
350 _state.dir ("thumbs");
353 /** The post-processing GUI part of a thumbs update.
354 * Must be called from the GUI thread.
357 Film::update_thumbs_post_gui ()
359 string const tdir = _state.dir ("thumbs");
361 for (filesystem::directory_iterator i = filesystem::directory_iterator (tdir); i != filesystem::directory_iterator(); ++i) {
363 /* Aah, the sweet smell of progress */
364 #if BOOST_FILESYSTEM_VERSION == 3
365 string const l = filesystem::path(*i).leaf().generic_string();
367 string const l = i->leaf ();
370 size_t const d = l.find (".tiff");
371 if (d != string::npos) {
372 _state.thumbs.push_back (atoi (l.substr (0, d).c_str()));
376 sort (_state.thumbs.begin(), _state.thumbs.end());
379 signal_changed (THUMBS);
382 /** @return the number of thumbnail images that we have */
384 Film::num_thumbs () const
386 return _state.thumbs.size ();
389 /** @param n A thumb index.
390 * @return The frame within the Film that it is for.
393 Film::thumb_frame (int n) const
395 return _state.thumb_frame (n);
398 /** @param n A thumb index.
399 * @return The path to the thumb's image file.
402 Film::thumb_file (int n) const
404 return _state.thumb_file (n);
407 /** @return The path to the directory to write JPEG2000 files to */
409 Film::j2k_dir () const
419 pair<string, string> f = Filter::ffmpeg_strings (filters ());
421 /* Write stuff to specify the filter / post-processing settings that are in use,
422 so that we don't get confused about J2K files generated using different
426 s << _state.format->id()
427 << "_" << _state.content_digest
428 << "_" << left_crop() << "_" << right_crop() << "_" << top_crop() << "_" << bottom_crop()
429 << "_" << f.first << "_" << f.second
430 << "_" << _state.scaler->id();
434 /* Similarly for the A/B case */
437 pair<string, string> fa = Filter::ffmpeg_strings (Config::instance()->reference_filters());
438 s << "ab_" << Config::instance()->reference_scaler()->id() << "_" << fa.first << "_" << fa.second;
442 return _state.dir (p.string ());
445 /** Handle a change to the Film's metadata */
447 Film::signal_changed (Property p)
453 /** Add suitable Jobs to the JobManager to create a DCP for this Film.
454 * @param true to transcode, false to use the WAV and J2K files that are already there.
457 Film::make_dcp (bool transcode, int freq)
459 string const t = name ();
460 if (t.find ("/") != string::npos) {
461 throw BadSettingError ("name", "cannot contain slashes");
466 s << "DVD-o-matic " << DVDOMATIC_VERSION << " using " << dependency_version_summary ();
467 log()->log (s.str ());
472 gethostname (buffer, sizeof (buffer));
474 s << "Starting to make a DCP on " << buffer;
475 log()->log (s.str ());
479 throw MissingSettingError ("format");
482 if (content().empty ()) {
483 throw MissingSettingError ("content");
486 if (dcp_content_type() == 0) {
487 throw MissingSettingError ("content type");
490 if (name().empty()) {
491 throw MissingSettingError ("name");
494 shared_ptr<const FilmState> fs = state_copy ();
495 shared_ptr<Options> o (new Options (j2k_dir(), ".j2c", _state.dir ("wavs")));
496 o->out_size = format()->dcp_size ();
497 if (dcp_frames() == 0) {
498 /* Decode the whole film, no blacking */
502 switch (dcp_trim_action()) {
504 /* Decode only part of the film, no blacking */
505 o->num_frames = dcp_frames ();
509 /* Decode the whole film, but black some frames out */
511 o->black_after = dcp_frames ();
515 o->decode_video_frequency = freq;
516 o->padding = format()->dcp_padding ();
517 o->ratio = format()->ratio_as_float ();
521 JobManager::instance()->add (shared_ptr<Job> (new ABTranscodeJob (fs, o, log ())));
523 JobManager::instance()->add (shared_ptr<Job> (new TranscodeJob (fs, o, log ())));
527 JobManager::instance()->add (shared_ptr<Job> (new MakeDCPJob (fs, o, log ())));
530 shared_ptr<FilmState>
531 Film::state_copy () const
533 return shared_ptr<FilmState> (new FilmState (_state));
537 Film::copy_from_dvd_post_gui ()
539 const string dvd_dir = _state.dir ("dvd");
542 uintmax_t largest_size = 0;
543 for (filesystem::directory_iterator i = filesystem::directory_iterator (dvd_dir); i != filesystem::directory_iterator(); ++i) {
544 uintmax_t const s = filesystem::file_size (*i);
545 if (s > largest_size) {
547 #if BOOST_FILESYSTEM_VERSION == 3
548 largest_file = filesystem::path(*i).generic_string();
550 largest_file = i->string ();
556 set_content (largest_file);
560 Film::examine_content ()
562 if (_examine_content_job) {
566 _examine_content_job.reset (new ExamineContentJob (state_copy (), log ()));
567 _examine_content_job->Finished.connect (sigc::mem_fun (*this, &Film::examine_content_post_gui));
568 JobManager::instance()->add (_examine_content_job);
572 Film::examine_content_post_gui ()
574 _state.length = _examine_content_job->last_video_frame ();
575 signal_changed (LENGTH);
577 _examine_content_job.reset ();
581 Film::set_scaler (Scaler const * s)
584 signal_changed (SCALER);
588 Film::set_frames_per_second (float f)
590 _state.frames_per_second = f;
591 signal_changed (FRAMES_PER_SECOND);
594 /** @return full paths to any audio files that this Film has */
596 Film::audio_files () const
599 for (filesystem::directory_iterator i = filesystem::directory_iterator (_state.dir("wavs")); i != filesystem::directory_iterator(); ++i) {
600 f.push_back (i->path().string ());
607 Film::content_type () const
609 return _state.content_type ();
613 Film::set_still_duration (int d)
615 _state.still_duration = d;
616 signal_changed (STILL_DURATION);
620 Film::send_dcp_to_tms ()
622 shared_ptr<Job> j (new SCPDCPJob (state_copy (), log ()));
623 JobManager::instance()->add (j);
627 Film::copy_from_dvd ()
629 shared_ptr<Job> j (new CopyFromDVDJob (state_copy (), log ()));
630 j->Finished.connect (sigc::mem_fun (*this, &Film::copy_from_dvd_post_gui));
631 JobManager::instance()->add (j);