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(). */
34 #include "intfloat_readwrite.h"
37 typedef struct Track {
38 MatroskaTrackType type;
40 /* Unique track number and track ID. stream_index is the index that
41 * the calling app uses for this track. */
52 unsigned char *codec_priv;
55 uint64_t default_duration;
56 MatroskaTrackFlags flags;
59 typedef struct MatroskaVideoTrack {
69 MatroskaAspectRatioMode ar_mode;
70 MatroskaEyeMode eye_mode;
75 typedef struct MatroskaAudioTrack {
80 int internal_samplerate;
84 /* real audio header */
95 typedef struct MatroskaSubtitleTrack {
100 } MatroskaSubtitleTrack;
102 #define MAX_TRACK_SIZE (FFMAX(FFMAX(sizeof(MatroskaVideoTrack), \
103 sizeof(MatroskaAudioTrack)), \
104 sizeof(MatroskaSubtitleTrack)))
106 typedef struct MatroskaLevel {
111 typedef struct MatroskaDemuxIndex {
112 uint64_t pos; /* of the corresponding *cluster*! */
113 uint16_t track; /* reference to 'num' */
114 uint64_t time; /* in nanoseconds */
115 } MatroskaDemuxIndex;
117 typedef struct MatroskaDemuxContext {
118 AVFormatContext *ctx;
122 MatroskaLevel levels[EBML_MAX_DEPTH];
130 /* timescale in the file */
133 /* num_streams is the number of streams that av_new_stream() was called
134 * for ( = that are available to the calling program). */
137 MatroskaTrack *tracks[MAX_STREAMS];
139 /* cache for ID peeking */
142 /* byte position of the segment inside the stream */
143 offset_t segment_start;
145 /* The packet queue. */
149 /* have we already parse metadata/cues/clusters? */
154 /* The index for seeking. */
156 MatroskaDemuxIndex *index;
158 /* What to skip before effectively reading a packet. */
159 int skip_to_keyframe;
160 AVStream *skip_to_stream;
161 } MatroskaDemuxContext;
164 * The first few functions handle EBML file parsing. The rest
165 * is the document interpretation. Matroska really just is a
170 * Return: the amount of levels in the hierarchy that the
171 * current element lies higher than the previous one.
172 * The opposite isn't done - that's auto-done using master
177 ebml_read_element_level_up (MatroskaDemuxContext *matroska)
179 ByteIOContext *pb = matroska->ctx->pb;
180 offset_t pos = url_ftell(pb);
183 while (matroska->num_levels > 0) {
184 MatroskaLevel *level = &matroska->levels[matroska->num_levels - 1];
186 if (pos >= level->start + level->length) {
187 matroska->num_levels--;
198 * Read: an "EBML number", which is defined as a variable-length
199 * array of bytes. The first byte indicates the length by giving a
200 * number of 0-bits followed by a one. The position of the first
201 * "one" bit inside the first byte indicates the length of this
203 * Returns: num. of bytes read. < 0 on error.
207 ebml_read_num (MatroskaDemuxContext *matroska,
211 ByteIOContext *pb = matroska->ctx->pb;
212 int len_mask = 0x80, read = 1, n = 1;
215 /* the first byte tells us the length in bytes - get_byte() can normally
216 * return 0, but since that's not a valid first ebmlID byte, we can
217 * use it safely here to catch EOS. */
218 if (!(total = get_byte(pb))) {
219 /* we might encounter EOS here */
221 offset_t pos = url_ftell(pb);
222 av_log(matroska->ctx, AV_LOG_ERROR,
223 "Read error at pos. %"PRIu64" (0x%"PRIx64")\n",
226 return AVERROR(EIO); /* EOS or actual I/O error */
229 /* get the length of the EBML number */
230 while (read <= max_size && !(total & len_mask)) {
234 if (read > max_size) {
235 offset_t pos = url_ftell(pb) - 1;
236 av_log(matroska->ctx, AV_LOG_ERROR,
237 "Invalid EBML number size tag 0x%02x at pos %"PRIu64" (0x%"PRIx64")\n",
238 (uint8_t) total, pos, pos);
239 return AVERROR_INVALIDDATA;
242 /* read out length */
245 total = (total << 8) | get_byte(pb);
253 * Read: the element content data ID.
254 * Return: the number of bytes read or < 0 on error.
258 ebml_read_element_id (MatroskaDemuxContext *matroska,
265 /* if we re-call this, use our cached ID */
266 if (matroska->peek_id != 0) {
269 *id = matroska->peek_id;
273 /* read out the "EBML number", include tag in ID */
274 if ((read = ebml_read_num(matroska, 4, &total)) < 0)
276 *id = matroska->peek_id = total | (1 << (read * 7));
280 *level_up = ebml_read_element_level_up(matroska);
286 * Read: element content length.
287 * Return: the number of bytes read or < 0 on error.
291 ebml_read_element_length (MatroskaDemuxContext *matroska,
294 /* clear cache since we're now beyond that data point */
295 matroska->peek_id = 0;
297 /* read out the "EBML number", include tag in ID */
298 return ebml_read_num(matroska, 8, length);
302 * Return: the ID of the next element, or 0 on error.
303 * Level_up contains the amount of levels that this
304 * next element lies higher than the previous one.
308 ebml_peek_id (MatroskaDemuxContext *matroska,
313 if (ebml_read_element_id(matroska, &id, level_up) < 0)
320 * Seek to a given offset.
321 * 0 is success, -1 is failure.
325 ebml_read_seek (MatroskaDemuxContext *matroska,
328 ByteIOContext *pb = matroska->ctx->pb;
330 /* clear ID cache, if any */
331 matroska->peek_id = 0;
333 return (url_fseek(pb, offset, SEEK_SET) == offset) ? 0 : -1;
337 * Skip the next element.
338 * 0 is success, -1 is failure.
342 ebml_read_skip (MatroskaDemuxContext *matroska)
344 ByteIOContext *pb = matroska->ctx->pb;
349 if ((res = ebml_read_element_id(matroska, &id, NULL)) < 0 ||
350 (res = ebml_read_element_length(matroska, &length)) < 0)
353 url_fskip(pb, length);
359 * Read the next element as an unsigned int.
360 * 0 is success, < 0 is failure.
364 ebml_read_uint (MatroskaDemuxContext *matroska,
368 ByteIOContext *pb = matroska->ctx->pb;
369 int n = 0, size, res;
372 if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
373 (res = ebml_read_element_length(matroska, &rlength)) < 0)
376 if (size < 1 || size > 8) {
377 offset_t pos = url_ftell(pb);
378 av_log(matroska->ctx, AV_LOG_ERROR,
379 "Invalid uint element size %d at position %"PRId64" (0x%"PRIx64")\n",
381 return AVERROR_INVALIDDATA;
384 /* big-endian ordening; build up number */
387 *num = (*num << 8) | get_byte(pb);
393 * Read the next element as a signed int.
394 * 0 is success, < 0 is failure.
398 ebml_read_sint (MatroskaDemuxContext *matroska,
402 ByteIOContext *pb = matroska->ctx->pb;
403 int size, n = 1, negative = 0, res;
406 if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
407 (res = ebml_read_element_length(matroska, &rlength)) < 0)
410 if (size < 1 || size > 8) {
411 offset_t pos = url_ftell(pb);
412 av_log(matroska->ctx, AV_LOG_ERROR,
413 "Invalid sint element size %d at position %"PRId64" (0x%"PRIx64")\n",
415 return AVERROR_INVALIDDATA;
417 if ((*num = get_byte(pb)) & 0x80) {
422 *num = (*num << 8) | get_byte(pb);
426 *num = *num - (1LL << ((8 * size) - 1));
432 * Read the next element as a float.
433 * 0 is success, < 0 is failure.
437 ebml_read_float (MatroskaDemuxContext *matroska,
441 ByteIOContext *pb = matroska->ctx->pb;
445 if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
446 (res = ebml_read_element_length(matroska, &rlength)) < 0)
451 *num= av_int2flt(get_be32(pb));
453 *num= av_int2dbl(get_be64(pb));
455 offset_t pos = url_ftell(pb);
456 av_log(matroska->ctx, AV_LOG_ERROR,
457 "Invalid float element size %d at position %"PRIu64" (0x%"PRIx64")\n",
459 return AVERROR_INVALIDDATA;
466 * Read the next element as an ASCII string.
467 * 0 is success, < 0 is failure.
471 ebml_read_ascii (MatroskaDemuxContext *matroska,
475 ByteIOContext *pb = matroska->ctx->pb;
479 if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
480 (res = ebml_read_element_length(matroska, &rlength)) < 0)
484 /* ebml strings are usually not 0-terminated, so we allocate one
485 * byte more, read the string and NULL-terminate it ourselves. */
486 if (size < 0 || !(*str = av_malloc(size + 1))) {
487 av_log(matroska->ctx, AV_LOG_ERROR, "Memory allocation failed\n");
488 return AVERROR(ENOMEM);
490 if (get_buffer(pb, (uint8_t *) *str, size) != size) {
491 offset_t pos = url_ftell(pb);
492 av_log(matroska->ctx, AV_LOG_ERROR,
493 "Read error at pos. %"PRIu64" (0x%"PRIx64")\n", pos, pos);
502 * Read the next element as a UTF-8 string.
503 * 0 is success, < 0 is failure.
507 ebml_read_utf8 (MatroskaDemuxContext *matroska,
511 return ebml_read_ascii(matroska, id, str);
515 * Read the next element as a date (nanoseconds since 1/1/2000).
516 * 0 is success, < 0 is failure.
520 ebml_read_date (MatroskaDemuxContext *matroska,
524 return ebml_read_sint(matroska, id, date);
528 * Read the next element, but only the header. The contents
529 * are supposed to be sub-elements which can be read separately.
530 * 0 is success, < 0 is failure.
534 ebml_read_master (MatroskaDemuxContext *matroska,
537 ByteIOContext *pb = matroska->ctx->pb;
539 MatroskaLevel *level;
542 if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
543 (res = ebml_read_element_length(matroska, &length)) < 0)
546 /* protect... (Heaven forbids that the '>' is true) */
547 if (matroska->num_levels >= EBML_MAX_DEPTH) {
548 av_log(matroska->ctx, AV_LOG_ERROR,
549 "File moves beyond max. allowed depth (%d)\n", EBML_MAX_DEPTH);
550 return AVERROR(ENOSYS);
554 level = &matroska->levels[matroska->num_levels++];
555 level->start = url_ftell(pb);
556 level->length = length;
562 * Read the next element as binary data.
563 * 0 is success, < 0 is failure.
567 ebml_read_binary (MatroskaDemuxContext *matroska,
572 ByteIOContext *pb = matroska->ctx->pb;
576 if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
577 (res = ebml_read_element_length(matroska, &rlength)) < 0)
581 if (!(*binary = av_malloc(*size))) {
582 av_log(matroska->ctx, AV_LOG_ERROR,
583 "Memory allocation error\n");
584 return AVERROR(ENOMEM);
587 if (get_buffer(pb, *binary, *size) != *size) {
588 offset_t pos = url_ftell(pb);
589 av_log(matroska->ctx, AV_LOG_ERROR,
590 "Read error at pos. %"PRIu64" (0x%"PRIx64")\n", pos, pos);
598 * Read signed/unsigned "EBML" numbers.
599 * Return: number of bytes processed, < 0 on error.
600 * XXX: use ebml_read_num().
604 matroska_ebmlnum_uint (uint8_t *data,
608 int len_mask = 0x80, read = 1, n = 1, num_ffs = 0;
612 return AVERROR_INVALIDDATA;
615 while (read <= 8 && !(total & len_mask)) {
620 return AVERROR_INVALIDDATA;
622 if ((total &= (len_mask - 1)) == len_mask - 1)
625 return AVERROR_INVALIDDATA;
629 total = (total << 8) | data[n];
642 * Same as above, but signed.
646 matroska_ebmlnum_sint (uint8_t *data,
653 /* read as unsigned number first */
654 if ((res = matroska_ebmlnum_uint(data, size, &unum)) < 0)
657 /* make signed (weird way) */
658 if (unum == (uint64_t)-1)
661 *num = unum - ((1LL << ((7 * res) - 1)) - 1);
667 * Read an EBML header.
668 * 0 is success, < 0 is failure.
672 ebml_read_header (MatroskaDemuxContext *matroska,
677 int level_up, res = 0;
685 if (!(id = ebml_peek_id(matroska, &level_up)) ||
686 level_up != 0 || id != EBML_ID_HEADER) {
687 av_log(matroska->ctx, AV_LOG_ERROR,
688 "This is not an EBML file (id=0x%x/0x%x)\n", id, EBML_ID_HEADER);
689 return AVERROR_INVALIDDATA;
691 if ((res = ebml_read_master(matroska, &id)) < 0)
695 if (!(id = ebml_peek_id(matroska, &level_up)))
703 /* is our read version uptodate? */
704 case EBML_ID_EBMLREADVERSION: {
707 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
709 if (num > EBML_VERSION) {
710 av_log(matroska->ctx, AV_LOG_ERROR,
711 "EBML version %"PRIu64" (> %d) is not supported\n",
713 return AVERROR_INVALIDDATA;
718 /* we only handle 8 byte lengths at max */
719 case EBML_ID_EBMLMAXSIZELENGTH: {
722 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
724 if (num > sizeof(uint64_t)) {
725 av_log(matroska->ctx, AV_LOG_ERROR,
726 "Integers of size %"PRIu64" (> %zd) not supported\n",
727 num, sizeof(uint64_t));
728 return AVERROR_INVALIDDATA;
733 /* we handle 4 byte IDs at max */
734 case EBML_ID_EBMLMAXIDLENGTH: {
737 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
739 if (num > sizeof(uint32_t)) {
740 av_log(matroska->ctx, AV_LOG_ERROR,
741 "IDs of size %"PRIu64" (> %zu) not supported\n",
742 num, sizeof(uint32_t));
743 return AVERROR_INVALIDDATA;
748 case EBML_ID_DOCTYPE: {
751 if ((res = ebml_read_ascii(matroska, &id, &text)) < 0)
762 case EBML_ID_DOCTYPEREADVERSION: {
765 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
773 av_log(matroska->ctx, AV_LOG_INFO,
774 "Unknown data type 0x%x in EBML header", id);
778 /* we ignore these two, as they don't tell us anything we
780 case EBML_ID_EBMLVERSION:
781 case EBML_ID_DOCTYPEVERSION:
782 res = ebml_read_skip (matroska);
792 matroska_find_track_by_num (MatroskaDemuxContext *matroska,
797 for (i = 0; i < matroska->num_tracks; i++)
798 if (matroska->tracks[i]->num == num)
806 * Put one packet in an application-supplied AVPacket struct.
807 * Returns 0 on success or -1 on failure.
811 matroska_deliver_packet (MatroskaDemuxContext *matroska,
814 if (matroska->num_packets > 0) {
815 memcpy(pkt, matroska->packets[0], sizeof(AVPacket));
816 av_free(matroska->packets[0]);
817 if (matroska->num_packets > 1) {
818 memmove(&matroska->packets[0], &matroska->packets[1],
819 (matroska->num_packets - 1) * sizeof(AVPacket *));
821 av_realloc(matroska->packets, (matroska->num_packets - 1) *
824 av_freep(&matroska->packets);
826 matroska->num_packets--;
834 * Put a packet into our internal queue. Will be delivered to the
835 * user/application during the next get_packet() call.
839 matroska_queue_packet (MatroskaDemuxContext *matroska,
843 av_realloc(matroska->packets, (matroska->num_packets + 1) *
845 matroska->packets[matroska->num_packets] = pkt;
846 matroska->num_packets++;
855 matroska_probe (AVProbeData *p)
858 int len_mask = 0x80, size = 1, n = 1;
859 uint8_t probe_data[] = { 'm', 'a', 't', 'r', 'o', 's', 'k', 'a' };
862 if (AV_RB32(p->buf) != EBML_ID_HEADER)
865 /* length of header */
867 while (size <= 8 && !(total & len_mask)) {
873 total &= (len_mask - 1);
875 total = (total << 8) | p->buf[4 + n++];
877 /* does the probe data contain the whole header? */
878 if (p->buf_size < 4 + size + total)
881 /* the header must contain the document type 'matroska'. For now,
882 * we don't parse the whole header but simply check for the
883 * availability of that array of characters inside the header.
884 * Not fully fool-proof, but good enough. */
885 for (n = 4 + size; n <= 4 + size + total - sizeof(probe_data); n++)
886 if (!memcmp (&p->buf[n], probe_data, sizeof(probe_data)))
887 return AVPROBE_SCORE_MAX;
893 * From here on, it's all XML-style DTD stuff... Needs no comments.
897 matroska_parse_info (MatroskaDemuxContext *matroska)
902 av_log(matroska->ctx, AV_LOG_DEBUG, "Parsing info...\n");
905 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
908 } else if (matroska->level_up) {
909 matroska->level_up--;
914 /* cluster timecode */
915 case MATROSKA_ID_TIMECODESCALE: {
917 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
919 matroska->time_scale = num;
923 case MATROSKA_ID_DURATION: {
925 if ((res = ebml_read_float(matroska, &id, &num)) < 0)
927 matroska->ctx->duration = num * matroska->time_scale * 1000 / AV_TIME_BASE;
931 case MATROSKA_ID_TITLE: {
933 if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
935 strncpy(matroska->ctx->title, text,
936 sizeof(matroska->ctx->title)-1);
941 case MATROSKA_ID_WRITINGAPP: {
943 if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
945 matroska->writing_app = text;
949 case MATROSKA_ID_MUXINGAPP: {
951 if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
953 matroska->muxing_app = text;
957 case MATROSKA_ID_DATEUTC: {
959 if ((res = ebml_read_date(matroska, &id, &time)) < 0)
961 matroska->created = time;
966 av_log(matroska->ctx, AV_LOG_INFO,
967 "Unknown entry 0x%x in info header\n", id);
971 res = ebml_read_skip(matroska);
975 if (matroska->level_up) {
976 matroska->level_up--;
985 matroska_add_stream (MatroskaDemuxContext *matroska)
989 MatroskaTrack *track;
991 av_log(matroska->ctx, AV_LOG_DEBUG, "parsing track, adding stream..,\n");
993 /* Allocate a generic track. As soon as we know its type we'll realloc. */
994 track = av_mallocz(MAX_TRACK_SIZE);
995 matroska->num_tracks++;
996 strcpy(track->language, "eng");
998 /* start with the master */
999 if ((res = ebml_read_master(matroska, &id)) < 0)
1002 /* try reading the trackentry headers */
1004 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1007 } else if (matroska->level_up > 0) {
1008 matroska->level_up--;
1013 /* track number (unique stream ID) */
1014 case MATROSKA_ID_TRACKNUMBER: {
1016 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1022 /* track UID (unique identifier) */
1023 case MATROSKA_ID_TRACKUID: {
1025 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1031 /* track type (video, audio, combined, subtitle, etc.) */
1032 case MATROSKA_ID_TRACKTYPE: {
1034 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1036 if (track->type && track->type != num) {
1037 av_log(matroska->ctx, AV_LOG_INFO,
1038 "More than one tracktype in an entry - skip\n");
1043 switch (track->type) {
1044 case MATROSKA_TRACK_TYPE_VIDEO:
1045 case MATROSKA_TRACK_TYPE_AUDIO:
1046 case MATROSKA_TRACK_TYPE_SUBTITLE:
1048 case MATROSKA_TRACK_TYPE_COMPLEX:
1049 case MATROSKA_TRACK_TYPE_LOGO:
1050 case MATROSKA_TRACK_TYPE_CONTROL:
1052 av_log(matroska->ctx, AV_LOG_INFO,
1053 "Unknown or unsupported track type 0x%x\n",
1058 matroska->tracks[matroska->num_tracks - 1] = track;
1062 /* tracktype specific stuff for video */
1063 case MATROSKA_ID_TRACKVIDEO: {
1064 MatroskaVideoTrack *videotrack;
1066 track->type = MATROSKA_TRACK_TYPE_VIDEO;
1067 if (track->type != MATROSKA_TRACK_TYPE_VIDEO) {
1068 av_log(matroska->ctx, AV_LOG_INFO,
1069 "video data in non-video track - ignoring\n");
1070 res = AVERROR_INVALIDDATA;
1072 } else if ((res = ebml_read_master(matroska, &id)) < 0)
1074 videotrack = (MatroskaVideoTrack *)track;
1077 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1080 } else if (matroska->level_up > 0) {
1081 matroska->level_up--;
1086 /* fixme, this should be one-up, but I get it here */
1087 case MATROSKA_ID_TRACKDEFAULTDURATION: {
1089 if ((res = ebml_read_uint (matroska, &id,
1092 track->default_duration = num;
1096 /* video framerate */
1097 case MATROSKA_ID_VIDEOFRAMERATE: {
1099 if ((res = ebml_read_float(matroska, &id,
1102 if (!track->default_duration)
1103 track->default_duration = 1000000000/num;
1107 /* width of the size to display the video at */
1108 case MATROSKA_ID_VIDEODISPLAYWIDTH: {
1110 if ((res = ebml_read_uint(matroska, &id,
1113 videotrack->display_width = num;
1117 /* height of the size to display the video at */
1118 case MATROSKA_ID_VIDEODISPLAYHEIGHT: {
1120 if ((res = ebml_read_uint(matroska, &id,
1123 videotrack->display_height = num;
1127 /* width of the video in the file */
1128 case MATROSKA_ID_VIDEOPIXELWIDTH: {
1130 if ((res = ebml_read_uint(matroska, &id,
1133 videotrack->pixel_width = num;
1137 /* height of the video in the file */
1138 case MATROSKA_ID_VIDEOPIXELHEIGHT: {
1140 if ((res = ebml_read_uint(matroska, &id,
1143 videotrack->pixel_height = num;
1147 /* whether the video is interlaced */
1148 case MATROSKA_ID_VIDEOFLAGINTERLACED: {
1150 if ((res = ebml_read_uint(matroska, &id,
1155 MATROSKA_VIDEOTRACK_INTERLACED;
1158 ~MATROSKA_VIDEOTRACK_INTERLACED;
1162 /* stereo mode (whether the video has two streams,
1163 * where one is for the left eye and the other for
1164 * the right eye, which creates a 3D-like
1166 case MATROSKA_ID_VIDEOSTEREOMODE: {
1168 if ((res = ebml_read_uint(matroska, &id,
1171 if (num != MATROSKA_EYE_MODE_MONO &&
1172 num != MATROSKA_EYE_MODE_LEFT &&
1173 num != MATROSKA_EYE_MODE_RIGHT &&
1174 num != MATROSKA_EYE_MODE_BOTH) {
1175 av_log(matroska->ctx, AV_LOG_INFO,
1176 "Ignoring unknown eye mode 0x%x\n",
1180 videotrack->eye_mode = num;
1184 /* aspect ratio behaviour */
1185 case MATROSKA_ID_VIDEOASPECTRATIO: {
1187 if ((res = ebml_read_uint(matroska, &id,
1190 if (num != MATROSKA_ASPECT_RATIO_MODE_FREE &&
1191 num != MATROSKA_ASPECT_RATIO_MODE_KEEP &&
1192 num != MATROSKA_ASPECT_RATIO_MODE_FIXED) {
1193 av_log(matroska->ctx, AV_LOG_INFO,
1194 "Ignoring unknown aspect ratio 0x%x\n",
1198 videotrack->ar_mode = num;
1202 /* colourspace (only matters for raw video)
1204 case MATROSKA_ID_VIDEOCOLOURSPACE: {
1206 if ((res = ebml_read_uint(matroska, &id,
1209 videotrack->fourcc = num;
1214 av_log(matroska->ctx, AV_LOG_INFO,
1215 "Unknown video track header entry "
1216 "0x%x - ignoring\n", id);
1220 res = ebml_read_skip(matroska);
1224 if (matroska->level_up) {
1225 matroska->level_up--;
1232 /* tracktype specific stuff for audio */
1233 case MATROSKA_ID_TRACKAUDIO: {
1234 MatroskaAudioTrack *audiotrack;
1236 track->type = MATROSKA_TRACK_TYPE_AUDIO;
1237 if (track->type != MATROSKA_TRACK_TYPE_AUDIO) {
1238 av_log(matroska->ctx, AV_LOG_INFO,
1239 "audio data in non-audio track - ignoring\n");
1240 res = AVERROR_INVALIDDATA;
1242 } else if ((res = ebml_read_master(matroska, &id)) < 0)
1244 audiotrack = (MatroskaAudioTrack *)track;
1245 audiotrack->channels = 1;
1246 audiotrack->samplerate = 8000;
1249 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1252 } else if (matroska->level_up > 0) {
1253 matroska->level_up--;
1259 case MATROSKA_ID_AUDIOSAMPLINGFREQ: {
1261 if ((res = ebml_read_float(matroska, &id,
1264 audiotrack->internal_samplerate =
1265 audiotrack->samplerate = num;
1269 case MATROSKA_ID_AUDIOOUTSAMPLINGFREQ: {
1271 if ((res = ebml_read_float(matroska, &id,
1274 audiotrack->samplerate = num;
1279 case MATROSKA_ID_AUDIOBITDEPTH: {
1281 if ((res = ebml_read_uint(matroska, &id,
1284 audiotrack->bitdepth = num;
1289 case MATROSKA_ID_AUDIOCHANNELS: {
1291 if ((res = ebml_read_uint(matroska, &id,
1294 audiotrack->channels = num;
1299 av_log(matroska->ctx, AV_LOG_INFO,
1300 "Unknown audio track header entry "
1301 "0x%x - ignoring\n", id);
1305 res = ebml_read_skip(matroska);
1309 if (matroska->level_up) {
1310 matroska->level_up--;
1317 /* codec identifier */
1318 case MATROSKA_ID_CODECID: {
1320 if ((res = ebml_read_ascii(matroska, &id, &text)) < 0)
1322 track->codec_id = text;
1326 /* codec private data */
1327 case MATROSKA_ID_CODECPRIVATE: {
1330 if ((res = ebml_read_binary(matroska, &id, &data, &size) < 0))
1332 track->codec_priv = data;
1333 track->codec_priv_size = size;
1337 /* name of the codec */
1338 case MATROSKA_ID_CODECNAME: {
1340 if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
1342 track->codec_name = text;
1346 /* name of this track */
1347 case MATROSKA_ID_TRACKNAME: {
1349 if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
1355 /* language (matters for audio/subtitles, mostly) */
1356 case MATROSKA_ID_TRACKLANGUAGE: {
1358 if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
1360 if ((end = strchr(text, '-')))
1362 if (strlen(text) == 3)
1363 strcpy(track->language, text);
1368 /* whether this is actually used */
1369 case MATROSKA_ID_TRACKFLAGENABLED: {
1371 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1374 track->flags |= MATROSKA_TRACK_ENABLED;
1376 track->flags &= ~MATROSKA_TRACK_ENABLED;
1380 /* whether it's the default for this track type */
1381 case MATROSKA_ID_TRACKFLAGDEFAULT: {
1383 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1386 track->flags |= MATROSKA_TRACK_DEFAULT;
1388 track->flags &= ~MATROSKA_TRACK_DEFAULT;
1392 /* lacing (like MPEG, where blocks don't end/start on frame
1394 case MATROSKA_ID_TRACKFLAGLACING: {
1396 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1399 track->flags |= MATROSKA_TRACK_LACING;
1401 track->flags &= ~MATROSKA_TRACK_LACING;
1405 /* default length (in time) of one data block in this track */
1406 case MATROSKA_ID_TRACKDEFAULTDURATION: {
1408 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1410 track->default_duration = num;
1415 av_log(matroska->ctx, AV_LOG_INFO,
1416 "Unknown track header entry 0x%x - ignoring\n", id);
1420 /* we ignore these because they're nothing useful. */
1421 case MATROSKA_ID_CODECINFOURL:
1422 case MATROSKA_ID_CODECDOWNLOADURL:
1423 case MATROSKA_ID_TRACKMINCACHE:
1424 case MATROSKA_ID_TRACKMAXCACHE:
1425 res = ebml_read_skip(matroska);
1429 if (matroska->level_up) {
1430 matroska->level_up--;
1439 matroska_parse_tracks (MatroskaDemuxContext *matroska)
1444 av_log(matroska->ctx, AV_LOG_DEBUG, "parsing tracks...\n");
1447 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1450 } else if (matroska->level_up) {
1451 matroska->level_up--;
1456 /* one track within the "all-tracks" header */
1457 case MATROSKA_ID_TRACKENTRY:
1458 res = matroska_add_stream(matroska);
1462 av_log(matroska->ctx, AV_LOG_INFO,
1463 "Unknown entry 0x%x in track header\n", id);
1467 res = ebml_read_skip(matroska);
1471 if (matroska->level_up) {
1472 matroska->level_up--;
1481 matroska_parse_index (MatroskaDemuxContext *matroska)
1485 MatroskaDemuxIndex idx;
1487 av_log(matroska->ctx, AV_LOG_DEBUG, "parsing index...\n");
1490 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1493 } else if (matroska->level_up) {
1494 matroska->level_up--;
1499 /* one single index entry ('point') */
1500 case MATROSKA_ID_POINTENTRY:
1501 if ((res = ebml_read_master(matroska, &id)) < 0)
1504 /* in the end, we hope to fill one entry with a
1505 * timestamp, a file position and a tracknum */
1506 idx.pos = (uint64_t) -1;
1507 idx.time = (uint64_t) -1;
1508 idx.track = (uint16_t) -1;
1511 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1514 } else if (matroska->level_up) {
1515 matroska->level_up--;
1520 /* one single index entry ('point') */
1521 case MATROSKA_ID_CUETIME: {
1523 if ((res = ebml_read_uint(matroska, &id,
1526 idx.time = time * matroska->time_scale;
1530 /* position in the file + track to which it
1532 case MATROSKA_ID_CUETRACKPOSITION:
1533 if ((res = ebml_read_master(matroska, &id)) < 0)
1537 if (!(id = ebml_peek_id (matroska,
1538 &matroska->level_up))) {
1541 } else if (matroska->level_up) {
1542 matroska->level_up--;
1548 case MATROSKA_ID_CUETRACK: {
1550 if ((res = ebml_read_uint(matroska,
1557 /* position in file */
1558 case MATROSKA_ID_CUECLUSTERPOSITION: {
1560 if ((res = ebml_read_uint(matroska,
1563 idx.pos = num+matroska->segment_start;
1568 av_log(matroska->ctx, AV_LOG_INFO,
1569 "Unknown entry 0x%x in "
1570 "CuesTrackPositions\n", id);
1574 res = ebml_read_skip(matroska);
1578 if (matroska->level_up) {
1579 matroska->level_up--;
1587 av_log(matroska->ctx, AV_LOG_INFO,
1588 "Unknown entry 0x%x in cuespoint "
1593 res = ebml_read_skip(matroska);
1597 if (matroska->level_up) {
1598 matroska->level_up--;
1603 /* so let's see if we got what we wanted */
1604 if (idx.pos != (uint64_t) -1 &&
1605 idx.time != (uint64_t) -1 &&
1606 idx.track != (uint16_t) -1) {
1607 if (matroska->num_indexes % 32 == 0) {
1608 /* re-allocate bigger index */
1610 av_realloc(matroska->index,
1611 (matroska->num_indexes + 32) *
1612 sizeof(MatroskaDemuxIndex));
1614 matroska->index[matroska->num_indexes] = idx;
1615 matroska->num_indexes++;
1620 av_log(matroska->ctx, AV_LOG_INFO,
1621 "Unknown entry 0x%x in cues header\n", id);
1625 res = ebml_read_skip(matroska);
1629 if (matroska->level_up) {
1630 matroska->level_up--;
1639 matroska_parse_metadata (MatroskaDemuxContext *matroska)
1645 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1648 } else if (matroska->level_up) {
1649 matroska->level_up--;
1654 /* Hm, this is unsupported... */
1656 av_log(matroska->ctx, AV_LOG_INFO,
1657 "Unknown entry 0x%x in metadata header\n", id);
1661 res = ebml_read_skip(matroska);
1665 if (matroska->level_up) {
1666 matroska->level_up--;
1675 matroska_parse_seekhead (MatroskaDemuxContext *matroska)
1680 av_log(matroska->ctx, AV_LOG_DEBUG, "parsing seekhead...\n");
1683 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1686 } else if (matroska->level_up) {
1687 matroska->level_up--;
1692 case MATROSKA_ID_SEEKENTRY: {
1693 uint32_t seek_id = 0, peek_id_cache = 0;
1694 uint64_t seek_pos = (uint64_t) -1, t;
1696 if ((res = ebml_read_master(matroska, &id)) < 0)
1700 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1703 } else if (matroska->level_up) {
1704 matroska->level_up--;
1709 case MATROSKA_ID_SEEKID:
1710 res = ebml_read_uint(matroska, &id, &t);
1714 case MATROSKA_ID_SEEKPOSITION:
1715 res = ebml_read_uint(matroska, &id, &seek_pos);
1719 av_log(matroska->ctx, AV_LOG_INFO,
1720 "Unknown seekhead ID 0x%x\n", id);
1724 res = ebml_read_skip(matroska);
1728 if (matroska->level_up) {
1729 matroska->level_up--;
1734 if (!seek_id || seek_pos == (uint64_t) -1) {
1735 av_log(matroska->ctx, AV_LOG_INFO,
1736 "Incomplete seekhead entry (0x%x/%"PRIu64")\n",
1742 case MATROSKA_ID_CUES:
1743 case MATROSKA_ID_TAGS: {
1744 uint32_t level_up = matroska->level_up;
1745 offset_t before_pos;
1747 MatroskaLevel level;
1749 /* remember the peeked ID and the current position */
1750 peek_id_cache = matroska->peek_id;
1751 before_pos = url_ftell(matroska->ctx->pb);
1754 if ((res = ebml_read_seek(matroska, seek_pos +
1755 matroska->segment_start)) < 0)
1758 /* we don't want to lose our seekhead level, so we add
1759 * a dummy. This is a crude hack. */
1760 if (matroska->num_levels == EBML_MAX_DEPTH) {
1761 av_log(matroska->ctx, AV_LOG_INFO,
1762 "Max EBML element depth (%d) reached, "
1763 "cannot parse further.\n", EBML_MAX_DEPTH);
1764 return AVERROR_UNKNOWN;
1768 level.length = (uint64_t)-1;
1769 matroska->levels[matroska->num_levels] = level;
1770 matroska->num_levels++;
1773 if (!(id = ebml_peek_id (matroska,
1774 &matroska->level_up)))
1776 if (id != seek_id) {
1777 av_log(matroska->ctx, AV_LOG_INFO,
1778 "We looked for ID=0x%x but got "
1779 "ID=0x%x (pos=%"PRIu64")",
1780 seek_id, id, seek_pos +
1781 matroska->segment_start);
1785 /* read master + parse */
1786 if ((res = ebml_read_master(matroska, &id)) < 0)
1789 case MATROSKA_ID_CUES:
1790 if (!(res = matroska_parse_index(matroska)) ||
1791 url_feof(matroska->ctx->pb)) {
1792 matroska->index_parsed = 1;
1796 case MATROSKA_ID_TAGS:
1797 if (!(res = matroska_parse_metadata(matroska)) ||
1798 url_feof(matroska->ctx->pb)) {
1799 matroska->metadata_parsed = 1;
1806 /* remove dummy level */
1807 while (matroska->num_levels) {
1808 matroska->num_levels--;
1810 matroska->levels[matroska->num_levels].length;
1811 if (length == (uint64_t)-1)
1816 if ((res = ebml_read_seek(matroska, before_pos)) < 0)
1818 matroska->peek_id = peek_id_cache;
1819 matroska->level_up = level_up;
1824 av_log(matroska->ctx, AV_LOG_INFO,
1825 "Ignoring seekhead entry for ID=0x%x\n",
1834 av_log(matroska->ctx, AV_LOG_INFO,
1835 "Unknown seekhead ID 0x%x\n", id);
1839 res = ebml_read_skip(matroska);
1843 if (matroska->level_up) {
1844 matroska->level_up--;
1852 #define ARRAY_SIZE(x) (sizeof(x)/sizeof(*x))
1855 matroska_aac_profile (char *codec_id)
1857 static const char *aac_profiles[] = {
1862 for (profile=0; profile<ARRAY_SIZE(aac_profiles); profile++)
1863 if (strstr(codec_id, aac_profiles[profile]))
1869 matroska_aac_sri (int samplerate)
1871 static const int aac_sample_rates[] = {
1872 96000, 88200, 64000, 48000, 44100, 32000,
1873 24000, 22050, 16000, 12000, 11025, 8000,
1877 for (sri=0; sri<ARRAY_SIZE(aac_sample_rates); sri++)
1878 if (aac_sample_rates[sri] == samplerate)
1884 matroska_read_header (AVFormatContext *s,
1885 AVFormatParameters *ap)
1887 MatroskaDemuxContext *matroska = s->priv_data;
1889 int version, last_level, res = 0;
1894 /* First read the EBML header. */
1896 if ((res = ebml_read_header(matroska, &doctype, &version)) < 0)
1898 if ((doctype == NULL) || strcmp(doctype, "matroska")) {
1899 av_log(matroska->ctx, AV_LOG_ERROR,
1900 "Wrong EBML doctype ('%s' != 'matroska').\n",
1901 doctype ? doctype : "(none)");
1904 return AVERROR_NOFMT;
1908 av_log(matroska->ctx, AV_LOG_ERROR,
1909 "Matroska demuxer version 2 too old for file version %d\n",
1911 return AVERROR_NOFMT;
1914 /* The next thing is a segment. */
1916 if (!(id = ebml_peek_id(matroska, &last_level)))
1917 return AVERROR(EIO);
1918 if (id == MATROSKA_ID_SEGMENT)
1922 av_log(matroska->ctx, AV_LOG_INFO,
1923 "Expected a Segment ID (0x%x), but received 0x%x!\n",
1924 MATROSKA_ID_SEGMENT, id);
1925 if ((res = ebml_read_skip(matroska)) < 0)
1929 /* We now have a Matroska segment.
1930 * Seeks are from the beginning of the segment,
1931 * after the segment ID/length. */
1932 if ((res = ebml_read_master(matroska, &id)) < 0)
1934 matroska->segment_start = url_ftell(s->pb);
1936 matroska->time_scale = 1000000;
1937 /* we've found our segment, start reading the different contents in here */
1939 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1942 } else if (matroska->level_up) {
1943 matroska->level_up--;
1949 case MATROSKA_ID_INFO: {
1950 if ((res = ebml_read_master(matroska, &id)) < 0)
1952 res = matroska_parse_info(matroska);
1956 /* track info headers */
1957 case MATROSKA_ID_TRACKS: {
1958 if ((res = ebml_read_master(matroska, &id)) < 0)
1960 res = matroska_parse_tracks(matroska);
1965 case MATROSKA_ID_CUES: {
1966 if (!matroska->index_parsed) {
1967 if ((res = ebml_read_master(matroska, &id)) < 0)
1969 res = matroska_parse_index(matroska);
1971 res = ebml_read_skip(matroska);
1976 case MATROSKA_ID_TAGS: {
1977 if (!matroska->metadata_parsed) {
1978 if ((res = ebml_read_master(matroska, &id)) < 0)
1980 res = matroska_parse_metadata(matroska);
1982 res = ebml_read_skip(matroska);
1986 /* file index (if seekable, seek to Cues/Tags to parse it) */
1987 case MATROSKA_ID_SEEKHEAD: {
1988 if ((res = ebml_read_master(matroska, &id)) < 0)
1990 res = matroska_parse_seekhead(matroska);
1994 case MATROSKA_ID_CLUSTER: {
1995 /* Do not read the master - this will be done in the next
1996 * call to matroska_read_packet. */
2002 av_log(matroska->ctx, AV_LOG_INFO,
2003 "Unknown matroska file header ID 0x%x\n", id);
2007 res = ebml_read_skip(matroska);
2011 if (matroska->level_up) {
2012 matroska->level_up--;
2017 /* Have we found a cluster? */
2018 if (ebml_peek_id(matroska, NULL) == MATROSKA_ID_CLUSTER) {
2020 MatroskaTrack *track;
2023 for (i = 0; i < matroska->num_tracks; i++) {
2024 enum CodecID codec_id = CODEC_ID_NONE;
2025 uint8_t *extradata = NULL;
2026 int extradata_size = 0;
2027 int extradata_offset = 0;
2028 track = matroska->tracks[i];
2029 track->stream_index = -1;
2031 /* Apply some sanity checks. */
2032 if (track->codec_id == NULL)
2035 for(j=0; ff_mkv_codec_tags[j].str; j++){
2036 if(!strncmp(ff_mkv_codec_tags[j].str, track->codec_id,
2037 strlen(ff_mkv_codec_tags[j].str))){
2038 codec_id= ff_mkv_codec_tags[j].id;
2043 /* Set the FourCC from the CodecID. */
2044 /* This is the MS compatibility mode which stores a
2045 * BITMAPINFOHEADER in the CodecPrivate. */
2046 if (!strcmp(track->codec_id,
2047 MATROSKA_CODEC_ID_VIDEO_VFW_FOURCC) &&
2048 (track->codec_priv_size >= 40) &&
2049 (track->codec_priv != NULL)) {
2050 MatroskaVideoTrack *vtrack = (MatroskaVideoTrack *) track;
2052 /* Offset of biCompression. Stored in LE. */
2053 vtrack->fourcc = AV_RL32(track->codec_priv + 16);
2054 codec_id = codec_get_id(codec_bmp_tags, vtrack->fourcc);
2058 /* This is the MS compatibility mode which stores a
2059 * WAVEFORMATEX in the CodecPrivate. */
2060 else if (!strcmp(track->codec_id,
2061 MATROSKA_CODEC_ID_AUDIO_ACM) &&
2062 (track->codec_priv_size >= 18) &&
2063 (track->codec_priv != NULL)) {
2066 /* Offset of wFormatTag. Stored in LE. */
2067 tag = AV_RL16(track->codec_priv);
2068 codec_id = codec_get_id(codec_wav_tags, tag);
2072 else if (codec_id == CODEC_ID_AAC && !track->codec_priv_size) {
2073 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *) track;
2074 int profile = matroska_aac_profile(track->codec_id);
2075 int sri = matroska_aac_sri(audiotrack->internal_samplerate);
2076 extradata = av_malloc(5);
2077 if (extradata == NULL)
2078 return AVERROR(ENOMEM);
2079 extradata[0] = (profile << 3) | ((sri&0x0E) >> 1);
2080 extradata[1] = ((sri&0x01) << 7) | (audiotrack->channels<<3);
2081 if (strstr(track->codec_id, "SBR")) {
2082 sri = matroska_aac_sri(audiotrack->samplerate);
2083 extradata[2] = 0x56;
2084 extradata[3] = 0xE5;
2085 extradata[4] = 0x80 | (sri<<3);
2092 else if (codec_id == CODEC_ID_TTA) {
2093 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *) track;
2095 extradata_size = 30;
2096 extradata = av_mallocz(extradata_size);
2097 if (extradata == NULL)
2098 return AVERROR(ENOMEM);
2099 init_put_byte(&b, extradata, extradata_size, 1,
2100 NULL, NULL, NULL, NULL);
2101 put_buffer(&b, (uint8_t *) "TTA1", 4);
2103 put_le16(&b, audiotrack->channels);
2104 put_le16(&b, audiotrack->bitdepth);
2105 put_le32(&b, audiotrack->samplerate);
2106 put_le32(&b, matroska->ctx->duration * audiotrack->samplerate);
2109 else if (codec_id == CODEC_ID_RV10 || codec_id == CODEC_ID_RV20 ||
2110 codec_id == CODEC_ID_RV30 || codec_id == CODEC_ID_RV40) {
2111 extradata_offset = 26;
2112 track->codec_priv_size -= extradata_offset;
2115 else if (codec_id == CODEC_ID_RA_144) {
2116 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
2117 audiotrack->samplerate = 8000;
2118 audiotrack->channels = 1;
2121 else if (codec_id == CODEC_ID_RA_288 ||
2122 codec_id == CODEC_ID_COOK ||
2123 codec_id == CODEC_ID_ATRAC3) {
2124 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
2127 init_put_byte(&b, track->codec_priv, track->codec_priv_size, 0,
2128 NULL, NULL, NULL, NULL);
2130 audiotrack->coded_framesize = get_be32(&b);
2132 audiotrack->sub_packet_h = get_be16(&b);
2133 audiotrack->frame_size = get_be16(&b);
2134 audiotrack->sub_packet_size = get_be16(&b);
2135 audiotrack->buf = av_malloc(audiotrack->frame_size * audiotrack->sub_packet_h);
2136 if (codec_id == CODEC_ID_RA_288) {
2137 audiotrack->block_align = audiotrack->coded_framesize;
2138 track->codec_priv_size = 0;
2140 audiotrack->block_align = audiotrack->sub_packet_size;
2141 extradata_offset = 78;
2142 track->codec_priv_size -= extradata_offset;
2146 else if (codec_id == CODEC_ID_TEXT) {
2147 MatroskaSubtitleTrack *subtrack=(MatroskaSubtitleTrack *)track;
2148 if (!strcmp(track->codec_id, "S_TEXT/ASS") ||
2149 !strcmp(track->codec_id, "S_TEXT/SSA") ||
2150 !strcmp(track->codec_id, "S_ASS") ||
2151 !strcmp(track->codec_id, "S_SSA"))
2155 if (codec_id == CODEC_ID_NONE) {
2156 av_log(matroska->ctx, AV_LOG_INFO,
2157 "Unknown/unsupported CodecID %s.\n",
2161 track->stream_index = matroska->num_streams;
2163 matroska->num_streams++;
2164 st = av_new_stream(s, track->stream_index);
2166 return AVERROR(ENOMEM);
2167 av_set_pts_info(st, 64, matroska->time_scale, 1000*1000*1000); /* 64 bit pts in ns */
2169 st->codec->codec_id = codec_id;
2171 if (strcmp(track->language, "und"))
2172 strcpy(st->language, track->language);
2174 if (track->default_duration)
2175 av_reduce(&st->codec->time_base.num, &st->codec->time_base.den,
2176 track->default_duration, 1000000000, 30000);
2179 st->codec->extradata = extradata;
2180 st->codec->extradata_size = extradata_size;
2181 } else if(track->codec_priv && track->codec_priv_size > 0){
2182 st->codec->extradata = av_malloc(track->codec_priv_size);
2183 if(st->codec->extradata == NULL)
2184 return AVERROR(ENOMEM);
2185 st->codec->extradata_size = track->codec_priv_size;
2186 memcpy(st->codec->extradata,track->codec_priv+extradata_offset,
2187 track->codec_priv_size);
2190 if (track->type == MATROSKA_TRACK_TYPE_VIDEO) {
2191 MatroskaVideoTrack *videotrack = (MatroskaVideoTrack *)track;
2193 st->codec->codec_type = CODEC_TYPE_VIDEO;
2194 st->codec->codec_tag = videotrack->fourcc;
2195 st->codec->width = videotrack->pixel_width;
2196 st->codec->height = videotrack->pixel_height;
2197 if (videotrack->display_width == 0)
2198 videotrack->display_width= videotrack->pixel_width;
2199 if (videotrack->display_height == 0)
2200 videotrack->display_height= videotrack->pixel_height;
2201 av_reduce(&st->codec->sample_aspect_ratio.num,
2202 &st->codec->sample_aspect_ratio.den,
2203 st->codec->height * videotrack->display_width,
2204 st->codec-> width * videotrack->display_height,
2206 st->need_parsing = AVSTREAM_PARSE_HEADERS;
2207 } else if (track->type == MATROSKA_TRACK_TYPE_AUDIO) {
2208 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
2210 st->codec->codec_type = CODEC_TYPE_AUDIO;
2211 st->codec->sample_rate = audiotrack->samplerate;
2212 st->codec->channels = audiotrack->channels;
2213 st->codec->block_align = audiotrack->block_align;
2214 } else if (track->type == MATROSKA_TRACK_TYPE_SUBTITLE) {
2215 st->codec->codec_type = CODEC_TYPE_SUBTITLE;
2218 /* What do we do with private data? E.g. for Vorbis. */
2223 if (matroska->index_parsed) {
2224 int i, track, stream;
2225 for (i=0; i<matroska->num_indexes; i++) {
2226 MatroskaDemuxIndex *idx = &matroska->index[i];
2227 track = matroska_find_track_by_num(matroska, idx->track);
2228 stream = matroska->tracks[track]->stream_index;
2230 av_add_index_entry(matroska->ctx->streams[stream],
2231 idx->pos, idx->time/matroska->time_scale,
2232 0, 0, AVINDEX_KEYFRAME);
2240 matroska_parse_block(MatroskaDemuxContext *matroska, uint8_t *data, int size,
2241 int64_t pos, uint64_t cluster_time, uint64_t duration,
2242 int is_keyframe, int is_bframe)
2248 uint8_t *origdata = data;
2250 uint32_t *lace_size = NULL;
2251 int n, flags, laces = 0;
2254 /* first byte(s): tracknum */
2255 if ((n = matroska_ebmlnum_uint(data, size, &num)) < 0) {
2256 av_log(matroska->ctx, AV_LOG_ERROR, "EBML block data error\n");
2263 /* fetch track from num */
2264 track = matroska_find_track_by_num(matroska, num);
2265 if (size <= 3 || track < 0 || track >= matroska->num_tracks) {
2266 av_log(matroska->ctx, AV_LOG_INFO,
2267 "Invalid stream %d or size %u\n", track, size);
2271 if (matroska->tracks[track]->stream_index < 0)
2273 st = matroska->ctx->streams[matroska->tracks[track]->stream_index];
2274 if (st->discard >= AVDISCARD_ALL) {
2278 if (duration == AV_NOPTS_VALUE)
2279 duration = matroska->tracks[track]->default_duration / matroska->time_scale;
2281 /* block_time (relative to cluster time) */
2282 block_time = AV_RB16(data);
2286 if (is_keyframe == -1)
2287 is_keyframe = flags & 0x80 ? PKT_FLAG_KEY : 0;
2289 if (matroska->skip_to_keyframe) {
2290 if (!is_keyframe || st != matroska->skip_to_stream)
2292 matroska->skip_to_keyframe = 0;
2295 switch ((flags & 0x06) >> 1) {
2296 case 0x0: /* no lacing */
2298 lace_size = av_mallocz(sizeof(int));
2299 lace_size[0] = size;
2302 case 0x1: /* xiph lacing */
2303 case 0x2: /* fixed-size lacing */
2304 case 0x3: /* EBML lacing */
2309 laces = (*data) + 1;
2312 lace_size = av_mallocz(laces * sizeof(int));
2314 switch ((flags & 0x06) >> 1) {
2315 case 0x1: /* xiph lacing */ {
2318 for (n = 0; res == 0 && n < laces - 1; n++) {
2325 lace_size[n] += temp;
2331 total += lace_size[n];
2333 lace_size[n] = size - total;
2337 case 0x2: /* fixed-size lacing */
2338 for (n = 0; n < laces; n++)
2339 lace_size[n] = size / laces;
2342 case 0x3: /* EBML lacing */ {
2344 n = matroska_ebmlnum_uint(data, size, &num);
2346 av_log(matroska->ctx, AV_LOG_INFO,
2347 "EBML block data error\n");
2352 total = lace_size[0] = num;
2353 for (n = 1; res == 0 && n < laces - 1; n++) {
2356 r = matroska_ebmlnum_sint (data, size, &snum);
2358 av_log(matroska->ctx, AV_LOG_INFO,
2359 "EBML block data error\n");
2364 lace_size[n] = lace_size[n - 1] + snum;
2365 total += lace_size[n];
2367 lace_size[n] = size - total;
2375 uint64_t timecode = AV_NOPTS_VALUE;
2377 if (cluster_time != (uint64_t)-1
2378 && (block_time >= 0 || cluster_time >= -block_time))
2379 timecode = cluster_time + block_time;
2381 for (n = 0; n < laces; n++) {
2382 if (st->codec->codec_id == CODEC_ID_RA_288 ||
2383 st->codec->codec_id == CODEC_ID_COOK ||
2384 st->codec->codec_id == CODEC_ID_ATRAC3) {
2385 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)matroska->tracks[track];
2386 int a = st->codec->block_align;
2387 int sps = audiotrack->sub_packet_size;
2388 int cfs = audiotrack->coded_framesize;
2389 int h = audiotrack->sub_packet_h;
2390 int y = audiotrack->sub_packet_cnt;
2391 int w = audiotrack->frame_size;
2394 if (!audiotrack->pkt_cnt) {
2395 if (st->codec->codec_id == CODEC_ID_RA_288)
2396 for (x=0; x<h/2; x++)
2397 memcpy(audiotrack->buf+x*2*w+y*cfs,
2400 for (x=0; x<w/sps; x++)
2401 memcpy(audiotrack->buf+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), data+x*sps, sps);
2403 if (++audiotrack->sub_packet_cnt >= h) {
2404 audiotrack->sub_packet_cnt = 0;
2405 audiotrack->pkt_cnt = h*w / a;
2408 while (audiotrack->pkt_cnt) {
2409 pkt = av_mallocz(sizeof(AVPacket));
2410 av_new_packet(pkt, a);
2411 memcpy(pkt->data, audiotrack->buf
2412 + a * (h*w / a - audiotrack->pkt_cnt--), a);
2414 pkt->stream_index = matroska->tracks[track]->stream_index;
2415 matroska_queue_packet(matroska, pkt);
2420 if (st->codec->codec_id == CODEC_ID_TEXT
2421 && ((MatroskaSubtitleTrack *)(matroska->tracks[track]))->ass) {
2423 for (i=0; i<8 && data[offset]; offset++)
2424 if (data[offset] == ',')
2428 pkt = av_mallocz(sizeof(AVPacket));
2429 /* XXX: prevent data copy... */
2430 if (av_new_packet(pkt, lace_size[n]-offset) < 0) {
2431 res = AVERROR(ENOMEM);
2435 memcpy (pkt->data, data+offset, lace_size[n]-offset);
2438 pkt->flags = is_keyframe;
2439 pkt->stream_index = matroska->tracks[track]->stream_index;
2441 pkt->pts = timecode;
2443 pkt->duration = duration;
2445 matroska_queue_packet(matroska, pkt);
2448 if (timecode != AV_NOPTS_VALUE)
2449 timecode = duration ? timecode + duration : AV_NOPTS_VALUE;
2450 data += lace_size[n];
2460 matroska_parse_blockgroup (MatroskaDemuxContext *matroska,
2461 uint64_t cluster_time)
2466 int is_keyframe = PKT_FLAG_KEY, last_num_packets = matroska->num_packets;
2467 uint64_t duration = AV_NOPTS_VALUE;
2472 av_log(matroska->ctx, AV_LOG_DEBUG, "parsing blockgroup...\n");
2475 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2478 } else if (matroska->level_up) {
2479 matroska->level_up--;
2484 /* one block inside the group. Note, block parsing is one
2485 * of the harder things, so this code is a bit complicated.
2486 * See http://www.matroska.org/ for documentation. */
2487 case MATROSKA_ID_BLOCK: {
2488 pos = url_ftell(matroska->ctx->pb);
2489 res = ebml_read_binary(matroska, &id, &data, &size);
2493 case MATROSKA_ID_BLOCKDURATION: {
2494 if ((res = ebml_read_uint(matroska, &id, &duration)) < 0)
2499 case MATROSKA_ID_BLOCKREFERENCE: {
2501 /* We've found a reference, so not even the first frame in
2502 * the lace is a key frame. */
2504 if (last_num_packets != matroska->num_packets)
2505 matroska->packets[last_num_packets]->flags = 0;
2506 if ((res = ebml_read_sint(matroska, &id, &num)) < 0)
2514 av_log(matroska->ctx, AV_LOG_INFO,
2515 "Unknown entry 0x%x in blockgroup data\n", id);
2519 res = ebml_read_skip(matroska);
2523 if (matroska->level_up) {
2524 matroska->level_up--;
2533 res = matroska_parse_block(matroska, data, size, pos, cluster_time,
2534 duration, is_keyframe, is_bframe);
2540 matroska_parse_cluster (MatroskaDemuxContext *matroska)
2544 uint64_t cluster_time = 0;
2549 av_log(matroska->ctx, AV_LOG_DEBUG,
2550 "parsing cluster at %"PRId64"\n", url_ftell(matroska->ctx->pb));
2553 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2556 } else if (matroska->level_up) {
2557 matroska->level_up--;
2562 /* cluster timecode */
2563 case MATROSKA_ID_CLUSTERTIMECODE: {
2565 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
2571 /* a group of blocks inside a cluster */
2572 case MATROSKA_ID_BLOCKGROUP:
2573 if ((res = ebml_read_master(matroska, &id)) < 0)
2575 res = matroska_parse_blockgroup(matroska, cluster_time);
2578 case MATROSKA_ID_SIMPLEBLOCK:
2579 pos = url_ftell(matroska->ctx->pb);
2580 res = ebml_read_binary(matroska, &id, &data, &size);
2582 res = matroska_parse_block(matroska, data, size, pos,
2583 cluster_time, AV_NOPTS_VALUE,
2588 av_log(matroska->ctx, AV_LOG_INFO,
2589 "Unknown entry 0x%x in cluster data\n", id);
2593 res = ebml_read_skip(matroska);
2597 if (matroska->level_up) {
2598 matroska->level_up--;
2607 matroska_read_packet (AVFormatContext *s,
2610 MatroskaDemuxContext *matroska = s->priv_data;
2614 /* Read stream until we have a packet queued. */
2615 while (matroska_deliver_packet(matroska, pkt)) {
2617 /* Have we already reached the end? */
2619 return AVERROR(EIO);
2623 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2624 return AVERROR(EIO);
2625 } else if (matroska->level_up) {
2626 matroska->level_up--;
2631 case MATROSKA_ID_CLUSTER:
2632 if ((res = ebml_read_master(matroska, &id)) < 0)
2634 if ((res = matroska_parse_cluster(matroska)) == 0)
2635 res = 1; /* Parsed one cluster, let's get out. */
2640 res = ebml_read_skip(matroska);
2644 if (matroska->level_up) {
2645 matroska->level_up--;
2658 matroska_read_seek (AVFormatContext *s, int stream_index, int64_t timestamp,
2661 MatroskaDemuxContext *matroska = s->priv_data;
2662 AVStream *st = s->streams[stream_index];
2665 /* find index entry */
2666 index = av_index_search_timestamp(st, timestamp, flags);
2671 url_fseek(s->pb, st->index_entries[index].pos, SEEK_SET);
2672 matroska->skip_to_keyframe = !(flags & AVSEEK_FLAG_ANY);
2673 matroska->skip_to_stream = st;
2674 matroska->num_packets = 0;
2675 matroska->peek_id = 0;
2680 matroska_read_close (AVFormatContext *s)
2682 MatroskaDemuxContext *matroska = s->priv_data;
2685 av_free(matroska->writing_app);
2686 av_free(matroska->muxing_app);
2687 av_free(matroska->index);
2689 if (matroska->packets != NULL) {
2690 for (n = 0; n < matroska->num_packets; n++) {
2691 av_free_packet(matroska->packets[n]);
2692 av_free(matroska->packets[n]);
2694 av_free(matroska->packets);
2697 for (n = 0; n < matroska->num_tracks; n++) {
2698 MatroskaTrack *track = matroska->tracks[n];
2699 av_free(track->codec_id);
2700 av_free(track->codec_name);
2701 av_free(track->codec_priv);
2702 av_free(track->name);
2704 if (track->type == MATROSKA_TRACK_TYPE_AUDIO) {
2705 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
2706 av_free(audiotrack->buf);
2715 AVInputFormat matroska_demuxer = {
2717 "Matroska file format",
2718 sizeof(MatroskaDemuxContext),
2720 matroska_read_header,
2721 matroska_read_packet,
2722 matroska_read_close,