ogginfo2.c 24.8 KB
Newer Older
1 2 3 4
/* Ogginfo
 *
 * A tool to describe ogg file contents and metadata.
 *
5
 * Copyright 2002 Michael Smith <msmith@xiph.org>
6 7 8 9 10 11 12 13
 * Licensed under the GNU GPL, distributed with this program.
 */


#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
14 15
#include <stdarg.h>
#include <getopt.h>
16 17 18 19 20 21 22 23 24 25

#include <ogg/ogg.h>
#include <vorbis/codec.h>

#include <locale.h>
#include "utf8.h"
#include "i18n.h"

#define CHUNK 4500

26 27 28 29 30 31 32 33 34 35 36
struct vorbis_release {
    char *vendor_string;
    char *desc;
} releases[] = {
        {"Xiphophorus libVorbis I 20000508", "1.0 beta 1 or beta 2"},
        {"Xiphophorus libVorbis I 20001031", "1.0 beta 3"},
        {"Xiphophorus libVorbis I 20010225", "1.0 beta 4"},
        {"Xiphophorus libVorbis I 20010615", "1.0 rc1"},
        {"Xiphophorus libVorbis I 20010813", "1.0 rc2"},
        {"Xiphophorus libVorbis I 20011217", "1.0 rc3"},
        {"Xiphophorus libVorbis I 20011231", "1.0 rc3"},
Ralph Giles's avatar
Ralph Giles committed
37
        {"Xiph.Org libVorbis I 20020717", "1.0"},
Monty's avatar
 
Monty committed
38
        {"Xiph.Org libVorbis I 20030909", "1.0.1"},
39 40 41 42
        {NULL, NULL},
    };


43 44 45
/* TODO:
 *
 * - detect violations of muxing constraints
46 47
 * - detect granulepos 'gaps' (possibly vorbis-specific). (seperate from 
 *   serial-number gaps)
48 49
 */

50 51 52 53
typedef struct _stream_processor {
    void (*process_page)(struct _stream_processor *, ogg_page *);
    void (*process_end)(struct _stream_processor *);
    int isillegal;
54
    int constraint_violated;
55 56
    int shownillegal;
    int isnew;
57 58
    long seqno;
    int lostseq;
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82

    int start;
    int end;

    int num;
    char *type;

    ogg_uint32_t serial; /* must be 32 bit unsigned */
    ogg_stream_state os;
    void *data;
} stream_processor;

typedef struct {
    stream_processor *streams;
    int allocated;
    int used;

    int in_headers;
} stream_set;

typedef struct {
    vorbis_info vi;
    vorbis_comment vc;

Monty's avatar
 
Monty committed
83
    ogg_int64_t bytes;
84
    ogg_int64_t lastgranulepos;
85
    ogg_int64_t firstgranulepos;
86 87 88 89

    int doneheaders;
} misc_vorbis_info;

90 91 92 93
static int printinfo = 1;
static int printwarn = 1;
static int verbose = 1;

94 95
static int flawed;

96 97 98
#define CONSTRAINT_PAGE_AFTER_EOS   1
#define CONSTRAINT_MUXING_VIOLATED  2

99 100 101 102 103 104 105 106 107 108
static stream_set *create_stream_set(void) {
    stream_set *set = calloc(1, sizeof(stream_set));

    set->streams = calloc(5, sizeof(stream_processor));
    set->allocated = 5;
    set->used = 0;

    return set;
}

109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
static void info(char *format, ...) 
{
    va_list ap;

    if(!printinfo)
        return;

    va_start(ap, format);
    vfprintf(stdout, format, ap);
    va_end(ap);
}

static void warn(char *format, ...) 
{
    va_list ap;

125
    flawed = 1;
126 127 128 129 130 131 132 133 134 135 136 137
    if(!printwarn)
        return;

    va_start(ap, format);
    vfprintf(stdout, format, ap);
    va_end(ap);
}

