1 #define GL_GLEXT_PROTOTYPES 1
25 #include <condition_variable>
27 #include <diffusion_effect.h>
29 #include <effect_chain.h>
30 #include <flat_input.h>
31 #include <image_format.h>
33 #include <lift_gamma_gain_effect.h>
34 #include <saturation_effect.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>
44 #include <resource_pool.h>
45 #include <effect_util.h>
49 #include "h264encode.h"
52 #include "pbo_frame_allocator.h"
54 using namespace movit;
56 using namespace std::placeholders;
58 // shared between all EGL contexts
59 EGLDisplay egl_display;
60 EGLSurface egl_surface;
63 EGL_CONTEXT_CLIENT_VERSION, 2,
64 EGL_CONTEXT_MAJOR_VERSION_KHR, 3,
65 EGL_CONTEXT_MINOR_VERSION_KHR, 1,
66 //EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR, EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR,
67 EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR, EGL_CONTEXT_OPENGL_COMPATIBILITY_PROFILE_BIT,
71 EGLConfig pbuffer_ecfg;
73 std::mutex bmusb_mutex; // protects <cards>
79 bool thread_initialized;
81 QOpenGLContext *context;
83 bool new_data_ready; // Whether new_frame contains anything.
84 PBOFrameAllocator::Frame new_frame;
85 GLsync new_data_ready_fence; // Whether new_frame is ready for rendering.
86 std::condition_variable new_data_ready_changed; // Set whenever new_data_ready is changed.
88 CaptureCard cards[NUM_CARDS];
90 void bm_frame(int card_index, uint16_t timecode,
91 FrameAllocator::Frame video_frame, size_t video_offset, uint16_t video_format,
92 FrameAllocator::Frame audio_frame, size_t audio_offset, uint16_t audio_format)
94 CaptureCard *card = &cards[card_index];
95 if (!card->thread_initialized) {
96 printf("initializing context for bmusb thread %d\n", card_index);
97 eglBindAPI(EGL_OPENGL_API);
98 card->context = create_context();
99 if (!make_current(card->context, card->surface)) {
100 printf("failed to create bmusb context\n");
103 card->thread_initialized = true;
106 if (video_frame.len - video_offset != 1280 * 750 * 2) {
107 printf("dropping frame with wrong length (%ld)\n", video_frame.len - video_offset);
108 FILE *fp = fopen("frame.raw", "wb");
109 fwrite(video_frame.data, video_frame.len, 1, fp);
112 card->usb->get_video_frame_allocator()->release_frame(video_frame);
113 card->usb->get_audio_frame_allocator()->release_frame(audio_frame);
117 // Wait until the previous frame was consumed.
118 std::unique_lock<std::mutex> lock(bmusb_mutex);
119 card->new_data_ready_changed.wait(lock, [card]{ return !card->new_data_ready; });
121 GLuint pbo = (GLint)(intptr_t)video_frame.userdata;
123 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbo);
125 glFlushMappedBufferRange(GL_PIXEL_UNPACK_BUFFER, 0, video_frame.size);
127 //glMemoryBarrier(GL_CLIENT_MAPPED_BUFFER_BARRIER_BIT);
129 GLsync fence = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, /*flags=*/0);
131 assert(fence != nullptr);
133 std::unique_lock<std::mutex> lock(bmusb_mutex);
134 card->new_data_ready = true;
135 card->new_frame = video_frame;
136 card->new_data_ready_fence = fence;
137 card->new_data_ready_changed.notify_all();
140 // Video frame will be released later.
141 card->usb->get_audio_frame_allocator()->release_frame(audio_frame);
144 void place_rectangle(Effect *resample_effect, Effect *padding_effect, float x0, float y0, float x1, float y1)
152 if (x0 > 1280.0 || x1 < 0.0 || y0 > 720.0 || y1 < 0.0) {
153 CHECK(resample_effect->set_int("width", 1));
154 CHECK(resample_effect->set_int("height", 1));
155 CHECK(resample_effect->set_float("zoom_x", 1280.0));
156 CHECK(resample_effect->set_float("zoom_y", 720.0));
157 CHECK(padding_effect->set_int("left", 2000));
158 CHECK(padding_effect->set_int("top", 2000));
162 // Clip. (TODO: Clip on upper/left sides, too.)
164 srcx1 = (1280.0 - x0) / (x1 - x0);
168 srcy1 = (720.0 - y0) / (y1 - y0);
172 float x_subpixel_offset = x0 - floor(x0);
173 float y_subpixel_offset = y0 - floor(y0);
175 // Resampling must be to an integral number of pixels. Round up,
176 // and then add an extra pixel so we have some leeway for the border.
177 int width = int(ceil(x1 - x0)) + 1;
178 int height = int(ceil(y1 - y0)) + 1;
179 CHECK(resample_effect->set_int("width", width));
180 CHECK(resample_effect->set_int("height", height));
182 // Correct the discrepancy with zoom. (This will leave a small
183 // excess edge of pixels and subpixels, which we'll correct for soon.)
184 float zoom_x = (x1 - x0) / (width * (srcx1 - srcx0));
185 float zoom_y = (y1 - y0) / (height * (srcy1 - srcy0));
186 CHECK(resample_effect->set_float("zoom_x", zoom_x));
187 CHECK(resample_effect->set_float("zoom_y", zoom_y));
188 CHECK(resample_effect->set_float("zoom_center_x", 0.0f));
189 CHECK(resample_effect->set_float("zoom_center_y", 0.0f));
191 // Padding must also be to a whole-pixel offset.
192 CHECK(padding_effect->set_int("left", floor(x0)));
193 CHECK(padding_effect->set_int("top", floor(y0)));
195 // Correct _that_ discrepancy by subpixel offset in the resampling.
196 CHECK(resample_effect->set_float("left", -x_subpixel_offset / zoom_x));
197 CHECK(resample_effect->set_float("top", -y_subpixel_offset / zoom_y));
199 // Finally, adjust the border so it is exactly where we want it.
200 CHECK(padding_effect->set_float("border_offset_left", x_subpixel_offset));
201 CHECK(padding_effect->set_float("border_offset_right", x1 - (floor(x0) + width)));
202 CHECK(padding_effect->set_float("border_offset_top", y_subpixel_offset));
203 CHECK(padding_effect->set_float("border_offset_bottom", y1 - (floor(y0) + height)));
206 static bool quit = false;
208 void mixer_thread_func(QSurface *surface, QSurface *surface2, QSurface *surface3, QSurface *surface4)
210 cards[0].surface = surface3;
212 cards[1].surface = surface4;
215 eglBindAPI(EGL_OPENGL_API);
216 QOpenGLContext *context = create_context();
217 if (!make_current(context, surface)) {
222 CHECK(init_movit("/usr/share/movit", MOVIT_DEBUG_ON));
225 EffectChain chain(WIDTH, HEIGHT);
228 ImageFormat inout_format;
229 inout_format.color_space = COLORSPACE_sRGB;
230 inout_format.gamma_curve = GAMMA_sRGB;
232 YCbCrFormat ycbcr_format;
233 ycbcr_format.chroma_subsampling_x = 2;
234 ycbcr_format.chroma_subsampling_y = 1;
235 ycbcr_format.cb_x_position = 0.0;
236 ycbcr_format.cr_x_position = 0.0;
237 ycbcr_format.cb_y_position = 0.5;
238 ycbcr_format.cr_y_position = 0.5;
239 ycbcr_format.luma_coefficients = YCBCR_REC_601;
240 ycbcr_format.full_range = false;
242 YCbCrInput *input[NUM_CARDS];
244 input[0] = new YCbCrInput(inout_format, ycbcr_format, WIDTH, HEIGHT, YCBCR_INPUT_SPLIT_Y_AND_CBCR);
245 chain.add_input(input[0]);
246 input[1] = new YCbCrInput(inout_format, ycbcr_format, WIDTH, HEIGHT, YCBCR_INPUT_SPLIT_Y_AND_CBCR);
247 chain.add_input(input[1]);
248 Effect *resample_effect = chain.add_effect(new ResampleEffect(), input[0]);
249 Effect *padding_effect = chain.add_effect(new IntegralPaddingEffect());
250 float border_color[] = { 0.0f, 0.0f, 0.0f, 1.0f };
251 CHECK(padding_effect->set_vec4("border_color", border_color));
253 Effect *resample2_effect = chain.add_effect(new ResampleEffect(), input[1]);
254 Effect *saturation_effect = chain.add_effect(new SaturationEffect());
255 CHECK(saturation_effect->set_float("saturation", 0.3f));
256 Effect *wb_effect = chain.add_effect(new WhiteBalanceEffect());
257 CHECK(wb_effect->set_float("output_color_temperature", 3500.0));
258 Effect *padding2_effect = chain.add_effect(new IntegralPaddingEffect());
260 chain.add_effect(new OverlayEffect(), padding_effect, padding2_effect);
262 ycbcr_format.chroma_subsampling_x = 1;
264 chain.add_ycbcr_output(inout_format, OUTPUT_ALPHA_FORMAT_POSTMULTIPLIED, ycbcr_format, YCBCR_OUTPUT_SPLIT_Y_AND_CBCR);
265 chain.set_dither_bits(8);
266 chain.set_output_origin(OUTPUT_ORIGIN_TOP_LEFT);
269 H264Encoder h264_encoder(surface2, WIDTH, HEIGHT, "test.mp4");
271 printf("Configuring first card...\n");
272 cards[0].usb = new BMUSBCapture(0x1edb, 0xbd3b); // 0xbd4f
273 //cards[0].usb = new BMUSBCapture(0x1edb, 0xbd4f);
274 cards[0].usb->set_frame_callback(std::bind(bm_frame, 0, _1, _2, _3, _4, _5, _6, _7));
275 std::unique_ptr<PBOFrameAllocator> pbo_allocator1(new PBOFrameAllocator(1280 * 750 * 2 + 44));
276 cards[0].usb->set_video_frame_allocator(pbo_allocator1.get());
277 cards[0].usb->configure_card();
279 std::unique_ptr<PBOFrameAllocator> pbo_allocator2(new PBOFrameAllocator(1280 * 750 * 2 + 44));
280 if (NUM_CARDS == 2) {
281 printf("Configuring second card...\n");
282 cards[1].usb = new BMUSBCapture(0x1edb, 0xbd4f);
283 cards[1].usb->set_frame_callback(std::bind(bm_frame, 1, _1, _2, _3, _4, _5, _6, _7));
284 cards[1].usb->set_video_frame_allocator(pbo_allocator2.get());
285 cards[1].usb->configure_card();
288 BMUSBCapture::start_bm_thread();
290 for (int card_index = 0; card_index < NUM_CARDS; ++card_index) {
291 cards[card_index].usb->start_bm_capture();
295 #if _POSIX_C_SOURCE >= 199309L
296 struct timespec start, now;
297 clock_gettime(CLOCK_MONOTONIC, &start);
299 struct timeval start, now;
300 gettimeofday(&start, NULL);
303 PBOFrameAllocator::Frame bmusb_current_rendering_frame[NUM_CARDS];
304 for (int card_index = 0; card_index < NUM_CARDS; ++card_index) {
305 bmusb_current_rendering_frame[card_index] =
306 cards[card_index].usb->get_video_frame_allocator()->alloc_frame();
307 GLint input_tex_pbo = (GLint)(intptr_t)bmusb_current_rendering_frame[card_index].userdata;
308 input[card_index]->set_pixel_data(0, nullptr, input_tex_pbo);
309 input[card_index]->set_pixel_data(1, nullptr, input_tex_pbo);
313 //chain.enable_phase_timing(true);
315 // Set up stuff for NV12 conversion.
316 ResourcePool *resource_pool = chain.get_resource_pool();
317 GLuint chroma_tex = resource_pool->create_2d_texture(GL_RG8, WIDTH, HEIGHT);
320 string cbcr_vert_shader = read_file("vs-cbcr.130.vert");
321 string cbcr_frag_shader =
324 "uniform sampler2D cbcr_tex; \n"
326 " gl_FragColor = texture2D(cbcr_tex, tc0); \n"
328 GLuint cbcr_program_num = resource_pool->compile_glsl_program(cbcr_vert_shader, cbcr_frag_shader);
331 glGenVertexArrays(1, &vao);
337 //int width0 = lrintf(848 * (1.0 + 0.2 * sin(frame * 0.02)));
339 int height0 = lrintf(width0 * 9.0 / 16.0);
341 //float top0 = 96 + 48 * sin(frame * 0.005);
342 //float left0 = 96 + 48 * cos(frame * 0.006);
345 float bottom0 = top0 + height0;
346 float right0 = left0 + width0;
351 float bottom1 = 720 - 48;
352 float right1 = 1280 - 16;
353 float top1 = bottom1 - height1;
354 float left1 = right1 - width1;
356 float t = 0.5 + 0.5 * cos(frame * 0.006);
358 float scale0 = 1.0 + t * (1280.0 / 848.0 - 1.0);
359 float tx0 = 0.0 + t * (-16.0 * scale0);
360 float ty0 = 0.0 + t * (-48.0 * scale0);
362 top0 = top0 * scale0 + ty0;
363 bottom0 = bottom0 * scale0 + ty0;
364 left0 = left0 * scale0 + tx0;
365 right0 = right0 * scale0 + tx0;
367 top1 = top1 * scale0 + ty0;
368 bottom1 = bottom1 * scale0 + ty0;
369 left1 = left1 * scale0 + tx0;
370 right1 = right1 * scale0 + tx0;
372 place_rectangle(resample_effect, padding_effect, left0, top0, right0, bottom0);
373 place_rectangle(resample2_effect, padding2_effect, left1, top1, right1, bottom1);
375 CaptureCard card_copy[NUM_CARDS];
378 std::unique_lock<std::mutex> lock(bmusb_mutex);
380 // The first card is the master timer, so wait for it to have a new frame.
381 // TODO: Make configurable, and with a timeout.
382 cards[0].new_data_ready_changed.wait(lock, []{ return cards[0].new_data_ready; });
384 for (int card_index = 0; card_index < NUM_CARDS; ++card_index) {
385 CaptureCard *card = &cards[card_index];
386 card_copy[card_index].usb = card->usb;
387 card_copy[card_index].new_data_ready = card->new_data_ready;
388 card_copy[card_index].new_frame = card->new_frame;
389 card_copy[card_index].new_data_ready_fence = card->new_data_ready_fence;
390 card->new_data_ready = false;
391 card->new_data_ready_changed.notify_all();
395 vector<FrameAllocator::Frame> input_frames_to_release;
397 for (int card_index = 0; card_index < NUM_CARDS; ++card_index) {
398 CaptureCard *card = &card_copy[card_index];
399 if (!card->new_data_ready)
402 // Now we're done with the previous frame, so we can definitely
403 // release it when this is done rendering. (Actually, we could do
404 // it one frame earlier, but before we have a new one, there's no
405 // knowing when the current one is released.)
406 input_frames_to_release.push_back(bmusb_current_rendering_frame[card_index]);
407 bmusb_current_rendering_frame[card_index] = card->new_frame;
409 // The new texture might still be uploaded,
410 // tell the GPU to wait until it's there.
411 if (card->new_data_ready_fence)
412 glWaitSync(card->new_data_ready_fence, /*flags=*/0, GL_TIMEOUT_IGNORED);
414 glDeleteSync(card->new_data_ready_fence);
416 GLint input_tex_pbo = (GLint)(intptr_t)card->new_frame.userdata;
417 input[card_index]->set_pixel_data(0, (unsigned char *)BUFFER_OFFSET((1280 * 750 * 2 + 44) / 2 + 1280 * 25 + 22), input_tex_pbo);
418 input[card_index]->set_pixel_data(1, (unsigned char *)BUFFER_OFFSET(1280 * 25 + 22), input_tex_pbo);
420 if (NUM_CARDS == 1) {
421 // Set to the other one, too.
422 input[1]->set_pixel_data(0, (unsigned char *)BUFFER_OFFSET((1280 * 750 * 2 + 44) / 2 + 1280 * 25 + 22), input_tex_pbo);
423 input[1]->set_pixel_data(1, (unsigned char *)BUFFER_OFFSET(1280 * 25 + 22), input_tex_pbo);
427 GLuint y_tex, cbcr_tex;
428 bool got_frame = h264_encoder.begin_frame(&y_tex, &cbcr_tex);
433 GLuint ycbcr_fbo = resource_pool->create_fbo(y_tex, chroma_tex);
434 chain.render_to_fbo(ycbcr_fbo, WIDTH, HEIGHT);
435 resource_pool->release_fbo(ycbcr_fbo);
438 // Set up for extraction.
445 glBindVertexArray(vao);
449 GLuint cbcr_fbo = resource_pool->create_fbo(cbcr_tex);
450 glBindFramebuffer(GL_FRAMEBUFFER, cbcr_fbo);
451 glViewport(0, 0, WIDTH/2, HEIGHT/2);
455 glUseProgram(cbcr_program_num);
458 glActiveTexture(GL_TEXTURE0);
460 glBindTexture(GL_TEXTURE_2D, chroma_tex);
462 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
464 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
466 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
469 float chroma_offset_0[] = { -0.5f / WIDTH, 0.0f };
470 set_uniform_vec2(cbcr_program_num, "foo", "chroma_offset_0", chroma_offset_0);
472 GLuint position_vbo = fill_vertex_attribute(cbcr_program_num, "position", 2, GL_FLOAT, sizeof(vertices), vertices);
473 GLuint texcoord_vbo = fill_vertex_attribute(cbcr_program_num, "texcoord", 2, GL_FLOAT, sizeof(vertices), vertices); // Same as vertices.
475 glDrawArrays(GL_TRIANGLES, 0, 3);
478 cleanup_vertex_attribute(cbcr_program_num, "position", position_vbo);
479 cleanup_vertex_attribute(cbcr_program_num, "texcoord", texcoord_vbo);
484 fence = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, /*flags=*/0);
487 resource_pool->release_fbo(cbcr_fbo);
490 h264_encoder.end_frame(fence, input_frames_to_release);
494 #if _POSIX_C_SOURCE >= 199309L
495 clock_gettime(CLOCK_MONOTONIC, &now);
496 double elapsed = now.tv_sec - start.tv_sec +
497 1e-9 * (now.tv_nsec - start.tv_nsec);
499 gettimeofday(&now, NULL);
500 double elapsed = now.tv_sec - start.tv_sec +
501 1e-6 * (now.tv_usec - start.tv_usec);
503 if (frame % 100 == 0) {
504 printf("%d frames in %.3f seconds = %.1f fps (%.1f ms/frame)\n",
505 frame, elapsed, frame / elapsed,
506 1e3 * elapsed / frame);
507 // chain.print_phase_timing();
510 // Reset every 100 frames, so that local variations in frame times
511 // (especially for the first few frames, when the shaders are
512 // compiled etc.) don't make it hard to measure for the entire
513 // remaining duration of the program.
514 if (frame == 10000) {
520 glDeleteVertexArrays(1, &vao);
521 resource_pool->release_glsl_program(cbcr_program_num);
522 resource_pool->release_2d_texture(chroma_tex);
523 BMUSBCapture::stop_bm_thread();
526 std::thread mixer_thread;
528 void start_mixer(QSurface *surface, QSurface *surface2, QSurface *surface3, QSurface *surface4)
530 mixer_thread = std::thread([surface, surface2, surface3, surface4]{
531 mixer_thread_func(surface, surface2, surface3, surface4);