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

Jean-Baptiste Kempf jb at videolan.org
Sun Feb 28 22:22:34 CET 2016


On 28 Feb, Michael Tänzer wrote :
> ---
>  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

Missing NEWS entry.

> +++ b/modules/codec/oggspots.c
> + * Copyright (C) 1999-2012 VLC authors and VideoLAN

Wrong date.

> +#include <vlc_common.h>
> +#include <vlc_plugin.h>
> +#include <vlc_modules.h>
> +#include <vlc_codec.h>
> +#include <vlc_sout.h>

sout?

> +#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;
> +    }

Are you sure you cannot use image_* functions?

> +/****************************************************************************
> + * 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. */
> +

Typo on necessary ?

> +    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);
> +}

With my kindest regards,

-- 
Jean-Baptiste Kempf
http://www.jbkempf.com/ - +33 672 704 734
Sent from my Electronic Device


More information about the vlc-devel mailing list