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 "alsa_output.h"
26 #include "ebu_r128_proc.h"
27 #include "h264encode.h"
29 #include "pbo_frame_allocator.h"
30 #include "ref_counted_frame.h"
31 #include "ref_counted_gl_sync.h"
32 #include "resampling_queue.h"
35 #include "stereocompressor.h"
37 #include "input_state.h"
38 #include "correlation_measurer.h"
57 // The surface format is used for offscreen destinations for OpenGL contexts we need.
58 Mixer(const QSurfaceFormat &format, unsigned num_cards);
63 void transition_clicked(int transition_num);
64 void channel_clicked(int preview_num);
69 OUTPUT_INPUT0, // 1, 2, 3, up to 15 follow numerically.
74 // The chain for rendering this frame. To render a display frame,
75 // first wait for <ready_fence>, then call <setup_chain>
76 // to wire up all the inputs, and then finally call
77 // chain->render_to_screen() or similar.
78 movit::EffectChain *chain;
79 std::function<void()> setup_chain;
81 // Asserted when all the inputs are ready; you cannot render the chain
83 RefCountedGLsync ready_fence;
85 // Holds on to all the input frames needed for this display frame,
86 // so they are not released while still rendering.
87 std::vector<RefCountedFrame> input_frames;
89 // Textures that should be released back to the resource pool
90 // when this frame disappears, if any.
91 // TODO: Refcount these as well?
92 std::vector<GLuint> temp_textures;
94 // Implicitly frees the previous one if there's a new frame available.
95 bool get_display_frame(Output output, DisplayFrame *frame) {
96 return output_channel[output].get_display_frame(frame);
99 typedef std::function<void()> new_frame_ready_callback_t;
100 void set_frame_ready_callback(Output output, new_frame_ready_callback_t callback)
102 output_channel[output].set_frame_ready_callback(callback);
105 typedef std::function<void(float level_lufs, float peak_db,
106 float global_level_lufs, float range_low_lufs, float range_high_lufs,
107 float gain_staging_db, float final_makeup_gain_db,
108 float correlation)> audio_level_callback_t;
109 void set_audio_level_callback(audio_level_callback_t callback)
111 audio_level_callback = callback;
114 std::vector<std::string> get_transition_names()
116 return theme->get_transition_names(pts());
119 unsigned get_num_channels() const
121 return theme->get_num_channels();
124 std::string get_channel_name(unsigned channel) const
126 return theme->get_channel_name(channel);
129 bool get_supports_set_wb(unsigned channel) const
131 return theme->get_supports_set_wb(channel);
134 void set_wb(unsigned channel, double r, double g, double b) const
136 theme->set_wb(channel, r, g, b);
139 void set_locut_cutoff(float cutoff_hz)
141 locut_cutoff_hz = cutoff_hz;
144 void set_locut_enabled(bool enabled)
146 locut_enabled = enabled;
149 float get_limiter_threshold_dbfs()
151 return limiter_threshold_dbfs;
154 float get_compressor_threshold_dbfs()
156 return compressor_threshold_dbfs;
159 void set_limiter_threshold_dbfs(float threshold_dbfs)
161 limiter_threshold_dbfs = threshold_dbfs;
164 void set_compressor_threshold_dbfs(float threshold_dbfs)
166 compressor_threshold_dbfs = threshold_dbfs;
169 void set_limiter_enabled(bool enabled)
171 limiter_enabled = enabled;
174 void set_compressor_enabled(bool enabled)
176 compressor_enabled = enabled;
179 void set_gain_staging_db(float gain_db)
181 std::unique_lock<std::mutex> lock(compressor_mutex);
182 level_compressor_enabled = false;
183 gain_staging_db = gain_db;
186 void set_gain_staging_auto(bool enabled)
188 std::unique_lock<std::mutex> lock(compressor_mutex);
189 level_compressor_enabled = enabled;
192 void set_final_makeup_gain_db(float gain_db)
194 std::unique_lock<std::mutex> lock(compressor_mutex);
195 final_makeup_gain_auto = false;
196 final_makeup_gain = pow(10.0f, gain_db / 20.0f);
199 void set_final_makeup_gain_auto(bool enabled)
201 std::unique_lock<std::mutex> lock(compressor_mutex);
202 final_makeup_gain_auto = enabled;
213 void bm_frame(unsigned card_index, uint16_t timecode,
214 FrameAllocator::Frame video_frame, size_t video_offset, uint16_t video_format,
215 FrameAllocator::Frame audio_frame, size_t audio_offset, uint16_t audio_format);
216 void place_rectangle(movit::Effect *resample_effect, movit::Effect *padding_effect, float x0, float y0, float x1, float y1);
218 void audio_thread_func();
219 void process_audio_one_frame(int64_t frame_pts_int, int num_samples);
220 void subsample_chroma(GLuint src_tex, GLuint dst_dst);
221 void release_display_frame(DisplayFrame *frame);
222 double pts() { return double(pts_int) / TIMEBASE; }
227 QSurface *mixer_surface, *h264_encoder_surface;
228 std::unique_ptr<movit::ResourcePool> resource_pool;
229 std::unique_ptr<Theme> theme;
230 std::unique_ptr<movit::EffectChain> display_chain;
231 GLuint cbcr_program_num; // Owned by <resource_pool>.
232 std::unique_ptr<H264Encoder> h264_encoder;
234 // Effects part of <display_chain>. Owned by <display_chain>.
235 movit::FlatInput *display_input;
237 int64_t pts_int = 0; // In TIMEBASE units.
239 std::mutex bmusb_mutex;
242 std::unique_ptr<PBOFrameAllocator> frame_allocator;
244 // Stuff for the OpenGL context (for texture uploading).
246 QOpenGLContext *context;
248 bool new_data_ready = false; // Whether new_frame contains anything.
249 bool should_quit = false;
250 RefCountedFrame new_frame;
251 int64_t new_frame_length; // In TIMEBASE units.
252 bool new_frame_interlaced;
253 unsigned new_frame_field; // Which field (0 or 1) of the frame to use. Always 0 for progressive.
254 GLsync new_data_ready_fence; // Whether new_frame is ready for rendering.
255 std::condition_variable new_data_ready_changed; // Set whenever new_data_ready is changed.
256 unsigned dropped_frames = 0; // Before new_frame.
258 // Accumulated errors in number of 1/TIMEBASE samples. If OUTPUT_FREQUENCY divided by
259 // frame rate is integer, will always stay zero.
260 unsigned fractional_samples = 0;
262 std::mutex audio_mutex;
263 std::unique_ptr<ResamplingQueue> resampling_queue; // Under audio_mutex.
264 int last_timecode = -1; // Unwrapped.
265 int64_t next_local_pts = 0; // Beginning of next frame, in TIMEBASE units.
267 CaptureCard cards[MAX_CARDS]; // protected by <bmusb_mutex>
269 InputState input_state;
271 class OutputChannel {
274 void output_frame(DisplayFrame frame);
275 bool get_display_frame(DisplayFrame *frame);
276 void set_frame_ready_callback(new_frame_ready_callback_t callback);
281 Mixer *parent = nullptr; // Not owned.
282 std::mutex frame_mutex;
283 DisplayFrame current_frame, ready_frame; // protected by <frame_mutex>
284 bool has_current_frame = false, has_ready_frame = false; // protected by <frame_mutex>
285 new_frame_ready_callback_t new_frame_ready_callback;
286 bool has_new_frame_ready_callback = false;
288 OutputChannel output_channel[NUM_OUTPUTS];
290 std::thread mixer_thread;
291 std::thread audio_thread;
292 std::atomic<bool> should_quit{false};
293 std::atomic<bool> should_cut{false};
295 audio_level_callback_t audio_level_callback = nullptr;
296 std::mutex compressor_mutex;
297 Ebu_r128_proc r128; // Under compressor_mutex.
298 CorrelationMeasurer correlation; // Under compressor_mutex.
300 Resampler peak_resampler;
301 std::atomic<float> peak{0.0f};
303 StereoFilter locut; // Default cutoff 120 Hz, 24 dB/oct.
304 std::atomic<float> locut_cutoff_hz;
305 std::atomic<bool> locut_enabled{true};
307 // First compressor; takes us up to about -12 dBFS.
308 StereoCompressor level_compressor; // Under compressor_mutex. Used to set/override gain_staging_db if <level_compressor_enabled>.
309 float gain_staging_db = 0.0f; // Under compressor_mutex.
310 bool level_compressor_enabled = true; // Under compressor_mutex.
312 static constexpr float ref_level_dbfs = -14.0f; // Chosen so that we end up around 0 LU in practice.
313 static constexpr float ref_level_lufs = -23.0f; // 0 LU, more or less by definition.
315 StereoCompressor limiter;
316 std::atomic<float> limiter_threshold_dbfs{ref_level_dbfs + 4.0f}; // 4 dB.
317 std::atomic<bool> limiter_enabled{true};
318 StereoCompressor compressor;
319 std::atomic<float> compressor_threshold_dbfs{ref_level_dbfs - 12.0f}; // -12 dB.
320 std::atomic<bool> compressor_enabled{true};
322 double final_makeup_gain = 1.0; // Under compressor_mutex. Read/write by the user. Note: Not in dB, we want the numeric precision so that we can change it slowly.
323 bool final_makeup_gain_auto = true; // Under compressor_mutex.
325 std::unique_ptr<ALSAOutput> alsa;
331 std::mutex audio_mutex;
332 std::condition_variable audio_task_queue_changed;
333 std::queue<AudioTask> audio_task_queue; // Under audio_mutex.
336 extern Mixer *global_mixer;
338 #endif // !defined(_MIXER_H)