Various bits related to subtitle font handling, particularly copying fonts to the...
[dcpomatic.git] / src / lib / writer.h
1 /*
2     Copyright (C) 2012-2014 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 "exceptions.h"
25 #include "types.h"
26 #include "player_subtitles.h"
27 #include <boost/shared_ptr.hpp>
28 #include <boost/weak_ptr.hpp>
29 #include <boost/thread.hpp>
30 #include <boost/thread/condition.hpp>
31 #include <list>
32
33 class Film;
34 class EncodedData;
35 class AudioBuffers;
36 class Job;
37 class Font;
38
39 namespace dcp {
40         class MonoPictureMXF;
41         class MonoPictureMXFWriter;
42         class StereoPictureMXF;
43         class StereoPictureMXFWriter;
44         class PictureMXF;
45         class PictureMXFWriter;
46         class SoundMXF;
47         class SoundMXFWriter;
48         class InteropSubtitleContent;
49 }
50
51 struct QueueItem
52 {
53 public:
54         enum Type {
55                 /** a normal frame with some JPEG200 data */
56                 FULL,
57                 /** a frame whose data already exists in the MXF,
58                     and we fake-write it; i.e. we update the writer's
59                     state but we use the data that is already on disk.
60                 */
61                 FAKE,
62         } type;
63
64         /** encoded data for FULL */
65         boost::shared_ptr<const EncodedData> encoded;
66         /** size of data for FAKE */
67         int size;
68         /** frame index */
69         int frame;
70         Eyes eyes;
71 };
72
73 bool operator< (QueueItem const & a, QueueItem const & b);
74 bool operator== (QueueItem const & a, QueueItem const & b);
75
76 /** @class Writer
77  *  @brief Class to manage writing JPEG2000 and audio data to MXFs on disk.
78  *
79  *  This class creates sound and picture MXFs, then takes EncodedData
80  *  or AudioBuffers objects (containing image or sound data respectively)
81  *  and writes them to the MXFs.
82  *
83  *  ::write() for EncodedData can be called out of order, and the Writer
84  *  will sort it out.  write() for AudioBuffers must be called in order.
85  */
86
87 class Writer : public ExceptionStore, public boost::noncopyable
88 {
89 public:
90         Writer (boost::shared_ptr<const Film>, boost::weak_ptr<Job>);
91         ~Writer ();
92
93         bool can_fake_write (int) const;
94         
95         void write (boost::shared_ptr<const EncodedData>, int, Eyes);
96         void fake_write (int, Eyes);
97         void write (boost::shared_ptr<const AudioBuffers>);
98         void write (PlayerSubtitles subs);
99         void write (std::list<boost::shared_ptr<Font> > fonts);
100         void repeat (int f, Eyes);
101         void finish ();
102
103 private:
104
105         void thread ();
106         void terminate_thread (bool);
107         void check_existing_picture_mxf ();
108         bool check_existing_picture_mxf_frame (FILE *, int, Eyes);
109         bool have_sequenced_image_at_queue_head ();
110         /** maximum number of frames to hold in memory, for when we are managing
111          *  ordering
112          */
113         int maximum_frames_in_memory () const;
114
115         /** our Film */
116         boost::shared_ptr<const Film> _film;
117         boost::weak_ptr<Job> _job;
118         /** the first frame index that does not already exist in our MXF */
119         int _first_nonexistant_frame;
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 _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         /** the data of the last written frame, or 0 if there isn't one */
136         boost::shared_ptr<const EncodedData> _last_written[EYES_COUNT];
137         /** the index of the last written frame */
138         int _last_written_frame;
139         Eyes _last_written_eyes;
140
141         /** number of FULL written frames */
142         int _full_written;
143         /** number of FAKE written frames */
144         int _fake_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         boost::shared_ptr<dcp::PictureMXF> _picture_mxf;
151         boost::shared_ptr<dcp::PictureMXFWriter> _picture_mxf_writer;
152         boost::shared_ptr<dcp::SoundMXF> _sound_mxf;
153         boost::shared_ptr<dcp::SoundMXFWriter> _sound_mxf_writer;
154         boost::shared_ptr<dcp::InteropSubtitleContent> _subtitle_content;
155
156         std::list<boost::shared_ptr<Font> > _fonts;
157 };