2 * Matroska file demuxer (no muxer yet)
3 * Copyright (c) 2003-2004 The ffmpeg Project
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * Matroska file demuxer
25 * by Ronald Bultje <rbultje@ronald.bitfreak.net>
26 * with a little help from Moritz Bunkus <moritz@bunkus.org>
27 * Specs available on the matroska project page:
28 * http://www.matroska.org/.
32 /* For codec_get_id(). */
36 #include "libavcodec/mpeg4audio.h"
37 #include "libavutil/intfloat_readwrite.h"
38 #include "libavutil/avstring.h"
39 #include "libavutil/lzo.h"
59 typedef const struct EbmlSyntax {
68 const struct EbmlSyntax *n;
88 uint64_t doctype_version;
91 typedef struct Track {
92 MatroskaTrackType type;
94 /* Unique track number and track ID. stream_index is the index that
95 * the calling app uses for this track. */
105 unsigned char *codec_priv;
109 uint64_t default_duration;
110 uint64_t flag_default;
113 MatroskaTrackEncodingCompAlgo encoding_algo;
114 uint8_t *encoding_settings;
115 int encoding_settings_len;
118 typedef struct MatroskaVideoTrack {
129 } MatroskaVideoTrack;
131 typedef struct MatroskaAudioTrack {
136 int internal_samplerate;
140 /* real audio header */
149 } MatroskaAudioTrack;
151 typedef struct MatroskaSubtitleTrack {
154 } MatroskaSubtitleTrack;
156 #define MAX_TRACK_SIZE (FFMAX3(sizeof(MatroskaVideoTrack), \
157 sizeof(MatroskaAudioTrack), \
158 sizeof(MatroskaSubtitleTrack)))
160 typedef struct MatroskaLevel {
165 typedef struct MatroskaDemuxIndex {
166 uint64_t pos; /* of the corresponding *cluster*! */
167 uint16_t track; /* reference to 'num' */
168 uint64_t time; /* in nanoseconds */
169 } MatroskaDemuxIndex;
171 typedef struct MatroskaDemuxContext {
172 AVFormatContext *ctx;
176 MatroskaLevel levels[EBML_MAX_DEPTH];
179 /* timescale in the file */
182 /* num_streams is the number of streams that av_new_stream() was called
183 * for ( = that are available to the calling program). */
186 MatroskaTrack *tracks[MAX_STREAMS];
188 /* cache for ID peeking */
191 /* byte position of the segment inside the stream */
192 offset_t segment_start;
194 /* The packet queue. */
198 /* have we already parse metadata/cues/clusters? */
203 /* The index for seeking. */
205 MatroskaDemuxIndex *index;
207 /* What to skip before effectively reading a packet. */
208 int skip_to_keyframe;
209 AVStream *skip_to_stream;
210 } MatroskaDemuxContext;
212 #define ARRAY_SIZE(x) (sizeof(x)/sizeof(*x))
214 static EbmlSyntax ebml_header[] = {
215 { EBML_ID_EBMLREADVERSION, EBML_UINT, 0, offsetof(Ebml,version), {.u=EBML_VERSION} },
216 { EBML_ID_EBMLMAXSIZELENGTH, EBML_UINT, 0, offsetof(Ebml,max_size), {.u=8} },
217 { EBML_ID_EBMLMAXIDLENGTH, EBML_UINT, 0, offsetof(Ebml,id_length), {.u=4} },
218 { EBML_ID_DOCTYPE, EBML_STR, 0, offsetof(Ebml,doctype), {.s="(none)"} },
219 { EBML_ID_DOCTYPEREADVERSION, EBML_UINT, 0, offsetof(Ebml,doctype_version), {.u=1} },
220 { EBML_ID_EBMLVERSION, EBML_NONE },
221 { EBML_ID_DOCTYPEVERSION, EBML_NONE },
222 { EBML_ID_VOID, EBML_NONE },
226 static EbmlSyntax ebml_syntax[] = {
227 { EBML_ID_HEADER, EBML_NEST, 0, 0, {.n=ebml_header} },
232 * The first few functions handle EBML file parsing. The rest
233 * is the document interpretation. Matroska really just is a
238 * Return: the amount of levels in the hierarchy that the
239 * current element lies higher than the previous one.
240 * The opposite isn't done - that's auto-done using master
245 ebml_read_element_level_up (MatroskaDemuxContext *matroska)
247 ByteIOContext *pb = matroska->ctx->pb;
248 offset_t pos = url_ftell(pb);
251 while (matroska->num_levels > 0) {
252 MatroskaLevel *level = &matroska->levels[matroska->num_levels - 1];
254 if (pos >= level->start + level->length) {
255 matroska->num_levels--;
266 * Read: an "EBML number", which is defined as a variable-length
267 * array of bytes. The first byte indicates the length by giving a
268 * number of 0-bits followed by a one. The position of the first
269 * "one" bit inside the first byte indicates the length of this
271 * Returns: num. of bytes read. < 0 on error.
275 ebml_read_num (MatroskaDemuxContext *matroska,
279 ByteIOContext *pb = matroska->ctx->pb;
280 int len_mask = 0x80, read = 1, n = 1;
283 /* the first byte tells us the length in bytes - get_byte() can normally
284 * return 0, but since that's not a valid first ebmlID byte, we can
285 * use it safely here to catch EOS. */
286 if (!(total = get_byte(pb))) {
287 /* we might encounter EOS here */
289 offset_t pos = url_ftell(pb);
290 av_log(matroska->ctx, AV_LOG_ERROR,
291 "Read error at pos. %"PRIu64" (0x%"PRIx64")\n",
294 return AVERROR(EIO); /* EOS or actual I/O error */
297 /* get the length of the EBML number */
298 while (read <= max_size && !(total & len_mask)) {
302 if (read > max_size) {
303 offset_t pos = url_ftell(pb) - 1;
304 av_log(matroska->ctx, AV_LOG_ERROR,
305 "Invalid EBML number size tag 0x%02x at pos %"PRIu64" (0x%"PRIx64")\n",
306 (uint8_t) total, pos, pos);
307 return AVERROR_INVALIDDATA;
310 /* read out length */
313 total = (total << 8) | get_byte(pb);
321 * Read: the element content data ID.
322 * Return: the number of bytes read or < 0 on error.
326 ebml_read_element_id (MatroskaDemuxContext *matroska,
333 /* if we re-call this, use our cached ID */
334 if (matroska->peek_id != 0) {
337 *id = matroska->peek_id;
341 /* read out the "EBML number", include tag in ID */
342 if ((read = ebml_read_num(matroska, 4, &total)) < 0)
344 *id = matroska->peek_id = total | (1 << (read * 7));
348 *level_up = ebml_read_element_level_up(matroska);
354 * Read: element content length.
355 * Return: the number of bytes read or < 0 on error.
359 ebml_read_element_length (MatroskaDemuxContext *matroska,
362 /* clear cache since we're now beyond that data point */
363 matroska->peek_id = 0;
365 /* read out the "EBML number", include tag in ID */
366 return ebml_read_num(matroska, 8, length);
370 * Return: the ID of the next element, or 0 on error.
371 * Level_up contains the amount of levels that this
372 * next element lies higher than the previous one.
376 ebml_peek_id (MatroskaDemuxContext *matroska,
381 if (ebml_read_element_id(matroska, &id, level_up) < 0)
388 * Seek to a given offset.
389 * 0 is success, -1 is failure.
393 ebml_read_seek (MatroskaDemuxContext *matroska,
396 ByteIOContext *pb = matroska->ctx->pb;
398 /* clear ID cache, if any */
399 matroska->peek_id = 0;
401 return (url_fseek(pb, offset, SEEK_SET) == offset) ? 0 : -1;
405 * Skip the next element.
406 * 0 is success, -1 is failure.
410 ebml_read_skip (MatroskaDemuxContext *matroska)
412 ByteIOContext *pb = matroska->ctx->pb;
417 if ((res = ebml_read_element_id(matroska, &id, NULL)) < 0 ||
418 (res = ebml_read_element_length(matroska, &length)) < 0)
421 url_fskip(pb, length);
427 * Read the next element as an unsigned int.
428 * 0 is success, < 0 is failure.
432 ebml_read_uint (MatroskaDemuxContext *matroska,
436 ByteIOContext *pb = matroska->ctx->pb;
437 int n = 0, size, res;
440 if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
441 (res = ebml_read_element_length(matroska, &rlength)) < 0)
444 if (size < 1 || size > 8) {
445 offset_t pos = url_ftell(pb);
446 av_log(matroska->ctx, AV_LOG_ERROR,
447 "Invalid uint element size %d at position %"PRId64" (0x%"PRIx64")\n",
449 return AVERROR_INVALIDDATA;
452 /* big-endian ordening; build up number */
455 *num = (*num << 8) | get_byte(pb);
461 * Read the next element as a signed int.
462 * 0 is success, < 0 is failure.
466 ebml_read_sint (MatroskaDemuxContext *matroska,
470 ByteIOContext *pb = matroska->ctx->pb;
471 int size, n = 1, negative = 0, res;
474 if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
475 (res = ebml_read_element_length(matroska, &rlength)) < 0)
478 if (size < 1 || size > 8) {
479 offset_t pos = url_ftell(pb);
480 av_log(matroska->ctx, AV_LOG_ERROR,
481 "Invalid sint element size %d at position %"PRId64" (0x%"PRIx64")\n",
483 return AVERROR_INVALIDDATA;
485 if ((*num = get_byte(pb)) & 0x80) {
490 *num = (*num << 8) | get_byte(pb);
494 *num = *num - (1LL << ((8 * size) - 1));
500 * Read the next element as a float.
501 * 0 is success, < 0 is failure.
505 ebml_read_float (MatroskaDemuxContext *matroska,
509 ByteIOContext *pb = matroska->ctx->pb;
513 if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
514 (res = ebml_read_element_length(matroska, &rlength)) < 0)
519 *num= av_int2flt(get_be32(pb));
521 *num= av_int2dbl(get_be64(pb));
523 offset_t pos = url_ftell(pb);
524 av_log(matroska->ctx, AV_LOG_ERROR,
525 "Invalid float element size %d at position %"PRIu64" (0x%"PRIx64")\n",
527 return AVERROR_INVALIDDATA;
534 * Read the next element as an ASCII string.
535 * 0 is success, < 0 is failure.
539 ebml_read_ascii (MatroskaDemuxContext *matroska,
543 ByteIOContext *pb = matroska->ctx->pb;
547 if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
548 (res = ebml_read_element_length(matroska, &rlength)) < 0)
552 /* ebml strings are usually not 0-terminated, so we allocate one
553 * byte more, read the string and NULL-terminate it ourselves. */
554 if (size < 0 || !(*str = av_malloc(size + 1))) {
555 av_log(matroska->ctx, AV_LOG_ERROR, "Memory allocation failed\n");
556 return AVERROR(ENOMEM);
558 if (get_buffer(pb, (uint8_t *) *str, size) != size) {
559 offset_t pos = url_ftell(pb);
560 av_log(matroska->ctx, AV_LOG_ERROR,
561 "Read error at pos. %"PRIu64" (0x%"PRIx64")\n", pos, pos);
571 * Read the next element as a UTF-8 string.
572 * 0 is success, < 0 is failure.
576 ebml_read_utf8 (MatroskaDemuxContext *matroska,
580 return ebml_read_ascii(matroska, id, str);
584 * Read the next element, but only the header. The contents
585 * are supposed to be sub-elements which can be read separately.
586 * 0 is success, < 0 is failure.
590 ebml_read_master (MatroskaDemuxContext *matroska,
593 ByteIOContext *pb = matroska->ctx->pb;
595 MatroskaLevel *level;
598 if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
599 (res = ebml_read_element_length(matroska, &length)) < 0)
602 /* protect... (Heaven forbids that the '>' is true) */
603 if (matroska->num_levels >= EBML_MAX_DEPTH) {
604 av_log(matroska->ctx, AV_LOG_ERROR,
605 "File moves beyond max. allowed depth (%d)\n", EBML_MAX_DEPTH);
606 return AVERROR(ENOSYS);
610 level = &matroska->levels[matroska->num_levels++];
611 level->start = url_ftell(pb);
612 level->length = length;
618 * Read the next element as binary data.
619 * 0 is success, < 0 is failure.
623 ebml_read_binary (MatroskaDemuxContext *matroska,
628 ByteIOContext *pb = matroska->ctx->pb;
632 if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
633 (res = ebml_read_element_length(matroska, &rlength)) < 0)
637 if (!(*binary = av_malloc(*size))) {
638 av_log(matroska->ctx, AV_LOG_ERROR,
639 "Memory allocation error\n");
640 return AVERROR(ENOMEM);
643 if (get_buffer(pb, *binary, *size) != *size) {
644 offset_t pos = url_ftell(pb);
645 av_log(matroska->ctx, AV_LOG_ERROR,
646 "Read error at pos. %"PRIu64" (0x%"PRIx64")\n", pos, pos);
654 * Read signed/unsigned "EBML" numbers.
655 * Return: number of bytes processed, < 0 on error.
656 * XXX: use ebml_read_num().
660 matroska_ebmlnum_uint (uint8_t *data,
664 int len_mask = 0x80, read = 1, n = 1, num_ffs = 0;
668 return AVERROR_INVALIDDATA;
671 while (read <= 8 && !(total & len_mask)) {
676 return AVERROR_INVALIDDATA;
678 if ((total &= (len_mask - 1)) == len_mask - 1)
681 return AVERROR_INVALIDDATA;
685 total = (total << 8) | data[n];
698 * Same as above, but signed.
702 matroska_ebmlnum_sint (uint8_t *data,
709 /* read as unsigned number first */
710 if ((res = matroska_ebmlnum_uint(data, size, &unum)) < 0)
713 /* make signed (weird way) */
714 if (unum == (uint64_t)-1)
717 *num = unum - ((1LL << ((7 * res) - 1)) - 1);
724 matroska_find_track_by_num (MatroskaDemuxContext *matroska,
729 for (i = 0; i < matroska->num_tracks; i++)
730 if (matroska->tracks[i]->num == num)
738 * Put one packet in an application-supplied AVPacket struct.
739 * Returns 0 on success or -1 on failure.
743 matroska_deliver_packet (MatroskaDemuxContext *matroska,
746 if (matroska->num_packets > 0) {
747 memcpy(pkt, matroska->packets[0], sizeof(AVPacket));
748 av_free(matroska->packets[0]);
749 if (matroska->num_packets > 1) {
750 memmove(&matroska->packets[0], &matroska->packets[1],
751 (matroska->num_packets - 1) * sizeof(AVPacket *));
753 av_realloc(matroska->packets, (matroska->num_packets - 1) *
756 av_freep(&matroska->packets);
758 matroska->num_packets--;
766 * Put a packet into our internal queue. Will be delivered to the
767 * user/application during the next get_packet() call.
771 matroska_queue_packet (MatroskaDemuxContext *matroska,
775 av_realloc(matroska->packets, (matroska->num_packets + 1) *
777 matroska->packets[matroska->num_packets] = pkt;
778 matroska->num_packets++;
782 * Free all packets in our internal queue.
785 matroska_clear_queue (MatroskaDemuxContext *matroska)
787 if (matroska->packets) {
789 for (n = 0; n < matroska->num_packets; n++) {
790 av_free_packet(matroska->packets[n]);
791 av_free(matroska->packets[n]);
793 av_free(matroska->packets);
794 matroska->packets = NULL;
795 matroska->num_packets = 0;
805 matroska_probe (AVProbeData *p)
808 int len_mask = 0x80, size = 1, n = 1;
809 uint8_t probe_data[] = { 'm', 'a', 't', 'r', 'o', 's', 'k', 'a' };
812 if (AV_RB32(p->buf) != EBML_ID_HEADER)
815 /* length of header */
817 while (size <= 8 && !(total & len_mask)) {
823 total &= (len_mask - 1);
825 total = (total << 8) | p->buf[4 + n++];
827 /* does the probe data contain the whole header? */
828 if (p->buf_size < 4 + size + total)
831 /* the header must contain the document type 'matroska'. For now,
832 * we don't parse the whole header but simply check for the
833 * availability of that array of characters inside the header.
834 * Not fully fool-proof, but good enough. */
835 for (n = 4 + size; n <= 4 + size + total - sizeof(probe_data); n++)
836 if (!memcmp (&p->buf[n], probe_data, sizeof(probe_data)))
837 return AVPROBE_SCORE_MAX;
843 * From here on, it's all XML-style DTD stuff... Needs no comments.
846 static int ebml_parse(MatroskaDemuxContext *matroska, EbmlSyntax *syntax,
847 void *data, uint32_t expected_id, int once);
849 static int ebml_parse_elem(MatroskaDemuxContext *matroska,
850 EbmlSyntax *syntax, void *data)
852 uint32_t id = syntax->id;
856 data = (char *)data + syntax->data_offset;
857 if (syntax->list_elem_size) {
858 EbmlList *list = data;
859 list->elem = av_realloc(list->elem, (list->nb_elem+1)*syntax->list_elem_size);
860 data = (char*)list->elem + list->nb_elem*syntax->list_elem_size;
861 memset(data, 0, syntax->list_elem_size);
866 switch (syntax->type) {
867 case EBML_UINT: return ebml_read_uint (matroska, &id, data);
868 case EBML_FLOAT: return ebml_read_float(matroska, &id, data);
870 case EBML_UTF8: av_free(*(char **)data);
871 return ebml_read_ascii(matroska, &id, data);
872 case EBML_BIN: av_free(bin->data);
873 bin->pos = url_ftell(matroska->ctx->pb);
874 return ebml_read_binary(matroska, &id, &bin->data,
876 case EBML_NEST: if ((res=ebml_read_master(matroska, &id)) < 0)
878 if (id == MATROSKA_ID_SEGMENT)
879 matroska->segment_start = url_ftell(matroska->ctx->pb);
880 return ebml_parse(matroska, syntax->def.n, data, 0, 0);
881 case EBML_PASS: return ebml_parse(matroska, syntax->def.n, data, 0, 1);
882 case EBML_STOP: *(int *)data = 1; return 1;
883 default: return ebml_read_skip(matroska);
887 static int ebml_parse_id(MatroskaDemuxContext *matroska, EbmlSyntax *syntax,
888 uint32_t id, void *data)
891 for (i=0; syntax[i].id; i++)
892 if (id == syntax[i].id)
895 av_log(matroska->ctx, AV_LOG_INFO, "Unknown entry 0x%X\n", id);
896 return ebml_parse_elem(matroska, &syntax[i], data);
899 static int ebml_parse(MatroskaDemuxContext *matroska, EbmlSyntax *syntax,
900 void *data, uint32_t expected_id, int once)
905 for (i=0; syntax[i].id; i++)
906 switch (syntax[i].type) {
908 *(uint64_t *)((char *)data+syntax[i].data_offset) = syntax[i].def.u;
911 *(double *)((char *)data+syntax[i].data_offset) = syntax[i].def.f;
915 *(char **)((char *)data+syntax[i].data_offset) = av_strdup(syntax[i].def.s);
920 res = ebml_read_master(matroska, &id);
921 if (id != expected_id)
922 return AVERROR_INVALIDDATA;
923 if (id == MATROSKA_ID_SEGMENT)
924 matroska->segment_start = url_ftell(matroska->ctx->pb);
928 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
931 } else if (matroska->level_up) {
932 matroska->level_up--;
936 res = ebml_parse_id(matroska, syntax, id, data);
941 if (matroska->level_up) {
942 matroska->level_up--;
950 static void ebml_free(EbmlSyntax *syntax, void *data)
953 for (i=0; syntax[i].id; i++) {
954 void *data_off = (char *)data + syntax[i].data_offset;
955 switch (syntax[i].type) {
957 case EBML_UTF8: av_freep(data_off); break;
958 case EBML_BIN: av_freep(&((EbmlBin *)data_off)->data); break;
960 if (syntax[i].list_elem_size) {
961 EbmlList *list = data_off;
962 char *ptr = list->elem;
963 for (j=0; j<list->nb_elem; j++, ptr+=syntax[i].list_elem_size)
964 ebml_free(syntax[i].def.n, ptr);
967 ebml_free(syntax[i].def.n, data_off);
974 matroska_parse_info (MatroskaDemuxContext *matroska)
979 av_log(matroska->ctx, AV_LOG_DEBUG, "Parsing info...\n");
982 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
985 } else if (matroska->level_up) {
986 matroska->level_up--;
991 /* cluster timecode */
992 case MATROSKA_ID_TIMECODESCALE: {
994 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
996 matroska->time_scale = num;
1000 case MATROSKA_ID_DURATION: {
1002 if ((res = ebml_read_float(matroska, &id, &num)) < 0)
1004 matroska->ctx->duration = num * matroska->time_scale * 1000 / AV_TIME_BASE;
1008 case MATROSKA_ID_TITLE: {
1010 if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
1012 strncpy(matroska->ctx->title, text,
1013 sizeof(matroska->ctx->title)-1);
1019 av_log(matroska->ctx, AV_LOG_INFO,
1020 "Unknown entry 0x%x in info header\n", id);
1023 case MATROSKA_ID_WRITINGAPP:
1024 case MATROSKA_ID_MUXINGAPP:
1025 case MATROSKA_ID_DATEUTC:
1026 case MATROSKA_ID_SEGMENTUID:
1028 res = ebml_read_skip(matroska);
1032 if (matroska->level_up) {
1033 matroska->level_up--;
1042 matroska_decode_buffer(uint8_t** buf, int* buf_size, MatroskaTrack *track)
1044 uint8_t* data = *buf;
1045 int isize = *buf_size;
1046 uint8_t* pkt_data = NULL;
1047 int pkt_size = isize;
1051 switch (track->encoding_algo) {
1052 case MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP:
1053 return track->encoding_settings_len;
1054 case MATROSKA_TRACK_ENCODING_COMP_LZO:
1056 olen = pkt_size *= 3;
1057 pkt_data = av_realloc(pkt_data,
1058 pkt_size+LZO_OUTPUT_PADDING);
1059 result = lzo1x_decode(pkt_data, &olen, data, &isize);
1060 } while (result==LZO_OUTPUT_FULL && pkt_size<10000000);
1066 case MATROSKA_TRACK_ENCODING_COMP_ZLIB: {
1067 z_stream zstream = {0};
1068 if (inflateInit(&zstream) != Z_OK)
1070 zstream.next_in = data;
1071 zstream.avail_in = isize;
1074 pkt_data = av_realloc(pkt_data, pkt_size);
1075 zstream.avail_out = pkt_size - zstream.total_out;
1076 zstream.next_out = pkt_data + zstream.total_out;
1077 result = inflate(&zstream, Z_NO_FLUSH);
1078 } while (result==Z_OK && pkt_size<10000000);
1079 pkt_size = zstream.total_out;
1080 inflateEnd(&zstream);
1081 if (result != Z_STREAM_END)
1087 case MATROSKA_TRACK_ENCODING_COMP_BZLIB: {
1088 bz_stream bzstream = {0};
1089 if (BZ2_bzDecompressInit(&bzstream, 0, 0) != BZ_OK)
1091 bzstream.next_in = data;
1092 bzstream.avail_in = isize;
1095 pkt_data = av_realloc(pkt_data, pkt_size);
1096 bzstream.avail_out = pkt_size - bzstream.total_out_lo32;
1097 bzstream.next_out = pkt_data + bzstream.total_out_lo32;
1098 result = BZ2_bzDecompress(&bzstream);
1099 } while (result==BZ_OK && pkt_size<10000000);
1100 pkt_size = bzstream.total_out_lo32;
1101 BZ2_bzDecompressEnd(&bzstream);
1102 if (result != BZ_STREAM_END)
1110 *buf_size = pkt_size;
1118 matroska_add_stream (MatroskaDemuxContext *matroska)
1122 MatroskaTrack *track;
1124 /* start with the master */
1125 if ((res = ebml_read_master(matroska, &id)) < 0)
1128 av_log(matroska->ctx, AV_LOG_DEBUG, "parsing track, adding stream..,\n");
1130 /* Allocate a generic track. */
1131 track = av_mallocz(MAX_TRACK_SIZE);
1132 track->time_scale = 1.0;
1133 strcpy(track->language, "eng");
1135 /* try reading the trackentry headers */
1137 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1140 } else if (matroska->level_up > 0) {
1141 matroska->level_up--;
1146 /* track number (unique stream ID) */
1147 case MATROSKA_ID_TRACKNUMBER: {
1149 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1155 /* track UID (unique identifier) */
1156 case MATROSKA_ID_TRACKUID: {
1158 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1164 /* track type (video, audio, combined, subtitle, etc.) */
1165 case MATROSKA_ID_TRACKTYPE: {
1167 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1169 if (track->type && track->type != num) {
1170 av_log(matroska->ctx, AV_LOG_INFO,
1171 "More than one tracktype in an entry - skip\n");
1176 switch (track->type) {
1177 case MATROSKA_TRACK_TYPE_VIDEO:
1178 case MATROSKA_TRACK_TYPE_AUDIO:
1179 case MATROSKA_TRACK_TYPE_SUBTITLE:
1181 case MATROSKA_TRACK_TYPE_COMPLEX:
1182 case MATROSKA_TRACK_TYPE_LOGO:
1183 case MATROSKA_TRACK_TYPE_CONTROL:
1185 av_log(matroska->ctx, AV_LOG_INFO,
1186 "Unknown or unsupported track type 0x%x\n",
1188 track->type = MATROSKA_TRACK_TYPE_NONE;
1194 /* tracktype specific stuff for video */
1195 case MATROSKA_ID_TRACKVIDEO: {
1196 MatroskaVideoTrack *videotrack;
1198 track->type = MATROSKA_TRACK_TYPE_VIDEO;
1199 if (track->type != MATROSKA_TRACK_TYPE_VIDEO) {
1200 av_log(matroska->ctx, AV_LOG_INFO,
1201 "video data in non-video track - ignoring\n");
1202 res = AVERROR_INVALIDDATA;
1204 } else if ((res = ebml_read_master(matroska, &id)) < 0)
1206 videotrack = (MatroskaVideoTrack *)track;
1209 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1212 } else if (matroska->level_up > 0) {
1213 matroska->level_up--;
1218 /* fixme, this should be one-up, but I get it here */
1219 case MATROSKA_ID_TRACKDEFAULTDURATION: {
1221 if ((res = ebml_read_uint (matroska, &id,
1224 track->default_duration = num;
1228 /* video framerate */
1229 case MATROSKA_ID_VIDEOFRAMERATE: {
1231 if ((res = ebml_read_float(matroska, &id,
1234 if (!track->default_duration)
1235 track->default_duration = 1000000000/num;
1239 /* width of the size to display the video at */
1240 case MATROSKA_ID_VIDEODISPLAYWIDTH: {
1242 if ((res = ebml_read_uint(matroska, &id,
1245 videotrack->display_width = num;
1249 /* height of the size to display the video at */
1250 case MATROSKA_ID_VIDEODISPLAYHEIGHT: {
1252 if ((res = ebml_read_uint(matroska, &id,
1255 videotrack->display_height = num;
1259 /* width of the video in the file */
1260 case MATROSKA_ID_VIDEOPIXELWIDTH: {
1262 if ((res = ebml_read_uint(matroska, &id,
1265 videotrack->pixel_width = num;
1269 /* height of the video in the file */
1270 case MATROSKA_ID_VIDEOPIXELHEIGHT: {
1272 if ((res = ebml_read_uint(matroska, &id,
1275 videotrack->pixel_height = num;
1279 /* whether the video is interlaced */
1280 case MATROSKA_ID_VIDEOFLAGINTERLACED: {
1282 if ((res = ebml_read_uint(matroska, &id,
1288 /* colorspace (only matters for raw video)
1290 case MATROSKA_ID_VIDEOCOLORSPACE: {
1292 if ((res = ebml_read_uint(matroska, &id,
1295 videotrack->fourcc = num;
1300 av_log(matroska->ctx, AV_LOG_INFO,
1301 "Unknown video track header entry "
1302 "0x%x - ignoring\n", id);
1305 case MATROSKA_ID_VIDEOSTEREOMODE:
1306 case MATROSKA_ID_VIDEOASPECTRATIO:
1308 res = ebml_read_skip(matroska);
1312 if (matroska->level_up) {
1313 matroska->level_up--;
1320 /* tracktype specific stuff for audio */
1321 case MATROSKA_ID_TRACKAUDIO: {
1322 MatroskaAudioTrack *audiotrack;
1324 track->type = MATROSKA_TRACK_TYPE_AUDIO;
1325 if (track->type != MATROSKA_TRACK_TYPE_AUDIO) {
1326 av_log(matroska->ctx, AV_LOG_INFO,
1327 "audio data in non-audio track - ignoring\n");
1328 res = AVERROR_INVALIDDATA;
1330 } else if ((res = ebml_read_master(matroska, &id)) < 0)
1332 audiotrack = (MatroskaAudioTrack *)track;
1333 audiotrack->channels = 1;
1334 audiotrack->samplerate = 8000;
1337 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1340 } else if (matroska->level_up > 0) {
1341 matroska->level_up--;
1347 case MATROSKA_ID_AUDIOSAMPLINGFREQ: {
1349 if ((res = ebml_read_float(matroska, &id,
1352 audiotrack->internal_samplerate =
1353 audiotrack->samplerate = num;
1357 case MATROSKA_ID_AUDIOOUTSAMPLINGFREQ: {
1359 if ((res = ebml_read_float(matroska, &id,
1362 audiotrack->samplerate = num;
1367 case MATROSKA_ID_AUDIOBITDEPTH: {
1369 if ((res = ebml_read_uint(matroska, &id,
1372 audiotrack->bitdepth = num;
1377 case MATROSKA_ID_AUDIOCHANNELS: {
1379 if ((res = ebml_read_uint(matroska, &id,
1382 audiotrack->channels = num;
1387 av_log(matroska->ctx, AV_LOG_INFO,
1388 "Unknown audio track header entry "
1389 "0x%x - ignoring\n", id);
1393 res = ebml_read_skip(matroska);
1397 if (matroska->level_up) {
1398 matroska->level_up--;
1405 /* codec identifier */
1406 case MATROSKA_ID_CODECID: {
1408 if ((res = ebml_read_ascii(matroska, &id, &text)) < 0)
1410 track->codec_id = text;
1414 /* codec private data */
1415 case MATROSKA_ID_CODECPRIVATE: {
1418 if ((res = ebml_read_binary(matroska, &id, &data, &size) < 0))
1420 track->codec_priv = data;
1421 track->codec_priv_size = size;
1425 /* name of this track */
1426 case MATROSKA_ID_TRACKNAME: {
1428 if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
1434 /* language (matters for audio/subtitles, mostly) */
1435 case MATROSKA_ID_TRACKLANGUAGE: {
1437 if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
1439 if ((end = strchr(text, '-')))
1441 if (strlen(text) == 3)
1442 strcpy(track->language, text);
1447 /* whether this is actually used */
1448 case MATROSKA_ID_TRACKFLAGENABLED: {
1450 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1455 /* whether it's the default for this track type */
1456 case MATROSKA_ID_TRACKFLAGDEFAULT: {
1458 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1460 track->flag_default = num;
1464 /* lacing (like MPEG, where blocks don't end/start on frame
1466 case MATROSKA_ID_TRACKFLAGLACING: {
1468 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1473 /* default length (in time) of one data block in this track */
1474 case MATROSKA_ID_TRACKDEFAULTDURATION: {
1476 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1478 track->default_duration = num;
1482 case MATROSKA_ID_TRACKCONTENTENCODINGS: {
1483 if ((res = ebml_read_master(matroska, &id)) < 0)
1487 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1490 } else if (matroska->level_up > 0) {
1491 matroska->level_up--;
1496 case MATROSKA_ID_TRACKCONTENTENCODING: {
1497 int encoding_scope = 1;
1498 if ((res = ebml_read_master(matroska, &id)) < 0)
1502 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1505 } else if (matroska->level_up > 0) {
1506 matroska->level_up--;
1511 case MATROSKA_ID_ENCODINGSCOPE: {
1513 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1515 encoding_scope = num;
1519 case MATROSKA_ID_ENCODINGTYPE: {
1521 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1524 av_log(matroska->ctx, AV_LOG_ERROR,
1525 "Unsupported encoding type");
1529 case MATROSKA_ID_ENCODINGCOMPRESSION: {
1530 if ((res = ebml_read_master(matroska, &id)) < 0)
1534 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1537 } else if (matroska->level_up > 0) {
1538 matroska->level_up--;
1543 case MATROSKA_ID_ENCODINGCOMPALGO: {
1545 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1547 if (num != MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP &&
1549 num != MATROSKA_TRACK_ENCODING_COMP_ZLIB &&
1552 num != MATROSKA_TRACK_ENCODING_COMP_BZLIB &&
1554 num != MATROSKA_TRACK_ENCODING_COMP_LZO)
1555 av_log(matroska->ctx, AV_LOG_ERROR,
1556 "Unsupported compression algo\n");
1557 track->encoding_algo = num;
1561 case MATROSKA_ID_ENCODINGCOMPSETTINGS: {
1564 if ((res = ebml_read_binary(matroska, &id, &data, &size) < 0))
1566 track->encoding_settings = data;
1567 track->encoding_settings_len = size;
1572 av_log(matroska->ctx, AV_LOG_INFO,
1573 "Unknown compression header entry "
1574 "0x%x - ignoring\n", id);
1578 res = ebml_read_skip(matroska);
1582 if (matroska->level_up) {
1583 matroska->level_up--;
1591 av_log(matroska->ctx, AV_LOG_INFO,
1592 "Unknown content encoding header entry "
1593 "0x%x - ignoring\n", id);
1597 res = ebml_read_skip(matroska);
1601 if (matroska->level_up) {
1602 matroska->level_up--;
1607 track->encoding_scope = encoding_scope;
1612 av_log(matroska->ctx, AV_LOG_INFO,
1613 "Unknown content encodings header entry "
1614 "0x%x - ignoring\n", id);
1618 res = ebml_read_skip(matroska);
1622 if (matroska->level_up) {
1623 matroska->level_up--;
1630 case MATROSKA_ID_TRACKTIMECODESCALE: {
1632 if ((res = ebml_read_float(matroska, &id, &num)) < 0)
1634 track->time_scale = num;
1639 av_log(matroska->ctx, AV_LOG_INFO,
1640 "Unknown track header entry 0x%x - ignoring\n", id);
1644 /* we ignore these because they're nothing useful. */
1645 case MATROSKA_ID_TRACKFLAGFORCED:
1646 case MATROSKA_ID_CODECNAME:
1647 case MATROSKA_ID_CODECDECODEALL:
1648 case MATROSKA_ID_CODECINFOURL:
1649 case MATROSKA_ID_CODECDOWNLOADURL:
1650 case MATROSKA_ID_TRACKMINCACHE:
1651 case MATROSKA_ID_TRACKMAXCACHE:
1652 res = ebml_read_skip(matroska);
1656 if (matroska->level_up) {
1657 matroska->level_up--;
1662 if (track->codec_priv_size && track->encoding_scope & 2) {
1663 uint8_t *orig_priv = track->codec_priv;
1664 int offset = matroska_decode_buffer(&track->codec_priv,
1665 &track->codec_priv_size, track);
1667 track->codec_priv = av_malloc(track->codec_priv_size + offset);
1668 memcpy(track->codec_priv, track->encoding_settings, offset);
1669 memcpy(track->codec_priv+offset, orig_priv, track->codec_priv_size);
1670 track->codec_priv_size += offset;
1672 } else if (!offset) {
1675 av_log(matroska->ctx, AV_LOG_ERROR,
1676 "Failed to decode codec private data\n");
1679 if (track->type && matroska->num_tracks < ARRAY_SIZE(matroska->tracks)) {
1680 matroska->tracks[matroska->num_tracks++] = track;
1688 matroska_parse_tracks (MatroskaDemuxContext *matroska)
1693 av_log(matroska->ctx, AV_LOG_DEBUG, "parsing tracks...\n");
1696 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1699 } else if (matroska->level_up) {
1700 matroska->level_up--;
1705 /* one track within the "all-tracks" header */
1706 case MATROSKA_ID_TRACKENTRY:
1707 res = matroska_add_stream(matroska);
1711 av_log(matroska->ctx, AV_LOG_INFO,
1712 "Unknown entry 0x%x in track header\n", id);
1716 res = ebml_read_skip(matroska);
1720 if (matroska->level_up) {
1721 matroska->level_up--;
1730 matroska_parse_index (MatroskaDemuxContext *matroska)
1734 MatroskaDemuxIndex idx;
1736 av_log(matroska->ctx, AV_LOG_DEBUG, "parsing index...\n");
1739 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1742 } else if (matroska->level_up) {
1743 matroska->level_up--;
1748 /* one single index entry ('point') */
1749 case MATROSKA_ID_POINTENTRY:
1750 if ((res = ebml_read_master(matroska, &id)) < 0)
1753 /* in the end, we hope to fill one entry with a
1754 * timestamp, a file position and a tracknum */
1755 idx.pos = (uint64_t) -1;
1756 idx.time = (uint64_t) -1;
1757 idx.track = (uint16_t) -1;
1760 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1763 } else if (matroska->level_up) {
1764 matroska->level_up--;
1769 /* one single index entry ('point') */
1770 case MATROSKA_ID_CUETIME: {
1772 if ((res = ebml_read_uint(matroska, &id,
1775 idx.time = time * matroska->time_scale;
1779 /* position in the file + track to which it
1781 case MATROSKA_ID_CUETRACKPOSITION:
1782 if ((res = ebml_read_master(matroska, &id)) < 0)
1786 if (!(id = ebml_peek_id (matroska,
1787 &matroska->level_up))) {
1790 } else if (matroska->level_up) {
1791 matroska->level_up--;
1797 case MATROSKA_ID_CUETRACK: {
1799 if ((res = ebml_read_uint(matroska,
1806 /* position in file */
1807 case MATROSKA_ID_CUECLUSTERPOSITION: {
1809 if ((res = ebml_read_uint(matroska,
1812 idx.pos = num+matroska->segment_start;
1817 av_log(matroska->ctx, AV_LOG_INFO,
1818 "Unknown entry 0x%x in "
1819 "CuesTrackPositions\n", id);
1823 res = ebml_read_skip(matroska);
1827 if (matroska->level_up) {
1828 matroska->level_up--;
1836 av_log(matroska->ctx, AV_LOG_INFO,
1837 "Unknown entry 0x%x in cuespoint "
1842 res = ebml_read_skip(matroska);
1846 if (matroska->level_up) {
1847 matroska->level_up--;
1852 /* so let's see if we got what we wanted */
1853 if (idx.pos != (uint64_t) -1 &&
1854 idx.time != (uint64_t) -1 &&
1855 idx.track != (uint16_t) -1) {
1856 if (matroska->num_indexes % 32 == 0) {
1857 /* re-allocate bigger index */
1859 av_realloc(matroska->index,
1860 (matroska->num_indexes + 32) *
1861 sizeof(MatroskaDemuxIndex));
1863 matroska->index[matroska->num_indexes] = idx;
1864 matroska->num_indexes++;
1869 av_log(matroska->ctx, AV_LOG_INFO,
1870 "Unknown entry 0x%x in cues header\n", id);
1874 res = ebml_read_skip(matroska);
1878 if (matroska->level_up) {
1879 matroska->level_up--;
1888 matroska_parse_metadata (MatroskaDemuxContext *matroska)
1894 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1897 } else if (matroska->level_up) {
1898 matroska->level_up--;
1903 /* Hm, this is unsupported... */
1905 av_log(matroska->ctx, AV_LOG_INFO,
1906 "Unknown entry 0x%x in metadata header\n", id);
1910 res = ebml_read_skip(matroska);
1914 if (matroska->level_up) {
1915 matroska->level_up--;
1924 matroska_parse_seekhead (MatroskaDemuxContext *matroska)
1929 av_log(matroska->ctx, AV_LOG_DEBUG, "parsing seekhead...\n");
1932 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1935 } else if (matroska->level_up) {
1936 matroska->level_up--;
1941 case MATROSKA_ID_SEEKENTRY: {
1942 uint32_t seek_id = 0, peek_id_cache = 0;
1943 uint64_t seek_pos = (uint64_t) -1, t;
1944 int dummy_level = 0;
1946 if ((res = ebml_read_master(matroska, &id)) < 0)
1950 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1953 } else if (matroska->level_up) {
1954 matroska->level_up--;
1959 case MATROSKA_ID_SEEKID:
1960 res = ebml_read_uint(matroska, &id, &t);
1964 case MATROSKA_ID_SEEKPOSITION:
1965 res = ebml_read_uint(matroska, &id, &seek_pos);
1969 av_log(matroska->ctx, AV_LOG_INFO,
1970 "Unknown seekhead ID 0x%x\n", id);
1974 res = ebml_read_skip(matroska);
1978 if (matroska->level_up) {
1979 matroska->level_up--;
1984 if (!seek_id || seek_pos == (uint64_t) -1) {
1985 av_log(matroska->ctx, AV_LOG_INFO,
1986 "Incomplete seekhead entry (0x%x/%"PRIu64")\n",
1992 case MATROSKA_ID_CUES:
1993 case MATROSKA_ID_TAGS: {
1994 uint32_t level_up = matroska->level_up;
1995 offset_t before_pos;
1997 MatroskaLevel level;
1999 /* remember the peeked ID and the current position */
2000 peek_id_cache = matroska->peek_id;
2001 before_pos = url_ftell(matroska->ctx->pb);
2004 if ((res = ebml_read_seek(matroska, seek_pos +
2005 matroska->segment_start)) < 0)
2008 /* we don't want to lose our seekhead level, so we add
2009 * a dummy. This is a crude hack. */
2010 if (matroska->num_levels == EBML_MAX_DEPTH) {
2011 av_log(matroska->ctx, AV_LOG_INFO,
2012 "Max EBML element depth (%d) reached, "
2013 "cannot parse further.\n", EBML_MAX_DEPTH);
2014 return AVERROR_UNKNOWN;
2018 level.length = (uint64_t)-1;
2019 matroska->levels[matroska->num_levels] = level;
2020 matroska->num_levels++;
2024 if (!(id = ebml_peek_id (matroska,
2025 &matroska->level_up)))
2027 if (id != seek_id) {
2028 av_log(matroska->ctx, AV_LOG_INFO,
2029 "We looked for ID=0x%x but got "
2030 "ID=0x%x (pos=%"PRIu64")",
2031 seek_id, id, seek_pos +
2032 matroska->segment_start);
2036 /* read master + parse */
2037 if ((res = ebml_read_master(matroska, &id)) < 0)
2040 case MATROSKA_ID_CUES:
2041 if (!(res = matroska_parse_index(matroska)) ||
2042 url_feof(matroska->ctx->pb)) {
2043 matroska->index_parsed = 1;
2047 case MATROSKA_ID_TAGS:
2048 if (!(res = matroska_parse_metadata(matroska)) ||
2049 url_feof(matroska->ctx->pb)) {
2050 matroska->metadata_parsed = 1;
2057 /* remove dummy level */
2059 while (matroska->num_levels) {
2060 matroska->num_levels--;
2062 matroska->levels[matroska->num_levels].length;
2063 if (length == (uint64_t)-1)
2068 if ((res = ebml_read_seek(matroska, before_pos)) < 0)
2070 matroska->peek_id = peek_id_cache;
2071 matroska->level_up = level_up;
2076 av_log(matroska->ctx, AV_LOG_INFO,
2077 "Ignoring seekhead entry for ID=0x%x\n",
2086 av_log(matroska->ctx, AV_LOG_INFO,
2087 "Unknown seekhead ID 0x%x\n", id);
2091 res = ebml_read_skip(matroska);
2095 if (matroska->level_up) {
2096 matroska->level_up--;
2105 matroska_parse_attachments(AVFormatContext *s)
2107 MatroskaDemuxContext *matroska = s->priv_data;
2111 av_log(matroska->ctx, AV_LOG_DEBUG, "parsing attachments...\n");
2114 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2117 } else if (matroska->level_up) {
2118 matroska->level_up--;
2123 case MATROSKA_ID_ATTACHEDFILE: {
2126 uint8_t* data = NULL;
2127 int i, data_size = 0;
2130 if ((res = ebml_read_master(matroska, &id)) < 0)
2134 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2137 } else if (matroska->level_up) {
2138 matroska->level_up--;
2143 case MATROSKA_ID_FILENAME:
2144 res = ebml_read_utf8 (matroska, &id, &name);
2147 case MATROSKA_ID_FILEMIMETYPE:
2148 res = ebml_read_ascii (matroska, &id, &mime);
2151 case MATROSKA_ID_FILEDATA:
2152 res = ebml_read_binary(matroska, &id, &data, &data_size);
2156 av_log(matroska->ctx, AV_LOG_INFO,
2157 "Unknown attachedfile ID 0x%x\n", id);
2158 case MATROSKA_ID_FILEUID:
2160 res = ebml_read_skip(matroska);
2164 if (matroska->level_up) {
2165 matroska->level_up--;
2170 if (!(name && mime && data && data_size > 0)) {
2171 av_log(matroska->ctx, AV_LOG_ERROR, "incomplete attachment\n");
2175 st = av_new_stream(s, matroska->num_streams++);
2177 return AVERROR(ENOMEM);
2178 st->filename = av_strdup(name);
2179 st->codec->codec_id = CODEC_ID_NONE;
2180 st->codec->codec_type = CODEC_TYPE_ATTACHMENT;
2181 st->codec->extradata = av_malloc(data_size);
2182 if(st->codec->extradata == NULL)
2183 return AVERROR(ENOMEM);
2184 st->codec->extradata_size = data_size;
2185 memcpy(st->codec->extradata, data, data_size);
2187 for (i=0; ff_mkv_mime_tags[i].id != CODEC_ID_NONE; i++) {
2188 if (!strncmp(ff_mkv_mime_tags[i].str, mime,
2189 strlen(ff_mkv_mime_tags[i].str))) {
2190 st->codec->codec_id = ff_mkv_mime_tags[i].id;
2195 av_log(matroska->ctx, AV_LOG_DEBUG, "new attachment: %s, %s, size %d \n", name, mime, data_size);
2200 av_log(matroska->ctx, AV_LOG_INFO,
2201 "Unknown attachments ID 0x%x\n", id);
2205 res = ebml_read_skip(matroska);
2209 if (matroska->level_up) {
2210 matroska->level_up--;
2219 matroska_parse_chapters(AVFormatContext *s)
2221 MatroskaDemuxContext *matroska = s->priv_data;
2225 av_log(s, AV_LOG_DEBUG, "parsing chapters...\n");
2228 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2231 } else if (matroska->level_up) {
2232 matroska->level_up--;
2237 case MATROSKA_ID_EDITIONENTRY: {
2238 uint64_t end = AV_NOPTS_VALUE, start = AV_NOPTS_VALUE;
2241 /* if there is more than one chapter edition
2242 we take only the first one */
2244 ebml_read_skip(matroska);
2248 if ((res = ebml_read_master(matroska, &id)) < 0)
2252 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2255 } else if (matroska->level_up) {
2256 matroska->level_up--;
2261 case MATROSKA_ID_CHAPTERATOM:
2262 if ((res = ebml_read_master(matroska, &id)) < 0)
2266 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2269 } else if (matroska->level_up) {
2270 matroska->level_up--;
2275 case MATROSKA_ID_CHAPTERTIMEEND:
2276 res = ebml_read_uint(matroska, &id, &end);
2279 case MATROSKA_ID_CHAPTERTIMESTART:
2280 res = ebml_read_uint(matroska, &id, &start);
2283 case MATROSKA_ID_CHAPTERDISPLAY:
2284 if ((res = ebml_read_master(matroska, &id)) < 0)
2288 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2291 } else if (matroska->level_up) {
2292 matroska->level_up--;
2297 case MATROSKA_ID_CHAPSTRING:
2298 res = ebml_read_utf8(matroska, &id, &title);
2302 av_log(s, AV_LOG_INFO, "Ignoring unknown Chapter display ID 0x%x\n", id);
2304 res = ebml_read_skip(matroska);
2308 if (matroska->level_up) {
2309 matroska->level_up--;
2315 case MATROSKA_ID_CHAPTERUID:
2316 res = ebml_read_uint(matroska, &id, &uid);
2319 av_log(s, AV_LOG_INFO, "Ignoring unknown Chapter atom ID 0x%x\n", id);
2320 case MATROSKA_ID_CHAPTERFLAGHIDDEN:
2322 res = ebml_read_skip(matroska);
2326 if (matroska->level_up) {
2327 matroska->level_up--;
2332 if (start != AV_NOPTS_VALUE && uid != -1) {
2333 if(!ff_new_chapter(s, uid, (AVRational){1, 1000000000}, start, end, title))
2334 res= AVERROR(ENOMEM);
2340 av_log(s, AV_LOG_INFO, "Ignoring unknown Edition entry ID 0x%x\n", id);
2341 case MATROSKA_ID_EDITIONUID:
2342 case MATROSKA_ID_EDITIONFLAGHIDDEN:
2343 case MATROSKA_ID_EDITIONFLAGDEFAULT:
2345 res = ebml_read_skip(matroska);
2350 if (matroska->level_up) {
2351 matroska->level_up--;
2359 av_log(s, AV_LOG_INFO, "Expected an Edition entry (0x%x), but found 0x%x\n", MATROSKA_ID_EDITIONENTRY, id);
2361 res = ebml_read_skip(matroska);
2365 if (matroska->level_up) {
2366 matroska->level_up--;
2375 matroska_aac_profile (char *codec_id)
2377 static const char *aac_profiles[] = {
2382 for (profile=0; profile<ARRAY_SIZE(aac_profiles); profile++)
2383 if (strstr(codec_id, aac_profiles[profile]))
2389 matroska_aac_sri (int samplerate)
2393 for (sri=0; sri<ARRAY_SIZE(ff_mpeg4audio_sample_rates); sri++)
2394 if (ff_mpeg4audio_sample_rates[sri] == samplerate)
2400 matroska_read_header (AVFormatContext *s,
2401 AVFormatParameters *ap)
2403 MatroskaDemuxContext *matroska = s->priv_data;
2404 int last_level, res = 0;
2410 /* First read the EBML header. */
2411 if (ebml_parse(matroska, ebml_syntax, &ebml, 0, 1)
2412 || ebml.version > EBML_VERSION || ebml.max_size > sizeof(uint64_t)
2413 || ebml.id_length > sizeof(uint32_t) || strcmp(ebml.doctype, "matroska")
2414 || ebml.doctype_version > 2) {
2415 av_log(matroska->ctx, AV_LOG_ERROR,
2416 "EBML header using unsupported features\n"
2417 "(EBML version %"PRIu64", doctype %s, doc version %"PRIu64")\n",
2418 ebml.version, ebml.doctype, ebml.doctype_version);
2419 return AVERROR_NOFMT;
2421 ebml_free(ebml_syntax, &ebml);
2423 /* The next thing is a segment. */
2425 if (!(id = ebml_peek_id(matroska, &last_level)))
2426 return AVERROR(EIO);
2427 if (id == MATROSKA_ID_SEGMENT)
2431 av_log(matroska->ctx, AV_LOG_INFO,
2432 "Expected a Segment ID (0x%x), but received 0x%x!\n",
2433 MATROSKA_ID_SEGMENT, id);
2434 if ((res = ebml_read_skip(matroska)) < 0)
2438 /* We now have a Matroska segment.
2439 * Seeks are from the beginning of the segment,
2440 * after the segment ID/length. */
2441 if ((res = ebml_read_master(matroska, &id)) < 0)
2443 matroska->segment_start = url_ftell(s->pb);
2445 matroska->time_scale = 1000000;
2446 /* we've found our segment, start reading the different contents in here */
2448 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2451 } else if (matroska->level_up) {
2452 matroska->level_up--;
2458 case MATROSKA_ID_INFO: {
2459 if ((res = ebml_read_master(matroska, &id)) < 0)
2461 res = matroska_parse_info(matroska);
2465 /* track info headers */
2466 case MATROSKA_ID_TRACKS: {
2467 if ((res = ebml_read_master(matroska, &id)) < 0)
2469 res = matroska_parse_tracks(matroska);
2474 case MATROSKA_ID_CUES: {
2475 if (!matroska->index_parsed) {
2476 if ((res = ebml_read_master(matroska, &id)) < 0)
2478 res = matroska_parse_index(matroska);
2480 res = ebml_read_skip(matroska);
2485 case MATROSKA_ID_TAGS: {
2486 if (!matroska->metadata_parsed) {
2487 if ((res = ebml_read_master(matroska, &id)) < 0)
2489 res = matroska_parse_metadata(matroska);
2491 res = ebml_read_skip(matroska);
2495 /* file index (if seekable, seek to Cues/Tags to parse it) */
2496 case MATROSKA_ID_SEEKHEAD: {
2497 if ((res = ebml_read_master(matroska, &id)) < 0)
2499 res = matroska_parse_seekhead(matroska);
2503 case MATROSKA_ID_ATTACHMENTS: {
2504 if ((res = ebml_read_master(matroska, &id)) < 0)
2506 res = matroska_parse_attachments(s);
2510 case MATROSKA_ID_CLUSTER: {
2511 /* Do not read the master - this will be done in the next
2512 * call to matroska_read_packet. */
2517 case MATROSKA_ID_CHAPTERS: {
2518 if ((res = ebml_read_master(matroska, &id)) < 0)
2520 res = matroska_parse_chapters(s);
2525 av_log(matroska->ctx, AV_LOG_INFO,
2526 "Unknown matroska file header ID 0x%x\n", id);
2530 res = ebml_read_skip(matroska);
2534 if (matroska->level_up) {
2535 matroska->level_up--;
2540 /* Have we found a cluster? */
2541 if (ebml_peek_id(matroska, NULL) == MATROSKA_ID_CLUSTER) {
2543 MatroskaTrack *track;
2546 for (i = 0; i < matroska->num_tracks; i++) {
2547 enum CodecID codec_id = CODEC_ID_NONE;
2548 uint8_t *extradata = NULL;
2549 int extradata_size = 0;
2550 int extradata_offset = 0;
2551 track = matroska->tracks[i];
2552 track->stream_index = -1;
2554 /* Apply some sanity checks. */
2555 if (track->codec_id == NULL)
2558 for(j=0; ff_mkv_codec_tags[j].id != CODEC_ID_NONE; j++){
2559 if(!strncmp(ff_mkv_codec_tags[j].str, track->codec_id,
2560 strlen(ff_mkv_codec_tags[j].str))){
2561 codec_id= ff_mkv_codec_tags[j].id;
2566 /* Set the FourCC from the CodecID. */
2567 /* This is the MS compatibility mode which stores a
2568 * BITMAPINFOHEADER in the CodecPrivate. */
2569 if (!strcmp(track->codec_id,
2570 MATROSKA_CODEC_ID_VIDEO_VFW_FOURCC) &&
2571 (track->codec_priv_size >= 40) &&
2572 (track->codec_priv != NULL)) {
2573 MatroskaVideoTrack *vtrack = (MatroskaVideoTrack *) track;
2575 /* Offset of biCompression. Stored in LE. */
2576 vtrack->fourcc = AV_RL32(track->codec_priv + 16);
2577 codec_id = codec_get_id(codec_bmp_tags, vtrack->fourcc);
2581 /* This is the MS compatibility mode which stores a
2582 * WAVEFORMATEX in the CodecPrivate. */
2583 else if (!strcmp(track->codec_id,
2584 MATROSKA_CODEC_ID_AUDIO_ACM) &&
2585 (track->codec_priv_size >= 18) &&
2586 (track->codec_priv != NULL)) {
2589 /* Offset of wFormatTag. Stored in LE. */
2590 tag = AV_RL16(track->codec_priv);
2591 codec_id = codec_get_id(codec_wav_tags, tag);
2595 if (!strcmp(track->codec_id, "V_QUICKTIME") &&
2596 (track->codec_priv_size >= 86) &&
2597 (track->codec_priv != NULL)) {
2598 MatroskaVideoTrack *vtrack = (MatroskaVideoTrack *) track;
2600 vtrack->fourcc = AV_RL32(track->codec_priv);
2601 codec_id = codec_get_id(codec_movvideo_tags, vtrack->fourcc);
2604 else if (codec_id == CODEC_ID_AAC && !track->codec_priv_size) {
2605 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *) track;
2606 int profile = matroska_aac_profile(track->codec_id);
2607 int sri = matroska_aac_sri(audiotrack->internal_samplerate);
2608 extradata = av_malloc(5);
2609 if (extradata == NULL)
2610 return AVERROR(ENOMEM);
2611 extradata[0] = (profile << 3) | ((sri&0x0E) >> 1);
2612 extradata[1] = ((sri&0x01) << 7) | (audiotrack->channels<<3);
2613 if (strstr(track->codec_id, "SBR")) {
2614 sri = matroska_aac_sri(audiotrack->samplerate);
2615 extradata[2] = 0x56;
2616 extradata[3] = 0xE5;
2617 extradata[4] = 0x80 | (sri<<3);
2624 else if (codec_id == CODEC_ID_TTA) {
2625 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *) track;
2627 extradata_size = 30;
2628 extradata = av_mallocz(extradata_size);
2629 if (extradata == NULL)
2630 return AVERROR(ENOMEM);
2631 init_put_byte(&b, extradata, extradata_size, 1,
2632 NULL, NULL, NULL, NULL);
2633 put_buffer(&b, "TTA1", 4);
2635 put_le16(&b, audiotrack->channels);
2636 put_le16(&b, audiotrack->bitdepth);
2637 put_le32(&b, audiotrack->samplerate);
2638 put_le32(&b, matroska->ctx->duration * audiotrack->samplerate);
2641 else if (codec_id == CODEC_ID_RV10 || codec_id == CODEC_ID_RV20 ||
2642 codec_id == CODEC_ID_RV30 || codec_id == CODEC_ID_RV40) {
2643 extradata_offset = 26;
2644 track->codec_priv_size -= extradata_offset;
2647 else if (codec_id == CODEC_ID_RA_144) {
2648 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
2649 audiotrack->samplerate = 8000;
2650 audiotrack->channels = 1;
2653 else if (codec_id == CODEC_ID_RA_288 ||
2654 codec_id == CODEC_ID_COOK ||
2655 codec_id == CODEC_ID_ATRAC3) {
2656 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
2659 init_put_byte(&b, track->codec_priv, track->codec_priv_size, 0,
2660 NULL, NULL, NULL, NULL);
2662 audiotrack->coded_framesize = get_be32(&b);
2664 audiotrack->sub_packet_h = get_be16(&b);
2665 audiotrack->frame_size = get_be16(&b);
2666 audiotrack->sub_packet_size = get_be16(&b);
2667 audiotrack->buf = av_malloc(audiotrack->frame_size * audiotrack->sub_packet_h);
2668 if (codec_id == CODEC_ID_RA_288) {
2669 audiotrack->block_align = audiotrack->coded_framesize;
2670 track->codec_priv_size = 0;
2672 audiotrack->block_align = audiotrack->sub_packet_size;
2673 extradata_offset = 78;
2674 track->codec_priv_size -= extradata_offset;
2678 if (codec_id == CODEC_ID_NONE) {
2679 av_log(matroska->ctx, AV_LOG_INFO,
2680 "Unknown/unsupported CodecID %s.\n",
2684 track->stream_index = matroska->num_streams;
2686 matroska->num_streams++;
2687 st = av_new_stream(s, track->stream_index);
2689 return AVERROR(ENOMEM);
2690 av_set_pts_info(st, 64, matroska->time_scale*track->time_scale, 1000*1000*1000); /* 64 bit pts in ns */
2692 st->codec->codec_id = codec_id;
2694 if (strcmp(track->language, "und"))
2695 av_strlcpy(st->language, track->language, 4);
2697 if (track->flag_default)
2698 st->disposition |= AV_DISPOSITION_DEFAULT;
2700 if (track->default_duration)
2701 av_reduce(&st->codec->time_base.num, &st->codec->time_base.den,
2702 track->default_duration, 1000000000, 30000);
2705 st->codec->extradata = extradata;
2706 st->codec->extradata_size = extradata_size;
2707 } else if(track->codec_priv && track->codec_priv_size > 0){
2708 st->codec->extradata = av_malloc(track->codec_priv_size);
2709 if(st->codec->extradata == NULL)
2710 return AVERROR(ENOMEM);
2711 st->codec->extradata_size = track->codec_priv_size;
2712 memcpy(st->codec->extradata,track->codec_priv+extradata_offset,
2713 track->codec_priv_size);
2716 if (track->type == MATROSKA_TRACK_TYPE_VIDEO) {
2717 MatroskaVideoTrack *videotrack = (MatroskaVideoTrack *)track;
2719 st->codec->codec_type = CODEC_TYPE_VIDEO;
2720 st->codec->codec_tag = videotrack->fourcc;
2721 st->codec->width = videotrack->pixel_width;
2722 st->codec->height = videotrack->pixel_height;
2723 if (videotrack->display_width == 0)
2724 videotrack->display_width= videotrack->pixel_width;
2725 if (videotrack->display_height == 0)
2726 videotrack->display_height= videotrack->pixel_height;
2727 av_reduce(&st->codec->sample_aspect_ratio.num,
2728 &st->codec->sample_aspect_ratio.den,
2729 st->codec->height * videotrack->display_width,
2730 st->codec-> width * videotrack->display_height,
2732 st->need_parsing = AVSTREAM_PARSE_HEADERS;
2733 } else if (track->type == MATROSKA_TRACK_TYPE_AUDIO) {
2734 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
2736 st->codec->codec_type = CODEC_TYPE_AUDIO;
2737 st->codec->sample_rate = audiotrack->samplerate;
2738 st->codec->channels = audiotrack->channels;
2739 st->codec->block_align = audiotrack->block_align;
2740 } else if (track->type == MATROSKA_TRACK_TYPE_SUBTITLE) {
2741 st->codec->codec_type = CODEC_TYPE_SUBTITLE;
2744 /* What do we do with private data? E.g. for Vorbis. */
2749 if (matroska->index_parsed) {
2750 int i, track, stream;
2751 for (i=0; i<matroska->num_indexes; i++) {
2752 MatroskaDemuxIndex *idx = &matroska->index[i];
2753 track = matroska_find_track_by_num(matroska, idx->track);
2754 if (track < 0) continue;
2755 stream = matroska->tracks[track]->stream_index;
2756 if (stream >= 0 && stream < matroska->ctx->nb_streams)
2757 av_add_index_entry(matroska->ctx->streams[stream],
2758 idx->pos, idx->time/AV_TIME_BASE,
2759 0, 0, AVINDEX_KEYFRAME);
2767 matroska_parse_block(MatroskaDemuxContext *matroska, uint8_t *data, int size,
2768 int64_t pos, uint64_t cluster_time, uint64_t duration,
2775 uint8_t *origdata = data;
2777 uint32_t *lace_size = NULL;
2778 int n, flags, laces = 0;
2782 /* first byte(s): tracknum */
2783 if ((n = matroska_ebmlnum_uint(data, size, &num)) < 0) {
2784 av_log(matroska->ctx, AV_LOG_ERROR, "EBML block data error\n");
2791 /* fetch track from num */
2792 track = matroska_find_track_by_num(matroska, num);
2793 if (size <= 3 || track < 0 || track >= matroska->num_tracks) {
2794 av_log(matroska->ctx, AV_LOG_INFO,
2795 "Invalid stream %d or size %u\n", track, size);
2799 stream_index = matroska->tracks[track]->stream_index;
2800 if (stream_index < 0 || stream_index >= matroska->ctx->nb_streams) {
2804 st = matroska->ctx->streams[stream_index];
2805 if (st->discard >= AVDISCARD_ALL) {
2809 if (duration == AV_NOPTS_VALUE)
2810 duration = matroska->tracks[track]->default_duration / matroska->time_scale;
2812 /* block_time (relative to cluster time) */
2813 block_time = AV_RB16(data);
2817 if (is_keyframe == -1)
2818 is_keyframe = flags & 0x80 ? PKT_FLAG_KEY : 0;
2820 if (matroska->skip_to_keyframe) {
2821 if (!is_keyframe || st != matroska->skip_to_stream) {
2825 matroska->skip_to_keyframe = 0;
2828 switch ((flags & 0x06) >> 1) {
2829 case 0x0: /* no lacing */
2831 lace_size = av_mallocz(sizeof(int));
2832 lace_size[0] = size;
2835 case 0x1: /* xiph lacing */
2836 case 0x2: /* fixed-size lacing */
2837 case 0x3: /* EBML lacing */
2838 assert(size>0); // size <=3 is checked before size-=3 above
2839 laces = (*data) + 1;
2842 lace_size = av_mallocz(laces * sizeof(int));
2844 switch ((flags & 0x06) >> 1) {
2845 case 0x1: /* xiph lacing */ {
2848 for (n = 0; res == 0 && n < laces - 1; n++) {
2855 lace_size[n] += temp;
2861 total += lace_size[n];
2863 lace_size[n] = size - total;
2867 case 0x2: /* fixed-size lacing */
2868 for (n = 0; n < laces; n++)
2869 lace_size[n] = size / laces;
2872 case 0x3: /* EBML lacing */ {
2874 n = matroska_ebmlnum_uint(data, size, &num);
2876 av_log(matroska->ctx, AV_LOG_INFO,
2877 "EBML block data error\n");
2882 total = lace_size[0] = num;
2883 for (n = 1; res == 0 && n < laces - 1; n++) {
2886 r = matroska_ebmlnum_sint (data, size, &snum);
2888 av_log(matroska->ctx, AV_LOG_INFO,
2889 "EBML block data error\n");
2894 lace_size[n] = lace_size[n - 1] + snum;
2895 total += lace_size[n];
2897 lace_size[n] = size - total;
2905 uint64_t timecode = AV_NOPTS_VALUE;
2907 if (cluster_time != (uint64_t)-1
2908 && (block_time >= 0 || cluster_time >= -block_time))
2909 timecode = cluster_time + block_time;
2911 for (n = 0; n < laces; n++) {
2912 if (st->codec->codec_id == CODEC_ID_RA_288 ||
2913 st->codec->codec_id == CODEC_ID_COOK ||
2914 st->codec->codec_id == CODEC_ID_ATRAC3) {
2915 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)matroska->tracks[track];
2916 int a = st->codec->block_align;
2917 int sps = audiotrack->sub_packet_size;
2918 int cfs = audiotrack->coded_framesize;
2919 int h = audiotrack->sub_packet_h;
2920 int y = audiotrack->sub_packet_cnt;
2921 int w = audiotrack->frame_size;
2924 if (!audiotrack->pkt_cnt) {
2925 if (st->codec->codec_id == CODEC_ID_RA_288)
2926 for (x=0; x<h/2; x++)
2927 memcpy(audiotrack->buf+x*2*w+y*cfs,
2930 for (x=0; x<w/sps; x++)
2931 memcpy(audiotrack->buf+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), data+x*sps, sps);
2933 if (++audiotrack->sub_packet_cnt >= h) {
2934 audiotrack->sub_packet_cnt = 0;
2935 audiotrack->pkt_cnt = h*w / a;
2938 while (audiotrack->pkt_cnt) {
2939 pkt = av_mallocz(sizeof(AVPacket));
2940 av_new_packet(pkt, a);
2941 memcpy(pkt->data, audiotrack->buf
2942 + a * (h*w / a - audiotrack->pkt_cnt--), a);
2944 pkt->stream_index = stream_index;
2945 matroska_queue_packet(matroska, pkt);
2948 int offset = 0, pkt_size = lace_size[n];
2949 uint8_t *pkt_data = data;
2951 if (matroska->tracks[track]->encoding_scope & 1) {
2952 offset = matroska_decode_buffer(&pkt_data, &pkt_size,
2953 matroska->tracks[track]);
2958 pkt = av_mallocz(sizeof(AVPacket));
2959 /* XXX: prevent data copy... */
2960 if (av_new_packet(pkt, pkt_size+offset) < 0) {
2962 res = AVERROR(ENOMEM);
2967 memcpy (pkt->data, matroska->tracks[track]->encoding_settings, offset);
2968 memcpy (pkt->data+offset, pkt_data, pkt_size);
2970 if (pkt_data != data)
2974 pkt->flags = is_keyframe;
2975 pkt->stream_index = stream_index;
2977 pkt->pts = timecode;
2979 pkt->duration = duration;
2981 matroska_queue_packet(matroska, pkt);
2984 if (timecode != AV_NOPTS_VALUE)
2985 timecode = duration ? timecode + duration : AV_NOPTS_VALUE;
2986 data += lace_size[n];
2996 matroska_parse_blockgroup (MatroskaDemuxContext *matroska,
2997 uint64_t cluster_time)
3001 int is_keyframe = PKT_FLAG_KEY, last_num_packets = matroska->num_packets;
3002 uint64_t duration = AV_NOPTS_VALUE;
3007 av_log(matroska->ctx, AV_LOG_DEBUG, "parsing blockgroup...\n");
3010 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
3013 } else if (matroska->level_up) {
3014 matroska->level_up--;
3019 /* one block inside the group. Note, block parsing is one
3020 * of the harder things, so this code is a bit complicated.
3021 * See http://www.matroska.org/ for documentation. */
3022 case MATROSKA_ID_BLOCK: {
3023 pos = url_ftell(matroska->ctx->pb);
3024 res = ebml_read_binary(matroska, &id, &data, &size);
3028 case MATROSKA_ID_BLOCKDURATION: {
3029 if ((res = ebml_read_uint(matroska, &id, &duration)) < 0)
3034 case MATROSKA_ID_BLOCKREFERENCE: {
3036 /* We've found a reference, so not even the first frame in
3037 * the lace is a key frame. */
3039 if (last_num_packets != matroska->num_packets)
3040 matroska->packets[last_num_packets]->flags = 0;
3041 if ((res = ebml_read_sint(matroska, &id, &num)) < 0)
3047 av_log(matroska->ctx, AV_LOG_INFO,
3048 "Unknown entry 0x%x in blockgroup data\n", id);
3052 res = ebml_read_skip(matroska);
3056 if (matroska->level_up) {
3057 matroska->level_up--;
3066 res = matroska_parse_block(matroska, data, size, pos, cluster_time,
3067 duration, is_keyframe);
3073 matroska_parse_cluster (MatroskaDemuxContext *matroska)
3077 uint64_t cluster_time = 0;
3082 av_log(matroska->ctx, AV_LOG_DEBUG,
3083 "parsing cluster at %"PRId64"\n", url_ftell(matroska->ctx->pb));
3086 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
3089 } else if (matroska->level_up) {
3090 matroska->level_up--;
3095 /* cluster timecode */
3096 case MATROSKA_ID_CLUSTERTIMECODE: {
3098 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
3104 /* a group of blocks inside a cluster */
3105 case MATROSKA_ID_BLOCKGROUP:
3106 if ((res = ebml_read_master(matroska, &id)) < 0)
3108 res = matroska_parse_blockgroup(matroska, cluster_time);
3111 case MATROSKA_ID_SIMPLEBLOCK:
3112 pos = url_ftell(matroska->ctx->pb);
3113 res = ebml_read_binary(matroska, &id, &data, &size);
3115 res = matroska_parse_block(matroska, data, size, pos,
3116 cluster_time, AV_NOPTS_VALUE,
3121 av_log(matroska->ctx, AV_LOG_INFO,
3122 "Unknown entry 0x%x in cluster data\n", id);
3126 res = ebml_read_skip(matroska);
3130 if (matroska->level_up) {
3131 matroska->level_up--;
3140 matroska_read_packet (AVFormatContext *s,
3143 MatroskaDemuxContext *matroska = s->priv_data;
3147 /* Read stream until we have a packet queued. */
3148 while (matroska_deliver_packet(matroska, pkt)) {
3150 /* Have we already reached the end? */
3152 return AVERROR(EIO);
3156 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
3157 return AVERROR(EIO);
3158 } else if (matroska->level_up) {
3159 matroska->level_up--;
3164 case MATROSKA_ID_CLUSTER:
3165 if ((res = ebml_read_master(matroska, &id)) < 0)
3167 if ((res = matroska_parse_cluster(matroska)) == 0)
3168 res = 1; /* Parsed one cluster, let's get out. */
3173 res = ebml_read_skip(matroska);
3177 if (matroska->level_up) {
3178 matroska->level_up--;
3191 matroska_read_seek (AVFormatContext *s, int stream_index, int64_t timestamp,
3194 MatroskaDemuxContext *matroska = s->priv_data;
3195 AVStream *st = s->streams[stream_index];
3198 /* find index entry */
3199 index = av_index_search_timestamp(st, timestamp, flags);
3203 matroska_clear_queue(matroska);
3206 url_fseek(s->pb, st->index_entries[index].pos, SEEK_SET);
3207 matroska->skip_to_keyframe = !(flags & AVSEEK_FLAG_ANY);
3208 matroska->skip_to_stream = st;
3209 matroska->peek_id = 0;
3210 av_update_cur_dts(s, st, st->index_entries[index].timestamp);
3215 matroska_read_close (AVFormatContext *s)
3217 MatroskaDemuxContext *matroska = s->priv_data;
3220 av_free(matroska->index);
3222 matroska_clear_queue(matroska);
3224 for (n = 0; n < matroska->num_tracks; n++) {
3225 MatroskaTrack *track = matroska->tracks[n];
3226 av_free(track->codec_id);
3227 av_free(track->codec_priv);
3228 av_free(track->name);
3230 if (track->type == MATROSKA_TRACK_TYPE_AUDIO) {
3231 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
3232 av_free(audiotrack->buf);
3241 AVInputFormat matroska_demuxer = {
3243 NULL_IF_CONFIG_SMALL("Matroska file format"),
3244 sizeof(MatroskaDemuxContext),
3246 matroska_read_header,
3247 matroska_read_packet,
3248 matroska_read_close,