changeset 461:63540e5504f7 libavformat

bytestream tracing support sync points removing timestamp lru prediction extendible frame_code / frame_code table
author michael
date Thu, 20 May 2004 16:00:22 +0000
parents 6624afe8bf65
children b69898ffc92a
files nut.c
diffstat 1 files changed, 216 insertions(+), 114 deletions(-) [+]
line wrap: on
line diff
--- a/nut.c	Tue May 18 02:33:27 2004 +0000
+++ b/nut.c	Thu May 20 16:00:22 2004 +0000
@@ -39,6 +39,8 @@
 #undef NDEBUG
 #include <assert.h>
 
+//#define TRACE
+
 //from /dev/random
 
 #define     MAIN_STARTCODE (0x7A561F5F04ADULL + (((uint64_t)('N'<<8) + 'M')<<48)) 
@@ -50,17 +52,17 @@
 #define MAX_DISTANCE (1024*16-1)
 #define MAX_SHORT_DISTANCE (1024*4-1)
 
-#define MAX_PTS_LRU 3
-
-#define FLAG_DATA_SIZE       4
-#define FLAG_PTS             3
-#define FLAG_KEY_FRAME       8
+#define FLAG_DATA_SIZE       1
+#define FLAG_KEY_FRAME       2
+#define FLAG_INVALID         4
 
 typedef struct {
     uint8_t flags;
     uint8_t stream_id_plus1;
     uint16_t size_mul;
     uint16_t size_lsb;
+    int16_t timestamp_delta;
+    uint8_t reserved_count;
 } FrameCode;
 
 typedef struct {
@@ -69,8 +71,6 @@
     int rate_num;
     int rate_den;
     int64_t last_pts;
-    int lru_pts_delta[MAX_PTS_LRU];
-    int initial_pts_predictor[MAX_PTS_LRU];
     int64_t last_sync_pos;                    ///<pos of last 1/2 type frame
     int decode_delay;
 } StreamContext;
@@ -84,6 +84,9 @@
     uint64_t next_startcode;     ///< stores the next startcode if it has alraedy been parsed but the stream isnt seekable
     StreamContext *stream;
     int max_distance;
+    int rate_num;
+    int rate_den;
+    int short_startcode;
 } NUTContext;
 
 static char *info_table[][2]={
@@ -106,34 +109,15 @@
 	{"Cover"		, "PNG"},
 };
 
-static void update_lru(int *lru, int current, int count){
-    int i;
-
-    for(i=0; i<count-1; i++){
-        if(lru[i] == current)
-            break;
-    }
-
-    for(; i; i--){
-        lru[i]= lru[i-1];
-    }
-
-    lru[0]= current;
-}
-
 static void update(NUTContext *nut, int stream_index, int64_t frame_start, int frame_type, int frame_code, int key_frame, int size, int64_t pts){
     StreamContext *stream= &nut->stream[stream_index];
     
     stream->last_key_frame= key_frame;
     nut->packet_start[ frame_type ]= frame_start;
-    if(stream->last_pts != AV_NOPTS_VALUE){
-        assert(frame_type < 2);
-        update_lru(stream->lru_pts_delta, pts - stream->last_pts, 3);
-    }
     stream->last_pts= pts;
 }
 
-static void reset(AVFormatContext *s/*, int frame_type*/){
+static void reset(AVFormatContext *s, int64_t global_ts){
     NUTContext *nut = s->priv_data;
     int i;
     
@@ -141,24 +125,26 @@
         StreamContext *stream= &nut->stream[i];
     
         stream->last_key_frame= 1;
-        stream->last_pts= AV_NOPTS_VALUE;
-        memcpy(stream->lru_pts_delta, stream->initial_pts_predictor, sizeof(int)*MAX_PTS_LRU);
+
+        stream->last_pts= av_rescale(global_ts, stream->rate_num*(int64_t)nut->rate_den, stream->rate_den*(int64_t)nut->rate_num);
     }
 }
 
 static void build_frame_code(AVFormatContext *s){
     NUTContext *nut = s->priv_data;
-    int key_frame, index, pred, stream_id, full_pts;
+    int key_frame, index, pred, stream_id;
     int start=0;
     int end= 255;
     int keyframe_0_esc= s->nb_streams > 2;
+    int pred_table[10];
 
     if(keyframe_0_esc){
         /* keyframe = 0 escape */
         FrameCode *ft= &nut->frame_code[start];
-        ft->flags= FLAG_DATA_SIZE | FLAG_PTS; 
+        ft->flags= FLAG_DATA_SIZE;
         ft->stream_id_plus1= 0;
         ft->size_mul=1;
+        ft->timestamp_delta=0;
         start++;
     }
 
@@ -177,9 +163,10 @@
             {
                 FrameCode *ft= &nut->frame_code[start2];
                 ft->flags= FLAG_KEY_FRAME*key_frame;
-                ft->flags|= FLAG_DATA_SIZE | FLAG_PTS;
+                ft->flags|= FLAG_DATA_SIZE;
                 ft->stream_id_plus1= stream_id + 1;
                 ft->size_mul=1;
+                ft->timestamp_delta=0;
                 start2++;
             }
         }
@@ -188,15 +175,16 @@
 #if 1
         if(is_audio){
             int frame_bytes= codec->frame_size*(int64_t)codec->bit_rate / (8*codec->sample_rate);
-            for(full_pts=0; full_pts<2; full_pts++){
+            int pts;
+            for(pts=0; pts<2; pts++){
                 for(pred=0; pred<2; pred++){
                     FrameCode *ft= &nut->frame_code[start2];
-                    ft->flags= FLAG_KEY_FRAME*key_frame + FLAG_PTS*full_pts;
+                    ft->flags= FLAG_KEY_FRAME*key_frame;
                     ft->stream_id_plus1= stream_id + 1;
                     ft->size_mul=frame_bytes + 2;
                     ft->size_lsb=frame_bytes + pred;
+                    ft->timestamp_delta=pts;
                     start2++;
-                    av_log(NULL, AV_LOG_DEBUG, "%d\n", frame_bytes + pred);
                 }
             }
         }else{
@@ -204,28 +192,46 @@
             ft->flags= FLAG_KEY_FRAME | FLAG_DATA_SIZE;
             ft->stream_id_plus1= stream_id + 1;
             ft->size_mul=1;
+            ft->timestamp_delta=1;
             start2++;
         }
 #endif
-        pred_count= 2 + codec->has_b_frames + (codec->codec_id == CODEC_ID_VORBIS);
+
+        if(codec->has_b_frames){
+            pred_count=5;
+            pred_table[0]=-2;
+            pred_table[1]=-1;
+            pred_table[2]=1;
+            pred_table[3]=3;
+            pred_table[4]=4;
+        }else if(codec->codec_id == CODEC_ID_VORBIS){
+            pred_count=3;
+            pred_table[0]=2;
+            pred_table[1]=9;
+            pred_table[2]=16;
+        }else{
+            pred_count=1;
+            pred_table[0]=1;
+        }
+
         for(pred=0; pred<pred_count; pred++){
             int start3= start2 + (end2-start2)*pred / pred_count;
             int end3  = start2 + (end2-start2)*(pred+1) / pred_count;
 
             for(index=start3; index<end3; index++){
                 FrameCode *ft= &nut->frame_code[index];
-                ft->flags= FLAG_KEY_FRAME*key_frame + pred*4;
+                ft->flags= FLAG_KEY_FRAME*key_frame;
                 ft->flags|= FLAG_DATA_SIZE;
                 ft->stream_id_plus1= stream_id + 1;
 //FIXME use single byte size and pred from last
                 ft->size_mul= end3-start3;
                 ft->size_lsb= index - start3;
+                ft->timestamp_delta= pred_table[pred];
             }
-            nut->stream[stream_id].initial_pts_predictor[pred]= pred+1;
         }
     }
     memmove(&nut->frame_code['N'+1], &nut->frame_code['N'], sizeof(FrameCode)*(255-'N'));
