]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/nutdec.c
split rm muxer and demuxer in their own files
[ffmpeg] / libavformat / nutdec.c
index 462d63eb7396086c1ff3255dc0147e35ca86f780..8688f1971719422c148b0c6ee4e5474f2ebaf272 100644 (file)
  *
  */
 
+#include "tree.h"
 #include "nut.h"
 
 #undef NDEBUG
 #include <assert.h>
 
-static uint64_t get_v(ByteIOContext *bc/*, maxstuffing*/){
+static uint64_t get_v(ByteIOContext *bc){
     uint64_t val = 0;
+    int tmp;
 
-    for(;;)
-    {
-        int tmp = get_byte(bc);
-
-//         if(tmp=0x80){
-//             if(!maxstuffing-- || val)
-//                 return -1;
-//         }
-
-        if (tmp&0x80)
-            val= (val<<7) + tmp - 0x80;
-        else{
-            return (val<<7) + tmp;
-        }
-    }
-    return -1;
+    do{
+        tmp = get_byte(bc);
+        val= (val<<7) + (tmp&127);
+    }while(tmp&128);
+    return val;
 }
 
 static int get_str(ByteIOContext *bc, char *string, unsigned int maxlen){
@@ -109,16 +100,15 @@ static inline uint64_t get_vb_trace(ByteIOContext *bc, char *file, char *func, i
 
 static int get_packetheader(NUTContext *nut, ByteIOContext *bc, int calculate_checksum)
 {
-    int64_t start, size;
+    int64_t size;
 //    start= url_ftell(bc) - 8;
 
     size= get_v(bc);
+    if(size > 4096)
+        get_be32(bc); //FIXME check this
 
     init_checksum(bc, calculate_checksum ? av_crc04C11DB7_update : NULL, 0);
 
-//    nut->packet_start[2] = start;
-//    nut->written_packet_size= size;
-
     return size;
 }
 
@@ -190,7 +180,6 @@ static int nut_probe(AVProbeData *p){
 
 static int skip_reserved(ByteIOContext *bc, int64_t pos){
     pos -= url_ftell(bc);
-av_log(NULL, AV_LOG_ERROR, "skip %d\n", (int)pos);
     if(pos<0){
         url_fseek(bc, pos, SEEK_CUR);
         return -1;
@@ -292,7 +281,7 @@ static int decode_stream_header(NUTContext *nut){
     AVFormatContext *s= nut->avf;
     ByteIOContext *bc = &s->pb;
     StreamContext *stc;
-    int class, nom, denom, stream_id;
+    int class, stream_id;
     uint64_t tmp, end;
     AVStream *st;
 
@@ -432,11 +421,29 @@ static int decode_info_header(NUTContext *nut){
     return 0;
 }
 
-static int decode_syncpoint(NUTContext *nut){
+static int sp_pos_cmp(syncpoint_t *a, syncpoint_t *b){
+    return (a->pos - b->pos>>32) - (b->pos - a->pos>>32);
+}
+
+static int sp_pts_cmp(syncpoint_t *a, syncpoint_t *b){
+    return (a->ts - b->ts>>32) - (b->ts - a->ts>>32);
+}
+
+static void add_sp(NUTContext *nut, int64_t pos, int64_t back_ptr, int64_t ts){
+    syncpoint_t *sp2, *sp= av_mallocz(sizeof(syncpoint_t));
+
+    sp->pos= pos;
+    sp->back_ptr= back_ptr;
+    sp->ts= ts;
+    sp2= av_tree_insert(&nut->syncpoints, sp, sp_pos_cmp);
+    if(sp2 && sp2 != sp)
+        av_free(sp);
+}
+
+static int decode_syncpoint(NUTContext *nut, int64_t *ts, int64_t *back_ptr){
     AVFormatContext *s= nut->avf;
     ByteIOContext *bc = &s->pb;
-    int64_t end;
-    uint64_t tmp;
+    int64_t end, tmp;
     int i;
     AVRational time_base;
 
@@ -446,7 +453,9 @@ static int decode_syncpoint(NUTContext *nut){
     end += url_ftell(bc);
 
     tmp= get_v(bc);
-    get_v(bc); //back_ptr_div16
+    *back_ptr= nut->last_syncpoint_pos - 16*get_v(bc);
+    if(*back_ptr < 0)
+        return -1;
 
     time_base= nut->time_base[tmp % nut->time_base_count];
     for(i=0; i<s->nb_streams; i++){
@@ -463,6 +472,10 @@ static int decode_syncpoint(NUTContext *nut){
         av_log(s, AV_LOG_ERROR, "sync point checksum mismatch\n");
         return -1;
     }
+
+    *ts= tmp / s->nb_streams * av_q2d(nut->time_base[tmp % s->nb_streams])*AV_TIME_BASE;
+    add_sp(nut, nut->last_syncpoint_pos, *back_ptr, *ts);
+
     return 0;
 }
 
@@ -608,7 +621,7 @@ static int nut_read_header(AVFormatContext *s, AVFormatParameters *ap)
 
     s->data_offset= pos-8;
 
-    if(0 &&!url_is_streamed(bc)){
+    if(!url_is_streamed(bc)){
         int64_t orig_pos= url_ftell(bc);
         find_and_decode_index(nut);
         url_fseek(bc, orig_pos, SEEK_SET);
@@ -618,7 +631,7 @@ static int nut_read_header(AVFormatContext *s, AVFormatParameters *ap)
     return 0;
 }
 
-static int decode_frame_header(NUTContext *nut, int *flags_ret, int64_t *pts, int *stream_id, int frame_code){
+static int decode_frame_header(NUTContext *nut, int64_t *pts, int *stream_id, int frame_code){
     AVFormatContext *s= nut->avf;
     ByteIOContext *bc = &s->pb;
     StreamContext *stc;
@@ -663,21 +676,13 @@ static int decode_frame_header(NUTContext *nut, int *flags_ret, int64_t *pts, in
         get_v(bc);
     if(flags&FLAG_CHECKSUM){
         get_be32(bc); //FIXME check this
+    }else if(size > 2*nut->max_distance || FFABS(stc->last_pts - *pts) > stc->max_pts_distance){
+        av_log(s, AV_LOG_ERROR, "frame size > 2max_distance and no checksum\n");
+        return -1;
     }
-    *flags_ret= flags;
 
     stc->last_pts= *pts;
-    stc->last_key_frame= flags&FLAG_KEY; //FIXME change to last flags
-
-    if(flags&FLAG_KEY){
-        av_add_index_entry(
-            s->streams[*stream_id],
-            nut->last_syncpoint_pos,
-            *pts,
-            0,
-            0,
-            AVINDEX_KEYFRAME);
-    }
+    stc->last_flags= flags;
 
     return size;
 }
@@ -685,25 +690,32 @@ static int decode_frame_header(NUTContext *nut, int *flags_ret, int64_t *pts, in
 static int decode_frame(NUTContext *nut, AVPacket *pkt, int frame_code){
     AVFormatContext *s= nut->avf;
     ByteIOContext *bc = &s->pb;
-    int size, stream_id, flags, discard;
+    int size, stream_id, discard;
     int64_t pts, last_IP_pts;
+    StreamContext *stc;
 
-    size= decode_frame_header(nut, &flags, &pts, &stream_id, frame_code);
+    size= decode_frame_header(nut, &pts, &stream_id, frame_code);
     if(size < 0)
         return -1;
 
+    stc= &nut->stream[stream_id];
+
+    if (stc->last_flags & FLAG_KEY)
+        stc->skip_until_key_frame=0;
+
     discard= s->streams[ stream_id ]->discard;
     last_IP_pts= s->streams[ stream_id ]->last_IP_pts;
-    if(  (discard >= AVDISCARD_NONKEY && !(flags & FLAG_KEY))
+    if(  (discard >= AVDISCARD_NONKEY && !(stc->last_flags & FLAG_KEY))
        ||(discard >= AVDISCARD_BIDIR && last_IP_pts != AV_NOPTS_VALUE && last_IP_pts > pts)
-       || discard >= AVDISCARD_ALL){
+       || discard >= AVDISCARD_ALL
+       || stc->skip_until_key_frame){
         url_fskip(bc, size);
         return 1;
     }
 
     av_get_packet(bc, pkt, size);
     pkt->stream_index = stream_id;
-    if (flags & FLAG_KEY)
+    if (stc->last_flags & FLAG_KEY)
         pkt->flags |= PKT_FLAG_KEY;
     pkt->pts = pts;
 
@@ -715,19 +727,19 @@ static int nut_read_packet(AVFormatContext *s, AVPacket *pkt)
     NUTContext *nut = s->priv_data;
     ByteIOContext *bc = &s->pb;
     int i, frame_code=0, ret, skip;
+    int64_t ts, back_ptr;
 
     for(;;){
         int64_t pos= url_ftell(bc);
         uint64_t tmp= nut->next_startcode;
         nut->next_startcode=0;
 
-        if (url_feof(bc))
-            return -1;
-
         if(tmp){
             pos-=8;
         }else{
             frame_code = get_byte(bc);
+            if(url_feof(bc))
+                return -1;
             if(frame_code == 'N'){
                 tmp= frame_code;
                 for(i=1; i<8; i++)
@@ -746,7 +758,7 @@ static int nut_read_packet(AVFormatContext *s, AVPacket *pkt)
                 goto resync;
             break;
         case SYNCPOINT_STARTCODE:
-            if(decode_syncpoint(nut)<0)
+            if(decode_syncpoint(nut, &ts, &back_ptr)<0)
                 goto resync;
             frame_code = get_byte(bc);
         case 0:
@@ -758,7 +770,7 @@ static int nut_read_packet(AVFormatContext *s, AVPacket *pkt)
         default:
 resync:
 av_log(s, AV_LOG_DEBUG, "syncing from %"PRId64"\n", pos);
-            tmp= find_any_startcode(bc, pos+1);
+            tmp= find_any_startcode(bc, nut->last_syncpoint_pos+1);
             if(tmp==0)
                 return -1;
 av_log(s, AV_LOG_DEBUG, "sync\n");
@@ -770,8 +782,7 @@ av_log(s, AV_LOG_DEBUG, "sync\n");
 static int64_t nut_read_timestamp(AVFormatContext *s, int stream_index, int64_t *pos_arg, int64_t pos_limit){
     NUTContext *nut = s->priv_data;
     ByteIOContext *bc = &s->pb;
-    int64_t pos, pts;
-    int frame_code, stream_id,size, flags;
+    int64_t pos, pts, back_ptr;
 av_log(s, AV_LOG_DEBUG, "read_timestamp(X,%d,%"PRId64",%"PRId64")\n", stream_index, *pos_arg, pos_limit);
 
     pos= *pos_arg;
@@ -783,28 +794,67 @@ resync:
             av_log(s, AV_LOG_ERROR, "read_timestamp failed\n");
             return AV_NOPTS_VALUE;
         }
-    }while(decode_syncpoint(nut) < 0);
+    }while(decode_syncpoint(nut, &pts, &back_ptr) < 0);
     *pos_arg = pos-1;
     assert(nut->last_syncpoint_pos == *pos_arg);
 
-    do{
-        frame_code= get_byte(bc);
-        if(frame_code == 'N'){
-            pos= url_ftell(bc)-1;
-            goto resync;
-        }
-        //FIXME consider pos_limit and eof
-        size= decode_frame_header(nut, &flags, &pts, &stream_id, frame_code);
+    av_log(s, AV_LOG_DEBUG, "return %Ld %Ld\n", pts,back_ptr );
+    if     (stream_index == -1) return pts;
+    else if(stream_index == -2) return back_ptr;
 
-        if(size < 0)
-            goto resync;
+assert(0);
+}
 
-        url_fseek(bc, size, SEEK_CUR);
-    }while(stream_id != stream_index || !(flags & FLAG_KEY));
-    assert(nut->next_startcode == 0);
-    av_log(s, AV_LOG_DEBUG, "read_timestamp success\n");
+static int read_seek(AVFormatContext *s, int stream_index, int64_t pts, int flags){
+    NUTContext *nut = s->priv_data;
+    AVStream *st= s->streams[stream_index];
+    syncpoint_t dummy={.ts= pts*av_q2d(st->time_base)*AV_TIME_BASE};
+    syncpoint_t nopts_sp= {.ts= AV_NOPTS_VALUE, .back_ptr= AV_NOPTS_VALUE};
+    syncpoint_t *sp, *next_node[2]= {&nopts_sp, &nopts_sp};
+    int64_t pos, pos2, ts;
+    int i;
+
+    if(st->index_entries){
+        int index= av_index_search_timestamp(st, pts, flags);
+        if(index<0)
+            return -1;
+
+        pos2= st->index_entries[index].pos;
+        ts  = st->index_entries[index].timestamp;
+    }else{
+        av_tree_find(nut->syncpoints, &dummy, sp_pts_cmp, next_node);
+        av_log(s, AV_LOG_DEBUG, "%Ld-%Ld %Ld-%Ld\n", next_node[0]->pos, next_node[1]->pos,
+                                                    next_node[0]->ts , next_node[1]->ts);
+        pos= av_gen_search(s, -1, dummy.ts, next_node[0]->pos, next_node[1]->pos, next_node[1]->pos,
+                                            next_node[0]->ts , next_node[1]->ts, AVSEEK_FLAG_BACKWARD, &ts, nut_read_timestamp);
+
+        if(!(flags & AVSEEK_FLAG_BACKWARD)){
+            dummy.pos= pos+16;
+            next_node[1]= &nopts_sp;
+            av_tree_find(nut->syncpoints, &dummy, sp_pos_cmp, next_node);
+            pos2= av_gen_search(s, -2, dummy.pos, next_node[0]->pos     , next_node[1]->pos, next_node[1]->pos,
+                                                next_node[0]->back_ptr, next_node[1]->back_ptr, flags, &ts, nut_read_timestamp);
+            if(pos2>=0)
+                pos= pos2;
+            //FIXME dir but i think it doesnt matter
+        }
+        dummy.pos= pos;
+        sp= av_tree_find(nut->syncpoints, &dummy, sp_pos_cmp, NULL);
 
-    return pts;
+        assert(sp);
+        pos2= sp->back_ptr  - 15;
+    }
+    av_log(NULL, AV_LOG_DEBUG, "SEEKTO: %"PRId64"\n", pos2);
+    pos= find_startcode(&s->pb, SYNCPOINT_STARTCODE, pos2);
+    url_fseek(&s->pb, pos, SEEK_SET);
+    av_log(NULL, AV_LOG_DEBUG, "SP: %"PRId64"\n", pos);
+    if(pos2 > pos || pos2 + 15 < pos){
+        av_log(NULL, AV_LOG_ERROR, "no syncpoint at backptr pos\n");
+    }
+    for(i=0; i<s->nb_streams; i++)
+        nut->stream[i].skip_until_key_frame=1;
+
+    return 0;
 }
 
 static int nut_read_close(AVFormatContext *s)
@@ -826,8 +876,7 @@ AVInputFormat nut_demuxer = {
     nut_read_header,
     nut_read_packet,
     nut_read_close,
-    NULL,
-    nut_read_timestamp,
+    read_seek,
     .extensions = "nut",
 };
 #endif