]> git.sesse.net Git - ffmpeg/blobdiff - libav/ffm.c
http protocol now uses tcp: protocol (simpler)
[ffmpeg] / libav / ffm.c
index 80f41ff0f0379f6e2d429096ef9684b3839cb252..1d1564a6399df54fd3452245012519bdfcd7f24a 100644 (file)
@@ -1,20 +1,20 @@
 /*
  * FFM (ffserver live feed) encoder and decoder
- * Copyright (c) 2001 Gerard Lantau.
+ * Copyright (c) 2001 Fabrice Bellard.
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
+ * This library 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.
  *
- * This program is distributed in the hope that it will be useful,
+ * This library 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 General Public License for more details.
+ * 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 program; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ * You should have received a copy of the GNU Lesser 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
  */
 #include "avformat.h"
 #include <unistd.h>
@@ -24,7 +24,7 @@
 #define PACKET_ID       0x666d
 
 /* each packet contains frames (which can span several packets */
-#define FRAME_HEADER_SIZE    5
+#define FRAME_HEADER_SIZE    8
 #define FLAG_KEY_FRAME       0x01
 
 typedef struct FFMStream {
@@ -39,7 +39,7 @@ enum {
 typedef struct FFMContext {
     /* only reading mode */
     offset_t write_index, file_size;
-    int read_state; 
+    int read_state;
     UINT8 header[FRAME_HEADER_SIZE];
 
     /* read and write */
@@ -48,9 +48,12 @@ typedef struct FFMContext {
     int frame_offset;
     INT64 pts;
     UINT8 *packet_ptr, *packet_end;
-    UINT8 packet[1]; /* must be last */
+    UINT8 packet[FFM_PACKET_SIZE];
 } FFMContext;
 
+/* disable pts hack for testing */
+int ffm_nopts = 0;
+
 static void flush_packet(AVFormatContext *s)
 {
     FFMContext *ffm = s->priv_data;
@@ -59,9 +62,9 @@ static void flush_packet(AVFormatContext *s)
 
     fill_size = ffm->packet_end - ffm->packet_ptr;
     memset(ffm->packet_ptr, 0, fill_size);
-    
+
     /* put header */
-    put_be16(pb, PACKET_ID); 
+    put_be16(pb, PACKET_ID);
     put_be16(pb, fill_size);
     put_be64(pb, ffm->pts);
     h = ffm->frame_offset;
@@ -78,8 +81,8 @@ static void flush_packet(AVFormatContext *s)
 }
 
 /* 'first' is true if first data of a frame */
-static void ffm_write_data(AVFormatContext *s, 
-                           UINT8 *buf, int size, 
+static void ffm_write_data(AVFormatContext *s,
+                           UINT8 *buf, int size,
                            INT64 pts, int first)
 {
     FFMContext *ffm = s->priv_data;
@@ -96,7 +99,7 @@ static void ffm_write_data(AVFormatContext *s,
         if (len > size)
             len = size;
         memcpy(ffm->packet_ptr, buf, len);
-        
+
         ffm->packet_ptr += len;
         buf += len;
         size -= len;
@@ -112,20 +115,15 @@ static void ffm_write_data(AVFormatContext *s,
 
 static int ffm_write_header(AVFormatContext *s)
 {
+    FFMContext *ffm = s->priv_data;
     AVStream *st;
     FFMStream *fst;
-    FFMContext *ffm;
     ByteIOContext *pb = &s->pb;
     AVCodecContext *codec;
     int bit_rate, i;
 
-    ffm = av_mallocz(sizeof(FFMContext) + FFM_PACKET_SIZE);
-    if (!ffm)
-        return -1;
-
-    s->priv_data = ffm;
     ffm->packet_size = FFM_PACKET_SIZE;
-    
+
     /* header */
     put_tag(pb, "FFM1");
     put_be32(pb, ffm->packet_size);
@@ -143,7 +141,7 @@ static int ffm_write_header(AVFormatContext *s)
     /* list of streams */
     for(i=0;i<s->nb_streams;i++) {
         st = s->streams[i];
-        fst = av_mallocz(sizeof(FFMStream) + ffm->packet_size);
+        fst = av_mallocz(sizeof(FFMStream));
         if (!fst)
             goto fail;
         st->priv_data = fst;
@@ -153,20 +151,34 @@ static int ffm_write_header(AVFormatContext *s)
         put_be32(pb, codec->codec_id);
         put_byte(pb, codec->codec_type);
         put_be32(pb, codec->bit_rate);
+        put_be32(pb, codec->flags);
         /* specific info */
         switch(codec->codec_type) {
         case CODEC_TYPE_VIDEO:
             put_be32(pb, (codec->frame_rate * 1000) / FRAME_RATE_BASE);
             put_be16(pb, codec->width);
             put_be16(pb, codec->height);
+            put_be16(pb, codec->gop_size);
+            put_byte(pb, codec->qmin);
+            put_byte(pb, codec->qmax);
+            put_byte(pb, codec->max_qdiff);
+            put_be16(pb, (int) (codec->qcompress * 10000.0));
+            put_be16(pb, (int) (codec->qblur * 10000.0));
+            put_be32(pb, codec->bit_rate_tolerance);
             break;
         case CODEC_TYPE_AUDIO:
             put_be32(pb, codec->sample_rate);
             put_le16(pb, codec->channels);
+            put_le16(pb, codec->frame_size);
             break;
+        default:
+            av_abort();
         }
         /* hack to have real time */
-        fst->pts = gettime();
+        if (ffm_nopts)
+            fst->pts = 0;
+        else
+            fst->pts = gettime();
     }
 
     /* flush until end of block reached */
@@ -186,21 +198,25 @@ static int ffm_write_header(AVFormatContext *s)
  fail:
     for(i=0;i<s->nb_streams;i++) {
         st = s->streams[i];
-        fst = st->priv_data;
-        if (fst)
-            free(fst);
+        av_freep(&st->priv_data);
     }
-    free(ffm);
     return -1;
 }
 
 static int ffm_write_packet(AVFormatContext *s, int stream_index,
-                            UINT8 *buf, int size)
+                            UINT8 *buf, int size, int force_pts)
 {
     AVStream *st = s->streams[stream_index];
     FFMStream *fst = st->priv_data;
     INT64 pts;
     UINT8 header[FRAME_HEADER_SIZE];
+    int duration;
+
+    if (st->codec.codec_type == CODEC_TYPE_AUDIO) {
+        duration = ((float)st->codec.frame_size / st->codec.sample_rate * 1000000.0);
+    } else {
+        duration = (1000000.0 * FRAME_RATE_BASE / (float)st->codec.frame_rate);
+    }
 
     pts = fst->pts;
     /* packet size & key_frame */
@@ -211,14 +227,13 @@ static int ffm_write_packet(AVFormatContext *s, int stream_index,
     header[2] = (size >> 16) & 0xff;
     header[3] = (size >> 8) & 0xff;
     header[4] = size & 0xff;
+    header[5] = (duration >> 16) & 0xff;
+    header[6] = (duration >> 8) & 0xff;
+    header[7] = duration & 0xff;
     ffm_write_data(s, header, FRAME_HEADER_SIZE, pts, 1);
     ffm_write_data(s, buf, size, pts, 0);
 
-    if (st->codec.codec_type == CODEC_TYPE_AUDIO) {
-        fst->pts += (INT64)((float)st->codec.frame_size / st->codec.sample_rate * 1000000.0);
-    } else {
-        fst->pts += (INT64)(1000000.0 * FRAME_RATE_BASE / (float)st->codec.frame_rate);
-    }
+    fst->pts += duration;
     return 0;
 }
 
@@ -234,9 +249,17 @@ static int ffm_write_trailer(AVFormatContext *s)
 
     put_flush_packet(pb);
 
+    if (!url_is_streamed(pb)) {
+        INT64 size;
+        /* update the write offset */
+        size = url_ftell(pb);
+        url_fseek(pb, 8, SEEK_SET);
+        put_be64(pb, size);
+        put_flush_packet(pb);
+    }
+
     for(i=0;i<s->nb_streams;i++)
-        free(s->streams[i]->priv_data);
-    free(ffm);
+        av_freep(&s->streams[i]->priv_data);
     return 0;
 }
 
@@ -249,8 +272,11 @@ static int ffm_is_avail_data(AVFormatContext *s, int size)
     int len;
 
     len = ffm->packet_end - ffm->packet_ptr;
-    if (size <= len)
-        return 1;
+    if (!ffm_nopts) {
+        /* XXX: I don't understand this test, so I disabled it for testing */
+        if (size <= len)
+            return 1;
+    }
     pos = url_ftell(&s->pb);
     if (pos == ffm->write_index) {
         /* exactly at the end of stream */
@@ -268,8 +294,8 @@ static int ffm_is_avail_data(AVFormatContext *s, int size)
 }
 
 /* first is true if we read the frame header */
-static int ffm_read_data(AVFormatContext *s, 
-                          UINT8 *buf, int size, int first)
+static int ffm_read_data(AVFormatContext *s,
+                         UINT8 *buf, int size, int first)
 {
     FFMContext *ffm = s->priv_data;
     ByteIOContext *pb = &s->pb;
@@ -284,6 +310,7 @@ static int ffm_read_data(AVFormatContext *s,
         if (len == 0) {
             if (url_ftell(pb) == ffm->file_size)
                 url_fseek(pb, ffm->packet_size, SEEK_SET);
+    retry_read:
             get_be16(pb); /* PACKET_ID */
             fill_size = get_be16(pb);
             ffm->pts = get_be64(pb);
@@ -293,7 +320,18 @@ static int ffm_read_data(AVFormatContext *s,
             /* if first packet or resynchronization packet, we must
                handle it specifically */
             if (ffm->first_packet || (frame_offset & 0x8000)) {
+                if (!frame_offset) {
+                    /* This packet has no frame headers in it */
+                    if (url_ftell(pb) >= ffm->packet_size * 3) {
+                        url_fseek(pb, -ffm->packet_size * 2, SEEK_CUR);
+                        goto retry_read;
+                    }
+                    /* This is bad, we cannot find a valid frame header */
+                    return 0;
+                }
                 ffm->first_packet = 0;
+                if ((frame_offset & 0x7ffff) < FFM_HEADER_SIZE)
+                    av_abort();
                 ffm->packet_ptr = ffm->packet + (frame_offset & 0x7fff) - FFM_HEADER_SIZE;
                 if (!first)
                     break;
@@ -314,20 +352,14 @@ static int ffm_read_data(AVFormatContext *s,
 
 static int ffm_read_header(AVFormatContext *s, AVFormatParameters *ap)
 {
+    FFMContext *ffm = s->priv_data;
     AVStream *st;
     FFMStream *fst;
-    FFMContext *ffm;
     ByteIOContext *pb = &s->pb;
     AVCodecContext *codec;
     int i;
     UINT32 tag;
 
-    ffm = av_mallocz(sizeof(FFMContext) + FFM_PACKET_SIZE);
-    if (!ffm)
-        return -1;
-
-    s->priv_data = ffm;
-
     /* header */
     tag = get_le32(pb);
     if (tag != MKTAG('F', 'F', 'M', '1'))
@@ -351,7 +383,7 @@ static int ffm_read_header(AVFormatContext *s, AVFormatParameters *ap)
         if (!st)
             goto fail;
         s->streams[i] = st;
-        fst = av_mallocz(sizeof(FFMStream) + ffm->packet_size);
+        fst = av_mallocz(sizeof(FFMStream));
         if (!fst)
             goto fail;
         st->priv_data = fst;
@@ -361,17 +393,28 @@ static int ffm_read_header(AVFormatContext *s, AVFormatParameters *ap)
         st->codec.codec_id = get_be32(pb);
         st->codec.codec_type = get_byte(pb); /* codec_type */
         codec->bit_rate = get_be32(pb);
+        codec->flags = get_be32(pb);
         /* specific info */
         switch(codec->codec_type) {
         case CODEC_TYPE_VIDEO:
             codec->frame_rate = ((INT64)get_be32(pb) * FRAME_RATE_BASE) / 1000;
             codec->width = get_be16(pb);
             codec->height = get_be16(pb);
+            codec->gop_size = get_be16(pb);
+            codec->qmin = get_byte(pb);
+            codec->qmax = get_byte(pb);
+            codec->max_qdiff = get_byte(pb);
+            codec->qcompress = get_be16(pb) / 10000.0;
+            codec->qblur = get_be16(pb) / 10000.0;
+            codec->bit_rate_tolerance = get_be32(pb);
             break;
         case CODEC_TYPE_AUDIO:
             codec->sample_rate = get_be32(pb);
             codec->channels = get_le16(pb);
+            codec->frame_size = get_le16(pb);
             break;
+        default:
+            av_abort();
         }
 
     }
@@ -392,14 +435,10 @@ static int ffm_read_header(AVFormatContext *s, AVFormatParameters *ap)
     for(i=0;i<s->nb_streams;i++) {
         st = s->streams[i];
         if (st) {
-            fst = st->priv_data;
-            if (fst)
-                free(fst);
-            free(st);
+            av_freep(&st->priv_data);
+            av_free(st);
         }
     }
-    if (ffm)
-        free(ffm);
     return -1;
 }
 
@@ -408,18 +447,20 @@ static int ffm_read_packet(AVFormatContext *s, AVPacket *pkt)
 {
     int size;
     FFMContext *ffm = s->priv_data;
+    int duration;
 
     switch(ffm->read_state) {
     case READ_HEADER:
-        if (!ffm_is_avail_data(s, FRAME_HEADER_SIZE))
+        if (!ffm_is_avail_data(s, FRAME_HEADER_SIZE)) {
             return -EAGAIN;
+        }
 #if 0
-        printf("pos=%08Lx spos=%Lx, write_index=%Lx size=%Lx\n", 
+        printf("pos=%08Lx spos=%Lx, write_index=%Lx size=%Lx\n",
                url_ftell(&s->pb), s->pb.pos, ffm->write_index, ffm->file_size);
 #endif
-        if (ffm_read_data(s, ffm->header, FRAME_HEADER_SIZE, 1) != FRAME_HEADER_SIZE)
+        if (ffm_read_data(s, ffm->header, FRAME_HEADER_SIZE, 1) != 
+            FRAME_HEADER_SIZE)
             return -EAGAIN;
-            
 #if 0
         {
             int i;
@@ -436,17 +477,21 @@ static int ffm_read_packet(AVFormatContext *s, AVPacket *pkt)
             return -EAGAIN;
         }
 
+        duration = (ffm->header[5] << 16) | (ffm->header[6] << 8) | ffm->header[7];
+
         av_new_packet(pkt, size);
         pkt->stream_index = ffm->header[0];
         if (ffm->header[1] & FLAG_KEY_FRAME)
             pkt->flags |= PKT_FLAG_KEY;
-    
+
         ffm->read_state = READ_HEADER;
         if (ffm_read_data(s, pkt->data, size, 0) != size) {
             /* bad case: desynchronized packet. we cancel all the packet loading */
             av_free_packet(pkt);
             return -EAGAIN;
         }
+        pkt->pts = ffm->pts;
+        pkt->duration = duration;
         break;
     }
     return 0;
@@ -476,7 +521,7 @@ static INT64 get_pts(AVFormatContext *s, offset_t pos)
     ByteIOContext *pb = &s->pb;
     INT64 pts;
 
-    ffm_seek1(s, pos); 
+    ffm_seek1(s, pos);
     url_fskip(pb, 4);
     pts = get_be64(pb);
 #ifdef DEBUG_SEEK
@@ -506,7 +551,7 @@ static int ffm_seek(AVFormatContext *s, INT64 wanted_pts)
         pts_min = get_pts(s, pos_min);
         pts_max = get_pts(s, pos_max);
         /* linear interpolation */
-        pos1 = (double)(pos_max - pos_min) * (double)(wanted_pts - pts_min) / 
+        pos1 = (double)(pos_max - pos_min) * (double)(wanted_pts - pts_min) /
             (double)(pts_max - pts_min);
         pos = (((INT64)pos1) / FFM_PACKET_SIZE) * FFM_PACKET_SIZE;
         if (pos <= pos_min)
@@ -540,7 +585,7 @@ offset_t ffm_read_write_index(int fd)
     lseek(fd, 8, SEEK_SET);
     read(fd, buf, 8);
     pos = 0;
-    for(i=0;i<8;i++) 
+    for(i=0;i<8;i++)
         pos |= buf[i] << (56 - i * 8);
     return pos;
 }
@@ -550,7 +595,7 @@ void ffm_write_write_index(int fd, offset_t pos)
     UINT8 buf[8];
     int i;
 
-    for(i=0;i<8;i++) 
+    for(i=0;i<8;i++)
         buf[i] = (pos >> (56 - i * 8)) & 0xff;
     lseek(fd, 8, SEEK_SET);
     write(fd, buf, 8);
@@ -570,25 +615,48 @@ static int ffm_read_close(AVFormatContext *s)
 
     for(i=0;i<s->nb_streams;i++) {
         st = s->streams[i];
-        free(st->priv_data);
+        av_freep(&st->priv_data);
     }
-    free(s->priv_data);
     return 0;
 }
 
-AVFormat ffm_format = {
+static int ffm_probe(AVProbeData *p)
+{
+    if (p->buf_size >= 4 &&
+        p->buf[0] == 'F' && p->buf[1] == 'F' && p->buf[2] == 'M' && 
+        p->buf[3] == '1')
+        return AVPROBE_SCORE_MAX + 1;
+    return 0;
+}
+
+AVInputFormat ffm_iformat = {
+    "ffm",
+    "ffm format",
+    sizeof(FFMContext),
+    ffm_probe,
+    ffm_read_header,
+    ffm_read_packet,
+    ffm_read_close,
+    ffm_seek,
+};
+
+AVOutputFormat ffm_oformat = {
     "ffm",
     "ffm format",
     "",
     "ffm",
+    sizeof(FFMContext),
     /* not really used */
     CODEC_ID_MP2,
     CODEC_ID_MPEG1VIDEO,
     ffm_write_header,
     ffm_write_packet,
     ffm_write_trailer,
-    ffm_read_header,
-    ffm_read_packet,
-    ffm_read_close,
-    ffm_seek,
 };
+
+int ffm_init(void)
+{
+    av_register_input_format(&ffm_iformat);
+    av_register_output_format(&ffm_oformat);
+    return 0;
+}