]> git.sesse.net Git - vlc/blobdiff - modules/demux/util/sub.c
* modules/demux/util/*: a bit of cleanup.
[vlc] / modules / demux / util / sub.c
index a044e90df314de11dfcde67b595215aa249c3c1b..bf4958d6cfe094a654436f6894b28d1a54168151 100644 (file)
@@ -2,7 +2,7 @@
  * sub.c
  *****************************************************************************
  * Copyright (C) 1999-2003 VideoLAN
- * $Id: sub.c,v 1.34 2003/11/05 00:17:50 hartman Exp $
+ * $Id: sub.c,v 1.42 2004/01/26 20:02:15 gbazin Exp $
  *
  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
  *
@@ -53,9 +53,8 @@ static void sub_close( subtitle_demux_t *p_sub );
 
 static void sub_fix( subtitle_demux_t *p_sub );
 
-static char *ppsz_sub_type[] = { "auto", "microdvd", "subrip", "ssa1", "ssa2-4",
-                                 "vplayer", "sami", "vobsub", NULL };
-
+static char *ppsz_sub_type[] = { "auto", "microdvd", "subrip", "ssa1",
+  "ssa2-4", "vplayer", "sami", "vobsub" };
 
 /*****************************************************************************
  * Module descriptor
@@ -72,16 +71,15 @@ static char *ppsz_sub_type[] = { "auto", "microdvd", "subrip", "ssa1", "ssa2-4",
 vlc_module_begin();
     set_description( _("Text subtitles demux") );
     set_capability( "subtitle demux", 12 );
-    add_category_hint( "Subtitles", NULL, VLC_TRUE );
-        add_float( "sub-fps", 0.0, NULL,
-                   "Frames per second",
-                   SUB_FPS_LONGTEXT, VLC_TRUE );
-        add_integer( "sub-delay", 0, NULL,
-                     "Delay subtitles (in 1/10s)",
-                     SUB_DELAY_LONGTEXT, VLC_TRUE );
-        add_string_from_list( "sub-type", "auto", ppsz_sub_type, NULL,
-                              "subtitles type",
-                              SUB_TYPE_LONGTEXT, VLC_TRUE );
+    add_float( "sub-fps", 25.0, NULL,
+               N_("Frames per second"),
+               SUB_FPS_LONGTEXT, VLC_TRUE );
+    add_integer( "sub-delay", 0, NULL,
+                 N_("Delay subtitles (in 1/10s)"),
+                 SUB_DELAY_LONGTEXT, VLC_TRUE );
+    add_string( "sub-type", "auto", NULL, "Subtitles fileformat",
+                SUB_TYPE_LONGTEXT, VLC_TRUE );
+        change_string_list( ppsz_sub_type, 0, 0 );
     set_callbacks( Open, NULL );
 vlc_module_end();
 
@@ -238,18 +236,18 @@ static char * local_stristr( char *psz_big, char *psz_little)
 
     while (*p_pos)
     {
-       if (toupper(*p_pos) == toupper(*psz_little))
+        if (toupper(*p_pos) == toupper(*psz_little))
         {
-           char * psz_cur1 = p_pos + 1;
-           char * psz_cur2 = psz_little + 1;
-           while (*psz_cur1 && *psz_cur2 && toupper(*psz_cur1) == toupper(*psz_cur2))
+            char * psz_cur1 = p_pos + 1;
+            char * psz_cur2 = psz_little + 1;
+            while (*psz_cur1 && *psz_cur2 && toupper(*psz_cur1) == toupper(*psz_cur2))
             {
-               psz_cur1++;
-               psz_cur2++;
-           }
-           if (!*psz_cur2) return p_pos;
-       }
-       p_pos++;
+                psz_cur1++;
+                psz_cur2++;
+            }
+            if (!*psz_cur2) return p_pos;
+        }
+        p_pos++;
     }
     return NULL;
 }
@@ -257,19 +255,16 @@ static char * local_stristr( char *psz_big, char *psz_little)
 /*****************************************************************************
  * sub_open: Open a subtitle file and add subtitle ES
  *****************************************************************************/
