2 * "NUT" Container Format muxer and demuxer (DRAFT-200403??)
3 * Copyright (c) 2003 Alex Beregszaszi
4 * Copyright (c) 2004 Michael Niedermayer
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.
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.
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
20 * NUT DRAFT can be found in MPlayer CVS at DOCS/tech/mpcf.txt
22 * AND http://people.fsn.hu/~alex/nut/ (TeX, pdf, ps, dvi, ..)
31 * - info and index packet reading support
32 * - startcode searching for broken streams
39 #include "mpegaudio.h"
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))
53 #define MAX_TYPE1_DISTANCE (1024*16-1)
54 #define MAX_TYPE2_DISTANCE (1024*64-1)
56 #define MAX_SIZE_LRU 2
59 #define FLAG_FRAME_TYPE 1
60 #define FLAG_DATA_SIZE 2
62 #define FLAG_FULL_PTS 4
63 #define FLAG_KEY_FRAME 32
64 #define FLAG_PRED_KEY_FRAME 64
68 uint8_t stream_id_plus1;
75 int msb_timestamp_shift;
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];
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;
97 static void update_lru(int *lru, int current, int count){
100 for(i=0; i<count-1; i++){
101 if(lru[i] == current)
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];
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;
125 static void reset(AVFormatContext *s/*, int frame_type*/){
126 NUTContext *nut = s->priv_data;
129 for(i=0; i<s->nb_streams; i++){
130 StreamContext *stream= &nut->stream[i];
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);
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;
143 int keyframe_0_esc= s->nb_streams > 2;
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;
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;
167 for(key_frame=0; key_frame<2; key_frame++){
168 if(intra_only && keyframe_0_esc && key_frame==0)
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;
183 key_frame= intra_only;
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;
193 ft->size_lsb=1 + pred;
196 nut->stream[stream_id].initial_size_predictor[pred]= frame_bytes + pred;
199 FrameCode *ft= &nut->frame_code[start2];
200 ft->flags= FLAG_KEY_FRAME | FLAG_DATA_SIZE;
201 ft->stream_id_plus1= stream_id + 1;
206 pred_count= 2 + codec->has_b_frames;
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;
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;
220 nut->stream[stream_id].initial_pts_predictor[pred]= pred+1;
223 memmove(&nut->frame_code['N'+1], &nut->frame_code['N'], sizeof(FrameCode)*(255-'N'));
224 nut->frame_code['N'].flags= 1;
227 static int bytes_left(ByteIOContext *bc)
229 return bc->buf_end - bc->buf_ptr;
232 static uint64_t get_v(ByteIOContext *bc)
236 for(; bytes_left(bc) > 0; )
238 int tmp = get_byte(bc);
241 val= (val<<7) + tmp - 0x80;
243 return (val<<7) + tmp;
248 static int get_b(ByteIOContext *bc, char *data, int maxlen)
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);
261 static int get_bi(ByteIOContext *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);
274 static int get_packetheader(NUTContext *nut, ByteIOContext *bc, int prefix_length)
276 int64_t start, size, last_size;
277 start= url_ftell(bc) - prefix_length;
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");
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);
291 nut->last_packet_start = nut->packet_start;
292 nut->packet_start = start;
293 nut->written_packet_size= size;
301 static int get_length(uint64_t val){
308 return 7; //not reached
311 #ifdef CONFIG_ENCODERS
312 static int put_v(ByteIOContext *bc, uint64_t val)
315 // if (bytes_left(s)*8 < 9)
318 if (bytes_left(bc) < 1)
321 val &= 0x7FFFFFFFFFFFFFFFULL; // FIXME can only encode upto 63 bits currently
324 for (i-=7; i>0; i-=7){
325 put_byte(bc, 0x80 | (val>>i));
328 put_byte(bc, val&0x7f);
333 static int put_b(ByteIOContext *bc, char *data, int len)
338 for (i = 0; i < len; i++)
339 put_byte(bc, data[i]);
344 static int put_bi(ByteIOContext *bc, int val)
351 static int put_packetheader(NUTContext *nut, ByteIOContext *bc, int max_size)
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;
360 put_v(bc, nut->written_packet_size); /* forward ptr */
361 put_v(bc, nut->packet_start - nut->last_packet_start); /* backward ptr */
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;
371 if(size != nut->written_packet_size){
374 assert( size <= nut->written_packet_size );
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)
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
388 static int nut_write_header(AVFormatContext *s)
390 NUTContext *nut = s->priv_data;
391 ByteIOContext *bc = &s->pb;
392 AVCodecContext *codec;
398 av_mallocz(sizeof(StreamContext)*s->nb_streams);
400 av_set_pts_info(s, 60, 1, AV_TIME_BASE);
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 */
410 assert(nut->frame_code['N'].flags == 1);
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);
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){
434 put_be32(bc, 0); /* FIXME: checksum */
436 update_packetheader(nut, bc, 0);
439 for (i = 0; i < s->nb_streams; i++)
443 codec = &s->streams[i]->codec;
445 put_be64(bc, STREAM_STARTCODE);
446 put_packetheader(nut, bc, 120);
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)
453 int tmp = codec_get_bmp_tag(codec->codec_id);
456 else if (codec->codec_type == CODEC_TYPE_AUDIO)
458 int tmp = codec_get_wav_tag(codec->codec_id);
462 if (codec->codec_type == CODEC_TYPE_VIDEO)
464 nom = codec->frame_rate;
465 denom = codec->frame_rate_base;
469 nom = codec->sample_rate;
470 if(codec->frame_size>0)
471 denom= codec->frame_size;
475 gcd= ff_gcd(nom, denom);
478 nut->stream[i].rate_num= nom;
479 nut->stream[i].rate_den= denom;
481 put_v(bc, codec->bit_rate);
482 put_v(bc, 0); /* no language code */
485 if(nom / denom < 1000)
486 nut->stream[i].msb_timestamp_shift = 7;
488 nut->stream[i].msb_timestamp_shift = 14;
489 put_v(bc, nut->stream[i].msb_timestamp_shift);
491 put_v(bc, nut->stream[i].initial_pts_predictor[j]);
493 put_v(bc, nut->stream[i].initial_size_predictor[j]);
494 put_byte(bc, 0); /* flags: 0x1 - fixed_fps, 0x2 - index_present */
496 if(codec->extradata_size){
498 put_v(bc, codec->extradata_size);
499 put_buffer(bc, codec->extradata, codec->extradata_size);
501 put_v(bc, 0); /* end of codec specific headers */
503 switch(codec->codec_type)
505 case CODEC_TYPE_AUDIO:
506 put_v(bc, (codec->sample_rate * denom) / nom);
507 put_v(bc, codec->channels);
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 */
519 put_be32(bc, 0); /* FIXME: checksum */
520 update_packetheader(nut, bc, 0);
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));
530 put_v(bc, 5); /* type */
531 put_b(bc, s->author, strlen(s->author));
535 put_v(bc, 6); /* type */
536 put_b(bc, s->title, strlen(s->title));
540 put_v(bc, 7); /* type */
541 put_b(bc, s->comment, strlen(s->comment));
545 put_v(bc, 8); /* type */
546 put_b(bc, s->copyright, strlen(s->copyright));
549 put_v(bc, 9); /* type */
550 put_b(bc, LIBAVFORMAT_IDENT "\0", strlen(LIBAVFORMAT_IDENT));
552 put_v(bc, 0); /* eof info */
554 put_be32(bc, 0); /* FIXME: checksum */
555 update_packetheader(nut, bc, 0);
558 put_flush_packet(bc);
563 static int nut_write_packet(AVFormatContext *s, int stream_index,
564 const uint8_t *buf, int size, int64_t pts)
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;
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);
575 if (stream_index > s->nb_streams)
578 pts= (av_rescale(pts, stream->rate_num, stream->rate_den) + AV_TIME_BASE/2) / AV_TIME_BASE;
580 enc = &s->streams[stream_index]->codec;
581 key_frame = enc->coded_frame->key_frame;
582 delta_pts= pts - stream->last_pts;
585 if(frame_start + size + 20 - FFMAX(nut->last_frame_start[1], nut->last_frame_start[2]) > MAX_TYPE1_DISTANCE)
588 if(frame_type==1 && frame_start + size - nut->last_frame_start[2] > MAX_TYPE2_DISTANCE)
590 if(!stream->last_key_frame)
595 update_packetheader(nut, bc, 0);
599 //FIXME ensure that the timestamp can be represented by either delta or lsb or full_pts=1
601 lsb_pts = pts & ((1 << stream->msb_timestamp_shift)-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;
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;
613 if(stream_id_plus1 == 0) length+= get_length(stream_index);
614 else if(stream_id_plus1 - 1 != stream_index)
616 if(flags & FLAG_PRED_KEY_FRAME){
617 if(flags & FLAG_KEY_FRAME)
618 fc_key_frame= !fc_key_frame;
620 fc_key_frame= !!(flags & FLAG_KEY_FRAME);
622 assert(key_frame==0 || key_frame==1);
623 if(fc_key_frame != key_frame)
626 if((!!(flags & FLAG_FRAME_TYPE)) != (frame_type > 0))
629 if(size_mul <= size_lsb){
630 int p= stream->lru_size[size_lsb - size_mul];
634 if(size % size_mul != size_lsb)
636 if(flags & FLAG_DATA_SIZE)
637 length += get_length(size / size_mul);
638 else if(size/size_mul)
642 if(full_pts != ((flags & FLAG_PTS) && (flags & FLAG_FULL_PTS)))
646 if(flags&FLAG_FULL_PTS){
647 length += get_length(pts);
649 length += get_length(lsb_pts);
652 int delta= stream->lru_pts_delta[(flags & 12)>>2];
653 if(delta != pts - stream->last_pts)
655 assert(frame_type == 0);
658 if(length < best_length){
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);
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;
671 best_length ++; //frame_code
673 best_length += 4; //packet header
675 best_length += 8; // startcode
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));
681 put_be64(bc, KEYFRAME_STARTCODE);
682 put_byte(bc, frame_code);
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)
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);
701 put_buffer(bc, buf, size);
703 update(nut, stream_index, frame_start, frame_type, frame_code, key_frame, size, pts);
708 static int nut_write_trailer(AVFormatContext *s)
710 NUTContext *nut = s->priv_data;
711 ByteIOContext *bc = &s->pb;
713 update_packetheader(nut, bc, 0);
720 for (i = 0; s->nb_streams; i++)
722 put_be64(bc, INDEX_STARTCODE);
723 put_packetheader(nut, bc, 64);
724 put_v(bc, s->streams[i]->id);
726 put_be32(bc, 0); /* FIXME: checksum */
727 update_packetheader(nut, bc, 0);
731 put_flush_packet(bc);
733 av_freep(&nut->stream);
737 #endif //CONFIG_ENCODERS
739 static int nut_probe(AVProbeData *p)
745 for (i = 0; i < p->buf_size; i++) {
747 code = (code << 8) | c;
748 if (code == MAIN_STARTCODE)
749 return AVPROBE_SCORE_MAX;
754 static int nut_read_header(AVFormatContext *s, AVFormatParameters *ap)
756 NUTContext *nut = s->priv_data;
757 ByteIOContext *bc = &s->pb;
759 int cur_stream, nb_streams, i, j;
763 av_set_pts_info(s, 60, 1, AV_TIME_BASE);
767 if (tmp != MAIN_STARTCODE)
768 av_log(s, AV_LOG_ERROR, "damaged? startcode!=1 (%Ld)\n", tmp);
769 get_packetheader(nut, bc, 8);
773 av_log(s, AV_LOG_ERROR, "bad version (%Ld)\n", tmp);
775 nb_streams = get_v(bc);
776 get_v(bc); //checksum threshold
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);
785 if(count == 0 || i+count > 256){
786 av_log(s, AV_LOG_ERROR, "illegal count %d at %d\n", count, i);
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");
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");
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");
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){
817 if(nut->frame_code['N'].flags != 1){
818 av_log(s, AV_LOG_ERROR, "illegal frame_code table\n");
822 get_be32(bc); /* checkusm */
826 nut->stream = av_malloc(sizeof(StreamContext)*nb_streams);
829 for (cur_stream = 0; cur_stream < nb_streams; cur_stream++)
831 int class, nom, denom;
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));
840 return AVERROR_NOMEM;
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");
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");
858 av_log(s, AV_LOG_ERROR, "Unknown stream class (%d)\n", class);
861 s->bit_rate += get_v(bc);
862 get_b(bc, NULL, 0); /* language code */
865 nut->stream[cur_stream].msb_timestamp_shift = get_v(bc);
867 nut->stream[cur_stream].initial_pts_predictor[i]= get_v(bc);
869 nut->stream[cur_stream].initial_size_predictor[i]= get_v(bc);
870 get_byte(bc); /* flags */
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));
880 if (class == 0) /* VIDEO */
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 */
888 st->codec.frame_rate = nom;
889 st->codec.frame_rate_base = denom;
891 if (class == 32) /* AUDIO */
893 st->codec.sample_rate = (get_v(bc) * nom) / denom;
894 st->codec.channels = get_v(bc);
896 get_be32(bc); /* checksum */
897 nut->stream[cur_stream].rate_num= nom;
898 nut->stream[cur_stream].rate_den= denom;
904 static int nut_read_packet(AVFormatContext *s, AVPacket *pkt)
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;
913 const int64_t frame_start= url_ftell(bc);
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)
926 frame_code = get_byte(bc);
930 av_log(s, AV_LOG_ERROR, "error in zero bit / startcode %LX\n", tmp);
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;
937 if(flags & FLAG_FRAME_TYPE){
940 get_packetheader(nut, bc, 8+1);
942 get_packetheader(nut, bc, 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");
953 stream= &nut->stream[stream_id];
955 if(flags & FLAG_PRED_KEY_FRAME){
956 if(flags & FLAG_KEY_FRAME)
957 key_frame= !stream->last_key_frame;
959 key_frame= stream->last_key_frame;
961 key_frame= !!(flags & FLAG_KEY_FRAME);
964 if(flags & FLAG_PTS){
965 if(flags & FLAG_FULL_PTS){
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;
973 pts= stream->last_pts + stream->lru_pts_delta[(flags&12)>>2];
976 if(size_mul <= size_lsb){
977 size= stream->lru_size[size_lsb - size_mul];
979 if(flags & FLAG_DATA_SIZE)
980 size= size_mul*get_v(bc) + size_lsb;
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);
987 av_new_packet(pkt, size);
988 get_buffer(bc, pkt->data, size);
989 pkt->stream_index = stream_id;
991 pkt->flags |= PKT_FLAG_KEY;
992 pkt->pts = pts * AV_TIME_BASE * stream->rate_den / stream->rate_num;
994 update(nut, stream_id, frame_start, frame_type, frame_code, key_frame, size, pts);
999 static int nut_read_close(AVFormatContext *s)
1001 NUTContext *nut = s->priv_data;
1004 for(i=0;i<s->nb_streams;i++) {
1005 av_freep(&s->streams[i]->codec.extradata);
1007 av_freep(&nut->stream);
1012 static AVInputFormat nut_iformat = {
1021 .extensions = "nut",
1024 #ifdef CONFIG_ENCODERS
1025 static AVOutputFormat nut_oformat = {
1031 #if 0 //#ifdef CONFIG_VORBIS
1033 #elif defined(CONFIG_MP3LAME)
1036 CODEC_ID_MP2, /* AC3 needs liba52 decoder */
1043 #endif //CONFIG_ENCODERS
1047 av_register_input_format(&nut_iformat);
1048 #ifdef CONFIG_ENCODERS
1049 av_register_output_format(&nut_oformat);
1050 #endif //CONFIG_ENCODERS