static void error(char *format, ...) 
{
    va_list ap;

138 139
    flawed = 1;

140 141 142 143 144
    va_start(ap, format);
    vfprintf(stdout, format, ap);
    va_end(ap);
}

145 146 147
static void vorbis_process(stream_processor *stream, ogg_page *page )
{
    ogg_packet packet;
148
    misc_vorbis_info *inf = stream->data;
149
    int i, header=0;
150
    int k;
151 152

    ogg_stream_pagein(&stream->os, page);
153 154
    if(inf->doneheaders < 3)
        header = 1;
155 156

    while(ogg_stream_packetout(&stream->os, &packet) > 0) {
157 158 159
        if(inf->doneheaders < 3) {
            if(vorbis_synthesis_headerin(&inf->vi, &inf->vc, &packet) < 0) {
                warn(_("Warning: Could not decode vorbis header "
160 161 162
                       "packet - invalid vorbis stream (%d)\n"), stream->num);
                continue;
            }
163 164
            inf->doneheaders++;
            if(inf->doneheaders == 3) {
165
                if(ogg_page_granulepos(page) != 0 || ogg_stream_packetpeek(&stream->os, NULL) == 1)
166 167 168 169
                    warn(_("Warning: Vorbis stream %d does not have headers "
                           "correctly framed. Terminal header page contains "
                           "additional packets or has non-zero granulepos\n"),
                            stream->num);
170 171 172 173
                info(_("Vorbis headers parsed for stream %d, "
                       "information follows...\n"), stream->num);

                info(_("Version: %d\n"), inf->vi.version);
174 175 176 177 178 179 180 181 182 183 184
                k = 0;
                while(releases[k].vendor_string) {
                    if(!strcmp(inf->vc.vendor, releases[k].vendor_string)) {
                        info(_("Vendor: %s (%s)\n"), inf->vc.vendor, 
                                    releases[k].desc);
                        break;
                    }
                    k++;
                }
                if(!releases[k].vendor_string)
                    info(_("Vendor: %s\n"), inf->vc.vendor);
185 186 187
                info(_("Channels: %d\n"), inf->vi.channels);
                info(_("Rate: %ld\n\n"), inf->vi.rate);

188
                if(inf->vi.bitrate_nominal > 0)
189 190
                    info(_("Nominal bitrate: %f kb/s\n"), 
                            (double)inf->vi.bitrate_nominal / 1000.0);
191
                else
192
                    info(_("Nominal bitrate not set\n"));
193

194
                if(inf->vi.bitrate_upper > 0)
195 196
                    info(_("Upper bitrate: %f kb/s\n"), 
                            (double)inf->vi.bitrate_upper / 1000.0);
197
                else
198
                    info(_("Upper bitrate not set\n"));
199

200
                if(inf->vi.bitrate_lower > 0)
201 202
                    info(_("Lower bitrate: %f kb/s\n"), 
                            (double)inf->vi.bitrate_lower / 1000.0);
203
                else
204
                    info(_("Lower bitrate not set\n"));
205

206 207
                if(inf->vc.comments > 0)
                    info(_("User comments section follows...\n"));
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
                for(i=0; i < inf->vc.comments; i++) {
                    char *sep = strchr(inf->vc.user_comments[i], '=');
                    char *decoded;
                    int j;
                    int broken = 0;
                    unsigned char *val;
                    int bytes;
                    int remaining;

                    if(sep == NULL) {
                        warn(_("Warning: Comment %d in stream %d is invalidly "
                               "formatted, does not contain '=': \"%s\"\n"), 
                               i, stream->num, inf->vc.user_comments[i]);
                        continue;
                    }

                    for(j=0; j < sep-inf->vc.user_comments[i]; j++) {
                        if(inf->vc.user_comments[i][j] < 0x20 ||
                           inf->vc.user_comments[i][j] > 0x7D) {
                            warn(_("Warning: Invalid comment fieldname in "
                                   "comment %d (stream %d): \"%s\"\n"),
                                    i, stream->num, inf->vc.user_comments[i]);
                            broken = 1;
                            break;
                        }
                    }

                    if(broken)
                        continue;

                    val = inf->vc.user_comments[i];

                    j = sep-inf->vc.user_comments[i]+1;
                    while(j < inf->vc.comment_lengths[i])
                    {
                        remaining = inf->vc.comment_lengths[i] - j;
                        if((val[j] & 0x80) == 0)
                            bytes = 1;
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
                        else if((val[j] & 0x40) == 0x40) {
                            if((val[j] & 0x20) == 0)
                                bytes = 2;
                            else if((val[j] & 0x10) == 0)
                                bytes = 3;
                            else if((val[j] & 0x08) == 0)
                                bytes = 4;
                            else if((val[j] & 0x04) == 0)
                                bytes = 5;
                            else if((val[j] & 0x02) == 0)
                                bytes = 6;
                            else {
                                warn(_("Warning: Illegal UTF-8 sequence in "
                                       "comment %d (stream %d): length "
                                       "marker wrong\n"),
                                        i, stream->num);
                                broken = 1;
                                break;
                            }
                        }
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
                        else {
                            warn(_("Warning: Illegal UTF-8 sequence in comment "
                                   "%d (stream %d): length marker wrong\n"),
                                    i, stream->num);
                            broken = 1;
                            break;
                        }

                        if(bytes > remaining) {
                            warn(_("Warning: Illegal UTF-8 sequence in comment "
                                   "%d (stream %d): too few bytes\n"),
                                    i, stream->num);
                            broken = 1;
                            break;
                        }

                        switch(bytes) {
                            case 1:
                                /* No more checks needed */
                                break;
                            case 2:
                                if((val[j+1] & 0xC0) != 0x80)
                                    broken = 1;
                                if((val[j] & 0xFE) == 0xC0)
                                    broken = 1;
                                break;
                            case 3:
                                if(!((val[j] == 0xE0 && val[j+1] >= 0xA0 && 
                                        val[j+1] <= 0xBF && 
                                        (val[j+2] & 0xC0) == 0x80) ||
                                   (val[j] >= 0xE1 && val[j] <= 0xEC &&
                                        (val[j+1] & 0xC0) == 0x80 &&
                                        (val[j+2] & 0xC0) == 0x80) ||
                                   (val[j] == 0xED && val[j+1] >= 0x80 &&
                                        val[j+1] <= 0x9F &&
                                        (val[j+2] & 0xC0) == 0x80) ||
                                   (val[j] >= 0xEE && val[j] <= 0xEF &&
                                        (val[j+1] & 0xC0) == 0x80 &&
                                        (val[j+2] & 0xC0) == 0x80)))
                                    broken = 1;
                                if(val[j] == 0xE0 && (val[j+1] & 0xE0) == 0x80)
                                    broken = 1;
                                break;
                            case 4:
                                if(!((val[j] == 0xF0 && val[j+1] >= 0x90 &&
                                        val[j+1] <= 0xBF &&
                                        (val[j+2] & 0xC0) == 0x80 &&
                                        (val[j+3] & 0xC0) == 0x80) ||
                                     (val[j] >= 0xF1 && val[j] <= 0xF3 &&
                                        (val[j+1] & 0xC0) == 0x80 &&
                                        (val[j+2] & 0xC0) == 0x80 &&
                                        (val[j+3] & 0xC0) == 0x80) ||
                                     (val[j] == 0xF4 && val[j+1] >= 0x80 &&
                                        val[j+1] <= 0x8F &&
                                        (val[j+2] & 0xC0) == 0x80 &&
                                        (val[j+3] & 0xC0) == 0x80)))
                                    broken = 1;
                                if(val[j] == 0xF0 && (val[j+1] & 0xF0) == 0x80)
                                    broken = 1;
                                break;
                            /* 5 and 6 aren't actually allowed at this point*/  
                            case 5:
                                broken = 1;
                                break;
                            case 6:
                                broken = 1;
                                break;
                        }

                        if(broken) {
                            warn(_("Warning: Illegal UTF-8 sequence in comment "
                                   "%d (stream %d): invalid sequence\n"),
                                    i, stream->num);
                            broken = 1;
                            break;
                        }

                        j += bytes;
                    }

                    if(!broken) {
Michael Smith's avatar
Michael Smith committed
348
                        if(utf8_decode(sep+1, &decoded) < 0) {
349 350 351 352 353 354
                            warn(_("Warning: Failure in utf8 decoder. This "
                                   "should be impossible\n"));
                            continue;
                        }
                        *sep = 0;
                        info("\t%s=%s\n", inf->vc.user_comments[i], decoded);
Michael Smith's avatar
Michael Smith committed
355
                        free(decoded);
356 357
                    }
                }
358 359 360 361 362
            }
        }
    }

    if(!header) {
363 364 365
        ogg_int64_t gp = ogg_page_granulepos(page);
        if(gp > 0) {
            if(gp < inf->lastgranulepos)
366 367 368 369
#ifdef _WIN32
                warn(_("Warning: granulepos in stream %d decreases from %I64d to %I64d" ),
                        stream->num, inf->lastgranulepos, gp);
#else
calc's avatar
calc committed
370
                warn(_("Warning: granulepos in stream %d decreases from %lld to %lld" ),
371 372
                        stream->num, inf->lastgranulepos, gp);
#endif
373 374
            inf->lastgranulepos = gp;
        }
375 376 377 378 379
        else {
            warn(_("Negative granulepos on vorbis stream outside of headers. This file was created by a buggy encoder\n"));
        }
        if(inf->firstgranulepos < 0) { /* Not set yet */
        }
380
        inf->bytes += page->header_len + page->body_len;
381 382 383 384 385
    }
}

