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

9 10 11
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
12 13 14 15 16

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
17 18
#include <stdarg.h>
#include <getopt.h>
Ralph Giles's avatar
Ralph Giles committed
19
#include <math.h>
20 21 22 23

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

24 25 26 27
#ifdef HAVE_KATE
#include <kate/oggkate.h>
#endif

28 29 30 31
#include <locale.h>
#include "utf8.h"
#include "i18n.h"

32 33
#include "theora.h"

34 35
#define CHUNK 4500

36 37 38 39 40 41
#ifdef _WIN32
#define I64FORMAT "I64d"
#else
#define I64FORMAT "lld"
#endif

42 43 44 45 46 47 48 49 50 51 52
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
53
        {"Xiph.Org libVorbis I 20020717", "1.0"},
Monty's avatar
 
Monty committed
54
        {"Xiph.Org libVorbis I 20030909", "1.0.1"},
55 56
        {"Xiph.Org libVorbis I 20040629", "1.1.0"},
	{"Xiph.Org libVorbis I 20050304", "1.1.1"},
57 58
	{"Xiph.Org libVorbis I 20050304", "1.1.2"},
	{"Xiph.Org libVorbis I 20070622", "1.2.0"},
59
	{"Xiph.Org libVorbis I 20080501", "1.2.1"},
60 61 62 63
        {NULL, NULL},
    };


64 65 66
/* TODO:
 *
 * - detect violations of muxing constraints
67
 * - detect granulepos 'gaps' (possibly vorbis-specific). (seperate from
68
 *   serial-number gaps)
69 70
 */

71 72 73 74
typedef struct _stream_processor {
    void (*process_page)(struct _stream_processor *, ogg_page *);
    void (*process_end)(struct _stream_processor *);
    int isillegal;
75
    int constraint_violated;
76 77
    int shownillegal;
    int isnew;
78 79
    long seqno;
    int lostseq;
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103

    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
104
    ogg_int64_t bytes;
105
    ogg_int64_t lastgranulepos;
106
    ogg_int64_t firstgranulepos;
107 108 109 110

    int doneheaders;
} misc_vorbis_info;

111 112 113 114 115 116 117 118 119
typedef struct {
    theora_info ti;
    theora_comment tc;

    ogg_int64_t bytes;
    ogg_int64_t lastgranulepos;
    ogg_int64_t firstgranulepos;

    int doneheaders;
120 121

    ogg_int64_t framenum_expected;
122 123
} misc_theora_info;

124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
typedef struct {
#ifdef HAVE_KATE
    kate_info ki;
    kate_comment kc;
#else
    int num_headers;
#endif

    int major;
    int minor;
    char language[16];
    char category[16];

    ogg_int64_t bytes;
    ogg_int64_t lastgranulepos;
    ogg_int64_t firstgranulepos;

    int doneheaders;
} misc_kate_info;

static int printlots = 0;
145 146 147 148
static int printinfo = 1;
static int printwarn = 1;
static int verbose = 1;

149 150
static int flawed;

151 152 153
#define CONSTRAINT_PAGE_AFTER_EOS   1
#define CONSTRAINT_MUXING_VIOLATED  2

154 155 156 157 158 159 160 161 162 163
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;
}

