#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){
// 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);
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;
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;
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);
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;
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;
}
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;
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");
NUTContext *nut = s->priv_data;
ByteIOContext *bc = &s->pb;
int64_t pos, pts, back_ptr;
- int frame_code, stream_id,size, flags;
av_log(s, AV_LOG_DEBUG, "read_timestamp(X,%d,%"PRId64",%"PRId64")\n", stream_index, *pos_arg, pos_limit);
pos= *pos_arg;
else if(stream_index == -2) return back_ptr;
assert(0);
- 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);
-
- if(size < 0)
- goto resync;
-
- 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");
-
- return pts;
}
static int read_seek(AVFormatContext *s, int stream_index, int64_t pts, int flags){
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;
- 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);
-
- assert(sp);
-
- av_log(NULL, AV_LOG_DEBUG, "SEEKTO: %"PRId64"\n", sp->back_ptr);
- pos= find_startcode(&s->pb, SYNCPOINT_STARTCODE, sp->back_ptr - 15);
+ 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);
+
+ 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(sp->back_ptr - 15 > pos || sp->back_ptr < 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;
}
nut_read_packet,
nut_read_close,
read_seek,
-// nut_read_timestamp,
.extensions = "nut",
};
#endif