static void vorbis_end(stream_processor *stream) 
{
386
    misc_vorbis_info *inf = stream->data;
387
    long minutes, seconds, milliseconds;
388 389
    double bitrate, time;

390
    /* This should be lastgranulepos - startgranulepos, or something like that*/
391
    time = (double)inf->lastgranulepos / inf->vi.rate;
392 393
    minutes = (long)time / 60;
    seconds = (long)time - minutes*60;
394
    milliseconds = (long)((time - minutes*60 - seconds)*1000);
395
    bitrate = inf->bytes*8 / time / 1000.0;
396

397 398 399
#ifdef _WIN32
    info(_("Vorbis stream %d:\n"
           "\tTotal data length: %I64d bytes\n"
400
           "\tPlayback length: %ldm:%02ld.%03lds\n"
401
           "\tAverage bitrate: %f kbps\n"), 
402
            stream->num,inf->bytes, minutes, seconds, milliseconds, bitrate);
403
#else
404
    info(_("Vorbis stream %d:\n"
calc's avatar
calc committed
405
           "\tTotal data length: %lld bytes\n"
406
           "\tPlayback length: %ldm:%02ld.%03lds\n"
407
           "\tAverage bitrate: %f kbps\n"), 
408
            stream->num,inf->bytes, minutes, seconds, milliseconds, bitrate);
409
#endif
410

411 412
    vorbis_comment_clear(&inf->vc);
    vorbis_info_clear(&inf->vi);
413 414 415 416

    free(stream->data);
}

