bpg.c 4.78 KB
Newer Older
Tristan Matthews's avatar
Tristan Matthews committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
/*****************************************************************************
 * bpg.c: bpg decoder module using libbpg.
 *****************************************************************************
 * Copyright (C) 2015 VLC authors and VideoLAN
 *
 * Author: Tristan Matthews <tmatth@videolan.org>
 *
 * 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.
 *****************************************************************************/

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <vlc_common.h>
#include <vlc_plugin.h>
#include <vlc_codec.h>
#include <libbpg.h>

struct decoder_sys_t
{
    struct BPGDecoderContext *p_bpg;
};

static int  OpenDecoder(vlc_object_t *);
static void CloseDecoder(vlc_object_t *);

static picture_t *DecodeBlock(decoder_t *, block_t **);

/*
 * Module descriptor
 */
vlc_module_begin()
    set_category( CAT_INPUT )
    set_subcategory( SUBCAT_INPUT_VCODEC )
    /* decoder main module */
    set_description( N_("BPG image decoder") )
    set_capability( "decoder", 60 )
    set_callbacks( OpenDecoder, CloseDecoder )
    add_shortcut( "bpg" )
vlc_module_end()


static int OpenDecoder(vlc_object_t *p_this)
{
    decoder_t *p_dec = (decoder_t *)p_this;

    if( p_dec->fmt_in.i_codec != VLC_CODEC_BPG )
    {
        return VLC_EGENERIC;
    }

    decoder_sys_t *p_sys = malloc( sizeof( decoder_sys_t ) );
    if( p_sys == NULL )
    {
        return VLC_ENOMEM;
    }

    p_dec->p_sys = p_sys;

    p_sys->p_bpg = bpg_decoder_open();
    if( !p_sys->p_bpg )
    {
        return VLC_EGENERIC;
    }

    /* Set output properties */
    p_dec->fmt_out.i_cat = VIDEO_ES;

    /* Set callbacks */
    p_dec->pf_decode_video = DecodeBlock;

    return VLC_SUCCESS;
}

/*
 * This function must be fed with a complete compressed frame.
 */
static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
{
    decoder_sys_t *p_sys = p_dec->p_sys;
    block_t *p_block;
    picture_t *p_pic = 0;
    BPGImageInfo img_info;

    if( !pp_block || !*pp_block )
99
    {
Tristan Matthews's avatar
Tristan Matthews committed
100
        return NULL;
101
    }
Tristan Matthews's avatar
Tristan Matthews committed
102 103 104

    p_block = *pp_block;

105
    if( p_block->i_flags & BLOCK_FLAG_DISCONTINUITY )
106 107 108 109 110
    {
        block_Release(p_block);
        *pp_block = NULL;
        return NULL;
    }
Tristan Matthews's avatar
Tristan Matthews committed
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165

    /* Decode picture */

    if( bpg_decoder_decode( p_sys->p_bpg,
                            p_block->p_buffer,
                            p_block->i_buffer ) < 0 )
    {
        msg_Err( p_dec, "Could not decode block" );
        goto error;
    }

    if( bpg_decoder_get_info( p_sys->p_bpg, &img_info ) )
    {
        msg_Err( p_dec, "Could not get info for decoder" );
        goto error;
    }

    if( bpg_decoder_start( p_sys->p_bpg, BPG_OUTPUT_FORMAT_RGB24 ) )
    {
        msg_Err( p_dec, "Could not start decoder" );
        goto error;
    }

    /* Set output properties */
    p_dec->fmt_out.i_codec = VLC_CODEC_RGB24;
    p_dec->fmt_out.video.i_visible_width  = p_dec->fmt_out.video.i_width  = img_info.width;
    p_dec->fmt_out.video.i_visible_height = p_dec->fmt_out.video.i_height = img_info.height;
    p_dec->fmt_out.video.i_sar_num = 1;
    p_dec->fmt_out.video.i_sar_den = 1;
    p_dec->fmt_out.video.i_rmask = 0x000000ff;
    p_dec->fmt_out.video.i_gmask = 0x0000ff00;
    p_dec->fmt_out.video.i_bmask = 0x00ff0000;

    /* Get a new picture */
    p_pic = decoder_NewPicture( p_dec );
    if( !p_pic )
    {
        goto error;
    }

    const int img_height = img_info.height;
    for (int i = 0; i < img_height; i++)
    {
        if( bpg_decoder_get_line( p_sys->p_bpg,
                                  p_pic->p->p_pixels + p_pic->p->i_pitch * i )
                                  < 0 )
        {
            msg_Err( p_dec, "Could not decode line" );
            goto error;
        }
    }

    p_pic->date = p_block->i_pts > VLC_TS_INVALID ? p_block->i_pts : p_block->i_dts;

    block_Release( p_block );
166 167
    *pp_block = NULL;

Tristan Matthews's avatar
Tristan Matthews committed
168 169 170
    return p_pic;

error:
171

Tristan Matthews's avatar
Tristan Matthews committed
172
    block_Release( p_block );
173 174
    *pp_block = NULL;

Tristan Matthews's avatar
Tristan Matthews committed
175 176 177 178 179 180 181 182 183 184 185 186 187
    return NULL;
}

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;

    if( p_sys->p_bpg )
        bpg_decoder_close( p_sys->p_bpg );

    free( p_sys );
}