oggz_read.c 24.8 KB
Newer Older
andre's avatar
andre committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
/*
   Copyright (C) 2003 Commonwealth Scientific and Industrial Research
   Organisation (CSIRO) Australia

   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions
   are met:

   - Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.

   - Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.

   - Neither the name of CSIRO Australia nor the names of its
   contributors may be used to endorse or promote products derived from
   this software without specific prior written permission.

   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
   PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE ORGANISATION OR
   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/*
 * oggz_read.c
 *
 * Conrad Parker <conrad@annodex.net>
 */

#include "config.h"

#if OGGZ_CONFIG_READ

#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
48

49
#ifdef HAVE_UNISTD_H
andre's avatar
andre committed
50
#include <unistd.h>
51 52
#endif

andre's avatar
andre committed
53 54 55 56 57 58 59
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <time.h>

#include <ogg/ogg.h>

60
#include "oggz_compat.h"
andre's avatar
andre committed
61 62 63 64 65 66 67 68
#include "oggz_private.h"

/*#define DEBUG*/

/*#define DEBUG_BY_READING_PAGES*/

#define CHUNKSIZE 8500

conrad's avatar
conrad committed
69 70
#define oggz_off_t long

andre's avatar
andre committed
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
OGGZ *
oggz_read_init (OGGZ * oggz)
{
  OggzReader * reader = &oggz->x.reader;

  ogg_sync_init (&reader->ogg_sync);
  ogg_stream_init (&reader->ogg_stream, (int)-1);
  reader->current_serialno = -1;

  reader->read_packet = NULL;
  reader->read_user_data = NULL;

  reader->current_unit = 0;

  return oggz;
}

OGGZ *
oggz_read_close (OGGZ * oggz)
{
  OggzReader * reader = &oggz->x.reader;

  ogg_sync_clear (&reader->ogg_sync);

  return oggz;
}

int
oggz_set_read_callback (OGGZ * oggz, long serialno,
			OggzReadPacket read_packet, void * user_data)
{
  OggzReader * reader;
  oggz_stream_t * stream;

  if (oggz == NULL) return OGGZ_ERR_BAD_OGGZ;

  reader =  &oggz->x.reader;

  if (oggz->flags & OGGZ_WRITE) {
    return OGGZ_ERR_INVALID;
  }

  if (serialno == -1) {
    reader->read_packet = read_packet;
    reader->read_user_data = user_data;
  } else {
    stream = oggz_get_stream (oggz, serialno);
conrad's avatar
conrad committed
118
#if 0
andre's avatar
andre committed
119
    if (stream == NULL) return OGGZ_ERR_BAD_SERIALNO;
conrad's avatar
conrad committed
120 121 122 123
#else
    if (stream == NULL)
      stream = oggz_add_stream (oggz, serialno);
#endif
andre's avatar
andre committed
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141

    stream->read_packet = read_packet;
    stream->read_user_data = user_data;
  }

  return 0;
}

/*
 * oggz_get_next_page_7 (oggz, og, do_read)
 *
 * MODIFIED COPY OF CODE FROM BELOW SEEKING STUFF
 *
 * retrieves the next page.
 * returns >= 0 if found; return value is offset of page start
 * returns -1 on error
 * returns -2 if EOF was encountered
 */
conrad's avatar
conrad committed
142
static oggz_off_t
andre's avatar
andre committed
143 144 145 146 147 148 149
oggz_get_next_page_7 (OGGZ * oggz, ogg_page * og)
{
  OggzReader * reader = &oggz->x.reader;
#if _UNMODIFIED
  char * buffer;
#endif
  long bytes = 0, more;
conrad's avatar
conrad committed
150
  oggz_off_t page_offset = 0, ret;
andre's avatar
andre committed
151 152 153 154 155 156 157 158 159
  int found = 0;

  do {
    more = ogg_sync_pageseek (&reader->ogg_sync, og);

    if (more == 0) {
      page_offset = 0;
#if _UMMODIFIED_
      buffer = ogg_sync_buffer (&reader->ogg_sync, CHUNKSIZE);
160
      if ((bytes = oggz_io_read (oggz, buffer, CHUNKSIZE)) == 0) {
conrad's avatar
conrad committed
161 162 163 164
	if (ferror (oggz->file)) {
	  oggz_set_error (oggz, OGGZ_ERR_SYSTEM);
	  return -1;
	}
andre's avatar
andre committed
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
      }

      if (bytes == 0) {
	return -2;
      }

      ogg_sync_wrote(&reader->ogg_sync, bytes);
#else
      return -2;
#endif
    } else if (more < 0) {
#ifdef DEBUG
      printf ("get_next_page: skipped %ld bytes\n", -more);
#endif
      page_offset -= more;
    } else {
#ifdef DEBUG
      printf ("get_next_page: page has %ld bytes\n", more);
#endif
      found = 1;
    }

  } while (!found);

  /* Calculate the byte offset of the page which was found */
  if (bytes > 0) {
191
    oggz->offset = oggz_io_tell (oggz) - bytes + page_offset;
andre's avatar
andre committed
192 193 194 195 196 197 198 199 200 201
    ret = oggz->offset;
  } else {
    /* didn't need to do any reading -- accumulate the page_offset */
    ret = oggz->offset + page_offset;
    oggz->offset += page_offset + more;
  }

  return ret;
}

