]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/nut.c
yop: initialize palette to 0
[ffmpeg] / libavformat / nut.c
index 2eec692d293c9dd617ab3552e5bf4cd94121ac43..9267226ac6323ff393ff2165d847aed7dc5d18bc 100644 (file)
 /*
- * NUT (de)muxer based on initial draft
- * Copyright (c) 2003 Alex Beregszaszi
+ * nut
+ * Copyright (c) 2004-2007 Michael Niedermayer
  *
- * This library is free software; you can redistribute it and/or
+ * This file is part of Libav.
+ *
+ * Libav is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
- * version 2 of the License, or (at your option) any later version.
+ * version 2.1 of the License, or (at your option) any later version.
  *
- * This library is distributed in the hope that it will be useful,
+ * Libav is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
- * You should have received a copy of the GNU General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
- *
- * NUT DRAFT can be found in MPlayer CVS at DOCS/tech/mpcf.txt
- *
- * Compatible with draft version 20030906
- *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Libav; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
-/*
- * TODO:
- * - checksumming
- * - correct rate denom/nom and sample_mul
- * - correct timestamp handling
- * - index writing
- * - info and index packet reading support
- * - startcode searching for broken streams
- * - subpacket support
- * - handling of codec specific headers
-*/
-
-//#define DEBUG 1
-
-#include "avformat.h"
-#include "mpegaudio.h"
-
-//from /dev/random
-
-#define     MAIN_STARTCODE (0xF9526A6200000000ULL + ('N'<<24) + ('U'<<16) + ('T'<<8) + 'M')
-#define   STREAM_STARTCODE (0xD667773F00000000ULL + ('N'<<24) + ('U'<<16) + ('T'<<8) + 'S')
-#define KEYFRAME_STARTCODE (0xCB86308700000000ULL + ('N'<<24) + ('U'<<16) + ('T'<<8) + 'K')
-#define    INDEX_STARTCODE (0xEBFCDE0E00000000ULL + ('N'<<24) + ('U'<<16) + ('T'<<8) + 'X')
-#define     INFO_STARTCODE (0xA37B643500000000ULL + ('N'<<24) + ('U'<<16) + ('T'<<8) + 'I')
-
-typedef struct {
-    int curr_frame_start;
-    int last_frame_size;
-    int curr_frame_size;
-} NUTContext;
-
-static int bytes_left(ByteIOContext *bc)
-{
-    return bc->buf_end - bc->buf_ptr;
-}
-
-static uint64_t get_v(ByteIOContext *bc)
-{
-    uint64_t val = 0;
-
-//    for (; bytes_left(s)*8 > 0; )
-    for(; bytes_left(bc) > 0; )
-    {
-       int tmp = get_byte(bc);
-
-       if (tmp&0x80)
-           val= (val<<7) + tmp - 0x80;
-       else
-           return (val<<7) + tmp;
-    }
-    return -1;
-}
-
-static int64_t get_s(ByteIOContext *bc)
-{
-    int64_t v = get_v(bc) + 1;
-
-    if (v&1)
-        return -(v>>1);
-    else
-        return (v>>1);
-}
-
-static int get_b(ByteIOContext *bc, char *data, int maxlen)
-{
-    int i, len;
-    
-    len = get_v(bc);
-    for (i = 0; i < len && i < maxlen; i++)
-       data[i] = get_byte(bc);
-    if (i < len)
-    {
-       len = i;
-       for (i = 0; i < len; i++)
-           get_byte(bc);
-    }
-
-    return 0;
-}
+#include "libavutil/mathematics.h"
+#include "libavutil/tree.h"
+#include "nut.h"
+#include "riff.h"
+#include "internal.h"
+
+const AVCodecTag ff_nut_subtitle_tags[] = {
+    { AV_CODEC_ID_TEXT        , MKTAG('U', 'T', 'F', '8') },
+    { AV_CODEC_ID_SSA         , MKTAG('S', 'S', 'A',  0 ) },
+    { AV_CODEC_ID_DVD_SUBTITLE, MKTAG('D', 'V', 'D', 'S') },
+    { AV_CODEC_ID_DVB_SUBTITLE, MKTAG('D', 'V', 'B', 'S') },
+    { AV_CODEC_ID_NONE        , 0                         }
+};
 
