]> git.sesse.net Git - vlc/blobdiff - src/spu_decoder/spu_decoder.c
* DirectX plugin by Gildas Bazin <gbazin@netcourrier.com>.
[vlc] / src / spu_decoder / spu_decoder.c
index a5e9ed2701637f684c34a4a94fbc5b58e429e134..1d02bb0dd2f44ad6487730f24a0a06a1ff3d28c9 100644 (file)
@@ -2,6 +2,7 @@
  * spu_decoder.c : spu decoder thread
  *****************************************************************************
  * Copyright (C) 2000 VideoLAN
+ * $Id: spu_decoder.c,v 1.47 2001/06/02 01:09:03 sam Exp $
  *
  * Authors: Samuel Hocevar <sam@zoy.org>
  *
  *****************************************************************************/
 #include "defs.h"
 
-#include <stdlib.h>                                      /* malloc(), free() */
+#ifdef HAVE_UNISTD_H
 #include <unistd.h>                                              /* getpid() */
+#endif
+
+#ifdef WIN32                   /* getpid() for win32 is located in process.h */
+#include <process.h>
+#endif
+
+#include <stdlib.h>                                      /* malloc(), free() */
+#include <string.h>                                    /* memcpy(), memset() */
 
 #include "config.h"
 #include "common.h"
@@ -54,7 +63,7 @@ static void EndThread   ( spudec_thread_t * );
 static int  SyncPacket           ( spudec_thread_t * );
 static void ParsePacket          ( spudec_thread_t * );
 static int  ParseControlSequences( spudec_thread_t *, subpicture_t * );
-static int  ParseRLE             ( u8 *,              subpicture_t * );
+static int  ParseRLE             ( spudec_thread_t *, subpicture_t *, u8 * );
 
 /*****************************************************************************
  * spudec_CreateThread: create a spu decoder thread
@@ -77,10 +86,38 @@ vlc_thread_t spudec_CreateThread( vdec_config_t * p_config )
      * Initialize the thread properties
      */
     p_spudec->p_config = p_config;
+
     p_spudec->p_fifo = p_config->decoder_config.p_decoder_fifo;
 
