]> git.sesse.net Git - vlc/blob - src/spu_decoder/spu_decoder.c
* Code cleanup: removed a few tabulations.
[vlc] / src / spu_decoder / spu_decoder.c
1 /*****************************************************************************
2  * spu_decoder.c : spu decoder thread
3  *****************************************************************************
4  * Copyright (C) 2000 VideoLAN
5  * $Id: spu_decoder.c,v 1.35 2001/04/06 09:15:48 sam Exp $
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *
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.
13  * 
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include "defs.h"
28
29 #include <unistd.h>                                              /* getpid() */
30
31 #include <stdlib.h>                                      /* malloc(), free() */
32 #include <string.h>                                    /* memcpy(), memset() */
33
34 #include "config.h"
35 #include "common.h"
36 #include "threads.h"
37 #include "mtime.h"
38
39 #include "intf_msg.h"
40
41 #include "stream_control.h"
42 #include "input_ext-dec.h"
43
44 #include "video.h"
45 #include "video_output.h"
46
47 #include "spu_decoder.h"
48
49 /*****************************************************************************
50  * Local prototypes
51  *****************************************************************************/
52 static int  InitThread  ( spudec_thread_t * );
53 static void RunThread   ( spudec_thread_t * );
54 static void ErrorThread ( spudec_thread_t * );
55 static void EndThread   ( spudec_thread_t * );
56
57 static int  SyncPacket           ( spudec_thread_t * );
58 static void ParsePacket          ( spudec_thread_t * );
59 static int  ParseControlSequences( spudec_thread_t *, subpicture_t * );
60 static int  ParseRLE             ( u8 *,              subpicture_t * );
61
62 /*****************************************************************************
63  * spudec_CreateThread: create a spu decoder thread
64  *****************************************************************************/
65 vlc_thread_t spudec_CreateThread( vdec_config_t * p_config )
66 {
67     spudec_thread_t *     p_spudec;
68
69     /* Allocate the memory needed to store the thread's structure */
70     p_spudec = (spudec_thread_t *)malloc( sizeof(spudec_thread_t) );
71
72     if ( p_spudec == NULL )
73     {
74         intf_ErrMsg( "spudec error: not enough memory "
75                      "for spudec_CreateThread() to create the new thread" );
76         return( 0 );
77     }
78
79     /*
80      * Initialize the thread properties
81      */
82     p_spudec->p_config = p_config;
83     p_spudec->p_fifo = p_config->decoder_config.p_decoder_fifo;
84
85     /* Get the video output informations */
86     p_spudec->p_vout = p_config->p_vout;
87
88     /* Spawn the spu decoder thread */
89     if ( vlc_thread_create(&p_spudec->thread_id, "spu decoder",
90          (vlc_thread_func_t)RunThread, (void *)p_spudec) )
91     {
92         intf_ErrMsg( "spudec error: can't spawn spu decoder thread" );
93         free( p_spudec );
94         return( 0 );
95     }
96
97     return( p_spudec->thread_id );
98 }
99
100 /* following functions are local */
101
102 /*****************************************************************************
103  * InitThread: initialize spu decoder thread
104  *****************************************************************************
105  * This function is called from RunThread and performs the second step of the
106  * initialization. It returns 0 on success. Note that the thread's flag are not
107  * modified inside this function.
108  *****************************************************************************/
109 static int InitThread( spudec_thread_t *p_spudec )
110 {
111     p_spudec->p_config->decoder_config.pf_init_bit_stream(
112             &p_spudec->bit_stream,
113             p_spudec->p_config->decoder_config.p_decoder_fifo );
114
115     /* Mark thread as running and return */
116     return( 0 );
117 }
118
119 /*****************************************************************************
120  * RunThread: spu decoder thread
121  *****************************************************************************
122  * spu decoder thread. This function only returns when the thread is
123  * terminated.
124  *****************************************************************************/
125 static void RunThread( spudec_thread_t *p_spudec )
126 {
127     intf_WarnMsg( 1, "spudec: spu decoder thread %i spawned", getpid() );
128
129     /*
130      * Initialize thread and free configuration
131      */
132     p_spudec->p_fifo->b_error = InitThread( p_spudec );
133
134     /*
135      * Main loop - it is not executed if an error occured during
136      * initialization
137      */
138     while( (!p_spudec->p_fifo->b_die) && (!p_spudec->p_fifo->b_error) )
139     {
140         if( !SyncPacket( p_spudec ) )
141         {
142             ParsePacket( p_spudec );
143         }
144     }
145
146     /*
147      * Error loop
148      */
149     if( p_spudec->p_fifo->b_error )
150     {
151         ErrorThread( p_spudec );
152     }
153
154     /* End of thread */
155     intf_WarnMsg( 1, "spudec: destroying spu decoder thread %i", getpid() );
156     EndThread( p_spudec );
157 }
158
159 /*****************************************************************************
160  * ErrorThread: RunThread() error loop
161  *****************************************************************************
162  * This function is called when an error occured during thread main's loop. The
163  * thread can still receive feed, but must be ready to terminate as soon as
164  * possible.
165  *****************************************************************************/
166 static void ErrorThread( spudec_thread_t *p_spudec )
167 {
168     /* We take the lock, because we are going to read/write the start/end
169      * indexes of the decoder fifo */
170     vlc_mutex_lock( &p_spudec->p_fifo->data_lock );
171
172     /* Wait until a `die' order is sent */
173     while( !p_spudec->p_fifo->b_die )
174     {
175         /* Trash all received PES packets */
176         while( !DECODER_FIFO_ISEMPTY(*p_spudec->p_fifo) )
177         {
178             p_spudec->p_fifo->pf_delete_pes( p_spudec->p_fifo->p_packets_mgt,
179                     DECODER_FIFO_START(*p_spudec->p_fifo) );
180             DECODER_FIFO_INCSTART( *p_spudec->p_fifo );
181         }
182
183         /* Waiting for the input thread to put new PES packets in the fifo */
184         vlc_cond_wait( &p_spudec->p_fifo->data_wait,
185                        &p_spudec->p_fifo->data_lock );
186     }
187
188     /* We can release the lock before leaving */
189     vlc_mutex_unlock( &p_spudec->p_fifo->data_lock );
190 }
191
192 /*****************************************************************************
193  * EndThread: thread destruction
194  *****************************************************************************
195  * This function is called when the thread ends after a sucessful
196  * initialization.
197  *****************************************************************************/
198 static void EndThread( spudec_thread_t *p_spudec )
199 {
200     free( p_spudec->p_config );
201     free( p_spudec );
202 }
203
204 /*****************************************************************************
205  * SyncPacket: get in sync with the stream
206  *****************************************************************************
207  * This function makes a few sanity checks and returns 0 if it looks like we
208  * are at the beginning of a subpicture packet.
209  *****************************************************************************/
210 static int SyncPacket( spudec_thread_t *p_spudec )
211 {
212     /* Re-align the buffer on an 8-bit boundary */
213     RealignBits( &p_spudec->bit_stream );
214
215     /* The total SPU packet size, often bigger than a PS packet */
216     p_spudec->i_spu_size = GetBits( &p_spudec->bit_stream, 16 );
217
218     /* The RLE stuff size (remove 4 because we just read 32 bits) */
219     p_spudec->i_rle_size = ShowBits( &p_spudec->bit_stream, 16 ) - 4;
220
221     /* If the values we got are a bit strange, skip packet */
222     if( p_spudec->i_rle_size >= p_spudec->i_spu_size )
223     {
224         return( 1 );
225     }
226
227     RemoveBits( &p_spudec->bit_stream, 16 );
228
229     return( 0 );
230 }
231
232 /*****************************************************************************
233  * ParsePacket: parse an SPU packet and send it to the video output
234  *****************************************************************************
235  * This function parses the SPU packet and, if valid, sends it to the
236  * video output.
237  *****************************************************************************/
238 static void ParsePacket( spudec_thread_t *p_spudec )
239 {
240     subpicture_t * p_spu;
241     u8           * p_src;
242
243     /* We cannot display a subpicture with no date */
244     if( DECODER_FIFO_START(*p_spudec->p_fifo)->i_pts == 0 )
245     {
246         return;
247     }
248
249     /* Allocate the subpicture internal data. */
250     p_spu = vout_CreateSubPicture( p_spudec->p_vout, DVD_SUBPICTURE,
251                                    p_spudec->i_rle_size * 4 );
252     /* Rationale for the "p_spudec->i_rle_size * 4": we are going to
253      * expand the RLE stuff so that we won't need to read nibbles later
254      * on. This will speed things up a lot. Plus, we'll only need to do
255      * this stupid interlacing stuff once. */
256
257     if( p_spu == NULL )
258     {
259         return;
260     }
261
262     /* Get display time now. If we do it later, we may miss a PTS. */
263     p_spu->begin_date = p_spu->end_date
264                     = DECODER_FIFO_START(*p_spudec->p_fifo)->i_pts;
265
266     /* Allocate the temporary buffer we will parse */
267     p_src = malloc( p_spudec->i_rle_size );
268
269     if( p_src == NULL )
270     {
271         intf_ErrMsg( "spudec error: could not allocate p_src" );
272         vout_DestroySubPicture( p_spudec->p_vout, p_spu );
273         return;
274     }
275
276     /* Get RLE data */
277     GetChunk( &p_spudec->bit_stream, p_src, p_spudec->i_rle_size );
278
279 #if 0
280     /* Dump the subtitle info */
281     intf_WarnHexDump( 0, p_spu->p_data, p_spudec->i_rle_size );
282 #endif
283
284     /* Getting the control part */
285     if( ParseControlSequences( p_spudec, p_spu ) )
286     {
287         /* There was a parse error, delete the subpicture */
288         free( p_src );
289         vout_DestroySubPicture( p_spudec->p_vout, p_spu );
290         return;
291     }
292
293     if( ParseRLE( p_src, p_spu ) )
294     {
295         /* There was a parse error, delete the subpicture */
296         free( p_src );
297         vout_DestroySubPicture( p_spudec->p_vout, p_spu );
298         return;
299     }
300
301     intf_WarnMsg( 1, "spudec: got a valid %ix%i subtitle at (%i,%i), "
302                      "RLE offsets: 0x%x 0x%x",
303                   p_spu->i_width, p_spu->i_height, p_spu->i_x, p_spu->i_y,
304                   p_spu->type.spu.i_offset[0], p_spu->type.spu.i_offset[1] );
305
306     /* SPU is finished - we can tell the video output to display it */
307     vout_DisplaySubPicture( p_spudec->p_vout, p_spu );
308
309     /* Clean up */
310     free( p_src );
311 }
312
313 /*****************************************************************************
314  * ParseControlSequences: parse all SPU control sequences
315  *****************************************************************************
316  * This is the most important part in SPU decoding. We get dates, palette
317  * information, coordinates, and so on. For more information on the
318  * subtitles format, see http://sam.zoy.org/doc/dvd/subtitles/index.html
319  *****************************************************************************/
320 static int ParseControlSequences( spudec_thread_t *p_spudec,
321                                   subpicture_t * p_spu )
322 {
323     /* Our current index in the SPU packet */
324     int i_index = p_spudec->i_rle_size + 4;
325
326     /* The next start-of-control-sequence index and the previous one */
327     int i_next_index = 0, i_prev_index;
328
329     /* Command time and date */
330     u8  i_command;
331     int i_date;
332
333     do
334     {
335         /* Get the control sequence date */
336         i_date = GetBits( &p_spudec->bit_stream, 16 );
337  
338         /* Next offset */
339         i_prev_index = i_next_index;
340         i_next_index = GetBits( &p_spudec->bit_stream, 16 );
341  
342         /* Skip what we just read */
343         i_index += 4;
344  
345         do
346         {
347             i_command = GetBits( &p_spudec->bit_stream, 8 );
348             i_index++;
349  
350             switch( i_command )
351             {
352                 case SPU_CMD_FORCE_DISPLAY:
353  
354                     /* 00 (force displaying) */
355  
356                     break;
357  
358                 /* FIXME: here we have to calculate dates. It's around
359                  * i_date * 12000 but I don't know how much exactly. */
360                 case SPU_CMD_START_DISPLAY:
361  
362                     /* 01 (start displaying) */
363                     p_spu->begin_date += ( i_date * 11000 );
364  
365                     break;
366  
367                 case SPU_CMD_STOP_DISPLAY:
368  
369                     /* 02 (stop displaying) */
370                     p_spu->end_date += ( i_date * 11000 );
371  
372                     break;
373  
374                 case SPU_CMD_SET_PALETTE:
375  
376                     /* 03xxxx (palette) - trashed */
377                     RemoveBits( &p_spudec->bit_stream, 16 );
378                     i_index += 2;
379  
380                     break;
381  
382                 case SPU_CMD_SET_ALPHACHANNEL:
383  
384                     /* 04xxxx (alpha channel) - trashed */
385                     RemoveBits( &p_spudec->bit_stream, 16 );
386                     i_index += 2;
387  
388                     break;
389  
390                 case SPU_CMD_SET_COORDINATES:
391  
392                     /* 05xxxyyyxxxyyy (coordinates) */
393                     p_spu->i_x = GetBits( &p_spudec->bit_stream, 12 );
394                     p_spu->i_width = GetBits( &p_spudec->bit_stream, 12 )
395                                       - p_spu->i_x + 1;
396  
397                     p_spu->i_y = GetBits( &p_spudec->bit_stream, 12 );
398                     p_spu->i_height = GetBits( &p_spudec->bit_stream, 12 )
399                                        - p_spu->i_y + 1;
400  
401                     i_index += 6;
402  
403                     break;
404  
405                 case SPU_CMD_SET_OFFSETS:
406  
407                     /* 06xxxxyyyy (byte offsets) */
408                     p_spu->type.spu.i_offset[0] =
409                         GetBits( &p_spudec->bit_stream, 16 ) - 4;
410  
411                     p_spu->type.spu.i_offset[1] =
412                         GetBits( &p_spudec->bit_stream, 16 ) - 4;
413  
414                     i_index += 4;
415  
416                     break;
417  
418                 case SPU_CMD_END:
419  
420                     /* ff (end) */
421  
422                     break;
423  
424                 default:
425  
426                     /* ?? (unknown command) */
427                     intf_ErrMsg( "spudec error: unknown command 0x%.2x",
428                                  i_command );
429                     return( 1 );
430             }
431
432         } while( i_command != SPU_CMD_END );
433
434     } while( i_index == i_next_index );
435
436     /* Check that the last index matches the previous one */
437     if( i_next_index != i_prev_index )
438     {
439         intf_ErrMsg( "spudec error: index mismatch (0x%.4x != 0x%.4x)",
440                      i_next_index, i_prev_index );
441         return( 1 );
442     }
443
444     if( i_index > p_spudec->i_spu_size )
445     {
446         intf_ErrMsg( "spudec error: uh-oh, we went too far (0x%.4x > 0x%.4x)",
447                      i_index, p_spudec->i_spu_size );
448         return( 1 );
449     }
450
451     /* Get rid of padding bytes */
452     switch( p_spudec->i_spu_size - i_index )
453     {
454         case 1:
455
456             RemoveBits( &p_spudec->bit_stream, 8 );
457             i_index++;
458
459         case 0:
460
461             /* Zero or one padding byte, quite usual */
462
463             break;
464
465         default:
466
467             /* More than one padding byte - this is very strange, but
468              * we can deal with it */
469             intf_WarnMsg( 2, "spudec warning: %i padding bytes",
470                           p_spudec->i_spu_size - i_index );
471
472             while( i_index < p_spudec->i_spu_size )
473             {
474                 RemoveBits( &p_spudec->bit_stream, 8 );
475                 i_index++;
476             }
477
478             break;
479     }
480
481     /* Successfully parsed ! */
482     return( 0 );
483 }
484
485 /*****************************************************************************
486  * ParseRLE: parse the RLE part of the subtitle
487  *****************************************************************************
488  * This part parses the subtitle graphical data and stores it in a more
489  * convenient structure for later decoding. For more information on the
490  * subtitles format, see http://sam.zoy.org/doc/dvd/subtitles/index.html
491  *****************************************************************************/
492 static int ParseRLE( u8 *p_src, subpicture_t * p_spu )
493 {
494     unsigned int i_code;
495     unsigned int i_id = 0;
496
497     unsigned int i_width = p_spu->i_width;
498     unsigned int i_height = p_spu->i_height;
499     unsigned int i_x, i_y;
500
501     u16 *p_dest = (u16 *)p_spu->p_data;
502
503     /* The subtitles are interlaced, we need two offsets */
504     unsigned int  pi_table[2];
505     unsigned int *pi_offset;
506     pi_table[0] = p_spu->type.spu.i_offset[0] << 1;
507     pi_table[1] = p_spu->type.spu.i_offset[1] << 1;
508
509     for( i_y = 0 ; i_y < i_height ; i_y++ )
510     {
511         pi_offset = pi_table + i_id;
512
513         for( i_x = 0 ; i_x < i_width ; i_x += i_code >> 2 )
514         {
515             i_code = AddNibble( 0, p_src, pi_offset );
516
517             if( i_code < 0x04 )
518             {
519                 i_code = AddNibble( i_code, p_src, pi_offset );
520
521                 if( i_code < 0x10 )
522                 {
523                     i_code = AddNibble( i_code, p_src, pi_offset );
524
525                     if( i_code < 0x040 )
526                     {
527                         i_code = AddNibble( i_code, p_src, pi_offset );
528
529                         if( i_code < 0x0100 )
530                         {
531                             /* If the 14 first bits are set to 0, then it's a
532                              * new line. We emulate it. */
533                             if( i_code < 0x0004 )
534                             {
535                                 i_code |= ( i_width - i_x ) << 2;
536                             }
537                             else
538                             {
539                                 /* We have a boo boo ! */
540                                 intf_ErrMsg( "spudec error: unknown code %.4x",
541                                              i_code );
542                                 return( 1 );
543                             }
544                         }
545                     }
546                 }
547             }
548
549             if( ( (i_code >> 2) + i_x + i_y * i_width ) > i_height * i_width )
550             {
551                 intf_ErrMsg( "spudec error: out of bounds, %i at (%i,%i) is "
552                              "out of %ix%i",
553                              i_code >> 2, i_x, i_y, i_width, i_height);
554                 return( 1 );
555             }
556
557             /* We got a valid code, store it */
558             *p_dest++ = i_code;
559         }
560
561         /* Check that we didn't go too far */
562         if( i_x > i_width )
563         {
564             intf_ErrMsg( "spudec error: i_x overflowed, %i > %i",
565                          i_x, i_width );
566             return( 1 );
567         }
568
569         /* Byte-align the stream */
570         if( *pi_offset & 0x1 )
571         {
572             (*pi_offset)++;
573         }
574
575         /* Swap fields */
576         i_id = ~i_id & 0x1;
577     }
578
579     /* FIXME: we shouldn't need these padding bytes */
580     while( i_y < i_height )
581     {
582         *p_dest++ = i_width << 2;
583         i_y++;
584     }
585
586     return( 0 );
587 }
588