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 std::stringstream;
49 using boost::shared_ptr;
50 using boost::weak_ptr;
52 int const Writer::_maximum_frames_in_memory = Config::instance()->num_local_encoding_threads() + 4;
54 Writer::Writer (shared_ptr<const Film> f, weak_ptr<Job> j)
57 , _first_nonexistant_frame (0)
60 , _queued_full_in_memory (0)
61 , _last_written_frame (-1)
62 , _last_written_eyes (EYES_RIGHT)
68 /* Remove any old DCP */
69 boost::filesystem::remove_all (_film->dir (_film->dcp_name ()));
71 shared_ptr<Job> job = _job.lock ();
74 job->sub (_("Checking existing image data"));
75 check_existing_picture_mxf ();
77 /* Create our picture asset in a subdirectory, named according to those
78 film's parameters which affect the video output. We will hard-link
79 it into the DCP later.
82 if (_film->three_d ()) {
83 _picture_asset.reset (new libdcp::StereoPictureAsset (_film->internal_video_mxf_dir (), _film->internal_video_mxf_filename ()));
85 _picture_asset.reset (new libdcp::MonoPictureAsset (_film->internal_video_mxf_dir (), _film->internal_video_mxf_filename ()));
88 _picture_asset->set_edit_rate (_film->video_frame_rate ());
89 _picture_asset->set_size (fit_ratio_within (_film->container()->ratio(), _film->full_frame ()));
90 _picture_asset->set_interop (_film->interop ());
92 if (_film->encrypted ()) {
93 _picture_asset->set_key (_film->key ());
96 _picture_asset_writer = _picture_asset->start_write (_first_nonexistant_frame > 0);
98 _sound_asset.reset (new libdcp::SoundAsset (_film->directory (), _film->audio_mxf_filename ()));
99 _sound_asset->set_edit_rate (_film->video_frame_rate ());
100 _sound_asset->set_channels (_film->audio_channels ());
101 _sound_asset->set_sampling_rate (_film->audio_frame_rate ());
102 _sound_asset->set_interop (_film->interop ());
104 if (_film->encrypted ()) {
105 _sound_asset->set_key (_film->key ());
108 /* Write the sound asset into the film directory so that we leave the creation
109 of the DCP directory until the last minute.
111 _sound_asset_writer = _sound_asset->start_write ();
113 _thread = new boost::thread (boost::bind (&Writer::thread, this));
115 job->sub (_("Encoding image data"));
120 terminate_thread (false);
124 Writer::write (shared_ptr<const EncodedData> encoded, int frame, Eyes eyes)
126 boost::mutex::scoped_lock lock (_mutex);
128 while (_queued_full_in_memory > _maximum_frames_in_memory) {
129 _full_condition.wait (lock);
133 qi.type = QueueItem::FULL;
134 qi.encoded = encoded;
137 if (_film->three_d() && eyes == EYES_BOTH) {
138 /* 2D material in a 3D DCP; fake the 3D */
140 _queue.push_back (qi);
141 ++_queued_full_in_memory;
142 qi.eyes = EYES_RIGHT;
143 _queue.push_back (qi);
144 ++_queued_full_in_memory;
147 _queue.push_back (qi);
148 ++_queued_full_in_memory;
151 _empty_condition.notify_all ();
155 Writer::fake_write (int frame, Eyes eyes)
157 boost::mutex::scoped_lock lock (_mutex);
159 while (_queued_full_in_memory > _maximum_frames_in_memory) {
160 _full_condition.wait (lock);
163 FILE* ifi = fopen_boost (_film->info_path (frame, eyes), "r");
164 libdcp::FrameInfo info (ifi);
168 qi.type = QueueItem::FAKE;
171 if (_film->three_d() && eyes == EYES_BOTH) {
173 _queue.push_back (qi);
174 qi.eyes = EYES_RIGHT;
175 _queue.push_back (qi);
178 _queue.push_back (qi);
181 _empty_condition.notify_all ();
184 /** This method is not thread safe */
186 Writer::write (shared_ptr<const AudioBuffers> audio)
188 _sound_asset_writer->write (audio->data(), audio->frames());
191 /** This must be called from Writer::thread() with an appropriate lock held */
193 Writer::have_sequenced_image_at_queue_head ()
195 if (_queue.empty ()) {
201 /* The queue should contain only EYES_LEFT/EYES_RIGHT pairs or EYES_BOTH */
203 if (_queue.front().eyes == EYES_BOTH) {
205 return _queue.front().frame == (_last_written_frame + 1);
210 if (_last_written_eyes == EYES_LEFT && _queue.front().frame == _last_written_frame && _queue.front().eyes == EYES_RIGHT) {
214 if (_last_written_eyes == EYES_RIGHT && _queue.front().frame == (_last_written_frame + 1) && _queue.front().eyes == EYES_LEFT) {
227 boost::mutex::scoped_lock lock (_mutex);
231 if (_finish || _queued_full_in_memory > _maximum_frames_in_memory || have_sequenced_image_at_queue_head ()) {
235 TIMING (N_("writer sleeps with a queue of %1"), _queue.size());
236 _empty_condition.wait (lock);
237 TIMING (N_("writer wakes with a queue of %1"), _queue.size());
240 if (_finish && _queue.empty()) {
244 /* Write any frames that we can write; i.e. those that are in sequence. */
245 while (have_sequenced_image_at_queue_head ()) {
246 QueueItem qi = _queue.front ();
248 if (qi.type == QueueItem::FULL && qi.encoded) {
249 --_queued_full_in_memory;
254 case QueueItem::FULL:
256 _film->log()->log (String::compose (N_("Writer FULL-writes %1 to MXF"), qi.frame));
258 qi.encoded.reset (new EncodedData (_film->j2c_path (qi.frame, qi.eyes, false)));
261 libdcp::FrameInfo fin = _picture_asset_writer->write (qi.encoded->data(), qi.encoded->size());
262 qi.encoded->write_info (_film, qi.frame, qi.eyes, fin);
263 _last_written[qi.eyes] = qi.encoded;
267 case QueueItem::FAKE:
268 _film->log()->log (String::compose (N_("Writer FAKE-writes %1 to MXF"), qi.frame));
269 _picture_asset_writer->fake_write (qi.size);
270 _last_written[qi.eyes].reset ();
273 case QueueItem::REPEAT:
275 _film->log()->log (String::compose (N_("Writer REPEAT-writes %1 to MXF"), qi.frame));
276 libdcp::FrameInfo fin = _picture_asset_writer->write (
277 _last_written[qi.eyes]->data(),
278 _last_written[qi.eyes]->size()
281 _last_written[qi.eyes]->write_info (_film, qi.frame, qi.eyes, fin);
288 _last_written_frame = qi.frame;
289 _last_written_eyes = qi.eyes;
291 if (_film->length()) {
292 shared_ptr<Job> job = _job.lock ();
294 int total = _film->time_to_video_frames (_film->length ());
295 if (_film->three_d ()) {
296 /* _full_written and so on are incremented for each eye, so we need to double the total
297 frames to get the correct progress.
301 job->set_progress (float (_full_written + _fake_written + _repeat_written) / total);
305 while (_queued_full_in_memory > _maximum_frames_in_memory) {
306 /* Too many frames in memory which can't yet be written to the stream.
307 Write some FULL frames to disk.
310 /* Find one from the back of the queue */
312 list<QueueItem>::reverse_iterator i = _queue.rbegin ();
313 while (i != _queue.rend() && (i->type != QueueItem::FULL || !i->encoded)) {
317 assert (i != _queue.rend());
326 "Writer full (awaiting %1 [last eye was %2]); pushes %3 to disk",
327 _last_written_frame + 1,
328 _last_written_eyes, qi.frame)
331 qi.encoded->write (_film, qi.frame, qi.eyes);
334 --_queued_full_in_memory;
337 _full_condition.notify_all ();
346 Writer::terminate_thread (bool can_throw)
348 boost::mutex::scoped_lock lock (_mutex);
354 _empty_condition.notify_all ();
355 _full_condition.notify_all ();
374 terminate_thread (true);
376 _picture_asset_writer->finalize ();
377 _sound_asset_writer->finalize ();
379 int const frames = _last_written_frame + 1;
381 _picture_asset->set_duration (frames);
383 /* Hard-link the video MXF into the DCP */
384 boost::filesystem::path video_from;
385 video_from /= _film->internal_video_mxf_dir();
386 video_from /= _film->internal_video_mxf_filename();
388 boost::filesystem::path video_to;
389 video_to /= _film->dir (_film->dcp_name());
390 video_to /= _film->video_mxf_filename ();
392 boost::system::error_code ec;
393 boost::filesystem::create_hard_link (video_from, video_to, ec);
395 /* hard link failed; copy instead */
396 boost::filesystem::copy_file (video_from, video_to);
397 _film->log()->log ("Hard-link failed; fell back to copying");
400 /* And update the asset */
402 _picture_asset->set_directory (_film->dir (_film->dcp_name ()));
403 _picture_asset->set_file_name (_film->video_mxf_filename ());
405 /* Move the audio MXF into the DCP */
407 boost::filesystem::path audio_to;
408 audio_to /= _film->dir (_film->dcp_name ());
409 audio_to /= _film->audio_mxf_filename ();
411 boost::filesystem::rename (_film->file (_film->audio_mxf_filename ()), audio_to, ec);
414 String::compose (_("could not move audio MXF into the DCP (%1)"), ec.value ()), _film->file (_film->audio_mxf_filename ())
418 _sound_asset->set_directory (_film->dir (_film->dcp_name ()));
419 _sound_asset->set_duration (frames);
421 libdcp::DCP dcp (_film->dir (_film->dcp_name()));
423 shared_ptr<libdcp::CPL> cpl (
425 _film->dir (_film->dcp_name()),
427 _film->dcp_content_type()->libdcp_kind (),
429 _film->video_frame_rate ()
435 cpl->add_reel (shared_ptr<libdcp::Reel> (new libdcp::Reel (
438 shared_ptr<libdcp::SubtitleAsset> ()
442 shared_ptr<Job> job = _job.lock ();
445 job->sub (_("Computing image digest"));
446 _picture_asset->compute_digest (boost::bind (&Job::set_progress, job.get(), _1, false));
448 job->sub (_("Computing audio digest"));
449 _sound_asset->compute_digest (boost::bind (&Job::set_progress, job.get(), _1, false));
451 libdcp::XMLMetadata meta = Config::instance()->dcp_metadata ();
452 meta.set_issue_date_now ();
453 dcp.write_xml (_film->interop (), meta, _film->is_signed() ? make_signer () : shared_ptr<const libdcp::Signer> ());
456 String::compose (N_("Wrote %1 FULL, %2 FAKE, %3 REPEAT; %4 pushed to disk"), _full_written, _fake_written, _repeat_written, _pushed_to_disk)
460 /** Tell the writer that frame `f' should be a repeat of the frame before it */
462 Writer::repeat (int f, Eyes e)
464 boost::mutex::scoped_lock lock (_mutex);
466 while (_queued_full_in_memory > _maximum_frames_in_memory) {
467 _full_condition.wait (lock);
471 qi.type = QueueItem::REPEAT;
473 if (_film->three_d() && e == EYES_BOTH) {
475 _queue.push_back (qi);
476 qi.eyes = EYES_RIGHT;
477 _queue.push_back (qi);
480 _queue.push_back (qi);
483 _empty_condition.notify_all ();
487 Writer::check_existing_picture_mxf_frame (FILE* mxf, int f, Eyes eyes)
489 /* Read the frame info as written */
490 FILE* ifi = fopen_boost (_film->info_path (f, eyes), "r");
492 _film->log()->log (String::compose ("Existing frame %1 has no info file", f));
496 libdcp::FrameInfo info (ifi);
498 if (info.size == 0) {
499 _film->log()->log (String::compose ("Existing frame %1 has no info file", f));
503 /* Read the data from the MXF and hash it */
504 dcpomatic_fseek (mxf, info.offset, SEEK_SET);
505 EncodedData data (info.size);
506 size_t const read = fread (data.data(), 1, data.size(), mxf);
507 if (read != static_cast<size_t> (data.size ())) {
508 _film->log()->log (String::compose ("Existing frame %1 is incomplete", f));
512 string const existing_hash = md5_digest (data.data(), data.size());
513 if (existing_hash != info.hash) {
514 _film->log()->log (String::compose ("Existing frame %1 failed hash check", f));
522 Writer::check_existing_picture_mxf ()
524 /* Try to open the existing MXF */
525 boost::filesystem::path p;
526 p /= _film->internal_video_mxf_dir ();
527 p /= _film->internal_video_mxf_filename ();
528 FILE* mxf = fopen_boost (p, "rb");
530 _film->log()->log (String::compose ("Could not open existing MXF at %1 (errno=%2)", p.string(), errno));
535 for (boost::filesystem::directory_iterator i (_film->info_dir ()); i != boost::filesystem::directory_iterator (); ++i) {
541 shared_ptr<Job> job = _job.lock ();
545 job->set_progress (float (_first_nonexistant_frame) / N);
548 if (_film->three_d ()) {
549 if (!check_existing_picture_mxf_frame (mxf, _first_nonexistant_frame, EYES_LEFT)) {
552 if (!check_existing_picture_mxf_frame (mxf, _first_nonexistant_frame, EYES_RIGHT)) {
556 if (!check_existing_picture_mxf_frame (mxf, _first_nonexistant_frame, EYES_BOTH)) {
561 _film->log()->log (String::compose ("Have existing frame %1", _first_nonexistant_frame));
562 ++_first_nonexistant_frame;
568 /** @param frame Frame index.
569 * @return true if we can fake-write this frame.
572 Writer::can_fake_write (int frame) const
574 /* We have to do a proper write of the first frame so that we can set up the JPEG2000
575 parameters in the MXF writer.
577 return (frame != 0 && frame < _first_nonexistant_frame);
581 operator< (QueueItem const & a, QueueItem const & b)
583 if (a.frame != b.frame) {
584 return a.frame < b.frame;
587 return static_cast<int> (a.eyes) < static_cast<int> (b.eyes);
591 operator== (QueueItem const & a, QueueItem const & b)
593 return a.frame == b.frame && a.eyes == b.eyes;