-    /* Get the video output informations */
-    p_spudec->p_vout = p_config->p_vout;
+    /* Spawn a video output if there is none */
+    vlc_mutex_lock( &p_vout_bank->lock );
+
+    if( p_vout_bank->i_count == 0 )
+    {
+        intf_WarnMsg( 1, "spudec: no vout present, spawning one" );
+
+        p_spudec->p_vout = vout_CreateThread( NULL );
+
+        /* Everything failed */
+        if( p_spudec->p_vout == NULL )
+        {
+            intf_Msg( "spudec: can't open vout, aborting" );
+            vlc_mutex_unlock( &p_vout_bank->lock );
+            free( p_spudec );
+
+            return 0;
+        }
+
+        p_vout_bank->pp_vout[ p_vout_bank->i_count ] = p_spudec->p_vout;
+        p_vout_bank->i_count++;
+    }
+    else
+    {
+        /* Take the first video output FIXME: take the best one */
+        p_spudec->p_vout = p_vout_bank->pp_vout[ 0 ];
+    }
+
+    vlc_mutex_unlock( &p_vout_bank->lock );
 
     /* Spawn the spu decoder thread */
     if ( vlc_thread_create(&p_spudec->thread_id, "spu decoder",
@@ -88,7 +125,7 @@ vlc_thread_t spudec_CreateThread( vdec_config_t * p_config )
     {
         intf_ErrMsg( "spudec error: can't spawn spu decoder thread" );
         free( p_spudec );
-        return( 0 );
+        return 0;
     }
 
     return( p_spudec->thread_id );
@@ -107,7 +144,7 @@ static int InitThread( spudec_thread_t *p_spudec )
 {
     p_spudec->p_config->decoder_config.pf_init_bit_stream(
             &p_spudec->bit_stream,
-            p_spudec->p_config->decoder_config.p_decoder_fifo );
+            p_spudec->p_config->decoder_config.p_decoder_fifo, NULL, NULL );
 
     /* Mark thread as running and return */
     return( 0 );
@@ -121,7 +158,7 @@ static int InitThread( spudec_thread_t *p_spudec )
  *****************************************************************************/
 static void RunThread( spudec_thread_t *p_spudec )
 {
-    intf_WarnMsg( 1, "spudec: spu decoder thread %i spawned", getpid() );
+    intf_WarnMsg( 3, "spudec: spu decoder thread %i spawned", getpid() );
 
     /*
      * Initialize thread and free configuration
@@ -149,7 +186,7 @@ static void RunThread( spudec_thread_t *p_spudec )
     }
 
     /* End of thread */
-    intf_WarnMsg( 1, "spudec: destroying spu decoder thread %i", getpid() );
+    intf_WarnMsg( 3, "spudec: destroying spu decoder thread %i", getpid() );
     EndThread( p_spudec );
 }
 
@@ -213,14 +250,17 @@ static int SyncPacket( spudec_thread_t *p_spudec )
     p_spudec->i_spu_size = GetBits( &p_spudec->bit_stream, 16 );
 
     /* The RLE stuff size (remove 4 because we just read 32 bits) */
-    p_spudec->i_rle_size = GetBits( &p_spudec->bit_stream, 16 ) - 4;
+    p_spudec->i_rle_size = ShowBits( &p_spudec->bit_stream, 16 ) - 4;
 
     /* If the values we got are a bit strange, skip packet */
-    if( p_spudec->i_rle_size >= p_spudec->i_spu_size )
+    if( !p_spudec->i_spu_size
+         || ( p_spudec->i_rle_size >= p_spudec->i_spu_size ) )
     {
         return( 1 );
     }
 
+    RemoveBits( &p_spudec->bit_stream, 16 );
+
     return( 0 );
 }
 
@@ -233,11 +273,16 @@ static int SyncPacket( spudec_thread_t *p_spudec )
 static void ParsePacket( spudec_thread_t *p_spudec )
 {
     subpicture_t * p_spu;
-    u8           * p_source;
+    u8           * p_src;
+    unsigned int   i_offset;
+
+    intf_WarnMsg( 3, "spudec: trying to gather a 0x%.2x long subtitle",
+                  p_spudec->i_spu_size );
 
     /* We cannot display a subpicture with no date */
     if( DECODER_FIFO_START(*p_spudec->p_fifo)->i_pts == 0 )
     {
+        intf_WarnMsg( 3, "spudec error: subtitle without a date" );
         return;
     }
 
@@ -246,63 +291,76 @@ static void ParsePacket( spudec_thread_t *p_spudec )
                                    p_spudec->i_rle_size * 4 );
     /* Rationale for the "p_spudec->i_rle_size * 4": we are going to
      * expand the RLE stuff so that we won't need to read nibbles later
-     * on. This will speed things up a lot. Plus, we won't need to do
-     * this stupid interlacing stuff. */
+     * on. This will speed things up a lot. Plus, we'll only need to do
+     * this stupid interlacing stuff once. */
 
     if( p_spu == NULL )
     {
         return;
     }
 
-    /* Get display time now. If we do it later, we may miss a PTS. */
-    p_spu->begin_date = p_spu->end_date
-                    = DECODER_FIFO_START(*p_spudec->p_fifo)->i_pts;
+    /* Get display time now. If we do it later, we may miss the PTS. */
+    p_spudec->i_pts = DECODER_FIFO_START(*p_spudec->p_fifo)->i_pts;
 
     /* Allocate the temporary buffer we will parse */
-    p_source = malloc( p_spudec->i_rle_size );
+    p_src = malloc( p_spudec->i_rle_size );
 
-    if( p_source == NULL )
+    if( p_src == NULL )
     {
-        intf_ErrMsg( "spudec error: could not allocate p_source" );
+        intf_ErrMsg( "spudec error: could not allocate p_src" );
         vout_DestroySubPicture( p_spudec->p_vout, p_spu );
         return;
     }
 
     /* Get RLE data */
-    GetChunk( &p_spudec->bit_stream, p_source, p_spudec->i_rle_size );
+    for( i_offset = 0;
+         i_offset + SPU_CHUNK_SIZE < p_spudec->i_rle_size;
+         i_offset += SPU_CHUNK_SIZE )
+    {
+        GetChunk( &p_spudec->bit_stream, p_src + i_offset, SPU_CHUNK_SIZE );
+
+        if( p_spudec->p_fifo->b_die )
+        {
+            free( p_src );
+            vout_DestroySubPicture( p_spudec->p_vout, p_spu );
+            return;
+        }
+    }
+
+    GetChunk( &p_spudec->bit_stream, p_src + i_offset,
+              p_spudec->i_rle_size - i_offset );
 
 #if 0
     /* Dump the subtitle info */
-    intf_WarnHexDump( 0, p_spu->p_data, p_spudec->i_rle_size );
+    intf_WarnHexDump( 5, p_spu->p_data, p_spudec->i_rle_size );
 #endif
 
     /* Getting the control part */
-    if( ParseControlSequences( p_spudec, p_spu ) )
+    if( p_spudec->p_fifo->b_die || ParseControlSequences( p_spudec, p_spu ) )
     {
         /* There was a parse error, delete the subpicture */
-        free( p_source );
+        free( p_src );
         vout_DestroySubPicture( p_spudec->p_vout, p_spu );
         return;
     }
 
-    intf_WarnMsg( 1, "spudec: got a valid %ix%i subtitle at (%i,%i), "
-                     "RLE offsets: 0x%x 0x%x",
-                  p_spu->i_width, p_spu->i_height, p_spu->i_x, p_spu->i_y,
-                  p_spu->type.spu.i_offset[0], p_spu->type.spu.i_offset[1] );
-
-    if( ParseRLE( p_source, p_spu ) )
+    if( ParseRLE( p_spudec, p_spu, p_src ) )
     {
         /* There was a parse error, delete the subpicture */
-        free( p_source );
+        free( p_src );
         vout_DestroySubPicture( p_spudec->p_vout, p_spu );
         return;
     }
 
-    /* SPU is finished - we can tell the video output to display it */
+    intf_WarnMsg( 3, "spudec: total size: 0x%x, RLE offsets: 0x%x 0x%x",
+                  p_spudec->i_spu_size,
+                  p_spu->type.spu.i_offset[0], p_spu->type.spu.i_offset[1] );
+
+    /* SPU is finished - we can ask the video output to display it */
     vout_DisplaySubPicture( p_spudec->p_vout, p_spu );
 
     /* Clean up */
-    free( p_source );
+    free( p_src );
 }
 
 /*****************************************************************************
@@ -319,20 +377,27 @@ static int ParseControlSequences( spudec_thread_t *p_spudec,
     int i_index = p_spudec->i_rle_size + 4;
 
     /* The next start-of-control-sequence index and the previous one */
