[FFmpeg-devel] [PATCH] lavf: Add SMJPEG demuxer.

Paul B Mahol onemda at gmail.com
Tue Dec 20 00:43:23 CET 2011


Signed-off-by: Paul B Mahol <onemda at gmail.com>
---
 Changelog                |    1 +
 doc/general.texi         |    1 +
 libavformat/Makefile     |    1 +
 libavformat/allformats.c |    1 +
 libavformat/smjpeg.c     |  187 ++++++++++++++++++++++++++++++++++++++++++++++
 libavformat/version.h    |    2 +-
 6 files changed, 192 insertions(+), 1 deletions(-)
 create mode 100644 libavformat/smjpeg.c

diff --git a/Changelog b/Changelog
index d7dd629..c7ae969 100644
--- a/Changelog
+++ b/Changelog
@@ -138,6 +138,7 @@ easier to use. The changes are:
 - v410 Quicktime Uncompressed 4:4:4 10-bit encoder and decoder
 - SBaGen (SBG) binaural beats script demuxer
 - OpenMG Audio muxer
+- SMJPEG demuxer
 
 
 version 0.8:
diff --git a/doc/general.texi b/doc/general.texi
index 2b048f9..a45eef0 100644
--- a/doc/general.texi
+++ b/doc/general.texi
@@ -307,6 +307,7 @@ library:
 @item RTP                       @tab X @tab X
 @item RTSP                      @tab X @tab X
 @item SAP                       @tab X @tab X
+ at item SMJPEG                    @tab   @tab X
 @item SBG                       @tab   @tab X
 @item SDP                       @tab   @tab X
 @item Sega FILM/CPK             @tab   @tab X
diff --git a/libavformat/Makefile b/libavformat/Makefile
index 155e389..388e0b2 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -282,6 +282,7 @@ OBJS-$(CONFIG_RTSP_MUXER)                += rtsp.o rtspenc.o httpauth.o \
                                             rtpenc_chain.o
 OBJS-$(CONFIG_SAP_DEMUXER)               += sapdec.o
 OBJS-$(CONFIG_SAP_MUXER)                 += sapenc.o rtpenc_chain.o
+OBJS-$(CONFIG_SMJPEG_DEMUXER)            += smjpeg.o
 OBJS-$(CONFIG_SBG_DEMUXER)               += sbgdec.o
 OBJS-$(CONFIG_SDP_DEMUXER)               += rtsp.o
 OBJS-$(CONFIG_SEGAFILM_DEMUXER)          += segafilm.o
diff --git a/libavformat/allformats.c b/libavformat/allformats.c
index c8dc3b0..3b810b8 100644
--- a/libavformat/allformats.c
+++ b/libavformat/allformats.c
@@ -201,6 +201,7 @@ void av_register_all(void)
     REGISTER_MUXDEMUX (RTP, rtp);
     REGISTER_MUXDEMUX (RTSP, rtsp);
     REGISTER_MUXDEMUX (SAP, sap);
+    REGISTER_DEMUXER  (SMJPEG, smjpeg);
     REGISTER_DEMUXER  (SBG, sbg);
     REGISTER_DEMUXER  (SDP, sdp);
 #if CONFIG_RTPDEC
