📄 subtitle.c
字号:
{ es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','u','b','t' ) ); } if( p_sys->psz_header != NULL ) { fmt.i_extra = strlen( p_sys->psz_header ) + 1; fmt.p_extra = strdup( p_sys->psz_header ); } p_sys->es = es_out_Add( p_demux->out, &fmt ); return VLC_SUCCESS;}/***************************************************************************** * Close: Close subtitle demux *****************************************************************************/static void Close( vlc_object_t *p_this ){ demux_t *p_demux = (demux_t*)p_this; demux_sys_t *p_sys = p_demux->p_sys; int i; for( i = 0; i < p_sys->i_subtitles; i++ ) free( p_sys->subtitle[i].psz_text ); free( p_sys->subtitle ); free( p_sys );}/***************************************************************************** * Control: *****************************************************************************/static int Control( demux_t *p_demux, int i_query, va_list args ){ demux_sys_t *p_sys = p_demux->p_sys; int64_t *pi64, i64; double *pf, f; switch( i_query ) { case DEMUX_GET_LENGTH: pi64 = (int64_t*)va_arg( args, int64_t * ); *pi64 = p_sys->i_length; return VLC_SUCCESS; case DEMUX_GET_TIME: pi64 = (int64_t*)va_arg( args, int64_t * ); if( p_sys->i_subtitle < p_sys->i_subtitles ) { *pi64 = p_sys->subtitle[p_sys->i_subtitle].i_start; return VLC_SUCCESS; } return VLC_EGENERIC; case DEMUX_SET_TIME: i64 = (int64_t)va_arg( args, int64_t ); p_sys->i_subtitle = 0; while( p_sys->i_subtitle < p_sys->i_subtitles && p_sys->subtitle[p_sys->i_subtitle].i_start < i64 ) { p_sys->i_subtitle++; } if( p_sys->i_subtitle >= p_sys->i_subtitles ) return VLC_EGENERIC; return VLC_SUCCESS; case DEMUX_GET_POSITION: pf = (double*)va_arg( args, double * ); if( p_sys->i_subtitle >= p_sys->i_subtitles ) { *pf = 1.0; } else if( p_sys->i_subtitles > 0 ) { *pf = (double)p_sys->subtitle[p_sys->i_subtitle].i_start / (double)p_sys->i_length; } else { *pf = 0.0; } return VLC_SUCCESS; case DEMUX_SET_POSITION: f = (double)va_arg( args, double ); i64 = f * p_sys->i_length; p_sys->i_subtitle = 0; while( p_sys->i_subtitle < p_sys->i_subtitles && p_sys->subtitle[p_sys->i_subtitle].i_start < i64 ) { p_sys->i_subtitle++; } if( p_sys->i_subtitle >= p_sys->i_subtitles ) return VLC_EGENERIC; return VLC_SUCCESS; case DEMUX_SET_NEXT_DEMUX_TIME: p_sys->i_next_demux_date = (int64_t)va_arg( args, int64_t ); return VLC_SUCCESS; case DEMUX_GET_FPS: case DEMUX_GET_META: case DEMUX_GET_ATTACHMENTS: case DEMUX_GET_TITLE_INFO: case DEMUX_HAS_UNSUPPORTED_META: return VLC_EGENERIC; default: msg_Err( p_demux, "unknown query %d in subtitle control", i_query ); return VLC_EGENERIC; }}/***************************************************************************** * Demux: Send subtitle to decoder *****************************************************************************/static int Demux( demux_t *p_demux ){ demux_sys_t *p_sys = p_demux->p_sys; int64_t i_maxdate; if( p_sys->i_subtitle >= p_sys->i_subtitles ) return 0; i_maxdate = p_sys->i_next_demux_date - var_GetTime( p_demux->p_parent, "spu-delay" );; if( i_maxdate <= 0 && p_sys->i_subtitle < p_sys->i_subtitles ) { /* Should not happen */ i_maxdate = p_sys->subtitle[p_sys->i_subtitle].i_start + 1; } while( p_sys->i_subtitle < p_sys->i_subtitles && p_sys->subtitle[p_sys->i_subtitle].i_start < i_maxdate ) { block_t *p_block; int i_len = strlen( p_sys->subtitle[p_sys->i_subtitle].psz_text ) + 1; if( i_len <= 1 ) { /* empty subtitle */ p_sys->i_subtitle++; continue; } if( ( p_block = block_New( p_demux, i_len ) ) == NULL ) { p_sys->i_subtitle++; continue; } if( p_sys->subtitle[p_sys->i_subtitle].i_start < 0 ) { p_sys->i_subtitle++; continue; } p_block->i_pts = p_sys->subtitle[p_sys->i_subtitle].i_start; p_block->i_dts = p_block->i_pts; if( p_sys->subtitle[p_sys->i_subtitle].i_stop > 0 ) { p_block->i_length = p_sys->subtitle[p_sys->i_subtitle].i_stop - p_block->i_pts; } memcpy( p_block->p_buffer, p_sys->subtitle[p_sys->i_subtitle].psz_text, i_len ); if( p_block->i_pts > 0 ) { es_out_Send( p_demux->out, p_sys->es, p_block ); } else { block_Release( p_block ); } p_sys->i_subtitle++; } /* */ p_sys->i_next_demux_date = 0; return 1;}/***************************************************************************** * Fix: fix time stamp and order of subtitle *****************************************************************************/#ifdef USE_THIS_UNUSED_PIECE_OF_CODEstatic void Fix( demux_t *p_demux ){ demux_sys_t *p_sys = p_demux->p_sys; bool b_done; int i_index; /* *** fix order (to be sure...) *** */ /* We suppose that there are near in order and this durty bubble sort * wont take too much time */ do { b_done = true; for( i_index = 1; i_index < p_sys->i_subtitles; i_index++ ) { if( p_sys->subtitle[i_index].i_start < p_sys->subtitle[i_index - 1].i_start ) { subtitle_t sub_xch; memcpy( &sub_xch, p_sys->subtitle + i_index - 1, sizeof( subtitle_t ) ); memcpy( p_sys->subtitle + i_index - 1, p_sys->subtitle + i_index, sizeof( subtitle_t ) ); memcpy( p_sys->subtitle + i_index, &sub_xch, sizeof( subtitle_t ) ); b_done = false; } } } while( !b_done );}#endifstatic int TextLoad( text_t *txt, stream_t *s ){ int i_line_max; /* init txt */ i_line_max = 500; txt->i_line_count = 0; txt->i_line = 0; txt->line = calloc( i_line_max, sizeof( char * ) ); /* load the complete file */ for( ;; ) { char *psz = stream_ReadLine( s ); if( psz == NULL ) break; txt->line[txt->i_line_count++] = psz; if( txt->i_line_count >= i_line_max ) { i_line_max += 100; txt->line = realloc( txt->line, i_line_max * sizeof( char * ) ); } } if( txt->i_line_count <= 0 ) { free( txt->line ); return VLC_EGENERIC; } return VLC_SUCCESS;}static void TextUnload( text_t *txt ){ int i; for( i = 0; i < txt->i_line_count; i++ ) { free( txt->line[i] ); } free( txt->line ); txt->i_line = 0; txt->i_line_count = 0;}static char *TextGetLine( text_t *txt ){ if( txt->i_line >= txt->i_line_count ) return( NULL ); return txt->line[txt->i_line++];}static void TextPreviousLine( text_t *txt ){ if( txt->i_line > 0 ) txt->i_line--;}/***************************************************************************** * Specific Subtitle function *****************************************************************************//* ParseMicroDvd: * Format: * {n1}{n2}Line1|Line2|Line3.... * where n1 and n2 are the video frame number (n2 can be empty) */static int ParseMicroDvd( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx ){ VLC_UNUSED( i_idx ); demux_sys_t *p_sys = p_demux->p_sys; text_t *txt = &p_sys->txt; char *psz_text; int i_start; int i_stop; int i; for( ;; ) { const char *s = TextGetLine( txt ); if( !s ) return VLC_EGENERIC; psz_text = malloc( strlen(s) + 1 ); if( !psz_text ) return VLC_ENOMEM; i_start = 0; i_stop = 0; if( sscanf( s, "{%d}{}%[^\r\n]", &i_start, psz_text ) == 2 || sscanf( s, "{%d}{%d}%[^\r\n]", &i_start, &i_stop, psz_text ) == 3) { float f_fps; if( i_start != 1 || i_stop != 1 ) break; /* We found a possible setting of the framerate "{1}{1}23.976" */ /* Check if it's usable, and if the sub-fps is not set */ f_fps = us_strtod( psz_text, NULL ); if( f_fps > 0.0 && var_GetFloat( p_demux, "sub-fps" ) <= 0.0 ) p_sys->i_microsecperframe = (int64_t)((float)1000000 / f_fps); } free( psz_text ); } /* replace | by \n */ for( i = 0; psz_text[i] != '\0'; i++ ) { if( psz_text[i] == '|' ) psz_text[i] = '\n'; } /* */ p_subtitle->i_start = i_start * p_sys->i_microsecperframe; p_subtitle->i_stop = i_stop * p_sys->i_microsecperframe; p_subtitle->psz_text = psz_text; return VLC_SUCCESS;}/* ParseSubRipSubViewer * Format SubRip * n * h1:m1:s1,d1 --> h2:m2:s2,d2 * Line1 * Line2 * .... * [Empty line] * Format SubViewer v1/v2 * h1:m1:s1.d1,h2:m2:s2.d2 * Line1[br]Line2 * Line3 * ... * [empty line] * We ignore line number for SubRip */static int ParseSubRipSubViewer( demux_t *p_demux, subtitle_t *p_subtitle, const char *psz_fmt, bool b_replace_br ){ demux_sys_t *p_sys = p_demux->p_sys; text_t *txt = &p_sys->txt; char *psz_text; for( ;; ) { const char *s = TextGetLine( txt ); int h1, m1, s1, d1, h2, m2, s2, d2; if( !s ) return VLC_EGENERIC; if( sscanf( s, psz_fmt, &h1, &m1, &s1, &d1, &h2, &m2, &s2, &d2 ) == 8 ) { p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 + (int64_t)m1 * 60*1000 + (int64_t)s1 * 1000 + (int64_t)d1 ) * 1000; p_subtitle->i_stop = ( (int64_t)h2 * 3600*1000 + (int64_t)m2 * 60*1000 + (int64_t)s2 * 1000 + (int64_t)d2 ) * 1000; break; } } /* Now read text until an empty line */ psz_text = strdup(""); if( !psz_text ) return VLC_ENOMEM; for( ;; ) { const char *s = TextGetLine( txt ); int i_len; int i_old; if( !s ) { free( psz_text ); return VLC_EGENERIC; } i_len = strlen( s ); if( i_len <= 0 ) { p_subtitle->psz_text = psz_text; return VLC_SUCCESS; } i_old = strlen( psz_text ); psz_text = realloc( psz_text, i_old + i_len + 1 + 1 ); if( !psz_text ) return VLC_ENOMEM; strcat( psz_text, s ); strcat( psz_text, "\n" ); /* replace [br] by \n */ if( b_replace_br ) { char *p; while( ( p = strstr( psz_text, "[br]" ) ) ) { *p++ = '\n'; memmove( p, &p[3], strlen(&p[3])+1 ); } } }}/* ParseSubRip */static int ParseSubRip( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx ){ VLC_UNUSED( i_idx ); return ParseSubRipSubViewer( p_demux, p_subtitle, "%d:%d:%d,%d --> %d:%d:%d,%d", false );}/* ParseSubViewer */static int ParseSubViewer( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx ){ VLC_UNUSED( i_idx ); return ParseSubRipSubViewer( p_demux, p_subtitle, "%d:%d:%d.%d,%d:%d:%d.%d", true );}/* ParseSSA */static int ParseSSA( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx ){ demux_sys_t *p_sys = p_demux->p_sys; text_t *txt = &p_sys->txt; for( ;; ) { const char *s = TextGetLine( txt ); int h1, m1, s1, c1, h2, m2, s2, c2; char *psz_text; char temp[16]; if( !s ) return VLC_EGENERIC; /* We expect (SSA2-4): * Format: Marked, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text * Dialogue: Marked=0,0:02:40.65,0:02:41.79,Wolf main,Cher,0000,0000,0000,,Et les enregistrements de ses ondes delta ? * * SSA-1 is similar but only has 8 commas up untill the subtitle text. Probably the Effect field is no present, but not 100 % sure. */ /* For ASS: * Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text * Dialogue: Layer#,0:02:40.65,0:02:41.79,Wolf main,Cher,0000,0000,0000,,Et les enregistrements de ses ondes delta ? */ /* The output text is - at least, not removing numbers - 18 chars shorter than the input text. */ psz_text = malloc( strlen(s) ); if( !psz_text ) return VLC_ENOMEM; if( sscanf( s, "Dialogue: %15[^,],%d:%d:%d.%d,%d:%d:%d.%d,%[^\r\n]", temp, &h1, &m1, &s1, &c1, &h2, &m2, &s2, &c2, psz_text ) == 10 ) { /* The dec expects: ReadOrder, Layer, Style, Name, MarginL, MarginR, MarginV, Effect, Text */ /* (Layer comes from ASS specs ... it's empty for SSA.) */ if( p_sys->i_type == SUB_TYPE_SSA1 ) { /* SSA1 has only 8 commas before the text starts, not 9 */ memmove( &psz_text[1], psz_text, strlen(psz_text)+1 ); psz_text[0] = ',';
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -