]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/utils.c
Add missing header #includes.
[ffmpeg] / libavformat / utils.c
index 329228db92831d6da1fa75b4820234a171ccc2aa..d1647d2a06fff07380add546d9e94534b39b1b09 100644 (file)
@@ -299,10 +299,14 @@ static int set_codec_from_probe_data(AVStream *st, AVProbeData *pd, int score)
     fmt = av_probe_input_format2(pd, 1, &score);
 
     if (fmt) {
-        if (strncmp(fmt->name, "mp3", 3) == 0)
+        if (!strcmp(fmt->name, "mp3"))
             st->codec->codec_id = CODEC_ID_MP3;
-        else if (strncmp(fmt->name, "ac3", 3) == 0)
+        else if (!strcmp(fmt->name, "ac3"))
             st->codec->codec_id = CODEC_ID_AC3;
+        else if (!strcmp(fmt->name, "mpegvideo"))
+            st->codec->codec_id = CODEC_ID_MPEG2VIDEO;
+        else if (!strcmp(fmt->name, "h264"))
+            st->codec->codec_id = CODEC_ID_H264;
     }
     return !!fmt;
 }
@@ -540,25 +544,59 @@ int av_read_packet(AVFormatContext *s, AVPacket *pkt)
 {
     int ret;
     AVStream *st;
-    av_init_packet(pkt);
-    ret= s->iformat->read_packet(s, pkt);
-    if (ret < 0)
-        return ret;
-    st= s->streams[pkt->stream_index];
 
-    switch(st->codec->codec_type){
-    case CODEC_TYPE_VIDEO:
-        if(s->video_codec_id)   st->codec->codec_id= s->video_codec_id;
-        break;
-    case CODEC_TYPE_AUDIO:
-        if(s->audio_codec_id)   st->codec->codec_id= s->audio_codec_id;
-        break;
-    case CODEC_TYPE_SUBTITLE:
-        if(s->subtitle_codec_id)st->codec->codec_id= s->subtitle_codec_id;
-        break;
-    }
+    for(;;){
+        AVPacketList *pktl = s->raw_packet_buffer;
 
-    return ret;
+        if (pktl) {
+            *pkt = pktl->pkt;
+            if(s->streams[pkt->stream_index]->codec->codec_id != CODEC_ID_PROBE){
+                s->raw_packet_buffer = pktl->next;
+                av_free(pktl);
+                return 0;
+            }
+        }
+
+        av_init_packet(pkt);
+        ret= s->iformat->read_packet(s, pkt);
+        if (ret < 0)
+            return ret;
+        st= s->streams[pkt->stream_index];
+
+        switch(st->codec->codec_type){
+        case CODEC_TYPE_VIDEO:
+            if(s->video_codec_id)   st->codec->codec_id= s->video_codec_id;
+            break;
+        case CODEC_TYPE_AUDIO:
+            if(s->audio_codec_id)   st->codec->codec_id= s->audio_codec_id;
+            break;
+        case CODEC_TYPE_SUBTITLE:
+            if(s->subtitle_codec_id)st->codec->codec_id= s->subtitle_codec_id;
+            break;
+        }
+
+        if(!pktl && st->codec->codec_id!=CODEC_ID_PROBE)
+            return ret;
+
+        add_to_pktbuf(&s->raw_packet_buffer, pkt);
+
+        if(st->codec->codec_id == CODEC_ID_PROBE){
+            AVProbeData *pd = &st->probe_data;
+
+            pd->buf = av_realloc(pd->buf, pd->buf_size+pkt->size+AVPROBE_PADDING_SIZE);
+            memcpy(pd->buf+pd->buf_size, pkt->data, pkt->size);
+            pd->buf_size += pkt->size;
+            memset(pd->buf+pd->buf_size, 0, AVPROBE_PADDING_SIZE);
+
+            if(av_log2(pd->buf_size) != av_log2(pd->buf_size - pkt->size)){
+                set_codec_from_probe_data(st, pd, 1);
+                if(st->codec->codec_id != CODEC_ID_PROBE){
+                    pd->buf_size=0;
+                    av_freep(&pd->buf);
+                }
+            }
+        }
+    }
 }
 
 /**********************************************************/
