Statistics
| Branch: | Revision:

ffmpeg / libavformat / nut.c @ a1d4d70c

History | View | Annotate | Download (29.3 KB)

1
/*
2
 * "NUT" Container Format muxer and demuxer (DRAFT-200403??)
3
 * Copyright (c) 2003 Alex Beregszaszi
4
 * Copyright (c) 2004 Michael Niedermayer
5
 *
6
 * This library is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2 of the License, or (at your option) any later version.
10
 *
11
 * This library is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public
17
 * License along with this library; if not, write to the Free Software
18
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
 *
20
 * NUT DRAFT can be found in MPlayer CVS at DOCS/tech/mpcf.txt
21
 *
22
 * AND http://people.fsn.hu/~alex/nut/ (TeX, pdf, ps, dvi, ..)
23
 *
24
 */
25

    
26
/*
27
 * TODO:
28
 * - checksumming
29
 * - seeking
30
 * - index writing
31
 * - info and index packet reading support
32
 * - startcode searching for broken streams
33
*/
34

    
35
//#define DEBUG 1
36

    
37
#include <limits.h>
38
#include "avformat.h"
39
#include "mpegaudio.h"
40
#include "avi.h"
41

    
42
#undef NDEBUG
43
#include <assert.h>
44

    
45
//from /dev/random
46

    
47
#define     MAIN_STARTCODE (0x7A561F5F04ADULL + (((uint64_t)('N'<<8) + 'M')<<48)) 
48
#define   STREAM_STARTCODE (0x11405BF2F9DBULL + (((uint64_t)('N'<<8) + 'S')<<48)) 
49
#define KEYFRAME_STARTCODE (0xE4ADEECA4569ULL + (((uint64_t)('N'<<8) + 'K')<<48)) 
50
#define    INDEX_STARTCODE (0xDD672F23E64EULL + (((uint64_t)('N'<<8) + 'X')<<48)) 
51
#define     INFO_STARTCODE (0xAB68B596BA78ULL + (((uint64_t)('N'<<8) + 'I')<<48)) 
52

    
53
#define MAX_TYPE1_DISTANCE (1024*16-1)
54
#define MAX_TYPE2_DISTANCE (1024*64-1)
55

    
56
#define MAX_SIZE_LRU 2
57
#define MAX_PTS_LRU 3
58

    
59
#define FLAG_FRAME_TYPE      1
60
#define FLAG_DATA_SIZE       2
61
#define FLAG_PTS            16
62
#define FLAG_FULL_PTS        4
63
#define FLAG_KEY_FRAME      32
64
#define FLAG_PRED_KEY_FRAME 64
65

    
66
typedef struct {
67
    uint8_t flags;
68
    uint8_t stream_id_plus1;
69
    uint8_t size_mul;
70
    uint8_t size_lsb;
71
} FrameCode;
72

    
73
typedef struct {
74
    int last_key_frame;
75
    int msb_timestamp_shift;
76
    int rate_num;
77
    int rate_den;
78
    int64_t last_pts;
79
    int64_t last_full_pts;
80
    int lru_pts_delta[MAX_PTS_LRU];
81
    int lru_size[MAX_SIZE_LRU];
82
    int initial_pts_predictor[MAX_PTS_LRU];
83
    int initial_size_predictor[MAX_SIZE_LRU];
84
} StreamContext;
85

    
86
typedef struct {
87
    AVFormatContext *avf;
88
    int64_t packet_start;
89
    int64_t last_packet_start;
90
    int written_packet_size;
91
    int64_t packet_size_pos;
92
    int64_t last_frame_start[3];
93
    FrameCode frame_code[256];
94
    StreamContext *stream;
95
} NUTContext;
96

    
97
static void update_lru(int *lru, int current, int count){
98
    int i;
99

    
100
    for(i=0; i<count-1; i++){
101
        if(lru[i] == current)
102
            break;
103
    }
104

    
105
    for(; i; i--){
106
        lru[i]= lru[i-1];
107
    }
108

    
109
    lru[0]= current;
110
}
111

    
112
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){
113
    StreamContext *stream= &nut->stream[stream_index];
114
    
115
    stream->last_key_frame= key_frame;
116
    nut->last_frame_start[ frame_type ]= frame_start;
117
    update_lru(stream->lru_pts_delta, pts - stream->last_pts, 3);
118
    update_lru(stream->lru_size     ,                   size, 2);
119
    stream->last_pts= pts;
120
    if(    nut->frame_code[frame_code].flags & FLAG_PTS 
121
        && nut->frame_code[frame_code].flags & FLAG_FULL_PTS)
122
        stream->last_full_pts= pts;
123
}
124

    
125
static void reset(AVFormatContext *s/*, int frame_type*/){
126
    NUTContext *nut = s->priv_data;
127
    int i;
128
    
129
    for(i=0; i<s->nb_streams; i++){
130
        StreamContext *stream= &nut->stream[i];
131
    
132
        stream->last_key_frame= 1;
133
        memcpy(stream->lru_pts_delta, stream->initial_pts_predictor, sizeof(int)*MAX_PTS_LRU);
134
        memcpy(stream->lru_size, stream->initial_size_predictor, sizeof(int)*MAX_SIZE_LRU);
135
    }
136
}
137

    
138
static void build_frame_code(AVFormatContext *s){
139
    NUTContext *nut = s->priv_data;
140
    int key_frame, frame_type, full_pts, index, pred, stream_id;
141
    int start=0;
142
    int end= 255;
143
    int keyframe_0_esc= s->nb_streams > 2;
144

    
145
    if(keyframe_0_esc){
146
        /* keyframe = 0 escapes, 3 codes */
147
        for(frame_type=0; frame_type<2; frame_type++){
148
            for(full_pts=frame_type; full_pts<2; full_pts++){
149
                FrameCode *ft= &nut->frame_code[start];
150
                ft->flags= FLAG_FRAME_TYPE*frame_type + FLAG_FULL_PTS*full_pts;
151
                ft->flags|= FLAG_DATA_SIZE | FLAG_PTS; 
152
                ft->stream_id_plus1= 0;
153
                ft->size_mul=1;
154
                start++;
155
            }
156
        }
157
    }
158

    
159
    for(stream_id= 0; stream_id<s->nb_streams; stream_id++){
160
        int start2= start + (end-start)*stream_id / s->nb_streams;
161
        int end2  = start + (end-start)*(stream_id+1) / s->nb_streams;
162
        AVCodecContext *codec = &s->streams[stream_id]->codec;
163
        int is_audio= codec->codec_type == CODEC_TYPE_AUDIO;
164
        int intra_only= /*codec->intra_only || */is_audio;
165
        int pred_count;
166

    
167
        for(key_frame=0; key_frame<2; key_frame++){
168
            if(intra_only && keyframe_0_esc && key_frame==0)
169
                continue;
170
            
171
            for(frame_type=0; frame_type<2; frame_type++){
172
                for(full_pts=frame_type; full_pts<2; full_pts++){
173
                    FrameCode *ft= &nut->frame_code[start2];
174
                    ft->flags= FLAG_FRAME_TYPE*frame_type + FLAG_FULL_PTS*full_pts + FLAG_KEY_FRAME*key_frame;
175
                    ft->flags|= FLAG_DATA_SIZE | FLAG_PTS;
176
                    ft->stream_id_plus1= stream_id + 1;
177
                    ft->size_mul=1;
178
                    start2++;
179
                }
180
            }
181
        }
182

    
183
        key_frame= intra_only;
184
#if 1
185
        if(is_audio){
186
            int frame_bytes= codec->frame_size*(int64_t)codec->bit_rate / (8*codec->sample_rate);
187
            for(pred=0; pred<MAX_SIZE_LRU; pred++){
188
                for(frame_type=0; frame_type<1; frame_type++){
189
                    FrameCode *ft= &nut->frame_code[start2];
190
                    ft->flags= FLAG_KEY_FRAME*key_frame + (FLAG_FULL_PTS+FLAG_PTS+FLAG_FRAME_TYPE)*frame_type;
191
                    ft->stream_id_plus1= stream_id + 1;
192
                    ft->size_mul=1;
193
                    ft->size_lsb=1 + pred;
194
                    start2++;
195
                }
196
                nut->stream[stream_id].initial_size_predictor[pred]= frame_bytes + pred;
197
            }
198
        }else{
199
            FrameCode *ft= &nut->frame_code[start2];
200
            ft->flags= FLAG_KEY_FRAME | FLAG_DATA_SIZE;
201
            ft->stream_id_plus1= stream_id + 1;
202
            ft->size_mul=1;
203
            start2++;
204
        }
205
#endif
206
        pred_count= 2 + codec->has_b_frames + (codec->codec_id == CODEC_ID_VORBIS);
207
        for(pred=0; pred<pred_count; pred++){
208
            int start3= start2 + (end2-start2)*pred / pred_count;
209
            int end3  = start2 + (end2-start2)*(pred+1) / pred_count;
210

    
211
            for(index=start3; index<end3; index++){
212
                FrameCode *ft= &nut->frame_code[index];
213
                ft->flags= FLAG_KEY_FRAME*key_frame + pred*4;
214
                ft->flags|= FLAG_DATA_SIZE;
215
                ft->stream_id_plus1= stream_id + 1;
216
//FIXME use single byte size and pred from last
217
                ft->size_mul= end3-start3;
218
                ft->size_lsb= index - start3;
219
            }
220
            nut->stream[stream_id].initial_pts_predictor[pred]= pred+1;
221
        }
222
    }
223
    memmove(&nut->frame_code['N'+1], &nut->frame_code['N'], sizeof(FrameCode)*(255-'N'));
224
    nut->frame_code['N'].flags= 1;
225
}
226

    
227
static int bytes_left(ByteIOContext *bc)
228
{
229
    return bc->buf_end - bc->buf_ptr;
230
}
231

    
232
static uint64_t get_v(ByteIOContext *bc)
233
{
234
    uint64_t val = 0;
235

    
236
    for(; bytes_left(bc) > 0; )
237
    {
238
        int tmp = get_byte(bc);
239

    
240
        if (tmp&0x80)
241
            val= (val<<7) + tmp - 0x80;
242
        else
243
            return (val<<7) + tmp;
244
    }
245
    return -1;
246
}
247

    
248
static int get_b(ByteIOContext *bc, char *data, int maxlen)
249
{
250
    int i, len;
251
    
252
    len = get_v(bc);
253
    for (i = 0; i < len && i < maxlen; i++)
254
        data[i] = get_byte(bc);
255
    /* skip remaining bytes */
256
    url_fskip(bc, len-i);
257

    
258
    return 0;
259
}
260

    
261
static int get_bi(ByteIOContext *bc)
262
{
263
   int i, len, val = 0;
264
    
265
    len = get_v(bc);
266
    for (i = 0; i < len && i <= 4; i++)
267
        val |= get_byte(bc) << (i * 8);
268
    /* skip remaining bytes */
269
    url_fskip(bc, len-i);
270

    
271
    return val;
272
}
273

    
274
static int get_packetheader(NUTContext *nut, ByteIOContext *bc, int prefix_length)
275
{
276
    int64_t start, size, last_size;
277
    start= url_ftell(bc) - prefix_length;
278

    
279
    if(start != nut->packet_start + nut->written_packet_size){
280
        av_log(nut->avf, AV_LOG_ERROR, "get_packetheader called at weird position\n");
281
        return -1;
282
    }
283

    
284
    size= get_v(bc);
285
    last_size= get_v(bc);
286
    if(nut->written_packet_size != last_size){
287
        av_log(nut->avf, AV_LOG_ERROR, "packet size missmatch %d != %lld at %lld\n", nut->written_packet_size, last_size, start);
288
        return -1;
289
    }
290

    
291
    nut->last_packet_start = nut->packet_start;
292
    nut->packet_start = start;
293
    nut->written_packet_size= size;
294

    
295
    return size;
296
}
297

    
298
/**
299
 * 
300
 */