417 418 419 420 421
static void process_null(stream_processor *stream, ogg_page *page)
{
    /* This is for invalid streams. */
}

422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
static void process_other(stream_processor *stream, ogg_page *page )
{
    ogg_packet packet;

    ogg_stream_pagein(&stream->os, page);

    while(ogg_stream_packetout(&stream->os, &packet) > 0) {
        /* Should we do anything here? Currently, we don't */
    }
}
        

static void free_stream_set(stream_set *set)
{
    int i;
    for(i=0; i < set->used; i++) {
        if(!set->streams[i].end) {
439
            warn(_("Warning: EOS not set on stream %d\n"), 
440
                    set->streams[i].num);
441 442
            if(set->streams[i].process_end)
                set->streams[i].process_end(&set->streams[i]);
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
        }
        ogg_stream_clear(&set->streams[i].os);
    }

    free(set->streams);
    free(set);
}

static int streams_open(stream_set *set)
{
    int i;
    int res=0;
    for(i=0; i < set->used; i++) {
        if(!set->streams[i].end)
            res++;
    }

    return res;
}

463 464 465 466 467 468 469
static void null_start(stream_processor *stream)
{
    stream->process_end = NULL;
    stream->type = "invalid";
    stream->process_page = process_null;
}

470
static void other_start(stream_processor *stream, char *type)
471
{
472 473 474 475
    if(type)
        stream->type = type;
    else
        stream->type = "unknown";
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
    stream->process_page = process_other;
    stream->process_end = NULL;
}