-static int get_packetheader(NUTContext *nut, ByteIOContext *bc)
-{
-    nut->curr_frame_start = url_ftell(bc);
-    nut->curr_frame_size = get_v(bc);
-    nut->last_frame_size = get_v(bc);
-    dprintf("Packet: fwd: %d bwd: %d\n",
-       nut->curr_frame_size, nut->last_frame_size);
-    
-    return 0;
-}
+const AVCodecTag ff_nut_data_tags[] = {
+    { AV_CODEC_ID_TEXT        , MKTAG('U', 'T', 'F', '8') },
+    { AV_CODEC_ID_NONE        , 0                         }
+};
 
-/**
- * 
- */
-static int get_length(uint64_t val){
-    int i;
+const AVCodecTag ff_nut_video_tags[] = {
+    { AV_CODEC_ID_VP9,      MKTAG('V', 'P', '9', '0') },
+    { AV_CODEC_ID_RAWVIDEO, MKTAG('R', 'G', 'B', 15 ) },
+    { AV_CODEC_ID_RAWVIDEO, MKTAG('B', 'G', 'R', 15 ) },
+    { AV_CODEC_ID_RAWVIDEO, MKTAG('R', 'G', 'B', 16 ) },
+    { AV_CODEC_ID_RAWVIDEO, MKTAG('B', 'G', 'R', 16 ) },
+    { AV_CODEC_ID_RAWVIDEO, MKTAG(15 , 'B', 'G', 'R') },
+    { AV_CODEC_ID_RAWVIDEO, MKTAG(15 , 'R', 'G', 'B') },
+    { AV_CODEC_ID_RAWVIDEO, MKTAG(16 , 'B', 'G', 'R') },
+    { AV_CODEC_ID_RAWVIDEO, MKTAG(16 , 'R', 'G', 'B') },
+    { AV_CODEC_ID_RAWVIDEO, MKTAG('R', 'G', 'B', 12 ) },
+    { AV_CODEC_ID_RAWVIDEO, MKTAG('B', 'G', 'R', 12 ) },
+    { AV_CODEC_ID_RAWVIDEO, MKTAG(12 , 'B', 'G', 'R') },
+    { AV_CODEC_ID_RAWVIDEO, MKTAG(12 , 'R', 'G', 'B') },
+    { AV_CODEC_ID_RAWVIDEO, MKTAG('R', 'G', 'B', 'A') },
+    { AV_CODEC_ID_RAWVIDEO, MKTAG('B', 'G', 'R', 'A') },
+    { AV_CODEC_ID_RAWVIDEO, MKTAG('A', 'B', 'G', 'R') },
+    { AV_CODEC_ID_RAWVIDEO, MKTAG('A', 'R', 'G', 'B') },
+    { AV_CODEC_ID_RAWVIDEO, MKTAG('R', 'G', 'B', 24 ) },
+    { AV_CODEC_ID_RAWVIDEO, MKTAG('B', 'G', 'R', 24 ) },
+    { AV_CODEC_ID_RAWVIDEO, MKTAG('4', '1', '1', 'P') },
+    { AV_CODEC_ID_RAWVIDEO, MKTAG('4', '2', '2', 'P') },
+    { AV_CODEC_ID_RAWVIDEO, MKTAG('4', '2', '2', 'P') },
+    { AV_CODEC_ID_RAWVIDEO, MKTAG('4', '4', '0', 'P') },
+    { AV_CODEC_ID_RAWVIDEO, MKTAG('4', '4', '0', 'P') },
+    { AV_CODEC_ID_RAWVIDEO, MKTAG('4', '4', '4', 'P') },
+    { AV_CODEC_ID_RAWVIDEO, MKTAG('4', '4', '4', 'P') },
+    { AV_CODEC_ID_RAWVIDEO, MKTAG('B', '1', 'W', '0') },
+    { AV_CODEC_ID_RAWVIDEO, MKTAG('B', '0', 'W', '1') },
+    { AV_CODEC_ID_RAWVIDEO, MKTAG('B', 'G', 'R',  8 ) },
+    { AV_CODEC_ID_RAWVIDEO, MKTAG('R', 'G', 'B',  8 ) },
+    { AV_CODEC_ID_RAWVIDEO, MKTAG('B', 'G', 'R',  4 ) },
+    { AV_CODEC_ID_RAWVIDEO, MKTAG('R', 'G', 'B',  4 ) },
+    { AV_CODEC_ID_RAWVIDEO, MKTAG('B', '4', 'B', 'Y') },
+    { AV_CODEC_ID_RAWVIDEO, MKTAG('R', '4', 'B', 'Y') },
+    { AV_CODEC_ID_RAWVIDEO, MKTAG('B', 'G', 'R', 48 ) },
+    { AV_CODEC_ID_RAWVIDEO, MKTAG('R', 'G', 'B', 48 ) },
+    { AV_CODEC_ID_RAWVIDEO, MKTAG(48 , 'B', 'G', 'R') },
+    { AV_CODEC_ID_RAWVIDEO, MKTAG(48 , 'R', 'G', 'B') },
+    { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', '3', 11 , 10 ) },
+    { AV_CODEC_ID_RAWVIDEO, MKTAG(10 , 11 , '3', 'Y') },
+    { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', '3', 10 , 10 ) },
+    { AV_CODEC_ID_RAWVIDEO, MKTAG(10 , 10 , '3', 'Y') },
+    { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', '3',  0 , 10 ) },
+    { AV_CODEC_ID_RAWVIDEO, MKTAG(10 ,  0 , '3', 'Y') },
+    { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', '1',  0 , 16 ) },
+    { AV_CODEC_ID_RAWVIDEO, MKTAG(16 ,  0 , '1', 'Y') },
+    { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', '3', 11 , 16 ) },
+    { AV_CODEC_ID_RAWVIDEO, MKTAG(16 , 11 , '3', 'Y') },
+    { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', '3', 10 , 16 ) },
+    { AV_CODEC_ID_RAWVIDEO, MKTAG(16 , 10 , '3', 'Y') },
+    { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', '3',  0 , 16 ) },
+    { AV_CODEC_ID_RAWVIDEO, MKTAG(16 ,  0 , '3', 'Y') },
+    { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', '4', 11 ,  8 ) },
+    { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', '2',  0 ,  8 ) },
+
+    { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', '1',  0 ,  9 ) },
+    { AV_CODEC_ID_RAWVIDEO, MKTAG( 9 ,  0 , '1', 'Y') },
+    { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', '4', 11 ,  9 ) },
+    { AV_CODEC_ID_RAWVIDEO, MKTAG( 9 , 11 , '4', 'Y') },
+    { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', '4', 10 ,  9 ) },
+    { AV_CODEC_ID_RAWVIDEO, MKTAG( 9 , 10 , '4', 'Y') },
+    { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', '4',  0 ,  9 ) },
+    { AV_CODEC_ID_RAWVIDEO, MKTAG( 9 ,  0 , '4', 'Y') },
+
+    { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', '1',  0 , 10 ) },
+    { AV_CODEC_ID_RAWVIDEO, MKTAG(10 ,  0 , '1', 'Y') },
+    { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', '4', 11 , 10 ) },
+    { AV_CODEC_ID_RAWVIDEO, MKTAG(10 , 11 , '4', 'Y') },
+    { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', '4', 10 , 10 ) },
+    { AV_CODEC_ID_RAWVIDEO, MKTAG(10 , 10 , '4', 'Y') },
+    { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', '4',  0 , 10 ) },
+    { AV_CODEC_ID_RAWVIDEO, MKTAG(10 ,  0 , '4', 'Y') },
+
+    { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', '1',  0 , 16 ) },
+    { AV_CODEC_ID_RAWVIDEO, MKTAG(16 ,  0 , '1', 'Y') },
+    { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', '4', 11 , 16 ) },
+    { AV_CODEC_ID_RAWVIDEO, MKTAG(16 , 11 , '4', 'Y') },
+    { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', '4', 10 , 16 ) },
+    { AV_CODEC_ID_RAWVIDEO, MKTAG(16 , 10 , '4', 'Y') },
+    { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', '4',  0 , 16 ) },
+    { AV_CODEC_ID_RAWVIDEO, MKTAG(16 ,  0 , '4', 'Y') },
+
+    { AV_CODEC_ID_NONE    , 0                         }
+};
 
-    for (i=7; ; i+=7)
-       if ((val>>i) == 0)
-           return i;
+const AVCodecTag ff_nut_audio_tags[] = {
+    { AV_CODEC_ID_PCM_ALAW,         MKTAG('A', 'L', 'A', 'W') },
+    { AV_CODEC_ID_PCM_MULAW,        MKTAG('U', 'L', 'A', 'W') },
+    { AV_CODEC_ID_PCM_F32BE,        MKTAG(32 , 'D', 'F', 'P') },
+    { AV_CODEC_ID_PCM_F32LE,        MKTAG('P', 'F', 'D', 32 ) },
+    { AV_CODEC_ID_PCM_F64BE,        MKTAG(64 , 'D', 'F', 'P') },
+    { AV_CODEC_ID_PCM_F64LE,        MKTAG('P', 'F', 'D', 64 ) },
+    { AV_CODEC_ID_PCM_S16BE,        MKTAG(16 , 'D', 'S', 'P') },
+    { AV_CODEC_ID_PCM_S16LE,        MKTAG('P', 'S', 'D', 16 ) },
+    { AV_CODEC_ID_PCM_S24BE,        MKTAG(24 , 'D', 'S', 'P') },
+    { AV_CODEC_ID_PCM_S24LE,        MKTAG('P', 'S', 'D', 24 ) },
+    { AV_CODEC_ID_PCM_S32BE,        MKTAG(32 , 'D', 'S', 'P') },
+    { AV_CODEC_ID_PCM_S32LE,        MKTAG('P', 'S', 'D', 32 ) },
+    { AV_CODEC_ID_PCM_S8,           MKTAG('P', 'S', 'D',  8 ) },
+    { AV_CODEC_ID_PCM_U16BE,        MKTAG(16 , 'D', 'U', 'P') },
+    { AV_CODEC_ID_PCM_U16LE,        MKTAG('P', 'U', 'D', 16 ) },
+    { AV_CODEC_ID_PCM_U24BE,        MKTAG(24 , 'D', 'U', 'P') },
+    { AV_CODEC_ID_PCM_U24LE,        MKTAG('P', 'U', 'D', 24 ) },
+    { AV_CODEC_ID_PCM_U32BE,        MKTAG(32 , 'D', 'U', 'P') },
+    { AV_CODEC_ID_PCM_U32LE,        MKTAG('P', 'U', 'D', 32 ) },
+    { AV_CODEC_ID_PCM_U8,           MKTAG('P', 'U', 'D',  8 ) },
+    { AV_CODEC_ID_PCM_S16LE_PLANAR, MKTAG('P', 'S', 'P', 16 ) },
+    { AV_CODEC_ID_MP3,              MKTAG('M', 'P', '3', ' ') },
+    { AV_CODEC_ID_NONE,             0                         }
+};
 
-    return 7; //not reached
-}
+const AVCodecTag * const ff_nut_codec_tags[] = {
+    ff_nut_video_tags, ff_nut_audio_tags, ff_nut_subtitle_tags,
+    ff_codec_bmp_tags, ff_codec_wav_tags, ff_nut_data_tags, 0
+};
 
-static int put_v(ByteIOContext *bc, uint64_t val)
-{
+void ff_nut_reset_ts(NUTContext *nut, AVRational time_base, int64_t val){
     int i;
-
-//    if (bytes_left(s)*8 < 9)
-//     return -1;
-
-    if (bytes_left(bc) < 1)
-       return -1;
-
-    val &= 0x7FFFFFFFFFFFFFFFULL; // FIXME can only encode upto 63 bits currently
-    i= get_length(val);
-
-    for (i-=7; i>0; i-=7){
-       put_byte(bc, 0x80 | (val>>i));
+    for(i=0; i<nut->avf->nb_streams; i++){
+        nut->stream[i].last_pts= av_rescale_rnd(
+            val,
+            time_base.num * (int64_t)nut->stream[i].time_base->den,
+            time_base.den * (int64_t)nut->stream[i].time_base->num,
+            AV_ROUND_DOWN);
     }
-
-    put_byte(bc, val&0x7f);
-
-    return 0;
 }
 
-static int put_s(ByteIOContext *bc, uint64_t val)
-{
-    if (val<=0)
-       return put_v(bc, -2*val);
-    else
-       return put_v(bc, 2*val-1);
+int64_t ff_lsb2full(StreamContext *stream, int64_t lsb){
+    int64_t mask = (1<<stream->msb_pts_shift)-1;
+    int64_t delta= stream->last_pts - mask/2;
+    return  ((lsb - delta)&mask) + delta;
 }
 
-static int put_b(ByteIOContext *bc, char *data, int len)
-{
-    int i;
-    
-    put_v(bc, len);
-    for (i = 0; i < len; i++)
-       put_byte(bc, data[i]);
-
-    return 0;
+int ff_nut_sp_pos_cmp(const Syncpoint *a, const Syncpoint *b){
+    return ((a->pos - b->pos) >> 32) - ((b->pos - a->pos) >> 32);
 }
 
-static int put_packetheader(NUTContext *nut, ByteIOContext *bc, int max_size)
-{
-    put_flush_packet(bc);
-    nut->curr_frame_start = url_ftell(bc);
-    nut->curr_frame_size = max_size;
-    
-    /* packet header */
-    put_v(bc, nut->curr_frame_size); /* forward ptr */
-    put_v(bc, nut->last_frame_size); /* backward ptr */
-    dprintf("Packet: fwd: %d, bwd: %d\n",
-       nut->curr_frame_size, nut->last_frame_size);
-
-    nut->last_frame_size = nut->curr_frame_size;
-    
-    return 0;
-}
-
-static int update_packetheader(NUTContext *nut, ByteIOContext *bc, int additional_size){
-    offset_t start= nut->curr_frame_start;
-    offset_t cur= url_ftell(bc);
-    int size= cur - start + additional_size;
-    
-    assert( size <= nut->curr_frame_size );
-    
-    url_fseek(bc, start, SEEK_SET);
-    put_v(bc, size);
-    if(get_length(size) < get_length(nut->curr_frame_size))
-        put_byte(bc, 0x80);
-    nut->curr_frame_size= size;
-    dprintf("Packet update: size: %d\n", size);
-
-    url_fseek(bc, cur, SEEK_SET);    
-    
-    return 0;
-}
-
-static int nut_write_header(AVFormatContext *s)
-{
-    NUTContext *nut = s->priv_data;
-    ByteIOContext *bc = &s->pb;
-    AVCodecContext *codec;
-    int i;
-    int stream_length = 0;
-
-    for (i = 0; i < s->nb_streams; i++)
-    {
-       if (stream_length < (s->streams[i]->duration * (AV_TIME_BASE / 1000)))
-           stream_length = s->streams[i]->duration * (AV_TIME_BASE / 1000);
-    }
-
-    /* main header */
-    put_be64(bc, MAIN_STARTCODE);
-    put_packetheader(nut, bc, 120);
-    put_v(bc, 0); /* version */
-    put_v(bc, s->nb_streams);
-    put_v(bc, 0); /* file size */
-    put_v(bc, stream_length); /* len in msec */
-    put_be32(bc, 0); /* FIXME: checksum */
-    
-    update_packetheader(nut, bc, 0);
-    
-    /* stream headers */
-    for (i = 0; i < s->nb_streams; i++)
-    {
-       codec = &s->streams[i]->codec;
-       
-       put_be64(bc, STREAM_STARTCODE);
-       put_packetheader(nut, bc, 120);
-       put_v(bc, i /*s->streams[i]->index*/);
-       put_v(bc, (codec->codec_type == CODEC_TYPE_AUDIO) ? 32 : 0);
-       if (codec->codec_tag)
-           put_b(bc, &codec->codec_tag, 4);
-       else if (codec->codec_type == CODEC_TYPE_VIDEO)
-       {
-           int tmp = codec_get_bmp_tag(codec->codec_id);
-           put_b(bc, &tmp, 4);
-//         put_v(bc, 4); /* len */
-//         put_be32(bc, codec_get_bmp_tag(codec->codec_id));
-       }
-       else if (codec->codec_type == CODEC_TYPE_AUDIO)
-       {
-           int tmp = codec_get_wav_tag(codec->codec_id);
-           put_b(bc, &tmp, 4);
-//         put_v(bc, 4); /* len */
-//         put_be32(bc, codec_get_wav_tag(codec->codec_id));
-       }
-       put_v(bc, codec->bit_rate);
-       put_v(bc, 0); /* no language code */
-       put_v(bc, codec->frame_rate_base);
-       put_v(bc, codec->frame_rate);
-       put_v(bc, 0); /* timestamp_shift */
-       put_v(bc, 0); /* shuffle type */
-       put_byte(bc, 0); /* flags: 0x1 - fixed_fps, 0x2 - index_present */
-       
-       put_v(bc, 0); /* no codec specific headers */
-       
-       switch(codec->codec_type)
-       {
-           case CODEC_TYPE_AUDIO:
-               put_v(bc, codec->sample_rate / (double)(codec->frame_rate_base / codec->frame_rate));
-               put_v(bc, codec->channels);
-               put_be32(bc, 0); /* FIXME: checksum */
-               break;
-           case CODEC_TYPE_VIDEO:
-               put_v(bc, codec->width);
-               put_v(bc, codec->height);
-               put_v(bc, 0); /* aspected w */
-               put_v(bc, 0); /* aspected h */
-               put_v(bc, 0); /* csp type -- unknown */
-               put_be32(bc, 0); /* FIXME: checksum */
-               break;
-       }
-        update_packetheader(nut, bc, 0);
-    }
-
-#if 0
-    /* info header */
-    put_be64(bc, INFO_STARTCODE);
-    put_packetheader(nut, bc, 16+strlen(s->author)+strlen(s->title)+
-        strlen(s->comment)+strlen(s->copyright)); 
-    if (s->author[0])
-    {
-        put_v(bc, 5); /* type */
-        put_b(bc, s->author, strlen(s->author));
-    }
-    if (s->title[0])
-    {
-        put_v(bc, 6); /* type */
-        put_b(bc, s->title, strlen(s->title));
-    }
-    if (s->comment[0])
-    {
-        put_v(bc, 7); /* type */
-        put_b(bc, s->comment, strlen(s->comment));
-    }
-    if (s->copyright[0])
-    {
-        put_v(bc, 8); /* type */
-        put_b(bc, s->copyright, strlen(s->copyright));
-    }
-    /* encoder */
-    put_v(bc, 9); /* type */
-    put_b(bc, LIBAVFORMAT_IDENT "\0", strlen(LIBAVFORMAT_IDENT));
-    
-    put_v(bc, 0); /* eof info */
-
-    put_be32(bc, 0); /* FIXME: checksum */
-    update_packetheader(nut, bc, 0);
-#endif
-        
-    put_flush_packet(bc);
-    
-    return 0;
-}
-
-static int nut_write_packet(AVFormatContext *s, int stream_index, 
-                           uint8_t *buf, int size, int force_pts)
-{
-    NUTContext *nut = s->priv_data;
-    ByteIOContext *bc = &s->pb;
-    int key_frame = 0;
-    int flags, size2;
-    AVCodecContext *enc;
-
-    if (stream_index > s->nb_streams)
-       return 1;
-
-    enc = &s->streams[stream_index]->codec;
-    if (enc->codec_type == CODEC_TYPE_VIDEO)
-       key_frame = enc->coded_frame->key_frame;
-
-    if (key_frame)
-       put_be64(bc, KEYFRAME_STARTCODE);
-    
-    flags=0;
-    flags<<=2; flags|=1; //priority
-    flags<<=1; flags|=0; //checksum
-    flags<<=1; flags|=0; //msb_timestamp_flag
-    flags<<=2; flags|=1; //subpacket_type
-    flags<<=1; flags|=0; //reserved
-
-    put_byte(bc, flags);
-
-    put_packetheader(nut, bc, size+20);
-    put_v(bc, stream_index);
-    put_s(bc, force_pts); /* lsb_timestamp */
-    update_packetheader(nut, bc, size);
-    
-    put_buffer(bc, buf, size);
-    
-    put_flush_packet(bc);
-
-    return 0;
+int ff_nut_sp_pts_cmp(const Syncpoint *a, const Syncpoint *b){
+    return ((a->ts - b->ts) >> 32) - ((b->ts - a->ts) >> 32);
 }
 
-static int nut_write_trailer(AVFormatContext *s)
-{
-    NUTContext *nut = s->priv_data;
-    ByteIOContext *bc = &s->pb;
-#if 0
-    int i;
-
-    /* WRITE INDEX */
+void ff_nut_add_sp(NUTContext *nut, int64_t pos, int64_t back_ptr, int64_t ts){
+    Syncpoint *sp= av_mallocz(sizeof(Syncpoint));
+    struct AVTreeNode *node = av_tree_node_alloc();
 
-    for (i = 0; s->nb_streams; i++)
-    {
-       put_be64(bc, INDEX_STARTCODE);
-       put_packetheader(nut, bc, 64);
-       put_v(bc, s->streams[i]->id);
-       put_v(bc, ...);
-       put_be32(bc, 0); /* FIXME: checksum */
-        update_packetheader(nut, bc, 0);
+    sp->pos= pos;
+    sp->back_ptr= back_ptr;
+    sp->ts= ts;
+    av_tree_insert(&nut->syncpoints, sp, (void *) ff_nut_sp_pos_cmp, &node);
+    if(node){
+        av_free(sp);
+        av_free(node);
     }
-#endif
-
-    put_flush_packet(bc);
-
-    return 0;
 }
 
-static int nut_probe(AVProbeData *p)
+static int enu_free(void *opaque, void *elem)
 {
-    int i;
-    uint64_t code;
-
-    code = 0xff;
-    for (i = 0; i < p->buf_size; i++) {
-        int c = p->buf[i];
-        code = (code << 8) | c;
-        code &= 0xFFFFFFFFFFFFFFFFULL;
-        if (code == MAIN_STARTCODE)
-            return AVPROBE_SCORE_MAX;
-    }
+    av_free(elem);
     return 0;
 }
 
-static int nut_read_header(AVFormatContext *s, AVFormatParameters *ap)
+void ff_nut_free_sp(NUTContext *nut)
 {
-    NUTContext *nut = s->priv_data;
-    ByteIOContext *bc = &s->pb;
-    uint64_t tmp;
-    int cur_stream, nb_streams;
-    
-    /* main header */
-    tmp = get_be64(bc);
-    if (tmp != MAIN_STARTCODE)
-       fprintf(stderr, "damaged? startcode!=1 (%Ld)\n", tmp);
-    get_packetheader(nut, bc);
-    
-    tmp = get_v(bc);
-    if (tmp != 0)
-       fprintf(stderr, "bad version (%Ld)\n", tmp);
-    
-    nb_streams = get_v(bc);
-    
-    s->file_size = get_v(bc);
-    s->duration = get_v(bc) / (AV_TIME_BASE / 1000);
-
-    get_be32(bc); /* checkusm */
-    
-    s->bit_rate = 0;
-    
-    /* stream header */
-    for (cur_stream = 0; cur_stream < nb_streams; cur_stream++)
-    {
-       int class;
-       AVStream *st;
-       
-       tmp = get_be64(bc);
-       if (tmp != STREAM_STARTCODE)
-           fprintf(stderr, "damaged? startcode!=1 (%Ld)\n", tmp);
-       get_packetheader(nut, bc);
-       st = av_new_stream(s, get_v(bc));
-       if (!st)
-           return AVERROR_NOMEM;
-       class = get_v(bc);
-       switch(class)
-       {
-           case 0:
-               st->codec.codec_type = CODEC_TYPE_VIDEO;
-//             get_v(bc);
-//             tmp = get_be32(bc);
-               get_b(bc, (char*)&tmp, 4);
-               st->codec.codec_id = codec_get_bmp_id(tmp);
-               if (st->codec.codec_id == CODEC_ID_NONE)
-                   fprintf(stderr, "Unknown codec?!\n");
-               break;
-           case 32:
-               st->codec.codec_type = CODEC_TYPE_AUDIO;
-//             tmp = get_v(bc);
-//             tmp = get_be32(bc);
-               get_b(bc, (char*)&tmp, 4);
-               st->codec.codec_id = codec_get_wav_id(tmp);
-               if (st->codec.codec_id == CODEC_ID_NONE)
-                   fprintf(stderr, "Unknown codec?!\n");
-               break;
-           default:
-               fprintf(stderr, "Unknown stream class (%d)\n", class);
-               return -1;
-       }
-       s->bit_rate += get_v(bc);
-       tmp = get_v(bc); /* language code */
-       while(tmp--)
-           get_byte(bc);
-       st->codec.frame_rate_base = get_v(bc);
-       st->codec.frame_rate = get_v(bc);
-       get_v(bc); /* FIXME: msb timestamp base */
-       get_v(bc); /* shuffle type */
-       get_byte(bc); /* flags */
-       
-       get_v(bc); /* FIXME: codec specific data headers */
-       
-       if (class == 0) /* VIDEO */
-       {
-           st->codec.width = get_v(bc);
-           st->codec.height = get_v(bc);
-           get_v(bc); /* aspected w */
-           get_v(bc); /* aspected h */
-           get_v(bc); /* csp type */
-           get_le32(bc); /* checksum */
-       }
-       if (class == 32) /* AUDIO */
-       {
-           st->codec.sample_rate = get_v(bc) * (double)(st->codec.frame_rate_base / st->codec.frame_rate);
-           st->codec.channels = get_v(bc);
-           get_le32(bc); /* checksum */
-       }
-    }    
-    
-    return 0;
+    av_tree_enumerate(nut->syncpoints, NULL, NULL, enu_free);
+    av_tree_destroy(nut->syncpoints);
 }
 
-static int nut_read_packet(AVFormatContext *s, AVPacket *pkt)
-{
-    NUTContext *nut = s->priv_data;
-    ByteIOContext *bc = &s->pb;
-    int id, timestamp, size;
-    int key_frame = 0;
-    uint64_t tmp;
-
-
-    if (url_feof(bc))
-       return -1;
-    
-    tmp = get_byte(bc);
-    if (tmp & 0x80) /* zero bit set? */
-    {
-       tmp<<=8 ; tmp |= get_byte(bc);
-       tmp<<=16; tmp |= get_be16(bc);
-       tmp<<=32; tmp |= get_be32(bc);
-       if (tmp == KEYFRAME_STARTCODE)
-       {
-           key_frame = 1;
-           tmp = get_byte(bc); /* flags */
-       }
-       else
-           fprintf(stderr, "error in zero bit / startcode %LX\n", tmp);
-    }
-    get_packetheader(nut, bc);
-#if 0
-    if (((tmp & 0x60)>>5) > 3) /* priority <= 3 */
-       fprintf(stderr, "sanity check failed!\n");
-#endif
-    id = get_v(bc);
-    timestamp = get_s(bc);
-    
-    size = (nut->curr_frame_size - (url_ftell(bc)-nut->curr_frame_start));
-    dprintf("flags: 0x%Lx, timestamp: %d, packet size: %d\n", tmp, timestamp, size);
-    
-    if (size < 0)
-       return -1;
-
-    av_new_packet(pkt, size);
-    get_buffer(bc, pkt->data, size);
-    pkt->stream_index = id;
-    if (key_frame)
-       pkt->flags |= PKT_FLAG_KEY;
-    pkt->pts = timestamp;
-
-    return 0;
-}
-
-static AVInputFormat nut_iformat = {
-    "nut",
-    "nut format",
-    sizeof(NUTContext),
-    nut_probe,
-    nut_read_header,
-    nut_read_packet,
-//    nut_read_close,
-//    nut_read_seek,
-    .extensions = "nut",
+const Dispositions ff_nut_dispositions[] = {
+    {"default"     , AV_DISPOSITION_DEFAULT},
+    {"dub"         , AV_DISPOSITION_DUB},
+    {"original"    , AV_DISPOSITION_ORIGINAL},
+    {"comment"     , AV_DISPOSITION_COMMENT},
+    {"lyrics"      , AV_DISPOSITION_LYRICS},
+    {"karaoke"     , AV_DISPOSITION_KARAOKE},
+    {""            , 0}
 };
 
-static AVOutputFormat nut_oformat = {
-    "nut",
-    "nut format",
-    "video/x-nut",
-    "nut",
-    sizeof(NUTContext),
-#ifdef CONFIG_VORBIS
-    CODEC_ID_VORBIS,
-#elif defined(CONFIG_MP3LAME)
-    CODEC_ID_MP3LAME,
-#else
-    CODEC_ID_MP2, /* AC3 needs liba52 decoder */
-#endif
-    CODEC_ID_MPEG4,
-    nut_write_header,
-    nut_write_packet,
-    nut_write_trailer,
+const AVMetadataConv ff_nut_metadata_conv[] = {
+    { "Author",         "artist"      },
+    { "X-CreationTime", "date"        },
+    { "CreationTime",   "date"        },
+    { "SourceFilename", "filename"    },
+    { "X-Language",     "language"    },
+    { "X-Disposition",  "disposition" },
+    { "X-Replaces",     "replaces"    },
+    { "X-Depends",      "depends"     },
+    { "X-Uses",         "uses"        },
+    { "X-UsesFont",     "usesfont"    },
+    { 0 },
 };
-
-int nut_init(void)
-{
-    av_register_input_format(&nut_iformat);
-    av_register_output_format(&nut_oformat);
-    return 0;
-}