]> git.sesse.net Git - nageru/blobdiff - mixer.cpp
Fix an endianness issue.
[nageru] / mixer.cpp
index 4e3449b3dfdce62352299e7c7384a1d77b8bc7a4..05b7c51dcb78110f5bb94dcf910dbd3feeecef81 100644 (file)
--- a/mixer.cpp
+++ b/mixer.cpp
@@ -3,6 +3,7 @@
 #include "mixer.h"
 
 #include <assert.h>
+#include <endian.h>
 #include <epoxy/egl.h>
 #include <movit/effect_chain.h>
 #include <movit/effect_util.h>
 #include <sys/resource.h>
 
 #include "bmusb/bmusb.h"
+#include "bmusb/fake_capture.h"
 #include "context.h"
 #include "decklink_capture.h"
 #include "defs.h"
-#include "fake_capture.h"
 #include "flags.h"
 #include "video_encoder.h"
 #include "pbo_frame_allocator.h"
@@ -46,6 +47,7 @@ class QOpenGLContext;
 using namespace movit;
 using namespace std;
 using namespace std::placeholders;
+using namespace bmusb;
 
 Mixer *global_mixer = nullptr;
 bool uses_mlock = false;
@@ -61,7 +63,7 @@ void convert_fixed24_to_fp32(float *dst, size_t out_channels, const uint8_t *src
                        uint32_t s2 = *src++;
                        uint32_t s3 = *src++;
                        uint32_t s = s1 | (s1 << 8) | (s2 << 16) | (s3 << 24);
-                       dst[i * out_channels + j] = int(s) * (1.0f / 4294967296.0f);
+                       dst[i * out_channels + j] = int(s) * (1.0f / 2147483648.0f);
                }
                src += 3 * (in_channels - out_channels);
        }
