csri.c 10.5 KB
Newer Older
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
/*****************************************************************************
 * csri.c : Load CSRI subtitle renderer
 *****************************************************************************
 * Copyright (C) 2007 the VideoLAN team
 * $Id$
 *
 * Authors: David Lamparter <equinox@videolan.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 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 General Public License for more details.
 *
 * You should have received a copy of the GNU 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
 *****************************************************************************/
27 28 29 30 31

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

32
#include <string.h>
Laurent Aimar's avatar
Laurent Aimar committed
33
#include <math.h>
34

35
#include <vlc_common.h>
36
#include <vlc_plugin.h>
37 38 39
#include <vlc_vout.h>
#include <vlc_codec.h>
#include <vlc_osd.h>
40
#include <vlc_input.h>
41 42 43 44 45

#include <csri/csri.h>
#include <csri/stream.h>

/*****************************************************************************
46
 * Module descriptor
47 48 49 50 51
 *****************************************************************************/
static int  Create ( vlc_object_t * );
static void Destroy( vlc_object_t * );

vlc_module_begin();
52 53
    set_shortname( N_("Subtitles (advanced)"));
    set_description( N_("Wrapper for subtitle renderers using CSRI/asa") );
54 55 56 57 58 59 60
    set_capability( "decoder", 60 );
    set_category( CAT_INPUT );
    set_subcategory( SUBCAT_INPUT_SCODEC );
    set_callbacks( Create, Destroy );
vlc_module_end();

/*****************************************************************************
61 62 63 64
 * Local prototypes
 *****************************************************************************/
static subpicture_t *DecodeBlock( decoder_t *, block_t ** );
static void DestroySubpicture( subpicture_t * );
65 66 67
static void PreRender( spu_t *, subpicture_t *, const video_format_t * );
static void UpdateRegions( spu_t *, subpicture_t *,
                           const video_format_t *,  mtime_t );
68 69 70

/*****************************************************************************
 * decoder_sys_t: decoder data
71 72 73 74 75 76
 *****************************************************************************/
struct decoder_sys_t
{
    subpicture_t *p_spu_final;
    video_format_t fmt_cached;
    csri_inst *p_instance;
77 78 79

    struct csri_stream_ext *p_stream_ext;

80 81 82 83 84 85 86 87
    void (*pf_push_packet)(csri_inst *inst, const void *packet,
                           size_t packetlen, double pts_start,
                           double pts_end);
};

struct subpicture_sys_t
{
    decoder_t *p_dec;
88 89
    void      *p_subs_data;
    int       i_subs_len;
Laurent Aimar's avatar
Laurent Aimar committed
90
    mtime_t   i_pts;
91 92 93 94 95 96 97 98 99 100 101 102
};

/*****************************************************************************
 * Create: Open CSRI renderer
 *****************************************************************************
 * Comment me.
 *****************************************************************************/
static int Create( vlc_object_t *p_this )
{
    decoder_t *p_dec = (decoder_t *)p_this;
    decoder_sys_t *p_sys;
    csri_rend *p_render;
103
    struct csri_stream_ext *p_stream_ext;
104 105 106 107 108 109 110 111 112 113

    if( p_dec->fmt_in.i_codec != VLC_FOURCC('s','s','a',' ') )
        return VLC_EGENERIC;

    p_render = csri_renderer_default();
    if (!p_render)
    {
        msg_Err( p_dec, "can't load csri renderer" );
        return VLC_EGENERIC;
    }
114 115
    p_stream_ext = csri_query_ext(p_render, CSRI_EXT_STREAM_ASS);
    if (!p_stream_ext)
116 117 118 119 120 121 122 123 124 125
    {
        msg_Err( p_dec, "csri renderer does not support ASS streaming" );
        return VLC_EGENERIC;
    }

    p_dec->pf_decode_sub = DecodeBlock;

    p_dec->p_sys = p_sys = malloc( sizeof( decoder_sys_t ) );
    if( !p_sys )
        return VLC_ENOMEM;
126 127 128 129 130 131 132 133
    memset( &p_sys->fmt_cached, 0, sizeof( p_sys->fmt_cached ) );

    p_sys->p_stream_ext = p_stream_ext;
    p_sys->pf_push_packet = p_stream_ext->push_packet;
    p_sys->p_instance = p_stream_ext->init_stream( p_render,
                                                   p_dec->fmt_in.p_extra,
                                                   p_dec->fmt_in.p_extra ? strnlen( p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra ) : 0,
                                                   NULL);
134 135 136 137 138 139 140 141 142 143
    return VLC_SUCCESS;
}

