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 GNU
16 * General Public License for more details.
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 *****************************************************************************/
24 /* repompé sur video_decoder.c
25 * FIXME: passer en terminate/destroy avec les signaux supplémentaires ?? */
27 /*****************************************************************************
29 *****************************************************************************/
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() */
44 #include "debug.h" /* ASSERT */
47 #include "input_netlist.h"
48 #include "decoder_fifo.h"
51 #include "video_output.h"
53 #include "spu_decoder.h"
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 );
63 #define GetWord( i ) \
64 i = GetByte( &p_spudec->bit_stream ) << 8; \
65 i += GetByte( &p_spudec->bit_stream ); \
68 /*****************************************************************************
69 * spudec_CreateThread: create a spu decoder thread
70 *****************************************************************************/
71 spudec_thread_t * spudec_CreateThread( input_thread_t * p_input )
73 spudec_thread_t * p_spudec;
75 intf_DbgMsg("spudec debug: creating spu decoder thread\n");
77 /* Allocate the memory needed to store the thread's structure */
78 if ( (p_spudec = (spudec_thread_t *)malloc( sizeof(spudec_thread_t) )) == NULL )
80 intf_ErrMsg("spudec error: not enough memory for spudec_CreateThread() to create the new thread\n");
85 * Initialize the thread properties
88 p_spudec->b_error = 0;
91 * Initialize the input properties
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;
105 /* Get the video output informations */
106 p_spudec->p_vout = p_input->p_vout;
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) )
112 intf_ErrMsg("spudec error: can't spawn spu decoder thread\n");
117 intf_DbgMsg("spudec debug: spu decoder thread (%p) created\n", p_spudec);
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 )
130 intf_DbgMsg("spudec debug: requesting termination of 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 does only return 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;
214 while( !DECODER_FIFO_ISEMPTY(p_spudec->fifo) )
216 printf( "*** tracking next SPU PES\n" );
219 i_spu_id = GetByte( &p_spudec->bit_stream );
221 while( (i_spu_id & 0xe0) != 0x20 );
222 i_pes_size = DECODER_FIFO_START(p_spudec->fifo)->i_pes_size;
223 printf( "got it. size = 0x%.4x\n", i_pes_size );
225 printf( "SPU id: 0x%.2x\n", i_spu_id );
229 GetWord( i_packet_size );
230 printf( "total size: 0x%.4x\n", i_packet_size );
232 GetWord( i_rle_size );
233 printf( "RLE size: 0x%.4x\n", i_rle_size );
235 /* we already read 4 bytes for the total size and the RLE size */
237 p_spu = vout_CreateSubPicture( p_spudec->p_vout,
238 DVD_SUBPICTURE, i_rle_size );
239 p_spu_data = p_spu->p_data;
241 if( (i_rle_size < i_packet_size)
242 && ((i_spu_id & 0xe0) == 0x20) )
244 printf( "doing RLE stuff (%i bytes)\n", i_rle_size );
245 printf( "index/size %i/%i\n", i_index, i_pes_size );
246 while( i_index++ <i_rle_size )
248 //*p_spu_data++ = GetByte( &p_spudec->bit_stream );
249 if (i_index == i_pes_size) printf ("\n **** \n");
251 if (i_index == i_pes_size) printf( "%.2x", *p_spu_data++ = GetByte( &p_spudec->bit_stream ) );
252 printf( "%.2x", *p_spu_data++ = GetByte( &p_spudec->bit_stream ) );
254 printf( "\nindex/size %i/%i\n", i_index, i_pes_size );
258 printf( "control stuff\n" );
265 printf( "date: 0x%.4x\n", i_word );
268 printf( " next: 0x%.4x (i-5: %.4x)\n", i_word, i_index-5 );
269 b_finished = (i_index - 5 >= i_word );
273 i_cmd = GetByte( &p_spudec->bit_stream );
279 printf( " 00 (display now)\n" );
282 printf( " 01 (start displaying)\n" );
285 printf( " 02 (stop displaying)\n" );
289 printf( " 03 (palette) - %.4x\n", i_word );
293 printf( " 04 (alpha channel) - %.4x\n", i_word );
297 printf( " 05 (coordinates) - %.4x", i_word );
299 printf( "%.4x", i_word );
301 printf( "%.4x\n", i_word );
305 printf( " 06 (byte offsets) - %.4x", i_word );
307 printf( "%.4x\n", i_word );
310 printf( " ff (end)\n" );
313 printf( " %.2x (unknown command)\n", i_cmd );
318 while( i_cmd != 0xff );
321 while( !b_finished );
322 printf( "control stuff finished\n" );
323 printf( "*** end of PES !\n\n" );
327 printf( "*** invalid PES !\n\n" );
328 /* trash the PES packet */
329 /*vlc_mutex_lock( &p_spudec->fifo.data_lock );
330 input_NetlistFreePES( p_spudec->bit_stream.p_input,
331 DECODER_FIFO_START(p_spudec->fifo) );
332 DECODER_FIFO_INCSTART( p_spudec->fifo );
333 vlc_mutex_unlock( &p_spudec->fifo.data_lock );*/
337 /* Waiting for the input thread to put new PES packets in the fifo */
338 printf( "decoder fifo is empty\n" );
339 vlc_cond_wait( &p_spudec->fifo.data_wait, &p_spudec->fifo.data_lock );
345 if( p_spudec->b_error )
347 ErrorThread( p_spudec );
353 EndThread( p_spudec );
356 /*****************************************************************************
357 * ErrorThread: RunThread() error loop
358 *****************************************************************************
359 * This function is called when an error occured during thread main's loop. The
360 * thread can still receive feed, but must be ready to terminate as soon as
362 *****************************************************************************/
363 static void ErrorThread( spudec_thread_t *p_spudec )
365 /* We take the lock, because we are going to read/write the start/end
366 * indexes of the decoder fifo */
367 vlc_mutex_lock( &p_spudec->fifo.data_lock );
369 /* Wait until a `die' order is sent */
370 while( !p_spudec->b_die )
372 /* Trash all received PES packets */
373 while( !DECODER_FIFO_ISEMPTY(p_spudec->fifo) )
375 input_NetlistFreePES( p_spudec->bit_stream.p_input, DECODER_FIFO_START(p_spudec->fifo) );
376 DECODER_FIFO_INCSTART( p_spudec->fifo );
379 /* Waiting for the input thread to put new PES packets in the fifo */
380 vlc_cond_wait( &p_spudec->fifo.data_wait, &p_spudec->fifo.data_lock );
383 /* We can release the lock before leaving */
384 vlc_mutex_unlock( &p_spudec->fifo.data_lock );
387 /*****************************************************************************
388 * EndThread: thread destruction
389 *****************************************************************************
390 * This function is called when the thread ends after a sucessful
392 *****************************************************************************/
393 static void EndThread( spudec_thread_t *p_spudec )
395 intf_DbgMsg( "spudec debug: destroying spu decoder thread %p\n", p_spudec );
397 intf_DbgMsg( "spudec debug: spu decoder thread %p destroyed\n", p_spudec);