2 Copyright (C) 2012-2014 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 <dcp/mono_picture_mxf.h>
23 #include <dcp/stereo_picture_mxf.h>
24 #include <dcp/sound_mxf.h>
25 #include <dcp/sound_mxf_writer.h>
27 #include <dcp/reel_mono_picture_asset.h>
28 #include <dcp/reel_stereo_picture_asset.h>
29 #include <dcp/reel_sound_asset.h>
33 #include "compose.hpp"
37 #include "dcp_video_frame.h"
38 #include "dcp_content_type.h"
40 #include "audio_mapping.h"
44 #include "audio_buffers.h"
48 #define LOG_GENERAL(...) _film->log()->log (String::compose (__VA_ARGS__), Log::TYPE_GENERAL);
49 #define LOG_TIMING(...) _film->log()->microsecond_log (String::compose (__VA_ARGS__), Log::TYPE_TIMING);
50 #define LOG_WARNING_NC(...) _film->log()->log (__VA_ARGS__, Log::TYPE_WARNING);
52 /* OS X strikes again */
60 using std::stringstream;
61 using boost::shared_ptr;
62 using boost::weak_ptr;
63 using boost::dynamic_pointer_cast;
65 int const Writer::_maximum_frames_in_memory = Config::instance()->num_local_encoding_threads() + 4;
67 Writer::Writer (shared_ptr<const Film> f, weak_ptr<Job> j)
70 , _first_nonexistant_frame (0)
73 , _queued_full_in_memory (0)
74 , _last_written_frame (-1)
75 , _last_written_eyes (EYES_RIGHT)
80 /* Remove any old DCP */
81 boost::filesystem::remove_all (_film->dir (_film->dcp_name ()));
83 shared_ptr<Job> job = _job.lock ();
86 job->sub (_("Checking existing image data"));
87 check_existing_picture_mxf ();
89 /* Create our picture asset in a subdirectory, named according to those
90 film's parameters which affect the video output. We will hard-link
91 it into the DCP later.
94 if (_film->three_d ()) {
95 _picture_mxf.reset (new dcp::StereoPictureMXF (dcp::Fraction (_film->video_frame_rate (), 1)));
97 _picture_mxf.reset (new dcp::MonoPictureMXF (dcp::Fraction (_film->video_frame_rate (), 1)));
100 _picture_mxf->set_size (_film->frame_size ());
102 if (_film->encrypted ()) {
103 _picture_mxf->set_key (_film->key ());
106 _picture_mxf_writer = _picture_mxf->start_write (
107 _film->internal_video_mxf_dir() / _film->internal_video_mxf_filename(),
108 _film->interop() ? dcp::INTEROP : dcp::SMPTE,
109 _first_nonexistant_frame > 0
112 if (_film->audio_channels ()) {
113 _sound_mxf.reset (new dcp::SoundMXF (dcp::Fraction (_film->video_frame_rate(), 1), _film->audio_frame_rate (), _film->audio_channels ()));
115 if (_film->encrypted ()) {
116 _sound_mxf->set_key (_film->key ());
119 /* Write the sound MXF into the film directory so that we leave the creation
120 of the DCP directory until the last minute.
122 _sound_mxf_writer = _sound_mxf->start_write (_film->directory() / _film->audio_mxf_filename(), _film->interop() ? dcp::INTEROP : dcp::SMPTE);
125 _thread = new boost::thread (boost::bind (&Writer::thread, this));
127 job->sub (_("Encoding image data"));
132 terminate_thread (false);
136 Writer::write (shared_ptr<const EncodedData> encoded, int frame, Eyes eyes)
138 boost::mutex::scoped_lock lock (_mutex);
140 while (_queued_full_in_memory > _maximum_frames_in_memory) {
141 _full_condition.wait (lock);
145 qi.type = QueueItem::FULL;
146 qi.encoded = encoded;
149 if (_film->three_d() && eyes == EYES_BOTH) {
150 /* 2D material in a 3D DCP; fake the 3D */
152 _queue.push_back (qi);
153 ++_queued_full_in_memory;
154 qi.eyes = EYES_RIGHT;
155 _queue.push_back (qi);
156 ++_queued_full_in_memory;
159 _queue.push_back (qi);
160 ++_queued_full_in_memory;
163 _empty_condition.notify_all ();
167 Writer::fake_write (int frame, Eyes eyes)
169 boost::mutex::scoped_lock lock (_mutex);
171 while (_queued_full_in_memory > _maximum_frames_in_memory) {
172 _full_condition.wait (lock);
175 FILE* ifi = fopen_boost (_film->info_path (frame, eyes), "r");
176 dcp::FrameInfo info (ifi);
180 qi.type = QueueItem::FAKE;
183 if (_film->three_d() && eyes == EYES_BOTH) {
185 _queue.push_back (qi);
186 qi.eyes = EYES_RIGHT;
187 _queue.push_back (qi);
190 _queue.push_back (qi);
193 _empty_condition.notify_all ();
196 /** This method is not thread safe */
198 Writer::write (shared_ptr<const AudioBuffers> audio)
200 if (_sound_mxf_writer) {
201 _sound_mxf_writer->write (audio->data(), audio->frames());
205 /** This must be called from Writer::thread() with an appropriate lock held */
207 Writer::have_sequenced_image_at_queue_head ()
209 if (_queue.empty ()) {
215 /* The queue should contain only EYES_LEFT/EYES_RIGHT pairs or EYES_BOTH */
217 if (_queue.front().eyes == EYES_BOTH) {
219 return _queue.front().frame == (_last_written_frame + 1);
224 if (_last_written_eyes == EYES_LEFT && _queue.front().frame == _last_written_frame && _queue.front().eyes == EYES_RIGHT) {
228 if (_last_written_eyes == EYES_RIGHT && _queue.front().frame == (_last_written_frame + 1) && _queue.front().eyes == EYES_LEFT) {
241 boost::mutex::scoped_lock lock (_mutex);
245 if (_finish || _queued_full_in_memory > _maximum_frames_in_memory || have_sequenced_image_at_queue_head ()) {
249 LOG_TIMING (N_("writer sleeps with a queue of %1"), _queue.size());
250 _empty_condition.wait (lock);
251 LOG_TIMING (N_("writer wakes with a queue of %1"), _queue.size());
254 if (_finish && _queue.empty()) {
258 /* Write any frames that we can write; i.e. those that are in sequence. */
259 while (have_sequenced_image_at_queue_head ()) {
260 QueueItem qi = _queue.front ();
262 if (qi.type == QueueItem::FULL && qi.encoded) {
263 --_queued_full_in_memory;
268 case QueueItem::FULL:
270 LOG_GENERAL (N_("Writer FULL-writes %1 to MXF"), qi.frame);
272 qi.encoded.reset (new EncodedData (_film->j2c_path (qi.frame, qi.eyes, false)));
275 dcp::FrameInfo fin = _picture_mxf_writer->write (qi.encoded->data(), qi.encoded->size());
276 qi.encoded->write_info (_film, qi.frame, qi.eyes, fin);
277 _last_written[qi.eyes] = qi.encoded;
281 case QueueItem::FAKE:
282 LOG_GENERAL (N_("Writer FAKE-writes %1 to MXF"), qi.frame);
283 _picture_mxf_writer->fake_write (qi.size);
284 _last_written[qi.eyes].reset ();
290 _last_written_frame = qi.frame;
291 _last_written_eyes = qi.eyes;
293 shared_ptr<Job> job = _job.lock ();
295 int64_t total = _film->length().frames (_film->video_frame_rate ());
296 if (_film->three_d ()) {
297 /* _full_written and so on are incremented for each eye, so we need to double the total
298 frames to get the correct progress.
303 job->set_progress (float (_full_written + _fake_written) / total);
307 while (_queued_full_in_memory > _maximum_frames_in_memory) {
308 /* Too many frames in memory which can't yet be written to the stream.
309 Write some FULL frames to disk.
312 /* Find one from the back of the queue */
314 list<QueueItem>::reverse_iterator i = _queue.rbegin ();
315 while (i != _queue.rend() && (i->type != QueueItem::FULL || !i->encoded)) {
319 assert (i != _queue.rend());
327 "Writer full (awaiting %1 [last eye was %2]); pushes %3 to disk",
328 _last_written_frame + 1,
329 _last_written_eyes, qi.frame
332 qi.encoded->write (_film, qi.frame, qi.eyes);
335 --_queued_full_in_memory;
338 _full_condition.notify_all ();
347 Writer::terminate_thread (bool can_throw)
349 boost::mutex::scoped_lock lock (_mutex);
355 _empty_condition.notify_all ();
356 _full_condition.notify_all ();
375 terminate_thread (true);
377 _picture_mxf_writer->finalize ();
378 if (_sound_mxf_writer) {
379 _sound_mxf_writer->finalize ();
382 /* Hard-link the video MXF into the DCP */
383 boost::filesystem::path video_from;
384 video_from /= _film->internal_video_mxf_dir();
385 video_from /= _film->internal_video_mxf_filename();
387 boost::filesystem::path video_to;
388 video_to /= _film->dir (_film->dcp_name());
389 video_to /= _film->video_mxf_filename ();
391 boost::system::error_code ec;
392 boost::filesystem::create_hard_link (video_from, video_to, ec);
394 /* hard link failed; copy instead */
395 boost::filesystem::copy_file (video_from, video_to);
396 LOG_WARNING_NC ("Hard-link failed; fell back to copying");
399 _picture_mxf->set_file (video_to);
401 /* Move the audio MXF into the DCP */
404 boost::filesystem::path audio_to;
405 audio_to /= _film->dir (_film->dcp_name ());
406 audio_to /= _film->audio_mxf_filename ();
408 boost::filesystem::rename (_film->file (_film->audio_mxf_filename ()), audio_to, ec);
411 String::compose (_("could not move audio MXF into the DCP (%1)"), ec.value ()), _film->file (_film->audio_mxf_filename ())
415 _sound_mxf->set_file (audio_to);
418 dcp::DCP dcp (_film->dir (_film->dcp_name()));
420 shared_ptr<dcp::CPL> cpl (
423 _film->dcp_content_type()->libdcp_kind ()
429 shared_ptr<dcp::Reel> reel (new dcp::Reel ());
431 shared_ptr<dcp::MonoPictureMXF> mono = dynamic_pointer_cast<dcp::MonoPictureMXF> (_picture_mxf);
433 reel->add (shared_ptr<dcp::ReelPictureAsset> (new dcp::ReelMonoPictureAsset (mono, 0)));
437 shared_ptr<dcp::StereoPictureMXF> stereo = dynamic_pointer_cast<dcp::StereoPictureMXF> (_picture_mxf);
439 reel->add (shared_ptr<dcp::ReelPictureAsset> (new dcp::ReelStereoPictureAsset (stereo, 0)));
444 reel->add (shared_ptr<dcp::ReelSoundAsset> (new dcp::ReelSoundAsset (_sound_mxf, 0)));
445 dcp.add (_sound_mxf);
450 shared_ptr<Job> job = _job.lock ();
453 job->sub (_("Computing image digest"));
454 _picture_mxf->hash (boost::bind (&Job::set_progress, job.get(), _1, false));
457 job->sub (_("Computing audio digest"));
458 _sound_mxf->hash (boost::bind (&Job::set_progress, job.get(), _1, false));
461 dcp::XMLMetadata meta = Config::instance()->dcp_metadata ();
462 meta.set_issue_date_now ();
464 dcp.write_xml (_film->interop () ? dcp::INTEROP : dcp::SMPTE, meta, _film->is_signed() ? make_signer () : shared_ptr<const dcp::Signer> ());
467 N_("Wrote %1 FULL, %2 FAKE, %3 pushed to disk"), _full_written, _fake_written, _pushed_to_disk
472 Writer::check_existing_picture_mxf_frame (FILE* mxf, int f, Eyes eyes)
474 /* Read the frame info as written */
475 FILE* ifi = fopen_boost (_film->info_path (f, eyes), "r");
477 LOG_GENERAL ("Existing frame %1 has no info file", f);
481 dcp::FrameInfo info (ifi);
483 if (info.size == 0) {
484 LOG_GENERAL ("Existing frame %1 has no info file", f);
488 /* Read the data from the MXF and hash it */
489 dcpomatic_fseek (mxf, info.offset, SEEK_SET);
490 EncodedData data (info.size);
491 size_t const read = fread (data.data(), 1, data.size(), mxf);
492 if (read != static_cast<size_t> (data.size ())) {
493 LOG_GENERAL ("Existing frame %1 is incomplete", f);
497 string const existing_hash = md5_digest (data.data(), data.size());
498 if (existing_hash != info.hash) {
499 LOG_GENERAL ("Existing frame %1 failed hash check", f);
507 Writer::check_existing_picture_mxf ()
509 /* Try to open the existing MXF */
510 boost::filesystem::path p;
511 p /= _film->internal_video_mxf_dir ();
512 p /= _film->internal_video_mxf_filename ();
513 FILE* mxf = fopen_boost (p, "rb");
515 LOG_GENERAL ("Could not open existing MXF at %1 (errno=%2)", p.string(), errno);
520 for (boost::filesystem::directory_iterator i (_film->info_dir ()); i != boost::filesystem::directory_iterator (); ++i) {
526 shared_ptr<Job> job = _job.lock ();
530 job->set_progress (float (_first_nonexistant_frame) / N);
533 if (_film->three_d ()) {
534 if (!check_existing_picture_mxf_frame (mxf, _first_nonexistant_frame, EYES_LEFT)) {
537 if (!check_existing_picture_mxf_frame (mxf, _first_nonexistant_frame, EYES_RIGHT)) {
541 if (!check_existing_picture_mxf_frame (mxf, _first_nonexistant_frame, EYES_BOTH)) {
546 LOG_GENERAL ("Have existing frame %1", _first_nonexistant_frame);
547 ++_first_nonexistant_frame;
553 /** @param frame Frame index.
554 * @return true if we can fake-write this frame.
557 Writer::can_fake_write (int frame) const
559 /* We have to do a proper write of the first frame so that we can set up the JPEG2000
560 parameters in the MXF writer.
562 return (frame != 0 && frame < _first_nonexistant_frame);
566 operator< (QueueItem const & a, QueueItem const & b)
568 if (a.frame != b.frame) {
569 return a.frame < b.frame;
572 return static_cast<int> (a.eyes) < static_cast<int> (b.eyes);
576 operator== (QueueItem const & a, QueueItem const & b)
578 return a.frame == b.frame && a.eyes == b.eyes;