oggz_tools.c 11.7 KB
Newer Older
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
/*
  Copyright (C) 2005 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"

35
#include <stdio.h>
36 37
#include <stdlib.h>
#include <string.h>
38 39
#include <getopt.h>

40
#include "oggz/oggz.h"
41

j's avatar
j committed
42
#include "dirac.h"
43
#include "oggz_tools_dirac.h"
44

45
#if defined (WIN32) || defined (__EMX__)
giles's avatar
giles committed
46
#include <fcntl.h>
47 48
#include <io.h>
#define snprintf _snprintf
giles's avatar
giles committed
49
#endif
50

51 52 53 54 55 56
#ifdef HAVE_INTTYPES_H
#  include <inttypes.h>
#else
#  define PRId64 "I64d"
#endif

j's avatar
j committed
57

58 59 60 61 62 63 64 65 66 67 68 69 70
static  ogg_uint32_t
_le_32 (ogg_uint32_t i)
{
   ogg_uint32_t ret=i;
#ifdef WORDS_BIGENDIAN
   ret =  (i>>24);
   ret += (i>>8) & 0x0000ff00;
   ret += (i<<8) & 0x00ff0000;
   ret += (i<<24);
#endif
   return ret;
}

71 72
static  ogg_uint16_t
_be_16 (ogg_uint16_t s)
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
{
  unsigned short ret=s;
#ifndef WORDS_BIGENDIAN
  ret = (s>>8) & 0x00ffU;
  ret += (s<<8) & 0xff00U;
#endif
  return ret;
}

static  ogg_uint32_t
_be_32 (ogg_uint32_t i)
{
   ogg_uint32_t ret=i;
#ifndef WORDS_BIGENDIAN
   ret =  (i>>24);
   ret += (i>>8) & 0x0000ff00;
   ret += (i<<8) & 0x00ff0000;
   ret += (i<<24);
#endif
   return ret;
}

95 96 97 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
static  ogg_int64_t
_le_64 (ogg_int64_t l)
{
  ogg_int64_t ret=l;
  unsigned char *ucptr = (unsigned char *)&ret;
#ifdef WORDS_BIGENDIAN
  unsigned char temp;

  temp = ucptr [0] ;
  ucptr [0] = ucptr [7] ;
  ucptr [7] = temp ;

  temp = ucptr [1] ;
  ucptr [1] = ucptr [6] ;
  ucptr [6] = temp ;

  temp = ucptr [2] ;
  ucptr [2] = ucptr [5] ;
  ucptr [5] = temp ;

  temp = ucptr [3] ;
  ucptr [3] = ucptr [4] ;
  ucptr [4] = temp ;

#endif
  return (*(ogg_int64_t *)ucptr);
}

123
#define INT32_LE_AT(x) _le_32((*(ogg_int32_t *)(x)))
124
#define INT16_BE_AT(x) _be_16((*(ogg_uint16_t *)(x)))
125
#define INT32_BE_AT(x) _be_32((*(ogg_int32_t *)(x)))
126
#define INT64_LE_AT(x) _le_64((*(ogg_int64_t *)(x)))
127 128 129 130 131 132 133 134 135 136 137

typedef char * (* OTCodecInfoFunc) (unsigned char * data, long n);

static char *
ot_theora_info (unsigned char * data, long len)
{
  char * buf;
  int width, height;

  if (len < 41) return NULL;

138
  buf = malloc (128);
139

conrad's avatar
conrad committed
140 141
  width = INT16_BE_AT(&data[15]);
  height = INT16_BE_AT(&data[18]);
142

143
  snprintf (buf, 128,
144
            "\tTheora-Version: %d.%d.%d\n"
145 146
	    "\tVideo-Framerate: %.3f fps\n"
	    "\tVideo-Width: %d\n\tVideo-Height: %d\n",
147
            data[7], data[8], data[9],
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
	    (double)INT32_BE_AT(&data[22])/ (double)INT32_BE_AT(&data[26]),
	    width, height);

  return buf;
}

static char *
ot_vorbis_info (unsigned char * data, long len)
{
  char * buf;

  if (len < 30) return NULL;

  buf = malloc (60);

  snprintf (buf, 60,
	    "\tAudio-Samplerate: %d Hz\n\tAudio-Channels: %d\n",
	    INT32_LE_AT(&data[12]), (int)(data[11]));

  return buf;
}

static char *
ot_speex_info (unsigned char * data, long len)
{
  char * buf;

  if (len < 68) return NULL;

  buf = malloc (60);

  snprintf (buf, 60,
	    "\tAudio-Samplerate: %d Hz\n\tAudio-Channels: %d\n",
	    INT32_LE_AT(&data[36]), INT32_LE_AT(&data[48]));

  return buf;
}

186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
static char *
ot_celt_info (unsigned char * data, long len)
{
  char * buf;

  if (len < 56) return NULL;

  buf = malloc (60);

  snprintf (buf, 60,
	    "\tAudio-Samplerate: %d Hz\n\tAudio-Channels: %d\n",
	    INT32_LE_AT(&data[40]), INT32_LE_AT(&data[44]));

  return buf;
}

202 203 204 205
static char *
ot_flac_info (unsigned char * data, long len)
{
  char * buf;
206 207
  int n;
  int version_major, version_minor;
208 209 210 211 212
  int samplerate;
  int channels;

  if (len < 30) return NULL;

213 214 215 216
  buf = malloc (120);

  version_major = data[5];
  version_minor = data[6];
217

giles's avatar
giles committed
218
  samplerate = (ogg_int64_t) (data[27] << 12) | (data[28] << 4) |
219
               ((data[29] >> 4)&0xf);
220
  channels = 1 + ((data[29] >> 1)&0x7);
221

222
  n = snprintf (buf, 120,
223 224 225
	    "\tAudio-Samplerate: %d Hz\n\tAudio-Channels: %d\n",
            samplerate, channels);

226
  snprintf (buf+n, 120-n,
227 228 229
            "\tFLAC-Ogg-Mapping-Version: %d.%d\n",
            version_major, version_minor);

230 231 232
  return buf;
}

233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
static char *
ot_oggpcm2_info (unsigned char * data, long len)
{
  char * buf;

  if (len < 28) return NULL;

  buf = malloc (60);

  snprintf (buf, 60,
	    "\tAudio-Samplerate: %d Hz\n\tAudio-Channels: %d\n",
	    INT32_BE_AT(&data[16]), (int)data[21]);

  return buf;
}

249 250 251 252 253 254 255
static char *
ot_kate_info (unsigned char * data, long len)
{
  char * buf;

  static const size_t KATE_INFO_BUFFER_LEN =
    1 /* tab */
