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