[FFmpeg-devel] [PATCH] Vivo demuxer

Paul B Mahol onemda at gmail.com
Tue Jul 17 03:08:24 CEST 2012


From: Daniel Verkamp <daniel at drv.nu>

---
Only video for version 1 is playable.
---
 libavformat/Makefile     |    1 +
 libavformat/allformats.c |    1 +
 libavformat/vivo.c       |  336 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 338 insertions(+), 0 deletions(-)
 create mode 100644 libavformat/vivo.c

diff --git a/libavformat/Makefile b/libavformat/Makefile
index a3ddcad..08bf1c5 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -336,6 +336,7 @@ OBJS-$(CONFIG_TXD_DEMUXER)               += txd.o
 OBJS-$(CONFIG_VC1_DEMUXER)               += rawdec.o
 OBJS-$(CONFIG_VC1T_DEMUXER)              += vc1test.o
 OBJS-$(CONFIG_VC1T_MUXER)                += vc1testenc.o
+OBJS-$(CONFIG_VIVO_DEMUXER)              += vivo.o
 OBJS-$(CONFIG_VMD_DEMUXER)               += sierravmd.o
 OBJS-$(CONFIG_VOC_DEMUXER)               += vocdec.o voc.o
 OBJS-$(CONFIG_VOC_MUXER)                 += vocenc.o voc.o
diff --git a/libavformat/allformats.c b/libavformat/allformats.c
index 7ea4ebb..a8d2152 100644
--- a/libavformat/allformats.c
+++ b/libavformat/allformats.c
@@ -242,6 +242,7 @@ void av_register_all(void)
     REGISTER_DEMUXER  (TTY, tty);
     REGISTER_DEMUXER  (VC1, vc1);
     REGISTER_MUXDEMUX (VC1T, vc1t);
+    REGISTER_DEMUXER  (VIVO, vivo);
     REGISTER_DEMUXER  (VMD, vmd);
     REGISTER_MUXDEMUX (VOC, voc);
     REGISTER_DEMUXER  (VQF, vqf);
