oggzinfo.c 12.1 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
/*
   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 <stdio.h>
#include <stdlib.h>
conrad's avatar
conrad committed
35 36
#include <limits.h> /* LONG_MAX */
#include <math.h>
andre's avatar
andre committed
37

38 39 40 41
#include <getopt.h>
#include <errno.h>

#include <oggz/oggz.h>
conrad's avatar
conrad committed
42 43
#include "oggz_tools.h"

44 45 46 47 48 49
#ifndef WIN32
#include <inttypes.h>
#else
#define PRId64 "ld"
#endif

50 51 52 53
static void
usage (char * progname)
{
  printf ("Usage: %s [options] filename ...\n", progname);
54
  printf ("Display information about one or more Ogg files and their bitstreams\n");
55 56 57 58 59 60 61 62 63 64 65 66 67
  printf ("\nDisplay options\n");
  printf ("  -l, --length           Display content lengths\n");
  printf ("  -b, --bitrate          Display bitrate information\n");
  printf ("  -g, --page-stats       Display Ogg page statistics\n");
  printf ("  -p, --packet-stats     Display Ogg packet statistics\n");
  printf ("  -a, --all              Display all information\n");
  printf ("\nMiscellaneous options\n");
  printf ("  -h, --help             Display this help and exit\n");
  printf ("  -v, --version          Output version information and exit\n");
  printf ("\n");
  printf ("Please report bugs to <ogg-dev@xiph.org>\n");
}

68 69
#define SEP "------------------------------------------------------------"

conrad's avatar
conrad committed
70 71 72
typedef struct _OI_Info OI_Info;
typedef struct _OI_Stats OI_Stats;
typedef struct _OI_TrackInfo OI_TrackInfo;
73

conrad's avatar
conrad committed
74 75
/* Let's get functional */
typedef void (*OI_TrackFunc) (OI_Info * info, OI_TrackInfo * oit, long serialno);
andre's avatar
andre committed
76

conrad's avatar
conrad committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
struct _OI_Info {
  OggzTable * tracks;
  ogg_int64_t duration;
  long length_total;
};

struct _OI_Stats {
  /* Pass 1 */
  long count;
  long length_total;
  long length_min;
  long length_max;

  /* Pass 2 */
  long length_avg;
  long length_deviation_total;
  double length_stddev;
};

struct _OI_TrackInfo {
  OI_Stats pages;
  OI_Stats packets;
conrad's avatar
conrad committed
99
  const char * codec_name;
100
  char * codec_info;
conrad's avatar
conrad committed
101 102
};

103 104 105 106 107
static int show_length = 0;
static int show_bitrate = 0;
static int show_page_stats = 0;
static int show_packet_stats = 0;

conrad's avatar
conrad committed
108 109
static void
oggzinfo_apply (OI_TrackFunc func, OI_Info * info)
andre's avatar
andre committed
110
{
conrad's avatar
conrad committed
111 112 113
  OI_TrackInfo * oit;
  long serialno;
  int n, i;
andre's avatar
andre committed
114

conrad's avatar
conrad committed
115 116 117 118
  n = oggz_table_size (info->tracks);
  for (i = 0; i < n; i++) {
    oit = oggz_table_nth (info->tracks, i, &serialno);
    if (oit) func (info, oit, serialno);
andre's avatar
andre committed
119
  }
conrad's avatar
conrad committed
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
}

static void
oi_stats_clear (OI_Stats * stats)
{
  stats->count = 0;

  stats->length_total = 0;
  stats->length_min = LONG_MAX;
  stats->length_max = 0;

  stats->length_avg = 0;
  stats->length_deviation_total = 0;
  stats->length_stddev = 0;
}

static OI_TrackInfo *
oggzinfo_trackinfo_new (void)
{
  OI_TrackInfo * oit;

  oit = malloc (sizeof (OI_TrackInfo));

  oi_stats_clear (&oit->pages);
  oi_stats_clear (&oit->packets);

  return oit;
}

static long
oi_bitrate (long bytes, ogg_int64_t ms)
{
  return (long) (((ogg_int64_t)bytes * 8 * 1000) / ms);
}

static void
oi_stats_print (OI_Info * info, OI_Stats * stats, char * label)
{
158 159 160 161 162 163 164 165 166
  printf ("\t%s-Length-Maximum: ", label);
  ot_print_bytes (stats->length_max);
  putchar ('\n');

  printf ("\t%s-Length-StdDev: ", label);
  ot_print_bytes (stats->length_stddev);
  putchar ('\n');

#if 0
conrad's avatar
conrad committed
167 168
  printf ("\t%s-Length-Maximum: %ld bytes\n", label, stats->length_max);
  /*printf ("\t%s-Length-Average: %ld bytes\n", label, stats->length_avg);*/
169
  printf ("\t%s-Length-StdDev: %.0f bytes\n", label, stats->length_stddev);
conrad's avatar
conrad committed
170 171 172 173
  /*
  printf ("\tRange: [%ld - %ld] bytes, Std.Dev. %.3f bytes\n",
	  stats->length_min, stats->length_max, stats->length_stddev);
  */
174
#endif
conrad's avatar
conrad committed
175 176 177 178 179 180
}

