]> git.sesse.net Git - nageru/commitdiff
Finally add defines for sample rate and fps (although they should really be non-compi...
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Fri, 6 Nov 2015 23:54:18 +0000 (00:54 +0100)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Fri, 6 Nov 2015 23:54:18 +0000 (00:54 +0100)
defs.h [new file with mode: 0644]
h264encode.cpp
httpd.cpp
mixer.cpp

diff --git a/defs.h b/defs.h
new file mode 100644 (file)
index 0000000..fe27804
--- /dev/null
+++ b/defs.h
@@ -0,0 +1,8 @@
+#ifndef _DEFS_H
+#define _DEFS_H
+
+// OUTPUT_FREQUENCY/FPS must be an integer for now.
+#define OUTPUT_FREQUENCY 48000
+#define FPS 60
+
+#endif  // !defined(_DEFS_H)
index ad688fc58ee87b59e5232b94ad75bc4c24b36ad1..dd2c80560840c52155dca439c1a8c1335145731f 100644 (file)
@@ -29,6 +29,7 @@
 #include <utility>
 
 #include "context.h"
+#include "defs.h"
 #include "httpd.h"
 #include "timebase.h"
 
@@ -113,7 +114,7 @@ static  int frame_width = 176;
 static  int frame_height = 144;
 static  int frame_width_mbaligned;
 static  int frame_height_mbaligned;
-static  int frame_rate = 60;
+static  int frame_rate = FPS;
 static  unsigned int frame_bitrate = 0;
 static  unsigned int frame_slices = 1;
 static  double frame_size = 0;
@@ -121,7 +122,7 @@ static  int initial_qp = 15;
 //static  int initial_qp = 28;
 static  int minimal_qp = 0;
 static  int intra_period = 30;
-static  int intra_idr_period = 60;
+static  int intra_idr_period = FPS;
 static  int ip_period = 3;
 static  int rc_mode = -1;
 static  int rc_default_modes[] = {
@@ -1835,7 +1836,7 @@ H264Encoder::H264Encoder(QSurface *surface, int width, int height, HTTPD *httpd)
        AVCodec *codec_audio = avcodec_find_encoder(AV_CODEC_ID_MP3);
        context_audio = avcodec_alloc_context3(codec_audio);
        context_audio->bit_rate = 256000;
-       context_audio->sample_rate = 48000;
+       context_audio->sample_rate = OUTPUT_FREQUENCY;
        context_audio->sample_fmt = AV_SAMPLE_FMT_FLTP;
        context_audio->channels = 2;
        context_audio->channel_layout = AV_CH_LAYOUT_STEREO;
index db3cb61eafa4fdf554ca9de966e625ce6bd7d429..359bd7285d98c9cf50586e82ee73156551d9f134 100644 (file)
--- a/httpd.cpp
+++ b/httpd.cpp
@@ -16,6 +16,7 @@ extern "C" {
 
 #include "httpd.h"
 
+#include "defs.h"
 #include "timebase.h"
 
 struct MHD_Connection;
@@ -120,7 +121,7 @@ HTTPD::Mux::Mux(AVFormatContext *avctx, int width, int height)
        }
        avstream_audio->time_base = AVRational{1, TIMEBASE};
        avstream_audio->codec->bit_rate = 256000;
-       avstream_audio->codec->sample_rate = 48000;
+       avstream_audio->codec->sample_rate = OUTPUT_FREQUENCY;
        avstream_audio->codec->sample_fmt = AV_SAMPLE_FMT_FLTP;
        avstream_audio->codec->channels = 2;
        avstream_audio->codec->channel_layout = AV_CH_LAYOUT_STEREO;
index 6f9bf05d725d73aebc47639fdc0d26cad55ca608..d90359265b9cec2b722cbc5b141d9ffb1f943e8c 100644 (file)
--- a/mixer.cpp
+++ b/mixer.cpp
@@ -33,6 +33,7 @@
 
 #include "bmusb/bmusb.h"
 #include "context.h"
+#include "defs.h"
 #include "h264encode.h"
 #include "pbo_frame_allocator.h"
 #include "ref_counted_gl_sync.h"
@@ -69,7 +70,7 @@ Mixer::Mixer(const QSurfaceFormat &format, unsigned num_cards)
          num_cards(num_cards),
          mixer_surface(create_surface(format)),
          h264_encoder_surface(create_surface(format)),
-         level_compressor(48000.0f)
+         level_compressor(OUTPUT_FREQUENCY)
 {
        httpd.start(9095);
 
@@ -121,7 +122,7 @@ Mixer::Mixer(const QSurfaceFormat &format, unsigned num_cards)
                        [this]{
                                resource_pool->clean_context();
                        });
-               card->resampler.reset(new Resampler(48000.0, 48000.0, 2));
+               card->resampler.reset(new Resampler(OUTPUT_FREQUENCY, OUTPUT_FREQUENCY, 2));
                card->usb->configure_card();
        }
 
@@ -146,7 +147,7 @@ Mixer::Mixer(const QSurfaceFormat &format, unsigned num_cards)
                "} \n";
        cbcr_program_num = resource_pool->compile_glsl_program(cbcr_vert_shader, cbcr_frag_shader);
 
-       r128.init(2, 48000);
+       r128.init(2, OUTPUT_FREQUENCY);
        r128.integr_start();
 }
 
