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

多媒体

开发平台:

MultiPlatform

  1. /*****************************************************************************
  2.  * subsdec.c : text subtitles decoder
  3.  *****************************************************************************
  4.  * Copyright (C) 2000-2001 VideoLAN
  5.  * $Id: subsdec.c 8659 2004-09-07 21:16:49Z gbazin $
  6.  *
  7.  * Authors: Gildas Bazin <gbazin@videolan.org>
  8.  *          Samuel Hocevar <sam@zoy.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 <vlc/vlc.h>
  28. #include <vlc/vout.h>
  29. #include <vlc/decoder.h>
  30. #include "osd.h"
  31. #include "vlc_filter.h"
  32. #include "charset.h"
  33. /*****************************************************************************
  34.  * decoder_sys_t : decoder descriptor
  35.  *****************************************************************************/
  36. struct decoder_sys_t
  37. {
  38.     int                 i_align;          /* Subtitles alignment on the vout */
  39.     vlc_iconv_t         iconv_handle;            /* handle to iconv instance */
  40. };
  41. /*****************************************************************************
  42.  * Local prototypes
  43.  *****************************************************************************/
  44. static int  OpenDecoder   ( vlc_object_t * );
  45. static void CloseDecoder  ( vlc_object_t * );
  46. static subpicture_t *DecodeBlock   ( decoder_t *, block_t ** );
  47. static subpicture_t *ParseText     ( decoder_t *, block_t * );
  48. static void         StripTags      ( char * );
  49. #define DEFAULT_NAME "System Default"
  50. /*****************************************************************************
  51.  * Module descriptor.
  52.  *****************************************************************************/
  53. static char *ppsz_encodings[] = { DEFAULT_NAME, "ASCII", "UTF-8", "",
  54.     "ISO-8859-1", "CP1252", "MacRoman", "MacIceland","ISO-8859-15", "",
  55.     "ISO-8859-2", "CP1250", "MacCentralEurope", "MacCroatian", "MacRomania", "",
  56.     "ISO-8859-5", "CP1251", "MacCyrillic", "MacUkraine", "KOI8-R", "KOI8-U", "KOI8-RU", "",
  57.     "ISO-8859-6", "CP1256", "MacArabic", "",
  58.     "ISO-8859-7", "CP1253", "MacGreek", "",
  59.     "ISO-8859-8", "CP1255", "MacHebrew", "",
  60.     "ISO-8859-9", "CP1254", "MacTurkish", "",
  61.     "ISO-8859-13", "CP1257", "",
  62.     "ISO-2022-JP", "ISO-2022-JP-1", "ISO-2022-JP-2", "EUC-JP", "SHIFT_JIS", "",
  63.     "ISO-2022-CN", "ISO-2022-CN-EXT", "EUC-CN", "EUC-TW", "BIG5", "BIG5-HKSCS", "",
  64.     "ISO-2022-KR", "EUC-KR", "",
  65.     "MacThai", "KOI8-T", "",
  66.     "ISO-8859-3", "ISO-8859-4", "ISO-8859-10", "ISO-8859-14", "ISO-8859-16", "",
  67.     "CP850", "CP862", "CP866", "CP874", "CP932", "CP949", "CP950", "CP1133", "CP1258", "",
  68.     "Macintosh", "",
  69.     "UTF-7", "UTF-16", "UTF-16BE", "UTF-16LE", "UTF-32", "UTF-32BE", "UTF-32LE",
  70.     "C99", "JAVA", "UCS-2", "UCS-2BE", "UCS-2LE", "UCS-4", "UCS-4BE", "UCS-4LE", "",
  71.     "HZ", "GBK", "GB18030", "JOHAB", "ARMSCII-8",
  72.     "Georgian-Academy", "Georgian-PS", "TIS-620", "MuleLao-1", "VISCII", "TCVN",
  73.     "HPROMAN8", "NEXTSTEP" };
  74. static int  pi_justification[] = { 0, 1, 2 };
  75. static char *ppsz_justification_text[] = {N_("Center"),N_("Left"),N_("Right")};
  76. #define ENCODING_TEXT N_("Subtitles text encoding")
  77. #define ENCODING_LONGTEXT N_("Set the encoding used in text subtitles")
  78. #define ALIGN_TEXT N_("Subtitles justification")
  79. #define ALIGN_LONGTEXT N_("Set the justification of subtitles")
  80. vlc_module_begin();
  81.     set_description( _("text subtitles decoder") );
  82.     set_capability( "decoder", 50 );
  83.     set_callbacks( OpenDecoder, CloseDecoder );
  84.     add_integer( "subsdec-align", 0, NULL, ALIGN_TEXT, ALIGN_LONGTEXT,
  85.                  VLC_TRUE );
  86.         change_integer_list( pi_justification, ppsz_justification_text, 0 );
  87.     add_string( "subsdec-encoding", DEFAULT_NAME, NULL,
  88.                 ENCODING_TEXT, ENCODING_LONGTEXT, VLC_FALSE );
  89.         change_string_list( ppsz_encodings, 0, 0 );
  90. vlc_module_end();
  91. /*****************************************************************************
  92.  * OpenDecoder: probe the decoder and return score
  93.  *****************************************************************************
  94.  * Tries to launch a decoder and return score so that the interface is able
  95.  * to chose.
  96.  *****************************************************************************/
  97. static int OpenDecoder( vlc_object_t *p_this )
  98. {
  99.     decoder_t     *p_dec = (decoder_t*)p_this;
  100.     decoder_sys_t *p_sys;
  101.     vlc_value_t val;
  102.     if( p_dec->fmt_in.i_codec != VLC_FOURCC('s','u','b','t') &&
  103.         p_dec->fmt_in.i_codec != VLC_FOURCC('s','s','a',' ') )
  104.     {
  105.         return VLC_EGENERIC;
  106.     }
  107.     p_dec->pf_decode_sub = DecodeBlock;
  108.     /* Allocate the memory needed to store the decoder's structure */
  109.     if( ( p_dec->p_sys = p_sys =
  110.           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
  111.     {
  112.         msg_Err( p_dec, "out of memory" );
  113.         return VLC_EGENERIC;
  114.     }
  115.     var_Create( p_dec, "subsdec-align", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
  116.     var_Get( p_dec, "subsdec-align", &val );
  117.     p_sys->i_align = val.i_int;
  118.     if( p_dec->fmt_in.subs.psz_encoding && *p_dec->fmt_in.subs.psz_encoding )
  119.     {
  120.         msg_Dbg( p_dec, "using character encoding: %s",
  121.                  p_dec->fmt_in.subs.psz_encoding );
  122.         p_sys->iconv_handle =
  123.             vlc_iconv_open( "UTF-8", p_dec->fmt_in.subs.psz_encoding );
  124.     }
  125.     else
  126.     {
  127.         var_Create( p_dec, "subsdec-encoding",
  128.                     VLC_VAR_STRING | VLC_VAR_DOINHERIT );
  129.         var_Get( p_dec, "subsdec-encoding", &val );
  130.         if( !strcmp( val.psz_string, DEFAULT_NAME ) )
  131.         {
  132.             char *psz_charset =(char*)malloc( 100 );
  133.             vlc_current_charset( &psz_charset );
  134.             p_sys->iconv_handle = vlc_iconv_open( "UTF-8", psz_charset );
  135.             msg_Dbg( p_dec, "using character encoding: %s", psz_charset );
  136.             free( psz_charset );
  137.         }
  138.         else if( val.psz_string )
  139.         {
  140.             msg_Dbg( p_dec, "using character encoding: %s", val.psz_string );
  141.             p_sys->iconv_handle = vlc_iconv_open( "UTF-8", val.psz_string );
  142.         }
  143.         if( p_sys->iconv_handle == (vlc_iconv_t)-1 )
  144.         {
  145.             msg_Warn( p_dec, "unable to do requested conversion" );
  146.         }
  147.         if( val.psz_string ) free( val.psz_string );
  148.     }
  149.     return VLC_SUCCESS;
  150. }
  151. /****************************************************************************
  152.  * DecodeBlock: the whole thing
  153.  ****************************************************************************
  154.  * This function must be fed with complete subtitles units.
  155.  ****************************************************************************/
  156. static subpicture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
  157. {
  158.     subpicture_t *p_spu;
  159.     if( !pp_block || *pp_block == NULL ) return NULL;
  160.     p_spu = ParseText( p_dec, *pp_block );
  161.     block_Release( *pp_block );
  162.     *pp_block = NULL;
  163.     return p_spu;
  164. }
  165. /*****************************************************************************
  166.  * CloseDecoder: clean up the decoder
  167.  *****************************************************************************/
  168. static void CloseDecoder( vlc_object_t *p_this )
  169. {
  170.     decoder_t *p_dec = (decoder_t *)p_this;
  171.     decoder_sys_t *p_sys = p_dec->p_sys;
  172.     if( p_sys->iconv_handle != (vlc_iconv_t)-1 )
  173.     {
  174.         vlc_iconv_close( p_sys->iconv_handle );
  175.     }
  176.     free( p_sys );
  177. }
  178. /*****************************************************************************
  179.  * ParseText: parse an text subtitle packet and send it to the video output
  180.  *****************************************************************************/
  181. static subpicture_t *ParseText( decoder_t *p_dec, block_t *p_block )
  182. {
  183.     decoder_sys_t *p_sys = p_dec->p_sys;
  184.     subpicture_t *p_spu = 0;
  185.     char *psz_subtitle;
  186.     int i_align_h, i_align_v;
  187.     video_format_t fmt;
  188.     /* We cannot display a subpicture with no date */
  189.     if( p_block->i_pts == 0 )
  190.     {
  191.         msg_Warn( p_dec, "subtitle without a date" );
  192.         return NULL;
  193.     }
  194.     /* Check validity of packet data */
  195.     if( p_block->i_buffer <= 1 || p_block->p_buffer[0] == '' )
  196.     {
  197.         msg_Warn( p_dec, "empty subtitle" );
  198.         return NULL;
  199.     }
  200.     /* Should be resiliant against bad subtitles */
  201.     psz_subtitle = strndup( p_block->p_buffer, p_block->i_buffer );
  202.     i_align_h = p_sys->i_align ? 20 : 0;
  203.     i_align_v = 10;
  204.     if( p_sys->iconv_handle != (vlc_iconv_t)-1 )
  205.     {
  206.         char *psz_new_subtitle;
  207.         char *psz_convert_buffer_out;
  208.         char *psz_convert_buffer_in;
  209.         size_t ret, inbytes_left, outbytes_left;
  210.         psz_new_subtitle = malloc( 6 * strlen( psz_subtitle ) );
  211.         psz_convert_buffer_out = psz_new_subtitle;
  212.         psz_convert_buffer_in = psz_subtitle;
  213.         inbytes_left = strlen( psz_subtitle );
  214.         outbytes_left = 6 * inbytes_left;
  215.         ret = vlc_iconv( p_sys->iconv_handle, &psz_convert_buffer_in,
  216.                          &inbytes_left, &psz_convert_buffer_out,
  217.                          &outbytes_left );
  218.         *psz_convert_buffer_out = '';
  219.         if( inbytes_left )
  220.         {
  221.             msg_Warn( p_dec, "Failed to convert subtitle encoding, "
  222.                       "dropping subtitle.nTry setting a different "
  223.                       "character-encoding for the subtitle." );
  224.             free( psz_subtitle );
  225.             return NULL;
  226.         }
  227.         else
  228.         {
  229.             free( psz_subtitle );
  230.             psz_subtitle = psz_new_subtitle;
  231.         }
  232.     }
  233.     if( p_dec->fmt_in.i_codec == VLC_FOURCC('s','s','a',' ') )
  234.     {
  235.         /* Decode SSA strings */
  236.         /* We expect: ReadOrder, Layer, Style, Name, MarginL, MarginR,
  237.          * MarginV, Effect, Text */
  238.         char *psz_new_subtitle;
  239.         char *psz_buffer_sub;
  240.         int         i_comma;
  241.         int         i_text;
  242.         psz_buffer_sub = psz_subtitle;
  243.         for( ;; )
  244.         {
  245.             i_comma = 0;
  246.             while( i_comma < 8 &&
  247.                 *psz_buffer_sub != '' )
  248.             {
  249.                 if( *psz_buffer_sub == ',' )
  250.                 {
  251.                     i_comma++;
  252.                 }
  253.                 psz_buffer_sub++;
  254.             }
  255.             psz_new_subtitle = malloc( strlen( psz_buffer_sub ) + 1);
  256.             i_text = 0;
  257.             while( psz_buffer_sub[0] != '' )
  258.             {
  259.                 if( psz_buffer_sub[0] == '\' && ( psz_buffer_sub[1] == 'n' ||
  260.                     psz_buffer_sub[1] == 'N' ) )
  261.                 {
  262.                     psz_new_subtitle[i_text] = 'n';
  263.                     i_text++;
  264.                     psz_buffer_sub += 2;
  265.                 }
  266.                 else if( psz_buffer_sub[0] == '{' &&
  267.                          psz_buffer_sub[1] == '\' )
  268.                 {
  269.                     /* SSA control code */
  270.                     while( psz_buffer_sub[0] != '' &&
  271.                            psz_buffer_sub[0] != '}' )
  272.                     {
  273.                         psz_buffer_sub++;
  274.                     }
  275.                     psz_buffer_sub++;
  276.                 }
  277.                 else
  278.                 {
  279.                     psz_new_subtitle[i_text] = psz_buffer_sub[0];
  280.                     i_text++;
  281.                     psz_buffer_sub++;
  282.                 }
  283.             }
  284.             psz_new_subtitle[i_text] = '';
  285.             free( psz_subtitle );
  286.             psz_subtitle = psz_new_subtitle;
  287.             break;
  288.         }
  289.     }
  290.     StripTags( psz_subtitle );
  291.     p_spu = p_dec->pf_spu_buffer_new( p_dec );
  292.     if( !p_spu )
  293.     {
  294.         msg_Warn( p_dec, "can't get spu buffer" );
  295.         free( psz_subtitle );
  296.         return 0;
  297.     }
  298.     /* Create a new subpicture region */
  299.     memset( &fmt, 0, sizeof(video_format_t) );
  300.     fmt.i_chroma = VLC_FOURCC('T','E','X','T');
  301.     fmt.i_aspect = 0;
  302.     fmt.i_width = fmt.i_height = 0;
  303.     fmt.i_x_offset = fmt.i_y_offset = 0;
  304.     p_spu->p_region = p_spu->pf_create_region( VLC_OBJECT(p_dec), &fmt );
  305.     if( !p_spu->p_region )
  306.     {
  307.         msg_Err( p_dec, "cannot allocate SPU region" );
  308.         free( psz_subtitle );
  309.         p_dec->pf_spu_buffer_del( p_dec, p_spu );
  310.         return 0;
  311.     }
  312.     p_spu->p_region->psz_text = psz_subtitle;
  313.     p_spu->i_start = p_block->i_pts;
  314.     p_spu->i_stop = p_block->i_pts + p_block->i_length;
  315.     p_spu->b_ephemer = (p_block->i_length == 0);
  316.     p_spu->b_absolute = VLC_FALSE;
  317.     p_spu->i_flags = OSD_ALIGN_BOTTOM | p_sys->i_align;
  318.     p_spu->i_x = i_align_h;
  319.     p_spu->i_y = i_align_v;
  320.     return p_spu;
  321. }
  322. static void StripTags( char *psz_text )
  323. {
  324.     int i_left_moves = 0;
  325.     vlc_bool_t b_inside_tag = VLC_FALSE;
  326.     int i = 0;
  327.     int i_tag_start = -1;
  328.     while( psz_text[ i ] )
  329.     {
  330.         if( !b_inside_tag )
  331.         {
  332.             if( psz_text[ i ] == '<' )
  333.             {
  334.                 b_inside_tag = VLC_TRUE;
  335.                 i_tag_start = i;
  336.             }
  337.             psz_text[ i - i_left_moves ] = psz_text[ i ];
  338.         }
  339.         else
  340.         {
  341.             if( ( psz_text[ i ] == ' ' ) ||
  342.                 ( psz_text[ i ] == 't' ) ||
  343.                 ( psz_text[ i ] == 'n' ) ||
  344.                 ( psz_text[ i ] == 'r' ) )
  345.             {
  346.                 b_inside_tag = VLC_FALSE;
  347.                 i_tag_start = -1;
  348.             }
  349.             else if( psz_text[ i ] == '>' )
  350.             {
  351.                 i_left_moves += i - i_tag_start + 1;
  352.                 i_tag_start = -1;
  353.                 b_inside_tag = VLC_FALSE;
  354.             }
  355.             else
  356.             {
  357.                 psz_text[ i - i_left_moves ] = psz_text[ i ];
  358.             }
  359.         }
  360.         i++;
  361.     }
  362.     psz_text[ i - i_left_moves ] = '';
  363. }