256
  +18 /* "Content-Language: " */
257 258 259
  +15 /* 15 chars + NUL for language */
   +1 /* \n */
   +1 /* tab */
260
  +18 /* "Content-Category: " */
261 262 263 264 265 266 267 268 269 270
  +15 /* 15 chars + NUL for category */
   +1 /* \n */
   +1;/* terminating NUL */

  if (len < 64) return NULL;

  buf = malloc (KATE_INFO_BUFFER_LEN);

  /* Are these headers coming from some standard ? If so, need to find what should these be for Kate */
  snprintf (buf, KATE_INFO_BUFFER_LEN,
271 272
	    "\tContent-Language: %s\n"
            "\tContent-Category: %s\n",
273 274 275 276 277 278 279
	    &data[32], &data[48]);

#undef KATE_INFO_BUFFER_LEN

  return buf;
}

j's avatar
j committed
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
static char *
ot_dirac_info (unsigned char * data, long len)
{
  char * buf;
  dirac_info *info;

  /* read in useful bits from sequence header */
  if (len < 24) return NULL;

  buf = malloc (80);
  info = malloc(sizeof(dirac_info));

  dirac_parse_info(info, data, len);

  snprintf (buf, 80,
	    "\tVideo-Framerate: %.3f fps\n"
	    "\tVideo-Width: %d\n\tVideo-Height: %d\n",
	    (double)info->fps_numerator/ (double)info->fps_denominator,
	    info->width, info->height);

  free(info);

  return buf;
}


