spudec.c 8.5 KB
Newer Older
1
/*****************************************************************************
2
 * spudec.c : SPU decoder thread
3
 *****************************************************************************
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
4
 * Copyright (C) 2000-2001, 2006 VLC authors and VideoLAN
5
 * $Id$
6
 *
7
 * Authors: Sam Hocevar <sam@zoy.org>
Laurent Aimar's avatar
Laurent Aimar committed
8
 *          Laurent Aimar <fenrir@via.ecp.fr>
9
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
10 11 12
 * 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
13
 * (at your option) any later version.
Laurent Aimar's avatar
Laurent Aimar committed
14
 *
15 16
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
17 18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
19
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
20 21 22
 * 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.
23 24 25 26 27
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
28 29 30 31
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

32
#include <vlc_common.h>
33
#include <vlc_plugin.h>
Clément Stenac's avatar
Clément Stenac committed
34
#include <vlc_codec.h>
35 36 37 38 39 40

#include "spudec.h"

/*****************************************************************************
 * Module descriptor.
 *****************************************************************************/
Laurent Aimar's avatar
Laurent Aimar committed
41 42
static int  DecoderOpen   ( vlc_object_t * );
static int  PacketizerOpen( vlc_object_t * );
43
static void Close         ( vlc_object_t * );
Laurent Aimar's avatar
Laurent Aimar committed
44

45 46 47 48
#define DVDSUBTRANS_DISABLE_TEXT N_("Disable DVD subtitle transparency")
#define DVDSUBTRANS_DISABLE_LONGTEXT N_("Removes all transparency effects " \
                                        "used in DVD subtitles.")

49 50
vlc_module_begin ()
    set_description( N_("DVD subtitles decoder") )
51
    set_shortname( N_("DVD subtitles") )
52
    set_capability( "spu decoder", 75 )
53 54 55
    set_category( CAT_INPUT )
    set_subcategory( SUBCAT_INPUT_SCODEC )
    set_callbacks( DecoderOpen, Close )
Laurent Aimar's avatar
Laurent Aimar committed
56

57
    add_bool( "dvdsub-transparency", false,
58
              DVDSUBTRANS_DISABLE_TEXT, DVDSUBTRANS_DISABLE_LONGTEXT, true )
59 60 61 62 63
    add_submodule ()
    set_description( N_("DVD subtitles packetizer") )
    set_capability( "packetizer", 50 )
    set_callbacks( PacketizerOpen, Close )
vlc_module_end ()
64 65

/*****************************************************************************
Laurent Aimar's avatar
Laurent Aimar committed
66 67
 * Local prototypes
 *****************************************************************************/
68 69
static block_t *      Reassemble( decoder_t *, block_t * );
static int            Decode    ( decoder_t *, block_t * );
70
static block_t *      Packetize ( decoder_t *, block_t ** );
Laurent Aimar's avatar
Laurent Aimar committed
71 72 73

/*****************************************************************************
 * DecoderOpen
74
 *****************************************************************************
Laurent Aimar's avatar
Laurent Aimar committed
75
 * Tries to launch a decoder and return score so that the interface is able
76 77
 * to chose.
 *****************************************************************************/
Laurent Aimar's avatar
Laurent Aimar committed
78
static int DecoderOpen( vlc_object_t *p_this )
79
{
Laurent Aimar's avatar
Laurent Aimar committed
80 81
    decoder_t     *p_dec = (decoder_t*)p_this;
    decoder_sys_t *p_sys;
82

83
    if( p_dec->fmt_in.i_codec != VLC_CODEC_SPU )
84
        return VLC_EGENERIC;
85

Laurent Aimar's avatar
Laurent Aimar committed
86 87
    p_dec->p_sys = p_sys = malloc( sizeof( decoder_sys_t ) );

88
    p_sys->b_packetizer = false;
89
    p_sys->b_disabletrans = var_InheritBool( p_dec, "dvdsub-transparency" );
Laurent Aimar's avatar
Laurent Aimar committed
90 91 92 93
    p_sys->i_spu_size = 0;
    p_sys->i_spu      = 0;
    p_sys->p_block    = NULL;

94
    p_dec->fmt_out.i_codec = VLC_CODEC_SPU;
Laurent Aimar's avatar
Laurent Aimar committed
95

96 97
    p_dec->pf_decode    = Decode;
    p_dec->pf_packetize = NULL;
98 99

    return VLC_SUCCESS;
100 101 102
}

