oggz_read.c 28.7 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
#include "oggz_private.h"

/*#define DEBUG*/
64
/* #define DEBUG_VERBOSE */
andre's avatar
andre committed
65

66
#define CHUNKSIZE 65536
andre's avatar
andre committed
67

68
#define OGGZ_READ_EMPTY (-404)
69

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

andre's avatar
andre committed
72 73 74 75 76 77 78 79 80 81 82 83
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;

conrad's avatar
conrad committed
84 85 86
  reader->read_page = NULL;
  reader->read_page_user_data = NULL;

andre's avatar
andre committed
87 88 89 90 91 92 93 94 95 96
  reader->current_unit = 0;

  return oggz;
}

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

97
  ogg_stream_clear (&reader->ogg_stream);
andre's avatar
andre committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
  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
123
#if 0
andre's avatar
andre committed
124
    if (stream == NULL) return OGGZ_ERR_BAD_SERIALNO;
conrad's avatar
conrad committed
125 126 127 128
#else
    if (stream == NULL)
      stream = oggz_add_stream (oggz, serialno);
#endif
andre's avatar
andre committed
129 130 131 132 133 134 135 136

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

  return 0;
}

137
int
138 139
oggz_set_read_page (OGGZ * oggz, long serialno, OggzReadPage read_page,
		    void * user_data)
140 141
{
  OggzReader * reader;
142
  oggz_stream_t * stream;
143 144 145 146 147 148 149 150 151

  if (oggz == NULL) return OGGZ_ERR_BAD_OGGZ;

  reader =  &oggz->x.reader;

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

152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
  if (serialno == -1) {
    reader->read_page = read_page;
    reader->read_page_user_data = user_data;
  } else {
    stream = oggz_get_stream (oggz, serialno);
#if 0
    if (stream == NULL) return OGGZ_ERR_BAD_SERIALNO;
#else
    if (stream == NULL)
      stream = oggz_add_stream (oggz, serialno);
#endif

    stream->read_page = read_page;
    stream->read_page_user_data = user_data;
  }
167 168 169 170

  return 0;
}

andre's avatar
andre committed
171 172 173 174 175 176 177 178 179 180
/*
 * 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
181
static oggz_off_t
andre's avatar
andre committed
182 183 184 185 186 187 188
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
189
  oggz_off_t page_offset = 0, ret;
andre's avatar
andre committed
190 191 192 193 194 195 196 197 198
  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);
199
      if ((bytes = oggz_io_read (oggz, buffer, CHUNKSIZE)) == 0) {
200
#if 0
conrad's avatar
conrad committed
201 202 203 204
	if (ferror (oggz->file)) {
	  oggz_set_error (oggz, OGGZ_ERR_SYSTEM);
	  return -1;
	}
205
#endif
andre's avatar
andre committed
206 207 208 209 210 211 212 213 214 215 216
      }

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

      ogg_sync_wrote(&reader->ogg_sync, bytes);
#else
      return -2;
#endif
    } else if (more < 0) {
217
#ifdef DEBUG_VERBOSE
andre's avatar
andre committed
218 219 220 221
      printf ("get_next_page: skipped %ld bytes\n", -more);
#endif
      page_offset -= more;
    } else {
222
#ifdef DEBUG_VERBOSE
andre's avatar
andre committed
223 224 225 226 227 228 229 230 231
      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) {
232
    oggz->offset = oggz_io_tell (oggz) - bytes + page_offset;
andre's avatar
andre committed
233 234 235 236 237 238 239 240 241 242
    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;
}

243
static int
andre's avatar
andre committed
244 245 246 247 248 249 250 251 252 253 254 255
oggz_read_sync (OGGZ * oggz)
{
  OggzReader * reader = &oggz->x.reader;

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

  ogg_packet packet;
  ogg_page og;

256 257
  int cb_ret = 0;

andre's avatar
andre committed
258 259 260 261 262
  /*os = &reader->ogg_stream;*/
  op = &packet;

  /* handle one packet.  Try to fetch it from current stream state */
  /* extract packets from page */
