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