/*****************************************************************************
Laurent Aimar's avatar
Laurent Aimar committed
103 104 105 106
 * PacketizerOpen
 *****************************************************************************
 * Tries to launch a decoder and return score so that the interface is able
 * to chose.
107
 *****************************************************************************/
Laurent Aimar's avatar
Laurent Aimar committed
108
static int PacketizerOpen( vlc_object_t *p_this )
109
{
Laurent Aimar's avatar
Laurent Aimar committed
110
    decoder_t *p_dec = (decoder_t*)p_this;
111

Laurent Aimar's avatar
Laurent Aimar committed
112
    if( DecoderOpen( p_this ) )
113
    {
Laurent Aimar's avatar
Laurent Aimar committed
114
        return VLC_EGENERIC;
115
    }
116
    p_dec->pf_packetize  = Packetize;
117
    p_dec->p_sys->b_packetizer = true;
118
    es_format_Copy( &p_dec->fmt_out, &p_dec->fmt_in );
119
    p_dec->fmt_out.i_codec = VLC_CODEC_SPU;
Laurent Aimar's avatar
Laurent Aimar committed
120 121 122 123 124 125 126 127 128 129 130

    return VLC_SUCCESS;
}

/*****************************************************************************
 * Close:
 *****************************************************************************/
static void Close( vlc_object_t *p_this )
{
    decoder_t     *p_dec = (decoder_t*)p_this;
    decoder_sys_t *p_sys = p_dec->p_sys;
131

132 133 134 135 136
    if( p_sys->p_block )
    {
        block_ChainRelease( p_sys->p_block );
    }

Laurent Aimar's avatar
Laurent Aimar committed
137 138
    free( p_sys );
}
139

Laurent Aimar's avatar
Laurent Aimar committed
140 141 142
/*****************************************************************************
 * Decode:
 *****************************************************************************/
143
static int Decode( decoder_t *p_dec, block_t *p_block )
Laurent Aimar's avatar
Laurent Aimar committed
144 145
{
    decoder_sys_t *p_sys = p_dec->p_sys;
146
    block_t       *p_spu_block;
147
    subpicture_t  *p_spu;
Laurent Aimar's avatar
Laurent Aimar committed
148

149 150 151
    if( p_block == NULL ) /* No Drain */
        return VLCDEC_SUCCESS;
    p_spu_block = Reassemble( p_dec, p_block );
Laurent Aimar's avatar
Laurent Aimar committed
152

153 154
    if( ! p_spu_block )
    {
155
        return VLCDEC_SUCCESS;
156
    }
157

158 159 160 161
    /* FIXME: what the, we shouldn’t need to allocate 64k of buffer --sam. */
    p_sys->i_spu = block_ChainExtract( p_spu_block, p_sys->buffer, 65536 );
    p_sys->i_pts = p_spu_block->i_pts;
    block_ChainRelease( p_spu_block );
Laurent Aimar's avatar
Laurent Aimar committed
162

163
    /* Parse and decode */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
164
    p_spu = ParsePacket( p_dec );
165

166 167 168 169 170
    /* reinit context */
    p_sys->i_spu_size = 0;
    p_sys->i_rle_size = 0;
    p_sys->i_spu      = 0;
    p_sys->p_block    = NULL;
171

172 173 174
    if( p_spu != NULL )
        decoder_QueueSub( p_dec, p_spu );
    return VLCDEC_SUCCESS;
Laurent Aimar's avatar
Laurent Aimar committed
175 176 177 178 179 180 181 182
}