/* oggzinfo_trackinfo_print() */
static void
oit_print (OI_Info * info, OI_TrackInfo * oit, long serialno)
{
181
  printf ("\n%s: serialno %010ld\n", oit->codec_name, serialno);
182
  printf ("\t%ld packets in %ld pages, %.1f packets/page\n",
conrad's avatar
conrad committed
183 184
	  oit->packets.count, oit->pages.count,
	  (double)oit->packets.count / (double)oit->pages.count);
185 186 187 188 189 190 191 192 193

  if (show_length) {
    printf ("\tContent-Length: %ld bytes\n", oit->pages.length_total);
  }

  if (show_bitrate) {
    printf ("\tContent-Bitrate-Average: %ld bps\n",
	    oi_bitrate (oit->pages.length_total, info->duration));
  }
conrad's avatar
conrad committed
194

195
  if (oit->codec_info != NULL) {
196
    fputs (oit->codec_info, stdout);
197 198
  }

199 200 201
  if (show_page_stats) {
    oi_stats_print (info, &oit->pages, "Page");
  }
conrad's avatar
conrad committed
202

203 204 205
  if (show_packet_stats) {
    oi_stats_print (info, &oit->packets, "Packet");
  }
conrad's avatar
conrad committed
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
}

static void
oi_stats_average (OI_Stats * stats)
{
  stats->length_avg = stats->length_total / stats->count;
}

static void
oit_calc_average (OI_Info * info, OI_TrackInfo * oit, long serialno)
{
  oi_stats_average (&oit->pages);
  oi_stats_average (&oit->packets);
}

static void
oi_stats_stddev (OI_Stats * stats)
{
  double variance;

  variance = (double)stats->length_deviation_total / (double)(stats->count - 1);
  stats->length_stddev = sqrt (variance);

}

static void
oit_calc_stddev (OI_Info * info, OI_TrackInfo * oit, long serialno)
{
  oi_stats_stddev (&oit->pages);
  oi_stats_stddev (&oit->packets);
}

static int
read_page_pass1 (OGGZ * oggz, const ogg_page * og, long serialno, void * user_data)
{
  OI_Info * info = (OI_Info *)user_data;
  OI_TrackInfo * oit;
  long bytes;

  oit = oggz_table_lookup (info->tracks, serialno);
  if (oit == NULL) {
    oit = oggzinfo_trackinfo_new ();
    oggz_table_insert (info->tracks, serialno, oit);
andre's avatar
andre committed
249 250
  }

conrad's avatar
conrad committed
251
  if (ogg_page_bos ((ogg_page *)og)) {
252
    oit->codec_name = ot_page_identify (og, &oit->codec_info);
conrad's avatar
conrad committed
253 254
  }

conrad's avatar
conrad committed
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 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 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
  bytes = og->header_len + og->body_len;

  /* Increment the total stream length */
  info->length_total += bytes;

  /* Increment the page statistics */
  oit->pages.count++;
  oit->pages.length_total += bytes;
  if (bytes < oit->pages.length_min)
    oit->pages.length_min = bytes;
  if (bytes > oit->pages.length_max)
    oit->pages.length_max = bytes;

  return 0;
}

static int
read_page_pass2 (OGGZ * oggz, const ogg_page * og, long serialno, void * user_data)
{
  OI_Info * info = (OI_Info *)user_data;
  OI_TrackInfo * oit;
  long bytes, deviation;

  oit = oggz_table_lookup (info->tracks, serialno);

  /* Increment the page length deviation squared total */
  bytes = og->header_len + og->body_len;
  deviation = bytes - oit->pages.length_avg;
  oit->pages.length_deviation_total += (deviation * deviation);

  return 0;
}

static int
read_packet_pass1 (OGGZ * oggz, ogg_packet * op, long serialno,
		   void * user_data)
{
  OI_Info * info = (OI_Info *)user_data;
  OI_TrackInfo * oit;

  oit = oggz_table_lookup (info->tracks, serialno);

  /* Increment the packet statistics */
  oit->packets.count++;
  oit->packets.length_total += op->bytes;
  if (op->bytes < oit->packets.length_min)
    oit->packets.length_min = op->bytes;
  if (op->bytes > oit->packets.length_max)
    oit->packets.length_max = op->bytes;

  return 0;
}

static int
read_packet_pass2 (OGGZ * oggz, ogg_packet * op, long serialno,
		   void * user_data)
{
  OI_Info * info = (OI_Info *)user_data;
  OI_TrackInfo * oit;
  long deviation;

  oit = oggz_table_lookup (info->tracks, serialno);

  /* Increment the packet length deviation squared total */
  deviation = op->bytes - oit->packets.length_avg;
  oit->packets.length_deviation_total += (deviation * deviation);

  return 0;
}