202
static int
andre's avatar
andre committed
203 204 205 206 207 208 209 210 211 212 213 214 215
oggz_read_sync (OGGZ * oggz)
{
  OggzReader * reader = &oggz->x.reader;
  long nread = 0;

  oggz_stream_t * stream;
  ogg_stream_state * os;
  ogg_packet * op;
  long serialno;

  ogg_packet packet;
  ogg_page og;

216 217
  int cb_ret = 0;

andre's avatar
andre committed
218 219 220 221 222
  /*os = &reader->ogg_stream;*/
  op = &packet;

  /* handle one packet.  Try to fetch it from current stream state */
  /* extract packets from page */
223
  while(cb_ret == 0){
andre's avatar
andre committed
224 225 226 227

    if (reader->current_serialno != -1) {
    /* process a packet if we can.  If the machine isn't loaded,
       neither is a page */
228
    while(cb_ret == 0) {
andre's avatar
andre committed
229 230 231 232 233 234 235 236 237 238 239
      ogg_int64_t granulepos;
      int result;

      serialno = reader->current_serialno;

      stream = oggz_get_stream (oggz, serialno);
      
      if (stream == NULL) {
	/* new stream ... check bos etc. */
	if ((stream = oggz_add_stream (oggz, serialno)) == NULL) {
	  /* error -- could not add stream */
240
	  return -7;
andre's avatar
andre committed
241 242 243
	}
      }
      os = &stream->ogg_stream;
244

andre's avatar
andre committed
245 246 247 248
      result = ogg_stream_packetout(os, op);

      if(result == -1) {
	/* hole in the data. */
249
	return -7;
andre's avatar
andre committed
250 251 252 253 254 255
      }

      if(result > 0){
	/* got a packet.  process it */
	granulepos = op->granulepos;

conrad's avatar
conrad committed
256 257 258 259
	if (!stream->metric && (oggz->flags & OGGZ_AUTO)) {
	  oggz_auto (oggz, op, serialno, NULL);
	}

andre's avatar
andre committed
260 261 262 263 264
	if ((oggz->metric || stream->metric) && granulepos != -1) {
	  reader->current_unit = oggz_get_unit (oggz, serialno, granulepos);
	}
#ifndef DEBUG_BY_READING_PAGES
	if (stream->read_packet) {
265 266
	  cb_ret =
	    stream->read_packet (oggz, op, serialno, stream->read_user_data);
andre's avatar
andre committed
267
	} else if (reader->read_packet) {
268 269
	  cb_ret =
	    reader->read_packet (oggz, op, serialno, reader->read_user_data);
andre's avatar
andre committed
270 271 272 273 274 275 276 277
	}
#endif /* DEBUG_BY_READING_PAGES */
      }
      else
	break;
    }
    }

278 279 280
    /* If we've got a stop already, don't read more data in */
    if (cb_ret != 0) return cb_ret;

andre's avatar
andre committed
281 282 283 284 285 286 287 288 289 290 291 292
    if(oggz_get_next_page_7 (oggz, &og) < 0)
      return -404; /* eof. leave unitialized */

    serialno = ogg_page_serialno (&og);
    reader->current_serialno = serialno; /* XXX: maybe not necessary */

    stream = oggz_get_stream (oggz, serialno);
      
    if (stream == NULL) {
      /* new stream ... check bos etc. */
      if ((stream = oggz_add_stream (oggz, serialno)) == NULL) {
	/* error -- could not add stream */
293
	return -7;
andre's avatar
andre committed
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
      }
    }
    os = &stream->ogg_stream;

#ifdef DEBUG_BY_READING_PAGES
    {
      ogg_packet op_debug;
      op_debug.packet = og.body;
      op_debug.bytes = og.body_len;
      op_debug.b_o_s = ogg_page_bos (&og);
      op_debug.e_o_s = ogg_page_eos (&og);
      op_debug.granulepos = ogg_page_granulepos (&og);
      op_debug.packetno = ogg_page_packets (&og);

      if (stream->read_packet) {
309 310
	cb_ret = stream->read_packet (oggz, &op_debug, serialno,
				      stream->read_user_data);
andre's avatar
andre committed
311
      } else if (reader->read_packet) {
312 313
	cb_ret = reader->read_packet (oggz, &op_debug, serialno,
				      reader->read_user_data);
andre's avatar
andre committed
314 315 316 317 318 319 320 321 322 323 324 325 326
      }
    }
#endif

#if 0
    /* bitrate tracking; add the header's bytes here, the body bytes
       are done by packet above */
    vf->bittrack+=og.header_len*8;
#endif

    ogg_stream_pagein(os, &og);
  }

327
  return cb_ret;
andre's avatar
andre committed
328 329 330 331 332 333 334 335
}