164 165 166 167 168 169 170 171 172 173 174 175
static void info(char *format, ...) 
{
    va_list ap;

    if(!printinfo)
        return;

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

176
static void warn(char *format, ...)
177 178 179
{
    va_list ap;

180
    flawed = 1;
181 182 183 184 185 186 187 188
    if(!printwarn)
        return;

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

189
static void error(char *format, ...)
190 191 192
{
    va_list ap;

193 194
    flawed = 1;

195 196 197 198 199
    va_start(ap, format);
    vfprintf(stdout, format, ap);
    va_end(ap);
}

200
static void check_xiph_comment(stream_processor *stream, int i, const char *comment,
201
    int comment_length)
202 203 204 205 206 207 208 209 210 211
{
    char *sep = strchr(comment, '=');
    char *decoded;
    int j;
    int broken = 0;
    unsigned char *val;
    int bytes;
    int remaining;

    if(sep == NULL) {
212
        warn(_("WARNING: Comment %d in stream %d has invalid "
213 214 215 216 217 218 219
              "format, does not contain '=': \"%s\"\n"), 
              i, stream->num, comment);
             return;
    }

    for(j=0; j < sep-comment; j++) {
        if(comment[j] < 0x20 || comment[j] > 0x7D) {
220
            warn(_("WARNING: Invalid comment fieldname in "
221 222 223 224 225 226 227 228 229 230
                   "comment %d (stream %d): \"%s\"\n"),
                   i, stream->num, comment);
            broken = 1;
            break;
        }
    }

    if(broken)
	return;

Ralph Giles's avatar
Ralph Giles committed
231
    val = (unsigned char *)comment;
232

233 234
    j = sep-comment+1;
    while(j < comment_length)
235
    {
236
        remaining = comment_length - j;
237 238 239 240 241 242 243 244 245 246 247 248 249 250
        if((val[j] & 0x80) == 0)
            bytes = 1;
        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 {
251
                warn(_("WARNING: Illegal UTF-8 sequence in "
252 253 254 255 256 257 258
                    "comment %d (stream %d): length marker wrong\n"),
                    i, stream->num);
                broken = 1;
                break;
            }
        }
        else {
259
            warn(_("WARNING: Illegal UTF-8 sequence in comment "
260 261 262 263 264 265
                "%d (stream %d): length marker wrong\n"), i, stream->num);
            broken = 1;
            break;
        }

        if(bytes > remaining) {
266
            warn(_("WARNING: Illegal UTF-8 sequence in comment "
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
                "%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) {
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
             char *simple = malloc (comment_length + 1);
             char *seq = malloc (comment_length * 3 + 1);
             static char hex[] = {'0', '1', '2', '3', '4', '5', '6', '7', 
                                  '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
             int i, c1 = 0, c2 = 0;
             for (i = 0; i < comment_length; i++) {
               seq[c1++] = hex[((unsigned char)comment[i]) >> 4];
               seq[c1++] = hex[((unsigned char)comment[i]) & 0xf];
               seq[c1++] = ' ';

               if(comment[i] < 0x20 || comment[i] > 0x7D)
                 simple[c2++] = '?';
               else
                 simple[c2++] = comment[i];
             }
             seq[c1] = 0;
             simple[c2] = 0;
342
             warn(_("WARNING: Illegal UTF-8 sequence in comment "
343 344
                   "%d (stream %d): invalid sequence \"%s\": %s\n"), i, 
                   stream->num, simple, seq);
345
             broken = 1;
346 347
             free (simple);
             free (seq);
348 349 350 351 352 353 354 355
             break;
         }

         j += bytes;
     }

     if(!broken) {
         if(utf8_decode(sep+1, &decoded) < 0) {
356
             warn(_("WARNING: Failure in UTF-8 decoder. This should not be possible\n"));
357 358
             return;
	 }
359
         *sep = 0;
360 361 362 363
         if(!broken) {
           info("\t%s=%s\n", comment, decoded);
           free(decoded);
         }
364 365 366 367 368 369 370 371
     }
}

static void theora_process(stream_processor *stream, ogg_page *page)
{
    ogg_packet packet;
    misc_theora_info *inf = stream->data;
    int i, header=0;
372
    int res;
373 374 375 376 377

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

378 379 380
    while(1) {
        res = ogg_stream_packetout(&stream->os, &packet);
        if(res < 0) {
381
           warn(_("WARNING: discontinuity in stream (%d)\n"), stream->num);
382 383 384 385 386
           continue;
        }
        else if (res == 0)
            break;

387 388
        if(inf->doneheaders < 3) {
            if(theora_decode_header(&inf->ti, &inf->tc, &packet) < 0) {
389 390
                warn(_("WARNING: Could not decode Theora header "
                       "packet - invalid Theora stream (%d)\n"), stream->num);
391 392 393 394 395
                continue;
            }
            inf->doneheaders++;
            if(inf->doneheaders == 3) {
                if(ogg_page_granulepos(page) != 0 || ogg_stream_packetpeek(&stream->os, NULL) == 1)
396
                    warn(_("WARNING: Theora stream %d does not have headers "
397 398 399 400 401 402 403
                           "correctly framed. Terminal header page contains "
                           "additional packets or has non-zero granulepos\n"),
                            stream->num);
                info(_("Theora headers parsed for stream %d, "
                       "information follows...\n"), stream->num);

                info(_("Version: %d.%d.%d\n"), inf->ti.version_major, inf->ti.version_minor, inf->ti.version_subminor);
404

405 406 407
                info(_("Vendor: %s\n"), inf->tc.vendor);
                info(_("Width: %d\n"), inf->ti.frame_width);
                info(_("Height: %d\n"), inf->ti.frame_height);
408
		info(_("Total image: %d by %d, crop offset (%d, %d)\n"),
409 410 411 412 413 414 415 416 417
		    inf->ti.width, inf->ti.height, inf->ti.offset_x, inf->ti.offset_y);
		if(inf->ti.offset_x + inf->ti.frame_width > inf->ti.width)
		    warn(_("Frame offset/size invalid: width incorrect\n"));
		if(inf->ti.offset_y + inf->ti.frame_height > inf->ti.height)
		    warn(_("Frame offset/size invalid: height incorrect\n"));

		if(inf->ti.fps_numerator == 0 || inf->ti.fps_denominator == 0) 
		   warn(_("Invalid zero framerate\n"));
		else
418
		   info(_("Framerate %d/%d (%.02f fps)\n"), inf->ti.fps_numerator, inf->ti.fps_denominator, (float)inf->ti.fps_numerator/(float)inf->ti.fps_denominator);
419 420 421 422 423 424 425 426
		
		if(inf->ti.aspect_numerator == 0 || inf->ti.aspect_denominator == 0) 
		{
		    info(_("Aspect ratio undefined\n"));
		}	
		else
		{
		    float frameaspect = (float)inf->ti.frame_width/(float)inf->ti.frame_height * (float)inf->ti.aspect_numerator/(float)inf->ti.aspect_denominator; 
427
		    info(_("Pixel aspect ratio %d:%d (%f:1)\n"), inf->ti.aspect_numerator, inf->ti.aspect_denominator, (float)inf->ti.aspect_numerator/(float)inf->ti.aspect_denominator);
j's avatar
j committed
428
                    if(fabs(frameaspect - 4.0/3.0) < 0.02)
429
			info(_("Frame aspect 4:3\n"));
j's avatar
j committed
430
                    else if(fabs(frameaspect - 16.0/9.0) < 0.02)
431
			info(_("Frame aspect 16:9\n"));
432
		    else
j's avatar
j committed
433
			info(_("Frame aspect %f:1\n"), frameaspect);
434 435 436
		}

		if(inf->ti.colorspace == OC_CS_ITU_REC_470M)
437
		    info(_("Colourspace: Rec. ITU-R BT.470-6 System M (NTSC)\n")); 
438
		else if(inf->ti.colorspace == OC_CS_ITU_REC_470BG)
439
		    info(_("Colourspace: Rec. ITU-R BT.470-6 Systems B and G (PAL)\n")); 
440
		else
441
		    info(_("Colourspace unspecified\n"));
442 443

		if(inf->ti.pixelformat == OC_PF_420)
444
		    info(_("Pixel format 4:2:0\n"));
445
		else if(inf->ti.pixelformat == OC_PF_422)
446
		    info(_("Pixel format 4:2:2\n"));
447
		else if(inf->ti.pixelformat == OC_PF_444)
448
		    info(_("Pixel format 4:4:4\n"));
449
		else
450
		    warn(_("Pixel format invalid\n"));
451

452 453
		info(_("Target bitrate: %d kbps\n"), inf->ti.target_bitrate/1000);
		info(_("Nominal quality setting (0-63): %d\n"), inf->ti.quality);
454 455 456 457 458 459

                if(inf->tc.comments > 0)
                    info(_("User comments section follows...\n"));

                for(i=0; i < inf->tc.comments; i++) {
                    char *comment = inf->tc.user_comments[i];
460 461
		    check_xiph_comment(stream, i, comment, 
		            inf->tc.comment_lengths[i]);
462 463 464
		}
	    }
	}
Michael Smith's avatar
Michael Smith committed
465 466 467 468 469 470 471 472 473 474 475 476
        else {
            ogg_int64_t framenum;
            ogg_int64_t iframe,pframe;
            ogg_int64_t gp = packet.granulepos;

            if(gp > 0) {
                iframe=gp>>inf->ti.granule_shift;
                pframe=gp-(iframe<<inf->ti.granule_shift);
                framenum = iframe+pframe;
                if(inf->framenum_expected >= 0 && 
                    inf->framenum_expected != framenum)
                {
477
                    warn(_("WARNING: Expected frame %" I64FORMAT 
Michael Smith's avatar
Michael Smith committed
478 479 480 481 482 483 484 485 486
                           ", got %" I64FORMAT "\n"), 
                           inf->framenum_expected, framenum);
                }
                inf->framenum_expected = framenum + 1;
            }
            else if (inf->framenum_expected >= 0) {
                inf->framenum_expected++;
            }
        }
487 488 489 490 491 492
    }

    if(!header) {
        ogg_int64_t gp = ogg_page_granulepos(page);
        if(gp > 0) {
            if(gp < inf->lastgranulepos)
493
                warn(_("WARNING: granulepos in stream %d decreases from %" 
494
                        I64FORMAT " to %" I64FORMAT "\n"),
495 496 497 498 499 500 501 502 503 504 505 506 507 508
                        stream->num, inf->lastgranulepos, gp);
            inf->lastgranulepos = gp;
        }
        if(inf->firstgranulepos < 0) { /* Not set yet */
        }
        inf->bytes += page->header_len + page->body_len;
    }
}

static void theora_end(stream_processor *stream) 
{
    misc_theora_info *inf = stream->data;
    long minutes, seconds, milliseconds;
    double bitrate, time;
509 510 511 512
    int new_gp;
    new_gp = inf->ti.version_major > 3
       || (inf->ti.version_major == 3 && (inf->ti.version_minor > 2
       || (inf->ti.version_minor == 2 && inf->ti.version_subminor > 0)));
513 514

    /* This should be lastgranulepos - startgranulepos, or something like that*/
515 516
    ogg_int64_t iframe=inf->lastgranulepos>>inf->ti.granule_shift;
    ogg_int64_t pframe=inf->lastgranulepos-(iframe<<inf->ti.granule_shift);
517 518 519 520
    /* The granule position starts at 0 for stream version 3.2.0, but starts at
       1 for version 3.2.1 and above. In the former case, we need to add one
       to the final granule position to get the frame count. */
    time = (double)(iframe+pframe+!new_gp) /
521 522 523 524 525 526 527
	((float)inf->ti.fps_numerator/(float)inf->ti.fps_denominator);
    minutes = (long)time / 60;
    seconds = (long)time - minutes*60;
    milliseconds = (long)((time - minutes*60 - seconds)*1000);
    bitrate = inf->bytes*8 / time / 1000.0;

    info(_("Theora stream %d:\n"
528
           "\tTotal data length: %" I64FORMAT " bytes\n"
529 530 531 532 533 534 535 536 537 538 539
           "\tPlayback length: %ldm:%02ld.%03lds\n"
           "\tAverage bitrate: %f kb/s\n"), 
            stream->num,inf->bytes, minutes, seconds, milliseconds, bitrate);

    theora_comment_clear(&inf->tc);
    theora_info_clear(&inf->ti);

    free(stream->data);
}


540 541 542
static void vorbis_process(stream_processor *stream, ogg_page *page )
{
    ogg_packet packet;
543
    misc_vorbis_info *inf = stream->data;
544
    int i, header=0, packets=0;
545
    int k;
546
    int res;
547 548

    ogg_stream_pagein(&stream->os, page);
549 550
    if(inf->doneheaders < 3)
        header = 1;
551

552 553 554
    while(1) {
        res = ogg_stream_packetout(&stream->os, &packet);
        if(res < 0) {
555
           warn(_("WARNING: discontinuity in stream (%d)\n"), stream->num);
556 557 558 559 560
           continue;
        }
        else if (res == 0)
            break;

561
        packets++;
562 563
        if(inf->doneheaders < 3) {
            if(vorbis_synthesis_headerin(&inf->vi, &inf->vc, &packet) < 0) {
564
                warn(_("WARNING: Could not decode Vorbis header "
565
                       "packet %d - invalid Vorbis stream (%d)\n"), 
566
                        inf->doneheaders, stream->num);
567 568
                continue;
            }
569 570
            inf->doneheaders++;
            if(inf->doneheaders == 3) {
571
                if(ogg_page_granulepos(page) != 0 || ogg_stream_packetpeek(&stream->os, NULL) == 1)
572
                    warn(_("WARNING: Vorbis stream %d does not have headers "
573 574 575
                           "correctly framed. Terminal header page contains "
                           "additional packets or has non-zero granulepos\n"),
                            stream->num);
576 577 578 579
                info(_("Vorbis headers parsed for stream %d, "
                       "information follows...\n"), stream->num);

                info(_("Version: %d\n"), inf->vi.version);
580 581 582 583 584 585 586 587 588 589 590
                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);
591 592 593
                info(_("Channels: %d\n"), inf->vi.channels);
                info(_("Rate: %ld\n\n"), inf->vi.rate);

594
                if(inf->vi.bitrate_nominal > 0)
595 596
                    info(_("Nominal bitrate: %f kb/s\n"), 
                            (double)inf->vi.bitrate_nominal / 1000.0);
597
                else
598
                    info(_("Nominal bitrate not set\n"));
599

600
                if(inf->vi.bitrate_upper > 0)
601 602
                    info(_("Upper bitrate: %f kb/s\n"), 
                            (double)inf->vi.bitrate_upper / 1000.0);
603
                else
604
                    info(_("Upper bitrate not set\n"));
605

606
                if(inf->vi.bitrate_lower > 0)
607 608
                    info(_("Lower bitrate: %f kb/s\n"), 
                            (double)inf->vi.bitrate_lower / 1000.0);
609
                else
610
                    info(_("Lower bitrate not set\n"));
611

612 613
                if(inf->vc.comments > 0)
                    info(_("User comments section follows...\n"));
614

615
                for(i=0; i < inf->vc.comments; i++) {
616
                    char *comment = inf->vc.user_comments[i];
617 618
		    check_xiph_comment(stream, i, comment, 
		            inf->vc.comment_lengths[i]);
619
		}
620 621 622 623 624
            }
        }
    }

    if(!header) {
625 626 627
        ogg_int64_t gp = ogg_page_granulepos(page);
        if(gp > 0) {
            if(gp < inf->lastgranulepos)
628
                warn(_("WARNING: granulepos in stream %d decreases from %" 
629
                        I64FORMAT " to %" I64FORMAT "\n" ),
630
                        stream->num, inf->lastgranulepos, gp);
631 632
            inf->lastgranulepos = gp;
        }
633 634 635 636
        else if(packets) {
            /* Only do this if we saw at least one packet ending on this page.
             * It's legal (though very unusual) to have no packets in a page at
             * all - this is occasionally used to have an empty EOS page */
637
            warn(_("Negative or zero granulepos (%" I64FORMAT ") on Vorbis stream outside of headers. This file was created by a buggy encoder\n"), gp);
638 639 640
        }
        if(inf->firstgranulepos < 0) { /* Not set yet */
        }
641
        inf->bytes += page->header_len + page->body_len;
642 643 644 645 646
    }
}

static void vorbis_end(stream_processor *stream) 
{
647
    misc_vorbis_info *inf = stream->data;
648
    long minutes, seconds, milliseconds;
649 650
    double bitrate, time;

651
    /* This should be lastgranulepos - startgranulepos, or something like that*/
652
    time = (double)inf->lastgranulepos / inf->vi.rate;
653 654
    minutes = (long)time / 60;
    seconds = (long)time - minutes*60;
655
    milliseconds = (long)((time - minutes*60 - seconds)*1000);
656
    bitrate = inf->bytes*8 / time / 1000.0;
657

658
    info(_("Vorbis stream %d:\n"
659
           "\tTotal data length: %" I64FORMAT " bytes\n"
660
           "\tPlayback length: %ldm:%02ld.%03lds\n"
661
           "\tAverage bitrate: %f kb/s\n"), 
662
            stream->num,inf->bytes, minutes, seconds, milliseconds, bitrate);
663

664 665
    vorbis_comment_clear(&inf->vc);
    vorbis_info_clear(&inf->vi);
666 667 668 669

    free(stream->data);
}

670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687
static void kate_process(stream_processor *stream, ogg_page *page )
{
    ogg_packet packet;
    misc_kate_info *inf = stream->data;
    int header=0, packets=0;
    int res;
#ifdef HAVE_KATE
    int i;
    const char *encoding = NULL, *directionality = NULL;
#endif

    ogg_stream_pagein(&stream->os, page);
    if(!inf->doneheaders)
        header = 1;

    while(1) {
        res = ogg_stream_packetout(&stream->os, &packet);
        if(res < 0) {
688
           warn(_("WARNING: discontinuity in stream (%d)\n"), stream->num);
689 690 691 692 693 694 695 696 697 698
           continue;
        }
        else if (res == 0)
            break;

        packets++;
        if(!inf->doneheaders) {
#ifdef HAVE_KATE
            int ret = kate_ogg_decode_headerin(&inf->ki, &inf->kc, &packet);
            if(ret < 0) {
699 700
                warn(_("WARNING: Could not decode Kate header "
                       "packet %d - invalid Kate stream (%d)\n"), 
701 702 703 704 705 706 707 708
                        packet.packetno, stream->num);
                continue;
            }
            else if (ret > 0) {
                inf->doneheaders=1;
            }
#else
            /* if we're not building against libkate, do some limited checks */
709
            if (packet.bytes<64 || memcmp(packet.packet+1, "kate\0\0\0", 7)) {
710
                warn(_("WARNING: packet %d does not seem to be a Kate header - "
711
                       "invalid Kate stream (%d)\n"), 
712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
                        packet.packetno, stream->num);
                continue;
            }
            if (packet.packetno==inf->num_headers) {
                inf->doneheaders=1;
            }
#endif

            if (packet.packetno==0) {
#ifdef HAVE_KATE
                inf->major = inf->ki.bitstream_version_major;
                inf->minor = inf->ki.bitstream_version_minor;
                memcpy(inf->language, inf->ki.language, 16);
                inf->language[15] = 0;
                memcpy(inf->category, inf->ki.category, 16);
                inf->category[15] = 0;
#else
                inf->major = packet.packet[9];
                inf->minor = packet.packet[10];
                inf->num_headers = packet.packet[11];
                memcpy(inf->language, packet.packet+32, 16);
                inf->language[15] = 0;
                memcpy(inf->category, packet.packet+48, 16);
                inf->category[15] = 0;
#endif
            }

            if(inf->doneheaders) {
                if(ogg_page_granulepos(page) != 0 || ogg_stream_packetpeek(&stream->os, NULL) == 1)
741
                    warn(_("WARNING: Kate stream %d does not have headers "
742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825
                           "correctly framed. Terminal header page contains "
                           "additional packets or has non-zero granulepos\n"),
                            stream->num);
                info(_("Kate headers parsed for stream %d, "
                       "information follows...\n"), stream->num);

                info(_("Version: %d.%d\n"), inf->major, inf->minor);
#ifdef HAVE_KATE
                info(_("Vendor: %s\n"), inf->kc.vendor);
#endif

                if (*inf->language) {
                    info(_("Language: %s\n"), inf->language);
                }
                else {
                    info(_("No language set\n"));
                }
                if (*inf->category) {
                    info(_("Category: %s\n"), inf->category);
                }
                else {
                    info(_("No category set\n"));
                }

#ifdef HAVE_KATE
                switch (inf->ki.text_encoding) {
                  case kate_utf8: encoding=_("utf-8"); break;
                  default: encoding=NULL; break;
                }
                if (encoding) {
                    info(_("Character encoding: %s\n"),encoding);
                }
                else {
                    info(_("Unknown character encoding\n"));
                }

                if (printlots) {
                    switch (inf->ki.text_directionality) {
                      case kate_l2r_t2b: directionality=_("left to right, top to bottom"); break;
                      case kate_r2l_t2b: directionality=_("right to left, top to bottom"); break;
                      case kate_t2b_r2l: directionality=_("top to bottom, right to left"); break;
                      case kate_t2b_l2r: directionality=_("top to bottom, left to right"); break;
                      default: directionality=NULL; break;
                    }
                    if (directionality) {
                        info(_("Text directionality: %s\n"),directionality);
                    }
                    else {
                        info(_("Unknown text directionality\n"));
                    }

                    info("%u regions, %u styles, %u curves, %u motions, %u palettes,\n"
                         "%u bitmaps, %u font ranges, %u font mappings\n",
                         inf->ki.nregions, inf->ki.nstyles,
                         inf->ki.ncurves, inf->ki.nmotions,
                         inf->ki.npalettes, inf->ki.nbitmaps,
                         inf->ki.nfont_ranges, inf->ki.nfont_mappings);
                }

		if(inf->ki.gps_numerator == 0 || inf->ki.gps_denominator == 0) 
		   warn(_("Invalid zero granulepos rate\n"));
		else
		   info(_("Granulepos rate %d/%d (%.02f gps)\n"),
                       inf->ki.gps_numerator, inf->ki.gps_denominator,
                       (float)inf->ki.gps_numerator/(float)inf->ki.gps_denominator);
		
                if(inf->kc.comments > 0)
                    info(_("User comments section follows...\n"));

                for(i=0; i < inf->kc.comments; i++) {
                    const char *comment = inf->kc.user_comments[i];
		    check_xiph_comment(stream, i, comment, 
		            inf->kc.comment_lengths[i]);
		}
#endif
                info(_("\n"));
            }
        }
    }

    if(!header) {
        ogg_int64_t gp = ogg_page_granulepos(page);
        if(gp > 0) {
            if(gp < inf->lastgranulepos)
826
                warn(_("WARNING: granulepos in stream %d decreases from %" 
827 828 829 830 831 832 833 834
                        I64FORMAT " to %" I64FORMAT "\n" ),
                        stream->num, inf->lastgranulepos, gp);
            inf->lastgranulepos = gp;
        }
        else if(packets && gp<0) { /* zero granpos on data is valid for kate */
            /* Only do this if we saw at least one packet ending on this page.
             * It's legal (though very unusual) to have no packets in a page at
             * all - this is occasionally used to have an empty EOS page */
835
            warn(_("Negative granulepos (%" I64FORMAT ") on Kate stream outside of headers. This file was created by a buggy encoder\n"), gp);
836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877
        }
        if(inf->firstgranulepos < 0) { /* Not set yet */
        }
        inf->bytes += page->header_len + page->body_len;
    }
}

#ifdef HAVE_KATE
static void kate_end(stream_processor *stream) 
{
    misc_kate_info *inf = stream->data;
    long minutes, seconds, milliseconds;
    double bitrate, time;

    /* This should be lastgranulepos - startgranulepos, or something like that*/
    //time = (double)(inf->lastgranulepos>>inf->ki.granule_shift) * inf->ki.gps_denominator / inf->ki.gps_numerator;
    ogg_int64_t gbase=inf->lastgranulepos>>inf->ki.granule_shift;
    ogg_int64_t goffset=inf->lastgranulepos-(gbase<<inf->ki.granule_shift);
    time = (double)(gbase+goffset) / ((float)inf->ki.gps_numerator/(float)inf->ki.gps_denominator);
    minutes = (long)time / 60;
    seconds = (long)time - minutes*60;
    milliseconds = (long)((time - minutes*60 - seconds)*1000);
    bitrate = inf->bytes*8 / time / 1000.0;

    info(_("Kate stream %d:\n"
           "\tTotal data length: %" I64FORMAT " bytes\n"
           "\tPlayback length: %ldm:%02ld.%03lds\n"
           "\tAverage bitrate: %f kb/s\n"), 
            stream->num,inf->bytes, minutes, seconds, milliseconds, bitrate);

    kate_comment_clear(&inf->kc);
    kate_info_clear(&inf->ki);

    free(stream->data);
}
#else
static void kate_end(stream_processor *stream) 
{
}
#endif


878 879 880 881 882
static void process_null(stream_processor *stream, ogg_page *page)
{
    /* This is for invalid streams. */
}

883 884 885 886 887 888 889 890 891 892
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 */
    }
}
893

894 895 896 897 898 899

static void free_stream_set(stream_set *set)
{
    int i;
    for(i=0; i < set->used; i++) {
        if(!set->streams[i].end) {
900
            warn(_("WARNING: EOS not set on stream %d\n"), 
901
                    set->streams[i].num);
902 903
            if(set->streams[i].process_end)
                set->streams[i].process_end(&set->streams[i]);
904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923
        }
        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;
}

924 925 926 927 928 929 930
static void null_start(stream_processor *stream)
{
    stream->process_end = NULL;
    stream->type = "invalid";
    stream->process_page = process_null;
}

931
static void other_start(stream_processor *stream, char *type)
932
{
933 934 935 936
    if(type)
        stream->type = type;
    else
        stream->type = "unknown";
937 938 939 940
    stream->process_page = process_other;
    stream->process_end = NULL;
}

941 942 943 944 945 946 947 948 949 950
static void theora_start(stream_processor *stream)
{
    misc_theora_info *info;

    stream->type = "theora";
    stream->process_page = theora_process;
    stream->process_end = theora_end;

    stream->data = calloc(1, sizeof(misc_theora_info));
    info = stream->data;
951
    info->framenum_expected = -1;
952 953
}

954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970
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);

}