diff --git a/libavformat/smjpeg.c b/libavformat/smjpeg.c
new file mode 100644
index 0000000..f13b019
--- /dev/null
+++ b/libavformat/smjpeg.c
@@ -0,0 +1,187 @@
+/*
+ * SMJPEG demuxer
+ * Copyright (c) 2011 Paul B Mahol
+ *
+ * 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
+ * This is a demuxer for Loki SDL MJPEG files
+ */
+
+#include "avformat.h"
+#include "internal.h"
+
+typedef struct SMJPEGContext {
+    int audio_stream_index;
+    int video_stream_index;
+} SMJPEGContext;
+
+static int smjpeg_probe(AVProbeData *p)
+{
+    if ((p->buf[0] == 0 && p->buf[1] == 0xa &&
+         p->buf[2] == 'S' && p->buf[3] == 'M' &&
+         p->buf[4] == 'J' && p->buf[5] == 'P' &&
+         p->buf[6] == 'E' && p->buf[7] == 'G'))
+        return AVPROBE_SCORE_MAX;
+    return 0;
+}
+
+static int smjpeg_read_header(AVFormatContext *s, AVFormatParameters *ap)
+{
+    SMJPEGContext *sc = s->priv_data;
+    AVStream *ast = NULL, *vst = NULL;
+    AVIOContext *pb = s->pb;
+    uint32_t version, encoding, htype, hlength, duration;
+    char *comment;
+
+    avio_skip(pb, 8); // magic
+    version = avio_rb32(pb);
+    if (version) {
+        av_log(s, AV_LOG_ERROR, "unknown version %d\n", version);
+        return AVERROR_PATCHWELCOME;
+    }
+    duration = avio_rb32(pb); // in msec
+
+    while (!pb->eof_reached) {
+        htype = avio_rl32(pb);
+        switch (htype) {
+        case MKTAG('_', 'T', 'X', 'T'):
+            hlength = avio_rb32(pb);
+            if (!hlength || hlength > 512)
+                return AVERROR_INVALIDDATA;
+            comment = av_malloc(hlength + 1);
+            if (!comment)
+                return AVERROR(ENOMEM);
+            if (avio_read(pb, comment, hlength) != hlength) {
+                av_freep(&comment);
+                av_log(s, AV_LOG_ERROR, "error when parsing comment\n");
+                return AVERROR_INVALIDDATA;
+            }
+            comment[hlength] = 0;
+            av_dict_set(&s->metadata, "comment", comment,
+                        AV_DICT_DONT_STRDUP_VAL );
+            break;
+        case MKTAG('_', 'S', 'N', 'D'):
+            if (ast)
+                return AVERROR_INVALIDDATA;
+            hlength = avio_rb32(pb);
+            if (hlength < 8)
+                return AVERROR_INVALIDDATA;
+            ast = avformat_new_stream(s, 0);
+            if (!ast)
+                return AVERROR(ENOMEM);
+            ast->codec->codec_type  = AVMEDIA_TYPE_AUDIO;
+            ast->codec->sample_rate = avio_rb16(pb);
+            ast->codec->bits_per_coded_sample = avio_r8(pb);
+            ast->codec->channels    = avio_r8(pb);
+            encoding                = avio_rl32(pb);
+            if (encoding == MKTAG('A', 'P', 'C', 'M')) {
+                ast->codec->codec_id = CODEC_ID_ADPCM_IMA_SMJPEG;
+            } else if (encoding == MKTAG('N', 'O', 'N', 'E')) {
+                ast->codec->codec_id = CODEC_ID_PCM_S16BE;
+            } else {
+                av_log(s, AV_LOG_ERROR, "unknown audio encoding %d\n", encoding);
+            }
+            ast->codec->bit_rate    = ast->codec->sample_rate *
+                                      ast->codec->bits_per_coded_sample *
+                                      ast->codec->channels;
+            ast->duration           = duration;
+            sc->audio_stream_index  = ast->index;
+            avpriv_set_pts_info(ast, 32, 1, 1000);
+            avio_skip(pb, hlength - 8);
+            break;
+        case MKTAG('_', 'V', 'I', 'D'):
+            if (vst)
+                return AVERROR_INVALIDDATA;
+            hlength = avio_rb32(pb);
+            if (hlength < 12)
+                return AVERROR_INVALIDDATA;
+            avio_skip(pb, 4); // number of frames
+            vst = avformat_new_stream(s, 0);
+            if (!vst)
+                return AVERROR(ENOMEM);
+            vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
+            vst->codec->width      = avio_rb16(pb);
+            vst->codec->height     = avio_rb16(pb);
+            encoding               = avio_rl32(pb);
+            if (encoding == MKTAG('J', 'F', 'I', 'F')) {
+                vst->codec->codec_id   = CODEC_ID_MJPEG;
+            } else {
+                av_log(s, AV_LOG_ERROR, "unknown video encoding %d\n", encoding);
+            }
+            vst->duration          = duration;
+            sc->video_stream_index = vst->index;
+            avpriv_set_pts_info(vst, 32, 1, 1000);
+            avio_skip(pb, hlength - 12);
+            break;
+        case MKTAG('H', 'E', 'N', 'D'):
+            avio_seek(pb, avio_tell(pb), SEEK_SET);
+            return 0;
+            break;
+        default:
+            av_log(s, AV_LOG_ERROR, "unknown header %d\n", htype);
+            break;
+        }
+    }
+
+    return AVERROR(EIO);
+}
+
+static int smjpeg_read_packet(AVFormatContext *s, AVPacket *pkt)
+{
+    SMJPEGContext *sc = s->priv_data;
+    uint32_t dtype, ret, size, timestamp;
+
+    if (s->pb->eof_reached)
+        return AVERROR(EIO);
+    dtype = avio_rl32(s->pb);
+    switch (dtype) {
+    case MKTAG('s', 'n', 'd', 'D'):
+        timestamp = avio_rb32(s->pb);
+        size = avio_rb32(s->pb);
+        ret = av_get_packet(s->pb, pkt, size);
+        pkt->stream_index = sc->audio_stream_index;
+        pkt->pts = timestamp;
+        break;
+    case MKTAG('v', 'i', 'd', 'D'):
+        timestamp = avio_rb32(s->pb);
+        size = avio_rb32(s->pb);
+        ret = av_get_packet(s->pb, pkt, size);
+        pkt->stream_index = sc->video_stream_index;
+        pkt->pts = timestamp;
+        break;
+    case MKTAG('D', 'O', 'N', 'E'):
+        ret = AVERROR(EIO);
+        break;
+    default:
+        ret = AVERROR(EAGAIN);
+        break;
+    }
+    return ret;
+}
+
+AVInputFormat ff_smjpeg_demuxer = {
+    .name           = "smjpeg",
+    .long_name      = NULL_IF_CONFIG_SMALL("Loki SDL MJPEG"),
+    .priv_data_size = sizeof(SMJPEGContext),
+    .read_probe     = smjpeg_probe,
+    .read_header    = smjpeg_read_header,
+    .read_packet    = smjpeg_read_packet,
+    .extensions     = "mjpg",
+};
diff --git a/libavformat/version.h b/libavformat/version.h
index c447eb4..7e19291 100644
--- a/libavformat/version.h
+++ b/libavformat/version.h
@@ -30,7 +30,7 @@
 #include "libavutil/avutil.h"
 
 #define LIBAVFORMAT_VERSION_MAJOR 53
-#define LIBAVFORMAT_VERSION_MINOR 27
+#define LIBAVFORMAT_VERSION_MINOR 28
 #define LIBAVFORMAT_VERSION_MICRO  0
 
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
-- 
1.7.7



More information about the ffmpeg-devel mailing list