]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/matroskadec.c
matroskadec: use generic parser to parse chapters
[ffmpeg] / libavformat / matroskadec.c
index 40dcc9c79d230e19a3f5e25b0d11912e5b3f3109..8d2f9c2270f6d0b6c47cc79cf7123bffed66e84d 100644 (file)
 #include "avformat.h"
 /* For codec_get_id(). */
 #include "riff.h"
+#include "isom.h"
 #include "matroska.h"
 #include "libavcodec/mpeg4audio.h"
 #include "libavutil/intfloat_readwrite.h"
+#include "libavutil/avstring.h"
 #include "libavutil/lzo.h"
 #ifdef CONFIG_ZLIB
 #include <zlib.h>
 #endif
+#ifdef CONFIG_BZLIB
+#include <bzlib.h>
+#endif
+
+typedef enum {
+    EBML_NONE,
+    EBML_UINT,
+    EBML_FLOAT,
+    EBML_STR,
+    EBML_UTF8,
+    EBML_BIN,
+    EBML_NEST,
+    EBML_PASS,
+    EBML_STOP,
+} EbmlType;
+
+typedef const struct EbmlSyntax {
+    uint32_t id;
+    EbmlType type;
+    int list_elem_size;
+    int data_offset;
+    union {
+        uint64_t    u;
+        double      f;
+        const char *s;
+        const struct EbmlSyntax *n;
+    } def;
+} EbmlSyntax;
+
+typedef struct {
+    int nb_elem;
+    void *elem;
+} EbmlList;
+
+typedef struct {
+    int      size;
+    uint8_t *data;
+    int64_t  pos;
+} EbmlBin;
+
+typedef struct {
+    uint64_t version;
+    uint64_t max_size;
+    uint64_t id_length;
+    char    *doctype;
+    uint64_t doctype_version;
+} Ebml;
 
 typedef struct Track {
     MatroskaTrackType type;
@@ -46,24 +95,25 @@ typedef struct Track {
      * the calling app uses for this track. */
     uint32_t num;
     uint32_t uid;
-    int stream_index;
 
     char *name;
     char language[4];
 
     char *codec_id;
-    char *codec_name;
 
     unsigned char *codec_priv;
     int codec_priv_size;
 
+    double time_scale;
     uint64_t default_duration;
-    MatroskaTrackFlags flags;
+    uint64_t flag_default;
 
     int encoding_scope;
     MatroskaTrackEncodingCompAlgo encoding_algo;
     uint8_t *encoding_settings;
     int encoding_settings_len;
+
+    AVStream *stream;
 } MatroskaTrack;
 
 typedef struct MatroskaVideoTrack {
@@ -76,9 +126,6 @@ typedef struct MatroskaVideoTrack {
 
     uint32_t fourcc;
 
-    MatroskaAspectRatioMode ar_mode;
-    MatroskaEyeMode eye_mode;
-
     //..
 } MatroskaVideoTrack;
 
@@ -111,17 +158,28 @@ typedef struct MatroskaSubtitleTrack {
                                     sizeof(MatroskaAudioTrack), \
                                     sizeof(MatroskaSubtitleTrack)))
 
+typedef struct {
+    uint64_t start;
+    uint64_t end;
+    uint64_t uid;
+    char    *title;
+} MatroskaChapter;
+
+typedef struct {
+    uint64_t track;
+    uint64_t pos;
+} MatroskaIndexPos;
+
+typedef struct {
+    uint64_t time;
+    EbmlList pos;
+} MatroskaIndex;
+
 typedef struct MatroskaLevel {
     uint64_t start;
     uint64_t length;
 } MatroskaLevel;
 
-typedef struct MatroskaDemuxIndex {
-  uint64_t        pos;   /* of the corresponding *cluster*! */
-  uint16_t        track; /* reference to 'num' */
-  uint64_t        time;  /* in nanoseconds */
-} MatroskaDemuxIndex;
-
 typedef struct MatroskaDemuxContext {
     AVFormatContext *ctx;
 
@@ -130,13 +188,10 @@ typedef struct MatroskaDemuxContext {
     MatroskaLevel levels[EBML_MAX_DEPTH];
     int level_up;
 
-    /* matroska stuff */
-    char *writing_app;
-    char *muxing_app;
-    int64_t created;
-
     /* timescale in the file */
     int64_t time_scale;
+    EbmlList chapters;
+    EbmlList index;
 
     /* num_streams is the number of streams that av_new_stream() was called
      * for ( = that are available to the calling program). */
@@ -159,15 +214,86 @@ typedef struct MatroskaDemuxContext {
     int index_parsed;
     int done;
 
-    /* The index for seeking. */
-    int num_indexes;
-    MatroskaDemuxIndex *index;
-
     /* What to skip before effectively reading a packet. */
     int skip_to_keyframe;
     AVStream *skip_to_stream;
 } MatroskaDemuxContext;
 
+#define ARRAY_SIZE(x)  (sizeof(x)/sizeof(*x))
+
+static EbmlSyntax ebml_header[] = {
+    { EBML_ID_EBMLREADVERSION,        EBML_UINT, 0, offsetof(Ebml,version), {.u=EBML_VERSION} },
+    { EBML_ID_EBMLMAXSIZELENGTH,      EBML_UINT, 0, offsetof(Ebml,max_size), {.u=8} },
+    { EBML_ID_EBMLMAXIDLENGTH,        EBML_UINT, 0, offsetof(Ebml,id_length), {.u=4} },
+    { EBML_ID_DOCTYPE,                EBML_STR,  0, offsetof(Ebml,doctype), {.s="(none)"} },
+    { EBML_ID_DOCTYPEREADVERSION,     EBML_UINT, 0, offsetof(Ebml,doctype_version), {.u=1} },
+    { EBML_ID_EBMLVERSION,            EBML_NONE },
+    { EBML_ID_DOCTYPEVERSION,         EBML_NONE },
+    { EBML_ID_VOID,                   EBML_NONE },
+    { 0 }
+};
+
+static EbmlSyntax ebml_syntax[] = {
+    { EBML_ID_HEADER,                 EBML_NEST, 0, 0, {.n=ebml_header} },
+    { 0 }
+};
+
+static EbmlSyntax matroska_chapter_display[] = {
+    { MATROSKA_ID_CHAPSTRING,         EBML_UTF8, 0, offsetof(MatroskaChapter,title) },
+    { EBML_ID_VOID,                   EBML_NONE },
+    { 0 }
+};
+
+static EbmlSyntax matroska_chapter_entry[] = {
+    { MATROSKA_ID_CHAPTERTIMESTART,   EBML_UINT, 0, offsetof(MatroskaChapter,start), {.u=AV_NOPTS_VALUE} },
+    { MATROSKA_ID_CHAPTERTIMEEND,     EBML_UINT, 0, offsetof(MatroskaChapter,end), {.u=AV_NOPTS_VALUE} },
+    { MATROSKA_ID_CHAPTERUID,         EBML_UINT, 0, offsetof(MatroskaChapter,uid) },
+    { MATROSKA_ID_CHAPTERDISPLAY,     EBML_NEST, 0, 0, {.n=matroska_chapter_display} },
+    { MATROSKA_ID_CHAPTERFLAGHIDDEN,  EBML_NONE },
+    { EBML_ID_VOID,                   EBML_NONE },
+    { 0 }
+};
+
+static EbmlSyntax matroska_chapter[] = {
+    { MATROSKA_ID_CHAPTERATOM,        EBML_NEST, sizeof(MatroskaChapter), offsetof(MatroskaDemuxContext,chapters), {.n=matroska_chapter_entry} },
+    { MATROSKA_ID_EDITIONUID,         EBML_NONE },
+    { MATROSKA_ID_EDITIONFLAGHIDDEN,  EBML_NONE },
+    { MATROSKA_ID_EDITIONFLAGDEFAULT, EBML_NONE },
+    { EBML_ID_VOID,                   EBML_NONE },
+    { 0 }
+};
+
+static EbmlSyntax matroska_chapters[] = {
+    { MATROSKA_ID_EDITIONENTRY,       EBML_NEST, 0, 0, {.n=matroska_chapter} },
+    { EBML_ID_VOID,                   EBML_NONE },
+    { 0 }
+};
+
+static EbmlSyntax matroska_index_pos[] = {
+    { MATROSKA_ID_CUETRACK,           EBML_UINT, 0, offsetof(MatroskaIndexPos,track) },
+    { MATROSKA_ID_CUECLUSTERPOSITION, EBML_UINT, 0, offsetof(MatroskaIndexPos,pos)   },
+    { EBML_ID_VOID,                   EBML_NONE },
+    { 0 }
+};
+
+static EbmlSyntax matroska_index_entry[] = {
+    { MATROSKA_ID_CUETIME,            EBML_UINT, 0, offsetof(MatroskaIndex,time) },
+    { MATROSKA_ID_CUETRACKPOSITION,   EBML_NEST, sizeof(MatroskaIndexPos), offsetof(MatroskaIndex,pos), {.n=matroska_index_pos} },
+    { EBML_ID_VOID,                   EBML_NONE },
+    { 0 }
+};
+
+static EbmlSyntax matroska_index[] = {
+    { MATROSKA_ID_POINTENTRY,         EBML_NEST, sizeof(MatroskaIndex), offsetof(MatroskaDemuxContext,index), {.n=matroska_index_entry} },
+    { EBML_ID_VOID,                   EBML_NONE },
+    { 0 }
+};
+
+static EbmlSyntax matroska_tags[] = {
+    { EBML_ID_VOID,                   EBML_NONE },
+    { 0 }
+};
+
 /*
  * The first few functions handle EBML file parsing. The rest
  * is the document interpretation. Matroska really just is a
@@ -499,6 +625,7 @@ ebml_read_ascii (MatroskaDemuxContext *matroska,
         offset_t pos = url_ftell(pb);
         av_log(matroska->ctx, AV_LOG_ERROR,
                "Read error at pos. %"PRIu64" (0x%"PRIx64")\n", pos, pos);
+        av_free(*str);
         return AVERROR(EIO);
     }
     (*str)[size] = '\0';
@@ -519,19 +646,6 @@ ebml_read_utf8 (MatroskaDemuxContext *matroska,
   return ebml_read_ascii(matroska, id, str);
 }
 
-/*
- * Read the next element as a date (nanoseconds since 1/1/2000).
- * 0 is success, < 0 is failure.
- */
-
-static int
-ebml_read_date (MatroskaDemuxContext *matroska,
-                uint32_t             *id,
-                int64_t              *date)
-{
-  return ebml_read_sint(matroska, id, date);
-}
-
 /*
  * Read the next element, but only the header. The contents
  * are supposed to be sub-elements which can be read separately.
@@ -671,132 +785,8 @@ matroska_ebmlnum_sint (uint8_t  *data,
     return res;
 }
 
-/*
- * Read an EBML header.
- * 0 is success, < 0 is failure.
- */
-
-static int
-ebml_read_header (MatroskaDemuxContext *matroska,
-                  char                **doctype,
-                  int                  *version)
-{
-    uint32_t id;
-    int level_up, res = 0;
-
-    /* default init */
-    if (doctype)
-        *doctype = NULL;
-    if (version)
-        *version = 1;
-
-    if (!(id = ebml_peek_id(matroska, &level_up)) ||
-        level_up != 0 || id != EBML_ID_HEADER) {
-        av_log(matroska->ctx, AV_LOG_ERROR,
-               "This is not an EBML file (id=0x%x/0x%x)\n", id, EBML_ID_HEADER);
-        return AVERROR_INVALIDDATA;
-    }
-    if ((res = ebml_read_master(matroska, &id)) < 0)
-        return res;
-
-    while (res == 0) {
-        if (!(id = ebml_peek_id(matroska, &level_up)))
-            return AVERROR(EIO);
-
-        /* end-of-header */
-        if (level_up)
-            break;
-
-        switch (id) {
-            /* is our read version uptodate? */
-            case EBML_ID_EBMLREADVERSION: {
-                uint64_t num;
-
-                if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
-                    return res;
-                if (num > EBML_VERSION) {
-                    av_log(matroska->ctx, AV_LOG_ERROR,
-                           "EBML version %"PRIu64" (> %d) is not supported\n",
-                           num, EBML_VERSION);
-                    return AVERROR_INVALIDDATA;
-                }
-                break;
-            }
-
-            /* we only handle 8 byte lengths at max */
-            case EBML_ID_EBMLMAXSIZELENGTH: {
-                uint64_t num;
-
-                if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
-                    return res;
-                if (num > sizeof(uint64_t)) {
-                    av_log(matroska->ctx, AV_LOG_ERROR,
-                           "Integers of size %"PRIu64" (> %zd) not supported\n",
-                           num, sizeof(uint64_t));
-                    return AVERROR_INVALIDDATA;
-                }
-                break;
-            }
-
-            /* we handle 4 byte IDs at max */
-            case EBML_ID_EBMLMAXIDLENGTH: {
-                uint64_t num;
-
-                if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
-                    return res;
-                if (num > sizeof(uint32_t)) {
-                    av_log(matroska->ctx, AV_LOG_ERROR,
-                           "IDs of size %"PRIu64" (> %zu) not supported\n",
-                            num, sizeof(uint32_t));
-                    return AVERROR_INVALIDDATA;
-                }
-                break;
-            }
-
-            case EBML_ID_DOCTYPE: {
-                char *text;
-
-                if ((res = ebml_read_ascii(matroska, &id, &text)) < 0)
-                    return res;
-                if (doctype) {
-                    if (*doctype)
-                        av_free(*doctype);
-                    *doctype = text;
-                } else
-                    av_free(text);
-                break;
-            }
-
-            case EBML_ID_DOCTYPEREADVERSION: {
-                uint64_t num;
-
-                if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
-                    return res;
-                if (version)
-                    *version = num;
-                break;
-            }
-
-            default:
-                av_log(matroska->ctx, AV_LOG_INFO,
-                       "Unknown data type 0x%x in EBML header", id);
-                /* pass-through */
-
-            case EBML_ID_VOID:
-            /* we ignore these two, as they don't tell us anything we
-             * care about */
-            case EBML_ID_EBMLVERSION:
-            case EBML_ID_DOCTYPEVERSION:
-                res = ebml_read_skip (matroska);
-                break;
-        }
-    }
-
-    return 0;
-}
-
 
-static int
+static MatroskaTrack *
 matroska_find_track_by_num (MatroskaDemuxContext *matroska,
                             int                   num)
 {
@@ -804,9 +794,10 @@ matroska_find_track_by_num (MatroskaDemuxContext *matroska,
 
     for (i = 0; i < matroska->num_tracks; i++)
         if (matroska->tracks[i]->num == num)
-            return i;
+            return matroska->tracks[i];
 
-    return -1;
+    av_log(matroska->ctx, AV_LOG_ERROR, "Invalid track number %d\n", num);
+    return NULL;
 }
 
 
@@ -919,6 +910,133 @@ matroska_probe (AVProbeData *p)
  * From here on, it's all XML-style DTD stuff... Needs no comments.
  */
 
+static int ebml_parse(MatroskaDemuxContext *matroska, EbmlSyntax *syntax,
+                      void *data, uint32_t expected_id, int once);
+
+static int ebml_parse_elem(MatroskaDemuxContext *matroska,
+                           EbmlSyntax *syntax, void *data)
+{
+    uint32_t id = syntax->id;
+    EbmlBin *bin;
+    int res;
+
+    data = (char *)data + syntax->data_offset;
+    if (syntax->list_elem_size) {
+        EbmlList *list = data;
+        list->elem = av_realloc(list->elem, (list->nb_elem+1)*syntax->list_elem_size);
+        data = (char*)list->elem + list->nb_elem*syntax->list_elem_size;
+        memset(data, 0, syntax->list_elem_size);
+        list->nb_elem++;
+    }
+    bin = data;
+
+    switch (syntax->type) {
+    case EBML_UINT:  return ebml_read_uint (matroska, &id, data);
+    case EBML_FLOAT: return ebml_read_float(matroska, &id, data);
+    case EBML_STR:
+    case EBML_UTF8:  av_free(*(char **)data);
+                     return ebml_read_ascii(matroska, &id, data);
+    case EBML_BIN:   av_free(bin->data);
+                     bin->pos = url_ftell(matroska->ctx->pb);
+                     return ebml_read_binary(matroska, &id, &bin->data,
+                                                            &bin->size);
+    case EBML_NEST:  if ((res=ebml_read_master(matroska, &id)) < 0)
+                         return res;
+                     if (id == MATROSKA_ID_SEGMENT)
+                         matroska->segment_start = url_ftell(matroska->ctx->pb);
+                     return ebml_parse(matroska, syntax->def.n, data, 0, 0);
+    case EBML_PASS:  return ebml_parse(matroska, syntax->def.n, data, 0, 1);
+    case EBML_STOP:  *(int *)data = 1;      return 1;
+    default:         return ebml_read_skip(matroska);
+    }
+}
+
+static int ebml_parse_id(MatroskaDemuxContext *matroska, EbmlSyntax *syntax,
+                         uint32_t id, void *data)
+{
+    int i;
+    for (i=0; syntax[i].id; i++)
+        if (id == syntax[i].id)
+            break;
+    if (!syntax[i].id)
+        av_log(matroska->ctx, AV_LOG_INFO, "Unknown entry 0x%X\n", id);
+    return ebml_parse_elem(matroska, &syntax[i], data);
+}
+
+static int ebml_parse(MatroskaDemuxContext *matroska, EbmlSyntax *syntax,
+                      void *data, uint32_t expected_id, int once)
+{
+    int i, res = 0;
+    uint32_t id = 0;
+
+    for (i=0; syntax[i].id; i++)
+        switch (syntax[i].type) {
+        case EBML_UINT:
+            *(uint64_t *)((char *)data+syntax[i].data_offset) = syntax[i].def.u;
+            break;
+        case EBML_FLOAT:
+            *(double   *)((char *)data+syntax[i].data_offset) = syntax[i].def.f;
+            break;
+        case EBML_STR:
+        case EBML_UTF8:
+            *(char    **)((char *)data+syntax[i].data_offset) = av_strdup(syntax[i].def.s);
+            break;
+        }
+
+    if (expected_id) {
+        res = ebml_read_master(matroska, &id);
+        if (id != expected_id)
+            return AVERROR_INVALIDDATA;
+        if (id == MATROSKA_ID_SEGMENT)
+            matroska->segment_start = url_ftell(matroska->ctx->pb);
+    }
+
+    while (!res) {
+        if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
+            res = AVERROR(EIO);
+            break;
+        } else if (matroska->level_up) {
+            matroska->level_up--;
+            break;
+        }
+
+        res = ebml_parse_id(matroska, syntax, id, data);
+        if (once)
+            break;
+
+
+        if (matroska->level_up) {
+            matroska->level_up--;
+            break;
+        }
+    }
+
+    return res;
+}
+
+static void ebml_free(EbmlSyntax *syntax, void *data)
+{
+    int i, j;
+    for (i=0; syntax[i].id; i++) {
+        void *data_off = (char *)data + syntax[i].data_offset;
+        switch (syntax[i].type) {
+        case EBML_STR:
+        case EBML_UTF8:  av_freep(data_off);                      break;
+        case EBML_BIN:   av_freep(&((EbmlBin *)data_off)->data);  break;
+        case EBML_NEST:
+            if (syntax[i].list_elem_size) {
+                EbmlList *list = data_off;
+                char *ptr = list->elem;
+                for (j=0; j<list->nb_elem; j++, ptr+=syntax[i].list_elem_size)
+                    ebml_free(syntax[i].def.n, ptr);
+                av_free(list->elem);
+            } else
+                ebml_free(syntax[i].def.n, data_off);
+        default:  break;
+        }
+    }
+}
+
 static int
 matroska_parse_info (MatroskaDemuxContext *matroska)
 {
@@ -964,35 +1082,15 @@ matroska_parse_info (MatroskaDemuxContext *matroska)
                 break;
             }
 
-            case MATROSKA_ID_WRITINGAPP: {
-                char *text;
-                if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
-                    break;
-                matroska->writing_app = text;
-                break;
-            }
-
-            case MATROSKA_ID_MUXINGAPP: {
-                char *text;
-                if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
-                    break;
-                matroska->muxing_app = text;
-                break;
-            }
-
-            case MATROSKA_ID_DATEUTC: {
-                int64_t time;
-                if ((res = ebml_read_date(matroska, &id, &time)) < 0)
-                    break;
-                matroska->created = time;
-                break;
-            }
-
             default:
                 av_log(matroska->ctx, AV_LOG_INFO,
                        "Unknown entry 0x%x in info header\n", id);
                 /* fall-through */
 
+            case MATROSKA_ID_WRITINGAPP:
+            case MATROSKA_ID_MUXINGAPP:
+            case MATROSKA_ID_DATEUTC:
+            case MATROSKA_ID_SEGMENTUID:
             case EBML_ID_VOID:
                 res = ebml_read_skip(matroska);
                 break;
@@ -1007,6 +1105,82 @@ matroska_parse_info (MatroskaDemuxContext *matroska)
     return res;
 }
 
+static int
+matroska_decode_buffer(uint8_t** buf, int* buf_size, MatroskaTrack *track)
+{
+    uint8_t* data = *buf;
+    int isize = *buf_size;
+    uint8_t* pkt_data = NULL;
+    int pkt_size = isize;
+    int result = 0;
+    int olen;
+
+    switch (track->encoding_algo) {
+    case MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP:
+        return track->encoding_settings_len;
+    case MATROSKA_TRACK_ENCODING_COMP_LZO:
+        do {
+            olen = pkt_size *= 3;
+            pkt_data = av_realloc(pkt_data,
+                                  pkt_size+LZO_OUTPUT_PADDING);
+            result = lzo1x_decode(pkt_data, &olen, data, &isize);
+        } while (result==LZO_OUTPUT_FULL && pkt_size<10000000);
+        if (result)
+            goto failed;
+        pkt_size -= olen;
+        break;
+#ifdef CONFIG_ZLIB
+    case MATROSKA_TRACK_ENCODING_COMP_ZLIB: {
+        z_stream zstream = {0};
+        if (inflateInit(&zstream) != Z_OK)
+            return -1;
+        zstream.next_in = data;
+        zstream.avail_in = isize;
+        do {
+            pkt_size *= 3;
+            pkt_data = av_realloc(pkt_data, pkt_size);
+            zstream.avail_out = pkt_size - zstream.total_out;
+            zstream.next_out = pkt_data + zstream.total_out;
+            result = inflate(&zstream, Z_NO_FLUSH);
+        } while (result==Z_OK && pkt_size<10000000);
+        pkt_size = zstream.total_out;
+        inflateEnd(&zstream);
+        if (result != Z_STREAM_END)
+            goto failed;
+        break;
+    }
+#endif
+#ifdef CONFIG_BZLIB
+    case MATROSKA_TRACK_ENCODING_COMP_BZLIB: {
+        bz_stream bzstream = {0};
+        if (BZ2_bzDecompressInit(&bzstream, 0, 0) != BZ_OK)
+            return -1;
+        bzstream.next_in = data;
+        bzstream.avail_in = isize;
+        do {
+            pkt_size *= 3;
+            pkt_data = av_realloc(pkt_data, pkt_size);
+            bzstream.avail_out = pkt_size - bzstream.total_out_lo32;
+            bzstream.next_out = pkt_data + bzstream.total_out_lo32;
+            result = BZ2_bzDecompress(&bzstream);
+        } while (result==BZ_OK && pkt_size<10000000);
+        pkt_size = bzstream.total_out_lo32;
+        BZ2_bzDecompressEnd(&bzstream);
+        if (result != BZ_STREAM_END)
+            goto failed;
+        break;
+    }
+#endif
+    }
+
+    *buf = pkt_data;
+    *buf_size = pkt_size;
+    return 0;
+ failed:
+    av_free(pkt_data);
+    return -1;
+}
+
 static int
 matroska_add_stream (MatroskaDemuxContext *matroska)
 {
@@ -1014,17 +1188,17 @@ matroska_add_stream (MatroskaDemuxContext *matroska)
     uint32_t id;
     MatroskaTrack *track;
 
+    /* start with the master */
+    if ((res = ebml_read_master(matroska, &id)) < 0)
+        return res;
+
     av_log(matroska->ctx, AV_LOG_DEBUG, "parsing track, adding stream..,\n");
 
-    /* Allocate a generic track. As soon as we know its type we'll realloc. */
+    /* Allocate a generic track. */
     track = av_mallocz(MAX_TRACK_SIZE);
-    matroska->num_tracks++;
+    track->time_scale = 1.0;
     strcpy(track->language, "eng");
 
-    /* start with the master */
-    if ((res = ebml_read_master(matroska, &id)) < 0)
-        return res;
-
     /* try reading the trackentry headers */
     while (res == 0) {
         if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
@@ -1081,7 +1255,6 @@ matroska_add_stream (MatroskaDemuxContext *matroska)
                         track->type = MATROSKA_TRACK_TYPE_NONE;
                         break;
                 }
-                matroska->tracks[matroska->num_tracks - 1] = track;
                 break;
             }
 
@@ -1176,52 +1349,6 @@ matroska_add_stream (MatroskaDemuxContext *matroska)
                             if ((res = ebml_read_uint(matroska, &id,
                                                       &num)) < 0)
                                 break;
-                            if (num)
-                                track->flags |=
-                                    MATROSKA_VIDEOTRACK_INTERLACED;
-                            else
-                                track->flags &=
-                                    ~MATROSKA_VIDEOTRACK_INTERLACED;
-                            break;
-                        }
-
-                        /* stereo mode (whether the video has two streams,
-                         * where one is for the left eye and the other for
-                         * the right eye, which creates a 3D-like
-                         * effect) */
-                        case MATROSKA_ID_VIDEOSTEREOMODE: {
-                            uint64_t num;
-                            if ((res = ebml_read_uint(matroska, &id,
-                                                      &num)) < 0)
-                                break;
-                            if (num != MATROSKA_EYE_MODE_MONO &&
-                                num != MATROSKA_EYE_MODE_LEFT &&
-                                num != MATROSKA_EYE_MODE_RIGHT &&
-                                num != MATROSKA_EYE_MODE_BOTH) {
-                                av_log(matroska->ctx, AV_LOG_INFO,
-                                       "Ignoring unknown eye mode 0x%x\n",
-                                       (uint32_t) num);
-                                break;
-                            }
-                            videotrack->eye_mode = num;
-                            break;
-                        }
-
-                        /* aspect ratio behaviour */
-                        case MATROSKA_ID_VIDEOASPECTRATIO: {
-                            uint64_t num;
-                            if ((res = ebml_read_uint(matroska, &id,
-                                                      &num)) < 0)
-                                break;
-                            if (num != MATROSKA_ASPECT_RATIO_MODE_FREE &&
-                                num != MATROSKA_ASPECT_RATIO_MODE_KEEP &&
-                                num != MATROSKA_ASPECT_RATIO_MODE_FIXED) {
-                                av_log(matroska->ctx, AV_LOG_INFO,
-                                       "Ignoring unknown aspect ratio 0x%x\n",
-                                       (uint32_t) num);
-                                break;
-                            }
-                            videotrack->ar_mode = num;
                             break;
                         }
 
@@ -1242,6 +1369,8 @@ matroska_add_stream (MatroskaDemuxContext *matroska)
                                    "0x%x - ignoring\n", id);
                             /* pass-through */
 
