ogginfo2.c 13.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/* Ogginfo
 *
 * A tool to describe ogg file contents and metadata.
 *
 * Copyright 2002 Michael Smith <msmith@layrinth.net.au>
 * 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 37 38 39 40 41 42 43
/* Different implementations have different format strings for 64 bit ints. */
#ifdef _WIN32
#define INT64FORMAT "%I64d"
#else
#define INT64FORMAT "%Ld"
#endif


/* TODO:
 *
 * - detect decreasing granulepos
 * - detect violations of muxing constraints
 * - better EOS detection (when EOS not explicitly set)
 * - detect granulepos 'gaps' (possibly vorbis-specific).
 * - check for serial number == (unsigned)-1 (will break some tools?)
 * - more options (e.g. less or more verbose)
 */

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
typedef struct _stream_processor {
    void (*process_page)(struct _stream_processor *, ogg_page *);
    void (*process_end)(struct _stream_processor *);
    int isillegal;
    int shownillegal;
    int isnew;

    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;

    long bytes;
    ogg_int64_t lastgranulepos;

    int doneheaders;
} misc_vorbis_info;

80 81 82 83
static int printinfo = 1;
static int printwarn = 1;
static int verbose = 1;

84 85 86 87 88 89 90 91 92 93
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;
}

94 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 123 124 125 126
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;

    if(!printwarn)
        return;

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

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

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

127 128 129
static void vorbis_process(stream_processor *stream, ogg_page *page )
{
    ogg_packet packet;
130
    misc_vorbis_info *inf = stream->data;
131 132 133 134 135
    int i, header=0;

    ogg_stream_pagein(&stream->os, page);

    while(ogg_stream_packetout(&stream->os, &packet) > 0) {
136 137 138
        if(inf->doneheaders < 3) {
            if(vorbis_synthesis_headerin(&inf->vi, &inf->vc, &packet) < 0) {
                warn(_("Warning: Could not decode vorbis header "
139 140 141 142
                       "packet - invalid vorbis stream (%d)\n"), stream->num);
                continue;
            }
            header = 1;
143 144 145 146 147 148 149 150 151 152 153 154 155
            inf->doneheaders++;
            if(inf->doneheaders == 3) {
                info(_("Vorbis headers parsed for stream %d, "
                       "information follows...\n"), stream->num);

                info(_("Version: %d\n"), inf->vi.version);
                info(_("Vendor: %s\n"), inf->vc.vendor);
                info(_("Channels: %d\n"), inf->vi.channels);
                info(_("Rate: %ld\n\n"), inf->vi.rate);

                if(inf->vi.bitrate_nominal >= 0)
                    info(_("Nominal bitrate: %f kb/s\n"), 
                            (double)inf->vi.bitrate_nominal / 1000.0);
156
                else
157
                    info(_("Nominal bitrate not set\n"));
158

159 160 161
                if(inf->vi.bitrate_upper >= 0)
                    info(_("Upper bitrate: %f kb/s\n"), 
                            (double)inf->vi.bitrate_upper / 1000.0);
162
                else
163
                    info(_("Upper bitrate not set\n"));
164

165 166 167
                if(inf->vi.bitrate_lower >= 0)
                    info(_("Lower bitrate: %f kb/s\n"), 
                            (double)inf->vi.bitrate_lower / 1000.0);
168
                else
169
                    info(_("Lower bitrate not set\n"));
170

171 172
                if(inf->vc.comments > 0)
                    info(_("User comments section follows...\n"));
173

174 175
                for(i=0; i < inf->vc.comments; i++)
                    info("\t%s\n", inf->vc.user_comments[i]);
176 177 178 179 180
            }
        }
    }

    if(!header) {
181 182 183 184 185 186 187 188 189
        ogg_int64_t gp = ogg_page_granulepos(page);
        if(gp > 0) {
            if(gp < inf->lastgranulepos)
                warn(_("Warning: granulepos in stream %d decreases from " 
                        INT64FORMAT " to " INT64FORMAT "\n"), stream->num,
                        inf->lastgranulepos, gp);
            inf->lastgranulepos = gp;
        }
        inf->bytes += page->header_len + page->body_len;
190 191 192 193 194
    }
}

static void vorbis_end(stream_processor *stream) 
{
195
    misc_vorbis_info *inf = stream->data;
196 197 198
    long minutes, seconds;
    double bitrate, time;

199
    time = (double)inf->lastgranulepos / inf->vi.rate;
200 201
    minutes = (long)time / 60;
    seconds = (long)time - minutes*60;
202
    bitrate = inf->bytes*8 / time / 1000.0;
203

204 205 206 207 208
    info(_("Vorbis stream %d:\n"
           "\tTotal data length: %ld bytes\n"
           "\tPlayback length: %ldm:%02lds\n"
           "\tAverage bitrate: %f kbps\n"), 
            stream->num,inf->bytes, minutes, seconds, bitrate);
209

210 211
    vorbis_comment_clear(&inf->vc);
    vorbis_info_clear(&inf->vi);
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232

    free(stream->data);
}

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) {
233
            warn(_("Warning: EOS not set on stream %d\n"), 
234
                    set->streams[i].num);
235
            set->streams[i].process_end(&set->streams[i]);
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
        }
        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;
}

