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 ()));
91 if (_film->encrypted ()) {
92 _picture_asset->set_key (_film->key ());
95 _picture_asset_writer = _picture_asset->start_write (_first_nonexistant_frame > 0);
97 _sound_asset.reset (new libdcp::SoundAsset (_film->directory (), _film->audio_mxf_filename ()));
98 _sound_asset->set_edit_rate (_film->video_frame_rate ());
99 _sound_asset->set_channels (_film->audio_channels ());
100 _sound_asset->set_sampling_rate (_film->audio_frame_rate ());
102 if (_film->encrypted ()) {
103 _sound_asset->set_key (_film->key ());
106 /* Write the sound asset into the film directory so that we leave the creation
107 of the DCP directory until the last minute.
109 _sound_asset_writer = _sound_asset->start_write ();
111 _thread = new boost::thread (boost::bind (&Writer::thread, this));
113 job->sub (_("Encoding image data"));
118 terminate_thread (false);
122 Writer::write (shared_ptr<const EncodedData> encoded, int frame, Eyes eyes)
124 boost::mutex::scoped_lock lock (_mutex);
126 while (_queued_full_in_memory > _maximum_frames_in_memory) {
127 _full_condition.wait (lock);
131 qi.type = QueueItem::FULL;
132 qi.encoded = encoded;
135 if (_film->three_d() && eyes == EYES_BOTH) {
136 /* 2D material in a 3D DCP; fake the 3D */
138 _queue.push_back (qi);
139 ++_queued_full_in_memory;
140 qi.eyes = EYES_RIGHT;
141 _queue.push_back (qi);
142 ++_queued_full_in_memory;
145 _queue.push_back (qi);
146 ++_queued_full_in_memory;
149 _empty_condition.notify_all ();
153 Writer::fake_write (int frame, Eyes eyes)
155 boost::mutex::scoped_lock lock (_mutex);
157 while (_queued_full_in_memory > _maximum_frames_in_memory) {
158 _full_condition.wait (lock);
161 FILE* ifi = fopen_boost (_film->info_path (frame, eyes), "r");
162 libdcp::FrameInfo info (ifi);
166 qi.type = QueueItem::FAKE;
169 if (_film->three_d() && eyes == EYES_BOTH) {
171 _queue.push_back (qi);
172 qi.eyes = EYES_RIGHT;
173 _queue.push_back (qi);
176 _queue.push_back (qi);
179 _empty_condition.notify_all ();
182 /** This method is not thread safe */
184 Writer::write (shared_ptr<const AudioBuffers> audio)
186 _sound_asset_writer->write (audio->data(), audio->frames());
189 /** This must be called from Writer::thread() with an appropriate lock held */
191 Writer::have_sequenced_image_at_queue_head ()
193 if (_queue.empty ()) {
199 /* The queue should contain only EYES_LEFT/EYES_RIGHT pairs or EYES_BOTH */
201 if (_queue.front().eyes == EYES_BOTH) {
203 return _queue.front().frame == (_last_written_frame + 1);
208 if (_last_written_eyes == EYES_LEFT && _queue.front().frame == _last_written_frame && _queue.front().eyes == EYES_RIGHT) {
212 if (_last_written_eyes == EYES_RIGHT && _queue.front().frame == (_last_written_frame + 1) && _queue.front().eyes == EYES_LEFT) {
225 boost::mutex::scoped_lock lock (_mutex);
229 if (_finish || _queued_full_in_memory > _maximum_frames_in_memory || have_sequenced_image_at_queue_head ()) {
233 TIMING (N_("writer sleeps with a queue of %1"), _queue.size());
234 _empty_condition.wait (lock);
235 TIMING (N_("writer wakes with a queue of %1"), _queue.size());
238 if (_finish && _queue.empty()) {
242 /* Write any frames that we can write; i.e. those that are in sequence. */
243 while (have_sequenced_image_at_queue_head ()) {
244 QueueItem qi = _queue.front ();
246 if (qi.type == QueueItem::FULL && qi.encoded) {
247 --_queued_full_in_memory;
252 case QueueItem::FULL:
254 _film->log()->log (String::compose (N_("Writer FULL-writes %1 to MXF"), qi.frame));
256 qi.encoded.reset (new EncodedData (_film->j2c_path (qi.frame, qi.eyes, false)));
259 libdcp::FrameInfo fin = _picture_asset_writer->write (qi.encoded->data(), qi.encoded->size());
260 qi.encoded->write_info (_film, qi.frame, qi.eyes, fin);
261 _last_written[qi.eyes] = qi.encoded;
265 case QueueItem::FAKE:
266 _film->log()->log (String::compose (N_("Writer FAKE-writes %1 to MXF"), qi.frame));
267 _picture_asset_writer->fake_write (qi.size);
268 _last_written[qi.eyes].reset ();
271 case QueueItem::REPEAT:
273 _film->log()->log (String::compose (N_("Writer REPEAT-writes %1 to MXF"), qi.frame));
274 libdcp::FrameInfo fin = _picture_asset_writer->write (
275 _last_written[qi.eyes]->data(),
276 _last_written[qi.eyes]->size()
279 _last_written[qi.eyes]->write_info (_film, qi.frame, qi.eyes, fin);
286 _last_written_frame = qi.frame;
287 _last_written_eyes = qi.eyes;
289 if (_film->length()) {
290 shared_ptr<Job> job = _job.lock ();
292 int total = _film->time_to_video_frames (_film->length ());
293 if (_film->three_d ()) {
294 /* _full_written and so on are incremented for each eye, so we need to double the total
295 frames to get the correct progress.
299 job->set_progress (float (_full_written + _fake_written + _repeat_written) / total);
303 while (_queued_full_in_memory > _maximum_frames_in_memory) {
304 /* Too many frames in memory which can't yet be written to the stream.
305 Write some FULL frames to disk.
308 /* Find one from the back of the queue */
310 list<QueueItem>::reverse_iterator i = _queue.rbegin ();
311 while (i != _queue.rend() && (i->type != QueueItem::FULL || !i->encoded)) {
315 assert (i != _queue.rend());
324 "Writer full (awaiting %1 [last eye was %2]); pushes %3 to disk",
325 _last_written_frame + 1,
326 _last_written_eyes, qi.frame)
329 qi.encoded->write (_film, qi.frame, qi.eyes);
332 --_queued_full_in_memory;
335 _full_condition.notify_all ();
344 Writer::terminate_thread (bool can_throw)
346 boost::mutex::scoped_lock lock (_mutex);
352 _empty_condition.notify_all ();
353 _full_condition.notify_all ();
372 terminate_thread (true);
374 _picture_asset_writer->finalize ();
375 _sound_asset_writer->finalize ();
377 int const frames = _last_written_frame + 1;
379 _picture_asset->set_duration (frames);
381 /* Hard-link the video MXF into the DCP */
382 boost::filesystem::path video_from;
383 video_from /= _film->internal_video_mxf_dir();
384 video_from /= _film->internal_video_mxf_filename();
386 boost::filesystem::path video_to;
387 video_to /= _film->dir (_film->dcp_name());
388 video_to /= _film->video_mxf_filename ();
390 boost::system::error_code ec;
391 boost::filesystem::create_hard_link (video_from, video_to, ec);
393 /* hard link failed; copy instead */
394 boost::filesystem::copy_file (video_from, video_to);
395 _film->log()->log ("Hard-link failed; fell back to copying");
398 /* And update the asset */
400 _picture_asset->set_directory (_film->dir (_film->dcp_name ()));
401 _picture_asset->set_file_name (_film->video_mxf_filename ());
403 /* Move the audio MXF into the DCP */
405 boost::filesystem::path audio_to;
406 audio_to /= _film->dir (_film->dcp_name ());
407 audio_to /= _film->audio_mxf_filename ();
409 boost::filesystem::rename (_film->file (_film->audio_mxf_filename ()), audio_to, ec);
412 String::compose (_("could not move audio MXF into the DCP (%1)"), ec.value ()), _film->file (_film->audio_mxf_filename ())
416 _sound_asset->set_directory (_film->dir (_film->dcp_name ()));
417 _sound_asset->set_duration (frames);
419 libdcp::DCP dcp (_film->dir (_film->dcp_name()));
421 shared_ptr<libdcp::CPL> cpl (
423 _film->dir (_film->dcp_name()),
425 _film->dcp_content_type()->libdcp_kind (),
427 _film->video_frame_rate ()
433 cpl->add_reel (shared_ptr<libdcp::Reel> (new libdcp::Reel (
436 shared_ptr<libdcp::SubtitleAsset> ()
440 shared_ptr<Job> job = _job.lock ();
443 job->sub (_("Computing image digest"));
444 _picture_asset->compute_digest (boost::bind (&Job::set_progress, job.get(), _1, false));
446 job->sub (_("Computing audio digest"));
447 _sound_asset->compute_digest (boost::bind (&Job::set_progress, job.get(), _1, false));
449 libdcp::XMLMetadata meta = Config::instance()->dcp_metadata ();
450 meta.set_issue_date_now ();
451 dcp.write_xml (_film->interop (), meta, _film->is_signed() ? make_signer () : shared_ptr<const libdcp::Signer> ());
454 String::compose (N_("Wrote %1 FULL, %2 FAKE, %3 REPEAT; %4 pushed to disk"), _full_written, _fake_written, _repeat_written, _pushed_to_disk)
458 /** Tell the writer that frame `f' should be a repeat of the frame before it */
460 Writer::repeat (int f, Eyes e)
462 boost::mutex::scoped_lock lock (_mutex);
464 while (_queued_full_in_memory > _maximum_frames_in_memory) {
465 _full_condition.wait (lock);
469 qi.type = QueueItem::REPEAT;
471 if (_film->three_d() && e == EYES_BOTH) {
473 _queue.push_back (qi);
474 qi.eyes = EYES_RIGHT;
475 _queue.push_back (qi);
478 _queue.push_back (qi);
481 _empty_condition.notify_all ();
485 Writer::check_existing_picture_mxf_frame (FILE* mxf, int f, Eyes eyes)
487 /* Read the frame info as written */
488 FILE* ifi = fopen_boost (_film->info_path (f, eyes), "r");
490 _film->log()->log (String::compose ("Existing frame %1 has no info file", f));
494 libdcp::FrameInfo info (ifi);
496 if (info.size == 0) {
497 _film->log()->log (String::compose ("Existing frame %1 has no info file", f));
501 /* Read the data from the MXF and hash it */
502 dcpomatic_fseek (mxf, info.offset, SEEK_SET);
503 EncodedData data (info.size);
504 size_t const read = fread (data.data(), 1, data.size(), mxf);
505 if (read != static_cast<size_t> (data.size ())) {
506 _film->log()->log (String::compose ("Existing frame %1 is incomplete", f));
510 string const existing_hash = md5_digest (data.data(), data.size());
511 if (existing_hash != info.hash) {
512 _film->log()->log (String::compose ("Existing frame %1 failed hash check", f));
520 Writer::check_existing_picture_mxf ()
522 /* Try to open the existing MXF */
523 boost::filesystem::path p;
524 p /= _film->internal_video_mxf_dir ();
525 p /= _film->internal_video_mxf_filename ();
526 FILE* mxf = fopen_boost (p, "rb");
528 _film->log()->log (String::compose ("Could not open existing MXF at %1 (errno=%2)", p.string(), errno));
533 for (boost::filesystem::directory_iterator i (_film->info_dir ()); i != boost::filesystem::directory_iterator (); ++i) {
539 shared_ptr<Job> job = _job.lock ();
542 job->set_progress (float (_first_nonexistant_frame) / N);
544 if (_film->three_d ()) {
545 if (!check_existing_picture_mxf_frame (mxf, _first_nonexistant_frame, EYES_LEFT)) {
548 if (!check_existing_picture_mxf_frame (mxf, _first_nonexistant_frame, EYES_RIGHT)) {
552 if (!check_existing_picture_mxf_frame (mxf, _first_nonexistant_frame, EYES_BOTH)) {
557 _film->log()->log (String::compose ("Have existing frame %1", _first_nonexistant_frame));
558 ++_first_nonexistant_frame;
564 /** @param frame Frame index.
565 * @return true if we can fake-write this frame.
568 Writer::can_fake_write (int frame) const
570 /* We have to do a proper write of the first frame so that we can set up the JPEG2000
571 parameters in the MXF writer.
573 return (frame != 0 && frame < _first_nonexistant_frame);
577 operator< (QueueItem const & a, QueueItem const & b)
579 if (a.frame != b.frame) {
580 return a.frame < b.frame;
583 return static_cast<int> (a.eyes) < static_cast<int> (b.eyes);
587 operator== (QueueItem const & a, QueueItem const & b)
589 return a.frame == b.frame && a.eyes == b.eyes;