+                        case MATROSKA_ID_VIDEOSTEREOMODE:
+                        case MATROSKA_ID_VIDEOASPECTRATIO:
                         case EBML_ID_VOID:
                             res = ebml_read_skip(matroska);
                             break;
@@ -1360,15 +1489,6 @@ matroska_add_stream (MatroskaDemuxContext *matroska)
                 break;
             }
 
-                /* name of the codec */
-            case MATROSKA_ID_CODECNAME: {
-                char *text;
-                if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
-                    break;
-                track->codec_name = text;
-                break;
-            }
-
                 /* name of this track */
             case MATROSKA_ID_TRACKNAME: {
                 char *text;
@@ -1396,10 +1516,6 @@ matroska_add_stream (MatroskaDemuxContext *matroska)
                 uint64_t num;
                 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
                     break;
-                if (num)
-                    track->flags |= MATROSKA_TRACK_ENABLED;
-                else
-                    track->flags &= ~MATROSKA_TRACK_ENABLED;
                 break;
             }
 
@@ -1408,10 +1524,7 @@ matroska_add_stream (MatroskaDemuxContext *matroska)
                 uint64_t num;
                 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
                     break;
-                if (num)
-                    track->flags |= MATROSKA_TRACK_DEFAULT;
-                else
-                    track->flags &= ~MATROSKA_TRACK_DEFAULT;
+                track->flag_default = num;
                 break;
             }
 
