*
*/
+#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;
for(;;)
{
int tmp = get_byte(bc);
-// if(tmp=0x80){
-// if(!maxstuffing-- || val)
-// return -1;
-// }
-
if (tmp&0x80)
val= (val<<7) + tmp - 0x80;
else{
size= get_v(bc);
- init_checksum(bc, calculate_checksum ? av_crc04C11DB7_update : NULL, 1);
+ init_checksum(bc, calculate_checksum ? av_crc04C11DB7_update : NULL, 0);
// nut->packet_start[2] = start;
// nut->written_packet_size= size;
return size;
}
-static int check_checksum(ByteIOContext *bc){
- unsigned long checksum= get_checksum(bc);
-// return checksum != get_be32(bc);
-
- av_log(NULL, AV_LOG_ERROR, "%08X %08X\n", checksum, (int)get_be32(bc));
-
- return 0;
-}
-
static uint64_t find_any_startcode(ByteIOContext *bc, int64_t pos){
uint64_t state=0;
static int skip_reserved(ByteIOContext *bc, int64_t pos){
pos -= url_ftell(bc);
-
if(pos<0){
url_fseek(bc, pos, SEEK_CUR);
return -1;
int i, j, tmp_stream, tmp_mul, tmp_pts, tmp_size, count, tmp_res;
end= get_packetheader(nut, bc, 1);
- end += url_ftell(bc) - 4;
+ end += url_ftell(bc);
GET_V(tmp , tmp >=2 && tmp <= 3)
GET_V(stream_count , tmp > 0 && tmp <=MAX_STREAMS)
}
assert(nut->frame_code['N'].flags == FLAG_INVALID);
- if(skip_reserved(bc, end) || check_checksum(bc)){
+ if(skip_reserved(bc, end) || get_checksum(bc)){
av_log(s, AV_LOG_ERROR, "Main header checksum mismatch\n");
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;
end= get_packetheader(nut, bc, 1);
- end += url_ftell(bc) - 4;
+ end += url_ftell(bc);
GET_V(stream_id, tmp < s->nb_streams && !nut->stream[tmp].time_base.num);
stc= &nut->stream[stream_id];
}
GET_V(st->codec->channels, tmp > 0)
}
- if(skip_reserved(bc, end) || check_checksum(bc)){
+ if(skip_reserved(bc, end) || get_checksum(bc)){
av_log(s, AV_LOG_ERROR, "Stream header %d checksum mismatch\n", stream_id);
return -1;
}
char name[256], str_value[1024], type_str[256], *type= type_str;
end= get_packetheader(nut, bc, 1);
- end += url_ftell(bc) - 4;
+ end += url_ftell(bc);
GET_V(stream_id_plus1, tmp <= s->nb_streams)
chapter_id = get_s(bc);
}
}
- if(skip_reserved(bc, end) || check_checksum(bc)){
+ if(skip_reserved(bc, end) || get_checksum(bc)){
av_log(s, AV_LOG_ERROR, "Info header checksum mismatch\n");
return -1;
}
return 0;
}
-static int decode_syncpoint(NUTContext *nut){
+int sp_pos_cmp(syncpoint_t *a, syncpoint_t *b){
+ return (a->pos - b->pos>>32) - (b->pos - a->pos>>32);
+}
+
+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;
nut->last_syncpoint_pos= url_ftell(bc)-8;
end= get_packetheader(nut, bc, 1);
- end += url_ftell(bc) - 4;
+ 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++){
- nut->stream[i].last_pts= av_rescale_q(tmp / nut->time_base_count, time_base, nut->stream[i].time_base); //FIXME rounding and co
+ 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
- if(skip_reserved(bc, end) || check_checksum(bc)){
+ if(skip_reserved(bc, end) || get_checksum(bc)){
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;
+}
+
+static int find_and_decode_index(NUTContext *nut){
+ AVFormatContext *s= nut->avf;
+ ByteIOContext *bc = &s->pb;
+ uint64_t tmp, end;
+ int i, j, syncpoint_count;
+ int64_t filesize= url_fsize(bc);
+ int64_t *syncpoints;
+ int8_t *has_keyframe;
+
+ url_fseek(bc, filesize-12, SEEK_SET);
+ url_fseek(bc, filesize-get_be64(bc), SEEK_SET);
+ if(get_be64(bc) != INDEX_STARTCODE){
+ av_log(s, AV_LOG_ERROR, "no index at the end\n");
+ return -1;
+ }
+
+ end= get_packetheader(nut, bc, 1);
+ end += url_ftell(bc);
+
+ get_v(bc); //max_pts
+ GET_V(syncpoint_count, tmp < INT_MAX/8 && tmp > 0)
+ syncpoints= av_malloc(sizeof(int64_t)*syncpoint_count);
+ has_keyframe= av_malloc(sizeof(int8_t)*(syncpoint_count+1));
+ for(i=0; i<syncpoint_count; i++){
+ GET_V(syncpoints[i], tmp>0)
+ if(i)
+ syncpoints[i] += syncpoints[i-1];
+ }
+
+ for(i=0; i<s->nb_streams; i++){
+ int64_t last_pts= -1;
+ for(j=0; j<syncpoint_count;){
+ uint64_t x= get_v(bc);
+ int type= x&1;
+ int n= j;
+ x>>=1;
+ if(type){
+ int flag= x&1;
+ x>>=1;
+ if(n+x >= syncpoint_count + 1){
+ av_log(s, AV_LOG_ERROR, "index overflow A\n");
+ return -1;
+ }
+ while(x--)
+ has_keyframe[n++]= flag;
+ has_keyframe[n++]= !flag;
+ }else{
+ while(x != 1){
+ if(n>=syncpoint_count + 1){
+ av_log(s, AV_LOG_ERROR, "index overflow B\n");
+ return -1;
+ }
+ has_keyframe[n++]= x&1;
+ x>>=1;
+ }
+ }
+ if(has_keyframe[0]){
+ av_log(s, AV_LOG_ERROR, "keyframe before first syncpoint in index\n");
+ return -1;
+ }
+ assert(n<=syncpoint_count+1);
+ for(; j<n; j++){
+ if(has_keyframe[j]){
+ uint64_t B, A= get_v(bc);
+ if(!A){
+ A= get_v(bc);
+ B= get_v(bc);
+ //eor_pts[j][i] = last_pts + A + B
+ }else
+ B= 0;
+ av_add_index_entry(
+ s->streams[i],
+ 16*syncpoints[j-1],
+ last_pts + A,
+ 0,
+ 0,
+ AVINDEX_KEYFRAME);
+ last_pts += A + B;
+ }
+ }
+ }
+ }
+
+ if(skip_reserved(bc, end) || get_checksum(bc)){
+ av_log(s, AV_LOG_ERROR, "Index checksum mismatch\n");
+ return -1;
+ }
return 0;
}
/* main header */
pos=0;
- for(;;){
+ do{
pos= find_startcode(bc, MAIN_STARTCODE, pos)+1;
if (pos<0+1){
av_log(s, AV_LOG_ERROR, "no main startcode found\n");
return -1;
}
- if(decode_main_header(nut) >= 0)
- break;
- }
+ }while(decode_main_header(nut) < 0);
/* stream headers */
pos=0;
s->data_offset= pos-8;
+ if(!url_is_streamed(bc)){
+ int64_t orig_pos= url_ftell(bc);
+ find_and_decode_index(nut);
+ url_fseek(bc, orig_pos, SEEK_SET);
+ }
+ assert(nut->next_startcode == SYNCPOINT_STARTCODE);
+
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;
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);
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:
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");
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;
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;
+
+assert(0);
+}
+
+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;
- if(size < 0)
- goto resync;
+ 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);
- 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");
+ 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 pts;
+ return 0;
}
static int nut_read_close(AVFormatContext *s)
nut_read_header,
nut_read_packet,
nut_read_close,
- NULL,
- nut_read_timestamp,
+ read_seek,
.extensions = "nut",
};
#endif