263
  while(cb_ret == 0){
andre's avatar
andre committed
264 265

    if (reader->current_serialno != -1) {
266 267 268 269 270
    /* process a packet if we can.  If the machine isn't loaded,
       neither is a page */
    while(cb_ret == 0) {
      ogg_int64_t granulepos;
      int result;
andre's avatar
andre committed
271

272
      serialno = reader->current_serialno;
andre's avatar
andre committed
273

274
      stream = oggz_get_stream (oggz, serialno);
275

276 277 278 279 280
      if (stream == NULL) {
	/* new stream ... check bos etc. */
	if ((stream = oggz_add_stream (oggz, serialno)) == NULL) {
	  /* error -- could not add stream */
	  return -7;
andre's avatar
andre committed
281
	}
282 283
      }
      os = &stream->ogg_stream;
284

285
      result = ogg_stream_packetout(os, op);
andre's avatar
andre committed
286

287 288 289 290
      if(result == -1) {
	/* hole in the data. */
	return -7;
      }
andre's avatar
andre committed
291

292 293 294
      if(result > 0){
	/* got a packet.  process it */
	granulepos = op->granulepos;
andre's avatar
andre committed
295

296 297 298
	if (!stream->metric && (oggz->flags & OGGZ_AUTO)) {
	  oggz_auto (oggz, op, serialno, NULL);
	}
conrad's avatar
conrad committed
299

300
	/* set unit on last packet of page */
301 302 303
	if ((oggz->metric || stream->metric) && granulepos != -1) {
	  reader->current_unit = oggz_get_unit (oggz, serialno, granulepos);
	}
304

305 306 307 308 309 310
	if (stream->read_packet) {
	  cb_ret =
	    stream->read_packet (oggz, op, serialno, stream->read_user_data);
	} else if (reader->read_packet) {
	  cb_ret =
	    reader->read_packet (oggz, op, serialno, reader->read_user_data);
311
	}
andre's avatar
andre committed
312
      }
313 314 315
      else
	break;
    }
andre's avatar
andre committed
316
    }
317

318
    /* If we've got a stop already, don't read more data in */
319
    if (cb_ret == OGGZ_STOP_OK || cb_ret == OGGZ_STOP_ERR) return cb_ret;
andre's avatar
andre committed
320

321
    if(oggz_get_next_page_7 (oggz, &og) < 0)
322
      return OGGZ_READ_EMPTY; /* eof. leave uninitialized */
andre's avatar
andre committed
323

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

    stream = oggz_get_stream (oggz, serialno);
328

329 330 331 332 333
    if (stream == NULL) {
      /* new stream ... check bos etc. */
      if ((stream = oggz_add_stream (oggz, serialno)) == NULL) {
	/* error -- could not add stream */
	return -7;
andre's avatar
andre committed
334
      }
335 336
    }
    os = &stream->ogg_stream;
andre's avatar
andre committed
337

338
    {
339 340 341
      ogg_int64_t granulepos;

      granulepos = ogg_page_granulepos (&og);
342

343 344 345 346
      if ((oggz->metric || stream->metric) && granulepos != -1) {
	reader->current_unit = oggz_get_unit (oggz, serialno, granulepos);
      } else if (granulepos == 0) {
	reader->current_unit = 0;
andre's avatar
andre committed
347
      }
348
    }
349

350 351 352 353 354 355
    if (stream->read_page) {
      cb_ret =
	stream->read_page (oggz, &og, serialno, stream->read_page_user_data);
    } else if (reader->read_page) {
      cb_ret = reader->read_page (oggz, &og, serialno,
				  reader->read_page_user_data);
356
    }
andre's avatar
andre committed
357 358

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

364
    ogg_stream_pagein(os, &og);
andre's avatar
andre committed
365 366
  }

367
  return cb_ret;
andre's avatar
andre committed
368 369 370 371 372 373 374 375
}

