Commit 636f75c4 authored by conrad's avatar conrad

* ensure current unit is calculated before read_packet

* code cleanups, move seek guess calculation to separate function
* improved debugging, add DEBUG_VERBOSE for page reading output


git-svn-id: http://svn.annodex.net/liboggz/trunk@768 8158c8cd-e7e1-0310-9fa4-c5954c97daef
parent bc9c61f5
...@@ -61,6 +61,7 @@ ...@@ -61,6 +61,7 @@
#include "oggz_private.h" #include "oggz_private.h"
/*#define DEBUG*/ /*#define DEBUG*/
/* #define DEBUG_VERBOSE */
#define CHUNKSIZE 65536 #define CHUNKSIZE 65536
...@@ -213,12 +214,12 @@ oggz_get_next_page_7 (OGGZ * oggz, ogg_page * og) ...@@ -213,12 +214,12 @@ oggz_get_next_page_7 (OGGZ * oggz, ogg_page * og)
return -2; return -2;
#endif #endif
} else if (more < 0) { } else if (more < 0) {
#ifdef DEBUG #ifdef DEBUG_VERBOSE
printf ("get_next_page: skipped %ld bytes\n", -more); printf ("get_next_page: skipped %ld bytes\n", -more);
#endif #endif
page_offset -= more; page_offset -= more;
} else { } else {
#ifdef DEBUG #ifdef DEBUG_VERBOSE
printf ("get_next_page: page has %ld bytes\n", more); printf ("get_next_page: page has %ld bytes\n", more);
#endif #endif
found = 1; found = 1;
...@@ -296,11 +297,10 @@ oggz_read_sync (OGGZ * oggz) ...@@ -296,11 +297,10 @@ oggz_read_sync (OGGZ * oggz)
oggz_auto (oggz, op, serialno, NULL); oggz_auto (oggz, op, serialno, NULL);
} }
#if 0 /* set unit on last packet of page */ /* set unit on last packet of page */
if ((oggz->metric || stream->metric) && granulepos != -1) { if ((oggz->metric || stream->metric) && granulepos != -1) {
reader->current_unit = oggz_get_unit (oggz, serialno, granulepos); reader->current_unit = oggz_get_unit (oggz, serialno, granulepos);
} }
#endif
if (stream->read_packet) { if (stream->read_packet) {
cb_ret = cb_ret =
...@@ -339,6 +339,7 @@ oggz_read_sync (OGGZ * oggz) ...@@ -339,6 +339,7 @@ oggz_read_sync (OGGZ * oggz)
ogg_int64_t granulepos; ogg_int64_t granulepos;
granulepos = ogg_page_granulepos (&og); granulepos = ogg_page_granulepos (&og);
if ((oggz->metric || stream->metric) && granulepos != -1) { if ((oggz->metric || stream->metric) && granulepos != -1) {
reader->current_unit = oggz_get_unit (oggz, serialno, granulepos); reader->current_unit = oggz_get_unit (oggz, serialno, granulepos);
} else if (granulepos == 0) { } else if (granulepos == 0) {
...@@ -652,12 +653,12 @@ oggz_get_next_page (OGGZ * oggz, ogg_page * og) ...@@ -652,12 +653,12 @@ oggz_get_next_page (OGGZ * oggz, ogg_page * og)
} }
if (bytes == 0) { if (bytes == 0) {
#ifdef DEBUG #ifdef DEBUG_VERBOSE
printf ("get_next_page: bytes == 0, returning -2\n"); printf ("get_next_page: bytes == 0, returning -2\n");
#endif #endif
return -2; return -2;
} else if (oggz->file && feof (oggz->file)) { } else if (oggz->file && feof (oggz->file)) {
#ifdef DEBUG #ifdef DEBUG_VERBOSE
printf ("get_next_page: feof (oggz->file), returning -2\n"); printf ("get_next_page: feof (oggz->file), returning -2\n");
#endif #endif
clearerr (oggz->file); clearerr (oggz->file);
...@@ -667,12 +668,12 @@ oggz_get_next_page (OGGZ * oggz, ogg_page * og) ...@@ -667,12 +668,12 @@ oggz_get_next_page (OGGZ * oggz, ogg_page * og)
ogg_sync_wrote(&reader->ogg_sync, bytes); ogg_sync_wrote(&reader->ogg_sync, bytes);
} else if (more < 0) { } else if (more < 0) {
#ifdef DEBUG #ifdef DEBUG_VERBOSE
printf ("get_next_page: skipped %ld bytes\n", -more); printf ("get_next_page: skipped %ld bytes\n", -more);
#endif #endif
page_offset -= more; page_offset -= more;
} else { } else {
#ifdef DEBUG #ifdef DEBUG_VERBOSE
printf ("get_next_page: page has %ld bytes\n", more); printf ("get_next_page: page has %ld bytes\n", more);
#endif #endif
found = 1; found = 1;
...@@ -873,13 +874,13 @@ oggz_scan_for_page (OGGZ * oggz, ogg_page * og, ogg_int64_t unit_target, ...@@ -873,13 +874,13 @@ oggz_scan_for_page (OGGZ * oggz, ogg_page * og, ogg_int64_t unit_target,
if (unit_at < unit_target) { if (unit_at < unit_target) {
#ifdef DEBUG #ifdef DEBUG
printf (" scan: (%ld) < (%ld)\n", unit_at, unit_target); printf (" scan: (%lld) < (%lld)\n", unit_at, unit_target);
#endif #endif
offset_prev = offset_next; offset_prev = offset_next;
offset_begin = offset_next+1; offset_begin = offset_next+1;
} else if (unit_at > unit_target) { } else if (unit_at > unit_target) {
#ifdef DEBUG #ifdef DEBUG
printf (" scan: (%ld) > (%ld)\n", unit_at, unit_target); printf (" scan: (%lld) > (%lld)\n", unit_at, unit_target);
#endif #endif
#if 0 #if 0
/* hole ? */ /* hole ? */
...@@ -899,7 +900,7 @@ oggz_scan_for_page (OGGZ * oggz, ogg_page * og, ogg_int64_t unit_target, ...@@ -899,7 +900,7 @@ oggz_scan_for_page (OGGZ * oggz, ogg_page * og, ogg_int64_t unit_target,
#endif #endif
} else if (unit_at == unit_target) { } else if (unit_at == unit_target) {
#ifdef DEBUG #ifdef DEBUG
printf (" scan: (%ld) == (%ld)\n", unit_at, unit_target); printf (" scan: (%lld) == (%lld)\n", unit_at, unit_target);
#endif #endif
break; break;
} }
...@@ -910,6 +911,30 @@ oggz_scan_for_page (OGGZ * oggz, ogg_page * og, ogg_int64_t unit_target, ...@@ -910,6 +911,30 @@ oggz_scan_for_page (OGGZ * oggz, ogg_page * og, ogg_int64_t unit_target,
#define GUESS_MULTIPLIER (1<<16) #define GUESS_MULTIPLIER (1<<16)
static ogg_int64_t
guess (ogg_int64_t unit_at, ogg_int64_t unit_target,
ogg_int64_t unit_begin, ogg_int64_t unit_end,
oggz_off_t offset_begin, oggz_off_t offset_end)
{
ogg_int64_t guess_ratio;
oggz_off_t offset_guess;
guess_ratio =
GUESS_MULTIPLIER * (unit_target - unit_begin) /
(unit_at - unit_begin);
#ifdef DEBUG
printf ("oggz_seek::guess: guess_ratio %lld = (%lld - %lld) / (%lld - %lld)\n",
guess_ratio, unit_target, unit_begin, unit_at, unit_begin);
#endif
offset_guess = offset_begin +
(oggz_off_t)(((offset_end - offset_begin) * guess_ratio) /
GUESS_MULTIPLIER);
return offset_guess;
}
static ogg_int64_t static ogg_int64_t
oggz_seek_set (OGGZ * oggz, ogg_int64_t unit_target) oggz_seek_set (OGGZ * oggz, ogg_int64_t unit_target)
{ {
...@@ -921,7 +946,6 @@ oggz_seek_set (OGGZ * oggz, ogg_int64_t unit_target) ...@@ -921,7 +946,6 @@ oggz_seek_set (OGGZ * oggz, ogg_int64_t unit_target)
ogg_int64_t granule_at; ogg_int64_t granule_at;
ogg_int64_t unit_at, unit_begin = 0, unit_end = -1; ogg_int64_t unit_at, unit_begin = 0, unit_end = -1;
long serialno; long serialno;
ogg_int64_t guess_ratio;
ogg_page * og; ogg_page * og;
if (oggz == NULL) { if (oggz == NULL) {
...@@ -929,7 +953,9 @@ oggz_seek_set (OGGZ * oggz, ogg_int64_t unit_target) ...@@ -929,7 +953,9 @@ oggz_seek_set (OGGZ * oggz, ogg_int64_t unit_target)
} }
if (unit_target > 0 && !oggz_has_metrics (oggz)) { if (unit_target > 0 && !oggz_has_metrics (oggz)) {
/* No metric defined */ #ifdef DEBUG
printf ("oggz_seek_set: No metric defined, FAIL\n");
#endif
return -1; return -1;
} }
...@@ -984,11 +1010,7 @@ oggz_seek_set (OGGZ * oggz, ogg_int64_t unit_target) ...@@ -984,11 +1010,7 @@ oggz_seek_set (OGGZ * oggz, ogg_int64_t unit_target)
offset_at = oggz_tell_raw (oggz); offset_at = oggz_tell_raw (oggz);
if (offset_at == -1) return -1; if (offset_at == -1) return -1;
#if 0
offset_orig = offset_at;
#else
offset_orig = oggz->offset; offset_orig = oggz->offset;
#endif
offset_begin = 0; offset_begin = 0;
...@@ -1007,56 +1029,21 @@ oggz_seek_set (OGGZ * oggz, ogg_int64_t unit_target) ...@@ -1007,56 +1029,21 @@ oggz_seek_set (OGGZ * oggz, ogg_int64_t unit_target)
if (unit_end == -1) { if (unit_end == -1) {
if (unit_at == unit_begin) { if (unit_at == unit_begin) {
#ifdef DEBUG
printf ("*G1*");
#endif
offset_guess = offset_begin + (offset_end - offset_begin)/2; offset_guess = offset_begin + (offset_end - offset_begin)/2;
} else { } else {
#ifdef DEBUG offset_guess = guess (unit_at, unit_target, unit_begin, unit_end,
printf ("*G2*"); offset_begin, offset_at);
#endif
guess_ratio =
GUESS_MULTIPLIER * (unit_target - unit_begin) /
(unit_at - unit_begin);
#ifdef DEBUG
printf ("\noggz_seek_set: [B] guess_ratio %lld = (%lld - %lld) / (%lld - %lld)\n",
guess_ratio, unit_target, unit_begin, unit_at, unit_begin);
#endif
offset_guess = offset_begin +
(oggz_off_t)(((offset_at - offset_begin) * guess_ratio) /
GUESS_MULTIPLIER);
} }
} else if (unit_end <= unit_begin) { } else if (unit_end <= unit_begin) {
#ifdef DEBUG #ifdef DEBUG
printf ("unit_end <= unit_begin\n"); printf ("oggz_seek_set: unit_end <= unit_begin (ERROR)\n");
#endif #endif
break; break;
} else { } else {
#if 1 offset_guess = guess (unit_at, unit_target, unit_begin, unit_end,
guess_ratio = offset_begin, offset_end);
GUESS_MULTIPLIER * (unit_target - unit_begin) /
(unit_end - unit_begin);
offset_guess = offset_begin +
(oggz_off_t)(((offset_end - offset_begin) * guess_ratio) /
GUESS_MULTIPLIER);
/*
if (offset_guess <= offset_begin) {
offset_guess = offset_begin + 1;
}
*/
#else
offset_guess = offset_begin + (offset_end - offset_begin)/2;
#endif
} }
#ifdef DEBUG
printf ("%ld ->", offset_guess);
#endif
offset_at = oggz_seek_raw (oggz, offset_guess, SEEK_SET); offset_at = oggz_seek_raw (oggz, offset_guess, SEEK_SET);
if (offset_at == -1) { if (offset_at == -1) {
goto notfound; goto notfound;
...@@ -1064,10 +1051,6 @@ oggz_seek_set (OGGZ * oggz, ogg_int64_t unit_target) ...@@ -1064,10 +1051,6 @@ oggz_seek_set (OGGZ * oggz, ogg_int64_t unit_target)
offset_next = oggz_get_next_start_page (oggz, og); offset_next = oggz_get_next_start_page (oggz, og);
#ifdef DEBUG
printf ("\n");
#endif
if (unit_end == -1 && offset_next == -2) { /* reached eof, backtrack */ if (unit_end == -1 && offset_next == -2) { /* reached eof, backtrack */
offset_next = oggz_get_prev_start_page (oggz, og, &granule_at, offset_next = oggz_get_prev_start_page (oggz, og, &granule_at,
&serialno); &serialno);
...@@ -1087,12 +1070,9 @@ oggz_seek_set (OGGZ * oggz, ogg_int64_t unit_target) ...@@ -1087,12 +1070,9 @@ oggz_seek_set (OGGZ * oggz, ogg_int64_t unit_target)
} }
if (offset_next < offset_end) { if (offset_next < offset_end) {
offset_next = oggz_scan_for_page (oggz, og, unit_target, offset_next =
offset_begin, offset_end); oggz_scan_for_page (oggz, og, unit_target, offset_begin, offset_end);
if (offset_next < 0) goto notfound;
if (offset_next < 0) {
goto notfound;
}
offset_at = offset_next; offset_at = offset_next;
serialno = ogg_page_serialno (og); serialno = ogg_page_serialno (og);
...@@ -1197,13 +1177,25 @@ oggz_seek_units (OGGZ * oggz, ogg_int64_t units, int whence) ...@@ -1197,13 +1177,25 @@ oggz_seek_units (OGGZ * oggz, ogg_int64_t units, int whence)
{ {
OggzReader * reader = &oggz->x.reader; OggzReader * reader = &oggz->x.reader;
if (oggz == NULL) return -1; if (oggz == NULL) {
#ifdef DEBUG
printf ("oggz_seek_units: oggz NULL, FAIL\n");
#endif
return -1;
}
if (oggz->flags & OGGZ_WRITE) { if (oggz->flags & OGGZ_WRITE) {
#ifdef DEBUG
printf ("oggz_seek_units: is OGGZ_WRITE, FAIL\n");
#endif
return -1; return -1;
} }
if (!oggz_has_metrics (oggz)) { if (!oggz_has_metrics (oggz)) {
#ifdef DEBUG
printf ("oggz_seek_units: !has_metrics, FAIL\n");
#endif
return -1; return -1;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment