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 <unistd.h> /* getpid() */
37 #include "debug.h" /* ASSERT */
39 #include "stream_control.h"
40 #include "input_ext-dec.h"
43 #include "video_output.h"
45 #include "spu_decoder.h"
50 static int InitThread ( spudec_thread_t *p_spudec );
51 static void RunThread ( spudec_thread_t *p_spudec );
52 static void ErrorThread ( spudec_thread_t *p_spudec );
53 static void EndThread ( spudec_thread_t *p_spudec );
55 /*****************************************************************************
56 * spudec_CreateThread: create a spu decoder thread
57 *****************************************************************************/
58 vlc_thread_t spudec_CreateThread( vdec_config_t * p_config )
60 spudec_thread_t * p_spudec;
62 intf_DbgMsg("spudec debug: creating spu decoder thread");
64 /* Allocate the memory needed to store the thread's structure */
65 p_spudec = (spudec_thread_t *)malloc( sizeof(spudec_thread_t) );
67 if ( p_spudec == NULL )
69 intf_ErrMsg( "spudec error: not enough memory "
70 "for spudec_CreateThread() to create the new thread" );
75 * Initialize the thread properties
77 p_spudec->p_config = p_config;
78 p_spudec->p_fifo = p_config->decoder_config.p_decoder_fifo;
80 /* Get the video output informations */
81 p_spudec->p_vout = p_config->p_vout;
83 /* Spawn the spu decoder thread */
84 if ( vlc_thread_create(&p_spudec->thread_id, "spu decoder",
85 (vlc_thread_func_t)RunThread, (void *)p_spudec) )
87 intf_ErrMsg("spudec error: can't spawn spu decoder thread");
92 intf_DbgMsg("spudec debug: spu decoder thread (%p) created", p_spudec);
93 return( p_spudec->thread_id );
96 /* following functions are local */
98 /*****************************************************************************
99 * InitThread: initialize spu decoder thread
100 *****************************************************************************
101 * This function is called from RunThread and performs the second step of the
102 * initialization. It returns 0 on success. Note that the thread's flag are not
103 * modified inside this function.
104 *****************************************************************************/
105 static int InitThread( spudec_thread_t *p_spudec )
107 intf_DbgMsg("spudec debug: initializing spu decoder thread %p", p_spudec);
109 p_spudec->p_config->decoder_config.pf_init_bit_stream(
110 &p_spudec->bit_stream,
111 p_spudec->p_config->decoder_config.p_decoder_fifo );
113 /* Mark thread as running and return */
114 intf_DbgMsg( "spudec debug: InitThread(%p) succeeded", p_spudec );
118 /*****************************************************************************
119 * RunThread: spu decoder thread
120 *****************************************************************************
121 * spu decoder thread. This function only returns when the thread is
123 *****************************************************************************/
124 static void RunThread( spudec_thread_t *p_spudec )
126 intf_DbgMsg("spudec debug: running spu decoder thread (%p) (pid == %i)",
130 * Initialize thread and free configuration
132 p_spudec->p_fifo->b_error = InitThread( p_spudec );
135 * Main loop - it is not executed if an error occured during
138 while( (!p_spudec->p_fifo->b_die) && (!p_spudec->p_fifo->b_error) )
144 subpicture_t * p_spu = NULL;
146 /* wait for the next SPU ID.
147 * XXX: We trash 0xff bytes since they probably come from
148 * an incomplete previous packet */
151 i_packet_size = GetBits( &p_spudec->bit_stream, 8 );
153 while( i_packet_size == 0xff );
155 if( p_spudec->p_fifo->b_die )
160 /* the total size - should equal the sum of the
161 * PES packet size that form the SPU packet */
162 i_packet_size = i_packet_size << 8
163 | GetBits( &p_spudec->bit_stream, 8 );
165 /* the RLE stuff size */
166 i_rle_size = GetBits( &p_spudec->bit_stream, 16 );
168 /* if the values we got aren't too strange, decode the data */
169 if( i_rle_size < i_packet_size )
171 /* allocate the subpicture.
172 * FIXME: we should check if the allocation failed */
173 p_spu = vout_CreateSubPicture( p_spudec->p_vout,
174 DVD_SUBPICTURE, i_rle_size );
175 /* get display time */
176 p_spu->begin_date = p_spu->end_date
177 = DECODER_FIFO_START(*p_spudec->p_fifo)->i_pts;
179 /* get RLE data, skip 4 bytes for the first two read offsets */
180 GetChunk( &p_spudec->bit_stream, p_spu->p_data,
183 if( p_spudec->p_fifo->b_die )
188 /* continue parsing after the RLE part */
189 i_index = i_rle_size;
191 /* assume packet is valid */
194 /* getting the control part */
200 /* Get the sequence date */
201 i_date = GetBits( &p_spudec->bit_stream, 16 );
204 i_next = GetBits( &p_spudec->bit_stream, 16 );
210 i_cmd = GetBits( &p_spudec->bit_stream, 8 );
215 case SPU_CMD_FORCE_DISPLAY:
216 /* 00 (force displaying) */
218 /* FIXME: here we have to calculate dates. It's
219 * around i_date * 12000 but I don't know
222 case SPU_CMD_START_DISPLAY:
223 /* 01 (start displaying) */
224 p_spu->begin_date += ( i_date * 12000 );
226 case SPU_CMD_STOP_DISPLAY:
227 /* 02 (stop displaying) */
228 p_spu->end_date += ( i_date * 12000 );
230 case SPU_CMD_SET_PALETTE:
231 /* 03xxxx (palette) - trashed */
232 RemoveBits( &p_spudec->bit_stream, 16 );
235 case SPU_CMD_SET_ALPHACHANNEL:
236 /* 04xxxx (alpha channel) - trashed */
237 RemoveBits( &p_spudec->bit_stream, 16 );
240 case SPU_CMD_SET_COORDINATES:
241 /* 05xxxyyyxxxyyy (coordinates) */
243 GetBits( &p_spudec->bit_stream, 12 );
245 p_spu->i_width = p_spu->i_x -
246 GetBits( &p_spudec->bit_stream, 12 ) + 1;
249 GetBits( &p_spudec->bit_stream, 12 );
251 p_spu->i_height = p_spu->i_y -
252 GetBits( &p_spudec->bit_stream, 12 ) + 1;
256 case SPU_CMD_SET_OFFSETS:
257 /* 06xxxxyyyy (byte offsets) */
258 p_spu->type.spu.i_offset[0] =
259 GetBits( &p_spudec->bit_stream, 16 ) - 4;
260 p_spu->type.spu.i_offset[1] =
261 GetBits( &p_spudec->bit_stream, 16 ) - 4;
268 /* ?? (unknown command) */
269 intf_ErrMsg( "spudec: unknown command 0x%.2x",
275 while( b_valid && ( i_cmd != SPU_CMD_END ) );
277 while( b_valid && ( i_index == i_next ) );
281 /* SPU is finished - we can tell the video output
283 vout_DisplaySubPicture( p_spudec->p_vout, p_spu );
287 vout_DestroySubPicture( p_spudec->p_vout, p_spu );
292 /* Unexpected PES packet - trash it */
293 intf_ErrMsg( "spudec: trying to recover from bad packet" );
294 vlc_mutex_lock( &p_spudec->p_fifo->data_lock );
295 p_spudec->p_fifo->pf_delete_pes( p_spudec->p_fifo->p_packets_mgt,
296 DECODER_FIFO_START(*p_spudec->p_fifo) );
297 DECODER_FIFO_INCSTART( *p_spudec->p_fifo );
298 vlc_mutex_unlock( &p_spudec->p_fifo->data_lock );
305 if( p_spudec->p_fifo->b_error )
307 ErrorThread( p_spudec );
311 EndThread( p_spudec );
314 /*****************************************************************************
315 * ErrorThread: RunThread() error loop
316 *****************************************************************************
317 * This function is called when an error occured during thread main's loop. The
318 * thread can still receive feed, but must be ready to terminate as soon as
320 *****************************************************************************/
321 static void ErrorThread( spudec_thread_t *p_spudec )
323 /* We take the lock, because we are going to read/write the start/end
324 * indexes of the decoder fifo */
325 vlc_mutex_lock( &p_spudec->p_fifo->data_lock );
327 /* Wait until a `die' order is sent */
328 while( !p_spudec->p_fifo->b_die )
330 /* Trash all received PES packets */
331 while( !DECODER_FIFO_ISEMPTY(*p_spudec->p_fifo) )
333 p_spudec->p_fifo->pf_delete_pes( p_spudec->p_fifo->p_packets_mgt,
334 DECODER_FIFO_START(*p_spudec->p_fifo) );
335 DECODER_FIFO_INCSTART( *p_spudec->p_fifo );
338 /* Waiting for the input thread to put new PES packets in the fifo */
339 vlc_cond_wait( &p_spudec->p_fifo->data_wait, &p_spudec->p_fifo->data_lock );
342 /* We can release the lock before leaving */
343 vlc_mutex_unlock( &p_spudec->p_fifo->data_lock );
346 /*****************************************************************************
347 * EndThread: thread destruction
348 *****************************************************************************
349 * This function is called when the thread ends after a sucessful
351 *****************************************************************************/
352 static void EndThread( spudec_thread_t *p_spudec )
354 intf_DbgMsg( "spudec debug: destroying spu decoder thread %p", p_spudec );
355 free( p_spudec->p_config );
357 intf_DbgMsg( "spudec debug: spu decoder thread %p destroyed", p_spudec);