@@ -1421,10 +1534,6 @@ matroska_add_stream (MatroskaDemuxContext *matroska)
                 uint64_t num;
                 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
                     break;
-                if (num)
-                    track->flags |= MATROSKA_TRACK_LACING;
-                else
-                    track->flags &= ~MATROSKA_TRACK_LACING;
                 break;
             }
 
@@ -1505,10 +1614,13 @@ matroska_add_stream (MatroskaDemuxContext *matroska)
                                                     if (num != MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP &&
 #ifdef CONFIG_ZLIB
                                                         num != MATROSKA_TRACK_ENCODING_COMP_ZLIB &&
+#endif
+#ifdef CONFIG_BZLIB
+                                                        num != MATROSKA_TRACK_ENCODING_COMP_BZLIB &&
 #endif
                                                         num != MATROSKA_TRACK_ENCODING_COMP_LZO)
                                                         av_log(matroska->ctx, AV_LOG_ERROR,
-                                                               "Unsupported compression algo");
+                                                               "Unsupported compression algo\n");
                                                     track->encoding_algo = num;
                                                     break;
                                                 }
@@ -1582,6 +1694,14 @@ matroska_add_stream (MatroskaDemuxContext *matroska)
                 break;
             }
 
+            case MATROSKA_ID_TRACKTIMECODESCALE: {
+                double num;
+                if ((res = ebml_read_float(matroska, &id, &num)) < 0)
+                    break;
+                track->time_scale = num;
+                break;
+            }
+
             default:
                 av_log(matroska->ctx, AV_LOG_INFO,
                        "Unknown track header entry 0x%x - ignoring\n", id);
