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

63 64
/* #define DEBUG */
/* #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

andre's avatar
andre committed
70 71 72 73 74 75 76 77 78 79 80 81
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
82 83 84
  reader->read_page = NULL;
  reader->read_page_user_data = NULL;

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

  return oggz;
}

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

95
  ogg_stream_clear (&reader->ogg_stream);
andre's avatar
andre committed
96 97 98 99 100 101 102
  ogg_sync_clear (&reader->ogg_sync);

  return oggz;
}

int
oggz_set_read_callback (OGGZ * oggz, long serialno,
conrad's avatar
conrad committed
103
                        OggzReadPacket read_packet, void * user_data)
andre's avatar
andre committed
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
{
  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
121
#if 0
andre's avatar
andre committed
122
    if (stream == NULL) return OGGZ_ERR_BAD_SERIALNO;
conrad's avatar
conrad committed
123 124 125 126
#else
    if (stream == NULL)
      stream = oggz_add_stream (oggz, serialno);
#endif
andre's avatar
andre committed
127 128 129 130 131 132 133 134

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

  return 0;
}

135
int
136
oggz_set_read_page (OGGZ * oggz, long serialno, OggzReadPage read_page,
conrad's avatar
conrad committed
137
                    void * user_data)
138 139
{
  OggzReader * reader;
140
  oggz_stream_t * stream;
141 142 143 144 145 146 147 148 149

  if (oggz == NULL) return OGGZ_ERR_BAD_OGGZ;

  reader =  &oggz->x.reader;

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

150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
  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;
  }
165 166 167 168

  return 0;
}

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

      if (bytes == 0) {
conrad's avatar
conrad committed
207
        return -2;
andre's avatar
andre committed
208 209 210 211 212 213 214
      }

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

241
static int
andre's avatar
andre committed
242 243 244 245 246 247 248 249 250 251 252 253
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;

254 255
  int cb_ret = 0;

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

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

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

270
        serialno = reader->current_serialno;
andre's avatar
andre committed
271

272
        stream = oggz_get_stream (oggz, serialno);
273

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

283
        result = ogg_stream_packetout(os, op);
andre's avatar
andre committed
284

285
        if(result == -1) {
286
#ifdef DEBUG
conrad's avatar
conrad committed
287
          printf ("oggz_read_sync: hole in the data\n");
288
#endif
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
          result = ogg_stream_packetout(os, op);
          if (result == -1) {
#ifdef DEBUG
            /*
             * libogg flags "holes in the data" (which are really 
             * inconsistencies in the page sequence number) by returning
             * -1.  This occurs in some files and pretty much doesn't matter,
             *  so we silently swallow the notification and reget the packet.
             *  If the result is *still* -1 then something strange is happening.
             */
            printf ("shouldn't get here");
#endif
            return -7;
          }
        }
andre's avatar
andre committed
304

305 306
        if(result > 0){
          int content;
307 308

          stream->packetno++;
309
          
conrad's avatar
conrad committed
310 311
          /* got a packet.  process it */
          granulepos = op->granulepos;
andre's avatar
andre committed
312

313
          content = oggz_stream_get_content(oggz, serialno);
shans's avatar
 
shans committed
314
  
315 316 317
          /*
           * if we have no metrics for this stream yet, then generate them
           */      
conrad's avatar
conrad committed
318
          if 
319 320 321 322 323 324
          (
            (!stream->metric || (content == OGGZ_CONTENT_SKELETON)) 
            && 
            (oggz->flags & OGGZ_AUTO)
          ) 
          {
conrad's avatar
conrad committed
325 326
            oggz_auto_get_granulerate (oggz, op, serialno, NULL);
          }
327 328 329 330 331 332 333 334 335

          /* attempt to determine granulepos for this packet */
          if (oggz->flags & OGGZ_AUTO) {
            reader->current_granulepos = 
              oggz_auto_calculate_granulepos (content, granulepos, stream, op); 
          } else {
            reader->current_granulepos = granulepos;
          }
          stream->last_granulepos = reader->current_granulepos;
336
        
conrad's avatar
conrad committed
337 338
          /* set unit on last packet of page */
          if 
339 340 341 342
          (
            (oggz->metric || stream->metric) && reader->current_granulepos != -1
          ) 
          {
conrad's avatar
conrad committed
343 344 345 346
            reader->current_unit =
              oggz_get_unit (oggz, serialno, reader->current_granulepos);
          }

347 348 349 350
          if (stream->packetno == 1) {
            oggz_auto_read_comments (oggz, stream, serialno, op);
          }

conrad's avatar
conrad committed
351 352 353 354 355 356 357
          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);
          }
