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