@@ -1589,6 +1709,9 @@ matroska_add_stream (MatroskaDemuxContext *matroska)
 
             case EBML_ID_VOID:
             /* we ignore these because they're nothing useful. */
+            case MATROSKA_ID_TRACKFLAGFORCED:
+            case MATROSKA_ID_CODECNAME:
+            case MATROSKA_ID_CODECDECODEALL:
             case MATROSKA_ID_CODECINFOURL:
             case MATROSKA_ID_CODECDOWNLOADURL:
             case MATROSKA_ID_TRACKMINCACHE:
@@ -1603,6 +1726,28 @@ matroska_add_stream (MatroskaDemuxContext *matroska)
         }
     }
 
+    if (track->codec_priv_size && track->encoding_scope & 2) {
+        uint8_t *orig_priv = track->codec_priv;
+        int offset = matroska_decode_buffer(&track->codec_priv,
+                                            &track->codec_priv_size, track);
+        if (offset > 0) {
+            track->codec_priv = av_malloc(track->codec_priv_size + offset);
+            memcpy(track->codec_priv, track->encoding_settings, offset);
+            memcpy(track->codec_priv+offset, orig_priv, track->codec_priv_size);
+            track->codec_priv_size += offset;
+            av_free(orig_priv);
+        } else if (!offset) {
+            av_free(orig_priv);
+        } else
+            av_log(matroska->ctx, AV_LOG_ERROR,
+                   "Failed to decode codec private data\n");
+    }
+
+    if (track->type && matroska->num_tracks < ARRAY_SIZE(matroska->tracks)) {
+        matroska->tracks[matroska->num_tracks++] = track;
+    } else {
+        av_free(track);
+    }
     return res;
 }
 