/*****************************************************************************
 * Destroy: finish
 *****************************************************************************
 * Comment me.
 *****************************************************************************/
static void Destroy( vlc_object_t *p_this )
{
144 145
    decoder_t *p_dec = (decoder_t *)p_this;
    decoder_sys_t *p_sys = p_dec->p_sys;
146

147 148
    if( p_sys->p_stream_ext->discard )
        p_sys->p_stream_ext->discard( p_sys->p_instance, true );
149 150 151 152 153 154 155 156 157 158
    free( p_sys );
}

/****************************************************************************
 * DecodeBlock: the whole thing
 ****************************************************************************
 * This function must be fed with complete subtitles units.
 ****************************************************************************/
static subpicture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
{
159 160
    decoder_sys_t *p_sys = p_dec->p_sys;

161 162 163
    subpicture_t *p_spu = NULL;
    block_t *p_block;

164 165 166 167
    //msg_Dbg( p_dec, "DecodeBlock %p", (void *)pp_block);
    if( !pp_block || *pp_block == NULL )
        return NULL;

168
    p_block = *pp_block;
169 170 171 172 173 174 175
    *pp_block = NULL;

    if( p_block->i_buffer == 0 || p_block->p_buffer[0] == '\0' )
    {
        block_Release( p_block );
        return NULL;
    }
176 177 178 179 180

    p_spu = p_dec->pf_spu_buffer_new( p_dec );
    if( !p_spu )
    {
        msg_Warn( p_dec, "can't get spu buffer" );
181
        block_Release( p_block );
182 183 184 185 186 187 188
        return NULL;
    }

    p_spu->p_sys = malloc( sizeof( subpicture_sys_t ));
    if( !p_spu->p_sys )
    {
        p_dec->pf_spu_buffer_del( p_dec, p_spu );
189
        block_Release( p_block );
190 191 192 193 194 195 196 197 198 199
        return NULL;
    }
    p_spu->p_sys->p_dec = p_dec;

    p_spu->p_sys->i_subs_len = p_block->i_buffer;
    p_spu->p_sys->p_subs_data = malloc( p_block->i_buffer );
    if( !p_spu->p_sys->p_subs_data )
    {
        free( p_spu->p_sys );
        p_dec->pf_spu_buffer_del( p_dec, p_spu );
200
        block_Release( p_block );
201 202 203 204
        return NULL;
    }
    memcpy( p_spu->p_sys->p_subs_data, p_block->p_buffer,
            p_block->i_buffer );
Laurent Aimar's avatar
Laurent Aimar committed
205
    p_spu->p_sys->i_pts = p_block->i_pts;
206 207 208

    p_spu->i_start = p_block->i_pts;
    p_spu->i_stop = p_block->i_pts + p_block->i_length;
209 210
    p_spu->b_ephemer = false;
    p_spu->b_absolute = false;
211

212 213 214 215 216
    //msg_Dbg( p_dec, "BS %lf..%lf", p_spu->i_start * 0.000001, p_spu->i_stop * 0.000001);
    p_sys->pf_push_packet( p_sys->p_instance,
                           p_spu->p_sys->p_subs_data, p_spu->p_sys->i_subs_len,
                           p_spu->i_start * 0.000001,
                           p_spu->i_stop * 0.000001);
217 218 219 220 221

    p_spu->pf_pre_render = PreRender;
    p_spu->pf_update_regions = UpdateRegions;
    p_spu->pf_destroy = DestroySubpicture;

222
    block_Release( p_block );
223 224 225 226 227 228

    return p_spu;
}

static void DestroySubpicture( subpicture_t *p_subpic )
{
229
    //msg_Dbg( p_subpic->p_sys->p_dec, "drop spu %p", (void *)p_subpic );
230 231 232 233
    free( p_subpic->p_sys->p_subs_data );
    free( p_subpic->p_sys );
}

