oggz.c 13.3 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
/*
   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.
*/

#include "config.h"

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

41
#ifdef HAVE_UNISTD_H
andre's avatar
andre committed
42
#include <unistd.h>
43 44
#endif

andre's avatar
andre committed
45 46 47 48 49 50 51
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <time.h>

#include <ogg/ogg.h>

52
#include "oggz_compat.h"
andre's avatar
andre committed
53
#include "oggz_private.h"
conrad's avatar
conrad committed
54
#include "oggz_vector.h"
andre's avatar
andre committed
55

56 57 58 59
/* Definitions for oggz_run() */
long oggz_read (OGGZ * oggz, long n);
long oggz_write (OGGZ * oggz, long n);

andre's avatar
andre committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
/*#define DEBUG*/

static int
oggz_flags_disabled (int flags)
{
 if (flags & OGGZ_WRITE) {
   if (!OGGZ_CONFIG_WRITE) return OGGZ_ERR_DISABLED;
 } else {
   if (!OGGZ_CONFIG_READ) return OGGZ_ERR_DISABLED;
 }

 return 0;
}

OGGZ *
oggz_new (int flags)
{
  OGGZ * oggz;

  if (oggz_flags_disabled (flags)) return NULL;

  oggz = (OGGZ *) oggz_malloc (sizeof (OGGZ));
  if (oggz == NULL) return NULL;

  oggz->flags = flags;
conrad's avatar
conrad committed
85
  oggz->file = NULL;
86
  oggz->io = NULL;
andre's avatar
andre committed
87 88 89 90

  oggz->offset = 0;
  oggz->offset_data_begin = 0;

conrad's avatar
conrad committed
91
  oggz->run_blocksize = 1024;
92 93
  oggz->cb_next = 0;

conrad's avatar
conrad committed
94
  oggz->streams = oggz_vector_new ();
andre's avatar
andre committed
95 96 97 98 99 100 101 102 103
  oggz->all_at_eos = 0;

  oggz->metric = NULL;
  oggz->metric_user_data = NULL;
  oggz->metric_internal = 0;

  oggz->order = NULL;
  oggz->order_user_data = NULL;

104
  oggz->packet_buffer = oggz_dlist_new ();
105

andre's avatar
andre committed
106 107 108 109 110 111 112 113 114 115 116 117 118
  if (OGGZ_CONFIG_WRITE && (oggz->flags & OGGZ_WRITE)) {
    oggz_write_init (oggz);
  } else if (OGGZ_CONFIG_READ) {
    oggz_read_init (oggz);
  }

  return oggz;
}

OGGZ *
oggz_open (char * filename, int flags)
{
  OGGZ * oggz = NULL;
conrad's avatar
conrad committed
119
  FILE * file = NULL;
andre's avatar
andre committed
120 121 122 123

  if (oggz_flags_disabled (flags)) return NULL;

  if (flags & OGGZ_WRITE) {
124
    file = fopen (filename, "wb");
andre's avatar
andre committed
125
  } else {
126
    file = fopen (filename, "rb");
andre's avatar
andre committed
127
  }
conrad's avatar
conrad committed
128
  if (file == NULL) return NULL;
andre's avatar
andre committed
129

130 131
  if ((oggz = oggz_new (flags)) == NULL) {
    fclose (file);
132
    return NULL;
133
  }
134

conrad's avatar
conrad committed
135
  oggz->file = file;
andre's avatar
andre committed
136 137 138 139 140

  return oggz;
}

OGGZ *
conrad's avatar
conrad committed
141
oggz_open_stdio (FILE * file, int flags)
andre's avatar
andre committed
142 143 144 145 146
{
  OGGZ * oggz = NULL;

  if (oggz_flags_disabled (flags)) return NULL;

147 148 149
  if ((oggz = oggz_new (flags)) == NULL)
    return NULL;

conrad's avatar
conrad committed
150
  oggz->file = file;
andre's avatar
andre committed
151 152 153 154 155 156 157 158 159

  return oggz;
}

int
oggz_flush (OGGZ * oggz)
{
  if (oggz == NULL) return OGGZ_ERR_BAD_OGGZ;

160 161 162 163
  if (OGGZ_CONFIG_WRITE && (oggz->flags & OGGZ_WRITE)) {
    oggz_write_flush (oggz);
  }

164
  return oggz_io_flush (oggz);
andre's avatar
andre committed
165 166 167 168 169 170 171
}

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

172 173
  oggz_comments_free (stream);

andre's avatar
andre committed
174 175 176 177 178 179
  if (stream->ogg_stream.serialno != -1)
    ogg_stream_clear (&stream->ogg_stream);

  if (stream->metric_internal)
    oggz_free (stream->metric_user_data);

