00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00031 #include "avformat.h"
00032
00033 #include "riff.h"
00034 #include "intfloat_readwrite.h"
00035 #include "matroska.h"
00036
00037 typedef struct Track {
00038 MatroskaTrackType type;
00039
00040
00041
00042 uint32_t num;
00043 uint32_t uid;
00044 int stream_index;
00045
00046 char *name;
00047 char language[4];
00048
00049 char *codec_id;
00050 char *codec_name;
00051
00052 unsigned char *codec_priv;
00053 int codec_priv_size;
00054
00055 uint64_t default_duration;
00056 MatroskaTrackFlags flags;
00057 } MatroskaTrack;
00058
00059 typedef struct MatroskaVideoTrack {
00060 MatroskaTrack track;
00061
00062 int pixel_width;
00063 int pixel_height;
00064 int display_width;
00065 int display_height;
00066
00067 uint32_t fourcc;
00068
00069 MatroskaAspectRatioMode ar_mode;
00070 MatroskaEyeMode eye_mode;
00071
00072
00073 } MatroskaVideoTrack;
00074
00075 typedef struct MatroskaAudioTrack {
00076 MatroskaTrack track;
00077
00078 int channels;
00079 int bitdepth;
00080 int internal_samplerate;
00081 int samplerate;
00082 int block_align;
00083
00084
00085 int coded_framesize;
00086 int sub_packet_h;
00087 int frame_size;
00088 int sub_packet_size;
00089 int sub_packet_cnt;
00090 int pkt_cnt;
00091 uint8_t *buf;
00092
00093 } MatroskaAudioTrack;
00094
00095 typedef struct MatroskaSubtitleTrack {
00096 MatroskaTrack track;
00097
00098 } MatroskaSubtitleTrack;
00099
00100 #define MAX_TRACK_SIZE (FFMAX(FFMAX(sizeof(MatroskaVideoTrack), \
00101 sizeof(MatroskaAudioTrack)), \
00102 sizeof(MatroskaSubtitleTrack)))
00103
00104 typedef struct MatroskaLevel {
00105 uint64_t start;
00106 uint64_t length;
00107 } MatroskaLevel;
00108
00109 typedef struct MatroskaDemuxIndex {
00110 uint64_t pos;
00111 uint16_t track;
00112 uint64_t time;
00113 } MatroskaDemuxIndex;
00114
00115 typedef struct MatroskaDemuxContext {
00116 AVFormatContext *ctx;
00117
00118
00119 int num_levels;
00120 MatroskaLevel levels[EBML_MAX_DEPTH];
00121 int level_up;
00122
00123
00124 char *writing_app;
00125 char *muxing_app;
00126 int64_t created;
00127
00128
00129 int64_t time_scale;
00130
00131
00132
00133 int num_tracks;
00134 int num_streams;
00135 MatroskaTrack *tracks[MAX_STREAMS];
00136
00137
00138 uint32_t peek_id;
00139
00140
00141 offset_t segment_start;
00142
00143
00144 AVPacket **packets;
00145 int num_packets;
00146
00147
00148 int metadata_parsed;
00149 int index_parsed;
00150 int done;
00151
00152
00153 int num_indexes;
00154 MatroskaDemuxIndex *index;
00155
00156
00157 int skip_to_keyframe;
00158 AVStream *skip_to_stream;
00159 } MatroskaDemuxContext;
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174 static int
00175 ebml_read_element_level_up (MatroskaDemuxContext *matroska)
00176 {
00177 ByteIOContext *pb = matroska->ctx->pb;
00178 offset_t pos = url_ftell(pb);
00179 int num = 0;
00180
00181 while (matroska->num_levels > 0) {
00182 MatroskaLevel *level = &matroska->levels[matroska->num_levels - 1];
00183
00184 if (pos >= level->start + level->length) {
00185 matroska->num_levels--;
00186 num++;
00187 } else {
00188 break;
00189 }
00190 }
00191
00192 return num;
00193 }
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204 static int
00205 ebml_read_num (MatroskaDemuxContext *matroska,
00206 int max_size,
00207 uint64_t *number)
00208 {
00209 ByteIOContext *pb = matroska->ctx->pb;
00210 int len_mask = 0x80, read = 1, n = 1;
00211 int64_t total = 0;
00212
00213
00214
00215
00216 if (!(total = get_byte(pb))) {
00217
00218 if (!url_feof(pb)) {
00219 offset_t pos = url_ftell(pb);
00220 av_log(matroska->ctx, AV_LOG_ERROR,
00221 "Read error at pos. %"PRIu64" (0x%"PRIx64")\n",
00222 pos, pos);
00223 }
00224 return AVERROR(EIO);
00225 }
00226
00227
00228 while (read <= max_size && !(total & len_mask)) {
00229 read++;
00230 len_mask >>= 1;
00231 }
00232 if (read > max_size) {
00233 offset_t pos = url_ftell(pb) - 1;
00234 av_log(matroska->ctx, AV_LOG_ERROR,
00235 "Invalid EBML number size tag 0x%02x at pos %"PRIu64" (0x%"PRIx64")\n",
00236 (uint8_t) total, pos, pos);
00237 return AVERROR_INVALIDDATA;
00238 }
00239
00240
00241 total &= ~len_mask;
00242 while (n++ < read)
00243 total = (total << 8) | get_byte(pb);
00244
00245 *number = total;
00246
00247 return read;
00248 }
00249
00250
00251
00252
00253
00254
00255 static int
00256 ebml_read_element_id (MatroskaDemuxContext *matroska,
00257 uint32_t *id,
00258 int *level_up)
00259 {
00260 int read;
00261 uint64_t total;
00262
00263
00264 if (matroska->peek_id != 0) {
00265 if (level_up)
00266 *level_up = 0;
00267 *id = matroska->peek_id;
00268 return 0;
00269 }
00270
00271
00272 if ((read = ebml_read_num(matroska, 4, &total)) < 0)
00273 return read;
00274 *id = matroska->peek_id = total | (1 << (read * 7));
00275
00276
00277 if (level_up)
00278 *level_up = ebml_read_element_level_up(matroska);
00279
00280 return read;
00281 }
00282
00283
00284
00285
00286
00287
00288 static int
00289 ebml_read_element_length (MatroskaDemuxContext *matroska,
00290 uint64_t *length)
00291 {
00292
00293 matroska->peek_id = 0;
00294
00295
00296 return ebml_read_num(matroska, 8, length);
00297 }
00298
00299
00300
00301
00302
00303
00304
00305 static uint32_t
00306 ebml_peek_id (MatroskaDemuxContext *matroska,
00307 int *level_up)
00308 {
00309 uint32_t id;
00310
00311 if (ebml_read_element_id(matroska, &id, level_up) < 0)
00312 return 0;
00313
00314 return id;
00315 }
00316
00317
00318
00319
00320
00321
00322 static int
00323 ebml_read_seek (MatroskaDemuxContext *matroska,
00324 offset_t offset)
00325 {
00326 ByteIOContext *pb = matroska->ctx->pb;
00327
00328
00329 matroska->peek_id = 0;
00330
00331 return (url_fseek(pb, offset, SEEK_SET) == offset) ? 0 : -1;
00332 }
00333
00334
00335
00336
00337
00338
00339 static int
00340 ebml_read_skip (MatroskaDemuxContext *matroska)
00341 {
00342 ByteIOContext *pb = matroska->ctx->pb;
00343 uint32_t id;
00344 uint64_t length;
00345 int res;
00346
00347 if ((res = ebml_read_element_id(matroska, &id, NULL)) < 0 ||
00348 (res = ebml_read_element_length(matroska, &length)) < 0)
00349 return res;
00350
00351 url_fskip(pb, length);
00352
00353 return 0;
00354 }
00355
00356
00357
00358
00359
00360
00361 static int
00362 ebml_read_uint (MatroskaDemuxContext *matroska,
00363 uint32_t *id,
00364 uint64_t *num)
00365 {
00366 ByteIOContext *pb = matroska->ctx->pb;
00367 int n = 0, size, res;
00368 uint64_t rlength;
00369
00370 if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
00371 (res = ebml_read_element_length(matroska, &rlength)) < 0)
00372 return res;
00373 size = rlength;
00374 if (size < 1 || size > 8) {
00375 offset_t pos = url_ftell(pb);
00376 av_log(matroska->ctx, AV_LOG_ERROR,
00377 "Invalid uint element size %d at position %"PRId64" (0x%"PRIx64")\n",
00378 size, pos, pos);
00379 return AVERROR_INVALIDDATA;
00380 }
00381
00382
00383 *num = 0;
00384 while (n++ < size)
00385 *num = (*num << 8) | get_byte(pb);
00386
00387 return 0;
00388 }
00389
00390
00391
00392
00393
00394
00395 static int
00396 ebml_read_sint (MatroskaDemuxContext *matroska,
00397 uint32_t *id,
00398 int64_t *num)
00399 {
00400 ByteIOContext *pb = matroska->ctx->pb;
00401 int size, n = 1, negative = 0, res;
00402 uint64_t rlength;
00403
00404 if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
00405 (res = ebml_read_element_length(matroska, &rlength)) < 0)
00406 return res;
00407 size = rlength;
00408 if (size < 1 || size > 8) {
00409 offset_t pos = url_ftell(pb);
00410 av_log(matroska->ctx, AV_LOG_ERROR,
00411 "Invalid sint element size %d at position %"PRId64" (0x%"PRIx64")\n",
00412 size, pos, pos);
00413 return AVERROR_INVALIDDATA;
00414 }
00415 if ((*num = get_byte(pb)) & 0x80) {
00416 negative = 1;
00417 *num &= ~0x80;
00418 }
00419 while (n++ < size)
00420 *num = (*num << 8) | get_byte(pb);
00421
00422
00423 if (negative)
00424 *num = *num - (1LL << ((8 * size) - 1));
00425
00426 return 0;
00427 }
00428
00429
00430
00431
00432
00433
00434 static int
00435 ebml_read_float (MatroskaDemuxContext *matroska,
00436 uint32_t *id,
00437 double *num)
00438 {
00439 ByteIOContext *pb = matroska->ctx->pb;
00440 int size, res;
00441 uint64_t rlength;
00442
00443 if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
00444 (res = ebml_read_element_length(matroska, &rlength)) < 0)
00445 return res;
00446 size = rlength;
00447
00448 if (size == 4) {
00449 *num= av_int2flt(get_be32(pb));
00450 } else if(size==8){
00451 *num= av_int2dbl(get_be64(pb));
00452 } else{
00453 offset_t pos = url_ftell(pb);
00454 av_log(matroska->ctx, AV_LOG_ERROR,
00455 "Invalid float element size %d at position %"PRIu64" (0x%"PRIx64")\n",
00456 size, pos, pos);
00457 return AVERROR_INVALIDDATA;
00458 }
00459
00460 return 0;
00461 }
00462
00463
00464
00465
00466
00467
00468 static int
00469 ebml_read_ascii (MatroskaDemuxContext *matroska,
00470 uint32_t *id,
00471 char **str)
00472 {
00473 ByteIOContext *pb = matroska->ctx->pb;
00474 int size, res;
00475 uint64_t rlength;
00476
00477 if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
00478 (res = ebml_read_element_length(matroska, &rlength)) < 0)
00479 return res;
00480 size = rlength;
00481
00482
00483
00484 if (size < 0 || !(*str = av_malloc(size + 1))) {
00485 av_log(matroska->ctx, AV_LOG_ERROR, "Memory allocation failed\n");
00486 return AVERROR(ENOMEM);
00487 }
00488 if (get_buffer(pb, (uint8_t *) *str, size) != size) {
00489 offset_t pos = url_ftell(pb);
00490 av_log(matroska->ctx, AV_LOG_ERROR,
00491 "Read error at pos. %"PRIu64" (0x%"PRIx64")\n", pos, pos);
00492 return AVERROR(EIO);
00493 }
00494 (*str)[size] = '\0';
00495
00496 return 0;
00497 }
00498
00499
00500
00501
00502
00503
00504 static int
00505 ebml_read_utf8 (MatroskaDemuxContext *matroska,
00506 uint32_t *id,
00507 char **str)
00508 {
00509 return ebml_read_ascii(matroska, id, str);
00510 }
00511
00512
00513
00514
00515
00516
00517 static int
00518 ebml_read_date (MatroskaDemuxContext *matroska,
00519 uint32_t *id,
00520 int64_t *date)
00521 {
00522 return ebml_read_sint(matroska, id, date);
00523 }
00524
00525
00526
00527
00528
00529
00530
00531 static int
00532 ebml_read_master (MatroskaDemuxContext *matroska,
00533 uint32_t *id)
00534 {
00535 ByteIOContext *pb = matroska->ctx->pb;
00536 uint64_t length;
00537 MatroskaLevel *level;
00538 int res;
00539
00540 if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
00541 (res = ebml_read_element_length(matroska, &length)) < 0)
00542 return res;
00543
00544
00545 if (matroska->num_levels >= EBML_MAX_DEPTH) {
00546 av_log(matroska->ctx, AV_LOG_ERROR,
00547 "File moves beyond max. allowed depth (%d)\n", EBML_MAX_DEPTH);
00548 return AVERROR(ENOSYS);
00549 }
00550
00551
00552 level = &matroska->levels[matroska->num_levels++];
00553 level->start = url_ftell(pb);
00554 level->length = length;
00555
00556 return 0;
00557 }
00558
00559
00560
00561
00562
00563
00564 static int
00565 ebml_read_binary (MatroskaDemuxContext *matroska,
00566 uint32_t *id,
00567 uint8_t **binary,
00568 int *size)
00569 {
00570 ByteIOContext *pb = matroska->ctx->pb;
00571 uint64_t rlength;
00572 int res;
00573
00574 if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
00575 (res = ebml_read_element_length(matroska, &rlength)) < 0)
00576 return res;
00577 *size = rlength;
00578
00579 if (!(*binary = av_malloc(*size))) {
00580 av_log(matroska->ctx, AV_LOG_ERROR,
00581 "Memory allocation error\n");
00582 return AVERROR(ENOMEM);
00583 }
00584
00585 if (get_buffer(pb, *binary, *size) != *size) {
00586 offset_t pos = url_ftell(pb);
00587 av_log(matroska->ctx, AV_LOG_ERROR,
00588 "Read error at pos. %"PRIu64" (0x%"PRIx64")\n", pos, pos);
00589 return AVERROR(EIO);
00590 }
00591
00592 return 0;
00593 }
00594
00595
00596
00597
00598
00599
00600
00601 static int
00602 matroska_ebmlnum_uint (uint8_t *data,
00603 uint32_t size,
00604 uint64_t *num)
00605 {
00606 int len_mask = 0x80, read = 1, n = 1, num_ffs = 0;
00607 uint64_t total;
00608
00609 if (size <= 0)
00610 return AVERROR_INVALIDDATA;
00611
00612 total = data[0];
00613 while (read <= 8 && !(total & len_mask)) {
00614 read++;
00615 len_mask >>= 1;
00616 }
00617 if (read > 8)
00618 return AVERROR_INVALIDDATA;
00619
00620 if ((total &= (len_mask - 1)) == len_mask - 1)
00621 num_ffs++;
00622 if (size < read)
00623 return AVERROR_INVALIDDATA;
00624 while (n < read) {
00625 if (data[n] == 0xff)
00626 num_ffs++;
00627 total = (total << 8) | data[n];
00628 n++;
00629 }
00630
00631 if (read == num_ffs)
00632 *num = (uint64_t)-1;
00633 else
00634 *num = total;
00635
00636 return read;
00637 }
00638
00639
00640
00641
00642
00643 static int
00644 matroska_ebmlnum_sint (uint8_t *data,
00645 uint32_t size,
00646 int64_t *num)
00647 {
00648 uint64_t unum;
00649 int res;
00650
00651
00652 if ((res = matroska_ebmlnum_uint(data, size, &unum)) < 0)
00653 return res;
00654
00655
00656 if (unum == (uint64_t)-1)
00657 *num = INT64_MAX;
00658 else
00659 *num = unum - ((1LL << ((7 * res) - 1)) - 1);
00660
00661 return res;
00662 }
00663
00664
00665
00666
00667
00668
00669 static int
00670 ebml_read_header (MatroskaDemuxContext *matroska,
00671 char **doctype,
00672 int *version)
00673 {
00674 uint32_t id;
00675 int level_up, res = 0;
00676
00677
00678 if (doctype)
00679 *doctype = NULL;
00680 if (version)
00681 *version = 1;
00682
00683 if (!(id = ebml_peek_id(matroska, &level_up)) ||
00684 level_up != 0 || id != EBML_ID_HEADER) {
00685 av_log(matroska->ctx, AV_LOG_ERROR,
00686 "This is not an EBML file (id=0x%x/0x%x)\n", id, EBML_ID_HEADER);
00687 return AVERROR_INVALIDDATA;
00688 }
00689 if ((res = ebml_read_master(matroska, &id)) < 0)
00690 return res;
00691
00692 while (res == 0) {
00693 if (!(id = ebml_peek_id(matroska, &level_up)))
00694 return AVERROR(EIO);
00695
00696
00697 if (level_up)
00698 break;
00699
00700 switch (id) {
00701
00702 case EBML_ID_EBMLREADVERSION: {
00703 uint64_t num;
00704
00705 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
00706 return res;
00707 if (num > EBML_VERSION) {
00708 av_log(matroska->ctx, AV_LOG_ERROR,
00709 "EBML version %"PRIu64" (> %d) is not supported\n",
00710 num, EBML_VERSION);
00711 return AVERROR_INVALIDDATA;
00712 }
00713 break;
00714 }
00715
00716
00717 case EBML_ID_EBMLMAXSIZELENGTH: {
00718 uint64_t num;
00719
00720 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
00721 return res;
00722 if (num > sizeof(uint64_t)) {
00723 av_log(matroska->ctx, AV_LOG_ERROR,
00724 "Integers of size %"PRIu64" (> %zd) not supported\n",
00725 num, sizeof(uint64_t));
00726 return AVERROR_INVALIDDATA;
00727 }
00728 break;
00729 }
00730
00731
00732 case EBML_ID_EBMLMAXIDLENGTH: {
00733 uint64_t num;
00734
00735 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
00736 return res;
00737 if (num > sizeof(uint32_t)) {
00738 av_log(matroska->ctx, AV_LOG_ERROR,
00739 "IDs of size %"PRIu64" (> %zu) not supported\n",
00740 num, sizeof(uint32_t));
00741 return AVERROR_INVALIDDATA;
00742 }
00743 break;
00744 }
00745
00746 case EBML_ID_DOCTYPE: {
00747 char *text;
00748
00749 if ((res = ebml_read_ascii(matroska, &id, &text)) < 0)
00750 return res;
00751 if (doctype) {
00752 if (*doctype)
00753 av_free(*doctype);
00754 *doctype = text;
00755 } else
00756 av_free(text);
00757 break;
00758 }
00759
00760 case EBML_ID_DOCTYPEREADVERSION: {
00761 uint64_t num;
00762
00763 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
00764 return res;
00765 if (version)
00766 *version = num;
00767 break;
00768 }
00769
00770 default:
00771 av_log(matroska->ctx, AV_LOG_INFO,
00772 "Unknown data type 0x%x in EBML header", id);
00773
00774
00775 case EBML_ID_VOID:
00776
00777
00778 case EBML_ID_EBMLVERSION:
00779 case EBML_ID_DOCTYPEVERSION:
00780 res = ebml_read_skip (matroska);
00781 break;
00782 }
00783 }
00784
00785 return 0;
00786 }
00787
00788
00789 static int
00790 matroska_find_track_by_num (MatroskaDemuxContext *matroska,
00791 int num)
00792 {
00793 int i;
00794
00795 for (i = 0; i < matroska->num_tracks; i++)
00796 if (matroska->tracks[i]->num == num)
00797 return i;
00798
00799 return -1;
00800 }
00801
00802
00803
00804
00805
00806
00807
00808 static int
00809 matroska_deliver_packet (MatroskaDemuxContext *matroska,
00810 AVPacket *pkt)
00811 {
00812 if (matroska->num_packets > 0) {
00813 memcpy(pkt, matroska->packets[0], sizeof(AVPacket));
00814 av_free(matroska->packets[0]);
00815 if (matroska->num_packets > 1) {
00816 memmove(&matroska->packets[0], &matroska->packets[1],
00817 (matroska->num_packets - 1) * sizeof(AVPacket *));
00818 matroska->packets =
00819 av_realloc(matroska->packets, (matroska->num_packets - 1) *
00820 sizeof(AVPacket *));
00821 } else {
00822 av_freep(&matroska->packets);
00823 }
00824 matroska->num_packets--;
00825 return 0;
00826 }
00827
00828 return -1;
00829 }
00830
00831
00832
00833
00834
00835
00836 static void
00837 matroska_queue_packet (MatroskaDemuxContext *matroska,
00838 AVPacket *pkt)
00839 {
00840 matroska->packets =
00841 av_realloc(matroska->packets, (matroska->num_packets + 1) *
00842 sizeof(AVPacket *));
00843 matroska->packets[matroska->num_packets] = pkt;
00844 matroska->num_packets++;
00845 }
00846
00847
00848
00849
00850 static void
00851 matroska_clear_queue (MatroskaDemuxContext *matroska)
00852 {
00853 if (matroska->packets) {
00854 int n;
00855 for (n = 0; n < matroska->num_packets; n++) {
00856 av_free_packet(matroska->packets[n]);
00857 av_free(matroska->packets[n]);
00858 }
00859 av_free(matroska->packets);
00860 matroska->packets = NULL;
00861 matroska->num_packets = 0;
00862 }
00863 }
00864
00865
00866
00867
00868
00869
00870 static int
00871 matroska_probe (AVProbeData *p)
00872 {
00873 uint64_t total = 0;
00874 int len_mask = 0x80, size = 1, n = 1;
00875 uint8_t probe_data[] = { 'm', 'a', 't', 'r', 'o', 's', 'k', 'a' };
00876
00877
00878 if (AV_RB32(p->buf) != EBML_ID_HEADER)
00879 return 0;
00880
00881
00882 total = p->buf[4];
00883 while (size <= 8 && !(total & len_mask)) {
00884 size++;
00885 len_mask >>= 1;
00886 }
00887 if (size > 8)
00888 return 0;
00889 total &= (len_mask - 1);
00890 while (n < size)
00891 total = (total << 8) | p->buf[4 + n++];
00892
00893
00894 if (p->buf_size < 4 + size + total)
00895 return 0;
00896
00897
00898
00899
00900
00901 for (n = 4 + size; n <= 4 + size + total - sizeof(probe_data); n++)
00902 if (!memcmp (&p->buf[n], probe_data, sizeof(probe_data)))
00903 return AVPROBE_SCORE_MAX;
00904
00905 return 0;
00906 }
00907
00908
00909
00910
00911
00912 static int
00913 matroska_parse_info (MatroskaDemuxContext *matroska)
00914 {
00915 int res = 0;
00916 uint32_t id;
00917
00918 av_log(matroska->ctx, AV_LOG_DEBUG, "Parsing info...\n");
00919
00920 while (res == 0) {
00921 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
00922 res = AVERROR(EIO);
00923 break;
00924 } else if (matroska->level_up) {
00925 matroska->level_up--;
00926 break;
00927 }
00928
00929 switch (id) {
00930
00931 case MATROSKA_ID_TIMECODESCALE: {
00932 uint64_t num;
00933 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
00934 break;
00935 matroska->time_scale = num;
00936 break;
00937 }
00938
00939 case MATROSKA_ID_DURATION: {
00940 double num;
00941 if ((res = ebml_read_float(matroska, &id, &num)) < 0)
00942 break;
00943 matroska->ctx->duration = num * matroska->time_scale * 1000 / AV_TIME_BASE;
00944 break;
00945 }
00946
00947 case MATROSKA_ID_TITLE: {
00948 char *text;
00949 if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
00950 break;
00951 strncpy(matroska->ctx->title, text,
00952 sizeof(matroska->ctx->title)-1);
00953 av_free(text);
00954 break;
00955 }
00956
00957 case MATROSKA_ID_WRITINGAPP: {
00958 char *text;
00959 if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
00960 break;
00961 matroska->writing_app = text;
00962 break;
00963 }
00964
00965 case MATROSKA_ID_MUXINGAPP: {
00966 char *text;
00967 if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
00968 break;
00969 matroska->muxing_app = text;
00970 break;
00971 }
00972
00973 case MATROSKA_ID_DATEUTC: {
00974 int64_t time;
00975 if ((res = ebml_read_date(matroska, &id, &time)) < 0)
00976 break;
00977 matroska->created = time;
00978 break;
00979 }
00980
00981 default:
00982 av_log(matroska->ctx, AV_LOG_INFO,
00983 "Unknown entry 0x%x in info header\n", id);
00984
00985
00986 case EBML_ID_VOID:
00987 res = ebml_read_skip(matroska);
00988 break;
00989 }
00990
00991 if (matroska->level_up) {
00992 matroska->level_up--;
00993 break;
00994 }
00995 }
00996
00997 return res;
00998 }
00999
01000 static int
01001 matroska_add_stream (MatroskaDemuxContext *matroska)
01002 {
01003 int res = 0;
01004 uint32_t id;
01005 MatroskaTrack *track;
01006
01007 av_log(matroska->ctx, AV_LOG_DEBUG, "parsing track, adding stream..,\n");
01008
01009
01010 track = av_mallocz(MAX_TRACK_SIZE);
01011 matroska->num_tracks++;
01012 strcpy(track->language, "eng");
01013
01014
01015 if ((res = ebml_read_master(matroska, &id)) < 0)
01016 return res;
01017
01018
01019 while (res == 0) {
01020 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
01021 res = AVERROR(EIO);
01022 break;
01023 } else if (matroska->level_up > 0) {
01024 matroska->level_up--;
01025 break;
01026 }
01027
01028 switch (id) {
01029
01030 case MATROSKA_ID_TRACKNUMBER: {
01031 uint64_t num;
01032 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
01033 break;
01034 track->num = num;
01035 break;
01036 }
01037
01038
01039 case MATROSKA_ID_TRACKUID: {
01040 uint64_t num;
01041 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
01042 break;
01043 track->uid = num;
01044 break;
01045 }
01046
01047
01048 case MATROSKA_ID_TRACKTYPE: {
01049 uint64_t num;
01050 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
01051 break;
01052 if (track->type && track->type != num) {
01053 av_log(matroska->ctx, AV_LOG_INFO,
01054 "More than one tracktype in an entry - skip\n");
01055 break;
01056 }
01057 track->type = num;
01058
01059 switch (track->type) {
01060 case MATROSKA_TRACK_TYPE_VIDEO:
01061 case MATROSKA_TRACK_TYPE_AUDIO:
01062 case MATROSKA_TRACK_TYPE_SUBTITLE:
01063 break;
01064 case MATROSKA_TRACK_TYPE_COMPLEX:
01065 case MATROSKA_TRACK_TYPE_LOGO:
01066 case MATROSKA_TRACK_TYPE_CONTROL:
01067 default:
01068 av_log(matroska->ctx, AV_LOG_INFO,
01069 "Unknown or unsupported track type 0x%x\n",
01070 track->type);
01071 track->type = 0;
01072 break;
01073 }
01074 matroska->tracks[matroska->num_tracks - 1] = track;
01075 break;
01076 }
01077
01078
01079 case MATROSKA_ID_TRACKVIDEO: {
01080 MatroskaVideoTrack *videotrack;
01081 if (!track->type)
01082 track->type = MATROSKA_TRACK_TYPE_VIDEO;
01083 if (track->type != MATROSKA_TRACK_TYPE_VIDEO) {
01084 av_log(matroska->ctx, AV_LOG_INFO,
01085 "video data in non-video track - ignoring\n");
01086 res = AVERROR_INVALIDDATA;
01087 break;
01088 } else if ((res = ebml_read_master(matroska, &id)) < 0)
01089 break;
01090 videotrack = (MatroskaVideoTrack *)track;
01091
01092 while (res == 0) {
01093 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
01094 res = AVERROR(EIO);
01095 break;
01096 } else if (matroska->level_up > 0) {
01097 matroska->level_up--;
01098 break;
01099 }
01100
01101 switch (id) {
01102
01103 case MATROSKA_ID_TRACKDEFAULTDURATION: {
01104 uint64_t num;
01105 if ((res = ebml_read_uint (matroska, &id,
01106 &num)) < 0)
01107 break;
01108 track->default_duration = num;
01109 break;
01110 }
01111
01112
01113 case MATROSKA_ID_VIDEOFRAMERATE: {
01114 double num;
01115 if ((res = ebml_read_float(matroska, &id,
01116 &num)) < 0)
01117 break;
01118 if (!track->default_duration)
01119 track->default_duration = 1000000000/num;
01120 break;
01121 }
01122
01123
01124 case MATROSKA_ID_VIDEODISPLAYWIDTH: {
01125 uint64_t num;
01126 if ((res = ebml_read_uint(matroska, &id,
01127 &num)) < 0)
01128 break;
01129 videotrack->display_width = num;
01130 break;
01131 }
01132
01133
01134 case MATROSKA_ID_VIDEODISPLAYHEIGHT: {
01135 uint64_t num;
01136 if ((res = ebml_read_uint(matroska, &id,
01137 &num)) < 0)
01138 break;
01139 videotrack->display_height = num;
01140 break;
01141 }
01142
01143
01144 case MATROSKA_ID_VIDEOPIXELWIDTH: {
01145 uint64_t num;
01146 if ((res = ebml_read_uint(matroska, &id,
01147 &num)) < 0)
01148 break;
01149 videotrack->pixel_width = num;
01150 break;
01151 }
01152
01153
01154 case MATROSKA_ID_VIDEOPIXELHEIGHT: {
01155 uint64_t num;
01156 if ((res = ebml_read_uint(matroska, &id,
01157 &num)) < 0)
01158 break;
01159 videotrack->pixel_height = num;
01160 break;
01161 }
01162
01163
01164 case MATROSKA_ID_VIDEOFLAGINTERLACED: {
01165 uint64_t num;
01166 if ((res = ebml_read_uint(matroska, &id,
01167 &num)) < 0)
01168 break;
01169 if (num)
01170 track->flags |=
01171 MATROSKA_VIDEOTRACK_INTERLACED;
01172 else
01173 track->flags &=
01174 ~MATROSKA_VIDEOTRACK_INTERLACED;
01175 break;
01176 }
01177
01178
01179
01180
01181
01182 case MATROSKA_ID_VIDEOSTEREOMODE: {
01183 uint64_t num;
01184 if ((res = ebml_read_uint(matroska, &id,
01185 &num)) < 0)
01186 break;
01187 if (num != MATROSKA_EYE_MODE_MONO &&
01188 num != MATROSKA_EYE_MODE_LEFT &&
01189 num != MATROSKA_EYE_MODE_RIGHT &&
01190 num != MATROSKA_EYE_MODE_BOTH) {
01191 av_log(matroska->ctx, AV_LOG_INFO,
01192 "Ignoring unknown eye mode 0x%x\n",
01193 (uint32_t) num);
01194 break;
01195 }
01196 videotrack->eye_mode = num;
01197 break;
01198 }
01199
01200
01201 case MATROSKA_ID_VIDEOASPECTRATIO: {
01202 uint64_t num;
01203 if ((res = ebml_read_uint(matroska, &id,
01204 &num)) < 0)
01205 break;
01206 if (num != MATROSKA_ASPECT_RATIO_MODE_FREE &&
01207 num != MATROSKA_ASPECT_RATIO_MODE_KEEP &&
01208 num != MATROSKA_ASPECT_RATIO_MODE_FIXED) {
01209 av_log(matroska->ctx, AV_LOG_INFO,
01210 "Ignoring unknown aspect ratio 0x%x\n",
01211 (uint32_t) num);
01212 break;
01213 }
01214 videotrack->ar_mode = num;
01215 break;
01216 }
01217
01218
01219
01220 case MATROSKA_ID_VIDEOCOLORSPACE: {
01221 uint64_t num;
01222 if ((res = ebml_read_uint(matroska, &id,
01223 &num)) < 0)
01224 break;
01225 videotrack->fourcc = num;
01226 break;
01227 }
01228
01229 default:
01230 av_log(matroska->ctx, AV_LOG_INFO,
01231 "Unknown video track header entry "
01232 "0x%x - ignoring\n", id);
01233
01234
01235 case EBML_ID_VOID:
01236 res = ebml_read_skip(matroska);
01237 break;
01238 }
01239
01240 if (matroska->level_up) {
01241 matroska->level_up--;
01242 break;
01243 }
01244 }
01245 break;
01246 }
01247
01248
01249 case MATROSKA_ID_TRACKAUDIO: {
01250 MatroskaAudioTrack *audiotrack;
01251 if (!track->type)
01252 track->type = MATROSKA_TRACK_TYPE_AUDIO;
01253 if (track->type != MATROSKA_TRACK_TYPE_AUDIO) {
01254 av_log(matroska->ctx, AV_LOG_INFO,
01255 "audio data in non-audio track - ignoring\n");
01256 res = AVERROR_INVALIDDATA;
01257 break;
01258 } else if ((res = ebml_read_master(matroska, &id)) < 0)
01259 break;
01260 audiotrack = (MatroskaAudioTrack *)track;
01261 audiotrack->channels = 1;
01262 audiotrack->samplerate = 8000;
01263
01264 while (res == 0) {
01265 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
01266 res = AVERROR(EIO);
01267 break;
01268 } else if (matroska->level_up > 0) {
01269 matroska->level_up--;
01270 break;
01271 }
01272
01273 switch (id) {
01274
01275 case MATROSKA_ID_AUDIOSAMPLINGFREQ: {
01276 double num;
01277 if ((res = ebml_read_float(matroska, &id,
01278 &num)) < 0)
01279 break;
01280 audiotrack->internal_samplerate =
01281 audiotrack->samplerate = num;
01282 break;
01283 }
01284
01285 case MATROSKA_ID_AUDIOOUTSAMPLINGFREQ: {
01286 double num;
01287 if ((res = ebml_read_float(matroska, &id,
01288 &num)) < 0)
01289 break;
01290 audiotrack->samplerate = num;
01291 break;
01292 }
01293
01294
01295 case MATROSKA_ID_AUDIOBITDEPTH: {
01296 uint64_t num;
01297 if ((res = ebml_read_uint(matroska, &id,
01298 &num)) < 0)
01299 break;
01300 audiotrack->bitdepth = num;
01301 break;
01302 }
01303
01304
01305 case MATROSKA_ID_AUDIOCHANNELS: {
01306 uint64_t num;
01307 if ((res = ebml_read_uint(matroska, &id,
01308 &num)) < 0)
01309 break;
01310 audiotrack->channels = num;
01311 break;
01312 }
01313
01314 default:
01315 av_log(matroska->ctx, AV_LOG_INFO,
01316 "Unknown audio track header entry "
01317 "0x%x - ignoring\n", id);
01318
01319
01320 case EBML_ID_VOID:
01321 res = ebml_read_skip(matroska);
01322 break;
01323 }
01324
01325 if (matroska->level_up) {
01326 matroska->level_up--;
01327 break;
01328 }
01329 }
01330 break;
01331 }
01332
01333
01334 case MATROSKA_ID_CODECID: {
01335 char *text;
01336 if ((res = ebml_read_ascii(matroska, &id, &text)) < 0)
01337 break;
01338 track->codec_id = text;
01339 break;
01340 }
01341
01342
01343 case MATROSKA_ID_CODECPRIVATE: {
01344 uint8_t *data;
01345 int size;
01346 if ((res = ebml_read_binary(matroska, &id, &data, &size) < 0))
01347 break;
01348 track->codec_priv = data;
01349 track->codec_priv_size = size;
01350 break;
01351 }
01352
01353
01354 case MATROSKA_ID_CODECNAME: {
01355 char *text;
01356 if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
01357 break;
01358 track->codec_name = text;
01359 break;
01360 }
01361
01362
01363 case MATROSKA_ID_TRACKNAME: {
01364 char *text;
01365 if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
01366 break;
01367 track->name = text;
01368 break;
01369 }
01370
01371
01372 case MATROSKA_ID_TRACKLANGUAGE: {
01373 char *text, *end;
01374 if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
01375 break;
01376 if ((end = strchr(text, '-')))
01377 *end = '\0';
01378 if (strlen(text) == 3)
01379 strcpy(track->language, text);
01380 av_free(text);
01381 break;
01382 }
01383
01384
01385 case MATROSKA_ID_TRACKFLAGENABLED: {
01386 uint64_t num;
01387 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
01388 break;
01389 if (num)
01390 track->flags |= MATROSKA_TRACK_ENABLED;
01391 else
01392 track->flags &= ~MATROSKA_TRACK_ENABLED;
01393 break;
01394 }
01395
01396
01397 case MATROSKA_ID_TRACKFLAGDEFAULT: {
01398 uint64_t num;
01399 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
01400 break;
01401 if (num)
01402 track->flags |= MATROSKA_TRACK_DEFAULT;
01403 else
01404 track->flags &= ~MATROSKA_TRACK_DEFAULT;
01405 break;
01406 }
01407
01408
01409
01410 case MATROSKA_ID_TRACKFLAGLACING: {
01411 uint64_t num;
01412 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
01413 break;
01414 if (num)
01415 track->flags |= MATROSKA_TRACK_LACING;
01416 else
01417 track->flags &= ~MATROSKA_TRACK_LACING;
01418 break;
01419 }
01420
01421
01422 case MATROSKA_ID_TRACKDEFAULTDURATION: {
01423 uint64_t num;
01424 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
01425 break;
01426 track->default_duration = num;
01427 break;
01428 }
01429
01430 default:
01431 av_log(matroska->ctx, AV_LOG_INFO,
01432 "Unknown track header entry 0x%x - ignoring\n", id);
01433
01434
01435 case EBML_ID_VOID:
01436
01437 case MATROSKA_ID_CODECINFOURL:
01438 case MATROSKA_ID_CODECDOWNLOADURL:
01439 case MATROSKA_ID_TRACKMINCACHE:
01440 case MATROSKA_ID_TRACKMAXCACHE:
01441 res = ebml_read_skip(matroska);
01442 break;
01443 }
01444
01445 if (matroska->level_up) {
01446 matroska->level_up--;
01447 break;
01448 }
01449 }
01450
01451 return res;
01452 }
01453
01454 static int
01455 matroska_parse_tracks (MatroskaDemuxContext *matroska)
01456 {
01457 int res = 0;
01458 uint32_t id;
01459
01460 av_log(matroska->ctx, AV_LOG_DEBUG, "parsing tracks...\n");
01461
01462 while (res == 0) {
01463 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
01464 res = AVERROR(EIO);
01465 break;
01466 } else if (matroska->level_up) {
01467 matroska->level_up--;
01468 break;
01469 }
01470
01471 switch (id) {
01472
01473 case MATROSKA_ID_TRACKENTRY:
01474 res = matroska_add_stream(matroska);
01475 break;
01476
01477 default:
01478 av_log(matroska->ctx, AV_LOG_INFO,
01479 "Unknown entry 0x%x in track header\n", id);
01480
01481
01482 case EBML_ID_VOID:
01483 res = ebml_read_skip(matroska);
01484 break;
01485 }
01486
01487 if (matroska->level_up) {
01488 matroska->level_up--;
01489 break;
01490 }
01491 }
01492
01493 return res;
01494 }
01495
01496 static int
01497 matroska_parse_index (MatroskaDemuxContext *matroska)
01498 {
01499 int res = 0;
01500 uint32_t id;
01501 MatroskaDemuxIndex idx;
01502
01503 av_log(matroska->ctx, AV_LOG_DEBUG, "parsing index...\n");
01504
01505 while (res == 0) {
01506 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
01507 res = AVERROR(EIO);
01508 break;
01509 } else if (matroska->level_up) {
01510 matroska->level_up--;
01511 break;
01512 }
01513
01514 switch (id) {
01515
01516 case MATROSKA_ID_POINTENTRY:
01517 if ((res = ebml_read_master(matroska, &id)) < 0)
01518 break;
01519
01520
01521
01522 idx.pos = (uint64_t) -1;
01523 idx.time = (uint64_t) -1;
01524 idx.track = (uint16_t) -1;
01525
01526 while (res == 0) {
01527 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
01528 res = AVERROR(EIO);
01529 break;
01530 } else if (matroska->level_up) {
01531 matroska->level_up--;
01532 break;
01533 }
01534
01535 switch (id) {
01536
01537 case MATROSKA_ID_CUETIME: {
01538 uint64_t time;
01539 if ((res = ebml_read_uint(matroska, &id,
01540 &time)) < 0)
01541 break;
01542 idx.time = time * matroska->time_scale;
01543 break;
01544 }
01545
01546
01547
01548 case MATROSKA_ID_CUETRACKPOSITION:
01549 if ((res = ebml_read_master(matroska, &id)) < 0)
01550 break;
01551
01552 while (res == 0) {
01553 if (!(id = ebml_peek_id (matroska,
01554 &matroska->level_up))) {
01555 res = AVERROR(EIO);
01556 break;
01557 } else if (matroska->level_up) {
01558 matroska->level_up--;
01559 break;
01560 }
01561
01562 switch (id) {
01563
01564 case MATROSKA_ID_CUETRACK: {
01565 uint64_t num;
01566 if ((res = ebml_read_uint(matroska,
01567 &id, &num)) < 0)
01568 break;
01569 idx.track = num;
01570 break;
01571 }
01572
01573
01574 case MATROSKA_ID_CUECLUSTERPOSITION: {
01575 uint64_t num;
01576 if ((res = ebml_read_uint(matroska,
01577 &id, &num)) < 0)
01578 break;
01579 idx.pos = num+matroska->segment_start;
01580 break;
01581 }
01582
01583 default:
01584 av_log(matroska->ctx, AV_LOG_INFO,
01585 "Unknown entry 0x%x in "
01586 "CuesTrackPositions\n", id);
01587
01588
01589 case EBML_ID_VOID:
01590 res = ebml_read_skip(matroska);
01591 break;
01592 }
01593
01594 if (matroska->level_up) {
01595 matroska->level_up--;
01596 break;
01597 }
01598 }
01599
01600 break;
01601
01602 default:
01603 av_log(matroska->ctx, AV_LOG_INFO,
01604 "Unknown entry 0x%x in cuespoint "
01605 "index\n", id);
01606
01607
01608 case EBML_ID_VOID:
01609 res = ebml_read_skip(matroska);
01610 break;
01611 }
01612
01613 if (matroska->level_up) {
01614 matroska->level_up--;
01615 break;
01616 }
01617 }
01618
01619
01620 if (idx.pos != (uint64_t) -1 &&
01621 idx.time != (uint64_t) -1 &&
01622 idx.track != (uint16_t) -1) {
01623 if (matroska->num_indexes % 32 == 0) {
01624
01625 matroska->index =
01626 av_realloc(matroska->index,
01627 (matroska->num_indexes + 32) *
01628 sizeof(MatroskaDemuxIndex));
01629 }
01630 matroska->index[matroska->num_indexes] = idx;
01631 matroska->num_indexes++;
01632 }
01633 break;
01634
01635 default:
01636 av_log(matroska->ctx, AV_LOG_INFO,
01637 "Unknown entry 0x%x in cues header\n", id);
01638
01639
01640 case EBML_ID_VOID:
01641 res = ebml_read_skip(matroska);
01642 break;
01643 }
01644
01645 if (matroska->level_up) {
01646 matroska->level_up--;
01647 break;
01648 }
01649 }
01650
01651 return res;
01652 }
01653
01654 static int
01655 matroska_parse_metadata (MatroskaDemuxContext *matroska)
01656 {
01657 int res = 0;
01658 uint32_t id;
01659
01660 while (res == 0) {
01661 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
01662 res = AVERROR(EIO);
01663 break;
01664 } else if (matroska->level_up) {
01665 matroska->level_up--;
01666 break;
01667 }
01668
01669 switch (id) {
01670
01671 default:
01672 av_log(matroska->ctx, AV_LOG_INFO,
01673 "Unknown entry 0x%x in metadata header\n", id);
01674
01675
01676 case EBML_ID_VOID:
01677 res = ebml_read_skip(matroska);
01678 break;
01679 }
01680
01681 if (matroska->level_up) {
01682 matroska->level_up--;
01683 break;
01684 }
01685 }
01686
01687 return res;
01688 }
01689
01690 static int
01691 matroska_parse_seekhead (MatroskaDemuxContext *matroska)
01692 {
01693 int res = 0;
01694 uint32_t id;
01695
01696 av_log(matroska->ctx, AV_LOG_DEBUG, "parsing seekhead...\n");
01697
01698 while (res == 0) {
01699 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
01700 res = AVERROR(EIO);
01701 break;
01702 } else if (matroska->level_up) {
01703 matroska->level_up--;
01704 break;
01705 }
01706
01707 switch (id) {
01708 case MATROSKA_ID_SEEKENTRY: {
01709 uint32_t seek_id = 0, peek_id_cache = 0;
01710 uint64_t seek_pos = (uint64_t) -1, t;
01711
01712 if ((res = ebml_read_master(matroska, &id)) < 0)
01713 break;
01714
01715 while (res == 0) {
01716 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
01717 res = AVERROR(EIO);
01718 break;
01719 } else if (matroska->level_up) {
01720 matroska->level_up--;
01721 break;
01722 }
01723
01724 switch (id) {
01725 case MATROSKA_ID_SEEKID:
01726 res = ebml_read_uint(matroska, &id, &t);
01727 seek_id = t;
01728 break;
01729
01730 case MATROSKA_ID_SEEKPOSITION:
01731 res = ebml_read_uint(matroska, &id, &seek_pos);
01732 break;
01733
01734 default:
01735 av_log(matroska->ctx, AV_LOG_INFO,
01736 "Unknown seekhead ID 0x%x\n", id);
01737
01738
01739 case EBML_ID_VOID:
01740 res = ebml_read_skip(matroska);
01741 break;
01742 }
01743
01744 if (matroska->level_up) {
01745 matroska->level_up--;
01746 break;
01747 }
01748 }
01749
01750 if (!seek_id || seek_pos == (uint64_t) -1) {
01751 av_log(matroska->ctx, AV_LOG_INFO,
01752 "Incomplete seekhead entry (0x%x/%"PRIu64")\n",
01753 seek_id, seek_pos);
01754 break;
01755 }
01756
01757 switch (seek_id) {
01758 case MATROSKA_ID_CUES:
01759 case MATROSKA_ID_TAGS: {
01760 uint32_t level_up = matroska->level_up;
01761 offset_t before_pos;
01762 uint64_t length;
01763 MatroskaLevel level;
01764
01765
01766 peek_id_cache = matroska->peek_id;
01767 before_pos = url_ftell(matroska->ctx->pb);
01768
01769
01770 if ((res = ebml_read_seek(matroska, seek_pos +
01771 matroska->segment_start)) < 0)
01772 return res;
01773
01774
01775
01776 if (matroska->num_levels == EBML_MAX_DEPTH) {
01777 av_log(matroska->ctx, AV_LOG_INFO,
01778 "Max EBML element depth (%d) reached, "
01779 "cannot parse further.\n", EBML_MAX_DEPTH);
01780 return AVERROR_UNKNOWN;
01781 }
01782
01783 level.start = 0;
01784 level.length = (uint64_t)-1;
01785 matroska->levels[matroska->num_levels] = level;
01786 matroska->num_levels++;
01787
01788
01789 if (!(id = ebml_peek_id (matroska,
01790 &matroska->level_up)))
01791 goto finish;
01792 if (id != seek_id) {
01793 av_log(matroska->ctx, AV_LOG_INFO,
01794 "We looked for ID=0x%x but got "
01795 "ID=0x%x (pos=%"PRIu64")",
01796 seek_id, id, seek_pos +
01797 matroska->segment_start);
01798 goto finish;
01799 }
01800
01801
01802 if ((res = ebml_read_master(matroska, &id)) < 0)
01803 goto finish;
01804 switch (id) {
01805 case MATROSKA_ID_CUES:
01806 if (!(res = matroska_parse_index(matroska)) ||
01807 url_feof(matroska->ctx->pb)) {
01808 matroska->index_parsed = 1;
01809 res = 0;
01810 }
01811 break;
01812 case MATROSKA_ID_TAGS:
01813 if (!(res = matroska_parse_metadata(matroska)) ||
01814 url_feof(matroska->ctx->pb)) {
01815 matroska->metadata_parsed = 1;
01816 res = 0;
01817 }
01818 break;
01819 }
01820
01821 finish:
01822
01823 while (matroska->num_levels) {
01824 matroska->num_levels--;
01825 length =
01826 matroska->levels[matroska->num_levels].length;
01827 if (length == (uint64_t)-1)
01828 break;
01829 }
01830
01831
01832 if ((res = ebml_read_seek(matroska, before_pos)) < 0)
01833 return res;
01834 matroska->peek_id = peek_id_cache;
01835 matroska->level_up = level_up;
01836 break;
01837 }
01838
01839 default:
01840 av_log(matroska->ctx, AV_LOG_INFO,
01841 "Ignoring seekhead entry for ID=0x%x\n",
01842 seek_id);
01843 break;
01844 }
01845
01846 break;
01847 }
01848
01849 default:
01850 av_log(matroska->ctx, AV_LOG_INFO,
01851 "Unknown seekhead ID 0x%x\n", id);
01852
01853
01854 case EBML_ID_VOID:
01855 res = ebml_read_skip(matroska);
01856 break;
01857 }
01858
01859 if (matroska->level_up) {
01860 matroska->level_up--;
01861 break;
01862 }
01863 }
01864
01865 return res;
01866 }
01867
01868 static int
01869 matroska_parse_attachments(AVFormatContext *s)
01870 {
01871 MatroskaDemuxContext *matroska = s->priv_data;
01872 int res = 0;
01873 uint32_t id;
01874
01875 av_log(matroska->ctx, AV_LOG_DEBUG, "parsing attachments...\n");
01876
01877 while (res == 0) {
01878 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
01879 res = AVERROR(EIO);
01880 break;
01881 } else if (matroska->level_up) {
01882 matroska->level_up--;
01883 break;
01884 }
01885
01886 switch (id) {
01887 case MATROSKA_ID_ATTACHEDFILE: {
01888 char* name = NULL;
01889 char* mime = NULL;
01890 uint8_t* data = NULL;
01891 int i, data_size = 0;
01892 AVStream *st;
01893
01894 if ((res = ebml_read_master(matroska, &id)) < 0)
01895 break;
01896
01897 while (res == 0) {
01898 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
01899 res = AVERROR(EIO);
01900 break;
01901 } else if (matroska->level_up) {
01902 matroska->level_up--;
01903 break;
01904 }
01905
01906 switch (id) {
01907 case MATROSKA_ID_FILENAME:
01908 res = ebml_read_utf8 (matroska, &id, &name);
01909 break;
01910
01911 case MATROSKA_ID_FILEMIMETYPE:
01912 res = ebml_read_ascii (matroska, &id, &mime);
01913 break;
01914
01915 case MATROSKA_ID_FILEDATA:
01916 res = ebml_read_binary(matroska, &id, &data, &data_size);
01917 break;
01918
01919 default:
01920 av_log(matroska->ctx, AV_LOG_INFO,
01921 "Unknown attachedfile ID 0x%x\n", id);
01922 case EBML_ID_VOID:
01923 res = ebml_read_skip(matroska);
01924 break;
01925 }
01926
01927 if (matroska->level_up) {
01928 matroska->level_up--;
01929 break;
01930 }
01931 }
01932
01933 if (!(name && mime && data && data_size > 0)) {
01934 av_log(matroska->ctx, AV_LOG_ERROR, "incomplete attachment\n");
01935 break;
01936 }
01937
01938 st = av_new_stream(s, matroska->num_streams++);
01939 if (st == NULL)
01940 return AVERROR(ENOMEM);
01941 st->filename = av_strdup(name);
01942 st->codec->codec_id = CODEC_ID_NONE;
01943 st->codec->codec_type = CODEC_TYPE_ATTACHMENT;
01944 st->codec->extradata = av_malloc(data_size);
01945 if(st->codec->extradata == NULL)
01946 return AVERROR(ENOMEM);
01947 st->codec->extradata_size = data_size;
01948 memcpy(st->codec->extradata, data, data_size);
01949
01950 for (i=0; ff_mkv_mime_tags[i].id != CODEC_ID_NONE; i++) {
01951 if (!strncmp(ff_mkv_mime_tags[i].str, mime,
01952 strlen(ff_mkv_mime_tags[i].str))) {
01953 st->codec->codec_id = ff_mkv_mime_tags[i].id;
01954 break;
01955 }
01956 }
01957
01958 av_log(matroska->ctx, AV_LOG_DEBUG, "new attachment: %s, %s, size %d \n", name, mime, data_size);
01959 break;
01960 }
01961
01962 default:
01963 av_log(matroska->ctx, AV_LOG_INFO,
01964 "Unknown attachments ID 0x%x\n", id);
01965
01966
01967 case EBML_ID_VOID:
01968 res = ebml_read_skip(matroska);
01969 break;
01970 }
01971
01972 if (matroska->level_up) {
01973 matroska->level_up--;
01974 break;
01975 }
01976 }
01977
01978 return res;
01979 }
01980
01981 #define ARRAY_SIZE(x) (sizeof(x)/sizeof(*x))
01982
01983 static int
01984 matroska_aac_profile (char *codec_id)
01985 {
01986 static const char *aac_profiles[] = {
01987 "MAIN", "LC", "SSR"
01988 };
01989 int profile;
01990
01991 for (profile=0; profile<ARRAY_SIZE(aac_profiles); profile++)
01992 if (strstr(codec_id, aac_profiles[profile]))
01993 break;
01994 return profile + 1;
01995 }
01996
01997 static int
01998 matroska_aac_sri (int samplerate)
01999 {
02000 static const int aac_sample_rates[] = {
02001 96000, 88200, 64000, 48000, 44100, 32000,
02002 24000, 22050, 16000, 12000, 11025, 8000,
02003 };
02004 int sri;
02005
02006 for (sri=0; sri<ARRAY_SIZE(aac_sample_rates); sri++)
02007 if (aac_sample_rates[sri] == samplerate)
02008 break;
02009 return sri;
02010 }
02011
02012 static int
02013 matroska_read_header (AVFormatContext *s,
02014 AVFormatParameters *ap)
02015 {
02016 MatroskaDemuxContext *matroska = s->priv_data;
02017 char *doctype;
02018 int version, last_level, res = 0;
02019 uint32_t id;
02020
02021 matroska->ctx = s;
02022
02023
02024 doctype = NULL;
02025 if ((res = ebml_read_header(matroska, &doctype, &version)) < 0)
02026 return res;
02027 if ((doctype == NULL) || strcmp(doctype, "matroska")) {
02028 av_log(matroska->ctx, AV_LOG_ERROR,
02029 "Wrong EBML doctype ('%s' != 'matroska').\n",
02030 doctype ? doctype : "(none)");
02031 if (doctype)
02032 av_free(doctype);
02033 return AVERROR_NOFMT;
02034 }
02035 av_free(doctype);
02036 if (version > 2) {
02037 av_log(matroska->ctx, AV_LOG_ERROR,
02038 "Matroska demuxer version 2 too old for file version %d\n",
02039 version);
02040 return AVERROR_NOFMT;
02041 }
02042
02043
02044 while (1) {
02045 if (!(id = ebml_peek_id(matroska, &last_level)))
02046 return AVERROR(EIO);
02047 if (id == MATROSKA_ID_SEGMENT)
02048 break;
02049
02050
02051 av_log(matroska->ctx, AV_LOG_INFO,
02052 "Expected a Segment ID (0x%x), but received 0x%x!\n",
02053 MATROSKA_ID_SEGMENT, id);
02054 if ((res = ebml_read_skip(matroska)) < 0)
02055 return res;
02056 }
02057
02058
02059
02060
02061 if ((res = ebml_read_master(matroska, &id)) < 0)
02062 return res;
02063 matroska->segment_start = url_ftell(s->pb);
02064
02065 matroska->time_scale = 1000000;
02066
02067 while (res == 0) {
02068 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
02069 res = AVERROR(EIO);
02070 break;
02071 } else if (matroska->level_up) {
02072 matroska->level_up--;
02073 break;
02074 }
02075
02076 switch (id) {
02077
02078 case MATROSKA_ID_INFO: {
02079 if ((res = ebml_read_master(matroska, &id)) < 0)
02080 break;
02081 res = matroska_parse_info(matroska);
02082 break;
02083 }
02084
02085
02086 case MATROSKA_ID_TRACKS: {
02087 if ((res = ebml_read_master(matroska, &id)) < 0)
02088 break;
02089 res = matroska_parse_tracks(matroska);
02090 break;
02091 }
02092
02093
02094 case MATROSKA_ID_CUES: {
02095 if (!matroska->index_parsed) {
02096 if ((res = ebml_read_master(matroska, &id)) < 0)
02097 break;
02098 res = matroska_parse_index(matroska);
02099 } else
02100 res = ebml_read_skip(matroska);
02101 break;
02102 }
02103
02104
02105 case MATROSKA_ID_TAGS: {
02106 if (!matroska->metadata_parsed) {
02107 if ((res = ebml_read_master(matroska, &id)) < 0)
02108 break;
02109 res = matroska_parse_metadata(matroska);
02110 } else
02111 res = ebml_read_skip(matroska);
02112 break;
02113 }
02114
02115
02116 case MATROSKA_ID_SEEKHEAD: {
02117 if ((res = ebml_read_master(matroska, &id)) < 0)
02118 break;
02119 res = matroska_parse_seekhead(matroska);
02120 break;
02121 }
02122
02123 case MATROSKA_ID_ATTACHMENTS: {
02124 if ((res = ebml_read_master(matroska, &id)) < 0)
02125 break;
02126 res = matroska_parse_attachments(s);
02127 break;
02128 }
02129
02130 case MATROSKA_ID_CLUSTER: {
02131
02132
02133 res = 1;
02134 break;
02135 }
02136
02137 default:
02138 av_log(matroska->ctx, AV_LOG_INFO,
02139 "Unknown matroska file header ID 0x%x\n", id);
02140
02141
02142 case EBML_ID_VOID:
02143 res = ebml_read_skip(matroska);
02144 break;
02145 }
02146
02147 if (matroska->level_up) {
02148 matroska->level_up--;
02149 break;
02150 }
02151 }
02152
02153
02154 if (ebml_peek_id(matroska, NULL) == MATROSKA_ID_CLUSTER) {
02155 int i, j;
02156 MatroskaTrack *track;
02157 AVStream *st;
02158
02159 for (i = 0; i < matroska->num_tracks; i++) {
02160 enum CodecID codec_id = CODEC_ID_NONE;
02161 uint8_t *extradata = NULL;
02162 int extradata_size = 0;
02163 int extradata_offset = 0;
02164 track = matroska->tracks[i];
02165 track->stream_index = -1;
02166
02167
02168 if (track->codec_id == NULL)
02169 continue;
02170
02171 for(j=0; ff_mkv_codec_tags[j].id != CODEC_ID_NONE; j++){
02172 if(!strncmp(ff_mkv_codec_tags[j].str, track->codec_id,
02173 strlen(ff_mkv_codec_tags[j].str))){
02174 codec_id= ff_mkv_codec_tags[j].id;
02175 break;
02176 }
02177 }
02178
02179
02180
02181
02182 if (!strcmp(track->codec_id,
02183 MATROSKA_CODEC_ID_VIDEO_VFW_FOURCC) &&
02184 (track->codec_priv_size >= 40) &&
02185 (track->codec_priv != NULL)) {
02186 MatroskaVideoTrack *vtrack = (MatroskaVideoTrack *) track;
02187
02188
02189 vtrack->fourcc = AV_RL32(track->codec_priv + 16);
02190 codec_id = codec_get_id(codec_bmp_tags, vtrack->fourcc);
02191
02192 }
02193
02194
02195
02196 else if (!strcmp(track->codec_id,
02197 MATROSKA_CODEC_ID_AUDIO_ACM) &&
02198 (track->codec_priv_size >= 18) &&
02199 (track->codec_priv != NULL)) {
02200 uint16_t tag;
02201
02202
02203 tag = AV_RL16(track->codec_priv);
02204 codec_id = codec_get_id(codec_wav_tags, tag);
02205
02206 }
02207
02208 else if (codec_id == CODEC_ID_AAC && !track->codec_priv_size) {
02209 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *) track;
02210 int profile = matroska_aac_profile(track->codec_id);
02211 int sri = matroska_aac_sri(audiotrack->internal_samplerate);
02212 extradata = av_malloc(5);
02213 if (extradata == NULL)
02214 return AVERROR(ENOMEM);
02215 extradata[0] = (profile << 3) | ((sri&0x0E) >> 1);
02216 extradata[1] = ((sri&0x01) << 7) | (audiotrack->channels<<3);
02217 if (strstr(track->codec_id, "SBR")) {
02218 sri = matroska_aac_sri(audiotrack->samplerate);
02219 extradata[2] = 0x56;
02220 extradata[3] = 0xE5;
02221 extradata[4] = 0x80 | (sri<<3);
02222 extradata_size = 5;
02223 } else {
02224 extradata_size = 2;
02225 }
02226 }
02227
02228 else if (codec_id == CODEC_ID_TTA) {
02229 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *) track;
02230 ByteIOContext b;
02231 extradata_size = 30;
02232 extradata = av_mallocz(extradata_size);
02233 if (extradata == NULL)
02234 return AVERROR(ENOMEM);
02235 init_put_byte(&b, extradata, extradata_size, 1,
02236 NULL, NULL, NULL, NULL);
02237 put_buffer(&b, "TTA1", 4);
02238 put_le16(&b, 1);
02239 put_le16(&b, audiotrack->channels);
02240 put_le16(&b, audiotrack->bitdepth);
02241 put_le32(&b, audiotrack->samplerate);
02242 put_le32(&b, matroska->ctx->duration * audiotrack->samplerate);
02243 }
02244
02245 else if (codec_id == CODEC_ID_RV10 || codec_id == CODEC_ID_RV20 ||
02246 codec_id == CODEC_ID_RV30 || codec_id == CODEC_ID_RV40) {
02247 extradata_offset = 26;
02248 track->codec_priv_size -= extradata_offset;
02249 }
02250
02251 else if (codec_id == CODEC_ID_RA_144) {
02252 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
02253 audiotrack->samplerate = 8000;
02254 audiotrack->channels = 1;
02255 }
02256
02257 else if (codec_id == CODEC_ID_RA_288 ||
02258 codec_id == CODEC_ID_COOK ||
02259 codec_id == CODEC_ID_ATRAC3) {
02260 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
02261 ByteIOContext b;
02262
02263 init_put_byte(&b, track->codec_priv, track->codec_priv_size, 0,
02264 NULL, NULL, NULL, NULL);
02265 url_fskip(&b, 24);
02266 audiotrack->coded_framesize = get_be32(&b);
02267 url_fskip(&b, 12);
02268 audiotrack->sub_packet_h = get_be16(&b);
02269 audiotrack->frame_size = get_be16(&b);
02270 audiotrack->sub_packet_size = get_be16(&b);
02271 audiotrack->buf = av_malloc(audiotrack->frame_size * audiotrack->sub_packet_h);
02272 if (codec_id == CODEC_ID_RA_288) {
02273 audiotrack->block_align = audiotrack->coded_framesize;
02274 track->codec_priv_size = 0;
02275 } else {
02276 audiotrack->block_align = audiotrack->sub_packet_size;
02277 extradata_offset = 78;
02278 track->codec_priv_size -= extradata_offset;
02279 }
02280 }
02281
02282 if (codec_id == CODEC_ID_NONE) {
02283 av_log(matroska->ctx, AV_LOG_INFO,
02284 "Unknown/unsupported CodecID %s.\n",
02285 track->codec_id);
02286 }
02287
02288 track->stream_index = matroska->num_streams;
02289
02290 matroska->num_streams++;
02291 st = av_new_stream(s, track->stream_index);
02292 if (st == NULL)
02293 return AVERROR(ENOMEM);
02294 av_set_pts_info(st, 64, matroska->time_scale, 1000*1000*1000);
02295
02296 st->codec->codec_id = codec_id;
02297 st->start_time = 0;
02298 if (strcmp(track->language, "und"))
02299 strcpy(st->language, track->language);
02300
02301 if (track->default_duration)
02302 av_reduce(&st->codec->time_base.num, &st->codec->time_base.den,
02303 track->default_duration, 1000000000, 30000);
02304
02305 if(extradata){
02306 st->codec->extradata = extradata;
02307 st->codec->extradata_size = extradata_size;
02308 } else if(track->codec_priv && track->codec_priv_size > 0){
02309 st->codec->extradata = av_malloc(track->codec_priv_size);
02310 if(st->codec->extradata == NULL)
02311 return AVERROR(ENOMEM);
02312 st->codec->extradata_size = track->codec_priv_size;
02313 memcpy(st->codec->extradata,track->codec_priv+extradata_offset,
02314 track->codec_priv_size);
02315 }
02316
02317 if (track->type == MATROSKA_TRACK_TYPE_VIDEO) {
02318 MatroskaVideoTrack *videotrack = (MatroskaVideoTrack *)track;
02319
02320 st->codec->codec_type = CODEC_TYPE_VIDEO;
02321 st->codec->codec_tag = videotrack->fourcc;
02322 st->codec->width = videotrack->pixel_width;
02323 st->codec->height = videotrack->pixel_height;
02324 if (videotrack->display_width == 0)
02325 videotrack->display_width= videotrack->pixel_width;
02326 if (videotrack->display_height == 0)
02327 videotrack->display_height= videotrack->pixel_height;
02328 av_reduce(&st->codec->sample_aspect_ratio.num,
02329 &st->codec->sample_aspect_ratio.den,
02330 st->codec->height * videotrack->display_width,
02331 st->codec-> width * videotrack->display_height,
02332 255);
02333 st->need_parsing = AVSTREAM_PARSE_HEADERS;
02334 } else if (track->type == MATROSKA_TRACK_TYPE_AUDIO) {
02335 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
02336
02337 st->codec->codec_type = CODEC_TYPE_AUDIO;
02338 st->codec->sample_rate = audiotrack->samplerate;
02339 st->codec->channels = audiotrack->channels;
02340 st->codec->block_align = audiotrack->block_align;
02341 } else if (track->type == MATROSKA_TRACK_TYPE_SUBTITLE) {
02342 st->codec->codec_type = CODEC_TYPE_SUBTITLE;
02343 }
02344
02345
02346 }
02347 res = 0;
02348 }
02349
02350 if (matroska->index_parsed) {
02351 int i, track, stream;
02352 for (i=0; i<matroska->num_indexes; i++) {
02353 MatroskaDemuxIndex *idx = &matroska->index[i];
02354 track = matroska_find_track_by_num(matroska, idx->track);
02355 stream = matroska->tracks[track]->stream_index;
02356 if (stream >= 0)
02357 av_add_index_entry(matroska->ctx->streams[stream],
02358 idx->pos, idx->time/matroska->time_scale,
02359 0, 0, AVINDEX_KEYFRAME);
02360 }
02361 }
02362
02363 return res;
02364 }
02365
02366 static int
02367 matroska_parse_block(MatroskaDemuxContext *matroska, uint8_t *data, int size,
02368 int64_t pos, uint64_t cluster_time, uint64_t duration,
02369 int is_keyframe, int is_bframe)
02370 {
02371 int res = 0;
02372 int track;
02373 AVStream *st;
02374 AVPacket *pkt;
02375 uint8_t *origdata = data;
02376 int16_t block_time;
02377 uint32_t *lace_size = NULL;
02378 int n, flags, laces = 0;
02379 uint64_t num;
02380
02381
02382 if ((n = matroska_ebmlnum_uint(data, size, &num)) < 0) {
02383 av_log(matroska->ctx, AV_LOG_ERROR, "EBML block data error\n");
02384 av_free(origdata);
02385 return res;
02386 }
02387 data += n;
02388 size -= n;
02389
02390
02391 track = matroska_find_track_by_num(matroska, num);
02392 if (size <= 3 || track < 0 || track >= matroska->num_tracks) {
02393 av_log(matroska->ctx, AV_LOG_INFO,
02394 "Invalid stream %d or size %u\n", track, size);
02395 av_free(origdata);
02396 return res;
02397 }
02398 if (matroska->tracks[track]->stream_index < 0) {
02399 av_free(origdata);
02400 return res;
02401 }
02402 st = matroska->ctx->streams[matroska->tracks[track]->stream_index];
02403 if (st->discard >= AVDISCARD_ALL) {
02404 av_free(origdata);
02405 return res;
02406 }
02407 if (duration == AV_NOPTS_VALUE)
02408 duration = matroska->tracks[track]->default_duration / matroska->time_scale;
02409
02410
02411 block_time = AV_RB16(data);
02412 data += 2;
02413 flags = *data++;
02414 size -= 3;
02415 if (is_keyframe == -1)
02416 is_keyframe = flags & 0x80 ? PKT_FLAG_KEY : 0;
02417
02418 if (matroska->skip_to_keyframe) {
02419 if (!is_keyframe || st != matroska->skip_to_stream) {
02420 av_free(origdata);
02421 return res;
02422 }
02423 matroska->skip_to_keyframe = 0;
02424 }
02425
02426 switch ((flags & 0x06) >> 1) {
02427 case 0x0:
02428 laces = 1;
02429 lace_size = av_mallocz(sizeof(int));
02430 lace_size[0] = size;
02431 break;
02432
02433 case 0x1:
02434 case 0x2:
02435 case 0x3:
02436 if (size == 0) {
02437 res = -1;
02438 break;
02439 }
02440 laces = (*data) + 1;
02441 data += 1;
02442 size -= 1;
02443 lace_size = av_mallocz(laces * sizeof(int));
02444
02445 switch ((flags & 0x06) >> 1) {
02446 case 0x1: {
02447 uint8_t temp;
02448 uint32_t total = 0;
02449 for (n = 0; res == 0 && n < laces - 1; n++) {
02450 while (1) {
02451 if (size == 0) {
02452 res = -1;
02453 break;
02454 }
02455 temp = *data;
02456 lace_size[n] += temp;
02457 data += 1;
02458 size -= 1;
02459 if (temp != 0xff)
02460 break;
02461 }
02462 total += lace_size[n];
02463 }
02464 lace_size[n] = size - total;
02465 break;
02466 }
02467
02468 case 0x2:
02469 for (n = 0; n < laces; n++)
02470 lace_size[n] = size / laces;
02471 break;
02472
02473 case 0x3: {
02474 uint32_t total;
02475 n = matroska_ebmlnum_uint(data, size, &num);
02476 if (n < 0) {
02477 av_log(matroska->ctx, AV_LOG_INFO,
02478 "EBML block data error\n");
02479 break;
02480 }
02481 data += n;
02482 size -= n;
02483 total = lace_size[0] = num;
02484 for (n = 1; res == 0 && n < laces - 1; n++) {
02485 int64_t snum;
02486 int r;
02487 r = matroska_ebmlnum_sint (data, size, &snum);
02488 if (r < 0) {
02489 av_log(matroska->ctx, AV_LOG_INFO,
02490 "EBML block data error\n");
02491 break;
02492 }
02493 data += r;
02494 size -= r;
02495 lace_size[n] = lace_size[n - 1] + snum;
02496 total += lace_size[n];
02497 }
02498 lace_size[n] = size - total;
02499 break;
02500 }
02501 }
02502 break;
02503 }
02504
02505 if (res == 0) {
02506 uint64_t timecode = AV_NOPTS_VALUE;
02507
02508 if (cluster_time != (uint64_t)-1
02509 && (block_time >= 0 || cluster_time >= -block_time))
02510 timecode = cluster_time + block_time;
02511
02512 for (n = 0; n < laces; n++) {
02513 if (st->codec->codec_id == CODEC_ID_RA_288 ||
02514 st->codec->codec_id == CODEC_ID_COOK ||
02515 st->codec->codec_id == CODEC_ID_ATRAC3) {
02516 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)matroska->tracks[track];
02517 int a = st->codec->block_align;
02518 int sps = audiotrack->sub_packet_size;
02519 int cfs = audiotrack->coded_framesize;
02520 int h = audiotrack->sub_packet_h;
02521 int y = audiotrack->sub_packet_cnt;
02522 int w = audiotrack->frame_size;
02523 int x;
02524
02525 if (!audiotrack->pkt_cnt) {
02526 if (st->codec->codec_id == CODEC_ID_RA_288)
02527 for (x=0; x<h/2; x++)
02528 memcpy(audiotrack->buf+x*2*w+y*cfs,
02529 data+x*cfs, cfs);
02530 else
02531 for (x=0; x<w/sps; x++)
02532 memcpy(audiotrack->buf+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), data+x*sps, sps);
02533
02534 if (++audiotrack->sub_packet_cnt >= h) {
02535 audiotrack->sub_packet_cnt = 0;
02536 audiotrack->pkt_cnt = h*w / a;
02537 }
02538 }
02539 while (audiotrack->pkt_cnt) {
02540 pkt = av_mallocz(sizeof(AVPacket));
02541 av_new_packet(pkt, a);
02542 memcpy(pkt->data, audiotrack->buf
02543 + a * (h*w / a - audiotrack->pkt_cnt--), a);
02544 pkt->pos = pos;
02545 pkt->stream_index = matroska->tracks[track]->stream_index;
02546 matroska_queue_packet(matroska, pkt);
02547 }
02548 } else {
02549 int offset = 0;
02550
02551 pkt = av_mallocz(sizeof(AVPacket));
02552
02553 if (av_new_packet(pkt, lace_size[n]-offset) < 0) {
02554 res = AVERROR(ENOMEM);
02555 n = laces-1;
02556 break;
02557 }
02558 memcpy (pkt->data, data+offset, lace_size[n]-offset);
02559
02560 if (n == 0)
02561 pkt->flags = is_keyframe;
02562 pkt->stream_index = matroska->tracks[track]->stream_index;
02563
02564 pkt->pts = timecode;
02565 pkt->pos = pos;
02566 pkt->duration = duration;
02567
02568 matroska_queue_packet(matroska, pkt);
02569 }
02570
02571 if (timecode != AV_NOPTS_VALUE)
02572 timecode = duration ? timecode + duration : AV_NOPTS_VALUE;
02573 data += lace_size[n];
02574 }
02575 }
02576
02577 av_free(lace_size);
02578 av_free(origdata);
02579 return res;
02580 }
02581
02582 static int
02583 matroska_parse_blockgroup (MatroskaDemuxContext *matroska,
02584 uint64_t cluster_time)
02585 {
02586 int res = 0;
02587 uint32_t id;
02588 int is_bframe = 0;
02589 int is_keyframe = PKT_FLAG_KEY, last_num_packets = matroska->num_packets;
02590 uint64_t duration = AV_NOPTS_VALUE;
02591 uint8_t *data;
02592 int size = 0;
02593 int64_t pos = 0;
02594
02595 av_log(matroska->ctx, AV_LOG_DEBUG, "parsing blockgroup...\n");
02596
02597 while (res == 0) {
02598 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
02599 res = AVERROR(EIO);
02600 break;
02601 } else if (matroska->level_up) {
02602 matroska->level_up--;
02603 break;
02604 }
02605
02606 switch (id) {
02607
02608
02609
02610 case MATROSKA_ID_BLOCK: {
02611 pos = url_ftell(matroska->ctx->pb);
02612 res = ebml_read_binary(matroska, &id, &data, &size);
02613 break;
02614 }
02615
02616 case MATROSKA_ID_BLOCKDURATION: {
02617 if ((res = ebml_read_uint(matroska, &id, &duration)) < 0)
02618 break;
02619 break;
02620 }
02621
02622 case MATROSKA_ID_BLOCKREFERENCE: {
02623 int64_t num;
02624
02625
02626 is_keyframe = 0;
02627 if (last_num_packets != matroska->num_packets)
02628 matroska->packets[last_num_packets]->flags = 0;
02629 if ((res = ebml_read_sint(matroska, &id, &num)) < 0)
02630 break;
02631 if (num > 0)
02632 is_bframe = 1;
02633 break;
02634 }
02635
02636 default:
02637 av_log(matroska->ctx, AV_LOG_INFO,
02638 "Unknown entry 0x%x in blockgroup data\n", id);
02639
02640
02641 case EBML_ID_VOID:
02642 res = ebml_read_skip(matroska);
02643 break;
02644 }
02645
02646 if (matroska->level_up) {
02647 matroska->level_up--;
02648 break;
02649 }
02650 }
02651
02652 if (res)
02653 return res;
02654
02655 if (size > 0)
02656 res = matroska_parse_block(matroska, data, size, pos, cluster_time,
02657 duration, is_keyframe, is_bframe);
02658
02659 return res;
02660 }
02661
02662 static int
02663 matroska_parse_cluster (MatroskaDemuxContext *matroska)
02664 {
02665 int res = 0;
02666 uint32_t id;
02667 uint64_t cluster_time = 0;
02668 uint8_t *data;
02669 int64_t pos;
02670 int size;
02671
02672 av_log(matroska->ctx, AV_LOG_DEBUG,
02673 "parsing cluster at %"PRId64"\n", url_ftell(matroska->ctx->pb));
02674
02675 while (res == 0) {
02676 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
02677 res = AVERROR(EIO);
02678 break;
02679 } else if (matroska->level_up) {
02680 matroska->level_up--;
02681 break;
02682 }
02683
02684 switch (id) {
02685
02686 case MATROSKA_ID_CLUSTERTIMECODE: {
02687 uint64_t num;
02688 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
02689 break;
02690 cluster_time = num;
02691 break;
02692 }
02693
02694
02695 case MATROSKA_ID_BLOCKGROUP:
02696 if ((res = ebml_read_master(matroska, &id)) < 0)
02697 break;
02698 res = matroska_parse_blockgroup(matroska, cluster_time);
02699 break;
02700
02701 case MATROSKA_ID_SIMPLEBLOCK:
02702 pos = url_ftell(matroska->ctx->pb);
02703 res = ebml_read_binary(matroska, &id, &data, &size);
02704 if (res == 0)
02705 res = matroska_parse_block(matroska, data, size, pos,
02706 cluster_time, AV_NOPTS_VALUE,
02707 -1, 0);
02708 break;
02709
02710 default:
02711 av_log(matroska->ctx, AV_LOG_INFO,
02712 "Unknown entry 0x%x in cluster data\n", id);
02713
02714
02715 case EBML_ID_VOID:
02716 res = ebml_read_skip(matroska);
02717 break;
02718 }
02719
02720 if (matroska->level_up) {
02721 matroska->level_up--;
02722 break;
02723 }
02724 }
02725
02726 return res;
02727 }
02728
02729 static int
02730 matroska_read_packet (AVFormatContext *s,
02731 AVPacket *pkt)
02732 {
02733 MatroskaDemuxContext *matroska = s->priv_data;
02734 int res;
02735 uint32_t id;
02736
02737
02738 while (matroska_deliver_packet(matroska, pkt)) {
02739
02740
02741 if (matroska->done)
02742 return AVERROR(EIO);
02743
02744 res = 0;
02745 while (res == 0) {
02746 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
02747 return AVERROR(EIO);
02748 } else if (matroska->level_up) {
02749 matroska->level_up--;
02750 break;
02751 }
02752
02753 switch (id) {
02754 case MATROSKA_ID_CLUSTER:
02755 if ((res = ebml_read_master(matroska, &id)) < 0)
02756 break;
02757 if ((res = matroska_parse_cluster(matroska)) == 0)
02758 res = 1;
02759 break;
02760
02761 default:
02762 case EBML_ID_VOID:
02763 res = ebml_read_skip(matroska);
02764 break;
02765 }
02766
02767 if (matroska->level_up) {
02768 matroska->level_up--;
02769 break;
02770 }
02771 }
02772
02773 if (res == -1)
02774 matroska->done = 1;
02775 }
02776
02777 return 0;
02778 }
02779
02780 static int
02781 matroska_read_seek (AVFormatContext *s, int stream_index, int64_t timestamp,
02782 int flags)
02783 {
02784 MatroskaDemuxContext *matroska = s->priv_data;
02785 AVStream *st = s->streams[stream_index];
02786 int index;
02787
02788
02789 index = av_index_search_timestamp(st, timestamp, flags);
02790 if (index < 0)
02791 return 0;
02792
02793 matroska_clear_queue(matroska);
02794
02795
02796 url_fseek(s->pb, st->index_entries[index].pos, SEEK_SET);
02797 matroska->skip_to_keyframe = !(flags & AVSEEK_FLAG_ANY);
02798 matroska->skip_to_stream = st;
02799 matroska->peek_id = 0;
02800 return 0;
02801 }
02802
02803 static int
02804 matroska_read_close (AVFormatContext *s)
02805 {
02806 MatroskaDemuxContext *matroska = s->priv_data;
02807 int n = 0;
02808
02809 av_free(matroska->writing_app);
02810 av_free(matroska->muxing_app);
02811 av_free(matroska->index);
02812
02813 matroska_clear_queue(matroska);
02814
02815 for (n = 0; n < matroska->num_tracks; n++) {
02816 MatroskaTrack *track = matroska->tracks[n];
02817 av_free(track->codec_id);
02818 av_free(track->codec_name);
02819 av_free(track->codec_priv);
02820 av_free(track->name);
02821
02822 if (track->type == MATROSKA_TRACK_TYPE_AUDIO) {
02823 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
02824 av_free(audiotrack->buf);
02825 }
02826
02827 av_free(track);
02828 }
02829
02830 return 0;
02831 }
02832
02833 AVInputFormat matroska_demuxer = {
02834 "matroska",
02835 "Matroska file format",
02836 sizeof(MatroskaDemuxContext),
02837 matroska_probe,
02838 matroska_read_header,
02839 matroska_read_packet,
02840 matroska_read_close,
02841 matroska_read_seek,
02842 };