-static int  sub_open ( subtitle_demux_t *p_sub,
-                       input_thread_t  *p_input,
-                       char     *psz_name,
-                       mtime_t i_microsecperframe,
-                       int i_track_id )
+static int sub_open( subtitle_demux_t *p_sub, input_thread_t  *p_input,
+                     char *psz_name, mtime_t i_microsecperframe,
+                     int i_track_id )
 {
     text_t  txt;
     vlc_value_t val;
-
-    int     i;
-    int     i_sub_type;
-    int     i_max;
-    int (*pf_read_subtitle)( subtitle_demux_t *, text_t *, subtitle_t *, mtime_t ) = NULL;
+    es_format_t fmt;
+    int i, i_sub_type, i_max;
+    int (*pf_read_subtitle)( subtitle_demux_t *, text_t *, subtitle_t *,
+                             mtime_t ) = NULL;
 
     p_sub->i_sub_type = SUB_TYPE_UNKNOWN;
     p_sub->p_es = NULL;
@@ -280,7 +275,6 @@ static int  sub_open ( subtitle_demux_t *p_sub,
     if( !psz_name || !*psz_name )
     {
         msg_Err( p_sub, "no subtitle file specified" );
-        if( psz_name ) free( psz_name );
         return VLC_EGENERIC;
     }
 
@@ -288,19 +282,17 @@ static int  sub_open ( subtitle_demux_t *p_sub,
     if( text_load( &txt, psz_name ) )
     {
         msg_Err( p_sub, "cannot open `%s' subtitle file", psz_name );
-        free( psz_name );
         return VLC_EGENERIC;
     }
+
     msg_Dbg( p_sub, "opened `%s'", psz_name );
-    free( psz_name );
 
     var_Get( p_sub, "sub-fps", &val );
     if( val.i_int >= 1.0 )
     {
-        var_Get( p_sub, "sub-fps", &val );
         i_microsecperframe = (mtime_t)( (float)1000000 / val.f_float );
     }
-    else if( i_microsecperframe <= 0 )
+    else if( val.f_float <= 0 )
     {
         i_microsecperframe = 40000; /* default: 25fps */
     }
@@ -470,37 +462,39 @@ static int  sub_open ( subtitle_demux_t *p_sub,
     }
 
     /* *** add subtitle ES *** */
-    vlc_mutex_lock( &p_input->stream.stream_lock );
-    p_sub->p_es = input_AddES( p_input, p_input->stream.p_selected_program,
-                               0xff - i_track_id,    /* FIXME */
-                               SPU_ES, NULL, 0 );
-    vlc_mutex_unlock( &p_input->stream.stream_lock );
-
-    p_sub->p_es->i_stream_id = 0xff - i_track_id;    /* FIXME */
-    
-    if( p_sub->psz_header != NULL )
-    {
-        p_sub->p_es->p_demux_data = malloc( sizeof( subtitle_data_t ) );
-        p_sub->p_es->p_demux_data->psz_header = strdup( p_sub->psz_header );
-        free( p_sub->psz_header );
-    }
-
     if( p_sub->i_sub_type == SUB_TYPE_VOBSUB )
     {
-        p_sub->p_es->i_fourcc    = VLC_FOURCC( 's','p','u',' ' );
-        /* open vobsub file */
+        int i_len = strlen( psz_name );
+        char *psz_vobname = strdup(psz_name);
+
+        strcpy( psz_vobname + i_len - 4, ".sub" );
+
+        /* open file */
+        if( !( p_sub->p_vobsub_file = fopen( psz_vobname, "rb" ) ) )
+        {
+            msg_Err( p_sub, "couldn't open .sub Vobsub file: %s", psz_vobname );
+        }
+        free( psz_vobname );
+
+        es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','p','u',' ' ) );
     }
     else if( p_sub->i_sub_type == SUB_TYPE_SSA1 ||
              p_sub->i_sub_type == SUB_TYPE_SSA2_4 )
     {
-        p_sub->p_es->i_fourcc    = VLC_FOURCC( 's','s','a',' ' );
+        es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','s','a',' ' ) );
     }
     else
     {
-        p_sub->p_es->i_fourcc    = VLC_FOURCC( 's','u','b','t' );
+        es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','u','b','t' ) );
     }
-
+    if( p_sub->psz_header != NULL )
+    {
+        fmt.i_extra = strlen( p_sub->psz_header ) + 1;
+        fmt.p_extra = strdup( p_sub->psz_header );
+    }
+    p_sub->p_es = es_out_Add( p_input->p_es_out, &fmt );
     p_sub->i_previously_selected = 0;
+
     return VLC_SUCCESS;
 }
 
@@ -509,13 +503,17 @@ static int  sub_open ( subtitle_demux_t *p_sub,
  *****************************************************************************/
 static int  sub_demux( subtitle_demux_t *p_sub, mtime_t i_maxdate )
 {
-    if( p_sub->p_es->p_decoder_fifo && !p_sub->i_previously_selected )
+    input_thread_t *p_input = p_sub->p_input;
+    vlc_bool_t     b;
+
+    es_out_Control( p_input->p_es_out, ES_OUT_GET_ES_STATE, p_sub->p_es, &b );
+    if( b && !p_sub->i_previously_selected )
     {
         p_sub->i_previously_selected = 1;
         p_sub->pf_seek( p_sub, i_maxdate );
         return VLC_SUCCESS;
     }
-    else if( !p_sub->p_es->p_decoder_fifo && p_sub->i_previously_selected )
+    else if( !b && p_sub->i_previously_selected )
     {
         p_sub->i_previously_selected = 0;
         return VLC_SUCCESS;
@@ -524,9 +522,7 @@ static int  sub_demux( subtitle_demux_t *p_sub, mtime_t i_maxdate )
     while( p_sub->i_subtitle < p_sub->i_subtitles &&
            p_sub->subtitle[p_sub->i_subtitle].i_start < i_maxdate )
     {
-        pes_packet_t    *p_pes;
-        data_packet_t   *p_data;
-
+        block_t *p_block;
         int i_len;
 
         i_len = strlen( p_sub->subtitle[p_sub->i_subtitle].psz_text ) + 1;
@@ -537,22 +533,14 @@ static int  sub_demux( subtitle_demux_t *p_sub, mtime_t i_maxdate )
             p_sub->i_subtitle++;
             continue;
         }
-        if( !( p_pes = input_NewPES( p_sub->p_input->p_method_data ) ) )
-        {
-            p_sub->i_subtitle++;
-            continue;
-        }
 
-        if( !( p_data = input_NewPacket( p_sub->p_input->p_method_data,
-                                         i_len ) ) )
+        if( !( p_block = block_New( p_sub->p_input, i_len ) ) )
         {
-            input_DeletePES( p_sub->p_input->p_method_data, p_pes );
             p_sub->i_subtitle++;
             continue;
         }
-        p_data->p_payload_end = p_data->p_payload_start + i_len;
 
-        p_pes->i_pts =
+        p_block->i_pts =
             input_ClockGetTS( p_sub->p_input,
                               p_sub->p_input->stream.p_selected_program,
                               p_sub->subtitle[p_sub->i_subtitle].i_start*9/100);
@@ -561,37 +549,31 @@ static int  sub_demux( subtitle_demux_t *p_sub, mtime_t i_maxdate )
             /* FIXME kludge ...
              * i_dts means end of display...
              */
-            p_pes->i_dts =
+            p_block->i_dts =
                 input_ClockGetTS( p_sub->p_input,
                               p_sub->p_input->stream.p_selected_program,
                               p_sub->subtitle[p_sub->i_subtitle].i_stop *9/100);
         }
         else
         {
-            p_pes->i_dts = 0;
+            p_block->i_dts = 0;
         }
 
-        p_pes->i_nb_data = 1;
-        p_pes->p_first =
-            p_pes->p_last = p_data;
-        p_pes->i_pes_size = i_len;
+        memcpy( p_block->p_buffer,
+                p_sub->subtitle[p_sub->i_subtitle].psz_text, i_len );
 
-        memcpy( p_data->p_payload_start,
-                p_sub->subtitle[p_sub->i_subtitle].psz_text,
-                i_len );
-        if( p_sub->p_es->p_decoder_fifo && p_pes->i_pts > 0 )
+        if( p_block->i_pts > 0 )
         {
-
-            input_DecodePES( p_sub->p_es->p_decoder_fifo, p_pes );
+            es_out_Send( p_input->p_es_out, p_sub->p_es, p_block );
         }
         else
         {
-            input_DeletePES( p_sub->p_input->p_method_data, p_pes );
+            block_Release( p_block );
         }
 
         p_sub->i_subtitle++;
     }
-    return( 0 );
+    return VLC_SUCCESS;
 }
 
 /*****************************************************************************
@@ -606,7 +588,6 @@ static int  sub_seek ( subtitle_demux_t *p_sub, mtime_t i_date )
     {
         p_sub->i_subtitle++;
     }
-
     return( 0 );
 }
 
@@ -627,6 +608,10 @@ static void sub_close( subtitle_demux_t *p_sub )
         }
         free( p_sub->subtitle );
     }
+    if( p_sub->p_vobsub_file )
+    {
+        fclose( p_sub->p_vobsub_file );
+    }
 }
 
 /*****************************************************************************
@@ -700,8 +685,8 @@ static int  sub_MicroDvdRead( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *
     char *s;
 
     char buffer_text[MAX_LINE + 1];
-    uint32_t    i_start;
-    uint32_t    i_stop;
+    unsigned int    i_start;
+    unsigned int    i_stop;
     unsigned int i;
 
     for( ;; )
@@ -1070,7 +1055,7 @@ static int  sub_VobSub( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subt
     char *p;
 
     char buffer_text[MAX_LINE + 1];
-    uint32_t    i_start, i_location;
+    unsigned int    i_start, i_location;
 
     for( ;; )
     {
@@ -1100,5 +1085,214 @@ static int  sub_VobSub( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subt
     fprintf( stderr, "time: %x, location: %x\n", i_start, i_location );
     return( 0 );
 }
+/*
+
+#define mpeg_read(buf, num) _mpeg_read(srcbuf, size, srcpos, buf, num)
+static unsigned int _mpeg_read(unsigned char *srcbufunsigned int, unsigned int size,
+                           unsigned int &srcpos, unsigned char *dstbuf,
+                           unsigned int num) {
+  unsigned int real_num;
+
+  if ((srcpos + num) >= size)
+    real_num = size - srcpos;
+  else
+    real_num = num;
+  memcpy(dstbuf, &srcbuf[srcpos], real_num);
+  srcpos += real_num;
+
+  return real_num;
+}
+
+#define mpeg_getc() _mpeg_getch(srcbuf, size, srcpos)
+static int _mpeg_getch(unsigned char *srcbuf, unsigned int size,
+                       unsigned int &srcpos) {
+  unsigned char c;
+
+  if (mpeg_read(&c, 1) != 1)
+    return -1;
+  return (int)c;
+}
 
+#define mpeg_seek(b, w) _mpeg_seek(size, srcpos, b, w)
+static int _mpeg_seek(unsigned int size, unsigned int &srcpos, unsigned int num,
+                      int whence) {
+  unsigned int new_pos;
+
+  if (whence == SEEK_SET)
+    new_pos = num;
+  else if (whence == SEEK_CUR)
+    new_pos = srcpos + num;
+  else
+    abort();
+
+  if (new_pos >= size) {
+    srcpos = size;
+    return 1;
+  }
+
+  srcpos = new_pos;
+  return 0;
+}
+
+#define mpeg_tell() srcpos
+*/
+/*
+static int mpeg_run(demuxer_t *demuxer, unsigned char *srcbuf, unsigned int size) {
+  unsigned int len, idx, version, srcpos, packet_size;
+  int c, aid;
+  float pts;*/
+  /* Goto start of a packet, it starts with 0x000001?? */
+  /*const unsigned char wanted[] = { 0, 0, 1 };
+  unsigned char buf[5];
+  demux_packet_t *dp;
+  demux_stream_t *ds;
+  mkv_demuxer_t *mkv_d;
+
+  mkv_d = (mkv_demuxer_t *)demuxer->priv;
+  ds = demuxer->sub;
+
+  srcpos = 0;
+  packet_size = 0;
+  while (1) {
+    if (mpeg_read(buf, 4) != 4)
+      return -1;
+    while (memcmp(buf, wanted, sizeof(wanted)) != 0) {
+      c = mpeg_getc();
+      if (c < 0)
+        return -1;
+      memmove(buf, buf + 1, 3);
+      buf[3] = c;
+    }
+    switch (buf[3]) {
+      case 0xb9:       */              /* System End Code */
+   /*     return 0;
+        break;
+
+      case 0xba:                    */  /* Packet start code */
+ /*       c = mpeg_getc();
+        if (c < 0)
+          return -1;
+        if ((c & 0xc0) == 0x40)
+          version = 4;
+        else if ((c & 0xf0) == 0x20)
+          version = 2;
+        else {
+          mp_msg(MSGT_DEMUX, MSGL_ERR, "[mkv] VobSub: Unsupported MPEG "
+                 "version: 0x%02x\n", c);
+          return -1;
+        }
+
+        if (version == 4) {
+          if (mpeg_seek(9, SEEK_CUR))
+            return -1;
+        } else if (version == 2) {
+          if (mpeg_seek(7, SEEK_CUR))
+            return -1;
+        } else
+          abort();
+        break;
+
+      case 0xbd:       */              /* packet */
+/*        if (mpeg_read(buf, 2) != 2)
+          return -1;
+        len = buf[0] << 8 | buf[1];
+        idx = mpeg_tell();
+        c = mpeg_getc();
+        if (c < 0)
+          return -1;
+        if ((c & 0xC0) == 0x40) {*/ /* skip STD scale & size */
+/*          if (mpeg_getc() < 0)
+            return -1;
+          c = mpeg_getc();
+          if (c < 0)
+            return -1;
+        }
+        if ((c & 0xf0) == 0x20) { */ /* System-1 stream timestamp */
+          /* Do we need this? */
+/*          abort();
+        } else if ((c & 0xf0) == 0x30) {
+  */        /* Do we need this? */
+/*          abort();
+        } else if ((c & 0xc0) == 0x80) {*/ /* System-2 (.VOB) stream */
+/*          unsigned int pts_flags, hdrlen, dataidx;
+          c = mpeg_getc();
+          if (c < 0)
+            return -1;
+          pts_flags = c;
+          c = mpeg_getc();
+          if (c < 0)
+            return -1;
+          hdrlen = c;
+          dataidx = mpeg_tell() + hdrlen;
+          if (dataidx > idx + len) {
+            mp_msg(MSGT_DEMUX, MSGL_ERR, "[mkv] VobSub: Invalid header "
+                   "length: %d (total length: %d, idx: %d, dataidx: %d)\n",
+                   hdrlen, len, idx, dataidx);
+            return -1;
+          }
+          if ((pts_flags & 0xc0) == 0x80) {
+            if (mpeg_read(buf, 5) != 5)
+              return -1;
+            if (!(((buf[0] & 0xf0) == 0x20) && (buf[0] & 1) && (buf[2] & 1) &&
+                  (buf[4] & 1))) {
+              mp_msg(MSGT_DEMUX, MSGL_ERR, "[mkv] VobSub PTS error: 0x%02x "
+                     "%02x%02x %02x%02x \n",
+                     buf[0], buf[1], buf[2], buf[3], buf[4]);
+              pts = 0;
+            } else
+              pts = ((buf[0] & 0x0e) << 29 | buf[1] << 22 |
+                     (buf[2] & 0xfe) << 14 | buf[3] << 7 | (buf[4] >> 1));
+          } else*/ /* if ((pts_flags & 0xc0) == 0xc0) */// {
+            /* what's this? */
+            /* abort(); */
+    /*      }
+          mpeg_seek(dataidx, SEEK_SET);
+          aid = mpeg_getc();
+          if (aid < 0) {
+            mp_msg(MSGT_DEMUX, MSGL_ERR, "[mkv] VobSub: Bogus aid %d\n", aid);
+            return -1;
+          }
+          packet_size = len - ((unsigned int)mpeg_tell() - idx);
+          
+          dp = new_demux_packet(packet_size);
+          dp->flags = 1;
+          dp->pts = mkv_d->last_pts;
+          if (mpeg_read(dp->buffer, packet_size) != packet_size) {
+            mp_msg(MSGT_DEMUX, MSGL_ERR, "[mkv] VobSub: mpeg_read failure");
+            packet_size = 0;
+            return -1;
+          }
+          ds_add_packet(ds, dp);
+          idx = len;
+        }
+        break;
+
+      case 0xbe:               */      /* Padding */
+ /*       if (mpeg_read(buf, 2) != 2)
+          return -1;
+        len = buf[0] << 8 | buf[1];
+        if ((len > 0) && mpeg_seek(len, SEEK_CUR))
+          return -1;
+        break;
+
+      default:
+        if ((0xc0 <= buf[3]) && (buf[3] < 0xf0)) {
+  */        /* MPEG audio or video */
+  /*        if (mpeg_read(buf, 2) != 2)
+            return -1;
+          len = (buf[0] << 8) | buf[1];
+          if ((len > 0) && mpeg_seek(len, SEEK_CUR))
+            return -1;
+
+        }
+        else {
+          mp_msg(MSGT_DEMUX, MSGL_ERR, "[mkv] VobSub: unknown header "
+                 "0x%02X%02X%02X%02X\n", buf[0], buf[1], buf[2], buf[3]);
+          return -1;
+        }
+    }
+  }
+  return 0;
+}
 
+*/