234 235
static void PreRender( spu_t *p_spu, subpicture_t *p_subpic,
                       const video_format_t *p_fmt )
236 237 238
{
    decoder_t *p_dec = p_subpic->p_sys->p_dec;
    p_dec->p_sys->p_spu_final = p_subpic;
239 240
    VLC_UNUSED(p_fmt);
    VLC_UNUSED(p_spu);
241 242
}

243
static void UpdateRegions( spu_t *p_spu, subpicture_t *p_subpic,
244
                           const video_format_t *p_fmt, mtime_t i_ts )
245 246
{
    decoder_t *p_dec = p_subpic->p_sys->p_dec;
247 248
    decoder_sys_t *p_sys = p_dec->p_sys;

249 250 251
    subpicture_region_t *p_spu_region;
    video_format_t fmt;

252
    /* TODO maybe checking if we really need redrawing */
253
    subpicture_region_ChainDelete( p_subpic->p_region );
254 255 256
    p_subpic->p_region = NULL;

    /* FIXME check why this is needed */
257
    if( p_subpic != p_sys->p_spu_final )
258
        return;
259

260 261 262 263 264 265
#if 0
    msg_Warn( p_dec, "---- fmt: %dx%d %dx%d chroma=%4.4s",
            p_fmt->i_width, p_fmt->i_height,
            p_fmt->i_visible_width, p_fmt->i_visible_height,
            (const char*)&p_fmt->i_chroma );
#endif
266 267
    /* XXX On x86 at least our RGBA is mapped to their BGRA
     * TODO confirm that is the same on big endian cpu */
268
    fmt = *p_fmt;
269 270 271 272 273
    fmt.i_chroma = VLC_FOURCC('R','G','B','A');
    fmt.i_width = fmt.i_visible_width;
    fmt.i_height = fmt.i_visible_height;
    fmt.i_bits_per_pixel = 0;
    fmt.i_x_offset = fmt.i_y_offset = 0;
274 275
    fmt.i_sar_num = 1;
    fmt.i_sar_den = 1;
276

277
    if( memcmp(&fmt, &p_sys->fmt_cached, sizeof(fmt)) )
278
    {
279 280
        //msg_Warn( p_dec, "---- fmt: new %dx%d", fmt.i_width, fmt.i_height );

281 282
        struct csri_fmt csri_fmt;
        memset(&csri_fmt, 0, sizeof(csri_fmt));
283
        csri_fmt.pixfmt = CSRI_F_BGRA;
284 285
        csri_fmt.width = fmt.i_width;
        csri_fmt.height = fmt.i_height;
286
        if( csri_request_fmt( p_sys->p_instance, &csri_fmt ) )
287 288
            msg_Dbg( p_dec, "csri error: format not supported" );

289
        p_sys->fmt_cached = fmt;
290 291
    }

292 293
    p_subpic->i_original_picture_height = fmt.i_height;
    p_subpic->i_original_picture_width = fmt.i_width;
294

295
    p_spu_region = p_subpic->p_region = subpicture_region_New( &fmt );
296

297 298 299 300 301 302
    if( p_spu_region )
    {
        struct csri_frame csri_frame;

        /* */
        p_spu_region->i_align = SUBPICTURE_ALIGN_TOP | SUBPICTURE_ALIGN_LEFT;
303
        memset( p_spu_region->p_picture->Y_PIXELS, 0x00, p_spu_region->p_picture->Y_PITCH * p_sys->fmt_cached.i_height );
304 305

        /* */
Laurent Aimar's avatar
Laurent Aimar committed
306 307
        const mtime_t i_stream_date = p_subpic->p_sys->i_pts + (i_ts - p_subpic->i_start);

308 309
        //msg_Dbg( p_dec, "TS %lf", ts * 0.000001 );
        memset( &csri_frame, 0, sizeof(csri_frame) );
310
        csri_frame.pixfmt = CSRI_F_BGRA;
311 312
        csri_frame.planes[0] = (unsigned char*)p_spu_region->p_picture->Y_PIXELS;
        csri_frame.strides[0] = p_spu_region->p_picture->Y_PITCH;
Laurent Aimar's avatar
Laurent Aimar committed
313
        csri_render( p_sys->p_instance, &csri_frame, i_stream_date * 0.000001 );
314
    }
315 316
}