remove unused context field
[ffmpeg.git] / libavformat / matroska.c
index b3ea7a0..867a2a3 100644 (file)
@@ -178,7 +178,6 @@ typedef enum {
   MATROSKA_TRACK_DEFAULT = (1<<1),
   MATROSKA_TRACK_LACING  = (1<<2),
   MATROSKA_TRACK_REAL_V  = (1<<4),
-  MATROSKA_TRACK_REORDER = (1<<8),
   MATROSKA_TRACK_SHIFT   = (1<<16)
 } MatroskaTrackFlags;
 
@@ -213,6 +212,7 @@ static CodecTags codec_tags[]={
     {"V_REAL/RV20"      , CODEC_ID_RV20},
     {"V_REAL/RV30"      , CODEC_ID_RV30},
     {"V_REAL/RV40"      , CODEC_ID_RV40},
+    {"V_THEORA"         , CODEC_ID_THEORA},
 /* TODO: Real/Quicktime */
 
 //    {"A_MS/ACM"         , CODEC_ID_NONE},
@@ -320,9 +320,6 @@ typedef struct MatroskaDemuxContext {
     /* timescale in the file */
     int64_t time_scale;
 
-    /* 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). */
     int num_tracks, num_streams;
@@ -337,10 +334,6 @@ typedef struct MatroskaDemuxContext {
     /* The packet queue. */
     AVPacket **packets;
     int num_packets;
-    /* Second packet queue used to reorder pts of some video track. */
-    AVPacket **packets_reorder;
-    int num_packets_reorder;
-    uint64_t reorder_max_pts;
 
     /* have we already parse metadata/cues/clusters? */
     int metadata_parsed,
@@ -1025,42 +1018,6 @@ matroska_queue_packet (MatroskaDemuxContext *matroska,
     matroska->num_packets++;
 }
 
-/*
- * Put a packet into our internal reordering queue. Will be moved to the
- * main packet queue when enough packets are available to reorder pts.
- */
-
-static void
-matroska_queue_packet_reordered (MatroskaDemuxContext *matroska,
-                                 AVPacket             *pkt,
-                                 int                   is_bframe)
-{
-    if (matroska->num_packets_reorder && !is_bframe
-        && pkt->pts > matroska->reorder_max_pts) {
-        /* reorder pts */
-        int i, j, k = 1;
-        for (j=matroska->num_packets_reorder-1; j && k; j--) {
-            k = 0;
-            for (i=0; i<j; i++) {
-                if (matroska->packets_reorder[i]->pts > matroska->packets_reorder[i+1]->pts) {
-                    FFSWAP(uint64_t, matroska->packets_reorder[i]->pts, matroska->packets_reorder[i+1]->pts);
-                    k = 1;
-                }
-            }
-        }
-        /* then really queue the packets */
-        for (i=0; i<matroska->num_packets_reorder; i++)
-            matroska_queue_packet (matroska, matroska->packets_reorder[i]);
-        matroska->num_packets_reorder = 0;
-    }
-    matroska->packets_reorder =
-        av_realloc(matroska->packets_reorder,
-                   (matroska->num_packets_reorder + 1) * sizeof(AVPacket *));
-    matroska->packets_reorder[matroska->num_packets_reorder++] = pkt;
-    if (pkt->pts > matroska->reorder_max_pts)
-        matroska->reorder_max_pts = pkt->pts;
-}
-
 
 /*
  * Autodetecting...
@@ -1073,9 +1030,6 @@ matroska_probe (AVProbeData *p)
     int len_mask = 0x80, size = 1, n = 1;
     uint8_t probe_data[] = { 'm', 'a', 't', 'r', 'o', 's', 'k', 'a' };
 
-    if (p->buf_size < 5)
-        return 0;
-
     /* ebml header? */
     if ((p->buf[0] << 24 | p->buf[1] << 16 |
          p->buf[2] << 8 | p->buf[3]) != EBML_ID_HEADER)
@@ -2287,14 +2241,6 @@ matroska_read_header (AVFormatContext    *s,
 
             }
 
-            else if (codec_id == CODEC_ID_MPEG1VIDEO ||
-                     codec_id == CODEC_ID_MPEG2VIDEO ||
-                     codec_id == CODEC_ID_MPEG4      ||
-                     codec_id == CODEC_ID_MSMPEG4V3  ||
-                     codec_id == CODEC_ID_H264) {
-                track->flags |= MATROSKA_TRACK_REORDER;
-            }
-
             else if (codec_id == CODEC_ID_AAC && !track->codec_priv_size) {
                 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *) track;
                 int profile = matroska_aac_profile(track->codec_id);
@@ -2313,6 +2259,7 @@ matroska_read_header (AVFormatContext    *s,
                 } else {
                     extradata_size = 2;
                 }
+                track->default_duration = 1024*1000 / audiotrack->internal_samplerate;
             }
 
             else if (codec_id == CODEC_ID_TTA) {
@@ -2387,6 +2334,7 @@ matroska_read_header (AVFormatContext    *s,
                           st->codec->height * videotrack->display_width,
                           st->codec-> width * videotrack->display_height,
                           255);
+                st->need_parsing = 2;
             } else if (track->type == MATROSKA_TRACK_TYPE_AUDIO) {
                 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
 
@@ -2426,9 +2374,8 @@ rv_offset(uint8_t *data, int slice, int slices)
 
 static int
 matroska_parse_block(MatroskaDemuxContext *matroska, uint8_t *data, int size,
-                     int64_t pos, uint64_t cluster_time,
-                     int is_keyframe, int is_bframe,
-                     int *ptrack, AVPacket **ppkt)
+                     int64_t pos, uint64_t cluster_time, uint64_t duration,
+                     int is_keyframe, int is_bframe)
 {
     int res = 0;
     int track;
@@ -2450,7 +2397,6 @@ matroska_parse_block(MatroskaDemuxContext *matroska, uint8_t *data, int size,
 
     /* fetch track from num */
     track = matroska_find_track_by_num(matroska, num);
-    if (ptrack)  *ptrack = track;
     if (size <= 3 || track < 0 || track >= matroska->num_tracks) {
         av_log(matroska->ctx, AV_LOG_INFO,
                "Invalid stream %d or size %u\n", track, size);
@@ -2461,6 +2407,8 @@ matroska_parse_block(MatroskaDemuxContext *matroska, uint8_t *data, int size,
         av_free(origdata);
         return res;
     }
+    if (duration == AV_NOPTS_VALUE)
+        duration = matroska->tracks[track]->default_duration;
 
     /* block_time (relative to cluster time) */
     block_time = (data[0] << 8) | data[1];
@@ -2564,7 +2512,6 @@ matroska_parse_block(MatroskaDemuxContext *matroska, uint8_t *data, int size,
                 slices = *data++ + 1;
                 lace_size[n]--;
             }
-            /* FIXME: duration */
 
             for (slice=0; slice<slices; slice++) {
                 int slice_size, slice_offset = 0;
@@ -2575,7 +2522,6 @@ matroska_parse_block(MatroskaDemuxContext *matroska, uint8_t *data, int size,
                 else
                     slice_size = rv_offset(data, slice+1, slices) - slice_offset;
                 pkt = av_mallocz(sizeof(AVPacket));
-                if (ppkt)  *ppkt = pkt;
                 /* XXX: prevent data copy... */
                 if (av_new_packet(pkt, slice_size) < 0) {
                     res = AVERROR_NOMEM;
@@ -2590,13 +2536,12 @@ matroska_parse_block(MatroskaDemuxContext *matroska, uint8_t *data, int size,
 
                 pkt->pts = timecode;
                 pkt->pos = pos;
+                pkt->duration = duration;
 
-                if (matroska->tracks[track]->flags & MATROSKA_TRACK_REORDER)
-                    matroska_queue_packet_reordered(matroska, pkt, is_bframe);
-                else
                     matroska_queue_packet(matroska, pkt);
 
-                timecode = AV_NOPTS_VALUE;
+                if (timecode != AV_NOPTS_VALUE)
+                    timecode = duration ? timecode + duration : AV_NOPTS_VALUE;
             }
             data += lace_size[n];
         }
@@ -2613,11 +2558,9 @@ matroska_parse_blockgroup (MatroskaDemuxContext *matroska,
 {
     int res = 0;
     uint32_t id;
-    AVPacket *pkt = NULL;
     int is_bframe = 0;
     int is_keyframe = PKT_FLAG_KEY, last_num_packets = matroska->num_packets;
     uint64_t duration = AV_NOPTS_VALUE;
-    int track = -1;
     uint8_t *data;
     int size = 0;
     int64_t pos = 0;
@@ -2685,15 +2628,7 @@ matroska_parse_blockgroup (MatroskaDemuxContext *matroska,
 
     if (size > 0)
         res = matroska_parse_block(matroska, data, size, pos, cluster_time,
-                                   is_keyframe, is_bframe, &track, &pkt);
-
-    if (pkt)
-    {
-        if (duration != AV_NOPTS_VALUE)
-            pkt->duration = duration;
-        else if (track >= 0 && track < matroska->num_tracks)
-            pkt->duration = matroska->tracks[track]->default_duration;
-    }
+                                   duration, is_keyframe, is_bframe);
 
     return res;
 }
@@ -2742,7 +2677,8 @@ matroska_parse_cluster (MatroskaDemuxContext *matroska)
                 res = ebml_read_binary(matroska, &id, &data, &size);
                 if (res == 0)
                     res = matroska_parse_block(matroska, data, size, pos,
-                                               cluster_time, -1,0, NULL, NULL);
+                                               cluster_time, AV_NOPTS_VALUE,
+                                               -1, 0);
                 break;
 
             default:
@@ -2831,13 +2767,6 @@ matroska_read_close (AVFormatContext *s)
         }
         av_free(matroska->packets);
     }
-    if (matroska->packets_reorder) {
-        for (n = 0; n < matroska->num_packets_reorder; n++) {
-            av_free_packet(matroska->packets_reorder[n]);
-            av_free(matroska->packets_reorder[n]);
-        }
-        av_free(matroska->packets_reorder);
-    }
 
     for (n = 0; n < matroska->num_tracks; n++) {
         MatroskaTrack *track = matroska->tracks[n];