]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/nutdec.c
Take the target address family in account when determining the family of
[ffmpeg] / libavformat / nutdec.c
index 8eb727b257602a1ee2c0010b5b925ad9c565815c..e3b117d8bf93ae1881406abaf6d0198d16f82077 100644 (file)
@@ -76,21 +76,21 @@ static uint64_t get_fourcc(ByteIOContext *bc){
 static inline uint64_t get_v_trace(ByteIOContext *bc, char *file, char *func, int line){
     uint64_t v= get_v(bc);
 
-    printf("get_v %5"PRId64" / %"PRIX64" in %s %s:%d\n", v, v, file, func, line);
+    av_log(NULL, AV_LOG_DEBUG, "get_v %5"PRId64" / %"PRIX64" 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 %5"PRId64" / %"PRIX64" in %s %s:%d\n", v, v, file, func, line);
+    av_log(NULL, AV_LOG_DEBUG, "get_s %5"PRId64" / %"PRIX64" 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 %5"PRId64" / %"PRIX64" in %s %s:%d\n", v, v, file, func, line);
+    av_log(NULL, AV_LOG_DEBUG, "get_vb %5"PRId64" / %"PRIX64" in %s %s:%d\n", v, v, file, func, line);
     return v;
 }
 #define get_v(bc)  get_v_trace(bc, __FILE__, __PRETTY_FUNCTION__, __LINE__)
@@ -98,16 +98,22 @@ static inline uint64_t get_vb_trace(ByteIOContext *bc, char *file, char *func, i
 #define get_vb(bc)  get_vb_trace(bc, __FILE__, __PRETTY_FUNCTION__, __LINE__)
 #endif
 
-static int get_packetheader(NUTContext *nut, ByteIOContext *bc, int calculate_checksum)
+static int get_packetheader(NUTContext *nut, ByteIOContext *bc, int calculate_checksum, uint64_t startcode)
 {
     int64_t size;
 //    start= url_ftell(bc) - 8;
 
+    startcode= be2me_64(startcode);
+    startcode= ff_crc04C11DB7_update(0, &startcode, 8);
+
+    init_checksum(bc, ff_crc04C11DB7_update, startcode);
     size= get_v(bc);
     if(size > 4096)
-        get_be32(bc); //FIXME check this
+        get_be32(bc);
+    if(get_checksum(bc) && size > 4096)
+        return -1;
 
-    init_checksum(bc, calculate_checksum ? av_crc04C11DB7_update : NULL, 0);
+    init_checksum(bc, calculate_checksum ? ff_crc04C11DB7_update : NULL, 0);
 
     return size;
 }
@@ -136,7 +142,7 @@ static uint64_t find_any_startcode(ByteIOContext *bc, int64_t pos){
 }
 
 /**
- * find the given startcode.
+ * Find the given startcode.
  * @param code the startcode
  * @param pos the start position of the search, or -1 if the current position
  * @returns the position of the startcode or -1 if not found
@@ -152,12 +158,6 @@ static int64_t find_startcode(ByteIOContext *bc, uint64_t code, int64_t pos){
     }
 }
 
-static int64_t lsb2full(StreamContext *stream, int64_t lsb){
-    int64_t mask = (1<<stream->msb_pts_shift)-1;
-    int64_t delta= stream->last_pts - mask/2;
-    return  ((lsb - delta)&mask) + delta;
-}
-
 static int nut_probe(AVProbeData *p){
     int i;
     uint64_t code= 0;
@@ -197,7 +197,7 @@ static int decode_main_header(NUTContext *nut){
     unsigned int stream_count;
     int i, j, tmp_stream, tmp_mul, tmp_pts, tmp_size, count, tmp_res;
 
-    end= get_packetheader(nut, bc, 1);
+    end= get_packetheader(nut, bc, 1, MAIN_STARTCODE);
     end += url_ftell(bc);
 
     GET_V(tmp              , tmp >=2 && tmp <= 3)
@@ -265,7 +265,7 @@ static int decode_main_header(NUTContext *nut){
     assert(nut->frame_code['N'].flags == FLAG_INVALID);
 
     if(skip_reserved(bc, end) || get_checksum(bc)){
-        av_log(s, AV_LOG_ERROR, "Main header checksum mismatch\n");
+        av_log(s, AV_LOG_ERROR, "main header checksum mismatch\n");
         return -1;
     }
 
@@ -285,10 +285,10 @@ static int decode_stream_header(NUTContext *nut){
     uint64_t tmp, end;
     AVStream *st;
 
-    end= get_packetheader(nut, bc, 1);
+    end= get_packetheader(nut, bc, 1, STREAM_STARTCODE);
     end += url_ftell(bc);
 
-    GET_V(stream_id, tmp < s->nb_streams && !nut->stream[tmp].time_base.num);
+    GET_V(stream_id, tmp < s->nb_streams && !nut->stream[tmp].time_base);
     stc= &nut->stream[stream_id];
 
     st = s->streams[stream_id];
@@ -319,13 +319,13 @@ static int decode_stream_header(NUTContext *nut){
             st->codec->codec_type = CODEC_TYPE_DATA;
             break;
         default:
-            av_log(s, AV_LOG_ERROR, "Unknown stream class (%d)\n", class);
+            av_log(s, AV_LOG_ERROR, "unknown stream class (%d)\n", class);
             return -1;
     }
     GET_V(stc->time_base_id    , tmp < nut->time_base_count);
     GET_V(stc->msb_pts_shift   , tmp < 16);
     stc->max_pts_distance= get_v(bc);
-    GET_V(stc->decode_delay    , tmp < 1000); //sanity limit, raise this if moors law is true
+    GET_V(stc->decode_delay    , tmp < 1000); //sanity limit, raise this if Moore's law is true
     st->codec->has_b_frames= stc->decode_delay;
     get_v(bc); //stream flags
 
@@ -349,17 +349,17 @@ static int decode_stream_header(NUTContext *nut){
         GET_V(st->codec->sample_rate , tmp > 0)
         tmp= get_v(bc); // samplerate_den
         if(tmp > st->codec->sample_rate){
-            av_log(s, AV_LOG_ERROR, "bleh, libnut muxed this ;)\n");
+            av_log(s, AV_LOG_ERROR, "Bleh, libnut muxed this ;)\n");
             st->codec->sample_rate= tmp;
         }
         GET_V(st->codec->channels, tmp > 0)
     }
     if(skip_reserved(bc, end) || get_checksum(bc)){
-        av_log(s, AV_LOG_ERROR, "Stream header %d checksum mismatch\n", stream_id);
+        av_log(s, AV_LOG_ERROR, "stream header %d checksum mismatch\n", stream_id);
         return -1;
     }
-    stc->time_base= nut->time_base[stc->time_base_id];
-    av_set_pts_info(s->streams[stream_id], 63, stc->time_base.num, stc->time_base.den);
+    stc->time_base= &nut->time_base[stc->time_base_id];
+    av_set_pts_info(s->streams[stream_id], 63, stc->time_base->num, stc->time_base->den);
     return 0;
 }
 
@@ -372,7 +372,7 @@ static int decode_info_header(NUTContext *nut){
     int64_t value, end;
     char name[256], str_value[1024], type_str[256], *type= type_str;
 
-    end= get_packetheader(nut, bc, 1);
+    end= get_packetheader(nut, bc, 1, INFO_STARTCODE);
     end += url_ftell(bc);
 
     GET_V(stream_id_plus1, tmp <= s->nb_streams)
@@ -415,41 +415,20 @@ static int decode_info_header(NUTContext *nut){
     }
 
     if(skip_reserved(bc, end) || get_checksum(bc)){
-        av_log(s, AV_LOG_ERROR, "Info header checksum mismatch\n");
+        av_log(s, AV_LOG_ERROR, "info header checksum mismatch\n");
         return -1;
     }
     return 0;
 }
 
-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, tmp;
-    int i;
-    AVRational time_base;
 
     nut->last_syncpoint_pos= url_ftell(bc)-8;
 
-    end= get_packetheader(nut, bc, 1);
+    end= get_packetheader(nut, bc, 1, SYNCPOINT_STARTCODE);
     end += url_ftell(bc);
 
     tmp= get_v(bc);
@@ -457,16 +436,7 @@ static int decode_syncpoint(NUTContext *nut, int64_t *ts, int64_t *back_ptr){
     if(*back_ptr < 0)
         return -1;
 
-    time_base= nut->time_base[tmp % nut->time_base_count];
-    for(i=0; i<s->nb_streams; i++){
-        nut->stream[i].last_pts= av_rescale_rnd(
-            tmp / nut->time_base_count,
-            time_base.num * (int64_t)nut->stream[i].time_base.den,
-            time_base.den * (int64_t)nut->stream[i].time_base.num,
-            AV_ROUND_DOWN);
-        //last_key_frame ?
-    }
-    //FIXME put this in a reset func maybe
+    ff_nut_reset_ts(nut, nut->time_base[tmp % nut->time_base_count], tmp);
 
     if(skip_reserved(bc, end) || get_checksum(bc)){
         av_log(s, AV_LOG_ERROR, "sync point checksum mismatch\n");
@@ -474,7 +444,7 @@ static int decode_syncpoint(NUTContext *nut, int64_t *ts, int64_t *back_ptr){
     }
 
     *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);
+    ff_nut_add_sp(nut, nut->last_syncpoint_pos, *back_ptr, *ts);
 
     return 0;
 }
@@ -495,7 +465,7 @@ static int find_and_decode_index(NUTContext *nut){
         return -1;
     }
 
-    end= get_packetheader(nut, bc, 1);
+    end= get_packetheader(nut, bc, 1, INDEX_STARTCODE);
     end += url_ftell(bc);
 
     get_v(bc); //max_pts
@@ -563,7 +533,7 @@ static int find_and_decode_index(NUTContext *nut){
     }
 
     if(skip_reserved(bc, end) || get_checksum(bc)){
-        av_log(s, AV_LOG_ERROR, "Index checksum mismatch\n");
+        av_log(s, AV_LOG_ERROR, "index checksum mismatch\n");
         return -1;
     }
     return 0;
@@ -583,7 +553,7 @@ static int nut_read_header(AVFormatContext *s, AVFormatParameters *ap)
     do{
         pos= find_startcode(bc, MAIN_STARTCODE, pos)+1;
         if (pos<0+1){
-            av_log(s, AV_LOG_ERROR, "no main startcode found\n");
+            av_log(s, AV_LOG_ERROR, "No main startcode found.\n");
             return -1;
         }
     }while(decode_main_header(nut) < 0);
@@ -593,7 +563,7 @@ static int nut_read_header(AVFormatContext *s, AVFormatParameters *ap)
     for(inited_stream_count=0; inited_stream_count < s->nb_streams;){
         pos= find_startcode(bc, STREAM_STARTCODE, pos)+1;
         if (pos<0+1){
-            av_log(s, AV_LOG_ERROR, "not all stream headers found\n");
+            av_log(s, AV_LOG_ERROR, "Not all stream headers found.\n");
             return -1;
         }
         if(decode_stream_header(nut) >= 0)
@@ -639,7 +609,7 @@ static int decode_frame_header(NUTContext *nut, int64_t *pts, int *stream_id, in
     uint64_t tmp;
 
     if(url_ftell(bc) > nut->last_syncpoint_pos + nut->max_distance){
-        av_log(s, AV_LOG_ERROR, "last frame must have been damaged %"PRId64" > %"PRId64" + %d\n", url_ftell(bc), nut->last_syncpoint_pos, nut->max_distance);
+        av_log(s, AV_LOG_ERROR, "Last frame must have been damaged %"PRId64" > %"PRId64" + %d\n", url_ftell(bc), nut->last_syncpoint_pos, nut->max_distance);
         return -1;
     }
 
@@ -662,7 +632,7 @@ static int decode_frame_header(NUTContext *nut, int64_t *pts, int *stream_id, in
         int coded_pts= get_v(bc);
 //FIXME check last_pts validity?
         if(coded_pts < (1<<stc->msb_pts_shift)){
-            *pts=lsb2full(stc, coded_pts);
+            *pts=ff_lsb2full(stc, coded_pts);
         }else
             *pts=coded_pts - (1<<stc->msb_pts_shift);
     }else
@@ -750,7 +720,7 @@ static int nut_read_packet(AVFormatContext *s, AVPacket *pkt)
         case MAIN_STARTCODE:
         case STREAM_STARTCODE:
         case INDEX_STARTCODE:
-            skip= get_packetheader(nut, bc, 0);
+            skip= get_packetheader(nut, bc, 0, tmp);
             url_fseek(bc, skip, SEEK_CUR);
             break;
         case INFO_STARTCODE:
@@ -786,12 +756,11 @@ static int64_t nut_read_timestamp(AVFormatContext *s, int stream_index, int64_t
 av_log(s, AV_LOG_DEBUG, "read_timestamp(X,%d,%"PRId64",%"PRId64")\n", stream_index, *pos_arg, pos_limit);
 
     pos= *pos_arg;
-resync:
     do{
         pos= find_startcode(bc, SYNCPOINT_STARTCODE, pos)+1;
         if(pos < 1){
             assert(nut->next_startcode == 0);
-            av_log(s, AV_LOG_ERROR, "read_timestamp failed\n");
+            av_log(s, AV_LOG_ERROR, "read_timestamp failed.\n");
             return AV_NOPTS_VALUE;
         }
     }while(decode_syncpoint(nut, &pts, &back_ptr) < 0);
@@ -822,7 +791,7 @@ static int read_seek(AVFormatContext *s, int stream_index, int64_t pts, int flag
         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_tree_find(nut->syncpoints, &dummy, ff_nut_sp_pts_cmp, next_node);
         av_log(s, AV_LOG_DEBUG, "%"PRIu64"-%"PRIu64" %"PRId64"-%"PRId64"\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,
@@ -831,7 +800,7 @@ static int read_seek(AVFormatContext *s, int stream_index, int64_t pts, int flag
         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);
+            av_tree_find(nut->syncpoints, &dummy, ff_nut_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)
@@ -839,7 +808,7 @@ static int read_seek(AVFormatContext *s, int stream_index, int64_t pts, int flag
             //FIXME dir but i think it does not matter
         }
         dummy.pos= pos;
-        sp= av_tree_find(nut->syncpoints, &dummy, sp_pos_cmp, NULL);
+        sp= av_tree_find(nut->syncpoints, &dummy, ff_nut_sp_pos_cmp, NULL);
 
         assert(sp);
         pos2= sp->back_ptr  - 15;