Sort of works to a first-order approximation.
[dcpomatic.git] / src / lib / encoder.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 #ifndef DVDOMATIC_ENCODER_H
21 #define DVDOMATIC_ENCODER_H
22
23 /** @file src/encoder.h
24  *  @brief Parent class for classes which can encode video and audio frames.
25  */
26
27 #include <boost/shared_ptr.hpp>
28 #include <boost/thread/mutex.hpp>
29 #include <boost/thread/condition.hpp>
30 #include <boost/thread.hpp>
31 #include <boost/optional.hpp>
32 #include <list>
33 #include <stdint.h>
34 extern "C" {
35 #include <libavutil/samplefmt.h>
36 }
37 #ifdef HAVE_SWRESAMPLE
38 extern "C" {
39 #include <libswresample/swresample.h>
40 }
41 #endif
42 #include <sndfile.h>
43 #include "util.h"
44 #include "video_sink.h"
45 #include "audio_sink.h"
46
47 class Image;
48 class Subtitle;
49 class AudioBuffers;
50 class Film;
51 class ServerDescription;
52 class DCPVideoFrame;
53 class EncodedData;
54
55 namespace libdcp {
56         class MonoPictureAsset;
57         class MonoPictureAssetWriter;
58 }
59
60 /** @class Encoder
61  *  @brief Encoder to J2K and WAV for DCP.
62  *
63  *  Video is supplied to process_video as YUV frames, and audio
64  *  is supplied as uncompressed PCM in blocks of various sizes.
65  */
66
67 class Encoder : public VideoSink, public AudioSink
68 {
69 public:
70         Encoder (boost::shared_ptr<const Film> f);
71         virtual ~Encoder ();
72
73         /** Called to indicate that a processing run is about to begin */
74         virtual void process_begin ();
75
76         /** Call with a frame of video.
77          *  @param i Video frame image.
78          *  @param same true if i is the same as the last time we were called.
79          *  @param s A subtitle that should be on this frame, or 0.
80          */
81         void process_video (boost::shared_ptr<Image> i, bool same, boost::shared_ptr<Subtitle> s);
82
83         /** Call with some audio data */
84         void process_audio (boost::shared_ptr<AudioBuffers>);
85
86         /** Called when a processing run has finished */
87         virtual void process_end ();
88
89         float current_frames_per_second () const;
90         bool skipping () const;
91         int video_frames_out () const;
92
93 private:
94         
95         void frame_done ();
96         void frame_skipped ();
97         
98         void close_sound_files ();
99         void write_audio (boost::shared_ptr<const AudioBuffers> audio);
100
101         void encoder_thread (ServerDescription *);
102         void terminate_worker_threads ();
103         void link (std::string, std::string) const;
104
105         /** Film that we are encoding */
106         boost::shared_ptr<const Film> _film;
107
108         /** Mutex for _time_history, _just_skipped and _last_frame */
109         mutable boost::mutex _history_mutex;
110         /** List of the times of completion of the last _history_size frames;
111             first is the most recently completed.
112         */
113         std::list<struct timeval> _time_history;
114         /** Number of frames that we should keep history for */
115         static int const _history_size;
116         /** true if the last frame we processed was skipped (because it was already done) */
117         bool _just_skipped;
118
119         /** Number of video frames received so far */
120         SourceFrame _video_frames_in;
121         /** Number of audio frames received so far */
122         int64_t _audio_frames_in;
123         /** Number of video frames written for the DCP so far */
124         int _video_frames_out;
125         /** Number of audio frames written for the DCP so far */
126         int64_t _audio_frames_out;
127
128         void writer_thread ();
129         void finish_writer_thread ();
130
131 #if HAVE_SWRESAMPLE     
132         SwrContext* _swr_context;
133 #endif
134
135         std::vector<SNDFILE*> _sound_files;
136
137         bool _have_a_real_frame;
138         bool _terminate_encoder;
139         std::list<boost::shared_ptr<DCPVideoFrame> > _encode_queue;
140         std::list<boost::thread *> _worker_threads;
141         mutable boost::mutex _worker_mutex;
142         boost::condition _worker_condition;
143
144         boost::thread* _writer_thread;
145         bool _finish_writer;
146         std::list<std::pair<boost::shared_ptr<EncodedData>, int> > _write_queue;
147         mutable boost::mutex _writer_mutex;
148         boost::condition _writer_condition;
149         boost::shared_ptr<EncodedData> _last_written;
150         std::list<int> _pending;
151         int _last_written_frame;
152         static const unsigned int _maximum_frames_in_memory;
153
154         boost::shared_ptr<libdcp::MonoPictureAsset> _picture_asset;
155         boost::shared_ptr<libdcp::MonoPictureAssetWriter> _picture_asset_writer;
156 };
157
158 #endif