More static code analysis fixes

This commit is contained in:
twinaphex 2015-09-28 16:57:07 +02:00
parent 6647eab237
commit d482dba2eb
10 changed files with 150 additions and 100 deletions

View File

@ -28,14 +28,18 @@
int main(int argc, char *argv[])
{
srand(time(NULL));
int16_t input_i[1024];
int16_t output_i[1024 * 8];
float input_f[1024];
float output_f[1024 * 8];
double in_rate, out_rate, ratio;
double ratio_max_deviation = 0.0;
const rarch_resampler_t *resampler = NULL;
void *re = NULL;
srand(time(NULL));
if (argc < 3 || argc > 4)
{
@ -48,18 +52,16 @@ int main(int argc, char *argv[])
fprintf(stderr, "Ratio deviation: %.4f.\n", ratio_max_deviation);
}
double in_rate = strtod(argv[1], NULL);
double out_rate = strtod(argv[2], NULL);
in_rate = strtod(argv[1], NULL);
out_rate = strtod(argv[2], NULL);
ratio = out_rate / in_rate;
double ratio = out_rate / in_rate;
if (ratio >= 7.99)
{
fprintf(stderr, "Ratio is too high.\n");
return 1;
}
const rarch_resampler_t *resampler = NULL;
void *re = NULL;
if (!rarch_resampler_realloc(&re, &resampler, RESAMPLER_IDENT, out_rate / in_rate))
{
fprintf(stderr, "Failed to allocate resampler ...\n");
@ -68,24 +70,26 @@ int main(int argc, char *argv[])
for (;;)
{
size_t output_samples;
struct resampler_data data;
double uniform, rate_mod;
if (fread(input_i, sizeof(int16_t), 1024, stdin) != 1024)
break;
double uniform = (2.0 * rand()) / RAND_MAX - 1.0;
double rate_mod = 1.0 + ratio_max_deviation * uniform;
uniform = (2.0 * rand()) / RAND_MAX - 1.0;
rate_mod = 1.0 + ratio_max_deviation * uniform;
audio_convert_s16_to_float(input_f, input_i, 1024, 1.0f);
struct resampler_data data = {
.data_in = input_f,
.data_out = output_f,
.input_frames = sizeof(input_f) / (2 * sizeof(float)),
.ratio = ratio * rate_mod,
};
data.data_in = input_f;
data.data_out = output_f;
data.input_frames = sizeof(input_f) / (2 * sizeof(float));
data.ratio = ratio * rate_mod;
rarch_resampler_process(resampler, re, &data);
size_t output_samples = data.output_frames * 2;
output_samples = data.output_frames * 2;
audio_convert_float_to_s16(output_i, output_f, output_samples);

View File

@ -33,7 +33,9 @@
static void gen_signal(float *out, double omega, double bias_samples, size_t samples)
{
for (size_t i = 0; i < samples; i += 2)
size_t i;
for (i = 0; i < samples; i += 2)
{
out[i + 0] = cos(((i >> 1) + bias_samples) * omega);
out[i + 1] = out[i + 0];
@ -60,21 +62,26 @@ static unsigned bitrange(unsigned len)
static unsigned bitswap(unsigned i, unsigned range)
{
unsigned shifts;
unsigned ret = 0;
for (unsigned shifts = 0; shifts < range; shifts++)
for (shifts = 0; shifts < range; shifts++)
ret |= i & (1 << (range - shifts - 1)) ? (1 << shifts) : 0;
return ret;
}
// When interleaving the butterfly buffer, addressing puts bits in reverse.
// [0, 1, 2, 3, 4, 5, 6, 7] => [0, 4, 2, 6, 1, 5, 3, 7]
/* When interleaving the butterfly buffer, addressing puts bits in reverse.
* [0, 1, 2, 3, 4, 5, 6, 7] => [0, 4, 2, 6, 1, 5, 3, 7] */
static void interleave(complex double *butterfly_buf, size_t samples)
{
unsigned i;
unsigned range = bitrange(samples);
for (unsigned i = 0; i < samples; i++)
for (i = 0; i < samples; i++)
{
unsigned target = bitswap(i, range);
if (target > i)
{
complex double tmp = butterfly_buf[target];
@ -91,58 +98,68 @@ static complex double gen_phase(double index)
static void butterfly(complex double *a, complex double *b, complex double mod)
{
complex double a_;
complex double b_;
mod *= *b;
complex double a_ = *a + mod;
complex double b_ = *a - mod;
*a = a_;
*b = b_;
a_ = *a + mod;
b_ = *a - mod;
*a = a_;
*b = b_;
}
static void butterflies(complex double *butterfly_buf, double phase_dir, size_t step_size, size_t samples)
{
for (unsigned i = 0; i < samples; i += 2 * step_size)
for (unsigned j = i; j < i + step_size; j++)
unsigned i, j;
for (i = 0; i < samples; i += 2 * step_size)
for (j = i; j < i + step_size; j++)
butterfly(&butterfly_buf[j], &butterfly_buf[j + step_size], gen_phase((phase_dir * (j - i)) / step_size));
}
static void calculate_fft(const float *data, complex double *butterfly_buf, size_t samples)
{
// Enforce POT.
unsigned i;
/* Enforce POT. */
assert((samples & (samples - 1)) == 0);
for (unsigned i = 0; i < samples; i++)
for (i = 0; i < samples; i++)
butterfly_buf[i] = data[2 * i];
// Interleave buffer to work with FFT.
/* Interleave buffer to work with FFT. */
interleave(butterfly_buf, samples);
// Fly, lovely butterflies! :D
/* Fly, lovely butterflies! :D */
for (unsigned step_size = 1; step_size < samples; step_size *= 2)
butterflies(butterfly_buf, -1.0, step_size, samples);
}
static void calculate_fft_adjust(complex double *butterfly_buf, double gain, bool merge_high, size_t samples)
{
unsigned i;
if (merge_high)
{
for (unsigned i = 1; i < samples / 2; i++)
for (i = 1; i < samples / 2; i++)
butterfly_buf[i] *= 2.0;
}
// Normalize amplitudes.
for (unsigned i = 0; i < samples; i++)
/* Normalize amplitudes. */
for (i = 0; i < samples; i++)
butterfly_buf[i] *= gain;
}
static void calculate_ifft(complex double *butterfly_buf, size_t samples, bool normalize)
{
// Enforce POT.
unsigned step_size;
/* Enforce POT. */
assert((samples & (samples - 1)) == 0);
interleave(butterfly_buf, samples);
// Fly, lovely butterflies! In opposite direction! :D
for (unsigned step_size = 1; step_size < samples; step_size *= 2)
/* Fly, lovely butterflies! In opposite direction! :D */
for (step_size = 1; step_size < samples; step_size *= 2)
butterflies(butterfly_buf, 1.0, step_size, samples);
if (normalize)
@ -151,25 +168,25 @@ static void calculate_ifft(complex double *butterfly_buf, size_t samples, bool n
static void test_fft(void)
{
fprintf(stderr, "Sanity checking FFT ...\n");
unsigned i, j;
float signal[32];
complex double butterfly_buf[16];
complex double buf_tmp[16];
const float cos_freqs[] = {
1.0, 4.0, 6.0,
};
const float sin_freqs[] = {
-2.0, 5.0, 7.0,
};
for (unsigned i = 0; i < 16; i++)
fprintf(stderr, "Sanity checking FFT ...\n");
for (i = 0; i < 16; i++)
{
signal[2 * i] = 0.0;
for (unsigned j = 0; j < sizeof(cos_freqs) / sizeof(cos_freqs[0]); j++)
for (j = 0; j < sizeof(cos_freqs) / sizeof(cos_freqs[0]); j++)
signal[2 * i] += cos(2.0 * M_PI * i * cos_freqs[j] / 16.0);
for (unsigned j = 0; j < sizeof(sin_freqs) / sizeof(sin_freqs[0]); j++)
for ( j = 0; j < sizeof(sin_freqs) / sizeof(sin_freqs[0]); j++)
signal[2 * i] += sin(2.0 * M_PI * i * sin_freqs[j] / 16.0);
}
@ -178,26 +195,27 @@ static void test_fft(void)
calculate_fft_adjust(buf_tmp, 1.0 / 16, true, 16);
fprintf(stderr, "FFT: { ");
for (unsigned i = 0; i < 7; i++)
for (i = 0; i < 7; i++)
fprintf(stderr, "(%4.2lf, %4.2lf), ", creal(buf_tmp[i]), cimag(buf_tmp[i]));
fprintf(stderr, "(%4.2lf, %4.2lf) }\n", creal(buf_tmp[7]), cimag(buf_tmp[7]));
calculate_ifft(butterfly_buf, 16, true);
fprintf(stderr, "Original: { ");
for (unsigned i = 0; i < 15; i++)
for (i = 0; i < 15; i++)
fprintf(stderr, "%5.2f, ", signal[2 * i]);
fprintf(stderr, "%5.2f }\n", signal[2 * 15]);
fprintf(stderr, "FFT => IFFT: { ");
for (unsigned i = 0; i < 15; i++)
for ( i = 0; i < 15; i++)
fprintf(stderr, "%5.2lf, ", creal(butterfly_buf[i]));
fprintf(stderr, "%5.2lf }\n", creal(butterfly_buf[15]));
}
static void set_alias_power(struct snr_result *res, unsigned freq, double power)
{
for (unsigned i = 0; i < 3; i++)
unsigned i;
for (i = 0; i < 3; i++)
{
if (power >= res->alias_power[i])
{
@ -212,21 +230,24 @@ static void set_alias_power(struct snr_result *res, unsigned freq, double power)
static void calculate_snr(struct snr_result *res,
unsigned in_rate, unsigned max_rate,
const float *resamp, complex double *butterfly_buf, size_t samples)
const float *resamp, complex double *butterfly_buf,
size_t samples)
{
unsigned i;
double signal;
double noise = 0.0;
samples >>= 1;
calculate_fft(resamp, butterfly_buf, samples);
calculate_fft_adjust(butterfly_buf, 1.0 / samples, true, samples);
memset(res, 0, sizeof(*res));
double signal = cabs(butterfly_buf[in_rate] * butterfly_buf[in_rate]);
signal = cabs(butterfly_buf[in_rate] * butterfly_buf[in_rate]);
butterfly_buf[in_rate] = 0.0;
double noise = 0.0;
// Aliased frequencies above half the original sampling rate are not considered.
for (unsigned i = 0; i <= max_rate; i++)
/* Aliased frequencies above half the original sampling rate are not considered. */
for (i = 0; i <= max_rate; i++)
{
double power = cabs(butterfly_buf[i] * butterfly_buf[i]);
set_alias_power(res, i, power);
@ -236,25 +257,12 @@ static void calculate_snr(struct snr_result *res,
res->snr = 10.0 * log10(signal / noise);
res->gain = 10.0 * log10(signal);
for (unsigned i = 0; i < 3; i++)
for (i = 0; i < 3; i++)
res->alias_power[i] = 10.0 * log10(res->alias_power[i]);
}
int main(int argc, char *argv[])
{
if (argc != 2)
{
fprintf(stderr, "Usage: %s <ratio> (out-rate is fixed for FFT).\n", argv[0]);
return 1;
}
double ratio = strtod(argv[1], NULL);
const unsigned fft_samples = 1024 * 128;
unsigned out_rate = fft_samples / 2;
unsigned in_rate = round(out_rate / ratio);
ratio = (double)out_rate / in_rate;
static const float freq_list[] = {
0.001, 0.002, 0.003, 0.004, 0.005, 0.006, 0.007, 0.008, 0.009,
0.010, 0.015, 0.020, 0.025, 0.030, 0.035, 0.040, 0.045, 0.050,
@ -265,38 +273,63 @@ int main(int argc, char *argv[])
0.495, 0.496, 0.497, 0.498, 0.499,
};
unsigned samples = in_rate * 4;
float *input = calloc(sizeof(float), samples);
float *output = calloc(sizeof(float), (fft_samples + 16) * 2);
complex double *butterfly_buf = calloc(sizeof(complex double), fft_samples / 2);
unsigned out_rate, in_rate, samples, i;
double ratio;
float *input, *output;
complex double *butterfly_buf;
const rarch_resampler_t *resampler = NULL;
const unsigned fft_samples = 1024 * 128;
void *re = NULL;
if (argc != 2)
{
fprintf(stderr, "Usage: %s <ratio> (out-rate is fixed for FFT).\n", argv[0]);
return 1;
}
ratio = strtod(argv[1], NULL);
out_rate = fft_samples / 2;
in_rate = round(out_rate / ratio);
ratio = (double)out_rate / in_rate;
samples = in_rate * 4;
input = calloc(sizeof(float), samples);
output = calloc(sizeof(float), (fft_samples + 16) * 2);
butterfly_buf = calloc(sizeof(complex double), fft_samples / 2);
assert(input);
assert(output);
void *re = NULL;
const rarch_resampler_t *resampler = NULL;
if (!rarch_resampler_realloc(&re, &resampler, RESAMPLER_IDENT, ratio))
{
free(input);
free(output);
free(butterfly_buf);
return 1;
}
test_fft();
for (unsigned i = 0; i < sizeof(freq_list) / sizeof(freq_list[0]); i++)
for (i = 0; i < sizeof(freq_list) / sizeof(freq_list[0]); i++)
{
struct resampler_data data;
unsigned max_freq;
struct snr_result res = {0};
unsigned freq = freq_list[i] * in_rate;
double omega = 2.0 * M_PI * freq / in_rate;
double omega = 2.0 * M_PI * freq / in_rate;
gen_signal(input, omega, 0, samples);
struct resampler_data data = {
.data_in = input,
.data_out = output,
.input_frames = in_rate * 2,
.ratio = ratio,
};
data.data_in = input;
data.data_out = output;
data.input_frames = in_rate * 2;
data.ratio = ratio;
rarch_resampler_process(resampler, re, &data);
// We generate 2 seconds worth of audio, however, only the last second is considered so phase has stabilized.
struct snr_result res = {0};
unsigned max_freq = min(in_rate, out_rate) / 2;
/* We generate 2 seconds worth of audio, however,
* only the last second is considered so phase has stabilized. */
max_freq = min(in_rate, out_rate) / 2;
if (freq > max_freq)
continue;
@ -312,6 +345,7 @@ int main(int argc, char *argv[])
}
rarch_resampler_freep(&resampler, &re);
free(input);
free(output);
free(butterfly_buf);

View File

@ -473,18 +473,19 @@ database_info_list_t *database_info_list_new(
if (ret == 0)
{
database_info_t *db_ptr = NULL;
database_info = (database_info_t*)
database_info_t *db_ptr = NULL;
database_info_t *new_ptr = (database_info_t*)
realloc(database_info, (k+1) * sizeof(database_info_t));
if (!database_info)
if (!new_ptr)
{
database_info_list_free(database_info_list);
database_info_list = NULL;
goto end;
}
db_ptr = &database_info[k];
database_info = new_ptr;
db_ptr = &database_info[k];
if (!db_ptr)
continue;

2
deps/zlib/gzread.c vendored
View File

@ -249,7 +249,7 @@ local int gz_fetch(gz_statep state)
/* Skip len uncompressed bytes of output. Return -1 on error, 0 on success. */
local int gz_skip(gz_statep state, z_off64_t len)
{
unsigned n;
unsigned n = 0;
/* skip over len bytes or reach end-of-file, whichever comes first */
while (len)

2
deps/zlib/gzwrite.c vendored
View File

@ -132,7 +132,7 @@ local int gz_comp(gz_statep state, int flush)
local int gz_zero(gz_statep state, z_off64_t len)
{
int first;
unsigned n;
unsigned n = 0;
z_streamp strm = &(state->strm);
/* consume whatever's left in the input buffer */

View File

@ -653,7 +653,8 @@ static bool d3d_construct(d3d_video_t *d3d,
}
video_monitor_get_fps(buffer, sizeof(buffer), NULL, 0);
sprintf(buffer, "%s || Direct3D", buffer);
strlcat(buffer, " || Direct3D", sizeof(buffer));
d3d->hWnd = CreateWindowEx(0, "RetroArch", buffer,
info->fullscreen ?

View File

@ -465,6 +465,13 @@ void init_video(void)
if (av_info)
geom = (const struct retro_game_geometry*)&av_info->geometry;
if (!geom)
{
RARCH_ERR("AV geometry not initialized, cannot initialize video driver.\n");
rarch_fail(1, "init_video()");
}
max_dim = max(geom->max_width, geom->max_height);
scale = next_pow2(max_dim) / RARCH_SCALE_BASE;
scale = max(scale, 1);
@ -621,7 +628,7 @@ void video_driver_set_nonblock_state(bool toggle)
driver_t *driver = driver_get_ptr();
const video_driver_t *video = video_driver_ctx_get_ptr(driver);
if (video->set_nonblock_state)
if (video && video->set_nonblock_state)
video->set_nonblock_state(driver->video_data, toggle);
}
@ -631,7 +638,7 @@ bool video_driver_set_viewport(unsigned width, unsigned height,
driver_t *driver = driver_get_ptr();
const video_driver_t *video = video_driver_ctx_get_ptr(driver);
if (video->set_viewport)
if (video && video->set_viewport)
{
video->set_viewport(driver->video_data, width, height,
force_fullscreen, allow_rotate);
@ -645,7 +652,7 @@ bool video_driver_set_rotation(unsigned rotation)
driver_t *driver = driver_get_ptr();
const video_driver_t *video = video_driver_ctx_get_ptr(driver);
if (video->set_rotation)
if (video && video->set_rotation)
{
video->set_rotation(driver->video_data, rotation);
return true;
@ -736,7 +743,7 @@ bool video_driver_viewport_info(struct video_viewport *vp)
driver_t *driver = driver_get_ptr();
const video_driver_t *video = video_driver_ctx_get_ptr(driver);
if (video->viewport_info)
if (video && video->viewport_info)
{
video->viewport_info(driver->video_data, vp);
return true;
@ -749,7 +756,7 @@ bool video_driver_read_viewport(uint8_t *buffer)
driver_t *driver = driver_get_ptr();
const video_driver_t *video = video_driver_ctx_get_ptr(driver);
if (video->read_viewport)
if (video && video->read_viewport)
return video->read_viewport(driver->video_data,
buffer);
return false;
@ -778,13 +785,13 @@ bool video_driver_overlay_interface(const video_overlay_interface_t **iface)
}
#endif
void * video_driver_read_frame_raw(unsigned *width,
void *video_driver_read_frame_raw(unsigned *width,
unsigned *height, size_t *pitch)
{
driver_t *driver = driver_get_ptr();
const video_driver_t *video = video_driver_ctx_get_ptr(driver);
if (video->read_frame_raw)
if (video && video->read_frame_raw)
return video->read_frame_raw(driver->video_data, width,
height, pitch);
return NULL;
@ -930,7 +937,7 @@ void video_monitor_adjust_system_rates(void)
if (av_info)
info = (const struct retro_system_timing*)&av_info->timing;
if (info->fps <= 0.0)
if (!info || info->fps <= 0.0)
return;
timing_skew = fabs(1.0f - info->fps / settings->video.refresh_rate);

View File

@ -53,6 +53,9 @@ void handle_xkb(
if (value)
num_syms = xkb_state_key_get_syms(xkb_state, xk_code, &syms);
if (!syms)
return;
xkb_state_update_key(xkb_state, xk_code, value ? XKB_KEY_DOWN : XKB_KEY_UP);
/* Build mod state. */

View File

@ -41,7 +41,7 @@ static int call_init(lua_State * L, int argc, const char ** argv)
static int value_provider(void * ctx, struct rmsgpack_dom_value *out)
{
int rv;
int rv = 0;
lua_State * L = ctx;
lua_getglobal(L, "get_value");

View File

@ -59,7 +59,7 @@ static void push_rmsgpack_value(lua_State *L, struct rmsgpack_dom_value *value)
static int value_provider(void *ctx, struct rmsgpack_dom_value *out)
{
int rv;
int rv = 0;
lua_State *L = ctx;
lua_getfield(L, LUA_REGISTRYINDEX, "testlib_get_value");