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>
26 #include "compose.hpp"
30 #include "dcp_video_frame.h"
40 using boost::shared_ptr;
42 int const Writer::_maximum_frames_in_memory = 8;
44 Writer::Writer (shared_ptr<Film> f)
46 , _first_nonexistant_frame (0)
49 , _queued_full_in_memory (0)
50 , _last_written_frame (-1)
56 /* Remove any old DCP */
57 boost::filesystem::remove_all (_film->dir (_film->dcp_name ()));
59 check_existing_picture_mxf ();
61 /* Create our picture asset in a subdirectory, named according to those
62 film's parameters which affect the video output. We will hard-link
63 it into the DCP later.
66 _picture_asset.reset (
67 new libdcp::MonoPictureAsset (
68 _film->internal_video_mxf_dir (),
69 _film->internal_video_mxf_filename (),
70 _film->dcp_frame_rate (),
71 _film->format()->dcp_size ()
75 _picture_asset_writer = _picture_asset->start_write (_first_nonexistant_frame > 0);
77 AudioMapping m (_film->audio_channels ());
79 if (m.dcp_channels() > 0) {
81 new libdcp::SoundAsset (
82 _film->dir (_film->dcp_name()),
83 _film->dcp_audio_mxf_filename (),
84 _film->dcp_frame_rate (),
86 dcp_audio_sample_rate (_film->audio_stream()->sample_rate())
90 _sound_asset_writer = _sound_asset->start_write ();
93 _thread = new boost::thread (boost::bind (&Writer::thread, this));
97 Writer::write (shared_ptr<const EncodedData> encoded, int frame)
99 boost::mutex::scoped_lock lock (_mutex);
102 qi.type = QueueItem::FULL;
103 qi.encoded = encoded;
105 _queue.push_back (qi);
106 ++_queued_full_in_memory;
108 _condition.notify_all ();
112 Writer::fake_write (int frame)
114 boost::mutex::scoped_lock lock (_mutex);
116 ifstream ifi (_film->info_path (frame).c_str());
117 libdcp::FrameInfo info (ifi);
120 qi.type = QueueItem::FAKE;
123 _queue.push_back (qi);
125 _condition.notify_all ();
128 /** This method is not thread safe */
130 Writer::write (shared_ptr<const AudioBuffers> audio)
132 _sound_asset_writer->write (audio->data(), audio->frames());
141 boost::mutex::scoped_lock lock (_mutex);
148 _queued_full_in_memory > _maximum_frames_in_memory ||
149 (!_queue.empty() && _queue.front().frame == (_last_written_frame + 1))) {
154 TIMING (N_("writer sleeps with a queue of %1"), _queue.size());
155 _condition.wait (lock);
156 TIMING (N_("writer wakes with a queue of %1"), _queue.size());
159 if (_finish && _queue.empty()) {
163 /* Write any frames that we can write; i.e. those that are in sequence */
164 while (!_queue.empty() && _queue.front().frame == (_last_written_frame + 1)) {
165 QueueItem qi = _queue.front ();
167 if (qi.type == QueueItem::FULL && qi.encoded) {
168 --_queued_full_in_memory;
173 case QueueItem::FULL:
175 _film->log()->log (String::compose (N_("Writer FULL-writes %1 to MXF"), qi.frame));
177 qi.encoded.reset (new EncodedData (_film->j2c_path (qi.frame, false)));
179 libdcp::FrameInfo const fin = _picture_asset_writer->write (qi.encoded->data(), qi.encoded->size());
180 qi.encoded->write_info (_film, qi.frame, fin);
181 _last_written = qi.encoded;
185 case QueueItem::FAKE:
186 _film->log()->log (String::compose (N_("Writer FAKE-writes %1 to MXF"), qi.frame));
187 _picture_asset_writer->fake_write (qi.size);
188 _last_written.reset ();
191 case QueueItem::REPEAT:
193 _film->log()->log (String::compose (N_("Writer REPEAT-writes %1 to MXF"), qi.frame));
194 libdcp::FrameInfo const fin = _picture_asset_writer->write (_last_written->data(), _last_written->size());
195 _last_written->write_info (_film, qi.frame, fin);
202 ++_last_written_frame;
205 while (_queued_full_in_memory > _maximum_frames_in_memory) {
206 /* Too many frames in memory which can't yet be written to the stream.
207 Write some FULL frames to disk.
211 list<QueueItem>::reverse_iterator i = _queue.rbegin ();
212 while (i != _queue.rend() && (i->type != QueueItem::FULL || !i->encoded)) {
216 assert (i != _queue.rend());
222 _film->log()->log (String::compose (N_("Writer full (awaiting %1); pushes %2 to disk"), _last_written_frame + 1, qi.frame));
223 qi.encoded->write (_film, qi.frame);
226 --_queued_full_in_memory;
242 boost::mutex::scoped_lock lock (_mutex);
244 _condition.notify_all ();
255 _picture_asset_writer->finalize ();
257 if (_sound_asset_writer) {
258 _sound_asset_writer->finalize ();
261 int const frames = _last_written_frame + 1;
262 int const duration = frames - _film->trim_start() - _film->trim_end();
264 _picture_asset->set_entry_point (_film->trim_start ());
265 _picture_asset->set_duration (duration);
267 /* Hard-link the video MXF into the DCP */
269 boost::filesystem::path from;
270 from /= _film->internal_video_mxf_dir();
271 from /= _film->internal_video_mxf_filename();
273 boost::filesystem::path to;
274 to /= _film->dir (_film->dcp_name());
275 to /= _film->dcp_video_mxf_filename ();
277 boost::system::error_code ec;
278 boost::filesystem::create_hard_link (from, to, ec);
280 /* hard link failed; copy instead */
281 boost::filesystem::copy_file (from, to);
282 _film->log()->log ("Hard-link failed; fell back to copying");
285 /* And update the asset */
287 _picture_asset->set_directory (_film->dir (_film->dcp_name ()));
288 _picture_asset->set_file_name (_film->dcp_video_mxf_filename ());
291 _sound_asset->set_entry_point (_film->trim_start ());
292 _sound_asset->set_duration (duration);
295 libdcp::DCP dcp (_film->dir (_film->dcp_name()));
297 shared_ptr<libdcp::CPL> cpl (
299 _film->dir (_film->dcp_name()),
301 _film->dcp_content_type()->libdcp_kind (),
303 _film->dcp_frame_rate ()
309 cpl->add_reel (shared_ptr<libdcp::Reel> (new libdcp::Reel (
312 shared_ptr<libdcp::SubtitleAsset> ()
318 _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));
321 /** Tell the writer that frame `f' should be a repeat of the frame before it */
323 Writer::repeat (int f)
325 boost::mutex::scoped_lock lock (_mutex);
328 qi.type = QueueItem::REPEAT;
331 _queue.push_back (qi);
333 _condition.notify_all ();
338 Writer::check_existing_picture_mxf ()
340 /* Try to open the existing MXF */
341 boost::filesystem::path p;
342 p /= _film->internal_video_mxf_dir ();
343 p /= _film->internal_video_mxf_filename ();
344 FILE* mxf = fopen (p.string().c_str(), N_("rb"));
351 /* Read the frame info as written */
352 ifstream ifi (_film->info_path (_first_nonexistant_frame).c_str());
353 libdcp::FrameInfo info (ifi);
355 /* Read the data from the MXF and hash it */
356 fseek (mxf, info.offset, SEEK_SET);
357 EncodedData data (info.size);
358 fread (data.data(), 1, data.size(), mxf);
359 string const existing_hash = md5_digest (data.data(), data.size());
361 if (existing_hash != info.hash) {
362 _film->log()->log (String::compose (N_("Existing frame %1 failed hash check"), _first_nonexistant_frame));
366 _film->log()->log (String::compose (N_("Have existing frame %1"), _first_nonexistant_frame));
367 ++_first_nonexistant_frame;
373 /** @param frame Frame index.
374 * @return true if we can fake-write this frame.
377 Writer::can_fake_write (int frame) const
379 /* We have to do a proper write of the first frame so that we can set up the JPEG2000
380 parameters in the MXF writer.
382 return (frame != 0 && frame < _first_nonexistant_frame);
386 operator< (QueueItem const & a, QueueItem const & b)
388 return a.frame < b.frame;
392 operator== (QueueItem const & a, QueueItem const & b)
394 return a.frame == b.frame;