@@ -241,21 +242,21 @@ void Mixer::bm_frame(unsigned card_index, uint16_t timecode,
                unique_lock<mutex> lock(card->audio_mutex);
 
                int unwrapped_timecode = timecode;
-               if (dropped_frames > 60 * 2) {
+               if (dropped_frames > FPS * 2) {
                        fprintf(stderr, "Card %d lost more than two seconds (or time code jumping around), resetting resampler\n",
                                card_index);
-                       card->resampler.reset(new Resampler(48000.0, 48000.0, 2));
+                       card->resampler.reset(new Resampler(OUTPUT_FREQUENCY, OUTPUT_FREQUENCY, 2));
                } else if (dropped_frames > 0) {
                        // 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<float> silence;
-                       silence.resize((48000 / 60) * 2);
+                       silence.resize((OUTPUT_FREQUENCY / FPS) * 2);
                        for (int i = 0; i < dropped_frames; ++i) {
-                               card->resampler->add_input_samples((unwrapped_timecode - dropped_frames + i) / 60.0, silence.data(), (48000 / 60));
+                               card->resampler->add_input_samples((unwrapped_timecode - dropped_frames + i) / double(FPS), silence.data(), (OUTPUT_FREQUENCY / FPS));
                        }
                }
-               card->resampler->add_input_samples(unwrapped_timecode / 60.0, audio.data(), num_samples);
+               card->resampler->add_input_samples(unwrapped_timecode / double(FPS), audio.data(), num_samples);
        }
 
        // Done with the audio, so release it.
@@ -370,7 +371,7 @@ void Mixer::thread_func()
                        if (frame_num != card_copy[0].dropped_frames) {
                                // For dropped frames, increase the pts.
                                ++dropped_frames;
-                               pts_int += TIMEBASE / 60;
+                               pts_int += TIMEBASE / FPS;
                        }
                }
 
@@ -398,7 +399,7 @@ void Mixer::thread_func()
                // just increase the pts (skipping over this frame) and don't try to compute anything new.
                if (card_copy[0].new_frame->len == 0) {
                        ++dropped_frames;
-                       pts_int += TIMEBASE / 60;
+                       pts_int += TIMEBASE / FPS;
                        continue;
                }
 
@@ -463,7 +464,7 @@ void Mixer::thread_func()
                const int64_t av_delay = TIMEBASE / 10;  // Corresponds to the fixed delay in resampler.h. TODO: Make less hard-coded.
                h264_encoder->end_frame(fence, pts_int + av_delay, input_frames);
                ++frame;
-               pts_int += TIMEBASE / 60;
+               pts_int += TIMEBASE / FPS;
 
                // The live frame just shows the RGBA texture we just rendered.
                // It owns rgba_tex now.
@@ -524,10 +525,10 @@ void Mixer::process_audio_one_frame()
        vector<float> samples_card;
        vector<float> samples_out;
        for (unsigned card_index = 0; card_index < num_cards; ++card_index) {
-               samples_card.resize((48000 / 60) * 2);
+               samples_card.resize((OUTPUT_FREQUENCY / FPS) * 2);
                {
                        unique_lock<mutex> lock(cards[card_index].audio_mutex);
-                       if (!cards[card_index].resampler->get_output_samples(pts(), &samples_card[0], 48000 / 60)) {
+                       if (!cards[card_index].resampler->get_output_samples(pts(), &samples_card[0], OUTPUT_FREQUENCY / FPS)) {
                                printf("Card %d reported previous underrun.\n", card_index);
                        }
                }