306 307 308 309
static char *
ot_skeleton_info (unsigned char * data, long len)
{
  char * buf;
310 311
  double pres_n, pres_d, pres;
  double base_n, base_d, base;
312

313
  if (len < 64L) return NULL;
314 315 316

  buf = malloc (60);

317 318 319 320 321 322 323 324
  pres_n = (double)INT64_LE_AT(&data[12]);
  pres_d = (double)INT64_LE_AT(&data[20]);
  if (pres_d != 0.0) {
    pres = pres_n / pres_d;
  } else {
    pres = 0.0;
  }

325 326
  base_n = (double)INT64_LE_AT(&data[28]);
  base_d = (double)INT64_LE_AT(&data[36]);
327 328 329 330 331 332
  if (base_d != 0.0) {
    base = base_n / base_d;
  } else {
    base = 0.0;
  }

333
  snprintf (buf, 60,
334
	    "\tPresentation-Time: %.3f\n\tBasetime: %.3f\n", pres, base);
335 336 337 338

  return buf;
}

339
static const OTCodecInfoFunc codec_ident[] = {
340
  ot_theora_info,
341 342 343 344 345 346 347
  ot_vorbis_info,
  ot_speex_info,
  ot_oggpcm2_info,
  NULL,             /* CMML */
  NULL,             /* ANNODEX */
  ot_skeleton_info,
  NULL,             /* FLAC0 */
348
  ot_flac_info,     /* FLAC */
349
  NULL,             /* ANXDATA */
350
  ot_celt_info,     /* CELT */
351
  ot_kate_info,     /* KATE */
352 353
  ot_dirac_info,    /* BBCD */
  NULL              /* UNKNOWN */
354 355 356
};

const char *
357
ot_page_identify (OGGZ *oggz, const ogg_page * og, char ** info)
358 359
{
  const char * ret = NULL;
360 361 362 363 364 365 366
  int serial_no;
  int content;

  /*
   * identify stream content using oggz_stream_get_content, identify
   * stream content name using oggz_stream_get_content_type
   */
giles's avatar
giles committed
367

368
  serial_no = ogg_page_serialno((ogg_page *)og);
giles's avatar
giles committed
369

370
  content = oggz_stream_get_content(oggz, serial_no);
371 372
  if (content == OGGZ_ERR_BAD_SERIALNO) return NULL;

373 374 375 376 377 378 379
  ret = oggz_stream_get_content_type(oggz, serial_no);

  if (info != NULL)
  {
    if (codec_ident[content] != NULL)
    {
      *info = codec_ident[content](og->body, og->body_len);
380 381 382 383 384
    }
  }

  return ret;
}
385

386 387 388 389 390
/*
 * Print a number of bytes to 3 significant figures
 * using standard abbreviations (GB, MB, kB, byte[s])
 */
int
391
ot_fprint_bytes (FILE * stream, long nr_bytes)
392 393
{
  if (nr_bytes > (1L<<30)) {
394
    return fprintf (stream, "%0.3f GB",
395 396
		   (double)nr_bytes / (1024.0 * 1024.0 * 1024.0));
  } else if (nr_bytes > (1L<<20)) {
397
    return fprintf (stream, "%0.3f MB",
398 399
		   (double)nr_bytes / (1024.0 * 1024.0));
  } else if (nr_bytes > (1L<<10)) {
400
    return fprintf (stream, "%0.3f kB",
401 402
		   (double)nr_bytes / (1024.0));
  } else if (nr_bytes == 1) {
403
    return fprintf (stream, "1 byte");
404
  } else {
405
    return fprintf (stream, "%ld bytes", nr_bytes);
406 407 408
  }
}

409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
/*
 * Print a bitrate to 3 significant figures
 * using quasi-standard abbreviations (Gbps, Mbps, kbps, bps)
 */
