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