Remove ref_write mechanism and instead maintain state for each
[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 "exception_store.h"
28 #include <dcp/picture_asset_writer.h>
29 #include <boost/shared_ptr.hpp>
30 #include <boost/weak_ptr.hpp>
31 #include <boost/thread.hpp>
32 #include <boost/thread/condition.hpp>
33 #include <list>
34
35 class Film;
36 class Data;
37 class AudioBuffers;
38 class Job;
39 class Font;
40 class ReferencedReelAsset;
41
42 namespace dcp {
43         class MonoPictureAsset;
44         class MonoPictureAssetWriter;
45         class StereoPictureAsset;
46         class StereoPictureAssetWriter;
47         class PictureAsset;
48         class PictureAssetWriter;
49         class SoundAsset;
50         class SoundAssetWriter;
51         class SubtitleAsset;
52         class ReelAsset;
53 }
54
55 struct QueueItem
56 {
57 public:
58         QueueItem ()
59                 : size (0)
60         {}
61
62         enum Type {
63                 /** a normal frame with some JPEG200 data */
64                 FULL,
65                 /** a frame whose data already exists in the MXF,
66                     and we fake-write it; i.e. we update the writer's
67                     state but we use the data that is already on disk.
68                 */
69                 FAKE,
70                 REPEAT,
71         } type;
72
73         /** encoded data for FULL */
74         boost::optional<Data> encoded;
75         /** size of data for FAKE */
76         int size;
77         /** reel index */
78         size_t reel;
79         /** frame index within the reel */
80         int frame;
81         /** eyes for FULL, FAKE and REPEAT */
82         Eyes eyes;
83 };
84
85 bool operator< (QueueItem const & a, QueueItem const & b);
86 bool operator== (QueueItem const & a, QueueItem const & b);
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 boost::noncopyable
100 {
101 public:
102         Writer (boost::shared_ptr<const Film>, boost::weak_ptr<Job>);
103         ~Writer ();
104
105         void start ();
106
107         bool can_fake_write (Frame) const;
108
109         void write (Data, Frame, Eyes);
110         void fake_write (Frame, Eyes);
111         void repeat (Frame, Eyes);
112         void write (boost::shared_ptr<const AudioBuffers>);
113         void write (PlayerSubtitles subs);
114         void write (std::list<boost::shared_ptr<Font> > fonts);
115         void write (ReferencedReelAsset asset);
116         void finish ();
117
118         void set_encoder_threads (int threads);
119
120 private:
121
122         class Reel {
123         public:
124                 Reel ()
125                         : first_nonexistant_frame (0)
126                         , last_written_video_frame (-1)
127                         , last_written_eyes (EYES_RIGHT)
128                         , total_written_audio_frames (0)
129                 {}
130
131                 DCPTimePeriod period;
132                 /** the first frame index that does not already exist in our MXF */
133                 int first_nonexistant_frame;
134                 /** the data of the last written frame, if there is one */
135                 boost::optional<Data> last_written[EYES_COUNT];
136                 /** the index of the last written video frame within the reel */
137                 int last_written_video_frame;
138                 Eyes last_written_eyes;
139                 /** the number of audio frames that have been written to the reel */
140                 int total_written_audio_frames;
141
142                 boost::shared_ptr<dcp::PictureAsset> picture_asset;
143                 boost::shared_ptr<dcp::PictureAssetWriter> picture_asset_writer;
144                 boost::shared_ptr<dcp::SoundAsset> sound_asset;
145                 boost::shared_ptr<dcp::SoundAssetWriter> sound_asset_writer;
146                 boost::shared_ptr<dcp::SubtitleAsset> subtitle_asset;
147         };
148
149         void thread ();
150         void terminate_thread (bool);
151         void check_existing_picture_asset (Reel& reel);
152         bool have_sequenced_image_at_queue_head ();
153         void write_frame_info (Reel const & reel, int frame, Eyes eyes, dcp::FrameInfo info) const;
154         long frame_info_position (int frame, Eyes eyes) const;
155         dcp::FrameInfo read_frame_info (FILE* file, int frame, Eyes eyes) const;
156         size_t video_reel (int frame) const;
157
158         /** our Film */
159         boost::shared_ptr<const Film> _film;
160         boost::weak_ptr<Job> _job;
161         std::vector<Reel> _reels;
162         std::vector<Reel>::iterator _audio_reel;
163         std::vector<Reel>::iterator _subtitle_reel;
164
165         /** our thread, or 0 */
166         boost::thread* _thread;
167         /** true if our thread should finish */
168         bool _finish;
169         /** queue of things to write to disk */
170         std::list<QueueItem> _queue;
171         /** number of FULL frames whose JPEG200 data is currently held in RAM */
172         int _queued_full_in_memory;
173         /** mutex for thread state */
174         mutable boost::mutex _state_mutex;
175         /** condition to manage thread wakeups when we have nothing to do  */
176         boost::condition _empty_condition;
177         /** condition to manage thread wakeups when we have too much to do */
178         boost::condition _full_condition;
179         /** maximum number of frames to hold in memory, for when we are managing
180          *  ordering
181          */
182         int _maximum_frames_in_memory;
183
184         /** number of FULL written frames */
185         int _full_written;
186         /** number of FAKE written frames */
187         int _fake_written;
188         int _repeat_written;
189         /** number of frames pushed to disk and then recovered
190             due to the limit of frames to be held in memory.
191         */
192         int _pushed_to_disk;
193
194         std::list<ReferencedReelAsset> _reel_assets;
195
196         std::list<boost::shared_ptr<Font> > _fonts;
197
198         static int const _info_size;
199 };