Merge master.
[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 <list>
25 #include <boost/shared_ptr.hpp>
26 #include <boost/thread.hpp>
27 #include <boost/thread/condition.hpp>
28 #include <dcp/subtitle_content.h>
29 #include "exceptions.h"
30 #include "types.h"
31 #include "player_subtitles.h"
32
33 class Film;
34 class EncodedData;
35 class AudioBuffers;
36 class Job;
37
38 namespace dcp {
39         class MonoPictureMXF;
40         class MonoPictureMXFWriter;
41         class StereoPictureMXF;
42         class StereoPictureMXFWriter;
43         class PictureMXF;
44         class PictureMXFWriter;
45         class SoundMXF;
46         class SoundMXFWriter;
47 }
48
49 struct QueueItem
50 {
51 public:
52         enum Type {
53                 /** a normal frame with some JPEG200 data */
54                 FULL,
55                 /** a frame whose data already exists in the MXF,
56                     and we fake-write it; i.e. we update the writer's
57                     state but we use the data that is already on disk.
58                 */
59                 FAKE,
60         } type;
61
62         /** encoded data for FULL */
63         boost::shared_ptr<const EncodedData> encoded;
64         /** size of data for FAKE */
65         int size;
66         /** frame index */
67         int frame;
68         Eyes eyes;
69 };
70
71 bool operator< (QueueItem const & a, QueueItem const & b);
72 bool operator== (QueueItem const & a, QueueItem const & b);
73
74 /** @class Writer
75  *  @brief Class to manage writing JPEG2000 and audio data to MXFs on disk.
76  *
77  *  This class creates sound and picture MXFs, then takes EncodedData
78  *  or AudioBuffers objects (containing image or sound data respectively)
79  *  and writes them to the MXFs.
80  *
81  *  ::write() for EncodedData can be called out of order, and the Writer
82  *  will sort it out.  write() for AudioBuffers must be called in order.
83  */
84
85 class Writer : public ExceptionStore, public boost::noncopyable
86 {
87 public:
88         Writer (boost::shared_ptr<const Film>, boost::weak_ptr<Job>);
89         ~Writer ();
90
91         bool can_fake_write (int) const;
92         
93         void write (boost::shared_ptr<const EncodedData>, int, Eyes);
94         void fake_write (int, Eyes);
95         void write (boost::shared_ptr<const AudioBuffers>);
96         void write (PlayerSubtitles);
97         void repeat (int f, Eyes);
98         void finish ();
99
100 private:
101
102         void thread ();
103         void terminate_thread (bool);
104         void check_existing_picture_mxf ();
105         bool check_existing_picture_mxf_frame (FILE *, int, Eyes);
106         bool have_sequenced_image_at_queue_head ();
107
108         /** our Film */
109         boost::shared_ptr<const Film> _film;
110         boost::weak_ptr<Job> _job;
111         /** the first frame index that does not already exist in our MXF */
112         int _first_nonexistant_frame;
113
114         /** our thread, or 0 */
115         boost::thread* _thread;
116         /** true if our thread should finish */
117         bool _finish;
118         /** queue of things to write to disk */
119         std::list<QueueItem> _queue;
120         /** number of FULL frames whose JPEG200 data is currently held in RAM */
121         int _queued_full_in_memory;
122         /** mutex for thread state */
123         mutable boost::mutex _mutex;
124         /** condition to manage thread wakeups when we have nothing to do  */
125         boost::condition _empty_condition;
126         /** condition to manage thread wakeups when we have too much to do */
127         boost::condition _full_condition;
128         /** the data of the last written frame, or 0 if there isn't one */
129         boost::shared_ptr<const EncodedData> _last_written[EYES_COUNT];
130         /** the index of the last written frame */
131         int _last_written_frame;
132         Eyes _last_written_eyes;
133         /** maximum number of frames to hold in memory, for when we are managing
134             ordering
135         */
136         static const int _maximum_frames_in_memory;
137
138         /** number of FULL written frames */
139         int _full_written;
140         /** number of FAKE written frames */
141         int _fake_written;
142         /** number of frames pushed to disk and then recovered
143             due to the limit of frames to be held in memory.
144         */
145         int _pushed_to_disk;
146         
147         boost::shared_ptr<dcp::PictureMXF> _picture_mxf;
148         boost::shared_ptr<dcp::PictureMXFWriter> _picture_mxf_writer;
149         boost::shared_ptr<dcp::SoundMXF> _sound_mxf;
150         boost::shared_ptr<dcp::SoundMXFWriter> _sound_mxf_writer;
151         boost::shared_ptr<dcp::SubtitleContent> _subtitle_content;
152 };