4 // The actual video mixer, running in its own separate background thread.
11 #include <movit/effect_chain.h>
12 #include <movit/flat_input.h>
14 #include <condition_variable>
23 #include "bmusb/bmusb.h"
24 #include "ebu_r128_proc.h"
25 #include "h264encode.h"
27 #include "pbo_frame_allocator.h"
28 #include "ref_counted_frame.h"
29 #include "ref_counted_gl_sync.h"
30 #include "resampler.h"
33 #include "stereocompressor.h"
53 // The surface format is used for offscreen destinations for OpenGL contexts we need.
54 Mixer(const QSurfaceFormat &format, unsigned num_cards);
59 void transition_clicked(int transition_num);
60 void channel_clicked(int preview_num);
65 OUTPUT_INPUT0, // 1, 2, 3, up to 15 follow numerically.
70 // The chain for rendering this frame. To render a display frame,
71 // first wait for <ready_fence>, then call <setup_chain>
72 // to wire up all the inputs, and then finally call
73 // chain->render_to_screen() or similar.
74 movit::EffectChain *chain;
75 std::function<void()> setup_chain;
77 // Asserted when all the inputs are ready; you cannot render the chain
79 RefCountedGLsync ready_fence;
81 // Holds on to all the input frames needed for this display frame,
82 // so they are not released while still rendering.
83 std::vector<RefCountedFrame> input_frames;
85 // Textures that should be released back to the resource pool
86 // when this frame disappears, if any.
87 // TODO: Refcount these as well?
88 std::vector<GLuint> temp_textures;
90 // Implicitly frees the previous one if there's a new frame available.
91 bool get_display_frame(Output output, DisplayFrame *frame) {
92 return output_channel[output].get_display_frame(frame);
95 typedef std::function<void()> new_frame_ready_callback_t;
96 void set_frame_ready_callback(Output output, new_frame_ready_callback_t callback)
98 output_channel[output].set_frame_ready_callback(callback);
101 typedef std::function<void(float level_lufs, float peak_db,
102 float global_level_lufs, float range_low_lufs, float range_high_lufs,
103 float auto_gain_staging_db)> audio_level_callback_t;
104 void set_audio_level_callback(audio_level_callback_t callback)
106 audio_level_callback = callback;
109 std::vector<std::string> get_transition_names()
111 return theme->get_transition_names(pts());
114 unsigned get_num_channels() const
116 return theme->get_num_channels();
119 std::string get_channel_name(unsigned channel) const
121 return theme->get_channel_name(channel);
124 bool get_supports_set_wb(unsigned channel) const
126 return theme->get_supports_set_wb(channel);
129 void set_wb(unsigned channel, double r, double g, double b) const
131 theme->set_wb(channel, r, g, b);
134 void set_locut_cutoff(float cutoff_hz)
136 locut_cutoff_hz = cutoff_hz;
142 void bm_frame(unsigned card_index, uint16_t timecode,
143 FrameAllocator::Frame video_frame, size_t video_offset, uint16_t video_format,
144 FrameAllocator::Frame audio_frame, size_t audio_offset, uint16_t audio_format);
145 void place_rectangle(movit::Effect *resample_effect, movit::Effect *padding_effect, float x0, float y0, float x1, float y1);
147 void process_audio_one_frame();
148 void subsample_chroma(GLuint src_tex, GLuint dst_dst);
149 void release_display_frame(DisplayFrame *frame);
150 double pts() { return double(pts_int) / TIMEBASE; }
155 QSurface *mixer_surface, *h264_encoder_surface;
156 std::unique_ptr<movit::ResourcePool> resource_pool;
157 std::unique_ptr<Theme> theme;
158 std::unique_ptr<movit::EffectChain> display_chain;
159 GLuint cbcr_program_num; // Owned by <resource_pool>.
160 std::unique_ptr<H264Encoder> h264_encoder;
162 // Effects part of <display_chain>. Owned by <display_chain>.
163 movit::FlatInput *display_input;
165 int64_t pts_int = 0; // In TIMEBASE units.
167 std::mutex bmusb_mutex;
170 std::unique_ptr<PBOFrameAllocator> frame_allocator;
172 // Stuff for the OpenGL context (for texture uploading).
174 QOpenGLContext *context;
176 bool new_data_ready = false; // Whether new_frame contains anything.
177 bool should_quit = false;
178 RefCountedFrame new_frame;
179 GLsync new_data_ready_fence; // Whether new_frame is ready for rendering.
180 std::condition_variable new_data_ready_changed; // Set whenever new_data_ready is changed.
181 unsigned dropped_frames = 0; // Before new_frame.
183 std::mutex audio_mutex;
184 std::unique_ptr<Resampler> resampler; // Under audio_mutex.
185 int last_timecode = -1; // Unwrapped.
187 CaptureCard cards[MAX_CARDS]; // protected by <bmusb_mutex>
189 RefCountedFrame bmusb_current_rendering_frame[MAX_CARDS];
191 class OutputChannel {
194 void output_frame(DisplayFrame frame);
195 bool get_display_frame(DisplayFrame *frame);
196 void set_frame_ready_callback(new_frame_ready_callback_t callback);
201 Mixer *parent = nullptr; // Not owned.
202 std::mutex frame_mutex;
203 DisplayFrame current_frame, ready_frame; // protected by <frame_mutex>
204 bool has_current_frame = false, has_ready_frame = false; // protected by <frame_mutex>
205 new_frame_ready_callback_t new_frame_ready_callback;
206 bool has_new_frame_ready_callback = false;
208 OutputChannel output_channel[NUM_OUTPUTS];
210 std::thread mixer_thread;
211 bool should_quit = false;
213 audio_level_callback_t audio_level_callback = nullptr;
216 // TODO: Implement oversampled peak detection.
217 std::atomic<float> peak{0.0f};
219 StereoFilter locut; // Default cutoff 150 Hz, 24 dB/oct.
220 std::atomic<float> locut_cutoff_hz;
222 // First compressor; takes us up to about -12 dBFS.
223 StereoCompressor level_compressor;
224 float last_gain_staging_db = 0.0f;
226 StereoCompressor limiter;
227 StereoCompressor compressor;
230 extern Mixer *global_mixer;
232 #endif // !defined(_MIXER_H)