long
oggz_read (OGGZ * oggz, long n)
{
  OggzReader * reader;
  char * buffer;
  long bytes, bytes_read = 1, remaining = n, nread = 0;
336
  int cb_ret = 0;
andre's avatar
andre committed
337 338 339 340 341 342 343 344 345

  if (oggz == NULL) return OGGZ_ERR_BAD_OGGZ;

  if (oggz->flags & OGGZ_WRITE) {
    return OGGZ_ERR_INVALID;
  }

  reader = &oggz->x.reader;

346
  cb_ret = oggz_read_sync (oggz);
andre's avatar
andre committed
347

conrad's avatar
conrad committed
348
  /* If there's nothing to read yet, don't flag an error */
349 350
  if (reader->current_unit == 0 && cb_ret == -404) cb_ret = 0;

351
  while (cb_ret != -1 && cb_ret != 1 && bytes_read > 0 && remaining > 0) {
andre's avatar
andre committed
352 353
    bytes = MIN (remaining, 4096);
    buffer = ogg_sync_buffer (&reader->ogg_sync, bytes);
354
    if ((bytes_read = (long) oggz_io_read (oggz, buffer, bytes)) == 0) {
355

conrad's avatar
conrad committed
356 357 358
      if (ferror (oggz->file)) {
	return OGGZ_ERR_SYSTEM;
      }
andre's avatar
andre committed
359 360 361
    }

    ogg_sync_wrote (&reader->ogg_sync, bytes_read);
362

andre's avatar
andre committed
363 364 365
    remaining -= bytes_read;
    nread += bytes_read;

366
    cb_ret = oggz_read_sync (oggz);
andre's avatar
andre committed
367 368
  }

369 370
  if (cb_ret == -1) oggz_purge (oggz);

andre's avatar
andre committed
371 372 373 374 375 376 377 378 379 380
  return nread;
}

/* generic */
long
oggz_read_input (OGGZ * oggz, unsigned char * buf, long n)
{
  OggzReader * reader;
  char * buffer;
  long bytes, remaining = n, nread = 0;
381
  int cb_ret = 0;
andre's avatar
andre committed
382 383 384 385 386 387 388 389 390

  if (oggz == NULL) return OGGZ_ERR_BAD_OGGZ;

  if (oggz->flags & OGGZ_WRITE) {
    return OGGZ_ERR_INVALID;
  }

  reader = &oggz->x.reader;

391
  cb_ret = oggz_read_sync (oggz);
andre's avatar
andre committed
392

conrad's avatar
conrad committed
393 394 395
  /* If there's nothing to read yet, don't flag an error */
  if (reader->current_unit == 0 && cb_ret == -404) cb_ret = 0;

396
  while (cb_ret != -1 && cb_ret != 1 && /* !oggz->eos && */ remaining > 0) {
andre's avatar
andre committed
397 398 399 400 401 402 403 404 405
    bytes = MIN (remaining, 4096);
    buffer = ogg_sync_buffer (&reader->ogg_sync, bytes);
    memcpy (buffer, buf, bytes);
    ogg_sync_wrote (&reader->ogg_sync, bytes);

    buf += bytes;
    remaining -= bytes;
    nread += bytes;

406
    cb_ret = oggz_read_sync (oggz);    
andre's avatar
andre committed
407 408
  }

409 410
  if (cb_ret == -1) oggz_purge (oggz);

andre's avatar
andre committed
411 412 413
  return nread;
}

414
/* oggz_seek() (and oggz_purge()) related functions from here down */
andre's avatar
andre committed
415 416

