OLD | NEW |
---|---|
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 |
9 * License as published by the Free Software Foundation; either | 9 * License as published by the Free Software Foundation; either |
10 * version 2.1 of the License, or (at your option) any later version. | 10 * version 2.1 of the License, or (at your option) any later version. |
(...skipping 17 matching lines...) Expand all Loading... | |
28 * @see specs available on the Matroska project page: http://www.matroska.org/ | 28 * @see specs available on the Matroska project page: http://www.matroska.org/ |
29 */ | 29 */ |
30 | 30 |
31 #include <stdio.h> | 31 #include <stdio.h> |
32 #include "avformat.h" | 32 #include "avformat.h" |
33 #include "internal.h" | 33 #include "internal.h" |
34 #include "avio_internal.h" | 34 #include "avio_internal.h" |
35 /* For ff_codec_get_id(). */ | 35 /* For ff_codec_get_id(). */ |
36 #include "riff.h" | 36 #include "riff.h" |
37 #include "isom.h" | 37 #include "isom.h" |
38 #if CONFIG_SIPR_DECODER | |
38 #include "rm.h" | 39 #include "rm.h" |
40 #endif | |
39 #include "matroska.h" | 41 #include "matroska.h" |
40 #include "libavcodec/mpeg4audio.h" | 42 #include "libavcodec/mpeg4audio.h" |
41 #include "libavutil/intfloat.h" | 43 #include "libavutil/intfloat.h" |
42 #include "libavutil/intreadwrite.h" | 44 #include "libavutil/intreadwrite.h" |
43 #include "libavutil/avstring.h" | 45 #include "libavutil/avstring.h" |
46 #if HAVE_LZO1X_999_COMPRESS | |
44 #include "libavutil/lzo.h" | 47 #include "libavutil/lzo.h" |
48 #endif | |
45 #include "libavutil/dict.h" | 49 #include "libavutil/dict.h" |
46 #if CONFIG_ZLIB | 50 #if CONFIG_ZLIB |
47 #include <zlib.h> | 51 #include <zlib.h> |
48 #endif | 52 #endif |
49 #if CONFIG_BZLIB | 53 #if CONFIG_BZLIB |
50 #include <bzlib.h> | 54 #include <bzlib.h> |
51 #endif | 55 #endif |
52 | 56 |
53 typedef enum { | 57 typedef enum { |
54 EBML_NONE, | 58 EBML_NONE, |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
217 uint64_t id; | 221 uint64_t id; |
218 uint64_t pos; | 222 uint64_t pos; |
219 } MatroskaSeekhead; | 223 } MatroskaSeekhead; |
220 | 224 |
221 typedef struct { | 225 typedef struct { |
222 uint64_t start; | 226 uint64_t start; |
223 uint64_t length; | 227 uint64_t length; |
224 } MatroskaLevel; | 228 } MatroskaLevel; |
225 | 229 |
226 typedef struct { | 230 typedef struct { |
231 uint64_t timecode; | |
232 EbmlList blocks; | |
233 } MatroskaCluster; | |
234 | |
235 typedef struct { | |
227 AVFormatContext *ctx; | 236 AVFormatContext *ctx; |
228 | 237 |
229 /* EBML stuff */ | 238 /* EBML stuff */ |
230 int num_levels; | 239 int num_levels; |
231 MatroskaLevel levels[EBML_MAX_DEPTH]; | 240 MatroskaLevel levels[EBML_MAX_DEPTH]; |
232 int level_up; | 241 int level_up; |
233 uint32_t current_id; | 242 uint32_t current_id; |
234 | 243 |
235 uint64_t time_scale; | 244 uint64_t time_scale; |
236 double duration; | 245 double duration; |
(...skipping 14 matching lines...) Expand all Loading... | |
251 AVPacket *prev_pkt; | 260 AVPacket *prev_pkt; |
252 | 261 |
253 int done; | 262 int done; |
254 | 263 |
255 /* What to skip before effectively reading a packet. */ | 264 /* What to skip before effectively reading a packet. */ |
256 int skip_to_keyframe; | 265 int skip_to_keyframe; |
257 uint64_t skip_to_timecode; | 266 uint64_t skip_to_timecode; |
258 | 267 |
259 /* File has a CUES element, but we defer parsing until it is needed. */ | 268 /* File has a CUES element, but we defer parsing until it is needed. */ |
260 int cues_parsing_deferred; | 269 int cues_parsing_deferred; |
270 | |
271 int current_cluster_num_blocks; | |
272 int64_t current_cluster_pos; | |
273 MatroskaCluster current_cluster; | |
261 } MatroskaDemuxContext; | 274 } MatroskaDemuxContext; |
262 | 275 |
263 typedef struct { | 276 typedef struct { |
264 uint64_t duration; | 277 uint64_t duration; |
265 int64_t reference; | 278 int64_t reference; |
266 uint64_t non_simple; | 279 uint64_t non_simple; |
267 EbmlBin bin; | 280 EbmlBin bin; |
268 } MatroskaBlock; | 281 } MatroskaBlock; |
269 | 282 |
270 typedef struct { | |
271 uint64_t timecode; | |
272 EbmlList blocks; | |
273 } MatroskaCluster; | |
274 | |
275 static EbmlSyntax ebml_header[] = { | 283 static EbmlSyntax ebml_header[] = { |
276 { EBML_ID_EBMLREADVERSION, EBML_UINT, 0, offsetof(Ebml,version), {.u= EBML_VERSION} }, | 284 { EBML_ID_EBMLREADVERSION, EBML_UINT, 0, offsetof(Ebml,version), {.u= EBML_VERSION} }, |
277 { EBML_ID_EBMLMAXSIZELENGTH, EBML_UINT, 0, offsetof(Ebml,max_size), {.u =8} }, | 285 { EBML_ID_EBMLMAXSIZELENGTH, EBML_UINT, 0, offsetof(Ebml,max_size), {.u =8} }, |
278 { EBML_ID_EBMLMAXIDLENGTH, EBML_UINT, 0, offsetof(Ebml,id_length), {. u=4} }, | 286 { EBML_ID_EBMLMAXIDLENGTH, EBML_UINT, 0, offsetof(Ebml,id_length), {. u=4} }, |
279 { EBML_ID_DOCTYPE, EBML_STR, 0, offsetof(Ebml,doctype), {.s= "(none)"} }, | 287 { EBML_ID_DOCTYPE, EBML_STR, 0, offsetof(Ebml,doctype), {.s= "(none)"} }, |
280 { EBML_ID_DOCTYPEREADVERSION, EBML_UINT, 0, offsetof(Ebml,doctype_versio n), {.u=1} }, | 288 { EBML_ID_DOCTYPEREADVERSION, EBML_UINT, 0, offsetof(Ebml,doctype_versio n), {.u=1} }, |
281 { EBML_ID_EBMLVERSION, EBML_NONE }, | 289 { EBML_ID_EBMLVERSION, EBML_NONE }, |
282 { EBML_ID_DOCTYPEVERSION, EBML_NONE }, | 290 { EBML_ID_DOCTYPEVERSION, EBML_NONE }, |
283 { 0 } | 291 { 0 } |
284 }; | 292 }; |
(...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
535 | 543 |
536 static EbmlSyntax matroska_clusters[] = { | 544 static EbmlSyntax matroska_clusters[] = { |
537 { MATROSKA_ID_CLUSTER, EBML_NEST, 0, 0, {.n=matroska_cluster} }, | 545 { MATROSKA_ID_CLUSTER, EBML_NEST, 0, 0, {.n=matroska_cluster} }, |
538 { MATROSKA_ID_INFO, EBML_NONE }, | 546 { MATROSKA_ID_INFO, EBML_NONE }, |
539 { MATROSKA_ID_CUES, EBML_NONE }, | 547 { MATROSKA_ID_CUES, EBML_NONE }, |
540 { MATROSKA_ID_TAGS, EBML_NONE }, | 548 { MATROSKA_ID_TAGS, EBML_NONE }, |
541 { MATROSKA_ID_SEEKHEAD, EBML_NONE }, | 549 { MATROSKA_ID_SEEKHEAD, EBML_NONE }, |
542 { 0 } | 550 { 0 } |
543 }; | 551 }; |
544 | 552 |
553 static EbmlSyntax matroska_cluster_incremental_parsing[] = { | |
554 { MATROSKA_ID_CLUSTERTIMECODE,EBML_UINT,0, offsetof(MatroskaCluster,timecode ) }, | |
555 { MATROSKA_ID_BLOCKGROUP, EBML_NEST, sizeof(MatroskaBlock), offsetof(Mat roskaCluster,blocks), {.n=matroska_blockgroup} }, | |
556 { MATROSKA_ID_SIMPLEBLOCK, EBML_PASS, sizeof(MatroskaBlock), offsetof(Mat roskaCluster,blocks), {.n=matroska_blockgroup} }, | |
557 { MATROSKA_ID_CLUSTERPOSITION,EBML_NONE }, | |
558 { MATROSKA_ID_CLUSTERPREVSIZE,EBML_NONE }, | |
559 { MATROSKA_ID_INFO, EBML_NONE }, | |
560 { MATROSKA_ID_CUES, EBML_NONE }, | |
561 { MATROSKA_ID_TAGS, EBML_NONE }, | |
562 { MATROSKA_ID_SEEKHEAD, EBML_NONE }, | |
563 { MATROSKA_ID_CLUSTER, EBML_STOP }, | |
564 { 0 } | |
565 }; | |
566 | |
567 static EbmlSyntax matroska_cluster_incremental[] = { | |
568 { MATROSKA_ID_CLUSTERTIMECODE,EBML_UINT,0, offsetof(MatroskaCluster,timecode ) }, | |
569 { MATROSKA_ID_BLOCKGROUP, EBML_STOP }, | |
570 { MATROSKA_ID_SIMPLEBLOCK, EBML_STOP }, | |
571 { MATROSKA_ID_CLUSTERPOSITION,EBML_NONE }, | |
572 { MATROSKA_ID_CLUSTERPREVSIZE,EBML_NONE }, | |
573 { 0 } | |
574 }; | |
575 | |
576 static EbmlSyntax matroska_clusters_incremental[] = { | |
577 { MATROSKA_ID_CLUSTER, EBML_NEST, 0, 0, {.n=matroska_cluster_incremen tal} }, | |
578 { MATROSKA_ID_INFO, EBML_NONE }, | |
579 { MATROSKA_ID_CUES, EBML_NONE }, | |
580 { MATROSKA_ID_TAGS, EBML_NONE }, | |
581 { MATROSKA_ID_SEEKHEAD, EBML_NONE }, | |
582 { 0 } | |
583 }; | |
584 | |
545 static const char *matroska_doctypes[] = { "matroska", "webm" }; | 585 static const char *matroska_doctypes[] = { "matroska", "webm" }; |
546 | 586 |
547 /* | 587 /* |
548 * Return: Whether we reached the end of a level in the hierarchy or not. | 588 * Return: Whether we reached the end of a level in the hierarchy or not. |
549 */ | 589 */ |
550 static int ebml_level_end(MatroskaDemuxContext *matroska) | 590 static int ebml_level_end(MatroskaDemuxContext *matroska) |
551 { | 591 { |
552 AVIOContext *pb = matroska->ctx->pb; | 592 AVIOContext *pb = matroska->ctx->pb; |
553 int64_t pos = avio_tell(pb); | 593 int64_t pos = avio_tell(pb); |
554 | 594 |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
661 return 0; | 701 return 0; |
662 } | 702 } |
663 | 703 |
664 /* | 704 /* |
665 * Read the next element as an ASCII string. | 705 * Read the next element as an ASCII string. |
666 * 0 is success, < 0 is failure. | 706 * 0 is success, < 0 is failure. |
667 */ | 707 */ |
668 static int ebml_read_ascii(AVIOContext *pb, int size, char **str) | 708 static int ebml_read_ascii(AVIOContext *pb, int size, char **str) |
669 { | 709 { |
670 av_free(*str); | 710 av_free(*str); |
711 *str = NULL; | |
671 /* EBML strings are usually not 0-terminated, so we allocate one | 712 /* EBML strings are usually not 0-terminated, so we allocate one |
672 * byte more, read the string and NULL-terminate it ourselves. */ | 713 * byte more, read the string and NULL-terminate it ourselves. */ |
673 if (!(*str = av_malloc(size + 1))) | 714 if (size < 0 || !(*str = av_malloc(size + 1))) |
674 return AVERROR(ENOMEM); | 715 return AVERROR(ENOMEM); |
675 if (avio_read(pb, (uint8_t *) *str, size) != size) { | 716 if (avio_read(pb, (uint8_t *) *str, size) != size) { |
676 av_freep(str); | 717 av_freep(str); |
677 return AVERROR(EIO); | 718 return AVERROR(EIO); |
678 } | 719 } |
679 (*str)[size] = '\0'; | 720 (*str)[size] = '\0'; |
680 | 721 |
681 return 0; | 722 return 0; |
682 } | 723 } |
683 | 724 |
(...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
929 if (p->buf_size < 4 + size + total) | 970 if (p->buf_size < 4 + size + total) |
930 return 0; | 971 return 0; |
931 | 972 |
932 /* The header should contain a known document type. For now, | 973 /* The header should contain a known document type. For now, |
933 * we don't parse the whole header but simply check for the | 974 * we don't parse the whole header but simply check for the |
934 * availability of that array of characters inside the header. | 975 * availability of that array of characters inside the header. |
935 * Not fully fool-proof, but good enough. */ | 976 * Not fully fool-proof, but good enough. */ |
936 for (i = 0; i < FF_ARRAY_ELEMS(matroska_doctypes); i++) { | 977 for (i = 0; i < FF_ARRAY_ELEMS(matroska_doctypes); i++) { |
937 int probelen = strlen(matroska_doctypes[i]); | 978 int probelen = strlen(matroska_doctypes[i]); |
938 if (total < probelen) | 979 if (total < probelen) |
939 continue; | 980 continue; |
DaleCurtis
2012/02/04 18:06:20
11b_mkv_buffer_overflow was partially upstreamed,
| |
940 for (n = 4+size; n <= 4+size+total-probelen; n++) | 981 for (n = 4+size; n <= 4+size+total-probelen; n++) |
941 if (!memcmp(p->buf+n, matroska_doctypes[i], probelen)) | 982 if (!memcmp(p->buf+n, matroska_doctypes[i], probelen)) |
942 return AVPROBE_SCORE_MAX; | 983 return AVPROBE_SCORE_MAX; |
943 } | 984 } |
944 | 985 |
945 // probably valid EBML header but no recognized doctype | 986 // probably valid EBML header but no recognized doctype |
946 return AVPROBE_SCORE_MAX/2; | 987 return AVPROBE_SCORE_MAX/2; |
947 } | 988 } |
948 | 989 |
949 static MatroskaTrack *matroska_find_track_by_num(MatroskaDemuxContext *matroska, | 990 static MatroskaTrack *matroska_find_track_by_num(MatroskaDemuxContext *matroska, |
(...skipping 21 matching lines...) Expand all Loading... | |
971 int pkt_size = isize; | 1012 int pkt_size = isize; |
972 int result = 0; | 1013 int result = 0; |
973 int olen; | 1014 int olen; |
974 | 1015 |
975 if (pkt_size >= 10000000) | 1016 if (pkt_size >= 10000000) |
976 return -1; | 1017 return -1; |
977 | 1018 |
978 switch (encodings[0].compression.algo) { | 1019 switch (encodings[0].compression.algo) { |
979 case MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP: | 1020 case MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP: |
980 return encodings[0].compression.settings.size; | 1021 return encodings[0].compression.settings.size; |
1022 #if HAVE_LZO1X_999_COMPRESS | |
981 case MATROSKA_TRACK_ENCODING_COMP_LZO: | 1023 case MATROSKA_TRACK_ENCODING_COMP_LZO: |
982 do { | 1024 do { |
983 olen = pkt_size *= 3; | 1025 olen = pkt_size *= 3; |
984 pkt_data = av_realloc(pkt_data, pkt_size+AV_LZO_OUTPUT_PADDING); | 1026 pkt_data = av_realloc(pkt_data, pkt_size+AV_LZO_OUTPUT_PADDING); |
985 result = av_lzo1x_decode(pkt_data, &olen, data, &isize); | 1027 result = av_lzo1x_decode(pkt_data, &olen, data, &isize); |
986 } while (result==AV_LZO_OUTPUT_FULL && pkt_size<10000000); | 1028 } while (result==AV_LZO_OUTPUT_FULL && pkt_size<10000000); |
987 if (result) | 1029 if (result) |
988 goto failed; | 1030 goto failed; |
989 pkt_size -= olen; | 1031 pkt_size -= olen; |
990 break; | 1032 break; |
1033 #endif | |
991 #if CONFIG_ZLIB | 1034 #if CONFIG_ZLIB |
992 case MATROSKA_TRACK_ENCODING_COMP_ZLIB: { | 1035 case MATROSKA_TRACK_ENCODING_COMP_ZLIB: { |
993 z_stream zstream = {0}; | 1036 z_stream zstream = {0}; |
994 if (inflateInit(&zstream) != Z_OK) | 1037 if (inflateInit(&zstream) != Z_OK) |
995 return -1; | 1038 return -1; |
996 zstream.next_in = data; | 1039 zstream.next_in = data; |
997 zstream.avail_in = isize; | 1040 zstream.avail_in = isize; |
998 do { | 1041 do { |
999 pkt_size *= 3; | 1042 pkt_size *= 3; |
1000 newpktdata = av_realloc(pkt_data, pkt_size); | 1043 newpktdata = av_realloc(pkt_data, pkt_size); |
(...skipping 332 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1333 "(EBML version %"PRIu64", doctype %s, doc version %"PRIu64")\n", | 1376 "(EBML version %"PRIu64", doctype %s, doc version %"PRIu64")\n", |
1334 ebml.version, ebml.doctype, ebml.doctype_version); | 1377 ebml.version, ebml.doctype, ebml.doctype_version); |
1335 ebml_free(ebml_syntax, &ebml); | 1378 ebml_free(ebml_syntax, &ebml); |
1336 return AVERROR_PATCHWELCOME; | 1379 return AVERROR_PATCHWELCOME; |
1337 } else if (ebml.doctype_version == 3) { | 1380 } else if (ebml.doctype_version == 3) { |
1338 av_log(matroska->ctx, AV_LOG_WARNING, | 1381 av_log(matroska->ctx, AV_LOG_WARNING, |
1339 "EBML header using unsupported features\n" | 1382 "EBML header using unsupported features\n" |
1340 "(EBML version %"PRIu64", doctype %s, doc version %"PRIu64")\n", | 1383 "(EBML version %"PRIu64", doctype %s, doc version %"PRIu64")\n", |
1341 ebml.version, ebml.doctype, ebml.doctype_version); | 1384 ebml.version, ebml.doctype, ebml.doctype_version); |
1342 } | 1385 } |
1386 av_dict_set(&s->metadata, "doctype", ebml.doctype, 0); | |
1343 for (i = 0; i < FF_ARRAY_ELEMS(matroska_doctypes); i++) | 1387 for (i = 0; i < FF_ARRAY_ELEMS(matroska_doctypes); i++) |
1344 if (!strcmp(ebml.doctype, matroska_doctypes[i])) | 1388 if (!strcmp(ebml.doctype, matroska_doctypes[i])) |
1345 break; | 1389 break; |
1346 if (i >= FF_ARRAY_ELEMS(matroska_doctypes)) { | 1390 if (i >= FF_ARRAY_ELEMS(matroska_doctypes)) { |
1347 av_log(s, AV_LOG_WARNING, "Unknown EBML doctype '%s'\n", ebml.doctype); | 1391 av_log(s, AV_LOG_WARNING, "Unknown EBML doctype '%s'\n", ebml.doctype); |
1348 } | 1392 } |
1349 ebml_free(ebml_syntax, &ebml); | 1393 ebml_free(ebml_syntax, &ebml); |
1350 | 1394 |
1351 /* The next thing is a segment. */ | 1395 /* The next thing is a segment. */ |
1352 if ((res = ebml_parse(matroska, matroska_segments, matroska)) < 0) | 1396 if ((res = ebml_parse(matroska, matroska_segments, matroska)) < 0) |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1395 fourcc = AV_RL32(track->video.color_space.data); | 1439 fourcc = AV_RL32(track->video.color_space.data); |
1396 } else if (track->type == MATROSKA_TRACK_TYPE_AUDIO) { | 1440 } else if (track->type == MATROSKA_TRACK_TYPE_AUDIO) { |
1397 if (!track->audio.out_samplerate) | 1441 if (!track->audio.out_samplerate) |
1398 track->audio.out_samplerate = track->audio.samplerate; | 1442 track->audio.out_samplerate = track->audio.samplerate; |
1399 } | 1443 } |
1400 if (encodings_list->nb_elem > 1) { | 1444 if (encodings_list->nb_elem > 1) { |
1401 av_log(matroska->ctx, AV_LOG_ERROR, | 1445 av_log(matroska->ctx, AV_LOG_ERROR, |
1402 "Multiple combined encodings not supported"); | 1446 "Multiple combined encodings not supported"); |
1403 } else if (encodings_list->nb_elem == 1) { | 1447 } else if (encodings_list->nb_elem == 1) { |
1404 if (encodings[0].type || | 1448 if (encodings[0].type || |
1405 (encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_H EADERSTRIP && | 1449 (encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_H EADERSTRIP |
1406 #if CONFIG_ZLIB | 1450 #if CONFIG_ZLIB |
1407 encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_Z LIB && | 1451 && encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COM P_ZLIB |
1408 #endif | 1452 #endif |
1409 #if CONFIG_BZLIB | 1453 #if CONFIG_BZLIB |
1410 encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_B ZLIB && | 1454 && encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COM P_BZLIB |
1411 #endif | 1455 #endif |
1412 encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_L ZO)) { | 1456 #if HAVE_LZO1X_999_COMPRESS |
1457 && encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COM P_LZO | |
1458 #endif | |
1459 )) { | |
1413 encodings[0].scope = 0; | 1460 encodings[0].scope = 0; |
1414 av_log(matroska->ctx, AV_LOG_ERROR, | 1461 av_log(matroska->ctx, AV_LOG_ERROR, |
1415 "Unsupported encoding type"); | 1462 "Unsupported encoding type"); |
1416 } else if (track->codec_priv.size && encodings[0].scope&2) { | 1463 } else if (track->codec_priv.size && encodings[0].scope&2) { |
1417 uint8_t *codec_priv = track->codec_priv.data; | 1464 uint8_t *codec_priv = track->codec_priv.data; |
1418 int offset = matroska_decode_buffer(&track->codec_priv.data, | 1465 int offset = matroska_decode_buffer(&track->codec_priv.data, |
1419 &track->codec_priv.size, | 1466 &track->codec_priv.size, |
1420 track); | 1467 track); |
1421 if (offset < 0) { | 1468 if (offset < 0) { |
1422 track->codec_priv.data = NULL; | 1469 track->codec_priv.data = NULL; |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1530 track->audio.coded_framesize = avio_rb32(&b); | 1577 track->audio.coded_framesize = avio_rb32(&b); |
1531 avio_skip(&b, 12); | 1578 avio_skip(&b, 12); |
1532 track->audio.sub_packet_h = avio_rb16(&b); | 1579 track->audio.sub_packet_h = avio_rb16(&b); |
1533 track->audio.frame_size = avio_rb16(&b); | 1580 track->audio.frame_size = avio_rb16(&b); |
1534 track->audio.sub_packet_size = avio_rb16(&b); | 1581 track->audio.sub_packet_size = avio_rb16(&b); |
1535 track->audio.buf = av_malloc(track->audio.frame_size * track->audio. sub_packet_h); | 1582 track->audio.buf = av_malloc(track->audio.frame_size * track->audio. sub_packet_h); |
1536 if (codec_id == CODEC_ID_RA_288) { | 1583 if (codec_id == CODEC_ID_RA_288) { |
1537 st->codec->block_align = track->audio.coded_framesize; | 1584 st->codec->block_align = track->audio.coded_framesize; |
1538 track->codec_priv.size = 0; | 1585 track->codec_priv.size = 0; |
1539 } else { | 1586 } else { |
1587 #if CONFIG_SIPR_DECODER | |
1540 if (codec_id == CODEC_ID_SIPR && flavor < 4) { | 1588 if (codec_id == CODEC_ID_SIPR && flavor < 4) { |
1541 const int sipr_bit_rate[4] = { 6504, 8496, 5000, 16000 }; | 1589 const int sipr_bit_rate[4] = { 6504, 8496, 5000, 16000 }; |
1542 track->audio.sub_packet_size = ff_sipr_subpk_size[flavor]; | 1590 track->audio.sub_packet_size = ff_sipr_subpk_size[flavor]; |
1543 st->codec->bit_rate = sipr_bit_rate[flavor]; | 1591 st->codec->bit_rate = sipr_bit_rate[flavor]; |
1544 } | 1592 } |
1593 #endif | |
1545 st->codec->block_align = track->audio.sub_packet_size; | 1594 st->codec->block_align = track->audio.sub_packet_size; |
1546 extradata_offset = 78; | 1595 extradata_offset = 78; |
1547 } | 1596 } |
1548 } | 1597 } |
1549 track->codec_priv.size -= extradata_offset; | 1598 track->codec_priv.size -= extradata_offset; |
1550 | 1599 |
1551 if (codec_id == CODEC_ID_NONE) | 1600 if (codec_id == CODEC_ID_NONE) |
1552 av_log(matroska->ctx, AV_LOG_INFO, | 1601 av_log(matroska->ctx, AV_LOG_INFO, |
1553 "Unknown/unsupported CodecID %s.\n", track->codec_id); | 1602 "Unknown/unsupported CodecID %s.\n", track->codec_id); |
1554 | 1603 |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1614 for (k=0; k < matroska->tracks.nb_elem; k++) | 1663 for (k=0; k < matroska->tracks.nb_elem; k++) |
1615 if (planes[j].uid == tracks[k].uid) { | 1664 if (planes[j].uid == tracks[k].uid) { |
1616 av_dict_set(&s->streams[k]->metadata, | 1665 av_dict_set(&s->streams[k]->metadata, |
1617 "stereo_mode", buf, 0); | 1666 "stereo_mode", buf, 0); |
1618 break; | 1667 break; |
1619 } | 1668 } |
1620 } | 1669 } |
1621 } else if (track->type == MATROSKA_TRACK_TYPE_AUDIO) { | 1670 } else if (track->type == MATROSKA_TRACK_TYPE_AUDIO) { |
1622 st->codec->codec_type = AVMEDIA_TYPE_AUDIO; | 1671 st->codec->codec_type = AVMEDIA_TYPE_AUDIO; |
1623 st->codec->sample_rate = track->audio.out_samplerate; | 1672 st->codec->sample_rate = track->audio.out_samplerate; |
1673 if (st->codec->sample_rate <= 0) | |
1674 st->codec->sample_rate = 1; | |
1624 st->codec->channels = track->audio.channels; | 1675 st->codec->channels = track->audio.channels; |
1625 if (st->codec->codec_id != CODEC_ID_AAC) | 1676 if (st->codec->codec_id != CODEC_ID_AAC) |
1626 st->need_parsing = AVSTREAM_PARSE_HEADERS; | 1677 st->need_parsing = AVSTREAM_PARSE_HEADERS; |
1627 } else if (track->type == MATROSKA_TRACK_TYPE_SUBTITLE) { | 1678 } else if (track->type == MATROSKA_TRACK_TYPE_SUBTITLE) { |
1628 st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE; | 1679 st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE; |
1629 } | 1680 } |
1630 } | 1681 } |
1631 | 1682 |
1632 attachements = attachements_list->elem; | 1683 attachements = attachements_list->elem; |
1633 for (j=0; j<attachements_list->nb_elem; j++) { | 1684 for (j=0; j<attachements_list->nb_elem; j++) { |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1692 if (matroska->num_packets > 1) { | 1743 if (matroska->num_packets > 1) { |
1693 void *newpackets; | 1744 void *newpackets; |
1694 memmove(&matroska->packets[0], &matroska->packets[1], | 1745 memmove(&matroska->packets[0], &matroska->packets[1], |
1695 (matroska->num_packets - 1) * sizeof(AVPacket *)); | 1746 (matroska->num_packets - 1) * sizeof(AVPacket *)); |
1696 newpackets = av_realloc(matroska->packets, | 1747 newpackets = av_realloc(matroska->packets, |
1697 (matroska->num_packets - 1) * sizeof(AVPacket *)); | 1748 (matroska->num_packets - 1) * sizeof(AVPacket *)); |
1698 if (newpackets) | 1749 if (newpackets) |
1699 matroska->packets = newpackets; | 1750 matroska->packets = newpackets; |
1700 } else { | 1751 } else { |
1701 av_freep(&matroska->packets); | 1752 av_freep(&matroska->packets); |
1753 matroska->prev_pkt = NULL; | |
1702 } | 1754 } |
1703 matroska->num_packets--; | 1755 matroska->num_packets--; |
1704 return 0; | 1756 return 0; |
1705 } | 1757 } |
1706 | 1758 |
1707 return -1; | 1759 return -1; |
1708 } | 1760 } |
1709 | 1761 |
1710 /* | 1762 /* |
1711 * Free all packets in our internal queue. | 1763 * Free all packets in our internal queue. |
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1880 for (x=0; x<h/2; x++) | 1932 for (x=0; x<h/2; x++) |
1881 memcpy(track->audio.buf+x*2*w+y*cfs, | 1933 memcpy(track->audio.buf+x*2*w+y*cfs, |
1882 data+x*cfs, cfs); | 1934 data+x*cfs, cfs); |
1883 else if (st->codec->codec_id == CODEC_ID_SIPR) | 1935 else if (st->codec->codec_id == CODEC_ID_SIPR) |
1884 memcpy(track->audio.buf + y*w, data, w); | 1936 memcpy(track->audio.buf + y*w, data, w); |
1885 else | 1937 else |
1886 for (x=0; x<w/sps; x++) | 1938 for (x=0; x<w/sps; x++) |
1887 memcpy(track->audio.buf+sps*(h*x+((h+1)/2)*(y&1)+(y> >1)), data+x*sps, sps); | 1939 memcpy(track->audio.buf+sps*(h*x+((h+1)/2)*(y&1)+(y> >1)), data+x*sps, sps); |
1888 | 1940 |
1889 if (++track->audio.sub_packet_cnt >= h) { | 1941 if (++track->audio.sub_packet_cnt >= h) { |
1942 #if CONFIG_SIPR_DECODER | |
1890 if (st->codec->codec_id == CODEC_ID_SIPR) | 1943 if (st->codec->codec_id == CODEC_ID_SIPR) |
1891 ff_rm_reorder_sipr_data(track->audio.buf, h, w); | 1944 ff_rm_reorder_sipr_data(track->audio.buf, h, w); |
1945 #endif | |
1892 track->audio.sub_packet_cnt = 0; | 1946 track->audio.sub_packet_cnt = 0; |
1893 track->audio.pkt_cnt = h*w / a; | 1947 track->audio.pkt_cnt = h*w / a; |
1894 } | 1948 } |
1895 } | 1949 } |
1896 while (track->audio.pkt_cnt) { | 1950 while (track->audio.pkt_cnt) { |
1897 pkt = av_mallocz(sizeof(AVPacket)); | 1951 pkt = av_mallocz(sizeof(AVPacket)); |
1898 av_new_packet(pkt, a); | 1952 av_new_packet(pkt, a); |
1899 memcpy(pkt->data, track->audio.buf | 1953 memcpy(pkt->data, track->audio.buf |
1900 + a * (h*w / a - track->audio.pkt_cnt--), a); | 1954 + a * (h*w / a - track->audio.pkt_cnt--), a); |
1901 pkt->pts = track->audio.buf_timecode; | 1955 pkt->pts = track->audio.buf_timecode; |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1994 blocks[i].bin.data, blocks[i].bin.size, | 2048 blocks[i].bin.data, blocks[i].bin.size, |
1995 blocks[i].bin.pos, cluster.timecode, | 2049 blocks[i].bin.pos, cluster.timecode, |
1996 blocks[i].duration, is_keyframe, | 2050 blocks[i].duration, is_keyframe, |
1997 pos); | 2051 pos); |
1998 } | 2052 } |
1999 ebml_free(matroska_cluster, &cluster); | 2053 ebml_free(matroska_cluster, &cluster); |
2000 if (res < 0) matroska->done = 1; | 2054 if (res < 0) matroska->done = 1; |
2001 return res; | 2055 return res; |
2002 } | 2056 } |
2003 | 2057 |
2058 static int matroska_parse_cluster_incremental(MatroskaDemuxContext *matroska) | |
2059 { | |
2060 EbmlList *blocks_list; | |
2061 MatroskaBlock *blocks; | |
2062 int i, res; | |
2063 res = ebml_parse(matroska, | |
2064 matroska_cluster_incremental_parsing, | |
2065 &matroska->current_cluster); | |
2066 if (res == 1) { | |
2067 /* New Cluster */ | |
2068 if (matroska->current_cluster_pos) | |
2069 ebml_level_end(matroska); | |
2070 ebml_free(matroska_cluster, &matroska->current_cluster); | |
2071 memset(&matroska->current_cluster, 0, sizeof(MatroskaCluster)); | |
2072 matroska->current_cluster_num_blocks = 0; | |
2073 matroska->current_cluster_pos = avio_tell(matroska->ctx->pb); | |
2074 matroska->prev_pkt = NULL; | |
2075 /* sizeof the ID which was already read */ | |
2076 if (matroska->current_id) | |
2077 matroska->current_cluster_pos -= 4; | |
2078 res = ebml_parse(matroska, | |
2079 matroska_clusters_incremental, | |
2080 &matroska->current_cluster); | |
2081 /* Try parsing the block agiain. */ | |
2082 if (res == 1) | |
2083 res = ebml_parse(matroska, | |
2084 matroska_cluster_incremental_parsing, | |
2085 &matroska->current_cluster); | |
2086 } | |
2087 | |
2088 if (!res && | |
2089 matroska->current_cluster_num_blocks < | |
2090 matroska->current_cluster.blocks.nb_elem) { | |
2091 blocks_list = &matroska->current_cluster.blocks; | |
2092 blocks = blocks_list->elem; | |
2093 | |
2094 matroska->current_cluster_num_blocks = blocks_list->nb_elem; | |
2095 i = blocks_list->nb_elem - 1; | |
2096 if (blocks[i].bin.size > 0 && blocks[i].bin.data) { | |
2097 int is_keyframe = blocks[i].non_simple ? !blocks[i].reference : -1; | |
2098 res=matroska_parse_block(matroska, | |
2099 blocks[i].bin.data, blocks[i].bin.size, | |
2100 blocks[i].bin.pos, | |
2101 matroska->current_cluster.timecode, | |
2102 blocks[i].duration, is_keyframe, | |
2103 matroska->current_cluster_pos); | |
2104 } | |
2105 } | |
2106 | |
2107 if (res < 0) matroska->done = 1; | |
2108 return res; | |
2109 } | |
2110 | |
2004 static int matroska_read_packet(AVFormatContext *s, AVPacket *pkt) | 2111 static int matroska_read_packet(AVFormatContext *s, AVPacket *pkt) |
2005 { | 2112 { |
2006 MatroskaDemuxContext *matroska = s->priv_data; | 2113 MatroskaDemuxContext *matroska = s->priv_data; |
2114 int ret = 0; | |
2007 | 2115 |
2008 while (matroska_deliver_packet(matroska, pkt)) { | 2116 while (!ret && matroska_deliver_packet(matroska, pkt)) { |
2009 if (matroska->done) | 2117 if (matroska->done) |
2010 return AVERROR_EOF; | 2118 return AVERROR_EOF; |
2011 matroska_parse_cluster(matroska); | 2119 ret = matroska_parse_cluster_incremental(matroska); |
2012 } | 2120 } |
2013 | 2121 |
2014 return 0; | 2122 return ret; |
2015 } | 2123 } |
2016 | 2124 |
2017 static int matroska_read_seek(AVFormatContext *s, int stream_index, | 2125 static int matroska_read_seek(AVFormatContext *s, int stream_index, |
2018 int64_t timestamp, int flags) | 2126 int64_t timestamp, int flags) |
2019 { | 2127 { |
2020 MatroskaDemuxContext *matroska = s->priv_data; | 2128 MatroskaDemuxContext *matroska = s->priv_data; |
2021 MatroskaTrack *tracks = matroska->tracks.elem; | 2129 MatroskaTrack *tracks = matroska->tracks.elem; |
2022 AVStream *st = s->streams[stream_index]; | 2130 AVStream *st = s->streams[stream_index]; |
2023 int i, index, index_sub, index_min; | 2131 int i, index, index_sub, index_min; |
2024 | 2132 |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2075 { | 2183 { |
2076 MatroskaDemuxContext *matroska = s->priv_data; | 2184 MatroskaDemuxContext *matroska = s->priv_data; |
2077 MatroskaTrack *tracks = matroska->tracks.elem; | 2185 MatroskaTrack *tracks = matroska->tracks.elem; |
2078 int n; | 2186 int n; |
2079 | 2187 |
2080 matroska_clear_queue(matroska); | 2188 matroska_clear_queue(matroska); |
2081 | 2189 |
2082 for (n=0; n < matroska->tracks.nb_elem; n++) | 2190 for (n=0; n < matroska->tracks.nb_elem; n++) |
2083 if (tracks[n].type == MATROSKA_TRACK_TYPE_AUDIO) | 2191 if (tracks[n].type == MATROSKA_TRACK_TYPE_AUDIO) |
2084 av_free(tracks[n].audio.buf); | 2192 av_free(tracks[n].audio.buf); |
2193 ebml_free(matroska_cluster, &matroska->current_cluster); | |
2085 ebml_free(matroska_segment, matroska); | 2194 ebml_free(matroska_segment, matroska); |
2086 | 2195 |
2087 return 0; | 2196 return 0; |
2088 } | 2197 } |
2089 | 2198 |
2090 AVInputFormat ff_matroska_demuxer = { | 2199 AVInputFormat ff_matroska_demuxer = { |
2091 .name = "matroska,webm", | 2200 .name = "matroska,webm", |
2092 .long_name = NULL_IF_CONFIG_SMALL("Matroska/WebM file format"), | 2201 .long_name = NULL_IF_CONFIG_SMALL("Matroska/WebM file format"), |
2093 .priv_data_size = sizeof(MatroskaDemuxContext), | 2202 .priv_data_size = sizeof(MatroskaDemuxContext), |
2094 .read_probe = matroska_probe, | 2203 .read_probe = matroska_probe, |
2095 .read_header = matroska_read_header, | 2204 .read_header = matroska_read_header, |
2096 .read_packet = matroska_read_packet, | 2205 .read_packet = matroska_read_packet, |
2097 .read_close = matroska_read_close, | 2206 .read_close = matroska_read_close, |
2098 .read_seek = matroska_read_seek, | 2207 .read_seek = matroska_read_seek, |
2099 }; | 2208 }; |
OLD | NEW |