long
oggz_read (OGGZ * oggz, long n)
{
  OggzReader * reader;
  char * buffer;
  long bytes, bytes_read = 1, remaining = n, nread = 0;
376
  int cb_ret = 0;
andre's avatar
andre committed
377 378 379 380 381 382 383 384 385

  if (oggz == NULL) return OGGZ_ERR_BAD_OGGZ;

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

  reader = &oggz->x.reader;

386
  cb_ret = oggz_read_sync (oggz);
andre's avatar
andre committed
387

388
#if 0
389
  if (cb_ret == OGGZ_READ_EMPTY) {
390 391
    /* If there's nothing to read yet, don't return 0 (eof) */
    if (reader->current_unit == 0) cb_ret = 0;
392 393 394 395 396 397 398
    else {
#if 0
      printf ("oggz_read: EMPTY, current_unit %ld != 0\n",
	      reader->current_unit);
      return 0;
#endif
    }
399
  }
400
#endif
401

402 403
  while (cb_ret != OGGZ_STOP_ERR && cb_ret != OGGZ_STOP_OK &&
	 bytes_read > 0 && remaining > 0) {
404
    bytes = MIN (remaining, CHUNKSIZE);
andre's avatar
andre committed
405
    buffer = ogg_sync_buffer (&reader->ogg_sync, bytes);
406
    if ((bytes_read = (long) oggz_io_read (oggz, buffer, bytes)) == 0) {
407 408 409 410
      /* schyeah! */
    }
    if (bytes_read == OGGZ_ERR_SYSTEM) {
      return OGGZ_ERR_SYSTEM;
andre's avatar
andre committed
411 412
    }

conrad's avatar
 
conrad committed
413 414 415 416 417 418 419 420
    if (bytes_read > 0) {
      ogg_sync_wrote (&reader->ogg_sync, bytes_read);
      
      remaining -= bytes_read;
      nread += bytes_read;
      
      cb_ret = oggz_read_sync (oggz);
    }
andre's avatar
andre committed
421 422
  }

423
  if (cb_ret == OGGZ_STOP_ERR) oggz_purge (oggz);
424

425
  /* Don't return 0 unless it's actually an EOF condition */
426
  if (nread == 0) {
conrad's avatar
conrad committed
427 428 429 430 431 432 433
    switch (bytes_read) {
    case OGGZ_ERR_IO_AGAIN:
    case OGGZ_ERR_SYSTEM:
      return bytes_read; break;
    default: break;
    }

434
    switch (cb_ret) {
435 436 437 438 439
    case OGGZ_CONTINUE: case OGGZ_READ_EMPTY: 
#ifdef DEBUG
      printf ("oggz_read: nread==0, cb_ret==%d, returning 0\n", cb_ret);
#endif
      return 0; break;
440 441 442 443
    case OGGZ_STOP_ERR: return OGGZ_ERR_READ_STOP_ERR; break;
    case OGGZ_STOP_OK: default: return OGGZ_ERR_READ_STOP_OK; break;
    }
  }
444

andre's avatar
andre committed
445 446 447 448 449 450 451 452 453 454
  return nread;
}