/*
conrad's avatar
conrad committed
417
 * The typical usage is:
andre's avatar
andre committed
418 419 420 421
 *
 *   oggz_set_data_start (oggz, oggz_tell (oggz));
 */
int
conrad's avatar
conrad committed
422
oggz_set_data_start (OGGZ * oggz, oggz_off_t offset)
andre's avatar
andre committed
423 424 425 426 427 428 429 430 431 432
{
  if (oggz == NULL) return -1;

  if (offset < 0) return -1;

  oggz->offset_data_begin = offset;

  return 0;
}

conrad's avatar
conrad committed
433
static oggz_off_t
andre's avatar
andre committed
434 435
oggz_tell_raw (OGGZ * oggz)
{
conrad's avatar
conrad committed
436
  oggz_off_t offset_at;
andre's avatar
andre committed
437

438
  offset_at = oggz_io_tell (oggz);
andre's avatar
andre committed
439 440 441 442 443 444 445

  return offset_at;
}

/*
 * seeks and syncs
 */
conrad's avatar
conrad committed
446 447
static oggz_off_t
oggz_seek_raw (OGGZ * oggz, oggz_off_t offset, int whence)
andre's avatar
andre committed
448 449
{
  OggzReader * reader = &oggz->x.reader;
conrad's avatar
conrad committed
450
  oggz_off_t offset_at;
andre's avatar
andre committed
451

452
  if (oggz_io_seek (oggz, offset, whence) == -1) {
andre's avatar
andre committed
453 454 455
    return -1;
  }

456
  offset_at = oggz_io_tell (oggz);
conrad's avatar
conrad committed
457

andre's avatar
andre committed
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
  oggz->offset = offset_at;

  ogg_sync_reset (&reader->ogg_sync);

  return offset_at;
}

static int
oggz_stream_reset (void * data)
{
  oggz_stream_t * stream = (oggz_stream_t *) data;

  if (stream->ogg_stream.serialno != -1) {
    ogg_stream_reset (&stream->ogg_stream);
  }

  return 0;
}

477 478 479 480 481 482
static void
oggz_reset_streams (OGGZ * oggz)
{
  oggz_vector_foreach (oggz->streams, oggz_stream_reset);
}

andre's avatar
andre committed
483
static long
484
oggz_reset_seek (OGGZ * oggz, oggz_off_t offset, ogg_int64_t unit, int whence)
andre's avatar
andre committed
485 486 487
{
  OggzReader * reader = &oggz->x.reader;

conrad's avatar
conrad committed
488
  oggz_off_t offset_at;
andre's avatar
andre committed
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503

  offset_at = oggz_seek_raw (oggz, offset, whence);
  if (offset_at == -1) return -1;

  oggz->offset = offset_at;

#ifdef DEBUG
  printf ("reset to %ld\n", offset_at);
#endif

  if (unit != -1) reader->current_unit = unit;

  return offset_at;
}

504 505 506 507 508 509 510
static long
oggz_reset (OGGZ * oggz, oggz_off_t offset, ogg_int64_t unit, int whence)
{
  oggz_reset_streams (oggz);
  return oggz_reset_seek (oggz, offset, unit, whence);
}

511 512 513 514 515 516 517 518 519 520 521
int
oggz_purge (OGGZ * oggz)
{
  long reset_ret;

  if (oggz == NULL) return OGGZ_ERR_BAD_OGGZ;

  if (oggz->flags & OGGZ_WRITE) {
    return OGGZ_ERR_INVALID;
  }

522 523 524
  oggz_reset_streams (oggz);

  if (oggz->file && oggz_reset (oggz, oggz->offset, -1, SEEK_SET) < 0) {
525 526 527 528 529 530
    return OGGZ_ERR_SYSTEM;
  }

  return 0;
}

andre's avatar
andre committed
531 532 533 534 535 536 537 538
/*
 * oggz_get_next_page (oggz, og, do_read)
 *
 * retrieves the next page.
 * returns >= 0 if found; return value is offset of page start
 * returns -1 on error
 * returns -2 if EOF was encountered
 */
