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.
22 #include <libdcp/mono_picture_asset.h>
23 #include <libdcp/stereo_picture_asset.h>
24 #include <libdcp/sound_asset.h>
25 #include <libdcp/reel.h>
26 #include <libdcp/dcp.h>
27 #include <libdcp/cpl.h>
29 #include "compose.hpp"
33 #include "dcp_video_frame.h"
34 #include "dcp_content_type.h"
36 #include "audio_mapping.h"
48 using boost::shared_ptr;
49 using boost::weak_ptr;
51 int const Writer::_maximum_frames_in_memory = 8;
53 Writer::Writer (shared_ptr<const Film> f, weak_ptr<Job> j)
56 , _first_nonexistant_frame (0)
59 , _queued_full_in_memory (0)
60 , _last_written_frame (-1)
61 , _last_written_eyes (EYES_RIGHT)
67 /* Remove any old DCP */
68 boost::filesystem::remove_all (_film->dir (_film->dcp_name ()));
70 shared_ptr<Job> job = _job.lock ();
73 job->sub (_("Checking existing image data"));
74 check_existing_picture_mxf ();
76 /* Create our picture asset in a subdirectory, named according to those
77 film's parameters which affect the video output. We will hard-link
78 it into the DCP later.
81 if (_film->three_d ()) {
82 _picture_asset.reset (new libdcp::StereoPictureAsset (_film->internal_video_mxf_dir (), _film->internal_video_mxf_filename ()));
84 _picture_asset.reset (new libdcp::MonoPictureAsset (_film->internal_video_mxf_dir (), _film->internal_video_mxf_filename ()));
87 _picture_asset->set_edit_rate (_film->video_frame_rate ());
88 _picture_asset->set_size (fit_ratio_within (_film->container()->ratio(), _film->full_frame ()));
90 if (_film->encrypted ()) {
91 _picture_asset->set_key (_film->key ());
94 _picture_asset_writer = _picture_asset->start_write (_first_nonexistant_frame > 0);
96 /* Write the sound asset into the film directory so that we leave the creation
97 of the DCP directory until the last minute.
99 _sound_asset.reset (new libdcp::SoundAsset (_film->dir ("."), _film->audio_mxf_filename ()));
100 _sound_asset->set_edit_rate (_film->video_frame_rate ());
101 _sound_asset->set_channels (_film->audio_channels ());
102 _sound_asset->set_sampling_rate (_film->audio_frame_rate ());
104 if (_film->encrypted ()) {
105 _sound_asset->set_key (_film->key ());
108 _sound_asset_writer = _sound_asset->start_write ();
110 _thread = new boost::thread (boost::bind (&Writer::thread, this));
112 job->sub (_("Encoding image data"));
116 Writer::write (shared_ptr<const EncodedData> encoded, int frame, Eyes eyes)
118 boost::mutex::scoped_lock lock (_mutex);
121 qi.type = QueueItem::FULL;
122 qi.encoded = encoded;
125 if (_film->three_d() && eyes == EYES_BOTH) {
126 /* 2D material in a 3D DCP; fake the 3D */
128 _queue.push_back (qi);
129 ++_queued_full_in_memory;
130 qi.eyes = EYES_RIGHT;
131 _queue.push_back (qi);
132 ++_queued_full_in_memory;
135 _queue.push_back (qi);
136 ++_queued_full_in_memory;
139 _condition.notify_all ();
143 Writer::fake_write (int frame, Eyes eyes)
145 boost::mutex::scoped_lock lock (_mutex);
147 FILE* ifi = fopen_boost (_film->info_path (frame, eyes), "r");
148 libdcp::FrameInfo info (ifi);
152 qi.type = QueueItem::FAKE;
155 if (_film->three_d() && eyes == EYES_BOTH) {
157 _queue.push_back (qi);
158 qi.eyes = EYES_RIGHT;
159 _queue.push_back (qi);
162 _queue.push_back (qi);
165 _condition.notify_all ();
168 /** This method is not thread safe */
170 Writer::write (shared_ptr<const AudioBuffers> audio)
172 _sound_asset_writer->write (audio->data(), audio->frames());
175 /** This must be called from Writer::thread() with an appropriate lock held,
176 * and with _queue sorted.
179 Writer::have_sequenced_image_at_queue_head () const
181 if (_queue.empty ()) {
185 /* The queue should contain only EYES_LEFT/EYES_RIGHT pairs or EYES_BOTH */
187 if (_queue.front().eyes == EYES_BOTH) {
189 return _queue.front().frame == (_last_written_frame + 1);
194 if (_last_written_eyes == EYES_LEFT && _queue.front().frame == _last_written_frame && _queue.front().eyes == EYES_RIGHT) {
198 if (_last_written_eyes == EYES_RIGHT && _queue.front().frame == (_last_written_frame + 1) && _queue.front().eyes == EYES_LEFT) {
211 boost::mutex::scoped_lock lock (_mutex);
217 if (_finish || _queued_full_in_memory > _maximum_frames_in_memory || have_sequenced_image_at_queue_head ()) {
221 TIMING (N_("writer sleeps with a queue of %1"), _queue.size());
222 _condition.wait (lock);
223 TIMING (N_("writer wakes with a queue of %1"), _queue.size());
226 if (_finish && _queue.empty()) {
230 /* Write any frames that we can write; i.e. those that are in sequence */
231 while (have_sequenced_image_at_queue_head ()) {
232 QueueItem qi = _queue.front ();
234 if (qi.type == QueueItem::FULL && qi.encoded) {
235 --_queued_full_in_memory;
240 case QueueItem::FULL:
242 _film->log()->log (String::compose (N_("Writer FULL-writes %1 to MXF"), qi.frame));
244 qi.encoded.reset (new EncodedData (_film->j2c_path (qi.frame, qi.eyes, false)));
247 libdcp::FrameInfo fin = _picture_asset_writer->write (qi.encoded->data(), qi.encoded->size());
248 qi.encoded->write_info (_film, qi.frame, qi.eyes, fin);
249 _last_written[qi.eyes] = qi.encoded;
253 case QueueItem::FAKE:
254 _film->log()->log (String::compose (N_("Writer FAKE-writes %1 to MXF"), qi.frame));
255 _picture_asset_writer->fake_write (qi.size);
256 _last_written[qi.eyes].reset ();
259 case QueueItem::REPEAT:
261 _film->log()->log (String::compose (N_("Writer REPEAT-writes %1 to MXF"), qi.frame));
262 libdcp::FrameInfo fin = _picture_asset_writer->write (
263 _last_written[qi.eyes]->data(),
264 _last_written[qi.eyes]->size()
267 _last_written[qi.eyes]->write_info (_film, qi.frame, qi.eyes, fin);
274 _last_written_frame = qi.frame;
275 _last_written_eyes = qi.eyes;
277 if (_film->length()) {
278 shared_ptr<Job> job = _job.lock ();
280 int total = _film->time_to_video_frames (_film->length ());
281 if (_film->three_d ()) {
282 /* _full_written and so on are incremented for each eye, so we need to double the total
283 frames to get the correct progress.
287 job->set_progress (float (_full_written + _fake_written + _repeat_written) / total);
291 while (_queued_full_in_memory > _maximum_frames_in_memory) {
292 /* Too many frames in memory which can't yet be written to the stream.
293 Write some FULL frames to disk.
297 list<QueueItem>::reverse_iterator i = _queue.rbegin ();
298 while (i != _queue.rend() && (i->type != QueueItem::FULL || !i->encoded)) {
302 assert (i != _queue.rend());
311 "Writer full (awaiting %1 [last eye was %2]); pushes %3 to disk",
312 _last_written_frame + 1,
313 _last_written_eyes, qi.frame)
316 qi.encoded->write (_film, qi.frame, qi.eyes);
319 --_queued_full_in_memory;
335 boost::mutex::scoped_lock lock (_mutex);
337 _condition.notify_all ();
346 _picture_asset_writer->finalize ();
347 _sound_asset_writer->finalize ();
349 int const frames = _last_written_frame + 1;
351 _picture_asset->set_duration (frames);
353 /* Hard-link the video MXF into the DCP */
354 boost::filesystem::path video_from;
355 video_from /= _film->internal_video_mxf_dir();
356 video_from /= _film->internal_video_mxf_filename();
358 boost::filesystem::path video_to;
359 video_to /= _film->dir (_film->dcp_name());
360 video_to /= _film->video_mxf_filename ();
362 boost::system::error_code ec;
363 boost::filesystem::create_hard_link (video_from, video_to, ec);
365 /* hard link failed; copy instead */
366 boost::filesystem::copy_file (video_from, video_to);
367 _film->log()->log ("Hard-link failed; fell back to copying");
370 /* And update the asset */
372 _picture_asset->set_directory (_film->dir (_film->dcp_name ()));
373 _picture_asset->set_file_name (_film->video_mxf_filename ());
375 /* Move the audio MXF into the DCP */
377 boost::filesystem::path audio_from;
378 audio_from /= _film->dir (".");
379 audio_from /= _film->audio_mxf_filename ();
381 boost::filesystem::path audio_to;
382 audio_to /= _film->dir (_film->dcp_name ());
383 audio_to /= _film->audio_mxf_filename ();
385 boost::filesystem::rename (audio_from, audio_to, ec);
387 throw FileError (String::compose (_("could not move audio MXF into the DCP (%1)"), ec.value ()), audio_from);
390 _sound_asset->set_directory (_film->dir (_film->dcp_name ()));
391 _sound_asset->set_duration (frames);
393 libdcp::DCP dcp (_film->dir (_film->dcp_name()));
395 shared_ptr<libdcp::CPL> cpl (
397 _film->dir (_film->dcp_name()),
399 _film->dcp_content_type()->libdcp_kind (),
401 _film->video_frame_rate ()
407 cpl->add_reel (shared_ptr<libdcp::Reel> (new libdcp::Reel (
410 shared_ptr<libdcp::SubtitleAsset> ()
414 shared_ptr<Job> job = _job.lock ();
417 job->sub (_("Computing image digest"));
418 _picture_asset->compute_digest (boost::bind (&Job::set_progress, job.get(), _1, false));
420 job->sub (_("Computing audio digest"));
421 _sound_asset->compute_digest (boost::bind (&Job::set_progress, job.get(), _1, false));
423 libdcp::XMLMetadata meta = Config::instance()->dcp_metadata ();
424 meta.set_issue_date_now ();
425 dcp.write_xml (_film->interop (), meta, _film->is_signed() ? make_signer () : shared_ptr<const libdcp::Signer> ());
427 _film->log()->log (String::compose (N_("Wrote %1 FULL, %2 FAKE, %3 REPEAT; %4 pushed to disk"), _full_written, _fake_written, _repeat_written, _pushed_to_disk));
430 /** Tell the writer that frame `f' should be a repeat of the frame before it */
432 Writer::repeat (int f, Eyes e)
434 boost::mutex::scoped_lock lock (_mutex);
437 qi.type = QueueItem::REPEAT;
439 if (_film->three_d() && e == EYES_BOTH) {
441 _queue.push_back (qi);
442 qi.eyes = EYES_RIGHT;
443 _queue.push_back (qi);
446 _queue.push_back (qi);
449 _condition.notify_all ();
453 Writer::check_existing_picture_mxf_frame (FILE* mxf, int f, Eyes eyes)
455 /* Read the frame info as written */
456 FILE* ifi = fopen_boost (_film->info_path (f, eyes), "r");
458 _film->log()->log (String::compose ("Existing frame %1 has no info file", f));
462 libdcp::FrameInfo info (ifi);
464 if (info.size == 0) {
465 _film->log()->log (String::compose ("Existing frame %1 has no info file", f));
469 /* Read the data from the MXF and hash it */
470 dcpomatic_fseek (mxf, info.offset, SEEK_SET);
471 EncodedData data (info.size);
472 size_t const read = fread (data.data(), 1, data.size(), mxf);
473 if (read != static_cast<size_t> (data.size ())) {
474 _film->log()->log (String::compose ("Existing frame %1 is incomplete", f));
478 string const existing_hash = md5_digest (data.data(), data.size());
479 if (existing_hash != info.hash) {
480 _film->log()->log (String::compose ("Existing frame %1 failed hash check", f));
488 Writer::check_existing_picture_mxf ()
490 /* Try to open the existing MXF */
491 boost::filesystem::path p;
492 p /= _film->internal_video_mxf_dir ();
493 p /= _film->internal_video_mxf_filename ();
494 FILE* mxf = fopen_boost (p, "rb");
496 _film->log()->log (String::compose ("Could not open existing MXF at %1 (errno=%2)", p.string(), errno));
501 for (boost::filesystem::directory_iterator i (_film->info_dir ()); i != boost::filesystem::directory_iterator (); ++i) {
507 shared_ptr<Job> job = _job.lock ();
510 job->set_progress (float (_first_nonexistant_frame) / N);
512 if (_film->three_d ()) {
513 if (!check_existing_picture_mxf_frame (mxf, _first_nonexistant_frame, EYES_LEFT)) {
516 if (!check_existing_picture_mxf_frame (mxf, _first_nonexistant_frame, EYES_RIGHT)) {
520 if (!check_existing_picture_mxf_frame (mxf, _first_nonexistant_frame, EYES_BOTH)) {
525 _film->log()->log (String::compose ("Have existing frame %1", _first_nonexistant_frame));
526 ++_first_nonexistant_frame;
532 /** @param frame Frame index.
533 * @return true if we can fake-write this frame.
536 Writer::can_fake_write (int frame) const
538 /* We have to do a proper write of the first frame so that we can set up the JPEG2000
539 parameters in the MXF writer.
541 return (frame != 0 && frame < _first_nonexistant_frame);
545 operator< (QueueItem const & a, QueueItem const & b)
547 if (a.frame != b.frame) {
548 return a.frame < b.frame;
551 return static_cast<int> (a.eyes) < static_cast<int> (b.eyes);
555 operator== (QueueItem const & a, QueueItem const & b)
557 return a.frame == b.frame && a.eyes == b.eyes;