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;
50 int const Writer::_maximum_frames_in_memory = 8;
52 Writer::Writer (shared_ptr<const Film> f, shared_ptr<Job> j)
55 , _first_nonexistant_frame (0)
58 , _queued_full_in_memory (0)
59 , _last_written_frame (-1)
60 , _last_written_eyes (EYES_RIGHT)
66 /* Remove any old DCP */
67 boost::filesystem::remove_all (_film->dir (_film->dcp_name ()));
69 check_existing_picture_mxf ();
71 /* Create our picture asset in a subdirectory, named according to those
72 film's parameters which affect the video output. We will hard-link
73 it into the DCP later.
76 if (_film->three_d ()) {
77 _picture_asset.reset (new libdcp::StereoPictureAsset (_film->internal_video_mxf_dir (), _film->internal_video_mxf_filename ()));
79 _picture_asset.reset (new libdcp::MonoPictureAsset (_film->internal_video_mxf_dir (), _film->internal_video_mxf_filename ()));
82 _picture_asset->set_edit_rate (_film->video_frame_rate ());
83 _picture_asset->set_size (fit_ratio_within (_film->container()->ratio(), _film->full_frame ()));
85 if (_film->encrypted ()) {
86 _picture_asset->set_key (_film->key ());
89 _picture_asset_writer = _picture_asset->start_write (_first_nonexistant_frame > 0);
91 _sound_asset.reset (new libdcp::SoundAsset (_film->dir (_film->dcp_name()), _film->audio_mxf_filename ()));
92 _sound_asset->set_edit_rate (_film->video_frame_rate ());
93 _sound_asset->set_channels (_film->audio_channels ());
94 _sound_asset->set_sampling_rate (_film->audio_frame_rate ());
96 if (_film->encrypted ()) {
97 _sound_asset->set_key (_film->key ());
100 _sound_asset_writer = _sound_asset->start_write ();
102 _thread = new boost::thread (boost::bind (&Writer::thread, this));
108 Writer::write (shared_ptr<const EncodedData> encoded, int frame, Eyes eyes)
110 boost::mutex::scoped_lock lock (_mutex);
113 qi.type = QueueItem::FULL;
114 qi.encoded = encoded;
117 if (_film->three_d() && eyes == EYES_BOTH) {
118 /* 2D material in a 3D DCP; fake the 3D */
120 _queue.push_back (qi);
121 ++_queued_full_in_memory;
122 qi.eyes = EYES_RIGHT;
123 _queue.push_back (qi);
124 ++_queued_full_in_memory;
127 _queue.push_back (qi);
128 ++_queued_full_in_memory;
131 _condition.notify_all ();
135 Writer::fake_write (int frame, Eyes eyes)
137 boost::mutex::scoped_lock lock (_mutex);
139 ifstream ifi (_film->info_path (frame, eyes).c_str());
140 libdcp::FrameInfo info (ifi);
143 qi.type = QueueItem::FAKE;
146 if (_film->three_d() && eyes == EYES_BOTH) {
148 _queue.push_back (qi);
149 qi.eyes = EYES_RIGHT;
150 _queue.push_back (qi);
153 _queue.push_back (qi);
156 _condition.notify_all ();
159 /** This method is not thread safe */
161 Writer::write (shared_ptr<const AudioBuffers> audio)
163 _sound_asset_writer->write (audio->data(), audio->frames());
166 /** This must be called from Writer::thread() with an appropriate lock held,
167 * and with _queue sorted.
170 Writer::have_sequenced_image_at_queue_head () const
172 if (_queue.empty ()) {
176 /* The queue should contain only EYES_LEFT/EYES_RIGHT pairs or EYES_BOTH */
178 if (_queue.front().eyes == EYES_BOTH) {
180 return _queue.front().frame == (_last_written_frame + 1);
185 if (_last_written_eyes == EYES_LEFT && _queue.front().frame == _last_written_frame && _queue.front().eyes == EYES_RIGHT) {
189 if (_last_written_eyes == EYES_RIGHT && _queue.front().frame == (_last_written_frame + 1) && _queue.front().eyes == EYES_LEFT) {
202 boost::mutex::scoped_lock lock (_mutex);
208 if (_finish || _queued_full_in_memory > _maximum_frames_in_memory || have_sequenced_image_at_queue_head ()) {
212 TIMING (N_("writer sleeps with a queue of %1"), _queue.size());
213 _condition.wait (lock);
214 TIMING (N_("writer wakes with a queue of %1"), _queue.size());
217 if (_finish && _queue.empty()) {
221 /* Write any frames that we can write; i.e. those that are in sequence */
222 while (have_sequenced_image_at_queue_head ()) {
223 QueueItem qi = _queue.front ();
225 if (qi.type == QueueItem::FULL && qi.encoded) {
226 --_queued_full_in_memory;
231 case QueueItem::FULL:
233 _film->log()->log (String::compose (N_("Writer FULL-writes %1 to MXF"), qi.frame));
235 qi.encoded.reset (new EncodedData (_film->j2c_path (qi.frame, qi.eyes, false)));
238 libdcp::FrameInfo fin = _picture_asset_writer->write (qi.encoded->data(), qi.encoded->size());
239 qi.encoded->write_info (_film, qi.frame, qi.eyes, fin);
240 _last_written[qi.eyes] = qi.encoded;
244 case QueueItem::FAKE:
245 _film->log()->log (String::compose (N_("Writer FAKE-writes %1 to MXF"), qi.frame));
246 _picture_asset_writer->fake_write (qi.size);
247 _last_written[qi.eyes].reset ();
250 case QueueItem::REPEAT:
252 _film->log()->log (String::compose (N_("Writer REPEAT-writes %1 to MXF"), qi.frame));
253 libdcp::FrameInfo fin = _picture_asset_writer->write (
254 _last_written[qi.eyes]->data(),
255 _last_written[qi.eyes]->size()
258 _last_written[qi.eyes]->write_info (_film, qi.frame, qi.eyes, fin);
265 _last_written_frame = qi.frame;
266 _last_written_eyes = qi.eyes;
268 if (_film->length()) {
270 float (_full_written + _fake_written + _repeat_written) / _film->time_to_video_frames (_film->length())
275 while (_queued_full_in_memory > _maximum_frames_in_memory) {
276 /* Too many frames in memory which can't yet be written to the stream.
277 Write some FULL frames to disk.
281 list<QueueItem>::reverse_iterator i = _queue.rbegin ();
282 while (i != _queue.rend() && (i->type != QueueItem::FULL || !i->encoded)) {
286 assert (i != _queue.rend());
295 "Writer full (awaiting %1 [last eye was %2]); pushes %3 to disk",
296 _last_written_frame + 1,
297 _last_written_eyes, qi.frame)
300 qi.encoded->write (_film, qi.frame, qi.eyes);
303 --_queued_full_in_memory;
319 boost::mutex::scoped_lock lock (_mutex);
321 _condition.notify_all ();
332 _picture_asset_writer->finalize ();
333 _sound_asset_writer->finalize ();
335 int const frames = _last_written_frame + 1;
337 _picture_asset->set_duration (frames);
339 /* Hard-link the video MXF into the DCP */
341 boost::filesystem::path from;
342 from /= _film->internal_video_mxf_dir();
343 from /= _film->internal_video_mxf_filename();
345 boost::filesystem::path to;
346 to /= _film->dir (_film->dcp_name());
347 to /= _film->video_mxf_filename ();
349 boost::system::error_code ec;
350 boost::filesystem::create_hard_link (from, to, ec);
352 /* hard link failed; copy instead */
353 boost::filesystem::copy_file (from, to);
354 _film->log()->log ("Hard-link failed; fell back to copying");
357 /* And update the asset */
359 _picture_asset->set_directory (_film->dir (_film->dcp_name ()));
360 _picture_asset->set_file_name (_film->video_mxf_filename ());
361 _sound_asset->set_duration (frames);
363 libdcp::DCP dcp (_film->dir (_film->dcp_name()));
365 shared_ptr<libdcp::CPL> cpl (
367 _film->dir (_film->dcp_name()),
369 _film->dcp_content_type()->libdcp_kind (),
371 _film->video_frame_rate ()
377 cpl->add_reel (shared_ptr<libdcp::Reel> (new libdcp::Reel (
380 shared_ptr<libdcp::SubtitleAsset> ()
384 /* Compute the digests for the assets now so that we can keep track of progress.
385 We did _job->descend (0.9) in our constructor */
389 _picture_asset->compute_digest (boost::bind (&Job::set_progress, _job.get(), _1));
393 _sound_asset->compute_digest (boost::bind (&Job::set_progress, _job.get(), _1));
396 libdcp::XMLMetadata meta = Config::instance()->dcp_metadata ();
397 meta.set_issue_date_now ();
398 dcp.write_xml (_film->interop (), meta, make_signer ());
400 _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));
403 /** Tell the writer that frame `f' should be a repeat of the frame before it */
405 Writer::repeat (int f, Eyes e)
407 boost::mutex::scoped_lock lock (_mutex);
410 qi.type = QueueItem::REPEAT;
412 if (_film->three_d() && e == EYES_BOTH) {
414 _queue.push_back (qi);
415 qi.eyes = EYES_RIGHT;
416 _queue.push_back (qi);
419 _queue.push_back (qi);
422 _condition.notify_all ();
426 Writer::check_existing_picture_mxf_frame (FILE* mxf, int f, Eyes eyes)
428 /* Read the frame info as written */
429 ifstream ifi (_film->info_path (f, eyes).c_str());
430 libdcp::FrameInfo info (ifi);
431 if (info.size == 0) {
432 _film->log()->log (String::compose ("Existing frame %1 has no info file", f));
436 /* Read the data from the MXF and hash it */
437 #ifdef DCPOMATIC_WINDOWS
438 _fseeki64 (mxf, info.offset, SEEK_SET);
440 fseek (mxf, info.offset, SEEK_SET);
442 EncodedData data (info.size);
443 size_t const read = fread (data.data(), 1, data.size(), mxf);
444 if (read != static_cast<size_t> (data.size ())) {
445 _film->log()->log (String::compose ("Existing frame %1 is incomplete", f));
449 string const existing_hash = md5_digest (data.data(), data.size());
450 if (existing_hash != info.hash) {
451 _film->log()->log (String::compose ("Existing frame %1 failed hash check", f));
459 Writer::check_existing_picture_mxf ()
461 /* Try to open the existing MXF */
462 boost::filesystem::path p;
463 p /= _film->internal_video_mxf_dir ();
464 p /= _film->internal_video_mxf_filename ();
465 FILE* mxf = fopen (p.string().c_str(), "rb");
467 _film->log()->log (String::compose ("Could not open existing MXF at %1 (errno=%2)", p.string(), errno));
473 if (_film->three_d ()) {
474 if (!check_existing_picture_mxf_frame (mxf, _first_nonexistant_frame, EYES_LEFT)) {
477 if (!check_existing_picture_mxf_frame (mxf, _first_nonexistant_frame, EYES_RIGHT)) {
481 if (!check_existing_picture_mxf_frame (mxf, _first_nonexistant_frame, EYES_BOTH)) {
486 _film->log()->log (String::compose ("Have existing frame %1", _first_nonexistant_frame));
487 ++_first_nonexistant_frame;
493 /** @param frame Frame index.
494 * @return true if we can fake-write this frame.
497 Writer::can_fake_write (int frame) const
499 /* We have to do a proper write of the first frame so that we can set up the JPEG2000
500 parameters in the MXF writer.
502 return (frame != 0 && frame < _first_nonexistant_frame);
506 operator< (QueueItem const & a, QueueItem const & b)
508 if (a.frame != b.frame) {
509 return a.frame < b.frame;
512 return static_cast<int> (a.eyes) < static_cast<int> (b.eyes);
516 operator== (QueueItem const & a, QueueItem const & b)
518 return a.frame == b.frame && a.eyes == b.eyes;