Statistics
| Branch: | Revision:

ffmpeg / libavformat / nut.c @ c04c3282

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

828
    /* WRITE INDEX */
829

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