Statistics
| Branch: | Revision:

ffmpeg / libavformat / nut.c @ ca6ecf50

History | View | Annotate | Download (41.2 KB)

1 3aa180b8 Alex Beregszaszi
/*
2 a5247e3d Michael Niedermayer
 * "NUT" Container Format muxer and demuxer (DRAFT-200403??)
3 3aa180b8 Alex Beregszaszi
 * Copyright (c) 2003 Alex Beregszaszi
4 a5247e3d Michael Niedermayer
 * Copyright (c) 2004 Michael Niedermayer
5 3aa180b8 Alex Beregszaszi
 *
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 e692fd60 Alex Beregszaszi
 * AND http://people.fsn.hu/~alex/nut/ (TeX, pdf, ps, dvi, ..)
23 3aa180b8 Alex Beregszaszi
 *
24
 */
25
26
/*
27
 * TODO:
28
 * - index writing
29 e52788d0 Michael Niedermayer
 * - index packet reading support
30 3aa180b8 Alex Beregszaszi
*/
31
32
//#define DEBUG 1
33
34 a5247e3d Michael Niedermayer
#include <limits.h>
35 3aa180b8 Alex Beregszaszi
#include "avformat.h"
36
#include "mpegaudio.h"
37 a182f5da Fabrice Bellard
#include "avi.h"
38 3aa180b8 Alex Beregszaszi
39 a5247e3d Michael Niedermayer
#undef NDEBUG
40
#include <assert.h>
41
42 84ad6130 Michael Niedermayer
//#define TRACE
43
44 3ac8749b Michael Niedermayer
//from /dev/random
45
46 a5247e3d Michael Niedermayer
#define     MAIN_STARTCODE (0x7A561F5F04ADULL + (((uint64_t)('N'<<8) + 'M')<<48)) 
47
#define   STREAM_STARTCODE (0x11405BF2F9DBULL + (((uint64_t)('N'<<8) + 'S')<<48)) 
48
#define KEYFRAME_STARTCODE (0xE4ADEECA4569ULL + (((uint64_t)('N'<<8) + 'K')<<48)) 
49
#define    INDEX_STARTCODE (0xDD672F23E64EULL + (((uint64_t)('N'<<8) + 'X')<<48)) 
50
#define     INFO_STARTCODE (0xAB68B596BA78ULL + (((uint64_t)('N'<<8) + 'I')<<48)) 
51
52 ca6ecf50 Michael Niedermayer
#define ID_STRING "nut/multimedia container\0"
53
54 0b43db14 Michael Niedermayer
#define MAX_DISTANCE (1024*16-1)
55
#define MAX_SHORT_DISTANCE (1024*4-1)
56 a5247e3d Michael Niedermayer
57 84ad6130 Michael Niedermayer
#define FLAG_DATA_SIZE       1
58
#define FLAG_KEY_FRAME       2
59
#define FLAG_INVALID         4
60 a5247e3d Michael Niedermayer
61
typedef struct {
62
    uint8_t flags;
63
    uint8_t stream_id_plus1;
64 0b43db14 Michael Niedermayer
    uint16_t size_mul;
65
    uint16_t size_lsb;
66 84ad6130 Michael Niedermayer
    int16_t timestamp_delta;
67
    uint8_t reserved_count;
68 a5247e3d Michael Niedermayer
} FrameCode;
69
70
typedef struct {
71
    int last_key_frame;
72
    int msb_timestamp_shift;
73
    int rate_num;
74
    int rate_den;
75
    int64_t last_pts;
76 7dddf251 Michael Niedermayer
    int64_t last_sync_pos;                    ///<pos of last 1/2 type frame
77 0b43db14 Michael Niedermayer
    int decode_delay;
78 a5247e3d Michael Niedermayer
} StreamContext;
79 3ac8749b Michael Niedermayer
80 3aa180b8 Alex Beregszaszi
typedef struct {
81 a5247e3d Michael Niedermayer
    AVFormatContext *avf;
82
    int written_packet_size;
83 0b43db14 Michael Niedermayer
    int64_t packet_start[3]; //0-> startcode less, 1-> short startcode 2-> long startcodes
84 a5247e3d Michael Niedermayer
    FrameCode frame_code[256];
85 7c5934ed Michael Niedermayer
    int stream_count;
86 465e1dad Michael Niedermayer
    uint64_t next_startcode;     ///< stores the next startcode if it has alraedy been parsed but the stream isnt seekable
87 a5247e3d Michael Niedermayer
    StreamContext *stream;
88 0b43db14 Michael Niedermayer
    int max_distance;
89 ca6ecf50 Michael Niedermayer
    int max_short_distance;
90 84ad6130 Michael Niedermayer
    int rate_num;
91
    int rate_den;
92
    int short_startcode;
93 3aa180b8 Alex Beregszaszi
} NUTContext;
94
95 e52788d0 Michael Niedermayer
static char *info_table[][2]={
96
        {NULL                        ,  NULL }, // end
97
        {NULL                        ,  NULL },
98
        {NULL                        , "UTF8"},
99
        {NULL                        , "v"},
100
        {NULL                        , "s"},
101
        {"StreamId"                , "v"},
102
        {"SegmentId"                , "v"},
103
        {"StartTimestamp"        , "v"},
104
        {"EndTimestamp"                , "v"},
105
        {"Author"                , "UTF8"},
106 5a757507 Michael Niedermayer
        {"Title"                , "UTF8"},
107 e52788d0 Michael Niedermayer
        {"Description"                , "UTF8"},
108
        {"Copyright"                , "UTF8"},
109
        {"Encoder"                , "UTF8"},
110
        {"Keyword"                , "UTF8"},
111
        {"Cover"                , "JPEG"},
112
        {"Cover"                , "PNG"},
113
};
114
115 a5247e3d Michael Niedermayer
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){
116
    StreamContext *stream= &nut->stream[stream_index];
117
    
118
    stream->last_key_frame= key_frame;
119 0b43db14 Michael Niedermayer
    nut->packet_start[ frame_type ]= frame_start;
120 a5247e3d Michael Niedermayer
    stream->last_pts= pts;
121
}
122
123 84ad6130 Michael Niedermayer
static void reset(AVFormatContext *s, int64_t global_ts){
124 a5247e3d Michael Niedermayer
    NUTContext *nut = s->priv_data;
125
    int i;
126
    
127
    for(i=0; i<s->nb_streams; i++){
128
        StreamContext *stream= &nut->stream[i];
129
    
130
        stream->last_key_frame= 1;
131 84ad6130 Michael Niedermayer
132
        stream->last_pts= av_rescale(global_ts, stream->rate_num*(int64_t)nut->rate_den, stream->rate_den*(int64_t)nut->rate_num);
133 a5247e3d Michael Niedermayer
    }
134
}
135
136
static void build_frame_code(AVFormatContext *s){
137
    NUTContext *nut = s->priv_data;
138 84ad6130 Michael Niedermayer
    int key_frame, index, pred, stream_id;
139 a5247e3d Michael Niedermayer
    int start=0;
140
    int end= 255;
141
    int keyframe_0_esc= s->nb_streams > 2;
142 84ad6130 Michael Niedermayer
    int pred_table[10];
143 a5247e3d Michael Niedermayer
144
    if(keyframe_0_esc){
145 0b43db14 Michael Niedermayer
        /* keyframe = 0 escape */
146
        FrameCode *ft= &nut->frame_code[start];
147 84ad6130 Michael Niedermayer
        ft->flags= FLAG_DATA_SIZE;
148 0b43db14 Michael Niedermayer
        ft->stream_id_plus1= 0;
149
        ft->size_mul=1;
150 84ad6130 Michael Niedermayer
        ft->timestamp_delta=0;
151 0b43db14 Michael Niedermayer
        start++;
152 a5247e3d Michael Niedermayer
    }
153
154
    for(stream_id= 0; stream_id<s->nb_streams; stream_id++){
155
        int start2= start + (end-start)*stream_id / s->nb_streams;
156
        int end2  = start + (end-start)*(stream_id+1) / s->nb_streams;
157
        AVCodecContext *codec = &s->streams[stream_id]->codec;
158
        int is_audio= codec->codec_type == CODEC_TYPE_AUDIO;
159
        int intra_only= /*codec->intra_only || */is_audio;
160
        int pred_count;
161
162
        for(key_frame=0; key_frame<2; key_frame++){
163
            if(intra_only && keyframe_0_esc && key_frame==0)
164
                continue;
165
            
166 0b43db14 Michael Niedermayer
            {
167
                FrameCode *ft= &nut->frame_code[start2];
168
                ft->flags= FLAG_KEY_FRAME*key_frame;
169 84ad6130 Michael Niedermayer
                ft->flags|= FLAG_DATA_SIZE;
170 0b43db14 Michael Niedermayer
                ft->stream_id_plus1= stream_id + 1;
171
                ft->size_mul=1;
172 84ad6130 Michael Niedermayer
                ft->timestamp_delta=0;
173 0b43db14 Michael Niedermayer
                start2++;
174 a5247e3d Michael Niedermayer
            }
175
        }
176
177
        key_frame= intra_only;
178
#if 1
179
        if(is_audio){
180
            int frame_bytes= codec->frame_size*(int64_t)codec->bit_rate / (8*codec->sample_rate);
181 84ad6130 Michael Niedermayer
            int pts;
182
            for(pts=0; pts<2; pts++){
183 0b43db14 Michael Niedermayer
                for(pred=0; pred<2; pred++){
184 a5247e3d Michael Niedermayer
                    FrameCode *ft= &nut->frame_code[start2];
185 84ad6130 Michael Niedermayer
                    ft->flags= FLAG_KEY_FRAME*key_frame;
186 a5247e3d Michael Niedermayer
                    ft->stream_id_plus1= stream_id + 1;
187 0b43db14 Michael Niedermayer
                    ft->size_mul=frame_bytes + 2;
188
                    ft->size_lsb=frame_bytes + pred;
189 84ad6130 Michael Niedermayer
                    ft->timestamp_delta=pts;
190 a5247e3d Michael Niedermayer
                    start2++;
191
                }
192
            }
193
        }else{
194
            FrameCode *ft= &nut->frame_code[start2];
195
            ft->flags= FLAG_KEY_FRAME | FLAG_DATA_SIZE;
196
            ft->stream_id_plus1= stream_id + 1;
197
            ft->size_mul=1;
198 84ad6130 Michael Niedermayer
            ft->timestamp_delta=1;
199 a5247e3d Michael Niedermayer
            start2++;
200
        }
201
#endif
202 84ad6130 Michael Niedermayer
203
        if(codec->has_b_frames){
204
            pred_count=5;
205
            pred_table[0]=-2;
206
            pred_table[1]=-1;
207
            pred_table[2]=1;
208
            pred_table[3]=3;
209
            pred_table[4]=4;
210
        }else if(codec->codec_id == CODEC_ID_VORBIS){
211
            pred_count=3;
212
            pred_table[0]=2;
213
            pred_table[1]=9;
214
            pred_table[2]=16;
215
        }else{
216
            pred_count=1;
217
            pred_table[0]=1;
218
        }
219
220 a5247e3d Michael Niedermayer
        for(pred=0; pred<pred_count; pred++){
221
            int start3= start2 + (end2-start2)*pred / pred_count;
222
            int end3  = start2 + (end2-start2)*(pred+1) / pred_count;
223
224
            for(index=start3; index<end3; index++){
225
                FrameCode *ft= &nut->frame_code[index];
226 84ad6130 Michael Niedermayer
                ft->flags= FLAG_KEY_FRAME*key_frame;
227 a5247e3d Michael Niedermayer
                ft->flags|= FLAG_DATA_SIZE;
228
                ft->stream_id_plus1= stream_id + 1;
229
//FIXME use single byte size and pred from last
230
                ft->size_mul= end3-start3;
231
                ft->size_lsb= index - start3;
232 84ad6130 Michael Niedermayer
                ft->timestamp_delta= pred_table[pred];
233 a5247e3d Michael Niedermayer
            }
234
        }
235
    }
236
    memmove(&nut->frame_code['N'+1], &nut->frame_code['N'], sizeof(FrameCode)*(255-'N'));
237 84ad6130 Michael Niedermayer
    nut->frame_code['N'].flags= FLAG_INVALID;
238 a5247e3d Michael Niedermayer
}
239
240 3aa180b8 Alex Beregszaszi
static uint64_t get_v(ByteIOContext *bc)
241
{
242
    uint64_t val = 0;
243
244 465e1dad Michael Niedermayer
    for(;;)
245 3aa180b8 Alex Beregszaszi
    {
246
        int tmp = get_byte(bc);
247
248
        if (tmp&0x80)
249
            val= (val<<7) + tmp - 0x80;
250 0b43db14 Michael Niedermayer
        else{
251
//av_log(NULL, AV_LOG_DEBUG, "get_v()= %lld\n", (val<<7) + tmp);
252 3aa180b8 Alex Beregszaszi
            return (val<<7) + tmp;
253 0b43db14 Michael Niedermayer
        }
254 3aa180b8 Alex Beregszaszi
    }
255
    return -1;
256
}
257
258 e52788d0 Michael Niedermayer
static int get_str(ByteIOContext *bc, char *string, int maxlen){
259
    int len= get_v(bc);
260 3aa180b8 Alex Beregszaszi
    
261 e52788d0 Michael Niedermayer
    if(len && maxlen)
262
        get_buffer(bc, string, FFMIN(len, maxlen));
263
    while(len > maxlen){
264
        get_byte(bc);
265
        len--;
266
    }
267 8c653280 Michael Niedermayer
268 e52788d0 Michael Niedermayer
    if(maxlen)
269
        string[FFMIN(len, maxlen-1)]= 0;
270 8c653280 Michael Niedermayer
    
271 e52788d0 Michael Niedermayer
    if(maxlen == len)
272
        return -1;
273
    else
274
        return 0;
275 3aa180b8 Alex Beregszaszi
}
276
277 84ad6130 Michael Niedermayer
static int64_t get_s(ByteIOContext *bc){
278
    int64_t v = get_v(bc) + 1;
279
280
    if (v&1) return -(v>>1);
281
    else     return  (v>>1);
282
}
283
284 7dddf251 Michael Niedermayer
static uint64_t get_vb(ByteIOContext *bc){
285
    uint64_t val=0;
286
    int i= get_v(bc);
287
    
288
    if(i>8)
289
        return UINT64_MAX;
290
    
291
    while(i--)
292
        val = (val<<8) + get_byte(bc);
293
    
294 0b43db14 Michael Niedermayer
//av_log(NULL, AV_LOG_DEBUG, "get_vb()= %lld\n", val);
295 7dddf251 Michael Niedermayer
    return val;
296
}
297
298 84ad6130 Michael Niedermayer
#ifdef TRACE
299
static inline uint64_t get_v_trace(ByteIOContext *bc, char *file, char *func, int line){
300
    uint64_t v= get_v(bc);
301
302
    printf("get_v %5lld / %llX in %s %s:%d\n", v, v, file, func, line);
303
    return v;
304
}
305
306
static inline int64_t get_s_trace(ByteIOContext *bc, char *file, char *func, int line){
307
    int64_t v= get_s(bc);
308
309
    printf("get_s %5lld / %llX in %s %s:%d\n", v, v, file, func, line);
310
    return v;
311
}
312
313
static inline uint64_t get_vb_trace(ByteIOContext *bc, char *file, char *func, int line){
314
    uint64_t v= get_vb(bc);
315
316
    printf("get_vb %5lld / %llX in %s %s:%d\n", v, v, file, func, line);
317
    return v;
318
}
319
#define get_v(bc)  get_v_trace(bc, __FILE__, __PRETTY_FUNCTION__, __LINE__)
320
#define get_s(bc)  get_s_trace(bc, __FILE__, __PRETTY_FUNCTION__, __LINE__)
321
#define get_vb(bc)  get_vb_trace(bc, __FILE__, __PRETTY_FUNCTION__, __LINE__)
322
#endif
323
324
325 0b43db14 Michael Niedermayer
static int get_packetheader(NUTContext *nut, ByteIOContext *bc, int calculate_checksum)
326 3aa180b8 Alex Beregszaszi
{
327 0b43db14 Michael Niedermayer
    int64_t start, size;
328
    start= url_ftell(bc) - 8;
329 a5247e3d Michael Niedermayer
330 465e1dad Michael Niedermayer
    init_checksum(bc, calculate_checksum ? update_adler32 : NULL, 0);
331 a5247e3d Michael Niedermayer
332
    size= get_v(bc);
333
334 0b43db14 Michael Niedermayer
    nut->packet_start[2] = start;
335 a5247e3d Michael Niedermayer
    nut->written_packet_size= size;
336
337
    return size;
338 3aa180b8 Alex Beregszaszi
}
339
340 ee9f36a8 Michael Niedermayer
static int check_checksum(ByteIOContext *bc){
341
    unsigned long checksum= get_checksum(bc);
342
    return checksum != get_be32(bc);
343
}
344
345 203504d9 Michael Niedermayer
/**
346
 * 
347
 */