180 181 182
  if (stream->calculate_data != NULL)
    oggz_free (stream->calculate_data);
  
183 184
  oggz_free (stream);

andre's avatar
andre committed
185 186 187 188 189 190 191 192 193 194 195 196 197 198
  return 0;
}

int
oggz_close (OGGZ * oggz)
{
  if (oggz == NULL) return OGGZ_ERR_BAD_OGGZ;

  if (OGGZ_CONFIG_WRITE && (oggz->flags & OGGZ_WRITE)) {
    oggz_write_close (oggz);
  } else if (OGGZ_CONFIG_READ) {
    oggz_read_close (oggz);
  }

199 200 201
  oggz_vector_foreach (oggz->streams, oggz_stream_clear);
  oggz_vector_delete (oggz->streams);

202 203 204
  assert(oggz_dlist_is_empty(oggz->packet_buffer));
  oggz_dlist_delete(oggz->packet_buffer);
  
205 206 207
  if (oggz->metric_internal)
    oggz_free (oggz->metric_user_data);

conrad's avatar
conrad committed
208 209
  if (oggz->file != NULL) {
    if (fclose (oggz->file) == EOF) {
andre's avatar
andre committed
210 211 212 213
      return OGGZ_ERR_SYSTEM;
    }
  }

214
  if (oggz->io != NULL) {
215
    oggz_io_flush (oggz);
216 217 218
    oggz_free (oggz->io);
  }

andre's avatar
andre committed
219 220 221 222 223
  oggz_free (oggz);

  return 0;
}

224
oggz_off_t
andre's avatar
andre committed
225 226 227 228 229 230 231
oggz_tell (OGGZ * oggz)
{
  if (oggz == NULL) return OGGZ_ERR_BAD_OGGZ;

  return oggz->offset;
}

conrad's avatar
conrad committed
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
ogg_int64_t
oggz_tell_units (OGGZ * oggz)
{
  OggzReader * reader;

  if (oggz == NULL) return OGGZ_ERR_BAD_OGGZ;

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

  reader = &oggz->x.reader;

  if (OGGZ_CONFIG_READ) {
    return reader->current_unit;
  } else {
    return OGGZ_ERR_DISABLED;
  }
}

252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
ogg_int64_t
oggz_tell_granulepos (OGGZ * oggz)
{
  if (oggz == NULL) return OGGZ_ERR_BAD_OGGZ;
  
  if (oggz->flags & OGGZ_WRITE) {
    return OGGZ_ERR_INVALID;
  }

  if (OGGZ_CONFIG_READ) {
    return oggz->x.reader.current_granulepos;
  } else {
    return OGGZ_ERR_DISABLED;
  }
}

268 269 270 271 272 273 274 275
long
oggz_run (OGGZ * oggz)
{
  long n = OGGZ_ERR_DISABLED;

  if (oggz == NULL) return OGGZ_ERR_BAD_OGGZ;

  if (OGGZ_CONFIG_WRITE && (oggz->flags & OGGZ_WRITE)) {
conrad's avatar
conrad committed
276
    while ((n = oggz_write (oggz, oggz->run_blocksize)) > 0);
277
  } else if (OGGZ_CONFIG_READ) {
conrad's avatar
conrad committed
278
    while ((n = oggz_read (oggz, oggz->run_blocksize)) > 0);
279 280 281 282 283
  }

  return n;
}

conrad's avatar
conrad committed
284 285 286 287 288 289 290 291 292 293 294 295
int
oggz_run_set_blocksize (OGGZ * oggz, long blocksize)
{
  if (oggz == NULL) return OGGZ_ERR_BAD_OGGZ;

  if (blocksize <= 0) return OGGZ_ERR_INVALID;

  oggz->run_blocksize = blocksize;

  return 0;
}

andre's avatar
andre committed
296 297 298 299 300 301 302 303 304 305 306 307 308
/******** oggz_stream management ********/

static int
oggz_find_stream (void * data, long serialno)
{
  oggz_stream_t * stream = (oggz_stream_t *) data;

  return (stream->ogg_stream.serialno == serialno);
}

oggz_stream_t *
oggz_get_stream (OGGZ * oggz, long serialno)
{
309
  if (serialno == -1) return NULL;
andre's avatar
andre committed
310

311
  return oggz_vector_find_with (oggz->streams, oggz_find_stream, serialno);
andre's avatar
andre committed
312 313 314 315 316 317 318 319 320 321 322 323
}