@@ -1651,195 +1796,13 @@ matroska_parse_tracks (MatroskaDemuxContext *matroska)
 static int
 matroska_parse_index (MatroskaDemuxContext *matroska)
 {
-    int res = 0;
-    uint32_t id;
-    MatroskaDemuxIndex idx;
-
-    av_log(matroska->ctx, AV_LOG_DEBUG, "parsing index...\n");
-
-    while (res == 0) {
-        if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
-            res = AVERROR(EIO);
-            break;
-        } else if (matroska->level_up) {
-            matroska->level_up--;
-            break;
-        }
-
-        switch (id) {
-            /* one single index entry ('point') */
-            case MATROSKA_ID_POINTENTRY:
-                if ((res = ebml_read_master(matroska, &id)) < 0)
-                    break;
-
-                /* in the end, we hope to fill one entry with a
-                 * timestamp, a file position and a tracknum */
-                idx.pos   = (uint64_t) -1;
-                idx.time  = (uint64_t) -1;
-                idx.track = (uint16_t) -1;
-
-                while (res == 0) {
-                    if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
-                        res = AVERROR(EIO);
-                        break;
-                    } else if (matroska->level_up) {
-                        matroska->level_up--;
-                        break;
-                    }
-
-                    switch (id) {
-                        /* one single index entry ('point') */
-                        case MATROSKA_ID_CUETIME: {
-                            uint64_t time;
-                            if ((res = ebml_read_uint(matroska, &id,
-                                                      &time)) < 0)
-                                break;
-                            idx.time = time * matroska->time_scale;
-                            break;
-                        }
-
-                        /* position in the file + track to which it
-                         * belongs */
-                        case MATROSKA_ID_CUETRACKPOSITION:
-                            if ((res = ebml_read_master(matroska, &id)) < 0)
-                                break;
-
-                            while (res == 0) {
-                                if (!(id = ebml_peek_id (matroska,
-                                                    &matroska->level_up))) {
-                                    res = AVERROR(EIO);
-                                    break;
-                                } else if (matroska->level_up) {
-                                    matroska->level_up--;
-                                    break;
-                                }
-
-                                switch (id) {
-                                    /* track number */
-                                    case MATROSKA_ID_CUETRACK: {
-                                        uint64_t num;
-                                        if ((res = ebml_read_uint(matroska,
-                                                          &id, &num)) < 0)
-                                            break;
-                                        idx.track = num;
-                                        break;
-                                    }
-
-                                        /* position in file */
-                                    case MATROSKA_ID_CUECLUSTERPOSITION: {
-                                        uint64_t num;
-                                        if ((res = ebml_read_uint(matroska,
-                                                          &id, &num)) < 0)
-                                            break;
-                                        idx.pos = num+matroska->segment_start;
-                                        break;
-                                    }
-
-                                    default:
-                                        av_log(matroska->ctx, AV_LOG_INFO,
-                                               "Unknown entry 0x%x in "
-                                               "CuesTrackPositions\n", id);
-                                        /* fall-through */
-
-                                    case EBML_ID_VOID:
-                                        res = ebml_read_skip(matroska);
-                                        break;
-                                }
-
-                                if (matroska->level_up) {
-                                    matroska->level_up--;
-                                    break;
-                                }
-                            }
-
-                            break;
-
-                        default:
-                            av_log(matroska->ctx, AV_LOG_INFO,
-                                   "Unknown entry 0x%x in cuespoint "
-                                   "index\n", id);
-                            /* fall-through */
-
-                        case EBML_ID_VOID:
-                            res = ebml_read_skip(matroska);
-                            break;
-                    }
-
-                    if (matroska->level_up) {
-                        matroska->level_up--;
-                        break;
-                    }
-                }
-
-                /* so let's see if we got what we wanted */
-                if (idx.pos   != (uint64_t) -1 &&
-                    idx.time  != (uint64_t) -1 &&
-                    idx.track != (uint16_t) -1) {
-                    if (matroska->num_indexes % 32 == 0) {
-                        /* re-allocate bigger index */
-                        matroska->index =
-                            av_realloc(matroska->index,
-                                       (matroska->num_indexes + 32) *
-                                       sizeof(MatroskaDemuxIndex));
-                    }
-                    matroska->index[matroska->num_indexes] = idx;
-                    matroska->num_indexes++;
-                }
-                break;
-
-            default:
-                av_log(matroska->ctx, AV_LOG_INFO,
-                       "Unknown entry 0x%x in cues header\n", id);
-                /* fall-through */
-
-            case EBML_ID_VOID:
-                res = ebml_read_skip(matroska);
-                break;
-        }
-
-        if (matroska->level_up) {
-            matroska->level_up--;
-            break;
-        }
-    }
-
-    return res;
+    return ebml_parse(matroska, matroska_index, matroska, MATROSKA_ID_CUES, 0);
 }
 
 static int
 matroska_parse_metadata (MatroskaDemuxContext *matroska)
 {
-    int res = 0;
-    uint32_t id;
-
-    while (res == 0) {
-        if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
-            res = AVERROR(EIO);
-            break;
-        } else if (matroska->level_up) {
-            matroska->level_up--;
-            break;
-        }
-
-        switch (id) {
-            /* Hm, this is unsupported... */
-            default:
-                av_log(matroska->ctx, AV_LOG_INFO,
-                       "Unknown entry 0x%x in metadata header\n", id);
-                /* fall-through */
-
-            case EBML_ID_VOID:
-                res = ebml_read_skip(matroska);
-                break;
-        }
-
-        if (matroska->level_up) {
-            matroska->level_up--;
-            break;
-        }
-    }
-
-    return res;
+    return ebml_parse(matroska, matroska_tags, matroska, MATROSKA_ID_TAGS, 0);
 }
 
 static int
