1 /*****************************************************************************
2 * spu_decoder.c : spu decoder thread
3 *****************************************************************************
4 * Copyright (C) 2000 VideoLAN
5 * $Id: spu_decoder.c,v 1.34 2001/03/21 13:42:34 sam Exp $
7 * Authors: Samuel Hocevar <sam@zoy.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
29 #include <stdlib.h> /* malloc(), free() */
30 #include <unistd.h> /* getpid() */
39 #include "stream_control.h"
40 #include "input_ext-dec.h"
43 #include "video_output.h"
45 #include "spu_decoder.h"
47 /*****************************************************************************
49 *****************************************************************************/
50 static int InitThread ( spudec_thread_t * );
51 static void RunThread ( spudec_thread_t * );
52 static void ErrorThread ( spudec_thread_t * );
53 static void EndThread ( spudec_thread_t * );
55 static int SyncPacket ( spudec_thread_t * );
56 static void ParsePacket ( spudec_thread_t * );
57 static int ParseControlSequences( spudec_thread_t *, subpicture_t * );
58 static int ParseRLE ( u8 *, subpicture_t * );
60 /*****************************************************************************
61 * spudec_CreateThread: create a spu decoder thread
62 *****************************************************************************/
63 vlc_thread_t spudec_CreateThread( vdec_config_t * p_config )
65 spudec_thread_t * p_spudec;
67 /* Allocate the memory needed to store the thread's structure */
68 p_spudec = (spudec_thread_t *)malloc( sizeof(spudec_thread_t) );
70 if ( p_spudec == NULL )
72 intf_ErrMsg( "spudec error: not enough memory "
73 "for spudec_CreateThread() to create the new thread" );
78 * Initialize the thread properties
80 p_spudec->p_config = p_config;
81 p_spudec->p_fifo = p_config->decoder_config.p_decoder_fifo;
83 /* Get the video output informations */
84 p_spudec->p_vout = p_config->p_vout;
86 /* Spawn the spu decoder thread */
87 if ( vlc_thread_create(&p_spudec->thread_id, "spu decoder",
88 (vlc_thread_func_t)RunThread, (void *)p_spudec) )
90 intf_ErrMsg( "spudec error: can't spawn spu decoder thread" );
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 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 */
117 /*****************************************************************************
118 * RunThread: spu decoder thread
119 *****************************************************************************
120 * spu decoder thread. This function only returns when the thread is
122 *****************************************************************************/
123 static void RunThread( spudec_thread_t *p_spudec )
125 intf_WarnMsg( 1, "spudec: spu decoder thread %i spawned", getpid() );
128 * Initialize thread and free configuration
130 p_spudec->p_fifo->b_error = InitThread( p_spudec );
133 * Main loop - it is not executed if an error occured during
136 while( (!p_spudec->p_fifo->b_die) && (!p_spudec->p_fifo->b_error) )
138 if( !SyncPacket( p_spudec ) )
140 ParsePacket( p_spudec );
147 if( p_spudec->p_fifo->b_error )
149 ErrorThread( p_spudec );
153 intf_WarnMsg( 1, "spudec: destroying spu decoder thread %i", getpid() );
154 EndThread( p_spudec );
157 /*****************************************************************************
158 * ErrorThread: RunThread() error loop
159 *****************************************************************************
160 * This function is called when an error occured during thread main's loop. The
161 * thread can still receive feed, but must be ready to terminate as soon as
163 *****************************************************************************/
164 static void ErrorThread( spudec_thread_t *p_spudec )
166 /* We take the lock, because we are going to read/write the start/end
167 * indexes of the decoder fifo */
168 vlc_mutex_lock( &p_spudec->p_fifo->data_lock );
170 /* Wait until a `die' order is sent */
171 while( !p_spudec->p_fifo->b_die )
173 /* Trash all received PES packets */
174 while( !DECODER_FIFO_ISEMPTY(*p_spudec->p_fifo) )
176 p_spudec->p_fifo->pf_delete_pes( p_spudec->p_fifo->p_packets_mgt,
177 DECODER_FIFO_START(*p_spudec->p_fifo) );
178 DECODER_FIFO_INCSTART( *p_spudec->p_fifo );
181 /* Waiting for the input thread to put new PES packets in the fifo */
182 vlc_cond_wait( &p_spudec->p_fifo->data_wait,
183 &p_spudec->p_fifo->data_lock );
186 /* We can release the lock before leaving */
187 vlc_mutex_unlock( &p_spudec->p_fifo->data_lock );
190 /*****************************************************************************
191 * EndThread: thread destruction
192 *****************************************************************************
193 * This function is called when the thread ends after a sucessful
195 *****************************************************************************/
196 static void EndThread( spudec_thread_t *p_spudec )
198 free( p_spudec->p_config );
202 /*****************************************************************************
203 * SyncPacket: get in sync with the stream
204 *****************************************************************************
205 * This function makes a few sanity checks and returns 0 if it looks like we
206 * are at the beginning of a subpicture packet.
207 *****************************************************************************/
208 static int SyncPacket( spudec_thread_t *p_spudec )
210 /* Re-align the buffer on an 8-bit boundary */
211 RealignBits( &p_spudec->bit_stream );
213 /* The total SPU packet size, often bigger than a PS packet */
214 p_spudec->i_spu_size = GetBits( &p_spudec->bit_stream, 16 );
216 /* The RLE stuff size (remove 4 because we just read 32 bits) */
217 p_spudec->i_rle_size = ShowBits( &p_spudec->bit_stream, 16 ) - 4;
219 /* If the values we got are a bit strange, skip packet */
220 if( p_spudec->i_rle_size >= p_spudec->i_spu_size )
225 RemoveBits( &p_spudec->bit_stream, 16 );
230 /*****************************************************************************
231 * ParsePacket: parse an SPU packet and send it to the video output
232 *****************************************************************************
233 * This function parses the SPU packet and, if valid, sends it to the
235 *****************************************************************************/
236 static void ParsePacket( spudec_thread_t *p_spudec )
238 subpicture_t * p_spu;
241 /* We cannot display a subpicture with no date */
242 if( DECODER_FIFO_START(*p_spudec->p_fifo)->i_pts == 0 )
247 /* Allocate the subpicture internal data. */
248 p_spu = vout_CreateSubPicture( p_spudec->p_vout, DVD_SUBPICTURE,
249 p_spudec->i_rle_size * 4 );
250 /* Rationale for the "p_spudec->i_rle_size * 4": we are going to
251 * expand the RLE stuff so that we won't need to read nibbles later
252 * on. This will speed things up a lot. Plus, we'll only need to do
253 * this stupid interlacing stuff once. */
260 /* Get display time now. If we do it later, we may miss a PTS. */
261 p_spu->begin_date = p_spu->end_date
262 = DECODER_FIFO_START(*p_spudec->p_fifo)->i_pts;
264 /* Allocate the temporary buffer we will parse */
265 p_src = malloc( p_spudec->i_rle_size );
269 intf_ErrMsg( "spudec error: could not allocate p_src" );
270 vout_DestroySubPicture( p_spudec->p_vout, p_spu );
275 GetChunk( &p_spudec->bit_stream, p_src, p_spudec->i_rle_size );
278 /* Dump the subtitle info */
279 intf_WarnHexDump( 0, p_spu->p_data, p_spudec->i_rle_size );
282 /* Getting the control part */
283 if( ParseControlSequences( p_spudec, p_spu ) )
285 /* There was a parse error, delete the subpicture */
287 vout_DestroySubPicture( p_spudec->p_vout, p_spu );
291 if( ParseRLE( p_src, p_spu ) )
293 /* There was a parse error, delete the subpicture */
295 vout_DestroySubPicture( p_spudec->p_vout, p_spu );
299 intf_WarnMsg( 1, "spudec: got a valid %ix%i subtitle at (%i,%i), "
300 "RLE offsets: 0x%x 0x%x",
301 p_spu->i_width, p_spu->i_height, p_spu->i_x, p_spu->i_y,
302 p_spu->type.spu.i_offset[0], p_spu->type.spu.i_offset[1] );
304 /* SPU is finished - we can tell the video output to display it */
305 vout_DisplaySubPicture( p_spudec->p_vout, p_spu );
311 /*****************************************************************************
312 * ParseControlSequences: parse all SPU control sequences
313 *****************************************************************************
314 * This is the most important part in SPU decoding. We get dates, palette
315 * information, coordinates, and so on. For more information on the
316 * subtitles format, see http://sam.zoy.org/doc/dvd/subtitles/index.html
317 *****************************************************************************/
318 static int ParseControlSequences( spudec_thread_t *p_spudec,
319 subpicture_t * p_spu )
321 /* Our current index in the SPU packet */
322 int i_index = p_spudec->i_rle_size + 4;
324 /* The next start-of-control-sequence index and the previous one */
325 int i_next_index = 0, i_prev_index;
327 /* Command time and date */
333 /* Get the control sequence date */
334 i_date = GetBits( &p_spudec->bit_stream, 16 );
337 i_prev_index = i_next_index;
338 i_next_index = GetBits( &p_spudec->bit_stream, 16 );
340 /* Skip what we just read */
345 i_command = GetBits( &p_spudec->bit_stream, 8 );
350 case SPU_CMD_FORCE_DISPLAY:
352 /* 00 (force displaying) */
356 /* FIXME: here we have to calculate dates. It's around
357 * i_date * 12000 but I don't know how much exactly. */
358 case SPU_CMD_START_DISPLAY:
360 /* 01 (start displaying) */
361 p_spu->begin_date += ( i_date * 11000 );
365 case SPU_CMD_STOP_DISPLAY:
367 /* 02 (stop displaying) */
368 p_spu->end_date += ( i_date * 11000 );
372 case SPU_CMD_SET_PALETTE:
374 /* 03xxxx (palette) - trashed */
375 RemoveBits( &p_spudec->bit_stream, 16 );
380 case SPU_CMD_SET_ALPHACHANNEL:
382 /* 04xxxx (alpha channel) - trashed */
383 RemoveBits( &p_spudec->bit_stream, 16 );
388 case SPU_CMD_SET_COORDINATES:
390 /* 05xxxyyyxxxyyy (coordinates) */
391 p_spu->i_x = GetBits( &p_spudec->bit_stream, 12 );
392 p_spu->i_width = GetBits( &p_spudec->bit_stream, 12 )
395 p_spu->i_y = GetBits( &p_spudec->bit_stream, 12 );
396 p_spu->i_height = GetBits( &p_spudec->bit_stream, 12 )
403 case SPU_CMD_SET_OFFSETS:
405 /* 06xxxxyyyy (byte offsets) */
406 p_spu->type.spu.i_offset[0] =
407 GetBits( &p_spudec->bit_stream, 16 ) - 4;
409 p_spu->type.spu.i_offset[1] =
410 GetBits( &p_spudec->bit_stream, 16 ) - 4;
424 /* ?? (unknown command) */
425 intf_ErrMsg( "spudec error: unknown command 0x%.2x",
430 } while( i_command != SPU_CMD_END );
432 } while( i_index == i_next_index );
434 /* Check that the last index matches the previous one */
435 if( i_next_index != i_prev_index )
437 intf_ErrMsg( "spudec error: index mismatch (0x%.4x != 0x%.4x)",
438 i_next_index, i_prev_index );
442 if( i_index > p_spudec->i_spu_size )
444 intf_ErrMsg( "spudec error: uh-oh, we went too far (0x%.4x > 0x%.4x)",
445 i_index, p_spudec->i_spu_size );
449 /* Get rid of padding bytes */
450 switch( p_spudec->i_spu_size - i_index )
454 RemoveBits( &p_spudec->bit_stream, 8 );
459 /* Zero or one padding byte, quite usual */
465 /* More than one padding byte - this is very strange, but
466 * we can deal with it */
467 intf_WarnMsg( 2, "spudec warning: %i padding bytes",
468 p_spudec->i_spu_size - i_index );
470 while( i_index < p_spudec->i_spu_size )
472 RemoveBits( &p_spudec->bit_stream, 8 );
479 /* Successfully parsed ! */
483 /*****************************************************************************
484 * ParseRLE: parse the RLE part of the subtitle
485 *****************************************************************************
486 * This part parses the subtitle graphical data and stores it in a more
487 * convenient structure for later decoding. For more information on the
488 * subtitles format, see http://sam.zoy.org/doc/dvd/subtitles/index.html
489 *****************************************************************************/
490 static int ParseRLE( u8 *p_src, subpicture_t * p_spu )
493 unsigned int i_id = 0;
495 unsigned int i_width = p_spu->i_width;
496 unsigned int i_height = p_spu->i_height;
497 unsigned int i_x, i_y;
499 u16 *p_dest = (u16 *)p_spu->p_data;
501 /* The subtitles are interlaced, we need two offsets */
502 unsigned int pi_table[2];
503 unsigned int *pi_offset;
504 pi_table[0] = p_spu->type.spu.i_offset[0] << 1;
505 pi_table[1] = p_spu->type.spu.i_offset[1] << 1;
507 for( i_y = 0 ; i_y < i_height ; i_y++ )
509 pi_offset = pi_table + i_id;
511 for( i_x = 0 ; i_x < i_width ; i_x += i_code >> 2 )
513 i_code = AddNibble( 0, p_src, pi_offset );
517 i_code = AddNibble( i_code, p_src, pi_offset );
521 i_code = AddNibble( i_code, p_src, pi_offset );
525 i_code = AddNibble( i_code, p_src, pi_offset );
527 if( i_code < 0x0100 )
529 /* If the 14 first bits are set to 0, then it's a
530 * new line. We emulate it. */
531 if( i_code < 0x0004 )
533 i_code |= ( i_width - i_x ) << 2;
537 /* We have a boo boo ! */
538 intf_ErrMsg( "spudec error: unknown code %.4x",
547 if( ( (i_code >> 2) + i_x + i_y * i_width ) > i_height * i_width )
549 intf_ErrMsg( "spudec error: out of bounds, %i at (%i,%i) is "
551 i_code >> 2, i_x, i_y, i_width, i_height);
555 /* We got a valid code, store it */
559 /* Check that we didn't go too far */
562 intf_ErrMsg( "spudec error: i_x overflowed, %i > %i",
567 /* Byte-align the stream */
568 if( *pi_offset & 0x1 )
577 /* FIXME: we shouldn't need these padding bytes */
578 while( i_y < i_height )
580 *p_dest++ = i_width << 2;