int
ot_print_bitrate (long bps)
{
  if (bps > (1000000000L)) {
    return printf ("%0.3f Gbps",
		   (double)bps / (1000.0 * 1000.0 * 1000.0));
  } else if (bps > (1000000L)) {
    return printf ("%0.3f Mbps",
		   (double)bps / (1000.0 * 1000.0));
  } else if (bps > (1000L)) {
    return printf ("%0.3f kbps",
		   (double)bps / (1000.0));
  } else {
    return printf ("%ld bps", bps);
  }
}

430
int
431
ot_fprint_time (FILE * stream, double seconds)
432 433 434 435 436 437 438 439 440 441 442 443 444
{
  int hrs, min;
  double sec;
  char * sign;

  sign = (seconds < 0.0) ? "-" : "";

  if (seconds < 0.0) seconds = -seconds;

  hrs = (int) (seconds/3600.0);
  min = (int) ((seconds - ((double)hrs * 3600.0)) / 60.0);
  sec = seconds - ((double)hrs * 3600.0)- ((double)min * 60.0);

445
  return fprintf (stream, "%s%02d:%02d:%06.3f", sign, hrs, min, sec);
446
}
conrad's avatar
conrad committed
447

448 449 450 451 452 453 454
void
ot_dirac_gpos_parse (ogg_int64_t iframe, ogg_int64_t pframe,
                     struct ot_dirac_gpos * dg)
{
  dg->pt = (iframe + pframe) >> 9;
  dg->dist = ((iframe & 0xff) << 8) | (pframe & 0xff);
  dg->delay = pframe >> 9;
oggk's avatar
oggk committed
455
  dg->dt = (ogg_int64_t)dg->pt - dg->delay;
456 457
}

458 459 460 461 462 463 464 465 466 467 468 469 470
int
ot_fprint_granulepos (FILE * stream, OGGZ * oggz, long serialno,
                      ogg_int64_t granulepos)
{
  int ret, granuleshift = oggz_get_granuleshift (oggz, serialno);

  if (granuleshift < 1) {
    ret = fprintf (stream, "%" PRId64, granulepos);
  } else {
    ogg_int64_t iframe, pframe;
    iframe = granulepos >> granuleshift;
    pframe = granulepos - (iframe << granuleshift);

471 472 473
    if (oggz_stream_get_content (oggz, serialno) != OGGZ_CONTENT_DIRAC) {
      ret = fprintf (stream, "%" PRId64 "|%" PRId64, iframe, pframe);
    } else {
474 475
      struct ot_dirac_gpos dg;
      ot_dirac_gpos_parse (iframe, pframe, &dg);
476 477
      ret = fprintf (stream,
		     "(pt:%u,dt:%" PRId64 ",dist:%hu,delay:%hu)",
478
		     dg.pt, dg.dt, dg.dist, dg.delay);
479 480 481
    }

}
482 483 484 485

  return ret;
}

conrad's avatar
conrad committed
486 487 488 489
void
ot_init (void)
{
#ifdef _WIN32
490
  /* We need to set stdin/stdout to binary mode on Win32 */
conrad's avatar
conrad committed
491 492 493 494

  _setmode( _fileno( stdin ), _O_BINARY );
  _setmode( _fileno( stdout ), _O_BINARY );
#endif
495 496 497 498 499 500
#ifdef __EMX__
  /* We need to set stdin/stdout to binary mode on OS/2*/

  setmode( fileno( stdin ), O_BINARY );
  setmode( fileno( stdout ), O_BINARY );
#endif
conrad's avatar
conrad committed
501
}
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526

void
ot_print_short_options (char * optstring)
{
  char *c;

  for (c=optstring; *c; c++) {
    if (*c != ':') printf ("-%c ", *c);
  }

  printf ("\n");
}

#ifdef HAVE_GETOPT_LONG
void
ot_print_options (struct option long_options[], char * optstring)
{
  int i;
  for (i=0; long_options[i].name != NULL; i++)  {
    printf ("--%s ", long_options[i].name);
  }

  ot_print_short_options (optstring);
}
#endif