]> git.sesse.net Git - vlc/blob - src/spu_decoder/spu_decoder.c
* Changed default values :
[vlc] / src / spu_decoder / spu_decoder.c
1 /*****************************************************************************
2  * spu_decoder.c : spu decoder thread
3  *****************************************************************************
4  * Copyright (C) 2000 VideoLAN
5  *
6  * Authors:
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  * 
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
21  *****************************************************************************/
22
23 /*****************************************************************************
24  * Preamble
25  *****************************************************************************/
26 #include "defs.h"
27
28 #include <stdlib.h>                                      /* malloc(), free() */
29 #include <unistd.h>                                              /* getpid() */
30
31 #include "config.h"
32 #include "common.h"
33 #include "threads.h"
34 #include "mtime.h"
35 #include "plugins.h"
36
37 #include "intf_msg.h"
38 #include "debug.h"                                                 /* ASSERT */
39
40 #include "stream_control.h"
41 #include "input_ext-dec.h"
42
43 #include "video.h"
44 #include "video_output.h"
45
46 #include "spu_decoder.h"
47
48 /*
49  * Local prototypes
50  */
51 static int      InitThread          ( spudec_thread_t *p_spudec );
52 static void     RunThread           ( spudec_thread_t *p_spudec );
53 static void     ErrorThread         ( spudec_thread_t *p_spudec );
54 static void     EndThread           ( spudec_thread_t *p_spudec );
55
56 /*****************************************************************************
57  * spudec_CreateThread: create a spu decoder thread
58  *****************************************************************************/
59 vlc_thread_t spudec_CreateThread( vdec_config_t * p_config )
60 {
61     spudec_thread_t *     p_spudec;
62
63     intf_DbgMsg("spudec debug: creating spu decoder thread");
64
65     /* Allocate the memory needed to store the thread's structure */
66     p_spudec = (spudec_thread_t *)malloc( sizeof(spudec_thread_t) );
67
68     if ( p_spudec == NULL )
69     {
70         intf_ErrMsg( "spudec error: not enough memory "
71                      "for spudec_CreateThread() to create the new thread" );
72         return( 0 );
73     }
74
75     /*
76      * Initialize the thread properties
77      */
78     p_spudec->p_config = p_config;
79     p_spudec->p_fifo = p_config->decoder_config.p_decoder_fifo;
80
81     /* Get the video output informations */
82     p_spudec->p_vout = p_config->p_vout;
83
84     /* Spawn the spu decoder thread */
85     if ( vlc_thread_create(&p_spudec->thread_id, "spu decoder",
86          (vlc_thread_func_t)RunThread, (void *)p_spudec) )
87     {
88         intf_ErrMsg("spudec error: can't spawn spu decoder thread");
89         free( p_spudec );
90         return( 0 );
91     }
92
93     intf_DbgMsg("spudec debug: spu decoder thread (%p) created", p_spudec);
94     return( p_spudec->thread_id );
95 }
96
97 /* following functions are local */
98
99 /*****************************************************************************
100  * InitThread: initialize spu decoder thread
101  *****************************************************************************
102  * This function is called from RunThread and performs the second step of the
103  * initialization. It returns 0 on success. Note that the thread's flag are not
104  * modified inside this function.
105  *****************************************************************************/
106 static int InitThread( spudec_thread_t *p_spudec )
107 {
108     intf_DbgMsg("spudec debug: initializing spu decoder thread %p", p_spudec);
109
110     p_spudec->p_config->decoder_config.pf_init_bit_stream(
111             &p_spudec->bit_stream,
112             p_spudec->p_config->decoder_config.p_decoder_fifo );
113
114     /* Mark thread as running and return */
115     intf_DbgMsg( "spudec debug: InitThread(%p) succeeded", p_spudec );
116     return( 0 );
117 }
118
119 /*****************************************************************************
120  * RunThread: spu decoder thread
121  *****************************************************************************
122  * spu decoder thread. This function only returns when the thread is
123  * terminated.
124  *****************************************************************************/
125 static void RunThread( spudec_thread_t *p_spudec )
126 {
127     intf_DbgMsg("spudec debug: running spu decoder thread (%p) (pid == %i)",
128         p_spudec, getpid());
129
130     /*
131      * Initialize thread and free configuration
132      */
133     p_spudec->p_fifo->b_error = InitThread( p_spudec );
134
135     /*
136      * Main loop - it is not executed if an error occured during
137      * initialization
138      */
139     while( (!p_spudec->p_fifo->b_die) && (!p_spudec->p_fifo->b_error) )
140     {
141         int i_packet_size;
142         int i_rle_size;
143         int i_index, i_next;
144         boolean_t b_valid;
145         subpicture_t * p_spu = NULL;
146
147         /* wait for the next SPU ID.
148          * XXX: We trash 0xff bytes since they probably come from
149          * an incomplete previous packet */
150         do
151         {
152             i_packet_size = GetBits( &p_spudec->bit_stream, 8 );
153         }
154         while( i_packet_size == 0xff );
155
156         if( p_spudec->p_fifo->b_die )
157         {
158             break;
159         }
160
161         /* the total size - should equal the sum of the
162          * PES packet size that form the SPU packet */
163         i_packet_size = i_packet_size << 8
164                          | GetBits( &p_spudec->bit_stream, 8 );
165
166         /* the RLE stuff size */
167         i_rle_size = GetBits( &p_spudec->bit_stream, 16 );
168
169         /* if the values we got aren't too strange, decode the data */
170         if( i_rle_size < i_packet_size )
171         {
172             /* allocate the subpicture.
173              * FIXME: we should check if the allocation failed */
174             p_spu = vout_CreateSubPicture( p_spudec->p_vout,
175                                            DVD_SUBPICTURE, i_rle_size );
176             /* get display time */
177             p_spu->begin_date = p_spu->end_date
178                             = DECODER_FIFO_START(*p_spudec->p_fifo)->i_pts;
179
180             /* get RLE data, skip 4 bytes for the first two read offsets */
181             GetChunk( &p_spudec->bit_stream, p_spu->p_data,
182                       i_rle_size - 4 );
183
184             if( p_spudec->p_fifo->b_die )
185             {
186                 break;
187             }
188
189             /* continue parsing after the RLE part */
190             i_index = i_rle_size;
191
192             /* assume packet is valid */
193             b_valid = 1;
194
195             /* getting the control part */
196             do
197             {
198                 unsigned char   i_cmd;
199                 u16             i_date;
200
201                 /* Get the sequence date */
202                 i_date = GetBits( &p_spudec->bit_stream, 16 );
203
204                 /* Next offset */
205                 i_next = GetBits( &p_spudec->bit_stream, 16 );
206
207                 i_index += 4;
208
209                 do
210                 {
211                     i_cmd = GetBits( &p_spudec->bit_stream, 8 );
212                     i_index++;
213
214                     switch( i_cmd )
215                     {
216                         case SPU_CMD_FORCE_DISPLAY:
217                             /* 00 (force displaying) */
218                             break;
219                         /* FIXME: here we have to calculate dates. It's
220                          * around i_date * 12000 but I don't know
221                          * how much exactly.
222                          */
223                         case SPU_CMD_START_DISPLAY:
224                             /* 01 (start displaying) */
225                             p_spu->begin_date += ( i_date * 12000 );
226                             break;
227                         case SPU_CMD_STOP_DISPLAY:
228                             /* 02 (stop displaying) */
229                             p_spu->end_date += ( i_date * 12000 );
230                             break;
231                         case SPU_CMD_SET_PALETTE:
232                             /* 03xxxx (palette) - trashed */
233                             RemoveBits( &p_spudec->bit_stream, 16 );
234                             i_index += 2;
235                             break;
236                         case SPU_CMD_SET_ALPHACHANNEL:
237                             /* 04xxxx (alpha channel) - trashed */
238                             RemoveBits( &p_spudec->bit_stream, 16 );
239                             i_index += 2;
240                             break;
241                         case SPU_CMD_SET_COORDINATES:
242                             /* 05xxxyyyxxxyyy (coordinates) */
243                             p_spu->i_x =
244                                 GetBits( &p_spudec->bit_stream, 12 );
245
246                             p_spu->i_width = p_spu->i_x -
247                                 GetBits( &p_spudec->bit_stream, 12 ) + 1;
248
249                             p_spu->i_y =
250                                 GetBits( &p_spudec->bit_stream, 12 );
251
252                             p_spu->i_height = p_spu->i_y -
253                                 GetBits( &p_spudec->bit_stream, 12 ) + 1;
254
255                             i_index += 6;
256                             break;
257                         case SPU_CMD_SET_OFFSETS:
258                             /* 06xxxxyyyy (byte offsets) */
259                             p_spu->type.spu.i_offset[0] =
260                                 GetBits( &p_spudec->bit_stream, 16 ) - 4;
261                             p_spu->type.spu.i_offset[1] =
262                                 GetBits( &p_spudec->bit_stream, 16 ) - 4;
263                             i_index += 4;
264                             break;
265                         case SPU_CMD_END:
266                             /* ff (end) */
267                             break;
268                         default:
269                             /* ?? (unknown command) */
270                             intf_ErrMsg( "spudec: unknown command 0x%.2x",
271                                          i_cmd );
272                             b_valid = 0;
273                             break;
274                     }
275                 }
276                 while( b_valid && ( i_cmd != SPU_CMD_END ) );
277             }
278             while( b_valid && ( i_index == i_next ) );
279
280             if( b_valid )
281             {
282                 /* SPU is finished - we can tell the video output
283                  * to display it */
284                 vout_DisplaySubPicture( p_spudec->p_vout, p_spu );
285             }
286             else
287             {
288                 vout_DestroySubPicture( p_spudec->p_vout, p_spu );
289             }
290         }
291         else 
292         {
293             /* Unexpected PES packet - trash it */
294             intf_ErrMsg( "spudec: trying to recover from bad packet" );
295             vlc_mutex_lock( &p_spudec->p_fifo->data_lock );
296             p_spudec->p_fifo->pf_delete_pes( p_spudec->p_fifo->p_packets_mgt,
297                                   DECODER_FIFO_START(*p_spudec->p_fifo) );
298             DECODER_FIFO_INCSTART( *p_spudec->p_fifo );
299             vlc_mutex_unlock( &p_spudec->p_fifo->data_lock );
300         }
301     }
302
303     /*
304      * Error loop
305      */
306     if( p_spudec->p_fifo->b_error )
307     {
308         ErrorThread( p_spudec );
309     }
310
311     /* End of thread */
312     EndThread( p_spudec );
313 }
314
315 /*****************************************************************************
316  * ErrorThread: RunThread() error loop
317  *****************************************************************************
318  * This function is called when an error occured during thread main's loop. The
319  * thread can still receive feed, but must be ready to terminate as soon as
320  * possible.
321  *****************************************************************************/
322 static void ErrorThread( spudec_thread_t *p_spudec )
323 {
324     /* We take the lock, because we are going to read/write the start/end
325      * indexes of the decoder fifo */
326     vlc_mutex_lock( &p_spudec->p_fifo->data_lock );
327
328     /* Wait until a `die' order is sent */
329     while( !p_spudec->p_fifo->b_die )
330     {
331         /* Trash all received PES packets */
332         while( !DECODER_FIFO_ISEMPTY(*p_spudec->p_fifo) )
333         {
334             p_spudec->p_fifo->pf_delete_pes( p_spudec->p_fifo->p_packets_mgt,
335                     DECODER_FIFO_START(*p_spudec->p_fifo) );
336             DECODER_FIFO_INCSTART( *p_spudec->p_fifo );
337         }
338
339         /* Waiting for the input thread to put new PES packets in the fifo */
340         vlc_cond_wait( &p_spudec->p_fifo->data_wait, &p_spudec->p_fifo->data_lock );
341     }
342
343     /* We can release the lock before leaving */
344     vlc_mutex_unlock( &p_spudec->p_fifo->data_lock );
345 }
346
347 /*****************************************************************************
348  * EndThread: thread destruction
349  *****************************************************************************
350  * This function is called when the thread ends after a sucessful
351  * initialization.
352  *****************************************************************************/
353 static void EndThread( spudec_thread_t *p_spudec )
354 {
355     intf_DbgMsg( "spudec debug: destroying spu decoder thread %p", p_spudec );
356     free( p_spudec->p_config );
357     free( p_spudec );
358     intf_DbgMsg( "spudec debug: spu decoder thread %p destroyed", p_spudec);
359 }
360