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/picture_asset.h>
23 #include <libdcp/sound_asset.h>
24 #include <libdcp/picture_frame.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.
77 _picture_asset.reset (
78 new libdcp::StereoPictureAsset (
79 _film->internal_video_mxf_dir (),
80 _film->internal_video_mxf_filename (),
81 _film->video_frame_rate (),
82 _film->container()->size (_film->full_frame ())
87 _picture_asset.reset (
88 new libdcp::MonoPictureAsset (
89 _film->internal_video_mxf_dir (),
90 _film->internal_video_mxf_filename (),
91 _film->video_frame_rate (),
92 _film->container()->size (_film->full_frame ())
98 _picture_asset_writer = _picture_asset->start_write (_first_nonexistant_frame > 0);
101 new libdcp::SoundAsset (
102 _film->dir (_film->dcp_name()),
103 _film->audio_mxf_filename (),
104 _film->video_frame_rate (),
105 _film->audio_channels (),
106 _film->audio_frame_rate ()
110 _sound_asset_writer = _sound_asset->start_write ();
112 _thread = new boost::thread (boost::bind (&Writer::thread, this));
116 Writer::write (shared_ptr<const EncodedData> encoded, int frame, Eyes eyes)
118 boost::mutex::scoped_lock lock (_mutex);
121 qi.type = QueueItem::FULL;
122 qi.encoded = encoded;
125 if (_film->three_d() && eyes == EYES_BOTH) {
126 /* 2D material in a 3D DCP; fake the 3D */
128 _queue.push_back (qi);
129 ++_queued_full_in_memory;
130 qi.eyes = EYES_RIGHT;
131 _queue.push_back (qi);
132 ++_queued_full_in_memory;
135 _queue.push_back (qi);
136 ++_queued_full_in_memory;
139 _condition.notify_all ();
143 Writer::fake_write (int frame, Eyes eyes)
145 boost::mutex::scoped_lock lock (_mutex);
147 ifstream ifi (_film->info_path (frame, eyes).c_str());
148 libdcp::FrameInfo info (ifi);
151 qi.type = QueueItem::FAKE;
155 _queue.push_back (qi);
157 _condition.notify_all ();
160 /** This method is not thread safe */
162 Writer::write (shared_ptr<const AudioBuffers> audio)
164 _sound_asset_writer->write (audio->data(), audio->frames());
167 /** This must be called from Writer::thread() with an appropriate lock held,
168 * and with _queue sorted.
171 Writer::have_sequenced_image_at_queue_head () const
173 if (_queue.empty ()) {
177 /* The queue should contain only EYES_LEFT/EYES_RIGHT pairs or EYES_BOTH */
179 if (_queue.front().eyes == EYES_BOTH) {
181 return _queue.front().frame == (_last_written_frame + 1);
186 if (_last_written_eyes == EYES_LEFT && _queue.front().frame == _last_written_frame && _queue.front().eyes == EYES_RIGHT) {
190 if (_last_written_eyes == EYES_RIGHT && _queue.front().frame == (_last_written_frame + 1) && _queue.front().eyes == EYES_LEFT) {
203 boost::mutex::scoped_lock lock (_mutex);
209 if (_finish || _queued_full_in_memory > _maximum_frames_in_memory || have_sequenced_image_at_queue_head ()) {
213 TIMING (N_("writer sleeps with a queue of %1"), _queue.size());
214 _condition.wait (lock);
215 TIMING (N_("writer wakes with a queue of %1"), _queue.size());
218 if (_finish && _queue.empty()) {
222 /* Write any frames that we can write; i.e. those that are in sequence */
223 while (have_sequenced_image_at_queue_head ()) {
224 QueueItem qi = _queue.front ();
226 if (qi.type == QueueItem::FULL && qi.encoded) {
227 --_queued_full_in_memory;
232 case QueueItem::FULL:
234 _film->log()->log (String::compose (N_("Writer FULL-writes %1 to MXF"), qi.frame));
236 qi.encoded.reset (new EncodedData (_film->j2c_path (qi.frame, qi.eyes, false)));
239 libdcp::FrameInfo fin = _picture_asset_writer->write (qi.encoded->data(), qi.encoded->size());
240 qi.encoded->write_info (_film, qi.frame, qi.eyes, fin);
241 _last_written[qi.eyes] = qi.encoded;
245 case QueueItem::FAKE:
246 _film->log()->log (String::compose (N_("Writer FAKE-writes %1 to MXF"), qi.frame));
247 _picture_asset_writer->fake_write (qi.size);
248 _last_written[qi.eyes].reset ();
251 case QueueItem::REPEAT:
253 _film->log()->log (String::compose (N_("Writer REPEAT-writes %1 to MXF"), qi.frame));
254 libdcp::FrameInfo fin = _picture_asset_writer->write (
255 _last_written[qi.eyes]->data(),
256 _last_written[qi.eyes]->size()
259 _last_written[qi.eyes]->write_info (_film, qi.frame, qi.eyes, fin);
266 _last_written_frame = qi.frame;
267 _last_written_eyes = qi.eyes;
269 if (_film->length()) {
271 float (_full_written + _fake_written + _repeat_written) / _film->time_to_video_frames (_film->length())
276 while (_queued_full_in_memory > _maximum_frames_in_memory) {
277 /* Too many frames in memory which can't yet be written to the stream.
278 Write some FULL frames to disk.
282 list<QueueItem>::reverse_iterator i = _queue.rbegin ();
283 while (i != _queue.rend() && (i->type != QueueItem::FULL || !i->encoded)) {
287 assert (i != _queue.rend());
296 "Writer full (awaiting %1 [last eye was %2]); pushes %3 to disk",
297 _last_written_frame + 1,
298 _last_written_eyes, qi.frame)
301 qi.encoded->write (_film, qi.frame, qi.eyes);
304 --_queued_full_in_memory;
320 boost::mutex::scoped_lock lock (_mutex);
322 _condition.notify_all ();
333 _picture_asset_writer->finalize ();
334 _sound_asset_writer->finalize ();
336 int const frames = _last_written_frame + 1;
338 _picture_asset->set_duration (frames);
340 /* Hard-link the video MXF into the DCP */
342 boost::filesystem::path from;
343 from /= _film->internal_video_mxf_dir();
344 from /= _film->internal_video_mxf_filename();
346 boost::filesystem::path to;
347 to /= _film->dir (_film->dcp_name());
348 to /= _film->video_mxf_filename ();
350 boost::system::error_code ec;
351 boost::filesystem::create_hard_link (from, to, ec);
353 /* hard link failed; copy instead */
354 boost::filesystem::copy_file (from, to);
355 _film->log()->log ("Hard-link failed; fell back to copying");
358 /* And update the asset */
360 _picture_asset->set_directory (_film->dir (_film->dcp_name ()));
361 _picture_asset->set_file_name (_film->video_mxf_filename ());
362 _sound_asset->set_duration (frames);
364 libdcp::DCP dcp (_film->dir (_film->dcp_name()));
366 shared_ptr<libdcp::CPL> cpl (
368 _film->dir (_film->dcp_name()),
370 _film->dcp_content_type()->libdcp_kind (),
372 _film->video_frame_rate ()
378 cpl->add_reel (shared_ptr<libdcp::Reel> (new libdcp::Reel (
381 shared_ptr<libdcp::SubtitleAsset> ()
385 libdcp::XMLMetadata meta = Config::instance()->dcp_metadata ();
386 meta.set_issue_date_now ();
387 dcp.write_xml (meta);
389 _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));
392 /** Tell the writer that frame `f' should be a repeat of the frame before it */
394 Writer::repeat (int f, Eyes e)
396 boost::mutex::scoped_lock lock (_mutex);
399 qi.type = QueueItem::REPEAT;
403 _queue.push_back (qi);
405 _condition.notify_all ();
409 Writer::check_existing_picture_mxf_frame (FILE* mxf, int f, Eyes eyes)
411 /* Read the frame info as written */
412 ifstream ifi (_film->info_path (f, eyes).c_str());
413 libdcp::FrameInfo info (ifi);
415 /* Read the data from the MXF and hash it */
416 fseek (mxf, info.offset, SEEK_SET);
417 EncodedData data (info.size);
418 size_t const read = fread (data.data(), 1, data.size(), mxf);
419 if (read != static_cast<size_t> (data.size ())) {
420 _film->log()->log (String::compose ("Existing frame %1 is incomplete", f));
424 string const existing_hash = md5_digest (data.data(), data.size());
425 if (existing_hash != info.hash) {
426 _film->log()->log (String::compose ("Existing frame %1 failed hash check", f));
434 Writer::check_existing_picture_mxf ()
436 /* Try to open the existing MXF */
437 boost::filesystem::path p;
438 p /= _film->internal_video_mxf_dir ();
439 p /= _film->internal_video_mxf_filename ();
440 FILE* mxf = fopen (p.string().c_str(), "rb");
442 _film->log()->log (String::compose ("Could not open existing MXF at %1 (errno=%2)", p.string(), errno));
448 if (_film->three_d ()) {
449 if (!check_existing_picture_mxf_frame (mxf, _first_nonexistant_frame, EYES_LEFT)) {
452 if (!check_existing_picture_mxf_frame (mxf, _first_nonexistant_frame, EYES_RIGHT)) {
456 if (!check_existing_picture_mxf_frame (mxf, _first_nonexistant_frame, EYES_BOTH)) {
461 _film->log()->log (String::compose ("Have existing frame %1", _first_nonexistant_frame));
462 ++_first_nonexistant_frame;
468 /** @param frame Frame index.
469 * @return true if we can fake-write this frame.
472 Writer::can_fake_write (int frame) const
474 /* We have to do a proper write of the first frame so that we can set up the JPEG2000
475 parameters in the MXF writer.
477 return (frame != 0 && frame < _first_nonexistant_frame);
481 operator< (QueueItem const & a, QueueItem const & b)
483 if (a.frame != b.frame) {
484 return a.frame < b.frame;
487 return a.eyes == EYES_LEFT && b.eyes == EYES_RIGHT;
491 operator== (QueueItem const & a, QueueItem const & b)
493 return a.frame == b.frame && a.eyes == b.eyes;