vobsub.c
上传用户:kjfoods
上传日期:2020-07-06
资源大小:29949k
文件大小:23k
源码类别:

midi

开发平台:

Unix_Linux

  1. /*****************************************************************************
  2.  * subtitle.c: Demux vobsub files.
  3.  *****************************************************************************
  4.  * Copyright (C) 1999-2004 the VideoLAN team
  5.  * $Id: bb0a47e7c74db913125eb1c8e4115234dcc0e696 $
  6.  *
  7.  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
  8.  *          Derk-Jan Hartman <hartman at videolan dot org>
  9.  *
  10.  * This program is free software; you can redistribute it and/or modify
  11.  * it under the terms of the GNU General Public License as published by
  12.  * the Free Software Foundation; either version 2 of the License, or
  13.  * (at your option) any later version.
  14.  *
  15.  * This program is distributed in the hope that it will be useful,
  16.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.  * GNU General Public License for more details.
  19.  *
  20.  * You should have received a copy of the GNU General Public License
  21.  * along with this program; if not, write to the Free Software
  22.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  23.  *****************************************************************************/
  24. /*****************************************************************************
  25.  * Preamble
  26.  *****************************************************************************/
  27. #ifdef HAVE_CONFIG_H
  28. # include "config.h"
  29. #endif
  30. #include <vlc_common.h>
  31. #include <vlc_plugin.h>
  32. #include <errno.h>
  33. #include <sys/types.h>
  34. #include <limits.h>
  35. #include <vlc_demux.h>
  36. #include <vlc_charset.h>
  37. #include "ps.h"
  38. #define MAX_LINE 8192
  39. /*****************************************************************************
  40.  * Module descriptor
  41.  *****************************************************************************/
  42. static int  Open ( vlc_object_t *p_this );
  43. static void Close( vlc_object_t *p_this );
  44. vlc_module_begin ()
  45.     set_description( N_("Vobsub subtitles parser") )
  46.     set_category( CAT_INPUT )
  47.     set_subcategory( SUBCAT_INPUT_DEMUX )
  48.     set_capability( "demux", 1 )
  49.     set_callbacks( Open, Close )
  50.     add_shortcut( "vobsub" )
  51.     add_shortcut( "subtitle" )
  52. vlc_module_end ()
  53. /*****************************************************************************
  54.  * Prototypes:
  55.  *****************************************************************************/
  56. typedef struct
  57. {
  58.     int     i_line_count;
  59.     int     i_line;
  60.     char    **line;
  61. } text_t;
  62. static int  TextLoad( text_t *, stream_t *s );
  63. static void TextUnload( text_t * );
  64. typedef struct
  65. {
  66.     int64_t i_start;
  67.     int     i_vobsub_location;
  68. } subtitle_t;
  69. typedef struct
  70. {
  71.     es_format_t fmt;
  72.     es_out_id_t *p_es;
  73.     int         i_track_id;
  74.     int         i_current_subtitle;
  75.     int         i_subtitles;
  76.     subtitle_t  *p_subtitles;
  77.     int64_t     i_delay;
  78. } vobsub_track_t;
  79. struct demux_sys_t
  80. {
  81.     int64_t     i_next_demux_date;
  82.     int64_t     i_length;
  83.     text_t      txt;
  84.     stream_t    *p_vobsub_stream;
  85.     /* all tracks */
  86.     int            i_tracks;
  87.     vobsub_track_t *track;
  88.     int         i_original_frame_width;
  89.     int         i_original_frame_height;
  90.     bool  b_palette;
  91.     uint32_t    palette[16];
  92. };
  93. static int Demux( demux_t * );
  94. static int Control( demux_t *, int, va_list );
  95. static int ParseVobSubIDX( demux_t * );
  96. static int DemuxVobSub( demux_t *, block_t *);
  97. /*****************************************************************************
  98.  * Module initializer
  99.  *****************************************************************************/
  100. static int Open ( vlc_object_t *p_this )
  101. {
  102.     demux_t     *p_demux = (demux_t*)p_this;
  103.     demux_sys_t *p_sys;
  104.     char *psz_vobname, *s;
  105.     int i_len;
  106.     if( ( s = stream_ReadLine( p_demux->s ) ) != NULL )
  107.     {
  108.         if( !strcasestr( s, "# VobSub index file" ) )
  109.         {
  110.             msg_Dbg( p_demux, "this doesn't seem to be a vobsub file" );
  111.             free( s );
  112.             if( stream_Seek( p_demux->s, 0 ) )
  113.             {
  114.                 msg_Warn( p_demux, "failed to rewind" );
  115.             }
  116.             return VLC_EGENERIC;
  117.         }
  118.         free( s );
  119.     }
  120.     else
  121.     {
  122.         msg_Dbg( p_demux, "could not read vobsub IDX file" );
  123.         return VLC_EGENERIC;
  124.     }
  125.     p_demux->pf_demux = Demux;
  126.     p_demux->pf_control = Control;
  127.     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
  128.     p_sys->i_length = 0;
  129.     p_sys->p_vobsub_stream = NULL;
  130.     p_sys->i_tracks = 0;
  131.     p_sys->track = (vobsub_track_t *)malloc( sizeof( vobsub_track_t ) );
  132.     p_sys->i_original_frame_width = -1;
  133.     p_sys->i_original_frame_height = -1;
  134.     p_sys->b_palette = false;
  135.     memset( p_sys->palette, 0, 16 * sizeof( uint32_t ) );
  136.     /* Load the whole file */
  137.     TextLoad( &p_sys->txt, p_demux->s );
  138.     /* Parse it */
  139.     ParseVobSubIDX( p_demux );
  140.     /* Unload */
  141.     TextUnload( &p_sys->txt );
  142.     /* Find the total length of the vobsubs */
  143.     if( p_sys->i_tracks > 0 )
  144.     {
  145.         int i;
  146.         for( i = 0; i < p_sys->i_tracks; i++ )
  147.         {
  148.             if( p_sys->track[i].i_subtitles > 1 )
  149.             {
  150.                 if( p_sys->track[i].p_subtitles[p_sys->track[i].i_subtitles-1].i_start > p_sys->i_length )
  151.                     p_sys->i_length = (int64_t) p_sys->track[i].p_subtitles[p_sys->track[i].i_subtitles-1].i_start + ( 1 *1000 *1000 );
  152.             }
  153.         }
  154.     }
  155.     if( asprintf( &psz_vobname, "%s://%s", p_demux->psz_access, p_demux->psz_path ) == -1 )
  156.     {
  157.         free( p_sys );
  158.         return VLC_EGENERIC;
  159.     }
  160.     i_len = strlen( psz_vobname );
  161.     if( i_len >= 4 ) memcpy( psz_vobname + i_len - 4, ".sub", 4 );
  162.     /* open file */
  163.     p_sys->p_vobsub_stream = stream_UrlNew( p_demux, psz_vobname );
  164.     if( p_sys->p_vobsub_stream == NULL )
  165.     {
  166.         msg_Err( p_demux, "couldn't open .sub Vobsub file: %s",
  167.                  psz_vobname );
  168.         free( psz_vobname );
  169.         free( p_sys );
  170.         return VLC_EGENERIC;
  171.     }
  172.     free( psz_vobname );
  173.     return VLC_SUCCESS;
  174. }
  175. /*****************************************************************************
  176.  * Close: Close subtitle demux
  177.  *****************************************************************************/
  178. static void Close( vlc_object_t *p_this )
  179. {
  180.     int i;
  181.     demux_t *p_demux = (demux_t*)p_this;
  182.     demux_sys_t *p_sys = p_demux->p_sys;
  183.     /* Clean all subs from all tracks */
  184.     for( i = 0; i < p_sys->i_tracks; i++ )
  185.         free( p_sys->track[i].p_subtitles );
  186.     free( p_sys->track );
  187.     if( p_sys->p_vobsub_stream )
  188.         stream_Delete( p_sys->p_vobsub_stream );
  189.     free( p_sys );
  190. }
  191. /*****************************************************************************
  192.  * Control:
  193.  *****************************************************************************/
  194. static int Control( demux_t *p_demux, int i_query, va_list args )
  195. {
  196.     demux_sys_t *p_sys = p_demux->p_sys;
  197.     int64_t *pi64, i64;
  198.     int i;
  199.     double *pf, f;
  200.     switch( i_query )
  201.     {
  202.         case DEMUX_GET_LENGTH:
  203.             pi64 = (int64_t*)va_arg( args, int64_t * );
  204.             *pi64 = (int64_t) p_sys->i_length;
  205.             return VLC_SUCCESS;
  206.         case DEMUX_GET_TIME:
  207.             pi64 = (int64_t*)va_arg( args, int64_t * );
  208.             for( i = 0; i < p_sys->i_tracks; i++ )
  209.             {
  210.                 bool b_selected;
  211.                 /* Check the ES is selected */
  212.                 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
  213.                                 p_sys->track[i].p_es, &b_selected );
  214.                 if( b_selected ) break;
  215.             }
  216.             if( i < p_sys->i_tracks && p_sys->track[i].i_current_subtitle < p_sys->track[i].i_subtitles )
  217.             {
  218.                 *pi64 = p_sys->track[i].p_subtitles[p_sys->track[i].i_current_subtitle].i_start;
  219.                 return VLC_SUCCESS;
  220.             }
  221.             return VLC_EGENERIC;
  222.         case DEMUX_SET_TIME:
  223.             i64 = (int64_t)va_arg( args, int64_t );
  224.             for( i = 0; i < p_sys->i_tracks; i++ )
  225.             {
  226.                 p_sys->track[i].i_current_subtitle = 0;
  227.                 while( p_sys->track[i].i_current_subtitle < p_sys->track[i].i_subtitles &&
  228.                        p_sys->track[i].p_subtitles[p_sys->track[i].i_current_subtitle].i_start < i64 )
  229.                 {
  230.                     p_sys->track[i].i_current_subtitle++;
  231.                 }
  232.                 if( p_sys->track[i].i_current_subtitle >= p_sys->track[i].i_subtitles )
  233.                     return VLC_EGENERIC;
  234.             }
  235.             return VLC_SUCCESS;
  236.         case DEMUX_GET_POSITION:
  237.             pf = (double*)va_arg( args, double * );
  238.             for( i = 0; i < p_sys->i_tracks; i++ )
  239.             {
  240.                 bool b_selected;
  241.                 /* Check the ES is selected */
  242.                 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
  243.                                 p_sys->track[i].p_es, &b_selected );
  244.                 if( b_selected ) break;
  245.             }
  246.             if( p_sys->track[i].i_current_subtitle >= p_sys->track[i].i_subtitles )
  247.             {
  248.                 *pf = 1.0;
  249.             }
  250.             else if( p_sys->track[i].i_subtitles > 0 )
  251.             {
  252.                 *pf = (double)p_sys->track[i].p_subtitles[p_sys->track[i].i_current_subtitle].i_start /
  253.                       (double)p_sys->i_length;
  254.             }
  255.             else
  256.             {
  257.                 *pf = 0.0;
  258.             }
  259.             return VLC_SUCCESS;
  260.         case DEMUX_SET_POSITION:
  261.             f = (double)va_arg( args, double );
  262.             i64 = (int64_t) f * p_sys->i_length;
  263.             for( i = 0; i < p_sys->i_tracks; i++ )
  264.             {
  265.                 p_sys->track[i].i_current_subtitle = 0;
  266.                 while( p_sys->track[i].i_current_subtitle < p_sys->track[i].i_subtitles &&
  267.                        p_sys->track[i].p_subtitles[p_sys->track[i].i_current_subtitle].i_start < i64 )
  268.                 {
  269.                     p_sys->track[i].i_current_subtitle++;
  270.                 }
  271.                 if( p_sys->track[i].i_current_subtitle >= p_sys->track[i].i_subtitles )
  272.                     return VLC_EGENERIC;
  273.             }
  274.             return VLC_SUCCESS;
  275.         case DEMUX_SET_NEXT_DEMUX_TIME:
  276.             p_sys->i_next_demux_date = (int64_t)va_arg( args, int64_t );
  277.             return VLC_SUCCESS;
  278.         case DEMUX_GET_FPS:
  279.         case DEMUX_GET_META:
  280.         case DEMUX_GET_TITLE_INFO:
  281.         case DEMUX_HAS_UNSUPPORTED_META:
  282.         case DEMUX_GET_ATTACHMENTS:
  283.         case DEMUX_CAN_RECORD:
  284.             return VLC_EGENERIC;
  285.         default:
  286.             msg_Warn( p_demux, "unknown query in subtitle control" );
  287.             return VLC_EGENERIC;
  288.     }
  289. }
  290. /*****************************************************************************
  291.  * Demux: Send subtitle to decoder
  292.  *****************************************************************************/
  293. static int Demux( demux_t *p_demux )
  294. {
  295.     demux_sys_t *p_sys = p_demux->p_sys;
  296.     int64_t i_maxdate;
  297.     int i, i_read;
  298.     for( i = 0; i < p_sys->i_tracks; i++ )
  299.     {
  300. #define tk p_sys->track[i]
  301.         if( tk.i_current_subtitle >= tk.i_subtitles )
  302.             continue;
  303.         i_maxdate = p_sys->i_next_demux_date;
  304.         if( i_maxdate <= 0 && tk.i_current_subtitle < tk.i_subtitles )
  305.         {
  306.             /* Should not happen */
  307.             i_maxdate = tk.p_subtitles[tk.i_current_subtitle].i_start + 1;
  308.         }
  309.         while( tk.i_current_subtitle < tk.i_subtitles &&
  310.                tk.p_subtitles[tk.i_current_subtitle].i_start < i_maxdate )
  311.         {
  312.             int i_pos = tk.p_subtitles[tk.i_current_subtitle].i_vobsub_location;
  313.             block_t *p_block;
  314.             int i_size = 0;
  315.             /* first compute SPU size */
  316.             if( tk.i_current_subtitle + 1 < tk.i_subtitles )
  317.             {
  318.                 i_size = tk.p_subtitles[tk.i_current_subtitle+1].i_vobsub_location - i_pos;
  319.             }
  320.             if( i_size <= 0 ) i_size = 65535;   /* Invalid or EOF */
  321.             /* Seek at the right place */
  322.             if( stream_Seek( p_sys->p_vobsub_stream, i_pos ) )
  323.             {
  324.                 msg_Warn( p_demux,
  325.                           "cannot seek in the VobSub to the correct time %d", i_pos );
  326.                 tk.i_current_subtitle++;
  327.                 continue;
  328.             }
  329.             /* allocate a packet */
  330.             if( ( p_block = block_New( p_demux, i_size ) ) == NULL )
  331.             {
  332.                 tk.i_current_subtitle++;
  333.                 continue;
  334.             }
  335.             /* read data */
  336.             i_read = stream_Read( p_sys->p_vobsub_stream, p_block->p_buffer, i_size );
  337.             if( i_read <= 6 )
  338.             {
  339.                 block_Release( p_block );
  340.                 tk.i_current_subtitle++;
  341.                 continue;
  342.             }
  343.             p_block->i_buffer = i_read;
  344.             /* pts */
  345.             p_block->i_pts = tk.p_subtitles[tk.i_current_subtitle].i_start;
  346.             /* demux this block */
  347.             DemuxVobSub( p_demux, p_block );
  348.             tk.i_current_subtitle++;
  349.         }
  350. #undef tk
  351.     }
  352.     /* */
  353.     p_sys->i_next_demux_date = 0;
  354.     return 1;
  355. }
  356. static int TextLoad( text_t *txt, stream_t *s )
  357. {
  358.     char **lines = NULL;
  359.     size_t n = 0;
  360.     /* load the complete file */
  361.     for( ;; )
  362.     {
  363.         char *psz = stream_ReadLine( s );
  364.         char **ppsz_new;
  365.         if( psz == NULL || (n >= INT_MAX/sizeof(char *)) )
  366.             break;
  367.         ppsz_new = realloc( lines, (n + 1) * sizeof (char *) );
  368.         if( ppsz_new == NULL )
  369.         {
  370.             free( psz );
  371.             break;
  372.         }
  373.         lines = ppsz_new;
  374.         lines[n++] = psz;
  375.     }
  376.     txt->i_line_count = n;
  377.     txt->i_line       = 0;
  378.     txt->line         = lines;
  379.     return VLC_SUCCESS;
  380. }
  381. static void TextUnload( text_t *txt )
  382. {
  383.     int i;
  384.     for( i = 0; i < txt->i_line_count; i++ )
  385.         free( txt->line[i] );
  386.     free( txt->line );
  387.     txt->i_line       = 0;
  388.     txt->i_line_count = 0;
  389. }
  390. static char *TextGetLine( text_t *txt )
  391. {
  392.     if( txt->i_line >= txt->i_line_count )
  393.         return( NULL );
  394.     return txt->line[txt->i_line++];
  395. }
  396. static int ParseVobSubIDX( demux_t *p_demux )
  397. {
  398.     demux_sys_t *p_sys = p_demux->p_sys;
  399.     text_t      *txt = &p_sys->txt;
  400.     char        *line;
  401.     vobsub_track_t *current_tk = NULL;
  402.     for( ;; )
  403.     {
  404.         if( ( line = TextGetLine( txt ) ) == NULL )
  405.         {
  406.             return( VLC_EGENERIC );
  407.         }
  408.         if( *line == 0 || *line == 'r' || *line == 'n' || *line == '#' )
  409.         {
  410.             continue;
  411.         }
  412.         else if( !strncmp( "size:", line, 5 ) )
  413.         {
  414.             /* Store the original size of the video */
  415.             if( sscanf( line, "size: %dx%d",
  416.                         &p_sys->i_original_frame_width, &p_sys->i_original_frame_height ) == 2 )
  417.             {
  418.                 msg_Dbg( p_demux, "original frame size: %dx%d", p_sys->i_original_frame_width, p_sys->i_original_frame_height );
  419.             }
  420.             else
  421.             {
  422.                 msg_Warn( p_demux, "reading original frame size failed" );
  423.             }
  424.         }
  425.         else if( !strncmp( "palette:", line, 8 ) )
  426.         {
  427.             int i;
  428.             /* Store the palette of the subs */
  429.             if( sscanf( line, "palette: %x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x",
  430.                         &p_sys->palette[0], &p_sys->palette[1], &p_sys->palette[2], &p_sys->palette[3],
  431.                         &p_sys->palette[4], &p_sys->palette[5], &p_sys->palette[6], &p_sys->palette[7],
  432.                         &p_sys->palette[8], &p_sys->palette[9], &p_sys->palette[10], &p_sys->palette[11],
  433.                         &p_sys->palette[12], &p_sys->palette[13], &p_sys->palette[14], &p_sys->palette[15] ) == 16 )
  434.             {
  435.                 for( i = 0; i < 16; i++ )
  436.                 {
  437.                     uint8_t r = 0, g = 0, b = 0;
  438.                     uint8_t y = 0, u = 0, v = 0;
  439.                     r = (p_sys->palette[i] >> 16) & 0xff;
  440.                     g = (p_sys->palette[i] >> 8) & 0xff;
  441.                     b = (p_sys->palette[i] >> 0) & 0xff;
  442.                     /* msg_Dbg( p_demux, "palette %d: R=%x, G=%x, B=%x", i, r, g, b ); */
  443.                     y = (uint8_t) __MIN(abs(r * 2104 + g * 4130 + b * 802 + 4096 + 131072) >> 13, 235);
  444.                     u = (uint8_t) __MIN(abs(r * -1214 + g * -2384 + b * 3598 + 4096 + 1048576) >> 13, 240);
  445.                     v = (uint8_t) __MIN(abs(r * 3598 + g * -3013 + b * -585 + 4096 + 1048576) >> 13, 240);
  446.                     p_sys->palette[i] = 0;
  447.                     p_sys->palette[i] |= (y&0xff)<<16;
  448.                     p_sys->palette[i] |= (u&0xff);
  449.                     p_sys->palette[i] |= (v&0xff)<<8;
  450.                     /* msg_Dbg( p_demux, "palette %d: y=%x, u=%x, v=%x", i, y, u, v ); */
  451.                 }
  452.                 p_sys->b_palette = true;
  453.                 msg_Dbg( p_demux, "vobsub palette read" );
  454.             }
  455.             else
  456.             {
  457.                 msg_Warn( p_demux, "reading original palette failed" );
  458.             }
  459.         }
  460.         else if( !strncmp( "id:", line, 3 ) )
  461.         {
  462.             char language[3];
  463.             int i_track_id;
  464.             es_format_t fmt;
  465.             /* Lets start a new track */
  466.             if( sscanf( line, "id: %2s, index: %d",
  467.                         language, &i_track_id ) == 2 )
  468.             {
  469.                 p_sys->i_tracks++;
  470.                 p_sys->track = realloc( p_sys->track, sizeof( vobsub_track_t ) * (p_sys->i_tracks + 1 ) );
  471.                 language[2] = '';
  472.                 /* Init the track */
  473.                 current_tk = &p_sys->track[p_sys->i_tracks - 1];
  474.                 memset( current_tk, 0, sizeof( vobsub_track_t ) );
  475.                 current_tk->i_current_subtitle = 0;
  476.                 current_tk->i_subtitles = 0;
  477.                 current_tk->p_subtitles = malloc( sizeof( subtitle_t ) );;
  478.                 current_tk->i_track_id = i_track_id;
  479.                 current_tk->i_delay = (int64_t)0;
  480.                 es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','p','u',' ' ) );
  481.                 fmt.subs.spu.i_original_frame_width = p_sys->i_original_frame_width;
  482.                 fmt.subs.spu.i_original_frame_height = p_sys->i_original_frame_height;
  483.                 fmt.psz_language = language;
  484.                 if( p_sys->b_palette )
  485.                 {
  486.                     fmt.subs.spu.palette[0] = 0xBeef;
  487.                     memcpy( &fmt.subs.spu.palette[1], p_sys->palette, 16 * sizeof( uint32_t ) );
  488.                 }
  489.                 current_tk->p_es = es_out_Add( p_demux->out, &fmt );
  490.                 msg_Dbg( p_demux, "new vobsub track detected" );
  491.             }
  492.             else
  493.             {
  494.                 msg_Warn( p_demux, "reading new track failed" );
  495.             }
  496.         }
  497.         else if( !strncmp( line, "timestamp:", 10 ) )
  498.         {
  499.             /*
  500.              * timestamp: [sign]hh:mm:ss:mss, filepos: loc
  501.              * loc is the hex location of the spu in the .sub file
  502.              */
  503.             int h, m, s, ms, count, loc = 0;
  504.             int i_sign = 1;
  505.             int64_t i_start, i_location = 0;
  506.             if( p_sys->i_tracks > 0 &&
  507.                 sscanf( line, "timestamp: %d%n:%d:%d:%d, filepos: %x",
  508.                         &h, &count, &m, &s, &ms, &loc ) >= 5  )
  509.             {
  510.                 vobsub_track_t *current_tk = &p_sys->track[p_sys->i_tracks - 1];
  511.                 subtitle_t *current_sub;
  512.                 if( line[count-3] == '-' )
  513.                 {
  514.                     i_sign = -1;
  515.                     h = -h;
  516.                 }
  517.                 i_start = (int64_t) ( h * 3600*1000 +
  518.                             m * 60*1000 +
  519.                             s * 1000 +
  520.                             ms ) * 1000;
  521.                 i_location = loc;
  522.                 current_tk->i_subtitles++;
  523.                 current_tk->p_subtitles = realloc( current_tk->p_subtitles, sizeof( subtitle_t ) * (current_tk->i_subtitles + 1 ) );
  524.                 current_sub = &current_tk->p_subtitles[current_tk->i_subtitles - 1];
  525.                 current_sub->i_start = i_start * i_sign;
  526.                 current_sub->i_start += current_tk->i_delay;
  527.                 current_sub->i_vobsub_location = i_location;
  528.             }
  529.             else
  530.             {
  531.                 msg_Warn( p_demux, "reading timestamp failed" );
  532.             }
  533.         }
  534.         else if( !strncasecmp( line, "delay:", 6 ) )
  535.         {
  536.             /*
  537.              * delay: [sign]hh:mm:ss:mss
  538.              */
  539.             int h, m, s, ms, count = 0;
  540.             int i_sign = 1;
  541.             int64_t i_gap = 0;
  542.             if( p_sys->i_tracks > 0 &&
  543.                 sscanf( line, "%*celay: %d%n:%d:%d:%d",
  544.                         &h, &count, &m, &s, &ms ) >= 4 )
  545.             {
  546.                 vobsub_track_t *current_tk = &p_sys->track[p_sys->i_tracks - 1];
  547.                 if( line[count-3] == '-' )
  548.                 {
  549.                     i_sign = -1;
  550.                     h = -h;
  551.                 }
  552.                 i_gap = (int64_t) ( h * 3600*1000 +
  553.                             m * 60*1000 +
  554.                             s * 1000 +
  555.                             ms ) * 1000;
  556.                 current_tk->i_delay = current_tk->i_delay + (i_gap * i_sign);
  557.                 msg_Dbg( p_demux, "sign: %+d gap: %+lld global delay: %+lld",
  558.                          i_sign, (long long)i_gap,
  559.                          (long long)current_tk->i_delay  );
  560.             }
  561.             else
  562.             {
  563.                 msg_Warn( p_demux, "reading delay failed" );
  564.             }
  565.         }
  566.     }
  567.     return( 0 );
  568. }
  569. static int DemuxVobSub( demux_t *p_demux, block_t *p_bk )
  570. {
  571.     demux_sys_t *p_sys = p_demux->p_sys;
  572.     uint8_t     *p = p_bk->p_buffer;
  573.     uint8_t     *p_end = &p_bk->p_buffer[p_bk->i_buffer];
  574.     int i;
  575.     while( p + 6 < p_end )
  576.     {
  577.         int i_size = ps_pkt_size( p, p_end - p );
  578.         block_t *p_pkt;
  579.         int      i_id;
  580.         int      i_spu;
  581.         if( i_size <= 0 )
  582.             break;
  583.         if( i_size > p_end - p )
  584.         {
  585.             msg_Warn( p_demux, "broken PES size" );
  586.             break;
  587.         }
  588.         if( p[0] != 0 || p[1] != 0 || p[2] != 0x01 )
  589.         {
  590.             msg_Warn( p_demux, "invalid PES" );
  591.             break;
  592.         }
  593.         if( p[3] != 0xbd )
  594.         {
  595.             /* msg_Dbg( p_demux, "we don't need these ps packets (id=0x1%2.2x)", p[3] ); */
  596.             p += i_size;
  597.             continue;
  598.         }
  599.         /* Create a block */
  600.         p_pkt = block_New( p_demux, i_size );
  601.         memcpy( p_pkt->p_buffer, p, i_size);
  602.         p += i_size;
  603.         i_id = ps_pkt_id( p_pkt );
  604.         if( (i_id&0xffe0) != 0xbd20 ||
  605.             ps_pkt_parse_pes( p_pkt, 1 ) )
  606.         {
  607.             block_Release( p_pkt );
  608.             continue;
  609.         }
  610.         i_spu = i_id&0x1f;
  611.         /* msg_Dbg( p_demux, "SPU track %d size %d", i_spu, i_size ); */
  612.         for( i = 0; i < p_sys->i_tracks; i++ )
  613.         {
  614.             vobsub_track_t *p_tk = &p_sys->track[i];
  615.             p_pkt->i_dts = p_pkt->i_pts = p_bk->i_pts;
  616.             p_pkt->i_length = 0;
  617.             if( p_tk->p_es && p_tk->i_track_id == i_spu )
  618.             {
  619.                 es_out_Send( p_demux->out, p_tk->p_es, p_pkt );
  620.                 p_bk->i_pts = 0;     /*only first packet has a pts */
  621.                 break;
  622.             }
  623.         }
  624.         if( i >= p_sys->i_tracks )
  625.         {
  626.             block_Release( p_pkt );
  627.         }
  628.     }
  629.     return VLC_SUCCESS;
  630. }