From 7ba7dc6aa24c837764df94ca14dd11a990dcc6aa Mon Sep 17 00:00:00 2001 From: drvee Date: Sun, 25 Feb 2007 01:41:44 +0000 Subject: [PATCH] 2007-02-25 Krzysztof Kosciuszkiewicz * V142 * pace.c (brandom): Use higher bits from rand(). (randomize): Uses wall time as RNG seed. Removed biostime(). * utils.c: New file, factored out from pace.c. * mmfile.c (mm_decode_audio): Support for many channels. (mm_convert_audio): New function, but SDL resampling is crap. * admin.c (FileAccess, EndOfTurnSave): Use randomize(). * Makefile: Added utils object file. --- ChangeLog | 17 ++++ Makefile.in | 7 +- admin.c | 5 +- fs.c | 1 + gx.c | 1 + intro.c | 1 + main.c | 1 + mis_c.c | 1 + mis_m.c | 1 + mmfile.c | 243 +++++++++++++++++++++++++++++++++------------------- mmfile.h | 5 ++ music.c | 1 + options.c | 1 + pace.c | 145 +------------------------------ pace.h | 31 ------- place.c | 1 + port.c | 1 + sdl.c | 1 + utils.c | 140 ++++++++++++++++++++++++++++++ utils.h | 18 ++++ vab.c | 1 + version.c | 2 +- vtest2.c | 62 ++++---------- 23 files changed, 375 insertions(+), 312 deletions(-) create mode 100644 utils.c create mode 100644 utils.h diff --git a/ChangeLog b/ChangeLog index 054e044..84545c1 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,20 @@ +2007-02-25 Krzysztof Kosciuszkiewicz + + * V142 + + * pace.c (brandom): Use higher bits from rand(). + (randomize): Uses wall time as RNG seed. + Removed biostime(). + + * utils.c: New file, factored out from pace.c. + + * mmfile.c (mm_decode_audio): Support for many channels. + (mm_convert_audio): New function, but SDL resampling is crap. + + * admin.c (FileAccess, EndOfTurnSave): Use randomize(). + + * Makefile: Added utils object file. + 2007-02-24 Pace Willisson * V141 diff --git a/Makefile.in b/Makefile.in index fa1927d..fbe6a9c 100644 --- a/Makefile.in +++ b/Makefile.in @@ -42,20 +42,21 @@ PROGS = $(addsuffix @EXEEXT@, \ EXTRA_SOURCES = AUTHORS README COPYING DEVELOPER TODO ChangeLog \ raceintospace.6 \ configure Makefile.in race.h.in \ - $(shell ls m4/*.m4) \ + $(wildcard m4/*.m4) \ $(shell find os_macosx -name CVS -prune -o -type f -print) \ os_win32/configure.mingw os_win32/Shuttle.ico \ os_win32/installer.nsi.in os_win32/Makefile.os BARIS_HFILES = av.h Buzz_inc.h data.h endianness.h externs.h gamedata.h \ int_types.h macros.h mis.h music.h pace.h proto.h records.h soundfx.h \ - soundint.h mmfile.h options.h fs.h + soundint.h mmfile.h options.h fs.h utils.h BARIS_OBJS = $(addsuffix .@OBJEXT@, \ admin aimast aimis aipur ast0 ast1 ast2 ast3 ast4 budget crew endgame \ endianness futbub future hardef intel intro main mc2 mc mis_c mis_m museum \ newmis news news_sup news_suq place port prefs prest radar rdplex records \ replay review rush start vab pace gx gr sdl music gamedata mmfile options fs \ + utils \ ) all: $(PROG_NAME) @@ -96,7 +97,7 @@ $(PROG_NAME): $(BARIS_OBJS) @echo "$(CC) ...flags... -c $<" @$(CC) $(CPPFLAGS) $(CFLAGS) $(EXTRA_WARNINGS) -c $< -vtest2@EXEEXT@: $(addsuffix @OBJEXT@, vtest2 mmfile) +vtest2@EXEEXT@: $(addsuffix .@OBJEXT@, vtest2 mmfile utils) $(CC) $(CFLAGS) -o $@ $^ @OGG_LIBS@ @VORBIS_LIBS@ @THEORA_LIBS@ @SDL_LIBS@ imgsplit@EXEEXT@: imgsplit.@OBJEXT@ diff --git a/admin.c b/admin.c index 95cb371..afed10a 100644 --- a/admin.c +++ b/admin.c @@ -28,6 +28,7 @@ #include "Buzz_inc.h" #include "externs.h" #include "av.h" +#include "utils.h" #define MODEM_ERROR 4 #define NOTSAME 2 @@ -348,7 +349,7 @@ void FileAccess(char mode) int moo = 0; srand(SaveHdr->compSize); for(moo=0;moocompSize;moo++) load_buffer[moo]^=random(256); - srand(biostime(0,0L)); + randomize(); } #endif RLED((char *) load_buffer,(char *)Data,SaveHdr->compSize); @@ -1381,7 +1382,7 @@ void EndOfTurnSave(char *inData, int dataLen) } // Reseed the random number generator // -- this may have been the source of created complaints about randomness - srand(biostime(0,0L)); + randomize(); } #endif diff --git a/fs.c b/fs.c index 134e205..79ab9ec 100644 --- a/fs.c +++ b/fs.c @@ -19,6 +19,7 @@ #include "fs.h" #include "options.h" #include "pace.h" +#include "utils.h" #include #include #include diff --git a/gx.c b/gx.c index 154d8a8..24a4a5a 100644 --- a/gx.c +++ b/gx.c @@ -3,6 +3,7 @@ #include #include "externs.h" #include "av.h" +#include "utils.h" #include extern unsigned char *screen; diff --git a/intro.c b/intro.c index 8fc1075..d0b8da0 100644 --- a/intro.c +++ b/intro.c @@ -25,6 +25,7 @@ #include "Buzz_inc.h" #include "externs.h" +#include "utils.h" GXHEADER local2; struct CREDIT { diff --git a/main.c b/main.c index 04ff194..ec0b7e2 100644 --- a/main.c +++ b/main.c @@ -28,6 +28,7 @@ #include "Buzz_inc.h" #include "options.h" +#include "utils.h" #ifdef CONFIG_MACOSX // SDL.h needs to be included here to replace the original main() with diff --git a/mis_c.c b/mis_c.c index 67cd6ea..35adcce 100644 --- a/mis_c.c +++ b/mis_c.c @@ -29,6 +29,7 @@ #include "mis.h" #include "av.h" #include "mmfile.h" +#include "utils.h" #define FRM_Delay 22 diff --git a/mis_m.c b/mis_m.c index 47a832b..b60fb12 100644 --- a/mis_m.c +++ b/mis_m.c @@ -9,6 +9,7 @@ #include "Buzz_inc.h" #include "externs.h" #include "mis.h" +#include "utils.h" #define MIS_SET 0 // FAILURES ON diff --git a/mmfile.c b/mmfile.c index a4b1bf9..6f397e0 100644 --- a/mmfile.c +++ b/mmfile.c @@ -19,7 +19,7 @@ #include "mmfile.h" #include "int_types.h" #include "macros.h" -#include "pace.h" +#include "utils.h" #include #include #include @@ -46,19 +46,19 @@ get_page(mm_file * mf, ogg_page * pg) { p = ogg_sync_buffer(&mf->sync, bufsize); if (!p) - { + { /* ERROR */ printf("ogg_sync_buffer\n"); - return -1; - } + return -1; + } if (0 == (n = fread(p, 1, bufsize, mf->file))) return (feof(mf->file)) ? 0 : -1; if (ogg_sync_wrote(&mf->sync, n)) - { - /* ERROR */printf("ogg_sync_wrote, buffer overflow\n"); - return -1; - } + { + /* ERROR */ printf("ogg_sync_wrote, buffer overflow\n"); + return -1; + } } /* XXX: following may segfault if non-ogg file is read */ if (res < 0 || ogg_page_version(pg) != 0) @@ -71,7 +71,7 @@ get_packet(mm_file * mf, ogg_packet * pkt, enum stream_type type) { ogg_stream_state *stream, *other; ogg_page pg; - enum stream_type other_type; + enum stream_type other_type; int rv = 0; assert(mf); @@ -82,55 +82,56 @@ get_packet(mm_file * mf, ogg_packet * pkt, enum stream_type type) assert(mf->video); stream = mf->video; other = mf->audio; - other_type = MEDIA_AUDIO; + other_type = MEDIA_AUDIO; break; case MEDIA_AUDIO: assert(mf->audio); stream = mf->audio; other = mf->video; - other_type = MEDIA_VIDEO; + other_type = MEDIA_VIDEO; break; default: - /*ERROR*/ - printf("bad stream type\n"); + /*ERROR*/ printf("bad stream type\n"); return -1; } - if (mf->end_of_stream & type) - return 0; + if (mf->end_of_stream & type) + return 0; while (0 == ogg_stream_packetout(stream, pkt)) { rv = get_page(mf, &pg); if (rv <= 0) return rv; if (ogg_stream_pagein(stream, &pg) < 0) - { - if (other && ogg_stream_pagein(other, &pg) == 0) - { - /* - * Got page from other stream. If user won't ever decode this - * then we need to clean up it here - otherwise read but not - * decoded packets would accumulate. - */ - if (mf->drop_packets & other_type) - { - ogg_packet packet; - while (ogg_stream_packetout(other, &packet)) - /* just drop packets */; - } - } - else - { - /* DEBUG */ printf("got page not associated with any stream, " - "serial %x\n", ogg_page_serialno(&pg)); - /* - * drop page. Ogg source code says ogg_page member pointers are - * initialized to static buffers, so there is no need to free - * anything. - */ - } - } + { + if (other && ogg_stream_pagein(other, &pg) == 0) + { + /* + * Got page from other stream. If user won't ever decode this + * then we need to clean up it here - otherwise read but not + * decoded packets would accumulate. + */ + if (mf->drop_packets & other_type) + { + ogg_packet packet; + + while (ogg_stream_packetout(other, &packet)) + /* just drop packets */ ; + } + } + else + { + /* DEBUG */ + printf("got page not associated with any stream, " + "serial %x\n", ogg_page_serialno(&pg)); + /* + * drop page. Ogg source code says ogg_page member pointers are + * initialized to static buffers, so there is no need to free + * anything. + */ + } + } } - mf->end_of_stream |= (!!pkt->e_o_s) * type; + mf->end_of_stream |= (!!pkt->e_o_s) * type; return 1; } @@ -283,8 +284,7 @@ init_vorbis(mm_file * mf, ogg_page * pg) } static int -yuv_to_overlay(const mm_file * mf, const yuv_buffer * yuv, - SDL_Overlay * ovl) +yuv_to_overlay(const mm_file * mf, const yuv_buffer * yuv, SDL_Overlay * ovl) { unsigned i, h, w, xoff, yoff; uint8_t *yp, *up, *vp; @@ -356,7 +356,7 @@ yuv_to_overlay(const mm_file * mf, const yuv_buffer * yuv, /* rval < 0: error, > 0: have audio or video */ int -mm_open_fp(mm_file * mf, FILE *file) +mm_open_fp(mm_file * mf, FILE * file) { int retval = -1; int res = 0; @@ -397,17 +397,18 @@ mm_open_fp(mm_file * mf, FILE *file) int mm_open(mm_file * mf, const char *fname) { - assert(mf); - assert(fname); - return mm_open_fp(mf, fopen(fname, "rb")); + assert(mf); + assert(fname); + return mm_open_fp(mf, fopen(fname, "rb")); } unsigned mm_ignore(mm_file * mf, unsigned mask) { - unsigned old = mf->drop_packets; - mf->drop_packets = mask; - return old; + unsigned old = mf->drop_packets; + + mf->drop_packets = mask; + return old; } int @@ -465,7 +466,8 @@ mm_close(mm_file * mf) /* rval < 0: no video in file */ int -mm_video_info(const mm_file * mf, unsigned *width, unsigned *height, float *fps) +mm_video_info(const mm_file * mf, unsigned *width, unsigned *height, + float *fps) { assert(mf); if (!mf->video) @@ -504,12 +506,12 @@ mm_decode_video(mm_file * mf, SDL_Overlay * ovl) assert(mf); if (!mf->video) return -1; - if (mf->drop_packets & MEDIA_VIDEO) - { - /* WARNING */ - printf("requested decode but MEDIA_VIDEO is set to ignore\n"); - return -1; - } + if (mf->drop_packets & MEDIA_VIDEO) + { + /* WARNING */ + printf("requested decode but MEDIA_VIDEO is set to ignore\n"); + return -1; + } for (;;) { rv = get_packet(mf, &pkt, MEDIA_VIDEO); @@ -535,47 +537,60 @@ mm_decode_video(mm_file * mf, SDL_Overlay * ovl) int mm_decode_audio(mm_file * mf, void *buf, int buflen) { - int rv = 0, samples = 0, left = buflen, total = 0; - ogg_packet pkt; - float **pcm; + const int max_val = UCHAR_MAX; + const int min_val = 0; + const int bytes_per_sample = 1; + + int rv = 0, samples = 0, left = 0, total = 0; + unsigned channels = 0; assert(mf); - if (!mf->audio) + if (-1 == mm_audio_info(mf, &channels, NULL)) return -1; - if (mf->drop_packets & MEDIA_AUDIO) - { - /* WARNING */ - printf("requested decode but MEDIA_AUDIO is set to ignore\n"); - return -1; - } - for (;;) + if (mf->drop_packets & MEDIA_AUDIO) { - /* output any samples left from last conversion */ - while ((samples = vorbis_synthesis_pcmout(mf->audio_ctx, &pcm)) > 0) + /* WARNING */ + printf("requested decode but MEDIA_AUDIO is set to ignore\n"); + return -1; + } + + /* convert buflen [bytes] to left [samples] */ + left = buflen; + left = left / channels / bytes_per_sample; + + while (left > 0) + { + float **pcm; + ogg_packet pkt; + + /* also outputs any samples left from last decoding */ + while (left > 0 + && (samples = vorbis_synthesis_pcmout(mf->audio_ctx, &pcm)) > 0) { int i = 0; - float *mono = pcm[0]; /* just left channel, or mono */ - - if (left <= 0) - return total; + unsigned ch = 0; samples = min(samples, left); - /* conv floats to uint8_t */ for (i = 0; i < samples; ++i) { - int val = roundf((mono[i] + 1.0) / 2.0 * UCHAR_MAX); - - if (val > UCHAR_MAX) - val = UCHAR_MAX; - if (val < 0) - val = 0; - *((uint8_t *) buf + total + i) = val; + for (ch = 0; ch < channels; ++ch) + { + /* XXX: lrint requires C99 */ + int val = lrint((pcm[ch][i] + 1.0) / 2.0 * max_val); + + if (val > max_val) + val = max_val; + if (val < min_val) + val = min_val; + *((uint8_t *) buf + (total + i) * channels + ch) = val; + } } total += samples; left -= samples; vorbis_synthesis_read(mf->audio_ctx, samples); + } /* grab new packets if we need more */ for (;;) @@ -584,7 +599,7 @@ mm_decode_audio(mm_file * mf, void *buf, int buflen) if (rv < 0) return rv; else if (rv == 0) - return total; + return total * channels * bytes_per_sample; /* have packet, synthesize */ if (vorbis_synthesis(mf->audio_blk, &pkt) == 0) @@ -595,11 +610,65 @@ mm_decode_audio(mm_file * mf, void *buf, int buflen) else { /* DEBUG */ - printf("packet does not a valid vorbis frame!\n"); + printf("packet does not a valid vorbis frame!\n"); /* get next packet */ } } } - /* NOT REACHED */ - return 0; + + return total * channels * bytes_per_sample; +} + +#if 0 +int +mm_convert_audio(mm_file * mf, void *buf, int buflen, SDL_AudioSpec * spec) +{ + SDL_AudioCVT cvt; + unsigned channels, rate; + uint8_t *tmp_buf = NULL; + int allocated = 0; + int to_decode = 0; + int decoded = 0; + + assert(mf); + assert(spec); + + if (-1 == mm_audio_info(mf, &channels, &rate)) + return -1; + + if (-1 == SDL_BuildAudioCVT(&cvt, + MM_AUDIO_FORMAT, channels, rate, + spec->format, spec->channels, spec->freq)) + return -1; + + /* Check if we need alloc memory or can dest buffer be used directly */ + to_decode = buflen / cvt.len_ratio; + if (to_decode > buflen) + { + allocated = 1; + tmp_buf = xmalloc(to_decode); + } + else + tmp_buf = buf; + + decoded = mm_decode_audio(mf, tmp_buf, to_decode); + if (decoded <= 0) + return decoded; + + cvt.buf = tmp_buf; + cvt.len = decoded; + + if (-1 == SDL_ConvertAudio(&cvt)) + return -1; + + if (allocated) + { + memcpy(buf, tmp_buf, cvt.len * cvt.len_ratio); + free(tmp_buf); + } + + return cvt.len * cvt.len_ratio; } +#endif + +/* vi: set noet ts=4 sw=4 tw=78: */ diff --git a/mmfile.h b/mmfile.h index 8acbc3d..1e937d0 100644 --- a/mmfile.h +++ b/mmfile.h @@ -7,6 +7,8 @@ #include #include +#define MM_AUDIO_FORMAT AUDIO_U16SYS + enum stream_type { MEDIA_AUDIO = 1, @@ -36,5 +38,8 @@ extern int mm_video_info(const mm_file * mf, unsigned *width, unsigned *height, extern int mm_audio_info(const mm_file * mf, unsigned *channels, unsigned *rate); extern int mm_decode_video(mm_file * mf, SDL_Overlay * ovl); extern int mm_decode_audio(mm_file * mf, void *buf, int buflen); +#if 0 +extern int mm_convert_audio(mm_file * mf, void *buf, int buflen, SDL_AudioSpec *spec); +#endif #endif /* _MM_FILE_H */ diff --git a/music.c b/music.c index d407a28..a8cf944 100644 --- a/music.c +++ b/music.c @@ -2,6 +2,7 @@ #include "Buzz_inc.h" #include "mmfile.h" #include "pace.h" +#include "utils.h" struct music_file { diff --git a/options.c b/options.c index d890d3e..db7b768 100644 --- a/options.c +++ b/options.c @@ -20,6 +20,7 @@ #include "macros.h" #include "pace.h" #include "fs.h" +#include "utils.h" #include #include #include diff --git a/pace.c b/pace.c index e1fbb3f..05d7cd3 100644 --- a/pace.c +++ b/pace.c @@ -4,6 +4,7 @@ #include "pace.h" #include "av.h" #include "options.h" +#include "utils.h" extern GXHEADER vhptr; @@ -38,7 +39,7 @@ char *letter_dat; void OpenEmUp(void) { - srand(clock()); + randomize(); seq_init (); @@ -49,14 +50,6 @@ OpenEmUp(void) letter_dat = slurp_gamedat ("letter.dat"); } -double -get_time (void) -{ - struct timeval tv; - gettimeofday (&tv, NULL); - return (tv.tv_sec + tv.tv_usec / 1e6); -} - int PCX_D (void *src_raw,void *dest_raw,unsigned src_size) { @@ -210,13 +203,12 @@ CDAccess (int drive,int track,char op) return (0); } - int brandom (int limit) { if (limit == 0) return (0); - return (rand () % limit); + return (int) (limit * (rand() / (RAND_MAX + 1.0))); } long RLEC (char *src, char *dest, unsigned int src_size) @@ -250,17 +242,6 @@ long RLEC (char *src, char *dest, unsigned int src_size) return (dest_i); } -#if defined(__linux__) || defined(CONFIG_MACOSX) -int -biostime (int a, long b) -{ - struct timeval tv; - - gettimeofday (&tv, NULL); - return (tv.tv_sec * 1000 * 1000 + tv.tv_usec); -} -#endif - void StopAudio(char mode) { @@ -369,7 +350,7 @@ LMove (void *p) void randomize (void) { - srand (clock ()); + srand (get_time() * 1000); } void @@ -525,85 +506,6 @@ play_audio (int sidx, int mode) PlayVoice (); } -void * -xmalloc (size_t n) -{ - void *p; - - if ((p = malloc (n)) == NULL) { - perror("malloc"); - exit(EXIT_FAILURE); - } - - return (p); -} - -void * -xcalloc (size_t a, size_t b) -{ - void *p; - - if ((p = calloc (a, b)) == NULL) { - perror("calloc"); - exit(EXIT_FAILURE); - } - - return (p); -} - -char * -xstrdup (const char *s) -{ - void *p; - - p = xmalloc (strlen (s) + 1); - strcpy (p, s); - return (p); -} - -void * -xrealloc(void *ptr, size_t size) -{ - void *p = realloc(ptr, size); - - if (!p) - { - perror("realloc"); - exit(EXIT_FAILURE); - } - - return (p); -} - -ssize_t -fread_dyn(char **destp, size_t *n, FILE *stream) -{ - const unsigned bsize = 8192; - size_t total = 0, cnt = 0; - - assert(destp); - assert(n); - assert(stream); - - if (!*destp) - *destp = xmalloc(*n = bsize); - - while (1) { - cnt = fread(*destp+total, 1, *n-total, stream); - if (cnt != *n-total) - { - if (feof(stream)) - return total+cnt; - else if (ferror(stream)) - return -1; - } - total += cnt; - - if (*n <= total) - *destp = xrealloc(*destp, *n *= 2); - } -} - #define debug_file stdout void vdbg (char const *fmt, va_list args) @@ -672,42 +574,3 @@ dbg (char const *fmt, ...) vdbg (fmt, args); va_end (args); } - -#ifdef _WIN32 -#include -int -gettimeofday (struct timeval *tv, struct timezone *tz) -{ - struct _timeb t; - - _ftime (&t); - tv->tv_sec = t.time; - tv->tv_usec = t.millitm * 1000; - return (0); -} -#endif - -int -xstrcasecmp (char const *a, char const *b) -{ - while (*a) { - if (tolower (*a & 0xff) != tolower (*b & 0xff)) - break; - a++; - b++; - } - return (*a - *b); -} - -int -xstrncasecmp (char const *a, char const *b, int n) -{ - while (n && *a) { - if (tolower (*a & 0xff) != tolower (*b & 0xff)) - break; - a++; - b++; - n--; - } - return (*a - *b); -} diff --git a/pace.h b/pace.h index 21b5037..97d8d6d 100644 --- a/pace.h +++ b/pace.h @@ -72,7 +72,6 @@ void gxVirtualScale (GXHEADER *a, GXHEADER *b); void delay (int millisecs); int brandom (int limit); -int biostime (int, long); int bioskey (int wait); int inp (int port); @@ -101,9 +100,6 @@ void LMove (void *p); int getch (void); -int setup_gtk (int *argcp, char ***argvp); -void step_gtk (void); -void display_gtk_img (unsigned char *rgb); double get_time (void); int show_intro_flag; @@ -117,20 +113,6 @@ void gr_set_color_map (unsigned char *map); int RLED_img (void *src, void *dest, unsigned int src_size, int w, int h); -struct frm { - FILE *fin; - int next_frame_chunks; - int frame_idx; - int nframes; - int frame_rate; - unsigned char pal[768]; -}; - -struct frm *frm_open (char *filename); -struct frm *frm_open_seq (int seq, int mode); -int frm_get2 (struct frm *frm, void *pixels_arg, void *map); -void frm_close (struct frm *frm); - void idle_loop (int ticks); void idle_loop_secs (double secs); @@ -142,17 +124,4 @@ void GetMouse_fast (void); void dbg (char const *fmt, ...) __attribute__ ((format (printf, 1, 2))); -#ifdef _WIN32 -struct timezone; -int gettimeofday (struct timeval *tv, struct timezone *tz); -#endif - -void *xmalloc (size_t size); -void *xcalloc (size_t nelems, size_t size); -void *xrealloc (void *ptr, size_t size); -char *xstrdup (char const *a); -int xstrcasecmp (char const *a, char const *b); -int xstrncasecmp (char const *a, char const *b, int len); -ssize_t fread_dyn(char **destp, size_t *n, FILE *stream); - #endif /* __PACE_H__ */ diff --git a/place.c b/place.c index 7849508..20fed0c 100644 --- a/place.c +++ b/place.c @@ -20,6 +20,7 @@ #include "Buzz_inc.h" #include "externs.h" #include "av.h" +#include "utils.h" extern char IDT[5],IKEY[5],AL_CALL,AI[2]; extern struct mStr Mis; diff --git a/port.c b/port.c index 23e0669..34a2608 100644 --- a/port.c +++ b/port.c @@ -28,6 +28,7 @@ #include "externs.h" #include "macros.h" #include "av.h" +#include "utils.h" #define LET_A 0x09 #define LET_M 0x0A diff --git a/sdl.c b/sdl.c index 241943c..6ce8226 100644 --- a/sdl.c +++ b/sdl.c @@ -11,6 +11,7 @@ #include "Buzz_inc.h" #include "macros.h" #include "options.h" +#include "utils.h" #include "av.h" #define MAX_X 320 diff --git a/utils.c b/utils.c new file mode 100644 index 0000000..3cbe5ee --- /dev/null +++ b/utils.c @@ -0,0 +1,140 @@ +#include "utils.h" +#include +#include +#include +#include +#include +#include + +/* taken from ffmpeg project */ +#ifdef CONFIG_WIN32 +# include +# include +#elif defined(CONFIG_OS2) +# include +# include +#else +# include +# include +# include +#endif + +double +get_time (void) +{ +#ifdef CONFIG_WIN32 + struct timeb tb; + + _ftime(&tb); + return tb.time + tb.millitm / 1e3; +#else + struct timeval tv; + + gettimeofday(&tv, NULL); + return tv.tv_sec + tv.tv_usec / 1e6; +#endif +} + +int +xstrcasecmp (char const *a, char const *b) +{ + while (*a) { + if (tolower (*a & 0xff) != tolower (*b & 0xff)) + break; + a++; + b++; + } + return (tolower(*a) - tolower(*b)); +} + +int +xstrncasecmp (char const *a, char const *b, int n) +{ + while (n && *a) { + if (tolower (*a & 0xff) != tolower (*b & 0xff)) + break; + a++; + b++; + n--; + } + return (tolower(*a) - tolower(*b)); +} + +void * +xmalloc (size_t n) +{ + void *p; + + if ((p = malloc (n)) == NULL) { + perror("malloc"); + exit(EXIT_FAILURE); + } + + return (p); +} + +void * +xcalloc (size_t a, size_t b) +{ + void *p; + + if ((p = calloc (a, b)) == NULL) { + perror("calloc"); + exit(EXIT_FAILURE); + } + + return (p); +} + +char * +xstrdup (const char *s) +{ + void *p; + + p = xmalloc (strlen (s) + 1); + strcpy (p, s); + return (p); +} + +void * +xrealloc(void *ptr, size_t size) +{ + void *p = realloc(ptr, size); + + if (!p) + { + perror("realloc"); + exit(EXIT_FAILURE); + } + + return (p); +} + +ssize_t +fread_dyn(char **destp, size_t *n, FILE *stream) +{ + const unsigned bsize = 8192; + size_t total = 0, cnt = 0; + + assert(destp); + assert(n); + assert(stream); + + if (!*destp) + *destp = xmalloc(*n = bsize); + + while (1) { + cnt = fread(*destp+total, 1, *n-total, stream); + if (cnt != *n-total) + { + if (feof(stream)) + return total+cnt; + else if (ferror(stream)) + return -1; + } + total += cnt; + + if (*n <= total) + *destp = xrealloc(*destp, *n *= 2); + } +} diff --git a/utils.h b/utils.h new file mode 100644 index 0000000..3fc00f3 --- /dev/null +++ b/utils.h @@ -0,0 +1,18 @@ +#ifndef _UTILS_H +#define _UTILS_H + +#include "race.h" +#include "inttypes.h" +#include +#include + +void *xmalloc (size_t size); +void *xcalloc (size_t nelems, size_t size); +void *xrealloc (void *ptr, size_t size); +char *xstrdup (char const *a); +int xstrcasecmp (char const *a, char const *b); +int xstrncasecmp (char const *a, char const *b, int len); +ssize_t fread_dyn(char **destp, size_t *n, FILE *stream); +double get_time (void); + +#endif /* _UTILS_H */ diff --git a/vab.c b/vab.c index cf515ee..7ed1a64 100644 --- a/vab.c +++ b/vab.c @@ -27,6 +27,7 @@ #include "Buzz_inc.h" #include "externs.h" +#include "utils.h" extern char AI[2]; struct VInfo VAS[7][4]; diff --git a/version.c b/version.c index ee07c1f..1f99473 100644 --- a/version.c +++ b/version.c @@ -1,2 +1,2 @@ -141 +142 diff --git a/vtest2.c b/vtest2.c index 29878c9..7907618 100644 --- a/vtest2.c +++ b/vtest2.c @@ -16,7 +16,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "race.h" -#include "pace.h" +#include "utils.h" #include "mmfile.h" #include "int_types.h" #include @@ -26,47 +26,13 @@ #include #include #include -#include #include -#include -#include #include #ifndef min # define min(a, b) ((a) <= (b) ? (a) : (b)) #endif -/* taken from ffmpeg project */ -#ifdef CONFIG_WIN32 -# include -# include -#elif defined(CONFIG_OS2) -# include -# include -#else -# include -# include -# include -#endif - -/* TODO: make sure it works and move where it belongs (portability?) */ -int64_t -xgettime(void) -{ -#ifdef CONFIG_WIN32 - struct timeb tb; - - _ftime(&tb); - return ((int64_t) tb.time * (int64_t) (1000) - + (int64_t) tb.millitm) * (int64_t) (1000); -#else - struct timeval tv; - - gettimeofday(&tv, NULL); - return (int64_t) tv.tv_sec * 1000000 + tv.tv_usec; -#endif -} - void eperror(const char *str) { @@ -164,25 +130,27 @@ main(int argc, char **argv) if (have_audio) { int bytes; - uint64_t tdiff; + int bps; + double tdiff; SDL_AudioSpec desired; desired.channels = ch; desired.freq = hz; desired.format = AUDIO_U8; + bps = 1*1; desired.samples = 4096; desired.callback = audio_cb; desired.userdata = &abuf; if (SDL_OpenAudio(&desired, NULL) < 0) eprintf("SDL_OpenAudio: %s\n", SDL_GetError()); - abuf.size = 4096; + abuf.size = 4*4096; abuf.off = 0; abuf.bytes = 0; abuf.buf = xmalloc(abuf.size); - tdiff = xgettime(); - while ((bytes = mm_decode_audio(&media, + tdiff = get_time(); + while ((bytes = mm_convert_audio(&media, abuf.buf + abuf.bytes, abuf.size - abuf.bytes)) > 0) { abuf.bytes += bytes; @@ -190,11 +158,11 @@ main(int argc, char **argv) abuf.buf = xrealloc(abuf.buf, abuf.size *= 2); }; if (bytes < 0) - eperror("decode_audio"); + eperror("convert_audio"); - printf("Decoding: %.3fs\n", (xgettime() - tdiff) / 1e6); - printf("Audio: %d samples, %.2fs\n", abuf.bytes / ch, - (double) (abuf.bytes) / ch / hz); + printf("Decoding: %.3f seconds\n", get_time() - tdiff); + printf("Audio: %d samples, %.2f seconds\n", abuf.bytes / bps, + (double) (abuf.bytes) / bps / desired.freq); SDL_PauseAudio(0); } @@ -217,15 +185,15 @@ main(int argc, char **argv) } if (have_video && !end) { - static int64_t oldt, newt; + static double oldt, newt; if (mm_decode_video(&media, ovl) > 0) { SDL_Rect r = { 0, 0, w, h }; - newt = 1e6 / fps + oldt - xgettime(); + newt = 1 / fps + oldt - get_time(); if (newt > 0) - SDL_Delay(newt/1000); + SDL_Delay(newt * 1000); SDL_DisplayYUVOverlay(ovl, &r); - oldt = xgettime(); + oldt = get_time(); } else end = 1;