c8a007f78aa5aff7454686d1382c96134c9ede45
[dcpomatic.git] / src / lib / writer.h
1 /*
2     Copyright (C) 2012-2020 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 /** @file  src/lib/writer.h
22  *  @brief Writer class.
23  */
24
25 #include "atmos_metadata.h"
26 #include "types.h"
27 #include "player_text.h"
28 #include "exception_store.h"
29 #include "dcp_text_track.h"
30 #include "weak_film.h"
31 #include <dcp/atmos_frame.h>
32 #include <boost/thread.hpp>
33 #include <boost/thread/condition.hpp>
34 #include <list>
35
36 namespace dcp {
37         class Data;
38 }
39
40 namespace dcpomatic {
41         class FontData;
42 }
43
44 class Film;
45 class AudioBuffers;
46 class Job;
47 class ReferencedReelAsset;
48 class ReelWriter;
49
50 struct QueueItem
51 {
52 public:
53         QueueItem ()
54                 : size (0)
55                 , reel (0)
56                 , frame (0)
57                 , eyes (EYES_BOTH)
58         {}
59
60         enum Type {
61                 /** a normal frame with some JPEG200 data */
62                 FULL,
63                 /** a frame whose data already exists in the MXF,
64                     and we fake-write it; i.e. we update the writer's
65                     state but we use the data that is already on disk.
66                 */
67                 FAKE,
68                 REPEAT,
69         } type;
70
71         /** encoded data for FULL */
72         std::shared_ptr<const dcp::Data> encoded;
73         /** size of data for FAKE */
74         int size;
75         /** reel index */
76         size_t reel;
77         /** frame index within the reel */
78         int frame;
79         /** eyes for FULL, FAKE and REPEAT */
80         Eyes eyes;
81 };
82
83 bool operator< (QueueItem const & a, QueueItem const & b);
84 bool operator== (QueueItem const & a, QueueItem const & b);
85
86 /** @class Writer
87  *  @brief Class to manage writing JPEG2000 and audio data to assets on disk.
88  *
89  *  This class creates sound and picture assets, then takes Data
90  *  or AudioBuffers objects (containing image or sound data respectively)
91  *  and writes them to the assets.
92  *
93  *  write() for Data (picture) can be called out of order, and the Writer
94  *  will sort it out.  write() for AudioBuffers must be called in order.
95  */
96
97 class Writer : public ExceptionStore, public boost::noncopyable, public WeakConstFilm
98 {
99 public:
100         Writer (std::weak_ptr<const Film>, std::weak_ptr<Job>, bool text_only = false);
101         ~Writer ();
102
103         void start ();
104
105         bool can_fake_write (Frame) const;
106
107         void write (std::shared_ptr<const dcp::Data>, Frame, Eyes);
108         void fake_write (Frame, Eyes);
109         bool can_repeat (Frame) const;
110         void repeat (Frame, Eyes);
111         void write (std::shared_ptr<const AudioBuffers>, dcpomatic::DCPTime time);
112         void write (PlayerText text, TextType type, boost::optional<DCPTextTrack>, dcpomatic::DCPTimePeriod period);
113         void write (std::vector<dcpomatic::FontData> fonts);
114         void write (ReferencedReelAsset asset);
115         void write (std::shared_ptr<const dcp::AtmosFrame> atmos, dcpomatic::DCPTime time, AtmosMetadata metadata);
116         void finish (boost::filesystem::path output_dcp);
117
118         void set_encoder_threads (int threads);
119
120 private:
121         void thread ();
122         void terminate_thread (bool);
123         bool have_sequenced_image_at_queue_head ();
124         size_t video_reel (int frame) const;
125         void set_digest_progress (Job* job, float progress);
126         void write_cover_sheet (boost::filesystem::path output_dcp);
127         void calculate_referenced_digests (boost::function<void (float)> set_progress);
128
129         std::weak_ptr<Job> _job;
130         std::vector<ReelWriter> _reels;
131         std::vector<ReelWriter>::iterator _audio_reel;
132         std::vector<ReelWriter>::iterator _subtitle_reel;
133         std::map<DCPTextTrack, std::vector<ReelWriter>::iterator> _caption_reels;
134         std::vector<ReelWriter>::iterator _atmos_reel;
135
136         /** our thread */
137         boost::thread _thread;
138         /** true if our thread should finish */
139         bool _finish;
140         /** queue of things to write to disk */
141         std::list<QueueItem> _queue;
142         /** number of FULL frames whose JPEG200 data is currently held in RAM */
143         int _queued_full_in_memory;
144         /** mutex for thread state */
145         mutable boost::mutex _state_mutex;
146         /** condition to manage thread wakeups when we have nothing to do  */
147         boost::condition _empty_condition;
148         /** condition to manage thread wakeups when we have too much to do */
149         boost::condition _full_condition;
150         /** maximum number of frames to hold in memory, for when we are managing
151          *  ordering
152          */
153         int _maximum_frames_in_memory;
154         unsigned int _maximum_queue_size;
155
156         class LastWritten
157         {
158         public:
159                 LastWritten()
160                         : _frame(-1)
161                         , _eyes(EYES_RIGHT)
162                 {}
163
164                 /** @return true if qi is the next item after this one */
165                 bool next (QueueItem qi) const;
166                 void update (QueueItem qi);
167
168                 int frame () const {
169                         return _frame;
170                 }
171
172         private:
173                 int _frame;
174                 Eyes _eyes;
175         };
176
177         /** The last frame written to each reel */
178         std::vector<LastWritten> _last_written;
179
180         /** number of FULL written frames */
181         int _full_written;
182         /** number of FAKE written frames */
183         int _fake_written;
184         int _repeat_written;
185         /** number of frames pushed to disk and then recovered
186             due to the limit of frames to be held in memory.
187         */
188         int _pushed_to_disk;
189
190         bool _text_only;
191
192         boost::mutex _digest_progresses_mutex;
193         std::map<boost::thread::id, float> _digest_progresses;
194
195         std::list<ReferencedReelAsset> _reel_assets;
196
197         std::vector<dcpomatic::FontData> _fonts;
198
199         /** true if any reel has any subtitles */
200         bool _have_subtitles;
201         /** all closed caption tracks that we have on any reel */
202         std::set<DCPTextTrack> _have_closed_captions;
203 };