]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/matroskadec.c
avoid infinite loop at the end of files which are not properly terminated
[ffmpeg] / libavformat / matroskadec.c
index b7d8cd649797898c2b782bb9b71b7ac605f46e64..0daf8f19eecb55bb4aa7d98cde55a9f0f34d98cd 100644 (file)
@@ -223,7 +223,7 @@ ebml_read_num (MatroskaDemuxContext *matroska,
                    "Read error at pos. %"PRIu64" (0x%"PRIx64")\n",
                    pos, pos);
         }
-        return AVERROR_IO; /* EOS or actual I/O error */
+        return AVERROR(EIO); /* EOS or actual I/O error */
     }
 
     /* get the length of the EBML number */
@@ -485,13 +485,13 @@ ebml_read_ascii (MatroskaDemuxContext *matroska,
      * byte more, read the string and NULL-terminate it ourselves. */
     if (size < 0 || !(*str = av_malloc(size + 1))) {
         av_log(matroska->ctx, AV_LOG_ERROR, "Memory allocation failed\n");
-        return AVERROR_NOMEM;
+        return AVERROR(ENOMEM);
     }
     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. %"PRIu64" (0x%"PRIx64")\n", pos, pos);
-        return AVERROR_IO;
+        return AVERROR(EIO);
     }
     (*str)[size] = '\0';
 
