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)
65 /* Remove any old DCP */
66 boost::filesystem::remove_all (_film->dir (_film->dcp_name ()));
68 check_existing_picture_mxf ();
70 /* Create our picture asset in a subdirectory, named according to those
71 film's parameters which affect the video output. We will hard-link
72 it into the DCP later.
75 _picture_asset.reset (
76 new libdcp::MonoPictureAsset (
77 _film->internal_video_mxf_dir (),
78 _film->internal_video_mxf_filename (),
79 _film->dcp_video_frame_rate (),
80 _film->container()->size (_film->full_frame ())
84 _picture_asset_writer = _picture_asset->start_write (_first_nonexistant_frame > 0);
87 new libdcp::SoundAsset (
88 _film->dir (_film->dcp_name()),
89 _film->dcp_audio_mxf_filename (),
90 _film->dcp_video_frame_rate (),
91 _film->dcp_audio_channels (),
92 _film->dcp_audio_frame_rate ()
96 _sound_asset_writer = _sound_asset->start_write ();
98 _thread = new boost::thread (boost::bind (&Writer::thread, this));
102 Writer::write (shared_ptr<const EncodedData> encoded, int frame)
104 boost::mutex::scoped_lock lock (_mutex);
107 qi.type = QueueItem::FULL;
108 qi.encoded = encoded;
110 _queue.push_back (qi);
111 ++_queued_full_in_memory;
113 _condition.notify_all ();
117 Writer::fake_write (int frame)
119 boost::mutex::scoped_lock lock (_mutex);
121 ifstream ifi (_film->info_path (frame).c_str());
122 libdcp::FrameInfo info (ifi);
125 qi.type = QueueItem::FAKE;
128 _queue.push_back (qi);
130 _condition.notify_all ();
133 /** This method is not thread safe */
135 Writer::write (shared_ptr<const AudioBuffers> audio)
137 _sound_asset_writer->write (audio->data(), audio->frames());
146 boost::mutex::scoped_lock lock (_mutex);
153 _queued_full_in_memory > _maximum_frames_in_memory ||
154 (!_queue.empty() && _queue.front().frame == (_last_written_frame + 1))) {
159 TIMING (N_("writer sleeps with a queue of %1"), _queue.size());
160 _condition.wait (lock);
161 TIMING (N_("writer wakes with a queue of %1"), _queue.size());
164 if (_finish && _queue.empty()) {
168 /* Write any frames that we can write; i.e. those that are in sequence */
169 while (!_queue.empty() && _queue.front().frame == (_last_written_frame + 1)) {
170 QueueItem qi = _queue.front ();
172 if (qi.type == QueueItem::FULL && qi.encoded) {
173 --_queued_full_in_memory;
178 case QueueItem::FULL:
180 _film->log()->log (String::compose (N_("Writer FULL-writes %1 to MXF"), qi.frame));
182 qi.encoded.reset (new EncodedData (_film->j2c_path (qi.frame, false)));
184 libdcp::FrameInfo const fin = _picture_asset_writer->write (qi.encoded->data(), qi.encoded->size());
185 qi.encoded->write_info (_film, qi.frame, fin);
186 _last_written = qi.encoded;
190 case QueueItem::FAKE:
191 _film->log()->log (String::compose (N_("Writer FAKE-writes %1 to MXF"), qi.frame));
192 _picture_asset_writer->fake_write (qi.size);
193 _last_written.reset ();
196 case QueueItem::REPEAT:
198 _film->log()->log (String::compose (N_("Writer REPEAT-writes %1 to MXF"), qi.frame));
199 libdcp::FrameInfo const fin = _picture_asset_writer->write (_last_written->data(), _last_written->size());
200 _last_written->write_info (_film, qi.frame, fin);
207 if (_film->length_with_loop()) {
209 float (_full_written + _fake_written + _repeat_written) / _film->time_to_video_frames (_film->length_with_loop())
213 ++_last_written_frame;
216 while (_queued_full_in_memory > _maximum_frames_in_memory) {
217 /* Too many frames in memory which can't yet be written to the stream.
218 Write some FULL frames to disk.
222 list<QueueItem>::reverse_iterator i = _queue.rbegin ();
223 while (i != _queue.rend() && (i->type != QueueItem::FULL || !i->encoded)) {
227 assert (i != _queue.rend());
233 _film->log()->log (String::compose (N_("Writer full (awaiting %1); pushes %2 to disk"), _last_written_frame + 1, qi.frame));
234 qi.encoded->write (_film, qi.frame);
237 --_queued_full_in_memory;
253 boost::mutex::scoped_lock lock (_mutex);
255 _condition.notify_all ();
266 _picture_asset_writer->finalize ();
267 _sound_asset_writer->finalize ();
269 int const frames = _last_written_frame + 1;
271 _picture_asset->set_duration (frames);
273 /* Hard-link the video MXF into the DCP */
275 boost::filesystem::path from;
276 from /= _film->internal_video_mxf_dir();
277 from /= _film->internal_video_mxf_filename();
279 boost::filesystem::path to;
280 to /= _film->dir (_film->dcp_name());
281 to /= _film->dcp_video_mxf_filename ();
283 boost::system::error_code ec;
284 boost::filesystem::create_hard_link (from, to, ec);
286 /* hard link failed; copy instead */
287 boost::filesystem::copy_file (from, to);
288 _film->log()->log ("Hard-link failed; fell back to copying");
291 /* And update the asset */
293 _picture_asset->set_directory (_film->dir (_film->dcp_name ()));
294 _picture_asset->set_file_name (_film->dcp_video_mxf_filename ());
295 _sound_asset->set_duration (frames);
297 libdcp::DCP dcp (_film->dir (_film->dcp_name()));
299 shared_ptr<libdcp::CPL> cpl (
301 _film->dir (_film->dcp_name()),
303 _film->dcp_content_type()->libdcp_kind (),
305 _film->dcp_video_frame_rate ()
311 cpl->add_reel (shared_ptr<libdcp::Reel> (new libdcp::Reel (
314 shared_ptr<libdcp::SubtitleAsset> ()
318 libdcp::XMLMetadata meta = Config::instance()->dcp_metadata ();
319 meta.set_issue_date_now ();
320 dcp.write_xml (meta);
322 _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));
325 /** Tell the writer that frame `f' should be a repeat of the frame before it */
327 Writer::repeat (int f)
329 boost::mutex::scoped_lock lock (_mutex);
332 qi.type = QueueItem::REPEAT;
335 _queue.push_back (qi);
337 _condition.notify_all ();
342 Writer::check_existing_picture_mxf ()
344 /* Try to open the existing MXF */
345 boost::filesystem::path p;
346 p /= _film->internal_video_mxf_dir ();
347 p /= _film->internal_video_mxf_filename ();
348 FILE* mxf = fopen (p.string().c_str(), "rb");
350 _film->log()->log (String::compose ("Could not open existing MXF at %1 (errno=%2)", p.string(), errno));
356 /* Read the frame info as written */
357 ifstream ifi (_film->info_path (_first_nonexistant_frame).c_str());
358 libdcp::FrameInfo info (ifi);
360 /* Read the data from the MXF and hash it */
361 fseek (mxf, info.offset, SEEK_SET);
362 EncodedData data (info.size);
363 size_t const read = fread (data.data(), 1, data.size(), mxf);
364 if (read != static_cast<size_t> (data.size ())) {
365 _film->log()->log (String::compose ("Existing frame %1 is incomplete", _first_nonexistant_frame));
369 string const existing_hash = md5_digest (data.data(), data.size());
370 if (existing_hash != info.hash) {
371 _film->log()->log (String::compose ("Existing frame %1 failed hash check", _first_nonexistant_frame));
375 _film->log()->log (String::compose ("Have existing frame %1", _first_nonexistant_frame));
376 ++_first_nonexistant_frame;
382 /** @param frame Frame index.
383 * @return true if we can fake-write this frame.
386 Writer::can_fake_write (int frame) const
388 /* We have to do a proper write of the first frame so that we can set up the JPEG2000
389 parameters in the MXF writer.
391 return (frame != 0 && frame < _first_nonexistant_frame);
395 operator< (QueueItem const & a, QueueItem const & b)
397 return a.frame < b.frame;
401 operator== (QueueItem const & a, QueueItem const & b)
403 return a.frame == b.frame;