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 _sound_asset.reset (new libdcp::SoundAsset (_film->dir (_film->dcp_name()), _film->audio_mxf_filename ()));
97 _sound_asset->set_edit_rate (_film->video_frame_rate ());
98 _sound_asset->set_channels (_film->audio_channels ());
99 _sound_asset->set_sampling_rate (_film->audio_frame_rate ());
101 if (_film->encrypted ()) {
102 _sound_asset->set_key (_film->key ());
105 _sound_asset_writer = _sound_asset->start_write ();
107 _thread = new boost::thread (boost::bind (&Writer::thread, this));
109 job->sub (_("Encoding image data"));
113 Writer::write (shared_ptr<const EncodedData> encoded, int frame, Eyes eyes)
115 boost::mutex::scoped_lock lock (_mutex);
118 qi.type = QueueItem::FULL;
119 qi.encoded = encoded;
122 if (_film->three_d() && eyes == EYES_BOTH) {
123 /* 2D material in a 3D DCP; fake the 3D */
125 _queue.push_back (qi);
126 ++_queued_full_in_memory;
127 qi.eyes = EYES_RIGHT;
128 _queue.push_back (qi);
129 ++_queued_full_in_memory;
132 _queue.push_back (qi);
133 ++_queued_full_in_memory;
136 _condition.notify_all ();
140 Writer::fake_write (int frame, Eyes eyes)
142 boost::mutex::scoped_lock lock (_mutex);
144 FILE* ifi = fopen_boost (_film->info_path (frame, eyes), "r");
145 libdcp::FrameInfo info (ifi);
149 qi.type = QueueItem::FAKE;
152 if (_film->three_d() && eyes == EYES_BOTH) {
154 _queue.push_back (qi);
155 qi.eyes = EYES_RIGHT;
156 _queue.push_back (qi);
159 _queue.push_back (qi);
162 _condition.notify_all ();
165 /** This method is not thread safe */
167 Writer::write (shared_ptr<const AudioBuffers> audio)
169 _sound_asset_writer->write (audio->data(), audio->frames());
172 /** This must be called from Writer::thread() with an appropriate lock held,
173 * and with _queue sorted.
176 Writer::have_sequenced_image_at_queue_head () const
178 if (_queue.empty ()) {
182 /* The queue should contain only EYES_LEFT/EYES_RIGHT pairs or EYES_BOTH */
184 if (_queue.front().eyes == EYES_BOTH) {
186 return _queue.front().frame == (_last_written_frame + 1);
191 if (_last_written_eyes == EYES_LEFT && _queue.front().frame == _last_written_frame && _queue.front().eyes == EYES_RIGHT) {
195 if (_last_written_eyes == EYES_RIGHT && _queue.front().frame == (_last_written_frame + 1) && _queue.front().eyes == EYES_LEFT) {
208 boost::mutex::scoped_lock lock (_mutex);
214 if (_finish || _queued_full_in_memory > _maximum_frames_in_memory || have_sequenced_image_at_queue_head ()) {
218 TIMING (N_("writer sleeps with a queue of %1"), _queue.size());
219 _condition.wait (lock);
220 TIMING (N_("writer wakes with a queue of %1"), _queue.size());
223 if (_finish && _queue.empty()) {
227 /* Write any frames that we can write; i.e. those that are in sequence */
228 while (have_sequenced_image_at_queue_head ()) {
229 QueueItem qi = _queue.front ();
231 if (qi.type == QueueItem::FULL && qi.encoded) {
232 --_queued_full_in_memory;
237 case QueueItem::FULL:
239 _film->log()->log (String::compose (N_("Writer FULL-writes %1 to MXF"), qi.frame));
241 qi.encoded.reset (new EncodedData (_film->j2c_path (qi.frame, qi.eyes, false)));
244 libdcp::FrameInfo fin = _picture_asset_writer->write (qi.encoded->data(), qi.encoded->size());
245 qi.encoded->write_info (_film, qi.frame, qi.eyes, fin);
246 _last_written[qi.eyes] = qi.encoded;
250 case QueueItem::FAKE:
251 _film->log()->log (String::compose (N_("Writer FAKE-writes %1 to MXF"), qi.frame));
252 _picture_asset_writer->fake_write (qi.size);
253 _last_written[qi.eyes].reset ();
256 case QueueItem::REPEAT:
258 _film->log()->log (String::compose (N_("Writer REPEAT-writes %1 to MXF"), qi.frame));
259 libdcp::FrameInfo fin = _picture_asset_writer->write (
260 _last_written[qi.eyes]->data(),
261 _last_written[qi.eyes]->size()
264 _last_written[qi.eyes]->write_info (_film, qi.frame, qi.eyes, fin);
271 _last_written_frame = qi.frame;
272 _last_written_eyes = qi.eyes;
274 if (_film->length()) {
275 shared_ptr<Job> job = _job.lock ();
277 int total = _film->time_to_video_frames (_film->length ());
278 if (_film->three_d ()) {
279 /* _full_written and so on are incremented for each eye, so we need to double the total
280 frames to get the correct progress.
284 job->set_progress (float (_full_written + _fake_written + _repeat_written) / total);
288 while (_queued_full_in_memory > _maximum_frames_in_memory) {
289 /* Too many frames in memory which can't yet be written to the stream.
290 Write some FULL frames to disk.
294 list<QueueItem>::reverse_iterator i = _queue.rbegin ();
295 while (i != _queue.rend() && (i->type != QueueItem::FULL || !i->encoded)) {
299 assert (i != _queue.rend());
308 "Writer full (awaiting %1 [last eye was %2]); pushes %3 to disk",
309 _last_written_frame + 1,
310 _last_written_eyes, qi.frame)
313 qi.encoded->write (_film, qi.frame, qi.eyes);
316 --_queued_full_in_memory;
332 boost::mutex::scoped_lock lock (_mutex);
334 _condition.notify_all ();
343 _picture_asset_writer->finalize ();
344 _sound_asset_writer->finalize ();
346 int const frames = _last_written_frame + 1;
348 _picture_asset->set_duration (frames);
350 /* Hard-link the video MXF into the DCP */
352 boost::filesystem::path from;
353 from /= _film->internal_video_mxf_dir();
354 from /= _film->internal_video_mxf_filename();
356 boost::filesystem::path to;
357 to /= _film->dir (_film->dcp_name());
358 to /= _film->video_mxf_filename ();
360 boost::system::error_code ec;
361 boost::filesystem::create_hard_link (from, to, ec);
363 /* hard link failed; copy instead */
364 boost::filesystem::copy_file (from, to);
365 _film->log()->log ("Hard-link failed; fell back to copying");
368 /* And update the asset */
370 _picture_asset->set_directory (_film->dir (_film->dcp_name ()));
371 _picture_asset->set_file_name (_film->video_mxf_filename ());
372 _sound_asset->set_duration (frames);
374 libdcp::DCP dcp (_film->dir (_film->dcp_name()));
376 shared_ptr<libdcp::CPL> cpl (
378 _film->dir (_film->dcp_name()),
380 _film->dcp_content_type()->libdcp_kind (),
382 _film->video_frame_rate ()
388 cpl->add_reel (shared_ptr<libdcp::Reel> (new libdcp::Reel (
391 shared_ptr<libdcp::SubtitleAsset> ()
395 shared_ptr<Job> job = _job.lock ();
398 job->sub (_("Computing image digest"));
399 _picture_asset->compute_digest (boost::bind (&Job::set_progress, job.get(), _1, false));
401 job->sub (_("Computing audio digest"));
402 _sound_asset->compute_digest (boost::bind (&Job::set_progress, job.get(), _1, false));
404 libdcp::XMLMetadata meta = Config::instance()->dcp_metadata ();
405 meta.set_issue_date_now ();
406 dcp.write_xml (_film->interop (), meta, _film->is_signed() ? make_signer () : shared_ptr<const libdcp::Signer> ());
408 _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));
411 /** Tell the writer that frame `f' should be a repeat of the frame before it */
413 Writer::repeat (int f, Eyes e)
415 boost::mutex::scoped_lock lock (_mutex);
418 qi.type = QueueItem::REPEAT;
420 if (_film->three_d() && e == EYES_BOTH) {
422 _queue.push_back (qi);
423 qi.eyes = EYES_RIGHT;
424 _queue.push_back (qi);
427 _queue.push_back (qi);
430 _condition.notify_all ();
434 Writer::check_existing_picture_mxf_frame (FILE* mxf, int f, Eyes eyes)
436 /* Read the frame info as written */
437 FILE* ifi = fopen_boost (_film->info_path (f, eyes), "r");
439 _film->log()->log (String::compose ("Existing frame %1 has no info file", f));
443 libdcp::FrameInfo info (ifi);
445 if (info.size == 0) {
446 _film->log()->log (String::compose ("Existing frame %1 has no info file", f));
450 /* Read the data from the MXF and hash it */
451 #ifdef DCPOMATIC_WINDOWS
452 _fseeki64 (mxf, info.offset, SEEK_SET);
454 fseek (mxf, info.offset, SEEK_SET);
456 EncodedData data (info.size);
457 size_t const read = fread (data.data(), 1, data.size(), mxf);
458 if (read != static_cast<size_t> (data.size ())) {
459 _film->log()->log (String::compose ("Existing frame %1 is incomplete", f));
463 string const existing_hash = md5_digest (data.data(), data.size());
464 if (existing_hash != info.hash) {
465 _film->log()->log (String::compose ("Existing frame %1 failed hash check", f));
473 Writer::check_existing_picture_mxf ()
475 /* Try to open the existing MXF */
476 boost::filesystem::path p;
477 p /= _film->internal_video_mxf_dir ();
478 p /= _film->internal_video_mxf_filename ();
479 FILE* mxf = fopen_boost (p, "rb");
481 _film->log()->log (String::compose ("Could not open existing MXF at %1 (errno=%2)", p.string(), errno));
486 for (boost::filesystem::directory_iterator i (_film->info_dir ()); i != boost::filesystem::directory_iterator (); ++i) {
492 shared_ptr<Job> job = _job.lock ();
495 job->set_progress (float (_first_nonexistant_frame) / N);
497 if (_film->three_d ()) {
498 if (!check_existing_picture_mxf_frame (mxf, _first_nonexistant_frame, EYES_LEFT)) {
501 if (!check_existing_picture_mxf_frame (mxf, _first_nonexistant_frame, EYES_RIGHT)) {
505 if (!check_existing_picture_mxf_frame (mxf, _first_nonexistant_frame, EYES_BOTH)) {
510 _film->log()->log (String::compose ("Have existing frame %1", _first_nonexistant_frame));
511 ++_first_nonexistant_frame;
517 /** @param frame Frame index.
518 * @return true if we can fake-write this frame.
521 Writer::can_fake_write (int frame) const
523 /* We have to do a proper write of the first frame so that we can set up the JPEG2000
524 parameters in the MXF writer.
526 return (frame != 0 && frame < _first_nonexistant_frame);
530 operator< (QueueItem const & a, QueueItem const & b)
532 if (a.frame != b.frame) {
533 return a.frame < b.frame;
536 return static_cast<int> (a.eyes) < static_cast<int> (b.eyes);
540 operator== (QueueItem const & a, QueueItem const & b)
542 return a.frame == b.frame && a.eyes == b.eyes;