conrad's avatar
conrad committed
539
static oggz_off_t
andre's avatar
andre committed
540 541 542 543 544
oggz_get_next_page (OGGZ * oggz, ogg_page * og)
{
  OggzReader * reader = &oggz->x.reader;
  char * buffer;
  long bytes = 0, more;
conrad's avatar
conrad committed
545
  oggz_off_t page_offset = 0, ret;
andre's avatar
andre committed
546 547 548 549 550 551 552 553 554
  int found = 0;

  do {
    more = ogg_sync_pageseek (&reader->ogg_sync, og);

    if (more == 0) {
      page_offset = 0;

      buffer = ogg_sync_buffer (&reader->ogg_sync, CHUNKSIZE);
555
      if ((bytes = (long) oggz_io_read (oggz, buffer, CHUNKSIZE)) == 0) {
conrad's avatar
conrad committed
556 557 558 559
	if (ferror (oggz->file)) {
	  /*oggz_set_error (oggz, OGGZ_ERR_SYSTEM);*/
	  return -1;
	}
andre's avatar
andre committed
560 561
      }

conrad's avatar
conrad committed
562 563
      if (bytes == 0 || feof (oggz->file)) {
	clearerr (oggz->file);
andre's avatar
andre committed
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
	return -2;
      }

      ogg_sync_wrote(&reader->ogg_sync, bytes);

    } else if (more < 0) {
#ifdef DEBUG
      printf ("get_next_page: skipped %ld bytes\n", -more);
#endif
      page_offset -= more;
    } else {
#ifdef DEBUG
      printf ("get_next_page: page has %ld bytes\n", more);
#endif
      found = 1;
    }

  } while (!found);

  /* Calculate the byte offset of the page which was found */
  if (bytes > 0) {
    oggz->offset = oggz_tell_raw (oggz) - bytes + page_offset;
    ret = oggz->offset;
  } else {
    /* didn't need to do any reading -- accumulate the page_offset */
    ret = oggz->offset + page_offset;
    oggz->offset += page_offset + more;
  }

  return ret;
}

conrad's avatar
conrad committed
596
static oggz_off_t
andre's avatar
andre committed
597 598
oggz_get_next_start_page (OGGZ * oggz, ogg_page * og)
{
conrad's avatar
conrad committed
599
  oggz_off_t page_offset;
andre's avatar
andre committed
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
  int found = 0;

  while (!found) {
    page_offset = oggz_get_next_page (oggz, og);

    /* Return this value if one of the following conditions is met:
     *
     *   page_offset < 0     : error or EOF
     *   page_offset == 0    : start of stream
     *   !ogg_page_continued : start of page
     */
    if (page_offset <= 0 || !ogg_page_continued (og))
      found = 1;
  }

  return page_offset;
}

conrad's avatar
conrad committed
618
static oggz_off_t
andre's avatar
andre committed
619 620 621
oggz_get_prev_start_page (OGGZ * oggz, ogg_page * og,
			 ogg_int64_t * granule, long * serialno)
{
conrad's avatar
conrad committed
622 623
  oggz_off_t offset_at, offset_start;
  oggz_off_t page_offset, prev_offset = 0;
andre's avatar
andre committed
624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
  long granule_at = -1;

#if 0
  offset_at = oggz_tell_raw (oggz);
  if (offset_at == -1) return -1;
#else
  offset_at = oggz->offset;
#endif

  offset_start = offset_at;

  do {

    offset_start = offset_at - CHUNKSIZE;
    if (offset_start < 0) offset_start = 0;

    offset_start = oggz_seek_raw (oggz, offset_start, SEEK_SET);
    if (offset_start == -1) return -1;

#ifdef DEBUG

    printf ("[A] offset_at: @%ld\toffset_start: @%ld\n",
	    offset_at, offset_start);

    printf ("*** get_prev_start_page: seeked to %ld\n", offset_start);
#endif

    page_offset = 0;

    do {
      prev_offset = page_offset;

      page_offset = oggz_get_next_start_page (oggz, og);
      if (page_offset == -1) return -1;
      if (page_offset == -2) break;

      granule_at = (long)ogg_page_granulepos (og);

#ifdef DEBUG
      printf ("\tGOT page (%ld) @%ld\tat @%ld\n", granule_at,
	      page_offset, offset_at);
#endif

      /* Need to stash the granule and serialno of this page because og
       * will be overwritten by the time we realise this was the desired
       * prev page */
      if (page_offset >= 0 && page_offset < offset_at) {
	*granule = granule_at;
	*serialno = ogg_page_serialno (og);
      }

    } while (page_offset >= 0 && page_offset < offset_at);

#ifdef DEBUG
    printf ("[B] offset_at: @%ld\toffset_start: @%ld\n"
	    "prev_offset: @%ld\tpage_offset: @%ld\n",
	    offset_at, offset_start, prev_offset, page_offset);
#endif
    /* reset the file offset */
    offset_at = offset_start;

  } while (offset_at > 0 && prev_offset == 0);

  if (offset_at > 0)
    return prev_offset;
  else
    return -1;
}

