4 // The actual video mixer, running in its own separate background thread.
11 #include <movit/effect_chain.h>
12 #include <movit/flat_input.h>
13 #include <zita-resampler/resampler.h>
15 #include <condition_variable>
24 #include "bmusb/bmusb.h"
25 #include "ebu_r128_proc.h"
26 #include "h264encode.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"
34 #include "stereocompressor.h"
54 // The surface format is used for offscreen destinations for OpenGL contexts we need.
55 Mixer(const QSurfaceFormat &format, unsigned num_cards);
60 void transition_clicked(int transition_num);
61 void channel_clicked(int preview_num);
66 OUTPUT_INPUT0, // 1, 2, 3, up to 15 follow numerically.
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;
78 // Asserted when all the inputs are ready; you cannot render the chain
80 RefCountedGLsync ready_fence;
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;
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;
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);
96 typedef std::function<void()> new_frame_ready_callback_t;
97 void set_frame_ready_callback(Output output, new_frame_ready_callback_t callback)
99 output_channel[output].set_frame_ready_callback(callback);
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)
107 audio_level_callback = callback;
110 std::vector<std::string> get_transition_names()
112 return theme->get_transition_names(pts());
115 unsigned get_num_channels() const
117 return theme->get_num_channels();
120 std::string get_channel_name(unsigned channel) const
122 return theme->get_channel_name(channel);
125 bool get_supports_set_wb(unsigned channel) const
127 return theme->get_supports_set_wb(channel);
130 void set_wb(unsigned channel, double r, double g, double b) const
132 theme->set_wb(channel, r, g, b);
135 void set_locut_cutoff(float cutoff_hz)
137 locut_cutoff_hz = cutoff_hz;
140 float get_limiter_threshold_dbfs()
142 return limiter_threshold_dbfs;
145 float get_compressor_threshold_dbfs()
147 return compressor_threshold_dbfs;
150 void set_limiter_threshold_dbfs(float threshold_dbfs)
152 limiter_threshold_dbfs = threshold_dbfs;
155 void set_compressor_threshold_dbfs(float threshold_dbfs)
157 compressor_threshold_dbfs = threshold_dbfs;
160 void set_limiter_enabled(bool enabled)
162 limiter_enabled = enabled;
165 void set_compressor_enabled(bool enabled)
167 compressor_enabled = enabled;
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);
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; }
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;
193 // Effects part of <display_chain>. Owned by <display_chain>.
194 movit::FlatInput *display_input;
196 int64_t pts_int = 0; // In TIMEBASE units.
198 std::mutex bmusb_mutex;
201 std::unique_ptr<PBOFrameAllocator> frame_allocator;
203 // Stuff for the OpenGL context (for texture uploading).
205 QOpenGLContext *context;
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.
214 std::mutex audio_mutex;
215 std::unique_ptr<ResamplingQueue> resampling_queue; // Under audio_mutex.
216 int last_timecode = -1; // Unwrapped.
218 CaptureCard cards[MAX_CARDS]; // protected by <bmusb_mutex>
220 RefCountedFrame bmusb_current_rendering_frame[MAX_CARDS];
222 class 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);
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;
239 OutputChannel output_channel[NUM_OUTPUTS];
241 std::thread mixer_thread;
242 bool should_quit = false;
244 audio_level_callback_t audio_level_callback = nullptr;
247 Resampler peak_resampler;
248 std::atomic<float> peak{0.0f};
250 StereoFilter locut; // Default cutoff 150 Hz, 24 dB/oct.
251 std::atomic<float> locut_cutoff_hz;
253 // First compressor; takes us up to about -12 dBFS.
254 StereoCompressor level_compressor;
255 float last_gain_staging_db = 0.0f;
257 static constexpr float ref_level_dbfs = -14.0f;
259 StereoCompressor limiter;
260 std::atomic<float> limiter_threshold_dbfs{ref_level_dbfs + 0.0f}; // 0 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};
267 extern Mixer *global_mixer;
269 #endif // !defined(_MIXER_H)