@@ -547,7 +547,7 @@ ebml_read_master (MatroskaDemuxContext *matroska,
     if (matroska->num_levels >= EBML_MAX_DEPTH) {
         av_log(matroska->ctx, AV_LOG_ERROR,
                "File moves beyond max. allowed depth (%d)\n", EBML_MAX_DEPTH);
-        return AVERROR_NOTSUPP;
+        return AVERROR(ENOSYS);
     }
 
     /* remember level */
@@ -581,14 +581,14 @@ ebml_read_binary (MatroskaDemuxContext *matroska,
     if (!(*binary = av_malloc(*size))) {
         av_log(matroska->ctx, AV_LOG_ERROR,
                "Memory allocation error\n");
-        return AVERROR_NOMEM;
+        return AVERROR(ENOMEM);
     }
 
     if (get_buffer(pb, *binary, *size) != *size) {
         offset_t pos = url_ftell(pb);
         av_log(matroska->ctx, AV_LOG_ERROR,
                "Read error at pos. %"PRIu64" (0x%"PRIx64")\n", pos, pos);
-        return AVERROR_IO;
+        return AVERROR(EIO);
     }
 
     return 0;
@@ -693,7 +693,7 @@ ebml_read_header (MatroskaDemuxContext *matroska,
 
     while (res == 0) {
         if (!(id = ebml_peek_id(matroska, &level_up)))
-            return AVERROR_IO;
+            return AVERROR(EIO);
 
         /* end-of-header */
         if (level_up)
@@ -903,7 +903,7 @@ matroska_parse_info (MatroskaDemuxContext *matroska)
 
     while (res == 0) {
         if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
-            res = AVERROR_IO;
+            res = AVERROR(EIO);
             break;
         } else if (matroska->level_up) {
             matroska->level_up--;
@@ -1002,7 +1002,7 @@ matroska_add_stream (MatroskaDemuxContext *matroska)
     /* try reading the trackentry headers */
     while (res == 0) {
         if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
-            res = AVERROR_IO;
+            res = AVERROR(EIO);
             break;
         } else if (matroska->level_up > 0) {
             matroska->level_up--;
@@ -1075,7 +1075,7 @@ matroska_add_stream (MatroskaDemuxContext *matroska)
 
                 while (res == 0) {
                     if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
-                        res = AVERROR_IO;
+                        res = AVERROR(EIO);
                         break;
                     } else if (matroska->level_up > 0) {
                         matroska->level_up--;
@@ -1089,7 +1089,7 @@ matroska_add_stream (MatroskaDemuxContext *matroska)
                             if ((res = ebml_read_uint (matroska, &id,
                                                        &num)) < 0)
                                 break;
-                            track->default_duration = num/matroska->time_scale;
+                            track->default_duration = num;
                             break;
                         }
 
@@ -1099,7 +1099,8 @@ matroska_add_stream (MatroskaDemuxContext *matroska)
                             if ((res = ebml_read_float(matroska, &id,
                                                        &num)) < 0)
                                 break;
-                            track->default_duration = 1000000000/(matroska->time_scale*num);
+                            if (!track->default_duration)
+                                track->default_duration = 1000000000/num;
                             break;
                         }
 
@@ -1246,7 +1247,7 @@ matroska_add_stream (MatroskaDemuxContext *matroska)
 
                 while (res == 0) {
                     if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
-                        res = AVERROR_IO;
+                        res = AVERROR(EIO);
                         break;
                     } else if (matroska->level_up > 0) {
                         matroska->level_up--;
@@ -1406,7 +1407,7 @@ matroska_add_stream (MatroskaDemuxContext *matroska)
                 uint64_t num;
                 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
                     break;
-                track->default_duration = num / matroska->time_scale;
+                track->default_duration = num;
                 break;
             }
 
@@ -1444,7 +1445,7 @@ matroska_parse_tracks (MatroskaDemuxContext *matroska)
 
     while (res == 0) {
         if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
-            res = AVERROR_IO;
+            res = AVERROR(EIO);
             break;
         } else if (matroska->level_up) {
             matroska->level_up--;
@@ -1487,7 +1488,7 @@ matroska_parse_index (MatroskaDemuxContext *matroska)
 
     while (res == 0) {
         if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
-            res = AVERROR_IO;
+            res = AVERROR(EIO);
             break;
         } else if (matroska->level_up) {
             matroska->level_up--;
@@ -1508,7 +1509,7 @@ matroska_parse_index (MatroskaDemuxContext *matroska)
 
                 while (res == 0) {
                     if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
-                        res = AVERROR_IO;
+                        res = AVERROR(EIO);
                         break;
                     } else if (matroska->level_up) {
                         matroska->level_up--;
@@ -1535,7 +1536,7 @@ matroska_parse_index (MatroskaDemuxContext *matroska)
                             while (res == 0) {
                                 if (!(id = ebml_peek_id (matroska,
                                                     &matroska->level_up))) {
-                                    res = AVERROR_IO;
+                                    res = AVERROR(EIO);
                                     break;
                                 } else if (matroska->level_up) {
                                     matroska->level_up--;
@@ -1642,7 +1643,7 @@ matroska_parse_metadata (MatroskaDemuxContext *matroska)
 
     while (res == 0) {
         if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
-            res = AVERROR_IO;
+            res = AVERROR(EIO);
             break;
         } else if (matroska->level_up) {
             matroska->level_up--;
@@ -1680,7 +1681,7 @@ matroska_parse_seekhead (MatroskaDemuxContext *matroska)
 
     while (res == 0) {
         if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
-            res = AVERROR_IO;
+            res = AVERROR(EIO);
             break;
         } else if (matroska->level_up) {
             matroska->level_up--;
@@ -1697,7 +1698,7 @@ matroska_parse_seekhead (MatroskaDemuxContext *matroska)
 
                 while (res == 0) {
                     if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
-                        res = AVERROR_IO;
+                        res = AVERROR(EIO);
                         break;
                     } else if (matroska->level_up) {
                         matroska->level_up--;
@@ -1913,7 +1914,7 @@ matroska_read_header (AVFormatContext    *s,
     /* The next thing is a segment. */
     while (1) {
         if (!(id = ebml_peek_id(matroska, &last_level)))
-            return AVERROR_IO;
+            return AVERROR(EIO);
         if (id == MATROSKA_ID_SEGMENT)
             break;
 
@@ -1936,7 +1937,7 @@ matroska_read_header (AVFormatContext    *s,
     /* we've found our segment, start reading the different contents in here */
     while (res == 0) {
         if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
-            res = AVERROR_IO;
+            res = AVERROR(EIO);
             break;
         } else if (matroska->level_up) {
             matroska->level_up--;
@@ -2074,7 +2075,7 @@ matroska_read_header (AVFormatContext    *s,
                 int sri = matroska_aac_sri(audiotrack->internal_samplerate);
                 extradata = av_malloc(5);
                 if (extradata == NULL)
-                    return AVERROR_NOMEM;
+                    return AVERROR(ENOMEM);
                 extradata[0] = (profile << 3) | ((sri&0x0E) >> 1);
                 extradata[1] = ((sri&0x01) << 7) | (audiotrack->channels<<3);
                 if (strstr(track->codec_id, "SBR")) {
@@ -2086,7 +2087,6 @@ matroska_read_header (AVFormatContext    *s,
                 } else {
                     extradata_size = 2;
                 }
-                track->default_duration = 1024*1000 / audiotrack->internal_samplerate;
             }
 
             else if (codec_id == CODEC_ID_TTA) {
@@ -2095,7 +2095,7 @@ matroska_read_header (AVFormatContext    *s,
                 extradata_size = 30;
                 extradata = av_mallocz(extradata_size);
                 if (extradata == NULL)
-                    return AVERROR_NOMEM;
+                    return AVERROR(ENOMEM);
                 init_put_byte(&b, extradata, extradata_size, 1,
                               NULL, NULL, NULL, NULL);
                 put_buffer(&b, (uint8_t *) "TTA1", 4);
@@ -2110,7 +2110,6 @@ matroska_read_header (AVFormatContext    *s,
                      codec_id == CODEC_ID_RV30 || codec_id == CODEC_ID_RV40) {
                 extradata_offset = 26;
                 track->codec_priv_size -= extradata_offset;
-                track->flags |= MATROSKA_TRACK_REAL_V;
             }
 
             else if (codec_id == CODEC_ID_RA_144) {
@@ -2147,7 +2146,9 @@ matroska_read_header (AVFormatContext    *s,
             else if (codec_id == CODEC_ID_TEXT) {
                 MatroskaSubtitleTrack *subtrack=(MatroskaSubtitleTrack *)track;
                 if (!strcmp(track->codec_id, "S_TEXT/ASS") ||
-                    !strcmp(track->codec_id, "S_TEXT/SSA"))
+                    !strcmp(track->codec_id, "S_TEXT/SSA") ||
+                    !strcmp(track->codec_id, "S_ASS") ||
+                    !strcmp(track->codec_id, "S_SSA"))
                     subtrack->ass = 1;
             }
 
@@ -2162,7 +2163,7 @@ matroska_read_header (AVFormatContext    *s,
             matroska->num_streams++;
             st = av_new_stream(s, track->stream_index);
             if (st == NULL)
-                return AVERROR_NOMEM;
+                return AVERROR(ENOMEM);
             av_set_pts_info(st, 64, matroska->time_scale, 1000*1000*1000); /* 64 bit pts in ns */
 
             st->codec->codec_id = codec_id;
@@ -2172,7 +2173,7 @@ matroska_read_header (AVFormatContext    *s,
 
             if (track->default_duration)
                 av_reduce(&st->codec->time_base.num, &st->codec->time_base.den,
-                          track->default_duration, 1000, 30000);
+                          track->default_duration, 1000000000, 30000);
 
             if(extradata){
                 st->codec->extradata = extradata;
@@ -2180,7 +2181,7 @@ matroska_read_header (AVFormatContext    *s,
             } else if(track->codec_priv && track->codec_priv_size > 0){
                 st->codec->extradata = av_malloc(track->codec_priv_size);
                 if(st->codec->extradata == NULL)
-                    return AVERROR_NOMEM;
+                    return AVERROR(ENOMEM);
                 st->codec->extradata_size = track->codec_priv_size;
                 memcpy(st->codec->extradata,track->codec_priv+extradata_offset,
                        track->codec_priv_size);
@@ -2235,12 +2236,6 @@ matroska_read_header (AVFormatContext    *s,
     return res;
 }
 
-static inline int
-rv_offset(uint8_t *data, int slice, int slices)
-{
-    return AV_RL32(data+8*slice+4) + 8*slices;
-}
-
 static int
 matroska_parse_block(MatroskaDemuxContext *matroska, uint8_t *data, int size,
                      int64_t pos, uint64_t cluster_time, uint64_t duration,
@@ -2281,7 +2276,7 @@ matroska_parse_block(MatroskaDemuxContext *matroska, uint8_t *data, int size,
         return res;
     }
     if (duration == AV_NOPTS_VALUE)
-        duration = matroska->tracks[track]->default_duration;
+        duration = matroska->tracks[track]->default_duration / matroska->time_scale;
 
     /* block_time (relative to cluster time) */
     block_time = AV_RB16(data);
@@ -2289,7 +2284,7 @@ matroska_parse_block(MatroskaDemuxContext *matroska, uint8_t *data, int size,
     flags = *data++;
     size -= 3;
     if (is_keyframe == -1)
-        is_keyframe = flags & 1 ? PKT_FLAG_KEY : 0;
+        is_keyframe = flags & 0x80 ? PKT_FLAG_KEY : 0;
 
     if (matroska->skip_to_keyframe) {
         if (!is_keyframe || st != matroska->skip_to_stream)
@@ -2377,98 +2372,81 @@ matroska_parse_block(MatroskaDemuxContext *matroska, uint8_t *data, int size,
     }
 
     if (res == 0) {
-        int real_v = matroska->tracks[track]->flags & MATROSKA_TRACK_REAL_V;
         uint64_t timecode = AV_NOPTS_VALUE;
 
-        if (cluster_time != (uint64_t)-1 && cluster_time + block_time >= 0)
+        if (cluster_time != (uint64_t)-1
+            && (block_time >= 0 || cluster_time >= -block_time))
             timecode = cluster_time + block_time;
 
         for (n = 0; n < laces; n++) {
-            int slice, slices = 1;
-
-            if (real_v) {
-                slices = *data++ + 1;
-                lace_size[n]--;
-            }
-
-            for (slice=0; slice<slices; slice++) {
-                int slice_size, slice_offset = 0;
-                if (real_v)
-                    slice_offset = rv_offset(data, slice, slices);
-                if (slice+1 == slices)
-                    slice_size = lace_size[n] - slice_offset;
-                else
-                    slice_size = rv_offset(data, slice+1, slices) - slice_offset;
-
-                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];
-                    int a = st->codec->block_align;
-                    int sps = audiotrack->sub_packet_size;
-                    int cfs = audiotrack->coded_framesize;
-                    int h = audiotrack->sub_packet_h;
-                    int y = audiotrack->sub_packet_cnt;
-                    int w = audiotrack->frame_size;
-                    int x;
-
-                    if (!audiotrack->pkt_cnt) {
-                        if (st->codec->codec_id == CODEC_ID_RA_288)
-                            for (x=0; x<h/2; x++)
-                                memcpy(audiotrack->buf+x*2*w+y*cfs,
-                                       data+x*cfs, cfs);
-                        else
-                            for (x=0; x<w/sps; x++)
-                                memcpy(audiotrack->buf+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), data+x*sps, sps);
-
-                        if (++audiotrack->sub_packet_cnt >= h) {
-                            audiotrack->sub_packet_cnt = 0;
-                            audiotrack->pkt_cnt = h*w / a;
-                        }
-                    }
-                    while (audiotrack->pkt_cnt) {
-                        pkt = av_mallocz(sizeof(AVPacket));
-                        av_new_packet(pkt, a);
-                        memcpy(pkt->data, audiotrack->buf
-                               + a * (h*w / a - audiotrack->pkt_cnt--), a);
-                        pkt->pos = pos;
-                        pkt->stream_index = matroska->tracks[track]->stream_index;
-                        matroska_queue_packet(matroska, pkt);
-                    }
-                } else {
-                    int offset = 0;
-
-                    if (st->codec->codec_id == CODEC_ID_TEXT
-                        && ((MatroskaSubtitleTrack *)(matroska->tracks[track]))->ass) {
-                        int i;
-                        for (i=0; i<8 && data[slice_offset+offset]; offset++)
-                            if (data[slice_offset+offset] == ',')
-                                i++;
+            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];
+                int a = st->codec->block_align;
+                int sps = audiotrack->sub_packet_size;
+                int cfs = audiotrack->coded_framesize;
+                int h = audiotrack->sub_packet_h;
+                int y = audiotrack->sub_packet_cnt;
+                int w = audiotrack->frame_size;
+                int x;
+
+                if (!audiotrack->pkt_cnt) {
+                    if (st->codec->codec_id == CODEC_ID_RA_288)
+                        for (x=0; x<h/2; x++)
+                            memcpy(audiotrack->buf+x*2*w+y*cfs,
+                                   data+x*cfs, cfs);
+                    else
+                        for (x=0; x<w/sps; x++)
+                            memcpy(audiotrack->buf+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), data+x*sps, sps);
+
+                    if (++audiotrack->sub_packet_cnt >= h) {
+                        audiotrack->sub_packet_cnt = 0;
+                        audiotrack->pkt_cnt = h*w / a;
                     }
-
+                }
+                while (audiotrack->pkt_cnt) {
                     pkt = av_mallocz(sizeof(AVPacket));
-                    /* XXX: prevent data copy... */
-                    if (av_new_packet(pkt, slice_size-offset) < 0) {
-                        res = AVERROR_NOMEM;
-                        n = laces-1;
-                        break;
-                    }
-                    memcpy (pkt->data, data+slice_offset+offset, slice_size-offset);
-
-                    if (n == 0)
-                        pkt->flags = is_keyframe;
-                    pkt->stream_index = matroska->tracks[track]->stream_index;
-
-                    pkt->pts = timecode;
+                    av_new_packet(pkt, a);
+                    memcpy(pkt->data, audiotrack->buf
+                           + a * (h*w / a - audiotrack->pkt_cnt--), a);
                     pkt->pos = pos;
-                    pkt->duration = duration;
-
+                    pkt->stream_index = matroska->tracks[track]->stream_index;
                     matroska_queue_packet(matroska, pkt);
                 }
+            } else {
+                int offset = 0;
+
+                if (st->codec->codec_id == CODEC_ID_TEXT
+                    && ((MatroskaSubtitleTrack *)(matroska->tracks[track]))->ass) {
+                    int i;
+                    for (i=0; i<8 && data[offset]; offset++)
+                        if (data[offset] == ',')
+                            i++;
+                }
 
-                if (timecode != AV_NOPTS_VALUE)
-                    timecode = duration ? timecode + duration : AV_NOPTS_VALUE;
+                pkt = av_mallocz(sizeof(AVPacket));
+                /* XXX: prevent data copy... */
+                if (av_new_packet(pkt, lace_size[n]-offset) < 0) {
+                    res = AVERROR(ENOMEM);
+                    n = laces-1;
+                    break;
+                }
+                memcpy (pkt->data, data+offset, lace_size[n]-offset);
+
+                if (n == 0)
+                    pkt->flags = is_keyframe;
+                pkt->stream_index = matroska->tracks[track]->stream_index;
+
+                pkt->pts = timecode;
+                pkt->pos = pos;
+                pkt->duration = duration;
+
+                matroska_queue_packet(matroska, pkt);
             }
+
+            if (timecode != AV_NOPTS_VALUE)
+                timecode = duration ? timecode + duration : AV_NOPTS_VALUE;
             data += lace_size[n];
         }
     }
@@ -2495,7 +2473,7 @@ matroska_parse_blockgroup (MatroskaDemuxContext *matroska,
 
     while (res == 0) {
         if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
-            res = AVERROR_IO;
+            res = AVERROR(EIO);
             break;
         } else if (matroska->level_up) {
             matroska->level_up--;
@@ -2573,7 +2551,7 @@ matroska_parse_cluster (MatroskaDemuxContext *matroska)
 
     while (res == 0) {
         if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
-            res = AVERROR_IO;
+            res = AVERROR(EIO);
             break;
         } else if (matroska->level_up) {
             matroska->level_up--;
@@ -2638,12 +2616,12 @@ matroska_read_packet (AVFormatContext *s,
 
         /* Have we already reached the end? */
         if (matroska->done)
-            return AVERROR_IO;
+            return AVERROR(EIO);
 
         res = 0;
         while (res == 0) {
             if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
-                return AVERROR_IO;
+                return AVERROR(EIO);
             } else if (matroska->level_up) {
                 matroska->level_up--;
                 break;