X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=mixer.cpp;h=6e1e45d953762ba55523248ea9a38345fe669c23;hb=906cab7a1020e9f416a8b768c28ea25a9caac66a;hp=13fa29928ad8ecd9efc7f0d706c84621d51e761c;hpb=5c8dc09f89df0f25434bcefc9102438d2b491df2;p=nageru diff --git a/mixer.cpp b/mixer.cpp index 13fa299..6e1e45d 100644 --- a/mixer.cpp +++ b/mixer.cpp @@ -10,12 +10,12 @@ #include #include #include +#include #include #include #include #include #include -#include #include #include #include @@ -59,10 +59,42 @@ void convert_fixed24_to_fp32(float *dst, size_t out_channels, const uint8_t *src } } +void insert_new_frame(RefCountedFrame frame, unsigned field_num, bool interlaced, unsigned card_index, InputState *input_state) +{ + if (interlaced) { + for (unsigned frame_num = FRAME_HISTORY_LENGTH; frame_num --> 1; ) { // :-) + input_state->buffered_frames[card_index][frame_num] = + input_state->buffered_frames[card_index][frame_num - 1]; + } + input_state->buffered_frames[card_index][0] = { frame, field_num }; + } else { + for (unsigned frame_num = 0; frame_num < FRAME_HISTORY_LENGTH; ++frame_num) { + input_state->buffered_frames[card_index][frame_num] = { frame, field_num }; + } + } +} + +string generate_local_dump_filename(int frame) +{ + time_t now = time(NULL); + tm now_tm; + localtime_r(&now, &now_tm); + + char timestamp[256]; + strftime(timestamp, sizeof(timestamp), "%F-%T%z", &now_tm); + + // Use the frame number to disambiguate between two cuts starting + // on the same second. + char filename[256]; + snprintf(filename, sizeof(filename), "%s%s-f%02d%s", + LOCAL_DUMP_PREFIX, timestamp, frame % 100, LOCAL_DUMP_SUFFIX); + return filename; +} + } // namespace Mixer::Mixer(const QSurfaceFormat &format, unsigned num_cards) - : httpd(LOCAL_DUMP_FILE_NAME, WIDTH, HEIGHT), + : httpd(WIDTH, HEIGHT), num_cards(num_cards), mixer_surface(create_surface(format)), h264_encoder_surface(create_surface(format)), @@ -70,6 +102,7 @@ Mixer::Mixer(const QSurfaceFormat &format, unsigned num_cards) limiter(OUTPUT_FREQUENCY), compressor(OUTPUT_FREQUENCY) { + httpd.open_output_file(generate_local_dump_filename(/*frame=*/0).c_str()); httpd.start(9095); CHECK(init_movit(MOVIT_SHADER_DIR, MOVIT_DEBUG_OFF)); @@ -130,8 +163,6 @@ Mixer::Mixer(const QSurfaceFormat &format, unsigned num_cards) cards[card_index].usb->start_bm_capture(); } - //chain->enable_phase_timing(true); - // Set up stuff for NV12 conversion. // Cb/Cr shader. @@ -143,7 +174,8 @@ Mixer::Mixer(const QSurfaceFormat &format, unsigned num_cards) "void main() { \n" " gl_FragColor = texture2D(cbcr_tex, tc0); \n" "} \n"; - cbcr_program_num = resource_pool->compile_glsl_program(cbcr_vert_shader, cbcr_frag_shader); + vector frag_shader_outputs; + cbcr_program_num = resource_pool->compile_glsl_program(cbcr_vert_shader, cbcr_frag_shader, frag_shader_outputs); r128.init(2, OUTPUT_FREQUENCY); r128.integr_start(); @@ -190,7 +222,7 @@ float find_peak(const float *samples, size_t num_samples) { float m = fabs(samples[0]); for (size_t i = 1; i < num_samples; ++i) { - m = std::max(m, fabs(samples[i])); + m = max(m, fabs(samples[i])); } return m; } @@ -267,8 +299,7 @@ void Mixer::bm_frame(unsigned card_index, uint16_t timecode, // Insert silence as needed. fprintf(stderr, "Card %d dropped %d frame(s) (before timecode 0x%04x), inserting silence.\n", card_index, dropped_frames, timecode); - vector silence; - silence.resize(silence_samples * 2); + vector silence(silence_samples * 2, 0.0f); for (int i = 0; i < dropped_frames; ++i) { card->resampling_queue->add_input_samples(local_pts / double(TIMEBASE), silence.data(), silence_samples); // Note that if the format changed in the meantime, we have @@ -299,11 +330,12 @@ void Mixer::bm_frame(unsigned card_index, uint16_t timecode, if (card->should_quit) return; } + size_t expected_length = width * (height + extra_lines_top + extra_lines_bottom) * 2; if (video_frame.len - video_offset == 0 || - video_frame.len - video_offset != size_t(width * (height + extra_lines_top + extra_lines_bottom) * 2)) { + video_frame.len - video_offset != expected_length) { if (video_frame.len != 0) { - printf("Card %d: Dropping video frame with wrong length (%ld)\n", - card_index, video_frame.len - video_offset); + printf("Card %d: Dropping video frame with wrong length (%ld; expected %ld)\n", + card_index, video_frame.len - video_offset, expected_length); } if (video_frame.owner) { video_frame.owner->release_frame(video_frame); @@ -316,6 +348,7 @@ void Mixer::bm_frame(unsigned card_index, uint16_t timecode, card->new_data_ready = true; card->new_frame = RefCountedFrame(FrameAllocator::Frame()); card->new_frame_length = frame_length; + card->new_frame_interlaced = false; card->new_data_ready_fence = nullptr; card->dropped_frames = dropped_frames; card->new_data_ready_changed.notify_all(); @@ -328,9 +361,8 @@ void Mixer::bm_frame(unsigned card_index, uint16_t timecode, unsigned num_fields = interlaced ? 2 : 1; timespec frame_upload_start; if (interlaced) { - // NOTE: This isn't deinterlacing. This is just sending the two fields along - // as separate frames without considering anything like the half-field offset. - // We'll need to add a proper deinterlacer on the receiving side to get this right. + // Send the two fields along as separate frames; the other side will need to add + // a deinterlacer to actually get this right. assert(height % 2 == 0); height /= 2; assert(frame_length % 2 == 0); @@ -338,6 +370,9 @@ void Mixer::bm_frame(unsigned card_index, uint16_t timecode, num_fields = 2; clock_gettime(CLOCK_MONOTONIC, &frame_upload_start); } + userdata->last_interlaced = interlaced; + userdata->last_frame_rate_nom = frame_rate_nom; + userdata->last_frame_rate_den = frame_rate_den; RefCountedFrame new_frame(video_frame); // Upload the textures. @@ -371,10 +406,8 @@ void Mixer::bm_frame(unsigned card_index, uint16_t timecode, check_error(); glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbo); check_error(); - glFlushMappedBufferRange(GL_PIXEL_UNPACK_BUFFER, 0, video_frame.size); + glMemoryBarrier(GL_CLIENT_MAPPED_BUFFER_BARRIER_BIT); check_error(); - //glMemoryBarrier(GL_CLIENT_MAPPED_BUFFER_BARRIER_BIT); - //check_error(); glBindTexture(GL_TEXTURE_2D, userdata->tex_cbcr[field]); check_error(); @@ -415,6 +448,7 @@ void Mixer::bm_frame(unsigned card_index, uint16_t timecode, card->new_frame = new_frame; card->new_frame_length = frame_length; card->new_frame_field = field; + card->new_frame_interlaced = interlaced; card->new_data_ready_fence = fence; card->dropped_frames = dropped_frames; card->new_data_ready_changed.notify_all(); @@ -461,6 +495,7 @@ void Mixer::thread_func() card_copy[card_index].new_frame = card->new_frame; card_copy[card_index].new_frame_length = card->new_frame_length; card_copy[card_index].new_frame_field = card->new_frame_field; + card_copy[card_index].new_frame_interlaced = card->new_frame_interlaced; card_copy[card_index].new_data_ready_fence = card->new_data_ready_fence; card_copy[card_index].dropped_frames = card->dropped_frames; card->new_data_ready = false; @@ -491,6 +526,7 @@ void Mixer::thread_func() } if (audio_level_callback != nullptr) { + unique_lock lock(r128_mutex); double loudness_s = r128.loudness_S(); double loudness_i = r128.integrated(); double loudness_range_low = r128.range_min(); @@ -525,7 +561,7 @@ void Mixer::thread_func() continue; assert(card->new_frame != nullptr); - bmusb_current_rendering_frame[card_index] = card->new_frame; + insert_new_frame(card->new_frame, card->new_frame_field, card->new_frame_interlaced, card_index, &input_state); check_error(); // The new texture might still be uploaded, @@ -536,19 +572,13 @@ void Mixer::thread_func() glDeleteSync(card->new_data_ready_fence); check_error(); } - const PBOFrameAllocator::Userdata *userdata = (const PBOFrameAllocator::Userdata *)card->new_frame->userdata; - theme->set_input_textures( - card_index, - userdata->tex_y[card->new_frame_field], - userdata->tex_cbcr[card->new_frame_field], - userdata->last_width[card->new_frame_field], - userdata->last_height[card->new_frame_field]); } // Get the main chain from the theme, and set its state immediately. - pair> theme_main_chain = theme->get_chain(0, pts(), WIDTH, HEIGHT); - EffectChain *chain = theme_main_chain.first; - theme_main_chain.second(); + Theme::Chain theme_main_chain = theme->get_chain(0, pts(), WIDTH, HEIGHT, input_state); + EffectChain *chain = theme_main_chain.chain; + theme_main_chain.setup_chain(); + //theme_main_chain.chain->enable_phase_timing(true); GLuint y_tex, cbcr_tex; bool got_frame = h264_encoder->begin_frame(&y_tex, &cbcr_tex); @@ -575,15 +605,8 @@ void Mixer::thread_func() RefCountedGLsync fence(GL_SYNC_GPU_COMMANDS_COMPLETE, /*flags=*/0); check_error(); - // Make sure the H.264 gets a reference to all the - // input frames needed, so that they are not released back - // until the rendering is done. - vector input_frames; - for (unsigned card_index = 0; card_index < num_cards; ++card_index) { - input_frames.push_back(bmusb_current_rendering_frame[card_index]); - } const int64_t av_delay = TIMEBASE / 10; // Corresponds to the fixed delay in resampling_queue.h. TODO: Make less hard-coded. - h264_encoder->end_frame(fence, pts_int + av_delay, input_frames); + h264_encoder->end_frame(fence, pts_int + av_delay, theme_main_chain.input_frames); ++frame; pts_int += card_copy[0].new_frame_length; @@ -602,15 +625,11 @@ void Mixer::thread_func() // Set up preview and any additional channels. for (int i = 1; i < theme->get_num_channels() + 2; ++i) { DisplayFrame display_frame; - pair> chain = theme->get_chain(i, pts(), WIDTH, HEIGHT); // FIXME: dimensions - display_frame.chain = chain.first; - display_frame.setup_chain = chain.second; + Theme::Chain chain = theme->get_chain(i, pts(), WIDTH, HEIGHT, input_state); // FIXME: dimensions + display_frame.chain = chain.chain; + display_frame.setup_chain = chain.setup_chain; display_frame.ready_fence = fence; - - // FIXME: possible to do better? - for (unsigned card_index = 0; card_index < num_cards; ++card_index) { - display_frame.input_frames.push_back(bmusb_current_rendering_frame[card_index]); - } + display_frame.input_frames = chain.input_frames; display_frame.temp_textures = {}; output_channel[i].output_frame(display_frame); } @@ -625,6 +644,15 @@ void Mixer::thread_func() // chain->print_phase_timing(); } + if (should_cut.exchange(false)) { // Test and clear. + string filename = generate_local_dump_filename(frame); + printf("Starting new recording: %s\n", filename.c_str()); + h264_encoder->shutdown(); + httpd.close_output_file(); + httpd.open_output_file(filename.c_str()); + h264_encoder.reset(new H264Encoder(h264_encoder_surface, WIDTH, HEIGHT, &httpd)); + } + #if 0 // Reset every 100 frames, so that local variations in frame times // (especially for the first few frames, when the shaders are @@ -750,7 +778,10 @@ void Mixer::process_audio_one_frame(int64_t frame_pts_int, int num_samples) vector left, right; deinterleave_samples(samples_out, &left, &right); float *ptrs[] = { left.data(), right.data() }; - r128.process(left.size(), ptrs); + { + unique_lock lock(r128_mutex); + r128.process(left.size(), ptrs); + } // Send the samples to the sound card. if (alsa) {