sub.c
来自「VLC媒体播放程序」· C语言 代码 · 共 1,224 行 · 第 1/3 页
C
1,224 行
{ msg_Dbg( p_input, "unknown subtitle file" ); text_unload( &txt ); return VLC_EGENERIC; } if( sub_read_subtitle_function[i].i_type == i_sub_type ) { msg_Dbg( p_input, "detected %s format", sub_read_subtitle_function[i].psz_name ); p_sub->i_sub_type = i_sub_type; pf_read_subtitle = sub_read_subtitle_function[i].pf_read_subtitle; break; } } for( i_max = 0;; ) { if( p_sub->i_subtitles >= i_max ) { i_max += 128; if( !( p_sub->subtitle = realloc( p_sub->subtitle, sizeof(subtitle_t) * i_max ) ) ) { msg_Err( p_sub, "out of memory"); return VLC_ENOMEM; } } if( pf_read_subtitle( p_sub, &txt, p_sub->subtitle + p_sub->i_subtitles, i_microsecperframe ) < 0 ) { break; } p_sub->i_subtitles++; } msg_Dbg( p_sub, "loaded %d subtitles", p_sub->i_subtitles ); /* *** Close the file *** */ text_unload( &txt ); /* *** fix subtitle (order and time) *** */ p_sub->i_subtitle = 0; /* will be modified by sub_fix */ if( p_sub->i_sub_type != SUB_TYPE_VOBSUB ) { sub_fix( p_sub ); } /* *** add subtitle ES *** */ if( p_sub->i_sub_type == SUB_TYPE_VOBSUB ) { int i_len = strlen( psz_name ); char *psz_vobname = strdup(psz_name); strcpy( psz_vobname + i_len - 4, ".sub" ); /* open file */ if( !( p_sub->p_vobsub_file = fopen( psz_vobname, "rb" ) ) ) { msg_Err( p_sub, "couldn't open .sub Vobsub file: %s", psz_vobname ); } free( psz_vobname ); es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','p','u',' ' ) ); } else if( p_sub->i_sub_type == SUB_TYPE_SSA1 || p_sub->i_sub_type == SUB_TYPE_SSA2_4 ) { es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','s','a',' ' ) ); } else { es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','u','b','t' ) ); } if( p_sub->psz_header != NULL ) { fmt.i_extra = strlen( p_sub->psz_header ) + 1; fmt.p_extra = strdup( p_sub->psz_header ); } p_sub->p_es = es_out_Add( p_input->p_es_out, &fmt ); p_sub->i_previously_selected = 0; return VLC_SUCCESS;}/***************************************************************************** * sub_demux: Send subtitle to decoder until i_maxdate *****************************************************************************/static int sub_demux( subtitle_demux_t *p_sub, mtime_t i_maxdate ){ input_thread_t *p_input = p_sub->p_input; vlc_bool_t b; es_out_Control( p_input->p_es_out, ES_OUT_GET_ES_STATE, p_sub->p_es, &b ); if( b && !p_sub->i_previously_selected ) { p_sub->i_previously_selected = 1; p_sub->pf_seek( p_sub, i_maxdate ); return VLC_SUCCESS; } else if( !b && p_sub->i_previously_selected ) { p_sub->i_previously_selected = 0; return VLC_SUCCESS; } if( p_sub->i_sub_type != SUB_TYPE_VOBSUB ) { while( p_sub->i_subtitle < p_sub->i_subtitles && p_sub->subtitle[p_sub->i_subtitle].i_start < i_maxdate ) { block_t *p_block; int i_len = strlen( p_sub->subtitle[p_sub->i_subtitle].psz_text ) + 1; if( i_len <= 1 ) { /* empty subtitle */ p_sub->i_subtitle++; continue; } if( ( p_block = block_New( p_sub->p_input, i_len ) ) == NULL ) { p_sub->i_subtitle++; continue; } /* XXX we should convert all demuxers to use es_out_Control to set pcr and * then remove that */ p_block->i_pts = input_ClockGetTS( p_sub->p_input, p_sub->p_input->stream.p_selected_program, p_sub->subtitle[p_sub->i_subtitle].i_start*9/100); p_block->i_dts = 0; if( p_sub->subtitle[p_sub->i_subtitle].i_stop > 0 ) { /* FIXME kludge i_dts means end of display... */ p_block->i_dts = input_ClockGetTS( p_sub->p_input, p_sub->p_input->stream.p_selected_program, p_sub->subtitle[p_sub->i_subtitle].i_stop *9/100); } memcpy( p_block->p_buffer, p_sub->subtitle[p_sub->i_subtitle].psz_text, i_len ); if( p_block->i_pts > 0 ) { es_out_Send( p_input->p_es_out, p_sub->p_es, p_block ); } else { block_Release( p_block ); } p_sub->i_subtitle++; } } else { while( p_sub->i_subtitle < p_sub->i_subtitles && p_sub->subtitle[p_sub->i_subtitle].i_start < i_maxdate ) { int i_pos = p_sub->subtitle[p_sub->i_subtitle].i_vobsub_location; block_t *p_block; int i_size = 0; /* first compute SPU size */ if( p_sub->i_subtitle + 1 < p_sub->i_subtitles ) { i_size = p_sub->subtitle[p_sub->i_subtitle+1].i_vobsub_location - i_pos; } if( i_size <= 0 ) i_size = 65535; /* Invalid or EOF */ /* Seek at the right place (could be avoid if sub_seek is fixed to do his job) */ if( fseek( p_sub->p_vobsub_file, i_pos, SEEK_SET ) ) { msg_Warn( p_sub, "cannot seek at right vobsub location %d", i_pos ); p_sub->i_subtitle++; continue; } /* allocate a packet */ if( ( p_block = block_New( p_sub, i_size ) ) == NULL ) { p_sub->i_subtitle++; continue; } /* read data */ p_block->i_buffer = fread( p_block->p_buffer, 1, i_size, p_sub->p_vobsub_file ); if( p_block->i_buffer <= 6 ) { block_Release( p_block ); p_sub->i_subtitle++; continue; } /* pts */ p_block->i_pts = input_ClockGetTS( p_sub->p_input, p_sub->p_input->stream.p_selected_program, p_sub->subtitle[p_sub->i_subtitle].i_start*9/100); /* demux this block */ DemuxVobSub( p_sub, p_block ); p_sub->i_subtitle++; } } return VLC_SUCCESS;}/***************************************************************************** * sub_seek: Seek to i_date *****************************************************************************/static int sub_seek ( subtitle_demux_t *p_sub, mtime_t i_date ){ /* should be fast enough... */ p_sub->i_subtitle = 0; while( p_sub->i_subtitle < p_sub->i_subtitles && p_sub->subtitle[p_sub->i_subtitle].i_start < i_date ) { p_sub->i_subtitle++; } return( 0 );}/***************************************************************************** * sub_close: Close subtitle demux *****************************************************************************/static void sub_close( subtitle_demux_t *p_sub ){ if( p_sub->subtitle ) { int i; for( i = 0; i < p_sub->i_subtitles; i++ ) { if( p_sub->subtitle[i].psz_text ) { free( p_sub->subtitle[i].psz_text ); } } free( p_sub->subtitle ); } if( p_sub->p_vobsub_file ) { fclose( p_sub->p_vobsub_file ); }}/***************************************************************************** * sub_fix: fix time stamp and order of subtitle *****************************************************************************/static void sub_fix( subtitle_demux_t *p_sub ){ int i; mtime_t i_delay; int i_index; int i_done; vlc_value_t val; /* *** fix order (to be sure...) *** */ /* We suppose that there are near in order and this durty bubble sort * wont take too much time */ do { i_done = 1; for( i_index = 1; i_index < p_sub->i_subtitles; i_index++ ) { if( p_sub->subtitle[i_index].i_start < p_sub->subtitle[i_index - 1].i_start ) { subtitle_t sub_xch; memcpy( &sub_xch, p_sub->subtitle + i_index - 1, sizeof( subtitle_t ) ); memcpy( p_sub->subtitle + i_index - 1, p_sub->subtitle + i_index, sizeof( subtitle_t ) ); memcpy( p_sub->subtitle + i_index, &sub_xch, sizeof( subtitle_t ) ); i_done = 0; } } } while( !i_done ); /* *** and at the end add delay *** */ var_Get( p_sub, "sub-delay", &val ); i_delay = (mtime_t) val.i_int * 100000; if( i_delay != 0 ) { for( i = 0; i < p_sub->i_subtitles; i++ ) { p_sub->subtitle[i].i_start += i_delay; p_sub->subtitle[i].i_stop += i_delay; if( p_sub->subtitle[i].i_start < 0 ) { p_sub->i_subtitle = i + 1; } } }}/***************************************************************************** * Specific Subtitle function *****************************************************************************/static int sub_MicroDvdRead( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe){ /* * each line: * {n1}{n2}Line1|Line2|Line3.... * where n1 and n2 are the video frame number... * */ char *s; char buffer_text[MAX_LINE + 1]; unsigned int i_start; unsigned int i_stop; unsigned int i; p_subtitle->i_start = 0; p_subtitle->i_stop = 0; p_subtitle->i_vobsub_location = 0; p_subtitle->psz_text = NULL; for( ;; ) { if( ( s = text_get_line( txt ) ) == NULL ) { return( VLC_EGENERIC ); } i_start = 0; i_stop = 0; memset( buffer_text, '\0', MAX_LINE ); if( sscanf( s, "{%d}{}%[^\r\n]", &i_start, buffer_text ) == 2 || sscanf( s, "{%d}{%d}%[^\r\n]", &i_start, &i_stop, buffer_text ) == 3) { break; } } /* replace | by \n */ for( i = 0; i < strlen( buffer_text ); i++ ) { if( buffer_text[i] == '|' ) { buffer_text[i] = '\n'; } } p_subtitle->i_start = (mtime_t)i_start * (mtime_t)i_microsecperframe; p_subtitle->i_stop = (mtime_t)i_stop * (mtime_t)i_microsecperframe; p_subtitle->psz_text = strndup( buffer_text, MAX_LINE ); return( 0 );}static int sub_SubRipRead( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe ){ /* * n * h1:m1:s1,d1 --> h2:m2:s2,d2 * Line1 * Line2 * ... * [empty line] * */ char *s; char buffer_text[ 10 * MAX_LINE]; int i_buffer_text; mtime_t i_start; mtime_t i_stop; p_subtitle->i_start = 0; p_subtitle->i_stop = 0; p_subtitle->i_vobsub_location = 0; p_subtitle->psz_text = NULL; for( ;; ) { int h1, m1, s1, d1, h2, m2, s2, d2; if( ( s = text_get_line( txt ) ) == NULL ) { return( VLC_EGENERIC ); } if( sscanf( s, "%d:%d:%d,%d --> %d:%d:%d,%d", &h1, &m1, &s1, &d1, &h2, &m2, &s2, &d2 ) == 8 ) { i_start = ( (mtime_t)h1 * 3600*1000 + (mtime_t)m1 * 60*1000 + (mtime_t)s1 * 1000 + (mtime_t)d1 ) * 1000; i_stop = ( (mtime_t)h2 * 3600*1000 + (mtime_t)m2 * 60*1000 + (mtime_t)s2 * 1000 + (mtime_t)d2 ) * 1000; /* Now read text until an empty line */ for( i_buffer_text = 0;; ) { int i_len;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?