static void vorbis_start(stream_processor *stream)
{
    misc_vorbis_info *info;

    stream->type = "vorbis";
    stream->process_page = vorbis_process;
    stream->process_end = vorbis_end;

    stream->data = calloc(1, sizeof(misc_vorbis_info));

    info = stream->data;

    vorbis_comment_init(&info->vc);
    vorbis_info_init(&info->vi);

}

static stream_processor *find_stream_processor(stream_set *set, ogg_page *page)
{
    ogg_uint32_t serial = ogg_page_serialno(page);
    int i, found = 0;
    int invalid = 0;
502
    int constraint = 0;
503 504 505 506 507 508 509 510 511 512 513 514
    stream_processor *stream;

    for(i=0; i < set->used; i++) {
        if(serial == set->streams[i].serial) {
            /* We have a match! */
            found = 1;
            stream = &(set->streams[i]);

            set->in_headers = 0;
            /* if we have detected EOS, then this can't occur here. */
            if(stream->end) {
                stream->isillegal = 1;
515
                stream->constraint_violated = CONSTRAINT_PAGE_AFTER_EOS;
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
                return stream;
            }

            stream->isnew = 0;
            stream->start = ogg_page_bos(page);
            stream->end = ogg_page_eos(page);
            stream->serial = serial;
            return stream;
        }
    }

    /* If there are streams open, and we've reached the end of the
     * headers, then we can't be starting a new stream.
     * XXX: might this sometimes catch ok streams if EOS flag is missing,
     * but the stream is otherwise ok?
     */
532 533
    if(streams_open(set) && !set->in_headers) {
        constraint = CONSTRAINT_MUXING_VIOLATED;
534
        invalid = 1;
535
    }
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551

    set->in_headers = 1;

    if(set->allocated < set->used)
        stream = &set->streams[set->used];
    else {
        set->allocated += 5;
        set->streams = realloc(set->streams, sizeof(stream_processor)*
                set->allocated);
        stream = &set->streams[set->used];
    }
    set->used++;
    stream->num = set->used; /* We count from 1 */

    stream->isnew = 1;
    stream->isillegal = invalid;
552
    stream->constraint_violated = constraint;
553 554 555 556 557 558 559 560 561 562

    {
        int res;
        ogg_packet packet;

        /* We end up processing the header page twice, but that's ok. */
        ogg_stream_init(&stream->os, serial);
        ogg_stream_pagein(&stream->os, page);
        res = ogg_stream_packetout(&stream->os, &packet);
        if(res <= 0) {
563
            warn(_("Warning: Invalid header page, no packet found\n"));
564
            null_start(stream);
565
        }
566
        else if(packet.bytes >= 7 && memcmp(packet.packet, "\001vorbis", 7)==0)
567
            vorbis_start(stream);
568 569 570 571
        else if(packet.bytes >= 8 && memcmp(packet.packet, "OggMIDI\0", 8)==0) 
            other_start(stream, "MIDI");
        else
            other_start(stream, NULL);
572 573 574

        res = ogg_stream_packetout(&stream->os, &packet);
        if(res > 0) {
575
            warn(_("Warning: Invalid header page in stream %d, "
576 577 578 579 580 581 582 583 584 585 586 587
                              "contains multiple packets\n"), stream->num);
        }

        /* re-init, ready for processing */
        ogg_stream_clear(&stream->os);
        ogg_stream_init(&stream->os, serial);
   }

   stream->start = ogg_page_bos(page);
   stream->end = ogg_page_eos(page);
   stream->serial = serial;

588 589 590 591 592
   if(stream->serial == 0 || stream->serial == -1) {
       info(_("Note: Stream %d has serial number %d, which is legal but may "
              "cause problems with some tools."), stream->num, stream->serial);
   }

593 594 595
   return stream;
}

