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