- if (ret == OV_EVERSION)
- return -E_OGG_VERSION;
- if (ret == OV_EBADHEADER)
- return -E_OGG_BAD_HEADER;
- if (ret < 0)
- return -E_OGG_UNKNOWN_ERROR;
- return 1;
-
-}
-
-static int ogg_compute_header_len(char *map, size_t numbytes,
- struct afh_info *afhi)
-{
- int ret;
- size_t len = PARA_MIN(numbytes, (size_t)CHUNK_SIZE);
- int serial;
- char *buf;
-
- ogg_page page;
- ogg_packet packet;
- vorbis_comment vc;
- vorbis_info vi;
- ogg_stream_state *stream_in = para_malloc(sizeof(ogg_stream_state));
- ogg_stream_state *stream_out = para_malloc(sizeof(ogg_stream_state));
- ogg_sync_state *sync_in = para_malloc(sizeof(ogg_sync_state));
-
- ogg_sync_init(sync_in);
- vorbis_info_init(&vi);
- vorbis_comment_init(&vc);
- buf = ogg_sync_buffer(sync_in, (long)len);
- memcpy(buf, map, len);
- ogg_sync_wrote(sync_in, (long)len);
- ret = -E_SYNC_PAGEOUT;
- if (ogg_sync_pageout(sync_in, &page) <= 0) {
- free(stream_in);
- free(stream_out);
- goto err1;
- }
- serial = ogg_page_serialno(&page);
- ogg_stream_init(stream_in, serial);
- ogg_stream_init(stream_out, serial);
- ret = ogg_stream_pagein(stream_in, &page);
- if (ret < 0) {
- ret = -E_STREAM_PAGEIN;
- goto err2;
- }
- ret = ogg_stream_packetout(stream_in, &packet);
- if (ret != 1) {
- ret = -E_STREAM_PACKETOUT;
- goto err2;
- }
- ret = -E_VORBIS;
- if (vorbis_synthesis_headerin(&vi, &vc, &packet) < 0)
- goto err2;
- PARA_DEBUG_LOG("channels: %i, rate: %li\n", vi.channels, vi.rate);
- ogg_stream_packetin(stream_out, &packet);
- ret = ogg_sync_pageout(sync_in, &page);
- if (ret <= 0) {
- ret = -E_SYNC_PAGEOUT;
- goto err2;
- }
- ogg_stream_pagein(stream_in, &page);
- ogg_stream_packetout(stream_in, &packet);
- ogg_stream_packetin(stream_out, &packet);
-
- ret = ogg_sync_pageout(sync_in, &page);
- if (ret <= 0) {
- ret = -E_SYNC_PAGEOUT;
- goto err2;
- }
- ogg_stream_pagein(stream_in, &page);
- ogg_stream_packetout(stream_in, &packet);
- ogg_stream_packetin(stream_out, &packet);
-
- afhi->header_len = 0;
- while (ogg_stream_flush(stream_out, &page))
- afhi->header_len += page.body_len + page.header_len;
- PARA_DEBUG_LOG("header_len = %d\n", afhi->header_len);
- afhi->header_offset = 0;
- ret = 1;
-err2:
- ogg_stream_destroy(stream_in);
- ogg_stream_destroy(stream_out);
-err1:
- ogg_sync_destroy(sync_in);
- vorbis_info_clear(&vi);
- vorbis_comment_clear(&vc);
- return ret;
-}
-
-/*
- * Alloc and fill array table of byte offsets. chunk_table[i] is the
- * offset in the current input file at which the sample containing time i *
- * CHUNK_TIME begins. Always successful.
- */
-static long unsigned ogg_compute_chunk_table(OggVorbis_File *of,
- struct afh_info *afhi, long unsigned time_total)
-{
- int i, ret, num;
- long unsigned num_chunks;
- ogg_int64_t max = 0, min = 0, old_pos = 0;
-
- num = time_total / chunk_time + 3;
- PARA_DEBUG_LOG("chunk time: %g allocating %d chunk pointers\n",
- chunk_time, num);
- afhi->chunk_table = para_malloc((num + 1) * sizeof(size_t));
- afhi->chunk_table[0] = 0;
- for (i = 1; i <= num; i++) {
- ogg_int64_t diff, pos;
- ret = ov_time_seek(of, i * chunk_time);
- if (ret)
- break;
- pos = ov_raw_tell(of);
- diff = pos - old_pos;
- max = PARA_MAX(max, diff);
- min = (i == 1)? diff : PARA_MIN(min, diff);
- afhi->chunk_table[i] = pos;
- old_pos = pos;