596 597
static int get_next_page(FILE *f, ogg_sync_state *sync, ogg_page *page, 
        ogg_int64_t *written)
598 599 600 601 602 603 604
{
    int ret;
    char *buffer;
    int bytes;

    while((ret = ogg_sync_pageout(sync, page)) <= 0) {
        if(ret < 0)
605 606 607
#ifdef _WIN32
            warn(_("Warning: Hole in data found at approximate offset %I64d bytes. Corrupted ogg.\n"), *written);
#else
calc's avatar
calc committed
608
            warn(_("Warning: Hole in data found at approximate offset %lld bytes. Corrupted ogg.\n"), *written);
609
#endif
610 611 612

        buffer = ogg_sync_buffer(sync, CHUNK);
        bytes = fread(buffer, 1, CHUNK, f);
613 614
        if(bytes <= 0) {
            ogg_sync_wrote(sync, 0);
615
            return 0;
616 617 618
        }
        ogg_sync_wrote(sync, bytes);
        *written += bytes;
619 620 621 622 623 624 625 626 627 628
    }

    return 1;
}

static void process_file(char *filename) {
    FILE *file = fopen(filename, "rb");
    ogg_sync_state sync;
    ogg_page page;
    stream_set *processors = create_stream_set();
629 630
    int gotpage = 0;
    ogg_int64_t written = 0;
631 632

    if(!file) {
633
        error(_("Error opening input file \"%s\": %s\n"), filename,
634 635 636 637
                    strerror(errno));
        return;
    }

638
    printf(_("Processing file \"%s\"...\n\n"), filename);
639 640 641

    ogg_sync_init(&sync);

642
    while(get_next_page(file, &sync, &page, &written)) {
643
        stream_processor *p = find_stream_processor(processors, &page);
644
        gotpage = 1;
645 646

        if(!p) {
647
            error(_("Could not find a processor for stream, bailing\n"));
648 649 650 651
            return;
        }

        if(p->isillegal && !p->shownillegal) {
652 653 654 655 656 657 658 659 660 661 662 663 664
            char *constraint;
            switch(p->constraint_violated) {
                case CONSTRAINT_PAGE_AFTER_EOS:
                    constraint = _("Page found for stream after EOS flag");
                    break;
                case CONSTRAINT_MUXING_VIOLATED:
                    constraint = _("Ogg muxing constraints violated, new "
                                   "stream before EOS of all previous streams");
                    break;
                default:
                    constraint = _("Error unknown.");
            }

665
            warn(_("Warning: illegally placed page(s) for logical stream %d\n"
666 667
                   "This indicates a corrupt ogg file: %s.\n"), 
                    p->num, constraint);
668
            p->shownillegal = 1;
669 670 671 672 673
            /* If it's a new stream, we want to continue processing this page
             * anyway to suppress additional spurious errors
             */
            if(!p->isnew)
                continue;
674 675 676
        }

        if(p->isnew) {
677
            info(_("New logical stream (#%d, serial: %08x): type %s\n"), 
678 679
                    p->num, p->serial, p->type);
            if(!p->start)
680
                warn(_("Warning: stream start flag not set on stream %d\n"),
681 682 683
                        p->num);
        }
        else if(p->start)
684
            warn(_("Warning: stream start flag found in mid-stream "
685 686
                      "on stream %d\n"), p->num);

687 688 689 690 691 692 693 694 695 696 697
        if(p->seqno++ != ogg_page_pageno(&page)) {
            if(!p->lostseq) 
                warn(_("Warning: sequence number gap in stream %d. Got page "
                       "%ld when expecting page %ld. Indicates missing data.\n"
                       ), p->num, ogg_page_pageno(&page), p->seqno - 1);
            p->seqno = ogg_page_pageno(&page);
            p->lostseq = 1;
        }
        else
            p->lostseq = 0;

698 699 700 701 702 703
        if(!p->isillegal) {
            p->process_page(p, &page);

            if(p->end) {
                if(p->process_end)
                    p->process_end(p);
704
                info(_("Logical stream %d ended\n"), p->num);
705
                p->isillegal = 1;
706
                p->constraint_violated = CONSTRAINT_PAGE_AFTER_EOS;
707 708 709 710
            }
        }
    }

711 712 713 714
    if(!gotpage)
        error(_("Error: No ogg data found in file \"%s\".\n"
                "Input probably not ogg.\n"), filename);

715 716 717 718 719 720 721
    free_stream_set(processors);

    ogg_sync_clear(&sync);

    fclose(file);
}

