[vlc-devel] [PATCH 3/3] oggspots: Add OggSpots codec module

Michael Tänzer neo at nhng.de
Sun Feb 28 21:22:08 CET 2016


---
 configure.ac              |   5 +
 modules/MODULES_LIST      |   1 +
 modules/codec/Makefile.am |   7 +
 modules/codec/oggspots.c  | 465 ++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 478 insertions(+)
 create mode 100644 modules/codec/oggspots.c

diff --git a/configure.ac b/configure.ac
index cd7ccbb..4980484 100644
--- a/configure.ac
+++ b/configure.ac
@@ -2768,6 +2768,11 @@ dnl
 PKG_ENABLE_MODULES_VLC([THEORA], [], [ogg theoradec >= 1.0 theoraenc], [experimental theora codec], [auto])
 
 dnl
+dnl  OggSpots decoder plugin
+dnl
+PKG_ENABLE_MODULES_VLC([OGGSPOTS], [], [ogg], [experimental OggSpots codec], [auto])
+
+dnl
 dnl  Daala decoder plugin
 dnl
 PKG_ENABLE_MODULES_VLC([DAALA], [], [ogg daaladec daalaenc], [experimental daala codec], [disabled])
diff --git a/modules/MODULES_LIST b/modules/MODULES_LIST
index 55822e1..5dcd63d 100644
--- a/modules/MODULES_LIST
+++ b/modules/MODULES_LIST
@@ -266,6 +266,7 @@ $Id$
  * ntservice: run VLC as a NT service
  * nuv: NUV demuxer
  * ogg: input module for OGG decapsulation
+ * oggspots: an OggSpots video decoder/packetizer/encoder
  * oldmovie: oldmovie style video filter
  * oldrc: old interface module using stdio
  * omxil: OpenMAX IL audio/video decoder
diff --git a/modules/codec/Makefile.am b/modules/codec/Makefile.am
index 8cb785a..0756f2f 100644
--- a/modules/codec/Makefile.am
+++ b/modules/codec/Makefile.am
@@ -289,6 +289,13 @@ libvorbis_plugin_la_LIBADD = $(VORBIS_LIBS)
 EXTRA_LTLIBRARIES += libvorbis_plugin.la
 codec_LTLIBRARIES += $(LTLIBvorbis)
 
+liboggspots_plugin_la_SOURCES = codec/oggspots.c
+liboggspots_plugin_la_CFLAGS = $(AM_CFLAGS) $(OGGSPOTS_CFLAGS)
+liboggspots_plugin_la_LDFLAGS = $(AM_LDFLAGS) -rpath '$(codecdir)'
+liboggspots_plugin_la_LIBADD = $(OGGSPOTS_LIBS)
+EXTRA_LTLIBRARIES += liboggspots_plugin.la
+codec_LTLIBRARIES += $(LTLIBoggspots)
+
 libvideotoolbox_plugin_la_SOURCES = video_chroma/copy.c video_chroma/copy.h codec/videotoolbox.m \
                                     packetizer/h264_nal.c packetizer/h264_nal.h \
                                     packetizer/hxxx_nal.c packetizer/hxxx_nal.h
