comparison nut.c @ 456:c13fd248e222 libavformat

match latest spec seeking to startcode-less frames
author michael
date Sun, 02 May 2004 01:03:55 +0000
parents e17fb306b107
children 63540e5504f7
comparison
equal deleted inserted replaced
455:e5174af0f52f 456:c13fd248e222
45 #define STREAM_STARTCODE (0x11405BF2F9DBULL + (((uint64_t)('N'<<8) + 'S')<<48)) 45 #define STREAM_STARTCODE (0x11405BF2F9DBULL + (((uint64_t)('N'<<8) + 'S')<<48))
46 #define KEYFRAME_STARTCODE (0xE4ADEECA4569ULL + (((uint64_t)('N'<<8) + 'K')<<48)) 46 #define KEYFRAME_STARTCODE (0xE4ADEECA4569ULL + (((uint64_t)('N'<<8) + 'K')<<48))
47 #define INDEX_STARTCODE (0xDD672F23E64EULL + (((uint64_t)('N'<<8) + 'X')<<48)) 47 #define INDEX_STARTCODE (0xDD672F23E64EULL + (((uint64_t)('N'<<8) + 'X')<<48))
48 #define INFO_STARTCODE (0xAB68B596BA78ULL + (((uint64_t)('N'<<8) + 'I')<<48)) 48 #define INFO_STARTCODE (0xAB68B596BA78ULL + (((uint64_t)('N'<<8) + 'I')<<48))
49 49
50 #define MAX_TYPE1_DISTANCE (1024*16-1) 50 #define MAX_DISTANCE (1024*16-1)
51 #define MAX_TYPE2_DISTANCE (1024*64-1) 51 #define MAX_SHORT_DISTANCE (1024*4-1)
52 52
53 #define MAX_SIZE_LRU 2
54 #define MAX_PTS_LRU 3 53 #define MAX_PTS_LRU 3
55 54
56 #define FLAG_FRAME_TYPE 1 55 #define FLAG_DATA_SIZE 4
57 #define FLAG_DATA_SIZE 2 56 #define FLAG_PTS 3
58 #define FLAG_PTS 16 57 #define FLAG_KEY_FRAME 8
59 #define FLAG_FULL_PTS 4
60 #define FLAG_KEY_FRAME 32
61 58
62 typedef struct { 59 typedef struct {
63 uint8_t flags; 60 uint8_t flags;
64 uint8_t stream_id_plus1; 61 uint8_t stream_id_plus1;
65 uint8_t size_mul; 62 uint16_t size_mul;
66 uint8_t size_lsb; 63 uint16_t size_lsb;
67 } FrameCode; 64 } FrameCode;
68 65
69 typedef struct { 66 typedef struct {
70 int last_key_frame; 67 int last_key_frame;
71 int msb_timestamp_shift; 68 int msb_timestamp_shift;
72 int rate_num; 69 int rate_num;
73 int rate_den; 70 int rate_den;
74 int64_t last_pts; 71 int64_t last_pts;
75 int64_t last_full_pts;
76 int lru_pts_delta[MAX_PTS_LRU]; 72 int lru_pts_delta[MAX_PTS_LRU];
77 int lru_size[MAX_SIZE_LRU];
78 int initial_pts_predictor[MAX_PTS_LRU]; 73 int initial_pts_predictor[MAX_PTS_LRU];
79 int initial_size_predictor[MAX_SIZE_LRU];
80 int64_t last_sync_pos; ///<pos of last 1/2 type frame 74 int64_t last_sync_pos; ///<pos of last 1/2 type frame
75 int decode_delay;
81 } StreamContext; 76 } StreamContext;
82 77
83 typedef struct { 78 typedef struct {
84 AVFormatContext *avf; 79 AVFormatContext *avf;
85 int64_t packet_start;
86 int64_t last_packet_start;
87 int written_packet_size; 80 int written_packet_size;
88 int64_t packet_size_pos; 81 int64_t packet_start[3]; //0-> startcode less, 1-> short startcode 2-> long startcodes
89 int64_t last_frame_start[3];
90 FrameCode frame_code[256]; 82 FrameCode frame_code[256];
91 int stream_count; 83 int stream_count;
92 uint64_t next_startcode; ///< stores the next startcode if it has alraedy been parsed but the stream isnt seekable 84 uint64_t next_startcode; ///< stores the next startcode if it has alraedy been parsed but the stream isnt seekable
93 StreamContext *stream; 85 StreamContext *stream;
86 int max_distance;
94 } NUTContext; 87 } NUTContext;
95 88
96 static char *info_table[][2]={ 89 static char *info_table[][2]={
97 {NULL , NULL }, // end 90 {NULL , NULL }, // end
98 {NULL , NULL }, 91 {NULL , NULL },
128 lru[0]= current; 121 lru[0]= current;
129 } 122 }
130 123
131 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){ 124 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){
132 StreamContext *stream= &nut->stream[stream_index]; 125 StreamContext *stream= &nut->stream[stream_index];
133 const int flags=nut->frame_code[frame_code].flags;
134 126
135 stream->last_key_frame= key_frame; 127 stream->last_key_frame= key_frame;
136 nut->last_frame_start[ frame_type ]= frame_start; 128 nut->packet_start[ frame_type ]= frame_start;
137 if(frame_type == 0) 129 if(stream->last_pts != AV_NOPTS_VALUE){
130 assert(frame_type < 2);
138 update_lru(stream->lru_pts_delta, pts - stream->last_pts, 3); 131 update_lru(stream->lru_pts_delta, pts - stream->last_pts, 3);
139 update_lru(stream->lru_size, size, 2); 132 }
140 stream->last_pts= pts; 133 stream->last_pts= pts;
141 if((flags & FLAG_PTS) && (flags & FLAG_FULL_PTS))
142 stream->last_full_pts= pts;
143 } 134 }
144 135
145 static void reset(AVFormatContext *s/*, int frame_type*/){ 136 static void reset(AVFormatContext *s/*, int frame_type*/){
146 NUTContext *nut = s->priv_data; 137 NUTContext *nut = s->priv_data;
147 int i; 138 int i;
148 139
149 for(i=0; i<s->nb_streams; i++){ 140 for(i=0; i<s->nb_streams; i++){
150 StreamContext *stream= &nut->stream[i]; 141 StreamContext *stream= &nut->stream[i];
151 142
152 stream->last_key_frame= 1; 143 stream->last_key_frame= 1;
144 stream->last_pts= AV_NOPTS_VALUE;
153 memcpy(stream->lru_pts_delta, stream->initial_pts_predictor, sizeof(int)*MAX_PTS_LRU); 145 memcpy(stream->lru_pts_delta, stream->initial_pts_predictor, sizeof(int)*MAX_PTS_LRU);
154 memcpy(stream->lru_size, stream->initial_size_predictor, sizeof(int)*MAX_SIZE_LRU);
155 } 146 }
156 } 147 }
157 148
158 static void build_frame_code(AVFormatContext *s){ 149 static void build_frame_code(AVFormatContext *s){
159 NUTContext *nut = s->priv_data; 150 NUTContext *nut = s->priv_data;
160 int key_frame, frame_type, full_pts, index, pred, stream_id; 151 int key_frame, index, pred, stream_id, full_pts;
161 int start=0; 152 int start=0;
162 int end= 255; 153 int end= 255;
163 int keyframe_0_esc= s->nb_streams > 2; 154 int keyframe_0_esc= s->nb_streams > 2;
164 155
165 if(keyframe_0_esc){ 156 if(keyframe_0_esc){
166 /* keyframe = 0 escapes, 3 codes */ 157 /* keyframe = 0 escape */
167 for(frame_type=0; frame_type<2; frame_type++){ 158 FrameCode *ft= &nut->frame_code[start];
168 for(full_pts=frame_type; full_pts<2; full_pts++){ 159 ft->flags= FLAG_DATA_SIZE | FLAG_PTS;
169 FrameCode *ft= &nut->frame_code[start]; 160 ft->stream_id_plus1= 0;
170 ft->flags= FLAG_FRAME_TYPE*frame_type + FLAG_FULL_PTS*full_pts; 161 ft->size_mul=1;
171 ft->flags|= FLAG_DATA_SIZE | FLAG_PTS; 162 start++;
172 ft->stream_id_plus1= 0;
173 ft->size_mul=1;
174 start++;
175 }
176 }
177 } 163 }
178 164
179 for(stream_id= 0; stream_id<s->nb_streams; stream_id++){ 165 for(stream_id= 0; stream_id<s->nb_streams; stream_id++){
180 int start2= start + (end-start)*stream_id / s->nb_streams; 166 int start2= start + (end-start)*stream_id / s->nb_streams;
181 int end2 = start + (end-start)*(stream_id+1) / s->nb_streams; 167 int end2 = start + (end-start)*(stream_id+1) / s->nb_streams;
186 172
187 for(key_frame=0; key_frame<2; key_frame++){ 173 for(key_frame=0; key_frame<2; key_frame++){
188 if(intra_only && keyframe_0_esc && key_frame==0) 174 if(intra_only && keyframe_0_esc && key_frame==0)
189 continue; 175 continue;
190 176
191 for(frame_type=0; frame_type<2; frame_type++){ 177 {
192 for(full_pts=frame_type; full_pts<2; full_pts++){ 178 FrameCode *ft= &nut->frame_code[start2];
193 FrameCode *ft= &nut->frame_code[start2]; 179 ft->flags= FLAG_KEY_FRAME*key_frame;
194 ft->flags= FLAG_FRAME_TYPE*frame_type + FLAG_FULL_PTS*full_pts + FLAG_KEY_FRAME*key_frame; 180 ft->flags|= FLAG_DATA_SIZE | FLAG_PTS;
195 ft->flags|= FLAG_DATA_SIZE | FLAG_PTS; 181 ft->stream_id_plus1= stream_id + 1;
196 ft->stream_id_plus1= stream_id + 1; 182 ft->size_mul=1;
197 ft->size_mul=1; 183 start2++;
198 start2++;
199 }
200 } 184 }
201 } 185 }
202 186
203 key_frame= intra_only; 187 key_frame= intra_only;
204 #if 1 188 #if 1
205 if(is_audio){ 189 if(is_audio){
206 int frame_bytes= codec->frame_size*(int64_t)codec->bit_rate / (8*codec->sample_rate); 190 int frame_bytes= codec->frame_size*(int64_t)codec->bit_rate / (8*codec->sample_rate);
207 for(pred=0; pred<MAX_SIZE_LRU; pred++){ 191 for(full_pts=0; full_pts<2; full_pts++){
208 for(frame_type=0; frame_type<1; frame_type++){ 192 for(pred=0; pred<2; pred++){
209 FrameCode *ft= &nut->frame_code[start2]; 193 FrameCode *ft= &nut->frame_code[start2];
210 ft->flags= FLAG_KEY_FRAME*key_frame + (FLAG_FULL_PTS+FLAG_PTS+FLAG_FRAME_TYPE)*frame_type; 194 ft->flags= FLAG_KEY_FRAME*key_frame + FLAG_PTS*full_pts;
211 ft->stream_id_plus1= stream_id + 1; 195 ft->stream_id_plus1= stream_id + 1;
212 ft->size_mul=1; 196 ft->size_mul=frame_bytes + 2;
213 ft->size_lsb=1 + pred; 197 ft->size_lsb=frame_bytes + pred;
214 start2++; 198 start2++;
199 av_log(NULL, AV_LOG_DEBUG, "%d\n", frame_bytes + pred);
215 } 200 }
216 nut->stream[stream_id].initial_size_predictor[pred]= frame_bytes + pred;
217 } 201 }
218 }else{ 202 }else{
219 FrameCode *ft= &nut->frame_code[start2]; 203 FrameCode *ft= &nut->frame_code[start2];
220 ft->flags= FLAG_KEY_FRAME | FLAG_DATA_SIZE; 204 ft->flags= FLAG_KEY_FRAME | FLAG_DATA_SIZE;
221 ft->stream_id_plus1= stream_id + 1; 205 ft->stream_id_plus1= stream_id + 1;
252 { 236 {
253 int tmp = get_byte(bc); 237 int tmp = get_byte(bc);
254 238
255 if (tmp&0x80) 239 if (tmp&0x80)
256 val= (val<<7) + tmp - 0x80; 240 val= (val<<7) + tmp - 0x80;
257 else 241 else{
242 //av_log(NULL, AV_LOG_DEBUG, "get_v()= %lld\n", (val<<7) + tmp);
258 return (val<<7) + tmp; 243 return (val<<7) + tmp;
244 }
259 } 245 }
260 return -1; 246 return -1;
261 } 247 }
262 248
263 static int get_str(ByteIOContext *bc, char *string, int maxlen){ 249 static int get_str(ByteIOContext *bc, char *string, int maxlen){
287 return UINT64_MAX; 273 return UINT64_MAX;
288 274
289 while(i--) 275 while(i--)
290 val = (val<<8) + get_byte(bc); 276 val = (val<<8) + get_byte(bc);
291 277
278 //av_log(NULL, AV_LOG_DEBUG, "get_vb()= %lld\n", val);
292 return val; 279 return val;
293 } 280 }
294 281
295 static int get_packetheader(NUTContext *nut, ByteIOContext *bc, int prefix_length, int calculate_checksum) 282 static int get_packetheader(NUTContext *nut, ByteIOContext *bc, int calculate_checksum)
296 { 283 {
297 int64_t start, size, last_size; 284 int64_t start, size;
298 start= url_ftell(bc) - prefix_length; 285 start= url_ftell(bc) - 8;
299 286
300 if(nut->written_packet_size >= 0 && start != nut->packet_start + nut->written_packet_size){
301 av_log(nut->avf, AV_LOG_ERROR, "get_packetheader called at weird position\n");
302 if(prefix_length<8)
303 return -1;
304 }
305
306 init_checksum(bc, calculate_checksum ? update_adler32 : NULL, 0); 287 init_checksum(bc, calculate_checksum ? update_adler32 : NULL, 0);
307 288
308 size= get_v(bc); 289 size= get_v(bc);
309 last_size= get_v(bc); 290
310 if(nut->written_packet_size >= 0 && nut->written_packet_size != last_size){ 291 nut->packet_start[2] = start;
311 av_log(nut->avf, AV_LOG_ERROR, "packet size missmatch %d != %lld at %lld\n", nut->written_packet_size, last_size, start);
312 if(prefix_length<8)
313 return -1;
314 }
315
316 nut->last_packet_start = nut->packet_start;
317 nut->packet_start = start;
318 nut->written_packet_size= size; 292 nut->written_packet_size= size;
319 293
320 return size; 294 return size;
321 } 295 }
322 296
379 #ifdef CONFIG_ENCODERS 353 #ifdef CONFIG_ENCODERS
380 static void put_v(ByteIOContext *bc, uint64_t val) 354 static void put_v(ByteIOContext *bc, uint64_t val)
381 { 355 {
382 int i; 356 int i;
383 357
358 //av_log(NULL, AV_LOG_DEBUG, "put_v()= %lld\n", val);
384 val &= 0x7FFFFFFFFFFFFFFFULL; // FIXME can only encode upto 63 bits currently 359 val &= 0x7FFFFFFFFFFFFFFFULL; // FIXME can only encode upto 63 bits currently
385 i= get_length(val); 360 i= get_length(val);
386 361
387 for (i-=7; i>0; i-=7){ 362 for (i-=7; i>0; i-=7){
388 put_byte(bc, 0x80 | (val>>i)); 363 put_byte(bc, 0x80 | (val>>i));
412 } 387 }
413 388
414 static int put_packetheader(NUTContext *nut, ByteIOContext *bc, int max_size, int calculate_checksum) 389 static int put_packetheader(NUTContext *nut, ByteIOContext *bc, int max_size, int calculate_checksum)
415 { 390 {
416 put_flush_packet(bc); 391 put_flush_packet(bc);
417 nut->last_packet_start= nut->packet_start; 392 nut->packet_start[2]+= nut->written_packet_size;
418 nut->packet_start+= nut->written_packet_size; 393 assert(url_ftell(bc) - 8 == nut->packet_start[2]);
419 nut->packet_size_pos = url_ftell(bc);
420 nut->written_packet_size = max_size; 394 nut->written_packet_size = max_size;
421 395
422 if(calculate_checksum) 396 if(calculate_checksum)
423 init_checksum(bc, update_adler32, 0); 397 init_checksum(bc, update_adler32, 0);
424 398
425 /* packet header */ 399 /* packet header */
426 put_v(bc, nut->written_packet_size); /* forward ptr */ 400 put_v(bc, nut->written_packet_size); /* forward ptr */
427 put_v(bc, nut->packet_start - nut->last_packet_start); /* backward ptr */
428 401
429 return 0; 402 return 0;
430 } 403 }
431 404
432 static int update_packetheader(NUTContext *nut, ByteIOContext *bc, int additional_size, int calculate_checksum){ 405 static int update_packetheader(NUTContext *nut, ByteIOContext *bc, int additional_size, int calculate_checksum){
433 int64_t start= nut->packet_start; 406 int64_t start= nut->packet_start[2];
434 int64_t cur= url_ftell(bc); 407 int64_t cur= url_ftell(bc);
435 int size= cur - start + additional_size; 408 int size= cur - start + additional_size;
436 409
437 if(calculate_checksum) 410 if(calculate_checksum)
438 size += 4; 411 size += 4;
440 if(size != nut->written_packet_size){ 413 if(size != nut->written_packet_size){
441 int i; 414 int i;
442 415
443 assert( size <= nut->written_packet_size ); 416 assert( size <= nut->written_packet_size );
444 417
445 url_fseek(bc, nut->packet_size_pos, SEEK_SET); 418 url_fseek(bc, start + 8, SEEK_SET);
446 for(i=get_length(size); i < get_length(nut->written_packet_size); i+=7) 419 for(i=get_length(size); i < get_length(nut->written_packet_size); i+=7)
447 put_byte(bc, 0x80); 420 put_byte(bc, 0x80);
448 put_v(bc, size); 421 put_v(bc, size);
449 422
450 url_fseek(bc, cur, SEEK_SET); 423 url_fseek(bc, cur, SEEK_SET);
472 av_set_pts_info(s, 60, 1, AV_TIME_BASE); 445 av_set_pts_info(s, 60, 1, AV_TIME_BASE);
473 446
474 /* main header */ 447 /* main header */
475 put_be64(bc, MAIN_STARTCODE); 448 put_be64(bc, MAIN_STARTCODE);
476 put_packetheader(nut, bc, 120+5*256, 1); 449 put_packetheader(nut, bc, 120+5*256, 1);
477 put_v(bc, 1); /* version */ 450 put_v(bc, 2); /* version */
478 put_v(bc, s->nb_streams); 451 put_v(bc, s->nb_streams);
452 put_v(bc, MAX_DISTANCE);
479 453
480 build_frame_code(s); 454 build_frame_code(s);
481 assert(nut->frame_code['N'].flags == 1); 455 assert(nut->frame_code['N'].flags == 1);
482 for(i=0; i<256;){ 456 for(i=0; i<256;){
483 int tmp_flags = nut->frame_code[i].flags; 457 int tmp_flags = nut->frame_code[i].flags;
556 else 530 else
557 nut->stream[i].msb_timestamp_shift = 14; 531 nut->stream[i].msb_timestamp_shift = 14;
558 put_v(bc, nut->stream[i].msb_timestamp_shift); 532 put_v(bc, nut->stream[i].msb_timestamp_shift);
559 for(j=0; j<3; j++) 533 for(j=0; j<3; j++)
560 put_v(bc, nut->stream[i].initial_pts_predictor[j]); 534 put_v(bc, nut->stream[i].initial_pts_predictor[j]);
561 for(j=0; j<2; j++) 535 put_v(bc, codec->has_b_frames);
562 put_v(bc, nut->stream[i].initial_size_predictor[j]);
563 put_byte(bc, 0); /* flags: 0x1 - fixed_fps, 0x2 - index_present */ 536 put_byte(bc, 0); /* flags: 0x1 - fixed_fps, 0x2 - index_present */
564 537
565 if(codec->extradata_size){ 538 if(codec->extradata_size){
566 put_v(bc, 1); 539 put_v(bc, 1);
567 put_v(bc, codec->extradata_size); 540 put_v(bc, codec->extradata_size);
570 put_v(bc, 0); /* end of codec specific headers */ 543 put_v(bc, 0); /* end of codec specific headers */
571 544
572 switch(codec->codec_type) 545 switch(codec->codec_type)
573 { 546 {
574 case CODEC_TYPE_AUDIO: 547 case CODEC_TYPE_AUDIO:
575 put_v(bc, (codec->sample_rate * denom) / nom); 548 put_v(bc, codec->sample_rate);
549 put_v(bc, 1);
576 put_v(bc, codec->channels); 550 put_v(bc, codec->channels);
577 break; 551 break;
578 case CODEC_TYPE_VIDEO: 552 case CODEC_TYPE_VIDEO:
579 put_v(bc, codec->width); 553 put_v(bc, codec->width);
580 put_v(bc, codec->height); 554 put_v(bc, codec->height);
624 put_flush_packet(bc); 598 put_flush_packet(bc);
625 599
626 return 0; 600 return 0;
627 } 601 }
628 602
603 static int64_t lsb2full(StreamContext *stream, int64_t lsb){
604 int64_t mask = (1<<stream->msb_timestamp_shift)-1;
605 int64_t delta= stream->last_pts - mask/2;
606 return ((lsb - delta)&mask) + delta;
607 }
608
629 static int nut_write_packet(AVFormatContext *s, int stream_index, 609 static int nut_write_packet(AVFormatContext *s, int stream_index,
630 const uint8_t *buf, int size, int64_t pts) 610 const uint8_t *buf, int size, int64_t pts)
631 { 611 {
632 NUTContext *nut = s->priv_data; 612 NUTContext *nut = s->priv_data;
633 StreamContext *stream= &nut->stream[stream_index]; 613 StreamContext *stream= &nut->stream[stream_index];
634 ByteIOContext *bc = &s->pb; 614 ByteIOContext *bc = &s->pb;
635 int key_frame = 0, full_pts=0; 615 int key_frame = 0, full_pts=0;
636 AVCodecContext *enc; 616 AVCodecContext *enc;
637 int64_t lsb_pts, delta_pts; 617 int64_t coded_pts;
638 int frame_type, best_length, frame_code, flags, i, size_mul, size_lsb; 618 int frame_type, best_length, frame_code, flags, i, size_mul, size_lsb;
639 const int64_t frame_start= url_ftell(bc); 619 const int64_t frame_start= url_ftell(bc);
640 620
641 if (stream_index > s->nb_streams) 621 if (stream_index > s->nb_streams)
642 return 1; 622 return 1;
645 625
646 enc = &s->streams[stream_index]->codec; 626 enc = &s->streams[stream_index]->codec;
647 key_frame = enc->coded_frame->key_frame; 627 key_frame = enc->coded_frame->key_frame;
648 if(enc->coded_frame->pts != AV_NOPTS_VALUE) 628 if(enc->coded_frame->pts != AV_NOPTS_VALUE)
649 pts= (av_rescale(enc->coded_frame->pts, stream->rate_num, stream->rate_den) + AV_TIME_BASE/2) / AV_TIME_BASE; //FIXME XXX HACK 629 pts= (av_rescale(enc->coded_frame->pts, stream->rate_num, stream->rate_den) + AV_TIME_BASE/2) / AV_TIME_BASE; //FIXME XXX HACK
650 delta_pts= pts - stream->last_pts;
651 630
652 frame_type=0; 631 frame_type=0;
653 if(frame_start + size + 20 - FFMAX(nut->last_frame_start[1], nut->last_frame_start[2]) > MAX_TYPE1_DISTANCE) 632 if(frame_start + size + 20 - FFMAX(nut->packet_start[1], nut->packet_start[2]) > MAX_DISTANCE)
654 frame_type=1; 633 frame_type=2;
655 if(key_frame){ 634 if(key_frame && !stream->last_key_frame)
656 if(frame_type==1 && frame_start + size - nut->last_frame_start[2] > MAX_TYPE2_DISTANCE) 635 frame_type=2;
657 frame_type=2; 636
658 if(!stream->last_key_frame) 637 if(frame_type>1)
659 frame_type=2;
660 }
661
662 if(frame_type>0){
663 update_packetheader(nut, bc, 0, 0);
664 reset(s); 638 reset(s);
639 if(stream->last_pts == AV_NOPTS_VALUE)
665 full_pts=1; 640 full_pts=1;
666 } 641 else{
667 //FIXME ensure that the timestamp can be represented by either delta or lsb or full_pts=1 642 coded_pts = pts & ((1<<stream->msb_timestamp_shift)-1);
668 643 if(lsb2full(stream, coded_pts) != pts)
669 lsb_pts = pts & ((1 << stream->msb_timestamp_shift)-1); 644 full_pts=1;
645 }
646
647 if(full_pts)
648 coded_pts= pts + (1<<stream->msb_timestamp_shift);
670 649
671 best_length=INT_MAX; 650 best_length=INT_MAX;
672 frame_code= -1; 651 frame_code= -1;
673 for(i=0; i<256; i++){ 652 for(i=0; i<256; i++){
674 int stream_id_plus1= nut->frame_code[i].stream_id_plus1; 653 int stream_id_plus1= nut->frame_code[i].stream_id_plus1;
676 int length=0; 655 int length=0;
677 size_mul= nut->frame_code[i].size_mul; 656 size_mul= nut->frame_code[i].size_mul;
678 size_lsb= nut->frame_code[i].size_lsb; 657 size_lsb= nut->frame_code[i].size_lsb;
679 flags= nut->frame_code[i].flags; 658 flags= nut->frame_code[i].flags;
680 659
660 assert(size_mul > size_lsb);
661
681 if(stream_id_plus1 == 0) length+= get_length(stream_index); 662 if(stream_id_plus1 == 0) length+= get_length(stream_index);
682 else if(stream_id_plus1 - 1 != stream_index) 663 else if(stream_id_plus1 - 1 != stream_index)
683 continue; 664 continue;
684 fc_key_frame= !!(flags & FLAG_KEY_FRAME); 665 fc_key_frame= !!(flags & FLAG_KEY_FRAME);
685 666
686 assert(key_frame==0 || key_frame==1); 667 assert(key_frame==0 || key_frame==1);
687 if(fc_key_frame != key_frame) 668 if(fc_key_frame != key_frame)
688 continue; 669 continue;
689 670
690 if((!!(flags & FLAG_FRAME_TYPE)) != (frame_type > 0)) 671 if(flags & FLAG_DATA_SIZE){
691 continue;
692
693 if(size_mul <= size_lsb){
694 int p= stream->lru_size[size_lsb - size_mul];
695 if(p != size)
696 continue;
697 }else{
698 if(size % size_mul != size_lsb) 672 if(size % size_mul != size_lsb)
699 continue; 673 continue;
700 if(flags & FLAG_DATA_SIZE) 674 length += get_length(size / size_mul);
701 length += get_length(size / size_mul); 675 }else if(size != size_lsb)
702 else if(size/size_mul) 676 continue;
703 continue; 677
704 } 678 if(full_pts && (flags & FLAG_PTS) != FLAG_PTS)
705
706 if(full_pts != ((flags & FLAG_PTS) && (flags & FLAG_FULL_PTS)))
707 continue; 679 continue;
708 680
709 if(flags&FLAG_PTS){ 681 if(flags&FLAG_PTS){
710 if(flags&FLAG_FULL_PTS){ 682 length += get_length(coded_pts);
711 length += get_length(pts);
712 }else{
713 length += get_length(lsb_pts);
714 }
715 }else{ 683 }else{
716 int delta= stream->lru_pts_delta[(flags & 12)>>2]; 684 int delta= stream->lru_pts_delta[flags & FLAG_PTS];
717 if(delta != pts - stream->last_pts) 685 if(delta != pts - stream->last_pts)
718 continue; 686 continue;
719 assert(frame_type == 0); 687 assert(frame_type < 2);
720 } 688 }
721 689
722 if(length < best_length){ 690 if(length < best_length){
723 best_length= length; 691 best_length= length;
724 frame_code=i; 692 frame_code=i;
731 size_mul= nut->frame_code[frame_code].size_mul; 699 size_mul= nut->frame_code[frame_code].size_mul;
732 size_lsb= nut->frame_code[frame_code].size_lsb; 700 size_lsb= nut->frame_code[frame_code].size_lsb;
733 #if 0 701 #if 0
734 best_length /= 7; 702 best_length /= 7;
735 best_length ++; //frame_code 703 best_length ++; //frame_code
736 if(frame_type>0){ 704 if(frame_type==2){
737 best_length += 4; //packet header 705 best_length += 8; // startcode
738 if(frame_type>1) 706 }
739 best_length += 8; // startcode 707 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);
740 } 708 // 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]);
741 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\n", key_frame, frame_type, full_pts ? 2 : ((flags & 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));
742 #endif 709 #endif
743 710
744 if (frame_type==2) 711 if (frame_type==2)
745 put_be64(bc, KEYFRAME_STARTCODE); 712 put_be64(bc, KEYFRAME_STARTCODE);
713 assert(frame_type != 1); //short startcode not implemented yet
746 put_byte(bc, frame_code); 714 put_byte(bc, frame_code);
747 715
748 if(frame_type>0)
749 put_packetheader(nut, bc, FFMAX(size+20, MAX_TYPE1_DISTANCE), 0);
750 if(nut->frame_code[frame_code].stream_id_plus1 == 0) 716 if(nut->frame_code[frame_code].stream_id_plus1 == 0)
751 put_v(bc, stream_index); 717 put_v(bc, stream_index);
752 if (flags & FLAG_PTS){ 718 if ((flags & FLAG_PTS) == FLAG_PTS){
753 if (flags & FLAG_FULL_PTS) 719 put_v(bc, coded_pts);
754 put_v(bc, pts);
755 else
756 put_v(bc, lsb_pts);
757 } 720 }
758 if(flags & FLAG_DATA_SIZE) 721 if(flags & FLAG_DATA_SIZE)
759 put_v(bc, size / size_mul); 722 put_v(bc, size / size_mul);
760 if(size > MAX_TYPE1_DISTANCE){ 723 if(size > MAX_DISTANCE){
761 assert(frame_type > 0); 724 assert(frame_type > 1);
762 update_packetheader(nut, bc, size, 0);
763 } 725 }
764 726
765 put_buffer(bc, buf, size); 727 put_buffer(bc, buf, size);
766 728
767 update(nut, stream_index, frame_start, frame_type, frame_code, key_frame, size, pts); 729 update(nut, stream_index, frame_start, frame_type, frame_code, key_frame, size, pts);
771 733
772 static int nut_write_trailer(AVFormatContext *s) 734 static int nut_write_trailer(AVFormatContext *s)
773 { 735 {
774 NUTContext *nut = s->priv_data; 736 NUTContext *nut = s->priv_data;
775 ByteIOContext *bc = &s->pb; 737 ByteIOContext *bc = &s->pb;
776
777 update_packetheader(nut, bc, 0, 0);
778 738
779 #if 0 739 #if 0
780 int i; 740 int i;
781 741
782 /* WRITE INDEX */ 742 /* WRITE INDEX */
800 #endif //CONFIG_ENCODERS 760 #endif //CONFIG_ENCODERS
801 761
802 static int nut_probe(AVProbeData *p) 762 static int nut_probe(AVProbeData *p)
803 { 763 {
804 int i; 764 int i;
805 uint64_t code; 765 uint64_t code= 0xff;
806 766
807 code = 0xff;
808 for (i = 0; i < p->buf_size; i++) { 767 for (i = 0; i < p->buf_size; i++) {
809 int c = p->buf[i]; 768 code = (code << 8) | p->buf[i];
810 code = (code << 8) | c;
811 if (code == MAIN_STARTCODE) 769 if (code == MAIN_STARTCODE)
812 return AVPROBE_SCORE_MAX; 770 return AVPROBE_SCORE_MAX;
813 } 771 }
814 return 0; 772 return 0;
815 } 773 }
818 AVFormatContext *s= nut->avf; 776 AVFormatContext *s= nut->avf;
819 ByteIOContext *bc = &s->pb; 777 ByteIOContext *bc = &s->pb;
820 uint64_t tmp; 778 uint64_t tmp;
821 int i, j; 779 int i, j;
822 780
823 get_packetheader(nut, bc, 8, 1); 781 get_packetheader(nut, bc, 1);
824 782
825 tmp = get_v(bc); 783 tmp = get_v(bc);
826 if (tmp != 1){ 784 if (tmp != 2){
827 av_log(s, AV_LOG_ERROR, "bad version (%Ld)\n", tmp); 785 av_log(s, AV_LOG_ERROR, "bad version (%Ld)\n", tmp);
828 return -1; 786 return -1;
829 } 787 }
830 788
831 nut->stream_count = get_v(bc); 789 nut->stream_count = get_v(bc);
832 790 nut->max_distance = get_v(bc);
791
833 for(i=0; i<256;){ 792 for(i=0; i<256;){
834 int tmp_flags = get_v(bc); 793 int tmp_flags = get_v(bc);
835 int tmp_stream= get_v(bc); 794 int tmp_stream= get_v(bc);
836 int tmp_mul = get_v(bc); 795 int tmp_mul = get_v(bc);
837 int tmp_size = get_v(bc); 796 int tmp_size = get_v(bc);
838 int count = get_v(bc); 797 int count = get_v(bc);
839 798
840 if(count == 0 || i+count > 256){ 799 if(count == 0 || i+count > 256){
841 av_log(s, AV_LOG_ERROR, "illegal count %d at %d\n", count, i); 800 av_log(s, AV_LOG_ERROR, "illegal count %d at %d\n", count, i);
842 return -1; 801 return -1;
843 }
844
845 if((tmp_flags & FLAG_FRAME_TYPE) && tmp_flags != 1){
846 if(!(tmp_flags & FLAG_PTS) || !(tmp_flags & FLAG_FULL_PTS) ){
847 av_log(s, AV_LOG_ERROR, "no full pts in non 0 frame type\n");
848 return -1;
849 }
850 } 802 }
851 803
852 for(j=0; j<count; j++,i++){ 804 for(j=0; j<count; j++,i++){
853 if(tmp_stream > nut->stream_count + 1){ 805 if(tmp_stream > nut->stream_count + 1){
854 av_log(s, AV_LOG_ERROR, "illegal stream number\n"); 806 av_log(s, AV_LOG_ERROR, "illegal stream number\n");
883 ByteIOContext *bc = &s->pb; 835 ByteIOContext *bc = &s->pb;
884 int class, nom, denom, stream_id, i; 836 int class, nom, denom, stream_id, i;
885 uint64_t tmp; 837 uint64_t tmp;
886 AVStream *st; 838 AVStream *st;
887 839
888 get_packetheader(nut, bc, 8, 1); 840 get_packetheader(nut, bc, 1);
889 stream_id= get_v(bc); 841 stream_id= get_v(bc);
890 if(stream_id >= nut->stream_count || s->streams[stream_id]) 842 if(stream_id >= nut->stream_count || s->streams[stream_id])
891 return -1; 843 return -1;
892 844
893 st = av_new_stream(s, stream_id); 845 st = av_new_stream(s, stream_id);
918 get_vb(bc); /* language code */ 870 get_vb(bc); /* language code */
919 nom = get_v(bc); 871 nom = get_v(bc);
920 denom = get_v(bc); 872 denom = get_v(bc);
921 nut->stream[stream_id].msb_timestamp_shift = get_v(bc); 873 nut->stream[stream_id].msb_timestamp_shift = get_v(bc);
922 for(i=0; i<3; i++) 874 for(i=0; i<3; i++)
923 nut->stream[stream_id].initial_pts_predictor[i]= get_v(bc); 875 nut->stream[stream_id].initial_pts_predictor[i]= get_v(bc);
924 for(i=0; i<2; i++) 876 nut->stream[stream_id].decode_delay= get_v(bc);
925 nut->stream[stream_id].initial_size_predictor[i]= get_v(bc);
926 get_byte(bc); /* flags */ 877 get_byte(bc); /* flags */
927 878
928 /* codec specific data headers */ 879 /* codec specific data headers */
929 while(get_v(bc) != 0){ 880 while(get_v(bc) != 0){
930 st->codec.extradata_size= get_v(bc); 881 st->codec.extradata_size= get_v(bc);
944 st->codec.frame_rate = nom; 895 st->codec.frame_rate = nom;
945 st->codec.frame_rate_base = denom; 896 st->codec.frame_rate_base = denom;
946 } 897 }
947 if (class == 32) /* AUDIO */ 898 if (class == 32) /* AUDIO */
948 { 899 {
949 st->codec.sample_rate = (get_v(bc) * nom) / denom; 900 st->codec.sample_rate = get_v(bc);
901 get_v(bc); // samplerate_den
950 st->codec.channels = get_v(bc); 902 st->codec.channels = get_v(bc);
951 } 903 }
952 if(check_checksum(bc)){ 904 if(check_checksum(bc)){
953 av_log(s, AV_LOG_ERROR, "Stream header %d checksum missmatch\n", stream_id); 905 av_log(s, AV_LOG_ERROR, "Stream header %d checksum missmatch\n", stream_id);
954 return -1; 906 return -1;
960 912
961 static int decode_info_header(NUTContext *nut){ 913 static int decode_info_header(NUTContext *nut){
962 AVFormatContext *s= nut->avf; 914 AVFormatContext *s= nut->avf;
963 ByteIOContext *bc = &s->pb; 915 ByteIOContext *bc = &s->pb;
964 916
965 get_packetheader(nut, bc, 8, 1); 917 get_packetheader(nut, bc, 1);
966 918
967 for(;;){ 919 for(;;){
968 int id= get_v(bc); 920 int id= get_v(bc);
969 char *name, *type, custom_name[256], custom_type[256]; 921 char *name, *type, custom_name[256], custom_type[256];
970 922
1070 } 1022 }
1071 1023
1072 return 0; 1024 return 0;
1073 } 1025 }
1074 1026
1075 static int decode_frame(NUTContext *nut, AVPacket *pkt, int frame_code, int frame_type){ 1027 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){
1076 AVFormatContext *s= nut->avf; 1028 AVFormatContext *s= nut->avf;
1077 StreamContext *stream; 1029 StreamContext *stream;
1078 ByteIOContext *bc = &s->pb; 1030 ByteIOContext *bc = &s->pb;
1079 int size, flags, size_mul, size_lsb, stream_id; 1031 int size, flags, size_mul, size_lsb, stream_id;
1080 int key_frame = 0;
1081 int64_t pts = 0; 1032 int64_t pts = 0;
1082 const int prefix_len= frame_type == 2 ? 8+1 : 1; 1033 const int prefix_len= frame_type == 2 ? 8+1 : 1;
1083 const int64_t frame_start= url_ftell(bc) - prefix_len; 1034 const int64_t frame_start= url_ftell(bc) - prefix_len;
1084 1035
1036 if(frame_type < 2 && frame_start - nut->packet_start[2] > nut->max_distance){
1037 av_log(s, AV_LOG_ERROR, "last frame must have been damaged\n");
1038 return -1;
1039 }
1040
1041 if(frame_type)
1042 nut->packet_start[ frame_type ]= frame_start; //otherwise 1 goto 1 may happen
1043
1085 flags= nut->frame_code[frame_code].flags; 1044 flags= nut->frame_code[frame_code].flags;
1086 size_mul= nut->frame_code[frame_code].size_mul; 1045 size_mul= nut->frame_code[frame_code].size_mul;
1087 size_lsb= nut->frame_code[frame_code].size_lsb; 1046 size_lsb= nut->frame_code[frame_code].size_lsb;
1088 stream_id= nut->frame_code[frame_code].stream_id_plus1 - 1; 1047 stream_id= nut->frame_code[frame_code].stream_id_plus1 - 1;
1089 1048
1090 if(flags & FLAG_FRAME_TYPE){ 1049 if(frame_type == 2)
1091 reset(s); 1050 reset(s);
1092 if(get_packetheader(nut, bc, prefix_len, 0) < 0)
1093 return -1;
1094 if(frame_type!=2)
1095 frame_type= 1;
1096 }
1097 1051
1098 if(stream_id==-1) 1052 if(stream_id==-1)
1099 stream_id= get_v(bc); 1053 stream_id= get_v(bc);
1100 if(stream_id >= s->nb_streams){ 1054 if(stream_id >= s->nb_streams){
1101 av_log(s, AV_LOG_ERROR, "illegal stream_id\n"); 1055 av_log(s, AV_LOG_ERROR, "illegal stream_id\n");
1102 return -1; 1056 return -1;
1103 } 1057 }
1104 stream= &nut->stream[stream_id]; 1058 stream= &nut->stream[stream_id];
1105 1059
1106 // av_log(s, AV_LOG_DEBUG, "ft:%d ppts:%d %d %d\n", frame_type, stream->lru_pts_delta[0], stream->lru_pts_delta[1], stream->lru_pts_delta[2]); 1060 // av_log(s, AV_LOG_DEBUG, "ft:%d ppts:%d %d %d\n", frame_type, stream->lru_pts_delta[0], stream->lru_pts_delta[1], stream->lru_pts_delta[2]);
1107 1061
1108 key_frame= !!(flags & FLAG_KEY_FRAME); 1062 *key_frame_ret= !!(flags & FLAG_KEY_FRAME);
1109 1063
1110 if(flags & FLAG_PTS){ 1064 if((flags & FLAG_PTS) == FLAG_PTS){
1111 if(flags & FLAG_FULL_PTS){ 1065 int64_t mask = (1<<stream->msb_timestamp_shift)-1;
1112 pts= get_v(bc); 1066 pts= get_v(bc);
1113 if(frame_type && key_frame){ 1067 if(pts > mask){
1114 int64_t av_pts= pts * AV_TIME_BASE * stream->rate_den / stream->rate_num; 1068 pts -= mask+1;
1115 av_add_index_entry( 1069 }else{
1116 s->streams[stream_id], 1070 if(stream->last_pts == AV_NOPTS_VALUE){
1117 frame_start, 1071 av_log(s, AV_LOG_ERROR, "no reference pts available\n");
1118 av_pts, 1072 return -1;
1119 frame_start - nut->stream[stream_id].last_sync_pos,
1120 AVINDEX_KEYFRAME);
1121 nut->stream[stream_id].last_sync_pos= frame_start;
1122 assert(nut->packet_start == frame_start);
1123 } 1073 }
1124 }else{ 1074 pts= lsb2full(stream, pts);
1125 int64_t mask = (1<<stream->msb_timestamp_shift)-1;
1126 int64_t delta= stream->last_pts - mask/2;
1127 pts= ((get_v(bc) - delta)&mask) + delta;
1128 } 1075 }
1129 }else{ 1076 }else{
1130 pts= stream->last_pts + stream->lru_pts_delta[(flags&12)>>2]; 1077 if(stream->last_pts == AV_NOPTS_VALUE){
1131 } 1078 av_log(s, AV_LOG_ERROR, "no reference pts available\n");
1132 1079 return -1;
1133 if(size_mul <= size_lsb){ 1080 }
1134 size= stream->lru_size[size_lsb - size_mul]; 1081 pts= stream->last_pts + stream->lru_pts_delta[flags&FLAG_PTS];
1135 }else{ 1082 }
1136 if(flags & FLAG_DATA_SIZE) 1083
1137 size= size_mul*get_v(bc) + size_lsb; 1084 if(*key_frame_ret){
1138 else 1085 int64_t av_pts= pts * AV_TIME_BASE * stream->rate_den / stream->rate_num;
1139 size= size_lsb; 1086 // av_log(s, AV_LOG_DEBUG, "stream:%d start:%lld pts:%lld length:%lld\n",stream_id, frame_start, av_pts, frame_start - nut->stream[stream_id].last_sync_pos);
1140 } 1087 av_add_index_entry(
1088 s->streams[stream_id],
1089 frame_start,
1090 av_pts,
1091 frame_start - nut->stream[stream_id].last_sync_pos,
1092 AVINDEX_KEYFRAME);
1093 nut->stream[stream_id].last_sync_pos= frame_start;
1094 // assert(nut->packet_start == frame_start);
1095 }
1096
1097 assert(size_mul > size_lsb);
1098 size= size_lsb;
1099 if(flags & FLAG_DATA_SIZE)
1100 size+= size_mul*get_v(bc);
1141 1101
1142 //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); 1102 //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);
1143 1103
1144 if(url_ftell(bc) - nut->packet_start + size > nut->written_packet_size){ 1104 if(frame_type==0 && url_ftell(bc) - nut->packet_start[2] + size > nut->max_distance){
1145 av_log(s, AV_LOG_ERROR, "frame size too large\n"); 1105 av_log(s, AV_LOG_ERROR, "frame size too large\n");
1146 return -1; 1106 return -1;
1147 } 1107 }
1148 1108
1109 *stream_id_ret = stream_id;
1110 *pts_ret = pts * AV_TIME_BASE * stream->rate_den / stream->rate_num;
1111
1112 update(nut, stream_id, frame_start, frame_type, frame_code, *key_frame_ret, size, pts);
1113
1114 return size;
1115 }
1116
1117 static int decode_frame(NUTContext *nut, AVPacket *pkt, int frame_code, int frame_type){
1118 AVFormatContext *s= nut->avf;
1119 ByteIOContext *bc = &s->pb;
1120 int size, stream_id, key_frame;
1121 int64_t pts;
1122
1123 size= decode_frame_header(nut, &key_frame, &pts, &stream_id, frame_code, frame_type);
1124 if(size < 0)
1125 return -1;
1126
1149 av_new_packet(pkt, size); 1127 av_new_packet(pkt, size);
1150 get_buffer(bc, pkt->data, size); 1128 get_buffer(bc, pkt->data, size);
1151 pkt->stream_index = stream_id; 1129 pkt->stream_index = stream_id;
1152 if (key_frame) 1130 if (key_frame)
1153 pkt->flags |= PKT_FLAG_KEY; 1131 pkt->flags |= PKT_FLAG_KEY;
1154 pkt->pts = pts * AV_TIME_BASE * stream->rate_den / stream->rate_num; 1132 pkt->pts = pts;
1155 1133
1156 update(nut, stream_id, frame_start, frame_type, frame_code, key_frame, size, pts);
1157
1158 return 0; 1134 return 0;
1159 } 1135 }
1160 1136
1161 static int nut_read_packet(AVFormatContext *s, AVPacket *pkt) 1137 static int nut_read_packet(AVFormatContext *s, AVPacket *pkt)
1162 { 1138 {
1163 NUTContext *nut = s->priv_data; 1139 NUTContext *nut = s->priv_data;
1164 ByteIOContext *bc = &s->pb; 1140 ByteIOContext *bc = &s->pb;
1165 int size, i, frame_code=0; 1141 int i, frame_code=0;
1166 int64_t pos;
1167 1142
1168 for(;;){ 1143 for(;;){
1169 int frame_type= 0; 1144 int frame_type= 0;
1170 uint64_t tmp= nut->next_startcode; 1145 uint64_t tmp= nut->next_startcode;
1171 nut->next_startcode=0; 1146 nut->next_startcode=0;
1183 } 1158 }
1184 switch(tmp){ 1159 switch(tmp){
1185 case MAIN_STARTCODE: 1160 case MAIN_STARTCODE:
1186 case STREAM_STARTCODE: 1161 case STREAM_STARTCODE:
1187 case INDEX_STARTCODE: 1162 case INDEX_STARTCODE:
1188 get_packetheader(nut, bc, 8, 0); 1163 get_packetheader(nut, bc, 0);
1189 url_fseek(bc, nut->written_packet_size + nut->packet_start, SEEK_SET); 1164 url_fseek(bc, nut->written_packet_size + nut->packet_start[2], SEEK_SET);
1190 break; 1165 break;
1191 case INFO_STARTCODE: 1166 case INFO_STARTCODE:
1192 if(decode_info_header(nut)<0) 1167 if(decode_info_header(nut)<0)
1193 goto resync; 1168 goto resync;
1194 break; 1169 break;
1198 case 0: 1173 case 0:
1199 if(decode_frame(nut, pkt, frame_code, frame_type)>=0) 1174 if(decode_frame(nut, pkt, frame_code, frame_type)>=0)
1200 return 0; 1175 return 0;
1201 default: 1176 default:
1202 resync: 1177 resync:
1203 av_log(s, AV_LOG_DEBUG, "syncing from %lld\n", nut->packet_start+1); 1178 av_log(s, AV_LOG_DEBUG, "syncing from %lld\n", nut->packet_start[2]+1);
1204 tmp= find_any_startcode(bc, nut->packet_start+1); 1179 tmp= find_any_startcode(bc, nut->packet_start[2]+1);
1205 if(tmp==0) 1180 if(tmp==0)
1206 return -1; 1181 return -1;
1207 av_log(s, AV_LOG_DEBUG, "sync\n"); 1182 av_log(s, AV_LOG_DEBUG, "sync\n");
1208 if(url_is_streamed(bc)){ 1183 nut->next_startcode= tmp;
1209 nut->next_startcode= tmp;
1210 break;
1211 }
1212
1213 pos= url_ftell(bc) - 8;
1214 av_log(s, AV_LOG_DEBUG, "at %lld code=%llX\n", pos, tmp);
1215 if(tmp==KEYFRAME_STARTCODE){
1216 get_byte(bc);
1217 }
1218 get_v(bc);
1219 size= get_v(bc);
1220
1221 while(size > 2 && size < 100000 && nut->packet_start < pos - size){
1222 url_fseek(bc, pos - size, SEEK_SET);
1223 frame_code= get_byte(bc);
1224 if(!(nut->frame_code[ frame_code ].flags & FLAG_FRAME_TYPE))
1225 break;
1226 if(get_v(bc) != size)
1227 break;
1228 pos -= size;
1229 size= get_v(bc);
1230 av_log(s, AV_LOG_DEBUG, "steping back to %lld next %d\n", pos, size);
1231 }
1232 url_fseek(bc, pos, SEEK_SET);
1233
1234 nut->written_packet_size= -1;
1235 } 1184 }
1236 } 1185 }
1237 } 1186 }
1238 1187
1239 static int64_t nut_read_timestamp(AVFormatContext *s, int stream_index, int64_t *pos_arg, int64_t pos_limit){ 1188 static int64_t nut_read_timestamp(AVFormatContext *s, int stream_index, int64_t *pos_arg, int64_t pos_limit){
1240 NUTContext *nut = s->priv_data; 1189 NUTContext *nut = s->priv_data;
1241 StreamContext *stream; 1190 StreamContext *stream;
1242 ByteIOContext *bc = &s->pb; 1191 ByteIOContext *bc = &s->pb;
1243 int64_t pos, pts; 1192 int64_t pos, pts;
1244 uint64_t code; 1193 uint64_t code;
1245 int frame_code,step, flags, stream_id, i; 1194 int frame_code,step, stream_id, i,size, key_frame;
1246 av_log(s, AV_LOG_DEBUG, "read_timestamp(X,%d,%lld,%lld)\n", stream_index, *pos_arg, pos_limit); 1195 av_log(s, AV_LOG_DEBUG, "read_timestamp(X,%d,%lld,%lld)\n", stream_index, *pos_arg, pos_limit);
1247 1196
1248 if(*pos_arg < 0) 1197 if(*pos_arg < 0)
1249 return AV_NOPTS_VALUE; 1198 return AV_NOPTS_VALUE;
1250 1199
1251 // find a previous startcode, FIXME use forward search and follow backward pointers if undamaged stream
1252 pos= *pos_arg; 1200 pos= *pos_arg;
1253 step= FFMIN(16*1024, pos); 1201 step= FFMIN(16*1024, pos);
1254 do{ 1202 do{
1255 pos-= step; 1203 pos-= step;
1256 code= find_any_startcode(bc, pos); 1204 code= find_any_startcode(bc, pos);
1257 1205
1258 if(code && url_ftell(bc) - 8 < *pos_arg) 1206 if(code && url_ftell(bc) - 8 <= *pos_arg)
1259 break; 1207 break;
1260 step= FFMIN(2*step, pos); 1208 step= FFMIN(2*step, pos);
1261 }while(step); 1209 }while(step);
1262 1210
1263 if(!code) //nothing found, not even after pos_arg 1211 if(!code) //nothing found, not even after pos_arg
1266 url_fseek(bc, -8, SEEK_CUR); 1214 url_fseek(bc, -8, SEEK_CUR);
1267 for(i=0; i<s->nb_streams; i++) 1215 for(i=0; i<s->nb_streams; i++)
1268 nut->stream[i].last_sync_pos= url_ftell(bc); 1216 nut->stream[i].last_sync_pos= url_ftell(bc);
1269 1217
1270 for(;;){ 1218 for(;;){
1219 int frame_type=0;
1271 int64_t pos= url_ftell(bc); 1220 int64_t pos= url_ftell(bc);
1272 uint64_t tmp=0; 1221 uint64_t tmp=0;
1273 int prefix_len=1;
1274 1222
1275 if(pos > pos_limit) 1223 if(pos > pos_limit || url_feof(bc))
1276 return AV_NOPTS_VALUE; 1224 return AV_NOPTS_VALUE;
1277 1225
1278 frame_code = get_byte(bc); 1226 frame_code = get_byte(bc);
1279 if(frame_code == 'N'){ 1227 if(frame_code == 'N'){
1280 tmp= frame_code; 1228 tmp= frame_code;
1286 switch(tmp){ 1234 switch(tmp){
1287 case MAIN_STARTCODE: 1235 case MAIN_STARTCODE:
1288 case STREAM_STARTCODE: 1236 case STREAM_STARTCODE:
1289 case INDEX_STARTCODE: 1237 case INDEX_STARTCODE:
1290 case INFO_STARTCODE: 1238 case INFO_STARTCODE:
1291 nut->written_packet_size= -1; 1239 get_packetheader(nut, bc, 0);
1292 get_packetheader(nut, bc, 8, 0); 1240 assert(nut->packet_start[2]==pos);
1293 url_fseek(bc, nut->written_packet_size + nut->packet_start, SEEK_SET); 1241 url_fseek(bc, nut->written_packet_size + pos, SEEK_SET);
1294 break; 1242 break;
1295 case KEYFRAME_STARTCODE: 1243 case KEYFRAME_STARTCODE:
1296 nut->written_packet_size= -1; 1244 frame_type=2;
1297 prefix_len+=8;
1298 frame_code = get_byte(bc); 1245 frame_code = get_byte(bc);
1299 case 0: 1246 case 0:
1300 flags= nut->frame_code[frame_code].flags; 1247 size= decode_frame_header(nut, &key_frame, &pts, &stream_id, frame_code, frame_type);
1301 stream_id= nut->frame_code[frame_code].stream_id_plus1 - 1; 1248 if(size < 0)
1302
1303 if(get_packetheader(nut, bc, prefix_len, 0) < 0)
1304 goto resync;
1305
1306 if(!(flags & FLAG_FRAME_TYPE) || !(flags & FLAG_PTS) || !(flags & FLAG_FULL_PTS))
1307 goto resync;
1308
1309 if(stream_id==-1)
1310 stream_id= get_v(bc);
1311 if(stream_id >= s->nb_streams)
1312 goto resync; 1249 goto resync;
1313 1250
1314 stream= &nut->stream[stream_id]; 1251 stream= &nut->stream[stream_id];
1315 pts= get_v(bc) * AV_TIME_BASE * stream->rate_den / stream->rate_num; 1252 if(stream_id != stream_index || !key_frame || pos < *pos_arg){
1316 1253 url_fseek(bc, size, SEEK_CUR);
1317 if(flags & FLAG_KEY_FRAME){
1318 av_add_index_entry(
1319 s->streams[stream_id],
1320 pos,
1321 pts,
1322 pos - nut->stream[stream_id].last_sync_pos,
1323 AVINDEX_KEYFRAME);
1324 nut->stream[stream_id].last_sync_pos= pos;
1325 }
1326 if(stream_id != stream_index || !(flags & FLAG_KEY_FRAME) || nut->packet_start < *pos_arg){
1327 url_fseek(bc, nut->written_packet_size + nut->packet_start, SEEK_SET);
1328 break; 1254 break;
1329 } 1255 }
1330 1256
1331 *pos_arg= nut->packet_start; 1257 *pos_arg= pos;
1332 assert(nut->packet_start == pos);
1333 return pts; 1258 return pts;
1334 default: 1259 default:
1335 resync: 1260 resync:
1336 av_log(s, AV_LOG_DEBUG, "syncing from %lld\n", nut->packet_start+1); 1261 av_log(s, AV_LOG_DEBUG, "syncing from %lld\n", nut->packet_start[2]+1);
1337 if(!find_any_startcode(bc, nut->packet_start+1)) 1262 if(!find_any_startcode(bc, nut->packet_start[2]+1))
1338 return AV_NOPTS_VALUE; 1263 return AV_NOPTS_VALUE;
1339 1264
1340 url_fseek(bc, -8, SEEK_CUR); 1265 url_fseek(bc, -8, SEEK_CUR);
1341 } 1266 }
1342 } 1267 }
1343 return AV_NOPTS_VALUE; 1268 return AV_NOPTS_VALUE;
1344 } 1269 }
1345 1270
1346 static int nut_read_seek(AVFormatContext *s, int stream_index, int64_t target_ts){ 1271 static int nut_read_seek(AVFormatContext *s, int stream_index, int64_t target_ts){
1347 NUTContext *nut = s->priv_data; 1272 // NUTContext *nut = s->priv_data;
1348 int64_t pos; 1273 int64_t pos;
1349 int i;
1350 1274
1351 if(av_seek_frame_binary(s, stream_index, target_ts) < 0) 1275 if(av_seek_frame_binary(s, stream_index, target_ts) < 0)
1352 return -1; 1276 return -1;
1353 1277
1354 nut->written_packet_size= -1;
1355 pos= url_ftell(&s->pb); 1278 pos= url_ftell(&s->pb);
1356 for(i=0; i<s->nb_streams; i++) 1279 nut_read_timestamp(s, stream_index, &pos, pos-1);
1357 nut->stream[i].last_sync_pos= pos;
1358 1280
1359 return 0; 1281 return 0;
1360 } 1282 }
1361 1283
1362 static int nut_read_close(AVFormatContext *s) 1284 static int nut_read_close(AVFormatContext *s)