oggz_stream_t *
oggz_add_stream (OGGZ * oggz, long serialno)
{
  oggz_stream_t * stream;

  stream = oggz_malloc (sizeof (oggz_stream_t));
  if (stream == NULL) return NULL;

  ogg_stream_init (&stream->ogg_stream, (int)serialno);

324 325
  oggz_comments_init (stream);

326
  stream->content = OGGZ_CONTENT_UNKNOWN;
327
  stream->numheaders = 3; /* Default to 3 headers for Ogg logical bitstreams */
328 329 330 331 332
  stream->preroll = 0;
  stream->granulerate_n = 1;
  stream->granulerate_d = 1;
  stream->basegranule = 0;
  stream->granuleshift = 0;
333

andre's avatar
andre committed
334 335 336 337
  stream->delivered_non_b_o_s = 0;
  stream->b_o_s = 1;
  stream->e_o_s = 0;
  stream->granulepos = 0;
338
  stream->packetno = -1; /* will be incremented on first read or write */
andre's avatar
andre committed
339 340 341 342 343 344 345 346

  stream->metric = NULL;
  stream->metric_user_data = NULL;
  stream->metric_internal = 0;
  stream->order = NULL;
  stream->order_user_data = NULL;
  stream->read_packet = NULL;
  stream->read_user_data = NULL;
347 348
  stream->read_page = NULL;
  stream->read_page_user_data = NULL;
andre's avatar
andre committed
349

350 351
  stream->calculate_data = NULL;
  
conrad's avatar
conrad committed
352
  oggz_vector_insert_p (oggz->streams, stream);
andre's avatar
andre committed
353 354 355 356 357 358 359 360

  return stream;
}

int
oggz_get_bos (OGGZ * oggz, long serialno)
{
  oggz_stream_t * stream;
conrad's avatar
conrad committed
361
  int i, size;
andre's avatar
andre committed
362 363 364 365

  if (oggz == NULL) return OGGZ_ERR_BAD_OGGZ;

  if (serialno == -1) {
conrad's avatar
conrad committed
366 367 368
    size = oggz_vector_size (oggz->streams);
    for (i = 0; i < size; i++) {
      stream = (oggz_stream_t *)oggz_vector_nth_p (oggz->streams, i);
andre's avatar
andre committed
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
#if 1
      /* If this stream has delivered a non bos packet, return FALSE */
      if (stream->delivered_non_b_o_s) return 0;
#else
      /* If this stream has delivered its bos packet, return FALSE */
      if (!stream->b_o_s) return 0;
#endif
    }
    return 1;
  } else {
    stream = oggz_get_stream (oggz, serialno);
    if (stream == NULL)
      return OGGZ_ERR_BAD_SERIALNO;

    return stream->b_o_s;
  }
}

int
oggz_get_eos (OGGZ * oggz, long serialno)
{
  oggz_stream_t * stream;
conrad's avatar
conrad committed
391
  int i, size;
andre's avatar
andre committed
392 393 394 395

  if (oggz == NULL) return OGGZ_ERR_BAD_OGGZ;

  if (serialno == -1) {
conrad's avatar
conrad committed
396 397 398
    size = oggz_vector_size (oggz->streams);
    for (i = 0; i < size; i++) {
      stream = (oggz_stream_t *)oggz_vector_nth_p (oggz->streams, i);
andre's avatar
andre committed
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
      if (!stream->e_o_s) return 0;
    }
    return 1;
  } else {
    stream = oggz_get_stream (oggz, serialno);
    if (stream == NULL)
      return OGGZ_ERR_BAD_SERIALNO;

    return stream->e_o_s;
  }
}

int
oggz_set_eos (OGGZ * oggz, long serialno)
{
  oggz_stream_t * stream;
conrad's avatar
conrad committed
415
  int i, size;
andre's avatar
andre committed
416 417 418 419

  if (oggz == NULL) return OGGZ_ERR_BAD_OGGZ;

  if (serialno == -1) {
conrad's avatar
conrad committed
420 421 422
    size = oggz_vector_size (oggz->streams);
    for (i = 0; i < size; i++) {
      stream = (oggz_stream_t *) oggz_vector_nth_p (oggz->streams, i);
andre's avatar
andre committed
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
      stream->e_o_s = 1;
    }
    oggz->all_at_eos = 1;
  } else {
    stream = oggz_get_stream (oggz, serialno);
    if (stream == NULL)
      return OGGZ_ERR_BAD_SERIALNO;

    stream->e_o_s = 1;

    if (oggz_get_eos (oggz, -1))
      oggz->all_at_eos = 1;
  }

  return 0;
}

440 441 442 443 444 445 446
int
oggz_get_numtracks (OGGZ * oggz)
{
  if (oggz == NULL) return OGGZ_ERR_BAD_OGGZ;
  return oggz_vector_size (oggz->streams);
}

447 448 449 450 451 452 453 454
/* Generate a pseudorandom serialno on request, ensuring that the number
 * selected is not -1 or the serialno of an existing logical bitstream.
 * NB. This inlines a simple linear congruential generator to avoid problems
 * of portability of rand() vs. the POSIX random()/initstate()/getstate(), and
 * in the case of rand() in order to avoid interfering with the random number
 * sequence.
 * Adapated from a patch by Erik de Castro Lopo, July 2007.
 */