diff --git a/modules/codec/oggspots.c b/modules/codec/oggspots.c
new file mode 100644
index 0000000..9d4fec9
--- /dev/null
+++ b/modules/codec/oggspots.c
@@ -0,0 +1,465 @@
+/*****************************************************************************
+ * oggspots.c: OggSpots decoder module.
+ *****************************************************************************
+ * Copyright (C) 1999-2012 VLC authors and VideoLAN
+ * $Id$
+ *
+ * Authors: Michael Taenzer <neo at nhng.de>
+ *
+ * This program 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.
+ *
+ * This program 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 this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+/*****************************************************************************
+ * Preamble
+ *****************************************************************************/
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <vlc_common.h>
+#include <vlc_plugin.h>
+#include <vlc_modules.h>
+#include <vlc_codec.h>
+#include <vlc_sout.h>
+#include <vlc_input.h>
+
+#include <assert.h>
+#include <limits.h>
+
+/*****************************************************************************
+ * decoder_sys_t : oggspots decoder descriptor
+ *****************************************************************************/
+struct decoder_sys_t
+{
+    /* Module mode */
+    bool b_packetizer;
+
+    /*
+     * Input properties
+     */
+    bool b_has_headers;
+
+    /*
+     * Output decoder
+     */
+    decoder_t* p_img_dec;
+
+    /*
+     * Common properties
+     */
+    mtime_t i_pts;
+};
+
+/*****************************************************************************
+ * Local prototypes
+ *****************************************************************************/
+static int  OpenDecoder   (vlc_object_t*);
+static int  OpenPacketizer(vlc_object_t*);
+static void CloseDecoder  (vlc_object_t*);
+
+static void*      DecodeBlock  (decoder_t*, block_t**);
+static int        ProcessHeader(decoder_t*);
+static void*      ProcessPacket(decoder_t*, block_t**);
+static void       Flush        (decoder_t*);
+static picture_t* DecodePacket (decoder_t*, block_t*);
+
+static int        Forward_vout_format_update(decoder_t*);
+static picture_t* Forward_vout_buffer_new   (decoder_t*);
+
+
+/*****************************************************************************
+ * Module descriptor
+ *****************************************************************************/
+
+vlc_module_begin ()
+    set_category(CAT_INPUT)
+    set_subcategory(SUBCAT_INPUT_VCODEC)
+    set_shortname("OggSpots")
+    set_description(N_("OggSpots video decoder"))
+    set_capability("decoder", 10)
+    set_callbacks(OpenDecoder, CloseDecoder)
+    add_shortcut("oggspots")
+
+    add_submodule ()
+    set_description(N_("OggSpots video packetizer"))
+    set_capability("packetizer", 10)
+    set_callbacks(OpenPacketizer, CloseDecoder)
+    add_shortcut("oggspots")
+vlc_module_end ()
+
+/*****************************************************************************
+ * OpenDecoder: probe the decoder and return score
+ *****************************************************************************/
+static int OpenDecoder(vlc_object_t* p_this)
+{
+    decoder_t* p_dec = (decoder_t*)p_this;
+    decoder_sys_t* p_sys;
+    decoder_t* p_img_dec;
+
+    if (p_dec->fmt_in.i_codec != VLC_CODEC_OGGSPOTS) {
+        return VLC_EGENERIC;
+    }
+
+    /* Allocate the memory needed to store the decoder's structure */
+    p_sys = malloc(sizeof(*p_sys));
+    if (p_sys == NULL) {
+        return VLC_ENOMEM;
+    }
+    p_dec->p_sys = p_sys;
+    p_sys->b_packetizer = false;
+    p_sys->b_has_headers = false;
+    p_sys->i_pts = VLC_TS_INVALID;
+
+    /* Set output properties */
+    p_dec->fmt_out.i_cat = VIDEO_ES;
+    p_dec->fmt_out.i_codec = VLC_CODEC_RGBA;
+
+    /* Set up pseudo decoder structure for the image codec
+     * Yes, this is a hack but better than implementing our own decoding */
+    p_sys->p_img_dec = p_img_dec = vlc_object_create(p_dec, sizeof(decoder_t));
+    if (p_img_dec == NULL) {
+        free(p_sys);
+        return VLC_ENOMEM;
+    }
+
+    memcpy(&p_img_dec->fmt_in,  &p_dec->fmt_in,  sizeof(p_img_dec->fmt_in));
+    p_img_dec->fmt_in.i_codec = VLC_CODEC_PNG;
+    memcpy(&p_img_dec->fmt_out, &p_dec->fmt_out, sizeof(p_img_dec->fmt_out));
+    p_img_dec->b_frame_drop_allowed = p_dec->b_frame_drop_allowed;
+
+    p_img_dec->pf_vout_format_update = Forward_vout_format_update;
+    p_img_dec->pf_vout_buffer_new    = Forward_vout_buffer_new;
+    /* Initialisation of the picture decoder will happen in ProcessHeader()
+     * Then we know which decoder we actually need */
+
+    /* Set callbacks */
+    p_dec->pf_decode_video = (picture_t*(*)(decoder_t*, block_t**))
+        DecodeBlock;
+    p_dec->pf_packetize    = (block_t*(*)(decoder_t*, block_t**))
+        DecodeBlock;
+    p_dec->pf_flush        = Flush;
+
+    return VLC_SUCCESS;
+}
+
+static int OpenPacketizer(vlc_object_t* p_this)
+{
+    decoder_t* p_dec = (decoder_t*)p_this;
+
+    int i_ret = OpenDecoder(p_this);
+
+    if (i_ret == VLC_SUCCESS) {
+        p_dec->p_sys->b_packetizer = true;
+        p_dec->fmt_out.i_codec = VLC_CODEC_OGGSPOTS;
+    }
+
+    return i_ret;
+}
+
+/****************************************************************************
+ * DecodeBlock: the whole thing
+ ****************************************************************************
+ * This function must be fed with ogg packets.
+ ****************************************************************************/
+static void* DecodeBlock(decoder_t* p_dec, block_t** pp_block)
+{
+    decoder_sys_t* p_sys = p_dec->p_sys;
+
+    if (!pp_block || !*pp_block) return NULL;
+
+    /* Check for headers */
+    if (!p_sys->b_has_headers) {
+        if (ProcessHeader(p_dec)) {
+            block_Release(*pp_block);
+            return NULL;
+        }
+        p_sys->b_has_headers = true;
+    }
+
+    return ProcessPacket(p_dec, pp_block);
+}
+
+/*****************************************************************************
+ * ProcessHeader: process OggSpots header.
+ *****************************************************************************/
+static int ProcessHeader(decoder_t* p_dec)
+{
+    decoder_sys_t* p_sys = p_dec->p_sys;
+    decoder_t* p_img_dec = p_sys->p_img_dec;
+    const uint8_t* p_extra;
+    int i_major;
+    int i_minor;
+    uint64_t i_granulerate_numerator;
+    uint64_t i_granulerate_denominator;
+
+    /* The OggSpots header is always 52 bytes */
+    if (p_dec->fmt_in.i_extra != 52) {
+        return VLC_EGENERIC;
+    }
+    p_extra = p_dec->fmt_in.p_extra;
+
+    /* Identification string */
+    if ( memcmp(p_extra, "SPOTS\0\0\0", 8) ) {
+        return VLC_EGENERIC;
+    }
+
+    /* Version number */
+    i_major = GetWLE(&p_extra[ 8]); /* major version num */
+    i_minor = GetWLE(&p_extra[10]); /* minor version num */
+    if (i_major != 0 || i_minor != 1) {
+        return VLC_EGENERIC;
+    }
+
+    /* Granule rate */
+    i_granulerate_numerator   = GetQWLE(&p_extra[12]);
+    i_granulerate_denominator = GetQWLE(&p_extra[20]);
+    if (i_granulerate_numerator == 0 || i_granulerate_denominator == 0) {
+        return VLC_EGENERIC;
+    }
+
+    /* The OggSpots spec contained an error and there are implementations out
+     * there that used the wrong value. So we detect that case and switch
+     * numerator and denominator in that case */
+    if (i_granulerate_numerator == 1 && i_granulerate_denominator == 30) {
+        i_granulerate_numerator   = 30;
+        i_granulerate_denominator = 1;
+    }
+
+    /* Normalize granulerate */
+    vlc_ureduce(&p_dec->fmt_in.video.i_frame_rate,
+                &p_dec->fmt_in.video.i_frame_rate_base,
+                i_granulerate_numerator, i_granulerate_denominator, 0);
+
+    /* Image format */
+    if (!p_sys->b_packetizer) {
+        if ( !memcmp(&p_extra[32], "PNG", 3) ) {
+            p_img_dec->fmt_in.i_codec = VLC_CODEC_PNG;
+        }
+        else if ( !memcmp(&p_extra[32], "JPEG", 4) ) {
+            p_img_dec->fmt_in.i_codec = VLC_CODEC_JPEG;
+        }
+        else {
+            char psz_image_type[8+1];
+            strncpy(psz_image_type, (char*)&p_extra[32], 8);
+            psz_image_type[sizeof(psz_image_type)-1] = '\0';
+
+            msg_Warn(p_dec, "Unsupported image format: %s", psz_image_type);
+        }
+
+        /* Initialize image codec */
+        p_img_dec->p_module =
+                module_need(p_img_dec, "decoder", "any", false);
+        if (p_img_dec->p_module == NULL) {
+            msg_Err(p_dec, "Failed to initialize image codec");
+            return VLC_EGENERIC;
+        }
+        memcpy(&p_dec->fmt_out, &p_img_dec->fmt_out, sizeof(p_dec->fmt_out));
+    }
+
+    /* Dimensions */
+    p_dec->fmt_out.video.i_width  = p_dec->fmt_out.video.i_visible_width  =
+            GetWLE(&p_extra[40]);
+    p_dec->fmt_out.video.i_height = p_dec->fmt_out.video.i_visible_height =
+            GetWLE(&p_extra[42]);
+
+    /* We assume square pixels */
+    p_dec->fmt_out.video.i_sar_num = 1;
+    p_dec->fmt_out.video.i_sar_den = 1;
+
+    /* We don't implement background color, alignment and options at the
+     * moment because the former doesn't seem neccesary right now and the
+     * latter are underspecified. */
+
+    if (p_sys->b_packetizer) {
+        p_dec->fmt_out.i_extra = p_dec->fmt_in.i_extra;
+        p_dec->fmt_out.p_extra = xrealloc(p_dec->fmt_out.p_extra,
+                                          p_dec->fmt_out.i_extra);
+        memcpy(p_dec->fmt_out.p_extra,
+               p_dec->fmt_in.p_extra, p_dec->fmt_out.i_extra);
+    }
+
+    return VLC_SUCCESS;
+}
+
+/*****************************************************************************
+ * Flush:
+ *****************************************************************************/
+static void Flush(decoder_t* p_dec)
+{
+    decoder_sys_t* p_sys = p_dec->p_sys;
+
+    p_sys->i_pts = VLC_TS_INVALID;
+}
+
+/*****************************************************************************
+ * ProcessPacket: processes an OggSpots packet.
+ *****************************************************************************/
+static void* ProcessPacket(decoder_t* p_dec, block_t** pp_block)
+{
+    decoder_sys_t* p_sys = p_dec->p_sys;
+    block_t* p_block = *pp_block;
+    void* p_buf;
+
+    *pp_block = NULL; /* To avoid being fed the same packet again */
+
+    if (!p_block)
+        return NULL;
+
+    if ( (p_block->i_flags & BLOCK_FLAG_DISCONTINUITY) != 0 ) {
+        p_sys->i_pts = p_block->i_pts;
+    }
+
+    if ( (p_block->i_flags & BLOCK_FLAG_CORRUPTED) != 0 ) {
+        block_Release(p_block);
+        return NULL;
+    }
+
+    /* Date management */
+    if (p_block->i_pts > VLC_TS_INVALID && p_block->i_pts != p_sys->i_pts) {
+        p_sys->i_pts = p_block->i_pts;
+    }
+
+    if (p_sys->b_packetizer) {
+        /* Date management */
+        /* FIXME: This is copied from theora but it looks wrong.
+         * p_block->i_length will always be zero. */
+        p_block->i_dts = p_block->i_pts = p_sys->i_pts;
+
+        p_block->i_length = p_sys->i_pts - p_block->i_pts;
+
+        p_buf = p_block;
+    }
+    else {
+        p_buf = DecodePacket(p_dec, p_block);
+    }
+
+    ///* Date management */
+    //p_sys->i_pts += (CLOCK_FREQ * p_sys->ti.fps_denominator /
+    //                 p_sys->ti.fps_numerator ); /* 1 frame per packet */
+
+    return p_buf;
+}
+
+/*****************************************************************************
+ * DecodePacket: decodes an OggSpots packet.
+ *****************************************************************************/
+static picture_t* DecodePacket(decoder_t* p_dec, block_t* p_block)
+{
+    decoder_sys_t* p_sys = p_dec->p_sys;
+    decoder_t* p_img_dec = p_sys->p_img_dec;
+    uint32_t i_img_offset;
+    vlc_fourcc_t i_codec;
+    picture_t* p_pic;
+
+    if (p_block->i_buffer < 20) {
+        msg_Dbg(p_dec, "Packet too short");
+        goto error;
+    }
+
+    /* Byte offset */
+    i_img_offset = GetDWLE(p_block->p_buffer);
+    if (i_img_offset < 20) {
+        msg_Dbg(p_dec, "Invalid byte offset");
+        goto error;
+    }
+
+    /* Image format */
+    if ( !memcmp(&p_block->p_buffer[4], "PNG", 3) ) {
+        i_codec = VLC_CODEC_PNG;
+    }
+    else if ( !memcmp(&p_block->p_buffer[4], "JPEG", 4) ) {
+        i_codec = VLC_CODEC_JPEG;
+    }
+    else {
+        char psz_image_type[8+1];
+        strncpy(psz_image_type, (char*)&p_block->p_buffer[4], 8);
+        psz_image_type[sizeof(psz_image_type)-1] = '\0';
+
+        msg_Dbg(p_dec, "Unsupported image format: %s", psz_image_type);
+        goto error;
+    }
+
+    if (i_codec != p_img_dec->fmt_in.i_codec) {
+        msg_Dbg(p_dec, "Currently we support only one image format per stream");
+        goto error;
+    }
+
+    /* We currently ignore the rest of the header and let the image format
+     * handle the details */
+
+    p_block->i_buffer -= i_img_offset;
+    p_block->p_buffer += i_img_offset;
+
+    p_pic = p_img_dec->pf_decode_video(p_img_dec, &p_block);
+    if (p_pic != NULL) {
+        p_pic->b_force = true;
+    }
+    return p_pic;
+
+error:
+    block_Release(p_block);
+    return NULL;
+}
+
+/*****************************************************************************
+ * CloseDecoder: OggSpots decoder destruction
+ *****************************************************************************/
+static void CloseDecoder(vlc_object_t* p_this)
+{
+    decoder_t* p_dec = (decoder_t*)p_this;
+    decoder_sys_t* p_sys = p_dec->p_sys;
+    decoder_t* p_img_dec = p_sys->p_img_dec;
+
+    if (p_img_dec->p_module) {
+        module_unneed(p_img_dec, p_img_dec->p_module);
+    }
+
+    vlc_object_release(p_img_dec);
+    free(p_sys);
+}
+
+/*****************************************************************************
+ * Forward_vout_format_update: Forward calls to the pseudo decoder owner to
+ * our own decoder owner
+ *****************************************************************************/
+static int Forward_vout_format_update(decoder_t* p_pseudo_dec)
+{
+    decoder_t* p_dec = (decoder_t*)p_pseudo_dec->p_parent;
+    decoder_t* p_img_dec;
+    assert(p_dec != NULL);
+    p_img_dec = p_dec->p_sys->p_img_dec;
+
+    memcpy(&p_dec->fmt_out, &p_img_dec->fmt_out, sizeof(p_dec->fmt_out));
+
+    if (p_dec->pf_vout_format_update == NULL) {
+        return -1;
+    }
+    return p_dec->pf_vout_format_update(p_dec);
+}
+
+/*****************************************************************************
+ * Forward_vout_buffer_new: Forward calls to the pseudo decoder owner to
+ * our own decoder owner
+ *****************************************************************************/
+static picture_t* Forward_vout_buffer_new(decoder_t* p_pseudo_dec)
+{
+    decoder_t* p_dec = (decoder_t*)p_pseudo_dec->p_parent;
+    assert(p_dec != NULL);
+
+    if (p_dec->pf_vout_buffer_new == NULL) {
+        return NULL;
+    }
+    return p_dec->pf_vout_buffer_new(p_dec);
+}
-- 
2.5.0



More information about the vlc-devel mailing list