From: Steinar H. Gunderson Date: Fri, 6 Nov 2015 23:54:18 +0000 (+0100) Subject: Finally add defines for sample rate and fps (although they should really be non-compi... X-Git-Tag: 1.0.0~159 X-Git-Url: https://git.sesse.net/?p=nageru;a=commitdiff_plain;h=0f079f3c88e85d49208836642a579b4fd520fb76 Finally add defines for sample rate and fps (although they should really be non-compiled in). --- diff --git a/defs.h b/defs.h new file mode 100644 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) diff --git a/h264encode.cpp b/h264encode.cpp index ad688fc..dd2c805 100644 --- a/h264encode.cpp +++ b/h264encode.cpp @@ -29,6 +29,7 @@ #include #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; diff --git a/httpd.cpp b/httpd.cpp index db3cb61..359bd72 100644 --- 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; diff --git a/mixer.cpp b/mixer.cpp index 6f9bf05..d903592 100644 --- 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 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 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 samples_card; vector 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 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); } }