-    nut->frame_code['N'].flags= 1;
+    nut->frame_code['N'].flags= FLAG_INVALID;
 }
 
 static uint64_t get_v(ByteIOContext *bc)
@@ -265,6 +271,13 @@
         return 0;
 }
 
+static int64_t get_s(ByteIOContext *bc){
+    int64_t v = get_v(bc) + 1;
+
+    if (v&1) return -(v>>1);
+    else     return  (v>>1);
+}
+
 static uint64_t get_vb(ByteIOContext *bc){
     uint64_t val=0;
     int i= get_v(bc);
@@ -279,6 +292,33 @@
     return val;
 }
 
+#ifdef TRACE
+static inline uint64_t get_v_trace(ByteIOContext *bc, char *file, char *func, int line){
+    uint64_t v= get_v(bc);
+
+    printf("get_v %5lld / %llX in %s %s:%d\n", v, v, file, func, line);
+    return v;
+}
+
+static inline int64_t get_s_trace(ByteIOContext *bc, char *file, char *func, int line){
+    int64_t v= get_s(bc);
+
+    printf("get_s %5lld / %llX in %s %s:%d\n", v, v, file, func, line);
+    return v;
+}
+
+static inline uint64_t get_vb_trace(ByteIOContext *bc, char *file, char *func, int line){
+    uint64_t v= get_vb(bc);
+
+    printf("get_vb %5lld / %llX in %s %s:%d\n", v, v, file, func, line);
+    return v;
+}
+#define get_v(bc)  get_v_trace(bc, __FILE__, __PRETTY_FUNCTION__, __LINE__)
+#define get_s(bc)  get_s_trace(bc, __FILE__, __PRETTY_FUNCTION__, __LINE__)
+#define get_vb(bc)  get_vb_trace(bc, __FILE__, __PRETTY_FUNCTION__, __LINE__)
+#endif
+
+
 static int get_packetheader(NUTContext *nut, ByteIOContext *bc, int calculate_checksum)
 {
     int64_t start, size;
@@ -351,6 +391,7 @@
 }
 
 #ifdef CONFIG_ENCODERS
+
 static void put_v(ByteIOContext *bc, uint64_t val)
 {
     int i;
@@ -376,6 +417,11 @@
     put_buffer(bc, string, len);
 }
 
+static void put_s(ByteIOContext *bc, uint64_t val){
+    if (val<=0) put_v(bc, -2*val  );
+    else        put_v(bc,  2*val-1);
+}
+
 static void put_vb(ByteIOContext *bc, uint64_t val){
     int i;
     
@@ -386,6 +432,29 @@
         put_byte(bc, (val>>i)&0xFF);
 }
 