@@ -1863,6 +1826,7 @@ matroska_parse_seekhead (MatroskaDemuxContext *matroska)
             case MATROSKA_ID_SEEKENTRY: {
                 uint32_t seek_id = 0, peek_id_cache = 0;
                 uint64_t seek_pos = (uint64_t) -1, t;
+                int dummy_level = 0;
 
                 if ((res = ebml_read_master(matroska, &id)) < 0)
                     break;
@@ -1939,6 +1903,7 @@ matroska_parse_seekhead (MatroskaDemuxContext *matroska)
                         level.length = (uint64_t)-1;
                         matroska->levels[matroska->num_levels] = level;
                         matroska->num_levels++;
+                        dummy_level = 1;
 
                         /* check ID */
                         if (!(id = ebml_peek_id (matroska,
@@ -1954,8 +1919,6 @@ matroska_parse_seekhead (MatroskaDemuxContext *matroska)
                         }
 
                         /* read master + parse */
-                        if ((res = ebml_read_master(matroska, &id)) < 0)
-                            goto finish;
                         switch (id) {
                             case MATROSKA_ID_CUES:
                                 if (!(res = matroska_parse_index(matroska)) ||
@@ -1975,13 +1938,14 @@ matroska_parse_seekhead (MatroskaDemuxContext *matroska)
 
                     finish:
                         /* remove dummy level */
-                        while (matroska->num_levels) {
-                            matroska->num_levels--;
-                            length =
-                                matroska->levels[matroska->num_levels].length;
-                            if (length == (uint64_t)-1)
-                                break;
-                        }
+                        if (dummy_level)
+                            while (matroska->num_levels) {
+                                matroska->num_levels--;
+                                length =
+                                  matroska->levels[matroska->num_levels].length;
+                                if (length == (uint64_t)-1)
+                                    break;
+                            }
 
                         /* seek back */
                         if ((res = ebml_read_seek(matroska, before_pos)) < 0)
@@ -2074,6 +2038,7 @@ matroska_parse_attachments(AVFormatContext *s)
                 default:
                     av_log(matroska->ctx, AV_LOG_INFO,
                            "Unknown attachedfile ID 0x%x\n", id);
+                case MATROSKA_ID_FILEUID:
                 case EBML_ID_VOID:
                     res = ebml_read_skip(matroska);
                     break;
@@ -2133,7 +2098,25 @@ matroska_parse_attachments(AVFormatContext *s)
     return res;
 }
 
-#define ARRAY_SIZE(x)  (sizeof(x)/sizeof(*x))
+static int
+matroska_parse_chapters(AVFormatContext *s)
+{
+    MatroskaDemuxContext *matroska = s->priv_data;
+    EbmlList *chapters_list = &matroska->chapters;
+    MatroskaChapter *chapters;
+    int i, res;
+
+    res = ebml_parse(matroska, matroska_chapters, matroska, MATROSKA_ID_CHAPTERS, 0);
+
+    chapters = chapters_list->elem;
+    for (i=0; i<chapters_list->nb_elem; i++)
+        if (chapters[i].start != AV_NOPTS_VALUE && chapters[i].uid)
+            ff_new_chapter(s, chapters[i].uid, (AVRational){1, 1000000000},
+                           chapters[i].start, chapters[i].end,
+                           chapters[i].title);
+
+    return res;
+}
 
 static int
 matroska_aac_profile (char *codec_id)
@@ -2165,31 +2148,26 @@ matroska_read_header (AVFormatContext    *s,
                       AVFormatParameters *ap)
 {
     MatroskaDemuxContext *matroska = s->priv_data;
-    char *doctype;
-    int version, last_level, res = 0;
+    EbmlList *index_list;
+    MatroskaIndex *index;
+    int i, j, last_level, res = 0;
+    Ebml ebml = { 0 };
     uint32_t id;
 
     matroska->ctx = s;
 
     /* First read the EBML header. */
-    doctype = NULL;
-    if ((res = ebml_read_header(matroska, &doctype, &version)) < 0)
-        return res;
-    if ((doctype == NULL) || strcmp(doctype, "matroska")) {
+    if (ebml_parse(matroska, ebml_syntax, &ebml, 0, 1)
+        || ebml.version > EBML_VERSION       || ebml.max_size > sizeof(uint64_t)
+        || ebml.id_length > sizeof(uint32_t) || strcmp(ebml.doctype, "matroska")
+        || ebml.doctype_version > 2) {
         av_log(matroska->ctx, AV_LOG_ERROR,
-               "Wrong EBML doctype ('%s' != 'matroska').\n",
-               doctype ? doctype : "(none)");
-        if (doctype)
-            av_free(doctype);
-        return AVERROR_NOFMT;
-    }
-    av_free(doctype);
-    if (version > 2) {
-        av_log(matroska->ctx, AV_LOG_ERROR,
-               "Matroska demuxer version 2 too old for file version %d\n",
-               version);
+               "EBML header using unsupported features\n"
+               "(EBML version %"PRIu64", doctype %s, doc version %"PRIu64")\n",
+               ebml.version, ebml.doctype, ebml.doctype_version);
         return AVERROR_NOFMT;
     }
+    ebml_free(ebml_syntax, &ebml);
 
     /* The next thing is a segment. */
     while (1) {
@@ -2244,8 +2222,6 @@ matroska_read_header (AVFormatContext    *s,
             /* stream index */
             case MATROSKA_ID_CUES: {
                 if (!matroska->index_parsed) {
-                    if ((res = ebml_read_master(matroska, &id)) < 0)
-                        break;
                     res = matroska_parse_index(matroska);
                 } else
                     res = ebml_read_skip(matroska);
@@ -2255,8 +2231,6 @@ matroska_read_header (AVFormatContext    *s,
             /* metadata */
             case MATROSKA_ID_TAGS: {
                 if (!matroska->metadata_parsed) {
-                    if ((res = ebml_read_master(matroska, &id)) < 0)
-                        break;
                     res = matroska_parse_metadata(matroska);
                 } else
                     res = ebml_read_skip(matroska);
@@ -2285,6 +2259,11 @@ matroska_read_header (AVFormatContext    *s,
                 break;
             }
 
+            case MATROSKA_ID_CHAPTERS: {
+                res = matroska_parse_chapters(s);
+                break;
+            }
+
             default:
                 av_log(matroska->ctx, AV_LOG_INFO,
                        "Unknown matroska file header ID 0x%x\n", id);
@@ -2313,7 +2292,6 @@ matroska_read_header (AVFormatContext    *s,
             int extradata_size = 0;
             int extradata_offset = 0;
             track = matroska->tracks[i];
-            track->stream_index = -1;
 
             /* Apply some sanity checks. */
             if (track->codec_id == NULL)
@@ -2327,6 +2305,10 @@ matroska_read_header (AVFormatContext    *s,
                 }
             }
 
+            st = track->stream = av_new_stream(s, matroska->num_streams++);
+            if (st == NULL)
+                return AVERROR(ENOMEM);
+
             /* Set the FourCC from the CodecID. */
             /* This is the MS compatibility mode which stores a
              * BITMAPINFOHEADER in the CodecPrivate. */
@@ -2356,6 +2338,15 @@ matroska_read_header (AVFormatContext    *s,
 
             }
 
+            if (!strcmp(track->codec_id, "V_QUICKTIME") &&
+                (track->codec_priv_size >= 86) &&
+                (track->codec_priv != NULL)) {
+                MatroskaVideoTrack *vtrack = (MatroskaVideoTrack *) track;
+
+                vtrack->fourcc = AV_RL32(track->codec_priv);
+                codec_id = codec_get_id(codec_movvideo_tags, vtrack->fourcc);
+            }
+
             else if (codec_id == CODEC_ID_AAC && !track->codec_priv_size) {
                 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *) track;
                 int profile = matroska_aac_profile(track->codec_id);
@@ -2436,20 +2427,14 @@ matroska_read_header (AVFormatContext    *s,
                        track->codec_id);
             }
 
-            track->stream_index = matroska->num_streams;
-
-            matroska->num_streams++;
-            st = av_new_stream(s, track->stream_index);
-            if (st == NULL)
-                return AVERROR(ENOMEM);
-            av_set_pts_info(st, 64, matroska->time_scale, 1000*1000*1000); /* 64 bit pts in ns */
+            av_set_pts_info(st, 64, matroska->time_scale*track->time_scale, 1000*1000*1000); /* 64 bit pts in ns */
 
             st->codec->codec_id = codec_id;
             st->start_time = 0;
             if (strcmp(track->language, "und"))
-                strcpy(st->language, track->language);
+                av_strlcpy(st->language, track->language, 4);
 
-            if (track->flags & MATROSKA_TRACK_DEFAULT)
+            if (track->flag_default)
                 st->disposition |= AV_DISPOSITION_DEFAULT;
 
             if (track->default_duration)
@@ -2501,16 +2486,18 @@ matroska_read_header (AVFormatContext    *s,
         res = 0;
     }
 
-    if (matroska->index_parsed) {
-        int i, track, stream;
-        for (i=0; i<matroska->num_indexes; i++) {
-            MatroskaDemuxIndex *idx = &matroska->index[i];
-            track = matroska_find_track_by_num(matroska, idx->track);
-            if (track < 0)  continue;
-            stream = matroska->tracks[track]->stream_index;
-            if (stream >= 0 && stream < matroska->ctx->nb_streams)
-                av_add_index_entry(matroska->ctx->streams[stream],
-                                   idx->pos, idx->time/matroska->time_scale,
+    index_list = &matroska->index;
+    index = index_list->elem;
+    for (i=0; i<index_list->nb_elem; i++) {
+        EbmlList *pos_list = &index[i].pos;
+        MatroskaIndexPos *pos = pos_list->elem;
+        for (j=0; j<pos_list->nb_elem; j++) {
+            MatroskaTrack *track = matroska_find_track_by_num(matroska,
+                                                              pos[j].track);
+            if (track && track->stream)
+                av_add_index_entry(track->stream,
+                                   pos[j].pos + matroska->segment_start,
+                                   index[i].time*matroska->time_scale/AV_TIME_BASE,
                                    0, 0, AVINDEX_KEYFRAME);
         }
     }
@@ -2521,10 +2508,10 @@ matroska_read_header (AVFormatContext    *s,
 static int
 matroska_parse_block(MatroskaDemuxContext *matroska, uint8_t *data, int size,
                      int64_t pos, uint64_t cluster_time, uint64_t duration,
-                     int is_keyframe, int is_bframe)
+                     int is_keyframe)
 {
+    MatroskaTrack *track;
     int res = 0;
-    int track;
     AVStream *st;
     AVPacket *pkt;
     uint8_t *origdata = data;
@@ -2532,7 +2519,6 @@ matroska_parse_block(MatroskaDemuxContext *matroska, uint8_t *data, int size,
     uint32_t *lace_size = NULL;
     int n, flags, laces = 0;
     uint64_t num;
-    int stream_index;
 
     /* first byte(s): tracknum */
     if ((n = matroska_ebmlnum_uint(data, size, &num)) < 0) {
@@ -2545,24 +2531,19 @@ matroska_parse_block(MatroskaDemuxContext *matroska, uint8_t *data, int size,
 
     /* fetch track from num */
     track = matroska_find_track_by_num(matroska, num);
-    if (size <= 3 || track < 0 || track >= matroska->num_tracks) {
+    if (size <= 3 || !track || !track->stream) {
         av_log(matroska->ctx, AV_LOG_INFO,
-               "Invalid stream %d or size %u\n", track, size);
-        av_free(origdata);
-        return res;
-    }
-    stream_index = matroska->tracks[track]->stream_index;
-    if (stream_index < 0 || stream_index >= matroska->ctx->nb_streams) {
+               "Invalid stream %"PRIu64" or size %u\n", num, size);
         av_free(origdata);
         return res;
     }
-    st = matroska->ctx->streams[stream_index];
+    st = track->stream;
     if (st->discard >= AVDISCARD_ALL) {
         av_free(origdata);
         return res;
     }
     if (duration == AV_NOPTS_VALUE)
-        duration = matroska->tracks[track]->default_duration / matroska->time_scale;
+        duration = track->default_duration / matroska->time_scale;
 
     /* block_time (relative to cluster time) */
     block_time = AV_RB16(data);
@@ -2590,10 +2571,7 @@ matroska_parse_block(MatroskaDemuxContext *matroska, uint8_t *data, int size,
         case 0x1: /* xiph lacing */
         case 0x2: /* fixed-size lacing */
         case 0x3: /* EBML lacing */
-            if (size == 0) {
-                res = -1;
-                break;
-            }
+            assert(size>0); // size <=3 is checked before size-=3 above
             laces = (*data) + 1;
             data += 1;
             size -= 1;
@@ -2670,7 +2648,7 @@ matroska_parse_block(MatroskaDemuxContext *matroska, uint8_t *data, int size,
             if (st->codec->codec_id == CODEC_ID_RA_288 ||
                 st->codec->codec_id == CODEC_ID_COOK ||
                 st->codec->codec_id == CODEC_ID_ATRAC3) {
-                MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)matroska->tracks[track];
+                MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
                 int a = st->codec->block_align;
                 int sps = audiotrack->sub_packet_size;
                 int cfs = audiotrack->coded_framesize;
@@ -2699,74 +2677,38 @@ matroska_parse_block(MatroskaDemuxContext *matroska, uint8_t *data, int size,
                     memcpy(pkt->data, audiotrack->buf
                            + a * (h*w / a - audiotrack->pkt_cnt--), a);
                     pkt->pos = pos;
-                    pkt->stream_index = stream_index;
+                    pkt->stream_index = st->index;
                     matroska_queue_packet(matroska, pkt);
                 }
             } else {
-                int result, offset = 0, ilen, olen, pkt_size = lace_size[n];
+                int offset = 0, pkt_size = lace_size[n];
                 uint8_t *pkt_data = data;
 
-                if (matroska->tracks[track]->encoding_scope & 1) {
-                    switch (matroska->tracks[track]->encoding_algo) {
-                    case MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP:
-                        offset = matroska->tracks[track]->encoding_settings_len;
-                        break;
-                    case MATROSKA_TRACK_ENCODING_COMP_LZO:
-                        pkt_data = NULL;
-                        do {
-                            ilen = lace_size[n];
-                            olen = pkt_size *= 3;
-                            pkt_data = av_realloc(pkt_data,
-                                                  pkt_size+LZO_OUTPUT_PADDING);
-                            result = lzo1x_decode(pkt_data, &olen, data, &ilen);
-                        } while (result==LZO_OUTPUT_FULL && pkt_size<10000000);
-                        if (result) {
-                            av_free(pkt_data);
-                            continue;
-                        }
-                        pkt_size -= olen;
-                        break;
-#ifdef CONFIG_ZLIB
-                    case MATROSKA_TRACK_ENCODING_COMP_ZLIB: {
-                        z_stream zstream = {0};
-                        pkt_data = NULL;
-                        if (inflateInit(&zstream) != Z_OK)
-                            continue;
-                        zstream.next_in = data;
-                        zstream.avail_in = lace_size[n];
-                        do {
-                            pkt_size *= 3;
-                            pkt_data = av_realloc(pkt_data, pkt_size);
-                            zstream.avail_out = pkt_size - zstream.total_out;
-                            zstream.next_out = pkt_data + zstream.total_out;
-                            result = inflate(&zstream, Z_NO_FLUSH);
-                        } while (result==Z_OK && pkt_size<10000000);
-                        pkt_size = zstream.total_out;
-                        inflateEnd(&zstream);
-                        if (result != Z_STREAM_END) {
-                            av_free(pkt_data);
-                            continue;
-                        }
-                        break;
-                    }
-#endif
-                    }
+                if (track->encoding_scope & 1) {
+                    offset = matroska_decode_buffer(&pkt_data, &pkt_size,
+                                                    track);
+                    if (offset < 0)
+                        continue;
                 }
 
                 pkt = av_mallocz(sizeof(AVPacket));
                 /* XXX: prevent data copy... */
                 if (av_new_packet(pkt, pkt_size+offset) < 0) {
+                    av_free(pkt);
                     res = AVERROR(ENOMEM);
                     n = laces-1;
                     break;
                 }
                 if (offset)
-                    memcpy (pkt->data, matroska->tracks[track]->encoding_settings, offset);
+                    memcpy (pkt->data, track->encoding_settings, offset);
                 memcpy (pkt->data+offset, pkt_data, pkt_size);
 
+                if (pkt_data != data)
+                    av_free(pkt_data);
+
                 if (n == 0)
                     pkt->flags = is_keyframe;
-                pkt->stream_index = stream_index;
+                pkt->stream_index = st->index;
 
                 pkt->pts = timecode;
                 pkt->pos = pos;
@@ -2792,7 +2734,6 @@ matroska_parse_blockgroup (MatroskaDemuxContext *matroska,
 {
     int res = 0;
     uint32_t id;
-    int is_bframe = 0;
     int is_keyframe = PKT_FLAG_KEY, last_num_packets = matroska->num_packets;
     uint64_t duration = AV_NOPTS_VALUE;
     uint8_t *data;
@@ -2835,8 +2776,6 @@ matroska_parse_blockgroup (MatroskaDemuxContext *matroska,
                     matroska->packets[last_num_packets]->flags = 0;
                 if ((res = ebml_read_sint(matroska, &id, &num)) < 0)
                     break;
-                if (num > 0)
-                    is_bframe = 1;
                 break;
             }
 
@@ -2861,7 +2800,7 @@ matroska_parse_blockgroup (MatroskaDemuxContext *matroska,
 
     if (size > 0)
         res = matroska_parse_block(matroska, data, size, pos, cluster_time,
-                                   duration, is_keyframe, is_bframe);
+                                   duration, is_keyframe);
 
     return res;
 }
@@ -2911,7 +2850,7 @@ matroska_parse_cluster (MatroskaDemuxContext *matroska)
                 if (res == 0)
                     res = matroska_parse_block(matroska, data, size, pos,
                                                cluster_time, AV_NOPTS_VALUE,
-                                               -1, 0);
+                                               -1);
                 break;
 
             default:
@@ -3004,6 +2943,7 @@ matroska_read_seek (AVFormatContext *s, int stream_index, int64_t timestamp,
     matroska->skip_to_keyframe = !(flags & AVSEEK_FLAG_ANY);
     matroska->skip_to_stream = st;
     matroska->peek_id = 0;
+    av_update_cur_dts(s, st, st->index_entries[index].timestamp);
     return 0;
 }
 
@@ -3013,16 +2953,11 @@ matroska_read_close (AVFormatContext *s)
     MatroskaDemuxContext *matroska = s->priv_data;
     int n = 0;
 
-    av_free(matroska->writing_app);
-    av_free(matroska->muxing_app);
-    av_free(matroska->index);
-
     matroska_clear_queue(matroska);
 
     for (n = 0; n < matroska->num_tracks; n++) {
         MatroskaTrack *track = matroska->tracks[n];
         av_free(track->codec_id);
-        av_free(track->codec_name);
         av_free(track->codec_priv);
         av_free(track->name);
 
@@ -3033,13 +2968,14 @@ matroska_read_close (AVFormatContext *s)
 
         av_free(track);
     }
+    ebml_free(matroska_index, matroska);
 
     return 0;
 }
 
 AVInputFormat matroska_demuxer = {
     "matroska",
-    "Matroska file format",
+    NULL_IF_CONFIG_SMALL("Matroska file format"),
     sizeof(MatroskaDemuxContext),
     matroska_probe,
     matroska_read_header,