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 void set_limiter_threshold_dbfs(float threshold_dbfs)
142 limiter_threshold_dbfs = threshold_dbfs;
145 void set_compressor_threshold_dbfs(float threshold_dbfs)
147 compressor_threshold_dbfs = threshold_dbfs;
150 void set_limiter_enabled(bool enabled)
152 limiter_enabled = enabled;
155 void set_compressor_enabled(bool enabled)
157 compressor_enabled = enabled;
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);
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; }
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;
183 // Effects part of <display_chain>. Owned by <display_chain>.
184 movit::FlatInput *display_input;
186 int64_t pts_int = 0; // In TIMEBASE units.
188 std::mutex bmusb_mutex;
191 std::unique_ptr<PBOFrameAllocator> frame_allocator;
193 // Stuff for the OpenGL context (for texture uploading).
195 QOpenGLContext *context;
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.
204 std::mutex audio_mutex;
205 std::unique_ptr<ResamplingQueue> resampling_queue; // Under audio_mutex.
206 int last_timecode = -1; // Unwrapped.
208 CaptureCard cards[MAX_CARDS]; // protected by <bmusb_mutex>
210 RefCountedFrame bmusb_current_rendering_frame[MAX_CARDS];
212 class 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);
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;
229 OutputChannel output_channel[NUM_OUTPUTS];
231 std::thread mixer_thread;
232 bool should_quit = false;
234 audio_level_callback_t audio_level_callback = nullptr;
237 Resampler peak_resampler;
238 std::atomic<float> peak{0.0f};
240 StereoFilter locut; // Default cutoff 150 Hz, 24 dB/oct.
241 std::atomic<float> locut_cutoff_hz;
243 // First compressor; takes us up to about -12 dBFS.
244 StereoCompressor level_compressor;
245 float last_gain_staging_db = 0.0f;
247 static constexpr float ref_level_dbfs = -14.0f;
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};
257 extern Mixer *global_mixer;
259 #endif // !defined(_MIXER_H)