+#ifdef TRACE
+static inline void put_v_trace(ByteIOContext *bc, uint64_t v, char *file, char *func, int line){
+    printf("get_v %5lld / %llX in %s %s:%d\n", v, v, file, func, line);
+    
+    put_v(bc, v);
+}
+
+static inline void put_s_trace(ByteIOContext *bc, int64_t v, char *file, char *func, int line){
+    printf("get_s %5lld / %llX in %s %s:%d\n", v, v, file, func, line);
+    
+    put_s(bc, v);
+}
+
+static inline void put_vb_trace(ByteIOContext *bc, uint64_t v, char *file, char *func, int line){
+    printf("get_vb %5lld / %llX in %s %s:%d\n", v, v, file, func, line);
+    
+    put_vb(bc, v);
+}
+#define put_v(bc, v)  put_v_trace(bc, v, __FILE__, __PRETTY_FUNCTION__, __LINE__)
+#define put_s(bc, v)  put_s_trace(bc, v, __FILE__, __PRETTY_FUNCTION__, __LINE__)
+#define put_vb(bc, v)  put_vb_trace(bc, v, __FILE__, __PRETTY_FUNCTION__, __LINE__)
+#endif
+
 static int put_packetheader(NUTContext *nut, ByteIOContext *bc, int max_size, int calculate_checksum)
 {
     put_flush_packet(bc);
@@ -435,7 +504,7 @@
     NUTContext *nut = s->priv_data;
     ByteIOContext *bc = &s->pb;
     AVCodecContext *codec;
-    int i, j;
+    int i, j, tmp_time, tmp_flags,tmp_stream, tmp_mul, tmp_size, tmp_fields;
 
     nut->avf= s;
     
@@ -451,29 +520,48 @@
     put_v(bc, s->nb_streams);
     put_v(bc, MAX_DISTANCE);
     
+    put_v(bc, nut->rate_num=1);
+    put_v(bc, nut->rate_den=2);
+    put_v(bc, nut->short_startcode=0x4EFE79);
+    
     build_frame_code(s);
-    assert(nut->frame_code['N'].flags == 1);
+    assert(nut->frame_code['N'].flags == FLAG_INVALID);
+    
+    tmp_time= tmp_flags= tmp_stream= tmp_mul= tmp_size= /*tmp_res=*/ INT_MAX;
     for(i=0; i<256;){
-        int tmp_flags = nut->frame_code[i].flags;
-        int tmp_stream= nut->frame_code[i].stream_id_plus1;
-        int tmp_mul   = nut->frame_code[i].size_mul;
-        int tmp_size  = nut->frame_code[i].size_lsb;
-        put_v(bc, tmp_flags);
-        put_v(bc, tmp_stream);
-        put_v(bc, tmp_mul);
-        put_v(bc, tmp_size);
+        tmp_fields=0;
+        tmp_size= 0;
+        if(tmp_time   != nut->frame_code[i].timestamp_delta) tmp_fields=1;
+        if(tmp_mul    != nut->frame_code[i].size_mul       ) tmp_fields=2;
+        if(tmp_stream != nut->frame_code[i].stream_id_plus1) tmp_fields=3;
+        if(tmp_size   != nut->frame_code[i].size_lsb       ) tmp_fields=4;
+//        if(tmp_res    != nut->frame_code[i].res            ) tmp_fields=5;
 
+        tmp_time  = nut->frame_code[i].timestamp_delta;
+        tmp_flags = nut->frame_code[i].flags;
+        tmp_stream= nut->frame_code[i].stream_id_plus1;
+        tmp_mul   = nut->frame_code[i].size_mul;
+        tmp_size  = nut->frame_code[i].size_lsb;
+//        tmp_res   = nut->frame_code[i].res;
+        
         for(j=0; i<256; j++,i++){
+            if(nut->frame_code[i].timestamp_delta != tmp_time  ) break;
             if(nut->frame_code[i].flags           != tmp_flags ) break;
             if(nut->frame_code[i].stream_id_plus1 != tmp_stream) break;
             if(nut->frame_code[i].size_mul        != tmp_mul   ) break;
-            if(nut->frame_code[i].size_lsb        != tmp_size  ) break;
-            if(++tmp_size >= tmp_mul){
-                tmp_size=0;
-                tmp_stream++;
-            }
+            if(nut->frame_code[i].size_lsb        != tmp_size+j) break;
+//            if(nut->frame_code[i].res             != tmp_res   ) break;
         }
-        put_v(bc, j);
+        if(j != tmp_mul - tmp_size) tmp_fields=6;
+
+        put_v(bc, tmp_flags);
+        put_v(bc, tmp_fields);
+        if(tmp_fields>0) put_s(bc, tmp_time);
+        if(tmp_fields>1) put_v(bc, tmp_mul);
+        if(tmp_fields>2) put_v(bc, tmp_stream);
+        if(tmp_fields>3) put_v(bc, tmp_size);
+        if(tmp_fields>4) put_v(bc, 0 /*tmp_res*/);
+        if(tmp_fields>5) put_v(bc, j);
     }
 
     update_packetheader(nut, bc, 0, 1);
@@ -530,8 +618,6 @@
         else
 	    nut->stream[i].msb_timestamp_shift = 14;
 	put_v(bc, nut->stream[i].msb_timestamp_shift);
-	for(j=0; j<3; j++)
-		put_v(bc, nut->stream[i].initial_pts_predictor[j]);
         put_v(bc, codec->has_b_frames);
 	put_byte(bc, 0); /* flags: 0x1 - fixed_fps, 0x2 - index_present */
 	
@@ -615,18 +701,18 @@
     int key_frame = 0, full_pts=0;
     AVCodecContext *enc;
     int64_t coded_pts;
-    int frame_type, best_length, frame_code, flags, i, size_mul, size_lsb;
+    int frame_type, best_length, frame_code, flags, i, size_mul, size_lsb, time_delta;
     const int64_t frame_start= url_ftell(bc);
 
     if (stream_index > s->nb_streams)
 	return 1;
         
-    pts= (av_rescale(pts, stream->rate_num, stream->rate_den) + AV_TIME_BASE/2) / AV_TIME_BASE;
+    pts= av_rescale(pts, stream->rate_num, stream->rate_den*(int64_t)AV_TIME_BASE);
 
     enc = &s->streams[stream_index]->codec;
     key_frame = enc->coded_frame->key_frame;
     if(enc->coded_frame->pts != AV_NOPTS_VALUE)
-        pts= (av_rescale(enc->coded_frame->pts, stream->rate_num, stream->rate_den) + AV_TIME_BASE/2) / AV_TIME_BASE; //FIXME XXX HACK
+        pts= av_rescale(enc->coded_frame->pts, stream->rate_num, stream->rate_den*(int64_t)AV_TIME_BASE); //FIXME XXX HACK
     
     frame_type=0;
     if(frame_start + size + 20 - FFMAX(nut->packet_start[1], nut->packet_start[2]) > MAX_DISTANCE)
@@ -634,15 +720,16 @@
     if(key_frame && !stream->last_key_frame)
         frame_type=2;
 
-    if(frame_type>1)
-        reset(s);
-    if(stream->last_pts == AV_NOPTS_VALUE)
+    if(frame_type>1){
+        int64_t global_ts= av_rescale(pts, stream->rate_den*(int64_t)nut->rate_num, stream->rate_num*(int64_t)nut->rate_den);
+        reset(s, global_ts);
+	put_be64(bc, KEYFRAME_STARTCODE);
+        put_v(bc, global_ts);
+    }
+    assert(stream->last_pts != AV_NOPTS_VALUE);
+    coded_pts = pts & ((1<<stream->msb_timestamp_shift)-1);
+    if(lsb2full(stream, coded_pts) != pts)
         full_pts=1;
-    else{
-        coded_pts = pts & ((1<<stream->msb_timestamp_shift)-1);
-        if(lsb2full(stream, coded_pts) != pts)
-            full_pts=1;
-    }
 
     if(full_pts)
         coded_pts= pts + (1<<stream->msb_timestamp_shift);
@@ -655,6 +742,7 @@
         int length=0;
         size_mul= nut->frame_code[i].size_mul;
         size_lsb= nut->frame_code[i].size_lsb;
+        time_delta= nut->frame_code[i].timestamp_delta;
         flags= nut->frame_code[i].flags;
 
         assert(size_mul > size_lsb);
@@ -675,16 +763,14 @@
         }else if(size != size_lsb)
             continue;
 