/* generic */
long
oggz_read_input (OGGZ * oggz, unsigned char * buf, long n)
{
  OggzReader * reader;
  char * buffer;
  long bytes, remaining = n, nread = 0;
455
  int cb_ret = 0;
andre's avatar
andre committed
456 457 458 459 460 461 462 463 464

  if (oggz == NULL) return OGGZ_ERR_BAD_OGGZ;

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

  reader = &oggz->x.reader;

465 466
  cb_ret = oggz_read_sync (oggz);

467
#if 0
468
  if (cb_ret == OGGZ_READ_EMPTY) {
469 470 471 472
    /* If there's nothing to read yet, don't return 0 (eof) */
    if (reader->current_unit == 0) cb_ret = 0;
    else return 0;
  }
473
#endif
conrad's avatar
conrad committed
474

475
  while (cb_ret != -1 && cb_ret != 1 && /* !oggz->eos && */ remaining > 0) {
andre's avatar
andre committed
476 477 478 479 480 481 482 483 484
    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;

485
    cb_ret = oggz_read_sync (oggz);
andre's avatar
andre committed
486 487
  }

488
  if (cb_ret == OGGZ_STOP_ERR) oggz_purge (oggz);
489

490
  /* Don't return 0 unless it's actually an EOF condition */
491 492
  if (nread == 0) {
    switch (cb_ret) {
493 494 495 496 497 498 499
    case OGGZ_CONTINUE: return 0; break;
    case OGGZ_READ_EMPTY:
#ifdef DEBUG
      printf ("oggz_read_input: OUT EMPTY\n");
#endif
      return OGGZ_ERR_READ_STOP_OK;
      break;
500 501 502 503
    case OGGZ_STOP_ERR: return OGGZ_ERR_READ_STOP_ERR; break;
    case OGGZ_STOP_OK: default: return OGGZ_ERR_READ_STOP_OK; break;
    }
  }
504

andre's avatar
andre committed
505 506 507
  return nread;
}

508
/* oggz_seek() (and oggz_purge()) related functions from here down */
andre's avatar
andre committed
509 510

/*
conrad's avatar
conrad committed
511
 * The typical usage is:
andre's avatar
andre committed
512 513 514 515
 *
 *   oggz_set_data_start (oggz, oggz_tell (oggz));
 */
int
conrad's avatar
conrad committed
516
oggz_set_data_start (OGGZ * oggz, oggz_off_t offset)
andre's avatar
andre committed
517 518 519 520 521 522 523 524 525 526
{
  if (oggz == NULL) return -1;

  if (offset < 0) return -1;

  oggz->offset_data_begin = offset;

  return 0;
}

conrad's avatar
conrad committed
527
static oggz_off_t
andre's avatar
andre committed
528 529
oggz_tell_raw (OGGZ * oggz)
{
conrad's avatar
conrad committed
530
  oggz_off_t offset_at;
andre's avatar
andre committed
531

532
  offset_at = oggz_io_tell (oggz);
andre's avatar
andre committed
533 534 535 536 537 538 539

  return offset_at;
}

/*
 * seeks and syncs
 */
conrad's avatar
conrad committed
540 541
static oggz_off_t
oggz_seek_raw (OGGZ * oggz, oggz_off_t offset, int whence)
andre's avatar
andre committed
542 543
{
  OggzReader * reader = &oggz->x.reader;
conrad's avatar
conrad committed
544
  oggz_off_t offset_at;
andre's avatar
andre committed
545

546
  if (oggz_io_seek (oggz, offset, whence) == -1) {
andre's avatar
andre committed
547 548 549
    return -1;
  }

550
  offset_at = oggz_io_tell (oggz);
conrad's avatar
conrad committed
551

andre's avatar
andre committed
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
  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;
}

571 572 573 574 575 576
static void
oggz_reset_streams (OGGZ * oggz)
{
  oggz_vector_foreach (oggz->streams, oggz_stream_reset);
}

andre's avatar
andre committed
577
static long
578
oggz_reset_seek (OGGZ * oggz, oggz_off_t offset, ogg_int64_t unit, int whence)
andre's avatar
andre committed
579 580 581
{
  OggzReader * reader = &oggz->x.reader;

conrad's avatar
conrad committed
582
  oggz_off_t offset_at;
andre's avatar
andre committed
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597

  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;
}

598 599 600 601 602 603 604
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);
}

605 606 607 608 609 610 611 612 613
int
oggz_purge (OGGZ * oggz)
{
  if (oggz == NULL) return OGGZ_ERR_BAD_OGGZ;

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

614 615 616
  oggz_reset_streams (oggz);

  if (oggz->file && oggz_reset (oggz, oggz->offset, -1, SEEK_SET) < 0) {
617 618 619 620 621 622
    return OGGZ_ERR_SYSTEM;
  }

  return 0;
}

