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