Statistics
| Branch: | Revision:

ffmpeg / libavformat / nut.c @ 756fb7fe

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

834
    /* WRITE INDEX */
835

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