348
static int get_length(uint64_t val){
349
    int i;
350 3aa180b8 Alex Beregszaszi
351 7dddf251 Michael Niedermayer
    for (i=7; val>>i; i+=7);
352 3aa180b8 Alex Beregszaszi
353 7dddf251 Michael Niedermayer
    return i;
354 3aa180b8 Alex Beregszaszi
}
355
356 7c5934ed Michael Niedermayer
static uint64_t find_any_startcode(ByteIOContext *bc, int64_t pos){
357
    uint64_t state=0;
358
    
359
    if(pos >= 0)
360
        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
361
362 465e1dad Michael Niedermayer
    while(!url_feof(bc)){
363 7c5934ed Michael Niedermayer
        state= (state<<8) | get_byte(bc);
364
        if((state>>56) != 'N')
365
            continue;
366
        switch(state){
367
        case MAIN_STARTCODE:
368
        case STREAM_STARTCODE:
369
        case KEYFRAME_STARTCODE:
370
        case INFO_STARTCODE:
371
        case INDEX_STARTCODE:
372
            return state;
373
        }
374
    }
375 7dddf251 Michael Niedermayer
376 7c5934ed Michael Niedermayer
    return 0;
377
}
378
379 7dddf251 Michael Niedermayer
/**
380
 * find the given startcode.
381
 * @param code the startcode
382
 * @param pos the start position of the search, or -1 if the current position
383
 * @returns the position of the startcode or -1 if not found
384
 */
