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 /*****************************************************************************
25 *****************************************************************************/
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() */
40 #include "debug.h" /* ASSERT */
42 #include "stream_control.h"
43 #include "input_ext-dec.h"
46 #include "video_output.h"
48 #include "spu_decoder.h"
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 );
58 /*****************************************************************************
59 * spudec_CreateThread: create a spu decoder thread
60 *****************************************************************************/
61 vlc_thread_t spudec_CreateThread( vdec_config_t * p_config )
63 spudec_thread_t * p_spudec;
65 intf_DbgMsg("spudec debug: creating spu decoder thread");
67 /* Allocate the memory needed to store the thread's structure */
68 if ( (p_spudec = (spudec_thread_t *)malloc( sizeof(spudec_thread_t) )) == NULL )
70 intf_ErrMsg("spudec error: not enough memory for spudec_CreateThread() to create the new thread");
75 * Initialize the thread properties
77 p_spudec->p_fifo->b_die = 0;
78 p_spudec->p_fifo->b_error = 0;
79 p_spudec->p_config = p_config;
80 p_spudec->p_fifo = p_config->decoder_config.p_decoder_fifo;
82 /* Get the video output informations */
83 p_spudec->p_vout = p_config->p_vout;
85 /* Spawn the spu decoder thread */
86 if ( vlc_thread_create(&p_spudec->thread_id, "spu decoder",
87 (vlc_thread_func_t)RunThread, (void *)p_spudec) )
89 intf_ErrMsg("spudec error: can't spawn spu decoder thread");
94 intf_DbgMsg("spudec debug: spu decoder thread (%p) created", p_spudec);
95 return( p_spudec->thread_id );
98 /* following functions are local */
100 /*****************************************************************************
101 * InitThread: initialize spu decoder thread
102 *****************************************************************************
103 * This function is called from RunThread and performs the second step of the
104 * initialization. It returns 0 on success. Note that the thread's flag are not
105 * modified inside this function.
106 *****************************************************************************/
107 static int InitThread( spudec_thread_t *p_spudec )
109 intf_DbgMsg("spudec debug: initializing spu decoder thread %p", p_spudec);
111 p_spudec->p_config->decoder_config.pf_init_bit_stream( &p_spudec->bit_stream,
112 p_spudec->p_config->decoder_config.p_decoder_fifo );
114 /* Mark thread as running and return */
115 intf_DbgMsg( "spudec debug: InitThread(%p) succeeded", p_spudec );
119 /*****************************************************************************
120 * RunThread: spu decoder thread
121 *****************************************************************************
122 * spu decoder thread. This function only returns when the thread is
124 *****************************************************************************/
125 static void RunThread( spudec_thread_t *p_spudec )
127 intf_DbgMsg("spudec debug: running spu decoder thread (%p) (pid == %i)",
131 * Initialize thread and free configuration
133 p_spudec->p_fifo->b_error = InitThread( p_spudec );
136 * Main loop - it is not executed if an error occured during
139 while( (!p_spudec->p_fifo->b_die) && (!p_spudec->p_fifo->b_error) )
145 subpicture_t * p_spu = NULL;
147 while( !DECODER_FIFO_ISEMPTY(*p_spudec->p_fifo) )
149 /* wait for the next SPU ID.
150 * XXX: We trash 0xff bytes since they probably come from
151 * an incomplete previous packet */
154 i_packet_size = GetByte( &p_spudec->bit_stream );
156 while( i_packet_size == 0xff );
158 if( p_spudec->p_fifo->b_die )
163 /* the total size - should equal the sum of the
164 * PES packet size that form the SPU packet */
165 i_packet_size = i_packet_size << 8
166 | GetByte( &p_spudec->bit_stream );
168 /* the RLE stuff size */
169 i_rle_size = GetByte( &p_spudec->bit_stream ) << 8
170 | GetByte( &p_spudec->bit_stream );
172 /* if the values we got aren't too strange, decode the data */
173 if( i_rle_size < i_packet_size )
175 /* allocate the subpicture.
176 * FIXME: we should check if the allocation failed */
177 p_spu = vout_CreateSubPicture( p_spudec->p_vout,
178 DVD_SUBPICTURE, i_rle_size );
179 /* get display time */
180 p_spu->begin_date = p_spu->end_date
181 = DECODER_FIFO_START(*p_spudec->p_fifo)->i_pts;
183 /* get RLE data, skip 4 bytes for the first two read offsets */
184 GetChunk( &p_spudec->bit_stream, p_spu->p_data,
187 if( p_spudec->p_fifo->b_die )
192 /* continue parsing after the RLE part */
193 i_index = i_rle_size;
195 /* assume packet is valid */
198 /* getting the control part */
202 unsigned int i_word, i_date;
204 /* Get the sequence date */
205 i_date = GetByte( &p_spudec->bit_stream ) << 8
206 | GetByte( &p_spudec->bit_stream );
209 i_next = GetByte( &p_spudec->bit_stream ) << 8
210 | GetByte( &p_spudec->bit_stream );
216 i_cmd = GetByte( &p_spudec->bit_stream );
221 case SPU_CMD_FORCE_DISPLAY:
222 /* 00 (force displaying) */
224 /* FIXME: here we have to calculate dates. It's
225 * around i_date * 12000 but I don't know
228 case SPU_CMD_START_DISPLAY:
229 /* 01 (start displaying) */
230 p_spu->begin_date += ( i_date * 12000 );
232 case SPU_CMD_STOP_DISPLAY:
233 /* 02 (stop displaying) */
234 p_spu->end_date += ( i_date * 12000 );
236 case SPU_CMD_SET_PALETTE:
237 /* 03xxxx (palette) */
238 i_word = GetByte( &p_spudec->bit_stream ) << 8
239 | GetByte( &p_spudec->bit_stream );
242 case SPU_CMD_SET_ALPHACHANNEL:
243 /* 04xxxx (alpha channel) */
244 i_word = GetByte( &p_spudec->bit_stream ) << 8
245 | GetByte( &p_spudec->bit_stream );
248 case SPU_CMD_SET_COORDINATES:
249 /* 05xxxyyyxxxyyy (coordinates) */
250 i_word = GetByte( &p_spudec->bit_stream );
251 p_spu->i_x = (i_word << 4)
252 | GetBits( &p_spudec->bit_stream, 4 );
254 i_word = GetBits( &p_spudec->bit_stream, 4 );
255 p_spu->i_width = p_spu->i_x - ( (i_word << 8)
256 | GetBits( &p_spudec->bit_stream, 8 ) ) + 1;
258 i_word = GetBits( &p_spudec->bit_stream, 8 );
259 p_spu->i_y = (i_word << 4)
260 | GetBits( &p_spudec->bit_stream, 4 );
262 i_word = GetBits( &p_spudec->bit_stream, 4 );
263 p_spu->i_height = p_spu->i_y - ( (i_word << 8)
264 | GetByte( &p_spudec->bit_stream ) ) + 1;
268 case SPU_CMD_SET_OFFSETS:
269 /* 06xxxxyyyy (byte offsets) */
270 p_spu->type.spu.i_offset[0] =
271 ( GetByte( &p_spudec->bit_stream ) << 8
272 | GetByte( &p_spudec->bit_stream ) ) - 4;
273 p_spu->type.spu.i_offset[1] =
274 ( GetByte( &p_spudec->bit_stream ) << 8
275 | GetByte( &p_spudec->bit_stream ) ) - 4;
282 /* ?? (unknown command) */
283 intf_ErrMsg( "spudec: unknown command 0x%.2x",
289 while( b_valid && ( i_cmd != SPU_CMD_END ) );
291 while( b_valid && ( i_index == i_next ) );
295 /* SPU is finished - we can tell the video output
297 vout_DisplaySubPicture( p_spudec->p_vout, p_spu );
301 vout_DestroySubPicture( p_spudec->p_vout, p_spu );
306 /* Unexpected PES packet - trash it */
307 intf_ErrMsg( "spudec: trying to recover from bad packet" );
308 vlc_mutex_lock( &p_spudec->p_fifo->data_lock );
309 p_spudec->p_fifo->pf_delete_pes( p_spudec->p_fifo->p_packets_mgt,
310 DECODER_FIFO_START(*p_spudec->p_fifo) );
311 DECODER_FIFO_INCSTART( *p_spudec->p_fifo );
312 vlc_mutex_unlock( &p_spudec->p_fifo->data_lock );
321 if( p_spudec->p_fifo->b_error )
323 ErrorThread( p_spudec );
327 EndThread( p_spudec );
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
336 *****************************************************************************/
337 static void ErrorThread( spudec_thread_t *p_spudec )
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 );
343 /* Wait until a `die' order is sent */
344 while( !p_spudec->p_fifo->b_die )
346 /* Trash all received PES packets */
347 while( !DECODER_FIFO_ISEMPTY(*p_spudec->p_fifo) )
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 );
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 );
358 /* We can release the lock before leaving */
359 vlc_mutex_unlock( &p_spudec->p_fifo->data_lock );
362 /*****************************************************************************
363 * EndThread: thread destruction
364 *****************************************************************************
365 * This function is called when the thread ends after a sucessful
367 *****************************************************************************/
368 static void EndThread( spudec_thread_t *p_spudec )
370 intf_DbgMsg( "spudec debug: destroying spu decoder thread %p", p_spudec );
371 free( p_spudec->p_config );
373 intf_DbgMsg( "spudec debug: spu decoder thread %p destroyed", p_spudec);