-    int i_next_index = 0, i_prev_index;
+    int i_next_seq, i_cur_seq;
 
     /* Command time and date */
     u8  i_command;
     int i_date;
 
+    /* XXX: temporary variables */
+    boolean_t b_force_display = 0;
+
+    /* Initialize the structure */
+    p_spu->i_start = p_spu->i_stop = 0;
+    p_spu->b_ephemer = 0;
+
     do
     {
         /* Get the control sequence date */
         i_date = GetBits( &p_spudec->bit_stream, 16 );
  
         /* Next offset */
-        i_prev_index = i_next_index;
-        i_next_index = GetBits( &p_spudec->bit_stream, 16 );
+        i_cur_seq = i_index;
+        i_next_seq = GetBits( &p_spudec->bit_stream, 16 );
  
         /* Skip what we just read */
         i_index += 4;
@@ -345,24 +410,25 @@ static int ParseControlSequences( spudec_thread_t *p_spudec,
             switch( i_command )
             {
                 case SPU_CMD_FORCE_DISPLAY:
+
                     /* 00 (force displaying) */
+                    p_spu->i_start = p_spudec->i_pts + ( i_date * 11000 );
+                    b_force_display = 1;
  
                     break;
  
-                /* FIXME: here we have to calculate dates. It's around
-                 * i_date * 12000 but I don't know how much exactly. */
+                /* Convert the dates in seconds to PTS values */
                 case SPU_CMD_START_DISPLAY:
  
                     /* 01 (start displaying) */
-                    p_spu->begin_date += ( i_date * 12000 );
+                    p_spu->i_start = p_spudec->i_pts + ( i_date * 11000 );
  
                     break;
  
                 case SPU_CMD_STOP_DISPLAY:
  
                     /* 02 (stop displaying) */
-                    p_spu->end_date += ( i_date * 12000 );
+                    p_spu->i_stop = p_spudec->i_pts + ( i_date * 11000 );
  
                     break;
  
@@ -413,12 +479,11 @@ static int ParseControlSequences( spudec_thread_t *p_spudec,
                 case SPU_CMD_END:
  
                     /* ff (end) */
                     break;
  
                 default:
  
-                    /* ?? (unknown command) */
+                    /* xx (unknown command) */
                     intf_ErrMsg( "spudec error: unknown command 0x%.2x",
                                  i_command );
                     return( 1 );
@@ -426,13 +491,13 @@ static int ParseControlSequences( spudec_thread_t *p_spudec,
 
         } while( i_command != SPU_CMD_END );
 
-    } while( i_index == i_next_index );
+    } while( i_index == i_next_seq );
 
-    /* Check that the last index matches the previous one */
-    if( i_next_index != i_prev_index )
+    /* Check that the next sequence index matches the current one */
+    if( i_next_seq != i_cur_seq )
     {
         intf_ErrMsg( "spudec error: index mismatch (0x%.4x != 0x%.4x)",
-                     i_next_index, i_prev_index );
+                     i_next_seq, i_cur_seq );
         return( 1 );
     }
 
@@ -443,25 +508,33 @@ static int ParseControlSequences( spudec_thread_t *p_spudec,
         return( 1 );
     }
 
+    if( !p_spu->i_start )
+    {
+        intf_ErrMsg( "spudec error: no `start display' command" );
+    }
+
+    if( !p_spu->i_stop )
+    {
+        /* This subtitle will live for 5 seconds or until the next subtitle */
+        p_spu->i_stop = p_spu->i_start + 500 * 11000;
+        p_spu->b_ephemer = 1;
+    }
+
     /* Get rid of padding bytes */
     switch( p_spudec->i_spu_size - i_index )
     {
+        /* Zero or one padding byte, quite usual */
         case 1:
-
             RemoveBits( &p_spudec->bit_stream, 8 );
             i_index++;
-
         case 0:
-
-            /* Zero or one padding byte, quite usual */
-
             break;
 
+        /* More than one padding byte - this is very strange, but
+         * we can deal with it */
         default:
-
-            /* More than one padding byte - this is very strange, but
-             * we can deal with it */
-            intf_WarnMsg( 2, "spudec warning: %i padding bytes",
+            intf_WarnMsg( 2, "spudec warning: %i padding bytes, we usually "
+                             "get 1 or none",
                           p_spudec->i_spu_size - i_index );
 
             while( i_index < p_spudec->i_spu_size )
@@ -473,6 +546,13 @@ static int ParseControlSequences( spudec_thread_t *p_spudec,
             break;
     }
 
+    if( b_force_display )
+    {
+        intf_ErrMsg( "spudec: \"force display\" command" );
+        intf_ErrMsg( "spudec: send mail to <sam@zoy.org> if you "
+                     "want to help debugging this" );
+    }
+
     /* Successfully parsed ! */
     return( 0 );
 }
@@ -484,118 +564,153 @@ static int ParseControlSequences( spudec_thread_t *p_spudec,
  * convenient structure for later decoding. For more information on the
  * subtitles format, see http://sam.zoy.org/doc/dvd/subtitles/index.html
  *****************************************************************************/
-static int ParseRLE( u8 *p_source, subpicture_t * p_spu )
+static int ParseRLE( spudec_thread_t *p_spudec,
+                     subpicture_t * p_spu, u8 * p_src )
 {
-    int i_code;
-    int i_id = 0;
+    unsigned int i_code;
 
-    int i_width = p_spu->i_width;
-    int i_height = p_spu->i_height;
-    int i_x = 0, i_y = 0;
+    unsigned int i_width = p_spu->i_width;
+    unsigned int i_height = p_spu->i_height;
+    unsigned int i_x, i_y;
 
     u16 *p_dest = (u16 *)p_spu->p_data;
-    int pi_index[2];
 
-    pi_index[0] = p_spu->type.spu.i_offset[0] << 1;
-    pi_index[1] = p_spu->type.spu.i_offset[1] << 1;
+    /* The subtitles are interlaced, we need two offsets */
+    unsigned int  i_id = 0;                   /* Start on the even SPU layer */
+    unsigned int  pi_table[ 2 ];
+    unsigned int *pi_offset;
+
+    boolean_t b_empty_top = 1,
+              b_empty_bottom = 0;
+    unsigned int i_skipped_top = 0,
+                 i_skipped_bottom = 0;
 
-    while( i_y < i_height )
+    pi_table[ 0 ] = p_spu->type.spu.i_offset[ 0 ] << 1;
+    pi_table[ 1 ] = p_spu->type.spu.i_offset[ 1 ] << 1;
+
+    for( i_y = 0 ; i_y < i_height ; i_y++ )
     {
-        i_code = GetNibble( p_source, pi_index + i_id );
+        pi_offset = pi_table + i_id;
 
-        if( i_code >= 0x04 )
+        for( i_x = 0 ; i_x < i_width ; i_x += i_code >> 2 )
         {
-            found_code:
+            i_code = AddNibble( 0, p_src, pi_offset );
 
-            if( ((i_code >> 2) + i_x + i_y * i_width) > i_height * i_width )
+            if( i_code < 0x04 )
             {
-                intf_ErrMsg( "spudec error: out of bounds, %i at (%i,%i) is "
-                             "out of %ix%i",
-                             i_code >> 2, i_x, i_y, i_width, i_height);
-                return( 1 );
-            }
-            else
-            {
-                /* Store the code */
-                *p_dest++ = i_code;
+                i_code = AddNibble( i_code, p_src, pi_offset );
 
-                i_x += i_code >> 2;
+                if( i_code < 0x10 )
+                {
+                    i_code = AddNibble( i_code, p_src, pi_offset );
+
+                    if( i_code < 0x040 )
+                    {
+                        i_code = AddNibble( i_code, p_src, pi_offset );
+
+                        if( i_code < 0x0100 )
+                        {
+                            /* If the 14 first bits are set to 0, then it's a
+                             * new line. We emulate it. */
+                            if( i_code < 0x0004 )
+                            {
+                                i_code |= ( i_width - i_x ) << 2;
+                            }
+                            else
+                            {
+                                /* We have a boo boo ! */
+                                intf_ErrMsg( "spudec error: unknown RLE code "
+                                             "0x%.4x", i_code );
+                                return( 1 );
+                            }
+                        }
+                    }
+                }
             }
 
-            if( i_x > i_width )
+            if( ( (i_code >> 2) + i_x + i_y * i_width ) > i_height * i_width )
             {
-                intf_ErrMsg( "spudec error: i_x overflowed, %i > %i",
-                             i_x, i_width );
+                intf_ErrMsg( "spudec error: out of bounds, %i at (%i,%i) is "
+                             "out of %ix%i",
+                             i_code >> 2, i_x, i_y, i_width, i_height);
                 return( 1 );
             }
 
-            if( i_x == i_width )
+            if( i_code == (i_width << 2) ) /* FIXME: we assume 0 is transp */
             {
-                /* byte-align the stream */
-                if( pi_index[i_id] & 0x1 )
+                if( b_empty_top )
                 {
-                    pi_index[i_id]++;
+                    /* This is a blank top line, we skip it */
+                    i_skipped_top++;
                 }
-
-                i_id = ~i_id & 0x1;
-
-                i_y++;
-                i_x = 0;
-
-                if( i_y > i_height )
+                else
                 {
-                    intf_ErrMsg( "spudec error: i_y overflowed at EOL, "
-                                 "%i > %i", i_y, i_height );
-                    return( 1 );
+                    /* We can't be sure the current lines will be skipped,
+                     * so we store the code just in case. */
+                    *p_dest++ = i_code;
+
+                    b_empty_bottom = 1;
+                    i_skipped_bottom++;
                 }
             }
+            else
+            {
+                /* We got a valid code, store it */
+                *p_dest++ = i_code;
 
-            continue;
+                /* Valid code means no blank line */
+                b_empty_top = 0;
+                b_empty_bottom = 0;
+                i_skipped_bottom = 0;
+            }
         }
 
-        i_code = ( i_code << 4 ) + GetNibble( p_source, pi_index + i_id );
-
-        /* 00 11 xx cc */
-        if( i_code >= 0x10 )
+        /* Check that we didn't go too far */
+        if( i_x > i_width )
         {
-            /* 00 01 xx cc */
-            goto found_code;
+            intf_ErrMsg( "spudec error: i_x overflowed, %i > %i",
+                         i_x, i_width );
+            return( 1 );
         }
 
-        i_code = ( i_code << 4 ) + GetNibble( p_source, pi_index + i_id );
-
-        /* 00 00 11 xx xx cc */
-        if( i_code >= 0x040 )
+        /* Byte-align the stream */
+        if( *pi_offset & 0x1 )
         {
-            goto found_code;   /* 00 00 01 xx xx cc */
+            (*pi_offset)++;
         }
 
-        i_code = ( i_code << 4 ) + GetNibble( p_source, pi_index + i_id );
+        /* Swap fields */
+        i_id = ~i_id & 0x1;
+    }
 
-        if( i_code >= 0x0100 ) /* 00 00 00 11 xx xx xx cc */
-        {
-            goto found_code;   /* 00 00 00 01 xx xx xx cc */
-        }
+    /* We shouldn't get any padding bytes */
+    if( i_y < i_height )
+    {
+        intf_ErrMsg( "spudec: padding bytes found in RLE sequence" );
+        intf_ErrMsg( "spudec: send mail to <sam@zoy.org> if you "
+                     "want to help debugging this" );
 
-        if( i_code & ~0x0003 )
-        {
-            /* We have a boo boo ! */
-            intf_ErrMsg( "spudec error: unknown code 0x%.4x", i_code );
-            return( 1 );
-        }
-        else
-        {
-            /* If the 14 first bits are 0, then it's a new line */
-            i_code |= ( i_width - i_x ) << 2;
-            goto found_code;
-        }
+        /* Skip them just in case */
+        while( i_y < i_height )
+       {
+            *p_dest++ = i_width << 2;
+            i_y++;
+       }
+
+        return( 1 );
     }
 
-    /* FIXME: we shouldn't need these padding bytes */
-    while( i_y < i_height )
+    intf_WarnMsg( 3, "spudec: valid subtitle, size: %ix%i, position: %i,%i",
+                  p_spu->i_width, p_spu->i_height, p_spu->i_x, p_spu->i_y );
+
+    /* Crop if necessary */
+    if( i_skipped_top || i_skipped_bottom )
     {
-        *p_dest++ = i_width << 2;
-        i_y++;
+        p_spu->i_y += i_skipped_top;
+        p_spu->i_height -= i_skipped_top + i_skipped_bottom;
+
+        intf_WarnMsg( 3, "spudec: cropped to: %ix%i, position: %i,%i",
+                      p_spu->i_width, p_spu->i_height, p_spu->i_x, p_spu->i_y );
     }
 
     return( 0 );