]> git.sesse.net Git - ffmpeg/blob - libavformat/nut.c
removing keyframe prediction and checksum threshold
[ffmpeg] / libavformat / nut.c
1 /*
2  * "NUT" Container Format muxer and demuxer (DRAFT-200403??)
3  * Copyright (c) 2003 Alex Beregszaszi
4  * Copyright (c) 2004 Michael Niedermayer
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  * NUT DRAFT can be found in MPlayer CVS at DOCS/tech/mpcf.txt
21  *
22  * AND http://people.fsn.hu/~alex/nut/ (TeX, pdf, ps, dvi, ..)
23  *
24  */
25
26 /*
27  * TODO:
28  * - index writing
29  * - index packet reading support
30 */
31
32 //#define DEBUG 1
33
34 #include <limits.h>
35 #include "avformat.h"
36 #include "mpegaudio.h"
37 #include "avi.h"
38
39 #undef NDEBUG
40 #include <assert.h>
41
42 //from /dev/random
43
44 #define     MAIN_STARTCODE (0x7A561F5F04ADULL + (((uint64_t)('N'<<8) + 'M')<<48)) 
45 #define   STREAM_STARTCODE (0x11405BF2F9DBULL + (((uint64_t)('N'<<8) + 'S')<<48)) 
46 #define KEYFRAME_STARTCODE (0xE4ADEECA4569ULL + (((uint64_t)('N'<<8) + 'K')<<48)) 
47 #define    INDEX_STARTCODE (0xDD672F23E64EULL + (((uint64_t)('N'<<8) + 'X')<<48)) 
48 #define     INFO_STARTCODE (0xAB68B596BA78ULL + (((uint64_t)('N'<<8) + 'I')<<48)) 
49
50 #define MAX_TYPE1_DISTANCE (1024*16-1)
51 #define MAX_TYPE2_DISTANCE (1024*64-1)
52
53 #define MAX_SIZE_LRU 2
54 #define MAX_PTS_LRU 3
55
56 #define FLAG_FRAME_TYPE      1
57 #define FLAG_DATA_SIZE       2
58 #define FLAG_PTS            16
59 #define FLAG_FULL_PTS        4
60 #define FLAG_KEY_FRAME      32
61
62 typedef struct {
63     uint8_t flags;
64     uint8_t stream_id_plus1;
65     uint8_t size_mul;
66     uint8_t size_lsb;
67 } FrameCode;
68
69 typedef struct {
70     int last_key_frame;
71     int msb_timestamp_shift;
72     int rate_num;
73     int rate_den;
74     int64_t last_pts;
75     int64_t last_full_pts;
76     int lru_pts_delta[MAX_PTS_LRU];
77     int lru_size[MAX_SIZE_LRU];
78     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
81 } StreamContext;
82
83 typedef struct {
84     AVFormatContext *avf;
85     int64_t packet_start;
86     int64_t last_packet_start;
87     int written_packet_size;
88     int64_t packet_size_pos;
89     int64_t last_frame_start[3];
90     FrameCode frame_code[256];
91     int stream_count;
92     uint64_t next_startcode;     ///< stores the next startcode if it has alraedy been parsed but the stream isnt seekable
93     StreamContext *stream;
94 } NUTContext;
95
96 static char *info_table[][2]={
97         {NULL                   ,  NULL }, // end
98         {NULL                   ,  NULL },
99         {NULL                   , "UTF8"},
100         {NULL                   , "v"},
101         {NULL                   , "s"},
102         {"StreamId"             , "v"},
103         {"SegmentId"            , "v"},
104         {"StartTimestamp"       , "v"},
105         {"EndTimestamp"         , "v"},
106         {"Author"               , "UTF8"},
107         {"Title"                , "UTF8"},
108         {"Description"          , "UTF8"},
109         {"Copyright"            , "UTF8"},
110         {"Encoder"              , "UTF8"},
111         {"Keyword"              , "UTF8"},
112         {"Cover"                , "JPEG"},
113         {"Cover"                , "PNG"},
114 };
115
116 static void update_lru(int *lru, int current, int count){
117     int i;
118
119     for(i=0; i<count-1; i++){
120         if(lru[i] == current)
121             break;
122     }
123
124     for(; i; i--){
125         lru[i]= lru[i-1];
126     }
127
128     lru[0]= current;
129 }
130
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){
132     StreamContext *stream= &nut->stream[stream_index];
133     const int flags=nut->frame_code[frame_code].flags; 
134     
135     stream->last_key_frame= key_frame;
136     nut->last_frame_start[ frame_type ]= frame_start;
137     if(frame_type == 0)
138         update_lru(stream->lru_pts_delta, pts - stream->last_pts, 3);
139     update_lru(stream->lru_size, size, 2);
140     stream->last_pts= pts;
141     if((flags & FLAG_PTS) && (flags & FLAG_FULL_PTS))
142         stream->last_full_pts= pts;
143 }
144
145 static void reset(AVFormatContext *s/*, int frame_type*/){
146     NUTContext *nut = s->priv_data;
147     int i;
148     
149     for(i=0; i<s->nb_streams; i++){
150         StreamContext *stream= &nut->stream[i];
151     
152         stream->last_key_frame= 1;
153         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     }
156 }
157
158 static void build_frame_code(AVFormatContext *s){
159     NUTContext *nut = s->priv_data;
160     int key_frame, frame_type, full_pts, index, pred, stream_id;
161     int start=0;
162     int end= 255;
163     int keyframe_0_esc= s->nb_streams > 2;
164
165     if(keyframe_0_esc){
166         /* keyframe = 0 escapes, 3 codes */
167         for(frame_type=0; frame_type<2; frame_type++){
168             for(full_pts=frame_type; full_pts<2; full_pts++){
169                 FrameCode *ft= &nut->frame_code[start];
170                 ft->flags= FLAG_FRAME_TYPE*frame_type + FLAG_FULL_PTS*full_pts;
171                 ft->flags|= FLAG_DATA_SIZE | FLAG_PTS; 
172                 ft->stream_id_plus1= 0;
173                 ft->size_mul=1;
174                 start++;
175             }
176         }
177     }
178
179     for(stream_id= 0; stream_id<s->nb_streams; stream_id++){
180         int start2= start + (end-start)*stream_id / s->nb_streams;
181         int end2  = start + (end-start)*(stream_id+1) / s->nb_streams;
182         AVCodecContext *codec = &s->streams[stream_id]->codec;
183         int is_audio= codec->codec_type == CODEC_TYPE_AUDIO;
184         int intra_only= /*codec->intra_only || */is_audio;
185         int pred_count;
186
187         for(key_frame=0; key_frame<2; key_frame++){
188             if(intra_only && keyframe_0_esc && key_frame==0)
189                 continue;
190             
191             for(frame_type=0; frame_type<2; frame_type++){
192                 for(full_pts=frame_type; full_pts<2; full_pts++){
193                     FrameCode *ft= &nut->frame_code[start2];
194                     ft->flags= FLAG_FRAME_TYPE*frame_type + FLAG_FULL_PTS*full_pts + FLAG_KEY_FRAME*key_frame;
195                     ft->flags|= FLAG_DATA_SIZE | FLAG_PTS;
196                     ft->stream_id_plus1= stream_id + 1;
197                     ft->size_mul=1;
198                     start2++;
199                 }
200             }
201         }
202
203         key_frame= intra_only;
204 #if 1
205         if(is_audio){
206             int frame_bytes= codec->frame_size*(int64_t)codec->bit_rate / (8*codec->sample_rate);
207             for(pred=0; pred<MAX_SIZE_LRU; pred++){
208                 for(frame_type=0; frame_type<1; frame_type++){
209                     FrameCode *ft= &nut->frame_code[start2];
210                     ft->flags= FLAG_KEY_FRAME*key_frame + (FLAG_FULL_PTS+FLAG_PTS+FLAG_FRAME_TYPE)*frame_type;
211                     ft->stream_id_plus1= stream_id + 1;
212                     ft->size_mul=1;
213                     ft->size_lsb=1 + pred;
214                     start2++;
215                 }
216                 nut->stream[stream_id].initial_size_predictor[pred]= frame_bytes + pred;
217             }
218         }else{
219             FrameCode *ft= &nut->frame_code[start2];
220             ft->flags= FLAG_KEY_FRAME | FLAG_DATA_SIZE;
221             ft->stream_id_plus1= stream_id + 1;
222             ft->size_mul=1;
223             start2++;
224         }
225 #endif
226         pred_count= 2 + codec->has_b_frames + (codec->codec_id == CODEC_ID_VORBIS);
227         for(pred=0; pred<pred_count; pred++){
228             int start3= start2 + (end2-start2)*pred / pred_count;
229             int end3  = start2 + (end2-start2)*(pred+1) / pred_count;
230
231             for(index=start3; index<end3; index++){
232                 FrameCode *ft= &nut->frame_code[index];
233                 ft->flags= FLAG_KEY_FRAME*key_frame + pred*4;
234                 ft->flags|= FLAG_DATA_SIZE;
235                 ft->stream_id_plus1= stream_id + 1;
236 //FIXME use single byte size and pred from last
237                 ft->size_mul= end3-start3;
238                 ft->size_lsb= index - start3;
239             }
240             nut->stream[stream_id].initial_pts_predictor[pred]= pred+1;
241         }
242     }
243     memmove(&nut->frame_code['N'+1], &nut->frame_code['N'], sizeof(FrameCode)*(255-'N'));
244     nut->frame_code['N'].flags= 1;
245 }
246
247 static uint64_t get_v(ByteIOContext *bc)
248 {
249     uint64_t val = 0;
250
251     for(;;)
252     {
253         int tmp = get_byte(bc);
254
255         if (tmp&0x80)
256             val= (val<<7) + tmp - 0x80;
257         else
258             return (val<<7) + tmp;
259     }
260     return -1;
261 }
262
263 static int get_str(ByteIOContext *bc, char *string, int maxlen){
264     int len= get_v(bc);
265     
266     if(len && maxlen)
267         get_buffer(bc, string, FFMIN(len, maxlen));
268     while(len > maxlen){
269         get_byte(bc);
270         len--;
271     }
272
273     if(maxlen)
274         string[FFMIN(len, maxlen-1)]= 0;
275     
276     if(maxlen == len)
277         return -1;
278     else
279         return 0;
280 }
281
282 static uint64_t get_vb(ByteIOContext *bc){
283     uint64_t val=0;
284     int i= get_v(bc);
285     
286     if(i>8)
287         return UINT64_MAX;
288     
289     while(i--)
290         val = (val<<8) + get_byte(bc);
291     
292     return val;
293 }
294
295 static int get_packetheader(NUTContext *nut, ByteIOContext *bc, int prefix_length, int calculate_checksum)
296 {
297     int64_t start, size, last_size;
298     start= url_ftell(bc) - prefix_length;
299
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);
307
308     size= get_v(bc);
309     last_size= get_v(bc);
310     if(nut->written_packet_size >= 0 && nut->written_packet_size != last_size){
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;
319
320     return size;
321 }
322
323 static int check_checksum(ByteIOContext *bc){
324     unsigned long checksum= get_checksum(bc);
325     return checksum != get_be32(bc);
326 }
327
328 /**
329  * 
330  */
331 static int get_length(uint64_t val){
332     int i;
333
334     for (i=7; val>>i; i+=7);
335
336     return i;
337 }
338
339 static uint64_t find_any_startcode(ByteIOContext *bc, int64_t pos){
340     uint64_t state=0;
341     
342     if(pos >= 0)
343         url_fseek(bc, pos, SEEK_SET); //note, this may fail if the stream isnt seekable, but that shouldnt matter, as in this case we simply start where we are currently
344
345     while(!url_feof(bc)){
346         state= (state<<8) | get_byte(bc);
347         if((state>>56) != 'N')
348             continue;
349         switch(state){
350         case MAIN_STARTCODE:
351         case STREAM_STARTCODE:
352         case KEYFRAME_STARTCODE:
353         case INFO_STARTCODE:
354         case INDEX_STARTCODE:
355             return state;
356         }
357     }
358
359     return 0;
360 }
361
362 /**
363  * find the given startcode.
364  * @param code the startcode
365  * @param pos the start position of the search, or -1 if the current position
366  * @returns the position of the startcode or -1 if not found
367  */
368 static int64_t find_startcode(ByteIOContext *bc, uint64_t code, int64_t pos){
369     for(;;){
370         uint64_t startcode= find_any_startcode(bc, pos);
371         if(startcode == code)
372             return url_ftell(bc) - 8;
373         else if(startcode == 0)
374             return -1;
375         pos=-1;
376     }
377 }
378
379 #ifdef CONFIG_ENCODERS
380 static void put_v(ByteIOContext *bc, uint64_t val)
381 {
382     int i;
383
384     val &= 0x7FFFFFFFFFFFFFFFULL; // FIXME can only encode upto 63 bits currently
385     i= get_length(val);
386
387     for (i-=7; i>0; i-=7){
388         put_byte(bc, 0x80 | (val>>i));
389     }
390
391     put_byte(bc, val&0x7f);
392 }
393
394 /**
395  * stores a string as vb.
396  */
397 static void put_str(ByteIOContext *bc, const char *string){
398     int len= strlen(string);
399     
400     put_v(bc, len);
401     put_buffer(bc, string, len);
402 }
403
404 static void put_vb(ByteIOContext *bc, uint64_t val){
405     int i;
406     
407     for (i=8; val>>i; i+=8);
408
409     put_v(bc, i>>3);
410     for(i-=8; i>=0; i-=8)
411         put_byte(bc, (val>>i)&0xFF);
412 }
413
414 static int put_packetheader(NUTContext *nut, ByteIOContext *bc, int max_size, int calculate_checksum)
415 {
416     put_flush_packet(bc);
417     nut->last_packet_start= nut->packet_start;
418     nut->packet_start+= nut->written_packet_size;
419     nut->packet_size_pos = url_ftell(bc);
420     nut->written_packet_size = max_size;
421     
422     if(calculate_checksum)
423         init_checksum(bc, update_adler32, 0);
424
425     /* packet header */
426     put_v(bc, nut->written_packet_size); /* forward ptr */
427     put_v(bc, nut->packet_start - nut->last_packet_start); /* backward ptr */
428
429     return 0;
430 }
431
432 static int update_packetheader(NUTContext *nut, ByteIOContext *bc, int additional_size, int calculate_checksum){
433     int64_t start= nut->packet_start;
434     int64_t cur= url_ftell(bc);
435     int size= cur - start + additional_size;
436     
437     if(calculate_checksum)
438         size += 4;
439     
440     if(size != nut->written_packet_size){
441         int i;
442
443         assert( size <= nut->written_packet_size );
444     
445         url_fseek(bc, nut->packet_size_pos, SEEK_SET);
446         for(i=get_length(size); i < get_length(nut->written_packet_size); i+=7)
447             put_byte(bc, 0x80);
448         put_v(bc, size);
449
450         url_fseek(bc, cur, SEEK_SET);
451         nut->written_packet_size= size; //FIXME may fail if multiple updates with differing sizes, as get_length may differ
452         
453         if(calculate_checksum)
454             put_be32(bc, get_checksum(bc));
455     }
456     
457     return 0;
458 }
459
460 static int nut_write_header(AVFormatContext *s)
461 {
462     NUTContext *nut = s->priv_data;
463     ByteIOContext *bc = &s->pb;
464     AVCodecContext *codec;
465     int i, j;
466
467     nut->avf= s;
468     
469     nut->stream =       
470         av_mallocz(sizeof(StreamContext)*s->nb_streams);
471     
472     av_set_pts_info(s, 60, 1, AV_TIME_BASE);
473     
474     /* main header */
475     put_be64(bc, MAIN_STARTCODE);
476     put_packetheader(nut, bc, 120+5*256, 1);
477     put_v(bc, 1); /* version */
478     put_v(bc, s->nb_streams);
479     
480     build_frame_code(s);
481     assert(nut->frame_code['N'].flags == 1);
482     for(i=0; i<256;){
483         int tmp_flags = nut->frame_code[i].flags;
484         int tmp_stream= nut->frame_code[i].stream_id_plus1;
485         int tmp_mul   = nut->frame_code[i].size_mul;
486         int tmp_size  = nut->frame_code[i].size_lsb;
487         put_v(bc, tmp_flags);
488         put_v(bc, tmp_stream);
489         put_v(bc, tmp_mul);
490         put_v(bc, tmp_size);
491
492         for(j=0; i<256; j++,i++){
493             if(nut->frame_code[i].flags           != tmp_flags ) break;
494             if(nut->frame_code[i].stream_id_plus1 != tmp_stream) break;
495             if(nut->frame_code[i].size_mul        != tmp_mul   ) break;
496             if(nut->frame_code[i].size_lsb        != tmp_size  ) break;
497             if(++tmp_size >= tmp_mul){
498                 tmp_size=0;
499                 tmp_stream++;
500             }
501         }
502         put_v(bc, j);
503     }
504
505     update_packetheader(nut, bc, 0, 1);
506     
507     /* stream headers */
508     for (i = 0; i < s->nb_streams; i++)
509     {
510         int nom, denom, gcd;
511
512         codec = &s->streams[i]->codec;
513         
514         put_be64(bc, STREAM_STARTCODE);
515         put_packetheader(nut, bc, 120 + codec->extradata_size, 1);
516         put_v(bc, i /*s->streams[i]->index*/);
517         put_v(bc, (codec->codec_type == CODEC_TYPE_AUDIO) ? 32 : 0);
518         if (codec->codec_tag)
519             put_vb(bc, codec->codec_tag);
520         else if (codec->codec_type == CODEC_TYPE_VIDEO)
521         {
522             put_vb(bc, codec_get_bmp_tag(codec->codec_id));
523         }
524         else if (codec->codec_type == CODEC_TYPE_AUDIO)
525         {
526             put_vb(bc, codec_get_wav_tag(codec->codec_id));
527         }
528         else
529             put_vb(bc, 0);
530
531         if (codec->codec_type == CODEC_TYPE_VIDEO)
532         {
533             nom = codec->frame_rate;
534             denom = codec->frame_rate_base;
535         }
536         else
537         {
538             nom = codec->sample_rate;
539             if(codec->frame_size>0)
540                 denom= codec->frame_size;
541             else
542                 denom= 1; //unlucky
543         }
544         gcd= ff_gcd(nom, denom);
545         nom   /= gcd;
546         denom /= gcd;
547         nut->stream[i].rate_num= nom;
548         nut->stream[i].rate_den= denom;
549
550         put_v(bc, codec->bit_rate);
551         put_vb(bc, 0); /* no language code */
552         put_v(bc, nom);
553         put_v(bc, denom);
554         if(nom / denom < 1000)
555             nut->stream[i].msb_timestamp_shift = 7;
556         else
557             nut->stream[i].msb_timestamp_shift = 14;
558         put_v(bc, nut->stream[i].msb_timestamp_shift);
559         for(j=0; j<3; j++)
560                 put_v(bc, nut->stream[i].initial_pts_predictor[j]);
561         for(j=0; j<2; j++)
562                 put_v(bc, nut->stream[i].initial_size_predictor[j]);
563         put_byte(bc, 0); /* flags: 0x1 - fixed_fps, 0x2 - index_present */
564         
565         if(codec->extradata_size){
566             put_v(bc, 1);
567             put_v(bc, codec->extradata_size);
568             put_buffer(bc, codec->extradata, codec->extradata_size);            
569         }
570         put_v(bc, 0); /* end of codec specific headers */
571         
572         switch(codec->codec_type)
573         {
574             case CODEC_TYPE_AUDIO:
575                 put_v(bc, (codec->sample_rate * denom) / nom);
576                 put_v(bc, codec->channels);
577                 break;
578             case CODEC_TYPE_VIDEO:
579                 put_v(bc, codec->width);
580                 put_v(bc, codec->height);
581                 put_v(bc, codec->sample_aspect_ratio.num);
582                 put_v(bc, codec->sample_aspect_ratio.den);
583                 put_v(bc, 0); /* csp type -- unknown */
584                 break;
585             default:
586                 break;
587         }
588         update_packetheader(nut, bc, 0, 1);
589     }
590
591     /* info header */
592     put_be64(bc, INFO_STARTCODE);
593     put_packetheader(nut, bc, 30+strlen(s->author)+strlen(s->title)+
594         strlen(s->comment)+strlen(s->copyright)+strlen(LIBAVFORMAT_IDENT), 1); 
595     if (s->author[0])
596     {
597         put_v(bc, 9); /* type */
598         put_str(bc, s->author);
599     }
600     if (s->title[0])
601     {
602         put_v(bc, 10); /* type */
603         put_str(bc, s->title);
604     }
605     if (s->comment[0])
606     {
607         put_v(bc, 11); /* type */
608         put_str(bc, s->comment);
609     }
610     if (s->copyright[0])
611     {
612         put_v(bc, 12); /* type */
613         put_str(bc, s->copyright);
614     }
615     /* encoder */
616     put_v(bc, 13); /* type */
617     put_str(bc, LIBAVFORMAT_IDENT);
618     
619     put_v(bc, 0); /* eof info */
620     update_packetheader(nut, bc, 0, 1);
621         
622     put_flush_packet(bc);
623     
624     return 0;
625 }
626
627 static int nut_write_packet(AVFormatContext *s, int stream_index, 
628                             const uint8_t *buf, int size, int64_t pts)
629 {
630     NUTContext *nut = s->priv_data;
631     StreamContext *stream= &nut->stream[stream_index];
632     ByteIOContext *bc = &s->pb;
633     int key_frame = 0, full_pts=0;
634     AVCodecContext *enc;
635     int64_t lsb_pts, delta_pts;
636     int frame_type, best_length, frame_code, flags, i, size_mul, size_lsb;
637     const int64_t frame_start= url_ftell(bc);
638
639     if (stream_index > s->nb_streams)
640         return 1;
641         
642     pts= (av_rescale(pts, stream->rate_num, stream->rate_den) + AV_TIME_BASE/2) / AV_TIME_BASE;
643
644     enc = &s->streams[stream_index]->codec;
645     key_frame = enc->coded_frame->key_frame;
646     delta_pts= pts - stream->last_pts;
647     
648     frame_type=0;
649     if(frame_start + size + 20 - FFMAX(nut->last_frame_start[1], nut->last_frame_start[2]) > MAX_TYPE1_DISTANCE)
650         frame_type=1;
651     if(key_frame){
652         if(frame_type==1 && frame_start + size - nut->last_frame_start[2] > MAX_TYPE2_DISTANCE)
653             frame_type=2;
654         if(!stream->last_key_frame)
655             frame_type=2;
656     }
657
658     if(frame_type>0){
659         update_packetheader(nut, bc, 0, 0);
660         reset(s);
661         full_pts=1;
662     }
663     //FIXME ensure that the timestamp can be represented by either delta or lsb or full_pts=1
664
665     lsb_pts = pts & ((1 << stream->msb_timestamp_shift)-1);
666
667     best_length=INT_MAX;
668     frame_code= -1;
669     for(i=0; i<256; i++){
670         int stream_id_plus1= nut->frame_code[i].stream_id_plus1;
671         int fc_key_frame;
672         int length=0;
673         size_mul= nut->frame_code[i].size_mul;
674         size_lsb= nut->frame_code[i].size_lsb;
675         flags= nut->frame_code[i].flags;
676
677         if(stream_id_plus1 == 0) length+= get_length(stream_index);
678         else if(stream_id_plus1 - 1 != stream_index)
679             continue;
680         fc_key_frame= !!(flags & FLAG_KEY_FRAME);
681
682         assert(key_frame==0 || key_frame==1);
683         if(fc_key_frame != key_frame)
684             continue;
685
686         if((!!(flags & FLAG_FRAME_TYPE)) != (frame_type > 0))
687             continue;
688         
689         if(size_mul <= size_lsb){
690             int p= stream->lru_size[size_lsb - size_mul];
691             if(p != size)
692                 continue;
693         }else{
694             if(size % size_mul != size_lsb)
695                 continue;
696             if(flags & FLAG_DATA_SIZE)
697                 length += get_length(size / size_mul);
698             else if(size/size_mul)
699                 continue;
700         }
701
702         if(full_pts != ((flags & FLAG_PTS) && (flags & FLAG_FULL_PTS)))
703             continue;
704             
705         if(flags&FLAG_PTS){
706             if(flags&FLAG_FULL_PTS){
707                 length += get_length(pts);
708             }else{
709                 length += get_length(lsb_pts);
710             }
711         }else{
712             int delta= stream->lru_pts_delta[(flags & 12)>>2];
713             if(delta != pts - stream->last_pts)
714                 continue;
715             assert(frame_type == 0);
716         }
717
718         if(length < best_length){
719             best_length= length;
720             frame_code=i;
721         }
722 //    av_log(s, AV_LOG_DEBUG, "%d %d %d %d %d %d %d %d %d %d\n", key_frame, frame_type, full_pts, size, stream_index, flags, size_mul, size_lsb, stream_id_plus1, length);
723     }
724
725     assert(frame_code != -1);
726     flags= nut->frame_code[frame_code].flags;
727     size_mul= nut->frame_code[frame_code].size_mul;
728     size_lsb= nut->frame_code[frame_code].size_lsb;
729 #if 0
730     best_length /= 7;
731     best_length ++; //frame_code
732     if(frame_type>0){
733         best_length += 4; //packet header
734         if(frame_type>1)
735             best_length += 8; // startcode
736     }
737     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));
738 #endif
739
740     if (frame_type==2)
741         put_be64(bc, KEYFRAME_STARTCODE);
742     put_byte(bc, frame_code);
743
744     if(frame_type>0)
745         put_packetheader(nut, bc, FFMAX(size+20, MAX_TYPE1_DISTANCE), 0);
746     if(nut->frame_code[frame_code].stream_id_plus1 == 0)
747         put_v(bc, stream_index);
748     if (flags & FLAG_PTS){
749         if (flags & FLAG_FULL_PTS)
750             put_v(bc, pts);
751         else
752             put_v(bc, lsb_pts);
753     }
754     if(flags & FLAG_DATA_SIZE)
755         put_v(bc, size / size_mul);
756     if(size > MAX_TYPE1_DISTANCE){
757         assert(frame_type > 0);
758         update_packetheader(nut, bc, size, 0);
759     }
760     
761     put_buffer(bc, buf, size);
762
763     update(nut, stream_index, frame_start, frame_type, frame_code, key_frame, size, pts);
764     
765     return 0;
766 }
767
768 static int nut_write_trailer(AVFormatContext *s)
769 {
770     NUTContext *nut = s->priv_data;
771     ByteIOContext *bc = &s->pb;
772
773     update_packetheader(nut, bc, 0, 0);
774
775 #if 0
776     int i;
777
778     /* WRITE INDEX */
779
780     for (i = 0; s->nb_streams; i++)
781     {
782         put_be64(bc, INDEX_STARTCODE);
783         put_packetheader(nut, bc, 64, 1);
784         put_v(bc, s->streams[i]->id);
785         put_v(bc, ...);
786         update_packetheader(nut, bc, 0, 1);
787     }
788 #endif
789
790     put_flush_packet(bc);
791     
792     av_freep(&nut->stream);
793
794     return 0;
795 }
796 #endif //CONFIG_ENCODERS
797
798 static int nut_probe(AVProbeData *p)
799 {
800     int i;
801     uint64_t code;
802
803     code = 0xff;
804     for (i = 0; i < p->buf_size; i++) {
805         int c = p->buf[i];
806         code = (code << 8) | c;
807         if (code == MAIN_STARTCODE)
808             return AVPROBE_SCORE_MAX;
809     }
810     return 0;
811 }
812
813 static int decode_main_header(NUTContext *nut){
814     AVFormatContext *s= nut->avf;
815     ByteIOContext *bc = &s->pb;
816     uint64_t tmp;
817     int i, j;
818     
819     get_packetheader(nut, bc, 8, 1);
820
821     tmp = get_v(bc);
822     if (tmp != 1){
823         av_log(s, AV_LOG_ERROR, "bad version (%Ld)\n", tmp);
824         return -1;
825     }
826     
827     nut->stream_count = get_v(bc);
828
829     for(i=0; i<256;){
830         int tmp_flags = get_v(bc);
831         int tmp_stream= get_v(bc);
832         int tmp_mul   = get_v(bc);
833         int tmp_size  = get_v(bc);
834         int count     = get_v(bc);
835
836         if(count == 0 || i+count > 256){
837             av_log(s, AV_LOG_ERROR, "illegal count %d at %d\n", count, i);
838             return -1;
839         }
840
841         if((tmp_flags & FLAG_FRAME_TYPE) && tmp_flags != 1){
842             if(!(tmp_flags & FLAG_PTS) || !(tmp_flags & FLAG_FULL_PTS) ){
843                 av_log(s, AV_LOG_ERROR, "no full pts in non 0 frame type\n");
844                 return -1;
845             }
846         }
847
848         for(j=0; j<count; j++,i++){
849             if(tmp_stream > nut->stream_count + 1){
850                 av_log(s, AV_LOG_ERROR, "illegal stream number\n");
851                 return -1;
852             }
853
854             nut->frame_code[i].flags           = tmp_flags ;
855             nut->frame_code[i].stream_id_plus1 = tmp_stream;
856             nut->frame_code[i].size_mul        = tmp_mul   ;
857             nut->frame_code[i].size_lsb        = tmp_size  ;
858             if(++tmp_size >= tmp_mul){
859                 tmp_size=0;
860                 tmp_stream++;
861             }
862         }
863     }
864     if(nut->frame_code['N'].flags != 1){
865         av_log(s, AV_LOG_ERROR, "illegal frame_code table\n");
866         return -1;
867     }
868
869     if(check_checksum(bc)){
870         av_log(s, AV_LOG_ERROR, "Main header checksum missmatch\n");
871         return -1;
872     }
873
874     return 0;
875 }
876
877 static int decode_stream_header(NUTContext *nut){
878     AVFormatContext *s= nut->avf;
879     ByteIOContext *bc = &s->pb;
880     int class, nom, denom, stream_id, i;
881     uint64_t tmp;
882     AVStream *st;
883     
884     get_packetheader(nut, bc, 8, 1);
885     stream_id= get_v(bc);
886     if(stream_id >= nut->stream_count || s->streams[stream_id])
887         return -1;
888     
889     st = av_new_stream(s, stream_id);
890     if (!st)
891         return AVERROR_NOMEM;
892     class = get_v(bc);
893     tmp = get_vb(bc);
894     switch(class)
895     {
896         case 0:
897             st->codec.codec_type = CODEC_TYPE_VIDEO;
898             st->codec.codec_id = codec_get_bmp_id(tmp);
899             if (st->codec.codec_id == CODEC_ID_NONE)
900                 av_log(s, AV_LOG_ERROR, "Unknown codec?!\n");
901             break;
902         case 32:
903             st->codec.codec_type = CODEC_TYPE_AUDIO;
904             st->codec.codec_id = codec_get_wav_id(tmp);
905             if (st->codec.codec_id == CODEC_ID_NONE)
906                 av_log(s, AV_LOG_ERROR, "Unknown codec?!\n");
907             break;
908         default:
909             av_log(s, AV_LOG_ERROR, "Unknown stream class (%d)\n", class);
910             return -1;
911     }
912     s->bit_rate += get_v(bc);
913     get_vb(bc); /* language code */
914     nom = get_v(bc);
915     denom = get_v(bc);
916     nut->stream[stream_id].msb_timestamp_shift = get_v(bc);
917     for(i=0; i<3; i++)
918             nut->stream[stream_id].initial_pts_predictor[i]= get_v(bc);
919     for(i=0; i<2; i++)
920             nut->stream[stream_id].initial_size_predictor[i]= get_v(bc);
921     get_byte(bc); /* flags */
922
923     /* codec specific data headers */
924     while(get_v(bc) != 0){
925         st->codec.extradata_size= get_v(bc);
926         st->codec.extradata= av_mallocz(st->codec.extradata_size);
927         get_buffer(bc, st->codec.extradata, st->codec.extradata_size);            
928 //          url_fskip(bc, get_v(bc));
929     }
930     
931     if (class == 0) /* VIDEO */
932     {
933         st->codec.width = get_v(bc);
934         st->codec.height = get_v(bc);
935         st->codec.sample_aspect_ratio.num= get_v(bc);
936         st->codec.sample_aspect_ratio.den= get_v(bc);
937         get_v(bc); /* csp type */
938
939         st->codec.frame_rate = nom;
940         st->codec.frame_rate_base = denom;
941     }
942     if (class == 32) /* AUDIO */
943     {
944         st->codec.sample_rate = (get_v(bc) * nom) / denom;
945         st->codec.channels = get_v(bc);
946     }
947     if(check_checksum(bc)){
948         av_log(s, AV_LOG_ERROR, "Stream header %d checksum missmatch\n", stream_id);
949         return -1;
950     }
951     nut->stream[stream_id].rate_num= nom;
952     nut->stream[stream_id].rate_den= denom;
953     return 0;
954 }
955
956 static int decode_info_header(NUTContext *nut){
957     AVFormatContext *s= nut->avf;
958     ByteIOContext *bc = &s->pb;
959     
960     get_packetheader(nut, bc, 8, 1);
961
962     for(;;){
963         int id= get_v(bc);
964         char *name, *type, custom_name[256], custom_type[256];
965
966         if(!id)
967             break;
968         else if(id >= sizeof(info_table)/sizeof(info_table[0])){
969             av_log(s, AV_LOG_ERROR, "info id is too large %d %d\n", id, sizeof(info_table)/sizeof(info_table[0]));
970             return -1;
971         }
972
973         type= info_table[id][1];
974         name= info_table[id][0];
975 //av_log(s, AV_LOG_DEBUG, "%d %s %s\n", id, type, name);
976
977         if(!type){
978             get_str(bc, custom_type, sizeof(custom_type));
979             type= custom_type;
980         }
981         if(!name){
982             get_str(bc, custom_name, sizeof(custom_name));
983             name= custom_name;
984         }
985         
986         if(!strcmp(type, "v")){
987             int value= get_v(bc);
988         }else{
989             if(!strcmp(name, "Author"))
990                 get_str(bc, s->author, sizeof(s->author));
991             else if(!strcmp(name, "Title"))
992                 get_str(bc, s->title, sizeof(s->title));
993             else if(!strcmp(name, "Copyright"))
994                 get_str(bc, s->copyright, sizeof(s->copyright));
995             else if(!strcmp(name, "Description"))
996                 get_str(bc, s->comment, sizeof(s->comment));
997             else
998                 get_str(bc, NULL, 0);
999         }
1000     }
1001     if(check_checksum(bc)){
1002         av_log(s, AV_LOG_ERROR, "Info header checksum missmatch\n");
1003         return -1;
1004     }
1005     return 0;
1006 }
1007
1008 static int nut_read_header(AVFormatContext *s, AVFormatParameters *ap)
1009 {
1010     NUTContext *nut = s->priv_data;
1011     ByteIOContext *bc = &s->pb;
1012     int64_t pos;
1013     int inited_stream_count;
1014
1015     nut->avf= s;
1016     
1017     av_set_pts_info(s, 60, 1, AV_TIME_BASE);
1018
1019     /* main header */
1020     pos=0;
1021     for(;;){
1022         pos= find_startcode(bc, MAIN_STARTCODE, pos)+1;
1023         if (pos<0){
1024             av_log(s, AV_LOG_ERROR, "no main startcode found\n");
1025             return -1;
1026         }
1027         if(decode_main_header(nut) >= 0)
1028             break;
1029     }
1030     
1031     
1032     s->bit_rate = 0;
1033
1034     nut->stream = av_malloc(sizeof(StreamContext)*nut->stream_count);
1035
1036     /* stream headers */
1037     pos=0;
1038     for(inited_stream_count=0; inited_stream_count < nut->stream_count;){
1039         pos= find_startcode(bc, STREAM_STARTCODE, pos)+1;
1040         if (pos<0){
1041             av_log(s, AV_LOG_ERROR, "not all stream headers found\n");
1042             return -1;
1043         }
1044         if(decode_stream_header(nut) >= 0)
1045             inited_stream_count++;
1046     }
1047
1048     /* info headers */
1049     pos=0;
1050     for(;;){
1051         uint64_t startcode= find_any_startcode(bc, pos);
1052         pos= url_ftell(bc);
1053
1054         if(startcode==0){
1055             av_log(s, AV_LOG_ERROR, "EOF before video frames\n");
1056             return -1;
1057         }else if(startcode == KEYFRAME_STARTCODE){
1058             nut->next_startcode= startcode;
1059             break;
1060         }else if(startcode != INFO_STARTCODE){
1061             continue;
1062         }
1063
1064         decode_info_header(nut);
1065     }
1066
1067     return 0;
1068 }
1069
1070 static int decode_frame(NUTContext *nut, AVPacket *pkt, int frame_code, int frame_type){
1071     AVFormatContext *s= nut->avf;
1072     StreamContext *stream;
1073     ByteIOContext *bc = &s->pb;
1074     int size, flags, size_mul, size_lsb, stream_id;
1075     int key_frame = 0;
1076     int64_t pts = 0;
1077     const int prefix_len= frame_type == 2 ? 8+1 : 1;
1078     const int64_t frame_start= url_ftell(bc) - prefix_len;
1079
1080     flags= nut->frame_code[frame_code].flags;
1081     size_mul= nut->frame_code[frame_code].size_mul;
1082     size_lsb= nut->frame_code[frame_code].size_lsb;
1083     stream_id= nut->frame_code[frame_code].stream_id_plus1 - 1;
1084
1085     if(flags & FLAG_FRAME_TYPE){
1086         reset(s);
1087         if(get_packetheader(nut, bc, prefix_len, 0) < 0)
1088             return -1;
1089         if(frame_type!=2)
1090             frame_type= 1;
1091     }
1092
1093     if(stream_id==-1)
1094         stream_id= get_v(bc);
1095     if(stream_id >= s->nb_streams){
1096         av_log(s, AV_LOG_ERROR, "illegal stream_id\n");
1097         return -1;
1098     }
1099     stream= &nut->stream[stream_id];
1100
1101 //    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]);
1102     
1103     key_frame= !!(flags & FLAG_KEY_FRAME);
1104
1105     if(flags & FLAG_PTS){
1106         if(flags & FLAG_FULL_PTS){
1107             pts= get_v(bc);
1108             if(frame_type && key_frame){
1109                 av_add_index_entry(
1110                     s->streams[stream_id], 
1111                     frame_start, 
1112                     pts, 
1113                     frame_start - nut->stream[stream_id].last_sync_pos,
1114                     AVINDEX_KEYFRAME);
1115                 nut->stream[stream_id].last_sync_pos= frame_start;
1116                 assert(nut->packet_start == frame_start);
1117             }
1118         }else{
1119             int64_t mask = (1<<stream->msb_timestamp_shift)-1;
1120             int64_t delta= stream->last_pts - mask/2;
1121             pts= ((get_v(bc) - delta)&mask) + delta;
1122         }
1123     }else{
1124         pts= stream->last_pts + stream->lru_pts_delta[(flags&12)>>2];
1125     }
1126   
1127     if(size_mul <= size_lsb){
1128         size= stream->lru_size[size_lsb - size_mul];
1129     }else{
1130         if(flags & FLAG_DATA_SIZE)
1131             size= size_mul*get_v(bc) + size_lsb;
1132         else
1133             size= size_lsb;
1134     }
1135       
1136 //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);
1137
1138     if(url_ftell(bc) - nut->packet_start + size > nut->written_packet_size){
1139         av_log(s, AV_LOG_ERROR, "frame size too large\n");
1140         return -1;
1141     }
1142     
1143     av_new_packet(pkt, size);
1144     get_buffer(bc, pkt->data, size);
1145     pkt->stream_index = stream_id;
1146     if (key_frame)
1147         pkt->flags |= PKT_FLAG_KEY;
1148     pkt->pts = pts * AV_TIME_BASE * stream->rate_den / stream->rate_num;
1149
1150     update(nut, stream_id, frame_start, frame_type, frame_code, key_frame, size, pts);
1151     
1152     return 0;
1153 }
1154
1155 static int nut_read_packet(AVFormatContext *s, AVPacket *pkt)
1156 {
1157     NUTContext *nut = s->priv_data;
1158     ByteIOContext *bc = &s->pb;
1159     int size, i, frame_code=0;
1160     int64_t pos;
1161
1162     for(;;){
1163         int frame_type= 0;
1164         uint64_t tmp= nut->next_startcode;
1165         nut->next_startcode=0;
1166
1167         if (url_feof(bc))
1168             return -1;
1169
1170         if(!tmp){
1171             frame_code = get_byte(bc);
1172             if(frame_code == 'N'){
1173                 tmp= frame_code;
1174                 for(i=1; i<8; i++)
1175                     tmp = (tmp<<8) + get_byte(bc);
1176             }
1177         }
1178         switch(tmp){
1179         case MAIN_STARTCODE:
1180         case STREAM_STARTCODE:
1181         case INDEX_STARTCODE:
1182             get_packetheader(nut, bc, 8, 0);
1183             url_fseek(bc, nut->written_packet_size + nut->packet_start, SEEK_SET);
1184             break;
1185         case INFO_STARTCODE:
1186             if(decode_info_header(nut)<0)
1187                 goto resync;
1188             break;
1189         case KEYFRAME_STARTCODE:
1190             frame_type = 2;
1191             frame_code = get_byte(bc);
1192         case 0:
1193             if(decode_frame(nut, pkt, frame_code, frame_type)>=0)
1194                 return 0;
1195         default:
1196 resync:
1197 av_log(s, AV_LOG_DEBUG, "syncing from %lld\n", nut->packet_start+1);
1198             tmp= find_any_startcode(bc, nut->packet_start+1);
1199             if(tmp==0)
1200                 return -1;
1201 av_log(s, AV_LOG_DEBUG, "sync\n");
1202             if(url_is_streamed(bc)){
1203                 nut->next_startcode= tmp;
1204                 break;
1205             }
1206
1207             pos= url_ftell(bc) - 8;
1208 av_log(s, AV_LOG_DEBUG, "at %lld code=%llX\n", pos, tmp);
1209             if(tmp==KEYFRAME_STARTCODE){
1210                 get_byte(bc);
1211             }
1212             get_v(bc);
1213             size= get_v(bc);
1214             
1215             while(size > 2 && size < 100000 && nut->packet_start < pos - size){
1216                 url_fseek(bc, pos - size, SEEK_SET);
1217                 frame_code= get_byte(bc);
1218                 if(!(nut->frame_code[ frame_code ].flags & FLAG_FRAME_TYPE))
1219                     break;
1220                 if(get_v(bc) != size)
1221                     break;
1222                 pos -= size;
1223                 size= get_v(bc);
1224 av_log(s, AV_LOG_DEBUG, "steping back to %lld next %d\n", pos, size);
1225             }
1226             url_fseek(bc, pos, SEEK_SET);
1227             
1228             nut->written_packet_size= -1;
1229         }
1230     }
1231 }
1232
1233 static int64_t read_timestamp(AVFormatContext *s, int stream_index, int64_t *pos_arg, int64_t pos_limit){
1234     NUTContext *nut = s->priv_data;
1235     ByteIOContext *bc = &s->pb;
1236     int64_t pos, pts;
1237     uint64_t code;
1238     int frame_code,step, flags, stream_id, i;
1239 av_log(s, AV_LOG_DEBUG, "read_timestamp(X,%d,%lld,%lld)\n", stream_index, *pos_arg, pos_limit);
1240
1241     if(*pos_arg < 0)
1242         return AV_NOPTS_VALUE;
1243
1244     // find a previous startcode, FIXME use forward search and follow backward pointers if undamaged stream
1245     pos= *pos_arg;
1246     step= FFMIN(16*1024, pos);
1247     do{
1248         pos-= step;
1249         code= find_any_startcode(bc, pos);
1250
1251         if(code && url_ftell(bc) - 8 < *pos_arg)
1252             break;
1253         step= FFMIN(2*step, pos);
1254     }while(step);
1255
1256     if(!code) //nothing found, not even after pos_arg
1257         return AV_NOPTS_VALUE;
1258
1259     url_fseek(bc, -8, SEEK_CUR);
1260     for(i=0; i<s->nb_streams; i++)
1261         nut->stream[i].last_sync_pos= url_ftell(bc);
1262         
1263     for(;;){
1264         int64_t pos= url_ftell(bc);
1265         uint64_t tmp=0;
1266         int prefix_len=1;
1267         
1268         if(pos > pos_limit)
1269             return AV_NOPTS_VALUE;
1270
1271         frame_code = get_byte(bc);
1272         if(frame_code == 'N'){
1273             tmp= frame_code;
1274             for(i=1; i<8; i++)
1275                 tmp = (tmp<<8) + get_byte(bc);
1276         }
1277 //av_log(s, AV_LOG_DEBUG, "before switch %llX at=%lld\n", tmp, pos);
1278
1279         switch(tmp){
1280         case MAIN_STARTCODE:
1281         case STREAM_STARTCODE:
1282         case INDEX_STARTCODE:
1283         case INFO_STARTCODE:
1284             nut->written_packet_size= -1;
1285             get_packetheader(nut, bc, 8, 0);
1286             url_fseek(bc, nut->written_packet_size + nut->packet_start, SEEK_SET);
1287             break;
1288         case KEYFRAME_STARTCODE:
1289             nut->written_packet_size= -1;
1290             prefix_len+=8;
1291             frame_code = get_byte(bc);
1292         case 0:
1293             flags= nut->frame_code[frame_code].flags;
1294             stream_id= nut->frame_code[frame_code].stream_id_plus1 - 1;
1295
1296             if(get_packetheader(nut, bc, prefix_len, 0) < 0)
1297                 goto resync;
1298
1299             if(!(flags & FLAG_FRAME_TYPE) || !(flags & FLAG_PTS) || !(flags & FLAG_FULL_PTS))
1300                 goto resync;
1301
1302             if(stream_id==-1)
1303                 stream_id= get_v(bc);
1304             if(stream_id >= s->nb_streams)
1305                 goto resync;
1306                 
1307             pts= get_v(bc);
1308     
1309             if(flags & FLAG_KEY_FRAME){
1310                 av_add_index_entry(
1311                     s->streams[stream_id], 
1312                     pos, 
1313                     pts, 
1314                     pos - nut->stream[stream_id].last_sync_pos,
1315                     AVINDEX_KEYFRAME);
1316                 nut->stream[stream_id].last_sync_pos= pos;
1317             }
1318             if(stream_id != stream_index || !(flags & FLAG_KEY_FRAME) || nut->packet_start < *pos_arg){
1319                 url_fseek(bc, nut->written_packet_size + nut->packet_start, SEEK_SET);
1320                 break;
1321             }
1322  
1323             *pos_arg= nut->packet_start;
1324             assert(nut->packet_start == pos);
1325             return pts;
1326         default:
1327 resync:
1328 av_log(s, AV_LOG_DEBUG, "syncing from %lld\n", nut->packet_start+1);
1329             if(!find_any_startcode(bc, nut->packet_start+1))
1330                 return AV_NOPTS_VALUE;
1331
1332             url_fseek(bc, -8, SEEK_CUR);
1333         }
1334     }
1335     return AV_NOPTS_VALUE;
1336 }
1337
1338 #define DEBUG_SEEK
1339 static int nut_read_seek(AVFormatContext *s, int stream_index, int64_t target_ts){
1340     NUTContext *nut = s->priv_data;
1341     StreamContext *stream;
1342     int64_t pos_min, pos_max, pos, pos_limit;
1343     int64_t ts_min, ts_max, ts;
1344     int64_t start_pos;
1345     int index, no_change,i;
1346     AVStream *st;
1347
1348     if (stream_index < 0) {
1349         stream_index = av_find_default_stream_index(s);
1350         if (stream_index < 0)
1351             return -1;
1352     }
1353     stream= &nut->stream[stream_index];
1354     target_ts= (av_rescale(target_ts, stream->rate_num, stream->rate_den) + AV_TIME_BASE/2) / AV_TIME_BASE;
1355
1356 #ifdef DEBUG_SEEK
1357     av_log(s, AV_LOG_DEBUG, "read_seek: %d %lld\n", stream_index, target_ts);
1358 #endif
1359
1360     ts_max=
1361     ts_min= AV_NOPTS_VALUE;
1362     pos_limit= -1; //gcc falsely says it may be uninitalized
1363
1364     st= s->streams[stream_index];
1365     if(st->index_entries){
1366         AVIndexEntry *e;
1367
1368         index= av_index_search_timestamp(st, target_ts);
1369         e= &st->index_entries[index];
1370
1371         if(e->timestamp <= target_ts || e->pos == e->min_distance){
1372             pos_min= e->pos;
1373             ts_min= e->timestamp;
1374 #ifdef DEBUG_SEEK
1375         av_log(s, AV_LOG_DEBUG, "unsing cached pos_min=0x%llx dts_min=%lld\n", 
1376                pos_min,ts_min);
1377 #endif
1378         }else{
1379             assert(index==0);
1380         }
1381         index++;
1382         if(index < st->nb_index_entries){
1383             e= &st->index_entries[index];
1384             assert(e->timestamp >= target_ts);
1385             pos_max= e->pos;
1386             ts_max= e->timestamp;
1387             pos_limit= pos_max - e->min_distance;
1388 #ifdef DEBUG_SEEK
1389         av_log(s, AV_LOG_DEBUG, "unsing cached pos_max=0x%llx pos_limit=0x%llx dts_max=%lld\n", 
1390                pos_max,pos_limit, ts_max);
1391 #endif
1392         }
1393     }
1394
1395     if(ts_min == AV_NOPTS_VALUE){
1396         pos_min = 0;
1397         ts_min = read_timestamp(s, stream_index, &pos_min, INT64_MAX);
1398         if (ts_min == AV_NOPTS_VALUE)
1399             return -1;
1400     }
1401
1402     if(ts_max == AV_NOPTS_VALUE){
1403         int step= 1024;
1404         pos_max = url_filesize(url_fileno(&s->pb)) - 1;
1405         do{
1406             pos_max -= step;
1407             ts_max = read_timestamp(s, stream_index, &pos_max, pos_max + step);
1408             step += step;
1409         }while(ts_max == AV_NOPTS_VALUE && pos_max >= step);
1410         if (ts_max == AV_NOPTS_VALUE)
1411             return -1;
1412         
1413         for(;;){
1414             int64_t tmp_pos= pos_max + 1;
1415             int64_t tmp_ts= read_timestamp(s, stream_index, &tmp_pos, INT64_MAX);
1416             if(tmp_ts == AV_NOPTS_VALUE)
1417                 break;
1418             ts_max= tmp_ts;
1419             pos_max= tmp_pos;
1420         }
1421         pos_limit= pos_max;
1422     }
1423
1424     no_change=0;
1425     while (pos_min < pos_limit) {
1426 #ifdef DEBUG_SEEK
1427         av_log(s, AV_LOG_DEBUG, "pos_min=0x%llx pos_max=0x%llx dts_min=%lld dts_max=%lld\n", 
1428                pos_min, pos_max,
1429                ts_min, ts_max);
1430 #endif
1431         assert(pos_limit <= pos_max);
1432
1433         if(no_change==0){
1434             int64_t approximate_keyframe_distance= pos_max - pos_limit;
1435             // interpolate position (better than dichotomy)
1436             pos = (int64_t)((double)(pos_max - pos_min) *
1437                             (double)(target_ts - ts_min) /
1438                             (double)(ts_max - ts_min)) + pos_min - approximate_keyframe_distance;
1439         }else if(no_change==1){
1440             // bisection, if interpolation failed to change min or max pos last time
1441             pos = (pos_min + pos_limit)>>1;
1442         }else{
1443             // linear search if bisection failed, can only happen if there are very few or no keframes between min/max
1444             pos=pos_min;
1445         }
1446         if(pos <= pos_min)
1447             pos= pos_min + 1;
1448         else if(pos > pos_limit)
1449             pos= pos_limit;
1450         start_pos= pos;
1451
1452         ts = read_timestamp(s, stream_index, &pos, INT64_MAX); //may pass pos_limit instead of -1
1453         if(pos == pos_max)
1454             no_change++;
1455         else
1456             no_change=0;
1457 #ifdef DEBUG_SEEK
1458 av_log(s, AV_LOG_DEBUG, "%Ld %Ld %Ld / %Ld %Ld %Ld target:%Ld limit:%Ld start:%Ld noc:%d\n", pos_min, pos, pos_max, ts_min, ts, ts_max, target_ts, pos_limit, start_pos, no_change);
1459 #endif
1460         assert(ts != AV_NOPTS_VALUE);
1461         if (target_ts < ts) {
1462             pos_limit = start_pos - 1;
1463             pos_max = pos;
1464             ts_max = ts;
1465         } else {
1466             pos_min = pos;
1467             ts_min = ts;
1468             /* check if we are lucky */
1469             if (target_ts == ts)
1470                 break;
1471         }
1472     }
1473     
1474     pos = pos_min;
1475 #ifdef DEBUG_SEEK
1476     pos_min = pos;
1477     ts_min = read_timestamp(s, stream_index, &pos_min, INT64_MAX);
1478     pos_min++;
1479     ts_max = read_timestamp(s, stream_index, &pos_min, INT64_MAX);
1480     av_log(s, AV_LOG_DEBUG, "pos=0x%llx %lld<=%lld<=%lld\n", 
1481            pos, ts_min, target_ts, ts_max);
1482 #endif
1483     /* do the seek */
1484     url_fseek(&s->pb, pos, SEEK_SET);
1485
1486     nut->written_packet_size= -1;
1487     for(i=0; i<s->nb_streams; i++)
1488         nut->stream[i].last_sync_pos= pos;
1489
1490     return 0;
1491 }
1492
1493 static int nut_read_close(AVFormatContext *s)
1494 {
1495     NUTContext *nut = s->priv_data;
1496     int i;
1497
1498     for(i=0;i<s->nb_streams;i++) {
1499         av_freep(&s->streams[i]->codec.extradata);
1500     }
1501     av_freep(&nut->stream);
1502
1503     return 0;
1504 }
1505
1506 static AVInputFormat nut_iformat = {
1507     "nut",
1508     "nut format",
1509     sizeof(NUTContext),
1510     nut_probe,
1511     nut_read_header,
1512     nut_read_packet,
1513     nut_read_close,
1514     nut_read_seek,
1515     .extensions = "nut",
1516 };
1517
1518 #ifdef CONFIG_ENCODERS
1519 static AVOutputFormat nut_oformat = {
1520     "nut",
1521     "nut format",
1522     "video/x-nut",
1523     "nut",
1524     sizeof(NUTContext),
1525 #ifdef CONFIG_VORBIS
1526     CODEC_ID_VORBIS,
1527 #elif defined(CONFIG_MP3LAME)
1528     CODEC_ID_MP3,
1529 #else
1530     CODEC_ID_MP2, /* AC3 needs liba52 decoder */
1531 #endif
1532     CODEC_ID_MPEG4,
1533     nut_write_header,
1534     nut_write_packet,
1535     nut_write_trailer,
1536 };
1537 #endif //CONFIG_ENCODERS
1538
1539 int nut_init(void)
1540 {
1541     av_register_input_format(&nut_iformat);
1542 #ifdef CONFIG_ENCODERS
1543     av_register_output_format(&nut_oformat);
1544 #endif //CONFIG_ENCODERS
1545     return 0;
1546 }