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"
55 // The surface format is used for offscreen destinations for OpenGL contexts we need.
56 Mixer(const QSurfaceFormat &format, unsigned num_cards);
61 void transition_clicked(int transition_num);
62 void channel_clicked(int preview_num);
67 OUTPUT_INPUT0, // 1, 2, 3, up to 15 follow numerically.
72 // The chain for rendering this frame. To render a display frame,
73 // first wait for <ready_fence>, then call <setup_chain>
74 // to wire up all the inputs, and then finally call
75 // chain->render_to_screen() or similar.
76 movit::EffectChain *chain;
77 std::function<void()> setup_chain;
79 // Asserted when all the inputs are ready; you cannot render the chain
81 RefCountedGLsync ready_fence;
83 // Holds on to all the input frames needed for this display frame,
84 // so they are not released while still rendering.
85 std::vector<RefCountedFrame> input_frames;
87 // Textures that should be released back to the resource pool
88 // when this frame disappears, if any.
89 // TODO: Refcount these as well?
90 std::vector<GLuint> temp_textures;
92 // Implicitly frees the previous one if there's a new frame available.
93 bool get_display_frame(Output output, DisplayFrame *frame) {
94 return output_channel[output].get_display_frame(frame);
97 typedef std::function<void()> new_frame_ready_callback_t;
98 void set_frame_ready_callback(Output output, new_frame_ready_callback_t callback)
100 output_channel[output].set_frame_ready_callback(callback);
103 typedef std::function<void(float level_lufs, float peak_db,
104 float global_level_lufs, float range_low_lufs, float range_high_lufs,
105 float auto_gain_staging_db)> audio_level_callback_t;
106 void set_audio_level_callback(audio_level_callback_t callback)
108 audio_level_callback = callback;
111 std::vector<std::string> get_transition_names()
113 return theme->get_transition_names(pts());
116 unsigned get_num_channels() const
118 return theme->get_num_channels();
121 std::string get_channel_name(unsigned channel) const
123 return theme->get_channel_name(channel);
126 bool get_supports_set_wb(unsigned channel) const
128 return theme->get_supports_set_wb(channel);
131 void set_wb(unsigned channel, double r, double g, double b) const
133 theme->set_wb(channel, r, g, b);
136 void set_locut_cutoff(float cutoff_hz)
138 locut_cutoff_hz = cutoff_hz;
141 float get_limiter_threshold_dbfs()
143 return limiter_threshold_dbfs;
146 float get_compressor_threshold_dbfs()
148 return compressor_threshold_dbfs;
151 void set_limiter_threshold_dbfs(float threshold_dbfs)
153 limiter_threshold_dbfs = threshold_dbfs;
156 void set_compressor_threshold_dbfs(float threshold_dbfs)
158 compressor_threshold_dbfs = threshold_dbfs;
161 void set_limiter_enabled(bool enabled)
163 limiter_enabled = enabled;
166 void set_compressor_enabled(bool enabled)
168 compressor_enabled = enabled;
174 void bm_frame(unsigned card_index, uint16_t timecode,
175 FrameAllocator::Frame video_frame, size_t video_offset, uint16_t video_format,
176 FrameAllocator::Frame audio_frame, size_t audio_offset, uint16_t audio_format);
177 void place_rectangle(movit::Effect *resample_effect, movit::Effect *padding_effect, float x0, float y0, float x1, float y1);
179 void audio_thread_func();
180 void process_audio_one_frame(int64_t frame_pts_int);
181 void subsample_chroma(GLuint src_tex, GLuint dst_dst);
182 void release_display_frame(DisplayFrame *frame);
183 double pts() { return double(pts_int) / TIMEBASE; }
188 QSurface *mixer_surface, *h264_encoder_surface;
189 std::unique_ptr<movit::ResourcePool> resource_pool;
190 std::unique_ptr<Theme> theme;
191 std::unique_ptr<movit::EffectChain> display_chain;
192 GLuint cbcr_program_num; // Owned by <resource_pool>.
193 std::unique_ptr<H264Encoder> h264_encoder;
195 // Effects part of <display_chain>. Owned by <display_chain>.
196 movit::FlatInput *display_input;
198 int64_t pts_int = 0; // In TIMEBASE units.
200 std::mutex bmusb_mutex;
203 std::unique_ptr<PBOFrameAllocator> frame_allocator;
205 // Stuff for the OpenGL context (for texture uploading).
207 QOpenGLContext *context;
209 bool new_data_ready = false; // Whether new_frame contains anything.
210 bool should_quit = false;
211 RefCountedFrame new_frame;
212 GLsync new_data_ready_fence; // Whether new_frame is ready for rendering.
213 std::condition_variable new_data_ready_changed; // Set whenever new_data_ready is changed.
214 unsigned dropped_frames = 0; // Before new_frame.
216 std::mutex audio_mutex;
217 std::unique_ptr<ResamplingQueue> resampling_queue; // Under audio_mutex.
218 int last_timecode = -1; // Unwrapped.
220 CaptureCard cards[MAX_CARDS]; // protected by <bmusb_mutex>
222 RefCountedFrame bmusb_current_rendering_frame[MAX_CARDS];
224 class OutputChannel {
227 void output_frame(DisplayFrame frame);
228 bool get_display_frame(DisplayFrame *frame);
229 void set_frame_ready_callback(new_frame_ready_callback_t callback);
234 Mixer *parent = nullptr; // Not owned.
235 std::mutex frame_mutex;
236 DisplayFrame current_frame, ready_frame; // protected by <frame_mutex>
237 bool has_current_frame = false, has_ready_frame = false; // protected by <frame_mutex>
238 new_frame_ready_callback_t new_frame_ready_callback;
239 bool has_new_frame_ready_callback = false;
241 OutputChannel output_channel[NUM_OUTPUTS];
243 std::thread mixer_thread;
244 std::thread audio_thread;
245 std::atomic<bool> should_quit{false};
247 audio_level_callback_t audio_level_callback = nullptr;
250 Resampler peak_resampler;
251 std::atomic<float> peak{0.0f};
253 StereoFilter locut; // Default cutoff 150 Hz, 24 dB/oct.
254 std::atomic<float> locut_cutoff_hz;
256 // First compressor; takes us up to about -12 dBFS.
257 StereoCompressor level_compressor;
258 float last_gain_staging_db = 0.0f;
260 static constexpr float ref_level_dbfs = -14.0f;
262 StereoCompressor limiter;
263 std::atomic<float> limiter_threshold_dbfs{ref_level_dbfs + 4.0f}; // 4 dB.
264 std::atomic<bool> limiter_enabled{true};
265 StereoCompressor compressor;
266 std::atomic<float> compressor_threshold_dbfs{ref_level_dbfs - 12.0f}; // -12 dB.
267 std::atomic<bool> compressor_enabled{true};
269 std::unique_ptr<ALSAOutput> alsa;
271 std::mutex audio_mutex;
272 std::condition_variable audio_pts_queue_changed;
273 std::queue<int64_t> audio_pts_queue;
276 extern Mixer *global_mixer;
278 #endif // !defined(_MIXER_H)