]> git.sesse.net Git - nageru/blob - mixer.cpp
Hook up the cut button to something that is not really cut, but is more useful than...
[nageru] / mixer.cpp
1 #define GL_GLEXT_PROTOTYPES 1
2 #define NO_SDL_GLEXT 1
3 #define NUM_CARDS 2
4
5 #define WIDTH 1280
6 #define HEIGHT 720
7
8 #include <epoxy/gl.h>
9 #include <epoxy/egl.h>
10
11 #undef Success
12
13 #include <assert.h>
14 #include <features.h>
15 #include <math.h>
16 #include <png.h>
17 #include <pngconf.h>
18 #include <setjmp.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <sys/time.h>
22 #include <time.h>
23 #include <mutex>
24 #include <queue>
25 #include <condition_variable>
26
27 #include <diffusion_effect.h>
28 #include <effect.h>
29 #include <effect_chain.h>
30 #include <flat_input.h>
31 #include <image_format.h>
32 #include <init.h>
33 #include <lift_gamma_gain_effect.h>
34 #include <saturation_effect.h>
35 #include <util.h>
36 #include <ycbcr_input.h>
37 #include <vignette_effect.h>
38 #include <resample_effect.h>
39 #include <resize_effect.h>
40 #include <overlay_effect.h>
41 #include <padding_effect.h>
42 #include <white_balance_effect.h>
43 #include <ycbcr.h>
44 #include <resource_pool.h>
45 #include <effect_util.h>
46
47 #include <EGL/egl.h>
48
49 #include "h264encode.h"
50 #include "context.h"
51 #include "bmusb.h"
52 #include "pbo_frame_allocator.h"
53 #include "mixer.h"
54 #include "ref_counted_gl_sync.h"
55
56 using namespace movit;
57 using namespace std;
58 using namespace std::placeholders;
59
60 Source current_source = SOURCE_INPUT1;
61
62 ResourcePool *resource_pool;
63
64 std::mutex display_frame_mutex;
65 DisplayFrame current_display_frame, ready_display_frame;  // protected by <frame_mutex>
66 bool has_current_display_frame = false, has_ready_display_frame = false;  // protected by <frame_mutex>
67
68 std::mutex bmusb_mutex;
69 struct CaptureCard {
70         BMUSBCapture *usb;
71
72         // Threading stuff
73         bool thread_initialized;
74         QSurface *surface;
75         QOpenGLContext *context;
76
77         bool new_data_ready;  // Whether new_frame contains anything.
78         PBOFrameAllocator::Frame new_frame;
79         GLsync new_data_ready_fence;  // Whether new_frame is ready for rendering.
80         std::condition_variable new_data_ready_changed;  // Set whenever new_data_ready is changed.
81 };
82 CaptureCard cards[NUM_CARDS];  // protected by <bmusb_mutex>
83
84 new_frame_ready_callback_t new_frame_ready_callback;
85 bool has_new_frame_ready_callback = false;
86
87 void bm_frame(int card_index, uint16_t timecode,
88               FrameAllocator::Frame video_frame, size_t video_offset, uint16_t video_format,
89               FrameAllocator::Frame audio_frame, size_t audio_offset, uint16_t audio_format)
90 {
91         CaptureCard *card = &cards[card_index];
92         if (!card->thread_initialized) {
93                 printf("initializing context for bmusb thread %d\n", card_index);
94                 eglBindAPI(EGL_OPENGL_API);
95                 card->context = create_context();
96                 if (!make_current(card->context, card->surface)) {
97                         printf("failed to create bmusb context\n");
98                         exit(1);
99                 }
100                 card->thread_initialized = true;
101         }       
102
103         if (video_frame.len - video_offset != 1280 * 750 * 2) {
104                 printf("dropping frame with wrong length (%ld)\n", video_frame.len - video_offset);
105                 FILE *fp = fopen("frame.raw", "wb");
106                 fwrite(video_frame.data, video_frame.len, 1, fp);
107                 fclose(fp);
108                 //exit(1);
109                 card->usb->get_video_frame_allocator()->release_frame(video_frame);
110                 card->usb->get_audio_frame_allocator()->release_frame(audio_frame);
111                 return;
112         }
113         {
114                 // Wait until the previous frame was consumed.
115                 std::unique_lock<std::mutex> lock(bmusb_mutex);
116                 card->new_data_ready_changed.wait(lock, [card]{ return !card->new_data_ready; });
117         }
118         GLuint pbo = (GLint)(intptr_t)video_frame.userdata;
119         check_error();
120         glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbo);
121         check_error();
122         glFlushMappedBufferRange(GL_PIXEL_UNPACK_BUFFER, 0, video_frame.size);
123         check_error();
124         //glMemoryBarrier(GL_CLIENT_MAPPED_BUFFER_BARRIER_BIT);
125         //check_error();
126         GLsync fence = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, /*flags=*/0);              
127         check_error();
128         assert(fence != nullptr);
129         {
130                 std::unique_lock<std::mutex> lock(bmusb_mutex);
131                 card->new_data_ready = true;
132                 card->new_frame = video_frame;
133                 card->new_data_ready_fence = fence;
134                 card->new_data_ready_changed.notify_all();
135         }
136
137         // Video frame will be released later.
138         card->usb->get_audio_frame_allocator()->release_frame(audio_frame);
139 }
140         
141 void place_rectangle(Effect *resample_effect, Effect *padding_effect, float x0, float y0, float x1, float y1)
142 {
143         float srcx0 = 0.0f;
144         float srcx1 = 1.0f;
145         float srcy0 = 0.0f;
146         float srcy1 = 1.0f;
147
148         // Cull.
149         if (x0 > 1280.0 || x1 < 0.0 || y0 > 720.0 || y1 < 0.0) {
150                 CHECK(resample_effect->set_int("width", 1));
151                 CHECK(resample_effect->set_int("height", 1));
152                 CHECK(resample_effect->set_float("zoom_x", 1280.0));
153                 CHECK(resample_effect->set_float("zoom_y", 720.0));
154                 CHECK(padding_effect->set_int("left", 2000));
155                 CHECK(padding_effect->set_int("top", 2000));
156                 return; 
157         }
158
159         // Clip. (TODO: Clip on upper/left sides, too.)
160         if (x1 > 1280.0) {
161                 srcx1 = (1280.0 - x0) / (x1 - x0);
162                 x1 = 1280.0;
163         }
164         if (y1 > 720.0) {
165                 srcy1 = (720.0 - y0) / (y1 - y0);
166                 y1 = 720.0;
167         }
168
169         float x_subpixel_offset = x0 - floor(x0);
170         float y_subpixel_offset = y0 - floor(y0);
171
172         // Resampling must be to an integral number of pixels. Round up,
173         // and then add an extra pixel so we have some leeway for the border.
174         int width = int(ceil(x1 - x0)) + 1;
175         int height = int(ceil(y1 - y0)) + 1;
176         CHECK(resample_effect->set_int("width", width));
177         CHECK(resample_effect->set_int("height", height));
178
179         // Correct the discrepancy with zoom. (This will leave a small
180         // excess edge of pixels and subpixels, which we'll correct for soon.)
181         float zoom_x = (x1 - x0) / (width * (srcx1 - srcx0));
182         float zoom_y = (y1 - y0) / (height * (srcy1 - srcy0));
183         CHECK(resample_effect->set_float("zoom_x", zoom_x));
184         CHECK(resample_effect->set_float("zoom_y", zoom_y));
185         CHECK(resample_effect->set_float("zoom_center_x", 0.0f));
186         CHECK(resample_effect->set_float("zoom_center_y", 0.0f));
187
188         // Padding must also be to a whole-pixel offset.
189         CHECK(padding_effect->set_int("left", floor(x0)));
190         CHECK(padding_effect->set_int("top", floor(y0)));
191
192         // Correct _that_ discrepancy by subpixel offset in the resampling.
193         CHECK(resample_effect->set_float("left", -x_subpixel_offset / zoom_x));
194         CHECK(resample_effect->set_float("top", -y_subpixel_offset / zoom_y));
195
196         // Finally, adjust the border so it is exactly where we want it.
197         CHECK(padding_effect->set_float("border_offset_left", x_subpixel_offset));
198         CHECK(padding_effect->set_float("border_offset_right", x1 - (floor(x0) + width)));
199         CHECK(padding_effect->set_float("border_offset_top", y_subpixel_offset));
200         CHECK(padding_effect->set_float("border_offset_bottom", y1 - (floor(y0) + height)));
201 }
202         
203 static bool quit = false;
204         
205 void mixer_thread_func(QSurface *surface, QSurface *surface2, QSurface *surface3, QSurface *surface4)
206 {
207         cards[0].surface = surface3;
208 #if NUM_CARDS == 2
209         cards[1].surface = surface4;
210 #endif
211
212         eglBindAPI(EGL_OPENGL_API);
213         QOpenGLContext *context = create_context();
214         if (!make_current(context, surface)) {
215                 printf("oops\n");
216                 exit(1);
217         }
218
219         CHECK(init_movit(MOVIT_SHADER_DIR, MOVIT_DEBUG_OFF));
220         check_error();
221
222         EffectChain chain(WIDTH, HEIGHT);
223         check_error();
224
225         ImageFormat inout_format;
226         inout_format.color_space = COLORSPACE_sRGB;
227         inout_format.gamma_curve = GAMMA_sRGB;
228
229         YCbCrFormat ycbcr_format;
230         ycbcr_format.chroma_subsampling_x = 2;
231         ycbcr_format.chroma_subsampling_y = 1;
232         ycbcr_format.cb_x_position = 0.0;
233         ycbcr_format.cr_x_position = 0.0;
234         ycbcr_format.cb_y_position = 0.5;
235         ycbcr_format.cr_y_position = 0.5;
236         ycbcr_format.luma_coefficients = YCBCR_REC_601;
237         ycbcr_format.full_range = false;
238
239         YCbCrInput *input[NUM_CARDS];
240
241         input[0] = new YCbCrInput(inout_format, ycbcr_format, WIDTH, HEIGHT, YCBCR_INPUT_SPLIT_Y_AND_CBCR);
242         chain.add_input(input[0]);
243         input[1] = new YCbCrInput(inout_format, ycbcr_format, WIDTH, HEIGHT, YCBCR_INPUT_SPLIT_Y_AND_CBCR);
244         chain.add_input(input[1]);
245         Effect *resample_effect = chain.add_effect(new ResampleEffect(), input[0]);
246         Effect *padding_effect = chain.add_effect(new IntegralPaddingEffect());
247         float border_color[] = { 0.0f, 0.0f, 0.0f, 1.0f };
248         CHECK(padding_effect->set_vec4("border_color", border_color));
249
250         Effect *resample2_effect = chain.add_effect(new ResampleEffect(), input[1]);
251         Effect *saturation_effect = chain.add_effect(new SaturationEffect());
252         CHECK(saturation_effect->set_float("saturation", 0.3f));
253         Effect *wb_effect = chain.add_effect(new WhiteBalanceEffect());
254         CHECK(wb_effect->set_float("output_color_temperature", 3500.0));
255         Effect *padding2_effect = chain.add_effect(new IntegralPaddingEffect());
256
257         chain.add_effect(new OverlayEffect(), padding_effect, padding2_effect);
258
259         ycbcr_format.chroma_subsampling_x = 1;
260
261         chain.add_output(inout_format, OUTPUT_ALPHA_FORMAT_POSTMULTIPLIED);
262         chain.add_ycbcr_output(inout_format, OUTPUT_ALPHA_FORMAT_POSTMULTIPLIED, ycbcr_format, YCBCR_OUTPUT_SPLIT_Y_AND_CBCR);
263         chain.set_dither_bits(8);
264         chain.set_output_origin(OUTPUT_ORIGIN_TOP_LEFT);
265         chain.finalize();
266
267         H264Encoder h264_encoder(surface2, WIDTH, HEIGHT, "test.mp4");
268
269         printf("Configuring first card...\n");
270         cards[0].usb = new BMUSBCapture(0x1edb, 0xbd3b);  // 0xbd4f
271         //cards[0].usb = new BMUSBCapture(0x1edb, 0xbd4f);
272         cards[0].usb->set_frame_callback(std::bind(bm_frame, 0, _1, _2, _3, _4, _5, _6, _7));
273         std::unique_ptr<PBOFrameAllocator> pbo_allocator1(new PBOFrameAllocator(1280 * 750 * 2 + 44));
274         cards[0].usb->set_video_frame_allocator(pbo_allocator1.get());
275         cards[0].usb->configure_card();
276
277         std::unique_ptr<PBOFrameAllocator> pbo_allocator2(new PBOFrameAllocator(1280 * 750 * 2 + 44));
278         if (NUM_CARDS == 2) {
279                 printf("Configuring second card...\n");
280                 cards[1].usb = new BMUSBCapture(0x1edb, 0xbd4f);
281                 cards[1].usb->set_frame_callback(std::bind(bm_frame, 1, _1, _2, _3, _4, _5, _6, _7));
282                 cards[1].usb->set_video_frame_allocator(pbo_allocator2.get());
283                 cards[1].usb->configure_card();
284         }
285
286         BMUSBCapture::start_bm_thread();
287
288         for (int card_index = 0; card_index < NUM_CARDS; ++card_index) {
289                 cards[card_index].usb->start_bm_capture();
290         }
291
292         int frame = 0;
293 #if _POSIX_C_SOURCE >= 199309L
294         struct timespec start, now;
295         clock_gettime(CLOCK_MONOTONIC, &start);
296 #else
297         struct timeval start, now;
298         gettimeofday(&start, NULL);
299 #endif
300
301         PBOFrameAllocator::Frame bmusb_current_rendering_frame[NUM_CARDS];
302         for (int card_index = 0; card_index < NUM_CARDS; ++card_index) {
303                 bmusb_current_rendering_frame[card_index] =
304                         cards[card_index].usb->get_video_frame_allocator()->alloc_frame();
305                 GLint input_tex_pbo = (GLint)(intptr_t)bmusb_current_rendering_frame[card_index].userdata;
306                 input[card_index]->set_pixel_data(0, nullptr, input_tex_pbo);
307                 input[card_index]->set_pixel_data(1, nullptr, input_tex_pbo);
308         }
309
310         //chain.enable_phase_timing(true);
311
312         // Set up stuff for NV12 conversion.
313         resource_pool = chain.get_resource_pool();
314         GLuint chroma_tex = resource_pool->create_2d_texture(GL_RG8, WIDTH, HEIGHT);
315
316         // Cb/Cr shader.
317         string cbcr_vert_shader = read_file("vs-cbcr.130.vert");
318         string cbcr_frag_shader =
319                 "#version 130 \n"
320                 "in vec2 tc0; \n"
321                 "uniform sampler2D cbcr_tex; \n"
322                 "void main() { \n"
323                 "    gl_FragColor = texture2D(cbcr_tex, tc0); \n"
324                 "} \n";
325         GLuint cbcr_program_num = resource_pool->compile_glsl_program(cbcr_vert_shader, cbcr_frag_shader);
326
327         GLuint vao;
328         glGenVertexArrays(1, &vao);
329         check_error();
330
331         while (!quit) {
332                 ++frame;
333
334                 //int width0 = lrintf(848 * (1.0 + 0.2 * sin(frame * 0.02)));
335                 int width0 = 848;
336                 int height0 = lrintf(width0 * 9.0 / 16.0);
337
338                 //float top0 = 96 + 48 * sin(frame * 0.005);
339                 //float left0 = 96 + 48 * cos(frame * 0.006);
340                 float top0 = 48;
341                 float left0 = 16;
342                 float bottom0 = top0 + height0;
343                 float right0 = left0 + width0;
344
345                 int width1 = 384;
346                 int height1 = 216;
347         
348                 float bottom1 = 720 - 48;
349                 float right1 = 1280 - 16;
350                 float top1 = bottom1 - height1;
351                 float left1 = right1 - width1;
352         
353                 if (current_source == SOURCE_INPUT1) {
354                         top0 = 0.0;
355                         bottom0 = HEIGHT;
356                         left0 = 0.0;
357                         right0 = WIDTH;
358
359                         top1 = HEIGHT + 10;
360                         bottom1 = HEIGHT + 20;
361                         left1 = WIDTH + 10;
362                         right1 = WIDTH + 20;
363                 } else if (current_source == SOURCE_INPUT2) {
364                         top1 = 0.0;
365                         bottom1 = HEIGHT;
366                         left1 = 0.0;
367                         right1 = WIDTH;
368
369                         top0 = HEIGHT + 10;
370                         bottom0 = HEIGHT + 20;
371                         left0 = WIDTH + 10;
372                         right0 = WIDTH + 20;
373                 } else {
374                         float t = 0.5 + 0.5 * cos(frame * 0.006);
375                         float scale0 = 1.0 + t * (1280.0 / 848.0 - 1.0);
376                         float tx0 = 0.0 + t * (-16.0 * scale0);
377                         float ty0 = 0.0 + t * (-48.0 * scale0);
378
379                         top0 = top0 * scale0 + ty0;
380                         bottom0 = bottom0 * scale0 + ty0;
381                         left0 = left0 * scale0 + tx0;
382                         right0 = right0 * scale0 + tx0;
383
384                         top1 = top1 * scale0 + ty0;
385                         bottom1 = bottom1 * scale0 + ty0;
386                         left1 = left1 * scale0 + tx0;
387                         right1 = right1 * scale0 + tx0;
388                 }
389
390                 place_rectangle(resample_effect, padding_effect, left0, top0, right0, bottom0);
391                 place_rectangle(resample2_effect, padding2_effect, left1, top1, right1, bottom1);
392
393                 CaptureCard card_copy[NUM_CARDS];
394
395                 {
396                         std::unique_lock<std::mutex> lock(bmusb_mutex);
397
398                         // The first card is the master timer, so wait for it to have a new frame.
399                         // TODO: Make configurable, and with a timeout.
400                         cards[0].new_data_ready_changed.wait(lock, []{ return cards[0].new_data_ready; });
401
402                         for (int card_index = 0; card_index < NUM_CARDS; ++card_index) {
403                                 CaptureCard *card = &cards[card_index];
404                                 card_copy[card_index].usb = card->usb;
405                                 card_copy[card_index].new_data_ready = card->new_data_ready;
406                                 card_copy[card_index].new_frame = card->new_frame;
407                                 card_copy[card_index].new_data_ready_fence = card->new_data_ready_fence;
408                                 card->new_data_ready = false;
409                                 card->new_data_ready_changed.notify_all();
410                         }
411                 }
412
413                 vector<FrameAllocator::Frame> input_frames_to_release;
414         
415                 for (int card_index = 0; card_index < NUM_CARDS; ++card_index) {
416                         CaptureCard *card = &card_copy[card_index];
417                         if (!card->new_data_ready)
418                                 continue;
419
420                         // Now we're done with the previous frame, so we can definitely
421                         // release it when this is done rendering. (Actually, we could do
422                         // it one frame earlier, but before we have a new one, there's no
423                         // knowing when the current one is released.)
424                         input_frames_to_release.push_back(bmusb_current_rendering_frame[card_index]);
425                         bmusb_current_rendering_frame[card_index] = card->new_frame;
426                         check_error();
427
428                         // The new texture might still be uploaded,
429                         // tell the GPU to wait until it's there.
430                         if (card->new_data_ready_fence)
431                                 glWaitSync(card->new_data_ready_fence, /*flags=*/0, GL_TIMEOUT_IGNORED);
432                         check_error();
433                         glDeleteSync(card->new_data_ready_fence);
434                         check_error();
435                         GLint input_tex_pbo = (GLint)(intptr_t)card->new_frame.userdata;
436                         input[card_index]->set_pixel_data(0, (unsigned char *)BUFFER_OFFSET((1280 * 750 * 2 + 44) / 2 + 1280 * 25 + 22), input_tex_pbo);
437                         input[card_index]->set_pixel_data(1, (unsigned char *)BUFFER_OFFSET(1280 * 25 + 22), input_tex_pbo);
438
439                         if (NUM_CARDS == 1) {
440                                 // Set to the other one, too.
441                                 input[1]->set_pixel_data(0, (unsigned char *)BUFFER_OFFSET((1280 * 750 * 2 + 44) / 2 + 1280 * 25 + 22), input_tex_pbo);
442                                 input[1]->set_pixel_data(1, (unsigned char *)BUFFER_OFFSET(1280 * 25 + 22), input_tex_pbo);
443                         }
444                 }
445
446                 GLuint y_tex, cbcr_tex;
447                 bool got_frame = h264_encoder.begin_frame(&y_tex, &cbcr_tex);
448                 assert(got_frame);
449
450                 // Render chain.
451                 GLuint rgba_tex = resource_pool->create_2d_texture(GL_RGBA8, WIDTH, HEIGHT);
452                 GLuint ycbcr_fbo = resource_pool->create_fbo(y_tex, chroma_tex, rgba_tex);
453                 chain.render_to_fbo(ycbcr_fbo, WIDTH, HEIGHT);
454                 resource_pool->release_fbo(ycbcr_fbo);
455
456                 // Set up for extraction.
457                 float vertices[] = {
458                         0.0f, 2.0f,
459                         0.0f, 0.0f,
460                         2.0f, 0.0f
461                 };
462
463                 glBindVertexArray(vao);
464                 check_error();
465
466                 // Extract Cb/Cr.
467                 GLuint cbcr_fbo = resource_pool->create_fbo(cbcr_tex);
468                 glBindFramebuffer(GL_FRAMEBUFFER, cbcr_fbo);
469                 glViewport(0, 0, WIDTH/2, HEIGHT/2);
470                 check_error();
471
472                 glUseProgram(cbcr_program_num);
473                 check_error();
474
475                 glActiveTexture(GL_TEXTURE0);
476                 check_error();
477                 glBindTexture(GL_TEXTURE_2D, chroma_tex);
478                 check_error();
479                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
480                 check_error();
481                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
482                 check_error();
483                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
484                 check_error();
485
486                 float chroma_offset_0[] = { -0.5f / WIDTH, 0.0f };
487                 set_uniform_vec2(cbcr_program_num, "foo", "chroma_offset_0", chroma_offset_0);
488
489                 GLuint position_vbo = fill_vertex_attribute(cbcr_program_num, "position", 2, GL_FLOAT, sizeof(vertices), vertices);
490                 GLuint texcoord_vbo = fill_vertex_attribute(cbcr_program_num, "texcoord", 2, GL_FLOAT, sizeof(vertices), vertices);  // Same as vertices.
491
492                 glDrawArrays(GL_TRIANGLES, 0, 3);
493                 check_error();
494
495                 RefCountedGLsync fence(GL_SYNC_GPU_COMMANDS_COMPLETE, /*flags=*/0);
496                 check_error();
497
498                 cleanup_vertex_attribute(cbcr_program_num, "position", position_vbo);
499                 cleanup_vertex_attribute(cbcr_program_num, "texcoord", texcoord_vbo);
500
501                 glUseProgram(0);
502                 check_error();
503
504                 resource_pool->release_fbo(cbcr_fbo);
505
506                 h264_encoder.end_frame(fence, input_frames_to_release);
507
508                 // Store this frame for display. Remove the ready frame if any
509                 // (it was seemingly never used).
510                 {
511                         std::unique_lock<std::mutex> lock(display_frame_mutex);
512                         if (has_ready_display_frame) {
513                                 resource_pool->release_2d_texture(ready_display_frame.texnum);
514                                 ready_display_frame.ready_fence.reset();
515                         }
516                         ready_display_frame.texnum = rgba_tex;
517                         ready_display_frame.ready_fence = fence;
518                         has_ready_display_frame = true;
519                 }
520
521                 if (has_new_frame_ready_callback) {
522                         new_frame_ready_callback();
523                 }
524
525 #if 1
526 #if _POSIX_C_SOURCE >= 199309L
527                 clock_gettime(CLOCK_MONOTONIC, &now);
528                 double elapsed = now.tv_sec - start.tv_sec +
529                         1e-9 * (now.tv_nsec - start.tv_nsec);
530 #else
531                 gettimeofday(&now, NULL);
532                 double elapsed = now.tv_sec - start.tv_sec +
533                         1e-6 * (now.tv_usec - start.tv_usec);
534 #endif
535                 if (frame % 100 == 0) {
536                         printf("%d frames in %.3f seconds = %.1f fps (%.1f ms/frame)\n",
537                                 frame, elapsed, frame / elapsed,
538                                 1e3 * elapsed / frame);
539                 //      chain.print_phase_timing();
540                 }
541
542                 // Reset every 100 frames, so that local variations in frame times
543                 // (especially for the first few frames, when the shaders are
544                 // compiled etc.) don't make it hard to measure for the entire
545                 // remaining duration of the program.
546                 if (frame == 10000) {
547                         frame = 0;
548                         start = now;
549                 }
550 #endif
551                 check_error();
552         }
553         glDeleteVertexArrays(1, &vao);
554         resource_pool->release_glsl_program(cbcr_program_num);
555         resource_pool->release_2d_texture(chroma_tex);
556         BMUSBCapture::stop_bm_thread();
557 }
558
559 bool mixer_get_display_frame(DisplayFrame *frame)
560 {
561         std::unique_lock<std::mutex> lock(display_frame_mutex);
562         if (!has_current_display_frame && !has_ready_display_frame) {
563                 return false;
564         }
565
566         if (has_current_display_frame && has_ready_display_frame) {
567                 // We have a new ready frame. Toss the current one.
568                 resource_pool->release_2d_texture(current_display_frame.texnum);
569                 current_display_frame.ready_fence.reset();
570                 has_current_display_frame = false;
571         }
572         if (has_ready_display_frame) {
573                 assert(!has_current_display_frame);
574                 current_display_frame = ready_display_frame;
575                 ready_display_frame.ready_fence.reset();  // Drop the refcount.
576                 has_current_display_frame = true;
577                 has_ready_display_frame = false;
578         }
579
580         *frame = current_display_frame;
581         return true;
582 }
583
584 void set_frame_ready_fallback(new_frame_ready_callback_t callback)
585 {
586         new_frame_ready_callback = callback;
587         has_new_frame_ready_callback = true;
588 }
589
590 std::thread mixer_thread;
591
592 void start_mixer(QSurface *surface, QSurface *surface2, QSurface *surface3, QSurface *surface4)
593 {
594         mixer_thread = std::thread([surface, surface2, surface3, surface4]{
595                 mixer_thread_func(surface, surface2, surface3, surface4);
596         });
597 }
598
599 void mixer_quit()
600 {
601         quit = true;
602         mixer_thread.join();
603 }
604
605 void mixer_cut(Source source)
606 {
607         current_source = source;
608 }