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