conrad's avatar
conrad committed
693
static oggz_off_t
andre's avatar
andre committed
694
oggz_scan_for_page (OGGZ * oggz, ogg_page * og, ogg_int64_t unit_target,
conrad's avatar
conrad committed
695
		   oggz_off_t offset_begin, oggz_off_t offset_end)
andre's avatar
andre committed
696
{
conrad's avatar
conrad committed
697 698
  oggz_off_t offset_at, offset_next;
  oggz_off_t offset_prev = -1;
andre's avatar
andre committed
699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794
  ogg_int64_t granule_at;
  ogg_int64_t unit_at;
  long serialno;

#ifdef DEBUG
  printf (" SCANNING from %ld...", offset_begin);
#endif

  while (1) {
    offset_at = oggz_seek_raw (oggz, offset_begin, SEEK_SET);
    if (offset_at == -1) return -1;

#ifdef DEBUG
    printf (" scan @%ld\n", offset_at);
#endif

    offset_next = oggz_get_next_start_page (oggz, og);

    if (offset_next < 0) {
      return offset_next;
    }

    if (offset_next == 0 && offset_begin != 0) {
#ifdef DEBUG
      printf (" ... scanned past EOF\n");
#endif
      return -1;
    }
    if (offset_next > offset_end) {
#ifdef DEBUG
      printf (" ... scanned to page %ld\n", (long)ogg_page_granulepos (og));
#endif
      if (offset_prev != -1) {
	offset_at = oggz_seek_raw (oggz, offset_prev, SEEK_SET);
	if (offset_at == -1) return -1;

	offset_next = oggz_get_next_start_page (oggz, og);
	if (offset_next < 0) return offset_next;

	serialno = ogg_page_serialno (og);
	granule_at = ogg_page_granulepos (og);
	unit_at = oggz_get_unit (oggz, serialno, granule_at);

	return offset_at;
      } else {
	return -1;
      }
    }

    offset_at = offset_next;

    serialno = ogg_page_serialno (og);
    granule_at = ogg_page_granulepos (og);
    unit_at = oggz_get_unit (oggz, serialno, granule_at);

    if (unit_at < unit_target) {
#ifdef DEBUG
      printf (" scan: (%ld) < (%ld)\n", unit_at, unit_target);
#endif
      offset_prev = offset_next;
      offset_begin = offset_next+1;
    } else if (unit_at > unit_target) {
#ifdef DEBUG
      printf (" scan: (%ld) > (%ld)\n", unit_at, unit_target);
#endif
#if 0
      /* hole ? */
      offset_at = oggz_seek_raw (oggz, offset_begin, SEEK_SET);
      if (offset_at == -1) return -1;

      offset_next = oggz_get_next_start_page (oggz, og);
      if (offset_next < 0) return offset_next;

      serialno = ogg_page_serialno (og);
      granule_at = ogg_page_granulepos (og);
      unit_at = oggz_get_unit (oggz, serialno, granule_at);

      break;
#else
      return offset_at;
#endif
    } else if (unit_at == unit_target) {
#ifdef DEBUG
      printf (" scan: (%ld) == (%ld)\n", unit_at, unit_target);
#endif
      break;
    }
  }

  return offset_at;
}