diff --git a/libavformat/vivo.c b/libavformat/vivo.c
new file mode 100644
index 0000000..42a2fdc
--- /dev/null
+++ b/libavformat/vivo.c
@@ -0,0 +1,336 @@
+/*
+ * Vivo stream demuxer
+ * Copyright (c) 2009 Daniel Verkamp <daniel at drv.nu>
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg 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.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg 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 Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+/**
+ * @file
+ * @brief Vivo stream demuxer
+ * @author Daniel Verkamp <daniel at drv.nu>
+ * @sa http://wiki.multimedia.cx/index.php?title=Vivo
+ */
+
+#include "avformat.h"
+#include "internal.h"
+
+typedef struct VivoContext {
+    int version;
+
+    unsigned type;
+    unsigned sequence;
+    unsigned len;
+
+    long duration;
+    double fps;
+    AVRational timebase;
+    int nominal_bitrate; // audio bitrate
+} VivoContext;
+
+static int vivo_probe(AVProbeData *p)
+{
+    const unsigned char *buf = p->buf;
+    unsigned c, len = 0;
+
+    // stream must start with packet of type 0 and sequence number 0
+    if (*buf++ != 0)
+        return 0;
+
+    // read at most 2 bytes of coded length
+    c = *buf++;
+    len = c & 0x7F;
+    if (c & 0x80) {
+        c = *buf++;
+        len = (len << 7) | (c & 0x7F);
+    }
+    if (c & 0x80 || len > 1024 || len < 21)
+        return 0;
+
+    if (memcmp(buf, "\r\nVersion:Vivo/", 15))
+        return 0;
+    buf += 15;
+
+    if (*buf != '0' && *buf != '1' && *buf != '2')
+        return 0;
+
+    return AVPROBE_SCORE_MAX;
+}
+
+static int vivo_get_packet_header(AVFormatContext *s)
+{
+    VivoContext *vivo = s->priv_data;
+    AVIOContext *pb = s->pb;
+    unsigned c, get_len = 0;
+
+    if (url_feof(pb))
+        return AVERROR_EOF;
+
+    c = avio_r8(pb);
+
+    if (c == 0x82) {
+        get_len = 1;
+        c = avio_r8(pb);
+    }
+
+    vivo->type     = c >> 4;
+    vivo->sequence = c & 0xF;
+
+    switch (vivo->type) {
+    case 0:   get_len =   1; break;
+    case 1: vivo->len = 128; break;
+    case 2:   get_len =   1; break;
+    case 3: vivo->len =  40; break;
+    case 4: vivo->len =  24; break;
+    default:
+        av_log(s, AV_LOG_ERROR, "unknown packet type %d\n", vivo->type);
+        return AVERROR_INVALIDDATA;
+    }
+
+    if (get_len) {
+        c = avio_r8(pb);
+        vivo->len = c & 0x7F;
+        if (c & 0x80) {
+            c = avio_r8(pb);
+            vivo->len = (vivo->len << 7) | (c & 0x7F);
+
+            if (c & 0x80) {
+                av_log(s, AV_LOG_ERROR, "coded length is more than two bytes\n");
+                return AVERROR_INVALIDDATA;
+            }
+        }
+    }
+
+    return 0;
+}
+
+static int vivo_read_text_header(AVFormatContext *s,
+                                 AVStream *vst, AVStream *ast)
+{
+    VivoContext *vivo = s->priv_data;
+    AVIOContext *pb = s->pb;
+    unsigned char text[vivo->len + 1];
+    unsigned char *line, *line_end, *key, *value;
+    char *end_value;
+    long value_int, value_used;
+
+    avio_read(pb, text, vivo->len);
+    text[vivo->len] = 0;
+
+    line = text;
+    while (*line) {
+        line_end = strstr(line, "\r\n");
+        if (!line_end)
+            break;
+
+        *line_end = '\0';
+        key = line;
+        line = line_end + 2; // skip \r\n
+
+        if (line_end == key) // skip blank lines
+            continue;
+
+        value = strchr(key, ':');
+        if (!value) {
+            av_log(s, AV_LOG_WARNING, "missing colon in key:value pair '%s'\n",
+                   value);
+            continue;
+        }
+
+        *value++ = 0;
+
+        av_log(s, AV_LOG_DEBUG, "header: '%s' = '%s'\n", key, value);
+
+        value_int = strtol(value, &end_value, 10);
+        value_used = 0;
+        if (*end_value == 0) { // valid integer
+            av_log(s, AV_LOG_DEBUG, "got a valid integer (%ld)\n", value_int);
+            value_used = 1;
+            if (!strcmp(key, "Duration")) {
+                vivo->duration = value_int;
+            } else  if (!strcmp(key, "Width")) {
+                vst->codec->width = value_int;
+            } else if (!strcmp(key, "Height")) {
+                vst->codec->height = value_int;
+            } else if (!strcmp(key, "TimeUnitNumerator")) {
+                vivo->timebase.num = value_int;
+            } else if (!strcmp(key, "TimeUnitDenominator")) {
+                vivo->timebase.den = value_int;
+            } else if (!strcmp(key, "SamplingFrequency")) {
+                ast->codec->sample_rate = value_int;
+                if (value_int == 16000) {
+                    //ast->codec->codec_id = CODEC_ID_SIREN;
+                } else if (value_int == 8000) {
+                    //ast->codec->codec_id = CODEC_ID_G723;
+                }
+            } else if (!strcmp(key, "NominalBitrate")) {
+                vivo->nominal_bitrate = value_int;
+            } else if (!strcmp(key, "Length")) {
+                // size of file
+            } else {
+                value_used = 0;
+            }
+        }
+
+        if (!strcmp(key, "Version")) {
+            if (sscanf(value, "Vivo/%d.", &vivo->version) != 1)
+                return AVERROR_INVALIDDATA;
+            value_used = 1;
+        } else if (!strcmp(key, "FPS")) {
+            vivo->fps = atof(value);
+            value_used = 1;
+        }
+
+        if (!value_used)
+            av_dict_set(&s->metadata, key, value, 0);
+    }
+
+    return 0;
+}
+
+static int vivo_read_header(AVFormatContext *s)
+{
+    VivoContext *vivo = s->priv_data;
+    AVStream *ast, *vst;
+    int ret;
+
+    vst = avformat_new_stream(s, 0);
+    ast = avformat_new_stream(s, 0);
+    if (!ast || !vst)
+        return AVERROR(ENOMEM);
+
+    while (1) {
+        if ((ret = vivo_get_packet_header(s)) < 0)
+            return ret;
+
+        // done reading all text header packets?
+        if (vivo->sequence || vivo->type)
+            break;
+
+        if ((ret = vivo_read_text_header(s, vst, ast)) < 0)
+            return ret;
+    }
+
+    vst->start_time        = 0;
+    vst->codec->codec_tag  = 0;
+    vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
+    if (vivo->version == 1)
+        vst->codec->codec_id = CODEC_ID_H263;
+
+    ast->start_time        = 0;
+    ast->codec->codec_tag  = 0;
+    ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
+    ast->codec->channels = 1;
+    ast->codec->bits_per_coded_sample = 8;
+    ast->codec->sample_fmt = AV_SAMPLE_FMT_U8;
+    ast->codec->sample_rate = 0;
+    vivo->nominal_bitrate = 0;
+
+    if (ast->codec->sample_rate == 0) {
+        ast->codec->sample_rate = 8000;
+        //ast->codec->codec_id = CODEC_ID_G723;
+    }
+
+    if (vivo->nominal_bitrate == 0)
+        vivo->nominal_bitrate = 800;
+
+    if (vivo->fps)
+        vivo->timebase = av_d2q(1.0 / vivo->fps, INT_MAX);
+
+    avpriv_set_pts_info(vst, 64, vivo->timebase.num, vivo->timebase.den);
+    avpriv_set_pts_info(ast, 64, 1, ast->codec->sample_rate * vivo->nominal_bitrate >> 3);
+    vst->duration = av_rescale(vivo->duration, vst->time_base.den, vst->time_base.num * 1000);
+
+    return 0;
+}
+
+static int vivo_read_packet(AVFormatContext *s, AVPacket *pkt)
+{
+    VivoContext *vivo = s->priv_data;
+    AVIOContext *pb = s->pb;
+    unsigned old_sequence = vivo->sequence, old_type = vivo->type;
+    int stream_index, ret = 0;
+
+restart:
+
+    if (url_feof(pb))
+        return AVERROR_EOF;
+
+    switch (vivo->type) {
+    case 0:
+        avio_skip(pb, vivo->len);
+        if ((ret = vivo_get_packet_header(s)) < 0) {
+            av_log(s, AV_LOG_ERROR, "couldn't get packet header\n");
+            return AVERROR(EIO);
+        }
+        goto restart;
+    case 1:
+    case 2: // video
+        stream_index = 0;
+        break;
+    case 3:
+    case 4: // audio
+        stream_index = 1;
+        break;
+    default:
+        av_log(s, AV_LOG_ERROR, "unknown packet type %d\n", vivo->type);
+        return AVERROR_INVALIDDATA;
+    }
+
+    if ((ret = av_get_packet(pb, pkt, vivo->len)) < 0)
+        goto fail;
+
+    // get next packet header
+    if ((ret = vivo_get_packet_header(s)) < 0) {
+        av_log(s, AV_LOG_ERROR, "couldn't get packet header\n");
+        goto fail;
+    }
+
+    while (vivo->sequence == old_sequence &&
+           (((vivo->type - 1) >> 1) == ((old_type - 1) >> 1))) {
+        if (url_feof(pb)) {
+            ret = AVERROR_EOF;
+            break;
+        }
+
+        if ((ret = av_append_packet(pb, pkt, vivo->len)) < 0)
+            break;
+
+        // get next packet header
+        if ((ret = vivo_get_packet_header(s)) < 0) {
+            av_log(s, AV_LOG_ERROR, "couldn't get packet header\n");
+            break;
+        }
+    }
+
+    pkt->stream_index = stream_index;
+
+fail:
+    if (ret < 0)
+        av_free_packet(pkt);
+    return ret;
+}
+
+AVInputFormat ff_vivo_demuxer = {
+    .name           = "vivo",
+    .long_name      = NULL_IF_CONFIG_SMALL("Vivo"),
+    .priv_data_size = sizeof(VivoContext),
+    .read_probe     = vivo_probe,
+    .read_header    = vivo_read_header,
+    .read_packet    = vivo_read_packet,
+    .extensions     = "viv",
+};
-- 
1.7.7



More information about the ffmpeg-devel mailing list