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"
43 /* OS X strikes again */
51 using std::stringstream;
52 using boost::shared_ptr;
53 using boost::weak_ptr;
55 int const Writer::_maximum_frames_in_memory = Config::instance()->num_local_encoding_threads() + 4;
57 Writer::Writer (shared_ptr<const Film> f, weak_ptr<Job> j)
60 , _first_nonexistant_frame (0)
63 , _queued_full_in_memory (0)
64 , _last_written_frame (-1)
65 , _last_written_eyes (EYES_RIGHT)
71 /* Remove any old DCP */
72 boost::filesystem::remove_all (_film->dir (_film->dcp_name ()));
74 shared_ptr<Job> job = _job.lock ();
77 job->sub (_("Checking existing image data"));
78 check_existing_picture_mxf ();
80 /* Create our picture asset in a subdirectory, named according to those
81 film's parameters which affect the video output. We will hard-link
82 it into the DCP later.
85 if (_film->three_d ()) {
86 _picture_asset.reset (new libdcp::StereoPictureAsset (_film->internal_video_mxf_dir (), _film->internal_video_mxf_filename ()));
88 _picture_asset.reset (new libdcp::MonoPictureAsset (_film->internal_video_mxf_dir (), _film->internal_video_mxf_filename ()));
91 _picture_asset->set_edit_rate (_film->video_frame_rate ());
92 _picture_asset->set_size (_film->frame_size ());
93 _picture_asset->set_interop (_film->interop ());
95 if (_film->encrypted ()) {
96 _picture_asset->set_key (_film->key ());
99 _picture_asset_writer = _picture_asset->start_write (_first_nonexistant_frame > 0);
101 _sound_asset.reset (new libdcp::SoundAsset (_film->directory (), _film->audio_mxf_filename ()));
102 _sound_asset->set_edit_rate (_film->video_frame_rate ());
103 _sound_asset->set_channels (_film->audio_channels ());
104 _sound_asset->set_sampling_rate (_film->audio_frame_rate ());
105 _sound_asset->set_interop (_film->interop ());
107 if (_film->encrypted ()) {
108 _sound_asset->set_key (_film->key ());
111 /* Write the sound asset into the film directory so that we leave the creation
112 of the DCP directory until the last minute.
114 _sound_asset_writer = _sound_asset->start_write ();
116 _thread = new boost::thread (boost::bind (&Writer::thread, this));
118 job->sub (_("Encoding image data"));
123 terminate_thread (false);
127 Writer::write (shared_ptr<const EncodedData> encoded, int frame, Eyes eyes)
129 boost::mutex::scoped_lock lock (_mutex);
131 while (_queued_full_in_memory > _maximum_frames_in_memory) {
132 _full_condition.wait (lock);
136 qi.type = QueueItem::FULL;
137 qi.encoded = encoded;
140 if (_film->three_d() && eyes == EYES_BOTH) {
141 /* 2D material in a 3D DCP; fake the 3D */
143 _queue.push_back (qi);
144 ++_queued_full_in_memory;
145 qi.eyes = EYES_RIGHT;
146 _queue.push_back (qi);
147 ++_queued_full_in_memory;
150 _queue.push_back (qi);
151 ++_queued_full_in_memory;
154 _empty_condition.notify_all ();
158 Writer::fake_write (int frame, Eyes eyes)
160 boost::mutex::scoped_lock lock (_mutex);
162 while (_queued_full_in_memory > _maximum_frames_in_memory) {
163 _full_condition.wait (lock);
166 FILE* ifi = fopen_boost (_film->info_path (frame, eyes), "r");
167 libdcp::FrameInfo info (ifi);
171 qi.type = QueueItem::FAKE;
174 if (_film->three_d() && eyes == EYES_BOTH) {
176 _queue.push_back (qi);
177 qi.eyes = EYES_RIGHT;
178 _queue.push_back (qi);
181 _queue.push_back (qi);
184 _empty_condition.notify_all ();
187 /** This method is not thread safe */
189 Writer::write (shared_ptr<const AudioBuffers> audio)
191 _sound_asset_writer->write (audio->data(), audio->frames());
194 /** This must be called from Writer::thread() with an appropriate lock held */
196 Writer::have_sequenced_image_at_queue_head ()
198 if (_queue.empty ()) {
204 /* The queue should contain only EYES_LEFT/EYES_RIGHT pairs or EYES_BOTH */
206 if (_queue.front().eyes == EYES_BOTH) {
208 return _queue.front().frame == (_last_written_frame + 1);
213 if (_last_written_eyes == EYES_LEFT && _queue.front().frame == _last_written_frame && _queue.front().eyes == EYES_RIGHT) {
217 if (_last_written_eyes == EYES_RIGHT && _queue.front().frame == (_last_written_frame + 1) && _queue.front().eyes == EYES_LEFT) {
230 boost::mutex::scoped_lock lock (_mutex);
234 if (_finish || _queued_full_in_memory > _maximum_frames_in_memory || have_sequenced_image_at_queue_head ()) {
238 TIMING (N_("writer sleeps with a queue of %1"), _queue.size());
239 _empty_condition.wait (lock);
240 TIMING (N_("writer wakes with a queue of %1"), _queue.size());
243 if (_finish && _queue.empty()) {
247 /* Write any frames that we can write; i.e. those that are in sequence. */
248 while (have_sequenced_image_at_queue_head ()) {
249 QueueItem qi = _queue.front ();
251 if (qi.type == QueueItem::FULL && qi.encoded) {
252 --_queued_full_in_memory;
257 case QueueItem::FULL:
259 _film->log()->log (String::compose (N_("Writer FULL-writes %1 to MXF"), qi.frame));
261 qi.encoded.reset (new EncodedData (_film->j2c_path (qi.frame, qi.eyes, false)));
264 libdcp::FrameInfo fin = _picture_asset_writer->write (qi.encoded->data(), qi.encoded->size());
265 qi.encoded->write_info (_film, qi.frame, qi.eyes, fin);
266 _last_written[qi.eyes] = qi.encoded;
270 case QueueItem::FAKE:
271 _film->log()->log (String::compose (N_("Writer FAKE-writes %1 to MXF"), qi.frame));
272 _picture_asset_writer->fake_write (qi.size);
273 _last_written[qi.eyes].reset ();
276 case QueueItem::REPEAT:
278 _film->log()->log (String::compose (N_("Writer REPEAT-writes %1 to MXF"), qi.frame));
279 libdcp::FrameInfo fin = _picture_asset_writer->write (
280 _last_written[qi.eyes]->data(),
281 _last_written[qi.eyes]->size()
284 _last_written[qi.eyes]->write_info (_film, qi.frame, qi.eyes, fin);
291 _last_written_frame = qi.frame;
292 _last_written_eyes = qi.eyes;
294 if (_film->length()) {
295 shared_ptr<Job> job = _job.lock ();
297 int total = _film->time_to_video_frames (_film->length ());
298 if (_film->three_d ()) {
299 /* _full_written and so on are incremented for each eye, so we need to double the total
300 frames to get the correct progress.
304 job->set_progress (float (_full_written + _fake_written + _repeat_written) / total);
308 while (_queued_full_in_memory > _maximum_frames_in_memory) {
309 /* Too many frames in memory which can't yet be written to the stream.
310 Write some FULL frames to disk.
313 /* Find one from the back of the queue */
315 list<QueueItem>::reverse_iterator i = _queue.rbegin ();
316 while (i != _queue.rend() && (i->type != QueueItem::FULL || !i->encoded)) {
320 assert (i != _queue.rend());
329 "Writer full (awaiting %1 [last eye was %2]); pushes %3 to disk",
330 _last_written_frame + 1,
331 _last_written_eyes, qi.frame)
334 qi.encoded->write (_film, qi.frame, qi.eyes);
337 --_queued_full_in_memory;
340 _full_condition.notify_all ();
349 Writer::terminate_thread (bool can_throw)
351 boost::mutex::scoped_lock lock (_mutex);
357 _empty_condition.notify_all ();
358 _full_condition.notify_all ();
377 terminate_thread (true);
379 _picture_asset_writer->finalize ();
380 _sound_asset_writer->finalize ();
382 int const frames = _last_written_frame + 1;
384 _picture_asset->set_duration (frames);
386 /* Hard-link the video MXF into the DCP */
387 boost::filesystem::path video_from;
388 video_from /= _film->internal_video_mxf_dir();
389 video_from /= _film->internal_video_mxf_filename();
391 boost::filesystem::path video_to;
392 video_to /= _film->dir (_film->dcp_name());
393 video_to /= _film->video_mxf_filename ();
395 boost::system::error_code ec;
396 boost::filesystem::create_hard_link (video_from, video_to, ec);
398 /* hard link failed; copy instead */
399 boost::filesystem::copy_file (video_from, video_to);
400 _film->log()->log ("Hard-link failed; fell back to copying");
403 /* And update the asset */
405 _picture_asset->set_directory (_film->dir (_film->dcp_name ()));
406 _picture_asset->set_file_name (_film->video_mxf_filename ());
408 /* Move the audio MXF into the DCP */
410 boost::filesystem::path audio_to;
411 audio_to /= _film->dir (_film->dcp_name ());
412 audio_to /= _film->audio_mxf_filename ();
414 boost::filesystem::rename (_film->file (_film->audio_mxf_filename ()), audio_to, ec);
417 String::compose (_("could not move audio MXF into the DCP (%1)"), ec.value ()), _film->file (_film->audio_mxf_filename ())
421 _sound_asset->set_directory (_film->dir (_film->dcp_name ()));
422 _sound_asset->set_duration (frames);
424 libdcp::DCP dcp (_film->dir (_film->dcp_name()));
426 shared_ptr<libdcp::CPL> cpl (
428 _film->dir (_film->dcp_name()),
430 _film->dcp_content_type()->libdcp_kind (),
432 _film->video_frame_rate ()
438 cpl->add_reel (shared_ptr<libdcp::Reel> (new libdcp::Reel (
441 shared_ptr<libdcp::SubtitleAsset> ()
445 shared_ptr<Job> job = _job.lock ();
448 job->sub (_("Computing image digest"));
449 _picture_asset->compute_digest (boost::bind (&Job::set_progress, job.get(), _1, false));
451 job->sub (_("Computing audio digest"));
452 _sound_asset->compute_digest (boost::bind (&Job::set_progress, job.get(), _1, false));
454 libdcp::XMLMetadata meta = Config::instance()->dcp_metadata ();
455 meta.set_issue_date_now ();
456 dcp.write_xml (_film->interop (), meta, _film->is_signed() ? make_signer () : shared_ptr<const libdcp::Signer> ());
459 String::compose (N_("Wrote %1 FULL, %2 FAKE, %3 REPEAT; %4 pushed to disk"), _full_written, _fake_written, _repeat_written, _pushed_to_disk)
463 /** Tell the writer that frame `f' should be a repeat of the frame before it */
465 Writer::repeat (int f, Eyes e)
467 boost::mutex::scoped_lock lock (_mutex);
469 while (_queued_full_in_memory > _maximum_frames_in_memory) {
470 _full_condition.wait (lock);
474 qi.type = QueueItem::REPEAT;
476 if (_film->three_d() && e == EYES_BOTH) {
478 _queue.push_back (qi);
479 qi.eyes = EYES_RIGHT;
480 _queue.push_back (qi);
483 _queue.push_back (qi);
486 _empty_condition.notify_all ();
490 Writer::check_existing_picture_mxf_frame (FILE* mxf, int f, Eyes eyes)
492 /* Read the frame info as written */
493 FILE* ifi = fopen_boost (_film->info_path (f, eyes), "r");
495 _film->log()->log (String::compose ("Existing frame %1 has no info file", f));
499 libdcp::FrameInfo info (ifi);
501 if (info.size == 0) {
502 _film->log()->log (String::compose ("Existing frame %1 has no info file", f));
506 /* Read the data from the MXF and hash it */
507 dcpomatic_fseek (mxf, info.offset, SEEK_SET);
508 EncodedData data (info.size);
509 size_t const read = fread (data.data(), 1, data.size(), mxf);
510 if (read != static_cast<size_t> (data.size ())) {
511 _film->log()->log (String::compose ("Existing frame %1 is incomplete", f));
515 string const existing_hash = md5_digest (data.data(), data.size());
516 if (existing_hash != info.hash) {
517 _film->log()->log (String::compose ("Existing frame %1 failed hash check", f));
525 Writer::check_existing_picture_mxf ()
527 /* Try to open the existing MXF */
528 boost::filesystem::path p;
529 p /= _film->internal_video_mxf_dir ();
530 p /= _film->internal_video_mxf_filename ();
531 FILE* mxf = fopen_boost (p, "rb");
533 _film->log()->log (String::compose ("Could not open existing MXF at %1 (errno=%2)", p.string(), errno));
538 for (boost::filesystem::directory_iterator i (_film->info_dir ()); i != boost::filesystem::directory_iterator (); ++i) {
544 shared_ptr<Job> job = _job.lock ();
548 job->set_progress (float (_first_nonexistant_frame) / N);
551 if (_film->three_d ()) {
552 if (!check_existing_picture_mxf_frame (mxf, _first_nonexistant_frame, EYES_LEFT)) {
555 if (!check_existing_picture_mxf_frame (mxf, _first_nonexistant_frame, EYES_RIGHT)) {
559 if (!check_existing_picture_mxf_frame (mxf, _first_nonexistant_frame, EYES_BOTH)) {
564 _film->log()->log (String::compose ("Have existing frame %1", _first_nonexistant_frame));
565 ++_first_nonexistant_frame;
571 /** @param frame Frame index.
572 * @return true if we can fake-write this frame.
575 Writer::can_fake_write (int frame) const
577 /* We have to do a proper write of the first frame so that we can set up the JPEG2000
578 parameters in the MXF writer.
580 return (frame != 0 && frame < _first_nonexistant_frame);
584 operator< (QueueItem const & a, QueueItem const & b)
586 if (a.frame != b.frame) {
587 return a.frame < b.frame;
590 return static_cast<int> (a.eyes) < static_cast<int> (b.eyes);
594 operator== (QueueItem const & a, QueueItem const & b)
596 return a.frame == b.frame && a.eyes == b.eyes;