]> git.sesse.net Git - vlc/blob - src/spu_decoder/spu_decoder.c
. les sous-titres sont affich�s et effac�s � l'heure
[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 GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public
19  * License along with this program; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  *****************************************************************************/
23
24 /* repompé sur video_decoder.c
25  * FIXME: passer en terminate/destroy avec les signaux supplémentaires ?? */
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30 #include "defs.h"
31
32 #include <stdlib.h>                                      /* malloc(), free() */
33 #include <sys/types.h>                        /* on BSD, uio.h needs types.h */
34 #include <sys/uio.h>                                          /* for input.h */
35 #include <unistd.h>                                              /* getpid() */
36
37 #include "config.h"
38 #include "common.h"
39 #include "threads.h"
40 #include "mtime.h"
41 #include "plugins.h"
42
43 #include "intf_msg.h"
44 #include "debug.h"                                                 /* ASSERT */
45
46 #include "input.h"
47 #include "input_netlist.h"
48 #include "decoder_fifo.h"
49
50 #include "video.h"
51 #include "video_output.h"
52
53 #include "spu_decoder.h"
54
55 /*
56  * Local prototypes
57  */
58 static int      InitThread          ( spudec_thread_t *p_spudec );
59 static void     RunThread           ( spudec_thread_t *p_spudec );
60 static void     ErrorThread         ( spudec_thread_t *p_spudec );
61 static void     EndThread           ( spudec_thread_t *p_spudec );
62
63 #define GetWord( i ) \
64     i  = GetByte( &p_spudec->bit_stream ) << 8; \
65     i += GetByte( &p_spudec->bit_stream ); \
66     i_index += 2;
67
68 /*****************************************************************************
69  * spudec_CreateThread: create a spu decoder thread
70  *****************************************************************************/
71 spudec_thread_t * spudec_CreateThread( input_thread_t * p_input )
72 {
73     spudec_thread_t *     p_spudec;
74
75     intf_DbgMsg("spudec debug: creating spu decoder thread\n");
76
77     /* Allocate the memory needed to store the thread's structure */
78     if ( (p_spudec = (spudec_thread_t *)malloc( sizeof(spudec_thread_t) )) == NULL )
79     {
80         intf_ErrMsg("spudec error: not enough memory for spudec_CreateThread() to create the new thread\n");
81         return( NULL );
82     }
83
84     /*
85      * Initialize the thread properties
86      */
87     p_spudec->b_die = 0;
88     p_spudec->b_error = 0;
89
90     /*
91      * Initialize the input properties
92      */
93     /* Initialize the decoder fifo's data lock and conditional variable and set
94      * its buffer as empty */
95     vlc_mutex_init( &p_spudec->fifo.data_lock );
96     vlc_cond_init( &p_spudec->fifo.data_wait );
97     p_spudec->fifo.i_start = 0;
98     p_spudec->fifo.i_end = 0;
99     /* Initialize the bit stream structure */
100     p_spudec->bit_stream.p_input = p_input;
101     p_spudec->bit_stream.p_decoder_fifo = &p_spudec->fifo;
102     p_spudec->bit_stream.fifo.buffer = 0;
103     p_spudec->bit_stream.fifo.i_available = 0;
104
105     /* Get the video output informations */
106     p_spudec->p_vout = p_input->p_vout;
107
108     /* Spawn the spu decoder thread */
109     if ( vlc_thread_create(&p_spudec->thread_id, "spu decoder",
110          (vlc_thread_func_t)RunThread, (void *)p_spudec) )
111     {
112         intf_ErrMsg("spudec error: can't spawn spu decoder thread\n");
113         free( p_spudec );
114         return( NULL );
115     }
116
117     intf_DbgMsg("spudec debug: spu decoder thread (%p) created\n", p_spudec);
118     return( p_spudec );
119 }
120
121 /*****************************************************************************
122  * spudec_DestroyThread: destroy a spu decoder thread
123  *****************************************************************************
124  * Destroy and terminate thread. This function will return 0 if the thread could
125  * be destroyed, and non 0 else. The last case probably means that the thread
126  * was still active, and another try may succeed.
127  *****************************************************************************/
128 void spudec_DestroyThread( spudec_thread_t *p_spudec )
129 {
130     intf_DbgMsg("spudec debug: requesting termination of spu decoder thread %p\n", p_spudec);
131
132     /* Ask thread to kill itself */
133     p_spudec->b_die = 1;
134
135     /* Warn the decoder that we're quitting */
136     vlc_mutex_lock( &p_spudec->fifo.data_lock );
137     vlc_cond_signal( &p_spudec->fifo.data_wait );
138     vlc_mutex_unlock( &p_spudec->fifo.data_lock );
139
140     /* Waiting for the decoder thread to exit */
141     /* Remove this as soon as the "status" flag is implemented */
142     vlc_thread_join( p_spudec->thread_id );
143 }
144
145 /* following functions are local */
146
147 /*****************************************************************************
148  * InitThread: initialize spu decoder thread
149  *****************************************************************************
150  * This function is called from RunThread and performs the second step of the
151  * initialization. It returns 0 on success. Note that the thread's flag are not
152  * modified inside this function.
153  *****************************************************************************/
154 static int InitThread( spudec_thread_t *p_spudec )
155 {
156     intf_DbgMsg("spudec debug: initializing spu decoder thread %p\n", p_spudec);
157
158     /* Our first job is to initialize the bit stream structure with the
159      * beginning of the input stream */
160     vlc_mutex_lock( &p_spudec->fifo.data_lock );
161     while ( DECODER_FIFO_ISEMPTY(p_spudec->fifo) )
162     {
163         if ( p_spudec->b_die )
164         {
165             vlc_mutex_unlock( &p_spudec->fifo.data_lock );
166             return( 1 );
167         }
168         vlc_cond_wait( &p_spudec->fifo.data_wait, &p_spudec->fifo.data_lock );
169     }
170
171     p_spudec->bit_stream.p_ts = DECODER_FIFO_START( p_spudec->fifo )->p_first_ts;
172     p_spudec->bit_stream.p_byte = p_spudec->bit_stream.p_ts->buffer + p_spudec->bit_stream.p_ts->i_payload_start;
173     p_spudec->bit_stream.p_end = p_spudec->bit_stream.p_ts->buffer + p_spudec->bit_stream.p_ts->i_payload_end;
174     vlc_mutex_unlock( &p_spudec->fifo.data_lock );
175
176     /* Mark thread as running and return */
177     intf_DbgMsg( "spudec debug: InitThread(%p) succeeded\n", p_spudec );
178     return( 0 );
179 }
180
181 /*****************************************************************************
182  * RunThread: spu decoder thread
183  *****************************************************************************
184  * spu decoder thread. This function does only return when the thread is
185  * terminated.
186  *****************************************************************************/
187 static void RunThread( spudec_thread_t *p_spudec )
188 {
189     intf_DbgMsg("spudec debug: running spu decoder thread (%p) (pid == %i)\n",
190         p_spudec, getpid());
191
192     /*
193      * Initialize thread and free configuration
194      */
195     p_spudec->b_error = InitThread( p_spudec );
196
197     p_spudec->b_run = 1;
198
199     /*
200      * Main loop - it is not executed if an error occured during
201      * initialization
202      */
203     while( (!p_spudec->b_die) && (!p_spudec->b_error) )
204     {
205         int i_packet_size;
206         int i_rle_size;
207         int i_index;
208         int i_pes_size;
209         int i_pes_count;
210         boolean_t       b_finished;
211         unsigned char * p_spu_data;
212         subpicture_t  * p_spu = NULL;
213
214         while( !DECODER_FIFO_ISEMPTY(p_spudec->fifo) )
215         {
216             /* wait for the next SPU ID.
217              * FIXME: We trash 0xff bytes since they come from
218              * an incomplete previous packet */
219             do
220             {
221                 i_packet_size = GetByte( &p_spudec->bit_stream );
222             }
223             while( i_packet_size == 0xff );
224
225             /* the total size - should equal the sum of the
226              * PES packet size that form the SPU packet */
227             i_packet_size = ( i_packet_size << 8 )
228                             + GetByte( &p_spudec->bit_stream );
229             i_index = 2;
230
231             /* get the useful PES size (real size - 10) */
232             i_pes_size = DECODER_FIFO_START(p_spudec->fifo)->i_pes_size - 9;
233             i_pes_count = 1;
234
235             /* the RLE stuff size */
236             GetWord( i_rle_size );
237
238             /* if the values we got aren't too strange, decode the data */
239             if( i_rle_size < i_packet_size )
240             {
241                 /* allocate the subpicture.
242                  * FIXME: we should check if the allocation failed */
243                 p_spu = vout_CreateSubPicture( p_spudec->p_vout,
244                                            DVD_SUBPICTURE, i_rle_size );
245                 p_spu_data = p_spu->p_data;
246
247                 /* get display time */
248                 p_spu->begin_date = p_spu->end_date
249                                 = DECODER_FIFO_START(p_spudec->fifo)->i_pts;
250
251                 /* getting the RLE part */
252                 while( i_index++ < i_rle_size )
253                 {
254                     /* skip the leading byte of a PES */
255                     if ( !((i_index + 3) % i_pes_size) )
256                     {
257                         i_pes_count++;
258                     }
259                     *p_spu_data++ = GetByte( &p_spudec->bit_stream );
260                 }
261
262                 /* getting the control part */
263                 b_finished = 0;
264                 do
265                 {
266                     unsigned char i_cmd;
267                     unsigned int i_word;
268                     unsigned int i_date;
269
270                     /* the date */
271                     GetWord( i_date );
272
273                     /* next offset, no next offset if == i_index-5 */
274                     GetWord( i_word );
275                     b_finished = ( i_index - 5 >= i_word );
276
277                     do
278                     {
279                         i_cmd = GetByte( &p_spudec->bit_stream );
280                         i_index++;
281
282                         switch( i_cmd )
283                         {
284                             case 0x00:
285                                 /* 00 (force displaying) */
286                                 break;
287                             case 0x01:
288                                 /* 01 (start displaying) */
289                                 p_spu->begin_date += (i_date * 1000000 / 80);
290                                 break;    /* FIXME: 80 is absolutely empiric */
291                             case 0x02:
292                                 /* 02 (stop displaying) */
293                                 p_spu->end_date += (i_date * 1000000 / 80);
294                                 break;    /* FIXME: 80 is absolutely empiric */
295                             case 0x03:
296                                 /* 03xxxx (palette) */
297                                 GetWord( i_word );
298                                 break;
299                             case 0x04:
300                                 /* 04xxxx (alpha channel) */
301                                 GetWord( i_word );
302                                 break;
303                             case 0x05:
304                                 /* 05xxxyyyxxxyyy (coordinates) */
305                                 i_word = GetByte( &p_spudec->bit_stream );
306                                 p_spu->type.spu.i_x1 = (i_word << 4)
307                                         | GetBits( &p_spudec->bit_stream, 4 );
308
309                                 i_word = GetBits( &p_spudec->bit_stream, 4 );
310                                 p_spu->type.spu.i_x2 = (i_word << 8)
311                                         | GetByte( &p_spudec->bit_stream );
312
313                                 i_word = GetByte( &p_spudec->bit_stream );
314                                 p_spu->type.spu.i_y1 = (i_word << 4)
315                                         | GetBits( &p_spudec->bit_stream, 4 );
316
317                                 i_word = GetBits( &p_spudec->bit_stream, 4 );
318                                 p_spu->type.spu.i_y2 = (i_word << 8)
319                                         | GetByte( &p_spudec->bit_stream );
320
321                                 i_index += 6;
322                                 break;
323                             case 0x06:
324                                 /* 06xxxxyyyy (byte offsets) */
325                                 GetWord( i_word );
326                                 p_spu->type.spu.i_offset[0] = i_word - 4;
327                                 GetWord( i_word );
328                                 p_spu->type.spu.i_offset[1] = i_word - 4;
329                                 break;
330                             case 0xff:
331                                 /* ff (end) */
332                                 break;
333                             default:
334                                 /* ?? (unknown command) */
335                                 break;
336                         }
337                     }
338                     while( i_cmd != 0xff );
339                 }
340                 while( !b_finished );
341
342                 /* SPU is finished - we can display it */
343                 vout_DisplaySubPicture( p_spudec->p_vout, p_spu );
344             }
345             else 
346             {
347                 /* Unexpected PES packet - trash it */
348                 vlc_mutex_lock( &p_spudec->fifo.data_lock );
349                 input_NetlistFreePES( p_spudec->bit_stream.p_input,
350                                       DECODER_FIFO_START(p_spudec->fifo) );
351                 DECODER_FIFO_INCSTART( p_spudec->fifo );
352                 vlc_mutex_unlock( &p_spudec->fifo.data_lock );
353             }
354
355         }
356         /* Waiting for the input thread to put new PES packets in the fifo */
357         vlc_cond_wait( &p_spudec->fifo.data_wait, &p_spudec->fifo.data_lock );
358
359         if( p_spu ) vout_DestroySubPicture( p_spudec->p_vout, p_spu );
360     }
361
362     /*
363      * Error loop
364      */
365     if( p_spudec->b_error )
366     {
367         ErrorThread( p_spudec );
368     }
369
370     p_spudec->b_run = 0;
371
372     /* End of thread */
373     EndThread( p_spudec );
374 }
375
376 /*****************************************************************************
377  * ErrorThread: RunThread() error loop
378  *****************************************************************************
379  * This function is called when an error occured during thread main's loop. The
380  * thread can still receive feed, but must be ready to terminate as soon as
381  * possible.
382  *****************************************************************************/
383 static void ErrorThread( spudec_thread_t *p_spudec )
384 {
385     /* We take the lock, because we are going to read/write the start/end
386      * indexes of the decoder fifo */
387     vlc_mutex_lock( &p_spudec->fifo.data_lock );
388
389     /* Wait until a `die' order is sent */
390     while( !p_spudec->b_die )
391     {
392         /* Trash all received PES packets */
393         while( !DECODER_FIFO_ISEMPTY(p_spudec->fifo) )
394         {
395             input_NetlistFreePES( p_spudec->bit_stream.p_input, DECODER_FIFO_START(p_spudec->fifo) );
396             DECODER_FIFO_INCSTART( p_spudec->fifo );
397         }
398
399         /* Waiting for the input thread to put new PES packets in the fifo */
400         vlc_cond_wait( &p_spudec->fifo.data_wait, &p_spudec->fifo.data_lock );
401     }
402
403     /* We can release the lock before leaving */
404     vlc_mutex_unlock( &p_spudec->fifo.data_lock );
405 }
406
407 /*****************************************************************************
408  * EndThread: thread destruction
409  *****************************************************************************
410  * This function is called when the thread ends after a sucessful
411  * initialization.
412  *****************************************************************************/
413 static void EndThread( spudec_thread_t *p_spudec )
414 {
415     intf_DbgMsg( "spudec debug: destroying spu decoder thread %p\n", p_spudec );
416     free( p_spudec );
417     intf_DbgMsg( "spudec debug: spu decoder thread %p destroyed\n", p_spudec);
418 }