oggz-validate.c 10.7 KB
Newer Older
conrad's avatar
conrad 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>
35
#include <string.h>
conrad's avatar
conrad committed
36 37 38 39 40 41 42 43 44 45 46 47 48
#include <getopt.h>
#include <errno.h>

#include <oggz/oggz.h>

#include "oggz_tools.h"

#define MAX_ERRORS 10

#define SUBSECONDS 1000.0

typedef ogg_int64_t timestamp_t;

49 50 51
typedef struct _OVData {
  OGGZ * writer;
  OggzTable * missing_eos;
52 53
  int theora_count;
  int audio_count;
54 55
} OVData;

conrad's avatar
conrad committed
56 57 58 59 60 61
typedef struct {
  int error;
  char * description;
} error_text;

static error_text errors[] = {
62 63
  {-20, "Packet belongs to unknown serialno"},
  {-24, "Granulepos decreasing within track"},
64 65
  {-5, "Multiple bos packets"},
  {-6, "Multiple eos packets"},
conrad's avatar
conrad committed
66 67 68 69 70 71 72 73 74 75
  {0, NULL}
};

static int multifile = 0;
static char * current_filename = NULL;
static timestamp_t current_timestamp = 0;
static int exit_status = 0;
static int nr_errors = 0;

static void
76
list_errors (void)
conrad's avatar
conrad committed
77 78 79
{
  int i = 0;

80 81
  printf ("  File contains no Ogg packets\n");
  printf ("  Packets out of order\n");
conrad's avatar
conrad committed
82 83 84
  for (i = 0; errors[i].error; i++) {
    printf ("  %s\n", errors[i].description);
  }
85 86 87 88
  printf ("  eos marked but no bos\n");
  printf ("  Missing eos packets\n");
  printf ("  Granulepos on page with no completed packets\n");
  printf ("  Theora video bos page after audio bos page\n");
89 90 91 92 93 94 95 96 97 98 99
}
static void
usage (char * progname)
{

  printf ("Usage: %s [options] filename ...\n", progname);
  printf ("Validate the Ogg framing of one or more files\n");
  printf ("\n%s detects the following errors in Ogg framing:\n", progname);

  list_errors ();

conrad's avatar
conrad committed
100 101
  printf ("\nMiscellaneous options\n");
  printf ("  -h, --help             Display this help and exit\n");
102
  printf ("  -E, --help-errors      List known types of error and exit\n");
conrad's avatar
conrad committed
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 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
  printf ("  -v, --version          Output version information and exit\n");
  printf ("\n");
  printf ("Exit status is 0 if all input files are valid, 1 otherwise.\n\n");
  printf ("Please report bugs to <ogg-dev@xiph.org>\n");
}

static int
log_error (void)
{
  if (multifile && nr_errors == 0) {
    fprintf (stderr, "%s: Error:\n", current_filename);
  }

  exit_status = 1;

  nr_errors++;
  if (nr_errors > MAX_ERRORS)
    return OGGZ_STOP_ERR;

  return OGGZ_STOP_OK;
}

static ogg_int64_t
gp_to_granule (OGGZ * oggz, long serialno, ogg_int64_t granulepos)
{
  int granuleshift;
  ogg_int64_t iframe, pframe;

  granuleshift = oggz_get_granuleshift (oggz, serialno);

  iframe = granulepos >> granuleshift;
  pframe = granulepos - (iframe << granuleshift);

  return (iframe + pframe);
}

static timestamp_t
gp_to_time (OGGZ * oggz, long serialno, ogg_int64_t granulepos)
{
  ogg_int64_t gr_n, gr_d;
  ogg_int64_t granule;

  if (granulepos == -1) return -1.0;
  if (oggz_get_granulerate (oggz, serialno, &gr_n, &gr_d) != 0) return -1.0;

  granule = gp_to_granule (oggz, serialno, granulepos);

  return (timestamp_t)((double)(granule * gr_d) / (double)gr_n);
}

