1 /*****************************************************************************
2 * spu_decoder.c : spu decoder thread
3 *****************************************************************************
4 * Copyright (C) 2000 VideoLAN
6 * Authors: Samuel Hocevar <sam@zoy.org>
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() */
38 #include "stream_control.h"
39 #include "input_ext-dec.h"
42 #include "video_output.h"
44 #include "spu_decoder.h"
46 /*****************************************************************************
48 *****************************************************************************/
49 static int InitThread ( spudec_thread_t * );
50 static void RunThread ( spudec_thread_t * );
51 static void ErrorThread ( spudec_thread_t * );
52 static void EndThread ( spudec_thread_t * );
54 static int SyncPacket ( spudec_thread_t * );
55 static void ParsePacket ( spudec_thread_t * );
56 static int ParseRLE ( spudec_thread_t *, subpicture_t * );
57 static int ParseControlSequences( spudec_thread_t *, subpicture_t * );
59 /*****************************************************************************
60 * spudec_CreateThread: create a spu decoder thread
61 *****************************************************************************/
62 vlc_thread_t spudec_CreateThread( vdec_config_t * p_config )
64 spudec_thread_t * p_spudec;
66 /* Allocate the memory needed to store the thread's structure */
67 p_spudec = (spudec_thread_t *)malloc( sizeof(spudec_thread_t) );
69 if ( p_spudec == NULL )
71 intf_ErrMsg( "spudec error: not enough memory "
72 "for spudec_CreateThread() to create the new thread" );
77 * Initialize the thread properties
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 return( p_spudec->thread_id );
97 /* following functions are local */
99 /*****************************************************************************
100 * InitThread: initialize spu decoder thread
101 *****************************************************************************
102 * This function is called from RunThread and performs the second step of the
103 * initialization. It returns 0 on success. Note that the thread's flag are not
104 * modified inside this function.
105 *****************************************************************************/
106 static int InitThread( spudec_thread_t *p_spudec )
108 p_spudec->p_config->decoder_config.pf_init_bit_stream(
109 &p_spudec->bit_stream,
110 p_spudec->p_config->decoder_config.p_decoder_fifo );
112 /* Mark thread as running and return */
116 /*****************************************************************************
117 * RunThread: spu decoder thread
118 *****************************************************************************
119 * spu decoder thread. This function only returns when the thread is
121 *****************************************************************************/
122 static void RunThread( spudec_thread_t *p_spudec )
124 intf_WarnMsg( 1, "spudec: spu decoder thread %i spawned", getpid() );
127 * Initialize thread and free configuration
129 p_spudec->p_fifo->b_error = InitThread( p_spudec );
132 * Main loop - it is not executed if an error occured during
135 while( (!p_spudec->p_fifo->b_die) && (!p_spudec->p_fifo->b_error) )
137 if( !SyncPacket( p_spudec ) )
139 ParsePacket( p_spudec );
146 if( p_spudec->p_fifo->b_error )
148 ErrorThread( p_spudec );
152 intf_WarnMsg( 1, "spudec: destroying spu decoder thread %i", getpid() );
153 EndThread( p_spudec );
156 /*****************************************************************************
157 * ErrorThread: RunThread() error loop
158 *****************************************************************************
159 * This function is called when an error occured during thread main's loop. The
160 * thread can still receive feed, but must be ready to terminate as soon as
162 *****************************************************************************/
163 static void ErrorThread( spudec_thread_t *p_spudec )
165 /* We take the lock, because we are going to read/write the start/end
166 * indexes of the decoder fifo */
167 vlc_mutex_lock( &p_spudec->p_fifo->data_lock );
169 /* Wait until a `die' order is sent */
170 while( !p_spudec->p_fifo->b_die )
172 /* Trash all received PES packets */
173 while( !DECODER_FIFO_ISEMPTY(*p_spudec->p_fifo) )
175 p_spudec->p_fifo->pf_delete_pes( p_spudec->p_fifo->p_packets_mgt,
176 DECODER_FIFO_START(*p_spudec->p_fifo) );
177 DECODER_FIFO_INCSTART( *p_spudec->p_fifo );
180 /* Waiting for the input thread to put new PES packets in the fifo */
181 vlc_cond_wait( &p_spudec->p_fifo->data_wait,
182 &p_spudec->p_fifo->data_lock );
185 /* We can release the lock before leaving */
186 vlc_mutex_unlock( &p_spudec->p_fifo->data_lock );
189 /*****************************************************************************
190 * EndThread: thread destruction
191 *****************************************************************************
192 * This function is called when the thread ends after a sucessful
194 *****************************************************************************/
195 static void EndThread( spudec_thread_t *p_spudec )
197 free( p_spudec->p_config );
201 /*****************************************************************************
202 * SyncPacket: get in sync with the stream
203 *****************************************************************************
204 * This function makes a few sanity checks and returns 0 if it looks like we
205 * are at the beginning of a subpicture packet.
206 *****************************************************************************/
207 static int SyncPacket( spudec_thread_t *p_spudec )
209 /* Re-align the buffer on an 8-bit boundary */
210 RealignBits( &p_spudec->bit_stream );
212 /* The total SPU packet size, often bigger than a PS packet */
213 p_spudec->i_spu_size = GetBits( &p_spudec->bit_stream, 16 );
215 /* The RLE stuff size */
216 p_spudec->i_rle_size = GetBits( &p_spudec->bit_stream, 16 );
218 /* If the values we got are a bit strange, skip packet */
219 if( p_spudec->i_rle_size >= p_spudec->i_spu_size )
227 /*****************************************************************************
228 * ParsePacket: parse an SPU packet and send it to the video output
229 *****************************************************************************
230 * This function parses the SPU packet and, if valid, sends it to the
232 *****************************************************************************/
233 static void ParsePacket( spudec_thread_t *p_spudec )
235 subpicture_t * p_spu;
237 /* Allocate the subpicture internal data. */
238 p_spu = vout_CreateSubPicture( p_spudec->p_vout, DVD_SUBPICTURE,
239 p_spudec->i_rle_size );
246 /* Get display time */
247 p_spu->begin_date = p_spu->end_date
248 = DECODER_FIFO_START(*p_spudec->p_fifo)->i_pts;
250 if( ParseRLE( p_spudec, p_spu ) )
252 /* There was a parse error, delete the subpicture */
253 vout_DestroySubPicture( p_spudec->p_vout, p_spu );
257 /* Dump the subtitle info */
258 intf_WarnHexDump( 0, p_spu->p_data, p_spudec->i_rle_size - 4 );
260 /* Getting the control part */
261 if( ParseControlSequences( p_spudec, p_spu ) )
263 /* There was a parse error, delete the subpicture */
264 vout_DestroySubPicture( p_spudec->p_vout, p_spu );
268 intf_WarnMsg( 1, "spudec: got a valid %ix%i subtitle at (%i,%i), "
269 "RLE offsets: 0x%x 0x%x",
270 p_spu->i_width, p_spu->i_height, p_spu->i_x, p_spu->i_y,
271 p_spu->type.spu.i_offset[0], p_spu->type.spu.i_offset[1] );
273 /* SPU is finished - we can tell the video output to display it */
274 vout_DisplaySubPicture( p_spudec->p_vout, p_spu );
277 /*****************************************************************************
278 * ParseRLE: parse the RLE part of the subtitle
279 *****************************************************************************
280 * This part parses the subtitle graphical data and stores it in a more
281 * convenient structure for later decoding. For more information on the
282 * subtitles format, see http://sam.zoy.org/doc/dvd/subtitles/index.html
283 * TODO: pre-parse the RLE stuff here.
284 *****************************************************************************/
285 static int ParseRLE( spudec_thread_t *p_spudec, subpicture_t * p_spu )
287 /* Get RLE data, skip 4 bytes for the first two read offsets */
288 GetChunk( &p_spudec->bit_stream, p_spu->p_data, p_spudec->i_rle_size - 4 );
290 if( p_spudec->p_fifo->b_die )
298 /*****************************************************************************
299 * ParseControlSequences: parse all SPU control sequences
300 *****************************************************************************
301 * This is the most important part in SPU decoding. We get dates, palette
302 * information, coordinates, and so on. For more information on the
303 * subtitles format, see http://sam.zoy.org/doc/dvd/subtitles/index.html
304 *****************************************************************************/
305 static int ParseControlSequences( spudec_thread_t *p_spudec,
306 subpicture_t * p_spu )
308 int i_index = p_spudec->i_rle_size;
309 int i_next_index = 0, i_prev_index;
316 /* Get the control sequence date */
317 i_date = GetBits( &p_spudec->bit_stream, 16 );
320 i_prev_index = i_next_index;
321 i_next_index = GetBits( &p_spudec->bit_stream, 16 );
328 i_command = GetBits( &p_spudec->bit_stream, 8 );
333 case SPU_CMD_FORCE_DISPLAY:
335 /* 00 (force displaying) */
339 /* FIXME: here we have to calculate dates. It's around
340 * i_date * 12000 but I don't know how much exactly. */
341 case SPU_CMD_START_DISPLAY:
343 /* 01 (start displaying) */
344 p_spu->begin_date += ( i_date * 12000 );
348 case SPU_CMD_STOP_DISPLAY:
350 /* 02 (stop displaying) */
351 p_spu->end_date += ( i_date * 12000 );
355 case SPU_CMD_SET_PALETTE:
357 /* 03xxxx (palette) - trashed */
358 RemoveBits( &p_spudec->bit_stream, 16 );
363 case SPU_CMD_SET_ALPHACHANNEL:
365 /* 04xxxx (alpha channel) - trashed */
366 RemoveBits( &p_spudec->bit_stream, 16 );
371 case SPU_CMD_SET_COORDINATES:
373 /* 05xxxyyyxxxyyy (coordinates) */
374 p_spu->i_x = GetBits( &p_spudec->bit_stream, 12 );
375 p_spu->i_width = GetBits( &p_spudec->bit_stream, 12 )
378 p_spu->i_y = GetBits( &p_spudec->bit_stream, 12 );
379 p_spu->i_height = GetBits( &p_spudec->bit_stream, 12 )
386 case SPU_CMD_SET_OFFSETS:
388 /* 06xxxxyyyy (byte offsets) */
389 p_spu->type.spu.i_offset[0] =
390 GetBits( &p_spudec->bit_stream, 16 ) - 4;
392 p_spu->type.spu.i_offset[1] =
393 GetBits( &p_spudec->bit_stream, 16 ) - 4;
407 /* ?? (unknown command) */
408 intf_ErrMsg( "spudec error: unknown command 0x%.2x",
413 } while( i_command != SPU_CMD_END );
415 } while( i_index == i_next_index );
417 /* Check that the last index matches the previous one */
418 if( i_next_index != i_prev_index )
420 intf_ErrMsg( "spudec error: index mismatch (0x%.4x != 0x%.4x)",
421 i_next_index, i_prev_index );
425 if( i_index > p_spudec->i_spu_size )
427 intf_ErrMsg( "spudec error: uh-oh, we went too far (0x%.4x > 0x%.4x)",
428 i_index, p_spudec->i_spu_size );
432 /* Get rid of padding bytes */
433 switch( p_spudec->i_spu_size - i_index )
437 RemoveBits( &p_spudec->bit_stream, 8 );
442 /* Zero or one padding byte, quite usual */
448 /* More than one padding byte - this is very strange, but
449 * we can deal with it */
450 intf_WarnMsg( 2, "spudec warning: %i padding bytes",
451 p_spudec->i_spu_size - i_index );
453 while( i_index < p_spudec->i_spu_size )
455 RemoveBits( &p_spudec->bit_stream, 8 );
462 /* Successfully parsed ! */