📄 libass.c
字号:
/***************************************************************************** * SSA/ASS subtitle decoder using libass. ***************************************************************************** * Copyright (C) 2008 the VideoLAN team * $Id: 02d58a37e2e2b921b56c213484302bde2f458284 $ * * Authors: Laurent Aimar <fenrir@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. *****************************************************************************//***************************************************************************** * Preamble *****************************************************************************/#ifdef HAVE_CONFIG_H# include "config.h"#endif#include <string.h>#include <limits.h>#include <assert.h>#include <math.h>#include <vlc_common.h>#include <vlc_plugin.h>#include <vlc_vout.h>#include <vlc_codec.h>#include <vlc_osd.h>#include <vlc_input.h>#include <ass/ass.h>/***************************************************************************** * Module descriptor *****************************************************************************/static int Create ( vlc_object_t * );static void Destroy( vlc_object_t * );vlc_module_begin(); set_shortname( N_("Subtitles (advanced)")); set_description( N_("Subtitle renderers using libass") ); set_capability( "decoder", 100 ); set_category( CAT_INPUT ); set_subcategory( SUBCAT_INPUT_SCODEC ); set_callbacks( Create, Destroy );vlc_module_end();/***************************************************************************** * Local prototypes *****************************************************************************/static subpicture_t *DecodeBlock( decoder_t *, block_t ** );static void DestroySubpicture( subpicture_t * );static void PreRender( video_format_t *, spu_t *, subpicture_t * );static void UpdateRegions( video_format_t *, spu_t *, subpicture_t *, mtime_t );/* Yes libass sux with threads */typedef struct { vlc_object_t *p_libvlc; vlc_mutex_t *p_lock; int i_refcount; ass_library_t *p_library; ass_renderer_t *p_renderer; video_format_t fmt;} ass_handle_t;static ass_handle_t *AssHandleYield( decoder_t *p_dec );static void AssHandleRelease( ass_handle_t * );/* */struct decoder_sys_t{ /* decoder_sys_t is shared between decoder and spu units */ vlc_mutex_t lock; int i_refcount; /* */ ass_handle_t *p_ass; /* */ ass_track_t *p_track; /* */ subpicture_t *p_spu_final;};static void DecSysRelease( decoder_sys_t *p_sys );static void DecSysYield( decoder_sys_t *p_sys );struct subpicture_sys_t{ decoder_sys_t *p_dec_sys; void *p_subs_data; int i_subs_len;};typedef struct{ int x0; int y0; int x1; int y1;} rectangle_t;static int BuildRegions( spu_t *p_spu, rectangle_t *p_region, int i_max_region, ass_image_t *p_img_list, int i_width, int i_height );static void SubpictureReleaseRegions( spu_t *p_spu, subpicture_t *p_subpic );static void RegionDraw( subpicture_region_t *p_region, ass_image_t *p_img );//#define DEBUG_REGION/***************************************************************************** * Create: Open libass decoder. *****************************************************************************/static int Create( vlc_object_t *p_this ){ decoder_t *p_dec = (decoder_t *)p_this; decoder_sys_t *p_sys; ass_track_t *p_track; if( p_dec->fmt_in.i_codec != VLC_FOURCC('s','s','a',' ') ) return VLC_EGENERIC; p_dec->pf_decode_sub = DecodeBlock; p_dec->p_sys = p_sys = malloc( sizeof( decoder_sys_t ) ); if( !p_sys ) return VLC_ENOMEM; /* */ p_sys->p_ass = AssHandleYield( p_dec ); if( !p_sys->p_ass ) { free( p_sys ); return VLC_EGENERIC; } vlc_mutex_init( &p_sys->lock ); p_sys->i_refcount = 1; /* Add a track */ vlc_mutex_lock( p_sys->p_ass->p_lock ); p_sys->p_track = p_track = ass_new_track( p_sys->p_ass->p_library ); if( !p_track ) { vlc_mutex_unlock( p_sys->p_ass->p_lock ); DecSysRelease( p_sys ); return VLC_EGENERIC; } ass_process_codec_private( p_track, p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra ); vlc_mutex_unlock( p_sys->p_ass->p_lock ); return VLC_SUCCESS;}/***************************************************************************** * Destroy: finish *****************************************************************************/static void Destroy( vlc_object_t *p_this ){ decoder_t *p_dec = (decoder_t *)p_this; DecSysRelease( p_dec->p_sys );}static void DecSysYield( decoder_sys_t *p_sys ){ vlc_mutex_lock( &p_sys->lock ); p_sys->i_refcount++; vlc_mutex_unlock( &p_sys->lock );}static void DecSysRelease( decoder_sys_t *p_sys ){ /* */ vlc_mutex_lock( &p_sys->lock ); p_sys->i_refcount--; if( p_sys->i_refcount > 0 ) { vlc_mutex_unlock( &p_sys->lock ); return; } vlc_mutex_unlock( &p_sys->lock ); vlc_mutex_destroy( &p_sys->lock ); vlc_mutex_lock( p_sys->p_ass->p_lock ); if( p_sys->p_track ) ass_free_track( p_sys->p_track ); vlc_mutex_unlock( p_sys->p_ass->p_lock ); AssHandleRelease( p_sys->p_ass ); free( p_sys );}/**************************************************************************** * DecodeBlock: ****************************************************************************/static subpicture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block ){ decoder_sys_t *p_sys = p_dec->p_sys; subpicture_t *p_spu = NULL; block_t *p_block; if( !pp_block || *pp_block == NULL ) return NULL; p_block = *pp_block; if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) ) { msg_Dbg( p_dec, "Resetting libass track after time discontinuity" ); /* We need to reset our tracks for the time discontinuity to be * handled */ vlc_mutex_lock( p_sys->p_ass->p_lock ); if( p_sys->p_track ) ass_free_track( p_sys->p_track ); p_sys->p_track = ass_new_track( p_sys->p_ass->p_library ); if( p_sys->p_track ) ass_process_codec_private( p_sys->p_track, p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra ); vlc_mutex_unlock( p_sys->p_ass->p_lock ); block_Release( p_block ); return NULL; } if( p_block->i_rate != 0 ) p_block->i_length = p_block->i_length * p_block->i_rate / INPUT_RATE_DEFAULT; *pp_block = NULL; if( p_block->i_buffer == 0 || p_block->p_buffer[0] == '\0' ) { block_Release( p_block ); return NULL; } p_spu = p_dec->pf_spu_buffer_new( p_dec ); if( !p_spu ) { msg_Warn( p_dec, "can't get spu buffer" ); block_Release( p_block ); return NULL; } p_spu->p_sys = malloc( sizeof( subpicture_sys_t )); if( !p_spu->p_sys ) { p_dec->pf_spu_buffer_del( p_dec, p_spu ); block_Release( p_block ); return NULL; } p_spu->p_sys->i_subs_len = p_block->i_buffer; p_spu->p_sys->p_subs_data = malloc( p_block->i_buffer ); if( !p_spu->p_sys->p_subs_data ) { free( p_spu->p_sys ); p_dec->pf_spu_buffer_del( p_dec, p_spu ); block_Release( p_block ); return NULL; } memcpy( p_spu->p_sys->p_subs_data, p_block->p_buffer, p_block->i_buffer ); p_spu->i_x = 0; p_spu->i_y = 0; p_spu->i_start = p_block->i_pts; p_spu->i_stop = p_block->i_pts + p_block->i_length; p_spu->b_ephemer = true; p_spu->b_absolute = true; p_spu->b_pausable = true; /* ? */ vlc_mutex_lock( p_sys->p_ass->p_lock ); if( p_sys->p_track ) { ass_process_chunk( p_sys->p_track, p_spu->p_sys->p_subs_data, p_spu->p_sys->i_subs_len, p_spu->i_start / 1000, (p_spu->i_stop-p_spu->i_start) / 1000 ); } vlc_mutex_unlock( p_sys->p_ass->p_lock ); p_spu->pf_pre_render = PreRender; p_spu->pf_update_regions = UpdateRegions; p_spu->pf_destroy = DestroySubpicture; p_spu->p_sys->p_dec_sys = p_sys; DecSysYield( p_sys ); block_Release( p_block ); return p_spu;}/**************************************************************************** * ****************************************************************************/static void DestroySubpicture( subpicture_t *p_subpic ){ DecSysRelease( p_subpic->p_sys->p_dec_sys ); free( p_subpic->p_sys->p_subs_data ); free( p_subpic->p_sys );}static void PreRender( video_format_t *p_fmt, spu_t *p_spu, subpicture_t *p_subpic ){ decoder_sys_t *p_dec_sys = p_subpic->p_sys->p_dec_sys; p_dec_sys->p_spu_final = p_subpic; VLC_UNUSED(p_fmt); VLC_UNUSED(p_spu);}static void UpdateRegions( video_format_t *p_fmt, spu_t *p_spu, subpicture_t *p_subpic, mtime_t i_ts ){ decoder_sys_t *p_sys = p_subpic->p_sys->p_dec_sys; ass_handle_t *p_ass = p_sys->p_ass; video_format_t fmt; bool b_fmt_changed; if( p_subpic != p_sys->p_spu_final ) { SubpictureReleaseRegions( p_spu, p_subpic ); return; } vlc_mutex_lock( p_ass->p_lock ); /* */ fmt = *p_fmt; fmt.i_chroma = VLC_FOURCC('R','G','B','A'); fmt.i_width = fmt.i_visible_width; fmt.i_height = fmt.i_visible_height; fmt.i_bits_per_pixel = 0; fmt.i_x_offset = fmt.i_y_offset = 0; fmt.i_sar_num = 1; fmt.i_sar_den = 1; b_fmt_changed = memcmp( &fmt, &p_ass->fmt, sizeof(fmt) ) != 0; if( b_fmt_changed ) { ass_set_frame_size( p_ass->p_renderer, fmt.i_width, fmt.i_height ); ass_set_aspect_ratio( p_ass->p_renderer, 1.0 ); // TODO ? p_ass->fmt = fmt; } /* */ int i_changed; ass_image_t *p_img = ass_render_frame( p_ass->p_renderer, p_sys->p_track, i_ts/1000, &i_changed ); if( !i_changed && !b_fmt_changed ) { vlc_mutex_unlock( p_ass->p_lock ); return; } /* */ p_subpic->i_original_picture_height = fmt.i_height; p_subpic->i_original_picture_width = fmt.i_width; SubpictureReleaseRegions( p_spu, p_subpic ); /* XXX to improve efficiency we merge regions that are close minimizing * the lost surface. * libass tends to create a lot of small regions and thus spu engine * reinstanciate a lot the scaler, and as we do not support subpel blending * it looks ugly (text unaligned). */ const int i_max_region = 4; rectangle_t region[i_max_region]; const int i_region = BuildRegions( p_spu, region, i_max_region, p_img, fmt.i_width, fmt.i_height ); if( i_region <= 0 ) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -