No-op; fix GPL address and use the explicit-program-name version.
[dcpomatic.git] / src / lib / writer.h
1 /*
2     Copyright (C) 2012-2015 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 "types.h"
26 #include "player_subtitles.h"
27 #include "exception_store.h"
28 #include <boost/shared_ptr.hpp>
29 #include <boost/weak_ptr.hpp>
30 #include <boost/thread.hpp>
31 #include <boost/thread/condition.hpp>
32 #include <list>
33
34 namespace dcp {
35         class Data;
36 }
37
38 class Film;
39 class AudioBuffers;
40 class Job;
41 class Font;
42 class ReferencedReelAsset;
43 class ReelWriter;
44
45 struct QueueItem
46 {
47 public:
48         QueueItem ()
49                 : size (0)
50         {}
51
52         enum Type {
53                 /** a normal frame with some JPEG200 data */
54                 FULL,
55                 /** a frame whose data already exists in the MXF,
56                     and we fake-write it; i.e. we update the writer's
57                     state but we use the data that is already on disk.
58                 */
59                 FAKE,
60                 REPEAT,
61         } type;
62
63         /** encoded data for FULL */
64         boost::optional<dcp::Data> encoded;
65         /** size of data for FAKE */
66         int size;
67         /** reel index */
68         size_t reel;
69         /** frame index within the reel */
70         int frame;
71         /** eyes for FULL, FAKE and REPEAT */
72         Eyes eyes;
73 };
74
75 bool operator< (QueueItem const & a, QueueItem const & b);
76 bool operator== (QueueItem const & a, QueueItem const & b);
77
78 /** @class Writer
79  *  @brief Class to manage writing JPEG2000 and audio data to assets on disk.
80  *
81  *  This class creates sound and picture assets, then takes Data
82  *  or AudioBuffers objects (containing image or sound data respectively)
83  *  and writes them to the assets.
84  *
85  *  ::write() for Data (picture) can be called out of order, and the Writer
86  *  will sort it out.  write() for AudioBuffers must be called in order.
87  */
88
89 class Writer : public ExceptionStore, public boost::noncopyable
90 {
91 public:
92         Writer (boost::shared_ptr<const Film>, boost::weak_ptr<Job>);
93         ~Writer ();
94
95         void start ();
96
97         bool can_fake_write (Frame) const;
98
99         void write (dcp::Data, Frame, Eyes);
100         void fake_write (Frame, Eyes);
101         bool can_repeat (Frame) const;
102         void repeat (Frame, Eyes);
103         void write (boost::shared_ptr<const AudioBuffers>);
104         void write (PlayerSubtitles subs);
105         void write (std::list<boost::shared_ptr<Font> > fonts);
106         void write (ReferencedReelAsset asset);
107         void finish ();
108
109         void set_encoder_threads (int threads);
110
111 private:
112         void thread ();
113         void terminate_thread (bool);
114         bool have_sequenced_image_at_queue_head ();
115         size_t video_reel (int frame) const;
116
117         /** our Film */
118         boost::shared_ptr<const Film> _film;
119         boost::weak_ptr<Job> _job;
120         std::vector<ReelWriter> _reels;
121         std::vector<ReelWriter>::iterator _audio_reel;
122         std::vector<ReelWriter>::iterator _subtitle_reel;
123
124         /** our thread, or 0 */
125         boost::thread* _thread;
126         /** true if our thread should finish */
127         bool _finish;
128         /** queue of things to write to disk */
129         std::list<QueueItem> _queue;
130         /** number of FULL frames whose JPEG200 data is currently held in RAM */
131         int _queued_full_in_memory;
132         /** mutex for thread state */
133         mutable boost::mutex _state_mutex;
134         /** condition to manage thread wakeups when we have nothing to do  */
135         boost::condition _empty_condition;
136         /** condition to manage thread wakeups when we have too much to do */
137         boost::condition _full_condition;
138         /** maximum number of frames to hold in memory, for when we are managing
139          *  ordering
140          */
141         int _maximum_frames_in_memory;
142
143         /** number of FULL written frames */
144         int _full_written;
145         /** number of FAKE written frames */
146         int _fake_written;
147         int _repeat_written;
148         /** number of frames pushed to disk and then recovered
149             due to the limit of frames to be held in memory.
150         */
151         int _pushed_to_disk;
152
153         std::list<ReferencedReelAsset> _reel_assets;
154
155         std::list<boost::shared_ptr<Font> > _fonts;
156 };