358 359
        }
        else
conrad's avatar
conrad committed
360
          break;
andre's avatar
andre committed
361 362
      }
    }
363

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

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

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

    stream = oggz_get_stream (oggz, serialno);
374

375 376 377
    if (stream == NULL) {
      /* new stream ... check bos etc. */
      if ((stream = oggz_add_stream (oggz, serialno)) == NULL) {
conrad's avatar
conrad committed
378 379
        /* error -- could not add stream */
        return -7;
andre's avatar
andre committed
380
      }
381 382 383 384 385 386 387 388 389 390

      /* identify stream type */
      oggz_auto_identify(oggz, &og, serialno);
    }
    else if (oggz_stream_get_content(oggz, serialno) == OGGZ_CONTENT_ANXDATA)
    {
      /*
       * re-identify ANXDATA streams as these are now content streams
       */
      oggz_auto_identify(oggz, &og, serialno);
391
    }
392
    
393
    os = &stream->ogg_stream;
andre's avatar
andre committed
394

395
    {
396 397 398
      ogg_int64_t granulepos;

      granulepos = ogg_page_granulepos (&og);
399
      stream->page_granulepos = granulepos;
400

401
      if ((oggz->metric || stream->metric) && granulepos != -1) {
402
       reader->current_unit = oggz_get_unit (oggz, serialno, granulepos);
403
      } else if (granulepos == 0) {
404
       reader->current_unit = 0;
andre's avatar
andre committed
405
      }
406
    }
407

408 409
    if (stream->read_page) {
      cb_ret =
conrad's avatar
conrad committed
410
        stream->read_page (oggz, &og, serialno, stream->read_page_user_data);
411
    } else if (reader->read_page) {
conrad's avatar
conrad committed
412 413
      cb_ret =
        reader->read_page (oggz, &og, serialno, reader->read_page_user_data);
414
    }
andre's avatar
andre committed
415 416

#if 0
417 418 419
    /* 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
420 421
#endif

422
    ogg_stream_pagein(os, &og);
andre's avatar
andre committed
423 424
  }

425
  return cb_ret;
andre's avatar
andre committed
426 427 428 429 430 431 432 433
}

long
oggz_read (OGGZ * oggz, long n)
{
  OggzReader * reader;
  char * buffer;
  long bytes, bytes_read = 1, remaining = n, nread = 0;
434
  int cb_ret = 0;
andre's avatar
andre committed
435 436 437 438 439 440 441

  if (oggz == NULL) return OGGZ_ERR_BAD_OGGZ;

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

442 443
  if ((cb_ret = oggz->cb_next) != OGGZ_CONTINUE) {
    oggz->cb_next = 0;
444
    return oggz_map_return_value_to_error (cb_ret);
445 446
  }

andre's avatar
andre committed
447 448
  reader = &oggz->x.reader;

449
  cb_ret = oggz_read_sync (oggz);
andre's avatar
andre committed
450

451
#if 0
452
  if (cb_ret == OGGZ_READ_EMPTY) {
453 454
    /* If there's nothing to read yet, don't return 0 (eof) */
    if (reader->current_unit == 0) cb_ret = 0;
455 456 457
    else {
#if 0
      printf ("oggz_read: EMPTY, current_unit %ld != 0\n",
conrad's avatar
conrad committed
458
              reader->current_unit);
459 460 461
      return 0;
#endif
    }
462
  }
463
#endif
464