static int
oi_pass1 (OGGZ * oggz, OI_Info * info)
{
  long n;

  oggz_seek (oggz, 0, SEEK_SET);
  oggz_set_read_page (oggz, -1, read_page_pass1, info);
  oggz_set_read_callback (oggz, -1, read_packet_pass1, info);

  while ((n = oggz_read (oggz, 1024)) > 0);

  oggzinfo_apply (oit_calc_average, info);

  return 0;
}

static int
oi_pass2 (OGGZ * oggz, OI_Info * info)
{
  long n;

  oggz_seek (oggz, 0, SEEK_SET);
  oggz_set_read_page (oggz, -1, read_page_pass2, info);
  oggz_set_read_callback (oggz, -1, read_packet_pass2, info);

  while ((n = oggz_read (oggz, 1024)) > 0);

  oggzinfo_apply (oit_calc_stddev, info);

andre's avatar
andre committed
354 355 356
  return 0;
}

357 358 359 360 361 362
static void
oit_delete (OI_Info * info, OI_TrackInfo * oit, long serialno)
{
  if (oit->codec_info) free (oit->codec_info);
}

andre's avatar
andre committed
363 364 365
int
main (int argc, char ** argv)
{
366 367 368 369 370 371 372
  int show_version = 0;
  int show_help = 0;

  char * progname;
  int i;
  int show_all = 0;

373
  int many_files = 0;
374
  char * infilename;
andre's avatar
andre committed
375
  OGGZ * oggz;
conrad's avatar
conrad committed
376
  OI_Info info;
andre's avatar
andre committed
377

378 379
  progname = argv[0];

andre's avatar
andre committed
380
  if (argc < 2) {
381
    usage (progname);
382
    return (1);
andre's avatar
andre committed
383 384
  }

385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
  while (1) {
    char * optstring = "hvlbgpa";

#ifdef HAVE_GETOPT_LONG
    static struct option long_options[] = {
      {"help", no_argument, 0, 'h'},
      {"version", no_argument, 0, 'v'},
      {"length", no_argument, 0, 'l'},
      {"bitrate", no_argument, 0, 'b'},
      {"page-stats", no_argument, 0, 'g'},
      {"packet-stats", no_argument, 0, 'p'},
      {"all", no_argument, 0, 'a'},
      {0,0,0,0}
    };

    i = getopt_long (argc, argv, optstring, long_options, NULL);
#else
    i = getopt (argc, argv, optstring);
#endif
    if (i == -1) break;
    if (i == ':') {
      usage (progname);
      goto exit_err;
    }

    switch (i) {
    case 'h': /* help */
      show_help = 1;
      break;
    case 'v': /* version */
      show_version = 1;
      break;
    case 'l': /* length */
      show_length = 1;
      break;
    case 'b': /* bitrate */
      show_bitrate = 1;
      break;
    case 'g': /* page stats */
      show_page_stats = 1;
      break;
    case 'p': /* packet stats */
      show_packet_stats = 1;
      break;
    case 'a':
      show_all = 1;
      break;
    default:
      break;
    }
  }

  if (show_version) {
    printf ("%s version " VERSION "\n", progname);
  }

  if (show_help) {
    usage (progname);
  }

  if (show_version || show_help) {
    goto exit_ok;
  }

  if (optind >= argc) {
    usage (progname);
    goto exit_err;
  }

  if (show_all) {
    show_length = 1;
    show_bitrate = 1;
    show_page_stats = 1;
    show_packet_stats = 1;
  }

461 462
  if (argc > optind+1) {
    many_files = 1;
andre's avatar
andre committed
463 464
  }

465 466
  while (optind < argc) {
    infilename = argv[optind++];
conrad's avatar
conrad committed
467

468 469 470 471
    if ((oggz = oggz_open (infilename, OGGZ_READ|OGGZ_AUTO)) == NULL) {
      printf ("unable to open file %s\n", argv[1]);
      return (1);
    }
andre's avatar
andre committed
472

473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
    info.tracks = oggz_table_new ();
    info.length_total = 0;
    
    oi_pass1 (oggz, &info);

    oggz_seek_units (oggz, 0, SEEK_END);
    info.duration = oggz_tell_units (oggz);
    
    oi_pass2 (oggz, &info);
    
    oggz_close (oggz);
    
    /* Print summary information */
    if (many_files)
      printf ("Filename: %s\n", infilename);
    fputs ("Content-Duration: ", stdout);
    ot_print_time ((double)info.duration / 1000.0);
    putchar ('\n');
    
    if (show_length) {
493 494 495
      fputs ("Content-Length: ", stdout);
      ot_print_bytes (info.length_total);
      putchar ('\n');
496 497 498 499 500 501
    }
    
    if (show_bitrate) {
      printf ("Content-Bitrate-Average: %ld bps\n",
	      oi_bitrate (info.length_total, info.duration));
    }
502

503 504 505 506
    oggzinfo_apply (oit_print, &info);
    
    oggzinfo_apply (oit_delete, &info);
    oggz_table_delete (info.tracks);
507

508
    if (optind < argc) puts (SEP);
509
  }
conrad's avatar
conrad committed
510

511 512 513 514 515
 exit_ok:
  exit (0);

 exit_err:
  exit (1);
andre's avatar
andre committed
516
}