153 154 155
static int
read_page (OGGZ * oggz, const ogg_page * og, long serialno, void * user_data)
{
156
  OVData * ovdata = (OVData *)user_data;
157
  ogg_int64_t gpos = ogg_page_granulepos((ogg_page *)og);
158
  const char * content_type;
159 160
  int ret = 0;

161 162 163 164 165 166 167 168 169 170 171 172 173
  if (ogg_page_bos ((ogg_page *)og)) {
    content_type = ot_page_identify (og, NULL);
    if (!strcmp (content_type, "Theora")) {
      ovdata->theora_count++;
      if (ovdata->audio_count > 0) {
        log_error ();
        fprintf (stderr, "serialno %010ld: Theora video bos page after audio bos page\n", serialno);
      }
    } else if (!strcmp (content_type, "Vorbis") || !strcmp (content_type, "Speex")) {
      ovdata->audio_count++;
    }
  }

174 175 176 177 178 179 180 181
  if(gpos != -1 && ogg_page_packets((ogg_page *)og) == 0) {
    ret = log_error ();
    fprintf (stderr, "serialno %010ld: granulepos %lld on page with no completed packets, must be -1\n", serialno, gpos);
  }

  return ret;
}

182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
static void
ovdata_init (OVData * ovdata)
{
  current_timestamp = 0;

  if ((ovdata->writer = oggz_new (OGGZ_WRITE|OGGZ_AUTO)) == NULL) {
    fprintf (stderr, "oggz-validate: unable to create new writer\n");
    exit (1);
  }

  ovdata->missing_eos = oggz_table_new ();
  ovdata->theora_count = 0;
  ovdata->audio_count = 0;
}

static void
ovdata_clear (OVData * ovdata)
{
  long serialno;
  int i, nr_missing_eos = 0;

  oggz_close (ovdata->writer);

  if (nr_errors <= MAX_ERRORS) {
    nr_missing_eos = oggz_table_size (ovdata->missing_eos);
    for (i = 0; i < nr_missing_eos; i++) {
      log_error ();
      oggz_table_nth (ovdata->missing_eos, i, &serialno);
      fprintf (stderr, "serialno %010ld: missing *** eos\n", serialno);
    }
  }

  oggz_table_delete (ovdata->missing_eos);
}

conrad's avatar
conrad committed
217 218 219
static int
read_packet (OGGZ * oggz, ogg_packet * op, long serialno, void * user_data)
{
220
  OVData * ovdata = (OVData *)user_data;
conrad's avatar
conrad committed
221 222
  timestamp_t timestamp;
  int flush;
223
  int ret = 0, feed_err = 0, i;
conrad's avatar
conrad committed
224

225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
  if (op->b_o_s) {
    oggz_table_insert (ovdata->missing_eos, serialno, (void *)0x1);
  }

  if (oggz_table_lookup (ovdata->missing_eos, serialno) == NULL) {
    ret = log_error ();
    fprintf (stderr, "serialno %010ld: missing *** bos\n", serialno);
  }

  if (op->e_o_s) {
    if (oggz_table_remove (ovdata->missing_eos, serialno) == -1) {
      ret = log_error ();
      fprintf (stderr, "serialno %010ld: *** eos marked but no bos\n",
	       serialno);
    }
  }

conrad's avatar
conrad committed
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
  timestamp = gp_to_time (oggz, serialno, op->granulepos);
  if (timestamp != -1.0) {
    if (timestamp < current_timestamp) {
      ret = log_error();
      ot_fprint_time (stderr, (double)timestamp/SUBSECONDS);
      fprintf (stderr, ": serialno %010ld: Packet out of order (previous ",
	       serialno);
      ot_fprint_time (stderr, (double)current_timestamp/SUBSECONDS);
      fprintf (stderr, ")\n");
    }
    current_timestamp = timestamp;
  }

  if (op->granulepos == -1) {
    flush = 0;
  } else {
    flush = OGGZ_FLUSH_AFTER;
  }

261
  if ((feed_err = oggz_write_feed (ovdata->writer, op, serialno, flush, NULL)) != 0) {
conrad's avatar
conrad committed
262 263 264 265 266 267
    ret = log_error ();
    if (timestamp == -1.0) {
      fprintf (stderr, "%ld", oggz_tell (oggz));
    } else {
      ot_fprint_time (stderr, (double)timestamp/SUBSECONDS);
    }
268 269 270 271 272 273 274 275 276 277 278 279
    fprintf (stderr, ": serialno %010ld: ", serialno);
    for (i = 0; errors[i].error; i++) {
      if (errors[i].error == feed_err) {
	fprintf (stderr, "%s\n", errors[i].description);
	break;
      }
    }
    if (errors[i].error == 0) {
      fprintf (stderr,
	       "Packet violates Ogg framing constraints: %d\n",
	       feed_err);
    }
conrad's avatar
conrad committed
280 281
  }

282 283 284 285 286 287
  /* If this is the last packet in a chain, reset ovdata */
  if (op->e_o_s && oggz_table_size (ovdata->missing_eos) == 0) {
    ovdata_clear (ovdata);
    ovdata_init (ovdata);
  }

conrad's avatar
conrad committed
288 289 290 291 292 293
  return ret;
}