andre's avatar
andre committed
455 456 457
long
oggz_serialno_new (OGGZ * oggz)
{
458 459 460 461
  static long serialno = 0;
  int k;

  if (serialno == 0) serialno = time(NULL);
andre's avatar
andre committed
462 463

  do {
464 465 466
    for (k = 0; k < 3 || serialno == 0; k++)
      serialno = 11117 * serialno + 211231;
  } while (serialno == -1 || oggz_get_stream (oggz, serialno) != NULL);
andre's avatar
andre committed
467 468 469 470 471 472

  return serialno;
}

/******** OggzMetric management ********/

conrad's avatar
conrad committed
473
int
andre's avatar
andre committed
474 475 476 477 478 479 480 481
oggz_set_metric_internal (OGGZ * oggz, long serialno,
			  OggzMetric metric, void * user_data, int internal)
{
  oggz_stream_t * stream;

  if (oggz == NULL) return OGGZ_ERR_BAD_OGGZ;

  if (serialno == -1) {
482
    if (oggz->metric_internal && oggz->metric_user_data)
andre's avatar
andre committed
483 484 485 486 487 488 489 490
      oggz_free (oggz->metric_user_data);
    oggz->metric = metric;
    oggz->metric_user_data = user_data;
    oggz->metric_internal = internal;
  } else {
    stream = oggz_get_stream (oggz, serialno);
    if (stream == NULL) return OGGZ_ERR_BAD_SERIALNO;

491
    if (stream->metric_internal && stream->metric_user_data)
andre's avatar
andre committed
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
      oggz_free (stream->metric_user_data);
    stream->metric = metric;
    stream->metric_user_data = user_data;
    stream->metric_internal = internal;
  }

  return 0;
}

int
oggz_set_metric (OGGZ * oggz, long serialno,
		 OggzMetric metric, void * user_data)
{
  return oggz_set_metric_internal (oggz, serialno, metric, user_data, 0);
}

508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
/*
 * Check if a stream in an oggz has a metric
 */
int
oggz_stream_has_metric (OGGZ * oggz, long serialno)
{
  oggz_stream_t * stream;

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

  stream = oggz_get_stream (oggz, serialno);
  if (stream == NULL) return OGGZ_ERR_BAD_SERIALNO;

  if (stream->metric != NULL) return 1;

  return 0;
}

526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
/*
 * Check if an oggz has metrics for all streams
 */
int
oggz_has_metrics (OGGZ * oggz)
{
  int i, size;
  oggz_stream_t * stream;

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

  size = oggz_vector_size (oggz->streams);
  for (i = 0; i < size; i++) {
    stream = (oggz_stream_t *)oggz_vector_nth_p (oggz->streams, i);
    if (stream->metric == NULL) return 0;
  }

  return 1;
}

andre's avatar
andre committed
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 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 596 597 598
ogg_int64_t
oggz_get_unit (OGGZ * oggz, long serialno, ogg_int64_t granulepos)
{
  oggz_stream_t * stream;

  if (oggz == NULL) return OGGZ_ERR_BAD_OGGZ;

  if (granulepos == -1) return -1;

  if (serialno == -1) {
    if (oggz->metric)
      return oggz->metric (oggz, serialno, granulepos,
			   oggz->metric_user_data);
  } else {
    stream = oggz_get_stream (oggz, serialno);
    if (!stream) return -1;

    if (stream->metric) {
      return stream->metric (oggz, serialno, granulepos,
			     stream->metric_user_data);
    } else if (oggz->metric) {
      return oggz->metric (oggz, serialno, granulepos,
			   oggz->metric_user_data);
    }
  }

  return -1;
}

int
oggz_set_order (OGGZ * oggz, long serialno,
		OggzOrder order, void * user_data)
{
  oggz_stream_t * stream;

  if (oggz == NULL) return OGGZ_ERR_BAD_OGGZ;

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

  if (serialno == -1) {
    oggz->order = order;
    oggz->order_user_data = user_data;
  } else {
    stream = oggz_get_stream (oggz, serialno);
    stream->order = order;
    stream->order_user_data = user_data;
  }

  return 0;
}

599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
/* Map callback return values to error return values */
int
oggz_map_return_value_to_error (int cb_ret)
{
  switch (cb_ret) {
  case OGGZ_CONTINUE:
    return OGGZ_ERR_OK;
  case OGGZ_STOP_OK:
    return OGGZ_ERR_STOP_OK;
  case OGGZ_STOP_ERR:
    return OGGZ_ERR_STOP_ERR;
  default:
    return OGGZ_ERR_STOP_ERR;
  }
}