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"
41 using boost::shared_ptr;
43 int const Writer::_maximum_frames_in_memory = 8;
45 Writer::Writer (shared_ptr<Film> f, shared_ptr<Playlist> p)
48 , _first_nonexistant_frame (0)
51 , _queued_full_in_memory (0)
52 , _last_written_frame (-1)
58 /* Remove any old DCP */
59 boost::filesystem::remove_all (_film->dir (_film->dcp_name ()));
61 check_existing_picture_mxf ();
63 /* Create our picture asset in a subdirectory, named according to those
64 film's parameters which affect the video output. We will hard-link
65 it into the DCP later.
68 _picture_asset.reset (
69 new libdcp::MonoPictureAsset (
70 _film->video_mxf_dir (),
71 _film->video_mxf_filename (),
72 _film->dcp_frame_rate (),
73 _film->format()->dcp_size ()
77 _picture_asset_writer = _picture_asset->start_write (_first_nonexistant_frame > 0);
79 AudioMapping m (_playlist->audio_channels ());
81 if (m.dcp_channels() > 0) {
83 new libdcp::SoundAsset (
84 _film->dir (_film->dcp_name()),
86 _film->dcp_frame_rate (),
88 dcp_audio_sample_rate (_playlist->audio_frame_rate())
92 _sound_asset_writer = _sound_asset->start_write ();
95 _thread = new boost::thread (boost::bind (&Writer::thread, this));
99 Writer::write (shared_ptr<const EncodedData> encoded, int frame)
101 boost::mutex::scoped_lock lock (_mutex);
104 qi.type = QueueItem::FULL;
105 qi.encoded = encoded;
107 _queue.push_back (qi);
108 ++_queued_full_in_memory;
110 _condition.notify_all ();
114 Writer::fake_write (int frame)
116 boost::mutex::scoped_lock lock (_mutex);
118 ifstream ifi (_film->info_path (frame).c_str());
119 libdcp::FrameInfo info (ifi);
122 qi.type = QueueItem::FAKE;
125 _queue.push_back (qi);
127 _condition.notify_all ();
130 /** This method is not thread safe */
132 Writer::write (shared_ptr<const AudioBuffers> audio)
134 _sound_asset_writer->write (audio->data(), audio->frames());
143 boost::mutex::scoped_lock lock (_mutex);
150 _queued_full_in_memory > _maximum_frames_in_memory ||
151 (!_queue.empty() && _queue.front().frame == (_last_written_frame + 1))) {
156 TIMING (N_("writer sleeps with a queue of %1"), _queue.size());
157 _condition.wait (lock);
158 TIMING (N_("writer wakes with a queue of %1"), _queue.size());
161 if (_finish && _queue.empty()) {
165 /* Write any frames that we can write; i.e. those that are in sequence */
166 while (!_queue.empty() && _queue.front().frame == (_last_written_frame + 1)) {
167 QueueItem qi = _queue.front ();
169 if (qi.type == QueueItem::FULL && qi.encoded) {
170 --_queued_full_in_memory;
175 case QueueItem::FULL:
177 _film->log()->log (String::compose (N_("Writer FULL-writes %1 to MXF"), qi.frame));
179 qi.encoded.reset (new EncodedData (_film->j2c_path (qi.frame, false)));
181 libdcp::FrameInfo const fin = _picture_asset_writer->write (qi.encoded->data(), qi.encoded->size());
182 qi.encoded->write_info (_film, qi.frame, fin);
183 _last_written = qi.encoded;
187 case QueueItem::FAKE:
188 _film->log()->log (String::compose (N_("Writer FAKE-writes %1 to MXF"), qi.frame));
189 _picture_asset_writer->fake_write (qi.size);
190 _last_written.reset ();
193 case QueueItem::REPEAT:
195 _film->log()->log (String::compose (N_("Writer REPEAT-writes %1 to MXF"), qi.frame));
196 libdcp::FrameInfo const fin = _picture_asset_writer->write (_last_written->data(), _last_written->size());
197 _last_written->write_info (_film, qi.frame, fin);
204 ++_last_written_frame;
207 while (_queued_full_in_memory > _maximum_frames_in_memory) {
208 /* Too many frames in memory which can't yet be written to the stream.
209 Write some FULL frames to disk.
213 list<QueueItem>::reverse_iterator i = _queue.rbegin ();
214 while (i != _queue.rend() && (i->type != QueueItem::FULL || !i->encoded)) {
218 assert (i != _queue.rend());
224 _film->log()->log (String::compose (N_("Writer full (awaiting %1); pushes %2 to disk"), _last_written_frame + 1, qi.frame));
225 qi.encoded->write (_film, qi.frame);
228 --_queued_full_in_memory;
244 boost::mutex::scoped_lock lock (_mutex);
246 _condition.notify_all ();
257 _picture_asset_writer->finalize ();
259 if (_sound_asset_writer) {
260 _sound_asset_writer->finalize ();
263 int const frames = _last_written_frame + 1;
264 int const duration = frames - _film->trim_start() - _film->trim_end();
266 _picture_asset->set_entry_point (_film->trim_start ());
267 _picture_asset->set_duration (duration);
269 /* Hard-link the video MXF into the DCP */
271 boost::filesystem::path from;
272 from /= _film->video_mxf_dir();
273 from /= _film->video_mxf_filename();
275 boost::filesystem::path to;
276 to /= _film->dir (_film->dcp_name());
277 to /= N_("video.mxf");
279 boost::system::error_code ec;
280 boost::filesystem::create_hard_link (from, to, ec);
282 /* hard link failed; copy instead */
283 boost::filesystem::copy_file (from, to);
284 _film->log()->log ("Hard-link failed; fell back to copying");
287 /* And update the asset */
289 _picture_asset->set_directory (_film->dir (_film->dcp_name ()));
290 _picture_asset->set_file_name (N_("video.mxf"));
293 _sound_asset->set_entry_point (_film->trim_start ());
294 _sound_asset->set_duration (duration);
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_frame_rate ()
311 cpl->add_reel (shared_ptr<libdcp::Reel> (new libdcp::Reel (
314 shared_ptr<libdcp::SubtitleAsset> ()
320 _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));
323 /** Tell the writer that frame `f' should be a repeat of the frame before it */
325 Writer::repeat (int f)
327 boost::mutex::scoped_lock lock (_mutex);
330 qi.type = QueueItem::REPEAT;
333 _queue.push_back (qi);
335 _condition.notify_all ();
340 Writer::check_existing_picture_mxf ()
342 /* Try to open the existing MXF */
343 boost::filesystem::path p;
344 p /= _film->video_mxf_dir ();
345 p /= _film->video_mxf_filename ();
346 FILE* mxf = fopen (p.string().c_str(), N_("rb"));
353 /* Read the frame info as written */
354 ifstream ifi (_film->info_path (_first_nonexistant_frame).c_str());
355 libdcp::FrameInfo info (ifi);
357 /* Read the data from the MXF and hash it */
358 fseek (mxf, info.offset, SEEK_SET);
359 EncodedData data (info.size);
360 fread (data.data(), 1, data.size(), mxf);
361 string const existing_hash = md5_digest (data.data(), data.size());
363 if (existing_hash != info.hash) {
364 _film->log()->log (String::compose (N_("Existing frame %1 failed hash check"), _first_nonexistant_frame));
368 _film->log()->log (String::compose (N_("Have existing frame %1"), _first_nonexistant_frame));
369 ++_first_nonexistant_frame;
375 /** @param frame Frame index.
376 * @return true if we can fake-write this frame.
379 Writer::can_fake_write (int frame) const
381 /* We have to do a proper write of the first frame so that we can set up the JPEG2000
382 parameters in the MXF writer.
384 return (frame != 0 && frame < _first_nonexistant_frame);
388 operator< (QueueItem const & a, QueueItem const & b)
390 return a.frame < b.frame;
394 operator== (QueueItem const & a, QueueItem const & b)
396 return a.frame == b.frame;