static int
validate (char * filename)
{
294 295
  OGGZ * reader;
  OVData ovdata;
conrad's avatar
conrad committed
296
  unsigned char buf[1024];
297 298
  long n, nout = 0, bytes_written = 0;
  int active = 1;
conrad's avatar
conrad committed
299 300

  current_filename = filename;
301
  current_timestamp = 0;
conrad's avatar
conrad committed
302 303 304
  nr_errors = 0;

  /*printf ("oggz-validate: %s\n", filename);*/
305

306 307 308 309 310 311
  if (!strncmp (filename, "-", 2)) {
    if ((reader = oggz_open_stdio (stdin, OGGZ_READ|OGGZ_AUTO)) == NULL) {
      fprintf (stderr, "oggz-validate: unable to open stdin\n");
      return -1;
    }
  } else if ((reader = oggz_open (filename, OGGZ_READ|OGGZ_AUTO)) == NULL) {
conrad's avatar
conrad committed
312
    fprintf (stderr, "oggz-validate: unable to open file %s\n", filename);
313
    return -1;
conrad's avatar
conrad committed
314 315
  }

316
  ovdata_init (&ovdata);
conrad's avatar
conrad committed
317

318
  oggz_set_read_callback (reader, -1, read_packet, &ovdata);
319
  oggz_set_read_page (reader, -1, read_page, &ovdata);
conrad's avatar
conrad committed
320 321 322 323 324 325

  while (active && (n = oggz_read (reader, 1024)) != 0) {
    if (nr_errors > MAX_ERRORS) {
      fprintf (stderr,
	       "oggz-validate: maximum error count reached, bailing out ...\n");
      active = 0;
326 327 328
    } else while ((nout = oggz_write_output (ovdata.writer, buf, n)) > 0) {
      bytes_written += nout;
    }
conrad's avatar
conrad committed
329 330 331 332
  }

  oggz_close (reader);

333 334 335 336 337
  if (bytes_written == 0) {
    log_error ();
    fprintf (stderr, "File contains no Ogg packets\n");
  }

338
  ovdata_clear (&ovdata);
339

conrad's avatar
conrad committed
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
  return active ? 0 : -1;
}

int
main (int argc, char ** argv)
{
  int show_version = 0;
  int show_help = 0;

  char * progname;
  char * filename;
  int i = 1;

  ot_init();

  progname = argv[0];

  if (argc < 2) {
    usage (progname);
    return (1);
  }

  while (1) {
363
    char * optstring = "hvE";
conrad's avatar
conrad committed
364 365 366 367

#ifdef HAVE_GETOPT_LONG
    static struct option long_options[] = {
      {"help", no_argument, 0, 'h'},
368
      {"help-errors", no_argument, 0, 'E'},
conrad's avatar
conrad committed
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
      {"version", no_argument, 0, 'v'},
      {0,0,0,0}
    };

    i = getopt_long(argc, argv, optstring, long_options, NULL);
#else
    i = getopt (argc, argv, optstring);
#endif
    if (i == -1) {
      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;
393 394 395
    case 'E': /* list errors */
      show_help = 2;
      break;
conrad's avatar
conrad committed
396 397 398 399 400 401 402 403 404
    default:
      break;
    }
  }

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

405
  if (show_help == 1) {
conrad's avatar
conrad committed
406
    usage (progname);
407 408
  } else if (show_help == 2) {
    list_errors ();
conrad's avatar
conrad committed
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
  }

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

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

  if (argc-i > 2) multifile = 1;

  for (; i < argc; i++) {
    filename = argv[i];
424 425
    if (validate (filename) == -1)
      exit_status = 1;
conrad's avatar
conrad committed
426 427 428 429 430 431 432 433
  }

 exit_out:
  exit (exit_status);

 exit_err:
  exit (1);
}