]> git.sesse.net Git - nageru/blob - mixer.h
Add some GUI elements (hooked up) to help tuning the compressor.
[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         void set_limiter_threshold_dbfs(float threshold_dbfs)
141         {
142                 limiter_threshold_dbfs = threshold_dbfs;
143         }
144
145         void set_compressor_threshold_dbfs(float threshold_dbfs)
146         {
147                 compressor_threshold_dbfs = threshold_dbfs;
148         }
149
150         void set_limiter_enabled(bool enabled)
151         {
152                 limiter_enabled = enabled;
153         }
154
155         void set_compressor_enabled(bool enabled)
156         {
157                 compressor_enabled = enabled;
158         }
159
160         void reset_meters();
161
162 private:
163         void bm_frame(unsigned card_index, uint16_t timecode,
164                 FrameAllocator::Frame video_frame, size_t video_offset, uint16_t video_format,
165                 FrameAllocator::Frame audio_frame, size_t audio_offset, uint16_t audio_format);
166         void place_rectangle(movit::Effect *resample_effect, movit::Effect *padding_effect, float x0, float y0, float x1, float y1);
167         void thread_func();
168         void process_audio_one_frame();
169         void subsample_chroma(GLuint src_tex, GLuint dst_dst);
170         void release_display_frame(DisplayFrame *frame);
171         double pts() { return double(pts_int) / TIMEBASE; }
172
173         HTTPD httpd;
174         unsigned num_cards;
175
176         QSurface *mixer_surface, *h264_encoder_surface;
177         std::unique_ptr<movit::ResourcePool> resource_pool;
178         std::unique_ptr<Theme> theme;
179         std::unique_ptr<movit::EffectChain> display_chain;
180         GLuint cbcr_program_num;  // Owned by <resource_pool>.
181         std::unique_ptr<H264Encoder> h264_encoder;
182
183         // Effects part of <display_chain>. Owned by <display_chain>.
184         movit::FlatInput *display_input;
185
186         int64_t pts_int = 0;  // In TIMEBASE units.
187
188         std::mutex bmusb_mutex;
189         struct CaptureCard {
190                 BMUSBCapture *usb;
191                 std::unique_ptr<PBOFrameAllocator> frame_allocator;
192
193                 // Stuff for the OpenGL context (for texture uploading).
194                 QSurface *surface;
195                 QOpenGLContext *context;
196
197                 bool new_data_ready = false;  // Whether new_frame contains anything.
198                 bool should_quit = false;
199                 RefCountedFrame new_frame;
200                 GLsync new_data_ready_fence;  // Whether new_frame is ready for rendering.
201                 std::condition_variable new_data_ready_changed;  // Set whenever new_data_ready is changed.
202                 unsigned dropped_frames = 0;  // Before new_frame.
203
204                 std::mutex audio_mutex;
205                 std::unique_ptr<ResamplingQueue> resampling_queue;  // Under audio_mutex.
206                 int last_timecode = -1;  // Unwrapped.
207         };
208         CaptureCard cards[MAX_CARDS];  // protected by <bmusb_mutex>
209
210         RefCountedFrame bmusb_current_rendering_frame[MAX_CARDS];
211
212         class OutputChannel {
213         public:
214                 ~OutputChannel();
215                 void output_frame(DisplayFrame frame);
216                 bool get_display_frame(DisplayFrame *frame);
217                 void set_frame_ready_callback(new_frame_ready_callback_t callback);
218
219         private:
220                 friend class Mixer;
221
222                 Mixer *parent = nullptr;  // Not owned.
223                 std::mutex frame_mutex;
224                 DisplayFrame current_frame, ready_frame;  // protected by <frame_mutex>
225                 bool has_current_frame = false, has_ready_frame = false;  // protected by <frame_mutex>
226                 new_frame_ready_callback_t new_frame_ready_callback;
227                 bool has_new_frame_ready_callback = false;
228         };
229         OutputChannel output_channel[NUM_OUTPUTS];
230
231         std::thread mixer_thread;
232         bool should_quit = false;
233
234         audio_level_callback_t audio_level_callback = nullptr;
235         Ebu_r128_proc r128;
236
237         Resampler peak_resampler;
238         std::atomic<float> peak{0.0f};
239
240         StereoFilter locut;  // Default cutoff 150 Hz, 24 dB/oct.
241         std::atomic<float> locut_cutoff_hz;
242
243         // First compressor; takes us up to about -12 dBFS.
244         StereoCompressor level_compressor;
245         float last_gain_staging_db = 0.0f;
246
247         static constexpr float ref_level_dbfs = -14.0f;
248
249         StereoCompressor limiter;
250         std::atomic<float> limiter_threshold_dbfs{ref_level_dbfs + 0.0f};   // 0 dB.
251         std::atomic<bool> limiter_enabled{true};
252         StereoCompressor compressor;
253         std::atomic<float> compressor_threshold_dbfs{ref_level_dbfs - 12.0f};  // -12 dB.
254         std::atomic<bool> compressor_enabled{true};
255 };
256
257 extern Mixer *global_mixer;
258
259 #endif  // !defined(_MIXER_H)