]> git.sesse.net Git - nageru/blob - mixer.h
Include the limiter and the main compressor. Still needs some tuning, though.
[nageru] / mixer.h
1 #ifndef _MIXER_H
2 #define _MIXER_H 1
3
4 // The actual video mixer, running in its own separate background thread.
5
6 #include <epoxy/gl.h>
7 #undef Success
8 #include <stdbool.h>
9 #include <stdint.h>
10
11 #include <movit/effect_chain.h>
12 #include <movit/flat_input.h>
13 #include <atomic>
14 #include <condition_variable>
15 #include <cstddef>
16 #include <functional>
17 #include <memory>
18 #include <mutex>
19 #include <string>
20 #include <thread>
21 #include <vector>
22
23 #include "bmusb/bmusb.h"
24 #include "ebu_r128_proc.h"
25 #include "h264encode.h"
26 #include "httpd.h"
27 #include "pbo_frame_allocator.h"
28 #include "ref_counted_frame.h"
29 #include "ref_counted_gl_sync.h"
30 #include "resampler.h"
31 #include "theme.h"
32 #include "timebase.h"
33 #include "stereocompressor.h"
34 #include "filter.h"
35
36 class H264Encoder;
37 class QSurface;
38 namespace movit {
39 class Effect;
40 class EffectChain;
41 class FlatInput;
42 class ResourcePool;
43 }  // namespace movit
44
45 namespace movit {
46 class YCbCrInput;
47 }
48 class QOpenGLContext;
49 class QSurfaceFormat;
50
51 class Mixer {
52 public:
53         // The surface format is used for offscreen destinations for OpenGL contexts we need.
54         Mixer(const QSurfaceFormat &format, unsigned num_cards);
55         ~Mixer();
56         void start();
57         void quit();
58
59         void transition_clicked(int transition_num);
60         void channel_clicked(int preview_num);
61
62         enum Output {
63                 OUTPUT_LIVE = 0,
64                 OUTPUT_PREVIEW,
65                 OUTPUT_INPUT0,  // 1, 2, 3, up to 15 follow numerically.
66                 NUM_OUTPUTS = 18
67         };
68
69         struct DisplayFrame {
70                 // The chain for rendering this frame. To render a display frame,
71                 // first wait for <ready_fence>, then call <setup_chain>
72                 // to wire up all the inputs, and then finally call
73                 // chain->render_to_screen() or similar.
74                 movit::EffectChain *chain;
75                 std::function<void()> setup_chain;
76
77                 // Asserted when all the inputs are ready; you cannot render the chain
78                 // before this.
79                 RefCountedGLsync ready_fence;
80
81                 // Holds on to all the input frames needed for this display frame,
82                 // so they are not released while still rendering.
83                 std::vector<RefCountedFrame> input_frames;
84
85                 // Textures that should be released back to the resource pool
86                 // when this frame disappears, if any.
87                 // TODO: Refcount these as well?
88                 std::vector<GLuint> temp_textures;
89         };
90         // Implicitly frees the previous one if there's a new frame available.
91         bool get_display_frame(Output output, DisplayFrame *frame) {
92                 return output_channel[output].get_display_frame(frame);
93         }
94
95         typedef std::function<void()> new_frame_ready_callback_t;
96         void set_frame_ready_callback(Output output, new_frame_ready_callback_t callback)
97         {
98                 output_channel[output].set_frame_ready_callback(callback);
99         }
100
101         typedef std::function<void(float level_lufs, float peak_db,
102                                    float global_level_lufs, float range_low_lufs, float range_high_lufs,
103                                    float auto_gain_staging_db)> audio_level_callback_t;
104         void set_audio_level_callback(audio_level_callback_t callback)
105         {
106                 audio_level_callback = callback;
107         }
108
109         std::vector<std::string> get_transition_names()
110         {
111                 return theme->get_transition_names(pts());
112         }
113
114         unsigned get_num_channels() const
115         {
116                 return theme->get_num_channels();
117         }
118
119         std::string get_channel_name(unsigned channel) const
120         {
121                 return theme->get_channel_name(channel);
122         }
123
124         bool get_supports_set_wb(unsigned channel) const
125         {
126                 return theme->get_supports_set_wb(channel);
127         }
128
129         void set_wb(unsigned channel, double r, double g, double b) const
130         {
131                 theme->set_wb(channel, r, g, b);
132         }
133
134         void set_locut_cutoff(float cutoff_hz)
135         {
136                 locut_cutoff_hz = cutoff_hz;
137         }
138
139 private:
140         void bm_frame(unsigned card_index, uint16_t timecode,
141                 FrameAllocator::Frame video_frame, size_t video_offset, uint16_t video_format,
142                 FrameAllocator::Frame audio_frame, size_t audio_offset, uint16_t audio_format);
143         void place_rectangle(movit::Effect *resample_effect, movit::Effect *padding_effect, float x0, float y0, float x1, float y1);
144         void thread_func();
145         void process_audio_one_frame();
146         void subsample_chroma(GLuint src_tex, GLuint dst_dst);
147         void release_display_frame(DisplayFrame *frame);
148         double pts() { return double(pts_int) / TIMEBASE; }
149
150         HTTPD httpd;
151         unsigned num_cards;
152
153         QSurface *mixer_surface, *h264_encoder_surface;
154         std::unique_ptr<movit::ResourcePool> resource_pool;
155         std::unique_ptr<Theme> theme;
156         std::unique_ptr<movit::EffectChain> display_chain;
157         GLuint cbcr_program_num;  // Owned by <resource_pool>.
158         std::unique_ptr<H264Encoder> h264_encoder;
159
160         // Effects part of <display_chain>. Owned by <display_chain>.
161         movit::FlatInput *display_input;
162
163         int64_t pts_int = 0;  // In TIMEBASE units.
164
165         std::mutex bmusb_mutex;
166         struct CaptureCard {
167                 BMUSBCapture *usb;
168                 std::unique_ptr<PBOFrameAllocator> frame_allocator;
169
170                 // Stuff for the OpenGL context (for texture uploading).
171                 QSurface *surface;
172                 QOpenGLContext *context;
173
174                 bool new_data_ready = false;  // Whether new_frame contains anything.
175                 bool should_quit = false;
176                 RefCountedFrame new_frame;
177                 GLsync new_data_ready_fence;  // Whether new_frame is ready for rendering.
178                 std::condition_variable new_data_ready_changed;  // Set whenever new_data_ready is changed.
179                 unsigned dropped_frames = 0;  // Before new_frame.
180
181                 std::mutex audio_mutex;
182                 std::unique_ptr<Resampler> resampler;  // Under audio_mutex.
183                 int last_timecode = -1;  // Unwrapped.
184         };
185         CaptureCard cards[MAX_CARDS];  // protected by <bmusb_mutex>
186
187         RefCountedFrame bmusb_current_rendering_frame[MAX_CARDS];
188
189         class OutputChannel {
190         public:
191                 ~OutputChannel();
192                 void output_frame(DisplayFrame frame);
193                 bool get_display_frame(DisplayFrame *frame);
194                 void set_frame_ready_callback(new_frame_ready_callback_t callback);
195
196         private:
197                 friend class Mixer;
198
199                 Mixer *parent = nullptr;  // Not owned.
200                 std::mutex frame_mutex;
201                 DisplayFrame current_frame, ready_frame;  // protected by <frame_mutex>
202                 bool has_current_frame = false, has_ready_frame = false;  // protected by <frame_mutex>
203                 new_frame_ready_callback_t new_frame_ready_callback;
204                 bool has_new_frame_ready_callback = false;
205         };
206         OutputChannel output_channel[NUM_OUTPUTS];
207
208         std::thread mixer_thread;
209         bool should_quit = false;
210
211         audio_level_callback_t audio_level_callback = nullptr;
212         Ebu_r128_proc r128;
213
214         // TODO: Implement oversampled peak detection.
215         float peak = 0.0f;
216
217         StereoFilter locut;  // Default cutoff 150 Hz, 24 dB/oct.
218         std::atomic<float> locut_cutoff_hz;
219
220         // First compressor; takes us up to about -12 dBFS.
221         StereoCompressor level_compressor;
222         float last_gain_staging_db = 0.0f;
223
224         StereoCompressor limiter;
225         StereoCompressor compressor;
226 };
227
228 extern Mixer *global_mixer;
229
230 #endif  // !defined(_MIXER_H)