465
  while (cb_ret != OGGZ_STOP_ERR && cb_ret != OGGZ_STOP_OK &&
conrad's avatar
conrad committed
466
         bytes_read > 0 && remaining > 0) {
467
    bytes = MIN (remaining, CHUNKSIZE);
andre's avatar
andre committed
468
    buffer = ogg_sync_buffer (&reader->ogg_sync, bytes);
469
    if ((bytes_read = (long) oggz_io_read (oggz, buffer, bytes)) == 0) {
470 471 472 473
      /* schyeah! */
    }
    if (bytes_read == OGGZ_ERR_SYSTEM) {
      return OGGZ_ERR_SYSTEM;
andre's avatar
andre committed
474 475
    }

conrad's avatar
 
conrad committed
476 477 478 479 480 481 482 483
    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
484 485
  }

486
  if (cb_ret == OGGZ_STOP_ERR) oggz_purge (oggz);
487

488
  /* Don't return 0 unless it's actually an EOF condition */
489
  if (nread == 0) {
conrad's avatar
conrad committed
490 491 492 493 494 495 496
    switch (bytes_read) {
    case OGGZ_ERR_IO_AGAIN:
    case OGGZ_ERR_SYSTEM:
      return bytes_read; break;
    default: break;
    }

497 498 499 500 501
    if (cb_ret == OGGZ_READ_EMPTY) {
      return 0;
    } else {
      return oggz_map_return_value_to_error (cb_ret);
    }
502 503 504 505

  } else {
    if (cb_ret == OGGZ_READ_EMPTY) cb_ret = OGGZ_CONTINUE;
    oggz->cb_next = cb_ret;
506
  }
507

andre's avatar
andre committed
508 509 510 511 512 513 514 515 516 517
  return nread;
}

/* generic */
long
oggz_read_input (OGGZ * oggz, unsigned char * buf, long n)
{
  OggzReader * reader;
  char * buffer;
  long bytes, remaining = n, nread = 0;
518
  int cb_ret = 0;
andre's avatar
andre committed
519 520 521 522 523 524 525

  if (oggz == NULL) return OGGZ_ERR_BAD_OGGZ;

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

526 527
  if ((cb_ret = oggz->cb_next) != OGGZ_CONTINUE) {
    oggz->cb_next = 0;
528
    return oggz_map_return_value_to_error (cb_ret);
529 530
  }

andre's avatar
andre committed
531 532
  reader = &oggz->x.reader;

533 534
  cb_ret = oggz_read_sync (oggz);

535
#if 0
536
  if (cb_ret == OGGZ_READ_EMPTY) {
537 538 539 540
    /* If there's nothing to read yet, don't return 0 (eof) */
    if (reader->current_unit == 0) cb_ret = 0;
    else return 0;
  }
541
#endif
conrad's avatar
conrad committed
542

543
  while (cb_ret != OGGZ_STOP_ERR && cb_ret != OGGZ_STOP_OK  &&
conrad's avatar
conrad committed
544
         /* !oggz->eos && */ remaining > 0) {
andre's avatar
andre committed
545 546 547 548 549 550 551 552 553
    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;

554
    cb_ret = oggz_read_sync (oggz);
andre's avatar
andre committed
555 556
  }

557
  if (cb_ret == OGGZ_STOP_ERR) oggz_purge (oggz);
558

559
  if (nread == 0) {
560 561
    /* Don't return 0 unless it's actually an EOF condition */
    if (cb_ret == OGGZ_READ_EMPTY) {
562
      return OGGZ_ERR_STOP_OK;
563
    } else {
564
      return oggz_map_return_value_to_error (cb_ret);
565
    }
566 567 568
  } else {
    if (cb_ret == OGGZ_READ_EMPTY) cb_ret = OGGZ_CONTINUE;
    oggz->cb_next = cb_ret;
569
  }
570

andre's avatar
andre committed
571 572 573 574 575 576 577 578 579
  return nread;
}


#else /* OGGZ_CONFIG_READ */

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

580 581 582 583 584 585 586 587 588 589 590 591
OGGZ *
oggz_read_init (OGGZ * oggz)
{
  return NULL;
}

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

andre's avatar
andre committed
592 593
int
oggz_set_read_callback (OGGZ * oggz, long serialno,
conrad's avatar
conrad committed
594
                        OggzReadPacket read_packet, void * user_data)
andre's avatar
andre committed
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611
{
  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;
}

#endif