]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/mpeg.c
Warn user about invalid timestamps.
[ffmpeg] / libavformat / mpeg.c
index 3101c2a30f919c40af01262efb4a23a253d902a5..3f508dcddf6db22e58a412402b8d239d7b5b0896 100644 (file)
@@ -120,17 +120,12 @@ static int mpegps_read_header(AVFormatContext *s,
 
 static int64_t get_pts(ByteIOContext *pb, int c)
 {
-    int64_t pts;
-    int val;
-
-    if (c < 0)
-        c = get_byte(pb);
-    pts = (int64_t)(c & 0x0e) << 29;
-    val = get_be16(pb);
-    pts |= (int64_t)(val >> 1) << 15;
-    val = get_be16(pb);
-    pts |= (int64_t)(val >> 1);
-    return pts;
+    uint8_t buf[5];
+
+    buf[0] = c<0 ? get_byte(pb) : c;
+    get_buffer(pb, buf+1, 4);
+
+    return ff_parse_pes_pts(buf);
 }
 
 static int find_next_start_code(ByteIOContext *pb, int *size_ptr,
@@ -339,15 +334,20 @@ static int mpegps_read_pes_header(AVFormatContext *s,
                 header_len -= 5;
             }
         }
+        if (flags & 0x3f && header_len == 0){
+            flags &= 0xC0;
+            av_log(s, AV_LOG_WARNING, "Further flags set but no bytes left\n");
+        }
         if (flags & 0x01) { /* PES extension */
             pes_ext = get_byte(s->pb);
             header_len--;
-            if (pes_ext & 0x40) { /* pack header - should be zero in PS */
-                goto error_redo;
-            }
             /* Skip PES private data, program packet sequence counter and P-STD buffer */
             skip = (pes_ext >> 4) & 0xb;
             skip += skip & 0x9;
+            if (pes_ext & 0x40 || skip > header_len){
+                av_log(s, AV_LOG_WARNING, "pes_ext %X is invalid\n", pes_ext);
+                pes_ext=skip=0;
+            }
             url_fskip(s->pb, skip);
             header_len -= skip;
 
@@ -390,7 +390,9 @@ static int mpegps_read_pes_header(AVFormatContext *s,
     if(dts != AV_NOPTS_VALUE && ppos){
         int i;
         for(i=0; i<s->nb_streams; i++){
-            if(startcode == s->streams[i]->id) {
+            if(startcode == s->streams[i]->id &&
+               !url_is_streamed(s->pb) /* index useless on streams anyway */) {
+                ff_reduce_index(s, i);
                 av_add_index_entry(s->streams[i], *ppos, dts, 0, 0, AVINDEX_KEYFRAME /* FIXME keyframe? */);
             }
         }
@@ -472,7 +474,8 @@ static int mpegps_read_packet(AVFormatContext *s,
         codec_id = CODEC_ID_DTS;
     } else if (startcode >= 0xa0 && startcode <= 0xaf) {
         type = CODEC_TYPE_AUDIO;
-        codec_id = CODEC_ID_PCM_S16BE;
+        /* 16 bit form will be handled as CODEC_ID_PCM_S16BE */
+        codec_id = CODEC_ID_PCM_DVD;
     } else if (startcode >= 0xb0 && startcode <= 0xbf) {
         type = CODEC_TYPE_AUDIO;
         codec_id = CODEC_ID_MLP;
@@ -517,7 +520,14 @@ static int mpegps_read_packet(AVFormatContext *s,
         freq = (b1 >> 4) & 3;
         st->codec->sample_rate = lpcm_freq_tab[freq];
         st->codec->channels = 1 + (b1 & 7);
-        st->codec->bit_rate = st->codec->channels * st->codec->sample_rate * 2;
+        st->codec->bits_per_sample = 16 + ((b1 >> 6) & 3) * 4;
+        st->codec->bit_rate = st->codec->channels *
+                              st->codec->sample_rate *
+                              st->codec->bits_per_sample;
+        if (st->codec->bits_per_sample == 16)
+            st->codec->codec_id = CODEC_ID_PCM_S16BE;
+        else if (st->codec->bits_per_sample == 28)
+            return AVERROR(EINVAL);
     }
     av_new_packet(pkt, len);
     get_buffer(s->pb, pkt->data, pkt->size);