Commit bcf0128c authored by conrad's avatar conrad

cleanups -- move file length and offset guessing routines to separate functions


git-svn-id: http://svn.annodex.net/liboggz/trunk@820 8158c8cd-e7e1-0310-9fa4-c5954c97daef
parent 692e27d6
...@@ -373,7 +373,7 @@ oggz_get_prev_start_page (OGGZ * oggz, ogg_page * og, ...@@ -373,7 +373,7 @@ oggz_get_prev_start_page (OGGZ * oggz, ogg_page * og,
if (offset_at == -1) return -1; if (offset_at == -1) return -1;
if (offset_at > 0) if (offset_at >= 0)
return prev_offset; return prev_offset;
else else
return -1; return -1;
...@@ -479,7 +479,7 @@ oggz_scan_for_page (OGGZ * oggz, ogg_page * og, ogg_int64_t unit_target, ...@@ -479,7 +479,7 @@ 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 static oggz_off_t
guess (ogg_int64_t unit_at, ogg_int64_t unit_target, guess (ogg_int64_t unit_at, ogg_int64_t unit_target,
ogg_int64_t unit_begin, ogg_int64_t unit_end, ogg_int64_t unit_begin, ogg_int64_t unit_end,
oggz_off_t offset_begin, oggz_off_t offset_end) oggz_off_t offset_begin, oggz_off_t offset_end)
...@@ -505,31 +505,41 @@ guess (ogg_int64_t unit_at, ogg_int64_t unit_target, ...@@ -505,31 +505,41 @@ guess (ogg_int64_t unit_at, ogg_int64_t unit_target,
return offset_guess; return offset_guess;
} }
static ogg_int64_t static oggz_off_t
oggz_seek_set (OGGZ * oggz, ogg_int64_t unit_target) oggz_seek_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_at,
oggz_off_t offset_begin, oggz_off_t offset_end)
{ {
OggzReader * reader = &oggz->x.reader; oggz_off_t offset_guess;
int fd;
struct stat statbuf;
oggz_off_t offset_orig, offset_at, offset_guess;
oggz_off_t offset_begin, offset_end = -1, offset_next;
ogg_int64_t granule_at;
ogg_int64_t unit_at, unit_begin = 0, unit_end = -1, unit_last_iter = -1;
long serialno;
ogg_page * og;
int looping = 0;
if (oggz == NULL) {
return -1;
}
if (unit_target > 0 && !oggz_has_metrics (oggz)) { if (unit_end == -1) {
if (unit_at == unit_begin) {
offset_guess = offset_begin + (offset_end - offset_begin)/2;
} else {
offset_guess = guess (unit_at, unit_target, unit_begin, unit_end,
offset_begin, offset_at);
}
} else if (unit_end <= unit_begin) {
#ifdef DEBUG #ifdef DEBUG
printf ("oggz_seek_set: No metric defined, FAIL\n"); printf ("oggz_seek_guess: unit_end <= unit_begin (ERROR)\n");
#endif #endif
return -1; offset_guess = -1;
} else {
offset_guess = guess (unit_at, unit_target, unit_begin, unit_end,
offset_begin, offset_end);
} }
return offset_guess;
}
static oggz_off_t
oggz_offset_end (OGGZ * oggz)
{
int fd;
struct stat statbuf;
oggz_off_t offset_end = -1;
if (oggz->file != NULL) { if (oggz->file != NULL) {
if ((fd = fileno (oggz->file)) == -1) { if ((fd = fileno (oggz->file)) == -1) {
/*oggz_set_error (oggz, OGGZ_ERR_SYSTEM);*/ /*oggz_set_error (oggz, OGGZ_ERR_SYSTEM);*/
...@@ -544,7 +554,7 @@ oggz_seek_set (OGGZ * oggz, ogg_int64_t unit_target) ...@@ -544,7 +554,7 @@ oggz_seek_set (OGGZ * oggz, ogg_int64_t unit_target)
if (oggz_stat_regular (statbuf.st_mode)) { if (oggz_stat_regular (statbuf.st_mode)) {
offset_end = statbuf.st_size; offset_end = statbuf.st_size;
#ifdef DEBUG #ifdef DEBUG
printf ("oggz_seek_set: stat size %ld\n", offset_end); printf ("oggz_offset_end: stat size %ld\n", offset_end);
#endif #endif
} else { } else {
/*oggz_set_error (oggz, OGGZ_ERR_NOSEEK);*/ /*oggz_set_error (oggz, OGGZ_ERR_NOSEEK);*/
...@@ -571,6 +581,36 @@ oggz_seek_set (OGGZ * oggz, ogg_int64_t unit_target) ...@@ -571,6 +581,36 @@ oggz_seek_set (OGGZ * oggz, ogg_int64_t unit_target)
} }
} }
return offset_end;
}
static ogg_int64_t
oggz_seek_set (OGGZ * oggz, ogg_int64_t unit_target)
{
OggzReader * reader = &oggz->x.reader;
oggz_off_t offset_orig, offset_at, offset_guess;
oggz_off_t offset_begin, offset_end = -1, offset_next;
ogg_int64_t granule_at;
ogg_int64_t unit_at, unit_begin = 0, unit_end = -1, unit_last_iter = -1;
long serialno;
ogg_page * og;
int hit_eof = 0;
if (oggz == NULL) {
return -1;
}
if (unit_target > 0 && !oggz_has_metrics (oggz)) {
#ifdef DEBUG
printf ("oggz_seek_set: No metric defined, FAIL\n");
#endif
return -1;
}
if ((offset_end = oggz_offset_end (oggz)) == -1) {
return -1;
}
if (unit_target == reader->current_unit) { if (unit_target == reader->current_unit) {
return (long)reader->current_unit; return (long)reader->current_unit;
} }
...@@ -597,33 +637,18 @@ oggz_seek_set (OGGZ * oggz, ogg_int64_t unit_target) ...@@ -597,33 +637,18 @@ oggz_seek_set (OGGZ * oggz, ogg_int64_t unit_target)
for ( ; ; ) { for ( ; ; ) {
unit_last_iter = unit_at; unit_last_iter = unit_at;
hit_eof = 0;
#ifdef DEBUG #ifdef DEBUG
printf ("oggz_seek_set: [A] want u%lld: (u%lld - u%lld) [@%ld - @%ld]\n", printf ("oggz_seek_set: [A] want u%lld: (u%lld - u%lld) [@%ld - @%ld]\n",
unit_target, unit_begin, unit_end, offset_begin, offset_end); unit_target, unit_begin, unit_end, offset_begin, offset_end);
#endif #endif
if (unit_end == -1) { offset_guess = oggz_seek_guess (unit_at, unit_target,
if (unit_at == unit_begin) { unit_begin, unit_end,
offset_guess = offset_begin + (offset_end - offset_begin)/2; offset_at,
} else { offset_begin, offset_end);
offset_guess = guess (unit_at, unit_target, unit_begin, unit_end, if (offset_guess == -1) break;
offset_begin, offset_at);
}
} else if (unit_end == unit_begin) {
#ifdef DEBUG
printf ("oggz_seek_set: unit_end == unit_begin (FOUND)\n");
#endif
goto found;
} else if (unit_end <= unit_begin) {
#ifdef DEBUG
printf ("oggz_seek_set: unit_end <= unit_begin (ERROR)\n");
#endif
break;
} else {
offset_guess = guess (unit_at, unit_target, unit_begin, unit_end,
offset_begin, offset_end);
}
#ifdef DEBUG #ifdef DEBUG
printf ("oggz_seek_set: guessed %ld\n", offset_guess); printf ("oggz_seek_set: guessed %ld\n", offset_guess);
...@@ -631,7 +656,7 @@ oggz_seek_set (OGGZ * oggz, ogg_int64_t unit_target) ...@@ -631,7 +656,7 @@ oggz_seek_set (OGGZ * oggz, ogg_int64_t unit_target)
if (offset_guess == offset_at) { if (offset_guess == offset_at) {
/* Already there, looping */ /* Already there, looping */
looping = 1; break;
} }
offset_at = oggz_seek_raw (oggz, offset_guess, SEEK_SET); offset_at = oggz_seek_raw (oggz, offset_guess, SEEK_SET);
...@@ -646,6 +671,7 @@ oggz_seek_set (OGGZ * oggz, ogg_int64_t unit_target) ...@@ -646,6 +671,7 @@ oggz_seek_set (OGGZ * oggz, ogg_int64_t unit_target)
#endif #endif
if (unit_end == -1 && offset_next == -2) { /* reached eof, backtrack */ if (unit_end == -1 && offset_next == -2) { /* reached eof, backtrack */
hit_eof = 1;
offset_next = oggz_get_prev_start_page (oggz, og, &granule_at, offset_next = oggz_get_prev_start_page (oggz, og, &granule_at,
&serialno); &serialno);
unit_end = oggz_get_unit (oggz, serialno, granule_at); unit_end = oggz_get_unit (oggz, serialno, granule_at);
...@@ -664,7 +690,7 @@ oggz_seek_set (OGGZ * oggz, ogg_int64_t unit_target) ...@@ -664,7 +690,7 @@ oggz_seek_set (OGGZ * oggz, ogg_int64_t unit_target)
goto notfound; goto notfound;
} }
if (offset_next > offset_end) { if (hit_eof || offset_next > offset_end) {
offset_next = offset_next =
oggz_scan_for_page (oggz, og, unit_target, 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;
...@@ -682,19 +708,21 @@ oggz_seek_set (OGGZ * oggz, ogg_int64_t unit_target) ...@@ -682,19 +708,21 @@ oggz_seek_set (OGGZ * oggz, ogg_int64_t unit_target)
unit_at = oggz_get_unit (oggz, serialno, granule_at); unit_at = oggz_get_unit (oggz, serialno, granule_at);
if (unit_at == unit_last_iter) looping = 1; if (unit_at == unit_last_iter) break;
#ifdef DEBUG #ifdef DEBUG
printf ("oggz_seek_set: [D] want u%lld, got page u%lld @%ld g%lld\n", printf ("oggz_seek_set: [D] want u%lld, got page u%lld @%ld g%lld\n",
unit_target, unit_at, offset_at, granule_at); unit_target, unit_at, offset_at, granule_at);
#endif #endif
if (!looping && unit_at < unit_target) { if (unit_at < unit_target) {
offset_begin = offset_at; offset_begin = offset_at;
unit_begin = unit_at; unit_begin = unit_at;
} else if (!looping && unit_at > unit_target) { if (unit_end == unit_begin) break;
} else if (unit_at > unit_target) {
offset_end = offset_at-1; offset_end = offset_at-1;
unit_end = unit_at; unit_end = unit_at;
if (unit_end == unit_begin) break;
} else { } else {
break; break;
} }
......
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