301
static int get_length(uint64_t val){
302
    int i;
303

    
304
    for (i=7; ; i+=7)
305
        if ((val>>i) == 0)
306
            return i;
307

    
308
    return 7; //not reached
309
}
310

    
311
#ifdef CONFIG_ENCODERS
312
static int put_v(ByteIOContext *bc, uint64_t val)
313
{
314
    int i;
315
//    if (bytes_left(s)*8 < 9)
316
//        return -1;
317

    
318
    if (bytes_left(bc) < 1)
319
        return -1;
320

    
321
    val &= 0x7FFFFFFFFFFFFFFFULL; // FIXME can only encode upto 63 bits currently
322
    i= get_length(val);
323

    
324
    for (i-=7; i>0; i-=7){
325
        put_byte(bc, 0x80 | (val>>i));
326
    }
327

    
328
    put_byte(bc, val&0x7f);
329

    
330
    return 0;
331
}
332

    
333
static int put_b(ByteIOContext *bc, char *data, int len)
334
{
335
    int i;
336
    
337
    put_v(bc, len);
338
    for (i = 0; i < len; i++)
339
        put_byte(bc, data[i]);
340

    
341
    return 0;
342
}
343

    
344
static int put_bi(ByteIOContext *bc, int val)
345
{
346
    put_v(bc, 4);
347
    put_le32(bc, val);
348
    return 0;
349
}
350

    
351
static int put_packetheader(NUTContext *nut, ByteIOContext *bc, int max_size)
352
{
353
    put_flush_packet(bc);
354
    nut->last_packet_start= nut->packet_start;
355
    nut->packet_start+= nut->written_packet_size;
356
    nut->packet_size_pos = url_ftell(bc);
357
    nut->written_packet_size = max_size;
358

    
359
    /* packet header */
360
    put_v(bc, nut->written_packet_size); /* forward ptr */
361
    put_v(bc, nut->packet_start - nut->last_packet_start); /* backward ptr */
362

    
363
    return 0;
364
}
365

    
366
static int update_packetheader(NUTContext *nut, ByteIOContext *bc, int additional_size){
367
    int64_t start= nut->packet_start;
368
    int64_t cur= url_ftell(bc);
369
    int size= cur - start + additional_size;
370
    
371
    if(size != nut->written_packet_size){
372
        int i;
373

    
374
        assert( size <= nut->written_packet_size );
375
    
376
        url_fseek(bc, nut->packet_size_pos, SEEK_SET);
377
        for(i=get_length(size); i < get_length(nut->written_packet_size); i+=7)
378
            put_byte(bc, 0x80);
379
        put_v(bc, size);
380

    
381
        url_fseek(bc, cur, SEEK_SET);
382
        nut->written_packet_size= size; //FIXME may fail if multiple updates with differing sizes, as get_length may differ
383
    }
384
    
385
    return 0;
386
}
387

    
388
static int nut_write_header(AVFormatContext *s)
389
{
390
    NUTContext *nut = s->priv_data;
391
    ByteIOContext *bc = &s->pb;
392
    AVCodecContext *codec;
393
    int i, j;
394

    
395
    nut->avf= s;
396
    
397
    nut->stream =        
398
        av_mallocz(sizeof(StreamContext)*s->nb_streams);
399
    
400
    av_set_pts_info(s, 60, 1, AV_TIME_BASE);
401
    
402
    /* main header */
403
    put_be64(bc, MAIN_STARTCODE);
404
    put_packetheader(nut, bc, 120+5*256);
405
    put_v(bc, 1); /* version */
406
    put_v(bc, s->nb_streams);
407
    put_v(bc, 3); /* checksum threshold */
408
    
409
    build_frame_code(s);
410
    assert(nut->frame_code['N'].flags == 1);
411
    for(i=0; i<256;){
412
        int tmp_flags = nut->frame_code[i].flags;
413
        int tmp_stream= nut->frame_code[i].stream_id_plus1;
414
        int tmp_mul   = nut->frame_code[i].size_mul;
415
        int tmp_size  = nut->frame_code[i].size_lsb;
416
        put_v(bc, tmp_flags);
417
        put_v(bc, tmp_stream);
418
        put_v(bc, tmp_mul);
419
        put_v(bc, tmp_size);
420

    
421
        for(j=0; i<256; j++,i++){
422
            if(nut->frame_code[i].flags           != tmp_flags ) break;
423
            if(nut->frame_code[i].stream_id_plus1 != tmp_stream) break;
424
            if(nut->frame_code[i].size_mul        != tmp_mul   ) break;
425
            if(nut->frame_code[i].size_lsb        != tmp_size  ) break;
426
            if(++tmp_size >= tmp_mul){
427
                tmp_size=0;
428
                tmp_stream++;
429
            }
430
        }
431
        put_v(bc, j);
432
    }
433

    
434
    put_be32(bc, 0); /* FIXME: checksum */
435
    
436
    update_packetheader(nut, bc, 0);
437
    
438
    /* stream headers */
439
    for (i = 0; i < s->nb_streams; i++)
440
    {
441
        int nom, denom, gcd;
442

    
443
        codec = &s->streams[i]->codec;
444
        
445
        put_be64(bc, STREAM_STARTCODE);
446
        put_packetheader(nut, bc, 120 + codec->extradata_size);
447
        put_v(bc, i /*s->streams[i]->index*/);
448
        put_v(bc, (codec->codec_type == CODEC_TYPE_AUDIO) ? 32 : 0);
449
        if (codec->codec_tag)
450
            put_bi(bc, codec->codec_tag);
451
        else if (codec->codec_type == CODEC_TYPE_VIDEO)
452
        {
453
            int tmp = codec_get_bmp_tag(codec->codec_id);
454
            put_bi(bc, tmp);
455
        }
456
        else if (codec->codec_type == CODEC_TYPE_AUDIO)
457
        {
458
            int tmp = codec_get_wav_tag(codec->codec_id);
459
            put_bi(bc, tmp);
460
        }
461

    
462
        if (codec->codec_type == CODEC_TYPE_VIDEO)
463
        {
464
            nom = codec->frame_rate;
465
            denom = codec->frame_rate_base;
466
        }
467
        else
468
        {
469
            nom = codec->sample_rate;
470
            if(codec->frame_size>0)
471
                denom= codec->frame_size;
472
            else
473
                denom= 1; //unlucky
474
        }
475
        gcd= ff_gcd(nom, denom);
476
        nom   /= gcd;
477
        denom /= gcd;
478
        nut->stream[i].rate_num= nom;
479
        nut->stream[i].rate_den= denom;
480

    
481
        put_v(bc, codec->bit_rate);
482
        put_v(bc, 0); /* no language code */
483
        put_v(bc, nom);
484
        put_v(bc, denom);
485
        if(nom / denom < 1000)
486
            nut->stream[i].msb_timestamp_shift = 7;
487
        else
488
            nut->stream[i].msb_timestamp_shift = 14;
489
        put_v(bc, nut->stream[i].msb_timestamp_shift);
490
        for(j=0; j<3; j++)
491
                put_v(bc, nut->stream[i].initial_pts_predictor[j]);
492
        for(j=0; j<2; j++)
493
                put_v(bc, nut->stream[i].initial_size_predictor[j]);
494
        put_byte(bc, 0); /* flags: 0x1 - fixed_fps, 0x2 - index_present */
495
        
496
        if(codec->extradata_size){
497
            put_v(bc, 1);
498
            put_v(bc, codec->extradata_size);
499
            put_buffer(bc, codec->extradata, codec->extradata_size);            
500
        }
501
        put_v(bc, 0); /* end of codec specific headers */
502
        
503
        switch(codec->codec_type)
504
        {
505
            case CODEC_TYPE_AUDIO:
506
                put_v(bc, (codec->sample_rate * denom) / nom);
507
                put_v(bc, codec->channels);
508
                break;
509
            case CODEC_TYPE_VIDEO:
510
                put_v(bc, codec->width);
511
                put_v(bc, codec->height);
512
                put_v(bc, codec->sample_aspect_ratio.num);
513
                put_v(bc, codec->sample_aspect_ratio.den);
514
                put_v(bc, 0); /* csp type -- unknown */
515
                break;
516
            default:
517
                break;
518
        }
519
        put_be32(bc, 0); /* FIXME: checksum */
520
        update_packetheader(nut, bc, 0);
521
    }
522

    
523
#if 0
524
    /* info header */
525
    put_be64(bc, INFO_STARTCODE);
526
    put_packetheader(nut, bc, 16+strlen(s->author)+strlen(s->title)+
527
        strlen(s->comment)+strlen(s->copyright)); 
528
    if (s->author[0])
529
    {
530
        put_v(bc, 5); /* type */
531
        put_b(bc, s->author, strlen(s->author));
532
    }
533
    if (s->title[0])
534
    {
535
        put_v(bc, 6); /* type */
536
        put_b(bc, s->title, strlen(s->title));
537
    }
538
    if (s->comment[0])
539
    {
540
        put_v(bc, 7); /* type */
541
        put_b(bc, s->comment, strlen(s->comment));
542
    }
543
    if (s->copyright[0])
544
    {
545
        put_v(bc, 8); /* type */
546
        put_b(bc, s->copyright, strlen(s->copyright));
547
    }
548
    /* encoder */
549
    put_v(bc, 9); /* type */
550
    put_b(bc, LIBAVFORMAT_IDENT "\0", strlen(LIBAVFORMAT_IDENT));
551
    
552
    put_v(bc, 0); /* eof info */
553

554
    put_be32(bc, 0); /* FIXME: checksum */
555
    update_packetheader(nut, bc, 0);
556
#endif
557
        
558
    put_flush_packet(bc);
559
    
560
    return 0;
561
}
562

    
563
static int nut_write_packet(AVFormatContext *s, int stream_index, 
564
                            const uint8_t *buf, int size, int64_t pts)