@@ -1392,7 +1430,7 @@ static int av_seek_frame_byte(AVFormatContext *s, int stream_index, int64_t pos,
 static int av_seek_frame_generic(AVFormatContext *s,
                                  int stream_index, int64_t timestamp, int flags)
 {
-    int index;
+    int index, ret;
     AVStream *st;
     AVIndexEntry *ie;
 
@@ -1407,11 +1445,13 @@ static int av_seek_frame_generic(AVFormatContext *s,
         if(st->nb_index_entries){
             assert(st->index_entries);
             ie= &st->index_entries[st->nb_index_entries-1];
-            url_fseek(s->pb, ie->pos, SEEK_SET);
+            if ((ret = url_fseek(s->pb, ie->pos, SEEK_SET)) < 0)
+                return ret;
             av_update_cur_dts(s, st, ie->timestamp);
-        }else
-            url_fseek(s->pb, 0, SEEK_SET);
-
+        }else{
+            if ((ret = url_fseek(s->pb, 0, SEEK_SET)) < 0)
+                return ret;
+        }
         for(i=0;; i++) {
             int ret = av_read_frame(s, &pkt);
             if(ret<0)
@@ -1433,8 +1473,8 @@ static int av_seek_frame_generic(AVFormatContext *s,
             return 0;
     }
     ie = &st->index_entries[index];
-    url_fseek(s->pb, ie->pos, SEEK_SET);
-
+    if ((ret = url_fseek(s->pb, ie->pos, SEEK_SET)) < 0)
+        return ret;
     av_update_cur_dts(s, st, ie->timestamp);
 
     return 0;
@@ -1904,8 +1944,6 @@ int av_find_stream_info(AVFormatContext *ic)
     offset_t old_offset = url_ftell(ic->pb);
     int64_t codec_info_duration[MAX_STREAMS]={0};
     int codec_info_nb_frames[MAX_STREAMS]={0};
-    AVProbeData probe_data[MAX_STREAMS];
-    int codec_identified[MAX_STREAMS]={0};
 
     duration_error = av_mallocz(MAX_STREAMS * sizeof(*duration_error));
     if (!duration_error) return AVERROR(ENOMEM);
@@ -1931,7 +1969,6 @@ int av_find_stream_info(AVFormatContext *ic)
         last_dts[i]= AV_NOPTS_VALUE;
     }
 
-    memset(probe_data, 0, sizeof(probe_data));
     count = 0;
     read_size = 0;
     for(;;) {
@@ -1985,8 +2022,10 @@ int av_find_stream_info(AVFormatContext *ic)
         }
 
         pkt= add_to_pktbuf(&ic->packet_buffer, &pkt1);
-        if(av_dup_packet(pkt) < 0)
+        if(av_dup_packet(pkt) < 0) {
+            av_free(duration_error);
             return AVERROR(ENOMEM);
+        }
 
         read_size += pkt->size;
 
@@ -2018,14 +2057,6 @@ int av_find_stream_info(AVFormatContext *ic)
             }
             if(last == AV_NOPTS_VALUE || duration_count[index]<=1)
                 last_dts[pkt->stream_index]= pkt->dts;
-
-            if (st->codec->codec_id == CODEC_ID_NONE) {
-                AVProbeData *pd = &(probe_data[st->index]);
-                pd->buf = av_realloc(pd->buf, pd->buf_size+pkt->size+AVPROBE_PADDING_SIZE);
-                memcpy(pd->buf+pd->buf_size, pkt->data, pkt->size);
-                pd->buf_size += pkt->size;
-                memset(pd->buf+pd->buf_size, 0, AVPROBE_PADDING_SIZE);
-            }
         }
         if(st->parser && st->parser->parser->split && !st->codec->extradata){
             int i= st->parser->parser->split(st->codec, pkt->data, pkt->size);
@@ -2105,12 +2136,6 @@ int av_find_stream_info(AVFormatContext *ic)
                 }
             }
         }else if(st->codec->codec_type == CODEC_TYPE_AUDIO) {
-            if (st->codec->codec_id == CODEC_ID_NONE && probe_data[st->index].buf_size > 0) {
-                codec_identified[st->index] = set_codec_from_probe_data(st, &(probe_data[st->index]), 1);
-                if (codec_identified[st->index]) {
-                    st->need_parsing = AVSTREAM_PARSE_FULL;
-                }
-            }
             if(!st->codec->bits_per_sample)
                 st->codec->bits_per_sample= av_get_bits_per_sample(st->codec->codec_id);
         }
@@ -2118,24 +2143,6 @@ int av_find_stream_info(AVFormatContext *ic)
 
     av_estimate_timings(ic, old_offset);
 
-    for(i=0;i<ic->nb_streams;i++) {
-        st = ic->streams[i];
-        if (codec_identified[st->index])
-            break;
-    }
-    //FIXME this is a mess
-    if(i!=ic->nb_streams){
-        av_read_frame_flush(ic);
-        for(i=0;i<ic->nb_streams;i++) {
-            st = ic->streams[i];
-            if (codec_identified[st->index]) {
-                av_seek_frame(ic, st->index, 0.0, 0);
-            }
-            st->cur_dts= st->first_dts;
-        }
-        url_fseek(ic->pb, ic->data_offset, SEEK_SET);
-    }
-
     compute_chapters_end(ic);
 
 #if 0
@@ -2164,9 +2171,6 @@ int av_find_stream_info(AVFormatContext *ic)
 #endif
 
     av_free(duration_error);
-    for(i=0;i<MAX_STREAMS;i++){
-        av_freep(&(probe_data[i].buf));
-    }
 
     return ret;
 }
@@ -3152,9 +3156,13 @@ void url_split(char *proto, int proto_size,
 void av_set_pts_info(AVStream *s, int pts_wrap_bits,
                      int pts_num, int pts_den)
 {
+    unsigned int gcd= ff_gcd(pts_num, pts_den);
     s->pts_wrap_bits = pts_wrap_bits;
-    s->time_base.num = pts_num;
-    s->time_base.den = pts_den;
+    s->time_base.num = pts_num/gcd;
+    s->time_base.den = pts_den/gcd;
+
+    if(gcd>1)
+        av_log(NULL, AV_LOG_DEBUG, "st:%d removing common factor %d from timebase\n", s->index, gcd);
 }
 
 /* fraction handling */