Basics of reel split.
[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
41 namespace dcp {
42         class MonoPictureAsset;
43         class MonoPictureAssetWriter;
44         class StereoPictureAsset;
45         class StereoPictureAssetWriter;
46         class PictureAsset;
47         class PictureAssetWriter;
48         class SoundAsset;
49         class SoundAssetWriter;
50         class SubtitleAsset;
51         class ReelAsset;
52 }
53
54 struct QueueItem
55 {
56 public:
57         QueueItem ()
58                 : size (0)
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<Data> encoded;
74         /** size of data for FAKE */
75         int size;
76         /** frame index */
77         int frame;
78         Eyes eyes;
79 };
80
81 bool operator< (QueueItem const & a, QueueItem const & b);
82 bool operator== (QueueItem const & a, QueueItem const & b);
83
84 /** @class Writer
85  *  @brief Class to manage writing JPEG2000 and audio data to assets on disk.
86  *
87  *  This class creates sound and picture assets, then takes Data
88  *  or AudioBuffers objects (containing image or sound data respectively)
89  *  and writes them to the assets.
90  *
91  *  ::write() for Data can be called out of order, and the Writer
92  *  will sort it out.  write() for AudioBuffers must be called in order.
93  */
94
95 class Writer : public ExceptionStore, public boost::noncopyable
96 {
97 public:
98         Writer (boost::shared_ptr<const Film>, boost::weak_ptr<Job>);
99         ~Writer ();
100
101         void start ();
102
103         bool can_fake_write (int) const;
104
105         void write (Data, int, Eyes);
106         void fake_write (int, Eyes);
107         void repeat (int, Eyes);
108         void write (boost::shared_ptr<const AudioBuffers>);
109         void write (PlayerSubtitles subs);
110         void write (std::list<boost::shared_ptr<Font> > fonts);
111         void write (boost::shared_ptr<dcp::ReelAsset> reel_asset);
112         void finish ();
113
114         void set_encoder_threads (int threads);
115
116 private:
117
118         class Reel {
119         public:
120                 Reel ()
121                         : first_nonexistant_frame (0)
122                 {}
123
124                 DCPTimePeriod period;
125                 /** the first frame index that does not already exist in our MXF */
126                 int first_nonexistant_frame;
127
128                 boost::shared_ptr<dcp::PictureAsset> picture_asset;
129                 boost::shared_ptr<dcp::PictureAssetWriter> picture_asset_writer;
130                 boost::shared_ptr<dcp::SoundAsset> sound_asset;
131                 boost::shared_ptr<dcp::SoundAssetWriter> sound_asset_writer;
132                 boost::shared_ptr<dcp::SubtitleAsset> subtitle_asset;
133         };
134
135         void thread ();
136         void terminate_thread (bool);
137         void check_existing_picture_asset (Reel& reel);
138         bool have_sequenced_image_at_queue_head ();
139         void write_frame_info (int frame, Eyes eyes, dcp::FrameInfo info) const;
140         long frame_info_position (int frame, Eyes eyes) const;
141         dcp::FrameInfo read_frame_info (FILE* file, int frame, Eyes eyes) const;
142         Reel const & video_reel (int frame) const;
143
144         /** our Film */
145         boost::shared_ptr<const Film> _film;
146         boost::weak_ptr<Job> _job;
147         std::list<Reel> _reels;
148         std::list<Reel>::iterator _audio_reel;
149         std::list<Reel>::iterator _subtitle_reel;
150
151         /** our thread, or 0 */
152         boost::thread* _thread;
153         /** true if our thread should finish */
154         bool _finish;
155         /** queue of things to write to disk */
156         std::list<QueueItem> _queue;
157         /** number of FULL frames whose JPEG200 data is currently held in RAM */
158         int _queued_full_in_memory;
159         /** mutex for thread state */
160         mutable boost::mutex _state_mutex;
161         /** condition to manage thread wakeups when we have nothing to do  */
162         boost::condition _empty_condition;
163         /** condition to manage thread wakeups when we have too much to do */
164         boost::condition _full_condition;
165         /** the data of the last written frame, if there is one */
166         boost::optional<Data> _last_written[EYES_COUNT];
167         /** the index of the last written frame */
168         int _last_written_frame;
169         Eyes _last_written_eyes;
170         /** maximum number of frames to hold in memory, for when we are managing
171          *  ordering
172          */
173         int _maximum_frames_in_memory;
174
175         /** number of FULL written frames */
176         int _full_written;
177         /** number of FAKE written frames */
178         int _fake_written;
179         int _repeat_written;
180         /** number of frames pushed to disk and then recovered
181             due to the limit of frames to be held in memory.
182         */
183         int _pushed_to_disk;
184
185         std::list<boost::shared_ptr<dcp::ReelAsset> > _reel_assets;
186
187         std::list<boost::shared_ptr<Font> > _fonts;
188
189         static int const _info_size;
190 };