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