Trying to create export audio encoders with between 9 and 15 channels
[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 <dcp/atmos_frame.h>
31 #include <boost/shared_ptr.hpp>
32 #include <boost/weak_ptr.hpp>
33 #include <boost/thread.hpp>
34 #include <boost/thread/condition.hpp>
35 #include <list>
36
37 namespace dcp {
38         class Data;
39 }
40
41 namespace dcpomatic {
42         class Font;
43 }
44
45 class Film;
46 class AudioBuffers;
47 class Job;
48 class ReferencedReelAsset;
49 class ReelWriter;
50
51 struct QueueItem
52 {
53 public:
54         QueueItem ()
55                 : size (0)
56                 , reel (0)
57                 , frame (0)
58                 , eyes (EYES_BOTH)
59         {}
60
61         enum Type {
62                 /** a normal frame with some JPEG200 data */
63                 FULL,
64                 /** a frame whose data already exists in the MXF,
65                     and we fake-write it; i.e. we update the writer's
66                     state but we use the data that is already on disk.
67                 */
68                 FAKE,
69                 REPEAT,
70         } type;
71
72         /** encoded data for FULL */
73         boost::optional<dcp::Data> encoded;
74         /** size of data for FAKE */
75         int size;
76         /** reel index */
77         size_t reel;
78         /** frame index within the reel */
79         int frame;
80         /** eyes for FULL, FAKE and REPEAT */
81         Eyes eyes;
82 };
83
84 bool operator< (QueueItem const & a, QueueItem const & b);
85 bool operator== (QueueItem const & a, QueueItem const & b);
86
87 /** @class Writer
88  *  @brief Class to manage writing JPEG2000 and audio data to assets on disk.
89  *
90  *  This class creates sound and picture assets, then takes Data
91  *  or AudioBuffers objects (containing image or sound data respectively)
92  *  and writes them to the assets.
93  *
94  *  write() for Data (picture) can be called out of order, and the Writer
95  *  will sort it out.  write() for AudioBuffers must be called in order.
96  */
97
98 class Writer : public ExceptionStore, public boost::noncopyable
99 {
100 public:
101         Writer (boost::shared_ptr<const Film>, boost::weak_ptr<Job>);
102         ~Writer ();
103
104         void start ();
105
106         bool can_fake_write (Frame) const;
107
108         void write (dcp::Data, Frame, Eyes);
109         void fake_write (Frame, Eyes);
110         bool can_repeat (Frame) const;
111         void repeat (Frame, Eyes);
112         void write (boost::shared_ptr<const AudioBuffers>, dcpomatic::DCPTime time);
113         void write (PlayerText text, TextType type, boost::optional<DCPTextTrack>, dcpomatic::DCPTimePeriod period);
114         void write (std::list<boost::shared_ptr<dcpomatic::Font> > fonts);
115         void write (ReferencedReelAsset asset);
116         void write (boost::shared_ptr<const dcp::AtmosFrame> atmos, dcpomatic::DCPTime time, AtmosMetadata metadata);
117         void finish ();
118
119         void set_encoder_threads (int threads);
120
121 private:
122         void thread ();
123         void terminate_thread (bool);
124         bool have_sequenced_image_at_queue_head ();
125         size_t video_reel (int frame) const;
126         void set_digest_progress (Job* job, float progress);
127         void write_cover_sheet ();
128
129         /** our Film */
130         boost::shared_ptr<const Film> _film;
131         boost::weak_ptr<Job> _job;
132         std::vector<ReelWriter> _reels;
133         std::vector<ReelWriter>::iterator _audio_reel;
134         std::vector<ReelWriter>::iterator _subtitle_reel;
135         std::map<DCPTextTrack, std::vector<ReelWriter>::iterator> _caption_reels;
136         std::vector<ReelWriter>::iterator _atmos_reel;
137
138         /** our thread */
139         boost::thread _thread;
140         /** true if our thread should finish */
141         bool _finish;
142         /** queue of things to write to disk */
143         std::list<QueueItem> _queue;
144         /** number of FULL frames whose JPEG200 data is currently held in RAM */
145         int _queued_full_in_memory;
146         /** mutex for thread state */
147         mutable boost::mutex _state_mutex;
148         /** condition to manage thread wakeups when we have nothing to do  */
149         boost::condition _empty_condition;
150         /** condition to manage thread wakeups when we have too much to do */
151         boost::condition _full_condition;
152         /** maximum number of frames to hold in memory, for when we are managing
153          *  ordering
154          */
155         int _maximum_frames_in_memory;
156         unsigned int _maximum_queue_size;
157
158         class LastWritten
159         {
160         public:
161                 LastWritten()
162                         : _frame(-1)
163                         , _eyes(EYES_RIGHT)
164                 {}
165
166                 /** @return true if qi is the next item after this one */
167                 bool next (QueueItem qi) const;
168                 void update (QueueItem qi);
169
170                 int frame () const {
171                         return _frame;
172                 }
173
174         private:
175                 int _frame;
176                 Eyes _eyes;
177         };
178
179         /** The last frame written to each reel */
180         std::vector<LastWritten> _last_written;
181
182         /** number of FULL written frames */
183         int _full_written;
184         /** number of FAKE written frames */
185         int _fake_written;
186         int _repeat_written;
187         /** number of frames pushed to disk and then recovered
188             due to the limit of frames to be held in memory.
189         */
190         int _pushed_to_disk;
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::list<boost::shared_ptr<dcpomatic::Font> > _fonts;
198 };