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.
21 #include <libdcp/picture_asset.h>
22 #include <libdcp/sound_asset.h>
23 #include <libdcp/picture_frame.h>
24 #include <libdcp/reel.h>
25 #include <libdcp/dcp.h>
26 #include <libdcp/cpl.h>
28 #include "compose.hpp"
30 #include "container.h"
32 #include "dcp_video_frame.h"
33 #include "dcp_content_type.h"
35 #include "audio_mapping.h"
47 using boost::shared_ptr;
49 int const Writer::_maximum_frames_in_memory = 8;
51 Writer::Writer (shared_ptr<Film> f, shared_ptr<Job> j)
54 , _first_nonexistant_frame (0)
57 , _queued_full_in_memory (0)
58 , _last_written_frame (-1)
64 /* Remove any old DCP */
65 boost::filesystem::remove_all (_film->dir (_film->dcp_name ()));
67 check_existing_picture_mxf ();
69 /* Create our picture asset in a subdirectory, named according to those
70 film's parameters which affect the video output. We will hard-link
71 it into the DCP later.
74 _picture_asset.reset (
75 new libdcp::MonoPictureAsset (
76 _film->internal_video_mxf_dir (),
77 _film->internal_video_mxf_filename (),
78 _film->dcp_video_frame_rate (),
79 _film->container()->dcp_size ()
83 _picture_asset_writer = _picture_asset->start_write (_first_nonexistant_frame > 0);
86 new libdcp::SoundAsset (
87 _film->dir (_film->dcp_name()),
88 _film->dcp_audio_mxf_filename (),
89 _film->dcp_video_frame_rate (),
90 _film->dcp_audio_channels (),
91 _film->dcp_audio_frame_rate()
95 _sound_asset_writer = _sound_asset->start_write ();
97 _thread = new boost::thread (boost::bind (&Writer::thread, this));
101 Writer::write (shared_ptr<const EncodedData> encoded, int frame)
103 boost::mutex::scoped_lock lock (_mutex);
106 qi.type = QueueItem::FULL;
107 qi.encoded = encoded;
109 _queue.push_back (qi);
110 ++_queued_full_in_memory;
112 _condition.notify_all ();
116 Writer::fake_write (int frame)
118 boost::mutex::scoped_lock lock (_mutex);
120 ifstream ifi (_film->info_path (frame).c_str());
121 libdcp::FrameInfo info (ifi);
124 qi.type = QueueItem::FAKE;
127 _queue.push_back (qi);
129 _condition.notify_all ();
132 /** This method is not thread safe */
134 Writer::write (shared_ptr<const AudioBuffers> audio)
136 _sound_asset_writer->write (audio->data(), audio->frames());
145 boost::mutex::scoped_lock lock (_mutex);
152 _queued_full_in_memory > _maximum_frames_in_memory ||
153 (!_queue.empty() && _queue.front().frame == (_last_written_frame + 1))) {
158 TIMING (N_("writer sleeps with a queue of %1"), _queue.size());
159 _condition.wait (lock);
160 TIMING (N_("writer wakes with a queue of %1"), _queue.size());
163 if (_finish && _queue.empty()) {
167 /* Write any frames that we can write; i.e. those that are in sequence */
168 while (!_queue.empty() && _queue.front().frame == (_last_written_frame + 1)) {
169 QueueItem qi = _queue.front ();
171 if (qi.type == QueueItem::FULL && qi.encoded) {
172 --_queued_full_in_memory;
177 case QueueItem::FULL:
179 _film->log()->log (String::compose (N_("Writer FULL-writes %1 to MXF"), qi.frame));
181 qi.encoded.reset (new EncodedData (_film->j2c_path (qi.frame, false)));
183 libdcp::FrameInfo const fin = _picture_asset_writer->write (qi.encoded->data(), qi.encoded->size());
184 qi.encoded->write_info (_film, qi.frame, fin);
185 _last_written = qi.encoded;
189 case QueueItem::FAKE:
190 _film->log()->log (String::compose (N_("Writer FAKE-writes %1 to MXF"), qi.frame));
191 _picture_asset_writer->fake_write (qi.size);
192 _last_written.reset ();
195 case QueueItem::REPEAT:
197 _film->log()->log (String::compose (N_("Writer REPEAT-writes %1 to MXF"), qi.frame));
198 libdcp::FrameInfo const fin = _picture_asset_writer->write (_last_written->data(), _last_written->size());
199 _last_written->write_info (_film, qi.frame, fin);
206 if (_film->length ()) {
207 _job->set_progress (float(_full_written + _fake_written + _repeat_written) / _film->time_to_video_frames (_film->length()));
210 ++_last_written_frame;
213 while (_queued_full_in_memory > _maximum_frames_in_memory) {
214 /* Too many frames in memory which can't yet be written to the stream.
215 Write some FULL frames to disk.
219 list<QueueItem>::reverse_iterator i = _queue.rbegin ();
220 while (i != _queue.rend() && (i->type != QueueItem::FULL || !i->encoded)) {
224 assert (i != _queue.rend());
230 _film->log()->log (String::compose (N_("Writer full (awaiting %1); pushes %2 to disk"), _last_written_frame + 1, qi.frame));
231 qi.encoded->write (_film, qi.frame);
234 --_queued_full_in_memory;
250 boost::mutex::scoped_lock lock (_mutex);
252 _condition.notify_all ();
263 _picture_asset_writer->finalize ();
264 _sound_asset_writer->finalize ();
266 int const frames = _last_written_frame + 1;
268 _picture_asset->set_duration (frames);
270 /* Hard-link the video MXF into the DCP */
272 boost::filesystem::path from;
273 from /= _film->internal_video_mxf_dir();
274 from /= _film->internal_video_mxf_filename();
276 boost::filesystem::path to;
277 to /= _film->dir (_film->dcp_name());
278 to /= _film->dcp_video_mxf_filename ();
280 boost::system::error_code ec;
281 boost::filesystem::create_hard_link (from, to, ec);
283 /* hard link failed; copy instead */
284 boost::filesystem::copy_file (from, to);
285 _film->log()->log ("Hard-link failed; fell back to copying");
288 /* And update the asset */
290 _picture_asset->set_directory (_film->dir (_film->dcp_name ()));
291 _picture_asset->set_file_name (_film->dcp_video_mxf_filename ());
292 _sound_asset->set_duration (frames);
294 libdcp::DCP dcp (_film->dir (_film->dcp_name()));
296 shared_ptr<libdcp::CPL> cpl (
298 _film->dir (_film->dcp_name()),
300 _film->dcp_content_type()->libdcp_kind (),
302 _film->dcp_video_frame_rate ()
308 cpl->add_reel (shared_ptr<libdcp::Reel> (new libdcp::Reel (
311 shared_ptr<libdcp::SubtitleAsset> ()
315 libdcp::XMLMetadata meta = Config::instance()->dcp_metadata ();
316 meta.set_issue_date_now ();
317 dcp.write_xml (meta);
319 _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));
322 /** Tell the writer that frame `f' should be a repeat of the frame before it */
324 Writer::repeat (int f)
326 boost::mutex::scoped_lock lock (_mutex);
329 qi.type = QueueItem::REPEAT;
332 _queue.push_back (qi);
334 _condition.notify_all ();
339 Writer::check_existing_picture_mxf ()
341 /* Try to open the existing MXF */
342 boost::filesystem::path p;
343 p /= _film->internal_video_mxf_dir ();
344 p /= _film->internal_video_mxf_filename ();
345 FILE* mxf = fopen (p.string().c_str(), N_("rb"));
352 /* Read the frame info as written */
353 ifstream ifi (_film->info_path (_first_nonexistant_frame).c_str());
354 libdcp::FrameInfo info (ifi);
356 /* Read the data from the MXF and hash it */
357 fseek (mxf, info.offset, SEEK_SET);
358 EncodedData data (info.size);
359 fread (data.data(), 1, data.size(), mxf);
360 string const existing_hash = md5_digest (data.data(), data.size());
362 if (existing_hash != info.hash) {
363 _film->log()->log (String::compose (N_("Existing frame %1 failed hash check"), _first_nonexistant_frame));
367 _film->log()->log (String::compose (N_("Have existing frame %1"), _first_nonexistant_frame));
368 ++_first_nonexistant_frame;
374 /** @param frame Frame index.
375 * @return true if we can fake-write this frame.
378 Writer::can_fake_write (int frame) const
380 /* We have to do a proper write of the first frame so that we can set up the JPEG2000
381 parameters in the MXF writer.
383 return (frame != 0 && frame < _first_nonexistant_frame);
387 operator< (QueueItem const & a, QueueItem const & b)
389 return a.frame < b.frame;
393 operator== (QueueItem const & a, QueueItem const & b)
395 return a.frame == b.frame;