385
static int64_t find_startcode(ByteIOContext *bc, uint64_t code, int64_t pos){
386 7c5934ed Michael Niedermayer
    for(;;){
387
        uint64_t startcode= find_any_startcode(bc, pos);
388
        if(startcode == code)
389 7dddf251 Michael Niedermayer
            return url_ftell(bc) - 8;
390 7c5934ed Michael Niedermayer
        else if(startcode == 0)
391
            return -1;
392
        pos=-1;
393
    }
394
}
395
396 764ef400 Mike Melanson
#ifdef CONFIG_ENCODERS
397 84ad6130 Michael Niedermayer
398 7dddf251 Michael Niedermayer
static void put_v(ByteIOContext *bc, uint64_t val)
399 3aa180b8 Alex Beregszaszi
{
400
    int i;
401
402 0b43db14 Michael Niedermayer
//av_log(NULL, AV_LOG_DEBUG, "put_v()= %lld\n", val);
403 3aa180b8 Alex Beregszaszi
    val &= 0x7FFFFFFFFFFFFFFFULL; // FIXME can only encode upto 63 bits currently
404 203504d9 Michael Niedermayer
    i= get_length(val);
405 3aa180b8 Alex Beregszaszi
406 3ac8749b Michael Niedermayer
    for (i-=7; i>0; i-=7){
407 3aa180b8 Alex Beregszaszi
        put_byte(bc, 0x80 | (val>>i));
408 3ac8749b Michael Niedermayer
    }
409 3aa180b8 Alex Beregszaszi
410
    put_byte(bc, val&0x7f);
411
}
412
413 7dddf251 Michael Niedermayer
/**
414
 * stores a string as vb.
415
 */
