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"
56 // The surface format is used for offscreen destinations for OpenGL contexts we need.
57 Mixer(const QSurfaceFormat &format, unsigned num_cards);
62 void transition_clicked(int transition_num);
63 void channel_clicked(int preview_num);
68 OUTPUT_INPUT0, // 1, 2, 3, up to 15 follow numerically.
73 // The chain for rendering this frame. To render a display frame,
74 // first wait for <ready_fence>, then call <setup_chain>
75 // to wire up all the inputs, and then finally call
76 // chain->render_to_screen() or similar.
77 movit::EffectChain *chain;
78 std::function<void()> setup_chain;
80 // Asserted when all the inputs are ready; you cannot render the chain
82 RefCountedGLsync ready_fence;
84 // Holds on to all the input frames needed for this display frame,
85 // so they are not released while still rendering.
86 std::vector<RefCountedFrame> input_frames;
88 // Textures that should be released back to the resource pool
89 // when this frame disappears, if any.
90 // TODO: Refcount these as well?
91 std::vector<GLuint> temp_textures;
93 // Implicitly frees the previous one if there's a new frame available.
94 bool get_display_frame(Output output, DisplayFrame *frame) {
95 return output_channel[output].get_display_frame(frame);
98 typedef std::function<void()> new_frame_ready_callback_t;
99 void set_frame_ready_callback(Output output, new_frame_ready_callback_t callback)
101 output_channel[output].set_frame_ready_callback(callback);
104 typedef std::function<void(float level_lufs, float peak_db,
105 float global_level_lufs, float range_low_lufs, float range_high_lufs,
106 float auto_gain_staging_db)> audio_level_callback_t;
107 void set_audio_level_callback(audio_level_callback_t callback)
109 audio_level_callback = callback;
112 std::vector<std::string> get_transition_names()
114 return theme->get_transition_names(pts());
117 unsigned get_num_channels() const
119 return theme->get_num_channels();
122 std::string get_channel_name(unsigned channel) const
124 return theme->get_channel_name(channel);
127 bool get_supports_set_wb(unsigned channel) const
129 return theme->get_supports_set_wb(channel);
132 void set_wb(unsigned channel, double r, double g, double b) const
134 theme->set_wb(channel, r, g, b);
137 void set_locut_cutoff(float cutoff_hz)
139 locut_cutoff_hz = cutoff_hz;
142 float get_limiter_threshold_dbfs()
144 return limiter_threshold_dbfs;
147 float get_compressor_threshold_dbfs()
149 return compressor_threshold_dbfs;
152 void set_limiter_threshold_dbfs(float threshold_dbfs)
154 limiter_threshold_dbfs = threshold_dbfs;
157 void set_compressor_threshold_dbfs(float threshold_dbfs)
159 compressor_threshold_dbfs = threshold_dbfs;
162 void set_limiter_enabled(bool enabled)
164 limiter_enabled = enabled;
167 void set_compressor_enabled(bool enabled)
169 compressor_enabled = enabled;
175 void bm_frame(unsigned card_index, uint16_t timecode,
176 FrameAllocator::Frame video_frame, size_t video_offset, uint16_t video_format,
177 FrameAllocator::Frame audio_frame, size_t audio_offset, uint16_t audio_format);
178 void place_rectangle(movit::Effect *resample_effect, movit::Effect *padding_effect, float x0, float y0, float x1, float y1);
180 void audio_thread_func();
181 void process_audio_one_frame(int64_t frame_pts_int, int num_samples);
182 void subsample_chroma(GLuint src_tex, GLuint dst_dst);
183 void release_display_frame(DisplayFrame *frame);
184 double pts() { return double(pts_int) / TIMEBASE; }
189 QSurface *mixer_surface, *h264_encoder_surface;
190 std::unique_ptr<movit::ResourcePool> resource_pool;
191 std::unique_ptr<Theme> theme;
192 std::unique_ptr<movit::EffectChain> display_chain;
193 GLuint cbcr_program_num; // Owned by <resource_pool>.
194 std::unique_ptr<H264Encoder> h264_encoder;
196 // Effects part of <display_chain>. Owned by <display_chain>.
197 movit::FlatInput *display_input;
199 int64_t pts_int = 0; // In TIMEBASE units.
201 std::mutex bmusb_mutex;
204 std::unique_ptr<PBOFrameAllocator> frame_allocator;
206 // Stuff for the OpenGL context (for texture uploading).
208 QOpenGLContext *context;
210 bool new_data_ready = false; // Whether new_frame contains anything.
211 bool should_quit = false;
212 RefCountedFrame new_frame;
213 int64_t new_frame_length; // In TIMEBASE units.
214 bool new_frame_interlaced;
215 unsigned new_frame_field; // Which field (0 or 1) of the frame to use. Always 0 for progressive.
216 GLsync new_data_ready_fence; // Whether new_frame is ready for rendering.
217 std::condition_variable new_data_ready_changed; // Set whenever new_data_ready is changed.
218 unsigned dropped_frames = 0; // Before new_frame.
220 // Accumulated errors in number of 1/TIMEBASE samples. If OUTPUT_FREQUENCY divided by
221 // frame rate is integer, will always stay zero.
222 unsigned fractional_samples = 0;
224 std::mutex audio_mutex;
225 std::unique_ptr<ResamplingQueue> resampling_queue; // Under audio_mutex.
226 int last_timecode = -1; // Unwrapped.
227 int64_t next_local_pts = 0; // Beginning of next frame, in TIMEBASE units.
229 CaptureCard cards[MAX_CARDS]; // protected by <bmusb_mutex>
231 InputState input_state;
233 class OutputChannel {
236 void output_frame(DisplayFrame frame);
237 bool get_display_frame(DisplayFrame *frame);
238 void set_frame_ready_callback(new_frame_ready_callback_t callback);
243 Mixer *parent = nullptr; // Not owned.
244 std::mutex frame_mutex;
245 DisplayFrame current_frame, ready_frame; // protected by <frame_mutex>
246 bool has_current_frame = false, has_ready_frame = false; // protected by <frame_mutex>
247 new_frame_ready_callback_t new_frame_ready_callback;
248 bool has_new_frame_ready_callback = false;
250 OutputChannel output_channel[NUM_OUTPUTS];
252 std::thread mixer_thread;
253 std::thread audio_thread;
254 std::atomic<bool> should_quit{false};
256 audio_level_callback_t audio_level_callback = nullptr;
257 std::mutex r128_mutex;
258 Ebu_r128_proc r128; // Under r128_mutex.
260 Resampler peak_resampler;
261 std::atomic<float> peak{0.0f};
263 StereoFilter locut; // Default cutoff 150 Hz, 24 dB/oct.
264 std::atomic<float> locut_cutoff_hz;
266 // First compressor; takes us up to about -12 dBFS.
267 StereoCompressor level_compressor;
268 float last_gain_staging_db = 0.0f;
270 static constexpr float ref_level_dbfs = -14.0f;
272 StereoCompressor limiter;
273 std::atomic<float> limiter_threshold_dbfs{ref_level_dbfs + 4.0f}; // 4 dB.
274 std::atomic<bool> limiter_enabled{true};
275 StereoCompressor compressor;
276 std::atomic<float> compressor_threshold_dbfs{ref_level_dbfs - 12.0f}; // -12 dB.
277 std::atomic<bool> compressor_enabled{true};
279 std::unique_ptr<ALSAOutput> alsa;
285 std::mutex audio_mutex;
286 std::condition_variable audio_task_queue_changed;
287 std::queue<AudioTask> audio_task_queue; // Under audio_mutex.
290 extern Mixer *global_mixer;
292 #endif // !defined(_MIXER_H)