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