]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/matroska.c
Minor resync optimization
[ffmpeg] / libavformat / matroska.c
index 0661151487c3b1481f5e746d6e0819ace57aa946..d22e85f2c2e93bdd9a4334ab46bf7dedbe8cb4fe 100644 (file)
@@ -14,7 +14,7 @@
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
 /**
@@ -28,7 +28,8 @@
 
 #include "avformat.h"
 /* For codec_get_bmp_id and codec_get_wav_id. */
-#include "avi.h"
+#include "riff.h"
+#include "intfloat_readwrite.h"
 
 /* EBML version supported */
 #define EBML_VERSION 1
@@ -181,32 +182,46 @@ typedef enum {
  * Matroska Codec IDs. Strings.
  */
 
+typedef struct CodecTags{
+    const char *str;
+    enum CodecID id;
+}CodecTags;
+
 #define MATROSKA_CODEC_ID_VIDEO_VFW_FOURCC   "V_MS/VFW/FOURCC"
-#define MATROSKA_CODEC_ID_VIDEO_UNCOMPRESSED "V_UNCOMPRESSED"
-#define MATROSKA_CODEC_ID_VIDEO_MPEG4_SP     "V_MPEG4/ISO/SP"
-#define MATROSKA_CODEC_ID_VIDEO_MPEG4_ASP    "V_MPEG4/ISO/ASP"
-#define MATROSKA_CODEC_ID_VIDEO_MPEG4_AP     "V_MPEG4/ISO/AP"
-#define MATROSKA_CODEC_ID_VIDEO_MPEG4_AVC    "V_MPEG4/ISO/AVC"
-#define MATROSKA_CODEC_ID_VIDEO_MSMPEG4V3    "V_MPEG4/MS/V3"
-#define MATROSKA_CODEC_ID_VIDEO_MPEG1        "V_MPEG1"
-#define MATROSKA_CODEC_ID_VIDEO_MPEG2        "V_MPEG2"
-#define MATROSKA_CODEC_ID_VIDEO_MJPEG        "V_MJPEG"
+#define MATROSKA_CODEC_ID_AUDIO_ACM          "A_MS/ACM"
+
+static CodecTags codec_tags[]={
+//    {"V_MS/VFW/FOURCC"  , CODEC_ID_NONE},
+    {"V_UNCOMPRESSED"   , CODEC_ID_RAWVIDEO},
+    {"V_MPEG4/ISO/SP"   , CODEC_ID_MPEG4},
+    {"V_MPEG4/ISO/ASP"  , CODEC_ID_MPEG4},
+    {"V_MPEG4/ISO/AP"   , CODEC_ID_MPEG4},
+    {"V_MPEG4/ISO/AVC"  , CODEC_ID_H264},
+    {"V_MPEG4/MS/V3"    , CODEC_ID_MSMPEG4V3},
+    {"V_MPEG1"          , CODEC_ID_MPEG1VIDEO},
+    {"V_MPEG2"          , CODEC_ID_MPEG2VIDEO},
+    {"V_MJPEG"          , CODEC_ID_MJPEG},
+    {"V_REAL/RV10"      , CODEC_ID_RV10},
+    {"V_REAL/RV20"      , CODEC_ID_RV20},
+    {"V_REAL/RV30"      , CODEC_ID_RV30},
+    {"V_REAL/RV40"      , CODEC_ID_RV40},
 /* TODO: Real/Quicktime */
 
-#define MATROSKA_CODEC_ID_AUDIO_ACM          "A_MS/ACM"
-#define MATROSKA_CODEC_ID_AUDIO_MPEG1_L1     "A_MPEG/L1"
-#define MATROSKA_CODEC_ID_AUDIO_MPEG1_L2     "A_MPEG/L2"
-#define MATROSKA_CODEC_ID_AUDIO_MPEG1_L3     "A_MPEG/L3"
-#define MATROSKA_CODEC_ID_AUDIO_PCM_INT_BE   "A_PCM/INT/BIG"
-#define MATROSKA_CODEC_ID_AUDIO_PCM_INT_LE   "A_PCM/INT/LIT"
-#define MATROSKA_CODEC_ID_AUDIO_PCM_FLOAT    "A_PCM/FLOAT/IEEE"
-#define MATROSKA_CODEC_ID_AUDIO_AC3          "A_AC3"
-#define MATROSKA_CODEC_ID_AUDIO_DTS          "A_DTS"
-#define MATROSKA_CODEC_ID_AUDIO_VORBIS       "A_VORBIS"
-#define MATROSKA_CODEC_ID_AUDIO_ACM          "A_MS/ACM"
-#define MATROSKA_CODEC_ID_AUDIO_MPEG2        "A_AAC/MPEG2/"
-#define MATROSKA_CODEC_ID_AUDIO_MPEG4        "A_AAC/MPEG4/"
+//    {"A_MS/ACM"         , CODEC_ID_NONE},
+    {"A_MPEG/L1"        , CODEC_ID_MP3},
+    {"A_MPEG/L2"        , CODEC_ID_MP3},
+    {"A_MPEG/L3"        , CODEC_ID_MP3},
+    {"A_PCM/INT/BIG"    , CODEC_ID_PCM_U16BE},
+    {"A_PCM/INT/LIT"    , CODEC_ID_PCM_U16LE},
+//    {"A_PCM/FLOAT/IEEE" , CODEC_ID_NONE},
+    {"A_AC3"            , CODEC_ID_AC3},
+    {"A_DTS"            , CODEC_ID_DTS},
+    {"A_VORBIS"         , CODEC_ID_VORBIS},
+    {"A_AAC/MPEG2/"     , CODEC_ID_AAC},
+    {"A_AAC/MPEG4/"     , CODEC_ID_AAC},
+    {NULL               , CODEC_ID_NONE}
 /* TODO: AC3-9/10 (?), Real, Musepack, Quicktime */
+};
 
 /* max. depth in the EBML tree structure */
 #define EBML_MAX_DEPTH 16
@@ -290,9 +305,8 @@ typedef struct MatroskaDemuxContext {
     /* timescale in the file */
     int64_t time_scale;
 
-    /* length, position (time, ns) */
-    int64_t duration,
-        pos;
+    /* position (time, ns) */
+    int64_t pos;
 
     /* num_streams is the number of streams that av_new_stream() was called
      * for ( = that are available to the calling program). */
@@ -379,7 +393,7 @@ ebml_read_num (MatroskaDemuxContext *matroska,
         if (!url_feof(pb)) {
             offset_t pos = url_ftell(pb);
             av_log(matroska->ctx, AV_LOG_ERROR,
-                   "Read error at pos. %llu (0x%llx)\n",
+                   "Read error at pos. %"PRIu64" (0x%"PRIx64")\n",
                    pos, pos);
         }
         return AVERROR_IO; /* EOS or actual I/O error */
@@ -393,7 +407,7 @@ ebml_read_num (MatroskaDemuxContext *matroska,
     if (read > max_size) {
         offset_t pos = url_ftell(pb) - 1;
         av_log(matroska->ctx, AV_LOG_ERROR,
-               "Invalid EBML number size tag 0x%02x at pos %llu (0x%llx)\n",
+               "Invalid EBML number size tag 0x%02x at pos %"PRIu64" (0x%"PRIx64")\n",
                (uint8_t) total, pos, pos);
         return AVERROR_INVALIDDATA;
     }
@@ -537,7 +551,7 @@ ebml_read_uint (MatroskaDemuxContext *matroska,
     if (size < 1 || size > 8) {
         offset_t pos = url_ftell(pb);
         av_log(matroska->ctx, AV_LOG_ERROR,
-               "Invalid uint element size %d at position %lld (0x%llx)\n",
+               "Invalid uint element size %d at position %"PRId64" (0x%"PRIx64")\n",
                 size, pos, pos);
         return AVERROR_INVALIDDATA;
     }
@@ -571,7 +585,7 @@ ebml_read_sint (MatroskaDemuxContext *matroska,
     if (size < 1 || size > 8) {
         offset_t pos = url_ftell(pb);
         av_log(matroska->ctx, AV_LOG_ERROR,
-               "Invalid sint element size %d at position %lld (0x%llx)\n",
+               "Invalid sint element size %d at position %"PRId64" (0x%"PRIx64")\n",
                 size, pos, pos);
         return AVERROR_INVALIDDATA;
     }
@@ -597,8 +611,8 @@ ebml_read_sint (MatroskaDemuxContext *matroska,
 
 static int
 ebml_read_float (MatroskaDemuxContext *matroska,
-                uint32_t             *id,
-                double               *num)
+                 uint32_t             *id,
+                 double               *num)
 {
     ByteIOContext *pb = &matroska->ctx->pb;
     int size, res;
@@ -609,42 +623,21 @@ ebml_read_float (MatroskaDemuxContext *matroska,
         return res;
     size = rlength;
 
-    if (size != 4 && size != 8 && size != 10) {
+    if (size == 4) {
+        *num= av_int2flt(get_be32(pb));
+    } else if(size==8){
+        *num= av_int2dbl(get_be64(pb));
+    } else if(size==10){
+        av_log(matroska->ctx, AV_LOG_ERROR,
+               "FIXME! 10-byte floats unimplemented\n");
+        return AVERROR_UNKNOWN;
+    } else{
         offset_t pos = url_ftell(pb);
         av_log(matroska->ctx, AV_LOG_ERROR,
-               "Invalid float element size %d at position %llu (0x%llx)\n",
+               "Invalid float element size %d at position %"PRIu64" (0x%"PRIx64")\n",
                size, pos, pos);
         return AVERROR_INVALIDDATA;
     }
-    if (size == 10) {
-        av_log(matroska->ctx, AV_LOG_ERROR,
-               "FIXME! 10-byte floats unimplemented\n");
-        return AVERROR_UNKNOWN;
-    }
-
-    if (size == 4) {
-        float f;
-
-        while (size-- > 0)
-#ifdef WORDS_BIGENDIAN
-            ((uint8_t *) &f)[3 - size] = get_byte(pb);
-#else
-            ((uint8_t *) &f)[size] = get_byte(pb);
-#endif
-
-        *num = f;
-    } else {
-        double d;
-
-        while (size-- > 0)
-#ifdef WORDS_BIGENDIAN
-            ((uint8_t *) &d)[7 - size] = get_byte(pb);
-#else
-            ((uint8_t *) &d)[size] = get_byte(pb);
-#endif
-
-        *num = d;
-    }
 
     return 0;
 }
@@ -677,7 +670,7 @@ ebml_read_ascii (MatroskaDemuxContext *matroska,
     if (get_buffer(pb, (uint8_t *) *str, size) != size) {
         offset_t pos = url_ftell(pb);
         av_log(matroska->ctx, AV_LOG_ERROR,
-               "Read error at pos. %llu (0x%llx)\n", pos, pos);
+               "Read error at pos. %"PRIu64" (0x%"PRIx64")\n", pos, pos);
         return AVERROR_IO;
     }
     (*str)[size] = '\0';
@@ -774,7 +767,7 @@ ebml_read_binary (MatroskaDemuxContext *matroska,
     if (get_buffer(pb, *binary, *size) != *size) {
         offset_t pos = url_ftell(pb);
         av_log(matroska->ctx, AV_LOG_ERROR,
-               "Read error at pos. %llu (0x%llx)\n", pos, pos);
+               "Read error at pos. %"PRIu64" (0x%"PRIx64")\n", pos, pos);
         return AVERROR_IO;
     }
 
@@ -817,9 +810,6 @@ matroska_ebmlnum_uint (uint8_t  *data,
         n++;
     }
 
-    if (!total)
-        return AVERROR_INVALIDDATA;
-
     if (read == num_ffs)
         *num = (uint64_t)-1;
     else
@@ -898,7 +888,7 @@ ebml_read_header (MatroskaDemuxContext *matroska,
                     return res;
                 if (num > EBML_VERSION) {
                     av_log(matroska->ctx, AV_LOG_ERROR,
-                           "EBML version %llu (> %d) is not supported\n",
+                           "EBML version %"PRIu64" (> %d) is not supported\n",
                            num, EBML_VERSION);
                     return AVERROR_INVALIDDATA;
                 }
@@ -913,7 +903,7 @@ ebml_read_header (MatroskaDemuxContext *matroska,
                     return res;
                 if (num > sizeof(uint64_t)) {
                     av_log(matroska->ctx, AV_LOG_ERROR,
-                           "Integers of size %llu (> %d) not supported\n",
+                           "Integers of size %"PRIu64" (> %zd) not supported\n",
                            num, sizeof(uint64_t));
                     return AVERROR_INVALIDDATA;
                 }
@@ -928,8 +918,8 @@ ebml_read_header (MatroskaDemuxContext *matroska,
                     return res;
                 if (num > sizeof(uint32_t)) {
                     av_log(matroska->ctx, AV_LOG_ERROR,
-                           "IDs of size %llu (> %u) not supported\n",
-                           num, sizeof(uint32_t));
+                           "IDs of size %"PRIu64" (> %zu) not supported\n",
+                            num, sizeof(uint32_t));
                     return AVERROR_INVALIDDATA;
                 }
                 break;
@@ -1103,7 +1093,7 @@ matroska_parse_info (MatroskaDemuxContext *matroska)
                 double num;
                 if ((res = ebml_read_float(matroska, &id, &num)) < 0)
                     break;
-                matroska->duration = num * matroska->time_scale;
+                matroska->ctx->duration = num * matroska->time_scale * 1000 / AV_TIME_BASE;
                 break;
             }
 
@@ -1685,7 +1675,7 @@ matroska_parse_index (MatroskaDemuxContext *matroska)
                             break;
                         }
 
-                        /* position in the file + track to which it 
+                        /* position in the file + track to which it
                          * belongs */
                         case MATROSKA_ID_CUETRACKPOSITION:
                             if ((res = ebml_read_master(matroska, &id)) < 0)
@@ -1891,7 +1881,7 @@ matroska_parse_seekhead (MatroskaDemuxContext *matroska)
 
                 if (!seek_id || seek_pos == (uint64_t) -1) {
                     av_log(matroska->ctx, AV_LOG_INFO,
-                           "Incomplete seekhead entry (0x%x/%llu)\n",
+                           "Incomplete seekhead entry (0x%x/%"PRIu64")\n",
                            seek_id, seek_pos);
                     break;
                 }
@@ -1921,7 +1911,7 @@ matroska_parse_seekhead (MatroskaDemuxContext *matroska)
                                    "cannot parse further.\n", EBML_MAX_DEPTH);
                             return AVERROR_UNKNOWN;
                         }
-                            
+
                         level.start = 0;
                         level.length = (uint64_t)-1;
                         matroska->levels[matroska->num_levels] = level;
@@ -1934,7 +1924,7 @@ matroska_parse_seekhead (MatroskaDemuxContext *matroska)
                         if (id != seek_id) {
                             av_log(matroska->ctx, AV_LOG_INFO,
                                    "We looked for ID=0x%x but got "
-                                   "ID=0x%x (pos=%llu)",
+                                   "ID=0x%x (pos=%"PRIu64")",
                                    seek_id, id, seek_pos +
                                    matroska->segment_start);
                             goto finish;
@@ -2148,14 +2138,14 @@ matroska_read_header (AVFormatContext    *s,
 
     /* Have we found a cluster? */
     if (res == 1) {
-        int i;
-        enum CodecID codec_id;
+        int i, j;
+        enum CodecID codec_id= CODEC_ID_NONE;
         MatroskaTrack *track;
         AVStream *st;
-        void *extradata = NULL;
-        int extradata_size = 0;
 
         for (i = 0; i < matroska->num_tracks; i++) {
+            void *extradata = NULL;
+            int extradata_size = 0;
             track = matroska->tracks[i];
 
             /* libavformat does not really support subtitles.
@@ -2164,6 +2154,13 @@ matroska_read_header (AVFormatContext    *s,
                 (track->codec_id == NULL))
                 continue;
 
+            for(j=0; codec_tags[j].str; j++){
+                if(!strcmp(codec_tags[j].str, track->codec_id)){
+                    codec_id= codec_tags[j].id;
+                    break;
+                }
+            }
+
             /* Set the FourCC from the CodecID. */
             /* This is the MS compatibility mode which stores a
              * BITMAPINFOHEADER in the CodecPrivate. */
@@ -2179,31 +2176,11 @@ matroska_read_header (AVFormatContext    *s,
                                  (p[2] << 16) | (p[1] << 8) | p[0];
                 codec_id = codec_get_bmp_id(((MatroskaVideoTrack *)track)->fourcc);
 
-            } else if (!strcmp(track->codec_id,
-                               MATROSKA_CODEC_ID_VIDEO_MPEG4_SP) ||
-                       !strcmp(track->codec_id,
-                               MATROSKA_CODEC_ID_VIDEO_MPEG4_ASP) ||
-                       !strcmp(track->codec_id,
-                               MATROSKA_CODEC_ID_VIDEO_MPEG4_AP))
-                codec_id = CODEC_ID_MPEG4;
-            else if (!strcmp(track->codec_id,
-                             MATROSKA_CODEC_ID_VIDEO_MPEG4_AVC))
-                codec_id = CODEC_ID_H264;
-/*             else if (!strcmp(track->codec_id, */
-/*                              MATROSKA_CODEC_ID_VIDEO_UNCOMPRESSED)) */
-/*                 codec_id = CODEC_ID_???; */
-            else if (!strcmp(track->codec_id,
-                             MATROSKA_CODEC_ID_VIDEO_MSMPEG4V3))
-                codec_id = CODEC_ID_MSMPEG4V3;
-            else if (!strcmp(track->codec_id,
-                             MATROSKA_CODEC_ID_VIDEO_MPEG1) ||
-                     !strcmp(track->codec_id,
-                             MATROSKA_CODEC_ID_VIDEO_MPEG2))
-                codec_id = CODEC_ID_MPEG2VIDEO;
+            }
 
             /* This is the MS compatibility mode which stores a
              * WAVEFORMATEX in the CodecPrivate. */
-            else if (!strcmp(track->codec_id, 
+            else if (!strcmp(track->codec_id,
                              MATROSKA_CODEC_ID_AUDIO_ACM) &&
                 (track->codec_priv_size >= 18) &&
                 (track->codec_priv != NULL)) {
@@ -2215,49 +2192,7 @@ matroska_read_header (AVFormatContext    *s,
                 tag = (p[1] << 8) | p[0];
                 codec_id = codec_get_wav_id(tag);
 
-            } else if (!strcmp(track->codec_id,
-                               MATROSKA_CODEC_ID_AUDIO_MPEG1_L1) ||
-                       !strcmp(track->codec_id,
-                               MATROSKA_CODEC_ID_AUDIO_MPEG1_L2) ||
-                       !strcmp(track->codec_id,
-                               MATROSKA_CODEC_ID_AUDIO_MPEG1_L3))
-                codec_id = CODEC_ID_MP3;
-            else if (!strcmp(track->codec_id,
-                             MATROSKA_CODEC_ID_AUDIO_PCM_INT_BE))
-                codec_id = CODEC_ID_PCM_U16BE;
-            else if (!strcmp(track->codec_id,
-                             MATROSKA_CODEC_ID_AUDIO_PCM_INT_LE))
-                codec_id = CODEC_ID_PCM_U16LE;
-/*             else if (!strcmp(track->codec_id, */
-/*                              MATROSKA_CODEC_ID_AUDIO_PCM_FLOAT)) */
-/*                 codec_id = CODEC_ID_PCM_???; */
-            else if (!strcmp(track->codec_id,
-                             MATROSKA_CODEC_ID_AUDIO_AC3))
-                codec_id = CODEC_ID_AC3;
-            else if (!strcmp(track->codec_id,
-                             MATROSKA_CODEC_ID_AUDIO_DTS))
-                codec_id = CODEC_ID_DTS;
-            /* No such codec id so far. */
-/*             else if (!strcmp(track->codec_id, */
-/*                              MATROSKA_CODEC_ID_AUDIO_DTS)) */
-/*                 codec_id = CODEC_ID_DTS; */
-            else if (!strcmp(track->codec_id,
-                             MATROSKA_CODEC_ID_AUDIO_VORBIS)) {
-                extradata_size = track->codec_priv_size;
-                if(extradata_size) {
-                    extradata = av_malloc(extradata_size);
-                    if(extradata == NULL)
-                        return AVERROR_NOMEM;
-                    memcpy(extradata, track->codec_priv, extradata_size);
-                }
-                codec_id = CODEC_ID_VORBIS;
-            } else if (!strcmp(track->codec_id,
-                               MATROSKA_CODEC_ID_AUDIO_MPEG2) ||
-                       !strcmp(track->codec_id,
-                               MATROSKA_CODEC_ID_AUDIO_MPEG4))
-                codec_id = CODEC_ID_AAC;
-            else
-                codec_id = CODEC_ID_NONE;
+            }
 
             if (codec_id == CODEC_ID_NONE) {
                 av_log(matroska->ctx, AV_LOG_INFO,
@@ -2271,7 +2206,7 @@ matroska_read_header (AVFormatContext    *s,
             st = av_new_stream(s, track->stream_index);
             if (st == NULL)
                 return AVERROR_NOMEM;
-            av_set_pts_info(st, 24, 1, 1000); /* 24 bit pts in ms */
+            av_set_pts_info(st, 64, matroska->time_scale, 1000*1000*1000); /* 64 bit pts in ns */
 
             st->codec->codec_id = codec_id;
 
@@ -2295,24 +2230,22 @@ matroska_read_header (AVFormatContext    *s,
                 st->codec->width = videotrack->pixel_width;
                 st->codec->height = videotrack->pixel_height;
                 if (videotrack->display_width == 0)
-                    st->codec->sample_aspect_ratio.num =
-                        videotrack->pixel_width;
-                else
-                    st->codec->sample_aspect_ratio.num =
-                        videotrack->display_width;
+                    videotrack->display_width= videotrack->pixel_width;
                 if (videotrack->display_height == 0)
-                    st->codec->sample_aspect_ratio.num =
-                        videotrack->pixel_height;
-                else
-                    st->codec->sample_aspect_ratio.num =
-                        videotrack->display_height;
-
+                    videotrack->display_height= videotrack->pixel_height;
+                av_reduce(&st->codec->sample_aspect_ratio.num,
+                          &st->codec->sample_aspect_ratio.den,
+                          st->codec->height * videotrack->display_width,
+                          st->codec-> width * videotrack->display_height,
+                          255);
             } else if (track->type == MATROSKA_TRACK_TYPE_AUDIO) {
                 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
 
                 st->codec->codec_type = CODEC_TYPE_AUDIO;
                 st->codec->sample_rate = audiotrack->samplerate;
                 st->codec->channels = audiotrack->channels;
+            } else if (track->type == MATROSKA_TRACK_TYPE_SUBTITLE) {
+                st->codec->codec_type = CODEC_TYPE_SUBTITLE;
             }
 
             /* What do we do with private data? E.g. for Vorbis. */
@@ -2362,7 +2295,7 @@ matroska_parse_blockgroup (MatroskaDemuxContext *matroska,
             case MATROSKA_ID_BLOCK: {
                 uint8_t *data, *origdata;
                 int size;
-                uint64_t time;
+                int16_t block_time;
                 uint32_t *lace_size = NULL;
                 int n, track, flags, laces = 0;
                 uint64_t num;
@@ -2392,11 +2325,11 @@ matroska_parse_blockgroup (MatroskaDemuxContext *matroska,
                 }
                 if(matroska->ctx->streams[ matroska->tracks[track]->stream_index ]->discard >= AVDISCARD_ALL){
                     av_free(origdata);
-                    break;                
+                    break;
                 }
 
-                /* time (relative to cluster time) */
-                time = ((data[0] << 8) | data[1]) * matroska->time_scale;
+                /* block_time (relative to cluster time) */
+                block_time = (data[0] << 8) | data[1];
                 data += 2;
                 size -= 2;
                 flags = *data;
@@ -2484,7 +2417,7 @@ matroska_parse_blockgroup (MatroskaDemuxContext *matroska,
 
                 if (res == 0) {
                     for (n = 0; n < laces; n++) {
-                        uint64_t timecode = 0;
+                        uint64_t timecode = AV_NOPTS_VALUE;
 
                         pkt = av_mallocz(sizeof(AVPacket));
                         /* XXX: prevent data copy... */
@@ -2492,11 +2425,9 @@ matroska_parse_blockgroup (MatroskaDemuxContext *matroska,
                             res = AVERROR_NOMEM;
                             break;
                         }
-                        if (cluster_time != (uint64_t)-1) {
-                            if (time < 0 && (-time) > cluster_time)
-                                timecode = cluster_time;
-                            else
-                                timecode = cluster_time + time;
+                        if (cluster_time != (uint64_t)-1 && n == 0) {
+                            if (cluster_time + block_time >= 0)
+                                timecode = cluster_time + block_time;
                         }
                         /* FIXME: duration */
 
@@ -2507,7 +2438,7 @@ matroska_parse_blockgroup (MatroskaDemuxContext *matroska,
                         pkt->stream_index =
                             matroska->tracks[track]->stream_index;
 
-                        pkt->pts = timecode / 1000000; /* ns to ms */
+                        pkt->pts = timecode;
                         pkt->pos= pos;
 
                         matroska_queue_packet(matroska, pkt);
@@ -2564,7 +2495,7 @@ matroska_parse_cluster (MatroskaDemuxContext *matroska)
     uint64_t cluster_time = 0;
 
     av_log(matroska->ctx, AV_LOG_DEBUG,
-           "parsing cluster at %lld\n", url_ftell(&matroska->ctx->pb));
+           "parsing cluster at %"PRId64"\n", url_ftell(&matroska->ctx->pb));
 
     while (res == 0) {
         if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
@@ -2581,7 +2512,7 @@ matroska_parse_cluster (MatroskaDemuxContext *matroska)
                 uint64_t num;
                 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
                     break;
-                cluster_time = num * matroska->time_scale;
+                cluster_time = num;
                 break;
             }
 
@@ -2699,16 +2630,12 @@ matroska_read_close (AVFormatContext *s)
         av_free(track);
     }
 
-    for (n = 0; n < s->nb_streams; n++) {
-        av_free(s->streams[n]->codec->extradata);
-    }
-
     memset(matroska, 0, sizeof(MatroskaDemuxContext));
 
     return 0;
 }
 
-static AVInputFormat matroska_iformat = {
+AVInputFormat matroska_demuxer = {
     "matroska",
     "Matroska file format",
     sizeof(MatroskaDemuxContext),
@@ -2717,10 +2644,3 @@ static AVInputFormat matroska_iformat = {
     matroska_read_packet,
     matroska_read_close,
 };
-
-int
-matroska_init(void)
-{
-    av_register_input_format(&matroska_iformat);
-    return 0;
-}