X-Git-Url: http://git.tuebingen.mpg.de/?p=paraslash.git;a=blobdiff_plain;f=ogg_afh.c;h=709e7d4b3c5e7278c72ba6fb2451c8798dcab411;hp=d295f2d31ba1a482f7c67a0d0663a1efc5c82867;hb=3af76637cf5a96111d76e422b6f16a3883c931b5;hpb=c79899fe6a1e8eb3086749f46c599d9790d99921;ds=sidebyside diff --git a/ogg_afh.c b/ogg_afh.c index d295f2d3..709e7d4b 100644 --- a/ogg_afh.c +++ b/ogg_afh.c @@ -34,17 +34,14 @@ static double chunk_time = 0.25; static OggVorbis_File *oggvorbis_file; -static FILE *infile; -static int header_len, oggbuf_len, vi_channels; -static char *header, *oggbuf; -static ssize_t *chunk_table, max_chunk_len; +static int header_len; +static char *header; +static ssize_t *chunk_table; static struct audio_format_handler *af; -static long vi_sampling_rate, vi_bitrate, vi_bitrate_nominal, - num_chunks; -static int ogg_compute_header_len(void) +static int ogg_compute_header_len(FILE *file) { - int ret, len, in = fileno(infile); + int ret, len, in = fileno(file); unsigned int serial; char *buf; ogg_page page; @@ -81,11 +78,9 @@ static int ogg_compute_header_len(void) goto err2; } ret = -E_VORBIS; - if (vorbis_synthesis_headerin(&vi, &vc, &packet) < 0) { + if (vorbis_synthesis_headerin(&vi, &vc, &packet) < 0) goto err2; - } else - PARA_INFO_LOG("channels: %i, rate: %li\n", vi.channels, - vi.rate); + PARA_INFO_LOG("channels: %i, rate: %li\n", vi.channels, vi.rate); ogg_stream_packetin(stream_out, &packet); ret = ogg_sync_pageout(sync_in, &page); @@ -121,7 +116,7 @@ err1: return ret; } -static void tunetable(void) +static void tunetable(long unsigned num_chunks) { int i = 1, j = -1, lp = 1; while (i < num_chunks) { @@ -148,23 +143,23 @@ static void tunetable(void) /* * Alloc and fill array table of byte offsets. chunk_table[i] is the - * offset in the current infile at which the sample containing time i * + * offset in the current input file at which the sample containing time i * * CHUNK_TIME begins. */ -static void ogg_compute_chunk_table(double time_total) +static long unsigned ogg_compute_chunk_table(double time_total) { int i, ret, num; - ssize_t pos = 0, min = 0, old_pos; + ssize_t max_chunk_len, pos = 0, min = 0, old_pos; + long unsigned num_chunks; old_pos = 0; ret = 0; num = time_total / chunk_time + 3; PARA_DEBUG_LOG("chunk time: %g allocating %d chunk pointers\n", chunk_time, num); - chunk_table = para_malloc(num * sizeof(ogg_int64_t)); + chunk_table = para_malloc(num * sizeof(size_t)); chunk_table[0] = 0; max_chunk_len = 0; - rewind(infile); for (i = 1; ret == 0; i++) { ogg_int64_t diff; ret = ov_time_seek(oggvorbis_file, i * chunk_time); @@ -183,10 +178,10 @@ static void ogg_compute_chunk_table(double time_total) } num_chunks = i - 1; chunk_table[i] = pos; - tunetable(); - PARA_INFO_LOG("%li chunks (%fs), max chunk: %zd, min chunk: %zd\n", + tunetable(num_chunks); + PARA_INFO_LOG("%zu chunks (%fs), max chunk: %zd, min chunk: %zd\n", num_chunks, chunk_time, max_chunk_len, min); - rewind(infile); + return num_chunks; } static void ogg_close_audio_file(void) @@ -202,10 +197,6 @@ static void ogg_close_audio_file(void) header_len = 0; free(chunk_table); chunk_table = NULL; - num_chunks = 0; - free(oggbuf); - oggbuf = NULL; - oggbuf_len = 0; } static int ogg_save_header(FILE *file, int len) @@ -224,17 +215,17 @@ static int ogg_save_header(FILE *file, int len) * Init oggvorbis file and write some tech data to given pointers. */ static int ogg_get_file_info(FILE *file, char *info_str, long unsigned *frames, - int *seconds) + int *seconds, size_t **vss_chunk_table) { int ret; double time_total; vorbis_info *vi; ogg_int64_t raw_total; + long vi_sampling_rate, vi_bitrate; - infile = file; if (!file) return -E_OGG_NO_FILE; - ret = ogg_compute_header_len(); + ret = ogg_compute_header_len(file); if (ret < 0) return ret; ret = ogg_save_header(file, header_len); @@ -257,15 +248,15 @@ static int ogg_get_file_info(FILE *file, char *info_str, long unsigned *frames, *seconds = time_total; vi_sampling_rate = vi->rate; vi_bitrate = ov_bitrate(oggvorbis_file, 0); - vi_bitrate_nominal = vi->bitrate_nominal; - vi_channels = vi->channels; - ogg_compute_chunk_table(time_total); - *frames = num_chunks; + rewind(file); + *frames = ogg_compute_chunk_table(time_total); + rewind(file); + *vss_chunk_table = chunk_table; sprintf(info_str, "audio_file_info1:%lu x %lu, %ldkHz, %d channels, %ldkbps\n" "audio_file_info2: \n" "audio_file_info3: \n", - num_chunks, (long unsigned) (chunk_time * 1000 * 1000), - vi_sampling_rate / 1000, vi_channels, vi_bitrate / 1000 + *frames, (long unsigned) (chunk_time * 1000 * 1000), + vi_sampling_rate / 1000, vi->channels, vi_bitrate / 1000 ); rewind(file); return 1; @@ -274,73 +265,6 @@ err: return ret; } -/* - * Simple stream reposition routine - */ -static int ogg_reposition_stream(long unsigned request) -{ - int ret; - long seek; - - seek = chunk_table[request]; - ret = fseek(infile, seek, SEEK_SET); - PARA_DEBUG_LOG("seek to %li returned %d. offset:%li\n", seek, - ret, ftell(infile)); - return ret < 0? -E_OGG_REPOS : 1; -} - -static ogg_int64_t get_chunk_size(long unsigned chunk_num) -{ - ogg_int64_t ret; - if (chunk_num >= num_chunks) - return -1; - ret = chunk_table[chunk_num + 1] - chunk_table[chunk_num]; - if (!ret) - return ret; -#if 0 - PARA_DEBUG_LOG("chunk %d: %lli-%lli (%lli bytes)\n", - chunk_num, - chunk_table[chunk_num], - chunk_table[chunk_num + 1] - 1, - ret); -#endif - return ret; -} - -static char *ogg_read_chunk(long unsigned current_chunk, ssize_t *len) -{ - int ret; - ogg_int64_t cs = get_chunk_size(current_chunk); - if (!cs) { /* nothing to send for this run */ - *len = 0; - return oggbuf; - } - if (cs < 0) { /* eof */ - *len = 0; - return NULL; - } - *len = cs; - if (!oggbuf || oggbuf_len < *len) { - PARA_INFO_LOG("increasing ogg buffer size (%d -> %zu)\n", - oggbuf_len, *len); - oggbuf = para_realloc(oggbuf, *len); - oggbuf_len = *len; - } - ret = read(fileno(infile), oggbuf, *len); - if (!ret) { - *len = 0; - return NULL; - } - if (ret < 0) { - *len = -E_OGG_READ; - return NULL; - } - if (ret != *len) - PARA_WARNING_LOG("short read (%d/%zd)\n", ret, *len); - *len = ret; - return oggbuf; -} - static char *ogg_get_header_info(int *len) { *len = header_len; @@ -357,9 +281,7 @@ static const char* ogg_suffixes[] = {"ogg", NULL}; void ogg_init(struct audio_format_handler *p) { af = p; - af->reposition_stream = ogg_reposition_stream; af->get_file_info = ogg_get_file_info, - af->read_chunk = ogg_read_chunk; af->close_audio_file = ogg_close_audio_file; af->get_header_info = ogg_get_header_info; af->chunk_tv.tv_sec = 0;