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