subsusf.c
资源名称:vlc-1.0.5.zip [点击查看]
上传用户:kjfoods
上传日期:2020-07-06
资源大小:29949k
文件大小:45k
源码类别:
midi
开发平台:
Unix_Linux
- /*****************************************************************************
- * subsusf.c : USF subtitles decoder
- *****************************************************************************
- * Copyright (C) 2000-2006 the VideoLAN team
- * $Id: 330e167ab3c10f21b9cb53c7e77628ee90370e81 $
- *
- * Authors: Bernie Purcell <bitmap@videolan.org>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
- *****************************************************************************/
- #ifdef HAVE_CONFIG_H
- # include "config.h"
- #endif
- #include "subsdec.h"
- #include <vlc_plugin.h>
- #include <assert.h>
- /*****************************************************************************
- * Local prototypes
- *****************************************************************************/
- static int OpenDecoder ( vlc_object_t * );
- static void CloseDecoder ( vlc_object_t * );
- static subpicture_t *DecodeBlock ( decoder_t *, block_t ** );
- static char *CreatePlainText( char * );
- static int ParseImageAttachments( decoder_t *p_dec );
- static subpicture_t *ParseText ( decoder_t *, block_t * );
- static void ParseUSFHeader( decoder_t * );
- static subpicture_region_t *ParseUSFString( decoder_t *, char *, subpicture_t * );
- static subpicture_region_t *LoadEmbeddedImage( decoder_t *p_dec, subpicture_t *p_spu, const char *psz_filename, int i_transparent_color );
- /*****************************************************************************
- * Module descriptor.
- *****************************************************************************/
- vlc_module_begin ()
- set_capability( "decoder", 40 )
- set_shortname( N_("USFSubs"))
- set_description( N_("USF subtitles decoder") )
- set_callbacks( OpenDecoder, CloseDecoder )
- set_category( CAT_INPUT )
- set_subcategory( SUBCAT_INPUT_SCODEC )
- /* We inherit subsdec-align and subsdec-formatted from subsdec.c */
- vlc_module_end ()
- /*****************************************************************************
- * OpenDecoder: probe the decoder and return score
- *****************************************************************************
- * Tries to launch a decoder and return score so that the interface is able
- * to chose.
- *****************************************************************************/
- static int OpenDecoder( vlc_object_t *p_this )
- {
- decoder_t *p_dec = (decoder_t*)p_this;
- decoder_sys_t *p_sys;
- if( p_dec->fmt_in.i_codec != VLC_FOURCC('u','s','f',' ') )
- return VLC_EGENERIC;
- /* Allocate the memory needed to store the decoder's structure */
- if( ( p_dec->p_sys = p_sys = calloc(1, sizeof(decoder_sys_t)) ) == NULL )
- return VLC_ENOMEM;
- p_dec->pf_decode_sub = DecodeBlock;
- p_dec->fmt_out.i_cat = SPU_ES;
- p_dec->fmt_out.i_codec = 0;
- /* Unused fields of p_sys - not needed for USF decoding */
- p_sys->b_ass = false;
- p_sys->iconv_handle = (vlc_iconv_t)-1;
- p_sys->b_autodetect_utf8 = false;
- /* init of p_sys */
- p_sys->i_align = 0;
- p_sys->i_original_height = 0;
- p_sys->i_original_width = 0;
- TAB_INIT( p_sys->i_ssa_styles, p_sys->pp_ssa_styles );
- TAB_INIT( p_sys->i_images, p_sys->pp_images );
- /* USF subtitles are mandated to be UTF-8, so don't need vlc_iconv */
- p_sys->i_align = var_CreateGetInteger( p_dec, "subsdec-align" );
- ParseImageAttachments( p_dec );
- if( var_CreateGetBool( p_dec, "subsdec-formatted" ) )
- {
- if( p_dec->fmt_in.i_extra > 0 )
- ParseUSFHeader( p_dec );
- }
- return VLC_SUCCESS;
- }
- /****************************************************************************
- * DecodeBlock: the whole thing
- ****************************************************************************
- * This function must be fed with complete subtitles units.
- ****************************************************************************/
- static subpicture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
- {
- subpicture_t *p_spu;
- block_t *p_block;
- if( !pp_block || *pp_block == NULL )
- return NULL;
- p_block = *pp_block;
- p_spu = ParseText( p_dec, p_block );
- block_Release( p_block );
- *pp_block = NULL;
- return p_spu;
- }
- /*****************************************************************************
- * CloseDecoder: clean up the decoder
- *****************************************************************************/
- static void CloseDecoder( vlc_object_t *p_this )
- {
- decoder_t *p_dec = (decoder_t *)p_this;
- decoder_sys_t *p_sys = p_dec->p_sys;
- if( p_sys->pp_ssa_styles )
- {
- int i;
- for( i = 0; i < p_sys->i_ssa_styles; i++ )
- {
- if( !p_sys->pp_ssa_styles[i] )
- continue;
- free( p_sys->pp_ssa_styles[i]->psz_stylename );
- free( p_sys->pp_ssa_styles[i]->font_style.psz_fontname );
- free( p_sys->pp_ssa_styles[i] );
- }
- TAB_CLEAN( p_sys->i_ssa_styles, p_sys->pp_ssa_styles );
- }
- if( p_sys->pp_images )
- {
- int i;
- for( i = 0; i < p_sys->i_images; i++ )
- {
- if( !p_sys->pp_images[i] )
- continue;
- if( p_sys->pp_images[i]->p_pic )
- picture_Release( p_sys->pp_images[i]->p_pic );
- free( p_sys->pp_images[i]->psz_filename );
- free( p_sys->pp_images[i] );
- }
- TAB_CLEAN( p_sys->i_images, p_sys->pp_images );
- }
- free( p_sys );
- }
- /*****************************************************************************
- * ParseText: parse an text subtitle packet and send it to the video output
- *****************************************************************************/
- static subpicture_t *ParseText( decoder_t *p_dec, block_t *p_block )
- {
- decoder_sys_t *p_sys = p_dec->p_sys;
- subpicture_t *p_spu = NULL;
- char *psz_subtitle = NULL;
- /* We cannot display a subpicture with no date */
- if( p_block->i_pts == 0 )
- {
- msg_Warn( p_dec, "subtitle without a date" );
- return NULL;
- }
- /* Check validity of packet data */
- /* An "empty" line containing only