Rest of src/lib/*.h tidying.
[dcpomatic.git] / src / lib / writer.h
1 /*
2     Copyright (C) 2012-2015 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 /** @file  src/lib/writer.h
21  *  @brief Writer class.
22  */
23
24 #include "types.h"
25 #include "player_subtitles.h"
26 #include "data.h"
27 #include <dcp/picture_asset_writer.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 class Film;
35 class Data;
36 class AudioBuffers;
37 class Job;
38 class Font;
39
40 namespace dcp {
41         class MonoPictureAsset;
42         class MonoPictureAssetWriter;
43         class StereoPictureAsset;
44         class StereoPictureAssetWriter;
45         class PictureAsset;
46         class PictureAssetWriter;
47         class SoundAsset;
48         class SoundAssetWriter;
49         class SubtitleAsset;
50 }
51
52 struct QueueItem
53 {
54 public:
55         QueueItem ()
56                 : size (0)
57         {}
58
59         enum Type {
60                 /** a normal frame with some JPEG200 data */
61                 FULL,
62                 /** a frame whose data already exists in the MXF,
63                     and we fake-write it; i.e. we update the writer's
64                     state but we use the data that is already on disk.
65                 */
66                 FAKE,
67                 REPEAT,
68         } type;
69
70         /** encoded data for FULL */
71         boost::optional<Data> encoded;
72         /** size of data for FAKE */
73         int size;
74         /** frame index */
75         int frame;
76         Eyes eyes;
77 };
78
79 bool operator< (QueueItem const & a, QueueItem const & b);
80 bool operator== (QueueItem const & a, QueueItem const & b);
81
82 /** @class Writer
83  *  @brief Class to manage writing JPEG2000 and audio data to assets on disk.
84  *
85  *  This class creates sound and picture assets, then takes Data
86  *  or AudioBuffers objects (containing image or sound data respectively)
87  *  and writes them to the assets.
88  *
89  *  ::write() for Data can be called out of order, and the Writer
90  *  will sort it out.  write() for AudioBuffers must be called in order.
91  */
92
93 class Writer : public ExceptionStore, public boost::noncopyable
94 {
95 public:
96         Writer (boost::shared_ptr<const Film>, boost::weak_ptr<Job>);
97         ~Writer ();
98
99         bool can_fake_write (int) const;
100
101         void write (Data, int, Eyes);
102         void fake_write (int, Eyes);
103         void repeat (int, Eyes);
104         void write (boost::shared_ptr<const AudioBuffers>);
105         void write (PlayerSubtitles subs);
106         void write (std::list<boost::shared_ptr<Font> > fonts);
107         void finish ();
108
109         void set_encoder_threads (int threads);
110
111 private:
112
113         void thread ();
114         void terminate_thread (bool);
115         void check_existing_picture_asset ();
116         bool check_existing_picture_asset_frame (FILE *, int, Eyes);
117         bool have_sequenced_image_at_queue_head ();
118         void write_frame_info (int frame, Eyes eyes, dcp::FrameInfo info) const;
119         long frame_info_position (int frame, Eyes eyes) const;
120         dcp::FrameInfo read_frame_info (FILE* file, int frame, Eyes eyes) const;
121
122         /** our Film */
123         boost::shared_ptr<const Film> _film;
124         boost::weak_ptr<Job> _job;
125         /** the first frame index that does not already exist in our MXF */
126         int _first_nonexistant_frame;
127
128         /** our thread, or 0 */
129         boost::thread* _thread;
130         /** true if our thread should finish */
131         bool _finish;
132         /** queue of things to write to disk */
133         std::list<QueueItem> _queue;
134         /** number of FULL frames whose JPEG200 data is currently held in RAM */
135         int _queued_full_in_memory;
136         /** mutex for thread state */
137         mutable boost::mutex _mutex;
138         /** condition to manage thread wakeups when we have nothing to do  */
139         boost::condition _empty_condition;
140         /** condition to manage thread wakeups when we have too much to do */
141         boost::condition _full_condition;
142         /** the data of the last written frame, if there is one */
143         boost::optional<Data> _last_written[EYES_COUNT];
144         /** the index of the last written frame */
145         int _last_written_frame;
146         Eyes _last_written_eyes;
147         /** maximum number of frames to hold in memory, for when we are managing
148          *  ordering
149          */
150         int _maximum_frames_in_memory;
151
152         /** number of FULL written frames */
153         int _full_written;
154         /** number of FAKE written frames */
155         int _fake_written;
156         int _repeat_written;
157         /** number of frames pushed to disk and then recovered
158             due to the limit of frames to be held in memory.
159         */
160         int _pushed_to_disk;
161
162         boost::shared_ptr<dcp::PictureAsset> _picture_asset;
163         boost::shared_ptr<dcp::PictureAssetWriter> _picture_asset_writer;
164         boost::shared_ptr<dcp::SoundAsset> _sound_asset;
165         boost::shared_ptr<dcp::SoundAssetWriter> _sound_asset_writer;
166         boost::shared_ptr<dcp::SubtitleAsset> _subtitle_asset;
167
168         std::list<boost::shared_ptr<Font> > _fonts;
169 };