Missed update to private test repo version.
[dcpomatic.git] / src / lib / writer.h
1 /*
2     Copyright (C) 2012-2021 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
22 /** @file  src/lib/writer.h
23  *  @brief Writer class.
24  */
25
26
27 #include "atmos_metadata.h"
28 #include "dcp_text_track.h"
29 #include "dcpomatic_time.h"
30 #include "exception_store.h"
31 #include "font_id_map.h"
32 #include "player_text.h"
33 #include "types.h"
34 #include "weak_film.h"
35 #include <dcp/atmos_frame.h>
36 #include <boost/thread.hpp>
37 #include <boost/thread/condition.hpp>
38 #include <list>
39
40
41 namespace dcp {
42         class Data;
43 }
44
45 class AudioBuffers;
46 class Film;
47 class Job;
48 class ReelWriter;
49 class ReferencedReelAsset;
50 struct writer_disambiguate_font_ids1;
51 struct writer_disambiguate_font_ids2;
52 struct writer_disambiguate_font_ids3;
53
54
55 struct QueueItem
56 {
57 public:
58         QueueItem () {}
59
60         enum class 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 = 0;
75         /** reel index */
76         size_t reel = 0;
77         /** frame index within the reel */
78         int frame = 0;
79         /** eyes for FULL, FAKE and REPEAT */
80         Eyes eyes = Eyes::BOTH;
81 };
82
83
84 bool operator< (QueueItem const & a, QueueItem const & b);
85 bool operator== (QueueItem const & a, QueueItem const & b);
86
87
88 /** @class Writer
89  *  @brief Class to manage writing JPEG2000 and audio data to assets on disk.
90  *
91  *  This class creates sound and picture assets, then takes Data
92  *  or AudioBuffers objects (containing image or sound data respectively)
93  *  and writes them to the assets.
94  *
95  *  write() for Data (picture) can be called out of order, and the Writer
96  *  will sort it out.  write() for AudioBuffers must be called in order.
97  */
98
99 class Writer : public ExceptionStore, public WeakConstFilm
100 {
101 public:
102         Writer (std::weak_ptr<const Film>, std::weak_ptr<Job>, bool text_only = false);
103         ~Writer ();
104
105         Writer (Writer const &) = delete;
106         Writer& operator= (Writer const &) = delete;
107
108         void start ();
109
110         bool can_fake_write (Frame) const;
111
112         void write (std::shared_ptr<const dcp::Data>, Frame, Eyes);
113         void fake_write (Frame, Eyes);
114         bool can_repeat (Frame) const;
115         void repeat (Frame, Eyes);
116         void write (std::shared_ptr<const AudioBuffers>, dcpomatic::DCPTime time);
117         void write (PlayerText text, TextType type, boost::optional<DCPTextTrack>, dcpomatic::DCPTimePeriod period);
118         void write (std::vector<std::shared_ptr<dcpomatic::Font>> fonts);
119         void write (ReferencedReelAsset asset);
120         void write (std::shared_ptr<const dcp::AtmosFrame> atmos, dcpomatic::DCPTime time, AtmosMetadata metadata);
121         void finish (boost::filesystem::path output_dcp);
122
123         void set_encoder_threads (int threads);
124
125 private:
126         friend struct ::writer_disambiguate_font_ids1;
127         friend struct ::writer_disambiguate_font_ids2;
128         friend struct ::writer_disambiguate_font_ids3;
129
130         void thread ();
131         void terminate_thread (bool);
132         bool have_sequenced_image_at_queue_head ();
133         size_t video_reel (int frame) const;
134         void set_digest_progress (Job* job, float progress);
135         void write_cover_sheet (boost::filesystem::path output_dcp);
136         void calculate_referenced_digests (std::function<void (float)> set_progress);
137         void write_hanging_text (ReelWriter& reel);
138         void calculate_digests ();
139
140         std::weak_ptr<Job> _job;
141         std::vector<ReelWriter> _reels;
142         std::vector<ReelWriter>::iterator _audio_reel;
143         std::vector<ReelWriter>::iterator _subtitle_reel;
144         std::map<DCPTextTrack, std::vector<ReelWriter>::iterator> _caption_reels;
145         std::vector<ReelWriter>::iterator _atmos_reel;
146
147         /** our thread */
148         boost::thread _thread;
149         /** true if our thread should finish */
150         bool _finish = false;
151         /** queue of things to write to disk */
152         std::list<QueueItem> _queue;
153         /** number of FULL frames whose JPEG200 data is currently held in RAM */
154         int _queued_full_in_memory = 0;
155         /** mutex for thread state */
156         mutable boost::mutex _state_mutex;
157         /** condition to manage thread wakeups when we have nothing to do  */
158         boost::condition _empty_condition;
159         /** condition to manage thread wakeups when we have too much to do */
160         boost::condition _full_condition;
161         /** maximum number of frames to hold in memory, for when we are managing
162          *  ordering
163          */
164         int _maximum_frames_in_memory;
165         unsigned int _maximum_queue_size;
166
167         class LastWritten
168         {
169         public:
170                 LastWritten()
171                         : _frame(-1)
172                         , _eyes(Eyes::RIGHT)
173                 {}
174
175                 /** @return true if qi is the next item after this one */
176                 bool next (QueueItem qi) const;
177                 void update (QueueItem qi);
178
179                 int frame () const {
180                         return _frame;
181                 }
182
183         private:
184                 int _frame;
185                 Eyes _eyes;
186         };
187
188         /** The last frame written to each reel */
189         std::vector<LastWritten> _last_written;
190
191         /** number of FULL written frames */
192         int _full_written = 0;
193         /** number of FAKE written frames */
194         int _fake_written = 0;
195         int _repeat_written = 0;
196         /** number of frames pushed to disk and then recovered
197             due to the limit of frames to be held in memory.
198         */
199         int _pushed_to_disk = 0;
200
201         bool _text_only;
202
203         boost::mutex _digest_progresses_mutex;
204         std::map<boost::thread::id, float> _digest_progresses;
205
206         std::list<ReferencedReelAsset> _reel_assets;
207
208         FontIdMap _fonts;
209         /** If we are given many fonts, but we're making an Interop DCP, we'll choose a single
210          *  one that we'll use everywher.  This is that chosen font.
211          */
212         std::shared_ptr<dcpomatic::Font> _chosen_interop_font;
213
214         /** true if any reel has any subtitles */
215         bool _have_subtitles = false;
216         /** all closed caption tracks that we have on any reel */
217         std::set<DCPTextTrack> _have_closed_captions;
218
219         struct HangingText {
220                 PlayerText text;
221                 TextType type;
222                 boost::optional<DCPTextTrack> track;
223                 dcpomatic::DCPTimePeriod period;
224         };
225
226         std::vector<HangingText> _hanging_texts;
227 };