]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/utils.c
rename last_frame_offset to next_frame_offset, due to r13615
[ffmpeg] / libavformat / utils.c
index d98db39632eacf84a46c3561da28e6ce682bdc79..a12f26afd78496174bf04cf882e66e60ccfb6468 100644 (file)
@@ -703,7 +703,7 @@ static void compute_pkt_fields(AVFormatContext *s, AVStream *st,
 
 //    av_log(NULL, AV_LOG_DEBUG, "IN delayed:%d pts:%"PRId64", dts:%"PRId64" cur_dts:%"PRId64" st:%d pc:%p\n", presentation_delayed, pkt->pts, pkt->dts, st->cur_dts, pkt->stream_index, pc);
     /* interpolate PTS and DTS if they are not present */
-    if(delay <=1){
+    if(delay==0 || (delay==1 && pc)){
         if (presentation_delayed) {
             /* DTS = decompression timestamp */
             /* PTS = presentation timestamp */
@@ -850,6 +850,18 @@ static int av_read_frame_internal(AVFormatContext *s, AVPacket *pkt)
                 return ret;
             }
 
+            if(s->cur_pkt.pts != AV_NOPTS_VALUE &&
+               s->cur_pkt.dts != AV_NOPTS_VALUE &&
+               s->cur_pkt.pts < s->cur_pkt.dts){
+                av_log(s, AV_LOG_WARNING, "Invalid timestamps stream=%d, pts=%"PRId64", dts=%"PRId64", size=%d\n",
+                    s->cur_pkt.stream_index,
+                    s->cur_pkt.pts,
+                    s->cur_pkt.dts,
+                    s->cur_pkt.size);
+//                av_free_packet(&s->cur_pkt);
+//                return -1;
+            }
+
             st = s->streams[s->cur_pkt.stream_index];
             if(st->codec->debug & FF_DEBUG_PTS)
                 av_log(s, AV_LOG_DEBUG, "av_read_packet stream=%d, pts=%"PRId64", dts=%"PRId64", size=%d\n",
@@ -870,7 +882,7 @@ static int av_read_frame_internal(AVFormatContext *s, AVPacket *pkt)
                     st->parser->flags |= PARSER_FLAG_COMPLETE_FRAMES;
                 }
                 if(st->parser && (s->iformat->flags & AVFMT_GENERIC_INDEX)){
-                    st->parser->last_frame_offset=
+                    st->parser->next_frame_offset=
                     st->parser->cur_offset= s->cur_pkt.pos;
                 }
             }
@@ -1342,7 +1354,8 @@ static int av_seek_frame_generic(AVFormatContext *s,
         int i;
         AVPacket pkt;
 
-        if(st->index_entries && st->nb_index_entries){
+        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);
             av_update_cur_dts(s, st, ie->timestamp);
@@ -1793,6 +1806,26 @@ enum CodecID av_codec_get_id(const AVCodecTag *tags[4], unsigned int tag)
     return CODEC_ID_NONE;
 }
 
+static void compute_chapters_end(AVFormatContext *s)
+{
+    unsigned int i;
+
+    for (i=0; i+1<s->nb_chapters; i++)
+        if (s->chapters[i]->end == AV_NOPTS_VALUE) {
+            assert(s->chapters[i]->start <= s->chapters[i+1]->start);
+            assert(!av_cmp_q(s->chapters[i]->time_base, s->chapters[i+1]->time_base));
+            s->chapters[i]->end = s->chapters[i+1]->start;
+        }
+
+    if (s->nb_chapters && s->chapters[i]->end == AV_NOPTS_VALUE) {
+        assert(s->start_time != AV_NOPTS_VALUE);
+        assert(s->duration > 0);
+        s->chapters[i]->end = av_rescale_q(s->start_time + s->duration,
+                                           AV_TIME_BASE_Q,
+                                           s->chapters[i]->time_base);
+    }
+}
+
 /* absolute maximum size we read until we abort */
 #define MAX_READ_SIZE        5000000
 
@@ -2063,6 +2096,8 @@ int av_find_stream_info(AVFormatContext *ic)
         url_fseek(ic->pb, ic->data_offset, SEEK_SET);
     }
 
+    compute_chapters_end(ic);
+
 #if 0
     /* correct DTS for B-frame streams with no timestamps */
     for(i=0;i<ic->nb_streams;i++) {
@@ -2148,9 +2183,9 @@ void av_close_input_stream(AVFormatContext *s)
     av_freep(&s->programs);
     flush_packet_queue(s);
     av_freep(&s->priv_data);
-    while(s->num_chapters--) {
-        av_free(s->chapters[s->num_chapters]->title);
-        av_free(s->chapters[s->num_chapters]);
+    while(s->nb_chapters--) {
+        av_free(s->chapters[s->nb_chapters]->title);
+        av_free(s->chapters[s->nb_chapters]);
     }
     av_freep(&s->chapters);
     av_free(s);
@@ -2234,18 +2269,29 @@ void av_set_program_name(AVProgram *program, char *provider_name, char *name)
     }
 }
 
-int ff_new_chapter(AVFormatContext *s, int64_t start, int64_t end, const char *title)
+AVChapter *ff_new_chapter(AVFormatContext *s, int id, AVRational time_base, int64_t start, int64_t end, const char *title)
 {
-    AVChapter *chapter = av_mallocz(sizeof(AVChapter));
-    if(!chapter)
-        return AVERROR(ENOMEM);
+    AVChapter *chapter = NULL;
+    int i;
+
+    for(i=0; i<s->nb_chapters; i++)
+        if(s->chapters[i]->id == id)
+            chapter = s->chapters[i];
+
+    if(!chapter){
+        chapter= av_mallocz(sizeof(AVChapter));
+        if(!chapter)
+            return NULL;
+        dynarray_add(&s->chapters, &s->nb_chapters, chapter);
+    }
+    av_free(chapter->title);
     chapter->title = av_strdup(title);
+    chapter->id    = id;
+    chapter->time_base= time_base;
     chapter->start = start;
-    chapter->end = end;
+    chapter->end   = end;
 
-    dynarray_add(&s->chapters, &s->num_chapters, chapter);
-
-    return 0;
+    return chapter;
 }
 
 /************************************************************/