-        if(full_pts && (flags & FLAG_PTS) != FLAG_PTS)
+        if(full_pts && time_delta)
             continue;
             
-        if(flags&FLAG_PTS){
+        if(!time_delta){
             length += get_length(coded_pts);
         }else{
-            int delta= stream->lru_pts_delta[flags & FLAG_PTS];
-            if(delta != pts - stream->last_pts)
+            if(time_delta != pts - stream->last_pts)
                 continue;
-            assert(frame_type < 2);
         }
 
         if(length < best_length){
@@ -698,28 +784,29 @@
     flags= nut->frame_code[frame_code].flags;
     size_mul= nut->frame_code[frame_code].size_mul;
     size_lsb= nut->frame_code[frame_code].size_lsb;
-#if 0
+    time_delta= nut->frame_code[frame_code].timestamp_delta;
+#ifdef TRACE
     best_length /= 7;
     best_length ++; //frame_code
     if(frame_type==2){
         best_length += 8; // startcode
     }
-    av_log(s, AV_LOG_DEBUG, "kf:%d ft:%d pt:%d fc:%2X len:%2d size:%d stream:%d flag:%d mul:%d lsb:%d s+1:%d pts_delta:%d pts:%lld\n", key_frame, frame_type, full_pts ? 2 : ((flags & FLAG_PTS) == FLAG_PTS ? 1 : 0), frame_code, best_length, size, stream_index, flags, size_mul, size_lsb, nut->frame_code[frame_code].stream_id_plus1,(int)(pts - stream->last_pts), pts);
+    av_log(s, AV_LOG_DEBUG, "kf:%d ft:%d pt:%d fc:%2X len:%2d size:%d stream:%d flag:%d mul:%d lsb:%d s+1:%d pts_delta:%d pts:%lld fs:%lld\n", key_frame, frame_type, full_pts ? 1 : 0, frame_code, best_length, size, stream_index, flags, size_mul, size_lsb, nut->frame_code[frame_code].stream_id_plus1,(int)(pts - stream->last_pts), pts, frame_start);
 //    av_log(s, AV_LOG_DEBUG, "%d %d %d\n", stream->lru_pts_delta[0], stream->lru_pts_delta[1], stream->lru_pts_delta[2]);
 #endif
 
-    if (frame_type==2)
-	put_be64(bc, KEYFRAME_STARTCODE);
     assert(frame_type != 1); //short startcode not implemented yet
     put_byte(bc, frame_code);
 
     if(nut->frame_code[frame_code].stream_id_plus1 == 0)
         put_v(bc, stream_index);
-    if ((flags & FLAG_PTS) == FLAG_PTS){
+    if (!time_delta){
         put_v(bc, coded_pts);
     }
     if(flags & FLAG_DATA_SIZE)
         put_v(bc, size / size_mul);
+    else
+        assert(size == size_lsb);
     if(size > MAX_DISTANCE){
         assert(frame_type > 1);
     }
@@ -776,7 +863,7 @@
     AVFormatContext *s= nut->avf;
     ByteIOContext *bc = &s->pb;
     uint64_t tmp;
-    int i, j;
+    int i, j, tmp_stream, tmp_mul, tmp_time, tmp_size, count, tmp_res;
     
     get_packetheader(nut, bc, 1);
 
@@ -788,36 +875,49 @@
     
     nut->stream_count = get_v(bc);
     nut->max_distance = get_v(bc);
+    nut->rate_num= get_v(bc);
+    nut->rate_den= get_v(bc);
+    nut->short_startcode= get_v(bc);
+    if(nut->short_startcode>>16 != 'N'){
+	av_log(s, AV_LOG_ERROR, "invalid short startcode %X\n", nut->short_startcode);
+        return -1;
+    }
     
     for(i=0; i<256;){
         int tmp_flags = get_v(bc);
-        int tmp_stream= get_v(bc);
-        int tmp_mul   = get_v(bc);
-        int tmp_size  = get_v(bc);
-        int count     = get_v(bc);
-
+        int tmp_fields= get_v(bc);
+        if(tmp_fields>0) tmp_time  = get_s(bc);
+        if(tmp_fields>1) tmp_mul   = get_v(bc);
+        if(tmp_fields>2) tmp_stream= get_v(bc);
+        if(tmp_fields>3) tmp_size  = get_v(bc);
+        else             tmp_size  = 0;
+        if(tmp_fields>4) tmp_res   = get_v(bc);
+        else             tmp_res   = 0;
+        if(tmp_fields>5) count     = get_v(bc);
+        else             count     = tmp_mul - tmp_size;
+        
+        while(tmp_fields-- > 6) 
+           get_v(bc);
+        
         if(count == 0 || i+count > 256){
             av_log(s, AV_LOG_ERROR, "illegal count %d at %d\n", count, i);
             return -1;
         }
+        if(tmp_stream > nut->stream_count + 1){
+            av_log(s, AV_LOG_ERROR, "illegal stream number\n");
+            return -1;
+        }
 
         for(j=0; j<count; j++,i++){
-            if(tmp_stream > nut->stream_count + 1){
-                av_log(s, AV_LOG_ERROR, "illegal stream number\n");
-                return -1;
-            }
-
             nut->frame_code[i].flags           = tmp_flags ;
+            nut->frame_code[i].timestamp_delta = tmp_time  ;
             nut->frame_code[i].stream_id_plus1 = tmp_stream;
             nut->frame_code[i].size_mul        = tmp_mul   ;
-            nut->frame_code[i].size_lsb        = tmp_size  ;
-            if(++tmp_size >= tmp_mul){
-                tmp_size=0;
-                tmp_stream++;
-            }
+            nut->frame_code[i].size_lsb        = tmp_size+j;
+            nut->frame_code[i].reserved_count  = tmp_res   ;
         }
     }
-    if(nut->frame_code['N'].flags != 1){
+    if(nut->frame_code['N'].flags != FLAG_INVALID){
         av_log(s, AV_LOG_ERROR, "illegal frame_code table\n");
         return -1;
     }
@@ -833,7 +933,7 @@
 static int decode_stream_header(NUTContext *nut){
     AVFormatContext *s= nut->avf;
     ByteIOContext *bc = &s->pb;
-    int class, nom, denom, stream_id, i;
+    int class, nom, denom, stream_id;
     uint64_t tmp;
     AVStream *st;
     
@@ -871,8 +971,6 @@
     nom = get_v(bc);
     denom = get_v(bc);
     nut->stream[stream_id].msb_timestamp_shift = get_v(bc);
-    for(i=0; i<3; i++)
-        nut->stream[stream_id].initial_pts_predictor[i]= get_v(bc);
     nut->stream[stream_id].decode_delay= get_v(bc);
     get_byte(bc); /* flags */
 
@@ -1024,14 +1122,12 @@
     return 0;
 }
 
-static int decode_frame_header(NUTContext *nut, int *key_frame_ret, int64_t *pts_ret, int *stream_id_ret, int frame_code, int frame_type){
+static int decode_frame_header(NUTContext *nut, int *key_frame_ret, int64_t *pts_ret, int *stream_id_ret, int frame_code, int frame_type, int64_t frame_start){
     AVFormatContext *s= nut->avf;
     StreamContext *stream;
     ByteIOContext *bc = &s->pb;
-    int size, flags, size_mul, size_lsb, stream_id;
+    int size, flags, size_mul, size_lsb, stream_id, time_delta;
     int64_t pts = 0;
-    const int prefix_len= frame_type == 2 ? 8+1 : 1;
-    const int64_t frame_start= url_ftell(bc) - prefix_len;
 
     if(frame_type < 2 && frame_start - nut->packet_start[2] > nut->max_distance){
         av_log(s, AV_LOG_ERROR, "last frame must have been damaged\n");
@@ -1045,10 +1141,8 @@
     size_mul= nut->frame_code[frame_code].size_mul;
     size_lsb= nut->frame_code[frame_code].size_lsb;
     stream_id= nut->frame_code[frame_code].stream_id_plus1 - 1;
-
-    if(frame_type == 2)
-        reset(s);
-
+    time_delta= nut->frame_code[frame_code].timestamp_delta;
+    
     if(stream_id==-1)
         stream_id= get_v(bc);
     if(stream_id >= s->nb_streams){
@@ -1061,7 +1155,7 @@
 
     *key_frame_ret= !!(flags & FLAG_KEY_FRAME);
 
-    if((flags & FLAG_PTS) == FLAG_PTS){
+    if(!time_delta){
         int64_t mask = (1<<stream->msb_timestamp_shift)-1;
         pts= get_v(bc);
         if(pts > mask){
@@ -1078,7 +1172,7 @@
             av_log(s, AV_LOG_ERROR, "no reference pts available\n");
             return -1;
         }
-        pts= stream->last_pts + stream->lru_pts_delta[flags&FLAG_PTS];
+        pts= stream->last_pts + time_delta;
     }
 
     if(*key_frame_ret){
@@ -1099,7 +1193,9 @@
     if(flags & FLAG_DATA_SIZE)
         size+= size_mul*get_v(bc);
       
-//av_log(s, AV_LOG_DEBUG, "fs:%lld fc:%d ft:%d kf:%d pts:%lld size:%d\n", frame_start, frame_code, frame_type, key_frame, pts, size);
+#ifdef TRACE
+av_log(s, AV_LOG_DEBUG, "fs:%lld fc:%d ft:%d kf:%d pts:%lld size:%d mul:%d lsb:%d flags:%d delta:%d\n", frame_start, frame_code, frame_type, *key_frame_ret, pts, size, size_mul, size_lsb, flags, time_delta);
+#endif
 
     if(frame_type==0 && url_ftell(bc) - nut->packet_start[2] + size > nut->max_distance){
         av_log(s, AV_LOG_ERROR, "frame size too large\n");
@@ -1114,13 +1210,13 @@
     return size;
 }
 
-static int decode_frame(NUTContext *nut, AVPacket *pkt, int frame_code, int frame_type){
+static int decode_frame(NUTContext *nut, AVPacket *pkt, int frame_code, int frame_type, int64_t frame_start){
     AVFormatContext *s= nut->avf;
     ByteIOContext *bc = &s->pb;
     int size, stream_id, key_frame;
     int64_t pts;
     
-    size= decode_frame_header(nut, &key_frame, &pts, &stream_id, frame_code, frame_type);
+    size= decode_frame_header(nut, &key_frame, &pts, &stream_id, frame_code, frame_type, frame_start);
     if(size < 0)
         return -1;
 
@@ -1141,6 +1237,7 @@
     int i, frame_code=0;
 
     for(;;){
+        int64_t pos= url_ftell(bc);
         int frame_type= 0;
         uint64_t tmp= nut->next_startcode;
         nut->next_startcode=0;
@@ -1148,7 +1245,9 @@
         if (url_feof(bc))
             return -1;
 
-        if(!tmp){
+        if(tmp){
+            pos-=8;
+        }else{
             frame_code = get_byte(bc);
             if(frame_code == 'N'){
                 tmp= frame_code;
@@ -1161,6 +1260,7 @@
         case STREAM_STARTCODE:
         case INDEX_STARTCODE:
             get_packetheader(nut, bc, 0);
+            assert(nut->packet_start[2] == pos);
             url_fseek(bc, nut->written_packet_size + nut->packet_start[2], SEEK_SET);
             break;
         case INFO_STARTCODE:
@@ -1169,9 +1269,10 @@
             break;
         case KEYFRAME_STARTCODE:
             frame_type = 2;
+            reset(s, get_v(bc));
             frame_code = get_byte(bc);
         case 0:
-            if(decode_frame(nut, pkt, frame_code, frame_type)>=0)
+            if(decode_frame(nut, pkt, frame_code, frame_type, pos)>=0)
                 return 0;
         default:
 resync:
@@ -1242,9 +1343,10 @@
             break;
         case KEYFRAME_STARTCODE:
             frame_type=2;
+            reset(s, get_v(bc));
             frame_code = get_byte(bc);
         case 0:
-            size= decode_frame_header(nut, &key_frame, &pts, &stream_id, frame_code, frame_type);
+            size= decode_frame_header(nut, &key_frame, &pts, &stream_id, frame_code, frame_type, pos);
             if(size < 0)
                 goto resync;