]> 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 95dee601dc2edaeb28c745ee6434e42576ef213c..1d02bb0dd2f44ad6487730f24a0a06a1ff3d28c9 100644 (file)
@@ -2,7 +2,7 @@
  * spu_decoder.c : spu decoder thread
  *****************************************************************************
  * Copyright (C) 2000 VideoLAN
- * $Id: spu_decoder.c,v 1.43 2001/05/08 20:38:25 sam Exp $
+ * $Id: spu_decoder.c,v 1.47 2001/06/02 01:09:03 sam Exp $
  *
  * Authors: Samuel Hocevar <sam@zoy.org>
  *
  *****************************************************************************/
 #include "defs.h"
 
+#ifdef HAVE_UNISTD_H
 #include <unistd.h>                                              /* getpid() */
+#endif
+
 #ifdef WIN32                   /* getpid() for win32 is located in process.h */
 #include <process.h>
 #endif
@@ -60,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
@@ -250,7 +253,8 @@ static int SyncPacket( spudec_thread_t *p_spudec )
     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 );
     }
@@ -270,6 +274,7 @@ static void ParsePacket( spudec_thread_t *p_spudec )
 {
     subpicture_t * p_spu;
     u8           * p_src;
+    unsigned int   i_offset;
 
     intf_WarnMsg( 3, "spudec: trying to gather a 0x%.2x long subtitle",
                   p_spudec->i_spu_size );
@@ -308,7 +313,22 @@ static void ParsePacket( spudec_thread_t *p_spudec )
     }
 
     /* Get RLE data */
-    GetChunk( &p_spudec->bit_stream, p_src, 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 */
@@ -316,7 +336,7 @@ static void ParsePacket( spudec_thread_t *p_spudec )
 #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_src );
@@ -324,7 +344,7 @@ static void ParsePacket( spudec_thread_t *p_spudec )
         return;
     }
 
-    if( ParseRLE( p_src, p_spu ) )
+    if( ParseRLE( p_spudec, p_spu, p_src ) )
     {
         /* There was a parse error, delete the subpicture */
         free( p_src );
@@ -332,9 +352,6 @@ static void ParsePacket( spudec_thread_t *p_spudec )
         return;
     }
 
-    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 );
-                     
     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] );
@@ -366,6 +383,9 @@ static int ParseControlSequences( spudec_thread_t *p_spudec,
     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;
@@ -392,9 +412,8 @@ static int ParseControlSequences( spudec_thread_t *p_spudec,
                 case SPU_CMD_FORCE_DISPLAY:
 
                     /* 00 (force displaying) */
-                    intf_ErrMsg( "spudec: \"force display\" command" );
-                    intf_ErrMsg( "spudec: send mail to <sam@zoy.org> if you "
-                                         "want to help debugging this" );
+                    p_spu->i_start = p_spudec->i_pts + ( i_date * 11000 );
+                    b_force_display = 1;
  
                     break;
  
@@ -527,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 );
 }
@@ -538,7 +564,8 @@ 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_src, subpicture_t * p_spu )
+static int ParseRLE( spudec_thread_t *p_spudec,
+                     subpicture_t * p_spu, u8 * p_src )
 {
     unsigned int i_code;
 
@@ -553,6 +580,11 @@ static int ParseRLE( u8 *p_src, subpicture_t * p_spu )
     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;
+
     pi_table[ 0 ] = p_spu->type.spu.i_offset[ 0 ] << 1;
     pi_table[ 1 ] = p_spu->type.spu.i_offset[ 1 ] << 1;
 
@@ -604,8 +636,33 @@ static int ParseRLE( u8 *p_src, subpicture_t * p_spu )
                 return( 1 );
             }
 
-            /* We got a valid code, store it */
-            *p_dest++ = i_code;
+            if( i_code == (i_width << 2) ) /* FIXME: we assume 0 is transp */
+            {
+                if( b_empty_top )
+                {
+                    /* This is a blank top line, we skip it */
+                    i_skipped_top++;
+                }
+                else
+                {
+                    /* 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;
+
+                /* Valid code means no blank line */
+                b_empty_top = 0;
+                b_empty_bottom = 0;
+                i_skipped_bottom = 0;
+            }
         }
 
         /* Check that we didn't go too far */
@@ -626,11 +683,34 @@ static int ParseRLE( u8 *p_src, subpicture_t * p_spu )
         i_id = ~i_id & 0x1;
     }
 
-    /* FIXME: we shouldn't need these padding bytes */
-    while( i_y < i_height )
+    /* 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" );
+
+        /* Skip them just in case */
+        while( i_y < i_height )
+       {
+            *p_dest++ = i_width << 2;
+            i_y++;
+       }
+
+        return( 1 );
+    }
+
+    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 );