]> git.sesse.net Git - vlc/blob - modules/codec/spudec/spudec.c
Improvements to preferences
[vlc] / modules / codec / spudec / spudec.c
1 /*****************************************************************************
2  * spudec.c : SPU decoder thread
3  *****************************************************************************
4  * Copyright (C) 2000-2001 VideoLAN
5  * $Id$
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *          Laurent Aimar <fenrir@via.ecp.fr>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <vlc/vlc.h>
29 #include <vlc/decoder.h>
30
31 #include "spudec.h"
32
33 /*****************************************************************************
34  * Module descriptor.
35  *****************************************************************************/
36 static int  DecoderOpen   ( vlc_object_t * );
37 static int  PacketizerOpen( vlc_object_t * );
38 static void Close         ( vlc_object_t * );
39
40 vlc_module_begin();
41     set_description( _("DVD subtitles decoder") );
42     set_capability( "decoder", 50 );
43     set_category( CAT_INPUT );
44     set_subcategory( SUBCAT_INPUT_SCODEC );
45     set_callbacks( DecoderOpen, Close );
46
47     add_submodule();
48     set_description( _("DVD subtitles packetizer") );
49     set_capability( "packetizer", 50 );
50     set_callbacks( PacketizerOpen, Close );
51 vlc_module_end();
52
53 /*****************************************************************************
54  * Local prototypes
55  *****************************************************************************/
56 static block_t *      Reassemble( decoder_t *, block_t ** );
57 static subpicture_t * Decode    ( decoder_t *, block_t ** );
58 static block_t *      Packetize ( decoder_t *, block_t ** );
59
60 /*****************************************************************************
61  * DecoderOpen
62  *****************************************************************************
63  * Tries to launch a decoder and return score so that the interface is able
64  * to chose.
65  *****************************************************************************/
66 static int DecoderOpen( vlc_object_t *p_this )
67 {
68     decoder_t     *p_dec = (decoder_t*)p_this;
69     decoder_sys_t *p_sys;
70
71     if( p_dec->fmt_in.i_codec != VLC_FOURCC( 's','p','u',' ' ) &&
72         p_dec->fmt_in.i_codec != VLC_FOURCC( 's','p','u','b' ) )
73     {
74         return VLC_EGENERIC;
75     }
76
77     p_dec->p_sys = p_sys = malloc( sizeof( decoder_sys_t ) );
78
79     p_sys->b_packetizer = VLC_FALSE;
80     p_sys->i_spu_size = 0;
81     p_sys->i_spu      = 0;
82     p_sys->p_block    = NULL;
83
84     es_format_Init( &p_dec->fmt_out, SPU_ES, VLC_FOURCC( 's','p','u',' ' ) );
85
86     p_dec->pf_decode_sub = Decode;
87     p_dec->pf_packetize  = NULL;
88
89     return VLC_SUCCESS;
90 }
91
92 /*****************************************************************************
93  * PacketizerOpen
94  *****************************************************************************
95  * Tries to launch a decoder and return score so that the interface is able
96  * to chose.
97  *****************************************************************************/
98 static int PacketizerOpen( vlc_object_t *p_this )
99 {
100     decoder_t *p_dec = (decoder_t*)p_this;
101
102     if( DecoderOpen( p_this ) )
103     {
104         return VLC_EGENERIC;
105     }
106     p_dec->pf_packetize  = Packetize;
107     p_dec->p_sys->b_packetizer = VLC_TRUE;
108     es_format_Copy( &p_dec->fmt_out, &p_dec->fmt_in );
109     p_dec->fmt_out.i_codec = VLC_FOURCC( 's','p','u',' ' );
110
111     return VLC_SUCCESS;
112 }
113
114 /*****************************************************************************
115  * Close:
116  *****************************************************************************/
117 static void Close( vlc_object_t *p_this )
118 {
119     decoder_t     *p_dec = (decoder_t*)p_this;
120     decoder_sys_t *p_sys = p_dec->p_sys;
121
122     if( p_sys->p_block ) block_ChainRelease( p_sys->p_block );
123     free( p_sys );
124 }
125
126 /*****************************************************************************
127  * Decode:
128  *****************************************************************************/
129 static subpicture_t *Decode( decoder_t *p_dec, block_t **pp_block )
130 {
131     decoder_sys_t *p_sys = p_dec->p_sys;
132     block_t       *p_spu_block;
133
134     if( (p_spu_block = Reassemble( p_dec, pp_block )) )
135     {
136         subpicture_t *p_spu;
137
138         p_sys->i_spu = block_ChainExtract( p_spu_block, p_sys->buffer, 65536 );
139         p_sys->i_pts = p_spu_block->i_pts;
140         block_ChainRelease( p_spu_block );
141
142         /* Parse and decode */
143         p_spu = E_(ParsePacket)( p_dec );
144
145         /* reinit context */
146         p_sys->i_spu_size = 0;
147         p_sys->i_rle_size = 0;
148         p_sys->i_spu      = 0;
149         p_sys->p_block    = NULL;
150
151         return p_spu;
152     }
153
154     return NULL;
155 }
156
157 /*****************************************************************************
158  * Packetize:
159  *****************************************************************************/
160 static block_t *Packetize( decoder_t *p_dec, block_t **pp_block )
161 {
162     decoder_sys_t *p_sys = p_dec->p_sys;
163     block_t       *p_spu = Reassemble( p_dec, pp_block );
164
165     if( p_spu )
166     {
167         p_spu->i_dts = p_spu->i_pts;
168         p_spu->i_length = 0;
169
170         /* reinit context */
171         p_sys->i_spu_size = 0;
172         p_sys->i_rle_size = 0;
173         p_sys->i_spu      = 0;
174         p_sys->p_block    = NULL;
175
176         return block_ChainGather( p_spu );
177     }
178
179     return NULL;
180 }
181
182 /*****************************************************************************
183  * Reassemble:
184  *****************************************************************************/
185 static block_t *Reassemble( decoder_t *p_dec, block_t **pp_block )
186 {
187     decoder_sys_t *p_sys = p_dec->p_sys;
188     block_t *p_block;
189
190     if( pp_block == NULL || *pp_block == NULL ) return NULL;
191     p_block = *pp_block;
192     *pp_block = NULL;
193
194     if( p_sys->i_spu_size <= 0 &&
195         ( p_block->i_pts <= 0 || p_block->i_buffer < 4 ) )
196     {
197         msg_Dbg( p_dec, "invalid starting packet (size < 4 or pts <=0)" );
198         msg_Dbg( p_dec, "spu size: %d, i_pts: "I64Fd" i_buffer: %d",
199                  p_sys->i_spu_size, p_block->i_pts, p_block->i_buffer );
200         block_Release( p_block );
201         return NULL;
202     }
203
204     block_ChainAppend( &p_sys->p_block, p_block );
205     p_sys->i_spu += p_block->i_buffer;
206
207     if( p_sys->i_spu_size <= 0 )
208     {
209         p_sys->i_spu_size = ( p_block->p_buffer[0] << 8 )|
210             p_block->p_buffer[1];
211         p_sys->i_rle_size = ( ( p_block->p_buffer[2] << 8 )|
212             p_block->p_buffer[3] ) - 4;
213
214         /* msg_Dbg( p_dec, "i_spu_size=%d i_rle=%d",
215                     p_sys->i_spu_size, p_sys->i_rle_size ); */
216
217         if( p_sys->i_spu_size <= 0 || p_sys->i_rle_size >= p_sys->i_spu_size )
218         {
219             p_sys->i_spu_size = 0;
220             p_sys->i_rle_size = 0;
221             p_sys->i_spu      = 0;
222             p_sys->p_block    = NULL;
223
224             block_Release( p_block );
225             return NULL;
226         }
227     }
228
229     if( p_sys->i_spu >= p_sys->i_spu_size )
230     {
231         /* We have a complete sub */
232         msg_Dbg( p_dec, "SPU packets size=%d should be %d",
233                  p_sys->i_spu, p_sys->i_spu_size );
234
235         return p_sys->p_block;
236     }
237     return NULL;
238 }