256
static void other_start(stream_processor *stream, char *type)
257
{
258 259 260 261
    if(type)
        stream->type = type;
    else
        stream->type = "unknown";
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
    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;
    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;
                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?
     */
    if(streams_open(set) && !set->in_headers)
        invalid = 1;

    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;

    {
        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) {
344
            warn(_("Warning: Invalid header page, no packet found\n"));
345 346 347 348 349
            return NULL;
        }

        if(packet.bytes >= 7 && memcmp(packet.packet, "\001vorbis", 7)==0)
            vorbis_start(stream);
350 351 352 353
        else if(packet.bytes >= 8 && memcmp(packet.packet, "OggMIDI\0", 8)==0) 
            other_start(stream, "MIDI");
        else
            other_start(stream, NULL);
354 355 356

        res = ogg_stream_packetout(&stream->os, &packet);
        if(res > 0) {
357
            warn(_("Warning: Invalid header page in stream %d, "
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
                              "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;

   return stream;
}

static int get_next_page(FILE *f, ogg_sync_state *sync, ogg_page *page)
{
    int ret;
    char *buffer;
    int bytes;

    while((ret = ogg_sync_pageout(sync, page)) <= 0) {
        if(ret < 0)
381
            warn(_("Warning: Hole in data found. Corrupted ogg\n"));
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399

        buffer = ogg_sync_buffer(sync, CHUNK);
        bytes = fread(buffer, 1, CHUNK, f);
        ogg_sync_wrote(sync, bytes);
        if(bytes == 0)
            return 0;
    }

    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();

    if(!file) {
400
        error(_("Error opening input file \"%s\": %s\n"), filename,
401 402 403 404
                    strerror(errno));
        return;
    }

405
    info(_("Processing file \"%s\"...\n\n"), filename);
406 407 408 409 410 411 412

    ogg_sync_init(&sync);

    while(get_next_page(file, &sync, &page)) {
        stream_processor *p = find_stream_processor(processors, &page);

        if(!p) {
413
            error(_("Could not find a processor for stream, bailing\n"));
414 415 416 417
            return;
        }

        if(p->isillegal && !p->shownillegal) {
418 419
            warn(_("Warning: illegally placed page(s) for logical stream %d\n"
                   "This indicates a corrupt ogg file.\n"), p->num);
420 421 422 423 424
            p->shownillegal = 1;
            continue;
        }

        if(p->isnew) {
425
            info(_("New logical stream (#%d, serial: %08x): type %s\n"), 
426 427
                    p->num, p->serial, p->type);
            if(!p->start)
428
                warn(_("Warning: stream start flag not set on stream %d\n"),
429 430 431
                        p->num);
        }
        else if(p->start)
432
            warn(_("Warning: stream start flag found in mid-stream "
433 434 435 436 437 438 439 440
                      "on stream %d\n"), p->num);

        if(!p->isillegal) {
            p->process_page(p, &page);

            if(p->end) {
                if(p->process_end)
                    p->process_end(p);
441
                info(_("Logical stream %d ended\n"), p->num);
442 443 444 445 446 447 448 449 450 451 452 453
                p->isillegal = 1;
            }
        }
    }

    free_stream_set(processors);

    ogg_sync_clear(&sync);

    fclose(file);
}

454 455 456 457 458 459 460 461 462 463 464 465 466
static void usage(void) {
    printf(_("ogginfo 1.0\n"
             "(c) 2002 Michael Smith <msmith@labyrinth.net.au>\n"
             "\n"
             "Usage: ogginfo [flags] files1.ogg [file2.ogg ... fileN.ogg]\n"
             "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"));
}

467
int main(int argc, char **argv) {
468
    int f, ret;
469 470 471 472 473 474 475

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

    if(argc < 2) {
        fprintf(stderr, 
476
                _("Usage: ogginfo [flags] file1.ogg [file2.ogg ... fileN.ogg]\n"
477 478
                  "\n"
                  "Ogginfo is a tool for printing information about ogg files\n"
479 480
                  "and for diagnosing problems with them.\n"
                  "Full help shown with \"ogginfo -h\".\n"));
481 482 483
        exit(1);
    }

484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
    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) {
        fprintf(stderr, _("No input files specified. \"ogginfo -h\" for help\n"));
        return 1;
    }

    for(f=optind; f < argc; f++) {
509 510 511 512 513 514
        process_file(argv[f]);
    }

    return 0;
}