1 /*****************************************************************************
2 * spu_decoder.c : spu decoder thread
3 *****************************************************************************
4 * Copyright (C) 2000 VideoLAN
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.
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.
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 *****************************************************************************/
23 /* repompé sur video_decoder.c
24 * FIXME: passer en terminate/destroy avec les signaux supplémentaires ?? */
26 /*****************************************************************************
28 *****************************************************************************/
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() */
43 #include "debug.h" /* ASSERT */
46 #include "input_netlist.h"
47 #include "decoder_fifo.h"
50 #include "video_output.h"
52 #include "spu_decoder.h"
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 );
62 #define GetWord( i ) \
63 i = GetByte( &p_spudec->bit_stream ) << 8; \
64 i += GetByte( &p_spudec->bit_stream ); \
67 /*****************************************************************************
68 * spudec_CreateThread: create a spu decoder thread
69 *****************************************************************************/
70 spudec_thread_t * spudec_CreateThread( input_thread_t * p_input )
72 spudec_thread_t * p_spudec;
74 intf_DbgMsg("spudec debug: creating spu decoder thread\n");
76 /* Allocate the memory needed to store the thread's structure */
77 if ( (p_spudec = (spudec_thread_t *)malloc( sizeof(spudec_thread_t) )) == NULL )
79 intf_ErrMsg("spudec error: not enough memory for spudec_CreateThread() to create the new thread\n");
84 * Initialize the thread properties
87 p_spudec->b_error = 0;
90 * Initialize the input properties
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;
104 /* Get the video output informations */
105 p_spudec->p_vout = p_input->p_vout;
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) )
111 intf_ErrMsg("spudec error: can't spawn spu decoder thread\n");
116 intf_DbgMsg("spudec debug: spu decoder thread (%p) created\n", p_spudec);
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 )
129 intf_DbgMsg( "spudec debug: requesting termination of "
130 "spu decoder thread %p\n", p_spudec);
132 /* Ask thread to kill itself */
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 );
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 );
145 /* following functions are local */
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 )
156 intf_DbgMsg("spudec debug: initializing spu decoder thread %p\n", p_spudec);
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) )
163 if ( p_spudec->b_die )
165 vlc_mutex_unlock( &p_spudec->fifo.data_lock );
168 vlc_cond_wait( &p_spudec->fifo.data_wait, &p_spudec->fifo.data_lock );
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 );
176 /* Mark thread as running and return */
177 intf_DbgMsg( "spudec debug: InitThread(%p) succeeded\n", p_spudec );
181 /*****************************************************************************
182 * RunThread: spu decoder thread
183 *****************************************************************************
184 * spu decoder thread. This function only returns when the thread is
186 *****************************************************************************/
187 static void RunThread( spudec_thread_t *p_spudec )
189 intf_DbgMsg("spudec debug: running spu decoder thread (%p) (pid == %i)\n",
193 * Initialize thread and free configuration
195 p_spudec->b_error = InitThread( p_spudec );
200 * Main loop - it is not executed if an error occured during
203 while( (!p_spudec->b_die) && (!p_spudec->b_error) )
210 boolean_t b_finished;
211 unsigned char * p_spu_data;
212 subpicture_t * p_spu = NULL;
214 while( !DECODER_FIFO_ISEMPTY(p_spudec->fifo) )
216 /* wait for the next SPU ID.
217 * XXX: We trash 0xff bytes since they probably come from
218 * an incomplete previous packet */
221 i_packet_size = GetByte( &p_spudec->bit_stream );
223 while( i_packet_size == 0xff );
225 if( p_spudec->b_die )
228 /* the total size - should equal the sum of the
229 * PES packet size that form the SPU packet */
230 i_packet_size = ( i_packet_size << 8 )
231 + GetByte( &p_spudec->bit_stream );
234 /* get the useful PES size (real size - 10) */
235 i_pes_size = DECODER_FIFO_START(p_spudec->fifo)->i_pes_size - 9;
238 /* the RLE stuff size */
239 GetWord( i_rle_size );
241 /* if the values we got aren't too strange, decode the data */
242 if( i_rle_size < i_packet_size )
244 /* allocate the subpicture.
245 * FIXME: we should check if the allocation failed */
246 p_spu = vout_CreateSubPicture( p_spudec->p_vout,
247 DVD_SUBPICTURE, i_rle_size );
248 p_spu_data = p_spu->p_data;
250 /* get display time */
251 p_spu->begin_date = p_spu->end_date
252 = DECODER_FIFO_START(p_spudec->fifo)->i_pts;
254 /* getting the RLE part */
255 while( i_index++ < i_rle_size )
257 /* skip the leading byte of a PES */
258 /* FIXME: this part definitely looks strange */
259 if ( !((i_index + 3) % i_pes_size) )
263 *p_spu_data++ = GetByte( &p_spudec->bit_stream );
266 /* getting the control part */
277 /* next offset, no next offset if == i_index-5 */
279 b_finished = ( i_index - 5 >= i_word );
283 i_cmd = GetByte( &p_spudec->bit_stream );
288 case SPU_CMD_FORCE_DISPLAY:
289 /* 00 (force displaying) */
291 /* FIXME: here we have to calculate dates. It's
292 * around i_date * 12000 but I don't know
295 case SPU_CMD_START_DISPLAY:
296 /* 01 (start displaying) */
297 p_spu->begin_date += ( i_date * 12000 );
299 case SPU_CMD_STOP_DISPLAY:
300 /* 02 (stop displaying) */
301 p_spu->end_date += ( i_date * 12000 );
303 case SPU_CMD_SET_PALETTE:
304 /* 03xxxx (palette) */
307 case SPU_CMD_SET_ALPHACHANNEL:
308 /* 04xxxx (alpha channel) */
311 case SPU_CMD_SET_COORDINATES:
312 /* 05xxxyyyxxxyyy (coordinates) */
313 i_word = GetByte( &p_spudec->bit_stream );
314 p_spu->i_x = (i_word << 4)
315 | GetBits( &p_spudec->bit_stream, 4 );
317 i_word = GetBits( &p_spudec->bit_stream, 4 );
318 p_spu->i_width = p_spu->i_x - ( (i_word << 8)
319 | GetByte( &p_spudec->bit_stream ) ) + 1;
321 i_word = GetByte( &p_spudec->bit_stream );
322 p_spu->i_y = (i_word << 4)
323 | GetBits( &p_spudec->bit_stream, 4 );
325 i_word = GetBits( &p_spudec->bit_stream, 4 );
326 p_spu->i_height = p_spu->i_y - ( (i_word << 8)
327 | GetByte( &p_spudec->bit_stream ) ) + 1;
331 case SPU_CMD_SET_OFFSETS:
332 /* 06xxxxyyyy (byte offsets) */
334 p_spu->type.spu.i_offset[0] = i_word - 4;
336 p_spu->type.spu.i_offset[1] = i_word - 4;
342 /* ?? (unknown command) */
343 intf_ErrMsg( "spudec: unknown command 0x%.2x\n",
348 while( i_cmd != SPU_CMD_END );
350 while( !b_finished );
352 /* SPU is finished - we can tell the video output
354 vout_DisplaySubPicture( p_spudec->p_vout, p_spu );
358 /* Unexpected PES packet - trash it */
359 intf_ErrMsg( "spudec: trying to recover from bad packet\n" );
360 vlc_mutex_lock( &p_spudec->fifo.data_lock );
361 input_NetlistFreePES( p_spudec->bit_stream.p_input,
362 DECODER_FIFO_START(p_spudec->fifo) );
363 DECODER_FIFO_INCSTART( p_spudec->fifo );
364 vlc_mutex_unlock( &p_spudec->fifo.data_lock );
373 if( p_spudec->b_error )
375 ErrorThread( p_spudec );
381 EndThread( p_spudec );
384 /*****************************************************************************
385 * ErrorThread: RunThread() error loop
386 *****************************************************************************
387 * This function is called when an error occured during thread main's loop. The
388 * thread can still receive feed, but must be ready to terminate as soon as
390 *****************************************************************************/
391 static void ErrorThread( spudec_thread_t *p_spudec )
393 /* We take the lock, because we are going to read/write the start/end
394 * indexes of the decoder fifo */
395 vlc_mutex_lock( &p_spudec->fifo.data_lock );
397 /* Wait until a `die' order is sent */
398 while( !p_spudec->b_die )
400 /* Trash all received PES packets */
401 while( !DECODER_FIFO_ISEMPTY(p_spudec->fifo) )
403 input_NetlistFreePES( p_spudec->bit_stream.p_input, DECODER_FIFO_START(p_spudec->fifo) );
404 DECODER_FIFO_INCSTART( p_spudec->fifo );
407 /* Waiting for the input thread to put new PES packets in the fifo */
408 vlc_cond_wait( &p_spudec->fifo.data_wait, &p_spudec->fifo.data_lock );
411 /* We can release the lock before leaving */
412 vlc_mutex_unlock( &p_spudec->fifo.data_lock );
415 /*****************************************************************************
416 * EndThread: thread destruction
417 *****************************************************************************
418 * This function is called when the thread ends after a sucessful
420 *****************************************************************************/
421 static void EndThread( spudec_thread_t *p_spudec )
423 intf_DbgMsg( "spudec debug: destroying spu decoder thread %p\n", p_spudec );
425 intf_DbgMsg( "spudec debug: spu decoder thread %p destroyed\n", p_spudec);