andre's avatar
andre committed
623 624 625 626 627 628 629 630
/*
 * 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
631
static oggz_off_t
andre's avatar
andre committed
632 633 634 635 636
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
637
  oggz_off_t page_offset = 0, ret;
andre's avatar
andre committed
638 639 640 641 642 643 644 645 646
  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);
647
      if ((bytes = (long) oggz_io_read (oggz, buffer, CHUNKSIZE)) == 0) {
648 649
	/* schyeah! */
      }
650
      if (bytes == OGGZ_ERR_SYSTEM) {
conrad's avatar
conrad committed
651 652
	  /*oggz_set_error (oggz, OGGZ_ERR_SYSTEM);*/
	  return -1;
andre's avatar
andre committed
653 654
      }

655
      if (bytes == 0) {
656
#ifdef DEBUG_VERBOSE
conrad's avatar
conrad committed
657 658
	printf ("get_next_page: bytes == 0, returning -2\n");
#endif
659 660
	return -2;
      } else if (oggz->file && feof (oggz->file)) {
661
#ifdef DEBUG_VERBOSE
conrad's avatar
conrad committed
662 663
	printf ("get_next_page: feof (oggz->file), returning -2\n");
#endif
664 665 666 667
	clearerr (oggz->file);
	return -2;
      }

