comparison matroskadec.c @ 3692:e8f67b6063c3 libavformat

misc spelling/grammar fixes
author diego
date Tue, 05 Aug 2008 08:28:57 +0000
parents b91cad3c97e8
children 5825a6cfd3bd
comparison
equal deleted inserted replaced
3691:b91cad3c97e8 3692:e8f67b6063c3
1 /* 1 /*
2 * Matroska file demuxer 2 * Matroska file demuxer
3 * Copyright (c) 2003-2008 The ffmpeg Project 3 * Copyright (c) 2003-2008 The FFmpeg Project
4 * 4 *
5 * This file is part of FFmpeg. 5 * This file is part of FFmpeg.
6 * 6 *
7 * FFmpeg is free software; you can redistribute it and/or 7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public 8 * modify it under the terms of the GNU Lesser General Public
23 * @file matroskadec.c 23 * @file matroskadec.c
24 * Matroska file demuxer 24 * Matroska file demuxer
25 * by Ronald Bultje <rbultje@ronald.bitfreak.net> 25 * by Ronald Bultje <rbultje@ronald.bitfreak.net>
26 * with a little help from Moritz Bunkus <moritz@bunkus.org> 26 * with a little help from Moritz Bunkus <moritz@bunkus.org>
27 * totally reworked by Aurelien Jacobs <aurel@gnuage.org> 27 * totally reworked by Aurelien Jacobs <aurel@gnuage.org>
28 * Specs available on the matroska project page: http://www.matroska.org/. 28 * Specs available on the Matroska project page: http://www.matroska.org/.
29 */ 29 */
30 30
31 #include "avformat.h" 31 #include "avformat.h"
32 /* For codec_get_id(). */ 32 /* For codec_get_id(). */
33 #include "riff.h" 33 #include "riff.h"
174 } MatroskaLevel; 174 } MatroskaLevel;
175 175
176 typedef struct { 176 typedef struct {
177 AVFormatContext *ctx; 177 AVFormatContext *ctx;
178 178
179 /* ebml stuff */ 179 /* EBML stuff */
180 int num_levels; 180 int num_levels;
181 MatroskaLevel levels[EBML_MAX_DEPTH]; 181 MatroskaLevel levels[EBML_MAX_DEPTH];
182 int level_up; 182 int level_up;
183 183
184 uint64_t time_scale; 184 uint64_t time_scale;
191 EbmlList seekhead; 191 EbmlList seekhead;
192 192
193 /* byte position of the segment inside the stream */ 193 /* byte position of the segment inside the stream */
194 offset_t segment_start; 194 offset_t segment_start;
195 195
196 /* The packet queue. */ 196 /* the packet queue */
197 AVPacket **packets; 197 AVPacket **packets;
198 int num_packets; 198 int num_packets;
199 199
200 int done; 200 int done;
201 int has_cluster_id; 201 int has_cluster_id;
447 { MATROSKA_ID_CLUSTER, EBML_NEST, 0, 0, {.n=matroska_cluster} }, 447 { MATROSKA_ID_CLUSTER, EBML_NEST, 0, 0, {.n=matroska_cluster} },
448 { 0 } 448 { 0 }
449 }; 449 };
450 450
451 /* 451 /*
452 * Return: whether we reached the end of a level in the hierarchy or not 452 * Return: Whether we reached the end of a level in the hierarchy or not.
453 */ 453 */
454 static int ebml_level_end(MatroskaDemuxContext *matroska) 454 static int ebml_level_end(MatroskaDemuxContext *matroska)
455 { 455 {
456 ByteIOContext *pb = matroska->ctx->pb; 456 ByteIOContext *pb = matroska->ctx->pb;
457 offset_t pos = url_ftell(pb); 457 offset_t pos = url_ftell(pb);
470 * Read: an "EBML number", which is defined as a variable-length 470 * Read: an "EBML number", which is defined as a variable-length
471 * array of bytes. The first byte indicates the length by giving a 471 * array of bytes. The first byte indicates the length by giving a
472 * number of 0-bits followed by a one. The position of the first 472 * number of 0-bits followed by a one. The position of the first
473 * "one" bit inside the first byte indicates the length of this 473 * "one" bit inside the first byte indicates the length of this
474 * number. 474 * number.
475 * Returns: num. of bytes read. < 0 on error. 475 * Returns: number of bytes read, < 0 on error
476 */ 476 */
477 static int ebml_read_num(MatroskaDemuxContext *matroska, ByteIOContext *pb, 477 static int ebml_read_num(MatroskaDemuxContext *matroska, ByteIOContext *pb,
478 int max_size, uint64_t *number) 478 int max_size, uint64_t *number)
479 { 479 {
480 int len_mask = 0x80, read = 1, n = 1; 480 int len_mask = 0x80, read = 1, n = 1;
481 int64_t total = 0; 481 int64_t total = 0;
482 482
483 /* the first byte tells us the length in bytes - get_byte() can normally 483 /* The first byte tells us the length in bytes - get_byte() can normally
484 * return 0, but since that's not a valid first ebmlID byte, we can 484 * return 0, but since that's not a valid first ebmlID byte, we can
485 * use it safely here to catch EOS. */ 485 * use it safely here to catch EOS. */
486 if (!(total = get_byte(pb))) { 486 if (!(total = get_byte(pb))) {
487 /* we might encounter EOS here */ 487 /* we might encounter EOS here */
488 if (!url_feof(pb)) { 488 if (!url_feof(pb)) {
526 int n = 0; 526 int n = 0;
527 527
528 if (size < 1 || size > 8) 528 if (size < 1 || size > 8)
529 return AVERROR_INVALIDDATA; 529 return AVERROR_INVALIDDATA;
530 530
531 /* big-endian ordening; build up number */ 531 /* big-endian ordering; build up number */
532 *num = 0; 532 *num = 0;
533 while (n++ < size) 533 while (n++ < size)
534 *num = (*num << 8) | get_byte(pb); 534 *num = (*num << 8) | get_byte(pb);
535 535
536 return 0; 536 return 0;
557 * 0 is success, < 0 is failure. 557 * 0 is success, < 0 is failure.
558 */ 558 */
559 static int ebml_read_ascii(ByteIOContext *pb, int size, char **str) 559 static int ebml_read_ascii(ByteIOContext *pb, int size, char **str)
560 { 560 {
561 av_free(*str); 561 av_free(*str);
562 /* ebml strings are usually not 0-terminated, so we allocate one 562 /* EBML strings are usually not 0-terminated, so we allocate one
563 * byte more, read the string and NULL-terminate it ourselves. */ 563 * byte more, read the string and NULL-terminate it ourselves. */
564 if (!(*str = av_malloc(size + 1))) 564 if (!(*str = av_malloc(size + 1)))
565 return AVERROR(ENOMEM); 565 return AVERROR(ENOMEM);
566 if (get_buffer(pb, (uint8_t *) *str, size) != size) { 566 if (get_buffer(pb, (uint8_t *) *str, size) != size) {
567 av_free(*str); 567 av_free(*str);
613 return 0; 613 return 0;
614 } 614 }
615 615
616 /* 616 /*
617 * Read signed/unsigned "EBML" numbers. 617 * Read signed/unsigned "EBML" numbers.
618 * Return: number of bytes processed, < 0 on error. 618 * Return: number of bytes processed, < 0 on error
619 */ 619 */
620 static int matroska_ebmlnum_uint(MatroskaDemuxContext *matroska, 620 static int matroska_ebmlnum_uint(MatroskaDemuxContext *matroska,
621 uint8_t *data, uint32_t size, uint64_t *num) 621 uint8_t *data, uint32_t size, uint64_t *num)
622 { 622 {
623 ByteIOContext pb; 623 ByteIOContext pb;
767 { 767 {
768 uint64_t total = 0; 768 uint64_t total = 0;
769 int len_mask = 0x80, size = 1, n = 1; 769 int len_mask = 0x80, size = 1, n = 1;
770 char probe_data[] = "matroska"; 770 char probe_data[] = "matroska";
771 771
772 /* ebml header? */ 772 /* EBML header? */
773 if (AV_RB32(p->buf) != EBML_ID_HEADER) 773 if (AV_RB32(p->buf) != EBML_ID_HEADER)
774 return 0; 774 return 0;
775 775
776 /* length of header */ 776 /* length of header */
777 total = p->buf[4]; 777 total = p->buf[4];
783 return 0; 783 return 0;
784 total &= (len_mask - 1); 784 total &= (len_mask - 1);
785 while (n < size) 785 while (n < size)
786 total = (total << 8) | p->buf[4 + n++]; 786 total = (total << 8) | p->buf[4 + n++];
787 787
788 /* does the probe data contain the whole header? */ 788 /* Does the probe data contain the whole header? */
789 if (p->buf_size < 4 + size + total) 789 if (p->buf_size < 4 + size + total)
790 return 0; 790 return 0;
791 791
792 /* the header must contain the document type 'matroska'. For now, 792 /* The header must contain the document type 'matroska'. For now,
793 * we don't parse the whole header but simply check for the 793 * we don't parse the whole header but simply check for the
794 * availability of that array of characters inside the header. 794 * availability of that array of characters inside the header.
795 * Not fully fool-proof, but good enough. */ 795 * Not fully fool-proof, but good enough. */
796 for (n = 4+size; n <= 4+size+total-(sizeof(probe_data)-1); n++) 796 for (n = 4+size; n <= 4+size+total-(sizeof(probe_data)-1); n++)
797 if (!memcmp(p->buf+n, probe_data, sizeof(probe_data)-1)) 797 if (!memcmp(p->buf+n, probe_data, sizeof(probe_data)-1))
910 910
911 /* seek */ 911 /* seek */
912 if (url_fseek(matroska->ctx->pb, offset, SEEK_SET) != offset) 912 if (url_fseek(matroska->ctx->pb, offset, SEEK_SET) != offset)
913 continue; 913 continue;
914 914
915 /* we don't want to lose our seekhead level, so we add 915 /* We don't want to lose our seekhead level, so we add
916 * a dummy. This is a crude hack. */ 916 * a dummy. This is a crude hack. */
917 if (matroska->num_levels == EBML_MAX_DEPTH) { 917 if (matroska->num_levels == EBML_MAX_DEPTH) {
918 av_log(matroska->ctx, AV_LOG_INFO, 918 av_log(matroska->ctx, AV_LOG_INFO,
919 "Max EBML element depth (%d) reached, " 919 "Max EBML element depth (%d) reached, "
920 "cannot parse further.\n", EBML_MAX_DEPTH); 920 "cannot parse further.\n", EBML_MAX_DEPTH);
1373 laces = 1; 1373 laces = 1;
1374 lace_size = av_mallocz(sizeof(int)); 1374 lace_size = av_mallocz(sizeof(int));
1375 lace_size[0] = size; 1375 lace_size[0] = size;
1376 break; 1376 break;
1377 1377
1378 case 0x1: /* xiph lacing */ 1378 case 0x1: /* Xiph lacing */
1379 case 0x2: /* fixed-size lacing */ 1379 case 0x2: /* fixed-size lacing */
1380 case 0x3: /* EBML lacing */ 1380 case 0x3: /* EBML lacing */
1381 assert(size>0); // size <=3 is checked before size-=3 above 1381 assert(size>0); // size <=3 is checked before size-=3 above
1382 laces = (*data) + 1; 1382 laces = (*data) + 1;
1383 data += 1; 1383 data += 1;
1384 size -= 1; 1384 size -= 1;
1385 lace_size = av_mallocz(laces * sizeof(int)); 1385 lace_size = av_mallocz(laces * sizeof(int));
1386 1386
1387 switch ((flags & 0x06) >> 1) { 1387 switch ((flags & 0x06) >> 1) {
1388 case 0x1: /* xiph lacing */ { 1388 case 0x1: /* Xiph lacing */ {
1389 uint8_t temp; 1389 uint8_t temp;
1390 uint32_t total = 0; 1390 uint32_t total = 0;
1391 for (n = 0; res == 0 && n < laces - 1; n++) { 1391 for (n = 0; res == 0 && n < laces - 1; n++) {
1392 while (1) { 1392 while (1) {
1393 if (size == 0) { 1393 if (size == 0) {
1538 MatroskaCluster cluster = { 0 }; 1538 MatroskaCluster cluster = { 0 };
1539 EbmlList *blocks_list; 1539 EbmlList *blocks_list;
1540 MatroskaBlock *blocks; 1540 MatroskaBlock *blocks;
1541 int i, res; 1541 int i, res;
1542 if (matroska->has_cluster_id){ 1542 if (matroska->has_cluster_id){
1543 /* For the first cluster we parse, it's ID was already read as 1543 /* For the first cluster we parse, its ID was already read as
1544 part of matroska_read_header(), so don't read it again */ 1544 part of matroska_read_header(), so don't read it again */
1545 res = ebml_parse_id(matroska, matroska_clusters, 1545 res = ebml_parse_id(matroska, matroska_clusters,
1546 MATROSKA_ID_CLUSTER, &cluster); 1546 MATROSKA_ID_CLUSTER, &cluster);
1547 matroska->has_cluster_id = 0; 1547 matroska->has_cluster_id = 0;
1548 } else 1548 } else