416
static void put_str(ByteIOContext *bc, const char *string){
417 e52788d0 Michael Niedermayer
    int len= strlen(string);
418 3aa180b8 Alex Beregszaszi
    
419
    put_v(bc, len);
420 e52788d0 Michael Niedermayer
    put_buffer(bc, string, len);
421 7dddf251 Michael Niedermayer
}
422
423 84ad6130 Michael Niedermayer
static void put_s(ByteIOContext *bc, uint64_t val){
424
    if (val<=0) put_v(bc, -2*val  );
425
    else        put_v(bc,  2*val-1);
426
}
427
428 7dddf251 Michael Niedermayer
static void put_vb(ByteIOContext *bc, uint64_t val){
429
    int i;
430 e52788d0 Michael Niedermayer
    
431 7dddf251 Michael Niedermayer
    for (i=8; val>>i; i+=8);
432
433
    put_v(bc, i>>3);
434
    for(i-=8; i>=0; i-=8)
435
        put_byte(bc, (val>>i)&0xFF);
436 a182f5da Fabrice Bellard
}
437
438 84ad6130 Michael Niedermayer
#ifdef TRACE
439
static inline void put_v_trace(ByteIOContext *bc, uint64_t v, char *file, char *func, int line){
440
    printf("get_v %5lld / %llX in %s %s:%d\n", v, v, file, func, line);
441
    
442
    put_v(bc, v);
443
}
444
445
static inline void put_s_trace(ByteIOContext *bc, int64_t v, char *file, char *func, int line){
446
    printf("get_s %5lld / %llX in %s %s:%d\n", v, v, file, func, line);
447
    
448
    put_s(bc, v);
449
}
450
451
static inline void put_vb_trace(ByteIOContext *bc, uint64_t v, char *file, char *func, int line){
452
    printf("get_vb %5lld / %llX in %s %s:%d\n", v, v, file, func, line);
453
    
454
    put_vb(bc, v);
455
}
456
#define put_v(bc, v)  put_v_trace(bc, v, __FILE__, __PRETTY_FUNCTION__, __LINE__)
457
#define put_s(bc, v)  put_s_trace(bc, v, __FILE__, __PRETTY_FUNCTION__, __LINE__)
458
#define put_vb(bc, v)  put_vb_trace(bc, v, __FILE__, __PRETTY_FUNCTION__, __LINE__)
459
#endif
460
461 ee9f36a8 Michael Niedermayer
static int put_packetheader(NUTContext *nut, ByteIOContext *bc, int max_size, int calculate_checksum)
462 3aa180b8 Alex Beregszaszi
{
463
    put_flush_packet(bc);
464 0b43db14 Michael Niedermayer
    nut->packet_start[2]+= nut->written_packet_size;
465
    assert(url_ftell(bc) - 8 == nut->packet_start[2]);
466 a5247e3d Michael Niedermayer
    nut->written_packet_size = max_size;
467 ee9f36a8 Michael Niedermayer
    
468
    if(calculate_checksum)
469
        init_checksum(bc, update_adler32, 0);
470 a5247e3d Michael Niedermayer
471 3aa180b8 Alex Beregszaszi
    /* packet header */
472 a5247e3d Michael Niedermayer
    put_v(bc, nut->written_packet_size); /* forward ptr */
473 3aa180b8 Alex Beregszaszi
474
    return 0;
475
}
476
477 ee9f36a8 Michael Niedermayer
static int update_packetheader(NUTContext *nut, ByteIOContext *bc, int additional_size, int calculate_checksum){
478 0b43db14 Michael Niedermayer
    int64_t start= nut->packet_start[2];
479 a5247e3d Michael Niedermayer
    int64_t cur= url_ftell(bc);
480 203504d9 Michael Niedermayer
    int size= cur - start + additional_size;
481
    
482 ee9f36a8 Michael Niedermayer
    if(calculate_checksum)
483
        size += 4;
484
    
485 a5247e3d Michael Niedermayer
    if(size != nut->written_packet_size){
486
        int i;
487
488
        assert( size <= nut->written_packet_size );
489 203504d9 Michael Niedermayer
    
490 0b43db14 Michael Niedermayer
        url_fseek(bc, start + 8, SEEK_SET);
491 a5247e3d Michael Niedermayer
        for(i=get_length(size); i < get_length(nut->written_packet_size); i+=7)
492
            put_byte(bc, 0x80);
493
        put_v(bc, size);
494
495
        url_fseek(bc, cur, SEEK_SET);
496
        nut->written_packet_size= size; //FIXME may fail if multiple updates with differing sizes, as get_length may differ
497 ee9f36a8 Michael Niedermayer
        
498
        if(calculate_checksum)
499
            put_be32(bc, get_checksum(bc));
500 a5247e3d Michael Niedermayer
    }
501 3aa180b8 Alex Beregszaszi
    
502
    return 0;
503
}
504
505
static int nut_write_header(AVFormatContext *s)
506
{
507
    NUTContext *nut = s->priv_data;
508
    ByteIOContext *bc = &s->pb;
509
    AVCodecContext *codec;
510 84ad6130 Michael Niedermayer
    int i, j, tmp_time, tmp_flags,tmp_stream, tmp_mul, tmp_size, tmp_fields;
511 3aa180b8 Alex Beregszaszi
512 a5247e3d Michael Niedermayer
    nut->avf= s;
513
    
514
    nut->stream =        
515
        av_mallocz(sizeof(StreamContext)*s->nb_streams);
516 ca6ecf50 Michael Niedermayer
        
517
518
    put_buffer(bc, ID_STRING, strlen(ID_STRING));
519
    put_byte(bc, 0);
520
    nut->packet_start[2]= url_ftell(bc);
521 a5247e3d Michael Niedermayer
    
522 3aa180b8 Alex Beregszaszi
    /* main header */
523 3ac8749b Michael Niedermayer
    put_be64(bc, MAIN_STARTCODE);
524 ee9f36a8 Michael Niedermayer
    put_packetheader(nut, bc, 120+5*256, 1);
525 0b43db14 Michael Niedermayer
    put_v(bc, 2); /* version */
526 3aa180b8 Alex Beregszaszi
    put_v(bc, s->nb_streams);
527 0b43db14 Michael Niedermayer
    put_v(bc, MAX_DISTANCE);
528 ca6ecf50 Michael Niedermayer
    put_v(bc, MAX_SHORT_DISTANCE);
529 a5247e3d Michael Niedermayer
    
530 84ad6130 Michael Niedermayer
    put_v(bc, nut->rate_num=1);
531
    put_v(bc, nut->rate_den=2);
532
    put_v(bc, nut->short_startcode=0x4EFE79);
533
    
534 a5247e3d Michael Niedermayer
    build_frame_code(s);
535 84ad6130 Michael Niedermayer
    assert(nut->frame_code['N'].flags == FLAG_INVALID);
536
    
537
    tmp_time= tmp_flags= tmp_stream= tmp_mul= tmp_size= /*tmp_res=*/ INT_MAX;
538 a5247e3d Michael Niedermayer
    for(i=0; i<256;){
539 84ad6130 Michael Niedermayer
        tmp_fields=0;
540
        tmp_size= 0;
541
        if(tmp_time   != nut->frame_code[i].timestamp_delta) tmp_fields=1;
542
        if(tmp_mul    != nut->frame_code[i].size_mul       ) tmp_fields=2;
543
        if(tmp_stream != nut->frame_code[i].stream_id_plus1) tmp_fields=3;
544
        if(tmp_size   != nut->frame_code[i].size_lsb       ) tmp_fields=4;
545
//        if(tmp_res    != nut->frame_code[i].res            ) tmp_fields=5;
546
547
        tmp_time  = nut->frame_code[i].timestamp_delta;
548
        tmp_flags = nut->frame_code[i].flags;
549
        tmp_stream= nut->frame_code[i].stream_id_plus1;
550
        tmp_mul   = nut->frame_code[i].size_mul;
551
        tmp_size  = nut->frame_code[i].size_lsb;
552
//        tmp_res   = nut->frame_code[i].res;
553
        
554 a5247e3d Michael Niedermayer
        for(j=0; i<256; j++,i++){
555 84ad6130 Michael Niedermayer
            if(nut->frame_code[i].timestamp_delta != tmp_time  ) break;
556 a5247e3d Michael Niedermayer
            if(nut->frame_code[i].flags           != tmp_flags ) break;
557
            if(nut->frame_code[i].stream_id_plus1 != tmp_stream) break;
558
            if(nut->frame_code[i].size_mul        != tmp_mul   ) break;
559 84ad6130 Michael Niedermayer
            if(nut->frame_code[i].size_lsb        != tmp_size+j) break;
560
//            if(nut->frame_code[i].res             != tmp_res   ) break;
561 a5247e3d Michael Niedermayer
        }
562 84ad6130 Michael Niedermayer
        if(j != tmp_mul - tmp_size) tmp_fields=6;
563
564
        put_v(bc, tmp_flags);
565
        put_v(bc, tmp_fields);
566
        if(tmp_fields>0) put_s(bc, tmp_time);
567
        if(tmp_fields>1) put_v(bc, tmp_mul);
568
        if(tmp_fields>2) put_v(bc, tmp_stream);
569
        if(tmp_fields>3) put_v(bc, tmp_size);
570
        if(tmp_fields>4) put_v(bc, 0 /*tmp_res*/);
571
        if(tmp_fields>5) put_v(bc, j);
572 a5247e3d Michael Niedermayer
    }
573
574 ee9f36a8 Michael Niedermayer
    update_packetheader(nut, bc, 0, 1);
575 203504d9 Michael Niedermayer
    
576 3aa180b8 Alex Beregszaszi
    /* stream headers */
577
    for (i = 0; i < s->nb_streams; i++)
578
    {
579 a5247e3d Michael Niedermayer
        int nom, denom, gcd;
580 cf246283 Alex Beregszaszi
581 3aa180b8 Alex Beregszaszi
        codec = &s->streams[i]->codec;
582
        
583 3ac8749b Michael Niedermayer
        put_be64(bc, STREAM_STARTCODE);
584 ee9f36a8 Michael Niedermayer
        put_packetheader(nut, bc, 120 + codec->extradata_size, 1);
585 99b17f25 Alex Beregszaszi
        put_v(bc, i /*s->streams[i]->index*/);
586 3aa180b8 Alex Beregszaszi
        put_v(bc, (codec->codec_type == CODEC_TYPE_AUDIO) ? 32 : 0);
587
        if (codec->codec_tag)
588 7dddf251 Michael Niedermayer
            put_vb(bc, codec->codec_tag);
589 3aa180b8 Alex Beregszaszi
        else if (codec->codec_type == CODEC_TYPE_VIDEO)
590
        {
591 7dddf251 Michael Niedermayer
            put_vb(bc, codec_get_bmp_tag(codec->codec_id));
592 3aa180b8 Alex Beregszaszi
        }
593
        else if (codec->codec_type == CODEC_TYPE_AUDIO)
594
        {
595 7dddf251 Michael Niedermayer
            put_vb(bc, codec_get_wav_tag(codec->codec_id));
596 4b684105 Alex Beregszaszi
        }
597 e52788d0 Michael Niedermayer
        else
598 7dddf251 Michael Niedermayer
            put_vb(bc, 0);
599 4b684105 Alex Beregszaszi
600
        if (codec->codec_type == CODEC_TYPE_VIDEO)
601
        {
602
            nom = codec->frame_rate;
603
            denom = codec->frame_rate_base;
604
        }
605
        else
606
        {
607 a5247e3d Michael Niedermayer
            nom = codec->sample_rate;
608
            if(codec->frame_size>0)
609
                denom= codec->frame_size;
610
            else
611
                denom= 1; //unlucky
612 3aa180b8 Alex Beregszaszi
        }
613 a5247e3d Michael Niedermayer
        gcd= ff_gcd(nom, denom);
614
        nom   /= gcd;
615
        denom /= gcd;
616
        nut->stream[i].rate_num= nom;
617
        nut->stream[i].rate_den= denom;
618 cdd5034f Michael Niedermayer
        av_set_pts_info(s->streams[i], 60, denom, nom);
619 a5247e3d Michael Niedermayer
620 3aa180b8 Alex Beregszaszi
        put_v(bc, codec->bit_rate);
621 7dddf251 Michael Niedermayer
        put_vb(bc, 0); /* no language code */
622 cf246283 Alex Beregszaszi
        put_v(bc, nom);
623
        put_v(bc, denom);
624 a5247e3d Michael Niedermayer
        if(nom / denom < 1000)
625
            nut->stream[i].msb_timestamp_shift = 7;
626
        else
627
            nut->stream[i].msb_timestamp_shift = 14;
628
        put_v(bc, nut->stream[i].msb_timestamp_shift);
629 0b43db14 Michael Niedermayer
        put_v(bc, codec->has_b_frames);
630 3aa180b8 Alex Beregszaszi
        put_byte(bc, 0); /* flags: 0x1 - fixed_fps, 0x2 - index_present */
631
        
632 a5247e3d Michael Niedermayer
        if(codec->extradata_size){
633
            put_v(bc, 1);
634
            put_v(bc, codec->extradata_size);
635
            put_buffer(bc, codec->extradata, codec->extradata_size);            
636
        }
637
        put_v(bc, 0); /* end of codec specific headers */
638 3aa180b8 Alex Beregszaszi
        
639
        switch(codec->codec_type)
640
        {
641
            case CODEC_TYPE_AUDIO:
642 0b43db14 Michael Niedermayer
                put_v(bc, codec->sample_rate);
643
                put_v(bc, 1);
644 3aa180b8 Alex Beregszaszi
                put_v(bc, codec->channels);
645
                break;
646
            case CODEC_TYPE_VIDEO:
647
                put_v(bc, codec->width);
648
                put_v(bc, codec->height);
649 a5247e3d Michael Niedermayer
                put_v(bc, codec->sample_aspect_ratio.num);
650
                put_v(bc, codec->sample_aspect_ratio.den);
651 3aa180b8 Alex Beregszaszi
                put_v(bc, 0); /* csp type -- unknown */
652
                break;
653 a182f5da Fabrice Bellard
            default:
654
                break;
655 3aa180b8 Alex Beregszaszi
        }
656 ee9f36a8 Michael Niedermayer
        update_packetheader(nut, bc, 0, 1);
657 3aa180b8 Alex Beregszaszi
    }
658
659
    /* info header */
660 ed0bb7be Alex Beregszaszi
    put_be64(bc, INFO_STARTCODE);
661 e52788d0 Michael Niedermayer
    put_packetheader(nut, bc, 30+strlen(s->author)+strlen(s->title)+
662 ee9f36a8 Michael Niedermayer
        strlen(s->comment)+strlen(s->copyright)+strlen(LIBAVFORMAT_IDENT), 1); 
663 3aa180b8 Alex Beregszaszi
    if (s->author[0])
664
    {
665 e52788d0 Michael Niedermayer
        put_v(bc, 9); /* type */
666
        put_str(bc, s->author);
667 3aa180b8 Alex Beregszaszi
    }
668
    if (s->title[0])
669
    {
670 e52788d0 Michael Niedermayer
        put_v(bc, 10); /* type */
671
        put_str(bc, s->title);
672 3aa180b8 Alex Beregszaszi
    }
673
    if (s->comment[0])
674
    {
675 e52788d0 Michael Niedermayer
        put_v(bc, 11); /* type */
676
        put_str(bc, s->comment);
677 3aa180b8 Alex Beregszaszi
    }
678
    if (s->copyright[0])
679
    {
680 e52788d0 Michael Niedermayer
        put_v(bc, 12); /* type */
681
        put_str(bc, s->copyright);
682 3aa180b8 Alex Beregszaszi
    }
683
    /* encoder */
684 75553f82 Michael Niedermayer
    if(!(s->streams[0]->codec.flags & CODEC_FLAG_BITEXACT)){
685
        put_v(bc, 13); /* type */
686
        put_str(bc, LIBAVFORMAT_IDENT);
687
    }
688 99b17f25 Alex Beregszaszi
    
689
    put_v(bc, 0); /* eof info */
690 ee9f36a8 Michael Niedermayer
    update_packetheader(nut, bc, 0, 1);
691 3aa180b8 Alex Beregszaszi
        
692
    put_flush_packet(bc);
693
    
694
    return 0;
695
}
696
697 0b43db14 Michael Niedermayer
static int64_t lsb2full(StreamContext *stream, int64_t lsb){
698
    int64_t mask = (1<<stream->msb_timestamp_shift)-1;
699
    int64_t delta= stream->last_pts - mask/2;
700
    return  ((lsb - delta)&mask) + delta;
701
}
702
703 e928649b Michael Niedermayer
static int nut_write_packet(AVFormatContext *s, AVPacket *pkt)
704 3aa180b8 Alex Beregszaszi
{
705
    NUTContext *nut = s->priv_data;
706 e928649b Michael Niedermayer
    StreamContext *stream= &nut->stream[pkt->stream_index];
707 3aa180b8 Alex Beregszaszi
    ByteIOContext *bc = &s->pb;
708 a5247e3d Michael Niedermayer
    int key_frame = 0, full_pts=0;
709 3aa180b8 Alex Beregszaszi
    AVCodecContext *enc;
710 0b43db14 Michael Niedermayer
    int64_t coded_pts;
711 84ad6130 Michael Niedermayer
    int frame_type, best_length, frame_code, flags, i, size_mul, size_lsb, time_delta;
712 a5247e3d Michael Niedermayer
    const int64_t frame_start= url_ftell(bc);
713 e928649b Michael Niedermayer
    int64_t pts= pkt->pts;
714
    int size= pkt->size;
715
    int stream_index= pkt->stream_index;
716 3aa180b8 Alex Beregszaszi
717
    enc = &s->streams[stream_index]->codec;
718 e928649b Michael Niedermayer
    key_frame = !!(pkt->flags & PKT_FLAG_KEY);
719 a5247e3d Michael Niedermayer
    
720
    frame_type=0;
721 0b43db14 Michael Niedermayer
    if(frame_start + size + 20 - FFMAX(nut->packet_start[1], nut->packet_start[2]) > MAX_DISTANCE)
722
        frame_type=2;
723
    if(key_frame && !stream->last_key_frame)
724
        frame_type=2;
725 3aa180b8 Alex Beregszaszi
726 84ad6130 Michael Niedermayer
    if(frame_type>1){
727
        int64_t global_ts= av_rescale(pts, stream->rate_den*(int64_t)nut->rate_num, stream->rate_num*(int64_t)nut->rate_den);
728
        reset(s, global_ts);
729
        put_be64(bc, KEYFRAME_STARTCODE);
730
        put_v(bc, global_ts);
731 a5247e3d Michael Niedermayer
    }
732 84ad6130 Michael Niedermayer
    assert(stream->last_pts != AV_NOPTS_VALUE);
733
    coded_pts = pts & ((1<<stream->msb_timestamp_shift)-1);
734
    if(lsb2full(stream, coded_pts) != pts)
735
        full_pts=1;
736 a5247e3d Michael Niedermayer
737 0b43db14 Michael Niedermayer
    if(full_pts)
738
        coded_pts= pts + (1<<stream->msb_timestamp_shift);
739 a5247e3d Michael Niedermayer
740
    best_length=INT_MAX;
741
    frame_code= -1;
742
    for(i=0; i<256; i++){
743
        int stream_id_plus1= nut->frame_code[i].stream_id_plus1;
744 aec8f88a Michael Niedermayer
        int fc_key_frame;
745 a5247e3d Michael Niedermayer
        int length=0;
746
        size_mul= nut->frame_code[i].size_mul;
747
        size_lsb= nut->frame_code[i].size_lsb;
748 84ad6130 Michael Niedermayer
        time_delta= nut->frame_code[i].timestamp_delta;
749 a5247e3d Michael Niedermayer
        flags= nut->frame_code[i].flags;
750
751 0b43db14 Michael Niedermayer
        assert(size_mul > size_lsb);
752
        
753 a5247e3d Michael Niedermayer
        if(stream_id_plus1 == 0) length+= get_length(stream_index);
754
        else if(stream_id_plus1 - 1 != stream_index)
755
            continue;
756 aec8f88a Michael Niedermayer
        fc_key_frame= !!(flags & FLAG_KEY_FRAME);
757
758 a5247e3d Michael Niedermayer
        assert(key_frame==0 || key_frame==1);
759
        if(fc_key_frame != key_frame)
760
            continue;
761
762 0b43db14 Michael Niedermayer
        if(flags & FLAG_DATA_SIZE){
763 a5247e3d Michael Niedermayer
            if(size % size_mul != size_lsb)
764
                continue;
765 0b43db14 Michael Niedermayer
            length += get_length(size / size_mul);
766
        }else if(size != size_lsb)
767
            continue;
768 a5247e3d Michael Niedermayer
769 84ad6130 Michael Niedermayer
        if(full_pts && time_delta)
770 a5247e3d Michael Niedermayer
            continue;
771
            
772 84ad6130 Michael Niedermayer
        if(!time_delta){
773 0b43db14 Michael Niedermayer
            length += get_length(coded_pts);
774 a5247e3d Michael Niedermayer
        }else{
775 84ad6130 Michael Niedermayer
            if(time_delta != pts - stream->last_pts)
776 a5247e3d Michael Niedermayer
                continue;
777
        }
778
779
        if(length < best_length){
780
            best_length= length;
781
            frame_code=i;
782
        }
783
//    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);
784
    }
785 3ac8749b Michael Niedermayer
786 a5247e3d Michael Niedermayer
    assert(frame_code != -1);
787
    flags= nut->frame_code[frame_code].flags;
788
    size_mul= nut->frame_code[frame_code].size_mul;
789
    size_lsb= nut->frame_code[frame_code].size_lsb;
790 84ad6130 Michael Niedermayer
    time_delta= nut->frame_code[frame_code].timestamp_delta;
791
#ifdef TRACE
792 a5247e3d Michael Niedermayer
    best_length /= 7;
793
    best_length ++; //frame_code
794 0b43db14 Michael Niedermayer
    if(frame_type==2){
795
        best_length += 8; // startcode
796 a5247e3d Michael Niedermayer
    }
797 84ad6130 Michael Niedermayer
    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 fs:%lld\n", key_frame, frame_type, full_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, frame_start);
798 0b43db14 Michael Niedermayer
//    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]);
799 a5247e3d Michael Niedermayer
#endif
800
801 0b43db14 Michael Niedermayer
    assert(frame_type != 1); //short startcode not implemented yet
802 a5247e3d Michael Niedermayer
    put_byte(bc, frame_code);
803
804
    if(nut->frame_code[frame_code].stream_id_plus1 == 0)
805
        put_v(bc, stream_index);
806 84ad6130 Michael Niedermayer
    if (!time_delta){
807 0b43db14 Michael Niedermayer
        put_v(bc, coded_pts);
808 a5247e3d Michael Niedermayer
    }
809
    if(flags & FLAG_DATA_SIZE)
810
        put_v(bc, size / size_mul);
811 84ad6130 Michael Niedermayer
    else
812
        assert(size == size_lsb);
813 0b43db14 Michael Niedermayer
    if(size > MAX_DISTANCE){
814
        assert(frame_type > 1);
815 a5247e3d Michael Niedermayer
    }
816 3aa180b8 Alex Beregszaszi
    
817 e928649b Michael Niedermayer
    put_buffer(bc, pkt->data, size);
818 3aa180b8 Alex Beregszaszi
819 a5247e3d Michael Niedermayer
    update(nut, stream_index, frame_start, frame_type, frame_code, key_frame, size, pts);
820
    
821 3aa180b8 Alex Beregszaszi
    return 0;
822
}
823
824
static int nut_write_trailer(AVFormatContext *s)
825
{
826 f0a93eb0 Alex Beregszaszi
    NUTContext *nut = s->priv_data;
827 3aa180b8 Alex Beregszaszi
    ByteIOContext *bc = &s->pb;
828 a5247e3d Michael Niedermayer
829 3aa180b8 Alex Beregszaszi
#if 0
830
    int i;
831

832
    /* WRITE INDEX */
833

834
    for (i = 0; s->nb_streams; i++)
835
    {
836 3ac8749b Michael Niedermayer
        put_be64(bc, INDEX_STARTCODE);
837 ee9f36a8 Michael Niedermayer
        put_packetheader(nut, bc, 64, 1);
838 3aa180b8 Alex Beregszaszi
        put_v(bc, s->streams[i]->id);
839
        put_v(bc, ...);
840 ee9f36a8 Michael Niedermayer
        update_packetheader(nut, bc, 0, 1);
841 3aa180b8 Alex Beregszaszi
    }
842
#endif
843
844
    put_flush_packet(bc);
845 f0a93eb0 Alex Beregszaszi
    
846 a5247e3d Michael Niedermayer
    av_freep(&nut->stream);
847 3aa180b8 Alex Beregszaszi
848
    return 0;
849
}
850 764ef400 Mike Melanson
#endif //CONFIG_ENCODERS
851 3aa180b8 Alex Beregszaszi
852
static int nut_probe(AVProbeData *p)
853
{
854 3ac8749b Michael Niedermayer
    int i;
855 0b43db14 Michael Niedermayer
    uint64_t code= 0xff;
856 3ac8749b Michael Niedermayer
857
    for (i = 0; i < p->buf_size; i++) {
858 0b43db14 Michael Niedermayer
        code = (code << 8) | p->buf[i];
859 3ac8749b Michael Niedermayer
        if (code == MAIN_STARTCODE)
860
            return AVPROBE_SCORE_MAX;
861
    }
862
    return 0;
863 3aa180b8 Alex Beregszaszi
}
864
865 7c5934ed Michael Niedermayer
static int decode_main_header(NUTContext *nut){
866
    AVFormatContext *s= nut->avf;
867 3aa180b8 Alex Beregszaszi
    ByteIOContext *bc = &s->pb;
868 3ac8749b Michael Niedermayer
    uint64_t tmp;
869 84ad6130 Michael Niedermayer
    int i, j, tmp_stream, tmp_mul, tmp_time, tmp_size, count, tmp_res;
870 3aa180b8 Alex Beregszaszi
    
871 0b43db14 Michael Niedermayer
    get_packetheader(nut, bc, 1);
872 7c5934ed Michael Niedermayer
873 3aa180b8 Alex Beregszaszi
    tmp = get_v(bc);
874 0b43db14 Michael Niedermayer
    if (tmp != 2){
875 bc874dae Michel Bardiaux
        av_log(s, AV_LOG_ERROR, "bad version (%Ld)\n", tmp);
876 7c5934ed Michael Niedermayer
        return -1;
877
    }
878 3aa180b8 Alex Beregszaszi
    
879 7c5934ed Michael Niedermayer
    nut->stream_count = get_v(bc);
880 0b43db14 Michael Niedermayer
    nut->max_distance = get_v(bc);
881 ca6ecf50 Michael Niedermayer
    nut->max_short_distance = get_v(bc);
882 84ad6130 Michael Niedermayer
    nut->rate_num= get_v(bc);
883
    nut->rate_den= get_v(bc);
884
    nut->short_startcode= get_v(bc);
885
    if(nut->short_startcode>>16 != 'N'){
886
        av_log(s, AV_LOG_ERROR, "invalid short startcode %X\n", nut->short_startcode);
887
        return -1;
888
    }
889 0b43db14 Michael Niedermayer
    
890 a5247e3d Michael Niedermayer
    for(i=0; i<256;){
891
        int tmp_flags = get_v(bc);
892 84ad6130 Michael Niedermayer
        int tmp_fields= get_v(bc);
893
        if(tmp_fields>0) tmp_time  = get_s(bc);
894
        if(tmp_fields>1) tmp_mul   = get_v(bc);
895
        if(tmp_fields>2) tmp_stream= get_v(bc);
896
        if(tmp_fields>3) tmp_size  = get_v(bc);
897
        else             tmp_size  = 0;
898
        if(tmp_fields>4) tmp_res   = get_v(bc);
899
        else             tmp_res   = 0;
900
        if(tmp_fields>5) count     = get_v(bc);
901
        else             count     = tmp_mul - tmp_size;
902
        
903
        while(tmp_fields-- > 6) 
904
           get_v(bc);
905
        
906 a5247e3d Michael Niedermayer
        if(count == 0 || i+count > 256){
907
            av_log(s, AV_LOG_ERROR, "illegal count %d at %d\n", count, i);
908
            return -1;
909
        }
910 84ad6130 Michael Niedermayer
        if(tmp_stream > nut->stream_count + 1){
911
            av_log(s, AV_LOG_ERROR, "illegal stream number\n");
912
            return -1;
913
        }
914 a5247e3d Michael Niedermayer
915
        for(j=0; j<count; j++,i++){
916
            nut->frame_code[i].flags           = tmp_flags ;
917 84ad6130 Michael Niedermayer
            nut->frame_code[i].timestamp_delta = tmp_time  ;
918 a5247e3d Michael Niedermayer
            nut->frame_code[i].stream_id_plus1 = tmp_stream;
919
            nut->frame_code[i].size_mul        = tmp_mul   ;
920 84ad6130 Michael Niedermayer
            nut->frame_code[i].size_lsb        = tmp_size+j;
921
            nut->frame_code[i].reserved_count  = tmp_res   ;
922 a5247e3d Michael Niedermayer
        }
923
    }
924 84ad6130 Michael Niedermayer
    if(nut->frame_code['N'].flags != FLAG_INVALID){
925 a5247e3d Michael Niedermayer
        av_log(s, AV_LOG_ERROR, "illegal frame_code table\n");
926
        return -1;
927
    }
928 7c5934ed Michael Niedermayer
929 ee9f36a8 Michael Niedermayer
    if(check_checksum(bc)){
930
        av_log(s, AV_LOG_ERROR, "Main header checksum missmatch\n");
931
        return -1;
932
    }
933 f0a93eb0 Alex Beregszaszi
934 7c5934ed Michael Niedermayer
    return 0;
935
}
936
937
static int decode_stream_header(NUTContext *nut){
938
    AVFormatContext *s= nut->avf;
939
    ByteIOContext *bc = &s->pb;
940 84ad6130 Michael Niedermayer
    int class, nom, denom, stream_id;
941 7c5934ed Michael Niedermayer
    uint64_t tmp;
942
    AVStream *st;
943
    
944 0b43db14 Michael Niedermayer
    get_packetheader(nut, bc, 1);
945 7c5934ed Michael Niedermayer
    stream_id= get_v(bc);
946
    if(stream_id >= nut->stream_count || s->streams[stream_id])
947
        return -1;
948 3aa180b8 Alex Beregszaszi
    
949 7c5934ed Michael Niedermayer
    st = av_new_stream(s, stream_id);
950
    if (!st)
951
        return AVERROR_NOMEM;
952 9ee91c2f Michael Niedermayer
953 7c5934ed Michael Niedermayer
    class = get_v(bc);
954 7dddf251 Michael Niedermayer
    tmp = get_vb(bc);
955 ee76016c Michael Niedermayer
    st->codec.codec_tag= tmp;
956 7c5934ed Michael Niedermayer
    switch(class)
957 3aa180b8 Alex Beregszaszi
    {
958 7c5934ed Michael Niedermayer
        case 0:
959
            st->codec.codec_type = CODEC_TYPE_VIDEO;
960
            st->codec.codec_id = codec_get_bmp_id(tmp);
961
            if (st->codec.codec_id == CODEC_ID_NONE)
962
                av_log(s, AV_LOG_ERROR, "Unknown codec?!\n");
963
            break;
964
        case 32:
965
            st->codec.codec_type = CODEC_TYPE_AUDIO;
966
            st->codec.codec_id = codec_get_wav_id(tmp);
967
            if (st->codec.codec_id == CODEC_ID_NONE)
968
                av_log(s, AV_LOG_ERROR, "Unknown codec?!\n");
969
            break;
970
        default:
971
            av_log(s, AV_LOG_ERROR, "Unknown stream class (%d)\n", class);
972 ee9f36a8 Michael Niedermayer
            return -1;
973 e52788d0 Michael Niedermayer
    }
974 7c5934ed Michael Niedermayer
    s->bit_rate += get_v(bc);
975 7dddf251 Michael Niedermayer
    get_vb(bc); /* language code */
976 7c5934ed Michael Niedermayer
    nom = get_v(bc);
977
    denom = get_v(bc);
978
    nut->stream[stream_id].msb_timestamp_shift = get_v(bc);
979 0b43db14 Michael Niedermayer
    nut->stream[stream_id].decode_delay= get_v(bc);
980 7c5934ed Michael Niedermayer
    get_byte(bc); /* flags */
981
982
    /* codec specific data headers */
983
    while(get_v(bc) != 0){
984
        st->codec.extradata_size= get_v(bc);
985
        st->codec.extradata= av_mallocz(st->codec.extradata_size);
986
        get_buffer(bc, st->codec.extradata, st->codec.extradata_size);            
987
//            url_fskip(bc, get_v(bc));
988
    }
989 e52788d0 Michael Niedermayer
    
990 7c5934ed Michael Niedermayer
    if (class == 0) /* VIDEO */
991
    {
992
        st->codec.width = get_v(bc);
993
        st->codec.height = get_v(bc);
994
        st->codec.sample_aspect_ratio.num= get_v(bc);
995
        st->codec.sample_aspect_ratio.den= get_v(bc);
996
        get_v(bc); /* csp type */
997
998
        st->codec.frame_rate = nom;
999
        st->codec.frame_rate_base = denom;
1000
    }
1001
    if (class == 32) /* AUDIO */
1002
    {
1003 0b43db14 Michael Niedermayer
        st->codec.sample_rate = get_v(bc);
1004
        get_v(bc); // samplerate_den
1005 7c5934ed Michael Niedermayer
        st->codec.channels = get_v(bc);
1006
    }
1007
    if(check_checksum(bc)){
1008
        av_log(s, AV_LOG_ERROR, "Stream header %d checksum missmatch\n", stream_id);
1009
        return -1;
1010
    }
1011 cdd5034f Michael Niedermayer
    av_set_pts_info(s->streams[stream_id], 60, denom, nom);
1012 7c5934ed Michael Niedermayer
    nut->stream[stream_id].rate_num= nom;
1013
    nut->stream[stream_id].rate_den= denom;
1014
    return 0;
1015
}
1016 e52788d0 Michael Niedermayer
1017 7c5934ed Michael Niedermayer
static int decode_info_header(NUTContext *nut){
1018
    AVFormatContext *s= nut->avf;
1019
    ByteIOContext *bc = &s->pb;
1020
    
1021 0b43db14 Michael Niedermayer
    get_packetheader(nut, bc, 1);
1022 7c5934ed Michael Niedermayer
1023
    for(;;){
1024
        int id= get_v(bc);
1025
        char *name, *type, custom_name[256], custom_type[256];
1026
1027
        if(!id)
1028
            break;
1029
        else if(id >= sizeof(info_table)/sizeof(info_table[0])){
1030
            av_log(s, AV_LOG_ERROR, "info id is too large %d %d\n", id, sizeof(info_table)/sizeof(info_table[0]));
1031
            return -1;
1032
        }
1033 e52788d0 Michael Niedermayer
1034 7c5934ed Michael Niedermayer
        type= info_table[id][1];
1035
        name= info_table[id][0];
1036 e52788d0 Michael Niedermayer
//av_log(s, AV_LOG_DEBUG, "%d %s %s\n", id, type, name);
1037
1038 7c5934ed Michael Niedermayer
        if(!type){
1039
            get_str(bc, custom_type, sizeof(custom_type));
1040
            type= custom_type;
1041
        }
1042
        if(!name){
1043
            get_str(bc, custom_name, sizeof(custom_name));
1044
            name= custom_name;
1045 e52788d0 Michael Niedermayer
        }
1046 7c5934ed Michael Niedermayer
        
1047
        if(!strcmp(type, "v")){
1048
            int value= get_v(bc);
1049
        }else{
1050
            if(!strcmp(name, "Author"))
1051
                get_str(bc, s->author, sizeof(s->author));
1052
            else if(!strcmp(name, "Title"))
1053
                get_str(bc, s->title, sizeof(s->title));
1054
            else if(!strcmp(name, "Copyright"))
1055
                get_str(bc, s->copyright, sizeof(s->copyright));
1056
            else if(!strcmp(name, "Description"))
1057
                get_str(bc, s->comment, sizeof(s->comment));
1058
            else
1059
                get_str(bc, NULL, 0);
1060 ee9f36a8 Michael Niedermayer
        }
1061 7c5934ed Michael Niedermayer
    }
1062
    if(check_checksum(bc)){
1063
        av_log(s, AV_LOG_ERROR, "Info header checksum missmatch\n");
1064
        return -1;
1065
    }
1066
    return 0;
1067
}
1068
1069
static int nut_read_header(AVFormatContext *s, AVFormatParameters *ap)
1070
{
1071
    NUTContext *nut = s->priv_data;
1072
    ByteIOContext *bc = &s->pb;
1073
    int64_t pos;
1074
    int inited_stream_count;
1075
1076
    nut->avf= s;
1077 3aa180b8 Alex Beregszaszi
    
1078 7c5934ed Michael Niedermayer
    /* main header */
1079
    pos=0;
1080
    for(;;){
1081 7dddf251 Michael Niedermayer
        pos= find_startcode(bc, MAIN_STARTCODE, pos)+1;
1082
        if (pos<0){
1083 7c5934ed Michael Niedermayer
            av_log(s, AV_LOG_ERROR, "no main startcode found\n");
1084
            return -1;
1085
        }
1086
        if(decode_main_header(nut) >= 0)
1087
            break;
1088
    }
1089
    
1090
    
1091
    s->bit_rate = 0;
1092
1093
    nut->stream = av_malloc(sizeof(StreamContext)*nut->stream_count);
1094
1095
    /* stream headers */
1096
    pos=0;
1097
    for(inited_stream_count=0; inited_stream_count < nut->stream_count;){
1098 7dddf251 Michael Niedermayer
        pos= find_startcode(bc, STREAM_STARTCODE, pos)+1;
1099
        if (pos<0){
1100 7c5934ed Michael Niedermayer
            av_log(s, AV_LOG_ERROR, "not all stream headers found\n");
1101
            return -1;
1102
        }
1103
        if(decode_stream_header(nut) >= 0)
1104
            inited_stream_count++;
1105
    }
1106
1107
    /* info headers */
1108
    pos=0;
1109
    for(;;){
1110
        uint64_t startcode= find_any_startcode(bc, pos);
1111
        pos= url_ftell(bc);
1112
1113
        if(startcode==0){
1114
            av_log(s, AV_LOG_ERROR, "EOF before video frames\n");
1115
            return -1;
1116
        }else if(startcode == KEYFRAME_STARTCODE){
1117 465e1dad Michael Niedermayer
            nut->next_startcode= startcode;
1118 7c5934ed Michael Niedermayer
            break;
1119
        }else if(startcode != INFO_STARTCODE){
1120
            continue;
1121
        }
1122
1123
        decode_info_header(nut);
1124
    }
1125
1126 3aa180b8 Alex Beregszaszi
    return 0;
1127
}
1128
1129 84ad6130 Michael Niedermayer
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, int64_t frame_start){
1130 465e1dad Michael Niedermayer
    AVFormatContext *s= nut->avf;
1131 a5247e3d Michael Niedermayer
    StreamContext *stream;
1132 3aa180b8 Alex Beregszaszi
    ByteIOContext *bc = &s->pb;
1133 84ad6130 Michael Niedermayer
    int size, flags, size_mul, size_lsb, stream_id, time_delta;
1134 f0a93eb0 Alex Beregszaszi
    int64_t pts = 0;
1135 3aa180b8 Alex Beregszaszi
1136 0b43db14 Michael Niedermayer
    if(frame_type < 2 && frame_start - nut->packet_start[2] > nut->max_distance){
1137
        av_log(s, AV_LOG_ERROR, "last frame must have been damaged\n");
1138
        return -1;
1139
    }
1140
1141
    if(frame_type)
1142
        nut->packet_start[ frame_type ]= frame_start; //otherwise 1 goto 1 may happen
1143
    
1144 a5247e3d Michael Niedermayer
    flags= nut->frame_code[frame_code].flags;
1145
    size_mul= nut->frame_code[frame_code].size_mul;
1146
    size_lsb= nut->frame_code[frame_code].size_lsb;
1147
    stream_id= nut->frame_code[frame_code].stream_id_plus1 - 1;
1148 84ad6130 Michael Niedermayer
    time_delta= nut->frame_code[frame_code].timestamp_delta;
1149
    
1150 a5247e3d Michael Niedermayer
    if(stream_id==-1)
1151
        stream_id= get_v(bc);
1152
    if(stream_id >= s->nb_streams){
1153
        av_log(s, AV_LOG_ERROR, "illegal stream_id\n");
1154
        return -1;
1155
    }
1156
    stream= &nut->stream[stream_id];
1157 465e1dad Michael Niedermayer
1158
//    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]);
1159 0b43db14 Michael Niedermayer
1160
    *key_frame_ret= !!(flags & FLAG_KEY_FRAME);
1161
1162 84ad6130 Michael Niedermayer
    if(!time_delta){
1163 0b43db14 Michael Niedermayer
        int64_t mask = (1<<stream->msb_timestamp_shift)-1;
1164
        pts= get_v(bc);
1165
        if(pts > mask){
1166
            pts -= mask+1;
1167 a5247e3d Michael Niedermayer
        }else{
1168 0b43db14 Michael Niedermayer
            if(stream->last_pts == AV_NOPTS_VALUE){
1169
                av_log(s, AV_LOG_ERROR, "no reference pts available\n");
1170
                return -1;
1171
            }
1172
            pts= lsb2full(stream, pts);
1173 a5247e3d Michael Niedermayer
        }
1174
    }else{
1175 0b43db14 Michael Niedermayer
        if(stream->last_pts == AV_NOPTS_VALUE){
1176
            av_log(s, AV_LOG_ERROR, "no reference pts available\n");
1177
            return -1;
1178
        }
1179 84ad6130 Michael Niedermayer
        pts= stream->last_pts + time_delta;
1180 a5247e3d Michael Niedermayer
    }
1181 0b43db14 Michael Niedermayer
1182
    if(*key_frame_ret){
1183
//        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);
1184
        av_add_index_entry(
1185
            s->streams[stream_id], 
1186
            frame_start, 
1187 cdd5034f Michael Niedermayer
            pts, 
1188 0b43db14 Michael Niedermayer
            frame_start - nut->stream[stream_id].last_sync_pos,
1189
            AVINDEX_KEYFRAME);
1190
        nut->stream[stream_id].last_sync_pos= frame_start;
1191
//                assert(nut->packet_start == frame_start);
1192 a5247e3d Michael Niedermayer
    }
1193 0b43db14 Michael Niedermayer
1194
    assert(size_mul > size_lsb);
1195
    size= size_lsb;
1196
    if(flags & FLAG_DATA_SIZE)
1197
        size+= size_mul*get_v(bc);
1198 a5247e3d Michael Niedermayer
      
1199 84ad6130 Michael Niedermayer
#ifdef TRACE
1200
av_log(s, AV_LOG_DEBUG, "fs:%lld fc:%d ft:%d kf:%d pts:%lld size:%d mul:%d lsb:%d flags:%d delta:%d\n", frame_start, frame_code, frame_type, *key_frame_ret, pts, size, size_mul, size_lsb, flags, time_delta);
1201
#endif
1202 465e1dad Michael Niedermayer
1203 0b43db14 Michael Niedermayer
    if(frame_type==0 && url_ftell(bc) - nut->packet_start[2] + size > nut->max_distance){
1204 465e1dad Michael Niedermayer
        av_log(s, AV_LOG_ERROR, "frame size too large\n");
1205
        return -1;
1206
    }
1207 a5247e3d Michael Niedermayer
    
1208 0b43db14 Michael Niedermayer
    *stream_id_ret = stream_id;
1209 cdd5034f Michael Niedermayer
    *pts_ret = pts;
1210 0b43db14 Michael Niedermayer
1211
    update(nut, stream_id, frame_start, frame_type, frame_code, *key_frame_ret, size, pts);
1212
1213
    return size;
1214
}
1215
1216 84ad6130 Michael Niedermayer
static int decode_frame(NUTContext *nut, AVPacket *pkt, int frame_code, int frame_type, int64_t frame_start){
1217 0b43db14 Michael Niedermayer
    AVFormatContext *s= nut->avf;
1218
    ByteIOContext *bc = &s->pb;
1219
    int size, stream_id, key_frame;
1220
    int64_t pts;
1221
    
1222 84ad6130 Michael Niedermayer
    size= decode_frame_header(nut, &key_frame, &pts, &stream_id, frame_code, frame_type, frame_start);
1223 0b43db14 Michael Niedermayer
    if(size < 0)
1224
        return -1;
1225
1226 3aa180b8 Alex Beregszaszi
    av_new_packet(pkt, size);
1227
    get_buffer(bc, pkt->data, size);
1228 a5247e3d Michael Niedermayer
    pkt->stream_index = stream_id;
1229 3aa180b8 Alex Beregszaszi
    if (key_frame)
1230
        pkt->flags |= PKT_FLAG_KEY;
1231 0b43db14 Michael Niedermayer
    pkt->pts = pts;
1232 a5247e3d Michael Niedermayer
1233
    return 0;
1234
}
1235
1236 465e1dad Michael Niedermayer
static int nut_read_packet(AVFormatContext *s, AVPacket *pkt)
1237
{
1238
    NUTContext *nut = s->priv_data;
1239
    ByteIOContext *bc = &s->pb;
1240 0b43db14 Michael Niedermayer
    int i, frame_code=0;
1241 465e1dad Michael Niedermayer
1242
    for(;;){
1243 84ad6130 Michael Niedermayer
        int64_t pos= url_ftell(bc);
1244 7dddf251 Michael Niedermayer
        int frame_type= 0;
1245 465e1dad Michael Niedermayer
        uint64_t tmp= nut->next_startcode;
1246
        nut->next_startcode=0;
1247
1248
        if (url_feof(bc))
1249
            return -1;
1250
1251 84ad6130 Michael Niedermayer
        if(tmp){
1252
            pos-=8;
1253
        }else{
1254 465e1dad Michael Niedermayer
            frame_code = get_byte(bc);
1255
            if(frame_code == 'N'){
1256
                tmp= frame_code;
1257 7dddf251 Michael Niedermayer
                for(i=1; i<8; i++)
1258
                    tmp = (tmp<<8) + get_byte(bc);
1259 465e1dad Michael Niedermayer
            }
1260
        }
1261
        switch(tmp){
1262
        case MAIN_STARTCODE:
1263
        case STREAM_STARTCODE:
1264
        case INDEX_STARTCODE:
1265 0b43db14 Michael Niedermayer
            get_packetheader(nut, bc, 0);
1266 84ad6130 Michael Niedermayer
            assert(nut->packet_start[2] == pos);
1267 0b43db14 Michael Niedermayer
            url_fseek(bc, nut->written_packet_size + nut->packet_start[2], SEEK_SET);
1268 465e1dad Michael Niedermayer
            break;
1269
        case INFO_STARTCODE:
1270
            if(decode_info_header(nut)<0)
1271
                goto resync;
1272
            break;
1273 7dddf251 Michael Niedermayer
        case KEYFRAME_STARTCODE:
1274
            frame_type = 2;
1275 84ad6130 Michael Niedermayer
            reset(s, get_v(bc));
1276 7dddf251 Michael Niedermayer
            frame_code = get_byte(bc);
1277 465e1dad Michael Niedermayer
        case 0:
1278 84ad6130 Michael Niedermayer
            if(decode_frame(nut, pkt, frame_code, frame_type, pos)>=0)
1279 465e1dad Michael Niedermayer
                return 0;
1280
        default:
1281
resync:
1282 0b43db14 Michael Niedermayer
av_log(s, AV_LOG_DEBUG, "syncing from %lld\n", nut->packet_start[2]+1);
1283
            tmp= find_any_startcode(bc, nut->packet_start[2]+1);
1284 465e1dad Michael Niedermayer
            if(tmp==0)
1285
                return -1;
1286
av_log(s, AV_LOG_DEBUG, "sync\n");
1287 0b43db14 Michael Niedermayer
            nut->next_startcode= tmp;
1288 7dddf251 Michael Niedermayer
        }
1289
    }
1290
}
1291
1292 8d14a25c Michael Niedermayer
static int64_t nut_read_timestamp(AVFormatContext *s, int stream_index, int64_t *pos_arg, int64_t pos_limit){
1293 7dddf251 Michael Niedermayer
    NUTContext *nut = s->priv_data;
1294 8d14a25c Michael Niedermayer
    StreamContext *stream;
1295 7dddf251 Michael Niedermayer
    ByteIOContext *bc = &s->pb;
1296
    int64_t pos, pts;
1297
    uint64_t code;
1298 0b43db14 Michael Niedermayer
    int frame_code,step, stream_id, i,size, key_frame;
1299 7dddf251 Michael Niedermayer
av_log(s, AV_LOG_DEBUG, "read_timestamp(X,%d,%lld,%lld)\n", stream_index, *pos_arg, pos_limit);
1300
1301
    if(*pos_arg < 0)
1302
        return AV_NOPTS_VALUE;
1303
1304
    pos= *pos_arg;
1305
    step= FFMIN(16*1024, pos);
1306
    do{
1307
        pos-= step;
1308
        code= find_any_startcode(bc, pos);
1309
1310 0b43db14 Michael Niedermayer
        if(code && url_ftell(bc) - 8 <= *pos_arg)
1311 7dddf251 Michael Niedermayer
            break;
1312
        step= FFMIN(2*step, pos);
1313
    }while(step);
1314
1315
    if(!code) //nothing found, not even after pos_arg
1316
        return AV_NOPTS_VALUE;
1317
1318
    url_fseek(bc, -8, SEEK_CUR);
1319
    for(i=0; i<s->nb_streams; i++)
1320
        nut->stream[i].last_sync_pos= url_ftell(bc);
1321
        
1322
    for(;;){
1323 0b43db14 Michael Niedermayer
        int frame_type=0;
1324 7dddf251 Michael Niedermayer
        int64_t pos= url_ftell(bc);
1325
        uint64_t tmp=0;
1326
        
1327 0b43db14 Michael Niedermayer
        if(pos > pos_limit || url_feof(bc))
1328 7dddf251 Michael Niedermayer
            return AV_NOPTS_VALUE;
1329
1330
        frame_code = get_byte(bc);
1331
        if(frame_code == 'N'){
1332
            tmp= frame_code;
1333
            for(i=1; i<8; i++)
1334
                tmp = (tmp<<8) + get_byte(bc);
1335
        }
1336
//av_log(s, AV_LOG_DEBUG, "before switch %llX at=%lld\n", tmp, pos);
1337
1338
        switch(tmp){
1339
        case MAIN_STARTCODE:
1340
        case STREAM_STARTCODE:
1341
        case INDEX_STARTCODE:
1342
        case INFO_STARTCODE:
1343 0b43db14 Michael Niedermayer
            get_packetheader(nut, bc, 0);
1344
            assert(nut->packet_start[2]==pos);
1345
            url_fseek(bc, nut->written_packet_size + pos, SEEK_SET);
1346 7dddf251 Michael Niedermayer
            break;
1347
        case KEYFRAME_STARTCODE:
1348 0b43db14 Michael Niedermayer
            frame_type=2;
1349 84ad6130 Michael Niedermayer
            reset(s, get_v(bc));
1350 7dddf251 Michael Niedermayer
            frame_code = get_byte(bc);
1351
        case 0:
1352 84ad6130 Michael Niedermayer
            size= decode_frame_header(nut, &key_frame, &pts, &stream_id, frame_code, frame_type, pos);
1353 0b43db14 Michael Niedermayer
            if(size < 0)
1354 7dddf251 Michael Niedermayer
                goto resync;
1355
                
1356 8d14a25c Michael Niedermayer
            stream= &nut->stream[stream_id];
1357 0b43db14 Michael Niedermayer
            if(stream_id != stream_index || !key_frame || pos < *pos_arg){
1358
                url_fseek(bc, size, SEEK_CUR);
1359 7dddf251 Michael Niedermayer
                break;
1360
            }
1361
 
1362 0b43db14 Michael Niedermayer
            *pos_arg= pos;
1363 7dddf251 Michael Niedermayer
            return pts;
1364
        default:
1365
resync:
1366 0b43db14 Michael Niedermayer
av_log(s, AV_LOG_DEBUG, "syncing from %lld\n", nut->packet_start[2]+1);
1367
            if(!find_any_startcode(bc, nut->packet_start[2]+1))
1368 7dddf251 Michael Niedermayer
                return AV_NOPTS_VALUE;
1369
1370
            url_fseek(bc, -8, SEEK_CUR);
1371 465e1dad Michael Niedermayer
        }
1372
    }
1373 7dddf251 Michael Niedermayer
    return AV_NOPTS_VALUE;
1374
}
1375
1376
static int nut_read_seek(AVFormatContext *s, int stream_index, int64_t target_ts){
1377 0b43db14 Michael Niedermayer
//    NUTContext *nut = s->priv_data;
1378 8d14a25c Michael Niedermayer
    int64_t pos;
1379 7dddf251 Michael Niedermayer
1380 8d14a25c Michael Niedermayer
    if(av_seek_frame_binary(s, stream_index, target_ts) < 0)
1381
        return -1;
1382 7dddf251 Michael Niedermayer
1383 8d14a25c Michael Niedermayer
    pos= url_ftell(&s->pb);
1384 0b43db14 Michael Niedermayer
    nut_read_timestamp(s, stream_index, &pos, pos-1);
1385 7dddf251 Michael Niedermayer
1386
    return 0;
1387 465e1dad Michael Niedermayer
}
1388
1389 a5247e3d Michael Niedermayer
static int nut_read_close(AVFormatContext *s)
1390
{
1391
    NUTContext *nut = s->priv_data;
1392
    int i;
1393
1394
    for(i=0;i<s->nb_streams;i++) {
1395
        av_freep(&s->streams[i]->codec.extradata);
1396
    }
1397
    av_freep(&nut->stream);
1398 3aa180b8 Alex Beregszaszi
1399
    return 0;
1400
}
1401
1402
static AVInputFormat nut_iformat = {
1403
    "nut",
1404
    "nut format",
1405
    sizeof(NUTContext),
1406
    nut_probe,
1407
    nut_read_header,
1408
    nut_read_packet,
1409 a5247e3d Michael Niedermayer
    nut_read_close,
1410 7dddf251 Michael Niedermayer
    nut_read_seek,
1411 8d14a25c Michael Niedermayer
    nut_read_timestamp,
1412 3aa180b8 Alex Beregszaszi
    .extensions = "nut",
1413
};
1414
1415 764ef400 Mike Melanson
#ifdef CONFIG_ENCODERS
1416 3aa180b8 Alex Beregszaszi
static AVOutputFormat nut_oformat = {
1417
    "nut",
1418
    "nut format",
1419
    "video/x-nut",
1420
    "nut",
1421
    sizeof(NUTContext),
1422 ae3fa4ea Michael Niedermayer
#ifdef CONFIG_VORBIS
1423 3aa180b8 Alex Beregszaszi
    CODEC_ID_VORBIS,
1424
#elif defined(CONFIG_MP3LAME)
1425 80783dc2 Fabrice Bellard
    CODEC_ID_MP3,
1426 3aa180b8 Alex Beregszaszi
#else
1427 ed0bb7be Alex Beregszaszi
    CODEC_ID_MP2, /* AC3 needs liba52 decoder */
1428 3aa180b8 Alex Beregszaszi
#endif
1429
    CODEC_ID_MPEG4,
1430
    nut_write_header,
1431
    nut_write_packet,
1432
    nut_write_trailer,
1433
};
1434 764ef400 Mike Melanson
#endif //CONFIG_ENCODERS
1435 3aa180b8 Alex Beregszaszi
1436
int nut_init(void)
1437
{
1438
    av_register_input_format(&nut_iformat);
1439 764ef400 Mike Melanson
#ifdef CONFIG_ENCODERS
1440 3aa180b8 Alex Beregszaszi
    av_register_output_format(&nut_oformat);
1441 764ef400 Mike Melanson
#endif //CONFIG_ENCODERS
1442 3aa180b8 Alex Beregszaszi
    return 0;
1443
}