971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988
static void kate_start(stream_processor *stream)
{
    misc_kate_info *info;

    stream->type = "kate";
    stream->process_page = kate_process;
    stream->process_end = kate_end;

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

    info = stream->data;

#ifdef HAVE_KATE
    kate_comment_init(&info->kc);
    kate_info_init(&info->ki);
#endif
}

989 990 991
static stream_processor *find_stream_processor(stream_set *set, ogg_page *page)
{
    ogg_uint32_t serial = ogg_page_serialno(page);
Ralph Giles's avatar
Ralph Giles committed
992
    int i;
993
    int invalid = 0;
994
    int constraint = 0;
995 996 997 998 999 1000 1001 1002 1003 1004 1005
    stream_processor *stream;

    for(i=0; i < set->used; i++) {
        if(serial == set->streams[i].serial) {
            /* We have a match! */
            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;
1006
                stream->constraint_violated = CONSTRAINT_PAGE_AFTER_EOS;
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
                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?
     */
1023 1024
    if(streams_open(set) && !set->in_headers) {
        constraint = CONSTRAINT_MUXING_VIOLATED;
1025
        invalid = 1;
1026
    }
1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042

    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;
1043
    stream->constraint_violated = constraint;
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053

    {
        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) {
1054
            warn(_("WARNING: Invalid header page, no packet found\n"));
1055
            null_start(stream);
1056
        }
j's avatar
j committed
1057
        else if(packet.bytes >= 7 && memcmp(packet.packet, "\x01vorbis", 7)==0)
1058
            vorbis_start(stream);
ivo's avatar
ivo committed
1059
        else if(packet.bytes >= 7 && memcmp(packet.packet, "\x80theora", 7)==0)
1060
            theora_start(stream);
ivo's avatar
ivo committed
1061
        else if(packet.bytes >= 8 && memcmp(packet.packet, "OggMIDI\0", 8)==0)
1062
            other_start(stream, "MIDI");
1063
        else if(packet.bytes >= 5 && memcmp(packet.packet, "\177FLAC", 5)==0)
1064
            other_start(stream, "FLAC");
ivo's avatar
ivo committed
1065 1066
        else if(packet.bytes == 4 && memcmp(packet.packet, "fLaC", 4)==0)
            other_start(stream, "FLAC (legacy)");
ivo's avatar
ivo committed
1067
        else if(packet.bytes >= 8 && memcmp(packet.packet, "Speex   ", 8)==0)
1068
            other_start(stream, "speex");
ivo's avatar
ivo committed
1069
        else if(packet.bytes >= 8 && memcmp(packet.packet, "fishead\0", 8)==0)
j's avatar
j committed
1070
            other_start(stream, "skeleton");
1071
        else if(packet.bytes >= 5 && memcmp(packet.packet, "BBCD\0", 5)==0)
ivo's avatar
ivo committed
1072
            other_start(stream, "dirac");
1073
        else if(packet.bytes >= 8 && memcmp(packet.packet, "KW-DIRAC", 8)==0)
ivo's avatar
ivo committed
1074
            other_start(stream, "dirac (legacy)");
1075
        else if(packet.bytes >= 8 && memcmp(packet.packet, "\x80kate\0\0\0", 8)==0)
1076
            kate_start(stream);
1077 1078
        else
            other_start(stream, NULL);
1079 1080 1081

        res = ogg_stream_packetout(&stream->os, &packet);
        if(res > 0) {
1082
            warn(_("WARNING: Invalid header page in stream %d, "
1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094
                              "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;

1095 1096
   if(stream->serial == 0 || stream->serial == -1) {
       info(_("Note: Stream %d has serial number %d, which is legal but may "
1097 1098
              "cause problems with some tools.\n"), stream->num, 
               stream->serial);
1099 1100
   }

1101 1102 1103
   return stream;
}

1104 1105
static int get_next_page(FILE *f, ogg_sync_state *sync, ogg_page *page, 
        ogg_int64_t *written)
1106 1107 1108 1109 1110
{
    int ret;
    char *buffer;
    int bytes;

1111
    while((ret = ogg_sync_pageseek(sync, page)) <= 0) {
1112 1113 1114 1115 1116
        if(ret < 0) {
            /* unsynced, we jump over bytes to a possible capture - we don't need to read more just yet */
            warn(_("WARNING: Hole in data (%d bytes) found at approximate offset %" I64FORMAT " bytes. Corrupted Ogg.\n"), -ret, *written);
            continue;
        }
1117

1118
        /* zero return, we didn't have enough data to find a whole page, read */
1119 1120
        buffer = ogg_sync_buffer(sync, CHUNK);
        bytes = fread(buffer, 1, CHUNK, f);
1121 1122
        if(bytes <= 0) {
            ogg_sync_wrote(sync, 0);
1123
            return 0;
1124 1125 1126
        }
        ogg_sync_wrote(sync, bytes);
        *written += bytes;
1127 1128 1129 1130 1131 1132 1133 1134 1135 1136
    }

    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();
1137 1138
    int gotpage = 0;
    ogg_int64_t written = 0;
1139 1140

    if(!file) {
1141
        error(_("Error opening input file \"%s\": %s\n"), filename,
1142 1143 1144 1145
                    strerror(errno));
        return;
    }

1146
    printf(_("Processing file \"%s\"...\n\n"), filename);
1147 1148 1149

    ogg_sync_init(&sync);

1150
    while(get_next_page(file, &sync, &page, &written)) {
1151
        stream_processor *p = find_stream_processor(processors, &page);
1152
        gotpage = 1;
1153 1154

        if(!p) {
1155
            error(_("Could not find a processor for stream, bailing\n"));
1156 1157 1158 1159
            return;
        }

        if(p->isillegal && !p->shownillegal) {
1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172
            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.");
            }

1173 1174
            warn(_("WARNING: illegally placed page(s) for logical stream %d\n"
                   "This indicates a corrupt Ogg file: %s.\n"), 
1175
                    p->num, constraint);
1176
            p->shownillegal = 1;
1177 1178 1179 1180 1181
            /* If it's a new stream, we want to continue processing this page
             * anyway to suppress additional spurious errors
             */
            if(!p->isnew)
                continue;
1182 1183 1184
        }

        if(p->isnew) {
1185
            info(_("New logical stream (#%d, serial: %08x): type %s\n"), 
1186 1187
                    p->num, p->serial, p->type);
            if(!p->start)