andre's avatar
andre committed
668 669 670
      ogg_sync_wrote(&reader->ogg_sync, bytes);

    } else if (more < 0) {
671
#ifdef DEBUG_VERBOSE
andre's avatar
andre committed
672 673 674 675
      printf ("get_next_page: skipped %ld bytes\n", -more);
#endif
      page_offset -= more;
    } else {
676
#ifdef DEBUG_VERBOSE
andre's avatar
andre committed
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696
      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
697
static oggz_off_t
andre's avatar
andre committed
698 699
oggz_get_next_start_page (OGGZ * oggz, ogg_page * og)
{
conrad's avatar
conrad committed
700
  oggz_off_t page_offset;
andre's avatar
andre committed
701 702
  int found = 0;

colinw's avatar
colinw committed
703
  do {
andre's avatar
andre committed
704 705 706 707 708 709
    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
conrad's avatar
conrad committed
710 711
     *   !ogg_page_continued : page contains start of a packet
     *   ogg_page_packets > 1: page contains start of a packet
andre's avatar
andre committed
712
     */
conrad's avatar
conrad committed
713 714
    if (page_offset <= 0 || !ogg_page_continued (og) ||
	ogg_page_packets (og) > 1)
andre's avatar
andre committed
715 716
      found = 1;
  }
colinw's avatar
colinw committed
717
  while (!found);
andre's avatar
andre committed
718 719 720 721

  return page_offset;
}

conrad's avatar
conrad committed
722
static oggz_off_t
andre's avatar
andre committed
723 724 725
oggz_get_prev_start_page (OGGZ * oggz, ogg_page * og,
			 ogg_int64_t * granule, long * serialno)
{
conrad's avatar
conrad committed
726 727
  oggz_off_t offset_at, offset_start;
  oggz_off_t page_offset, prev_offset = 0;
conrad's avatar
conrad committed
728
  ogg_int64_t unit_at;
andre's avatar
andre committed
729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749
  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

conrad's avatar
conrad committed
750
    printf ("get_prev_start_page: [A] offset_at: @%ld\toffset_start: @%ld\n",
andre's avatar
andre committed
751 752
	    offset_at, offset_start);

conrad's avatar
conrad committed
753
    printf ("get_prev_start_page: seeked to %ld\n", offset_start);
andre's avatar
andre committed
754 755 756 757 758 759 760 761 762
#endif

    page_offset = 0;

    do {
      prev_offset = page_offset;

      page_offset = oggz_get_next_start_page (oggz, og);
      if (page_offset == -1) return -1;
conrad's avatar
conrad committed
763 764 765 766 767 768
      if (page_offset == -2) {
#ifdef DEBUG
	printf ("*** get_prev_start_page: page_offset = -2\n");
#endif
	break;
      }
andre's avatar
andre committed
769 770 771 772

      granule_at = (long)ogg_page_granulepos (og);

#ifdef DEBUG
conrad's avatar
conrad committed
773 774
      printf ("get_prev_start_page: GOT page (%ld) @%ld\tat @%ld\n",
	      granule_at, page_offset, offset_at);
andre's avatar
andre committed
775 776 777 778 779 780 781 782 783 784 785 786 787
#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
conrad's avatar
conrad committed
788
    printf ("get_prev_start_page: [B] offset_at: @%ld\toffset_start: @%ld\n"
andre's avatar
andre committed
789 790 791 792 793 794 795 796
	    "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);

conrad's avatar
conrad committed
797 798
  unit_at = oggz_get_unit (oggz, *serialno, *granule);
  offset_at = oggz_reset (oggz, prev_offset, unit_at, SEEK_SET);
799 800 801 802 803 804 805

#ifdef DEBUG
    printf ("get_prev_start_page: [C] offset_at: @%ld\t"
	    "prev_offset: @%ld\tunit_at: %ld\n",
	    offset_at, prev_offset, unit_at);
#endif

conrad's avatar
conrad committed
806 807
  if (offset_at == -1) return -1;

andre's avatar
andre committed
808 809 810 811 812 813
  if (offset_at > 0)
    return prev_offset;
  else
    return -1;
}

conrad's avatar
conrad committed
814
static oggz_off_t
andre's avatar
andre committed
815
oggz_scan_for_page (OGGZ * oggz, ogg_page * og, ogg_int64_t unit_target,
conrad's avatar
conrad committed
816
		   oggz_off_t offset_begin, oggz_off_t offset_end)
andre's avatar
andre committed
817
{
conrad's avatar
conrad committed
818 819
  oggz_off_t offset_at, offset_next;
  oggz_off_t offset_prev = -1;
andre's avatar
andre committed
820 821 822 823 824 825 826 827
  ogg_int64_t granule_at;
  ogg_int64_t unit_at;
  long serialno;

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

colinw's avatar
colinw committed
828
  for ( ; ; ) {
andre's avatar
andre committed
829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876
    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
877
      printf (" scan: (%lld) < (%lld)\n", unit_at, unit_target);
andre's avatar
andre committed
878 879 880 881 882
#endif
      offset_prev = offset_next;
      offset_begin = offset_next+1;
    } else if (unit_at > unit_target) {
#ifdef DEBUG
883
      printf (" scan: (%lld) > (%lld)\n", unit_at, unit_target);
andre's avatar
andre committed
884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902
#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
903
      printf (" scan: (%lld) == (%lld)\n", unit_at, unit_target);
andre's avatar
andre committed
904 905 906 907 908 909 910 911
#endif
      break;
    }
  }

  return offset_at;
}

912 913
#define GUESS_MULTIPLIER (1<<16)

914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937
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;
}

conrad's avatar
conrad committed
938
static ogg_int64_t
andre's avatar
andre committed
939 940 941
oggz_seek_set (OGGZ * oggz, ogg_int64_t unit_target)
{
  OggzReader * reader = &oggz->x.reader;
conrad's avatar
conrad committed
942
  int fd;
andre's avatar
andre committed
943
  struct stat statbuf;
conrad's avatar
conrad committed
944
  oggz_off_t offset_orig, offset_at, offset_guess;
945
  oggz_off_t offset_begin, offset_end = -1, offset_next;
andre's avatar
andre committed
946 947 948 949 950 951 952 953 954
  ogg_int64_t granule_at;
  ogg_int64_t unit_at, unit_begin = 0, unit_end = -1;
  long serialno;
  ogg_page * og;

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

955
  if (unit_target > 0 && !oggz_has_metrics (oggz)) {
956 957 958
#ifdef DEBUG
    printf ("oggz_seek_set: No metric defined, FAIL\n");
#endif
andre's avatar
andre committed
959 960 961
    return -1;
  }

962
  if (oggz->file != NULL) {
963 964 965 966
    if ((fd = fileno (oggz->file)) == -1) {
      /*oggz_set_error (oggz, OGGZ_ERR_SYSTEM);*/
      return -1;
    }
andre's avatar
andre committed
967

968 969 970 971
    if (fstat (fd, &statbuf) == -1) {
      /*oggz_set_error (oggz, OGGZ_ERR_SYSTEM);*/
      return -1;
    }
andre's avatar
andre committed
972

973 974 975 976
    if (oggz_stat_regular (statbuf.st_mode)) {
      offset_end = statbuf.st_size;
    } else {
      /*oggz_set_error (oggz, OGGZ_ERR_NOSEEK);*/
977 978 979

      /* XXX: should be able to just carry on and guess, as per io */
      /*return -1;*/
980
    }
981
  } else {
982 983 984
    oggz_off_t offset_save;

    if (oggz->io == NULL || oggz->io->seek == NULL) {
985 986
      /* No file, and no io seek method */
      return -1;
987 988 989 990 991 992 993 994 995 996 997
    }

    /* Get the offset of the end by querying the io seek method */
    offset_save = oggz_io_tell (oggz);
    if (oggz_io_seek (oggz, 0, SEEK_END) == -1) {
      return -1;
    }
    offset_end = oggz_io_tell (oggz);
    if (oggz_io_seek (oggz, offset_save, SEEK_SET) == -1) {
      return -1; /* fubar */
    }
998
  }
andre's avatar
andre committed
999 1000

  if (unit_target == reader->current_unit) {
1001
    return (long)reader->current_unit;
andre's avatar
andre committed
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
  }

  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;

  offset_orig = oggz->offset;

  offset_begin = 0;

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

  og = &oggz->current_page;

colinw's avatar
colinw committed
1023
  for ( ; ; ) {
andre's avatar
andre committed
1024 1025

#ifdef DEBUG
1026
    printf ("oggz_seek_set: [A] want u%ld: (u%ld - u%ld) [@%ld - @%ld]\n",
andre's avatar
andre committed
1027 1028 1029 1030 1031 1032 1033
	    unit_target, unit_begin, unit_end, offset_begin, offset_end);
#endif

    if (unit_end == -1) {
      if (unit_at == unit_begin) {
	offset_guess = offset_begin + (offset_end - offset_begin)/2;
      } else {
1034 1035
	offset_guess = guess (unit_at, unit_target, unit_begin, unit_end,
			      offset_begin, offset_at);
andre's avatar
andre committed
1036 1037 1038
      }
    } else if (unit_end <= unit_begin) {
#ifdef DEBUG
1039
      printf ("oggz_seek_set: unit_end <= unit_begin (ERROR)\n");
andre's avatar
andre committed
1040 1041 1042
#endif
      break;
    } else {
1043 1044
      offset_guess = guess (unit_at, unit_target, unit_begin, unit_end,
			    offset_begin, offset_end);
andre's avatar
andre committed
1045 1046 1047 1048 1049 1050 1051 1052 1053
    }

    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);

1054 1055 1056
    if (unit_end == -1 && offset_next == -2) { /* reached eof, backtrack */
      offset_next = oggz_get_prev_start_page (oggz, og, &granule_at,
					      &serialno);
1057 1058 1059 1060 1061 1062 1063 1064 1065
#ifdef DEBUG
      printf ("oggz_seek_set: [C] offset_next @%ld, g%lld, (s%ld)\n",
	      offset_next, granule_at, serialno);
      printf ("oggz_seek_set: [c] u%lld\n",
	      oggz_get_unit (oggz, serialno, granule_at));
#endif
    } else {
      serialno = ogg_page_serialno (og);
      granule_at = ogg_page_granulepos (og);
1066 1067
    }

andre's avatar
andre committed
1068 1069 1070 1071
    if (offset_next < 0) {
      goto notfound;
    }

1072
    if (offset_next < offset_end) {
1073 1074 1075
      offset_next =
	oggz_scan_for_page (oggz, og, unit_target, offset_begin, offset_end);
      if (offset_next < 0) goto notfound;
andre's avatar
andre committed
1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090

      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;

    unit_at = oggz_get_unit (oggz, serialno, granule_at);

#ifdef DEBUG
1091 1092
    printf ("oggz_seek_set: [D] want u%lld, got page u%lld @%ld g%lld\n",
	    unit_target, unit_at, offset_at, granule_at);
andre's avatar
andre committed
1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113
#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;

1114
  return (long)reader->current_unit;
andre's avatar
andre committed
1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125

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

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

  return -1;
}

conrad's avatar
conrad committed
1126
static ogg_int64_t
andre's avatar
andre committed
1127 1128
oggz_seek_end (OGGZ * oggz, ogg_int64_t unit_offset)
{
conrad's avatar
conrad committed
1129
  oggz_off_t offset_orig, offset_at, offset_end;
andre's avatar
andre committed
1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149
  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;
  }
1150

andre's avatar
andre committed
1151
#ifdef DEBUG
conrad's avatar
conrad committed
1152 1153
  printf ("*** oggz_seek_end: found packet (%ld) at @%ld [%ld]\n",
	  unit_end, offset_end, granulepos);
andre's avatar
andre committed
1154 1155 1156 1157 1158 1159
#endif

  return oggz_seek_set (oggz, unit_end + unit_offset);
}

off_t
conrad's avatar
conrad committed
1160
oggz_seek (OGGZ * oggz, oggz_off_t offset, int whence)
andre's avatar
andre committed
1161 1162 1163 1164 1165 1166 1167 1168
{
  ogg_int64_t units = -1;

  if (oggz == NULL) return -1;

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

andre's avatar
andre committed
1170 1171
  if (offset == 0) units = 0;

conrad's avatar
conrad committed
1172
  return (off_t)oggz_reset (oggz, offset, units, whence);
andre's avatar
andre committed
1173 1174
}

conrad's avatar
conrad committed
1175
ogg_int64_t
andre's avatar
andre committed
1176 1177 1178 1179
oggz_seek_units (OGGZ * oggz, ogg_int64_t units, int whence)
{
  OggzReader * reader = &oggz->x.reader;

1180 1181 1182 1183 1184 1185 1186
  if (oggz == NULL) {
#ifdef DEBUG
    printf ("oggz_seek_units: oggz NULL, FAIL\n");
#endif
    return -1;
  }

andre's avatar
andre committed
1187 1188

  if (oggz->flags & OGGZ_WRITE) {
1189 1190 1191
#ifdef DEBUG
    printf ("oggz_seek_units: is OGGZ_WRITE, FAIL\n");
#endif
andre's avatar
andre committed
1192 1193 1194
    return -1;
  }

1195
  if (!oggz_has_metrics (oggz)) {
1196 1197 1198
#ifdef DEBUG
    printf ("oggz_seek_units: !has_metrics, FAIL\n");
#endif
andre's avatar
andre committed
1199 1200 1201 1202 1203 1204 1205
    return -1;
  }

  switch (whence) {
  case SEEK_SET:
    return oggz_seek_set (oggz, units);
    break;
1206
  case SEEK_CUR:
andre's avatar
andre committed
1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236
    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"

1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248
OGGZ *
oggz_read_init (OGGZ * oggz)
{
  return NULL;
}

OGGZ *
oggz_read_close (OGGZ * oggz)
{
  return NULL;
}

andre's avatar
andre committed
1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268
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
1269
oggz_seek (OGGZ * oggz, oggz_off_t offset, int whence)
andre's avatar
andre committed
1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292
{
  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