/*****************************************************************************
 * Packetize:
 *****************************************************************************/
static block_t *Packetize( decoder_t *p_dec, block_t **pp_block )
{
    decoder_sys_t *p_sys = p_dec->p_sys;
183 184 185 186 187 188 189
    if( pp_block == NULL ) /* No Drain */
        return NULL;
    block_t *p_block = *pp_block; *pp_block = NULL;
    if( p_block == NULL )
        return NULL;

    block_t *p_spu = Reassemble( p_dec, p_block );
190

191
    if( ! p_spu )
192
    {
193 194
        return NULL;
    }
195

196 197
    p_spu->i_dts = p_spu->i_pts;
    p_spu->i_length = 0;
198

199 200 201 202 203
    /* reinit context */
    p_sys->i_spu_size = 0;
    p_sys->i_rle_size = 0;
    p_sys->i_spu      = 0;
    p_sys->p_block    = NULL;
204

205
    return block_ChainGather( p_spu );
206 207 208
}

/*****************************************************************************
Laurent Aimar's avatar
Laurent Aimar committed
209
 * Reassemble:
210
 *****************************************************************************/
211
static block_t *Reassemble( decoder_t *p_dec, block_t *p_block )
212
{
Laurent Aimar's avatar
Laurent Aimar committed
213 214
    decoder_sys_t *p_sys = p_dec->p_sys;

215 216 217 218 219 220
    if( p_block->i_flags & BLOCK_FLAG_CORRUPTED )
    {
        block_Release( p_block );
        return NULL;
    }

221
    if( p_sys->i_spu_size <= 0 &&
222
        ( p_block->i_pts <= VLC_TS_INVALID || p_block->i_buffer < 4 ) )
223
    {
Laurent Aimar's avatar
Laurent Aimar committed
224
        msg_Dbg( p_dec, "invalid starting packet (size < 4 or pts <=0)" );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
225
        msg_Dbg( p_dec, "spu size: %d, i_pts: %"PRId64" i_buffer: %zu",
226
                 p_sys->i_spu_size, p_block->i_pts, p_block->i_buffer );
Laurent Aimar's avatar
Laurent Aimar committed
227 228
        block_Release( p_block );
        return NULL;
229
    }
Laurent Aimar's avatar
Laurent Aimar committed
230 231 232 233 234 235

    block_ChainAppend( &p_sys->p_block, p_block );
    p_sys->i_spu += p_block->i_buffer;

    if( p_sys->i_spu_size <= 0 )
    {
236 237 238 239
        p_sys->i_spu_size = ( p_block->p_buffer[0] << 8 )|
            p_block->p_buffer[1];
        p_sys->i_rle_size = ( ( p_block->p_buffer[2] << 8 )|
            p_block->p_buffer[3] ) - 4;
Laurent Aimar's avatar
Laurent Aimar committed
240

241 242
        /* msg_Dbg( p_dec, "i_spu_size=%d i_rle=%d",
                    p_sys->i_spu_size, p_sys->i_rle_size ); */
243

Laurent Aimar's avatar
Laurent Aimar committed
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
        if( p_sys->i_spu_size <= 0 || p_sys->i_rle_size >= p_sys->i_spu_size )
        {
            p_sys->i_spu_size = 0;
            p_sys->i_rle_size = 0;
            p_sys->i_spu      = 0;
            p_sys->p_block    = NULL;

            block_Release( p_block );
            return NULL;
        }
    }

    if( p_sys->i_spu >= p_sys->i_spu_size )
    {
        /* We have a complete sub */
259 260 261
        if( p_sys->i_spu > p_sys->i_spu_size )
            msg_Dbg( p_dec, "SPU packets size=%d should be %d",
                     p_sys->i_spu, p_sys->i_spu_size );
262 263

        return p_sys->p_block;
Laurent Aimar's avatar
Laurent Aimar committed
264 265
    }
    return NULL;
266
}