565
{
566
    NUTContext *nut = s->priv_data;
567
    StreamContext *stream= &nut->stream[stream_index];
568
    ByteIOContext *bc = &s->pb;
569
    int key_frame = 0, full_pts=0;
570
    AVCodecContext *enc;
571
    int64_t lsb_pts, delta_pts;
572
    int frame_type, best_length, frame_code, flags, i, size_mul, size_lsb;
573
    const int64_t frame_start= url_ftell(bc);
574

    
575
    if (stream_index > s->nb_streams)
576
        return 1;
577
        
578
    pts= (av_rescale(pts, stream->rate_num, stream->rate_den) + AV_TIME_BASE/2) / AV_TIME_BASE;
579

    
580
    enc = &s->streams[stream_index]->codec;
581
    key_frame = enc->coded_frame->key_frame;
582
    delta_pts= pts - stream->last_pts;
583
    
584
    frame_type=0;
585
    if(frame_start + size + 20 - FFMAX(nut->last_frame_start[1], nut->last_frame_start[2]) > MAX_TYPE1_DISTANCE)
586
        frame_type=1;
587
    if(key_frame){
588
        if(frame_type==1 && frame_start + size - nut->last_frame_start[2] > MAX_TYPE2_DISTANCE)
589
            frame_type=2;
590
        if(!stream->last_key_frame)
591
            frame_type=2;
592
    }
593

    
594
    if(frame_type>0){
595
        update_packetheader(nut, bc, 0);
596
        reset(s);
597
        full_pts=1;
598
    }
599
    //FIXME ensure that the timestamp can be represented by either delta or lsb or full_pts=1
600

    
601
    lsb_pts = pts & ((1 << stream->msb_timestamp_shift)-1);
602

    
603
    best_length=INT_MAX;
604
    frame_code= -1;
605
    for(i=0; i<256; i++){
606
        int stream_id_plus1= nut->frame_code[i].stream_id_plus1;
607
        int fc_key_frame= stream->last_key_frame;
608
        int length=0;
609
        size_mul= nut->frame_code[i].size_mul;
610
        size_lsb= nut->frame_code[i].size_lsb;
611
        flags= nut->frame_code[i].flags;
612

    
613
        if(stream_id_plus1 == 0) length+= get_length(stream_index);
614
        else if(stream_id_plus1 - 1 != stream_index)
615
            continue;
616
        if(flags & FLAG_PRED_KEY_FRAME){
617
            if(flags & FLAG_KEY_FRAME)
618
                fc_key_frame= !fc_key_frame;
619
        }else{
620
            fc_key_frame= !!(flags & FLAG_KEY_FRAME);
621
        }
622
        assert(key_frame==0 || key_frame==1);
623
        if(fc_key_frame != key_frame)
624
            continue;
625

    
626
        if((!!(flags & FLAG_FRAME_TYPE)) != (frame_type > 0))
627
            continue;
628
        
629
        if(size_mul <= size_lsb){
630
            int p= stream->lru_size[size_lsb - size_mul];
631
            if(p != size)
632
                continue;
633
        }else{
634
            if(size % size_mul != size_lsb)
635
                continue;
636
            if(flags & FLAG_DATA_SIZE)
637
                length += get_length(size / size_mul);
638
            else if(size/size_mul)
639
                continue;
640
        }
641

    
642
        if(full_pts != ((flags & FLAG_PTS) && (flags & FLAG_FULL_PTS)))
643
            continue;
644
            
645
        if(flags&FLAG_PTS){
646
            if(flags&FLAG_FULL_PTS){
647
                length += get_length(pts);
648
            }else{
649
                length += get_length(lsb_pts);
650
            }
651
        }else{
652
            int delta= stream->lru_pts_delta[(flags & 12)>>2];
653
            if(delta != pts - stream->last_pts)
654
                continue;
655
            assert(frame_type == 0);
656
        }
657

    
658
        if(length < best_length){
659
            best_length= length;
660
            frame_code=i;
661
        }
662
//    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);
663
    }
664

    
665
    assert(frame_code != -1);
666
    flags= nut->frame_code[frame_code].flags;
667
    size_mul= nut->frame_code[frame_code].size_mul;
668
    size_lsb= nut->frame_code[frame_code].size_lsb;
669
#if 0
670
    best_length /= 7;
671
    best_length ++; //frame_code
672
    if(frame_type>0){
673
        best_length += 4; //packet header
674
        if(frame_type>1)
675
            best_length += 8; // startcode
676
    }
677
    av_log(s, AV_LOG_DEBUG, "kf:%d ft:%d pt:%d fc:%2X len:%2d size:%d stream:%d flag:%d mul:%d lsb:%d s+1:%d pts_delta:%d\n", key_frame, frame_type, full_pts ? 2 : ((flags & FLAG_PTS) ? 1 : 0), frame_code, best_length, size, stream_index, flags, size_mul, size_lsb, nut->frame_code[frame_code].stream_id_plus1,(int)(pts - stream->last_pts));
678
#endif
679

    
680
    if (frame_type==2)
681
        put_be64(bc, KEYFRAME_STARTCODE);
682
    put_byte(bc, frame_code);
683

    
684
    if(frame_type>0)
685
        put_packetheader(nut, bc, FFMAX(size+20, MAX_TYPE1_DISTANCE));
686
    if(nut->frame_code[frame_code].stream_id_plus1 == 0)
687
        put_v(bc, stream_index);
688
    if (flags & FLAG_PTS){
689
        if (flags & FLAG_FULL_PTS)
690
            put_v(bc, pts);
691
        else
692
            put_v(bc, lsb_pts);
693
    }
694
    if(flags & FLAG_DATA_SIZE)
695
        put_v(bc, size / size_mul);
696
    if(size > MAX_TYPE1_DISTANCE){
697
        assert(frame_type > 0);
698
        update_packetheader(nut, bc, size);
699
    }
700
    
701
    put_buffer(bc, buf, size);
702

    
703
    update(nut, stream_index, frame_start, frame_type, frame_code, key_frame, size, pts);
704
    
705
    return 0;
706
}
707

    
708
static int nut_write_trailer(AVFormatContext *s)
709
{
710
    NUTContext *nut = s->priv_data;
711
    ByteIOContext *bc = &s->pb;
712

    
713
    update_packetheader(nut, bc, 0);
714

    
715
#if 0
716
    int i;
717

718
    /* WRITE INDEX */
719

720
    for (i = 0; s->nb_streams; i++)
721
    {
722
        put_be64(bc, INDEX_STARTCODE);
723
        put_packetheader(nut, bc, 64);
724
        put_v(bc, s->streams[i]->id);
725
        put_v(bc, ...);
726
        put_be32(bc, 0); /* FIXME: checksum */
727
        update_packetheader(nut, bc, 0);
728
    }
729
#endif
730

    
731
    put_flush_packet(bc);
732
    
733
    av_freep(&nut->stream);
734

    
735
    return 0;
736
}
737
#endif //CONFIG_ENCODERS
738

    
739
static int nut_probe(AVProbeData *p)
740
{
741
    int i;
742
    uint64_t code;
743

    
744
    code = 0xff;
745
    for (i = 0; i < p->buf_size; i++) {
746
        int c = p->buf[i];
747
        code = (code << 8) | c;
748
        if (code == MAIN_STARTCODE)
749
            return AVPROBE_SCORE_MAX;
750
    }
751
    return 0;
752
}
753

    
754
static int nut_read_header(AVFormatContext *s, AVFormatParameters *ap)
755
{
756
    NUTContext *nut = s->priv_data;
757
    ByteIOContext *bc = &s->pb;
758
    uint64_t tmp;
759
    int cur_stream, nb_streams, i, j;
760

    
761
    nut->avf= s;
762
    
763
    av_set_pts_info(s, 60, 1, AV_TIME_BASE);
764

    
765
    /* main header */
766
    tmp = get_be64(bc);
767
    if (tmp != MAIN_STARTCODE)
768
        av_log(s, AV_LOG_ERROR, "damaged? startcode!=1 (%Ld)\n", tmp);
769
    get_packetheader(nut, bc, 8);
770
    
771
    tmp = get_v(bc);
772
    if (tmp != 1)
773
        av_log(s, AV_LOG_ERROR, "bad version (%Ld)\n", tmp);
774
    
775
    nb_streams = get_v(bc);
776
    get_v(bc); //checksum threshold
777

    
778
    for(i=0; i<256;){
779
        int tmp_flags = get_v(bc);
780
        int tmp_stream= get_v(bc);
781
        int tmp_mul   = get_v(bc);
782
        int tmp_size  = get_v(bc);
783
        int count     = get_v(bc);
784

    
785
        if(count == 0 || i+count > 256){
786
            av_log(s, AV_LOG_ERROR, "illegal count %d at %d\n", count, i);
787
            return -1;
788
        }
789

    
790
        if((tmp_flags & FLAG_FRAME_TYPE) && tmp_flags != 1){
791
            if(tmp_flags & FLAG_PRED_KEY_FRAME){
792
                av_log(s, AV_LOG_ERROR, "keyframe prediction in non 0 frame type\n");
793
                return -1;
794
            }
795
            if(!(tmp_flags & FLAG_PTS) || !(tmp_flags & FLAG_FULL_PTS) ){
796
                av_log(s, AV_LOG_ERROR, "no full pts in non 0 frame type\n");
797
                return -1;
798
            }
799
        }
800

    
801
        for(j=0; j<count; j++,i++){
802
            if(tmp_stream > nb_streams + 1){
803
                av_log(s, AV_LOG_ERROR, "illegal stream number\n");
804
                return -1;
805
            }
806

    
807
            nut->frame_code[i].flags           = tmp_flags ;
808
            nut->frame_code[i].stream_id_plus1 = tmp_stream;
809
            nut->frame_code[i].size_mul        = tmp_mul   ;
810
            nut->frame_code[i].size_lsb        = tmp_size  ;
811
            if(++tmp_size >= tmp_mul){
812
                tmp_size=0;
813
                tmp_stream++;
814
            }
815
        }
816
    }
817
    if(nut->frame_code['N'].flags != 1){
818
        av_log(s, AV_LOG_ERROR, "illegal frame_code table\n");
819
        return -1;
820
    }
821
    
822
    get_be32(bc); /* checkusm */
823
    
824
    s->bit_rate = 0;
825

    
826
    nut->stream = av_malloc(sizeof(StreamContext)*nb_streams);
827
    
828
    /* stream header */
829
    for (cur_stream = 0; cur_stream < nb_streams; cur_stream++)
830
    {
831
        int class, nom, denom;
832
        AVStream *st;
833
        
834
        tmp = get_be64(bc);
835
        if (tmp != STREAM_STARTCODE)
836
            av_log(s, AV_LOG_ERROR, "damaged? startcode!=1 (%Ld)\n", tmp);
837
        get_packetheader(nut, bc, 8);
838
        st = av_new_stream(s, get_v(bc));
839
        if (!st)
840
            return AVERROR_NOMEM;
841
        class = get_v(bc);
842
        tmp = get_bi(bc);
843
        switch(class)
844
        {
845
            case 0:
846
                st->codec.codec_type = CODEC_TYPE_VIDEO;
847
                st->codec.codec_id = codec_get_bmp_id(tmp);
848
                if (st->codec.codec_id == CODEC_ID_NONE)
849
                    av_log(s, AV_LOG_ERROR, "Unknown codec?!\n");
850
                break;
851
            case 32:
852
                st->codec.codec_type = CODEC_TYPE_AUDIO;
853
                st->codec.codec_id = codec_get_wav_id(tmp);
854
                if (st->codec.codec_id == CODEC_ID_NONE)
855
                    av_log(s, AV_LOG_ERROR, "Unknown codec?!\n");
856
                break;
857
            default:
858
                av_log(s, AV_LOG_ERROR, "Unknown stream class (%d)\n", class);
859
                return -1;
860
        }
861
        s->bit_rate += get_v(bc);
862
        get_b(bc, NULL, 0); /* language code */
863
        nom = get_v(bc);
864
        denom = get_v(bc);
865
        nut->stream[cur_stream].msb_timestamp_shift = get_v(bc);
866
        for(i=0; i<3; i++)
867
                nut->stream[cur_stream].initial_pts_predictor[i]= get_v(bc);
868
        for(i=0; i<2; i++)
869
                nut->stream[cur_stream].initial_size_predictor[i]= get_v(bc);
870
        get_byte(bc); /* flags */
871

    
872
        /* codec specific data headers */
873
        while(get_v(bc) != 0){
874
            st->codec.extradata_size= get_v(bc);
875
            st->codec.extradata= av_mallocz(st->codec.extradata_size);
876
            get_buffer(bc, st->codec.extradata, st->codec.extradata_size);            
877
//            url_fskip(bc, get_v(bc));
878
        }
879
        
880
        if (class == 0) /* VIDEO */
881
        {
882
            st->codec.width = get_v(bc);
883
            st->codec.height = get_v(bc);
884
            st->codec.sample_aspect_ratio.num= get_v(bc);
885
            st->codec.sample_aspect_ratio.den= get_v(bc);
886
            get_v(bc); /* csp type */
887

    
888
            st->codec.frame_rate = nom;
889
            st->codec.frame_rate_base = denom;
890
        }
891
        if (class == 32) /* AUDIO */
892
        {
893
            st->codec.sample_rate = (get_v(bc) * nom) / denom;
894
            st->codec.channels = get_v(bc);
895
        }
896
        get_be32(bc); /* checksum */
897
        nut->stream[cur_stream].rate_num= nom;
898
        nut->stream[cur_stream].rate_den= denom;
899
    }    
900
    
901
    return 0;
902
}
903

    
904
static int nut_read_packet(AVFormatContext *s, AVPacket *pkt)
905
{
906
    NUTContext *nut = s->priv_data;
907
    StreamContext *stream;
908
    ByteIOContext *bc = &s->pb;
909
    int size, frame_code, flags, size_mul, size_lsb, stream_id;
910
    int key_frame = 0;
911
    int frame_type= 0;
912
    int64_t pts = 0;
913
    const int64_t frame_start= url_ftell(bc);
914

    
915
    if (url_feof(bc))
916
        return -1;
917
    
918
    frame_code = get_byte(bc);
919
    if(frame_code == 'N'){
920
        uint64_t tmp= frame_code;
921
        tmp<<=8 ; tmp |= get_byte(bc);
922
        tmp<<=16; tmp |= get_be16(bc);
923
        tmp<<=32; tmp |= get_be32(bc);
924
        if (tmp == KEYFRAME_STARTCODE)
925
        {
926
            frame_code = get_byte(bc);
927
            frame_type = 2;
928
        }
929
        else
930
            av_log(s, AV_LOG_ERROR, "error in zero bit / startcode %LX\n", tmp);
931
    }
932
    flags= nut->frame_code[frame_code].flags;
933
    size_mul= nut->frame_code[frame_code].size_mul;
934
    size_lsb= nut->frame_code[frame_code].size_lsb;
935
    stream_id= nut->frame_code[frame_code].stream_id_plus1 - 1;
936

    
937
    if(flags & FLAG_FRAME_TYPE){
938
        reset(s);
939
        if(frame_type==2){
940
            get_packetheader(nut, bc, 8+1);
941
        }else{
942
            get_packetheader(nut, bc, 1);
943
            frame_type= 1;
944
        }
945
    }
946

    
947
    if(stream_id==-1)
948
        stream_id= get_v(bc);
949
    if(stream_id >= s->nb_streams){
950
        av_log(s, AV_LOG_ERROR, "illegal stream_id\n");
951
        return -1;
952
    }
953
    stream= &nut->stream[stream_id];
954
    
955
    if(flags & FLAG_PRED_KEY_FRAME){
956
        if(flags & FLAG_KEY_FRAME)
957
            key_frame= !stream->last_key_frame;
958
        else
959
            key_frame= stream->last_key_frame;
960
    }else{
961
        key_frame= !!(flags & FLAG_KEY_FRAME);
962
    }
963

    
964
    if(flags & FLAG_PTS){
965
        if(flags & FLAG_FULL_PTS){
966
            pts= get_v(bc);
967
        }else{
968
            int64_t mask = (1<<stream->msb_timestamp_shift)-1;
969
            int64_t delta= stream->last_pts - mask/2;
970
            pts= ((get_v(bc) - delta)&mask) + delta;
971
        }
972
    }else{
973
        pts= stream->last_pts + stream->lru_pts_delta[(flags&12)>>2];
974
    }
975
  
976
    if(size_mul <= size_lsb){
977
        size= stream->lru_size[size_lsb - size_mul];
978
    }else{
979
        if(flags & FLAG_DATA_SIZE)
980
            size= size_mul*get_v(bc) + size_lsb;
981
        else
982
            size= size_lsb;
983
    }
984
      
985
//av_log(s, AV_LOG_DEBUG, "fs:%lld fc:%d ft:%d kf:%d pts:%lld\n", frame_start, frame_code, frame_type, key_frame, pts);
986
    
987
    av_new_packet(pkt, size);
988
    get_buffer(bc, pkt->data, size);
989
    pkt->stream_index = stream_id;
990
    if (key_frame)
991
        pkt->flags |= PKT_FLAG_KEY;
992
    pkt->pts = pts * AV_TIME_BASE * stream->rate_den / stream->rate_num;
993

    
994
    update(nut, stream_id, frame_start, frame_type, frame_code, key_frame, size, pts);
995

    
996
    return 0;
997
}
998

    
999
static int nut_read_close(AVFormatContext *s)
1000
{
1001
    NUTContext *nut = s->priv_data;
1002
    int i;
1003

    
1004
    for(i=0;i<s->nb_streams;i++) {
1005
        av_freep(&s->streams[i]->codec.extradata);
1006
    }
1007
    av_freep(&nut->stream);
1008

    
1009
    return 0;
1010
}
1011

    
1012
static AVInputFormat nut_iformat = {
1013
    "nut",
1014
    "nut format",
1015
    sizeof(NUTContext),
1016
    nut_probe,
1017
    nut_read_header,
1018
    nut_read_packet,
1019
    nut_read_close,
1020
//    nut_read_seek,
1021
    .extensions = "nut",
1022
};
1023

    
1024
#ifdef CONFIG_ENCODERS
1025
static AVOutputFormat nut_oformat = {
1026
    "nut",
1027
    "nut format",
1028
    "video/x-nut",
1029
    "nut",
1030
    sizeof(NUTContext),
1031
#if 0 //#ifdef CONFIG_VORBIS
1032
    CODEC_ID_VORBIS,
1033
#elif defined(CONFIG_MP3LAME)
1034
    CODEC_ID_MP3,
1035
#else
1036
    CODEC_ID_MP2, /* AC3 needs liba52 decoder */
1037
#endif
1038
    CODEC_ID_MPEG4,
1039
    nut_write_header,
1040
    nut_write_packet,
1041
    nut_write_trailer,
1042
};
1043
#endif //CONFIG_ENCODERS
1044

    
1045
int nut_init(void)
1046
{
1047
    av_register_input_format(&nut_iformat);
1048
#ifdef CONFIG_ENCODERS
1049
    av_register_output_format(&nut_oformat);
1050
#endif //CONFIG_ENCODERS
1051
    return 0;
1052
}