@@ -72,9 +74,8 @@ void convert_fixed32_to_fp32(float *dst, size_t out_channels, const uint8_t *src
        assert(in_channels >= out_channels);
        for (size_t i = 0; i < num_samples; ++i) {
                for (size_t j = 0; j < out_channels; ++j) {
-                       // Note: Assumes little-endian.
-                       int32_t s = *(int32_t *)src;
-                       dst[i * out_channels + j] = s * (1.0f / 4294967296.0f);
+                       int32_t s = le32toh(*(int32_t *)src);
+                       dst[i * out_channels + j] = s * (1.0f / 2147483648.0f);
                        src += 4;
                }
                src += 4 * (in_channels - out_channels);
@@ -143,7 +144,7 @@ Mixer::Mixer(const QSurfaceFormat &format, unsigned num_cards)
        movit_texel_subpixel_precision /= 2.0;
 
        resource_pool.reset(new ResourcePool);
-       theme.reset(new Theme(global_flags.theme_filename.c_str(), resource_pool.get(), num_cards));
+       theme.reset(new Theme(global_flags.theme_filename, global_flags.theme_dirs, resource_pool.get(), num_cards));
        for (unsigned i = 0; i < NUM_OUTPUTS; ++i) {
                output_channel[i].parent = this;
                output_channel[i].channel = i;
@@ -167,24 +168,12 @@ Mixer::Mixer(const QSurfaceFormat &format, unsigned num_cards)
        // Start listening for clients only once VideoEncoder has written its header, if any.
        httpd.start(9095);
 
-       // First try initializing the fake devices, then PCI devices, then USB,
-       // until we have the desired number of cards.
-       unsigned num_pci_devices = 0, num_usb_devices = 0;
+       // First try initializing the then PCI devices, then USB, then
+       // fill up with fake cards until we have the desired number of cards.
+       unsigned num_pci_devices = 0;
        unsigned card_index = 0;
 
-       assert(global_flags.num_fake_cards >= 0);  // Enforced in flags.cpp.
-       unsigned num_fake_cards = global_flags.num_fake_cards;
-
-       assert(num_fake_cards <= num_cards);  // Enforced in flags.cpp.
-       for ( ; card_index < num_fake_cards; ++card_index) {
-               configure_card(card_index, new FakeCapture(card_index), /*is_fake_capture=*/true);
-       }
-
-       if (global_flags.num_fake_cards > 0) {
-               fprintf(stderr, "Initialized %d fake cards.\n", global_flags.num_fake_cards);
-       }
-
-       if (card_index < num_cards) {
+       {
                IDeckLinkIterator *decklink_iterator = CreateDeckLinkIteratorInstance();
                if (decklink_iterator != nullptr) {
                        for ( ; card_index < num_cards; ++card_index) {
@@ -193,28 +182,36 @@ Mixer::Mixer(const QSurfaceFormat &format, unsigned num_cards)
                                        break;
                                }
 
-                               configure_card(card_index, new DeckLinkCapture(decklink, card_index - num_fake_cards), /*is_fake_capture=*/false);
+                               configure_card(card_index, new DeckLinkCapture(decklink, card_index), /*is_fake_capture=*/false);
                                ++num_pci_devices;
                        }
                        decklink_iterator->Release();
-                       fprintf(stderr, "Found %d DeckLink PCI card(s).\n", num_pci_devices);
+                       fprintf(stderr, "Found %u DeckLink PCI card(s).\n", num_pci_devices);
                } else {
                        fprintf(stderr, "DeckLink drivers not found. Probing for USB cards only.\n");
                }
        }
-       for ( ; card_index < num_cards; ++card_index) {
-               BMUSBCapture *capture = new BMUSBCapture(card_index - num_pci_devices - num_fake_cards);
+       unsigned num_usb_devices = BMUSBCapture::num_cards();
+       for (unsigned usb_card_index = 0; usb_card_index < num_usb_devices && card_index < num_cards; ++usb_card_index, ++card_index) {
+               BMUSBCapture *capture = new BMUSBCapture(usb_card_index);
                capture->set_card_disconnected_callback(bind(&Mixer::bm_hotplug_remove, this, card_index));
                configure_card(card_index, capture, /*is_fake_capture=*/false);
-               ++num_usb_devices;
        }
+       fprintf(stderr, "Found %u USB card(s).\n", num_usb_devices);
 
-       if (num_usb_devices > 0) {
-               has_bmusb_thread = true;
-               BMUSBCapture::set_card_connected_callback(bind(&Mixer::bm_hotplug_add, this, _1));
-               BMUSBCapture::start_bm_thread();
+       unsigned num_fake_cards = 0;
+       for ( ; card_index < num_cards; ++card_index, ++num_fake_cards) {
+               FakeCapture *capture = new FakeCapture(WIDTH, HEIGHT, FAKE_FPS, OUTPUT_FREQUENCY, card_index, global_flags.fake_cards_audio);
+               configure_card(card_index, capture, /*is_fake_capture=*/true);
        }
 
+       if (num_fake_cards > 0) {
+               fprintf(stderr, "Initialized %u fake cards.\n", num_fake_cards);
+       }
+
+       BMUSBCapture::set_card_connected_callback(bind(&Mixer::bm_hotplug_add, this, _1));
+       BMUSBCapture::start_bm_thread();
+
        for (card_index = 0; card_index < num_cards; ++card_index) {
                cards[card_index].queue_length_policy.reset(card_index);
                cards[card_index].capture->start_bm_capture();
@@ -268,14 +265,12 @@ Mixer::Mixer(const QSurfaceFormat &format, unsigned num_cards)
 
        locut.init(FILTER_HPF, 2);
 
-       // If --flat-audio is given, turn off everything that messes with the sound,
-       // except the final makeup gain.
-       if (global_flags.flat_audio) {
-               set_locut_enabled(false);
-               set_gain_staging_auto(false);
-               set_limiter_enabled(false);
-               set_compressor_enabled(false);
-       }
+       set_locut_enabled(global_flags.locut_enabled);
+       set_gain_staging_db(global_flags.initial_gain_staging_db);
+       set_gain_staging_auto(global_flags.gain_staging_auto);
+       set_compressor_enabled(global_flags.compressor_enabled);
+       set_limiter_enabled(global_flags.limiter_enabled);
+       set_final_makeup_gain_auto(global_flags.final_makeup_gain_auto);
 
        // hlen=16 is pretty low quality, but we use quite a bit of CPU otherwise,
        // and there's a limit to how important the peak meter is.
@@ -290,9 +285,7 @@ Mixer::~Mixer()
 {
        resource_pool->release_glsl_program(cbcr_program_num);
        glDeleteBuffers(1, &cbcr_vbo);
-       if (has_bmusb_thread) {
-               BMUSBCapture::stop_bm_thread();
-       }
+       BMUSBCapture::stop_bm_thread();
 
        for (unsigned card_index = 0; card_index < num_cards; ++card_index) {
                {
@@ -716,34 +709,20 @@ void Mixer::thread_func()
                double elapsed = now.tv_sec - start.tv_sec +
                        1e-9 * (now.tv_nsec - start.tv_nsec);
                if (frame % 100 == 0) {
-               // check our memory usage, to see if we are close to our mlockall()
-               // limit (if at all set).
-               rusage used;
-               if (getrusage(RUSAGE_SELF, &used) == -1) {
-                       perror("getrusage(RUSAGE_SELF)");
-                       assert(false);
-               }
-
-               rlimit limit;
-               if (getrlimit(RLIMIT_MEMLOCK, &limit) == -1) {
-                       perror("getrlimit(RLIMIT_MEMLOCK)");
-                       assert(false);
-               }
-
                        printf("%d frames (%d dropped) in %.3f seconds = %.1f fps (%.1f ms/frame)",
                                frame, stats_dropped_frames, elapsed, frame / elapsed,
                                1e3 * elapsed / frame);
                //      chain->print_phase_timing();
 
-                       if (uses_mlock) {
-                               // Check our memory usage, to see if we are close to our mlockall()
-                               // limit (if at all set).
-                               rusage used;
-                               if (getrusage(RUSAGE_SELF, &used) == -1) {
-                                       perror("getrusage(RUSAGE_SELF)");
-                                       assert(false);
-                               }
+                       // Check our memory usage, to see if we are close to our mlockall()
+                       // limit (if at all set).
+                       rusage used;
+                       if (getrusage(RUSAGE_SELF, &used) == -1) {
+                               perror("getrusage(RUSAGE_SELF)");
+                               assert(false);
+                       }
 
+                       if (uses_mlock) {
                                rlimit limit;
                                if (getrlimit(RLIMIT_MEMLOCK, &limit) == -1) {
                                        perror("getrlimit(RLIMIT_MEMLOCK)");
@@ -754,6 +733,9 @@ void Mixer::thread_func()
                                        long(used.ru_maxrss / 1024),
                                        long(limit.rlim_cur / 1048576),
                                        float(100.0 * (used.ru_maxrss * 1024.0) / limit.rlim_cur));
+                       } else {
+                               printf(", using %ld MB memory (not locked)",
+                                       long(used.ru_maxrss / 1024));
                        }
 
                        printf("\n");
@@ -836,7 +818,8 @@ void Mixer::handle_hotplugged_cards()
                CaptureCard *card = &cards[card_index];
                if (card->capture->get_disconnected()) {
                        fprintf(stderr, "Card %u went away, replacing with a fake card.\n", card_index);
-                       configure_card(card_index, new FakeCapture(card_index), /*is_fake_capture=*/true);
+                       FakeCapture *capture = new FakeCapture(WIDTH, HEIGHT, FAKE_FPS, OUTPUT_FREQUENCY, card_index, global_flags.fake_cards_audio);
+                       configure_card(card_index, capture, /*is_fake_capture=*/true);
                        card->queue_length_policy.reset(card_index);
                        card->capture->start_bm_capture();
                }
@@ -1077,21 +1060,6 @@ void Mixer::process_audio_one_frame(int64_t frame_pts_int, int num_samples)
 
 //     printf("limiter=%+5.1f  compressor=%+5.1f\n", 20.0*log10(limiter_att), 20.0*log10(compressor_att));
 
-       // Upsample 4x to find interpolated peak.
-       peak_resampler.inp_data = samples_out.data();
-       peak_resampler.inp_count = samples_out.size() / 2;
-
-       vector<float> interpolated_samples_out;
-       interpolated_samples_out.resize(samples_out.size());
-       while (peak_resampler.inp_count > 0) {  // About four iterations.
-               peak_resampler.out_data = &interpolated_samples_out[0];
-               peak_resampler.out_count = interpolated_samples_out.size() / 2;
-               peak_resampler.process();
-               size_t out_stereo_samples = interpolated_samples_out.size() / 2 - peak_resampler.out_count;
-               peak = max<float>(peak, find_peak(interpolated_samples_out.data(), out_stereo_samples * 2));
-               peak_resampler.out_data = nullptr;
-       }
-
        // At this point, we are most likely close to +0 LU, but all of our
        // measurements have been on raw sample values, not R128 values.
        // So we have a final makeup gain to get us to +0 LU; the gain
@@ -1132,6 +1100,21 @@ void Mixer::process_audio_one_frame(int64_t frame_pts_int, int num_samples)
                final_makeup_gain = m;
        }
 
+       // Upsample 4x to find interpolated peak.
+       peak_resampler.inp_data = samples_out.data();
+       peak_resampler.inp_count = samples_out.size() / 2;
+
+       vector<float> interpolated_samples_out;
+       interpolated_samples_out.resize(samples_out.size());
+       while (peak_resampler.inp_count > 0) {  // About four iterations.
+               peak_resampler.out_data = &interpolated_samples_out[0];
+               peak_resampler.out_count = interpolated_samples_out.size() / 2;
+               peak_resampler.process();
+               size_t out_stereo_samples = interpolated_samples_out.size() / 2 - peak_resampler.out_count;
+               peak = max<float>(peak, find_peak(interpolated_samples_out.data(), out_stereo_samples * 2));
+               peak_resampler.out_data = nullptr;
+       }
+
        // Find R128 levels and L/R correlation.
        vector<float> left, right;
        deinterleave_samples(samples_out, &left, &right);