722
static void usage(void) {
carsten's avatar
carsten committed
723 724
    printf(_("ogginfo 1.0.1\n"
             "(c) 2003 Michael Smith <msmith@xiph.org>\n"
725
             "\n"
Michael Smith's avatar
Michael Smith committed
726
             "Usage: ogginfo [flags] file1.ogg [file2.ogg ... fileN.ogg]\n"
727 728 729 730 731 732 733 734
             "Flags supported:\n"
             "\t-h Show this help message\n"
             "\t-q Make less verbose. Once will remove detailed informative\n"
             "\t   messages, two will remove warnings\n"
             "\t-v Make more verbose. This may enable more detailed checks\n"
             "\t   for some stream types.\n\n"));
}

735
int main(int argc, char **argv) {
736
    int f, ret;
737 738 739 740 741 742 743

    setlocale(LC_ALL, "");
    bindtextdomain(PACKAGE, LOCALEDIR);
    textdomain(PACKAGE);

    if(argc < 2) {
        fprintf(stderr, 
744
                _("Usage: ogginfo [flags] file1.ogg [file2.ogg ... fileN.ogg]\n"
745 746
                  "\n"
                  "Ogginfo is a tool for printing information about ogg files\n"
747 748
                  "and for diagnosing problems with them.\n"
                  "Full help shown with \"ogginfo -h\".\n"));
749 750 751
        exit(1);
    }

752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771
    while((ret = getopt(argc, argv, "hvq")) >= 0) {
        switch(ret) {
            case 'h':
                usage();
                return 0;
            case 'v':
                verbose++;
                break;
            case 'q':
                verbose--;
                break;
        }
    }

    if(verbose < 1)
        printinfo = 0;
    if(verbose < 0) 
        printwarn = 0;

    if(optind >= argc) {
772 773
        fprintf(stderr, 
                _("No input files specified. \"ogginfo -h\" for help\n"));
774 775 776
        return 1;
    }

777 778
    ret = 0;

779
    for(f=optind; f < argc; f++) {
780
        flawed = 0;
781
        process_file(argv[f]);
782 783
        if(flawed != 0)
            ret = flawed;
784 785
    }

786
    return ret;
787 788
}