- 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, CHUNK_SIZE);
- len = read(in, buf, CHUNK_SIZE);
- ret = -E_OGG_READ;
- if (len <= 0)
- goto err1;
- ogg_sync_wrote(sync_in, len);
- ret = -E_SYNC_PAGEOUT;
- if (ogg_sync_pageout(sync_in, &page) <= 0)
- 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_INFO_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);
-
- header_len = 0;
- while (ogg_stream_flush(stream_out, &page))
- header_len += page.body_len + page.header_len;
- ret = len;
- PARA_INFO_LOG("header_len = %d\n", header_len);
-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;
-}
-
-static void tunetable(void)
-{
- int i = 1, j = -1, lp = 1;
- while (i < num_chunks) {
- if (chunk_table[i] == chunk_table[lp]) {
- i++;
- continue;
- }
- if (j < 0)
- tv_scale(i, &af->chunk_tv, &af->eof_tv);
- for (j = lp; j < i; j++)
- chunk_table[j] = chunk_table[i];
- lp = i;
- }
-#if 1
- for (i = 2; i < num_chunks; i++)
- if (chunk_table[i] != chunk_table[1])
- break;
- lp = i;
- for (i = 2; i < num_chunks - lp; i++)
- chunk_table[i] = chunk_table[i + lp];
-#endif
-}
-
-
-/*
- * 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 *
- * CHUNK_TIME begins.
- */
-static void ogg_compute_chunk_table(double time_total)
-{
- int i, ret, num;
- ssize_t pos = 0, min = 0, old_pos;
-
- 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(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);
- if (ret)
- break;
- pos = ov_raw_tell(oggvorbis_file);
- diff = pos - old_pos;
- max_chunk_len = PARA_MAX(max_chunk_len, diff);
- min = (i == 1)? diff : PARA_MIN(min, diff);
- chunk_table[i] = pos;
- if (i < 11 || !((i - 1) % 1000)|| i > num - 11)
- PARA_DEBUG_LOG("chunk #%d: %g secs, pos: %zd, "
- "size: %zd\n", i - 1,
- i * chunk_time, pos, pos - old_pos);
- old_pos = pos;
- }
- num_chunks = i - 1;
- chunk_table[i] = pos;
- tunetable();
- PARA_INFO_LOG("%zu chunks (%fs), max chunk: %zd, min chunk: %zd\n",
- num_chunks, chunk_time, max_chunk_len, min);
- rewind(infile);
-}