static long
oggz_seek_set (OGGZ * oggz, ogg_int64_t unit_target)
{
  OggzReader * reader = &oggz->x.reader;
conrad's avatar
conrad committed
795
  int fd;
andre's avatar
andre committed
796
  struct stat statbuf;
conrad's avatar
conrad committed
797 798
  oggz_off_t offset_orig, offset_at, offset_guess;
  oggz_off_t offset_begin, offset_end, offset_next;
andre's avatar
andre committed
799 800 801 802 803 804 805 806 807 808 809 810 811 812 813
  ogg_int64_t granule_at;
  ogg_int64_t unit_at, unit_begin = 0, unit_end = -1;
  long serialno;
  double guess_ratio;
  ogg_page * og;

  if (oggz == NULL) {
    return -1;
  }

  if (unit_target > 0 && oggz->metric == NULL) {
    /* No metric defined */
    return -1;
  }

conrad's avatar
conrad committed
814 815 816 817 818 819 820
  if (oggz->file == NULL) {
    /*oggz_set_error (oggz, OGGZ_ERR_NOSEEK);*/
    return -1;
  }

  if ((fd = fileno (oggz->file)) == -1) {
    /*oggz_set_error (oggz, OGGZ_ERR_SYSTEM);*/
andre's avatar
andre committed
821 822 823
    return -1;
  }

conrad's avatar
conrad committed
824 825
  if (fstat (fd, &statbuf) == -1) {
    /*oggz_set_error (oggz, OGGZ_ERR_SYSTEM);*/
andre's avatar
andre committed
826 827 828
    return -1;
  }

829
#if 0
830
#ifndef WIN32
andre's avatar
andre committed
831 832 833
  if (S_ISREG(statbuf.st_mode) || S_ISLNK(statbuf.st_mode)) {
    offset_end = statbuf.st_size;
  } else {
conrad's avatar
conrad committed
834
    /*oggz_set_error (oggz, OGGZ_ERR_NOSEEK);*/
andre's avatar
andre committed
835 836
    return -1;
  }
837
#else
838 839 840 841 842 843 844 845 846
  if (statbuf.st_mode & S_IFREG) {
    offset_end = statbuf.st_size;
  } else {
    /*oggz_set_error (oggz, OGGZ_ERR_NOSEEK);*/
    return -1;
  }
#endif
#else
  if (oggz_stat_regular (statbuf.st_mode)) {
847 848 849 850 851 852
    offset_end = statbuf.st_size;
  } else {
    /*oggz_set_error (oggz, OGGZ_ERR_NOSEEK);*/
    return -1;
  }
#endif
andre's avatar
andre committed
853 854

  if (unit_target == reader->current_unit) {
855
    return (long)reader->current_unit;
andre's avatar
andre committed
856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907
  }

  if (unit_target == 0) {
    offset_at = oggz_reset (oggz, oggz->offset_data_begin, 0, SEEK_SET);
    if (offset_at == -1) return -1;
    return 0;
  }

  offset_at = oggz_tell_raw (oggz);
  if (offset_at == -1) return -1;

#if 0
  offset_orig = offset_at;
#else
  offset_orig = oggz->offset;
#endif

  offset_begin = 0;

  unit_at = reader->current_unit;
  unit_begin = 0;
  unit_end = -1;

  og = &oggz->current_page;

  while (1) {

#ifdef DEBUG
    printf ("oggz_read_seek (%ld): (%ld - %ld) [%ld - %ld]\t",
	    unit_target, unit_begin, unit_end, offset_begin, offset_end);
#endif

    if (unit_end == -1) {
      if (unit_at == unit_begin) {
#ifdef DEBUG
	printf ("*G1*");
#endif
	offset_guess = offset_begin + (offset_end - offset_begin)/2;
      } else {
#ifdef DEBUG
	printf ("*G2*");
#endif
	guess_ratio =
	  (double)(unit_target - unit_begin) /
	  (double)(unit_at - unit_begin);

#ifdef DEBUG
	printf ("\nguess_ration %f = (%ld - %ld) / (%ld - %ld)\n",
		guess_ratio, unit_target, unit_begin, unit_at, unit_begin);
#endif

	offset_guess = offset_begin +
conrad's avatar
conrad committed
908
	  (oggz_off_t)((offset_at - offset_begin) * guess_ratio);
andre's avatar
andre committed
909 910 911 912 913 914 915 916 917 918 919 920 921
      }
    } else if (unit_end <= unit_begin) {
#ifdef DEBUG
      printf ("unit_end <= unit_begin\n");
#endif
      break;
    } else {
#if 1
      guess_ratio =
	(double)(unit_target - unit_begin) /
	(double)(unit_end - unit_begin);

      offset_guess = offset_begin +
conrad's avatar
conrad committed
922
	(oggz_off_t)((offset_end - offset_begin) * guess_ratio);
andre's avatar
andre committed
923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001

      /*
      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);
    if (offset_at == -1) {
      goto notfound;
    }

    offset_next = oggz_get_next_start_page (oggz, og);

#ifdef DEBUG
    printf ("\n");
#endif

    if (offset_next < 0) {
      goto notfound;
    }

    if (offset_next > offset_end) {
      offset_next = oggz_scan_for_page (oggz, og, unit_target,
					offset_begin, offset_end);

      if (offset_next < 0) {
	goto notfound;
      }

      offset_at = offset_next;
      serialno = ogg_page_serialno (og);
      granule_at = ogg_page_granulepos (og);

      unit_at = oggz_get_unit (oggz, serialno, granule_at);

      goto found;
    }

    offset_at = offset_next;
    serialno = ogg_page_serialno (og);
    granule_at = ogg_page_granulepos (og);

    unit_at = oggz_get_unit (oggz, serialno, granule_at);

#ifdef DEBUG
    printf ("oggz_read_seek (%ld): got page (%ld) @%ld\n", unit_target,
	    unit_at, offset_at);
#endif

    if (unit_at < unit_target) {
      offset_begin = offset_at;
      unit_begin = unit_at;
    } else if (unit_at > unit_target) {
      offset_end = offset_at-1;
      unit_end = unit_at;
    } else {
      break;
    }


  }

 found:
#ifdef DEBUG
  printf ("FOUND (%ld)\n", unit_at);
#endif

  offset_at = oggz_reset (oggz, offset_at, unit_at, SEEK_SET);
  if (offset_at == -1) return -1;

1002
  return (long)reader->current_unit;
andre's avatar
andre committed
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016

 notfound:
#ifdef DEBUG
  printf ("NOT FOUND\n");
#endif

  oggz_reset (oggz, offset_orig, -1, SEEK_SET);

  return -1;
}

static long
oggz_seek_end (OGGZ * oggz, ogg_int64_t unit_offset)
{
conrad's avatar
conrad committed
1017
  oggz_off_t offset_orig, offset_at, offset_end;
andre's avatar
andre committed
1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
  ogg_int64_t granulepos;
  ogg_int64_t unit_end;
  long serialno;
  ogg_page * og;

  og = &oggz->current_page;

  offset_orig = oggz->offset;

  offset_at = oggz_seek_raw (oggz, 0, SEEK_END);
  if (offset_at == -1) return -1;

  offset_end = oggz_get_prev_start_page (oggz, og, &granulepos, &serialno);

  unit_end = oggz_get_unit (oggz, serialno, granulepos);

  if (offset_end < 0) {
    oggz_reset (oggz, offset_orig, -1, SEEK_SET);
    return -1;
  }
  
#ifdef DEBUG
  printf ("*** oggz_seek_end: found packet (%ld) at @%ld\n",
	  unit_end, offset_end);
#endif

  return oggz_seek_set (oggz, unit_end + unit_offset);
}

off_t
conrad's avatar
conrad committed
1048
oggz_seek (OGGZ * oggz, oggz_off_t offset, int whence)
andre's avatar
andre committed
1049 1050 1051 1052 1053 1054 1055 1056
{
  ogg_int64_t units = -1;

  if (oggz == NULL) return -1;

  if (oggz->flags & OGGZ_WRITE) {
    return -1;
  }
1057
  
andre's avatar
andre committed
1058 1059
  if (offset == 0) units = 0;

conrad's avatar
conrad committed
1060
  return (off_t)oggz_reset (oggz, offset, units, whence);
andre's avatar
andre committed
1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132
}

long
oggz_seek_units (OGGZ * oggz, ogg_int64_t units, int whence)
{
  OggzReader * reader = &oggz->x.reader;

  if (oggz == NULL) return -1;

  if (oggz->flags & OGGZ_WRITE) {
    return -1;
  }

  if (oggz->metric == NULL) {
    return -1;
  }

  switch (whence) {
  case SEEK_SET:
    return oggz_seek_set (oggz, units);
    break;
  case SEEK_CUR: 
    units += reader->current_unit;
    return oggz_seek_set (oggz, units);
    break;
  case SEEK_END:
    return oggz_seek_end (oggz, units);
    break;
  default:
    /*oggz_set_error (oggz, OGGZ_EINVALID);*/
    return -1;
    break;
  }
}

long
oggz_seek_byorder (OGGZ * oggz, void * target)
{
  return -1;
}

long
oggz_seek_packets (OGGZ * oggz, long serialno, long packets, int whence)
{
  return -1;
}

#else /* OGGZ_CONFIG_READ */

#include <ogg/ogg.h>
#include "oggz_private.h"

int
oggz_set_read_callback (OGGZ * oggz, long serialno,
			OggzReadPacket read_packet, void * user_data)
{
  return OGGZ_ERR_DISABLED;
}

long
oggz_read (OGGZ * oggz, long n)
{
  return OGGZ_ERR_DISABLED;
}

long
oggz_read_input (OGGZ * oggz, unsigned char * buf, long n)
{
  return OGGZ_ERR_DISABLED;
}

off_t
conrad's avatar
conrad committed
1133
oggz_seek (OGGZ * oggz, oggz_off_t offset, int whence)
andre's avatar
andre committed
1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156
{
  return OGGZ_ERR_DISABLED;
}

long
oggz_seek_units (OGGZ * oggz, ogg_int64_t units, int whence)
{
  return OGGZ_ERR_DISABLED;
}

long
oggz_seek_byorder (OGGZ * oggz, void * target)
{
  return OGGZ_ERR_DISABLED;
}

long
oggz_seek_packets (OGGZ * oggz, long serialno, long packets, int whence)
{
  return OGGZ_ERR_DISABLED;
}

#endif