3ec88860a2cb8fda70bcdd2573c7b7c75be2b830
[dcpomatic.git] / src / lib / writer.cc
1 /*
2     Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
3
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.
8
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.
13
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.
17
18 */
19
20 #include <fstream>
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 "writer.h"
26 #include "compose.hpp"
27 #include "film.h"
28 #include "format.h"
29 #include "log.h"
30 #include "dcp_video_frame.h"
31
32 using std::make_pair;
33 using std::pair;
34 using std::string;
35 using std::ifstream;
36 using std::cout;
37 using boost::shared_ptr;
38
39 unsigned int const Writer::_maximum_frames_in_memory = 8;
40
41 Writer::Writer (shared_ptr<Film> f)
42         : _film (f)
43         , _first_nonexistant_frame (0)
44         , _thread (0)
45         , _finish (false)
46         , _last_written_frame (-1)
47 {
48         check_existing_picture_mxf ();
49         
50         /* Create our picture asset in a subdirectory, named according to the
51            film's parameters which affect the video output.  We will hard-link
52            it into the DCP later.
53         */
54         
55         _picture_asset.reset (
56                 new libdcp::MonoPictureAsset (
57                         _film->video_mxf_dir (),
58                         _film->video_mxf_filename (),
59                         DCPFrameRate (_film->frames_per_second()).frames_per_second,
60                         _film->format()->dcp_size()
61                         )
62                 );
63         
64         _picture_asset_writer = _picture_asset->start_write ();
65
66         if (_film->audio_channels() > 0) {
67                 _sound_asset.reset (
68                         new libdcp::SoundAsset (
69                                 _film->dir (_film->dcp_name()),
70                                 "audio.mxf",
71                                 DCPFrameRate (_film->frames_per_second()).frames_per_second,
72                                 _film->audio_channels(),
73                                 dcp_audio_sample_rate (_film->audio_stream()->sample_rate())
74                                 )
75                         );
76
77                 _sound_asset_writer = _sound_asset->start_write ();
78         }
79         
80         _thread = new boost::thread (boost::bind (&Writer::thread, this));
81 }
82
83 void
84 Writer::write (shared_ptr<const EncodedData> encoded, int frame)
85 {
86         boost::mutex::scoped_lock lock (_mutex);
87         _queue.push_back (make_pair (encoded, frame));
88         _condition.notify_all ();
89 }
90
91 /** This method is not thread safe */
92 void
93 Writer::write (shared_ptr<const AudioBuffers> audio)
94 {
95         _sound_asset_writer->write (audio->data(), audio->frames());
96 }
97
98 struct QueueSorter
99 {
100         bool operator() (pair<shared_ptr<const EncodedData>, int> const & a, pair<shared_ptr<const EncodedData>, int> const & b) {
101                 return a.second < b.second;
102         }
103 };
104
105 void
106 Writer::thread ()
107 {
108         while (1)
109         {
110                 boost::mutex::scoped_lock lock (_mutex);
111
112                 while (1) {
113                         if (_finish ||
114                             _queue.size() > _maximum_frames_in_memory ||
115                             (!_queue.empty() && _queue.front().second == (_last_written_frame + 1))) {
116                                     
117                                     break;
118                             }
119
120                             TIMING ("writer sleeps with a queue of %1; %2 pending", _queue.size(), _pending.size());
121                             _condition.wait (lock);
122                             TIMING ("writer wakes with a queue of %1", _queue.size());
123
124                             _queue.sort (QueueSorter ());
125                 }
126
127                 if (_finish && _queue.empty() && _pending.empty()) {
128                         return;
129                 }
130
131                 /* Write any frames that we can write; i.e. those that are in sequence */
132                 while (!_queue.empty() && _queue.front().second == (_last_written_frame + 1)) {
133                         pair<boost::shared_ptr<const EncodedData>, int> encoded = _queue.front ();
134                         _queue.pop_front ();
135
136                         lock.unlock ();
137                         _film->log()->log (String::compose ("Writer writes %1 to MXF", encoded.second));
138                         if (encoded.first) {
139                                 _picture_asset_writer->write (encoded.first->data(), encoded.first->size());
140                                 encoded.first->write_hash (_film, encoded.second);
141                                 _last_written = encoded.first;
142                         } else {
143                                 _picture_asset_writer->write (_last_written->data(), _last_written->size());
144                                 _last_written->write_hash (_film, encoded.second);
145                         }
146                         lock.lock ();
147
148                         ++_last_written_frame;
149                 }
150
151                 while (_queue.size() > _maximum_frames_in_memory) {
152                         /* Too many frames in memory which can't yet be written to the stream.
153                            Put some to disk.
154                         */
155
156                         pair<boost::shared_ptr<const EncodedData>, int> encoded = _queue.back ();
157                         _queue.pop_back ();
158                         if (!encoded.first) {
159                                 /* This is a `repeat-last' frame, so no need to write it to disk */
160                                 continue;
161                         }
162
163                         lock.unlock ();
164                         _film->log()->log (String::compose ("Writer full (awaiting %1); pushes %2 to disk", _last_written_frame + 1, encoded.second));
165                         encoded.first->write (_film, encoded.second);
166                         lock.lock ();
167
168                         _pending.push_back (encoded.second);
169                 }
170
171                 while (_queue.size() < _maximum_frames_in_memory && !_pending.empty()) {
172                         /* We have some space in memory.  Fetch some frames back off disk. */
173
174                         _pending.sort ();
175                         int const fetch = _pending.front ();
176
177                         lock.unlock ();
178                         _film->log()->log (String::compose ("Writer pulls %1 back from disk", fetch));
179                         shared_ptr<const EncodedData> encoded;
180                         if (boost::filesystem::exists (_film->j2c_path (fetch, false))) {
181                                 /* It's an actual frame (not a repeat-last); load it in */
182                                 encoded.reset (new EncodedData (_film->j2c_path (fetch, false)));
183                         }
184                         lock.lock ();
185
186                         _queue.push_back (make_pair (encoded, fetch));
187                         _pending.remove (fetch);
188                 }
189         }
190
191 }
192
193 void
194 Writer::finish ()
195 {
196         if (!_thread) {
197                 return;
198         }
199         
200         boost::mutex::scoped_lock lock (_mutex);
201         _finish = true;
202         _condition.notify_all ();
203         lock.unlock ();
204
205         _thread->join ();
206         delete _thread;
207         _thread = 0;
208
209         _picture_asset_writer->finalize ();
210
211         if (_sound_asset_writer) {
212                 _sound_asset_writer->finalize ();
213         }
214
215         int const frames = _last_written_frame + 1;
216         int const duration = frames - _film->trim_start() - _film->trim_end();
217         
218         _film->set_dcp_intrinsic_duration (frames);
219         
220         _picture_asset->set_entry_point (_film->trim_start ());
221         _picture_asset->set_duration (duration);
222
223         /* Hard-link the video MXF into the DCP */
224
225         boost::filesystem::path from;
226         from /= _film->video_mxf_dir();
227         from /= _film->video_mxf_filename();
228         
229         boost::filesystem::path to;
230         to /= _film->dir (_film->dcp_name());
231         to /= "video.mxf";
232         
233         boost::filesystem::create_hard_link (from, to);
234
235         /* And update the asset */
236
237         _picture_asset->set_directory (_film->dir (_film->dcp_name ()));
238         _picture_asset->set_file_name ("video.mxf");
239
240         if (_sound_asset) {
241                 _sound_asset->set_entry_point (_film->trim_start ());
242                 _sound_asset->set_duration (duration);
243         }
244         
245         libdcp::DCP dcp (_film->dir (_film->dcp_name()));
246         DCPFrameRate dfr (_film->frames_per_second ());
247
248         shared_ptr<libdcp::CPL> cpl (
249                 new libdcp::CPL (_film->dir (_film->dcp_name()), _film->dcp_name(), _film->dcp_content_type()->libdcp_kind (), frames, dfr.frames_per_second)
250                 );
251         
252         dcp.add_cpl (cpl);
253
254         cpl->add_reel (shared_ptr<libdcp::Reel> (new libdcp::Reel (
255                                                          _picture_asset,
256                                                          _sound_asset,
257                                                          shared_ptr<libdcp::SubtitleAsset> ()
258                                                          )
259                                ));
260
261         dcp.write_xml ();
262 }
263
264 /** Tell the writer that frame `f' should be a repeat of the frame before it */
265 void
266 Writer::repeat (int f)
267 {
268         boost::mutex::scoped_lock lock (_mutex);
269         _queue.push_back (make_pair (shared_ptr<const EncodedData> (), f));
270 }
271
272 void
273 Writer::check_existing_picture_mxf ()
274 {
275         boost::filesystem::path p;
276         p /= _film->video_mxf_dir ();
277         p /= _film->video_mxf_filename ();
278         if (!boost::filesystem::exists (p)) {
279                 return;
280         }
281
282         libdcp::MonoPictureAsset existing (_film->video_mxf_dir(), _film->video_mxf_filename());
283
284         while (_first_nonexistant_frame < existing.intrinsic_duration()) {
285
286                 shared_ptr<const libdcp::MonoPictureFrame> f = existing.get_frame (_first_nonexistant_frame);
287                 string const existing_hash = md5_digest (f->j2k_data(), f->j2k_size());
288                 
289                 ifstream hf (_film->hash_path (_first_nonexistant_frame).c_str ());
290                 string reference_hash;
291                 hf >> reference_hash;
292
293                 if (existing_hash != reference_hash) {
294                         cout << "frame " << _first_nonexistant_frame << " failed hash check.\n";
295                         break;
296                 }
297
298                 cout << "frame " << _first_nonexistant_frame << " ok.\n";
299                 ++_first_nonexistant_frame;
300         }
301 }
302