vobsub.c
上传用户:riyaled888
上传日期:2009-03-27
资源大小:7338k
文件大小:19k
源码类别:

多媒体

开发平台:

MultiPlatform

  1. /*****************************************************************************
  2.  * subtitle.c: Demux vobsub files.
  3.  *****************************************************************************
  4.  * Copyright (C) 1999-2004 VideoLAN
  5.  * $Id: vobsub.c 9003 2004-10-17 13:38:22Z hartman $
  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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
  23.  *****************************************************************************/
  24. /*****************************************************************************
  25.  * Preamble
  26.  *****************************************************************************/
  27. #include <stdlib.h>
  28. #include <errno.h>
  29. #include <sys/types.h>
  30. #include <vlc/vlc.h>
  31. #include <vlc/input.h>
  32. #include "vlc_video.h"
  33. #include "ps.h"
  34. #define MAX_LINE 8192
  35. /*****************************************************************************
  36.  * Module descriptor
  37.  *****************************************************************************/
  38. static int  Open ( vlc_object_t *p_this );
  39. static void Close( vlc_object_t *p_this );
  40. vlc_module_begin();
  41.     set_description( _("Vobsub subtitles demux") );
  42.     set_capability( "demux2", 1 );
  43.     
  44.     set_callbacks( Open, Close );
  45.     add_shortcut( "vobsub" );
  46.     add_shortcut( "subtitle" );
  47. vlc_module_end();
  48. /*****************************************************************************
  49.  * Prototypes:
  50.  *****************************************************************************/
  51. typedef struct
  52. {
  53.     int     i_line_count;
  54.     int     i_line;
  55.     char    **line;
  56. } text_t;
  57. static int  TextLoad( text_t *, stream_t *s );
  58. static void TextUnload( text_t * );
  59. typedef struct
  60. {
  61.     int64_t i_start;
  62.     int     i_vobsub_location;
  63. } subtitle_t;
  64. typedef struct
  65. {
  66.     es_format_t fmt;
  67.     es_out_id_t *p_es;
  68.     int         i_track_id;
  69.     
  70.     int         i_current_subtitle;
  71.     int         i_subtitles;
  72.     subtitle_t  *p_subtitles;
  73. } vobsub_track_t;
  74. struct demux_sys_t
  75. {
  76.     int64_t     i_next_demux_date;
  77.     int64_t     i_length;
  78.     text_t      txt;
  79.     FILE        *p_vobsub_file;
  80.     
  81.     /* all tracks */
  82.     int            i_tracks;
  83.     vobsub_track_t *track;
  84.     
  85.     int         i_original_frame_width;
  86.     int         i_original_frame_height;
  87. };
  88. static int Demux( demux_t * );
  89. static int Control( demux_t *, int, va_list );
  90. static int ParseVobSubIDX( demux_t * );
  91. static int DemuxVobSub( demux_t *, block_t *);
  92. /*****************************************************************************
  93.  * Module initializer
  94.  *****************************************************************************/
  95. static int Open ( vlc_object_t *p_this )
  96. {
  97.     demux_t     *p_demux = (demux_t*)p_this;
  98.     demux_sys_t *p_sys;
  99.     char *psz_vobname, *s;
  100.     int i_len;
  101.     if( ( s = stream_ReadLine( p_demux->s ) ) != NULL )
  102.     {
  103.         if( !strcasestr( s, "# VobSub index file" ) )
  104.         {
  105.             msg_Dbg( p_demux, "this doesn't seem to be a vobsub file" );
  106.             free( s );
  107.             if( stream_Seek( p_demux->s, 0 ) )
  108.             {
  109.                 msg_Warn( p_demux, "failed to rewind" );
  110.             }
  111.             return VLC_EGENERIC;
  112.         }
  113.         free( s );
  114.     }
  115.     else
  116.     {
  117.         msg_Dbg( p_demux, "could not read vobsub IDX file" );
  118.         return VLC_EGENERIC;
  119.     }
  120.     p_demux->pf_demux = Demux;
  121.     p_demux->pf_control = Control;
  122.     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
  123.     p_sys->i_length = 0;
  124.     p_sys->p_vobsub_file = NULL;
  125.     p_sys->i_tracks = 0;
  126.     p_sys->track = (vobsub_track_t *)malloc( sizeof( vobsub_track_t ) );
  127.     p_sys->i_original_frame_width = -1;
  128.     p_sys->i_original_frame_height = -1;
  129.     /* Load the whole file */
  130.     TextLoad( &p_sys->txt, p_demux->s );
  131.     /* Parse it */
  132.     ParseVobSubIDX( p_demux );
  133.     /* Unload */
  134.     TextUnload( &p_sys->txt );
  135.     /* Find the total length of the vobsubs */
  136.     if( p_sys->i_tracks > 0 )
  137.     {
  138.         int i;
  139.         for( i = 0; i < p_sys->i_tracks; i++ )
  140.         {
  141.             if( p_sys->track[i].i_subtitles > 1 )
  142.             {
  143.                 if( p_sys->track[i].p_subtitles[p_sys->track[i].i_subtitles-1].i_start > p_sys->i_length )
  144.                     p_sys->i_length = (int64_t) p_sys->track[i].p_subtitles[p_sys->track[i].i_subtitles-1].i_start + ( 1 *1000 *1000 );
  145.             }
  146.         }
  147.     }
  148.     i_len = strlen( p_demux->psz_path );
  149.     psz_vobname = strdup( p_demux->psz_path );
  150.     strcpy( psz_vobname + i_len - 4, ".sub" );
  151.     /* open file */
  152.     if( !( p_sys->p_vobsub_file = fopen( psz_vobname, "rb" ) ) )
  153.     {
  154.         msg_Err( p_demux, "couldn't open .sub Vobsub file: %s",
  155.                  psz_vobname );
  156.         free( p_sys );
  157.         free( psz_vobname );
  158.         return VLC_EGENERIC;
  159.     }
  160.     free( psz_vobname );
  161.     return VLC_SUCCESS;
  162. }
  163. /*****************************************************************************
  164.  * Close: Close subtitle demux
  165.  *****************************************************************************/
  166. static void Close( vlc_object_t *p_this )
  167. {
  168.     demux_t *p_demux = (demux_t*)p_this;
  169.     demux_sys_t *p_sys = p_demux->p_sys;
  170.     /* Clean all subs from all tracks
  171.     if( p_sys->subtitle )
  172.         free( p_sys->subtitle );
  173. */
  174.     if( p_sys->p_vobsub_file )
  175.         fclose( p_sys->p_vobsub_file );
  176.     free( p_sys );
  177. }
  178. /*****************************************************************************
  179.  * Control:
  180.  *****************************************************************************/
  181. static int Control( demux_t *p_demux, int i_query, va_list args )
  182. {
  183.     demux_sys_t *p_sys = p_demux->p_sys;
  184.     int64_t *pi64, i64;
  185.     int i;
  186.     double *pf, f;
  187.     switch( i_query )
  188.     {
  189.         case DEMUX_GET_LENGTH:
  190.             pi64 = (int64_t*)va_arg( args, int64_t * );
  191.             *pi64 = (int64_t) p_sys->i_length;
  192.             return VLC_SUCCESS;
  193.         case DEMUX_GET_TIME:
  194.             pi64 = (int64_t*)va_arg( args, int64_t * );
  195.             for( i = 0; i < p_sys->i_tracks; i++ )
  196.             {
  197.                 vlc_bool_t b_selected;
  198.                 /* Check the ES is selected */
  199.                 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
  200.                                 p_sys->track[i].p_es, &b_selected );
  201.                 if( b_selected ) break;
  202.             }
  203.             if( i < p_sys->i_tracks && p_sys->track[i].i_current_subtitle < p_sys->track[i].i_subtitles )
  204.             {
  205.                 *pi64 = p_sys->track[i].p_subtitles[p_sys->track[i].i_current_subtitle].i_start;
  206.                 return VLC_SUCCESS;
  207.             }
  208.             return VLC_EGENERIC;
  209.         case DEMUX_SET_TIME:
  210.             i64 = (int64_t)va_arg( args, int64_t );
  211.             for( i = 0; i < p_sys->i_tracks; i++ )
  212.             {
  213.                 p_sys->track[i].i_current_subtitle = 0;
  214.                 while( p_sys->track[i].i_current_subtitle < p_sys->track[i].i_subtitles &&
  215.                        p_sys->track[i].p_subtitles[p_sys->track[i].i_current_subtitle].i_start < i64 )
  216.                 {
  217.                     p_sys->track[i].i_current_subtitle++;
  218.                 }
  219.                 if( p_sys->track[i].i_current_subtitle >= p_sys->track[i].i_subtitles )
  220.                     return VLC_EGENERIC;
  221.             }
  222.             return VLC_SUCCESS;
  223.         case DEMUX_GET_POSITION:
  224.             pf = (double*)va_arg( args, double * );
  225.             for( i = 0; i < p_sys->i_tracks; i++ )
  226.             {
  227.                 vlc_bool_t b_selected;
  228.                 /* Check the ES is selected */
  229.                 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
  230.                                 p_sys->track[i].p_es, &b_selected );
  231.                 if( b_selected ) break;
  232.             }
  233.             if( p_sys->track[i].i_current_subtitle >= p_sys->track[i].i_subtitles )
  234.             {
  235.                 *pf = 1.0;
  236.             }
  237.             else if( p_sys->track[i].i_subtitles > 0 )
  238.             {
  239.                 *pf = (double)p_sys->track[i].p_subtitles[p_sys->track[i].i_current_subtitle].i_start /
  240.                       (double)p_sys->i_length;
  241.             }
  242.             else
  243.             {
  244.                 *pf = 0.0;
  245.             }
  246.             return VLC_SUCCESS;
  247.         case DEMUX_SET_POSITION:
  248.             f = (double)va_arg( args, double );
  249.             i64 = (int64_t) f * p_sys->i_length;
  250.             for( i = 0; i < p_sys->i_tracks; i++ )
  251.             {
  252.                 p_sys->track[i].i_current_subtitle = 0;
  253.                 while( p_sys->track[i].i_current_subtitle < p_sys->track[i].i_subtitles &&
  254.                        p_sys->track[i].p_subtitles[p_sys->track[i].i_current_subtitle].i_start < i64 )
  255.                 {
  256.                     p_sys->track[i].i_current_subtitle++;
  257.                 }
  258.                 if( p_sys->track[i].i_current_subtitle >= p_sys->track[i].i_subtitles )
  259.                     return VLC_EGENERIC;
  260.             }
  261.             return VLC_SUCCESS;
  262.         case DEMUX_SET_NEXT_DEMUX_TIME:
  263.             p_sys->i_next_demux_date = (int64_t)va_arg( args, int64_t );
  264.             return VLC_SUCCESS;
  265.         case DEMUX_GET_FPS:
  266.         case DEMUX_GET_META:
  267.         case DEMUX_GET_TITLE_INFO:
  268.             return VLC_EGENERIC;
  269.         default:
  270.             msg_Err( p_demux, "unknown query in subtitle control" );
  271.             return VLC_EGENERIC;
  272.     }
  273. }
  274. /*****************************************************************************
  275.  * Demux: Send subtitle to decoder
  276.  *****************************************************************************/
  277. static int Demux( demux_t *p_demux )
  278. {
  279.     demux_sys_t *p_sys = p_demux->p_sys;
  280.     int64_t i_maxdate;
  281.     int i;
  282.     for( i = 0; i < p_sys->i_tracks; i++ )
  283.     {
  284. #define tk p_sys->track[i]
  285.         if( tk.i_current_subtitle >= tk.i_subtitles )
  286.             continue;
  287.         i_maxdate = (int64_t) p_sys->i_next_demux_date;
  288.         if( i_maxdate <= 0 && tk.i_current_subtitle < tk.i_subtitles )
  289.         {
  290.             /* Should not happen */
  291.             i_maxdate = (int64_t) tk.p_subtitles[tk.i_current_subtitle].i_start + 1;
  292.         }
  293.         while( tk.i_current_subtitle < tk.i_subtitles &&
  294.                tk.p_subtitles[tk.i_current_subtitle].i_start < i_maxdate )
  295.         {
  296.             int i_pos = tk.p_subtitles[tk.i_current_subtitle].i_vobsub_location;
  297.             block_t *p_block;
  298.             int i_size = 0;
  299.             /* first compute SPU size */
  300.             if( tk.i_current_subtitle + 1 < tk.i_subtitles )
  301.             {
  302.                 i_size = tk.p_subtitles[tk.i_current_subtitle+1].i_vobsub_location - i_pos;
  303.             }
  304.             if( i_size <= 0 ) i_size = 65535;   /* Invalid or EOF */
  305.             /* Seek at the right place */
  306.             if( fseek( p_sys->p_vobsub_file, i_pos, SEEK_SET ) )
  307.             {
  308.                 msg_Warn( p_demux,
  309.                           "cannot seek at right vobsub location %d", i_pos );
  310.                 tk.i_current_subtitle++;
  311.                 continue;
  312.             }
  313.             /* allocate a packet */
  314.             if( ( p_block = block_New( p_demux, i_size ) ) == NULL )
  315.             {
  316.                 tk.i_current_subtitle++;
  317.                 continue;
  318.             }
  319.             /* read data */
  320.             p_block->i_buffer = fread( p_block->p_buffer, 1, i_size,
  321.                                        p_sys->p_vobsub_file );
  322.             if( p_block->i_buffer <= 6 )
  323.             {
  324.                 block_Release( p_block );
  325.                 tk.i_current_subtitle++;
  326.                 continue;
  327.             }
  328.             /* pts */
  329.             p_block->i_pts = tk.p_subtitles[tk.i_current_subtitle].i_start;
  330.             /* demux this block */
  331.             DemuxVobSub( p_demux, p_block );
  332.             tk.i_current_subtitle++;
  333.         }
  334. #undef tk
  335.     }
  336.     /* */
  337.     p_sys->i_next_demux_date = 0;
  338.     return 1;
  339. }
  340. static int TextLoad( text_t *txt, stream_t *s )
  341. {
  342.     int   i_line_max;
  343.     /* init txt */
  344.     i_line_max          = 500;
  345.     txt->i_line_count   = 0;
  346.     txt->i_line         = 0;
  347.     txt->line           = calloc( i_line_max, sizeof( char * ) );
  348.     /* load the complete file */
  349.     for( ;; )
  350.     {
  351.         char *psz = stream_ReadLine( s );
  352.         if( psz == NULL )
  353.             break;
  354.         txt->line[txt->i_line_count++] = psz;
  355.         if( txt->i_line_count >= i_line_max )
  356.         {
  357.             i_line_max += 100;
  358.             txt->line = realloc( txt->line, i_line_max * sizeof( char*) );
  359.         }
  360.     }
  361.     if( txt->i_line_count <= 0 )
  362.     {
  363.         free( txt->line );
  364.         return VLC_EGENERIC;
  365.     }
  366.     return VLC_SUCCESS;
  367. }
  368. static void TextUnload( text_t *txt )
  369. {
  370.     int i;
  371.     for( i = 0; i < txt->i_line_count; i++ )
  372.     {
  373.         free( txt->line[i] );
  374.     }
  375.     free( txt->line );
  376.     txt->i_line       = 0;
  377.     txt->i_line_count = 0;
  378. }
  379. static char *TextGetLine( text_t *txt )
  380. {
  381.     if( txt->i_line >= txt->i_line_count )
  382.         return( NULL );
  383.     return txt->line[txt->i_line++];
  384. }
  385. static int ParseVobSubIDX( demux_t *p_demux )
  386. {
  387.     demux_sys_t *p_sys = p_demux->p_sys;
  388.     text_t      *txt = &p_sys->txt;
  389.     char        *line;
  390.     vobsub_track_t *current_tk;
  391.     for( ;; )
  392.     {
  393.         if( ( line = TextGetLine( txt ) ) == NULL )
  394.         {
  395.             return( VLC_EGENERIC );
  396.         }
  397.         
  398.         if( *line == 0 || *line == 'r' || *line == 'n' || *line == '#' ) 
  399.             continue;
  400.         else if( !strncmp( "size:", line, 5 ) )
  401.         {
  402.             /* Store the original size of the video */
  403.             if( sscanf( line, "size: %dx%d",
  404.                         &p_sys->i_original_frame_width, &p_sys->i_original_frame_height ) == 2 )
  405.             {
  406.                 msg_Dbg( p_demux, "original frame size: %dx%d", p_sys->i_original_frame_width, p_sys->i_original_frame_height );
  407.             }
  408.             else
  409.             {
  410.                 msg_Warn( p_demux, "reading original frame size failed" );
  411.             }
  412.         }
  413.         else if( !strncmp( "id:", line, 3 ) )
  414.         {
  415.             char language[20];
  416.             int i_track_id;
  417.             es_format_t fmt;
  418.             /* Lets start a new track */
  419.             if( sscanf( line, "id: %2s, index: %d",
  420.                         language, &i_track_id ) == 2 )
  421.             {
  422.                 p_sys->i_tracks++;
  423.                 p_sys->track = (vobsub_track_t*)realloc( p_sys->track, sizeof( vobsub_track_t ) * (p_sys->i_tracks + 1 ) );
  424.                 /* Init the track */
  425.                 current_tk = &p_sys->track[p_sys->i_tracks - 1];
  426.                 memset( current_tk, 0, sizeof( vobsub_track_t ) );
  427.                 current_tk->i_current_subtitle = 0;
  428.                 current_tk->i_subtitles = 0;
  429.                 current_tk->p_subtitles = (subtitle_t*)malloc( sizeof( subtitle_t ) );;
  430.                 current_tk->i_track_id = i_track_id;
  431.                 es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','p','u',' ' ) );
  432.                 fmt.subs.spu.i_original_frame_width = p_sys->i_original_frame_width;
  433.                 fmt.subs.spu.i_original_frame_height = p_sys->i_original_frame_height;
  434.                 fmt.psz_language = strdup( language );
  435.                 current_tk->p_es = es_out_Add( p_demux->out, &fmt );
  436.                 msg_Dbg( p_demux, "new vobsub track detected" );
  437.             }
  438.             else
  439.             {
  440.                 msg_Warn( p_demux, "reading new track failed" );
  441.             }
  442.         }
  443.         else if( !strncmp( line, "timestamp:", 10 ) )
  444.         {
  445.             /*
  446.              * timestamp: hh:mm:ss:mss, filepos: loc
  447.              * loc is the hex location of the spu in the .sub file
  448.              *
  449.              */
  450.             int h, m, s, ms, loc;
  451.             int64_t i_start, i_location = 0;
  452.             
  453.             vobsub_track_t *current_tk = &p_sys->track[p_sys->i_tracks - 1];
  454.             if( sscanf( line, "timestamp: %d:%d:%d:%d, filepos: %x",
  455.                         &h, &m, &s, &ms, &loc ) == 5 )
  456.             {
  457.                 subtitle_t *current_sub;
  458.                 
  459.                 i_start = (int64_t) ( h * 3600*1000 +
  460.                             m * 60*1000 +
  461.                             s * 1000 +
  462.                             ms ) * 1000;
  463.                 i_location = loc;
  464.                 
  465.                 current_tk->i_subtitles++;
  466.                 current_tk->p_subtitles = (subtitle_t*)realloc( current_tk->p_subtitles, sizeof( subtitle_t ) * (current_tk->i_subtitles + 1 ) );
  467.                 current_sub = &current_tk->p_subtitles[current_tk->i_subtitles - 1];
  468.                 
  469.                 current_sub->i_start = (int64_t) i_start;
  470.                 current_sub->i_vobsub_location = i_location;
  471.             }
  472.         }
  473.     }
  474.     return( 0 );
  475. }
  476. static int DemuxVobSub( demux_t *p_demux, block_t *p_bk )
  477. {
  478.     demux_sys_t *p_sys = p_demux->p_sys;
  479.     uint8_t     *p = p_bk->p_buffer;
  480.     uint8_t     *p_end = &p_bk->p_buffer[p_bk->i_buffer];
  481.     int i;
  482.     while( p < p_end )
  483.     {
  484.         int i_size = ps_pkt_size( p, p_end - p );
  485.         block_t *p_pkt;
  486.         int      i_id;
  487.         int      i_spu;
  488.         if( i_size <= 0 )
  489.         {
  490.             break;
  491.         }
  492.         if( p[0] != 0 || p[1] != 0 || p[2] != 0x01 )
  493.         {
  494.             msg_Warn( p_demux, "invalid PES" );
  495.             break;
  496.         }
  497.         if( p[3] != 0xbd )
  498.         {
  499.             /* msg_Dbg( p_demux, "we don't need these ps packets (id=0x1%2.2x)", p[3] ); */
  500.             p += i_size;
  501.             continue;
  502.         }
  503.         /* Create a block */
  504.         p_pkt = block_New( p_demux, i_size );
  505.         memcpy( p_pkt->p_buffer, p, i_size);
  506.         p += i_size;
  507.         i_id = ps_pkt_id( p_pkt );
  508.         if( (i_id&0xffe0) != 0xbd20 ||
  509.             ps_pkt_parse_pes( p_pkt, 1 ) )
  510.         {
  511.             block_Release( p_pkt );
  512.             continue;
  513.         }
  514.         i_spu = i_id&0x1f;
  515.         /* msg_Dbg( p_demux, "SPU track %d size %d", i_spu, i_size ); */
  516.         for( i = 0; i < p_sys->i_tracks; i++ )
  517.         {
  518. #define tk p_sys->track[i]
  519.             p_pkt->i_dts = p_pkt->i_pts = p_bk->i_pts;
  520.             p_pkt->i_length = 0;
  521.             
  522.             if( tk.p_es && tk.i_track_id == i_spu )
  523.             {
  524.                 es_out_Send( p_demux->out, tk.p_es, p_pkt );
  525.                 p_bk->i_pts = 0;     /*only first packet has a pts */
  526.                 break;
  527.             }
  528.             else if( i == p_sys->i_tracks - 1 )
  529.             {
  530.                 block_Release( p_pkt );
  531.             }
  532. #undef tk
  533.         }
  534.     }
  535.     return VLC_SUCCESS;
  536. }